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);
335 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
337 ttm_bo_add_to_lru(bo);
338 atomic_set(&bo->reserved, 0);
339 wake_up_all(&bo->event_queue);
342 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
344 struct ttm_bo_global *glob = bo->glob;
346 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
347 ttm_bo_unreserve_locked(bo);
348 lockmgr(&glob->lru_lock, LK_RELEASE);
350 EXPORT_SYMBOL(ttm_bo_unreserve);
353 * Call bo->mutex locked.
355 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
357 struct ttm_bo_device *bdev = bo->bdev;
358 struct ttm_bo_global *glob = bo->glob;
360 uint32_t page_flags = 0;
362 TTM_ASSERT_LOCKED(&bo->mutex);
365 if (bdev->need_dma32)
366 page_flags |= TTM_PAGE_FLAG_DMA32;
369 case ttm_bo_type_device:
371 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
372 case ttm_bo_type_kernel:
373 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
374 page_flags, glob->dummy_read_page);
375 if (unlikely(bo->ttm == NULL))
379 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
380 page_flags | TTM_PAGE_FLAG_SG,
381 glob->dummy_read_page);
382 if (unlikely(bo->ttm == NULL)) {
386 bo->ttm->sg = bo->sg;
389 kprintf("[TTM] Illegal buffer object type\n");
397 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
398 struct ttm_mem_reg *mem,
399 bool evict, bool interruptible,
402 struct ttm_bo_device *bdev = bo->bdev;
403 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
404 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
405 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
406 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
409 if (old_is_pci || new_is_pci ||
410 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
411 ret = ttm_mem_io_lock(old_man, true);
412 if (unlikely(ret != 0))
414 ttm_bo_unmap_virtual_locked(bo);
415 ttm_mem_io_unlock(old_man);
419 * Create and bind a ttm if required.
422 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
423 if (bo->ttm == NULL) {
424 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
425 ret = ttm_bo_add_ttm(bo, zero);
430 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
434 if (mem->mem_type != TTM_PL_SYSTEM) {
435 ret = ttm_tt_bind(bo->ttm, mem);
440 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
441 if (bdev->driver->move_notify)
442 bdev->driver->move_notify(bo, mem);
449 if (bdev->driver->move_notify)
450 bdev->driver->move_notify(bo, mem);
452 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
453 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
454 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
455 else if (bdev->driver->move)
456 ret = bdev->driver->move(bo, evict, interruptible,
459 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
462 if (bdev->driver->move_notify) {
463 struct ttm_mem_reg tmp_mem = *mem;
466 bdev->driver->move_notify(bo, mem);
476 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
478 kprintf("[TTM] Can not flush read caches\n");
482 if (bo->mem.mm_node) {
483 bo->offset = (bo->mem.start << PAGE_SHIFT) +
484 bdev->man[bo->mem.mem_type].gpu_offset;
485 bo->cur_placement = bo->mem.placement;
492 new_man = &bdev->man[bo->mem.mem_type];
493 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
494 ttm_tt_unbind(bo->ttm);
495 ttm_tt_destroy(bo->ttm);
504 * Will release GPU memory type usage on destruction.
505 * This is the place to put in driver specific hooks to release
506 * driver private resources.
507 * Will release the bo::reserved lock.
510 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
512 if (bo->bdev->driver->move_notify)
513 bo->bdev->driver->move_notify(bo, NULL);
516 ttm_tt_unbind(bo->ttm);
517 ttm_tt_destroy(bo->ttm);
520 ttm_bo_mem_put(bo, &bo->mem);
522 atomic_set(&bo->reserved, 0);
523 wake_up_all(&bo->event_queue);
526 * Since the final reference to this bo may not be dropped by
527 * the current task we have to put a memory barrier here to make
528 * sure the changes done in this function are always visible.
530 * This function only needs protection against the final kref_put.
535 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
537 struct ttm_bo_device *bdev = bo->bdev;
538 struct ttm_bo_global *glob = bo->glob;
539 struct ttm_bo_driver *driver = bdev->driver;
540 void *sync_obj = NULL;
544 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
545 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
547 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
548 (void) ttm_bo_wait(bo, false, false, true);
549 if (!ret && !bo->sync_obj) {
550 lockmgr(&bdev->fence_lock, LK_RELEASE);
551 put_count = ttm_bo_del_from_lru(bo);
553 lockmgr(&glob->lru_lock, LK_RELEASE);
554 ttm_bo_cleanup_memtype_use(bo);
556 ttm_bo_list_ref_sub(bo, put_count, true);
561 sync_obj = driver->sync_obj_ref(bo->sync_obj);
562 lockmgr(&bdev->fence_lock, LK_RELEASE);
565 atomic_set(&bo->reserved, 0);
566 wake_up_all(&bo->event_queue);
569 kref_get(&bo->list_kref);
570 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
571 lockmgr(&glob->lru_lock, LK_RELEASE);
574 driver->sync_obj_flush(sync_obj);
575 driver->sync_obj_unref(&sync_obj);
577 schedule_delayed_work(&bdev->wq,
578 ((hz / 100) < 1) ? 1 : hz / 100);
582 * function ttm_bo_cleanup_refs_and_unlock
583 * If bo idle, remove from delayed- and lru lists, and unref.
584 * If not idle, do nothing.
586 * Must be called with lru_lock and reservation held, this function
587 * will drop both before returning.
589 * @interruptible Any sleeps should occur interruptibly.
590 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
593 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
597 struct ttm_bo_device *bdev = bo->bdev;
598 struct ttm_bo_driver *driver = bdev->driver;
599 struct ttm_bo_global *glob = bo->glob;
603 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
604 ret = ttm_bo_wait(bo, false, false, true);
606 if (ret && !no_wait_gpu) {
610 * Take a reference to the fence and unreserve,
611 * at this point the buffer should be dead, so
612 * no new sync objects can be attached.
614 sync_obj = driver->sync_obj_ref(bo->sync_obj);
615 lockmgr(&bdev->fence_lock, LK_RELEASE);
617 atomic_set(&bo->reserved, 0);
618 wake_up_all(&bo->event_queue);
619 lockmgr(&glob->lru_lock, LK_RELEASE);
621 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
622 driver->sync_obj_unref(&sync_obj);
627 * remove sync_obj with ttm_bo_wait, the wait should be
628 * finished, and no new wait object should have been added.
630 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
631 ret = ttm_bo_wait(bo, false, false, true);
633 lockmgr(&bdev->fence_lock, LK_RELEASE);
637 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
638 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
641 * We raced, and lost, someone else holds the reservation now,
642 * and is probably busy in ttm_bo_cleanup_memtype_use.
644 * Even if it's not the case, because we finished waiting any
645 * delayed destruction would succeed, so just return success
649 lockmgr(&glob->lru_lock, LK_RELEASE);
653 lockmgr(&bdev->fence_lock, LK_RELEASE);
655 if (ret || unlikely(list_empty(&bo->ddestroy))) {
656 atomic_set(&bo->reserved, 0);
657 wake_up_all(&bo->event_queue);
658 lockmgr(&glob->lru_lock, LK_RELEASE);
662 put_count = ttm_bo_del_from_lru(bo);
663 list_del_init(&bo->ddestroy);
666 lockmgr(&glob->lru_lock, LK_RELEASE);
667 ttm_bo_cleanup_memtype_use(bo);
669 ttm_bo_list_ref_sub(bo, put_count, true);
675 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
676 * encountered buffers.
679 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
681 struct ttm_bo_global *glob = bdev->glob;
682 struct ttm_buffer_object *entry = NULL;
685 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
686 if (list_empty(&bdev->ddestroy))
689 entry = list_first_entry(&bdev->ddestroy,
690 struct ttm_buffer_object, ddestroy);
691 kref_get(&entry->list_kref);
694 struct ttm_buffer_object *nentry = NULL;
696 if (entry->ddestroy.next != &bdev->ddestroy) {
697 nentry = list_first_entry(&entry->ddestroy,
698 struct ttm_buffer_object, ddestroy);
699 kref_get(&nentry->list_kref);
702 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
703 if (remove_all && ret) {
704 lockmgr(&glob->lru_lock, LK_RELEASE);
705 ret = ttm_bo_reserve_nolru(entry, false, false,
707 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
711 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
714 lockmgr(&glob->lru_lock, LK_RELEASE);
716 kref_put(&entry->list_kref, ttm_bo_release_list);
722 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
723 if (list_empty(&entry->ddestroy))
728 lockmgr(&glob->lru_lock, LK_RELEASE);
731 kref_put(&entry->list_kref, ttm_bo_release_list);
735 static void ttm_bo_delayed_workqueue(struct work_struct *work)
737 struct ttm_bo_device *bdev =
738 container_of(work, struct ttm_bo_device, wq.work);
740 if (ttm_bo_delayed_delete(bdev, false)) {
741 schedule_delayed_work(&bdev->wq,
742 ((hz / 100) < 1) ? 1 : hz / 100);
747 * NOTE: bdev->vm_lock already held on call, this function release it.
749 static void ttm_bo_release(struct kref *kref)
751 struct ttm_buffer_object *bo =
752 container_of(kref, struct ttm_buffer_object, kref);
753 struct ttm_bo_device *bdev = bo->bdev;
754 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
757 if (atomic_read(&bo->kref.refcount) > 0) {
758 lockmgr(&bdev->vm_lock, LK_RELEASE);
761 if (likely(bo->vm_node != NULL)) {
762 RB_REMOVE(ttm_bo_device_buffer_objects,
763 &bdev->addr_space_rb, bo);
764 drm_mm_put_block(bo->vm_node);
769 * Should we clean up our implied list_kref? Because ttm_bo_release()
770 * can be called reentrantly due to races (this may not be true any
771 * more with the lock management changes in the deref), it is possible
772 * to get here twice, but there's only one list_kref ref to drop and
773 * in the other path 'bo' can be kfree()d by another thread the
774 * instant we release our lock.
776 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
777 if (release_active) {
778 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
779 lockmgr(&bdev->vm_lock, LK_RELEASE);
780 ttm_mem_io_lock(man, false);
781 ttm_mem_io_free_vm(bo);
782 ttm_mem_io_unlock(man);
783 ttm_bo_cleanup_refs_or_queue(bo);
784 kref_put(&bo->list_kref, ttm_bo_release_list);
786 lockmgr(&bdev->vm_lock, LK_RELEASE);
790 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
792 struct ttm_buffer_object *bo = *p_bo;
793 struct ttm_bo_device *bdev = bo->bdev;
796 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
797 if (kref_put(&bo->kref, ttm_bo_release) == 0)
798 lockmgr(&bdev->vm_lock, LK_RELEASE);
800 EXPORT_SYMBOL(ttm_bo_unref);
802 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
804 return cancel_delayed_work_sync(&bdev->wq);
806 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
808 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
811 schedule_delayed_work(&bdev->wq,
812 ((hz / 100) < 1) ? 1 : hz / 100);
814 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
816 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
819 struct ttm_bo_device *bdev = bo->bdev;
820 struct ttm_mem_reg evict_mem;
821 struct ttm_placement placement;
824 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
825 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
826 lockmgr(&bdev->fence_lock, LK_RELEASE);
828 if (unlikely(ret != 0)) {
829 if (ret != -ERESTARTSYS) {
830 pr_err("Failed to expire sync object before buffer eviction\n");
835 BUG_ON(!ttm_bo_is_reserved(bo));
838 evict_mem.mm_node = NULL;
839 evict_mem.bus.io_reserved_vm = false;
840 evict_mem.bus.io_reserved_count = 0;
844 placement.num_placement = 0;
845 placement.num_busy_placement = 0;
846 bdev->driver->evict_flags(bo, &placement);
847 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
850 if (ret != -ERESTARTSYS) {
851 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
853 ttm_bo_mem_space_debug(bo, &placement);
858 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
861 if (ret != -ERESTARTSYS)
862 pr_err("Buffer eviction failed\n");
863 ttm_bo_mem_put(bo, &evict_mem);
871 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
876 struct ttm_bo_global *glob = bdev->glob;
877 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
878 struct ttm_buffer_object *bo;
879 int ret = -EBUSY, put_count;
881 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
882 list_for_each_entry(bo, &man->lru, lru) {
883 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
889 lockmgr(&glob->lru_lock, LK_RELEASE);
893 kref_get(&bo->list_kref);
895 if (!list_empty(&bo->ddestroy)) {
896 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
898 kref_put(&bo->list_kref, ttm_bo_release_list);
902 put_count = ttm_bo_del_from_lru(bo);
903 lockmgr(&glob->lru_lock, LK_RELEASE);
907 ttm_bo_list_ref_sub(bo, put_count, true);
909 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
910 ttm_bo_unreserve(bo);
912 kref_put(&bo->list_kref, ttm_bo_release_list);
916 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
918 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
921 (*man->func->put_node)(man, mem);
923 EXPORT_SYMBOL(ttm_bo_mem_put);
926 * Repeatedly evict memory from the LRU for @mem_type until we create enough
927 * space, or we've evicted everything and there isn't enough space.
929 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
931 struct ttm_placement *placement,
932 struct ttm_mem_reg *mem,
936 struct ttm_bo_device *bdev = bo->bdev;
937 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
941 ret = (*man->func->get_node)(man, bo, placement, mem);
942 if (unlikely(ret != 0))
946 ret = ttm_mem_evict_first(bdev, mem_type,
947 interruptible, no_wait_gpu);
948 if (unlikely(ret != 0))
951 if (mem->mm_node == NULL)
953 mem->mem_type = mem_type;
957 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
958 uint32_t cur_placement,
959 uint32_t proposed_placement)
961 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
962 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
965 * Keep current caching if possible.
968 if ((cur_placement & caching) != 0)
969 result |= (cur_placement & caching);
970 else if ((man->default_caching & caching) != 0)
971 result |= man->default_caching;
972 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
973 result |= TTM_PL_FLAG_CACHED;
974 else if ((TTM_PL_FLAG_WC & caching) != 0)
975 result |= TTM_PL_FLAG_WC;
976 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
977 result |= TTM_PL_FLAG_UNCACHED;
982 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
984 uint32_t proposed_placement,
985 uint32_t *masked_placement)
987 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
989 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
992 if ((proposed_placement & man->available_caching) == 0)
995 cur_flags |= (proposed_placement & man->available_caching);
997 *masked_placement = cur_flags;
1002 * Creates space for memory region @mem according to its type.
1004 * This function first searches for free space in compatible memory types in
1005 * the priority order defined by the driver. If free space isn't found, then
1006 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1009 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1010 struct ttm_placement *placement,
1011 struct ttm_mem_reg *mem,
1015 struct ttm_bo_device *bdev = bo->bdev;
1016 struct ttm_mem_type_manager *man;
1017 uint32_t mem_type = TTM_PL_SYSTEM;
1018 uint32_t cur_flags = 0;
1019 bool type_found = false;
1020 bool type_ok = false;
1021 bool has_erestartsys = false;
1024 mem->mm_node = NULL;
1025 for (i = 0; i < placement->num_placement; ++i) {
1026 ret = ttm_mem_type_from_flags(placement->placement[i],
1030 man = &bdev->man[mem_type];
1032 type_ok = ttm_bo_mt_compatible(man,
1034 placement->placement[i],
1040 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1043 * Use the access and other non-mapping-related flag bits from
1044 * the memory placement flags to the current flags
1046 ttm_flag_masked(&cur_flags, placement->placement[i],
1047 ~TTM_PL_MASK_MEMTYPE);
1049 if (mem_type == TTM_PL_SYSTEM)
1052 if (man->has_type && man->use_type) {
1054 ret = (*man->func->get_node)(man, bo, placement, mem);
1062 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1063 mem->mem_type = mem_type;
1064 mem->placement = cur_flags;
1071 for (i = 0; i < placement->num_busy_placement; ++i) {
1072 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1076 man = &bdev->man[mem_type];
1079 if (!ttm_bo_mt_compatible(man,
1081 placement->busy_placement[i],
1085 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1088 * Use the access and other non-mapping-related flag bits from
1089 * the memory placement flags to the current flags
1091 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1092 ~TTM_PL_MASK_MEMTYPE);
1095 if (mem_type == TTM_PL_SYSTEM) {
1096 mem->mem_type = mem_type;
1097 mem->placement = cur_flags;
1098 mem->mm_node = NULL;
1102 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1103 interruptible, no_wait_gpu);
1104 if (ret == 0 && mem->mm_node) {
1105 mem->placement = cur_flags;
1108 if (ret == -ERESTARTSYS)
1109 has_erestartsys = true;
1111 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1114 EXPORT_SYMBOL(ttm_bo_mem_space);
1117 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1118 struct ttm_placement *placement,
1123 struct ttm_mem_reg mem;
1124 struct ttm_bo_device *bdev = bo->bdev;
1126 BUG_ON(!ttm_bo_is_reserved(bo));
1129 * FIXME: It's possible to pipeline buffer moves.
1130 * Have the driver move function wait for idle when necessary,
1131 * instead of doing it here.
1133 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1134 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1135 lockmgr(&bdev->fence_lock, LK_RELEASE);
1138 mem.num_pages = bo->num_pages;
1139 mem.size = mem.num_pages << PAGE_SHIFT;
1140 mem.page_alignment = bo->mem.page_alignment;
1141 mem.bus.io_reserved_vm = false;
1142 mem.bus.io_reserved_count = 0;
1144 * Determine where to move the buffer.
1146 ret = ttm_bo_mem_space(bo, placement, &mem,
1147 interruptible, no_wait_gpu);
1150 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1151 interruptible, no_wait_gpu);
1153 if (ret && mem.mm_node)
1154 ttm_bo_mem_put(bo, &mem);
1158 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1159 struct ttm_mem_reg *mem)
1163 if (mem->mm_node && placement->lpfn != 0 &&
1164 (mem->start < placement->fpfn ||
1165 mem->start + mem->num_pages > placement->lpfn))
1168 for (i = 0; i < placement->num_placement; i++) {
1169 if ((placement->placement[i] & mem->placement &
1170 TTM_PL_MASK_CACHING) &&
1171 (placement->placement[i] & mem->placement &
1178 int ttm_bo_validate(struct ttm_buffer_object *bo,
1179 struct ttm_placement *placement,
1185 BUG_ON(!ttm_bo_is_reserved(bo));
1186 /* Check that range is valid */
1187 if (placement->lpfn || placement->fpfn)
1188 if (placement->fpfn > placement->lpfn ||
1189 (placement->lpfn - placement->fpfn) < bo->num_pages)
1192 * Check whether we need to move buffer.
1194 ret = ttm_bo_mem_compat(placement, &bo->mem);
1196 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1202 * Use the access and other non-mapping-related flag bits from
1203 * the compatible memory placement flags to the active flags
1205 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1206 ~TTM_PL_MASK_MEMTYPE);
1209 * We might need to add a TTM.
1211 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1212 ret = ttm_bo_add_ttm(bo, true);
1218 EXPORT_SYMBOL(ttm_bo_validate);
1220 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1221 struct ttm_placement *placement)
1223 BUG_ON((placement->fpfn || placement->lpfn) &&
1224 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1229 int ttm_bo_init(struct ttm_bo_device *bdev,
1230 struct ttm_buffer_object *bo,
1232 enum ttm_bo_type type,
1233 struct ttm_placement *placement,
1234 uint32_t page_alignment,
1236 struct vm_object *persistent_swap_storage,
1238 struct sg_table *sg,
1239 void (*destroy) (struct ttm_buffer_object *))
1242 unsigned long num_pages;
1243 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1245 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1247 kprintf("[TTM] Out of kernel memory\n");
1255 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1256 if (num_pages == 0) {
1257 kprintf("[TTM] Illegal buffer object size\n");
1262 ttm_mem_global_free(mem_glob, acc_size);
1265 bo->destroy = destroy;
1267 kref_init(&bo->kref);
1268 kref_init(&bo->list_kref);
1269 atomic_set(&bo->cpu_writers, 0);
1270 atomic_set(&bo->reserved, 1);
1271 init_waitqueue_head(&bo->event_queue);
1272 INIT_LIST_HEAD(&bo->lru);
1273 INIT_LIST_HEAD(&bo->ddestroy);
1274 INIT_LIST_HEAD(&bo->swap);
1275 INIT_LIST_HEAD(&bo->io_reserve_lru);
1276 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1278 bo->glob = bdev->glob;
1280 bo->num_pages = num_pages;
1281 bo->mem.size = num_pages << PAGE_SHIFT;
1282 bo->mem.mem_type = TTM_PL_SYSTEM;
1283 bo->mem.num_pages = bo->num_pages;
1284 bo->mem.mm_node = NULL;
1285 bo->mem.page_alignment = page_alignment;
1286 bo->mem.bus.io_reserved_vm = false;
1287 bo->mem.bus.io_reserved_count = 0;
1289 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1290 bo->seq_valid = false;
1291 bo->persistent_swap_storage = persistent_swap_storage;
1292 bo->acc_size = acc_size;
1294 atomic_inc(&bo->glob->bo_count);
1297 * Mirror ref from kref_init() for list_kref.
1299 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1301 ret = ttm_bo_check_placement(bo, placement);
1302 if (unlikely(ret != 0))
1306 * For ttm_bo_type_device buffers, allocate
1307 * address space from the device.
1309 if (bo->type == ttm_bo_type_device ||
1310 bo->type == ttm_bo_type_sg) {
1311 ret = ttm_bo_setup_vm(bo);
1316 ret = ttm_bo_validate(bo, placement, interruptible, false);
1320 ttm_bo_unreserve(bo);
1324 ttm_bo_unreserve(bo);
1329 EXPORT_SYMBOL(ttm_bo_init);
1331 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1332 unsigned long bo_size,
1333 unsigned struct_size)
1335 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1338 size += ttm_round_pot(struct_size);
1339 size += PAGE_ALIGN(npages * sizeof(void *));
1340 size += ttm_round_pot(sizeof(struct ttm_tt));
1343 EXPORT_SYMBOL(ttm_bo_acc_size);
1345 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1346 unsigned long bo_size,
1347 unsigned struct_size)
1349 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1352 size += ttm_round_pot(struct_size);
1353 size += PAGE_ALIGN(npages * sizeof(void *));
1354 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1355 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1358 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1360 int ttm_bo_create(struct ttm_bo_device *bdev,
1362 enum ttm_bo_type type,
1363 struct ttm_placement *placement,
1364 uint32_t page_alignment,
1366 struct vm_object *persistent_swap_storage,
1367 struct ttm_buffer_object **p_bo)
1369 struct ttm_buffer_object *bo;
1374 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1375 if (unlikely(bo == NULL))
1378 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1379 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1380 interruptible, persistent_swap_storage, acc_size,
1382 if (likely(ret == 0))
1387 EXPORT_SYMBOL(ttm_bo_create);
1389 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1390 unsigned mem_type, bool allow_errors)
1392 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1393 struct ttm_bo_global *glob = bdev->glob;
1397 * Can't use standard list traversal since we're unlocking.
1400 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1401 while (!list_empty(&man->lru)) {
1402 lockmgr(&glob->lru_lock, LK_RELEASE);
1403 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1408 kprintf("[TTM] Cleanup eviction failed\n");
1411 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1413 lockmgr(&glob->lru_lock, LK_RELEASE);
1417 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1419 struct ttm_mem_type_manager *man;
1422 if (mem_type >= TTM_NUM_MEM_TYPES) {
1423 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1426 man = &bdev->man[mem_type];
1428 if (!man->has_type) {
1429 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1434 man->use_type = false;
1435 man->has_type = false;
1439 ttm_bo_force_list_clean(bdev, mem_type, false);
1441 ret = (*man->func->takedown)(man);
1446 EXPORT_SYMBOL(ttm_bo_clean_mm);
1448 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1450 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1452 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1453 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1457 if (!man->has_type) {
1458 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1462 return ttm_bo_force_list_clean(bdev, mem_type, true);
1464 EXPORT_SYMBOL(ttm_bo_evict_mm);
1466 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1467 unsigned long p_size)
1470 struct ttm_mem_type_manager *man;
1472 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1473 man = &bdev->man[type];
1474 BUG_ON(man->has_type);
1475 man->io_reserve_fastpath = true;
1476 man->use_io_reserve_lru = false;
1477 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1478 INIT_LIST_HEAD(&man->io_reserve_lru);
1480 ret = bdev->driver->init_mem_type(bdev, type, man);
1486 if (type != TTM_PL_SYSTEM) {
1487 ret = (*man->func->init)(man, p_size);
1491 man->has_type = true;
1492 man->use_type = true;
1495 INIT_LIST_HEAD(&man->lru);
1499 EXPORT_SYMBOL(ttm_bo_init_mm);
1501 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1503 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1504 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1505 glob->dummy_read_page = NULL;
1507 vm_page_free(glob->dummy_read_page);
1511 void ttm_bo_global_release(struct drm_global_reference *ref)
1513 struct ttm_bo_global *glob = ref->object;
1515 if (refcount_release(&glob->kobj_ref))
1516 ttm_bo_global_kobj_release(glob);
1518 EXPORT_SYMBOL(ttm_bo_global_release);
1520 int ttm_bo_global_init(struct drm_global_reference *ref)
1522 struct ttm_bo_global_ref *bo_ref =
1523 container_of(ref, struct ttm_bo_global_ref, ref);
1524 struct ttm_bo_global *glob = ref->object;
1527 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1528 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1529 glob->mem_glob = bo_ref->mem_glob;
1530 glob->dummy_read_page = vm_page_alloc_contig(
1531 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1533 if (unlikely(glob->dummy_read_page == NULL)) {
1538 INIT_LIST_HEAD(&glob->swap_lru);
1539 INIT_LIST_HEAD(&glob->device_list);
1541 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1542 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1543 if (unlikely(ret != 0)) {
1544 kprintf("[TTM] Could not register buffer object swapout\n");
1548 atomic_set(&glob->bo_count, 0);
1550 refcount_init(&glob->kobj_ref, 1);
1554 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1555 glob->dummy_read_page = NULL;
1557 vm_page_free(glob->dummy_read_page);
1563 EXPORT_SYMBOL(ttm_bo_global_init);
1566 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1569 unsigned i = TTM_NUM_MEM_TYPES;
1570 struct ttm_mem_type_manager *man;
1571 struct ttm_bo_global *glob = bdev->glob;
1574 man = &bdev->man[i];
1575 if (man->has_type) {
1576 man->use_type = false;
1577 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1579 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1582 man->has_type = false;
1586 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1587 list_del(&bdev->device_list);
1588 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1590 cancel_delayed_work_sync(&bdev->wq);
1592 while (ttm_bo_delayed_delete(bdev, true))
1595 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1596 if (list_empty(&bdev->ddestroy))
1597 TTM_DEBUG("Delayed destroy list was clean\n");
1599 if (list_empty(&bdev->man[0].lru))
1600 TTM_DEBUG("Swap list was clean\n");
1601 lockmgr(&glob->lru_lock, LK_RELEASE);
1603 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1604 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1605 drm_mm_takedown(&bdev->addr_space_mm);
1606 lockmgr(&bdev->vm_lock, LK_RELEASE);
1610 EXPORT_SYMBOL(ttm_bo_device_release);
1612 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1613 struct ttm_bo_global *glob,
1614 struct ttm_bo_driver *driver,
1615 uint64_t file_page_offset,
1620 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1621 bdev->driver = driver;
1623 memset(bdev->man, 0, sizeof(bdev->man));
1626 * Initialize the system memory buffer type.
1627 * Other types need to be driver / IOCTL initialized.
1629 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1630 if (unlikely(ret != 0))
1633 RB_INIT(&bdev->addr_space_rb);
1634 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1635 if (unlikely(ret != 0))
1636 goto out_no_addr_mm;
1638 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1639 INIT_LIST_HEAD(&bdev->ddestroy);
1640 bdev->dev_mapping = NULL;
1642 bdev->need_dma32 = need_dma32;
1644 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1645 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1646 list_add_tail(&bdev->device_list, &glob->device_list);
1647 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1651 ttm_bo_clean_mm(bdev, 0);
1655 EXPORT_SYMBOL(ttm_bo_device_init);
1658 * buffer object vm functions.
1661 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1663 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1665 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1666 if (mem->mem_type == TTM_PL_SYSTEM)
1669 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1672 if (mem->placement & TTM_PL_FLAG_CACHED)
1678 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1681 ttm_bo_release_mmap(bo);
1682 ttm_mem_io_free_vm(bo);
1685 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1687 struct ttm_bo_device *bdev = bo->bdev;
1688 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1690 ttm_mem_io_lock(man, false);
1691 ttm_bo_unmap_virtual_locked(bo);
1692 ttm_mem_io_unlock(man);
1696 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1698 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1700 struct ttm_bo_device *bdev = bo->bdev;
1702 /* The caller acquired bdev->vm_lock. */
1703 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1709 * @bo: the buffer to allocate address space for
1711 * Allocate address space in the drm device so that applications
1712 * can mmap the buffer and access the contents. This only
1713 * applies to ttm_bo_type_device objects as others are not
1714 * placed in the drm device address space.
1717 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1719 struct ttm_bo_device *bdev = bo->bdev;
1723 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1724 if (unlikely(ret != 0))
1727 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1728 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1729 bo->mem.num_pages, 0, 0);
1731 if (unlikely(bo->vm_node == NULL)) {
1736 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1737 bo->mem.num_pages, 0);
1739 if (unlikely(bo->vm_node == NULL)) {
1740 lockmgr(&bdev->vm_lock, LK_RELEASE);
1744 ttm_bo_vm_insert_rb(bo);
1745 lockmgr(&bdev->vm_lock, LK_RELEASE);
1746 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1750 lockmgr(&bdev->vm_lock, LK_RELEASE);
1754 int ttm_bo_wait(struct ttm_buffer_object *bo,
1755 bool lazy, bool interruptible, bool no_wait)
1757 struct ttm_bo_driver *driver = bo->bdev->driver;
1758 struct ttm_bo_device *bdev = bo->bdev;
1762 if (likely(bo->sync_obj == NULL))
1765 while (bo->sync_obj) {
1767 if (driver->sync_obj_signaled(bo->sync_obj)) {
1768 void *tmp_obj = bo->sync_obj;
1769 bo->sync_obj = NULL;
1770 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1771 lockmgr(&bdev->fence_lock, LK_RELEASE);
1772 driver->sync_obj_unref(&tmp_obj);
1773 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1780 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1781 lockmgr(&bdev->fence_lock, LK_RELEASE);
1782 ret = driver->sync_obj_wait(sync_obj,
1783 lazy, interruptible);
1784 if (unlikely(ret != 0)) {
1785 driver->sync_obj_unref(&sync_obj);
1786 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1789 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1790 if (likely(bo->sync_obj == sync_obj)) {
1791 void *tmp_obj = bo->sync_obj;
1792 bo->sync_obj = NULL;
1793 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1795 lockmgr(&bdev->fence_lock, LK_RELEASE);
1796 driver->sync_obj_unref(&sync_obj);
1797 driver->sync_obj_unref(&tmp_obj);
1798 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1800 lockmgr(&bdev->fence_lock, LK_RELEASE);
1801 driver->sync_obj_unref(&sync_obj);
1802 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1807 EXPORT_SYMBOL(ttm_bo_wait);
1809 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1811 struct ttm_bo_device *bdev = bo->bdev;
1815 * Using ttm_bo_reserve makes sure the lru lists are updated.
1818 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1819 if (unlikely(ret != 0))
1821 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1822 ret = ttm_bo_wait(bo, false, true, no_wait);
1823 lockmgr(&bdev->fence_lock, LK_RELEASE);
1824 if (likely(ret == 0))
1825 atomic_inc(&bo->cpu_writers);
1826 ttm_bo_unreserve(bo);
1829 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1831 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1833 atomic_dec(&bo->cpu_writers);
1835 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1838 * A buffer object shrink method that tries to swap out the first
1839 * buffer object on the bo_global::swap_lru list.
1842 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1844 struct ttm_bo_global *glob =
1845 container_of(shrink, struct ttm_bo_global, shrink);
1846 struct ttm_buffer_object *bo;
1849 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1851 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1852 list_for_each_entry(bo, &glob->swap_lru, swap) {
1853 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1859 lockmgr(&glob->lru_lock, LK_RELEASE);
1863 kref_get(&bo->list_kref);
1865 if (!list_empty(&bo->ddestroy)) {
1866 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1867 kref_put(&bo->list_kref, ttm_bo_release_list);
1871 put_count = ttm_bo_del_from_lru(bo);
1872 lockmgr(&glob->lru_lock, LK_RELEASE);
1874 ttm_bo_list_ref_sub(bo, put_count, true);
1877 * Wait for GPU, then move to system cached.
1880 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1881 ret = ttm_bo_wait(bo, false, false, false);
1882 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1884 if (unlikely(ret != 0))
1887 if ((bo->mem.placement & swap_placement) != swap_placement) {
1888 struct ttm_mem_reg evict_mem;
1890 evict_mem = bo->mem;
1891 evict_mem.mm_node = NULL;
1892 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1893 evict_mem.mem_type = TTM_PL_SYSTEM;
1895 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1897 if (unlikely(ret != 0))
1901 ttm_bo_unmap_virtual(bo);
1904 * Swap out. Buffer will be swapped in again as soon as
1905 * anyone tries to access a ttm page.
1908 if (bo->bdev->driver->swap_notify)
1909 bo->bdev->driver->swap_notify(bo);
1911 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1916 * Unreserve without putting on LRU to avoid swapping out an
1917 * already swapped buffer.
1920 atomic_set(&bo->reserved, 0);
1921 wake_up_all(&bo->event_queue);
1922 kref_put(&bo->list_kref, ttm_bo_release_list);
1926 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1928 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1931 EXPORT_SYMBOL(ttm_bo_swapout_all);