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