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