gk20a.c 16.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
/*
 * 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
26 27
 * implementation must be done directly on top of system memory, while
 * preserving coherency for read and write operations.
28 29
 *
 * Instmem can be allocated through two means:
30
 * 1) If an IOMMU unit has been probed, the IOMMU API is used to make memory
31
 *    pages contiguous to the GPU. This is the preferred way.
32
 * 2) If no IOMMU unit is probed, the DMA API is used to allocate physically
33 34
 *    contiguous memory.
 *
35 36 37 38 39 40 41 42
 * In both cases CPU read and writes are performed by creating a write-combined
 * mapping. The GPU L2 cache must thus be flushed/invalidated when required. To
 * be conservative we do this every time we acquire or release an instobj, but
 * ideally L2 management should be handled at a higher level.
 *
 * To improve performance, CPU mappings are not removed upon instobj release.
 * Instead they are placed into a LRU list to be recycled when the mapped space
 * goes beyond a certain threshold. At the moment this limit is 1MB.
43
 */
44
#include "priv.h"
45

46
#include <core/memory.h>
47
#include <core/mm.h>
48
#include <core/tegra.h>
49
#include <subdev/fb.h>
50
#include <subdev/ltc.h>
51

B
Ben Skeggs 已提交
52
struct gk20a_instobj {
53 54
	struct nvkm_memory memory;
	struct nvkm_mem mem;
55 56 57 58
	struct gk20a_instmem *imem;

	/* CPU mapping */
	u32 *vaddr;
59
};
60
#define gk20a_instobj(p) container_of((p), struct gk20a_instobj, memory)
61 62 63 64 65

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

68 69 70
	dma_addr_t handle;
	struct nvkm_mm_node r;
};
71 72
#define gk20a_instobj_dma(p) \
	container_of(gk20a_instobj(p), struct gk20a_instobj_dma, base)
73

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

80 81 82 83 84
	/* to link into gk20a_instmem::vaddr_lru */
	struct list_head vaddr_node;
	/* how many clients are using vaddr? */
	u32 use_cpt;

85 86 87
	/* will point to the higher half of pages */
	dma_addr_t *dma_addrs;
	/* array of base.mem->size pages (+ dma_addr_ts) */
88 89
	struct page *pages[];
};
90 91
#define gk20a_instobj_iommu(p) \
	container_of(gk20a_instobj(p), struct gk20a_instobj_iommu, base)
92

B
Ben Skeggs 已提交
93
struct gk20a_instmem {
94
	struct nvkm_instmem base;
95 96

	/* protects vaddr_* and gk20a_instobj::vaddr* */
97
	struct mutex lock;
98 99 100 101 102

	/* CPU mappings LRU */
	unsigned int vaddr_use;
	unsigned int vaddr_max;
	struct list_head vaddr_lru;
103 104 105 106 107 108

	/* Only used if IOMMU if present */
	struct mutex *mm_mutex;
	struct nvkm_mm *mm;
	struct iommu_domain *domain;
	unsigned long iommu_pgshift;
109
	u16 iommu_bit;
110 111

	/* Only used by DMA API */
112
	unsigned long attrs;
113
};
114
#define gk20a_instmem(p) container_of((p), struct gk20a_instmem, base)
115

116 117 118
static enum nvkm_memory_target
gk20a_instobj_target(struct nvkm_memory *memory)
{
119
	return NVKM_MEM_TARGET_NCOH;
120 121
}

122 123 124 125 126 127
static u8
gk20a_instobj_page(struct nvkm_memory *memory)
{
	return 12;
}

128 129 130 131 132 133 134 135 136 137 138 139
static u64
gk20a_instobj_addr(struct nvkm_memory *memory)
{
	return gk20a_instobj(memory)->mem.offset;
}

static u64
gk20a_instobj_size(struct nvkm_memory *memory)
{
	return (u64)gk20a_instobj(memory)->mem.size << 12;
}

140
/*
141 142 143
 * Recycle the vaddr of obj. Must be called with gk20a_instmem::lock held.
 */
static void
144
gk20a_instobj_iommu_recycle_vaddr(struct gk20a_instobj_iommu *obj)
145
{
146
	struct gk20a_instmem *imem = obj->base.imem;
147 148 149
	/* there should not be any user left... */
	WARN_ON(obj->use_cpt);
	list_del(&obj->vaddr_node);
150 151 152
	vunmap(obj->base.vaddr);
	obj->base.vaddr = NULL;
	imem->vaddr_use -= nvkm_memory_size(&obj->base.memory);
153 154 155 156 157 158
	nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n", imem->vaddr_use,
		   imem->vaddr_max);
}

