xref: /linux/drivers/gpu/host1x/job.c (revision 67ed9f9d95189d0f99c38f34b8bb1a8fa135c877)
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 
826579324aSTerje Bergstrom 	kfree(job);
836579324aSTerje Bergstrom }
846579324aSTerje Bergstrom 
856579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job)
866579324aSTerje Bergstrom {
876579324aSTerje Bergstrom 	kref_put(&job->ref, job_free);
886579324aSTerje Bergstrom }
89fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put);
906579324aSTerje Bergstrom 
916579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
92326bbd79SThierry Reding 			   unsigned int words, unsigned int offset)
936579324aSTerje Bergstrom {
94326bbd79SThierry Reding 	struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
956579324aSTerje Bergstrom 
96326bbd79SThierry Reding 	gather->words = words;
97326bbd79SThierry Reding 	gather->bo = bo;
98326bbd79SThierry Reding 	gather->offset = offset;
99326bbd79SThierry Reding 
1006579324aSTerje Bergstrom 	job->num_gathers++;
1016579324aSTerje Bergstrom }
102fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather);
1036579324aSTerje Bergstrom 
104404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
1056579324aSTerje Bergstrom {
106af1cbfb9SThierry Reding 	struct host1x_client *client = job->client;
107af1cbfb9SThierry Reding 	struct device *dev = client->dev;
108fd323e9eSDmitry Osipenko 	struct host1x_job_gather *g;
109273da5a0SThierry Reding 	struct iommu_domain *domain;
1106579324aSTerje Bergstrom 	unsigned int i;
111404bfb78SMikko Perttunen 	int err;
1126579324aSTerje Bergstrom 
113273da5a0SThierry Reding 	domain = iommu_get_domain_for_dev(dev);
1146579324aSTerje Bergstrom 	job->num_unpins = 0;
1156579324aSTerje Bergstrom 
1166579324aSTerje Bergstrom 	for (i = 0; i < job->num_relocs; i++) {
11706490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
118af1cbfb9SThierry Reding 		dma_addr_t phys_addr, *phys;
1196579324aSTerje Bergstrom 		struct sg_table *sgt;
1206579324aSTerje Bergstrom 
121961e3beaSThierry Reding 		reloc->target.bo = host1x_bo_get(reloc->target.bo);
122404bfb78SMikko Perttunen 		if (!reloc->target.bo) {
123404bfb78SMikko Perttunen 			err = -EINVAL;
1246579324aSTerje Bergstrom 			goto unpin;
125404bfb78SMikko Perttunen 		}
1266579324aSTerje Bergstrom 
127273da5a0SThierry Reding 		/*
128273da5a0SThierry Reding 		 * If the client device is not attached to an IOMMU, the
129273da5a0SThierry Reding 		 * physical address of the buffer object can be used.
130273da5a0SThierry Reding 		 *
131273da5a0SThierry Reding 		 * Similarly, when an IOMMU domain is shared between all
132273da5a0SThierry Reding 		 * host1x clients, the IOVA is already available, so no
133273da5a0SThierry Reding 		 * need to map the buffer object again.
134273da5a0SThierry Reding 		 *
135273da5a0SThierry Reding 		 * XXX Note that this isn't always safe to do because it
136273da5a0SThierry Reding 		 * relies on an assumption that no cache maintenance is
137273da5a0SThierry Reding 		 * needed on the buffer objects.
138273da5a0SThierry Reding 		 */
139273da5a0SThierry Reding 		if (!domain || client->group)
140af1cbfb9SThierry Reding 			phys = &phys_addr;
141af1cbfb9SThierry Reding 		else
142af1cbfb9SThierry Reding 			phys = NULL;
143af1cbfb9SThierry Reding 
144af1cbfb9SThierry Reding 		sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
14580327ce3SThierry Reding 		if (IS_ERR(sgt)) {
14680327ce3SThierry Reding 			err = PTR_ERR(sgt);
14780327ce3SThierry Reding 			goto unpin;
14880327ce3SThierry Reding 		}
1496579324aSTerje Bergstrom 
150af1cbfb9SThierry Reding 		if (sgt) {
151af1cbfb9SThierry Reding 			unsigned long mask = HOST1X_RELOC_READ |
152af1cbfb9SThierry Reding 					     HOST1X_RELOC_WRITE;
153af1cbfb9SThierry Reding 			enum dma_data_direction dir;
154af1cbfb9SThierry Reding 
155af1cbfb9SThierry Reding 			switch (reloc->flags & mask) {
156af1cbfb9SThierry Reding 			case HOST1X_RELOC_READ:
157af1cbfb9SThierry Reding 				dir = DMA_TO_DEVICE;
158af1cbfb9SThierry Reding 				break;
159af1cbfb9SThierry Reding 
160af1cbfb9SThierry Reding 			case HOST1X_RELOC_WRITE:
161af1cbfb9SThierry Reding 				dir = DMA_FROM_DEVICE;
162af1cbfb9SThierry Reding 				break;
163af1cbfb9SThierry Reding 
164af1cbfb9SThierry Reding 			case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
165af1cbfb9SThierry Reding 				dir = DMA_BIDIRECTIONAL;
166af1cbfb9SThierry Reding 				break;
167af1cbfb9SThierry Reding 
168af1cbfb9SThierry Reding 			default:
169af1cbfb9SThierry Reding 				err = -EINVAL;
170af1cbfb9SThierry Reding 				goto unpin;
171af1cbfb9SThierry Reding 			}
172af1cbfb9SThierry Reding 
173*67ed9f9dSMarek Szyprowski 			err = dma_map_sgtable(dev, sgt, dir, 0);
174*67ed9f9dSMarek Szyprowski 			if (err)
175af1cbfb9SThierry Reding 				goto unpin;
176af1cbfb9SThierry Reding 
177af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dev = dev;
178af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dir = dir;
179af1cbfb9SThierry Reding 			phys_addr = sg_dma_address(sgt->sgl);
180af1cbfb9SThierry Reding 		}
181af1cbfb9SThierry Reding 
1826579324aSTerje Bergstrom 		job->addr_phys[job->num_unpins] = phys_addr;
183961e3beaSThierry Reding 		job->unpins[job->num_unpins].bo = reloc->target.bo;
1846579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
1856579324aSTerje Bergstrom 		job->num_unpins++;
1866579324aSTerje Bergstrom 	}
1876579324aSTerje Bergstrom 
18826c8de5eSDmitry Osipenko 	/*
18926c8de5eSDmitry Osipenko 	 * We will copy gathers BO content later, so there is no need to
19026c8de5eSDmitry Osipenko 	 * hold and pin them.
19126c8de5eSDmitry Osipenko 	 */
19226c8de5eSDmitry Osipenko 	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
19326c8de5eSDmitry Osipenko 		return 0;
19426c8de5eSDmitry Osipenko 
1956579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
196404bfb78SMikko Perttunen 		size_t gather_size = 0;
197404bfb78SMikko Perttunen 		struct scatterlist *sg;
1986579324aSTerje Bergstrom 		struct sg_table *sgt;
1996579324aSTerje Bergstrom 		dma_addr_t phys_addr;
200404bfb78SMikko Perttunen 		unsigned long shift;
201404bfb78SMikko Perttunen 		struct iova *alloc;
202273da5a0SThierry Reding 		dma_addr_t *phys;
203404bfb78SMikko Perttunen 		unsigned int j;
2046579324aSTerje Bergstrom 
205fd323e9eSDmitry Osipenko 		g = &job->gathers[i];
2066579324aSTerje Bergstrom 		g->bo = host1x_bo_get(g->bo);
207404bfb78SMikko Perttunen 		if (!g->bo) {
208404bfb78SMikko Perttunen 			err = -EINVAL;
2096579324aSTerje Bergstrom 			goto unpin;
210404bfb78SMikko Perttunen 		}
2116579324aSTerje Bergstrom 
212273da5a0SThierry Reding 		/**
213273da5a0SThierry Reding 		 * If the host1x is not attached to an IOMMU, there is no need
214273da5a0SThierry Reding 		 * to map the buffer object for the host1x, since the physical
215273da5a0SThierry Reding 		 * address can simply be used.
216273da5a0SThierry Reding 		 */
217273da5a0SThierry Reding 		if (!iommu_get_domain_for_dev(host->dev))
218273da5a0SThierry Reding 			phys = &phys_addr;
219273da5a0SThierry Reding 		else
220273da5a0SThierry Reding 			phys = NULL;
221273da5a0SThierry Reding 
222273da5a0SThierry Reding 		sgt = host1x_bo_pin(host->dev, g->bo, phys);
22380327ce3SThierry Reding 		if (IS_ERR(sgt)) {
22480327ce3SThierry Reding 			err = PTR_ERR(sgt);
225fd323e9eSDmitry Osipenko 			goto put;
22680327ce3SThierry Reding 		}
2276579324aSTerje Bergstrom 
22826c8de5eSDmitry Osipenko 		if (host->domain) {
229*67ed9f9dSMarek Szyprowski 			for_each_sgtable_sg(sgt, sg, j)
230404bfb78SMikko Perttunen 				gather_size += sg->length;
231404bfb78SMikko Perttunen 			gather_size = iova_align(&host->iova, gather_size);
232404bfb78SMikko Perttunen 
233404bfb78SMikko Perttunen 			shift = iova_shift(&host->iova);
234404bfb78SMikko Perttunen 			alloc = alloc_iova(&host->iova, gather_size >> shift,
235404bfb78SMikko Perttunen 					   host->iova_end >> shift, true);
236404bfb78SMikko Perttunen 			if (!alloc) {
237404bfb78SMikko Perttunen 				err = -ENOMEM;
238fd323e9eSDmitry Osipenko 				goto put;
239404bfb78SMikko Perttunen 			}
240404bfb78SMikko Perttunen 
241*67ed9f9dSMarek Szyprowski 			err = iommu_map_sgtable(host->domain,
242404bfb78SMikko Perttunen 					iova_dma_addr(&host->iova, alloc),
243*67ed9f9dSMarek Szyprowski 					sgt, IOMMU_READ);
244404bfb78SMikko Perttunen 			if (err == 0) {
245404bfb78SMikko Perttunen 				__free_iova(&host->iova, alloc);
246404bfb78SMikko Perttunen 				err = -EINVAL;
247fd323e9eSDmitry Osipenko 				goto put;
248404bfb78SMikko Perttunen 			}
249404bfb78SMikko Perttunen 
250404bfb78SMikko Perttunen 			job->unpins[job->num_unpins].size = gather_size;
251af1cbfb9SThierry Reding 			phys_addr = iova_dma_addr(&host->iova, alloc);
252273da5a0SThierry Reding 		} else if (sgt) {
253*67ed9f9dSMarek Szyprowski 			err = dma_map_sgtable(host->dev, sgt, DMA_TO_DEVICE, 0);
254*67ed9f9dSMarek Szyprowski 			if (err)
255fd323e9eSDmitry Osipenko 				goto put;
256404bfb78SMikko Perttunen 
25798ae41adSThierry Reding 			job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
258af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dev = host->dev;
259af1cbfb9SThierry Reding 			phys_addr = sg_dma_address(sgt->sgl);
260af1cbfb9SThierry Reding 		}
261404bfb78SMikko Perttunen 
262af1cbfb9SThierry Reding 		job->addr_phys[job->num_unpins] = phys_addr;
263af1cbfb9SThierry Reding 		job->gather_addr_phys[i] = phys_addr;
264af1cbfb9SThierry Reding 
2656579324aSTerje Bergstrom 		job->unpins[job->num_unpins].bo = g->bo;
2666579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
2676579324aSTerje Bergstrom 		job->num_unpins++;
2686579324aSTerje Bergstrom 	}
2696579324aSTerje Bergstrom 
270404bfb78SMikko Perttunen 	return 0;
2716579324aSTerje Bergstrom 
272fd323e9eSDmitry Osipenko put:
273fd323e9eSDmitry Osipenko 	host1x_bo_put(g->bo);
2746579324aSTerje Bergstrom unpin:
2756579324aSTerje Bergstrom 	host1x_job_unpin(job);
276404bfb78SMikko Perttunen 	return err;
2776579324aSTerje Bergstrom }
2786579324aSTerje Bergstrom 
27947f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
2806579324aSTerje Bergstrom {
2817a8139c5SDaniel Vetter 	void *cmdbuf_addr = NULL;
28247f89c10SDmitry Osipenko 	struct host1x_bo *cmdbuf = g->bo;
283d4ad3ad9SThierry Reding 	unsigned int i;
2846579324aSTerje Bergstrom 
2856579324aSTerje Bergstrom 	/* pin & patch the relocs for one gather */
2863364cd28SArto Merilainen 	for (i = 0; i < job->num_relocs; i++) {
28706490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
2886579324aSTerje Bergstrom 		u32 reloc_addr = (job->reloc_addr_phys[i] +
289961e3beaSThierry Reding 				  reloc->target.offset) >> reloc->shift;
2906579324aSTerje Bergstrom 		u32 *target;
2916579324aSTerje Bergstrom 
2926579324aSTerje Bergstrom 		/* skip all other gathers */
293961e3beaSThierry Reding 		if (cmdbuf != reloc->cmdbuf.bo)
2946579324aSTerje Bergstrom 			continue;
2956579324aSTerje Bergstrom 
29647f89c10SDmitry Osipenko 		if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
29747f89c10SDmitry Osipenko 			target = (u32 *)job->gather_copy_mapped +
29847f89c10SDmitry Osipenko 					reloc->cmdbuf.offset / sizeof(u32) +
29947f89c10SDmitry Osipenko 						g->offset / sizeof(u32);
30047f89c10SDmitry Osipenko 			goto patch_reloc;
30147f89c10SDmitry Osipenko 		}
30247f89c10SDmitry Osipenko 
3037a8139c5SDaniel Vetter 		if (!cmdbuf_addr) {
3047a8139c5SDaniel Vetter 			cmdbuf_addr = host1x_bo_mmap(cmdbuf);
3056579324aSTerje Bergstrom 
3067a8139c5SDaniel Vetter 			if (unlikely(!cmdbuf_addr)) {
3076579324aSTerje Bergstrom 				pr_err("Could not map cmdbuf for relocation\n");
3086579324aSTerje Bergstrom 				return -ENOMEM;
3096579324aSTerje Bergstrom 			}
3106579324aSTerje Bergstrom 		}
3116579324aSTerje Bergstrom 
3127a8139c5SDaniel Vetter 		target = cmdbuf_addr + reloc->cmdbuf.offset;
31347f89c10SDmitry Osipenko patch_reloc:
3146579324aSTerje Bergstrom 		*target = reloc_addr;
3156579324aSTerje Bergstrom 	}
3166579324aSTerje Bergstrom 
3177a8139c5SDaniel Vetter 	if (cmdbuf_addr)
3187a8139c5SDaniel Vetter 		host1x_bo_munmap(cmdbuf, cmdbuf_addr);
3196579324aSTerje Bergstrom 
3206579324aSTerje Bergstrom 	return 0;
3216579324aSTerje Bergstrom }
3226579324aSTerje Bergstrom 
3235060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
3246579324aSTerje Bergstrom 			unsigned int offset)
3256579324aSTerje Bergstrom {
3266579324aSTerje Bergstrom 	offset *= sizeof(u32);
3276579324aSTerje Bergstrom 
328961e3beaSThierry Reding 	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
3295060d8ecSArto Merilainen 		return false;
3306579324aSTerje Bergstrom 
331571cbf70SDmitry Osipenko 	/* relocation shift value validation isn't implemented yet */
332571cbf70SDmitry Osipenko 	if (reloc->shift)
333571cbf70SDmitry Osipenko 		return false;
334571cbf70SDmitry Osipenko 
3355060d8ecSArto Merilainen 	return true;
3366579324aSTerje Bergstrom }
3376579324aSTerje Bergstrom 
3386579324aSTerje Bergstrom struct host1x_firewall {
3396579324aSTerje Bergstrom 	struct host1x_job *job;
3406579324aSTerje Bergstrom 	struct device *dev;
3416579324aSTerje Bergstrom 
3426579324aSTerje Bergstrom 	unsigned int num_relocs;
3436579324aSTerje Bergstrom 	struct host1x_reloc *reloc;
3446579324aSTerje Bergstrom 
345d7fbcf47SThierry Reding 	struct host1x_bo *cmdbuf;
3466579324aSTerje Bergstrom 	unsigned int offset;
3476579324aSTerje Bergstrom 
3486579324aSTerje Bergstrom 	u32 words;
3496579324aSTerje Bergstrom 	u32 class;
3506579324aSTerje Bergstrom 	u32 reg;
3516579324aSTerje Bergstrom 	u32 mask;
3526579324aSTerje Bergstrom 	u32 count;
3536579324aSTerje Bergstrom };
3546579324aSTerje Bergstrom 
355d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset)
356d77563ffSThierry Reding {
3570f563a4bSDmitry Osipenko 	if (!fw->job->is_addr_reg)
3580f563a4bSDmitry Osipenko 		return 0;
3590f563a4bSDmitry Osipenko 
360d77563ffSThierry Reding 	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
361d77563ffSThierry Reding 		if (!fw->num_relocs)
362d77563ffSThierry Reding 			return -EINVAL;
363d77563ffSThierry Reding 
364d77563ffSThierry Reding 		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
365d77563ffSThierry Reding 			return -EINVAL;
366d77563ffSThierry Reding 
367d77563ffSThierry Reding 		fw->num_relocs--;
368d77563ffSThierry Reding 		fw->reloc++;
369d77563ffSThierry Reding 	}
370d77563ffSThierry Reding 
371d77563ffSThierry Reding 	return 0;
372d77563ffSThierry Reding }
373d77563ffSThierry Reding 
3740f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class)
3750f563a4bSDmitry Osipenko {
3760f563a4bSDmitry Osipenko 	if (!fw->job->is_valid_class) {
3770f563a4bSDmitry Osipenko 		if (fw->class != class)
3780f563a4bSDmitry Osipenko 			return -EINVAL;
3790f563a4bSDmitry Osipenko 	} else {
3800f563a4bSDmitry Osipenko 		if (!fw->job->is_valid_class(fw->class))
3810f563a4bSDmitry Osipenko 			return -EINVAL;
3820f563a4bSDmitry Osipenko 	}
3830f563a4bSDmitry Osipenko 
3840f563a4bSDmitry Osipenko 	return 0;
3850f563a4bSDmitry Osipenko }
3860f563a4bSDmitry Osipenko 
3876579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw)
3886579324aSTerje Bergstrom {
3896579324aSTerje Bergstrom 	u32 mask = fw->mask;
3906579324aSTerje Bergstrom 	u32 reg = fw->reg;
391d77563ffSThierry Reding 	int ret;
3926579324aSTerje Bergstrom 
3936579324aSTerje Bergstrom 	while (mask) {
3946579324aSTerje Bergstrom 		if (fw->words == 0)
3956579324aSTerje Bergstrom 			return -EINVAL;
3966579324aSTerje Bergstrom 
3976579324aSTerje Bergstrom 		if (mask & 1) {
398d77563ffSThierry Reding 			ret = check_register(fw, reg);
399d77563ffSThierry Reding 			if (ret < 0)
400d77563ffSThierry Reding 				return ret;
401d77563ffSThierry Reding 
4026579324aSTerje Bergstrom 			fw->words--;
4036579324aSTerje Bergstrom 			fw->offset++;
4046579324aSTerje Bergstrom 		}
4056579324aSTerje Bergstrom 		mask >>= 1;
4066579324aSTerje Bergstrom 		reg++;
4076579324aSTerje Bergstrom 	}
4086579324aSTerje Bergstrom 
4096579324aSTerje Bergstrom 	return 0;
4106579324aSTerje Bergstrom }
4116579324aSTerje Bergstrom 
4126579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw)
4136579324aSTerje Bergstrom {
4146579324aSTerje Bergstrom 	u32 count = fw->count;
4156579324aSTerje Bergstrom 	u32 reg = fw->reg;
416d77563ffSThierry Reding 	int ret;
4176579324aSTerje Bergstrom 
41864c173d3STerje Bergstrom 	while (count) {
4196579324aSTerje Bergstrom 		if (fw->words == 0)
4206579324aSTerje Bergstrom 			return -EINVAL;
4216579324aSTerje Bergstrom 
422d77563ffSThierry Reding 		ret = check_register(fw, reg);
423d77563ffSThierry Reding 		if (ret < 0)
424d77563ffSThierry Reding 			return ret;
425d77563ffSThierry Reding 
4266579324aSTerje Bergstrom 		reg++;
4276579324aSTerje Bergstrom 		fw->words--;
4286579324aSTerje Bergstrom 		fw->offset++;
4296579324aSTerje Bergstrom 		count--;
4306579324aSTerje Bergstrom 	}
4316579324aSTerje Bergstrom 
4326579324aSTerje Bergstrom 	return 0;
4336579324aSTerje Bergstrom }
4346579324aSTerje Bergstrom 
4356579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw)
4366579324aSTerje Bergstrom {
4376579324aSTerje Bergstrom 	u32 count = fw->count;
438d77563ffSThierry Reding 	int ret;
4396579324aSTerje Bergstrom 
4406579324aSTerje Bergstrom 	while (count) {
4416579324aSTerje Bergstrom 		if (fw->words == 0)
4426579324aSTerje Bergstrom 			return -EINVAL;
4436579324aSTerje Bergstrom 
444d77563ffSThierry Reding 		ret = check_register(fw, fw->reg);
445d77563ffSThierry Reding 		if (ret < 0)
446d77563ffSThierry Reding 			return ret;
447d77563ffSThierry Reding 
4486579324aSTerje Bergstrom 		fw->words--;
4496579324aSTerje Bergstrom 		fw->offset++;
4506579324aSTerje Bergstrom 		count--;
4516579324aSTerje Bergstrom 	}
4526579324aSTerje Bergstrom 
4536579324aSTerje Bergstrom 	return 0;
4546579324aSTerje Bergstrom }
4556579324aSTerje Bergstrom 
456afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
4576579324aSTerje Bergstrom {
4583364cd28SArto Merilainen 	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
4593364cd28SArto Merilainen 		(g->offset / sizeof(u32));
4600f563a4bSDmitry Osipenko 	u32 job_class = fw->class;
4616579324aSTerje Bergstrom 	int err = 0;
4626579324aSTerje Bergstrom 
463afac0e43STerje Bergstrom 	fw->words = g->words;
464d7fbcf47SThierry Reding 	fw->cmdbuf = g->bo;
465afac0e43STerje Bergstrom 	fw->offset = 0;
4666579324aSTerje Bergstrom 
467afac0e43STerje Bergstrom 	while (fw->words && !err) {
468afac0e43STerje Bergstrom 		u32 word = cmdbuf_base[fw->offset];
4696579324aSTerje Bergstrom 		u32 opcode = (word & 0xf0000000) >> 28;
4706579324aSTerje Bergstrom 
471afac0e43STerje Bergstrom 		fw->mask = 0;
472afac0e43STerje Bergstrom 		fw->reg = 0;
473afac0e43STerje Bergstrom 		fw->count = 0;
474afac0e43STerje Bergstrom 		fw->words--;
475afac0e43STerje Bergstrom 		fw->offset++;
4766579324aSTerje Bergstrom 
4776579324aSTerje Bergstrom 		switch (opcode) {
4786579324aSTerje Bergstrom 		case 0:
479afac0e43STerje Bergstrom 			fw->class = word >> 6 & 0x3ff;
480afac0e43STerje Bergstrom 			fw->mask = word & 0x3f;
481afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
4820f563a4bSDmitry Osipenko 			err = check_class(fw, job_class);
4830f563a4bSDmitry Osipenko 			if (!err)
484afac0e43STerje Bergstrom 				err = check_mask(fw);
4856579324aSTerje Bergstrom 			if (err)
4866579324aSTerje Bergstrom 				goto out;
4876579324aSTerje Bergstrom 			break;
4886579324aSTerje Bergstrom 		case 1:
489afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
490afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
491afac0e43STerje Bergstrom 			err = check_incr(fw);
4926579324aSTerje Bergstrom 			if (err)
4936579324aSTerje Bergstrom 				goto out;
4946579324aSTerje Bergstrom 			break;
4956579324aSTerje Bergstrom 
4966579324aSTerje Bergstrom 		case 2:
497afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
498afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
499afac0e43STerje Bergstrom 			err = check_nonincr(fw);
5006579324aSTerje Bergstrom 			if (err)
5016579324aSTerje Bergstrom 				goto out;
5026579324aSTerje Bergstrom 			break;
5036579324aSTerje Bergstrom 
5046579324aSTerje Bergstrom 		case 3:
505afac0e43STerje Bergstrom 			fw->mask = word & 0xffff;
506afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
507afac0e43STerje Bergstrom 			err = check_mask(fw);
5086579324aSTerje Bergstrom 			if (err)
5096579324aSTerje Bergstrom 				goto out;
5106579324aSTerje Bergstrom 			break;
5116579324aSTerje Bergstrom 		case 4:
5126579324aSTerje Bergstrom 		case 14:
5136579324aSTerje Bergstrom 			break;
5146579324aSTerje Bergstrom 		default:
5156579324aSTerje Bergstrom 			err = -EINVAL;
5166579324aSTerje Bergstrom 			break;
5176579324aSTerje Bergstrom 		}
5186579324aSTerje Bergstrom 	}
5196579324aSTerje Bergstrom 
5206579324aSTerje Bergstrom out:
5216579324aSTerje Bergstrom 	return err;
5226579324aSTerje Bergstrom }
5236579324aSTerje Bergstrom 
524b78e70c0SThierry Reding static inline int copy_gathers(struct device *host, struct host1x_job *job,
525b78e70c0SThierry Reding 			       struct device *dev)
5266579324aSTerje Bergstrom {
5273364cd28SArto Merilainen 	struct host1x_firewall fw;
5286579324aSTerje Bergstrom 	size_t size = 0;
5296579324aSTerje Bergstrom 	size_t offset = 0;
530d4ad3ad9SThierry Reding 	unsigned int i;
5316579324aSTerje Bergstrom 
5323364cd28SArto Merilainen 	fw.job = job;
5333364cd28SArto Merilainen 	fw.dev = dev;
53406490bb9SThierry Reding 	fw.reloc = job->relocs;
5353364cd28SArto Merilainen 	fw.num_relocs = job->num_relocs;
5363833d16fSDmitry Osipenko 	fw.class = job->class;
5373364cd28SArto Merilainen 
5386579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
5396579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
5406df633d0SThierry Reding 
5416579324aSTerje Bergstrom 		size += g->words * sizeof(u32);
5426579324aSTerje Bergstrom 	}
5436579324aSTerje Bergstrom 
54443240bbdSDmitry Osipenko 	/*
54543240bbdSDmitry Osipenko 	 * Try a non-blocking allocation from a higher priority pools first,
54643240bbdSDmitry Osipenko 	 * as awaiting for the allocation here is a major performance hit.
54743240bbdSDmitry Osipenko 	 */
548b78e70c0SThierry Reding 	job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
54943240bbdSDmitry Osipenko 					       GFP_NOWAIT);
55043240bbdSDmitry Osipenko 
55143240bbdSDmitry Osipenko 	/* the higher priority allocation failed, try the generic-blocking */
55243240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
553b78e70c0SThierry Reding 		job->gather_copy_mapped = dma_alloc_wc(host, size,
55443240bbdSDmitry Osipenko 						       &job->gather_copy,
5556579324aSTerje Bergstrom 						       GFP_KERNEL);
55643240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
557745cecc0SDan Carpenter 		return -ENOMEM;
5586579324aSTerje Bergstrom 
5596579324aSTerje Bergstrom 	job->gather_copy_size = size;
5606579324aSTerje Bergstrom 
5616579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
5626579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
5636579324aSTerje Bergstrom 		void *gather;
5646579324aSTerje Bergstrom 
5653364cd28SArto Merilainen 		/* Copy the gather */
5666579324aSTerje Bergstrom 		gather = host1x_bo_mmap(g->bo);
5676579324aSTerje Bergstrom 		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
5686579324aSTerje Bergstrom 		       g->words * sizeof(u32));
5696579324aSTerje Bergstrom 		host1x_bo_munmap(g->bo, gather);
5706579324aSTerje Bergstrom 
5713364cd28SArto Merilainen 		/* Store the location in the buffer */
5726579324aSTerje Bergstrom 		g->base = job->gather_copy;
5736579324aSTerje Bergstrom 		g->offset = offset;
5743364cd28SArto Merilainen 
5753364cd28SArto Merilainen 		/* Validate the job */
5763364cd28SArto Merilainen 		if (validate(&fw, g))
5773364cd28SArto Merilainen 			return -EINVAL;
5786579324aSTerje Bergstrom 
5796579324aSTerje Bergstrom 		offset += g->words * sizeof(u32);
5806579324aSTerje Bergstrom 	}
5816579324aSTerje Bergstrom 
58224c94e16SThierry Reding 	/* No relocs should remain at this point */
58324c94e16SThierry Reding 	if (fw.num_relocs)
584a9ff9995SErik Faye-Lund 		return -EINVAL;
585a9ff9995SErik Faye-Lund 
5866579324aSTerje Bergstrom 	return 0;
5876579324aSTerje Bergstrom }
5886579324aSTerje Bergstrom 
5896579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev)
5906579324aSTerje Bergstrom {
5916579324aSTerje Bergstrom 	int err;
5926579324aSTerje Bergstrom 	unsigned int i, j;
5936579324aSTerje Bergstrom 	struct host1x *host = dev_get_drvdata(dev->parent);
5946579324aSTerje Bergstrom 
5956579324aSTerje Bergstrom 	/* pin memory */
596404bfb78SMikko Perttunen 	err = pin_job(host, job);
597404bfb78SMikko Perttunen 	if (err)
5986579324aSTerje Bergstrom 		goto out;
5996579324aSTerje Bergstrom 
60047f89c10SDmitry Osipenko 	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
601b78e70c0SThierry Reding 		err = copy_gathers(host->dev, job, dev);
60247f89c10SDmitry Osipenko 		if (err)
60347f89c10SDmitry Osipenko 			goto out;
60447f89c10SDmitry Osipenko 	}
60547f89c10SDmitry Osipenko 
6066579324aSTerje Bergstrom 	/* patch gathers */
6076579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
6086579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
6096579324aSTerje Bergstrom 
6106579324aSTerje Bergstrom 		/* process each gather mem only once */
6116579324aSTerje Bergstrom 		if (g->handled)
6126579324aSTerje Bergstrom 			continue;
6136579324aSTerje Bergstrom 
61447f89c10SDmitry Osipenko 		/* copy_gathers() sets gathers base if firewall is enabled */
61547f89c10SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
6166579324aSTerje Bergstrom 			g->base = job->gather_addr_phys[i];
6176579324aSTerje Bergstrom 
618f08ef2d1SArto Merilainen 		for (j = i + 1; j < job->num_gathers; j++) {
619f08ef2d1SArto Merilainen 			if (job->gathers[j].bo == g->bo) {
6206579324aSTerje Bergstrom 				job->gathers[j].handled = true;
621f08ef2d1SArto Merilainen 				job->gathers[j].base = g->base;
622f08ef2d1SArto Merilainen 			}
623f08ef2d1SArto Merilainen 		}
6246579324aSTerje Bergstrom 
62547f89c10SDmitry Osipenko 		err = do_relocs(job, g);
6263364cd28SArto Merilainen 		if (err)
62747f89c10SDmitry Osipenko 			break;
6286579324aSTerje Bergstrom 	}
6296579324aSTerje Bergstrom 
6306579324aSTerje Bergstrom out:
631e5855aa3SDmitry Osipenko 	if (err)
632e5855aa3SDmitry Osipenko 		host1x_job_unpin(job);
6336579324aSTerje Bergstrom 	wmb();
6346579324aSTerje Bergstrom 
6356579324aSTerje Bergstrom 	return err;
6366579324aSTerje Bergstrom }
637fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin);
6386579324aSTerje Bergstrom 
6396579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job)
6406579324aSTerje Bergstrom {
641404bfb78SMikko Perttunen 	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
6426579324aSTerje Bergstrom 	unsigned int i;
6436579324aSTerje Bergstrom 
6446579324aSTerje Bergstrom 	for (i = 0; i < job->num_unpins; i++) {
6456579324aSTerje Bergstrom 		struct host1x_job_unpin_data *unpin = &job->unpins[i];
646af1cbfb9SThierry Reding 		struct device *dev = unpin->dev ?: host->dev;
647af1cbfb9SThierry Reding 		struct sg_table *sgt = unpin->sgt;
6486df633d0SThierry Reding 
649ec589232SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
650ec589232SDmitry Osipenko 		    unpin->size && host->domain) {
651404bfb78SMikko Perttunen 			iommu_unmap(host->domain, job->addr_phys[i],
652404bfb78SMikko Perttunen 				    unpin->size);
653404bfb78SMikko Perttunen 			free_iova(&host->iova,
654404bfb78SMikko Perttunen 				iova_pfn(&host->iova, job->addr_phys[i]));
655404bfb78SMikko Perttunen 		}
656404bfb78SMikko Perttunen 
657af1cbfb9SThierry Reding 		if (unpin->dev && sgt)
658*67ed9f9dSMarek Szyprowski 			dma_unmap_sgtable(unpin->dev, sgt, unpin->dir, 0);
659af1cbfb9SThierry Reding 
660af1cbfb9SThierry Reding 		host1x_bo_unpin(dev, unpin->bo, sgt);
6616579324aSTerje Bergstrom 		host1x_bo_put(unpin->bo);
6626579324aSTerje Bergstrom 	}
6630b8070d1SThierry Reding 
6646579324aSTerje Bergstrom 	job->num_unpins = 0;
6656579324aSTerje Bergstrom 
6666579324aSTerje Bergstrom 	if (job->gather_copy_size)
667b78e70c0SThierry Reding 		dma_free_wc(host->dev, job->gather_copy_size,
668f6e45661SLuis R. Rodriguez 			    job->gather_copy_mapped, job->gather_copy);
6696579324aSTerje Bergstrom }
670fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_unpin);
6716579324aSTerje Bergstrom 
6726579324aSTerje Bergstrom /*
6736579324aSTerje Bergstrom  * Debug routine used to dump job entries
6746579324aSTerje Bergstrom  */
6756579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job)
6766579324aSTerje Bergstrom {
6776579324aSTerje Bergstrom 	dev_dbg(dev, "    SYNCPT_ID   %d\n", job->syncpt_id);
6786579324aSTerje Bergstrom 	dev_dbg(dev, "    SYNCPT_VAL  %d\n", job->syncpt_end);
6796579324aSTerje Bergstrom 	dev_dbg(dev, "    FIRST_GET   0x%x\n", job->first_get);
6806579324aSTerje Bergstrom 	dev_dbg(dev, "    TIMEOUT     %d\n", job->timeout);
6816579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_SLOTS   %d\n", job->num_slots);
6826579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_HANDLES %d\n", job->num_unpins);
6836579324aSTerje Bergstrom }
684