This source file includes following definitions.
- ttm_bo_default_destroy
- ttm_mem_type_from_place
- ttm_mem_type_debug
- ttm_bo_mem_space_debug
- ttm_bo_global_show
- ttm_bo_type_flags
- ttm_bo_release_list
- ttm_bo_add_mem_to_lru
- ttm_bo_add_to_lru
- ttm_bo_ref_bug
- ttm_bo_del_from_lru
- ttm_bo_del_sub_from_lru
- ttm_bo_bulk_move_set_pos
- ttm_bo_move_to_lru_tail
- ttm_bo_bulk_move_lru_tail
- ttm_bo_handle_move_mem
- ttm_bo_cleanup_memtype_use
- ttm_bo_individualize_resv
- ttm_bo_flush_all_fences
- ttm_bo_cleanup_refs_or_queue
- ttm_bo_cleanup_refs
- ttm_bo_delayed_delete
- ttm_bo_delayed_workqueue
- ttm_bo_release
- ttm_bo_put
- ttm_bo_lock_delayed_workqueue
- ttm_bo_unlock_delayed_workqueue
- ttm_bo_evict
- ttm_bo_eviction_valuable
- ttm_bo_evict_swapout_allowable
- ttm_mem_evict_wait_busy
- ttm_mem_evict_first
- ttm_bo_mem_put
- ttm_bo_add_move_fence
- ttm_bo_mem_force_space
- ttm_bo_select_caching
- ttm_bo_mt_compatible
- ttm_bo_mem_placement
- ttm_bo_mem_space
- ttm_bo_move_buffer
- ttm_bo_places_compat
- ttm_bo_mem_compat
- ttm_bo_validate
- ttm_bo_init_reserved
- ttm_bo_init
- ttm_bo_acc_size
- ttm_bo_dma_acc_size
- ttm_bo_create
- ttm_bo_force_list_clean
- ttm_bo_clean_mm
- ttm_bo_evict_mm
- ttm_bo_init_mm
- ttm_bo_global_kobj_release
- ttm_bo_global_release
- ttm_bo_global_init
- ttm_bo_device_release
- ttm_bo_device_init
- ttm_mem_reg_is_pci
- ttm_bo_unmap_virtual_locked
- ttm_bo_unmap_virtual
- ttm_bo_wait
- ttm_bo_synccpu_write_grab
- ttm_bo_synccpu_write_release
- ttm_bo_swapout
- ttm_bo_swapout_all
- ttm_bo_wait_unreserved
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 #define pr_fmt(fmt) "[TTM] " fmt
33
34 #include <drm/ttm/ttm_module.h>
35 #include <drm/ttm/ttm_bo_driver.h>
36 #include <drm/ttm/ttm_placement.h>
37 #include <linux/jiffies.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
40 #include <linux/mm.h>
41 #include <linux/file.h>
42 #include <linux/module.h>
43 #include <linux/atomic.h>
44 #include <linux/dma-resv.h>
45
46 static void ttm_bo_global_kobj_release(struct kobject *kobj);
47
48
49
50
51 DEFINE_MUTEX(ttm_global_mutex);
52 unsigned ttm_bo_glob_use_count;
53 struct ttm_bo_global ttm_bo_glob;
54
55 static struct attribute ttm_bo_count = {
56 .name = "bo_count",
57 .mode = S_IRUGO
58 };
59
60
61 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
62 {
63 kfree(bo);
64 }
65
66 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
67 uint32_t *mem_type)
68 {
69 int pos;
70
71 pos = ffs(place->flags & TTM_PL_MASK_MEM);
72 if (unlikely(!pos))
73 return -EINVAL;
74
75 *mem_type = pos - 1;
76 return 0;
77 }
78
79 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, struct drm_printer *p,
80 int mem_type)
81 {
82 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
83
84 drm_printf(p, " has_type: %d\n", man->has_type);
85 drm_printf(p, " use_type: %d\n", man->use_type);
86 drm_printf(p, " flags: 0x%08X\n", man->flags);
87 drm_printf(p, " gpu_offset: 0x%08llX\n", man->gpu_offset);
88 drm_printf(p, " size: %llu\n", man->size);
89 drm_printf(p, " available_caching: 0x%08X\n", man->available_caching);
90 drm_printf(p, " default_caching: 0x%08X\n", man->default_caching);
91 if (mem_type != TTM_PL_SYSTEM)
92 (*man->func->debug)(man, p);
93 }
94
95 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
96 struct ttm_placement *placement)
97 {
98 struct drm_printer p = drm_debug_printer(TTM_PFX);
99 int i, ret, mem_type;
100
101 drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
102 bo, bo->mem.num_pages, bo->mem.size >> 10,
103 bo->mem.size >> 20);
104 for (i = 0; i < placement->num_placement; i++) {
105 ret = ttm_mem_type_from_place(&placement->placement[i],
106 &mem_type);
107 if (ret)
108 return;
109 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
110 i, placement->placement[i].flags, mem_type);
111 ttm_mem_type_debug(bo->bdev, &p, mem_type);
112 }
113 }
114
115 static ssize_t ttm_bo_global_show(struct kobject *kobj,
116 struct attribute *attr,
117 char *buffer)
118 {
119 struct ttm_bo_global *glob =
120 container_of(kobj, struct ttm_bo_global, kobj);
121
122 return snprintf(buffer, PAGE_SIZE, "%d\n",
123 atomic_read(&glob->bo_count));
124 }
125
126 static struct attribute *ttm_bo_global_attrs[] = {
127 &ttm_bo_count,
128 NULL
129 };
130
131 static const struct sysfs_ops ttm_bo_global_ops = {
132 .show = &ttm_bo_global_show
133 };
134
135 static struct kobj_type ttm_bo_glob_kobj_type = {
136 .release = &ttm_bo_global_kobj_release,
137 .sysfs_ops = &ttm_bo_global_ops,
138 .default_attrs = ttm_bo_global_attrs
139 };
140
141
142 static inline uint32_t ttm_bo_type_flags(unsigned type)
143 {
144 return 1 << (type);
145 }
146
147 static void ttm_bo_release_list(struct kref *list_kref)
148 {
149 struct ttm_buffer_object *bo =
150 container_of(list_kref, struct ttm_buffer_object, list_kref);
151 struct ttm_bo_device *bdev = bo->bdev;
152 size_t acc_size = bo->acc_size;
153
154 BUG_ON(kref_read(&bo->list_kref));
155 BUG_ON(kref_read(&bo->kref));
156 BUG_ON(atomic_read(&bo->cpu_writers));
157 BUG_ON(bo->mem.mm_node != NULL);
158 BUG_ON(!list_empty(&bo->lru));
159 BUG_ON(!list_empty(&bo->ddestroy));
160 ttm_tt_destroy(bo->ttm);
161 atomic_dec(&bo->bdev->glob->bo_count);
162 dma_fence_put(bo->moving);
163 if (!ttm_bo_uses_embedded_gem_object(bo))
164 dma_resv_fini(&bo->base._resv);
165 mutex_destroy(&bo->wu_mutex);
166 bo->destroy(bo);
167 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
168 }
169
170 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
171 struct ttm_mem_reg *mem)
172 {
173 struct ttm_bo_device *bdev = bo->bdev;
174 struct ttm_mem_type_manager *man;
175
176 dma_resv_assert_held(bo->base.resv);
177
178 if (!list_empty(&bo->lru))
179 return;
180
181 if (mem->placement & TTM_PL_FLAG_NO_EVICT)
182 return;
183
184 man = &bdev->man[mem->mem_type];
185 list_add_tail(&bo->lru, &man->lru[bo->priority]);
186 kref_get(&bo->list_kref);
187
188 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm &&
189 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
190 TTM_PAGE_FLAG_SWAPPED))) {
191 list_add_tail(&bo->swap, &bdev->glob->swap_lru[bo->priority]);
192 kref_get(&bo->list_kref);
193 }
194 }
195
196 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
197 {
198 ttm_bo_add_mem_to_lru(bo, &bo->mem);
199 }
200 EXPORT_SYMBOL(ttm_bo_add_to_lru);
201
202 static void ttm_bo_ref_bug(struct kref *list_kref)
203 {
204 BUG();
205 }
206
207 void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
208 {
209 struct ttm_bo_device *bdev = bo->bdev;
210 bool notify = false;
211
212 if (!list_empty(&bo->swap)) {
213 list_del_init(&bo->swap);
214 kref_put(&bo->list_kref, ttm_bo_ref_bug);
215 notify = true;
216 }
217 if (!list_empty(&bo->lru)) {
218 list_del_init(&bo->lru);
219 kref_put(&bo->list_kref, ttm_bo_ref_bug);
220 notify = true;
221 }
222
223 if (notify && bdev->driver->del_from_lru_notify)
224 bdev->driver->del_from_lru_notify(bo);
225 }
226
227 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
228 {
229 struct ttm_bo_global *glob = bo->bdev->glob;
230
231 spin_lock(&glob->lru_lock);
232 ttm_bo_del_from_lru(bo);
233 spin_unlock(&glob->lru_lock);
234 }
235 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
236
237 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
238 struct ttm_buffer_object *bo)
239 {
240 if (!pos->first)
241 pos->first = bo;
242 pos->last = bo;
243 }
244
245 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
246 struct ttm_lru_bulk_move *bulk)
247 {
248 dma_resv_assert_held(bo->base.resv);
249
250 ttm_bo_del_from_lru(bo);
251 ttm_bo_add_to_lru(bo);
252
253 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
254 switch (bo->mem.mem_type) {
255 case TTM_PL_TT:
256 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
257 break;
258
259 case TTM_PL_VRAM:
260 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
261 break;
262 }
263 if (bo->ttm && !(bo->ttm->page_flags &
264 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
265 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
266 }
267 }
268 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
269
270 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
271 {
272 unsigned i;
273
274 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
275 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
276 struct ttm_mem_type_manager *man;
277
278 if (!pos->first)
279 continue;
280
281 dma_resv_assert_held(pos->first->base.resv);
282 dma_resv_assert_held(pos->last->base.resv);
283
284 man = &pos->first->bdev->man[TTM_PL_TT];
285 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
286 &pos->last->lru);
287 }
288
289 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
290 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
291 struct ttm_mem_type_manager *man;
292
293 if (!pos->first)
294 continue;
295
296 dma_resv_assert_held(pos->first->base.resv);
297 dma_resv_assert_held(pos->last->base.resv);
298
299 man = &pos->first->bdev->man[TTM_PL_VRAM];
300 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
301 &pos->last->lru);
302 }
303
304 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
305 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
306 struct list_head *lru;
307
308 if (!pos->first)
309 continue;
310
311 dma_resv_assert_held(pos->first->base.resv);
312 dma_resv_assert_held(pos->last->base.resv);
313
314 lru = &pos->first->bdev->glob->swap_lru[i];
315 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
316 }
317 }
318 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
319
320 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
321 struct ttm_mem_reg *mem, bool evict,
322 struct ttm_operation_ctx *ctx)
323 {
324 struct ttm_bo_device *bdev = bo->bdev;
325 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
326 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
327 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
328 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
329 int ret = 0;
330
331 if (old_is_pci || new_is_pci ||
332 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
333 ret = ttm_mem_io_lock(old_man, true);
334 if (unlikely(ret != 0))
335 goto out_err;
336 ttm_bo_unmap_virtual_locked(bo);
337 ttm_mem_io_unlock(old_man);
338 }
339
340
341
342
343
344 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
345 if (bo->ttm == NULL) {
346 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
347 ret = ttm_tt_create(bo, zero);
348 if (ret)
349 goto out_err;
350 }
351
352 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
353 if (ret)
354 goto out_err;
355
356 if (mem->mem_type != TTM_PL_SYSTEM) {
357 ret = ttm_tt_bind(bo->ttm, mem, ctx);
358 if (ret)
359 goto out_err;
360 }
361
362 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
363 if (bdev->driver->move_notify)
364 bdev->driver->move_notify(bo, evict, mem);
365 bo->mem = *mem;
366 mem->mm_node = NULL;
367 goto moved;
368 }
369 }
370
371 if (bdev->driver->move_notify)
372 bdev->driver->move_notify(bo, evict, mem);
373
374 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
375 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
376 ret = ttm_bo_move_ttm(bo, ctx, mem);
377 else if (bdev->driver->move)
378 ret = bdev->driver->move(bo, evict, ctx, mem);
379 else
380 ret = ttm_bo_move_memcpy(bo, ctx, mem);
381
382 if (ret) {
383 if (bdev->driver->move_notify) {
384 swap(*mem, bo->mem);
385 bdev->driver->move_notify(bo, false, mem);
386 swap(*mem, bo->mem);
387 }
388
389 goto out_err;
390 }
391
392 moved:
393 if (bo->evicted) {
394 if (bdev->driver->invalidate_caches) {
395 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
396 if (ret)
397 pr_err("Can not flush read caches\n");
398 }
399 bo->evicted = false;
400 }
401
402 if (bo->mem.mm_node)
403 bo->offset = (bo->mem.start << PAGE_SHIFT) +
404 bdev->man[bo->mem.mem_type].gpu_offset;
405 else
406 bo->offset = 0;
407
408 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
409 return 0;
410
411 out_err:
412 new_man = &bdev->man[bo->mem.mem_type];
413 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
414 ttm_tt_destroy(bo->ttm);
415 bo->ttm = NULL;
416 }
417
418 return ret;
419 }
420
421
422
423
424
425
426
427
428
429 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
430 {
431 if (bo->bdev->driver->move_notify)
432 bo->bdev->driver->move_notify(bo, false, NULL);
433
434 ttm_tt_destroy(bo->ttm);
435 bo->ttm = NULL;
436 ttm_bo_mem_put(bo, &bo->mem);
437 }
438
439 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
440 {
441 int r;
442
443 if (bo->base.resv == &bo->base._resv)
444 return 0;
445
446 BUG_ON(!dma_resv_trylock(&bo->base._resv));
447
448 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
449 if (r)
450 dma_resv_unlock(&bo->base._resv);
451
452 return r;
453 }
454
455 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
456 {
457 struct dma_resv_list *fobj;
458 struct dma_fence *fence;
459 int i;
460
461 fobj = dma_resv_get_list(&bo->base._resv);
462 fence = dma_resv_get_excl(&bo->base._resv);
463 if (fence && !fence->ops->signaled)
464 dma_fence_enable_sw_signaling(fence);
465
466 for (i = 0; fobj && i < fobj->shared_count; ++i) {
467 fence = rcu_dereference_protected(fobj->shared[i],
468 dma_resv_held(bo->base.resv));
469
470 if (!fence->ops->signaled)
471 dma_fence_enable_sw_signaling(fence);
472 }
473 }
474
475 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
476 {
477 struct ttm_bo_device *bdev = bo->bdev;
478 struct ttm_bo_global *glob = bdev->glob;
479 int ret;
480
481 ret = ttm_bo_individualize_resv(bo);
482 if (ret) {
483
484
485
486 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
487 30 * HZ);
488 spin_lock(&glob->lru_lock);
489 goto error;
490 }
491
492 spin_lock(&glob->lru_lock);
493 ret = dma_resv_trylock(bo->base.resv) ? 0 : -EBUSY;
494 if (!ret) {
495 if (dma_resv_test_signaled_rcu(&bo->base._resv, true)) {
496 ttm_bo_del_from_lru(bo);
497 spin_unlock(&glob->lru_lock);
498 if (bo->base.resv != &bo->base._resv)
499 dma_resv_unlock(&bo->base._resv);
500
501 ttm_bo_cleanup_memtype_use(bo);
502 dma_resv_unlock(bo->base.resv);
503 return;
504 }
505
506 ttm_bo_flush_all_fences(bo);
507
508
509
510
511
512
513 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
514 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
515 ttm_bo_add_to_lru(bo);
516 }
517
518 dma_resv_unlock(bo->base.resv);
519 }
520 if (bo->base.resv != &bo->base._resv) {
521 ttm_bo_flush_all_fences(bo);
522 dma_resv_unlock(&bo->base._resv);
523 }
524
525 error:
526 kref_get(&bo->list_kref);
527 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
528 spin_unlock(&glob->lru_lock);
529
530 schedule_delayed_work(&bdev->wq,
531 ((HZ / 100) < 1) ? 1 : HZ / 100);
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
548 bool interruptible, bool no_wait_gpu,
549 bool unlock_resv)
550 {
551 struct ttm_bo_global *glob = bo->bdev->glob;
552 struct dma_resv *resv;
553 int ret;
554
555 if (unlikely(list_empty(&bo->ddestroy)))
556 resv = bo->base.resv;
557 else
558 resv = &bo->base._resv;
559
560 if (dma_resv_test_signaled_rcu(resv, true))
561 ret = 0;
562 else
563 ret = -EBUSY;
564
565 if (ret && !no_wait_gpu) {
566 long lret;
567
568 if (unlock_resv)
569 dma_resv_unlock(bo->base.resv);
570 spin_unlock(&glob->lru_lock);
571
572 lret = dma_resv_wait_timeout_rcu(resv, true,
573 interruptible,
574 30 * HZ);
575
576 if (lret < 0)
577 return lret;
578 else if (lret == 0)
579 return -EBUSY;
580
581 spin_lock(&glob->lru_lock);
582 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
583
584
585
586
587
588
589
590
591 spin_unlock(&glob->lru_lock);
592 return 0;
593 }
594 ret = 0;
595 }
596
597 if (ret || unlikely(list_empty(&bo->ddestroy))) {
598 if (unlock_resv)
599 dma_resv_unlock(bo->base.resv);
600 spin_unlock(&glob->lru_lock);
601 return ret;
602 }
603
604 ttm_bo_del_from_lru(bo);
605 list_del_init(&bo->ddestroy);
606 kref_put(&bo->list_kref, ttm_bo_ref_bug);
607
608 spin_unlock(&glob->lru_lock);
609 ttm_bo_cleanup_memtype_use(bo);
610
611 if (unlock_resv)
612 dma_resv_unlock(bo->base.resv);
613
614 return 0;
615 }
616
617
618
619
620
621 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
622 {
623 struct ttm_bo_global *glob = bdev->glob;
624 struct list_head removed;
625 bool empty;
626
627 INIT_LIST_HEAD(&removed);
628
629 spin_lock(&glob->lru_lock);
630 while (!list_empty(&bdev->ddestroy)) {
631 struct ttm_buffer_object *bo;
632
633 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
634 ddestroy);
635 kref_get(&bo->list_kref);
636 list_move_tail(&bo->ddestroy, &removed);
637
638 if (remove_all || bo->base.resv != &bo->base._resv) {
639 spin_unlock(&glob->lru_lock);
640 dma_resv_lock(bo->base.resv, NULL);
641
642 spin_lock(&glob->lru_lock);
643 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
644
645 } else if (dma_resv_trylock(bo->base.resv)) {
646 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
647 } else {
648 spin_unlock(&glob->lru_lock);
649 }
650
651 kref_put(&bo->list_kref, ttm_bo_release_list);
652 spin_lock(&glob->lru_lock);
653 }
654 list_splice_tail(&removed, &bdev->ddestroy);
655 empty = list_empty(&bdev->ddestroy);
656 spin_unlock(&glob->lru_lock);
657
658 return empty;
659 }
660
661 static void ttm_bo_delayed_workqueue(struct work_struct *work)
662 {
663 struct ttm_bo_device *bdev =
664 container_of(work, struct ttm_bo_device, wq.work);
665
666 if (!ttm_bo_delayed_delete(bdev, false))
667 schedule_delayed_work(&bdev->wq,
668 ((HZ / 100) < 1) ? 1 : HZ / 100);
669 }
670
671 static void ttm_bo_release(struct kref *kref)
672 {
673 struct ttm_buffer_object *bo =
674 container_of(kref, struct ttm_buffer_object, kref);
675 struct ttm_bo_device *bdev = bo->bdev;
676 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
677
678 if (bo->bdev->driver->release_notify)
679 bo->bdev->driver->release_notify(bo);
680
681 drm_vma_offset_remove(&bdev->vma_manager, &bo->base.vma_node);
682 ttm_mem_io_lock(man, false);
683 ttm_mem_io_free_vm(bo);
684 ttm_mem_io_unlock(man);
685 ttm_bo_cleanup_refs_or_queue(bo);
686 kref_put(&bo->list_kref, ttm_bo_release_list);
687 }
688
689 void ttm_bo_put(struct ttm_buffer_object *bo)
690 {
691 kref_put(&bo->kref, ttm_bo_release);
692 }
693 EXPORT_SYMBOL(ttm_bo_put);
694
695 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
696 {
697 return cancel_delayed_work_sync(&bdev->wq);
698 }
699 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
700
701 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
702 {
703 if (resched)
704 schedule_delayed_work(&bdev->wq,
705 ((HZ / 100) < 1) ? 1 : HZ / 100);
706 }
707 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
708
709 static int ttm_bo_evict(struct ttm_buffer_object *bo,
710 struct ttm_operation_ctx *ctx)
711 {
712 struct ttm_bo_device *bdev = bo->bdev;
713 struct ttm_mem_reg evict_mem;
714 struct ttm_placement placement;
715 int ret = 0;
716
717 dma_resv_assert_held(bo->base.resv);
718
719 placement.num_placement = 0;
720 placement.num_busy_placement = 0;
721 bdev->driver->evict_flags(bo, &placement);
722
723 if (!placement.num_placement && !placement.num_busy_placement) {
724 ret = ttm_bo_pipeline_gutting(bo);
725 if (ret)
726 return ret;
727
728 return ttm_tt_create(bo, false);
729 }
730
731 evict_mem = bo->mem;
732 evict_mem.mm_node = NULL;
733 evict_mem.bus.io_reserved_vm = false;
734 evict_mem.bus.io_reserved_count = 0;
735
736 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
737 if (ret) {
738 if (ret != -ERESTARTSYS) {
739 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
740 bo);
741 ttm_bo_mem_space_debug(bo, &placement);
742 }
743 goto out;
744 }
745
746 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
747 if (unlikely(ret)) {
748 if (ret != -ERESTARTSYS)
749 pr_err("Buffer eviction failed\n");
750 ttm_bo_mem_put(bo, &evict_mem);
751 goto out;
752 }
753 bo->evicted = true;
754 out:
755 return ret;
756 }
757
758 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
759 const struct ttm_place *place)
760 {
761
762
763
764 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
765 (place->lpfn && place->lpfn <= bo->mem.start))
766 return false;
767
768 return true;
769 }
770 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
771
772
773
774
775
776
777
778
779
780
781
782 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
783 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
784 {
785 bool ret = false;
786
787 if (bo->base.resv == ctx->resv) {
788 dma_resv_assert_held(bo->base.resv);
789 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
790 || !list_empty(&bo->ddestroy))
791 ret = true;
792 *locked = false;
793 if (busy)
794 *busy = false;
795 } else {
796 ret = dma_resv_trylock(bo->base.resv);
797 *locked = ret;
798 if (busy)
799 *busy = !ret;
800 }
801
802 return ret;
803 }
804
805
806
807
808
809
810
811
812
813
814 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
815 struct ttm_operation_ctx *ctx,
816 struct ww_acquire_ctx *ticket)
817 {
818 int r;
819
820 if (!busy_bo || !ticket)
821 return -EBUSY;
822
823 if (ctx->interruptible)
824 r = dma_resv_lock_interruptible(busy_bo->base.resv,
825 ticket);
826 else
827 r = dma_resv_lock(busy_bo->base.resv, ticket);
828
829
830
831
832
833
834 if (!r)
835 dma_resv_unlock(busy_bo->base.resv);
836
837 return r == -EDEADLK ? -EBUSY : r;
838 }
839
840 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
841 uint32_t mem_type,
842 const struct ttm_place *place,
843 struct ttm_operation_ctx *ctx,
844 struct ww_acquire_ctx *ticket)
845 {
846 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
847 struct ttm_bo_global *glob = bdev->glob;
848 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
849 bool locked = false;
850 unsigned i;
851 int ret;
852
853 spin_lock(&glob->lru_lock);
854 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
855 list_for_each_entry(bo, &man->lru[i], lru) {
856 bool busy;
857
858 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
859 &busy)) {
860 if (busy && !busy_bo && ticket !=
861 dma_resv_locking_ctx(bo->base.resv))
862 busy_bo = bo;
863 continue;
864 }
865
866 if (place && !bdev->driver->eviction_valuable(bo,
867 place)) {
868 if (locked)
869 dma_resv_unlock(bo->base.resv);
870 continue;
871 }
872 break;
873 }
874
875
876 if (&bo->lru != &man->lru[i])
877 break;
878
879 bo = NULL;
880 }
881
882 if (!bo) {
883 if (busy_bo)
884 kref_get(&busy_bo->list_kref);
885 spin_unlock(&glob->lru_lock);
886 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
887 if (busy_bo)
888 kref_put(&busy_bo->list_kref, ttm_bo_release_list);
889 return ret;
890 }
891
892 kref_get(&bo->list_kref);
893
894 if (!list_empty(&bo->ddestroy)) {
895 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
896 ctx->no_wait_gpu, locked);
897 kref_put(&bo->list_kref, ttm_bo_release_list);
898 return ret;
899 }
900
901 ttm_bo_del_from_lru(bo);
902 spin_unlock(&glob->lru_lock);
903
904 ret = ttm_bo_evict(bo, ctx);
905 if (locked) {
906 ttm_bo_unreserve(bo);
907 } else {
908 spin_lock(&glob->lru_lock);
909 ttm_bo_add_to_lru(bo);
910 spin_unlock(&glob->lru_lock);
911 }
912
913 kref_put(&bo->list_kref, ttm_bo_release_list);
914 return ret;
915 }
916
917 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
918 {
919 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
920
921 if (mem->mm_node)
922 (*man->func->put_node)(man, mem);
923 }
924 EXPORT_SYMBOL(ttm_bo_mem_put);
925
926
927
928
929 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
930 struct ttm_mem_type_manager *man,
931 struct ttm_mem_reg *mem,
932 bool no_wait_gpu)
933 {
934 struct dma_fence *fence;
935 int ret;
936
937 spin_lock(&man->move_lock);
938 fence = dma_fence_get(man->move);
939 spin_unlock(&man->move_lock);
940
941 if (!fence)
942 return 0;
943
944 if (no_wait_gpu)
945 return -EBUSY;
946
947 dma_resv_add_shared_fence(bo->base.resv, fence);
948
949 ret = dma_resv_reserve_shared(bo->base.resv, 1);
950 if (unlikely(ret)) {
951 dma_fence_put(fence);
952 return ret;
953 }
954
955 dma_fence_put(bo->moving);
956 bo->moving = fence;
957 return 0;
958 }
959
960
961
962
963
964 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
965 const struct ttm_place *place,
966 struct ttm_mem_reg *mem,
967 struct ttm_operation_ctx *ctx)
968 {
969 struct ttm_bo_device *bdev = bo->bdev;
970 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
971 struct ww_acquire_ctx *ticket;
972 int ret;
973
974 ticket = dma_resv_locking_ctx(bo->base.resv);
975 do {
976 ret = (*man->func->get_node)(man, bo, place, mem);
977 if (unlikely(ret != 0))
978 return ret;
979 if (mem->mm_node)
980 break;
981 ret = ttm_mem_evict_first(bdev, mem->mem_type, place, ctx,
982 ticket);
983 if (unlikely(ret != 0))
984 return ret;
985 } while (1);
986
987 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
988 }
989
990 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
991 uint32_t cur_placement,
992 uint32_t proposed_placement)
993 {
994 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
995 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
996
997
998
999
1000
1001 if ((cur_placement & caching) != 0)
1002 result |= (cur_placement & caching);
1003 else if ((man->default_caching & caching) != 0)
1004 result |= man->default_caching;
1005 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
1006 result |= TTM_PL_FLAG_CACHED;
1007 else if ((TTM_PL_FLAG_WC & caching) != 0)
1008 result |= TTM_PL_FLAG_WC;
1009 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
1010 result |= TTM_PL_FLAG_UNCACHED;
1011
1012 return result;
1013 }
1014
1015 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
1016 uint32_t mem_type,
1017 const struct ttm_place *place,
1018 uint32_t *masked_placement)
1019 {
1020 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
1021
1022 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
1023 return false;
1024
1025 if ((place->flags & man->available_caching) == 0)
1026 return false;
1027
1028 cur_flags |= (place->flags & man->available_caching);
1029
1030 *masked_placement = cur_flags;
1031 return true;
1032 }
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
1046 const struct ttm_place *place,
1047 struct ttm_mem_reg *mem,
1048 struct ttm_operation_ctx *ctx)
1049 {
1050 struct ttm_bo_device *bdev = bo->bdev;
1051 uint32_t mem_type = TTM_PL_SYSTEM;
1052 struct ttm_mem_type_manager *man;
1053 uint32_t cur_flags = 0;
1054 int ret;
1055
1056 ret = ttm_mem_type_from_place(place, &mem_type);
1057 if (ret)
1058 return ret;
1059
1060 man = &bdev->man[mem_type];
1061 if (!man->has_type || !man->use_type)
1062 return -EBUSY;
1063
1064 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1065 return -EBUSY;
1066
1067 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, cur_flags);
1068
1069
1070
1071
1072 ttm_flag_masked(&cur_flags, place->flags, ~TTM_PL_MASK_MEMTYPE);
1073
1074 mem->mem_type = mem_type;
1075 mem->placement = cur_flags;
1076
1077 if (bo->mem.mem_type < mem_type && !list_empty(&bo->lru)) {
1078 spin_lock(&bo->bdev->glob->lru_lock);
1079 ttm_bo_del_from_lru(bo);
1080 ttm_bo_add_mem_to_lru(bo, mem);
1081 spin_unlock(&bo->bdev->glob->lru_lock);
1082 }
1083
1084 return 0;
1085 }
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1096 struct ttm_placement *placement,
1097 struct ttm_mem_reg *mem,
1098 struct ttm_operation_ctx *ctx)
1099 {
1100 struct ttm_bo_device *bdev = bo->bdev;
1101 bool type_found = false;
1102 int i, ret;
1103
1104 ret = dma_resv_reserve_shared(bo->base.resv, 1);
1105 if (unlikely(ret))
1106 return ret;
1107
1108 mem->mm_node = NULL;
1109 for (i = 0; i < placement->num_placement; ++i) {
1110 const struct ttm_place *place = &placement->placement[i];
1111 struct ttm_mem_type_manager *man;
1112
1113 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1114 if (ret == -EBUSY)
1115 continue;
1116 if (ret)
1117 goto error;
1118
1119 type_found = true;
1120 mem->mm_node = NULL;
1121 if (mem->mem_type == TTM_PL_SYSTEM)
1122 return 0;
1123
1124 man = &bdev->man[mem->mem_type];
1125 ret = (*man->func->get_node)(man, bo, place, mem);
1126 if (unlikely(ret))
1127 goto error;
1128
1129 if (!mem->mm_node)
1130 continue;
1131
1132 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
1133 if (unlikely(ret)) {
1134 (*man->func->put_node)(man, mem);
1135 if (ret == -EBUSY)
1136 continue;
1137
1138 goto error;
1139 }
1140 return 0;
1141 }
1142
1143 for (i = 0; i < placement->num_busy_placement; ++i) {
1144 const struct ttm_place *place = &placement->busy_placement[i];
1145
1146 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1147 if (ret == -EBUSY)
1148 continue;
1149 if (ret)
1150 goto error;
1151
1152 type_found = true;
1153 mem->mm_node = NULL;
1154 if (mem->mem_type == TTM_PL_SYSTEM)
1155 return 0;
1156
1157 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
1158 if (ret == 0 && mem->mm_node)
1159 return 0;
1160
1161 if (ret && ret != -EBUSY)
1162 goto error;
1163 }
1164
1165 ret = -ENOMEM;
1166 if (!type_found) {
1167 pr_err(TTM_PFX "No compatible memory type found\n");
1168 ret = -EINVAL;
1169 }
1170
1171 error:
1172 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
1173 spin_lock(&bo->bdev->glob->lru_lock);
1174 ttm_bo_move_to_lru_tail(bo, NULL);
1175 spin_unlock(&bo->bdev->glob->lru_lock);
1176 }
1177
1178 return ret;
1179 }
1180 EXPORT_SYMBOL(ttm_bo_mem_space);
1181
1182 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1183 struct ttm_placement *placement,
1184 struct ttm_operation_ctx *ctx)
1185 {
1186 int ret = 0;
1187 struct ttm_mem_reg mem;
1188
1189 dma_resv_assert_held(bo->base.resv);
1190
1191 mem.num_pages = bo->num_pages;
1192 mem.size = mem.num_pages << PAGE_SHIFT;
1193 mem.page_alignment = bo->mem.page_alignment;
1194 mem.bus.io_reserved_vm = false;
1195 mem.bus.io_reserved_count = 0;
1196
1197
1198
1199 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1200 if (ret)
1201 goto out_unlock;
1202 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1203 out_unlock:
1204 if (ret && mem.mm_node)
1205 ttm_bo_mem_put(bo, &mem);
1206 return ret;
1207 }
1208
1209 static bool ttm_bo_places_compat(const struct ttm_place *places,
1210 unsigned num_placement,
1211 struct ttm_mem_reg *mem,
1212 uint32_t *new_flags)
1213 {
1214 unsigned i;
1215
1216 for (i = 0; i < num_placement; i++) {
1217 const struct ttm_place *heap = &places[i];
1218
1219 if (mem->mm_node && (mem->start < heap->fpfn ||
1220 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1221 continue;
1222
1223 *new_flags = heap->flags;
1224 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1225 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1226 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1227 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1228 return true;
1229 }
1230 return false;
1231 }
1232
1233 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1234 struct ttm_mem_reg *mem,
1235 uint32_t *new_flags)
1236 {
1237 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1238 mem, new_flags))
1239 return true;
1240
1241 if ((placement->busy_placement != placement->placement ||
1242 placement->num_busy_placement > placement->num_placement) &&
1243 ttm_bo_places_compat(placement->busy_placement,
1244 placement->num_busy_placement,
1245 mem, new_flags))
1246 return true;
1247
1248 return false;
1249 }
1250 EXPORT_SYMBOL(ttm_bo_mem_compat);
1251
1252 int ttm_bo_validate(struct ttm_buffer_object *bo,
1253 struct ttm_placement *placement,
1254 struct ttm_operation_ctx *ctx)
1255 {
1256 int ret;
1257 uint32_t new_flags;
1258
1259 dma_resv_assert_held(bo->base.resv);
1260
1261
1262
1263 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1264 ret = ttm_bo_move_buffer(bo, placement, ctx);
1265 if (ret)
1266 return ret;
1267 } else {
1268
1269
1270
1271
1272 ttm_flag_masked(&bo->mem.placement, new_flags,
1273 ~TTM_PL_MASK_MEMTYPE);
1274 }
1275
1276
1277
1278 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1279 ret = ttm_tt_create(bo, true);
1280 if (ret)
1281 return ret;
1282 }
1283 return 0;
1284 }
1285 EXPORT_SYMBOL(ttm_bo_validate);
1286
1287 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1288 struct ttm_buffer_object *bo,
1289 unsigned long size,
1290 enum ttm_bo_type type,
1291 struct ttm_placement *placement,
1292 uint32_t page_alignment,
1293 struct ttm_operation_ctx *ctx,
1294 size_t acc_size,
1295 struct sg_table *sg,
1296 struct dma_resv *resv,
1297 void (*destroy) (struct ttm_buffer_object *))
1298 {
1299 int ret = 0;
1300 unsigned long num_pages;
1301 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1302 bool locked;
1303
1304 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1305 if (ret) {
1306 pr_err("Out of kernel memory\n");
1307 if (destroy)
1308 (*destroy)(bo);
1309 else
1310 kfree(bo);
1311 return -ENOMEM;
1312 }
1313
1314 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1315 if (num_pages == 0) {
1316 pr_err("Illegal buffer object size\n");
1317 if (destroy)
1318 (*destroy)(bo);
1319 else
1320 kfree(bo);
1321 ttm_mem_global_free(mem_glob, acc_size);
1322 return -EINVAL;
1323 }
1324 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1325
1326 kref_init(&bo->kref);
1327 kref_init(&bo->list_kref);
1328 atomic_set(&bo->cpu_writers, 0);
1329 INIT_LIST_HEAD(&bo->lru);
1330 INIT_LIST_HEAD(&bo->ddestroy);
1331 INIT_LIST_HEAD(&bo->swap);
1332 INIT_LIST_HEAD(&bo->io_reserve_lru);
1333 mutex_init(&bo->wu_mutex);
1334 bo->bdev = bdev;
1335 bo->type = type;
1336 bo->num_pages = num_pages;
1337 bo->mem.size = num_pages << PAGE_SHIFT;
1338 bo->mem.mem_type = TTM_PL_SYSTEM;
1339 bo->mem.num_pages = bo->num_pages;
1340 bo->mem.mm_node = NULL;
1341 bo->mem.page_alignment = page_alignment;
1342 bo->mem.bus.io_reserved_vm = false;
1343 bo->mem.bus.io_reserved_count = 0;
1344 bo->moving = NULL;
1345 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1346 bo->acc_size = acc_size;
1347 bo->sg = sg;
1348 if (resv) {
1349 bo->base.resv = resv;
1350 dma_resv_assert_held(bo->base.resv);
1351 } else {
1352 bo->base.resv = &bo->base._resv;
1353 }
1354 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1355
1356
1357
1358
1359 dma_resv_init(&bo->base._resv);
1360 drm_vma_node_reset(&bo->base.vma_node);
1361 }
1362 atomic_inc(&bo->bdev->glob->bo_count);
1363
1364
1365
1366
1367
1368 if (bo->type == ttm_bo_type_device ||
1369 bo->type == ttm_bo_type_sg)
1370 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->base.vma_node,
1371 bo->mem.num_pages);
1372
1373
1374
1375
1376 if (!resv) {
1377 locked = dma_resv_trylock(bo->base.resv);
1378 WARN_ON(!locked);
1379 }
1380
1381 if (likely(!ret))
1382 ret = ttm_bo_validate(bo, placement, ctx);
1383
1384 if (unlikely(ret)) {
1385 if (!resv)
1386 ttm_bo_unreserve(bo);
1387
1388 ttm_bo_put(bo);
1389 return ret;
1390 }
1391
1392 if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1393 spin_lock(&bdev->glob->lru_lock);
1394 ttm_bo_add_to_lru(bo);
1395 spin_unlock(&bdev->glob->lru_lock);
1396 }
1397
1398 return ret;
1399 }
1400 EXPORT_SYMBOL(ttm_bo_init_reserved);
1401
1402 int ttm_bo_init(struct ttm_bo_device *bdev,
1403 struct ttm_buffer_object *bo,
1404 unsigned long size,
1405 enum ttm_bo_type type,
1406 struct ttm_placement *placement,
1407 uint32_t page_alignment,
1408 bool interruptible,
1409 size_t acc_size,
1410 struct sg_table *sg,
1411 struct dma_resv *resv,
1412 void (*destroy) (struct ttm_buffer_object *))
1413 {
1414 struct ttm_operation_ctx ctx = { interruptible, false };
1415 int ret;
1416
1417 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1418 page_alignment, &ctx, acc_size,
1419 sg, resv, destroy);
1420 if (ret)
1421 return ret;
1422
1423 if (!resv)
1424 ttm_bo_unreserve(bo);
1425
1426 return 0;
1427 }
1428 EXPORT_SYMBOL(ttm_bo_init);
1429
1430 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1431 unsigned long bo_size,
1432 unsigned struct_size)
1433 {
1434 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1435 size_t size = 0;
1436
1437 size += ttm_round_pot(struct_size);
1438 size += ttm_round_pot(npages * sizeof(void *));
1439 size += ttm_round_pot(sizeof(struct ttm_tt));
1440 return size;
1441 }
1442 EXPORT_SYMBOL(ttm_bo_acc_size);
1443
1444 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1445 unsigned long bo_size,
1446 unsigned struct_size)
1447 {
1448 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1449 size_t size = 0;
1450
1451 size += ttm_round_pot(struct_size);
1452 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1453 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1454 return size;
1455 }
1456 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1457
1458 int ttm_bo_create(struct ttm_bo_device *bdev,
1459 unsigned long size,
1460 enum ttm_bo_type type,
1461 struct ttm_placement *placement,
1462 uint32_t page_alignment,
1463 bool interruptible,
1464 struct ttm_buffer_object **p_bo)
1465 {
1466 struct ttm_buffer_object *bo;
1467 size_t acc_size;
1468 int ret;
1469
1470 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1471 if (unlikely(bo == NULL))
1472 return -ENOMEM;
1473
1474 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1475 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1476 interruptible, acc_size,
1477 NULL, NULL, NULL);
1478 if (likely(ret == 0))
1479 *p_bo = bo;
1480
1481 return ret;
1482 }
1483 EXPORT_SYMBOL(ttm_bo_create);
1484
1485 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1486 unsigned mem_type)
1487 {
1488 struct ttm_operation_ctx ctx = {
1489 .interruptible = false,
1490 .no_wait_gpu = false,
1491 .flags = TTM_OPT_FLAG_FORCE_ALLOC
1492 };
1493 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1494 struct ttm_bo_global *glob = bdev->glob;
1495 struct dma_fence *fence;
1496 int ret;
1497 unsigned i;
1498
1499
1500
1501
1502
1503 spin_lock(&glob->lru_lock);
1504 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1505 while (!list_empty(&man->lru[i])) {
1506 spin_unlock(&glob->lru_lock);
1507 ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx,
1508 NULL);
1509 if (ret)
1510 return ret;
1511 spin_lock(&glob->lru_lock);
1512 }
1513 }
1514 spin_unlock(&glob->lru_lock);
1515
1516 spin_lock(&man->move_lock);
1517 fence = dma_fence_get(man->move);
1518 spin_unlock(&man->move_lock);
1519
1520 if (fence) {
1521 ret = dma_fence_wait(fence, false);
1522 dma_fence_put(fence);
1523 if (ret)
1524 return ret;
1525 }
1526
1527 return 0;
1528 }
1529
1530 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1531 {
1532 struct ttm_mem_type_manager *man;
1533 int ret = -EINVAL;
1534
1535 if (mem_type >= TTM_NUM_MEM_TYPES) {
1536 pr_err("Illegal memory type %d\n", mem_type);
1537 return ret;
1538 }
1539 man = &bdev->man[mem_type];
1540
1541 if (!man->has_type) {
1542 pr_err("Trying to take down uninitialized memory manager type %u\n",
1543 mem_type);
1544 return ret;
1545 }
1546
1547 man->use_type = false;
1548 man->has_type = false;
1549
1550 ret = 0;
1551 if (mem_type > 0) {
1552 ret = ttm_bo_force_list_clean(bdev, mem_type);
1553 if (ret) {
1554 pr_err("Cleanup eviction failed\n");
1555 return ret;
1556 }
1557
1558 ret = (*man->func->takedown)(man);
1559 }
1560
1561 dma_fence_put(man->move);
1562 man->move = NULL;
1563
1564 return ret;
1565 }
1566 EXPORT_SYMBOL(ttm_bo_clean_mm);
1567
1568 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1569 {
1570 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1571
1572 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1573 pr_err("Illegal memory manager memory type %u\n", mem_type);
1574 return -EINVAL;
1575 }
1576
1577 if (!man->has_type) {
1578 pr_err("Memory type %u has not been initialized\n", mem_type);
1579 return 0;
1580 }
1581
1582 return ttm_bo_force_list_clean(bdev, mem_type);
1583 }
1584 EXPORT_SYMBOL(ttm_bo_evict_mm);
1585
1586 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1587 unsigned long p_size)
1588 {
1589 int ret;
1590 struct ttm_mem_type_manager *man;
1591 unsigned i;
1592
1593 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1594 man = &bdev->man[type];
1595 BUG_ON(man->has_type);
1596 man->io_reserve_fastpath = true;
1597 man->use_io_reserve_lru = false;
1598 mutex_init(&man->io_reserve_mutex);
1599 spin_lock_init(&man->move_lock);
1600 INIT_LIST_HEAD(&man->io_reserve_lru);
1601
1602 ret = bdev->driver->init_mem_type(bdev, type, man);
1603 if (ret)
1604 return ret;
1605 man->bdev = bdev;
1606
1607 if (type != TTM_PL_SYSTEM) {
1608 ret = (*man->func->init)(man, p_size);
1609 if (ret)
1610 return ret;
1611 }
1612 man->has_type = true;
1613 man->use_type = true;
1614 man->size = p_size;
1615
1616 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1617 INIT_LIST_HEAD(&man->lru[i]);
1618 man->move = NULL;
1619
1620 return 0;
1621 }
1622 EXPORT_SYMBOL(ttm_bo_init_mm);
1623
1624 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1625 {
1626 struct ttm_bo_global *glob =
1627 container_of(kobj, struct ttm_bo_global, kobj);
1628
1629 __free_page(glob->dummy_read_page);
1630 }
1631
1632 static void ttm_bo_global_release(void)
1633 {
1634 struct ttm_bo_global *glob = &ttm_bo_glob;
1635
1636 mutex_lock(&ttm_global_mutex);
1637 if (--ttm_bo_glob_use_count > 0)
1638 goto out;
1639
1640 kobject_del(&glob->kobj);
1641 kobject_put(&glob->kobj);
1642 ttm_mem_global_release(&ttm_mem_glob);
1643 memset(glob, 0, sizeof(*glob));
1644 out:
1645 mutex_unlock(&ttm_global_mutex);
1646 }
1647
1648 static int ttm_bo_global_init(void)
1649 {
1650 struct ttm_bo_global *glob = &ttm_bo_glob;
1651 int ret = 0;
1652 unsigned i;
1653
1654 mutex_lock(&ttm_global_mutex);
1655 if (++ttm_bo_glob_use_count > 1)
1656 goto out;
1657
1658 ret = ttm_mem_global_init(&ttm_mem_glob);
1659 if (ret)
1660 goto out;
1661
1662 spin_lock_init(&glob->lru_lock);
1663 glob->mem_glob = &ttm_mem_glob;
1664 glob->mem_glob->bo_glob = glob;
1665 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1666
1667 if (unlikely(glob->dummy_read_page == NULL)) {
1668 ret = -ENOMEM;
1669 goto out;
1670 }
1671
1672 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1673 INIT_LIST_HEAD(&glob->swap_lru[i]);
1674 INIT_LIST_HEAD(&glob->device_list);
1675 atomic_set(&glob->bo_count, 0);
1676
1677 ret = kobject_init_and_add(
1678 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1679 if (unlikely(ret != 0))
1680 kobject_put(&glob->kobj);
1681 out:
1682 mutex_unlock(&ttm_global_mutex);
1683 return ret;
1684 }
1685
1686 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1687 {
1688 int ret = 0;
1689 unsigned i = TTM_NUM_MEM_TYPES;
1690 struct ttm_mem_type_manager *man;
1691 struct ttm_bo_global *glob = bdev->glob;
1692
1693 while (i--) {
1694 man = &bdev->man[i];
1695 if (man->has_type) {
1696 man->use_type = false;
1697 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1698 ret = -EBUSY;
1699 pr_err("DRM memory manager type %d is not clean\n",
1700 i);
1701 }
1702 man->has_type = false;
1703 }
1704 }
1705
1706 mutex_lock(&ttm_global_mutex);
1707 list_del(&bdev->device_list);
1708 mutex_unlock(&ttm_global_mutex);
1709
1710 cancel_delayed_work_sync(&bdev->wq);
1711
1712 if (ttm_bo_delayed_delete(bdev, true))
1713 pr_debug("Delayed destroy list was clean\n");
1714
1715 spin_lock(&glob->lru_lock);
1716 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1717 if (list_empty(&bdev->man[0].lru[0]))
1718 pr_debug("Swap list %d was clean\n", i);
1719 spin_unlock(&glob->lru_lock);
1720
1721 drm_vma_offset_manager_destroy(&bdev->vma_manager);
1722
1723 if (!ret)
1724 ttm_bo_global_release();
1725
1726 return ret;
1727 }
1728 EXPORT_SYMBOL(ttm_bo_device_release);
1729
1730 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1731 struct ttm_bo_driver *driver,
1732 struct address_space *mapping,
1733 bool need_dma32)
1734 {
1735 struct ttm_bo_global *glob = &ttm_bo_glob;
1736 int ret;
1737
1738 ret = ttm_bo_global_init();
1739 if (ret)
1740 return ret;
1741
1742 bdev->driver = driver;
1743
1744 memset(bdev->man, 0, sizeof(bdev->man));
1745
1746
1747
1748
1749
1750 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1751 if (unlikely(ret != 0))
1752 goto out_no_sys;
1753
1754 drm_vma_offset_manager_init(&bdev->vma_manager,
1755 DRM_FILE_PAGE_OFFSET_START,
1756 DRM_FILE_PAGE_OFFSET_SIZE);
1757 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1758 INIT_LIST_HEAD(&bdev->ddestroy);
1759 bdev->dev_mapping = mapping;
1760 bdev->glob = glob;
1761 bdev->need_dma32 = need_dma32;
1762 mutex_lock(&ttm_global_mutex);
1763 list_add_tail(&bdev->device_list, &glob->device_list);
1764 mutex_unlock(&ttm_global_mutex);
1765
1766 return 0;
1767 out_no_sys:
1768 ttm_bo_global_release();
1769 return ret;
1770 }
1771 EXPORT_SYMBOL(ttm_bo_device_init);
1772
1773
1774
1775
1776
1777 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1778 {
1779 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1780
1781 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1782 if (mem->mem_type == TTM_PL_SYSTEM)
1783 return false;
1784
1785 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1786 return false;
1787
1788 if (mem->placement & TTM_PL_FLAG_CACHED)
1789 return false;
1790 }
1791 return true;
1792 }
1793
1794 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1795 {
1796 struct ttm_bo_device *bdev = bo->bdev;
1797
1798 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1799 ttm_mem_io_free_vm(bo);
1800 }
1801
1802 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1803 {
1804 struct ttm_bo_device *bdev = bo->bdev;
1805 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1806
1807 ttm_mem_io_lock(man, false);
1808 ttm_bo_unmap_virtual_locked(bo);
1809 ttm_mem_io_unlock(man);
1810 }
1811
1812
1813 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1814
1815 int ttm_bo_wait(struct ttm_buffer_object *bo,
1816 bool interruptible, bool no_wait)
1817 {
1818 long timeout = 15 * HZ;
1819
1820 if (no_wait) {
1821 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1822 return 0;
1823 else
1824 return -EBUSY;
1825 }
1826
1827 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1828 interruptible, timeout);
1829 if (timeout < 0)
1830 return timeout;
1831
1832 if (timeout == 0)
1833 return -EBUSY;
1834
1835 dma_resv_add_excl_fence(bo->base.resv, NULL);
1836 return 0;
1837 }
1838 EXPORT_SYMBOL(ttm_bo_wait);
1839
1840 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1841 {
1842 int ret = 0;
1843
1844
1845
1846
1847
1848 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
1849 if (unlikely(ret != 0))
1850 return ret;
1851 ret = ttm_bo_wait(bo, true, no_wait);
1852 if (likely(ret == 0))
1853 atomic_inc(&bo->cpu_writers);
1854 ttm_bo_unreserve(bo);
1855 return ret;
1856 }
1857 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1858
1859 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1860 {
1861 atomic_dec(&bo->cpu_writers);
1862 }
1863 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1864
1865
1866
1867
1868
1869 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1870 {
1871 struct ttm_buffer_object *bo;
1872 int ret = -EBUSY;
1873 bool locked;
1874 unsigned i;
1875
1876 spin_lock(&glob->lru_lock);
1877 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1878 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1879 if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1880 NULL)) {
1881 ret = 0;
1882 break;
1883 }
1884 }
1885 if (!ret)
1886 break;
1887 }
1888
1889 if (ret) {
1890 spin_unlock(&glob->lru_lock);
1891 return ret;
1892 }
1893
1894 kref_get(&bo->list_kref);
1895
1896 if (!list_empty(&bo->ddestroy)) {
1897 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1898 kref_put(&bo->list_kref, ttm_bo_release_list);
1899 return ret;
1900 }
1901
1902 ttm_bo_del_from_lru(bo);
1903 spin_unlock(&glob->lru_lock);
1904
1905
1906
1907
1908
1909 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1910 bo->ttm->caching_state != tt_cached) {
1911 struct ttm_operation_ctx ctx = { false, false };
1912 struct ttm_mem_reg evict_mem;
1913
1914 evict_mem = bo->mem;
1915 evict_mem.mm_node = NULL;
1916 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1917 evict_mem.mem_type = TTM_PL_SYSTEM;
1918
1919 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1920 if (unlikely(ret != 0))
1921 goto out;
1922 }
1923
1924
1925
1926
1927
1928 ret = ttm_bo_wait(bo, false, false);
1929 if (unlikely(ret != 0))
1930 goto out;
1931
1932 ttm_bo_unmap_virtual(bo);
1933
1934
1935
1936
1937
1938
1939 if (bo->bdev->driver->swap_notify)
1940 bo->bdev->driver->swap_notify(bo);
1941
1942 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1943 out:
1944
1945
1946
1947
1948
1949
1950 if (locked)
1951 dma_resv_unlock(bo->base.resv);
1952 kref_put(&bo->list_kref, ttm_bo_release_list);
1953 return ret;
1954 }
1955 EXPORT_SYMBOL(ttm_bo_swapout);
1956
1957 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1958 {
1959 struct ttm_operation_ctx ctx = {
1960 .interruptible = false,
1961 .no_wait_gpu = false
1962 };
1963
1964 while (ttm_bo_swapout(bdev->glob, &ctx) == 0)
1965 ;
1966 }
1967 EXPORT_SYMBOL(ttm_bo_swapout_all);
1968
1969
1970
1971
1972
1973
1974
1975 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1976 {
1977 int ret;
1978
1979
1980
1981
1982
1983
1984
1985
1986 ret = mutex_lock_interruptible(&bo->wu_mutex);
1987 if (unlikely(ret != 0))
1988 return -ERESTARTSYS;
1989 if (!dma_resv_is_locked(bo->base.resv))
1990 goto out_unlock;
1991 ret = dma_resv_lock_interruptible(bo->base.resv, NULL);
1992 if (ret == -EINTR)
1993 ret = -ERESTARTSYS;
1994 if (unlikely(ret != 0))
1995 goto out_unlock;
1996 dma_resv_unlock(bo->base.resv);
1997
1998 out_unlock:
1999 mutex_unlock(&bo->wu_mutex);
2000 return ret;
2001 }