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