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