1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include "ttm/ttm_module.h"
32 #include "ttm/ttm_bo_driver.h"
33 #include "ttm/ttm_placement.h"
34 #include <linux/jiffies.h>
35 #include <linux/slab.h>
36 #include <linux/sched.h>
38 #include <linux/file.h>
39 #include <linux/module.h>
40 #include <linux/atomic.h>
42 #define TTM_ASSERT_LOCKED(param)
43 #define TTM_DEBUG(fmt, arg...)
44 #define TTM_BO_HASH_ORDER 13
46 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
);
47 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
);
48 static void ttm_bo_global_kobj_release(struct kobject
*kobj
);
50 static struct attribute ttm_bo_count
= {
55 static inline int ttm_mem_type_from_flags(uint32_t flags
, uint32_t *mem_type
)
59 for (i
= 0; i
<= TTM_PL_PRIV5
; i
++)
60 if (flags
& (1 << i
)) {
67 static void ttm_mem_type_debug(struct ttm_bo_device
*bdev
, int mem_type
)
69 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
71 printk(KERN_ERR TTM_PFX
" has_type: %d\n", man
->has_type
);
72 printk(KERN_ERR TTM_PFX
" use_type: %d\n", man
->use_type
);
73 printk(KERN_ERR TTM_PFX
" flags: 0x%08X\n", man
->flags
);
74 printk(KERN_ERR TTM_PFX
" gpu_offset: 0x%08lX\n", man
->gpu_offset
);
75 printk(KERN_ERR TTM_PFX
" size: %llu\n", man
->size
);
76 printk(KERN_ERR TTM_PFX
" available_caching: 0x%08X\n",
77 man
->available_caching
);
78 printk(KERN_ERR TTM_PFX
" default_caching: 0x%08X\n",
79 man
->default_caching
);
80 if (mem_type
!= TTM_PL_SYSTEM
)
81 (*man
->func
->debug
)(man
, TTM_PFX
);
84 static void ttm_bo_mem_space_debug(struct ttm_buffer_object
*bo
,
85 struct ttm_placement
*placement
)
89 printk(KERN_ERR TTM_PFX
"No space for %p (%lu pages, %luK, %luM)\n",
90 bo
, bo
->mem
.num_pages
, bo
->mem
.size
>> 10,
92 for (i
= 0; i
< placement
->num_placement
; i
++) {
93 ret
= ttm_mem_type_from_flags(placement
->placement
[i
],
97 printk(KERN_ERR TTM_PFX
" placement[%d]=0x%08X (%d)\n",
98 i
, placement
->placement
[i
], mem_type
);
99 ttm_mem_type_debug(bo
->bdev
, mem_type
);
103 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
104 struct attribute
*attr
,
107 struct ttm_bo_global
*glob
=
108 container_of(kobj
, struct ttm_bo_global
, kobj
);
110 return snprintf(buffer
, PAGE_SIZE
, "%lu\n",
111 (unsigned long) atomic_read(&glob
->bo_count
));
114 static struct attribute
*ttm_bo_global_attrs
[] = {
119 static const struct sysfs_ops ttm_bo_global_ops
= {
120 .show
= &ttm_bo_global_show
123 static struct kobj_type ttm_bo_glob_kobj_type
= {
124 .release
= &ttm_bo_global_kobj_release
,
125 .sysfs_ops
= &ttm_bo_global_ops
,
126 .default_attrs
= ttm_bo_global_attrs
130 static inline uint32_t ttm_bo_type_flags(unsigned type
)
135 static void ttm_bo_release_list(struct kref
*list_kref
)
137 struct ttm_buffer_object
*bo
=
138 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
139 struct ttm_bo_device
*bdev
= bo
->bdev
;
140 size_t acc_size
= bo
->acc_size
;
142 BUG_ON(atomic_read(&bo
->list_kref
.refcount
));
143 BUG_ON(atomic_read(&bo
->kref
.refcount
));
144 BUG_ON(atomic_read(&bo
->cpu_writers
));
145 BUG_ON(bo
->sync_obj
!= NULL
);
146 BUG_ON(bo
->mem
.mm_node
!= NULL
);
147 BUG_ON(!list_empty(&bo
->lru
));
148 BUG_ON(!list_empty(&bo
->ddestroy
));
151 ttm_tt_destroy(bo
->ttm
);
152 atomic_dec(&bo
->glob
->bo_count
);
158 ttm_mem_global_free(bdev
->glob
->mem_glob
, acc_size
);
161 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
, bool interruptible
)
164 return wait_event_interruptible(bo
->event_queue
,
165 atomic_read(&bo
->reserved
) == 0);
167 wait_event(bo
->event_queue
, atomic_read(&bo
->reserved
) == 0);
171 EXPORT_SYMBOL(ttm_bo_wait_unreserved
);
173 void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
175 struct ttm_bo_device
*bdev
= bo
->bdev
;
176 struct ttm_mem_type_manager
*man
;
178 BUG_ON(!atomic_read(&bo
->reserved
));
180 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
182 BUG_ON(!list_empty(&bo
->lru
));
184 man
= &bdev
->man
[bo
->mem
.mem_type
];
185 list_add_tail(&bo
->lru
, &man
->lru
);
186 kref_get(&bo
->list_kref
);
188 if (bo
->ttm
!= NULL
) {
189 list_add_tail(&bo
->swap
, &bo
->glob
->swap_lru
);
190 kref_get(&bo
->list_kref
);
195 int ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
199 if (!list_empty(&bo
->swap
)) {
200 list_del_init(&bo
->swap
);
203 if (!list_empty(&bo
->lru
)) {
204 list_del_init(&bo
->lru
);
209 * TODO: Add a driver hook to delete from
210 * driver-specific LRU's here.
216 int ttm_bo_reserve_locked(struct ttm_buffer_object
*bo
,
218 bool no_wait
, bool use_sequence
, uint32_t sequence
)
220 struct ttm_bo_global
*glob
= bo
->glob
;
223 while (unlikely(atomic_cmpxchg(&bo
->reserved
, 0, 1) != 0)) {
225 * Deadlock avoidance for multi-bo reserving.
227 if (use_sequence
&& bo
->seq_valid
) {
229 * We've already reserved this one.
231 if (unlikely(sequence
== bo
->val_seq
))
234 * Already reserved by a thread that will not back
235 * off for us. We need to back off.
237 if (unlikely(sequence
- bo
->val_seq
< (1 << 31)))
244 spin_unlock(&glob
->lru_lock
);
245 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
246 spin_lock(&glob
->lru_lock
);
254 * Wake up waiters that may need to recheck for deadlock,
255 * if we decreased the sequence number.
257 if (unlikely((bo
->val_seq
- sequence
< (1 << 31))
259 wake_up_all(&bo
->event_queue
);
261 bo
->val_seq
= sequence
;
262 bo
->seq_valid
= true;
264 bo
->seq_valid
= false;
269 EXPORT_SYMBOL(ttm_bo_reserve
);
271 static void ttm_bo_ref_bug(struct kref
*list_kref
)
276 void ttm_bo_list_ref_sub(struct ttm_buffer_object
*bo
, int count
,
279 kref_sub(&bo
->list_kref
, count
,
280 (never_free
) ? ttm_bo_ref_bug
: ttm_bo_release_list
);
283 int ttm_bo_reserve(struct ttm_buffer_object
*bo
,
285 bool no_wait
, bool use_sequence
, uint32_t sequence
)
287 struct ttm_bo_global
*glob
= bo
->glob
;
291 spin_lock(&glob
->lru_lock
);
292 ret
= ttm_bo_reserve_locked(bo
, interruptible
, no_wait
, use_sequence
,
294 if (likely(ret
== 0))
295 put_count
= ttm_bo_del_from_lru(bo
);
296 spin_unlock(&glob
->lru_lock
);
298 ttm_bo_list_ref_sub(bo
, put_count
, true);
303 void ttm_bo_unreserve_locked(struct ttm_buffer_object
*bo
)
305 ttm_bo_add_to_lru(bo
);
306 atomic_set(&bo
->reserved
, 0);
307 wake_up_all(&bo
->event_queue
);
310 void ttm_bo_unreserve(struct ttm_buffer_object
*bo
)
312 struct ttm_bo_global
*glob
= bo
->glob
;
314 spin_lock(&glob
->lru_lock
);
315 ttm_bo_unreserve_locked(bo
);
316 spin_unlock(&glob
->lru_lock
);
318 EXPORT_SYMBOL(ttm_bo_unreserve
);
321 * Call bo->mutex locked.
323 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
325 struct ttm_bo_device
*bdev
= bo
->bdev
;
326 struct ttm_bo_global
*glob
= bo
->glob
;
328 uint32_t page_flags
= 0;
330 TTM_ASSERT_LOCKED(&bo
->mutex
);
333 if (bdev
->need_dma32
)
334 page_flags
|= TTM_PAGE_FLAG_DMA32
;
337 case ttm_bo_type_device
:
339 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
340 case ttm_bo_type_kernel
:
341 bo
->ttm
= bdev
->driver
->ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
342 page_flags
, glob
->dummy_read_page
);
343 if (unlikely(bo
->ttm
== NULL
))
347 printk(KERN_ERR TTM_PFX
"Illegal buffer object type\n");
355 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
356 struct ttm_mem_reg
*mem
,
357 bool evict
, bool interruptible
,
358 bool no_wait_reserve
, bool no_wait_gpu
)
360 struct ttm_bo_device
*bdev
= bo
->bdev
;
361 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
362 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
363 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
364 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
367 if (old_is_pci
|| new_is_pci
||
368 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0)) {
369 ret
= ttm_mem_io_lock(old_man
, true);
370 if (unlikely(ret
!= 0))
372 ttm_bo_unmap_virtual_locked(bo
);
373 ttm_mem_io_unlock(old_man
);
377 * Create and bind a ttm if required.
380 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
381 if (bo
->ttm
== NULL
) {
382 bool zero
= !(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
);
383 ret
= ttm_bo_add_ttm(bo
, zero
);
388 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
392 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
393 ret
= ttm_tt_bind(bo
->ttm
, mem
);
398 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
399 if (bdev
->driver
->move_notify
)
400 bdev
->driver
->move_notify(bo
, mem
);
407 if (bdev
->driver
->move_notify
)
408 bdev
->driver
->move_notify(bo
, mem
);
410 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
411 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
412 ret
= ttm_bo_move_ttm(bo
, evict
, no_wait_reserve
, no_wait_gpu
, mem
);
413 else if (bdev
->driver
->move
)
414 ret
= bdev
->driver
->move(bo
, evict
, interruptible
,
415 no_wait_reserve
, no_wait_gpu
, mem
);
417 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait_reserve
, no_wait_gpu
, mem
);
424 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
426 printk(KERN_ERR TTM_PFX
"Can not flush read caches\n");
430 if (bo
->mem
.mm_node
) {
431 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
432 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
433 bo
->cur_placement
= bo
->mem
.placement
;
440 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
441 if ((new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
) {
442 ttm_tt_unbind(bo
->ttm
);
443 ttm_tt_destroy(bo
->ttm
);
452 * Will release GPU memory type usage on destruction.
453 * This is the place to put in driver specific hooks to release
454 * driver private resources.
455 * Will release the bo::reserved lock.
458 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
461 ttm_tt_unbind(bo
->ttm
);
462 ttm_tt_destroy(bo
->ttm
);
465 ttm_bo_mem_put(bo
, &bo
->mem
);
467 atomic_set(&bo
->reserved
, 0);
470 * Make processes trying to reserve really pick it up.
472 smp_mb__after_atomic_dec();
473 wake_up_all(&bo
->event_queue
);
476 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
478 struct ttm_bo_device
*bdev
= bo
->bdev
;
479 struct ttm_bo_global
*glob
= bo
->glob
;
480 struct ttm_bo_driver
*driver
;
481 void *sync_obj
= NULL
;
486 spin_lock(&bdev
->fence_lock
);
487 (void) ttm_bo_wait(bo
, false, false, true);
490 spin_lock(&glob
->lru_lock
);
493 * Lock inversion between bo:reserve and bdev::fence_lock here,
494 * but that's OK, since we're only trylocking.
497 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
499 if (unlikely(ret
== -EBUSY
))
502 spin_unlock(&bdev
->fence_lock
);
503 put_count
= ttm_bo_del_from_lru(bo
);
505 spin_unlock(&glob
->lru_lock
);
506 ttm_bo_cleanup_memtype_use(bo
);
508 ttm_bo_list_ref_sub(bo
, put_count
, true);
512 spin_lock(&glob
->lru_lock
);
515 driver
= bdev
->driver
;
517 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
518 sync_obj_arg
= bo
->sync_obj_arg
;
520 kref_get(&bo
->list_kref
);
521 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
522 spin_unlock(&glob
->lru_lock
);
523 spin_unlock(&bdev
->fence_lock
);
526 driver
->sync_obj_flush(sync_obj
, sync_obj_arg
);
527 driver
->sync_obj_unref(&sync_obj
);
529 schedule_delayed_work(&bdev
->wq
,
530 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
534 * function ttm_bo_cleanup_refs
535 * If bo idle, remove from delayed- and lru lists, and unref.
536 * If not idle, do nothing.
538 * @interruptible Any sleeps should occur interruptibly.
539 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
540 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
543 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
545 bool no_wait_reserve
,
548 struct ttm_bo_device
*bdev
= bo
->bdev
;
549 struct ttm_bo_global
*glob
= bo
->glob
;
554 spin_lock(&bdev
->fence_lock
);
555 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
556 spin_unlock(&bdev
->fence_lock
);
558 if (unlikely(ret
!= 0))
561 spin_lock(&glob
->lru_lock
);
562 ret
= ttm_bo_reserve_locked(bo
, interruptible
,
563 no_wait_reserve
, false, 0);
565 if (unlikely(ret
!= 0) || list_empty(&bo
->ddestroy
)) {
566 spin_unlock(&glob
->lru_lock
);
571 * We can re-check for sync object without taking
572 * the bo::lock since setting the sync object requires
573 * also bo::reserved. A busy object at this point may
574 * be caused by another thread recently starting an accelerated
578 if (unlikely(bo
->sync_obj
)) {
579 atomic_set(&bo
->reserved
, 0);
580 wake_up_all(&bo
->event_queue
);
581 spin_unlock(&glob
->lru_lock
);
585 put_count
= ttm_bo_del_from_lru(bo
);
586 list_del_init(&bo
->ddestroy
);
589 spin_unlock(&glob
->lru_lock
);
590 ttm_bo_cleanup_memtype_use(bo
);
592 ttm_bo_list_ref_sub(bo
, put_count
, true);
598 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
599 * encountered buffers.
602 static int ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
604 struct ttm_bo_global
*glob
= bdev
->glob
;
605 struct ttm_buffer_object
*entry
= NULL
;
608 spin_lock(&glob
->lru_lock
);
609 if (list_empty(&bdev
->ddestroy
))
612 entry
= list_first_entry(&bdev
->ddestroy
,
613 struct ttm_buffer_object
, ddestroy
);
614 kref_get(&entry
->list_kref
);
617 struct ttm_buffer_object
*nentry
= NULL
;
619 if (entry
->ddestroy
.next
!= &bdev
->ddestroy
) {
620 nentry
= list_first_entry(&entry
->ddestroy
,
621 struct ttm_buffer_object
, ddestroy
);
622 kref_get(&nentry
->list_kref
);
625 spin_unlock(&glob
->lru_lock
);
626 ret
= ttm_bo_cleanup_refs(entry
, false, !remove_all
,
628 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
634 spin_lock(&glob
->lru_lock
);
635 if (list_empty(&entry
->ddestroy
))
640 spin_unlock(&glob
->lru_lock
);
643 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
647 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
649 struct ttm_bo_device
*bdev
=
650 container_of(work
, struct ttm_bo_device
, wq
.work
);
652 if (ttm_bo_delayed_delete(bdev
, false)) {
653 schedule_delayed_work(&bdev
->wq
,
654 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
658 static void ttm_bo_release(struct kref
*kref
)
660 struct ttm_buffer_object
*bo
=
661 container_of(kref
, struct ttm_buffer_object
, kref
);
662 struct ttm_bo_device
*bdev
= bo
->bdev
;
663 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
665 if (likely(bo
->vm_node
!= NULL
)) {
666 rb_erase(&bo
->vm_rb
, &bdev
->addr_space_rb
);
667 drm_mm_put_block(bo
->vm_node
);
670 write_unlock(&bdev
->vm_lock
);
671 ttm_mem_io_lock(man
, false);
672 ttm_mem_io_free_vm(bo
);
673 ttm_mem_io_unlock(man
);
674 ttm_bo_cleanup_refs_or_queue(bo
);
675 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
676 write_lock(&bdev
->vm_lock
);
679 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
681 struct ttm_buffer_object
*bo
= *p_bo
;
682 struct ttm_bo_device
*bdev
= bo
->bdev
;
685 write_lock(&bdev
->vm_lock
);
686 kref_put(&bo
->kref
, ttm_bo_release
);
687 write_unlock(&bdev
->vm_lock
);
689 EXPORT_SYMBOL(ttm_bo_unref
);
691 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
693 return cancel_delayed_work_sync(&bdev
->wq
);
695 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
697 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
700 schedule_delayed_work(&bdev
->wq
,
701 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
703 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
705 static int ttm_bo_evict(struct ttm_buffer_object
*bo
, bool interruptible
,
706 bool no_wait_reserve
, bool no_wait_gpu
)
708 struct ttm_bo_device
*bdev
= bo
->bdev
;
709 struct ttm_mem_reg evict_mem
;
710 struct ttm_placement placement
;
713 spin_lock(&bdev
->fence_lock
);
714 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
715 spin_unlock(&bdev
->fence_lock
);
717 if (unlikely(ret
!= 0)) {
718 if (ret
!= -ERESTARTSYS
) {
719 printk(KERN_ERR TTM_PFX
720 "Failed to expire sync object before "
721 "buffer eviction.\n");
726 BUG_ON(!atomic_read(&bo
->reserved
));
729 evict_mem
.mm_node
= NULL
;
730 evict_mem
.bus
.io_reserved_vm
= false;
731 evict_mem
.bus
.io_reserved_count
= 0;
735 placement
.num_placement
= 0;
736 placement
.num_busy_placement
= 0;
737 bdev
->driver
->evict_flags(bo
, &placement
);
738 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, interruptible
,
739 no_wait_reserve
, no_wait_gpu
);
741 if (ret
!= -ERESTARTSYS
) {
742 printk(KERN_ERR TTM_PFX
743 "Failed to find memory space for "
744 "buffer 0x%p eviction.\n", bo
);
745 ttm_bo_mem_space_debug(bo
, &placement
);
750 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, interruptible
,
751 no_wait_reserve
, no_wait_gpu
);
753 if (ret
!= -ERESTARTSYS
)
754 printk(KERN_ERR TTM_PFX
"Buffer eviction failed\n");
755 ttm_bo_mem_put(bo
, &evict_mem
);
763 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
765 bool interruptible
, bool no_wait_reserve
,
768 struct ttm_bo_global
*glob
= bdev
->glob
;
769 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
770 struct ttm_buffer_object
*bo
;
771 int ret
, put_count
= 0;
774 spin_lock(&glob
->lru_lock
);
775 if (list_empty(&man
->lru
)) {
776 spin_unlock(&glob
->lru_lock
);
780 bo
= list_first_entry(&man
->lru
, struct ttm_buffer_object
, lru
);
781 kref_get(&bo
->list_kref
);
783 if (!list_empty(&bo
->ddestroy
)) {
784 spin_unlock(&glob
->lru_lock
);
785 ret
= ttm_bo_cleanup_refs(bo
, interruptible
,
786 no_wait_reserve
, no_wait_gpu
);
787 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
789 if (likely(ret
== 0 || ret
== -ERESTARTSYS
))
795 ret
= ttm_bo_reserve_locked(bo
, false, no_wait_reserve
, false, 0);
797 if (unlikely(ret
== -EBUSY
)) {
798 spin_unlock(&glob
->lru_lock
);
799 if (likely(!no_wait_gpu
))
800 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
802 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
805 * We *need* to retry after releasing the lru lock.
808 if (unlikely(ret
!= 0))
813 put_count
= ttm_bo_del_from_lru(bo
);
814 spin_unlock(&glob
->lru_lock
);
818 ttm_bo_list_ref_sub(bo
, put_count
, true);
820 ret
= ttm_bo_evict(bo
, interruptible
, no_wait_reserve
, no_wait_gpu
);
821 ttm_bo_unreserve(bo
);
823 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
827 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
829 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
832 (*man
->func
->put_node
)(man
, mem
);
834 EXPORT_SYMBOL(ttm_bo_mem_put
);
837 * Repeatedly evict memory from the LRU for @mem_type until we create enough
838 * space, or we've evicted everything and there isn't enough space.
840 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
842 struct ttm_placement
*placement
,
843 struct ttm_mem_reg
*mem
,
845 bool no_wait_reserve
,
848 struct ttm_bo_device
*bdev
= bo
->bdev
;
849 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
853 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
854 if (unlikely(ret
!= 0))
858 ret
= ttm_mem_evict_first(bdev
, mem_type
, interruptible
,
859 no_wait_reserve
, no_wait_gpu
);
860 if (unlikely(ret
!= 0))
863 if (mem
->mm_node
== NULL
)
865 mem
->mem_type
= mem_type
;
869 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
870 uint32_t cur_placement
,
871 uint32_t proposed_placement
)
873 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
874 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
877 * Keep current caching if possible.
880 if ((cur_placement
& caching
) != 0)
881 result
|= (cur_placement
& caching
);
882 else if ((man
->default_caching
& caching
) != 0)
883 result
|= man
->default_caching
;
884 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
885 result
|= TTM_PL_FLAG_CACHED
;
886 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
887 result
|= TTM_PL_FLAG_WC
;
888 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
889 result
|= TTM_PL_FLAG_UNCACHED
;
894 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
896 uint32_t proposed_placement
,
897 uint32_t *masked_placement
)
899 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
901 if ((cur_flags
& proposed_placement
& TTM_PL_MASK_MEM
) == 0)
904 if ((proposed_placement
& man
->available_caching
) == 0)
907 cur_flags
|= (proposed_placement
& man
->available_caching
);
909 *masked_placement
= cur_flags
;
914 * Creates space for memory region @mem according to its type.
916 * This function first searches for free space in compatible memory types in
917 * the priority order defined by the driver. If free space isn't found, then
918 * ttm_bo_mem_force_space is attempted in priority order to evict and find
921 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
922 struct ttm_placement
*placement
,
923 struct ttm_mem_reg
*mem
,
924 bool interruptible
, bool no_wait_reserve
,
927 struct ttm_bo_device
*bdev
= bo
->bdev
;
928 struct ttm_mem_type_manager
*man
;
929 uint32_t mem_type
= TTM_PL_SYSTEM
;
930 uint32_t cur_flags
= 0;
931 bool type_found
= false;
932 bool type_ok
= false;
933 bool has_erestartsys
= false;
937 for (i
= 0; i
< placement
->num_placement
; ++i
) {
938 ret
= ttm_mem_type_from_flags(placement
->placement
[i
],
942 man
= &bdev
->man
[mem_type
];
944 type_ok
= ttm_bo_mt_compatible(man
,
946 placement
->placement
[i
],
952 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
955 * Use the access and other non-mapping-related flag bits from
956 * the memory placement flags to the current flags
958 ttm_flag_masked(&cur_flags
, placement
->placement
[i
],
959 ~TTM_PL_MASK_MEMTYPE
);
961 if (mem_type
== TTM_PL_SYSTEM
)
964 if (man
->has_type
&& man
->use_type
) {
966 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
974 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
975 mem
->mem_type
= mem_type
;
976 mem
->placement
= cur_flags
;
983 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
984 ret
= ttm_mem_type_from_flags(placement
->busy_placement
[i
],
988 man
= &bdev
->man
[mem_type
];
991 if (!ttm_bo_mt_compatible(man
,
993 placement
->busy_placement
[i
],
997 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
1000 * Use the access and other non-mapping-related flag bits from
1001 * the memory placement flags to the current flags
1003 ttm_flag_masked(&cur_flags
, placement
->busy_placement
[i
],
1004 ~TTM_PL_MASK_MEMTYPE
);
1007 if (mem_type
== TTM_PL_SYSTEM
) {
1008 mem
->mem_type
= mem_type
;
1009 mem
->placement
= cur_flags
;
1010 mem
->mm_node
= NULL
;
1014 ret
= ttm_bo_mem_force_space(bo
, mem_type
, placement
, mem
,
1015 interruptible
, no_wait_reserve
, no_wait_gpu
);
1016 if (ret
== 0 && mem
->mm_node
) {
1017 mem
->placement
= cur_flags
;
1020 if (ret
== -ERESTARTSYS
)
1021 has_erestartsys
= true;
1023 ret
= (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
1026 EXPORT_SYMBOL(ttm_bo_mem_space
);
1028 int ttm_bo_wait_cpu(struct ttm_buffer_object
*bo
, bool no_wait
)
1030 if ((atomic_read(&bo
->cpu_writers
) > 0) && no_wait
)
1033 return wait_event_interruptible(bo
->event_queue
,
1034 atomic_read(&bo
->cpu_writers
) == 0);
1036 EXPORT_SYMBOL(ttm_bo_wait_cpu
);
1038 int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1039 struct ttm_placement
*placement
,
1040 bool interruptible
, bool no_wait_reserve
,
1044 struct ttm_mem_reg mem
;
1045 struct ttm_bo_device
*bdev
= bo
->bdev
;
1047 BUG_ON(!atomic_read(&bo
->reserved
));
1050 * FIXME: It's possible to pipeline buffer moves.
1051 * Have the driver move function wait for idle when necessary,
1052 * instead of doing it here.
1054 spin_lock(&bdev
->fence_lock
);
1055 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
1056 spin_unlock(&bdev
->fence_lock
);
1059 mem
.num_pages
= bo
->num_pages
;
1060 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1061 mem
.page_alignment
= bo
->mem
.page_alignment
;
1062 mem
.bus
.io_reserved_vm
= false;
1063 mem
.bus
.io_reserved_count
= 0;
1065 * Determine where to move the buffer.
1067 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, interruptible
, no_wait_reserve
, no_wait_gpu
);
1070 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, interruptible
, no_wait_reserve
, no_wait_gpu
);
1072 if (ret
&& mem
.mm_node
)
1073 ttm_bo_mem_put(bo
, &mem
);
1077 static int ttm_bo_mem_compat(struct ttm_placement
*placement
,
1078 struct ttm_mem_reg
*mem
)
1082 if (mem
->mm_node
&& placement
->lpfn
!= 0 &&
1083 (mem
->start
< placement
->fpfn
||
1084 mem
->start
+ mem
->num_pages
> placement
->lpfn
))
1087 for (i
= 0; i
< placement
->num_placement
; i
++) {
1088 if ((placement
->placement
[i
] & mem
->placement
&
1089 TTM_PL_MASK_CACHING
) &&
1090 (placement
->placement
[i
] & mem
->placement
&
1097 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1098 struct ttm_placement
*placement
,
1099 bool interruptible
, bool no_wait_reserve
,
1104 BUG_ON(!atomic_read(&bo
->reserved
));
1105 /* Check that range is valid */
1106 if (placement
->lpfn
|| placement
->fpfn
)
1107 if (placement
->fpfn
> placement
->lpfn
||
1108 (placement
->lpfn
- placement
->fpfn
) < bo
->num_pages
)
1111 * Check whether we need to move buffer.
1113 ret
= ttm_bo_mem_compat(placement
, &bo
->mem
);
1115 ret
= ttm_bo_move_buffer(bo
, placement
, interruptible
, no_wait_reserve
, no_wait_gpu
);
1120 * Use the access and other non-mapping-related flag bits from
1121 * the compatible memory placement flags to the active flags
1123 ttm_flag_masked(&bo
->mem
.placement
, placement
->placement
[ret
],
1124 ~TTM_PL_MASK_MEMTYPE
);
1127 * We might need to add a TTM.
1129 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1130 ret
= ttm_bo_add_ttm(bo
, true);
1136 EXPORT_SYMBOL(ttm_bo_validate
);
1138 int ttm_bo_check_placement(struct ttm_buffer_object
*bo
,
1139 struct ttm_placement
*placement
)
1141 BUG_ON((placement
->fpfn
|| placement
->lpfn
) &&
1142 (bo
->mem
.num_pages
> (placement
->lpfn
- placement
->fpfn
)));
1147 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1148 struct ttm_buffer_object
*bo
,
1150 enum ttm_bo_type type
,
1151 struct ttm_placement
*placement
,
1152 uint32_t page_alignment
,
1153 unsigned long buffer_start
,
1155 struct file
*persistent_swap_storage
,
1157 void (*destroy
) (struct ttm_buffer_object
*))
1160 unsigned long num_pages
;
1161 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1163 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1165 printk(KERN_ERR TTM_PFX
"Out of kernel memory.\n");
1173 size
+= buffer_start
& ~PAGE_MASK
;
1174 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1175 if (num_pages
== 0) {
1176 printk(KERN_ERR TTM_PFX
"Illegal buffer object size.\n");
1183 bo
->destroy
= destroy
;
1185 kref_init(&bo
->kref
);
1186 kref_init(&bo
->list_kref
);
1187 atomic_set(&bo
->cpu_writers
, 0);
1188 atomic_set(&bo
->reserved
, 1);
1189 init_waitqueue_head(&bo
->event_queue
);
1190 INIT_LIST_HEAD(&bo
->lru
);
1191 INIT_LIST_HEAD(&bo
->ddestroy
);
1192 INIT_LIST_HEAD(&bo
->swap
);
1193 INIT_LIST_HEAD(&bo
->io_reserve_lru
);
1195 bo
->glob
= bdev
->glob
;
1197 bo
->num_pages
= num_pages
;
1198 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1199 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1200 bo
->mem
.num_pages
= bo
->num_pages
;
1201 bo
->mem
.mm_node
= NULL
;
1202 bo
->mem
.page_alignment
= page_alignment
;
1203 bo
->mem
.bus
.io_reserved_vm
= false;
1204 bo
->mem
.bus
.io_reserved_count
= 0;
1205 bo
->buffer_start
= buffer_start
& PAGE_MASK
;
1207 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1208 bo
->seq_valid
= false;
1209 bo
->persistent_swap_storage
= persistent_swap_storage
;
1210 bo
->acc_size
= acc_size
;
1211 atomic_inc(&bo
->glob
->bo_count
);
1213 ret
= ttm_bo_check_placement(bo
, placement
);
1214 if (unlikely(ret
!= 0))
1218 * For ttm_bo_type_device buffers, allocate
1219 * address space from the device.
1221 if (bo
->type
== ttm_bo_type_device
) {
1222 ret
= ttm_bo_setup_vm(bo
);
1227 ret
= ttm_bo_validate(bo
, placement
, interruptible
, false, false);
1231 ttm_bo_unreserve(bo
);
1235 ttm_bo_unreserve(bo
);
1240 EXPORT_SYMBOL(ttm_bo_init
);
1242 size_t ttm_bo_acc_size(struct ttm_bo_device
*bdev
,
1243 unsigned long bo_size
,
1244 unsigned struct_size
)
1246 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1249 size
+= ttm_round_pot(struct_size
);
1250 size
+= PAGE_ALIGN(npages
* sizeof(void *));
1251 size
+= ttm_round_pot(sizeof(struct ttm_tt
));
1254 EXPORT_SYMBOL(ttm_bo_acc_size
);
1256 size_t ttm_bo_dma_acc_size(struct ttm_bo_device
*bdev
,
1257 unsigned long bo_size
,
1258 unsigned struct_size
)
1260 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1263 size
+= ttm_round_pot(struct_size
);
1264 size
+= PAGE_ALIGN(npages
* sizeof(void *));
1265 size
+= PAGE_ALIGN(npages
* sizeof(dma_addr_t
));
1266 size
+= ttm_round_pot(sizeof(struct ttm_dma_tt
));
1269 EXPORT_SYMBOL(ttm_bo_dma_acc_size
);
1271 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1273 enum ttm_bo_type type
,
1274 struct ttm_placement
*placement
,
1275 uint32_t page_alignment
,
1276 unsigned long buffer_start
,
1278 struct file
*persistent_swap_storage
,
1279 struct ttm_buffer_object
**p_bo
)
1281 struct ttm_buffer_object
*bo
;
1282 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1286 acc_size
= ttm_bo_acc_size(bdev
, size
, sizeof(struct ttm_buffer_object
));
1287 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1288 if (unlikely(ret
!= 0))
1291 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1293 if (unlikely(bo
== NULL
)) {
1294 ttm_mem_global_free(mem_glob
, acc_size
);
1298 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1299 buffer_start
, interruptible
,
1300 persistent_swap_storage
, acc_size
, NULL
);
1301 if (likely(ret
== 0))
1306 EXPORT_SYMBOL(ttm_bo_create
);
1308 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1309 unsigned mem_type
, bool allow_errors
)
1311 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1312 struct ttm_bo_global
*glob
= bdev
->glob
;
1316 * Can't use standard list traversal since we're unlocking.
1319 spin_lock(&glob
->lru_lock
);
1320 while (!list_empty(&man
->lru
)) {
1321 spin_unlock(&glob
->lru_lock
);
1322 ret
= ttm_mem_evict_first(bdev
, mem_type
, false, false, false);
1327 printk(KERN_ERR TTM_PFX
1328 "Cleanup eviction failed\n");
1331 spin_lock(&glob
->lru_lock
);
1333 spin_unlock(&glob
->lru_lock
);
1337 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1339 struct ttm_mem_type_manager
*man
;
1342 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1343 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", mem_type
);
1346 man
= &bdev
->man
[mem_type
];
1348 if (!man
->has_type
) {
1349 printk(KERN_ERR TTM_PFX
"Trying to take down uninitialized "
1350 "memory manager type %u\n", mem_type
);
1354 man
->use_type
= false;
1355 man
->has_type
= false;
1359 ttm_bo_force_list_clean(bdev
, mem_type
, false);
1361 ret
= (*man
->func
->takedown
)(man
);
1366 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1368 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1370 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1372 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1373 printk(KERN_ERR TTM_PFX
1374 "Illegal memory manager memory type %u.\n",
1379 if (!man
->has_type
) {
1380 printk(KERN_ERR TTM_PFX
1381 "Memory type %u has not been initialized.\n",
1386 return ttm_bo_force_list_clean(bdev
, mem_type
, true);
1388 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1390 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1391 unsigned long p_size
)
1394 struct ttm_mem_type_manager
*man
;
1396 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1397 man
= &bdev
->man
[type
];
1398 BUG_ON(man
->has_type
);
1399 man
->io_reserve_fastpath
= true;
1400 man
->use_io_reserve_lru
= false;
1401 mutex_init(&man
->io_reserve_mutex
);
1402 INIT_LIST_HEAD(&man
->io_reserve_lru
);
1404 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1410 if (type
!= TTM_PL_SYSTEM
) {
1411 ret
= (*man
->func
->init
)(man
, p_size
);
1415 man
->has_type
= true;
1416 man
->use_type
= true;
1419 INIT_LIST_HEAD(&man
->lru
);
1423 EXPORT_SYMBOL(ttm_bo_init_mm
);
1425 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1427 struct ttm_bo_global
*glob
=
1428 container_of(kobj
, struct ttm_bo_global
, kobj
);
1430 ttm_mem_unregister_shrink(glob
->mem_glob
, &glob
->shrink
);
1431 __free_page(glob
->dummy_read_page
);
1435 void ttm_bo_global_release(struct drm_global_reference
*ref
)
1437 struct ttm_bo_global
*glob
= ref
->object
;
1439 kobject_del(&glob
->kobj
);
1440 kobject_put(&glob
->kobj
);
1442 EXPORT_SYMBOL(ttm_bo_global_release
);
1444 int ttm_bo_global_init(struct drm_global_reference
*ref
)
1446 struct ttm_bo_global_ref
*bo_ref
=
1447 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1448 struct ttm_bo_global
*glob
= ref
->object
;
1451 mutex_init(&glob
->device_list_mutex
);
1452 spin_lock_init(&glob
->lru_lock
);
1453 glob
->mem_glob
= bo_ref
->mem_glob
;
1454 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1456 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1461 INIT_LIST_HEAD(&glob
->swap_lru
);
1462 INIT_LIST_HEAD(&glob
->device_list
);
1464 ttm_mem_init_shrink(&glob
->shrink
, ttm_bo_swapout
);
1465 ret
= ttm_mem_register_shrink(glob
->mem_glob
, &glob
->shrink
);
1466 if (unlikely(ret
!= 0)) {
1467 printk(KERN_ERR TTM_PFX
1468 "Could not register buffer object swapout.\n");
1472 atomic_set(&glob
->bo_count
, 0);
1474 ret
= kobject_init_and_add(
1475 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1476 if (unlikely(ret
!= 0))
1477 kobject_put(&glob
->kobj
);
1480 __free_page(glob
->dummy_read_page
);
1485 EXPORT_SYMBOL(ttm_bo_global_init
);
1488 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1491 unsigned i
= TTM_NUM_MEM_TYPES
;
1492 struct ttm_mem_type_manager
*man
;
1493 struct ttm_bo_global
*glob
= bdev
->glob
;
1496 man
= &bdev
->man
[i
];
1497 if (man
->has_type
) {
1498 man
->use_type
= false;
1499 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1501 printk(KERN_ERR TTM_PFX
1502 "DRM memory manager type %d "
1503 "is not clean.\n", i
);
1505 man
->has_type
= false;
1509 mutex_lock(&glob
->device_list_mutex
);
1510 list_del(&bdev
->device_list
);
1511 mutex_unlock(&glob
->device_list_mutex
);
1513 cancel_delayed_work_sync(&bdev
->wq
);
1515 while (ttm_bo_delayed_delete(bdev
, true))
1518 spin_lock(&glob
->lru_lock
);
1519 if (list_empty(&bdev
->ddestroy
))
1520 TTM_DEBUG("Delayed destroy list was clean\n");
1522 if (list_empty(&bdev
->man
[0].lru
))
1523 TTM_DEBUG("Swap list was clean\n");
1524 spin_unlock(&glob
->lru_lock
);
1526 BUG_ON(!drm_mm_clean(&bdev
->addr_space_mm
));
1527 write_lock(&bdev
->vm_lock
);
1528 drm_mm_takedown(&bdev
->addr_space_mm
);
1529 write_unlock(&bdev
->vm_lock
);
1533 EXPORT_SYMBOL(ttm_bo_device_release
);
1535 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1536 struct ttm_bo_global
*glob
,
1537 struct ttm_bo_driver
*driver
,
1538 uint64_t file_page_offset
,
1543 rwlock_init(&bdev
->vm_lock
);
1544 bdev
->driver
= driver
;
1546 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1549 * Initialize the system memory buffer type.
1550 * Other types need to be driver / IOCTL initialized.
1552 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1553 if (unlikely(ret
!= 0))
1556 bdev
->addr_space_rb
= RB_ROOT
;
1557 ret
= drm_mm_init(&bdev
->addr_space_mm
, file_page_offset
, 0x10000000);
1558 if (unlikely(ret
!= 0))
1559 goto out_no_addr_mm
;
1561 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1562 bdev
->nice_mode
= true;
1563 INIT_LIST_HEAD(&bdev
->ddestroy
);
1564 bdev
->dev_mapping
= NULL
;
1566 bdev
->need_dma32
= need_dma32
;
1568 spin_lock_init(&bdev
->fence_lock
);
1569 mutex_lock(&glob
->device_list_mutex
);
1570 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1571 mutex_unlock(&glob
->device_list_mutex
);
1575 ttm_bo_clean_mm(bdev
, 0);
1579 EXPORT_SYMBOL(ttm_bo_device_init
);
1582 * buffer object vm functions.
1585 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1587 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1589 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1590 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1593 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1596 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1602 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object
*bo
)
1604 struct ttm_bo_device
*bdev
= bo
->bdev
;
1605 loff_t offset
= (loff_t
) bo
->addr_space_offset
;
1606 loff_t holelen
= ((loff_t
) bo
->mem
.num_pages
) << PAGE_SHIFT
;
1608 if (!bdev
->dev_mapping
)
1610 unmap_mapping_range(bdev
->dev_mapping
, offset
, holelen
, 1);
1611 ttm_mem_io_free_vm(bo
);
1614 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1616 struct ttm_bo_device
*bdev
= bo
->bdev
;
1617 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
1619 ttm_mem_io_lock(man
, false);
1620 ttm_bo_unmap_virtual_locked(bo
);
1621 ttm_mem_io_unlock(man
);
1625 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1627 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object
*bo
)
1629 struct ttm_bo_device
*bdev
= bo
->bdev
;
1630 struct rb_node
**cur
= &bdev
->addr_space_rb
.rb_node
;
1631 struct rb_node
*parent
= NULL
;
1632 struct ttm_buffer_object
*cur_bo
;
1633 unsigned long offset
= bo
->vm_node
->start
;
1634 unsigned long cur_offset
;
1638 cur_bo
= rb_entry(parent
, struct ttm_buffer_object
, vm_rb
);
1639 cur_offset
= cur_bo
->vm_node
->start
;
1640 if (offset
< cur_offset
)
1641 cur
= &parent
->rb_left
;
1642 else if (offset
> cur_offset
)
1643 cur
= &parent
->rb_right
;
1648 rb_link_node(&bo
->vm_rb
, parent
, cur
);
1649 rb_insert_color(&bo
->vm_rb
, &bdev
->addr_space_rb
);
1655 * @bo: the buffer to allocate address space for
1657 * Allocate address space in the drm device so that applications
1658 * can mmap the buffer and access the contents. This only
1659 * applies to ttm_bo_type_device objects as others are not
1660 * placed in the drm device address space.
1663 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
)
1665 struct ttm_bo_device
*bdev
= bo
->bdev
;
1669 ret
= drm_mm_pre_get(&bdev
->addr_space_mm
);
1670 if (unlikely(ret
!= 0))
1673 write_lock(&bdev
->vm_lock
);
1674 bo
->vm_node
= drm_mm_search_free(&bdev
->addr_space_mm
,
1675 bo
->mem
.num_pages
, 0, 0);
1677 if (unlikely(bo
->vm_node
== NULL
)) {
1682 bo
->vm_node
= drm_mm_get_block_atomic(bo
->vm_node
,
1683 bo
->mem
.num_pages
, 0);
1685 if (unlikely(bo
->vm_node
== NULL
)) {
1686 write_unlock(&bdev
->vm_lock
);
1690 ttm_bo_vm_insert_rb(bo
);
1691 write_unlock(&bdev
->vm_lock
);
1692 bo
->addr_space_offset
= ((uint64_t) bo
->vm_node
->start
) << PAGE_SHIFT
;
1696 write_unlock(&bdev
->vm_lock
);
1700 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1701 bool lazy
, bool interruptible
, bool no_wait
)
1703 struct ttm_bo_driver
*driver
= bo
->bdev
->driver
;
1704 struct ttm_bo_device
*bdev
= bo
->bdev
;
1709 if (likely(bo
->sync_obj
== NULL
))
1712 while (bo
->sync_obj
) {
1714 if (driver
->sync_obj_signaled(bo
->sync_obj
, bo
->sync_obj_arg
)) {
1715 void *tmp_obj
= bo
->sync_obj
;
1716 bo
->sync_obj
= NULL
;
1717 clear_bit(TTM_BO_PRIV_FLAG_MOVING
, &bo
->priv_flags
);
1718 spin_unlock(&bdev
->fence_lock
);
1719 driver
->sync_obj_unref(&tmp_obj
);
1720 spin_lock(&bdev
->fence_lock
);
1727 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
1728 sync_obj_arg
= bo
->sync_obj_arg
;
1729 spin_unlock(&bdev
->fence_lock
);
1730 ret
= driver
->sync_obj_wait(sync_obj
, sync_obj_arg
,
1731 lazy
, interruptible
);
1732 if (unlikely(ret
!= 0)) {
1733 driver
->sync_obj_unref(&sync_obj
);
1734 spin_lock(&bdev
->fence_lock
);
1737 spin_lock(&bdev
->fence_lock
);
1738 if (likely(bo
->sync_obj
== sync_obj
&&
1739 bo
->sync_obj_arg
== sync_obj_arg
)) {
1740 void *tmp_obj
= bo
->sync_obj
;
1741 bo
->sync_obj
= NULL
;
1742 clear_bit(TTM_BO_PRIV_FLAG_MOVING
,
1744 spin_unlock(&bdev
->fence_lock
);
1745 driver
->sync_obj_unref(&sync_obj
);
1746 driver
->sync_obj_unref(&tmp_obj
);
1747 spin_lock(&bdev
->fence_lock
);
1749 spin_unlock(&bdev
->fence_lock
);
1750 driver
->sync_obj_unref(&sync_obj
);
1751 spin_lock(&bdev
->fence_lock
);
1756 EXPORT_SYMBOL(ttm_bo_wait
);
1758 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1760 struct ttm_bo_device
*bdev
= bo
->bdev
;
1764 * Using ttm_bo_reserve makes sure the lru lists are updated.
1767 ret
= ttm_bo_reserve(bo
, true, no_wait
, false, 0);
1768 if (unlikely(ret
!= 0))
1770 spin_lock(&bdev
->fence_lock
);
1771 ret
= ttm_bo_wait(bo
, false, true, no_wait
);
1772 spin_unlock(&bdev
->fence_lock
);
1773 if (likely(ret
== 0))
1774 atomic_inc(&bo
->cpu_writers
);
1775 ttm_bo_unreserve(bo
);
1778 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1780 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1782 if (atomic_dec_and_test(&bo
->cpu_writers
))
1783 wake_up_all(&bo
->event_queue
);
1785 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1788 * A buffer object shrink method that tries to swap out the first
1789 * buffer object on the bo_global::swap_lru list.
1792 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
)
1794 struct ttm_bo_global
*glob
=
1795 container_of(shrink
, struct ttm_bo_global
, shrink
);
1796 struct ttm_buffer_object
*bo
;
1799 uint32_t swap_placement
= (TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_SYSTEM
);
1801 spin_lock(&glob
->lru_lock
);
1802 while (ret
== -EBUSY
) {
1803 if (unlikely(list_empty(&glob
->swap_lru
))) {
1804 spin_unlock(&glob
->lru_lock
);
1808 bo
= list_first_entry(&glob
->swap_lru
,
1809 struct ttm_buffer_object
, swap
);
1810 kref_get(&bo
->list_kref
);
1812 if (!list_empty(&bo
->ddestroy
)) {
1813 spin_unlock(&glob
->lru_lock
);
1814 (void) ttm_bo_cleanup_refs(bo
, false, false, false);
1815 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1820 * Reserve buffer. Since we unlock while sleeping, we need
1821 * to re-check that nobody removed us from the swap-list while
1825 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
1826 if (unlikely(ret
== -EBUSY
)) {
1827 spin_unlock(&glob
->lru_lock
);
1828 ttm_bo_wait_unreserved(bo
, false);
1829 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1830 spin_lock(&glob
->lru_lock
);
1835 put_count
= ttm_bo_del_from_lru(bo
);
1836 spin_unlock(&glob
->lru_lock
);
1838 ttm_bo_list_ref_sub(bo
, put_count
, true);
1841 * Wait for GPU, then move to system cached.
1844 spin_lock(&bo
->bdev
->fence_lock
);
1845 ret
= ttm_bo_wait(bo
, false, false, false);
1846 spin_unlock(&bo
->bdev
->fence_lock
);
1848 if (unlikely(ret
!= 0))
1851 if ((bo
->mem
.placement
& swap_placement
) != swap_placement
) {
1852 struct ttm_mem_reg evict_mem
;
1854 evict_mem
= bo
->mem
;
1855 evict_mem
.mm_node
= NULL
;
1856 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1857 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1859 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true,
1860 false, false, false);
1861 if (unlikely(ret
!= 0))
1865 ttm_bo_unmap_virtual(bo
);
1868 * Swap out. Buffer will be swapped in again as soon as
1869 * anyone tries to access a ttm page.
1872 if (bo
->bdev
->driver
->swap_notify
)
1873 bo
->bdev
->driver
->swap_notify(bo
);
1875 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistent_swap_storage
);
1880 * Unreserve without putting on LRU to avoid swapping out an
1881 * already swapped buffer.
1884 atomic_set(&bo
->reserved
, 0);
1885 wake_up_all(&bo
->event_queue
);
1886 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1890 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1892 while (ttm_bo_swapout(&bdev
->glob
->shrink
) == 0)
1895 EXPORT_SYMBOL(ttm_bo_swapout_all
);