/*
 * Must be called while holding gk20a_instmem::lock
159 160 161 162 163 164 165 166 167
 */
static void
gk20a_instmem_vaddr_gc(struct gk20a_instmem *imem, const u64 size)
{
	while (imem->vaddr_use + size > imem->vaddr_max) {
		/* no candidate that can be unmapped, abort... */
		if (list_empty(&imem->vaddr_lru))
			break;

168 169 170
		gk20a_instobj_iommu_recycle_vaddr(
				list_first_entry(&imem->vaddr_lru,
				struct gk20a_instobj_iommu, vaddr_node));
171 172 173
	}
}

174
static void __iomem *
175
gk20a_instobj_acquire_dma(struct nvkm_memory *memory)
176
{
177 178 179
	struct gk20a_instobj *node = gk20a_instobj(memory);
	struct gk20a_instmem *imem = node->imem;
	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
180 181 182 183 184 185 186 187 188 189 190 191

	nvkm_ltc_flush(ltc);

	return node->vaddr;
}

static void __iomem *
gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
{
	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
	struct gk20a_instmem *imem = node->base.imem;
	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
192 193 194 195
	const u64 size = nvkm_memory_size(memory);

	nvkm_ltc_flush(ltc);

196
	mutex_lock(&imem->lock);
197

198
	if (node->base.vaddr) {
199 200 201 202
		if (!node->use_cpt) {
			/* remove from LRU list since mapping in use again */
			list_del(&node->vaddr_node);
		}
203 204 205 206 207 208
		goto out;
	}

	/* try to free some address space if we reached the limit */
	gk20a_instmem_vaddr_gc(imem, size);

209 210 211 212
	/* map the pages */
	node->base.vaddr = vmap(node->pages, size >> PAGE_SHIFT, VM_MAP,
				pgprot_writecombine(PAGE_KERNEL));
	if (!node->base.vaddr) {
213 214 215 216 217 218 219 220 221 222
		nvkm_error(&imem->base.subdev, "cannot map instobj - "
			   "this is not going to end well...\n");
		goto out;
	}

	imem->vaddr_use += size;
	nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n",
		   imem->vaddr_use, imem->vaddr_max);

out:
223
	node->use_cpt++;
224
	mutex_unlock(&imem->lock);
225

226
	return node->base.vaddr;
227 228 229
}

static void
230
gk20a_instobj_release_dma(struct nvkm_memory *memory)
231
{
232 233 234
	struct gk20a_instobj *node = gk20a_instobj(memory);
	struct gk20a_instmem *imem = node->imem;
	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
235

236 237
	/* in case we got a write-combined mapping */
	wmb();
238 239 240 241 242 243 244 245 246
	nvkm_ltc_invalidate(ltc);
}

static void
gk20a_instobj_release_iommu(struct nvkm_memory *memory)
{
	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
	struct gk20a_instmem *imem = node->base.imem;
	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
247

248
	mutex_lock(&imem->lock);
249

250 251 252 253 254 255 256
	/* we should at least have one user to release... */
	if (WARN_ON(node->use_cpt == 0))
		goto out;

	/* add unused objs to the LRU list to recycle their mapping */
	if (--node->use_cpt == 0)
		list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
257

258
out:
259
	mutex_unlock(&imem->lock);
260 261 262 263

	wmb();
	nvkm_ltc_invalidate(ltc);
}
264

265
static u32
266
gk20a_instobj_rd32(struct nvkm_memory *memory, u64 offset)
267
{
268
	struct gk20a_instobj *node = gk20a_instobj(memory);
269 270

	return node->vaddr[offset / 4];
271 272 273
}

static void
274
gk20a_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
275
{
276
	struct gk20a_instobj *node = gk20a_instobj(memory);
277

278
	node->vaddr[offset / 4] = data;
279 280
}

281 282 283
static int
gk20a_instobj_map(struct nvkm_memory *memory, u64 offset, struct nvkm_vmm *vmm,
		  struct nvkm_vma *vma, void *argv, u32 argc)
