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