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 int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
);
47 static void ttm_bo_global_kobj_release(struct kobject
*kobj
);
49 static struct attribute ttm_bo_count
= {
54 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
55 struct attribute
*attr
,
58 struct ttm_bo_global
*glob
=
59 container_of(kobj
, struct ttm_bo_global
, kobj
);
61 return snprintf(buffer
, PAGE_SIZE
, "%lu\n",
62 (unsigned long) atomic_read(&glob
->bo_count
));
65 static struct attribute
*ttm_bo_global_attrs
[] = {
70 static struct sysfs_ops ttm_bo_global_ops
= {
71 .show
= &ttm_bo_global_show
74 static struct kobj_type ttm_bo_glob_kobj_type
= {
75 .release
= &ttm_bo_global_kobj_release
,
76 .sysfs_ops
= &ttm_bo_global_ops
,
77 .default_attrs
= ttm_bo_global_attrs
81 static inline uint32_t ttm_bo_type_flags(unsigned type
)
86 static void ttm_bo_release_list(struct kref
*list_kref
)
88 struct ttm_buffer_object
*bo
=
89 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
90 struct ttm_bo_device
*bdev
= bo
->bdev
;
92 BUG_ON(atomic_read(&bo
->list_kref
.refcount
));
93 BUG_ON(atomic_read(&bo
->kref
.refcount
));
94 BUG_ON(atomic_read(&bo
->cpu_writers
));
95 BUG_ON(bo
->sync_obj
!= NULL
);
96 BUG_ON(bo
->mem
.mm_node
!= NULL
);
97 BUG_ON(!list_empty(&bo
->lru
));
98 BUG_ON(!list_empty(&bo
->ddestroy
));
101 ttm_tt_destroy(bo
->ttm
);
102 atomic_dec(&bo
->glob
->bo_count
);
106 ttm_mem_global_free(bdev
->glob
->mem_glob
, bo
->acc_size
);
111 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
, bool interruptible
)
117 ret
= wait_event_interruptible(bo
->event_queue
,
118 atomic_read(&bo
->reserved
) == 0);
119 if (unlikely(ret
!= 0))
122 wait_event(bo
->event_queue
, atomic_read(&bo
->reserved
) == 0);
127 static void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
129 struct ttm_bo_device
*bdev
= bo
->bdev
;
130 struct ttm_mem_type_manager
*man
;
132 BUG_ON(!atomic_read(&bo
->reserved
));
134 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
136 BUG_ON(!list_empty(&bo
->lru
));
138 man
= &bdev
->man
[bo
->mem
.mem_type
];
139 list_add_tail(&bo
->lru
, &man
->lru
);
140 kref_get(&bo
->list_kref
);
142 if (bo
->ttm
!= NULL
) {
143 list_add_tail(&bo
->swap
, &bo
->glob
->swap_lru
);
144 kref_get(&bo
->list_kref
);
150 * Call with the lru_lock held.
153 static int ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
157 if (!list_empty(&bo
->swap
)) {
158 list_del_init(&bo
->swap
);
161 if (!list_empty(&bo
->lru
)) {
162 list_del_init(&bo
->lru
);
167 * TODO: Add a driver hook to delete from
168 * driver-specific LRU's here.
174 int ttm_bo_reserve_locked(struct ttm_buffer_object
*bo
,
176 bool no_wait
, bool use_sequence
, uint32_t sequence
)
178 struct ttm_bo_global
*glob
= bo
->glob
;
181 while (unlikely(atomic_cmpxchg(&bo
->reserved
, 0, 1) != 0)) {
182 if (use_sequence
&& bo
->seq_valid
&&
183 (sequence
- bo
->val_seq
< (1 << 31))) {
190 spin_unlock(&glob
->lru_lock
);
191 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
192 spin_lock(&glob
->lru_lock
);
199 bo
->val_seq
= sequence
;
200 bo
->seq_valid
= true;
202 bo
->seq_valid
= false;
207 EXPORT_SYMBOL(ttm_bo_reserve
);
209 static void ttm_bo_ref_bug(struct kref
*list_kref
)
214 int ttm_bo_reserve(struct ttm_buffer_object
*bo
,
216 bool no_wait
, bool use_sequence
, uint32_t sequence
)
218 struct ttm_bo_global
*glob
= bo
->glob
;
222 spin_lock(&glob
->lru_lock
);
223 ret
= ttm_bo_reserve_locked(bo
, interruptible
, no_wait
, use_sequence
,
225 if (likely(ret
== 0))
226 put_count
= ttm_bo_del_from_lru(bo
);
227 spin_unlock(&glob
->lru_lock
);
230 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
235 void ttm_bo_unreserve(struct ttm_buffer_object
*bo
)
237 struct ttm_bo_global
*glob
= bo
->glob
;
239 spin_lock(&glob
->lru_lock
);
240 ttm_bo_add_to_lru(bo
);
241 atomic_set(&bo
->reserved
, 0);
242 wake_up_all(&bo
->event_queue
);
243 spin_unlock(&glob
->lru_lock
);
245 EXPORT_SYMBOL(ttm_bo_unreserve
);
248 * Call bo->mutex locked.
251 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
253 struct ttm_bo_device
*bdev
= bo
->bdev
;
254 struct ttm_bo_global
*glob
= bo
->glob
;
256 uint32_t page_flags
= 0;
258 TTM_ASSERT_LOCKED(&bo
->mutex
);
261 if (bdev
->need_dma32
)
262 page_flags
|= TTM_PAGE_FLAG_DMA32
;
265 case ttm_bo_type_device
:
267 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
268 case ttm_bo_type_kernel
:
269 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
270 page_flags
, glob
->dummy_read_page
);
271 if (unlikely(bo
->ttm
== NULL
))
274 case ttm_bo_type_user
:
275 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
276 page_flags
| TTM_PAGE_FLAG_USER
,
277 glob
->dummy_read_page
);
278 if (unlikely(bo
->ttm
== NULL
))
282 ret
= ttm_tt_set_user(bo
->ttm
, current
,
283 bo
->buffer_start
, bo
->num_pages
);
284 if (unlikely(ret
!= 0))
285 ttm_tt_destroy(bo
->ttm
);
288 printk(KERN_ERR TTM_PFX
"Illegal buffer object type\n");
296 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
297 struct ttm_mem_reg
*mem
,
298 bool evict
, bool interruptible
, bool no_wait
)
300 struct ttm_bo_device
*bdev
= bo
->bdev
;
301 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
302 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
303 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
304 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
307 if (old_is_pci
|| new_is_pci
||
308 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0))
309 ttm_bo_unmap_virtual(bo
);
312 * Create and bind a ttm if required.
315 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && (bo
->ttm
== NULL
)) {
316 ret
= ttm_bo_add_ttm(bo
, false);
320 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
324 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
325 ret
= ttm_tt_bind(bo
->ttm
, mem
);
330 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
332 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
333 uint32_t save_flags
= old_mem
->placement
;
337 ttm_flag_masked(&save_flags
, mem
->placement
,
338 TTM_PL_MASK_MEMTYPE
);
344 if (bdev
->driver
->move_notify
)
345 bdev
->driver
->move_notify(bo
, mem
);
347 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
348 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
349 ret
= ttm_bo_move_ttm(bo
, evict
, no_wait
, mem
);
350 else if (bdev
->driver
->move
)
351 ret
= bdev
->driver
->move(bo
, evict
, interruptible
,
354 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait
, mem
);
361 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
363 printk(KERN_ERR TTM_PFX
"Can not flush read caches\n");
367 if (bo
->mem
.mm_node
) {
368 spin_lock(&bo
->lock
);
369 bo
->offset
= (bo
->mem
.mm_node
->start
<< PAGE_SHIFT
) +
370 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
371 bo
->cur_placement
= bo
->mem
.placement
;
372 spin_unlock(&bo
->lock
);
378 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
379 if ((new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
) {
380 ttm_tt_unbind(bo
->ttm
);
381 ttm_tt_destroy(bo
->ttm
);
389 * If bo idle, remove from delayed- and lru lists, and unref.
390 * If not idle, and already on delayed list, do nothing.
391 * If not idle, and not on delayed list, put on delayed list,
392 * up the list_kref and schedule a delayed list check.
395 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
, bool remove_all
)
397 struct ttm_bo_device
*bdev
= bo
->bdev
;
398 struct ttm_bo_global
*glob
= bo
->glob
;
399 struct ttm_bo_driver
*driver
= bdev
->driver
;
402 spin_lock(&bo
->lock
);
403 (void) ttm_bo_wait(bo
, false, false, !remove_all
);
408 spin_unlock(&bo
->lock
);
410 spin_lock(&glob
->lru_lock
);
411 ret
= ttm_bo_reserve_locked(bo
, false, false, false, 0);
414 ttm_tt_unbind(bo
->ttm
);
416 if (!list_empty(&bo
->ddestroy
)) {
417 list_del_init(&bo
->ddestroy
);
418 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
420 if (bo
->mem
.mm_node
) {
421 drm_mm_put_block(bo
->mem
.mm_node
);
422 bo
->mem
.mm_node
= NULL
;
424 put_count
= ttm_bo_del_from_lru(bo
);
425 spin_unlock(&glob
->lru_lock
);
427 atomic_set(&bo
->reserved
, 0);
430 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
435 spin_lock(&glob
->lru_lock
);
436 if (list_empty(&bo
->ddestroy
)) {
437 void *sync_obj
= bo
->sync_obj
;
438 void *sync_obj_arg
= bo
->sync_obj_arg
;
440 kref_get(&bo
->list_kref
);
441 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
442 spin_unlock(&glob
->lru_lock
);
443 spin_unlock(&bo
->lock
);
446 driver
->sync_obj_flush(sync_obj
, sync_obj_arg
);
447 schedule_delayed_work(&bdev
->wq
,
448 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
452 spin_unlock(&glob
->lru_lock
);
453 spin_unlock(&bo
->lock
);
461 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
462 * encountered buffers.
465 static int ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
467 struct ttm_bo_global
*glob
= bdev
->glob
;
468 struct ttm_buffer_object
*entry
, *nentry
;
469 struct list_head
*list
, *next
;
472 spin_lock(&glob
->lru_lock
);
473 list_for_each_safe(list
, next
, &bdev
->ddestroy
) {
474 entry
= list_entry(list
, struct ttm_buffer_object
, ddestroy
);
478 * Protect the next list entry from destruction while we
479 * unlock the lru_lock.
482 if (next
!= &bdev
->ddestroy
) {
483 nentry
= list_entry(next
, struct ttm_buffer_object
,
485 kref_get(&nentry
->list_kref
);
487 kref_get(&entry
->list_kref
);
489 spin_unlock(&glob
->lru_lock
);
490 ret
= ttm_bo_cleanup_refs(entry
, remove_all
);
491 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
493 spin_lock(&glob
->lru_lock
);
495 bool next_onlist
= !list_empty(next
);
496 spin_unlock(&glob
->lru_lock
);
497 kref_put(&nentry
->list_kref
, ttm_bo_release_list
);
498 spin_lock(&glob
->lru_lock
);
500 * Someone might have raced us and removed the
501 * next entry from the list. We don't bother restarting
511 ret
= !list_empty(&bdev
->ddestroy
);
512 spin_unlock(&glob
->lru_lock
);
517 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
519 struct ttm_bo_device
*bdev
=
520 container_of(work
, struct ttm_bo_device
, wq
.work
);
522 if (ttm_bo_delayed_delete(bdev
, false)) {
523 schedule_delayed_work(&bdev
->wq
,
524 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
528 static void ttm_bo_release(struct kref
*kref
)
530 struct ttm_buffer_object
*bo
=
531 container_of(kref
, struct ttm_buffer_object
, kref
);
532 struct ttm_bo_device
*bdev
= bo
->bdev
;
534 if (likely(bo
->vm_node
!= NULL
)) {
535 rb_erase(&bo
->vm_rb
, &bdev
->addr_space_rb
);
536 drm_mm_put_block(bo
->vm_node
);
539 write_unlock(&bdev
->vm_lock
);
540 ttm_bo_cleanup_refs(bo
, false);
541 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
542 write_lock(&bdev
->vm_lock
);
545 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
547 struct ttm_buffer_object
*bo
= *p_bo
;
548 struct ttm_bo_device
*bdev
= bo
->bdev
;
551 write_lock(&bdev
->vm_lock
);
552 kref_put(&bo
->kref
, ttm_bo_release
);
553 write_unlock(&bdev
->vm_lock
);
555 EXPORT_SYMBOL(ttm_bo_unref
);
557 static int ttm_bo_evict(struct ttm_buffer_object
*bo
, unsigned mem_type
,
558 bool interruptible
, bool no_wait
)
561 struct ttm_bo_device
*bdev
= bo
->bdev
;
562 struct ttm_bo_global
*glob
= bo
->glob
;
563 struct ttm_mem_reg evict_mem
;
564 uint32_t proposed_placement
;
566 if (bo
->mem
.mem_type
!= mem_type
)
569 spin_lock(&bo
->lock
);
570 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait
);
571 spin_unlock(&bo
->lock
);
573 if (unlikely(ret
!= 0)) {
574 if (ret
!= -ERESTART
) {
575 printk(KERN_ERR TTM_PFX
576 "Failed to expire sync object before "
577 "buffer eviction.\n");
582 BUG_ON(!atomic_read(&bo
->reserved
));
585 evict_mem
.mm_node
= NULL
;
587 proposed_placement
= bdev
->driver
->evict_flags(bo
);
589 ret
= ttm_bo_mem_space(bo
, proposed_placement
,
590 &evict_mem
, interruptible
, no_wait
);
591 if (unlikely(ret
!= 0 && ret
!= -ERESTART
))
592 ret
= ttm_bo_mem_space(bo
, TTM_PL_FLAG_SYSTEM
,
593 &evict_mem
, interruptible
, no_wait
);
596 if (ret
!= -ERESTART
)
597 printk(KERN_ERR TTM_PFX
598 "Failed to find memory space for "
599 "buffer 0x%p eviction.\n", bo
);
603 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, interruptible
,
606 if (ret
!= -ERESTART
)
607 printk(KERN_ERR TTM_PFX
"Buffer eviction failed\n");
611 spin_lock(&glob
->lru_lock
);
612 if (evict_mem
.mm_node
) {
613 drm_mm_put_block(evict_mem
.mm_node
);
614 evict_mem
.mm_node
= NULL
;
616 spin_unlock(&glob
->lru_lock
);
623 * Repeatedly evict memory from the LRU for @mem_type until we create enough
624 * space, or we've evicted everything and there isn't enough space.
626 static int ttm_bo_mem_force_space(struct ttm_bo_device
*bdev
,
627 struct ttm_mem_reg
*mem
,
629 bool interruptible
, bool no_wait
)
631 struct ttm_bo_global
*glob
= bdev
->glob
;
632 struct drm_mm_node
*node
;
633 struct ttm_buffer_object
*entry
;
634 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
635 struct list_head
*lru
;
636 unsigned long num_pages
= mem
->num_pages
;
641 ret
= drm_mm_pre_get(&man
->manager
);
642 if (unlikely(ret
!= 0))
645 spin_lock(&glob
->lru_lock
);
647 node
= drm_mm_search_free(&man
->manager
, num_pages
,
648 mem
->page_alignment
, 1);
656 entry
= list_first_entry(lru
, struct ttm_buffer_object
, lru
);
657 kref_get(&entry
->list_kref
);
660 ttm_bo_reserve_locked(entry
, interruptible
, no_wait
,
663 if (likely(ret
== 0))
664 put_count
= ttm_bo_del_from_lru(entry
);
666 spin_unlock(&glob
->lru_lock
);
668 if (unlikely(ret
!= 0))
672 kref_put(&entry
->list_kref
, ttm_bo_ref_bug
);
674 ret
= ttm_bo_evict(entry
, mem_type
, interruptible
, no_wait
);
676 ttm_bo_unreserve(entry
);
678 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
682 spin_lock(&glob
->lru_lock
);
686 spin_unlock(&glob
->lru_lock
);
690 node
= drm_mm_get_block_atomic(node
, num_pages
, mem
->page_alignment
);
691 if (unlikely(!node
)) {
692 spin_unlock(&glob
->lru_lock
);
696 spin_unlock(&glob
->lru_lock
);
698 mem
->mem_type
= mem_type
;
702 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
703 uint32_t cur_placement
,
704 uint32_t proposed_placement
)
706 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
707 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
710 * Keep current caching if possible.
713 if ((cur_placement
& caching
) != 0)
714 result
|= (cur_placement
& caching
);
715 else if ((man
->default_caching
& caching
) != 0)
716 result
|= man
->default_caching
;
717 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
718 result
|= TTM_PL_FLAG_CACHED
;
719 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
720 result
|= TTM_PL_FLAG_WC
;
721 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
722 result
|= TTM_PL_FLAG_UNCACHED
;
728 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
731 uint32_t proposed_placement
,
732 uint32_t *masked_placement
)
734 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
736 if ((man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && disallow_fixed
)
739 if ((cur_flags
& proposed_placement
& TTM_PL_MASK_MEM
) == 0)
742 if ((proposed_placement
& man
->available_caching
) == 0)
745 cur_flags
|= (proposed_placement
& man
->available_caching
);
747 *masked_placement
= cur_flags
;
752 * Creates space for memory region @mem according to its type.
754 * This function first searches for free space in compatible memory types in
755 * the priority order defined by the driver. If free space isn't found, then
756 * ttm_bo_mem_force_space is attempted in priority order to evict and find
759 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
760 uint32_t proposed_placement
,
761 struct ttm_mem_reg
*mem
,
762 bool interruptible
, bool no_wait
)
764 struct ttm_bo_device
*bdev
= bo
->bdev
;
765 struct ttm_bo_global
*glob
= bo
->glob
;
766 struct ttm_mem_type_manager
*man
;
768 uint32_t num_prios
= bdev
->driver
->num_mem_type_prio
;
769 const uint32_t *prios
= bdev
->driver
->mem_type_prio
;
771 uint32_t mem_type
= TTM_PL_SYSTEM
;
772 uint32_t cur_flags
= 0;
773 bool type_found
= false;
774 bool type_ok
= false;
775 bool has_eagain
= false;
776 struct drm_mm_node
*node
= NULL
;
780 for (i
= 0; i
< num_prios
; ++i
) {
782 man
= &bdev
->man
[mem_type
];
784 type_ok
= ttm_bo_mt_compatible(man
,
785 bo
->type
== ttm_bo_type_user
,
786 mem_type
, proposed_placement
,
792 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
795 if (mem_type
== TTM_PL_SYSTEM
)
798 if (man
->has_type
&& man
->use_type
) {
801 ret
= drm_mm_pre_get(&man
->manager
);
805 spin_lock(&glob
->lru_lock
);
806 node
= drm_mm_search_free(&man
->manager
,
810 if (unlikely(!node
)) {
811 spin_unlock(&glob
->lru_lock
);
814 node
= drm_mm_get_block_atomic(node
,
818 spin_unlock(&glob
->lru_lock
);
825 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || node
) {
827 mem
->mem_type
= mem_type
;
828 mem
->placement
= cur_flags
;
835 num_prios
= bdev
->driver
->num_mem_busy_prio
;
836 prios
= bdev
->driver
->mem_busy_prio
;
838 for (i
= 0; i
< num_prios
; ++i
) {
840 man
= &bdev
->man
[mem_type
];
845 if (!ttm_bo_mt_compatible(man
,
846 bo
->type
== ttm_bo_type_user
,
848 proposed_placement
, &cur_flags
))
851 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
854 ret
= ttm_bo_mem_force_space(bdev
, mem
, mem_type
,
855 interruptible
, no_wait
);
857 if (ret
== 0 && mem
->mm_node
) {
858 mem
->placement
= cur_flags
;
862 if (ret
== -ERESTART
)
866 ret
= (has_eagain
) ? -ERESTART
: -ENOMEM
;
869 EXPORT_SYMBOL(ttm_bo_mem_space
);
871 int ttm_bo_wait_cpu(struct ttm_buffer_object
*bo
, bool no_wait
)
875 if ((atomic_read(&bo
->cpu_writers
) > 0) && no_wait
)
878 ret
= wait_event_interruptible(bo
->event_queue
,
879 atomic_read(&bo
->cpu_writers
) == 0);
881 if (ret
== -ERESTARTSYS
)
887 int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
888 uint32_t proposed_placement
,
889 bool interruptible
, bool no_wait
)
891 struct ttm_bo_global
*glob
= bo
->glob
;
893 struct ttm_mem_reg mem
;
895 BUG_ON(!atomic_read(&bo
->reserved
));
898 * FIXME: It's possible to pipeline buffer moves.
899 * Have the driver move function wait for idle when necessary,
900 * instead of doing it here.
903 spin_lock(&bo
->lock
);
904 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait
);
905 spin_unlock(&bo
->lock
);
910 mem
.num_pages
= bo
->num_pages
;
911 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
912 mem
.page_alignment
= bo
->mem
.page_alignment
;
915 * Determine where to move the buffer.
918 ret
= ttm_bo_mem_space(bo
, proposed_placement
, &mem
,
919 interruptible
, no_wait
);
923 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, interruptible
, no_wait
);
926 if (ret
&& mem
.mm_node
) {
927 spin_lock(&glob
->lru_lock
);
928 drm_mm_put_block(mem
.mm_node
);
929 spin_unlock(&glob
->lru_lock
);
934 static int ttm_bo_mem_compat(uint32_t proposed_placement
,
935 struct ttm_mem_reg
*mem
)
937 if ((proposed_placement
& mem
->placement
& TTM_PL_MASK_MEM
) == 0)
939 if ((proposed_placement
& mem
->placement
& TTM_PL_MASK_CACHING
) == 0)
945 int ttm_buffer_object_validate(struct ttm_buffer_object
*bo
,
946 uint32_t proposed_placement
,
947 bool interruptible
, bool no_wait
)
951 BUG_ON(!atomic_read(&bo
->reserved
));
952 bo
->proposed_placement
= proposed_placement
;
954 TTM_DEBUG("Proposed placement 0x%08lx, Old flags 0x%08lx\n",
955 (unsigned long)proposed_placement
,
956 (unsigned long)bo
->mem
.placement
);
959 * Check whether we need to move buffer.
962 if (!ttm_bo_mem_compat(bo
->proposed_placement
, &bo
->mem
)) {
963 ret
= ttm_bo_move_buffer(bo
, bo
->proposed_placement
,
964 interruptible
, no_wait
);
966 if (ret
!= -ERESTART
)
967 printk(KERN_ERR TTM_PFX
968 "Failed moving buffer. "
969 "Proposed placement 0x%08x\n",
970 bo
->proposed_placement
);
972 printk(KERN_ERR TTM_PFX
973 "Out of aperture space or "
974 "DRM memory quota.\n");
980 * We might need to add a TTM.
983 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
984 ret
= ttm_bo_add_ttm(bo
, true);
989 * Validation has succeeded, move the access and other
990 * non-mapping-related flag bits from the proposed flags to
994 ttm_flag_masked(&bo
->mem
.placement
, bo
->proposed_placement
,
995 ~TTM_PL_MASK_MEMTYPE
);
999 EXPORT_SYMBOL(ttm_buffer_object_validate
);
1002 ttm_bo_check_placement(struct ttm_buffer_object
*bo
,
1003 uint32_t set_flags
, uint32_t clr_flags
)
1005 uint32_t new_mask
= set_flags
| clr_flags
;
1007 if ((bo
->type
== ttm_bo_type_user
) &&
1008 (clr_flags
& TTM_PL_FLAG_CACHED
)) {
1009 printk(KERN_ERR TTM_PFX
1010 "User buffers require cache-coherent memory.\n");
1014 if (!capable(CAP_SYS_ADMIN
)) {
1015 if (new_mask
& TTM_PL_FLAG_NO_EVICT
) {
1016 printk(KERN_ERR TTM_PFX
"Need to be root to modify"
1017 " NO_EVICT status.\n");
1021 if ((clr_flags
& bo
->mem
.placement
& TTM_PL_MASK_MEMTYPE
) &&
1022 (bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
1023 printk(KERN_ERR TTM_PFX
1024 "Incompatible memory specification"
1025 " for NO_EVICT buffer.\n");
1032 int ttm_buffer_object_init(struct ttm_bo_device
*bdev
,
1033 struct ttm_buffer_object
*bo
,
1035 enum ttm_bo_type type
,
1037 uint32_t page_alignment
,
1038 unsigned long buffer_start
,
1040 struct file
*persistant_swap_storage
,
1042 void (*destroy
) (struct ttm_buffer_object
*))
1045 unsigned long num_pages
;
1047 size
+= buffer_start
& ~PAGE_MASK
;
1048 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1049 if (num_pages
== 0) {
1050 printk(KERN_ERR TTM_PFX
"Illegal buffer object size.\n");
1053 bo
->destroy
= destroy
;
1055 spin_lock_init(&bo
->lock
);
1056 kref_init(&bo
->kref
);
1057 kref_init(&bo
->list_kref
);
1058 atomic_set(&bo
->cpu_writers
, 0);
1059 atomic_set(&bo
->reserved
, 1);
1060 init_waitqueue_head(&bo
->event_queue
);
1061 INIT_LIST_HEAD(&bo
->lru
);
1062 INIT_LIST_HEAD(&bo
->ddestroy
);
1063 INIT_LIST_HEAD(&bo
->swap
);
1065 bo
->glob
= bdev
->glob
;
1067 bo
->num_pages
= num_pages
;
1068 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1069 bo
->mem
.num_pages
= bo
->num_pages
;
1070 bo
->mem
.mm_node
= NULL
;
1071 bo
->mem
.page_alignment
= page_alignment
;
1072 bo
->buffer_start
= buffer_start
& PAGE_MASK
;
1074 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1075 bo
->seq_valid
= false;
1076 bo
->persistant_swap_storage
= persistant_swap_storage
;
1077 bo
->acc_size
= acc_size
;
1078 atomic_inc(&bo
->glob
->bo_count
);
1080 ret
= ttm_bo_check_placement(bo
, flags
, 0ULL);
1081 if (unlikely(ret
!= 0))
1085 * If no caching attributes are set, accept any form of caching.
1088 if ((flags
& TTM_PL_MASK_CACHING
) == 0)
1089 flags
|= TTM_PL_MASK_CACHING
;
1092 * For ttm_bo_type_device buffers, allocate
1093 * address space from the device.
1096 if (bo
->type
== ttm_bo_type_device
) {
1097 ret
= ttm_bo_setup_vm(bo
);
1102 ret
= ttm_buffer_object_validate(bo
, flags
, interruptible
, false);
1106 ttm_bo_unreserve(bo
);
1110 ttm_bo_unreserve(bo
);
1115 EXPORT_SYMBOL(ttm_buffer_object_init
);
1117 static inline size_t ttm_bo_size(struct ttm_bo_global
*glob
,
1118 unsigned long num_pages
)
1120 size_t page_array_size
= (num_pages
* sizeof(void *) + PAGE_SIZE
- 1) &
1123 return glob
->ttm_bo_size
+ 2 * page_array_size
;
1126 int ttm_buffer_object_create(struct ttm_bo_device
*bdev
,
1128 enum ttm_bo_type type
,
1130 uint32_t page_alignment
,
1131 unsigned long buffer_start
,
1133 struct file
*persistant_swap_storage
,
1134 struct ttm_buffer_object
**p_bo
)
1136 struct ttm_buffer_object
*bo
;
1138 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1141 ttm_bo_size(bdev
->glob
, (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
);
1142 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1143 if (unlikely(ret
!= 0))
1146 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1148 if (unlikely(bo
== NULL
)) {
1149 ttm_mem_global_free(mem_glob
, acc_size
);
1153 ret
= ttm_buffer_object_init(bdev
, bo
, size
, type
, flags
,
1154 page_alignment
, buffer_start
,
1156 persistant_swap_storage
, acc_size
, NULL
);
1157 if (likely(ret
== 0))
1163 static int ttm_bo_leave_list(struct ttm_buffer_object
*bo
,
1164 uint32_t mem_type
, bool allow_errors
)
1168 spin_lock(&bo
->lock
);
1169 ret
= ttm_bo_wait(bo
, false, false, false);
1170 spin_unlock(&bo
->lock
);
1172 if (ret
&& allow_errors
)
1175 if (bo
->mem
.mem_type
== mem_type
)
1176 ret
= ttm_bo_evict(bo
, mem_type
, false, false);
1183 printk(KERN_ERR TTM_PFX
"Cleanup eviction failed\n");
1191 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1192 struct list_head
*head
,
1193 unsigned mem_type
, bool allow_errors
)
1195 struct ttm_bo_global
*glob
= bdev
->glob
;
1196 struct ttm_buffer_object
*entry
;
1201 * Can't use standard list traversal since we're unlocking.
1204 spin_lock(&glob
->lru_lock
);
1206 while (!list_empty(head
)) {
1207 entry
= list_first_entry(head
, struct ttm_buffer_object
, lru
);
1208 kref_get(&entry
->list_kref
);
1209 ret
= ttm_bo_reserve_locked(entry
, false, false, false, 0);
1210 put_count
= ttm_bo_del_from_lru(entry
);
1211 spin_unlock(&glob
->lru_lock
);
1213 kref_put(&entry
->list_kref
, ttm_bo_ref_bug
);
1215 ret
= ttm_bo_leave_list(entry
, mem_type
, allow_errors
);
1216 ttm_bo_unreserve(entry
);
1217 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
1218 spin_lock(&glob
->lru_lock
);
1221 spin_unlock(&glob
->lru_lock
);
1226 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1228 struct ttm_bo_global
*glob
= bdev
->glob
;
1229 struct ttm_mem_type_manager
*man
;
1232 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1233 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", mem_type
);
1236 man
= &bdev
->man
[mem_type
];
1238 if (!man
->has_type
) {
1239 printk(KERN_ERR TTM_PFX
"Trying to take down uninitialized "
1240 "memory manager type %u\n", mem_type
);
1244 man
->use_type
= false;
1245 man
->has_type
= false;
1249 ttm_bo_force_list_clean(bdev
, &man
->lru
, mem_type
, false);
1251 spin_lock(&glob
->lru_lock
);
1252 if (drm_mm_clean(&man
->manager
))
1253 drm_mm_takedown(&man
->manager
);
1257 spin_unlock(&glob
->lru_lock
);
1262 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1264 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1266 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1268 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1269 printk(KERN_ERR TTM_PFX
1270 "Illegal memory manager memory type %u.\n",
1275 if (!man
->has_type
) {
1276 printk(KERN_ERR TTM_PFX
1277 "Memory type %u has not been initialized.\n",
1282 return ttm_bo_force_list_clean(bdev
, &man
->lru
, mem_type
, true);
1284 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1286 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1287 unsigned long p_offset
, unsigned long p_size
)
1290 struct ttm_mem_type_manager
*man
;
1292 if (type
>= TTM_NUM_MEM_TYPES
) {
1293 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", type
);
1297 man
= &bdev
->man
[type
];
1298 if (man
->has_type
) {
1299 printk(KERN_ERR TTM_PFX
1300 "Memory manager already initialized for type %d\n",
1305 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1310 if (type
!= TTM_PL_SYSTEM
) {
1312 printk(KERN_ERR TTM_PFX
1313 "Zero size memory manager type %d\n",
1317 ret
= drm_mm_init(&man
->manager
, p_offset
, p_size
);
1321 man
->has_type
= true;
1322 man
->use_type
= true;
1325 INIT_LIST_HEAD(&man
->lru
);
1329 EXPORT_SYMBOL(ttm_bo_init_mm
);
1331 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1333 struct ttm_bo_global
*glob
=
1334 container_of(kobj
, struct ttm_bo_global
, kobj
);
1336 ttm_mem_unregister_shrink(glob
->mem_glob
, &glob
->shrink
);
1337 __free_page(glob
->dummy_read_page
);
1341 void ttm_bo_global_release(struct ttm_global_reference
*ref
)
1343 struct ttm_bo_global
*glob
= ref
->object
;
1345 kobject_del(&glob
->kobj
);
1346 kobject_put(&glob
->kobj
);
1348 EXPORT_SYMBOL(ttm_bo_global_release
);
1350 int ttm_bo_global_init(struct ttm_global_reference
*ref
)
1352 struct ttm_bo_global_ref
*bo_ref
=
1353 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1354 struct ttm_bo_global
*glob
= ref
->object
;
1357 mutex_init(&glob
->device_list_mutex
);
1358 spin_lock_init(&glob
->lru_lock
);
1359 glob
->mem_glob
= bo_ref
->mem_glob
;
1360 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1362 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1367 INIT_LIST_HEAD(&glob
->swap_lru
);
1368 INIT_LIST_HEAD(&glob
->device_list
);
1370 ttm_mem_init_shrink(&glob
->shrink
, ttm_bo_swapout
);
1371 ret
= ttm_mem_register_shrink(glob
->mem_glob
, &glob
->shrink
);
1372 if (unlikely(ret
!= 0)) {
1373 printk(KERN_ERR TTM_PFX
1374 "Could not register buffer object swapout.\n");
1378 glob
->ttm_bo_extra_size
=
1379 ttm_round_pot(sizeof(struct ttm_tt
)) +
1380 ttm_round_pot(sizeof(struct ttm_backend
));
1382 glob
->ttm_bo_size
= glob
->ttm_bo_extra_size
+
1383 ttm_round_pot(sizeof(struct ttm_buffer_object
));
1385 atomic_set(&glob
->bo_count
, 0);
1387 kobject_init(&glob
->kobj
, &ttm_bo_glob_kobj_type
);
1388 ret
= kobject_add(&glob
->kobj
, ttm_get_kobj(), "buffer_objects");
1389 if (unlikely(ret
!= 0))
1390 kobject_put(&glob
->kobj
);
1393 __free_page(glob
->dummy_read_page
);
1398 EXPORT_SYMBOL(ttm_bo_global_init
);
1401 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1404 unsigned i
= TTM_NUM_MEM_TYPES
;
1405 struct ttm_mem_type_manager
*man
;
1406 struct ttm_bo_global
*glob
= bdev
->glob
;
1409 man
= &bdev
->man
[i
];
1410 if (man
->has_type
) {
1411 man
->use_type
= false;
1412 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1414 printk(KERN_ERR TTM_PFX
1415 "DRM memory manager type %d "
1416 "is not clean.\n", i
);
1418 man
->has_type
= false;
1422 mutex_lock(&glob
->device_list_mutex
);
1423 list_del(&bdev
->device_list
);
1424 mutex_unlock(&glob
->device_list_mutex
);
1426 if (!cancel_delayed_work(&bdev
->wq
))
1427 flush_scheduled_work();
1429 while (ttm_bo_delayed_delete(bdev
, true))
1432 spin_lock(&glob
->lru_lock
);
1433 if (list_empty(&bdev
->ddestroy
))
1434 TTM_DEBUG("Delayed destroy list was clean\n");
1436 if (list_empty(&bdev
->man
[0].lru
))
1437 TTM_DEBUG("Swap list was clean\n");
1438 spin_unlock(&glob
->lru_lock
);
1440 BUG_ON(!drm_mm_clean(&bdev
->addr_space_mm
));
1441 write_lock(&bdev
->vm_lock
);
1442 drm_mm_takedown(&bdev
->addr_space_mm
);
1443 write_unlock(&bdev
->vm_lock
);
1447 EXPORT_SYMBOL(ttm_bo_device_release
);
1449 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1450 struct ttm_bo_global
*glob
,
1451 struct ttm_bo_driver
*driver
,
1452 uint64_t file_page_offset
,
1457 rwlock_init(&bdev
->vm_lock
);
1458 bdev
->driver
= driver
;
1460 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1463 * Initialize the system memory buffer type.
1464 * Other types need to be driver / IOCTL initialized.
1466 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0, 0);
1467 if (unlikely(ret
!= 0))
1470 bdev
->addr_space_rb
= RB_ROOT
;
1471 ret
= drm_mm_init(&bdev
->addr_space_mm
, file_page_offset
, 0x10000000);
1472 if (unlikely(ret
!= 0))
1473 goto out_no_addr_mm
;
1475 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1476 bdev
->nice_mode
= true;
1477 INIT_LIST_HEAD(&bdev
->ddestroy
);
1478 bdev
->dev_mapping
= NULL
;
1480 bdev
->need_dma32
= need_dma32
;
1482 mutex_lock(&glob
->device_list_mutex
);
1483 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1484 mutex_unlock(&glob
->device_list_mutex
);
1488 ttm_bo_clean_mm(bdev
, 0);
1492 EXPORT_SYMBOL(ttm_bo_device_init
);
1495 * buffer object vm functions.
1498 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1500 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1502 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1503 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1506 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1509 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1515 int ttm_bo_pci_offset(struct ttm_bo_device
*bdev
,
1516 struct ttm_mem_reg
*mem
,
1517 unsigned long *bus_base
,
1518 unsigned long *bus_offset
, unsigned long *bus_size
)
1520 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1523 if (!(man
->flags
& TTM_MEMTYPE_FLAG_MAPPABLE
))
1526 if (ttm_mem_reg_is_pci(bdev
, mem
)) {
1527 *bus_offset
= mem
->mm_node
->start
<< PAGE_SHIFT
;
1528 *bus_size
= mem
->num_pages
<< PAGE_SHIFT
;
1529 *bus_base
= man
->io_offset
;
1535 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1537 struct ttm_bo_device
*bdev
= bo
->bdev
;
1538 loff_t offset
= (loff_t
) bo
->addr_space_offset
;
1539 loff_t holelen
= ((loff_t
) bo
->mem
.num_pages
) << PAGE_SHIFT
;
1541 if (!bdev
->dev_mapping
)
1544 unmap_mapping_range(bdev
->dev_mapping
, offset
, holelen
, 1);
1546 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1548 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object
*bo
)
1550 struct ttm_bo_device
*bdev
= bo
->bdev
;
1551 struct rb_node
**cur
= &bdev
->addr_space_rb
.rb_node
;
1552 struct rb_node
*parent
= NULL
;
1553 struct ttm_buffer_object
*cur_bo
;
1554 unsigned long offset
= bo
->vm_node
->start
;
1555 unsigned long cur_offset
;
1559 cur_bo
= rb_entry(parent
, struct ttm_buffer_object
, vm_rb
);
1560 cur_offset
= cur_bo
->vm_node
->start
;
1561 if (offset
< cur_offset
)
1562 cur
= &parent
->rb_left
;
1563 else if (offset
> cur_offset
)
1564 cur
= &parent
->rb_right
;
1569 rb_link_node(&bo
->vm_rb
, parent
, cur
);
1570 rb_insert_color(&bo
->vm_rb
, &bdev
->addr_space_rb
);
1576 * @bo: the buffer to allocate address space for
1578 * Allocate address space in the drm device so that applications
1579 * can mmap the buffer and access the contents. This only
1580 * applies to ttm_bo_type_device objects as others are not
1581 * placed in the drm device address space.
1584 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
)
1586 struct ttm_bo_device
*bdev
= bo
->bdev
;
1590 ret
= drm_mm_pre_get(&bdev
->addr_space_mm
);
1591 if (unlikely(ret
!= 0))
1594 write_lock(&bdev
->vm_lock
);
1595 bo
->vm_node
= drm_mm_search_free(&bdev
->addr_space_mm
,
1596 bo
->mem
.num_pages
, 0, 0);
1598 if (unlikely(bo
->vm_node
== NULL
)) {
1603 bo
->vm_node
= drm_mm_get_block_atomic(bo
->vm_node
,
1604 bo
->mem
.num_pages
, 0);
1606 if (unlikely(bo
->vm_node
== NULL
)) {
1607 write_unlock(&bdev
->vm_lock
);
1611 ttm_bo_vm_insert_rb(bo
);
1612 write_unlock(&bdev
->vm_lock
);
1613 bo
->addr_space_offset
= ((uint64_t) bo
->vm_node
->start
) << PAGE_SHIFT
;
1617 write_unlock(&bdev
->vm_lock
);
1621 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1622 bool lazy
, bool interruptible
, bool no_wait
)
1624 struct ttm_bo_driver
*driver
= bo
->bdev
->driver
;
1629 if (likely(bo
->sync_obj
== NULL
))
1632 while (bo
->sync_obj
) {
1634 if (driver
->sync_obj_signaled(bo
->sync_obj
, bo
->sync_obj_arg
)) {
1635 void *tmp_obj
= bo
->sync_obj
;
1636 bo
->sync_obj
= NULL
;
1637 clear_bit(TTM_BO_PRIV_FLAG_MOVING
, &bo
->priv_flags
);
1638 spin_unlock(&bo
->lock
);
1639 driver
->sync_obj_unref(&tmp_obj
);
1640 spin_lock(&bo
->lock
);
1647 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
1648 sync_obj_arg
= bo
->sync_obj_arg
;
1649 spin_unlock(&bo
->lock
);
1650 ret
= driver
->sync_obj_wait(sync_obj
, sync_obj_arg
,
1651 lazy
, interruptible
);
1652 if (unlikely(ret
!= 0)) {
1653 driver
->sync_obj_unref(&sync_obj
);
1654 spin_lock(&bo
->lock
);
1657 spin_lock(&bo
->lock
);
1658 if (likely(bo
->sync_obj
== sync_obj
&&
1659 bo
->sync_obj_arg
== sync_obj_arg
)) {
1660 void *tmp_obj
= bo
->sync_obj
;
1661 bo
->sync_obj
= NULL
;
1662 clear_bit(TTM_BO_PRIV_FLAG_MOVING
,
1664 spin_unlock(&bo
->lock
);
1665 driver
->sync_obj_unref(&sync_obj
);
1666 driver
->sync_obj_unref(&tmp_obj
);
1667 spin_lock(&bo
->lock
);
1669 spin_unlock(&bo
->lock
);
1670 driver
->sync_obj_unref(&sync_obj
);
1671 spin_lock(&bo
->lock
);
1676 EXPORT_SYMBOL(ttm_bo_wait
);
1678 void ttm_bo_unblock_reservation(struct ttm_buffer_object
*bo
)
1680 atomic_set(&bo
->reserved
, 0);
1681 wake_up_all(&bo
->event_queue
);
1684 int ttm_bo_block_reservation(struct ttm_buffer_object
*bo
, bool interruptible
,
1689 while (unlikely(atomic_cmpxchg(&bo
->reserved
, 0, 1) != 0)) {
1692 else if (interruptible
) {
1693 ret
= wait_event_interruptible
1694 (bo
->event_queue
, atomic_read(&bo
->reserved
) == 0);
1695 if (unlikely(ret
!= 0))
1698 wait_event(bo
->event_queue
,
1699 atomic_read(&bo
->reserved
) == 0);
1705 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1710 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1711 * makes sure the lru lists are updated.
1714 ret
= ttm_bo_reserve(bo
, true, no_wait
, false, 0);
1715 if (unlikely(ret
!= 0))
1717 spin_lock(&bo
->lock
);
1718 ret
= ttm_bo_wait(bo
, false, true, no_wait
);
1719 spin_unlock(&bo
->lock
);
1720 if (likely(ret
== 0))
1721 atomic_inc(&bo
->cpu_writers
);
1722 ttm_bo_unreserve(bo
);
1726 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1728 if (atomic_dec_and_test(&bo
->cpu_writers
))
1729 wake_up_all(&bo
->event_queue
);
1733 * A buffer object shrink method that tries to swap out the first
1734 * buffer object on the bo_global::swap_lru list.
1737 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
)
1739 struct ttm_bo_global
*glob
=
1740 container_of(shrink
, struct ttm_bo_global
, shrink
);
1741 struct ttm_buffer_object
*bo
;
1744 uint32_t swap_placement
= (TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_SYSTEM
);
1746 spin_lock(&glob
->lru_lock
);
1747 while (ret
== -EBUSY
) {
1748 if (unlikely(list_empty(&glob
->swap_lru
))) {
1749 spin_unlock(&glob
->lru_lock
);
1753 bo
= list_first_entry(&glob
->swap_lru
,
1754 struct ttm_buffer_object
, swap
);
1755 kref_get(&bo
->list_kref
);
1758 * Reserve buffer. Since we unlock while sleeping, we need
1759 * to re-check that nobody removed us from the swap-list while
1763 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
1764 if (unlikely(ret
== -EBUSY
)) {
1765 spin_unlock(&glob
->lru_lock
);
1766 ttm_bo_wait_unreserved(bo
, false);
1767 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1768 spin_lock(&glob
->lru_lock
);
1773 put_count
= ttm_bo_del_from_lru(bo
);
1774 spin_unlock(&glob
->lru_lock
);
1777 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
1780 * Wait for GPU, then move to system cached.
1783 spin_lock(&bo
->lock
);
1784 ret
= ttm_bo_wait(bo
, false, false, false);
1785 spin_unlock(&bo
->lock
);
1787 if (unlikely(ret
!= 0))
1790 if ((bo
->mem
.placement
& swap_placement
) != swap_placement
) {
1791 struct ttm_mem_reg evict_mem
;
1793 evict_mem
= bo
->mem
;
1794 evict_mem
.mm_node
= NULL
;
1795 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1796 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1798 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true,
1800 if (unlikely(ret
!= 0))
1804 ttm_bo_unmap_virtual(bo
);
1807 * Swap out. Buffer will be swapped in again as soon as
1808 * anyone tries to access a ttm page.
1811 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistant_swap_storage
);
1816 * Unreserve without putting on LRU to avoid swapping out an
1817 * already swapped buffer.
1820 atomic_set(&bo
->reserved
, 0);
1821 wake_up_all(&bo
->event_queue
);
1822 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1826 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1828 while (ttm_bo_swapout(&bdev
->glob
->shrink
) == 0)