1 /**************************************************************************
3 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include <drm/ttm/ttm_bo_driver.h>
32 #include <drm/ttm/ttm_placement.h>
33 #include <drm/drm_vma_manager.h>
35 #include <linux/highmem.h>
36 #include <linux/wait.h>
37 #include <linux/slab.h>
38 #include <linux/vmalloc.h>
39 #include <linux/module.h>
40 #include <linux/reservation.h>
42 struct ttm_transfer_obj {
43 struct ttm_buffer_object base;
44 struct ttm_buffer_object *bo;
47 void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
49 ttm_bo_mem_put(bo, &bo->mem);
52 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
53 struct ttm_operation_ctx *ctx,
54 struct ttm_mem_reg *new_mem)
56 struct ttm_tt *ttm = bo->ttm;
57 struct ttm_mem_reg *old_mem = &bo->mem;
60 if (old_mem->mem_type != TTM_PL_SYSTEM) {
61 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
63 if (unlikely(ret != 0)) {
64 if (ret != -ERESTARTSYS)
65 pr_err("Failed to expire sync object before unbinding TTM\n");
70 ttm_bo_free_old_node(bo);
71 ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
73 old_mem->mem_type = TTM_PL_SYSTEM;
76 ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
77 if (unlikely(ret != 0))
80 if (new_mem->mem_type != TTM_PL_SYSTEM) {
81 ret = ttm_tt_bind(ttm, new_mem, ctx);
82 if (unlikely(ret != 0))
87 new_mem->mm_node = NULL;
91 EXPORT_SYMBOL(ttm_bo_move_ttm);
93 int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
95 if (likely(man->io_reserve_fastpath))
99 return mutex_lock_interruptible(&man->io_reserve_mutex);
101 mutex_lock(&man->io_reserve_mutex);
104 EXPORT_SYMBOL(ttm_mem_io_lock);
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);
113 EXPORT_SYMBOL(ttm_mem_io_unlock);
115 static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
117 struct ttm_buffer_object *bo;
119 if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
122 bo = list_first_entry(&man->io_reserve_lru,
123 struct ttm_buffer_object,
125 list_del_init(&bo->io_reserve_lru);
126 ttm_bo_unmap_virtual_locked(bo);
132 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
133 struct ttm_mem_reg *mem)
135 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
138 if (!bdev->driver->io_mem_reserve)
140 if (likely(man->io_reserve_fastpath))
141 return bdev->driver->io_mem_reserve(bdev, mem);
143 if (bdev->driver->io_mem_reserve &&
144 mem->bus.io_reserved_count++ == 0) {
146 ret = bdev->driver->io_mem_reserve(bdev, mem);
147 if (ret == -EAGAIN) {
148 ret = ttm_mem_io_evict(man);
155 EXPORT_SYMBOL(ttm_mem_io_reserve);
157 void ttm_mem_io_free(struct ttm_bo_device *bdev,
158 struct ttm_mem_reg *mem)
160 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
162 if (likely(man->io_reserve_fastpath))
165 if (bdev->driver->io_mem_reserve &&
166 --mem->bus.io_reserved_count == 0 &&
167 bdev->driver->io_mem_free)
168 bdev->driver->io_mem_free(bdev, mem);
171 EXPORT_SYMBOL(ttm_mem_io_free);
173 int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
175 struct ttm_mem_reg *mem = &bo->mem;
178 if (!mem->bus.io_reserved_vm) {
179 struct ttm_mem_type_manager *man =
180 &bo->bdev->man[mem->mem_type];
182 ret = ttm_mem_io_reserve(bo->bdev, mem);
183 if (unlikely(ret != 0))
185 mem->bus.io_reserved_vm = true;
186 if (man->use_io_reserve_lru)
187 list_add_tail(&bo->io_reserve_lru,
188 &man->io_reserve_lru);
193 void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
195 struct ttm_mem_reg *mem = &bo->mem;
197 if (mem->bus.io_reserved_vm) {
198 mem->bus.io_reserved_vm = false;
199 list_del_init(&bo->io_reserve_lru);
200 ttm_mem_io_free(bo->bdev, mem);
204 static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
207 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
212 (void) ttm_mem_io_lock(man, false);
213 ret = ttm_mem_io_reserve(bdev, mem);
214 ttm_mem_io_unlock(man);
215 if (ret || !mem->bus.is_iomem)
219 addr = mem->bus.addr;
221 if (mem->placement & TTM_PL_FLAG_WC)
222 addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size);
224 addr = ioremap_nocache(mem->bus.base + mem->bus.offset, mem->bus.size);
226 (void) ttm_mem_io_lock(man, false);
227 ttm_mem_io_free(bdev, mem);
228 ttm_mem_io_unlock(man);
236 static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
239 struct ttm_mem_type_manager *man;
241 man = &bdev->man[mem->mem_type];
243 if (virtual && mem->bus.addr == NULL)
245 (void) ttm_mem_io_lock(man, false);
246 ttm_mem_io_free(bdev, mem);
247 ttm_mem_io_unlock(man);
250 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
253 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
255 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
258 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
259 iowrite32(ioread32(srcP++), dstP++);
264 #define __ttm_kmap_atomic_prot(__page, __prot) kmap_atomic_prot(__page, __prot)
265 #define __ttm_kunmap_atomic(__addr) kunmap_atomic(__addr)
267 #define __ttm_kmap_atomic_prot(__page, __prot) vmap(&__page, 1, 0, __prot)
268 #define __ttm_kunmap_atomic(__addr) vunmap(__addr)
273 * ttm_kmap_atomic_prot - Efficient kernel map of a single page with
274 * specified page protection.
276 * @page: The page to map.
277 * @prot: The page protection.
279 * This function maps a TTM page using the kmap_atomic api if available,
280 * otherwise falls back to vmap. The user must make sure that the
281 * specified page does not have an aliased mapping with a different caching
282 * policy unless the architecture explicitly allows it. Also mapping and
283 * unmapping using this api must be correctly nested. Unmapping should
284 * occur in the reverse order of mapping.
286 void *ttm_kmap_atomic_prot(struct page *page, pgprot_t prot)
288 if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL))
289 return kmap_atomic(page);
291 return __ttm_kmap_atomic_prot(page, prot);
293 EXPORT_SYMBOL(ttm_kmap_atomic_prot);
296 * ttm_kunmap_atomic_prot - Unmap a page that was mapped using
297 * ttm_kmap_atomic_prot.
299 * @addr: The virtual address from the map.
300 * @prot: The page protection.
302 void ttm_kunmap_atomic_prot(void *addr, pgprot_t prot)
304 if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL))
307 __ttm_kunmap_atomic(addr);
309 EXPORT_SYMBOL(ttm_kunmap_atomic_prot);
311 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
315 struct page *d = ttm->pages[page];
321 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
322 dst = ttm_kmap_atomic_prot(d, prot);
326 memcpy_fromio(dst, src, PAGE_SIZE);
328 ttm_kunmap_atomic_prot(dst, prot);
333 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
337 struct page *s = ttm->pages[page];
343 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
344 src = ttm_kmap_atomic_prot(s, prot);
348 memcpy_toio(dst, src, PAGE_SIZE);
350 ttm_kunmap_atomic_prot(src, prot);
355 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
356 struct ttm_operation_ctx *ctx,
357 struct ttm_mem_reg *new_mem)
359 struct ttm_bo_device *bdev = bo->bdev;
360 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
361 struct ttm_tt *ttm = bo->ttm;
362 struct ttm_mem_reg *old_mem = &bo->mem;
363 struct ttm_mem_reg old_copy = *old_mem;
369 unsigned long add = 0;
372 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
376 ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
379 ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
384 * Single TTM move. NOP.
386 if (old_iomap == NULL && new_iomap == NULL)
390 * Don't move nonexistent data. Clear destination instead.
392 if (old_iomap == NULL &&
393 (ttm == NULL || (ttm->state == tt_unpopulated &&
394 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
395 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
400 * TTM might be null for moves within the same region.
403 ret = ttm_tt_populate(ttm, ctx);
411 if ((old_mem->mem_type == new_mem->mem_type) &&
412 (new_mem->start < old_mem->start + old_mem->size)) {
414 add = new_mem->num_pages - 1;
417 for (i = 0; i < new_mem->num_pages; ++i) {
418 page = i * dir + add;
419 if (old_iomap == NULL) {
420 pgprot_t prot = ttm_io_prot(old_mem->placement,
422 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
424 } else if (new_iomap == NULL) {
425 pgprot_t prot = ttm_io_prot(new_mem->placement,
427 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
430 ret = ttm_copy_io_page(new_iomap, old_iomap, page);
439 new_mem->mm_node = NULL;
441 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
447 ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
449 ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
452 * On error, keep the mm node!
455 ttm_bo_mem_put(bo, &old_copy);
458 EXPORT_SYMBOL(ttm_bo_move_memcpy);
460 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
462 struct ttm_transfer_obj *fbo;
464 fbo = container_of(bo, struct ttm_transfer_obj, base);
465 ttm_bo_unref(&fbo->bo);
470 * ttm_buffer_object_transfer
472 * @bo: A pointer to a struct ttm_buffer_object.
473 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
474 * holding the data of @bo with the old placement.
476 * This is a utility function that may be called after an accelerated move
477 * has been scheduled. A new buffer object is created as a placeholder for
478 * the old data while it's being copied. When that buffer object is idle,
479 * it can be destroyed, releasing the space of the old placement.
484 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
485 struct ttm_buffer_object **new_obj)
487 struct ttm_transfer_obj *fbo;
490 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
495 fbo->bo = ttm_bo_reference(bo);
498 * Fix up members that we shouldn't copy directly:
499 * TODO: Explicit member copy would probably be better here.
502 atomic_inc(&bo->bdev->glob->bo_count);
503 INIT_LIST_HEAD(&fbo->base.ddestroy);
504 INIT_LIST_HEAD(&fbo->base.lru);
505 INIT_LIST_HEAD(&fbo->base.swap);
506 INIT_LIST_HEAD(&fbo->base.io_reserve_lru);
507 mutex_init(&fbo->base.wu_mutex);
508 fbo->base.moving = NULL;
509 drm_vma_node_reset(&fbo->base.vma_node);
510 atomic_set(&fbo->base.cpu_writers, 0);
512 kref_init(&fbo->base.list_kref);
513 kref_init(&fbo->base.kref);
514 fbo->base.destroy = &ttm_transfered_destroy;
515 fbo->base.acc_size = 0;
516 fbo->base.resv = &fbo->base.ttm_resv;
517 reservation_object_init(fbo->base.resv);
518 ret = reservation_object_trylock(fbo->base.resv);
521 *new_obj = &fbo->base;
525 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
527 /* Cached mappings need no adjustment */
528 if (caching_flags & TTM_PL_FLAG_CACHED)
531 #if defined(__i386__) || defined(__x86_64__)
532 if (caching_flags & TTM_PL_FLAG_WC)
533 tmp = pgprot_writecombine(tmp);
534 else if (boot_cpu_data.x86 > 3)
535 tmp = pgprot_noncached(tmp);
537 #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
539 if (caching_flags & TTM_PL_FLAG_WC)
540 tmp = pgprot_writecombine(tmp);
542 tmp = pgprot_noncached(tmp);
544 #if defined(__sparc__) || defined(__mips__)
545 tmp = pgprot_noncached(tmp);
549 EXPORT_SYMBOL(ttm_io_prot);
551 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
552 unsigned long offset,
554 struct ttm_bo_kmap_obj *map)
556 struct ttm_mem_reg *mem = &bo->mem;
558 if (bo->mem.bus.addr) {
559 map->bo_kmap_type = ttm_bo_map_premapped;
560 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
562 map->bo_kmap_type = ttm_bo_map_iomap;
563 if (mem->placement & TTM_PL_FLAG_WC)
564 map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
567 map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
570 return (!map->virtual) ? -ENOMEM : 0;
573 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
574 unsigned long start_page,
575 unsigned long num_pages,
576 struct ttm_bo_kmap_obj *map)
578 struct ttm_mem_reg *mem = &bo->mem;
579 struct ttm_operation_ctx ctx = {
580 .interruptible = false,
583 struct ttm_tt *ttm = bo->ttm;
589 ret = ttm_tt_populate(ttm, &ctx);
593 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
595 * We're mapping a single page, and the desired
596 * page protection is consistent with the bo.
599 map->bo_kmap_type = ttm_bo_map_kmap;
600 map->page = ttm->pages[start_page];
601 map->virtual = kmap(map->page);
604 * We need to use vmap to get the desired page protection
605 * or to make the buffer object look contiguous.
607 prot = ttm_io_prot(mem->placement, PAGE_KERNEL);
608 map->bo_kmap_type = ttm_bo_map_vmap;
609 map->virtual = vmap(ttm->pages + start_page, num_pages,
612 return (!map->virtual) ? -ENOMEM : 0;
615 int ttm_bo_kmap(struct ttm_buffer_object *bo,
616 unsigned long start_page, unsigned long num_pages,
617 struct ttm_bo_kmap_obj *map)
619 struct ttm_mem_type_manager *man =
620 &bo->bdev->man[bo->mem.mem_type];
621 unsigned long offset, size;
626 if (num_pages > bo->num_pages)
628 if (start_page > bo->num_pages)
631 if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
634 (void) ttm_mem_io_lock(man, false);
635 ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
636 ttm_mem_io_unlock(man);
639 if (!bo->mem.bus.is_iomem) {
640 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
642 offset = start_page << PAGE_SHIFT;
643 size = num_pages << PAGE_SHIFT;
644 return ttm_bo_ioremap(bo, offset, size, map);
647 EXPORT_SYMBOL(ttm_bo_kmap);
649 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
651 struct ttm_buffer_object *bo = map->bo;
652 struct ttm_mem_type_manager *man =
653 &bo->bdev->man[bo->mem.mem_type];
657 switch (map->bo_kmap_type) {
658 case ttm_bo_map_iomap:
659 iounmap(map->virtual);
661 case ttm_bo_map_vmap:
662 vunmap(map->virtual);
664 case ttm_bo_map_kmap:
667 case ttm_bo_map_premapped:
672 (void) ttm_mem_io_lock(man, false);
673 ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
674 ttm_mem_io_unlock(man);
678 EXPORT_SYMBOL(ttm_bo_kunmap);
680 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
681 struct dma_fence *fence,
683 struct ttm_mem_reg *new_mem)
685 struct ttm_bo_device *bdev = bo->bdev;
686 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
687 struct ttm_mem_reg *old_mem = &bo->mem;
689 struct ttm_buffer_object *ghost_obj;
691 reservation_object_add_excl_fence(bo->resv, fence);
693 ret = ttm_bo_wait(bo, false, false);
697 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
698 ttm_tt_destroy(bo->ttm);
701 ttm_bo_free_old_node(bo);
704 * This should help pipeline ordinary buffer moves.
706 * Hang old buffer memory on a new buffer object,
707 * and leave it to be released when the GPU
708 * operation has completed.
711 dma_fence_put(bo->moving);
712 bo->moving = dma_fence_get(fence);
714 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
718 reservation_object_add_excl_fence(ghost_obj->resv, fence);
721 * If we're not moving to fixed memory, the TTM object
722 * needs to stay alive. Otherwhise hang it on the ghost
723 * bo to be unbound and destroyed.
726 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
727 ghost_obj->ttm = NULL;
731 ttm_bo_unreserve(ghost_obj);
732 ttm_bo_unref(&ghost_obj);
736 new_mem->mm_node = NULL;
740 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
742 int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
743 struct dma_fence *fence, bool evict,
744 struct ttm_mem_reg *new_mem)
746 struct ttm_bo_device *bdev = bo->bdev;
747 struct ttm_mem_reg *old_mem = &bo->mem;
749 struct ttm_mem_type_manager *from = &bdev->man[old_mem->mem_type];
750 struct ttm_mem_type_manager *to = &bdev->man[new_mem->mem_type];
754 reservation_object_add_excl_fence(bo->resv, fence);
757 struct ttm_buffer_object *ghost_obj;
760 * This should help pipeline ordinary buffer moves.
762 * Hang old buffer memory on a new buffer object,
763 * and leave it to be released when the GPU
764 * operation has completed.
767 dma_fence_put(bo->moving);
768 bo->moving = dma_fence_get(fence);
770 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
774 reservation_object_add_excl_fence(ghost_obj->resv, fence);
777 * If we're not moving to fixed memory, the TTM object
778 * needs to stay alive. Otherwhise hang it on the ghost
779 * bo to be unbound and destroyed.
782 if (!(to->flags & TTM_MEMTYPE_FLAG_FIXED))
783 ghost_obj->ttm = NULL;
787 ttm_bo_unreserve(ghost_obj);
788 ttm_bo_unref(&ghost_obj);
790 } else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) {
793 * BO doesn't have a TTM we need to bind/unbind. Just remember
794 * this eviction and free up the allocation
797 spin_lock(&from->move_lock);
798 if (!from->move || dma_fence_is_later(fence, from->move)) {
799 dma_fence_put(from->move);
800 from->move = dma_fence_get(fence);
802 spin_unlock(&from->move_lock);
804 ttm_bo_free_old_node(bo);
806 dma_fence_put(bo->moving);
807 bo->moving = dma_fence_get(fence);
811 * Last resort, wait for the move to be completed.
813 * Should never happen in pratice.
816 ret = ttm_bo_wait(bo, false, false);
820 if (to->flags & TTM_MEMTYPE_FLAG_FIXED) {
821 ttm_tt_destroy(bo->ttm);
824 ttm_bo_free_old_node(bo);
828 new_mem->mm_node = NULL;
832 EXPORT_SYMBOL(ttm_bo_pipeline_move);
834 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
836 struct ttm_buffer_object *ghost;
839 ret = ttm_buffer_object_transfer(bo, &ghost);
843 ret = reservation_object_copy_fences(ghost->resv, bo->resv);
844 /* Last resort, wait for the BO to be idle when we are OOM */
846 ttm_bo_wait(bo, false, false);
848 memset(&bo->mem, 0, sizeof(bo->mem));
849 bo->mem.mem_type = TTM_PL_SYSTEM;
852 ttm_bo_unreserve(ghost);
853 ttm_bo_unref(&ghost);