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_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
43 #include <linux/dma-resv.h>
44
45 #include "ttm_module.h"
46
47 /* default destructor */
ttm_bo_default_destroy(struct ttm_buffer_object * bo)48 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
49 {
50 kfree(bo);
51 }
52
ttm_bo_mem_space_debug(struct ttm_buffer_object * bo,struct ttm_placement * placement)53 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
54 struct ttm_placement *placement)
55 {
56 struct drm_printer p = drm_debug_printer(TTM_PFX);
57 struct ttm_resource_manager *man;
58 int i, mem_type;
59
60 drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
61 bo, bo->resource->num_pages, bo->base.size >> 10,
62 bo->base.size >> 20);
63 for (i = 0; i < placement->num_placement; i++) {
64 mem_type = placement->placement[i].mem_type;
65 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
66 i, placement->placement[i].flags, mem_type);
67 man = ttm_manager_type(bo->bdev, mem_type);
68 ttm_resource_manager_debug(man, &p);
69 }
70 }
71
ttm_bo_del_from_lru(struct ttm_buffer_object * bo)72 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
73 {
74 struct ttm_device *bdev = bo->bdev;
75
76 list_del_init(&bo->lru);
77
78 if (bdev->funcs->del_from_lru_notify)
79 bdev->funcs->del_from_lru_notify(bo);
80 }
81
ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos * pos,struct ttm_buffer_object * bo)82 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
83 struct ttm_buffer_object *bo)
84 {
85 if (!pos->first)
86 pos->first = bo;
87 pos->last = bo;
88 }
89
ttm_bo_move_to_lru_tail(struct ttm_buffer_object * bo,struct ttm_resource * mem,struct ttm_lru_bulk_move * bulk)90 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
91 struct ttm_resource *mem,
92 struct ttm_lru_bulk_move *bulk)
93 {
94 struct ttm_device *bdev = bo->bdev;
95 struct ttm_resource_manager *man;
96
97 if (!bo->deleted)
98 dma_resv_assert_held(bo->base.resv);
99
100 if (bo->pin_count) {
101 ttm_bo_del_from_lru(bo);
102 return;
103 }
104
105 if (!mem)
106 return;
107
108 man = ttm_manager_type(bdev, mem->mem_type);
109 list_move_tail(&bo->lru, &man->lru[bo->priority]);
110
111 if (bdev->funcs->del_from_lru_notify)
112 bdev->funcs->del_from_lru_notify(bo);
113
114 if (bulk && !bo->pin_count) {
115 switch (bo->resource->mem_type) {
116 case TTM_PL_TT:
117 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
118 break;
119
120 case TTM_PL_VRAM:
121 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
122 break;
123 }
124 }
125 }
126 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
127
ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move * bulk)128 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
129 {
130 unsigned i;
131
132 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
133 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
134 struct ttm_resource_manager *man;
135
136 if (!pos->first)
137 continue;
138
139 dma_resv_assert_held(pos->first->base.resv);
140 dma_resv_assert_held(pos->last->base.resv);
141
142 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
143 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
144 &pos->last->lru);
145 }
146
147 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
148 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
149 struct ttm_resource_manager *man;
150
151 if (!pos->first)
152 continue;
153
154 dma_resv_assert_held(pos->first->base.resv);
155 dma_resv_assert_held(pos->last->base.resv);
156
157 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
158 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
159 &pos->last->lru);
160 }
161 }
162 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
163
ttm_bo_handle_move_mem(struct ttm_buffer_object * bo,struct ttm_resource * mem,bool evict,struct ttm_operation_ctx * ctx,struct ttm_place * hop)164 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
165 struct ttm_resource *mem, bool evict,
166 struct ttm_operation_ctx *ctx,
167 struct ttm_place *hop)
168 {
169 struct ttm_resource_manager *old_man, *new_man;
170 struct ttm_device *bdev = bo->bdev;
171 int ret;
172
173 old_man = ttm_manager_type(bdev, bo->resource->mem_type);
174 new_man = ttm_manager_type(bdev, mem->mem_type);
175
176 ttm_bo_unmap_virtual(bo);
177
178 /*
179 * Create and bind a ttm if required.
180 */
181
182 if (new_man->use_tt) {
183 /* Zero init the new TTM structure if the old location should
184 * have used one as well.
185 */
186 ret = ttm_tt_create(bo, old_man->use_tt);
187 if (ret)
188 goto out_err;
189
190 if (mem->mem_type != TTM_PL_SYSTEM) {
191 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
192 if (ret)
193 goto out_err;
194 }
195 }
196
197 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
198 if (ret) {
199 if (ret == -EMULTIHOP)
200 return ret;
201 goto out_err;
202 }
203
204 ctx->bytes_moved += bo->base.size;
205 return 0;
206
207 out_err:
208 new_man = ttm_manager_type(bdev, bo->resource->mem_type);
209 if (!new_man->use_tt)
210 ttm_bo_tt_destroy(bo);
211
212 return ret;
213 }
214
215 /*
216 * Call bo::reserved.
217 * Will release GPU memory type usage on destruction.
218 * This is the place to put in driver specific hooks to release
219 * driver private resources.
220 * Will release the bo::reserved lock.
221 */
222
ttm_bo_cleanup_memtype_use(struct ttm_buffer_object * bo)223 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
224 {
225 if (bo->bdev->funcs->delete_mem_notify)
226 bo->bdev->funcs->delete_mem_notify(bo);
227
228 ttm_bo_tt_destroy(bo);
229 ttm_resource_free(bo, &bo->resource);
230 }
231
ttm_bo_individualize_resv(struct ttm_buffer_object * bo)232 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
233 {
234 int r;
235
236 if (bo->base.resv == &bo->base._resv)
237 return 0;
238
239 BUG_ON(!dma_resv_trylock(&bo->base._resv));
240
241 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
242 dma_resv_unlock(&bo->base._resv);
243 if (r)
244 return r;
245
246 if (bo->type != ttm_bo_type_sg) {
247 /* This works because the BO is about to be destroyed and nobody
248 * reference it any more. The only tricky case is the trylock on
249 * the resv object while holding the lru_lock.
250 */
251 spin_lock(&bo->bdev->lru_lock);
252 bo->base.resv = &bo->base._resv;
253 spin_unlock(&bo->bdev->lru_lock);
254 }
255
256 return r;
257 }
258
ttm_bo_flush_all_fences(struct ttm_buffer_object * bo)259 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
260 {
261 struct dma_resv *resv = &bo->base._resv;
262 struct dma_resv_list *fobj;
263 struct dma_fence *fence;
264 int i;
265
266 rcu_read_lock();
267 fobj = dma_resv_shared_list(resv);
268 fence = dma_resv_excl_fence(resv);
269 if (fence && !fence->ops->signaled)
270 dma_fence_enable_sw_signaling(fence);
271
272 for (i = 0; fobj && i < fobj->shared_count; ++i) {
273 fence = rcu_dereference(fobj->shared[i]);
274
275 if (!fence->ops->signaled)
276 dma_fence_enable_sw_signaling(fence);
277 }
278 rcu_read_unlock();
279 }
280
281 /**
282 * ttm_bo_cleanup_refs
283 * If bo idle, remove from lru lists, and unref.
284 * If not idle, block if possible.
285 *
286 * Must be called with lru_lock and reservation held, this function
287 * will drop the lru lock and optionally the reservation lock before returning.
288 *
289 * @bo: The buffer object to clean-up
290 * @interruptible: Any sleeps should occur interruptibly.
291 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
292 * @unlock_resv: Unlock the reservation lock as well.
293 */
294
ttm_bo_cleanup_refs(struct ttm_buffer_object * bo,bool interruptible,bool no_wait_gpu,bool unlock_resv)295 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
296 bool interruptible, bool no_wait_gpu,
297 bool unlock_resv)
298 {
299 struct dma_resv *resv = &bo->base._resv;
300 int ret;
301
302 if (dma_resv_test_signaled(resv, true))
303 ret = 0;
304 else
305 ret = -EBUSY;
306
307 if (ret && !no_wait_gpu) {
308 long lret;
309
310 if (unlock_resv)
311 dma_resv_unlock(bo->base.resv);
312 spin_unlock(&bo->bdev->lru_lock);
313
314 lret = dma_resv_wait_timeout(resv, true, interruptible,
315 30 * HZ);
316
317 if (lret < 0)
318 return lret;
319 else if (lret == 0)
320 return -EBUSY;
321
322 spin_lock(&bo->bdev->lru_lock);
323 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
324 /*
325 * We raced, and lost, someone else holds the reservation now,
326 * and is probably busy in ttm_bo_cleanup_memtype_use.
327 *
328 * Even if it's not the case, because we finished waiting any
329 * delayed destruction would succeed, so just return success
330 * here.
331 */
332 spin_unlock(&bo->bdev->lru_lock);
333 return 0;
334 }
335 ret = 0;
336 }
337
338 if (ret || unlikely(list_empty(&bo->ddestroy))) {
339 if (unlock_resv)
340 dma_resv_unlock(bo->base.resv);
341 spin_unlock(&bo->bdev->lru_lock);
342 return ret;
343 }
344
345 ttm_bo_del_from_lru(bo);
346 list_del_init(&bo->ddestroy);
347 spin_unlock(&bo->bdev->lru_lock);
348 ttm_bo_cleanup_memtype_use(bo);
349
350 if (unlock_resv)
351 dma_resv_unlock(bo->base.resv);
352
353 ttm_bo_put(bo);
354
355 return 0;
356 }
357
358 /*
359 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
360 * encountered buffers.
361 */
ttm_bo_delayed_delete(struct ttm_device * bdev,bool remove_all)362 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
363 {
364 struct list_head removed;
365 bool empty;
366
367 INIT_LIST_HEAD(&removed);
368
369 spin_lock(&bdev->lru_lock);
370 while (!list_empty(&bdev->ddestroy)) {
371 struct ttm_buffer_object *bo;
372
373 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
374 ddestroy);
375 list_move_tail(&bo->ddestroy, &removed);
376 if (!ttm_bo_get_unless_zero(bo))
377 continue;
378
379 if (remove_all || bo->base.resv != &bo->base._resv) {
380 spin_unlock(&bdev->lru_lock);
381 dma_resv_lock(bo->base.resv, NULL);
382
383 spin_lock(&bdev->lru_lock);
384 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
385
386 } else if (dma_resv_trylock(bo->base.resv)) {
387 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
388 } else {
389 spin_unlock(&bdev->lru_lock);
390 }
391
392 ttm_bo_put(bo);
393 spin_lock(&bdev->lru_lock);
394 }
395 list_splice_tail(&removed, &bdev->ddestroy);
396 empty = list_empty(&bdev->ddestroy);
397 spin_unlock(&bdev->lru_lock);
398
399 return empty;
400 }
401
ttm_bo_release(struct kref * kref)402 static void ttm_bo_release(struct kref *kref)
403 {
404 struct ttm_buffer_object *bo =
405 container_of(kref, struct ttm_buffer_object, kref);
406 struct ttm_device *bdev = bo->bdev;
407 int ret;
408
409 WARN_ON_ONCE(bo->pin_count);
410
411 if (!bo->deleted) {
412 ret = ttm_bo_individualize_resv(bo);
413 if (ret) {
414 /* Last resort, if we fail to allocate memory for the
415 * fences block for the BO to become idle
416 */
417 dma_resv_wait_timeout(bo->base.resv, true, false,
418 30 * HZ);
419 }
420
421 if (bo->bdev->funcs->release_notify)
422 bo->bdev->funcs->release_notify(bo);
423
424 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
425 ttm_mem_io_free(bdev, bo->resource);
426 }
427
428 if (!dma_resv_test_signaled(bo->base.resv, true) ||
429 !dma_resv_trylock(bo->base.resv)) {
430 /* The BO is not idle, resurrect it for delayed destroy */
431 ttm_bo_flush_all_fences(bo);
432 bo->deleted = true;
433
434 spin_lock(&bo->bdev->lru_lock);
435
436 /*
437 * Make pinned bos immediately available to
438 * shrinkers, now that they are queued for
439 * destruction.
440 *
441 * FIXME: QXL is triggering this. Can be removed when the
442 * driver is fixed.
443 */
444 if (bo->pin_count) {
445 bo->pin_count = 0;
446 ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
447 }
448
449 kref_init(&bo->kref);
450 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
451 spin_unlock(&bo->bdev->lru_lock);
452
453 schedule_delayed_work(&bdev->wq,
454 ((HZ / 100) < 1) ? 1 : HZ / 100);
455 return;
456 }
457
458 spin_lock(&bo->bdev->lru_lock);
459 ttm_bo_del_from_lru(bo);
460 list_del(&bo->ddestroy);
461 spin_unlock(&bo->bdev->lru_lock);
462
463 ttm_bo_cleanup_memtype_use(bo);
464 dma_resv_unlock(bo->base.resv);
465
466 atomic_dec(&ttm_glob.bo_count);
467 dma_fence_put(bo->moving);
468 bo->destroy(bo);
469 }
470
ttm_bo_put(struct ttm_buffer_object * bo)471 void ttm_bo_put(struct ttm_buffer_object *bo)
472 {
473 kref_put(&bo->kref, ttm_bo_release);
474 }
475 EXPORT_SYMBOL(ttm_bo_put);
476
ttm_bo_lock_delayed_workqueue(struct ttm_device * bdev)477 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
478 {
479 return cancel_delayed_work_sync(&bdev->wq);
480 }
481 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
482
ttm_bo_unlock_delayed_workqueue(struct ttm_device * bdev,int resched)483 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
484 {
485 if (resched)
486 schedule_delayed_work(&bdev->wq,
487 ((HZ / 100) < 1) ? 1 : HZ / 100);
488 }
489 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
490
ttm_bo_bounce_temp_buffer(struct ttm_buffer_object * bo,struct ttm_resource ** mem,struct ttm_operation_ctx * ctx,struct ttm_place * hop)491 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
492 struct ttm_resource **mem,
493 struct ttm_operation_ctx *ctx,
494 struct ttm_place *hop)
495 {
496 struct ttm_placement hop_placement;
497 struct ttm_resource *hop_mem;
498 int ret;
499
500 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
501 hop_placement.placement = hop_placement.busy_placement = hop;
502
503 /* find space in the bounce domain */
504 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
505 if (ret)
506 return ret;
507 /* move to the bounce domain */
508 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
509 if (ret) {
510 ttm_resource_free(bo, &hop_mem);
511 return ret;
512 }
513 return 0;
514 }
515
ttm_bo_evict(struct ttm_buffer_object * bo,struct ttm_operation_ctx * ctx)516 static int ttm_bo_evict(struct ttm_buffer_object *bo,
517 struct ttm_operation_ctx *ctx)
518 {
519 struct ttm_device *bdev = bo->bdev;
520 struct ttm_resource *evict_mem;
521 struct ttm_placement placement;
522 struct ttm_place hop;
523 int ret = 0;
524
525 memset(&hop, 0, sizeof(hop));
526
527 dma_resv_assert_held(bo->base.resv);
528
529 placement.num_placement = 0;
530 placement.num_busy_placement = 0;
531 bdev->funcs->evict_flags(bo, &placement);
532
533 if (!placement.num_placement && !placement.num_busy_placement) {
534 ret = ttm_bo_wait(bo, true, false);
535 if (ret)
536 return ret;
537
538 /*
539 * Since we've already synced, this frees backing store
540 * immediately.
541 */
542 return ttm_bo_pipeline_gutting(bo);
543 }
544
545 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
546 if (ret) {
547 if (ret != -ERESTARTSYS) {
548 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
549 bo);
550 ttm_bo_mem_space_debug(bo, &placement);
551 }
552 goto out;
553 }
554
555 do {
556 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
557 if (ret != -EMULTIHOP)
558 break;
559
560 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
561 } while (!ret);
562
563 if (ret) {
564 ttm_resource_free(bo, &evict_mem);
565 if (ret != -ERESTARTSYS && ret != -EINTR)
566 pr_err("Buffer eviction failed\n");
567 }
568 out:
569 return ret;
570 }
571
ttm_bo_eviction_valuable(struct ttm_buffer_object * bo,const struct ttm_place * place)572 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
573 const struct ttm_place *place)
574 {
575 dma_resv_assert_held(bo->base.resv);
576 if (bo->resource->mem_type == TTM_PL_SYSTEM)
577 return true;
578
579 /* Don't evict this BO if it's outside of the
580 * requested placement range
581 */
582 if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
583 (place->lpfn && place->lpfn <= bo->resource->start))
584 return false;
585
586 return true;
587 }
588 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
589
590 /*
591 * Check the target bo is allowable to be evicted or swapout, including cases:
592 *
593 * a. if share same reservation object with ctx->resv, have assumption
594 * reservation objects should already be locked, so not lock again and
595 * return true directly when either the opreation allow_reserved_eviction
596 * or the target bo already is in delayed free list;
597 *
598 * b. Otherwise, trylock it.
599 */
ttm_bo_evict_swapout_allowable(struct ttm_buffer_object * bo,struct ttm_operation_ctx * ctx,const struct ttm_place * place,bool * locked,bool * busy)600 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
601 struct ttm_operation_ctx *ctx,
602 const struct ttm_place *place,
603 bool *locked, bool *busy)
604 {
605 bool ret = false;
606
607 if (bo->pin_count) {
608 *locked = false;
609 if (busy)
610 *busy = false;
611 return false;
612 }
613
614 if (bo->base.resv == ctx->resv) {
615 dma_resv_assert_held(bo->base.resv);
616 if (ctx->allow_res_evict)
617 ret = true;
618 *locked = false;
619 if (busy)
620 *busy = false;
621 } else {
622 ret = dma_resv_trylock(bo->base.resv);
623 *locked = ret;
624 if (busy)
625 *busy = !ret;
626 }
627
628 if (ret && place && !bo->bdev->funcs->eviction_valuable(bo, place)) {
629 ret = false;
630 if (*locked) {
631 dma_resv_unlock(bo->base.resv);
632 *locked = false;
633 }
634 }
635
636 return ret;
637 }
638
639 /**
640 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
641 *
642 * @busy_bo: BO which couldn't be locked with trylock
643 * @ctx: operation context
644 * @ticket: acquire ticket
645 *
646 * Try to lock a busy buffer object to avoid failing eviction.
647 */
ttm_mem_evict_wait_busy(struct ttm_buffer_object * busy_bo,struct ttm_operation_ctx * ctx,struct ww_acquire_ctx * ticket)648 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
649 struct ttm_operation_ctx *ctx,
650 struct ww_acquire_ctx *ticket)
651 {
652 int r;
653
654 if (!busy_bo || !ticket)
655 return -EBUSY;
656
657 if (ctx->interruptible)
658 r = dma_resv_lock_interruptible(busy_bo->base.resv,
659 ticket);
660 else
661 r = dma_resv_lock(busy_bo->base.resv, ticket);
662
663 /*
664 * TODO: It would be better to keep the BO locked until allocation is at
665 * least tried one more time, but that would mean a much larger rework
666 * of TTM.
667 */
668 if (!r)
669 dma_resv_unlock(busy_bo->base.resv);
670
671 return r == -EDEADLK ? -EBUSY : r;
672 }
673
ttm_mem_evict_first(struct ttm_device * bdev,struct ttm_resource_manager * man,const struct ttm_place * place,struct ttm_operation_ctx * ctx,struct ww_acquire_ctx * ticket)674 int ttm_mem_evict_first(struct ttm_device *bdev,
675 struct ttm_resource_manager *man,
676 const struct ttm_place *place,
677 struct ttm_operation_ctx *ctx,
678 struct ww_acquire_ctx *ticket)
679 {
680 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
681 bool locked = false;
682 unsigned i;
683 int ret;
684
685 spin_lock(&bdev->lru_lock);
686 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
687 list_for_each_entry(bo, &man->lru[i], lru) {
688 bool busy;
689
690 if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
691 &locked, &busy)) {
692 if (busy && !busy_bo && ticket !=
693 dma_resv_locking_ctx(bo->base.resv))
694 busy_bo = bo;
695 continue;
696 }
697
698 if (!ttm_bo_get_unless_zero(bo)) {
699 if (locked)
700 dma_resv_unlock(bo->base.resv);
701 continue;
702 }
703 break;
704 }
705
706 /* If the inner loop terminated early, we have our candidate */
707 if (&bo->lru != &man->lru[i])
708 break;
709
710 bo = NULL;
711 }
712
713 if (!bo) {
714 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
715 busy_bo = NULL;
716 spin_unlock(&bdev->lru_lock);
717 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
718 if (busy_bo)
719 ttm_bo_put(busy_bo);
720 return ret;
721 }
722
723 if (bo->deleted) {
724 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
725 ctx->no_wait_gpu, locked);
726 ttm_bo_put(bo);
727 return ret;
728 }
729
730 spin_unlock(&bdev->lru_lock);
731
732 ret = ttm_bo_evict(bo, ctx);
733 if (locked)
734 ttm_bo_unreserve(bo);
735 else
736 ttm_bo_move_to_lru_tail_unlocked(bo);
737
738 ttm_bo_put(bo);
739 return ret;
740 }
741
742 /*
743 * Add the last move fence to the BO and reserve a new shared slot. We only use
744 * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
745 * either stall or use an exclusive fence respectively set bo->moving.
746 */
ttm_bo_add_move_fence(struct ttm_buffer_object * bo,struct ttm_resource_manager * man,struct ttm_resource * mem,bool no_wait_gpu)747 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
748 struct ttm_resource_manager *man,
749 struct ttm_resource *mem,
750 bool no_wait_gpu)
751 {
752 struct dma_fence *fence;
753 int ret;
754
755 spin_lock(&man->move_lock);
756 fence = dma_fence_get(man->move);
757 spin_unlock(&man->move_lock);
758
759 if (!fence)
760 return 0;
761
762 if (no_wait_gpu) {
763 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
764 dma_fence_put(fence);
765 return ret;
766 }
767
768 dma_resv_add_shared_fence(bo->base.resv, fence);
769
770 ret = dma_resv_reserve_shared(bo->base.resv, 1);
771 if (unlikely(ret)) {
772 dma_fence_put(fence);
773 return ret;
774 }
775
776 dma_fence_put(bo->moving);
777 bo->moving = fence;
778 return 0;
779 }
780
781 /*
782 * Repeatedly evict memory from the LRU for @mem_type until we create enough
783 * space, or we've evicted everything and there isn't enough space.
784 */
ttm_bo_mem_force_space(struct ttm_buffer_object * bo,const struct ttm_place * place,struct ttm_resource ** mem,struct ttm_operation_ctx * ctx)785 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
786 const struct ttm_place *place,
787 struct ttm_resource **mem,
788 struct ttm_operation_ctx *ctx)
789 {
790 struct ttm_device *bdev = bo->bdev;
791 struct ttm_resource_manager *man;
792 struct ww_acquire_ctx *ticket;
793 int ret;
794
795 man = ttm_manager_type(bdev, place->mem_type);
796 ticket = dma_resv_locking_ctx(bo->base.resv);
797 do {
798 ret = ttm_resource_alloc(bo, place, mem);
799 if (likely(!ret))
800 break;
801 if (unlikely(ret != -ENOSPC))
802 return ret;
803 ret = ttm_mem_evict_first(bdev, man, place, ctx,
804 ticket);
805 if (unlikely(ret != 0))
806 return ret;
807 } while (1);
808
809 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
810 }
811
812 /*
813 * Creates space for memory region @mem according to its type.
814 *
815 * This function first searches for free space in compatible memory types in
816 * the priority order defined by the driver. If free space isn't found, then
817 * ttm_bo_mem_force_space is attempted in priority order to evict and find
818 * space.
819 */
ttm_bo_mem_space(struct ttm_buffer_object * bo,struct ttm_placement * placement,struct ttm_resource ** mem,struct ttm_operation_ctx * ctx)820 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
821 struct ttm_placement *placement,
822 struct ttm_resource **mem,
823 struct ttm_operation_ctx *ctx)
824 {
825 struct ttm_device *bdev = bo->bdev;
826 bool type_found = false;
827 int i, ret;
828
829 ret = dma_resv_reserve_shared(bo->base.resv, 1);
830 if (unlikely(ret))
831 return ret;
832
833 for (i = 0; i < placement->num_placement; ++i) {
834 const struct ttm_place *place = &placement->placement[i];
835 struct ttm_resource_manager *man;
836
837 man = ttm_manager_type(bdev, place->mem_type);
838 if (!man || !ttm_resource_manager_used(man))
839 continue;
840
841 type_found = true;
842 ret = ttm_resource_alloc(bo, place, mem);
843 if (ret == -ENOSPC)
844 continue;
845 if (unlikely(ret))
846 goto error;
847
848 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
849 if (unlikely(ret)) {
850 ttm_resource_free(bo, mem);
851 if (ret == -EBUSY)
852 continue;
853
854 goto error;
855 }
856 return 0;
857 }
858
859 for (i = 0; i < placement->num_busy_placement; ++i) {
860 const struct ttm_place *place = &placement->busy_placement[i];
861 struct ttm_resource_manager *man;
862
863 man = ttm_manager_type(bdev, place->mem_type);
864 if (!man || !ttm_resource_manager_used(man))
865 continue;
866
867 type_found = true;
868 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
869 if (likely(!ret))
870 return 0;
871
872 if (ret && ret != -EBUSY)
873 goto error;
874 }
875
876 ret = -ENOMEM;
877 if (!type_found) {
878 pr_err(TTM_PFX "No compatible memory type found\n");
879 ret = -EINVAL;
880 }
881
882 error:
883 if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
884 ttm_bo_move_to_lru_tail_unlocked(bo);
885
886 return ret;
887 }
888 EXPORT_SYMBOL(ttm_bo_mem_space);
889
ttm_bo_move_buffer(struct ttm_buffer_object * bo,struct ttm_placement * placement,struct ttm_operation_ctx * ctx)890 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
891 struct ttm_placement *placement,
892 struct ttm_operation_ctx *ctx)
893 {
894 struct ttm_resource *mem;
895 struct ttm_place hop;
896 int ret;
897
898 dma_resv_assert_held(bo->base.resv);
899
900 /*
901 * Determine where to move the buffer.
902 *
903 * If driver determines move is going to need
904 * an extra step then it will return -EMULTIHOP
905 * and the buffer will be moved to the temporary
906 * stop and the driver will be called to make
907 * the second hop.
908 */
909 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
910 if (ret)
911 return ret;
912 bounce:
913 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
914 if (ret == -EMULTIHOP) {
915 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
916 if (ret)
917 goto out;
918 /* try and move to final place now. */
919 goto bounce;
920 }
921 out:
922 if (ret)
923 ttm_resource_free(bo, &mem);
924 return ret;
925 }
926
ttm_bo_places_compat(const struct ttm_place * places,unsigned num_placement,struct ttm_resource * mem,uint32_t * new_flags)927 static bool ttm_bo_places_compat(const struct ttm_place *places,
928 unsigned num_placement,
929 struct ttm_resource *mem,
930 uint32_t *new_flags)
931 {
932 unsigned i;
933
934 if (mem->placement & TTM_PL_FLAG_TEMPORARY)
935 return false;
936
937 for (i = 0; i < num_placement; i++) {
938 const struct ttm_place *heap = &places[i];
939
940 if ((mem->start < heap->fpfn ||
941 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
942 continue;
943
944 *new_flags = heap->flags;
945 if ((mem->mem_type == heap->mem_type) &&
946 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
947 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
948 return true;
949 }
950 return false;
951 }
952
ttm_bo_mem_compat(struct ttm_placement * placement,struct ttm_resource * mem,uint32_t * new_flags)953 bool ttm_bo_mem_compat(struct ttm_placement *placement,
954 struct ttm_resource *mem,
955 uint32_t *new_flags)
956 {
957 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
958 mem, new_flags))
959 return true;
960
961 if ((placement->busy_placement != placement->placement ||
962 placement->num_busy_placement > placement->num_placement) &&
963 ttm_bo_places_compat(placement->busy_placement,
964 placement->num_busy_placement,
965 mem, new_flags))
966 return true;
967
968 return false;
969 }
970 EXPORT_SYMBOL(ttm_bo_mem_compat);
971
ttm_bo_validate(struct ttm_buffer_object * bo,struct ttm_placement * placement,struct ttm_operation_ctx * ctx)972 int ttm_bo_validate(struct ttm_buffer_object *bo,
973 struct ttm_placement *placement,
974 struct ttm_operation_ctx *ctx)
975 {
976 int ret;
977 uint32_t new_flags;
978
979 dma_resv_assert_held(bo->base.resv);
980
981 /*
982 * Remove the backing store if no placement is given.
983 */
984 if (!placement->num_placement && !placement->num_busy_placement)
985 return ttm_bo_pipeline_gutting(bo);
986
987 /*
988 * Check whether we need to move buffer.
989 */
990 if (!ttm_bo_mem_compat(placement, bo->resource, &new_flags)) {
991 ret = ttm_bo_move_buffer(bo, placement, ctx);
992 if (ret)
993 return ret;
994 }
995 /*
996 * We might need to add a TTM.
997 */
998 if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
999 ret = ttm_tt_create(bo, true);
1000 if (ret)
1001 return ret;
1002 }
1003 return 0;
1004 }
1005 EXPORT_SYMBOL(ttm_bo_validate);
1006
ttm_bo_init_reserved(struct ttm_device * bdev,struct ttm_buffer_object * bo,size_t size,enum ttm_bo_type type,struct ttm_placement * placement,uint32_t page_alignment,struct ttm_operation_ctx * ctx,struct sg_table * sg,struct dma_resv * resv,void (* destroy)(struct ttm_buffer_object *))1007 int ttm_bo_init_reserved(struct ttm_device *bdev,
1008 struct ttm_buffer_object *bo,
1009 size_t size,
1010 enum ttm_bo_type type,
1011 struct ttm_placement *placement,
1012 uint32_t page_alignment,
1013 struct ttm_operation_ctx *ctx,
1014 struct sg_table *sg,
1015 struct dma_resv *resv,
1016 void (*destroy) (struct ttm_buffer_object *))
1017 {
1018 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
1019 bool locked;
1020 int ret;
1021
1022 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1023
1024 kref_init(&bo->kref);
1025 INIT_LIST_HEAD(&bo->lru);
1026 INIT_LIST_HEAD(&bo->ddestroy);
1027 bo->bdev = bdev;
1028 bo->type = type;
1029 bo->page_alignment = page_alignment;
1030 bo->moving = NULL;
1031 bo->pin_count = 0;
1032 bo->sg = sg;
1033 if (resv) {
1034 bo->base.resv = resv;
1035 dma_resv_assert_held(bo->base.resv);
1036 } else {
1037 bo->base.resv = &bo->base._resv;
1038 }
1039 atomic_inc(&ttm_glob.bo_count);
1040
1041 ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
1042 if (unlikely(ret)) {
1043 ttm_bo_put(bo);
1044 return ret;
1045 }
1046
1047 /*
1048 * For ttm_bo_type_device buffers, allocate
1049 * address space from the device.
1050 */
1051 if (bo->type == ttm_bo_type_device ||
1052 bo->type == ttm_bo_type_sg)
1053 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1054 bo->resource->num_pages);
1055
1056 /* passed reservation objects should already be locked,
1057 * since otherwise lockdep will be angered in radeon.
1058 */
1059 if (!resv) {
1060 locked = dma_resv_trylock(bo->base.resv);
1061 WARN_ON(!locked);
1062 }
1063
1064 if (likely(!ret))
1065 ret = ttm_bo_validate(bo, placement, ctx);
1066
1067 if (unlikely(ret)) {
1068 if (!resv)
1069 ttm_bo_unreserve(bo);
1070
1071 ttm_bo_put(bo);
1072 return ret;
1073 }
1074
1075 ttm_bo_move_to_lru_tail_unlocked(bo);
1076
1077 return ret;
1078 }
1079 EXPORT_SYMBOL(ttm_bo_init_reserved);
1080
ttm_bo_init(struct ttm_device * bdev,struct ttm_buffer_object * bo,size_t size,enum ttm_bo_type type,struct ttm_placement * placement,uint32_t page_alignment,bool interruptible,struct sg_table * sg,struct dma_resv * resv,void (* destroy)(struct ttm_buffer_object *))1081 int ttm_bo_init(struct ttm_device *bdev,
1082 struct ttm_buffer_object *bo,
1083 size_t size,
1084 enum ttm_bo_type type,
1085 struct ttm_placement *placement,
1086 uint32_t page_alignment,
1087 bool interruptible,
1088 struct sg_table *sg,
1089 struct dma_resv *resv,
1090 void (*destroy) (struct ttm_buffer_object *))
1091 {
1092 struct ttm_operation_ctx ctx = { interruptible, false };
1093 int ret;
1094
1095 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1096 page_alignment, &ctx, sg, resv, destroy);
1097 if (ret)
1098 return ret;
1099
1100 if (!resv)
1101 ttm_bo_unreserve(bo);
1102
1103 return 0;
1104 }
1105 EXPORT_SYMBOL(ttm_bo_init);
1106
1107 /*
1108 * buffer object vm functions.
1109 */
1110
ttm_bo_unmap_virtual(struct ttm_buffer_object * bo)1111 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1112 {
1113 struct ttm_device *bdev = bo->bdev;
1114
1115 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1116 ttm_mem_io_free(bdev, bo->resource);
1117 }
1118 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1119
ttm_bo_wait(struct ttm_buffer_object * bo,bool interruptible,bool no_wait)1120 int ttm_bo_wait(struct ttm_buffer_object *bo,
1121 bool interruptible, bool no_wait)
1122 {
1123 long timeout = 15 * HZ;
1124
1125 if (no_wait) {
1126 if (dma_resv_test_signaled(bo->base.resv, true))
1127 return 0;
1128 else
1129 return -EBUSY;
1130 }
1131
1132 timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1133 timeout);
1134 if (timeout < 0)
1135 return timeout;
1136
1137 if (timeout == 0)
1138 return -EBUSY;
1139
1140 dma_resv_add_excl_fence(bo->base.resv, NULL);
1141 return 0;
1142 }
1143 EXPORT_SYMBOL(ttm_bo_wait);
1144
ttm_bo_swapout(struct ttm_buffer_object * bo,struct ttm_operation_ctx * ctx,gfp_t gfp_flags)1145 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1146 gfp_t gfp_flags)
1147 {
1148 struct ttm_place place;
1149 bool locked;
1150 int ret;
1151
1152 /*
1153 * While the bo may already reside in SYSTEM placement, set
1154 * SYSTEM as new placement to cover also the move further below.
1155 * The driver may use the fact that we're moving from SYSTEM
1156 * as an indication that we're about to swap out.
1157 */
1158 memset(&place, 0, sizeof(place));
1159 place.mem_type = TTM_PL_SYSTEM;
1160 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1161 return -EBUSY;
1162
1163 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1164 bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
1165 bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
1166 !ttm_bo_get_unless_zero(bo)) {
1167 if (locked)
1168 dma_resv_unlock(bo->base.resv);
1169 return -EBUSY;
1170 }
1171
1172 if (bo->deleted) {
1173 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1174 ttm_bo_put(bo);
1175 return ret == -EBUSY ? -ENOSPC : ret;
1176 }
1177
1178 ttm_bo_del_from_lru(bo);
1179 /* TODO: Cleanup the locking */
1180 spin_unlock(&bo->bdev->lru_lock);
1181
1182 /*
1183 * Move to system cached
1184 */
1185 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1186 struct ttm_operation_ctx ctx = { false, false };
1187 struct ttm_resource *evict_mem;
1188 struct ttm_place hop;
1189
1190 memset(&hop, 0, sizeof(hop));
1191 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1192 if (unlikely(ret))
1193 goto out;
1194
1195 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1196 if (unlikely(ret != 0)) {
1197 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1198 ttm_resource_free(bo, &evict_mem);
1199 goto out;
1200 }
1201 }
1202
1203 /*
1204 * Make sure BO is idle.
1205 */
1206 ret = ttm_bo_wait(bo, false, false);
1207 if (unlikely(ret != 0))
1208 goto out;
1209
1210 ttm_bo_unmap_virtual(bo);
1211
1212 /*
1213 * Swap out. Buffer will be swapped in again as soon as
1214 * anyone tries to access a ttm page.
1215 */
1216 if (bo->bdev->funcs->swap_notify)
1217 bo->bdev->funcs->swap_notify(bo);
1218
1219 if (ttm_tt_is_populated(bo->ttm))
1220 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1221 out:
1222
1223 /*
1224 * Unreserve without putting on LRU to avoid swapping out an
1225 * already swapped buffer.
1226 */
1227 if (locked)
1228 dma_resv_unlock(bo->base.resv);
1229 ttm_bo_put(bo);
1230 return ret == -EBUSY ? -ENOSPC : ret;
1231 }
1232
ttm_bo_tt_destroy(struct ttm_buffer_object * bo)1233 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1234 {
1235 if (bo->ttm == NULL)
1236 return;
1237
1238 ttm_tt_destroy(bo->bdev, bo->ttm);
1239 bo->ttm = NULL;
1240 }
1241