gk20a.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * GK20A does not have dedicated video memory, and to accurately represent this
 * fact Nouveau will not create a RAM device for it. Therefore its instmem
 * implementation must be done directly on top of system memory, while providing
 * coherent read and write operations.
 *
 * Instmem can be allocated through two means:
 * 1) If an IOMMU mapping has been probed, the IOMMU API is used to make memory
 *    pages contiguous to the GPU. This is the preferred way.
 * 2) If no IOMMU mapping is probed, the DMA API is used to allocate physically
 *    contiguous memory.
 *
 * In both cases CPU read and writes are performed using PRAMIN (i.e. using the
 * GPU path) to ensure these operations are coherent for the GPU. This allows us
 * to use more "relaxed" allocation parameters when using the DMA API, since we
 * never need a kernel mapping.
 */

41 42 43
#include <subdev/fb.h>
#include <core/mm.h>

44 45
#ifdef __KERNEL__
#include <linux/dma-attrs.h>
46 47
#include <linux/iommu.h>
#include <nouveau_platform.h>
48 49
#endif

50 51
#include "priv.h"

B
Ben Skeggs 已提交
52
struct gk20a_instobj {
53 54 55 56 57
	struct nvkm_instobj base;
	/* Must be second member here - see nouveau_gpuobj_map_vm() */
	struct nvkm_mem *mem;
	/* Pointed by mem */
	struct nvkm_mem _mem;
58 59 60 61 62 63
};

/*
 * Used for objects allocated using the DMA API
 */
struct gk20a_instobj_dma {
B
Ben Skeggs 已提交
64
	struct gk20a_instobj base;
65

66 67 68 69 70
	void *cpuaddr;
	dma_addr_t handle;
	struct nvkm_mm_node r;
};

71 72 73 74
/*
 * Used for objects flattened using the IOMMU API
 */
struct gk20a_instobj_iommu {
B
Ben Skeggs 已提交
75
	struct gk20a_instobj base;
76 77 78 79 80

	/* array of base.mem->size pages */
	struct page *pages[];
};

B
Ben Skeggs 已提交
81
struct gk20a_instmem {
82 83 84
	struct nvkm_instmem base;
	spinlock_t lock;
	u64 addr;
85 86 87 88 89 90 91 92

	/* Only used if IOMMU if present */
	struct mutex *mm_mutex;
	struct nvkm_mm *mm;
	struct iommu_domain *domain;
	unsigned long iommu_pgshift;

	/* Only used by DMA API */
93
	struct dma_attrs attrs;
94 95
};

96 97 98 99 100 101 102 103 104
/*
 * Use PRAMIN to read/write data and avoid coherency issues.
 * PRAMIN uses the GPU path and ensures data will always be coherent.
 *
 * A dynamic mapping based solution would be desirable in the future, but
 * the issue remains of how to maintain coherency efficiently. On ARM it is
 * not easy (if possible at all?) to create uncached temporary mappings.
 */

105 106 107
static u32
gk20a_instobj_rd32(struct nvkm_object *object, u64 offset)
{
B
Ben Skeggs 已提交
108 109
	struct gk20a_instmem *imem = (void *)nvkm_instmem(object);
	struct gk20a_instobj *node = (void *)object;
110
	struct nvkm_device *device = imem->base.subdev.device;
111 112 113 114 115
	unsigned long flags;
	u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
	u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
	u32 data;

B
Ben Skeggs 已提交
116 117
	spin_lock_irqsave(&imem->lock, flags);
	if (unlikely(imem->addr != base)) {
118
		nvkm_wr32(device, 0x001700, base >> 16);
B
Ben Skeggs 已提交
119
		imem->addr = base;
120
	}
121
	data = nvkm_rd32(device, 0x700000 + addr);
B
Ben Skeggs 已提交
122
	spin_unlock_irqrestore(&imem->lock, flags);
123 124 125 126 127 128
	return data;
}

static void
gk20a_instobj_wr32(struct nvkm_object *object, u64 offset, u32 data)
{
B
Ben Skeggs 已提交
129 130
	struct gk20a_instmem *imem = (void *)nvkm_instmem(object);
	struct gk20a_instobj *node = (void *)object;
131
	struct nvkm_device *device = imem->base.subdev.device;
132 133 134 135
	unsigned long flags;
	u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
	u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;

B
Ben Skeggs 已提交
136 137
	spin_lock_irqsave(&imem->lock, flags);
	if (unlikely(imem->addr != base)) {
138
		nvkm_wr32(device, 0x001700, base >> 16);
B
Ben Skeggs 已提交
139
		imem->addr = base;
140
	}
141
	nvkm_wr32(device, 0x700000 + addr, data);
B
Ben Skeggs 已提交
142
	spin_unlock_irqrestore(&imem->lock, flags);
143 144 145
}

