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>
41 #define TTM_ASSERT_LOCKED(param)
42 #define TTM_DEBUG(fmt, arg...)
43 #define TTM_BO_HASH_ORDER 13
45 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
);
46 static void ttm_bo_unmap_virtual(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 ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
56 struct attribute
*attr
,
59 struct ttm_bo_global
*glob
=
60 container_of(kobj
, struct ttm_bo_global
, kobj
);
62 return snprintf(buffer
, PAGE_SIZE
, "%lu\n",
63 (unsigned long) atomic_read(&glob
->bo_count
));
66 static struct attribute
*ttm_bo_global_attrs
[] = {
71 static struct sysfs_ops ttm_bo_global_ops
= {
72 .show
= &ttm_bo_global_show
75 static struct kobj_type ttm_bo_glob_kobj_type
= {
76 .release
= &ttm_bo_global_kobj_release
,
77 .sysfs_ops
= &ttm_bo_global_ops
,
78 .default_attrs
= ttm_bo_global_attrs
82 static inline uint32_t ttm_bo_type_flags(unsigned type
)
87 static void ttm_bo_release_list(struct kref
*list_kref
)
89 struct ttm_buffer_object
*bo
=
90 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
91 struct ttm_bo_device
*bdev
= bo
->bdev
;
93 BUG_ON(atomic_read(&bo
->list_kref
.refcount
));
94 BUG_ON(atomic_read(&bo
->kref
.refcount
));
95 BUG_ON(atomic_read(&bo
->cpu_writers
));
96 BUG_ON(bo
->sync_obj
!= NULL
);
97 BUG_ON(bo
->mem
.mm_node
!= NULL
);
98 BUG_ON(!list_empty(&bo
->lru
));
99 BUG_ON(!list_empty(&bo
->ddestroy
));
102 ttm_tt_destroy(bo
->ttm
);
103 atomic_dec(&bo
->glob
->bo_count
);
107 ttm_mem_global_free(bdev
->glob
->mem_glob
, bo
->acc_size
);
112 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
, bool interruptible
)
118 ret
= wait_event_interruptible(bo
->event_queue
,
119 atomic_read(&bo
->reserved
) == 0);
120 if (unlikely(ret
!= 0))
123 wait_event(bo
->event_queue
, atomic_read(&bo
->reserved
) == 0);
128 static void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
130 struct ttm_bo_device
*bdev
= bo
->bdev
;
131 struct ttm_mem_type_manager
*man
;
133 BUG_ON(!atomic_read(&bo
->reserved
));
135 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
137 BUG_ON(!list_empty(&bo
->lru
));
139 man
= &bdev
->man
[bo
->mem
.mem_type
];
140 list_add_tail(&bo
->lru
, &man
->lru
);
141 kref_get(&bo
->list_kref
);
143 if (bo
->ttm
!= NULL
) {
144 list_add_tail(&bo
->swap
, &bo
->glob
->swap_lru
);
145 kref_get(&bo
->list_kref
);
151 * Call with the lru_lock held.
154 static int ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
158 if (!list_empty(&bo
->swap
)) {
159 list_del_init(&bo
->swap
);
162 if (!list_empty(&bo
->lru
)) {
163 list_del_init(&bo
->lru
);
168 * TODO: Add a driver hook to delete from
169 * driver-specific LRU's here.
175 int ttm_bo_reserve_locked(struct ttm_buffer_object
*bo
,
177 bool no_wait
, bool use_sequence
, uint32_t sequence
)
179 struct ttm_bo_global
*glob
= bo
->glob
;
182 while (unlikely(atomic_cmpxchg(&bo
->reserved
, 0, 1) != 0)) {
183 if (use_sequence
&& bo
->seq_valid
&&
184 (sequence
- bo
->val_seq
< (1 << 31))) {
191 spin_unlock(&glob
->lru_lock
);
192 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
193 spin_lock(&glob
->lru_lock
);
200 bo
->val_seq
= sequence
;
201 bo
->seq_valid
= true;
203 bo
->seq_valid
= false;
208 EXPORT_SYMBOL(ttm_bo_reserve
);
210 static void ttm_bo_ref_bug(struct kref
*list_kref
)
215 int ttm_bo_reserve(struct ttm_buffer_object
*bo
,
217 bool no_wait
, bool use_sequence
, uint32_t sequence
)
219 struct ttm_bo_global
*glob
= bo
->glob
;
223 spin_lock(&glob
->lru_lock
);
224 ret
= ttm_bo_reserve_locked(bo
, interruptible
, no_wait
, use_sequence
,
226 if (likely(ret
== 0))
227 put_count
= ttm_bo_del_from_lru(bo
);
228 spin_unlock(&glob
->lru_lock
);
231 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
236 void ttm_bo_unreserve(struct ttm_buffer_object
*bo
)
238 struct ttm_bo_global
*glob
= bo
->glob
;
240 spin_lock(&glob
->lru_lock
);
241 ttm_bo_add_to_lru(bo
);
242 atomic_set(&bo
->reserved
, 0);
243 wake_up_all(&bo
->event_queue
);
244 spin_unlock(&glob
->lru_lock
);
246 EXPORT_SYMBOL(ttm_bo_unreserve
);
249 * Call bo->mutex locked.
252 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
254 struct ttm_bo_device
*bdev
= bo
->bdev
;
255 struct ttm_bo_global
*glob
= bo
->glob
;
257 uint32_t page_flags
= 0;
259 TTM_ASSERT_LOCKED(&bo
->mutex
);
263 case ttm_bo_type_device
:
265 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
266 case ttm_bo_type_kernel
:
267 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
268 page_flags
, glob
->dummy_read_page
);
269 if (unlikely(bo
->ttm
== NULL
))
272 case ttm_bo_type_user
:
273 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
274 page_flags
| TTM_PAGE_FLAG_USER
,
275 glob
->dummy_read_page
);
276 if (unlikely(bo
->ttm
== NULL
))
280 ret
= ttm_tt_set_user(bo
->ttm
, current
,
281 bo
->buffer_start
, bo
->num_pages
);
282 if (unlikely(ret
!= 0))
283 ttm_tt_destroy(bo
->ttm
);
286 printk(KERN_ERR TTM_PFX
"Illegal buffer object type\n");
294 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
295 struct ttm_mem_reg
*mem
,
296 bool evict
, bool interruptible
, bool no_wait
)
298 struct ttm_bo_device
*bdev
= bo
->bdev
;
299 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
300 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
301 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
302 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
305 if (old_is_pci
|| new_is_pci
||
306 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0))
307 ttm_bo_unmap_virtual(bo
);
310 * Create and bind a ttm if required.
313 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && (bo
->ttm
== NULL
)) {
314 ret
= ttm_bo_add_ttm(bo
, false);
318 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
322 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
323 ret
= ttm_tt_bind(bo
->ttm
, mem
);
328 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
330 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
331 uint32_t save_flags
= old_mem
->placement
;
335 ttm_flag_masked(&save_flags
, mem
->placement
,
336 TTM_PL_MASK_MEMTYPE
);
342 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
343 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
344 ret
= ttm_bo_move_ttm(bo
, evict
, no_wait
, mem
);
345 else if (bdev
->driver
->move
)
346 ret
= bdev
->driver
->move(bo
, evict
, interruptible
,
349 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait
, mem
);
356 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
358 printk(KERN_ERR TTM_PFX
"Can not flush read caches\n");
362 if (bo
->mem
.mm_node
) {
363 spin_lock(&bo
->lock
);
364 bo
->offset
= (bo
->mem
.mm_node
->start
<< PAGE_SHIFT
) +
365 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
366 bo
->cur_placement
= bo
->mem
.placement
;
367 spin_unlock(&bo
->lock
);
373 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
374 if ((new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
) {
375 ttm_tt_unbind(bo
->ttm
);
376 ttm_tt_destroy(bo
->ttm
);
384 * If bo idle, remove from delayed- and lru lists, and unref.
385 * If not idle, and already on delayed list, do nothing.
386 * If not idle, and not on delayed list, put on delayed list,
387 * up the list_kref and schedule a delayed list check.
390 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
, bool remove_all
)
392 struct ttm_bo_device
*bdev
= bo
->bdev
;
393 struct ttm_bo_global
*glob
= bo
->glob
;
394 struct ttm_bo_driver
*driver
= bdev
->driver
;
397 spin_lock(&bo
->lock
);
398 (void) ttm_bo_wait(bo
, false, false, !remove_all
);
403 spin_unlock(&bo
->lock
);
405 spin_lock(&glob
->lru_lock
);
406 ret
= ttm_bo_reserve_locked(bo
, false, false, false, 0);
409 ttm_tt_unbind(bo
->ttm
);
411 if (!list_empty(&bo
->ddestroy
)) {
412 list_del_init(&bo
->ddestroy
);
413 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
415 if (bo
->mem
.mm_node
) {
416 drm_mm_put_block(bo
->mem
.mm_node
);
417 bo
->mem
.mm_node
= NULL
;
419 put_count
= ttm_bo_del_from_lru(bo
);
420 spin_unlock(&glob
->lru_lock
);
422 atomic_set(&bo
->reserved
, 0);
425 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
430 spin_lock(&glob
->lru_lock
);
431 if (list_empty(&bo
->ddestroy
)) {
432 void *sync_obj
= bo
->sync_obj
;
433 void *sync_obj_arg
= bo
->sync_obj_arg
;
435 kref_get(&bo
->list_kref
);
436 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
437 spin_unlock(&glob
->lru_lock
);
438 spin_unlock(&bo
->lock
);
441 driver
->sync_obj_flush(sync_obj
, sync_obj_arg
);
442 schedule_delayed_work(&bdev
->wq
,
443 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
447 spin_unlock(&glob
->lru_lock
);
448 spin_unlock(&bo
->lock
);
456 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
457 * encountered buffers.
460 static int ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
462 struct ttm_bo_global
*glob
= bdev
->glob
;
463 struct ttm_buffer_object
*entry
, *nentry
;
464 struct list_head
*list
, *next
;
467 spin_lock(&glob
->lru_lock
);
468 list_for_each_safe(list
, next
, &bdev
->ddestroy
) {
469 entry
= list_entry(list
, struct ttm_buffer_object
, ddestroy
);
473 * Protect the next list entry from destruction while we
474 * unlock the lru_lock.
477 if (next
!= &bdev
->ddestroy
) {
478 nentry
= list_entry(next
, struct ttm_buffer_object
,
480 kref_get(&nentry
->list_kref
);
482 kref_get(&entry
->list_kref
);
484 spin_unlock(&glob
->lru_lock
);
485 ret
= ttm_bo_cleanup_refs(entry
, remove_all
);
486 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
488 spin_lock(&glob
->lru_lock
);
490 bool next_onlist
= !list_empty(next
);
491 spin_unlock(&glob
->lru_lock
);
492 kref_put(&nentry
->list_kref
, ttm_bo_release_list
);
493 spin_lock(&glob
->lru_lock
);
495 * Someone might have raced us and removed the
496 * next entry from the list. We don't bother restarting
506 ret
= !list_empty(&bdev
->ddestroy
);
507 spin_unlock(&glob
->lru_lock
);
512 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
514 struct ttm_bo_device
*bdev
=
515 container_of(work
, struct ttm_bo_device
, wq
.work
);
517 if (ttm_bo_delayed_delete(bdev
, false)) {
518 schedule_delayed_work(&bdev
->wq
,
519 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
523 static void ttm_bo_release(struct kref
*kref
)
525 struct ttm_buffer_object
*bo
=
526 container_of(kref
, struct ttm_buffer_object
, kref
);
527 struct ttm_bo_device
*bdev
= bo
->bdev
;
529 if (likely(bo
->vm_node
!= NULL
)) {
530 rb_erase(&bo
->vm_rb
, &bdev
->addr_space_rb
);
531 drm_mm_put_block(bo
->vm_node
);
534 write_unlock(&bdev
->vm_lock
);
535 ttm_bo_cleanup_refs(bo
, false);
536 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
537 write_lock(&bdev
->vm_lock
);
540 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
542 struct ttm_buffer_object
*bo
= *p_bo
;
543 struct ttm_bo_device
*bdev
= bo
->bdev
;
546 write_lock(&bdev
->vm_lock
);
547 kref_put(&bo
->kref
, ttm_bo_release
);
548 write_unlock(&bdev
->vm_lock
);
550 EXPORT_SYMBOL(ttm_bo_unref
);
552 static int ttm_bo_evict(struct ttm_buffer_object
*bo
, unsigned mem_type
,
553 bool interruptible
, bool no_wait
)
556 struct ttm_bo_device
*bdev
= bo
->bdev
;
557 struct ttm_bo_global
*glob
= bo
->glob
;
558 struct ttm_mem_reg evict_mem
;
559 uint32_t proposed_placement
;
561 if (bo
->mem
.mem_type
!= mem_type
)
564 spin_lock(&bo
->lock
);
565 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait
);
566 spin_unlock(&bo
->lock
);
568 if (unlikely(ret
!= 0)) {
569 if (ret
!= -ERESTART
) {
570 printk(KERN_ERR TTM_PFX
571 "Failed to expire sync object before "
572 "buffer eviction.\n");
577 BUG_ON(!atomic_read(&bo
->reserved
));
580 evict_mem
.mm_node
= NULL
;
582 proposed_placement
= bdev
->driver
->evict_flags(bo
);
584 ret
= ttm_bo_mem_space(bo
, proposed_placement
,
585 &evict_mem
, interruptible
, no_wait
);
586 if (unlikely(ret
!= 0 && ret
!= -ERESTART
))
587 ret
= ttm_bo_mem_space(bo
, TTM_PL_FLAG_SYSTEM
,
588 &evict_mem
, interruptible
, no_wait
);
591 if (ret
!= -ERESTART
)
592 printk(KERN_ERR TTM_PFX
593 "Failed to find memory space for "
594 "buffer 0x%p eviction.\n", bo
);
598 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, interruptible
,
601 if (ret
!= -ERESTART
)
602 printk(KERN_ERR TTM_PFX
"Buffer eviction failed\n");
606 spin_lock(&glob
->lru_lock
);
607 if (evict_mem
.mm_node
) {
608 drm_mm_put_block(evict_mem
.mm_node
);
609 evict_mem
.mm_node
= NULL
;
611 spin_unlock(&glob
->lru_lock
);
618 * Repeatedly evict memory from the LRU for @mem_type until we create enough
619 * space, or we've evicted everything and there isn't enough space.
621 static int ttm_bo_mem_force_space(struct ttm_bo_device
*bdev
,
622 struct ttm_mem_reg
*mem
,
624 bool interruptible
, bool no_wait
)
626 struct ttm_bo_global
*glob
= bdev
->glob
;
627 struct drm_mm_node
*node
;
628 struct ttm_buffer_object
*entry
;
629 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
630 struct list_head
*lru
;
631 unsigned long num_pages
= mem
->num_pages
;
636 ret
= drm_mm_pre_get(&man
->manager
);
637 if (unlikely(ret
!= 0))
640 spin_lock(&glob
->lru_lock
);
642 node
= drm_mm_search_free(&man
->manager
, num_pages
,
643 mem
->page_alignment
, 1);
651 entry
= list_first_entry(lru
, struct ttm_buffer_object
, lru
);
652 kref_get(&entry
->list_kref
);
655 ttm_bo_reserve_locked(entry
, interruptible
, no_wait
,
658 if (likely(ret
== 0))
659 put_count
= ttm_bo_del_from_lru(entry
);
661 spin_unlock(&glob
->lru_lock
);
663 if (unlikely(ret
!= 0))
667 kref_put(&entry
->list_kref
, ttm_bo_ref_bug
);
669 ret
= ttm_bo_evict(entry
, mem_type
, interruptible
, no_wait
);
671 ttm_bo_unreserve(entry
);
673 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
677 spin_lock(&glob
->lru_lock
);
681 spin_unlock(&glob
->lru_lock
);
685 node
= drm_mm_get_block_atomic(node
, num_pages
, mem
->page_alignment
);
686 if (unlikely(!node
)) {
687 spin_unlock(&glob
->lru_lock
);
691 spin_unlock(&glob
->lru_lock
);
693 mem
->mem_type
= mem_type
;
697 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
700 uint32_t mask
, uint32_t *res_mask
)
702 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
704 if ((man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && disallow_fixed
)
707 if ((cur_flags
& mask
& TTM_PL_MASK_MEM
) == 0)
710 if ((mask
& man
->available_caching
) == 0)
712 if (mask
& man
->default_caching
)
713 cur_flags
|= man
->default_caching
;
714 else if (mask
& TTM_PL_FLAG_CACHED
)
715 cur_flags
|= TTM_PL_FLAG_CACHED
;
716 else if (mask
& TTM_PL_FLAG_WC
)
717 cur_flags
|= TTM_PL_FLAG_WC
;
719 cur_flags
|= TTM_PL_FLAG_UNCACHED
;
721 *res_mask
= cur_flags
;
726 * Creates space for memory region @mem according to its type.
728 * This function first searches for free space in compatible memory types in
729 * the priority order defined by the driver. If free space isn't found, then
730 * ttm_bo_mem_force_space is attempted in priority order to evict and find
733 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
734 uint32_t proposed_placement
,
735 struct ttm_mem_reg
*mem
,
736 bool interruptible
, bool no_wait
)
738 struct ttm_bo_device
*bdev
= bo
->bdev
;
739 struct ttm_bo_global
*glob
= bo
->glob
;
740 struct ttm_mem_type_manager
*man
;
742 uint32_t num_prios
= bdev
->driver
->num_mem_type_prio
;
743 const uint32_t *prios
= bdev
->driver
->mem_type_prio
;
745 uint32_t mem_type
= TTM_PL_SYSTEM
;
746 uint32_t cur_flags
= 0;
747 bool type_found
= false;
748 bool type_ok
= false;
749 bool has_eagain
= false;
750 struct drm_mm_node
*node
= NULL
;
754 for (i
= 0; i
< num_prios
; ++i
) {
756 man
= &bdev
->man
[mem_type
];
758 type_ok
= ttm_bo_mt_compatible(man
,
759 bo
->type
== ttm_bo_type_user
,
760 mem_type
, proposed_placement
,
766 if (mem_type
== TTM_PL_SYSTEM
)
769 if (man
->has_type
&& man
->use_type
) {
772 ret
= drm_mm_pre_get(&man
->manager
);
776 spin_lock(&glob
->lru_lock
);
777 node
= drm_mm_search_free(&man
->manager
,
781 if (unlikely(!node
)) {
782 spin_unlock(&glob
->lru_lock
);
785 node
= drm_mm_get_block_atomic(node
,
789 spin_unlock(&glob
->lru_lock
);
796 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || node
) {
798 mem
->mem_type
= mem_type
;
799 mem
->placement
= cur_flags
;
806 num_prios
= bdev
->driver
->num_mem_busy_prio
;
807 prios
= bdev
->driver
->mem_busy_prio
;
809 for (i
= 0; i
< num_prios
; ++i
) {
811 man
= &bdev
->man
[mem_type
];
816 if (!ttm_bo_mt_compatible(man
,
817 bo
->type
== ttm_bo_type_user
,
819 proposed_placement
, &cur_flags
))
822 ret
= ttm_bo_mem_force_space(bdev
, mem
, mem_type
,
823 interruptible
, no_wait
);
825 if (ret
== 0 && mem
->mm_node
) {
826 mem
->placement
= cur_flags
;
830 if (ret
== -ERESTART
)
834 ret
= (has_eagain
) ? -ERESTART
: -ENOMEM
;
837 EXPORT_SYMBOL(ttm_bo_mem_space
);
839 int ttm_bo_wait_cpu(struct ttm_buffer_object
*bo
, bool no_wait
)
843 if ((atomic_read(&bo
->cpu_writers
) > 0) && no_wait
)
846 ret
= wait_event_interruptible(bo
->event_queue
,
847 atomic_read(&bo
->cpu_writers
) == 0);
849 if (ret
== -ERESTARTSYS
)
855 int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
856 uint32_t proposed_placement
,
857 bool interruptible
, bool no_wait
)
859 struct ttm_bo_global
*glob
= bo
->glob
;
861 struct ttm_mem_reg mem
;
863 BUG_ON(!atomic_read(&bo
->reserved
));
866 * FIXME: It's possible to pipeline buffer moves.
867 * Have the driver move function wait for idle when necessary,
868 * instead of doing it here.
871 spin_lock(&bo
->lock
);
872 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait
);
873 spin_unlock(&bo
->lock
);
878 mem
.num_pages
= bo
->num_pages
;
879 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
880 mem
.page_alignment
= bo
->mem
.page_alignment
;
883 * Determine where to move the buffer.
886 ret
= ttm_bo_mem_space(bo
, proposed_placement
, &mem
,
887 interruptible
, no_wait
);
891 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, interruptible
, no_wait
);
894 if (ret
&& mem
.mm_node
) {
895 spin_lock(&glob
->lru_lock
);
896 drm_mm_put_block(mem
.mm_node
);
897 spin_unlock(&glob
->lru_lock
);
902 static int ttm_bo_mem_compat(uint32_t proposed_placement
,
903 struct ttm_mem_reg
*mem
)
905 if ((proposed_placement
& mem
->placement
& TTM_PL_MASK_MEM
) == 0)
907 if ((proposed_placement
& mem
->placement
& TTM_PL_MASK_CACHING
) == 0)
913 int ttm_buffer_object_validate(struct ttm_buffer_object
*bo
,
914 uint32_t proposed_placement
,
915 bool interruptible
, bool no_wait
)
919 BUG_ON(!atomic_read(&bo
->reserved
));
920 bo
->proposed_placement
= proposed_placement
;
922 TTM_DEBUG("Proposed placement 0x%08lx, Old flags 0x%08lx\n",
923 (unsigned long)proposed_placement
,
924 (unsigned long)bo
->mem
.placement
);
927 * Check whether we need to move buffer.
930 if (!ttm_bo_mem_compat(bo
->proposed_placement
, &bo
->mem
)) {
931 ret
= ttm_bo_move_buffer(bo
, bo
->proposed_placement
,
932 interruptible
, no_wait
);
934 if (ret
!= -ERESTART
)
935 printk(KERN_ERR TTM_PFX
936 "Failed moving buffer. "
937 "Proposed placement 0x%08x\n",
938 bo
->proposed_placement
);
940 printk(KERN_ERR TTM_PFX
941 "Out of aperture space or "
942 "DRM memory quota.\n");
948 * We might need to add a TTM.
951 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
952 ret
= ttm_bo_add_ttm(bo
, true);
957 * Validation has succeeded, move the access and other
958 * non-mapping-related flag bits from the proposed flags to
962 ttm_flag_masked(&bo
->mem
.placement
, bo
->proposed_placement
,
963 ~TTM_PL_MASK_MEMTYPE
);
967 EXPORT_SYMBOL(ttm_buffer_object_validate
);
970 ttm_bo_check_placement(struct ttm_buffer_object
*bo
,
971 uint32_t set_flags
, uint32_t clr_flags
)
973 uint32_t new_mask
= set_flags
| clr_flags
;
975 if ((bo
->type
== ttm_bo_type_user
) &&
976 (clr_flags
& TTM_PL_FLAG_CACHED
)) {
977 printk(KERN_ERR TTM_PFX
978 "User buffers require cache-coherent memory.\n");
982 if (!capable(CAP_SYS_ADMIN
)) {
983 if (new_mask
& TTM_PL_FLAG_NO_EVICT
) {
984 printk(KERN_ERR TTM_PFX
"Need to be root to modify"
985 " NO_EVICT status.\n");
989 if ((clr_flags
& bo
->mem
.placement
& TTM_PL_MASK_MEMTYPE
) &&
990 (bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
991 printk(KERN_ERR TTM_PFX
992 "Incompatible memory specification"
993 " for NO_EVICT buffer.\n");
1000 int ttm_buffer_object_init(struct ttm_bo_device
*bdev
,
1001 struct ttm_buffer_object
*bo
,
1003 enum ttm_bo_type type
,
1005 uint32_t page_alignment
,
1006 unsigned long buffer_start
,
1008 struct file
*persistant_swap_storage
,
1010 void (*destroy
) (struct ttm_buffer_object
*))
1013 unsigned long num_pages
;
1015 size
+= buffer_start
& ~PAGE_MASK
;
1016 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1017 if (num_pages
== 0) {
1018 printk(KERN_ERR TTM_PFX
"Illegal buffer object size.\n");
1021 bo
->destroy
= destroy
;
1023 spin_lock_init(&bo
->lock
);
1024 kref_init(&bo
->kref
);
1025 kref_init(&bo
->list_kref
);
1026 atomic_set(&bo
->cpu_writers
, 0);
1027 atomic_set(&bo
->reserved
, 1);
1028 init_waitqueue_head(&bo
->event_queue
);
1029 INIT_LIST_HEAD(&bo
->lru
);
1030 INIT_LIST_HEAD(&bo
->ddestroy
);
1031 INIT_LIST_HEAD(&bo
->swap
);
1033 bo
->glob
= bdev
->glob
;
1035 bo
->num_pages
= num_pages
;
1036 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1037 bo
->mem
.num_pages
= bo
->num_pages
;
1038 bo
->mem
.mm_node
= NULL
;
1039 bo
->mem
.page_alignment
= page_alignment
;
1040 bo
->buffer_start
= buffer_start
& PAGE_MASK
;
1042 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1043 bo
->seq_valid
= false;
1044 bo
->persistant_swap_storage
= persistant_swap_storage
;
1045 bo
->acc_size
= acc_size
;
1046 atomic_inc(&bo
->glob
->bo_count
);
1048 ret
= ttm_bo_check_placement(bo
, flags
, 0ULL);
1049 if (unlikely(ret
!= 0))
1053 * If no caching attributes are set, accept any form of caching.
1056 if ((flags
& TTM_PL_MASK_CACHING
) == 0)
1057 flags
|= TTM_PL_MASK_CACHING
;
1060 * For ttm_bo_type_device buffers, allocate
1061 * address space from the device.
1064 if (bo
->type
== ttm_bo_type_device
) {
1065 ret
= ttm_bo_setup_vm(bo
);
1070 ret
= ttm_buffer_object_validate(bo
, flags
, interruptible
, false);
1074 ttm_bo_unreserve(bo
);
1078 ttm_bo_unreserve(bo
);
1083 EXPORT_SYMBOL(ttm_buffer_object_init
);
1085 static inline size_t ttm_bo_size(struct ttm_bo_global
*glob
,
1086 unsigned long num_pages
)
1088 size_t page_array_size
= (num_pages
* sizeof(void *) + PAGE_SIZE
- 1) &
1091 return glob
->ttm_bo_size
+ 2 * page_array_size
;
1094 int ttm_buffer_object_create(struct ttm_bo_device
*bdev
,
1096 enum ttm_bo_type type
,
1098 uint32_t page_alignment
,
1099 unsigned long buffer_start
,
1101 struct file
*persistant_swap_storage
,
1102 struct ttm_buffer_object
**p_bo
)
1104 struct ttm_buffer_object
*bo
;
1106 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1109 ttm_bo_size(bdev
->glob
, (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
);
1110 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1111 if (unlikely(ret
!= 0))
1114 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1116 if (unlikely(bo
== NULL
)) {
1117 ttm_mem_global_free(mem_glob
, acc_size
);
1121 ret
= ttm_buffer_object_init(bdev
, bo
, size
, type
, flags
,
1122 page_alignment
, buffer_start
,
1124 persistant_swap_storage
, acc_size
, NULL
);
1125 if (likely(ret
== 0))
1131 static int ttm_bo_leave_list(struct ttm_buffer_object
*bo
,
1132 uint32_t mem_type
, bool allow_errors
)
1136 spin_lock(&bo
->lock
);
1137 ret
= ttm_bo_wait(bo
, false, false, false);
1138 spin_unlock(&bo
->lock
);
1140 if (ret
&& allow_errors
)
1143 if (bo
->mem
.mem_type
== mem_type
)
1144 ret
= ttm_bo_evict(bo
, mem_type
, false, false);
1151 printk(KERN_ERR TTM_PFX
"Cleanup eviction failed\n");
1159 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1160 struct list_head
*head
,
1161 unsigned mem_type
, bool allow_errors
)
1163 struct ttm_bo_global
*glob
= bdev
->glob
;
1164 struct ttm_buffer_object
*entry
;
1169 * Can't use standard list traversal since we're unlocking.
1172 spin_lock(&glob
->lru_lock
);
1174 while (!list_empty(head
)) {
1175 entry
= list_first_entry(head
, struct ttm_buffer_object
, lru
);
1176 kref_get(&entry
->list_kref
);
1177 ret
= ttm_bo_reserve_locked(entry
, false, false, false, 0);
1178 put_count
= ttm_bo_del_from_lru(entry
);
1179 spin_unlock(&glob
->lru_lock
);
1181 kref_put(&entry
->list_kref
, ttm_bo_ref_bug
);
1183 ret
= ttm_bo_leave_list(entry
, mem_type
, allow_errors
);
1184 ttm_bo_unreserve(entry
);
1185 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
1186 spin_lock(&glob
->lru_lock
);
1189 spin_unlock(&glob
->lru_lock
);
1194 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1196 struct ttm_bo_global
*glob
= bdev
->glob
;
1197 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1200 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1201 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", mem_type
);
1205 if (!man
->has_type
) {
1206 printk(KERN_ERR TTM_PFX
"Trying to take down uninitialized "
1207 "memory manager type %u\n", mem_type
);
1211 man
->use_type
= false;
1212 man
->has_type
= false;
1216 ttm_bo_force_list_clean(bdev
, &man
->lru
, mem_type
, false);
1218 spin_lock(&glob
->lru_lock
);
1219 if (drm_mm_clean(&man
->manager
))
1220 drm_mm_takedown(&man
->manager
);
1224 spin_unlock(&glob
->lru_lock
);
1229 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1231 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1233 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1235 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1236 printk(KERN_ERR TTM_PFX
1237 "Illegal memory manager memory type %u.\n",
1242 if (!man
->has_type
) {
1243 printk(KERN_ERR TTM_PFX
1244 "Memory type %u has not been initialized.\n",
1249 return ttm_bo_force_list_clean(bdev
, &man
->lru
, mem_type
, true);
1251 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1253 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1254 unsigned long p_offset
, unsigned long p_size
)
1257 struct ttm_mem_type_manager
*man
;
1259 if (type
>= TTM_NUM_MEM_TYPES
) {
1260 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", type
);
1264 man
= &bdev
->man
[type
];
1265 if (man
->has_type
) {
1266 printk(KERN_ERR TTM_PFX
1267 "Memory manager already initialized for type %d\n",
1272 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1277 if (type
!= TTM_PL_SYSTEM
) {
1279 printk(KERN_ERR TTM_PFX
1280 "Zero size memory manager type %d\n",
1284 ret
= drm_mm_init(&man
->manager
, p_offset
, p_size
);
1288 man
->has_type
= true;
1289 man
->use_type
= true;
1292 INIT_LIST_HEAD(&man
->lru
);
1296 EXPORT_SYMBOL(ttm_bo_init_mm
);
1298 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1300 struct ttm_bo_global
*glob
=
1301 container_of(kobj
, struct ttm_bo_global
, kobj
);
1303 printk(KERN_INFO TTM_PFX
"Freeing bo global.\n");
1304 ttm_mem_unregister_shrink(glob
->mem_glob
, &glob
->shrink
);
1305 __free_page(glob
->dummy_read_page
);
1309 void ttm_bo_global_release(struct ttm_global_reference
*ref
)
1311 struct ttm_bo_global
*glob
= ref
->object
;
1313 kobject_del(&glob
->kobj
);
1314 kobject_put(&glob
->kobj
);
1316 EXPORT_SYMBOL(ttm_bo_global_release
);
1318 int ttm_bo_global_init(struct ttm_global_reference
*ref
)
1320 struct ttm_bo_global_ref
*bo_ref
=
1321 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1322 struct ttm_bo_global
*glob
= ref
->object
;
1325 mutex_init(&glob
->device_list_mutex
);
1326 spin_lock_init(&glob
->lru_lock
);
1327 glob
->mem_glob
= bo_ref
->mem_glob
;
1328 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1330 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1335 INIT_LIST_HEAD(&glob
->swap_lru
);
1336 INIT_LIST_HEAD(&glob
->device_list
);
1338 ttm_mem_init_shrink(&glob
->shrink
, ttm_bo_swapout
);
1339 ret
= ttm_mem_register_shrink(glob
->mem_glob
, &glob
->shrink
);
1340 if (unlikely(ret
!= 0)) {
1341 printk(KERN_ERR TTM_PFX
1342 "Could not register buffer object swapout.\n");
1346 glob
->ttm_bo_extra_size
=
1347 ttm_round_pot(sizeof(struct ttm_tt
)) +
1348 ttm_round_pot(sizeof(struct ttm_backend
));
1350 glob
->ttm_bo_size
= glob
->ttm_bo_extra_size
+
1351 ttm_round_pot(sizeof(struct ttm_buffer_object
));
1353 atomic_set(&glob
->bo_count
, 0);
1355 kobject_init(&glob
->kobj
, &ttm_bo_glob_kobj_type
);
1356 ret
= kobject_add(&glob
->kobj
, ttm_get_kobj(), "buffer_objects");
1357 if (unlikely(ret
!= 0))
1358 kobject_put(&glob
->kobj
);
1361 __free_page(glob
->dummy_read_page
);
1366 EXPORT_SYMBOL(ttm_bo_global_init
);
1369 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1372 unsigned i
= TTM_NUM_MEM_TYPES
;
1373 struct ttm_mem_type_manager
*man
;
1374 struct ttm_bo_global
*glob
= bdev
->glob
;
1377 man
= &bdev
->man
[i
];
1378 if (man
->has_type
) {
1379 man
->use_type
= false;
1380 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1382 printk(KERN_ERR TTM_PFX
1383 "DRM memory manager type %d "
1384 "is not clean.\n", i
);
1386 man
->has_type
= false;
1390 mutex_lock(&glob
->device_list_mutex
);
1391 list_del(&bdev
->device_list
);
1392 mutex_unlock(&glob
->device_list_mutex
);
1394 if (!cancel_delayed_work(&bdev
->wq
))
1395 flush_scheduled_work();
1397 while (ttm_bo_delayed_delete(bdev
, true))
1400 spin_lock(&glob
->lru_lock
);
1401 if (list_empty(&bdev
->ddestroy
))
1402 TTM_DEBUG("Delayed destroy list was clean\n");
1404 if (list_empty(&bdev
->man
[0].lru
))
1405 TTM_DEBUG("Swap list was clean\n");
1406 spin_unlock(&glob
->lru_lock
);
1408 BUG_ON(!drm_mm_clean(&bdev
->addr_space_mm
));
1409 write_lock(&bdev
->vm_lock
);
1410 drm_mm_takedown(&bdev
->addr_space_mm
);
1411 write_unlock(&bdev
->vm_lock
);
1415 EXPORT_SYMBOL(ttm_bo_device_release
);
1417 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1418 struct ttm_bo_global
*glob
,
1419 struct ttm_bo_driver
*driver
,
1420 uint64_t file_page_offset
)
1424 rwlock_init(&bdev
->vm_lock
);
1425 spin_lock_init(&glob
->lru_lock
);
1427 bdev
->driver
= driver
;
1429 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1432 * Initialize the system memory buffer type.
1433 * Other types need to be driver / IOCTL initialized.
1435 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0, 0);
1436 if (unlikely(ret
!= 0))
1439 bdev
->addr_space_rb
= RB_ROOT
;
1440 ret
= drm_mm_init(&bdev
->addr_space_mm
, file_page_offset
, 0x10000000);
1441 if (unlikely(ret
!= 0))
1442 goto out_no_addr_mm
;
1444 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1445 bdev
->nice_mode
= true;
1446 INIT_LIST_HEAD(&bdev
->ddestroy
);
1447 bdev
->dev_mapping
= NULL
;
1450 mutex_lock(&glob
->device_list_mutex
);
1451 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1452 mutex_unlock(&glob
->device_list_mutex
);
1456 ttm_bo_clean_mm(bdev
, 0);
1460 EXPORT_SYMBOL(ttm_bo_device_init
);
1463 * buffer object vm functions.
1466 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1468 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1470 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1471 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1474 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1477 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1483 int ttm_bo_pci_offset(struct ttm_bo_device
*bdev
,
1484 struct ttm_mem_reg
*mem
,
1485 unsigned long *bus_base
,
1486 unsigned long *bus_offset
, unsigned long *bus_size
)
1488 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1491 if (!(man
->flags
& TTM_MEMTYPE_FLAG_MAPPABLE
))
1494 if (ttm_mem_reg_is_pci(bdev
, mem
)) {
1495 *bus_offset
= mem
->mm_node
->start
<< PAGE_SHIFT
;
1496 *bus_size
= mem
->num_pages
<< PAGE_SHIFT
;
1497 *bus_base
= man
->io_offset
;
1503 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1505 struct ttm_bo_device
*bdev
= bo
->bdev
;
1506 loff_t offset
= (loff_t
) bo
->addr_space_offset
;
1507 loff_t holelen
= ((loff_t
) bo
->mem
.num_pages
) << PAGE_SHIFT
;
1509 if (!bdev
->dev_mapping
)
1512 unmap_mapping_range(bdev
->dev_mapping
, offset
, holelen
, 1);
1515 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object
*bo
)
1517 struct ttm_bo_device
*bdev
= bo
->bdev
;
1518 struct rb_node
**cur
= &bdev
->addr_space_rb
.rb_node
;
1519 struct rb_node
*parent
= NULL
;
1520 struct ttm_buffer_object
*cur_bo
;
1521 unsigned long offset
= bo
->vm_node
->start
;
1522 unsigned long cur_offset
;
1526 cur_bo
= rb_entry(parent
, struct ttm_buffer_object
, vm_rb
);
1527 cur_offset
= cur_bo
->vm_node
->start
;
1528 if (offset
< cur_offset
)
1529 cur
= &parent
->rb_left
;
1530 else if (offset
> cur_offset
)
1531 cur
= &parent
->rb_right
;
1536 rb_link_node(&bo
->vm_rb
, parent
, cur
);
1537 rb_insert_color(&bo
->vm_rb
, &bdev
->addr_space_rb
);
1543 * @bo: the buffer to allocate address space for
1545 * Allocate address space in the drm device so that applications
1546 * can mmap the buffer and access the contents. This only
1547 * applies to ttm_bo_type_device objects as others are not
1548 * placed in the drm device address space.
1551 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
)
1553 struct ttm_bo_device
*bdev
= bo
->bdev
;
1557 ret
= drm_mm_pre_get(&bdev
->addr_space_mm
);
1558 if (unlikely(ret
!= 0))
1561 write_lock(&bdev
->vm_lock
);
1562 bo
->vm_node
= drm_mm_search_free(&bdev
->addr_space_mm
,
1563 bo
->mem
.num_pages
, 0, 0);
1565 if (unlikely(bo
->vm_node
== NULL
)) {
1570 bo
->vm_node
= drm_mm_get_block_atomic(bo
->vm_node
,
1571 bo
->mem
.num_pages
, 0);
1573 if (unlikely(bo
->vm_node
== NULL
)) {
1574 write_unlock(&bdev
->vm_lock
);
1578 ttm_bo_vm_insert_rb(bo
);
1579 write_unlock(&bdev
->vm_lock
);
1580 bo
->addr_space_offset
= ((uint64_t) bo
->vm_node
->start
) << PAGE_SHIFT
;
1584 write_unlock(&bdev
->vm_lock
);
1588 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1589 bool lazy
, bool interruptible
, bool no_wait
)
1591 struct ttm_bo_driver
*driver
= bo
->bdev
->driver
;
1596 if (likely(bo
->sync_obj
== NULL
))
1599 while (bo
->sync_obj
) {
1601 if (driver
->sync_obj_signaled(bo
->sync_obj
, bo
->sync_obj_arg
)) {
1602 void *tmp_obj
= bo
->sync_obj
;
1603 bo
->sync_obj
= NULL
;
1604 clear_bit(TTM_BO_PRIV_FLAG_MOVING
, &bo
->priv_flags
);
1605 spin_unlock(&bo
->lock
);
1606 driver
->sync_obj_unref(&tmp_obj
);
1607 spin_lock(&bo
->lock
);
1614 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
1615 sync_obj_arg
= bo
->sync_obj_arg
;
1616 spin_unlock(&bo
->lock
);
1617 ret
= driver
->sync_obj_wait(sync_obj
, sync_obj_arg
,
1618 lazy
, interruptible
);
1619 if (unlikely(ret
!= 0)) {
1620 driver
->sync_obj_unref(&sync_obj
);
1621 spin_lock(&bo
->lock
);
1624 spin_lock(&bo
->lock
);
1625 if (likely(bo
->sync_obj
== sync_obj
&&
1626 bo
->sync_obj_arg
== sync_obj_arg
)) {
1627 void *tmp_obj
= bo
->sync_obj
;
1628 bo
->sync_obj
= NULL
;
1629 clear_bit(TTM_BO_PRIV_FLAG_MOVING
,
1631 spin_unlock(&bo
->lock
);
1632 driver
->sync_obj_unref(&sync_obj
);
1633 driver
->sync_obj_unref(&tmp_obj
);
1634 spin_lock(&bo
->lock
);
1639 EXPORT_SYMBOL(ttm_bo_wait
);
1641 void ttm_bo_unblock_reservation(struct ttm_buffer_object
*bo
)
1643 atomic_set(&bo
->reserved
, 0);
1644 wake_up_all(&bo
->event_queue
);
1647 int ttm_bo_block_reservation(struct ttm_buffer_object
*bo
, bool interruptible
,
1652 while (unlikely(atomic_cmpxchg(&bo
->reserved
, 0, 1) != 0)) {
1655 else if (interruptible
) {
1656 ret
= wait_event_interruptible
1657 (bo
->event_queue
, atomic_read(&bo
->reserved
) == 0);
1658 if (unlikely(ret
!= 0))
1661 wait_event(bo
->event_queue
,
1662 atomic_read(&bo
->reserved
) == 0);
1668 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1673 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1674 * makes sure the lru lists are updated.
1677 ret
= ttm_bo_reserve(bo
, true, no_wait
, false, 0);
1678 if (unlikely(ret
!= 0))
1680 spin_lock(&bo
->lock
);
1681 ret
= ttm_bo_wait(bo
, false, true, no_wait
);
1682 spin_unlock(&bo
->lock
);
1683 if (likely(ret
== 0))
1684 atomic_inc(&bo
->cpu_writers
);
1685 ttm_bo_unreserve(bo
);
1689 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1691 if (atomic_dec_and_test(&bo
->cpu_writers
))
1692 wake_up_all(&bo
->event_queue
);
1696 * A buffer object shrink method that tries to swap out the first
1697 * buffer object on the bo_global::swap_lru list.
1700 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
)
1702 struct ttm_bo_global
*glob
=
1703 container_of(shrink
, struct ttm_bo_global
, shrink
);
1704 struct ttm_buffer_object
*bo
;
1707 uint32_t swap_placement
= (TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_SYSTEM
);
1709 spin_lock(&glob
->lru_lock
);
1710 while (ret
== -EBUSY
) {
1711 if (unlikely(list_empty(&glob
->swap_lru
))) {
1712 spin_unlock(&glob
->lru_lock
);
1716 bo
= list_first_entry(&glob
->swap_lru
,
1717 struct ttm_buffer_object
, swap
);
1718 kref_get(&bo
->list_kref
);
1721 * Reserve buffer. Since we unlock while sleeping, we need
1722 * to re-check that nobody removed us from the swap-list while
1726 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
1727 if (unlikely(ret
== -EBUSY
)) {
1728 spin_unlock(&glob
->lru_lock
);
1729 ttm_bo_wait_unreserved(bo
, false);
1730 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1731 spin_lock(&glob
->lru_lock
);
1736 put_count
= ttm_bo_del_from_lru(bo
);
1737 spin_unlock(&glob
->lru_lock
);
1740 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
1743 * Wait for GPU, then move to system cached.
1746 spin_lock(&bo
->lock
);
1747 ret
= ttm_bo_wait(bo
, false, false, false);
1748 spin_unlock(&bo
->lock
);
1750 if (unlikely(ret
!= 0))
1753 if ((bo
->mem
.placement
& swap_placement
) != swap_placement
) {
1754 struct ttm_mem_reg evict_mem
;
1756 evict_mem
= bo
->mem
;
1757 evict_mem
.mm_node
= NULL
;
1758 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1759 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1761 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true,
1763 if (unlikely(ret
!= 0))
1767 ttm_bo_unmap_virtual(bo
);
1770 * Swap out. Buffer will be swapped in again as soon as
1771 * anyone tries to access a ttm page.
1774 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistant_swap_storage
);
1779 * Unreserve without putting on LRU to avoid swapping out an
1780 * already swapped buffer.
1783 atomic_set(&bo
->reserved
, 0);
1784 wake_up_all(&bo
->event_queue
);
1785 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1789 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1791 while (ttm_bo_swapout(&bdev
->glob
->shrink
) == 0)