284 285
{
	struct gk20a_instobj *node = gk20a_instobj(memory);
286 287
	nvkm_vm_map_at(vma, 0, &node->mem);
	return 0;
288 289
}

290 291
static void *
gk20a_instobj_dtor_dma(struct nvkm_memory *memory)
292
{
293 294
	struct gk20a_instobj_dma *node = gk20a_instobj_dma(memory);
	struct gk20a_instmem *imem = node->base.imem;
295
	struct device *dev = imem->base.subdev.device->dev;
296

297
	if (unlikely(!node->base.vaddr))
298
		goto out;
299

300
	dma_free_attrs(dev, node->base.mem.size << PAGE_SHIFT, node->base.vaddr,
301
		       node->handle, imem->attrs);
302 303 304

out:
	return node;
305 306
}

307 308
static void *
gk20a_instobj_dtor_iommu(struct nvkm_memory *memory)
309
{
310 311 312
	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
	struct gk20a_instmem *imem = node->base.imem;
	struct device *dev = imem->base.subdev.device->dev;
313
	struct nvkm_mm_node *r = node->base.mem.mem;
314 315
	int i;

316
	if (unlikely(!r))
317 318
		goto out;

319
	mutex_lock(&imem->lock);
320 321 322 323 324

	/* vaddr has already been recycled */
	if (node->base.vaddr)
		gk20a_instobj_iommu_recycle_vaddr(node);

325
	mutex_unlock(&imem->lock);
326

327 328
	/* clear IOMMU bit to unmap pages */
	r->offset &= ~BIT(imem->iommu_bit - imem->iommu_pgshift);
329 330

	/* Unmap pages from GPU address space and free them */
331
	for (i = 0; i < node->base.mem.size; i++) {
B
Ben Skeggs 已提交
332 333
		iommu_unmap(imem->domain,
			    (r->offset + i) << imem->iommu_pgshift, PAGE_SIZE);
334 335
		dma_unmap_page(dev, node->dma_addrs[i], PAGE_SIZE,
			       DMA_BIDIRECTIONAL);
336 337 338 339
		__free_page(node->pages[i]);
	}

	/* Release area from GPU address space */
B
Ben Skeggs 已提交
340 341 342
	mutex_lock(imem->mm_mutex);
	nvkm_mm_free(imem->mm, &r);
	mutex_unlock(imem->mm_mutex);
343

344
out:
345
	return node;
346 347
}

348
static const struct nvkm_memory_func
349 350 351
gk20a_instobj_func_dma = {
	.dtor = gk20a_instobj_dtor_dma,
	.target = gk20a_instobj_target,
352
	.page = gk20a_instobj_page,
353 354
	.addr = gk20a_instobj_addr,
	.size = gk20a_instobj_size,
355 356
	.acquire = gk20a_instobj_acquire_dma,
	.release = gk20a_instobj_release_dma,
357 358 359 360 361 362
	.map = gk20a_instobj_map,
};

static const struct nvkm_memory_func
gk20a_instobj_func_iommu = {
	.dtor = gk20a_instobj_dtor_iommu,
363
	.target = gk20a_instobj_target,
364
	.page = gk20a_instobj_page,
365 366
	.addr = gk20a_instobj_addr,
	.size = gk20a_instobj_size,
367 368
	.acquire = gk20a_instobj_acquire_iommu,
	.release = gk20a_instobj_release_iommu,
369 370 371 372 373
	.map = gk20a_instobj_map,
};

static const struct nvkm_memory_ptrs
gk20a_instobj_ptrs = {
374 375 376 377
	.rd32 = gk20a_instobj_rd32,
	.wr32 = gk20a_instobj_wr32,
};

378
static int
379
gk20a_instobj_ctor_dma(struct gk20a_instmem *imem, u32 npages, u32 align,
B
Ben Skeggs 已提交
380
		       struct gk20a_instobj **_node)
