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 #include "ttm/ttm_module.h"
32 #include "ttm/ttm_bo_driver.h"
33 #include "ttm/ttm_placement.h"
34 #include <linux/jiffies.h>
35 #include <linux/slab.h>
36 #include <linux/sched.h>
38 #include <linux/file.h>
39 #include <linux/module.h>
40 #include <linux/atomic.h>
42 #define TTM_ASSERT_LOCKED(param)
43 #define TTM_DEBUG(fmt, arg...)
44 #define TTM_BO_HASH_ORDER 13
46 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
47 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
48 static void ttm_bo_global_kobj_release(struct kobject *kobj);
50 static struct attribute ttm_bo_count = {
55 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
59 for (i = 0; i <= TTM_PL_PRIV5; i++)
60 if (flags & (1 << i)) {
67 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
69 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
71 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
72 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
73 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
74 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
75 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
76 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
77 man->available_caching);
78 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
79 man->default_caching);
80 if (mem_type != TTM_PL_SYSTEM)
81 (*man->func->debug)(man, TTM_PFX);
84 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
85 struct ttm_placement *placement)
89 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
90 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 for (i = 0; i < placement->num_placement; i++) {
93 ret = ttm_mem_type_from_flags(placement->placement[i],
97 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
98 i, placement->placement[i], mem_type);
99 ttm_mem_type_debug(bo->bdev, mem_type);
103 static ssize_t ttm_bo_global_show(struct kobject *kobj,
104 struct attribute *attr,
107 struct ttm_bo_global *glob =
108 container_of(kobj, struct ttm_bo_global, kobj);
110 return snprintf(buffer, PAGE_SIZE, "%lu\n",
111 (unsigned long) atomic_read(&glob->bo_count));
114 static struct attribute *ttm_bo_global_attrs[] = {
119 static const struct sysfs_ops ttm_bo_global_ops = {
120 .show = &ttm_bo_global_show
123 static struct kobj_type ttm_bo_glob_kobj_type = {
124 .release = &ttm_bo_global_kobj_release,
125 .sysfs_ops = &ttm_bo_global_ops,
126 .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 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
164 return wait_event_interruptible(bo->event_queue,
165 atomic_read(&bo->reserved) == 0);
167 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
171 EXPORT_SYMBOL(ttm_bo_wait_unreserved);
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(!atomic_read(&bo->reserved));
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_locked(struct ttm_buffer_object *bo,
218 bool no_wait, bool use_sequence, uint32_t sequence)
220 struct ttm_bo_global *glob = bo->glob;
223 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
225 * Deadlock avoidance for multi-bo reserving.
227 if (use_sequence && bo->seq_valid) {
229 * We've already reserved this one.
231 if (unlikely(sequence == 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(sequence - bo->val_seq < (1 << 31)))
244 spin_unlock(&glob->lru_lock);
245 ret = ttm_bo_wait_unreserved(bo, interruptible);
246 spin_lock(&glob->lru_lock);
254 * Wake up waiters that may need to recheck for deadlock,
255 * if we decreased the sequence number.
257 if (unlikely((bo->val_seq - sequence < (1 << 31))
259 wake_up_all(&bo->event_queue);
261 bo->val_seq = sequence;
262 bo->seq_valid = true;
264 bo->seq_valid = false;
269 EXPORT_SYMBOL(ttm_bo_reserve);
271 static void ttm_bo_ref_bug(struct kref *list_kref)
276 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
279 kref_sub(&bo->list_kref, count,
280 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
283 int ttm_bo_reserve(struct ttm_buffer_object *bo,
285 bool no_wait, bool use_sequence, uint32_t sequence)
287 struct ttm_bo_global *glob = bo->glob;
291 spin_lock(&glob->lru_lock);
292 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
294 if (likely(ret == 0))
295 put_count = ttm_bo_del_from_lru(bo);
296 spin_unlock(&glob->lru_lock);
298 ttm_bo_list_ref_sub(bo, put_count, true);
303 void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
305 ttm_bo_add_to_lru(bo);
306 atomic_set(&bo->reserved, 0);
307 wake_up_all(&bo->event_queue);
310 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
312 struct ttm_bo_global *glob = bo->glob;
314 spin_lock(&glob->lru_lock);
315 ttm_bo_unreserve_locked(bo);
316 spin_unlock(&glob->lru_lock);
318 EXPORT_SYMBOL(ttm_bo_unreserve);
321 * Call bo->mutex locked.
323 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
325 struct ttm_bo_device *bdev = bo->bdev;
326 struct ttm_bo_global *glob = bo->glob;
328 uint32_t page_flags = 0;
330 TTM_ASSERT_LOCKED(&bo->mutex);
333 if (bdev->need_dma32)
334 page_flags |= TTM_PAGE_FLAG_DMA32;
337 case ttm_bo_type_device:
339 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
340 case ttm_bo_type_kernel:
341 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
342 page_flags, glob->dummy_read_page);
343 if (unlikely(bo->ttm == NULL))
347 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
355 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
356 struct ttm_mem_reg *mem,
357 bool evict, bool interruptible,
358 bool no_wait_reserve, bool no_wait_gpu)
360 struct ttm_bo_device *bdev = bo->bdev;
361 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
362 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
363 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
364 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
367 if (old_is_pci || new_is_pci ||
368 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
369 ret = ttm_mem_io_lock(old_man, true);
370 if (unlikely(ret != 0))
372 ttm_bo_unmap_virtual_locked(bo);
373 ttm_mem_io_unlock(old_man);
377 * Create and bind a ttm if required.
380 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
381 if (bo->ttm == NULL) {
382 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
383 ret = ttm_bo_add_ttm(bo, zero);
388 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
392 if (mem->mem_type != TTM_PL_SYSTEM) {
393 ret = ttm_tt_bind(bo->ttm, mem);
398 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
399 if (bdev->driver->move_notify)
400 bdev->driver->move_notify(bo, mem);
407 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
408 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
409 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
410 else if (bdev->driver->move)
411 ret = bdev->driver->move(bo, evict, interruptible,
412 no_wait_reserve, no_wait_gpu, mem);
414 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
419 if (bdev->driver->move_notify)
420 bdev->driver->move_notify(bo, mem);
424 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
426 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
430 if (bo->mem.mm_node) {
431 bo->offset = (bo->mem.start << PAGE_SHIFT) +
432 bdev->man[bo->mem.mem_type].gpu_offset;
433 bo->cur_placement = bo->mem.placement;
440 new_man = &bdev->man[bo->mem.mem_type];
441 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
442 ttm_tt_unbind(bo->ttm);
443 ttm_tt_destroy(bo->ttm);
452 * Will release GPU memory type usage on destruction.
453 * This is the place to put in driver specific hooks to release
454 * driver private resources.
455 * Will release the bo::reserved lock.
458 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
460 if (bo->bdev->driver->move_notify)
461 bo->bdev->driver->move_notify(bo, NULL);
464 ttm_tt_unbind(bo->ttm);
465 ttm_tt_destroy(bo->ttm);
468 ttm_bo_mem_put(bo, &bo->mem);
470 atomic_set(&bo->reserved, 0);
473 * Make processes trying to reserve really pick it up.
475 smp_mb__after_atomic_dec();
476 wake_up_all(&bo->event_queue);
479 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
481 struct ttm_bo_device *bdev = bo->bdev;
482 struct ttm_bo_global *glob = bo->glob;
483 struct ttm_bo_driver *driver;
484 void *sync_obj = NULL;
489 spin_lock(&bdev->fence_lock);
490 (void) ttm_bo_wait(bo, false, false, true);
493 spin_lock(&glob->lru_lock);
496 * Lock inversion between bo:reserve and bdev::fence_lock here,
497 * but that's OK, since we're only trylocking.
500 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
502 if (unlikely(ret == -EBUSY))
505 spin_unlock(&bdev->fence_lock);
506 put_count = ttm_bo_del_from_lru(bo);
508 spin_unlock(&glob->lru_lock);
509 ttm_bo_cleanup_memtype_use(bo);
511 ttm_bo_list_ref_sub(bo, put_count, true);
515 spin_lock(&glob->lru_lock);
518 driver = bdev->driver;
520 sync_obj = driver->sync_obj_ref(bo->sync_obj);
521 sync_obj_arg = bo->sync_obj_arg;
523 kref_get(&bo->list_kref);
524 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
525 spin_unlock(&glob->lru_lock);
526 spin_unlock(&bdev->fence_lock);
529 driver->sync_obj_flush(sync_obj, sync_obj_arg);
530 driver->sync_obj_unref(&sync_obj);
532 schedule_delayed_work(&bdev->wq,
533 ((HZ / 100) < 1) ? 1 : HZ / 100);
537 * function ttm_bo_cleanup_refs
538 * If bo idle, remove from delayed- and lru lists, and unref.
539 * If not idle, do nothing.
541 * @interruptible Any sleeps should occur interruptibly.
542 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
543 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
546 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
548 bool no_wait_reserve,
551 struct ttm_bo_device *bdev = bo->bdev;
552 struct ttm_bo_global *glob = bo->glob;
557 spin_lock(&bdev->fence_lock);
558 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
559 spin_unlock(&bdev->fence_lock);
561 if (unlikely(ret != 0))
564 spin_lock(&glob->lru_lock);
566 if (unlikely(list_empty(&bo->ddestroy))) {
567 spin_unlock(&glob->lru_lock);
571 ret = ttm_bo_reserve_locked(bo, interruptible,
572 no_wait_reserve, false, 0);
574 if (unlikely(ret != 0)) {
575 spin_unlock(&glob->lru_lock);
580 * We can re-check for sync object without taking
581 * the bo::lock since setting the sync object requires
582 * also bo::reserved. A busy object at this point may
583 * be caused by another thread recently starting an accelerated
587 if (unlikely(bo->sync_obj)) {
588 atomic_set(&bo->reserved, 0);
589 wake_up_all(&bo->event_queue);
590 spin_unlock(&glob->lru_lock);
594 put_count = ttm_bo_del_from_lru(bo);
595 list_del_init(&bo->ddestroy);
598 spin_unlock(&glob->lru_lock);
599 ttm_bo_cleanup_memtype_use(bo);
601 ttm_bo_list_ref_sub(bo, put_count, true);
607 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
608 * encountered buffers.
611 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
613 struct ttm_bo_global *glob = bdev->glob;
614 struct ttm_buffer_object *entry = NULL;
617 spin_lock(&glob->lru_lock);
618 if (list_empty(&bdev->ddestroy))
621 entry = list_first_entry(&bdev->ddestroy,
622 struct ttm_buffer_object, ddestroy);
623 kref_get(&entry->list_kref);
626 struct ttm_buffer_object *nentry = NULL;
628 if (entry->ddestroy.next != &bdev->ddestroy) {
629 nentry = list_first_entry(&entry->ddestroy,
630 struct ttm_buffer_object, ddestroy);
631 kref_get(&nentry->list_kref);
634 spin_unlock(&glob->lru_lock);
635 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
637 kref_put(&entry->list_kref, ttm_bo_release_list);
643 spin_lock(&glob->lru_lock);
644 if (list_empty(&entry->ddestroy))
649 spin_unlock(&glob->lru_lock);
652 kref_put(&entry->list_kref, ttm_bo_release_list);
656 static void ttm_bo_delayed_workqueue(struct work_struct *work)
658 struct ttm_bo_device *bdev =
659 container_of(work, struct ttm_bo_device, wq.work);
661 if (ttm_bo_delayed_delete(bdev, false)) {
662 schedule_delayed_work(&bdev->wq,
663 ((HZ / 100) < 1) ? 1 : HZ / 100);
667 static void ttm_bo_release(struct kref *kref)
669 struct ttm_buffer_object *bo =
670 container_of(kref, struct ttm_buffer_object, kref);
671 struct ttm_bo_device *bdev = bo->bdev;
672 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
674 if (likely(bo->vm_node != NULL)) {
675 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
676 drm_mm_put_block(bo->vm_node);
679 write_unlock(&bdev->vm_lock);
680 ttm_mem_io_lock(man, false);
681 ttm_mem_io_free_vm(bo);
682 ttm_mem_io_unlock(man);
683 ttm_bo_cleanup_refs_or_queue(bo);
684 kref_put(&bo->list_kref, ttm_bo_release_list);
685 write_lock(&bdev->vm_lock);
688 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
690 struct ttm_buffer_object *bo = *p_bo;
691 struct ttm_bo_device *bdev = bo->bdev;
694 write_lock(&bdev->vm_lock);
695 kref_put(&bo->kref, ttm_bo_release);
696 write_unlock(&bdev->vm_lock);
698 EXPORT_SYMBOL(ttm_bo_unref);
700 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
702 return cancel_delayed_work_sync(&bdev->wq);
704 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
706 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
709 schedule_delayed_work(&bdev->wq,
710 ((HZ / 100) < 1) ? 1 : HZ / 100);
712 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
714 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
715 bool no_wait_reserve, bool no_wait_gpu)
717 struct ttm_bo_device *bdev = bo->bdev;
718 struct ttm_mem_reg evict_mem;
719 struct ttm_placement placement;
722 spin_lock(&bdev->fence_lock);
723 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
724 spin_unlock(&bdev->fence_lock);
726 if (unlikely(ret != 0)) {
727 if (ret != -ERESTARTSYS) {
728 printk(KERN_ERR TTM_PFX
729 "Failed to expire sync object before "
730 "buffer eviction.\n");
735 BUG_ON(!atomic_read(&bo->reserved));
738 evict_mem.mm_node = NULL;
739 evict_mem.bus.io_reserved_vm = false;
740 evict_mem.bus.io_reserved_count = 0;
744 placement.num_placement = 0;
745 placement.num_busy_placement = 0;
746 bdev->driver->evict_flags(bo, &placement);
747 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
748 no_wait_reserve, no_wait_gpu);
750 if (ret != -ERESTARTSYS) {
751 printk(KERN_ERR TTM_PFX
752 "Failed to find memory space for "
753 "buffer 0x%p eviction.\n", bo);
754 ttm_bo_mem_space_debug(bo, &placement);
759 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
760 no_wait_reserve, no_wait_gpu);
762 if (ret != -ERESTARTSYS)
763 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
764 ttm_bo_mem_put(bo, &evict_mem);
772 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
774 bool interruptible, bool no_wait_reserve,
777 struct ttm_bo_global *glob = bdev->glob;
778 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
779 struct ttm_buffer_object *bo;
780 int ret, put_count = 0;
783 spin_lock(&glob->lru_lock);
784 if (list_empty(&man->lru)) {
785 spin_unlock(&glob->lru_lock);
789 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
790 kref_get(&bo->list_kref);
792 if (!list_empty(&bo->ddestroy)) {
793 spin_unlock(&glob->lru_lock);
794 ret = ttm_bo_cleanup_refs(bo, interruptible,
795 no_wait_reserve, no_wait_gpu);
796 kref_put(&bo->list_kref, ttm_bo_release_list);
798 if (likely(ret == 0 || ret == -ERESTARTSYS))
804 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
806 if (unlikely(ret == -EBUSY)) {
807 spin_unlock(&glob->lru_lock);
808 if (likely(!no_wait_gpu))
809 ret = ttm_bo_wait_unreserved(bo, interruptible);
811 kref_put(&bo->list_kref, ttm_bo_release_list);
814 * We *need* to retry after releasing the lru lock.
817 if (unlikely(ret != 0))
822 put_count = ttm_bo_del_from_lru(bo);
823 spin_unlock(&glob->lru_lock);
827 ttm_bo_list_ref_sub(bo, put_count, true);
829 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
830 ttm_bo_unreserve(bo);
832 kref_put(&bo->list_kref, ttm_bo_release_list);
836 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
838 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
841 (*man->func->put_node)(man, mem);
843 EXPORT_SYMBOL(ttm_bo_mem_put);
846 * Repeatedly evict memory from the LRU for @mem_type until we create enough
847 * space, or we've evicted everything and there isn't enough space.
849 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
851 struct ttm_placement *placement,
852 struct ttm_mem_reg *mem,
854 bool no_wait_reserve,
857 struct ttm_bo_device *bdev = bo->bdev;
858 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
862 ret = (*man->func->get_node)(man, bo, placement, mem);
863 if (unlikely(ret != 0))
867 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
868 no_wait_reserve, no_wait_gpu);
869 if (unlikely(ret != 0))
872 if (mem->mm_node == NULL)
874 mem->mem_type = mem_type;
878 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
879 uint32_t cur_placement,
880 uint32_t proposed_placement)
882 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
883 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
886 * Keep current caching if possible.
889 if ((cur_placement & caching) != 0)
890 result |= (cur_placement & caching);
891 else if ((man->default_caching & caching) != 0)
892 result |= man->default_caching;
893 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
894 result |= TTM_PL_FLAG_CACHED;
895 else if ((TTM_PL_FLAG_WC & caching) != 0)
896 result |= TTM_PL_FLAG_WC;
897 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
898 result |= TTM_PL_FLAG_UNCACHED;
903 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
905 uint32_t proposed_placement,
906 uint32_t *masked_placement)
908 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
910 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
913 if ((proposed_placement & man->available_caching) == 0)
916 cur_flags |= (proposed_placement & man->available_caching);
918 *masked_placement = cur_flags;
923 * Creates space for memory region @mem according to its type.
925 * This function first searches for free space in compatible memory types in
926 * the priority order defined by the driver. If free space isn't found, then
927 * ttm_bo_mem_force_space is attempted in priority order to evict and find
930 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
931 struct ttm_placement *placement,
932 struct ttm_mem_reg *mem,
933 bool interruptible, bool no_wait_reserve,
936 struct ttm_bo_device *bdev = bo->bdev;
937 struct ttm_mem_type_manager *man;
938 uint32_t mem_type = TTM_PL_SYSTEM;
939 uint32_t cur_flags = 0;
940 bool type_found = false;
941 bool type_ok = false;
942 bool has_erestartsys = false;
946 for (i = 0; i < placement->num_placement; ++i) {
947 ret = ttm_mem_type_from_flags(placement->placement[i],
951 man = &bdev->man[mem_type];
953 type_ok = ttm_bo_mt_compatible(man,
955 placement->placement[i],
961 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
964 * Use the access and other non-mapping-related flag bits from
965 * the memory placement flags to the current flags
967 ttm_flag_masked(&cur_flags, placement->placement[i],
968 ~TTM_PL_MASK_MEMTYPE);
970 if (mem_type == TTM_PL_SYSTEM)
973 if (man->has_type && man->use_type) {
975 ret = (*man->func->get_node)(man, bo, placement, mem);
983 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
984 mem->mem_type = mem_type;
985 mem->placement = cur_flags;
992 for (i = 0; i < placement->num_busy_placement; ++i) {
993 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
997 man = &bdev->man[mem_type];
1000 if (!ttm_bo_mt_compatible(man,
1002 placement->busy_placement[i],
1006 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1009 * Use the access and other non-mapping-related flag bits from
1010 * the memory placement flags to the current flags
1012 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1013 ~TTM_PL_MASK_MEMTYPE);
1016 if (mem_type == TTM_PL_SYSTEM) {
1017 mem->mem_type = mem_type;
1018 mem->placement = cur_flags;
1019 mem->mm_node = NULL;
1023 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1024 interruptible, no_wait_reserve, no_wait_gpu);
1025 if (ret == 0 && mem->mm_node) {
1026 mem->placement = cur_flags;
1029 if (ret == -ERESTARTSYS)
1030 has_erestartsys = true;
1032 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1035 EXPORT_SYMBOL(ttm_bo_mem_space);
1037 int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1039 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1042 return wait_event_interruptible(bo->event_queue,
1043 atomic_read(&bo->cpu_writers) == 0);
1045 EXPORT_SYMBOL(ttm_bo_wait_cpu);
1047 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1048 struct ttm_placement *placement,
1049 bool interruptible, bool no_wait_reserve,
1053 struct ttm_mem_reg mem;
1054 struct ttm_bo_device *bdev = bo->bdev;
1056 BUG_ON(!atomic_read(&bo->reserved));
1059 * FIXME: It's possible to pipeline buffer moves.
1060 * Have the driver move function wait for idle when necessary,
1061 * instead of doing it here.
1063 spin_lock(&bdev->fence_lock);
1064 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1065 spin_unlock(&bdev->fence_lock);
1068 mem.num_pages = bo->num_pages;
1069 mem.size = mem.num_pages << PAGE_SHIFT;
1070 mem.page_alignment = bo->mem.page_alignment;
1071 mem.bus.io_reserved_vm = false;
1072 mem.bus.io_reserved_count = 0;
1074 * Determine where to move the buffer.
1076 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
1079 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
1081 if (ret && mem.mm_node)
1082 ttm_bo_mem_put(bo, &mem);
1086 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1087 struct ttm_mem_reg *mem)
1091 if (mem->mm_node && placement->lpfn != 0 &&
1092 (mem->start < placement->fpfn ||
1093 mem->start + mem->num_pages > placement->lpfn))
1096 for (i = 0; i < placement->num_placement; i++) {
1097 if ((placement->placement[i] & mem->placement &
1098 TTM_PL_MASK_CACHING) &&
1099 (placement->placement[i] & mem->placement &
1106 int ttm_bo_validate(struct ttm_buffer_object *bo,
1107 struct ttm_placement *placement,
1108 bool interruptible, bool no_wait_reserve,
1113 BUG_ON(!atomic_read(&bo->reserved));
1114 /* Check that range is valid */
1115 if (placement->lpfn || placement->fpfn)
1116 if (placement->fpfn > placement->lpfn ||
1117 (placement->lpfn - placement->fpfn) < bo->num_pages)
1120 * Check whether we need to move buffer.
1122 ret = ttm_bo_mem_compat(placement, &bo->mem);
1124 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
1129 * Use the access and other non-mapping-related flag bits from
1130 * the compatible memory placement flags to the active flags
1132 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1133 ~TTM_PL_MASK_MEMTYPE);
1136 * We might need to add a TTM.
1138 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1139 ret = ttm_bo_add_ttm(bo, true);
1145 EXPORT_SYMBOL(ttm_bo_validate);
1147 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1148 struct ttm_placement *placement)
1150 BUG_ON((placement->fpfn || placement->lpfn) &&
1151 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
1156 int ttm_bo_init(struct ttm_bo_device *bdev,
1157 struct ttm_buffer_object *bo,
1159 enum ttm_bo_type type,
1160 struct ttm_placement *placement,
1161 uint32_t page_alignment,
1162 unsigned long buffer_start,
1164 struct file *persistent_swap_storage,
1166 void (*destroy) (struct ttm_buffer_object *))
1169 unsigned long num_pages;
1170 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1172 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1174 printk(KERN_ERR TTM_PFX "Out of kernel memory.\n");
1182 size += buffer_start & ~PAGE_MASK;
1183 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1184 if (num_pages == 0) {
1185 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1192 bo->destroy = destroy;
1194 kref_init(&bo->kref);
1195 kref_init(&bo->list_kref);
1196 atomic_set(&bo->cpu_writers, 0);
1197 atomic_set(&bo->reserved, 1);
1198 init_waitqueue_head(&bo->event_queue);
1199 INIT_LIST_HEAD(&bo->lru);
1200 INIT_LIST_HEAD(&bo->ddestroy);
1201 INIT_LIST_HEAD(&bo->swap);
1202 INIT_LIST_HEAD(&bo->io_reserve_lru);
1204 bo->glob = bdev->glob;
1206 bo->num_pages = num_pages;
1207 bo->mem.size = num_pages << PAGE_SHIFT;
1208 bo->mem.mem_type = TTM_PL_SYSTEM;
1209 bo->mem.num_pages = bo->num_pages;
1210 bo->mem.mm_node = NULL;
1211 bo->mem.page_alignment = page_alignment;
1212 bo->mem.bus.io_reserved_vm = false;
1213 bo->mem.bus.io_reserved_count = 0;
1214 bo->buffer_start = buffer_start & PAGE_MASK;
1216 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1217 bo->seq_valid = false;
1218 bo->persistent_swap_storage = persistent_swap_storage;
1219 bo->acc_size = acc_size;
1220 atomic_inc(&bo->glob->bo_count);
1222 ret = ttm_bo_check_placement(bo, placement);
1223 if (unlikely(ret != 0))
1227 * For ttm_bo_type_device buffers, allocate
1228 * address space from the device.
1230 if (bo->type == ttm_bo_type_device) {
1231 ret = ttm_bo_setup_vm(bo);
1236 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
1240 ttm_bo_unreserve(bo);
1244 ttm_bo_unreserve(bo);
1249 EXPORT_SYMBOL(ttm_bo_init);
1251 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1252 unsigned long bo_size,
1253 unsigned struct_size)
1255 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1258 size += ttm_round_pot(struct_size);
1259 size += PAGE_ALIGN(npages * sizeof(void *));
1260 size += ttm_round_pot(sizeof(struct ttm_tt));
1263 EXPORT_SYMBOL(ttm_bo_acc_size);
1265 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1266 unsigned long bo_size,
1267 unsigned struct_size)
1269 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1272 size += ttm_round_pot(struct_size);
1273 size += PAGE_ALIGN(npages * sizeof(void *));
1274 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1275 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1278 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1280 int ttm_bo_create(struct ttm_bo_device *bdev,
1282 enum ttm_bo_type type,
1283 struct ttm_placement *placement,
1284 uint32_t page_alignment,
1285 unsigned long buffer_start,
1287 struct file *persistent_swap_storage,
1288 struct ttm_buffer_object **p_bo)
1290 struct ttm_buffer_object *bo;
1291 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1295 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1296 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1297 if (unlikely(ret != 0))
1300 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1302 if (unlikely(bo == NULL)) {
1303 ttm_mem_global_free(mem_glob, acc_size);
1307 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1308 buffer_start, interruptible,
1309 persistent_swap_storage, acc_size, NULL);
1310 if (likely(ret == 0))
1315 EXPORT_SYMBOL(ttm_bo_create);
1317 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1318 unsigned mem_type, bool allow_errors)
1320 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1321 struct ttm_bo_global *glob = bdev->glob;
1325 * Can't use standard list traversal since we're unlocking.
1328 spin_lock(&glob->lru_lock);
1329 while (!list_empty(&man->lru)) {
1330 spin_unlock(&glob->lru_lock);
1331 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
1336 printk(KERN_ERR TTM_PFX
1337 "Cleanup eviction failed\n");
1340 spin_lock(&glob->lru_lock);
1342 spin_unlock(&glob->lru_lock);
1346 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1348 struct ttm_mem_type_manager *man;
1351 if (mem_type >= TTM_NUM_MEM_TYPES) {
1352 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1355 man = &bdev->man[mem_type];
1357 if (!man->has_type) {
1358 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1359 "memory manager type %u\n", mem_type);
1363 man->use_type = false;
1364 man->has_type = false;
1368 ttm_bo_force_list_clean(bdev, mem_type, false);
1370 ret = (*man->func->takedown)(man);
1375 EXPORT_SYMBOL(ttm_bo_clean_mm);
1377 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1379 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1381 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1382 printk(KERN_ERR TTM_PFX
1383 "Illegal memory manager memory type %u.\n",
1388 if (!man->has_type) {
1389 printk(KERN_ERR TTM_PFX
1390 "Memory type %u has not been initialized.\n",
1395 return ttm_bo_force_list_clean(bdev, mem_type, true);
1397 EXPORT_SYMBOL(ttm_bo_evict_mm);
1399 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1400 unsigned long p_size)
1403 struct ttm_mem_type_manager *man;
1405 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1406 man = &bdev->man[type];
1407 BUG_ON(man->has_type);
1408 man->io_reserve_fastpath = true;
1409 man->use_io_reserve_lru = false;
1410 mutex_init(&man->io_reserve_mutex);
1411 INIT_LIST_HEAD(&man->io_reserve_lru);
1413 ret = bdev->driver->init_mem_type(bdev, type, man);
1419 if (type != TTM_PL_SYSTEM) {
1420 ret = (*man->func->init)(man, p_size);
1424 man->has_type = true;
1425 man->use_type = true;
1428 INIT_LIST_HEAD(&man->lru);
1432 EXPORT_SYMBOL(ttm_bo_init_mm);
1434 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1436 struct ttm_bo_global *glob =
1437 container_of(kobj, struct ttm_bo_global, kobj);
1439 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1440 __free_page(glob->dummy_read_page);
1444 void ttm_bo_global_release(struct drm_global_reference *ref)
1446 struct ttm_bo_global *glob = ref->object;
1448 kobject_del(&glob->kobj);
1449 kobject_put(&glob->kobj);
1451 EXPORT_SYMBOL(ttm_bo_global_release);
1453 int ttm_bo_global_init(struct drm_global_reference *ref)
1455 struct ttm_bo_global_ref *bo_ref =
1456 container_of(ref, struct ttm_bo_global_ref, ref);
1457 struct ttm_bo_global *glob = ref->object;
1460 mutex_init(&glob->device_list_mutex);
1461 spin_lock_init(&glob->lru_lock);
1462 glob->mem_glob = bo_ref->mem_glob;
1463 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1465 if (unlikely(glob->dummy_read_page == NULL)) {
1470 INIT_LIST_HEAD(&glob->swap_lru);
1471 INIT_LIST_HEAD(&glob->device_list);
1473 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1474 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1475 if (unlikely(ret != 0)) {
1476 printk(KERN_ERR TTM_PFX
1477 "Could not register buffer object swapout.\n");
1481 atomic_set(&glob->bo_count, 0);
1483 ret = kobject_init_and_add(
1484 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1485 if (unlikely(ret != 0))
1486 kobject_put(&glob->kobj);
1489 __free_page(glob->dummy_read_page);
1494 EXPORT_SYMBOL(ttm_bo_global_init);
1497 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1500 unsigned i = TTM_NUM_MEM_TYPES;
1501 struct ttm_mem_type_manager *man;
1502 struct ttm_bo_global *glob = bdev->glob;
1505 man = &bdev->man[i];
1506 if (man->has_type) {
1507 man->use_type = false;
1508 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1510 printk(KERN_ERR TTM_PFX
1511 "DRM memory manager type %d "
1512 "is not clean.\n", i);
1514 man->has_type = false;
1518 mutex_lock(&glob->device_list_mutex);
1519 list_del(&bdev->device_list);
1520 mutex_unlock(&glob->device_list_mutex);
1522 cancel_delayed_work_sync(&bdev->wq);
1524 while (ttm_bo_delayed_delete(bdev, true))
1527 spin_lock(&glob->lru_lock);
1528 if (list_empty(&bdev->ddestroy))
1529 TTM_DEBUG("Delayed destroy list was clean\n");
1531 if (list_empty(&bdev->man[0].lru))
1532 TTM_DEBUG("Swap list was clean\n");
1533 spin_unlock(&glob->lru_lock);
1535 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1536 write_lock(&bdev->vm_lock);
1537 drm_mm_takedown(&bdev->addr_space_mm);
1538 write_unlock(&bdev->vm_lock);
1542 EXPORT_SYMBOL(ttm_bo_device_release);
1544 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1545 struct ttm_bo_global *glob,
1546 struct ttm_bo_driver *driver,
1547 uint64_t file_page_offset,
1552 rwlock_init(&bdev->vm_lock);
1553 bdev->driver = driver;
1555 memset(bdev->man, 0, sizeof(bdev->man));
1558 * Initialize the system memory buffer type.
1559 * Other types need to be driver / IOCTL initialized.
1561 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1562 if (unlikely(ret != 0))
1565 bdev->addr_space_rb = RB_ROOT;
1566 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1567 if (unlikely(ret != 0))
1568 goto out_no_addr_mm;
1570 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1571 bdev->nice_mode = true;
1572 INIT_LIST_HEAD(&bdev->ddestroy);
1573 bdev->dev_mapping = NULL;
1575 bdev->need_dma32 = need_dma32;
1577 spin_lock_init(&bdev->fence_lock);
1578 mutex_lock(&glob->device_list_mutex);
1579 list_add_tail(&bdev->device_list, &glob->device_list);
1580 mutex_unlock(&glob->device_list_mutex);
1584 ttm_bo_clean_mm(bdev, 0);
1588 EXPORT_SYMBOL(ttm_bo_device_init);
1591 * buffer object vm functions.
1594 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1596 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1598 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1599 if (mem->mem_type == TTM_PL_SYSTEM)
1602 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1605 if (mem->placement & TTM_PL_FLAG_CACHED)
1611 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1613 struct ttm_bo_device *bdev = bo->bdev;
1614 loff_t offset = (loff_t) bo->addr_space_offset;
1615 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1617 if (!bdev->dev_mapping)
1619 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1620 ttm_mem_io_free_vm(bo);
1623 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1625 struct ttm_bo_device *bdev = bo->bdev;
1626 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1628 ttm_mem_io_lock(man, false);
1629 ttm_bo_unmap_virtual_locked(bo);
1630 ttm_mem_io_unlock(man);
1634 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1636 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1638 struct ttm_bo_device *bdev = bo->bdev;
1639 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1640 struct rb_node *parent = NULL;
1641 struct ttm_buffer_object *cur_bo;
1642 unsigned long offset = bo->vm_node->start;
1643 unsigned long cur_offset;
1647 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1648 cur_offset = cur_bo->vm_node->start;
1649 if (offset < cur_offset)
1650 cur = &parent->rb_left;
1651 else if (offset > cur_offset)
1652 cur = &parent->rb_right;
1657 rb_link_node(&bo->vm_rb, parent, cur);
1658 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1664 * @bo: the buffer to allocate address space for
1666 * Allocate address space in the drm device so that applications
1667 * can mmap the buffer and access the contents. This only
1668 * applies to ttm_bo_type_device objects as others are not
1669 * placed in the drm device address space.
1672 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1674 struct ttm_bo_device *bdev = bo->bdev;
1678 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1679 if (unlikely(ret != 0))
1682 write_lock(&bdev->vm_lock);
1683 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1684 bo->mem.num_pages, 0, 0);
1686 if (unlikely(bo->vm_node == NULL)) {
1691 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1692 bo->mem.num_pages, 0);
1694 if (unlikely(bo->vm_node == NULL)) {
1695 write_unlock(&bdev->vm_lock);
1699 ttm_bo_vm_insert_rb(bo);
1700 write_unlock(&bdev->vm_lock);
1701 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1705 write_unlock(&bdev->vm_lock);
1709 int ttm_bo_wait(struct ttm_buffer_object *bo,
1710 bool lazy, bool interruptible, bool no_wait)
1712 struct ttm_bo_driver *driver = bo->bdev->driver;
1713 struct ttm_bo_device *bdev = bo->bdev;
1718 if (likely(bo->sync_obj == NULL))
1721 while (bo->sync_obj) {
1723 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1724 void *tmp_obj = bo->sync_obj;
1725 bo->sync_obj = NULL;
1726 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1727 spin_unlock(&bdev->fence_lock);
1728 driver->sync_obj_unref(&tmp_obj);
1729 spin_lock(&bdev->fence_lock);
1736 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1737 sync_obj_arg = bo->sync_obj_arg;
1738 spin_unlock(&bdev->fence_lock);
1739 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1740 lazy, interruptible);
1741 if (unlikely(ret != 0)) {
1742 driver->sync_obj_unref(&sync_obj);
1743 spin_lock(&bdev->fence_lock);
1746 spin_lock(&bdev->fence_lock);
1747 if (likely(bo->sync_obj == sync_obj &&
1748 bo->sync_obj_arg == sync_obj_arg)) {
1749 void *tmp_obj = bo->sync_obj;
1750 bo->sync_obj = NULL;
1751 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1753 spin_unlock(&bdev->fence_lock);
1754 driver->sync_obj_unref(&sync_obj);
1755 driver->sync_obj_unref(&tmp_obj);
1756 spin_lock(&bdev->fence_lock);
1758 spin_unlock(&bdev->fence_lock);
1759 driver->sync_obj_unref(&sync_obj);
1760 spin_lock(&bdev->fence_lock);
1765 EXPORT_SYMBOL(ttm_bo_wait);
1767 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1769 struct ttm_bo_device *bdev = bo->bdev;
1773 * Using ttm_bo_reserve makes sure the lru lists are updated.
1776 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1777 if (unlikely(ret != 0))
1779 spin_lock(&bdev->fence_lock);
1780 ret = ttm_bo_wait(bo, false, true, no_wait);
1781 spin_unlock(&bdev->fence_lock);
1782 if (likely(ret == 0))
1783 atomic_inc(&bo->cpu_writers);
1784 ttm_bo_unreserve(bo);
1787 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1789 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1791 if (atomic_dec_and_test(&bo->cpu_writers))
1792 wake_up_all(&bo->event_queue);
1794 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1797 * A buffer object shrink method that tries to swap out the first
1798 * buffer object on the bo_global::swap_lru list.
1801 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1803 struct ttm_bo_global *glob =
1804 container_of(shrink, struct ttm_bo_global, shrink);
1805 struct ttm_buffer_object *bo;
1808 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1810 spin_lock(&glob->lru_lock);
1811 while (ret == -EBUSY) {
1812 if (unlikely(list_empty(&glob->swap_lru))) {
1813 spin_unlock(&glob->lru_lock);
1817 bo = list_first_entry(&glob->swap_lru,
1818 struct ttm_buffer_object, swap);
1819 kref_get(&bo->list_kref);
1821 if (!list_empty(&bo->ddestroy)) {
1822 spin_unlock(&glob->lru_lock);
1823 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1824 kref_put(&bo->list_kref, ttm_bo_release_list);
1829 * Reserve buffer. Since we unlock while sleeping, we need
1830 * to re-check that nobody removed us from the swap-list while
1834 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1835 if (unlikely(ret == -EBUSY)) {
1836 spin_unlock(&glob->lru_lock);
1837 ttm_bo_wait_unreserved(bo, false);
1838 kref_put(&bo->list_kref, ttm_bo_release_list);
1839 spin_lock(&glob->lru_lock);
1844 put_count = ttm_bo_del_from_lru(bo);
1845 spin_unlock(&glob->lru_lock);
1847 ttm_bo_list_ref_sub(bo, put_count, true);
1850 * Wait for GPU, then move to system cached.
1853 spin_lock(&bo->bdev->fence_lock);
1854 ret = ttm_bo_wait(bo, false, false, false);
1855 spin_unlock(&bo->bdev->fence_lock);
1857 if (unlikely(ret != 0))
1860 if ((bo->mem.placement & swap_placement) != swap_placement) {
1861 struct ttm_mem_reg evict_mem;
1863 evict_mem = bo->mem;
1864 evict_mem.mm_node = NULL;
1865 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1866 evict_mem.mem_type = TTM_PL_SYSTEM;
1868 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1869 false, false, false);
1870 if (unlikely(ret != 0))
1874 ttm_bo_unmap_virtual(bo);
1877 * Swap out. Buffer will be swapped in again as soon as
1878 * anyone tries to access a ttm page.
1881 if (bo->bdev->driver->swap_notify)
1882 bo->bdev->driver->swap_notify(bo);
1884 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1889 * Unreserve without putting on LRU to avoid swapping out an
1890 * already swapped buffer.
1893 atomic_set(&bo->reserved, 0);
1894 wake_up_all(&bo->event_queue);
1895 kref_put(&bo->list_kref, ttm_bo_release_list);
1899 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1901 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1904 EXPORT_SYMBOL(ttm_bo_swapout_all);