Home | History | Annotate | Line # | Download | only in instmem
      1 /*	$NetBSD: nouveau_nvkm_subdev_instmem_gk20a.c,v 1.10 2024/06/04 21:43:39 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2015, NVIDIA CORPORATION. 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 "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     22  * DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 /*
     26  * GK20A does not have dedicated video memory, and to accurately represent this
     27  * fact Nouveau will not create a RAM device for it. Therefore its instmem
     28  * implementation must be done directly on top of system memory, while
     29  * preserving coherency for read and write operations.
     30  *
     31  * Instmem can be allocated through two means:
     32  * 1) If an IOMMU unit has been probed, the IOMMU API is used to make memory
     33  *    pages contiguous to the GPU. This is the preferred way.
     34  * 2) If no IOMMU unit is probed, the DMA API is used to allocate physically
     35  *    contiguous memory.
     36  *
     37  * In both cases CPU read and writes are performed by creating a write-combined
     38  * mapping. The GPU L2 cache must thus be flushed/invalidated when required. To
     39  * be conservative we do this every time we acquire or release an instobj, but
     40  * ideally L2 management should be handled at a higher level.
     41  *
     42  * To improve performance, CPU mappings are not removed upon instobj release.
     43  * Instead they are placed into a LRU list to be recycled when the mapped space
     44  * goes beyond a certain threshold. At the moment this limit is 1MB.
     45  */
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_instmem_gk20a.c,v 1.10 2024/06/04 21:43:39 riastradh Exp $");
     48 
     49 #include "priv.h"
     50 
     51 #include <core/memory.h>
     52 #include <core/tegra.h>
     53 #include <subdev/ltc.h>
     54 #include <subdev/mmu.h>
     55 
     56 #include <linux/nbsd-namespace.h>
     57 
     58 #ifdef __NetBSD__
     59 #  define	__iomem	__nvkm_memory_iomem
     60 #endif
     61 
     62 struct gk20a_instobj {
     63 	struct nvkm_memory memory;
     64 	struct nvkm_mm_node *mn;
     65 	struct gk20a_instmem *imem;
     66 
     67 	/* CPU mapping */
     68 	u32 *vaddr;
     69 };
     70 #define gk20a_instobj(p) container_of((p), struct gk20a_instobj, memory)
     71 
     72 #ifndef __NetBSD__
     73 /*
     74  * Used for objects allocated using the DMA API
     75  */
     76 struct gk20a_instobj_dma {
     77 	struct gk20a_instobj base;
     78 
     79 	dma_addr_t handle;
     80 	struct nvkm_mm_node r;
     81 };
     82 #define gk20a_instobj_dma(p) \
     83 	container_of(gk20a_instobj(p), struct gk20a_instobj_dma, base)
     84 #endif
     85 
     86 /*
     87  * Used for objects flattened using the IOMMU API
     88  */
     89 struct gk20a_instobj_iommu {
     90 	struct gk20a_instobj base;
     91 
     92 	/* to link into gk20a_instmem::vaddr_lru */
     93 	struct list_head vaddr_node;
     94 	/* how many clients are using vaddr? */
     95 	u32 use_cpt;
     96 
     97 #ifdef __NetBSD__
     98 	struct nvkm_mm_node mm_node; /* XXX */
     99 	bus_dmamap_t map;
    100 	int nsegs;
    101 	bus_dma_segment_t segs[];
    102 #else
    103 	/* will point to the higher half of pages */
    104 	dma_addr_t *dma_addrs;
    105 	/* array of base.mem->size pages (+ dma_addr_ts) */
    106 	struct page *pages[];
    107 #endif
    108 };
    109 #define gk20a_instobj_iommu(p) \
    110 	container_of(gk20a_instobj(p), struct gk20a_instobj_iommu, base)
    111 
    112 struct gk20a_instmem {
    113 	struct nvkm_instmem base;
    114 
    115 	/* protects vaddr_* and gk20a_instobj::vaddr* */
    116 	struct mutex lock;
    117 
    118 	/* CPU mappings LRU */
    119 	unsigned int vaddr_use;
    120 	unsigned int vaddr_max;
    121 	struct list_head vaddr_lru;
    122 
    123 #ifdef __NetBSD__
    124 	bus_dma_tag_t dmat;
    125 #else
    126 	/* Only used if IOMMU if present */
    127 	struct mutex *mm_mutex;
    128 	struct nvkm_mm *mm;
    129 	struct iommu_domain *domain;
    130 	unsigned long iommu_pgshift;
    131 	u16 iommu_bit;
    132 
    133 	/* Only used by DMA API */
    134 	unsigned long attrs;
    135 #endif
    136 };
    137 #define gk20a_instmem(p) container_of((p), struct gk20a_instmem, base)
    138 
    139 static enum nvkm_memory_target
    140 gk20a_instobj_target(struct nvkm_memory *memory)
    141 {
    142 	return NVKM_MEM_TARGET_NCOH;
    143 }
    144 
    145 static u8
    146 gk20a_instobj_page(struct nvkm_memory *memory)
    147 {
    148 	return 12;
    149 }
    150 
    151 static u64
    152 gk20a_instobj_addr(struct nvkm_memory *memory)
    153 {
    154 	return (u64)gk20a_instobj(memory)->mn->offset << 12;
    155 }
    156 
    157 static u64
    158 gk20a_instobj_size(struct nvkm_memory *memory)
    159 {
    160 	return (u64)gk20a_instobj(memory)->mn->length << 12;
    161 }
    162 
    163 /*
    164  * Recycle the vaddr of obj. Must be called with gk20a_instmem::lock held.
    165  */
    166 static void
    167 gk20a_instobj_iommu_recycle_vaddr(struct gk20a_instobj_iommu *obj)
    168 {
    169 	struct gk20a_instmem *imem = obj->base.imem;
    170 	/* there should not be any user left... */
    171 	WARN_ON(obj->use_cpt);
    172 	list_del(&obj->vaddr_node);
    173 #ifdef __NetBSD__
    174 	bus_size_t size = nvkm_memory_size(&obj->base.memory);
    175 	bus_dmamem_unmap(imem->dmat, obj->base.vaddr, size);
    176 #else
    177 	vunmap(obj->base.vaddr);
    178 #endif
    179 	obj->base.vaddr = NULL;
    180 	imem->vaddr_use -= nvkm_memory_size(&obj->base.memory);
    181 	nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n", imem->vaddr_use,
    182 		   imem->vaddr_max);
    183 }
    184 
    185 
    186 /*
    187  * Must be called while holding gk20a_instmem::lock
    188  */
    189 static void
    190 gk20a_instmem_vaddr_gc(struct gk20a_instmem *imem, const u64 size)
    191 {
    192 	while (imem->vaddr_use + size > imem->vaddr_max) {
    193 		/* no candidate that can be unmapped, abort... */
    194 		if (list_empty(&imem->vaddr_lru))
    195 			break;
    196 
    197 		gk20a_instobj_iommu_recycle_vaddr(
    198 				list_first_entry(&imem->vaddr_lru,
    199 				struct gk20a_instobj_iommu, vaddr_node));
    200 	}
    201 }
    202 
    203 #ifndef __NetBSD__
    204 static void __iomem *
    205 gk20a_instobj_acquire_dma(struct nvkm_memory *memory)
    206 {
    207 	struct gk20a_instobj *node = gk20a_instobj(memory);
    208 	struct gk20a_instmem *imem = node->imem;
    209 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
    210 
    211 	nvkm_ltc_flush(ltc);
    212 
    213 	return node->vaddr;
    214 }
    215 #endif
    216 
    217 static void __iomem *
    218 gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
    219 {
    220 	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
    221 	struct gk20a_instmem *imem = node->base.imem;
    222 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
    223 	const u64 size = nvkm_memory_size(memory);
    224 
    225 	nvkm_ltc_flush(ltc);
    226 
    227 	mutex_lock(&imem->lock);
    228 
    229 	if (node->base.vaddr) {
    230 		if (!node->use_cpt) {
    231 			/* remove from LRU list since mapping in use again */
    232 			list_del(&node->vaddr_node);
    233 		}
    234 		goto out;
    235 	}
    236 
    237 	/* try to free some address space if we reached the limit */
    238 	gk20a_instmem_vaddr_gc(imem, size);
    239 
    240 	/* map the pages */
    241 #ifdef __NetBSD__
    242 	void *kva;
    243 	if (bus_dmamem_map(imem->dmat, node->segs, node->nsegs, size,
    244 		&kva, BUS_DMA_WAITOK|BUS_DMA_PREFETCHABLE))
    245 		node->base.vaddr = NULL;
    246 	else
    247 		node->base.vaddr = kva;
    248 #else
    249 	node->base.vaddr = vmap(node->pages, size >> PAGE_SHIFT, VM_MAP,
    250 				pgprot_writecombine(PAGE_KERNEL));
    251 #endif
    252 	if (!node->base.vaddr) {
    253 		nvkm_error(&imem->base.subdev, "cannot map instobj - "
    254 			   "this is not going to end well...\n");
    255 		goto out;
    256 	}
    257 
    258 	imem->vaddr_use += size;
    259 	nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n",
    260 		   imem->vaddr_use, imem->vaddr_max);
    261 
    262 out:
    263 	node->use_cpt++;
    264 	mutex_unlock(&imem->lock);
    265 
    266 	return node->base.vaddr;
    267 }
    268 
    269 #ifndef __NetBSD__
    270 static void
    271 gk20a_instobj_release_dma(struct nvkm_memory *memory)
    272 {
    273 	struct gk20a_instobj *node = gk20a_instobj(memory);
    274 	struct gk20a_instmem *imem = node->imem;
    275 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
    276 
    277 	/* in case we got a write-combined mapping */
    278 	wmb();
    279 	nvkm_ltc_invalidate(ltc);
    280 }
    281 #endif
    282 
    283 static void
    284 gk20a_instobj_release_iommu(struct nvkm_memory *memory)
    285 {
    286 	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
    287 	struct gk20a_instmem *imem = node->base.imem;
    288 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
    289 
    290 	mutex_lock(&imem->lock);
    291 
    292 	/* we should at least have one user to release... */
    293 	if (WARN_ON(node->use_cpt == 0))
    294 		goto out;
    295 
    296 	/* add unused objs to the LRU list to recycle their mapping */
    297 	if (--node->use_cpt == 0)
    298 		list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
    299 
    300 out:
    301 	mutex_unlock(&imem->lock);
    302 
    303 	wmb();
    304 	nvkm_ltc_invalidate(ltc);
    305 }
    306 
    307 static u32
    308 gk20a_instobj_rd32(struct nvkm_memory *memory, u64 offset)
    309 {
    310 	struct gk20a_instobj *node = gk20a_instobj(memory);
    311 
    312 	return node->vaddr[offset / 4];
    313 }
    314 
    315 static void
    316 gk20a_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
    317 {
    318 	struct gk20a_instobj *node = gk20a_instobj(memory);
    319 
    320 	node->vaddr[offset / 4] = data;
    321 }
    322 
    323 static int
    324 gk20a_instobj_map(struct nvkm_memory *memory, u64 offset, struct nvkm_vmm *vmm,
    325 		  struct nvkm_vma *vma, void *argv, u32 argc)
    326 {
    327 	struct gk20a_instobj *node = gk20a_instobj(memory);
    328 	struct nvkm_vmm_map map = {
    329 		.memory = &node->memory,
    330 		.offset = offset,
    331 		.mem = node->mn,
    332 	};
    333 
    334 	return nvkm_vmm_map(vmm, vma, argv, argc, &map);
    335 }
    336 
    337 #ifndef __NetBSD__
    338 static void *
    339 gk20a_instobj_dtor_dma(struct nvkm_memory *memory)
    340 {
    341 	struct gk20a_instobj_dma *node = gk20a_instobj_dma(memory);
    342 	struct gk20a_instmem *imem = node->base.imem;
    343 	struct device *dev = imem->base.subdev.device->dev;
    344 
    345 	if (unlikely(!node->base.vaddr))
    346 		goto out;
    347 
    348 	dma_free_attrs(dev, (u64)node->base.mn->length << PAGE_SHIFT,
    349 		       node->base.vaddr, node->handle, imem->attrs);
    350 
    351 out:
    352 	return node;
    353 }
    354 #endif
    355 
    356 static void *
    357 gk20a_instobj_dtor_iommu(struct nvkm_memory *memory)
    358 {
    359 	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
    360 	struct gk20a_instmem *imem = node->base.imem;
    361 	struct device *dev = imem->base.subdev.device->dev;
    362 	struct nvkm_mm_node *r = node->base.mn;
    363 	int i;
    364 
    365 	if (unlikely(!r))
    366 		goto out;
    367 
    368 	mutex_lock(&imem->lock);
    369 
    370 	/* vaddr has already been recycled */
    371 	if (node->base.vaddr)
    372 		gk20a_instobj_iommu_recycle_vaddr(node);
    373 
    374 	mutex_unlock(&imem->lock);
    375 
    376 #ifdef __NetBSD__
    377 	__USE(i);
    378 	__USE(dev);
    379 	bus_dmamap_unload(imem->dmat, node->map);
    380 	bus_dmamap_destroy(imem->dmat, node->map);
    381 	bus_dmamem_free(imem->dmat, node->segs, node->nsegs);
    382 #else
    383 	/* clear IOMMU bit to unmap pages */
    384 	r->offset &= ~BIT(imem->iommu_bit - imem->iommu_pgshift);
    385 
    386 	/* Unmap pages from GPU address space and free them */
    387 	for (i = 0; i < node->base.mn->length; i++) {
    388 		iommu_unmap(imem->domain,
    389 			    (r->offset + i) << imem->iommu_pgshift, PAGE_SIZE);
    390 		dma_unmap_page(dev, node->dma_addrs[i], PAGE_SIZE,
    391 			       DMA_BIDIRECTIONAL);
    392 		__free_page(node->pages[i]);
    393 	}
    394 
    395 	/* Release area from GPU address space */
    396 	mutex_lock(imem->mm_mutex);
    397 	nvkm_mm_free(imem->mm, &r);
    398 	mutex_unlock(imem->mm_mutex);
    399 #endif
    400 
    401 out:
    402 	return node;
    403 }
    404 
    405 #ifndef __NetBSD__
    406 static const struct nvkm_memory_func
    407 gk20a_instobj_func_dma = {
    408 	.dtor = gk20a_instobj_dtor_dma,
    409 	.target = gk20a_instobj_target,
    410 	.page = gk20a_instobj_page,
    411 	.addr = gk20a_instobj_addr,
    412 	.size = gk20a_instobj_size,
    413 	.acquire = gk20a_instobj_acquire_dma,
    414 	.release = gk20a_instobj_release_dma,
    415 	.map = gk20a_instobj_map,
    416 };
    417 #endif
    418 
    419 static const struct nvkm_memory_func
    420 gk20a_instobj_func_iommu = {
    421 	.dtor = gk20a_instobj_dtor_iommu,
    422 	.target = gk20a_instobj_target,
    423 	.page = gk20a_instobj_page,
    424 	.addr = gk20a_instobj_addr,
    425 	.size = gk20a_instobj_size,
    426 	.acquire = gk20a_instobj_acquire_iommu,
    427 	.release = gk20a_instobj_release_iommu,
    428 	.map = gk20a_instobj_map,
    429 };
    430 
    431 static const struct nvkm_memory_ptrs
    432 gk20a_instobj_ptrs = {
    433 	.rd32 = gk20a_instobj_rd32,
    434 	.wr32 = gk20a_instobj_wr32,
    435 };
    436 
    437 #ifndef __NetBSD__
    438 static int
    439 gk20a_instobj_ctor_dma(struct gk20a_instmem *imem, u32 npages, u32 align,
    440 		       struct gk20a_instobj **_node)
    441 {
    442 	struct gk20a_instobj_dma *node;
    443 	struct nvkm_subdev *subdev = &imem->base.subdev;
    444 	struct device *dev = subdev->device->dev;
    445 
    446 	if (!(node = kzalloc(sizeof(*node), GFP_KERNEL)))
    447 		return -ENOMEM;
    448 	*_node = &node->base;
    449 
    450 	nvkm_memory_ctor(&gk20a_instobj_func_dma, &node->base.memory);
    451 	node->base.memory.ptrs = &gk20a_instobj_ptrs;
    452 
    453 	node->base.vaddr = dma_alloc_attrs(dev, npages << PAGE_SHIFT,
    454 					   &node->handle, GFP_KERNEL,
    455 					   imem->attrs);
    456 	if (!node->base.vaddr) {
    457 		nvkm_error(subdev, "cannot allocate DMA memory\n");
    458 		return -ENOMEM;
    459 	}
    460 
    461 	/* alignment check */
    462 	if (unlikely(node->handle & (align - 1)))
    463 		nvkm_warn(subdev,
    464 			  "memory not aligned as requested: %pad (0x%x)\n",
    465 			  &node->handle, align);
    466 
    467 	/* present memory for being mapped using small pages */
    468 	node->r.type = 12;
    469 	node->r.offset = node->handle >> 12;
    470 	node->r.length = (npages << PAGE_SHIFT) >> 12;
    471 
    472 	node->base.mn = &node->r;
    473 	return 0;
    474 }
    475 #endif
    476 
    477 static int
    478 gk20a_instobj_ctor_iommu(struct gk20a_instmem *imem, u32 npages, u32 align,
    479 			 struct gk20a_instobj **_node)
    480 {
    481 	struct gk20a_instobj_iommu *node;
    482 	struct nvkm_subdev *subdev = &imem->base.subdev;
    483 	struct device *dev = subdev->device->dev;
    484 	struct nvkm_mm_node *r;
    485 	int ret;
    486 	int i;
    487 
    488 	/*
    489 	 * despite their variable size, instmem allocations are small enough
    490 	 * (< 1 page) to be handled by kzalloc
    491 	 */
    492 #ifdef __NetBSD__
    493 	node = kzalloc(struct_size(node, segs, npages), GFP_KERNEL);
    494 	if (node == NULL)
    495 		return -ENOMEM;
    496 #else
    497 	if (!(node = kzalloc(sizeof(*node) + ((sizeof(node->pages[0]) +
    498 			     sizeof(*node->dma_addrs)) * npages), GFP_KERNEL)))
    499 		return -ENOMEM;
    500 #endif
    501 	*_node = &node->base;
    502 #ifndef __NetBSD__
    503 	node->dma_addrs = (void *)(node->pages + npages);
    504 #endif
    505 
    506 	nvkm_memory_ctor(&gk20a_instobj_func_iommu, &node->base.memory);
    507 	node->base.memory.ptrs = &gk20a_instobj_ptrs;
    508 
    509 #ifdef __NetBSD__
    510 	bus_size_t nbytes = (bus_size_t)npages << PAGE_SHIFT;
    511 	__USE(i);
    512 	__USE(r);
    513 	__USE(dev);
    514 	/* XXX errno NetBSD->Linux */
    515 	ret = -bus_dmamem_alloc(imem->dmat, nbytes, PAGE_SIZE,
    516 	    PAGE_SIZE, node->segs, npages, &node->nsegs, BUS_DMA_WAITOK);
    517 	if (ret)
    518 fail0:		goto out;
    519 	/* XXX errno NetBSD->Linux */
    520 	ret = -bus_dmamap_create(imem->dmat, nbytes, 1, nbytes, PAGE_SIZE,
    521 	    BUS_DMA_WAITOK, &node->map);
    522 	if (ret) {
    523 fail1:		bus_dmamem_free(imem->dmat, node->segs, node->nsegs);
    524 		goto fail0;
    525 	}
    526 	/* XXX errno NetBSD->Linux */
    527 	ret = -bus_dmamap_load_raw(imem->dmat, node->map, node->segs,
    528 	    node->nsegs, nbytes, BUS_DMA_WAITOK);
    529 	if (ret) {
    530 fail2: __unused
    531 		bus_dmamap_destroy(imem->dmat, node->map);
    532 		goto fail1;
    533 	}
    534 	node->mm_node.type = 12; /* XXX ??? */
    535 	node->mm_node.offset = node->map->dm_segs[0].ds_addr;
    536 	node->mm_node.length = node->map->dm_segs[0].ds_len;
    537 	node->base.mn = &node->mm_node;
    538 out:
    539 #else
    540 	/* Allocate backing memory */
    541 	for (i = 0; i < npages; i++) {
    542 		struct page *p = alloc_page(GFP_KERNEL);
    543 		dma_addr_t dma_adr;
    544 
    545 		if (p == NULL) {
    546 			ret = -ENOMEM;
    547 			goto free_pages;
    548 		}
    549 		node->pages[i] = p;
    550 		dma_adr = dma_map_page(dev, p, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
    551 		if (dma_mapping_error(dev, dma_adr)) {
    552 			nvkm_error(subdev, "DMA mapping error!\n");
    553 			ret = -ENOMEM;
    554 			goto free_pages;
    555 		}
    556 		node->dma_addrs[i] = dma_adr;
    557 	}
    558 
    559 	mutex_lock(imem->mm_mutex);
    560 	/* Reserve area from GPU address space */
    561 	ret = nvkm_mm_head(imem->mm, 0, 1, npages, npages,
    562 			   align >> imem->iommu_pgshift, &r);
    563 	mutex_unlock(imem->mm_mutex);
    564 	if (ret) {
    565 		nvkm_error(subdev, "IOMMU space is full!\n");
    566 		goto free_pages;
    567 	}
    568 
    569 	/* Map into GPU address space */
    570 	for (i = 0; i < npages; i++) {
    571 		u32 offset = (r->offset + i) << imem->iommu_pgshift;
    572 
    573 		ret = iommu_map(imem->domain, offset, node->dma_addrs[i],
    574 				PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
    575 		if (ret < 0) {
    576 			nvkm_error(subdev, "IOMMU mapping failure: %d\n", ret);
    577 
    578 			while (i-- > 0) {
    579 				offset -= PAGE_SIZE;
    580 				iommu_unmap(imem->domain, offset, PAGE_SIZE);
    581 			}
    582 			goto release_area;
    583 		}
    584 	}
    585 
    586 	/* IOMMU bit tells that an address is to be resolved through the IOMMU */
    587 	r->offset |= BIT(imem->iommu_bit - imem->iommu_pgshift);
    588 
    589 	node->base.mn = r;
    590 	return 0;
    591 
    592 release_area:
    593 	mutex_lock(imem->mm_mutex);
    594 	nvkm_mm_free(imem->mm, &r);
    595 	mutex_unlock(imem->mm_mutex);
    596 
    597 free_pages:
    598 	for (i = 0; i < npages && node->pages[i] != NULL; i++) {
    599 		dma_addr_t dma_addr = node->dma_addrs[i];
    600 		if (dma_addr)
    601 			dma_unmap_page(dev, dma_addr, PAGE_SIZE,
    602 				       DMA_BIDIRECTIONAL);
    603 		__free_page(node->pages[i]);
    604 	}
    605 #endif
    606 
    607 	return ret;
    608 }
    609 
    610 static int
    611 gk20a_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
    612 		  struct nvkm_memory **pmemory)
    613 {
    614 	struct gk20a_instmem *imem = gk20a_instmem(base);
    615 	struct nvkm_subdev *subdev = &imem->base.subdev;
    616 	struct gk20a_instobj *node = NULL;
    617 	int ret = 0;
    618 
    619 #ifdef __NetBSD__
    620 	nvkm_debug(subdev, "%s (%s): size: %x align: %x\n", __func__,
    621 		   "bus_dma", size, align);
    622 #else
    623 	nvkm_debug(subdev, "%s (%s): size: %x align: %x\n", __func__,
    624 		   imem->domain ? "IOMMU" : "DMA", size, align);
    625 #endif
    626 
    627 	/* Round size and align to page bounds */
    628 	size = max(roundup(size, PAGE_SIZE), PAGE_SIZE);
    629 	align = max(roundup(align, PAGE_SIZE), PAGE_SIZE);
    630 
    631 #ifdef __NetBSD__
    632 	ret = gk20a_instobj_ctor_iommu(imem, size >> PAGE_SHIFT, align, &node);
    633 #else
    634 	if (imem->domain)
    635 		ret = gk20a_instobj_ctor_iommu(imem, size >> PAGE_SHIFT,
    636 					       align, &node);
    637 	else
    638 		ret = gk20a_instobj_ctor_dma(imem, size >> PAGE_SHIFT,
    639 					     align, &node);
    640 #endif
    641 	*pmemory = node ? &node->memory : NULL;
    642 	if (ret)
    643 		return ret;
    644 
    645 	node->imem = imem;
    646 
    647 	nvkm_debug(subdev, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%"PRIx64"\n",
    648 		   size, align, (u64)node->mn->offset << 12);
    649 
    650 	return 0;
    651 }
    652 
    653 static void *
    654 gk20a_instmem_dtor(struct nvkm_instmem *base)
    655 {
    656 	struct gk20a_instmem *imem = gk20a_instmem(base);
    657 
    658 	/* perform some sanity checks... */
    659 	if (!list_empty(&imem->vaddr_lru))
    660 		nvkm_warn(&base->subdev, "instobj LRU not empty!\n");
    661 
    662 	if (imem->vaddr_use != 0)
    663 		nvkm_warn(&base->subdev, "instobj vmap area not empty! "
    664 			  "0x%x bytes still mapped\n", imem->vaddr_use);
    665 
    666 	mutex_destroy(&imem->lock);
    667 
    668 	return imem;
    669 }
    670 
    671 static const struct nvkm_instmem_func
    672 gk20a_instmem = {
    673 	.dtor = gk20a_instmem_dtor,
    674 	.memory_new = gk20a_instobj_new,
    675 	.zero = false,
    676 };
    677 
    678 int
    679 gk20a_instmem_new(struct nvkm_device *device, int index,
    680 		  struct nvkm_instmem **pimem)
    681 {
    682 #ifndef __NetBSD__
    683 	struct nvkm_device_tegra *tdev = device->func->tegra(device);
    684 #endif
    685 	struct gk20a_instmem *imem;
    686 
    687 	if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
    688 		return -ENOMEM;
    689 	nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
    690 	mutex_init(&imem->lock);
    691 	*pimem = &imem->base;
    692 
    693 	/* do not allow more than 1MB of CPU-mapped instmem */
    694 	imem->vaddr_use = 0;
    695 	imem->vaddr_max = 0x100000;
    696 	INIT_LIST_HEAD(&imem->vaddr_lru);
    697 
    698 #ifdef __NetBSD__
    699 	imem->dmat = device->func->dma_tag(device);
    700 	nvkm_info(&imem->base.subdev, "using bus_dma\n");
    701 #else
    702 	if (tdev->iommu.domain) {
    703 		imem->mm_mutex = &tdev->iommu.mutex;
    704 		imem->mm = &tdev->iommu.mm;
    705 		imem->domain = tdev->iommu.domain;
    706 		imem->iommu_pgshift = tdev->iommu.pgshift;
    707 		imem->iommu_bit = tdev->func->iommu_bit;
    708 
    709 		nvkm_info(&imem->base.subdev, "using IOMMU\n");
    710 	} else {
    711 		imem->attrs = DMA_ATTR_NON_CONSISTENT |
    712 			      DMA_ATTR_WEAK_ORDERING |
    713 			      DMA_ATTR_WRITE_COMBINE;
    714 
    715 		nvkm_info(&imem->base.subdev, "using DMA API\n");
    716 	}
    717 #endif
    718 
    719 	return 0;
    720 }
    721