job.c 14.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4
/*
 * Tegra host1x Job
 *
5
 * Copyright (c) 2010-2015, NVIDIA Corporation.
6 7 8 9
 */

#include <linux/dma-mapping.h>
#include <linux/err.h>
10
#include <linux/host1x.h>
11
#include <linux/iommu.h>
12 13 14 15 16 17 18 19 20 21 22 23
#include <linux/kref.h>
#include <linux/module.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <trace/events/host1x.h>

#include "channel.h"
#include "dev.h"
#include "job.h"
#include "syncpt.h"

24 25
#define HOST1X_WAIT_SYNCPT_OFFSET 0x8

26
struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
27
				    u32 num_cmdbufs, u32 num_relocs)
28 29
{
	struct host1x_job *job = NULL;
30
	unsigned int num_unpins = num_relocs;
31 32 33
	u64 total;
	void *mem;

34 35 36
	if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
		num_unpins += num_cmdbufs;

37 38
	/* Check that we're not going to overflow */
	total = sizeof(struct host1x_job) +
39 40 41 42 43
		(u64)num_relocs * sizeof(struct host1x_reloc) +
		(u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
		(u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
		(u64)num_unpins * sizeof(dma_addr_t) +
		(u64)num_unpins * sizeof(u32 *);
44 45 46 47 48 49 50 51 52 53 54 55
	if (total > ULONG_MAX)
		return NULL;

	mem = job = kzalloc(total, GFP_KERNEL);
	if (!job)
		return NULL;

	kref_init(&job->ref);
	job->channel = ch;

	/* Redistribute memory to the structs  */
	mem += sizeof(struct host1x_job);
56
	job->relocs = num_relocs ? mem : NULL;
57 58 59 60 61 62 63 64 65 66 67 68
	mem += num_relocs * sizeof(struct host1x_reloc);
	job->unpins = num_unpins ? mem : NULL;
	mem += num_unpins * sizeof(struct host1x_job_unpin_data);
	job->gathers = num_cmdbufs ? mem : NULL;
	mem += num_cmdbufs * sizeof(struct host1x_job_gather);
	job->addr_phys = num_unpins ? mem : NULL;

	job->reloc_addr_phys = job->addr_phys;
	job->gather_addr_phys = &job->addr_phys[num_relocs];

	return job;
}
69
EXPORT_SYMBOL(host1x_job_alloc);
70 71 72 73 74 75

struct host1x_job *host1x_job_get(struct host1x_job *job)
{
	kref_get(&job->ref);
	return job;
}
76
EXPORT_SYMBOL(host1x_job_get);
77 78 79 80 81 82 83 84 85 86 87 88

static void job_free(struct kref *ref)
{
	struct host1x_job *job = container_of(ref, struct host1x_job, ref);

	kfree(job);
}

void host1x_job_put(struct host1x_job *job)
{
	kref_put(&job->ref, job_free);
}
89
EXPORT_SYMBOL(host1x_job_put);
90 91

void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
92
			   unsigned int words, unsigned int offset)
93
{
94 95 96 97 98
	struct host1x_job_gather *gather = &job->gathers[job->num_gathers];

	gather->words = words;
	gather->bo = bo;
	gather->offset = offset;
99 100 101

	job->num_gathers++;
}
102
EXPORT_SYMBOL(host1x_job_add_gather);
103

104
static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
105
{
106 107
	struct host1x_client *client = job->client;
	struct device *dev = client->dev;
108
	struct host1x_job_gather *g;
109
	struct iommu_domain *domain;
110
	unsigned int i;
111
	int err;
112

113
	domain = iommu_get_domain_for_dev(dev);
114 115 116
	job->num_unpins = 0;

	for (i = 0; i < job->num_relocs; i++) {
117
		struct host1x_reloc *reloc = &job->relocs[i];
118
		dma_addr_t phys_addr, *phys;
119 120
		struct sg_table *sgt;

121
		reloc->target.bo = host1x_bo_get(reloc->target.bo);
122 123
		if (!reloc->target.bo) {
			err = -EINVAL;
124
			goto unpin;
125
		}
126

127 128 129 130 131 132 133 134 135 136 137 138 139
		/*
		 * If the client device is not attached to an IOMMU, the
		 * physical address of the buffer object can be used.
		 *
		 * Similarly, when an IOMMU domain is shared between all
		 * host1x clients, the IOVA is already available, so no
		 * need to map the buffer object again.
		 *
		 * XXX Note that this isn't always safe to do because it
		 * relies on an assumption that no cache maintenance is
		 * needed on the buffer objects.
		 */
		if (!domain || client->group)
140 141 142 143 144
			phys = &phys_addr;
		else
			phys = NULL;

		sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
145 146 147 148
		if (IS_ERR(sgt)) {
			err = PTR_ERR(sgt);
			goto unpin;
		}
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
		if (sgt) {
			unsigned long mask = HOST1X_RELOC_READ |
					     HOST1X_RELOC_WRITE;
			enum dma_data_direction dir;

			switch (reloc->flags & mask) {
			case HOST1X_RELOC_READ:
				dir = DMA_TO_DEVICE;
				break;

			case HOST1X_RELOC_WRITE:
				dir = DMA_FROM_DEVICE;
				break;

			case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
				dir = DMA_BIDIRECTIONAL;
				break;

			default:
				err = -EINVAL;
				goto unpin;
			}

173 174
			err = dma_map_sgtable(dev, sgt, dir, 0);
			if (err)
175 176 177 178 179 180 181
				goto unpin;

			job->unpins[job->num_unpins].dev = dev;
			job->unpins[job->num_unpins].dir = dir;
			phys_addr = sg_dma_address(sgt->sgl);
		}

182
		job->addr_phys[job->num_unpins] = phys_addr;
183
		job->unpins[job->num_unpins].bo = reloc->target.bo;
184 185 186 187
		job->unpins[job->num_unpins].sgt = sgt;
		job->num_unpins++;
	}

188 189 190 191 192 193 194
	/*
	 * We will copy gathers BO content later, so there is no need to
	 * hold and pin them.
	 */
	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
		return 0;

195
	for (i = 0; i < job->num_gathers; i++) {
196 197
		size_t gather_size = 0;
		struct scatterlist *sg;
198 199
		struct sg_table *sgt;
		dma_addr_t phys_addr;
200 201
		unsigned long shift;
		struct iova *alloc;
202
		dma_addr_t *phys;
203
		unsigned int j;
204

205
		g = &job->gathers[i];
206
		g->bo = host1x_bo_get(g->bo);
207 208
		if (!g->bo) {
			err = -EINVAL;
209
			goto unpin;
210
		}
211

212 213 214 215 216 217 218 219 220 221 222
		/**
		 * If the host1x is not attached to an IOMMU, there is no need
		 * to map the buffer object for the host1x, since the physical
		 * address can simply be used.
		 */
		if (!iommu_get_domain_for_dev(host->dev))
			phys = &phys_addr;
		else
			phys = NULL;

		sgt = host1x_bo_pin(host->dev, g->bo, phys);
223 224
		if (IS_ERR(sgt)) {
			err = PTR_ERR(sgt);
225
			goto put;
226
		}
227

228
		if (host->domain) {
229
			for_each_sgtable_sg(sgt, sg, j)
230 231 232 233 234 235 236 237
				gather_size += sg->length;
			gather_size = iova_align(&host->iova, gather_size);

			shift = iova_shift(&host->iova);
			alloc = alloc_iova(&host->iova, gather_size >> shift,
					   host->iova_end >> shift, true);
			if (!alloc) {
				err = -ENOMEM;
238
				goto put;
239 240
			}

241
			err = iommu_map_sgtable(host->domain,
242
					iova_dma_addr(&host->iova, alloc),
243
					sgt, IOMMU_READ);
244 245 246
			if (err == 0) {
				__free_iova(&host->iova, alloc);
				err = -EINVAL;
247
				goto put;
248 249 250
			}

			job->unpins[job->num_unpins].size = gather_size;
251
			phys_addr = iova_dma_addr(&host->iova, alloc);
252
		} else if (sgt) {
253 254
			err = dma_map_sgtable(host->dev, sgt, DMA_TO_DEVICE, 0);
			if (err)
255
				goto put;
256

257
			job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
258 259
			job->unpins[job->num_unpins].dev = host->dev;
			phys_addr = sg_dma_address(sgt->sgl);
260 261
		}

262 263
		job->addr_phys[job->num_unpins] = phys_addr;
		job->gather_addr_phys[i] = phys_addr;
264 265 266 267 268 269

		job->unpins[job->num_unpins].bo = g->bo;
		job->unpins[job->num_unpins].sgt = sgt;
		job->num_unpins++;
	}

270
	return 0;
271

272 273
put:
	host1x_bo_put(g->bo);
274 275
unpin:
	host1x_job_unpin(job);
276
	return err;
277 278
}

279
static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
280
{
281
	void *cmdbuf_addr = NULL;
282
	struct host1x_bo *cmdbuf = g->bo;
283
	unsigned int i;
284 285

	/* pin & patch the relocs for one gather */
286
	for (i = 0; i < job->num_relocs; i++) {
287
		struct host1x_reloc *reloc = &job->relocs[i];
288
		u32 reloc_addr = (job->reloc_addr_phys[i] +
289
				  reloc->target.offset) >> reloc->shift;
290 291 292
		u32 *target;

		/* skip all other gathers */
293
		if (cmdbuf != reloc->cmdbuf.bo)
294 295
			continue;

296 297 298 299 300 301 302
		if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
			target = (u32 *)job->gather_copy_mapped +
					reloc->cmdbuf.offset / sizeof(u32) +
						g->offset / sizeof(u32);
			goto patch_reloc;
		}

303 304
		if (!cmdbuf_addr) {
			cmdbuf_addr = host1x_bo_mmap(cmdbuf);
305

306
			if (unlikely(!cmdbuf_addr)) {
307 308 309 310 311
				pr_err("Could not map cmdbuf for relocation\n");
				return -ENOMEM;
			}
		}

312
		target = cmdbuf_addr + reloc->cmdbuf.offset;
313
patch_reloc:
314 315 316
		*target = reloc_addr;
	}

317 318
	if (cmdbuf_addr)
		host1x_bo_munmap(cmdbuf, cmdbuf_addr);
319 320 321 322

	return 0;
}

