1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #include <drm/ttm/ttm_bo_driver.h>
33 #include <drm/ttm/ttm_placement.h>
34 #include <drm/drm_vma_manager.h>
36 #include <linux/highmem.h>
37 #include <linux/wait.h>
38 #include <linux/slab.h>
39 #include <linux/vmalloc.h>
40 #include <linux/module.h>
41 #include <linux/dma-resv.h>
43 struct ttm_transfer_obj {
44 struct ttm_buffer_object base;
45 struct ttm_buffer_object *bo;
48 void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
50 ttm_bo_mem_put(bo, &bo->mem);
53 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
54 struct ttm_operation_ctx *ctx,
55 struct ttm_mem_reg *new_mem)
57 struct ttm_tt *ttm = bo->ttm;
58 struct ttm_mem_reg *old_mem = &bo->mem;
61 if (old_mem->mem_type != TTM_PL_SYSTEM) {
62 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
64 if (unlikely(ret != 0)) {
65 if (ret != -ERESTARTSYS)
66 pr_err("Failed to expire sync object before unbinding TTM\n");
71 ttm_bo_free_old_node(bo);
72 ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
74 old_mem->mem_type = TTM_PL_SYSTEM;
77 ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
78 if (unlikely(ret != 0))
81 if (new_mem->mem_type != TTM_PL_SYSTEM) {
82 ret = ttm_tt_bind(ttm, new_mem, ctx);
83 if (unlikely(ret != 0))
88 new_mem->mm_node = NULL;
92 EXPORT_SYMBOL(ttm_bo_move_ttm);
94 int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
96 if (likely(man->io_reserve_fastpath))
100 return mutex_lock_interruptible(&man->io_reserve_mutex);
102 mutex_lock(&man->io_reserve_mutex);
106 void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
108 if (likely(man->io_reserve_fastpath))
111 mutex_unlock(&man->io_reserve_mutex);
114 static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
116 struct ttm_buffer_object *bo;
118 if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
121 bo = list_first_entry(&man->io_reserve_lru,
122 struct ttm_buffer_object,
124 list_del_init(&bo->io_reserve_lru);
125 ttm_bo_unmap_virtual_locked(bo);
131 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
132 struct ttm_mem_reg *mem)
134 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
137 if (!bdev->driver->io_mem_reserve)
139 if (likely(man->io_reserve_fastpath))
140 return bdev->driver->io_mem_reserve(bdev, mem);
142 if (bdev->driver->io_mem_reserve &&
143 mem->bus.io_reserved_count++ == 0) {
145 ret = bdev->driver->io_mem_reserve(bdev, mem);
146 if (ret == -EAGAIN) {
147 ret = ttm_mem_io_evict(man);
155 void ttm_mem_io_free(struct ttm_bo_device *bdev,
156 struct ttm_mem_reg *mem)
158 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
160 if (likely(man->io_reserve_fastpath))
163 if (bdev->driver->io_mem_reserve &&
164 --mem->bus.io_reserved_count == 0 &&
165 bdev->driver->io_mem_free)
166 bdev->driver->io_mem_free(bdev, mem);
170 int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
172 struct ttm_mem_reg *mem = &bo->mem;
175 if (!mem->bus.io_reserved_vm) {
176 struct ttm_mem_type_manager *man =
177 &bo->bdev->man[mem->mem_type];
179 ret = ttm_mem_io_reserve(bo->bdev, mem);
180 if (unlikely(ret != 0))
182 mem->bus.io_reserved_vm = true;
183 if (man->use_io_reserve_lru)
184 list_add_tail(&bo->io_reserve_lru,
185 &man->io_reserve_lru);
190 void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
192 struct ttm_mem_reg *mem = &bo->mem;
194 if (mem->bus.io_reserved_vm) {
195 mem->bus.io_reserved_vm = false;
196 list_del_init(&bo->io_reserve_lru);
197 ttm_mem_io_free(bo->bdev, mem);
201 static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
204 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
209 (void) ttm_mem_io_lock(man, false);
210 ret = ttm_mem_io_reserve(bdev, mem);
211 ttm_mem_io_unlock(man);
212 if (ret || !mem->bus.is_iomem)
216 addr = mem->bus.addr;
218 if (mem->placement & TTM_PL_FLAG_WC)
219 addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size);
221 addr = ioremap(mem->bus.base + mem->bus.offset, mem->bus.size);
223 (void) ttm_mem_io_lock(man, false);
224 ttm_mem_io_free(bdev, mem);
225 ttm_mem_io_unlock(man);
233 static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
236 struct ttm_mem_type_manager *man;
238 man = &bdev->man[mem->mem_type];
240 if (virtual && mem->bus.addr == NULL)
242 (void) ttm_mem_io_lock(man, false);
243 ttm_mem_io_free(bdev, mem);
244 ttm_mem_io_unlock(man);
247 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
250 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
252 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
255 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
256 iowrite32(ioread32(srcP++), dstP++);
260 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
264 struct page *d = ttm->pages[page];
270 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
271 dst = kmap_atomic_prot(d, prot);
275 memcpy_fromio(dst, src, PAGE_SIZE);
282 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
286 struct page *s = ttm->pages[page];
292 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
293 src = kmap_atomic_prot(s, prot);
297 memcpy_toio(dst, src, PAGE_SIZE);
304 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
305 struct ttm_operation_ctx *ctx,
306 struct ttm_mem_reg *new_mem)
308 struct ttm_bo_device *bdev = bo->bdev;
309 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
310 struct ttm_tt *ttm = bo->ttm;
311 struct ttm_mem_reg *old_mem = &bo->mem;
312 struct ttm_mem_reg old_copy = *old_mem;
318 unsigned long add = 0;
321 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
325 ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
328 ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
333 * Single TTM move. NOP.
335 if (old_iomap == NULL && new_iomap == NULL)
339 * Don't move nonexistent data. Clear destination instead.
341 if (old_iomap == NULL &&
342 (ttm == NULL || (ttm->state == tt_unpopulated &&
343 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
344 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
349 * TTM might be null for moves within the same region.
352 ret = ttm_tt_populate(ttm, ctx);
360 if ((old_mem->mem_type == new_mem->mem_type) &&
361 (new_mem->start < old_mem->start + old_mem->size)) {
363 add = new_mem->num_pages - 1;
366 for (i = 0; i < new_mem->num_pages; ++i) {
367 page = i * dir + add;
368 if (old_iomap == NULL) {
369 pgprot_t prot = ttm_io_prot(old_mem->placement,
371 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
373 } else if (new_iomap == NULL) {
374 pgprot_t prot = ttm_io_prot(new_mem->placement,
376 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
379 ret = ttm_copy_io_page(new_iomap, old_iomap, page);
388 new_mem->mm_node = NULL;
390 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
396 ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
398 ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
401 * On error, keep the mm node!
404 ttm_bo_mem_put(bo, &old_copy);
407 EXPORT_SYMBOL(ttm_bo_move_memcpy);
409 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
411 struct ttm_transfer_obj *fbo;
413 fbo = container_of(bo, struct ttm_transfer_obj, base);
419 * ttm_buffer_object_transfer
421 * @bo: A pointer to a struct ttm_buffer_object.
422 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
423 * holding the data of @bo with the old placement.
425 * This is a utility function that may be called after an accelerated move
426 * has been scheduled. A new buffer object is created as a placeholder for
427 * the old data while it's being copied. When that buffer object is idle,
428 * it can be destroyed, releasing the space of the old placement.
433 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
434 struct ttm_buffer_object **new_obj)
436 struct ttm_transfer_obj *fbo;
439 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
444 fbo->base.mem.placement |= TTM_PL_FLAG_NO_EVICT;
450 * Fix up members that we shouldn't copy directly:
451 * TODO: Explicit member copy would probably be better here.
454 atomic_inc(&ttm_bo_glob.bo_count);
455 INIT_LIST_HEAD(&fbo->base.ddestroy);
456 INIT_LIST_HEAD(&fbo->base.lru);
457 INIT_LIST_HEAD(&fbo->base.swap);
458 INIT_LIST_HEAD(&fbo->base.io_reserve_lru);
459 fbo->base.moving = NULL;
460 drm_vma_node_reset(&fbo->base.base.vma_node);
462 kref_init(&fbo->base.kref);
463 fbo->base.destroy = &ttm_transfered_destroy;
464 fbo->base.acc_size = 0;
465 if (bo->type != ttm_bo_type_sg)
466 fbo->base.base.resv = &fbo->base.base._resv;
468 dma_resv_init(&fbo->base.base._resv);
469 fbo->base.base.dev = NULL;
470 ret = dma_resv_trylock(&fbo->base.base._resv);
473 *new_obj = &fbo->base;
477 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
479 /* Cached mappings need no adjustment */
480 if (caching_flags & TTM_PL_FLAG_CACHED)
483 #if defined(__i386__) || defined(__x86_64__)
484 if (caching_flags & TTM_PL_FLAG_WC)
485 tmp = pgprot_writecombine(tmp);
486 else if (boot_cpu_data.x86 > 3)
487 tmp = pgprot_noncached(tmp);
489 #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
490 defined(__powerpc__) || defined(__mips__)
491 if (caching_flags & TTM_PL_FLAG_WC)
492 tmp = pgprot_writecombine(tmp);
494 tmp = pgprot_noncached(tmp);
496 #if defined(__sparc__)
497 tmp = pgprot_noncached(tmp);
501 EXPORT_SYMBOL(ttm_io_prot);
503 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
504 unsigned long offset,
506 struct ttm_bo_kmap_obj *map)
508 struct ttm_mem_reg *mem = &bo->mem;
510 if (bo->mem.bus.addr) {
511 map->bo_kmap_type = ttm_bo_map_premapped;
512 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
514 map->bo_kmap_type = ttm_bo_map_iomap;
515 if (mem->placement & TTM_PL_FLAG_WC)
516 map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
519 map->virtual = ioremap(bo->mem.bus.base + bo->mem.bus.offset + offset,
522 return (!map->virtual) ? -ENOMEM : 0;
525 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
526 unsigned long start_page,
527 unsigned long num_pages,
528 struct ttm_bo_kmap_obj *map)
530 struct ttm_mem_reg *mem = &bo->mem;
531 struct ttm_operation_ctx ctx = {
532 .interruptible = false,
535 struct ttm_tt *ttm = bo->ttm;
541 ret = ttm_tt_populate(ttm, &ctx);
545 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
547 * We're mapping a single page, and the desired
548 * page protection is consistent with the bo.
551 map->bo_kmap_type = ttm_bo_map_kmap;
552 map->page = ttm->pages[start_page];
553 map->virtual = kmap(map->page);
556 * We need to use vmap to get the desired page protection
557 * or to make the buffer object look contiguous.
559 prot = ttm_io_prot(mem->placement, PAGE_KERNEL);
560 map->bo_kmap_type = ttm_bo_map_vmap;
561 map->virtual = vmap(ttm->pages + start_page, num_pages,
564 return (!map->virtual) ? -ENOMEM : 0;
567 int ttm_bo_kmap(struct ttm_buffer_object *bo,
568 unsigned long start_page, unsigned long num_pages,
569 struct ttm_bo_kmap_obj *map)
571 struct ttm_mem_type_manager *man =
572 &bo->bdev->man[bo->mem.mem_type];
573 unsigned long offset, size;
578 if (num_pages > bo->num_pages)
580 if (start_page > bo->num_pages)
583 (void) ttm_mem_io_lock(man, false);
584 ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
585 ttm_mem_io_unlock(man);
588 if (!bo->mem.bus.is_iomem) {
589 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
591 offset = start_page << PAGE_SHIFT;
592 size = num_pages << PAGE_SHIFT;
593 return ttm_bo_ioremap(bo, offset, size, map);
596 EXPORT_SYMBOL(ttm_bo_kmap);
598 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
600 struct ttm_buffer_object *bo = map->bo;
601 struct ttm_mem_type_manager *man =
602 &bo->bdev->man[bo->mem.mem_type];
606 switch (map->bo_kmap_type) {
607 case ttm_bo_map_iomap:
608 iounmap(map->virtual);
610 case ttm_bo_map_vmap:
611 vunmap(map->virtual);
613 case ttm_bo_map_kmap:
616 case ttm_bo_map_premapped:
621 (void) ttm_mem_io_lock(man, false);
622 ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
623 ttm_mem_io_unlock(man);
627 EXPORT_SYMBOL(ttm_bo_kunmap);
629 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
630 struct dma_fence *fence,
632 struct ttm_mem_reg *new_mem)
634 struct ttm_bo_device *bdev = bo->bdev;
635 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
636 struct ttm_mem_reg *old_mem = &bo->mem;
638 struct ttm_buffer_object *ghost_obj;
640 dma_resv_add_excl_fence(bo->base.resv, fence);
642 ret = ttm_bo_wait(bo, false, false);
646 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
647 ttm_tt_destroy(bo->ttm);
650 ttm_bo_free_old_node(bo);
653 * This should help pipeline ordinary buffer moves.
655 * Hang old buffer memory on a new buffer object,
656 * and leave it to be released when the GPU
657 * operation has completed.
660 dma_fence_put(bo->moving);
661 bo->moving = dma_fence_get(fence);
663 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
667 dma_resv_add_excl_fence(&ghost_obj->base._resv, fence);
670 * If we're not moving to fixed memory, the TTM object
671 * needs to stay alive. Otherwhise hang it on the ghost
672 * bo to be unbound and destroyed.
675 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
676 ghost_obj->ttm = NULL;
680 dma_resv_unlock(&ghost_obj->base._resv);
681 ttm_bo_put(ghost_obj);
685 new_mem->mm_node = NULL;
689 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
691 int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
692 struct dma_fence *fence, bool evict,
693 struct ttm_mem_reg *new_mem)
695 struct ttm_bo_device *bdev = bo->bdev;
696 struct ttm_mem_reg *old_mem = &bo->mem;
698 struct ttm_mem_type_manager *from = &bdev->man[old_mem->mem_type];
699 struct ttm_mem_type_manager *to = &bdev->man[new_mem->mem_type];
703 dma_resv_add_excl_fence(bo->base.resv, fence);
706 struct ttm_buffer_object *ghost_obj;
709 * This should help pipeline ordinary buffer moves.
711 * Hang old buffer memory on a new buffer object,
712 * and leave it to be released when the GPU
713 * operation has completed.
716 dma_fence_put(bo->moving);
717 bo->moving = dma_fence_get(fence);
719 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
723 dma_resv_add_excl_fence(&ghost_obj->base._resv, fence);
726 * If we're not moving to fixed memory, the TTM object
727 * needs to stay alive. Otherwhise hang it on the ghost
728 * bo to be unbound and destroyed.
731 if (!(to->flags & TTM_MEMTYPE_FLAG_FIXED))
732 ghost_obj->ttm = NULL;
736 dma_resv_unlock(&ghost_obj->base._resv);
737 ttm_bo_put(ghost_obj);
739 } else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) {
742 * BO doesn't have a TTM we need to bind/unbind. Just remember
743 * this eviction and free up the allocation
746 spin_lock(&from->move_lock);
747 if (!from->move || dma_fence_is_later(fence, from->move)) {
748 dma_fence_put(from->move);
749 from->move = dma_fence_get(fence);
751 spin_unlock(&from->move_lock);
753 ttm_bo_free_old_node(bo);
755 dma_fence_put(bo->moving);
756 bo->moving = dma_fence_get(fence);
760 * Last resort, wait for the move to be completed.
762 * Should never happen in pratice.
765 ret = ttm_bo_wait(bo, false, false);
769 if (to->flags & TTM_MEMTYPE_FLAG_FIXED) {
770 ttm_tt_destroy(bo->ttm);
773 ttm_bo_free_old_node(bo);
777 new_mem->mm_node = NULL;
781 EXPORT_SYMBOL(ttm_bo_pipeline_move);
783 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
785 struct ttm_buffer_object *ghost;
788 ret = ttm_buffer_object_transfer(bo, &ghost);
792 ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
793 /* Last resort, wait for the BO to be idle when we are OOM */
795 ttm_bo_wait(bo, false, false);
797 memset(&bo->mem, 0, sizeof(bo->mem));
798 bo->mem.mem_type = TTM_PL_SYSTEM;
801 dma_resv_unlock(&ghost->base._resv);