gk20a.c 12.3 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 nvkm_subdev *subdev = &imem->base.subdev;
211
	struct device *dev = nv_device_base(nv_device(parent));
212 213 214
	int ret;

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

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

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

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	/* 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 已提交
250
			 struct gk20a_instobj **_node)
251 252
{
	struct gk20a_instobj_iommu *node;
B
Ben Skeggs 已提交
253
	struct gk20a_instmem *imem = (void *)nvkm_instmem(parent);
254
	struct nvkm_subdev *subdev = &imem->base.subdev;
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	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 已提交
277
	mutex_lock(imem->mm_mutex);
278
	/* Reserve area from GPU address space */
B
Ben Skeggs 已提交
279 280 281
	ret = nvkm_mm_head(imem->mm, 0, 1, npages, npages,
			   align >> imem->iommu_pgshift, &r);
	mutex_unlock(imem->mm_mutex);
282
	if (ret) {
283
		nvkm_error(subdev, "virtual space is full!\n");
284 285 286 287 288 289
		goto free_pages;
	}

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

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

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

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

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

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

	return 0;

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

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 已提交
333 334
	struct gk20a_instmem *imem = (void *)nvkm_instmem(parent);
	struct gk20a_instobj *node;
335
	struct nvkm_subdev *subdev = &imem->base.subdev;
336 337 338
	u32 size, align;
	int ret;

339 340
	nvkm_debug(subdev, "%s (%s): size: %x align: %x\n", __func__,
		   imem->domain ? "IOMMU" : "DMA", args->size, args->align);
341 342 343 344 345

	/* 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 已提交
346
	if (imem->domain)
347 348 349 350 351 352 353 354 355 356 357 358
		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 */
359 360 361 362 363 364 365
	node->mem->size = size >> 12;
	node->mem->memtype = 0;
	node->mem->page_shift = 12;

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

366 367
	nvkm_debug(subdev, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
		   size, align, node->mem->offset);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

	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 已提交
389 390 391
	struct gk20a_instmem *imem = (void *)object;
	imem->addr = ~0ULL;
	return nvkm_instmem_fini(&imem->base, suspend);
392 393 394 395 396 397 398
}

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 已提交
399
	struct gk20a_instmem *imem;
400
	struct nouveau_platform_device *plat;
401 402
	int ret;

B
Ben Skeggs 已提交
403 404
	ret = nvkm_instmem_create(parent, engine, oclass, &imem);
	*pobject = nv_object(imem);
405 406 407
	if (ret)
		return ret;

B
Ben Skeggs 已提交
408
	spin_lock_init(&imem->lock);
409

410 411
	plat = nv_device_to_platform(nv_device(parent));
	if (plat->gpu->iommu.domain) {
B
Ben Skeggs 已提交
412 413 414 415
		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;
416

417
		nvkm_info(&imem->base.subdev, "using IOMMU\n");
418
	} else {
B
Ben Skeggs 已提交
419
		init_dma_attrs(&imem->attrs);
420 421 422 423
		/*
		 * We will access instmem through PRAMIN and thus do not need a
		 * consistent CPU pointer or kernel mapping
		 */
B
Ben Skeggs 已提交
424 425 426 427
		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);
428

429
		nvkm_info(&imem->base.subdev, "using DMA API\n");
430
	}
431

432 433 434 435 436 437 438 439 440 441 442 443 444 445
	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;