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 spin_lock(&bdev->glob->lru_lock);
89 drm_mm_debug_table(&man->manager, TTM_PFX);
90 spin_unlock(&bdev->glob->lru_lock);
94 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
95 struct ttm_placement *placement)
99 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
100 bo, bo->mem.num_pages, bo->mem.size >> 10,
102 for (i = 0; i < placement->num_placement; i++) {
103 ret = ttm_mem_type_from_flags(placement->placement[i],
107 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
108 i, placement->placement[i], mem_type);
109 ttm_mem_type_debug(bo->bdev, mem_type);
113 static ssize_t ttm_bo_global_show(struct kobject *kobj,
114 struct attribute *attr,
117 struct ttm_bo_global *glob =
118 container_of(kobj, struct ttm_bo_global, kobj);
120 return snprintf(buffer, PAGE_SIZE, "%lu\n",
121 (unsigned long) atomic_read(&glob->bo_count));
124 static struct attribute *ttm_bo_global_attrs[] = {
129 static const struct sysfs_ops ttm_bo_global_ops = {
130 .show = &ttm_bo_global_show
133 static struct kobj_type ttm_bo_glob_kobj_type = {
134 .release = &ttm_bo_global_kobj_release,
135 .sysfs_ops = &ttm_bo_global_ops,
136 .default_attrs = ttm_bo_global_attrs
140 static inline uint32_t ttm_bo_type_flags(unsigned type)
145 static void ttm_bo_release_list(struct kref *list_kref)
147 struct ttm_buffer_object *bo =
148 container_of(list_kref, struct ttm_buffer_object, list_kref);
149 struct ttm_bo_device *bdev = bo->bdev;
151 BUG_ON(atomic_read(&bo->list_kref.refcount));
152 BUG_ON(atomic_read(&bo->kref.refcount));
153 BUG_ON(atomic_read(&bo->cpu_writers));
154 BUG_ON(bo->sync_obj != NULL);
155 BUG_ON(bo->mem.mm_node != NULL);
156 BUG_ON(!list_empty(&bo->lru));
157 BUG_ON(!list_empty(&bo->ddestroy));
160 ttm_tt_destroy(bo->ttm);
161 atomic_dec(&bo->glob->bo_count);
165 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
170 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
176 ret = wait_event_interruptible(bo->event_queue,
177 atomic_read(&bo->reserved) == 0);
178 if (unlikely(ret != 0))
181 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
185 EXPORT_SYMBOL(ttm_bo_wait_unreserved);
187 static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
189 struct ttm_bo_device *bdev = bo->bdev;
190 struct ttm_mem_type_manager *man;
192 BUG_ON(!atomic_read(&bo->reserved));
194 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
196 BUG_ON(!list_empty(&bo->lru));
198 man = &bdev->man[bo->mem.mem_type];
199 list_add_tail(&bo->lru, &man->lru);
200 kref_get(&bo->list_kref);
202 if (bo->ttm != NULL) {
203 list_add_tail(&bo->swap, &bo->glob->swap_lru);
204 kref_get(&bo->list_kref);
210 * Call with the lru_lock held.
213 static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
217 if (!list_empty(&bo->swap)) {
218 list_del_init(&bo->swap);
221 if (!list_empty(&bo->lru)) {
222 list_del_init(&bo->lru);
227 * TODO: Add a driver hook to delete from
228 * driver-specific LRU's here.
234 int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
236 bool no_wait, bool use_sequence, uint32_t sequence)
238 struct ttm_bo_global *glob = bo->glob;
241 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
242 if (use_sequence && bo->seq_valid &&
243 (sequence - bo->val_seq < (1 << 31))) {
250 spin_unlock(&glob->lru_lock);
251 ret = ttm_bo_wait_unreserved(bo, interruptible);
252 spin_lock(&glob->lru_lock);
259 bo->val_seq = sequence;
260 bo->seq_valid = true;
262 bo->seq_valid = false;
267 EXPORT_SYMBOL(ttm_bo_reserve);
269 static void ttm_bo_ref_bug(struct kref *list_kref)
274 int ttm_bo_reserve(struct ttm_buffer_object *bo,
276 bool no_wait, bool use_sequence, uint32_t sequence)
278 struct ttm_bo_global *glob = bo->glob;
282 spin_lock(&glob->lru_lock);
283 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
285 if (likely(ret == 0))
286 put_count = ttm_bo_del_from_lru(bo);
287 spin_unlock(&glob->lru_lock);
290 kref_put(&bo->list_kref, ttm_bo_ref_bug);
295 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
297 struct ttm_bo_global *glob = bo->glob;
299 spin_lock(&glob->lru_lock);
300 ttm_bo_add_to_lru(bo);
301 atomic_set(&bo->reserved, 0);
302 wake_up_all(&bo->event_queue);
303 spin_unlock(&glob->lru_lock);
305 EXPORT_SYMBOL(ttm_bo_unreserve);
308 * Call bo->mutex locked.
310 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
312 struct ttm_bo_device *bdev = bo->bdev;
313 struct ttm_bo_global *glob = bo->glob;
315 uint32_t page_flags = 0;
317 TTM_ASSERT_LOCKED(&bo->mutex);
320 if (bdev->need_dma32)
321 page_flags |= TTM_PAGE_FLAG_DMA32;
324 case ttm_bo_type_device:
326 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
327 case ttm_bo_type_kernel:
328 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
329 page_flags, glob->dummy_read_page);
330 if (unlikely(bo->ttm == NULL))
333 case ttm_bo_type_user:
334 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
335 page_flags | TTM_PAGE_FLAG_USER,
336 glob->dummy_read_page);
337 if (unlikely(bo->ttm == NULL)) {
342 ret = ttm_tt_set_user(bo->ttm, current,
343 bo->buffer_start, bo->num_pages);
344 if (unlikely(ret != 0))
345 ttm_tt_destroy(bo->ttm);
348 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
356 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
357 struct ttm_mem_reg *mem,
358 bool evict, bool interruptible,
359 bool no_wait_reserve, bool no_wait_gpu)
361 struct ttm_bo_device *bdev = bo->bdev;
362 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
363 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
364 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
365 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
368 if (old_is_pci || new_is_pci ||
369 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
370 ttm_bo_unmap_virtual(bo);
373 * Create and bind a ttm if required.
376 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
377 ret = ttm_bo_add_ttm(bo, false);
381 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
385 if (mem->mem_type != TTM_PL_SYSTEM) {
386 ret = ttm_tt_bind(bo->ttm, mem);
391 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
399 if (bdev->driver->move_notify)
400 bdev->driver->move_notify(bo, mem);
402 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
403 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
404 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
405 else if (bdev->driver->move)
406 ret = bdev->driver->move(bo, evict, interruptible,
407 no_wait_reserve, no_wait_gpu, mem);
409 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
416 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
418 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
422 if (bo->mem.mm_node) {
423 spin_lock(&bo->lock);
424 bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) +
425 bdev->man[bo->mem.mem_type].gpu_offset;
426 bo->cur_placement = bo->mem.placement;
427 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 * Call bo::reserved and with the lru lock held.
446 * Will release GPU memory type usage on destruction.
447 * This is the place to put in driver specific hooks.
448 * Will release the bo::reserved lock and the
452 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
454 struct ttm_bo_global *glob = bo->glob;
459 * Release the lru_lock, since we don't want to have
460 * an atomic requirement on ttm_tt[unbind|destroy].
463 spin_unlock(&glob->lru_lock);
464 ttm_tt_unbind(bo->ttm);
465 ttm_tt_destroy(bo->ttm);
467 spin_lock(&glob->lru_lock);
470 if (bo->mem.mm_node) {
471 drm_mm_put_block(bo->mem.mm_node);
472 bo->mem.mm_node = NULL;
475 atomic_set(&bo->reserved, 0);
476 wake_up_all(&bo->event_queue);
477 spin_unlock(&glob->lru_lock);
482 * If bo idle, remove from delayed- and lru lists, and unref.
483 * If not idle, and already on delayed list, do nothing.
484 * If not idle, and not on delayed list, put on delayed list,
485 * up the list_kref and schedule a delayed list check.
488 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
490 struct ttm_bo_device *bdev = bo->bdev;
491 struct ttm_bo_global *glob = bo->glob;
492 struct ttm_bo_driver *driver = bdev->driver;
495 spin_lock(&bo->lock);
497 (void) ttm_bo_wait(bo, false, false, !remove_all);
502 spin_unlock(&bo->lock);
504 spin_lock(&glob->lru_lock);
505 ret = ttm_bo_reserve_locked(bo, false, !remove_all, false, 0);
508 * Someone else has the object reserved. Bail and retry.
511 if (unlikely(ret == -EBUSY)) {
512 spin_unlock(&glob->lru_lock);
513 spin_lock(&bo->lock);
518 * We can re-check for sync object without taking
519 * the bo::lock since setting the sync object requires
520 * also bo::reserved. A busy object at this point may
521 * be caused by another thread starting an accelerated
525 if (unlikely(bo->sync_obj)) {
526 atomic_set(&bo->reserved, 0);
527 wake_up_all(&bo->event_queue);
528 spin_unlock(&glob->lru_lock);
529 spin_lock(&bo->lock);
536 put_count = ttm_bo_del_from_lru(bo);
538 if (!list_empty(&bo->ddestroy)) {
539 list_del_init(&bo->ddestroy);
543 ttm_bo_cleanup_memtype_use(bo);
546 kref_put(&bo->list_kref, ttm_bo_ref_bug);
551 spin_lock(&glob->lru_lock);
552 if (list_empty(&bo->ddestroy)) {
553 void *sync_obj = bo->sync_obj;
554 void *sync_obj_arg = bo->sync_obj_arg;
556 kref_get(&bo->list_kref);
557 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
558 spin_unlock(&glob->lru_lock);
559 spin_unlock(&bo->lock);
562 driver->sync_obj_flush(sync_obj, sync_obj_arg);
563 schedule_delayed_work(&bdev->wq,
564 ((HZ / 100) < 1) ? 1 : HZ / 100);
568 spin_unlock(&glob->lru_lock);
569 spin_unlock(&bo->lock);
577 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
578 * encountered buffers.
581 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
583 struct ttm_bo_global *glob = bdev->glob;
584 struct ttm_buffer_object *entry = NULL;
587 spin_lock(&glob->lru_lock);
588 if (list_empty(&bdev->ddestroy))
591 entry = list_first_entry(&bdev->ddestroy,
592 struct ttm_buffer_object, ddestroy);
593 kref_get(&entry->list_kref);
596 struct ttm_buffer_object *nentry = NULL;
598 if (entry->ddestroy.next != &bdev->ddestroy) {
599 nentry = list_first_entry(&entry->ddestroy,
600 struct ttm_buffer_object, ddestroy);
601 kref_get(&nentry->list_kref);
604 spin_unlock(&glob->lru_lock);
605 ret = ttm_bo_cleanup_refs(entry, remove_all);
606 kref_put(&entry->list_kref, ttm_bo_release_list);
612 spin_lock(&glob->lru_lock);
613 if (list_empty(&entry->ddestroy))
618 spin_unlock(&glob->lru_lock);
621 kref_put(&entry->list_kref, ttm_bo_release_list);
625 static void ttm_bo_delayed_workqueue(struct work_struct *work)
627 struct ttm_bo_device *bdev =
628 container_of(work, struct ttm_bo_device, wq.work);
630 if (ttm_bo_delayed_delete(bdev, false)) {
631 schedule_delayed_work(&bdev->wq,
632 ((HZ / 100) < 1) ? 1 : HZ / 100);
636 static void ttm_bo_release(struct kref *kref)
638 struct ttm_buffer_object *bo =
639 container_of(kref, struct ttm_buffer_object, kref);
640 struct ttm_bo_device *bdev = bo->bdev;
642 if (likely(bo->vm_node != NULL)) {
643 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
644 drm_mm_put_block(bo->vm_node);
647 write_unlock(&bdev->vm_lock);
648 ttm_bo_cleanup_refs(bo, false);
649 kref_put(&bo->list_kref, ttm_bo_release_list);
650 write_lock(&bdev->vm_lock);
653 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
655 struct ttm_buffer_object *bo = *p_bo;
656 struct ttm_bo_device *bdev = bo->bdev;
659 write_lock(&bdev->vm_lock);
660 kref_put(&bo->kref, ttm_bo_release);
661 write_unlock(&bdev->vm_lock);
663 EXPORT_SYMBOL(ttm_bo_unref);
665 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
667 return cancel_delayed_work_sync(&bdev->wq);
669 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
671 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
674 schedule_delayed_work(&bdev->wq,
675 ((HZ / 100) < 1) ? 1 : HZ / 100);
677 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
679 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
680 bool no_wait_reserve, bool no_wait_gpu)
682 struct ttm_bo_device *bdev = bo->bdev;
683 struct ttm_bo_global *glob = bo->glob;
684 struct ttm_mem_reg evict_mem;
685 struct ttm_placement placement;
688 spin_lock(&bo->lock);
689 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
690 spin_unlock(&bo->lock);
692 if (unlikely(ret != 0)) {
693 if (ret != -ERESTARTSYS) {
694 printk(KERN_ERR TTM_PFX
695 "Failed to expire sync object before "
696 "buffer eviction.\n");
701 BUG_ON(!atomic_read(&bo->reserved));
704 evict_mem.mm_node = NULL;
705 evict_mem.bus.io_reserved = false;
709 placement.num_placement = 0;
710 placement.num_busy_placement = 0;
711 bdev->driver->evict_flags(bo, &placement);
712 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
713 no_wait_reserve, no_wait_gpu);
715 if (ret != -ERESTARTSYS) {
716 printk(KERN_ERR TTM_PFX
717 "Failed to find memory space for "
718 "buffer 0x%p eviction.\n", bo);
719 ttm_bo_mem_space_debug(bo, &placement);
724 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
725 no_wait_reserve, no_wait_gpu);
727 if (ret != -ERESTARTSYS)
728 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
729 spin_lock(&glob->lru_lock);
730 if (evict_mem.mm_node) {
731 drm_mm_put_block(evict_mem.mm_node);
732 evict_mem.mm_node = NULL;
734 spin_unlock(&glob->lru_lock);
742 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
744 bool interruptible, bool no_wait_reserve,
747 struct ttm_bo_global *glob = bdev->glob;
748 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
749 struct ttm_buffer_object *bo;
750 int ret, put_count = 0;
753 spin_lock(&glob->lru_lock);
754 if (list_empty(&man->lru)) {
755 spin_unlock(&glob->lru_lock);
759 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
760 kref_get(&bo->list_kref);
762 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
764 if (unlikely(ret == -EBUSY)) {
765 spin_unlock(&glob->lru_lock);
766 if (likely(!no_wait_gpu))
767 ret = ttm_bo_wait_unreserved(bo, interruptible);
769 kref_put(&bo->list_kref, ttm_bo_release_list);
772 * We *need* to retry after releasing the lru lock.
775 if (unlikely(ret != 0))
780 put_count = ttm_bo_del_from_lru(bo);
781 spin_unlock(&glob->lru_lock);
786 kref_put(&bo->list_kref, ttm_bo_ref_bug);
788 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
789 ttm_bo_unreserve(bo);
791 kref_put(&bo->list_kref, ttm_bo_release_list);
795 static int ttm_bo_man_get_node(struct ttm_buffer_object *bo,
796 struct ttm_mem_type_manager *man,
797 struct ttm_placement *placement,
798 struct ttm_mem_reg *mem,
799 struct drm_mm_node **node)
801 struct ttm_bo_global *glob = bo->glob;
805 lpfn = placement->lpfn;
810 ret = drm_mm_pre_get(&man->manager);
814 spin_lock(&glob->lru_lock);
815 *node = drm_mm_search_free_in_range(&man->manager,
816 mem->num_pages, mem->page_alignment,
817 placement->fpfn, lpfn, 1);
818 if (unlikely(*node == NULL)) {
819 spin_unlock(&glob->lru_lock);
822 *node = drm_mm_get_block_atomic_range(*node, mem->num_pages,
826 spin_unlock(&glob->lru_lock);
827 } while (*node == NULL);
832 * Repeatedly evict memory from the LRU for @mem_type until we create enough
833 * space, or we've evicted everything and there isn't enough space.
835 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
837 struct ttm_placement *placement,
838 struct ttm_mem_reg *mem,
840 bool no_wait_reserve,
843 struct ttm_bo_device *bdev = bo->bdev;
844 struct ttm_bo_global *glob = bdev->glob;
845 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
846 struct drm_mm_node *node;
850 ret = ttm_bo_man_get_node(bo, man, placement, mem, &node);
851 if (unlikely(ret != 0))
855 spin_lock(&glob->lru_lock);
856 if (list_empty(&man->lru)) {
857 spin_unlock(&glob->lru_lock);
860 spin_unlock(&glob->lru_lock);
861 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
862 no_wait_reserve, no_wait_gpu);
863 if (unlikely(ret != 0))
869 mem->mem_type = mem_type;
873 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
874 uint32_t cur_placement,
875 uint32_t proposed_placement)
877 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
878 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
881 * Keep current caching if possible.
884 if ((cur_placement & caching) != 0)
885 result |= (cur_placement & caching);
886 else if ((man->default_caching & caching) != 0)
887 result |= man->default_caching;
888 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
889 result |= TTM_PL_FLAG_CACHED;
890 else if ((TTM_PL_FLAG_WC & caching) != 0)
891 result |= TTM_PL_FLAG_WC;
892 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
893 result |= TTM_PL_FLAG_UNCACHED;
898 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
901 uint32_t proposed_placement,
902 uint32_t *masked_placement)
904 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
906 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
909 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
912 if ((proposed_placement & man->available_caching) == 0)
915 cur_flags |= (proposed_placement & man->available_caching);
917 *masked_placement = cur_flags;
922 * Creates space for memory region @mem according to its type.
924 * This function first searches for free space in compatible memory types in
925 * the priority order defined by the driver. If free space isn't found, then
926 * ttm_bo_mem_force_space is attempted in priority order to evict and find
929 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
930 struct ttm_placement *placement,
931 struct ttm_mem_reg *mem,
932 bool interruptible, bool no_wait_reserve,
935 struct ttm_bo_device *bdev = bo->bdev;
936 struct ttm_mem_type_manager *man;
937 uint32_t mem_type = TTM_PL_SYSTEM;
938 uint32_t cur_flags = 0;
939 bool type_found = false;
940 bool type_ok = false;
941 bool has_erestartsys = false;
942 struct drm_mm_node *node = NULL;
946 for (i = 0; i < placement->num_placement; ++i) {
947 ret = ttm_mem_type_from_flags(placement->placement[i],
951 man = &bdev->man[mem_type];
953 type_ok = ttm_bo_mt_compatible(man,
954 bo->type == ttm_bo_type_user,
956 placement->placement[i],
962 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
965 * Use the access and other non-mapping-related flag bits from
966 * the memory placement flags to the current flags
968 ttm_flag_masked(&cur_flags, placement->placement[i],
969 ~TTM_PL_MASK_MEMTYPE);
971 if (mem_type == TTM_PL_SYSTEM)
974 if (man->has_type && man->use_type) {
976 ret = ttm_bo_man_get_node(bo, man, placement, mem,
985 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
987 mem->mem_type = mem_type;
988 mem->placement = cur_flags;
995 for (i = 0; i < placement->num_busy_placement; ++i) {
996 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1000 man = &bdev->man[mem_type];
1003 if (!ttm_bo_mt_compatible(man,
1004 bo->type == ttm_bo_type_user,
1006 placement->busy_placement[i],
1010 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1013 * Use the access and other non-mapping-related flag bits from
1014 * the memory placement flags to the current flags
1016 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1017 ~TTM_PL_MASK_MEMTYPE);
1020 if (mem_type == TTM_PL_SYSTEM) {
1021 mem->mem_type = mem_type;
1022 mem->placement = cur_flags;
1023 mem->mm_node = NULL;
1027 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1028 interruptible, no_wait_reserve, no_wait_gpu);
1029 if (ret == 0 && mem->mm_node) {
1030 mem->placement = cur_flags;
1033 if (ret == -ERESTARTSYS)
1034 has_erestartsys = true;
1036 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1039 EXPORT_SYMBOL(ttm_bo_mem_space);
1041 int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1043 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1046 return wait_event_interruptible(bo->event_queue,
1047 atomic_read(&bo->cpu_writers) == 0);
1049 EXPORT_SYMBOL(ttm_bo_wait_cpu);
1051 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1052 struct ttm_placement *placement,
1053 bool interruptible, bool no_wait_reserve,
1056 struct ttm_bo_global *glob = bo->glob;
1058 struct ttm_mem_reg mem;
1060 BUG_ON(!atomic_read(&bo->reserved));
1063 * FIXME: It's possible to pipeline buffer moves.
1064 * Have the driver move function wait for idle when necessary,
1065 * instead of doing it here.
1067 spin_lock(&bo->lock);
1068 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1069 spin_unlock(&bo->lock);
1072 mem.num_pages = bo->num_pages;
1073 mem.size = mem.num_pages << PAGE_SHIFT;
1074 mem.page_alignment = bo->mem.page_alignment;
1075 mem.bus.io_reserved = false;
1077 * Determine where to move the buffer.
1079 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
1082 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
1084 if (ret && mem.mm_node) {
1085 spin_lock(&glob->lru_lock);
1086 drm_mm_put_block(mem.mm_node);
1087 spin_unlock(&glob->lru_lock);
1092 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1093 struct ttm_mem_reg *mem)
1096 struct drm_mm_node *node = mem->mm_node;
1098 if (node && placement->lpfn != 0 &&
1099 (node->start < placement->fpfn ||
1100 node->start + node->size > placement->lpfn))
1103 for (i = 0; i < placement->num_placement; i++) {
1104 if ((placement->placement[i] & mem->placement &
1105 TTM_PL_MASK_CACHING) &&
1106 (placement->placement[i] & mem->placement &
1113 int ttm_bo_validate(struct ttm_buffer_object *bo,
1114 struct ttm_placement *placement,
1115 bool interruptible, bool no_wait_reserve,
1120 BUG_ON(!atomic_read(&bo->reserved));
1121 /* Check that range is valid */
1122 if (placement->lpfn || placement->fpfn)
1123 if (placement->fpfn > placement->lpfn ||
1124 (placement->lpfn - placement->fpfn) < bo->num_pages)
1127 * Check whether we need to move buffer.
1129 ret = ttm_bo_mem_compat(placement, &bo->mem);
1131 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
1136 * Use the access and other non-mapping-related flag bits from
1137 * the compatible memory placement flags to the active flags
1139 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1140 ~TTM_PL_MASK_MEMTYPE);
1143 * We might need to add a TTM.
1145 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1146 ret = ttm_bo_add_ttm(bo, true);
1152 EXPORT_SYMBOL(ttm_bo_validate);
1154 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1155 struct ttm_placement *placement)
1159 if (placement->fpfn || placement->lpfn) {
1160 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1161 printk(KERN_ERR TTM_PFX "Page number range to small "
1162 "Need %lu pages, range is [%u, %u]\n",
1163 bo->mem.num_pages, placement->fpfn,
1168 for (i = 0; i < placement->num_placement; i++) {
1169 if (!capable(CAP_SYS_ADMIN)) {
1170 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1171 printk(KERN_ERR TTM_PFX "Need to be root to "
1172 "modify NO_EVICT status.\n");
1177 for (i = 0; i < placement->num_busy_placement; i++) {
1178 if (!capable(CAP_SYS_ADMIN)) {
1179 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1180 printk(KERN_ERR TTM_PFX "Need to be root to "
1181 "modify NO_EVICT status.\n");
1189 int ttm_bo_init(struct ttm_bo_device *bdev,
1190 struct ttm_buffer_object *bo,
1192 enum ttm_bo_type type,
1193 struct ttm_placement *placement,
1194 uint32_t page_alignment,
1195 unsigned long buffer_start,
1197 struct file *persistant_swap_storage,
1199 void (*destroy) (struct ttm_buffer_object *))
1202 unsigned long num_pages;
1204 size += buffer_start & ~PAGE_MASK;
1205 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1206 if (num_pages == 0) {
1207 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1210 bo->destroy = destroy;
1212 spin_lock_init(&bo->lock);
1213 kref_init(&bo->kref);
1214 kref_init(&bo->list_kref);
1215 atomic_set(&bo->cpu_writers, 0);
1216 atomic_set(&bo->reserved, 1);
1217 init_waitqueue_head(&bo->event_queue);
1218 INIT_LIST_HEAD(&bo->lru);
1219 INIT_LIST_HEAD(&bo->ddestroy);
1220 INIT_LIST_HEAD(&bo->swap);
1222 bo->glob = bdev->glob;
1224 bo->num_pages = num_pages;
1225 bo->mem.size = num_pages << PAGE_SHIFT;
1226 bo->mem.mem_type = TTM_PL_SYSTEM;
1227 bo->mem.num_pages = bo->num_pages;
1228 bo->mem.mm_node = NULL;
1229 bo->mem.page_alignment = page_alignment;
1230 bo->mem.bus.io_reserved = false;
1231 bo->buffer_start = buffer_start & PAGE_MASK;
1233 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1234 bo->seq_valid = false;
1235 bo->persistant_swap_storage = persistant_swap_storage;
1236 bo->acc_size = acc_size;
1237 atomic_inc(&bo->glob->bo_count);
1239 ret = ttm_bo_check_placement(bo, placement);
1240 if (unlikely(ret != 0))
1244 * For ttm_bo_type_device buffers, allocate
1245 * address space from the device.
1247 if (bo->type == ttm_bo_type_device) {
1248 ret = ttm_bo_setup_vm(bo);
1253 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
1257 ttm_bo_unreserve(bo);
1261 ttm_bo_unreserve(bo);
1266 EXPORT_SYMBOL(ttm_bo_init);
1268 static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
1269 unsigned long num_pages)
1271 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1274 return glob->ttm_bo_size + 2 * page_array_size;
1277 int ttm_bo_create(struct ttm_bo_device *bdev,
1279 enum ttm_bo_type type,
1280 struct ttm_placement *placement,
1281 uint32_t page_alignment,
1282 unsigned long buffer_start,
1284 struct file *persistant_swap_storage,
1285 struct ttm_buffer_object **p_bo)
1287 struct ttm_buffer_object *bo;
1288 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1292 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
1293 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1294 if (unlikely(ret != 0))
1297 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1299 if (unlikely(bo == NULL)) {
1300 ttm_mem_global_free(mem_glob, acc_size);
1304 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1305 buffer_start, interruptible,
1306 persistant_swap_storage, acc_size, NULL);
1307 if (likely(ret == 0))
1313 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1314 unsigned mem_type, bool allow_errors)
1316 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1317 struct ttm_bo_global *glob = bdev->glob;
1321 * Can't use standard list traversal since we're unlocking.
1324 spin_lock(&glob->lru_lock);
1325 while (!list_empty(&man->lru)) {
1326 spin_unlock(&glob->lru_lock);
1327 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
1332 printk(KERN_ERR TTM_PFX
1333 "Cleanup eviction failed\n");
1336 spin_lock(&glob->lru_lock);
1338 spin_unlock(&glob->lru_lock);
1342 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1344 struct ttm_bo_global *glob = bdev->glob;
1345 struct ttm_mem_type_manager *man;
1348 if (mem_type >= TTM_NUM_MEM_TYPES) {
1349 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1352 man = &bdev->man[mem_type];
1354 if (!man->has_type) {
1355 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1356 "memory manager type %u\n", mem_type);
1360 man->use_type = false;
1361 man->has_type = false;
1365 ttm_bo_force_list_clean(bdev, mem_type, false);
1367 spin_lock(&glob->lru_lock);
1368 if (drm_mm_clean(&man->manager))
1369 drm_mm_takedown(&man->manager);
1373 spin_unlock(&glob->lru_lock);
1378 EXPORT_SYMBOL(ttm_bo_clean_mm);
1380 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1382 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1384 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1385 printk(KERN_ERR TTM_PFX
1386 "Illegal memory manager memory type %u.\n",
1391 if (!man->has_type) {
1392 printk(KERN_ERR TTM_PFX
1393 "Memory type %u has not been initialized.\n",
1398 return ttm_bo_force_list_clean(bdev, mem_type, true);
1400 EXPORT_SYMBOL(ttm_bo_evict_mm);
1402 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1403 unsigned long p_size)
1406 struct ttm_mem_type_manager *man;
1408 if (type >= TTM_NUM_MEM_TYPES) {
1409 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1413 man = &bdev->man[type];
1414 if (man->has_type) {
1415 printk(KERN_ERR TTM_PFX
1416 "Memory manager already initialized for type %d\n",
1421 ret = bdev->driver->init_mem_type(bdev, type, man);
1426 if (type != TTM_PL_SYSTEM) {
1428 printk(KERN_ERR TTM_PFX
1429 "Zero size memory manager type %d\n",
1433 ret = drm_mm_init(&man->manager, 0, p_size);
1437 man->has_type = true;
1438 man->use_type = true;
1441 INIT_LIST_HEAD(&man->lru);
1445 EXPORT_SYMBOL(ttm_bo_init_mm);
1447 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1449 struct ttm_bo_global *glob =
1450 container_of(kobj, struct ttm_bo_global, kobj);
1452 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1453 __free_page(glob->dummy_read_page);
1457 void ttm_bo_global_release(struct drm_global_reference *ref)
1459 struct ttm_bo_global *glob = ref->object;
1461 kobject_del(&glob->kobj);
1462 kobject_put(&glob->kobj);
1464 EXPORT_SYMBOL(ttm_bo_global_release);
1466 int ttm_bo_global_init(struct drm_global_reference *ref)
1468 struct ttm_bo_global_ref *bo_ref =
1469 container_of(ref, struct ttm_bo_global_ref, ref);
1470 struct ttm_bo_global *glob = ref->object;
1473 mutex_init(&glob->device_list_mutex);
1474 spin_lock_init(&glob->lru_lock);
1475 glob->mem_glob = bo_ref->mem_glob;
1476 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1478 if (unlikely(glob->dummy_read_page == NULL)) {
1483 INIT_LIST_HEAD(&glob->swap_lru);
1484 INIT_LIST_HEAD(&glob->device_list);
1486 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1487 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1488 if (unlikely(ret != 0)) {
1489 printk(KERN_ERR TTM_PFX
1490 "Could not register buffer object swapout.\n");
1494 glob->ttm_bo_extra_size =
1495 ttm_round_pot(sizeof(struct ttm_tt)) +
1496 ttm_round_pot(sizeof(struct ttm_backend));
1498 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1499 ttm_round_pot(sizeof(struct ttm_buffer_object));
1501 atomic_set(&glob->bo_count, 0);
1503 ret = kobject_init_and_add(
1504 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1505 if (unlikely(ret != 0))
1506 kobject_put(&glob->kobj);
1509 __free_page(glob->dummy_read_page);
1514 EXPORT_SYMBOL(ttm_bo_global_init);
1517 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1520 unsigned i = TTM_NUM_MEM_TYPES;
1521 struct ttm_mem_type_manager *man;
1522 struct ttm_bo_global *glob = bdev->glob;
1525 man = &bdev->man[i];
1526 if (man->has_type) {
1527 man->use_type = false;
1528 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1530 printk(KERN_ERR TTM_PFX
1531 "DRM memory manager type %d "
1532 "is not clean.\n", i);
1534 man->has_type = false;
1538 mutex_lock(&glob->device_list_mutex);
1539 list_del(&bdev->device_list);
1540 mutex_unlock(&glob->device_list_mutex);
1542 if (!cancel_delayed_work(&bdev->wq))
1543 flush_scheduled_work();
1545 while (ttm_bo_delayed_delete(bdev, true))
1548 spin_lock(&glob->lru_lock);
1549 if (list_empty(&bdev->ddestroy))
1550 TTM_DEBUG("Delayed destroy list was clean\n");
1552 if (list_empty(&bdev->man[0].lru))
1553 TTM_DEBUG("Swap list was clean\n");
1554 spin_unlock(&glob->lru_lock);
1556 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1557 write_lock(&bdev->vm_lock);
1558 drm_mm_takedown(&bdev->addr_space_mm);
1559 write_unlock(&bdev->vm_lock);
1563 EXPORT_SYMBOL(ttm_bo_device_release);
1565 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1566 struct ttm_bo_global *glob,
1567 struct ttm_bo_driver *driver,
1568 uint64_t file_page_offset,
1573 rwlock_init(&bdev->vm_lock);
1574 bdev->driver = driver;
1576 memset(bdev->man, 0, sizeof(bdev->man));
1579 * Initialize the system memory buffer type.
1580 * Other types need to be driver / IOCTL initialized.
1582 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1583 if (unlikely(ret != 0))
1586 bdev->addr_space_rb = RB_ROOT;
1587 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1588 if (unlikely(ret != 0))
1589 goto out_no_addr_mm;
1591 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1592 bdev->nice_mode = true;
1593 INIT_LIST_HEAD(&bdev->ddestroy);
1594 bdev->dev_mapping = NULL;
1596 bdev->need_dma32 = need_dma32;
1598 mutex_lock(&glob->device_list_mutex);
1599 list_add_tail(&bdev->device_list, &glob->device_list);
1600 mutex_unlock(&glob->device_list_mutex);
1604 ttm_bo_clean_mm(bdev, 0);
1608 EXPORT_SYMBOL(ttm_bo_device_init);
1611 * buffer object vm functions.
1614 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1616 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1618 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1619 if (mem->mem_type == TTM_PL_SYSTEM)
1622 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1625 if (mem->placement & TTM_PL_FLAG_CACHED)
1631 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1633 struct ttm_bo_device *bdev = bo->bdev;
1634 loff_t offset = (loff_t) bo->addr_space_offset;
1635 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1637 if (!bdev->dev_mapping)
1639 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1640 ttm_mem_io_free(bdev, &bo->mem);
1642 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1644 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1646 struct ttm_bo_device *bdev = bo->bdev;
1647 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1648 struct rb_node *parent = NULL;
1649 struct ttm_buffer_object *cur_bo;
1650 unsigned long offset = bo->vm_node->start;
1651 unsigned long cur_offset;
1655 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1656 cur_offset = cur_bo->vm_node->start;
1657 if (offset < cur_offset)
1658 cur = &parent->rb_left;
1659 else if (offset > cur_offset)
1660 cur = &parent->rb_right;
1665 rb_link_node(&bo->vm_rb, parent, cur);
1666 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1672 * @bo: the buffer to allocate address space for
1674 * Allocate address space in the drm device so that applications
1675 * can mmap the buffer and access the contents. This only
1676 * applies to ttm_bo_type_device objects as others are not
1677 * placed in the drm device address space.
1680 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1682 struct ttm_bo_device *bdev = bo->bdev;
1686 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1687 if (unlikely(ret != 0))
1690 write_lock(&bdev->vm_lock);
1691 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1692 bo->mem.num_pages, 0, 0);
1694 if (unlikely(bo->vm_node == NULL)) {
1699 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1700 bo->mem.num_pages, 0);
1702 if (unlikely(bo->vm_node == NULL)) {
1703 write_unlock(&bdev->vm_lock);
1707 ttm_bo_vm_insert_rb(bo);
1708 write_unlock(&bdev->vm_lock);
1709 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1713 write_unlock(&bdev->vm_lock);
1717 int ttm_bo_wait(struct ttm_buffer_object *bo,
1718 bool lazy, bool interruptible, bool no_wait)
1720 struct ttm_bo_driver *driver = bo->bdev->driver;
1725 if (likely(bo->sync_obj == NULL))
1728 while (bo->sync_obj) {
1730 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1731 void *tmp_obj = bo->sync_obj;
1732 bo->sync_obj = NULL;
1733 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1734 spin_unlock(&bo->lock);
1735 driver->sync_obj_unref(&tmp_obj);
1736 spin_lock(&bo->lock);
1743 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1744 sync_obj_arg = bo->sync_obj_arg;
1745 spin_unlock(&bo->lock);
1746 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1747 lazy, interruptible);
1748 if (unlikely(ret != 0)) {
1749 driver->sync_obj_unref(&sync_obj);
1750 spin_lock(&bo->lock);
1753 spin_lock(&bo->lock);
1754 if (likely(bo->sync_obj == sync_obj &&
1755 bo->sync_obj_arg == sync_obj_arg)) {
1756 void *tmp_obj = bo->sync_obj;
1757 bo->sync_obj = NULL;
1758 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1760 spin_unlock(&bo->lock);
1761 driver->sync_obj_unref(&sync_obj);
1762 driver->sync_obj_unref(&tmp_obj);
1763 spin_lock(&bo->lock);
1765 spin_unlock(&bo->lock);
1766 driver->sync_obj_unref(&sync_obj);
1767 spin_lock(&bo->lock);
1772 EXPORT_SYMBOL(ttm_bo_wait);
1774 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1779 * Using ttm_bo_reserve makes sure the lru lists are updated.
1782 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1783 if (unlikely(ret != 0))
1785 spin_lock(&bo->lock);
1786 ret = ttm_bo_wait(bo, false, true, no_wait);
1787 spin_unlock(&bo->lock);
1788 if (likely(ret == 0))
1789 atomic_inc(&bo->cpu_writers);
1790 ttm_bo_unreserve(bo);
1793 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1795 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1797 if (atomic_dec_and_test(&bo->cpu_writers))
1798 wake_up_all(&bo->event_queue);
1800 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1803 * A buffer object shrink method that tries to swap out the first
1804 * buffer object on the bo_global::swap_lru list.
1807 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1809 struct ttm_bo_global *glob =
1810 container_of(shrink, struct ttm_bo_global, shrink);
1811 struct ttm_buffer_object *bo;
1814 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1816 spin_lock(&glob->lru_lock);
1817 while (ret == -EBUSY) {
1818 if (unlikely(list_empty(&glob->swap_lru))) {
1819 spin_unlock(&glob->lru_lock);
1823 bo = list_first_entry(&glob->swap_lru,
1824 struct ttm_buffer_object, swap);
1825 kref_get(&bo->list_kref);
1828 * Reserve buffer. Since we unlock while sleeping, we need
1829 * to re-check that nobody removed us from the swap-list while
1833 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1834 if (unlikely(ret == -EBUSY)) {
1835 spin_unlock(&glob->lru_lock);
1836 ttm_bo_wait_unreserved(bo, false);
1837 kref_put(&bo->list_kref, ttm_bo_release_list);
1838 spin_lock(&glob->lru_lock);
1843 put_count = ttm_bo_del_from_lru(bo);
1844 spin_unlock(&glob->lru_lock);
1847 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1850 * Wait for GPU, then move to system cached.
1853 spin_lock(&bo->lock);
1854 ret = ttm_bo_wait(bo, false, false, false);
1855 spin_unlock(&bo->lock);
1857 if (unlikely(ret != 0))
1860 if ((bo->mem.placement & swap_placement) != swap_placement) {
1861 struct ttm_mem_reg evict_mem;
1863 evict_mem = bo->mem;
1864 evict_mem.mm_node = NULL;
1865 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1866 evict_mem.mem_type = TTM_PL_SYSTEM;
1868 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1869 false, false, false);
1870 if (unlikely(ret != 0))
1874 ttm_bo_unmap_virtual(bo);
1877 * Swap out. Buffer will be swapped in again as soon as
1878 * anyone tries to access a ttm page.
1881 if (bo->bdev->driver->swap_notify)
1882 bo->bdev->driver->swap_notify(bo);
1884 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1889 * Unreserve without putting on LRU to avoid swapping out an
1890 * already swapped buffer.
1893 atomic_set(&bo->reserved, 0);
1894 wake_up_all(&bo->event_queue);
1895 kref_put(&bo->list_kref, ttm_bo_release_list);
1899 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1901 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1904 EXPORT_SYMBOL(ttm_bo_swapout_all);