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 #define pr_fmt(fmt) "[TTM] " fmt
33 #include <drm/ttm/ttm_module.h>
34 #include <drm/ttm/ttm_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
44 #define TTM_ASSERT_LOCKED(param)
45 #define TTM_DEBUG(fmt, arg...)
46 #define TTM_BO_HASH_ORDER 13
48 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
);
49 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
);
50 static void ttm_bo_global_kobj_release(struct kobject
*kobj
);
52 static struct attribute ttm_bo_count
= {
57 static inline int ttm_mem_type_from_flags(uint32_t flags
, uint32_t *mem_type
)
61 for (i
= 0; i
<= TTM_PL_PRIV5
; i
++)
62 if (flags
& (1 << i
)) {
69 static void ttm_mem_type_debug(struct ttm_bo_device
*bdev
, int mem_type
)
71 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
73 pr_err(" has_type: %d\n", man
->has_type
);
74 pr_err(" use_type: %d\n", man
->use_type
);
75 pr_err(" flags: 0x%08X\n", man
->flags
);
76 pr_err(" gpu_offset: 0x%08lX\n", man
->gpu_offset
);
77 pr_err(" size: %llu\n", man
->size
);
78 pr_err(" available_caching: 0x%08X\n", man
->available_caching
);
79 pr_err(" default_caching: 0x%08X\n", 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 pr_err("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 pr_err(" placement[%d]=0x%08X (%d)\n",
98 i
, placement
->placement
[i
], mem_type
);
99 ttm_mem_type_debug(bo
->bdev
, mem_type
);
103 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
104 struct attribute
*attr
,
107 struct ttm_bo_global
*glob
=
108 container_of(kobj
, struct ttm_bo_global
, kobj
);
110 return snprintf(buffer
, PAGE_SIZE
, "%lu\n",
111 (unsigned long) atomic_read(&glob
->bo_count
));
114 static struct attribute
*ttm_bo_global_attrs
[] = {
119 static const struct sysfs_ops ttm_bo_global_ops
= {
120 .show
= &ttm_bo_global_show
123 static struct kobj_type ttm_bo_glob_kobj_type
= {
124 .release
= &ttm_bo_global_kobj_release
,
125 .sysfs_ops
= &ttm_bo_global_ops
,
126 .default_attrs
= ttm_bo_global_attrs
130 static inline uint32_t ttm_bo_type_flags(unsigned type
)
135 static void ttm_bo_release_list(struct kref
*list_kref
)
137 struct ttm_buffer_object
*bo
=
138 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
139 struct ttm_bo_device
*bdev
= bo
->bdev
;
140 size_t acc_size
= bo
->acc_size
;
142 BUG_ON(atomic_read(&bo
->list_kref
.refcount
));
143 BUG_ON(atomic_read(&bo
->kref
.refcount
));
144 BUG_ON(atomic_read(&bo
->cpu_writers
));
145 BUG_ON(bo
->sync_obj
!= NULL
);
146 BUG_ON(bo
->mem
.mm_node
!= NULL
);
147 BUG_ON(!list_empty(&bo
->lru
));
148 BUG_ON(!list_empty(&bo
->ddestroy
));
151 ttm_tt_destroy(bo
->ttm
);
152 atomic_dec(&bo
->glob
->bo_count
);
158 ttm_mem_global_free(bdev
->glob
->mem_glob
, acc_size
);
161 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
, bool interruptible
)
164 return wait_event_interruptible(bo
->event_queue
,
165 !ttm_bo_is_reserved(bo
));
167 wait_event(bo
->event_queue
, !ttm_bo_is_reserved(bo
));
171 EXPORT_SYMBOL(ttm_bo_wait_unreserved
);
173 void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
175 struct ttm_bo_device
*bdev
= bo
->bdev
;
176 struct ttm_mem_type_manager
*man
;
178 BUG_ON(!ttm_bo_is_reserved(bo
));
180 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
182 BUG_ON(!list_empty(&bo
->lru
));
184 man
= &bdev
->man
[bo
->mem
.mem_type
];
185 list_add_tail(&bo
->lru
, &man
->lru
);
186 kref_get(&bo
->list_kref
);
188 if (bo
->ttm
!= NULL
) {
189 list_add_tail(&bo
->swap
, &bo
->glob
->swap_lru
);
190 kref_get(&bo
->list_kref
);
195 int ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
199 if (!list_empty(&bo
->swap
)) {
200 list_del_init(&bo
->swap
);
203 if (!list_empty(&bo
->lru
)) {
204 list_del_init(&bo
->lru
);
209 * TODO: Add a driver hook to delete from
210 * driver-specific LRU's here.
216 int ttm_bo_reserve_locked(struct ttm_buffer_object
*bo
,
218 bool no_wait
, bool use_sequence
, uint32_t sequence
)
220 struct ttm_bo_global
*glob
= bo
->glob
;
223 while (unlikely(atomic_read(&bo
->reserved
) != 0)) {
225 * Deadlock avoidance for multi-bo reserving.
227 if (use_sequence
&& bo
->seq_valid
) {
229 * We've already reserved this one.
231 if (unlikely(sequence
== bo
->val_seq
))
234 * Already reserved by a thread that will not back
235 * off for us. We need to back off.
237 if (unlikely(sequence
- bo
->val_seq
< (1 << 31)))
244 spin_unlock(&glob
->lru_lock
);
245 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
246 spin_lock(&glob
->lru_lock
);
252 atomic_set(&bo
->reserved
, 1);
255 * Wake up waiters that may need to recheck for deadlock,
256 * if we decreased the sequence number.
258 if (unlikely((bo
->val_seq
- sequence
< (1 << 31))
260 wake_up_all(&bo
->event_queue
);
262 bo
->val_seq
= sequence
;
263 bo
->seq_valid
= true;
265 bo
->seq_valid
= false;
270 EXPORT_SYMBOL(ttm_bo_reserve
);
272 static void ttm_bo_ref_bug(struct kref
*list_kref
)
277 void ttm_bo_list_ref_sub(struct ttm_buffer_object
*bo
, int count
,
280 kref_sub(&bo
->list_kref
, count
,
281 (never_free
) ? ttm_bo_ref_bug
: ttm_bo_release_list
);
284 int ttm_bo_reserve(struct ttm_buffer_object
*bo
,
286 bool no_wait
, bool use_sequence
, uint32_t sequence
)
288 struct ttm_bo_global
*glob
= bo
->glob
;
292 spin_lock(&glob
->lru_lock
);
293 ret
= ttm_bo_reserve_locked(bo
, interruptible
, no_wait
, use_sequence
,
295 if (likely(ret
== 0))
296 put_count
= ttm_bo_del_from_lru(bo
);
297 spin_unlock(&glob
->lru_lock
);
299 ttm_bo_list_ref_sub(bo
, put_count
, true);
304 void ttm_bo_unreserve_locked(struct ttm_buffer_object
*bo
)
306 ttm_bo_add_to_lru(bo
);
307 atomic_set(&bo
->reserved
, 0);
308 wake_up_all(&bo
->event_queue
);
311 void ttm_bo_unreserve(struct ttm_buffer_object
*bo
)
313 struct ttm_bo_global
*glob
= bo
->glob
;
315 spin_lock(&glob
->lru_lock
);
316 ttm_bo_unreserve_locked(bo
);
317 spin_unlock(&glob
->lru_lock
);
319 EXPORT_SYMBOL(ttm_bo_unreserve
);
322 * Call bo->mutex locked.
324 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
326 struct ttm_bo_device
*bdev
= bo
->bdev
;
327 struct ttm_bo_global
*glob
= bo
->glob
;
329 uint32_t page_flags
= 0;
331 TTM_ASSERT_LOCKED(&bo
->mutex
);
334 if (bdev
->need_dma32
)
335 page_flags
|= TTM_PAGE_FLAG_DMA32
;
338 case ttm_bo_type_device
:
340 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
341 case ttm_bo_type_kernel
:
342 bo
->ttm
= bdev
->driver
->ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
343 page_flags
, glob
->dummy_read_page
);
344 if (unlikely(bo
->ttm
== NULL
))
348 bo
->ttm
= bdev
->driver
->ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
349 page_flags
| TTM_PAGE_FLAG_SG
,
350 glob
->dummy_read_page
);
351 if (unlikely(bo
->ttm
== NULL
)) {
355 bo
->ttm
->sg
= bo
->sg
;
358 pr_err("Illegal buffer object type\n");
366 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
367 struct ttm_mem_reg
*mem
,
368 bool evict
, bool interruptible
,
371 struct ttm_bo_device
*bdev
= bo
->bdev
;
372 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
373 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
374 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
375 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
378 if (old_is_pci
|| new_is_pci
||
379 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0)) {
380 ret
= ttm_mem_io_lock(old_man
, true);
381 if (unlikely(ret
!= 0))
383 ttm_bo_unmap_virtual_locked(bo
);
384 ttm_mem_io_unlock(old_man
);
388 * Create and bind a ttm if required.
391 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
392 if (bo
->ttm
== NULL
) {
393 bool zero
= !(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
);
394 ret
= ttm_bo_add_ttm(bo
, zero
);
399 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
403 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
404 ret
= ttm_tt_bind(bo
->ttm
, mem
);
409 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
410 if (bdev
->driver
->move_notify
)
411 bdev
->driver
->move_notify(bo
, mem
);
418 if (bdev
->driver
->move_notify
)
419 bdev
->driver
->move_notify(bo
, mem
);
421 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
422 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
423 ret
= ttm_bo_move_ttm(bo
, evict
, no_wait_gpu
, mem
);
424 else if (bdev
->driver
->move
)
425 ret
= bdev
->driver
->move(bo
, evict
, interruptible
,
428 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait_gpu
, mem
);
431 if (bdev
->driver
->move_notify
) {
432 struct ttm_mem_reg tmp_mem
= *mem
;
435 bdev
->driver
->move_notify(bo
, mem
);
445 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
447 pr_err("Can not flush read caches\n");
451 if (bo
->mem
.mm_node
) {
452 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
453 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
454 bo
->cur_placement
= bo
->mem
.placement
;
461 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
462 if ((new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
) {
463 ttm_tt_unbind(bo
->ttm
);
464 ttm_tt_destroy(bo
->ttm
);
473 * Will release GPU memory type usage on destruction.
474 * This is the place to put in driver specific hooks to release
475 * driver private resources.
476 * Will release the bo::reserved lock.
479 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
481 if (bo
->bdev
->driver
->move_notify
)
482 bo
->bdev
->driver
->move_notify(bo
, NULL
);
485 ttm_tt_unbind(bo
->ttm
);
486 ttm_tt_destroy(bo
->ttm
);
489 ttm_bo_mem_put(bo
, &bo
->mem
);
491 atomic_set(&bo
->reserved
, 0);
492 wake_up_all(&bo
->event_queue
);
495 * Since the final reference to this bo may not be dropped by
496 * the current task we have to put a memory barrier here to make
497 * sure the changes done in this function are always visible.
499 * This function only needs protection against the final kref_put.
501 smp_mb__before_atomic_dec();
504 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
506 struct ttm_bo_device
*bdev
= bo
->bdev
;
507 struct ttm_bo_global
*glob
= bo
->glob
;
508 struct ttm_bo_driver
*driver
= bdev
->driver
;
509 void *sync_obj
= NULL
;
513 spin_lock(&glob
->lru_lock
);
514 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
516 spin_lock(&bdev
->fence_lock
);
517 (void) ttm_bo_wait(bo
, false, false, true);
518 if (!ret
&& !bo
->sync_obj
) {
519 spin_unlock(&bdev
->fence_lock
);
520 put_count
= ttm_bo_del_from_lru(bo
);
522 spin_unlock(&glob
->lru_lock
);
523 ttm_bo_cleanup_memtype_use(bo
);
525 ttm_bo_list_ref_sub(bo
, put_count
, true);
530 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
531 spin_unlock(&bdev
->fence_lock
);
534 atomic_set(&bo
->reserved
, 0);
535 wake_up_all(&bo
->event_queue
);
538 kref_get(&bo
->list_kref
);
539 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
540 spin_unlock(&glob
->lru_lock
);
543 driver
->sync_obj_flush(sync_obj
);
544 driver
->sync_obj_unref(&sync_obj
);
546 schedule_delayed_work(&bdev
->wq
,
547 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
551 * function ttm_bo_cleanup_refs_and_unlock
552 * If bo idle, remove from delayed- and lru lists, and unref.
553 * If not idle, do nothing.
555 * Must be called with lru_lock and reservation held, this function
556 * will drop both before returning.
558 * @interruptible Any sleeps should occur interruptibly.
559 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
562 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object
*bo
,
566 struct ttm_bo_device
*bdev
= bo
->bdev
;
567 struct ttm_bo_driver
*driver
= bdev
->driver
;
568 struct ttm_bo_global
*glob
= bo
->glob
;
572 spin_lock(&bdev
->fence_lock
);
573 ret
= ttm_bo_wait(bo
, false, false, true);
575 if (ret
&& !no_wait_gpu
) {
579 * Take a reference to the fence and unreserve,
580 * at this point the buffer should be dead, so
581 * no new sync objects can be attached.
583 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
584 spin_unlock(&bdev
->fence_lock
);
586 atomic_set(&bo
->reserved
, 0);
587 wake_up_all(&bo
->event_queue
);
588 spin_unlock(&glob
->lru_lock
);
590 ret
= driver
->sync_obj_wait(sync_obj
, false, interruptible
);
591 driver
->sync_obj_unref(&sync_obj
);
596 * remove sync_obj with ttm_bo_wait, the wait should be
597 * finished, and no new wait object should have been added.
599 spin_lock(&bdev
->fence_lock
);
600 ret
= ttm_bo_wait(bo
, false, false, true);
602 spin_unlock(&bdev
->fence_lock
);
606 spin_lock(&glob
->lru_lock
);
607 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
610 * We raced, and lost, someone else holds the reservation now,
611 * and is probably busy in ttm_bo_cleanup_memtype_use.
613 * Even if it's not the case, because we finished waiting any
614 * delayed destruction would succeed, so just return success
618 spin_unlock(&glob
->lru_lock
);
622 spin_unlock(&bdev
->fence_lock
);
624 if (ret
|| unlikely(list_empty(&bo
->ddestroy
))) {
625 atomic_set(&bo
->reserved
, 0);
626 wake_up_all(&bo
->event_queue
);
627 spin_unlock(&glob
->lru_lock
);
631 put_count
= ttm_bo_del_from_lru(bo
);
632 list_del_init(&bo
->ddestroy
);
635 spin_unlock(&glob
->lru_lock
);
636 ttm_bo_cleanup_memtype_use(bo
);
638 ttm_bo_list_ref_sub(bo
, put_count
, true);
644 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
645 * encountered buffers.
648 static int ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
650 struct ttm_bo_global
*glob
= bdev
->glob
;
651 struct ttm_buffer_object
*entry
= NULL
;
654 spin_lock(&glob
->lru_lock
);
655 if (list_empty(&bdev
->ddestroy
))
658 entry
= list_first_entry(&bdev
->ddestroy
,
659 struct ttm_buffer_object
, ddestroy
);
660 kref_get(&entry
->list_kref
);
663 struct ttm_buffer_object
*nentry
= NULL
;
665 if (entry
->ddestroy
.next
!= &bdev
->ddestroy
) {
666 nentry
= list_first_entry(&entry
->ddestroy
,
667 struct ttm_buffer_object
, ddestroy
);
668 kref_get(&nentry
->list_kref
);
671 ret
= ttm_bo_reserve_locked(entry
, false, !remove_all
, false, 0);
673 ret
= ttm_bo_cleanup_refs_and_unlock(entry
, false,
676 spin_unlock(&glob
->lru_lock
);
678 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
684 spin_lock(&glob
->lru_lock
);
685 if (list_empty(&entry
->ddestroy
))
690 spin_unlock(&glob
->lru_lock
);
693 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
697 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
699 struct ttm_bo_device
*bdev
=
700 container_of(work
, struct ttm_bo_device
, wq
.work
);
702 if (ttm_bo_delayed_delete(bdev
, false)) {
703 schedule_delayed_work(&bdev
->wq
,
704 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
708 static void ttm_bo_release(struct kref
*kref
)
710 struct ttm_buffer_object
*bo
=
711 container_of(kref
, struct ttm_buffer_object
, kref
);
712 struct ttm_bo_device
*bdev
= bo
->bdev
;
713 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
715 write_lock(&bdev
->vm_lock
);
716 if (likely(bo
->vm_node
!= NULL
)) {
717 rb_erase(&bo
->vm_rb
, &bdev
->addr_space_rb
);
718 drm_mm_put_block(bo
->vm_node
);
721 write_unlock(&bdev
->vm_lock
);
722 ttm_mem_io_lock(man
, false);
723 ttm_mem_io_free_vm(bo
);
724 ttm_mem_io_unlock(man
);
725 ttm_bo_cleanup_refs_or_queue(bo
);
726 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
729 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
731 struct ttm_buffer_object
*bo
= *p_bo
;
734 kref_put(&bo
->kref
, ttm_bo_release
);
736 EXPORT_SYMBOL(ttm_bo_unref
);
738 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
740 return cancel_delayed_work_sync(&bdev
->wq
);
742 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
744 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
747 schedule_delayed_work(&bdev
->wq
,
748 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
750 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
752 static int ttm_bo_evict(struct ttm_buffer_object
*bo
, bool interruptible
,
755 struct ttm_bo_device
*bdev
= bo
->bdev
;
756 struct ttm_mem_reg evict_mem
;
757 struct ttm_placement placement
;
760 spin_lock(&bdev
->fence_lock
);
761 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
762 spin_unlock(&bdev
->fence_lock
);
764 if (unlikely(ret
!= 0)) {
765 if (ret
!= -ERESTARTSYS
) {
766 pr_err("Failed to expire sync object before buffer eviction\n");
771 BUG_ON(!ttm_bo_is_reserved(bo
));
774 evict_mem
.mm_node
= NULL
;
775 evict_mem
.bus
.io_reserved_vm
= false;
776 evict_mem
.bus
.io_reserved_count
= 0;
780 placement
.num_placement
= 0;
781 placement
.num_busy_placement
= 0;
782 bdev
->driver
->evict_flags(bo
, &placement
);
783 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, interruptible
,
786 if (ret
!= -ERESTARTSYS
) {
787 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
789 ttm_bo_mem_space_debug(bo
, &placement
);
794 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, interruptible
,
797 if (ret
!= -ERESTARTSYS
)
798 pr_err("Buffer eviction failed\n");
799 ttm_bo_mem_put(bo
, &evict_mem
);
807 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
812 struct ttm_bo_global
*glob
= bdev
->glob
;
813 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
814 struct ttm_buffer_object
*bo
;
815 int ret
= -EBUSY
, put_count
;
817 spin_lock(&glob
->lru_lock
);
818 list_for_each_entry(bo
, &man
->lru
, lru
) {
819 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
825 spin_unlock(&glob
->lru_lock
);
829 kref_get(&bo
->list_kref
);
831 if (!list_empty(&bo
->ddestroy
)) {
832 ret
= ttm_bo_cleanup_refs_and_unlock(bo
, interruptible
,
834 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
838 put_count
= ttm_bo_del_from_lru(bo
);
839 spin_unlock(&glob
->lru_lock
);
843 ttm_bo_list_ref_sub(bo
, put_count
, true);
845 ret
= ttm_bo_evict(bo
, interruptible
, no_wait_gpu
);
846 ttm_bo_unreserve(bo
);
848 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
852 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
854 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
857 (*man
->func
->put_node
)(man
, mem
);
859 EXPORT_SYMBOL(ttm_bo_mem_put
);
862 * Repeatedly evict memory from the LRU for @mem_type until we create enough
863 * space, or we've evicted everything and there isn't enough space.
865 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
867 struct ttm_placement
*placement
,
868 struct ttm_mem_reg
*mem
,
872 struct ttm_bo_device
*bdev
= bo
->bdev
;
873 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
877 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
878 if (unlikely(ret
!= 0))
882 ret
= ttm_mem_evict_first(bdev
, mem_type
,
883 interruptible
, no_wait_gpu
);
884 if (unlikely(ret
!= 0))
887 if (mem
->mm_node
== NULL
)
889 mem
->mem_type
= mem_type
;
893 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
894 uint32_t cur_placement
,
895 uint32_t proposed_placement
)
897 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
898 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
901 * Keep current caching if possible.
904 if ((cur_placement
& caching
) != 0)
905 result
|= (cur_placement
& caching
);
906 else if ((man
->default_caching
& caching
) != 0)
907 result
|= man
->default_caching
;
908 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
909 result
|= TTM_PL_FLAG_CACHED
;
910 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
911 result
|= TTM_PL_FLAG_WC
;
912 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
913 result
|= TTM_PL_FLAG_UNCACHED
;
918 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
920 uint32_t proposed_placement
,
921 uint32_t *masked_placement
)
923 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
925 if ((cur_flags
& proposed_placement
& TTM_PL_MASK_MEM
) == 0)
928 if ((proposed_placement
& man
->available_caching
) == 0)
931 cur_flags
|= (proposed_placement
& man
->available_caching
);
933 *masked_placement
= cur_flags
;
938 * Creates space for memory region @mem according to its type.
940 * This function first searches for free space in compatible memory types in
941 * the priority order defined by the driver. If free space isn't found, then
942 * ttm_bo_mem_force_space is attempted in priority order to evict and find
945 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
946 struct ttm_placement
*placement
,
947 struct ttm_mem_reg
*mem
,
951 struct ttm_bo_device
*bdev
= bo
->bdev
;
952 struct ttm_mem_type_manager
*man
;
953 uint32_t mem_type
= TTM_PL_SYSTEM
;
954 uint32_t cur_flags
= 0;
955 bool type_found
= false;
956 bool type_ok
= false;
957 bool has_erestartsys
= false;
961 for (i
= 0; i
< placement
->num_placement
; ++i
) {
962 ret
= ttm_mem_type_from_flags(placement
->placement
[i
],
966 man
= &bdev
->man
[mem_type
];
968 type_ok
= ttm_bo_mt_compatible(man
,
970 placement
->placement
[i
],
976 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
979 * Use the access and other non-mapping-related flag bits from
980 * the memory placement flags to the current flags
982 ttm_flag_masked(&cur_flags
, placement
->placement
[i
],
983 ~TTM_PL_MASK_MEMTYPE
);
985 if (mem_type
== TTM_PL_SYSTEM
)
988 if (man
->has_type
&& man
->use_type
) {
990 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
998 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
999 mem
->mem_type
= mem_type
;
1000 mem
->placement
= cur_flags
;
1007 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
1008 ret
= ttm_mem_type_from_flags(placement
->busy_placement
[i
],
1012 man
= &bdev
->man
[mem_type
];
1015 if (!ttm_bo_mt_compatible(man
,
1017 placement
->busy_placement
[i
],
1021 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
1024 * Use the access and other non-mapping-related flag bits from
1025 * the memory placement flags to the current flags
1027 ttm_flag_masked(&cur_flags
, placement
->busy_placement
[i
],
1028 ~TTM_PL_MASK_MEMTYPE
);
1031 if (mem_type
== TTM_PL_SYSTEM
) {
1032 mem
->mem_type
= mem_type
;
1033 mem
->placement
= cur_flags
;
1034 mem
->mm_node
= NULL
;
1038 ret
= ttm_bo_mem_force_space(bo
, mem_type
, placement
, mem
,
1039 interruptible
, no_wait_gpu
);
1040 if (ret
== 0 && mem
->mm_node
) {
1041 mem
->placement
= cur_flags
;
1044 if (ret
== -ERESTARTSYS
)
1045 has_erestartsys
= true;
1047 ret
= (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
1050 EXPORT_SYMBOL(ttm_bo_mem_space
);
1052 int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1053 struct ttm_placement
*placement
,
1058 struct ttm_mem_reg mem
;
1059 struct ttm_bo_device
*bdev
= bo
->bdev
;
1061 BUG_ON(!ttm_bo_is_reserved(bo
));
1064 * FIXME: It's possible to pipeline buffer moves.
1065 * Have the driver move function wait for idle when necessary,
1066 * instead of doing it here.
1068 spin_lock(&bdev
->fence_lock
);
1069 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
1070 spin_unlock(&bdev
->fence_lock
);
1073 mem
.num_pages
= bo
->num_pages
;
1074 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1075 mem
.page_alignment
= bo
->mem
.page_alignment
;
1076 mem
.bus
.io_reserved_vm
= false;
1077 mem
.bus
.io_reserved_count
= 0;
1079 * Determine where to move the buffer.
1081 ret
= ttm_bo_mem_space(bo
, placement
, &mem
,
1082 interruptible
, no_wait_gpu
);
1085 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false,
1086 interruptible
, no_wait_gpu
);
1088 if (ret
&& mem
.mm_node
)
1089 ttm_bo_mem_put(bo
, &mem
);
1093 static int ttm_bo_mem_compat(struct ttm_placement
*placement
,
1094 struct ttm_mem_reg
*mem
)
1098 if (mem
->mm_node
&& placement
->lpfn
!= 0 &&
1099 (mem
->start
< placement
->fpfn
||
1100 mem
->start
+ mem
->num_pages
> placement
->lpfn
))
1103 for (i
= 0; i
< placement
->num_placement
; i
++) {
1104 if ((placement
->placement
[i
] & mem
->placement
&
1105 TTM_PL_MASK_CACHING
) &&
1106 (placement
->placement
[i
] & mem
->placement
&
1113 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1114 struct ttm_placement
*placement
,
1120 BUG_ON(!ttm_bo_is_reserved(bo
));
1121 /* Check that range is valid */
1122 if (placement
->lpfn
|| placement
->fpfn
)
1123 if (placement
->fpfn
> placement
->lpfn
||
1124 (placement
->lpfn
- placement
->fpfn
) < bo
->num_pages
)
1127 * Check whether we need to move buffer.
1129 ret
= ttm_bo_mem_compat(placement
, &bo
->mem
);
1131 ret
= ttm_bo_move_buffer(bo
, placement
, interruptible
,
1137 * Use the access and other non-mapping-related flag bits from
1138 * the compatible memory placement flags to the active flags
1140 ttm_flag_masked(&bo
->mem
.placement
, placement
->placement
[ret
],
1141 ~TTM_PL_MASK_MEMTYPE
);
1144 * We might need to add a TTM.
1146 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1147 ret
= ttm_bo_add_ttm(bo
, true);
1153 EXPORT_SYMBOL(ttm_bo_validate
);
1155 int ttm_bo_check_placement(struct ttm_buffer_object
*bo
,
1156 struct ttm_placement
*placement
)
1158 BUG_ON((placement
->fpfn
|| placement
->lpfn
) &&
1159 (bo
->mem
.num_pages
> (placement
->lpfn
- placement
->fpfn
)));
1164 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1165 struct ttm_buffer_object
*bo
,
1167 enum ttm_bo_type type
,
1168 struct ttm_placement
*placement
,
1169 uint32_t page_alignment
,
1171 struct file
*persistent_swap_storage
,
1173 struct sg_table
*sg
,
1174 void (*destroy
) (struct ttm_buffer_object
*))
1177 unsigned long num_pages
;
1178 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1180 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1182 pr_err("Out of kernel memory\n");
1190 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1191 if (num_pages
== 0) {
1192 pr_err("Illegal buffer object size\n");
1197 ttm_mem_global_free(mem_glob
, acc_size
);
1200 bo
->destroy
= destroy
;
1202 kref_init(&bo
->kref
);
1203 kref_init(&bo
->list_kref
);
1204 atomic_set(&bo
->cpu_writers
, 0);
1205 atomic_set(&bo
->reserved
, 1);
1206 init_waitqueue_head(&bo
->event_queue
);
1207 INIT_LIST_HEAD(&bo
->lru
);
1208 INIT_LIST_HEAD(&bo
->ddestroy
);
1209 INIT_LIST_HEAD(&bo
->swap
);
1210 INIT_LIST_HEAD(&bo
->io_reserve_lru
);
1212 bo
->glob
= bdev
->glob
;
1214 bo
->num_pages
= num_pages
;
1215 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1216 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1217 bo
->mem
.num_pages
= bo
->num_pages
;
1218 bo
->mem
.mm_node
= NULL
;
1219 bo
->mem
.page_alignment
= page_alignment
;
1220 bo
->mem
.bus
.io_reserved_vm
= false;
1221 bo
->mem
.bus
.io_reserved_count
= 0;
1223 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1224 bo
->seq_valid
= false;
1225 bo
->persistent_swap_storage
= persistent_swap_storage
;
1226 bo
->acc_size
= acc_size
;
1228 atomic_inc(&bo
->glob
->bo_count
);
1230 ret
= ttm_bo_check_placement(bo
, placement
);
1231 if (unlikely(ret
!= 0))
1235 * For ttm_bo_type_device buffers, allocate
1236 * address space from the device.
1238 if (bo
->type
== ttm_bo_type_device
||
1239 bo
->type
== ttm_bo_type_sg
) {
1240 ret
= ttm_bo_setup_vm(bo
);
1245 ret
= ttm_bo_validate(bo
, placement
, interruptible
, false);
1249 ttm_bo_unreserve(bo
);
1253 ttm_bo_unreserve(bo
);
1258 EXPORT_SYMBOL(ttm_bo_init
);
1260 size_t ttm_bo_acc_size(struct ttm_bo_device
*bdev
,
1261 unsigned long bo_size
,
1262 unsigned struct_size
)
1264 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1267 size
+= ttm_round_pot(struct_size
);
1268 size
+= PAGE_ALIGN(npages
* sizeof(void *));
1269 size
+= ttm_round_pot(sizeof(struct ttm_tt
));
1272 EXPORT_SYMBOL(ttm_bo_acc_size
);
1274 size_t ttm_bo_dma_acc_size(struct ttm_bo_device
*bdev
,
1275 unsigned long bo_size
,
1276 unsigned struct_size
)
1278 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1281 size
+= ttm_round_pot(struct_size
);
1282 size
+= PAGE_ALIGN(npages
* sizeof(void *));
1283 size
+= PAGE_ALIGN(npages
* sizeof(dma_addr_t
));
1284 size
+= ttm_round_pot(sizeof(struct ttm_dma_tt
));
1287 EXPORT_SYMBOL(ttm_bo_dma_acc_size
);
1289 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1291 enum ttm_bo_type type
,
1292 struct ttm_placement
*placement
,
1293 uint32_t page_alignment
,
1295 struct file
*persistent_swap_storage
,
1296 struct ttm_buffer_object
**p_bo
)
1298 struct ttm_buffer_object
*bo
;
1302 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1303 if (unlikely(bo
== NULL
))
1306 acc_size
= ttm_bo_acc_size(bdev
, size
, sizeof(struct ttm_buffer_object
));
1307 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1308 interruptible
, persistent_swap_storage
, acc_size
,
1310 if (likely(ret
== 0))
1315 EXPORT_SYMBOL(ttm_bo_create
);
1317 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1318 unsigned mem_type
, bool allow_errors
)
1320 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1321 struct ttm_bo_global
*glob
= bdev
->glob
;
1325 * Can't use standard list traversal since we're unlocking.
1328 spin_lock(&glob
->lru_lock
);
1329 while (!list_empty(&man
->lru
)) {
1330 spin_unlock(&glob
->lru_lock
);
1331 ret
= ttm_mem_evict_first(bdev
, mem_type
, false, false);
1336 pr_err("Cleanup eviction failed\n");
1339 spin_lock(&glob
->lru_lock
);
1341 spin_unlock(&glob
->lru_lock
);
1345 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1347 struct ttm_mem_type_manager
*man
;
1350 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1351 pr_err("Illegal memory type %d\n", mem_type
);
1354 man
= &bdev
->man
[mem_type
];
1356 if (!man
->has_type
) {
1357 pr_err("Trying to take down uninitialized memory manager type %u\n",
1362 man
->use_type
= false;
1363 man
->has_type
= false;
1367 ttm_bo_force_list_clean(bdev
, mem_type
, false);
1369 ret
= (*man
->func
->takedown
)(man
);
1374 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1376 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1378 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1380 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1381 pr_err("Illegal memory manager memory type %u\n", mem_type
);
1385 if (!man
->has_type
) {
1386 pr_err("Memory type %u has not been initialized\n", mem_type
);
1390 return ttm_bo_force_list_clean(bdev
, mem_type
, true);
1392 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1394 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1395 unsigned long p_size
)
1398 struct ttm_mem_type_manager
*man
;
1400 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1401 man
= &bdev
->man
[type
];
1402 BUG_ON(man
->has_type
);
1403 man
->io_reserve_fastpath
= true;
1404 man
->use_io_reserve_lru
= false;
1405 mutex_init(&man
->io_reserve_mutex
);
1406 INIT_LIST_HEAD(&man
->io_reserve_lru
);
1408 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1414 if (type
!= TTM_PL_SYSTEM
) {
1415 ret
= (*man
->func
->init
)(man
, p_size
);
1419 man
->has_type
= true;
1420 man
->use_type
= true;
1423 INIT_LIST_HEAD(&man
->lru
);
1427 EXPORT_SYMBOL(ttm_bo_init_mm
);
1429 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1431 struct ttm_bo_global
*glob
=
1432 container_of(kobj
, struct ttm_bo_global
, kobj
);
1434 ttm_mem_unregister_shrink(glob
->mem_glob
, &glob
->shrink
);
1435 __free_page(glob
->dummy_read_page
);
1439 void ttm_bo_global_release(struct drm_global_reference
*ref
)
1441 struct ttm_bo_global
*glob
= ref
->object
;
1443 kobject_del(&glob
->kobj
);
1444 kobject_put(&glob
->kobj
);
1446 EXPORT_SYMBOL(ttm_bo_global_release
);
1448 int ttm_bo_global_init(struct drm_global_reference
*ref
)
1450 struct ttm_bo_global_ref
*bo_ref
=
1451 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1452 struct ttm_bo_global
*glob
= ref
->object
;
1455 mutex_init(&glob
->device_list_mutex
);
1456 spin_lock_init(&glob
->lru_lock
);
1457 glob
->mem_glob
= bo_ref
->mem_glob
;
1458 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1460 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1465 INIT_LIST_HEAD(&glob
->swap_lru
);
1466 INIT_LIST_HEAD(&glob
->device_list
);
1468 ttm_mem_init_shrink(&glob
->shrink
, ttm_bo_swapout
);
1469 ret
= ttm_mem_register_shrink(glob
->mem_glob
, &glob
->shrink
);
1470 if (unlikely(ret
!= 0)) {
1471 pr_err("Could not register buffer object swapout\n");
1475 atomic_set(&glob
->bo_count
, 0);
1477 ret
= kobject_init_and_add(
1478 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1479 if (unlikely(ret
!= 0))
1480 kobject_put(&glob
->kobj
);
1483 __free_page(glob
->dummy_read_page
);
1488 EXPORT_SYMBOL(ttm_bo_global_init
);
1491 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1494 unsigned i
= TTM_NUM_MEM_TYPES
;
1495 struct ttm_mem_type_manager
*man
;
1496 struct ttm_bo_global
*glob
= bdev
->glob
;
1499 man
= &bdev
->man
[i
];
1500 if (man
->has_type
) {
1501 man
->use_type
= false;
1502 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1504 pr_err("DRM memory manager type %d is not clean\n",
1507 man
->has_type
= false;
1511 mutex_lock(&glob
->device_list_mutex
);
1512 list_del(&bdev
->device_list
);
1513 mutex_unlock(&glob
->device_list_mutex
);
1515 cancel_delayed_work_sync(&bdev
->wq
);
1517 while (ttm_bo_delayed_delete(bdev
, true))
1520 spin_lock(&glob
->lru_lock
);
1521 if (list_empty(&bdev
->ddestroy
))
1522 TTM_DEBUG("Delayed destroy list was clean\n");
1524 if (list_empty(&bdev
->man
[0].lru
))
1525 TTM_DEBUG("Swap list was clean\n");
1526 spin_unlock(&glob
->lru_lock
);
1528 BUG_ON(!drm_mm_clean(&bdev
->addr_space_mm
));
1529 write_lock(&bdev
->vm_lock
);
1530 drm_mm_takedown(&bdev
->addr_space_mm
);
1531 write_unlock(&bdev
->vm_lock
);
1535 EXPORT_SYMBOL(ttm_bo_device_release
);
1537 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1538 struct ttm_bo_global
*glob
,
1539 struct ttm_bo_driver
*driver
,
1540 uint64_t file_page_offset
,
1545 rwlock_init(&bdev
->vm_lock
);
1546 bdev
->driver
= driver
;
1548 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1551 * Initialize the system memory buffer type.
1552 * Other types need to be driver / IOCTL initialized.
1554 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1555 if (unlikely(ret
!= 0))
1558 bdev
->addr_space_rb
= RB_ROOT
;
1559 ret
= drm_mm_init(&bdev
->addr_space_mm
, file_page_offset
, 0x10000000);
1560 if (unlikely(ret
!= 0))
1561 goto out_no_addr_mm
;
1563 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1564 INIT_LIST_HEAD(&bdev
->ddestroy
);
1565 bdev
->dev_mapping
= NULL
;
1567 bdev
->need_dma32
= need_dma32
;
1569 spin_lock_init(&bdev
->fence_lock
);
1570 mutex_lock(&glob
->device_list_mutex
);
1571 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1572 mutex_unlock(&glob
->device_list_mutex
);
1576 ttm_bo_clean_mm(bdev
, 0);
1580 EXPORT_SYMBOL(ttm_bo_device_init
);
1583 * buffer object vm functions.
1586 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1588 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1590 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1591 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1594 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1597 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1603 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object
*bo
)
1605 struct ttm_bo_device
*bdev
= bo
->bdev
;
1606 loff_t offset
= (loff_t
) bo
->addr_space_offset
;
1607 loff_t holelen
= ((loff_t
) bo
->mem
.num_pages
) << PAGE_SHIFT
;
1609 if (!bdev
->dev_mapping
)
1611 unmap_mapping_range(bdev
->dev_mapping
, offset
, holelen
, 1);
1612 ttm_mem_io_free_vm(bo
);
1615 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1617 struct ttm_bo_device
*bdev
= bo
->bdev
;
1618 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
1620 ttm_mem_io_lock(man
, false);
1621 ttm_bo_unmap_virtual_locked(bo
);
1622 ttm_mem_io_unlock(man
);
1626 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1628 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object
*bo
)
1630 struct ttm_bo_device
*bdev
= bo
->bdev
;
1631 struct rb_node
**cur
= &bdev
->addr_space_rb
.rb_node
;
1632 struct rb_node
*parent
= NULL
;
1633 struct ttm_buffer_object
*cur_bo
;
1634 unsigned long offset
= bo
->vm_node
->start
;
1635 unsigned long cur_offset
;
1639 cur_bo
= rb_entry(parent
, struct ttm_buffer_object
, vm_rb
);
1640 cur_offset
= cur_bo
->vm_node
->start
;
1641 if (offset
< cur_offset
)
1642 cur
= &parent
->rb_left
;
1643 else if (offset
> cur_offset
)
1644 cur
= &parent
->rb_right
;
1649 rb_link_node(&bo
->vm_rb
, parent
, cur
);
1650 rb_insert_color(&bo
->vm_rb
, &bdev
->addr_space_rb
);
1656 * @bo: the buffer to allocate address space for
1658 * Allocate address space in the drm device so that applications
1659 * can mmap the buffer and access the contents. This only
1660 * applies to ttm_bo_type_device objects as others are not
1661 * placed in the drm device address space.
1664 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
)
1666 struct ttm_bo_device
*bdev
= bo
->bdev
;
1670 ret
= drm_mm_pre_get(&bdev
->addr_space_mm
);
1671 if (unlikely(ret
!= 0))
1674 write_lock(&bdev
->vm_lock
);
1675 bo
->vm_node
= drm_mm_search_free(&bdev
->addr_space_mm
,
1676 bo
->mem
.num_pages
, 0, 0);
1678 if (unlikely(bo
->vm_node
== NULL
)) {
1683 bo
->vm_node
= drm_mm_get_block_atomic(bo
->vm_node
,
1684 bo
->mem
.num_pages
, 0);
1686 if (unlikely(bo
->vm_node
== NULL
)) {
1687 write_unlock(&bdev
->vm_lock
);
1691 ttm_bo_vm_insert_rb(bo
);
1692 write_unlock(&bdev
->vm_lock
);
1693 bo
->addr_space_offset
= ((uint64_t) bo
->vm_node
->start
) << PAGE_SHIFT
;
1697 write_unlock(&bdev
->vm_lock
);
1701 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1702 bool lazy
, bool interruptible
, bool no_wait
)
1704 struct ttm_bo_driver
*driver
= bo
->bdev
->driver
;
1705 struct ttm_bo_device
*bdev
= bo
->bdev
;
1709 if (likely(bo
->sync_obj
== NULL
))
1712 while (bo
->sync_obj
) {
1714 if (driver
->sync_obj_signaled(bo
->sync_obj
)) {
1715 void *tmp_obj
= bo
->sync_obj
;
1716 bo
->sync_obj
= NULL
;
1717 clear_bit(TTM_BO_PRIV_FLAG_MOVING
, &bo
->priv_flags
);
1718 spin_unlock(&bdev
->fence_lock
);
1719 driver
->sync_obj_unref(&tmp_obj
);
1720 spin_lock(&bdev
->fence_lock
);
1727 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
1728 spin_unlock(&bdev
->fence_lock
);
1729 ret
= driver
->sync_obj_wait(sync_obj
,
1730 lazy
, interruptible
);
1731 if (unlikely(ret
!= 0)) {
1732 driver
->sync_obj_unref(&sync_obj
);
1733 spin_lock(&bdev
->fence_lock
);
1736 spin_lock(&bdev
->fence_lock
);
1737 if (likely(bo
->sync_obj
== sync_obj
)) {
1738 void *tmp_obj
= bo
->sync_obj
;
1739 bo
->sync_obj
= NULL
;
1740 clear_bit(TTM_BO_PRIV_FLAG_MOVING
,
1742 spin_unlock(&bdev
->fence_lock
);
1743 driver
->sync_obj_unref(&sync_obj
);
1744 driver
->sync_obj_unref(&tmp_obj
);
1745 spin_lock(&bdev
->fence_lock
);
1747 spin_unlock(&bdev
->fence_lock
);
1748 driver
->sync_obj_unref(&sync_obj
);
1749 spin_lock(&bdev
->fence_lock
);
1754 EXPORT_SYMBOL(ttm_bo_wait
);
1756 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1758 struct ttm_bo_device
*bdev
= bo
->bdev
;
1762 * Using ttm_bo_reserve makes sure the lru lists are updated.
1765 ret
= ttm_bo_reserve(bo
, true, no_wait
, false, 0);
1766 if (unlikely(ret
!= 0))
1768 spin_lock(&bdev
->fence_lock
);
1769 ret
= ttm_bo_wait(bo
, false, true, no_wait
);
1770 spin_unlock(&bdev
->fence_lock
);
1771 if (likely(ret
== 0))
1772 atomic_inc(&bo
->cpu_writers
);
1773 ttm_bo_unreserve(bo
);
1776 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1778 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1780 atomic_dec(&bo
->cpu_writers
);
1782 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1785 * A buffer object shrink method that tries to swap out the first
1786 * buffer object on the bo_global::swap_lru list.
1789 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
)
1791 struct ttm_bo_global
*glob
=
1792 container_of(shrink
, struct ttm_bo_global
, shrink
);
1793 struct ttm_buffer_object
*bo
;
1796 uint32_t swap_placement
= (TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_SYSTEM
);
1798 spin_lock(&glob
->lru_lock
);
1799 list_for_each_entry(bo
, &glob
->swap_lru
, swap
) {
1800 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
1806 spin_unlock(&glob
->lru_lock
);
1810 kref_get(&bo
->list_kref
);
1812 if (!list_empty(&bo
->ddestroy
)) {
1813 ret
= ttm_bo_cleanup_refs_and_unlock(bo
, false, false);
1814 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1818 put_count
= ttm_bo_del_from_lru(bo
);
1819 spin_unlock(&glob
->lru_lock
);
1821 ttm_bo_list_ref_sub(bo
, put_count
, true);
1824 * Wait for GPU, then move to system cached.
1827 spin_lock(&bo
->bdev
->fence_lock
);
1828 ret
= ttm_bo_wait(bo
, false, false, false);
1829 spin_unlock(&bo
->bdev
->fence_lock
);
1831 if (unlikely(ret
!= 0))
1834 if ((bo
->mem
.placement
& swap_placement
) != swap_placement
) {
1835 struct ttm_mem_reg evict_mem
;
1837 evict_mem
= bo
->mem
;
1838 evict_mem
.mm_node
= NULL
;
1839 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1840 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1842 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true,
1844 if (unlikely(ret
!= 0))
1848 ttm_bo_unmap_virtual(bo
);
1851 * Swap out. Buffer will be swapped in again as soon as
1852 * anyone tries to access a ttm page.
1855 if (bo
->bdev
->driver
->swap_notify
)
1856 bo
->bdev
->driver
->swap_notify(bo
);
1858 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistent_swap_storage
);
1863 * Unreserve without putting on LRU to avoid swapping out an
1864 * already swapped buffer.
1867 atomic_set(&bo
->reserved
, 0);
1868 wake_up_all(&bo
->event_queue
);
1869 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1873 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1875 while (ttm_bo_swapout(&bdev
->glob
->shrink
) == 0)
1878 EXPORT_SYMBOL(ttm_bo_swapout_all
);