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