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 inline int ttm_mem_type_from_flags(uint32_t flags
, uint32_t *mem_type
)
58 for (i
= 0; i
<= TTM_PL_PRIV5
; i
++)
59 if (flags
& (1 << i
)) {
66 static void ttm_mem_type_debug(struct ttm_bo_device
*bdev
, int mem_type
)
68 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
70 printk(KERN_ERR TTM_PFX
" has_type: %d\n", man
->has_type
);
71 printk(KERN_ERR TTM_PFX
" use_type: %d\n", man
->use_type
);
72 printk(KERN_ERR TTM_PFX
" flags: 0x%08X\n", man
->flags
);
73 printk(KERN_ERR TTM_PFX
" gpu_offset: 0x%08lX\n", man
->gpu_offset
);
74 printk(KERN_ERR TTM_PFX
" size: %llu\n", man
->size
);
75 printk(KERN_ERR TTM_PFX
" available_caching: 0x%08X\n",
76 man
->available_caching
);
77 printk(KERN_ERR TTM_PFX
" default_caching: 0x%08X\n",
78 man
->default_caching
);
79 if (mem_type
!= TTM_PL_SYSTEM
)
80 (*man
->func
->debug
)(man
, TTM_PFX
);
83 static void ttm_bo_mem_space_debug(struct ttm_buffer_object
*bo
,
84 struct ttm_placement
*placement
)
88 printk(KERN_ERR TTM_PFX
"No space for %p (%lu pages, %luK, %luM)\n",
89 bo
, bo
->mem
.num_pages
, bo
->mem
.size
>> 10,
91 for (i
= 0; i
< placement
->num_placement
; i
++) {
92 ret
= ttm_mem_type_from_flags(placement
->placement
[i
],
96 printk(KERN_ERR TTM_PFX
" placement[%d]=0x%08X (%d)\n",
97 i
, placement
->placement
[i
], mem_type
);
98 ttm_mem_type_debug(bo
->bdev
, mem_type
);
102 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
103 struct attribute
*attr
,
106 struct ttm_bo_global
*glob
=
107 container_of(kobj
, struct ttm_bo_global
, kobj
);
109 return snprintf(buffer
, PAGE_SIZE
, "%lu\n",
110 (unsigned long) atomic_read(&glob
->bo_count
));
113 static struct attribute
*ttm_bo_global_attrs
[] = {
118 static const struct sysfs_ops ttm_bo_global_ops
= {
119 .show
= &ttm_bo_global_show
122 static struct kobj_type ttm_bo_glob_kobj_type
= {
123 .release
= &ttm_bo_global_kobj_release
,
124 .sysfs_ops
= &ttm_bo_global_ops
,
125 .default_attrs
= ttm_bo_global_attrs
129 static inline uint32_t ttm_bo_type_flags(unsigned type
)
134 static void ttm_bo_release_list(struct kref
*list_kref
)
136 struct ttm_buffer_object
*bo
=
137 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
138 struct ttm_bo_device
*bdev
= bo
->bdev
;
140 BUG_ON(atomic_read(&bo
->list_kref
.refcount
));
141 BUG_ON(atomic_read(&bo
->kref
.refcount
));
142 BUG_ON(atomic_read(&bo
->cpu_writers
));
143 BUG_ON(bo
->sync_obj
!= NULL
);
144 BUG_ON(bo
->mem
.mm_node
!= NULL
);
145 BUG_ON(!list_empty(&bo
->lru
));
146 BUG_ON(!list_empty(&bo
->ddestroy
));
149 ttm_tt_destroy(bo
->ttm
);
150 atomic_dec(&bo
->glob
->bo_count
);
154 ttm_mem_global_free(bdev
->glob
->mem_glob
, bo
->acc_size
);
159 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
, bool interruptible
)
162 return wait_event_interruptible(bo
->event_queue
,
163 atomic_read(&bo
->reserved
) == 0);
165 wait_event(bo
->event_queue
, atomic_read(&bo
->reserved
) == 0);
169 EXPORT_SYMBOL(ttm_bo_wait_unreserved
);
171 static void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
173 struct ttm_bo_device
*bdev
= bo
->bdev
;
174 struct ttm_mem_type_manager
*man
;
176 BUG_ON(!atomic_read(&bo
->reserved
));
178 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
180 BUG_ON(!list_empty(&bo
->lru
));
182 man
= &bdev
->man
[bo
->mem
.mem_type
];
183 list_add_tail(&bo
->lru
, &man
->lru
);
184 kref_get(&bo
->list_kref
);
186 if (bo
->ttm
!= NULL
) {
187 list_add_tail(&bo
->swap
, &bo
->glob
->swap_lru
);
188 kref_get(&bo
->list_kref
);
194 * Call with the lru_lock held.
197 static int ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
201 if (!list_empty(&bo
->swap
)) {
202 list_del_init(&bo
->swap
);
205 if (!list_empty(&bo
->lru
)) {
206 list_del_init(&bo
->lru
);
211 * TODO: Add a driver hook to delete from
212 * driver-specific LRU's here.
218 int ttm_bo_reserve_locked(struct ttm_buffer_object
*bo
,
220 bool no_wait
, bool use_sequence
, uint32_t sequence
)
222 struct ttm_bo_global
*glob
= bo
->glob
;
225 while (unlikely(atomic_cmpxchg(&bo
->reserved
, 0, 1) != 0)) {
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
);
243 bo
->val_seq
= sequence
;
244 bo
->seq_valid
= true;
246 bo
->seq_valid
= false;
251 EXPORT_SYMBOL(ttm_bo_reserve
);
253 static void ttm_bo_ref_bug(struct kref
*list_kref
)
258 int ttm_bo_reserve(struct ttm_buffer_object
*bo
,
260 bool no_wait
, bool use_sequence
, uint32_t sequence
)
262 struct ttm_bo_global
*glob
= bo
->glob
;
266 spin_lock(&glob
->lru_lock
);
267 ret
= ttm_bo_reserve_locked(bo
, interruptible
, no_wait
, use_sequence
,
269 if (likely(ret
== 0))
270 put_count
= ttm_bo_del_from_lru(bo
);
271 spin_unlock(&glob
->lru_lock
);
274 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
279 void ttm_bo_unreserve(struct ttm_buffer_object
*bo
)
281 struct ttm_bo_global
*glob
= bo
->glob
;
283 spin_lock(&glob
->lru_lock
);
284 ttm_bo_add_to_lru(bo
);
285 atomic_set(&bo
->reserved
, 0);
286 wake_up_all(&bo
->event_queue
);
287 spin_unlock(&glob
->lru_lock
);
289 EXPORT_SYMBOL(ttm_bo_unreserve
);
292 * Call bo->mutex locked.
294 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
296 struct ttm_bo_device
*bdev
= bo
->bdev
;
297 struct ttm_bo_global
*glob
= bo
->glob
;
299 uint32_t page_flags
= 0;
301 TTM_ASSERT_LOCKED(&bo
->mutex
);
304 if (bdev
->need_dma32
)
305 page_flags
|= TTM_PAGE_FLAG_DMA32
;
308 case ttm_bo_type_device
:
310 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
311 case ttm_bo_type_kernel
:
312 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
313 page_flags
, glob
->dummy_read_page
);
314 if (unlikely(bo
->ttm
== NULL
))
317 case ttm_bo_type_user
:
318 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
319 page_flags
| TTM_PAGE_FLAG_USER
,
320 glob
->dummy_read_page
);
321 if (unlikely(bo
->ttm
== NULL
)) {
326 ret
= ttm_tt_set_user(bo
->ttm
, current
,
327 bo
->buffer_start
, bo
->num_pages
);
328 if (unlikely(ret
!= 0))
329 ttm_tt_destroy(bo
->ttm
);
332 printk(KERN_ERR TTM_PFX
"Illegal buffer object type\n");
340 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
341 struct ttm_mem_reg
*mem
,
342 bool evict
, bool interruptible
,
343 bool no_wait_reserve
, bool no_wait_gpu
)
345 struct ttm_bo_device
*bdev
= bo
->bdev
;
346 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
347 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
348 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
349 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
352 if (old_is_pci
|| new_is_pci
||
353 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0))
354 ttm_bo_unmap_virtual(bo
);
357 * Create and bind a ttm if required.
360 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && (bo
->ttm
== NULL
)) {
361 ret
= ttm_bo_add_ttm(bo
, false);
365 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
369 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
370 ret
= ttm_tt_bind(bo
->ttm
, mem
);
375 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
383 if (bdev
->driver
->move_notify
)
384 bdev
->driver
->move_notify(bo
, mem
);
386 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
387 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
388 ret
= ttm_bo_move_ttm(bo
, evict
, no_wait_reserve
, no_wait_gpu
, mem
);
389 else if (bdev
->driver
->move
)
390 ret
= bdev
->driver
->move(bo
, evict
, interruptible
,
391 no_wait_reserve
, no_wait_gpu
, mem
);
393 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait_reserve
, no_wait_gpu
, mem
);
400 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
402 printk(KERN_ERR TTM_PFX
"Can not flush read caches\n");
406 if (bo
->mem
.mm_node
) {
407 spin_lock(&bo
->lock
);
408 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
409 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
410 bo
->cur_placement
= bo
->mem
.placement
;
411 spin_unlock(&bo
->lock
);
418 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
419 if ((new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
) {
420 ttm_tt_unbind(bo
->ttm
);
421 ttm_tt_destroy(bo
->ttm
);
430 * Will release GPU memory type usage on destruction.
431 * This is the place to put in driver specific hooks to release
432 * driver private resources.
433 * Will release the bo::reserved lock.
436 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
439 ttm_tt_unbind(bo
->ttm
);
440 ttm_tt_destroy(bo
->ttm
);
444 ttm_bo_mem_put(bo
, &bo
->mem
);
446 atomic_set(&bo
->reserved
, 0);
447 wake_up_all(&bo
->event_queue
);
450 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
452 struct ttm_bo_device
*bdev
= bo
->bdev
;
453 struct ttm_bo_global
*glob
= bo
->glob
;
454 struct ttm_bo_driver
*driver
;
460 spin_lock(&bo
->lock
);
461 (void) ttm_bo_wait(bo
, false, false, true);
464 spin_lock(&glob
->lru_lock
);
467 * Lock inversion between bo::reserve and bo::lock here,
468 * but that's OK, since we're only trylocking.
471 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
473 if (unlikely(ret
== -EBUSY
))
476 spin_unlock(&bo
->lock
);
477 put_count
= ttm_bo_del_from_lru(bo
);
479 spin_unlock(&glob
->lru_lock
);
480 ttm_bo_cleanup_memtype_use(bo
);
483 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
487 spin_lock(&glob
->lru_lock
);
490 sync_obj
= bo
->sync_obj
;
491 sync_obj_arg
= bo
->sync_obj_arg
;
492 driver
= bdev
->driver
;
494 kref_get(&bo
->list_kref
);
495 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
496 spin_unlock(&glob
->lru_lock
);
497 spin_unlock(&bo
->lock
);
500 driver
->sync_obj_flush(sync_obj
, sync_obj_arg
);
501 schedule_delayed_work(&bdev
->wq
,
502 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
506 * function ttm_bo_cleanup_refs
507 * If bo idle, remove from delayed- and lru lists, and unref.
508 * If not idle, do nothing.
510 * @interruptible Any sleeps should occur interruptibly.
511 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
512 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
515 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
517 bool no_wait_reserve
,
520 struct ttm_bo_global
*glob
= bo
->glob
;
525 spin_lock(&bo
->lock
);
526 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
527 spin_unlock(&bo
->lock
);
529 if (unlikely(ret
!= 0))
532 spin_lock(&glob
->lru_lock
);
533 ret
= ttm_bo_reserve_locked(bo
, interruptible
,
534 no_wait_reserve
, false, 0);
536 if (unlikely(ret
!= 0) || list_empty(&bo
->ddestroy
)) {
537 spin_unlock(&glob
->lru_lock
);
542 * We can re-check for sync object without taking
543 * the bo::lock since setting the sync object requires
544 * also bo::reserved. A busy object at this point may
545 * be caused by another thread recently starting an accelerated
549 if (unlikely(bo
->sync_obj
)) {
550 atomic_set(&bo
->reserved
, 0);
551 wake_up_all(&bo
->event_queue
);
552 spin_unlock(&glob
->lru_lock
);
556 put_count
= ttm_bo_del_from_lru(bo
);
557 list_del_init(&bo
->ddestroy
);
560 spin_unlock(&glob
->lru_lock
);
561 ttm_bo_cleanup_memtype_use(bo
);
564 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
570 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
571 * encountered buffers.
574 static int ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
576 struct ttm_bo_global
*glob
= bdev
->glob
;
577 struct ttm_buffer_object
*entry
= NULL
;
580 spin_lock(&glob
->lru_lock
);
581 if (list_empty(&bdev
->ddestroy
))
584 entry
= list_first_entry(&bdev
->ddestroy
,
585 struct ttm_buffer_object
, ddestroy
);
586 kref_get(&entry
->list_kref
);
589 struct ttm_buffer_object
*nentry
= NULL
;
591 if (entry
->ddestroy
.next
!= &bdev
->ddestroy
) {
592 nentry
= list_first_entry(&entry
->ddestroy
,
593 struct ttm_buffer_object
, ddestroy
);
594 kref_get(&nentry
->list_kref
);
597 spin_unlock(&glob
->lru_lock
);
598 ret
= ttm_bo_cleanup_refs(entry
, false, !remove_all
,
600 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
606 spin_lock(&glob
->lru_lock
);
607 if (list_empty(&entry
->ddestroy
))
612 spin_unlock(&glob
->lru_lock
);
615 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
619 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
621 struct ttm_bo_device
*bdev
=
622 container_of(work
, struct ttm_bo_device
, wq
.work
);
624 if (ttm_bo_delayed_delete(bdev
, false)) {
625 schedule_delayed_work(&bdev
->wq
,
626 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
630 static void ttm_bo_release(struct kref
*kref
)
632 struct ttm_buffer_object
*bo
=
633 container_of(kref
, struct ttm_buffer_object
, kref
);
634 struct ttm_bo_device
*bdev
= bo
->bdev
;
636 if (likely(bo
->vm_node
!= NULL
)) {
637 rb_erase(&bo
->vm_rb
, &bdev
->addr_space_rb
);
638 drm_mm_put_block(bo
->vm_node
);
641 write_unlock(&bdev
->vm_lock
);
642 ttm_bo_cleanup_refs_or_queue(bo
);
643 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
644 write_lock(&bdev
->vm_lock
);
647 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
649 struct ttm_buffer_object
*bo
= *p_bo
;
650 struct ttm_bo_device
*bdev
= bo
->bdev
;
653 write_lock(&bdev
->vm_lock
);
654 kref_put(&bo
->kref
, ttm_bo_release
);
655 write_unlock(&bdev
->vm_lock
);
657 EXPORT_SYMBOL(ttm_bo_unref
);
659 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
661 return cancel_delayed_work_sync(&bdev
->wq
);
663 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
665 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
668 schedule_delayed_work(&bdev
->wq
,
669 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
671 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
673 static int ttm_bo_evict(struct ttm_buffer_object
*bo
, bool interruptible
,
674 bool no_wait_reserve
, bool no_wait_gpu
)
676 struct ttm_bo_device
*bdev
= bo
->bdev
;
677 struct ttm_mem_reg evict_mem
;
678 struct ttm_placement placement
;
681 spin_lock(&bo
->lock
);
682 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
683 spin_unlock(&bo
->lock
);
685 if (unlikely(ret
!= 0)) {
686 if (ret
!= -ERESTARTSYS
) {
687 printk(KERN_ERR TTM_PFX
688 "Failed to expire sync object before "
689 "buffer eviction.\n");
694 BUG_ON(!atomic_read(&bo
->reserved
));
697 evict_mem
.mm_node
= NULL
;
698 evict_mem
.bus
.io_reserved
= false;
702 placement
.num_placement
= 0;
703 placement
.num_busy_placement
= 0;
704 bdev
->driver
->evict_flags(bo
, &placement
);
705 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, interruptible
,
706 no_wait_reserve
, no_wait_gpu
);
708 if (ret
!= -ERESTARTSYS
) {
709 printk(KERN_ERR TTM_PFX
710 "Failed to find memory space for "
711 "buffer 0x%p eviction.\n", bo
);
712 ttm_bo_mem_space_debug(bo
, &placement
);
717 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, interruptible
,
718 no_wait_reserve
, no_wait_gpu
);
720 if (ret
!= -ERESTARTSYS
)
721 printk(KERN_ERR TTM_PFX
"Buffer eviction failed\n");
722 ttm_bo_mem_put(bo
, &evict_mem
);
730 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
732 bool interruptible
, bool no_wait_reserve
,
735 struct ttm_bo_global
*glob
= bdev
->glob
;
736 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
737 struct ttm_buffer_object
*bo
;
738 int ret
, put_count
= 0;
741 spin_lock(&glob
->lru_lock
);
742 if (list_empty(&man
->lru
)) {
743 spin_unlock(&glob
->lru_lock
);
747 bo
= list_first_entry(&man
->lru
, struct ttm_buffer_object
, lru
);
748 kref_get(&bo
->list_kref
);
750 if (!list_empty(&bo
->ddestroy
)) {
751 spin_unlock(&glob
->lru_lock
);
752 ret
= ttm_bo_cleanup_refs(bo
, interruptible
,
753 no_wait_reserve
, no_wait_gpu
);
754 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
756 if (likely(ret
== 0 || ret
== -ERESTARTSYS
))
762 ret
= ttm_bo_reserve_locked(bo
, false, no_wait_reserve
, false, 0);
764 if (unlikely(ret
== -EBUSY
)) {
765 spin_unlock(&glob
->lru_lock
);
766 if (likely(!no_wait_gpu
))
767 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
769 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
772 * We *need* to retry after releasing the lru lock.
775 if (unlikely(ret
!= 0))
780 put_count
= ttm_bo_del_from_lru(bo
);
781 spin_unlock(&glob
->lru_lock
);
786 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
788 ret
= ttm_bo_evict(bo
, interruptible
, no_wait_reserve
, no_wait_gpu
);
789 ttm_bo_unreserve(bo
);
791 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
795 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
797 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
800 (*man
->func
->put_node
)(man
, mem
);
802 EXPORT_SYMBOL(ttm_bo_mem_put
);
805 * Repeatedly evict memory from the LRU for @mem_type until we create enough
806 * space, or we've evicted everything and there isn't enough space.
808 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
810 struct ttm_placement
*placement
,
811 struct ttm_mem_reg
*mem
,
813 bool no_wait_reserve
,
816 struct ttm_bo_device
*bdev
= bo
->bdev
;
817 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
821 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
822 if (unlikely(ret
!= 0))
826 ret
= ttm_mem_evict_first(bdev
, mem_type
, interruptible
,
827 no_wait_reserve
, no_wait_gpu
);
828 if (unlikely(ret
!= 0))
831 if (mem
->mm_node
== NULL
)
833 mem
->mem_type
= mem_type
;
837 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
838 uint32_t cur_placement
,
839 uint32_t proposed_placement
)
841 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
842 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
845 * Keep current caching if possible.
848 if ((cur_placement
& caching
) != 0)
849 result
|= (cur_placement
& caching
);
850 else if ((man
->default_caching
& caching
) != 0)
851 result
|= man
->default_caching
;
852 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
853 result
|= TTM_PL_FLAG_CACHED
;
854 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
855 result
|= TTM_PL_FLAG_WC
;
856 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
857 result
|= TTM_PL_FLAG_UNCACHED
;
862 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
865 uint32_t proposed_placement
,
866 uint32_t *masked_placement
)
868 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
870 if ((man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && disallow_fixed
)
873 if ((cur_flags
& proposed_placement
& TTM_PL_MASK_MEM
) == 0)
876 if ((proposed_placement
& man
->available_caching
) == 0)
879 cur_flags
|= (proposed_placement
& man
->available_caching
);
881 *masked_placement
= cur_flags
;
886 * Creates space for memory region @mem according to its type.
888 * This function first searches for free space in compatible memory types in
889 * the priority order defined by the driver. If free space isn't found, then
890 * ttm_bo_mem_force_space is attempted in priority order to evict and find
893 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
894 struct ttm_placement
*placement
,
895 struct ttm_mem_reg
*mem
,
896 bool interruptible
, bool no_wait_reserve
,
899 struct ttm_bo_device
*bdev
= bo
->bdev
;
900 struct ttm_mem_type_manager
*man
;
901 uint32_t mem_type
= TTM_PL_SYSTEM
;
902 uint32_t cur_flags
= 0;
903 bool type_found
= false;
904 bool type_ok
= false;
905 bool has_erestartsys
= false;
909 for (i
= 0; i
< placement
->num_placement
; ++i
) {
910 ret
= ttm_mem_type_from_flags(placement
->placement
[i
],
914 man
= &bdev
->man
[mem_type
];
916 type_ok
= ttm_bo_mt_compatible(man
,
917 bo
->type
== ttm_bo_type_user
,
919 placement
->placement
[i
],
925 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
928 * Use the access and other non-mapping-related flag bits from
929 * the memory placement flags to the current flags
931 ttm_flag_masked(&cur_flags
, placement
->placement
[i
],
932 ~TTM_PL_MASK_MEMTYPE
);
934 if (mem_type
== TTM_PL_SYSTEM
)
937 if (man
->has_type
&& man
->use_type
) {
939 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
947 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
948 mem
->mem_type
= mem_type
;
949 mem
->placement
= cur_flags
;
956 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
957 ret
= ttm_mem_type_from_flags(placement
->busy_placement
[i
],
961 man
= &bdev
->man
[mem_type
];
964 if (!ttm_bo_mt_compatible(man
,
965 bo
->type
== ttm_bo_type_user
,
967 placement
->busy_placement
[i
],
971 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
974 * Use the access and other non-mapping-related flag bits from
975 * the memory placement flags to the current flags
977 ttm_flag_masked(&cur_flags
, placement
->busy_placement
[i
],
978 ~TTM_PL_MASK_MEMTYPE
);
981 if (mem_type
== TTM_PL_SYSTEM
) {
982 mem
->mem_type
= mem_type
;
983 mem
->placement
= cur_flags
;
988 ret
= ttm_bo_mem_force_space(bo
, mem_type
, placement
, mem
,
989 interruptible
, no_wait_reserve
, no_wait_gpu
);
990 if (ret
== 0 && mem
->mm_node
) {
991 mem
->placement
= cur_flags
;
994 if (ret
== -ERESTARTSYS
)
995 has_erestartsys
= true;
997 ret
= (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
1000 EXPORT_SYMBOL(ttm_bo_mem_space
);
1002 int ttm_bo_wait_cpu(struct ttm_buffer_object
*bo
, bool no_wait
)
1004 if ((atomic_read(&bo
->cpu_writers
) > 0) && no_wait
)
1007 return wait_event_interruptible(bo
->event_queue
,
1008 atomic_read(&bo
->cpu_writers
) == 0);
1010 EXPORT_SYMBOL(ttm_bo_wait_cpu
);
1012 int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1013 struct ttm_placement
*placement
,
1014 bool interruptible
, bool no_wait_reserve
,
1018 struct ttm_mem_reg mem
;
1020 BUG_ON(!atomic_read(&bo
->reserved
));
1023 * FIXME: It's possible to pipeline buffer moves.
1024 * Have the driver move function wait for idle when necessary,
1025 * instead of doing it here.
1027 spin_lock(&bo
->lock
);
1028 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
1029 spin_unlock(&bo
->lock
);
1032 mem
.num_pages
= bo
->num_pages
;
1033 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1034 mem
.page_alignment
= bo
->mem
.page_alignment
;
1035 mem
.bus
.io_reserved
= false;
1037 * Determine where to move the buffer.
1039 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, interruptible
, no_wait_reserve
, no_wait_gpu
);
1042 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, interruptible
, no_wait_reserve
, no_wait_gpu
);
1044 if (ret
&& mem
.mm_node
)
1045 ttm_bo_mem_put(bo
, &mem
);
1049 static int ttm_bo_mem_compat(struct ttm_placement
*placement
,
1050 struct ttm_mem_reg
*mem
)
1054 if (mem
->mm_node
&& placement
->lpfn
!= 0 &&
1055 (mem
->start
< placement
->fpfn
||
1056 mem
->start
+ mem
->num_pages
> placement
->lpfn
))
1059 for (i
= 0; i
< placement
->num_placement
; i
++) {
1060 if ((placement
->placement
[i
] & mem
->placement
&
1061 TTM_PL_MASK_CACHING
) &&
1062 (placement
->placement
[i
] & mem
->placement
&
1069 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1070 struct ttm_placement
*placement
,
1071 bool interruptible
, bool no_wait_reserve
,
1076 BUG_ON(!atomic_read(&bo
->reserved
));
1077 /* Check that range is valid */
1078 if (placement
->lpfn
|| placement
->fpfn
)
1079 if (placement
->fpfn
> placement
->lpfn
||
1080 (placement
->lpfn
- placement
->fpfn
) < bo
->num_pages
)
1083 * Check whether we need to move buffer.
1085 ret
= ttm_bo_mem_compat(placement
, &bo
->mem
);
1087 ret
= ttm_bo_move_buffer(bo
, placement
, interruptible
, no_wait_reserve
, no_wait_gpu
);
1092 * Use the access and other non-mapping-related flag bits from
1093 * the compatible memory placement flags to the active flags
1095 ttm_flag_masked(&bo
->mem
.placement
, placement
->placement
[ret
],
1096 ~TTM_PL_MASK_MEMTYPE
);
1099 * We might need to add a TTM.
1101 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1102 ret
= ttm_bo_add_ttm(bo
, true);
1108 EXPORT_SYMBOL(ttm_bo_validate
);
1110 int ttm_bo_check_placement(struct ttm_buffer_object
*bo
,
1111 struct ttm_placement
*placement
)
1115 if (placement
->fpfn
|| placement
->lpfn
) {
1116 if (bo
->mem
.num_pages
> (placement
->lpfn
- placement
->fpfn
)) {
1117 printk(KERN_ERR TTM_PFX
"Page number range to small "
1118 "Need %lu pages, range is [%u, %u]\n",
1119 bo
->mem
.num_pages
, placement
->fpfn
,
1124 for (i
= 0; i
< placement
->num_placement
; i
++) {
1125 if (!capable(CAP_SYS_ADMIN
)) {
1126 if (placement
->placement
[i
] & TTM_PL_FLAG_NO_EVICT
) {
1127 printk(KERN_ERR TTM_PFX
"Need to be root to "
1128 "modify NO_EVICT status.\n");
1133 for (i
= 0; i
< placement
->num_busy_placement
; i
++) {
1134 if (!capable(CAP_SYS_ADMIN
)) {
1135 if (placement
->busy_placement
[i
] & TTM_PL_FLAG_NO_EVICT
) {
1136 printk(KERN_ERR TTM_PFX
"Need to be root to "
1137 "modify NO_EVICT status.\n");
1145 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1146 struct ttm_buffer_object
*bo
,
1148 enum ttm_bo_type type
,
1149 struct ttm_placement
*placement
,
1150 uint32_t page_alignment
,
1151 unsigned long buffer_start
,
1153 struct file
*persistant_swap_storage
,
1155 void (*destroy
) (struct ttm_buffer_object
*))
1158 unsigned long num_pages
;
1160 size
+= buffer_start
& ~PAGE_MASK
;
1161 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1162 if (num_pages
== 0) {
1163 printk(KERN_ERR TTM_PFX
"Illegal buffer object size.\n");
1166 bo
->destroy
= destroy
;
1168 spin_lock_init(&bo
->lock
);
1169 kref_init(&bo
->kref
);
1170 kref_init(&bo
->list_kref
);
1171 atomic_set(&bo
->cpu_writers
, 0);
1172 atomic_set(&bo
->reserved
, 1);
1173 init_waitqueue_head(&bo
->event_queue
);
1174 INIT_LIST_HEAD(&bo
->lru
);
1175 INIT_LIST_HEAD(&bo
->ddestroy
);
1176 INIT_LIST_HEAD(&bo
->swap
);
1178 bo
->glob
= bdev
->glob
;
1180 bo
->num_pages
= num_pages
;
1181 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1182 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1183 bo
->mem
.num_pages
= bo
->num_pages
;
1184 bo
->mem
.mm_node
= NULL
;
1185 bo
->mem
.page_alignment
= page_alignment
;
1186 bo
->mem
.bus
.io_reserved
= false;
1187 bo
->buffer_start
= buffer_start
& PAGE_MASK
;
1189 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1190 bo
->seq_valid
= false;
1191 bo
->persistant_swap_storage
= persistant_swap_storage
;
1192 bo
->acc_size
= acc_size
;
1193 atomic_inc(&bo
->glob
->bo_count
);
1195 ret
= ttm_bo_check_placement(bo
, placement
);
1196 if (unlikely(ret
!= 0))
1200 * For ttm_bo_type_device buffers, allocate
1201 * address space from the device.
1203 if (bo
->type
== ttm_bo_type_device
) {
1204 ret
= ttm_bo_setup_vm(bo
);
1209 ret
= ttm_bo_validate(bo
, placement
, interruptible
, false, false);
1213 ttm_bo_unreserve(bo
);
1217 ttm_bo_unreserve(bo
);
1222 EXPORT_SYMBOL(ttm_bo_init
);
1224 static inline size_t ttm_bo_size(struct ttm_bo_global
*glob
,
1225 unsigned long num_pages
)
1227 size_t page_array_size
= (num_pages
* sizeof(void *) + PAGE_SIZE
- 1) &
1230 return glob
->ttm_bo_size
+ 2 * page_array_size
;
1233 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1235 enum ttm_bo_type type
,
1236 struct ttm_placement
*placement
,
1237 uint32_t page_alignment
,
1238 unsigned long buffer_start
,
1240 struct file
*persistant_swap_storage
,
1241 struct ttm_buffer_object
**p_bo
)
1243 struct ttm_buffer_object
*bo
;
1244 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1248 ttm_bo_size(bdev
->glob
, (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
);
1249 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1250 if (unlikely(ret
!= 0))
1253 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1255 if (unlikely(bo
== NULL
)) {
1256 ttm_mem_global_free(mem_glob
, acc_size
);
1260 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1261 buffer_start
, interruptible
,
1262 persistant_swap_storage
, acc_size
, NULL
);
1263 if (likely(ret
== 0))
1269 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1270 unsigned mem_type
, bool allow_errors
)
1272 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1273 struct ttm_bo_global
*glob
= bdev
->glob
;
1277 * Can't use standard list traversal since we're unlocking.
1280 spin_lock(&glob
->lru_lock
);
1281 while (!list_empty(&man
->lru
)) {
1282 spin_unlock(&glob
->lru_lock
);
1283 ret
= ttm_mem_evict_first(bdev
, mem_type
, false, false, false);
1288 printk(KERN_ERR TTM_PFX
1289 "Cleanup eviction failed\n");
1292 spin_lock(&glob
->lru_lock
);
1294 spin_unlock(&glob
->lru_lock
);
1298 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1300 struct ttm_mem_type_manager
*man
;
1303 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1304 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", mem_type
);
1307 man
= &bdev
->man
[mem_type
];
1309 if (!man
->has_type
) {
1310 printk(KERN_ERR TTM_PFX
"Trying to take down uninitialized "
1311 "memory manager type %u\n", mem_type
);
1315 man
->use_type
= false;
1316 man
->has_type
= false;
1320 ttm_bo_force_list_clean(bdev
, mem_type
, false);
1322 ret
= (*man
->func
->takedown
)(man
);
1327 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1329 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1331 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1333 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1334 printk(KERN_ERR TTM_PFX
1335 "Illegal memory manager memory type %u.\n",
1340 if (!man
->has_type
) {
1341 printk(KERN_ERR TTM_PFX
1342 "Memory type %u has not been initialized.\n",
1347 return ttm_bo_force_list_clean(bdev
, mem_type
, true);
1349 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1351 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1352 unsigned long p_size
)
1355 struct ttm_mem_type_manager
*man
;
1357 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1358 man
= &bdev
->man
[type
];
1359 BUG_ON(man
->has_type
);
1361 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1367 if (type
!= TTM_PL_SYSTEM
) {
1368 ret
= (*man
->func
->init
)(man
, p_size
);
1372 man
->has_type
= true;
1373 man
->use_type
= true;
1376 INIT_LIST_HEAD(&man
->lru
);
1380 EXPORT_SYMBOL(ttm_bo_init_mm
);
1382 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1384 struct ttm_bo_global
*glob
=
1385 container_of(kobj
, struct ttm_bo_global
, kobj
);
1387 ttm_mem_unregister_shrink(glob
->mem_glob
, &glob
->shrink
);
1388 __free_page(glob
->dummy_read_page
);
1392 void ttm_bo_global_release(struct drm_global_reference
*ref
)
1394 struct ttm_bo_global
*glob
= ref
->object
;
1396 kobject_del(&glob
->kobj
);
1397 kobject_put(&glob
->kobj
);
1399 EXPORT_SYMBOL(ttm_bo_global_release
);
1401 int ttm_bo_global_init(struct drm_global_reference
*ref
)
1403 struct ttm_bo_global_ref
*bo_ref
=
1404 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1405 struct ttm_bo_global
*glob
= ref
->object
;
1408 mutex_init(&glob
->device_list_mutex
);
1409 spin_lock_init(&glob
->lru_lock
);
1410 glob
->mem_glob
= bo_ref
->mem_glob
;
1411 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1413 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1418 INIT_LIST_HEAD(&glob
->swap_lru
);
1419 INIT_LIST_HEAD(&glob
->device_list
);
1421 ttm_mem_init_shrink(&glob
->shrink
, ttm_bo_swapout
);
1422 ret
= ttm_mem_register_shrink(glob
->mem_glob
, &glob
->shrink
);
1423 if (unlikely(ret
!= 0)) {
1424 printk(KERN_ERR TTM_PFX
1425 "Could not register buffer object swapout.\n");
1429 glob
->ttm_bo_extra_size
=
1430 ttm_round_pot(sizeof(struct ttm_tt
)) +
1431 ttm_round_pot(sizeof(struct ttm_backend
));
1433 glob
->ttm_bo_size
= glob
->ttm_bo_extra_size
+
1434 ttm_round_pot(sizeof(struct ttm_buffer_object
));
1436 atomic_set(&glob
->bo_count
, 0);
1438 ret
= kobject_init_and_add(
1439 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1440 if (unlikely(ret
!= 0))
1441 kobject_put(&glob
->kobj
);
1444 __free_page(glob
->dummy_read_page
);
1449 EXPORT_SYMBOL(ttm_bo_global_init
);
1452 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1455 unsigned i
= TTM_NUM_MEM_TYPES
;
1456 struct ttm_mem_type_manager
*man
;
1457 struct ttm_bo_global
*glob
= bdev
->glob
;
1460 man
= &bdev
->man
[i
];
1461 if (man
->has_type
) {
1462 man
->use_type
= false;
1463 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1465 printk(KERN_ERR TTM_PFX
1466 "DRM memory manager type %d "
1467 "is not clean.\n", i
);
1469 man
->has_type
= false;
1473 mutex_lock(&glob
->device_list_mutex
);
1474 list_del(&bdev
->device_list
);
1475 mutex_unlock(&glob
->device_list_mutex
);
1477 if (!cancel_delayed_work(&bdev
->wq
))
1478 flush_scheduled_work();
1480 while (ttm_bo_delayed_delete(bdev
, true))
1483 spin_lock(&glob
->lru_lock
);
1484 if (list_empty(&bdev
->ddestroy
))
1485 TTM_DEBUG("Delayed destroy list was clean\n");
1487 if (list_empty(&bdev
->man
[0].lru
))
1488 TTM_DEBUG("Swap list was clean\n");
1489 spin_unlock(&glob
->lru_lock
);
1491 BUG_ON(!drm_mm_clean(&bdev
->addr_space_mm
));
1492 write_lock(&bdev
->vm_lock
);
1493 drm_mm_takedown(&bdev
->addr_space_mm
);
1494 write_unlock(&bdev
->vm_lock
);
1498 EXPORT_SYMBOL(ttm_bo_device_release
);
1500 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1501 struct ttm_bo_global
*glob
,
1502 struct ttm_bo_driver
*driver
,
1503 uint64_t file_page_offset
,
1508 rwlock_init(&bdev
->vm_lock
);
1509 bdev
->driver
= driver
;
1511 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1514 * Initialize the system memory buffer type.
1515 * Other types need to be driver / IOCTL initialized.
1517 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1518 if (unlikely(ret
!= 0))
1521 bdev
->addr_space_rb
= RB_ROOT
;
1522 ret
= drm_mm_init(&bdev
->addr_space_mm
, file_page_offset
, 0x10000000);
1523 if (unlikely(ret
!= 0))
1524 goto out_no_addr_mm
;
1526 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1527 bdev
->nice_mode
= true;
1528 INIT_LIST_HEAD(&bdev
->ddestroy
);
1529 bdev
->dev_mapping
= NULL
;
1531 bdev
->need_dma32
= need_dma32
;
1533 mutex_lock(&glob
->device_list_mutex
);
1534 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1535 mutex_unlock(&glob
->device_list_mutex
);
1539 ttm_bo_clean_mm(bdev
, 0);
1543 EXPORT_SYMBOL(ttm_bo_device_init
);
1546 * buffer object vm functions.
1549 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1551 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1553 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1554 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1557 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1560 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1566 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1568 struct ttm_bo_device
*bdev
= bo
->bdev
;
1569 loff_t offset
= (loff_t
) bo
->addr_space_offset
;
1570 loff_t holelen
= ((loff_t
) bo
->mem
.num_pages
) << PAGE_SHIFT
;
1572 if (!bdev
->dev_mapping
)
1574 unmap_mapping_range(bdev
->dev_mapping
, offset
, holelen
, 1);
1575 ttm_mem_io_free(bdev
, &bo
->mem
);
1577 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1579 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object
*bo
)
1581 struct ttm_bo_device
*bdev
= bo
->bdev
;
1582 struct rb_node
**cur
= &bdev
->addr_space_rb
.rb_node
;
1583 struct rb_node
*parent
= NULL
;
1584 struct ttm_buffer_object
*cur_bo
;
1585 unsigned long offset
= bo
->vm_node
->start
;
1586 unsigned long cur_offset
;
1590 cur_bo
= rb_entry(parent
, struct ttm_buffer_object
, vm_rb
);
1591 cur_offset
= cur_bo
->vm_node
->start
;
1592 if (offset
< cur_offset
)
1593 cur
= &parent
->rb_left
;
1594 else if (offset
> cur_offset
)
1595 cur
= &parent
->rb_right
;
1600 rb_link_node(&bo
->vm_rb
, parent
, cur
);
1601 rb_insert_color(&bo
->vm_rb
, &bdev
->addr_space_rb
);
1607 * @bo: the buffer to allocate address space for
1609 * Allocate address space in the drm device so that applications
1610 * can mmap the buffer and access the contents. This only
1611 * applies to ttm_bo_type_device objects as others are not
1612 * placed in the drm device address space.
1615 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
)
1617 struct ttm_bo_device
*bdev
= bo
->bdev
;
1621 ret
= drm_mm_pre_get(&bdev
->addr_space_mm
);
1622 if (unlikely(ret
!= 0))
1625 write_lock(&bdev
->vm_lock
);
1626 bo
->vm_node
= drm_mm_search_free(&bdev
->addr_space_mm
,
1627 bo
->mem
.num_pages
, 0, 0);
1629 if (unlikely(bo
->vm_node
== NULL
)) {
1634 bo
->vm_node
= drm_mm_get_block_atomic(bo
->vm_node
,
1635 bo
->mem
.num_pages
, 0);
1637 if (unlikely(bo
->vm_node
== NULL
)) {
1638 write_unlock(&bdev
->vm_lock
);
1642 ttm_bo_vm_insert_rb(bo
);
1643 write_unlock(&bdev
->vm_lock
);
1644 bo
->addr_space_offset
= ((uint64_t) bo
->vm_node
->start
) << PAGE_SHIFT
;
1648 write_unlock(&bdev
->vm_lock
);
1652 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1653 bool lazy
, bool interruptible
, bool no_wait
)
1655 struct ttm_bo_driver
*driver
= bo
->bdev
->driver
;
1660 if (likely(bo
->sync_obj
== NULL
))
1663 while (bo
->sync_obj
) {
1665 if (driver
->sync_obj_signaled(bo
->sync_obj
, bo
->sync_obj_arg
)) {
1666 void *tmp_obj
= bo
->sync_obj
;
1667 bo
->sync_obj
= NULL
;
1668 clear_bit(TTM_BO_PRIV_FLAG_MOVING
, &bo
->priv_flags
);
1669 spin_unlock(&bo
->lock
);
1670 driver
->sync_obj_unref(&tmp_obj
);
1671 spin_lock(&bo
->lock
);
1678 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
1679 sync_obj_arg
= bo
->sync_obj_arg
;
1680 spin_unlock(&bo
->lock
);
1681 ret
= driver
->sync_obj_wait(sync_obj
, sync_obj_arg
,
1682 lazy
, interruptible
);
1683 if (unlikely(ret
!= 0)) {
1684 driver
->sync_obj_unref(&sync_obj
);
1685 spin_lock(&bo
->lock
);
1688 spin_lock(&bo
->lock
);
1689 if (likely(bo
->sync_obj
== sync_obj
&&
1690 bo
->sync_obj_arg
== sync_obj_arg
)) {
1691 void *tmp_obj
= bo
->sync_obj
;
1692 bo
->sync_obj
= NULL
;
1693 clear_bit(TTM_BO_PRIV_FLAG_MOVING
,
1695 spin_unlock(&bo
->lock
);
1696 driver
->sync_obj_unref(&sync_obj
);
1697 driver
->sync_obj_unref(&tmp_obj
);
1698 spin_lock(&bo
->lock
);
1700 spin_unlock(&bo
->lock
);
1701 driver
->sync_obj_unref(&sync_obj
);
1702 spin_lock(&bo
->lock
);
1707 EXPORT_SYMBOL(ttm_bo_wait
);
1709 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1714 * Using ttm_bo_reserve makes sure the lru lists are updated.
1717 ret
= ttm_bo_reserve(bo
, true, no_wait
, false, 0);
1718 if (unlikely(ret
!= 0))
1720 spin_lock(&bo
->lock
);
1721 ret
= ttm_bo_wait(bo
, false, true, no_wait
);
1722 spin_unlock(&bo
->lock
);
1723 if (likely(ret
== 0))
1724 atomic_inc(&bo
->cpu_writers
);
1725 ttm_bo_unreserve(bo
);
1728 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1730 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1732 if (atomic_dec_and_test(&bo
->cpu_writers
))
1733 wake_up_all(&bo
->event_queue
);
1735 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1738 * A buffer object shrink method that tries to swap out the first
1739 * buffer object on the bo_global::swap_lru list.
1742 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
)
1744 struct ttm_bo_global
*glob
=
1745 container_of(shrink
, struct ttm_bo_global
, shrink
);
1746 struct ttm_buffer_object
*bo
;
1749 uint32_t swap_placement
= (TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_SYSTEM
);
1751 spin_lock(&glob
->lru_lock
);
1752 while (ret
== -EBUSY
) {
1753 if (unlikely(list_empty(&glob
->swap_lru
))) {
1754 spin_unlock(&glob
->lru_lock
);
1758 bo
= list_first_entry(&glob
->swap_lru
,
1759 struct ttm_buffer_object
, swap
);
1760 kref_get(&bo
->list_kref
);
1762 if (!list_empty(&bo
->ddestroy
)) {
1763 spin_unlock(&glob
->lru_lock
);
1764 (void) ttm_bo_cleanup_refs(bo
, false, false, false);
1765 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1770 * Reserve buffer. Since we unlock while sleeping, we need
1771 * to re-check that nobody removed us from the swap-list while
1775 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
1776 if (unlikely(ret
== -EBUSY
)) {
1777 spin_unlock(&glob
->lru_lock
);
1778 ttm_bo_wait_unreserved(bo
, false);
1779 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1780 spin_lock(&glob
->lru_lock
);
1785 put_count
= ttm_bo_del_from_lru(bo
);
1786 spin_unlock(&glob
->lru_lock
);
1789 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
1792 * Wait for GPU, then move to system cached.
1795 spin_lock(&bo
->lock
);
1796 ret
= ttm_bo_wait(bo
, false, false, false);
1797 spin_unlock(&bo
->lock
);
1799 if (unlikely(ret
!= 0))
1802 if ((bo
->mem
.placement
& swap_placement
) != swap_placement
) {
1803 struct ttm_mem_reg evict_mem
;
1805 evict_mem
= bo
->mem
;
1806 evict_mem
.mm_node
= NULL
;
1807 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1808 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1810 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true,
1811 false, false, false);
1812 if (unlikely(ret
!= 0))
1816 ttm_bo_unmap_virtual(bo
);
1819 * Swap out. Buffer will be swapped in again as soon as
1820 * anyone tries to access a ttm page.
1823 if (bo
->bdev
->driver
->swap_notify
)
1824 bo
->bdev
->driver
->swap_notify(bo
);
1826 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistant_swap_storage
);
1831 * Unreserve without putting on LRU to avoid swapping out an
1832 * already swapped buffer.
1835 atomic_set(&bo
->reserved
, 0);
1836 wake_up_all(&bo
->event_queue
);
1837 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1841 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1843 while (ttm_bo_swapout(&bdev
->glob
->shrink
) == 0)
1846 EXPORT_SYMBOL(ttm_bo_swapout_all
);