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>
32 * We store bo pointer in drm_mm_node struct so we know which bo own a
33 * specific node. There is no protection on the pointer, thus to make
34 * sure things don't go berserk you have to access this pointer while
35 * holding the global lru lock and make sure anytime you free a node you
36 * reset the pointer to NULL.
39 #include "ttm/ttm_module.h"
40 #include "ttm/ttm_bo_driver.h"
41 #include "ttm/ttm_placement.h"
42 #include <linux/jiffies.h>
43 #include <linux/slab.h>
44 #include <linux/sched.h>
46 #include <linux/file.h>
47 #include <linux/module.h>
49 #define TTM_ASSERT_LOCKED(param)
50 #define TTM_DEBUG(fmt, arg...)
51 #define TTM_BO_HASH_ORDER 13
53 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
54 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
55 static void ttm_bo_global_kobj_release(struct kobject *kobj);
57 static struct attribute ttm_bo_count = {
62 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
66 for (i = 0; i <= TTM_PL_PRIV5; i++)
67 if (flags & (1 << i)) {
74 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
76 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
78 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
79 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
80 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
81 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
82 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
83 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
84 man->available_caching);
85 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
86 man->default_caching);
87 if (mem_type != TTM_PL_SYSTEM)
88 (*man->func->debug)(man, TTM_PFX);
91 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
92 struct ttm_placement *placement)
96 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
97 bo, bo->mem.num_pages, bo->mem.size >> 10,
99 for (i = 0; i < placement->num_placement; i++) {
100 ret = ttm_mem_type_from_flags(placement->placement[i],
104 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
105 i, placement->placement[i], mem_type);
106 ttm_mem_type_debug(bo->bdev, mem_type);
110 static ssize_t ttm_bo_global_show(struct kobject *kobj,
111 struct attribute *attr,
114 struct ttm_bo_global *glob =
115 container_of(kobj, struct ttm_bo_global, kobj);
117 return snprintf(buffer, PAGE_SIZE, "%lu\n",
118 (unsigned long) atomic_read(&glob->bo_count));
121 static struct attribute *ttm_bo_global_attrs[] = {
126 static const struct sysfs_ops ttm_bo_global_ops = {
127 .show = &ttm_bo_global_show
130 static struct kobj_type ttm_bo_glob_kobj_type = {
131 .release = &ttm_bo_global_kobj_release,
132 .sysfs_ops = &ttm_bo_global_ops,
133 .default_attrs = ttm_bo_global_attrs
137 static inline uint32_t ttm_bo_type_flags(unsigned type)
142 static void ttm_bo_release_list(struct kref *list_kref)
144 struct ttm_buffer_object *bo =
145 container_of(list_kref, struct ttm_buffer_object, list_kref);
146 struct ttm_bo_device *bdev = bo->bdev;
148 BUG_ON(atomic_read(&bo->list_kref.refcount));
149 BUG_ON(atomic_read(&bo->kref.refcount));
150 BUG_ON(atomic_read(&bo->cpu_writers));
151 BUG_ON(bo->sync_obj != NULL);
152 BUG_ON(bo->mem.mm_node != NULL);
153 BUG_ON(!list_empty(&bo->lru));
154 BUG_ON(!list_empty(&bo->ddestroy));
157 ttm_tt_destroy(bo->ttm);
158 atomic_dec(&bo->glob->bo_count);
162 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
167 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
173 ret = wait_event_interruptible(bo->event_queue,
174 atomic_read(&bo->reserved) == 0);
175 if (unlikely(ret != 0))
178 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
182 EXPORT_SYMBOL(ttm_bo_wait_unreserved);
184 static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
186 struct ttm_bo_device *bdev = bo->bdev;
187 struct ttm_mem_type_manager *man;
189 BUG_ON(!atomic_read(&bo->reserved));
191 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
193 BUG_ON(!list_empty(&bo->lru));
195 man = &bdev->man[bo->mem.mem_type];
196 list_add_tail(&bo->lru, &man->lru);
197 kref_get(&bo->list_kref);
199 if (bo->ttm != NULL) {
200 list_add_tail(&bo->swap, &bo->glob->swap_lru);
201 kref_get(&bo->list_kref);
207 * Call with the lru_lock held.
210 static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
214 if (!list_empty(&bo->swap)) {
215 list_del_init(&bo->swap);
218 if (!list_empty(&bo->lru)) {
219 list_del_init(&bo->lru);
224 * TODO: Add a driver hook to delete from
225 * driver-specific LRU's here.
231 int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
233 bool no_wait, bool use_sequence, uint32_t sequence)
235 struct ttm_bo_global *glob = bo->glob;
238 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
239 if (use_sequence && bo->seq_valid &&
240 (sequence - bo->val_seq < (1 << 31))) {
247 spin_unlock(&glob->lru_lock);
248 ret = ttm_bo_wait_unreserved(bo, interruptible);
249 spin_lock(&glob->lru_lock);
256 bo->val_seq = sequence;
257 bo->seq_valid = true;
259 bo->seq_valid = false;
264 EXPORT_SYMBOL(ttm_bo_reserve);
266 static void ttm_bo_ref_bug(struct kref *list_kref)
271 int ttm_bo_reserve(struct ttm_buffer_object *bo,
273 bool no_wait, bool use_sequence, uint32_t sequence)
275 struct ttm_bo_global *glob = bo->glob;
279 spin_lock(&glob->lru_lock);
280 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
282 if (likely(ret == 0))
283 put_count = ttm_bo_del_from_lru(bo);
284 spin_unlock(&glob->lru_lock);
287 kref_put(&bo->list_kref, ttm_bo_ref_bug);
292 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
294 struct ttm_bo_global *glob = bo->glob;
296 spin_lock(&glob->lru_lock);
297 ttm_bo_add_to_lru(bo);
298 atomic_set(&bo->reserved, 0);
299 wake_up_all(&bo->event_queue);
300 spin_unlock(&glob->lru_lock);
302 EXPORT_SYMBOL(ttm_bo_unreserve);
305 * Call bo->mutex locked.
307 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
309 struct ttm_bo_device *bdev = bo->bdev;
310 struct ttm_bo_global *glob = bo->glob;
312 uint32_t page_flags = 0;
314 TTM_ASSERT_LOCKED(&bo->mutex);
317 if (bdev->need_dma32)
318 page_flags |= TTM_PAGE_FLAG_DMA32;
321 case ttm_bo_type_device:
323 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
324 case ttm_bo_type_kernel:
325 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
326 page_flags, glob->dummy_read_page);
327 if (unlikely(bo->ttm == NULL))
330 case ttm_bo_type_user:
331 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
332 page_flags | TTM_PAGE_FLAG_USER,
333 glob->dummy_read_page);
334 if (unlikely(bo->ttm == NULL)) {
339 ret = ttm_tt_set_user(bo->ttm, current,
340 bo->buffer_start, bo->num_pages);
341 if (unlikely(ret != 0))
342 ttm_tt_destroy(bo->ttm);
345 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
353 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
354 struct ttm_mem_reg *mem,
355 bool evict, bool interruptible,
356 bool no_wait_reserve, bool no_wait_gpu)
358 struct ttm_bo_device *bdev = bo->bdev;
359 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
360 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
361 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
362 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
365 if (old_is_pci || new_is_pci ||
366 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
367 ttm_bo_unmap_virtual(bo);
370 * Create and bind a ttm if required.
373 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
374 ret = ttm_bo_add_ttm(bo, false);
378 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
382 if (mem->mem_type != TTM_PL_SYSTEM) {
383 ret = ttm_tt_bind(bo->ttm, mem);
388 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
396 if (bdev->driver->move_notify)
397 bdev->driver->move_notify(bo, mem);
399 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
400 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
401 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
402 else if (bdev->driver->move)
403 ret = bdev->driver->move(bo, evict, interruptible,
404 no_wait_reserve, no_wait_gpu, mem);
406 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
413 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
415 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
419 if (bo->mem.mm_node) {
420 spin_lock(&bo->lock);
421 bo->offset = (bo->mem.start << PAGE_SHIFT) +
422 bdev->man[bo->mem.mem_type].gpu_offset;
423 bo->cur_placement = bo->mem.placement;
424 spin_unlock(&bo->lock);
431 new_man = &bdev->man[bo->mem.mem_type];
432 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
433 ttm_tt_unbind(bo->ttm);
434 ttm_tt_destroy(bo->ttm);
442 * Call bo::reserved and with the lru lock held.
443 * Will release GPU memory type usage on destruction.
444 * This is the place to put in driver specific hooks.
445 * Will release the bo::reserved lock and the
449 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
451 struct ttm_bo_global *glob = bo->glob;
456 * Release the lru_lock, since we don't want to have
457 * an atomic requirement on ttm_tt[unbind|destroy].
460 spin_unlock(&glob->lru_lock);
461 ttm_tt_unbind(bo->ttm);
462 ttm_tt_destroy(bo->ttm);
464 spin_lock(&glob->lru_lock);
467 ttm_bo_mem_put_locked(bo, &bo->mem);
469 atomic_set(&bo->reserved, 0);
470 wake_up_all(&bo->event_queue);
471 spin_unlock(&glob->lru_lock);
476 * If bo idle, remove from delayed- and lru lists, and unref.
477 * If not idle, and already on delayed list, do nothing.
478 * If not idle, and not on delayed list, put on delayed list,
479 * up the list_kref and schedule a delayed list check.
482 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
484 struct ttm_bo_device *bdev = bo->bdev;
485 struct ttm_bo_global *glob = bo->glob;
486 struct ttm_bo_driver *driver = bdev->driver;
489 spin_lock(&bo->lock);
491 (void) ttm_bo_wait(bo, false, false, !remove_all);
496 spin_unlock(&bo->lock);
498 spin_lock(&glob->lru_lock);
499 ret = ttm_bo_reserve_locked(bo, false, !remove_all, false, 0);
502 * Someone else has the object reserved. Bail and retry.
505 if (unlikely(ret == -EBUSY)) {
506 spin_unlock(&glob->lru_lock);
507 spin_lock(&bo->lock);
512 * We can re-check for sync object without taking
513 * the bo::lock since setting the sync object requires
514 * also bo::reserved. A busy object at this point may
515 * be caused by another thread starting an accelerated
519 if (unlikely(bo->sync_obj)) {
520 atomic_set(&bo->reserved, 0);
521 wake_up_all(&bo->event_queue);
522 spin_unlock(&glob->lru_lock);
523 spin_lock(&bo->lock);
530 put_count = ttm_bo_del_from_lru(bo);
532 if (!list_empty(&bo->ddestroy)) {
533 list_del_init(&bo->ddestroy);
537 ttm_bo_cleanup_memtype_use(bo);
540 kref_put(&bo->list_kref, ttm_bo_ref_bug);
545 spin_lock(&glob->lru_lock);
546 if (list_empty(&bo->ddestroy)) {
547 void *sync_obj = bo->sync_obj;
548 void *sync_obj_arg = bo->sync_obj_arg;
550 kref_get(&bo->list_kref);
551 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
552 spin_unlock(&glob->lru_lock);
553 spin_unlock(&bo->lock);
556 driver->sync_obj_flush(sync_obj, sync_obj_arg);
557 schedule_delayed_work(&bdev->wq,
558 ((HZ / 100) < 1) ? 1 : HZ / 100);
562 spin_unlock(&glob->lru_lock);
563 spin_unlock(&bo->lock);
571 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
572 * encountered buffers.
575 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
577 struct ttm_bo_global *glob = bdev->glob;
578 struct ttm_buffer_object *entry = NULL;
581 spin_lock(&glob->lru_lock);
582 if (list_empty(&bdev->ddestroy))
585 entry = list_first_entry(&bdev->ddestroy,
586 struct ttm_buffer_object, ddestroy);
587 kref_get(&entry->list_kref);
590 struct ttm_buffer_object *nentry = NULL;
592 if (entry->ddestroy.next != &bdev->ddestroy) {
593 nentry = list_first_entry(&entry->ddestroy,
594 struct ttm_buffer_object, ddestroy);
595 kref_get(&nentry->list_kref);
598 spin_unlock(&glob->lru_lock);
599 ret = ttm_bo_cleanup_refs(entry, remove_all);
600 kref_put(&entry->list_kref, ttm_bo_release_list);
606 spin_lock(&glob->lru_lock);
607 if (list_empty(&entry->ddestroy))
612 spin_unlock(&glob->lru_lock);
615 kref_put(&entry->list_kref, ttm_bo_release_list);
619 static void ttm_bo_delayed_workqueue(struct work_struct *work)
621 struct ttm_bo_device *bdev =
622 container_of(work, struct ttm_bo_device, wq.work);
624 if (ttm_bo_delayed_delete(bdev, false)) {
625 schedule_delayed_work(&bdev->wq,
626 ((HZ / 100) < 1) ? 1 : HZ / 100);
630 static void ttm_bo_release(struct kref *kref)
632 struct ttm_buffer_object *bo =
633 container_of(kref, struct ttm_buffer_object, kref);
634 struct ttm_bo_device *bdev = bo->bdev;
636 if (likely(bo->vm_node != NULL)) {
637 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
638 drm_mm_put_block(bo->vm_node);
641 write_unlock(&bdev->vm_lock);
642 ttm_bo_cleanup_refs(bo, false);
643 kref_put(&bo->list_kref, ttm_bo_release_list);
644 write_lock(&bdev->vm_lock);
647 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
649 struct ttm_buffer_object *bo = *p_bo;
650 struct ttm_bo_device *bdev = bo->bdev;
653 write_lock(&bdev->vm_lock);
654 kref_put(&bo->kref, ttm_bo_release);
655 write_unlock(&bdev->vm_lock);
657 EXPORT_SYMBOL(ttm_bo_unref);
659 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
661 return cancel_delayed_work_sync(&bdev->wq);
663 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
665 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
668 schedule_delayed_work(&bdev->wq,
669 ((HZ / 100) < 1) ? 1 : HZ / 100);
671 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
673 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
674 bool no_wait_reserve, bool no_wait_gpu)
676 struct ttm_bo_device *bdev = bo->bdev;
677 struct ttm_mem_reg evict_mem;
678 struct ttm_placement placement;
681 spin_lock(&bo->lock);
682 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
683 spin_unlock(&bo->lock);
685 if (unlikely(ret != 0)) {
686 if (ret != -ERESTARTSYS) {
687 printk(KERN_ERR TTM_PFX
688 "Failed to expire sync object before "
689 "buffer eviction.\n");
694 BUG_ON(!atomic_read(&bo->reserved));
697 evict_mem.mm_node = NULL;
698 evict_mem.bus.io_reserved = false;
702 placement.num_placement = 0;
703 placement.num_busy_placement = 0;
704 bdev->driver->evict_flags(bo, &placement);
705 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
706 no_wait_reserve, no_wait_gpu);
708 if (ret != -ERESTARTSYS) {
709 printk(KERN_ERR TTM_PFX
710 "Failed to find memory space for "
711 "buffer 0x%p eviction.\n", bo);
712 ttm_bo_mem_space_debug(bo, &placement);
717 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
718 no_wait_reserve, no_wait_gpu);
720 if (ret != -ERESTARTSYS)
721 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
722 ttm_bo_mem_put(bo, &evict_mem);
730 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
732 bool interruptible, bool no_wait_reserve,
735 struct ttm_bo_global *glob = bdev->glob;
736 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
737 struct ttm_buffer_object *bo;
738 int ret, put_count = 0;
741 spin_lock(&glob->lru_lock);
742 if (list_empty(&man->lru)) {
743 spin_unlock(&glob->lru_lock);
747 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
748 kref_get(&bo->list_kref);
750 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
752 if (unlikely(ret == -EBUSY)) {
753 spin_unlock(&glob->lru_lock);
754 if (likely(!no_wait_gpu))
755 ret = ttm_bo_wait_unreserved(bo, interruptible);
757 kref_put(&bo->list_kref, ttm_bo_release_list);
760 * We *need* to retry after releasing the lru lock.
763 if (unlikely(ret != 0))
768 put_count = ttm_bo_del_from_lru(bo);
769 spin_unlock(&glob->lru_lock);
774 kref_put(&bo->list_kref, ttm_bo_ref_bug);
776 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
777 ttm_bo_unreserve(bo);
779 kref_put(&bo->list_kref, ttm_bo_release_list);
783 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
785 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
788 (*man->func->put_node)(man, mem);
790 EXPORT_SYMBOL(ttm_bo_mem_put);
792 void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
794 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
797 (*man->func->put_node_locked)(man, mem);
799 EXPORT_SYMBOL(ttm_bo_mem_put_locked);
802 * Repeatedly evict memory from the LRU for @mem_type until we create enough
803 * space, or we've evicted everything and there isn't enough space.
805 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
807 struct ttm_placement *placement,
808 struct ttm_mem_reg *mem,
810 bool no_wait_reserve,
813 struct ttm_bo_device *bdev = bo->bdev;
814 struct ttm_bo_global *glob = bdev->glob;
815 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
819 ret = (*man->func->get_node)(man, bo, placement, mem);
820 if (unlikely(ret != 0))
824 spin_lock(&glob->lru_lock);
825 if (list_empty(&man->lru)) {
826 spin_unlock(&glob->lru_lock);
829 spin_unlock(&glob->lru_lock);
830 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
831 no_wait_reserve, no_wait_gpu);
832 if (unlikely(ret != 0))
835 if (mem->mm_node == NULL)
837 mem->mem_type = mem_type;
841 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
842 uint32_t cur_placement,
843 uint32_t proposed_placement)
845 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
846 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
849 * Keep current caching if possible.
852 if ((cur_placement & caching) != 0)
853 result |= (cur_placement & caching);
854 else if ((man->default_caching & caching) != 0)
855 result |= man->default_caching;
856 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
857 result |= TTM_PL_FLAG_CACHED;
858 else if ((TTM_PL_FLAG_WC & caching) != 0)
859 result |= TTM_PL_FLAG_WC;
860 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
861 result |= TTM_PL_FLAG_UNCACHED;
866 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
869 uint32_t proposed_placement,
870 uint32_t *masked_placement)
872 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
874 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
877 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
880 if ((proposed_placement & man->available_caching) == 0)
883 cur_flags |= (proposed_placement & man->available_caching);
885 *masked_placement = cur_flags;
890 * Creates space for memory region @mem according to its type.
892 * This function first searches for free space in compatible memory types in
893 * the priority order defined by the driver. If free space isn't found, then
894 * ttm_bo_mem_force_space is attempted in priority order to evict and find
897 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
898 struct ttm_placement *placement,
899 struct ttm_mem_reg *mem,
900 bool interruptible, bool no_wait_reserve,
903 struct ttm_bo_device *bdev = bo->bdev;
904 struct ttm_mem_type_manager *man;
905 uint32_t mem_type = TTM_PL_SYSTEM;
906 uint32_t cur_flags = 0;
907 bool type_found = false;
908 bool type_ok = false;
909 bool has_erestartsys = false;
913 for (i = 0; i < placement->num_placement; ++i) {
914 ret = ttm_mem_type_from_flags(placement->placement[i],
918 man = &bdev->man[mem_type];
920 type_ok = ttm_bo_mt_compatible(man,
921 bo->type == ttm_bo_type_user,
923 placement->placement[i],
929 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
932 * Use the access and other non-mapping-related flag bits from
933 * the memory placement flags to the current flags
935 ttm_flag_masked(&cur_flags, placement->placement[i],
936 ~TTM_PL_MASK_MEMTYPE);
938 if (mem_type == TTM_PL_SYSTEM)
941 if (man->has_type && man->use_type) {
943 ret = (*man->func->get_node)(man, bo, placement, mem);
951 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
952 mem->mem_type = mem_type;
953 mem->placement = cur_flags;
960 for (i = 0; i < placement->num_busy_placement; ++i) {
961 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
965 man = &bdev->man[mem_type];
968 if (!ttm_bo_mt_compatible(man,
969 bo->type == ttm_bo_type_user,
971 placement->busy_placement[i],
975 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
978 * Use the access and other non-mapping-related flag bits from
979 * the memory placement flags to the current flags
981 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
982 ~TTM_PL_MASK_MEMTYPE);
985 if (mem_type == TTM_PL_SYSTEM) {
986 mem->mem_type = mem_type;
987 mem->placement = cur_flags;
992 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
993 interruptible, no_wait_reserve, no_wait_gpu);
994 if (ret == 0 && mem->mm_node) {
995 mem->placement = cur_flags;
998 if (ret == -ERESTARTSYS)
999 has_erestartsys = true;
1001 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1004 EXPORT_SYMBOL(ttm_bo_mem_space);
1006 int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1008 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1011 return wait_event_interruptible(bo->event_queue,
1012 atomic_read(&bo->cpu_writers) == 0);
1014 EXPORT_SYMBOL(ttm_bo_wait_cpu);
1016 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1017 struct ttm_placement *placement,
1018 bool interruptible, bool no_wait_reserve,
1022 struct ttm_mem_reg mem;
1024 BUG_ON(!atomic_read(&bo->reserved));
1027 * FIXME: It's possible to pipeline buffer moves.
1028 * Have the driver move function wait for idle when necessary,
1029 * instead of doing it here.
1031 spin_lock(&bo->lock);
1032 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1033 spin_unlock(&bo->lock);
1036 mem.num_pages = bo->num_pages;
1037 mem.size = mem.num_pages << PAGE_SHIFT;
1038 mem.page_alignment = bo->mem.page_alignment;
1039 mem.bus.io_reserved = false;
1041 * Determine where to move the buffer.
1043 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
1046 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
1048 if (ret && mem.mm_node)
1049 ttm_bo_mem_put(bo, &mem);
1053 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1054 struct ttm_mem_reg *mem)
1058 if (mem->mm_node && placement->lpfn != 0 &&
1059 (mem->start < placement->fpfn ||
1060 mem->start + mem->num_pages > placement->lpfn))
1063 for (i = 0; i < placement->num_placement; i++) {
1064 if ((placement->placement[i] & mem->placement &
1065 TTM_PL_MASK_CACHING) &&
1066 (placement->placement[i] & mem->placement &
1073 int ttm_bo_validate(struct ttm_buffer_object *bo,
1074 struct ttm_placement *placement,
1075 bool interruptible, bool no_wait_reserve,
1080 BUG_ON(!atomic_read(&bo->reserved));
1081 /* Check that range is valid */
1082 if (placement->lpfn || placement->fpfn)
1083 if (placement->fpfn > placement->lpfn ||
1084 (placement->lpfn - placement->fpfn) < bo->num_pages)
1087 * Check whether we need to move buffer.
1089 ret = ttm_bo_mem_compat(placement, &bo->mem);
1091 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
1096 * Use the access and other non-mapping-related flag bits from
1097 * the compatible memory placement flags to the active flags
1099 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1100 ~TTM_PL_MASK_MEMTYPE);
1103 * We might need to add a TTM.
1105 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1106 ret = ttm_bo_add_ttm(bo, true);
1112 EXPORT_SYMBOL(ttm_bo_validate);
1114 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1115 struct ttm_placement *placement)
1119 if (placement->fpfn || placement->lpfn) {
1120 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1121 printk(KERN_ERR TTM_PFX "Page number range to small "
1122 "Need %lu pages, range is [%u, %u]\n",
1123 bo->mem.num_pages, placement->fpfn,
1128 for (i = 0; i < placement->num_placement; i++) {
1129 if (!capable(CAP_SYS_ADMIN)) {
1130 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1131 printk(KERN_ERR TTM_PFX "Need to be root to "
1132 "modify NO_EVICT status.\n");
1137 for (i = 0; i < placement->num_busy_placement; i++) {
1138 if (!capable(CAP_SYS_ADMIN)) {
1139 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1140 printk(KERN_ERR TTM_PFX "Need to be root to "
1141 "modify NO_EVICT status.\n");
1149 int ttm_bo_init(struct ttm_bo_device *bdev,
1150 struct ttm_buffer_object *bo,
1152 enum ttm_bo_type type,
1153 struct ttm_placement *placement,
1154 uint32_t page_alignment,
1155 unsigned long buffer_start,
1157 struct file *persistant_swap_storage,
1159 void (*destroy) (struct ttm_buffer_object *))
1162 unsigned long num_pages;
1164 size += buffer_start & ~PAGE_MASK;
1165 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1166 if (num_pages == 0) {
1167 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1170 bo->destroy = destroy;
1172 spin_lock_init(&bo->lock);
1173 kref_init(&bo->kref);
1174 kref_init(&bo->list_kref);
1175 atomic_set(&bo->cpu_writers, 0);
1176 atomic_set(&bo->reserved, 1);
1177 init_waitqueue_head(&bo->event_queue);
1178 INIT_LIST_HEAD(&bo->lru);
1179 INIT_LIST_HEAD(&bo->ddestroy);
1180 INIT_LIST_HEAD(&bo->swap);
1182 bo->glob = bdev->glob;
1184 bo->num_pages = num_pages;
1185 bo->mem.size = num_pages << PAGE_SHIFT;
1186 bo->mem.mem_type = TTM_PL_SYSTEM;
1187 bo->mem.num_pages = bo->num_pages;
1188 bo->mem.mm_node = NULL;
1189 bo->mem.page_alignment = page_alignment;
1190 bo->mem.bus.io_reserved = false;
1191 bo->buffer_start = buffer_start & PAGE_MASK;
1193 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1194 bo->seq_valid = false;
1195 bo->persistant_swap_storage = persistant_swap_storage;
1196 bo->acc_size = acc_size;
1197 atomic_inc(&bo->glob->bo_count);
1199 ret = ttm_bo_check_placement(bo, placement);
1200 if (unlikely(ret != 0))
1204 * For ttm_bo_type_device buffers, allocate
1205 * address space from the device.
1207 if (bo->type == ttm_bo_type_device) {
1208 ret = ttm_bo_setup_vm(bo);
1213 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
1217 ttm_bo_unreserve(bo);
1221 ttm_bo_unreserve(bo);
1226 EXPORT_SYMBOL(ttm_bo_init);
1228 static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
1229 unsigned long num_pages)
1231 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1234 return glob->ttm_bo_size + 2 * page_array_size;
1237 int ttm_bo_create(struct ttm_bo_device *bdev,
1239 enum ttm_bo_type type,
1240 struct ttm_placement *placement,
1241 uint32_t page_alignment,
1242 unsigned long buffer_start,
1244 struct file *persistant_swap_storage,
1245 struct ttm_buffer_object **p_bo)
1247 struct ttm_buffer_object *bo;
1248 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1252 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
1253 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1254 if (unlikely(ret != 0))
1257 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1259 if (unlikely(bo == NULL)) {
1260 ttm_mem_global_free(mem_glob, acc_size);
1264 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1265 buffer_start, interruptible,
1266 persistant_swap_storage, acc_size, NULL);
1267 if (likely(ret == 0))
1273 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1274 unsigned mem_type, bool allow_errors)
1276 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1277 struct ttm_bo_global *glob = bdev->glob;
1281 * Can't use standard list traversal since we're unlocking.
1284 spin_lock(&glob->lru_lock);
1285 while (!list_empty(&man->lru)) {
1286 spin_unlock(&glob->lru_lock);
1287 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
1292 printk(KERN_ERR TTM_PFX
1293 "Cleanup eviction failed\n");
1296 spin_lock(&glob->lru_lock);
1298 spin_unlock(&glob->lru_lock);
1302 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1304 struct ttm_mem_type_manager *man;
1307 if (mem_type >= TTM_NUM_MEM_TYPES) {
1308 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1311 man = &bdev->man[mem_type];
1313 if (!man->has_type) {
1314 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1315 "memory manager type %u\n", mem_type);
1319 man->use_type = false;
1320 man->has_type = false;
1324 ttm_bo_force_list_clean(bdev, mem_type, false);
1326 ret = (*man->func->takedown)(man);
1331 EXPORT_SYMBOL(ttm_bo_clean_mm);
1333 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1335 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1337 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1338 printk(KERN_ERR TTM_PFX
1339 "Illegal memory manager memory type %u.\n",
1344 if (!man->has_type) {
1345 printk(KERN_ERR TTM_PFX
1346 "Memory type %u has not been initialized.\n",
1351 return ttm_bo_force_list_clean(bdev, mem_type, true);
1353 EXPORT_SYMBOL(ttm_bo_evict_mm);
1355 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1356 unsigned long p_size)
1359 struct ttm_mem_type_manager *man;
1361 if (type >= TTM_NUM_MEM_TYPES) {
1362 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1366 man = &bdev->man[type];
1367 if (man->has_type) {
1368 printk(KERN_ERR TTM_PFX
1369 "Memory manager already initialized for type %d\n",
1374 ret = bdev->driver->init_mem_type(bdev, type, man);
1380 if (type != TTM_PL_SYSTEM) {
1382 printk(KERN_ERR TTM_PFX
1383 "Zero size memory manager type %d\n",
1388 ret = (*man->func->init)(man, p_size);
1392 man->has_type = true;
1393 man->use_type = true;
1396 INIT_LIST_HEAD(&man->lru);
1400 EXPORT_SYMBOL(ttm_bo_init_mm);
1402 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1404 struct ttm_bo_global *glob =
1405 container_of(kobj, struct ttm_bo_global, kobj);
1407 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1408 __free_page(glob->dummy_read_page);
1412 void ttm_bo_global_release(struct drm_global_reference *ref)
1414 struct ttm_bo_global *glob = ref->object;
1416 kobject_del(&glob->kobj);
1417 kobject_put(&glob->kobj);
1419 EXPORT_SYMBOL(ttm_bo_global_release);
1421 int ttm_bo_global_init(struct drm_global_reference *ref)
1423 struct ttm_bo_global_ref *bo_ref =
1424 container_of(ref, struct ttm_bo_global_ref, ref);
1425 struct ttm_bo_global *glob = ref->object;
1428 mutex_init(&glob->device_list_mutex);
1429 spin_lock_init(&glob->lru_lock);
1430 glob->mem_glob = bo_ref->mem_glob;
1431 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1433 if (unlikely(glob->dummy_read_page == NULL)) {
1438 INIT_LIST_HEAD(&glob->swap_lru);
1439 INIT_LIST_HEAD(&glob->device_list);
1441 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1442 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1443 if (unlikely(ret != 0)) {
1444 printk(KERN_ERR TTM_PFX
1445 "Could not register buffer object swapout.\n");
1449 glob->ttm_bo_extra_size =
1450 ttm_round_pot(sizeof(struct ttm_tt)) +
1451 ttm_round_pot(sizeof(struct ttm_backend));
1453 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1454 ttm_round_pot(sizeof(struct ttm_buffer_object));
1456 atomic_set(&glob->bo_count, 0);
1458 ret = kobject_init_and_add(
1459 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1460 if (unlikely(ret != 0))
1461 kobject_put(&glob->kobj);
1464 __free_page(glob->dummy_read_page);
1469 EXPORT_SYMBOL(ttm_bo_global_init);
1472 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1475 unsigned i = TTM_NUM_MEM_TYPES;
1476 struct ttm_mem_type_manager *man;
1477 struct ttm_bo_global *glob = bdev->glob;
1480 man = &bdev->man[i];
1481 if (man->has_type) {
1482 man->use_type = false;
1483 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1485 printk(KERN_ERR TTM_PFX
1486 "DRM memory manager type %d "
1487 "is not clean.\n", i);
1489 man->has_type = false;
1493 mutex_lock(&glob->device_list_mutex);
1494 list_del(&bdev->device_list);
1495 mutex_unlock(&glob->device_list_mutex);
1497 if (!cancel_delayed_work(&bdev->wq))
1498 flush_scheduled_work();
1500 while (ttm_bo_delayed_delete(bdev, true))
1503 spin_lock(&glob->lru_lock);
1504 if (list_empty(&bdev->ddestroy))
1505 TTM_DEBUG("Delayed destroy list was clean\n");
1507 if (list_empty(&bdev->man[0].lru))
1508 TTM_DEBUG("Swap list was clean\n");
1509 spin_unlock(&glob->lru_lock);
1511 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1512 write_lock(&bdev->vm_lock);
1513 drm_mm_takedown(&bdev->addr_space_mm);
1514 write_unlock(&bdev->vm_lock);
1518 EXPORT_SYMBOL(ttm_bo_device_release);
1520 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1521 struct ttm_bo_global *glob,
1522 struct ttm_bo_driver *driver,
1523 uint64_t file_page_offset,
1528 rwlock_init(&bdev->vm_lock);
1529 bdev->driver = driver;
1531 memset(bdev->man, 0, sizeof(bdev->man));
1534 * Initialize the system memory buffer type.
1535 * Other types need to be driver / IOCTL initialized.
1537 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1538 if (unlikely(ret != 0))
1541 bdev->addr_space_rb = RB_ROOT;
1542 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1543 if (unlikely(ret != 0))
1544 goto out_no_addr_mm;
1546 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1547 bdev->nice_mode = true;
1548 INIT_LIST_HEAD(&bdev->ddestroy);
1549 bdev->dev_mapping = NULL;
1551 bdev->need_dma32 = need_dma32;
1553 mutex_lock(&glob->device_list_mutex);
1554 list_add_tail(&bdev->device_list, &glob->device_list);
1555 mutex_unlock(&glob->device_list_mutex);
1559 ttm_bo_clean_mm(bdev, 0);
1563 EXPORT_SYMBOL(ttm_bo_device_init);
1566 * buffer object vm functions.
1569 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1571 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1573 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1574 if (mem->mem_type == TTM_PL_SYSTEM)
1577 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1580 if (mem->placement & TTM_PL_FLAG_CACHED)
1586 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1588 struct ttm_bo_device *bdev = bo->bdev;
1589 loff_t offset = (loff_t) bo->addr_space_offset;
1590 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1592 if (!bdev->dev_mapping)
1594 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1595 ttm_mem_io_free(bdev, &bo->mem);
1597 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1599 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1601 struct ttm_bo_device *bdev = bo->bdev;
1602 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1603 struct rb_node *parent = NULL;
1604 struct ttm_buffer_object *cur_bo;
1605 unsigned long offset = bo->vm_node->start;
1606 unsigned long cur_offset;
1610 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1611 cur_offset = cur_bo->vm_node->start;
1612 if (offset < cur_offset)
1613 cur = &parent->rb_left;
1614 else if (offset > cur_offset)
1615 cur = &parent->rb_right;
1620 rb_link_node(&bo->vm_rb, parent, cur);
1621 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1627 * @bo: the buffer to allocate address space for
1629 * Allocate address space in the drm device so that applications
1630 * can mmap the buffer and access the contents. This only
1631 * applies to ttm_bo_type_device objects as others are not
1632 * placed in the drm device address space.
1635 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1637 struct ttm_bo_device *bdev = bo->bdev;
1641 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1642 if (unlikely(ret != 0))
1645 write_lock(&bdev->vm_lock);
1646 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1647 bo->mem.num_pages, 0, 0);
1649 if (unlikely(bo->vm_node == NULL)) {
1654 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1655 bo->mem.num_pages, 0);
1657 if (unlikely(bo->vm_node == NULL)) {
1658 write_unlock(&bdev->vm_lock);
1662 ttm_bo_vm_insert_rb(bo);
1663 write_unlock(&bdev->vm_lock);
1664 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1668 write_unlock(&bdev->vm_lock);
1672 int ttm_bo_wait(struct ttm_buffer_object *bo,
1673 bool lazy, bool interruptible, bool no_wait)
1675 struct ttm_bo_driver *driver = bo->bdev->driver;
1680 if (likely(bo->sync_obj == NULL))
1683 while (bo->sync_obj) {
1685 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1686 void *tmp_obj = bo->sync_obj;
1687 bo->sync_obj = NULL;
1688 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1689 spin_unlock(&bo->lock);
1690 driver->sync_obj_unref(&tmp_obj);
1691 spin_lock(&bo->lock);
1698 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1699 sync_obj_arg = bo->sync_obj_arg;
1700 spin_unlock(&bo->lock);
1701 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1702 lazy, interruptible);
1703 if (unlikely(ret != 0)) {
1704 driver->sync_obj_unref(&sync_obj);
1705 spin_lock(&bo->lock);
1708 spin_lock(&bo->lock);
1709 if (likely(bo->sync_obj == sync_obj &&
1710 bo->sync_obj_arg == sync_obj_arg)) {
1711 void *tmp_obj = bo->sync_obj;
1712 bo->sync_obj = NULL;
1713 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1715 spin_unlock(&bo->lock);
1716 driver->sync_obj_unref(&sync_obj);
1717 driver->sync_obj_unref(&tmp_obj);
1718 spin_lock(&bo->lock);
1720 spin_unlock(&bo->lock);
1721 driver->sync_obj_unref(&sync_obj);
1722 spin_lock(&bo->lock);
1727 EXPORT_SYMBOL(ttm_bo_wait);
1729 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1734 * Using ttm_bo_reserve makes sure the lru lists are updated.
1737 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1738 if (unlikely(ret != 0))
1740 spin_lock(&bo->lock);
1741 ret = ttm_bo_wait(bo, false, true, no_wait);
1742 spin_unlock(&bo->lock);
1743 if (likely(ret == 0))
1744 atomic_inc(&bo->cpu_writers);
1745 ttm_bo_unreserve(bo);
1748 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1750 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1752 if (atomic_dec_and_test(&bo->cpu_writers))
1753 wake_up_all(&bo->event_queue);
1755 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1758 * A buffer object shrink method that tries to swap out the first
1759 * buffer object on the bo_global::swap_lru list.
1762 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1764 struct ttm_bo_global *glob =
1765 container_of(shrink, struct ttm_bo_global, shrink);
1766 struct ttm_buffer_object *bo;
1769 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1771 spin_lock(&glob->lru_lock);
1772 while (ret == -EBUSY) {
1773 if (unlikely(list_empty(&glob->swap_lru))) {
1774 spin_unlock(&glob->lru_lock);
1778 bo = list_first_entry(&glob->swap_lru,
1779 struct ttm_buffer_object, swap);
1780 kref_get(&bo->list_kref);
1783 * Reserve buffer. Since we unlock while sleeping, we need
1784 * to re-check that nobody removed us from the swap-list while
1788 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1789 if (unlikely(ret == -EBUSY)) {
1790 spin_unlock(&glob->lru_lock);
1791 ttm_bo_wait_unreserved(bo, false);
1792 kref_put(&bo->list_kref, ttm_bo_release_list);
1793 spin_lock(&glob->lru_lock);
1798 put_count = ttm_bo_del_from_lru(bo);
1799 spin_unlock(&glob->lru_lock);
1802 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1805 * Wait for GPU, then move to system cached.
1808 spin_lock(&bo->lock);
1809 ret = ttm_bo_wait(bo, false, false, false);
1810 spin_unlock(&bo->lock);
1812 if (unlikely(ret != 0))
1815 if ((bo->mem.placement & swap_placement) != swap_placement) {
1816 struct ttm_mem_reg evict_mem;
1818 evict_mem = bo->mem;
1819 evict_mem.mm_node = NULL;
1820 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1821 evict_mem.mem_type = TTM_PL_SYSTEM;
1823 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1824 false, false, false);
1825 if (unlikely(ret != 0))
1829 ttm_bo_unmap_virtual(bo);
1832 * Swap out. Buffer will be swapped in again as soon as
1833 * anyone tries to access a ttm page.
1836 if (bo->bdev->driver->swap_notify)
1837 bo->bdev->driver->swap_notify(bo);
1839 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1844 * Unreserve without putting on LRU to avoid swapping out an
1845 * already swapped buffer.
1848 atomic_set(&bo->reserved, 0);
1849 wake_up_all(&bo->event_queue);
1850 kref_put(&bo->list_kref, ttm_bo_release_list);
1854 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1856 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1859 EXPORT_SYMBOL(ttm_bo_swapout_all);