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 if (bo->mem.mm_node) {
468 ttm_bo_mem_put(bo, &bo->mem);
471 atomic_set(&bo->reserved, 0);
472 wake_up_all(&bo->event_queue);
473 spin_unlock(&glob->lru_lock);
478 * If bo idle, remove from delayed- and lru lists, and unref.
479 * If not idle, and already on delayed list, do nothing.
480 * If not idle, and not on delayed list, put on delayed list,
481 * up the list_kref and schedule a delayed list check.
484 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
486 struct ttm_bo_device *bdev = bo->bdev;
487 struct ttm_bo_global *glob = bo->glob;
488 struct ttm_bo_driver *driver = bdev->driver;
491 spin_lock(&bo->lock);
493 (void) ttm_bo_wait(bo, false, false, !remove_all);
498 spin_unlock(&bo->lock);
500 spin_lock(&glob->lru_lock);
501 ret = ttm_bo_reserve_locked(bo, false, !remove_all, false, 0);
504 * Someone else has the object reserved. Bail and retry.
507 if (unlikely(ret == -EBUSY)) {
508 spin_unlock(&glob->lru_lock);
509 spin_lock(&bo->lock);
514 * We can re-check for sync object without taking
515 * the bo::lock since setting the sync object requires
516 * also bo::reserved. A busy object at this point may
517 * be caused by another thread starting an accelerated
521 if (unlikely(bo->sync_obj)) {
522 atomic_set(&bo->reserved, 0);
523 wake_up_all(&bo->event_queue);
524 spin_unlock(&glob->lru_lock);
525 spin_lock(&bo->lock);
532 put_count = ttm_bo_del_from_lru(bo);
534 if (!list_empty(&bo->ddestroy)) {
535 list_del_init(&bo->ddestroy);
539 ttm_bo_cleanup_memtype_use(bo);
542 kref_put(&bo->list_kref, ttm_bo_ref_bug);
547 spin_lock(&glob->lru_lock);
548 if (list_empty(&bo->ddestroy)) {
549 void *sync_obj = bo->sync_obj;
550 void *sync_obj_arg = bo->sync_obj_arg;
552 kref_get(&bo->list_kref);
553 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
554 spin_unlock(&glob->lru_lock);
555 spin_unlock(&bo->lock);
558 driver->sync_obj_flush(sync_obj, sync_obj_arg);
559 schedule_delayed_work(&bdev->wq,
560 ((HZ / 100) < 1) ? 1 : HZ / 100);
564 spin_unlock(&glob->lru_lock);
565 spin_unlock(&bo->lock);
573 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
574 * encountered buffers.
577 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
579 struct ttm_bo_global *glob = bdev->glob;
580 struct ttm_buffer_object *entry = NULL;
583 spin_lock(&glob->lru_lock);
584 if (list_empty(&bdev->ddestroy))
587 entry = list_first_entry(&bdev->ddestroy,
588 struct ttm_buffer_object, ddestroy);
589 kref_get(&entry->list_kref);
592 struct ttm_buffer_object *nentry = NULL;
594 if (entry->ddestroy.next != &bdev->ddestroy) {
595 nentry = list_first_entry(&entry->ddestroy,
596 struct ttm_buffer_object, ddestroy);
597 kref_get(&nentry->list_kref);
600 spin_unlock(&glob->lru_lock);
601 ret = ttm_bo_cleanup_refs(entry, remove_all);
602 kref_put(&entry->list_kref, ttm_bo_release_list);
608 spin_lock(&glob->lru_lock);
609 if (list_empty(&entry->ddestroy))
614 spin_unlock(&glob->lru_lock);
617 kref_put(&entry->list_kref, ttm_bo_release_list);
621 static void ttm_bo_delayed_workqueue(struct work_struct *work)
623 struct ttm_bo_device *bdev =
624 container_of(work, struct ttm_bo_device, wq.work);
626 if (ttm_bo_delayed_delete(bdev, false)) {
627 schedule_delayed_work(&bdev->wq,
628 ((HZ / 100) < 1) ? 1 : HZ / 100);
632 static void ttm_bo_release(struct kref *kref)
634 struct ttm_buffer_object *bo =
635 container_of(kref, struct ttm_buffer_object, kref);
636 struct ttm_bo_device *bdev = bo->bdev;
638 if (likely(bo->vm_node != NULL)) {
639 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
640 drm_mm_put_block(bo->vm_node);
643 write_unlock(&bdev->vm_lock);
644 ttm_bo_cleanup_refs(bo, false);
645 kref_put(&bo->list_kref, ttm_bo_release_list);
646 write_lock(&bdev->vm_lock);
649 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
651 struct ttm_buffer_object *bo = *p_bo;
652 struct ttm_bo_device *bdev = bo->bdev;
655 write_lock(&bdev->vm_lock);
656 kref_put(&bo->kref, ttm_bo_release);
657 write_unlock(&bdev->vm_lock);
659 EXPORT_SYMBOL(ttm_bo_unref);
661 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
663 return cancel_delayed_work_sync(&bdev->wq);
665 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
667 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
670 schedule_delayed_work(&bdev->wq,
671 ((HZ / 100) < 1) ? 1 : HZ / 100);
673 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
675 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
676 bool no_wait_reserve, bool no_wait_gpu)
678 struct ttm_bo_device *bdev = bo->bdev;
679 struct ttm_mem_reg evict_mem;
680 struct ttm_placement placement;
683 spin_lock(&bo->lock);
684 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
685 spin_unlock(&bo->lock);
687 if (unlikely(ret != 0)) {
688 if (ret != -ERESTARTSYS) {
689 printk(KERN_ERR TTM_PFX
690 "Failed to expire sync object before "
691 "buffer eviction.\n");
696 BUG_ON(!atomic_read(&bo->reserved));
699 evict_mem.mm_node = NULL;
700 evict_mem.bus.io_reserved = false;
704 placement.num_placement = 0;
705 placement.num_busy_placement = 0;
706 bdev->driver->evict_flags(bo, &placement);
707 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
708 no_wait_reserve, no_wait_gpu);
710 if (ret != -ERESTARTSYS) {
711 printk(KERN_ERR TTM_PFX
712 "Failed to find memory space for "
713 "buffer 0x%p eviction.\n", bo);
714 ttm_bo_mem_space_debug(bo, &placement);
719 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
720 no_wait_reserve, no_wait_gpu);
722 if (ret != -ERESTARTSYS)
723 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
724 ttm_bo_mem_put(bo, &evict_mem);
732 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
734 bool interruptible, bool no_wait_reserve,
737 struct ttm_bo_global *glob = bdev->glob;
738 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
739 struct ttm_buffer_object *bo;
740 int ret, put_count = 0;
743 spin_lock(&glob->lru_lock);
744 if (list_empty(&man->lru)) {
745 spin_unlock(&glob->lru_lock);
749 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
750 kref_get(&bo->list_kref);
752 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
754 if (unlikely(ret == -EBUSY)) {
755 spin_unlock(&glob->lru_lock);
756 if (likely(!no_wait_gpu))
757 ret = ttm_bo_wait_unreserved(bo, interruptible);
759 kref_put(&bo->list_kref, ttm_bo_release_list);
762 * We *need* to retry after releasing the lru lock.
765 if (unlikely(ret != 0))
770 put_count = ttm_bo_del_from_lru(bo);
771 spin_unlock(&glob->lru_lock);
776 kref_put(&bo->list_kref, ttm_bo_ref_bug);
778 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
779 ttm_bo_unreserve(bo);
781 kref_put(&bo->list_kref, ttm_bo_release_list);
785 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
787 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
790 (*man->func->put_node)(man, mem);
792 EXPORT_SYMBOL(ttm_bo_mem_put);
795 * Repeatedly evict memory from the LRU for @mem_type until we create enough
796 * space, or we've evicted everything and there isn't enough space.
798 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
800 struct ttm_placement *placement,
801 struct ttm_mem_reg *mem,
803 bool no_wait_reserve,
806 struct ttm_bo_device *bdev = bo->bdev;
807 struct ttm_bo_global *glob = bdev->glob;
808 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
812 ret = (*man->func->get_node)(man, bo, placement, mem);
813 if (unlikely(ret != 0))
817 spin_lock(&glob->lru_lock);
818 if (list_empty(&man->lru)) {
819 spin_unlock(&glob->lru_lock);
822 spin_unlock(&glob->lru_lock);
823 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
824 no_wait_reserve, no_wait_gpu);
825 if (unlikely(ret != 0))
828 if (mem->mm_node == NULL)
830 mem->mem_type = mem_type;
834 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
835 uint32_t cur_placement,
836 uint32_t proposed_placement)
838 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
839 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
842 * Keep current caching if possible.
845 if ((cur_placement & caching) != 0)
846 result |= (cur_placement & caching);
847 else if ((man->default_caching & caching) != 0)
848 result |= man->default_caching;
849 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
850 result |= TTM_PL_FLAG_CACHED;
851 else if ((TTM_PL_FLAG_WC & caching) != 0)
852 result |= TTM_PL_FLAG_WC;
853 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
854 result |= TTM_PL_FLAG_UNCACHED;
859 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
862 uint32_t proposed_placement,
863 uint32_t *masked_placement)
865 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
867 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
870 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
873 if ((proposed_placement & man->available_caching) == 0)
876 cur_flags |= (proposed_placement & man->available_caching);
878 *masked_placement = cur_flags;
883 * Creates space for memory region @mem according to its type.
885 * This function first searches for free space in compatible memory types in
886 * the priority order defined by the driver. If free space isn't found, then
887 * ttm_bo_mem_force_space is attempted in priority order to evict and find
890 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
891 struct ttm_placement *placement,
892 struct ttm_mem_reg *mem,
893 bool interruptible, bool no_wait_reserve,
896 struct ttm_bo_device *bdev = bo->bdev;
897 struct ttm_mem_type_manager *man;
898 uint32_t mem_type = TTM_PL_SYSTEM;
899 uint32_t cur_flags = 0;
900 bool type_found = false;
901 bool type_ok = false;
902 bool has_erestartsys = false;
906 for (i = 0; i < placement->num_placement; ++i) {
907 ret = ttm_mem_type_from_flags(placement->placement[i],
911 man = &bdev->man[mem_type];
913 type_ok = ttm_bo_mt_compatible(man,
914 bo->type == ttm_bo_type_user,
916 placement->placement[i],
922 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
925 * Use the access and other non-mapping-related flag bits from
926 * the memory placement flags to the current flags
928 ttm_flag_masked(&cur_flags, placement->placement[i],
929 ~TTM_PL_MASK_MEMTYPE);
931 if (mem_type == TTM_PL_SYSTEM)
934 if (man->has_type && man->use_type) {
936 ret = (*man->func->get_node)(man, bo, placement, mem);
944 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
945 mem->mem_type = mem_type;
946 mem->placement = cur_flags;
953 for (i = 0; i < placement->num_busy_placement; ++i) {
954 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
958 man = &bdev->man[mem_type];
961 if (!ttm_bo_mt_compatible(man,
962 bo->type == ttm_bo_type_user,
964 placement->busy_placement[i],
968 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
971 * Use the access and other non-mapping-related flag bits from
972 * the memory placement flags to the current flags
974 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
975 ~TTM_PL_MASK_MEMTYPE);
978 if (mem_type == TTM_PL_SYSTEM) {
979 mem->mem_type = mem_type;
980 mem->placement = cur_flags;
985 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
986 interruptible, no_wait_reserve, no_wait_gpu);
987 if (ret == 0 && mem->mm_node) {
988 mem->placement = cur_flags;
991 if (ret == -ERESTARTSYS)
992 has_erestartsys = true;
994 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
997 EXPORT_SYMBOL(ttm_bo_mem_space);
999 int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1001 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1004 return wait_event_interruptible(bo->event_queue,
1005 atomic_read(&bo->cpu_writers) == 0);
1007 EXPORT_SYMBOL(ttm_bo_wait_cpu);
1009 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1010 struct ttm_placement *placement,
1011 bool interruptible, bool no_wait_reserve,
1015 struct ttm_mem_reg mem;
1017 BUG_ON(!atomic_read(&bo->reserved));
1020 * FIXME: It's possible to pipeline buffer moves.
1021 * Have the driver move function wait for idle when necessary,
1022 * instead of doing it here.
1024 spin_lock(&bo->lock);
1025 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1026 spin_unlock(&bo->lock);
1029 mem.num_pages = bo->num_pages;
1030 mem.size = mem.num_pages << PAGE_SHIFT;
1031 mem.page_alignment = bo->mem.page_alignment;
1032 mem.bus.io_reserved = false;
1034 * Determine where to move the buffer.
1036 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
1039 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
1041 if (ret && mem.mm_node)
1042 ttm_bo_mem_put(bo, &mem);
1046 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1047 struct ttm_mem_reg *mem)
1051 if (mem->mm_node && placement->lpfn != 0 &&
1052 (mem->start < placement->fpfn ||
1053 mem->start + mem->num_pages > placement->lpfn))
1056 for (i = 0; i < placement->num_placement; i++) {
1057 if ((placement->placement[i] & mem->placement &
1058 TTM_PL_MASK_CACHING) &&
1059 (placement->placement[i] & mem->placement &
1066 int ttm_bo_validate(struct ttm_buffer_object *bo,
1067 struct ttm_placement *placement,
1068 bool interruptible, bool no_wait_reserve,
1073 BUG_ON(!atomic_read(&bo->reserved));
1074 /* Check that range is valid */
1075 if (placement->lpfn || placement->fpfn)
1076 if (placement->fpfn > placement->lpfn ||
1077 (placement->lpfn - placement->fpfn) < bo->num_pages)
1080 * Check whether we need to move buffer.
1082 ret = ttm_bo_mem_compat(placement, &bo->mem);
1084 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
1089 * Use the access and other non-mapping-related flag bits from
1090 * the compatible memory placement flags to the active flags
1092 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1093 ~TTM_PL_MASK_MEMTYPE);
1096 * We might need to add a TTM.
1098 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1099 ret = ttm_bo_add_ttm(bo, true);
1105 EXPORT_SYMBOL(ttm_bo_validate);
1107 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1108 struct ttm_placement *placement)
1112 if (placement->fpfn || placement->lpfn) {
1113 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1114 printk(KERN_ERR TTM_PFX "Page number range to small "
1115 "Need %lu pages, range is [%u, %u]\n",
1116 bo->mem.num_pages, placement->fpfn,
1121 for (i = 0; i < placement->num_placement; i++) {
1122 if (!capable(CAP_SYS_ADMIN)) {
1123 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1124 printk(KERN_ERR TTM_PFX "Need to be root to "
1125 "modify NO_EVICT status.\n");
1130 for (i = 0; i < placement->num_busy_placement; i++) {
1131 if (!capable(CAP_SYS_ADMIN)) {
1132 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1133 printk(KERN_ERR TTM_PFX "Need to be root to "
1134 "modify NO_EVICT status.\n");
1142 int ttm_bo_init(struct ttm_bo_device *bdev,
1143 struct ttm_buffer_object *bo,
1145 enum ttm_bo_type type,
1146 struct ttm_placement *placement,
1147 uint32_t page_alignment,
1148 unsigned long buffer_start,
1150 struct file *persistant_swap_storage,
1152 void (*destroy) (struct ttm_buffer_object *))
1155 unsigned long num_pages;
1157 size += buffer_start & ~PAGE_MASK;
1158 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1159 if (num_pages == 0) {
1160 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1163 bo->destroy = destroy;
1165 spin_lock_init(&bo->lock);
1166 kref_init(&bo->kref);
1167 kref_init(&bo->list_kref);
1168 atomic_set(&bo->cpu_writers, 0);
1169 atomic_set(&bo->reserved, 1);
1170 init_waitqueue_head(&bo->event_queue);
1171 INIT_LIST_HEAD(&bo->lru);
1172 INIT_LIST_HEAD(&bo->ddestroy);
1173 INIT_LIST_HEAD(&bo->swap);
1175 bo->glob = bdev->glob;
1177 bo->num_pages = num_pages;
1178 bo->mem.size = num_pages << PAGE_SHIFT;
1179 bo->mem.mem_type = TTM_PL_SYSTEM;
1180 bo->mem.num_pages = bo->num_pages;
1181 bo->mem.mm_node = NULL;
1182 bo->mem.page_alignment = page_alignment;
1183 bo->mem.bus.io_reserved = false;
1184 bo->buffer_start = buffer_start & PAGE_MASK;
1186 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1187 bo->seq_valid = false;
1188 bo->persistant_swap_storage = persistant_swap_storage;
1189 bo->acc_size = acc_size;
1190 atomic_inc(&bo->glob->bo_count);
1192 ret = ttm_bo_check_placement(bo, placement);
1193 if (unlikely(ret != 0))
1197 * For ttm_bo_type_device buffers, allocate
1198 * address space from the device.
1200 if (bo->type == ttm_bo_type_device) {
1201 ret = ttm_bo_setup_vm(bo);
1206 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
1210 ttm_bo_unreserve(bo);
1214 ttm_bo_unreserve(bo);
1219 EXPORT_SYMBOL(ttm_bo_init);
1221 static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
1222 unsigned long num_pages)
1224 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1227 return glob->ttm_bo_size + 2 * page_array_size;
1230 int ttm_bo_create(struct ttm_bo_device *bdev,
1232 enum ttm_bo_type type,
1233 struct ttm_placement *placement,
1234 uint32_t page_alignment,
1235 unsigned long buffer_start,
1237 struct file *persistant_swap_storage,
1238 struct ttm_buffer_object **p_bo)
1240 struct ttm_buffer_object *bo;
1241 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1245 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
1246 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1247 if (unlikely(ret != 0))
1250 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1252 if (unlikely(bo == NULL)) {
1253 ttm_mem_global_free(mem_glob, acc_size);
1257 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1258 buffer_start, interruptible,
1259 persistant_swap_storage, acc_size, NULL);
1260 if (likely(ret == 0))
1266 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1267 unsigned mem_type, bool allow_errors)
1269 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1270 struct ttm_bo_global *glob = bdev->glob;
1274 * Can't use standard list traversal since we're unlocking.
1277 spin_lock(&glob->lru_lock);
1278 while (!list_empty(&man->lru)) {
1279 spin_unlock(&glob->lru_lock);
1280 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
1285 printk(KERN_ERR TTM_PFX
1286 "Cleanup eviction failed\n");
1289 spin_lock(&glob->lru_lock);
1291 spin_unlock(&glob->lru_lock);
1295 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1297 struct ttm_mem_type_manager *man;
1300 if (mem_type >= TTM_NUM_MEM_TYPES) {
1301 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1304 man = &bdev->man[mem_type];
1306 if (!man->has_type) {
1307 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1308 "memory manager type %u\n", mem_type);
1312 man->use_type = false;
1313 man->has_type = false;
1317 ttm_bo_force_list_clean(bdev, mem_type, false);
1319 ret = (*man->func->takedown)(man);
1324 EXPORT_SYMBOL(ttm_bo_clean_mm);
1326 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1328 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1330 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1331 printk(KERN_ERR TTM_PFX
1332 "Illegal memory manager memory type %u.\n",
1337 if (!man->has_type) {
1338 printk(KERN_ERR TTM_PFX
1339 "Memory type %u has not been initialized.\n",
1344 return ttm_bo_force_list_clean(bdev, mem_type, true);
1346 EXPORT_SYMBOL(ttm_bo_evict_mm);
1348 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1349 unsigned long p_size)
1352 struct ttm_mem_type_manager *man;
1354 if (type >= TTM_NUM_MEM_TYPES) {
1355 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1359 man = &bdev->man[type];
1360 if (man->has_type) {
1361 printk(KERN_ERR TTM_PFX
1362 "Memory manager already initialized for type %d\n",
1367 ret = bdev->driver->init_mem_type(bdev, type, man);
1373 if (type != TTM_PL_SYSTEM) {
1375 printk(KERN_ERR TTM_PFX
1376 "Zero size memory manager type %d\n",
1381 ret = (*man->func->init)(man, p_size);
1385 man->has_type = true;
1386 man->use_type = true;
1389 INIT_LIST_HEAD(&man->lru);
1393 EXPORT_SYMBOL(ttm_bo_init_mm);
1395 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1397 struct ttm_bo_global *glob =
1398 container_of(kobj, struct ttm_bo_global, kobj);
1400 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1401 __free_page(glob->dummy_read_page);
1405 void ttm_bo_global_release(struct drm_global_reference *ref)
1407 struct ttm_bo_global *glob = ref->object;
1409 kobject_del(&glob->kobj);
1410 kobject_put(&glob->kobj);
1412 EXPORT_SYMBOL(ttm_bo_global_release);
1414 int ttm_bo_global_init(struct drm_global_reference *ref)
1416 struct ttm_bo_global_ref *bo_ref =
1417 container_of(ref, struct ttm_bo_global_ref, ref);
1418 struct ttm_bo_global *glob = ref->object;
1421 mutex_init(&glob->device_list_mutex);
1422 spin_lock_init(&glob->lru_lock);
1423 glob->mem_glob = bo_ref->mem_glob;
1424 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1426 if (unlikely(glob->dummy_read_page == NULL)) {
1431 INIT_LIST_HEAD(&glob->swap_lru);
1432 INIT_LIST_HEAD(&glob->device_list);
1434 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1435 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1436 if (unlikely(ret != 0)) {
1437 printk(KERN_ERR TTM_PFX
1438 "Could not register buffer object swapout.\n");
1442 glob->ttm_bo_extra_size =
1443 ttm_round_pot(sizeof(struct ttm_tt)) +
1444 ttm_round_pot(sizeof(struct ttm_backend));
1446 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1447 ttm_round_pot(sizeof(struct ttm_buffer_object));
1449 atomic_set(&glob->bo_count, 0);
1451 ret = kobject_init_and_add(
1452 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1453 if (unlikely(ret != 0))
1454 kobject_put(&glob->kobj);
1457 __free_page(glob->dummy_read_page);
1462 EXPORT_SYMBOL(ttm_bo_global_init);
1465 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1468 unsigned i = TTM_NUM_MEM_TYPES;
1469 struct ttm_mem_type_manager *man;
1470 struct ttm_bo_global *glob = bdev->glob;
1473 man = &bdev->man[i];
1474 if (man->has_type) {
1475 man->use_type = false;
1476 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1478 printk(KERN_ERR TTM_PFX
1479 "DRM memory manager type %d "
1480 "is not clean.\n", i);
1482 man->has_type = false;
1486 mutex_lock(&glob->device_list_mutex);
1487 list_del(&bdev->device_list);
1488 mutex_unlock(&glob->device_list_mutex);
1490 if (!cancel_delayed_work(&bdev->wq))
1491 flush_scheduled_work();
1493 while (ttm_bo_delayed_delete(bdev, true))
1496 spin_lock(&glob->lru_lock);
1497 if (list_empty(&bdev->ddestroy))
1498 TTM_DEBUG("Delayed destroy list was clean\n");
1500 if (list_empty(&bdev->man[0].lru))
1501 TTM_DEBUG("Swap list was clean\n");
1502 spin_unlock(&glob->lru_lock);
1504 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1505 write_lock(&bdev->vm_lock);
1506 drm_mm_takedown(&bdev->addr_space_mm);
1507 write_unlock(&bdev->vm_lock);
1511 EXPORT_SYMBOL(ttm_bo_device_release);
1513 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1514 struct ttm_bo_global *glob,
1515 struct ttm_bo_driver *driver,
1516 uint64_t file_page_offset,
1521 rwlock_init(&bdev->vm_lock);
1522 bdev->driver = driver;
1524 memset(bdev->man, 0, sizeof(bdev->man));
1527 * Initialize the system memory buffer type.
1528 * Other types need to be driver / IOCTL initialized.
1530 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1531 if (unlikely(ret != 0))
1534 bdev->addr_space_rb = RB_ROOT;
1535 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1536 if (unlikely(ret != 0))
1537 goto out_no_addr_mm;
1539 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1540 bdev->nice_mode = true;
1541 INIT_LIST_HEAD(&bdev->ddestroy);
1542 bdev->dev_mapping = NULL;
1544 bdev->need_dma32 = need_dma32;
1546 mutex_lock(&glob->device_list_mutex);
1547 list_add_tail(&bdev->device_list, &glob->device_list);
1548 mutex_unlock(&glob->device_list_mutex);
1552 ttm_bo_clean_mm(bdev, 0);
1556 EXPORT_SYMBOL(ttm_bo_device_init);
1559 * buffer object vm functions.
1562 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1564 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1566 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1567 if (mem->mem_type == TTM_PL_SYSTEM)
1570 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1573 if (mem->placement & TTM_PL_FLAG_CACHED)
1579 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1581 struct ttm_bo_device *bdev = bo->bdev;
1582 loff_t offset = (loff_t) bo->addr_space_offset;
1583 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1585 if (!bdev->dev_mapping)
1587 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1588 ttm_mem_io_free(bdev, &bo->mem);
1590 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1592 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1594 struct ttm_bo_device *bdev = bo->bdev;
1595 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1596 struct rb_node *parent = NULL;
1597 struct ttm_buffer_object *cur_bo;
1598 unsigned long offset = bo->vm_node->start;
1599 unsigned long cur_offset;
1603 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1604 cur_offset = cur_bo->vm_node->start;
1605 if (offset < cur_offset)
1606 cur = &parent->rb_left;
1607 else if (offset > cur_offset)
1608 cur = &parent->rb_right;
1613 rb_link_node(&bo->vm_rb, parent, cur);
1614 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1620 * @bo: the buffer to allocate address space for
1622 * Allocate address space in the drm device so that applications
1623 * can mmap the buffer and access the contents. This only
1624 * applies to ttm_bo_type_device objects as others are not
1625 * placed in the drm device address space.
1628 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1630 struct ttm_bo_device *bdev = bo->bdev;
1634 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1635 if (unlikely(ret != 0))
1638 write_lock(&bdev->vm_lock);
1639 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1640 bo->mem.num_pages, 0, 0);
1642 if (unlikely(bo->vm_node == NULL)) {
1647 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1648 bo->mem.num_pages, 0);
1650 if (unlikely(bo->vm_node == NULL)) {
1651 write_unlock(&bdev->vm_lock);
1655 ttm_bo_vm_insert_rb(bo);
1656 write_unlock(&bdev->vm_lock);
1657 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1661 write_unlock(&bdev->vm_lock);
1665 int ttm_bo_wait(struct ttm_buffer_object *bo,
1666 bool lazy, bool interruptible, bool no_wait)
1668 struct ttm_bo_driver *driver = bo->bdev->driver;
1673 if (likely(bo->sync_obj == NULL))
1676 while (bo->sync_obj) {
1678 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1679 void *tmp_obj = bo->sync_obj;
1680 bo->sync_obj = NULL;
1681 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1682 spin_unlock(&bo->lock);
1683 driver->sync_obj_unref(&tmp_obj);
1684 spin_lock(&bo->lock);
1691 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1692 sync_obj_arg = bo->sync_obj_arg;
1693 spin_unlock(&bo->lock);
1694 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1695 lazy, interruptible);
1696 if (unlikely(ret != 0)) {
1697 driver->sync_obj_unref(&sync_obj);
1698 spin_lock(&bo->lock);
1701 spin_lock(&bo->lock);
1702 if (likely(bo->sync_obj == sync_obj &&
1703 bo->sync_obj_arg == sync_obj_arg)) {
1704 void *tmp_obj = bo->sync_obj;
1705 bo->sync_obj = NULL;
1706 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1708 spin_unlock(&bo->lock);
1709 driver->sync_obj_unref(&sync_obj);
1710 driver->sync_obj_unref(&tmp_obj);
1711 spin_lock(&bo->lock);
1713 spin_unlock(&bo->lock);
1714 driver->sync_obj_unref(&sync_obj);
1715 spin_lock(&bo->lock);
1720 EXPORT_SYMBOL(ttm_bo_wait);
1722 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1727 * Using ttm_bo_reserve makes sure the lru lists are updated.
1730 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1731 if (unlikely(ret != 0))
1733 spin_lock(&bo->lock);
1734 ret = ttm_bo_wait(bo, false, true, no_wait);
1735 spin_unlock(&bo->lock);
1736 if (likely(ret == 0))
1737 atomic_inc(&bo->cpu_writers);
1738 ttm_bo_unreserve(bo);
1741 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1743 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1745 if (atomic_dec_and_test(&bo->cpu_writers))
1746 wake_up_all(&bo->event_queue);
1748 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1751 * A buffer object shrink method that tries to swap out the first
1752 * buffer object on the bo_global::swap_lru list.
1755 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1757 struct ttm_bo_global *glob =
1758 container_of(shrink, struct ttm_bo_global, shrink);
1759 struct ttm_buffer_object *bo;
1762 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1764 spin_lock(&glob->lru_lock);
1765 while (ret == -EBUSY) {
1766 if (unlikely(list_empty(&glob->swap_lru))) {
1767 spin_unlock(&glob->lru_lock);
1771 bo = list_first_entry(&glob->swap_lru,
1772 struct ttm_buffer_object, swap);
1773 kref_get(&bo->list_kref);
1776 * Reserve buffer. Since we unlock while sleeping, we need
1777 * to re-check that nobody removed us from the swap-list while
1781 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1782 if (unlikely(ret == -EBUSY)) {
1783 spin_unlock(&glob->lru_lock);
1784 ttm_bo_wait_unreserved(bo, false);
1785 kref_put(&bo->list_kref, ttm_bo_release_list);
1786 spin_lock(&glob->lru_lock);
1791 put_count = ttm_bo_del_from_lru(bo);
1792 spin_unlock(&glob->lru_lock);
1795 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1798 * Wait for GPU, then move to system cached.
1801 spin_lock(&bo->lock);
1802 ret = ttm_bo_wait(bo, false, false, false);
1803 spin_unlock(&bo->lock);
1805 if (unlikely(ret != 0))
1808 if ((bo->mem.placement & swap_placement) != swap_placement) {
1809 struct ttm_mem_reg evict_mem;
1811 evict_mem = bo->mem;
1812 evict_mem.mm_node = NULL;
1813 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1814 evict_mem.mem_type = TTM_PL_SYSTEM;
1816 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1817 false, false, false);
1818 if (unlikely(ret != 0))
1822 ttm_bo_unmap_virtual(bo);
1825 * Swap out. Buffer will be swapped in again as soon as
1826 * anyone tries to access a ttm page.
1829 if (bo->bdev->driver->swap_notify)
1830 bo->bdev->driver->swap_notify(bo);
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)
1852 EXPORT_SYMBOL(ttm_bo_swapout_all);