static void
B
Ben Skeggs 已提交
146
gk20a_instobj_dtor_dma(struct gk20a_instobj *_node)
147
{
148
	struct gk20a_instobj_dma *node = (void *)_node;
B
Ben Skeggs 已提交
149 150
	struct gk20a_instmem *imem = (void *)nvkm_instmem(node);
	struct device *dev = nv_device_base(nv_device(imem));
151

152
	if (unlikely(!node->cpuaddr))
153 154
		return;

155
	dma_free_attrs(dev, _node->mem->size << PAGE_SHIFT, node->cpuaddr,
B
Ben Skeggs 已提交
156
		       node->handle, &imem->attrs);
157 158 159
}

static void
B
Ben Skeggs 已提交
160
gk20a_instobj_dtor_iommu(struct gk20a_instobj *_node)
161 162
{
	struct gk20a_instobj_iommu *node = (void *)_node;
B
Ben Skeggs 已提交
163
	struct gk20a_instmem *imem = (void *)nvkm_instmem(node);
164 165 166 167 168 169 170 171 172 173
	struct nvkm_mm_node *r;
	int i;

	if (unlikely(list_empty(&_node->mem->regions)))
		return;

	r = list_first_entry(&_node->mem->regions, struct nvkm_mm_node,
			     rl_entry);

	/* clear bit 34 to unmap pages */
B
Ben Skeggs 已提交
174
	r->offset &= ~BIT(34 - imem->iommu_pgshift);
175 176 177

	/* Unmap pages from GPU address space and free them */
	for (i = 0; i < _node->mem->size; i++) {
B
Ben Skeggs 已提交
178 179
		iommu_unmap(imem->domain,
			    (r->offset + i) << imem->iommu_pgshift, PAGE_SIZE);
180 181 182 183
		__free_page(node->pages[i]);
	}

	/* Release area from GPU address space */
B
Ben Skeggs 已提交
184 185 186
	mutex_lock(imem->mm_mutex);
	nvkm_mm_free(imem->mm, &r);
	mutex_unlock(imem->mm_mutex);
187 188 189 190 191
}

static void
gk20a_instobj_dtor(struct nvkm_object *object)
{
B
Ben Skeggs 已提交
192 193
	struct gk20a_instobj *node = (void *)object;
	struct gk20a_instmem *imem = (void *)nvkm_instmem(node);
194

B
Ben Skeggs 已提交
195
	if (imem->domain)
196 197 198
		gk20a_instobj_dtor_iommu(node);
	else
		gk20a_instobj_dtor_dma(node);
199 200 201 202 203

	nvkm_instobj_destroy(&node->base);
}

static int
204 205
gk20a_instobj_ctor_dma(struct nvkm_object *parent, struct nvkm_object *engine,
		       struct nvkm_oclass *oclass, u32 npages, u32 align,
B
Ben Skeggs 已提交
206
		       struct gk20a_instobj **_node)
207
{
208
	struct gk20a_instobj_dma *node;
B
Ben Skeggs 已提交
209
	struct gk20a_instmem *imem = (void *)nvkm_instmem(parent);
210
	struct device *dev = nv_device_base(nv_device(parent));
211 212 213
	int ret;

	ret = nvkm_instobj_create_(parent, engine, oclass, sizeof(*node),
214 215
				   (void **)&node);
	*_node = &node->base;
216 217 218
	if (ret)
		return ret;

219 220
	node->cpuaddr = dma_alloc_attrs(dev, npages << PAGE_SHIFT,
					&node->handle, GFP_KERNEL,
B
Ben Skeggs 已提交
221
					&imem->attrs);
222
	if (!node->cpuaddr) {
B
Ben Skeggs 已提交
223
		nv_error(imem, "cannot allocate DMA memory\n");
224 225 226 227 228
		return -ENOMEM;
	}

	/* alignment check */
	if (unlikely(node->handle & (align - 1)))
B
Ben Skeggs 已提交
229
		nv_warn(imem, "memory not aligned as requested: %pad (0x%x)\n",
230 231
			&node->handle, align);

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	/* present memory for being mapped using small pages */
	node->r.type = 12;
	node->r.offset = node->handle >> 12;
	node->r.length = (npages << PAGE_SHIFT) >> 12;

	node->base._mem.offset = node->handle;

	INIT_LIST_HEAD(&node->base._mem.regions);
	list_add_tail(&node->r.rl_entry, &node->base._mem.regions);

	return 0;
}

