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/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
44 #define TTM_ASSERT_LOCKED(param) do { } while (0)
45 #define TTM_DEBUG(fmt, arg...) do { } while (0)
46 #define TTM_BO_HASH_ORDER 13
48 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
49 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
50 static void ttm_bo_global_kobj_release(struct kobject *kobj);
52 static struct attribute ttm_bo_count = {
57 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
62 for (i = 0; i <= TTM_PL_PRIV5; i++)
63 if (place->flags & (1 << i)) {
70 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
72 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
74 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
77 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
78 pr_err(" size: %ju\n", (uintmax_t)man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
81 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
85 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
90 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
93 for (i = 0; i < placement->num_placement; i++) {
94 ret = ttm_mem_type_from_place(&placement->placement[i],
98 pr_err(" placement[%d]=0x%08X (%d)\n",
99 i, placement->placement[i].flags, mem_type);
100 ttm_mem_type_debug(bo->bdev, mem_type);
104 static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
111 return ksnprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
115 static struct attribute *ttm_bo_global_attrs[] = {
120 static const struct sysfs_ops ttm_bo_global_ops = {
121 .show = &ttm_bo_global_show
124 static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
130 static inline uint32_t ttm_bo_type_flags(unsigned type)
135 static void ttm_bo_release_list(struct kref *list_kref)
137 struct ttm_buffer_object *bo =
138 container_of(list_kref, struct ttm_buffer_object, list_kref);
139 struct ttm_bo_device *bdev = bo->bdev;
140 size_t acc_size = bo->acc_size;
142 BUG_ON(atomic_read(&bo->list_kref.refcount));
143 BUG_ON(atomic_read(&bo->kref.refcount));
144 BUG_ON(atomic_read(&bo->cpu_writers));
145 BUG_ON(bo->sync_obj != NULL);
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
151 ttm_tt_destroy(bo->ttm);
152 atomic_dec(&bo->glob->bo_count);
158 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
161 static int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,
165 return wait_event_interruptible(bo->event_queue,
166 !ttm_bo_is_reserved(bo));
168 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
173 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
175 struct ttm_bo_device *bdev = bo->bdev;
176 struct ttm_mem_type_manager *man;
178 BUG_ON(!ttm_bo_is_reserved(bo));
180 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
182 BUG_ON(!list_empty(&bo->lru));
184 man = &bdev->man[bo->mem.mem_type];
185 list_add_tail(&bo->lru, &man->lru);
186 kref_get(&bo->list_kref);
188 if (bo->ttm != NULL) {
189 list_add_tail(&bo->swap, &bo->glob->swap_lru);
190 kref_get(&bo->list_kref);
195 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
199 if (!list_empty(&bo->swap)) {
200 list_del_init(&bo->swap);
203 if (!list_empty(&bo->lru)) {
204 list_del_init(&bo->lru);
209 * TODO: Add a driver hook to delete from
210 * driver-specific LRU's here.
216 int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
218 bool no_wait, bool use_ticket,
219 struct ww_acquire_ctx *ticket)
223 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
225 * Deadlock avoidance for multi-bo reserving.
227 if (use_ticket && bo->seq_valid) {
229 * We've already reserved this one.
231 if (unlikely(ticket->stamp == bo->val_seq))
234 * Already reserved by a thread that will not back
235 * off for us. We need to back off.
237 if (unlikely(ticket->stamp - bo->val_seq <= LONG_MAX))
244 ret = ttm_bo_wait_unreserved(bo, interruptible);
251 bool wake_up = false;
254 * Wake up waiters that may need to recheck for deadlock,
255 * if we decreased the sequence number.
257 if (unlikely((bo->val_seq - ticket->stamp <= LONG_MAX)
262 * In the worst case with memory ordering these values can be
263 * seen in the wrong order. However since we call wake_up_all
264 * in that case, this will hopefully not pose a problem,
265 * and the worst case would only cause someone to accidentally
266 * hit -EAGAIN in ttm_bo_reserve when they see old value of
267 * val_seq. However this would only happen if seq_valid was
268 * written before val_seq was, and just means some slightly
269 * increased cpu usage
271 bo->val_seq = ticket->stamp;
272 bo->seq_valid = true;
274 wake_up_all(&bo->event_queue);
276 bo->seq_valid = false;
281 EXPORT_SYMBOL(ttm_bo_reserve);
283 static void ttm_bo_ref_bug(struct kref *list_kref)
288 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
291 kref_sub(&bo->list_kref, count,
292 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
295 int ttm_bo_reserve(struct ttm_buffer_object *bo,
297 bool no_wait, bool use_ticket,
298 struct ww_acquire_ctx *ticket)
300 struct ttm_bo_global *glob = bo->glob;
304 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_ticket,
306 if (likely(ret == 0)) {
307 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
308 put_count = ttm_bo_del_from_lru(bo);
309 lockmgr(&glob->lru_lock, LK_RELEASE);
310 ttm_bo_list_ref_sub(bo, put_count, true);
316 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
318 struct ww_acquire_ctx *ticket)
320 bool wake_up = false;
323 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
324 WARN_ON(bo->seq_valid && ticket->stamp == bo->val_seq);
326 ret = ttm_bo_wait_unreserved(bo, interruptible);
332 if (bo->val_seq - ticket->stamp < LONG_MAX || !bo->seq_valid)
336 * Wake up waiters that may need to recheck for deadlock,
337 * if we decreased the sequence number.
339 bo->val_seq = ticket->stamp;
340 bo->seq_valid = true;
342 wake_up_all(&bo->event_queue);
347 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
348 bool interruptible, struct ww_acquire_ctx *ticket)
350 struct ttm_bo_global *glob = bo->glob;
353 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, ticket);
355 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
356 put_count = ttm_bo_del_from_lru(bo);
357 lockmgr(&glob->lru_lock, LK_RELEASE);
358 ttm_bo_list_ref_sub(bo, put_count, true);
362 EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
364 void ttm_bo_unreserve_ticket_locked(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
366 ttm_bo_add_to_lru(bo);
367 atomic_set(&bo->reserved, 0);
368 wake_up_all(&bo->event_queue);
371 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
373 struct ttm_bo_global *glob = bo->glob;
375 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
376 ttm_bo_unreserve_ticket_locked(bo, NULL);
377 lockmgr(&glob->lru_lock, LK_RELEASE);
379 EXPORT_SYMBOL(ttm_bo_unreserve);
381 void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
383 struct ttm_bo_global *glob = bo->glob;
385 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
386 ttm_bo_unreserve_ticket_locked(bo, ticket);
387 lockmgr(&glob->lru_lock, LK_RELEASE);
389 EXPORT_SYMBOL(ttm_bo_unreserve_ticket);
392 * Call bo->mutex locked.
394 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
396 struct ttm_bo_device *bdev = bo->bdev;
397 struct ttm_bo_global *glob = bo->glob;
399 uint32_t page_flags = 0;
401 TTM_ASSERT_LOCKED(&bo->mutex);
404 if (bdev->need_dma32)
405 page_flags |= TTM_PAGE_FLAG_DMA32;
408 case ttm_bo_type_device:
410 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
411 case ttm_bo_type_kernel:
412 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
413 page_flags, glob->dummy_read_page);
414 if (unlikely(bo->ttm == NULL))
418 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
419 page_flags | TTM_PAGE_FLAG_SG,
420 glob->dummy_read_page);
421 if (unlikely(bo->ttm == NULL)) {
425 bo->ttm->sg = bo->sg;
428 pr_err("Illegal buffer object type\n");
436 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
437 struct ttm_mem_reg *mem,
438 bool evict, bool interruptible,
441 struct ttm_bo_device *bdev = bo->bdev;
442 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
443 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
444 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
445 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
448 if (old_is_pci || new_is_pci ||
449 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
450 ret = ttm_mem_io_lock(old_man, true);
451 if (unlikely(ret != 0))
453 ttm_bo_unmap_virtual_locked(bo);
454 ttm_mem_io_unlock(old_man);
458 * Create and bind a ttm if required.
461 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
462 if (bo->ttm == NULL) {
463 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
464 ret = ttm_bo_add_ttm(bo, zero);
469 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
473 if (mem->mem_type != TTM_PL_SYSTEM) {
474 ret = ttm_tt_bind(bo->ttm, mem);
479 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
480 if (bdev->driver->move_notify)
481 bdev->driver->move_notify(bo, mem);
488 if (bdev->driver->move_notify)
489 bdev->driver->move_notify(bo, mem);
491 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
492 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
493 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
494 else if (bdev->driver->move)
495 ret = bdev->driver->move(bo, evict, interruptible,
498 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
501 if (bdev->driver->move_notify) {
502 struct ttm_mem_reg tmp_mem = *mem;
505 bdev->driver->move_notify(bo, mem);
515 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
517 pr_err("Can not flush read caches\n");
521 if (bo->mem.mm_node) {
522 bo->offset = (bo->mem.start << PAGE_SHIFT) +
523 bdev->man[bo->mem.mem_type].gpu_offset;
524 bo->cur_placement = bo->mem.placement;
531 new_man = &bdev->man[bo->mem.mem_type];
532 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
533 ttm_tt_unbind(bo->ttm);
534 ttm_tt_destroy(bo->ttm);
543 * Will release GPU memory type usage on destruction.
544 * This is the place to put in driver specific hooks to release
545 * driver private resources.
546 * Will release the bo::reserved lock.
549 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
551 if (bo->bdev->driver->move_notify)
552 bo->bdev->driver->move_notify(bo, NULL);
555 ttm_tt_unbind(bo->ttm);
556 ttm_tt_destroy(bo->ttm);
559 ttm_bo_mem_put(bo, &bo->mem);
561 atomic_set(&bo->reserved, 0);
562 wake_up_all(&bo->event_queue);
565 * Since the final reference to this bo may not be dropped by
566 * the current task we have to put a memory barrier here to make
567 * sure the changes done in this function are always visible.
569 * This function only needs protection against the final kref_put.
574 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
576 struct ttm_bo_device *bdev = bo->bdev;
577 struct ttm_bo_global *glob = bo->glob;
578 struct ttm_bo_driver *driver = bdev->driver;
579 void *sync_obj = NULL;
583 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
584 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
586 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
587 (void) ttm_bo_wait(bo, false, false, true);
588 if (!ret && !bo->sync_obj) {
589 lockmgr(&bdev->fence_lock, LK_RELEASE);
590 put_count = ttm_bo_del_from_lru(bo);
592 lockmgr(&glob->lru_lock, LK_RELEASE);
593 ttm_bo_cleanup_memtype_use(bo);
595 ttm_bo_list_ref_sub(bo, put_count, true);
600 sync_obj = driver->sync_obj_ref(bo->sync_obj);
601 lockmgr(&bdev->fence_lock, LK_RELEASE);
604 atomic_set(&bo->reserved, 0);
605 wake_up_all(&bo->event_queue);
608 kref_get(&bo->list_kref);
609 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
610 lockmgr(&glob->lru_lock, LK_RELEASE);
613 driver->sync_obj_flush(sync_obj);
614 driver->sync_obj_unref(&sync_obj);
616 schedule_delayed_work(&bdev->wq,
617 ((hz / 100) < 1) ? 1 : hz / 100);
621 * function ttm_bo_cleanup_refs_and_unlock
622 * If bo idle, remove from delayed- and lru lists, and unref.
623 * If not idle, do nothing.
625 * Must be called with lru_lock and reservation held, this function
626 * will drop both before returning.
628 * @interruptible Any sleeps should occur interruptibly.
629 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
632 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
636 struct ttm_bo_device *bdev = bo->bdev;
637 struct ttm_bo_driver *driver = bdev->driver;
638 struct ttm_bo_global *glob = bo->glob;
642 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
643 ret = ttm_bo_wait(bo, false, false, true);
645 if (ret && !no_wait_gpu) {
649 * Take a reference to the fence and unreserve,
650 * at this point the buffer should be dead, so
651 * no new sync objects can be attached.
653 sync_obj = driver->sync_obj_ref(bo->sync_obj);
654 lockmgr(&bdev->fence_lock, LK_RELEASE);
656 atomic_set(&bo->reserved, 0);
657 wake_up_all(&bo->event_queue);
658 lockmgr(&glob->lru_lock, LK_RELEASE);
660 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
661 driver->sync_obj_unref(&sync_obj);
666 * remove sync_obj with ttm_bo_wait, the wait should be
667 * finished, and no new wait object should have been added.
669 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
670 ret = ttm_bo_wait(bo, false, false, true);
672 lockmgr(&bdev->fence_lock, LK_RELEASE);
676 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
677 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
680 * We raced, and lost, someone else holds the reservation now,
681 * and is probably busy in ttm_bo_cleanup_memtype_use.
683 * Even if it's not the case, because we finished waiting any
684 * delayed destruction would succeed, so just return success
688 lockmgr(&glob->lru_lock, LK_RELEASE);
692 lockmgr(&bdev->fence_lock, LK_RELEASE);
694 if (ret || unlikely(list_empty(&bo->ddestroy))) {
695 atomic_set(&bo->reserved, 0);
696 wake_up_all(&bo->event_queue);
697 lockmgr(&glob->lru_lock, LK_RELEASE);
701 put_count = ttm_bo_del_from_lru(bo);
702 list_del_init(&bo->ddestroy);
705 lockmgr(&glob->lru_lock, LK_RELEASE);
706 ttm_bo_cleanup_memtype_use(bo);
708 ttm_bo_list_ref_sub(bo, put_count, true);
714 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
715 * encountered buffers.
718 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
720 struct ttm_bo_global *glob = bdev->glob;
721 struct ttm_buffer_object *entry = NULL;
724 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
725 if (list_empty(&bdev->ddestroy))
728 entry = list_first_entry(&bdev->ddestroy,
729 struct ttm_buffer_object, ddestroy);
730 kref_get(&entry->list_kref);
733 struct ttm_buffer_object *nentry = NULL;
735 if (entry->ddestroy.next != &bdev->ddestroy) {
736 nentry = list_first_entry(&entry->ddestroy,
737 struct ttm_buffer_object, ddestroy);
738 kref_get(&nentry->list_kref);
741 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
742 if (remove_all && ret) {
743 lockmgr(&glob->lru_lock, LK_RELEASE);
744 ret = ttm_bo_reserve_nolru(entry, false, false,
746 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
750 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
753 lockmgr(&glob->lru_lock, LK_RELEASE);
755 kref_put(&entry->list_kref, ttm_bo_release_list);
761 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
762 if (list_empty(&entry->ddestroy))
767 lockmgr(&glob->lru_lock, LK_RELEASE);
770 kref_put(&entry->list_kref, ttm_bo_release_list);
774 static void ttm_bo_delayed_workqueue(struct work_struct *work)
776 struct ttm_bo_device *bdev =
777 container_of(work, struct ttm_bo_device, wq.work);
779 if (ttm_bo_delayed_delete(bdev, false)) {
780 schedule_delayed_work(&bdev->wq,
781 ((hz / 100) < 1) ? 1 : hz / 100);
786 * NOTE: bdev->vm_lock already held on call, this function release it.
788 static void ttm_bo_release(struct kref *kref)
790 struct ttm_buffer_object *bo =
791 container_of(kref, struct ttm_buffer_object, kref);
792 struct ttm_bo_device *bdev = bo->bdev;
793 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
796 if (atomic_read(&bo->kref.refcount) > 0) {
797 lockmgr(&bdev->vm_lock, LK_RELEASE);
800 if (likely(bo->vm_node != NULL)) {
801 RB_REMOVE(ttm_bo_device_buffer_objects,
802 &bdev->addr_space_rb, bo);
803 drm_mm_put_block(bo->vm_node);
808 * Should we clean up our implied list_kref? Because ttm_bo_release()
809 * can be called reentrantly due to races (this may not be true any
810 * more with the lock management changes in the deref), it is possible
811 * to get here twice, but there's only one list_kref ref to drop and
812 * in the other path 'bo' can be kfree()d by another thread the
813 * instant we release our lock.
815 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
816 if (release_active) {
817 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
818 lockmgr(&bdev->vm_lock, LK_RELEASE);
819 ttm_mem_io_lock(man, false);
820 ttm_mem_io_free_vm(bo);
821 ttm_mem_io_unlock(man);
822 ttm_bo_cleanup_refs_or_queue(bo);
823 kref_put(&bo->list_kref, ttm_bo_release_list);
825 lockmgr(&bdev->vm_lock, LK_RELEASE);
829 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
831 struct ttm_buffer_object *bo = *p_bo;
832 struct ttm_bo_device *bdev = bo->bdev;
835 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
836 if (kref_put(&bo->kref, ttm_bo_release) == 0)
837 lockmgr(&bdev->vm_lock, LK_RELEASE);
839 EXPORT_SYMBOL(ttm_bo_unref);
841 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
843 return cancel_delayed_work_sync(&bdev->wq);
845 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
847 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
850 schedule_delayed_work(&bdev->wq,
851 ((hz / 100) < 1) ? 1 : hz / 100);
853 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
855 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
858 struct ttm_bo_device *bdev = bo->bdev;
859 struct ttm_mem_reg evict_mem;
860 struct ttm_placement placement;
863 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
864 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
865 lockmgr(&bdev->fence_lock, LK_RELEASE);
867 if (unlikely(ret != 0)) {
868 if (ret != -ERESTARTSYS) {
869 pr_err("Failed to expire sync object before buffer eviction\n");
874 BUG_ON(!ttm_bo_is_reserved(bo));
877 evict_mem.mm_node = NULL;
878 evict_mem.bus.io_reserved_vm = false;
879 evict_mem.bus.io_reserved_count = 0;
881 placement.num_placement = 0;
882 placement.num_busy_placement = 0;
883 bdev->driver->evict_flags(bo, &placement);
884 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
887 if (ret != -ERESTARTSYS) {
888 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
890 ttm_bo_mem_space_debug(bo, &placement);
895 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
898 if (ret != -ERESTARTSYS)
899 pr_err("Buffer eviction failed\n");
900 ttm_bo_mem_put(bo, &evict_mem);
908 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
913 struct ttm_bo_global *glob = bdev->glob;
914 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
915 struct ttm_buffer_object *bo;
916 int ret = -EBUSY, put_count;
918 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
919 list_for_each_entry(bo, &man->lru, lru) {
920 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
926 lockmgr(&glob->lru_lock, LK_RELEASE);
930 kref_get(&bo->list_kref);
932 if (!list_empty(&bo->ddestroy)) {
933 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
935 kref_put(&bo->list_kref, ttm_bo_release_list);
939 put_count = ttm_bo_del_from_lru(bo);
940 lockmgr(&glob->lru_lock, LK_RELEASE);
944 ttm_bo_list_ref_sub(bo, put_count, true);
946 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
947 ttm_bo_unreserve(bo);
949 kref_put(&bo->list_kref, ttm_bo_release_list);
953 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
955 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
958 (*man->func->put_node)(man, mem);
960 EXPORT_SYMBOL(ttm_bo_mem_put);
963 * Repeatedly evict memory from the LRU for @mem_type until we create enough
964 * space, or we've evicted everything and there isn't enough space.
966 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
968 const struct ttm_place *place,
969 struct ttm_mem_reg *mem,
973 struct ttm_bo_device *bdev = bo->bdev;
974 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
978 ret = (*man->func->get_node)(man, bo, place, mem);
979 if (unlikely(ret != 0))
983 ret = ttm_mem_evict_first(bdev, mem_type,
984 interruptible, no_wait_gpu);
985 if (unlikely(ret != 0))
988 if (mem->mm_node == NULL)
990 mem->mem_type = mem_type;
994 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
995 uint32_t cur_placement,
996 uint32_t proposed_placement)
998 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
999 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
1002 * Keep current caching if possible.
1005 if ((cur_placement & caching) != 0)
1006 result |= (cur_placement & caching);
1007 else if ((man->default_caching & caching) != 0)
1008 result |= man->default_caching;
1009 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
1010 result |= TTM_PL_FLAG_CACHED;
1011 else if ((TTM_PL_FLAG_WC & caching) != 0)
1012 result |= TTM_PL_FLAG_WC;
1013 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
1014 result |= TTM_PL_FLAG_UNCACHED;
1019 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
1021 const struct ttm_place *place,
1022 uint32_t *masked_placement)
1024 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
1026 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
1029 if ((place->flags & man->available_caching) == 0)
1032 cur_flags |= (place->flags & man->available_caching);
1034 *masked_placement = cur_flags;
1039 * Creates space for memory region @mem according to its type.
1041 * This function first searches for free space in compatible memory types in
1042 * the priority order defined by the driver. If free space isn't found, then
1043 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1046 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1047 struct ttm_placement *placement,
1048 struct ttm_mem_reg *mem,
1052 struct ttm_bo_device *bdev = bo->bdev;
1053 struct ttm_mem_type_manager *man;
1054 uint32_t mem_type = TTM_PL_SYSTEM;
1055 uint32_t cur_flags = 0;
1056 bool type_found = false;
1057 bool type_ok = false;
1058 bool has_erestartsys = false;
1061 mem->mm_node = NULL;
1062 for (i = 0; i < placement->num_placement; ++i) {
1063 const struct ttm_place *place = &placement->placement[i];
1065 ret = ttm_mem_type_from_place(place, &mem_type);
1068 man = &bdev->man[mem_type];
1070 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
1076 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1079 * Use the access and other non-mapping-related flag bits from
1080 * the memory placement flags to the current flags
1082 ttm_flag_masked(&cur_flags, place->flags,
1083 ~TTM_PL_MASK_MEMTYPE);
1085 if (mem_type == TTM_PL_SYSTEM)
1088 if (man->has_type && man->use_type) {
1090 ret = (*man->func->get_node)(man, bo, place, mem);
1098 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1099 mem->mem_type = mem_type;
1100 mem->placement = cur_flags;
1107 for (i = 0; i < placement->num_busy_placement; ++i) {
1108 const struct ttm_place *place = &placement->busy_placement[i];
1110 ret = ttm_mem_type_from_place(place, &mem_type);
1113 man = &bdev->man[mem_type];
1116 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1119 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1122 * Use the access and other non-mapping-related flag bits from
1123 * the memory placement flags to the current flags
1125 ttm_flag_masked(&cur_flags, place->flags,
1126 ~TTM_PL_MASK_MEMTYPE);
1128 if (mem_type == TTM_PL_SYSTEM) {
1129 mem->mem_type = mem_type;
1130 mem->placement = cur_flags;
1131 mem->mm_node = NULL;
1135 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
1136 interruptible, no_wait_gpu);
1137 if (ret == 0 && mem->mm_node) {
1138 mem->placement = cur_flags;
1141 if (ret == -ERESTARTSYS)
1142 has_erestartsys = true;
1144 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1147 EXPORT_SYMBOL(ttm_bo_mem_space);
1149 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1150 struct ttm_placement *placement,
1155 struct ttm_mem_reg mem;
1156 struct ttm_bo_device *bdev = bo->bdev;
1158 BUG_ON(!ttm_bo_is_reserved(bo));
1161 * FIXME: It's possible to pipeline buffer moves.
1162 * Have the driver move function wait for idle when necessary,
1163 * instead of doing it here.
1165 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1166 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1167 lockmgr(&bdev->fence_lock, LK_RELEASE);
1170 mem.num_pages = bo->num_pages;
1171 mem.size = mem.num_pages << PAGE_SHIFT;
1172 mem.page_alignment = bo->mem.page_alignment;
1173 mem.bus.io_reserved_vm = false;
1174 mem.bus.io_reserved_count = 0;
1176 * Determine where to move the buffer.
1178 ret = ttm_bo_mem_space(bo, placement, &mem,
1179 interruptible, no_wait_gpu);
1182 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1183 interruptible, no_wait_gpu);
1185 if (ret && mem.mm_node)
1186 ttm_bo_mem_put(bo, &mem);
1190 static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1191 struct ttm_mem_reg *mem,
1192 uint32_t *new_flags)
1196 for (i = 0; i < placement->num_placement; i++) {
1197 const struct ttm_place *heap = &placement->placement[i];
1199 (mem->start < heap->fpfn ||
1200 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1203 *new_flags = heap->flags;
1204 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1205 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1209 for (i = 0; i < placement->num_busy_placement; i++) {
1210 const struct ttm_place *heap = &placement->busy_placement[i];
1212 (mem->start < heap->fpfn ||
1213 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1216 *new_flags = heap->flags;
1217 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1218 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1225 int ttm_bo_validate(struct ttm_buffer_object *bo,
1226 struct ttm_placement *placement,
1233 BUG_ON(!ttm_bo_is_reserved(bo));
1235 * Check whether we need to move buffer.
1237 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1238 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1244 * Use the access and other non-mapping-related flag bits from
1245 * the compatible memory placement flags to the active flags
1247 ttm_flag_masked(&bo->mem.placement, new_flags,
1248 ~TTM_PL_MASK_MEMTYPE);
1251 * We might need to add a TTM.
1253 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1254 ret = ttm_bo_add_ttm(bo, true);
1260 EXPORT_SYMBOL(ttm_bo_validate);
1262 int ttm_bo_init(struct ttm_bo_device *bdev,
1263 struct ttm_buffer_object *bo,
1265 enum ttm_bo_type type,
1266 struct ttm_placement *placement,
1267 uint32_t page_alignment,
1269 struct vm_object *persistent_swap_storage,
1271 struct sg_table *sg,
1272 void (*destroy) (struct ttm_buffer_object *))
1275 unsigned long num_pages;
1276 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1278 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1280 pr_err("Out of kernel memory\n");
1288 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1289 if (num_pages == 0) {
1290 pr_err("Illegal buffer object size\n");
1295 ttm_mem_global_free(mem_glob, acc_size);
1298 bo->destroy = destroy;
1300 kref_init(&bo->kref);
1301 kref_init(&bo->list_kref);
1302 atomic_set(&bo->cpu_writers, 0);
1303 atomic_set(&bo->reserved, 1);
1304 init_waitqueue_head(&bo->event_queue);
1305 INIT_LIST_HEAD(&bo->lru);
1306 INIT_LIST_HEAD(&bo->ddestroy);
1307 INIT_LIST_HEAD(&bo->swap);
1308 INIT_LIST_HEAD(&bo->io_reserve_lru);
1309 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1311 bo->glob = bdev->glob;
1313 bo->num_pages = num_pages;
1314 bo->mem.size = num_pages << PAGE_SHIFT;
1315 bo->mem.mem_type = TTM_PL_SYSTEM;
1316 bo->mem.num_pages = bo->num_pages;
1317 bo->mem.mm_node = NULL;
1318 bo->mem.page_alignment = page_alignment;
1319 bo->mem.bus.io_reserved_vm = false;
1320 bo->mem.bus.io_reserved_count = 0;
1322 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1323 bo->seq_valid = false;
1324 bo->persistent_swap_storage = persistent_swap_storage;
1325 bo->acc_size = acc_size;
1327 atomic_inc(&bo->glob->bo_count);
1330 * Mirror ref from kref_init() for list_kref.
1332 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1335 * For ttm_bo_type_device buffers, allocate
1336 * address space from the device.
1338 if (bo->type == ttm_bo_type_device ||
1339 bo->type == ttm_bo_type_sg) {
1340 ret = ttm_bo_setup_vm(bo);
1345 ret = ttm_bo_validate(bo, placement, interruptible, false);
1349 ttm_bo_unreserve(bo);
1353 ttm_bo_unreserve(bo);
1358 EXPORT_SYMBOL(ttm_bo_init);
1360 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1361 unsigned long bo_size,
1362 unsigned struct_size)
1364 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1367 size += ttm_round_pot(struct_size);
1368 size += PAGE_ALIGN(npages * sizeof(void *));
1369 size += ttm_round_pot(sizeof(struct ttm_tt));
1372 EXPORT_SYMBOL(ttm_bo_acc_size);
1374 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1375 unsigned long bo_size,
1376 unsigned struct_size)
1378 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1381 size += ttm_round_pot(struct_size);
1382 size += PAGE_ALIGN(npages * sizeof(void *));
1383 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1384 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1387 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1389 int ttm_bo_create(struct ttm_bo_device *bdev,
1391 enum ttm_bo_type type,
1392 struct ttm_placement *placement,
1393 uint32_t page_alignment,
1395 struct vm_object *persistent_swap_storage,
1396 struct ttm_buffer_object **p_bo)
1398 struct ttm_buffer_object *bo;
1403 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1404 if (unlikely(bo == NULL))
1407 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1408 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1409 interruptible, persistent_swap_storage, acc_size,
1411 if (likely(ret == 0))
1416 EXPORT_SYMBOL(ttm_bo_create);
1418 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1419 unsigned mem_type, bool allow_errors)
1421 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1422 struct ttm_bo_global *glob = bdev->glob;
1426 * Can't use standard list traversal since we're unlocking.
1429 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1430 while (!list_empty(&man->lru)) {
1431 lockmgr(&glob->lru_lock, LK_RELEASE);
1432 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1437 pr_err("Cleanup eviction failed\n");
1440 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1442 lockmgr(&glob->lru_lock, LK_RELEASE);
1446 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1448 struct ttm_mem_type_manager *man;
1451 if (mem_type >= TTM_NUM_MEM_TYPES) {
1452 pr_err("Illegal memory type %d\n", mem_type);
1455 man = &bdev->man[mem_type];
1457 if (!man->has_type) {
1458 pr_err("Trying to take down uninitialized memory manager type %u\n",
1463 man->use_type = false;
1464 man->has_type = false;
1468 ttm_bo_force_list_clean(bdev, mem_type, false);
1470 ret = (*man->func->takedown)(man);
1475 EXPORT_SYMBOL(ttm_bo_clean_mm);
1477 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1479 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1481 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1482 pr_err("Illegal memory manager memory type %u\n", mem_type);
1486 if (!man->has_type) {
1487 pr_err("Memory type %u has not been initialized\n", mem_type);
1491 return ttm_bo_force_list_clean(bdev, mem_type, true);
1493 EXPORT_SYMBOL(ttm_bo_evict_mm);
1495 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1496 unsigned long p_size)
1499 struct ttm_mem_type_manager *man;
1501 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1502 man = &bdev->man[type];
1503 BUG_ON(man->has_type);
1504 man->io_reserve_fastpath = true;
1505 man->use_io_reserve_lru = false;
1506 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1507 INIT_LIST_HEAD(&man->io_reserve_lru);
1509 ret = bdev->driver->init_mem_type(bdev, type, man);
1515 if (type != TTM_PL_SYSTEM) {
1516 ret = (*man->func->init)(man, p_size);
1520 man->has_type = true;
1521 man->use_type = true;
1524 INIT_LIST_HEAD(&man->lru);
1528 EXPORT_SYMBOL(ttm_bo_init_mm);
1530 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1532 struct ttm_bo_global *glob =
1533 container_of(kobj, struct ttm_bo_global, kobj);
1535 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1536 __free_page(glob->dummy_read_page);
1540 void ttm_bo_global_release(struct drm_global_reference *ref)
1542 struct ttm_bo_global *glob = ref->object;
1544 kobject_del(&glob->kobj);
1545 kobject_put(&glob->kobj);
1547 EXPORT_SYMBOL(ttm_bo_global_release);
1549 int ttm_bo_global_init(struct drm_global_reference *ref)
1551 struct ttm_bo_global_ref *bo_ref =
1552 container_of(ref, struct ttm_bo_global_ref, ref);
1553 struct ttm_bo_global *glob = ref->object;
1556 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1557 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1558 glob->mem_glob = bo_ref->mem_glob;
1559 glob->dummy_read_page = (struct page *)vm_page_alloc_contig(
1560 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1562 if (unlikely(glob->dummy_read_page == NULL)) {
1567 INIT_LIST_HEAD(&glob->swap_lru);
1568 INIT_LIST_HEAD(&glob->device_list);
1570 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1571 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1572 if (unlikely(ret != 0)) {
1573 pr_err("Could not register buffer object swapout\n");
1577 atomic_set(&glob->bo_count, 0);
1579 ret = kobject_init_and_add(
1580 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1581 if (unlikely(ret != 0))
1582 kobject_put(&glob->kobj);
1585 __free_page(glob->dummy_read_page);
1590 EXPORT_SYMBOL(ttm_bo_global_init);
1593 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1596 unsigned i = TTM_NUM_MEM_TYPES;
1597 struct ttm_mem_type_manager *man;
1598 struct ttm_bo_global *glob = bdev->glob;
1601 man = &bdev->man[i];
1602 if (man->has_type) {
1603 man->use_type = false;
1604 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1606 pr_err("DRM memory manager type %d is not clean\n",
1609 man->has_type = false;
1613 mutex_lock(&glob->device_list_mutex);
1614 list_del(&bdev->device_list);
1615 mutex_unlock(&glob->device_list_mutex);
1617 cancel_delayed_work_sync(&bdev->wq);
1619 while (ttm_bo_delayed_delete(bdev, true))
1622 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1623 if (list_empty(&bdev->ddestroy))
1624 TTM_DEBUG("Delayed destroy list was clean\n");
1626 if (list_empty(&bdev->man[0].lru))
1627 TTM_DEBUG("Swap list was clean\n");
1628 lockmgr(&glob->lru_lock, LK_RELEASE);
1630 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1631 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1632 drm_mm_takedown(&bdev->addr_space_mm);
1633 lockmgr(&bdev->vm_lock, LK_RELEASE);
1637 EXPORT_SYMBOL(ttm_bo_device_release);
1639 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1640 struct ttm_bo_global *glob,
1641 struct ttm_bo_driver *driver,
1642 uint64_t file_page_offset,
1647 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1648 bdev->driver = driver;
1650 memset(bdev->man, 0, sizeof(bdev->man));
1653 * Initialize the system memory buffer type.
1654 * Other types need to be driver / IOCTL initialized.
1656 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1657 if (unlikely(ret != 0))
1660 RB_INIT(&bdev->addr_space_rb);
1661 drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1663 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1664 INIT_LIST_HEAD(&bdev->ddestroy);
1665 bdev->dev_mapping = NULL;
1667 bdev->need_dma32 = need_dma32;
1669 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1670 mutex_lock(&glob->device_list_mutex);
1671 list_add_tail(&bdev->device_list, &glob->device_list);
1672 mutex_unlock(&glob->device_list_mutex);
1678 EXPORT_SYMBOL(ttm_bo_device_init);
1681 * buffer object vm functions.
1684 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1686 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1688 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1689 if (mem->mem_type == TTM_PL_SYSTEM)
1692 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1695 if (mem->placement & TTM_PL_FLAG_CACHED)
1701 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1704 ttm_bo_release_mmap(bo);
1705 ttm_mem_io_free_vm(bo);
1708 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1710 struct ttm_bo_device *bdev = bo->bdev;
1711 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1713 ttm_mem_io_lock(man, false);
1714 ttm_bo_unmap_virtual_locked(bo);
1715 ttm_mem_io_unlock(man);
1719 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1721 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1723 struct ttm_bo_device *bdev = bo->bdev;
1725 /* The caller acquired bdev->vm_lock. */
1726 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1732 * @bo: the buffer to allocate address space for
1734 * Allocate address space in the drm device so that applications
1735 * can mmap the buffer and access the contents. This only
1736 * applies to ttm_bo_type_device objects as others are not
1737 * placed in the drm device address space.
1740 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1742 struct ttm_bo_device *bdev = bo->bdev;
1746 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1747 if (unlikely(ret != 0))
1750 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1751 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1752 bo->mem.num_pages, 0, 0);
1754 if (unlikely(bo->vm_node == NULL)) {
1759 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1760 bo->mem.num_pages, 0);
1762 if (unlikely(bo->vm_node == NULL)) {
1763 lockmgr(&bdev->vm_lock, LK_RELEASE);
1767 ttm_bo_vm_insert_rb(bo);
1768 lockmgr(&bdev->vm_lock, LK_RELEASE);
1769 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1773 lockmgr(&bdev->vm_lock, LK_RELEASE);
1777 int ttm_bo_wait(struct ttm_buffer_object *bo,
1778 bool lazy, bool interruptible, bool no_wait)
1780 struct ttm_bo_driver *driver = bo->bdev->driver;
1781 struct ttm_bo_device *bdev = bo->bdev;
1785 if (likely(bo->sync_obj == NULL))
1788 while (bo->sync_obj) {
1790 if (driver->sync_obj_signaled(bo->sync_obj)) {
1791 void *tmp_obj = bo->sync_obj;
1792 bo->sync_obj = NULL;
1793 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1794 lockmgr(&bdev->fence_lock, LK_RELEASE);
1795 driver->sync_obj_unref(&tmp_obj);
1796 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1803 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1804 lockmgr(&bdev->fence_lock, LK_RELEASE);
1805 ret = driver->sync_obj_wait(sync_obj,
1806 lazy, interruptible);
1807 if (unlikely(ret != 0)) {
1808 driver->sync_obj_unref(&sync_obj);
1809 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1812 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1813 if (likely(bo->sync_obj == sync_obj)) {
1814 void *tmp_obj = bo->sync_obj;
1815 bo->sync_obj = NULL;
1816 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1818 lockmgr(&bdev->fence_lock, LK_RELEASE);
1819 driver->sync_obj_unref(&sync_obj);
1820 driver->sync_obj_unref(&tmp_obj);
1821 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1823 lockmgr(&bdev->fence_lock, LK_RELEASE);
1824 driver->sync_obj_unref(&sync_obj);
1825 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1830 EXPORT_SYMBOL(ttm_bo_wait);
1832 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1834 struct ttm_bo_device *bdev = bo->bdev;
1838 * Using ttm_bo_reserve makes sure the lru lists are updated.
1841 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1842 if (unlikely(ret != 0))
1844 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1845 ret = ttm_bo_wait(bo, false, true, no_wait);
1846 lockmgr(&bdev->fence_lock, LK_RELEASE);
1847 if (likely(ret == 0))
1848 atomic_inc(&bo->cpu_writers);
1849 ttm_bo_unreserve(bo);
1852 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1854 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1856 atomic_dec(&bo->cpu_writers);
1858 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1861 * A buffer object shrink method that tries to swap out the first
1862 * buffer object on the bo_global::swap_lru list.
1865 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1867 struct ttm_bo_global *glob =
1868 container_of(shrink, struct ttm_bo_global, shrink);
1869 struct ttm_buffer_object *bo;
1872 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1874 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1875 list_for_each_entry(bo, &glob->swap_lru, swap) {
1876 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1882 lockmgr(&glob->lru_lock, LK_RELEASE);
1886 kref_get(&bo->list_kref);
1888 if (!list_empty(&bo->ddestroy)) {
1889 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1890 kref_put(&bo->list_kref, ttm_bo_release_list);
1894 put_count = ttm_bo_del_from_lru(bo);
1895 lockmgr(&glob->lru_lock, LK_RELEASE);
1897 ttm_bo_list_ref_sub(bo, put_count, true);
1900 * Wait for GPU, then move to system cached.
1903 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1904 ret = ttm_bo_wait(bo, false, false, false);
1905 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1907 if (unlikely(ret != 0))
1910 if ((bo->mem.placement & swap_placement) != swap_placement) {
1911 struct ttm_mem_reg evict_mem;
1913 evict_mem = bo->mem;
1914 evict_mem.mm_node = NULL;
1915 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1916 evict_mem.mem_type = TTM_PL_SYSTEM;
1918 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1920 if (unlikely(ret != 0))
1924 ttm_bo_unmap_virtual(bo);
1927 * Swap out. Buffer will be swapped in again as soon as
1928 * anyone tries to access a ttm page.
1931 if (bo->bdev->driver->swap_notify)
1932 bo->bdev->driver->swap_notify(bo);
1934 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1939 * Unreserve without putting on LRU to avoid swapping out an
1940 * already swapped buffer.
1943 atomic_set(&bo->reserved, 0);
1944 wake_up_all(&bo->event_queue);
1945 kref_put(&bo->list_kref, ttm_bo_release_list);
1949 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1951 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1954 EXPORT_SYMBOL(ttm_bo_swapout_all);