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_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1058 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1061 return wait_event_interruptible(bo->event_queue,
1062 atomic_read(&bo->cpu_writers) == 0);
1064 EXPORT_SYMBOL(ttm_bo_wait_cpu);
1066 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1067 struct ttm_placement *placement,
1068 bool interruptible, bool no_wait_reserve,
1072 struct ttm_mem_reg mem;
1073 struct ttm_bo_device *bdev = bo->bdev;
1075 BUG_ON(!ttm_bo_is_reserved(bo));
1078 * FIXME: It's possible to pipeline buffer moves.
1079 * Have the driver move function wait for idle when necessary,
1080 * instead of doing it here.
1082 spin_lock(&bdev->fence_lock);
1083 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1084 spin_unlock(&bdev->fence_lock);
1087 mem.num_pages = bo->num_pages;
1088 mem.size = mem.num_pages << PAGE_SHIFT;
1089 mem.page_alignment = bo->mem.page_alignment;
1090 mem.bus.io_reserved_vm = false;
1091 mem.bus.io_reserved_count = 0;
1093 * Determine where to move the buffer.
1095 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
1098 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
1100 if (ret && mem.mm_node)
1101 ttm_bo_mem_put(bo, &mem);
1105 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1106 struct ttm_mem_reg *mem)
1110 if (mem->mm_node && placement->lpfn != 0 &&
1111 (mem->start < placement->fpfn ||
1112 mem->start + mem->num_pages > placement->lpfn))
1115 for (i = 0; i < placement->num_placement; i++) {
1116 if ((placement->placement[i] & mem->placement &
1117 TTM_PL_MASK_CACHING) &&
1118 (placement->placement[i] & mem->placement &
1125 int ttm_bo_validate(struct ttm_buffer_object *bo,
1126 struct ttm_placement *placement,
1127 bool interruptible, bool no_wait_reserve,
1132 BUG_ON(!ttm_bo_is_reserved(bo));
1133 /* Check that range is valid */
1134 if (placement->lpfn || placement->fpfn)
1135 if (placement->fpfn > placement->lpfn ||
1136 (placement->lpfn - placement->fpfn) < bo->num_pages)
1139 * Check whether we need to move buffer.
1141 ret = ttm_bo_mem_compat(placement, &bo->mem);
1143 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
1148 * Use the access and other non-mapping-related flag bits from
1149 * the compatible memory placement flags to the active flags
1151 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1152 ~TTM_PL_MASK_MEMTYPE);
1155 * We might need to add a TTM.
1157 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1158 ret = ttm_bo_add_ttm(bo, true);
1164 EXPORT_SYMBOL(ttm_bo_validate);
1166 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1167 struct ttm_placement *placement)
1169 BUG_ON((placement->fpfn || placement->lpfn) &&
1170 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1175 int ttm_bo_init(struct ttm_bo_device *bdev,
1176 struct ttm_buffer_object *bo,
1178 enum ttm_bo_type type,
1179 struct ttm_placement *placement,
1180 uint32_t page_alignment,
1182 struct file *persistent_swap_storage,
1184 struct sg_table *sg,
1185 void (*destroy) (struct ttm_buffer_object *))
1188 unsigned long num_pages;
1189 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1191 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1193 pr_err("Out of kernel memory\n");
1201 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1202 if (num_pages == 0) {
1203 pr_err("Illegal buffer object size\n");
1208 ttm_mem_global_free(mem_glob, acc_size);
1211 bo->destroy = destroy;
1213 kref_init(&bo->kref);
1214 kref_init(&bo->list_kref);
1215 atomic_set(&bo->cpu_writers, 0);
1216 atomic_set(&bo->reserved, 1);
1217 init_waitqueue_head(&bo->event_queue);
1218 INIT_LIST_HEAD(&bo->lru);
1219 INIT_LIST_HEAD(&bo->ddestroy);
1220 INIT_LIST_HEAD(&bo->swap);
1221 INIT_LIST_HEAD(&bo->io_reserve_lru);
1223 bo->glob = bdev->glob;
1225 bo->num_pages = num_pages;
1226 bo->mem.size = num_pages << PAGE_SHIFT;
1227 bo->mem.mem_type = TTM_PL_SYSTEM;
1228 bo->mem.num_pages = bo->num_pages;
1229 bo->mem.mm_node = NULL;
1230 bo->mem.page_alignment = page_alignment;
1231 bo->mem.bus.io_reserved_vm = false;
1232 bo->mem.bus.io_reserved_count = 0;
1234 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1235 bo->seq_valid = false;
1236 bo->persistent_swap_storage = persistent_swap_storage;
1237 bo->acc_size = acc_size;
1239 atomic_inc(&bo->glob->bo_count);
1241 ret = ttm_bo_check_placement(bo, placement);
1242 if (unlikely(ret != 0))
1246 * For ttm_bo_type_device buffers, allocate
1247 * address space from the device.
1249 if (bo->type == ttm_bo_type_device ||
1250 bo->type == ttm_bo_type_sg) {
1251 ret = ttm_bo_setup_vm(bo);
1256 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
1260 ttm_bo_unreserve(bo);
1264 ttm_bo_unreserve(bo);
1269 EXPORT_SYMBOL(ttm_bo_init);
1271 size_t ttm_bo_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 += ttm_round_pot(sizeof(struct ttm_tt));
1283 EXPORT_SYMBOL(ttm_bo_acc_size);
1285 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1286 unsigned long bo_size,
1287 unsigned struct_size)
1289 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1292 size += ttm_round_pot(struct_size);
1293 size += PAGE_ALIGN(npages * sizeof(void *));
1294 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1295 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1298 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1300 int ttm_bo_create(struct ttm_bo_device *bdev,
1302 enum ttm_bo_type type,
1303 struct ttm_placement *placement,
1304 uint32_t page_alignment,
1306 struct file *persistent_swap_storage,
1307 struct ttm_buffer_object **p_bo)
1309 struct ttm_buffer_object *bo;
1313 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1314 if (unlikely(bo == NULL))
1317 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1318 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1319 interruptible, persistent_swap_storage, acc_size,
1321 if (likely(ret == 0))
1326 EXPORT_SYMBOL(ttm_bo_create);
1328 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1329 unsigned mem_type, bool allow_errors)
1331 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1332 struct ttm_bo_global *glob = bdev->glob;
1336 * Can't use standard list traversal since we're unlocking.
1339 spin_lock(&glob->lru_lock);
1340 while (!list_empty(&man->lru)) {
1341 spin_unlock(&glob->lru_lock);
1342 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
1347 pr_err("Cleanup eviction failed\n");
1350 spin_lock(&glob->lru_lock);
1352 spin_unlock(&glob->lru_lock);
1356 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1358 struct ttm_mem_type_manager *man;
1361 if (mem_type >= TTM_NUM_MEM_TYPES) {
1362 pr_err("Illegal memory type %d\n", mem_type);
1365 man = &bdev->man[mem_type];
1367 if (!man->has_type) {
1368 pr_err("Trying to take down uninitialized memory manager type %u\n",
1373 man->use_type = false;
1374 man->has_type = false;
1378 ttm_bo_force_list_clean(bdev, mem_type, false);
1380 ret = (*man->func->takedown)(man);
1385 EXPORT_SYMBOL(ttm_bo_clean_mm);
1387 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1389 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1391 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1392 pr_err("Illegal memory manager memory type %u\n", mem_type);
1396 if (!man->has_type) {
1397 pr_err("Memory type %u has not been initialized\n", mem_type);
1401 return ttm_bo_force_list_clean(bdev, mem_type, true);
1403 EXPORT_SYMBOL(ttm_bo_evict_mm);
1405 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1406 unsigned long p_size)
1409 struct ttm_mem_type_manager *man;
1411 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1412 man = &bdev->man[type];
1413 BUG_ON(man->has_type);
1414 man->io_reserve_fastpath = true;
1415 man->use_io_reserve_lru = false;
1416 mutex_init(&man->io_reserve_mutex);
1417 INIT_LIST_HEAD(&man->io_reserve_lru);
1419 ret = bdev->driver->init_mem_type(bdev, type, man);
1425 if (type != TTM_PL_SYSTEM) {
1426 ret = (*man->func->init)(man, p_size);
1430 man->has_type = true;
1431 man->use_type = true;
1434 INIT_LIST_HEAD(&man->lru);
1438 EXPORT_SYMBOL(ttm_bo_init_mm);
1440 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1442 struct ttm_bo_global *glob =
1443 container_of(kobj, struct ttm_bo_global, kobj);
1445 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1446 __free_page(glob->dummy_read_page);
1450 void ttm_bo_global_release(struct drm_global_reference *ref)
1452 struct ttm_bo_global *glob = ref->object;
1454 kobject_del(&glob->kobj);
1455 kobject_put(&glob->kobj);
1457 EXPORT_SYMBOL(ttm_bo_global_release);
1459 int ttm_bo_global_init(struct drm_global_reference *ref)
1461 struct ttm_bo_global_ref *bo_ref =
1462 container_of(ref, struct ttm_bo_global_ref, ref);
1463 struct ttm_bo_global *glob = ref->object;
1466 mutex_init(&glob->device_list_mutex);
1467 spin_lock_init(&glob->lru_lock);
1468 glob->mem_glob = bo_ref->mem_glob;
1469 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1471 if (unlikely(glob->dummy_read_page == NULL)) {
1476 INIT_LIST_HEAD(&glob->swap_lru);
1477 INIT_LIST_HEAD(&glob->device_list);
1479 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1480 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1481 if (unlikely(ret != 0)) {
1482 pr_err("Could not register buffer object swapout\n");
1486 atomic_set(&glob->bo_count, 0);
1488 ret = kobject_init_and_add(
1489 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1490 if (unlikely(ret != 0))
1491 kobject_put(&glob->kobj);
1494 __free_page(glob->dummy_read_page);
1499 EXPORT_SYMBOL(ttm_bo_global_init);
1502 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1505 unsigned i = TTM_NUM_MEM_TYPES;
1506 struct ttm_mem_type_manager *man;
1507 struct ttm_bo_global *glob = bdev->glob;
1510 man = &bdev->man[i];
1511 if (man->has_type) {
1512 man->use_type = false;
1513 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1515 pr_err("DRM memory manager type %d is not clean\n",
1518 man->has_type = false;
1522 mutex_lock(&glob->device_list_mutex);
1523 list_del(&bdev->device_list);
1524 mutex_unlock(&glob->device_list_mutex);
1526 cancel_delayed_work_sync(&bdev->wq);
1528 while (ttm_bo_delayed_delete(bdev, true))
1531 spin_lock(&glob->lru_lock);
1532 if (list_empty(&bdev->ddestroy))
1533 TTM_DEBUG("Delayed destroy list was clean\n");
1535 if (list_empty(&bdev->man[0].lru))
1536 TTM_DEBUG("Swap list was clean\n");
1537 spin_unlock(&glob->lru_lock);
1539 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1540 write_lock(&bdev->vm_lock);
1541 drm_mm_takedown(&bdev->addr_space_mm);
1542 write_unlock(&bdev->vm_lock);
1546 EXPORT_SYMBOL(ttm_bo_device_release);
1548 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1549 struct ttm_bo_global *glob,
1550 struct ttm_bo_driver *driver,
1551 uint64_t file_page_offset,
1556 rwlock_init(&bdev->vm_lock);
1557 bdev->driver = driver;
1559 memset(bdev->man, 0, sizeof(bdev->man));
1562 * Initialize the system memory buffer type.
1563 * Other types need to be driver / IOCTL initialized.
1565 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1566 if (unlikely(ret != 0))
1569 bdev->addr_space_rb = RB_ROOT;
1570 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1571 if (unlikely(ret != 0))
1572 goto out_no_addr_mm;
1574 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1575 INIT_LIST_HEAD(&bdev->ddestroy);
1576 bdev->dev_mapping = NULL;
1578 bdev->need_dma32 = need_dma32;
1580 spin_lock_init(&bdev->fence_lock);
1581 mutex_lock(&glob->device_list_mutex);
1582 list_add_tail(&bdev->device_list, &glob->device_list);
1583 mutex_unlock(&glob->device_list_mutex);
1587 ttm_bo_clean_mm(bdev, 0);
1591 EXPORT_SYMBOL(ttm_bo_device_init);
1594 * buffer object vm functions.
1597 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1599 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1601 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1602 if (mem->mem_type == TTM_PL_SYSTEM)
1605 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1608 if (mem->placement & TTM_PL_FLAG_CACHED)
1614 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1616 struct ttm_bo_device *bdev = bo->bdev;
1617 loff_t offset = (loff_t) bo->addr_space_offset;
1618 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1620 if (!bdev->dev_mapping)
1622 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1623 ttm_mem_io_free_vm(bo);
1626 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1628 struct ttm_bo_device *bdev = bo->bdev;
1629 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1631 ttm_mem_io_lock(man, false);
1632 ttm_bo_unmap_virtual_locked(bo);
1633 ttm_mem_io_unlock(man);
1637 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1639 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1641 struct ttm_bo_device *bdev = bo->bdev;
1642 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1643 struct rb_node *parent = NULL;
1644 struct ttm_buffer_object *cur_bo;
1645 unsigned long offset = bo->vm_node->start;
1646 unsigned long cur_offset;
1650 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1651 cur_offset = cur_bo->vm_node->start;
1652 if (offset < cur_offset)
1653 cur = &parent->rb_left;
1654 else if (offset > cur_offset)
1655 cur = &parent->rb_right;
1660 rb_link_node(&bo->vm_rb, parent, cur);
1661 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1667 * @bo: the buffer to allocate address space for
1669 * Allocate address space in the drm device so that applications
1670 * can mmap the buffer and access the contents. This only
1671 * applies to ttm_bo_type_device objects as others are not
1672 * placed in the drm device address space.
1675 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1677 struct ttm_bo_device *bdev = bo->bdev;
1681 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1682 if (unlikely(ret != 0))
1685 write_lock(&bdev->vm_lock);
1686 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1687 bo->mem.num_pages, 0, 0);
1689 if (unlikely(bo->vm_node == NULL)) {
1694 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1695 bo->mem.num_pages, 0);
1697 if (unlikely(bo->vm_node == NULL)) {
1698 write_unlock(&bdev->vm_lock);
1702 ttm_bo_vm_insert_rb(bo);
1703 write_unlock(&bdev->vm_lock);
1704 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1708 write_unlock(&bdev->vm_lock);
1712 int ttm_bo_wait(struct ttm_buffer_object *bo,
1713 bool lazy, bool interruptible, bool no_wait)
1715 struct ttm_bo_driver *driver = bo->bdev->driver;
1716 struct ttm_bo_device *bdev = bo->bdev;
1720 if (likely(bo->sync_obj == NULL))
1723 while (bo->sync_obj) {
1725 if (driver->sync_obj_signaled(bo->sync_obj)) {
1726 void *tmp_obj = bo->sync_obj;
1727 bo->sync_obj = NULL;
1728 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1729 spin_unlock(&bdev->fence_lock);
1730 driver->sync_obj_unref(&tmp_obj);
1731 spin_lock(&bdev->fence_lock);
1738 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1739 spin_unlock(&bdev->fence_lock);
1740 ret = driver->sync_obj_wait(sync_obj,
1741 lazy, interruptible);
1742 if (unlikely(ret != 0)) {
1743 driver->sync_obj_unref(&sync_obj);
1744 spin_lock(&bdev->fence_lock);
1747 spin_lock(&bdev->fence_lock);
1748 if (likely(bo->sync_obj == sync_obj)) {
1749 void *tmp_obj = bo->sync_obj;
1750 bo->sync_obj = NULL;
1751 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1753 spin_unlock(&bdev->fence_lock);
1754 driver->sync_obj_unref(&sync_obj);
1755 driver->sync_obj_unref(&tmp_obj);
1756 spin_lock(&bdev->fence_lock);
1758 spin_unlock(&bdev->fence_lock);
1759 driver->sync_obj_unref(&sync_obj);
1760 spin_lock(&bdev->fence_lock);
1765 EXPORT_SYMBOL(ttm_bo_wait);
1767 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1769 struct ttm_bo_device *bdev = bo->bdev;
1773 * Using ttm_bo_reserve makes sure the lru lists are updated.
1776 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1777 if (unlikely(ret != 0))
1779 spin_lock(&bdev->fence_lock);
1780 ret = ttm_bo_wait(bo, false, true, no_wait);
1781 spin_unlock(&bdev->fence_lock);
1782 if (likely(ret == 0))
1783 atomic_inc(&bo->cpu_writers);
1784 ttm_bo_unreserve(bo);
1787 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1789 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1791 if (atomic_dec_and_test(&bo->cpu_writers))
1792 wake_up_all(&bo->event_queue);
1794 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1797 * A buffer object shrink method that tries to swap out the first
1798 * buffer object on the bo_global::swap_lru list.
1801 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1803 struct ttm_bo_global *glob =
1804 container_of(shrink, struct ttm_bo_global, shrink);
1805 struct ttm_buffer_object *bo;
1808 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1810 spin_lock(&glob->lru_lock);
1811 while (ret == -EBUSY) {
1812 if (unlikely(list_empty(&glob->swap_lru))) {
1813 spin_unlock(&glob->lru_lock);
1817 bo = list_first_entry(&glob->swap_lru,
1818 struct ttm_buffer_object, swap);
1819 kref_get(&bo->list_kref);
1821 if (!list_empty(&bo->ddestroy)) {
1822 spin_unlock(&glob->lru_lock);
1823 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1824 kref_put(&bo->list_kref, ttm_bo_release_list);
1825 spin_lock(&glob->lru_lock);
1830 * Reserve buffer. Since we unlock while sleeping, we need
1831 * to re-check that nobody removed us from the swap-list while
1835 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1836 if (unlikely(ret == -EBUSY)) {
1837 spin_unlock(&glob->lru_lock);
1838 ttm_bo_wait_unreserved(bo, false);
1839 kref_put(&bo->list_kref, ttm_bo_release_list);
1840 spin_lock(&glob->lru_lock);
1845 put_count = ttm_bo_del_from_lru(bo);
1846 spin_unlock(&glob->lru_lock);
1848 ttm_bo_list_ref_sub(bo, put_count, true);
1851 * Wait for GPU, then move to system cached.
1854 spin_lock(&bo->bdev->fence_lock);
1855 ret = ttm_bo_wait(bo, false, false, false);
1856 spin_unlock(&bo->bdev->fence_lock);
1858 if (unlikely(ret != 0))
1861 if ((bo->mem.placement & swap_placement) != swap_placement) {
1862 struct ttm_mem_reg evict_mem;
1864 evict_mem = bo->mem;
1865 evict_mem.mm_node = NULL;
1866 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1867 evict_mem.mem_type = TTM_PL_SYSTEM;
1869 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1870 false, false, false);
1871 if (unlikely(ret != 0))
1875 ttm_bo_unmap_virtual(bo);
1878 * Swap out. Buffer will be swapped in again as soon as
1879 * anyone tries to access a ttm page.
1882 if (bo->bdev->driver->swap_notify)
1883 bo->bdev->driver->swap_notify(bo);
1885 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1890 * Unreserve without putting on LRU to avoid swapping out an
1891 * already swapped buffer.
1894 atomic_set(&bo->reserved, 0);
1895 wake_up_all(&bo->event_queue);
1896 kref_put(&bo->list_kref, ttm_bo_release_list);
1900 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1902 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1905 EXPORT_SYMBOL(ttm_bo_swapout_all);