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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <dev/drm2/drmP.h>
35 #include <dev/drm2/ttm/ttm_module.h>
36 #include <dev/drm2/ttm/ttm_bo_driver.h>
37 #include <dev/drm2/ttm/ttm_placement.h>
38 #include <vm/vm_pageout.h>
40 #define TTM_ASSERT_LOCKED(param)
41 #define TTM_DEBUG(fmt, arg...)
42 #define TTM_BO_HASH_ORDER 13
44 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
45 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
46 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob);
48 MALLOC_DEFINE(M_TTM_BO, "ttm_bo", "TTM Buffer Objects");
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 printf(" has_type: %d\n", man->has_type);
67 printf(" use_type: %d\n", man->use_type);
68 printf(" flags: 0x%08X\n", man->flags);
69 printf(" gpu_offset: 0x%08lX\n", man->gpu_offset);
70 printf(" size: %ju\n", (uintmax_t)man->size);
71 printf(" available_caching: 0x%08X\n", man->available_caching);
72 printf(" 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 printf("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 printf(" 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 ttm_buffer_object *bo)
113 struct ttm_bo_device *bdev = bo->bdev;
114 size_t acc_size = bo->acc_size;
116 MPASS(atomic_read(&bo->list_kref) == 0);
117 MPASS(atomic_read(&bo->kref) == 0);
118 MPASS(atomic_read(&bo->cpu_writers) == 0);
119 MPASS(bo->sync_obj == NULL);
120 MPASS(bo->mem.mm_node == NULL);
121 MPASS(list_empty(&bo->lru));
122 MPASS(list_empty(&bo->ddestroy));
125 ttm_tt_destroy(bo->ttm);
126 atomic_dec(&bo->glob->bo_count);
132 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
136 ttm_bo_wait_unreserved_locked(struct ttm_buffer_object *bo, bool interruptible)
149 while (ttm_bo_is_reserved(bo)) {
150 ret = -msleep(bo, &bo->glob->lru_lock, flags, wmsg, 0);
151 if (ret == -EINTR || ret == -ERESTART)
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 MPASS(ttm_bo_is_reserved(bo));
166 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
168 MPASS(list_empty(&bo->lru));
170 man = &bdev->man[bo->mem.mem_type];
171 list_add_tail(&bo->lru, &man->lru);
172 refcount_acquire(&bo->list_kref);
174 if (bo->ttm != NULL) {
175 list_add_tail(&bo->swap, &bo->glob->swap_lru);
176 refcount_acquire(&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_locked(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;
260 bo->seq_valid = false;
266 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
271 old = atomic_fetchadd_int(&bo->list_kref, -count);
274 panic("ttm_bo_ref_buf");
275 ttm_bo_release_list(bo);
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 mtx_lock(&bo->glob->lru_lock);
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 mtx_unlock(&glob->lru_lock);
293 ttm_bo_list_ref_sub(bo, put_count, true);
295 mtx_unlock(&bo->glob->lru_lock);
300 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
301 bool interruptible, uint32_t sequence)
303 bool wake_up = false;
306 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
307 if (bo->seq_valid && sequence == bo->val_seq) {
309 "%s: bo->seq_valid && sequence == bo->val_seq",
313 ret = ttm_bo_wait_unreserved_locked(bo, interruptible);
319 if ((bo->val_seq - sequence < (1U << 31)) || !bo->seq_valid)
323 * Wake up waiters that may need to recheck for deadlock,
324 * if we decreased the sequence number.
326 bo->val_seq = sequence;
327 bo->seq_valid = true;
334 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
335 bool interruptible, uint32_t sequence)
337 struct ttm_bo_global *glob = bo->glob;
340 mtx_lock(&glob->lru_lock);
341 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence);
343 put_count = ttm_bo_del_from_lru(bo);
344 mtx_unlock(&glob->lru_lock);
345 ttm_bo_list_ref_sub(bo, put_count, true);
347 mtx_unlock(&glob->lru_lock);
351 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
353 ttm_bo_add_to_lru(bo);
354 atomic_set(&bo->reserved, 0);
358 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
360 struct ttm_bo_global *glob = bo->glob;
362 mtx_lock(&glob->lru_lock);
363 ttm_bo_unreserve_locked(bo);
364 mtx_unlock(&glob->lru_lock);
368 * Call bo->mutex locked.
370 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
372 struct ttm_bo_device *bdev = bo->bdev;
373 struct ttm_bo_global *glob = bo->glob;
375 uint32_t page_flags = 0;
377 TTM_ASSERT_LOCKED(&bo->mutex);
380 if (bdev->need_dma32)
381 page_flags |= TTM_PAGE_FLAG_DMA32;
384 case ttm_bo_type_device:
386 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
387 case ttm_bo_type_kernel:
388 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
389 page_flags, glob->dummy_read_page);
390 if (unlikely(bo->ttm == NULL))
394 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
395 page_flags | TTM_PAGE_FLAG_SG,
396 glob->dummy_read_page);
397 if (unlikely(bo->ttm == NULL)) {
401 bo->ttm->sg = bo->sg;
404 printf("[TTM] Illegal buffer object type\n");
412 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
413 struct ttm_mem_reg *mem,
414 bool evict, bool interruptible,
417 struct ttm_bo_device *bdev = bo->bdev;
418 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
419 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
420 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
421 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
424 if (old_is_pci || new_is_pci ||
425 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
426 ret = ttm_mem_io_lock(old_man, true);
427 if (unlikely(ret != 0))
429 ttm_bo_unmap_virtual_locked(bo);
430 ttm_mem_io_unlock(old_man);
434 * Create and bind a ttm if required.
437 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
438 if (bo->ttm == NULL) {
439 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
440 ret = ttm_bo_add_ttm(bo, zero);
445 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
449 if (mem->mem_type != TTM_PL_SYSTEM) {
450 ret = ttm_tt_bind(bo->ttm, mem);
455 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
456 if (bdev->driver->move_notify)
457 bdev->driver->move_notify(bo, mem);
464 if (bdev->driver->move_notify)
465 bdev->driver->move_notify(bo, mem);
467 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
468 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
469 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
470 else if (bdev->driver->move)
471 ret = bdev->driver->move(bo, evict, interruptible,
474 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
477 if (bdev->driver->move_notify) {
478 struct ttm_mem_reg tmp_mem = *mem;
481 bdev->driver->move_notify(bo, mem);
491 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
493 printf("[TTM] Can not flush read caches\n");
497 if (bo->mem.mm_node) {
498 bo->offset = (bo->mem.start << PAGE_SHIFT) +
499 bdev->man[bo->mem.mem_type].gpu_offset;
500 bo->cur_placement = bo->mem.placement;
507 new_man = &bdev->man[bo->mem.mem_type];
508 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
509 ttm_tt_unbind(bo->ttm);
510 ttm_tt_destroy(bo->ttm);
519 * Will release GPU memory type usage on destruction.
520 * This is the place to put in driver specific hooks to release
521 * driver private resources.
522 * Will release the bo::reserved lock.
525 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
527 if (bo->bdev->driver->move_notify)
528 bo->bdev->driver->move_notify(bo, NULL);
531 ttm_tt_unbind(bo->ttm);
532 ttm_tt_destroy(bo->ttm);
535 ttm_bo_mem_put(bo, &bo->mem);
537 atomic_set(&bo->reserved, 0);
541 * Since the final reference to this bo may not be dropped by
542 * the current task we have to put a memory barrier here to make
543 * sure the changes done in this function are always visible.
545 * This function only needs protection against the final kref_put.
550 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
552 struct ttm_bo_device *bdev = bo->bdev;
553 struct ttm_bo_global *glob = bo->glob;
554 struct ttm_bo_driver *driver = bdev->driver;
555 void *sync_obj = NULL;
559 mtx_lock(&glob->lru_lock);
560 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
562 mtx_lock(&bdev->fence_lock);
563 (void) ttm_bo_wait(bo, false, false, true);
564 if (!ret && !bo->sync_obj) {
565 mtx_unlock(&bdev->fence_lock);
566 put_count = ttm_bo_del_from_lru(bo);
568 mtx_unlock(&glob->lru_lock);
569 ttm_bo_cleanup_memtype_use(bo);
571 ttm_bo_list_ref_sub(bo, put_count, true);
576 sync_obj = driver->sync_obj_ref(bo->sync_obj);
577 mtx_unlock(&bdev->fence_lock);
580 atomic_set(&bo->reserved, 0);
584 refcount_acquire(&bo->list_kref);
585 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
586 mtx_unlock(&glob->lru_lock);
589 driver->sync_obj_flush(sync_obj);
590 driver->sync_obj_unref(&sync_obj);
592 taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
593 ((hz / 100) < 1) ? 1 : hz / 100);
597 * function ttm_bo_cleanup_refs_and_unlock
598 * If bo idle, remove from delayed- and lru lists, and unref.
599 * If not idle, do nothing.
601 * Must be called with lru_lock and reservation held, this function
602 * will drop both before returning.
604 * @interruptible Any sleeps should occur interruptibly.
605 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
608 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
612 struct ttm_bo_device *bdev = bo->bdev;
613 struct ttm_bo_driver *driver = bdev->driver;
614 struct ttm_bo_global *glob = bo->glob;
618 mtx_lock(&bdev->fence_lock);
619 ret = ttm_bo_wait(bo, false, false, true);
621 if (ret && !no_wait_gpu) {
625 * Take a reference to the fence and unreserve,
626 * at this point the buffer should be dead, so
627 * no new sync objects can be attached.
629 sync_obj = driver->sync_obj_ref(bo->sync_obj);
630 mtx_unlock(&bdev->fence_lock);
632 atomic_set(&bo->reserved, 0);
634 mtx_unlock(&glob->lru_lock);
636 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
637 driver->sync_obj_unref(&sync_obj);
642 * remove sync_obj with ttm_bo_wait, the wait should be
643 * finished, and no new wait object should have been added.
645 mtx_lock(&bdev->fence_lock);
646 ret = ttm_bo_wait(bo, false, false, true);
647 mtx_unlock(&bdev->fence_lock);
651 mtx_lock(&glob->lru_lock);
652 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
655 * We raced, and lost, someone else holds the reservation now,
656 * and is probably busy in ttm_bo_cleanup_memtype_use.
658 * Even if it's not the case, because we finished waiting any
659 * delayed destruction would succeed, so just return success
663 mtx_unlock(&glob->lru_lock);
667 mtx_unlock(&bdev->fence_lock);
669 if (ret || unlikely(list_empty(&bo->ddestroy))) {
670 atomic_set(&bo->reserved, 0);
672 mtx_unlock(&glob->lru_lock);
676 put_count = ttm_bo_del_from_lru(bo);
677 list_del_init(&bo->ddestroy);
680 mtx_unlock(&glob->lru_lock);
681 ttm_bo_cleanup_memtype_use(bo);
683 ttm_bo_list_ref_sub(bo, put_count, true);
689 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
690 * encountered buffers.
693 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
695 struct ttm_bo_global *glob = bdev->glob;
696 struct ttm_buffer_object *entry = NULL;
699 mtx_lock(&glob->lru_lock);
700 if (list_empty(&bdev->ddestroy))
703 entry = list_first_entry(&bdev->ddestroy,
704 struct ttm_buffer_object, ddestroy);
705 refcount_acquire(&entry->list_kref);
708 struct ttm_buffer_object *nentry = NULL;
710 if (entry->ddestroy.next != &bdev->ddestroy) {
711 nentry = list_first_entry(&entry->ddestroy,
712 struct ttm_buffer_object, ddestroy);
713 refcount_acquire(&nentry->list_kref);
716 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
717 if (remove_all && ret) {
718 ret = ttm_bo_reserve_nolru(entry, false, false,
723 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
726 mtx_unlock(&glob->lru_lock);
728 if (refcount_release(&entry->list_kref))
729 ttm_bo_release_list(entry);
735 mtx_lock(&glob->lru_lock);
736 if (list_empty(&entry->ddestroy))
741 mtx_unlock(&glob->lru_lock);
743 if (entry && refcount_release(&entry->list_kref))
744 ttm_bo_release_list(entry);
748 static void ttm_bo_delayed_workqueue(void *arg, int pending __unused)
750 struct ttm_bo_device *bdev = arg;
752 if (ttm_bo_delayed_delete(bdev, false)) {
753 taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
754 ((hz / 100) < 1) ? 1 : hz / 100);
758 static void ttm_bo_release(struct ttm_buffer_object *bo)
760 struct ttm_bo_device *bdev = bo->bdev;
761 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
763 rw_wlock(&bdev->vm_lock);
764 if (likely(bo->vm_node != NULL)) {
765 RB_REMOVE(ttm_bo_device_buffer_objects,
766 &bdev->addr_space_rb, bo);
767 drm_mm_put_block(bo->vm_node);
770 rw_wunlock(&bdev->vm_lock);
771 ttm_mem_io_lock(man, false);
772 ttm_mem_io_free_vm(bo);
773 ttm_mem_io_unlock(man);
774 ttm_bo_cleanup_refs_or_queue(bo);
775 if (refcount_release(&bo->list_kref))
776 ttm_bo_release_list(bo);
779 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
781 struct ttm_buffer_object *bo = *p_bo;
784 if (refcount_release(&bo->kref))
788 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
792 if (taskqueue_cancel_timeout(taskqueue_thread, &bdev->wq, &pending))
793 taskqueue_drain_timeout(taskqueue_thread, &bdev->wq);
797 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
800 taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
801 ((hz / 100) < 1) ? 1 : hz / 100);
805 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
808 struct ttm_bo_device *bdev = bo->bdev;
809 struct ttm_mem_reg evict_mem;
810 struct ttm_placement placement;
813 mtx_lock(&bdev->fence_lock);
814 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
815 mtx_unlock(&bdev->fence_lock);
817 if (unlikely(ret != 0)) {
818 if (ret != -ERESTARTSYS) {
819 printf("[TTM] Failed to expire sync object before buffer eviction\n");
824 MPASS(ttm_bo_is_reserved(bo));
827 evict_mem.mm_node = NULL;
828 evict_mem.bus.io_reserved_vm = false;
829 evict_mem.bus.io_reserved_count = 0;
833 placement.num_placement = 0;
834 placement.num_busy_placement = 0;
835 bdev->driver->evict_flags(bo, &placement);
836 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
839 if (ret != -ERESTARTSYS) {
840 printf("[TTM] Failed to find memory space for buffer 0x%p eviction\n",
842 ttm_bo_mem_space_debug(bo, &placement);
847 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
850 if (ret != -ERESTARTSYS)
851 printf("[TTM] Buffer eviction failed\n");
852 ttm_bo_mem_put(bo, &evict_mem);
860 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
865 struct ttm_bo_global *glob = bdev->glob;
866 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
867 struct ttm_buffer_object *bo;
868 int ret = -EBUSY, put_count;
870 mtx_lock(&glob->lru_lock);
871 list_for_each_entry(bo, &man->lru, lru) {
872 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
878 mtx_unlock(&glob->lru_lock);
882 refcount_acquire(&bo->list_kref);
884 if (!list_empty(&bo->ddestroy)) {
885 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
887 if (refcount_release(&bo->list_kref))
888 ttm_bo_release_list(bo);
892 put_count = ttm_bo_del_from_lru(bo);
893 mtx_unlock(&glob->lru_lock);
897 ttm_bo_list_ref_sub(bo, put_count, true);
899 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
900 ttm_bo_unreserve(bo);
902 if (refcount_release(&bo->list_kref))
903 ttm_bo_release_list(bo);
907 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
909 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
912 (*man->func->put_node)(man, mem);
916 * Repeatedly evict memory from the LRU for @mem_type until we create enough
917 * space, or we've evicted everything and there isn't enough space.
919 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
921 struct ttm_placement *placement,
922 struct ttm_mem_reg *mem,
926 struct ttm_bo_device *bdev = bo->bdev;
927 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
931 ret = (*man->func->get_node)(man, bo, placement, mem);
932 if (unlikely(ret != 0))
936 ret = ttm_mem_evict_first(bdev, mem_type,
937 interruptible, no_wait_gpu);
938 if (unlikely(ret != 0))
941 if (mem->mm_node == NULL)
943 mem->mem_type = mem_type;
947 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
948 uint32_t cur_placement,
949 uint32_t proposed_placement)
951 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
952 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
955 * Keep current caching if possible.
958 if ((cur_placement & caching) != 0)
959 result |= (cur_placement & caching);
960 else if ((man->default_caching & caching) != 0)
961 result |= man->default_caching;
962 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
963 result |= TTM_PL_FLAG_CACHED;
964 else if ((TTM_PL_FLAG_WC & caching) != 0)
965 result |= TTM_PL_FLAG_WC;
966 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
967 result |= TTM_PL_FLAG_UNCACHED;
972 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
974 uint32_t proposed_placement,
975 uint32_t *masked_placement)
977 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
979 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
982 if ((proposed_placement & man->available_caching) == 0)
985 cur_flags |= (proposed_placement & man->available_caching);
987 *masked_placement = cur_flags;
992 * Creates space for memory region @mem according to its type.
994 * This function first searches for free space in compatible memory types in
995 * the priority order defined by the driver. If free space isn't found, then
996 * ttm_bo_mem_force_space is attempted in priority order to evict and find
999 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1000 struct ttm_placement *placement,
1001 struct ttm_mem_reg *mem,
1005 struct ttm_bo_device *bdev = bo->bdev;
1006 struct ttm_mem_type_manager *man;
1007 uint32_t mem_type = TTM_PL_SYSTEM;
1008 uint32_t cur_flags = 0;
1009 bool type_found = false;
1010 bool type_ok = false;
1011 bool has_erestartsys = false;
1014 mem->mm_node = NULL;
1015 for (i = 0; i < placement->num_placement; ++i) {
1016 ret = ttm_mem_type_from_flags(placement->placement[i],
1020 man = &bdev->man[mem_type];
1022 type_ok = ttm_bo_mt_compatible(man,
1024 placement->placement[i],
1030 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1033 * Use the access and other non-mapping-related flag bits from
1034 * the memory placement flags to the current flags
1036 ttm_flag_masked(&cur_flags, placement->placement[i],
1037 ~TTM_PL_MASK_MEMTYPE);
1039 if (mem_type == TTM_PL_SYSTEM)
1042 if (man->has_type && man->use_type) {
1044 ret = (*man->func->get_node)(man, bo, placement, mem);
1052 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1053 mem->mem_type = mem_type;
1054 mem->placement = cur_flags;
1061 for (i = 0; i < placement->num_busy_placement; ++i) {
1062 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1066 man = &bdev->man[mem_type];
1069 if (!ttm_bo_mt_compatible(man,
1071 placement->busy_placement[i],
1075 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1078 * Use the access and other non-mapping-related flag bits from
1079 * the memory placement flags to the current flags
1081 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1082 ~TTM_PL_MASK_MEMTYPE);
1085 if (mem_type == TTM_PL_SYSTEM) {
1086 mem->mem_type = mem_type;
1087 mem->placement = cur_flags;
1088 mem->mm_node = NULL;
1092 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1093 interruptible, no_wait_gpu);
1094 if (ret == 0 && mem->mm_node) {
1095 mem->placement = cur_flags;
1098 if (ret == -ERESTARTSYS)
1099 has_erestartsys = true;
1101 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1106 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1107 struct ttm_placement *placement,
1112 struct ttm_mem_reg mem;
1113 struct ttm_bo_device *bdev = bo->bdev;
1115 MPASS(ttm_bo_is_reserved(bo));
1118 * FIXME: It's possible to pipeline buffer moves.
1119 * Have the driver move function wait for idle when necessary,
1120 * instead of doing it here.
1122 mtx_lock(&bdev->fence_lock);
1123 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1124 mtx_unlock(&bdev->fence_lock);
1127 mem.num_pages = bo->num_pages;
1128 mem.size = mem.num_pages << PAGE_SHIFT;
1129 mem.page_alignment = bo->mem.page_alignment;
1130 mem.bus.io_reserved_vm = false;
1131 mem.bus.io_reserved_count = 0;
1133 * Determine where to move the buffer.
1135 ret = ttm_bo_mem_space(bo, placement, &mem,
1136 interruptible, no_wait_gpu);
1139 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1140 interruptible, no_wait_gpu);
1142 if (ret && mem.mm_node)
1143 ttm_bo_mem_put(bo, &mem);
1147 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1148 struct ttm_mem_reg *mem)
1152 if (mem->mm_node && placement->lpfn != 0 &&
1153 (mem->start < placement->fpfn ||
1154 mem->start + mem->num_pages > placement->lpfn))
1157 for (i = 0; i < placement->num_placement; i++) {
1158 if ((placement->placement[i] & mem->placement &
1159 TTM_PL_MASK_CACHING) &&
1160 (placement->placement[i] & mem->placement &
1167 int ttm_bo_validate(struct ttm_buffer_object *bo,
1168 struct ttm_placement *placement,
1174 MPASS(ttm_bo_is_reserved(bo));
1175 /* Check that range is valid */
1176 if (placement->lpfn || placement->fpfn)
1177 if (placement->fpfn > placement->lpfn ||
1178 (placement->lpfn - placement->fpfn) < bo->num_pages)
1181 * Check whether we need to move buffer.
1183 ret = ttm_bo_mem_compat(placement, &bo->mem);
1185 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1191 * Use the access and other non-mapping-related flag bits from
1192 * the compatible memory placement flags to the active flags
1194 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1195 ~TTM_PL_MASK_MEMTYPE);
1198 * We might need to add a TTM.
1200 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1201 ret = ttm_bo_add_ttm(bo, true);
1208 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1209 struct ttm_placement *placement)
1211 MPASS(!((placement->fpfn || placement->lpfn) &&
1212 (bo->mem.num_pages > (placement->lpfn - placement->fpfn))));
1217 int ttm_bo_init(struct ttm_bo_device *bdev,
1218 struct ttm_buffer_object *bo,
1220 enum ttm_bo_type type,
1221 struct ttm_placement *placement,
1222 uint32_t page_alignment,
1224 struct vm_object *persistent_swap_storage,
1226 struct sg_table *sg,
1227 void (*destroy) (struct ttm_buffer_object *))
1230 unsigned long num_pages;
1231 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1233 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1235 printf("[TTM] Out of kernel memory\n");
1243 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1244 if (num_pages == 0) {
1245 printf("[TTM] Illegal buffer object size\n");
1250 ttm_mem_global_free(mem_glob, acc_size);
1253 bo->destroy = destroy;
1255 refcount_init(&bo->kref, 1);
1256 refcount_init(&bo->list_kref, 1);
1257 atomic_set(&bo->cpu_writers, 0);
1258 atomic_set(&bo->reserved, 1);
1259 INIT_LIST_HEAD(&bo->lru);
1260 INIT_LIST_HEAD(&bo->ddestroy);
1261 INIT_LIST_HEAD(&bo->swap);
1262 INIT_LIST_HEAD(&bo->io_reserve_lru);
1264 bo->glob = bdev->glob;
1266 bo->num_pages = num_pages;
1267 bo->mem.size = num_pages << PAGE_SHIFT;
1268 bo->mem.mem_type = TTM_PL_SYSTEM;
1269 bo->mem.num_pages = bo->num_pages;
1270 bo->mem.mm_node = NULL;
1271 bo->mem.page_alignment = page_alignment;
1272 bo->mem.bus.io_reserved_vm = false;
1273 bo->mem.bus.io_reserved_count = 0;
1275 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1276 bo->seq_valid = false;
1277 bo->persistent_swap_storage = persistent_swap_storage;
1278 bo->acc_size = acc_size;
1280 atomic_inc(&bo->glob->bo_count);
1282 ret = ttm_bo_check_placement(bo, placement);
1283 if (unlikely(ret != 0))
1287 * For ttm_bo_type_device buffers, allocate
1288 * address space from the device.
1290 if (bo->type == ttm_bo_type_device ||
1291 bo->type == ttm_bo_type_sg) {
1292 ret = ttm_bo_setup_vm(bo);
1297 ret = ttm_bo_validate(bo, placement, interruptible, false);
1301 ttm_bo_unreserve(bo);
1305 ttm_bo_unreserve(bo);
1311 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1312 unsigned long bo_size,
1313 unsigned struct_size)
1315 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1318 size += ttm_round_pot(struct_size);
1319 size += PAGE_ALIGN(npages * sizeof(void *));
1320 size += ttm_round_pot(sizeof(struct ttm_tt));
1324 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1325 unsigned long bo_size,
1326 unsigned struct_size)
1328 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1331 size += ttm_round_pot(struct_size);
1332 size += PAGE_ALIGN(npages * sizeof(void *));
1333 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1334 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1338 int ttm_bo_create(struct ttm_bo_device *bdev,
1340 enum ttm_bo_type type,
1341 struct ttm_placement *placement,
1342 uint32_t page_alignment,
1344 struct vm_object *persistent_swap_storage,
1345 struct ttm_buffer_object **p_bo)
1347 struct ttm_buffer_object *bo;
1351 bo = malloc(sizeof(*bo), M_TTM_BO, M_WAITOK | M_ZERO);
1352 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1353 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1354 interruptible, persistent_swap_storage, acc_size,
1356 if (likely(ret == 0))
1362 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1363 unsigned mem_type, bool allow_errors)
1365 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1366 struct ttm_bo_global *glob = bdev->glob;
1370 * Can't use standard list traversal since we're unlocking.
1373 mtx_lock(&glob->lru_lock);
1374 while (!list_empty(&man->lru)) {
1375 mtx_unlock(&glob->lru_lock);
1376 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1381 printf("[TTM] Cleanup eviction failed\n");
1384 mtx_lock(&glob->lru_lock);
1386 mtx_unlock(&glob->lru_lock);
1390 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1392 struct ttm_mem_type_manager *man;
1395 if (mem_type >= TTM_NUM_MEM_TYPES) {
1396 printf("[TTM] Illegal memory type %d\n", mem_type);
1399 man = &bdev->man[mem_type];
1401 if (!man->has_type) {
1402 printf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1407 man->use_type = false;
1408 man->has_type = false;
1412 ttm_bo_force_list_clean(bdev, mem_type, false);
1414 ret = (*man->func->takedown)(man);
1420 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1422 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1424 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1425 printf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1429 if (!man->has_type) {
1430 printf("[TTM] Memory type %u has not been initialized\n", mem_type);
1434 return ttm_bo_force_list_clean(bdev, mem_type, true);
1437 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1438 unsigned long p_size)
1441 struct ttm_mem_type_manager *man;
1443 MPASS(type < TTM_NUM_MEM_TYPES);
1444 man = &bdev->man[type];
1445 MPASS(!man->has_type);
1446 man->io_reserve_fastpath = true;
1447 man->use_io_reserve_lru = false;
1448 sx_init(&man->io_reserve_mutex, "ttmman");
1449 INIT_LIST_HEAD(&man->io_reserve_lru);
1451 ret = bdev->driver->init_mem_type(bdev, type, man);
1457 if (type != TTM_PL_SYSTEM) {
1458 ret = (*man->func->init)(man, p_size);
1462 man->has_type = true;
1463 man->use_type = true;
1466 INIT_LIST_HEAD(&man->lru);
1471 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1474 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1475 vm_page_free(glob->dummy_read_page);
1478 void ttm_bo_global_release(struct drm_global_reference *ref)
1480 struct ttm_bo_global *glob = ref->object;
1482 if (refcount_release(&glob->kobj_ref))
1483 ttm_bo_global_kobj_release(glob);
1486 int ttm_bo_global_init(struct drm_global_reference *ref)
1488 struct ttm_bo_global_ref *bo_ref =
1489 container_of(ref, struct ttm_bo_global_ref, ref);
1490 struct ttm_bo_global *glob = ref->object;
1494 sx_init(&glob->device_list_mutex, "ttmdlm");
1495 mtx_init(&glob->lru_lock, "ttmlru", NULL, MTX_DEF);
1496 glob->mem_glob = bo_ref->mem_glob;
1497 req = VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ;
1500 glob->dummy_read_page = vm_page_alloc_contig(NULL, 0, req,
1501 1, 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, VM_MEMATTR_UNCACHEABLE);
1503 if (unlikely(glob->dummy_read_page == NULL)) {
1504 if (tries < 1 && vm_page_reclaim_contig(req, 1,
1505 0, VM_MAX_ADDRESS, PAGE_SIZE, 0)) {
1513 INIT_LIST_HEAD(&glob->swap_lru);
1514 INIT_LIST_HEAD(&glob->device_list);
1516 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1517 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1518 if (unlikely(ret != 0)) {
1519 printf("[TTM] Could not register buffer object swapout\n");
1523 atomic_set(&glob->bo_count, 0);
1525 refcount_init(&glob->kobj_ref, 1);
1529 vm_page_free(glob->dummy_read_page);
1531 free(glob, M_DRM_GLOBAL);
1535 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1538 unsigned i = TTM_NUM_MEM_TYPES;
1539 struct ttm_mem_type_manager *man;
1540 struct ttm_bo_global *glob = bdev->glob;
1543 man = &bdev->man[i];
1544 if (man->has_type) {
1545 man->use_type = false;
1546 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1548 printf("[TTM] DRM memory manager type %d is not clean\n",
1551 man->has_type = false;
1555 sx_xlock(&glob->device_list_mutex);
1556 list_del(&bdev->device_list);
1557 sx_xunlock(&glob->device_list_mutex);
1559 if (taskqueue_cancel_timeout(taskqueue_thread, &bdev->wq, NULL))
1560 taskqueue_drain_timeout(taskqueue_thread, &bdev->wq);
1562 while (ttm_bo_delayed_delete(bdev, true))
1565 mtx_lock(&glob->lru_lock);
1566 if (list_empty(&bdev->ddestroy))
1567 TTM_DEBUG("Delayed destroy list was clean\n");
1569 if (list_empty(&bdev->man[0].lru))
1570 TTM_DEBUG("Swap list was clean\n");
1571 mtx_unlock(&glob->lru_lock);
1573 MPASS(drm_mm_clean(&bdev->addr_space_mm));
1574 rw_wlock(&bdev->vm_lock);
1575 drm_mm_takedown(&bdev->addr_space_mm);
1576 rw_wunlock(&bdev->vm_lock);
1581 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1582 struct ttm_bo_global *glob,
1583 struct ttm_bo_driver *driver,
1584 uint64_t file_page_offset,
1589 rw_init(&bdev->vm_lock, "ttmvml");
1590 bdev->driver = driver;
1592 memset(bdev->man, 0, sizeof(bdev->man));
1595 * Initialize the system memory buffer type.
1596 * Other types need to be driver / IOCTL initialized.
1598 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1599 if (unlikely(ret != 0))
1602 RB_INIT(&bdev->addr_space_rb);
1603 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1604 if (unlikely(ret != 0))
1605 goto out_no_addr_mm;
1607 TIMEOUT_TASK_INIT(taskqueue_thread, &bdev->wq, 0,
1608 ttm_bo_delayed_workqueue, bdev);
1609 INIT_LIST_HEAD(&bdev->ddestroy);
1610 bdev->dev_mapping = NULL;
1612 bdev->need_dma32 = need_dma32;
1614 mtx_init(&bdev->fence_lock, "ttmfence", NULL, MTX_DEF);
1615 sx_xlock(&glob->device_list_mutex);
1616 list_add_tail(&bdev->device_list, &glob->device_list);
1617 sx_xunlock(&glob->device_list_mutex);
1621 ttm_bo_clean_mm(bdev, 0);
1627 * buffer object vm functions.
1630 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1632 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1634 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1635 if (mem->mem_type == TTM_PL_SYSTEM)
1638 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1641 if (mem->placement & TTM_PL_FLAG_CACHED)
1647 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1650 ttm_bo_release_mmap(bo);
1651 ttm_mem_io_free_vm(bo);
1654 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1656 struct ttm_bo_device *bdev = bo->bdev;
1657 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1659 ttm_mem_io_lock(man, false);
1660 ttm_bo_unmap_virtual_locked(bo);
1661 ttm_mem_io_unlock(man);
1664 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1666 struct ttm_bo_device *bdev = bo->bdev;
1668 /* The caller acquired bdev->vm_lock. */
1669 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1675 * @bo: the buffer to allocate address space for
1677 * Allocate address space in the drm device so that applications
1678 * can mmap the buffer and access the contents. This only
1679 * applies to ttm_bo_type_device objects as others are not
1680 * placed in the drm device address space.
1683 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1685 struct ttm_bo_device *bdev = bo->bdev;
1689 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1690 if (unlikely(ret != 0))
1693 rw_wlock(&bdev->vm_lock);
1694 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1695 bo->mem.num_pages, 0, 0);
1697 if (unlikely(bo->vm_node == NULL)) {
1702 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1703 bo->mem.num_pages, 0);
1705 if (unlikely(bo->vm_node == NULL)) {
1706 rw_wunlock(&bdev->vm_lock);
1710 ttm_bo_vm_insert_rb(bo);
1711 rw_wunlock(&bdev->vm_lock);
1712 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1716 rw_wunlock(&bdev->vm_lock);
1720 int ttm_bo_wait(struct ttm_buffer_object *bo,
1721 bool lazy, bool interruptible, bool no_wait)
1723 struct ttm_bo_driver *driver = bo->bdev->driver;
1724 struct ttm_bo_device *bdev = bo->bdev;
1728 if (likely(bo->sync_obj == NULL))
1731 while (bo->sync_obj) {
1733 if (driver->sync_obj_signaled(bo->sync_obj)) {
1734 void *tmp_obj = bo->sync_obj;
1735 bo->sync_obj = NULL;
1736 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1737 mtx_unlock(&bdev->fence_lock);
1738 driver->sync_obj_unref(&tmp_obj);
1739 mtx_lock(&bdev->fence_lock);
1746 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1747 mtx_unlock(&bdev->fence_lock);
1748 ret = driver->sync_obj_wait(sync_obj,
1749 lazy, interruptible);
1750 if (unlikely(ret != 0)) {
1751 driver->sync_obj_unref(&sync_obj);
1752 mtx_lock(&bdev->fence_lock);
1755 mtx_lock(&bdev->fence_lock);
1756 if (likely(bo->sync_obj == sync_obj)) {
1757 void *tmp_obj = bo->sync_obj;
1758 bo->sync_obj = NULL;
1759 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1761 mtx_unlock(&bdev->fence_lock);
1762 driver->sync_obj_unref(&sync_obj);
1763 driver->sync_obj_unref(&tmp_obj);
1764 mtx_lock(&bdev->fence_lock);
1766 mtx_unlock(&bdev->fence_lock);
1767 driver->sync_obj_unref(&sync_obj);
1768 mtx_lock(&bdev->fence_lock);
1774 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1776 struct ttm_bo_device *bdev = bo->bdev;
1780 * Using ttm_bo_reserve makes sure the lru lists are updated.
1783 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1784 if (unlikely(ret != 0))
1786 mtx_lock(&bdev->fence_lock);
1787 ret = ttm_bo_wait(bo, false, true, no_wait);
1788 mtx_unlock(&bdev->fence_lock);
1789 if (likely(ret == 0))
1790 atomic_inc(&bo->cpu_writers);
1791 ttm_bo_unreserve(bo);
1795 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1797 atomic_dec(&bo->cpu_writers);
1801 * A buffer object shrink method that tries to swap out the first
1802 * buffer object on the bo_global::swap_lru list.
1805 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1807 struct ttm_bo_global *glob =
1808 container_of(shrink, struct ttm_bo_global, shrink);
1809 struct ttm_buffer_object *bo;
1812 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1814 mtx_lock(&glob->lru_lock);
1815 list_for_each_entry(bo, &glob->swap_lru, swap) {
1816 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1822 mtx_unlock(&glob->lru_lock);
1826 refcount_acquire(&bo->list_kref);
1828 if (!list_empty(&bo->ddestroy)) {
1829 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1830 if (refcount_release(&bo->list_kref))
1831 ttm_bo_release_list(bo);
1835 put_count = ttm_bo_del_from_lru(bo);
1836 mtx_unlock(&glob->lru_lock);
1838 ttm_bo_list_ref_sub(bo, put_count, true);
1841 * Wait for GPU, then move to system cached.
1844 mtx_lock(&bo->bdev->fence_lock);
1845 ret = ttm_bo_wait(bo, false, false, false);
1846 mtx_unlock(&bo->bdev->fence_lock);
1848 if (unlikely(ret != 0))
1851 if ((bo->mem.placement & swap_placement) != swap_placement) {
1852 struct ttm_mem_reg evict_mem;
1854 evict_mem = bo->mem;
1855 evict_mem.mm_node = NULL;
1856 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1857 evict_mem.mem_type = TTM_PL_SYSTEM;
1859 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1861 if (unlikely(ret != 0))
1865 ttm_bo_unmap_virtual(bo);
1868 * Swap out. Buffer will be swapped in again as soon as
1869 * anyone tries to access a ttm page.
1872 if (bo->bdev->driver->swap_notify)
1873 bo->bdev->driver->swap_notify(bo);
1875 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1880 * Unreserve without putting on LRU to avoid swapping out an
1881 * already swapped buffer.
1884 atomic_set(&bo->reserved, 0);
1886 if (refcount_release(&bo->list_kref))
1887 ttm_bo_release_list(bo);
1891 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1893 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)