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 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_sequence,
278 if (likely(ret == 0)) {
279 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
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);
288 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
289 bool interruptible, uint32_t sequence)
291 bool wake_up = false;
294 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
295 WARN_ON(bo->seq_valid && sequence == bo->val_seq);
297 ret = ttm_bo_wait_unreserved(bo, interruptible);
303 if ((bo->val_seq - sequence < (1U << 31)) || !bo->seq_valid)
307 * Wake up waiters that may need to recheck for deadlock,
308 * if we decreased the sequence number.
310 bo->val_seq = sequence;
311 bo->seq_valid = true;
313 wake_up_all(&bo->event_queue);
318 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
319 bool interruptible, uint32_t sequence)
321 struct ttm_bo_global *glob = bo->glob;
324 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence);
326 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
327 put_count = ttm_bo_del_from_lru(bo);
328 lockmgr(&glob->lru_lock, LK_RELEASE);
329 ttm_bo_list_ref_sub(bo, put_count, true);
333 EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
336 * Must interlock with event_queue to avoid race against
337 * wait_event_common() which can cause wait_event_common()
341 ttm_bo_unreserve_core(struct ttm_buffer_object *bo)
343 lockmgr(&bo->event_queue.lock, LK_EXCLUSIVE);
344 atomic_set(&bo->reserved, 0);
345 lockmgr(&bo->event_queue.lock, LK_RELEASE);
346 wake_up_all(&bo->event_queue);
349 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
351 ttm_bo_add_to_lru(bo);
352 ttm_bo_unreserve_core(bo);
355 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
357 struct ttm_bo_global *glob = bo->glob;
359 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
360 ttm_bo_unreserve_locked(bo);
361 lockmgr(&glob->lru_lock, LK_RELEASE);
363 EXPORT_SYMBOL(ttm_bo_unreserve);
366 * Call bo->mutex locked.
368 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
370 struct ttm_bo_device *bdev = bo->bdev;
371 struct ttm_bo_global *glob = bo->glob;
373 uint32_t page_flags = 0;
375 TTM_ASSERT_LOCKED(&bo->mutex);
378 if (bdev->need_dma32)
379 page_flags |= TTM_PAGE_FLAG_DMA32;
382 case ttm_bo_type_device:
384 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
385 case ttm_bo_type_kernel:
386 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
387 page_flags, glob->dummy_read_page);
388 if (unlikely(bo->ttm == NULL))
392 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
393 page_flags | TTM_PAGE_FLAG_SG,
394 glob->dummy_read_page);
395 if (unlikely(bo->ttm == NULL)) {
399 bo->ttm->sg = bo->sg;
402 kprintf("[TTM] Illegal buffer object type\n");
410 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
411 struct ttm_mem_reg *mem,
412 bool evict, bool interruptible,
415 struct ttm_bo_device *bdev = bo->bdev;
416 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
417 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
418 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
419 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
422 if (old_is_pci || new_is_pci ||
423 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
424 ret = ttm_mem_io_lock(old_man, true);
425 if (unlikely(ret != 0))
427 ttm_bo_unmap_virtual_locked(bo);
428 ttm_mem_io_unlock(old_man);
432 * Create and bind a ttm if required.
435 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
436 if (bo->ttm == NULL) {
437 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
438 ret = ttm_bo_add_ttm(bo, zero);
443 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
447 if (mem->mem_type != TTM_PL_SYSTEM) {
448 ret = ttm_tt_bind(bo->ttm, mem);
453 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
454 if (bdev->driver->move_notify)
455 bdev->driver->move_notify(bo, mem);
462 if (bdev->driver->move_notify)
463 bdev->driver->move_notify(bo, mem);
465 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
466 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
467 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
468 else if (bdev->driver->move)
469 ret = bdev->driver->move(bo, evict, interruptible,
472 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
475 if (bdev->driver->move_notify) {
476 struct ttm_mem_reg tmp_mem = *mem;
479 bdev->driver->move_notify(bo, mem);
489 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
491 kprintf("[TTM] Can not flush read caches\n");
495 if (bo->mem.mm_node) {
496 bo->offset = (bo->mem.start << PAGE_SHIFT) +
497 bdev->man[bo->mem.mem_type].gpu_offset;
498 bo->cur_placement = bo->mem.placement;
505 new_man = &bdev->man[bo->mem.mem_type];
506 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
507 ttm_tt_unbind(bo->ttm);
508 ttm_tt_destroy(bo->ttm);
517 * Will release GPU memory type usage on destruction.
518 * This is the place to put in driver specific hooks to release
519 * driver private resources.
520 * Will release the bo::reserved lock.
523 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
525 if (bo->bdev->driver->move_notify)
526 bo->bdev->driver->move_notify(bo, NULL);
529 ttm_tt_unbind(bo->ttm);
530 ttm_tt_destroy(bo->ttm);
533 ttm_bo_mem_put(bo, &bo->mem);
534 ttm_bo_unreserve_core(bo);
537 * Since the final reference to this bo may not be dropped by
538 * the current task we have to put a memory barrier here to make
539 * sure the changes done in this function are always visible.
541 * This function only needs protection against the final kref_put.
546 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
548 struct ttm_bo_device *bdev = bo->bdev;
549 struct ttm_bo_global *glob = bo->glob;
550 struct ttm_bo_driver *driver = bdev->driver;
551 void *sync_obj = NULL;
555 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
556 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
558 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
559 (void) ttm_bo_wait(bo, false, false, true);
560 if (!ret && !bo->sync_obj) {
561 lockmgr(&bdev->fence_lock, LK_RELEASE);
562 put_count = ttm_bo_del_from_lru(bo);
564 lockmgr(&glob->lru_lock, LK_RELEASE);
565 ttm_bo_cleanup_memtype_use(bo);
567 ttm_bo_list_ref_sub(bo, put_count, true);
572 sync_obj = driver->sync_obj_ref(bo->sync_obj);
573 lockmgr(&bdev->fence_lock, LK_RELEASE);
576 ttm_bo_unreserve_core(bo);
579 kref_get(&bo->list_kref);
580 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
581 lockmgr(&glob->lru_lock, LK_RELEASE);
584 driver->sync_obj_flush(sync_obj);
585 driver->sync_obj_unref(&sync_obj);
587 schedule_delayed_work(&bdev->wq,
588 ((hz / 100) < 1) ? 1 : hz / 100);
592 * function ttm_bo_cleanup_refs_and_unlock
593 * If bo idle, remove from delayed- and lru lists, and unref.
594 * If not idle, do nothing.
596 * Must be called with lru_lock and reservation held, this function
597 * will drop both before returning.
599 * @interruptible Any sleeps should occur interruptibly.
600 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
603 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
607 struct ttm_bo_device *bdev = bo->bdev;
608 struct ttm_bo_driver *driver = bdev->driver;
609 struct ttm_bo_global *glob = bo->glob;
613 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
614 ret = ttm_bo_wait(bo, false, false, true);
616 if (ret && !no_wait_gpu) {
620 * Take a reference to the fence and unreserve,
621 * at this point the buffer should be dead, so
622 * no new sync objects can be attached.
624 sync_obj = driver->sync_obj_ref(bo->sync_obj);
625 lockmgr(&bdev->fence_lock, LK_RELEASE);
627 ttm_bo_unreserve_core(bo);
628 lockmgr(&glob->lru_lock, LK_RELEASE);
630 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
631 driver->sync_obj_unref(&sync_obj);
636 * remove sync_obj with ttm_bo_wait, the wait should be
637 * finished, and no new wait object should have been added.
639 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
640 ret = ttm_bo_wait(bo, false, false, true);
642 lockmgr(&bdev->fence_lock, LK_RELEASE);
646 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
647 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
650 * We raced, and lost, someone else holds the reservation now,
651 * and is probably busy in ttm_bo_cleanup_memtype_use.
653 * Even if it's not the case, because we finished waiting any
654 * delayed destruction would succeed, so just return success
658 lockmgr(&glob->lru_lock, LK_RELEASE);
662 lockmgr(&bdev->fence_lock, LK_RELEASE);
664 if (ret || unlikely(list_empty(&bo->ddestroy))) {
665 ttm_bo_unreserve_core(bo);
666 lockmgr(&glob->lru_lock, LK_RELEASE);
670 put_count = ttm_bo_del_from_lru(bo);
671 list_del_init(&bo->ddestroy);
674 lockmgr(&glob->lru_lock, LK_RELEASE);
675 ttm_bo_cleanup_memtype_use(bo);
677 ttm_bo_list_ref_sub(bo, put_count, true);
683 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
684 * encountered buffers.
687 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
689 struct ttm_bo_global *glob = bdev->glob;
690 struct ttm_buffer_object *entry = NULL;
693 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
694 if (list_empty(&bdev->ddestroy))
697 entry = list_first_entry(&bdev->ddestroy,
698 struct ttm_buffer_object, ddestroy);
699 kref_get(&entry->list_kref);
702 struct ttm_buffer_object *nentry = NULL;
704 if (entry->ddestroy.next != &bdev->ddestroy) {
705 nentry = list_first_entry(&entry->ddestroy,
706 struct ttm_buffer_object, ddestroy);
707 kref_get(&nentry->list_kref);
710 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
711 if (remove_all && ret) {
712 lockmgr(&glob->lru_lock, LK_RELEASE);
713 ret = ttm_bo_reserve_nolru(entry, false, false,
715 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
719 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
722 lockmgr(&glob->lru_lock, LK_RELEASE);
724 kref_put(&entry->list_kref, ttm_bo_release_list);
730 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
731 if (list_empty(&entry->ddestroy))
736 lockmgr(&glob->lru_lock, LK_RELEASE);
739 kref_put(&entry->list_kref, ttm_bo_release_list);
743 static void ttm_bo_delayed_workqueue(struct work_struct *work)
745 struct ttm_bo_device *bdev =
746 container_of(work, struct ttm_bo_device, wq.work);
748 if (ttm_bo_delayed_delete(bdev, false)) {
749 schedule_delayed_work(&bdev->wq,
750 ((hz / 100) < 1) ? 1 : hz / 100);
755 * NOTE: bdev->vm_lock already held on call, this function release it.
757 static void ttm_bo_release(struct kref *kref)
759 struct ttm_buffer_object *bo =
760 container_of(kref, struct ttm_buffer_object, kref);
761 struct ttm_bo_device *bdev = bo->bdev;
762 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
765 if (atomic_read(&bo->kref.refcount) > 0) {
766 lockmgr(&bdev->vm_lock, LK_RELEASE);
769 if (likely(bo->vm_node != NULL)) {
770 RB_REMOVE(ttm_bo_device_buffer_objects,
771 &bdev->addr_space_rb, bo);
772 drm_mm_put_block(bo->vm_node);
777 * Should we clean up our implied list_kref? Because ttm_bo_release()
778 * can be called reentrantly due to races (this may not be true any
779 * more with the lock management changes in the deref), it is possible
780 * to get here twice, but there's only one list_kref ref to drop and
781 * in the other path 'bo' can be kfree()d by another thread the
782 * instant we release our lock.
784 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
785 if (release_active) {
786 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
787 lockmgr(&bdev->vm_lock, LK_RELEASE);
788 ttm_mem_io_lock(man, false);
789 ttm_mem_io_free_vm(bo);
790 ttm_mem_io_unlock(man);
791 ttm_bo_cleanup_refs_or_queue(bo);
792 kref_put(&bo->list_kref, ttm_bo_release_list);
794 lockmgr(&bdev->vm_lock, LK_RELEASE);
798 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
800 struct ttm_buffer_object *bo = *p_bo;
801 struct ttm_bo_device *bdev = bo->bdev;
804 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
805 if (kref_put(&bo->kref, ttm_bo_release) == 0)
806 lockmgr(&bdev->vm_lock, LK_RELEASE);
808 EXPORT_SYMBOL(ttm_bo_unref);
810 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
812 return cancel_delayed_work_sync(&bdev->wq);
814 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
816 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
819 schedule_delayed_work(&bdev->wq,
820 ((hz / 100) < 1) ? 1 : hz / 100);
822 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
824 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
827 struct ttm_bo_device *bdev = bo->bdev;
828 struct ttm_mem_reg evict_mem;
829 struct ttm_placement placement;
832 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
833 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
834 lockmgr(&bdev->fence_lock, LK_RELEASE);
836 if (unlikely(ret != 0)) {
837 if (ret != -ERESTARTSYS) {
838 pr_err("Failed to expire sync object before buffer eviction\n");
843 BUG_ON(!ttm_bo_is_reserved(bo));
846 evict_mem.mm_node = NULL;
847 evict_mem.bus.io_reserved_vm = false;
848 evict_mem.bus.io_reserved_count = 0;
852 placement.num_placement = 0;
853 placement.num_busy_placement = 0;
854 bdev->driver->evict_flags(bo, &placement);
855 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
858 if (ret != -ERESTARTSYS) {
859 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
861 ttm_bo_mem_space_debug(bo, &placement);
866 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
869 if (ret != -ERESTARTSYS)
870 pr_err("Buffer eviction failed\n");
871 ttm_bo_mem_put(bo, &evict_mem);
879 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
884 struct ttm_bo_global *glob = bdev->glob;
885 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
886 struct ttm_buffer_object *bo;
887 int ret = -EBUSY, put_count;
889 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
890 list_for_each_entry(bo, &man->lru, lru) {
891 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
897 lockmgr(&glob->lru_lock, LK_RELEASE);
901 kref_get(&bo->list_kref);
903 if (!list_empty(&bo->ddestroy)) {
904 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
906 kref_put(&bo->list_kref, ttm_bo_release_list);
910 put_count = ttm_bo_del_from_lru(bo);
911 lockmgr(&glob->lru_lock, LK_RELEASE);
915 ttm_bo_list_ref_sub(bo, put_count, true);
917 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
918 ttm_bo_unreserve(bo);
920 kref_put(&bo->list_kref, ttm_bo_release_list);
924 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
926 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
929 (*man->func->put_node)(man, mem);
931 EXPORT_SYMBOL(ttm_bo_mem_put);
934 * Repeatedly evict memory from the LRU for @mem_type until we create enough
935 * space, or we've evicted everything and there isn't enough space.
937 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
939 struct ttm_placement *placement,
940 struct ttm_mem_reg *mem,
944 struct ttm_bo_device *bdev = bo->bdev;
945 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
949 ret = (*man->func->get_node)(man, bo, placement, mem);
950 if (unlikely(ret != 0))
954 ret = ttm_mem_evict_first(bdev, mem_type,
955 interruptible, no_wait_gpu);
956 if (unlikely(ret != 0))
959 if (mem->mm_node == NULL)
961 mem->mem_type = mem_type;
965 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
966 uint32_t cur_placement,
967 uint32_t proposed_placement)
969 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
970 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
973 * Keep current caching if possible.
976 if ((cur_placement & caching) != 0)
977 result |= (cur_placement & caching);
978 else if ((man->default_caching & caching) != 0)
979 result |= man->default_caching;
980 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
981 result |= TTM_PL_FLAG_CACHED;
982 else if ((TTM_PL_FLAG_WC & caching) != 0)
983 result |= TTM_PL_FLAG_WC;
984 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
985 result |= TTM_PL_FLAG_UNCACHED;
990 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
992 uint32_t proposed_placement,
993 uint32_t *masked_placement)
995 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
997 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
1000 if ((proposed_placement & man->available_caching) == 0)
1003 cur_flags |= (proposed_placement & man->available_caching);
1005 *masked_placement = cur_flags;
1010 * Creates space for memory region @mem according to its type.
1012 * This function first searches for free space in compatible memory types in
1013 * the priority order defined by the driver. If free space isn't found, then
1014 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1017 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1018 struct ttm_placement *placement,
1019 struct ttm_mem_reg *mem,
1023 struct ttm_bo_device *bdev = bo->bdev;
1024 struct ttm_mem_type_manager *man;
1025 uint32_t mem_type = TTM_PL_SYSTEM;
1026 uint32_t cur_flags = 0;
1027 bool type_found = false;
1028 bool type_ok = false;
1029 bool has_erestartsys = false;
1032 mem->mm_node = NULL;
1033 for (i = 0; i < placement->num_placement; ++i) {
1034 ret = ttm_mem_type_from_flags(placement->placement[i],
1038 man = &bdev->man[mem_type];
1040 type_ok = ttm_bo_mt_compatible(man,
1042 placement->placement[i],
1048 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1051 * Use the access and other non-mapping-related flag bits from
1052 * the memory placement flags to the current flags
1054 ttm_flag_masked(&cur_flags, placement->placement[i],
1055 ~TTM_PL_MASK_MEMTYPE);
1057 if (mem_type == TTM_PL_SYSTEM)
1060 if (man->has_type && man->use_type) {
1062 ret = (*man->func->get_node)(man, bo, placement, mem);
1070 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1071 mem->mem_type = mem_type;
1072 mem->placement = cur_flags;
1079 for (i = 0; i < placement->num_busy_placement; ++i) {
1080 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1084 man = &bdev->man[mem_type];
1087 if (!ttm_bo_mt_compatible(man,
1089 placement->busy_placement[i],
1093 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1096 * Use the access and other non-mapping-related flag bits from
1097 * the memory placement flags to the current flags
1099 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1100 ~TTM_PL_MASK_MEMTYPE);
1103 if (mem_type == TTM_PL_SYSTEM) {
1104 mem->mem_type = mem_type;
1105 mem->placement = cur_flags;
1106 mem->mm_node = NULL;
1110 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1111 interruptible, no_wait_gpu);
1112 if (ret == 0 && mem->mm_node) {
1113 mem->placement = cur_flags;
1116 if (ret == -ERESTARTSYS)
1117 has_erestartsys = true;
1119 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1122 EXPORT_SYMBOL(ttm_bo_mem_space);
1125 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1126 struct ttm_placement *placement,
1131 struct ttm_mem_reg mem;
1132 struct ttm_bo_device *bdev = bo->bdev;
1134 BUG_ON(!ttm_bo_is_reserved(bo));
1137 * FIXME: It's possible to pipeline buffer moves.
1138 * Have the driver move function wait for idle when necessary,
1139 * instead of doing it here.
1141 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1142 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1143 lockmgr(&bdev->fence_lock, LK_RELEASE);
1146 mem.num_pages = bo->num_pages;
1147 mem.size = mem.num_pages << PAGE_SHIFT;
1148 mem.page_alignment = bo->mem.page_alignment;
1149 mem.bus.io_reserved_vm = false;
1150 mem.bus.io_reserved_count = 0;
1152 * Determine where to move the buffer.
1154 ret = ttm_bo_mem_space(bo, placement, &mem,
1155 interruptible, no_wait_gpu);
1158 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1159 interruptible, no_wait_gpu);
1161 if (ret && mem.mm_node)
1162 ttm_bo_mem_put(bo, &mem);
1166 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1167 struct ttm_mem_reg *mem)
1171 if (mem->mm_node && placement->lpfn != 0 &&
1172 (mem->start < placement->fpfn ||
1173 mem->start + mem->num_pages > placement->lpfn))
1176 for (i = 0; i < placement->num_placement; i++) {
1177 if ((placement->placement[i] & mem->placement &
1178 TTM_PL_MASK_CACHING) &&
1179 (placement->placement[i] & mem->placement &
1186 int ttm_bo_validate(struct ttm_buffer_object *bo,
1187 struct ttm_placement *placement,
1193 BUG_ON(!ttm_bo_is_reserved(bo));
1194 /* Check that range is valid */
1195 if (placement->lpfn || placement->fpfn)
1196 if (placement->fpfn > placement->lpfn ||
1197 (placement->lpfn - placement->fpfn) < bo->num_pages)
1200 * Check whether we need to move buffer.
1202 ret = ttm_bo_mem_compat(placement, &bo->mem);
1204 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1210 * Use the access and other non-mapping-related flag bits from
1211 * the compatible memory placement flags to the active flags
1213 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1214 ~TTM_PL_MASK_MEMTYPE);
1217 * We might need to add a TTM.
1219 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1220 ret = ttm_bo_add_ttm(bo, true);
1226 EXPORT_SYMBOL(ttm_bo_validate);
1228 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1229 struct ttm_placement *placement)
1231 BUG_ON((placement->fpfn || placement->lpfn) &&
1232 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1237 int ttm_bo_init(struct ttm_bo_device *bdev,
1238 struct ttm_buffer_object *bo,
1240 enum ttm_bo_type type,
1241 struct ttm_placement *placement,
1242 uint32_t page_alignment,
1244 struct vm_object *persistent_swap_storage,
1246 struct sg_table *sg,
1247 void (*destroy) (struct ttm_buffer_object *))
1250 unsigned long num_pages;
1251 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1253 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1255 kprintf("[TTM] Out of kernel memory\n");
1263 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1264 if (num_pages == 0) {
1265 kprintf("[TTM] Illegal buffer object size\n");
1270 ttm_mem_global_free(mem_glob, acc_size);
1273 bo->destroy = destroy;
1275 kref_init(&bo->kref);
1276 kref_init(&bo->list_kref);
1277 atomic_set(&bo->cpu_writers, 0);
1278 atomic_set(&bo->reserved, 1);
1279 init_waitqueue_head(&bo->event_queue);
1280 INIT_LIST_HEAD(&bo->lru);
1281 INIT_LIST_HEAD(&bo->ddestroy);
1282 INIT_LIST_HEAD(&bo->swap);
1283 INIT_LIST_HEAD(&bo->io_reserve_lru);
1284 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1286 bo->glob = bdev->glob;
1288 bo->num_pages = num_pages;
1289 bo->mem.size = num_pages << PAGE_SHIFT;
1290 bo->mem.mem_type = TTM_PL_SYSTEM;
1291 bo->mem.num_pages = bo->num_pages;
1292 bo->mem.mm_node = NULL;
1293 bo->mem.page_alignment = page_alignment;
1294 bo->mem.bus.io_reserved_vm = false;
1295 bo->mem.bus.io_reserved_count = 0;
1297 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1298 bo->seq_valid = false;
1299 bo->persistent_swap_storage = persistent_swap_storage;
1300 bo->acc_size = acc_size;
1302 atomic_inc(&bo->glob->bo_count);
1305 * Mirror ref from kref_init() for list_kref.
1307 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1309 ret = ttm_bo_check_placement(bo, placement);
1310 if (unlikely(ret != 0))
1314 * For ttm_bo_type_device buffers, allocate
1315 * address space from the device.
1317 if (bo->type == ttm_bo_type_device ||
1318 bo->type == ttm_bo_type_sg) {
1319 ret = ttm_bo_setup_vm(bo);
1324 ret = ttm_bo_validate(bo, placement, interruptible, false);
1328 ttm_bo_unreserve(bo);
1332 ttm_bo_unreserve(bo);
1337 EXPORT_SYMBOL(ttm_bo_init);
1339 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1340 unsigned long bo_size,
1341 unsigned struct_size)
1343 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1346 size += ttm_round_pot(struct_size);
1347 size += PAGE_ALIGN(npages * sizeof(void *));
1348 size += ttm_round_pot(sizeof(struct ttm_tt));
1351 EXPORT_SYMBOL(ttm_bo_acc_size);
1353 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1354 unsigned long bo_size,
1355 unsigned struct_size)
1357 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1360 size += ttm_round_pot(struct_size);
1361 size += PAGE_ALIGN(npages * sizeof(void *));
1362 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1363 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1366 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1368 int ttm_bo_create(struct ttm_bo_device *bdev,
1370 enum ttm_bo_type type,
1371 struct ttm_placement *placement,
1372 uint32_t page_alignment,
1374 struct vm_object *persistent_swap_storage,
1375 struct ttm_buffer_object **p_bo)
1377 struct ttm_buffer_object *bo;
1382 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1383 if (unlikely(bo == NULL))
1386 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1387 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1388 interruptible, persistent_swap_storage, acc_size,
1390 if (likely(ret == 0))
1395 EXPORT_SYMBOL(ttm_bo_create);
1397 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1398 unsigned mem_type, bool allow_errors)
1400 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1401 struct ttm_bo_global *glob = bdev->glob;
1405 * Can't use standard list traversal since we're unlocking.
1408 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1409 while (!list_empty(&man->lru)) {
1410 lockmgr(&glob->lru_lock, LK_RELEASE);
1411 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1416 kprintf("[TTM] Cleanup eviction failed\n");
1419 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1421 lockmgr(&glob->lru_lock, LK_RELEASE);
1425 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1427 struct ttm_mem_type_manager *man;
1430 if (mem_type >= TTM_NUM_MEM_TYPES) {
1431 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1434 man = &bdev->man[mem_type];
1436 if (!man->has_type) {
1437 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1442 man->use_type = false;
1443 man->has_type = false;
1447 ttm_bo_force_list_clean(bdev, mem_type, false);
1449 ret = (*man->func->takedown)(man);
1454 EXPORT_SYMBOL(ttm_bo_clean_mm);
1456 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1458 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1460 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1461 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1465 if (!man->has_type) {
1466 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1470 return ttm_bo_force_list_clean(bdev, mem_type, true);
1472 EXPORT_SYMBOL(ttm_bo_evict_mm);
1474 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1475 unsigned long p_size)
1478 struct ttm_mem_type_manager *man;
1480 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1481 man = &bdev->man[type];
1482 BUG_ON(man->has_type);
1483 man->io_reserve_fastpath = true;
1484 man->use_io_reserve_lru = false;
1485 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1486 INIT_LIST_HEAD(&man->io_reserve_lru);
1488 ret = bdev->driver->init_mem_type(bdev, type, man);
1494 if (type != TTM_PL_SYSTEM) {
1495 ret = (*man->func->init)(man, p_size);
1499 man->has_type = true;
1500 man->use_type = true;
1503 INIT_LIST_HEAD(&man->lru);
1507 EXPORT_SYMBOL(ttm_bo_init_mm);
1509 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1511 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1512 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1513 glob->dummy_read_page = NULL;
1515 vm_page_free(glob->dummy_read_page);
1519 void ttm_bo_global_release(struct drm_global_reference *ref)
1521 struct ttm_bo_global *glob = ref->object;
1523 if (refcount_release(&glob->kobj_ref))
1524 ttm_bo_global_kobj_release(glob);
1526 EXPORT_SYMBOL(ttm_bo_global_release);
1528 int ttm_bo_global_init(struct drm_global_reference *ref)
1530 struct ttm_bo_global_ref *bo_ref =
1531 container_of(ref, struct ttm_bo_global_ref, ref);
1532 struct ttm_bo_global *glob = ref->object;
1535 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1536 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1537 glob->mem_glob = bo_ref->mem_glob;
1538 glob->dummy_read_page = vm_page_alloc_contig(
1539 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1541 if (unlikely(glob->dummy_read_page == NULL)) {
1546 INIT_LIST_HEAD(&glob->swap_lru);
1547 INIT_LIST_HEAD(&glob->device_list);
1549 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1550 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1551 if (unlikely(ret != 0)) {
1552 kprintf("[TTM] Could not register buffer object swapout\n");
1556 atomic_set(&glob->bo_count, 0);
1558 refcount_init(&glob->kobj_ref, 1);
1562 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1563 glob->dummy_read_page = NULL;
1565 vm_page_free(glob->dummy_read_page);
1571 EXPORT_SYMBOL(ttm_bo_global_init);
1574 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1577 unsigned i = TTM_NUM_MEM_TYPES;
1578 struct ttm_mem_type_manager *man;
1579 struct ttm_bo_global *glob = bdev->glob;
1582 man = &bdev->man[i];
1583 if (man->has_type) {
1584 man->use_type = false;
1585 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1587 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1590 man->has_type = false;
1594 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1595 list_del(&bdev->device_list);
1596 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1598 cancel_delayed_work_sync(&bdev->wq);
1600 while (ttm_bo_delayed_delete(bdev, true))
1603 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1604 if (list_empty(&bdev->ddestroy))
1605 TTM_DEBUG("Delayed destroy list was clean\n");
1607 if (list_empty(&bdev->man[0].lru))
1608 TTM_DEBUG("Swap list was clean\n");
1609 lockmgr(&glob->lru_lock, LK_RELEASE);
1611 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1612 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1613 drm_mm_takedown(&bdev->addr_space_mm);
1614 lockmgr(&bdev->vm_lock, LK_RELEASE);
1618 EXPORT_SYMBOL(ttm_bo_device_release);
1620 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1621 struct ttm_bo_global *glob,
1622 struct ttm_bo_driver *driver,
1623 uint64_t file_page_offset,
1628 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1629 bdev->driver = driver;
1631 memset(bdev->man, 0, sizeof(bdev->man));
1634 * Initialize the system memory buffer type.
1635 * Other types need to be driver / IOCTL initialized.
1637 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1638 if (unlikely(ret != 0))
1641 RB_INIT(&bdev->addr_space_rb);
1642 drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1644 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1645 INIT_LIST_HEAD(&bdev->ddestroy);
1646 bdev->dev_mapping = NULL;
1648 bdev->need_dma32 = need_dma32;
1650 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1651 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1652 list_add_tail(&bdev->device_list, &glob->device_list);
1653 lockmgr(&glob->device_list_mutex, LK_RELEASE);
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 ttm_bo_unreserve_core(bo);
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);