Home | History | Annotate | Line # | Download | only in ttm
ttm_bo_util.c revision 1.4.6.1
      1 /**************************************************************************
      2  *
      3  * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
      4  * All Rights Reserved.
      5  *
      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:
     13  *
     14  * The above copyright notice and this permission notice (including the
     15  * next paragraph) shall be included in all copies or substantial portions
     16  * of the Software.
     17  *
     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.
     25  *
     26  **************************************************************************/
     27 /*
     28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
     29  */
     30 
     31 #include <drm/ttm/ttm_bo_driver.h>
     32 #include <drm/ttm/ttm_placement.h>
     33 #include <drm/drm_vma_manager.h>
     34 #include <linux/io.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/export.h>
     41 
     42 #ifdef __NetBSD__		/* PMAP_* caching flags for ttm_io_prot */
     43 #include <uvm/uvm_pmap.h>
     44 #endif
     45 
     46 void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
     47 {
     48 	ttm_bo_mem_put(bo, &bo->mem);
     49 }
     50 
     51 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
     52 		    bool evict,
     53 		    bool no_wait_gpu, struct ttm_mem_reg *new_mem)
     54 {
     55 	struct ttm_tt *ttm = bo->ttm;
     56 	struct ttm_mem_reg *old_mem = &bo->mem;
     57 	int ret;
     58 
     59 	if (old_mem->mem_type != TTM_PL_SYSTEM) {
     60 		ttm_tt_unbind(ttm);
     61 		ttm_bo_free_old_node(bo);
     62 		ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
     63 				TTM_PL_MASK_MEM);
     64 		old_mem->mem_type = TTM_PL_SYSTEM;
     65 	}
     66 
     67 	ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
     68 	if (unlikely(ret != 0))
     69 		return ret;
     70 
     71 	if (new_mem->mem_type != TTM_PL_SYSTEM) {
     72 		ret = ttm_tt_bind(ttm, new_mem);
     73 		if (unlikely(ret != 0))
     74 			return ret;
     75 	}
     76 
     77 	*old_mem = *new_mem;
     78 	new_mem->mm_node = NULL;
     79 
     80 	return 0;
     81 }
     82 EXPORT_SYMBOL(ttm_bo_move_ttm);
     83 
     84 int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
     85 {
     86 	if (likely(man->io_reserve_fastpath))
     87 		return 0;
     88 
     89 	if (interruptible)
     90 		return mutex_lock_interruptible(&man->io_reserve_mutex);
     91 
     92 	mutex_lock(&man->io_reserve_mutex);
     93 	return 0;
     94 }
     95 EXPORT_SYMBOL(ttm_mem_io_lock);
     96 
     97 void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
     98 {
     99 	if (likely(man->io_reserve_fastpath))
    100 		return;
    101 
    102 	mutex_unlock(&man->io_reserve_mutex);
    103 }
    104 EXPORT_SYMBOL(ttm_mem_io_unlock);
    105 
    106 static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
    107 {
    108 	struct ttm_buffer_object *bo;
    109 
    110 	if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
    111 		return -EAGAIN;
    112 
    113 	bo = list_first_entry(&man->io_reserve_lru,
    114 			      struct ttm_buffer_object,
    115 			      io_reserve_lru);
    116 	list_del_init(&bo->io_reserve_lru);
    117 	ttm_bo_unmap_virtual_locked(bo);
    118 
    119 	return 0;
    120 }
    121 
    122 
    123 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
    124 		       struct ttm_mem_reg *mem)
    125 {
    126 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
    127 	int ret = 0;
    128 
    129 	if (!bdev->driver->io_mem_reserve)
    130 		return 0;
    131 	if (likely(man->io_reserve_fastpath))
    132 		return bdev->driver->io_mem_reserve(bdev, mem);
    133 
    134 	if (bdev->driver->io_mem_reserve &&
    135 	    mem->bus.io_reserved_count++ == 0) {
    136 retry:
    137 		ret = bdev->driver->io_mem_reserve(bdev, mem);
    138 		if (ret == -EAGAIN) {
    139 			ret = ttm_mem_io_evict(man);
    140 			if (ret == 0)
    141 				goto retry;
    142 		}
    143 	}
    144 	return ret;
    145 }
    146 EXPORT_SYMBOL(ttm_mem_io_reserve);
    147 
    148 void ttm_mem_io_free(struct ttm_bo_device *bdev,
    149 		     struct ttm_mem_reg *mem)
    150 {
    151 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
    152 
    153 	if (likely(man->io_reserve_fastpath))
    154 		return;
    155 
    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);
    160 
    161 }
    162 EXPORT_SYMBOL(ttm_mem_io_free);
    163 
    164 int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
    165 {
    166 	struct ttm_mem_reg *mem = &bo->mem;
    167 	int ret;
    168 
    169 	if (!mem->bus.io_reserved_vm) {
    170 		struct ttm_mem_type_manager *man =
    171 			&bo->bdev->man[mem->mem_type];
    172 
    173 		ret = ttm_mem_io_reserve(bo->bdev, mem);
    174 		if (unlikely(ret != 0))
    175 			return ret;
    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);
    180 	}
    181 	return 0;
    182 }
    183 
    184 void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
    185 {
    186 	struct ttm_mem_reg *mem = &bo->mem;
    187 
    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);
    192 	}
    193 }
    194 
    195 static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
    196 			void **virtual)
    197 {
    198 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
    199 	int ret;
    200 	void *addr;
    201 
    202 	*virtual = NULL;
    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)
    207 		return ret;
    208 
    209 	if (mem->bus.addr) {
    210 		addr = mem->bus.addr;
    211 	} else {
    212 #ifdef __NetBSD__
    213 		const bus_addr_t bus_addr = (mem->bus.base + mem->bus.offset);
    214 		int flags = BUS_SPACE_MAP_LINEAR;
    215 
    216 		if (ISSET(mem->placement, TTM_PL_FLAG_WC))
    217 			flags |= BUS_SPACE_MAP_PREFETCHABLE;
    218 		/* XXX errno NetBSD->Linux */
    219 		ret = -bus_space_map(bdev->memt, bus_addr, mem->bus.size,
    220 		    flags, &mem->bus.memh);
    221 		if (ret) {
    222 			(void) ttm_mem_io_lock(man, false);
    223 			ttm_mem_io_free(bdev, mem);
    224 			ttm_mem_io_unlock(man);
    225 			return ret;
    226 		}
    227 		addr = bus_space_vaddr(bdev->memt, mem->bus.memh);
    228 #else
    229 		if (mem->placement & TTM_PL_FLAG_WC)
    230 			addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size);
    231 		else
    232 			addr = ioremap_nocache(mem->bus.base + mem->bus.offset, mem->bus.size);
    233 		if (!addr) {
    234 			(void) ttm_mem_io_lock(man, false);
    235 			ttm_mem_io_free(bdev, mem);
    236 			ttm_mem_io_unlock(man);
    237 			return -ENOMEM;
    238 		}
    239 #endif
    240 	}
    241 	*virtual = addr;
    242 	return 0;
    243 }
    244 
    245 static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
    246 			 void *virtual)
    247 {
    248 	struct ttm_mem_type_manager *man;
    249 
    250 	man = &bdev->man[mem->mem_type];
    251 
    252 	if (virtual && mem->bus.addr == NULL)
    253 #ifdef __NetBSD__
    254 		bus_space_unmap(bdev->memt, mem->bus.memh, mem->bus.size);
    255 #else
    256 		iounmap(virtual);
    257 #endif
    258 	(void) ttm_mem_io_lock(man, false);
    259 	ttm_mem_io_free(bdev, mem);
    260 	ttm_mem_io_unlock(man);
    261 }
    262 
    263 #ifdef __NetBSD__
    264 #  define	ioread32	fake_ioread32
    265 #  define	iowrite32	fake_iowrite32
    266 
    267 static inline uint32_t
    268 fake_ioread32(const volatile uint32_t *p)
    269 {
    270 	uint32_t v;
    271 
    272 	v = *p;
    273 	__insn_barrier();	/* XXX */
    274 
    275 	return v;
    276 }
    277 
    278 static inline void
    279 iowrite32(uint32_t v, volatile uint32_t *p)
    280 {
    281 
    282 	__insn_barrier();	/* XXX */
    283 	*p = v;
    284 }
    285 #endif
    286 
    287 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
    288 {
    289 	uint32_t *dstP =
    290 	    (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
    291 	uint32_t *srcP =
    292 	    (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
    293 
    294 	int i;
    295 	for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
    296 		iowrite32(ioread32(srcP++), dstP++);
    297 	return 0;
    298 }
    299 
    300 #ifdef __NetBSD__
    301 #  undef	ioread32
    302 #  undef	iowrite32
    303 #endif
    304 
    305 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
    306 				unsigned long page,
    307 				pgprot_t prot)
    308 {
    309 	struct page *d = ttm->pages[page];
    310 	void *dst;
    311 
    312 	if (!d)
    313 		return -ENOMEM;
    314 
    315 	src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
    316 
    317 #ifdef CONFIG_X86
    318 	dst = kmap_atomic_prot(d, prot);
    319 #else
    320 	if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
    321 		dst = vmap(&d, 1, 0, prot);
    322 	else
    323 		dst = kmap(d);
    324 #endif
    325 	if (!dst)
    326 		return -ENOMEM;
    327 
    328 	memcpy_fromio(dst, src, PAGE_SIZE);
    329 
    330 #ifdef CONFIG_X86
    331 	kunmap_atomic(dst);
    332 #else
    333 	if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
    334 #ifdef __NetBSD__
    335 		vunmap(dst, 1);
    336 #else
    337 		vunmap(dst);
    338 #endif
    339 	else
    340 		kunmap(d);
    341 #endif
    342 
    343 	return 0;
    344 }
    345 
    346 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
    347 				unsigned long page,
    348 				pgprot_t prot)
    349 {
    350 	struct page *s = ttm->pages[page];
    351 	void *src;
    352 
    353 	if (!s)
    354 		return -ENOMEM;
    355 
    356 	dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
    357 #ifdef CONFIG_X86
    358 	src = kmap_atomic_prot(s, prot);
    359 #else
    360 	if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
    361 		src = vmap(&s, 1, 0, prot);
    362 	else
    363 		src = kmap(s);
    364 #endif
    365 	if (!src)
    366 		return -ENOMEM;
    367 
    368 	memcpy_toio(dst, src, PAGE_SIZE);
    369 
    370 #ifdef CONFIG_X86
    371 	kunmap_atomic(src);
    372 #else
    373 	if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
    374 		vunmap(src);
    375 	else
    376 		kunmap(s);
    377 #endif
    378 
    379 	return 0;
    380 }
    381 
    382 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
    383 		       bool evict, bool no_wait_gpu,
    384 		       struct ttm_mem_reg *new_mem)
    385 {
    386 	struct ttm_bo_device *bdev = bo->bdev;
    387 	struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
    388 	struct ttm_tt *ttm = bo->ttm;
    389 	struct ttm_mem_reg *old_mem = &bo->mem;
    390 	struct ttm_mem_reg old_copy = *old_mem;
    391 	void *old_iomap;
    392 	void *new_iomap;
    393 	int ret;
    394 	unsigned long i;
    395 	unsigned long page;
    396 	unsigned long add = 0;
    397 	int dir;
    398 
    399 	ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
    400 	if (ret)
    401 		return ret;
    402 	ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
    403 	if (ret)
    404 		goto out;
    405 
    406 	/*
    407 	 * Single TTM move. NOP.
    408 	 */
    409 	if (old_iomap == NULL && new_iomap == NULL)
    410 		goto out2;
    411 
    412 	/*
    413 	 * Don't move nonexistent data. Clear destination instead.
    414 	 */
    415 	if (old_iomap == NULL &&
    416 	    (ttm == NULL || (ttm->state == tt_unpopulated &&
    417 			     !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
    418 		memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
    419 		goto out2;
    420 	}
    421 
    422 	/*
    423 	 * TTM might be null for moves within the same region.
    424 	 */
    425 	if (ttm && ttm->state == tt_unpopulated) {
    426 		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
    427 		if (ret)
    428 			goto out1;
    429 	}
    430 
    431 	add = 0;
    432 	dir = 1;
    433 
    434 	if ((old_mem->mem_type == new_mem->mem_type) &&
    435 	    (new_mem->start < old_mem->start + old_mem->size)) {
    436 		dir = -1;
    437 		add = new_mem->num_pages - 1;
    438 	}
    439 
    440 	for (i = 0; i < new_mem->num_pages; ++i) {
    441 		page = i * dir + add;
    442 		if (old_iomap == NULL) {
    443 			pgprot_t prot = ttm_io_prot(old_mem->placement,
    444 						    PAGE_KERNEL);
    445 			ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
    446 						   prot);
    447 		} else if (new_iomap == NULL) {
    448 			pgprot_t prot = ttm_io_prot(new_mem->placement,
    449 						    PAGE_KERNEL);
    450 			ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
    451 						   prot);
    452 		} else
    453 			ret = ttm_copy_io_page(new_iomap, old_iomap, page);
    454 		if (ret)
    455 			goto out1;
    456 	}
    457 	mb();
    458 out2:
    459 	old_copy = *old_mem;
    460 	*old_mem = *new_mem;
    461 	new_mem->mm_node = NULL;
    462 
    463 	if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) {
    464 		ttm_tt_unbind(ttm);
    465 		ttm_tt_destroy(ttm);
    466 		bo->ttm = NULL;
    467 	}
    468 
    469 out1:
    470 	ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
    471 out:
    472 	ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
    473 
    474 	/*
    475 	 * On error, keep the mm node!
    476 	 */
    477 	if (!ret)
    478 		ttm_bo_mem_put(bo, &old_copy);
    479 	return ret;
    480 }
    481 EXPORT_SYMBOL(ttm_bo_move_memcpy);
    482 
    483 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
    484 {
    485 	kfree(bo);
    486 }
    487 
    488 /**
    489  * ttm_buffer_object_transfer
    490  *
    491  * @bo: A pointer to a struct ttm_buffer_object.
    492  * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
    493  * holding the data of @bo with the old placement.
    494  *
    495  * This is a utility function that may be called after an accelerated move
    496  * has been scheduled. A new buffer object is created as a placeholder for
    497  * the old data while it's being copied. When that buffer object is idle,
    498  * it can be destroyed, releasing the space of the old placement.
    499  * Returns:
    500  * !0: Failure.
    501  */
    502 
    503 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
    504 				      struct ttm_buffer_object **new_obj)
    505 {
    506 	struct ttm_buffer_object *fbo;
    507 	struct ttm_bo_device *bdev = bo->bdev;
    508 	struct ttm_bo_driver *driver = bdev->driver;
    509 	int ret;
    510 
    511 	fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
    512 	if (!fbo)
    513 		return -ENOMEM;
    514 
    515 	*fbo = *bo;
    516 
    517 	/**
    518 	 * Fix up members that we shouldn't copy directly:
    519 	 * TODO: Explicit member copy would probably be better here.
    520 	 */
    521 
    522 	INIT_LIST_HEAD(&fbo->ddestroy);
    523 	INIT_LIST_HEAD(&fbo->lru);
    524 	INIT_LIST_HEAD(&fbo->swap);
    525 	INIT_LIST_HEAD(&fbo->io_reserve_lru);
    526 #ifdef __NetBSD__
    527 	linux_mutex_init(&fbo->wu_mutex);
    528 	drm_vma_node_init(&fbo->vma_node);
    529 	uvm_obj_init(&fbo->uvmobj, bdev->driver->ttm_uvm_ops, true, 1);
    530 	mutex_obj_hold(bo->uvmobj.vmobjlock);
    531 	uvm_obj_setlock(&fbo->uvmobj, bo->uvmobj.vmobjlock);
    532 #else
    533 	mutex_init(&fbo->wu_mutex);
    534 	drm_vma_node_reset(&fbo->vma_node);
    535 #endif
    536 	atomic_set(&fbo->cpu_writers, 0);
    537 
    538 	spin_lock(&bdev->fence_lock);
    539 	if (bo->sync_obj)
    540 		fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
    541 	else
    542 		fbo->sync_obj = NULL;
    543 	spin_unlock(&bdev->fence_lock);
    544 	kref_init(&fbo->list_kref);
    545 	kref_init(&fbo->kref);
    546 	fbo->destroy = &ttm_transfered_destroy;
    547 	fbo->acc_size = 0;
    548 	fbo->resv = &fbo->ttm_resv;
    549 	reservation_object_init(fbo->resv);
    550 	ret = ww_mutex_trylock(&fbo->resv->lock);
    551 	WARN_ON(!ret);
    552 
    553 	*new_obj = fbo;
    554 	return 0;
    555 }
    556 
    557 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
    558 {
    559 #ifdef __NetBSD__
    560 	switch (caching_flags & TTM_PL_MASK_CACHING) {
    561 	case TTM_PL_FLAG_CACHED:
    562 		return (tmp | PMAP_WRITE_BACK);
    563 	case TTM_PL_FLAG_WC:
    564 		return (tmp | PMAP_WRITE_COMBINE);
    565 	case TTM_PL_FLAG_UNCACHED:
    566 		return (tmp | PMAP_NOCACHE);
    567 	default:
    568 		panic("invalid caching flags: %"PRIx32"\n",
    569 		    (caching_flags & TTM_PL_MASK_CACHING));
    570 	}
    571 #else
    572 #if defined(__i386__) || defined(__x86_64__)
    573 	if (caching_flags & TTM_PL_FLAG_WC)
    574 		tmp = pgprot_writecombine(tmp);
    575 	else if (boot_cpu_data.x86 > 3)
    576 		tmp = pgprot_noncached(tmp);
    577 
    578 #elif defined(__powerpc__)
    579 	if (!(caching_flags & TTM_PL_FLAG_CACHED)) {
    580 		pgprot_val(tmp) |= _PAGE_NO_CACHE;
    581 		if (caching_flags & TTM_PL_FLAG_UNCACHED)
    582 			pgprot_val(tmp) |= _PAGE_GUARDED;
    583 	}
    584 #endif
    585 #if defined(__ia64__)
    586 	if (caching_flags & TTM_PL_FLAG_WC)
    587 		tmp = pgprot_writecombine(tmp);
    588 	else
    589 		tmp = pgprot_noncached(tmp);
    590 #endif
    591 #if defined(__sparc__) || defined(__mips__)
    592 	if (!(caching_flags & TTM_PL_FLAG_CACHED))
    593 		tmp = pgprot_noncached(tmp);
    594 #endif
    595 	return tmp;
    596 #endif
    597 }
    598 EXPORT_SYMBOL(ttm_io_prot);
    599 
    600 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
    601 			  unsigned long offset,
    602 			  unsigned long size,
    603 			  struct ttm_bo_kmap_obj *map)
    604 {
    605 	struct ttm_mem_reg *mem = &bo->mem;
    606 
    607 	if (bo->mem.bus.addr) {
    608 		map->bo_kmap_type = ttm_bo_map_premapped;
    609 		map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
    610 	} else {
    611 		map->bo_kmap_type = ttm_bo_map_iomap;
    612 #ifdef __NetBSD__
    613 	    {
    614 		bus_addr_t addr;
    615 		int flags = BUS_SPACE_MAP_LINEAR;
    616 		int ret;
    617 
    618 		addr = (bo->mem.bus.base + bo->mem.bus.offset + offset);
    619 		if (ISSET(mem->placement, TTM_PL_FLAG_WC))
    620 			flags |= BUS_SPACE_MAP_PREFETCHABLE;
    621 		/* XXX errno NetBSD->Linux */
    622 		ret = -bus_space_map(bo->bdev->memt, addr, size, flags,
    623 		    &map->u.io.memh);
    624 		if (ret)
    625 			return ret;
    626 		map->u.io.size = size;
    627 		map->virtual = bus_space_vaddr(bo->bdev->memt, map->u.io.memh);
    628 	    }
    629 #else
    630 		if (mem->placement & TTM_PL_FLAG_WC)
    631 			map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
    632 						  size);
    633 		else
    634 			map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
    635 						       size);
    636 #endif
    637 	}
    638 	return (!map->virtual) ? -ENOMEM : 0;
    639 }
    640 
    641 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
    642 			   unsigned long start_page,
    643 			   unsigned long num_pages,
    644 			   struct ttm_bo_kmap_obj *map)
    645 {
    646 	struct ttm_mem_reg *mem = &bo->mem;
    647 	pgprot_t prot;
    648 	struct ttm_tt *ttm = bo->ttm;
    649 #ifdef __NetBSD__
    650 	unsigned i;
    651 	vaddr_t vaddr;
    652 #endif
    653 	int ret;
    654 
    655 	BUG_ON(!ttm);
    656 
    657 	if (ttm->state == tt_unpopulated) {
    658 		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
    659 		if (ret)
    660 			return ret;
    661 	}
    662 
    663 #ifdef __NetBSD__
    664 	/*
    665 	 * Can't use uvm_map here because it provides no way to pass
    666 	 * along the cacheability flags.  So we'll uvm_km_alloc
    667 	 * ourselves some KVA and then pmap_kenter_pa directly.
    668 	 */
    669 
    670 	KASSERT(num_pages <= ttm->num_pages);
    671 	KASSERT(start_page <= (ttm->num_pages - num_pages));
    672 	prot = ttm_io_prot(mem->placement, (VM_PROT_READ | VM_PROT_WRITE));
    673 	vaddr = uvm_km_alloc(kernel_map, (num_pages << PAGE_SHIFT), PAGE_SIZE,
    674 	    UVM_KMF_VAONLY | UVM_KMF_CANFAIL | UVM_KMF_WAITVA);
    675 	if (vaddr == 0)
    676 		return -ENOMEM;
    677 	for (i = 0; i < num_pages; i++)
    678 		pmap_kenter_pa(vaddr + i*PAGE_SIZE,
    679 		    page_to_phys(ttm->pages[start_page + i]),
    680 		    (VM_PROT_READ | VM_PROT_WRITE), prot);
    681 	pmap_update(pmap_kernel());
    682 	map->bo_kmap_type = ttm_bo_map_vmap;
    683 	map->u.uvm.vsize = (num_pages << PAGE_SHIFT);
    684 	map->virtual = (void *)vaddr;
    685 	return 0;
    686 #else
    687 	if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
    688 		/*
    689 		 * We're mapping a single page, and the desired
    690 		 * page protection is consistent with the bo.
    691 		 */
    692 
    693 		map->bo_kmap_type = ttm_bo_map_kmap;
    694 		map->page = ttm->pages[start_page];
    695 		map->virtual = kmap(map->page);
    696 	} else {
    697 		/*
    698 		 * We need to use vmap to get the desired page protection
    699 		 * or to make the buffer object look contiguous.
    700 		 */
    701 		prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
    702 			PAGE_KERNEL :
    703 			ttm_io_prot(mem->placement, PAGE_KERNEL);
    704 		map->bo_kmap_type = ttm_bo_map_vmap;
    705 		map->virtual = vmap(ttm->pages + start_page, num_pages,
    706 				    0, prot);
    707 	}
    708 	return (!map->virtual) ? -ENOMEM : 0;
    709 #endif
    710 }
    711 
    712 int ttm_bo_kmap(struct ttm_buffer_object *bo,
    713 		unsigned long start_page, unsigned long num_pages,
    714 		struct ttm_bo_kmap_obj *map)
    715 {
    716 	struct ttm_mem_type_manager *man =
    717 		&bo->bdev->man[bo->mem.mem_type];
    718 	unsigned long offset, size;
    719 	int ret;
    720 
    721 	BUG_ON(!list_empty(&bo->swap));
    722 	map->virtual = NULL;
    723 	map->bo = bo;
    724 	if (num_pages > bo->num_pages)
    725 		return -EINVAL;
    726 	if (start_page > bo->num_pages)
    727 		return -EINVAL;
    728 #if 0
    729 	if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
    730 		return -EPERM;
    731 #endif
    732 	(void) ttm_mem_io_lock(man, false);
    733 	ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
    734 	ttm_mem_io_unlock(man);
    735 	if (ret)
    736 		return ret;
    737 	if (!bo->mem.bus.is_iomem) {
    738 		return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
    739 	} else {
    740 		offset = start_page << PAGE_SHIFT;
    741 		size = num_pages << PAGE_SHIFT;
    742 		return ttm_bo_ioremap(bo, offset, size, map);
    743 	}
    744 }
    745 EXPORT_SYMBOL(ttm_bo_kmap);
    746 
    747 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
    748 {
    749 	struct ttm_buffer_object *bo = map->bo;
    750 	struct ttm_mem_type_manager *man =
    751 		&bo->bdev->man[bo->mem.mem_type];
    752 
    753 	if (!map->virtual)
    754 		return;
    755 	switch (map->bo_kmap_type) {
    756 	case ttm_bo_map_iomap:
    757 #ifdef __NetBSD__
    758 		bus_space_unmap(bo->bdev->memt, map->u.io.memh,
    759 		    map->u.io.size);
    760 #else
    761 		iounmap(map->virtual);
    762 #endif
    763 		break;
    764 	case ttm_bo_map_vmap:
    765 #ifdef __NetBSD__
    766 		pmap_kremove((vaddr_t)map->virtual, map->u.uvm.vsize);
    767 		pmap_update(pmap_kernel());
    768 		uvm_km_free(kernel_map, (vaddr_t)map->virtual,
    769 		    map->u.uvm.vsize, UVM_KMF_VAONLY);
    770 #else
    771 		vunmap(map->virtual);
    772 #endif
    773 		break;
    774 	case ttm_bo_map_kmap:
    775 #ifdef __NetBSD__
    776 		panic("ttm_bo_map_kmap does not exist in NetBSD");
    777 #else
    778 		kunmap(map->page);
    779 #endif
    780 		break;
    781 	case ttm_bo_map_premapped:
    782 		break;
    783 	default:
    784 		BUG();
    785 	}
    786 	(void) ttm_mem_io_lock(man, false);
    787 	ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
    788 	ttm_mem_io_unlock(man);
    789 	map->virtual = NULL;
    790 #ifndef __NetBSD__
    791 	map->page = NULL;
    792 #endif
    793 }
    794 EXPORT_SYMBOL(ttm_bo_kunmap);
    795 
    796 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
    797 			      void *sync_obj,
    798 			      bool evict,
    799 			      bool no_wait_gpu,
    800 			      struct ttm_mem_reg *new_mem)
    801 {
    802 	struct ttm_bo_device *bdev = bo->bdev;
    803 	struct ttm_bo_driver *driver = bdev->driver;
    804 	struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
    805 	struct ttm_mem_reg *old_mem = &bo->mem;
    806 	int ret;
    807 	struct ttm_buffer_object *ghost_obj;
    808 	void *tmp_obj = NULL;
    809 
    810 	spin_lock(&bdev->fence_lock);
    811 	if (bo->sync_obj) {
    812 		tmp_obj = bo->sync_obj;
    813 		bo->sync_obj = NULL;
    814 	}
    815 	bo->sync_obj = driver->sync_obj_ref(sync_obj);
    816 	if (evict) {
    817 		ret = ttm_bo_wait(bo, false, false, false);
    818 		spin_unlock(&bdev->fence_lock);
    819 		if (tmp_obj)
    820 			driver->sync_obj_unref(&tmp_obj);
    821 		if (ret)
    822 			return ret;
    823 
    824 		if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
    825 		    (bo->ttm != NULL)) {
    826 			ttm_tt_unbind(bo->ttm);
    827 			ttm_tt_destroy(bo->ttm);
    828 			bo->ttm = NULL;
    829 		}
    830 		ttm_bo_free_old_node(bo);
    831 	} else {
    832 		/**
    833 		 * This should help pipeline ordinary buffer moves.
    834 		 *
    835 		 * Hang old buffer memory on a new buffer object,
    836 		 * and leave it to be released when the GPU
    837 		 * operation has completed.
    838 		 */
    839 
    840 		set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
    841 		spin_unlock(&bdev->fence_lock);
    842 		if (tmp_obj)
    843 			driver->sync_obj_unref(&tmp_obj);
    844 
    845 		ret = ttm_buffer_object_transfer(bo, &ghost_obj);
    846 		if (ret)
    847 			return ret;
    848 
    849 		/**
    850 		 * If we're not moving to fixed memory, the TTM object
    851 		 * needs to stay alive. Otherwhise hang it on the ghost
    852 		 * bo to be unbound and destroyed.
    853 		 */
    854 
    855 		if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
    856 			ghost_obj->ttm = NULL;
    857 		else
    858 			bo->ttm = NULL;
    859 
    860 		ttm_bo_unreserve(ghost_obj);
    861 		ttm_bo_unref(&ghost_obj);
    862 	}
    863 
    864 	*old_mem = *new_mem;
    865 	new_mem->mm_node = NULL;
    866 
    867 	return 0;
    868 }
    869 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
    870