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>
30 * $FreeBSD: head/sys/dev/drm2/ttm/ttm_bo.c 248060 2013-03-08 18:11:02Z dumbbell $
33 #define pr_fmt(fmt) "[TTM] " fmt
35 #include <drm/ttm/ttm_module.h>
36 #include <drm/ttm/ttm_bo_driver.h>
37 #include <drm/ttm/ttm_placement.h>
38 #include <linux/atomic.h>
39 #include <linux/export.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,
151 while (ttm_bo_is_reserved(bo)) {
152 ret = -lksleep(bo, &bo->glob->lru_lock, 0, wmsg, 0);
159 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
161 struct ttm_bo_device *bdev = bo->bdev;
162 struct ttm_mem_type_manager *man;
164 BUG_ON(!ttm_bo_is_reserved(bo));
166 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
168 BUG_ON(!list_empty(&bo->lru));
170 man = &bdev->man[bo->mem.mem_type];
171 list_add_tail(&bo->lru, &man->lru);
172 kref_get(&bo->list_kref);
174 if (bo->ttm != NULL) {
175 list_add_tail(&bo->swap, &bo->glob->swap_lru);
176 kref_get(&bo->list_kref);
181 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
185 if (!list_empty(&bo->swap)) {
186 list_del_init(&bo->swap);
189 if (!list_empty(&bo->lru)) {
190 list_del_init(&bo->lru);
195 * TODO: Add a driver hook to delete from
196 * driver-specific LRU's here.
202 int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
204 bool no_wait, bool use_sequence, uint32_t sequence)
208 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
210 * Deadlock avoidance for multi-bo reserving.
212 if (use_sequence && bo->seq_valid) {
214 * We've already reserved this one.
216 if (unlikely(sequence == bo->val_seq))
219 * Already reserved by a thread that will not back
220 * off for us. We need to back off.
222 if (unlikely(sequence - bo->val_seq < (1U << 31)))
229 ret = ttm_bo_wait_unreserved(bo, interruptible);
236 bool wake_up = false;
238 * Wake up waiters that may need to recheck for deadlock,
239 * if we decreased the sequence number.
241 if (unlikely((bo->val_seq - sequence < (1U << 31))
246 * In the worst case with memory ordering these values can be
247 * seen in the wrong order. However since we call wake_up_all
248 * in that case, this will hopefully not pose a problem,
249 * and the worst case would only cause someone to accidentally
250 * hit -EAGAIN in ttm_bo_reserve when they see old value of
251 * val_seq. However this would only happen if seq_valid was
252 * written before val_seq was, and just means some slightly
253 * increased cpu usage
255 bo->val_seq = sequence;
256 bo->seq_valid = true;
258 wake_up_all(&bo->event_queue);
260 bo->seq_valid = false;
265 EXPORT_SYMBOL(ttm_bo_reserve);
267 static void ttm_bo_ref_bug(struct kref *list_kref)
272 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
275 kref_sub(&bo->list_kref, count,
276 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
279 int ttm_bo_reserve(struct ttm_buffer_object *bo,
281 bool no_wait, bool use_sequence, uint32_t sequence)
283 struct ttm_bo_global *glob = bo->glob;
287 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
288 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_sequence,
290 if (likely(ret == 0)) {
291 put_count = ttm_bo_del_from_lru(bo);
292 lockmgr(&glob->lru_lock, LK_RELEASE);
293 ttm_bo_list_ref_sub(bo, put_count, true);
295 lockmgr(&glob->lru_lock, LK_RELEASE);
302 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
303 bool interruptible, uint32_t sequence)
305 bool wake_up = false;
308 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
309 WARN_ON(bo->seq_valid && sequence == bo->val_seq);
311 ret = ttm_bo_wait_unreserved(bo, interruptible);
317 if ((bo->val_seq - sequence < (1U << 31)) || !bo->seq_valid)
321 * Wake up waiters that may need to recheck for deadlock,
322 * if we decreased the sequence number.
324 bo->val_seq = sequence;
325 bo->seq_valid = true;
327 wake_up_all(&bo->event_queue);
332 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
333 bool interruptible, uint32_t sequence)
335 struct ttm_bo_global *glob = bo->glob;
338 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
339 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence);
341 put_count = ttm_bo_del_from_lru(bo);
342 lockmgr(&glob->lru_lock, LK_RELEASE);
343 ttm_bo_list_ref_sub(bo, put_count, true);
345 lockmgr(&glob->lru_lock, LK_RELEASE);
349 EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
351 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
353 ttm_bo_add_to_lru(bo);
354 atomic_set(&bo->reserved, 0);
355 wake_up_all(&bo->event_queue);
358 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
360 struct ttm_bo_global *glob = bo->glob;
362 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
363 ttm_bo_unreserve_locked(bo);
364 lockmgr(&glob->lru_lock, LK_RELEASE);
366 EXPORT_SYMBOL(ttm_bo_unreserve);
369 * Call bo->mutex locked.
371 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
373 struct ttm_bo_device *bdev = bo->bdev;
374 struct ttm_bo_global *glob = bo->glob;
376 uint32_t page_flags = 0;
378 TTM_ASSERT_LOCKED(&bo->mutex);
381 if (bdev->need_dma32)
382 page_flags |= TTM_PAGE_FLAG_DMA32;
385 case ttm_bo_type_device:
387 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
388 case ttm_bo_type_kernel:
389 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
390 page_flags, glob->dummy_read_page);
391 if (unlikely(bo->ttm == NULL))
395 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
396 page_flags | TTM_PAGE_FLAG_SG,
397 glob->dummy_read_page);
398 if (unlikely(bo->ttm == NULL)) {
402 bo->ttm->sg = bo->sg;
405 kprintf("[TTM] Illegal buffer object type\n");
413 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
414 struct ttm_mem_reg *mem,
415 bool evict, bool interruptible,
418 struct ttm_bo_device *bdev = bo->bdev;
419 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
420 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
421 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
422 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
425 if (old_is_pci || new_is_pci ||
426 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
427 ret = ttm_mem_io_lock(old_man, true);
428 if (unlikely(ret != 0))
430 ttm_bo_unmap_virtual_locked(bo);
431 ttm_mem_io_unlock(old_man);
435 * Create and bind a ttm if required.
438 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
439 if (bo->ttm == NULL) {
440 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
441 ret = ttm_bo_add_ttm(bo, zero);
446 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
450 if (mem->mem_type != TTM_PL_SYSTEM) {
451 ret = ttm_tt_bind(bo->ttm, mem);
456 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
457 if (bdev->driver->move_notify)
458 bdev->driver->move_notify(bo, mem);
465 if (bdev->driver->move_notify)
466 bdev->driver->move_notify(bo, mem);
468 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
469 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
470 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
471 else if (bdev->driver->move)
472 ret = bdev->driver->move(bo, evict, interruptible,
475 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
478 if (bdev->driver->move_notify) {
479 struct ttm_mem_reg tmp_mem = *mem;
482 bdev->driver->move_notify(bo, mem);
492 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
494 kprintf("[TTM] Can not flush read caches\n");
498 if (bo->mem.mm_node) {
499 bo->offset = (bo->mem.start << PAGE_SHIFT) +
500 bdev->man[bo->mem.mem_type].gpu_offset;
501 bo->cur_placement = bo->mem.placement;
508 new_man = &bdev->man[bo->mem.mem_type];
509 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
510 ttm_tt_unbind(bo->ttm);
511 ttm_tt_destroy(bo->ttm);
520 * Will release GPU memory type usage on destruction.
521 * This is the place to put in driver specific hooks to release
522 * driver private resources.
523 * Will release the bo::reserved lock.
526 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
528 if (bo->bdev->driver->move_notify)
529 bo->bdev->driver->move_notify(bo, NULL);
532 ttm_tt_unbind(bo->ttm);
533 ttm_tt_destroy(bo->ttm);
536 ttm_bo_mem_put(bo, &bo->mem);
538 atomic_set(&bo->reserved, 0);
539 wake_up_all(&bo->event_queue);
542 * Since the final reference to this bo may not be dropped by
543 * the current task we have to put a memory barrier here to make
544 * sure the changes done in this function are always visible.
546 * This function only needs protection against the final kref_put.
551 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
553 struct ttm_bo_device *bdev = bo->bdev;
554 struct ttm_bo_global *glob = bo->glob;
555 struct ttm_bo_driver *driver = bdev->driver;
556 void *sync_obj = NULL;
560 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
561 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
563 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
564 (void) ttm_bo_wait(bo, false, false, true);
565 if (!ret && !bo->sync_obj) {
566 lockmgr(&bdev->fence_lock, LK_RELEASE);
567 put_count = ttm_bo_del_from_lru(bo);
569 lockmgr(&glob->lru_lock, LK_RELEASE);
570 ttm_bo_cleanup_memtype_use(bo);
572 ttm_bo_list_ref_sub(bo, put_count, true);
577 sync_obj = driver->sync_obj_ref(bo->sync_obj);
578 lockmgr(&bdev->fence_lock, LK_RELEASE);
581 atomic_set(&bo->reserved, 0);
582 wake_up_all(&bo->event_queue);
585 kref_get(&bo->list_kref);
586 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
587 lockmgr(&glob->lru_lock, LK_RELEASE);
590 driver->sync_obj_flush(sync_obj);
591 driver->sync_obj_unref(&sync_obj);
593 schedule_delayed_work(&bdev->wq,
594 ((hz / 100) < 1) ? 1 : hz / 100);
598 * function ttm_bo_cleanup_refs_and_unlock
599 * If bo idle, remove from delayed- and lru lists, and unref.
600 * If not idle, do nothing.
602 * Must be called with lru_lock and reservation held, this function
603 * will drop both before returning.
605 * @interruptible Any sleeps should occur interruptibly.
606 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
609 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
613 struct ttm_bo_device *bdev = bo->bdev;
614 struct ttm_bo_driver *driver = bdev->driver;
615 struct ttm_bo_global *glob = bo->glob;
619 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
620 ret = ttm_bo_wait(bo, false, false, true);
622 if (ret && !no_wait_gpu) {
626 * Take a reference to the fence and unreserve,
627 * at this point the buffer should be dead, so
628 * no new sync objects can be attached.
630 sync_obj = driver->sync_obj_ref(bo->sync_obj);
631 lockmgr(&bdev->fence_lock, LK_RELEASE);
633 atomic_set(&bo->reserved, 0);
634 wake_up_all(&bo->event_queue);
635 lockmgr(&glob->lru_lock, LK_RELEASE);
637 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
638 driver->sync_obj_unref(&sync_obj);
643 * remove sync_obj with ttm_bo_wait, the wait should be
644 * finished, and no new wait object should have been added.
646 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
647 ret = ttm_bo_wait(bo, false, false, true);
649 lockmgr(&bdev->fence_lock, LK_RELEASE);
653 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
654 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
657 * We raced, and lost, someone else holds the reservation now,
658 * and is probably busy in ttm_bo_cleanup_memtype_use.
660 * Even if it's not the case, because we finished waiting any
661 * delayed destruction would succeed, so just return success
665 lockmgr(&glob->lru_lock, LK_RELEASE);
669 lockmgr(&bdev->fence_lock, LK_RELEASE);
671 if (ret || unlikely(list_empty(&bo->ddestroy))) {
672 atomic_set(&bo->reserved, 0);
673 wake_up_all(&bo->event_queue);
674 lockmgr(&glob->lru_lock, LK_RELEASE);
678 put_count = ttm_bo_del_from_lru(bo);
679 list_del_init(&bo->ddestroy);
682 lockmgr(&glob->lru_lock, LK_RELEASE);
683 ttm_bo_cleanup_memtype_use(bo);
685 ttm_bo_list_ref_sub(bo, put_count, true);
691 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
692 * encountered buffers.
695 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
697 struct ttm_bo_global *glob = bdev->glob;
698 struct ttm_buffer_object *entry = NULL;
701 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
702 if (list_empty(&bdev->ddestroy))
705 entry = list_first_entry(&bdev->ddestroy,
706 struct ttm_buffer_object, ddestroy);
707 kref_get(&entry->list_kref);
710 struct ttm_buffer_object *nentry = NULL;
712 if (entry->ddestroy.next != &bdev->ddestroy) {
713 nentry = list_first_entry(&entry->ddestroy,
714 struct ttm_buffer_object, ddestroy);
715 kref_get(&nentry->list_kref);
718 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
719 if (remove_all && ret) {
720 ret = ttm_bo_reserve_nolru(entry, false, false,
725 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
728 lockmgr(&glob->lru_lock, LK_RELEASE);
730 kref_put(&entry->list_kref, ttm_bo_release_list);
736 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
737 if (list_empty(&entry->ddestroy))
742 lockmgr(&glob->lru_lock, LK_RELEASE);
745 kref_put(&entry->list_kref, ttm_bo_release_list);
749 static void ttm_bo_delayed_workqueue(struct work_struct *work)
751 struct ttm_bo_device *bdev =
752 container_of(work, struct ttm_bo_device, wq.work);
754 if (ttm_bo_delayed_delete(bdev, false)) {
755 schedule_delayed_work(&bdev->wq,
756 ((hz / 100) < 1) ? 1 : hz / 100);
761 * NOTE: bdev->vm_lock already held on call, this function release it.
763 static void ttm_bo_release(struct kref *kref)
765 struct ttm_buffer_object *bo =
766 container_of(kref, struct ttm_buffer_object, kref);
767 struct ttm_bo_device *bdev = bo->bdev;
768 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
771 if (atomic_read(&bo->kref.refcount) > 0) {
772 lockmgr(&bdev->vm_lock, LK_RELEASE);
775 if (likely(bo->vm_node != NULL)) {
776 RB_REMOVE(ttm_bo_device_buffer_objects,
777 &bdev->addr_space_rb, bo);
778 drm_mm_put_block(bo->vm_node);
783 * Should we clean up our implied list_kref? Because ttm_bo_release()
784 * can be called reentrantly due to races (this may not be true any
785 * more with the lock management changes in the deref), it is possible
786 * to get here twice, but there's only one list_kref ref to drop and
787 * in the other path 'bo' can be kfree()d by another thread the
788 * instant we release our lock.
790 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
791 if (release_active) {
792 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
793 lockmgr(&bdev->vm_lock, LK_RELEASE);
794 ttm_mem_io_lock(man, false);
795 ttm_mem_io_free_vm(bo);
796 ttm_mem_io_unlock(man);
797 ttm_bo_cleanup_refs_or_queue(bo);
798 kref_put(&bo->list_kref, ttm_bo_release_list);
800 lockmgr(&bdev->vm_lock, LK_RELEASE);
804 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
806 struct ttm_buffer_object *bo = *p_bo;
807 struct ttm_bo_device *bdev = bo->bdev;
810 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
811 if (kref_put(&bo->kref, ttm_bo_release) == 0)
812 lockmgr(&bdev->vm_lock, LK_RELEASE);
814 EXPORT_SYMBOL(ttm_bo_unref);
816 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
818 return cancel_delayed_work_sync(&bdev->wq);
820 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
822 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
825 schedule_delayed_work(&bdev->wq,
826 ((hz / 100) < 1) ? 1 : hz / 100);
828 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
830 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
833 struct ttm_bo_device *bdev = bo->bdev;
834 struct ttm_mem_reg evict_mem;
835 struct ttm_placement placement;
838 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
839 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
840 lockmgr(&bdev->fence_lock, LK_RELEASE);
842 if (unlikely(ret != 0)) {
843 if (ret != -ERESTART) {
844 kprintf("[TTM] Failed to expire sync object before buffer eviction\n");
849 BUG_ON(!ttm_bo_is_reserved(bo));
852 evict_mem.mm_node = NULL;
853 evict_mem.bus.io_reserved_vm = false;
854 evict_mem.bus.io_reserved_count = 0;
858 placement.num_placement = 0;
859 placement.num_busy_placement = 0;
860 bdev->driver->evict_flags(bo, &placement);
861 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
864 if (ret != -ERESTART) {
865 kprintf("[TTM] Failed to find memory space for buffer 0x%p eviction\n",
867 ttm_bo_mem_space_debug(bo, &placement);
872 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
875 if (ret != -ERESTART)
876 kprintf("[TTM] Buffer eviction failed\n");
877 ttm_bo_mem_put(bo, &evict_mem);
885 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
890 struct ttm_bo_global *glob = bdev->glob;
891 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
892 struct ttm_buffer_object *bo;
893 int ret = -EBUSY, put_count;
895 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
896 list_for_each_entry(bo, &man->lru, lru) {
897 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
903 lockmgr(&glob->lru_lock, LK_RELEASE);
907 kref_get(&bo->list_kref);
909 if (!list_empty(&bo->ddestroy)) {
910 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
912 kref_put(&bo->list_kref, ttm_bo_release_list);
916 put_count = ttm_bo_del_from_lru(bo);
917 lockmgr(&glob->lru_lock, LK_RELEASE);
921 ttm_bo_list_ref_sub(bo, put_count, true);
923 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
924 ttm_bo_unreserve(bo);
926 kref_put(&bo->list_kref, ttm_bo_release_list);
930 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
932 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
935 (*man->func->put_node)(man, mem);
937 EXPORT_SYMBOL(ttm_bo_mem_put);
940 * Repeatedly evict memory from the LRU for @mem_type until we create enough
941 * space, or we've evicted everything and there isn't enough space.
943 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
945 struct ttm_placement *placement,
946 struct ttm_mem_reg *mem,
950 struct ttm_bo_device *bdev = bo->bdev;
951 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
955 ret = (*man->func->get_node)(man, bo, placement, mem);
956 if (unlikely(ret != 0))
960 ret = ttm_mem_evict_first(bdev, mem_type,
961 interruptible, no_wait_gpu);
962 if (unlikely(ret != 0))
965 if (mem->mm_node == NULL)
967 mem->mem_type = mem_type;
971 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
972 uint32_t cur_placement,
973 uint32_t proposed_placement)
975 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
976 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
979 * Keep current caching if possible.
982 if ((cur_placement & caching) != 0)
983 result |= (cur_placement & caching);
984 else if ((man->default_caching & caching) != 0)
985 result |= man->default_caching;
986 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
987 result |= TTM_PL_FLAG_CACHED;
988 else if ((TTM_PL_FLAG_WC & caching) != 0)
989 result |= TTM_PL_FLAG_WC;
990 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
991 result |= TTM_PL_FLAG_UNCACHED;
996 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
998 uint32_t proposed_placement,
999 uint32_t *masked_placement)
1001 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
1003 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
1006 if ((proposed_placement & man->available_caching) == 0)
1009 cur_flags |= (proposed_placement & man->available_caching);
1011 *masked_placement = cur_flags;
1016 * Creates space for memory region @mem according to its type.
1018 * This function first searches for free space in compatible memory types in
1019 * the priority order defined by the driver. If free space isn't found, then
1020 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1023 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1024 struct ttm_placement *placement,
1025 struct ttm_mem_reg *mem,
1029 struct ttm_bo_device *bdev = bo->bdev;
1030 struct ttm_mem_type_manager *man;
1031 uint32_t mem_type = TTM_PL_SYSTEM;
1032 uint32_t cur_flags = 0;
1033 bool type_found = false;
1034 bool type_ok = false;
1035 bool has_erestartsys = false;
1038 mem->mm_node = NULL;
1039 for (i = 0; i < placement->num_placement; ++i) {
1040 ret = ttm_mem_type_from_flags(placement->placement[i],
1044 man = &bdev->man[mem_type];
1046 type_ok = ttm_bo_mt_compatible(man,
1048 placement->placement[i],
1054 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1057 * Use the access and other non-mapping-related flag bits from
1058 * the memory placement flags to the current flags
1060 ttm_flag_masked(&cur_flags, placement->placement[i],
1061 ~TTM_PL_MASK_MEMTYPE);
1063 if (mem_type == TTM_PL_SYSTEM)
1066 if (man->has_type && man->use_type) {
1068 ret = (*man->func->get_node)(man, bo, placement, mem);
1076 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1077 mem->mem_type = mem_type;
1078 mem->placement = cur_flags;
1085 for (i = 0; i < placement->num_busy_placement; ++i) {
1086 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1090 man = &bdev->man[mem_type];
1093 if (!ttm_bo_mt_compatible(man,
1095 placement->busy_placement[i],
1099 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1102 * Use the access and other non-mapping-related flag bits from
1103 * the memory placement flags to the current flags
1105 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1106 ~TTM_PL_MASK_MEMTYPE);
1109 if (mem_type == TTM_PL_SYSTEM) {
1110 mem->mem_type = mem_type;
1111 mem->placement = cur_flags;
1112 mem->mm_node = NULL;
1116 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1117 interruptible, no_wait_gpu);
1118 if (ret == 0 && mem->mm_node) {
1119 mem->placement = cur_flags;
1122 if (ret == -ERESTART)
1123 has_erestartsys = true;
1125 ret = (has_erestartsys) ? -ERESTART : -ENOMEM;
1128 EXPORT_SYMBOL(ttm_bo_mem_space);
1131 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1132 struct ttm_placement *placement,
1137 struct ttm_mem_reg mem;
1138 struct ttm_bo_device *bdev = bo->bdev;
1140 BUG_ON(!ttm_bo_is_reserved(bo));
1143 * FIXME: It's possible to pipeline buffer moves.
1144 * Have the driver move function wait for idle when necessary,
1145 * instead of doing it here.
1147 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1148 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1149 lockmgr(&bdev->fence_lock, LK_RELEASE);
1152 mem.num_pages = bo->num_pages;
1153 mem.size = mem.num_pages << PAGE_SHIFT;
1154 mem.page_alignment = bo->mem.page_alignment;
1155 mem.bus.io_reserved_vm = false;
1156 mem.bus.io_reserved_count = 0;
1158 * Determine where to move the buffer.
1160 ret = ttm_bo_mem_space(bo, placement, &mem,
1161 interruptible, no_wait_gpu);
1164 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1165 interruptible, no_wait_gpu);
1167 if (ret && mem.mm_node)
1168 ttm_bo_mem_put(bo, &mem);
1172 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1173 struct ttm_mem_reg *mem)
1177 if (mem->mm_node && placement->lpfn != 0 &&
1178 (mem->start < placement->fpfn ||
1179 mem->start + mem->num_pages > placement->lpfn))
1182 for (i = 0; i < placement->num_placement; i++) {
1183 if ((placement->placement[i] & mem->placement &
1184 TTM_PL_MASK_CACHING) &&
1185 (placement->placement[i] & mem->placement &
1192 int ttm_bo_validate(struct ttm_buffer_object *bo,
1193 struct ttm_placement *placement,
1199 BUG_ON(!ttm_bo_is_reserved(bo));
1200 /* Check that range is valid */
1201 if (placement->lpfn || placement->fpfn)
1202 if (placement->fpfn > placement->lpfn ||
1203 (placement->lpfn - placement->fpfn) < bo->num_pages)
1206 * Check whether we need to move buffer.
1208 ret = ttm_bo_mem_compat(placement, &bo->mem);
1210 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1216 * Use the access and other non-mapping-related flag bits from
1217 * the compatible memory placement flags to the active flags
1219 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1220 ~TTM_PL_MASK_MEMTYPE);
1223 * We might need to add a TTM.
1225 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1226 ret = ttm_bo_add_ttm(bo, true);
1232 EXPORT_SYMBOL(ttm_bo_validate);
1234 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1235 struct ttm_placement *placement)
1237 BUG_ON((placement->fpfn || placement->lpfn) &&
1238 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1243 int ttm_bo_init(struct ttm_bo_device *bdev,
1244 struct ttm_buffer_object *bo,
1246 enum ttm_bo_type type,
1247 struct ttm_placement *placement,
1248 uint32_t page_alignment,
1250 struct vm_object *persistent_swap_storage,
1252 struct sg_table *sg,
1253 void (*destroy) (struct ttm_buffer_object *))
1256 unsigned long num_pages;
1257 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1259 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1261 kprintf("[TTM] Out of kernel memory\n");
1269 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1270 if (num_pages == 0) {
1271 kprintf("[TTM] Illegal buffer object size\n");
1276 ttm_mem_global_free(mem_glob, acc_size);
1279 bo->destroy = destroy;
1281 kref_init(&bo->kref);
1282 kref_init(&bo->list_kref);
1283 atomic_set(&bo->cpu_writers, 0);
1284 atomic_set(&bo->reserved, 1);
1285 init_waitqueue_head(&bo->event_queue);
1286 INIT_LIST_HEAD(&bo->lru);
1287 INIT_LIST_HEAD(&bo->ddestroy);
1288 INIT_LIST_HEAD(&bo->swap);
1289 INIT_LIST_HEAD(&bo->io_reserve_lru);
1290 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1292 bo->glob = bdev->glob;
1294 bo->num_pages = num_pages;
1295 bo->mem.size = num_pages << PAGE_SHIFT;
1296 bo->mem.mem_type = TTM_PL_SYSTEM;
1297 bo->mem.num_pages = bo->num_pages;
1298 bo->mem.mm_node = NULL;
1299 bo->mem.page_alignment = page_alignment;
1300 bo->mem.bus.io_reserved_vm = false;
1301 bo->mem.bus.io_reserved_count = 0;
1303 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1304 bo->seq_valid = false;
1305 bo->persistent_swap_storage = persistent_swap_storage;
1306 bo->acc_size = acc_size;
1308 atomic_inc(&bo->glob->bo_count);
1311 * Mirror ref from kref_init() for list_kref.
1313 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1315 ret = ttm_bo_check_placement(bo, placement);
1316 if (unlikely(ret != 0))
1320 * For ttm_bo_type_device buffers, allocate
1321 * address space from the device.
1323 if (bo->type == ttm_bo_type_device ||
1324 bo->type == ttm_bo_type_sg) {
1325 ret = ttm_bo_setup_vm(bo);
1330 ret = ttm_bo_validate(bo, placement, interruptible, false);
1334 ttm_bo_unreserve(bo);
1338 ttm_bo_unreserve(bo);
1343 EXPORT_SYMBOL(ttm_bo_init);
1345 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1346 unsigned long bo_size,
1347 unsigned struct_size)
1349 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1352 size += ttm_round_pot(struct_size);
1353 size += PAGE_ALIGN(npages * sizeof(void *));
1354 size += ttm_round_pot(sizeof(struct ttm_tt));
1357 EXPORT_SYMBOL(ttm_bo_acc_size);
1359 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1360 unsigned long bo_size,
1361 unsigned struct_size)
1363 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1366 size += ttm_round_pot(struct_size);
1367 size += PAGE_ALIGN(npages * sizeof(void *));
1368 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1369 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1372 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1374 int ttm_bo_create(struct ttm_bo_device *bdev,
1376 enum ttm_bo_type type,
1377 struct ttm_placement *placement,
1378 uint32_t page_alignment,
1380 struct vm_object *persistent_swap_storage,
1381 struct ttm_buffer_object **p_bo)
1383 struct ttm_buffer_object *bo;
1388 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1389 if (unlikely(bo == NULL))
1392 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1393 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1394 interruptible, persistent_swap_storage, acc_size,
1396 if (likely(ret == 0))
1401 EXPORT_SYMBOL(ttm_bo_create);
1403 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1404 unsigned mem_type, bool allow_errors)
1406 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1407 struct ttm_bo_global *glob = bdev->glob;
1411 * Can't use standard list traversal since we're unlocking.
1414 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1415 while (!list_empty(&man->lru)) {
1416 lockmgr(&glob->lru_lock, LK_RELEASE);
1417 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1422 kprintf("[TTM] Cleanup eviction failed\n");
1425 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1427 lockmgr(&glob->lru_lock, LK_RELEASE);
1431 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1433 struct ttm_mem_type_manager *man;
1436 if (mem_type >= TTM_NUM_MEM_TYPES) {
1437 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1440 man = &bdev->man[mem_type];
1442 if (!man->has_type) {
1443 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1448 man->use_type = false;
1449 man->has_type = false;
1453 ttm_bo_force_list_clean(bdev, mem_type, false);
1455 ret = (*man->func->takedown)(man);
1460 EXPORT_SYMBOL(ttm_bo_clean_mm);
1462 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1464 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1466 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1467 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1471 if (!man->has_type) {
1472 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1476 return ttm_bo_force_list_clean(bdev, mem_type, true);
1478 EXPORT_SYMBOL(ttm_bo_evict_mm);
1480 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1481 unsigned long p_size)
1484 struct ttm_mem_type_manager *man;
1486 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1487 man = &bdev->man[type];
1488 BUG_ON(man->has_type);
1489 man->io_reserve_fastpath = true;
1490 man->use_io_reserve_lru = false;
1491 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1492 INIT_LIST_HEAD(&man->io_reserve_lru);
1494 ret = bdev->driver->init_mem_type(bdev, type, man);
1500 if (type != TTM_PL_SYSTEM) {
1501 ret = (*man->func->init)(man, p_size);
1505 man->has_type = true;
1506 man->use_type = true;
1509 INIT_LIST_HEAD(&man->lru);
1513 EXPORT_SYMBOL(ttm_bo_init_mm);
1515 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1517 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1518 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1519 glob->dummy_read_page = NULL;
1521 vm_page_free(glob->dummy_read_page);
1525 void ttm_bo_global_release(struct drm_global_reference *ref)
1527 struct ttm_bo_global *glob = ref->object;
1529 if (refcount_release(&glob->kobj_ref))
1530 ttm_bo_global_kobj_release(glob);
1532 EXPORT_SYMBOL(ttm_bo_global_release);
1534 int ttm_bo_global_init(struct drm_global_reference *ref)
1536 struct ttm_bo_global_ref *bo_ref =
1537 container_of(ref, struct ttm_bo_global_ref, ref);
1538 struct ttm_bo_global *glob = ref->object;
1541 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1542 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1543 glob->mem_glob = bo_ref->mem_glob;
1544 glob->dummy_read_page = vm_page_alloc_contig(
1545 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1547 if (unlikely(glob->dummy_read_page == NULL)) {
1552 INIT_LIST_HEAD(&glob->swap_lru);
1553 INIT_LIST_HEAD(&glob->device_list);
1555 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1556 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1557 if (unlikely(ret != 0)) {
1558 kprintf("[TTM] Could not register buffer object swapout\n");
1562 atomic_set(&glob->bo_count, 0);
1564 refcount_init(&glob->kobj_ref, 1);
1568 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1569 glob->dummy_read_page = NULL;
1571 vm_page_free(glob->dummy_read_page);
1577 EXPORT_SYMBOL(ttm_bo_global_init);
1580 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1583 unsigned i = TTM_NUM_MEM_TYPES;
1584 struct ttm_mem_type_manager *man;
1585 struct ttm_bo_global *glob = bdev->glob;
1588 man = &bdev->man[i];
1589 if (man->has_type) {
1590 man->use_type = false;
1591 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1593 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1596 man->has_type = false;
1600 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1601 list_del(&bdev->device_list);
1602 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1604 cancel_delayed_work_sync(&bdev->wq);
1606 while (ttm_bo_delayed_delete(bdev, true))
1609 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1610 if (list_empty(&bdev->ddestroy))
1611 TTM_DEBUG("Delayed destroy list was clean\n");
1613 if (list_empty(&bdev->man[0].lru))
1614 TTM_DEBUG("Swap list was clean\n");
1615 lockmgr(&glob->lru_lock, LK_RELEASE);
1617 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1618 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1619 drm_mm_takedown(&bdev->addr_space_mm);
1620 lockmgr(&bdev->vm_lock, LK_RELEASE);
1624 EXPORT_SYMBOL(ttm_bo_device_release);
1626 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1627 struct ttm_bo_global *glob,
1628 struct ttm_bo_driver *driver,
1629 uint64_t file_page_offset,
1634 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1635 bdev->driver = driver;
1637 memset(bdev->man, 0, sizeof(bdev->man));
1640 * Initialize the system memory buffer type.
1641 * Other types need to be driver / IOCTL initialized.
1643 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1644 if (unlikely(ret != 0))
1647 RB_INIT(&bdev->addr_space_rb);
1648 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1649 if (unlikely(ret != 0))
1650 goto out_no_addr_mm;
1652 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1653 INIT_LIST_HEAD(&bdev->ddestroy);
1654 bdev->dev_mapping = NULL;
1656 bdev->need_dma32 = need_dma32;
1658 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1659 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1660 list_add_tail(&bdev->device_list, &glob->device_list);
1661 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1665 ttm_bo_clean_mm(bdev, 0);
1669 EXPORT_SYMBOL(ttm_bo_device_init);
1672 * buffer object vm functions.
1675 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1677 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1679 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1680 if (mem->mem_type == TTM_PL_SYSTEM)
1683 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1686 if (mem->placement & TTM_PL_FLAG_CACHED)
1692 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1695 ttm_bo_release_mmap(bo);
1696 ttm_mem_io_free_vm(bo);
1699 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1701 struct ttm_bo_device *bdev = bo->bdev;
1702 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1704 ttm_mem_io_lock(man, false);
1705 ttm_bo_unmap_virtual_locked(bo);
1706 ttm_mem_io_unlock(man);
1710 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1712 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1714 struct ttm_bo_device *bdev = bo->bdev;
1716 /* The caller acquired bdev->vm_lock. */
1717 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1723 * @bo: the buffer to allocate address space for
1725 * Allocate address space in the drm device so that applications
1726 * can mmap the buffer and access the contents. This only
1727 * applies to ttm_bo_type_device objects as others are not
1728 * placed in the drm device address space.
1731 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1733 struct ttm_bo_device *bdev = bo->bdev;
1737 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1738 if (unlikely(ret != 0))
1741 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1742 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1743 bo->mem.num_pages, 0, 0);
1745 if (unlikely(bo->vm_node == NULL)) {
1750 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1751 bo->mem.num_pages, 0);
1753 if (unlikely(bo->vm_node == NULL)) {
1754 lockmgr(&bdev->vm_lock, LK_RELEASE);
1758 ttm_bo_vm_insert_rb(bo);
1759 lockmgr(&bdev->vm_lock, LK_RELEASE);
1760 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1764 lockmgr(&bdev->vm_lock, LK_RELEASE);
1768 int ttm_bo_wait(struct ttm_buffer_object *bo,
1769 bool lazy, bool interruptible, bool no_wait)
1771 struct ttm_bo_driver *driver = bo->bdev->driver;
1772 struct ttm_bo_device *bdev = bo->bdev;
1776 if (likely(bo->sync_obj == NULL))
1779 while (bo->sync_obj) {
1781 if (driver->sync_obj_signaled(bo->sync_obj)) {
1782 void *tmp_obj = bo->sync_obj;
1783 bo->sync_obj = NULL;
1784 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1785 lockmgr(&bdev->fence_lock, LK_RELEASE);
1786 driver->sync_obj_unref(&tmp_obj);
1787 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1794 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1795 lockmgr(&bdev->fence_lock, LK_RELEASE);
1796 ret = driver->sync_obj_wait(sync_obj,
1797 lazy, interruptible);
1798 if (unlikely(ret != 0)) {
1799 driver->sync_obj_unref(&sync_obj);
1800 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1803 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1804 if (likely(bo->sync_obj == sync_obj)) {
1805 void *tmp_obj = bo->sync_obj;
1806 bo->sync_obj = NULL;
1807 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1809 lockmgr(&bdev->fence_lock, LK_RELEASE);
1810 driver->sync_obj_unref(&sync_obj);
1811 driver->sync_obj_unref(&tmp_obj);
1812 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1814 lockmgr(&bdev->fence_lock, LK_RELEASE);
1815 driver->sync_obj_unref(&sync_obj);
1816 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1821 EXPORT_SYMBOL(ttm_bo_wait);
1823 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1825 struct ttm_bo_device *bdev = bo->bdev;
1829 * Using ttm_bo_reserve makes sure the lru lists are updated.
1832 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1833 if (unlikely(ret != 0))
1835 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1836 ret = ttm_bo_wait(bo, false, true, no_wait);
1837 lockmgr(&bdev->fence_lock, LK_RELEASE);
1838 if (likely(ret == 0))
1839 atomic_inc(&bo->cpu_writers);
1840 ttm_bo_unreserve(bo);
1843 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1845 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1847 atomic_dec(&bo->cpu_writers);
1849 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1852 * A buffer object shrink method that tries to swap out the first
1853 * buffer object on the bo_global::swap_lru list.
1856 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1858 struct ttm_bo_global *glob =
1859 container_of(shrink, struct ttm_bo_global, shrink);
1860 struct ttm_buffer_object *bo;
1863 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1865 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1866 list_for_each_entry(bo, &glob->swap_lru, swap) {
1867 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1873 lockmgr(&glob->lru_lock, LK_RELEASE);
1877 kref_get(&bo->list_kref);
1879 if (!list_empty(&bo->ddestroy)) {
1880 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1881 kref_put(&bo->list_kref, ttm_bo_release_list);
1885 put_count = ttm_bo_del_from_lru(bo);
1886 lockmgr(&glob->lru_lock, LK_RELEASE);
1888 ttm_bo_list_ref_sub(bo, put_count, true);
1891 * Wait for GPU, then move to system cached.
1894 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1895 ret = ttm_bo_wait(bo, false, false, false);
1896 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1898 if (unlikely(ret != 0))
1901 if ((bo->mem.placement & swap_placement) != swap_placement) {
1902 struct ttm_mem_reg evict_mem;
1904 evict_mem = bo->mem;
1905 evict_mem.mm_node = NULL;
1906 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1907 evict_mem.mem_type = TTM_PL_SYSTEM;
1909 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1911 if (unlikely(ret != 0))
1915 ttm_bo_unmap_virtual(bo);
1918 * Swap out. Buffer will be swapped in again as soon as
1919 * anyone tries to access a ttm page.
1922 if (bo->bdev->driver->swap_notify)
1923 bo->bdev->driver->swap_notify(bo);
1925 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1930 * Unreserve without putting on LRU to avoid swapping out an
1931 * already swapped buffer.
1934 atomic_set(&bo->reserved, 0);
1935 wake_up_all(&bo->event_queue);
1936 kref_put(&bo->list_kref, ttm_bo_release_list);
1940 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1942 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1945 EXPORT_SYMBOL(ttm_bo_swapout_all);