static int
gk20a_instobj_ctor_iommu(struct nvkm_object *parent, struct nvkm_object *engine,
			 struct nvkm_oclass *oclass, u32 npages, u32 align,
B
Ben Skeggs 已提交
248
			 struct gk20a_instobj **_node)
249 250
{
	struct gk20a_instobj_iommu *node;
B
Ben Skeggs 已提交
251
	struct gk20a_instmem *imem = (void *)nvkm_instmem(parent);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	struct nvkm_mm_node *r;
	int ret;
	int i;

	ret = nvkm_instobj_create_(parent, engine, oclass,
				sizeof(*node) + sizeof(node->pages[0]) * npages,
				(void **)&node);
	*_node = &node->base;
	if (ret)
		return ret;

	/* Allocate backing memory */
	for (i = 0; i < npages; i++) {
		struct page *p = alloc_page(GFP_KERNEL);

		if (p == NULL) {
			ret = -ENOMEM;
			goto free_pages;
		}
		node->pages[i] = p;
	}

B
Ben Skeggs 已提交
274
	mutex_lock(imem->mm_mutex);
275
	/* Reserve area from GPU address space */
B
Ben Skeggs 已提交
276 277 278
	ret = nvkm_mm_head(imem->mm, 0, 1, npages, npages,
			   align >> imem->iommu_pgshift, &r);
	mutex_unlock(imem->mm_mutex);
279
	if (ret) {
B
Ben Skeggs 已提交
280
		nv_error(imem, "virtual space is full!\n");
281 282 283 284 285 286
		goto free_pages;
	}

	/* Map into GPU address space */
	for (i = 0; i < npages; i++) {
		struct page *p = node->pages[i];
B
Ben Skeggs 已提交
287
		u32 offset = (r->offset + i) << imem->iommu_pgshift;
288

B
Ben Skeggs 已提交
289
		ret = iommu_map(imem->domain, offset, page_to_phys(p),
290 291
				PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
		if (ret < 0) {
B
Ben Skeggs 已提交
292
			nv_error(imem, "IOMMU mapping failure: %d\n", ret);
293 294 295

			while (i-- > 0) {
				offset -= PAGE_SIZE;
B
Ben Skeggs 已提交
296
				iommu_unmap(imem->domain, offset, PAGE_SIZE);
297 298 299 300 301 302
			}
			goto release_area;
		}
	}

	/* Bit 34 tells that an address is to be resolved through the IOMMU */
B
Ben Skeggs 已提交
303
	r->offset |= BIT(34 - imem->iommu_pgshift);
304

B
Ben Skeggs 已提交
305
	node->base._mem.offset = ((u64)r->offset) << imem->iommu_pgshift;
306 307 308 309 310 311 312

	INIT_LIST_HEAD(&node->base._mem.regions);
	list_add_tail(&r->rl_entry, &node->base._mem.regions);

	return 0;

release_area:
B
Ben Skeggs 已提交
313 314 315
	mutex_lock(imem->mm_mutex);
	nvkm_mm_free(imem->mm, &r);
	mutex_unlock(imem->mm_mutex);
316 317 318 319 320 321 322 323 324 325 326 327 328 329

free_pages:
	for (i = 0; i < npages && node->pages[i] != NULL; i++)
		__free_page(node->pages[i]);

	return ret;
}

static int
gk20a_instobj_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		   struct nvkm_oclass *oclass, void *data, u32 _size,
		   struct nvkm_object **pobject)
{
	struct nvkm_instobj_args *args = data;
B
Ben Skeggs 已提交
330 331
	struct gk20a_instmem *imem = (void *)nvkm_instmem(parent);
	struct gk20a_instobj *node;
332 333 334 335
	u32 size, align;
	int ret;

	nv_debug(parent, "%s (%s): size: %x align: %x\n", __func__,
B
Ben Skeggs 已提交
336
		 imem->domain ? "IOMMU" : "DMA", args->size, args->align);
337 338 339 340 341

