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_ticket,
194 struct ww_acquire_ctx *ticket)
198 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
200 * Deadlock avoidance for multi-bo reserving.
202 if (use_ticket && bo->seq_valid) {
204 * We've already reserved this one.
206 if (unlikely(ticket->stamp == bo->val_seq))
209 * Already reserved by a thread that will not back
210 * off for us. We need to back off.
212 if (unlikely(ticket->stamp - bo->val_seq <= LONG_MAX))
219 ret = ttm_bo_wait_unreserved(bo, interruptible);
226 bool wake_up = false;
229 * Wake up waiters that may need to recheck for deadlock,
230 * if we decreased the sequence number.
232 if (unlikely((bo->val_seq - ticket->stamp <= LONG_MAX)
237 * In the worst case with memory ordering these values can be
238 * seen in the wrong order. However since we call wake_up_all
239 * in that case, this will hopefully not pose a problem,
240 * and the worst case would only cause someone to accidentally
241 * hit -EAGAIN in ttm_bo_reserve when they see old value of
242 * val_seq. However this would only happen if seq_valid was
243 * written before val_seq was, and just means some slightly
244 * increased cpu usage
246 bo->val_seq = ticket->stamp;
247 bo->seq_valid = true;
249 wake_up_all(&bo->event_queue);
251 bo->seq_valid = false;
256 EXPORT_SYMBOL(ttm_bo_reserve);
258 static void ttm_bo_ref_bug(struct kref *list_kref)
263 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
266 kref_sub(&bo->list_kref, count,
267 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
270 int ttm_bo_reserve(struct ttm_buffer_object *bo,
272 bool no_wait, bool use_ticket,
273 struct ww_acquire_ctx *ticket)
275 struct ttm_bo_global *glob = bo->glob;
279 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_ticket,
281 if (likely(ret == 0)) {
282 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
283 put_count = ttm_bo_del_from_lru(bo);
284 lockmgr(&glob->lru_lock, LK_RELEASE);
285 ttm_bo_list_ref_sub(bo, put_count, true);
291 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
293 struct ww_acquire_ctx *ticket)
295 bool wake_up = false;
298 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
299 WARN_ON(bo->seq_valid && ticket->stamp == bo->val_seq);
301 ret = ttm_bo_wait_unreserved(bo, interruptible);
307 if (bo->val_seq - ticket->stamp < LONG_MAX || !bo->seq_valid)
311 * Wake up waiters that may need to recheck for deadlock,
312 * if we decreased the sequence number.
314 bo->val_seq = ticket->stamp;
315 bo->seq_valid = true;
317 wake_up_all(&bo->event_queue);
322 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
323 bool interruptible, struct ww_acquire_ctx *ticket)
325 struct ttm_bo_global *glob = bo->glob;
328 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, ticket);
330 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
331 put_count = ttm_bo_del_from_lru(bo);
332 lockmgr(&glob->lru_lock, LK_RELEASE);
333 ttm_bo_list_ref_sub(bo, put_count, true);
337 EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
340 * Must interlock with event_queue to avoid race against
341 * wait_event_common() which can cause wait_event_common()
345 ttm_bo_unreserve_core(struct ttm_buffer_object *bo)
347 lockmgr(&bo->event_queue.lock, LK_EXCLUSIVE);
348 atomic_set(&bo->reserved, 0);
349 lockmgr(&bo->event_queue.lock, LK_RELEASE);
350 wake_up_all(&bo->event_queue);
353 void ttm_bo_unreserve_ticket_locked(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
355 ttm_bo_add_to_lru(bo);
356 ttm_bo_unreserve_core(bo);
359 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
361 struct ttm_bo_global *glob = bo->glob;
363 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
364 ttm_bo_unreserve_ticket_locked(bo, NULL);
365 lockmgr(&glob->lru_lock, LK_RELEASE);
367 EXPORT_SYMBOL(ttm_bo_unreserve);
369 void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
371 struct ttm_bo_global *glob = bo->glob;
373 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
374 ttm_bo_unreserve_ticket_locked(bo, ticket);
375 lockmgr(&glob->lru_lock, LK_RELEASE);
377 EXPORT_SYMBOL(ttm_bo_unreserve_ticket);
380 * Call bo->mutex locked.
382 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
384 struct ttm_bo_device *bdev = bo->bdev;
385 struct ttm_bo_global *glob = bo->glob;
387 uint32_t page_flags = 0;
389 TTM_ASSERT_LOCKED(&bo->mutex);
392 if (bdev->need_dma32)
393 page_flags |= TTM_PAGE_FLAG_DMA32;
396 case ttm_bo_type_device:
398 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
399 case ttm_bo_type_kernel:
400 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
401 page_flags, glob->dummy_read_page);
402 if (unlikely(bo->ttm == NULL))
406 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
407 page_flags | TTM_PAGE_FLAG_SG,
408 glob->dummy_read_page);
409 if (unlikely(bo->ttm == NULL)) {
413 bo->ttm->sg = bo->sg;
416 kprintf("[TTM] Illegal buffer object type\n");
424 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
425 struct ttm_mem_reg *mem,
426 bool evict, bool interruptible,
429 struct ttm_bo_device *bdev = bo->bdev;
430 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
431 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
432 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
433 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
436 if (old_is_pci || new_is_pci ||
437 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
438 ret = ttm_mem_io_lock(old_man, true);
439 if (unlikely(ret != 0))
441 ttm_bo_unmap_virtual_locked(bo);
442 ttm_mem_io_unlock(old_man);
446 * Create and bind a ttm if required.
449 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
450 if (bo->ttm == NULL) {
451 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
452 ret = ttm_bo_add_ttm(bo, zero);
457 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
461 if (mem->mem_type != TTM_PL_SYSTEM) {
462 ret = ttm_tt_bind(bo->ttm, mem);
467 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
468 if (bdev->driver->move_notify)
469 bdev->driver->move_notify(bo, mem);
476 if (bdev->driver->move_notify)
477 bdev->driver->move_notify(bo, mem);
479 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
480 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
481 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
482 else if (bdev->driver->move)
483 ret = bdev->driver->move(bo, evict, interruptible,
486 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
489 if (bdev->driver->move_notify) {
490 struct ttm_mem_reg tmp_mem = *mem;
493 bdev->driver->move_notify(bo, mem);
503 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
505 kprintf("[TTM] Can not flush read caches\n");
509 if (bo->mem.mm_node) {
510 bo->offset = (bo->mem.start << PAGE_SHIFT) +
511 bdev->man[bo->mem.mem_type].gpu_offset;
512 bo->cur_placement = bo->mem.placement;
519 new_man = &bdev->man[bo->mem.mem_type];
520 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
521 ttm_tt_unbind(bo->ttm);
522 ttm_tt_destroy(bo->ttm);
531 * Will release GPU memory type usage on destruction.
532 * This is the place to put in driver specific hooks to release
533 * driver private resources.
534 * Will release the bo::reserved lock.
537 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
539 if (bo->bdev->driver->move_notify)
540 bo->bdev->driver->move_notify(bo, NULL);
543 ttm_tt_unbind(bo->ttm);
544 ttm_tt_destroy(bo->ttm);
547 ttm_bo_mem_put(bo, &bo->mem);
548 ttm_bo_unreserve_core(bo);
551 * Since the final reference to this bo may not be dropped by
552 * the current task we have to put a memory barrier here to make
553 * sure the changes done in this function are always visible.
555 * This function only needs protection against the final kref_put.
560 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
562 struct ttm_bo_device *bdev = bo->bdev;
563 struct ttm_bo_global *glob = bo->glob;
564 struct ttm_bo_driver *driver = bdev->driver;
565 void *sync_obj = NULL;
569 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
570 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
572 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
573 (void) ttm_bo_wait(bo, false, false, true);
574 if (!ret && !bo->sync_obj) {
575 lockmgr(&bdev->fence_lock, LK_RELEASE);
576 put_count = ttm_bo_del_from_lru(bo);
578 lockmgr(&glob->lru_lock, LK_RELEASE);
579 ttm_bo_cleanup_memtype_use(bo);
581 ttm_bo_list_ref_sub(bo, put_count, true);
586 sync_obj = driver->sync_obj_ref(bo->sync_obj);
587 lockmgr(&bdev->fence_lock, LK_RELEASE);
592 * Make NO_EVICT bos immediately available to
593 * shrinkers, now that they are queued for
596 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
597 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
598 ttm_bo_add_to_lru(bo);
601 ttm_bo_unreserve_core(bo);
604 kref_get(&bo->list_kref);
605 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
606 lockmgr(&glob->lru_lock, LK_RELEASE);
609 driver->sync_obj_flush(sync_obj);
610 driver->sync_obj_unref(&sync_obj);
612 schedule_delayed_work(&bdev->wq,
613 ((hz / 100) < 1) ? 1 : hz / 100);
617 * function ttm_bo_cleanup_refs_and_unlock
618 * If bo idle, remove from delayed- and lru lists, and unref.
619 * If not idle, do nothing.
621 * Must be called with lru_lock and reservation held, this function
622 * will drop both before returning.
624 * @interruptible Any sleeps should occur interruptibly.
625 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
628 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
632 struct ttm_bo_device *bdev = bo->bdev;
633 struct ttm_bo_driver *driver = bdev->driver;
634 struct ttm_bo_global *glob = bo->glob;
638 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
639 ret = ttm_bo_wait(bo, false, false, true);
641 if (ret && !no_wait_gpu) {
645 * Take a reference to the fence and unreserve,
646 * at this point the buffer should be dead, so
647 * no new sync objects can be attached.
649 sync_obj = driver->sync_obj_ref(bo->sync_obj);
650 lockmgr(&bdev->fence_lock, LK_RELEASE);
652 ttm_bo_unreserve_core(bo);
653 lockmgr(&glob->lru_lock, LK_RELEASE);
655 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
656 driver->sync_obj_unref(&sync_obj);
661 * remove sync_obj with ttm_bo_wait, the wait should be
662 * finished, and no new wait object should have been added.
664 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
665 ret = ttm_bo_wait(bo, false, false, true);
667 lockmgr(&bdev->fence_lock, LK_RELEASE);
671 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
672 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
675 * We raced, and lost, someone else holds the reservation now,
676 * and is probably busy in ttm_bo_cleanup_memtype_use.
678 * Even if it's not the case, because we finished waiting any
679 * delayed destruction would succeed, so just return success
683 lockmgr(&glob->lru_lock, LK_RELEASE);
687 lockmgr(&bdev->fence_lock, LK_RELEASE);
689 if (ret || unlikely(list_empty(&bo->ddestroy))) {
690 ttm_bo_unreserve_core(bo);
691 lockmgr(&glob->lru_lock, LK_RELEASE);
695 put_count = ttm_bo_del_from_lru(bo);
696 list_del_init(&bo->ddestroy);
699 lockmgr(&glob->lru_lock, LK_RELEASE);
700 ttm_bo_cleanup_memtype_use(bo);
702 ttm_bo_list_ref_sub(bo, put_count, true);
708 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
709 * encountered buffers.
712 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
714 struct ttm_bo_global *glob = bdev->glob;
715 struct ttm_buffer_object *entry = NULL;
718 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
719 if (list_empty(&bdev->ddestroy))
722 entry = list_first_entry(&bdev->ddestroy,
723 struct ttm_buffer_object, ddestroy);
724 kref_get(&entry->list_kref);
727 struct ttm_buffer_object *nentry = NULL;
729 if (entry->ddestroy.next != &bdev->ddestroy) {
730 nentry = list_first_entry(&entry->ddestroy,
731 struct ttm_buffer_object, ddestroy);
732 kref_get(&nentry->list_kref);
735 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
736 if (remove_all && ret) {
737 lockmgr(&glob->lru_lock, LK_RELEASE);
738 ret = ttm_bo_reserve_nolru(entry, false, false,
740 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
744 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
747 lockmgr(&glob->lru_lock, LK_RELEASE);
749 kref_put(&entry->list_kref, ttm_bo_release_list);
755 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
756 if (list_empty(&entry->ddestroy))
761 lockmgr(&glob->lru_lock, LK_RELEASE);
764 kref_put(&entry->list_kref, ttm_bo_release_list);
768 static void ttm_bo_delayed_workqueue(struct work_struct *work)
770 struct ttm_bo_device *bdev =
771 container_of(work, struct ttm_bo_device, wq.work);
773 if (ttm_bo_delayed_delete(bdev, false)) {
774 schedule_delayed_work(&bdev->wq,
775 ((hz / 100) < 1) ? 1 : hz / 100);
780 * NOTE: bdev->vm_lock already held on call, this function release it.
782 static void ttm_bo_release(struct kref *kref)
784 struct ttm_buffer_object *bo =
785 container_of(kref, struct ttm_buffer_object, kref);
786 struct ttm_bo_device *bdev = bo->bdev;
787 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
790 if (atomic_read(&bo->kref.refcount) > 0) {
791 lockmgr(&bdev->vm_lock, LK_RELEASE);
794 if (likely(bo->vm_node != NULL)) {
795 RB_REMOVE(ttm_bo_device_buffer_objects,
796 &bdev->addr_space_rb, bo);
797 drm_mm_put_block(bo->vm_node);
802 * Should we clean up our implied list_kref? Because ttm_bo_release()
803 * can be called reentrantly due to races (this may not be true any
804 * more with the lock management changes in the deref), it is possible
805 * to get here twice, but there's only one list_kref ref to drop and
806 * in the other path 'bo' can be kfree()d by another thread the
807 * instant we release our lock.
809 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
810 if (release_active) {
811 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
812 lockmgr(&bdev->vm_lock, LK_RELEASE);
813 ttm_mem_io_lock(man, false);
814 ttm_mem_io_free_vm(bo);
815 ttm_mem_io_unlock(man);
816 ttm_bo_cleanup_refs_or_queue(bo);
817 kref_put(&bo->list_kref, ttm_bo_release_list);
819 lockmgr(&bdev->vm_lock, LK_RELEASE);
823 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
825 struct ttm_buffer_object *bo = *p_bo;
826 struct ttm_bo_device *bdev = bo->bdev;
829 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
830 if (kref_put(&bo->kref, ttm_bo_release) == 0)
831 lockmgr(&bdev->vm_lock, LK_RELEASE);
833 EXPORT_SYMBOL(ttm_bo_unref);
835 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
837 return cancel_delayed_work_sync(&bdev->wq);
839 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
841 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
844 schedule_delayed_work(&bdev->wq,
845 ((hz / 100) < 1) ? 1 : hz / 100);
847 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
849 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
852 struct ttm_bo_device *bdev = bo->bdev;
853 struct ttm_mem_reg evict_mem;
854 struct ttm_placement placement;
857 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
858 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
859 lockmgr(&bdev->fence_lock, LK_RELEASE);
861 if (unlikely(ret != 0)) {
862 if (ret != -ERESTARTSYS) {
863 pr_err("Failed to expire sync object before buffer eviction\n");
868 BUG_ON(!ttm_bo_is_reserved(bo));
871 evict_mem.mm_node = NULL;
872 evict_mem.bus.io_reserved_vm = false;
873 evict_mem.bus.io_reserved_count = 0;
877 placement.num_placement = 0;
878 placement.num_busy_placement = 0;
879 bdev->driver->evict_flags(bo, &placement);
880 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
883 if (ret != -ERESTARTSYS) {
884 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
886 ttm_bo_mem_space_debug(bo, &placement);
891 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
894 if (ret != -ERESTARTSYS)
895 pr_err("Buffer eviction failed\n");
896 ttm_bo_mem_put(bo, &evict_mem);
904 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
909 struct ttm_bo_global *glob = bdev->glob;
910 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
911 struct ttm_buffer_object *bo;
912 int ret = -EBUSY, put_count;
914 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
915 list_for_each_entry(bo, &man->lru, lru) {
916 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
922 lockmgr(&glob->lru_lock, LK_RELEASE);
926 kref_get(&bo->list_kref);
928 if (!list_empty(&bo->ddestroy)) {
929 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
931 kref_put(&bo->list_kref, ttm_bo_release_list);
935 put_count = ttm_bo_del_from_lru(bo);
936 lockmgr(&glob->lru_lock, LK_RELEASE);
940 ttm_bo_list_ref_sub(bo, put_count, true);
942 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
943 ttm_bo_unreserve(bo);
945 kref_put(&bo->list_kref, ttm_bo_release_list);
949 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
951 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
954 (*man->func->put_node)(man, mem);
956 EXPORT_SYMBOL(ttm_bo_mem_put);
959 * Repeatedly evict memory from the LRU for @mem_type until we create enough
960 * space, or we've evicted everything and there isn't enough space.
962 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
964 struct ttm_placement *placement,
965 struct ttm_mem_reg *mem,
969 struct ttm_bo_device *bdev = bo->bdev;
970 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
974 ret = (*man->func->get_node)(man, bo, placement, 0, mem);
975 if (unlikely(ret != 0))
979 ret = ttm_mem_evict_first(bdev, mem_type,
980 interruptible, no_wait_gpu);
981 if (unlikely(ret != 0))
984 if (mem->mm_node == NULL)
986 mem->mem_type = mem_type;
990 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
991 uint32_t cur_placement,
992 uint32_t proposed_placement)
994 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
995 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
998 * Keep current caching if possible.
1001 if ((cur_placement & caching) != 0)
1002 result |= (cur_placement & caching);
1003 else if ((man->default_caching & caching) != 0)
1004 result |= man->default_caching;
1005 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
1006 result |= TTM_PL_FLAG_CACHED;
1007 else if ((TTM_PL_FLAG_WC & caching) != 0)
1008 result |= TTM_PL_FLAG_WC;
1009 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
1010 result |= TTM_PL_FLAG_UNCACHED;
1015 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
1017 uint32_t proposed_placement,
1018 uint32_t *masked_placement)
1020 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
1022 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
1025 if ((proposed_placement & man->available_caching) == 0)
1028 cur_flags |= (proposed_placement & man->available_caching);
1030 *masked_placement = cur_flags;
1035 * Creates space for memory region @mem according to its type.
1037 * This function first searches for free space in compatible memory types in
1038 * the priority order defined by the driver. If free space isn't found, then
1039 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1042 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1043 struct ttm_placement *placement,
1044 struct ttm_mem_reg *mem,
1048 struct ttm_bo_device *bdev = bo->bdev;
1049 struct ttm_mem_type_manager *man;
1050 uint32_t mem_type = TTM_PL_SYSTEM;
1051 uint32_t cur_flags = 0;
1052 bool type_found = false;
1053 bool type_ok = false;
1054 bool has_erestartsys = false;
1057 mem->mm_node = NULL;
1058 for (i = 0; i < placement->num_placement; ++i) {
1059 ret = ttm_mem_type_from_flags(placement->placement[i],
1063 man = &bdev->man[mem_type];
1065 type_ok = ttm_bo_mt_compatible(man,
1067 placement->placement[i],
1073 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1076 * Use the access and other non-mapping-related flag bits from
1077 * the memory placement flags to the current flags
1079 ttm_flag_masked(&cur_flags, placement->placement[i],
1080 ~TTM_PL_MASK_MEMTYPE);
1082 if (mem_type == TTM_PL_SYSTEM)
1085 if (man->has_type && man->use_type) {
1087 ret = (*man->func->get_node)(man, bo, placement,
1096 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1097 mem->mem_type = mem_type;
1098 mem->placement = cur_flags;
1105 for (i = 0; i < placement->num_busy_placement; ++i) {
1106 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1110 man = &bdev->man[mem_type];
1113 if (!ttm_bo_mt_compatible(man,
1115 placement->busy_placement[i],
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, placement->busy_placement[i],
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, placement, 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 if (mem->mm_node && placement->lpfn != 0 &&
1197 (mem->start < placement->fpfn ||
1198 mem->start + mem->num_pages > placement->lpfn))
1201 for (i = 0; i < placement->num_placement; i++) {
1202 *new_flags = placement->placement[i];
1203 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1204 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1208 for (i = 0; i < placement->num_busy_placement; i++) {
1209 *new_flags = placement->busy_placement[i];
1210 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1211 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1218 int ttm_bo_validate(struct ttm_buffer_object *bo,
1219 struct ttm_placement *placement,
1226 BUG_ON(!ttm_bo_is_reserved(bo));
1227 /* Check that range is valid */
1228 if (placement->lpfn || placement->fpfn)
1229 if (placement->fpfn > placement->lpfn ||
1230 (placement->lpfn - placement->fpfn) < bo->num_pages)
1233 * Check whether we need to move buffer.
1235 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1236 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1242 * Use the access and other non-mapping-related flag bits from
1243 * the compatible memory placement flags to the active flags
1245 ttm_flag_masked(&bo->mem.placement, new_flags,
1246 ~TTM_PL_MASK_MEMTYPE);
1249 * We might need to add a TTM.
1251 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1252 ret = ttm_bo_add_ttm(bo, true);
1258 EXPORT_SYMBOL(ttm_bo_validate);
1260 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1261 struct ttm_placement *placement)
1263 BUG_ON((placement->fpfn || placement->lpfn) &&
1264 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1269 int ttm_bo_init(struct ttm_bo_device *bdev,
1270 struct ttm_buffer_object *bo,
1272 enum ttm_bo_type type,
1273 struct ttm_placement *placement,
1274 uint32_t page_alignment,
1276 struct vm_object *persistent_swap_storage,
1278 struct sg_table *sg,
1279 void (*destroy) (struct ttm_buffer_object *))
1282 unsigned long num_pages;
1283 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1285 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1287 kprintf("[TTM] Out of kernel memory\n");
1295 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1296 if (num_pages == 0) {
1297 kprintf("[TTM] Illegal buffer object size\n");
1302 ttm_mem_global_free(mem_glob, acc_size);
1305 bo->destroy = destroy;
1307 kref_init(&bo->kref);
1308 kref_init(&bo->list_kref);
1309 atomic_set(&bo->cpu_writers, 0);
1310 atomic_set(&bo->reserved, 1);
1311 init_waitqueue_head(&bo->event_queue);
1312 INIT_LIST_HEAD(&bo->lru);
1313 INIT_LIST_HEAD(&bo->ddestroy);
1314 INIT_LIST_HEAD(&bo->swap);
1315 INIT_LIST_HEAD(&bo->io_reserve_lru);
1316 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1318 bo->glob = bdev->glob;
1320 bo->num_pages = num_pages;
1321 bo->mem.size = num_pages << PAGE_SHIFT;
1322 bo->mem.mem_type = TTM_PL_SYSTEM;
1323 bo->mem.num_pages = bo->num_pages;
1324 bo->mem.mm_node = NULL;
1325 bo->mem.page_alignment = page_alignment;
1326 bo->mem.bus.io_reserved_vm = false;
1327 bo->mem.bus.io_reserved_count = 0;
1329 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1330 bo->seq_valid = false;
1331 bo->persistent_swap_storage = persistent_swap_storage;
1332 bo->acc_size = acc_size;
1334 atomic_inc(&bo->glob->bo_count);
1337 * Mirror ref from kref_init() for list_kref.
1339 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1341 ret = ttm_bo_check_placement(bo, placement);
1342 if (unlikely(ret != 0))
1346 * For ttm_bo_type_device buffers, allocate
1347 * address space from the device.
1349 if (bo->type == ttm_bo_type_device ||
1350 bo->type == ttm_bo_type_sg) {
1351 ret = ttm_bo_setup_vm(bo);
1356 ret = ttm_bo_validate(bo, placement, interruptible, false);
1360 ttm_bo_unreserve(bo);
1364 ttm_bo_unreserve(bo);
1369 EXPORT_SYMBOL(ttm_bo_init);
1371 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1372 unsigned long bo_size,
1373 unsigned struct_size)
1375 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1378 size += ttm_round_pot(struct_size);
1379 size += PAGE_ALIGN(npages * sizeof(void *));
1380 size += ttm_round_pot(sizeof(struct ttm_tt));
1383 EXPORT_SYMBOL(ttm_bo_acc_size);
1385 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1386 unsigned long bo_size,
1387 unsigned struct_size)
1389 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1392 size += ttm_round_pot(struct_size);
1393 size += PAGE_ALIGN(npages * sizeof(void *));
1394 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1395 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1398 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1400 int ttm_bo_create(struct ttm_bo_device *bdev,
1402 enum ttm_bo_type type,
1403 struct ttm_placement *placement,
1404 uint32_t page_alignment,
1406 struct vm_object *persistent_swap_storage,
1407 struct ttm_buffer_object **p_bo)
1409 struct ttm_buffer_object *bo;
1414 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1415 if (unlikely(bo == NULL))
1418 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1419 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1420 interruptible, persistent_swap_storage, acc_size,
1422 if (likely(ret == 0))
1427 EXPORT_SYMBOL(ttm_bo_create);
1429 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1430 unsigned mem_type, bool allow_errors)
1432 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1433 struct ttm_bo_global *glob = bdev->glob;
1437 * Can't use standard list traversal since we're unlocking.
1440 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1441 while (!list_empty(&man->lru)) {
1442 lockmgr(&glob->lru_lock, LK_RELEASE);
1443 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1448 kprintf("[TTM] Cleanup eviction failed\n");
1451 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1453 lockmgr(&glob->lru_lock, LK_RELEASE);
1457 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1459 struct ttm_mem_type_manager *man;
1462 if (mem_type >= TTM_NUM_MEM_TYPES) {
1463 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1466 man = &bdev->man[mem_type];
1468 if (!man->has_type) {
1469 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1474 man->use_type = false;
1475 man->has_type = false;
1479 ttm_bo_force_list_clean(bdev, mem_type, false);
1481 ret = (*man->func->takedown)(man);
1486 EXPORT_SYMBOL(ttm_bo_clean_mm);
1488 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1490 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1492 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1493 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1497 if (!man->has_type) {
1498 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1502 return ttm_bo_force_list_clean(bdev, mem_type, true);
1504 EXPORT_SYMBOL(ttm_bo_evict_mm);
1506 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1507 unsigned long p_size)
1510 struct ttm_mem_type_manager *man;
1512 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1513 man = &bdev->man[type];
1514 BUG_ON(man->has_type);
1515 man->io_reserve_fastpath = true;
1516 man->use_io_reserve_lru = false;
1517 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1518 INIT_LIST_HEAD(&man->io_reserve_lru);
1520 ret = bdev->driver->init_mem_type(bdev, type, man);
1526 if (type != TTM_PL_SYSTEM) {
1527 ret = (*man->func->init)(man, p_size);
1531 man->has_type = true;
1532 man->use_type = true;
1535 INIT_LIST_HEAD(&man->lru);
1539 EXPORT_SYMBOL(ttm_bo_init_mm);
1541 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1543 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1544 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1545 glob->dummy_read_page = NULL;
1547 vm_page_free(glob->dummy_read_page);
1551 void ttm_bo_global_release(struct drm_global_reference *ref)
1553 struct ttm_bo_global *glob = ref->object;
1555 if (refcount_release(&glob->kobj_ref))
1556 ttm_bo_global_kobj_release(glob);
1558 EXPORT_SYMBOL(ttm_bo_global_release);
1560 int ttm_bo_global_init(struct drm_global_reference *ref)
1562 struct ttm_bo_global_ref *bo_ref =
1563 container_of(ref, struct ttm_bo_global_ref, ref);
1564 struct ttm_bo_global *glob = ref->object;
1567 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1568 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1569 glob->mem_glob = bo_ref->mem_glob;
1570 glob->dummy_read_page = vm_page_alloc_contig(
1571 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1573 if (unlikely(glob->dummy_read_page == NULL)) {
1578 INIT_LIST_HEAD(&glob->swap_lru);
1579 INIT_LIST_HEAD(&glob->device_list);
1581 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1582 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1583 if (unlikely(ret != 0)) {
1584 kprintf("[TTM] Could not register buffer object swapout\n");
1588 atomic_set(&glob->bo_count, 0);
1590 refcount_init(&glob->kobj_ref, 1);
1594 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1595 glob->dummy_read_page = NULL;
1597 vm_page_free(glob->dummy_read_page);
1603 EXPORT_SYMBOL(ttm_bo_global_init);
1606 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1609 unsigned i = TTM_NUM_MEM_TYPES;
1610 struct ttm_mem_type_manager *man;
1611 struct ttm_bo_global *glob = bdev->glob;
1614 man = &bdev->man[i];
1615 if (man->has_type) {
1616 man->use_type = false;
1617 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1619 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1622 man->has_type = false;
1626 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1627 list_del(&bdev->device_list);
1628 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1630 cancel_delayed_work_sync(&bdev->wq);
1632 while (ttm_bo_delayed_delete(bdev, true))
1635 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1636 if (list_empty(&bdev->ddestroy))
1637 TTM_DEBUG("Delayed destroy list was clean\n");
1639 if (list_empty(&bdev->man[0].lru))
1640 TTM_DEBUG("Swap list was clean\n");
1641 lockmgr(&glob->lru_lock, LK_RELEASE);
1643 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1644 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1645 drm_mm_takedown(&bdev->addr_space_mm);
1646 lockmgr(&bdev->vm_lock, LK_RELEASE);
1650 EXPORT_SYMBOL(ttm_bo_device_release);
1652 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1653 struct ttm_bo_global *glob,
1654 struct ttm_bo_driver *driver,
1655 uint64_t file_page_offset,
1660 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1661 bdev->driver = driver;
1663 memset(bdev->man, 0, sizeof(bdev->man));
1666 * Initialize the system memory buffer type.
1667 * Other types need to be driver / IOCTL initialized.
1669 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1670 if (unlikely(ret != 0))
1673 RB_INIT(&bdev->addr_space_rb);
1674 drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1676 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1677 INIT_LIST_HEAD(&bdev->ddestroy);
1678 bdev->dev_mapping = NULL;
1680 bdev->need_dma32 = need_dma32;
1682 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1683 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1684 list_add_tail(&bdev->device_list, &glob->device_list);
1685 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1691 EXPORT_SYMBOL(ttm_bo_device_init);
1694 * buffer object vm functions.
1697 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1699 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1701 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1702 if (mem->mem_type == TTM_PL_SYSTEM)
1705 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1708 if (mem->placement & TTM_PL_FLAG_CACHED)
1714 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1717 ttm_bo_release_mmap(bo);
1718 ttm_mem_io_free_vm(bo);
1721 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1723 struct ttm_bo_device *bdev = bo->bdev;
1724 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1726 ttm_mem_io_lock(man, false);
1727 ttm_bo_unmap_virtual_locked(bo);
1728 ttm_mem_io_unlock(man);
1732 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1734 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1736 struct ttm_bo_device *bdev = bo->bdev;
1738 /* The caller acquired bdev->vm_lock. */
1739 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1745 * @bo: the buffer to allocate address space for
1747 * Allocate address space in the drm device so that applications
1748 * can mmap the buffer and access the contents. This only
1749 * applies to ttm_bo_type_device objects as others are not
1750 * placed in the drm device address space.
1753 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1755 struct ttm_bo_device *bdev = bo->bdev;
1759 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1760 if (unlikely(ret != 0))
1763 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1764 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1765 bo->mem.num_pages, 0, 0);
1767 if (unlikely(bo->vm_node == NULL)) {
1772 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1773 bo->mem.num_pages, 0);
1775 if (unlikely(bo->vm_node == NULL)) {
1776 lockmgr(&bdev->vm_lock, LK_RELEASE);
1780 ttm_bo_vm_insert_rb(bo);
1781 lockmgr(&bdev->vm_lock, LK_RELEASE);
1782 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1786 lockmgr(&bdev->vm_lock, LK_RELEASE);
1790 int ttm_bo_wait(struct ttm_buffer_object *bo,
1791 bool lazy, bool interruptible, bool no_wait)
1793 struct ttm_bo_driver *driver = bo->bdev->driver;
1794 struct ttm_bo_device *bdev = bo->bdev;
1798 if (likely(bo->sync_obj == NULL))
1801 while (bo->sync_obj) {
1803 if (driver->sync_obj_signaled(bo->sync_obj)) {
1804 void *tmp_obj = bo->sync_obj;
1805 bo->sync_obj = NULL;
1806 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1807 lockmgr(&bdev->fence_lock, LK_RELEASE);
1808 driver->sync_obj_unref(&tmp_obj);
1809 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1816 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1817 lockmgr(&bdev->fence_lock, LK_RELEASE);
1818 ret = driver->sync_obj_wait(sync_obj,
1819 lazy, interruptible);
1820 if (unlikely(ret != 0)) {
1821 driver->sync_obj_unref(&sync_obj);
1822 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1825 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1826 if (likely(bo->sync_obj == sync_obj)) {
1827 void *tmp_obj = bo->sync_obj;
1828 bo->sync_obj = NULL;
1829 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1831 lockmgr(&bdev->fence_lock, LK_RELEASE);
1832 driver->sync_obj_unref(&sync_obj);
1833 driver->sync_obj_unref(&tmp_obj);
1834 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1836 lockmgr(&bdev->fence_lock, LK_RELEASE);
1837 driver->sync_obj_unref(&sync_obj);
1838 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1843 EXPORT_SYMBOL(ttm_bo_wait);
1845 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1847 struct ttm_bo_device *bdev = bo->bdev;
1851 * Using ttm_bo_reserve makes sure the lru lists are updated.
1854 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1855 if (unlikely(ret != 0))
1857 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1858 ret = ttm_bo_wait(bo, false, true, no_wait);
1859 lockmgr(&bdev->fence_lock, LK_RELEASE);
1860 if (likely(ret == 0))
1861 atomic_inc(&bo->cpu_writers);
1862 ttm_bo_unreserve(bo);
1865 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1867 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1869 atomic_dec(&bo->cpu_writers);
1871 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1874 * A buffer object shrink method that tries to swap out the first
1875 * buffer object on the bo_global::swap_lru list.
1878 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1880 struct ttm_bo_global *glob =
1881 container_of(shrink, struct ttm_bo_global, shrink);
1882 struct ttm_buffer_object *bo;
1885 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1887 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1888 list_for_each_entry(bo, &glob->swap_lru, swap) {
1889 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1895 lockmgr(&glob->lru_lock, LK_RELEASE);
1899 kref_get(&bo->list_kref);
1901 if (!list_empty(&bo->ddestroy)) {
1902 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1903 kref_put(&bo->list_kref, ttm_bo_release_list);
1907 put_count = ttm_bo_del_from_lru(bo);
1908 lockmgr(&glob->lru_lock, LK_RELEASE);
1910 ttm_bo_list_ref_sub(bo, put_count, true);
1913 * Wait for GPU, then move to system cached.
1916 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1917 ret = ttm_bo_wait(bo, false, false, false);
1918 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1920 if (unlikely(ret != 0))
1923 if ((bo->mem.placement & swap_placement) != swap_placement) {
1924 struct ttm_mem_reg evict_mem;
1926 evict_mem = bo->mem;
1927 evict_mem.mm_node = NULL;
1928 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1929 evict_mem.mem_type = TTM_PL_SYSTEM;
1931 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1933 if (unlikely(ret != 0))
1937 ttm_bo_unmap_virtual(bo);
1940 * Swap out. Buffer will be swapped in again as soon as
1941 * anyone tries to access a ttm page.
1944 if (bo->bdev->driver->swap_notify)
1945 bo->bdev->driver->swap_notify(bo);
1947 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1952 * Unreserve without putting on LRU to avoid swapping out an
1953 * already swapped buffer.
1956 ttm_bo_unreserve_core(bo);
1957 kref_put(&bo->list_kref, ttm_bo_release_list);
1961 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1963 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1966 EXPORT_SYMBOL(ttm_bo_swapout_all);