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 #include <dev/drm2/drmP.h>
34 #include <dev/drm2/ttm/ttm_module.h>
35 #include <dev/drm2/ttm/ttm_bo_driver.h>
36 #include <dev/drm2/ttm/ttm_placement.h>
38 #define TTM_ASSERT_LOCKED(param)
39 #define TTM_DEBUG(fmt, arg...)
40 #define TTM_BO_HASH_ORDER 13
42 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
43 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
44 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob);
46 MALLOC_DEFINE(M_TTM_BO, "ttm_bo", "TTM Buffer Objects");
48 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
52 for (i = 0; i <= TTM_PL_PRIV5; i++)
53 if (flags & (1 << i)) {
60 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
62 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
64 kprintf(" has_type: %d\n", man->has_type);
65 kprintf(" use_type: %d\n", man->use_type);
66 kprintf(" flags: 0x%08X\n", man->flags);
67 kprintf(" gpu_offset: 0x%08lX\n", man->gpu_offset);
68 kprintf(" size: %ju\n", (uintmax_t)man->size);
69 kprintf(" available_caching: 0x%08X\n", man->available_caching);
70 kprintf(" default_caching: 0x%08X\n", man->default_caching);
71 if (mem_type != TTM_PL_SYSTEM)
72 (*man->func->debug)(man, TTM_PFX);
75 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
76 struct ttm_placement *placement)
80 kprintf("No space for %p (%lu pages, %luK, %luM)\n",
81 bo, bo->mem.num_pages, bo->mem.size >> 10,
83 for (i = 0; i < placement->num_placement; i++) {
84 ret = ttm_mem_type_from_flags(placement->placement[i],
88 kprintf(" placement[%d]=0x%08X (%d)\n",
89 i, placement->placement[i], mem_type);
90 ttm_mem_type_debug(bo->bdev, mem_type);
95 static ssize_t ttm_bo_global_show(struct ttm_bo_global *glob,
99 return snprintf(buffer, PAGE_SIZE, "%lu\n",
100 (unsigned long) atomic_read(&glob->bo_count));
104 static inline uint32_t ttm_bo_type_flags(unsigned type)
109 static void ttm_bo_release_list(struct ttm_buffer_object *bo)
111 struct ttm_bo_device *bdev = bo->bdev;
112 size_t acc_size = bo->acc_size;
114 KKASSERT(atomic_read(&bo->list_kref) == 0);
115 KKASSERT(atomic_read(&bo->kref) == 0);
116 KKASSERT(atomic_read(&bo->cpu_writers) == 0);
117 KKASSERT(bo->sync_obj == NULL);
118 KKASSERT(bo->mem.mm_node == NULL);
119 KKASSERT(list_empty(&bo->lru));
120 KKASSERT(list_empty(&bo->ddestroy));
123 ttm_tt_destroy(bo->ttm);
124 atomic_dec(&bo->glob->bo_count);
128 drm_free(bo, M_TTM_BO);
130 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
134 ttm_bo_wait_unreserved_locked(struct ttm_buffer_object *bo, bool interruptible)
147 while (!ttm_bo_is_reserved(bo)) {
148 ret = -msleep(bo, &bo->glob->lru_lock, flags, wmsg, 0);
155 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
157 struct ttm_bo_device *bdev = bo->bdev;
158 struct ttm_mem_type_manager *man;
160 KKASSERT(ttm_bo_is_reserved(bo));
162 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
164 KKASSERT(list_empty(&bo->lru));
166 man = &bdev->man[bo->mem.mem_type];
167 list_add_tail(&bo->lru, &man->lru);
168 refcount_acquire(&bo->list_kref);
170 if (bo->ttm != NULL) {
171 list_add_tail(&bo->swap, &bo->glob->swap_lru);
172 refcount_acquire(&bo->list_kref);
177 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
181 if (!list_empty(&bo->swap)) {
182 list_del_init(&bo->swap);
185 if (!list_empty(&bo->lru)) {
186 list_del_init(&bo->lru);
191 * TODO: Add a driver hook to delete from
192 * driver-specific LRU's here.
198 int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
200 bool no_wait, bool use_sequence, uint32_t sequence)
204 while (unlikely(atomic_read(&bo->reserved) != 0)) {
206 * Deadlock avoidance for multi-bo reserving.
208 if (use_sequence && bo->seq_valid) {
210 * We've already reserved this one.
212 if (unlikely(sequence == bo->val_seq))
215 * Already reserved by a thread that will not back
216 * off for us. We need to back off.
218 if (unlikely(sequence - bo->val_seq < (1 << 31)))
225 ret = ttm_bo_wait_unreserved_locked(bo, interruptible);
230 atomic_set(&bo->reserved, 1);
233 * Wake up waiters that may need to recheck for deadlock,
234 * if we decreased the sequence number.
236 if (unlikely((bo->val_seq - sequence < (1 << 31))
240 bo->val_seq = sequence;
241 bo->seq_valid = true;
243 bo->seq_valid = false;
249 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
254 old = atomic_fetchadd_int(&bo->list_kref, -count);
257 panic("ttm_bo_ref_buf");
258 ttm_bo_release_list(bo);
262 int ttm_bo_reserve(struct ttm_buffer_object *bo,
264 bool no_wait, bool use_sequence, uint32_t sequence)
266 struct ttm_bo_global *glob = bo->glob;
270 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
271 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
273 if (likely(ret == 0))
274 put_count = ttm_bo_del_from_lru(bo);
275 lockmgr(&glob->lru_lock, LK_RELEASE);
277 ttm_bo_list_ref_sub(bo, put_count, true);
282 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
284 ttm_bo_add_to_lru(bo);
285 atomic_set(&bo->reserved, 0);
289 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
291 struct ttm_bo_global *glob = bo->glob;
293 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
294 ttm_bo_unreserve_locked(bo);
295 lockmgr(&glob->lru_lock, LK_RELEASE);
299 * Call bo->mutex locked.
301 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
303 struct ttm_bo_device *bdev = bo->bdev;
304 struct ttm_bo_global *glob = bo->glob;
306 uint32_t page_flags = 0;
308 TTM_ASSERT_LOCKED(&bo->mutex);
311 if (bdev->need_dma32)
312 page_flags |= TTM_PAGE_FLAG_DMA32;
315 case ttm_bo_type_device:
317 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
318 case ttm_bo_type_kernel:
319 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
320 page_flags, glob->dummy_read_page);
321 if (unlikely(bo->ttm == NULL))
325 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
326 page_flags | TTM_PAGE_FLAG_SG,
327 glob->dummy_read_page);
328 if (unlikely(bo->ttm == NULL)) {
332 bo->ttm->sg = bo->sg;
335 kprintf("[TTM] Illegal buffer object type\n");
343 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
344 struct ttm_mem_reg *mem,
345 bool evict, bool interruptible,
348 struct ttm_bo_device *bdev = bo->bdev;
349 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
350 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
351 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
352 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
355 if (old_is_pci || new_is_pci ||
356 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
357 ret = ttm_mem_io_lock(old_man, true);
358 if (unlikely(ret != 0))
360 ttm_bo_unmap_virtual_locked(bo);
361 ttm_mem_io_unlock(old_man);
365 * Create and bind a ttm if required.
368 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
369 if (bo->ttm == NULL) {
370 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
371 ret = ttm_bo_add_ttm(bo, zero);
376 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
380 if (mem->mem_type != TTM_PL_SYSTEM) {
381 ret = ttm_tt_bind(bo->ttm, mem);
386 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
387 if (bdev->driver->move_notify)
388 bdev->driver->move_notify(bo, mem);
395 if (bdev->driver->move_notify)
396 bdev->driver->move_notify(bo, mem);
398 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
399 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
400 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
401 else if (bdev->driver->move)
402 ret = bdev->driver->move(bo, evict, interruptible,
405 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
408 if (bdev->driver->move_notify) {
409 struct ttm_mem_reg tmp_mem = *mem;
412 bdev->driver->move_notify(bo, mem);
421 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
423 kprintf("[TTM] Can not flush read caches\n");
427 if (bo->mem.mm_node) {
428 bo->offset = (bo->mem.start << PAGE_SHIFT) +
429 bdev->man[bo->mem.mem_type].gpu_offset;
430 bo->cur_placement = bo->mem.placement;
437 new_man = &bdev->man[bo->mem.mem_type];
438 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
439 ttm_tt_unbind(bo->ttm);
440 ttm_tt_destroy(bo->ttm);
449 * Will release GPU memory type usage on destruction.
450 * This is the place to put in driver specific hooks to release
451 * driver private resources.
452 * Will release the bo::reserved lock.
455 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
457 if (bo->bdev->driver->move_notify)
458 bo->bdev->driver->move_notify(bo, NULL);
461 ttm_tt_unbind(bo->ttm);
462 ttm_tt_destroy(bo->ttm);
465 ttm_bo_mem_put(bo, &bo->mem);
467 atomic_set(&bo->reserved, 0);
471 * Since the final reference to this bo may not be dropped by
472 * the current task we have to put a memory barrier here to make
473 * sure the changes done in this function are always visible.
475 * This function only needs protection against the final kref_put.
480 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
482 struct ttm_bo_device *bdev = bo->bdev;
483 struct ttm_bo_global *glob = bo->glob;
484 struct ttm_bo_driver *driver = bdev->driver;
485 void *sync_obj = NULL;
489 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
490 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
492 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
493 (void) ttm_bo_wait(bo, false, false, true);
494 if (!ret && !bo->sync_obj) {
495 lockmgr(&bdev->fence_lock, LK_RELEASE);
496 put_count = ttm_bo_del_from_lru(bo);
498 lockmgr(&glob->lru_lock, LK_RELEASE);
499 ttm_bo_cleanup_memtype_use(bo);
501 ttm_bo_list_ref_sub(bo, put_count, true);
506 sync_obj = driver->sync_obj_ref(bo->sync_obj);
507 lockmgr(&bdev->fence_lock, LK_RELEASE);
510 atomic_set(&bo->reserved, 0);
514 refcount_acquire(&bo->list_kref);
515 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
516 lockmgr(&glob->lru_lock, LK_RELEASE);
519 driver->sync_obj_flush(sync_obj);
520 driver->sync_obj_unref(&sync_obj);
522 taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
523 ((hz / 100) < 1) ? 1 : hz / 100);
527 * function ttm_bo_cleanup_refs_and_unlock
528 * If bo idle, remove from delayed- and lru lists, and unref.
529 * If not idle, do nothing.
531 * Must be called with lru_lock and reservation held, this function
532 * will drop both before returning.
534 * @interruptible Any sleeps should occur interruptibly.
535 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
538 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
542 struct ttm_bo_device *bdev = bo->bdev;
543 struct ttm_bo_driver *driver = bdev->driver;
544 struct ttm_bo_global *glob = bo->glob;
548 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
549 ret = ttm_bo_wait(bo, false, false, true);
551 if (ret && !no_wait_gpu) {
555 * Take a reference to the fence and unreserve,
556 * at this point the buffer should be dead, so
557 * no new sync objects can be attached.
559 sync_obj = driver->sync_obj_ref(bo->sync_obj);
560 lockmgr(&bdev->fence_lock, LK_RELEASE);
562 atomic_set(&bo->reserved, 0);
564 lockmgr(&glob->lru_lock, LK_RELEASE);
566 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
567 driver->sync_obj_unref(&sync_obj);
572 * remove sync_obj with ttm_bo_wait, the wait should be
573 * finished, and no new wait object should have been added.
575 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
576 ret = ttm_bo_wait(bo, false, false, true);
577 lockmgr(&bdev->fence_lock, LK_RELEASE);
581 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
582 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
585 * We raced, and lost, someone else holds the reservation now,
586 * and is probably busy in ttm_bo_cleanup_memtype_use.
588 * Even if it's not the case, because we finished waiting any
589 * delayed destruction would succeed, so just return success
593 lockmgr(&glob->lru_lock, LK_RELEASE);
597 lockmgr(&bdev->fence_lock, LK_RELEASE);
599 if (ret || unlikely(list_empty(&bo->ddestroy))) {
600 atomic_set(&bo->reserved, 0);
602 lockmgr(&glob->lru_lock, LK_RELEASE);
606 put_count = ttm_bo_del_from_lru(bo);
607 list_del_init(&bo->ddestroy);
610 lockmgr(&glob->lru_lock, LK_RELEASE);
611 ttm_bo_cleanup_memtype_use(bo);
613 ttm_bo_list_ref_sub(bo, put_count, true);
619 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
620 * encountered buffers.
623 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
625 struct ttm_bo_global *glob = bdev->glob;
626 struct ttm_buffer_object *entry = NULL;
629 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
630 if (list_empty(&bdev->ddestroy))
633 entry = list_first_entry(&bdev->ddestroy,
634 struct ttm_buffer_object, ddestroy);
635 refcount_acquire(&entry->list_kref);
638 struct ttm_buffer_object *nentry = NULL;
640 if (entry->ddestroy.next != &bdev->ddestroy) {
641 nentry = list_first_entry(&entry->ddestroy,
642 struct ttm_buffer_object, ddestroy);
643 refcount_acquire(&nentry->list_kref);
646 ret = ttm_bo_reserve_locked(entry, false, !remove_all, false, 0);
648 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
651 lockmgr(&glob->lru_lock, LK_RELEASE);
653 if (refcount_release(&entry->list_kref))
654 ttm_bo_release_list(entry);
660 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
661 if (list_empty(&entry->ddestroy))
666 lockmgr(&glob->lru_lock, LK_RELEASE);
668 if (entry && refcount_release(&entry->list_kref))
669 ttm_bo_release_list(entry);
673 static void ttm_bo_delayed_workqueue(void *arg, int pending __unused)
675 struct ttm_bo_device *bdev = arg;
677 if (ttm_bo_delayed_delete(bdev, false)) {
678 taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
679 ((hz / 100) < 1) ? 1 : hz / 100);
683 static void ttm_bo_release(struct ttm_buffer_object *bo)
685 struct ttm_bo_device *bdev = bo->bdev;
686 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
688 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
689 if (likely(bo->vm_node != NULL)) {
690 RB_REMOVE(ttm_bo_device_buffer_objects,
691 &bdev->addr_space_rb, bo);
692 drm_mm_put_block(bo->vm_node);
695 lockmgr(&bdev->vm_lock, LK_RELEASE);
696 ttm_mem_io_lock(man, false);
697 ttm_mem_io_free_vm(bo);
698 ttm_mem_io_unlock(man);
699 ttm_bo_cleanup_refs_or_queue(bo);
700 if (refcount_release(&bo->list_kref))
701 ttm_bo_release_list(bo);
704 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
706 struct ttm_buffer_object *bo = *p_bo;
709 if (refcount_release(&bo->kref))
713 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
717 taskqueue_cancel_timeout(taskqueue_thread, &bdev->wq, &pending);
719 taskqueue_drain_timeout(taskqueue_thread, &bdev->wq);
723 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
726 taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
727 ((hz / 100) < 1) ? 1 : hz / 100);
731 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
734 struct ttm_bo_device *bdev = bo->bdev;
735 struct ttm_mem_reg evict_mem;
736 struct ttm_placement placement;
739 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
740 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
741 lockmgr(&bdev->fence_lock, LK_RELEASE);
743 if (unlikely(ret != 0)) {
744 if (ret != -ERESTART) {
745 kprintf("[TTM] Failed to expire sync object before buffer eviction\n");
750 KKASSERT(ttm_bo_is_reserved(bo));
753 evict_mem.mm_node = NULL;
754 evict_mem.bus.io_reserved_vm = false;
755 evict_mem.bus.io_reserved_count = 0;
759 placement.num_placement = 0;
760 placement.num_busy_placement = 0;
761 bdev->driver->evict_flags(bo, &placement);
762 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
765 if (ret != -ERESTART) {
766 kprintf("[TTM] Failed to find memory space for buffer 0x%p eviction\n",
768 ttm_bo_mem_space_debug(bo, &placement);
773 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
776 if (ret != -ERESTART)
777 kprintf("[TTM] Buffer eviction failed\n");
778 ttm_bo_mem_put(bo, &evict_mem);
786 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
791 struct ttm_bo_global *glob = bdev->glob;
792 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
793 struct ttm_buffer_object *bo;
794 int ret = -EBUSY, put_count;
796 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
797 list_for_each_entry(bo, &man->lru, lru) {
798 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
804 lockmgr(&glob->lru_lock, LK_RELEASE);
808 refcount_acquire(&bo->list_kref);
810 if (!list_empty(&bo->ddestroy)) {
811 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
813 if (refcount_release(&bo->list_kref))
814 ttm_bo_release_list(bo);
818 put_count = ttm_bo_del_from_lru(bo);
819 lockmgr(&glob->lru_lock, LK_RELEASE);
823 ttm_bo_list_ref_sub(bo, put_count, true);
825 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
826 ttm_bo_unreserve(bo);
828 if (refcount_release(&bo->list_kref))
829 ttm_bo_release_list(bo);
833 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
835 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
838 (*man->func->put_node)(man, mem);
842 * Repeatedly evict memory from the LRU for @mem_type until we create enough
843 * space, or we've evicted everything and there isn't enough space.
845 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
847 struct ttm_placement *placement,
848 struct ttm_mem_reg *mem,
852 struct ttm_bo_device *bdev = bo->bdev;
853 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
857 ret = (*man->func->get_node)(man, bo, placement, mem);
858 if (unlikely(ret != 0))
862 ret = ttm_mem_evict_first(bdev, mem_type,
863 interruptible, no_wait_gpu);
864 if (unlikely(ret != 0))
867 if (mem->mm_node == NULL)
869 mem->mem_type = mem_type;
873 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
874 uint32_t cur_placement,
875 uint32_t proposed_placement)
877 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
878 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
881 * Keep current caching if possible.
884 if ((cur_placement & caching) != 0)
885 result |= (cur_placement & caching);
886 else if ((man->default_caching & caching) != 0)
887 result |= man->default_caching;
888 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
889 result |= TTM_PL_FLAG_CACHED;
890 else if ((TTM_PL_FLAG_WC & caching) != 0)
891 result |= TTM_PL_FLAG_WC;
892 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
893 result |= TTM_PL_FLAG_UNCACHED;
898 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
900 uint32_t proposed_placement,
901 uint32_t *masked_placement)
903 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
905 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
908 if ((proposed_placement & man->available_caching) == 0)
911 cur_flags |= (proposed_placement & man->available_caching);
913 *masked_placement = cur_flags;
918 * Creates space for memory region @mem according to its type.
920 * This function first searches for free space in compatible memory types in
921 * the priority order defined by the driver. If free space isn't found, then
922 * ttm_bo_mem_force_space is attempted in priority order to evict and find
925 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
926 struct ttm_placement *placement,
927 struct ttm_mem_reg *mem,
931 struct ttm_bo_device *bdev = bo->bdev;
932 struct ttm_mem_type_manager *man;
933 uint32_t mem_type = TTM_PL_SYSTEM;
934 uint32_t cur_flags = 0;
935 bool type_found = false;
936 bool type_ok = false;
937 bool has_erestartsys = false;
941 for (i = 0; i < placement->num_placement; ++i) {
942 ret = ttm_mem_type_from_flags(placement->placement[i],
946 man = &bdev->man[mem_type];
948 type_ok = ttm_bo_mt_compatible(man,
950 placement->placement[i],
956 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
959 * Use the access and other non-mapping-related flag bits from
960 * the memory placement flags to the current flags
962 ttm_flag_masked(&cur_flags, placement->placement[i],
963 ~TTM_PL_MASK_MEMTYPE);
965 if (mem_type == TTM_PL_SYSTEM)
968 if (man->has_type && man->use_type) {
970 ret = (*man->func->get_node)(man, bo, placement, mem);
978 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
979 mem->mem_type = mem_type;
980 mem->placement = cur_flags;
987 for (i = 0; i < placement->num_busy_placement; ++i) {
988 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
992 man = &bdev->man[mem_type];
995 if (!ttm_bo_mt_compatible(man,
997 placement->busy_placement[i],
1001 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1004 * Use the access and other non-mapping-related flag bits from
1005 * the memory placement flags to the current flags
1007 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1008 ~TTM_PL_MASK_MEMTYPE);
1011 if (mem_type == TTM_PL_SYSTEM) {
1012 mem->mem_type = mem_type;
1013 mem->placement = cur_flags;
1014 mem->mm_node = NULL;
1018 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1019 interruptible, no_wait_gpu);
1020 if (ret == 0 && mem->mm_node) {
1021 mem->placement = cur_flags;
1024 if (ret == -ERESTART)
1025 has_erestartsys = true;
1027 ret = (has_erestartsys) ? -ERESTART : -ENOMEM;
1032 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1033 struct ttm_placement *placement,
1038 struct ttm_mem_reg mem;
1039 struct ttm_bo_device *bdev = bo->bdev;
1041 KKASSERT(ttm_bo_is_reserved(bo));
1044 * FIXME: It's possible to pipeline buffer moves.
1045 * Have the driver move function wait for idle when necessary,
1046 * instead of doing it here.
1048 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1049 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1050 lockmgr(&bdev->fence_lock, LK_RELEASE);
1053 mem.num_pages = bo->num_pages;
1054 mem.size = mem.num_pages << PAGE_SHIFT;
1055 mem.page_alignment = bo->mem.page_alignment;
1056 mem.bus.io_reserved_vm = false;
1057 mem.bus.io_reserved_count = 0;
1059 * Determine where to move the buffer.
1061 ret = ttm_bo_mem_space(bo, placement, &mem,
1062 interruptible, no_wait_gpu);
1065 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1066 interruptible, no_wait_gpu);
1068 if (ret && mem.mm_node)
1069 ttm_bo_mem_put(bo, &mem);
1073 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1074 struct ttm_mem_reg *mem)
1078 if (mem->mm_node && placement->lpfn != 0 &&
1079 (mem->start < placement->fpfn ||
1080 mem->start + mem->num_pages > placement->lpfn))
1083 for (i = 0; i < placement->num_placement; i++) {
1084 if ((placement->placement[i] & mem->placement &
1085 TTM_PL_MASK_CACHING) &&
1086 (placement->placement[i] & mem->placement &
1093 int ttm_bo_validate(struct ttm_buffer_object *bo,
1094 struct ttm_placement *placement,
1100 KKASSERT(ttm_bo_is_reserved(bo));
1101 /* Check that range is valid */
1102 if (placement->lpfn || placement->fpfn)
1103 if (placement->fpfn > placement->lpfn ||
1104 (placement->lpfn - placement->fpfn) < bo->num_pages)
1107 * Check whether we need to move buffer.
1109 ret = ttm_bo_mem_compat(placement, &bo->mem);
1111 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1117 * Use the access and other non-mapping-related flag bits from
1118 * the compatible memory placement flags to the active flags
1120 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1121 ~TTM_PL_MASK_MEMTYPE);
1124 * We might need to add a TTM.
1126 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1127 ret = ttm_bo_add_ttm(bo, true);
1134 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1135 struct ttm_placement *placement)
1137 KKASSERT(!((placement->fpfn || placement->lpfn) &&
1138 (bo->mem.num_pages > (placement->lpfn - placement->fpfn))));
1143 int ttm_bo_init(struct ttm_bo_device *bdev,
1144 struct ttm_buffer_object *bo,
1146 enum ttm_bo_type type,
1147 struct ttm_placement *placement,
1148 uint32_t page_alignment,
1150 struct vm_object *persistent_swap_storage,
1152 struct sg_table *sg,
1153 void (*destroy) (struct ttm_buffer_object *))
1156 unsigned long num_pages;
1157 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1159 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1161 kprintf("[TTM] Out of kernel memory\n");
1165 drm_free(bo, M_TTM_BO);
1169 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1170 if (num_pages == 0) {
1171 kprintf("[TTM] Illegal buffer object size\n");
1175 drm_free(bo, M_TTM_BO);
1176 ttm_mem_global_free(mem_glob, acc_size);
1179 bo->destroy = destroy;
1181 refcount_init(&bo->kref, 1);
1182 refcount_init(&bo->list_kref, 1);
1183 atomic_set(&bo->cpu_writers, 0);
1184 atomic_set(&bo->reserved, 1);
1185 INIT_LIST_HEAD(&bo->lru);
1186 INIT_LIST_HEAD(&bo->ddestroy);
1187 INIT_LIST_HEAD(&bo->swap);
1188 INIT_LIST_HEAD(&bo->io_reserve_lru);
1190 bo->glob = bdev->glob;
1192 bo->num_pages = num_pages;
1193 bo->mem.size = num_pages << PAGE_SHIFT;
1194 bo->mem.mem_type = TTM_PL_SYSTEM;
1195 bo->mem.num_pages = bo->num_pages;
1196 bo->mem.mm_node = NULL;
1197 bo->mem.page_alignment = page_alignment;
1198 bo->mem.bus.io_reserved_vm = false;
1199 bo->mem.bus.io_reserved_count = 0;
1201 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1202 bo->seq_valid = false;
1203 bo->persistent_swap_storage = persistent_swap_storage;
1204 bo->acc_size = acc_size;
1206 atomic_inc(&bo->glob->bo_count);
1208 ret = ttm_bo_check_placement(bo, placement);
1209 if (unlikely(ret != 0))
1213 * For ttm_bo_type_device buffers, allocate
1214 * address space from the device.
1216 if (bo->type == ttm_bo_type_device ||
1217 bo->type == ttm_bo_type_sg) {
1218 ret = ttm_bo_setup_vm(bo);
1223 ret = ttm_bo_validate(bo, placement, interruptible, false);
1227 ttm_bo_unreserve(bo);
1231 ttm_bo_unreserve(bo);
1237 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1238 unsigned long bo_size,
1239 unsigned struct_size)
1241 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1244 size += ttm_round_pot(struct_size);
1245 size += PAGE_ALIGN(npages * sizeof(void *));
1246 size += ttm_round_pot(sizeof(struct ttm_tt));
1250 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1251 unsigned long bo_size,
1252 unsigned struct_size)
1254 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1257 size += ttm_round_pot(struct_size);
1258 size += PAGE_ALIGN(npages * sizeof(void *));
1259 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1260 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1264 int ttm_bo_create(struct ttm_bo_device *bdev,
1266 enum ttm_bo_type type,
1267 struct ttm_placement *placement,
1268 uint32_t page_alignment,
1270 struct vm_object *persistent_swap_storage,
1271 struct ttm_buffer_object **p_bo)
1273 struct ttm_buffer_object *bo;
1277 bo = kmalloc(sizeof(*bo), M_TTM_BO, M_WAITOK | M_ZERO);
1278 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1279 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1280 interruptible, persistent_swap_storage, acc_size,
1282 if (likely(ret == 0))
1288 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1289 unsigned mem_type, bool allow_errors)
1291 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1292 struct ttm_bo_global *glob = bdev->glob;
1296 * Can't use standard list traversal since we're unlocking.
1299 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1300 while (!list_empty(&man->lru)) {
1301 lockmgr(&glob->lru_lock, LK_RELEASE);
1302 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1307 kprintf("[TTM] Cleanup eviction failed\n");
1310 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1312 lockmgr(&glob->lru_lock, LK_RELEASE);
1316 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1318 struct ttm_mem_type_manager *man;
1321 if (mem_type >= TTM_NUM_MEM_TYPES) {
1322 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1325 man = &bdev->man[mem_type];
1327 if (!man->has_type) {
1328 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1333 man->use_type = false;
1334 man->has_type = false;
1338 ttm_bo_force_list_clean(bdev, mem_type, false);
1340 ret = (*man->func->takedown)(man);
1346 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1348 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1350 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1351 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1355 if (!man->has_type) {
1356 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1360 return ttm_bo_force_list_clean(bdev, mem_type, true);
1363 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1364 unsigned long p_size)
1367 struct ttm_mem_type_manager *man;
1369 KKASSERT(type < TTM_NUM_MEM_TYPES);
1370 man = &bdev->man[type];
1371 KKASSERT(!man->has_type);
1372 man->io_reserve_fastpath = true;
1373 man->use_io_reserve_lru = false;
1374 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1375 INIT_LIST_HEAD(&man->io_reserve_lru);
1377 ret = bdev->driver->init_mem_type(bdev, type, man);
1383 if (type != TTM_PL_SYSTEM) {
1384 ret = (*man->func->init)(man, p_size);
1388 man->has_type = true;
1389 man->use_type = true;
1392 INIT_LIST_HEAD(&man->lru);
1397 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1400 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1401 vm_page_free(glob->dummy_read_page);
1404 void ttm_bo_global_release(struct drm_global_reference *ref)
1406 struct ttm_bo_global *glob = ref->object;
1408 if (refcount_release(&glob->kobj_ref))
1409 ttm_bo_global_kobj_release(glob);
1412 int ttm_bo_global_init(struct drm_global_reference *ref)
1414 struct ttm_bo_global_ref *bo_ref =
1415 container_of(ref, struct ttm_bo_global_ref, ref);
1416 struct ttm_bo_global *glob = ref->object;
1419 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1420 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1421 glob->mem_glob = bo_ref->mem_glob;
1422 glob->dummy_read_page = vm_page_alloc_contig(NULL, 0,
1423 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ,
1424 1, 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, VM_MEMATTR_UNCACHEABLE);
1426 if (unlikely(glob->dummy_read_page == NULL)) {
1431 INIT_LIST_HEAD(&glob->swap_lru);
1432 INIT_LIST_HEAD(&glob->device_list);
1434 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1435 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1436 if (unlikely(ret != 0)) {
1437 kprintf("[TTM] Could not register buffer object swapout\n");
1441 atomic_set(&glob->bo_count, 0);
1443 refcount_init(&glob->kobj_ref, 1);
1447 vm_page_free(glob->dummy_read_page);
1449 drm_free(glob, M_DRM_GLOBAL);
1453 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1456 unsigned i = TTM_NUM_MEM_TYPES;
1457 struct ttm_mem_type_manager *man;
1458 struct ttm_bo_global *glob = bdev->glob;
1461 man = &bdev->man[i];
1462 if (man->has_type) {
1463 man->use_type = false;
1464 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1466 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1469 man->has_type = false;
1473 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1474 list_del(&bdev->device_list);
1475 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1477 if (taskqueue_cancel_timeout(taskqueue_thread, &bdev->wq, NULL))
1478 taskqueue_drain_timeout(taskqueue_thread, &bdev->wq);
1480 while (ttm_bo_delayed_delete(bdev, true))
1483 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1484 if (list_empty(&bdev->ddestroy))
1485 TTM_DEBUG("Delayed destroy list was clean\n");
1487 if (list_empty(&bdev->man[0].lru))
1488 TTM_DEBUG("Swap list was clean\n");
1489 lockmgr(&glob->lru_lock, LK_RELEASE);
1491 KKASSERT(drm_mm_clean(&bdev->addr_space_mm));
1492 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1493 drm_mm_takedown(&bdev->addr_space_mm);
1494 lockmgr(&bdev->vm_lock, LK_RELEASE);
1499 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1500 struct ttm_bo_global *glob,
1501 struct ttm_bo_driver *driver,
1502 uint64_t file_page_offset,
1507 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1508 bdev->driver = driver;
1510 memset(bdev->man, 0, sizeof(bdev->man));
1513 * Initialize the system memory buffer type.
1514 * Other types need to be driver / IOCTL initialized.
1516 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1517 if (unlikely(ret != 0))
1520 RB_INIT(&bdev->addr_space_rb);
1521 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1522 if (unlikely(ret != 0))
1523 goto out_no_addr_mm;
1525 TIMEOUT_TASK_INIT(taskqueue_thread, &bdev->wq, 0,
1526 ttm_bo_delayed_workqueue, bdev);
1527 INIT_LIST_HEAD(&bdev->ddestroy);
1528 bdev->dev_mapping = NULL;
1530 bdev->need_dma32 = need_dma32;
1532 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1533 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1534 list_add_tail(&bdev->device_list, &glob->device_list);
1535 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1539 ttm_bo_clean_mm(bdev, 0);
1545 * buffer object vm functions.
1548 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1550 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1552 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1553 if (mem->mem_type == TTM_PL_SYSTEM)
1556 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1559 if (mem->placement & TTM_PL_FLAG_CACHED)
1565 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1567 struct ttm_bo_device *bdev = bo->bdev;
1568 /* off_t offset = (off_t)bo->addr_space_offset;XXXKIB */
1569 /* off_t holelen = ((off_t)bo->mem.num_pages) << PAGE_SHIFT;XXXKIB */
1571 if (!bdev->dev_mapping)
1573 /* unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1); XXXKIB */
1574 ttm_mem_io_free_vm(bo);
1577 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1579 struct ttm_bo_device *bdev = bo->bdev;
1580 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1582 ttm_mem_io_lock(man, false);
1583 ttm_bo_unmap_virtual_locked(bo);
1584 ttm_mem_io_unlock(man);
1587 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1589 struct ttm_bo_device *bdev = bo->bdev;
1591 /* The caller acquired bdev->vm_lock. */
1592 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1598 * @bo: the buffer to allocate address space for
1600 * Allocate address space in the drm device so that applications
1601 * can mmap the buffer and access the contents. This only
1602 * applies to ttm_bo_type_device objects as others are not
1603 * placed in the drm device address space.
1606 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1608 struct ttm_bo_device *bdev = bo->bdev;
1612 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1613 if (unlikely(ret != 0))
1616 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1617 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1618 bo->mem.num_pages, 0, 0);
1620 if (unlikely(bo->vm_node == NULL)) {
1625 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1626 bo->mem.num_pages, 0);
1628 if (unlikely(bo->vm_node == NULL)) {
1629 lockmgr(&bdev->vm_lock, LK_RELEASE);
1633 ttm_bo_vm_insert_rb(bo);
1634 lockmgr(&bdev->vm_lock, LK_RELEASE);
1635 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1639 lockmgr(&bdev->vm_lock, LK_RELEASE);
1643 int ttm_bo_wait(struct ttm_buffer_object *bo,
1644 bool lazy, bool interruptible, bool no_wait)
1646 struct ttm_bo_driver *driver = bo->bdev->driver;
1647 struct ttm_bo_device *bdev = bo->bdev;
1651 if (likely(bo->sync_obj == NULL))
1654 while (bo->sync_obj) {
1656 if (driver->sync_obj_signaled(bo->sync_obj)) {
1657 void *tmp_obj = bo->sync_obj;
1658 bo->sync_obj = NULL;
1659 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1660 lockmgr(&bdev->fence_lock, LK_RELEASE);
1661 driver->sync_obj_unref(&tmp_obj);
1662 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1669 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1670 lockmgr(&bdev->fence_lock, LK_RELEASE);
1671 ret = driver->sync_obj_wait(sync_obj,
1672 lazy, interruptible);
1673 if (unlikely(ret != 0)) {
1674 driver->sync_obj_unref(&sync_obj);
1675 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1678 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1679 if (likely(bo->sync_obj == sync_obj)) {
1680 void *tmp_obj = bo->sync_obj;
1681 bo->sync_obj = NULL;
1682 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1684 lockmgr(&bdev->fence_lock, LK_RELEASE);
1685 driver->sync_obj_unref(&sync_obj);
1686 driver->sync_obj_unref(&tmp_obj);
1687 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1689 lockmgr(&bdev->fence_lock, LK_RELEASE);
1690 driver->sync_obj_unref(&sync_obj);
1691 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1697 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1699 struct ttm_bo_device *bdev = bo->bdev;
1703 * Using ttm_bo_reserve makes sure the lru lists are updated.
1706 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1707 if (unlikely(ret != 0))
1709 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1710 ret = ttm_bo_wait(bo, false, true, no_wait);
1711 lockmgr(&bdev->fence_lock, LK_RELEASE);
1712 if (likely(ret == 0))
1713 atomic_inc(&bo->cpu_writers);
1714 ttm_bo_unreserve(bo);
1718 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1720 atomic_dec(&bo->cpu_writers);
1724 * A buffer object shrink method that tries to swap out the first
1725 * buffer object on the bo_global::swap_lru list.
1728 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1730 struct ttm_bo_global *glob =
1731 container_of(shrink, struct ttm_bo_global, shrink);
1732 struct ttm_buffer_object *bo;
1735 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1737 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1738 list_for_each_entry(bo, &glob->swap_lru, swap) {
1739 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1745 lockmgr(&glob->lru_lock, LK_RELEASE);
1749 refcount_acquire(&bo->list_kref);
1751 if (!list_empty(&bo->ddestroy)) {
1752 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1753 if (refcount_release(&bo->list_kref))
1754 ttm_bo_release_list(bo);
1758 put_count = ttm_bo_del_from_lru(bo);
1759 lockmgr(&glob->lru_lock, LK_RELEASE);
1761 ttm_bo_list_ref_sub(bo, put_count, true);
1764 * Wait for GPU, then move to system cached.
1767 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1768 ret = ttm_bo_wait(bo, false, false, false);
1769 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1771 if (unlikely(ret != 0))
1774 if ((bo->mem.placement & swap_placement) != swap_placement) {
1775 struct ttm_mem_reg evict_mem;
1777 evict_mem = bo->mem;
1778 evict_mem.mm_node = NULL;
1779 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1780 evict_mem.mem_type = TTM_PL_SYSTEM;
1782 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1784 if (unlikely(ret != 0))
1788 ttm_bo_unmap_virtual(bo);
1791 * Swap out. Buffer will be swapped in again as soon as
1792 * anyone tries to access a ttm page.
1795 if (bo->bdev->driver->swap_notify)
1796 bo->bdev->driver->swap_notify(bo);
1798 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1803 * Unreserve without putting on LRU to avoid swapping out an
1804 * already swapped buffer.
1807 atomic_set(&bo->reserved, 0);
1809 if (refcount_release(&bo->list_kref))
1810 ttm_bo_release_list(bo);
1814 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1816 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)