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>
32 * We store bo pointer in drm_mm_node struct so we know which bo own a
33 * specific node. There is no protection on the pointer, thus to make
34 * sure things don't go berserk you have to access this pointer while
35 * holding the global lru lock and make sure anytime you free a node you
36 * reset the pointer to NULL.
39 #include "ttm/ttm_module.h"
40 #include "ttm/ttm_bo_driver.h"
41 #include "ttm/ttm_placement.h"
42 #include <linux/jiffies.h>
43 #include <linux/slab.h>
44 #include <linux/sched.h>
46 #include <linux/file.h>
47 #include <linux/module.h>
49 #define TTM_ASSERT_LOCKED(param)
50 #define TTM_DEBUG(fmt, arg...)
51 #define TTM_BO_HASH_ORDER 13
53 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
);
54 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
);
55 static void ttm_bo_global_kobj_release(struct kobject
*kobj
);
57 static struct attribute ttm_bo_count
= {
62 static inline int ttm_mem_type_from_flags(uint32_t flags
, uint32_t *mem_type
)
66 for (i
= 0; i
<= TTM_PL_PRIV5
; i
++)
67 if (flags
& (1 << i
)) {
74 static void ttm_mem_type_debug(struct ttm_bo_device
*bdev
, int mem_type
)
76 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
78 printk(KERN_ERR TTM_PFX
" has_type: %d\n", man
->has_type
);
79 printk(KERN_ERR TTM_PFX
" use_type: %d\n", man
->use_type
);
80 printk(KERN_ERR TTM_PFX
" flags: 0x%08X\n", man
->flags
);
81 printk(KERN_ERR TTM_PFX
" gpu_offset: 0x%08lX\n", man
->gpu_offset
);
82 printk(KERN_ERR TTM_PFX
" size: %llu\n", man
->size
);
83 printk(KERN_ERR TTM_PFX
" available_caching: 0x%08X\n",
84 man
->available_caching
);
85 printk(KERN_ERR TTM_PFX
" default_caching: 0x%08X\n",
86 man
->default_caching
);
87 if (mem_type
!= TTM_PL_SYSTEM
)
88 (*man
->func
->debug
)(man
, TTM_PFX
);
91 static void ttm_bo_mem_space_debug(struct ttm_buffer_object
*bo
,
92 struct ttm_placement
*placement
)
96 printk(KERN_ERR TTM_PFX
"No space for %p (%lu pages, %luK, %luM)\n",
97 bo
, bo
->mem
.num_pages
, bo
->mem
.size
>> 10,
99 for (i
= 0; i
< placement
->num_placement
; i
++) {
100 ret
= ttm_mem_type_from_flags(placement
->placement
[i
],
104 printk(KERN_ERR TTM_PFX
" placement[%d]=0x%08X (%d)\n",
105 i
, placement
->placement
[i
], mem_type
);
106 ttm_mem_type_debug(bo
->bdev
, mem_type
);
110 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
111 struct attribute
*attr
,
114 struct ttm_bo_global
*glob
=
115 container_of(kobj
, struct ttm_bo_global
, kobj
);
117 return snprintf(buffer
, PAGE_SIZE
, "%lu\n",
118 (unsigned long) atomic_read(&glob
->bo_count
));
121 static struct attribute
*ttm_bo_global_attrs
[] = {
126 static const struct sysfs_ops ttm_bo_global_ops
= {
127 .show
= &ttm_bo_global_show
130 static struct kobj_type ttm_bo_glob_kobj_type
= {
131 .release
= &ttm_bo_global_kobj_release
,
132 .sysfs_ops
= &ttm_bo_global_ops
,
133 .default_attrs
= ttm_bo_global_attrs
137 static inline uint32_t ttm_bo_type_flags(unsigned type
)
142 static void ttm_bo_release_list(struct kref
*list_kref
)
144 struct ttm_buffer_object
*bo
=
145 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
146 struct ttm_bo_device
*bdev
= bo
->bdev
;
148 BUG_ON(atomic_read(&bo
->list_kref
.refcount
));
149 BUG_ON(atomic_read(&bo
->kref
.refcount
));
150 BUG_ON(atomic_read(&bo
->cpu_writers
));
151 BUG_ON(bo
->sync_obj
!= NULL
);
152 BUG_ON(bo
->mem
.mm_node
!= NULL
);
153 BUG_ON(!list_empty(&bo
->lru
));
154 BUG_ON(!list_empty(&bo
->ddestroy
));
157 ttm_tt_destroy(bo
->ttm
);
158 atomic_dec(&bo
->glob
->bo_count
);
162 ttm_mem_global_free(bdev
->glob
->mem_glob
, bo
->acc_size
);
167 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
, bool interruptible
)
170 return wait_event_interruptible(bo
->event_queue
,
171 atomic_read(&bo
->reserved
) == 0);
173 wait_event(bo
->event_queue
, atomic_read(&bo
->reserved
) == 0);
177 EXPORT_SYMBOL(ttm_bo_wait_unreserved
);
179 static void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
181 struct ttm_bo_device
*bdev
= bo
->bdev
;
182 struct ttm_mem_type_manager
*man
;
184 BUG_ON(!atomic_read(&bo
->reserved
));
186 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
188 BUG_ON(!list_empty(&bo
->lru
));
190 man
= &bdev
->man
[bo
->mem
.mem_type
];
191 list_add_tail(&bo
->lru
, &man
->lru
);
192 kref_get(&bo
->list_kref
);
194 if (bo
->ttm
!= NULL
) {
195 list_add_tail(&bo
->swap
, &bo
->glob
->swap_lru
);
196 kref_get(&bo
->list_kref
);
202 * Call with the lru_lock held.
205 static int ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
209 if (!list_empty(&bo
->swap
)) {
210 list_del_init(&bo
->swap
);
213 if (!list_empty(&bo
->lru
)) {
214 list_del_init(&bo
->lru
);
219 * TODO: Add a driver hook to delete from
220 * driver-specific LRU's here.
226 int ttm_bo_reserve_locked(struct ttm_buffer_object
*bo
,
228 bool no_wait
, bool use_sequence
, uint32_t sequence
)
230 struct ttm_bo_global
*glob
= bo
->glob
;
233 while (unlikely(atomic_cmpxchg(&bo
->reserved
, 0, 1) != 0)) {
234 if (use_sequence
&& bo
->seq_valid
&&
235 (sequence
- bo
->val_seq
< (1 << 31))) {
242 spin_unlock(&glob
->lru_lock
);
243 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
244 spin_lock(&glob
->lru_lock
);
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 int ttm_bo_reserve(struct ttm_buffer_object
*bo
,
268 bool no_wait
, bool use_sequence
, uint32_t sequence
)
270 struct ttm_bo_global
*glob
= bo
->glob
;
274 spin_lock(&glob
->lru_lock
);
275 ret
= ttm_bo_reserve_locked(bo
, interruptible
, no_wait
, use_sequence
,
277 if (likely(ret
== 0))
278 put_count
= ttm_bo_del_from_lru(bo
);
279 spin_unlock(&glob
->lru_lock
);
282 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
287 void ttm_bo_unreserve(struct ttm_buffer_object
*bo
)
289 struct ttm_bo_global
*glob
= bo
->glob
;
291 spin_lock(&glob
->lru_lock
);
292 ttm_bo_add_to_lru(bo
);
293 atomic_set(&bo
->reserved
, 0);
294 wake_up_all(&bo
->event_queue
);
295 spin_unlock(&glob
->lru_lock
);
297 EXPORT_SYMBOL(ttm_bo_unreserve
);
300 * Call bo->mutex locked.
302 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
304 struct ttm_bo_device
*bdev
= bo
->bdev
;
305 struct ttm_bo_global
*glob
= bo
->glob
;
307 uint32_t page_flags
= 0;
309 TTM_ASSERT_LOCKED(&bo
->mutex
);
312 if (bdev
->need_dma32
)
313 page_flags
|= TTM_PAGE_FLAG_DMA32
;
316 case ttm_bo_type_device
:
318 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
319 case ttm_bo_type_kernel
:
320 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
321 page_flags
, glob
->dummy_read_page
);
322 if (unlikely(bo
->ttm
== NULL
))
325 case ttm_bo_type_user
:
326 bo
->ttm
= ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
327 page_flags
| TTM_PAGE_FLAG_USER
,
328 glob
->dummy_read_page
);
329 if (unlikely(bo
->ttm
== NULL
)) {
334 ret
= ttm_tt_set_user(bo
->ttm
, current
,
335 bo
->buffer_start
, bo
->num_pages
);
336 if (unlikely(ret
!= 0))
337 ttm_tt_destroy(bo
->ttm
);
340 printk(KERN_ERR TTM_PFX
"Illegal buffer object type\n");
348 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
349 struct ttm_mem_reg
*mem
,
350 bool evict
, bool interruptible
,
351 bool no_wait_reserve
, bool no_wait_gpu
)
353 struct ttm_bo_device
*bdev
= bo
->bdev
;
354 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
355 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
356 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
357 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
360 if (old_is_pci
|| new_is_pci
||
361 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0))
362 ttm_bo_unmap_virtual(bo
);
365 * Create and bind a ttm if required.
368 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && (bo
->ttm
== NULL
)) {
369 ret
= ttm_bo_add_ttm(bo
, false);
373 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
377 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
378 ret
= ttm_tt_bind(bo
->ttm
, mem
);
383 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
391 if (bdev
->driver
->move_notify
)
392 bdev
->driver
->move_notify(bo
, mem
);
394 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
395 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
396 ret
= ttm_bo_move_ttm(bo
, evict
, no_wait_reserve
, no_wait_gpu
, mem
);
397 else if (bdev
->driver
->move
)
398 ret
= bdev
->driver
->move(bo
, evict
, interruptible
,
399 no_wait_reserve
, no_wait_gpu
, mem
);
401 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait_reserve
, no_wait_gpu
, mem
);
408 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
410 printk(KERN_ERR TTM_PFX
"Can not flush read caches\n");
414 if (bo
->mem
.mm_node
) {
415 spin_lock(&bo
->lock
);
416 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
417 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
418 bo
->cur_placement
= bo
->mem
.placement
;
419 spin_unlock(&bo
->lock
);
426 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
427 if ((new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
) {
428 ttm_tt_unbind(bo
->ttm
);
429 ttm_tt_destroy(bo
->ttm
);
438 * Will release GPU memory type usage on destruction.
439 * This is the place to put in driver specific hooks to release
440 * driver private resources.
441 * Will release the bo::reserved lock.
444 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
447 ttm_tt_unbind(bo
->ttm
);
448 ttm_tt_destroy(bo
->ttm
);
452 ttm_bo_mem_put(bo
, &bo
->mem
);
454 atomic_set(&bo
->reserved
, 0);
455 wake_up_all(&bo
->event_queue
);
458 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
460 struct ttm_bo_device
*bdev
= bo
->bdev
;
461 struct ttm_bo_global
*glob
= bo
->glob
;
462 struct ttm_bo_driver
*driver
;
468 spin_lock(&bo
->lock
);
469 (void) ttm_bo_wait(bo
, false, false, true);
472 spin_lock(&glob
->lru_lock
);
475 * Lock inversion between bo::reserve and bo::lock here,
476 * but that's OK, since we're only trylocking.
479 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
481 if (unlikely(ret
== -EBUSY
))
484 spin_unlock(&bo
->lock
);
485 put_count
= ttm_bo_del_from_lru(bo
);
487 spin_unlock(&glob
->lru_lock
);
488 ttm_bo_cleanup_memtype_use(bo
);
491 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
495 spin_lock(&glob
->lru_lock
);
498 sync_obj
= bo
->sync_obj
;
499 sync_obj_arg
= bo
->sync_obj_arg
;
500 driver
= bdev
->driver
;
502 kref_get(&bo
->list_kref
);
503 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
504 spin_unlock(&glob
->lru_lock
);
505 spin_unlock(&bo
->lock
);
508 driver
->sync_obj_flush(sync_obj
, sync_obj_arg
);
509 schedule_delayed_work(&bdev
->wq
,
510 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
514 * function ttm_bo_cleanup_refs
515 * If bo idle, remove from delayed- and lru lists, and unref.
516 * If not idle, do nothing.
518 * @interruptible Any sleeps should occur interruptibly.
519 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
520 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
523 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
525 bool no_wait_reserve
,
528 struct ttm_bo_global
*glob
= bo
->glob
;
533 spin_lock(&bo
->lock
);
534 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
535 spin_unlock(&bo
->lock
);
537 if (unlikely(ret
!= 0))
540 spin_lock(&glob
->lru_lock
);
541 ret
= ttm_bo_reserve_locked(bo
, interruptible
,
542 no_wait_reserve
, false, 0);
544 if (unlikely(ret
!= 0) || list_empty(&bo
->ddestroy
)) {
545 spin_unlock(&glob
->lru_lock
);
550 * We can re-check for sync object without taking
551 * the bo::lock since setting the sync object requires
552 * also bo::reserved. A busy object at this point may
553 * be caused by another thread recently starting an accelerated
557 if (unlikely(bo
->sync_obj
)) {
558 atomic_set(&bo
->reserved
, 0);
559 wake_up_all(&bo
->event_queue
);
560 spin_unlock(&glob
->lru_lock
);
564 put_count
= ttm_bo_del_from_lru(bo
);
565 list_del_init(&bo
->ddestroy
);
568 spin_unlock(&glob
->lru_lock
);
569 ttm_bo_cleanup_memtype_use(bo
);
572 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
578 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
579 * encountered buffers.
582 static int ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
584 struct ttm_bo_global
*glob
= bdev
->glob
;
585 struct ttm_buffer_object
*entry
= NULL
;
588 spin_lock(&glob
->lru_lock
);
589 if (list_empty(&bdev
->ddestroy
))
592 entry
= list_first_entry(&bdev
->ddestroy
,
593 struct ttm_buffer_object
, ddestroy
);
594 kref_get(&entry
->list_kref
);
597 struct ttm_buffer_object
*nentry
= NULL
;
599 if (entry
->ddestroy
.next
!= &bdev
->ddestroy
) {
600 nentry
= list_first_entry(&entry
->ddestroy
,
601 struct ttm_buffer_object
, ddestroy
);
602 kref_get(&nentry
->list_kref
);
605 spin_unlock(&glob
->lru_lock
);
606 ret
= ttm_bo_cleanup_refs(entry
, false, !remove_all
,
608 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
614 spin_lock(&glob
->lru_lock
);
615 if (list_empty(&entry
->ddestroy
))
620 spin_unlock(&glob
->lru_lock
);
623 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
627 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
629 struct ttm_bo_device
*bdev
=
630 container_of(work
, struct ttm_bo_device
, wq
.work
);
632 if (ttm_bo_delayed_delete(bdev
, false)) {
633 schedule_delayed_work(&bdev
->wq
,
634 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
638 static void ttm_bo_release(struct kref
*kref
)
640 struct ttm_buffer_object
*bo
=
641 container_of(kref
, struct ttm_buffer_object
, kref
);
642 struct ttm_bo_device
*bdev
= bo
->bdev
;
644 if (likely(bo
->vm_node
!= NULL
)) {
645 rb_erase(&bo
->vm_rb
, &bdev
->addr_space_rb
);
646 drm_mm_put_block(bo
->vm_node
);
649 write_unlock(&bdev
->vm_lock
);
650 ttm_bo_cleanup_refs_or_queue(bo
);
651 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
652 write_lock(&bdev
->vm_lock
);
655 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
657 struct ttm_buffer_object
*bo
= *p_bo
;
658 struct ttm_bo_device
*bdev
= bo
->bdev
;
661 write_lock(&bdev
->vm_lock
);
662 kref_put(&bo
->kref
, ttm_bo_release
);
663 write_unlock(&bdev
->vm_lock
);
665 EXPORT_SYMBOL(ttm_bo_unref
);
667 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
669 return cancel_delayed_work_sync(&bdev
->wq
);
671 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
673 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
676 schedule_delayed_work(&bdev
->wq
,
677 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
679 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
681 static int ttm_bo_evict(struct ttm_buffer_object
*bo
, bool interruptible
,
682 bool no_wait_reserve
, bool no_wait_gpu
)
684 struct ttm_bo_device
*bdev
= bo
->bdev
;
685 struct ttm_mem_reg evict_mem
;
686 struct ttm_placement placement
;
689 spin_lock(&bo
->lock
);
690 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
691 spin_unlock(&bo
->lock
);
693 if (unlikely(ret
!= 0)) {
694 if (ret
!= -ERESTARTSYS
) {
695 printk(KERN_ERR TTM_PFX
696 "Failed to expire sync object before "
697 "buffer eviction.\n");
702 BUG_ON(!atomic_read(&bo
->reserved
));
705 evict_mem
.mm_node
= NULL
;
706 evict_mem
.bus
.io_reserved
= false;
710 placement
.num_placement
= 0;
711 placement
.num_busy_placement
= 0;
712 bdev
->driver
->evict_flags(bo
, &placement
);
713 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, interruptible
,
714 no_wait_reserve
, no_wait_gpu
);
716 if (ret
!= -ERESTARTSYS
) {
717 printk(KERN_ERR TTM_PFX
718 "Failed to find memory space for "
719 "buffer 0x%p eviction.\n", bo
);
720 ttm_bo_mem_space_debug(bo
, &placement
);
725 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, interruptible
,
726 no_wait_reserve
, no_wait_gpu
);
728 if (ret
!= -ERESTARTSYS
)
729 printk(KERN_ERR TTM_PFX
"Buffer eviction failed\n");
730 ttm_bo_mem_put(bo
, &evict_mem
);
738 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
740 bool interruptible
, bool no_wait_reserve
,
743 struct ttm_bo_global
*glob
= bdev
->glob
;
744 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
745 struct ttm_buffer_object
*bo
;
746 int ret
, put_count
= 0;
749 spin_lock(&glob
->lru_lock
);
750 if (list_empty(&man
->lru
)) {
751 spin_unlock(&glob
->lru_lock
);
755 bo
= list_first_entry(&man
->lru
, struct ttm_buffer_object
, lru
);
756 kref_get(&bo
->list_kref
);
758 if (!list_empty(&bo
->ddestroy
)) {
759 spin_unlock(&glob
->lru_lock
);
760 ret
= ttm_bo_cleanup_refs(bo
, interruptible
,
761 no_wait_reserve
, no_wait_gpu
);
762 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
764 if (likely(ret
== 0 || ret
== -ERESTARTSYS
))
770 ret
= ttm_bo_reserve_locked(bo
, false, no_wait_reserve
, false, 0);
772 if (unlikely(ret
== -EBUSY
)) {
773 spin_unlock(&glob
->lru_lock
);
774 if (likely(!no_wait_gpu
))
775 ret
= ttm_bo_wait_unreserved(bo
, interruptible
);
777 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
780 * We *need* to retry after releasing the lru lock.
783 if (unlikely(ret
!= 0))
788 put_count
= ttm_bo_del_from_lru(bo
);
789 spin_unlock(&glob
->lru_lock
);
794 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
796 ret
= ttm_bo_evict(bo
, interruptible
, no_wait_reserve
, no_wait_gpu
);
797 ttm_bo_unreserve(bo
);
799 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
803 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
805 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
808 (*man
->func
->put_node
)(man
, mem
);
810 EXPORT_SYMBOL(ttm_bo_mem_put
);
813 * Repeatedly evict memory from the LRU for @mem_type until we create enough
814 * space, or we've evicted everything and there isn't enough space.
816 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
818 struct ttm_placement
*placement
,
819 struct ttm_mem_reg
*mem
,
821 bool no_wait_reserve
,
824 struct ttm_bo_device
*bdev
= bo
->bdev
;
825 struct ttm_bo_global
*glob
= bdev
->glob
;
826 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
830 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
831 if (unlikely(ret
!= 0))
835 spin_lock(&glob
->lru_lock
);
836 if (list_empty(&man
->lru
)) {
837 spin_unlock(&glob
->lru_lock
);
840 spin_unlock(&glob
->lru_lock
);
841 ret
= ttm_mem_evict_first(bdev
, mem_type
, interruptible
,
842 no_wait_reserve
, no_wait_gpu
);
843 if (unlikely(ret
!= 0))
846 if (mem
->mm_node
== NULL
)
848 mem
->mem_type
= mem_type
;
852 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
853 uint32_t cur_placement
,
854 uint32_t proposed_placement
)
856 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
857 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
860 * Keep current caching if possible.
863 if ((cur_placement
& caching
) != 0)
864 result
|= (cur_placement
& caching
);
865 else if ((man
->default_caching
& caching
) != 0)
866 result
|= man
->default_caching
;
867 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
868 result
|= TTM_PL_FLAG_CACHED
;
869 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
870 result
|= TTM_PL_FLAG_WC
;
871 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
872 result
|= TTM_PL_FLAG_UNCACHED
;
877 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
880 uint32_t proposed_placement
,
881 uint32_t *masked_placement
)
883 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
885 if ((man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && disallow_fixed
)
888 if ((cur_flags
& proposed_placement
& TTM_PL_MASK_MEM
) == 0)
891 if ((proposed_placement
& man
->available_caching
) == 0)
894 cur_flags
|= (proposed_placement
& man
->available_caching
);
896 *masked_placement
= cur_flags
;
901 * Creates space for memory region @mem according to its type.
903 * This function first searches for free space in compatible memory types in
904 * the priority order defined by the driver. If free space isn't found, then
905 * ttm_bo_mem_force_space is attempted in priority order to evict and find
908 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
909 struct ttm_placement
*placement
,
910 struct ttm_mem_reg
*mem
,
911 bool interruptible
, bool no_wait_reserve
,
914 struct ttm_bo_device
*bdev
= bo
->bdev
;
915 struct ttm_mem_type_manager
*man
;
916 uint32_t mem_type
= TTM_PL_SYSTEM
;
917 uint32_t cur_flags
= 0;
918 bool type_found
= false;
919 bool type_ok
= false;
920 bool has_erestartsys
= false;
924 for (i
= 0; i
< placement
->num_placement
; ++i
) {
925 ret
= ttm_mem_type_from_flags(placement
->placement
[i
],
929 man
= &bdev
->man
[mem_type
];
931 type_ok
= ttm_bo_mt_compatible(man
,
932 bo
->type
== ttm_bo_type_user
,
934 placement
->placement
[i
],
940 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
943 * Use the access and other non-mapping-related flag bits from
944 * the memory placement flags to the current flags
946 ttm_flag_masked(&cur_flags
, placement
->placement
[i
],
947 ~TTM_PL_MASK_MEMTYPE
);
949 if (mem_type
== TTM_PL_SYSTEM
)
952 if (man
->has_type
&& man
->use_type
) {
954 ret
= (*man
->func
->get_node
)(man
, bo
, placement
, mem
);
962 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
963 mem
->mem_type
= mem_type
;
964 mem
->placement
= cur_flags
;
971 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
972 ret
= ttm_mem_type_from_flags(placement
->busy_placement
[i
],
976 man
= &bdev
->man
[mem_type
];
979 if (!ttm_bo_mt_compatible(man
,
980 bo
->type
== ttm_bo_type_user
,
982 placement
->busy_placement
[i
],
986 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
989 * Use the access and other non-mapping-related flag bits from
990 * the memory placement flags to the current flags
992 ttm_flag_masked(&cur_flags
, placement
->busy_placement
[i
],
993 ~TTM_PL_MASK_MEMTYPE
);
996 if (mem_type
== TTM_PL_SYSTEM
) {
997 mem
->mem_type
= mem_type
;
998 mem
->placement
= cur_flags
;
1003 ret
= ttm_bo_mem_force_space(bo
, mem_type
, placement
, mem
,
1004 interruptible
, no_wait_reserve
, no_wait_gpu
);
1005 if (ret
== 0 && mem
->mm_node
) {
1006 mem
->placement
= cur_flags
;
1009 if (ret
== -ERESTARTSYS
)
1010 has_erestartsys
= true;
1012 ret
= (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
1015 EXPORT_SYMBOL(ttm_bo_mem_space
);
1017 int ttm_bo_wait_cpu(struct ttm_buffer_object
*bo
, bool no_wait
)
1019 if ((atomic_read(&bo
->cpu_writers
) > 0) && no_wait
)
1022 return wait_event_interruptible(bo
->event_queue
,
1023 atomic_read(&bo
->cpu_writers
) == 0);
1025 EXPORT_SYMBOL(ttm_bo_wait_cpu
);
1027 int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1028 struct ttm_placement
*placement
,
1029 bool interruptible
, bool no_wait_reserve
,
1033 struct ttm_mem_reg mem
;
1035 BUG_ON(!atomic_read(&bo
->reserved
));
1038 * FIXME: It's possible to pipeline buffer moves.
1039 * Have the driver move function wait for idle when necessary,
1040 * instead of doing it here.
1042 spin_lock(&bo
->lock
);
1043 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
1044 spin_unlock(&bo
->lock
);
1047 mem
.num_pages
= bo
->num_pages
;
1048 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1049 mem
.page_alignment
= bo
->mem
.page_alignment
;
1050 mem
.bus
.io_reserved
= false;
1052 * Determine where to move the buffer.
1054 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, interruptible
, no_wait_reserve
, no_wait_gpu
);
1057 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, interruptible
, no_wait_reserve
, no_wait_gpu
);
1059 if (ret
&& mem
.mm_node
)
1060 ttm_bo_mem_put(bo
, &mem
);
1064 static int ttm_bo_mem_compat(struct ttm_placement
*placement
,
1065 struct ttm_mem_reg
*mem
)
1069 if (mem
->mm_node
&& placement
->lpfn
!= 0 &&
1070 (mem
->start
< placement
->fpfn
||
1071 mem
->start
+ mem
->num_pages
> placement
->lpfn
))
1074 for (i
= 0; i
< placement
->num_placement
; i
++) {
1075 if ((placement
->placement
[i
] & mem
->placement
&
1076 TTM_PL_MASK_CACHING
) &&
1077 (placement
->placement
[i
] & mem
->placement
&
1084 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1085 struct ttm_placement
*placement
,
1086 bool interruptible
, bool no_wait_reserve
,
1091 BUG_ON(!atomic_read(&bo
->reserved
));
1092 /* Check that range is valid */
1093 if (placement
->lpfn
|| placement
->fpfn
)
1094 if (placement
->fpfn
> placement
->lpfn
||
1095 (placement
->lpfn
- placement
->fpfn
) < bo
->num_pages
)
1098 * Check whether we need to move buffer.
1100 ret
= ttm_bo_mem_compat(placement
, &bo
->mem
);
1102 ret
= ttm_bo_move_buffer(bo
, placement
, interruptible
, no_wait_reserve
, no_wait_gpu
);
1107 * Use the access and other non-mapping-related flag bits from
1108 * the compatible memory placement flags to the active flags
1110 ttm_flag_masked(&bo
->mem
.placement
, placement
->placement
[ret
],
1111 ~TTM_PL_MASK_MEMTYPE
);
1114 * We might need to add a TTM.
1116 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1117 ret
= ttm_bo_add_ttm(bo
, true);
1123 EXPORT_SYMBOL(ttm_bo_validate
);
1125 int ttm_bo_check_placement(struct ttm_buffer_object
*bo
,
1126 struct ttm_placement
*placement
)
1130 if (placement
->fpfn
|| placement
->lpfn
) {
1131 if (bo
->mem
.num_pages
> (placement
->lpfn
- placement
->fpfn
)) {
1132 printk(KERN_ERR TTM_PFX
"Page number range to small "
1133 "Need %lu pages, range is [%u, %u]\n",
1134 bo
->mem
.num_pages
, placement
->fpfn
,
1139 for (i
= 0; i
< placement
->num_placement
; i
++) {
1140 if (!capable(CAP_SYS_ADMIN
)) {
1141 if (placement
->placement
[i
] & TTM_PL_FLAG_NO_EVICT
) {
1142 printk(KERN_ERR TTM_PFX
"Need to be root to "
1143 "modify NO_EVICT status.\n");
1148 for (i
= 0; i
< placement
->num_busy_placement
; i
++) {
1149 if (!capable(CAP_SYS_ADMIN
)) {
1150 if (placement
->busy_placement
[i
] & TTM_PL_FLAG_NO_EVICT
) {
1151 printk(KERN_ERR TTM_PFX
"Need to be root to "
1152 "modify NO_EVICT status.\n");
1160 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1161 struct ttm_buffer_object
*bo
,
1163 enum ttm_bo_type type
,
1164 struct ttm_placement
*placement
,
1165 uint32_t page_alignment
,
1166 unsigned long buffer_start
,
1168 struct file
*persistant_swap_storage
,
1170 void (*destroy
) (struct ttm_buffer_object
*))
1173 unsigned long num_pages
;
1175 size
+= buffer_start
& ~PAGE_MASK
;
1176 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1177 if (num_pages
== 0) {
1178 printk(KERN_ERR TTM_PFX
"Illegal buffer object size.\n");
1181 bo
->destroy
= destroy
;
1183 spin_lock_init(&bo
->lock
);
1184 kref_init(&bo
->kref
);
1185 kref_init(&bo
->list_kref
);
1186 atomic_set(&bo
->cpu_writers
, 0);
1187 atomic_set(&bo
->reserved
, 1);
1188 init_waitqueue_head(&bo
->event_queue
);
1189 INIT_LIST_HEAD(&bo
->lru
);
1190 INIT_LIST_HEAD(&bo
->ddestroy
);
1191 INIT_LIST_HEAD(&bo
->swap
);
1193 bo
->glob
= bdev
->glob
;
1195 bo
->num_pages
= num_pages
;
1196 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1197 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1198 bo
->mem
.num_pages
= bo
->num_pages
;
1199 bo
->mem
.mm_node
= NULL
;
1200 bo
->mem
.page_alignment
= page_alignment
;
1201 bo
->mem
.bus
.io_reserved
= false;
1202 bo
->buffer_start
= buffer_start
& PAGE_MASK
;
1204 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1205 bo
->seq_valid
= false;
1206 bo
->persistant_swap_storage
= persistant_swap_storage
;
1207 bo
->acc_size
= acc_size
;
1208 atomic_inc(&bo
->glob
->bo_count
);
1210 ret
= ttm_bo_check_placement(bo
, placement
);
1211 if (unlikely(ret
!= 0))
1215 * For ttm_bo_type_device buffers, allocate
1216 * address space from the device.
1218 if (bo
->type
== ttm_bo_type_device
) {
1219 ret
= ttm_bo_setup_vm(bo
);
1224 ret
= ttm_bo_validate(bo
, placement
, interruptible
, false, false);
1228 ttm_bo_unreserve(bo
);
1232 ttm_bo_unreserve(bo
);
1237 EXPORT_SYMBOL(ttm_bo_init
);
1239 static inline size_t ttm_bo_size(struct ttm_bo_global
*glob
,
1240 unsigned long num_pages
)
1242 size_t page_array_size
= (num_pages
* sizeof(void *) + PAGE_SIZE
- 1) &
1245 return glob
->ttm_bo_size
+ 2 * page_array_size
;
1248 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1250 enum ttm_bo_type type
,
1251 struct ttm_placement
*placement
,
1252 uint32_t page_alignment
,
1253 unsigned long buffer_start
,
1255 struct file
*persistant_swap_storage
,
1256 struct ttm_buffer_object
**p_bo
)
1258 struct ttm_buffer_object
*bo
;
1259 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1263 ttm_bo_size(bdev
->glob
, (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
);
1264 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1265 if (unlikely(ret
!= 0))
1268 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1270 if (unlikely(bo
== NULL
)) {
1271 ttm_mem_global_free(mem_glob
, acc_size
);
1275 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1276 buffer_start
, interruptible
,
1277 persistant_swap_storage
, acc_size
, NULL
);
1278 if (likely(ret
== 0))
1284 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1285 unsigned mem_type
, bool allow_errors
)
1287 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1288 struct ttm_bo_global
*glob
= bdev
->glob
;
1292 * Can't use standard list traversal since we're unlocking.
1295 spin_lock(&glob
->lru_lock
);
1296 while (!list_empty(&man
->lru
)) {
1297 spin_unlock(&glob
->lru_lock
);
1298 ret
= ttm_mem_evict_first(bdev
, mem_type
, false, false, false);
1303 printk(KERN_ERR TTM_PFX
1304 "Cleanup eviction failed\n");
1307 spin_lock(&glob
->lru_lock
);
1309 spin_unlock(&glob
->lru_lock
);
1313 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1315 struct ttm_mem_type_manager
*man
;
1318 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1319 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", mem_type
);
1322 man
= &bdev
->man
[mem_type
];
1324 if (!man
->has_type
) {
1325 printk(KERN_ERR TTM_PFX
"Trying to take down uninitialized "
1326 "memory manager type %u\n", mem_type
);
1330 man
->use_type
= false;
1331 man
->has_type
= false;
1335 ttm_bo_force_list_clean(bdev
, mem_type
, false);
1337 ret
= (*man
->func
->takedown
)(man
);
1342 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1344 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1346 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1348 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1349 printk(KERN_ERR TTM_PFX
1350 "Illegal memory manager memory type %u.\n",
1355 if (!man
->has_type
) {
1356 printk(KERN_ERR TTM_PFX
1357 "Memory type %u has not been initialized.\n",
1362 return ttm_bo_force_list_clean(bdev
, mem_type
, true);
1364 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1366 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1367 unsigned long p_size
)
1370 struct ttm_mem_type_manager
*man
;
1372 if (type
>= TTM_NUM_MEM_TYPES
) {
1373 printk(KERN_ERR TTM_PFX
"Illegal memory type %d\n", type
);
1377 man
= &bdev
->man
[type
];
1378 if (man
->has_type
) {
1379 printk(KERN_ERR TTM_PFX
1380 "Memory manager already initialized for type %d\n",
1385 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1391 if (type
!= TTM_PL_SYSTEM
) {
1393 printk(KERN_ERR TTM_PFX
1394 "Zero size memory manager type %d\n",
1399 ret
= (*man
->func
->init
)(man
, p_size
);
1403 man
->has_type
= true;
1404 man
->use_type
= true;
1407 INIT_LIST_HEAD(&man
->lru
);
1411 EXPORT_SYMBOL(ttm_bo_init_mm
);
1413 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1415 struct ttm_bo_global
*glob
=
1416 container_of(kobj
, struct ttm_bo_global
, kobj
);
1418 ttm_mem_unregister_shrink(glob
->mem_glob
, &glob
->shrink
);
1419 __free_page(glob
->dummy_read_page
);
1423 void ttm_bo_global_release(struct drm_global_reference
*ref
)
1425 struct ttm_bo_global
*glob
= ref
->object
;
1427 kobject_del(&glob
->kobj
);
1428 kobject_put(&glob
->kobj
);
1430 EXPORT_SYMBOL(ttm_bo_global_release
);
1432 int ttm_bo_global_init(struct drm_global_reference
*ref
)
1434 struct ttm_bo_global_ref
*bo_ref
=
1435 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1436 struct ttm_bo_global
*glob
= ref
->object
;
1439 mutex_init(&glob
->device_list_mutex
);
1440 spin_lock_init(&glob
->lru_lock
);
1441 glob
->mem_glob
= bo_ref
->mem_glob
;
1442 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1444 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1449 INIT_LIST_HEAD(&glob
->swap_lru
);
1450 INIT_LIST_HEAD(&glob
->device_list
);
1452 ttm_mem_init_shrink(&glob
->shrink
, ttm_bo_swapout
);
1453 ret
= ttm_mem_register_shrink(glob
->mem_glob
, &glob
->shrink
);
1454 if (unlikely(ret
!= 0)) {
1455 printk(KERN_ERR TTM_PFX
1456 "Could not register buffer object swapout.\n");
1460 glob
->ttm_bo_extra_size
=
1461 ttm_round_pot(sizeof(struct ttm_tt
)) +
1462 ttm_round_pot(sizeof(struct ttm_backend
));
1464 glob
->ttm_bo_size
= glob
->ttm_bo_extra_size
+
1465 ttm_round_pot(sizeof(struct ttm_buffer_object
));
1467 atomic_set(&glob
->bo_count
, 0);
1469 ret
= kobject_init_and_add(
1470 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1471 if (unlikely(ret
!= 0))
1472 kobject_put(&glob
->kobj
);
1475 __free_page(glob
->dummy_read_page
);
1480 EXPORT_SYMBOL(ttm_bo_global_init
);
1483 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1486 unsigned i
= TTM_NUM_MEM_TYPES
;
1487 struct ttm_mem_type_manager
*man
;
1488 struct ttm_bo_global
*glob
= bdev
->glob
;
1491 man
= &bdev
->man
[i
];
1492 if (man
->has_type
) {
1493 man
->use_type
= false;
1494 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1496 printk(KERN_ERR TTM_PFX
1497 "DRM memory manager type %d "
1498 "is not clean.\n", i
);
1500 man
->has_type
= false;
1504 mutex_lock(&glob
->device_list_mutex
);
1505 list_del(&bdev
->device_list
);
1506 mutex_unlock(&glob
->device_list_mutex
);
1508 if (!cancel_delayed_work(&bdev
->wq
))
1509 flush_scheduled_work();
1511 while (ttm_bo_delayed_delete(bdev
, true))
1514 spin_lock(&glob
->lru_lock
);
1515 if (list_empty(&bdev
->ddestroy
))
1516 TTM_DEBUG("Delayed destroy list was clean\n");
1518 if (list_empty(&bdev
->man
[0].lru
))
1519 TTM_DEBUG("Swap list was clean\n");
1520 spin_unlock(&glob
->lru_lock
);
1522 BUG_ON(!drm_mm_clean(&bdev
->addr_space_mm
));
1523 write_lock(&bdev
->vm_lock
);
1524 drm_mm_takedown(&bdev
->addr_space_mm
);
1525 write_unlock(&bdev
->vm_lock
);
1529 EXPORT_SYMBOL(ttm_bo_device_release
);
1531 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1532 struct ttm_bo_global
*glob
,
1533 struct ttm_bo_driver
*driver
,
1534 uint64_t file_page_offset
,
1539 rwlock_init(&bdev
->vm_lock
);
1540 bdev
->driver
= driver
;
1542 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1545 * Initialize the system memory buffer type.
1546 * Other types need to be driver / IOCTL initialized.
1548 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1549 if (unlikely(ret
!= 0))
1552 bdev
->addr_space_rb
= RB_ROOT
;
1553 ret
= drm_mm_init(&bdev
->addr_space_mm
, file_page_offset
, 0x10000000);
1554 if (unlikely(ret
!= 0))
1555 goto out_no_addr_mm
;
1557 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1558 bdev
->nice_mode
= true;
1559 INIT_LIST_HEAD(&bdev
->ddestroy
);
1560 bdev
->dev_mapping
= NULL
;
1562 bdev
->need_dma32
= need_dma32
;
1564 mutex_lock(&glob
->device_list_mutex
);
1565 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1566 mutex_unlock(&glob
->device_list_mutex
);
1570 ttm_bo_clean_mm(bdev
, 0);
1574 EXPORT_SYMBOL(ttm_bo_device_init
);
1577 * buffer object vm functions.
1580 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1582 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1584 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1585 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1588 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1591 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1597 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1599 struct ttm_bo_device
*bdev
= bo
->bdev
;
1600 loff_t offset
= (loff_t
) bo
->addr_space_offset
;
1601 loff_t holelen
= ((loff_t
) bo
->mem
.num_pages
) << PAGE_SHIFT
;
1603 if (!bdev
->dev_mapping
)
1605 unmap_mapping_range(bdev
->dev_mapping
, offset
, holelen
, 1);
1606 ttm_mem_io_free(bdev
, &bo
->mem
);
1608 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1610 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object
*bo
)
1612 struct ttm_bo_device
*bdev
= bo
->bdev
;
1613 struct rb_node
**cur
= &bdev
->addr_space_rb
.rb_node
;
1614 struct rb_node
*parent
= NULL
;
1615 struct ttm_buffer_object
*cur_bo
;
1616 unsigned long offset
= bo
->vm_node
->start
;
1617 unsigned long cur_offset
;
1621 cur_bo
= rb_entry(parent
, struct ttm_buffer_object
, vm_rb
);
1622 cur_offset
= cur_bo
->vm_node
->start
;
1623 if (offset
< cur_offset
)
1624 cur
= &parent
->rb_left
;
1625 else if (offset
> cur_offset
)
1626 cur
= &parent
->rb_right
;
1631 rb_link_node(&bo
->vm_rb
, parent
, cur
);
1632 rb_insert_color(&bo
->vm_rb
, &bdev
->addr_space_rb
);
1638 * @bo: the buffer to allocate address space for
1640 * Allocate address space in the drm device so that applications
1641 * can mmap the buffer and access the contents. This only
1642 * applies to ttm_bo_type_device objects as others are not
1643 * placed in the drm device address space.
1646 static int ttm_bo_setup_vm(struct ttm_buffer_object
*bo
)
1648 struct ttm_bo_device
*bdev
= bo
->bdev
;
1652 ret
= drm_mm_pre_get(&bdev
->addr_space_mm
);
1653 if (unlikely(ret
!= 0))
1656 write_lock(&bdev
->vm_lock
);
1657 bo
->vm_node
= drm_mm_search_free(&bdev
->addr_space_mm
,
1658 bo
->mem
.num_pages
, 0, 0);
1660 if (unlikely(bo
->vm_node
== NULL
)) {
1665 bo
->vm_node
= drm_mm_get_block_atomic(bo
->vm_node
,
1666 bo
->mem
.num_pages
, 0);
1668 if (unlikely(bo
->vm_node
== NULL
)) {
1669 write_unlock(&bdev
->vm_lock
);
1673 ttm_bo_vm_insert_rb(bo
);
1674 write_unlock(&bdev
->vm_lock
);
1675 bo
->addr_space_offset
= ((uint64_t) bo
->vm_node
->start
) << PAGE_SHIFT
;
1679 write_unlock(&bdev
->vm_lock
);
1683 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1684 bool lazy
, bool interruptible
, bool no_wait
)
1686 struct ttm_bo_driver
*driver
= bo
->bdev
->driver
;
1691 if (likely(bo
->sync_obj
== NULL
))
1694 while (bo
->sync_obj
) {
1696 if (driver
->sync_obj_signaled(bo
->sync_obj
, bo
->sync_obj_arg
)) {
1697 void *tmp_obj
= bo
->sync_obj
;
1698 bo
->sync_obj
= NULL
;
1699 clear_bit(TTM_BO_PRIV_FLAG_MOVING
, &bo
->priv_flags
);
1700 spin_unlock(&bo
->lock
);
1701 driver
->sync_obj_unref(&tmp_obj
);
1702 spin_lock(&bo
->lock
);
1709 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
1710 sync_obj_arg
= bo
->sync_obj_arg
;
1711 spin_unlock(&bo
->lock
);
1712 ret
= driver
->sync_obj_wait(sync_obj
, sync_obj_arg
,
1713 lazy
, interruptible
);
1714 if (unlikely(ret
!= 0)) {
1715 driver
->sync_obj_unref(&sync_obj
);
1716 spin_lock(&bo
->lock
);
1719 spin_lock(&bo
->lock
);
1720 if (likely(bo
->sync_obj
== sync_obj
&&
1721 bo
->sync_obj_arg
== sync_obj_arg
)) {
1722 void *tmp_obj
= bo
->sync_obj
;
1723 bo
->sync_obj
= NULL
;
1724 clear_bit(TTM_BO_PRIV_FLAG_MOVING
,
1726 spin_unlock(&bo
->lock
);
1727 driver
->sync_obj_unref(&sync_obj
);
1728 driver
->sync_obj_unref(&tmp_obj
);
1729 spin_lock(&bo
->lock
);
1731 spin_unlock(&bo
->lock
);
1732 driver
->sync_obj_unref(&sync_obj
);
1733 spin_lock(&bo
->lock
);
1738 EXPORT_SYMBOL(ttm_bo_wait
);
1740 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1745 * Using ttm_bo_reserve makes sure the lru lists are updated.
1748 ret
= ttm_bo_reserve(bo
, true, no_wait
, false, 0);
1749 if (unlikely(ret
!= 0))
1751 spin_lock(&bo
->lock
);
1752 ret
= ttm_bo_wait(bo
, false, true, no_wait
);
1753 spin_unlock(&bo
->lock
);
1754 if (likely(ret
== 0))
1755 atomic_inc(&bo
->cpu_writers
);
1756 ttm_bo_unreserve(bo
);
1759 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1761 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1763 if (atomic_dec_and_test(&bo
->cpu_writers
))
1764 wake_up_all(&bo
->event_queue
);
1766 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1769 * A buffer object shrink method that tries to swap out the first
1770 * buffer object on the bo_global::swap_lru list.
1773 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
)
1775 struct ttm_bo_global
*glob
=
1776 container_of(shrink
, struct ttm_bo_global
, shrink
);
1777 struct ttm_buffer_object
*bo
;
1780 uint32_t swap_placement
= (TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_SYSTEM
);
1782 spin_lock(&glob
->lru_lock
);
1783 while (ret
== -EBUSY
) {
1784 if (unlikely(list_empty(&glob
->swap_lru
))) {
1785 spin_unlock(&glob
->lru_lock
);
1789 bo
= list_first_entry(&glob
->swap_lru
,
1790 struct ttm_buffer_object
, swap
);
1791 kref_get(&bo
->list_kref
);
1793 if (!list_empty(&bo
->ddestroy
)) {
1794 spin_unlock(&glob
->lru_lock
);
1795 (void) ttm_bo_cleanup_refs(bo
, false, false, false);
1796 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1801 * Reserve buffer. Since we unlock while sleeping, we need
1802 * to re-check that nobody removed us from the swap-list while
1806 ret
= ttm_bo_reserve_locked(bo
, false, true, false, 0);
1807 if (unlikely(ret
== -EBUSY
)) {
1808 spin_unlock(&glob
->lru_lock
);
1809 ttm_bo_wait_unreserved(bo
, false);
1810 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1811 spin_lock(&glob
->lru_lock
);
1816 put_count
= ttm_bo_del_from_lru(bo
);
1817 spin_unlock(&glob
->lru_lock
);
1820 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
1823 * Wait for GPU, then move to system cached.
1826 spin_lock(&bo
->lock
);
1827 ret
= ttm_bo_wait(bo
, false, false, false);
1828 spin_unlock(&bo
->lock
);
1830 if (unlikely(ret
!= 0))
1833 if ((bo
->mem
.placement
& swap_placement
) != swap_placement
) {
1834 struct ttm_mem_reg evict_mem
;
1836 evict_mem
= bo
->mem
;
1837 evict_mem
.mm_node
= NULL
;
1838 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1839 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1841 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true,
1842 false, false, false);
1843 if (unlikely(ret
!= 0))
1847 ttm_bo_unmap_virtual(bo
);
1850 * Swap out. Buffer will be swapped in again as soon as
1851 * anyone tries to access a ttm page.
1854 if (bo
->bdev
->driver
->swap_notify
)
1855 bo
->bdev
->driver
->swap_notify(bo
);
1857 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistant_swap_storage
);
1862 * Unreserve without putting on LRU to avoid swapping out an
1863 * already swapped buffer.
1866 atomic_set(&bo
->reserved
, 0);
1867 wake_up_all(&bo
->event_queue
);
1868 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1872 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1874 while (ttm_bo_swapout(&bdev
->glob
->shrink
) == 0)
1877 EXPORT_SYMBOL(ttm_bo_swapout_all
);