323
static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
324
			unsigned int offset)
325 326 327
{
	offset *= sizeof(u32);

328
	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
329
		return false;
330

331 332 333 334
	/* relocation shift value validation isn't implemented yet */
	if (reloc->shift)
		return false;

335
	return true;
336 337 338 339 340 341 342 343 344
}

struct host1x_firewall {
	struct host1x_job *job;
	struct device *dev;

	unsigned int num_relocs;
	struct host1x_reloc *reloc;

345
	struct host1x_bo *cmdbuf;
346 347 348 349 350 351 352 353 354
	unsigned int offset;

	u32 words;
	u32 class;
	u32 reg;
	u32 mask;
	u32 count;
};

355 356
static int check_register(struct host1x_firewall *fw, unsigned long offset)
{
357 358 359
	if (!fw->job->is_addr_reg)
		return 0;

360 361 362 363 364 365 366 367 368 369 370 371 372 373
	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
		if (!fw->num_relocs)
			return -EINVAL;

		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
			return -EINVAL;

		fw->num_relocs--;
		fw->reloc++;
	}

	return 0;
}

374 375 376 377 378 379 380 381 382 383 384 385 386
static int check_class(struct host1x_firewall *fw, u32 class)
{
	if (!fw->job->is_valid_class) {
		if (fw->class != class)
			return -EINVAL;
	} else {
		if (!fw->job->is_valid_class(fw->class))
			return -EINVAL;
	}

	return 0;
}

