1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #define pr_fmt(fmt) "[TTM] " fmt
33 #include <drm/ttm/ttm_module.h>
34 #include <drm/ttm/ttm_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/atomic.h>
37 #include <linux/errno.h>
38 #include <linux/export.h>
39 #include <linux/wait.h>
41 #define TTM_ASSERT_LOCKED(param)
42 #define TTM_DEBUG(fmt, arg...)
43 #define TTM_BO_HASH_ORDER 13
45 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
46 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
47 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob);
49 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
53 for (i = 0; i <= TTM_PL_PRIV5; i++)
54 if (flags & (1 << i)) {
61 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
63 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
65 kprintf(" has_type: %d\n", man->has_type);
66 kprintf(" use_type: %d\n", man->use_type);
67 kprintf(" flags: 0x%08X\n", man->flags);
68 kprintf(" gpu_offset: 0x%08lX\n", man->gpu_offset);
69 kprintf(" size: %ju\n", (uintmax_t)man->size);
70 kprintf(" available_caching: 0x%08X\n", man->available_caching);
71 kprintf(" default_caching: 0x%08X\n", man->default_caching);
72 if (mem_type != TTM_PL_SYSTEM)
73 (*man->func->debug)(man, TTM_PFX);
76 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
77 struct ttm_placement *placement)
81 kprintf("No space for %p (%lu pages, %luK, %luM)\n",
82 bo, bo->mem.num_pages, bo->mem.size >> 10,
84 for (i = 0; i < placement->num_placement; i++) {
85 ret = ttm_mem_type_from_flags(placement->placement[i],
89 kprintf(" placement[%d]=0x%08X (%d)\n",
90 i, placement->placement[i], mem_type);
91 ttm_mem_type_debug(bo->bdev, mem_type);
96 static ssize_t ttm_bo_global_show(struct ttm_bo_global *glob,
100 return snprintf(buffer, PAGE_SIZE, "%lu\n",
101 (unsigned long) atomic_read(&glob->bo_count));
105 static inline uint32_t ttm_bo_type_flags(unsigned type)
110 static void ttm_bo_release_list(struct kref *list_kref)
112 struct ttm_buffer_object *bo =
113 container_of(list_kref, struct ttm_buffer_object, list_kref);
114 struct ttm_bo_device *bdev = bo->bdev;
115 size_t acc_size = bo->acc_size;
117 BUG_ON(atomic_read(&bo->list_kref.refcount));
118 BUG_ON(atomic_read(&bo->kref.refcount));
119 BUG_ON(atomic_read(&bo->cpu_writers));
120 BUG_ON(bo->sync_obj != NULL);
121 BUG_ON(bo->mem.mm_node != NULL);
122 BUG_ON(!list_empty(&bo->lru));
123 BUG_ON(!list_empty(&bo->ddestroy));
126 ttm_tt_destroy(bo->ttm);
127 atomic_dec(&bo->glob->bo_count);
133 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
136 static int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,
140 return wait_event_interruptible(bo->event_queue,
141 !ttm_bo_is_reserved(bo));
143 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
148 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
150 struct ttm_bo_device *bdev = bo->bdev;
151 struct ttm_mem_type_manager *man;
153 BUG_ON(!ttm_bo_is_reserved(bo));
155 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
157 BUG_ON(!list_empty(&bo->lru));
159 man = &bdev->man[bo->mem.mem_type];
160 list_add_tail(&bo->lru, &man->lru);
161 kref_get(&bo->list_kref);
163 if (bo->ttm != NULL) {
164 list_add_tail(&bo->swap, &bo->glob->swap_lru);
165 kref_get(&bo->list_kref);
170 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
174 if (!list_empty(&bo->swap)) {
175 list_del_init(&bo->swap);
178 if (!list_empty(&bo->lru)) {
179 list_del_init(&bo->lru);
184 * TODO: Add a driver hook to delete from
185 * driver-specific LRU's here.
191 int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
193 bool no_wait, bool use_sequence, uint32_t sequence)
197 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
199 * Deadlock avoidance for multi-bo reserving.
201 if (use_sequence && bo->seq_valid) {
203 * We've already reserved this one.
205 if (unlikely(sequence == bo->val_seq))
208 * Already reserved by a thread that will not back
209 * off for us. We need to back off.
211 if (unlikely(sequence - bo->val_seq < (1U << 31)))
218 ret = ttm_bo_wait_unreserved(bo, interruptible);
225 bool wake_up = false;
227 * Wake up waiters that may need to recheck for deadlock,
228 * if we decreased the sequence number.
230 if (unlikely((bo->val_seq - sequence < (1U << 31))
235 * In the worst case with memory ordering these values can be
236 * seen in the wrong order. However since we call wake_up_all
237 * in that case, this will hopefully not pose a problem,
238 * and the worst case would only cause someone to accidentally
239 * hit -EAGAIN in ttm_bo_reserve when they see old value of
240 * val_seq. However this would only happen if seq_valid was
241 * written before val_seq was, and just means some slightly
242 * increased cpu usage
244 bo->val_seq = sequence;
245 bo->seq_valid = true;
247 wake_up_all(&bo->event_queue);
249 bo->seq_valid = false;
254 EXPORT_SYMBOL(ttm_bo_reserve);
256 static void ttm_bo_ref_bug(struct kref *list_kref)
261 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
264 kref_sub(&bo->list_kref, count,
265 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
268 int ttm_bo_reserve(struct ttm_buffer_object *bo,
270 bool no_wait, bool use_sequence, uint32_t sequence)
272 struct ttm_bo_global *glob = bo->glob;
276 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
277 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_sequence,
279 if (likely(ret == 0)) {
280 put_count = ttm_bo_del_from_lru(bo);
281 lockmgr(&glob->lru_lock, LK_RELEASE);
282 ttm_bo_list_ref_sub(bo, put_count, true);
284 lockmgr(&glob->lru_lock, LK_RELEASE);
291 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
292 bool interruptible, uint32_t sequence)
294 bool wake_up = false;
297 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
298 WARN_ON(bo->seq_valid && sequence == bo->val_seq);
300 ret = ttm_bo_wait_unreserved(bo, interruptible);
306 if ((bo->val_seq - sequence < (1U << 31)) || !bo->seq_valid)
310 * Wake up waiters that may need to recheck for deadlock,
311 * if we decreased the sequence number.
313 bo->val_seq = sequence;
314 bo->seq_valid = true;
316 wake_up_all(&bo->event_queue);
321 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
322 bool interruptible, uint32_t sequence)
324 struct ttm_bo_global *glob = bo->glob;
327 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
328 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence);
330 put_count = ttm_bo_del_from_lru(bo);
331 lockmgr(&glob->lru_lock, LK_RELEASE);
332 ttm_bo_list_ref_sub(bo, put_count, true);
334 lockmgr(&glob->lru_lock, LK_RELEASE);
338 EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
340 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
342 ttm_bo_add_to_lru(bo);
343 atomic_set(&bo->reserved, 0);
344 wake_up_all(&bo->event_queue);
347 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
349 struct ttm_bo_global *glob = bo->glob;
351 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
352 ttm_bo_unreserve_locked(bo);
353 lockmgr(&glob->lru_lock, LK_RELEASE);
355 EXPORT_SYMBOL(ttm_bo_unreserve);
358 * Call bo->mutex locked.
360 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
362 struct ttm_bo_device *bdev = bo->bdev;
363 struct ttm_bo_global *glob = bo->glob;
365 uint32_t page_flags = 0;
367 TTM_ASSERT_LOCKED(&bo->mutex);
370 if (bdev->need_dma32)
371 page_flags |= TTM_PAGE_FLAG_DMA32;
374 case ttm_bo_type_device:
376 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
377 case ttm_bo_type_kernel:
378 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
379 page_flags, glob->dummy_read_page);
380 if (unlikely(bo->ttm == NULL))
384 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
385 page_flags | TTM_PAGE_FLAG_SG,
386 glob->dummy_read_page);
387 if (unlikely(bo->ttm == NULL)) {
391 bo->ttm->sg = bo->sg;
394 kprintf("[TTM] Illegal buffer object type\n");
402 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
403 struct ttm_mem_reg *mem,
404 bool evict, bool interruptible,
407 struct ttm_bo_device *bdev = bo->bdev;
408 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
409 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
410 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
411 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
414 if (old_is_pci || new_is_pci ||
415 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
416 ret = ttm_mem_io_lock(old_man, true);
417 if (unlikely(ret != 0))
419 ttm_bo_unmap_virtual_locked(bo);
420 ttm_mem_io_unlock(old_man);
424 * Create and bind a ttm if required.
427 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
428 if (bo->ttm == NULL) {
429 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
430 ret = ttm_bo_add_ttm(bo, zero);
435 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
439 if (mem->mem_type != TTM_PL_SYSTEM) {
440 ret = ttm_tt_bind(bo->ttm, mem);
445 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
446 if (bdev->driver->move_notify)
447 bdev->driver->move_notify(bo, mem);
454 if (bdev->driver->move_notify)
455 bdev->driver->move_notify(bo, mem);
457 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
458 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
459 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
460 else if (bdev->driver->move)
461 ret = bdev->driver->move(bo, evict, interruptible,
464 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
467 if (bdev->driver->move_notify) {
468 struct ttm_mem_reg tmp_mem = *mem;
471 bdev->driver->move_notify(bo, mem);
481 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
483 kprintf("[TTM] Can not flush read caches\n");
487 if (bo->mem.mm_node) {
488 bo->offset = (bo->mem.start << PAGE_SHIFT) +
489 bdev->man[bo->mem.mem_type].gpu_offset;
490 bo->cur_placement = bo->mem.placement;
497 new_man = &bdev->man[bo->mem.mem_type];
498 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
499 ttm_tt_unbind(bo->ttm);
500 ttm_tt_destroy(bo->ttm);
509 * Will release GPU memory type usage on destruction.
510 * This is the place to put in driver specific hooks to release
511 * driver private resources.
512 * Will release the bo::reserved lock.
515 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
517 if (bo->bdev->driver->move_notify)
518 bo->bdev->driver->move_notify(bo, NULL);
521 ttm_tt_unbind(bo->ttm);
522 ttm_tt_destroy(bo->ttm);
525 ttm_bo_mem_put(bo, &bo->mem);
527 atomic_set(&bo->reserved, 0);
528 wake_up_all(&bo->event_queue);
531 * Since the final reference to this bo may not be dropped by
532 * the current task we have to put a memory barrier here to make
533 * sure the changes done in this function are always visible.
535 * This function only needs protection against the final kref_put.
540 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
542 struct ttm_bo_device *bdev = bo->bdev;
543 struct ttm_bo_global *glob = bo->glob;
544 struct ttm_bo_driver *driver = bdev->driver;
545 void *sync_obj = NULL;
549 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
550 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
552 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
553 (void) ttm_bo_wait(bo, false, false, true);
554 if (!ret && !bo->sync_obj) {
555 lockmgr(&bdev->fence_lock, LK_RELEASE);
556 put_count = ttm_bo_del_from_lru(bo);
558 lockmgr(&glob->lru_lock, LK_RELEASE);
559 ttm_bo_cleanup_memtype_use(bo);
561 ttm_bo_list_ref_sub(bo, put_count, true);
566 sync_obj = driver->sync_obj_ref(bo->sync_obj);
567 lockmgr(&bdev->fence_lock, LK_RELEASE);
570 atomic_set(&bo->reserved, 0);
571 wake_up_all(&bo->event_queue);
574 kref_get(&bo->list_kref);
575 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
576 lockmgr(&glob->lru_lock, LK_RELEASE);
579 driver->sync_obj_flush(sync_obj);
580 driver->sync_obj_unref(&sync_obj);
582 schedule_delayed_work(&bdev->wq,
583 ((hz / 100) < 1) ? 1 : hz / 100);
587 * function ttm_bo_cleanup_refs_and_unlock
588 * If bo idle, remove from delayed- and lru lists, and unref.
589 * If not idle, do nothing.
591 * Must be called with lru_lock and reservation held, this function
592 * will drop both before returning.
594 * @interruptible Any sleeps should occur interruptibly.
595 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
598 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
602 struct ttm_bo_device *bdev = bo->bdev;
603 struct ttm_bo_driver *driver = bdev->driver;
604 struct ttm_bo_global *glob = bo->glob;
608 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
609 ret = ttm_bo_wait(bo, false, false, true);
611 if (ret && !no_wait_gpu) {
615 * Take a reference to the fence and unreserve,
616 * at this point the buffer should be dead, so
617 * no new sync objects can be attached.
619 sync_obj = driver->sync_obj_ref(bo->sync_obj);
620 lockmgr(&bdev->fence_lock, LK_RELEASE);
622 atomic_set(&bo->reserved, 0);
623 wake_up_all(&bo->event_queue);
624 lockmgr(&glob->lru_lock, LK_RELEASE);
626 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
627 driver->sync_obj_unref(&sync_obj);
632 * remove sync_obj with ttm_bo_wait, the wait should be
633 * finished, and no new wait object should have been added.
635 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
636 ret = ttm_bo_wait(bo, false, false, true);
638 lockmgr(&bdev->fence_lock, LK_RELEASE);
642 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
643 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
646 * We raced, and lost, someone else holds the reservation now,
647 * and is probably busy in ttm_bo_cleanup_memtype_use.
649 * Even if it's not the case, because we finished waiting any
650 * delayed destruction would succeed, so just return success
654 lockmgr(&glob->lru_lock, LK_RELEASE);
658 lockmgr(&bdev->fence_lock, LK_RELEASE);
660 if (ret || unlikely(list_empty(&bo->ddestroy))) {
661 atomic_set(&bo->reserved, 0);
662 wake_up_all(&bo->event_queue);
663 lockmgr(&glob->lru_lock, LK_RELEASE);
667 put_count = ttm_bo_del_from_lru(bo);
668 list_del_init(&bo->ddestroy);
671 lockmgr(&glob->lru_lock, LK_RELEASE);
672 ttm_bo_cleanup_memtype_use(bo);
674 ttm_bo_list_ref_sub(bo, put_count, true);
680 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
681 * encountered buffers.
684 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
686 struct ttm_bo_global *glob = bdev->glob;
687 struct ttm_buffer_object *entry = NULL;
690 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
691 if (list_empty(&bdev->ddestroy))
694 entry = list_first_entry(&bdev->ddestroy,
695 struct ttm_buffer_object, ddestroy);
696 kref_get(&entry->list_kref);
699 struct ttm_buffer_object *nentry = NULL;
701 if (entry->ddestroy.next != &bdev->ddestroy) {
702 nentry = list_first_entry(&entry->ddestroy,
703 struct ttm_buffer_object, ddestroy);
704 kref_get(&nentry->list_kref);
707 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
708 if (remove_all && ret) {
709 ret = ttm_bo_reserve_nolru(entry, false, false,
714 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
717 lockmgr(&glob->lru_lock, LK_RELEASE);
719 kref_put(&entry->list_kref, ttm_bo_release_list);
725 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
726 if (list_empty(&entry->ddestroy))
731 lockmgr(&glob->lru_lock, LK_RELEASE);
734 kref_put(&entry->list_kref, ttm_bo_release_list);
738 static void ttm_bo_delayed_workqueue(struct work_struct *work)
740 struct ttm_bo_device *bdev =
741 container_of(work, struct ttm_bo_device, wq.work);
743 if (ttm_bo_delayed_delete(bdev, false)) {
744 schedule_delayed_work(&bdev->wq,
745 ((hz / 100) < 1) ? 1 : hz / 100);
750 * NOTE: bdev->vm_lock already held on call, this function release it.
752 static void ttm_bo_release(struct kref *kref)
754 struct ttm_buffer_object *bo =
755 container_of(kref, struct ttm_buffer_object, kref);
756 struct ttm_bo_device *bdev = bo->bdev;
757 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
760 if (atomic_read(&bo->kref.refcount) > 0) {
761 lockmgr(&bdev->vm_lock, LK_RELEASE);
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);
772 * Should we clean up our implied list_kref? Because ttm_bo_release()
773 * can be called reentrantly due to races (this may not be true any
774 * more with the lock management changes in the deref), it is possible
775 * to get here twice, but there's only one list_kref ref to drop and
776 * in the other path 'bo' can be kfree()d by another thread the
777 * instant we release our lock.
779 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
780 if (release_active) {
781 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
782 lockmgr(&bdev->vm_lock, LK_RELEASE);
783 ttm_mem_io_lock(man, false);
784 ttm_mem_io_free_vm(bo);
785 ttm_mem_io_unlock(man);
786 ttm_bo_cleanup_refs_or_queue(bo);
787 kref_put(&bo->list_kref, ttm_bo_release_list);
789 lockmgr(&bdev->vm_lock, LK_RELEASE);
793 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
795 struct ttm_buffer_object *bo = *p_bo;
796 struct ttm_bo_device *bdev = bo->bdev;
799 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
800 if (kref_put(&bo->kref, ttm_bo_release) == 0)
801 lockmgr(&bdev->vm_lock, LK_RELEASE);
803 EXPORT_SYMBOL(ttm_bo_unref);
805 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
807 return cancel_delayed_work_sync(&bdev->wq);
809 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
811 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
814 schedule_delayed_work(&bdev->wq,
815 ((hz / 100) < 1) ? 1 : hz / 100);
817 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
819 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
822 struct ttm_bo_device *bdev = bo->bdev;
823 struct ttm_mem_reg evict_mem;
824 struct ttm_placement placement;
827 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
828 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
829 lockmgr(&bdev->fence_lock, LK_RELEASE);
831 if (unlikely(ret != 0)) {
832 if (ret != -ERESTARTSYS) {
833 pr_err("Failed to expire sync object before buffer eviction\n");
838 BUG_ON(!ttm_bo_is_reserved(bo));
841 evict_mem.mm_node = NULL;
842 evict_mem.bus.io_reserved_vm = false;
843 evict_mem.bus.io_reserved_count = 0;
847 placement.num_placement = 0;
848 placement.num_busy_placement = 0;
849 bdev->driver->evict_flags(bo, &placement);
850 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
853 if (ret != -ERESTARTSYS) {
854 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
856 ttm_bo_mem_space_debug(bo, &placement);
861 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
864 if (ret != -ERESTARTSYS)
865 pr_err("Buffer eviction failed\n");
866 ttm_bo_mem_put(bo, &evict_mem);
874 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
879 struct ttm_bo_global *glob = bdev->glob;
880 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
881 struct ttm_buffer_object *bo;
882 int ret = -EBUSY, put_count;
884 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
885 list_for_each_entry(bo, &man->lru, lru) {
886 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
892 lockmgr(&glob->lru_lock, LK_RELEASE);
896 kref_get(&bo->list_kref);
898 if (!list_empty(&bo->ddestroy)) {
899 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
901 kref_put(&bo->list_kref, ttm_bo_release_list);
905 put_count = ttm_bo_del_from_lru(bo);
906 lockmgr(&glob->lru_lock, LK_RELEASE);
910 ttm_bo_list_ref_sub(bo, put_count, true);
912 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
913 ttm_bo_unreserve(bo);
915 kref_put(&bo->list_kref, ttm_bo_release_list);
919 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
921 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
924 (*man->func->put_node)(man, mem);
926 EXPORT_SYMBOL(ttm_bo_mem_put);
929 * Repeatedly evict memory from the LRU for @mem_type until we create enough
930 * space, or we've evicted everything and there isn't enough space.
932 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
934 struct ttm_placement *placement,
935 struct ttm_mem_reg *mem,
939 struct ttm_bo_device *bdev = bo->bdev;
940 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
944 ret = (*man->func->get_node)(man, bo, placement, mem);
945 if (unlikely(ret != 0))
949 ret = ttm_mem_evict_first(bdev, mem_type,
950 interruptible, no_wait_gpu);
951 if (unlikely(ret != 0))
954 if (mem->mm_node == NULL)
956 mem->mem_type = mem_type;
960 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
961 uint32_t cur_placement,
962 uint32_t proposed_placement)
964 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
965 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
968 * Keep current caching if possible.
971 if ((cur_placement & caching) != 0)
972 result |= (cur_placement & caching);
973 else if ((man->default_caching & caching) != 0)
974 result |= man->default_caching;
975 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
976 result |= TTM_PL_FLAG_CACHED;
977 else if ((TTM_PL_FLAG_WC & caching) != 0)
978 result |= TTM_PL_FLAG_WC;
979 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
980 result |= TTM_PL_FLAG_UNCACHED;
985 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
987 uint32_t proposed_placement,
988 uint32_t *masked_placement)
990 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
992 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
995 if ((proposed_placement & man->available_caching) == 0)
998 cur_flags |= (proposed_placement & man->available_caching);
1000 *masked_placement = cur_flags;
1005 * Creates space for memory region @mem according to its type.
1007 * This function first searches for free space in compatible memory types in
1008 * the priority order defined by the driver. If free space isn't found, then
1009 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1012 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1013 struct ttm_placement *placement,
1014 struct ttm_mem_reg *mem,
1018 struct ttm_bo_device *bdev = bo->bdev;
1019 struct ttm_mem_type_manager *man;
1020 uint32_t mem_type = TTM_PL_SYSTEM;
1021 uint32_t cur_flags = 0;
1022 bool type_found = false;
1023 bool type_ok = false;
1024 bool has_erestartsys = false;
1027 mem->mm_node = NULL;
1028 for (i = 0; i < placement->num_placement; ++i) {
1029 ret = ttm_mem_type_from_flags(placement->placement[i],
1033 man = &bdev->man[mem_type];
1035 type_ok = ttm_bo_mt_compatible(man,
1037 placement->placement[i],
1043 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1046 * Use the access and other non-mapping-related flag bits from
1047 * the memory placement flags to the current flags
1049 ttm_flag_masked(&cur_flags, placement->placement[i],
1050 ~TTM_PL_MASK_MEMTYPE);
1052 if (mem_type == TTM_PL_SYSTEM)
1055 if (man->has_type && man->use_type) {
1057 ret = (*man->func->get_node)(man, bo, placement, mem);
1065 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1066 mem->mem_type = mem_type;
1067 mem->placement = cur_flags;
1074 for (i = 0; i < placement->num_busy_placement; ++i) {
1075 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1079 man = &bdev->man[mem_type];
1082 if (!ttm_bo_mt_compatible(man,
1084 placement->busy_placement[i],
1088 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1091 * Use the access and other non-mapping-related flag bits from
1092 * the memory placement flags to the current flags
1094 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1095 ~TTM_PL_MASK_MEMTYPE);
1098 if (mem_type == TTM_PL_SYSTEM) {
1099 mem->mem_type = mem_type;
1100 mem->placement = cur_flags;
1101 mem->mm_node = NULL;
1105 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1106 interruptible, no_wait_gpu);
1107 if (ret == 0 && mem->mm_node) {
1108 mem->placement = cur_flags;
1111 if (ret == -ERESTARTSYS)
1112 has_erestartsys = true;
1114 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1117 EXPORT_SYMBOL(ttm_bo_mem_space);
1120 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1121 struct ttm_placement *placement,
1126 struct ttm_mem_reg mem;
1127 struct ttm_bo_device *bdev = bo->bdev;
1129 BUG_ON(!ttm_bo_is_reserved(bo));
1132 * FIXME: It's possible to pipeline buffer moves.
1133 * Have the driver move function wait for idle when necessary,
1134 * instead of doing it here.
1136 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1137 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1138 lockmgr(&bdev->fence_lock, LK_RELEASE);
1141 mem.num_pages = bo->num_pages;
1142 mem.size = mem.num_pages << PAGE_SHIFT;
1143 mem.page_alignment = bo->mem.page_alignment;
1144 mem.bus.io_reserved_vm = false;
1145 mem.bus.io_reserved_count = 0;
1147 * Determine where to move the buffer.
1149 ret = ttm_bo_mem_space(bo, placement, &mem,
1150 interruptible, no_wait_gpu);
1153 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1154 interruptible, no_wait_gpu);
1156 if (ret && mem.mm_node)
1157 ttm_bo_mem_put(bo, &mem);
1161 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1162 struct ttm_mem_reg *mem)
1166 if (mem->mm_node && placement->lpfn != 0 &&
1167 (mem->start < placement->fpfn ||
1168 mem->start + mem->num_pages > placement->lpfn))
1171 for (i = 0; i < placement->num_placement; i++) {
1172 if ((placement->placement[i] & mem->placement &
1173 TTM_PL_MASK_CACHING) &&
1174 (placement->placement[i] & mem->placement &
1181 int ttm_bo_validate(struct ttm_buffer_object *bo,
1182 struct ttm_placement *placement,
1188 BUG_ON(!ttm_bo_is_reserved(bo));
1189 /* Check that range is valid */
1190 if (placement->lpfn || placement->fpfn)
1191 if (placement->fpfn > placement->lpfn ||
1192 (placement->lpfn - placement->fpfn) < bo->num_pages)
1195 * Check whether we need to move buffer.
1197 ret = ttm_bo_mem_compat(placement, &bo->mem);
1199 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1205 * Use the access and other non-mapping-related flag bits from
1206 * the compatible memory placement flags to the active flags
1208 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1209 ~TTM_PL_MASK_MEMTYPE);
1212 * We might need to add a TTM.
1214 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1215 ret = ttm_bo_add_ttm(bo, true);
1221 EXPORT_SYMBOL(ttm_bo_validate);
1223 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1224 struct ttm_placement *placement)
1226 BUG_ON((placement->fpfn || placement->lpfn) &&
1227 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1232 int ttm_bo_init(struct ttm_bo_device *bdev,
1233 struct ttm_buffer_object *bo,
1235 enum ttm_bo_type type,
1236 struct ttm_placement *placement,
1237 uint32_t page_alignment,
1239 struct vm_object *persistent_swap_storage,
1241 struct sg_table *sg,
1242 void (*destroy) (struct ttm_buffer_object *))
1245 unsigned long num_pages;
1246 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1248 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1250 kprintf("[TTM] Out of kernel memory\n");
1258 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1259 if (num_pages == 0) {
1260 kprintf("[TTM] Illegal buffer object size\n");
1265 ttm_mem_global_free(mem_glob, acc_size);
1268 bo->destroy = destroy;
1270 kref_init(&bo->kref);
1271 kref_init(&bo->list_kref);
1272 atomic_set(&bo->cpu_writers, 0);
1273 atomic_set(&bo->reserved, 1);
1274 init_waitqueue_head(&bo->event_queue);
1275 INIT_LIST_HEAD(&bo->lru);
1276 INIT_LIST_HEAD(&bo->ddestroy);
1277 INIT_LIST_HEAD(&bo->swap);
1278 INIT_LIST_HEAD(&bo->io_reserve_lru);
1279 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1281 bo->glob = bdev->glob;
1283 bo->num_pages = num_pages;
1284 bo->mem.size = num_pages << PAGE_SHIFT;
1285 bo->mem.mem_type = TTM_PL_SYSTEM;
1286 bo->mem.num_pages = bo->num_pages;
1287 bo->mem.mm_node = NULL;
1288 bo->mem.page_alignment = page_alignment;
1289 bo->mem.bus.io_reserved_vm = false;
1290 bo->mem.bus.io_reserved_count = 0;
1292 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1293 bo->seq_valid = false;
1294 bo->persistent_swap_storage = persistent_swap_storage;
1295 bo->acc_size = acc_size;
1297 atomic_inc(&bo->glob->bo_count);
1300 * Mirror ref from kref_init() for list_kref.
1302 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1304 ret = ttm_bo_check_placement(bo, placement);
1305 if (unlikely(ret != 0))
1309 * For ttm_bo_type_device buffers, allocate
1310 * address space from the device.
1312 if (bo->type == ttm_bo_type_device ||
1313 bo->type == ttm_bo_type_sg) {
1314 ret = ttm_bo_setup_vm(bo);
1319 ret = ttm_bo_validate(bo, placement, interruptible, false);
1323 ttm_bo_unreserve(bo);
1327 ttm_bo_unreserve(bo);
1332 EXPORT_SYMBOL(ttm_bo_init);
1334 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1335 unsigned long bo_size,
1336 unsigned struct_size)
1338 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1341 size += ttm_round_pot(struct_size);
1342 size += PAGE_ALIGN(npages * sizeof(void *));
1343 size += ttm_round_pot(sizeof(struct ttm_tt));
1346 EXPORT_SYMBOL(ttm_bo_acc_size);
1348 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1349 unsigned long bo_size,
1350 unsigned struct_size)
1352 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1355 size += ttm_round_pot(struct_size);
1356 size += PAGE_ALIGN(npages * sizeof(void *));
1357 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1358 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1361 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1363 int ttm_bo_create(struct ttm_bo_device *bdev,
1365 enum ttm_bo_type type,
1366 struct ttm_placement *placement,
1367 uint32_t page_alignment,
1369 struct vm_object *persistent_swap_storage,
1370 struct ttm_buffer_object **p_bo)
1372 struct ttm_buffer_object *bo;
1377 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1378 if (unlikely(bo == NULL))
1381 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1382 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1383 interruptible, persistent_swap_storage, acc_size,
1385 if (likely(ret == 0))
1390 EXPORT_SYMBOL(ttm_bo_create);
1392 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1393 unsigned mem_type, bool allow_errors)
1395 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1396 struct ttm_bo_global *glob = bdev->glob;
1400 * Can't use standard list traversal since we're unlocking.
1403 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1404 while (!list_empty(&man->lru)) {
1405 lockmgr(&glob->lru_lock, LK_RELEASE);
1406 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1411 kprintf("[TTM] Cleanup eviction failed\n");
1414 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1416 lockmgr(&glob->lru_lock, LK_RELEASE);
1420 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1422 struct ttm_mem_type_manager *man;
1425 if (mem_type >= TTM_NUM_MEM_TYPES) {
1426 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1429 man = &bdev->man[mem_type];
1431 if (!man->has_type) {
1432 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1437 man->use_type = false;
1438 man->has_type = false;
1442 ttm_bo_force_list_clean(bdev, mem_type, false);
1444 ret = (*man->func->takedown)(man);
1449 EXPORT_SYMBOL(ttm_bo_clean_mm);
1451 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1453 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1455 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1456 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1460 if (!man->has_type) {
1461 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1465 return ttm_bo_force_list_clean(bdev, mem_type, true);
1467 EXPORT_SYMBOL(ttm_bo_evict_mm);
1469 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1470 unsigned long p_size)
1473 struct ttm_mem_type_manager *man;
1475 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1476 man = &bdev->man[type];
1477 BUG_ON(man->has_type);
1478 man->io_reserve_fastpath = true;
1479 man->use_io_reserve_lru = false;
1480 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1481 INIT_LIST_HEAD(&man->io_reserve_lru);
1483 ret = bdev->driver->init_mem_type(bdev, type, man);
1489 if (type != TTM_PL_SYSTEM) {
1490 ret = (*man->func->init)(man, p_size);
1494 man->has_type = true;
1495 man->use_type = true;
1498 INIT_LIST_HEAD(&man->lru);
1502 EXPORT_SYMBOL(ttm_bo_init_mm);
1504 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1506 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1507 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1508 glob->dummy_read_page = NULL;
1510 vm_page_free(glob->dummy_read_page);
1514 void ttm_bo_global_release(struct drm_global_reference *ref)
1516 struct ttm_bo_global *glob = ref->object;
1518 if (refcount_release(&glob->kobj_ref))
1519 ttm_bo_global_kobj_release(glob);
1521 EXPORT_SYMBOL(ttm_bo_global_release);
1523 int ttm_bo_global_init(struct drm_global_reference *ref)
1525 struct ttm_bo_global_ref *bo_ref =
1526 container_of(ref, struct ttm_bo_global_ref, ref);
1527 struct ttm_bo_global *glob = ref->object;
1530 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1531 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1532 glob->mem_glob = bo_ref->mem_glob;
1533 glob->dummy_read_page = vm_page_alloc_contig(
1534 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1536 if (unlikely(glob->dummy_read_page == NULL)) {
1541 INIT_LIST_HEAD(&glob->swap_lru);
1542 INIT_LIST_HEAD(&glob->device_list);
1544 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1545 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1546 if (unlikely(ret != 0)) {
1547 kprintf("[TTM] Could not register buffer object swapout\n");
1551 atomic_set(&glob->bo_count, 0);
1553 refcount_init(&glob->kobj_ref, 1);
1557 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1558 glob->dummy_read_page = NULL;
1560 vm_page_free(glob->dummy_read_page);
1566 EXPORT_SYMBOL(ttm_bo_global_init);
1569 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1572 unsigned i = TTM_NUM_MEM_TYPES;
1573 struct ttm_mem_type_manager *man;
1574 struct ttm_bo_global *glob = bdev->glob;
1577 man = &bdev->man[i];
1578 if (man->has_type) {
1579 man->use_type = false;
1580 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1582 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1585 man->has_type = false;
1589 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1590 list_del(&bdev->device_list);
1591 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1593 cancel_delayed_work_sync(&bdev->wq);
1595 while (ttm_bo_delayed_delete(bdev, true))
1598 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1599 if (list_empty(&bdev->ddestroy))
1600 TTM_DEBUG("Delayed destroy list was clean\n");
1602 if (list_empty(&bdev->man[0].lru))
1603 TTM_DEBUG("Swap list was clean\n");
1604 lockmgr(&glob->lru_lock, LK_RELEASE);
1606 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1607 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1608 drm_mm_takedown(&bdev->addr_space_mm);
1609 lockmgr(&bdev->vm_lock, LK_RELEASE);
1613 EXPORT_SYMBOL(ttm_bo_device_release);
1615 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1616 struct ttm_bo_global *glob,
1617 struct ttm_bo_driver *driver,
1618 uint64_t file_page_offset,
1623 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1624 bdev->driver = driver;
1626 memset(bdev->man, 0, sizeof(bdev->man));
1629 * Initialize the system memory buffer type.
1630 * Other types need to be driver / IOCTL initialized.
1632 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1633 if (unlikely(ret != 0))
1636 RB_INIT(&bdev->addr_space_rb);
1637 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1638 if (unlikely(ret != 0))
1639 goto out_no_addr_mm;
1641 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1642 INIT_LIST_HEAD(&bdev->ddestroy);
1643 bdev->dev_mapping = NULL;
1645 bdev->need_dma32 = need_dma32;
1647 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1648 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1649 list_add_tail(&bdev->device_list, &glob->device_list);
1650 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1654 ttm_bo_clean_mm(bdev, 0);
1658 EXPORT_SYMBOL(ttm_bo_device_init);
1661 * buffer object vm functions.
1664 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1666 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1668 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1669 if (mem->mem_type == TTM_PL_SYSTEM)
1672 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1675 if (mem->placement & TTM_PL_FLAG_CACHED)
1681 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1684 ttm_bo_release_mmap(bo);
1685 ttm_mem_io_free_vm(bo);
1688 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1690 struct ttm_bo_device *bdev = bo->bdev;
1691 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1693 ttm_mem_io_lock(man, false);
1694 ttm_bo_unmap_virtual_locked(bo);
1695 ttm_mem_io_unlock(man);
1699 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1701 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1703 struct ttm_bo_device *bdev = bo->bdev;
1705 /* The caller acquired bdev->vm_lock. */
1706 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1712 * @bo: the buffer to allocate address space for
1714 * Allocate address space in the drm device so that applications
1715 * can mmap the buffer and access the contents. This only
1716 * applies to ttm_bo_type_device objects as others are not
1717 * placed in the drm device address space.
1720 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1722 struct ttm_bo_device *bdev = bo->bdev;
1726 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1727 if (unlikely(ret != 0))
1730 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1731 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1732 bo->mem.num_pages, 0, 0);
1734 if (unlikely(bo->vm_node == NULL)) {
1739 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1740 bo->mem.num_pages, 0);
1742 if (unlikely(bo->vm_node == NULL)) {
1743 lockmgr(&bdev->vm_lock, LK_RELEASE);
1747 ttm_bo_vm_insert_rb(bo);
1748 lockmgr(&bdev->vm_lock, LK_RELEASE);
1749 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1753 lockmgr(&bdev->vm_lock, LK_RELEASE);
1757 int ttm_bo_wait(struct ttm_buffer_object *bo,
1758 bool lazy, bool interruptible, bool no_wait)
1760 struct ttm_bo_driver *driver = bo->bdev->driver;
1761 struct ttm_bo_device *bdev = bo->bdev;
1765 if (likely(bo->sync_obj == NULL))
1768 while (bo->sync_obj) {
1770 if (driver->sync_obj_signaled(bo->sync_obj)) {
1771 void *tmp_obj = bo->sync_obj;
1772 bo->sync_obj = NULL;
1773 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1774 lockmgr(&bdev->fence_lock, LK_RELEASE);
1775 driver->sync_obj_unref(&tmp_obj);
1776 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1783 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1784 lockmgr(&bdev->fence_lock, LK_RELEASE);
1785 ret = driver->sync_obj_wait(sync_obj,
1786 lazy, interruptible);
1787 if (unlikely(ret != 0)) {
1788 driver->sync_obj_unref(&sync_obj);
1789 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1792 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1793 if (likely(bo->sync_obj == sync_obj)) {
1794 void *tmp_obj = bo->sync_obj;
1795 bo->sync_obj = NULL;
1796 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1798 lockmgr(&bdev->fence_lock, LK_RELEASE);
1799 driver->sync_obj_unref(&sync_obj);
1800 driver->sync_obj_unref(&tmp_obj);
1801 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1803 lockmgr(&bdev->fence_lock, LK_RELEASE);
1804 driver->sync_obj_unref(&sync_obj);
1805 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1810 EXPORT_SYMBOL(ttm_bo_wait);
1812 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1814 struct ttm_bo_device *bdev = bo->bdev;
1818 * Using ttm_bo_reserve makes sure the lru lists are updated.
1821 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1822 if (unlikely(ret != 0))
1824 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1825 ret = ttm_bo_wait(bo, false, true, no_wait);
1826 lockmgr(&bdev->fence_lock, LK_RELEASE);
1827 if (likely(ret == 0))
1828 atomic_inc(&bo->cpu_writers);
1829 ttm_bo_unreserve(bo);
1832 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1834 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1836 atomic_dec(&bo->cpu_writers);
1838 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1841 * A buffer object shrink method that tries to swap out the first
1842 * buffer object on the bo_global::swap_lru list.
1845 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1847 struct ttm_bo_global *glob =
1848 container_of(shrink, struct ttm_bo_global, shrink);
1849 struct ttm_buffer_object *bo;
1852 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1854 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1855 list_for_each_entry(bo, &glob->swap_lru, swap) {
1856 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1862 lockmgr(&glob->lru_lock, LK_RELEASE);
1866 kref_get(&bo->list_kref);
1868 if (!list_empty(&bo->ddestroy)) {
1869 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1870 kref_put(&bo->list_kref, ttm_bo_release_list);
1874 put_count = ttm_bo_del_from_lru(bo);
1875 lockmgr(&glob->lru_lock, LK_RELEASE);
1877 ttm_bo_list_ref_sub(bo, put_count, true);
1880 * Wait for GPU, then move to system cached.
1883 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1884 ret = ttm_bo_wait(bo, false, false, false);
1885 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1887 if (unlikely(ret != 0))
1890 if ((bo->mem.placement & swap_placement) != swap_placement) {
1891 struct ttm_mem_reg evict_mem;
1893 evict_mem = bo->mem;
1894 evict_mem.mm_node = NULL;
1895 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1896 evict_mem.mem_type = TTM_PL_SYSTEM;
1898 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1900 if (unlikely(ret != 0))
1904 ttm_bo_unmap_virtual(bo);
1907 * Swap out. Buffer will be swapped in again as soon as
1908 * anyone tries to access a ttm page.
1911 if (bo->bdev->driver->swap_notify)
1912 bo->bdev->driver->swap_notify(bo);
1914 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1919 * Unreserve without putting on LRU to avoid swapping out an
1920 * already swapped buffer.
1923 atomic_set(&bo->reserved, 0);
1924 wake_up_all(&bo->event_queue);
1925 kref_put(&bo->list_kref, ttm_bo_release_list);
1929 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1931 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1934 EXPORT_SYMBOL(ttm_bo_swapout_all);