381
{
382
	struct gk20a_instobj_dma *node;
383
	struct nvkm_subdev *subdev = &imem->base.subdev;
384
	struct device *dev = subdev->device->dev;
385

386 387
	if (!(node = kzalloc(sizeof(*node), GFP_KERNEL)))
		return -ENOMEM;
388
	*_node = &node->base;
389

390
	nvkm_memory_ctor(&gk20a_instobj_func_dma, &node->base.memory);
391
	node->base.memory.ptrs = &gk20a_instobj_ptrs;
392

393 394
	node->base.vaddr = dma_alloc_attrs(dev, npages << PAGE_SHIFT,
					   &node->handle, GFP_KERNEL,
395
					   imem->attrs);
396
	if (!node->base.vaddr) {
397
		nvkm_error(subdev, "cannot allocate DMA memory\n");
398 399 400 401 402
		return -ENOMEM;
	}

	/* alignment check */
	if (unlikely(node->handle & (align - 1)))
403 404 405
		nvkm_warn(subdev,
			  "memory not aligned as requested: %pad (0x%x)\n",
			  &node->handle, align);
406

407 408 409 410 411
	/* 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;

412
	node->base.mem.offset = node->handle;
413
	node->base.mem.mem = &node->r;
414 415 416 417
	return 0;
}

static int
418
gk20a_instobj_ctor_iommu(struct gk20a_instmem *imem, u32 npages, u32 align,
B
Ben Skeggs 已提交
419
			 struct gk20a_instobj **_node)
420 421
{
	struct gk20a_instobj_iommu *node;
422
	struct nvkm_subdev *subdev = &imem->base.subdev;
423
	struct device *dev = subdev->device->dev;
424 425 426 427
	struct nvkm_mm_node *r;
	int ret;
	int i;

428 429 430 431 432 433
	/*
	 * despite their variable size, instmem allocations are small enough
	 * (< 1 page) to be handled by kzalloc
	 */
	if (!(node = kzalloc(sizeof(*node) + ((sizeof(node->pages[0]) +
			     sizeof(*node->dma_addrs)) * npages), GFP_KERNEL)))
434
		return -ENOMEM;
435
	*_node = &node->base;
436 437 438
	node->dma_addrs = (void *)(node->pages + npages);

	nvkm_memory_ctor(&gk20a_instobj_func_iommu, &node->base.memory);
439
	node->base.memory.ptrs = &gk20a_instobj_ptrs;
440 441 442 443

	/* Allocate backing memory */
	for (i = 0; i < npages; i++) {
		struct page *p = alloc_page(GFP_KERNEL);
444
		dma_addr_t dma_adr;
445 446 447 448 449 450

		if (p == NULL) {
			ret = -ENOMEM;
			goto free_pages;
		}
		node->pages[i] = p;
451 452 453 454 455 456 457
		dma_adr = dma_map_page(dev, p, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
		if (dma_mapping_error(dev, dma_adr)) {
			nvkm_error(subdev, "DMA mapping error!\n");
			ret = -ENOMEM;
			goto free_pages;
		}
		node->dma_addrs[i] = dma_adr;
458 459
	}

B
Ben Skeggs 已提交
460
	mutex_lock(imem->mm_mutex);
461
	/* Reserve area from GPU address space */
B
Ben Skeggs 已提交
462 463 464
	ret = nvkm_mm_head(imem->mm, 0, 1, npages, npages,
			   align >> imem->iommu_pgshift, &r);
	mutex_unlock(imem->mm_mutex);
465
	if (ret) {
466
		nvkm_error(subdev, "IOMMU space is full!\n");
467 468 469 470 471
		goto free_pages;
	}

	/* Map into GPU address space */
	for (i = 0; i < npages; i++) {
B
Ben Skeggs 已提交
472
		u32 offset = (r->offset + i) << imem->iommu_pgshift;
473

474
		ret = iommu_map(imem->domain, offset, node->dma_addrs[i],
475 476
				PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
		if (ret < 0) {
477
			nvkm_error(subdev, "IOMMU mapping failure: %d\n", ret);
478 479 480

			while (i-- > 0) {
				offset -= PAGE_SIZE;
B
Ben Skeggs 已提交
481
				iommu_unmap(imem->domain, offset, PAGE_SIZE);
482 483 484 485 486
			}
			goto release_area;
		}
	}

487 488
	/* IOMMU bit tells that an address is to be resolved through the IOMMU */
	r->offset |= BIT(imem->iommu_bit - imem->iommu_pgshift);
489

490
	node->base.mem.offset = ((u64)r->offset) << imem->iommu_pgshift;
491
	node->base.mem.mem = r;
492 493 494
	return 0;

release_area:
B
Ben Skeggs 已提交
495 496 497
	mutex_lock(imem->mm_mutex);
	nvkm_mm_free(imem->mm, &r);
	mutex_unlock(imem->mm_mutex);
498 499

free_pages:
500 501 502 503 504
	for (i = 0; i < npages && node->pages[i] != NULL; i++) {
		dma_addr_t dma_addr = node->dma_addrs[i];
		if (dma_addr)
			dma_unmap_page(dev, dma_addr, PAGE_SIZE,
				       DMA_BIDIRECTIONAL);
505
		__free_page(node->pages[i]);
506
	}
507 508 509 510 511

	return ret;
}

static int
512 513
gk20a_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
		  struct nvkm_memory **pmemory)
514
{
515
	struct gk20a_instmem *imem = gk20a_instmem(base);
516
	struct nvkm_subdev *subdev = &imem->base.subdev;
517
	struct gk20a_instobj *node = NULL;
518 519
	int ret;

520
	nvkm_debug(subdev, "%s (%s): size: %x align: %x\n", __func__,
521
		   imem->domain ? "IOMMU" : "DMA", size, align);
522 523

	/* Round size and align to page bounds */
524 525
	size = max(roundup(size, PAGE_SIZE), PAGE_SIZE);
	align = max(roundup(align, PAGE_SIZE), PAGE_SIZE);
526

B
Ben Skeggs 已提交
527
	if (imem->domain)
528 529
		ret = gk20a_instobj_ctor_iommu(imem, size >> PAGE_SHIFT,
					       align, &node);
530
	else
531 532
		ret = gk20a_instobj_ctor_dma(imem, size >> PAGE_SHIFT,
					     align, &node);
533
	*pmemory = node ? &node->memory : NULL;
534 535 536
	if (ret)
		return ret;

537
	node->imem = imem;
538 539

	/* present memory for being mapped using small pages */
540 541
	node->mem.size = size >> 12;
	node->mem.memtype = 0;
542
	node->mem.memory = &node->memory;
543

544
	nvkm_debug(subdev, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
545
		   size, align, node->mem.offset);
546 547 548 549

	return 0;
}

550 551
static void *
gk20a_instmem_dtor(struct nvkm_instmem *base)
552
{
553 554 555 556 557 558 559 560 561 562 563
	struct gk20a_instmem *imem = gk20a_instmem(base);

	/* perform some sanity checks... */
	if (!list_empty(&imem->vaddr_lru))
		nvkm_warn(&base->subdev, "instobj LRU not empty!\n");

	if (imem->vaddr_use != 0)
		nvkm_warn(&base->subdev, "instobj vmap area not empty! "
			  "0x%x bytes still mapped\n", imem->vaddr_use);

	return imem;
564 565
}

566 567
static const struct nvkm_instmem_func
gk20a_instmem = {
568
	.dtor = gk20a_instmem_dtor,
569 570 571 572 573 574
	.memory_new = gk20a_instobj_new,
	.zero = false,
};

int
gk20a_instmem_new(struct nvkm_device *device, int index,
575
		  struct nvkm_instmem **pimem)
576
{
577
	struct nvkm_device_tegra *tdev = device->func->tegra(device);
B
Ben Skeggs 已提交
578
	struct gk20a_instmem *imem;
579

580 581 582
	if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
		return -ENOMEM;
	nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
583
	mutex_init(&imem->lock);
584
	*pimem = &imem->base;
585

586 587 588 589 590
	/* do not allow more than 1MB of CPU-mapped instmem */
	imem->vaddr_use = 0;
	imem->vaddr_max = 0x100000;
	INIT_LIST_HEAD(&imem->vaddr_lru);

591
	if (tdev->iommu.domain) {
592
		imem->mm_mutex = &tdev->iommu.mutex;
593
		imem->mm = &tdev->iommu.mm;
594
		imem->domain = tdev->iommu.domain;
595
		imem->iommu_pgshift = tdev->iommu.pgshift;
596
		imem->iommu_bit = tdev->func->iommu_bit;
597

598
		nvkm_info(&imem->base.subdev, "using IOMMU\n");
599
	} else {
600 601 602
		imem->attrs = DMA_ATTR_NON_CONSISTENT |
			      DMA_ATTR_WEAK_ORDERING |
			      DMA_ATTR_WRITE_COMBINE;
603

604
		nvkm_info(&imem->base.subdev, "using DMA API\n");
605
	}
606

607 608
	return 0;
}