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_resource_free(bo, &bo->mem);
53 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
54 struct ttm_operation_ctx *ctx,
55 struct ttm_resource *new_mem)
57 struct ttm_tt *ttm = bo->ttm;
58 struct ttm_resource *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");
70 ttm_tt_unbind(bo->bdev, ttm);
71 ttm_bo_free_old_node(bo);
72 old_mem->mem_type = TTM_PL_SYSTEM;
75 ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
76 if (unlikely(ret != 0))
79 if (new_mem->mem_type != TTM_PL_SYSTEM) {
80 ret = ttm_tt_bind(bo->bdev, ttm, new_mem, ctx);
81 if (unlikely(ret != 0))
86 new_mem->mm_node = NULL;
90 EXPORT_SYMBOL(ttm_bo_move_ttm);
92 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
93 struct ttm_resource *mem)
95 if (mem->bus.offset || mem->bus.addr)
98 mem->bus.is_iomem = false;
99 if (!bdev->driver->io_mem_reserve)
102 return bdev->driver->io_mem_reserve(bdev, mem);
105 void ttm_mem_io_free(struct ttm_bo_device *bdev,
106 struct ttm_resource *mem)
108 if (!mem->bus.offset && !mem->bus.addr)
111 if (bdev->driver->io_mem_free)
112 bdev->driver->io_mem_free(bdev, mem);
115 mem->bus.addr = NULL;
118 static int ttm_resource_ioremap(struct ttm_bo_device *bdev,
119 struct ttm_resource *mem,
126 ret = ttm_mem_io_reserve(bdev, mem);
127 if (ret || !mem->bus.is_iomem)
131 addr = mem->bus.addr;
133 size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
135 if (mem->placement & TTM_PL_FLAG_WC)
136 addr = ioremap_wc(mem->bus.offset, bus_size);
138 addr = ioremap(mem->bus.offset, bus_size);
140 ttm_mem_io_free(bdev, mem);
148 static void ttm_resource_iounmap(struct ttm_bo_device *bdev,
149 struct ttm_resource *mem,
152 if (virtual && mem->bus.addr == NULL)
154 ttm_mem_io_free(bdev, mem);
157 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
160 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
162 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
165 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
166 iowrite32(ioread32(srcP++), dstP++);
170 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
174 struct page *d = ttm->pages[page];
180 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
181 dst = kmap_atomic_prot(d, prot);
185 memcpy_fromio(dst, src, PAGE_SIZE);
192 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
196 struct page *s = ttm->pages[page];
202 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
203 src = kmap_atomic_prot(s, prot);
207 memcpy_toio(dst, src, PAGE_SIZE);
214 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
215 struct ttm_operation_ctx *ctx,
216 struct ttm_resource *new_mem)
218 struct ttm_bo_device *bdev = bo->bdev;
219 struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
220 struct ttm_tt *ttm = bo->ttm;
221 struct ttm_resource *old_mem = &bo->mem;
222 struct ttm_resource old_copy = *old_mem;
228 unsigned long add = 0;
231 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
235 ret = ttm_resource_ioremap(bdev, old_mem, &old_iomap);
238 ret = ttm_resource_ioremap(bdev, new_mem, &new_iomap);
243 * Single TTM move. NOP.
245 if (old_iomap == NULL && new_iomap == NULL)
249 * Don't move nonexistent data. Clear destination instead.
251 if (old_iomap == NULL &&
252 (ttm == NULL || (!ttm_tt_is_populated(ttm) &&
253 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
254 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
259 * TTM might be null for moves within the same region.
262 ret = ttm_tt_populate(bdev, ttm, ctx);
270 if ((old_mem->mem_type == new_mem->mem_type) &&
271 (new_mem->start < old_mem->start + old_mem->size)) {
273 add = new_mem->num_pages - 1;
276 for (i = 0; i < new_mem->num_pages; ++i) {
277 page = i * dir + add;
278 if (old_iomap == NULL) {
279 pgprot_t prot = ttm_io_prot(old_mem->placement,
281 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
283 } else if (new_iomap == NULL) {
284 pgprot_t prot = ttm_io_prot(new_mem->placement,
286 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
289 ret = ttm_copy_io_page(new_iomap, old_iomap, page);
298 new_mem->mm_node = NULL;
301 ttm_bo_tt_destroy(bo);
304 ttm_resource_iounmap(bdev, old_mem, new_iomap);
306 ttm_resource_iounmap(bdev, &old_copy, old_iomap);
309 * On error, keep the mm node!
312 ttm_resource_free(bo, &old_copy);
315 EXPORT_SYMBOL(ttm_bo_move_memcpy);
317 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
319 struct ttm_transfer_obj *fbo;
321 fbo = container_of(bo, struct ttm_transfer_obj, base);
327 * ttm_buffer_object_transfer
329 * @bo: A pointer to a struct ttm_buffer_object.
330 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
331 * holding the data of @bo with the old placement.
333 * This is a utility function that may be called after an accelerated move
334 * has been scheduled. A new buffer object is created as a placeholder for
335 * the old data while it's being copied. When that buffer object is idle,
336 * it can be destroyed, releasing the space of the old placement.
341 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
342 struct ttm_buffer_object **new_obj)
344 struct ttm_transfer_obj *fbo;
347 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
352 fbo->base.mem.placement |= TTM_PL_FLAG_NO_EVICT;
358 * Fix up members that we shouldn't copy directly:
359 * TODO: Explicit member copy would probably be better here.
362 atomic_inc(&ttm_bo_glob.bo_count);
363 INIT_LIST_HEAD(&fbo->base.ddestroy);
364 INIT_LIST_HEAD(&fbo->base.lru);
365 INIT_LIST_HEAD(&fbo->base.swap);
366 fbo->base.moving = NULL;
367 drm_vma_node_reset(&fbo->base.base.vma_node);
369 kref_init(&fbo->base.kref);
370 fbo->base.destroy = &ttm_transfered_destroy;
371 fbo->base.acc_size = 0;
372 if (bo->type != ttm_bo_type_sg)
373 fbo->base.base.resv = &fbo->base.base._resv;
375 dma_resv_init(&fbo->base.base._resv);
376 fbo->base.base.dev = NULL;
377 ret = dma_resv_trylock(&fbo->base.base._resv);
380 *new_obj = &fbo->base;
384 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
386 /* Cached mappings need no adjustment */
387 if (caching_flags & TTM_PL_FLAG_CACHED)
390 #if defined(__i386__) || defined(__x86_64__)
391 if (caching_flags & TTM_PL_FLAG_WC)
392 tmp = pgprot_writecombine(tmp);
393 else if (boot_cpu_data.x86 > 3)
394 tmp = pgprot_noncached(tmp);
396 #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
397 defined(__powerpc__) || defined(__mips__)
398 if (caching_flags & TTM_PL_FLAG_WC)
399 tmp = pgprot_writecombine(tmp);
401 tmp = pgprot_noncached(tmp);
403 #if defined(__sparc__)
404 tmp = pgprot_noncached(tmp);
408 EXPORT_SYMBOL(ttm_io_prot);
410 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
411 unsigned long offset,
413 struct ttm_bo_kmap_obj *map)
415 struct ttm_resource *mem = &bo->mem;
417 if (bo->mem.bus.addr) {
418 map->bo_kmap_type = ttm_bo_map_premapped;
419 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
421 map->bo_kmap_type = ttm_bo_map_iomap;
422 if (mem->placement & TTM_PL_FLAG_WC)
423 map->virtual = ioremap_wc(bo->mem.bus.offset + offset,
426 map->virtual = ioremap(bo->mem.bus.offset + offset,
429 return (!map->virtual) ? -ENOMEM : 0;
432 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
433 unsigned long start_page,
434 unsigned long num_pages,
435 struct ttm_bo_kmap_obj *map)
437 struct ttm_resource *mem = &bo->mem;
438 struct ttm_operation_ctx ctx = {
439 .interruptible = false,
442 struct ttm_tt *ttm = bo->ttm;
448 ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
452 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
454 * We're mapping a single page, and the desired
455 * page protection is consistent with the bo.
458 map->bo_kmap_type = ttm_bo_map_kmap;
459 map->page = ttm->pages[start_page];
460 map->virtual = kmap(map->page);
463 * We need to use vmap to get the desired page protection
464 * or to make the buffer object look contiguous.
466 prot = ttm_io_prot(mem->placement, PAGE_KERNEL);
467 map->bo_kmap_type = ttm_bo_map_vmap;
468 map->virtual = vmap(ttm->pages + start_page, num_pages,
471 return (!map->virtual) ? -ENOMEM : 0;
474 int ttm_bo_kmap(struct ttm_buffer_object *bo,
475 unsigned long start_page, unsigned long num_pages,
476 struct ttm_bo_kmap_obj *map)
478 unsigned long offset, size;
483 if (num_pages > bo->num_pages)
485 if (start_page > bo->num_pages)
488 ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
491 if (!bo->mem.bus.is_iomem) {
492 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
494 offset = start_page << PAGE_SHIFT;
495 size = num_pages << PAGE_SHIFT;
496 return ttm_bo_ioremap(bo, offset, size, map);
499 EXPORT_SYMBOL(ttm_bo_kmap);
501 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
505 switch (map->bo_kmap_type) {
506 case ttm_bo_map_iomap:
507 iounmap(map->virtual);
509 case ttm_bo_map_vmap:
510 vunmap(map->virtual);
512 case ttm_bo_map_kmap:
515 case ttm_bo_map_premapped:
520 ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
524 EXPORT_SYMBOL(ttm_bo_kunmap);
526 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
527 struct dma_fence *fence,
529 struct ttm_resource *new_mem)
531 struct ttm_bo_device *bdev = bo->bdev;
532 struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
533 struct ttm_resource *old_mem = &bo->mem;
535 struct ttm_buffer_object *ghost_obj;
537 dma_resv_add_excl_fence(bo->base.resv, fence);
539 ret = ttm_bo_wait(bo, false, false);
544 ttm_bo_tt_destroy(bo);
545 ttm_bo_free_old_node(bo);
548 * This should help pipeline ordinary buffer moves.
550 * Hang old buffer memory on a new buffer object,
551 * and leave it to be released when the GPU
552 * operation has completed.
555 dma_fence_put(bo->moving);
556 bo->moving = dma_fence_get(fence);
558 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
562 dma_resv_add_excl_fence(&ghost_obj->base._resv, fence);
565 * If we're not moving to fixed memory, the TTM object
566 * needs to stay alive. Otherwhise hang it on the ghost
567 * bo to be unbound and destroyed.
571 ghost_obj->ttm = NULL;
575 dma_resv_unlock(&ghost_obj->base._resv);
576 ttm_bo_put(ghost_obj);
580 new_mem->mm_node = NULL;
584 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
586 int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
587 struct dma_fence *fence, bool evict,
588 struct ttm_resource *new_mem)
590 struct ttm_bo_device *bdev = bo->bdev;
591 struct ttm_resource *old_mem = &bo->mem;
593 struct ttm_resource_manager *from = ttm_manager_type(bdev, old_mem->mem_type);
594 struct ttm_resource_manager *to = ttm_manager_type(bdev, new_mem->mem_type);
598 dma_resv_add_excl_fence(bo->base.resv, fence);
601 struct ttm_buffer_object *ghost_obj;
604 * This should help pipeline ordinary buffer moves.
606 * Hang old buffer memory on a new buffer object,
607 * and leave it to be released when the GPU
608 * operation has completed.
611 dma_fence_put(bo->moving);
612 bo->moving = dma_fence_get(fence);
614 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
618 dma_resv_add_excl_fence(&ghost_obj->base._resv, fence);
621 * If we're not moving to fixed memory, the TTM object
622 * needs to stay alive. Otherwhise hang it on the ghost
623 * bo to be unbound and destroyed.
627 ghost_obj->ttm = NULL;
631 dma_resv_unlock(&ghost_obj->base._resv);
632 ttm_bo_put(ghost_obj);
634 } else if (!from->use_tt) {
637 * BO doesn't have a TTM we need to bind/unbind. Just remember
638 * this eviction and free up the allocation
641 spin_lock(&from->move_lock);
642 if (!from->move || dma_fence_is_later(fence, from->move)) {
643 dma_fence_put(from->move);
644 from->move = dma_fence_get(fence);
646 spin_unlock(&from->move_lock);
648 ttm_bo_free_old_node(bo);
650 dma_fence_put(bo->moving);
651 bo->moving = dma_fence_get(fence);
655 * Last resort, wait for the move to be completed.
657 * Should never happen in pratice.
660 ret = ttm_bo_wait(bo, false, false);
665 ttm_bo_tt_destroy(bo);
666 ttm_bo_free_old_node(bo);
670 new_mem->mm_node = NULL;
674 EXPORT_SYMBOL(ttm_bo_pipeline_move);
676 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
678 struct ttm_buffer_object *ghost;
681 ret = ttm_buffer_object_transfer(bo, &ghost);
685 ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
686 /* Last resort, wait for the BO to be idle when we are OOM */
688 ttm_bo_wait(bo, false, false);
690 memset(&bo->mem, 0, sizeof(bo->mem));
691 bo->mem.mem_type = TTM_PL_SYSTEM;
694 dma_resv_unlock(&ghost->base._resv);