xref: /openbmc/linux/drivers/gpu/host1x/job.c (revision 2aed4f5ab04af922a7cf1b616701845c9ed2473f)
19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26579324aSTerje Bergstrom /*
36579324aSTerje Bergstrom  * Tegra host1x Job
46579324aSTerje Bergstrom  *
5f08ef2d1SArto Merilainen  * Copyright (c) 2010-2015, NVIDIA Corporation.
66579324aSTerje Bergstrom  */
76579324aSTerje Bergstrom 
86579324aSTerje Bergstrom #include <linux/dma-mapping.h>
96579324aSTerje Bergstrom #include <linux/err.h>
1035d747a8SThierry Reding #include <linux/host1x.h>
11273da5a0SThierry Reding #include <linux/iommu.h>
126579324aSTerje Bergstrom #include <linux/kref.h>
136579324aSTerje Bergstrom #include <linux/module.h>
146579324aSTerje Bergstrom #include <linux/scatterlist.h>
156579324aSTerje Bergstrom #include <linux/slab.h>
166579324aSTerje Bergstrom #include <linux/vmalloc.h>
176579324aSTerje Bergstrom #include <trace/events/host1x.h>
186579324aSTerje Bergstrom 
196579324aSTerje Bergstrom #include "channel.h"
206579324aSTerje Bergstrom #include "dev.h"
216579324aSTerje Bergstrom #include "job.h"
226579324aSTerje Bergstrom #include "syncpt.h"
236579324aSTerje Bergstrom 
24a47ac10eSDmitry Osipenko #define HOST1X_WAIT_SYNCPT_OFFSET 0x8
25a47ac10eSDmitry Osipenko 
266579324aSTerje Bergstrom struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
2724c94e16SThierry Reding 				    u32 num_cmdbufs, u32 num_relocs)
286579324aSTerje Bergstrom {
296579324aSTerje Bergstrom 	struct host1x_job *job = NULL;
3026c8de5eSDmitry Osipenko 	unsigned int num_unpins = num_relocs;
316579324aSTerje Bergstrom 	u64 total;
326579324aSTerje Bergstrom 	void *mem;
336579324aSTerje Bergstrom 
3426c8de5eSDmitry Osipenko 	if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
3526c8de5eSDmitry Osipenko 		num_unpins += num_cmdbufs;
3626c8de5eSDmitry Osipenko 
376579324aSTerje Bergstrom 	/* Check that we're not going to overflow */
386579324aSTerje Bergstrom 	total = sizeof(struct host1x_job) +
39f5fda676SDan Carpenter 		(u64)num_relocs * sizeof(struct host1x_reloc) +
40f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
41f5fda676SDan Carpenter 		(u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
42f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(dma_addr_t) +
43f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(u32 *);
446579324aSTerje Bergstrom 	if (total > ULONG_MAX)
456579324aSTerje Bergstrom 		return NULL;
466579324aSTerje Bergstrom 
476579324aSTerje Bergstrom 	mem = job = kzalloc(total, GFP_KERNEL);
486579324aSTerje Bergstrom 	if (!job)
496579324aSTerje Bergstrom 		return NULL;
506579324aSTerje Bergstrom 
516579324aSTerje Bergstrom 	kref_init(&job->ref);
526579324aSTerje Bergstrom 	job->channel = ch;
536579324aSTerje Bergstrom 
546579324aSTerje Bergstrom 	/* Redistribute memory to the structs  */
556579324aSTerje Bergstrom 	mem += sizeof(struct host1x_job);
5606490bb9SThierry Reding 	job->relocs = num_relocs ? mem : NULL;
576579324aSTerje Bergstrom 	mem += num_relocs * sizeof(struct host1x_reloc);
586579324aSTerje Bergstrom 	job->unpins = num_unpins ? mem : NULL;
596579324aSTerje Bergstrom 	mem += num_unpins * sizeof(struct host1x_job_unpin_data);
606579324aSTerje Bergstrom 	job->gathers = num_cmdbufs ? mem : NULL;
616579324aSTerje Bergstrom 	mem += num_cmdbufs * sizeof(struct host1x_job_gather);
626579324aSTerje Bergstrom 	job->addr_phys = num_unpins ? mem : NULL;
636579324aSTerje Bergstrom 
646579324aSTerje Bergstrom 	job->reloc_addr_phys = job->addr_phys;
656579324aSTerje Bergstrom 	job->gather_addr_phys = &job->addr_phys[num_relocs];
666579324aSTerje Bergstrom 
676579324aSTerje Bergstrom 	return job;
686579324aSTerje Bergstrom }
69fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_alloc);
706579324aSTerje Bergstrom 
716579324aSTerje Bergstrom struct host1x_job *host1x_job_get(struct host1x_job *job)
726579324aSTerje Bergstrom {
736579324aSTerje Bergstrom 	kref_get(&job->ref);
746579324aSTerje Bergstrom 	return job;
756579324aSTerje Bergstrom }
76fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_get);
776579324aSTerje Bergstrom 
786579324aSTerje Bergstrom static void job_free(struct kref *ref)
796579324aSTerje Bergstrom {
806579324aSTerje Bergstrom 	struct host1x_job *job = container_of(ref, struct host1x_job, ref);
816579324aSTerje Bergstrom 
82*2aed4f5aSMikko Perttunen 	if (job->syncpt)
83*2aed4f5aSMikko Perttunen 		host1x_syncpt_put(job->syncpt);
84*2aed4f5aSMikko Perttunen 
856579324aSTerje Bergstrom 	kfree(job);
866579324aSTerje Bergstrom }
876579324aSTerje Bergstrom 
886579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job)
896579324aSTerje Bergstrom {
906579324aSTerje Bergstrom 	kref_put(&job->ref, job_free);
916579324aSTerje Bergstrom }
92fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put);
936579324aSTerje Bergstrom 
946579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
95326bbd79SThierry Reding 			   unsigned int words, unsigned int offset)
966579324aSTerje Bergstrom {
97326bbd79SThierry Reding 	struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
986579324aSTerje Bergstrom 
99326bbd79SThierry Reding 	gather->words = words;
100326bbd79SThierry Reding 	gather->bo = bo;
101326bbd79SThierry Reding 	gather->offset = offset;
102326bbd79SThierry Reding 
1036579324aSTerje Bergstrom 	job->num_gathers++;
1046579324aSTerje Bergstrom }
105fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather);
1066579324aSTerje Bergstrom 
107404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
1086579324aSTerje Bergstrom {
109af1cbfb9SThierry Reding 	struct host1x_client *client = job->client;
110af1cbfb9SThierry Reding 	struct device *dev = client->dev;
111fd323e9eSDmitry Osipenko 	struct host1x_job_gather *g;
112273da5a0SThierry Reding 	struct iommu_domain *domain;
1136579324aSTerje Bergstrom 	unsigned int i;
114404bfb78SMikko Perttunen 	int err;
1156579324aSTerje Bergstrom 
116273da5a0SThierry Reding 	domain = iommu_get_domain_for_dev(dev);
1176579324aSTerje Bergstrom 	job->num_unpins = 0;
1186579324aSTerje Bergstrom 
1196579324aSTerje Bergstrom 	for (i = 0; i < job->num_relocs; i++) {
12006490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
121af1cbfb9SThierry Reding 		dma_addr_t phys_addr, *phys;
1226579324aSTerje Bergstrom 		struct sg_table *sgt;
1236579324aSTerje Bergstrom 
124961e3beaSThierry Reding 		reloc->target.bo = host1x_bo_get(reloc->target.bo);
125404bfb78SMikko Perttunen 		if (!reloc->target.bo) {
126404bfb78SMikko Perttunen 			err = -EINVAL;
1276579324aSTerje Bergstrom 			goto unpin;
128404bfb78SMikko Perttunen 		}
1296579324aSTerje Bergstrom 
130273da5a0SThierry Reding 		/*
131273da5a0SThierry Reding 		 * If the client device is not attached to an IOMMU, the
132273da5a0SThierry Reding 		 * physical address of the buffer object can be used.
133273da5a0SThierry Reding 		 *
134273da5a0SThierry Reding 		 * Similarly, when an IOMMU domain is shared between all
135273da5a0SThierry Reding 		 * host1x clients, the IOVA is already available, so no
136273da5a0SThierry Reding 		 * need to map the buffer object again.
137273da5a0SThierry Reding 		 *
138273da5a0SThierry Reding 		 * XXX Note that this isn't always safe to do because it
139273da5a0SThierry Reding 		 * relies on an assumption that no cache maintenance is
140273da5a0SThierry Reding 		 * needed on the buffer objects.
141273da5a0SThierry Reding 		 */
142273da5a0SThierry Reding 		if (!domain || client->group)
143af1cbfb9SThierry Reding 			phys = &phys_addr;
144af1cbfb9SThierry Reding 		else
145af1cbfb9SThierry Reding 			phys = NULL;
146af1cbfb9SThierry Reding 
147af1cbfb9SThierry Reding 		sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
14880327ce3SThierry Reding 		if (IS_ERR(sgt)) {
14980327ce3SThierry Reding 			err = PTR_ERR(sgt);
15080327ce3SThierry Reding 			goto unpin;
15180327ce3SThierry Reding 		}
1526579324aSTerje Bergstrom 
153af1cbfb9SThierry Reding 		if (sgt) {
154af1cbfb9SThierry Reding 			unsigned long mask = HOST1X_RELOC_READ |
155af1cbfb9SThierry Reding 					     HOST1X_RELOC_WRITE;
156af1cbfb9SThierry Reding 			enum dma_data_direction dir;
157af1cbfb9SThierry Reding 
158af1cbfb9SThierry Reding 			switch (reloc->flags & mask) {
159af1cbfb9SThierry Reding 			case HOST1X_RELOC_READ:
160af1cbfb9SThierry Reding 				dir = DMA_TO_DEVICE;
161af1cbfb9SThierry Reding 				break;
162af1cbfb9SThierry Reding 
163af1cbfb9SThierry Reding 			case HOST1X_RELOC_WRITE:
164af1cbfb9SThierry Reding 				dir = DMA_FROM_DEVICE;
165af1cbfb9SThierry Reding 				break;
166af1cbfb9SThierry Reding 
167af1cbfb9SThierry Reding 			case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
168af1cbfb9SThierry Reding 				dir = DMA_BIDIRECTIONAL;
169af1cbfb9SThierry Reding 				break;
170af1cbfb9SThierry Reding 
171af1cbfb9SThierry Reding 			default:
172af1cbfb9SThierry Reding 				err = -EINVAL;
173af1cbfb9SThierry Reding 				goto unpin;
174af1cbfb9SThierry Reding 			}
175af1cbfb9SThierry Reding 
17667ed9f9dSMarek Szyprowski 			err = dma_map_sgtable(dev, sgt, dir, 0);
17767ed9f9dSMarek Szyprowski 			if (err)
178af1cbfb9SThierry Reding 				goto unpin;
179af1cbfb9SThierry Reding 
180af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dev = dev;
181af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dir = dir;
182af1cbfb9SThierry Reding 			phys_addr = sg_dma_address(sgt->sgl);
183af1cbfb9SThierry Reding 		}
184af1cbfb9SThierry Reding 
1856579324aSTerje Bergstrom 		job->addr_phys[job->num_unpins] = phys_addr;
186961e3beaSThierry Reding 		job->unpins[job->num_unpins].bo = reloc->target.bo;
1876579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
1886579324aSTerje Bergstrom 		job->num_unpins++;
1896579324aSTerje Bergstrom 	}
1906579324aSTerje Bergstrom 
19126c8de5eSDmitry Osipenko 	/*
19226c8de5eSDmitry Osipenko 	 * We will copy gathers BO content later, so there is no need to
19326c8de5eSDmitry Osipenko 	 * hold and pin them.
19426c8de5eSDmitry Osipenko 	 */
19526c8de5eSDmitry Osipenko 	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
19626c8de5eSDmitry Osipenko 		return 0;
19726c8de5eSDmitry Osipenko 
1986579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
199404bfb78SMikko Perttunen 		size_t gather_size = 0;
200404bfb78SMikko Perttunen 		struct scatterlist *sg;
2016579324aSTerje Bergstrom 		struct sg_table *sgt;
2026579324aSTerje Bergstrom 		dma_addr_t phys_addr;
203404bfb78SMikko Perttunen 		unsigned long shift;
204404bfb78SMikko Perttunen 		struct iova *alloc;
205273da5a0SThierry Reding 		dma_addr_t *phys;
206404bfb78SMikko Perttunen 		unsigned int j;
2076579324aSTerje Bergstrom 
208fd323e9eSDmitry Osipenko 		g = &job->gathers[i];
2096579324aSTerje Bergstrom 		g->bo = host1x_bo_get(g->bo);
210404bfb78SMikko Perttunen 		if (!g->bo) {
211404bfb78SMikko Perttunen 			err = -EINVAL;
2126579324aSTerje Bergstrom 			goto unpin;
213404bfb78SMikko Perttunen 		}
2146579324aSTerje Bergstrom 
215273da5a0SThierry Reding 		/**
216273da5a0SThierry Reding 		 * If the host1x is not attached to an IOMMU, there is no need
217273da5a0SThierry Reding 		 * to map the buffer object for the host1x, since the physical
218273da5a0SThierry Reding 		 * address can simply be used.
219273da5a0SThierry Reding 		 */
220273da5a0SThierry Reding 		if (!iommu_get_domain_for_dev(host->dev))
221273da5a0SThierry Reding 			phys = &phys_addr;
222273da5a0SThierry Reding 		else
223273da5a0SThierry Reding 			phys = NULL;
224273da5a0SThierry Reding 
225273da5a0SThierry Reding 		sgt = host1x_bo_pin(host->dev, g->bo, phys);
22680327ce3SThierry Reding 		if (IS_ERR(sgt)) {
22780327ce3SThierry Reding 			err = PTR_ERR(sgt);
228fd323e9eSDmitry Osipenko 			goto put;
22980327ce3SThierry Reding 		}
2306579324aSTerje Bergstrom 
23126c8de5eSDmitry Osipenko 		if (host->domain) {
23267ed9f9dSMarek Szyprowski 			for_each_sgtable_sg(sgt, sg, j)
233404bfb78SMikko Perttunen 				gather_size += sg->length;
234404bfb78SMikko Perttunen 			gather_size = iova_align(&host->iova, gather_size);
235404bfb78SMikko Perttunen 
236404bfb78SMikko Perttunen 			shift = iova_shift(&host->iova);
237404bfb78SMikko Perttunen 			alloc = alloc_iova(&host->iova, gather_size >> shift,
238404bfb78SMikko Perttunen 					   host->iova_end >> shift, true);
239404bfb78SMikko Perttunen 			if (!alloc) {
240404bfb78SMikko Perttunen 				err = -ENOMEM;
241fd323e9eSDmitry Osipenko 				goto put;
242404bfb78SMikko Perttunen 			}
243404bfb78SMikko Perttunen 
24467ed9f9dSMarek Szyprowski 			err = iommu_map_sgtable(host->domain,
245404bfb78SMikko Perttunen 					iova_dma_addr(&host->iova, alloc),
24667ed9f9dSMarek Szyprowski 					sgt, IOMMU_READ);
247404bfb78SMikko Perttunen 			if (err == 0) {
248404bfb78SMikko Perttunen 				__free_iova(&host->iova, alloc);
249404bfb78SMikko Perttunen 				err = -EINVAL;
250fd323e9eSDmitry Osipenko 				goto put;
251404bfb78SMikko Perttunen 			}
252404bfb78SMikko Perttunen 
253404bfb78SMikko Perttunen 			job->unpins[job->num_unpins].size = gather_size;
254af1cbfb9SThierry Reding 			phys_addr = iova_dma_addr(&host->iova, alloc);
255273da5a0SThierry Reding 		} else if (sgt) {
25667ed9f9dSMarek Szyprowski 			err = dma_map_sgtable(host->dev, sgt, DMA_TO_DEVICE, 0);
25767ed9f9dSMarek Szyprowski 			if (err)
258fd323e9eSDmitry Osipenko 				goto put;
259404bfb78SMikko Perttunen 
26098ae41adSThierry Reding 			job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
261af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dev = host->dev;
262af1cbfb9SThierry Reding 			phys_addr = sg_dma_address(sgt->sgl);
263af1cbfb9SThierry Reding 		}
264404bfb78SMikko Perttunen 
265af1cbfb9SThierry Reding 		job->addr_phys[job->num_unpins] = phys_addr;
266af1cbfb9SThierry Reding 		job->gather_addr_phys[i] = phys_addr;
267af1cbfb9SThierry Reding 
2686579324aSTerje Bergstrom 		job->unpins[job->num_unpins].bo = g->bo;
2696579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
2706579324aSTerje Bergstrom 		job->num_unpins++;
2716579324aSTerje Bergstrom 	}
2726579324aSTerje Bergstrom 
273404bfb78SMikko Perttunen 	return 0;
2746579324aSTerje Bergstrom 
275fd323e9eSDmitry Osipenko put:
276fd323e9eSDmitry Osipenko 	host1x_bo_put(g->bo);
2776579324aSTerje Bergstrom unpin:
2786579324aSTerje Bergstrom 	host1x_job_unpin(job);
279404bfb78SMikko Perttunen 	return err;
2806579324aSTerje Bergstrom }
2816579324aSTerje Bergstrom 
28247f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
2836579324aSTerje Bergstrom {
2847a8139c5SDaniel Vetter 	void *cmdbuf_addr = NULL;
28547f89c10SDmitry Osipenko 	struct host1x_bo *cmdbuf = g->bo;
286d4ad3ad9SThierry Reding 	unsigned int i;
2876579324aSTerje Bergstrom 
2886579324aSTerje Bergstrom 	/* pin & patch the relocs for one gather */
2893364cd28SArto Merilainen 	for (i = 0; i < job->num_relocs; i++) {
29006490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
2916579324aSTerje Bergstrom 		u32 reloc_addr = (job->reloc_addr_phys[i] +
292961e3beaSThierry Reding 				  reloc->target.offset) >> reloc->shift;
2936579324aSTerje Bergstrom 		u32 *target;
2946579324aSTerje Bergstrom 
2956579324aSTerje Bergstrom 		/* skip all other gathers */
296961e3beaSThierry Reding 		if (cmdbuf != reloc->cmdbuf.bo)
2976579324aSTerje Bergstrom 			continue;
2986579324aSTerje Bergstrom 
29947f89c10SDmitry Osipenko 		if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
30047f89c10SDmitry Osipenko 			target = (u32 *)job->gather_copy_mapped +
30147f89c10SDmitry Osipenko 					reloc->cmdbuf.offset / sizeof(u32) +
30247f89c10SDmitry Osipenko 						g->offset / sizeof(u32);
30347f89c10SDmitry Osipenko 			goto patch_reloc;
30447f89c10SDmitry Osipenko 		}
30547f89c10SDmitry Osipenko 
3067a8139c5SDaniel Vetter 		if (!cmdbuf_addr) {
3077a8139c5SDaniel Vetter 			cmdbuf_addr = host1x_bo_mmap(cmdbuf);
3086579324aSTerje Bergstrom 
3097a8139c5SDaniel Vetter 			if (unlikely(!cmdbuf_addr)) {
3106579324aSTerje Bergstrom 				pr_err("Could not map cmdbuf for relocation\n");
3116579324aSTerje Bergstrom 				return -ENOMEM;
3126579324aSTerje Bergstrom 			}
3136579324aSTerje Bergstrom 		}
3146579324aSTerje Bergstrom 
3157a8139c5SDaniel Vetter 		target = cmdbuf_addr + reloc->cmdbuf.offset;
31647f89c10SDmitry Osipenko patch_reloc:
3176579324aSTerje Bergstrom 		*target = reloc_addr;
3186579324aSTerje Bergstrom 	}
3196579324aSTerje Bergstrom 
3207a8139c5SDaniel Vetter 	if (cmdbuf_addr)
3217a8139c5SDaniel Vetter 		host1x_bo_munmap(cmdbuf, cmdbuf_addr);
3226579324aSTerje Bergstrom 
3236579324aSTerje Bergstrom 	return 0;
3246579324aSTerje Bergstrom }
3256579324aSTerje Bergstrom 
3265060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
3276579324aSTerje Bergstrom 			unsigned int offset)
3286579324aSTerje Bergstrom {
3296579324aSTerje Bergstrom 	offset *= sizeof(u32);
3306579324aSTerje Bergstrom 
331961e3beaSThierry Reding 	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
3325060d8ecSArto Merilainen 		return false;
3336579324aSTerje Bergstrom 
334571cbf70SDmitry Osipenko 	/* relocation shift value validation isn't implemented yet */
335571cbf70SDmitry Osipenko 	if (reloc->shift)
336571cbf70SDmitry Osipenko 		return false;
337571cbf70SDmitry Osipenko 
3385060d8ecSArto Merilainen 	return true;
3396579324aSTerje Bergstrom }
3406579324aSTerje Bergstrom 
3416579324aSTerje Bergstrom struct host1x_firewall {
3426579324aSTerje Bergstrom 	struct host1x_job *job;
3436579324aSTerje Bergstrom 	struct device *dev;
3446579324aSTerje Bergstrom 
3456579324aSTerje Bergstrom 	unsigned int num_relocs;
3466579324aSTerje Bergstrom 	struct host1x_reloc *reloc;
3476579324aSTerje Bergstrom 
348d7fbcf47SThierry Reding 	struct host1x_bo *cmdbuf;
3496579324aSTerje Bergstrom 	unsigned int offset;
3506579324aSTerje Bergstrom 
3516579324aSTerje Bergstrom 	u32 words;
3526579324aSTerje Bergstrom 	u32 class;
3536579324aSTerje Bergstrom 	u32 reg;
3546579324aSTerje Bergstrom 	u32 mask;
3556579324aSTerje Bergstrom 	u32 count;
3566579324aSTerje Bergstrom };
3576579324aSTerje Bergstrom 
358d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset)
359d77563ffSThierry Reding {
3600f563a4bSDmitry Osipenko 	if (!fw->job->is_addr_reg)
3610f563a4bSDmitry Osipenko 		return 0;
3620f563a4bSDmitry Osipenko 
363d77563ffSThierry Reding 	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
364d77563ffSThierry Reding 		if (!fw->num_relocs)
365d77563ffSThierry Reding 			return -EINVAL;
366d77563ffSThierry Reding 
367d77563ffSThierry Reding 		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
368d77563ffSThierry Reding 			return -EINVAL;
369d77563ffSThierry Reding 
370d77563ffSThierry Reding 		fw->num_relocs--;
371d77563ffSThierry Reding 		fw->reloc++;
372d77563ffSThierry Reding 	}
373d77563ffSThierry Reding 
374d77563ffSThierry Reding 	return 0;
375d77563ffSThierry Reding }
376d77563ffSThierry Reding 
3770f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class)
3780f563a4bSDmitry Osipenko {
3790f563a4bSDmitry Osipenko 	if (!fw->job->is_valid_class) {
3800f563a4bSDmitry Osipenko 		if (fw->class != class)
3810f563a4bSDmitry Osipenko 			return -EINVAL;
3820f563a4bSDmitry Osipenko 	} else {
3830f563a4bSDmitry Osipenko 		if (!fw->job->is_valid_class(fw->class))
3840f563a4bSDmitry Osipenko 			return -EINVAL;
3850f563a4bSDmitry Osipenko 	}
3860f563a4bSDmitry Osipenko 
3870f563a4bSDmitry Osipenko 	return 0;
3880f563a4bSDmitry Osipenko }
3890f563a4bSDmitry Osipenko 
3906579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw)
3916579324aSTerje Bergstrom {
3926579324aSTerje Bergstrom 	u32 mask = fw->mask;
3936579324aSTerje Bergstrom 	u32 reg = fw->reg;
394d77563ffSThierry Reding 	int ret;
3956579324aSTerje Bergstrom 
3966579324aSTerje Bergstrom 	while (mask) {
3976579324aSTerje Bergstrom 		if (fw->words == 0)
3986579324aSTerje Bergstrom 			return -EINVAL;
3996579324aSTerje Bergstrom 
4006579324aSTerje Bergstrom 		if (mask & 1) {
401d77563ffSThierry Reding 			ret = check_register(fw, reg);
402d77563ffSThierry Reding 			if (ret < 0)
403d77563ffSThierry Reding 				return ret;
404d77563ffSThierry Reding 
4056579324aSTerje Bergstrom 			fw->words--;
4066579324aSTerje Bergstrom 			fw->offset++;
4076579324aSTerje Bergstrom 		}
4086579324aSTerje Bergstrom 		mask >>= 1;
4096579324aSTerje Bergstrom 		reg++;
4106579324aSTerje Bergstrom 	}
4116579324aSTerje Bergstrom 
4126579324aSTerje Bergstrom 	return 0;
4136579324aSTerje Bergstrom }
4146579324aSTerje Bergstrom 
4156579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw)
4166579324aSTerje Bergstrom {
4176579324aSTerje Bergstrom 	u32 count = fw->count;
4186579324aSTerje Bergstrom 	u32 reg = fw->reg;
419d77563ffSThierry Reding 	int ret;
4206579324aSTerje Bergstrom 
42164c173d3STerje Bergstrom 	while (count) {
4226579324aSTerje Bergstrom 		if (fw->words == 0)
4236579324aSTerje Bergstrom 			return -EINVAL;
4246579324aSTerje Bergstrom 
425d77563ffSThierry Reding 		ret = check_register(fw, reg);
426d77563ffSThierry Reding 		if (ret < 0)
427d77563ffSThierry Reding 			return ret;
428d77563ffSThierry Reding 
4296579324aSTerje Bergstrom 		reg++;
4306579324aSTerje Bergstrom 		fw->words--;
4316579324aSTerje Bergstrom 		fw->offset++;
4326579324aSTerje Bergstrom 		count--;
4336579324aSTerje Bergstrom 	}
4346579324aSTerje Bergstrom 
4356579324aSTerje Bergstrom 	return 0;
4366579324aSTerje Bergstrom }
4376579324aSTerje Bergstrom 
4386579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw)
4396579324aSTerje Bergstrom {
4406579324aSTerje Bergstrom 	u32 count = fw->count;
441d77563ffSThierry Reding 	int ret;
4426579324aSTerje Bergstrom 
4436579324aSTerje Bergstrom 	while (count) {
4446579324aSTerje Bergstrom 		if (fw->words == 0)
4456579324aSTerje Bergstrom 			return -EINVAL;
4466579324aSTerje Bergstrom 
447d77563ffSThierry Reding 		ret = check_register(fw, fw->reg);
448d77563ffSThierry Reding 		if (ret < 0)
449d77563ffSThierry Reding 			return ret;
450d77563ffSThierry Reding 
4516579324aSTerje Bergstrom 		fw->words--;
4526579324aSTerje Bergstrom 		fw->offset++;
4536579324aSTerje Bergstrom 		count--;
4546579324aSTerje Bergstrom 	}
4556579324aSTerje Bergstrom 
4566579324aSTerje Bergstrom 	return 0;
4576579324aSTerje Bergstrom }
4586579324aSTerje Bergstrom 
459afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
4606579324aSTerje Bergstrom {
4613364cd28SArto Merilainen 	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
4623364cd28SArto Merilainen 		(g->offset / sizeof(u32));
4630f563a4bSDmitry Osipenko 	u32 job_class = fw->class;
4646579324aSTerje Bergstrom 	int err = 0;
4656579324aSTerje Bergstrom 
466afac0e43STerje Bergstrom 	fw->words = g->words;
467d7fbcf47SThierry Reding 	fw->cmdbuf = g->bo;
468afac0e43STerje Bergstrom 	fw->offset = 0;
4696579324aSTerje Bergstrom 
470afac0e43STerje Bergstrom 	while (fw->words && !err) {
471afac0e43STerje Bergstrom 		u32 word = cmdbuf_base[fw->offset];
4726579324aSTerje Bergstrom 		u32 opcode = (word & 0xf0000000) >> 28;
4736579324aSTerje Bergstrom 
474afac0e43STerje Bergstrom 		fw->mask = 0;
475afac0e43STerje Bergstrom 		fw->reg = 0;
476afac0e43STerje Bergstrom 		fw->count = 0;
477afac0e43STerje Bergstrom 		fw->words--;
478afac0e43STerje Bergstrom 		fw->offset++;
4796579324aSTerje Bergstrom 
4806579324aSTerje Bergstrom 		switch (opcode) {
4816579324aSTerje Bergstrom 		case 0:
482afac0e43STerje Bergstrom 			fw->class = word >> 6 & 0x3ff;
483afac0e43STerje Bergstrom 			fw->mask = word & 0x3f;
484afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
4850f563a4bSDmitry Osipenko 			err = check_class(fw, job_class);
4860f563a4bSDmitry Osipenko 			if (!err)
487afac0e43STerje Bergstrom 				err = check_mask(fw);
4886579324aSTerje Bergstrom 			if (err)
4896579324aSTerje Bergstrom 				goto out;
4906579324aSTerje Bergstrom 			break;
4916579324aSTerje Bergstrom 		case 1:
492afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
493afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
494afac0e43STerje Bergstrom 			err = check_incr(fw);
4956579324aSTerje Bergstrom 			if (err)
4966579324aSTerje Bergstrom 				goto out;
4976579324aSTerje Bergstrom 			break;
4986579324aSTerje Bergstrom 
4996579324aSTerje Bergstrom 		case 2:
500afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
501afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
502afac0e43STerje Bergstrom 			err = check_nonincr(fw);
5036579324aSTerje Bergstrom 			if (err)
5046579324aSTerje Bergstrom 				goto out;
5056579324aSTerje Bergstrom 			break;
5066579324aSTerje Bergstrom 
5076579324aSTerje Bergstrom 		case 3:
508afac0e43STerje Bergstrom 			fw->mask = word & 0xffff;
509afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
510afac0e43STerje Bergstrom 			err = check_mask(fw);
5116579324aSTerje Bergstrom 			if (err)
5126579324aSTerje Bergstrom 				goto out;
5136579324aSTerje Bergstrom 			break;
5146579324aSTerje Bergstrom 		case 4:
5156579324aSTerje Bergstrom 		case 14:
5166579324aSTerje Bergstrom 			break;
5176579324aSTerje Bergstrom 		default:
5186579324aSTerje Bergstrom 			err = -EINVAL;
5196579324aSTerje Bergstrom 			break;
5206579324aSTerje Bergstrom 		}
5216579324aSTerje Bergstrom 	}
5226579324aSTerje Bergstrom 
5236579324aSTerje Bergstrom out:
5246579324aSTerje Bergstrom 	return err;
5256579324aSTerje Bergstrom }
5266579324aSTerje Bergstrom 
527b78e70c0SThierry Reding static inline int copy_gathers(struct device *host, struct host1x_job *job,
528b78e70c0SThierry Reding 			       struct device *dev)
5296579324aSTerje Bergstrom {
5303364cd28SArto Merilainen 	struct host1x_firewall fw;
5316579324aSTerje Bergstrom 	size_t size = 0;
5326579324aSTerje Bergstrom 	size_t offset = 0;
533d4ad3ad9SThierry Reding 	unsigned int i;
5346579324aSTerje Bergstrom 
5353364cd28SArto Merilainen 	fw.job = job;
5363364cd28SArto Merilainen 	fw.dev = dev;
53706490bb9SThierry Reding 	fw.reloc = job->relocs;
5383364cd28SArto Merilainen 	fw.num_relocs = job->num_relocs;
5393833d16fSDmitry Osipenko 	fw.class = job->class;
5403364cd28SArto Merilainen 
5416579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
5426579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
5436df633d0SThierry Reding 
5446579324aSTerje Bergstrom 		size += g->words * sizeof(u32);
5456579324aSTerje Bergstrom 	}
5466579324aSTerje Bergstrom 
54743240bbdSDmitry Osipenko 	/*
54843240bbdSDmitry Osipenko 	 * Try a non-blocking allocation from a higher priority pools first,
54943240bbdSDmitry Osipenko 	 * as awaiting for the allocation here is a major performance hit.
55043240bbdSDmitry Osipenko 	 */
551b78e70c0SThierry Reding 	job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
55243240bbdSDmitry Osipenko 					       GFP_NOWAIT);
55343240bbdSDmitry Osipenko 
55443240bbdSDmitry Osipenko 	/* the higher priority allocation failed, try the generic-blocking */
55543240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
556b78e70c0SThierry Reding 		job->gather_copy_mapped = dma_alloc_wc(host, size,
55743240bbdSDmitry Osipenko 						       &job->gather_copy,
5586579324aSTerje Bergstrom 						       GFP_KERNEL);
55943240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
560745cecc0SDan Carpenter 		return -ENOMEM;
5616579324aSTerje Bergstrom 
5626579324aSTerje Bergstrom 	job->gather_copy_size = size;
5636579324aSTerje Bergstrom 
5646579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
5656579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
5666579324aSTerje Bergstrom 		void *gather;
5676579324aSTerje Bergstrom 
5683364cd28SArto Merilainen 		/* Copy the gather */
5696579324aSTerje Bergstrom 		gather = host1x_bo_mmap(g->bo);
5706579324aSTerje Bergstrom 		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
5716579324aSTerje Bergstrom 		       g->words * sizeof(u32));
5726579324aSTerje Bergstrom 		host1x_bo_munmap(g->bo, gather);
5736579324aSTerje Bergstrom 
5743364cd28SArto Merilainen 		/* Store the location in the buffer */
5756579324aSTerje Bergstrom 		g->base = job->gather_copy;
5766579324aSTerje Bergstrom 		g->offset = offset;
5773364cd28SArto Merilainen 
5783364cd28SArto Merilainen 		/* Validate the job */
5793364cd28SArto Merilainen 		if (validate(&fw, g))
5803364cd28SArto Merilainen 			return -EINVAL;
5816579324aSTerje Bergstrom 
5826579324aSTerje Bergstrom 		offset += g->words * sizeof(u32);
5836579324aSTerje Bergstrom 	}
5846579324aSTerje Bergstrom 
58524c94e16SThierry Reding 	/* No relocs should remain at this point */
58624c94e16SThierry Reding 	if (fw.num_relocs)
587a9ff9995SErik Faye-Lund 		return -EINVAL;
588a9ff9995SErik Faye-Lund 
5896579324aSTerje Bergstrom 	return 0;
5906579324aSTerje Bergstrom }
5916579324aSTerje Bergstrom 
5926579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev)
5936579324aSTerje Bergstrom {
5946579324aSTerje Bergstrom 	int err;
5956579324aSTerje Bergstrom 	unsigned int i, j;
5966579324aSTerje Bergstrom 	struct host1x *host = dev_get_drvdata(dev->parent);
5976579324aSTerje Bergstrom 
5986579324aSTerje Bergstrom 	/* pin memory */
599404bfb78SMikko Perttunen 	err = pin_job(host, job);
600404bfb78SMikko Perttunen 	if (err)
6016579324aSTerje Bergstrom 		goto out;
6026579324aSTerje Bergstrom 
60347f89c10SDmitry Osipenko 	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
604b78e70c0SThierry Reding 		err = copy_gathers(host->dev, job, dev);
60547f89c10SDmitry Osipenko 		if (err)
60647f89c10SDmitry Osipenko 			goto out;
60747f89c10SDmitry Osipenko 	}
60847f89c10SDmitry Osipenko 
6096579324aSTerje Bergstrom 	/* patch gathers */
6106579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
6116579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
6126579324aSTerje Bergstrom 
6136579324aSTerje Bergstrom 		/* process each gather mem only once */
6146579324aSTerje Bergstrom 		if (g->handled)
6156579324aSTerje Bergstrom 			continue;
6166579324aSTerje Bergstrom 
61747f89c10SDmitry Osipenko 		/* copy_gathers() sets gathers base if firewall is enabled */
61847f89c10SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
6196579324aSTerje Bergstrom 			g->base = job->gather_addr_phys[i];
6206579324aSTerje Bergstrom 
621f08ef2d1SArto Merilainen 		for (j = i + 1; j < job->num_gathers; j++) {
622f08ef2d1SArto Merilainen 			if (job->gathers[j].bo == g->bo) {
6236579324aSTerje Bergstrom 				job->gathers[j].handled = true;
624f08ef2d1SArto Merilainen 				job->gathers[j].base = g->base;
625f08ef2d1SArto Merilainen 			}
626f08ef2d1SArto Merilainen 		}
6276579324aSTerje Bergstrom 
62847f89c10SDmitry Osipenko 		err = do_relocs(job, g);
6293364cd28SArto Merilainen 		if (err)
63047f89c10SDmitry Osipenko 			break;
6316579324aSTerje Bergstrom 	}
6326579324aSTerje Bergstrom 
6336579324aSTerje Bergstrom out:
634e5855aa3SDmitry Osipenko 	if (err)
635e5855aa3SDmitry Osipenko 		host1x_job_unpin(job);
6366579324aSTerje Bergstrom 	wmb();
6376579324aSTerje Bergstrom 
6386579324aSTerje Bergstrom 	return err;
6396579324aSTerje Bergstrom }
640fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin);
6416579324aSTerje Bergstrom 
6426579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job)
6436579324aSTerje Bergstrom {
644404bfb78SMikko Perttunen 	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
6456579324aSTerje Bergstrom 	unsigned int i;
6466579324aSTerje Bergstrom 
6476579324aSTerje Bergstrom 	for (i = 0; i < job->num_unpins; i++) {
6486579324aSTerje Bergstrom 		struct host1x_job_unpin_data *unpin = &job->unpins[i];
649af1cbfb9SThierry Reding 		struct device *dev = unpin->dev ?: host->dev;
650af1cbfb9SThierry Reding 		struct sg_table *sgt = unpin->sgt;
6516df633d0SThierry Reding 
652ec589232SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
653ec589232SDmitry Osipenko 		    unpin->size && host->domain) {
654404bfb78SMikko Perttunen 			iommu_unmap(host->domain, job->addr_phys[i],
655404bfb78SMikko Perttunen 				    unpin->size);
656404bfb78SMikko Perttunen 			free_iova(&host->iova,
657404bfb78SMikko Perttunen 				iova_pfn(&host->iova, job->addr_phys[i]));
658404bfb78SMikko Perttunen 		}
659404bfb78SMikko Perttunen 
660af1cbfb9SThierry Reding 		if (unpin->dev && sgt)
66167ed9f9dSMarek Szyprowski 			dma_unmap_sgtable(unpin->dev, sgt, unpin->dir, 0);
662af1cbfb9SThierry Reding 
663af1cbfb9SThierry Reding 		host1x_bo_unpin(dev, unpin->bo, sgt);
6646579324aSTerje Bergstrom 		host1x_bo_put(unpin->bo);
6656579324aSTerje Bergstrom 	}
6660b8070d1SThierry Reding 
6676579324aSTerje Bergstrom 	job->num_unpins = 0;
6686579324aSTerje Bergstrom 
6696579324aSTerje Bergstrom 	if (job->gather_copy_size)
670b78e70c0SThierry Reding 		dma_free_wc(host->dev, job->gather_copy_size,
671f6e45661SLuis R. Rodriguez 			    job->gather_copy_mapped, job->gather_copy);
6726579324aSTerje Bergstrom }
673fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_unpin);
6746579324aSTerje Bergstrom 
6756579324aSTerje Bergstrom /*
6766579324aSTerje Bergstrom  * Debug routine used to dump job entries
6776579324aSTerje Bergstrom  */
6786579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job)
6796579324aSTerje Bergstrom {
680*2aed4f5aSMikko Perttunen 	dev_dbg(dev, "    SYNCPT_ID   %d\n", job->syncpt->id);
6816579324aSTerje Bergstrom 	dev_dbg(dev, "    SYNCPT_VAL  %d\n", job->syncpt_end);
6826579324aSTerje Bergstrom 	dev_dbg(dev, "    FIRST_GET   0x%x\n", job->first_get);
6836579324aSTerje Bergstrom 	dev_dbg(dev, "    TIMEOUT     %d\n", job->timeout);
6846579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_SLOTS   %d\n", job->num_slots);
6856579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_HANDLES %d\n", job->num_unpins);
6866579324aSTerje Bergstrom }
687