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>
41 void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
43 ttm_bo_mem_put(bo, &bo->mem);
46 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
48 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
50 struct ttm_tt *ttm = bo->ttm;
51 struct ttm_mem_reg *old_mem = &bo->mem;
54 if (old_mem->mem_type != TTM_PL_SYSTEM) {
56 ttm_bo_free_old_node(bo);
57 ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
59 old_mem->mem_type = TTM_PL_SYSTEM;
62 ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
63 if (unlikely(ret != 0))
66 if (new_mem->mem_type != TTM_PL_SYSTEM) {
67 ret = ttm_tt_bind(ttm, new_mem);
68 if (unlikely(ret != 0))
73 new_mem->mm_node = NULL;
77 EXPORT_SYMBOL(ttm_bo_move_ttm);
79 int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
81 if (likely(man->io_reserve_fastpath))
85 if (lockmgr(&man->io_reserve_mutex,
86 LK_EXCLUSIVE | LK_SLEEPFAIL))
92 lockmgr(&man->io_reserve_mutex, LK_EXCLUSIVE);
95 EXPORT_SYMBOL(ttm_mem_io_lock);
97 void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
99 if (likely(man->io_reserve_fastpath))
102 lockmgr(&man->io_reserve_mutex, LK_RELEASE);
104 EXPORT_SYMBOL(ttm_mem_io_unlock);
106 static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
108 struct ttm_buffer_object *bo;
110 if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
113 bo = list_first_entry(&man->io_reserve_lru,
114 struct ttm_buffer_object,
116 list_del_init(&bo->io_reserve_lru);
117 ttm_bo_unmap_virtual_locked(bo);
123 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
124 struct ttm_mem_reg *mem)
126 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
129 if (!bdev->driver->io_mem_reserve)
131 if (likely(man->io_reserve_fastpath))
132 return bdev->driver->io_mem_reserve(bdev, mem);
134 if (bdev->driver->io_mem_reserve &&
135 mem->bus.io_reserved_count++ == 0) {
137 ret = bdev->driver->io_mem_reserve(bdev, mem);
138 if (ret == -EAGAIN) {
139 ret = ttm_mem_io_evict(man);
146 EXPORT_SYMBOL(ttm_mem_io_reserve);
148 void ttm_mem_io_free(struct ttm_bo_device *bdev,
149 struct ttm_mem_reg *mem)
151 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
153 if (likely(man->io_reserve_fastpath))
156 if (bdev->driver->io_mem_reserve &&
157 --mem->bus.io_reserved_count == 0 &&
158 bdev->driver->io_mem_free)
159 bdev->driver->io_mem_free(bdev, mem);
162 EXPORT_SYMBOL(ttm_mem_io_free);
164 int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
166 struct ttm_mem_reg *mem = &bo->mem;
169 if (!mem->bus.io_reserved_vm) {
170 struct ttm_mem_type_manager *man =
171 &bo->bdev->man[mem->mem_type];
173 ret = ttm_mem_io_reserve(bo->bdev, mem);
174 if (unlikely(ret != 0))
176 mem->bus.io_reserved_vm = true;
177 if (man->use_io_reserve_lru)
178 list_add_tail(&bo->io_reserve_lru,
179 &man->io_reserve_lru);
184 void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
186 struct ttm_mem_reg *mem = &bo->mem;
188 if (mem->bus.io_reserved_vm) {
189 mem->bus.io_reserved_vm = false;
190 list_del_init(&bo->io_reserve_lru);
191 ttm_mem_io_free(bo->bdev, mem);
195 static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
198 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
203 (void) ttm_mem_io_lock(man, false);
204 ret = ttm_mem_io_reserve(bdev, mem);
205 ttm_mem_io_unlock(man);
206 if (ret || !mem->bus.is_iomem)
210 addr = mem->bus.addr;
212 addr = pmap_mapdev_attr(mem->bus.base + mem->bus.offset,
213 mem->bus.size, (mem->placement & TTM_PL_FLAG_WC) ?
214 VM_MEMATTR_WRITE_COMBINING : VM_MEMATTR_UNCACHEABLE);
216 (void) ttm_mem_io_lock(man, false);
217 ttm_mem_io_free(bdev, mem);
218 ttm_mem_io_unlock(man);
226 static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
229 struct ttm_mem_type_manager *man;
231 man = &bdev->man[mem->mem_type];
233 if (virtual && mem->bus.addr == NULL)
234 pmap_unmapdev((vm_offset_t)virtual, mem->bus.size);
235 (void) ttm_mem_io_lock(man, false);
236 ttm_mem_io_free(bdev, mem);
237 ttm_mem_io_unlock(man);
240 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
243 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
245 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
248 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
249 /* iowrite32(ioread32(srcP++), dstP++); */
254 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
258 struct page *d = ttm->pages[page];
264 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
267 dst = kmap_atomic_prot(d, prot);
269 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
270 dst = vmap(&d, 1, 0, prot);
277 memcpy_fromio(dst, src, PAGE_SIZE);
282 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
291 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
295 struct page *s = ttm->pages[page];
301 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
303 src = kmap_atomic_prot(s, prot);
305 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
306 src = vmap(&s, 1, 0, prot);
313 memcpy_toio(dst, src, PAGE_SIZE);
318 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
327 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
328 bool evict, bool no_wait_gpu,
329 struct ttm_mem_reg *new_mem)
331 struct ttm_bo_device *bdev = bo->bdev;
332 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
333 struct ttm_tt *ttm = bo->ttm;
334 struct ttm_mem_reg *old_mem = &bo->mem;
335 struct ttm_mem_reg old_copy = *old_mem;
341 unsigned long add = 0;
344 ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
347 ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
352 * Single TTM move. NOP.
354 if (old_iomap == NULL && new_iomap == NULL)
358 * Don't move nonexistent data. Clear destination instead.
360 if (old_iomap == NULL &&
361 (ttm == NULL || (ttm->state == tt_unpopulated &&
362 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
363 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
368 * TTM might be null for moves within the same region.
370 if (ttm && ttm->state == tt_unpopulated) {
371 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
379 if ((old_mem->mem_type == new_mem->mem_type) &&
380 (new_mem->start < old_mem->start + old_mem->size)) {
382 add = new_mem->num_pages - 1;
385 for (i = 0; i < new_mem->num_pages; ++i) {
386 page = i * dir + add;
387 if (old_iomap == NULL) {
388 pgprot_t prot = ttm_io_prot(old_mem->placement,
390 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
392 } else if (new_iomap == NULL) {
393 pgprot_t prot = ttm_io_prot(new_mem->placement,
395 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
398 ret = ttm_copy_io_page(new_iomap, old_iomap, page);
400 /* failing here, means keep old copy as-is */
401 old_copy.mm_node = NULL;
409 new_mem->mm_node = NULL;
411 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) {
418 ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
420 ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
423 * On error, keep the mm node!
426 ttm_bo_mem_put(bo, &old_copy);
429 EXPORT_SYMBOL(ttm_bo_move_memcpy);
431 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
437 * ttm_buffer_object_transfer
439 * @bo: A pointer to a struct ttm_buffer_object.
440 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
441 * holding the data of @bo with the old placement.
443 * This is a utility function that may be called after an accelerated move
444 * has been scheduled. A new buffer object is created as a placeholder for
445 * the old data while it's being copied. When that buffer object is idle,
446 * it can be destroyed, releasing the space of the old placement.
451 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
452 struct ttm_buffer_object **new_obj)
454 struct ttm_buffer_object *fbo;
455 struct ttm_bo_device *bdev = bo->bdev;
456 struct ttm_bo_driver *driver = bdev->driver;
459 fbo = kmalloc(sizeof(*fbo), M_DRM, M_WAITOK);
466 * Fix up members that we shouldn't copy directly:
467 * TODO: Explicit member copy would probably be better here.
470 INIT_LIST_HEAD(&fbo->ddestroy);
471 INIT_LIST_HEAD(&fbo->lru);
472 INIT_LIST_HEAD(&fbo->swap);
473 INIT_LIST_HEAD(&fbo->io_reserve_lru);
474 drm_vma_node_reset(&fbo->vma_node);
475 atomic_set(&fbo->cpu_writers, 0);
477 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
479 fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
481 fbo->sync_obj = NULL;
482 lockmgr(&bdev->fence_lock, LK_RELEASE);
483 kref_init(&fbo->list_kref);
484 kref_init(&fbo->kref);
485 fbo->destroy = &ttm_transfered_destroy;
487 fbo->resv = &fbo->ttm_resv;
488 reservation_object_init(fbo->resv);
489 ret = ww_mutex_trylock(&fbo->resv->lock);
493 * Mirror ref from kref_init() for list_kref.
495 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &fbo->priv_flags);
501 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
503 #if defined(__i386__) || defined(__x86_64__)
504 if (caching_flags & TTM_PL_FLAG_WC)
505 tmp = pgprot_writecombine(tmp);
507 tmp = pgprot_noncached(tmp);
509 #elif defined(__powerpc__)
510 if (!(caching_flags & TTM_PL_FLAG_CACHED)) {
511 pgprot_val(tmp) |= _PAGE_NO_CACHE;
512 if (caching_flags & TTM_PL_FLAG_UNCACHED)
513 pgprot_val(tmp) |= _PAGE_GUARDED;
516 #if defined(__ia64__)
517 if (caching_flags & TTM_PL_FLAG_WC)
518 tmp = pgprot_writecombine(tmp);
520 tmp = pgprot_noncached(tmp);
522 #if defined(__sparc__) || defined(__mips__)
523 if (!(caching_flags & TTM_PL_FLAG_CACHED))
524 tmp = pgprot_noncached(tmp);
528 EXPORT_SYMBOL(ttm_io_prot);
530 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
531 unsigned long offset,
533 struct ttm_bo_kmap_obj *map)
535 struct ttm_mem_reg *mem = &bo->mem;
537 if (bo->mem.bus.addr) {
538 map->bo_kmap_type = ttm_bo_map_premapped;
539 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
541 map->bo_kmap_type = ttm_bo_map_iomap;
542 if (mem->placement & TTM_PL_FLAG_WC)
543 map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
546 map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
549 return (!map->virtual) ? -ENOMEM : 0;
552 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
553 unsigned long start_page,
554 unsigned long num_pages,
555 struct ttm_bo_kmap_obj *map)
557 struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
558 struct ttm_tt *ttm = bo->ttm;
563 if (ttm->state == tt_unpopulated) {
564 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
569 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
571 * We're mapping a single page, and the desired
572 * page protection is consistent with the bo.
575 map->bo_kmap_type = ttm_bo_map_kmap;
576 map->page = ttm->pages[start_page];
577 map->virtual = kmap(map->page);
580 * We need to use vmap to get the desired page protection
581 * or to make the buffer object look contiguous.
583 prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
585 ttm_io_prot(mem->placement, PAGE_KERNEL);
586 map->bo_kmap_type = ttm_bo_map_vmap;
587 map->virtual = vmap(ttm->pages + start_page, num_pages,
590 return (!map->virtual) ? -ENOMEM : 0;
593 int ttm_bo_kmap(struct ttm_buffer_object *bo,
594 unsigned long start_page, unsigned long num_pages,
595 struct ttm_bo_kmap_obj *map)
597 struct ttm_mem_type_manager *man =
598 &bo->bdev->man[bo->mem.mem_type];
599 unsigned long offset, size;
602 BUG_ON(!list_empty(&bo->swap));
605 if (num_pages > bo->num_pages)
607 if (start_page > bo->num_pages)
610 if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
613 (void) ttm_mem_io_lock(man, false);
614 ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
615 ttm_mem_io_unlock(man);
618 if (!bo->mem.bus.is_iomem) {
619 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
621 offset = start_page << PAGE_SHIFT;
622 size = num_pages << PAGE_SHIFT;
623 return ttm_bo_ioremap(bo, offset, size, map);
626 EXPORT_SYMBOL(ttm_bo_kmap);
628 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
630 struct ttm_buffer_object *bo = map->bo;
631 struct ttm_mem_type_manager *man =
632 &bo->bdev->man[bo->mem.mem_type];
636 switch (map->bo_kmap_type) {
637 case ttm_bo_map_iomap:
638 iounmap(map->virtual);
640 case ttm_bo_map_vmap:
641 vunmap(map->virtual);
643 case ttm_bo_map_kmap:
646 case ttm_bo_map_premapped:
651 (void) ttm_mem_io_lock(man, false);
652 ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
653 ttm_mem_io_unlock(man);
657 EXPORT_SYMBOL(ttm_bo_kunmap);
659 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
663 struct ttm_mem_reg *new_mem)
665 struct ttm_bo_device *bdev = bo->bdev;
666 struct ttm_bo_driver *driver = bdev->driver;
667 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
668 struct ttm_mem_reg *old_mem = &bo->mem;
670 struct ttm_buffer_object *ghost_obj;
671 void *tmp_obj = NULL;
673 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
675 tmp_obj = bo->sync_obj;
678 bo->sync_obj = driver->sync_obj_ref(sync_obj);
680 ret = ttm_bo_wait(bo, false, false, false);
681 lockmgr(&bdev->fence_lock, LK_RELEASE);
683 driver->sync_obj_unref(&tmp_obj);
687 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
689 ttm_tt_unbind(bo->ttm);
690 ttm_tt_destroy(bo->ttm);
693 ttm_bo_free_old_node(bo);
696 * This should help pipeline ordinary buffer moves.
698 * Hang old buffer memory on a new buffer object,
699 * and leave it to be released when the GPU
700 * operation has completed.
703 set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
704 lockmgr(&bdev->fence_lock, LK_RELEASE);
706 driver->sync_obj_unref(&tmp_obj);
708 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
713 * If we're not moving to fixed memory, the TTM object
714 * needs to stay alive. Otherwhise hang it on the ghost
715 * bo to be unbound and destroyed.
718 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
719 ghost_obj->ttm = NULL;
723 ttm_bo_unreserve(ghost_obj);
724 ttm_bo_unref(&ghost_obj);
728 new_mem->mm_node = NULL;
732 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);