Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | /* |
| 28 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
| 29 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 30 | /* Notes: |
| 31 | * |
| 32 | * We store bo pointer in drm_mm_node struct so we know which bo own a |
| 33 | * specific node. There is no protection on the pointer, thus to make |
| 34 | * sure things don't go berserk you have to access this pointer while |
| 35 | * holding the global lru lock and make sure anytime you free a node you |
| 36 | * reset the pointer to NULL. |
| 37 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 38 | |
| 39 | #include "ttm/ttm_module.h" |
| 40 | #include "ttm/ttm_bo_driver.h" |
| 41 | #include "ttm/ttm_placement.h" |
| 42 | #include <linux/jiffies.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/sched.h> |
| 45 | #include <linux/mm.h> |
| 46 | #include <linux/file.h> |
| 47 | #include <linux/module.h> |
| 48 | |
| 49 | #define TTM_ASSERT_LOCKED(param) |
| 50 | #define TTM_DEBUG(fmt, arg...) |
| 51 | #define TTM_BO_HASH_ORDER 13 |
| 52 | |
| 53 | static int ttm_bo_setup_vm(struct ttm_buffer_object *bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 54 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 55 | static void ttm_bo_global_kobj_release(struct kobject *kobj); |
| 56 | |
| 57 | static struct attribute ttm_bo_count = { |
| 58 | .name = "bo_count", |
| 59 | .mode = S_IRUGO |
| 60 | }; |
| 61 | |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 62 | static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type) |
| 63 | { |
| 64 | int i; |
| 65 | |
| 66 | for (i = 0; i <= TTM_PL_PRIV5; i++) |
| 67 | if (flags & (1 << i)) { |
| 68 | *mem_type = i; |
| 69 | return 0; |
| 70 | } |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
| 74 | static void ttm_mem_type_manager_debug(struct ttm_bo_global *glob, |
| 75 | struct ttm_mem_type_manager *man) |
| 76 | { |
| 77 | printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type); |
| 78 | printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type); |
| 79 | printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags); |
| 80 | printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset); |
| 81 | printk(KERN_ERR TTM_PFX " io_offset: 0x%08lX\n", man->io_offset); |
| 82 | printk(KERN_ERR TTM_PFX " io_size: %ld\n", man->io_size); |
Jerome Glisse | eb6d2c3 | 2009-12-10 16:15:52 +0100 | [diff] [blame^] | 83 | printk(KERN_ERR TTM_PFX " size: %llu\n", man->size); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 84 | printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n", |
| 85 | man->available_caching); |
| 86 | printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n", |
| 87 | man->default_caching); |
| 88 | spin_lock(&glob->lru_lock); |
| 89 | drm_mm_debug_table(&man->manager, TTM_PFX); |
| 90 | spin_unlock(&glob->lru_lock); |
| 91 | } |
| 92 | |
| 93 | static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, |
| 94 | struct ttm_placement *placement) |
| 95 | { |
| 96 | struct ttm_bo_device *bdev = bo->bdev; |
| 97 | struct ttm_bo_global *glob = bo->glob; |
| 98 | struct ttm_mem_type_manager *man; |
| 99 | int i, ret, mem_type; |
| 100 | |
Jerome Glisse | eb6d2c3 | 2009-12-10 16:15:52 +0100 | [diff] [blame^] | 101 | printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n", |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 102 | bo, bo->mem.num_pages, bo->mem.size >> 10, |
| 103 | bo->mem.size >> 20); |
| 104 | for (i = 0; i < placement->num_placement; i++) { |
| 105 | ret = ttm_mem_type_from_flags(placement->placement[i], |
| 106 | &mem_type); |
| 107 | if (ret) |
| 108 | return; |
| 109 | man = &bdev->man[mem_type]; |
| 110 | printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n", |
| 111 | i, placement->placement[i], mem_type); |
| 112 | ttm_mem_type_manager_debug(glob, man); |
| 113 | } |
| 114 | } |
| 115 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 116 | static ssize_t ttm_bo_global_show(struct kobject *kobj, |
| 117 | struct attribute *attr, |
| 118 | char *buffer) |
| 119 | { |
| 120 | struct ttm_bo_global *glob = |
| 121 | container_of(kobj, struct ttm_bo_global, kobj); |
| 122 | |
| 123 | return snprintf(buffer, PAGE_SIZE, "%lu\n", |
| 124 | (unsigned long) atomic_read(&glob->bo_count)); |
| 125 | } |
| 126 | |
| 127 | static struct attribute *ttm_bo_global_attrs[] = { |
| 128 | &ttm_bo_count, |
| 129 | NULL |
| 130 | }; |
| 131 | |
| 132 | static struct sysfs_ops ttm_bo_global_ops = { |
| 133 | .show = &ttm_bo_global_show |
| 134 | }; |
| 135 | |
| 136 | static struct kobj_type ttm_bo_glob_kobj_type = { |
| 137 | .release = &ttm_bo_global_kobj_release, |
| 138 | .sysfs_ops = &ttm_bo_global_ops, |
| 139 | .default_attrs = ttm_bo_global_attrs |
| 140 | }; |
| 141 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 142 | |
| 143 | static inline uint32_t ttm_bo_type_flags(unsigned type) |
| 144 | { |
| 145 | return 1 << (type); |
| 146 | } |
| 147 | |
| 148 | static void ttm_bo_release_list(struct kref *list_kref) |
| 149 | { |
| 150 | struct ttm_buffer_object *bo = |
| 151 | container_of(list_kref, struct ttm_buffer_object, list_kref); |
| 152 | struct ttm_bo_device *bdev = bo->bdev; |
| 153 | |
| 154 | BUG_ON(atomic_read(&bo->list_kref.refcount)); |
| 155 | BUG_ON(atomic_read(&bo->kref.refcount)); |
| 156 | BUG_ON(atomic_read(&bo->cpu_writers)); |
| 157 | BUG_ON(bo->sync_obj != NULL); |
| 158 | BUG_ON(bo->mem.mm_node != NULL); |
| 159 | BUG_ON(!list_empty(&bo->lru)); |
| 160 | BUG_ON(!list_empty(&bo->ddestroy)); |
| 161 | |
| 162 | if (bo->ttm) |
| 163 | ttm_tt_destroy(bo->ttm); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 164 | atomic_dec(&bo->glob->bo_count); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 165 | if (bo->destroy) |
| 166 | bo->destroy(bo); |
| 167 | else { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 168 | ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 169 | kfree(bo); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible) |
| 174 | { |
| 175 | |
| 176 | if (interruptible) { |
| 177 | int ret = 0; |
| 178 | |
| 179 | ret = wait_event_interruptible(bo->event_queue, |
| 180 | atomic_read(&bo->reserved) == 0); |
| 181 | if (unlikely(ret != 0)) |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 182 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 183 | } else { |
| 184 | wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0); |
| 185 | } |
| 186 | return 0; |
| 187 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 188 | EXPORT_SYMBOL(ttm_bo_wait_unreserved); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 189 | |
| 190 | static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) |
| 191 | { |
| 192 | struct ttm_bo_device *bdev = bo->bdev; |
| 193 | struct ttm_mem_type_manager *man; |
| 194 | |
| 195 | BUG_ON(!atomic_read(&bo->reserved)); |
| 196 | |
| 197 | if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { |
| 198 | |
| 199 | BUG_ON(!list_empty(&bo->lru)); |
| 200 | |
| 201 | man = &bdev->man[bo->mem.mem_type]; |
| 202 | list_add_tail(&bo->lru, &man->lru); |
| 203 | kref_get(&bo->list_kref); |
| 204 | |
| 205 | if (bo->ttm != NULL) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 206 | list_add_tail(&bo->swap, &bo->glob->swap_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 207 | kref_get(&bo->list_kref); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Call with the lru_lock held. |
| 214 | */ |
| 215 | |
| 216 | static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo) |
| 217 | { |
| 218 | int put_count = 0; |
| 219 | |
| 220 | if (!list_empty(&bo->swap)) { |
| 221 | list_del_init(&bo->swap); |
| 222 | ++put_count; |
| 223 | } |
| 224 | if (!list_empty(&bo->lru)) { |
| 225 | list_del_init(&bo->lru); |
| 226 | ++put_count; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * TODO: Add a driver hook to delete from |
| 231 | * driver-specific LRU's here. |
| 232 | */ |
| 233 | |
| 234 | return put_count; |
| 235 | } |
| 236 | |
| 237 | int ttm_bo_reserve_locked(struct ttm_buffer_object *bo, |
| 238 | bool interruptible, |
| 239 | bool no_wait, bool use_sequence, uint32_t sequence) |
| 240 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 241 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 242 | int ret; |
| 243 | |
| 244 | while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) { |
| 245 | if (use_sequence && bo->seq_valid && |
| 246 | (sequence - bo->val_seq < (1 << 31))) { |
| 247 | return -EAGAIN; |
| 248 | } |
| 249 | |
| 250 | if (no_wait) |
| 251 | return -EBUSY; |
| 252 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 253 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 254 | ret = ttm_bo_wait_unreserved(bo, interruptible); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 255 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 256 | |
| 257 | if (unlikely(ret)) |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | if (use_sequence) { |
| 262 | bo->val_seq = sequence; |
| 263 | bo->seq_valid = true; |
| 264 | } else { |
| 265 | bo->seq_valid = false; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | EXPORT_SYMBOL(ttm_bo_reserve); |
| 271 | |
| 272 | static void ttm_bo_ref_bug(struct kref *list_kref) |
| 273 | { |
| 274 | BUG(); |
| 275 | } |
| 276 | |
| 277 | int ttm_bo_reserve(struct ttm_buffer_object *bo, |
| 278 | bool interruptible, |
| 279 | bool no_wait, bool use_sequence, uint32_t sequence) |
| 280 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 281 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 282 | int put_count = 0; |
| 283 | int ret; |
| 284 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 285 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 286 | ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence, |
| 287 | sequence); |
| 288 | if (likely(ret == 0)) |
| 289 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 290 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 291 | |
| 292 | while (put_count--) |
| 293 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | void ttm_bo_unreserve(struct ttm_buffer_object *bo) |
| 299 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 300 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 301 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 302 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 303 | ttm_bo_add_to_lru(bo); |
| 304 | atomic_set(&bo->reserved, 0); |
| 305 | wake_up_all(&bo->event_queue); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 306 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 307 | } |
| 308 | EXPORT_SYMBOL(ttm_bo_unreserve); |
| 309 | |
| 310 | /* |
| 311 | * Call bo->mutex locked. |
| 312 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 313 | static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) |
| 314 | { |
| 315 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 316 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 317 | int ret = 0; |
| 318 | uint32_t page_flags = 0; |
| 319 | |
| 320 | TTM_ASSERT_LOCKED(&bo->mutex); |
| 321 | bo->ttm = NULL; |
| 322 | |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 323 | if (bdev->need_dma32) |
| 324 | page_flags |= TTM_PAGE_FLAG_DMA32; |
| 325 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 326 | switch (bo->type) { |
| 327 | case ttm_bo_type_device: |
| 328 | if (zero_alloc) |
| 329 | page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; |
| 330 | case ttm_bo_type_kernel: |
| 331 | bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 332 | page_flags, glob->dummy_read_page); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 333 | if (unlikely(bo->ttm == NULL)) |
| 334 | ret = -ENOMEM; |
| 335 | break; |
| 336 | case ttm_bo_type_user: |
| 337 | bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
| 338 | page_flags | TTM_PAGE_FLAG_USER, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 339 | glob->dummy_read_page); |
Dave Airlie | 447aeb9 | 2009-12-08 09:25:45 +1000 | [diff] [blame] | 340 | if (unlikely(bo->ttm == NULL)) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 341 | ret = -ENOMEM; |
Dave Airlie | 447aeb9 | 2009-12-08 09:25:45 +1000 | [diff] [blame] | 342 | break; |
| 343 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 344 | |
| 345 | ret = ttm_tt_set_user(bo->ttm, current, |
| 346 | bo->buffer_start, bo->num_pages); |
| 347 | if (unlikely(ret != 0)) |
| 348 | ttm_tt_destroy(bo->ttm); |
| 349 | break; |
| 350 | default: |
| 351 | printk(KERN_ERR TTM_PFX "Illegal buffer object type\n"); |
| 352 | ret = -EINVAL; |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, |
| 360 | struct ttm_mem_reg *mem, |
| 361 | bool evict, bool interruptible, bool no_wait) |
| 362 | { |
| 363 | struct ttm_bo_device *bdev = bo->bdev; |
| 364 | bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); |
| 365 | bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); |
| 366 | struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; |
| 367 | struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; |
| 368 | int ret = 0; |
| 369 | |
| 370 | if (old_is_pci || new_is_pci || |
| 371 | ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) |
| 372 | ttm_bo_unmap_virtual(bo); |
| 373 | |
| 374 | /* |
| 375 | * Create and bind a ttm if required. |
| 376 | */ |
| 377 | |
| 378 | if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) { |
| 379 | ret = ttm_bo_add_ttm(bo, false); |
| 380 | if (ret) |
| 381 | goto out_err; |
| 382 | |
| 383 | ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); |
| 384 | if (ret) |
Thomas Hellstrom | 87ef920 | 2009-06-17 12:29:57 +0200 | [diff] [blame] | 385 | goto out_err; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 386 | |
| 387 | if (mem->mem_type != TTM_PL_SYSTEM) { |
| 388 | ret = ttm_tt_bind(bo->ttm, mem); |
| 389 | if (ret) |
| 390 | goto out_err; |
| 391 | } |
| 392 | |
| 393 | if (bo->mem.mem_type == TTM_PL_SYSTEM) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 394 | bo->mem = *mem; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 395 | mem->mm_node = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 396 | goto moved; |
| 397 | } |
| 398 | |
| 399 | } |
| 400 | |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 401 | if (bdev->driver->move_notify) |
| 402 | bdev->driver->move_notify(bo, mem); |
| 403 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 404 | if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && |
| 405 | !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) |
| 406 | ret = ttm_bo_move_ttm(bo, evict, no_wait, mem); |
| 407 | else if (bdev->driver->move) |
| 408 | ret = bdev->driver->move(bo, evict, interruptible, |
| 409 | no_wait, mem); |
| 410 | else |
| 411 | ret = ttm_bo_move_memcpy(bo, evict, no_wait, mem); |
| 412 | |
| 413 | if (ret) |
| 414 | goto out_err; |
| 415 | |
| 416 | moved: |
| 417 | if (bo->evicted) { |
| 418 | ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); |
| 419 | if (ret) |
| 420 | printk(KERN_ERR TTM_PFX "Can not flush read caches\n"); |
| 421 | bo->evicted = false; |
| 422 | } |
| 423 | |
| 424 | if (bo->mem.mm_node) { |
| 425 | spin_lock(&bo->lock); |
| 426 | bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) + |
| 427 | bdev->man[bo->mem.mem_type].gpu_offset; |
| 428 | bo->cur_placement = bo->mem.placement; |
| 429 | spin_unlock(&bo->lock); |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | |
| 434 | out_err: |
| 435 | new_man = &bdev->man[bo->mem.mem_type]; |
| 436 | if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) { |
| 437 | ttm_tt_unbind(bo->ttm); |
| 438 | ttm_tt_destroy(bo->ttm); |
| 439 | bo->ttm = NULL; |
| 440 | } |
| 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * If bo idle, remove from delayed- and lru lists, and unref. |
| 447 | * If not idle, and already on delayed list, do nothing. |
| 448 | * If not idle, and not on delayed list, put on delayed list, |
| 449 | * up the list_kref and schedule a delayed list check. |
| 450 | */ |
| 451 | |
| 452 | static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all) |
| 453 | { |
| 454 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 455 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 456 | struct ttm_bo_driver *driver = bdev->driver; |
| 457 | int ret; |
| 458 | |
| 459 | spin_lock(&bo->lock); |
| 460 | (void) ttm_bo_wait(bo, false, false, !remove_all); |
| 461 | |
| 462 | if (!bo->sync_obj) { |
| 463 | int put_count; |
| 464 | |
| 465 | spin_unlock(&bo->lock); |
| 466 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 467 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | aaa2073 | 2009-12-02 18:33:45 +0100 | [diff] [blame] | 468 | put_count = ttm_bo_del_from_lru(bo); |
| 469 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 470 | ret = ttm_bo_reserve_locked(bo, false, false, false, 0); |
| 471 | BUG_ON(ret); |
| 472 | if (bo->ttm) |
| 473 | ttm_tt_unbind(bo->ttm); |
| 474 | |
| 475 | if (!list_empty(&bo->ddestroy)) { |
| 476 | list_del_init(&bo->ddestroy); |
Thomas Hellstrom | aaa2073 | 2009-12-02 18:33:45 +0100 | [diff] [blame] | 477 | ++put_count; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 478 | } |
| 479 | if (bo->mem.mm_node) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 480 | bo->mem.mm_node->private = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 481 | drm_mm_put_block(bo->mem.mm_node); |
| 482 | bo->mem.mm_node = NULL; |
| 483 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 484 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 485 | |
| 486 | atomic_set(&bo->reserved, 0); |
| 487 | |
| 488 | while (put_count--) |
Thomas Hellstrom | aaa2073 | 2009-12-02 18:33:45 +0100 | [diff] [blame] | 489 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 494 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 495 | if (list_empty(&bo->ddestroy)) { |
| 496 | void *sync_obj = bo->sync_obj; |
| 497 | void *sync_obj_arg = bo->sync_obj_arg; |
| 498 | |
| 499 | kref_get(&bo->list_kref); |
| 500 | list_add_tail(&bo->ddestroy, &bdev->ddestroy); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 501 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 502 | spin_unlock(&bo->lock); |
| 503 | |
| 504 | if (sync_obj) |
| 505 | driver->sync_obj_flush(sync_obj, sync_obj_arg); |
| 506 | schedule_delayed_work(&bdev->wq, |
| 507 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 508 | ret = 0; |
| 509 | |
| 510 | } else { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 511 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 512 | spin_unlock(&bo->lock); |
| 513 | ret = -EBUSY; |
| 514 | } |
| 515 | |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Traverse the delayed list, and call ttm_bo_cleanup_refs on all |
| 521 | * encountered buffers. |
| 522 | */ |
| 523 | |
| 524 | static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) |
| 525 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 526 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 527 | struct ttm_buffer_object *entry, *nentry; |
| 528 | struct list_head *list, *next; |
| 529 | int ret; |
| 530 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 531 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 532 | list_for_each_safe(list, next, &bdev->ddestroy) { |
| 533 | entry = list_entry(list, struct ttm_buffer_object, ddestroy); |
| 534 | nentry = NULL; |
| 535 | |
| 536 | /* |
| 537 | * Protect the next list entry from destruction while we |
| 538 | * unlock the lru_lock. |
| 539 | */ |
| 540 | |
| 541 | if (next != &bdev->ddestroy) { |
| 542 | nentry = list_entry(next, struct ttm_buffer_object, |
| 543 | ddestroy); |
| 544 | kref_get(&nentry->list_kref); |
| 545 | } |
| 546 | kref_get(&entry->list_kref); |
| 547 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 548 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 549 | ret = ttm_bo_cleanup_refs(entry, remove_all); |
| 550 | kref_put(&entry->list_kref, ttm_bo_release_list); |
| 551 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 552 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 553 | if (nentry) { |
| 554 | bool next_onlist = !list_empty(next); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 555 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 556 | kref_put(&nentry->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 557 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 558 | /* |
| 559 | * Someone might have raced us and removed the |
| 560 | * next entry from the list. We don't bother restarting |
| 561 | * list traversal. |
| 562 | */ |
| 563 | |
| 564 | if (!next_onlist) |
| 565 | break; |
| 566 | } |
| 567 | if (ret) |
| 568 | break; |
| 569 | } |
| 570 | ret = !list_empty(&bdev->ddestroy); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 571 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 572 | |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | static void ttm_bo_delayed_workqueue(struct work_struct *work) |
| 577 | { |
| 578 | struct ttm_bo_device *bdev = |
| 579 | container_of(work, struct ttm_bo_device, wq.work); |
| 580 | |
| 581 | if (ttm_bo_delayed_delete(bdev, false)) { |
| 582 | schedule_delayed_work(&bdev->wq, |
| 583 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static void ttm_bo_release(struct kref *kref) |
| 588 | { |
| 589 | struct ttm_buffer_object *bo = |
| 590 | container_of(kref, struct ttm_buffer_object, kref); |
| 591 | struct ttm_bo_device *bdev = bo->bdev; |
| 592 | |
| 593 | if (likely(bo->vm_node != NULL)) { |
| 594 | rb_erase(&bo->vm_rb, &bdev->addr_space_rb); |
| 595 | drm_mm_put_block(bo->vm_node); |
| 596 | bo->vm_node = NULL; |
| 597 | } |
| 598 | write_unlock(&bdev->vm_lock); |
| 599 | ttm_bo_cleanup_refs(bo, false); |
| 600 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 601 | write_lock(&bdev->vm_lock); |
| 602 | } |
| 603 | |
| 604 | void ttm_bo_unref(struct ttm_buffer_object **p_bo) |
| 605 | { |
| 606 | struct ttm_buffer_object *bo = *p_bo; |
| 607 | struct ttm_bo_device *bdev = bo->bdev; |
| 608 | |
| 609 | *p_bo = NULL; |
| 610 | write_lock(&bdev->vm_lock); |
| 611 | kref_put(&bo->kref, ttm_bo_release); |
| 612 | write_unlock(&bdev->vm_lock); |
| 613 | } |
| 614 | EXPORT_SYMBOL(ttm_bo_unref); |
| 615 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 616 | static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible, |
| 617 | bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 618 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 619 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 620 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 621 | struct ttm_mem_reg evict_mem; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 622 | struct ttm_placement placement; |
| 623 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 624 | |
| 625 | spin_lock(&bo->lock); |
| 626 | ret = ttm_bo_wait(bo, false, interruptible, no_wait); |
| 627 | spin_unlock(&bo->lock); |
| 628 | |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 629 | if (unlikely(ret != 0)) { |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 630 | if (ret != -ERESTARTSYS) { |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 631 | printk(KERN_ERR TTM_PFX |
| 632 | "Failed to expire sync object before " |
| 633 | "buffer eviction.\n"); |
| 634 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 635 | goto out; |
| 636 | } |
| 637 | |
| 638 | BUG_ON(!atomic_read(&bo->reserved)); |
| 639 | |
| 640 | evict_mem = bo->mem; |
| 641 | evict_mem.mm_node = NULL; |
| 642 | |
Jerome Glisse | 7cb7d1d | 2009-12-09 22:14:27 +0100 | [diff] [blame] | 643 | placement.fpfn = 0; |
| 644 | placement.lpfn = 0; |
| 645 | placement.num_placement = 0; |
| 646 | placement.num_busy_placement = 0; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 647 | bdev->driver->evict_flags(bo, &placement); |
| 648 | ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible, |
| 649 | no_wait); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 650 | if (ret) { |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 651 | if (ret != -ERESTARTSYS) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 652 | printk(KERN_ERR TTM_PFX |
| 653 | "Failed to find memory space for " |
| 654 | "buffer 0x%p eviction.\n", bo); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 655 | ttm_bo_mem_space_debug(bo, &placement); |
| 656 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 657 | goto out; |
| 658 | } |
| 659 | |
| 660 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible, |
| 661 | no_wait); |
| 662 | if (ret) { |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 663 | if (ret != -ERESTARTSYS) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 664 | printk(KERN_ERR TTM_PFX "Buffer eviction failed\n"); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 665 | spin_lock(&glob->lru_lock); |
| 666 | if (evict_mem.mm_node) { |
| 667 | evict_mem.mm_node->private = NULL; |
| 668 | drm_mm_put_block(evict_mem.mm_node); |
| 669 | evict_mem.mm_node = NULL; |
| 670 | } |
| 671 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 672 | goto out; |
| 673 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 674 | bo->evicted = true; |
| 675 | out: |
| 676 | return ret; |
| 677 | } |
| 678 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 679 | static int ttm_mem_evict_first(struct ttm_bo_device *bdev, |
| 680 | uint32_t mem_type, |
| 681 | bool interruptible, bool no_wait) |
| 682 | { |
| 683 | struct ttm_bo_global *glob = bdev->glob; |
| 684 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 685 | struct ttm_buffer_object *bo; |
| 686 | int ret, put_count = 0; |
| 687 | |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 688 | retry: |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 689 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 690 | if (list_empty(&man->lru)) { |
| 691 | spin_unlock(&glob->lru_lock); |
| 692 | return -EBUSY; |
| 693 | } |
| 694 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 695 | bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru); |
| 696 | kref_get(&bo->list_kref); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 697 | |
| 698 | ret = ttm_bo_reserve_locked(bo, false, true, false, 0); |
| 699 | |
| 700 | if (unlikely(ret == -EBUSY)) { |
| 701 | spin_unlock(&glob->lru_lock); |
| 702 | if (likely(!no_wait)) |
| 703 | ret = ttm_bo_wait_unreserved(bo, interruptible); |
| 704 | |
| 705 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 706 | |
| 707 | /** |
| 708 | * We *need* to retry after releasing the lru lock. |
| 709 | */ |
| 710 | |
| 711 | if (unlikely(ret != 0)) |
| 712 | return ret; |
| 713 | goto retry; |
| 714 | } |
| 715 | |
| 716 | put_count = ttm_bo_del_from_lru(bo); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 717 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 718 | |
| 719 | BUG_ON(ret != 0); |
| 720 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 721 | while (put_count--) |
| 722 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 723 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 724 | ret = ttm_bo_evict(bo, interruptible, no_wait); |
| 725 | ttm_bo_unreserve(bo); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 726 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 727 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 728 | return ret; |
| 729 | } |
| 730 | |
| 731 | static int ttm_bo_man_get_node(struct ttm_buffer_object *bo, |
| 732 | struct ttm_mem_type_manager *man, |
| 733 | struct ttm_placement *placement, |
| 734 | struct ttm_mem_reg *mem, |
| 735 | struct drm_mm_node **node) |
| 736 | { |
| 737 | struct ttm_bo_global *glob = bo->glob; |
| 738 | unsigned long lpfn; |
| 739 | int ret; |
| 740 | |
| 741 | lpfn = placement->lpfn; |
| 742 | if (!lpfn) |
| 743 | lpfn = man->size; |
| 744 | *node = NULL; |
| 745 | do { |
| 746 | ret = drm_mm_pre_get(&man->manager); |
| 747 | if (unlikely(ret)) |
| 748 | return ret; |
| 749 | |
| 750 | spin_lock(&glob->lru_lock); |
| 751 | *node = drm_mm_search_free_in_range(&man->manager, |
| 752 | mem->num_pages, mem->page_alignment, |
| 753 | placement->fpfn, lpfn, 1); |
| 754 | if (unlikely(*node == NULL)) { |
| 755 | spin_unlock(&glob->lru_lock); |
| 756 | return 0; |
| 757 | } |
| 758 | *node = drm_mm_get_block_atomic_range(*node, mem->num_pages, |
| 759 | mem->page_alignment, |
| 760 | placement->fpfn, |
| 761 | lpfn); |
| 762 | spin_unlock(&glob->lru_lock); |
| 763 | } while (*node == NULL); |
| 764 | return 0; |
| 765 | } |
| 766 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 767 | /** |
| 768 | * Repeatedly evict memory from the LRU for @mem_type until we create enough |
| 769 | * space, or we've evicted everything and there isn't enough space. |
| 770 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 771 | static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, |
| 772 | uint32_t mem_type, |
| 773 | struct ttm_placement *placement, |
| 774 | struct ttm_mem_reg *mem, |
| 775 | bool interruptible, bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 776 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 777 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 778 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 779 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 780 | struct drm_mm_node *node; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 781 | int ret; |
| 782 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 783 | do { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 784 | ret = ttm_bo_man_get_node(bo, man, placement, mem, &node); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 785 | if (unlikely(ret != 0)) |
| 786 | return ret; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 787 | if (node) |
| 788 | break; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 789 | spin_lock(&glob->lru_lock); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 790 | if (list_empty(&man->lru)) { |
| 791 | spin_unlock(&glob->lru_lock); |
| 792 | break; |
| 793 | } |
| 794 | spin_unlock(&glob->lru_lock); |
| 795 | ret = ttm_mem_evict_first(bdev, mem_type, interruptible, |
| 796 | no_wait); |
| 797 | if (unlikely(ret != 0)) |
| 798 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 799 | } while (1); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 800 | if (node == NULL) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 801 | return -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 802 | mem->mm_node = node; |
| 803 | mem->mem_type = mem_type; |
| 804 | return 0; |
| 805 | } |
| 806 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 807 | static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, |
| 808 | uint32_t cur_placement, |
| 809 | uint32_t proposed_placement) |
| 810 | { |
| 811 | uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; |
| 812 | uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; |
| 813 | |
| 814 | /** |
| 815 | * Keep current caching if possible. |
| 816 | */ |
| 817 | |
| 818 | if ((cur_placement & caching) != 0) |
| 819 | result |= (cur_placement & caching); |
| 820 | else if ((man->default_caching & caching) != 0) |
| 821 | result |= man->default_caching; |
| 822 | else if ((TTM_PL_FLAG_CACHED & caching) != 0) |
| 823 | result |= TTM_PL_FLAG_CACHED; |
| 824 | else if ((TTM_PL_FLAG_WC & caching) != 0) |
| 825 | result |= TTM_PL_FLAG_WC; |
| 826 | else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) |
| 827 | result |= TTM_PL_FLAG_UNCACHED; |
| 828 | |
| 829 | return result; |
| 830 | } |
| 831 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 832 | static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, |
| 833 | bool disallow_fixed, |
| 834 | uint32_t mem_type, |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 835 | uint32_t proposed_placement, |
| 836 | uint32_t *masked_placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 837 | { |
| 838 | uint32_t cur_flags = ttm_bo_type_flags(mem_type); |
| 839 | |
| 840 | if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed) |
| 841 | return false; |
| 842 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 843 | if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 844 | return false; |
| 845 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 846 | if ((proposed_placement & man->available_caching) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 847 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 848 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 849 | cur_flags |= (proposed_placement & man->available_caching); |
| 850 | |
| 851 | *masked_placement = cur_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 852 | return true; |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Creates space for memory region @mem according to its type. |
| 857 | * |
| 858 | * This function first searches for free space in compatible memory types in |
| 859 | * the priority order defined by the driver. If free space isn't found, then |
| 860 | * ttm_bo_mem_force_space is attempted in priority order to evict and find |
| 861 | * space. |
| 862 | */ |
| 863 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 864 | struct ttm_placement *placement, |
| 865 | struct ttm_mem_reg *mem, |
| 866 | bool interruptible, bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 867 | { |
| 868 | struct ttm_bo_device *bdev = bo->bdev; |
| 869 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 870 | uint32_t mem_type = TTM_PL_SYSTEM; |
| 871 | uint32_t cur_flags = 0; |
| 872 | bool type_found = false; |
| 873 | bool type_ok = false; |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 874 | bool has_erestartsys = false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 875 | struct drm_mm_node *node = NULL; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 876 | int i, ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 877 | |
| 878 | mem->mm_node = NULL; |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 879 | for (i = 0; i < placement->num_placement; ++i) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 880 | ret = ttm_mem_type_from_flags(placement->placement[i], |
| 881 | &mem_type); |
| 882 | if (ret) |
| 883 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 884 | man = &bdev->man[mem_type]; |
| 885 | |
| 886 | type_ok = ttm_bo_mt_compatible(man, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 887 | bo->type == ttm_bo_type_user, |
| 888 | mem_type, |
| 889 | placement->placement[i], |
| 890 | &cur_flags); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 891 | |
| 892 | if (!type_ok) |
| 893 | continue; |
| 894 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 895 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 896 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 897 | /* |
| 898 | * Use the access and other non-mapping-related flag bits from |
| 899 | * the memory placement flags to the current flags |
| 900 | */ |
| 901 | ttm_flag_masked(&cur_flags, placement->placement[i], |
| 902 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 903 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 904 | if (mem_type == TTM_PL_SYSTEM) |
| 905 | break; |
| 906 | |
| 907 | if (man->has_type && man->use_type) { |
| 908 | type_found = true; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 909 | ret = ttm_bo_man_get_node(bo, man, placement, mem, |
| 910 | &node); |
| 911 | if (unlikely(ret)) |
| 912 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 913 | } |
| 914 | if (node) |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) { |
| 919 | mem->mm_node = node; |
| 920 | mem->mem_type = mem_type; |
| 921 | mem->placement = cur_flags; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 922 | if (node) |
| 923 | node->private = bo; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | if (!type_found) |
| 928 | return -EINVAL; |
| 929 | |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 930 | for (i = 0; i < placement->num_busy_placement; ++i) { |
| 931 | ret = ttm_mem_type_from_flags(placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 932 | &mem_type); |
| 933 | if (ret) |
| 934 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 935 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 936 | if (!man->has_type) |
| 937 | continue; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 938 | if (!ttm_bo_mt_compatible(man, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 939 | bo->type == ttm_bo_type_user, |
| 940 | mem_type, |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 941 | placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 942 | &cur_flags)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 943 | continue; |
| 944 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 945 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 946 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 947 | /* |
| 948 | * Use the access and other non-mapping-related flag bits from |
| 949 | * the memory placement flags to the current flags |
| 950 | */ |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 951 | ttm_flag_masked(&cur_flags, placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 952 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 953 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 954 | ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem, |
| 955 | interruptible, no_wait); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 956 | if (ret == 0 && mem->mm_node) { |
| 957 | mem->placement = cur_flags; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 958 | mem->mm_node->private = bo; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 959 | return 0; |
| 960 | } |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 961 | if (ret == -ERESTARTSYS) |
| 962 | has_erestartsys = true; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 963 | } |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 964 | ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 965 | return ret; |
| 966 | } |
| 967 | EXPORT_SYMBOL(ttm_bo_mem_space); |
| 968 | |
| 969 | int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait) |
| 970 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 971 | if ((atomic_read(&bo->cpu_writers) > 0) && no_wait) |
| 972 | return -EBUSY; |
| 973 | |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 974 | return wait_event_interruptible(bo->event_queue, |
| 975 | atomic_read(&bo->cpu_writers) == 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 976 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 977 | EXPORT_SYMBOL(ttm_bo_wait_cpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 978 | |
| 979 | int ttm_bo_move_buffer(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 980 | struct ttm_placement *placement, |
| 981 | bool interruptible, bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 982 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 983 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 984 | int ret = 0; |
| 985 | struct ttm_mem_reg mem; |
| 986 | |
| 987 | BUG_ON(!atomic_read(&bo->reserved)); |
| 988 | |
| 989 | /* |
| 990 | * FIXME: It's possible to pipeline buffer moves. |
| 991 | * Have the driver move function wait for idle when necessary, |
| 992 | * instead of doing it here. |
| 993 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 994 | spin_lock(&bo->lock); |
| 995 | ret = ttm_bo_wait(bo, false, interruptible, no_wait); |
| 996 | spin_unlock(&bo->lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 997 | if (ret) |
| 998 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 999 | mem.num_pages = bo->num_pages; |
| 1000 | mem.size = mem.num_pages << PAGE_SHIFT; |
| 1001 | mem.page_alignment = bo->mem.page_alignment; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1002 | /* |
| 1003 | * Determine where to move the buffer. |
| 1004 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1005 | ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1006 | if (ret) |
| 1007 | goto out_unlock; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1008 | ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1009 | out_unlock: |
| 1010 | if (ret && mem.mm_node) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1011 | spin_lock(&glob->lru_lock); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1012 | mem.mm_node->private = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1013 | drm_mm_put_block(mem.mm_node); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1014 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1015 | } |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1019 | static int ttm_bo_mem_compat(struct ttm_placement *placement, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1020 | struct ttm_mem_reg *mem) |
| 1021 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1022 | int i; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1023 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1024 | for (i = 0; i < placement->num_placement; i++) { |
| 1025 | if ((placement->placement[i] & mem->placement & |
| 1026 | TTM_PL_MASK_CACHING) && |
| 1027 | (placement->placement[i] & mem->placement & |
| 1028 | TTM_PL_MASK_MEM)) |
| 1029 | return i; |
| 1030 | } |
| 1031 | return -1; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1034 | int ttm_bo_validate(struct ttm_buffer_object *bo, |
| 1035 | struct ttm_placement *placement, |
| 1036 | bool interruptible, bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1037 | { |
| 1038 | int ret; |
| 1039 | |
| 1040 | BUG_ON(!atomic_read(&bo->reserved)); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1041 | /* Check that range is valid */ |
| 1042 | if (placement->lpfn || placement->fpfn) |
| 1043 | if (placement->fpfn > placement->lpfn || |
| 1044 | (placement->lpfn - placement->fpfn) < bo->num_pages) |
| 1045 | return -EINVAL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1046 | /* |
| 1047 | * Check whether we need to move buffer. |
| 1048 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1049 | ret = ttm_bo_mem_compat(placement, &bo->mem); |
| 1050 | if (ret < 0) { |
| 1051 | ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait); |
| 1052 | if (ret) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1053 | return ret; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1054 | } else { |
| 1055 | /* |
| 1056 | * Use the access and other non-mapping-related flag bits from |
| 1057 | * the compatible memory placement flags to the active flags |
| 1058 | */ |
| 1059 | ttm_flag_masked(&bo->mem.placement, placement->placement[ret], |
| 1060 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1061 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1062 | /* |
| 1063 | * We might need to add a TTM. |
| 1064 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1065 | if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
| 1066 | ret = ttm_bo_add_ttm(bo, true); |
| 1067 | if (ret) |
| 1068 | return ret; |
| 1069 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1070 | return 0; |
| 1071 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1072 | EXPORT_SYMBOL(ttm_bo_validate); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1073 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1074 | int ttm_bo_check_placement(struct ttm_buffer_object *bo, |
| 1075 | struct ttm_placement *placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1076 | { |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1077 | int i; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1078 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1079 | if (placement->fpfn || placement->lpfn) { |
| 1080 | if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) { |
| 1081 | printk(KERN_ERR TTM_PFX "Page number range to small " |
| 1082 | "Need %lu pages, range is [%u, %u]\n", |
| 1083 | bo->mem.num_pages, placement->fpfn, |
| 1084 | placement->lpfn); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1085 | return -EINVAL; |
| 1086 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1087 | } |
| 1088 | for (i = 0; i < placement->num_placement; i++) { |
| 1089 | if (!capable(CAP_SYS_ADMIN)) { |
| 1090 | if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) { |
| 1091 | printk(KERN_ERR TTM_PFX "Need to be root to " |
| 1092 | "modify NO_EVICT status.\n"); |
| 1093 | return -EINVAL; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | for (i = 0; i < placement->num_busy_placement; i++) { |
| 1098 | if (!capable(CAP_SYS_ADMIN)) { |
| 1099 | if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) { |
| 1100 | printk(KERN_ERR TTM_PFX "Need to be root to " |
| 1101 | "modify NO_EVICT status.\n"); |
| 1102 | return -EINVAL; |
| 1103 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1109 | int ttm_bo_init(struct ttm_bo_device *bdev, |
| 1110 | struct ttm_buffer_object *bo, |
| 1111 | unsigned long size, |
| 1112 | enum ttm_bo_type type, |
| 1113 | struct ttm_placement *placement, |
| 1114 | uint32_t page_alignment, |
| 1115 | unsigned long buffer_start, |
| 1116 | bool interruptible, |
| 1117 | struct file *persistant_swap_storage, |
| 1118 | size_t acc_size, |
| 1119 | void (*destroy) (struct ttm_buffer_object *)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1120 | { |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1121 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1122 | unsigned long num_pages; |
| 1123 | |
| 1124 | size += buffer_start & ~PAGE_MASK; |
| 1125 | num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1126 | if (num_pages == 0) { |
| 1127 | printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n"); |
| 1128 | return -EINVAL; |
| 1129 | } |
| 1130 | bo->destroy = destroy; |
| 1131 | |
| 1132 | spin_lock_init(&bo->lock); |
| 1133 | kref_init(&bo->kref); |
| 1134 | kref_init(&bo->list_kref); |
| 1135 | atomic_set(&bo->cpu_writers, 0); |
| 1136 | atomic_set(&bo->reserved, 1); |
| 1137 | init_waitqueue_head(&bo->event_queue); |
| 1138 | INIT_LIST_HEAD(&bo->lru); |
| 1139 | INIT_LIST_HEAD(&bo->ddestroy); |
| 1140 | INIT_LIST_HEAD(&bo->swap); |
| 1141 | bo->bdev = bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1142 | bo->glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1143 | bo->type = type; |
| 1144 | bo->num_pages = num_pages; |
Jerome Glisse | eb6d2c3 | 2009-12-10 16:15:52 +0100 | [diff] [blame^] | 1145 | bo->mem.size = num_pages << PAGE_SHIFT; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1146 | bo->mem.mem_type = TTM_PL_SYSTEM; |
| 1147 | bo->mem.num_pages = bo->num_pages; |
| 1148 | bo->mem.mm_node = NULL; |
| 1149 | bo->mem.page_alignment = page_alignment; |
| 1150 | bo->buffer_start = buffer_start & PAGE_MASK; |
| 1151 | bo->priv_flags = 0; |
| 1152 | bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); |
| 1153 | bo->seq_valid = false; |
| 1154 | bo->persistant_swap_storage = persistant_swap_storage; |
| 1155 | bo->acc_size = acc_size; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1156 | atomic_inc(&bo->glob->bo_count); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1157 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1158 | ret = ttm_bo_check_placement(bo, placement); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1159 | if (unlikely(ret != 0)) |
| 1160 | goto out_err; |
| 1161 | |
| 1162 | /* |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1163 | * For ttm_bo_type_device buffers, allocate |
| 1164 | * address space from the device. |
| 1165 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1166 | if (bo->type == ttm_bo_type_device) { |
| 1167 | ret = ttm_bo_setup_vm(bo); |
| 1168 | if (ret) |
| 1169 | goto out_err; |
| 1170 | } |
| 1171 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1172 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1173 | if (ret) |
| 1174 | goto out_err; |
| 1175 | |
| 1176 | ttm_bo_unreserve(bo); |
| 1177 | return 0; |
| 1178 | |
| 1179 | out_err: |
| 1180 | ttm_bo_unreserve(bo); |
| 1181 | ttm_bo_unref(&bo); |
| 1182 | |
| 1183 | return ret; |
| 1184 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1185 | EXPORT_SYMBOL(ttm_bo_init); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1186 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1187 | static inline size_t ttm_bo_size(struct ttm_bo_global *glob, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1188 | unsigned long num_pages) |
| 1189 | { |
| 1190 | size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) & |
| 1191 | PAGE_MASK; |
| 1192 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1193 | return glob->ttm_bo_size + 2 * page_array_size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1196 | int ttm_bo_create(struct ttm_bo_device *bdev, |
| 1197 | unsigned long size, |
| 1198 | enum ttm_bo_type type, |
| 1199 | struct ttm_placement *placement, |
| 1200 | uint32_t page_alignment, |
| 1201 | unsigned long buffer_start, |
| 1202 | bool interruptible, |
| 1203 | struct file *persistant_swap_storage, |
| 1204 | struct ttm_buffer_object **p_bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1205 | { |
| 1206 | struct ttm_buffer_object *bo; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1207 | struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1208 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1209 | |
| 1210 | size_t acc_size = |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1211 | ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT); |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 1212 | ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1213 | if (unlikely(ret != 0)) |
| 1214 | return ret; |
| 1215 | |
| 1216 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
| 1217 | |
| 1218 | if (unlikely(bo == NULL)) { |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 1219 | ttm_mem_global_free(mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1220 | return -ENOMEM; |
| 1221 | } |
| 1222 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1223 | ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, |
| 1224 | buffer_start, interruptible, |
| 1225 | persistant_swap_storage, acc_size, NULL); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1226 | if (likely(ret == 0)) |
| 1227 | *p_bo = bo; |
| 1228 | |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1232 | static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1233 | unsigned mem_type, bool allow_errors) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1234 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1235 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1236 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1237 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1238 | |
| 1239 | /* |
| 1240 | * Can't use standard list traversal since we're unlocking. |
| 1241 | */ |
| 1242 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1243 | spin_lock(&glob->lru_lock); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1244 | while (!list_empty(&man->lru)) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1245 | spin_unlock(&glob->lru_lock); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1246 | ret = ttm_mem_evict_first(bdev, mem_type, false, false); |
| 1247 | if (ret) { |
| 1248 | if (allow_errors) { |
| 1249 | return ret; |
| 1250 | } else { |
| 1251 | printk(KERN_ERR TTM_PFX |
| 1252 | "Cleanup eviction failed\n"); |
| 1253 | } |
| 1254 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1255 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1256 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1257 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1262 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1263 | struct ttm_bo_global *glob = bdev->glob; |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1264 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1265 | int ret = -EINVAL; |
| 1266 | |
| 1267 | if (mem_type >= TTM_NUM_MEM_TYPES) { |
| 1268 | printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type); |
| 1269 | return ret; |
| 1270 | } |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1271 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1272 | |
| 1273 | if (!man->has_type) { |
| 1274 | printk(KERN_ERR TTM_PFX "Trying to take down uninitialized " |
| 1275 | "memory manager type %u\n", mem_type); |
| 1276 | return ret; |
| 1277 | } |
| 1278 | |
| 1279 | man->use_type = false; |
| 1280 | man->has_type = false; |
| 1281 | |
| 1282 | ret = 0; |
| 1283 | if (mem_type > 0) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1284 | ttm_bo_force_list_clean(bdev, mem_type, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1285 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1286 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1287 | if (drm_mm_clean(&man->manager)) |
| 1288 | drm_mm_takedown(&man->manager); |
| 1289 | else |
| 1290 | ret = -EBUSY; |
| 1291 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1292 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | return ret; |
| 1296 | } |
| 1297 | EXPORT_SYMBOL(ttm_bo_clean_mm); |
| 1298 | |
| 1299 | int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1300 | { |
| 1301 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 1302 | |
| 1303 | if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { |
| 1304 | printk(KERN_ERR TTM_PFX |
| 1305 | "Illegal memory manager memory type %u.\n", |
| 1306 | mem_type); |
| 1307 | return -EINVAL; |
| 1308 | } |
| 1309 | |
| 1310 | if (!man->has_type) { |
| 1311 | printk(KERN_ERR TTM_PFX |
| 1312 | "Memory type %u has not been initialized.\n", |
| 1313 | mem_type); |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1317 | return ttm_bo_force_list_clean(bdev, mem_type, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1318 | } |
| 1319 | EXPORT_SYMBOL(ttm_bo_evict_mm); |
| 1320 | |
| 1321 | int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1322 | unsigned long p_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1323 | { |
| 1324 | int ret = -EINVAL; |
| 1325 | struct ttm_mem_type_manager *man; |
| 1326 | |
| 1327 | if (type >= TTM_NUM_MEM_TYPES) { |
| 1328 | printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type); |
| 1329 | return ret; |
| 1330 | } |
| 1331 | |
| 1332 | man = &bdev->man[type]; |
| 1333 | if (man->has_type) { |
| 1334 | printk(KERN_ERR TTM_PFX |
| 1335 | "Memory manager already initialized for type %d\n", |
| 1336 | type); |
| 1337 | return ret; |
| 1338 | } |
| 1339 | |
| 1340 | ret = bdev->driver->init_mem_type(bdev, type, man); |
| 1341 | if (ret) |
| 1342 | return ret; |
| 1343 | |
| 1344 | ret = 0; |
| 1345 | if (type != TTM_PL_SYSTEM) { |
| 1346 | if (!p_size) { |
| 1347 | printk(KERN_ERR TTM_PFX |
| 1348 | "Zero size memory manager type %d\n", |
| 1349 | type); |
| 1350 | return ret; |
| 1351 | } |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1352 | ret = drm_mm_init(&man->manager, 0, p_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1353 | if (ret) |
| 1354 | return ret; |
| 1355 | } |
| 1356 | man->has_type = true; |
| 1357 | man->use_type = true; |
| 1358 | man->size = p_size; |
| 1359 | |
| 1360 | INIT_LIST_HEAD(&man->lru); |
| 1361 | |
| 1362 | return 0; |
| 1363 | } |
| 1364 | EXPORT_SYMBOL(ttm_bo_init_mm); |
| 1365 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1366 | static void ttm_bo_global_kobj_release(struct kobject *kobj) |
| 1367 | { |
| 1368 | struct ttm_bo_global *glob = |
| 1369 | container_of(kobj, struct ttm_bo_global, kobj); |
| 1370 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1371 | ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); |
| 1372 | __free_page(glob->dummy_read_page); |
| 1373 | kfree(glob); |
| 1374 | } |
| 1375 | |
| 1376 | void ttm_bo_global_release(struct ttm_global_reference *ref) |
| 1377 | { |
| 1378 | struct ttm_bo_global *glob = ref->object; |
| 1379 | |
| 1380 | kobject_del(&glob->kobj); |
| 1381 | kobject_put(&glob->kobj); |
| 1382 | } |
| 1383 | EXPORT_SYMBOL(ttm_bo_global_release); |
| 1384 | |
| 1385 | int ttm_bo_global_init(struct ttm_global_reference *ref) |
| 1386 | { |
| 1387 | struct ttm_bo_global_ref *bo_ref = |
| 1388 | container_of(ref, struct ttm_bo_global_ref, ref); |
| 1389 | struct ttm_bo_global *glob = ref->object; |
| 1390 | int ret; |
| 1391 | |
| 1392 | mutex_init(&glob->device_list_mutex); |
| 1393 | spin_lock_init(&glob->lru_lock); |
| 1394 | glob->mem_glob = bo_ref->mem_glob; |
| 1395 | glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); |
| 1396 | |
| 1397 | if (unlikely(glob->dummy_read_page == NULL)) { |
| 1398 | ret = -ENOMEM; |
| 1399 | goto out_no_drp; |
| 1400 | } |
| 1401 | |
| 1402 | INIT_LIST_HEAD(&glob->swap_lru); |
| 1403 | INIT_LIST_HEAD(&glob->device_list); |
| 1404 | |
| 1405 | ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout); |
| 1406 | ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink); |
| 1407 | if (unlikely(ret != 0)) { |
| 1408 | printk(KERN_ERR TTM_PFX |
| 1409 | "Could not register buffer object swapout.\n"); |
| 1410 | goto out_no_shrink; |
| 1411 | } |
| 1412 | |
| 1413 | glob->ttm_bo_extra_size = |
| 1414 | ttm_round_pot(sizeof(struct ttm_tt)) + |
| 1415 | ttm_round_pot(sizeof(struct ttm_backend)); |
| 1416 | |
| 1417 | glob->ttm_bo_size = glob->ttm_bo_extra_size + |
| 1418 | ttm_round_pot(sizeof(struct ttm_buffer_object)); |
| 1419 | |
| 1420 | atomic_set(&glob->bo_count, 0); |
| 1421 | |
| 1422 | kobject_init(&glob->kobj, &ttm_bo_glob_kobj_type); |
| 1423 | ret = kobject_add(&glob->kobj, ttm_get_kobj(), "buffer_objects"); |
| 1424 | if (unlikely(ret != 0)) |
| 1425 | kobject_put(&glob->kobj); |
| 1426 | return ret; |
| 1427 | out_no_shrink: |
| 1428 | __free_page(glob->dummy_read_page); |
| 1429 | out_no_drp: |
| 1430 | kfree(glob); |
| 1431 | return ret; |
| 1432 | } |
| 1433 | EXPORT_SYMBOL(ttm_bo_global_init); |
| 1434 | |
| 1435 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1436 | int ttm_bo_device_release(struct ttm_bo_device *bdev) |
| 1437 | { |
| 1438 | int ret = 0; |
| 1439 | unsigned i = TTM_NUM_MEM_TYPES; |
| 1440 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1441 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1442 | |
| 1443 | while (i--) { |
| 1444 | man = &bdev->man[i]; |
| 1445 | if (man->has_type) { |
| 1446 | man->use_type = false; |
| 1447 | if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { |
| 1448 | ret = -EBUSY; |
| 1449 | printk(KERN_ERR TTM_PFX |
| 1450 | "DRM memory manager type %d " |
| 1451 | "is not clean.\n", i); |
| 1452 | } |
| 1453 | man->has_type = false; |
| 1454 | } |
| 1455 | } |
| 1456 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1457 | mutex_lock(&glob->device_list_mutex); |
| 1458 | list_del(&bdev->device_list); |
| 1459 | mutex_unlock(&glob->device_list_mutex); |
| 1460 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1461 | if (!cancel_delayed_work(&bdev->wq)) |
| 1462 | flush_scheduled_work(); |
| 1463 | |
| 1464 | while (ttm_bo_delayed_delete(bdev, true)) |
| 1465 | ; |
| 1466 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1467 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1468 | if (list_empty(&bdev->ddestroy)) |
| 1469 | TTM_DEBUG("Delayed destroy list was clean\n"); |
| 1470 | |
| 1471 | if (list_empty(&bdev->man[0].lru)) |
| 1472 | TTM_DEBUG("Swap list was clean\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1473 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1474 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1475 | BUG_ON(!drm_mm_clean(&bdev->addr_space_mm)); |
| 1476 | write_lock(&bdev->vm_lock); |
| 1477 | drm_mm_takedown(&bdev->addr_space_mm); |
| 1478 | write_unlock(&bdev->vm_lock); |
| 1479 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1480 | return ret; |
| 1481 | } |
| 1482 | EXPORT_SYMBOL(ttm_bo_device_release); |
| 1483 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1484 | int ttm_bo_device_init(struct ttm_bo_device *bdev, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1485 | struct ttm_bo_global *glob, |
| 1486 | struct ttm_bo_driver *driver, |
Dave Airlie | 51c8b40 | 2009-08-20 13:38:04 +1000 | [diff] [blame] | 1487 | uint64_t file_page_offset, |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1488 | bool need_dma32) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1489 | { |
| 1490 | int ret = -EINVAL; |
| 1491 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1492 | rwlock_init(&bdev->vm_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1493 | bdev->driver = driver; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1494 | |
| 1495 | memset(bdev->man, 0, sizeof(bdev->man)); |
| 1496 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1497 | /* |
| 1498 | * Initialize the system memory buffer type. |
| 1499 | * Other types need to be driver / IOCTL initialized. |
| 1500 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1501 | ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1502 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1503 | goto out_no_sys; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1504 | |
| 1505 | bdev->addr_space_rb = RB_ROOT; |
| 1506 | ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000); |
| 1507 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1508 | goto out_no_addr_mm; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1509 | |
| 1510 | INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); |
| 1511 | bdev->nice_mode = true; |
| 1512 | INIT_LIST_HEAD(&bdev->ddestroy); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1513 | bdev->dev_mapping = NULL; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1514 | bdev->glob = glob; |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1515 | bdev->need_dma32 = need_dma32; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1516 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1517 | mutex_lock(&glob->device_list_mutex); |
| 1518 | list_add_tail(&bdev->device_list, &glob->device_list); |
| 1519 | mutex_unlock(&glob->device_list_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1520 | |
| 1521 | return 0; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1522 | out_no_addr_mm: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1523 | ttm_bo_clean_mm(bdev, 0); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1524 | out_no_sys: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1525 | return ret; |
| 1526 | } |
| 1527 | EXPORT_SYMBOL(ttm_bo_device_init); |
| 1528 | |
| 1529 | /* |
| 1530 | * buffer object vm functions. |
| 1531 | */ |
| 1532 | |
| 1533 | bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 1534 | { |
| 1535 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 1536 | |
| 1537 | if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
| 1538 | if (mem->mem_type == TTM_PL_SYSTEM) |
| 1539 | return false; |
| 1540 | |
| 1541 | if (man->flags & TTM_MEMTYPE_FLAG_CMA) |
| 1542 | return false; |
| 1543 | |
| 1544 | if (mem->placement & TTM_PL_FLAG_CACHED) |
| 1545 | return false; |
| 1546 | } |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
| 1550 | int ttm_bo_pci_offset(struct ttm_bo_device *bdev, |
| 1551 | struct ttm_mem_reg *mem, |
| 1552 | unsigned long *bus_base, |
| 1553 | unsigned long *bus_offset, unsigned long *bus_size) |
| 1554 | { |
| 1555 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 1556 | |
| 1557 | *bus_size = 0; |
| 1558 | if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) |
| 1559 | return -EINVAL; |
| 1560 | |
| 1561 | if (ttm_mem_reg_is_pci(bdev, mem)) { |
| 1562 | *bus_offset = mem->mm_node->start << PAGE_SHIFT; |
| 1563 | *bus_size = mem->num_pages << PAGE_SHIFT; |
| 1564 | *bus_base = man->io_offset; |
| 1565 | } |
| 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) |
| 1571 | { |
| 1572 | struct ttm_bo_device *bdev = bo->bdev; |
| 1573 | loff_t offset = (loff_t) bo->addr_space_offset; |
| 1574 | loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT; |
| 1575 | |
| 1576 | if (!bdev->dev_mapping) |
| 1577 | return; |
| 1578 | |
| 1579 | unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1); |
| 1580 | } |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 1581 | EXPORT_SYMBOL(ttm_bo_unmap_virtual); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1582 | |
| 1583 | static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo) |
| 1584 | { |
| 1585 | struct ttm_bo_device *bdev = bo->bdev; |
| 1586 | struct rb_node **cur = &bdev->addr_space_rb.rb_node; |
| 1587 | struct rb_node *parent = NULL; |
| 1588 | struct ttm_buffer_object *cur_bo; |
| 1589 | unsigned long offset = bo->vm_node->start; |
| 1590 | unsigned long cur_offset; |
| 1591 | |
| 1592 | while (*cur) { |
| 1593 | parent = *cur; |
| 1594 | cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb); |
| 1595 | cur_offset = cur_bo->vm_node->start; |
| 1596 | if (offset < cur_offset) |
| 1597 | cur = &parent->rb_left; |
| 1598 | else if (offset > cur_offset) |
| 1599 | cur = &parent->rb_right; |
| 1600 | else |
| 1601 | BUG(); |
| 1602 | } |
| 1603 | |
| 1604 | rb_link_node(&bo->vm_rb, parent, cur); |
| 1605 | rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb); |
| 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * ttm_bo_setup_vm: |
| 1610 | * |
| 1611 | * @bo: the buffer to allocate address space for |
| 1612 | * |
| 1613 | * Allocate address space in the drm device so that applications |
| 1614 | * can mmap the buffer and access the contents. This only |
| 1615 | * applies to ttm_bo_type_device objects as others are not |
| 1616 | * placed in the drm device address space. |
| 1617 | */ |
| 1618 | |
| 1619 | static int ttm_bo_setup_vm(struct ttm_buffer_object *bo) |
| 1620 | { |
| 1621 | struct ttm_bo_device *bdev = bo->bdev; |
| 1622 | int ret; |
| 1623 | |
| 1624 | retry_pre_get: |
| 1625 | ret = drm_mm_pre_get(&bdev->addr_space_mm); |
| 1626 | if (unlikely(ret != 0)) |
| 1627 | return ret; |
| 1628 | |
| 1629 | write_lock(&bdev->vm_lock); |
| 1630 | bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm, |
| 1631 | bo->mem.num_pages, 0, 0); |
| 1632 | |
| 1633 | if (unlikely(bo->vm_node == NULL)) { |
| 1634 | ret = -ENOMEM; |
| 1635 | goto out_unlock; |
| 1636 | } |
| 1637 | |
| 1638 | bo->vm_node = drm_mm_get_block_atomic(bo->vm_node, |
| 1639 | bo->mem.num_pages, 0); |
| 1640 | |
| 1641 | if (unlikely(bo->vm_node == NULL)) { |
| 1642 | write_unlock(&bdev->vm_lock); |
| 1643 | goto retry_pre_get; |
| 1644 | } |
| 1645 | |
| 1646 | ttm_bo_vm_insert_rb(bo); |
| 1647 | write_unlock(&bdev->vm_lock); |
| 1648 | bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT; |
| 1649 | |
| 1650 | return 0; |
| 1651 | out_unlock: |
| 1652 | write_unlock(&bdev->vm_lock); |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
| 1656 | int ttm_bo_wait(struct ttm_buffer_object *bo, |
| 1657 | bool lazy, bool interruptible, bool no_wait) |
| 1658 | { |
| 1659 | struct ttm_bo_driver *driver = bo->bdev->driver; |
| 1660 | void *sync_obj; |
| 1661 | void *sync_obj_arg; |
| 1662 | int ret = 0; |
| 1663 | |
| 1664 | if (likely(bo->sync_obj == NULL)) |
| 1665 | return 0; |
| 1666 | |
| 1667 | while (bo->sync_obj) { |
| 1668 | |
| 1669 | if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) { |
| 1670 | void *tmp_obj = bo->sync_obj; |
| 1671 | bo->sync_obj = NULL; |
| 1672 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags); |
| 1673 | spin_unlock(&bo->lock); |
| 1674 | driver->sync_obj_unref(&tmp_obj); |
| 1675 | spin_lock(&bo->lock); |
| 1676 | continue; |
| 1677 | } |
| 1678 | |
| 1679 | if (no_wait) |
| 1680 | return -EBUSY; |
| 1681 | |
| 1682 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
| 1683 | sync_obj_arg = bo->sync_obj_arg; |
| 1684 | spin_unlock(&bo->lock); |
| 1685 | ret = driver->sync_obj_wait(sync_obj, sync_obj_arg, |
| 1686 | lazy, interruptible); |
| 1687 | if (unlikely(ret != 0)) { |
| 1688 | driver->sync_obj_unref(&sync_obj); |
| 1689 | spin_lock(&bo->lock); |
| 1690 | return ret; |
| 1691 | } |
| 1692 | spin_lock(&bo->lock); |
| 1693 | if (likely(bo->sync_obj == sync_obj && |
| 1694 | bo->sync_obj_arg == sync_obj_arg)) { |
| 1695 | void *tmp_obj = bo->sync_obj; |
| 1696 | bo->sync_obj = NULL; |
| 1697 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, |
| 1698 | &bo->priv_flags); |
| 1699 | spin_unlock(&bo->lock); |
| 1700 | driver->sync_obj_unref(&sync_obj); |
| 1701 | driver->sync_obj_unref(&tmp_obj); |
| 1702 | spin_lock(&bo->lock); |
Thomas Hellstrom | fee280d | 2009-08-03 12:39:06 +0200 | [diff] [blame] | 1703 | } else { |
| 1704 | spin_unlock(&bo->lock); |
| 1705 | driver->sync_obj_unref(&sync_obj); |
| 1706 | spin_lock(&bo->lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | return 0; |
| 1710 | } |
| 1711 | EXPORT_SYMBOL(ttm_bo_wait); |
| 1712 | |
| 1713 | void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo) |
| 1714 | { |
| 1715 | atomic_set(&bo->reserved, 0); |
| 1716 | wake_up_all(&bo->event_queue); |
| 1717 | } |
| 1718 | |
| 1719 | int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible, |
| 1720 | bool no_wait) |
| 1721 | { |
| 1722 | int ret; |
| 1723 | |
| 1724 | while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) { |
| 1725 | if (no_wait) |
| 1726 | return -EBUSY; |
| 1727 | else if (interruptible) { |
| 1728 | ret = wait_event_interruptible |
| 1729 | (bo->event_queue, atomic_read(&bo->reserved) == 0); |
| 1730 | if (unlikely(ret != 0)) |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 1731 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1732 | } else { |
| 1733 | wait_event(bo->event_queue, |
| 1734 | atomic_read(&bo->reserved) == 0); |
| 1735 | } |
| 1736 | } |
| 1737 | return 0; |
| 1738 | } |
| 1739 | |
| 1740 | int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) |
| 1741 | { |
| 1742 | int ret = 0; |
| 1743 | |
| 1744 | /* |
| 1745 | * Using ttm_bo_reserve instead of ttm_bo_block_reservation |
| 1746 | * makes sure the lru lists are updated. |
| 1747 | */ |
| 1748 | |
| 1749 | ret = ttm_bo_reserve(bo, true, no_wait, false, 0); |
| 1750 | if (unlikely(ret != 0)) |
| 1751 | return ret; |
| 1752 | spin_lock(&bo->lock); |
| 1753 | ret = ttm_bo_wait(bo, false, true, no_wait); |
| 1754 | spin_unlock(&bo->lock); |
| 1755 | if (likely(ret == 0)) |
| 1756 | atomic_inc(&bo->cpu_writers); |
| 1757 | ttm_bo_unreserve(bo); |
| 1758 | return ret; |
| 1759 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1760 | EXPORT_SYMBOL(ttm_bo_synccpu_write_grab); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1761 | |
| 1762 | void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) |
| 1763 | { |
| 1764 | if (atomic_dec_and_test(&bo->cpu_writers)) |
| 1765 | wake_up_all(&bo->event_queue); |
| 1766 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1767 | EXPORT_SYMBOL(ttm_bo_synccpu_write_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1768 | |
| 1769 | /** |
| 1770 | * A buffer object shrink method that tries to swap out the first |
| 1771 | * buffer object on the bo_global::swap_lru list. |
| 1772 | */ |
| 1773 | |
| 1774 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) |
| 1775 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1776 | struct ttm_bo_global *glob = |
| 1777 | container_of(shrink, struct ttm_bo_global, shrink); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1778 | struct ttm_buffer_object *bo; |
| 1779 | int ret = -EBUSY; |
| 1780 | int put_count; |
| 1781 | uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM); |
| 1782 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1783 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1784 | while (ret == -EBUSY) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1785 | if (unlikely(list_empty(&glob->swap_lru))) { |
| 1786 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1787 | return -EBUSY; |
| 1788 | } |
| 1789 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1790 | bo = list_first_entry(&glob->swap_lru, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1791 | struct ttm_buffer_object, swap); |
| 1792 | kref_get(&bo->list_kref); |
| 1793 | |
| 1794 | /** |
| 1795 | * Reserve buffer. Since we unlock while sleeping, we need |
| 1796 | * to re-check that nobody removed us from the swap-list while |
| 1797 | * we slept. |
| 1798 | */ |
| 1799 | |
| 1800 | ret = ttm_bo_reserve_locked(bo, false, true, false, 0); |
| 1801 | if (unlikely(ret == -EBUSY)) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1802 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1803 | ttm_bo_wait_unreserved(bo, false); |
| 1804 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1805 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | BUG_ON(ret != 0); |
| 1810 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1811 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1812 | |
| 1813 | while (put_count--) |
| 1814 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
| 1815 | |
| 1816 | /** |
| 1817 | * Wait for GPU, then move to system cached. |
| 1818 | */ |
| 1819 | |
| 1820 | spin_lock(&bo->lock); |
| 1821 | ret = ttm_bo_wait(bo, false, false, false); |
| 1822 | spin_unlock(&bo->lock); |
| 1823 | |
| 1824 | if (unlikely(ret != 0)) |
| 1825 | goto out; |
| 1826 | |
| 1827 | if ((bo->mem.placement & swap_placement) != swap_placement) { |
| 1828 | struct ttm_mem_reg evict_mem; |
| 1829 | |
| 1830 | evict_mem = bo->mem; |
| 1831 | evict_mem.mm_node = NULL; |
| 1832 | evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; |
| 1833 | evict_mem.mem_type = TTM_PL_SYSTEM; |
| 1834 | |
| 1835 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, |
| 1836 | false, false); |
| 1837 | if (unlikely(ret != 0)) |
| 1838 | goto out; |
| 1839 | } |
| 1840 | |
| 1841 | ttm_bo_unmap_virtual(bo); |
| 1842 | |
| 1843 | /** |
| 1844 | * Swap out. Buffer will be swapped in again as soon as |
| 1845 | * anyone tries to access a ttm page. |
| 1846 | */ |
| 1847 | |
| 1848 | ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage); |
| 1849 | out: |
| 1850 | |
| 1851 | /** |
| 1852 | * |
| 1853 | * Unreserve without putting on LRU to avoid swapping out an |
| 1854 | * already swapped buffer. |
| 1855 | */ |
| 1856 | |
| 1857 | atomic_set(&bo->reserved, 0); |
| 1858 | wake_up_all(&bo->event_queue); |
| 1859 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1860 | return ret; |
| 1861 | } |
| 1862 | |
| 1863 | void ttm_bo_swapout_all(struct ttm_bo_device *bdev) |
| 1864 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1865 | while (ttm_bo_swapout(&bdev->glob->shrink) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1866 | ; |
| 1867 | } |