387 388 389 390
static int check_mask(struct host1x_firewall *fw)
{
	u32 mask = fw->mask;
	u32 reg = fw->reg;
391
	int ret;
392 393 394 395 396 397

	while (mask) {
		if (fw->words == 0)
			return -EINVAL;

		if (mask & 1) {
398 399 400 401
			ret = check_register(fw, reg);
			if (ret < 0)
				return ret;

402 403 404 405 406 407 408 409 410 411 412 413 414 415
			fw->words--;
			fw->offset++;
		}
		mask >>= 1;
		reg++;
	}

	return 0;
}

static int check_incr(struct host1x_firewall *fw)
{
	u32 count = fw->count;
	u32 reg = fw->reg;
416
	int ret;
417

418
	while (count) {
419 420 421
		if (fw->words == 0)
			return -EINVAL;

422 423 424 425
		ret = check_register(fw, reg);
		if (ret < 0)
			return ret;

426 427 428 429 430 431 432 433 434 435 436 437
		reg++;
		fw->words--;
		fw->offset++;
		count--;
	}

	return 0;
}

static int check_nonincr(struct host1x_firewall *fw)
{
	u32 count = fw->count;
438
	int ret;
439 440 441 442 443

	while (count) {
		if (fw->words == 0)
			return -EINVAL;

444 445 446 447
		ret = check_register(fw, fw->reg);
		if (ret < 0)
			return ret;

448 449 450 451 452 453 454 455
		fw->words--;
		fw->offset++;
		count--;
	}

	return 0;
}

