ttm_bo.c revision 1.13 1 /* $NetBSD: ttm_bo.c,v 1.13 2018/08/27 07:53:16 riastradh Exp $ */
2
3 /**************************************************************************
4 *
5 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29 /*
30 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ttm_bo.c,v 1.13 2018/08/27 07:53:16 riastradh Exp $");
35
36 #define pr_fmt(fmt) "[TTM] " fmt
37
38 #ifdef __NetBSD__
39 #include <sys/types.h>
40 #include <uvm/uvm_extern.h>
41 #include <uvm/uvm_object.h>
42 #endif
43
44 #include <drm/ttm/ttm_module.h>
45 #include <drm/ttm/ttm_bo_driver.h>
46 #include <drm/ttm/ttm_placement.h>
47 #include <linux/jiffies.h>
48 #include <linux/slab.h>
49 #include <linux/sched.h>
50 #include <linux/mm.h>
51 #include <linux/file.h>
52 #include <linux/module.h>
53 #include <linux/atomic.h>
54 #include <linux/reservation.h>
55 #include <linux/printk.h>
56 #include <linux/export.h>
57 #include <linux/fence.h>
58
59 #define TTM_ASSERT_LOCKED(param)
60 #define TTM_DEBUG(fmt, arg...) do {} while (0)
61 #define TTM_BO_HASH_ORDER 13
62
63 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
64 #ifndef __NetBSD__
65 static void ttm_bo_global_kobj_release(struct kobject *kobj);
66 #endif
67
68 #ifndef __NetBSD__ /* XXX sysfs */
69 static struct attribute ttm_bo_count = {
70 .name = "bo_count",
71 .mode = S_IRUGO
72 };
73 #endif
74
75 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
76 uint32_t *mem_type)
77 {
78 int i;
79
80 for (i = 0; i <= TTM_PL_PRIV5; i++)
81 if (place->flags & (1 << i)) {
82 *mem_type = i;
83 return 0;
84 }
85 return -EINVAL;
86 }
87
88 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
89 {
90 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
91
92 pr_err(" has_type: %d\n", man->has_type);
93 pr_err(" use_type: %d\n", man->use_type);
94 pr_err(" flags: 0x%08X\n", man->flags);
95 pr_err(" gpu_offset: 0x%"PRIX64"\n", man->gpu_offset);
96 pr_err(" size: %"PRIu64"\n", man->size);
97 pr_err(" available_caching: 0x%08X\n", man->available_caching);
98 pr_err(" default_caching: 0x%08X\n", man->default_caching);
99 if (mem_type != TTM_PL_SYSTEM)
100 (*man->func->debug)(man, TTM_PFX);
101 }
102
103 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
104 struct ttm_placement *placement)
105 {
106 int i, ret, mem_type;
107
108 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
109 bo, bo->mem.num_pages, bo->mem.size >> 10,
110 bo->mem.size >> 20);
111 for (i = 0; i < placement->num_placement; i++) {
112 ret = ttm_mem_type_from_place(&placement->placement[i],
113 &mem_type);
114 if (ret)
115 return;
116 pr_err(" placement[%d]=0x%08X (%d)\n",
117 i, placement->placement[i].flags, mem_type);
118 ttm_mem_type_debug(bo->bdev, mem_type);
119 }
120 }
121
122 #ifndef __NetBSD__ /* XXX sysfs */
123 static ssize_t ttm_bo_global_show(struct kobject *kobj,
124 struct attribute *attr,
125 char *buffer)
126 {
127 struct ttm_bo_global *glob =
128 container_of(kobj, struct ttm_bo_global, kobj);
129
130 return snprintf(buffer, PAGE_SIZE, "%lu\n",
131 (unsigned long) atomic_read(&glob->bo_count));
132 }
133
134 static struct attribute *ttm_bo_global_attrs[] = {
135 &ttm_bo_count,
136 NULL
137 };
138
139 static const struct sysfs_ops ttm_bo_global_ops = {
140 .show = &ttm_bo_global_show
141 };
142
143 static struct kobj_type ttm_bo_glob_kobj_type = {
144 .release = &ttm_bo_global_kobj_release,
145 .sysfs_ops = &ttm_bo_global_ops,
146 .default_attrs = ttm_bo_global_attrs
147 };
148 #endif /* __NetBSD__ */
149
150
151 static inline uint32_t ttm_bo_type_flags(unsigned type)
152 {
153 return 1 << (type);
154 }
155
156 static void ttm_bo_release_list(struct kref *list_kref)
157 {
158 struct ttm_buffer_object *bo =
159 container_of(list_kref, struct ttm_buffer_object, list_kref);
160 struct ttm_bo_device *bdev = bo->bdev;
161 size_t acc_size = bo->acc_size;
162
163 BUG_ON(kref_referenced_p(&bo->list_kref));
164 BUG_ON(kref_referenced_p(&bo->kref));
165 BUG_ON(atomic_read(&bo->cpu_writers));
166 BUG_ON(bo->mem.mm_node != NULL);
167 BUG_ON(!list_empty(&bo->lru));
168 BUG_ON(!list_empty(&bo->ddestroy));
169
170 if (bo->ttm)
171 ttm_tt_destroy(bo->ttm);
172 atomic_dec(&bo->glob->bo_count);
173 if (bo->resv == &bo->ttm_resv)
174 reservation_object_fini(&bo->ttm_resv);
175 #ifdef __NetBSD__
176 linux_mutex_destroy(&bo->wu_mutex);
177 #else
178 mutex_destroy(&bo->wu_mutex);
179 #endif
180 if (bo->destroy)
181 bo->destroy(bo);
182 else {
183 kfree(bo);
184 }
185 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
186 }
187
188 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
189 {
190 struct ttm_bo_device *bdev = bo->bdev;
191 struct ttm_mem_type_manager *man;
192
193 lockdep_assert_held(&bo->resv->lock.base);
194
195 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
196
197 BUG_ON(!list_empty(&bo->lru));
198
199 man = &bdev->man[bo->mem.mem_type];
200 list_add_tail(&bo->lru, &man->lru);
201 kref_get(&bo->list_kref);
202
203 if (bo->ttm != NULL) {
204 list_add_tail(&bo->swap, &bo->glob->swap_lru);
205 kref_get(&bo->list_kref);
206 }
207 }
208 }
209 EXPORT_SYMBOL(ttm_bo_add_to_lru);
210
211 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
212 {
213 int put_count = 0;
214
215 if (!list_empty(&bo->swap)) {
216 list_del_init(&bo->swap);
217 ++put_count;
218 }
219 if (!list_empty(&bo->lru)) {
220 list_del_init(&bo->lru);
221 ++put_count;
222 }
223
224 /*
225 * TODO: Add a driver hook to delete from
226 * driver-specific LRU's here.
227 */
228
229 return put_count;
230 }
231
232 static void ttm_bo_ref_bug(struct kref *list_kref)
233 {
234 BUG();
235 }
236
237 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
238 bool never_free)
239 {
240 kref_sub(&bo->list_kref, count,
241 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
242 }
243
244 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
245 {
246 int put_count;
247
248 spin_lock(&bo->glob->lru_lock);
249 put_count = ttm_bo_del_from_lru(bo);
250 spin_unlock(&bo->glob->lru_lock);
251 ttm_bo_list_ref_sub(bo, put_count, true);
252 }
253 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
254
255 /*
256 * Call bo->mutex locked.
257 */
258 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
259 {
260 struct ttm_bo_device *bdev = bo->bdev;
261 struct ttm_bo_global *glob = bo->glob;
262 int ret = 0;
263 uint32_t page_flags = 0;
264
265 TTM_ASSERT_LOCKED(&bo->mutex);
266 bo->ttm = NULL;
267
268 if (bdev->need_dma32)
269 page_flags |= TTM_PAGE_FLAG_DMA32;
270
271 switch (bo->type) {
272 case ttm_bo_type_device:
273 if (zero_alloc)
274 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
275 case ttm_bo_type_kernel:
276 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
277 page_flags, glob->dummy_read_page);
278 if (unlikely(bo->ttm == NULL))
279 ret = -ENOMEM;
280 break;
281 case ttm_bo_type_sg:
282 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
283 page_flags | TTM_PAGE_FLAG_SG,
284 glob->dummy_read_page);
285 if (unlikely(bo->ttm == NULL)) {
286 ret = -ENOMEM;
287 break;
288 }
289 bo->ttm->sg = bo->sg;
290 break;
291 default:
292 pr_err("Illegal buffer object type\n");
293 ret = -EINVAL;
294 break;
295 }
296
297 #ifdef __NetBSD__
298 if (ret)
299 return ret;
300
301 /*
302 * XXX This is gross. We ought to do it the other way around:
303 * set the uao to have the main uvm object's lock. However,
304 * uvm_obj_setlock is not safe on uvm_aobjs.
305 */
306 mutex_obj_hold(bo->ttm->swap_storage->vmobjlock);
307 uvm_obj_setlock(&bo->uvmobj, bo->ttm->swap_storage->vmobjlock);
308 return 0;
309 #else
310 return ret;
311 #endif
312 }
313
314 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
315 struct ttm_mem_reg *mem,
316 bool evict, bool interruptible,
317 bool no_wait_gpu)
318 {
319 struct ttm_bo_device *bdev = bo->bdev;
320 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
321 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
322 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
323 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
324 int ret = 0;
325
326 if (old_is_pci || new_is_pci ||
327 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
328 ret = ttm_mem_io_lock(old_man, true);
329 if (unlikely(ret != 0))
330 goto out_err;
331 ttm_bo_unmap_virtual_locked(bo);
332 ttm_mem_io_unlock(old_man);
333 }
334
335 /*
336 * Create and bind a ttm if required.
337 */
338
339 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
340 if (bo->ttm == NULL) {
341 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
342 ret = ttm_bo_add_ttm(bo, zero);
343 if (ret)
344 goto out_err;
345 }
346
347 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
348 if (ret)
349 goto out_err;
350
351 if (mem->mem_type != TTM_PL_SYSTEM) {
352 ret = ttm_tt_bind(bo->ttm, mem);
353 if (ret)
354 goto out_err;
355 }
356
357 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
358 if (bdev->driver->move_notify)
359 bdev->driver->move_notify(bo, mem);
360 bo->mem = *mem;
361 mem->mm_node = NULL;
362 goto moved;
363 }
364 }
365
366 if (bdev->driver->move_notify)
367 bdev->driver->move_notify(bo, mem);
368
369 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
370 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
371 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
372 else if (bdev->driver->move)
373 ret = bdev->driver->move(bo, evict, interruptible,
374 no_wait_gpu, mem);
375 else
376 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
377
378 if (ret) {
379 if (bdev->driver->move_notify) {
380 struct ttm_mem_reg tmp_mem = *mem;
381 *mem = bo->mem;
382 bo->mem = tmp_mem;
383 bdev->driver->move_notify(bo, mem);
384 bo->mem = *mem;
385 *mem = tmp_mem;
386 }
387
388 goto out_err;
389 }
390
391 moved:
392 if (bo->evicted) {
393 if (bdev->driver->invalidate_caches) {
394 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
395 if (ret)
396 pr_err("Can not flush read caches\n");
397 }
398 bo->evicted = false;
399 }
400
401 if (bo->mem.mm_node) {
402 bo->offset = (bo->mem.start << PAGE_SHIFT) +
403 bdev->man[bo->mem.mem_type].gpu_offset;
404 bo->cur_placement = bo->mem.placement;
405 } else
406 bo->offset = 0;
407
408 return 0;
409
410 out_err:
411 new_man = &bdev->man[bo->mem.mem_type];
412 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
413 ttm_tt_unbind(bo->ttm);
414 ttm_tt_destroy(bo->ttm);
415 bo->ttm = NULL;
416 }
417
418 return ret;
419 }
420
421 /**
422 * Call bo::reserved.
423 * Will release GPU memory type usage on destruction.
424 * This is the place to put in driver specific hooks to release
425 * driver private resources.
426 * Will release the bo::reserved lock.
427 */
428
429 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
430 {
431 if (bo->bdev->driver->move_notify)
432 bo->bdev->driver->move_notify(bo, NULL);
433
434 if (bo->ttm) {
435 ttm_tt_unbind(bo->ttm);
436 ttm_tt_destroy(bo->ttm);
437 bo->ttm = NULL;
438 }
439 ttm_bo_mem_put(bo, &bo->mem);
440
441 ww_mutex_unlock (&bo->resv->lock);
442 }
443
444 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
445 {
446 struct reservation_object_list *fobj;
447 struct fence *fence;
448 int i;
449
450 fobj = reservation_object_get_list(bo->resv);
451 fence = reservation_object_get_excl(bo->resv);
452 if (fence && !fence->ops->signaled)
453 fence_enable_sw_signaling(fence);
454
455 for (i = 0; fobj && i < fobj->shared_count; ++i) {
456 fence = rcu_dereference_protected(fobj->shared[i],
457 reservation_object_held(bo->resv));
458
459 if (!fence->ops->signaled)
460 fence_enable_sw_signaling(fence);
461 }
462 }
463
464 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
465 {
466 struct ttm_bo_device *bdev = bo->bdev;
467 struct ttm_bo_global *glob = bo->glob;
468 int put_count;
469 int ret;
470
471 spin_lock(&glob->lru_lock);
472 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
473
474 if (!ret) {
475 if (!ttm_bo_wait(bo, false, false, true)) {
476 put_count = ttm_bo_del_from_lru(bo);
477
478 spin_unlock(&glob->lru_lock);
479 ttm_bo_cleanup_memtype_use(bo);
480
481 ttm_bo_list_ref_sub(bo, put_count, true);
482
483 return;
484 } else
485 ttm_bo_flush_all_fences(bo);
486
487 /*
488 * Make NO_EVICT bos immediately available to
489 * shrinkers, now that they are queued for
490 * destruction.
491 */
492 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
493 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
494 ttm_bo_add_to_lru(bo);
495 }
496
497 __ttm_bo_unreserve(bo);
498 }
499
500 kref_get(&bo->list_kref);
501 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
502 spin_unlock(&glob->lru_lock);
503
504 schedule_delayed_work(&bdev->wq,
505 ((HZ / 100) < 1) ? 1 : HZ / 100);
506 }
507
508 /**
509 * function ttm_bo_cleanup_refs_and_unlock
510 * If bo idle, remove from delayed- and lru lists, and unref.
511 * If not idle, do nothing.
512 *
513 * Must be called with lru_lock and reservation held, this function
514 * will drop both before returning.
515 *
516 * @interruptible Any sleeps should occur interruptibly.
517 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
518 */
519
520 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
521 bool interruptible,
522 bool no_wait_gpu)
523 {
524 struct ttm_bo_global *glob = bo->glob;
525 int put_count;
526 int ret;
527
528 ret = ttm_bo_wait(bo, false, false, true);
529
530 if (ret && !no_wait_gpu) {
531 long lret;
532 ww_mutex_unlock(&bo->resv->lock);
533 spin_unlock(&glob->lru_lock);
534
535 lret = reservation_object_wait_timeout_rcu(bo->resv,
536 true,
537 interruptible,
538 30 * HZ);
539
540 if (lret < 0)
541 return lret;
542 else if (lret == 0)
543 return -EBUSY;
544
545 spin_lock(&glob->lru_lock);
546 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
547
548 /*
549 * We raced, and lost, someone else holds the reservation now,
550 * and is probably busy in ttm_bo_cleanup_memtype_use.
551 *
552 * Even if it's not the case, because we finished waiting any
553 * delayed destruction would succeed, so just return success
554 * here.
555 */
556 if (ret) {
557 spin_unlock(&glob->lru_lock);
558 return 0;
559 }
560
561 /*
562 * remove sync_obj with ttm_bo_wait, the wait should be
563 * finished, and no new wait object should have been added.
564 */
565 ret = ttm_bo_wait(bo, false, false, true);
566 WARN_ON(ret);
567 }
568
569 if (ret || unlikely(list_empty(&bo->ddestroy))) {
570 __ttm_bo_unreserve(bo);
571 spin_unlock(&glob->lru_lock);
572 return ret;
573 }
574
575 put_count = ttm_bo_del_from_lru(bo);
576 list_del_init(&bo->ddestroy);
577 ++put_count;
578
579 spin_unlock(&glob->lru_lock);
580 ttm_bo_cleanup_memtype_use(bo);
581
582 ttm_bo_list_ref_sub(bo, put_count, true);
583
584 return 0;
585 }
586
587 /**
588 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
589 * encountered buffers.
590 */
591
592 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
593 {
594 struct ttm_bo_global *glob = bdev->glob;
595 struct ttm_buffer_object *entry = NULL;
596 int ret = 0;
597
598 spin_lock(&glob->lru_lock);
599 if (list_empty(&bdev->ddestroy))
600 goto out_unlock;
601
602 entry = list_first_entry(&bdev->ddestroy,
603 struct ttm_buffer_object, ddestroy);
604 kref_get(&entry->list_kref);
605
606 for (;;) {
607 struct ttm_buffer_object *nentry = NULL;
608
609 if (entry->ddestroy.next != &bdev->ddestroy) {
610 nentry = list_first_entry(&entry->ddestroy,
611 struct ttm_buffer_object, ddestroy);
612 kref_get(&nentry->list_kref);
613 }
614
615 ret = __ttm_bo_reserve(entry, false, true, false, NULL);
616 if (remove_all && ret) {
617 spin_unlock(&glob->lru_lock);
618 ret = __ttm_bo_reserve(entry, false, false,
619 false, NULL);
620 spin_lock(&glob->lru_lock);
621 }
622
623 if (!ret)
624 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
625 !remove_all);
626 else
627 spin_unlock(&glob->lru_lock);
628
629 kref_put(&entry->list_kref, ttm_bo_release_list);
630 entry = nentry;
631
632 if (ret || !entry)
633 goto out;
634
635 spin_lock(&glob->lru_lock);
636 if (list_empty(&entry->ddestroy))
637 break;
638 }
639
640 out_unlock:
641 spin_unlock(&glob->lru_lock);
642 out:
643 if (entry)
644 kref_put(&entry->list_kref, ttm_bo_release_list);
645 return ret;
646 }
647
648 static void ttm_bo_delayed_workqueue(struct work_struct *work)
649 {
650 struct ttm_bo_device *bdev =
651 container_of(work, struct ttm_bo_device, wq.work);
652
653 if (ttm_bo_delayed_delete(bdev, false)) {
654 schedule_delayed_work(&bdev->wq,
655 ((HZ / 100) < 1) ? 1 : HZ / 100);
656 }
657 }
658
659 static void ttm_bo_release(struct kref *kref)
660 {
661 struct ttm_buffer_object *bo =
662 container_of(kref, struct ttm_buffer_object, kref);
663 struct ttm_bo_device *bdev = bo->bdev;
664 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
665
666 #ifdef __NetBSD__
667 uvm_obj_destroy(&bo->uvmobj, true);
668 #endif
669 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
670 #ifdef __NetBSD__
671 drm_vma_node_destroy(&bo->vma_node);
672 #endif
673 ttm_mem_io_lock(man, false);
674 ttm_mem_io_free_vm(bo);
675 ttm_mem_io_unlock(man);
676 ttm_bo_cleanup_refs_or_queue(bo);
677 kref_put(&bo->list_kref, ttm_bo_release_list);
678 }
679
680 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
681 {
682 struct ttm_buffer_object *bo = *p_bo;
683
684 *p_bo = NULL;
685 kref_put(&bo->kref, ttm_bo_release);
686 }
687 EXPORT_SYMBOL(ttm_bo_unref);
688
689 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
690 {
691 return cancel_delayed_work_sync(&bdev->wq);
692 }
693 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
694
695 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
696 {
697 if (resched)
698 schedule_delayed_work(&bdev->wq,
699 ((HZ / 100) < 1) ? 1 : HZ / 100);
700 }
701 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
702
703 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
704 bool no_wait_gpu)
705 {
706 struct ttm_bo_device *bdev = bo->bdev;
707 struct ttm_mem_reg evict_mem;
708 struct ttm_placement placement;
709 int ret = 0;
710
711 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
712
713 if (unlikely(ret != 0)) {
714 if (ret != -ERESTARTSYS) {
715 pr_err("Failed to expire sync object before buffer eviction\n");
716 }
717 goto out;
718 }
719
720 lockdep_assert_held(&bo->resv->lock.base);
721
722 evict_mem = bo->mem;
723 evict_mem.mm_node = NULL;
724 evict_mem.bus.io_reserved_vm = false;
725 evict_mem.bus.io_reserved_count = 0;
726
727 placement.num_placement = 0;
728 placement.num_busy_placement = 0;
729 bdev->driver->evict_flags(bo, &placement);
730 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
731 no_wait_gpu);
732 if (ret) {
733 if (ret != -ERESTARTSYS) {
734 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
735 bo);
736 ttm_bo_mem_space_debug(bo, &placement);
737 }
738 goto out;
739 }
740
741 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
742 no_wait_gpu);
743 if (ret) {
744 if (ret != -ERESTARTSYS)
745 pr_err("Buffer eviction failed\n");
746 ttm_bo_mem_put(bo, &evict_mem);
747 goto out;
748 }
749 bo->evicted = true;
750 out:
751 return ret;
752 }
753
754 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
755 uint32_t mem_type,
756 const struct ttm_place *place,
757 bool interruptible,
758 bool no_wait_gpu)
759 {
760 struct ttm_bo_global *glob = bdev->glob;
761 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
762 struct ttm_buffer_object *bo;
763 int ret = -EBUSY, put_count;
764
765 spin_lock(&glob->lru_lock);
766 list_for_each_entry(bo, &man->lru, lru) {
767 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
768 if (!ret) {
769 if (place && (place->fpfn || place->lpfn)) {
770 /* Don't evict this BO if it's outside of the
771 * requested placement range
772 */
773 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
774 (place->lpfn && place->lpfn <= bo->mem.start)) {
775 __ttm_bo_unreserve(bo);
776 ret = -EBUSY;
777 continue;
778 }
779 }
780
781 break;
782 }
783 }
784
785 if (ret) {
786 spin_unlock(&glob->lru_lock);
787 return ret;
788 }
789
790 kref_get(&bo->list_kref);
791
792 if (!list_empty(&bo->ddestroy)) {
793 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
794 no_wait_gpu);
795 kref_put(&bo->list_kref, ttm_bo_release_list);
796 return ret;
797 }
798
799 put_count = ttm_bo_del_from_lru(bo);
800 spin_unlock(&glob->lru_lock);
801
802 BUG_ON(ret != 0);
803
804 ttm_bo_list_ref_sub(bo, put_count, true);
805
806 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
807 ttm_bo_unreserve(bo);
808
809 kref_put(&bo->list_kref, ttm_bo_release_list);
810 return ret;
811 }
812
813 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
814 {
815 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
816
817 if (mem->mm_node)
818 (*man->func->put_node)(man, mem);
819 }
820 EXPORT_SYMBOL(ttm_bo_mem_put);
821
822 /**
823 * Repeatedly evict memory from the LRU for @mem_type until we create enough
824 * space, or we've evicted everything and there isn't enough space.
825 */
826 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
827 uint32_t mem_type,
828 const struct ttm_place *place,
829 struct ttm_mem_reg *mem,
830 bool interruptible,
831 bool no_wait_gpu)
832 {
833 struct ttm_bo_device *bdev = bo->bdev;
834 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
835 int ret;
836
837 do {
838 ret = (*man->func->get_node)(man, bo, place, mem);
839 if (unlikely(ret != 0))
840 return ret;
841 if (mem->mm_node)
842 break;
843 ret = ttm_mem_evict_first(bdev, mem_type, place,
844 interruptible, no_wait_gpu);
845 if (unlikely(ret != 0))
846 return ret;
847 } while (1);
848 if (mem->mm_node == NULL)
849 return -ENOMEM;
850 mem->mem_type = mem_type;
851 return 0;
852 }
853
854 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
855 uint32_t cur_placement,
856 uint32_t proposed_placement)
857 {
858 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
859 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
860
861 /**
862 * Keep current caching if possible.
863 */
864
865 if ((cur_placement & caching) != 0)
866 result |= (cur_placement & caching);
867 else if ((man->default_caching & caching) != 0)
868 result |= man->default_caching;
869 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
870 result |= TTM_PL_FLAG_CACHED;
871 else if ((TTM_PL_FLAG_WC & caching) != 0)
872 result |= TTM_PL_FLAG_WC;
873 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
874 result |= TTM_PL_FLAG_UNCACHED;
875
876 return result;
877 }
878
879 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
880 uint32_t mem_type,
881 const struct ttm_place *place,
882 uint32_t *masked_placement)
883 {
884 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
885
886 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
887 return false;
888
889 if ((place->flags & man->available_caching) == 0)
890 return false;
891
892 cur_flags |= (place->flags & man->available_caching);
893
894 *masked_placement = cur_flags;
895 return true;
896 }
897
898 /**
899 * Creates space for memory region @mem according to its type.
900 *
901 * This function first searches for free space in compatible memory types in
902 * the priority order defined by the driver. If free space isn't found, then
903 * ttm_bo_mem_force_space is attempted in priority order to evict and find
904 * space.
905 */
906 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
907 struct ttm_placement *placement,
908 struct ttm_mem_reg *mem,
909 bool interruptible,
910 bool no_wait_gpu)
911 {
912 struct ttm_bo_device *bdev = bo->bdev;
913 struct ttm_mem_type_manager *man;
914 uint32_t mem_type = TTM_PL_SYSTEM;
915 uint32_t cur_flags = 0;
916 bool type_found = false;
917 bool type_ok = false;
918 bool has_erestartsys = false;
919 int i, ret;
920
921 mem->mm_node = NULL;
922 for (i = 0; i < placement->num_placement; ++i) {
923 const struct ttm_place *place = &placement->placement[i];
924
925 ret = ttm_mem_type_from_place(place, &mem_type);
926 if (ret)
927 return ret;
928 man = &bdev->man[mem_type];
929 if (!man->has_type || !man->use_type)
930 continue;
931
932 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
933 &cur_flags);
934
935 if (!type_ok)
936 continue;
937
938 type_found = true;
939 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
940 cur_flags);
941 /*
942 * Use the access and other non-mapping-related flag bits from
943 * the memory placement flags to the current flags
944 */
945 ttm_flag_masked(&cur_flags, place->flags,
946 ~TTM_PL_MASK_MEMTYPE);
947
948 if (mem_type == TTM_PL_SYSTEM)
949 break;
950
951 ret = (*man->func->get_node)(man, bo, place, mem);
952 if (unlikely(ret))
953 return ret;
954
955 if (mem->mm_node)
956 break;
957 }
958
959 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
960 mem->mem_type = mem_type;
961 mem->placement = cur_flags;
962 return 0;
963 }
964
965 for (i = 0; i < placement->num_busy_placement; ++i) {
966 const struct ttm_place *place = &placement->busy_placement[i];
967
968 ret = ttm_mem_type_from_place(place, &mem_type);
969 if (ret)
970 return ret;
971 man = &bdev->man[mem_type];
972 if (!man->has_type || !man->use_type)
973 continue;
974 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
975 continue;
976
977 type_found = true;
978 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
979 cur_flags);
980 /*
981 * Use the access and other non-mapping-related flag bits from
982 * the memory placement flags to the current flags
983 */
984 ttm_flag_masked(&cur_flags, place->flags,
985 ~TTM_PL_MASK_MEMTYPE);
986
987 if (mem_type == TTM_PL_SYSTEM) {
988 mem->mem_type = mem_type;
989 mem->placement = cur_flags;
990 mem->mm_node = NULL;
991 return 0;
992 }
993
994 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
995 interruptible, no_wait_gpu);
996 if (ret == 0 && mem->mm_node) {
997 mem->placement = cur_flags;
998 return 0;
999 }
1000 if (ret == -ERESTARTSYS)
1001 has_erestartsys = true;
1002 }
1003
1004 if (!type_found) {
1005 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
1006 return -EINVAL;
1007 }
1008
1009 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1010 }
1011 EXPORT_SYMBOL(ttm_bo_mem_space);
1012
1013 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1014 struct ttm_placement *placement,
1015 bool interruptible,
1016 bool no_wait_gpu)
1017 {
1018 int ret = 0;
1019 struct ttm_mem_reg mem;
1020
1021 lockdep_assert_held(&bo->resv->lock.base);
1022
1023 /*
1024 * FIXME: It's possible to pipeline buffer moves.
1025 * Have the driver move function wait for idle when necessary,
1026 * instead of doing it here.
1027 */
1028 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1029 if (ret)
1030 return ret;
1031 mem.num_pages = bo->num_pages;
1032 mem.size = mem.num_pages << PAGE_SHIFT;
1033 mem.page_alignment = bo->mem.page_alignment;
1034 mem.bus.is_iomem = false;
1035 mem.bus.io_reserved_vm = false;
1036 mem.bus.io_reserved_count = 0;
1037 /*
1038 * Determine where to move the buffer.
1039 */
1040 ret = ttm_bo_mem_space(bo, placement, &mem,
1041 interruptible, no_wait_gpu);
1042 if (ret)
1043 goto out_unlock;
1044 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1045 interruptible, no_wait_gpu);
1046 out_unlock:
1047 if (ret && mem.mm_node)
1048 ttm_bo_mem_put(bo, &mem);
1049 return ret;
1050 }
1051
1052 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1053 struct ttm_mem_reg *mem,
1054 uint32_t *new_flags)
1055 {
1056 int i;
1057
1058 for (i = 0; i < placement->num_placement; i++) {
1059 const struct ttm_place *heap = &placement->placement[i];
1060 if (mem->mm_node &&
1061 (mem->start < heap->fpfn ||
1062 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1063 continue;
1064
1065 *new_flags = heap->flags;
1066 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1067 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1068 return true;
1069 }
1070
1071 for (i = 0; i < placement->num_busy_placement; i++) {
1072 const struct ttm_place *heap = &placement->busy_placement[i];
1073 if (mem->mm_node &&
1074 (mem->start < heap->fpfn ||
1075 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1076 continue;
1077
1078 *new_flags = heap->flags;
1079 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1080 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1081 return true;
1082 }
1083
1084 return false;
1085 }
1086 EXPORT_SYMBOL(ttm_bo_mem_compat);
1087
1088 int ttm_bo_validate(struct ttm_buffer_object *bo,
1089 struct ttm_placement *placement,
1090 bool interruptible,
1091 bool no_wait_gpu)
1092 {
1093 int ret;
1094 uint32_t new_flags;
1095
1096 lockdep_assert_held(&bo->resv->lock.base);
1097 /*
1098 * Check whether we need to move buffer.
1099 */
1100 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1101 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1102 no_wait_gpu);
1103 if (ret)
1104 return ret;
1105 } else {
1106 /*
1107 * Use the access and other non-mapping-related flag bits from
1108 * the compatible memory placement flags to the active flags
1109 */
1110 ttm_flag_masked(&bo->mem.placement, new_flags,
1111 ~TTM_PL_MASK_MEMTYPE);
1112 }
1113 /*
1114 * We might need to add a TTM.
1115 */
1116 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1117 ret = ttm_bo_add_ttm(bo, true);
1118 if (ret)
1119 return ret;
1120 }
1121 return 0;
1122 }
1123 EXPORT_SYMBOL(ttm_bo_validate);
1124
1125 int ttm_bo_init(struct ttm_bo_device *bdev,
1126 struct ttm_buffer_object *bo,
1127 unsigned long size,
1128 enum ttm_bo_type type,
1129 struct ttm_placement *placement,
1130 uint32_t page_alignment,
1131 bool interruptible,
1132 struct file *persistent_swap_storage,
1133 size_t acc_size,
1134 struct sg_table *sg,
1135 struct reservation_object *resv,
1136 void (*destroy) (struct ttm_buffer_object *))
1137 {
1138 int ret = 0;
1139 unsigned long num_pages;
1140 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1141 bool locked;
1142
1143 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1144 if (ret) {
1145 pr_err("Out of kernel memory\n");
1146 if (destroy)
1147 (*destroy)(bo);
1148 else
1149 kfree(bo);
1150 return -ENOMEM;
1151 }
1152
1153 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1154 if (num_pages == 0) {
1155 pr_err("Illegal buffer object size\n");
1156 if (destroy)
1157 (*destroy)(bo);
1158 else
1159 kfree(bo);
1160 ttm_mem_global_free(mem_glob, acc_size);
1161 return -EINVAL;
1162 }
1163 bo->destroy = destroy;
1164
1165 kref_init(&bo->kref);
1166 kref_init(&bo->list_kref);
1167 atomic_set(&bo->cpu_writers, 0);
1168 INIT_LIST_HEAD(&bo->lru);
1169 INIT_LIST_HEAD(&bo->ddestroy);
1170 INIT_LIST_HEAD(&bo->swap);
1171 INIT_LIST_HEAD(&bo->io_reserve_lru);
1172 #ifdef __NetBSD__
1173 linux_mutex_init(&bo->wu_mutex);
1174 #else
1175 mutex_init(&bo->wu_mutex);
1176 #endif
1177 bo->bdev = bdev;
1178 bo->glob = bdev->glob;
1179 bo->type = type;
1180 bo->num_pages = num_pages;
1181 bo->mem.size = num_pages << PAGE_SHIFT;
1182 bo->mem.mem_type = TTM_PL_SYSTEM;
1183 bo->mem.num_pages = bo->num_pages;
1184 bo->mem.mm_node = NULL;
1185 bo->mem.page_alignment = page_alignment;
1186 bo->mem.bus.io_reserved_vm = false;
1187 bo->mem.bus.io_reserved_count = 0;
1188 bo->priv_flags = 0;
1189 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1190 bo->persistent_swap_storage = persistent_swap_storage;
1191 bo->acc_size = acc_size;
1192 bo->sg = sg;
1193 if (resv) {
1194 bo->resv = resv;
1195 lockdep_assert_held(&bo->resv->lock.base);
1196 } else {
1197 bo->resv = &bo->ttm_resv;
1198 reservation_object_init(&bo->ttm_resv);
1199 }
1200 atomic_inc(&bo->glob->bo_count);
1201 #ifdef __NetBSD__
1202 drm_vma_node_init(&bo->vma_node);
1203 uvm_obj_init(&bo->uvmobj, bdev->driver->ttm_uvm_ops, true, 1);
1204 #else
1205 drm_vma_node_reset(&bo->vma_node);
1206 #endif
1207
1208 /*
1209 * For ttm_bo_type_device buffers, allocate
1210 * address space from the device.
1211 */
1212 if (bo->type == ttm_bo_type_device ||
1213 bo->type == ttm_bo_type_sg)
1214 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1215 bo->mem.num_pages);
1216
1217 /* passed reservation objects should already be locked,
1218 * since otherwise lockdep will be angered in radeon.
1219 */
1220 if (!resv) {
1221 locked = ww_mutex_trylock(&bo->resv->lock);
1222 WARN_ON(!locked);
1223 }
1224
1225 if (likely(!ret))
1226 ret = ttm_bo_validate(bo, placement, interruptible, false);
1227
1228 if (!resv)
1229 ttm_bo_unreserve(bo);
1230
1231 if (unlikely(ret))
1232 ttm_bo_unref(&bo);
1233
1234 return ret;
1235 }
1236 EXPORT_SYMBOL(ttm_bo_init);
1237
1238 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1239 unsigned long bo_size,
1240 unsigned struct_size)
1241 {
1242 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1243 size_t size = 0;
1244
1245 size += ttm_round_pot(struct_size);
1246 size += PAGE_ALIGN(npages * sizeof(void *));
1247 size += ttm_round_pot(sizeof(struct ttm_tt));
1248 return size;
1249 }
1250 EXPORT_SYMBOL(ttm_bo_acc_size);
1251
1252 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1253 unsigned long bo_size,
1254 unsigned struct_size)
1255 {
1256 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1257 size_t size = 0;
1258
1259 size += ttm_round_pot(struct_size);
1260 size += PAGE_ALIGN(npages * sizeof(void *));
1261 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1262 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1263 return size;
1264 }
1265 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1266
1267 int ttm_bo_create(struct ttm_bo_device *bdev,
1268 unsigned long size,
1269 enum ttm_bo_type type,
1270 struct ttm_placement *placement,
1271 uint32_t page_alignment,
1272 bool interruptible,
1273 struct file *persistent_swap_storage,
1274 struct ttm_buffer_object **p_bo)
1275 {
1276 struct ttm_buffer_object *bo;
1277 size_t acc_size;
1278 int ret;
1279
1280 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1281 if (unlikely(bo == NULL))
1282 return -ENOMEM;
1283
1284 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1285 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1286 interruptible, persistent_swap_storage, acc_size,
1287 NULL, NULL, NULL);
1288 if (likely(ret == 0))
1289 *p_bo = bo;
1290
1291 return ret;
1292 }
1293 EXPORT_SYMBOL(ttm_bo_create);
1294
1295 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1296 unsigned mem_type, bool allow_errors)
1297 {
1298 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1299 struct ttm_bo_global *glob = bdev->glob;
1300 int ret;
1301
1302 /*
1303 * Can't use standard list traversal since we're unlocking.
1304 */
1305
1306 spin_lock(&glob->lru_lock);
1307 while (!list_empty(&man->lru)) {
1308 spin_unlock(&glob->lru_lock);
1309 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
1310 if (ret) {
1311 if (allow_errors) {
1312 return ret;
1313 } else {
1314 pr_err("Cleanup eviction failed\n");
1315 }
1316 }
1317 spin_lock(&glob->lru_lock);
1318 }
1319 spin_unlock(&glob->lru_lock);
1320 return 0;
1321 }
1322
1323 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1324 {
1325 struct ttm_mem_type_manager *man;
1326 int ret = -EINVAL;
1327
1328 if (mem_type >= TTM_NUM_MEM_TYPES) {
1329 pr_err("Illegal memory type %d\n", mem_type);
1330 return ret;
1331 }
1332 man = &bdev->man[mem_type];
1333
1334 if (!man->has_type) {
1335 pr_err("Trying to take down uninitialized memory manager type %u\n",
1336 mem_type);
1337 return ret;
1338 }
1339
1340 man->use_type = false;
1341 man->has_type = false;
1342
1343 ret = 0;
1344 if (mem_type > 0) {
1345 ttm_bo_force_list_clean(bdev, mem_type, false);
1346
1347 ret = (*man->func->takedown)(man);
1348 }
1349
1350 #ifdef __NetBSD__
1351 linux_mutex_destroy(&man->io_reserve_mutex);
1352 #else
1353 mutex_destroy(&man->io_reserve_mutex);
1354 #endif
1355
1356 return ret;
1357 }
1358 EXPORT_SYMBOL(ttm_bo_clean_mm);
1359
1360 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1361 {
1362 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1363
1364 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1365 pr_err("Illegal memory manager memory type %u\n", mem_type);
1366 return -EINVAL;
1367 }
1368
1369 if (!man->has_type) {
1370 pr_err("Memory type %u has not been initialized\n", mem_type);
1371 return 0;
1372 }
1373
1374 return ttm_bo_force_list_clean(bdev, mem_type, true);
1375 }
1376 EXPORT_SYMBOL(ttm_bo_evict_mm);
1377
1378 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1379 unsigned long p_size)
1380 {
1381 int ret = -EINVAL;
1382 struct ttm_mem_type_manager *man;
1383
1384 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1385 man = &bdev->man[type];
1386 BUG_ON(man->has_type);
1387 man->io_reserve_fastpath = true;
1388 man->use_io_reserve_lru = false;
1389 #ifdef __NetBSD__
1390 linux_mutex_init(&man->io_reserve_mutex);
1391 #else
1392 mutex_init(&man->io_reserve_mutex);
1393 #endif
1394 INIT_LIST_HEAD(&man->io_reserve_lru);
1395
1396 ret = bdev->driver->init_mem_type(bdev, type, man);
1397 if (ret)
1398 return ret;
1399 man->bdev = bdev;
1400
1401 ret = 0;
1402 if (type != TTM_PL_SYSTEM) {
1403 ret = (*man->func->init)(man, p_size);
1404 if (ret)
1405 return ret;
1406 }
1407 man->has_type = true;
1408 man->use_type = true;
1409 man->size = p_size;
1410
1411 INIT_LIST_HEAD(&man->lru);
1412
1413 return 0;
1414 }
1415 EXPORT_SYMBOL(ttm_bo_init_mm);
1416
1417 #ifndef __NetBSD__
1418 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1419 {
1420 struct ttm_bo_global *glob =
1421 container_of(kobj, struct ttm_bo_global, kobj);
1422
1423 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1424 __free_page(glob->dummy_read_page);
1425 mutex_destroy(&glob->device_list_mutex);
1426 kfree(glob);
1427 }
1428 #endif
1429
1430 void ttm_bo_global_release(struct drm_global_reference *ref)
1431 {
1432 struct ttm_bo_global *glob = ref->object;
1433
1434 #ifdef __NetBSD__
1435 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1436 BUG_ON(glob->dummy_read_page != NULL);
1437 spin_lock_destroy(&glob->lru_lock);
1438 linux_mutex_destroy(&glob->device_list_mutex);
1439 kfree(glob);
1440 #else
1441 kobject_del(&glob->kobj);
1442 kobject_put(&glob->kobj);
1443 #endif
1444 }
1445 EXPORT_SYMBOL(ttm_bo_global_release);
1446
1447 int ttm_bo_global_init(struct drm_global_reference *ref)
1448 {
1449 struct ttm_bo_global_ref *bo_ref =
1450 container_of(ref, struct ttm_bo_global_ref, ref);
1451 struct ttm_bo_global *glob = ref->object;
1452 int ret;
1453
1454 #ifdef __NetBSD__
1455 linux_mutex_init(&glob->device_list_mutex);
1456 #else
1457 mutex_init(&glob->device_list_mutex);
1458 #endif
1459 spin_lock_init(&glob->lru_lock);
1460 glob->mem_glob = bo_ref->mem_glob;
1461 #ifdef __NetBSD__
1462 /* Only used by agp back end, will fix there. */
1463 /* XXX Fix agp back end to DTRT. */
1464 glob->dummy_read_page = NULL;
1465 #else
1466 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1467
1468 if (unlikely(glob->dummy_read_page == NULL)) {
1469 ret = -ENOMEM;
1470 goto out_no_drp;
1471 }
1472 #endif
1473
1474 INIT_LIST_HEAD(&glob->swap_lru);
1475 INIT_LIST_HEAD(&glob->device_list);
1476
1477 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1478 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1479 if (unlikely(ret != 0)) {
1480 pr_err("Could not register buffer object swapout\n");
1481 goto out_no_shrink;
1482 }
1483
1484 atomic_set(&glob->bo_count, 0);
1485
1486 #ifdef __NetBSD__
1487 ret = 0;
1488 #else
1489 ret = kobject_init_and_add(
1490 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1491 if (unlikely(ret != 0))
1492 kobject_put(&glob->kobj);
1493 #endif
1494 return ret;
1495 out_no_shrink:
1496 #ifndef __NetBSD__
1497 __free_page(glob->dummy_read_page);
1498 out_no_drp:
1499 #endif
1500 kfree(glob);
1501 return ret;
1502 }
1503 EXPORT_SYMBOL(ttm_bo_global_init);
1504
1505
1506 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1507 {
1508 int ret = 0;
1509 unsigned i = TTM_NUM_MEM_TYPES;
1510 struct ttm_mem_type_manager *man;
1511 struct ttm_bo_global *glob = bdev->glob;
1512
1513 while (i--) {
1514 man = &bdev->man[i];
1515 if (man->has_type) {
1516 man->use_type = false;
1517 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1518 ret = -EBUSY;
1519 pr_err("DRM memory manager type %d is not clean\n",
1520 i);
1521 }
1522 man->has_type = false;
1523 }
1524 }
1525
1526 mutex_lock(&glob->device_list_mutex);
1527 list_del(&bdev->device_list);
1528 mutex_unlock(&glob->device_list_mutex);
1529
1530 cancel_delayed_work_sync(&bdev->wq);
1531
1532 while (ttm_bo_delayed_delete(bdev, true))
1533 ;
1534
1535 spin_lock(&glob->lru_lock);
1536 if (list_empty(&bdev->ddestroy))
1537 TTM_DEBUG("Delayed destroy list was clean\n");
1538
1539 if (list_empty(&bdev->man[0].lru))
1540 TTM_DEBUG("Swap list was clean\n");
1541 spin_unlock(&glob->lru_lock);
1542
1543 drm_vma_offset_manager_destroy(&bdev->vma_manager);
1544
1545 return ret;
1546 }
1547 EXPORT_SYMBOL(ttm_bo_device_release);
1548
1549 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1550 struct ttm_bo_global *glob,
1551 struct ttm_bo_driver *driver,
1552 #ifdef __NetBSD__
1553 bus_space_tag_t memt,
1554 bus_dma_tag_t dmat,
1555 #else
1556 struct address_space *mapping,
1557 #endif
1558 uint64_t file_page_offset,
1559 bool need_dma32)
1560 {
1561 int ret = -EINVAL;
1562
1563 bdev->driver = driver;
1564
1565 memset(bdev->man, 0, sizeof(bdev->man));
1566
1567 /*
1568 * Initialize the system memory buffer type.
1569 * Other types need to be driver / IOCTL initialized.
1570 */
1571 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1572 if (unlikely(ret != 0))
1573 goto out_no_sys;
1574
1575 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1576 0x10000000);
1577 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1578 INIT_LIST_HEAD(&bdev->ddestroy);
1579 #ifdef __NetBSD__
1580 bdev->memt = memt;
1581 bdev->dmat = dmat;
1582 #else
1583 bdev->dev_mapping = mapping;
1584 #endif
1585 bdev->glob = glob;
1586 bdev->need_dma32 = need_dma32;
1587 bdev->val_seq = 0;
1588 mutex_lock(&glob->device_list_mutex);
1589 list_add_tail(&bdev->device_list, &glob->device_list);
1590 mutex_unlock(&glob->device_list_mutex);
1591
1592 return 0;
1593 out_no_sys:
1594 return ret;
1595 }
1596 EXPORT_SYMBOL(ttm_bo_device_init);
1597
1598 /*
1599 * buffer object vm functions.
1600 */
1601
1602 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1603 {
1604 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1605
1606 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1607 if (mem->mem_type == TTM_PL_SYSTEM)
1608 return false;
1609
1610 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1611 return false;
1612
1613 if (mem->placement & TTM_PL_FLAG_CACHED)
1614 return false;
1615 }
1616 return true;
1617 }
1618
1619 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1620 {
1621 #ifndef __NetBSD__
1622 struct ttm_bo_device *bdev = bo->bdev;
1623 #endif
1624
1625 #ifdef __NetBSD__
1626 if (bo->mem.bus.is_iomem) {
1627 paddr_t start, end, pa;
1628
1629 KASSERTMSG((bo->mem.bus.base & (PAGE_SIZE - 1)) == 0,
1630 "bo bus base addr not page-aligned: %lx",
1631 bo->mem.bus.base);
1632 KASSERTMSG((bo->mem.bus.offset & (PAGE_SIZE - 1)) == 0,
1633 "bo bus offset not page-aligned: %lx",
1634 bo->mem.bus.offset);
1635 start = bo->mem.bus.base + bo->mem.bus.offset;
1636 KASSERT((bo->mem.bus.size & (PAGE_SIZE - 1)) == 0);
1637 end = start + bo->mem.bus.size;
1638
1639 for (pa = start; pa < end; pa += PAGE_SIZE)
1640 pmap_pv_protect(pa, VM_PROT_NONE);
1641 } else if (bo->ttm != NULL) {
1642 unsigned i;
1643
1644 mutex_enter(bo->uvmobj.vmobjlock);
1645 for (i = 0; i < bo->ttm->num_pages; i++)
1646 pmap_page_protect(&bo->ttm->pages[i]->p_vmp,
1647 VM_PROT_NONE);
1648 mutex_exit(bo->uvmobj.vmobjlock);
1649 }
1650 #else
1651 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
1652 #endif
1653 ttm_mem_io_free_vm(bo);
1654 }
1655
1656 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1657 {
1658 struct ttm_bo_device *bdev = bo->bdev;
1659 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1660
1661 ttm_mem_io_lock(man, false);
1662 ttm_bo_unmap_virtual_locked(bo);
1663 ttm_mem_io_unlock(man);
1664 }
1665
1666
1667 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1668
1669 int ttm_bo_wait(struct ttm_buffer_object *bo,
1670 bool lazy, bool interruptible, bool no_wait)
1671 {
1672 struct reservation_object_list *fobj;
1673 struct reservation_object *resv;
1674 struct fence *excl;
1675 long timeout = 15 * HZ;
1676 int i;
1677
1678 resv = bo->resv;
1679 fobj = reservation_object_get_list(resv);
1680 excl = reservation_object_get_excl(resv);
1681 if (excl) {
1682 if (!fence_is_signaled(excl)) {
1683 if (no_wait)
1684 return -EBUSY;
1685
1686 timeout = fence_wait_timeout(excl,
1687 interruptible, timeout);
1688 }
1689 }
1690
1691 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1692 struct fence *fence;
1693 fence = rcu_dereference_protected(fobj->shared[i],
1694 reservation_object_held(resv));
1695
1696 if (!fence_is_signaled(fence)) {
1697 if (no_wait)
1698 return -EBUSY;
1699
1700 timeout = fence_wait_timeout(fence,
1701 interruptible, timeout);
1702 }
1703 }
1704
1705 if (timeout < 0)
1706 return timeout;
1707
1708 if (timeout == 0)
1709 return -EBUSY;
1710
1711 reservation_object_add_excl_fence(resv, NULL);
1712 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1713 return 0;
1714 }
1715 EXPORT_SYMBOL(ttm_bo_wait);
1716
1717 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1718 {
1719 int ret = 0;
1720
1721 /*
1722 * Using ttm_bo_reserve makes sure the lru lists are updated.
1723 */
1724
1725 ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
1726 if (unlikely(ret != 0))
1727 return ret;
1728 ret = ttm_bo_wait(bo, false, true, no_wait);
1729 if (likely(ret == 0))
1730 atomic_inc(&bo->cpu_writers);
1731 ttm_bo_unreserve(bo);
1732 return ret;
1733 }
1734 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1735
1736 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1737 {
1738 atomic_dec(&bo->cpu_writers);
1739 }
1740 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1741
1742 /**
1743 * A buffer object shrink method that tries to swap out the first
1744 * buffer object on the bo_global::swap_lru list.
1745 */
1746
1747 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1748 {
1749 struct ttm_bo_global *glob =
1750 container_of(shrink, struct ttm_bo_global, shrink);
1751 struct ttm_buffer_object *bo;
1752 int ret = -EBUSY;
1753 int put_count;
1754
1755 spin_lock(&glob->lru_lock);
1756 list_for_each_entry(bo, &glob->swap_lru, swap) {
1757 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
1758 if (!ret)
1759 break;
1760 }
1761
1762 if (ret) {
1763 spin_unlock(&glob->lru_lock);
1764 return ret;
1765 }
1766
1767 kref_get(&bo->list_kref);
1768
1769 if (!list_empty(&bo->ddestroy)) {
1770 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1771 kref_put(&bo->list_kref, ttm_bo_release_list);
1772 return ret;
1773 }
1774
1775 put_count = ttm_bo_del_from_lru(bo);
1776 spin_unlock(&glob->lru_lock);
1777
1778 ttm_bo_list_ref_sub(bo, put_count, true);
1779
1780 /**
1781 * Wait for GPU, then move to system cached.
1782 */
1783
1784 ret = ttm_bo_wait(bo, false, false, false);
1785
1786 if (unlikely(ret != 0))
1787 goto out;
1788
1789 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1790 bo->ttm->caching_state != tt_cached) {
1791 struct ttm_mem_reg evict_mem;
1792
1793 evict_mem = bo->mem;
1794 evict_mem.mm_node = NULL;
1795 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1796 evict_mem.mem_type = TTM_PL_SYSTEM;
1797
1798 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1799 false, false);
1800 if (unlikely(ret != 0))
1801 goto out;
1802 }
1803
1804 ttm_bo_unmap_virtual(bo);
1805
1806 /**
1807 * Swap out. Buffer will be swapped in again as soon as
1808 * anyone tries to access a ttm page.
1809 */
1810
1811 if (bo->bdev->driver->swap_notify)
1812 bo->bdev->driver->swap_notify(bo);
1813
1814 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1815 out:
1816
1817 /**
1818 *
1819 * Unreserve without putting on LRU to avoid swapping out an
1820 * already swapped buffer.
1821 */
1822
1823 __ttm_bo_unreserve(bo);
1824 kref_put(&bo->list_kref, ttm_bo_release_list);
1825 return ret;
1826 }
1827
1828 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1829 {
1830 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1831 ;
1832 }
1833 EXPORT_SYMBOL(ttm_bo_swapout_all);
1834
1835 /**
1836 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1837 * unreserved
1838 *
1839 * @bo: Pointer to buffer
1840 */
1841 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1842 {
1843 int ret;
1844
1845 /*
1846 * In the absense of a wait_unlocked API,
1847 * Use the bo::wu_mutex to avoid triggering livelocks due to
1848 * concurrent use of this function. Note that this use of
1849 * bo::wu_mutex can go away if we change locking order to
1850 * mmap_sem -> bo::reserve.
1851 */
1852 ret = mutex_lock_interruptible(&bo->wu_mutex);
1853 if (unlikely(ret != 0))
1854 return -ERESTARTSYS;
1855 if (!ww_mutex_is_locked(&bo->resv->lock))
1856 goto out_unlock;
1857 ret = __ttm_bo_reserve(bo, true, false, false, NULL);
1858 if (unlikely(ret != 0))
1859 goto out_unlock;
1860 __ttm_bo_unreserve(bo);
1861
1862 out_unlock:
1863 mutex_unlock(&bo->wu_mutex);
1864 return ret;
1865 }
1866