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 <asm/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
;
141 BUG_ON(atomic_read(&bo
->list_kref
.refcount
));
142 BUG_ON(atomic_read(&bo
->kref
.refcount
));
143 BUG_ON(atomic_read(&bo
->cpu_writers
));
144 BUG_ON(bo
->sync_obj
!= NULL
);
145 BUG_ON(bo
->mem
.mm_node
!= NULL
);
146 BUG_ON(!list_empty(&bo
->lru
));
147 BUG_ON(!list_empty(&bo
->ddestroy
));
150 ttm_tt_destroy(bo
->ttm
);
151 atomic_dec(&bo
->glob
->bo_count
);
155 ttm_mem_global_free(bdev
->glob
->mem_glob
, bo
->acc_size
);
160 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
, bool interruptible
)
163 return wait_event_interruptible(bo
->event_queue
,
164 atomic_read(&bo
->reserved
) == 0);
166 wait_event(bo
->event_queue
, atomic_read(&bo
->reserved
) == 0);
170 EXPORT_SYMBOL(ttm_bo_wait_unreserved
);
172 void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
174 struct ttm_bo_device
*bdev
= bo
->bdev
;
175 struct ttm_mem_type_manager
*man
;
177 BUG_ON(!atomic_read(&bo
->reserved
));
179 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
181 BUG_ON(!list_empty(&bo
->lru
));
183 man
= &bdev
->man
[bo
->mem
.mem_type
];
184 list_add_tail(&bo
->lru
, &man
->lru
);
185 kref_get(&bo
->list_kref
);
187 if (bo
->ttm
!= NULL
) {
188 list_add_tail(&bo
->swap
, &bo
->glob
->swap_lru
);
189 kref_get(&bo
->list_kref
);
194 int ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
198 if (!list_empty(&bo
->swap
)) {
199 list_del_init(&bo
->swap
);
202 if (!list_empty(&bo
->lru
)) {
203 list_del_init(&bo
->lru
);
208 * TODO: Add a driver hook to delete from
209 * driver-specific LRU's here.
215 int ttm_bo_reserve_locked(struct ttm_buffer_object
*bo
,
217 bool no_wait
, bool use_sequence
, uint32_t sequence
)
219 struct ttm_bo_global
*glob
= bo
->glob
;
222 while (unlikely(atomic_cmpxchg(&bo
->reserved
, 0, 1) != 0)) {
224 * Deadlock avoidance for multi-bo reserving.
226 if (use_sequence
&& bo
->seq_valid
&&
227 (sequence
- bo
->val_seq
< (1 << 31))) {
234 spin_unlock(&glob
->lru_lock
);
235 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
236 spin_lock(&glob
->lru_lock
);
244 * Wake up waiters that may need to recheck for deadlock,
245 * if we decreased the sequence number.
247 if (unlikely((bo
->val_seq
- sequence
< (1 << 31))
249 wake_up_all(&bo
->event_queue
);
251 bo
->val_seq
= sequence
;
252 bo
->seq_valid
= true;
254 bo
->seq_valid
= false;
259 EXPORT_SYMBOL(ttm_bo_reserve
);
261 static void ttm_bo_ref_bug(struct kref
*list_kref
)
266 void ttm_bo_list_ref_sub(struct ttm_buffer_object
*bo
, int count
,
270 kref_put(&bo
->list_kref
,
271 (never_free
|| (count
>= 0)) ? ttm_bo_ref_bug
:
272 ttm_bo_release_list
);
275 int ttm_bo_reserve(struct ttm_buffer_object
*bo
,
277 bool no_wait
, bool use_sequence
, uint32_t sequence
)
279 struct ttm_bo_global
*glob
= bo
->glob
;
283 spin_lock(&glob
->lru_lock
);
284 ret
= ttm_bo_reserve_locked(bo
, interruptible
, no_wait
, use_sequence
,
286 if (likely(ret
== 0))
287 put_count
= ttm_bo_del_from_lru(bo
);
288 spin_unlock(&glob
->lru_lock
);
290 ttm_bo_list_ref_sub(bo
, put_count
, true);
295 void ttm_bo_unreserve(struct ttm_buffer_object
*bo
)
297 struct ttm_bo_global
*glob
= bo
->glob
;
299 spin_lock(&glob
->lru_lock
);
300 ttm_bo_add_to_lru(bo
);
301 atomic_set(&bo
->reserved
, 0);
302 wake_up_all(&bo
->event_queue
);
303 spin_unlock(&glob
->lru_lock
);
305 EXPORT_SYMBOL(ttm_bo_unreserve
);
308 * Call bo->mutex locked.
310 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
312 struct ttm_bo_device
*bdev
= bo
->bdev
;
313 struct ttm_bo_global
*glob
= bo
->glob
;
315 uint32_t page_flags
= 0;
317 TTM_ASSERT_LOCKED(&bo
->mutex
);
320 if (bdev
->need_dma32
)
321 page_flags
|= TTM_PAGE_FLAG_DMA32
;
324 case ttm_bo_type_device
:
326 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
327 case ttm_bo_type_kernel
:
328 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
329 page_flags
, glob
->dummy_read_page
);
330 if (unlikely(bo
->ttm
== NULL
))
333 case ttm_bo_type_user
:
334 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
335 page_flags
| TTM_PAGE_FLAG_USER
,
336 glob
->dummy_read_page
);
337 if (unlikely(bo
->ttm
== NULL
)) {
342 ret
= ttm_tt_set_user(bo
->ttm
, current
,
343 bo
->buffer_start
, bo
->num_pages
);
344 if (unlikely(ret
!= 0))
345 ttm_tt_destroy(bo
->ttm
);
348 printk(KERN_ERR TTM_PFX
"Illegal buffer object type\n");
356 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
357 struct ttm_mem_reg
*mem
,
358 bool evict
, bool interruptible
,
359 bool no_wait_reserve
, bool no_wait_gpu
)
361 struct ttm_bo_device
*bdev
= bo
->bdev
;
362 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
363 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
364 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
365 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
368 if (old_is_pci
|| new_is_pci
||
369 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0))
370 ttm_bo_unmap_virtual(bo
);
373 * Create and bind a ttm if required.
376 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && (bo
->ttm
== NULL
)) {
377 ret
= ttm_bo_add_ttm(bo
, false);
381 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
385 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
386 ret
= ttm_tt_bind(bo
->ttm
, mem
);
391 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
399 if (bdev
->driver
->move_notify
)
400 bdev
->driver
->move_notify(bo
, mem
);
402 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
403 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
404 ret
= ttm_bo_move_ttm(bo
, evict
, no_wait_reserve
, no_wait_gpu
, mem
);
405 else if (bdev
->driver
->move
)
406 ret
= bdev
->driver
->move(bo
, evict
, interruptible
,
407 no_wait_reserve
, no_wait_gpu
, mem
);
409 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait_reserve
, no_wait_gpu
, mem
);
416 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
418 printk(KERN_ERR TTM_PFX
"Can not flush read caches\n");
422 if (bo
->mem
.mm_node
) {
423 spin_lock(&bo
->lock
);
424 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
425 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
426 bo
->cur_placement
= bo
->mem
.placement
;
427 spin_unlock(&bo
->lock
);
434 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
435 if ((new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
) {
436 ttm_tt_unbind(bo
->ttm
);
437 ttm_tt_destroy(bo
->ttm
);
446 * Will release GPU memory type usage on destruction.
447 * This is the place to put in driver specific hooks to release
448 * driver private resources.
449 * Will release the bo::reserved lock.
452 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
455 ttm_tt_unbind(bo
->ttm
);
456 ttm_tt_destroy(bo
->ttm
);
460 ttm_bo_mem_put(bo
, &bo
->mem
);
462 atomic_set(&bo
->reserved
, 0);
465 * Make processes trying to reserve really pick it up.
467 smp_mb__after_atomic_dec();
468 wake_up_all(&bo
->event_queue
);
471 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
473 struct ttm_bo_device
*bdev
= bo
->bdev
;
474 struct ttm_bo_global
*glob
= bo
->glob
;
475 struct ttm_bo_driver
*driver
;
476 void *sync_obj
= NULL
;
481 spin_lock(&bo
->lock
);
482 (void) ttm_bo_wait(bo
, false, false, true);
485 spin_lock(&glob
->lru_lock
);
488 * Lock inversion between bo::reserve and bo::lock here,
489 * but that's OK, since we're only trylocking.
492 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
494 if (unlikely(ret
== -EBUSY
))
497 spin_unlock(&bo
->lock
);
498 put_count
= ttm_bo_del_from_lru(bo
);
500 spin_unlock(&glob
->lru_lock
);
501 ttm_bo_cleanup_memtype_use(bo
);
503 ttm_bo_list_ref_sub(bo
, put_count
, true);
507 spin_lock(&glob
->lru_lock
);
510 driver
= bdev
->driver
;
512 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
513 sync_obj_arg
= bo
->sync_obj_arg
;
515 kref_get(&bo
->list_kref
);
516 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
517 spin_unlock(&glob
->lru_lock
);
518 spin_unlock(&bo
->lock
);
521 driver
->sync_obj_flush(sync_obj
, sync_obj_arg
);
522 driver
->sync_obj_unref(&sync_obj
);
524 schedule_delayed_work(&bdev
->wq
,
525 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
529 * function ttm_bo_cleanup_refs
530 * If bo idle, remove from delayed- and lru lists, and unref.
531 * If not idle, do nothing.
533 * @interruptible Any sleeps should occur interruptibly.
534 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
535 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
538 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
540 bool no_wait_reserve
,
543 struct ttm_bo_global
*glob
= bo
->glob
;
548 spin_lock(&bo
->lock
);
549 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
550 spin_unlock(&bo
->lock
);
552 if (unlikely(ret
!= 0))
555 spin_lock(&glob
->lru_lock
);
556 ret
= ttm_bo_reserve_locked(bo
, interruptible
,
557 no_wait_reserve
, false, 0);
559 if (unlikely(ret
!= 0) || list_empty(&bo
->ddestroy
)) {
560 spin_unlock(&glob
->lru_lock
);
565 * We can re-check for sync object without taking
566 * the bo::lock since setting the sync object requires
567 * also bo::reserved. A busy object at this point may
568 * be caused by another thread recently starting an accelerated
572 if (unlikely(bo
->sync_obj
)) {
573 atomic_set(&bo
->reserved
, 0);
574 wake_up_all(&bo
->event_queue
);
575 spin_unlock(&glob
->lru_lock
);
579 put_count
= ttm_bo_del_from_lru(bo
);
580 list_del_init(&bo
->ddestroy
);
583 spin_unlock(&glob
->lru_lock
);
584 ttm_bo_cleanup_memtype_use(bo
);
586 ttm_bo_list_ref_sub(bo
, put_count
, true);
592 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
593 * encountered buffers.
596 static int ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
598 struct ttm_bo_global
*glob
= bdev
->glob
;
599 struct ttm_buffer_object
*entry
= NULL
;
602 spin_lock(&glob
->lru_lock
);
603 if (list_empty(&bdev
->ddestroy
))
606 entry
= list_first_entry(&bdev
->ddestroy
,
607 struct ttm_buffer_object
, ddestroy
);
608 kref_get(&entry
->list_kref
);
611 struct ttm_buffer_object
*nentry
= NULL
;
613 if (entry
->ddestroy
.next
!= &bdev
->ddestroy
) {
614 nentry
= list_first_entry(&entry
->ddestroy
,
615 struct ttm_buffer_object
, ddestroy
);
616 kref_get(&nentry
->list_kref
);
619 spin_unlock(&glob
->lru_lock
);
620 ret
= ttm_bo_cleanup_refs(entry
, false, !remove_all
,
622 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
628 spin_lock(&glob
->lru_lock
);
629 if (list_empty(&entry
->ddestroy
))
634 spin_unlock(&glob
->lru_lock
);
637 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
641 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
643 struct ttm_bo_device
*bdev
=
644 container_of(work
, struct ttm_bo_device
, wq
.work
);
646 if (ttm_bo_delayed_delete(bdev
, false)) {
647 schedule_delayed_work(&bdev
->wq
,
648 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
652 static void ttm_bo_release(struct kref
*kref
)
654 struct ttm_buffer_object
*bo
=
655 container_of(kref
, struct ttm_buffer_object
, kref
);
656 struct ttm_bo_device
*bdev
= bo
->bdev
;
658 if (likely(bo
->vm_node
!= NULL
)) {
659 rb_erase(&bo
->vm_rb
, &bdev
->addr_space_rb
);
660 drm_mm_put_block(bo
->vm_node
);
663 write_unlock(&bdev
->vm_lock
);
664 ttm_bo_cleanup_refs_or_queue(bo
);
665 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
666 write_lock(&bdev
->vm_lock
);
669 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
671 struct ttm_buffer_object
*bo
= *p_bo
;
672 struct ttm_bo_device
*bdev
= bo
->bdev
;
675 write_lock(&bdev
->vm_lock
);
676 kref_put(&bo
->kref
, ttm_bo_release
);
677 write_unlock(&bdev
->vm_lock
);
679 EXPORT_SYMBOL(ttm_bo_unref
);
681 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
683 return cancel_delayed_work_sync(&bdev
->wq
);
685 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
687 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
690 schedule_delayed_work(&bdev
->wq
,
691 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
693 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
695 static int ttm_bo_evict(struct ttm_buffer_object
*bo
, bool interruptible
,
696 bool no_wait_reserve
, bool no_wait_gpu
)
698 struct ttm_bo_device
*bdev
= bo
->bdev
;
699 struct ttm_mem_reg evict_mem
;
700 struct ttm_placement placement
;
703 spin_lock(&bo
->lock
);
704 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
705 spin_unlock(&bo
->lock
);
707 if (unlikely(ret
!= 0)) {
708 if (ret
!= -ERESTARTSYS
) {
709 printk(KERN_ERR TTM_PFX
710 "Failed to expire sync object before "
711 "buffer eviction.\n");
716 BUG_ON(!atomic_read(&bo
->reserved
));
719 evict_mem
.mm_node
= NULL
;
720 evict_mem
.bus
.io_reserved
= false;
724 placement
.num_placement
= 0;
725 placement
.num_busy_placement
= 0;
726 bdev
->driver
->evict_flags(bo
, &placement
);
727 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, interruptible
,
728 no_wait_reserve
, no_wait_gpu
);
730 if (ret
!= -ERESTARTSYS
) {
731 printk(KERN_ERR TTM_PFX
732 "Failed to find memory space for "
733 "buffer 0x%p eviction.\n", bo
);
734 ttm_bo_mem_space_debug(bo
, &placement
);
739 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, interruptible
,
740 no_wait_reserve
, no_wait_gpu
);
742 if (ret
!= -ERESTARTSYS
)
743 printk(KERN_ERR TTM_PFX
"Buffer eviction failed\n");
744 ttm_bo_mem_put(bo
, &evict_mem
);
752 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
754 bool interruptible
, bool no_wait_reserve
,
757 struct ttm_bo_global
*glob
= bdev
->glob
;
758 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
759 struct ttm_buffer_object
*bo
;
760 int ret
, put_count
= 0;
763 spin_lock(&glob
->lru_lock
);
764 if (list_empty(&man
->lru
)) {
765 spin_unlock(&glob
->lru_lock
);
769 bo
= list_first_entry(&man
->lru
, struct ttm_buffer_object
, lru
);
770 kref_get(&bo
->list_kref
);
772 if (!list_empty(&bo
->ddestroy
)) {
773 spin_unlock(&glob
->lru_lock
);
774 ret
= ttm_bo_cleanup_refs(bo
, interruptible
,
775 no_wait_reserve
, no_wait_gpu
);
776 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
778 if (likely(ret
== 0 || ret
== -ERESTARTSYS
))
784 ret
= ttm_bo_reserve_locked(bo
, false, no_wait_reserve
, false, 0);
786 if (unlikely(ret
== -EBUSY
)) {
787 spin_unlock(&glob
->lru_lock
);
788 if (likely(!no_wait_gpu
))
789 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
791 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
794 * We *need* to retry after releasing the lru lock.
797 if (unlikely(ret
!= 0))
802 put_count
= ttm_bo_del_from_lru(bo
);
803 spin_unlock(&glob
->lru_lock
);
807 ttm_bo_list_ref_sub(bo
, put_count
, true);
809 ret
= ttm_bo_evict(bo
, interruptible
, no_wait_reserve
, no_wait_gpu
);
810 ttm_bo_unreserve(bo
);
812 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
816 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
818 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
821 (*man
->func
->put_node
)(man
, mem
);
823 EXPORT_SYMBOL(ttm_bo_mem_put
);
826 * Repeatedly evict memory from the LRU for @mem_type until we create enough
827 * space, or we've evicted everything and there isn't enough space.
829 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
831 struct ttm_placement
*placement
,
832 struct ttm_mem_reg
*mem
,
834 bool no_wait_reserve
,
837 struct ttm_bo_device
*bdev
= bo
->bdev
;
838 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
842 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
843 if (unlikely(ret
!= 0))
847 ret
= ttm_mem_evict_first(bdev
, mem_type
, interruptible
,
848 no_wait_reserve
, no_wait_gpu
);
849 if (unlikely(ret
!= 0))
852 if (mem
->mm_node
== NULL
)
854 mem
->mem_type
= mem_type
;
858 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
859 uint32_t cur_placement
,
860 uint32_t proposed_placement
)
862 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
863 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
866 * Keep current caching if possible.
869 if ((cur_placement
& caching
) != 0)
870 result
|= (cur_placement
& caching
);
871 else if ((man
->default_caching
& caching
) != 0)
872 result
|= man
->default_caching
;
873 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
874 result
|= TTM_PL_FLAG_CACHED
;
875 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
876 result
|= TTM_PL_FLAG_WC
;
877 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
878 result
|= TTM_PL_FLAG_UNCACHED
;
883 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
886 uint32_t proposed_placement
,
887 uint32_t *masked_placement
)
889 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
891 if ((man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && disallow_fixed
)
894 if ((cur_flags
& proposed_placement
& TTM_PL_MASK_MEM
) == 0)
897 if ((proposed_placement
& man
->available_caching
) == 0)
900 cur_flags
|= (proposed_placement
& man
->available_caching
);
902 *masked_placement
= cur_flags
;
907 * Creates space for memory region @mem according to its type.
909 * This function first searches for free space in compatible memory types in
910 * the priority order defined by the driver. If free space isn't found, then
911 * ttm_bo_mem_force_space is attempted in priority order to evict and find
914 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
915 struct ttm_placement
*placement
,
916 struct ttm_mem_reg
*mem
,
917 bool interruptible
, bool no_wait_reserve
,
920 struct ttm_bo_device
*bdev
= bo
->bdev
;
921 struct ttm_mem_type_manager
*man
;
922 uint32_t mem_type
= TTM_PL_SYSTEM
;
923 uint32_t cur_flags
= 0;
924 bool type_found
= false;
925 bool type_ok
= false;
926 bool has_erestartsys
= false;
930 for (i
= 0; i
< placement
->num_placement
; ++i
) {
931 ret
= ttm_mem_type_from_flags(placement
->placement
[i
],
935 man
= &bdev
->man
[mem_type
];
937 type_ok
= ttm_bo_mt_compatible(man
,
938 bo
->type
== ttm_bo_type_user
,
940 placement
->placement
[i
],
946 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
949 * Use the access and other non-mapping-related flag bits from
950 * the memory placement flags to the current flags
952 ttm_flag_masked(&cur_flags
, placement
->placement
[i
],
953 ~TTM_PL_MASK_MEMTYPE
);
955 if (mem_type
== TTM_PL_SYSTEM
)
958 if (man
->has_type
&& man
->use_type
) {
960 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
968 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
969 mem
->mem_type
= mem_type
;
970 mem
->placement
= cur_flags
;
977 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
978 ret
= ttm_mem_type_from_flags(placement
->busy_placement
[i
],
982 man
= &bdev
->man
[mem_type
];
985 if (!ttm_bo_mt_compatible(man
,
986 bo
->type
== ttm_bo_type_user
,
988 placement
->busy_placement
[i
],
992 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
995 * Use the access and other non-mapping-related flag bits from
996 * the memory placement flags to the current flags
998 ttm_flag_masked(&cur_flags
, placement
->busy_placement
[i
],
999 ~TTM_PL_MASK_MEMTYPE
);
1002 if (mem_type
== TTM_PL_SYSTEM
) {
1003 mem
->mem_type
= mem_type
;
1004 mem
->placement
= cur_flags
;
1005 mem
->mm_node
= NULL
;
1009 ret
= ttm_bo_mem_force_space(bo
, mem_type
, placement
, mem
,
1010 interruptible
, no_wait_reserve
, no_wait_gpu
);
1011 if (ret
== 0 && mem
->mm_node
) {
1012 mem
->placement
= cur_flags
;
1015 if (ret
== -ERESTARTSYS
)
1016 has_erestartsys
= true;
1018 ret
= (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
1021 EXPORT_SYMBOL(ttm_bo_mem_space
);
1023 int ttm_bo_wait_cpu(struct ttm_buffer_object
*bo
, bool no_wait
)
1025 if ((atomic_read(&bo
->cpu_writers
) > 0) && no_wait
)
1028 return wait_event_interruptible(bo
->event_queue
,
1029 atomic_read(&bo
->cpu_writers
) == 0);
1031 EXPORT_SYMBOL(ttm_bo_wait_cpu
);
1033 int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1034 struct ttm_placement
*placement
,
1035 bool interruptible
, bool no_wait_reserve
,
1039 struct ttm_mem_reg mem
;
1041 BUG_ON(!atomic_read(&bo
->reserved
));
1044 * FIXME: It's possible to pipeline buffer moves.
1045 * Have the driver move function wait for idle when necessary,
1046 * instead of doing it here.
1048 spin_lock(&bo
->lock
);
1049 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
1050 spin_unlock(&bo
->lock
);
1053 mem
.num_pages
= bo
->num_pages
;
1054 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1055 mem
.page_alignment
= bo
->mem
.page_alignment
;
1056 mem
.bus
.io_reserved
= false;
1058 * Determine where to move the buffer.
1060 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, interruptible
, no_wait_reserve
, no_wait_gpu
);
1063 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, interruptible
, no_wait_reserve
, no_wait_gpu
);
1065 if (ret
&& mem
.mm_node
)
1066 ttm_bo_mem_put(bo
, &mem
);
1070 static int ttm_bo_mem_compat(struct ttm_placement
*placement
,
1071 struct ttm_mem_reg
*mem
)
1075 if (mem
->mm_node
&& placement
->lpfn
!= 0 &&
1076 (mem
->start
< placement
->fpfn
||
1077 mem
->start
+ mem
->num_pages
> placement
->lpfn
))
1080 for (i
= 0; i
< placement
->num_placement
; i
++) {
1081 if ((placement
->placement
[i
] & mem
->placement
&
1082 TTM_PL_MASK_CACHING
) &&
1083 (placement
->placement
[i
] & mem
->placement
&
1090 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1091 struct ttm_placement
*placement
,
1092 bool interruptible
, bool no_wait_reserve
,
1097 BUG_ON(!atomic_read(&bo
->reserved
));
1098 /* Check that range is valid */
1099 if (placement
->lpfn
|| placement
->fpfn
)
1100 if (placement
->fpfn
> placement
->lpfn
||
1101 (placement
->lpfn
- placement
->fpfn
) < bo
->num_pages
)
1104 * Check whether we need to move buffer.
1106 ret
= ttm_bo_mem_compat(placement
, &bo
->mem
);
1108 ret
= ttm_bo_move_buffer(bo
, placement
, interruptible
, no_wait_reserve
, no_wait_gpu
);
1113 * Use the access and other non-mapping-related flag bits from
1114 * the compatible memory placement flags to the active flags
1116 ttm_flag_masked(&bo
->mem
.placement
, placement
->placement
[ret
],
1117 ~TTM_PL_MASK_MEMTYPE
);
1120 * We might need to add a TTM.
1122 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1123 ret
= ttm_bo_add_ttm(bo
, true);
1129 EXPORT_SYMBOL(ttm_bo_validate
);
1131 int ttm_bo_check_placement(struct ttm_buffer_object
*bo
,
1132 struct ttm_placement
*placement
)
1134 BUG_ON((placement
->fpfn
|| placement
->lpfn
) &&
1135 (bo
->mem
.num_pages
> (placement
->lpfn
- placement
->fpfn
)));
1140 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1141 struct ttm_buffer_object
*bo
,
1143 enum ttm_bo_type type
,
1144 struct ttm_placement
*placement
,
1145 uint32_t page_alignment
,
1146 unsigned long buffer_start
,
1148 struct file
*persistant_swap_storage
,
1150 void (*destroy
) (struct ttm_buffer_object
*))
1153 unsigned long num_pages
;
1155 size
+= buffer_start
& ~PAGE_MASK
;
1156 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1157 if (num_pages
== 0) {
1158 printk(KERN_ERR TTM_PFX
"Illegal buffer object size.\n");
1165 bo
->destroy
= destroy
;
1167 spin_lock_init(&bo
->lock
);
1168 kref_init(&bo
->kref
);
1169 kref_init(&bo
->list_kref
);
1170 atomic_set(&bo
->cpu_writers
, 0);
1171 atomic_set(&bo
->reserved
, 1);
1172 init_waitqueue_head(&bo
->event_queue
);
1173 INIT_LIST_HEAD(&bo
->lru
);
1174 INIT_LIST_HEAD(&bo
->ddestroy
);
1175 INIT_LIST_HEAD(&bo
->swap
);
1177 bo
->glob
= bdev
->glob
;
1179 bo
->num_pages
= num_pages
;
1180 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1181 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1182 bo
->mem
.num_pages
= bo
->num_pages
;
1183 bo
->mem
.mm_node
= NULL
;
1184 bo
->mem
.page_alignment
= page_alignment
;
1185 bo
->mem
.bus
.io_reserved
= false;
1186 bo
->buffer_start
= buffer_start
& PAGE_MASK
;
1188 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1189 bo
->seq_valid
= false;
1190 bo
->persistant_swap_storage
= persistant_swap_storage
;
1191 bo
->acc_size
= acc_size
;
1192 atomic_inc(&bo
->glob
->bo_count
);
1194 ret
= ttm_bo_check_placement(bo
, placement
);
1195 if (unlikely(ret
!= 0))
1199 * For ttm_bo_type_device buffers, allocate
1200 * address space from the device.
1202 if (bo
->type
== ttm_bo_type_device
) {
1203 ret
= ttm_bo_setup_vm(bo
);
1208 ret
= ttm_bo_validate(bo
, placement
, interruptible
, false, false);
1212 ttm_bo_unreserve(bo
);
1216 ttm_bo_unreserve(bo
);
1221 EXPORT_SYMBOL(ttm_bo_init
);
1223 static inline size_t ttm_bo_size(struct ttm_bo_global
*glob
,
1224 unsigned long num_pages
)
1226 size_t page_array_size
= (num_pages
* sizeof(void *) + PAGE_SIZE
- 1) &
1229 return glob
->ttm_bo_size
+ 2 * page_array_size
;
1232 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1234 enum ttm_bo_type type
,
1235 struct ttm_placement
*placement
,
1236 uint32_t page_alignment
,
1237 unsigned long buffer_start
,
1239 struct file
*persistant_swap_storage
,
1240 struct ttm_buffer_object
**p_bo
)
1242 struct ttm_buffer_object
*bo
;
1243 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1247 ttm_bo_size(bdev
->glob
, (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
);
1248 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1249 if (unlikely(ret
!= 0))
1252 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1254 if (unlikely(bo
== NULL
)) {
1255 ttm_mem_global_free(mem_glob
, acc_size
);
1259 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1260 buffer_start
, interruptible
,
1261 persistant_swap_storage
, acc_size
, NULL
);
1262 if (likely(ret
== 0))
1268 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1269 unsigned mem_type
, bool allow_errors
)
1271 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1272 struct ttm_bo_global
*glob
= bdev
->glob
;
1276 * Can't use standard list traversal since we're unlocking.
1279 spin_lock(&glob
->lru_lock
);
1280 while (!list_empty(&man
->lru
)) {
1281 spin_unlock(&glob
->lru_lock
);
1282 ret
= ttm_mem_evict_first(bdev
, mem_type
, false, false, false);
1287 printk(KERN_ERR TTM_PFX
1288 "Cleanup eviction failed\n");
1291 spin_lock(&glob
->lru_lock
);
1293 spin_unlock(&glob
->lru_lock
);
1297 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1299 struct ttm_mem_type_manager
*man
;
1302 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1303 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", mem_type
);
1306 man
= &bdev
->man
[mem_type
];
1308 if (!man
->has_type
) {
1309 printk(KERN_ERR TTM_PFX
"Trying to take down uninitialized "
1310 "memory manager type %u\n", mem_type
);
1314 man
->use_type
= false;
1315 man
->has_type
= false;
1319 ttm_bo_force_list_clean(bdev
, mem_type
, false);
1321 ret
= (*man
->func
->takedown
)(man
);
1326 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1328 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1330 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1332 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1333 printk(KERN_ERR TTM_PFX
1334 "Illegal memory manager memory type %u.\n",
1339 if (!man
->has_type
) {
1340 printk(KERN_ERR TTM_PFX
1341 "Memory type %u has not been initialized.\n",
1346 return ttm_bo_force_list_clean(bdev
, mem_type
, true);
1348 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1350 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1351 unsigned long p_size
)
1354 struct ttm_mem_type_manager
*man
;
1356 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1357 man
= &bdev
->man
[type
];
1358 BUG_ON(man
->has_type
);
1360 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1366 if (type
!= TTM_PL_SYSTEM
) {
1367 ret
= (*man
->func
->init
)(man
, p_size
);
1371 man
->has_type
= true;
1372 man
->use_type
= true;
1375 INIT_LIST_HEAD(&man
->lru
);
1379 EXPORT_SYMBOL(ttm_bo_init_mm
);
1381 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1383 struct ttm_bo_global
*glob
=
1384 container_of(kobj
, struct ttm_bo_global
, kobj
);
1386 ttm_mem_unregister_shrink(glob
->mem_glob
, &glob
->shrink
);
1387 __free_page(glob
->dummy_read_page
);
1391 void ttm_bo_global_release(struct drm_global_reference
*ref
)
1393 struct ttm_bo_global
*glob
= ref
->object
;
1395 kobject_del(&glob
->kobj
);
1396 kobject_put(&glob
->kobj
);
1398 EXPORT_SYMBOL(ttm_bo_global_release
);
1400 int ttm_bo_global_init(struct drm_global_reference
*ref
)
1402 struct ttm_bo_global_ref
*bo_ref
=
1403 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1404 struct ttm_bo_global
*glob
= ref
->object
;
1407 mutex_init(&glob
->device_list_mutex
);
1408 spin_lock_init(&glob
->lru_lock
);
1409 glob
->mem_glob
= bo_ref
->mem_glob
;
1410 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1412 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1417 INIT_LIST_HEAD(&glob
->swap_lru
);
1418 INIT_LIST_HEAD(&glob
->device_list
);
1420 ttm_mem_init_shrink(&glob
->shrink
, ttm_bo_swapout
);
1421 ret
= ttm_mem_register_shrink(glob
->mem_glob
, &glob
->shrink
);
1422 if (unlikely(ret
!= 0)) {
1423 printk(KERN_ERR TTM_PFX
1424 "Could not register buffer object swapout.\n");
1428 glob
->ttm_bo_extra_size
=
1429 ttm_round_pot(sizeof(struct ttm_tt
)) +
1430 ttm_round_pot(sizeof(struct ttm_backend
));
1432 glob
->ttm_bo_size
= glob
->ttm_bo_extra_size
+
1433 ttm_round_pot(sizeof(struct ttm_buffer_object
));
1435 atomic_set(&glob
->bo_count
, 0);
1437 ret
= kobject_init_and_add(
1438 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1439 if (unlikely(ret
!= 0))
1440 kobject_put(&glob
->kobj
);
1443 __free_page(glob
->dummy_read_page
);
1448 EXPORT_SYMBOL(ttm_bo_global_init
);
1451 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1454 unsigned i
= TTM_NUM_MEM_TYPES
;
1455 struct ttm_mem_type_manager
*man
;
1456 struct ttm_bo_global
*glob
= bdev
->glob
;
1459 man
= &bdev
->man
[i
];
1460 if (man
->has_type
) {
1461 man
->use_type
= false;
1462 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1464 printk(KERN_ERR TTM_PFX
1465 "DRM memory manager type %d "
1466 "is not clean.\n", i
);
1468 man
->has_type
= false;
1472 mutex_lock(&glob
->device_list_mutex
);
1473 list_del(&bdev
->device_list
);
1474 mutex_unlock(&glob
->device_list_mutex
);
1476 if (!cancel_delayed_work(&bdev
->wq
))
1477 flush_scheduled_work();
1479 while (ttm_bo_delayed_delete(bdev
, true))
1482 spin_lock(&glob
->lru_lock
);
1483 if (list_empty(&bdev
->ddestroy
))
1484 TTM_DEBUG("Delayed destroy list was clean\n");
1486 if (list_empty(&bdev
->man
[0].lru
))
1487 TTM_DEBUG("Swap list was clean\n");
1488 spin_unlock(&glob
->lru_lock
);
1490 BUG_ON(!drm_mm_clean(&bdev
->addr_space_mm
));
1491 write_lock(&bdev
->vm_lock
);
1492 drm_mm_takedown(&bdev
->addr_space_mm
);
1493 write_unlock(&bdev
->vm_lock
);
1497 EXPORT_SYMBOL(ttm_bo_device_release
);
1499 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1500 struct ttm_bo_global
*glob
,
1501 struct ttm_bo_driver
*driver
,
1502 uint64_t file_page_offset
,
1507 rwlock_init(&bdev
->vm_lock
);
1508 bdev
->driver
= driver
;
1510 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1513 * Initialize the system memory buffer type.
1514 * Other types need to be driver / IOCTL initialized.
1516 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1517 if (unlikely(ret
!= 0))
1520 bdev
->addr_space_rb
= RB_ROOT
;
1521 ret
= drm_mm_init(&bdev
->addr_space_mm
, file_page_offset
, 0x10000000);
1522 if (unlikely(ret
!= 0))
1523 goto out_no_addr_mm
;
1525 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1526 bdev
->nice_mode
= true;
1527 INIT_LIST_HEAD(&bdev
->ddestroy
);
1528 bdev
->dev_mapping
= NULL
;
1530 bdev
->need_dma32
= need_dma32
;
1532 mutex_lock(&glob
->device_list_mutex
);
1533 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1534 mutex_unlock(&glob
->device_list_mutex
);
1538 ttm_bo_clean_mm(bdev
, 0);
1542 EXPORT_SYMBOL(ttm_bo_device_init
);
1545 * buffer object vm functions.
1548 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1550 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1552 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1553 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1556 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1559 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1565 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1567 struct ttm_bo_device
*bdev
= bo
->bdev
;
1568 loff_t offset
= (loff_t
) bo
->addr_space_offset
;
1569 loff_t holelen
= ((loff_t
) bo
->mem
.num_pages
) << PAGE_SHIFT
;
1571 if (!bdev
->dev_mapping
)
1573 unmap_mapping_range(bdev
->dev_mapping
, offset
, holelen
, 1);
1574 ttm_mem_io_free(bdev
, &bo
->mem
);
1576 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1578 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object
*bo
)
1580 struct ttm_bo_device
*bdev
= bo
->bdev
;
1581 struct rb_node
**cur
= &bdev
->addr_space_rb
.rb_node
;
1582 struct rb_node
*parent
= NULL
;
1583 struct ttm_buffer_object
*cur_bo
;
1584 unsigned long offset
= bo
->vm_node
->start
;
1585 unsigned long cur_offset
;
1589 cur_bo
= rb_entry(parent
, struct ttm_buffer_object
, vm_rb
);
1590 cur_offset
= cur_bo
->vm_node
->start
;
1591 if (offset
< cur_offset
)
1592 cur
= &parent
->rb_left
;
1593 else if (offset
> cur_offset
)
1594 cur
= &parent
->rb_right
;
1599 rb_link_node(&bo
->vm_rb
, parent
, cur
);
1600 rb_insert_color(&bo
->vm_rb
, &bdev
->addr_space_rb
);
1606 * @bo: the buffer to allocate address space for
1608 * Allocate address space in the drm device so that applications
1609 * can mmap the buffer and access the contents. This only
1610 * applies to ttm_bo_type_device objects as others are not
1611 * placed in the drm device address space.
1614 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
)
1616 struct ttm_bo_device
*bdev
= bo
->bdev
;
1620 ret
= drm_mm_pre_get(&bdev
->addr_space_mm
);
1621 if (unlikely(ret
!= 0))
1624 write_lock(&bdev
->vm_lock
);
1625 bo
->vm_node
= drm_mm_search_free(&bdev
->addr_space_mm
,
1626 bo
->mem
.num_pages
, 0, 0);
1628 if (unlikely(bo
->vm_node
== NULL
)) {
1633 bo
->vm_node
= drm_mm_get_block_atomic(bo
->vm_node
,
1634 bo
->mem
.num_pages
, 0);
1636 if (unlikely(bo
->vm_node
== NULL
)) {
1637 write_unlock(&bdev
->vm_lock
);
1641 ttm_bo_vm_insert_rb(bo
);
1642 write_unlock(&bdev
->vm_lock
);
1643 bo
->addr_space_offset
= ((uint64_t) bo
->vm_node
->start
) << PAGE_SHIFT
;
1647 write_unlock(&bdev
->vm_lock
);
1651 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1652 bool lazy
, bool interruptible
, bool no_wait
)
1654 struct ttm_bo_driver
*driver
= bo
->bdev
->driver
;
1659 if (likely(bo
->sync_obj
== NULL
))
1662 while (bo
->sync_obj
) {
1664 if (driver
->sync_obj_signaled(bo
->sync_obj
, bo
->sync_obj_arg
)) {
1665 void *tmp_obj
= bo
->sync_obj
;
1666 bo
->sync_obj
= NULL
;
1667 clear_bit(TTM_BO_PRIV_FLAG_MOVING
, &bo
->priv_flags
);
1668 spin_unlock(&bo
->lock
);
1669 driver
->sync_obj_unref(&tmp_obj
);
1670 spin_lock(&bo
->lock
);
1677 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
1678 sync_obj_arg
= bo
->sync_obj_arg
;
1679 spin_unlock(&bo
->lock
);
1680 ret
= driver
->sync_obj_wait(sync_obj
, sync_obj_arg
,
1681 lazy
, interruptible
);
1682 if (unlikely(ret
!= 0)) {
1683 driver
->sync_obj_unref(&sync_obj
);
1684 spin_lock(&bo
->lock
);
1687 spin_lock(&bo
->lock
);
1688 if (likely(bo
->sync_obj
== sync_obj
&&
1689 bo
->sync_obj_arg
== sync_obj_arg
)) {
1690 void *tmp_obj
= bo
->sync_obj
;
1691 bo
->sync_obj
= NULL
;
1692 clear_bit(TTM_BO_PRIV_FLAG_MOVING
,
1694 spin_unlock(&bo
->lock
);
1695 driver
->sync_obj_unref(&sync_obj
);
1696 driver
->sync_obj_unref(&tmp_obj
);
1697 spin_lock(&bo
->lock
);
1699 spin_unlock(&bo
->lock
);
1700 driver
->sync_obj_unref(&sync_obj
);
1701 spin_lock(&bo
->lock
);
1706 EXPORT_SYMBOL(ttm_bo_wait
);
1708 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1713 * Using ttm_bo_reserve makes sure the lru lists are updated.
1716 ret
= ttm_bo_reserve(bo
, true, no_wait
, false, 0);
1717 if (unlikely(ret
!= 0))
1719 spin_lock(&bo
->lock
);
1720 ret
= ttm_bo_wait(bo
, false, true, no_wait
);
1721 spin_unlock(&bo
->lock
);
1722 if (likely(ret
== 0))
1723 atomic_inc(&bo
->cpu_writers
);
1724 ttm_bo_unreserve(bo
);
1727 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1729 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1731 if (atomic_dec_and_test(&bo
->cpu_writers
))
1732 wake_up_all(&bo
->event_queue
);
1734 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1737 * A buffer object shrink method that tries to swap out the first
1738 * buffer object on the bo_global::swap_lru list.
1741 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
)
1743 struct ttm_bo_global
*glob
=
1744 container_of(shrink
, struct ttm_bo_global
, shrink
);
1745 struct ttm_buffer_object
*bo
;
1748 uint32_t swap_placement
= (TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_SYSTEM
);
1750 spin_lock(&glob
->lru_lock
);
1751 while (ret
== -EBUSY
) {
1752 if (unlikely(list_empty(&glob
->swap_lru
))) {
1753 spin_unlock(&glob
->lru_lock
);
1757 bo
= list_first_entry(&glob
->swap_lru
,
1758 struct ttm_buffer_object
, swap
);
1759 kref_get(&bo
->list_kref
);
1761 if (!list_empty(&bo
->ddestroy
)) {
1762 spin_unlock(&glob
->lru_lock
);
1763 (void) ttm_bo_cleanup_refs(bo
, false, false, false);
1764 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1769 * Reserve buffer. Since we unlock while sleeping, we need
1770 * to re-check that nobody removed us from the swap-list while
1774 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
1775 if (unlikely(ret
== -EBUSY
)) {
1776 spin_unlock(&glob
->lru_lock
);
1777 ttm_bo_wait_unreserved(bo
, false);
1778 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1779 spin_lock(&glob
->lru_lock
);
1784 put_count
= ttm_bo_del_from_lru(bo
);
1785 spin_unlock(&glob
->lru_lock
);
1787 ttm_bo_list_ref_sub(bo
, put_count
, true);
1790 * Wait for GPU, then move to system cached.
1793 spin_lock(&bo
->lock
);
1794 ret
= ttm_bo_wait(bo
, false, false, false);
1795 spin_unlock(&bo
->lock
);
1797 if (unlikely(ret
!= 0))
1800 if ((bo
->mem
.placement
& swap_placement
) != swap_placement
) {
1801 struct ttm_mem_reg evict_mem
;
1803 evict_mem
= bo
->mem
;
1804 evict_mem
.mm_node
= NULL
;
1805 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1806 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1808 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true,
1809 false, false, false);
1810 if (unlikely(ret
!= 0))
1814 ttm_bo_unmap_virtual(bo
);
1817 * Swap out. Buffer will be swapped in again as soon as
1818 * anyone tries to access a ttm page.
1821 if (bo
->bdev
->driver
->swap_notify
)
1822 bo
->bdev
->driver
->swap_notify(bo
);
1824 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistant_swap_storage
);
1829 * Unreserve without putting on LRU to avoid swapping out an
1830 * already swapped buffer.
1833 atomic_set(&bo
->reserved
, 0);
1834 wake_up_all(&bo
->event_queue
);
1835 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1839 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1841 while (ttm_bo_swapout(&bdev
->glob
->shrink
) == 0)
1844 EXPORT_SYMBOL(ttm_bo_swapout_all
);