	/* Round size and align to page bounds */
	size = max(roundup(args->size, PAGE_SIZE), PAGE_SIZE);
	align = max(roundup(args->align, PAGE_SIZE), PAGE_SIZE);

B
Ben Skeggs 已提交
342
	if (imem->domain)
343 344 345 346 347 348 349 350 351 352 353 354
		ret = gk20a_instobj_ctor_iommu(parent, engine, oclass,
					      size >> PAGE_SHIFT, align, &node);
	else
		ret = gk20a_instobj_ctor_dma(parent, engine, oclass,
					     size >> PAGE_SHIFT, align, &node);
	*pobject = nv_object(node);
	if (ret)
		return ret;

	node->mem = &node->_mem;

	/* present memory for being mapped using small pages */
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	node->mem->size = size >> 12;
	node->mem->memtype = 0;
	node->mem->page_shift = 12;

	node->base.addr = node->mem->offset;
	node->base.size = size;

	nv_debug(parent, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
		 size, align, node->mem->offset);

	return 0;
}

static struct nvkm_instobj_impl
gk20a_instobj_oclass = {
	.base.ofuncs = &(struct nvkm_ofuncs) {
		.ctor = gk20a_instobj_ctor,
		.dtor = gk20a_instobj_dtor,
		.init = _nvkm_instobj_init,
		.fini = _nvkm_instobj_fini,
		.rd32 = gk20a_instobj_rd32,
		.wr32 = gk20a_instobj_wr32,
	},
};



static int
gk20a_instmem_fini(struct nvkm_object *object, bool suspend)
{
B
Ben Skeggs 已提交
385 386 387
	struct gk20a_instmem *imem = (void *)object;
	imem->addr = ~0ULL;
	return nvkm_instmem_fini(&imem->base, suspend);
388 389 390 391 392 393 394
}

static int
gk20a_instmem_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		   struct nvkm_oclass *oclass, void *data, u32 size,
		   struct nvkm_object **pobject)
{
B
Ben Skeggs 已提交
395
	struct gk20a_instmem *imem;
396
	struct nouveau_platform_device *plat;
397 398
	int ret;

B
Ben Skeggs 已提交
399 400
	ret = nvkm_instmem_create(parent, engine, oclass, &imem);
	*pobject = nv_object(imem);
401 402 403
	if (ret)
		return ret;

B
Ben Skeggs 已提交
404
	spin_lock_init(&imem->lock);
405

406 407
	plat = nv_device_to_platform(nv_device(parent));
	if (plat->gpu->iommu.domain) {
B
Ben Skeggs 已提交
408 409 410 411
		imem->domain = plat->gpu->iommu.domain;
		imem->mm = plat->gpu->iommu.mm;
		imem->iommu_pgshift = plat->gpu->iommu.pgshift;
		imem->mm_mutex = &plat->gpu->iommu.mutex;
412

B
Ben Skeggs 已提交
413
		nv_info(imem, "using IOMMU\n");
414
	} else {
B
Ben Skeggs 已提交
415
		init_dma_attrs(&imem->attrs);
416 417 418 419
		/*
		 * We will access instmem through PRAMIN and thus do not need a
		 * consistent CPU pointer or kernel mapping
		 */
B
Ben Skeggs 已提交
420 421 422 423
		dma_set_attr(DMA_ATTR_NON_CONSISTENT, &imem->attrs);
		dma_set_attr(DMA_ATTR_WEAK_ORDERING, &imem->attrs);
		dma_set_attr(DMA_ATTR_WRITE_COMBINE, &imem->attrs);
		dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &imem->attrs);
424

B
Ben Skeggs 已提交
425
		nv_info(imem, "using DMA API\n");
426
	}
427

428 429 430 431 432 433 434 435 436 437 438 439 440 441
	return 0;
}

struct nvkm_oclass *
gk20a_instmem_oclass = &(struct nvkm_instmem_impl) {
	.base.handle = NV_SUBDEV(INSTMEM, 0xea),
	.base.ofuncs = &(struct nvkm_ofuncs) {
		.ctor = gk20a_instmem_ctor,
		.dtor = _nvkm_instmem_dtor,
		.init = _nvkm_instmem_init,
		.fini = gk20a_instmem_fini,
	},
	.instobj = &gk20a_instobj_oclass.base,
}.base;