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>
35 #include <linux/dma-buf-map.h>
37 #include <linux/highmem.h>
38 #include <linux/wait.h>
39 #include <linux/slab.h>
40 #include <linux/vmalloc.h>
41 #include <linux/module.h>
42 #include <linux/dma-resv.h>
44 struct ttm_transfer_obj {
45 struct ttm_buffer_object base;
46 struct ttm_buffer_object *bo;
49 int ttm_mem_io_reserve(struct ttm_device *bdev,
50 struct ttm_resource *mem)
52 if (mem->bus.offset || mem->bus.addr)
55 mem->bus.is_iomem = false;
56 if (!bdev->funcs->io_mem_reserve)
59 return bdev->funcs->io_mem_reserve(bdev, mem);
62 void ttm_mem_io_free(struct ttm_device *bdev,
63 struct ttm_resource *mem)
65 if (!mem->bus.offset && !mem->bus.addr)
68 if (bdev->funcs->io_mem_free)
69 bdev->funcs->io_mem_free(bdev, mem);
75 static int ttm_resource_ioremap(struct ttm_device *bdev,
76 struct ttm_resource *mem,
83 ret = ttm_mem_io_reserve(bdev, mem);
84 if (ret || !mem->bus.is_iomem)
90 size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
92 if (mem->bus.caching == ttm_write_combined)
93 addr = ioremap_wc(mem->bus.offset, bus_size);
95 else if (mem->bus.caching == ttm_cached)
96 addr = ioremap_cache(mem->bus.offset, bus_size);
99 addr = ioremap(mem->bus.offset, bus_size);
101 ttm_mem_io_free(bdev, mem);
109 static void ttm_resource_iounmap(struct ttm_device *bdev,
110 struct ttm_resource *mem,
113 if (virtual && mem->bus.addr == NULL)
115 ttm_mem_io_free(bdev, mem);
118 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
121 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
123 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
126 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
127 iowrite32(ioread32(srcP++), dstP++);
131 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
135 struct page *d = ttm->pages[page];
141 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
142 dst = kmap_atomic_prot(d, prot);
146 memcpy_fromio(dst, src, PAGE_SIZE);
153 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
157 struct page *s = ttm->pages[page];
163 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
164 src = kmap_atomic_prot(s, prot);
168 memcpy_toio(dst, src, PAGE_SIZE);
175 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
176 struct ttm_operation_ctx *ctx,
177 struct ttm_resource *new_mem)
179 struct ttm_device *bdev = bo->bdev;
180 struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
181 struct ttm_tt *ttm = bo->ttm;
182 struct ttm_resource *old_mem = &bo->mem;
183 struct ttm_resource old_copy = *old_mem;
189 ret = ttm_bo_wait_ctx(bo, ctx);
193 ret = ttm_resource_ioremap(bdev, old_mem, &old_iomap);
196 ret = ttm_resource_ioremap(bdev, new_mem, &new_iomap);
201 * Single TTM move. NOP.
203 if (old_iomap == NULL && new_iomap == NULL)
207 * Don't move nonexistent data. Clear destination instead.
209 if (old_iomap == NULL &&
210 (ttm == NULL || (!ttm_tt_is_populated(ttm) &&
211 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
212 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
217 * TTM might be null for moves within the same region.
220 ret = ttm_tt_populate(bdev, ttm, ctx);
225 for (i = 0; i < new_mem->num_pages; ++i) {
226 if (old_iomap == NULL) {
227 pgprot_t prot = ttm_io_prot(bo, old_mem, PAGE_KERNEL);
228 ret = ttm_copy_ttm_io_page(ttm, new_iomap, i,
230 } else if (new_iomap == NULL) {
231 pgprot_t prot = ttm_io_prot(bo, new_mem, PAGE_KERNEL);
232 ret = ttm_copy_io_ttm_page(ttm, old_iomap, i,
235 ret = ttm_copy_io_page(new_iomap, old_iomap, i);
244 ttm_bo_assign_mem(bo, new_mem);
247 ttm_bo_tt_destroy(bo);
250 ttm_resource_iounmap(bdev, old_mem, new_iomap);
252 ttm_resource_iounmap(bdev, &old_copy, old_iomap);
255 * On error, keep the mm node!
258 ttm_resource_free(bo, &old_copy);
261 EXPORT_SYMBOL(ttm_bo_move_memcpy);
263 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
265 struct ttm_transfer_obj *fbo;
267 fbo = container_of(bo, struct ttm_transfer_obj, base);
273 * ttm_buffer_object_transfer
275 * @bo: A pointer to a struct ttm_buffer_object.
276 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
277 * holding the data of @bo with the old placement.
279 * This is a utility function that may be called after an accelerated move
280 * has been scheduled. A new buffer object is created as a placeholder for
281 * the old data while it's being copied. When that buffer object is idle,
282 * it can be destroyed, releasing the space of the old placement.
287 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
288 struct ttm_buffer_object **new_obj)
290 struct ttm_transfer_obj *fbo;
293 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
303 * Fix up members that we shouldn't copy directly:
304 * TODO: Explicit member copy would probably be better here.
307 atomic_inc(&ttm_glob.bo_count);
308 INIT_LIST_HEAD(&fbo->base.ddestroy);
309 INIT_LIST_HEAD(&fbo->base.lru);
310 fbo->base.moving = NULL;
311 drm_vma_node_reset(&fbo->base.base.vma_node);
313 kref_init(&fbo->base.kref);
314 fbo->base.destroy = &ttm_transfered_destroy;
315 fbo->base.pin_count = 0;
316 if (bo->type != ttm_bo_type_sg)
317 fbo->base.base.resv = &fbo->base.base._resv;
319 dma_resv_init(&fbo->base.base._resv);
320 fbo->base.base.dev = NULL;
321 ret = dma_resv_trylock(&fbo->base.base._resv);
324 ttm_bo_move_to_lru_tail_unlocked(&fbo->base);
326 *new_obj = &fbo->base;
330 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
333 struct ttm_resource_manager *man;
334 enum ttm_caching caching;
336 man = ttm_manager_type(bo->bdev, res->mem_type);
337 caching = man->use_tt ? bo->ttm->caching : res->bus.caching;
339 /* Cached mappings need no adjustment */
340 if (caching == ttm_cached)
343 #if defined(__i386__) || defined(__x86_64__)
344 if (caching == ttm_write_combined)
345 tmp = pgprot_writecombine(tmp);
346 else if (boot_cpu_data.x86 > 3)
347 tmp = pgprot_noncached(tmp);
349 #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
350 defined(__powerpc__) || defined(__mips__)
351 if (caching == ttm_write_combined)
352 tmp = pgprot_writecombine(tmp);
354 tmp = pgprot_noncached(tmp);
356 #if defined(__sparc__)
357 tmp = pgprot_noncached(tmp);
361 EXPORT_SYMBOL(ttm_io_prot);
363 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
364 unsigned long offset,
366 struct ttm_bo_kmap_obj *map)
368 struct ttm_resource *mem = &bo->mem;
370 if (bo->mem.bus.addr) {
371 map->bo_kmap_type = ttm_bo_map_premapped;
372 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
374 map->bo_kmap_type = ttm_bo_map_iomap;
375 if (mem->bus.caching == ttm_write_combined)
376 map->virtual = ioremap_wc(bo->mem.bus.offset + offset,
379 else if (mem->bus.caching == ttm_cached)
380 map->virtual = ioremap_cache(bo->mem.bus.offset + offset,
384 map->virtual = ioremap(bo->mem.bus.offset + offset,
387 return (!map->virtual) ? -ENOMEM : 0;
390 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
391 unsigned long start_page,
392 unsigned long num_pages,
393 struct ttm_bo_kmap_obj *map)
395 struct ttm_resource *mem = &bo->mem;
396 struct ttm_operation_ctx ctx = {
397 .interruptible = false,
400 struct ttm_tt *ttm = bo->ttm;
406 ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
410 if (num_pages == 1 && ttm->caching == ttm_cached) {
412 * We're mapping a single page, and the desired
413 * page protection is consistent with the bo.
416 map->bo_kmap_type = ttm_bo_map_kmap;
417 map->page = ttm->pages[start_page];
418 map->virtual = kmap(map->page);
421 * We need to use vmap to get the desired page protection
422 * or to make the buffer object look contiguous.
424 prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
425 map->bo_kmap_type = ttm_bo_map_vmap;
426 map->virtual = vmap(ttm->pages + start_page, num_pages,
429 return (!map->virtual) ? -ENOMEM : 0;
432 int ttm_bo_kmap(struct ttm_buffer_object *bo,
433 unsigned long start_page, unsigned long num_pages,
434 struct ttm_bo_kmap_obj *map)
436 unsigned long offset, size;
441 if (num_pages > bo->mem.num_pages)
443 if ((start_page + num_pages) > bo->mem.num_pages)
446 ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
449 if (!bo->mem.bus.is_iomem) {
450 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
452 offset = start_page << PAGE_SHIFT;
453 size = num_pages << PAGE_SHIFT;
454 return ttm_bo_ioremap(bo, offset, size, map);
457 EXPORT_SYMBOL(ttm_bo_kmap);
459 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
463 switch (map->bo_kmap_type) {
464 case ttm_bo_map_iomap:
465 iounmap(map->virtual);
467 case ttm_bo_map_vmap:
468 vunmap(map->virtual);
470 case ttm_bo_map_kmap:
473 case ttm_bo_map_premapped:
478 ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
482 EXPORT_SYMBOL(ttm_bo_kunmap);
484 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map)
486 struct ttm_resource *mem = &bo->mem;
489 ret = ttm_mem_io_reserve(bo->bdev, mem);
493 if (mem->bus.is_iomem) {
494 void __iomem *vaddr_iomem;
497 vaddr_iomem = (void __iomem *)mem->bus.addr;
498 else if (mem->bus.caching == ttm_write_combined)
499 vaddr_iomem = ioremap_wc(mem->bus.offset,
502 else if (mem->bus.caching == ttm_cached)
503 vaddr_iomem = ioremap_cache(mem->bus.offset,
507 vaddr_iomem = ioremap(mem->bus.offset, bo->base.size);
512 dma_buf_map_set_vaddr_iomem(map, vaddr_iomem);
515 struct ttm_operation_ctx ctx = {
516 .interruptible = false,
519 struct ttm_tt *ttm = bo->ttm;
523 ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
528 * We need to use vmap to get the desired page protection
529 * or to make the buffer object look contiguous.
531 prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
532 vaddr = vmap(ttm->pages, ttm->num_pages, 0, prot);
536 dma_buf_map_set_vaddr(map, vaddr);
541 EXPORT_SYMBOL(ttm_bo_vmap);
543 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map)
545 struct ttm_resource *mem = &bo->mem;
547 if (dma_buf_map_is_null(map))
552 else if (!mem->bus.addr)
553 iounmap(map->vaddr_iomem);
554 dma_buf_map_clear(map);
556 ttm_mem_io_free(bo->bdev, &bo->mem);
558 EXPORT_SYMBOL(ttm_bo_vunmap);
560 static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
564 ret = ttm_bo_wait(bo, false, false);
569 ttm_bo_tt_destroy(bo);
570 ttm_resource_free(bo, &bo->mem);
574 static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
575 struct dma_fence *fence,
578 struct ttm_buffer_object *ghost_obj;
582 * This should help pipeline ordinary buffer moves.
584 * Hang old buffer memory on a new buffer object,
585 * and leave it to be released when the GPU
586 * operation has completed.
589 dma_fence_put(bo->moving);
590 bo->moving = dma_fence_get(fence);
592 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
596 dma_resv_add_excl_fence(&ghost_obj->base._resv, fence);
599 * If we're not moving to fixed memory, the TTM object
600 * needs to stay alive. Otherwhise hang it on the ghost
601 * bo to be unbound and destroyed.
605 ghost_obj->ttm = NULL;
609 dma_resv_unlock(&ghost_obj->base._resv);
610 ttm_bo_put(ghost_obj);
614 static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
615 struct dma_fence *fence)
617 struct ttm_device *bdev = bo->bdev;
618 struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->mem.mem_type);
621 * BO doesn't have a TTM we need to bind/unbind. Just remember
622 * this eviction and free up the allocation
624 spin_lock(&from->move_lock);
625 if (!from->move || dma_fence_is_later(fence, from->move)) {
626 dma_fence_put(from->move);
627 from->move = dma_fence_get(fence);
629 spin_unlock(&from->move_lock);
631 ttm_resource_free(bo, &bo->mem);
633 dma_fence_put(bo->moving);
634 bo->moving = dma_fence_get(fence);
637 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
638 struct dma_fence *fence,
641 struct ttm_resource *new_mem)
643 struct ttm_device *bdev = bo->bdev;
644 struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->mem.mem_type);
645 struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
648 dma_resv_add_excl_fence(bo->base.resv, fence);
650 ret = ttm_bo_move_to_ghost(bo, fence, man->use_tt);
651 else if (!from->use_tt && pipeline)
652 ttm_bo_move_pipeline_evict(bo, fence);
654 ret = ttm_bo_wait_free_node(bo, man->use_tt);
659 ttm_bo_assign_mem(bo, new_mem);
663 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
665 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
667 struct ttm_buffer_object *ghost;
670 ret = ttm_buffer_object_transfer(bo, &ghost);
674 ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
675 /* Last resort, wait for the BO to be idle when we are OOM */
677 ttm_bo_wait(bo, false, false);
679 memset(&bo->mem, 0, sizeof(bo->mem));
680 bo->mem.mem_type = TTM_PL_SYSTEM;
683 dma_resv_unlock(&ghost->base._resv);