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/atomic.h>
37 #include <linux/errno.h>
38 #include <linux/export.h>
39 #include <linux/rbtree.h>
40 #include <linux/wait.h>
42 #define TTM_ASSERT_LOCKED(param)
43 #define TTM_DEBUG(fmt, arg...)
44 #define TTM_BO_HASH_ORDER 13
46 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
47 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
48 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob);
50 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
54 for (i = 0; i <= TTM_PL_PRIV5; i++)
55 if (flags & (1 << i)) {
62 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
64 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
66 kprintf(" has_type: %d\n", man->has_type);
67 kprintf(" use_type: %d\n", man->use_type);
68 kprintf(" flags: 0x%08X\n", man->flags);
69 kprintf(" gpu_offset: 0x%08lX\n", man->gpu_offset);
70 kprintf(" size: %ju\n", (uintmax_t)man->size);
71 kprintf(" available_caching: 0x%08X\n", man->available_caching);
72 kprintf(" default_caching: 0x%08X\n", man->default_caching);
73 if (mem_type != TTM_PL_SYSTEM)
74 (*man->func->debug)(man, TTM_PFX);
77 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
78 struct ttm_placement *placement)
82 kprintf("No space for %p (%lu pages, %luK, %luM)\n",
83 bo, bo->mem.num_pages, bo->mem.size >> 10,
85 for (i = 0; i < placement->num_placement; i++) {
86 ret = ttm_mem_type_from_flags(placement->placement[i],
90 kprintf(" placement[%d]=0x%08X (%d)\n",
91 i, placement->placement[i], mem_type);
92 ttm_mem_type_debug(bo->bdev, mem_type);
97 static ssize_t ttm_bo_global_show(struct ttm_bo_global *glob,
101 return snprintf(buffer, PAGE_SIZE, "%lu\n",
102 (unsigned long) atomic_read(&glob->bo_count));
106 static inline uint32_t ttm_bo_type_flags(unsigned type)
111 static void ttm_bo_release_list(struct kref *list_kref)
113 struct ttm_buffer_object *bo =
114 container_of(list_kref, struct ttm_buffer_object, list_kref);
115 struct ttm_bo_device *bdev = bo->bdev;
116 size_t acc_size = bo->acc_size;
118 BUG_ON(atomic_read(&bo->list_kref.refcount));
119 BUG_ON(atomic_read(&bo->kref.refcount));
120 BUG_ON(atomic_read(&bo->cpu_writers));
121 BUG_ON(bo->sync_obj != NULL);
122 BUG_ON(bo->mem.mm_node != NULL);
123 BUG_ON(!list_empty(&bo->lru));
124 BUG_ON(!list_empty(&bo->ddestroy));
127 ttm_tt_destroy(bo->ttm);
128 atomic_dec(&bo->glob->bo_count);
134 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
137 static int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,
141 return wait_event_interruptible(bo->event_queue,
142 !ttm_bo_is_reserved(bo));
144 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
149 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
151 struct ttm_bo_device *bdev = bo->bdev;
152 struct ttm_mem_type_manager *man;
154 BUG_ON(!ttm_bo_is_reserved(bo));
156 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
158 BUG_ON(!list_empty(&bo->lru));
160 man = &bdev->man[bo->mem.mem_type];
161 list_add_tail(&bo->lru, &man->lru);
162 kref_get(&bo->list_kref);
164 if (bo->ttm != NULL) {
165 list_add_tail(&bo->swap, &bo->glob->swap_lru);
166 kref_get(&bo->list_kref);
171 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
175 if (!list_empty(&bo->swap)) {
176 list_del_init(&bo->swap);
179 if (!list_empty(&bo->lru)) {
180 list_del_init(&bo->lru);
185 * TODO: Add a driver hook to delete from
186 * driver-specific LRU's here.
192 int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
194 bool no_wait, bool use_sequence, uint32_t sequence)
198 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
200 * Deadlock avoidance for multi-bo reserving.
202 if (use_sequence && bo->seq_valid) {
204 * We've already reserved this one.
206 if (unlikely(sequence == bo->val_seq))
209 * Already reserved by a thread that will not back
210 * off for us. We need to back off.
212 if (unlikely(sequence - bo->val_seq < (1U << 31)))
219 ret = ttm_bo_wait_unreserved(bo, interruptible);
226 bool wake_up = false;
228 * Wake up waiters that may need to recheck for deadlock,
229 * if we decreased the sequence number.
231 if (unlikely((bo->val_seq - sequence < (1U << 31))
236 * In the worst case with memory ordering these values can be
237 * seen in the wrong order. However since we call wake_up_all
238 * in that case, this will hopefully not pose a problem,
239 * and the worst case would only cause someone to accidentally
240 * hit -EAGAIN in ttm_bo_reserve when they see old value of
241 * val_seq. However this would only happen if seq_valid was
242 * written before val_seq was, and just means some slightly
243 * increased cpu usage
245 bo->val_seq = sequence;
246 bo->seq_valid = true;
248 wake_up_all(&bo->event_queue);
250 bo->seq_valid = false;
255 EXPORT_SYMBOL(ttm_bo_reserve);
257 static void ttm_bo_ref_bug(struct kref *list_kref)
262 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
265 kref_sub(&bo->list_kref, count,
266 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
269 int ttm_bo_reserve(struct ttm_buffer_object *bo,
271 bool no_wait, bool use_sequence, uint32_t sequence)
273 struct ttm_bo_global *glob = bo->glob;
277 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_sequence,
279 if (likely(ret == 0)) {
280 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
281 put_count = ttm_bo_del_from_lru(bo);
282 lockmgr(&glob->lru_lock, LK_RELEASE);
283 ttm_bo_list_ref_sub(bo, put_count, true);
289 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
290 bool interruptible, uint32_t sequence)
292 bool wake_up = false;
295 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
296 WARN_ON(bo->seq_valid && sequence == bo->val_seq);
298 ret = ttm_bo_wait_unreserved(bo, interruptible);
304 if ((bo->val_seq - sequence < (1U << 31)) || !bo->seq_valid)
308 * Wake up waiters that may need to recheck for deadlock,
309 * if we decreased the sequence number.
311 bo->val_seq = sequence;
312 bo->seq_valid = true;
314 wake_up_all(&bo->event_queue);
319 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
320 bool interruptible, uint32_t sequence)
322 struct ttm_bo_global *glob = bo->glob;
325 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence);
327 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
328 put_count = ttm_bo_del_from_lru(bo);
329 lockmgr(&glob->lru_lock, LK_RELEASE);
330 ttm_bo_list_ref_sub(bo, put_count, true);
334 EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
337 * Must interlock with event_queue to avoid race against
338 * wait_event_common() which can cause wait_event_common()
342 ttm_bo_unreserve_core(struct ttm_buffer_object *bo)
344 lockmgr(&bo->event_queue.lock, LK_EXCLUSIVE);
345 atomic_set(&bo->reserved, 0);
346 lockmgr(&bo->event_queue.lock, LK_RELEASE);
347 wake_up_all(&bo->event_queue);
350 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
352 ttm_bo_add_to_lru(bo);
353 ttm_bo_unreserve_core(bo);
356 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
358 struct ttm_bo_global *glob = bo->glob;
360 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
361 ttm_bo_unreserve_locked(bo);
362 lockmgr(&glob->lru_lock, LK_RELEASE);
364 EXPORT_SYMBOL(ttm_bo_unreserve);
367 * Call bo->mutex locked.
369 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
371 struct ttm_bo_device *bdev = bo->bdev;
372 struct ttm_bo_global *glob = bo->glob;
374 uint32_t page_flags = 0;
376 TTM_ASSERT_LOCKED(&bo->mutex);
379 if (bdev->need_dma32)
380 page_flags |= TTM_PAGE_FLAG_DMA32;
383 case ttm_bo_type_device:
385 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
386 case ttm_bo_type_kernel:
387 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
388 page_flags, glob->dummy_read_page);
389 if (unlikely(bo->ttm == NULL))
393 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
394 page_flags | TTM_PAGE_FLAG_SG,
395 glob->dummy_read_page);
396 if (unlikely(bo->ttm == NULL)) {
400 bo->ttm->sg = bo->sg;
403 kprintf("[TTM] Illegal buffer object type\n");
411 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
412 struct ttm_mem_reg *mem,
413 bool evict, bool interruptible,
416 struct ttm_bo_device *bdev = bo->bdev;
417 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
418 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
419 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
420 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
423 if (old_is_pci || new_is_pci ||
424 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
425 ret = ttm_mem_io_lock(old_man, true);
426 if (unlikely(ret != 0))
428 ttm_bo_unmap_virtual_locked(bo);
429 ttm_mem_io_unlock(old_man);
433 * Create and bind a ttm if required.
436 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
437 if (bo->ttm == NULL) {
438 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
439 ret = ttm_bo_add_ttm(bo, zero);
444 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
448 if (mem->mem_type != TTM_PL_SYSTEM) {
449 ret = ttm_tt_bind(bo->ttm, mem);
454 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
455 if (bdev->driver->move_notify)
456 bdev->driver->move_notify(bo, mem);
463 if (bdev->driver->move_notify)
464 bdev->driver->move_notify(bo, mem);
466 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
467 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
468 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
469 else if (bdev->driver->move)
470 ret = bdev->driver->move(bo, evict, interruptible,
473 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
476 if (bdev->driver->move_notify) {
477 struct ttm_mem_reg tmp_mem = *mem;
480 bdev->driver->move_notify(bo, mem);
490 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
492 kprintf("[TTM] Can not flush read caches\n");
496 if (bo->mem.mm_node) {
497 bo->offset = (bo->mem.start << PAGE_SHIFT) +
498 bdev->man[bo->mem.mem_type].gpu_offset;
499 bo->cur_placement = bo->mem.placement;
506 new_man = &bdev->man[bo->mem.mem_type];
507 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
508 ttm_tt_unbind(bo->ttm);
509 ttm_tt_destroy(bo->ttm);
518 * Will release GPU memory type usage on destruction.
519 * This is the place to put in driver specific hooks to release
520 * driver private resources.
521 * Will release the bo::reserved lock.
524 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
526 if (bo->bdev->driver->move_notify)
527 bo->bdev->driver->move_notify(bo, NULL);
530 ttm_tt_unbind(bo->ttm);
531 ttm_tt_destroy(bo->ttm);
534 ttm_bo_mem_put(bo, &bo->mem);
535 ttm_bo_unreserve_core(bo);
538 * Since the final reference to this bo may not be dropped by
539 * the current task we have to put a memory barrier here to make
540 * sure the changes done in this function are always visible.
542 * This function only needs protection against the final kref_put.
547 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
549 struct ttm_bo_device *bdev = bo->bdev;
550 struct ttm_bo_global *glob = bo->glob;
551 struct ttm_bo_driver *driver = bdev->driver;
552 void *sync_obj = NULL;
556 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
557 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
559 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
560 (void) ttm_bo_wait(bo, false, false, true);
561 if (!ret && !bo->sync_obj) {
562 lockmgr(&bdev->fence_lock, LK_RELEASE);
563 put_count = ttm_bo_del_from_lru(bo);
565 lockmgr(&glob->lru_lock, LK_RELEASE);
566 ttm_bo_cleanup_memtype_use(bo);
568 ttm_bo_list_ref_sub(bo, put_count, true);
573 sync_obj = driver->sync_obj_ref(bo->sync_obj);
574 lockmgr(&bdev->fence_lock, LK_RELEASE);
577 ttm_bo_unreserve_core(bo);
580 kref_get(&bo->list_kref);
581 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
582 lockmgr(&glob->lru_lock, LK_RELEASE);
585 driver->sync_obj_flush(sync_obj);
586 driver->sync_obj_unref(&sync_obj);
588 schedule_delayed_work(&bdev->wq,
589 ((hz / 100) < 1) ? 1 : hz / 100);
593 * function ttm_bo_cleanup_refs_and_unlock
594 * If bo idle, remove from delayed- and lru lists, and unref.
595 * If not idle, do nothing.
597 * Must be called with lru_lock and reservation held, this function
598 * will drop both before returning.
600 * @interruptible Any sleeps should occur interruptibly.
601 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
604 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
608 struct ttm_bo_device *bdev = bo->bdev;
609 struct ttm_bo_driver *driver = bdev->driver;
610 struct ttm_bo_global *glob = bo->glob;
614 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
615 ret = ttm_bo_wait(bo, false, false, true);
617 if (ret && !no_wait_gpu) {
621 * Take a reference to the fence and unreserve,
622 * at this point the buffer should be dead, so
623 * no new sync objects can be attached.
625 sync_obj = driver->sync_obj_ref(bo->sync_obj);
626 lockmgr(&bdev->fence_lock, LK_RELEASE);
628 ttm_bo_unreserve_core(bo);
629 lockmgr(&glob->lru_lock, LK_RELEASE);
631 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
632 driver->sync_obj_unref(&sync_obj);
637 * remove sync_obj with ttm_bo_wait, the wait should be
638 * finished, and no new wait object should have been added.
640 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
641 ret = ttm_bo_wait(bo, false, false, true);
643 lockmgr(&bdev->fence_lock, LK_RELEASE);
647 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
648 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
651 * We raced, and lost, someone else holds the reservation now,
652 * and is probably busy in ttm_bo_cleanup_memtype_use.
654 * Even if it's not the case, because we finished waiting any
655 * delayed destruction would succeed, so just return success
659 lockmgr(&glob->lru_lock, LK_RELEASE);
663 lockmgr(&bdev->fence_lock, LK_RELEASE);
665 if (ret || unlikely(list_empty(&bo->ddestroy))) {
666 ttm_bo_unreserve_core(bo);
667 lockmgr(&glob->lru_lock, LK_RELEASE);
671 put_count = ttm_bo_del_from_lru(bo);
672 list_del_init(&bo->ddestroy);
675 lockmgr(&glob->lru_lock, LK_RELEASE);
676 ttm_bo_cleanup_memtype_use(bo);
678 ttm_bo_list_ref_sub(bo, put_count, true);
684 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
685 * encountered buffers.
688 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
690 struct ttm_bo_global *glob = bdev->glob;
691 struct ttm_buffer_object *entry = NULL;
694 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
695 if (list_empty(&bdev->ddestroy))
698 entry = list_first_entry(&bdev->ddestroy,
699 struct ttm_buffer_object, ddestroy);
700 kref_get(&entry->list_kref);
703 struct ttm_buffer_object *nentry = NULL;
705 if (entry->ddestroy.next != &bdev->ddestroy) {
706 nentry = list_first_entry(&entry->ddestroy,
707 struct ttm_buffer_object, ddestroy);
708 kref_get(&nentry->list_kref);
711 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
712 if (remove_all && ret) {
713 lockmgr(&glob->lru_lock, LK_RELEASE);
714 ret = ttm_bo_reserve_nolru(entry, false, false,
716 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
720 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
723 lockmgr(&glob->lru_lock, LK_RELEASE);
725 kref_put(&entry->list_kref, ttm_bo_release_list);
731 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
732 if (list_empty(&entry->ddestroy))
737 lockmgr(&glob->lru_lock, LK_RELEASE);
740 kref_put(&entry->list_kref, ttm_bo_release_list);
744 static void ttm_bo_delayed_workqueue(struct work_struct *work)
746 struct ttm_bo_device *bdev =
747 container_of(work, struct ttm_bo_device, wq.work);
749 if (ttm_bo_delayed_delete(bdev, false)) {
750 schedule_delayed_work(&bdev->wq,
751 ((hz / 100) < 1) ? 1 : hz / 100);
756 * NOTE: bdev->vm_lock already held on call, this function release it.
758 static void ttm_bo_release(struct kref *kref)
760 struct ttm_buffer_object *bo =
761 container_of(kref, struct ttm_buffer_object, kref);
762 struct ttm_bo_device *bdev = bo->bdev;
763 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
766 if (atomic_read(&bo->kref.refcount) > 0) {
767 lockmgr(&bdev->vm_lock, LK_RELEASE);
770 if (likely(bo->vm_node != NULL)) {
771 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
772 drm_mm_put_block(bo->vm_node);
777 * Should we clean up our implied list_kref? Because ttm_bo_release()
778 * can be called reentrantly due to races (this may not be true any
779 * more with the lock management changes in the deref), it is possible
780 * to get here twice, but there's only one list_kref ref to drop and
781 * in the other path 'bo' can be kfree()d by another thread the
782 * instant we release our lock.
784 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
785 if (release_active) {
786 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
787 lockmgr(&bdev->vm_lock, LK_RELEASE);
788 ttm_mem_io_lock(man, false);
789 ttm_mem_io_free_vm(bo);
790 ttm_mem_io_unlock(man);
791 ttm_bo_cleanup_refs_or_queue(bo);
792 kref_put(&bo->list_kref, ttm_bo_release_list);
794 lockmgr(&bdev->vm_lock, LK_RELEASE);
798 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
800 struct ttm_buffer_object *bo = *p_bo;
801 struct ttm_bo_device *bdev = bo->bdev;
804 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
805 if (kref_put(&bo->kref, ttm_bo_release) == 0)
806 lockmgr(&bdev->vm_lock, LK_RELEASE);
808 EXPORT_SYMBOL(ttm_bo_unref);
810 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
812 return cancel_delayed_work_sync(&bdev->wq);
814 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
816 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
819 schedule_delayed_work(&bdev->wq,
820 ((hz / 100) < 1) ? 1 : hz / 100);
822 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
824 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
827 struct ttm_bo_device *bdev = bo->bdev;
828 struct ttm_mem_reg evict_mem;
829 struct ttm_placement placement;
832 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
833 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
834 lockmgr(&bdev->fence_lock, LK_RELEASE);
836 if (unlikely(ret != 0)) {
837 if (ret != -ERESTARTSYS) {
838 pr_err("Failed to expire sync object before buffer eviction\n");
843 BUG_ON(!ttm_bo_is_reserved(bo));
846 evict_mem.mm_node = NULL;
847 evict_mem.bus.io_reserved_vm = false;
848 evict_mem.bus.io_reserved_count = 0;
852 placement.num_placement = 0;
853 placement.num_busy_placement = 0;
854 bdev->driver->evict_flags(bo, &placement);
855 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
858 if (ret != -ERESTARTSYS) {
859 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
861 ttm_bo_mem_space_debug(bo, &placement);
866 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
869 if (ret != -ERESTARTSYS)
870 pr_err("Buffer eviction failed\n");
871 ttm_bo_mem_put(bo, &evict_mem);
879 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
884 struct ttm_bo_global *glob = bdev->glob;
885 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
886 struct ttm_buffer_object *bo;
887 int ret = -EBUSY, put_count;
889 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
890 list_for_each_entry(bo, &man->lru, lru) {
891 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
897 lockmgr(&glob->lru_lock, LK_RELEASE);
901 kref_get(&bo->list_kref);
903 if (!list_empty(&bo->ddestroy)) {
904 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
906 kref_put(&bo->list_kref, ttm_bo_release_list);
910 put_count = ttm_bo_del_from_lru(bo);
911 lockmgr(&glob->lru_lock, LK_RELEASE);
915 ttm_bo_list_ref_sub(bo, put_count, true);
917 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
918 ttm_bo_unreserve(bo);
920 kref_put(&bo->list_kref, ttm_bo_release_list);
924 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
926 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
929 (*man->func->put_node)(man, mem);
931 EXPORT_SYMBOL(ttm_bo_mem_put);
934 * Repeatedly evict memory from the LRU for @mem_type until we create enough
935 * space, or we've evicted everything and there isn't enough space.
937 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
939 struct ttm_placement *placement,
940 struct ttm_mem_reg *mem,
944 struct ttm_bo_device *bdev = bo->bdev;
945 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
949 ret = (*man->func->get_node)(man, bo, placement, mem);
950 if (unlikely(ret != 0))
954 ret = ttm_mem_evict_first(bdev, mem_type,
955 interruptible, no_wait_gpu);
956 if (unlikely(ret != 0))
959 if (mem->mm_node == NULL)
961 mem->mem_type = mem_type;
965 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
966 uint32_t cur_placement,
967 uint32_t proposed_placement)
969 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
970 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
973 * Keep current caching if possible.
976 if ((cur_placement & caching) != 0)
977 result |= (cur_placement & caching);
978 else if ((man->default_caching & caching) != 0)
979 result |= man->default_caching;
980 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
981 result |= TTM_PL_FLAG_CACHED;
982 else if ((TTM_PL_FLAG_WC & caching) != 0)
983 result |= TTM_PL_FLAG_WC;
984 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
985 result |= TTM_PL_FLAG_UNCACHED;
990 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
992 uint32_t proposed_placement,
993 uint32_t *masked_placement)
995 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
997 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
1000 if ((proposed_placement & man->available_caching) == 0)
1003 cur_flags |= (proposed_placement & man->available_caching);
1005 *masked_placement = cur_flags;
1010 * Creates space for memory region @mem according to its type.
1012 * This function first searches for free space in compatible memory types in
1013 * the priority order defined by the driver. If free space isn't found, then
1014 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1017 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1018 struct ttm_placement *placement,
1019 struct ttm_mem_reg *mem,
1023 struct ttm_bo_device *bdev = bo->bdev;
1024 struct ttm_mem_type_manager *man;
1025 uint32_t mem_type = TTM_PL_SYSTEM;
1026 uint32_t cur_flags = 0;
1027 bool type_found = false;
1028 bool type_ok = false;
1029 bool has_erestartsys = false;
1032 mem->mm_node = NULL;
1033 for (i = 0; i < placement->num_placement; ++i) {
1034 ret = ttm_mem_type_from_flags(placement->placement[i],
1038 man = &bdev->man[mem_type];
1040 type_ok = ttm_bo_mt_compatible(man,
1042 placement->placement[i],
1048 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1051 * Use the access and other non-mapping-related flag bits from
1052 * the memory placement flags to the current flags
1054 ttm_flag_masked(&cur_flags, placement->placement[i],
1055 ~TTM_PL_MASK_MEMTYPE);
1057 if (mem_type == TTM_PL_SYSTEM)
1060 if (man->has_type && man->use_type) {
1062 ret = (*man->func->get_node)(man, bo, placement, mem);
1070 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1071 mem->mem_type = mem_type;
1072 mem->placement = cur_flags;
1079 for (i = 0; i < placement->num_busy_placement; ++i) {
1080 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1084 man = &bdev->man[mem_type];
1087 if (!ttm_bo_mt_compatible(man,
1089 placement->busy_placement[i],
1093 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1096 * Use the access and other non-mapping-related flag bits from
1097 * the memory placement flags to the current flags
1099 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1100 ~TTM_PL_MASK_MEMTYPE);
1103 if (mem_type == TTM_PL_SYSTEM) {
1104 mem->mem_type = mem_type;
1105 mem->placement = cur_flags;
1106 mem->mm_node = NULL;
1110 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1111 interruptible, no_wait_gpu);
1112 if (ret == 0 && mem->mm_node) {
1113 mem->placement = cur_flags;
1116 if (ret == -ERESTARTSYS)
1117 has_erestartsys = true;
1119 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1122 EXPORT_SYMBOL(ttm_bo_mem_space);
1125 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1126 struct ttm_placement *placement,
1131 struct ttm_mem_reg mem;
1132 struct ttm_bo_device *bdev = bo->bdev;
1134 BUG_ON(!ttm_bo_is_reserved(bo));
1137 * FIXME: It's possible to pipeline buffer moves.
1138 * Have the driver move function wait for idle when necessary,
1139 * instead of doing it here.
1141 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1142 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1143 lockmgr(&bdev->fence_lock, LK_RELEASE);
1146 mem.num_pages = bo->num_pages;
1147 mem.size = mem.num_pages << PAGE_SHIFT;
1148 mem.page_alignment = bo->mem.page_alignment;
1149 mem.bus.io_reserved_vm = false;
1150 mem.bus.io_reserved_count = 0;
1152 * Determine where to move the buffer.
1154 ret = ttm_bo_mem_space(bo, placement, &mem,
1155 interruptible, no_wait_gpu);
1158 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1159 interruptible, no_wait_gpu);
1161 if (ret && mem.mm_node)
1162 ttm_bo_mem_put(bo, &mem);
1166 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1167 struct ttm_mem_reg *mem)
1171 if (mem->mm_node && placement->lpfn != 0 &&
1172 (mem->start < placement->fpfn ||
1173 mem->start + mem->num_pages > placement->lpfn))
1176 for (i = 0; i < placement->num_placement; i++) {
1177 if ((placement->placement[i] & mem->placement &
1178 TTM_PL_MASK_CACHING) &&
1179 (placement->placement[i] & mem->placement &
1186 int ttm_bo_validate(struct ttm_buffer_object *bo,
1187 struct ttm_placement *placement,
1193 BUG_ON(!ttm_bo_is_reserved(bo));
1194 /* Check that range is valid */
1195 if (placement->lpfn || placement->fpfn)
1196 if (placement->fpfn > placement->lpfn ||
1197 (placement->lpfn - placement->fpfn) < bo->num_pages)
1200 * Check whether we need to move buffer.
1202 ret = ttm_bo_mem_compat(placement, &bo->mem);
1204 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1210 * Use the access and other non-mapping-related flag bits from
1211 * the compatible memory placement flags to the active flags
1213 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1214 ~TTM_PL_MASK_MEMTYPE);
1217 * We might need to add a TTM.
1219 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1220 ret = ttm_bo_add_ttm(bo, true);
1226 EXPORT_SYMBOL(ttm_bo_validate);
1228 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1229 struct ttm_placement *placement)
1231 BUG_ON((placement->fpfn || placement->lpfn) &&
1232 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1237 int ttm_bo_init(struct ttm_bo_device *bdev,
1238 struct ttm_buffer_object *bo,
1240 enum ttm_bo_type type,
1241 struct ttm_placement *placement,
1242 uint32_t page_alignment,
1244 struct vm_object *persistent_swap_storage,
1246 struct sg_table *sg,
1247 void (*destroy) (struct ttm_buffer_object *))
1250 unsigned long num_pages;
1251 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1253 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1255 kprintf("[TTM] Out of kernel memory\n");
1263 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1264 if (num_pages == 0) {
1265 kprintf("[TTM] Illegal buffer object size\n");
1270 ttm_mem_global_free(mem_glob, acc_size);
1273 bo->destroy = destroy;
1275 kref_init(&bo->kref);
1276 kref_init(&bo->list_kref);
1277 atomic_set(&bo->cpu_writers, 0);
1278 atomic_set(&bo->reserved, 1);
1279 init_waitqueue_head(&bo->event_queue);
1280 INIT_LIST_HEAD(&bo->lru);
1281 INIT_LIST_HEAD(&bo->ddestroy);
1282 INIT_LIST_HEAD(&bo->swap);
1283 INIT_LIST_HEAD(&bo->io_reserve_lru);
1284 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1286 bo->glob = bdev->glob;
1288 bo->num_pages = num_pages;
1289 bo->mem.size = num_pages << PAGE_SHIFT;
1290 bo->mem.mem_type = TTM_PL_SYSTEM;
1291 bo->mem.num_pages = bo->num_pages;
1292 bo->mem.mm_node = NULL;
1293 bo->mem.page_alignment = page_alignment;
1294 bo->mem.bus.io_reserved_vm = false;
1295 bo->mem.bus.io_reserved_count = 0;
1297 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1298 bo->seq_valid = false;
1299 bo->persistent_swap_storage = persistent_swap_storage;
1300 bo->acc_size = acc_size;
1302 atomic_inc(&bo->glob->bo_count);
1305 * Mirror ref from kref_init() for list_kref.
1307 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1309 ret = ttm_bo_check_placement(bo, placement);
1310 if (unlikely(ret != 0))
1314 * For ttm_bo_type_device buffers, allocate
1315 * address space from the device.
1317 if (bo->type == ttm_bo_type_device ||
1318 bo->type == ttm_bo_type_sg) {
1319 ret = ttm_bo_setup_vm(bo);
1324 ret = ttm_bo_validate(bo, placement, interruptible, false);
1328 ttm_bo_unreserve(bo);
1332 ttm_bo_unreserve(bo);
1337 EXPORT_SYMBOL(ttm_bo_init);
1339 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1340 unsigned long bo_size,
1341 unsigned struct_size)
1343 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1346 size += ttm_round_pot(struct_size);
1347 size += PAGE_ALIGN(npages * sizeof(void *));
1348 size += ttm_round_pot(sizeof(struct ttm_tt));
1351 EXPORT_SYMBOL(ttm_bo_acc_size);
1353 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1354 unsigned long bo_size,
1355 unsigned struct_size)
1357 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1360 size += ttm_round_pot(struct_size);
1361 size += PAGE_ALIGN(npages * sizeof(void *));
1362 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1363 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1366 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1368 int ttm_bo_create(struct ttm_bo_device *bdev,
1370 enum ttm_bo_type type,
1371 struct ttm_placement *placement,
1372 uint32_t page_alignment,
1374 struct vm_object *persistent_swap_storage,
1375 struct ttm_buffer_object **p_bo)
1377 struct ttm_buffer_object *bo;
1382 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1383 if (unlikely(bo == NULL))
1386 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1387 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1388 interruptible, persistent_swap_storage, acc_size,
1390 if (likely(ret == 0))
1395 EXPORT_SYMBOL(ttm_bo_create);
1397 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1398 unsigned mem_type, bool allow_errors)
1400 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1401 struct ttm_bo_global *glob = bdev->glob;
1405 * Can't use standard list traversal since we're unlocking.
1408 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1409 while (!list_empty(&man->lru)) {
1410 lockmgr(&glob->lru_lock, LK_RELEASE);
1411 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1416 kprintf("[TTM] Cleanup eviction failed\n");
1419 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1421 lockmgr(&glob->lru_lock, LK_RELEASE);
1425 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1427 struct ttm_mem_type_manager *man;
1430 if (mem_type >= TTM_NUM_MEM_TYPES) {
1431 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1434 man = &bdev->man[mem_type];
1436 if (!man->has_type) {
1437 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1442 man->use_type = false;
1443 man->has_type = false;
1447 ttm_bo_force_list_clean(bdev, mem_type, false);
1449 ret = (*man->func->takedown)(man);
1454 EXPORT_SYMBOL(ttm_bo_clean_mm);
1456 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1458 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1460 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1461 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1465 if (!man->has_type) {
1466 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1470 return ttm_bo_force_list_clean(bdev, mem_type, true);
1472 EXPORT_SYMBOL(ttm_bo_evict_mm);
1474 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1475 unsigned long p_size)
1478 struct ttm_mem_type_manager *man;
1480 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1481 man = &bdev->man[type];
1482 BUG_ON(man->has_type);
1483 man->io_reserve_fastpath = true;
1484 man->use_io_reserve_lru = false;
1485 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1486 INIT_LIST_HEAD(&man->io_reserve_lru);
1488 ret = bdev->driver->init_mem_type(bdev, type, man);
1494 if (type != TTM_PL_SYSTEM) {
1495 ret = (*man->func->init)(man, p_size);
1499 man->has_type = true;
1500 man->use_type = true;
1503 INIT_LIST_HEAD(&man->lru);
1507 EXPORT_SYMBOL(ttm_bo_init_mm);
1509 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1511 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1512 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1513 glob->dummy_read_page = NULL;
1515 vm_page_free(glob->dummy_read_page);
1519 void ttm_bo_global_release(struct drm_global_reference *ref)
1521 struct ttm_bo_global *glob = ref->object;
1523 if (refcount_release(&glob->kobj_ref))
1524 ttm_bo_global_kobj_release(glob);
1526 EXPORT_SYMBOL(ttm_bo_global_release);
1528 int ttm_bo_global_init(struct drm_global_reference *ref)
1530 struct ttm_bo_global_ref *bo_ref =
1531 container_of(ref, struct ttm_bo_global_ref, ref);
1532 struct ttm_bo_global *glob = ref->object;
1535 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1536 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1537 glob->mem_glob = bo_ref->mem_glob;
1538 glob->dummy_read_page = vm_page_alloc_contig(
1539 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1541 if (unlikely(glob->dummy_read_page == NULL)) {
1546 INIT_LIST_HEAD(&glob->swap_lru);
1547 INIT_LIST_HEAD(&glob->device_list);
1549 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1550 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1551 if (unlikely(ret != 0)) {
1552 kprintf("[TTM] Could not register buffer object swapout\n");
1556 atomic_set(&glob->bo_count, 0);
1558 refcount_init(&glob->kobj_ref, 1);
1562 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1563 glob->dummy_read_page = NULL;
1565 vm_page_free(glob->dummy_read_page);
1571 EXPORT_SYMBOL(ttm_bo_global_init);
1574 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1577 unsigned i = TTM_NUM_MEM_TYPES;
1578 struct ttm_mem_type_manager *man;
1579 struct ttm_bo_global *glob = bdev->glob;
1582 man = &bdev->man[i];
1583 if (man->has_type) {
1584 man->use_type = false;
1585 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1587 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1590 man->has_type = false;
1594 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1595 list_del(&bdev->device_list);
1596 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1598 cancel_delayed_work_sync(&bdev->wq);
1600 while (ttm_bo_delayed_delete(bdev, true))
1603 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1604 if (list_empty(&bdev->ddestroy))
1605 TTM_DEBUG("Delayed destroy list was clean\n");
1607 if (list_empty(&bdev->man[0].lru))
1608 TTM_DEBUG("Swap list was clean\n");
1609 lockmgr(&glob->lru_lock, LK_RELEASE);
1611 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1612 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1613 drm_mm_takedown(&bdev->addr_space_mm);
1614 lockmgr(&bdev->vm_lock, LK_RELEASE);
1618 EXPORT_SYMBOL(ttm_bo_device_release);
1620 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1621 struct ttm_bo_global *glob,
1622 struct ttm_bo_driver *driver,
1623 uint64_t file_page_offset,
1628 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1629 bdev->driver = driver;
1631 memset(bdev->man, 0, sizeof(bdev->man));
1634 * Initialize the system memory buffer type.
1635 * Other types need to be driver / IOCTL initialized.
1637 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1638 if (unlikely(ret != 0))
1641 bdev->addr_space_rb = RB_ROOT;
1642 drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1644 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1645 INIT_LIST_HEAD(&bdev->ddestroy);
1646 bdev->dev_mapping = NULL;
1648 bdev->need_dma32 = need_dma32;
1650 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1651 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1652 list_add_tail(&bdev->device_list, &glob->device_list);
1653 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1659 EXPORT_SYMBOL(ttm_bo_device_init);
1662 * buffer object vm functions.
1665 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1667 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1669 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1670 if (mem->mem_type == TTM_PL_SYSTEM)
1673 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1676 if (mem->placement & TTM_PL_FLAG_CACHED)
1682 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1685 ttm_bo_release_mmap(bo);
1686 ttm_mem_io_free_vm(bo);
1689 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1691 struct ttm_bo_device *bdev = bo->bdev;
1692 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1694 ttm_mem_io_lock(man, false);
1695 ttm_bo_unmap_virtual_locked(bo);
1696 ttm_mem_io_unlock(man);
1700 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1702 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1704 struct ttm_bo_device *bdev = bo->bdev;
1706 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1707 struct rb_node *parent = NULL;
1708 struct ttm_buffer_object *cur_bo;
1709 unsigned long offset = bo->vm_node->start;
1710 unsigned long cur_offset;
1714 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1715 cur_offset = cur_bo->vm_node->start;
1716 if (offset < cur_offset)
1717 cur = &parent->rb_left;
1718 else if (offset > cur_offset)
1719 cur = &parent->rb_right;
1724 rb_link_node(&bo->vm_rb, parent, cur);
1725 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1731 * @bo: the buffer to allocate address space for
1733 * Allocate address space in the drm device so that applications
1734 * can mmap the buffer and access the contents. This only
1735 * applies to ttm_bo_type_device objects as others are not
1736 * placed in the drm device address space.
1739 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1741 struct ttm_bo_device *bdev = bo->bdev;
1745 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1746 if (unlikely(ret != 0))
1749 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1750 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1751 bo->mem.num_pages, 0, 0);
1753 if (unlikely(bo->vm_node == NULL)) {
1758 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1759 bo->mem.num_pages, 0);
1761 if (unlikely(bo->vm_node == NULL)) {
1762 lockmgr(&bdev->vm_lock, LK_RELEASE);
1766 ttm_bo_vm_insert_rb(bo);
1767 lockmgr(&bdev->vm_lock, LK_RELEASE);
1768 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1772 lockmgr(&bdev->vm_lock, LK_RELEASE);
1776 int ttm_bo_wait(struct ttm_buffer_object *bo,
1777 bool lazy, bool interruptible, bool no_wait)
1779 struct ttm_bo_driver *driver = bo->bdev->driver;
1780 struct ttm_bo_device *bdev = bo->bdev;
1784 if (likely(bo->sync_obj == NULL))
1787 while (bo->sync_obj) {
1789 if (driver->sync_obj_signaled(bo->sync_obj)) {
1790 void *tmp_obj = bo->sync_obj;
1791 bo->sync_obj = NULL;
1792 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1793 lockmgr(&bdev->fence_lock, LK_RELEASE);
1794 driver->sync_obj_unref(&tmp_obj);
1795 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1802 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1803 lockmgr(&bdev->fence_lock, LK_RELEASE);
1804 ret = driver->sync_obj_wait(sync_obj,
1805 lazy, interruptible);
1806 if (unlikely(ret != 0)) {
1807 driver->sync_obj_unref(&sync_obj);
1808 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1811 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1812 if (likely(bo->sync_obj == sync_obj)) {
1813 void *tmp_obj = bo->sync_obj;
1814 bo->sync_obj = NULL;
1815 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1817 lockmgr(&bdev->fence_lock, LK_RELEASE);
1818 driver->sync_obj_unref(&sync_obj);
1819 driver->sync_obj_unref(&tmp_obj);
1820 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1822 lockmgr(&bdev->fence_lock, LK_RELEASE);
1823 driver->sync_obj_unref(&sync_obj);
1824 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1829 EXPORT_SYMBOL(ttm_bo_wait);
1831 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1833 struct ttm_bo_device *bdev = bo->bdev;
1837 * Using ttm_bo_reserve makes sure the lru lists are updated.
1840 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1841 if (unlikely(ret != 0))
1843 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1844 ret = ttm_bo_wait(bo, false, true, no_wait);
1845 lockmgr(&bdev->fence_lock, LK_RELEASE);
1846 if (likely(ret == 0))
1847 atomic_inc(&bo->cpu_writers);
1848 ttm_bo_unreserve(bo);
1851 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1853 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1855 atomic_dec(&bo->cpu_writers);
1857 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1860 * A buffer object shrink method that tries to swap out the first
1861 * buffer object on the bo_global::swap_lru list.
1864 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1866 struct ttm_bo_global *glob =
1867 container_of(shrink, struct ttm_bo_global, shrink);
1868 struct ttm_buffer_object *bo;
1871 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1873 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1874 list_for_each_entry(bo, &glob->swap_lru, swap) {
1875 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1881 lockmgr(&glob->lru_lock, LK_RELEASE);
1885 kref_get(&bo->list_kref);
1887 if (!list_empty(&bo->ddestroy)) {
1888 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1889 kref_put(&bo->list_kref, ttm_bo_release_list);
1893 put_count = ttm_bo_del_from_lru(bo);
1894 lockmgr(&glob->lru_lock, LK_RELEASE);
1896 ttm_bo_list_ref_sub(bo, put_count, true);
1899 * Wait for GPU, then move to system cached.
1902 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1903 ret = ttm_bo_wait(bo, false, false, false);
1904 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1906 if (unlikely(ret != 0))
1909 if ((bo->mem.placement & swap_placement) != swap_placement) {
1910 struct ttm_mem_reg evict_mem;
1912 evict_mem = bo->mem;
1913 evict_mem.mm_node = NULL;
1914 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1915 evict_mem.mem_type = TTM_PL_SYSTEM;
1917 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1919 if (unlikely(ret != 0))
1923 ttm_bo_unmap_virtual(bo);
1926 * Swap out. Buffer will be swapped in again as soon as
1927 * anyone tries to access a ttm page.
1930 if (bo->bdev->driver->swap_notify)
1931 bo->bdev->driver->swap_notify(bo);
1933 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1938 * Unreserve without putting on LRU to avoid swapping out an
1939 * already swapped buffer.
1942 ttm_bo_unreserve_core(bo);
1943 kref_put(&bo->list_kref, ttm_bo_release_list);
1947 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1949 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1952 EXPORT_SYMBOL(ttm_bo_swapout_all);