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