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