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