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_place(const struct ttm_place *place,
54 for (i = 0; i <= TTM_PL_PRIV5; i++)
55 if (place->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_place(&placement->placement[i],
90 kprintf(" placement[%d]=0x%08X (%d)\n",
91 i, placement->placement[i].flags, 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_ticket,
195 struct ww_acquire_ctx *ticket)
199 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
201 * Deadlock avoidance for multi-bo reserving.
203 if (use_ticket && bo->seq_valid) {
205 * We've already reserved this one.
207 if (unlikely(ticket->stamp == bo->val_seq))
210 * Already reserved by a thread that will not back
211 * off for us. We need to back off.
213 if (unlikely(ticket->stamp - bo->val_seq <= LONG_MAX))
220 ret = ttm_bo_wait_unreserved(bo, interruptible);
227 bool wake_up = false;
230 * Wake up waiters that may need to recheck for deadlock,
231 * if we decreased the sequence number.
233 if (unlikely((bo->val_seq - ticket->stamp <= LONG_MAX)
238 * In the worst case with memory ordering these values can be
239 * seen in the wrong order. However since we call wake_up_all
240 * in that case, this will hopefully not pose a problem,
241 * and the worst case would only cause someone to accidentally
242 * hit -EAGAIN in ttm_bo_reserve when they see old value of
243 * val_seq. However this would only happen if seq_valid was
244 * written before val_seq was, and just means some slightly
245 * increased cpu usage
247 bo->val_seq = ticket->stamp;
248 bo->seq_valid = true;
250 wake_up_all(&bo->event_queue);
252 bo->seq_valid = false;
257 EXPORT_SYMBOL(ttm_bo_reserve);
259 static void ttm_bo_ref_bug(struct kref *list_kref)
264 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
267 kref_sub(&bo->list_kref, count,
268 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
271 int ttm_bo_reserve(struct ttm_buffer_object *bo,
273 bool no_wait, bool use_ticket,
274 struct ww_acquire_ctx *ticket)
276 struct ttm_bo_global *glob = bo->glob;
280 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_ticket,
282 if (likely(ret == 0)) {
283 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
284 put_count = ttm_bo_del_from_lru(bo);
285 lockmgr(&glob->lru_lock, LK_RELEASE);
286 ttm_bo_list_ref_sub(bo, put_count, true);
292 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
294 struct ww_acquire_ctx *ticket)
296 bool wake_up = false;
299 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
300 WARN_ON(bo->seq_valid && ticket->stamp == bo->val_seq);
302 ret = ttm_bo_wait_unreserved(bo, interruptible);
308 if (bo->val_seq - ticket->stamp < LONG_MAX || !bo->seq_valid)
312 * Wake up waiters that may need to recheck for deadlock,
313 * if we decreased the sequence number.
315 bo->val_seq = ticket->stamp;
316 bo->seq_valid = true;
318 wake_up_all(&bo->event_queue);
323 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
324 bool interruptible, struct ww_acquire_ctx *ticket)
326 struct ttm_bo_global *glob = bo->glob;
329 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, ticket);
331 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
332 put_count = ttm_bo_del_from_lru(bo);
333 lockmgr(&glob->lru_lock, LK_RELEASE);
334 ttm_bo_list_ref_sub(bo, put_count, true);
338 EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
341 * Must interlock with event_queue to avoid race against
342 * wait_event_common() which can cause wait_event_common()
346 ttm_bo_unreserve_core(struct ttm_buffer_object *bo)
348 lockmgr(&bo->event_queue.lock, LK_EXCLUSIVE);
349 atomic_set(&bo->reserved, 0);
350 lockmgr(&bo->event_queue.lock, LK_RELEASE);
351 wake_up_all(&bo->event_queue);
354 void ttm_bo_unreserve_ticket_locked(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
356 ttm_bo_add_to_lru(bo);
357 ttm_bo_unreserve_core(bo);
360 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
362 struct ttm_bo_global *glob = bo->glob;
364 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
365 ttm_bo_unreserve_ticket_locked(bo, NULL);
366 lockmgr(&glob->lru_lock, LK_RELEASE);
368 EXPORT_SYMBOL(ttm_bo_unreserve);
370 void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
372 struct ttm_bo_global *glob = bo->glob;
374 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
375 ttm_bo_unreserve_ticket_locked(bo, ticket);
376 lockmgr(&glob->lru_lock, LK_RELEASE);
378 EXPORT_SYMBOL(ttm_bo_unreserve_ticket);
381 * Call bo->mutex locked.
383 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
385 struct ttm_bo_device *bdev = bo->bdev;
386 struct ttm_bo_global *glob = bo->glob;
388 uint32_t page_flags = 0;
390 TTM_ASSERT_LOCKED(&bo->mutex);
393 if (bdev->need_dma32)
394 page_flags |= TTM_PAGE_FLAG_DMA32;
397 case ttm_bo_type_device:
399 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
400 case ttm_bo_type_kernel:
401 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
402 page_flags, glob->dummy_read_page);
403 if (unlikely(bo->ttm == NULL))
407 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
408 page_flags | TTM_PAGE_FLAG_SG,
409 glob->dummy_read_page);
410 if (unlikely(bo->ttm == NULL)) {
414 bo->ttm->sg = bo->sg;
417 kprintf("[TTM] Illegal buffer object type\n");
425 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
426 struct ttm_mem_reg *mem,
427 bool evict, bool interruptible,
430 struct ttm_bo_device *bdev = bo->bdev;
431 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
432 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
433 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
434 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
437 if (old_is_pci || new_is_pci ||
438 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
439 ret = ttm_mem_io_lock(old_man, true);
440 if (unlikely(ret != 0))
442 ttm_bo_unmap_virtual_locked(bo);
443 ttm_mem_io_unlock(old_man);
447 * Create and bind a ttm if required.
450 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
451 if (bo->ttm == NULL) {
452 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
453 ret = ttm_bo_add_ttm(bo, zero);
458 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
462 if (mem->mem_type != TTM_PL_SYSTEM) {
463 ret = ttm_tt_bind(bo->ttm, mem);
468 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
469 if (bdev->driver->move_notify)
470 bdev->driver->move_notify(bo, mem);
477 if (bdev->driver->move_notify)
478 bdev->driver->move_notify(bo, mem);
480 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
481 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
482 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
483 else if (bdev->driver->move)
484 ret = bdev->driver->move(bo, evict, interruptible,
487 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
490 if (bdev->driver->move_notify) {
491 struct ttm_mem_reg tmp_mem = *mem;
494 bdev->driver->move_notify(bo, mem);
504 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
506 kprintf("[TTM] Can not flush read caches\n");
510 if (bo->mem.mm_node) {
511 bo->offset = (bo->mem.start << PAGE_SHIFT) +
512 bdev->man[bo->mem.mem_type].gpu_offset;
513 bo->cur_placement = bo->mem.placement;
520 new_man = &bdev->man[bo->mem.mem_type];
521 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
522 ttm_tt_unbind(bo->ttm);
523 ttm_tt_destroy(bo->ttm);
532 * Will release GPU memory type usage on destruction.
533 * This is the place to put in driver specific hooks to release
534 * driver private resources.
535 * Will release the bo::reserved lock.
538 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
540 if (bo->bdev->driver->move_notify)
541 bo->bdev->driver->move_notify(bo, NULL);
544 ttm_tt_unbind(bo->ttm);
545 ttm_tt_destroy(bo->ttm);
548 ttm_bo_mem_put(bo, &bo->mem);
549 ttm_bo_unreserve_core(bo);
552 * Since the final reference to this bo may not be dropped by
553 * the current task we have to put a memory barrier here to make
554 * sure the changes done in this function are always visible.
556 * This function only needs protection against the final kref_put.
561 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
563 struct ttm_bo_device *bdev = bo->bdev;
564 struct ttm_bo_global *glob = bo->glob;
565 struct ttm_bo_driver *driver = bdev->driver;
566 void *sync_obj = NULL;
570 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
571 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
573 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
574 (void) ttm_bo_wait(bo, false, false, true);
575 if (!ret && !bo->sync_obj) {
576 lockmgr(&bdev->fence_lock, LK_RELEASE);
577 put_count = ttm_bo_del_from_lru(bo);
579 lockmgr(&glob->lru_lock, LK_RELEASE);
580 ttm_bo_cleanup_memtype_use(bo);
582 ttm_bo_list_ref_sub(bo, put_count, true);
587 sync_obj = driver->sync_obj_ref(bo->sync_obj);
588 lockmgr(&bdev->fence_lock, LK_RELEASE);
593 * Make NO_EVICT bos immediately available to
594 * shrinkers, now that they are queued for
597 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
598 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
599 ttm_bo_add_to_lru(bo);
602 ttm_bo_unreserve_core(bo);
605 kref_get(&bo->list_kref);
606 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
607 lockmgr(&glob->lru_lock, LK_RELEASE);
610 driver->sync_obj_flush(sync_obj);
611 driver->sync_obj_unref(&sync_obj);
613 schedule_delayed_work(&bdev->wq,
614 ((hz / 100) < 1) ? 1 : hz / 100);
618 * function ttm_bo_cleanup_refs_and_unlock
619 * If bo idle, remove from delayed- and lru lists, and unref.
620 * If not idle, do nothing.
622 * Must be called with lru_lock and reservation held, this function
623 * will drop both before returning.
625 * @interruptible Any sleeps should occur interruptibly.
626 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
629 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
633 struct ttm_bo_device *bdev = bo->bdev;
634 struct ttm_bo_driver *driver = bdev->driver;
635 struct ttm_bo_global *glob = bo->glob;
639 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
640 ret = ttm_bo_wait(bo, false, false, true);
642 if (ret && !no_wait_gpu) {
646 * Take a reference to the fence and unreserve,
647 * at this point the buffer should be dead, so
648 * no new sync objects can be attached.
650 sync_obj = driver->sync_obj_ref(bo->sync_obj);
651 lockmgr(&bdev->fence_lock, LK_RELEASE);
653 ttm_bo_unreserve_core(bo);
654 lockmgr(&glob->lru_lock, LK_RELEASE);
656 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
657 driver->sync_obj_unref(&sync_obj);
662 * remove sync_obj with ttm_bo_wait, the wait should be
663 * finished, and no new wait object should have been added.
665 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
666 ret = ttm_bo_wait(bo, false, false, true);
668 lockmgr(&bdev->fence_lock, LK_RELEASE);
672 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
673 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
676 * We raced, and lost, someone else holds the reservation now,
677 * and is probably busy in ttm_bo_cleanup_memtype_use.
679 * Even if it's not the case, because we finished waiting any
680 * delayed destruction would succeed, so just return success
684 lockmgr(&glob->lru_lock, LK_RELEASE);
688 lockmgr(&bdev->fence_lock, LK_RELEASE);
690 if (ret || unlikely(list_empty(&bo->ddestroy))) {
691 ttm_bo_unreserve_core(bo);
692 lockmgr(&glob->lru_lock, LK_RELEASE);
696 put_count = ttm_bo_del_from_lru(bo);
697 list_del_init(&bo->ddestroy);
700 lockmgr(&glob->lru_lock, LK_RELEASE);
701 ttm_bo_cleanup_memtype_use(bo);
703 ttm_bo_list_ref_sub(bo, put_count, true);
709 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
710 * encountered buffers.
713 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
715 struct ttm_bo_global *glob = bdev->glob;
716 struct ttm_buffer_object *entry = NULL;
719 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
720 if (list_empty(&bdev->ddestroy))
723 entry = list_first_entry(&bdev->ddestroy,
724 struct ttm_buffer_object, ddestroy);
725 kref_get(&entry->list_kref);
728 struct ttm_buffer_object *nentry = NULL;
730 if (entry->ddestroy.next != &bdev->ddestroy) {
731 nentry = list_first_entry(&entry->ddestroy,
732 struct ttm_buffer_object, ddestroy);
733 kref_get(&nentry->list_kref);
736 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
737 if (remove_all && ret) {
738 lockmgr(&glob->lru_lock, LK_RELEASE);
739 ret = ttm_bo_reserve_nolru(entry, false, false,
741 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
745 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
748 lockmgr(&glob->lru_lock, LK_RELEASE);
750 kref_put(&entry->list_kref, ttm_bo_release_list);
756 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
757 if (list_empty(&entry->ddestroy))
762 lockmgr(&glob->lru_lock, LK_RELEASE);
765 kref_put(&entry->list_kref, ttm_bo_release_list);
769 static void ttm_bo_delayed_workqueue(struct work_struct *work)
771 struct ttm_bo_device *bdev =
772 container_of(work, struct ttm_bo_device, wq.work);
774 if (ttm_bo_delayed_delete(bdev, false)) {
775 schedule_delayed_work(&bdev->wq,
776 ((hz / 100) < 1) ? 1 : hz / 100);
781 * NOTE: bdev->vm_lock already held on call, this function release it.
783 static void ttm_bo_release(struct kref *kref)
785 struct ttm_buffer_object *bo =
786 container_of(kref, struct ttm_buffer_object, kref);
787 struct ttm_bo_device *bdev = bo->bdev;
788 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
791 if (atomic_read(&bo->kref.refcount) > 0) {
792 lockmgr(&bdev->vm_lock, LK_RELEASE);
795 if (likely(bo->vm_node != NULL)) {
796 RB_REMOVE(ttm_bo_device_buffer_objects,
797 &bdev->addr_space_rb, bo);
798 drm_mm_put_block(bo->vm_node);
803 * Should we clean up our implied list_kref? Because ttm_bo_release()
804 * can be called reentrantly due to races (this may not be true any
805 * more with the lock management changes in the deref), it is possible
806 * to get here twice, but there's only one list_kref ref to drop and
807 * in the other path 'bo' can be kfree()d by another thread the
808 * instant we release our lock.
810 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
811 if (release_active) {
812 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
813 lockmgr(&bdev->vm_lock, LK_RELEASE);
814 ttm_mem_io_lock(man, false);
815 ttm_mem_io_free_vm(bo);
816 ttm_mem_io_unlock(man);
817 ttm_bo_cleanup_refs_or_queue(bo);
818 kref_put(&bo->list_kref, ttm_bo_release_list);
820 lockmgr(&bdev->vm_lock, LK_RELEASE);
824 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
826 struct ttm_buffer_object *bo = *p_bo;
827 struct ttm_bo_device *bdev = bo->bdev;
830 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
831 if (kref_put(&bo->kref, ttm_bo_release) == 0)
832 lockmgr(&bdev->vm_lock, LK_RELEASE);
834 EXPORT_SYMBOL(ttm_bo_unref);
836 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
838 return cancel_delayed_work_sync(&bdev->wq);
840 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
842 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
845 schedule_delayed_work(&bdev->wq,
846 ((hz / 100) < 1) ? 1 : hz / 100);
848 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
850 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
853 struct ttm_bo_device *bdev = bo->bdev;
854 struct ttm_mem_reg evict_mem;
855 struct ttm_placement placement;
858 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
859 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
860 lockmgr(&bdev->fence_lock, LK_RELEASE);
862 if (unlikely(ret != 0)) {
863 if (ret != -ERESTARTSYS) {
864 pr_err("Failed to expire sync object before buffer eviction\n");
869 BUG_ON(!ttm_bo_is_reserved(bo));
872 evict_mem.mm_node = NULL;
873 evict_mem.bus.io_reserved_vm = false;
874 evict_mem.bus.io_reserved_count = 0;
876 placement.num_placement = 0;
877 placement.num_busy_placement = 0;
878 bdev->driver->evict_flags(bo, &placement);
879 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
882 if (ret != -ERESTARTSYS) {
883 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
885 ttm_bo_mem_space_debug(bo, &placement);
890 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
893 if (ret != -ERESTARTSYS)
894 pr_err("Buffer eviction failed\n");
895 ttm_bo_mem_put(bo, &evict_mem);
903 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
908 struct ttm_bo_global *glob = bdev->glob;
909 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
910 struct ttm_buffer_object *bo;
911 int ret = -EBUSY, put_count;
913 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
914 list_for_each_entry(bo, &man->lru, lru) {
915 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
921 lockmgr(&glob->lru_lock, LK_RELEASE);
925 kref_get(&bo->list_kref);
927 if (!list_empty(&bo->ddestroy)) {
928 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
930 kref_put(&bo->list_kref, ttm_bo_release_list);
934 put_count = ttm_bo_del_from_lru(bo);
935 lockmgr(&glob->lru_lock, LK_RELEASE);
939 ttm_bo_list_ref_sub(bo, put_count, true);
941 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
942 ttm_bo_unreserve(bo);
944 kref_put(&bo->list_kref, ttm_bo_release_list);
948 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
950 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
953 (*man->func->put_node)(man, mem);
955 EXPORT_SYMBOL(ttm_bo_mem_put);
958 * Repeatedly evict memory from the LRU for @mem_type until we create enough
959 * space, or we've evicted everything and there isn't enough space.
961 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
963 const struct ttm_place *place,
964 struct ttm_mem_reg *mem,
968 struct ttm_bo_device *bdev = bo->bdev;
969 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
973 ret = (*man->func->get_node)(man, bo, place, mem);
974 if (unlikely(ret != 0))
978 ret = ttm_mem_evict_first(bdev, mem_type,
979 interruptible, no_wait_gpu);
980 if (unlikely(ret != 0))
983 if (mem->mm_node == NULL)
985 mem->mem_type = mem_type;
989 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
990 uint32_t cur_placement,
991 uint32_t proposed_placement)
993 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
994 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
997 * Keep current caching if possible.
1000 if ((cur_placement & caching) != 0)
1001 result |= (cur_placement & caching);
1002 else if ((man->default_caching & caching) != 0)
1003 result |= man->default_caching;
1004 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
1005 result |= TTM_PL_FLAG_CACHED;
1006 else if ((TTM_PL_FLAG_WC & caching) != 0)
1007 result |= TTM_PL_FLAG_WC;
1008 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
1009 result |= TTM_PL_FLAG_UNCACHED;
1014 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
1016 const struct ttm_place *place,
1017 uint32_t *masked_placement)
1019 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
1021 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
1024 if ((place->flags & man->available_caching) == 0)
1027 cur_flags |= (place->flags & man->available_caching);
1029 *masked_placement = cur_flags;
1034 * Creates space for memory region @mem according to its type.
1036 * This function first searches for free space in compatible memory types in
1037 * the priority order defined by the driver. If free space isn't found, then
1038 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1041 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1042 struct ttm_placement *placement,
1043 struct ttm_mem_reg *mem,
1047 struct ttm_bo_device *bdev = bo->bdev;
1048 struct ttm_mem_type_manager *man;
1049 uint32_t mem_type = TTM_PL_SYSTEM;
1050 uint32_t cur_flags = 0;
1051 bool type_found = false;
1052 bool type_ok = false;
1053 bool has_erestartsys = false;
1056 mem->mm_node = NULL;
1057 for (i = 0; i < placement->num_placement; ++i) {
1058 const struct ttm_place *place = &placement->placement[i];
1060 ret = ttm_mem_type_from_place(place, &mem_type);
1063 man = &bdev->man[mem_type];
1065 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
1071 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1074 * Use the access and other non-mapping-related flag bits from
1075 * the memory placement flags to the current flags
1077 ttm_flag_masked(&cur_flags, place->flags,
1078 ~TTM_PL_MASK_MEMTYPE);
1080 if (mem_type == TTM_PL_SYSTEM)
1083 if (man->has_type && man->use_type) {
1085 ret = (*man->func->get_node)(man, bo, place, mem);
1093 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1094 mem->mem_type = mem_type;
1095 mem->placement = cur_flags;
1102 for (i = 0; i < placement->num_busy_placement; ++i) {
1103 const struct ttm_place *place = &placement->busy_placement[i];
1105 ret = ttm_mem_type_from_place(place, &mem_type);
1108 man = &bdev->man[mem_type];
1111 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1114 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1117 * Use the access and other non-mapping-related flag bits from
1118 * the memory placement flags to the current flags
1120 ttm_flag_masked(&cur_flags, place->flags,
1121 ~TTM_PL_MASK_MEMTYPE);
1123 if (mem_type == TTM_PL_SYSTEM) {
1124 mem->mem_type = mem_type;
1125 mem->placement = cur_flags;
1126 mem->mm_node = NULL;
1130 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
1131 interruptible, no_wait_gpu);
1132 if (ret == 0 && mem->mm_node) {
1133 mem->placement = cur_flags;
1136 if (ret == -ERESTARTSYS)
1137 has_erestartsys = true;
1139 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1142 EXPORT_SYMBOL(ttm_bo_mem_space);
1144 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1145 struct ttm_placement *placement,
1150 struct ttm_mem_reg mem;
1151 struct ttm_bo_device *bdev = bo->bdev;
1153 BUG_ON(!ttm_bo_is_reserved(bo));
1156 * FIXME: It's possible to pipeline buffer moves.
1157 * Have the driver move function wait for idle when necessary,
1158 * instead of doing it here.
1160 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1161 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1162 lockmgr(&bdev->fence_lock, LK_RELEASE);
1165 mem.num_pages = bo->num_pages;
1166 mem.size = mem.num_pages << PAGE_SHIFT;
1167 mem.page_alignment = bo->mem.page_alignment;
1168 mem.bus.io_reserved_vm = false;
1169 mem.bus.io_reserved_count = 0;
1171 * Determine where to move the buffer.
1173 ret = ttm_bo_mem_space(bo, placement, &mem,
1174 interruptible, no_wait_gpu);
1177 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1178 interruptible, no_wait_gpu);
1180 if (ret && mem.mm_node)
1181 ttm_bo_mem_put(bo, &mem);
1185 static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1186 struct ttm_mem_reg *mem,
1187 uint32_t *new_flags)
1191 for (i = 0; i < placement->num_placement; i++) {
1192 const struct ttm_place *heap = &placement->placement[i];
1194 (mem->start < heap->fpfn ||
1195 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1198 *new_flags = heap->flags;
1199 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1200 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1204 for (i = 0; i < placement->num_busy_placement; i++) {
1205 const struct ttm_place *heap = &placement->busy_placement[i];
1207 (mem->start < heap->fpfn ||
1208 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1211 *new_flags = heap->flags;
1212 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1213 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1220 int ttm_bo_validate(struct ttm_buffer_object *bo,
1221 struct ttm_placement *placement,
1228 BUG_ON(!ttm_bo_is_reserved(bo));
1230 * Check whether we need to move buffer.
1232 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1233 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1239 * Use the access and other non-mapping-related flag bits from
1240 * the compatible memory placement flags to the active flags
1242 ttm_flag_masked(&bo->mem.placement, new_flags,
1243 ~TTM_PL_MASK_MEMTYPE);
1246 * We might need to add a TTM.
1248 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1249 ret = ttm_bo_add_ttm(bo, true);
1255 EXPORT_SYMBOL(ttm_bo_validate);
1257 int ttm_bo_init(struct ttm_bo_device *bdev,
1258 struct ttm_buffer_object *bo,
1260 enum ttm_bo_type type,
1261 struct ttm_placement *placement,
1262 uint32_t page_alignment,
1264 struct vm_object *persistent_swap_storage,
1266 struct sg_table *sg,
1267 void (*destroy) (struct ttm_buffer_object *))
1270 unsigned long num_pages;
1271 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1273 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1275 kprintf("[TTM] Out of kernel memory\n");
1283 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1284 if (num_pages == 0) {
1285 kprintf("[TTM] Illegal buffer object size\n");
1290 ttm_mem_global_free(mem_glob, acc_size);
1293 bo->destroy = destroy;
1295 kref_init(&bo->kref);
1296 kref_init(&bo->list_kref);
1297 atomic_set(&bo->cpu_writers, 0);
1298 atomic_set(&bo->reserved, 1);
1299 init_waitqueue_head(&bo->event_queue);
1300 INIT_LIST_HEAD(&bo->lru);
1301 INIT_LIST_HEAD(&bo->ddestroy);
1302 INIT_LIST_HEAD(&bo->swap);
1303 INIT_LIST_HEAD(&bo->io_reserve_lru);
1304 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1306 bo->glob = bdev->glob;
1308 bo->num_pages = num_pages;
1309 bo->mem.size = num_pages << PAGE_SHIFT;
1310 bo->mem.mem_type = TTM_PL_SYSTEM;
1311 bo->mem.num_pages = bo->num_pages;
1312 bo->mem.mm_node = NULL;
1313 bo->mem.page_alignment = page_alignment;
1314 bo->mem.bus.io_reserved_vm = false;
1315 bo->mem.bus.io_reserved_count = 0;
1317 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1318 bo->seq_valid = false;
1319 bo->persistent_swap_storage = persistent_swap_storage;
1320 bo->acc_size = acc_size;
1322 atomic_inc(&bo->glob->bo_count);
1325 * Mirror ref from kref_init() for list_kref.
1327 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1330 * For ttm_bo_type_device buffers, allocate
1331 * address space from the device.
1333 if (bo->type == ttm_bo_type_device ||
1334 bo->type == ttm_bo_type_sg) {
1335 ret = ttm_bo_setup_vm(bo);
1340 ret = ttm_bo_validate(bo, placement, interruptible, false);
1344 ttm_bo_unreserve(bo);
1348 ttm_bo_unreserve(bo);
1353 EXPORT_SYMBOL(ttm_bo_init);
1355 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1356 unsigned long bo_size,
1357 unsigned struct_size)
1359 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1362 size += ttm_round_pot(struct_size);
1363 size += PAGE_ALIGN(npages * sizeof(void *));
1364 size += ttm_round_pot(sizeof(struct ttm_tt));
1367 EXPORT_SYMBOL(ttm_bo_acc_size);
1369 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1370 unsigned long bo_size,
1371 unsigned struct_size)
1373 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1376 size += ttm_round_pot(struct_size);
1377 size += PAGE_ALIGN(npages * sizeof(void *));
1378 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1379 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1382 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1384 int ttm_bo_create(struct ttm_bo_device *bdev,
1386 enum ttm_bo_type type,
1387 struct ttm_placement *placement,
1388 uint32_t page_alignment,
1390 struct vm_object *persistent_swap_storage,
1391 struct ttm_buffer_object **p_bo)
1393 struct ttm_buffer_object *bo;
1398 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1399 if (unlikely(bo == NULL))
1402 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1403 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1404 interruptible, persistent_swap_storage, acc_size,
1406 if (likely(ret == 0))
1411 EXPORT_SYMBOL(ttm_bo_create);
1413 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1414 unsigned mem_type, bool allow_errors)
1416 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1417 struct ttm_bo_global *glob = bdev->glob;
1421 * Can't use standard list traversal since we're unlocking.
1424 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1425 while (!list_empty(&man->lru)) {
1426 lockmgr(&glob->lru_lock, LK_RELEASE);
1427 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1432 kprintf("[TTM] Cleanup eviction failed\n");
1435 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1437 lockmgr(&glob->lru_lock, LK_RELEASE);
1441 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1443 struct ttm_mem_type_manager *man;
1446 if (mem_type >= TTM_NUM_MEM_TYPES) {
1447 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1450 man = &bdev->man[mem_type];
1452 if (!man->has_type) {
1453 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1458 man->use_type = false;
1459 man->has_type = false;
1463 ttm_bo_force_list_clean(bdev, mem_type, false);
1465 ret = (*man->func->takedown)(man);
1470 EXPORT_SYMBOL(ttm_bo_clean_mm);
1472 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1474 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1476 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1477 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1481 if (!man->has_type) {
1482 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1486 return ttm_bo_force_list_clean(bdev, mem_type, true);
1488 EXPORT_SYMBOL(ttm_bo_evict_mm);
1490 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1491 unsigned long p_size)
1494 struct ttm_mem_type_manager *man;
1496 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1497 man = &bdev->man[type];
1498 BUG_ON(man->has_type);
1499 man->io_reserve_fastpath = true;
1500 man->use_io_reserve_lru = false;
1501 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1502 INIT_LIST_HEAD(&man->io_reserve_lru);
1504 ret = bdev->driver->init_mem_type(bdev, type, man);
1510 if (type != TTM_PL_SYSTEM) {
1511 ret = (*man->func->init)(man, p_size);
1515 man->has_type = true;
1516 man->use_type = true;
1519 INIT_LIST_HEAD(&man->lru);
1523 EXPORT_SYMBOL(ttm_bo_init_mm);
1525 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1527 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1528 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1529 glob->dummy_read_page = NULL;
1531 vm_page_free(glob->dummy_read_page);
1535 void ttm_bo_global_release(struct drm_global_reference *ref)
1537 struct ttm_bo_global *glob = ref->object;
1539 if (refcount_release(&glob->kobj_ref))
1540 ttm_bo_global_kobj_release(glob);
1542 EXPORT_SYMBOL(ttm_bo_global_release);
1544 int ttm_bo_global_init(struct drm_global_reference *ref)
1546 struct ttm_bo_global_ref *bo_ref =
1547 container_of(ref, struct ttm_bo_global_ref, ref);
1548 struct ttm_bo_global *glob = ref->object;
1551 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1552 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1553 glob->mem_glob = bo_ref->mem_glob;
1554 glob->dummy_read_page = vm_page_alloc_contig(
1555 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1557 if (unlikely(glob->dummy_read_page == NULL)) {
1562 INIT_LIST_HEAD(&glob->swap_lru);
1563 INIT_LIST_HEAD(&glob->device_list);
1565 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1566 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1567 if (unlikely(ret != 0)) {
1568 kprintf("[TTM] Could not register buffer object swapout\n");
1572 atomic_set(&glob->bo_count, 0);
1574 refcount_init(&glob->kobj_ref, 1);
1578 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1579 glob->dummy_read_page = NULL;
1581 vm_page_free(glob->dummy_read_page);
1587 EXPORT_SYMBOL(ttm_bo_global_init);
1590 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1593 unsigned i = TTM_NUM_MEM_TYPES;
1594 struct ttm_mem_type_manager *man;
1595 struct ttm_bo_global *glob = bdev->glob;
1598 man = &bdev->man[i];
1599 if (man->has_type) {
1600 man->use_type = false;
1601 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1603 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1606 man->has_type = false;
1610 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1611 list_del(&bdev->device_list);
1612 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1614 cancel_delayed_work_sync(&bdev->wq);
1616 while (ttm_bo_delayed_delete(bdev, true))
1619 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1620 if (list_empty(&bdev->ddestroy))
1621 TTM_DEBUG("Delayed destroy list was clean\n");
1623 if (list_empty(&bdev->man[0].lru))
1624 TTM_DEBUG("Swap list was clean\n");
1625 lockmgr(&glob->lru_lock, LK_RELEASE);
1627 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1628 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1629 drm_mm_takedown(&bdev->addr_space_mm);
1630 lockmgr(&bdev->vm_lock, LK_RELEASE);
1634 EXPORT_SYMBOL(ttm_bo_device_release);
1636 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1637 struct ttm_bo_global *glob,
1638 struct ttm_bo_driver *driver,
1639 uint64_t file_page_offset,
1644 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1645 bdev->driver = driver;
1647 memset(bdev->man, 0, sizeof(bdev->man));
1650 * Initialize the system memory buffer type.
1651 * Other types need to be driver / IOCTL initialized.
1653 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1654 if (unlikely(ret != 0))
1657 RB_INIT(&bdev->addr_space_rb);
1658 drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1660 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1661 INIT_LIST_HEAD(&bdev->ddestroy);
1662 bdev->dev_mapping = NULL;
1664 bdev->need_dma32 = need_dma32;
1666 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1667 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1668 list_add_tail(&bdev->device_list, &glob->device_list);
1669 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1675 EXPORT_SYMBOL(ttm_bo_device_init);
1678 * buffer object vm functions.
1681 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1683 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1685 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1686 if (mem->mem_type == TTM_PL_SYSTEM)
1689 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1692 if (mem->placement & TTM_PL_FLAG_CACHED)
1698 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1701 ttm_bo_release_mmap(bo);
1702 ttm_mem_io_free_vm(bo);
1705 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1707 struct ttm_bo_device *bdev = bo->bdev;
1708 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1710 ttm_mem_io_lock(man, false);
1711 ttm_bo_unmap_virtual_locked(bo);
1712 ttm_mem_io_unlock(man);
1716 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1718 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1720 struct ttm_bo_device *bdev = bo->bdev;
1722 /* The caller acquired bdev->vm_lock. */
1723 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1729 * @bo: the buffer to allocate address space for
1731 * Allocate address space in the drm device so that applications
1732 * can mmap the buffer and access the contents. This only
1733 * applies to ttm_bo_type_device objects as others are not
1734 * placed in the drm device address space.
1737 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1739 struct ttm_bo_device *bdev = bo->bdev;
1743 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1744 if (unlikely(ret != 0))
1747 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1748 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1749 bo->mem.num_pages, 0, 0);
1751 if (unlikely(bo->vm_node == NULL)) {
1756 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1757 bo->mem.num_pages, 0);
1759 if (unlikely(bo->vm_node == NULL)) {
1760 lockmgr(&bdev->vm_lock, LK_RELEASE);
1764 ttm_bo_vm_insert_rb(bo);
1765 lockmgr(&bdev->vm_lock, LK_RELEASE);
1766 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1770 lockmgr(&bdev->vm_lock, LK_RELEASE);
1774 int ttm_bo_wait(struct ttm_buffer_object *bo,
1775 bool lazy, bool interruptible, bool no_wait)
1777 struct ttm_bo_driver *driver = bo->bdev->driver;
1778 struct ttm_bo_device *bdev = bo->bdev;
1782 if (likely(bo->sync_obj == NULL))
1785 while (bo->sync_obj) {
1787 if (driver->sync_obj_signaled(bo->sync_obj)) {
1788 void *tmp_obj = bo->sync_obj;
1789 bo->sync_obj = NULL;
1790 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1791 lockmgr(&bdev->fence_lock, LK_RELEASE);
1792 driver->sync_obj_unref(&tmp_obj);
1793 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1800 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1801 lockmgr(&bdev->fence_lock, LK_RELEASE);
1802 ret = driver->sync_obj_wait(sync_obj,
1803 lazy, interruptible);
1804 if (unlikely(ret != 0)) {
1805 driver->sync_obj_unref(&sync_obj);
1806 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1809 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1810 if (likely(bo->sync_obj == sync_obj)) {
1811 void *tmp_obj = bo->sync_obj;
1812 bo->sync_obj = NULL;
1813 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1815 lockmgr(&bdev->fence_lock, LK_RELEASE);
1816 driver->sync_obj_unref(&sync_obj);
1817 driver->sync_obj_unref(&tmp_obj);
1818 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1820 lockmgr(&bdev->fence_lock, LK_RELEASE);
1821 driver->sync_obj_unref(&sync_obj);
1822 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1827 EXPORT_SYMBOL(ttm_bo_wait);
1829 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1831 struct ttm_bo_device *bdev = bo->bdev;
1835 * Using ttm_bo_reserve makes sure the lru lists are updated.
1838 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1839 if (unlikely(ret != 0))
1841 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1842 ret = ttm_bo_wait(bo, false, true, no_wait);
1843 lockmgr(&bdev->fence_lock, LK_RELEASE);
1844 if (likely(ret == 0))
1845 atomic_inc(&bo->cpu_writers);
1846 ttm_bo_unreserve(bo);
1849 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1851 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1853 atomic_dec(&bo->cpu_writers);
1855 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1858 * A buffer object shrink method that tries to swap out the first
1859 * buffer object on the bo_global::swap_lru list.
1862 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1864 struct ttm_bo_global *glob =
1865 container_of(shrink, struct ttm_bo_global, shrink);
1866 struct ttm_buffer_object *bo;
1869 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1871 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1872 list_for_each_entry(bo, &glob->swap_lru, swap) {
1873 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1879 lockmgr(&glob->lru_lock, LK_RELEASE);
1883 kref_get(&bo->list_kref);
1885 if (!list_empty(&bo->ddestroy)) {
1886 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1887 kref_put(&bo->list_kref, ttm_bo_release_list);
1891 put_count = ttm_bo_del_from_lru(bo);
1892 lockmgr(&glob->lru_lock, LK_RELEASE);
1894 ttm_bo_list_ref_sub(bo, put_count, true);
1897 * Wait for GPU, then move to system cached.
1900 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1901 ret = ttm_bo_wait(bo, false, false, false);
1902 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1904 if (unlikely(ret != 0))
1907 if ((bo->mem.placement & swap_placement) != swap_placement) {
1908 struct ttm_mem_reg evict_mem;
1910 evict_mem = bo->mem;
1911 evict_mem.mm_node = NULL;
1912 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1913 evict_mem.mem_type = TTM_PL_SYSTEM;
1915 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1917 if (unlikely(ret != 0))
1921 ttm_bo_unmap_virtual(bo);
1924 * Swap out. Buffer will be swapped in again as soon as
1925 * anyone tries to access a ttm page.
1928 if (bo->bdev->driver->swap_notify)
1929 bo->bdev->driver->swap_notify(bo);
1931 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1936 * Unreserve without putting on LRU to avoid swapping out an
1937 * already swapped buffer.
1940 ttm_bo_unreserve_core(bo);
1941 kref_put(&bo->list_kref, ttm_bo_release_list);
1945 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1947 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1950 EXPORT_SYMBOL(ttm_bo_swapout_all);