1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3 *
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28 /*
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
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 * ttm_global_mutex - protecting the global BO state
50 */
51 DEFINE_MUTEX(ttm_global_mutex);
52 unsigned ttm_bo_glob_use_count;
53 struct ttm_bo_global ttm_bo_glob;
54 EXPORT_SYMBOL(ttm_bo_glob);
55
56 static struct attribute ttm_bo_count = {
57 .name = "bo_count",
58 .mode = S_IRUGO
59 };
60
61 /* default destructor */
ttm_bo_default_destroy(struct ttm_buffer_object * bo)62 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
63 {
64 kfree(bo);
65 }
66
ttm_bo_mem_space_debug(struct ttm_buffer_object * bo,struct ttm_placement * placement)67 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
68 struct ttm_placement *placement)
69 {
70 struct drm_printer p = drm_debug_printer(TTM_PFX);
71 struct ttm_resource_manager *man;
72 int i, mem_type;
73
74 drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
75 bo, bo->mem.num_pages, bo->mem.size >> 10,
76 bo->mem.size >> 20);
77 for (i = 0; i < placement->num_placement; i++) {
78 mem_type = placement->placement[i].mem_type;
79 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
80 i, placement->placement[i].flags, mem_type);
81 man = ttm_manager_type(bo->bdev, mem_type);
82 ttm_resource_manager_debug(man, &p);
83 }
84 }
85
ttm_bo_global_show(struct kobject * kobj,struct attribute * attr,char * buffer)86 static ssize_t ttm_bo_global_show(struct kobject *kobj,
87 struct attribute *attr,
88 char *buffer)
89 {
90 struct ttm_bo_global *glob =
91 container_of(kobj, struct ttm_bo_global, kobj);
92
93 return snprintf(buffer, PAGE_SIZE, "%d\n",
94 atomic_read(&glob->bo_count));
95 }
96
97 static struct attribute *ttm_bo_global_attrs[] = {
98 &ttm_bo_count,
99 NULL
100 };
101
102 static const struct sysfs_ops ttm_bo_global_ops = {
103 .show = &ttm_bo_global_show
104 };
105
106 static struct kobj_type ttm_bo_glob_kobj_type = {
107 .release = &ttm_bo_global_kobj_release,
108 .sysfs_ops = &ttm_bo_global_ops,
109 .default_attrs = ttm_bo_global_attrs
110 };
111
ttm_bo_add_mem_to_lru(struct ttm_buffer_object * bo,struct ttm_resource * mem)112 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
113 struct ttm_resource *mem)
114 {
115 struct ttm_bo_device *bdev = bo->bdev;
116 struct ttm_resource_manager *man;
117
118 if (!list_empty(&bo->lru))
119 return;
120
121 if (mem->placement & TTM_PL_FLAG_NO_EVICT)
122 return;
123
124 man = ttm_manager_type(bdev, mem->mem_type);
125 list_add_tail(&bo->lru, &man->lru[bo->priority]);
126
127 if (man->use_tt && bo->ttm &&
128 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
129 TTM_PAGE_FLAG_SWAPPED))) {
130 list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]);
131 }
132 }
133
ttm_bo_del_from_lru(struct ttm_buffer_object * bo)134 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
135 {
136 struct ttm_bo_device *bdev = bo->bdev;
137 bool notify = false;
138
139 if (!list_empty(&bo->swap)) {
140 list_del_init(&bo->swap);
141 notify = true;
142 }
143 if (!list_empty(&bo->lru)) {
144 list_del_init(&bo->lru);
145 notify = true;
146 }
147
148 if (notify && bdev->driver->del_from_lru_notify)
149 bdev->driver->del_from_lru_notify(bo);
150 }
151
ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos * pos,struct ttm_buffer_object * bo)152 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
153 struct ttm_buffer_object *bo)
154 {
155 if (!pos->first)
156 pos->first = bo;
157 pos->last = bo;
158 }
159
ttm_bo_move_to_lru_tail(struct ttm_buffer_object * bo,struct ttm_lru_bulk_move * bulk)160 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
161 struct ttm_lru_bulk_move *bulk)
162 {
163 dma_resv_assert_held(bo->base.resv);
164
165 ttm_bo_del_from_lru(bo);
166 ttm_bo_add_mem_to_lru(bo, &bo->mem);
167
168 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
169 switch (bo->mem.mem_type) {
170 case TTM_PL_TT:
171 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
172 break;
173
174 case TTM_PL_VRAM:
175 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
176 break;
177 }
178 if (bo->ttm && !(bo->ttm->page_flags &
179 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
180 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
181 }
182 }
183 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
184
ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move * bulk)185 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
186 {
187 unsigned i;
188
189 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
190 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
191 struct ttm_resource_manager *man;
192
193 if (!pos->first)
194 continue;
195
196 dma_resv_assert_held(pos->first->base.resv);
197 dma_resv_assert_held(pos->last->base.resv);
198
199 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
200 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
201 &pos->last->lru);
202 }
203
204 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
205 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
206 struct ttm_resource_manager *man;
207
208 if (!pos->first)
209 continue;
210
211 dma_resv_assert_held(pos->first->base.resv);
212 dma_resv_assert_held(pos->last->base.resv);
213
214 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
215 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
216 &pos->last->lru);
217 }
218
219 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
220 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
221 struct list_head *lru;
222
223 if (!pos->first)
224 continue;
225
226 dma_resv_assert_held(pos->first->base.resv);
227 dma_resv_assert_held(pos->last->base.resv);
228
229 lru = &ttm_bo_glob.swap_lru[i];
230 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
231 }
232 }
233 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
234
ttm_bo_handle_move_mem(struct ttm_buffer_object * bo,struct ttm_resource * mem,bool evict,struct ttm_operation_ctx * ctx)235 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
236 struct ttm_resource *mem, bool evict,
237 struct ttm_operation_ctx *ctx)
238 {
239 struct ttm_bo_device *bdev = bo->bdev;
240 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
241 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
242 int ret;
243
244 ttm_bo_unmap_virtual(bo);
245
246 /*
247 * Create and bind a ttm if required.
248 */
249
250 if (new_man->use_tt) {
251 /* Zero init the new TTM structure if the old location should
252 * have used one as well.
253 */
254 ret = ttm_tt_create(bo, old_man->use_tt);
255 if (ret)
256 goto out_err;
257
258 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
259 if (ret)
260 goto out_err;
261
262 if (mem->mem_type != TTM_PL_SYSTEM) {
263 ret = ttm_tt_populate(bdev, bo->ttm, ctx);
264 if (ret)
265 goto out_err;
266
267 ret = ttm_bo_tt_bind(bo, mem);
268 if (ret)
269 goto out_err;
270 }
271
272 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
273 if (bdev->driver->move_notify)
274 bdev->driver->move_notify(bo, evict, mem);
275 bo->mem = *mem;
276 goto moved;
277 }
278 }
279
280 if (bdev->driver->move_notify)
281 bdev->driver->move_notify(bo, evict, mem);
282
283 if (old_man->use_tt && new_man->use_tt)
284 ret = ttm_bo_move_ttm(bo, ctx, mem);
285 else if (bdev->driver->move)
286 ret = bdev->driver->move(bo, evict, ctx, mem);
287 else
288 ret = ttm_bo_move_memcpy(bo, ctx, mem);
289
290 if (ret) {
291 if (bdev->driver->move_notify) {
292 swap(*mem, bo->mem);
293 bdev->driver->move_notify(bo, false, mem);
294 swap(*mem, bo->mem);
295 }
296
297 goto out_err;
298 }
299
300 moved:
301 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
302 return 0;
303
304 out_err:
305 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
306 if (!new_man->use_tt)
307 ttm_bo_tt_destroy(bo);
308
309 return ret;
310 }
311
312 /**
313 * Call bo::reserved.
314 * Will release GPU memory type usage on destruction.
315 * This is the place to put in driver specific hooks to release
316 * driver private resources.
317 * Will release the bo::reserved lock.
318 */
319
ttm_bo_cleanup_memtype_use(struct ttm_buffer_object * bo)320 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
321 {
322 if (bo->bdev->driver->move_notify)
323 bo->bdev->driver->move_notify(bo, false, NULL);
324
325 ttm_bo_tt_destroy(bo);
326 ttm_resource_free(bo, &bo->mem);
327 }
328
ttm_bo_individualize_resv(struct ttm_buffer_object * bo)329 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
330 {
331 int r;
332
333 if (bo->base.resv == &bo->base._resv)
334 return 0;
335
336 BUG_ON(!dma_resv_trylock(&bo->base._resv));
337
338 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
339 dma_resv_unlock(&bo->base._resv);
340 if (r)
341 return r;
342
343 if (bo->type != ttm_bo_type_sg) {
344 /* This works because the BO is about to be destroyed and nobody
345 * reference it any more. The only tricky case is the trylock on
346 * the resv object while holding the lru_lock.
347 */
348 spin_lock(&ttm_bo_glob.lru_lock);
349 bo->base.resv = &bo->base._resv;
350 spin_unlock(&ttm_bo_glob.lru_lock);
351 }
352
353 return r;
354 }
355
ttm_bo_flush_all_fences(struct ttm_buffer_object * bo)356 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
357 {
358 struct dma_resv *resv = &bo->base._resv;
359 struct dma_resv_list *fobj;
360 struct dma_fence *fence;
361 int i;
362
363 rcu_read_lock();
364 fobj = rcu_dereference(resv->fence);
365 fence = rcu_dereference(resv->fence_excl);
366 if (fence && !fence->ops->signaled)
367 dma_fence_enable_sw_signaling(fence);
368
369 for (i = 0; fobj && i < fobj->shared_count; ++i) {
370 fence = rcu_dereference(fobj->shared[i]);
371
372 if (!fence->ops->signaled)
373 dma_fence_enable_sw_signaling(fence);
374 }
375 rcu_read_unlock();
376 }
377
378 /**
379 * function ttm_bo_cleanup_refs
380 * If bo idle, remove from lru lists, and unref.
381 * If not idle, block if possible.
382 *
383 * Must be called with lru_lock and reservation held, this function
384 * will drop the lru lock and optionally the reservation lock before returning.
385 *
386 * @interruptible Any sleeps should occur interruptibly.
387 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
388 * @unlock_resv Unlock the reservation lock as well.
389 */
390
ttm_bo_cleanup_refs(struct ttm_buffer_object * bo,bool interruptible,bool no_wait_gpu,bool unlock_resv)391 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
392 bool interruptible, bool no_wait_gpu,
393 bool unlock_resv)
394 {
395 struct dma_resv *resv = &bo->base._resv;
396 int ret;
397
398 if (dma_resv_test_signaled_rcu(resv, true))
399 ret = 0;
400 else
401 ret = -EBUSY;
402
403 if (ret && !no_wait_gpu) {
404 long lret;
405
406 if (unlock_resv)
407 dma_resv_unlock(bo->base.resv);
408 spin_unlock(&ttm_bo_glob.lru_lock);
409
410 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
411 30 * HZ);
412
413 if (lret < 0)
414 return lret;
415 else if (lret == 0)
416 return -EBUSY;
417
418 spin_lock(&ttm_bo_glob.lru_lock);
419 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
420 /*
421 * We raced, and lost, someone else holds the reservation now,
422 * and is probably busy in ttm_bo_cleanup_memtype_use.
423 *
424 * Even if it's not the case, because we finished waiting any
425 * delayed destruction would succeed, so just return success
426 * here.
427 */
428 spin_unlock(&ttm_bo_glob.lru_lock);
429 return 0;
430 }
431 ret = 0;
432 }
433
434 if (ret || unlikely(list_empty(&bo->ddestroy))) {
435 if (unlock_resv)
436 dma_resv_unlock(bo->base.resv);
437 spin_unlock(&ttm_bo_glob.lru_lock);
438 return ret;
439 }
440
441 ttm_bo_del_from_lru(bo);
442 list_del_init(&bo->ddestroy);
443 spin_unlock(&ttm_bo_glob.lru_lock);
444 ttm_bo_cleanup_memtype_use(bo);
445
446 if (unlock_resv)
447 dma_resv_unlock(bo->base.resv);
448
449 ttm_bo_put(bo);
450
451 return 0;
452 }
453
454 /**
455 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
456 * encountered buffers.
457 */
ttm_bo_delayed_delete(struct ttm_bo_device * bdev,bool remove_all)458 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
459 {
460 struct ttm_bo_global *glob = &ttm_bo_glob;
461 struct list_head removed;
462 bool empty;
463
464 INIT_LIST_HEAD(&removed);
465
466 spin_lock(&glob->lru_lock);
467 while (!list_empty(&bdev->ddestroy)) {
468 struct ttm_buffer_object *bo;
469
470 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
471 ddestroy);
472 list_move_tail(&bo->ddestroy, &removed);
473 if (!ttm_bo_get_unless_zero(bo))
474 continue;
475
476 if (remove_all || bo->base.resv != &bo->base._resv) {
477 spin_unlock(&glob->lru_lock);
478 dma_resv_lock(bo->base.resv, NULL);
479
480 spin_lock(&glob->lru_lock);
481 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
482
483 } else if (dma_resv_trylock(bo->base.resv)) {
484 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
485 } else {
486 spin_unlock(&glob->lru_lock);
487 }
488
489 ttm_bo_put(bo);
490 spin_lock(&glob->lru_lock);
491 }
492 list_splice_tail(&removed, &bdev->ddestroy);
493 empty = list_empty(&bdev->ddestroy);
494 spin_unlock(&glob->lru_lock);
495
496 return empty;
497 }
498
ttm_bo_delayed_workqueue(struct work_struct * work)499 static void ttm_bo_delayed_workqueue(struct work_struct *work)
500 {
501 struct ttm_bo_device *bdev =
502 container_of(work, struct ttm_bo_device, wq.work);
503
504 if (!ttm_bo_delayed_delete(bdev, false))
505 schedule_delayed_work(&bdev->wq,
506 ((HZ / 100) < 1) ? 1 : HZ / 100);
507 }
508
ttm_bo_release(struct kref * kref)509 static void ttm_bo_release(struct kref *kref)
510 {
511 struct ttm_buffer_object *bo =
512 container_of(kref, struct ttm_buffer_object, kref);
513 struct ttm_bo_device *bdev = bo->bdev;
514 size_t acc_size = bo->acc_size;
515 int ret;
516
517 if (!bo->deleted) {
518 ret = ttm_bo_individualize_resv(bo);
519 if (ret) {
520 /* Last resort, if we fail to allocate memory for the
521 * fences block for the BO to become idle
522 */
523 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
524 30 * HZ);
525 }
526
527 if (bo->bdev->driver->release_notify)
528 bo->bdev->driver->release_notify(bo);
529
530 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
531 ttm_mem_io_free(bdev, &bo->mem);
532 }
533
534 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
535 !dma_resv_trylock(bo->base.resv)) {
536 /* The BO is not idle, resurrect it for delayed destroy */
537 ttm_bo_flush_all_fences(bo);
538 bo->deleted = true;
539
540 spin_lock(&ttm_bo_glob.lru_lock);
541
542 /*
543 * Make NO_EVICT bos immediately available to
544 * shrinkers, now that they are queued for
545 * destruction.
546 */
547 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
548 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
549 ttm_bo_del_from_lru(bo);
550 ttm_bo_add_mem_to_lru(bo, &bo->mem);
551 }
552
553 kref_init(&bo->kref);
554 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
555 spin_unlock(&ttm_bo_glob.lru_lock);
556
557 schedule_delayed_work(&bdev->wq,
558 ((HZ / 100) < 1) ? 1 : HZ / 100);
559 return;
560 }
561
562 spin_lock(&ttm_bo_glob.lru_lock);
563 ttm_bo_del_from_lru(bo);
564 list_del(&bo->ddestroy);
565 spin_unlock(&ttm_bo_glob.lru_lock);
566
567 ttm_bo_cleanup_memtype_use(bo);
568 dma_resv_unlock(bo->base.resv);
569
570 atomic_dec(&ttm_bo_glob.bo_count);
571 dma_fence_put(bo->moving);
572 if (!ttm_bo_uses_embedded_gem_object(bo))
573 dma_resv_fini(&bo->base._resv);
574 bo->destroy(bo);
575 ttm_mem_global_free(&ttm_mem_glob, acc_size);
576 }
577
ttm_bo_put(struct ttm_buffer_object * bo)578 void ttm_bo_put(struct ttm_buffer_object *bo)
579 {
580 kref_put(&bo->kref, ttm_bo_release);
581 }
582 EXPORT_SYMBOL(ttm_bo_put);
583
ttm_bo_lock_delayed_workqueue(struct ttm_bo_device * bdev)584 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
585 {
586 return cancel_delayed_work_sync(&bdev->wq);
587 }
588 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
589
ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device * bdev,int resched)590 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
591 {
592 if (resched)
593 schedule_delayed_work(&bdev->wq,
594 ((HZ / 100) < 1) ? 1 : HZ / 100);
595 }
596 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
597
ttm_bo_evict(struct ttm_buffer_object * bo,struct ttm_operation_ctx * ctx)598 static int ttm_bo_evict(struct ttm_buffer_object *bo,
599 struct ttm_operation_ctx *ctx)
600 {
601 struct ttm_bo_device *bdev = bo->bdev;
602 struct ttm_resource evict_mem;
603 struct ttm_placement placement;
604 int ret = 0;
605
606 dma_resv_assert_held(bo->base.resv);
607
608 placement.num_placement = 0;
609 placement.num_busy_placement = 0;
610 bdev->driver->evict_flags(bo, &placement);
611
612 if (!placement.num_placement && !placement.num_busy_placement) {
613 ttm_bo_wait(bo, false, false);
614
615 ttm_bo_cleanup_memtype_use(bo);
616 return ttm_tt_create(bo, false);
617 }
618
619 evict_mem = bo->mem;
620 evict_mem.mm_node = NULL;
621 evict_mem.bus.offset = 0;
622 evict_mem.bus.addr = NULL;
623
624 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
625 if (ret) {
626 if (ret != -ERESTARTSYS) {
627 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
628 bo);
629 ttm_bo_mem_space_debug(bo, &placement);
630 }
631 goto out;
632 }
633
634 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
635 if (unlikely(ret)) {
636 if (ret != -ERESTARTSYS)
637 pr_err("Buffer eviction failed\n");
638 ttm_resource_free(bo, &evict_mem);
639 }
640 out:
641 return ret;
642 }
643
ttm_bo_eviction_valuable(struct ttm_buffer_object * bo,const struct ttm_place * place)644 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
645 const struct ttm_place *place)
646 {
647 /* Don't evict this BO if it's outside of the
648 * requested placement range
649 */
650 if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) ||
651 (place->lpfn && place->lpfn <= bo->mem.start))
652 return false;
653
654 return true;
655 }
656 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
657
658 /**
659 * Check the target bo is allowable to be evicted or swapout, including cases:
660 *
661 * a. if share same reservation object with ctx->resv, have assumption
662 * reservation objects should already be locked, so not lock again and
663 * return true directly when either the opreation allow_reserved_eviction
664 * or the target bo already is in delayed free list;
665 *
666 * b. Otherwise, trylock it.
667 */
ttm_bo_evict_swapout_allowable(struct ttm_buffer_object * bo,struct ttm_operation_ctx * ctx,bool * locked,bool * busy)668 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
669 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
670 {
671 bool ret = false;
672
673 if (bo->base.resv == ctx->resv) {
674 dma_resv_assert_held(bo->base.resv);
675 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT)
676 ret = true;
677 *locked = false;
678 if (busy)
679 *busy = false;
680 } else {
681 ret = dma_resv_trylock(bo->base.resv);
682 *locked = ret;
683 if (busy)
684 *busy = !ret;
685 }
686
687 return ret;
688 }
689
690 /**
691 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
692 *
693 * @busy_bo: BO which couldn't be locked with trylock
694 * @ctx: operation context
695 * @ticket: acquire ticket
696 *
697 * Try to lock a busy buffer object to avoid failing eviction.
698 */
ttm_mem_evict_wait_busy(struct ttm_buffer_object * busy_bo,struct ttm_operation_ctx * ctx,struct ww_acquire_ctx * ticket)699 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
700 struct ttm_operation_ctx *ctx,
701 struct ww_acquire_ctx *ticket)
702 {
703 int r;
704
705 if (!busy_bo || !ticket)
706 return -EBUSY;
707
708 if (ctx->interruptible)
709 r = dma_resv_lock_interruptible(busy_bo->base.resv,
710 ticket);
711 else
712 r = dma_resv_lock(busy_bo->base.resv, ticket);
713
714 /*
715 * TODO: It would be better to keep the BO locked until allocation is at
716 * least tried one more time, but that would mean a much larger rework
717 * of TTM.
718 */
719 if (!r)
720 dma_resv_unlock(busy_bo->base.resv);
721
722 return r == -EDEADLK ? -EBUSY : r;
723 }
724
ttm_mem_evict_first(struct ttm_bo_device * bdev,struct ttm_resource_manager * man,const struct ttm_place * place,struct ttm_operation_ctx * ctx,struct ww_acquire_ctx * ticket)725 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
726 struct ttm_resource_manager *man,
727 const struct ttm_place *place,
728 struct ttm_operation_ctx *ctx,
729 struct ww_acquire_ctx *ticket)
730 {
731 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
732 bool locked = false;
733 unsigned i;
734 int ret;
735
736 spin_lock(&ttm_bo_glob.lru_lock);
737 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
738 list_for_each_entry(bo, &man->lru[i], lru) {
739 bool busy;
740
741 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
742 &busy)) {
743 if (busy && !busy_bo && ticket !=
744 dma_resv_locking_ctx(bo->base.resv))
745 busy_bo = bo;
746 continue;
747 }
748
749 if (place && !bdev->driver->eviction_valuable(bo,
750 place)) {
751 if (locked)
752 dma_resv_unlock(bo->base.resv);
753 continue;
754 }
755 if (!ttm_bo_get_unless_zero(bo)) {
756 if (locked)
757 dma_resv_unlock(bo->base.resv);
758 continue;
759 }
760 break;
761 }
762
763 /* If the inner loop terminated early, we have our candidate */
764 if (&bo->lru != &man->lru[i])
765 break;
766
767 bo = NULL;
768 }
769
770 if (!bo) {
771 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
772 busy_bo = NULL;
773 spin_unlock(&ttm_bo_glob.lru_lock);
774 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
775 if (busy_bo)
776 ttm_bo_put(busy_bo);
777 return ret;
778 }
779
780 if (bo->deleted) {
781 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
782 ctx->no_wait_gpu, locked);
783 ttm_bo_put(bo);
784 return ret;
785 }
786
787 spin_unlock(&ttm_bo_glob.lru_lock);
788
789 ret = ttm_bo_evict(bo, ctx);
790 if (locked)
791 ttm_bo_unreserve(bo);
792 else
793 ttm_bo_move_to_lru_tail_unlocked(bo);
794
795 ttm_bo_put(bo);
796 return ret;
797 }
798
799 /**
800 * Add the last move fence to the BO and reserve a new shared slot.
801 */
ttm_bo_add_move_fence(struct ttm_buffer_object * bo,struct ttm_resource_manager * man,struct ttm_resource * mem,bool no_wait_gpu)802 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
803 struct ttm_resource_manager *man,
804 struct ttm_resource *mem,
805 bool no_wait_gpu)
806 {
807 struct dma_fence *fence;
808 int ret;
809
810 spin_lock(&man->move_lock);
811 fence = dma_fence_get(man->move);
812 spin_unlock(&man->move_lock);
813
814 if (!fence)
815 return 0;
816
817 if (no_wait_gpu) {
818 dma_fence_put(fence);
819 return -EBUSY;
820 }
821
822 dma_resv_add_shared_fence(bo->base.resv, fence);
823
824 ret = dma_resv_reserve_shared(bo->base.resv, 1);
825 if (unlikely(ret)) {
826 dma_fence_put(fence);
827 return ret;
828 }
829
830 dma_fence_put(bo->moving);
831 bo->moving = fence;
832 return 0;
833 }
834
835 /**
836 * Repeatedly evict memory from the LRU for @mem_type until we create enough
837 * space, or we've evicted everything and there isn't enough space.
838 */
ttm_bo_mem_force_space(struct ttm_buffer_object * bo,const struct ttm_place * place,struct ttm_resource * mem,struct ttm_operation_ctx * ctx)839 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
840 const struct ttm_place *place,
841 struct ttm_resource *mem,
842 struct ttm_operation_ctx *ctx)
843 {
844 struct ttm_bo_device *bdev = bo->bdev;
845 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
846 struct ww_acquire_ctx *ticket;
847 int ret;
848
849 ticket = dma_resv_locking_ctx(bo->base.resv);
850 do {
851 ret = ttm_resource_alloc(bo, place, mem);
852 if (likely(!ret))
853 break;
854 if (unlikely(ret != -ENOSPC))
855 return ret;
856 ret = ttm_mem_evict_first(bdev, man, place, ctx,
857 ticket);
858 if (unlikely(ret != 0))
859 return ret;
860 } while (1);
861
862 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
863 }
864
ttm_bo_select_caching(struct ttm_resource_manager * man,uint32_t cur_placement,uint32_t proposed_placement)865 static uint32_t ttm_bo_select_caching(struct ttm_resource_manager *man,
866 uint32_t cur_placement,
867 uint32_t proposed_placement)
868 {
869 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
870 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
871
872 /**
873 * Keep current caching if possible.
874 */
875
876 if ((cur_placement & caching) != 0)
877 result |= (cur_placement & caching);
878 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
879 result |= TTM_PL_FLAG_CACHED;
880 else if ((TTM_PL_FLAG_WC & caching) != 0)
881 result |= TTM_PL_FLAG_WC;
882 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
883 result |= TTM_PL_FLAG_UNCACHED;
884
885 return result;
886 }
887
888 /**
889 * ttm_bo_mem_placement - check if placement is compatible
890 * @bo: BO to find memory for
891 * @place: where to search
892 * @mem: the memory object to fill in
893 * @ctx: operation context
894 *
895 * Check if placement is compatible and fill in mem structure.
896 * Returns -EBUSY if placement won't work or negative error code.
897 * 0 when placement can be used.
898 */
ttm_bo_mem_placement(struct ttm_buffer_object * bo,const struct ttm_place * place,struct ttm_resource * mem,struct ttm_operation_ctx * ctx)899 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
900 const struct ttm_place *place,
901 struct ttm_resource *mem,
902 struct ttm_operation_ctx *ctx)
903 {
904 struct ttm_bo_device *bdev = bo->bdev;
905 struct ttm_resource_manager *man;
906 uint32_t cur_flags = 0;
907
908 man = ttm_manager_type(bdev, place->mem_type);
909 if (!man || !ttm_resource_manager_used(man))
910 return -EBUSY;
911
912 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
913 place->flags);
914 cur_flags |= place->flags & ~TTM_PL_MASK_CACHING;
915
916 mem->mem_type = place->mem_type;
917 mem->placement = cur_flags;
918
919 spin_lock(&ttm_bo_glob.lru_lock);
920 ttm_bo_del_from_lru(bo);
921 ttm_bo_add_mem_to_lru(bo, mem);
922 spin_unlock(&ttm_bo_glob.lru_lock);
923
924 return 0;
925 }
926
927 /**
928 * Creates space for memory region @mem according to its type.
929 *
930 * This function first searches for free space in compatible memory types in
931 * the priority order defined by the driver. If free space isn't found, then
932 * ttm_bo_mem_force_space is attempted in priority order to evict and find
933 * space.
934 */
ttm_bo_mem_space(struct ttm_buffer_object * bo,struct ttm_placement * placement,struct ttm_resource * mem,struct ttm_operation_ctx * ctx)935 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
936 struct ttm_placement *placement,
937 struct ttm_resource *mem,
938 struct ttm_operation_ctx *ctx)
939 {
940 struct ttm_bo_device *bdev = bo->bdev;
941 bool type_found = false;
942 int i, ret;
943
944 ret = dma_resv_reserve_shared(bo->base.resv, 1);
945 if (unlikely(ret))
946 return ret;
947
948 for (i = 0; i < placement->num_placement; ++i) {
949 const struct ttm_place *place = &placement->placement[i];
950 struct ttm_resource_manager *man;
951
952 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
953 if (ret)
954 continue;
955
956 type_found = true;
957 ret = ttm_resource_alloc(bo, place, mem);
958 if (ret == -ENOSPC)
959 continue;
960 if (unlikely(ret))
961 goto error;
962
963 man = ttm_manager_type(bdev, mem->mem_type);
964 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
965 if (unlikely(ret)) {
966 ttm_resource_free(bo, mem);
967 if (ret == -EBUSY)
968 continue;
969
970 goto error;
971 }
972 return 0;
973 }
974
975 for (i = 0; i < placement->num_busy_placement; ++i) {
976 const struct ttm_place *place = &placement->busy_placement[i];
977
978 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
979 if (ret)
980 continue;
981
982 type_found = true;
983 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
984 if (likely(!ret))
985 return 0;
986
987 if (ret && ret != -EBUSY)
988 goto error;
989 }
990
991 ret = -ENOMEM;
992 if (!type_found) {
993 pr_err(TTM_PFX "No compatible memory type found\n");
994 ret = -EINVAL;
995 }
996
997 error:
998 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
999 ttm_bo_move_to_lru_tail_unlocked(bo);
1000 }
1001
1002 return ret;
1003 }
1004 EXPORT_SYMBOL(ttm_bo_mem_space);
1005
ttm_bo_move_buffer(struct ttm_buffer_object * bo,struct ttm_placement * placement,struct ttm_operation_ctx * ctx)1006 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1007 struct ttm_placement *placement,
1008 struct ttm_operation_ctx *ctx)
1009 {
1010 int ret = 0;
1011 struct ttm_resource mem;
1012
1013 dma_resv_assert_held(bo->base.resv);
1014
1015 mem.num_pages = bo->num_pages;
1016 mem.size = mem.num_pages << PAGE_SHIFT;
1017 mem.page_alignment = bo->mem.page_alignment;
1018 mem.bus.offset = 0;
1019 mem.bus.addr = NULL;
1020 mem.mm_node = NULL;
1021
1022 /*
1023 * Determine where to move the buffer.
1024 */
1025 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1026 if (ret)
1027 goto out_unlock;
1028 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1029 out_unlock:
1030 if (ret)
1031 ttm_resource_free(bo, &mem);
1032 return ret;
1033 }
1034
ttm_bo_places_compat(const struct ttm_place * places,unsigned num_placement,struct ttm_resource * mem,uint32_t * new_flags)1035 static bool ttm_bo_places_compat(const struct ttm_place *places,
1036 unsigned num_placement,
1037 struct ttm_resource *mem,
1038 uint32_t *new_flags)
1039 {
1040 unsigned i;
1041
1042 for (i = 0; i < num_placement; i++) {
1043 const struct ttm_place *heap = &places[i];
1044
1045 if ((mem->start < heap->fpfn ||
1046 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1047 continue;
1048
1049 *new_flags = heap->flags;
1050 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1051 (mem->mem_type == heap->mem_type) &&
1052 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1053 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1054 return true;
1055 }
1056 return false;
1057 }
1058
ttm_bo_mem_compat(struct ttm_placement * placement,struct ttm_resource * mem,uint32_t * new_flags)1059 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1060 struct ttm_resource *mem,
1061 uint32_t *new_flags)
1062 {
1063 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1064 mem, new_flags))
1065 return true;
1066
1067 if ((placement->busy_placement != placement->placement ||
1068 placement->num_busy_placement > placement->num_placement) &&
1069 ttm_bo_places_compat(placement->busy_placement,
1070 placement->num_busy_placement,
1071 mem, new_flags))
1072 return true;
1073
1074 return false;
1075 }
1076 EXPORT_SYMBOL(ttm_bo_mem_compat);
1077
ttm_bo_validate(struct ttm_buffer_object * bo,struct ttm_placement * placement,struct ttm_operation_ctx * ctx)1078 int ttm_bo_validate(struct ttm_buffer_object *bo,
1079 struct ttm_placement *placement,
1080 struct ttm_operation_ctx *ctx)
1081 {
1082 int ret;
1083 uint32_t new_flags;
1084
1085 dma_resv_assert_held(bo->base.resv);
1086
1087 /*
1088 * Remove the backing store if no placement is given.
1089 */
1090 if (!placement->num_placement && !placement->num_busy_placement) {
1091 ret = ttm_bo_pipeline_gutting(bo);
1092 if (ret)
1093 return ret;
1094
1095 return ttm_tt_create(bo, false);
1096 }
1097
1098 /*
1099 * Check whether we need to move buffer.
1100 */
1101 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1102 ret = ttm_bo_move_buffer(bo, placement, ctx);
1103 if (ret)
1104 return ret;
1105 } else {
1106 bo->mem.placement &= TTM_PL_MASK_CACHING;
1107 bo->mem.placement |= new_flags & ~TTM_PL_MASK_CACHING;
1108 }
1109 /*
1110 * We might need to add a TTM.
1111 */
1112 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1113 ret = ttm_tt_create(bo, true);
1114 if (ret)
1115 return ret;
1116 }
1117 return 0;
1118 }
1119 EXPORT_SYMBOL(ttm_bo_validate);
1120
ttm_bo_init_reserved(struct ttm_bo_device * bdev,struct ttm_buffer_object * bo,unsigned long size,enum ttm_bo_type type,struct ttm_placement * placement,uint32_t page_alignment,struct ttm_operation_ctx * ctx,size_t acc_size,struct sg_table * sg,struct dma_resv * resv,void (* destroy)(struct ttm_buffer_object *))1121 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1122 struct ttm_buffer_object *bo,
1123 unsigned long size,
1124 enum ttm_bo_type type,
1125 struct ttm_placement *placement,
1126 uint32_t page_alignment,
1127 struct ttm_operation_ctx *ctx,
1128 size_t acc_size,
1129 struct sg_table *sg,
1130 struct dma_resv *resv,
1131 void (*destroy) (struct ttm_buffer_object *))
1132 {
1133 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1134 int ret = 0;
1135 unsigned long num_pages;
1136 bool locked;
1137
1138 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1139 if (ret) {
1140 pr_err("Out of kernel memory\n");
1141 if (destroy)
1142 (*destroy)(bo);
1143 else
1144 kfree(bo);
1145 return -ENOMEM;
1146 }
1147
1148 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1149 if (num_pages == 0) {
1150 pr_err("Illegal buffer object size\n");
1151 if (destroy)
1152 (*destroy)(bo);
1153 else
1154 kfree(bo);
1155 ttm_mem_global_free(mem_glob, acc_size);
1156 return -EINVAL;
1157 }
1158 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1159
1160 kref_init(&bo->kref);
1161 INIT_LIST_HEAD(&bo->lru);
1162 INIT_LIST_HEAD(&bo->ddestroy);
1163 INIT_LIST_HEAD(&bo->swap);
1164 bo->bdev = bdev;
1165 bo->type = type;
1166 bo->num_pages = num_pages;
1167 bo->mem.size = num_pages << PAGE_SHIFT;
1168 bo->mem.mem_type = TTM_PL_SYSTEM;
1169 bo->mem.num_pages = bo->num_pages;
1170 bo->mem.mm_node = NULL;
1171 bo->mem.page_alignment = page_alignment;
1172 bo->mem.bus.offset = 0;
1173 bo->mem.bus.addr = NULL;
1174 bo->moving = NULL;
1175 bo->mem.placement = TTM_PL_FLAG_CACHED;
1176 bo->acc_size = acc_size;
1177 bo->sg = sg;
1178 if (resv) {
1179 bo->base.resv = resv;
1180 dma_resv_assert_held(bo->base.resv);
1181 } else {
1182 bo->base.resv = &bo->base._resv;
1183 }
1184 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1185 /*
1186 * bo.gem is not initialized, so we have to setup the
1187 * struct elements we want use regardless.
1188 */
1189 dma_resv_init(&bo->base._resv);
1190 drm_vma_node_reset(&bo->base.vma_node);
1191 }
1192 atomic_inc(&ttm_bo_glob.bo_count);
1193
1194 /*
1195 * For ttm_bo_type_device buffers, allocate
1196 * address space from the device.
1197 */
1198 if (bo->type == ttm_bo_type_device ||
1199 bo->type == ttm_bo_type_sg)
1200 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1201 bo->mem.num_pages);
1202
1203 /* passed reservation objects should already be locked,
1204 * since otherwise lockdep will be angered in radeon.
1205 */
1206 if (!resv) {
1207 locked = dma_resv_trylock(bo->base.resv);
1208 WARN_ON(!locked);
1209 }
1210
1211 if (likely(!ret))
1212 ret = ttm_bo_validate(bo, placement, ctx);
1213
1214 if (unlikely(ret)) {
1215 if (!resv)
1216 ttm_bo_unreserve(bo);
1217
1218 ttm_bo_put(bo);
1219 return ret;
1220 }
1221
1222 ttm_bo_move_to_lru_tail_unlocked(bo);
1223
1224 return ret;
1225 }
1226 EXPORT_SYMBOL(ttm_bo_init_reserved);
1227
ttm_bo_init(struct ttm_bo_device * bdev,struct ttm_buffer_object * bo,unsigned long size,enum ttm_bo_type type,struct ttm_placement * placement,uint32_t page_alignment,bool interruptible,size_t acc_size,struct sg_table * sg,struct dma_resv * resv,void (* destroy)(struct ttm_buffer_object *))1228 int ttm_bo_init(struct ttm_bo_device *bdev,
1229 struct ttm_buffer_object *bo,
1230 unsigned long size,
1231 enum ttm_bo_type type,
1232 struct ttm_placement *placement,
1233 uint32_t page_alignment,
1234 bool interruptible,
1235 size_t acc_size,
1236 struct sg_table *sg,
1237 struct dma_resv *resv,
1238 void (*destroy) (struct ttm_buffer_object *))
1239 {
1240 struct ttm_operation_ctx ctx = { interruptible, false };
1241 int ret;
1242
1243 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1244 page_alignment, &ctx, acc_size,
1245 sg, resv, destroy);
1246 if (ret)
1247 return ret;
1248
1249 if (!resv)
1250 ttm_bo_unreserve(bo);
1251
1252 return 0;
1253 }
1254 EXPORT_SYMBOL(ttm_bo_init);
1255
ttm_bo_acc_size(struct ttm_bo_device * bdev,unsigned long bo_size,unsigned struct_size)1256 static size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1257 unsigned long bo_size,
1258 unsigned struct_size)
1259 {
1260 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1261 size_t size = 0;
1262
1263 size += ttm_round_pot(struct_size);
1264 size += ttm_round_pot(npages * sizeof(void *));
1265 size += ttm_round_pot(sizeof(struct ttm_tt));
1266 return size;
1267 }
1268
ttm_bo_dma_acc_size(struct ttm_bo_device * bdev,unsigned long bo_size,unsigned struct_size)1269 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1270 unsigned long bo_size,
1271 unsigned struct_size)
1272 {
1273 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1274 size_t size = 0;
1275
1276 size += ttm_round_pot(struct_size);
1277 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1278 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1279 return size;
1280 }
1281 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1282
ttm_bo_create(struct ttm_bo_device * bdev,unsigned long size,enum ttm_bo_type type,struct ttm_placement * placement,uint32_t page_alignment,bool interruptible,struct ttm_buffer_object ** p_bo)1283 int ttm_bo_create(struct ttm_bo_device *bdev,
1284 unsigned long size,
1285 enum ttm_bo_type type,
1286 struct ttm_placement *placement,
1287 uint32_t page_alignment,
1288 bool interruptible,
1289 struct ttm_buffer_object **p_bo)
1290 {
1291 struct ttm_buffer_object *bo;
1292 size_t acc_size;
1293 int ret;
1294
1295 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1296 if (unlikely(bo == NULL))
1297 return -ENOMEM;
1298
1299 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1300 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1301 interruptible, acc_size,
1302 NULL, NULL, NULL);
1303 if (likely(ret == 0))
1304 *p_bo = bo;
1305
1306 return ret;
1307 }
1308 EXPORT_SYMBOL(ttm_bo_create);
1309
ttm_bo_evict_mm(struct ttm_bo_device * bdev,unsigned mem_type)1310 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1311 {
1312 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem_type);
1313
1314 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1315 pr_err("Illegal memory manager memory type %u\n", mem_type);
1316 return -EINVAL;
1317 }
1318
1319 if (!man) {
1320 pr_err("Memory type %u has not been initialized\n", mem_type);
1321 return 0;
1322 }
1323
1324 return ttm_resource_manager_force_list_clean(bdev, man);
1325 }
1326 EXPORT_SYMBOL(ttm_bo_evict_mm);
1327
ttm_bo_global_kobj_release(struct kobject * kobj)1328 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1329 {
1330 struct ttm_bo_global *glob =
1331 container_of(kobj, struct ttm_bo_global, kobj);
1332
1333 __free_page(glob->dummy_read_page);
1334 }
1335
ttm_bo_global_release(void)1336 static void ttm_bo_global_release(void)
1337 {
1338 struct ttm_bo_global *glob = &ttm_bo_glob;
1339
1340 mutex_lock(&ttm_global_mutex);
1341 if (--ttm_bo_glob_use_count > 0)
1342 goto out;
1343
1344 kobject_del(&glob->kobj);
1345 kobject_put(&glob->kobj);
1346 ttm_mem_global_release(&ttm_mem_glob);
1347 memset(glob, 0, sizeof(*glob));
1348 out:
1349 mutex_unlock(&ttm_global_mutex);
1350 }
1351
ttm_bo_global_init(void)1352 static int ttm_bo_global_init(void)
1353 {
1354 struct ttm_bo_global *glob = &ttm_bo_glob;
1355 int ret = 0;
1356 unsigned i;
1357
1358 mutex_lock(&ttm_global_mutex);
1359 if (++ttm_bo_glob_use_count > 1)
1360 goto out;
1361
1362 ret = ttm_mem_global_init(&ttm_mem_glob);
1363 if (ret)
1364 goto out;
1365
1366 spin_lock_init(&glob->lru_lock);
1367 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1368
1369 if (unlikely(glob->dummy_read_page == NULL)) {
1370 ret = -ENOMEM;
1371 goto out;
1372 }
1373
1374 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1375 INIT_LIST_HEAD(&glob->swap_lru[i]);
1376 INIT_LIST_HEAD(&glob->device_list);
1377 atomic_set(&glob->bo_count, 0);
1378
1379 ret = kobject_init_and_add(
1380 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1381 if (unlikely(ret != 0))
1382 kobject_put(&glob->kobj);
1383 out:
1384 mutex_unlock(&ttm_global_mutex);
1385 return ret;
1386 }
1387
ttm_bo_device_release(struct ttm_bo_device * bdev)1388 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1389 {
1390 struct ttm_bo_global *glob = &ttm_bo_glob;
1391 int ret = 0;
1392 unsigned i;
1393 struct ttm_resource_manager *man;
1394
1395 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1396 ttm_resource_manager_set_used(man, false);
1397 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1398
1399 mutex_lock(&ttm_global_mutex);
1400 list_del(&bdev->device_list);
1401 mutex_unlock(&ttm_global_mutex);
1402
1403 cancel_delayed_work_sync(&bdev->wq);
1404
1405 if (ttm_bo_delayed_delete(bdev, true))
1406 pr_debug("Delayed destroy list was clean\n");
1407
1408 spin_lock(&glob->lru_lock);
1409 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1410 if (list_empty(&man->lru[0]))
1411 pr_debug("Swap list %d was clean\n", i);
1412 spin_unlock(&glob->lru_lock);
1413
1414 if (!ret)
1415 ttm_bo_global_release();
1416
1417 return ret;
1418 }
1419 EXPORT_SYMBOL(ttm_bo_device_release);
1420
ttm_bo_init_sysman(struct ttm_bo_device * bdev)1421 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1422 {
1423 struct ttm_resource_manager *man = &bdev->sysman;
1424
1425 /*
1426 * Initialize the system memory buffer type.
1427 * Other types need to be driver / IOCTL initialized.
1428 */
1429 man->use_tt = true;
1430
1431 ttm_resource_manager_init(man, 0);
1432 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1433 ttm_resource_manager_set_used(man, true);
1434 }
1435
ttm_bo_device_init(struct ttm_bo_device * bdev,struct ttm_bo_driver * driver,struct address_space * mapping,struct drm_vma_offset_manager * vma_manager,bool need_dma32)1436 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1437 struct ttm_bo_driver *driver,
1438 struct address_space *mapping,
1439 struct drm_vma_offset_manager *vma_manager,
1440 bool need_dma32)
1441 {
1442 struct ttm_bo_global *glob = &ttm_bo_glob;
1443 int ret;
1444
1445 if (WARN_ON(vma_manager == NULL))
1446 return -EINVAL;
1447
1448 ret = ttm_bo_global_init();
1449 if (ret)
1450 return ret;
1451
1452 bdev->driver = driver;
1453
1454 ttm_bo_init_sysman(bdev);
1455
1456 bdev->vma_manager = vma_manager;
1457 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1458 INIT_LIST_HEAD(&bdev->ddestroy);
1459 bdev->dev_mapping = mapping;
1460 bdev->need_dma32 = need_dma32;
1461 mutex_lock(&ttm_global_mutex);
1462 list_add_tail(&bdev->device_list, &glob->device_list);
1463 mutex_unlock(&ttm_global_mutex);
1464
1465 return 0;
1466 }
1467 EXPORT_SYMBOL(ttm_bo_device_init);
1468
1469 /*
1470 * buffer object vm functions.
1471 */
1472
ttm_bo_unmap_virtual(struct ttm_buffer_object * bo)1473 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1474 {
1475 struct ttm_bo_device *bdev = bo->bdev;
1476
1477 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1478 ttm_mem_io_free(bdev, &bo->mem);
1479 }
1480 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1481
ttm_bo_wait(struct ttm_buffer_object * bo,bool interruptible,bool no_wait)1482 int ttm_bo_wait(struct ttm_buffer_object *bo,
1483 bool interruptible, bool no_wait)
1484 {
1485 long timeout = 15 * HZ;
1486
1487 if (no_wait) {
1488 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1489 return 0;
1490 else
1491 return -EBUSY;
1492 }
1493
1494 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1495 interruptible, timeout);
1496 if (timeout < 0)
1497 return timeout;
1498
1499 if (timeout == 0)
1500 return -EBUSY;
1501
1502 dma_resv_add_excl_fence(bo->base.resv, NULL);
1503 return 0;
1504 }
1505 EXPORT_SYMBOL(ttm_bo_wait);
1506
1507 /**
1508 * A buffer object shrink method that tries to swap out the first
1509 * buffer object on the bo_global::swap_lru list.
1510 */
ttm_bo_swapout(struct ttm_bo_global * glob,struct ttm_operation_ctx * ctx)1511 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1512 {
1513 struct ttm_buffer_object *bo;
1514 int ret = -EBUSY;
1515 bool locked;
1516 unsigned i;
1517
1518 spin_lock(&glob->lru_lock);
1519 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1520 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1521 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1522 NULL))
1523 continue;
1524
1525 if (!ttm_bo_get_unless_zero(bo)) {
1526 if (locked)
1527 dma_resv_unlock(bo->base.resv);
1528 continue;
1529 }
1530
1531 ret = 0;
1532 break;
1533 }
1534 if (!ret)
1535 break;
1536 }
1537
1538 if (ret) {
1539 spin_unlock(&glob->lru_lock);
1540 return ret;
1541 }
1542
1543 if (bo->deleted) {
1544 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1545 ttm_bo_put(bo);
1546 return ret;
1547 }
1548
1549 ttm_bo_del_from_lru(bo);
1550 spin_unlock(&glob->lru_lock);
1551
1552 /**
1553 * Move to system cached
1554 */
1555
1556 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1557 bo->ttm->caching_state != tt_cached) {
1558 struct ttm_operation_ctx ctx = { false, false };
1559 struct ttm_resource evict_mem;
1560
1561 evict_mem = bo->mem;
1562 evict_mem.mm_node = NULL;
1563 evict_mem.placement = TTM_PL_FLAG_CACHED;
1564 evict_mem.mem_type = TTM_PL_SYSTEM;
1565
1566 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1567 if (unlikely(ret != 0))
1568 goto out;
1569 }
1570
1571 /**
1572 * Make sure BO is idle.
1573 */
1574
1575 ret = ttm_bo_wait(bo, false, false);
1576 if (unlikely(ret != 0))
1577 goto out;
1578
1579 ttm_bo_unmap_virtual(bo);
1580
1581 /**
1582 * Swap out. Buffer will be swapped in again as soon as
1583 * anyone tries to access a ttm page.
1584 */
1585
1586 if (bo->bdev->driver->swap_notify)
1587 bo->bdev->driver->swap_notify(bo);
1588
1589 ret = ttm_tt_swapout(bo->bdev, bo->ttm, bo->persistent_swap_storage);
1590 out:
1591
1592 /**
1593 *
1594 * Unreserve without putting on LRU to avoid swapping out an
1595 * already swapped buffer.
1596 */
1597 if (locked)
1598 dma_resv_unlock(bo->base.resv);
1599 ttm_bo_put(bo);
1600 return ret;
1601 }
1602 EXPORT_SYMBOL(ttm_bo_swapout);
1603
ttm_bo_swapout_all(void)1604 void ttm_bo_swapout_all(void)
1605 {
1606 struct ttm_operation_ctx ctx = {
1607 .interruptible = false,
1608 .no_wait_gpu = false
1609 };
1610
1611 while (ttm_bo_swapout(&ttm_bo_glob, &ctx) == 0);
1612 }
1613 EXPORT_SYMBOL(ttm_bo_swapout_all);
1614
ttm_bo_tt_destroy(struct ttm_buffer_object * bo)1615 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1616 {
1617 if (bo->ttm == NULL)
1618 return;
1619
1620 ttm_tt_destroy(bo->bdev, bo->ttm);
1621 bo->ttm = NULL;
1622 }
1623
ttm_bo_tt_bind(struct ttm_buffer_object * bo,struct ttm_resource * mem)1624 int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem)
1625 {
1626 return bo->bdev->driver->ttm_tt_bind(bo->bdev, bo->ttm, mem);
1627 }
1628
ttm_bo_tt_unbind(struct ttm_buffer_object * bo)1629 void ttm_bo_tt_unbind(struct ttm_buffer_object *bo)
1630 {
1631 bo->bdev->driver->ttm_tt_unbind(bo->bdev, bo->ttm);
1632 }
1633