456
static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
457
{
458 459
	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
		(g->offset / sizeof(u32));
460
	u32 job_class = fw->class;
461 462
	int err = 0;

463
	fw->words = g->words;
464
	fw->cmdbuf = g->bo;
465
	fw->offset = 0;
466

467 468
	while (fw->words && !err) {
		u32 word = cmdbuf_base[fw->offset];
469 470
		u32 opcode = (word & 0xf0000000) >> 28;

471 472 473 474 475
		fw->mask = 0;
		fw->reg = 0;
		fw->count = 0;
		fw->words--;
		fw->offset++;
476 477 478

		switch (opcode) {
		case 0:
479 480 481
			fw->class = word >> 6 & 0x3ff;
			fw->mask = word & 0x3f;
			fw->reg = word >> 16 & 0xfff;
482 483 484
			err = check_class(fw, job_class);
			if (!err)
				err = check_mask(fw);
485 486 487 488
			if (err)
				goto out;
			break;
		case 1:
489 490 491
			fw->reg = word >> 16 & 0xfff;
			fw->count = word & 0xffff;
			err = check_incr(fw);
492 493 494 495 496
			if (err)
				goto out;
			break;

		case 2:
497 498 499
			fw->reg = word >> 16 & 0xfff;
			fw->count = word & 0xffff;
			err = check_nonincr(fw);
500 501 502 503 504
			if (err)
				goto out;
			break;

		case 3:
505 506 507
			fw->mask = word & 0xffff;
			fw->reg = word >> 16 & 0xfff;
			err = check_mask(fw);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
			if (err)
				goto out;
			break;
		case 4:
		case 14:
			break;
		default:
			err = -EINVAL;
			break;
		}
	}

out:
	return err;
}

524 525
static inline int copy_gathers(struct device *host, struct host1x_job *job,
			       struct device *dev)
526
{
527
	struct host1x_firewall fw;
528 529
	size_t size = 0;
	size_t offset = 0;
530
	unsigned int i;
531

532 533
	fw.job = job;
	fw.dev = dev;
534
	fw.reloc = job->relocs;
535
	fw.num_relocs = job->num_relocs;
536
	fw.class = job->class;
537

538 539
	for (i = 0; i < job->num_gathers; i++) {
		struct host1x_job_gather *g = &job->gathers[i];
540

541 542 543
		size += g->words * sizeof(u32);
	}

544 545 546 547
	/*
	 * Try a non-blocking allocation from a higher priority pools first,
	 * as awaiting for the allocation here is a major performance hit.
	 */
548
	job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
549 550 551 552
					       GFP_NOWAIT);

	/* the higher priority allocation failed, try the generic-blocking */
	if (!job->gather_copy_mapped)
553
		job->gather_copy_mapped = dma_alloc_wc(host, size,
554 555 556
						       &job->gather_copy,
						       GFP_KERNEL);
	if (!job->gather_copy_mapped)
557
		return -ENOMEM;
