1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 * $FreeBSD: head/sys/dev/drm2/ttm/ttm_bo.c 248060 2013-03-08 18:11:02Z dumbbell $
33 #define pr_fmt(fmt) "[TTM] " fmt
35 #include <drm/ttm/ttm_module.h>
36 #include <drm/ttm/ttm_bo_driver.h>
37 #include <drm/ttm/ttm_placement.h>
38 #include <linux/atomic.h>
39 #include <linux/export.h>
40 #include <linux/wait.h>
42 #define TTM_ASSERT_LOCKED(param)
43 #define TTM_DEBUG(fmt, arg...)
44 #define TTM_BO_HASH_ORDER 13
46 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
47 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
48 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob);
50 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
54 for (i = 0; i <= TTM_PL_PRIV5; i++)
55 if (flags & (1 << i)) {
62 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
64 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
66 kprintf(" has_type: %d\n", man->has_type);
67 kprintf(" use_type: %d\n", man->use_type);
68 kprintf(" flags: 0x%08X\n", man->flags);
69 kprintf(" gpu_offset: 0x%08lX\n", man->gpu_offset);
70 kprintf(" size: %ju\n", (uintmax_t)man->size);
71 kprintf(" available_caching: 0x%08X\n", man->available_caching);
72 kprintf(" default_caching: 0x%08X\n", man->default_caching);
73 if (mem_type != TTM_PL_SYSTEM)
74 (*man->func->debug)(man, TTM_PFX);
77 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
78 struct ttm_placement *placement)
82 kprintf("No space for %p (%lu pages, %luK, %luM)\n",
83 bo, bo->mem.num_pages, bo->mem.size >> 10,
85 for (i = 0; i < placement->num_placement; i++) {
86 ret = ttm_mem_type_from_flags(placement->placement[i],
90 kprintf(" placement[%d]=0x%08X (%d)\n",
91 i, placement->placement[i], mem_type);
92 ttm_mem_type_debug(bo->bdev, mem_type);
97 static ssize_t ttm_bo_global_show(struct ttm_bo_global *glob,
101 return snprintf(buffer, PAGE_SIZE, "%lu\n",
102 (unsigned long) atomic_read(&glob->bo_count));
106 static inline uint32_t ttm_bo_type_flags(unsigned type)
111 static void ttm_bo_release_list(struct kref *list_kref)
113 struct ttm_buffer_object *bo =
114 container_of(list_kref, struct ttm_buffer_object, list_kref);
115 struct ttm_bo_device *bdev = bo->bdev;
116 size_t acc_size = bo->acc_size;
118 BUG_ON(atomic_read(&bo->list_kref.refcount));
119 BUG_ON(atomic_read(&bo->kref.refcount));
120 BUG_ON(atomic_read(&bo->cpu_writers));
121 BUG_ON(bo->sync_obj != NULL);
122 BUG_ON(bo->mem.mm_node != NULL);
123 BUG_ON(!list_empty(&bo->lru));
124 BUG_ON(!list_empty(&bo->ddestroy));
127 ttm_tt_destroy(bo->ttm);
128 atomic_dec(&bo->glob->bo_count);
134 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
137 static int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,
141 return wait_event_interruptible(bo->event_queue,
142 !ttm_bo_is_reserved(bo));
144 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
149 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
151 struct ttm_bo_device *bdev = bo->bdev;
152 struct ttm_mem_type_manager *man;
154 BUG_ON(!ttm_bo_is_reserved(bo));
156 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
158 BUG_ON(!list_empty(&bo->lru));
160 man = &bdev->man[bo->mem.mem_type];
161 list_add_tail(&bo->lru, &man->lru);
162 kref_get(&bo->list_kref);
164 if (bo->ttm != NULL) {
165 list_add_tail(&bo->swap, &bo->glob->swap_lru);
166 kref_get(&bo->list_kref);
171 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
175 if (!list_empty(&bo->swap)) {
176 list_del_init(&bo->swap);
179 if (!list_empty(&bo->lru)) {
180 list_del_init(&bo->lru);
185 * TODO: Add a driver hook to delete from
186 * driver-specific LRU's here.
192 int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
194 bool no_wait, bool use_sequence, uint32_t sequence)
198 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
200 * Deadlock avoidance for multi-bo reserving.
202 if (use_sequence && bo->seq_valid) {
204 * We've already reserved this one.
206 if (unlikely(sequence == bo->val_seq))
209 * Already reserved by a thread that will not back
210 * off for us. We need to back off.
212 if (unlikely(sequence - bo->val_seq < (1U << 31)))
219 ret = ttm_bo_wait_unreserved(bo, interruptible);
226 bool wake_up = false;
228 * Wake up waiters that may need to recheck for deadlock,
229 * if we decreased the sequence number.
231 if (unlikely((bo->val_seq - sequence < (1U << 31))
236 * In the worst case with memory ordering these values can be
237 * seen in the wrong order. However since we call wake_up_all
238 * in that case, this will hopefully not pose a problem,
239 * and the worst case would only cause someone to accidentally
240 * hit -EAGAIN in ttm_bo_reserve when they see old value of
241 * val_seq. However this would only happen if seq_valid was
242 * written before val_seq was, and just means some slightly
243 * increased cpu usage
245 bo->val_seq = sequence;
246 bo->seq_valid = true;
248 wake_up_all(&bo->event_queue);
250 bo->seq_valid = false;
255 EXPORT_SYMBOL(ttm_bo_reserve);
257 static void ttm_bo_ref_bug(struct kref *list_kref)
262 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
265 kref_sub(&bo->list_kref, count,
266 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
269 int ttm_bo_reserve(struct ttm_buffer_object *bo,
271 bool no_wait, bool use_sequence, uint32_t sequence)
273 struct ttm_bo_global *glob = bo->glob;
277 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
278 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_sequence,
280 if (likely(ret == 0)) {
281 put_count = ttm_bo_del_from_lru(bo);
282 lockmgr(&glob->lru_lock, LK_RELEASE);
283 ttm_bo_list_ref_sub(bo, put_count, true);
285 lockmgr(&glob->lru_lock, LK_RELEASE);
292 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
293 bool interruptible, uint32_t sequence)
295 bool wake_up = false;
298 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
299 WARN_ON(bo->seq_valid && sequence == bo->val_seq);
301 ret = ttm_bo_wait_unreserved(bo, interruptible);
307 if ((bo->val_seq - sequence < (1U << 31)) || !bo->seq_valid)
311 * Wake up waiters that may need to recheck for deadlock,
312 * if we decreased the sequence number.
314 bo->val_seq = sequence;
315 bo->seq_valid = true;
317 wake_up_all(&bo->event_queue);
322 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
323 bool interruptible, uint32_t sequence)
325 struct ttm_bo_global *glob = bo->glob;
328 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
329 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence);
331 put_count = ttm_bo_del_from_lru(bo);
332 lockmgr(&glob->lru_lock, LK_RELEASE);
333 ttm_bo_list_ref_sub(bo, put_count, true);
335 lockmgr(&glob->lru_lock, LK_RELEASE);
339 EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
341 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
343 ttm_bo_add_to_lru(bo);
344 atomic_set(&bo->reserved, 0);
345 wake_up_all(&bo->event_queue);
348 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
350 struct ttm_bo_global *glob = bo->glob;
352 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
353 ttm_bo_unreserve_locked(bo);
354 lockmgr(&glob->lru_lock, LK_RELEASE);
356 EXPORT_SYMBOL(ttm_bo_unreserve);
359 * Call bo->mutex locked.
361 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
363 struct ttm_bo_device *bdev = bo->bdev;
364 struct ttm_bo_global *glob = bo->glob;
366 uint32_t page_flags = 0;
368 TTM_ASSERT_LOCKED(&bo->mutex);
371 if (bdev->need_dma32)
372 page_flags |= TTM_PAGE_FLAG_DMA32;
375 case ttm_bo_type_device:
377 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
378 case ttm_bo_type_kernel:
379 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
380 page_flags, glob->dummy_read_page);
381 if (unlikely(bo->ttm == NULL))
385 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
386 page_flags | TTM_PAGE_FLAG_SG,
387 glob->dummy_read_page);
388 if (unlikely(bo->ttm == NULL)) {
392 bo->ttm->sg = bo->sg;
395 kprintf("[TTM] Illegal buffer object type\n");
403 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
404 struct ttm_mem_reg *mem,
405 bool evict, bool interruptible,
408 struct ttm_bo_device *bdev = bo->bdev;
409 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
410 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
411 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
412 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
415 if (old_is_pci || new_is_pci ||
416 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
417 ret = ttm_mem_io_lock(old_man, true);
418 if (unlikely(ret != 0))
420 ttm_bo_unmap_virtual_locked(bo);
421 ttm_mem_io_unlock(old_man);
425 * Create and bind a ttm if required.
428 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
429 if (bo->ttm == NULL) {
430 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
431 ret = ttm_bo_add_ttm(bo, zero);
436 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
440 if (mem->mem_type != TTM_PL_SYSTEM) {
441 ret = ttm_tt_bind(bo->ttm, mem);
446 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
447 if (bdev->driver->move_notify)
448 bdev->driver->move_notify(bo, mem);
455 if (bdev->driver->move_notify)
456 bdev->driver->move_notify(bo, mem);
458 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
459 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
460 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
461 else if (bdev->driver->move)
462 ret = bdev->driver->move(bo, evict, interruptible,
465 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
468 if (bdev->driver->move_notify) {
469 struct ttm_mem_reg tmp_mem = *mem;
472 bdev->driver->move_notify(bo, mem);
482 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
484 kprintf("[TTM] Can not flush read caches\n");
488 if (bo->mem.mm_node) {
489 bo->offset = (bo->mem.start << PAGE_SHIFT) +
490 bdev->man[bo->mem.mem_type].gpu_offset;
491 bo->cur_placement = bo->mem.placement;
498 new_man = &bdev->man[bo->mem.mem_type];
499 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
500 ttm_tt_unbind(bo->ttm);
501 ttm_tt_destroy(bo->ttm);
510 * Will release GPU memory type usage on destruction.
511 * This is the place to put in driver specific hooks to release
512 * driver private resources.
513 * Will release the bo::reserved lock.
516 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
518 if (bo->bdev->driver->move_notify)
519 bo->bdev->driver->move_notify(bo, NULL);
522 ttm_tt_unbind(bo->ttm);
523 ttm_tt_destroy(bo->ttm);
526 ttm_bo_mem_put(bo, &bo->mem);
528 atomic_set(&bo->reserved, 0);
529 wake_up_all(&bo->event_queue);
532 * Since the final reference to this bo may not be dropped by
533 * the current task we have to put a memory barrier here to make
534 * sure the changes done in this function are always visible.
536 * This function only needs protection against the final kref_put.
541 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
543 struct ttm_bo_device *bdev = bo->bdev;
544 struct ttm_bo_global *glob = bo->glob;
545 struct ttm_bo_driver *driver = bdev->driver;
546 void *sync_obj = NULL;
550 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
551 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
553 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
554 (void) ttm_bo_wait(bo, false, false, true);
555 if (!ret && !bo->sync_obj) {
556 lockmgr(&bdev->fence_lock, LK_RELEASE);
557 put_count = ttm_bo_del_from_lru(bo);
559 lockmgr(&glob->lru_lock, LK_RELEASE);
560 ttm_bo_cleanup_memtype_use(bo);
562 ttm_bo_list_ref_sub(bo, put_count, true);
567 sync_obj = driver->sync_obj_ref(bo->sync_obj);
568 lockmgr(&bdev->fence_lock, LK_RELEASE);
571 atomic_set(&bo->reserved, 0);
572 wake_up_all(&bo->event_queue);
575 kref_get(&bo->list_kref);
576 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
577 lockmgr(&glob->lru_lock, LK_RELEASE);
580 driver->sync_obj_flush(sync_obj);
581 driver->sync_obj_unref(&sync_obj);
583 schedule_delayed_work(&bdev->wq,
584 ((hz / 100) < 1) ? 1 : hz / 100);
588 * function ttm_bo_cleanup_refs_and_unlock
589 * If bo idle, remove from delayed- and lru lists, and unref.
590 * If not idle, do nothing.
592 * Must be called with lru_lock and reservation held, this function
593 * will drop both before returning.
595 * @interruptible Any sleeps should occur interruptibly.
596 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
599 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
603 struct ttm_bo_device *bdev = bo->bdev;
604 struct ttm_bo_driver *driver = bdev->driver;
605 struct ttm_bo_global *glob = bo->glob;
609 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
610 ret = ttm_bo_wait(bo, false, false, true);
612 if (ret && !no_wait_gpu) {
616 * Take a reference to the fence and unreserve,
617 * at this point the buffer should be dead, so
618 * no new sync objects can be attached.
620 sync_obj = driver->sync_obj_ref(bo->sync_obj);
621 lockmgr(&bdev->fence_lock, LK_RELEASE);
623 atomic_set(&bo->reserved, 0);
624 wake_up_all(&bo->event_queue);
625 lockmgr(&glob->lru_lock, LK_RELEASE);
627 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
628 driver->sync_obj_unref(&sync_obj);
633 * remove sync_obj with ttm_bo_wait, the wait should be
634 * finished, and no new wait object should have been added.
636 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
637 ret = ttm_bo_wait(bo, false, false, true);
639 lockmgr(&bdev->fence_lock, LK_RELEASE);
643 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
644 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
647 * We raced, and lost, someone else holds the reservation now,
648 * and is probably busy in ttm_bo_cleanup_memtype_use.
650 * Even if it's not the case, because we finished waiting any
651 * delayed destruction would succeed, so just return success
655 lockmgr(&glob->lru_lock, LK_RELEASE);
659 lockmgr(&bdev->fence_lock, LK_RELEASE);
661 if (ret || unlikely(list_empty(&bo->ddestroy))) {
662 atomic_set(&bo->reserved, 0);
663 wake_up_all(&bo->event_queue);
664 lockmgr(&glob->lru_lock, LK_RELEASE);
668 put_count = ttm_bo_del_from_lru(bo);
669 list_del_init(&bo->ddestroy);
672 lockmgr(&glob->lru_lock, LK_RELEASE);
673 ttm_bo_cleanup_memtype_use(bo);
675 ttm_bo_list_ref_sub(bo, put_count, true);
681 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
682 * encountered buffers.
685 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
687 struct ttm_bo_global *glob = bdev->glob;
688 struct ttm_buffer_object *entry = NULL;
691 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
692 if (list_empty(&bdev->ddestroy))
695 entry = list_first_entry(&bdev->ddestroy,
696 struct ttm_buffer_object, ddestroy);
697 kref_get(&entry->list_kref);
700 struct ttm_buffer_object *nentry = NULL;
702 if (entry->ddestroy.next != &bdev->ddestroy) {
703 nentry = list_first_entry(&entry->ddestroy,
704 struct ttm_buffer_object, ddestroy);
705 kref_get(&nentry->list_kref);
708 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
709 if (remove_all && ret) {
710 ret = ttm_bo_reserve_nolru(entry, false, false,
715 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
718 lockmgr(&glob->lru_lock, LK_RELEASE);
720 kref_put(&entry->list_kref, ttm_bo_release_list);
726 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
727 if (list_empty(&entry->ddestroy))
732 lockmgr(&glob->lru_lock, LK_RELEASE);
735 kref_put(&entry->list_kref, ttm_bo_release_list);
739 static void ttm_bo_delayed_workqueue(struct work_struct *work)
741 struct ttm_bo_device *bdev =
742 container_of(work, struct ttm_bo_device, wq.work);
744 if (ttm_bo_delayed_delete(bdev, false)) {
745 schedule_delayed_work(&bdev->wq,
746 ((hz / 100) < 1) ? 1 : hz / 100);
751 * NOTE: bdev->vm_lock already held on call, this function release it.
753 static void ttm_bo_release(struct kref *kref)
755 struct ttm_buffer_object *bo =
756 container_of(kref, struct ttm_buffer_object, kref);
757 struct ttm_bo_device *bdev = bo->bdev;
758 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
761 if (atomic_read(&bo->kref.refcount) > 0) {
762 lockmgr(&bdev->vm_lock, LK_RELEASE);
765 if (likely(bo->vm_node != NULL)) {
766 RB_REMOVE(ttm_bo_device_buffer_objects,
767 &bdev->addr_space_rb, bo);
768 drm_mm_put_block(bo->vm_node);
773 * Should we clean up our implied list_kref? Because ttm_bo_release()
774 * can be called reentrantly due to races (this may not be true any
775 * more with the lock management changes in the deref), it is possible
776 * to get here twice, but there's only one list_kref ref to drop and
777 * in the other path 'bo' can be kfree()d by another thread the
778 * instant we release our lock.
780 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
781 if (release_active) {
782 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
783 lockmgr(&bdev->vm_lock, LK_RELEASE);
784 ttm_mem_io_lock(man, false);
785 ttm_mem_io_free_vm(bo);
786 ttm_mem_io_unlock(man);
787 ttm_bo_cleanup_refs_or_queue(bo);
788 kref_put(&bo->list_kref, ttm_bo_release_list);
790 lockmgr(&bdev->vm_lock, LK_RELEASE);
794 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
796 struct ttm_buffer_object *bo = *p_bo;
797 struct ttm_bo_device *bdev = bo->bdev;
800 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
801 if (kref_put(&bo->kref, ttm_bo_release) == 0)
802 lockmgr(&bdev->vm_lock, LK_RELEASE);
804 EXPORT_SYMBOL(ttm_bo_unref);
806 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
808 return cancel_delayed_work_sync(&bdev->wq);
810 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
812 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
815 schedule_delayed_work(&bdev->wq,
816 ((hz / 100) < 1) ? 1 : hz / 100);
818 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
820 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
823 struct ttm_bo_device *bdev = bo->bdev;
824 struct ttm_mem_reg evict_mem;
825 struct ttm_placement placement;
828 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
829 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
830 lockmgr(&bdev->fence_lock, LK_RELEASE);
832 if (unlikely(ret != 0)) {
833 if (ret != -ERESTART) {
834 kprintf("[TTM] Failed to expire sync object before buffer eviction\n");
839 BUG_ON(!ttm_bo_is_reserved(bo));
842 evict_mem.mm_node = NULL;
843 evict_mem.bus.io_reserved_vm = false;
844 evict_mem.bus.io_reserved_count = 0;
848 placement.num_placement = 0;
849 placement.num_busy_placement = 0;
850 bdev->driver->evict_flags(bo, &placement);
851 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
854 if (ret != -ERESTART) {
855 kprintf("[TTM] Failed to find memory space for buffer 0x%p eviction\n",
857 ttm_bo_mem_space_debug(bo, &placement);
862 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
865 if (ret != -ERESTART)
866 kprintf("[TTM] Buffer eviction failed\n");
867 ttm_bo_mem_put(bo, &evict_mem);
875 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
880 struct ttm_bo_global *glob = bdev->glob;
881 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
882 struct ttm_buffer_object *bo;
883 int ret = -EBUSY, put_count;
885 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
886 list_for_each_entry(bo, &man->lru, lru) {
887 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
893 lockmgr(&glob->lru_lock, LK_RELEASE);
897 kref_get(&bo->list_kref);
899 if (!list_empty(&bo->ddestroy)) {
900 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
902 kref_put(&bo->list_kref, ttm_bo_release_list);
906 put_count = ttm_bo_del_from_lru(bo);
907 lockmgr(&glob->lru_lock, LK_RELEASE);
911 ttm_bo_list_ref_sub(bo, put_count, true);
913 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
914 ttm_bo_unreserve(bo);
916 kref_put(&bo->list_kref, ttm_bo_release_list);
920 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
922 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
925 (*man->func->put_node)(man, mem);
927 EXPORT_SYMBOL(ttm_bo_mem_put);
930 * Repeatedly evict memory from the LRU for @mem_type until we create enough
931 * space, or we've evicted everything and there isn't enough space.
933 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
935 struct ttm_placement *placement,
936 struct ttm_mem_reg *mem,
940 struct ttm_bo_device *bdev = bo->bdev;
941 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
945 ret = (*man->func->get_node)(man, bo, placement, mem);
946 if (unlikely(ret != 0))
950 ret = ttm_mem_evict_first(bdev, mem_type,
951 interruptible, no_wait_gpu);
952 if (unlikely(ret != 0))
955 if (mem->mm_node == NULL)
957 mem->mem_type = mem_type;
961 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
962 uint32_t cur_placement,
963 uint32_t proposed_placement)
965 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
966 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
969 * Keep current caching if possible.
972 if ((cur_placement & caching) != 0)
973 result |= (cur_placement & caching);
974 else if ((man->default_caching & caching) != 0)
975 result |= man->default_caching;
976 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
977 result |= TTM_PL_FLAG_CACHED;
978 else if ((TTM_PL_FLAG_WC & caching) != 0)
979 result |= TTM_PL_FLAG_WC;
980 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
981 result |= TTM_PL_FLAG_UNCACHED;
986 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
988 uint32_t proposed_placement,
989 uint32_t *masked_placement)
991 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
993 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
996 if ((proposed_placement & man->available_caching) == 0)
999 cur_flags |= (proposed_placement & man->available_caching);
1001 *masked_placement = cur_flags;
1006 * Creates space for memory region @mem according to its type.
1008 * This function first searches for free space in compatible memory types in
1009 * the priority order defined by the driver. If free space isn't found, then
1010 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1013 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1014 struct ttm_placement *placement,
1015 struct ttm_mem_reg *mem,
1019 struct ttm_bo_device *bdev = bo->bdev;
1020 struct ttm_mem_type_manager *man;
1021 uint32_t mem_type = TTM_PL_SYSTEM;
1022 uint32_t cur_flags = 0;
1023 bool type_found = false;
1024 bool type_ok = false;
1025 bool has_erestartsys = false;
1028 mem->mm_node = NULL;
1029 for (i = 0; i < placement->num_placement; ++i) {
1030 ret = ttm_mem_type_from_flags(placement->placement[i],
1034 man = &bdev->man[mem_type];
1036 type_ok = ttm_bo_mt_compatible(man,
1038 placement->placement[i],
1044 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1047 * Use the access and other non-mapping-related flag bits from
1048 * the memory placement flags to the current flags
1050 ttm_flag_masked(&cur_flags, placement->placement[i],
1051 ~TTM_PL_MASK_MEMTYPE);
1053 if (mem_type == TTM_PL_SYSTEM)
1056 if (man->has_type && man->use_type) {
1058 ret = (*man->func->get_node)(man, bo, placement, mem);
1066 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1067 mem->mem_type = mem_type;
1068 mem->placement = cur_flags;
1075 for (i = 0; i < placement->num_busy_placement; ++i) {
1076 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1080 man = &bdev->man[mem_type];
1083 if (!ttm_bo_mt_compatible(man,
1085 placement->busy_placement[i],
1089 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1092 * Use the access and other non-mapping-related flag bits from
1093 * the memory placement flags to the current flags
1095 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1096 ~TTM_PL_MASK_MEMTYPE);
1099 if (mem_type == TTM_PL_SYSTEM) {
1100 mem->mem_type = mem_type;
1101 mem->placement = cur_flags;
1102 mem->mm_node = NULL;
1106 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1107 interruptible, no_wait_gpu);
1108 if (ret == 0 && mem->mm_node) {
1109 mem->placement = cur_flags;
1112 if (ret == -ERESTART)
1113 has_erestartsys = true;
1115 ret = (has_erestartsys) ? -ERESTART : -ENOMEM;
1118 EXPORT_SYMBOL(ttm_bo_mem_space);
1121 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1122 struct ttm_placement *placement,
1127 struct ttm_mem_reg mem;
1128 struct ttm_bo_device *bdev = bo->bdev;
1130 BUG_ON(!ttm_bo_is_reserved(bo));
1133 * FIXME: It's possible to pipeline buffer moves.
1134 * Have the driver move function wait for idle when necessary,
1135 * instead of doing it here.
1137 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1138 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1139 lockmgr(&bdev->fence_lock, LK_RELEASE);
1142 mem.num_pages = bo->num_pages;
1143 mem.size = mem.num_pages << PAGE_SHIFT;
1144 mem.page_alignment = bo->mem.page_alignment;
1145 mem.bus.io_reserved_vm = false;
1146 mem.bus.io_reserved_count = 0;
1148 * Determine where to move the buffer.
1150 ret = ttm_bo_mem_space(bo, placement, &mem,
1151 interruptible, no_wait_gpu);
1154 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1155 interruptible, no_wait_gpu);
1157 if (ret && mem.mm_node)
1158 ttm_bo_mem_put(bo, &mem);
1162 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1163 struct ttm_mem_reg *mem)
1167 if (mem->mm_node && placement->lpfn != 0 &&
1168 (mem->start < placement->fpfn ||
1169 mem->start + mem->num_pages > placement->lpfn))
1172 for (i = 0; i < placement->num_placement; i++) {
1173 if ((placement->placement[i] & mem->placement &
1174 TTM_PL_MASK_CACHING) &&
1175 (placement->placement[i] & mem->placement &
1182 int ttm_bo_validate(struct ttm_buffer_object *bo,
1183 struct ttm_placement *placement,
1189 BUG_ON(!ttm_bo_is_reserved(bo));
1190 /* Check that range is valid */
1191 if (placement->lpfn || placement->fpfn)
1192 if (placement->fpfn > placement->lpfn ||
1193 (placement->lpfn - placement->fpfn) < bo->num_pages)
1196 * Check whether we need to move buffer.
1198 ret = ttm_bo_mem_compat(placement, &bo->mem);
1200 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1206 * Use the access and other non-mapping-related flag bits from
1207 * the compatible memory placement flags to the active flags
1209 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1210 ~TTM_PL_MASK_MEMTYPE);
1213 * We might need to add a TTM.
1215 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1216 ret = ttm_bo_add_ttm(bo, true);
1222 EXPORT_SYMBOL(ttm_bo_validate);
1224 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1225 struct ttm_placement *placement)
1227 BUG_ON((placement->fpfn || placement->lpfn) &&
1228 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1233 int ttm_bo_init(struct ttm_bo_device *bdev,
1234 struct ttm_buffer_object *bo,
1236 enum ttm_bo_type type,
1237 struct ttm_placement *placement,
1238 uint32_t page_alignment,
1240 struct vm_object *persistent_swap_storage,
1242 struct sg_table *sg,
1243 void (*destroy) (struct ttm_buffer_object *))
1246 unsigned long num_pages;
1247 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1249 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1251 kprintf("[TTM] Out of kernel memory\n");
1259 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1260 if (num_pages == 0) {
1261 kprintf("[TTM] Illegal buffer object size\n");
1266 ttm_mem_global_free(mem_glob, acc_size);
1269 bo->destroy = destroy;
1271 kref_init(&bo->kref);
1272 kref_init(&bo->list_kref);
1273 atomic_set(&bo->cpu_writers, 0);
1274 atomic_set(&bo->reserved, 1);
1275 init_waitqueue_head(&bo->event_queue);
1276 INIT_LIST_HEAD(&bo->lru);
1277 INIT_LIST_HEAD(&bo->ddestroy);
1278 INIT_LIST_HEAD(&bo->swap);
1279 INIT_LIST_HEAD(&bo->io_reserve_lru);
1280 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1282 bo->glob = bdev->glob;
1284 bo->num_pages = num_pages;
1285 bo->mem.size = num_pages << PAGE_SHIFT;
1286 bo->mem.mem_type = TTM_PL_SYSTEM;
1287 bo->mem.num_pages = bo->num_pages;
1288 bo->mem.mm_node = NULL;
1289 bo->mem.page_alignment = page_alignment;
1290 bo->mem.bus.io_reserved_vm = false;
1291 bo->mem.bus.io_reserved_count = 0;
1293 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1294 bo->seq_valid = false;
1295 bo->persistent_swap_storage = persistent_swap_storage;
1296 bo->acc_size = acc_size;
1298 atomic_inc(&bo->glob->bo_count);
1301 * Mirror ref from kref_init() for list_kref.
1303 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1305 ret = ttm_bo_check_placement(bo, placement);
1306 if (unlikely(ret != 0))
1310 * For ttm_bo_type_device buffers, allocate
1311 * address space from the device.
1313 if (bo->type == ttm_bo_type_device ||
1314 bo->type == ttm_bo_type_sg) {
1315 ret = ttm_bo_setup_vm(bo);
1320 ret = ttm_bo_validate(bo, placement, interruptible, false);
1324 ttm_bo_unreserve(bo);
1328 ttm_bo_unreserve(bo);
1333 EXPORT_SYMBOL(ttm_bo_init);
1335 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1336 unsigned long bo_size,
1337 unsigned struct_size)
1339 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1342 size += ttm_round_pot(struct_size);
1343 size += PAGE_ALIGN(npages * sizeof(void *));
1344 size += ttm_round_pot(sizeof(struct ttm_tt));
1347 EXPORT_SYMBOL(ttm_bo_acc_size);
1349 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1350 unsigned long bo_size,
1351 unsigned struct_size)
1353 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1356 size += ttm_round_pot(struct_size);
1357 size += PAGE_ALIGN(npages * sizeof(void *));
1358 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1359 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1362 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1364 int ttm_bo_create(struct ttm_bo_device *bdev,
1366 enum ttm_bo_type type,
1367 struct ttm_placement *placement,
1368 uint32_t page_alignment,
1370 struct vm_object *persistent_swap_storage,
1371 struct ttm_buffer_object **p_bo)
1373 struct ttm_buffer_object *bo;
1378 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1379 if (unlikely(bo == NULL))
1382 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1383 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1384 interruptible, persistent_swap_storage, acc_size,
1386 if (likely(ret == 0))
1391 EXPORT_SYMBOL(ttm_bo_create);
1393 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1394 unsigned mem_type, bool allow_errors)
1396 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1397 struct ttm_bo_global *glob = bdev->glob;
1401 * Can't use standard list traversal since we're unlocking.
1404 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1405 while (!list_empty(&man->lru)) {
1406 lockmgr(&glob->lru_lock, LK_RELEASE);
1407 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1412 kprintf("[TTM] Cleanup eviction failed\n");
1415 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1417 lockmgr(&glob->lru_lock, LK_RELEASE);
1421 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1423 struct ttm_mem_type_manager *man;
1426 if (mem_type >= TTM_NUM_MEM_TYPES) {
1427 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1430 man = &bdev->man[mem_type];
1432 if (!man->has_type) {
1433 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1438 man->use_type = false;
1439 man->has_type = false;
1443 ttm_bo_force_list_clean(bdev, mem_type, false);
1445 ret = (*man->func->takedown)(man);
1450 EXPORT_SYMBOL(ttm_bo_clean_mm);
1452 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1454 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1456 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1457 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1461 if (!man->has_type) {
1462 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1466 return ttm_bo_force_list_clean(bdev, mem_type, true);
1468 EXPORT_SYMBOL(ttm_bo_evict_mm);
1470 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1471 unsigned long p_size)
1474 struct ttm_mem_type_manager *man;
1476 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1477 man = &bdev->man[type];
1478 BUG_ON(man->has_type);
1479 man->io_reserve_fastpath = true;
1480 man->use_io_reserve_lru = false;
1481 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1482 INIT_LIST_HEAD(&man->io_reserve_lru);
1484 ret = bdev->driver->init_mem_type(bdev, type, man);
1490 if (type != TTM_PL_SYSTEM) {
1491 ret = (*man->func->init)(man, p_size);
1495 man->has_type = true;
1496 man->use_type = true;
1499 INIT_LIST_HEAD(&man->lru);
1503 EXPORT_SYMBOL(ttm_bo_init_mm);
1505 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1507 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1508 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1509 glob->dummy_read_page = NULL;
1511 vm_page_free(glob->dummy_read_page);
1515 void ttm_bo_global_release(struct drm_global_reference *ref)
1517 struct ttm_bo_global *glob = ref->object;
1519 if (refcount_release(&glob->kobj_ref))
1520 ttm_bo_global_kobj_release(glob);
1522 EXPORT_SYMBOL(ttm_bo_global_release);
1524 int ttm_bo_global_init(struct drm_global_reference *ref)
1526 struct ttm_bo_global_ref *bo_ref =
1527 container_of(ref, struct ttm_bo_global_ref, ref);
1528 struct ttm_bo_global *glob = ref->object;
1531 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1532 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1533 glob->mem_glob = bo_ref->mem_glob;
1534 glob->dummy_read_page = vm_page_alloc_contig(
1535 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1537 if (unlikely(glob->dummy_read_page == NULL)) {
1542 INIT_LIST_HEAD(&glob->swap_lru);
1543 INIT_LIST_HEAD(&glob->device_list);
1545 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1546 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1547 if (unlikely(ret != 0)) {
1548 kprintf("[TTM] Could not register buffer object swapout\n");
1552 atomic_set(&glob->bo_count, 0);
1554 refcount_init(&glob->kobj_ref, 1);
1558 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1559 glob->dummy_read_page = NULL;
1561 vm_page_free(glob->dummy_read_page);
1567 EXPORT_SYMBOL(ttm_bo_global_init);
1570 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1573 unsigned i = TTM_NUM_MEM_TYPES;
1574 struct ttm_mem_type_manager *man;
1575 struct ttm_bo_global *glob = bdev->glob;
1578 man = &bdev->man[i];
1579 if (man->has_type) {
1580 man->use_type = false;
1581 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1583 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1586 man->has_type = false;
1590 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1591 list_del(&bdev->device_list);
1592 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1594 cancel_delayed_work_sync(&bdev->wq);
1596 while (ttm_bo_delayed_delete(bdev, true))
1599 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1600 if (list_empty(&bdev->ddestroy))
1601 TTM_DEBUG("Delayed destroy list was clean\n");
1603 if (list_empty(&bdev->man[0].lru))
1604 TTM_DEBUG("Swap list was clean\n");
1605 lockmgr(&glob->lru_lock, LK_RELEASE);
1607 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1608 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1609 drm_mm_takedown(&bdev->addr_space_mm);
1610 lockmgr(&bdev->vm_lock, LK_RELEASE);
1614 EXPORT_SYMBOL(ttm_bo_device_release);
1616 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1617 struct ttm_bo_global *glob,
1618 struct ttm_bo_driver *driver,
1619 uint64_t file_page_offset,
1624 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1625 bdev->driver = driver;
1627 memset(bdev->man, 0, sizeof(bdev->man));
1630 * Initialize the system memory buffer type.
1631 * Other types need to be driver / IOCTL initialized.
1633 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1634 if (unlikely(ret != 0))
1637 RB_INIT(&bdev->addr_space_rb);
1638 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1639 if (unlikely(ret != 0))
1640 goto out_no_addr_mm;
1642 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1643 INIT_LIST_HEAD(&bdev->ddestroy);
1644 bdev->dev_mapping = NULL;
1646 bdev->need_dma32 = need_dma32;
1648 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1649 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1650 list_add_tail(&bdev->device_list, &glob->device_list);
1651 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1655 ttm_bo_clean_mm(bdev, 0);
1659 EXPORT_SYMBOL(ttm_bo_device_init);
1662 * buffer object vm functions.
1665 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1667 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1669 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1670 if (mem->mem_type == TTM_PL_SYSTEM)
1673 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1676 if (mem->placement & TTM_PL_FLAG_CACHED)
1682 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1685 ttm_bo_release_mmap(bo);
1686 ttm_mem_io_free_vm(bo);
1689 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1691 struct ttm_bo_device *bdev = bo->bdev;
1692 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1694 ttm_mem_io_lock(man, false);
1695 ttm_bo_unmap_virtual_locked(bo);
1696 ttm_mem_io_unlock(man);
1700 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1702 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1704 struct ttm_bo_device *bdev = bo->bdev;
1706 /* The caller acquired bdev->vm_lock. */
1707 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1713 * @bo: the buffer to allocate address space for
1715 * Allocate address space in the drm device so that applications
1716 * can mmap the buffer and access the contents. This only
1717 * applies to ttm_bo_type_device objects as others are not
1718 * placed in the drm device address space.
1721 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1723 struct ttm_bo_device *bdev = bo->bdev;
1727 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1728 if (unlikely(ret != 0))
1731 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1732 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1733 bo->mem.num_pages, 0, 0);
1735 if (unlikely(bo->vm_node == NULL)) {
1740 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1741 bo->mem.num_pages, 0);
1743 if (unlikely(bo->vm_node == NULL)) {
1744 lockmgr(&bdev->vm_lock, LK_RELEASE);
1748 ttm_bo_vm_insert_rb(bo);
1749 lockmgr(&bdev->vm_lock, LK_RELEASE);
1750 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1754 lockmgr(&bdev->vm_lock, LK_RELEASE);
1758 int ttm_bo_wait(struct ttm_buffer_object *bo,
1759 bool lazy, bool interruptible, bool no_wait)
1761 struct ttm_bo_driver *driver = bo->bdev->driver;
1762 struct ttm_bo_device *bdev = bo->bdev;
1766 if (likely(bo->sync_obj == NULL))
1769 while (bo->sync_obj) {
1771 if (driver->sync_obj_signaled(bo->sync_obj)) {
1772 void *tmp_obj = bo->sync_obj;
1773 bo->sync_obj = NULL;
1774 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1775 lockmgr(&bdev->fence_lock, LK_RELEASE);
1776 driver->sync_obj_unref(&tmp_obj);
1777 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1784 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1785 lockmgr(&bdev->fence_lock, LK_RELEASE);
1786 ret = driver->sync_obj_wait(sync_obj,
1787 lazy, interruptible);
1788 if (unlikely(ret != 0)) {
1789 driver->sync_obj_unref(&sync_obj);
1790 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1793 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1794 if (likely(bo->sync_obj == sync_obj)) {
1795 void *tmp_obj = bo->sync_obj;
1796 bo->sync_obj = NULL;
1797 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1799 lockmgr(&bdev->fence_lock, LK_RELEASE);
1800 driver->sync_obj_unref(&sync_obj);
1801 driver->sync_obj_unref(&tmp_obj);
1802 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1804 lockmgr(&bdev->fence_lock, LK_RELEASE);
1805 driver->sync_obj_unref(&sync_obj);
1806 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1811 EXPORT_SYMBOL(ttm_bo_wait);
1813 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1815 struct ttm_bo_device *bdev = bo->bdev;
1819 * Using ttm_bo_reserve makes sure the lru lists are updated.
1822 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1823 if (unlikely(ret != 0))
1825 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1826 ret = ttm_bo_wait(bo, false, true, no_wait);
1827 lockmgr(&bdev->fence_lock, LK_RELEASE);
1828 if (likely(ret == 0))
1829 atomic_inc(&bo->cpu_writers);
1830 ttm_bo_unreserve(bo);
1833 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1835 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1837 atomic_dec(&bo->cpu_writers);
1839 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1842 * A buffer object shrink method that tries to swap out the first
1843 * buffer object on the bo_global::swap_lru list.
1846 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1848 struct ttm_bo_global *glob =
1849 container_of(shrink, struct ttm_bo_global, shrink);
1850 struct ttm_buffer_object *bo;
1853 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1855 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1856 list_for_each_entry(bo, &glob->swap_lru, swap) {
1857 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1863 lockmgr(&glob->lru_lock, LK_RELEASE);
1867 kref_get(&bo->list_kref);
1869 if (!list_empty(&bo->ddestroy)) {
1870 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1871 kref_put(&bo->list_kref, ttm_bo_release_list);
1875 put_count = ttm_bo_del_from_lru(bo);
1876 lockmgr(&glob->lru_lock, LK_RELEASE);
1878 ttm_bo_list_ref_sub(bo, put_count, true);
1881 * Wait for GPU, then move to system cached.
1884 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1885 ret = ttm_bo_wait(bo, false, false, false);
1886 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1888 if (unlikely(ret != 0))
1891 if ((bo->mem.placement & swap_placement) != swap_placement) {
1892 struct ttm_mem_reg evict_mem;
1894 evict_mem = bo->mem;
1895 evict_mem.mm_node = NULL;
1896 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1897 evict_mem.mem_type = TTM_PL_SYSTEM;
1899 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1901 if (unlikely(ret != 0))
1905 ttm_bo_unmap_virtual(bo);
1908 * Swap out. Buffer will be swapped in again as soon as
1909 * anyone tries to access a ttm page.
1912 if (bo->bdev->driver->swap_notify)
1913 bo->bdev->driver->swap_notify(bo);
1915 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1920 * Unreserve without putting on LRU to avoid swapping out an
1921 * already swapped buffer.
1924 atomic_set(&bo->reserved, 0);
1925 wake_up_all(&bo->event_queue);
1926 kref_put(&bo->list_kref, ttm_bo_release_list);
1930 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1932 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1935 EXPORT_SYMBOL(ttm_bo_swapout_all);