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 <drm/ttm/ttm_module.h>
34 #include <drm/ttm/ttm_bo_driver.h>
35 #include <drm/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 !ttm_bo_is_reserved(bo));
167 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
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(!ttm_bo_is_reserved(bo));
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_read(&bo->reserved) != 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);
252 atomic_set(&bo->reserved, 1);
255 * Wake up waiters that may need to recheck for deadlock,
256 * if we decreased the sequence number.
258 if (unlikely((bo->val_seq - sequence < (1 << 31))
260 wake_up_all(&bo->event_queue);
262 bo->val_seq = sequence;
263 bo->seq_valid = true;
265 bo->seq_valid = false;
270 EXPORT_SYMBOL(ttm_bo_reserve);
272 static void ttm_bo_ref_bug(struct kref *list_kref)
277 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
280 kref_sub(&bo->list_kref, count,
281 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
284 int ttm_bo_reserve(struct ttm_buffer_object *bo,
286 bool no_wait, bool use_sequence, uint32_t sequence)
288 struct ttm_bo_global *glob = bo->glob;
292 spin_lock(&glob->lru_lock);
293 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
295 if (likely(ret == 0))
296 put_count = ttm_bo_del_from_lru(bo);
297 spin_unlock(&glob->lru_lock);
299 ttm_bo_list_ref_sub(bo, put_count, true);
304 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
306 ttm_bo_add_to_lru(bo);
307 atomic_set(&bo->reserved, 0);
308 wake_up_all(&bo->event_queue);
311 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
313 struct ttm_bo_global *glob = bo->glob;
315 spin_lock(&glob->lru_lock);
316 ttm_bo_unreserve_locked(bo);
317 spin_unlock(&glob->lru_lock);
319 EXPORT_SYMBOL(ttm_bo_unreserve);
322 * Call bo->mutex locked.
324 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
326 struct ttm_bo_device *bdev = bo->bdev;
327 struct ttm_bo_global *glob = bo->glob;
329 uint32_t page_flags = 0;
331 TTM_ASSERT_LOCKED(&bo->mutex);
334 if (bdev->need_dma32)
335 page_flags |= TTM_PAGE_FLAG_DMA32;
338 case ttm_bo_type_device:
340 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
341 case ttm_bo_type_kernel:
342 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
343 page_flags, glob->dummy_read_page);
344 if (unlikely(bo->ttm == NULL))
348 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
349 page_flags | TTM_PAGE_FLAG_SG,
350 glob->dummy_read_page);
351 if (unlikely(bo->ttm == NULL)) {
355 bo->ttm->sg = bo->sg;
358 pr_err("Illegal buffer object type\n");
366 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
367 struct ttm_mem_reg *mem,
368 bool evict, bool interruptible,
369 bool no_wait_reserve, bool no_wait_gpu)
371 struct ttm_bo_device *bdev = bo->bdev;
372 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
373 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
374 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
375 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
378 if (old_is_pci || new_is_pci ||
379 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
380 ret = ttm_mem_io_lock(old_man, true);
381 if (unlikely(ret != 0))
383 ttm_bo_unmap_virtual_locked(bo);
384 ttm_mem_io_unlock(old_man);
388 * Create and bind a ttm if required.
391 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
392 if (bo->ttm == NULL) {
393 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
394 ret = ttm_bo_add_ttm(bo, zero);
399 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
403 if (mem->mem_type != TTM_PL_SYSTEM) {
404 ret = ttm_tt_bind(bo->ttm, mem);
409 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
410 if (bdev->driver->move_notify)
411 bdev->driver->move_notify(bo, mem);
418 if (bdev->driver->move_notify)
419 bdev->driver->move_notify(bo, mem);
421 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
422 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
423 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
424 else if (bdev->driver->move)
425 ret = bdev->driver->move(bo, evict, interruptible,
426 no_wait_reserve, no_wait_gpu, mem);
428 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
431 if (bdev->driver->move_notify) {
432 struct ttm_mem_reg tmp_mem = *mem;
435 bdev->driver->move_notify(bo, mem);
444 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
446 pr_err("Can not flush read caches\n");
450 if (bo->mem.mm_node) {
451 bo->offset = (bo->mem.start << PAGE_SHIFT) +
452 bdev->man[bo->mem.mem_type].gpu_offset;
453 bo->cur_placement = bo->mem.placement;
460 new_man = &bdev->man[bo->mem.mem_type];
461 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
462 ttm_tt_unbind(bo->ttm);
463 ttm_tt_destroy(bo->ttm);
472 * Will release GPU memory type usage on destruction.
473 * This is the place to put in driver specific hooks to release
474 * driver private resources.
475 * Will release the bo::reserved lock.
478 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
480 if (bo->bdev->driver->move_notify)
481 bo->bdev->driver->move_notify(bo, NULL);
484 ttm_tt_unbind(bo->ttm);
485 ttm_tt_destroy(bo->ttm);
488 ttm_bo_mem_put(bo, &bo->mem);
490 atomic_set(&bo->reserved, 0);
493 * Make processes trying to reserve really pick it up.
495 smp_mb__after_atomic_dec();
496 wake_up_all(&bo->event_queue);
499 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
501 struct ttm_bo_device *bdev = bo->bdev;
502 struct ttm_bo_global *glob = bo->glob;
503 struct ttm_bo_driver *driver;
504 void *sync_obj = NULL;
508 spin_lock(&bdev->fence_lock);
509 (void) ttm_bo_wait(bo, false, false, true);
512 spin_lock(&glob->lru_lock);
515 * Lock inversion between bo:reserve and bdev::fence_lock here,
516 * but that's OK, since we're only trylocking.
519 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
521 if (unlikely(ret == -EBUSY))
524 spin_unlock(&bdev->fence_lock);
525 put_count = ttm_bo_del_from_lru(bo);
527 spin_unlock(&glob->lru_lock);
528 ttm_bo_cleanup_memtype_use(bo);
530 ttm_bo_list_ref_sub(bo, put_count, true);
534 spin_lock(&glob->lru_lock);
537 driver = bdev->driver;
539 sync_obj = driver->sync_obj_ref(bo->sync_obj);
541 kref_get(&bo->list_kref);
542 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
543 spin_unlock(&glob->lru_lock);
544 spin_unlock(&bdev->fence_lock);
547 driver->sync_obj_flush(sync_obj);
548 driver->sync_obj_unref(&sync_obj);
550 schedule_delayed_work(&bdev->wq,
551 ((HZ / 100) < 1) ? 1 : HZ / 100);
555 * function ttm_bo_cleanup_refs
556 * If bo idle, remove from delayed- and lru lists, and unref.
557 * If not idle, do nothing.
559 * @interruptible Any sleeps should occur interruptibly.
560 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
561 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
564 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
566 bool no_wait_reserve,
569 struct ttm_bo_device *bdev = bo->bdev;
570 struct ttm_bo_global *glob = bo->glob;
575 spin_lock(&bdev->fence_lock);
576 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
577 spin_unlock(&bdev->fence_lock);
579 if (unlikely(ret != 0))
583 spin_lock(&glob->lru_lock);
585 if (unlikely(list_empty(&bo->ddestroy))) {
586 spin_unlock(&glob->lru_lock);
590 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
592 if (unlikely(ret == -EBUSY)) {
593 spin_unlock(&glob->lru_lock);
594 if (likely(!no_wait_reserve))
595 ret = ttm_bo_wait_unreserved(bo, interruptible);
596 if (unlikely(ret != 0))
605 * We can re-check for sync object without taking
606 * the bo::lock since setting the sync object requires
607 * also bo::reserved. A busy object at this point may
608 * be caused by another thread recently starting an accelerated
612 if (unlikely(bo->sync_obj)) {
613 atomic_set(&bo->reserved, 0);
614 wake_up_all(&bo->event_queue);
615 spin_unlock(&glob->lru_lock);
619 put_count = ttm_bo_del_from_lru(bo);
620 list_del_init(&bo->ddestroy);
623 spin_unlock(&glob->lru_lock);
624 ttm_bo_cleanup_memtype_use(bo);
626 ttm_bo_list_ref_sub(bo, put_count, true);
632 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
633 * encountered buffers.
636 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
638 struct ttm_bo_global *glob = bdev->glob;
639 struct ttm_buffer_object *entry = NULL;
642 spin_lock(&glob->lru_lock);
643 if (list_empty(&bdev->ddestroy))
646 entry = list_first_entry(&bdev->ddestroy,
647 struct ttm_buffer_object, ddestroy);
648 kref_get(&entry->list_kref);
651 struct ttm_buffer_object *nentry = NULL;
653 if (entry->ddestroy.next != &bdev->ddestroy) {
654 nentry = list_first_entry(&entry->ddestroy,
655 struct ttm_buffer_object, ddestroy);
656 kref_get(&nentry->list_kref);
659 spin_unlock(&glob->lru_lock);
660 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
662 kref_put(&entry->list_kref, ttm_bo_release_list);
668 spin_lock(&glob->lru_lock);
669 if (list_empty(&entry->ddestroy))
674 spin_unlock(&glob->lru_lock);
677 kref_put(&entry->list_kref, ttm_bo_release_list);
681 static void ttm_bo_delayed_workqueue(struct work_struct *work)
683 struct ttm_bo_device *bdev =
684 container_of(work, struct ttm_bo_device, wq.work);
686 if (ttm_bo_delayed_delete(bdev, false)) {
687 schedule_delayed_work(&bdev->wq,
688 ((HZ / 100) < 1) ? 1 : HZ / 100);
692 static void ttm_bo_release(struct kref *kref)
694 struct ttm_buffer_object *bo =
695 container_of(kref, struct ttm_buffer_object, kref);
696 struct ttm_bo_device *bdev = bo->bdev;
697 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
699 if (likely(bo->vm_node != NULL)) {
700 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
701 drm_mm_put_block(bo->vm_node);
704 write_unlock(&bdev->vm_lock);
705 ttm_mem_io_lock(man, false);
706 ttm_mem_io_free_vm(bo);
707 ttm_mem_io_unlock(man);
708 ttm_bo_cleanup_refs_or_queue(bo);
709 kref_put(&bo->list_kref, ttm_bo_release_list);
710 write_lock(&bdev->vm_lock);
713 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
715 struct ttm_buffer_object *bo = *p_bo;
716 struct ttm_bo_device *bdev = bo->bdev;
719 write_lock(&bdev->vm_lock);
720 kref_put(&bo->kref, ttm_bo_release);
721 write_unlock(&bdev->vm_lock);
723 EXPORT_SYMBOL(ttm_bo_unref);
725 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
727 return cancel_delayed_work_sync(&bdev->wq);
729 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
731 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
734 schedule_delayed_work(&bdev->wq,
735 ((HZ / 100) < 1) ? 1 : HZ / 100);
737 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
739 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
740 bool no_wait_reserve, bool no_wait_gpu)
742 struct ttm_bo_device *bdev = bo->bdev;
743 struct ttm_mem_reg evict_mem;
744 struct ttm_placement placement;
747 spin_lock(&bdev->fence_lock);
748 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
749 spin_unlock(&bdev->fence_lock);
751 if (unlikely(ret != 0)) {
752 if (ret != -ERESTARTSYS) {
753 pr_err("Failed to expire sync object before buffer eviction\n");
758 BUG_ON(!ttm_bo_is_reserved(bo));
761 evict_mem.mm_node = NULL;
762 evict_mem.bus.io_reserved_vm = false;
763 evict_mem.bus.io_reserved_count = 0;
767 placement.num_placement = 0;
768 placement.num_busy_placement = 0;
769 bdev->driver->evict_flags(bo, &placement);
770 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
771 no_wait_reserve, no_wait_gpu);
773 if (ret != -ERESTARTSYS) {
774 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
776 ttm_bo_mem_space_debug(bo, &placement);
781 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
782 no_wait_reserve, no_wait_gpu);
784 if (ret != -ERESTARTSYS)
785 pr_err("Buffer eviction failed\n");
786 ttm_bo_mem_put(bo, &evict_mem);
794 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
796 bool interruptible, bool no_wait_reserve,
799 struct ttm_bo_global *glob = bdev->glob;
800 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
801 struct ttm_buffer_object *bo;
802 int ret, put_count = 0;
805 spin_lock(&glob->lru_lock);
806 if (list_empty(&man->lru)) {
807 spin_unlock(&glob->lru_lock);
811 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
812 kref_get(&bo->list_kref);
814 if (!list_empty(&bo->ddestroy)) {
815 spin_unlock(&glob->lru_lock);
816 ret = ttm_bo_cleanup_refs(bo, interruptible,
817 no_wait_reserve, no_wait_gpu);
818 kref_put(&bo->list_kref, ttm_bo_release_list);
823 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
825 if (unlikely(ret == -EBUSY)) {
826 spin_unlock(&glob->lru_lock);
827 if (likely(!no_wait_reserve))
828 ret = ttm_bo_wait_unreserved(bo, interruptible);
830 kref_put(&bo->list_kref, ttm_bo_release_list);
833 * We *need* to retry after releasing the lru lock.
836 if (unlikely(ret != 0))
841 put_count = ttm_bo_del_from_lru(bo);
842 spin_unlock(&glob->lru_lock);
846 ttm_bo_list_ref_sub(bo, put_count, true);
848 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
849 ttm_bo_unreserve(bo);
851 kref_put(&bo->list_kref, ttm_bo_release_list);
855 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
857 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
860 (*man->func->put_node)(man, mem);
862 EXPORT_SYMBOL(ttm_bo_mem_put);
865 * Repeatedly evict memory from the LRU for @mem_type until we create enough
866 * space, or we've evicted everything and there isn't enough space.
868 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
870 struct ttm_placement *placement,
871 struct ttm_mem_reg *mem,
873 bool no_wait_reserve,
876 struct ttm_bo_device *bdev = bo->bdev;
877 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
881 ret = (*man->func->get_node)(man, bo, placement, mem);
882 if (unlikely(ret != 0))
886 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
887 no_wait_reserve, no_wait_gpu);
888 if (unlikely(ret != 0))
891 if (mem->mm_node == NULL)
893 mem->mem_type = mem_type;
897 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
898 uint32_t cur_placement,
899 uint32_t proposed_placement)
901 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
902 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
905 * Keep current caching if possible.
908 if ((cur_placement & caching) != 0)
909 result |= (cur_placement & caching);
910 else if ((man->default_caching & caching) != 0)
911 result |= man->default_caching;
912 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
913 result |= TTM_PL_FLAG_CACHED;
914 else if ((TTM_PL_FLAG_WC & caching) != 0)
915 result |= TTM_PL_FLAG_WC;
916 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
917 result |= TTM_PL_FLAG_UNCACHED;
922 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
924 uint32_t proposed_placement,
925 uint32_t *masked_placement)
927 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
929 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
932 if ((proposed_placement & man->available_caching) == 0)
935 cur_flags |= (proposed_placement & man->available_caching);
937 *masked_placement = cur_flags;
942 * Creates space for memory region @mem according to its type.
944 * This function first searches for free space in compatible memory types in
945 * the priority order defined by the driver. If free space isn't found, then
946 * ttm_bo_mem_force_space is attempted in priority order to evict and find
949 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
950 struct ttm_placement *placement,
951 struct ttm_mem_reg *mem,
952 bool interruptible, bool no_wait_reserve,
955 struct ttm_bo_device *bdev = bo->bdev;
956 struct ttm_mem_type_manager *man;
957 uint32_t mem_type = TTM_PL_SYSTEM;
958 uint32_t cur_flags = 0;
959 bool type_found = false;
960 bool type_ok = false;
961 bool has_erestartsys = false;
965 for (i = 0; i < placement->num_placement; ++i) {
966 ret = ttm_mem_type_from_flags(placement->placement[i],
970 man = &bdev->man[mem_type];
972 type_ok = ttm_bo_mt_compatible(man,
974 placement->placement[i],
980 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
983 * Use the access and other non-mapping-related flag bits from
984 * the memory placement flags to the current flags
986 ttm_flag_masked(&cur_flags, placement->placement[i],
987 ~TTM_PL_MASK_MEMTYPE);
989 if (mem_type == TTM_PL_SYSTEM)
992 if (man->has_type && man->use_type) {
994 ret = (*man->func->get_node)(man, bo, placement, mem);
1002 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1003 mem->mem_type = mem_type;
1004 mem->placement = cur_flags;
1011 for (i = 0; i < placement->num_busy_placement; ++i) {
1012 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1016 man = &bdev->man[mem_type];
1019 if (!ttm_bo_mt_compatible(man,
1021 placement->busy_placement[i],
1025 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1028 * Use the access and other non-mapping-related flag bits from
1029 * the memory placement flags to the current flags
1031 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1032 ~TTM_PL_MASK_MEMTYPE);
1035 if (mem_type == TTM_PL_SYSTEM) {
1036 mem->mem_type = mem_type;
1037 mem->placement = cur_flags;
1038 mem->mm_node = NULL;
1042 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1043 interruptible, no_wait_reserve, no_wait_gpu);
1044 if (ret == 0 && mem->mm_node) {
1045 mem->placement = cur_flags;
1048 if (ret == -ERESTARTSYS)
1049 has_erestartsys = true;
1051 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1054 EXPORT_SYMBOL(ttm_bo_mem_space);
1056 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1057 struct ttm_placement *placement,
1058 bool interruptible, bool no_wait_reserve,
1062 struct ttm_mem_reg mem;
1063 struct ttm_bo_device *bdev = bo->bdev;
1065 BUG_ON(!ttm_bo_is_reserved(bo));
1068 * FIXME: It's possible to pipeline buffer moves.
1069 * Have the driver move function wait for idle when necessary,
1070 * instead of doing it here.
1072 spin_lock(&bdev->fence_lock);
1073 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1074 spin_unlock(&bdev->fence_lock);
1077 mem.num_pages = bo->num_pages;
1078 mem.size = mem.num_pages << PAGE_SHIFT;
1079 mem.page_alignment = bo->mem.page_alignment;
1080 mem.bus.io_reserved_vm = false;
1081 mem.bus.io_reserved_count = 0;
1083 * Determine where to move the buffer.
1085 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
1088 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
1090 if (ret && mem.mm_node)
1091 ttm_bo_mem_put(bo, &mem);
1095 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1096 struct ttm_mem_reg *mem)
1100 if (mem->mm_node && placement->lpfn != 0 &&
1101 (mem->start < placement->fpfn ||
1102 mem->start + mem->num_pages > placement->lpfn))
1105 for (i = 0; i < placement->num_placement; i++) {
1106 if ((placement->placement[i] & mem->placement &
1107 TTM_PL_MASK_CACHING) &&
1108 (placement->placement[i] & mem->placement &
1115 int ttm_bo_validate(struct ttm_buffer_object *bo,
1116 struct ttm_placement *placement,
1117 bool interruptible, bool no_wait_reserve,
1122 BUG_ON(!ttm_bo_is_reserved(bo));
1123 /* Check that range is valid */
1124 if (placement->lpfn || placement->fpfn)
1125 if (placement->fpfn > placement->lpfn ||
1126 (placement->lpfn - placement->fpfn) < bo->num_pages)
1129 * Check whether we need to move buffer.
1131 ret = ttm_bo_mem_compat(placement, &bo->mem);
1133 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
1138 * Use the access and other non-mapping-related flag bits from
1139 * the compatible memory placement flags to the active flags
1141 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1142 ~TTM_PL_MASK_MEMTYPE);
1145 * We might need to add a TTM.
1147 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1148 ret = ttm_bo_add_ttm(bo, true);
1154 EXPORT_SYMBOL(ttm_bo_validate);
1156 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1157 struct ttm_placement *placement)
1159 BUG_ON((placement->fpfn || placement->lpfn) &&
1160 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1165 int ttm_bo_init(struct ttm_bo_device *bdev,
1166 struct ttm_buffer_object *bo,
1168 enum ttm_bo_type type,
1169 struct ttm_placement *placement,
1170 uint32_t page_alignment,
1172 struct file *persistent_swap_storage,
1174 struct sg_table *sg,
1175 void (*destroy) (struct ttm_buffer_object *))
1178 unsigned long num_pages;
1179 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1181 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1183 pr_err("Out of kernel memory\n");
1191 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1192 if (num_pages == 0) {
1193 pr_err("Illegal buffer object size\n");
1198 ttm_mem_global_free(mem_glob, acc_size);
1201 bo->destroy = destroy;
1203 kref_init(&bo->kref);
1204 kref_init(&bo->list_kref);
1205 atomic_set(&bo->cpu_writers, 0);
1206 atomic_set(&bo->reserved, 1);
1207 init_waitqueue_head(&bo->event_queue);
1208 INIT_LIST_HEAD(&bo->lru);
1209 INIT_LIST_HEAD(&bo->ddestroy);
1210 INIT_LIST_HEAD(&bo->swap);
1211 INIT_LIST_HEAD(&bo->io_reserve_lru);
1213 bo->glob = bdev->glob;
1215 bo->num_pages = num_pages;
1216 bo->mem.size = num_pages << PAGE_SHIFT;
1217 bo->mem.mem_type = TTM_PL_SYSTEM;
1218 bo->mem.num_pages = bo->num_pages;
1219 bo->mem.mm_node = NULL;
1220 bo->mem.page_alignment = page_alignment;
1221 bo->mem.bus.io_reserved_vm = false;
1222 bo->mem.bus.io_reserved_count = 0;
1224 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1225 bo->seq_valid = false;
1226 bo->persistent_swap_storage = persistent_swap_storage;
1227 bo->acc_size = acc_size;
1229 atomic_inc(&bo->glob->bo_count);
1231 ret = ttm_bo_check_placement(bo, placement);
1232 if (unlikely(ret != 0))
1236 * For ttm_bo_type_device buffers, allocate
1237 * address space from the device.
1239 if (bo->type == ttm_bo_type_device ||
1240 bo->type == ttm_bo_type_sg) {
1241 ret = ttm_bo_setup_vm(bo);
1246 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
1250 ttm_bo_unreserve(bo);
1254 ttm_bo_unreserve(bo);
1259 EXPORT_SYMBOL(ttm_bo_init);
1261 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1262 unsigned long bo_size,
1263 unsigned struct_size)
1265 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1268 size += ttm_round_pot(struct_size);
1269 size += PAGE_ALIGN(npages * sizeof(void *));
1270 size += ttm_round_pot(sizeof(struct ttm_tt));
1273 EXPORT_SYMBOL(ttm_bo_acc_size);
1275 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1276 unsigned long bo_size,
1277 unsigned struct_size)
1279 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1282 size += ttm_round_pot(struct_size);
1283 size += PAGE_ALIGN(npages * sizeof(void *));
1284 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1285 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1288 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1290 int ttm_bo_create(struct ttm_bo_device *bdev,
1292 enum ttm_bo_type type,
1293 struct ttm_placement *placement,
1294 uint32_t page_alignment,
1296 struct file *persistent_swap_storage,
1297 struct ttm_buffer_object **p_bo)
1299 struct ttm_buffer_object *bo;
1303 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1304 if (unlikely(bo == NULL))
1307 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1308 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1309 interruptible, persistent_swap_storage, acc_size,
1311 if (likely(ret == 0))
1316 EXPORT_SYMBOL(ttm_bo_create);
1318 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1319 unsigned mem_type, bool allow_errors)
1321 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1322 struct ttm_bo_global *glob = bdev->glob;
1326 * Can't use standard list traversal since we're unlocking.
1329 spin_lock(&glob->lru_lock);
1330 while (!list_empty(&man->lru)) {
1331 spin_unlock(&glob->lru_lock);
1332 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
1337 pr_err("Cleanup eviction failed\n");
1340 spin_lock(&glob->lru_lock);
1342 spin_unlock(&glob->lru_lock);
1346 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1348 struct ttm_mem_type_manager *man;
1351 if (mem_type >= TTM_NUM_MEM_TYPES) {
1352 pr_err("Illegal memory type %d\n", mem_type);
1355 man = &bdev->man[mem_type];
1357 if (!man->has_type) {
1358 pr_err("Trying to take down uninitialized memory manager type %u\n",
1363 man->use_type = false;
1364 man->has_type = false;
1368 ttm_bo_force_list_clean(bdev, mem_type, false);
1370 ret = (*man->func->takedown)(man);
1375 EXPORT_SYMBOL(ttm_bo_clean_mm);
1377 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1379 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1381 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1382 pr_err("Illegal memory manager memory type %u\n", mem_type);
1386 if (!man->has_type) {
1387 pr_err("Memory type %u has not been initialized\n", mem_type);
1391 return ttm_bo_force_list_clean(bdev, mem_type, true);
1393 EXPORT_SYMBOL(ttm_bo_evict_mm);
1395 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1396 unsigned long p_size)
1399 struct ttm_mem_type_manager *man;
1401 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1402 man = &bdev->man[type];
1403 BUG_ON(man->has_type);
1404 man->io_reserve_fastpath = true;
1405 man->use_io_reserve_lru = false;
1406 mutex_init(&man->io_reserve_mutex);
1407 INIT_LIST_HEAD(&man->io_reserve_lru);
1409 ret = bdev->driver->init_mem_type(bdev, type, man);
1415 if (type != TTM_PL_SYSTEM) {
1416 ret = (*man->func->init)(man, p_size);
1420 man->has_type = true;
1421 man->use_type = true;
1424 INIT_LIST_HEAD(&man->lru);
1428 EXPORT_SYMBOL(ttm_bo_init_mm);
1430 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1432 struct ttm_bo_global *glob =
1433 container_of(kobj, struct ttm_bo_global, kobj);
1435 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1436 __free_page(glob->dummy_read_page);
1440 void ttm_bo_global_release(struct drm_global_reference *ref)
1442 struct ttm_bo_global *glob = ref->object;
1444 kobject_del(&glob->kobj);
1445 kobject_put(&glob->kobj);
1447 EXPORT_SYMBOL(ttm_bo_global_release);
1449 int ttm_bo_global_init(struct drm_global_reference *ref)
1451 struct ttm_bo_global_ref *bo_ref =
1452 container_of(ref, struct ttm_bo_global_ref, ref);
1453 struct ttm_bo_global *glob = ref->object;
1456 mutex_init(&glob->device_list_mutex);
1457 spin_lock_init(&glob->lru_lock);
1458 glob->mem_glob = bo_ref->mem_glob;
1459 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1461 if (unlikely(glob->dummy_read_page == NULL)) {
1466 INIT_LIST_HEAD(&glob->swap_lru);
1467 INIT_LIST_HEAD(&glob->device_list);
1469 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1470 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1471 if (unlikely(ret != 0)) {
1472 pr_err("Could not register buffer object swapout\n");
1476 atomic_set(&glob->bo_count, 0);
1478 ret = kobject_init_and_add(
1479 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1480 if (unlikely(ret != 0))
1481 kobject_put(&glob->kobj);
1484 __free_page(glob->dummy_read_page);
1489 EXPORT_SYMBOL(ttm_bo_global_init);
1492 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1495 unsigned i = TTM_NUM_MEM_TYPES;
1496 struct ttm_mem_type_manager *man;
1497 struct ttm_bo_global *glob = bdev->glob;
1500 man = &bdev->man[i];
1501 if (man->has_type) {
1502 man->use_type = false;
1503 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1505 pr_err("DRM memory manager type %d is not clean\n",
1508 man->has_type = false;
1512 mutex_lock(&glob->device_list_mutex);
1513 list_del(&bdev->device_list);
1514 mutex_unlock(&glob->device_list_mutex);
1516 cancel_delayed_work_sync(&bdev->wq);
1518 while (ttm_bo_delayed_delete(bdev, true))
1521 spin_lock(&glob->lru_lock);
1522 if (list_empty(&bdev->ddestroy))
1523 TTM_DEBUG("Delayed destroy list was clean\n");
1525 if (list_empty(&bdev->man[0].lru))
1526 TTM_DEBUG("Swap list was clean\n");
1527 spin_unlock(&glob->lru_lock);
1529 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1530 write_lock(&bdev->vm_lock);
1531 drm_mm_takedown(&bdev->addr_space_mm);
1532 write_unlock(&bdev->vm_lock);
1536 EXPORT_SYMBOL(ttm_bo_device_release);
1538 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1539 struct ttm_bo_global *glob,
1540 struct ttm_bo_driver *driver,
1541 uint64_t file_page_offset,
1546 rwlock_init(&bdev->vm_lock);
1547 bdev->driver = driver;
1549 memset(bdev->man, 0, sizeof(bdev->man));
1552 * Initialize the system memory buffer type.
1553 * Other types need to be driver / IOCTL initialized.
1555 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1556 if (unlikely(ret != 0))
1559 bdev->addr_space_rb = RB_ROOT;
1560 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1561 if (unlikely(ret != 0))
1562 goto out_no_addr_mm;
1564 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1565 INIT_LIST_HEAD(&bdev->ddestroy);
1566 bdev->dev_mapping = NULL;
1568 bdev->need_dma32 = need_dma32;
1570 spin_lock_init(&bdev->fence_lock);
1571 mutex_lock(&glob->device_list_mutex);
1572 list_add_tail(&bdev->device_list, &glob->device_list);
1573 mutex_unlock(&glob->device_list_mutex);
1577 ttm_bo_clean_mm(bdev, 0);
1581 EXPORT_SYMBOL(ttm_bo_device_init);
1584 * buffer object vm functions.
1587 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1589 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1591 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1592 if (mem->mem_type == TTM_PL_SYSTEM)
1595 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1598 if (mem->placement & TTM_PL_FLAG_CACHED)
1604 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1606 struct ttm_bo_device *bdev = bo->bdev;
1607 loff_t offset = (loff_t) bo->addr_space_offset;
1608 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1610 if (!bdev->dev_mapping)
1612 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1613 ttm_mem_io_free_vm(bo);
1616 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1618 struct ttm_bo_device *bdev = bo->bdev;
1619 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1621 ttm_mem_io_lock(man, false);
1622 ttm_bo_unmap_virtual_locked(bo);
1623 ttm_mem_io_unlock(man);
1627 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1629 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1631 struct ttm_bo_device *bdev = bo->bdev;
1632 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1633 struct rb_node *parent = NULL;
1634 struct ttm_buffer_object *cur_bo;
1635 unsigned long offset = bo->vm_node->start;
1636 unsigned long cur_offset;
1640 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1641 cur_offset = cur_bo->vm_node->start;
1642 if (offset < cur_offset)
1643 cur = &parent->rb_left;
1644 else if (offset > cur_offset)
1645 cur = &parent->rb_right;
1650 rb_link_node(&bo->vm_rb, parent, cur);
1651 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1657 * @bo: the buffer to allocate address space for
1659 * Allocate address space in the drm device so that applications
1660 * can mmap the buffer and access the contents. This only
1661 * applies to ttm_bo_type_device objects as others are not
1662 * placed in the drm device address space.
1665 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1667 struct ttm_bo_device *bdev = bo->bdev;
1671 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1672 if (unlikely(ret != 0))
1675 write_lock(&bdev->vm_lock);
1676 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1677 bo->mem.num_pages, 0, 0);
1679 if (unlikely(bo->vm_node == NULL)) {
1684 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1685 bo->mem.num_pages, 0);
1687 if (unlikely(bo->vm_node == NULL)) {
1688 write_unlock(&bdev->vm_lock);
1692 ttm_bo_vm_insert_rb(bo);
1693 write_unlock(&bdev->vm_lock);
1694 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1698 write_unlock(&bdev->vm_lock);
1702 int ttm_bo_wait(struct ttm_buffer_object *bo,
1703 bool lazy, bool interruptible, bool no_wait)
1705 struct ttm_bo_driver *driver = bo->bdev->driver;
1706 struct ttm_bo_device *bdev = bo->bdev;
1710 if (likely(bo->sync_obj == NULL))
1713 while (bo->sync_obj) {
1715 if (driver->sync_obj_signaled(bo->sync_obj)) {
1716 void *tmp_obj = bo->sync_obj;
1717 bo->sync_obj = NULL;
1718 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1719 spin_unlock(&bdev->fence_lock);
1720 driver->sync_obj_unref(&tmp_obj);
1721 spin_lock(&bdev->fence_lock);
1728 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1729 spin_unlock(&bdev->fence_lock);
1730 ret = driver->sync_obj_wait(sync_obj,
1731 lazy, interruptible);
1732 if (unlikely(ret != 0)) {
1733 driver->sync_obj_unref(&sync_obj);
1734 spin_lock(&bdev->fence_lock);
1737 spin_lock(&bdev->fence_lock);
1738 if (likely(bo->sync_obj == sync_obj)) {
1739 void *tmp_obj = bo->sync_obj;
1740 bo->sync_obj = NULL;
1741 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1743 spin_unlock(&bdev->fence_lock);
1744 driver->sync_obj_unref(&sync_obj);
1745 driver->sync_obj_unref(&tmp_obj);
1746 spin_lock(&bdev->fence_lock);
1748 spin_unlock(&bdev->fence_lock);
1749 driver->sync_obj_unref(&sync_obj);
1750 spin_lock(&bdev->fence_lock);
1755 EXPORT_SYMBOL(ttm_bo_wait);
1757 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1759 struct ttm_bo_device *bdev = bo->bdev;
1763 * Using ttm_bo_reserve makes sure the lru lists are updated.
1766 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1767 if (unlikely(ret != 0))
1769 spin_lock(&bdev->fence_lock);
1770 ret = ttm_bo_wait(bo, false, true, no_wait);
1771 spin_unlock(&bdev->fence_lock);
1772 if (likely(ret == 0))
1773 atomic_inc(&bo->cpu_writers);
1774 ttm_bo_unreserve(bo);
1777 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1779 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1781 atomic_dec(&bo->cpu_writers);
1783 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1786 * A buffer object shrink method that tries to swap out the first
1787 * buffer object on the bo_global::swap_lru list.
1790 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1792 struct ttm_bo_global *glob =
1793 container_of(shrink, struct ttm_bo_global, shrink);
1794 struct ttm_buffer_object *bo;
1797 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1799 spin_lock(&glob->lru_lock);
1800 while (ret == -EBUSY) {
1801 if (unlikely(list_empty(&glob->swap_lru))) {
1802 spin_unlock(&glob->lru_lock);
1806 bo = list_first_entry(&glob->swap_lru,
1807 struct ttm_buffer_object, swap);
1808 kref_get(&bo->list_kref);
1810 if (!list_empty(&bo->ddestroy)) {
1811 spin_unlock(&glob->lru_lock);
1812 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1813 kref_put(&bo->list_kref, ttm_bo_release_list);
1814 spin_lock(&glob->lru_lock);
1819 * Reserve buffer. Since we unlock while sleeping, we need
1820 * to re-check that nobody removed us from the swap-list while
1824 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1825 if (unlikely(ret == -EBUSY)) {
1826 spin_unlock(&glob->lru_lock);
1827 ttm_bo_wait_unreserved(bo, false);
1828 kref_put(&bo->list_kref, ttm_bo_release_list);
1829 spin_lock(&glob->lru_lock);
1834 put_count = ttm_bo_del_from_lru(bo);
1835 spin_unlock(&glob->lru_lock);
1837 ttm_bo_list_ref_sub(bo, put_count, true);
1840 * Wait for GPU, then move to system cached.
1843 spin_lock(&bo->bdev->fence_lock);
1844 ret = ttm_bo_wait(bo, false, false, false);
1845 spin_unlock(&bo->bdev->fence_lock);
1847 if (unlikely(ret != 0))
1850 if ((bo->mem.placement & swap_placement) != swap_placement) {
1851 struct ttm_mem_reg evict_mem;
1853 evict_mem = bo->mem;
1854 evict_mem.mm_node = NULL;
1855 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1856 evict_mem.mem_type = TTM_PL_SYSTEM;
1858 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1859 false, false, false);
1860 if (unlikely(ret != 0))
1864 ttm_bo_unmap_virtual(bo);
1867 * Swap out. Buffer will be swapped in again as soon as
1868 * anyone tries to access a ttm page.
1871 if (bo->bdev->driver->swap_notify)
1872 bo->bdev->driver->swap_notify(bo);
1874 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1879 * Unreserve without putting on LRU to avoid swapping out an
1880 * already swapped buffer.
1883 atomic_set(&bo->reserved, 0);
1884 wake_up_all(&bo->event_queue);
1885 kref_put(&bo->list_kref, ttm_bo_release_list);
1889 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1891 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1894 EXPORT_SYMBOL(ttm_bo_swapout_all);