558 559 560 561 562 563 564

	job->gather_copy_size = size;

	for (i = 0; i < job->num_gathers; i++) {
		struct host1x_job_gather *g = &job->gathers[i];
		void *gather;

565
		/* Copy the gather */
566 567 568 569 570
		gather = host1x_bo_mmap(g->bo);
		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
		       g->words * sizeof(u32));
		host1x_bo_munmap(g->bo, gather);

571
		/* Store the location in the buffer */
572 573
		g->base = job->gather_copy;
		g->offset = offset;
574 575 576 577

		/* Validate the job */
		if (validate(&fw, g))
			return -EINVAL;
578 579 580 581

		offset += g->words * sizeof(u32);
	}

582 583
	/* No relocs should remain at this point */
	if (fw.num_relocs)
584 585
		return -EINVAL;

586 587 588 589 590 591 592 593 594 595
	return 0;
}

int host1x_job_pin(struct host1x_job *job, struct device *dev)
{
	int err;
	unsigned int i, j;
	struct host1x *host = dev_get_drvdata(dev->parent);

	/* pin memory */
596 597
	err = pin_job(host, job);
	if (err)
598 599
		goto out;

600
	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
601
		err = copy_gathers(host->dev, job, dev);
602 603 604 605
		if (err)
			goto out;
	}

606 607 608 609 610 611 612 613
	/* patch gathers */
	for (i = 0; i < job->num_gathers; i++) {
		struct host1x_job_gather *g = &job->gathers[i];

		/* process each gather mem only once */
		if (g->handled)
			continue;

614 615 616
		/* copy_gathers() sets gathers base if firewall is enabled */
		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
			g->base = job->gather_addr_phys[i];
617

618 619
		for (j = i + 1; j < job->num_gathers; j++) {
			if (job->gathers[j].bo == g->bo) {
620
				job->gathers[j].handled = true;
621 622 623
				job->gathers[j].base = g->base;
			}
		}
624

625
		err = do_relocs(job, g);
626
		if (err)
627
			break;
628 629 630
	}

out:
631 632
	if (err)
		host1x_job_unpin(job);
633 634 635 636
	wmb();

	return err;
}
637
EXPORT_SYMBOL(host1x_job_pin);
638 639 640

void host1x_job_unpin(struct host1x_job *job)
{
641
	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
642 643 644 645
	unsigned int i;

	for (i = 0; i < job->num_unpins; i++) {
		struct host1x_job_unpin_data *unpin = &job->unpins[i];
646 647
		struct device *dev = unpin->dev ?: host->dev;
		struct sg_table *sgt = unpin->sgt;
648

649 650
		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
		    unpin->size && host->domain) {
651 652 653 654 655 656
			iommu_unmap(host->domain, job->addr_phys[i],
				    unpin->size);
			free_iova(&host->iova,
				iova_pfn(&host->iova, job->addr_phys[i]));
		}

657
		if (unpin->dev && sgt)
658
			dma_unmap_sgtable(unpin->dev, sgt, unpin->dir, 0);
659 660

		host1x_bo_unpin(dev, unpin->bo, sgt);
661 662
		host1x_bo_put(unpin->bo);
	}
663

664 665 666
	job->num_unpins = 0;

	if (job->gather_copy_size)
667
		dma_free_wc(host->dev, job->gather_copy_size,
668
			    job->gather_copy_mapped, job->gather_copy);
669
}
670
EXPORT_SYMBOL(host1x_job_unpin);
671 672 673 674 675 676 677 678 679 680 681 682 683

/*
 * Debug routine used to dump job entries
 */
void host1x_job_dump(struct device *dev, struct host1x_job *job)
{
	dev_dbg(dev, "    SYNCPT_ID   %d\n", job->syncpt_id);
	dev_dbg(dev, "    SYNCPT_VAL  %d\n", job->syncpt_end);
	dev_dbg(dev, "    FIRST_GET   0x%x\n", job->first_get);
	dev_dbg(dev, "    TIMEOUT     %d\n", job->timeout);
	dev_dbg(dev, "    NUM_SLOTS   %d\n", job->num_slots);
	dev_dbg(dev, "    NUM_HANDLES %d\n", job->num_unpins);
}