1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #define pr_fmt(fmt) "[TTM] " fmt
33 #include "ttm/ttm_module.h"
34 #include "ttm/ttm_bo_driver.h"
35 #include "ttm/ttm_placement.h"
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
44 #define TTM_ASSERT_LOCKED(param)
45 #define TTM_DEBUG(fmt, arg...)
46 #define TTM_BO_HASH_ORDER 13
48 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
49 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
50 static void ttm_bo_global_kobj_release(struct kobject *kobj);
52 static struct attribute ttm_bo_count = {
57 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
61 for (i = 0; i <= TTM_PL_PRIV5; i++)
62 if (flags & (1 << i)) {
69 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
71 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
73 pr_err(" has_type: %d\n", man->has_type);
74 pr_err(" use_type: %d\n", man->use_type);
75 pr_err(" flags: 0x%08X\n", man->flags);
76 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
77 pr_err(" size: %llu\n", man->size);
78 pr_err(" available_caching: 0x%08X\n", man->available_caching);
79 pr_err(" default_caching: 0x%08X\n", man->default_caching);
80 if (mem_type != TTM_PL_SYSTEM)
81 (*man->func->debug)(man, TTM_PFX);
84 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
85 struct ttm_placement *placement)
89 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
90 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 for (i = 0; i < placement->num_placement; i++) {
93 ret = ttm_mem_type_from_flags(placement->placement[i],
97 pr_err(" placement[%d]=0x%08X (%d)\n",
98 i, placement->placement[i], mem_type);
99 ttm_mem_type_debug(bo->bdev, mem_type);
103 static ssize_t ttm_bo_global_show(struct kobject *kobj,
104 struct attribute *attr,
107 struct ttm_bo_global *glob =
108 container_of(kobj, struct ttm_bo_global, kobj);
110 return snprintf(buffer, PAGE_SIZE, "%lu\n",
111 (unsigned long) atomic_read(&glob->bo_count));
114 static struct attribute *ttm_bo_global_attrs[] = {
119 static const struct sysfs_ops ttm_bo_global_ops = {
120 .show = &ttm_bo_global_show
123 static struct kobj_type ttm_bo_glob_kobj_type = {
124 .release = &ttm_bo_global_kobj_release,
125 .sysfs_ops = &ttm_bo_global_ops,
126 .default_attrs = ttm_bo_global_attrs
130 static inline uint32_t ttm_bo_type_flags(unsigned type)
135 static void ttm_bo_release_list(struct kref *list_kref)
137 struct ttm_buffer_object *bo =
138 container_of(list_kref, struct ttm_buffer_object, list_kref);
139 struct ttm_bo_device *bdev = bo->bdev;
140 size_t acc_size = bo->acc_size;
142 BUG_ON(atomic_read(&bo->list_kref.refcount));
143 BUG_ON(atomic_read(&bo->kref.refcount));
144 BUG_ON(atomic_read(&bo->cpu_writers));
145 BUG_ON(bo->sync_obj != NULL);
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
151 ttm_tt_destroy(bo->ttm);
152 atomic_dec(&bo->glob->bo_count);
158 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
161 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
164 return wait_event_interruptible(bo->event_queue,
165 atomic_read(&bo->reserved) == 0);
167 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
171 EXPORT_SYMBOL(ttm_bo_wait_unreserved);
173 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
175 struct ttm_bo_device *bdev = bo->bdev;
176 struct ttm_mem_type_manager *man;
178 BUG_ON(!atomic_read(&bo->reserved));
180 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
182 BUG_ON(!list_empty(&bo->lru));
184 man = &bdev->man[bo->mem.mem_type];
185 list_add_tail(&bo->lru, &man->lru);
186 kref_get(&bo->list_kref);
188 if (bo->ttm != NULL) {
189 list_add_tail(&bo->swap, &bo->glob->swap_lru);
190 kref_get(&bo->list_kref);
195 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
199 if (!list_empty(&bo->swap)) {
200 list_del_init(&bo->swap);
203 if (!list_empty(&bo->lru)) {
204 list_del_init(&bo->lru);
209 * TODO: Add a driver hook to delete from
210 * driver-specific LRU's here.
216 int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
218 bool no_wait, bool use_sequence, uint32_t sequence)
220 struct ttm_bo_global *glob = bo->glob;
223 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
225 * Deadlock avoidance for multi-bo reserving.
227 if (use_sequence && bo->seq_valid) {
229 * We've already reserved this one.
231 if (unlikely(sequence == bo->val_seq))
234 * Already reserved by a thread that will not back
235 * off for us. We need to back off.
237 if (unlikely(sequence - bo->val_seq < (1 << 31)))
244 spin_unlock(&glob->lru_lock);
245 ret = ttm_bo_wait_unreserved(bo, interruptible);
246 spin_lock(&glob->lru_lock);
254 * Wake up waiters that may need to recheck for deadlock,
255 * if we decreased the sequence number.
257 if (unlikely((bo->val_seq - sequence < (1 << 31))
259 wake_up_all(&bo->event_queue);
261 bo->val_seq = sequence;
262 bo->seq_valid = true;
264 bo->seq_valid = false;
269 EXPORT_SYMBOL(ttm_bo_reserve);
271 static void ttm_bo_ref_bug(struct kref *list_kref)
276 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
279 kref_sub(&bo->list_kref, count,
280 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
283 int ttm_bo_reserve(struct ttm_buffer_object *bo,
285 bool no_wait, bool use_sequence, uint32_t sequence)
287 struct ttm_bo_global *glob = bo->glob;
291 spin_lock(&glob->lru_lock);
292 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
294 if (likely(ret == 0))
295 put_count = ttm_bo_del_from_lru(bo);
296 spin_unlock(&glob->lru_lock);
298 ttm_bo_list_ref_sub(bo, put_count, true);
303 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
305 ttm_bo_add_to_lru(bo);
306 atomic_set(&bo->reserved, 0);
307 wake_up_all(&bo->event_queue);
310 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
312 struct ttm_bo_global *glob = bo->glob;
314 spin_lock(&glob->lru_lock);
315 ttm_bo_unreserve_locked(bo);
316 spin_unlock(&glob->lru_lock);
318 EXPORT_SYMBOL(ttm_bo_unreserve);
321 * Call bo->mutex locked.
323 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
325 struct ttm_bo_device *bdev = bo->bdev;
326 struct ttm_bo_global *glob = bo->glob;
328 uint32_t page_flags = 0;
330 TTM_ASSERT_LOCKED(&bo->mutex);
333 if (bdev->need_dma32)
334 page_flags |= TTM_PAGE_FLAG_DMA32;
337 case ttm_bo_type_device:
339 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
340 case ttm_bo_type_kernel:
341 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
342 page_flags, glob->dummy_read_page);
343 if (unlikely(bo->ttm == NULL))
347 pr_err("Illegal buffer object type\n");
355 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
356 struct ttm_mem_reg *mem,
357 bool evict, bool interruptible,
358 bool no_wait_reserve, bool no_wait_gpu)
360 struct ttm_bo_device *bdev = bo->bdev;
361 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
362 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
363 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
364 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
367 if (old_is_pci || new_is_pci ||
368 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
369 ret = ttm_mem_io_lock(old_man, true);
370 if (unlikely(ret != 0))
372 ttm_bo_unmap_virtual_locked(bo);
373 ttm_mem_io_unlock(old_man);
377 * Create and bind a ttm if required.
380 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
381 if (bo->ttm == NULL) {
382 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
383 ret = ttm_bo_add_ttm(bo, zero);
388 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
392 if (mem->mem_type != TTM_PL_SYSTEM) {
393 ret = ttm_tt_bind(bo->ttm, mem);
398 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
399 if (bdev->driver->move_notify)
400 bdev->driver->move_notify(bo, mem);
407 if (bdev->driver->move_notify)
408 bdev->driver->move_notify(bo, mem);
410 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
411 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
412 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
413 else if (bdev->driver->move)
414 ret = bdev->driver->move(bo, evict, interruptible,
415 no_wait_reserve, no_wait_gpu, mem);
417 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
420 if (bdev->driver->move_notify) {
421 struct ttm_mem_reg tmp_mem = *mem;
424 bdev->driver->move_notify(bo, mem);
433 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
435 pr_err("Can not flush read caches\n");
439 if (bo->mem.mm_node) {
440 bo->offset = (bo->mem.start << PAGE_SHIFT) +
441 bdev->man[bo->mem.mem_type].gpu_offset;
442 bo->cur_placement = bo->mem.placement;
449 new_man = &bdev->man[bo->mem.mem_type];
450 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
451 ttm_tt_unbind(bo->ttm);
452 ttm_tt_destroy(bo->ttm);
461 * Will release GPU memory type usage on destruction.
462 * This is the place to put in driver specific hooks to release
463 * driver private resources.
464 * Will release the bo::reserved lock.
467 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
469 if (bo->bdev->driver->move_notify)
470 bo->bdev->driver->move_notify(bo, NULL);
473 ttm_tt_unbind(bo->ttm);
474 ttm_tt_destroy(bo->ttm);
477 ttm_bo_mem_put(bo, &bo->mem);
479 atomic_set(&bo->reserved, 0);
482 * Make processes trying to reserve really pick it up.
484 smp_mb__after_atomic_dec();
485 wake_up_all(&bo->event_queue);
488 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
490 struct ttm_bo_device *bdev = bo->bdev;
491 struct ttm_bo_global *glob = bo->glob;
492 struct ttm_bo_driver *driver;
493 void *sync_obj = NULL;
498 spin_lock(&bdev->fence_lock);
499 (void) ttm_bo_wait(bo, false, false, true);
502 spin_lock(&glob->lru_lock);
505 * Lock inversion between bo:reserve and bdev::fence_lock here,
506 * but that's OK, since we're only trylocking.
509 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
511 if (unlikely(ret == -EBUSY))
514 spin_unlock(&bdev->fence_lock);
515 put_count = ttm_bo_del_from_lru(bo);
517 spin_unlock(&glob->lru_lock);
518 ttm_bo_cleanup_memtype_use(bo);
520 ttm_bo_list_ref_sub(bo, put_count, true);
524 spin_lock(&glob->lru_lock);
527 driver = bdev->driver;
529 sync_obj = driver->sync_obj_ref(bo->sync_obj);
530 sync_obj_arg = bo->sync_obj_arg;
532 kref_get(&bo->list_kref);
533 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
534 spin_unlock(&glob->lru_lock);
535 spin_unlock(&bdev->fence_lock);
538 driver->sync_obj_flush(sync_obj, sync_obj_arg);
539 driver->sync_obj_unref(&sync_obj);
541 schedule_delayed_work(&bdev->wq,
542 ((HZ / 100) < 1) ? 1 : HZ / 100);
546 * function ttm_bo_cleanup_refs
547 * If bo idle, remove from delayed- and lru lists, and unref.
548 * If not idle, do nothing.
550 * @interruptible Any sleeps should occur interruptibly.
551 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
552 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
555 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
557 bool no_wait_reserve,
560 struct ttm_bo_device *bdev = bo->bdev;
561 struct ttm_bo_global *glob = bo->glob;
566 spin_lock(&bdev->fence_lock);
567 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
568 spin_unlock(&bdev->fence_lock);
570 if (unlikely(ret != 0))
573 spin_lock(&glob->lru_lock);
575 if (unlikely(list_empty(&bo->ddestroy))) {
576 spin_unlock(&glob->lru_lock);
580 ret = ttm_bo_reserve_locked(bo, interruptible,
581 no_wait_reserve, false, 0);
583 if (unlikely(ret != 0)) {
584 spin_unlock(&glob->lru_lock);
589 * We can re-check for sync object without taking
590 * the bo::lock since setting the sync object requires
591 * also bo::reserved. A busy object at this point may
592 * be caused by another thread recently starting an accelerated
596 if (unlikely(bo->sync_obj)) {
597 atomic_set(&bo->reserved, 0);
598 wake_up_all(&bo->event_queue);
599 spin_unlock(&glob->lru_lock);
603 put_count = ttm_bo_del_from_lru(bo);
604 list_del_init(&bo->ddestroy);
607 spin_unlock(&glob->lru_lock);
608 ttm_bo_cleanup_memtype_use(bo);
610 ttm_bo_list_ref_sub(bo, put_count, true);
616 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
617 * encountered buffers.
620 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
622 struct ttm_bo_global *glob = bdev->glob;
623 struct ttm_buffer_object *entry = NULL;
626 spin_lock(&glob->lru_lock);
627 if (list_empty(&bdev->ddestroy))
630 entry = list_first_entry(&bdev->ddestroy,
631 struct ttm_buffer_object, ddestroy);
632 kref_get(&entry->list_kref);
635 struct ttm_buffer_object *nentry = NULL;
637 if (entry->ddestroy.next != &bdev->ddestroy) {
638 nentry = list_first_entry(&entry->ddestroy,
639 struct ttm_buffer_object, ddestroy);
640 kref_get(&nentry->list_kref);
643 spin_unlock(&glob->lru_lock);
644 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
646 kref_put(&entry->list_kref, ttm_bo_release_list);
652 spin_lock(&glob->lru_lock);
653 if (list_empty(&entry->ddestroy))
658 spin_unlock(&glob->lru_lock);
661 kref_put(&entry->list_kref, ttm_bo_release_list);
665 static void ttm_bo_delayed_workqueue(struct work_struct *work)
667 struct ttm_bo_device *bdev =
668 container_of(work, struct ttm_bo_device, wq.work);
670 if (ttm_bo_delayed_delete(bdev, false)) {
671 schedule_delayed_work(&bdev->wq,
672 ((HZ / 100) < 1) ? 1 : HZ / 100);
676 static void ttm_bo_release(struct kref *kref)
678 struct ttm_buffer_object *bo =
679 container_of(kref, struct ttm_buffer_object, kref);
680 struct ttm_bo_device *bdev = bo->bdev;
681 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
683 if (likely(bo->vm_node != NULL)) {
684 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
685 drm_mm_put_block(bo->vm_node);
688 write_unlock(&bdev->vm_lock);
689 ttm_mem_io_lock(man, false);
690 ttm_mem_io_free_vm(bo);
691 ttm_mem_io_unlock(man);
692 ttm_bo_cleanup_refs_or_queue(bo);
693 kref_put(&bo->list_kref, ttm_bo_release_list);
694 write_lock(&bdev->vm_lock);
697 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
699 struct ttm_buffer_object *bo = *p_bo;
700 struct ttm_bo_device *bdev = bo->bdev;
703 write_lock(&bdev->vm_lock);
704 kref_put(&bo->kref, ttm_bo_release);
705 write_unlock(&bdev->vm_lock);
707 EXPORT_SYMBOL(ttm_bo_unref);
709 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
711 return cancel_delayed_work_sync(&bdev->wq);
713 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
715 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
718 schedule_delayed_work(&bdev->wq,
719 ((HZ / 100) < 1) ? 1 : HZ / 100);
721 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
723 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
724 bool no_wait_reserve, bool no_wait_gpu)
726 struct ttm_bo_device *bdev = bo->bdev;
727 struct ttm_mem_reg evict_mem;
728 struct ttm_placement placement;
731 spin_lock(&bdev->fence_lock);
732 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
733 spin_unlock(&bdev->fence_lock);
735 if (unlikely(ret != 0)) {
736 if (ret != -ERESTARTSYS) {
737 pr_err("Failed to expire sync object before buffer eviction\n");
742 BUG_ON(!atomic_read(&bo->reserved));
745 evict_mem.mm_node = NULL;
746 evict_mem.bus.io_reserved_vm = false;
747 evict_mem.bus.io_reserved_count = 0;
751 placement.num_placement = 0;
752 placement.num_busy_placement = 0;
753 bdev->driver->evict_flags(bo, &placement);
754 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
755 no_wait_reserve, no_wait_gpu);
757 if (ret != -ERESTARTSYS) {
758 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
760 ttm_bo_mem_space_debug(bo, &placement);
765 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
766 no_wait_reserve, no_wait_gpu);
768 if (ret != -ERESTARTSYS)
769 pr_err("Buffer eviction failed\n");
770 ttm_bo_mem_put(bo, &evict_mem);
778 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
780 bool interruptible, bool no_wait_reserve,
783 struct ttm_bo_global *glob = bdev->glob;
784 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
785 struct ttm_buffer_object *bo;
786 int ret, put_count = 0;
789 spin_lock(&glob->lru_lock);
790 if (list_empty(&man->lru)) {
791 spin_unlock(&glob->lru_lock);
795 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
796 kref_get(&bo->list_kref);
798 if (!list_empty(&bo->ddestroy)) {
799 spin_unlock(&glob->lru_lock);
800 ret = ttm_bo_cleanup_refs(bo, interruptible,
801 no_wait_reserve, no_wait_gpu);
802 kref_put(&bo->list_kref, ttm_bo_release_list);
804 if (likely(ret == 0 || ret == -ERESTARTSYS))
810 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
812 if (unlikely(ret == -EBUSY)) {
813 spin_unlock(&glob->lru_lock);
814 if (likely(!no_wait_gpu))
815 ret = ttm_bo_wait_unreserved(bo, interruptible);
817 kref_put(&bo->list_kref, ttm_bo_release_list);
820 * We *need* to retry after releasing the lru lock.
823 if (unlikely(ret != 0))
828 put_count = ttm_bo_del_from_lru(bo);
829 spin_unlock(&glob->lru_lock);
833 ttm_bo_list_ref_sub(bo, put_count, true);
835 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
836 ttm_bo_unreserve(bo);
838 kref_put(&bo->list_kref, ttm_bo_release_list);
842 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
844 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
847 (*man->func->put_node)(man, mem);
849 EXPORT_SYMBOL(ttm_bo_mem_put);
852 * Repeatedly evict memory from the LRU for @mem_type until we create enough
853 * space, or we've evicted everything and there isn't enough space.
855 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
857 struct ttm_placement *placement,
858 struct ttm_mem_reg *mem,
860 bool no_wait_reserve,
863 struct ttm_bo_device *bdev = bo->bdev;
864 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
868 ret = (*man->func->get_node)(man, bo, placement, mem);
869 if (unlikely(ret != 0))
873 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
874 no_wait_reserve, no_wait_gpu);
875 if (unlikely(ret != 0))
878 if (mem->mm_node == NULL)
880 mem->mem_type = mem_type;
884 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
885 uint32_t cur_placement,
886 uint32_t proposed_placement)
888 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
889 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
892 * Keep current caching if possible.
895 if ((cur_placement & caching) != 0)
896 result |= (cur_placement & caching);
897 else if ((man->default_caching & caching) != 0)
898 result |= man->default_caching;
899 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
900 result |= TTM_PL_FLAG_CACHED;
901 else if ((TTM_PL_FLAG_WC & caching) != 0)
902 result |= TTM_PL_FLAG_WC;
903 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
904 result |= TTM_PL_FLAG_UNCACHED;
909 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
911 uint32_t proposed_placement,
912 uint32_t *masked_placement)
914 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
916 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
919 if ((proposed_placement & man->available_caching) == 0)
922 cur_flags |= (proposed_placement & man->available_caching);
924 *masked_placement = cur_flags;
929 * Creates space for memory region @mem according to its type.
931 * This function first searches for free space in compatible memory types in
932 * the priority order defined by the driver. If free space isn't found, then
933 * ttm_bo_mem_force_space is attempted in priority order to evict and find
936 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
937 struct ttm_placement *placement,
938 struct ttm_mem_reg *mem,
939 bool interruptible, bool no_wait_reserve,
942 struct ttm_bo_device *bdev = bo->bdev;
943 struct ttm_mem_type_manager *man;
944 uint32_t mem_type = TTM_PL_SYSTEM;
945 uint32_t cur_flags = 0;
946 bool type_found = false;
947 bool type_ok = false;
948 bool has_erestartsys = false;
952 for (i = 0; i < placement->num_placement; ++i) {
953 ret = ttm_mem_type_from_flags(placement->placement[i],
957 man = &bdev->man[mem_type];
959 type_ok = ttm_bo_mt_compatible(man,
961 placement->placement[i],
967 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
970 * Use the access and other non-mapping-related flag bits from
971 * the memory placement flags to the current flags
973 ttm_flag_masked(&cur_flags, placement->placement[i],
974 ~TTM_PL_MASK_MEMTYPE);
976 if (mem_type == TTM_PL_SYSTEM)
979 if (man->has_type && man->use_type) {
981 ret = (*man->func->get_node)(man, bo, placement, mem);
989 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
990 mem->mem_type = mem_type;
991 mem->placement = cur_flags;
998 for (i = 0; i < placement->num_busy_placement; ++i) {
999 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1003 man = &bdev->man[mem_type];
1006 if (!ttm_bo_mt_compatible(man,
1008 placement->busy_placement[i],
1012 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1015 * Use the access and other non-mapping-related flag bits from
1016 * the memory placement flags to the current flags
1018 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1019 ~TTM_PL_MASK_MEMTYPE);
1022 if (mem_type == TTM_PL_SYSTEM) {
1023 mem->mem_type = mem_type;
1024 mem->placement = cur_flags;
1025 mem->mm_node = NULL;
1029 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1030 interruptible, no_wait_reserve, no_wait_gpu);
1031 if (ret == 0 && mem->mm_node) {
1032 mem->placement = cur_flags;
1035 if (ret == -ERESTARTSYS)
1036 has_erestartsys = true;
1038 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1041 EXPORT_SYMBOL(ttm_bo_mem_space);
1043 int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1045 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1048 return wait_event_interruptible(bo->event_queue,
1049 atomic_read(&bo->cpu_writers) == 0);
1051 EXPORT_SYMBOL(ttm_bo_wait_cpu);
1053 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1054 struct ttm_placement *placement,
1055 bool interruptible, bool no_wait_reserve,
1059 struct ttm_mem_reg mem;
1060 struct ttm_bo_device *bdev = bo->bdev;
1062 BUG_ON(!atomic_read(&bo->reserved));
1065 * FIXME: It's possible to pipeline buffer moves.
1066 * Have the driver move function wait for idle when necessary,
1067 * instead of doing it here.
1069 spin_lock(&bdev->fence_lock);
1070 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1071 spin_unlock(&bdev->fence_lock);
1074 mem.num_pages = bo->num_pages;
1075 mem.size = mem.num_pages << PAGE_SHIFT;
1076 mem.page_alignment = bo->mem.page_alignment;
1077 mem.bus.io_reserved_vm = false;
1078 mem.bus.io_reserved_count = 0;
1080 * Determine where to move the buffer.
1082 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
1085 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
1087 if (ret && mem.mm_node)
1088 ttm_bo_mem_put(bo, &mem);
1092 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1093 struct ttm_mem_reg *mem)
1097 if (mem->mm_node && placement->lpfn != 0 &&
1098 (mem->start < placement->fpfn ||
1099 mem->start + mem->num_pages > placement->lpfn))
1102 for (i = 0; i < placement->num_placement; i++) {
1103 if ((placement->placement[i] & mem->placement &
1104 TTM_PL_MASK_CACHING) &&
1105 (placement->placement[i] & mem->placement &
1112 int ttm_bo_validate(struct ttm_buffer_object *bo,
1113 struct ttm_placement *placement,
1114 bool interruptible, bool no_wait_reserve,
1119 BUG_ON(!atomic_read(&bo->reserved));
1120 /* Check that range is valid */
1121 if (placement->lpfn || placement->fpfn)
1122 if (placement->fpfn > placement->lpfn ||
1123 (placement->lpfn - placement->fpfn) < bo->num_pages)
1126 * Check whether we need to move buffer.
1128 ret = ttm_bo_mem_compat(placement, &bo->mem);
1130 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
1135 * Use the access and other non-mapping-related flag bits from
1136 * the compatible memory placement flags to the active flags
1138 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1139 ~TTM_PL_MASK_MEMTYPE);
1142 * We might need to add a TTM.
1144 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1145 ret = ttm_bo_add_ttm(bo, true);
1151 EXPORT_SYMBOL(ttm_bo_validate);
1153 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1154 struct ttm_placement *placement)
1156 BUG_ON((placement->fpfn || placement->lpfn) &&
1157 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1162 int ttm_bo_init(struct ttm_bo_device *bdev,
1163 struct ttm_buffer_object *bo,
1165 enum ttm_bo_type type,
1166 struct ttm_placement *placement,
1167 uint32_t page_alignment,
1168 unsigned long buffer_start,
1170 struct file *persistent_swap_storage,
1172 void (*destroy) (struct ttm_buffer_object *))
1175 unsigned long num_pages;
1176 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1178 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1180 pr_err("Out of kernel memory\n");
1188 size += buffer_start & ~PAGE_MASK;
1189 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1190 if (num_pages == 0) {
1191 pr_err("Illegal buffer object size\n");
1198 bo->destroy = destroy;
1200 kref_init(&bo->kref);
1201 kref_init(&bo->list_kref);
1202 atomic_set(&bo->cpu_writers, 0);
1203 atomic_set(&bo->reserved, 1);
1204 init_waitqueue_head(&bo->event_queue);
1205 INIT_LIST_HEAD(&bo->lru);
1206 INIT_LIST_HEAD(&bo->ddestroy);
1207 INIT_LIST_HEAD(&bo->swap);
1208 INIT_LIST_HEAD(&bo->io_reserve_lru);
1210 bo->glob = bdev->glob;
1212 bo->num_pages = num_pages;
1213 bo->mem.size = num_pages << PAGE_SHIFT;
1214 bo->mem.mem_type = TTM_PL_SYSTEM;
1215 bo->mem.num_pages = bo->num_pages;
1216 bo->mem.mm_node = NULL;
1217 bo->mem.page_alignment = page_alignment;
1218 bo->mem.bus.io_reserved_vm = false;
1219 bo->mem.bus.io_reserved_count = 0;
1220 bo->buffer_start = buffer_start & PAGE_MASK;
1222 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1223 bo->seq_valid = false;
1224 bo->persistent_swap_storage = persistent_swap_storage;
1225 bo->acc_size = acc_size;
1226 atomic_inc(&bo->glob->bo_count);
1228 ret = ttm_bo_check_placement(bo, placement);
1229 if (unlikely(ret != 0))
1233 * For ttm_bo_type_device buffers, allocate
1234 * address space from the device.
1236 if (bo->type == ttm_bo_type_device) {
1237 ret = ttm_bo_setup_vm(bo);
1242 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
1246 ttm_bo_unreserve(bo);
1250 ttm_bo_unreserve(bo);
1255 EXPORT_SYMBOL(ttm_bo_init);
1257 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1258 unsigned long bo_size,
1259 unsigned struct_size)
1261 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1264 size += ttm_round_pot(struct_size);
1265 size += PAGE_ALIGN(npages * sizeof(void *));
1266 size += ttm_round_pot(sizeof(struct ttm_tt));
1269 EXPORT_SYMBOL(ttm_bo_acc_size);
1271 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1272 unsigned long bo_size,
1273 unsigned struct_size)
1275 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1278 size += ttm_round_pot(struct_size);
1279 size += PAGE_ALIGN(npages * sizeof(void *));
1280 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1281 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1284 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1286 int ttm_bo_create(struct ttm_bo_device *bdev,
1288 enum ttm_bo_type type,
1289 struct ttm_placement *placement,
1290 uint32_t page_alignment,
1291 unsigned long buffer_start,
1293 struct file *persistent_swap_storage,
1294 struct ttm_buffer_object **p_bo)
1296 struct ttm_buffer_object *bo;
1297 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1301 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1302 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1303 if (unlikely(ret != 0))
1306 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1308 if (unlikely(bo == NULL)) {
1309 ttm_mem_global_free(mem_glob, acc_size);
1313 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1314 buffer_start, interruptible,
1315 persistent_swap_storage, acc_size, NULL);
1316 if (likely(ret == 0))
1321 EXPORT_SYMBOL(ttm_bo_create);
1323 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1324 unsigned mem_type, bool allow_errors)
1326 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1327 struct ttm_bo_global *glob = bdev->glob;
1331 * Can't use standard list traversal since we're unlocking.
1334 spin_lock(&glob->lru_lock);
1335 while (!list_empty(&man->lru)) {
1336 spin_unlock(&glob->lru_lock);
1337 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
1342 pr_err("Cleanup eviction failed\n");
1345 spin_lock(&glob->lru_lock);
1347 spin_unlock(&glob->lru_lock);
1351 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1353 struct ttm_mem_type_manager *man;
1356 if (mem_type >= TTM_NUM_MEM_TYPES) {
1357 pr_err("Illegal memory type %d\n", mem_type);
1360 man = &bdev->man[mem_type];
1362 if (!man->has_type) {
1363 pr_err("Trying to take down uninitialized memory manager type %u\n",
1368 man->use_type = false;
1369 man->has_type = false;
1373 ttm_bo_force_list_clean(bdev, mem_type, false);
1375 ret = (*man->func->takedown)(man);
1380 EXPORT_SYMBOL(ttm_bo_clean_mm);
1382 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1384 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1386 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1387 pr_err("Illegal memory manager memory type %u\n", mem_type);
1391 if (!man->has_type) {
1392 pr_err("Memory type %u has not been initialized\n", mem_type);
1396 return ttm_bo_force_list_clean(bdev, mem_type, true);
1398 EXPORT_SYMBOL(ttm_bo_evict_mm);
1400 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1401 unsigned long p_size)
1404 struct ttm_mem_type_manager *man;
1406 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1407 man = &bdev->man[type];
1408 BUG_ON(man->has_type);
1409 man->io_reserve_fastpath = true;
1410 man->use_io_reserve_lru = false;
1411 mutex_init(&man->io_reserve_mutex);
1412 INIT_LIST_HEAD(&man->io_reserve_lru);
1414 ret = bdev->driver->init_mem_type(bdev, type, man);
1420 if (type != TTM_PL_SYSTEM) {
1421 ret = (*man->func->init)(man, p_size);
1425 man->has_type = true;
1426 man->use_type = true;
1429 INIT_LIST_HEAD(&man->lru);
1433 EXPORT_SYMBOL(ttm_bo_init_mm);
1435 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1437 struct ttm_bo_global *glob =
1438 container_of(kobj, struct ttm_bo_global, kobj);
1440 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1441 __free_page(glob->dummy_read_page);
1445 void ttm_bo_global_release(struct drm_global_reference *ref)
1447 struct ttm_bo_global *glob = ref->object;
1449 kobject_del(&glob->kobj);
1450 kobject_put(&glob->kobj);
1452 EXPORT_SYMBOL(ttm_bo_global_release);
1454 int ttm_bo_global_init(struct drm_global_reference *ref)
1456 struct ttm_bo_global_ref *bo_ref =
1457 container_of(ref, struct ttm_bo_global_ref, ref);
1458 struct ttm_bo_global *glob = ref->object;
1461 mutex_init(&glob->device_list_mutex);
1462 spin_lock_init(&glob->lru_lock);
1463 glob->mem_glob = bo_ref->mem_glob;
1464 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1466 if (unlikely(glob->dummy_read_page == NULL)) {
1471 INIT_LIST_HEAD(&glob->swap_lru);
1472 INIT_LIST_HEAD(&glob->device_list);
1474 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1475 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1476 if (unlikely(ret != 0)) {
1477 pr_err("Could not register buffer object swapout\n");
1481 atomic_set(&glob->bo_count, 0);
1483 ret = kobject_init_and_add(
1484 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1485 if (unlikely(ret != 0))
1486 kobject_put(&glob->kobj);
1489 __free_page(glob->dummy_read_page);
1494 EXPORT_SYMBOL(ttm_bo_global_init);
1497 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1500 unsigned i = TTM_NUM_MEM_TYPES;
1501 struct ttm_mem_type_manager *man;
1502 struct ttm_bo_global *glob = bdev->glob;
1505 man = &bdev->man[i];
1506 if (man->has_type) {
1507 man->use_type = false;
1508 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1510 pr_err("DRM memory manager type %d is not clean\n",
1513 man->has_type = false;
1517 mutex_lock(&glob->device_list_mutex);
1518 list_del(&bdev->device_list);
1519 mutex_unlock(&glob->device_list_mutex);
1521 cancel_delayed_work_sync(&bdev->wq);
1523 while (ttm_bo_delayed_delete(bdev, true))
1526 spin_lock(&glob->lru_lock);
1527 if (list_empty(&bdev->ddestroy))
1528 TTM_DEBUG("Delayed destroy list was clean\n");
1530 if (list_empty(&bdev->man[0].lru))
1531 TTM_DEBUG("Swap list was clean\n");
1532 spin_unlock(&glob->lru_lock);
1534 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1535 write_lock(&bdev->vm_lock);
1536 drm_mm_takedown(&bdev->addr_space_mm);
1537 write_unlock(&bdev->vm_lock);
1541 EXPORT_SYMBOL(ttm_bo_device_release);
1543 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1544 struct ttm_bo_global *glob,
1545 struct ttm_bo_driver *driver,
1546 uint64_t file_page_offset,
1551 rwlock_init(&bdev->vm_lock);
1552 bdev->driver = driver;
1554 memset(bdev->man, 0, sizeof(bdev->man));
1557 * Initialize the system memory buffer type.
1558 * Other types need to be driver / IOCTL initialized.
1560 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1561 if (unlikely(ret != 0))
1564 bdev->addr_space_rb = RB_ROOT;
1565 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1566 if (unlikely(ret != 0))
1567 goto out_no_addr_mm;
1569 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1570 bdev->nice_mode = true;
1571 INIT_LIST_HEAD(&bdev->ddestroy);
1572 bdev->dev_mapping = NULL;
1574 bdev->need_dma32 = need_dma32;
1576 spin_lock_init(&bdev->fence_lock);
1577 mutex_lock(&glob->device_list_mutex);
1578 list_add_tail(&bdev->device_list, &glob->device_list);
1579 mutex_unlock(&glob->device_list_mutex);
1583 ttm_bo_clean_mm(bdev, 0);
1587 EXPORT_SYMBOL(ttm_bo_device_init);
1590 * buffer object vm functions.
1593 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1595 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1597 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1598 if (mem->mem_type == TTM_PL_SYSTEM)
1601 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1604 if (mem->placement & TTM_PL_FLAG_CACHED)
1610 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1612 struct ttm_bo_device *bdev = bo->bdev;
1613 loff_t offset = (loff_t) bo->addr_space_offset;
1614 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1616 if (!bdev->dev_mapping)
1618 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1619 ttm_mem_io_free_vm(bo);
1622 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1624 struct ttm_bo_device *bdev = bo->bdev;
1625 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1627 ttm_mem_io_lock(man, false);
1628 ttm_bo_unmap_virtual_locked(bo);
1629 ttm_mem_io_unlock(man);
1633 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1635 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1637 struct ttm_bo_device *bdev = bo->bdev;
1638 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1639 struct rb_node *parent = NULL;
1640 struct ttm_buffer_object *cur_bo;
1641 unsigned long offset = bo->vm_node->start;
1642 unsigned long cur_offset;
1646 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1647 cur_offset = cur_bo->vm_node->start;
1648 if (offset < cur_offset)
1649 cur = &parent->rb_left;
1650 else if (offset > cur_offset)
1651 cur = &parent->rb_right;
1656 rb_link_node(&bo->vm_rb, parent, cur);
1657 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1663 * @bo: the buffer to allocate address space for
1665 * Allocate address space in the drm device so that applications
1666 * can mmap the buffer and access the contents. This only
1667 * applies to ttm_bo_type_device objects as others are not
1668 * placed in the drm device address space.
1671 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1673 struct ttm_bo_device *bdev = bo->bdev;
1677 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1678 if (unlikely(ret != 0))
1681 write_lock(&bdev->vm_lock);
1682 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1683 bo->mem.num_pages, 0, 0);
1685 if (unlikely(bo->vm_node == NULL)) {
1690 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1691 bo->mem.num_pages, 0);
1693 if (unlikely(bo->vm_node == NULL)) {
1694 write_unlock(&bdev->vm_lock);
1698 ttm_bo_vm_insert_rb(bo);
1699 write_unlock(&bdev->vm_lock);
1700 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1704 write_unlock(&bdev->vm_lock);
1708 int ttm_bo_wait(struct ttm_buffer_object *bo,
1709 bool lazy, bool interruptible, bool no_wait)
1711 struct ttm_bo_driver *driver = bo->bdev->driver;
1712 struct ttm_bo_device *bdev = bo->bdev;
1717 if (likely(bo->sync_obj == NULL))
1720 while (bo->sync_obj) {
1722 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1723 void *tmp_obj = bo->sync_obj;
1724 bo->sync_obj = NULL;
1725 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1726 spin_unlock(&bdev->fence_lock);
1727 driver->sync_obj_unref(&tmp_obj);
1728 spin_lock(&bdev->fence_lock);
1735 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1736 sync_obj_arg = bo->sync_obj_arg;
1737 spin_unlock(&bdev->fence_lock);
1738 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1739 lazy, interruptible);
1740 if (unlikely(ret != 0)) {
1741 driver->sync_obj_unref(&sync_obj);
1742 spin_lock(&bdev->fence_lock);
1745 spin_lock(&bdev->fence_lock);
1746 if (likely(bo->sync_obj == sync_obj &&
1747 bo->sync_obj_arg == sync_obj_arg)) {
1748 void *tmp_obj = bo->sync_obj;
1749 bo->sync_obj = NULL;
1750 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1752 spin_unlock(&bdev->fence_lock);
1753 driver->sync_obj_unref(&sync_obj);
1754 driver->sync_obj_unref(&tmp_obj);
1755 spin_lock(&bdev->fence_lock);
1757 spin_unlock(&bdev->fence_lock);
1758 driver->sync_obj_unref(&sync_obj);
1759 spin_lock(&bdev->fence_lock);
1764 EXPORT_SYMBOL(ttm_bo_wait);
1766 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1768 struct ttm_bo_device *bdev = bo->bdev;
1772 * Using ttm_bo_reserve makes sure the lru lists are updated.
1775 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1776 if (unlikely(ret != 0))
1778 spin_lock(&bdev->fence_lock);
1779 ret = ttm_bo_wait(bo, false, true, no_wait);
1780 spin_unlock(&bdev->fence_lock);
1781 if (likely(ret == 0))
1782 atomic_inc(&bo->cpu_writers);
1783 ttm_bo_unreserve(bo);
1786 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1788 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1790 if (atomic_dec_and_test(&bo->cpu_writers))
1791 wake_up_all(&bo->event_queue);
1793 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1796 * A buffer object shrink method that tries to swap out the first
1797 * buffer object on the bo_global::swap_lru list.
1800 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1802 struct ttm_bo_global *glob =
1803 container_of(shrink, struct ttm_bo_global, shrink);
1804 struct ttm_buffer_object *bo;
1807 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1809 spin_lock(&glob->lru_lock);
1810 while (ret == -EBUSY) {
1811 if (unlikely(list_empty(&glob->swap_lru))) {
1812 spin_unlock(&glob->lru_lock);
1816 bo = list_first_entry(&glob->swap_lru,
1817 struct ttm_buffer_object, swap);
1818 kref_get(&bo->list_kref);
1820 if (!list_empty(&bo->ddestroy)) {
1821 spin_unlock(&glob->lru_lock);
1822 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1823 kref_put(&bo->list_kref, ttm_bo_release_list);
1828 * Reserve buffer. Since we unlock while sleeping, we need
1829 * to re-check that nobody removed us from the swap-list while
1833 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1834 if (unlikely(ret == -EBUSY)) {
1835 spin_unlock(&glob->lru_lock);
1836 ttm_bo_wait_unreserved(bo, false);
1837 kref_put(&bo->list_kref, ttm_bo_release_list);
1838 spin_lock(&glob->lru_lock);
1843 put_count = ttm_bo_del_from_lru(bo);
1844 spin_unlock(&glob->lru_lock);
1846 ttm_bo_list_ref_sub(bo, put_count, true);
1849 * Wait for GPU, then move to system cached.
1852 spin_lock(&bo->bdev->fence_lock);
1853 ret = ttm_bo_wait(bo, false, false, false);
1854 spin_unlock(&bo->bdev->fence_lock);
1856 if (unlikely(ret != 0))
1859 if ((bo->mem.placement & swap_placement) != swap_placement) {
1860 struct ttm_mem_reg evict_mem;
1862 evict_mem = bo->mem;
1863 evict_mem.mm_node = NULL;
1864 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1865 evict_mem.mem_type = TTM_PL_SYSTEM;
1867 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1868 false, false, false);
1869 if (unlikely(ret != 0))
1873 ttm_bo_unmap_virtual(bo);
1876 * Swap out. Buffer will be swapped in again as soon as
1877 * anyone tries to access a ttm page.
1880 if (bo->bdev->driver->swap_notify)
1881 bo->bdev->driver->swap_notify(bo);
1883 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1888 * Unreserve without putting on LRU to avoid swapping out an
1889 * already swapped buffer.
1892 atomic_set(&bo->reserved, 0);
1893 wake_up_all(&bo->event_queue);
1894 kref_put(&bo->list_kref, ttm_bo_release_list);
1898 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1900 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1903 EXPORT_SYMBOL(ttm_bo_swapout_all);