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