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);
590 ttm_bo_unreserve_core(bo);
593 kref_get(&bo->list_kref);
594 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
595 lockmgr(&glob->lru_lock, LK_RELEASE);
598 driver->sync_obj_flush(sync_obj);
599 driver->sync_obj_unref(&sync_obj);
601 schedule_delayed_work(&bdev->wq,
602 ((hz / 100) < 1) ? 1 : hz / 100);
606 * function ttm_bo_cleanup_refs_and_unlock
607 * If bo idle, remove from delayed- and lru lists, and unref.
608 * If not idle, do nothing.
610 * Must be called with lru_lock and reservation held, this function
611 * will drop both before returning.
613 * @interruptible Any sleeps should occur interruptibly.
614 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
617 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
621 struct ttm_bo_device *bdev = bo->bdev;
622 struct ttm_bo_driver *driver = bdev->driver;
623 struct ttm_bo_global *glob = bo->glob;
627 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
628 ret = ttm_bo_wait(bo, false, false, true);
630 if (ret && !no_wait_gpu) {
634 * Take a reference to the fence and unreserve,
635 * at this point the buffer should be dead, so
636 * no new sync objects can be attached.
638 sync_obj = driver->sync_obj_ref(bo->sync_obj);
639 lockmgr(&bdev->fence_lock, LK_RELEASE);
641 ttm_bo_unreserve_core(bo);
642 lockmgr(&glob->lru_lock, LK_RELEASE);
644 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
645 driver->sync_obj_unref(&sync_obj);
650 * remove sync_obj with ttm_bo_wait, the wait should be
651 * finished, and no new wait object should have been added.
653 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
654 ret = ttm_bo_wait(bo, false, false, true);
656 lockmgr(&bdev->fence_lock, LK_RELEASE);
660 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
661 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
664 * We raced, and lost, someone else holds the reservation now,
665 * and is probably busy in ttm_bo_cleanup_memtype_use.
667 * Even if it's not the case, because we finished waiting any
668 * delayed destruction would succeed, so just return success
672 lockmgr(&glob->lru_lock, LK_RELEASE);
676 lockmgr(&bdev->fence_lock, LK_RELEASE);
678 if (ret || unlikely(list_empty(&bo->ddestroy))) {
679 ttm_bo_unreserve_core(bo);
680 lockmgr(&glob->lru_lock, LK_RELEASE);
684 put_count = ttm_bo_del_from_lru(bo);
685 list_del_init(&bo->ddestroy);
688 lockmgr(&glob->lru_lock, LK_RELEASE);
689 ttm_bo_cleanup_memtype_use(bo);
691 ttm_bo_list_ref_sub(bo, put_count, true);
697 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
698 * encountered buffers.
701 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
703 struct ttm_bo_global *glob = bdev->glob;
704 struct ttm_buffer_object *entry = NULL;
707 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
708 if (list_empty(&bdev->ddestroy))
711 entry = list_first_entry(&bdev->ddestroy,
712 struct ttm_buffer_object, ddestroy);
713 kref_get(&entry->list_kref);
716 struct ttm_buffer_object *nentry = NULL;
718 if (entry->ddestroy.next != &bdev->ddestroy) {
719 nentry = list_first_entry(&entry->ddestroy,
720 struct ttm_buffer_object, ddestroy);
721 kref_get(&nentry->list_kref);
724 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
725 if (remove_all && ret) {
726 lockmgr(&glob->lru_lock, LK_RELEASE);
727 ret = ttm_bo_reserve_nolru(entry, false, false,
729 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
733 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
736 lockmgr(&glob->lru_lock, LK_RELEASE);
738 kref_put(&entry->list_kref, ttm_bo_release_list);
744 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
745 if (list_empty(&entry->ddestroy))
750 lockmgr(&glob->lru_lock, LK_RELEASE);
753 kref_put(&entry->list_kref, ttm_bo_release_list);
757 static void ttm_bo_delayed_workqueue(struct work_struct *work)
759 struct ttm_bo_device *bdev =
760 container_of(work, struct ttm_bo_device, wq.work);
762 if (ttm_bo_delayed_delete(bdev, false)) {
763 schedule_delayed_work(&bdev->wq,
764 ((hz / 100) < 1) ? 1 : hz / 100);
769 * NOTE: bdev->vm_lock already held on call, this function release it.
771 static void ttm_bo_release(struct kref *kref)
773 struct ttm_buffer_object *bo =
774 container_of(kref, struct ttm_buffer_object, kref);
775 struct ttm_bo_device *bdev = bo->bdev;
776 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
779 if (atomic_read(&bo->kref.refcount) > 0) {
780 lockmgr(&bdev->vm_lock, LK_RELEASE);
783 if (likely(bo->vm_node != NULL)) {
784 RB_REMOVE(ttm_bo_device_buffer_objects,
785 &bdev->addr_space_rb, bo);
786 drm_mm_put_block(bo->vm_node);
791 * Should we clean up our implied list_kref? Because ttm_bo_release()
792 * can be called reentrantly due to races (this may not be true any
793 * more with the lock management changes in the deref), it is possible
794 * to get here twice, but there's only one list_kref ref to drop and
795 * in the other path 'bo' can be kfree()d by another thread the
796 * instant we release our lock.
798 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
799 if (release_active) {
800 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
801 lockmgr(&bdev->vm_lock, LK_RELEASE);
802 ttm_mem_io_lock(man, false);
803 ttm_mem_io_free_vm(bo);
804 ttm_mem_io_unlock(man);
805 ttm_bo_cleanup_refs_or_queue(bo);
806 kref_put(&bo->list_kref, ttm_bo_release_list);
808 lockmgr(&bdev->vm_lock, LK_RELEASE);
812 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
814 struct ttm_buffer_object *bo = *p_bo;
815 struct ttm_bo_device *bdev = bo->bdev;
818 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
819 if (kref_put(&bo->kref, ttm_bo_release) == 0)
820 lockmgr(&bdev->vm_lock, LK_RELEASE);
822 EXPORT_SYMBOL(ttm_bo_unref);
824 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
826 return cancel_delayed_work_sync(&bdev->wq);
828 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
830 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
833 schedule_delayed_work(&bdev->wq,
834 ((hz / 100) < 1) ? 1 : hz / 100);
836 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
838 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
841 struct ttm_bo_device *bdev = bo->bdev;
842 struct ttm_mem_reg evict_mem;
843 struct ttm_placement placement;
846 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
847 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
848 lockmgr(&bdev->fence_lock, LK_RELEASE);
850 if (unlikely(ret != 0)) {
851 if (ret != -ERESTARTSYS) {
852 pr_err("Failed to expire sync object before buffer eviction\n");
857 BUG_ON(!ttm_bo_is_reserved(bo));
860 evict_mem.mm_node = NULL;
861 evict_mem.bus.io_reserved_vm = false;
862 evict_mem.bus.io_reserved_count = 0;
866 placement.num_placement = 0;
867 placement.num_busy_placement = 0;
868 bdev->driver->evict_flags(bo, &placement);
869 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
872 if (ret != -ERESTARTSYS) {
873 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
875 ttm_bo_mem_space_debug(bo, &placement);
880 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
883 if (ret != -ERESTARTSYS)
884 pr_err("Buffer eviction failed\n");
885 ttm_bo_mem_put(bo, &evict_mem);
893 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
898 struct ttm_bo_global *glob = bdev->glob;
899 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
900 struct ttm_buffer_object *bo;
901 int ret = -EBUSY, put_count;
903 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
904 list_for_each_entry(bo, &man->lru, lru) {
905 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
911 lockmgr(&glob->lru_lock, LK_RELEASE);
915 kref_get(&bo->list_kref);
917 if (!list_empty(&bo->ddestroy)) {
918 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
920 kref_put(&bo->list_kref, ttm_bo_release_list);
924 put_count = ttm_bo_del_from_lru(bo);
925 lockmgr(&glob->lru_lock, LK_RELEASE);
929 ttm_bo_list_ref_sub(bo, put_count, true);
931 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
932 ttm_bo_unreserve(bo);
934 kref_put(&bo->list_kref, ttm_bo_release_list);
938 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
940 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
943 (*man->func->put_node)(man, mem);
945 EXPORT_SYMBOL(ttm_bo_mem_put);
948 * Repeatedly evict memory from the LRU for @mem_type until we create enough
949 * space, or we've evicted everything and there isn't enough space.
951 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
953 struct ttm_placement *placement,
954 struct ttm_mem_reg *mem,
958 struct ttm_bo_device *bdev = bo->bdev;
959 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
963 ret = (*man->func->get_node)(man, bo, placement, mem);
964 if (unlikely(ret != 0))
968 ret = ttm_mem_evict_first(bdev, mem_type,
969 interruptible, no_wait_gpu);
970 if (unlikely(ret != 0))
973 if (mem->mm_node == NULL)
975 mem->mem_type = mem_type;
979 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
980 uint32_t cur_placement,
981 uint32_t proposed_placement)
983 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
984 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
987 * Keep current caching if possible.
990 if ((cur_placement & caching) != 0)
991 result |= (cur_placement & caching);
992 else if ((man->default_caching & caching) != 0)
993 result |= man->default_caching;
994 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
995 result |= TTM_PL_FLAG_CACHED;
996 else if ((TTM_PL_FLAG_WC & caching) != 0)
997 result |= TTM_PL_FLAG_WC;
998 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
999 result |= TTM_PL_FLAG_UNCACHED;
1004 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
1006 uint32_t proposed_placement,
1007 uint32_t *masked_placement)
1009 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
1011 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
1014 if ((proposed_placement & man->available_caching) == 0)
1017 cur_flags |= (proposed_placement & man->available_caching);
1019 *masked_placement = cur_flags;
1024 * Creates space for memory region @mem according to its type.
1026 * This function first searches for free space in compatible memory types in
1027 * the priority order defined by the driver. If free space isn't found, then
1028 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1031 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1032 struct ttm_placement *placement,
1033 struct ttm_mem_reg *mem,
1037 struct ttm_bo_device *bdev = bo->bdev;
1038 struct ttm_mem_type_manager *man;
1039 uint32_t mem_type = TTM_PL_SYSTEM;
1040 uint32_t cur_flags = 0;
1041 bool type_found = false;
1042 bool type_ok = false;
1043 bool has_erestartsys = false;
1046 mem->mm_node = NULL;
1047 for (i = 0; i < placement->num_placement; ++i) {
1048 ret = ttm_mem_type_from_flags(placement->placement[i],
1052 man = &bdev->man[mem_type];
1054 type_ok = ttm_bo_mt_compatible(man,
1056 placement->placement[i],
1062 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1065 * Use the access and other non-mapping-related flag bits from
1066 * the memory placement flags to the current flags
1068 ttm_flag_masked(&cur_flags, placement->placement[i],
1069 ~TTM_PL_MASK_MEMTYPE);
1071 if (mem_type == TTM_PL_SYSTEM)
1074 if (man->has_type && man->use_type) {
1076 ret = (*man->func->get_node)(man, bo, placement, mem);
1084 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1085 mem->mem_type = mem_type;
1086 mem->placement = cur_flags;
1093 for (i = 0; i < placement->num_busy_placement; ++i) {
1094 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1098 man = &bdev->man[mem_type];
1101 if (!ttm_bo_mt_compatible(man,
1103 placement->busy_placement[i],
1107 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1110 * Use the access and other non-mapping-related flag bits from
1111 * the memory placement flags to the current flags
1113 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1114 ~TTM_PL_MASK_MEMTYPE);
1117 if (mem_type == TTM_PL_SYSTEM) {
1118 mem->mem_type = mem_type;
1119 mem->placement = cur_flags;
1120 mem->mm_node = NULL;
1124 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1125 interruptible, no_wait_gpu);
1126 if (ret == 0 && mem->mm_node) {
1127 mem->placement = cur_flags;
1130 if (ret == -ERESTARTSYS)
1131 has_erestartsys = true;
1133 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1136 EXPORT_SYMBOL(ttm_bo_mem_space);
1139 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1140 struct ttm_placement *placement,
1145 struct ttm_mem_reg mem;
1146 struct ttm_bo_device *bdev = bo->bdev;
1148 BUG_ON(!ttm_bo_is_reserved(bo));
1151 * FIXME: It's possible to pipeline buffer moves.
1152 * Have the driver move function wait for idle when necessary,
1153 * instead of doing it here.
1155 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1156 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1157 lockmgr(&bdev->fence_lock, LK_RELEASE);
1160 mem.num_pages = bo->num_pages;
1161 mem.size = mem.num_pages << PAGE_SHIFT;
1162 mem.page_alignment = bo->mem.page_alignment;
1163 mem.bus.io_reserved_vm = false;
1164 mem.bus.io_reserved_count = 0;
1166 * Determine where to move the buffer.
1168 ret = ttm_bo_mem_space(bo, placement, &mem,
1169 interruptible, no_wait_gpu);
1172 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1173 interruptible, no_wait_gpu);
1175 if (ret && mem.mm_node)
1176 ttm_bo_mem_put(bo, &mem);
1180 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1181 struct ttm_mem_reg *mem)
1185 if (mem->mm_node && placement->lpfn != 0 &&
1186 (mem->start < placement->fpfn ||
1187 mem->start + mem->num_pages > placement->lpfn))
1190 for (i = 0; i < placement->num_placement; i++) {
1191 if ((placement->placement[i] & mem->placement &
1192 TTM_PL_MASK_CACHING) &&
1193 (placement->placement[i] & mem->placement &
1200 int ttm_bo_validate(struct ttm_buffer_object *bo,
1201 struct ttm_placement *placement,
1207 BUG_ON(!ttm_bo_is_reserved(bo));
1208 /* Check that range is valid */
1209 if (placement->lpfn || placement->fpfn)
1210 if (placement->fpfn > placement->lpfn ||
1211 (placement->lpfn - placement->fpfn) < bo->num_pages)
1214 * Check whether we need to move buffer.
1216 ret = ttm_bo_mem_compat(placement, &bo->mem);
1218 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1224 * Use the access and other non-mapping-related flag bits from
1225 * the compatible memory placement flags to the active flags
1227 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1228 ~TTM_PL_MASK_MEMTYPE);
1231 * We might need to add a TTM.
1233 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1234 ret = ttm_bo_add_ttm(bo, true);
1240 EXPORT_SYMBOL(ttm_bo_validate);
1242 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1243 struct ttm_placement *placement)
1245 BUG_ON((placement->fpfn || placement->lpfn) &&
1246 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1251 int ttm_bo_init(struct ttm_bo_device *bdev,
1252 struct ttm_buffer_object *bo,
1254 enum ttm_bo_type type,
1255 struct ttm_placement *placement,
1256 uint32_t page_alignment,
1258 struct vm_object *persistent_swap_storage,
1260 struct sg_table *sg,
1261 void (*destroy) (struct ttm_buffer_object *))
1264 unsigned long num_pages;
1265 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1267 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1269 kprintf("[TTM] Out of kernel memory\n");
1277 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1278 if (num_pages == 0) {
1279 kprintf("[TTM] Illegal buffer object size\n");
1284 ttm_mem_global_free(mem_glob, acc_size);
1287 bo->destroy = destroy;
1289 kref_init(&bo->kref);
1290 kref_init(&bo->list_kref);
1291 atomic_set(&bo->cpu_writers, 0);
1292 atomic_set(&bo->reserved, 1);
1293 init_waitqueue_head(&bo->event_queue);
1294 INIT_LIST_HEAD(&bo->lru);
1295 INIT_LIST_HEAD(&bo->ddestroy);
1296 INIT_LIST_HEAD(&bo->swap);
1297 INIT_LIST_HEAD(&bo->io_reserve_lru);
1298 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/
1300 bo->glob = bdev->glob;
1302 bo->num_pages = num_pages;
1303 bo->mem.size = num_pages << PAGE_SHIFT;
1304 bo->mem.mem_type = TTM_PL_SYSTEM;
1305 bo->mem.num_pages = bo->num_pages;
1306 bo->mem.mm_node = NULL;
1307 bo->mem.page_alignment = page_alignment;
1308 bo->mem.bus.io_reserved_vm = false;
1309 bo->mem.bus.io_reserved_count = 0;
1311 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1312 bo->seq_valid = false;
1313 bo->persistent_swap_storage = persistent_swap_storage;
1314 bo->acc_size = acc_size;
1316 atomic_inc(&bo->glob->bo_count);
1319 * Mirror ref from kref_init() for list_kref.
1321 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags);
1323 ret = ttm_bo_check_placement(bo, placement);
1324 if (unlikely(ret != 0))
1328 * For ttm_bo_type_device buffers, allocate
1329 * address space from the device.
1331 if (bo->type == ttm_bo_type_device ||
1332 bo->type == ttm_bo_type_sg) {
1333 ret = ttm_bo_setup_vm(bo);
1338 ret = ttm_bo_validate(bo, placement, interruptible, false);
1342 ttm_bo_unreserve(bo);
1346 ttm_bo_unreserve(bo);
1351 EXPORT_SYMBOL(ttm_bo_init);
1353 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1354 unsigned long bo_size,
1355 unsigned struct_size)
1357 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1360 size += ttm_round_pot(struct_size);
1361 size += PAGE_ALIGN(npages * sizeof(void *));
1362 size += ttm_round_pot(sizeof(struct ttm_tt));
1365 EXPORT_SYMBOL(ttm_bo_acc_size);
1367 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1368 unsigned long bo_size,
1369 unsigned struct_size)
1371 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1374 size += ttm_round_pot(struct_size);
1375 size += PAGE_ALIGN(npages * sizeof(void *));
1376 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1377 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1380 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1382 int ttm_bo_create(struct ttm_bo_device *bdev,
1384 enum ttm_bo_type type,
1385 struct ttm_placement *placement,
1386 uint32_t page_alignment,
1388 struct vm_object *persistent_swap_storage,
1389 struct ttm_buffer_object **p_bo)
1391 struct ttm_buffer_object *bo;
1396 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO);
1397 if (unlikely(bo == NULL))
1400 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1401 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1402 interruptible, persistent_swap_storage, acc_size,
1404 if (likely(ret == 0))
1409 EXPORT_SYMBOL(ttm_bo_create);
1411 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1412 unsigned mem_type, bool allow_errors)
1414 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1415 struct ttm_bo_global *glob = bdev->glob;
1419 * Can't use standard list traversal since we're unlocking.
1422 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1423 while (!list_empty(&man->lru)) {
1424 lockmgr(&glob->lru_lock, LK_RELEASE);
1425 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1430 kprintf("[TTM] Cleanup eviction failed\n");
1433 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1435 lockmgr(&glob->lru_lock, LK_RELEASE);
1439 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1441 struct ttm_mem_type_manager *man;
1444 if (mem_type >= TTM_NUM_MEM_TYPES) {
1445 kprintf("[TTM] Illegal memory type %d\n", mem_type);
1448 man = &bdev->man[mem_type];
1450 if (!man->has_type) {
1451 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1456 man->use_type = false;
1457 man->has_type = false;
1461 ttm_bo_force_list_clean(bdev, mem_type, false);
1463 ret = (*man->func->takedown)(man);
1468 EXPORT_SYMBOL(ttm_bo_clean_mm);
1470 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1472 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1474 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1475 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1479 if (!man->has_type) {
1480 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type);
1484 return ttm_bo_force_list_clean(bdev, mem_type, true);
1486 EXPORT_SYMBOL(ttm_bo_evict_mm);
1488 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1489 unsigned long p_size)
1492 struct ttm_mem_type_manager *man;
1494 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1495 man = &bdev->man[type];
1496 BUG_ON(man->has_type);
1497 man->io_reserve_fastpath = true;
1498 man->use_io_reserve_lru = false;
1499 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE);
1500 INIT_LIST_HEAD(&man->io_reserve_lru);
1502 ret = bdev->driver->init_mem_type(bdev, type, man);
1508 if (type != TTM_PL_SYSTEM) {
1509 ret = (*man->func->init)(man, p_size);
1513 man->has_type = true;
1514 man->use_type = true;
1517 INIT_LIST_HEAD(&man->lru);
1521 EXPORT_SYMBOL(ttm_bo_init_mm);
1523 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1525 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1526 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1527 glob->dummy_read_page = NULL;
1529 vm_page_free(glob->dummy_read_page);
1533 void ttm_bo_global_release(struct drm_global_reference *ref)
1535 struct ttm_bo_global *glob = ref->object;
1537 if (refcount_release(&glob->kobj_ref))
1538 ttm_bo_global_kobj_release(glob);
1540 EXPORT_SYMBOL(ttm_bo_global_release);
1542 int ttm_bo_global_init(struct drm_global_reference *ref)
1544 struct ttm_bo_global_ref *bo_ref =
1545 container_of(ref, struct ttm_bo_global_ref, ref);
1546 struct ttm_bo_global *glob = ref->object;
1549 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE);
1550 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE);
1551 glob->mem_glob = bo_ref->mem_glob;
1552 glob->dummy_read_page = vm_page_alloc_contig(
1553 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE);
1555 if (unlikely(glob->dummy_read_page == NULL)) {
1560 INIT_LIST_HEAD(&glob->swap_lru);
1561 INIT_LIST_HEAD(&glob->device_list);
1563 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1564 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1565 if (unlikely(ret != 0)) {
1566 kprintf("[TTM] Could not register buffer object swapout\n");
1570 atomic_set(&glob->bo_count, 0);
1572 refcount_init(&glob->kobj_ref, 1);
1576 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE);
1577 glob->dummy_read_page = NULL;
1579 vm_page_free(glob->dummy_read_page);
1585 EXPORT_SYMBOL(ttm_bo_global_init);
1588 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1591 unsigned i = TTM_NUM_MEM_TYPES;
1592 struct ttm_mem_type_manager *man;
1593 struct ttm_bo_global *glob = bdev->glob;
1596 man = &bdev->man[i];
1597 if (man->has_type) {
1598 man->use_type = false;
1599 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1601 kprintf("[TTM] DRM memory manager type %d is not clean\n",
1604 man->has_type = false;
1608 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1609 list_del(&bdev->device_list);
1610 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1612 cancel_delayed_work_sync(&bdev->wq);
1614 while (ttm_bo_delayed_delete(bdev, true))
1617 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1618 if (list_empty(&bdev->ddestroy))
1619 TTM_DEBUG("Delayed destroy list was clean\n");
1621 if (list_empty(&bdev->man[0].lru))
1622 TTM_DEBUG("Swap list was clean\n");
1623 lockmgr(&glob->lru_lock, LK_RELEASE);
1625 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1626 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1627 drm_mm_takedown(&bdev->addr_space_mm);
1628 lockmgr(&bdev->vm_lock, LK_RELEASE);
1632 EXPORT_SYMBOL(ttm_bo_device_release);
1634 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1635 struct ttm_bo_global *glob,
1636 struct ttm_bo_driver *driver,
1637 uint64_t file_page_offset,
1642 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE);
1643 bdev->driver = driver;
1645 memset(bdev->man, 0, sizeof(bdev->man));
1648 * Initialize the system memory buffer type.
1649 * Other types need to be driver / IOCTL initialized.
1651 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1652 if (unlikely(ret != 0))
1655 RB_INIT(&bdev->addr_space_rb);
1656 drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1658 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1659 INIT_LIST_HEAD(&bdev->ddestroy);
1660 bdev->dev_mapping = NULL;
1662 bdev->need_dma32 = need_dma32;
1664 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE);
1665 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE);
1666 list_add_tail(&bdev->device_list, &glob->device_list);
1667 lockmgr(&glob->device_list_mutex, LK_RELEASE);
1673 EXPORT_SYMBOL(ttm_bo_device_init);
1676 * buffer object vm functions.
1679 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1681 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1683 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1684 if (mem->mem_type == TTM_PL_SYSTEM)
1687 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1690 if (mem->placement & TTM_PL_FLAG_CACHED)
1696 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1699 ttm_bo_release_mmap(bo);
1700 ttm_mem_io_free_vm(bo);
1703 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1705 struct ttm_bo_device *bdev = bo->bdev;
1706 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1708 ttm_mem_io_lock(man, false);
1709 ttm_bo_unmap_virtual_locked(bo);
1710 ttm_mem_io_unlock(man);
1714 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1716 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1718 struct ttm_bo_device *bdev = bo->bdev;
1720 /* The caller acquired bdev->vm_lock. */
1721 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1727 * @bo: the buffer to allocate address space for
1729 * Allocate address space in the drm device so that applications
1730 * can mmap the buffer and access the contents. This only
1731 * applies to ttm_bo_type_device objects as others are not
1732 * placed in the drm device address space.
1735 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1737 struct ttm_bo_device *bdev = bo->bdev;
1741 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1742 if (unlikely(ret != 0))
1745 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE);
1746 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1747 bo->mem.num_pages, 0, 0);
1749 if (unlikely(bo->vm_node == NULL)) {
1754 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1755 bo->mem.num_pages, 0);
1757 if (unlikely(bo->vm_node == NULL)) {
1758 lockmgr(&bdev->vm_lock, LK_RELEASE);
1762 ttm_bo_vm_insert_rb(bo);
1763 lockmgr(&bdev->vm_lock, LK_RELEASE);
1764 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1768 lockmgr(&bdev->vm_lock, LK_RELEASE);
1772 int ttm_bo_wait(struct ttm_buffer_object *bo,
1773 bool lazy, bool interruptible, bool no_wait)
1775 struct ttm_bo_driver *driver = bo->bdev->driver;
1776 struct ttm_bo_device *bdev = bo->bdev;
1780 if (likely(bo->sync_obj == NULL))
1783 while (bo->sync_obj) {
1785 if (driver->sync_obj_signaled(bo->sync_obj)) {
1786 void *tmp_obj = bo->sync_obj;
1787 bo->sync_obj = NULL;
1788 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1789 lockmgr(&bdev->fence_lock, LK_RELEASE);
1790 driver->sync_obj_unref(&tmp_obj);
1791 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1798 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1799 lockmgr(&bdev->fence_lock, LK_RELEASE);
1800 ret = driver->sync_obj_wait(sync_obj,
1801 lazy, interruptible);
1802 if (unlikely(ret != 0)) {
1803 driver->sync_obj_unref(&sync_obj);
1804 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1807 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1808 if (likely(bo->sync_obj == sync_obj)) {
1809 void *tmp_obj = bo->sync_obj;
1810 bo->sync_obj = NULL;
1811 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1813 lockmgr(&bdev->fence_lock, LK_RELEASE);
1814 driver->sync_obj_unref(&sync_obj);
1815 driver->sync_obj_unref(&tmp_obj);
1816 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1818 lockmgr(&bdev->fence_lock, LK_RELEASE);
1819 driver->sync_obj_unref(&sync_obj);
1820 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1825 EXPORT_SYMBOL(ttm_bo_wait);
1827 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1829 struct ttm_bo_device *bdev = bo->bdev;
1833 * Using ttm_bo_reserve makes sure the lru lists are updated.
1836 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1837 if (unlikely(ret != 0))
1839 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
1840 ret = ttm_bo_wait(bo, false, true, no_wait);
1841 lockmgr(&bdev->fence_lock, LK_RELEASE);
1842 if (likely(ret == 0))
1843 atomic_inc(&bo->cpu_writers);
1844 ttm_bo_unreserve(bo);
1847 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1849 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1851 atomic_dec(&bo->cpu_writers);
1853 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1856 * A buffer object shrink method that tries to swap out the first
1857 * buffer object on the bo_global::swap_lru list.
1860 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1862 struct ttm_bo_global *glob =
1863 container_of(shrink, struct ttm_bo_global, shrink);
1864 struct ttm_buffer_object *bo;
1867 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1869 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1870 list_for_each_entry(bo, &glob->swap_lru, swap) {
1871 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1877 lockmgr(&glob->lru_lock, LK_RELEASE);
1881 kref_get(&bo->list_kref);
1883 if (!list_empty(&bo->ddestroy)) {
1884 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1885 kref_put(&bo->list_kref, ttm_bo_release_list);
1889 put_count = ttm_bo_del_from_lru(bo);
1890 lockmgr(&glob->lru_lock, LK_RELEASE);
1892 ttm_bo_list_ref_sub(bo, put_count, true);
1895 * Wait for GPU, then move to system cached.
1898 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE);
1899 ret = ttm_bo_wait(bo, false, false, false);
1900 lockmgr(&bo->bdev->fence_lock, LK_RELEASE);
1902 if (unlikely(ret != 0))
1905 if ((bo->mem.placement & swap_placement) != swap_placement) {
1906 struct ttm_mem_reg evict_mem;
1908 evict_mem = bo->mem;
1909 evict_mem.mm_node = NULL;
1910 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1911 evict_mem.mem_type = TTM_PL_SYSTEM;
1913 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1915 if (unlikely(ret != 0))
1919 ttm_bo_unmap_virtual(bo);
1922 * Swap out. Buffer will be swapped in again as soon as
1923 * anyone tries to access a ttm page.
1926 if (bo->bdev->driver->swap_notify)
1927 bo->bdev->driver->swap_notify(bo);
1929 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1934 * Unreserve without putting on LRU to avoid swapping out an
1935 * already swapped buffer.
1938 ttm_bo_unreserve_core(bo);
1939 kref_put(&bo->list_kref, ttm_bo_release_list);
1943 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1945 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1948 EXPORT_SYMBOL(ttm_bo_swapout_all);