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);
153 if (bo->resv == &bo->ttm_resv)
154 reservation_object_fini(&bo->ttm_resv);
161 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
164 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
166 struct ttm_bo_device *bdev = bo->bdev;
167 struct ttm_mem_type_manager *man;
169 lockdep_assert_held(&bo->resv->lock.base);
171 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
173 BUG_ON(!list_empty(&bo->lru));
175 man = &bdev->man[bo->mem.mem_type];
176 list_add_tail(&bo->lru, &man->lru);
177 kref_get(&bo->list_kref);
179 if (bo->ttm != NULL) {
180 list_add_tail(&bo->swap, &bo->glob->swap_lru);
181 kref_get(&bo->list_kref);
185 EXPORT_SYMBOL(ttm_bo_add_to_lru);
187 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
191 if (!list_empty(&bo->swap)) {
192 list_del_init(&bo->swap);
195 if (!list_empty(&bo->lru)) {
196 list_del_init(&bo->lru);
201 * TODO: Add a driver hook to delete from
202 * driver-specific LRU's here.
208 static void ttm_bo_ref_bug(struct kref *list_kref)
213 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
216 kref_sub(&bo->list_kref, count,
217 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
220 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
224 spin_lock(&bo->glob->lru_lock);
225 put_count = ttm_bo_del_from_lru(bo);
226 spin_unlock(&bo->glob->lru_lock);
227 ttm_bo_list_ref_sub(bo, put_count, true);
229 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
232 * Call bo->mutex locked.
234 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
236 struct ttm_bo_device *bdev = bo->bdev;
237 struct ttm_bo_global *glob = bo->glob;
239 uint32_t page_flags = 0;
241 TTM_ASSERT_LOCKED(&bo->mutex);
244 if (bdev->need_dma32)
245 page_flags |= TTM_PAGE_FLAG_DMA32;
248 case ttm_bo_type_device:
250 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
251 case ttm_bo_type_kernel:
252 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
253 page_flags, glob->dummy_read_page);
254 if (unlikely(bo->ttm == NULL))
258 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
259 page_flags | TTM_PAGE_FLAG_SG,
260 glob->dummy_read_page);
261 if (unlikely(bo->ttm == NULL)) {
265 bo->ttm->sg = bo->sg;
268 pr_err("Illegal buffer object type\n");
276 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
277 struct ttm_mem_reg *mem,
278 bool evict, bool interruptible,
281 struct ttm_bo_device *bdev = bo->bdev;
282 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
283 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
284 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
285 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
288 if (old_is_pci || new_is_pci ||
289 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
290 ret = ttm_mem_io_lock(old_man, true);
291 if (unlikely(ret != 0))
293 ttm_bo_unmap_virtual_locked(bo);
294 ttm_mem_io_unlock(old_man);
298 * Create and bind a ttm if required.
301 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
302 if (bo->ttm == NULL) {
303 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
304 ret = ttm_bo_add_ttm(bo, zero);
309 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
313 if (mem->mem_type != TTM_PL_SYSTEM) {
314 ret = ttm_tt_bind(bo->ttm, mem);
319 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
320 if (bdev->driver->move_notify)
321 bdev->driver->move_notify(bo, mem);
328 if (bdev->driver->move_notify)
329 bdev->driver->move_notify(bo, mem);
331 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
332 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
333 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
334 else if (bdev->driver->move)
335 ret = bdev->driver->move(bo, evict, interruptible,
338 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
341 if (bdev->driver->move_notify) {
342 struct ttm_mem_reg tmp_mem = *mem;
345 bdev->driver->move_notify(bo, mem);
355 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
357 pr_err("Can not flush read caches\n");
361 if (bo->mem.mm_node) {
362 bo->offset = (bo->mem.start << PAGE_SHIFT) +
363 bdev->man[bo->mem.mem_type].gpu_offset;
364 bo->cur_placement = bo->mem.placement;
371 new_man = &bdev->man[bo->mem.mem_type];
372 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
373 ttm_tt_unbind(bo->ttm);
374 ttm_tt_destroy(bo->ttm);
383 * Will release GPU memory type usage on destruction.
384 * This is the place to put in driver specific hooks to release
385 * driver private resources.
386 * Will release the bo::reserved lock.
389 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
391 if (bo->bdev->driver->move_notify)
392 bo->bdev->driver->move_notify(bo, NULL);
395 ttm_tt_unbind(bo->ttm);
396 ttm_tt_destroy(bo->ttm);
399 ttm_bo_mem_put(bo, &bo->mem);
401 ww_mutex_unlock (&bo->resv->lock);
404 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
406 struct ttm_bo_device *bdev = bo->bdev;
407 struct ttm_bo_global *glob = bo->glob;
408 struct ttm_bo_driver *driver = bdev->driver;
409 void *sync_obj = NULL;
413 spin_lock(&glob->lru_lock);
414 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
416 spin_lock(&bdev->fence_lock);
417 (void) ttm_bo_wait(bo, false, false, true);
418 if (!ret && !bo->sync_obj) {
419 spin_unlock(&bdev->fence_lock);
420 put_count = ttm_bo_del_from_lru(bo);
422 spin_unlock(&glob->lru_lock);
423 ttm_bo_cleanup_memtype_use(bo);
425 ttm_bo_list_ref_sub(bo, put_count, true);
430 sync_obj = driver->sync_obj_ref(bo->sync_obj);
431 spin_unlock(&bdev->fence_lock);
434 ww_mutex_unlock(&bo->resv->lock);
436 kref_get(&bo->list_kref);
437 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
438 spin_unlock(&glob->lru_lock);
441 driver->sync_obj_flush(sync_obj);
442 driver->sync_obj_unref(&sync_obj);
444 schedule_delayed_work(&bdev->wq,
445 ((HZ / 100) < 1) ? 1 : HZ / 100);
449 * function ttm_bo_cleanup_refs_and_unlock
450 * If bo idle, remove from delayed- and lru lists, and unref.
451 * If not idle, do nothing.
453 * Must be called with lru_lock and reservation held, this function
454 * will drop both before returning.
456 * @interruptible Any sleeps should occur interruptibly.
457 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
460 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
464 struct ttm_bo_device *bdev = bo->bdev;
465 struct ttm_bo_driver *driver = bdev->driver;
466 struct ttm_bo_global *glob = bo->glob;
470 spin_lock(&bdev->fence_lock);
471 ret = ttm_bo_wait(bo, false, false, true);
473 if (ret && !no_wait_gpu) {
477 * Take a reference to the fence and unreserve,
478 * at this point the buffer should be dead, so
479 * no new sync objects can be attached.
481 sync_obj = driver->sync_obj_ref(bo->sync_obj);
482 spin_unlock(&bdev->fence_lock);
484 ww_mutex_unlock(&bo->resv->lock);
485 spin_unlock(&glob->lru_lock);
487 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
488 driver->sync_obj_unref(&sync_obj);
493 * remove sync_obj with ttm_bo_wait, the wait should be
494 * finished, and no new wait object should have been added.
496 spin_lock(&bdev->fence_lock);
497 ret = ttm_bo_wait(bo, false, false, true);
499 spin_unlock(&bdev->fence_lock);
503 spin_lock(&glob->lru_lock);
504 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
507 * We raced, and lost, someone else holds the reservation now,
508 * and is probably busy in ttm_bo_cleanup_memtype_use.
510 * Even if it's not the case, because we finished waiting any
511 * delayed destruction would succeed, so just return success
515 spin_unlock(&glob->lru_lock);
519 spin_unlock(&bdev->fence_lock);
521 if (ret || unlikely(list_empty(&bo->ddestroy))) {
522 ww_mutex_unlock(&bo->resv->lock);
523 spin_unlock(&glob->lru_lock);
527 put_count = ttm_bo_del_from_lru(bo);
528 list_del_init(&bo->ddestroy);
531 spin_unlock(&glob->lru_lock);
532 ttm_bo_cleanup_memtype_use(bo);
534 ttm_bo_list_ref_sub(bo, put_count, true);
540 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
541 * encountered buffers.
544 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
546 struct ttm_bo_global *glob = bdev->glob;
547 struct ttm_buffer_object *entry = NULL;
550 spin_lock(&glob->lru_lock);
551 if (list_empty(&bdev->ddestroy))
554 entry = list_first_entry(&bdev->ddestroy,
555 struct ttm_buffer_object, ddestroy);
556 kref_get(&entry->list_kref);
559 struct ttm_buffer_object *nentry = NULL;
561 if (entry->ddestroy.next != &bdev->ddestroy) {
562 nentry = list_first_entry(&entry->ddestroy,
563 struct ttm_buffer_object, ddestroy);
564 kref_get(&nentry->list_kref);
567 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
568 if (remove_all && ret) {
569 spin_unlock(&glob->lru_lock);
570 ret = ttm_bo_reserve_nolru(entry, false, false,
572 spin_lock(&glob->lru_lock);
576 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
579 spin_unlock(&glob->lru_lock);
581 kref_put(&entry->list_kref, ttm_bo_release_list);
587 spin_lock(&glob->lru_lock);
588 if (list_empty(&entry->ddestroy))
593 spin_unlock(&glob->lru_lock);
596 kref_put(&entry->list_kref, ttm_bo_release_list);
600 static void ttm_bo_delayed_workqueue(struct work_struct *work)
602 struct ttm_bo_device *bdev =
603 container_of(work, struct ttm_bo_device, wq.work);
605 if (ttm_bo_delayed_delete(bdev, false)) {
606 schedule_delayed_work(&bdev->wq,
607 ((HZ / 100) < 1) ? 1 : HZ / 100);
611 static void ttm_bo_release(struct kref *kref)
613 struct ttm_buffer_object *bo =
614 container_of(kref, struct ttm_buffer_object, kref);
615 struct ttm_bo_device *bdev = bo->bdev;
616 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
618 write_lock(&bdev->vm_lock);
619 if (likely(bo->vm_node != NULL)) {
620 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
621 drm_mm_put_block(bo->vm_node);
624 write_unlock(&bdev->vm_lock);
625 ttm_mem_io_lock(man, false);
626 ttm_mem_io_free_vm(bo);
627 ttm_mem_io_unlock(man);
628 ttm_bo_cleanup_refs_or_queue(bo);
629 kref_put(&bo->list_kref, ttm_bo_release_list);
632 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
634 struct ttm_buffer_object *bo = *p_bo;
637 kref_put(&bo->kref, ttm_bo_release);
639 EXPORT_SYMBOL(ttm_bo_unref);
641 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
643 return cancel_delayed_work_sync(&bdev->wq);
645 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
647 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
650 schedule_delayed_work(&bdev->wq,
651 ((HZ / 100) < 1) ? 1 : HZ / 100);
653 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
655 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
658 struct ttm_bo_device *bdev = bo->bdev;
659 struct ttm_mem_reg evict_mem;
660 struct ttm_placement placement;
663 spin_lock(&bdev->fence_lock);
664 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
665 spin_unlock(&bdev->fence_lock);
667 if (unlikely(ret != 0)) {
668 if (ret != -ERESTARTSYS) {
669 pr_err("Failed to expire sync object before buffer eviction\n");
674 lockdep_assert_held(&bo->resv->lock.base);
677 evict_mem.mm_node = NULL;
678 evict_mem.bus.io_reserved_vm = false;
679 evict_mem.bus.io_reserved_count = 0;
683 placement.num_placement = 0;
684 placement.num_busy_placement = 0;
685 bdev->driver->evict_flags(bo, &placement);
686 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
689 if (ret != -ERESTARTSYS) {
690 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
692 ttm_bo_mem_space_debug(bo, &placement);
697 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
700 if (ret != -ERESTARTSYS)
701 pr_err("Buffer eviction failed\n");
702 ttm_bo_mem_put(bo, &evict_mem);
710 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
715 struct ttm_bo_global *glob = bdev->glob;
716 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
717 struct ttm_buffer_object *bo;
718 int ret = -EBUSY, put_count;
720 spin_lock(&glob->lru_lock);
721 list_for_each_entry(bo, &man->lru, lru) {
722 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
728 spin_unlock(&glob->lru_lock);
732 kref_get(&bo->list_kref);
734 if (!list_empty(&bo->ddestroy)) {
735 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
737 kref_put(&bo->list_kref, ttm_bo_release_list);
741 put_count = ttm_bo_del_from_lru(bo);
742 spin_unlock(&glob->lru_lock);
746 ttm_bo_list_ref_sub(bo, put_count, true);
748 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
749 ttm_bo_unreserve(bo);
751 kref_put(&bo->list_kref, ttm_bo_release_list);
755 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
757 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
760 (*man->func->put_node)(man, mem);
762 EXPORT_SYMBOL(ttm_bo_mem_put);
765 * Repeatedly evict memory from the LRU for @mem_type until we create enough
766 * space, or we've evicted everything and there isn't enough space.
768 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
770 struct ttm_placement *placement,
771 struct ttm_mem_reg *mem,
775 struct ttm_bo_device *bdev = bo->bdev;
776 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
780 ret = (*man->func->get_node)(man, bo, placement, mem);
781 if (unlikely(ret != 0))
785 ret = ttm_mem_evict_first(bdev, mem_type,
786 interruptible, no_wait_gpu);
787 if (unlikely(ret != 0))
790 if (mem->mm_node == NULL)
792 mem->mem_type = mem_type;
796 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
797 uint32_t cur_placement,
798 uint32_t proposed_placement)
800 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
801 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
804 * Keep current caching if possible.
807 if ((cur_placement & caching) != 0)
808 result |= (cur_placement & caching);
809 else if ((man->default_caching & caching) != 0)
810 result |= man->default_caching;
811 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
812 result |= TTM_PL_FLAG_CACHED;
813 else if ((TTM_PL_FLAG_WC & caching) != 0)
814 result |= TTM_PL_FLAG_WC;
815 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
816 result |= TTM_PL_FLAG_UNCACHED;
821 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
823 uint32_t proposed_placement,
824 uint32_t *masked_placement)
826 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
828 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
831 if ((proposed_placement & man->available_caching) == 0)
834 cur_flags |= (proposed_placement & man->available_caching);
836 *masked_placement = cur_flags;
841 * Creates space for memory region @mem according to its type.
843 * This function first searches for free space in compatible memory types in
844 * the priority order defined by the driver. If free space isn't found, then
845 * ttm_bo_mem_force_space is attempted in priority order to evict and find
848 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
849 struct ttm_placement *placement,
850 struct ttm_mem_reg *mem,
854 struct ttm_bo_device *bdev = bo->bdev;
855 struct ttm_mem_type_manager *man;
856 uint32_t mem_type = TTM_PL_SYSTEM;
857 uint32_t cur_flags = 0;
858 bool type_found = false;
859 bool type_ok = false;
860 bool has_erestartsys = false;
864 for (i = 0; i < placement->num_placement; ++i) {
865 ret = ttm_mem_type_from_flags(placement->placement[i],
869 man = &bdev->man[mem_type];
871 type_ok = ttm_bo_mt_compatible(man,
873 placement->placement[i],
879 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
882 * Use the access and other non-mapping-related flag bits from
883 * the memory placement flags to the current flags
885 ttm_flag_masked(&cur_flags, placement->placement[i],
886 ~TTM_PL_MASK_MEMTYPE);
888 if (mem_type == TTM_PL_SYSTEM)
891 if (man->has_type && man->use_type) {
893 ret = (*man->func->get_node)(man, bo, placement, mem);
901 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
902 mem->mem_type = mem_type;
903 mem->placement = cur_flags;
910 for (i = 0; i < placement->num_busy_placement; ++i) {
911 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
915 man = &bdev->man[mem_type];
918 if (!ttm_bo_mt_compatible(man,
920 placement->busy_placement[i],
924 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
927 * Use the access and other non-mapping-related flag bits from
928 * the memory placement flags to the current flags
930 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
931 ~TTM_PL_MASK_MEMTYPE);
934 if (mem_type == TTM_PL_SYSTEM) {
935 mem->mem_type = mem_type;
936 mem->placement = cur_flags;
941 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
942 interruptible, no_wait_gpu);
943 if (ret == 0 && mem->mm_node) {
944 mem->placement = cur_flags;
947 if (ret == -ERESTARTSYS)
948 has_erestartsys = true;
950 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
953 EXPORT_SYMBOL(ttm_bo_mem_space);
955 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
956 struct ttm_placement *placement,
961 struct ttm_mem_reg mem;
962 struct ttm_bo_device *bdev = bo->bdev;
964 lockdep_assert_held(&bo->resv->lock.base);
967 * FIXME: It's possible to pipeline buffer moves.
968 * Have the driver move function wait for idle when necessary,
969 * instead of doing it here.
971 spin_lock(&bdev->fence_lock);
972 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
973 spin_unlock(&bdev->fence_lock);
976 mem.num_pages = bo->num_pages;
977 mem.size = mem.num_pages << PAGE_SHIFT;
978 mem.page_alignment = bo->mem.page_alignment;
979 mem.bus.io_reserved_vm = false;
980 mem.bus.io_reserved_count = 0;
982 * Determine where to move the buffer.
984 ret = ttm_bo_mem_space(bo, placement, &mem,
985 interruptible, no_wait_gpu);
988 ret = ttm_bo_handle_move_mem(bo, &mem, false,
989 interruptible, no_wait_gpu);
991 if (ret && mem.mm_node)
992 ttm_bo_mem_put(bo, &mem);
996 static int ttm_bo_mem_compat(struct ttm_placement *placement,
997 struct ttm_mem_reg *mem)
1001 if (mem->mm_node && placement->lpfn != 0 &&
1002 (mem->start < placement->fpfn ||
1003 mem->start + mem->num_pages > placement->lpfn))
1006 for (i = 0; i < placement->num_placement; i++) {
1007 if ((placement->placement[i] & mem->placement &
1008 TTM_PL_MASK_CACHING) &&
1009 (placement->placement[i] & mem->placement &
1016 int ttm_bo_validate(struct ttm_buffer_object *bo,
1017 struct ttm_placement *placement,
1023 lockdep_assert_held(&bo->resv->lock.base);
1024 /* Check that range is valid */
1025 if (placement->lpfn || placement->fpfn)
1026 if (placement->fpfn > placement->lpfn ||
1027 (placement->lpfn - placement->fpfn) < bo->num_pages)
1030 * Check whether we need to move buffer.
1032 ret = ttm_bo_mem_compat(placement, &bo->mem);
1034 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1040 * Use the access and other non-mapping-related flag bits from
1041 * the compatible memory placement flags to the active flags
1043 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1044 ~TTM_PL_MASK_MEMTYPE);
1047 * We might need to add a TTM.
1049 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1050 ret = ttm_bo_add_ttm(bo, true);
1056 EXPORT_SYMBOL(ttm_bo_validate);
1058 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1059 struct ttm_placement *placement)
1061 BUG_ON((placement->fpfn || placement->lpfn) &&
1062 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1067 int ttm_bo_init(struct ttm_bo_device *bdev,
1068 struct ttm_buffer_object *bo,
1070 enum ttm_bo_type type,
1071 struct ttm_placement *placement,
1072 uint32_t page_alignment,
1074 struct file *persistent_swap_storage,
1076 struct sg_table *sg,
1077 void (*destroy) (struct ttm_buffer_object *))
1080 unsigned long num_pages;
1081 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1084 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1086 pr_err("Out of kernel memory\n");
1094 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1095 if (num_pages == 0) {
1096 pr_err("Illegal buffer object size\n");
1101 ttm_mem_global_free(mem_glob, acc_size);
1104 bo->destroy = destroy;
1106 kref_init(&bo->kref);
1107 kref_init(&bo->list_kref);
1108 atomic_set(&bo->cpu_writers, 0);
1109 INIT_LIST_HEAD(&bo->lru);
1110 INIT_LIST_HEAD(&bo->ddestroy);
1111 INIT_LIST_HEAD(&bo->swap);
1112 INIT_LIST_HEAD(&bo->io_reserve_lru);
1114 bo->glob = bdev->glob;
1116 bo->num_pages = num_pages;
1117 bo->mem.size = num_pages << PAGE_SHIFT;
1118 bo->mem.mem_type = TTM_PL_SYSTEM;
1119 bo->mem.num_pages = bo->num_pages;
1120 bo->mem.mm_node = NULL;
1121 bo->mem.page_alignment = page_alignment;
1122 bo->mem.bus.io_reserved_vm = false;
1123 bo->mem.bus.io_reserved_count = 0;
1125 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1126 bo->persistent_swap_storage = persistent_swap_storage;
1127 bo->acc_size = acc_size;
1129 bo->resv = &bo->ttm_resv;
1130 reservation_object_init(bo->resv);
1131 atomic_inc(&bo->glob->bo_count);
1133 ret = ttm_bo_check_placement(bo, placement);
1136 * For ttm_bo_type_device buffers, allocate
1137 * address space from the device.
1140 (bo->type == ttm_bo_type_device ||
1141 bo->type == ttm_bo_type_sg))
1142 ret = ttm_bo_setup_vm(bo);
1144 locked = ww_mutex_trylock(&bo->resv->lock);
1148 ret = ttm_bo_validate(bo, placement, interruptible, false);
1150 ttm_bo_unreserve(bo);
1157 EXPORT_SYMBOL(ttm_bo_init);
1159 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1160 unsigned long bo_size,
1161 unsigned struct_size)
1163 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1166 size += ttm_round_pot(struct_size);
1167 size += PAGE_ALIGN(npages * sizeof(void *));
1168 size += ttm_round_pot(sizeof(struct ttm_tt));
1171 EXPORT_SYMBOL(ttm_bo_acc_size);
1173 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1174 unsigned long bo_size,
1175 unsigned struct_size)
1177 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1180 size += ttm_round_pot(struct_size);
1181 size += PAGE_ALIGN(npages * sizeof(void *));
1182 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1183 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1186 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1188 int ttm_bo_create(struct ttm_bo_device *bdev,
1190 enum ttm_bo_type type,
1191 struct ttm_placement *placement,
1192 uint32_t page_alignment,
1194 struct file *persistent_swap_storage,
1195 struct ttm_buffer_object **p_bo)
1197 struct ttm_buffer_object *bo;
1201 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1202 if (unlikely(bo == NULL))
1205 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1206 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1207 interruptible, persistent_swap_storage, acc_size,
1209 if (likely(ret == 0))
1214 EXPORT_SYMBOL(ttm_bo_create);
1216 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1217 unsigned mem_type, bool allow_errors)
1219 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1220 struct ttm_bo_global *glob = bdev->glob;
1224 * Can't use standard list traversal since we're unlocking.
1227 spin_lock(&glob->lru_lock);
1228 while (!list_empty(&man->lru)) {
1229 spin_unlock(&glob->lru_lock);
1230 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1235 pr_err("Cleanup eviction failed\n");
1238 spin_lock(&glob->lru_lock);
1240 spin_unlock(&glob->lru_lock);
1244 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1246 struct ttm_mem_type_manager *man;
1249 if (mem_type >= TTM_NUM_MEM_TYPES) {
1250 pr_err("Illegal memory type %d\n", mem_type);
1253 man = &bdev->man[mem_type];
1255 if (!man->has_type) {
1256 pr_err("Trying to take down uninitialized memory manager type %u\n",
1261 man->use_type = false;
1262 man->has_type = false;
1266 ttm_bo_force_list_clean(bdev, mem_type, false);
1268 ret = (*man->func->takedown)(man);
1273 EXPORT_SYMBOL(ttm_bo_clean_mm);
1275 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1277 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1279 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1280 pr_err("Illegal memory manager memory type %u\n", mem_type);
1284 if (!man->has_type) {
1285 pr_err("Memory type %u has not been initialized\n", mem_type);
1289 return ttm_bo_force_list_clean(bdev, mem_type, true);
1291 EXPORT_SYMBOL(ttm_bo_evict_mm);
1293 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1294 unsigned long p_size)
1297 struct ttm_mem_type_manager *man;
1299 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1300 man = &bdev->man[type];
1301 BUG_ON(man->has_type);
1302 man->io_reserve_fastpath = true;
1303 man->use_io_reserve_lru = false;
1304 mutex_init(&man->io_reserve_mutex);
1305 INIT_LIST_HEAD(&man->io_reserve_lru);
1307 ret = bdev->driver->init_mem_type(bdev, type, man);
1313 if (type != TTM_PL_SYSTEM) {
1314 ret = (*man->func->init)(man, p_size);
1318 man->has_type = true;
1319 man->use_type = true;
1322 INIT_LIST_HEAD(&man->lru);
1326 EXPORT_SYMBOL(ttm_bo_init_mm);
1328 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1330 struct ttm_bo_global *glob =
1331 container_of(kobj, struct ttm_bo_global, kobj);
1333 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1334 __free_page(glob->dummy_read_page);
1338 void ttm_bo_global_release(struct drm_global_reference *ref)
1340 struct ttm_bo_global *glob = ref->object;
1342 kobject_del(&glob->kobj);
1343 kobject_put(&glob->kobj);
1345 EXPORT_SYMBOL(ttm_bo_global_release);
1347 int ttm_bo_global_init(struct drm_global_reference *ref)
1349 struct ttm_bo_global_ref *bo_ref =
1350 container_of(ref, struct ttm_bo_global_ref, ref);
1351 struct ttm_bo_global *glob = ref->object;
1354 mutex_init(&glob->device_list_mutex);
1355 spin_lock_init(&glob->lru_lock);
1356 glob->mem_glob = bo_ref->mem_glob;
1357 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1359 if (unlikely(glob->dummy_read_page == NULL)) {
1364 INIT_LIST_HEAD(&glob->swap_lru);
1365 INIT_LIST_HEAD(&glob->device_list);
1367 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1368 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1369 if (unlikely(ret != 0)) {
1370 pr_err("Could not register buffer object swapout\n");
1374 atomic_set(&glob->bo_count, 0);
1376 ret = kobject_init_and_add(
1377 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1378 if (unlikely(ret != 0))
1379 kobject_put(&glob->kobj);
1382 __free_page(glob->dummy_read_page);
1387 EXPORT_SYMBOL(ttm_bo_global_init);
1390 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1393 unsigned i = TTM_NUM_MEM_TYPES;
1394 struct ttm_mem_type_manager *man;
1395 struct ttm_bo_global *glob = bdev->glob;
1398 man = &bdev->man[i];
1399 if (man->has_type) {
1400 man->use_type = false;
1401 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1403 pr_err("DRM memory manager type %d is not clean\n",
1406 man->has_type = false;
1410 mutex_lock(&glob->device_list_mutex);
1411 list_del(&bdev->device_list);
1412 mutex_unlock(&glob->device_list_mutex);
1414 cancel_delayed_work_sync(&bdev->wq);
1416 while (ttm_bo_delayed_delete(bdev, true))
1419 spin_lock(&glob->lru_lock);
1420 if (list_empty(&bdev->ddestroy))
1421 TTM_DEBUG("Delayed destroy list was clean\n");
1423 if (list_empty(&bdev->man[0].lru))
1424 TTM_DEBUG("Swap list was clean\n");
1425 spin_unlock(&glob->lru_lock);
1427 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1428 write_lock(&bdev->vm_lock);
1429 drm_mm_takedown(&bdev->addr_space_mm);
1430 write_unlock(&bdev->vm_lock);
1434 EXPORT_SYMBOL(ttm_bo_device_release);
1436 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1437 struct ttm_bo_global *glob,
1438 struct ttm_bo_driver *driver,
1439 uint64_t file_page_offset,
1444 rwlock_init(&bdev->vm_lock);
1445 bdev->driver = driver;
1447 memset(bdev->man, 0, sizeof(bdev->man));
1450 * Initialize the system memory buffer type.
1451 * Other types need to be driver / IOCTL initialized.
1453 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1454 if (unlikely(ret != 0))
1457 bdev->addr_space_rb = RB_ROOT;
1458 drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1460 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1461 INIT_LIST_HEAD(&bdev->ddestroy);
1462 bdev->dev_mapping = NULL;
1464 bdev->need_dma32 = need_dma32;
1466 spin_lock_init(&bdev->fence_lock);
1467 mutex_lock(&glob->device_list_mutex);
1468 list_add_tail(&bdev->device_list, &glob->device_list);
1469 mutex_unlock(&glob->device_list_mutex);
1475 EXPORT_SYMBOL(ttm_bo_device_init);
1478 * buffer object vm functions.
1481 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1483 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1485 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1486 if (mem->mem_type == TTM_PL_SYSTEM)
1489 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1492 if (mem->placement & TTM_PL_FLAG_CACHED)
1498 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1500 struct ttm_bo_device *bdev = bo->bdev;
1501 loff_t offset = (loff_t) bo->addr_space_offset;
1502 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1504 if (!bdev->dev_mapping)
1506 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1507 ttm_mem_io_free_vm(bo);
1510 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1512 struct ttm_bo_device *bdev = bo->bdev;
1513 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1515 ttm_mem_io_lock(man, false);
1516 ttm_bo_unmap_virtual_locked(bo);
1517 ttm_mem_io_unlock(man);
1521 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1523 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1525 struct ttm_bo_device *bdev = bo->bdev;
1526 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1527 struct rb_node *parent = NULL;
1528 struct ttm_buffer_object *cur_bo;
1529 unsigned long offset = bo->vm_node->start;
1530 unsigned long cur_offset;
1534 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1535 cur_offset = cur_bo->vm_node->start;
1536 if (offset < cur_offset)
1537 cur = &parent->rb_left;
1538 else if (offset > cur_offset)
1539 cur = &parent->rb_right;
1544 rb_link_node(&bo->vm_rb, parent, cur);
1545 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1551 * @bo: the buffer to allocate address space for
1553 * Allocate address space in the drm device so that applications
1554 * can mmap the buffer and access the contents. This only
1555 * applies to ttm_bo_type_device objects as others are not
1556 * placed in the drm device address space.
1559 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1561 struct ttm_bo_device *bdev = bo->bdev;
1565 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1566 if (unlikely(ret != 0))
1569 write_lock(&bdev->vm_lock);
1570 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1571 bo->mem.num_pages, 0, 0);
1573 if (unlikely(bo->vm_node == NULL)) {
1578 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1579 bo->mem.num_pages, 0);
1581 if (unlikely(bo->vm_node == NULL)) {
1582 write_unlock(&bdev->vm_lock);
1586 ttm_bo_vm_insert_rb(bo);
1587 write_unlock(&bdev->vm_lock);
1588 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1592 write_unlock(&bdev->vm_lock);
1596 int ttm_bo_wait(struct ttm_buffer_object *bo,
1597 bool lazy, bool interruptible, bool no_wait)
1599 struct ttm_bo_driver *driver = bo->bdev->driver;
1600 struct ttm_bo_device *bdev = bo->bdev;
1604 if (likely(bo->sync_obj == NULL))
1607 while (bo->sync_obj) {
1609 if (driver->sync_obj_signaled(bo->sync_obj)) {
1610 void *tmp_obj = bo->sync_obj;
1611 bo->sync_obj = NULL;
1612 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1613 spin_unlock(&bdev->fence_lock);
1614 driver->sync_obj_unref(&tmp_obj);
1615 spin_lock(&bdev->fence_lock);
1622 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1623 spin_unlock(&bdev->fence_lock);
1624 ret = driver->sync_obj_wait(sync_obj,
1625 lazy, interruptible);
1626 if (unlikely(ret != 0)) {
1627 driver->sync_obj_unref(&sync_obj);
1628 spin_lock(&bdev->fence_lock);
1631 spin_lock(&bdev->fence_lock);
1632 if (likely(bo->sync_obj == sync_obj)) {
1633 void *tmp_obj = bo->sync_obj;
1634 bo->sync_obj = NULL;
1635 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1637 spin_unlock(&bdev->fence_lock);
1638 driver->sync_obj_unref(&sync_obj);
1639 driver->sync_obj_unref(&tmp_obj);
1640 spin_lock(&bdev->fence_lock);
1642 spin_unlock(&bdev->fence_lock);
1643 driver->sync_obj_unref(&sync_obj);
1644 spin_lock(&bdev->fence_lock);
1649 EXPORT_SYMBOL(ttm_bo_wait);
1651 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1653 struct ttm_bo_device *bdev = bo->bdev;
1657 * Using ttm_bo_reserve makes sure the lru lists are updated.
1660 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1661 if (unlikely(ret != 0))
1663 spin_lock(&bdev->fence_lock);
1664 ret = ttm_bo_wait(bo, false, true, no_wait);
1665 spin_unlock(&bdev->fence_lock);
1666 if (likely(ret == 0))
1667 atomic_inc(&bo->cpu_writers);
1668 ttm_bo_unreserve(bo);
1671 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1673 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1675 atomic_dec(&bo->cpu_writers);
1677 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1680 * A buffer object shrink method that tries to swap out the first
1681 * buffer object on the bo_global::swap_lru list.
1684 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1686 struct ttm_bo_global *glob =
1687 container_of(shrink, struct ttm_bo_global, shrink);
1688 struct ttm_buffer_object *bo;
1691 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1693 spin_lock(&glob->lru_lock);
1694 list_for_each_entry(bo, &glob->swap_lru, swap) {
1695 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1701 spin_unlock(&glob->lru_lock);
1705 kref_get(&bo->list_kref);
1707 if (!list_empty(&bo->ddestroy)) {
1708 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1709 kref_put(&bo->list_kref, ttm_bo_release_list);
1713 put_count = ttm_bo_del_from_lru(bo);
1714 spin_unlock(&glob->lru_lock);
1716 ttm_bo_list_ref_sub(bo, put_count, true);
1719 * Wait for GPU, then move to system cached.
1722 spin_lock(&bo->bdev->fence_lock);
1723 ret = ttm_bo_wait(bo, false, false, false);
1724 spin_unlock(&bo->bdev->fence_lock);
1726 if (unlikely(ret != 0))
1729 if ((bo->mem.placement & swap_placement) != swap_placement) {
1730 struct ttm_mem_reg evict_mem;
1732 evict_mem = bo->mem;
1733 evict_mem.mm_node = NULL;
1734 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1735 evict_mem.mem_type = TTM_PL_SYSTEM;
1737 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1739 if (unlikely(ret != 0))
1743 ttm_bo_unmap_virtual(bo);
1746 * Swap out. Buffer will be swapped in again as soon as
1747 * anyone tries to access a ttm page.
1750 if (bo->bdev->driver->swap_notify)
1751 bo->bdev->driver->swap_notify(bo);
1753 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1758 * Unreserve without putting on LRU to avoid swapping out an
1759 * already swapped buffer.
1762 ww_mutex_unlock(&bo->resv->lock);
1763 kref_put(&bo->list_kref, ttm_bo_release_list);
1767 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1769 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1772 EXPORT_SYMBOL(ttm_bo_swapout_all);