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_manager_debug(struct ttm_bo_global *glob,
75 struct ttm_mem_type_manager *man)
77 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
78 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
79 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
80 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
81 printk(KERN_ERR TTM_PFX " io_offset: 0x%08lX\n", man->io_offset);
82 printk(KERN_ERR TTM_PFX " io_size: %ld\n", man->io_size);
83 printk(KERN_ERR TTM_PFX " size: %ld\n", (unsigned long)man->size);
84 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
85 man->available_caching);
86 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
87 man->default_caching);
88 spin_lock(&glob->lru_lock);
89 drm_mm_debug_table(&man->manager, TTM_PFX);
90 spin_unlock(&glob->lru_lock);
93 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
94 struct ttm_placement *placement)
96 struct ttm_bo_device *bdev = bo->bdev;
97 struct ttm_bo_global *glob = bo->glob;
98 struct ttm_mem_type_manager *man;
101 printk(KERN_ERR TTM_PFX "No space for %p (%ld pages, %ldK, %ldM)\n",
102 bo, bo->mem.num_pages, bo->mem.size >> 10,
104 for (i = 0; i < placement->num_placement; i++) {
105 ret = ttm_mem_type_from_flags(placement->placement[i],
109 man = &bdev->man[mem_type];
110 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
111 i, placement->placement[i], mem_type);
112 ttm_mem_type_manager_debug(glob, man);
116 static ssize_t ttm_bo_global_show(struct kobject *kobj,
117 struct attribute *attr,
120 struct ttm_bo_global *glob =
121 container_of(kobj, struct ttm_bo_global, kobj);
123 return snprintf(buffer, PAGE_SIZE, "%lu\n",
124 (unsigned long) atomic_read(&glob->bo_count));
127 static struct attribute *ttm_bo_global_attrs[] = {
132 static struct sysfs_ops ttm_bo_global_ops = {
133 .show = &ttm_bo_global_show
136 static struct kobj_type ttm_bo_glob_kobj_type = {
137 .release = &ttm_bo_global_kobj_release,
138 .sysfs_ops = &ttm_bo_global_ops,
139 .default_attrs = ttm_bo_global_attrs
143 static inline uint32_t ttm_bo_type_flags(unsigned type)
148 static void ttm_bo_release_list(struct kref *list_kref)
150 struct ttm_buffer_object *bo =
151 container_of(list_kref, struct ttm_buffer_object, list_kref);
152 struct ttm_bo_device *bdev = bo->bdev;
154 BUG_ON(atomic_read(&bo->list_kref.refcount));
155 BUG_ON(atomic_read(&bo->kref.refcount));
156 BUG_ON(atomic_read(&bo->cpu_writers));
157 BUG_ON(bo->sync_obj != NULL);
158 BUG_ON(bo->mem.mm_node != NULL);
159 BUG_ON(!list_empty(&bo->lru));
160 BUG_ON(!list_empty(&bo->ddestroy));
163 ttm_tt_destroy(bo->ttm);
164 atomic_dec(&bo->glob->bo_count);
168 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
173 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
179 ret = wait_event_interruptible(bo->event_queue,
180 atomic_read(&bo->reserved) == 0);
181 if (unlikely(ret != 0))
184 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
189 static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
191 struct ttm_bo_device *bdev = bo->bdev;
192 struct ttm_mem_type_manager *man;
194 BUG_ON(!atomic_read(&bo->reserved));
196 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
198 BUG_ON(!list_empty(&bo->lru));
200 man = &bdev->man[bo->mem.mem_type];
201 list_add_tail(&bo->lru, &man->lru);
202 kref_get(&bo->list_kref);
204 if (bo->ttm != NULL) {
205 list_add_tail(&bo->swap, &bo->glob->swap_lru);
206 kref_get(&bo->list_kref);
212 * Call with the lru_lock held.
215 static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
219 if (!list_empty(&bo->swap)) {
220 list_del_init(&bo->swap);
223 if (!list_empty(&bo->lru)) {
224 list_del_init(&bo->lru);
229 * TODO: Add a driver hook to delete from
230 * driver-specific LRU's here.
236 int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
238 bool no_wait, bool use_sequence, uint32_t sequence)
240 struct ttm_bo_global *glob = bo->glob;
243 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
244 if (use_sequence && bo->seq_valid &&
245 (sequence - bo->val_seq < (1 << 31))) {
252 spin_unlock(&glob->lru_lock);
253 ret = ttm_bo_wait_unreserved(bo, interruptible);
254 spin_lock(&glob->lru_lock);
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 int ttm_bo_reserve(struct ttm_buffer_object *bo,
278 bool no_wait, bool use_sequence, uint32_t sequence)
280 struct ttm_bo_global *glob = bo->glob;
284 spin_lock(&glob->lru_lock);
285 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
287 if (likely(ret == 0))
288 put_count = ttm_bo_del_from_lru(bo);
289 spin_unlock(&glob->lru_lock);
292 kref_put(&bo->list_kref, ttm_bo_ref_bug);
297 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
299 struct ttm_bo_global *glob = bo->glob;
301 spin_lock(&glob->lru_lock);
302 ttm_bo_add_to_lru(bo);
303 atomic_set(&bo->reserved, 0);
304 wake_up_all(&bo->event_queue);
305 spin_unlock(&glob->lru_lock);
307 EXPORT_SYMBOL(ttm_bo_unreserve);
310 * Call bo->mutex locked.
312 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
314 struct ttm_bo_device *bdev = bo->bdev;
315 struct ttm_bo_global *glob = bo->glob;
317 uint32_t page_flags = 0;
319 TTM_ASSERT_LOCKED(&bo->mutex);
322 if (bdev->need_dma32)
323 page_flags |= TTM_PAGE_FLAG_DMA32;
326 case ttm_bo_type_device:
328 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
329 case ttm_bo_type_kernel:
330 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
331 page_flags, glob->dummy_read_page);
332 if (unlikely(bo->ttm == NULL))
335 case ttm_bo_type_user:
336 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
337 page_flags | TTM_PAGE_FLAG_USER,
338 glob->dummy_read_page);
339 if (unlikely(bo->ttm == NULL)) {
344 ret = ttm_tt_set_user(bo->ttm, current,
345 bo->buffer_start, bo->num_pages);
346 if (unlikely(ret != 0))
347 ttm_tt_destroy(bo->ttm);
350 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
358 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
359 struct ttm_mem_reg *mem,
360 bool evict, bool interruptible, bool no_wait)
362 struct ttm_bo_device *bdev = bo->bdev;
363 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
364 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
365 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
366 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
369 if (old_is_pci || new_is_pci ||
370 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
371 ttm_bo_unmap_virtual(bo);
374 * Create and bind a ttm if required.
377 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
378 ret = ttm_bo_add_ttm(bo, false);
382 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
386 if (mem->mem_type != TTM_PL_SYSTEM) {
387 ret = ttm_tt_bind(bo->ttm, mem);
392 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
400 if (bdev->driver->move_notify)
401 bdev->driver->move_notify(bo, mem);
403 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
404 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
405 ret = ttm_bo_move_ttm(bo, evict, no_wait, mem);
406 else if (bdev->driver->move)
407 ret = bdev->driver->move(bo, evict, interruptible,
410 ret = ttm_bo_move_memcpy(bo, evict, no_wait, mem);
417 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
419 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
423 if (bo->mem.mm_node) {
424 spin_lock(&bo->lock);
425 bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) +
426 bdev->man[bo->mem.mem_type].gpu_offset;
427 bo->cur_placement = bo->mem.placement;
428 spin_unlock(&bo->lock);
434 new_man = &bdev->man[bo->mem.mem_type];
435 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
436 ttm_tt_unbind(bo->ttm);
437 ttm_tt_destroy(bo->ttm);
445 * If bo idle, remove from delayed- and lru lists, and unref.
446 * If not idle, and already on delayed list, do nothing.
447 * If not idle, and not on delayed list, put on delayed list,
448 * up the list_kref and schedule a delayed list check.
451 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
453 struct ttm_bo_device *bdev = bo->bdev;
454 struct ttm_bo_global *glob = bo->glob;
455 struct ttm_bo_driver *driver = bdev->driver;
458 spin_lock(&bo->lock);
459 (void) ttm_bo_wait(bo, false, false, !remove_all);
464 spin_unlock(&bo->lock);
466 spin_lock(&glob->lru_lock);
467 ret = ttm_bo_reserve_locked(bo, false, false, false, 0);
470 ttm_tt_unbind(bo->ttm);
472 if (!list_empty(&bo->ddestroy)) {
473 list_del_init(&bo->ddestroy);
474 kref_put(&bo->list_kref, ttm_bo_ref_bug);
476 if (bo->mem.mm_node) {
477 bo->mem.mm_node->private = NULL;
478 drm_mm_put_block(bo->mem.mm_node);
479 bo->mem.mm_node = NULL;
481 put_count = ttm_bo_del_from_lru(bo);
482 spin_unlock(&glob->lru_lock);
484 atomic_set(&bo->reserved, 0);
487 kref_put(&bo->list_kref, ttm_bo_release_list);
492 spin_lock(&glob->lru_lock);
493 if (list_empty(&bo->ddestroy)) {
494 void *sync_obj = bo->sync_obj;
495 void *sync_obj_arg = bo->sync_obj_arg;
497 kref_get(&bo->list_kref);
498 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
499 spin_unlock(&glob->lru_lock);
500 spin_unlock(&bo->lock);
503 driver->sync_obj_flush(sync_obj, sync_obj_arg);
504 schedule_delayed_work(&bdev->wq,
505 ((HZ / 100) < 1) ? 1 : HZ / 100);
509 spin_unlock(&glob->lru_lock);
510 spin_unlock(&bo->lock);
518 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
519 * encountered buffers.
522 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
524 struct ttm_bo_global *glob = bdev->glob;
525 struct ttm_buffer_object *entry, *nentry;
526 struct list_head *list, *next;
529 spin_lock(&glob->lru_lock);
530 list_for_each_safe(list, next, &bdev->ddestroy) {
531 entry = list_entry(list, struct ttm_buffer_object, ddestroy);
535 * Protect the next list entry from destruction while we
536 * unlock the lru_lock.
539 if (next != &bdev->ddestroy) {
540 nentry = list_entry(next, struct ttm_buffer_object,
542 kref_get(&nentry->list_kref);
544 kref_get(&entry->list_kref);
546 spin_unlock(&glob->lru_lock);
547 ret = ttm_bo_cleanup_refs(entry, remove_all);
548 kref_put(&entry->list_kref, ttm_bo_release_list);
550 spin_lock(&glob->lru_lock);
552 bool next_onlist = !list_empty(next);
553 spin_unlock(&glob->lru_lock);
554 kref_put(&nentry->list_kref, ttm_bo_release_list);
555 spin_lock(&glob->lru_lock);
557 * Someone might have raced us and removed the
558 * next entry from the list. We don't bother restarting
568 ret = !list_empty(&bdev->ddestroy);
569 spin_unlock(&glob->lru_lock);
574 static void ttm_bo_delayed_workqueue(struct work_struct *work)
576 struct ttm_bo_device *bdev =
577 container_of(work, struct ttm_bo_device, wq.work);
579 if (ttm_bo_delayed_delete(bdev, false)) {
580 schedule_delayed_work(&bdev->wq,
581 ((HZ / 100) < 1) ? 1 : HZ / 100);
585 static void ttm_bo_release(struct kref *kref)
587 struct ttm_buffer_object *bo =
588 container_of(kref, struct ttm_buffer_object, kref);
589 struct ttm_bo_device *bdev = bo->bdev;
591 if (likely(bo->vm_node != NULL)) {
592 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
593 drm_mm_put_block(bo->vm_node);
596 write_unlock(&bdev->vm_lock);
597 ttm_bo_cleanup_refs(bo, false);
598 kref_put(&bo->list_kref, ttm_bo_release_list);
599 write_lock(&bdev->vm_lock);
602 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
604 struct ttm_buffer_object *bo = *p_bo;
605 struct ttm_bo_device *bdev = bo->bdev;
608 write_lock(&bdev->vm_lock);
609 kref_put(&bo->kref, ttm_bo_release);
610 write_unlock(&bdev->vm_lock);
612 EXPORT_SYMBOL(ttm_bo_unref);
614 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
617 struct ttm_bo_device *bdev = bo->bdev;
618 struct ttm_bo_global *glob = bo->glob;
619 struct ttm_mem_reg evict_mem;
620 struct ttm_placement placement;
623 spin_lock(&bo->lock);
624 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
625 spin_unlock(&bo->lock);
627 if (unlikely(ret != 0)) {
628 if (ret != -ERESTARTSYS) {
629 printk(KERN_ERR TTM_PFX
630 "Failed to expire sync object before "
631 "buffer eviction.\n");
636 BUG_ON(!atomic_read(&bo->reserved));
639 evict_mem.mm_node = NULL;
643 placement.num_placement = 0;
644 placement.num_busy_placement = 0;
645 bdev->driver->evict_flags(bo, &placement);
646 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
649 if (ret != -ERESTARTSYS) {
650 printk(KERN_ERR TTM_PFX
651 "Failed to find memory space for "
652 "buffer 0x%p eviction.\n", bo);
653 ttm_bo_mem_space_debug(bo, &placement);
658 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
661 if (ret != -ERESTARTSYS)
662 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
663 spin_lock(&glob->lru_lock);
664 if (evict_mem.mm_node) {
665 evict_mem.mm_node->private = NULL;
666 drm_mm_put_block(evict_mem.mm_node);
667 evict_mem.mm_node = NULL;
669 spin_unlock(&glob->lru_lock);
677 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
679 bool interruptible, bool no_wait)
681 struct ttm_bo_global *glob = bdev->glob;
682 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
683 struct ttm_buffer_object *bo;
684 int ret, put_count = 0;
686 spin_lock(&glob->lru_lock);
687 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
688 kref_get(&bo->list_kref);
689 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, false, 0);
690 if (likely(ret == 0))
691 put_count = ttm_bo_del_from_lru(bo);
692 spin_unlock(&glob->lru_lock);
693 if (unlikely(ret != 0))
696 kref_put(&bo->list_kref, ttm_bo_ref_bug);
697 ret = ttm_bo_evict(bo, interruptible, no_wait);
698 ttm_bo_unreserve(bo);
699 kref_put(&bo->list_kref, ttm_bo_release_list);
703 static int ttm_bo_man_get_node(struct ttm_buffer_object *bo,
704 struct ttm_mem_type_manager *man,
705 struct ttm_placement *placement,
706 struct ttm_mem_reg *mem,
707 struct drm_mm_node **node)
709 struct ttm_bo_global *glob = bo->glob;
713 lpfn = placement->lpfn;
718 ret = drm_mm_pre_get(&man->manager);
722 spin_lock(&glob->lru_lock);
723 *node = drm_mm_search_free_in_range(&man->manager,
724 mem->num_pages, mem->page_alignment,
725 placement->fpfn, lpfn, 1);
726 if (unlikely(*node == NULL)) {
727 spin_unlock(&glob->lru_lock);
730 *node = drm_mm_get_block_atomic_range(*node, mem->num_pages,
734 spin_unlock(&glob->lru_lock);
735 } while (*node == NULL);
740 * Repeatedly evict memory from the LRU for @mem_type until we create enough
741 * space, or we've evicted everything and there isn't enough space.
743 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
745 struct ttm_placement *placement,
746 struct ttm_mem_reg *mem,
747 bool interruptible, bool no_wait)
749 struct ttm_bo_device *bdev = bo->bdev;
750 struct ttm_bo_global *glob = bdev->glob;
751 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
752 struct drm_mm_node *node;
756 ret = ttm_bo_man_get_node(bo, man, placement, mem, &node);
757 if (unlikely(ret != 0))
761 spin_lock(&glob->lru_lock);
762 if (list_empty(&man->lru)) {
763 spin_unlock(&glob->lru_lock);
766 spin_unlock(&glob->lru_lock);
767 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
769 if (unlikely(ret != 0))
775 mem->mem_type = mem_type;
779 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
780 uint32_t cur_placement,
781 uint32_t proposed_placement)
783 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
784 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
787 * Keep current caching if possible.
790 if ((cur_placement & caching) != 0)
791 result |= (cur_placement & caching);
792 else if ((man->default_caching & caching) != 0)
793 result |= man->default_caching;
794 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
795 result |= TTM_PL_FLAG_CACHED;
796 else if ((TTM_PL_FLAG_WC & caching) != 0)
797 result |= TTM_PL_FLAG_WC;
798 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
799 result |= TTM_PL_FLAG_UNCACHED;
804 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
807 uint32_t proposed_placement,
808 uint32_t *masked_placement)
810 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
812 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
815 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
818 if ((proposed_placement & man->available_caching) == 0)
821 cur_flags |= (proposed_placement & man->available_caching);
823 *masked_placement = cur_flags;
828 * Creates space for memory region @mem according to its type.
830 * This function first searches for free space in compatible memory types in
831 * the priority order defined by the driver. If free space isn't found, then
832 * ttm_bo_mem_force_space is attempted in priority order to evict and find
835 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
836 struct ttm_placement *placement,
837 struct ttm_mem_reg *mem,
838 bool interruptible, bool no_wait)
840 struct ttm_bo_device *bdev = bo->bdev;
841 struct ttm_mem_type_manager *man;
842 uint32_t mem_type = TTM_PL_SYSTEM;
843 uint32_t cur_flags = 0;
844 bool type_found = false;
845 bool type_ok = false;
846 bool has_erestartsys = false;
847 struct drm_mm_node *node = NULL;
851 for (i = 0; i <= placement->num_placement; ++i) {
852 ret = ttm_mem_type_from_flags(placement->placement[i],
856 man = &bdev->man[mem_type];
858 type_ok = ttm_bo_mt_compatible(man,
859 bo->type == ttm_bo_type_user,
861 placement->placement[i],
867 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
870 * Use the access and other non-mapping-related flag bits from
871 * the memory placement flags to the current flags
873 ttm_flag_masked(&cur_flags, placement->placement[i],
874 ~TTM_PL_MASK_MEMTYPE);
876 if (mem_type == TTM_PL_SYSTEM)
879 if (man->has_type && man->use_type) {
881 ret = ttm_bo_man_get_node(bo, man, placement, mem,
890 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
892 mem->mem_type = mem_type;
893 mem->placement = cur_flags;
902 for (i = 0; i <= placement->num_busy_placement; ++i) {
903 ret = ttm_mem_type_from_flags(placement->placement[i],
907 man = &bdev->man[mem_type];
910 if (!ttm_bo_mt_compatible(man,
911 bo->type == ttm_bo_type_user,
913 placement->placement[i],
917 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
920 * Use the access and other non-mapping-related flag bits from
921 * the memory placement flags to the current flags
923 ttm_flag_masked(&cur_flags, placement->placement[i],
924 ~TTM_PL_MASK_MEMTYPE);
926 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
927 interruptible, no_wait);
928 if (ret == 0 && mem->mm_node) {
929 mem->placement = cur_flags;
930 mem->mm_node->private = bo;
933 if (ret == -ERESTARTSYS)
934 has_erestartsys = true;
936 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
939 EXPORT_SYMBOL(ttm_bo_mem_space);
941 int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
943 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
946 return wait_event_interruptible(bo->event_queue,
947 atomic_read(&bo->cpu_writers) == 0);
950 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
951 struct ttm_placement *placement,
952 bool interruptible, bool no_wait)
954 struct ttm_bo_global *glob = bo->glob;
956 struct ttm_mem_reg mem;
958 BUG_ON(!atomic_read(&bo->reserved));
961 * FIXME: It's possible to pipeline buffer moves.
962 * Have the driver move function wait for idle when necessary,
963 * instead of doing it here.
965 spin_lock(&bo->lock);
966 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
967 spin_unlock(&bo->lock);
970 mem.num_pages = bo->num_pages;
971 mem.size = mem.num_pages << PAGE_SHIFT;
972 mem.page_alignment = bo->mem.page_alignment;
974 * Determine where to move the buffer.
976 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait);
979 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait);
981 if (ret && mem.mm_node) {
982 spin_lock(&glob->lru_lock);
983 mem.mm_node->private = NULL;
984 drm_mm_put_block(mem.mm_node);
985 spin_unlock(&glob->lru_lock);
990 static int ttm_bo_mem_compat(struct ttm_placement *placement,
991 struct ttm_mem_reg *mem)
995 for (i = 0; i < placement->num_placement; i++) {
996 if ((placement->placement[i] & mem->placement &
997 TTM_PL_MASK_CACHING) &&
998 (placement->placement[i] & mem->placement &
1005 int ttm_buffer_object_validate(struct ttm_buffer_object *bo,
1006 struct ttm_placement *placement,
1007 bool interruptible, bool no_wait)
1011 BUG_ON(!atomic_read(&bo->reserved));
1012 /* Check that range is valid */
1013 if (placement->lpfn || placement->fpfn)
1014 if (placement->fpfn > placement->lpfn ||
1015 (placement->lpfn - placement->fpfn) < bo->num_pages)
1018 * Check whether we need to move buffer.
1020 ret = ttm_bo_mem_compat(placement, &bo->mem);
1022 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait);
1027 * Use the access and other non-mapping-related flag bits from
1028 * the compatible memory placement flags to the active flags
1030 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1031 ~TTM_PL_MASK_MEMTYPE);
1034 * We might need to add a TTM.
1036 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1037 ret = ttm_bo_add_ttm(bo, true);
1043 EXPORT_SYMBOL(ttm_buffer_object_validate);
1046 ttm_bo_check_placement(struct ttm_buffer_object *bo,
1047 uint32_t set_flags, uint32_t clr_flags)
1049 uint32_t new_mask = set_flags | clr_flags;
1051 if ((bo->type == ttm_bo_type_user) &&
1052 (clr_flags & TTM_PL_FLAG_CACHED)) {
1053 printk(KERN_ERR TTM_PFX
1054 "User buffers require cache-coherent memory.\n");
1058 if (!capable(CAP_SYS_ADMIN)) {
1059 if (new_mask & TTM_PL_FLAG_NO_EVICT) {
1060 printk(KERN_ERR TTM_PFX "Need to be root to modify"
1061 " NO_EVICT status.\n");
1065 if ((clr_flags & bo->mem.placement & TTM_PL_MASK_MEMTYPE) &&
1066 (bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1067 printk(KERN_ERR TTM_PFX
1068 "Incompatible memory specification"
1069 " for NO_EVICT buffer.\n");
1076 int ttm_buffer_object_init(struct ttm_bo_device *bdev,
1077 struct ttm_buffer_object *bo,
1079 enum ttm_bo_type type,
1081 uint32_t page_alignment,
1082 unsigned long buffer_start,
1084 struct file *persistant_swap_storage,
1086 void (*destroy) (struct ttm_buffer_object *))
1089 unsigned long num_pages;
1090 uint32_t placements[8];
1091 struct ttm_placement placement;
1093 size += buffer_start & ~PAGE_MASK;
1094 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1095 if (num_pages == 0) {
1096 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1099 bo->destroy = destroy;
1101 spin_lock_init(&bo->lock);
1102 kref_init(&bo->kref);
1103 kref_init(&bo->list_kref);
1104 atomic_set(&bo->cpu_writers, 0);
1105 atomic_set(&bo->reserved, 1);
1106 init_waitqueue_head(&bo->event_queue);
1107 INIT_LIST_HEAD(&bo->lru);
1108 INIT_LIST_HEAD(&bo->ddestroy);
1109 INIT_LIST_HEAD(&bo->swap);
1111 bo->glob = bdev->glob;
1113 bo->num_pages = num_pages;
1114 bo->mem.mem_type = TTM_PL_SYSTEM;
1115 bo->mem.num_pages = bo->num_pages;
1116 bo->mem.mm_node = NULL;
1117 bo->mem.page_alignment = page_alignment;
1118 bo->buffer_start = buffer_start & PAGE_MASK;
1120 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1121 bo->seq_valid = false;
1122 bo->persistant_swap_storage = persistant_swap_storage;
1123 bo->acc_size = acc_size;
1124 atomic_inc(&bo->glob->bo_count);
1126 ret = ttm_bo_check_placement(bo, flags, 0ULL);
1127 if (unlikely(ret != 0))
1131 * If no caching attributes are set, accept any form of caching.
1134 if ((flags & TTM_PL_MASK_CACHING) == 0)
1135 flags |= TTM_PL_MASK_CACHING;
1138 * For ttm_bo_type_device buffers, allocate
1139 * address space from the device.
1142 if (bo->type == ttm_bo_type_device) {
1143 ret = ttm_bo_setup_vm(bo);
1150 for (i = 0, c = 0; i <= TTM_PL_PRIV5; i++)
1151 if (flags & (1 << i))
1152 placements[c++] = (flags & ~TTM_PL_MASK_MEM) | (1 << i);
1153 placement.placement = placements;
1154 placement.num_placement = c;
1155 placement.busy_placement = placements;
1156 placement.num_busy_placement = c;
1157 ret = ttm_buffer_object_validate(bo, &placement, interruptible, false);
1161 ttm_bo_unreserve(bo);
1165 ttm_bo_unreserve(bo);
1170 EXPORT_SYMBOL(ttm_buffer_object_init);
1172 static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
1173 unsigned long num_pages)
1175 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1178 return glob->ttm_bo_size + 2 * page_array_size;
1181 int ttm_buffer_object_create(struct ttm_bo_device *bdev,
1183 enum ttm_bo_type type,
1185 uint32_t page_alignment,
1186 unsigned long buffer_start,
1188 struct file *persistant_swap_storage,
1189 struct ttm_buffer_object **p_bo)
1191 struct ttm_buffer_object *bo;
1192 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1196 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
1197 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1198 if (unlikely(ret != 0))
1201 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1203 if (unlikely(bo == NULL)) {
1204 ttm_mem_global_free(mem_glob, acc_size);
1208 ret = ttm_buffer_object_init(bdev, bo, size, type, flags,
1209 page_alignment, buffer_start,
1211 persistant_swap_storage, acc_size, NULL);
1212 if (likely(ret == 0))
1218 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1219 unsigned mem_type, bool allow_errors)
1221 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1222 struct ttm_bo_global *glob = bdev->glob;
1226 * Can't use standard list traversal since we're unlocking.
1229 spin_lock(&glob->lru_lock);
1230 while (!list_empty(&man->lru)) {
1231 spin_unlock(&glob->lru_lock);
1232 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1237 printk(KERN_ERR TTM_PFX
1238 "Cleanup eviction failed\n");
1241 spin_lock(&glob->lru_lock);
1243 spin_unlock(&glob->lru_lock);
1247 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1249 struct ttm_bo_global *glob = bdev->glob;
1250 struct ttm_mem_type_manager *man;
1253 if (mem_type >= TTM_NUM_MEM_TYPES) {
1254 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1257 man = &bdev->man[mem_type];
1259 if (!man->has_type) {
1260 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1261 "memory manager type %u\n", mem_type);
1265 man->use_type = false;
1266 man->has_type = false;
1270 ttm_bo_force_list_clean(bdev, mem_type, false);
1272 spin_lock(&glob->lru_lock);
1273 if (drm_mm_clean(&man->manager))
1274 drm_mm_takedown(&man->manager);
1278 spin_unlock(&glob->lru_lock);
1283 EXPORT_SYMBOL(ttm_bo_clean_mm);
1285 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1287 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1289 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1290 printk(KERN_ERR TTM_PFX
1291 "Illegal memory manager memory type %u.\n",
1296 if (!man->has_type) {
1297 printk(KERN_ERR TTM_PFX
1298 "Memory type %u has not been initialized.\n",
1303 return ttm_bo_force_list_clean(bdev, mem_type, true);
1305 EXPORT_SYMBOL(ttm_bo_evict_mm);
1307 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1308 unsigned long p_size)
1311 struct ttm_mem_type_manager *man;
1313 if (type >= TTM_NUM_MEM_TYPES) {
1314 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1318 man = &bdev->man[type];
1319 if (man->has_type) {
1320 printk(KERN_ERR TTM_PFX
1321 "Memory manager already initialized for type %d\n",
1326 ret = bdev->driver->init_mem_type(bdev, type, man);
1331 if (type != TTM_PL_SYSTEM) {
1333 printk(KERN_ERR TTM_PFX
1334 "Zero size memory manager type %d\n",
1338 ret = drm_mm_init(&man->manager, 0, p_size);
1342 man->has_type = true;
1343 man->use_type = true;
1346 INIT_LIST_HEAD(&man->lru);
1350 EXPORT_SYMBOL(ttm_bo_init_mm);
1352 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1354 struct ttm_bo_global *glob =
1355 container_of(kobj, struct ttm_bo_global, kobj);
1357 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1358 __free_page(glob->dummy_read_page);
1362 void ttm_bo_global_release(struct ttm_global_reference *ref)
1364 struct ttm_bo_global *glob = ref->object;
1366 kobject_del(&glob->kobj);
1367 kobject_put(&glob->kobj);
1369 EXPORT_SYMBOL(ttm_bo_global_release);
1371 int ttm_bo_global_init(struct ttm_global_reference *ref)
1373 struct ttm_bo_global_ref *bo_ref =
1374 container_of(ref, struct ttm_bo_global_ref, ref);
1375 struct ttm_bo_global *glob = ref->object;
1378 mutex_init(&glob->device_list_mutex);
1379 spin_lock_init(&glob->lru_lock);
1380 glob->mem_glob = bo_ref->mem_glob;
1381 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1383 if (unlikely(glob->dummy_read_page == NULL)) {
1388 INIT_LIST_HEAD(&glob->swap_lru);
1389 INIT_LIST_HEAD(&glob->device_list);
1391 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1392 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1393 if (unlikely(ret != 0)) {
1394 printk(KERN_ERR TTM_PFX
1395 "Could not register buffer object swapout.\n");
1399 glob->ttm_bo_extra_size =
1400 ttm_round_pot(sizeof(struct ttm_tt)) +
1401 ttm_round_pot(sizeof(struct ttm_backend));
1403 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1404 ttm_round_pot(sizeof(struct ttm_buffer_object));
1406 atomic_set(&glob->bo_count, 0);
1408 kobject_init(&glob->kobj, &ttm_bo_glob_kobj_type);
1409 ret = kobject_add(&glob->kobj, ttm_get_kobj(), "buffer_objects");
1410 if (unlikely(ret != 0))
1411 kobject_put(&glob->kobj);
1414 __free_page(glob->dummy_read_page);
1419 EXPORT_SYMBOL(ttm_bo_global_init);
1422 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1425 unsigned i = TTM_NUM_MEM_TYPES;
1426 struct ttm_mem_type_manager *man;
1427 struct ttm_bo_global *glob = bdev->glob;
1430 man = &bdev->man[i];
1431 if (man->has_type) {
1432 man->use_type = false;
1433 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1435 printk(KERN_ERR TTM_PFX
1436 "DRM memory manager type %d "
1437 "is not clean.\n", i);
1439 man->has_type = false;
1443 mutex_lock(&glob->device_list_mutex);
1444 list_del(&bdev->device_list);
1445 mutex_unlock(&glob->device_list_mutex);
1447 if (!cancel_delayed_work(&bdev->wq))
1448 flush_scheduled_work();
1450 while (ttm_bo_delayed_delete(bdev, true))
1453 spin_lock(&glob->lru_lock);
1454 if (list_empty(&bdev->ddestroy))
1455 TTM_DEBUG("Delayed destroy list was clean\n");
1457 if (list_empty(&bdev->man[0].lru))
1458 TTM_DEBUG("Swap list was clean\n");
1459 spin_unlock(&glob->lru_lock);
1461 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1462 write_lock(&bdev->vm_lock);
1463 drm_mm_takedown(&bdev->addr_space_mm);
1464 write_unlock(&bdev->vm_lock);
1468 EXPORT_SYMBOL(ttm_bo_device_release);
1470 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1471 struct ttm_bo_global *glob,
1472 struct ttm_bo_driver *driver,
1473 uint64_t file_page_offset,
1478 rwlock_init(&bdev->vm_lock);
1479 bdev->driver = driver;
1481 memset(bdev->man, 0, sizeof(bdev->man));
1484 * Initialize the system memory buffer type.
1485 * Other types need to be driver / IOCTL initialized.
1487 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1488 if (unlikely(ret != 0))
1491 bdev->addr_space_rb = RB_ROOT;
1492 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1493 if (unlikely(ret != 0))
1494 goto out_no_addr_mm;
1496 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1497 bdev->nice_mode = true;
1498 INIT_LIST_HEAD(&bdev->ddestroy);
1499 bdev->dev_mapping = NULL;
1501 bdev->need_dma32 = need_dma32;
1503 mutex_lock(&glob->device_list_mutex);
1504 list_add_tail(&bdev->device_list, &glob->device_list);
1505 mutex_unlock(&glob->device_list_mutex);
1509 ttm_bo_clean_mm(bdev, 0);
1513 EXPORT_SYMBOL(ttm_bo_device_init);
1516 * buffer object vm functions.
1519 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1521 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1523 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1524 if (mem->mem_type == TTM_PL_SYSTEM)
1527 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1530 if (mem->placement & TTM_PL_FLAG_CACHED)
1536 int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
1537 struct ttm_mem_reg *mem,
1538 unsigned long *bus_base,
1539 unsigned long *bus_offset, unsigned long *bus_size)
1541 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1544 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1547 if (ttm_mem_reg_is_pci(bdev, mem)) {
1548 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
1549 *bus_size = mem->num_pages << PAGE_SHIFT;
1550 *bus_base = man->io_offset;
1556 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1558 struct ttm_bo_device *bdev = bo->bdev;
1559 loff_t offset = (loff_t) bo->addr_space_offset;
1560 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1562 if (!bdev->dev_mapping)
1565 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1567 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1569 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1571 struct ttm_bo_device *bdev = bo->bdev;
1572 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1573 struct rb_node *parent = NULL;
1574 struct ttm_buffer_object *cur_bo;
1575 unsigned long offset = bo->vm_node->start;
1576 unsigned long cur_offset;
1580 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1581 cur_offset = cur_bo->vm_node->start;
1582 if (offset < cur_offset)
1583 cur = &parent->rb_left;
1584 else if (offset > cur_offset)
1585 cur = &parent->rb_right;
1590 rb_link_node(&bo->vm_rb, parent, cur);
1591 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1597 * @bo: the buffer to allocate address space for
1599 * Allocate address space in the drm device so that applications
1600 * can mmap the buffer and access the contents. This only
1601 * applies to ttm_bo_type_device objects as others are not
1602 * placed in the drm device address space.
1605 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1607 struct ttm_bo_device *bdev = bo->bdev;
1611 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1612 if (unlikely(ret != 0))
1615 write_lock(&bdev->vm_lock);
1616 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1617 bo->mem.num_pages, 0, 0);
1619 if (unlikely(bo->vm_node == NULL)) {
1624 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1625 bo->mem.num_pages, 0);
1627 if (unlikely(bo->vm_node == NULL)) {
1628 write_unlock(&bdev->vm_lock);
1632 ttm_bo_vm_insert_rb(bo);
1633 write_unlock(&bdev->vm_lock);
1634 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1638 write_unlock(&bdev->vm_lock);
1642 int ttm_bo_wait(struct ttm_buffer_object *bo,
1643 bool lazy, bool interruptible, bool no_wait)
1645 struct ttm_bo_driver *driver = bo->bdev->driver;
1650 if (likely(bo->sync_obj == NULL))
1653 while (bo->sync_obj) {
1655 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1656 void *tmp_obj = bo->sync_obj;
1657 bo->sync_obj = NULL;
1658 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1659 spin_unlock(&bo->lock);
1660 driver->sync_obj_unref(&tmp_obj);
1661 spin_lock(&bo->lock);
1668 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1669 sync_obj_arg = bo->sync_obj_arg;
1670 spin_unlock(&bo->lock);
1671 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1672 lazy, interruptible);
1673 if (unlikely(ret != 0)) {
1674 driver->sync_obj_unref(&sync_obj);
1675 spin_lock(&bo->lock);
1678 spin_lock(&bo->lock);
1679 if (likely(bo->sync_obj == sync_obj &&
1680 bo->sync_obj_arg == sync_obj_arg)) {
1681 void *tmp_obj = bo->sync_obj;
1682 bo->sync_obj = NULL;
1683 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1685 spin_unlock(&bo->lock);
1686 driver->sync_obj_unref(&sync_obj);
1687 driver->sync_obj_unref(&tmp_obj);
1688 spin_lock(&bo->lock);
1690 spin_unlock(&bo->lock);
1691 driver->sync_obj_unref(&sync_obj);
1692 spin_lock(&bo->lock);
1697 EXPORT_SYMBOL(ttm_bo_wait);
1699 void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo)
1701 atomic_set(&bo->reserved, 0);
1702 wake_up_all(&bo->event_queue);
1705 int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible,
1710 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
1713 else if (interruptible) {
1714 ret = wait_event_interruptible
1715 (bo->event_queue, atomic_read(&bo->reserved) == 0);
1716 if (unlikely(ret != 0))
1719 wait_event(bo->event_queue,
1720 atomic_read(&bo->reserved) == 0);
1726 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1731 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1732 * makes sure the lru lists are updated.
1735 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1736 if (unlikely(ret != 0))
1738 spin_lock(&bo->lock);
1739 ret = ttm_bo_wait(bo, false, true, no_wait);
1740 spin_unlock(&bo->lock);
1741 if (likely(ret == 0))
1742 atomic_inc(&bo->cpu_writers);
1743 ttm_bo_unreserve(bo);
1747 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1749 if (atomic_dec_and_test(&bo->cpu_writers))
1750 wake_up_all(&bo->event_queue);
1754 * A buffer object shrink method that tries to swap out the first
1755 * buffer object on the bo_global::swap_lru list.
1758 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1760 struct ttm_bo_global *glob =
1761 container_of(shrink, struct ttm_bo_global, shrink);
1762 struct ttm_buffer_object *bo;
1765 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1767 spin_lock(&glob->lru_lock);
1768 while (ret == -EBUSY) {
1769 if (unlikely(list_empty(&glob->swap_lru))) {
1770 spin_unlock(&glob->lru_lock);
1774 bo = list_first_entry(&glob->swap_lru,
1775 struct ttm_buffer_object, swap);
1776 kref_get(&bo->list_kref);
1779 * Reserve buffer. Since we unlock while sleeping, we need
1780 * to re-check that nobody removed us from the swap-list while
1784 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1785 if (unlikely(ret == -EBUSY)) {
1786 spin_unlock(&glob->lru_lock);
1787 ttm_bo_wait_unreserved(bo, false);
1788 kref_put(&bo->list_kref, ttm_bo_release_list);
1789 spin_lock(&glob->lru_lock);
1794 put_count = ttm_bo_del_from_lru(bo);
1795 spin_unlock(&glob->lru_lock);
1798 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1801 * Wait for GPU, then move to system cached.
1804 spin_lock(&bo->lock);
1805 ret = ttm_bo_wait(bo, false, false, false);
1806 spin_unlock(&bo->lock);
1808 if (unlikely(ret != 0))
1811 if ((bo->mem.placement & swap_placement) != swap_placement) {
1812 struct ttm_mem_reg evict_mem;
1814 evict_mem = bo->mem;
1815 evict_mem.mm_node = NULL;
1816 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1817 evict_mem.mem_type = TTM_PL_SYSTEM;
1819 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1821 if (unlikely(ret != 0))
1825 ttm_bo_unmap_virtual(bo);
1828 * Swap out. Buffer will be swapped in again as soon as
1829 * anyone tries to access a ttm page.
1832 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1837 * Unreserve without putting on LRU to avoid swapping out an
1838 * already swapped buffer.
1841 atomic_set(&bo->reserved, 0);
1842 wake_up_all(&bo->event_queue);
1843 kref_put(&bo->list_kref, ttm_bo_release_list);
1847 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1849 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)