xref: /linux/drivers/gpu/host1x/job.c (revision 98ae41adb252866158dd4e998551dfa85e612bed)
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;
306579324aSTerje Bergstrom 	unsigned int num_unpins = num_cmdbufs + num_relocs;
316579324aSTerje Bergstrom 	u64 total;
326579324aSTerje Bergstrom 	void *mem;
336579324aSTerje Bergstrom 
346579324aSTerje Bergstrom 	/* Check that we're not going to overflow */
356579324aSTerje Bergstrom 	total = sizeof(struct host1x_job) +
36f5fda676SDan Carpenter 		(u64)num_relocs * sizeof(struct host1x_reloc) +
37f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
38f5fda676SDan Carpenter 		(u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
39f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(dma_addr_t) +
40f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(u32 *);
416579324aSTerje Bergstrom 	if (total > ULONG_MAX)
426579324aSTerje Bergstrom 		return NULL;
436579324aSTerje Bergstrom 
446579324aSTerje Bergstrom 	mem = job = kzalloc(total, GFP_KERNEL);
456579324aSTerje Bergstrom 	if (!job)
466579324aSTerje Bergstrom 		return NULL;
476579324aSTerje Bergstrom 
486579324aSTerje Bergstrom 	kref_init(&job->ref);
496579324aSTerje Bergstrom 	job->channel = ch;
506579324aSTerje Bergstrom 
516579324aSTerje Bergstrom 	/* Redistribute memory to the structs  */
526579324aSTerje Bergstrom 	mem += sizeof(struct host1x_job);
5306490bb9SThierry Reding 	job->relocs = num_relocs ? mem : NULL;
546579324aSTerje Bergstrom 	mem += num_relocs * sizeof(struct host1x_reloc);
556579324aSTerje Bergstrom 	job->unpins = num_unpins ? mem : NULL;
566579324aSTerje Bergstrom 	mem += num_unpins * sizeof(struct host1x_job_unpin_data);
576579324aSTerje Bergstrom 	job->gathers = num_cmdbufs ? mem : NULL;
586579324aSTerje Bergstrom 	mem += num_cmdbufs * sizeof(struct host1x_job_gather);
596579324aSTerje Bergstrom 	job->addr_phys = num_unpins ? mem : NULL;
606579324aSTerje Bergstrom 
616579324aSTerje Bergstrom 	job->reloc_addr_phys = job->addr_phys;
626579324aSTerje Bergstrom 	job->gather_addr_phys = &job->addr_phys[num_relocs];
636579324aSTerje Bergstrom 
646579324aSTerje Bergstrom 	return job;
656579324aSTerje Bergstrom }
66fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_alloc);
676579324aSTerje Bergstrom 
686579324aSTerje Bergstrom struct host1x_job *host1x_job_get(struct host1x_job *job)
696579324aSTerje Bergstrom {
706579324aSTerje Bergstrom 	kref_get(&job->ref);
716579324aSTerje Bergstrom 	return job;
726579324aSTerje Bergstrom }
73fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_get);
746579324aSTerje Bergstrom 
756579324aSTerje Bergstrom static void job_free(struct kref *ref)
766579324aSTerje Bergstrom {
776579324aSTerje Bergstrom 	struct host1x_job *job = container_of(ref, struct host1x_job, ref);
786579324aSTerje Bergstrom 
796579324aSTerje Bergstrom 	kfree(job);
806579324aSTerje Bergstrom }
816579324aSTerje Bergstrom 
826579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job)
836579324aSTerje Bergstrom {
846579324aSTerje Bergstrom 	kref_put(&job->ref, job_free);
856579324aSTerje Bergstrom }
86fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put);
876579324aSTerje Bergstrom 
886579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
89326bbd79SThierry Reding 			   unsigned int words, unsigned int offset)
906579324aSTerje Bergstrom {
91326bbd79SThierry Reding 	struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
926579324aSTerje Bergstrom 
93326bbd79SThierry Reding 	gather->words = words;
94326bbd79SThierry Reding 	gather->bo = bo;
95326bbd79SThierry Reding 	gather->offset = offset;
96326bbd79SThierry Reding 
976579324aSTerje Bergstrom 	job->num_gathers++;
986579324aSTerje Bergstrom }
99fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather);
1006579324aSTerje Bergstrom 
101404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
1026579324aSTerje Bergstrom {
103af1cbfb9SThierry Reding 	struct host1x_client *client = job->client;
104af1cbfb9SThierry Reding 	struct device *dev = client->dev;
105273da5a0SThierry Reding 	struct iommu_domain *domain;
1066579324aSTerje Bergstrom 	unsigned int i;
107404bfb78SMikko Perttunen 	int err;
1086579324aSTerje Bergstrom 
109273da5a0SThierry Reding 	domain = iommu_get_domain_for_dev(dev);
1106579324aSTerje Bergstrom 	job->num_unpins = 0;
1116579324aSTerje Bergstrom 
1126579324aSTerje Bergstrom 	for (i = 0; i < job->num_relocs; i++) {
11306490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
114af1cbfb9SThierry Reding 		dma_addr_t phys_addr, *phys;
1156579324aSTerje Bergstrom 		struct sg_table *sgt;
1166579324aSTerje Bergstrom 
117961e3beaSThierry Reding 		reloc->target.bo = host1x_bo_get(reloc->target.bo);
118404bfb78SMikko Perttunen 		if (!reloc->target.bo) {
119404bfb78SMikko Perttunen 			err = -EINVAL;
1206579324aSTerje Bergstrom 			goto unpin;
121404bfb78SMikko Perttunen 		}
1226579324aSTerje Bergstrom 
123273da5a0SThierry Reding 		/*
124273da5a0SThierry Reding 		 * If the client device is not attached to an IOMMU, the
125273da5a0SThierry Reding 		 * physical address of the buffer object can be used.
126273da5a0SThierry Reding 		 *
127273da5a0SThierry Reding 		 * Similarly, when an IOMMU domain is shared between all
128273da5a0SThierry Reding 		 * host1x clients, the IOVA is already available, so no
129273da5a0SThierry Reding 		 * need to map the buffer object again.
130273da5a0SThierry Reding 		 *
131273da5a0SThierry Reding 		 * XXX Note that this isn't always safe to do because it
132273da5a0SThierry Reding 		 * relies on an assumption that no cache maintenance is
133273da5a0SThierry Reding 		 * needed on the buffer objects.
134273da5a0SThierry Reding 		 */
135273da5a0SThierry Reding 		if (!domain || client->group)
136af1cbfb9SThierry Reding 			phys = &phys_addr;
137af1cbfb9SThierry Reding 		else
138af1cbfb9SThierry Reding 			phys = NULL;
139af1cbfb9SThierry Reding 
140af1cbfb9SThierry Reding 		sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
14180327ce3SThierry Reding 		if (IS_ERR(sgt)) {
14280327ce3SThierry Reding 			err = PTR_ERR(sgt);
14380327ce3SThierry Reding 			goto unpin;
14480327ce3SThierry Reding 		}
1456579324aSTerje Bergstrom 
146af1cbfb9SThierry Reding 		if (sgt) {
147af1cbfb9SThierry Reding 			unsigned long mask = HOST1X_RELOC_READ |
148af1cbfb9SThierry Reding 					     HOST1X_RELOC_WRITE;
149af1cbfb9SThierry Reding 			enum dma_data_direction dir;
150af1cbfb9SThierry Reding 
151af1cbfb9SThierry Reding 			switch (reloc->flags & mask) {
152af1cbfb9SThierry Reding 			case HOST1X_RELOC_READ:
153af1cbfb9SThierry Reding 				dir = DMA_TO_DEVICE;
154af1cbfb9SThierry Reding 				break;
155af1cbfb9SThierry Reding 
156af1cbfb9SThierry Reding 			case HOST1X_RELOC_WRITE:
157af1cbfb9SThierry Reding 				dir = DMA_FROM_DEVICE;
158af1cbfb9SThierry Reding 				break;
159af1cbfb9SThierry Reding 
160af1cbfb9SThierry Reding 			case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
161af1cbfb9SThierry Reding 				dir = DMA_BIDIRECTIONAL;
162af1cbfb9SThierry Reding 				break;
163af1cbfb9SThierry Reding 
164af1cbfb9SThierry Reding 			default:
165af1cbfb9SThierry Reding 				err = -EINVAL;
166af1cbfb9SThierry Reding 				goto unpin;
167af1cbfb9SThierry Reding 			}
168af1cbfb9SThierry Reding 
169af1cbfb9SThierry Reding 			err = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
170af1cbfb9SThierry Reding 			if (!err) {
171af1cbfb9SThierry Reding 				err = -ENOMEM;
172af1cbfb9SThierry Reding 				goto unpin;
173af1cbfb9SThierry Reding 			}
174af1cbfb9SThierry Reding 
175af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dev = dev;
176af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dir = dir;
177af1cbfb9SThierry Reding 			phys_addr = sg_dma_address(sgt->sgl);
178af1cbfb9SThierry Reding 		}
179af1cbfb9SThierry Reding 
1806579324aSTerje Bergstrom 		job->addr_phys[job->num_unpins] = phys_addr;
181961e3beaSThierry Reding 		job->unpins[job->num_unpins].bo = reloc->target.bo;
1826579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
1836579324aSTerje Bergstrom 		job->num_unpins++;
1846579324aSTerje Bergstrom 	}
1856579324aSTerje Bergstrom 
1866579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
1876579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
188404bfb78SMikko Perttunen 		size_t gather_size = 0;
189404bfb78SMikko Perttunen 		struct scatterlist *sg;
1906579324aSTerje Bergstrom 		struct sg_table *sgt;
1916579324aSTerje Bergstrom 		dma_addr_t phys_addr;
192404bfb78SMikko Perttunen 		unsigned long shift;
193404bfb78SMikko Perttunen 		struct iova *alloc;
194273da5a0SThierry Reding 		dma_addr_t *phys;
195404bfb78SMikko Perttunen 		unsigned int j;
1966579324aSTerje Bergstrom 
1976579324aSTerje Bergstrom 		g->bo = host1x_bo_get(g->bo);
198404bfb78SMikko Perttunen 		if (!g->bo) {
199404bfb78SMikko Perttunen 			err = -EINVAL;
2006579324aSTerje Bergstrom 			goto unpin;
201404bfb78SMikko Perttunen 		}
2026579324aSTerje Bergstrom 
203273da5a0SThierry Reding 		/**
204273da5a0SThierry Reding 		 * If the host1x is not attached to an IOMMU, there is no need
205273da5a0SThierry Reding 		 * to map the buffer object for the host1x, since the physical
206273da5a0SThierry Reding 		 * address can simply be used.
207273da5a0SThierry Reding 		 */
208273da5a0SThierry Reding 		if (!iommu_get_domain_for_dev(host->dev))
209273da5a0SThierry Reding 			phys = &phys_addr;
210273da5a0SThierry Reding 		else
211273da5a0SThierry Reding 			phys = NULL;
212273da5a0SThierry Reding 
213273da5a0SThierry Reding 		sgt = host1x_bo_pin(host->dev, g->bo, phys);
21480327ce3SThierry Reding 		if (IS_ERR(sgt)) {
21580327ce3SThierry Reding 			err = PTR_ERR(sgt);
21680327ce3SThierry Reding 			goto unpin;
21780327ce3SThierry Reding 		}
2186579324aSTerje Bergstrom 
219404bfb78SMikko Perttunen 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
220404bfb78SMikko Perttunen 			for_each_sg(sgt->sgl, sg, sgt->nents, j)
221404bfb78SMikko Perttunen 				gather_size += sg->length;
222404bfb78SMikko Perttunen 			gather_size = iova_align(&host->iova, gather_size);
223404bfb78SMikko Perttunen 
224404bfb78SMikko Perttunen 			shift = iova_shift(&host->iova);
225404bfb78SMikko Perttunen 			alloc = alloc_iova(&host->iova, gather_size >> shift,
226404bfb78SMikko Perttunen 					   host->iova_end >> shift, true);
227404bfb78SMikko Perttunen 			if (!alloc) {
228404bfb78SMikko Perttunen 				err = -ENOMEM;
229404bfb78SMikko Perttunen 				goto unpin;
230404bfb78SMikko Perttunen 			}
231404bfb78SMikko Perttunen 
232404bfb78SMikko Perttunen 			err = iommu_map_sg(host->domain,
233404bfb78SMikko Perttunen 					iova_dma_addr(&host->iova, alloc),
234404bfb78SMikko Perttunen 					sgt->sgl, sgt->nents, IOMMU_READ);
235404bfb78SMikko Perttunen 			if (err == 0) {
236404bfb78SMikko Perttunen 				__free_iova(&host->iova, alloc);
237404bfb78SMikko Perttunen 				err = -EINVAL;
238404bfb78SMikko Perttunen 				goto unpin;
239404bfb78SMikko Perttunen 			}
240404bfb78SMikko Perttunen 
241404bfb78SMikko Perttunen 			job->unpins[job->num_unpins].size = gather_size;
242af1cbfb9SThierry Reding 			phys_addr = iova_dma_addr(&host->iova, alloc);
243273da5a0SThierry Reding 		} else if (sgt) {
244af1cbfb9SThierry Reding 			err = dma_map_sg(host->dev, sgt->sgl, sgt->nents,
245af1cbfb9SThierry Reding 					 DMA_TO_DEVICE);
246af1cbfb9SThierry Reding 			if (!err) {
247af1cbfb9SThierry Reding 				err = -ENOMEM;
248af1cbfb9SThierry Reding 				goto unpin;
249404bfb78SMikko Perttunen 			}
250404bfb78SMikko Perttunen 
251*98ae41adSThierry Reding 			job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
252af1cbfb9SThierry Reding 			job->unpins[job->num_unpins].dev = host->dev;
253af1cbfb9SThierry Reding 			phys_addr = sg_dma_address(sgt->sgl);
254af1cbfb9SThierry Reding 		}
255404bfb78SMikko Perttunen 
256af1cbfb9SThierry Reding 		job->addr_phys[job->num_unpins] = phys_addr;
257af1cbfb9SThierry Reding 		job->gather_addr_phys[i] = phys_addr;
258af1cbfb9SThierry Reding 
2596579324aSTerje Bergstrom 		job->unpins[job->num_unpins].bo = g->bo;
2606579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
2616579324aSTerje Bergstrom 		job->num_unpins++;
2626579324aSTerje Bergstrom 	}
2636579324aSTerje Bergstrom 
264404bfb78SMikko Perttunen 	return 0;
2656579324aSTerje Bergstrom 
2666579324aSTerje Bergstrom unpin:
2676579324aSTerje Bergstrom 	host1x_job_unpin(job);
268404bfb78SMikko Perttunen 	return err;
2696579324aSTerje Bergstrom }
2706579324aSTerje Bergstrom 
27147f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
2726579324aSTerje Bergstrom {
2736579324aSTerje Bergstrom 	u32 last_page = ~0;
2746579324aSTerje Bergstrom 	void *cmdbuf_page_addr = NULL;
27547f89c10SDmitry Osipenko 	struct host1x_bo *cmdbuf = g->bo;
276d4ad3ad9SThierry Reding 	unsigned int i;
2776579324aSTerje Bergstrom 
2786579324aSTerje Bergstrom 	/* pin & patch the relocs for one gather */
2793364cd28SArto Merilainen 	for (i = 0; i < job->num_relocs; i++) {
28006490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
2816579324aSTerje Bergstrom 		u32 reloc_addr = (job->reloc_addr_phys[i] +
282961e3beaSThierry Reding 				  reloc->target.offset) >> reloc->shift;
2836579324aSTerje Bergstrom 		u32 *target;
2846579324aSTerje Bergstrom 
2856579324aSTerje Bergstrom 		/* skip all other gathers */
286961e3beaSThierry Reding 		if (cmdbuf != reloc->cmdbuf.bo)
2876579324aSTerje Bergstrom 			continue;
2886579324aSTerje Bergstrom 
28947f89c10SDmitry Osipenko 		if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
29047f89c10SDmitry Osipenko 			target = (u32 *)job->gather_copy_mapped +
29147f89c10SDmitry Osipenko 					reloc->cmdbuf.offset / sizeof(u32) +
29247f89c10SDmitry Osipenko 						g->offset / sizeof(u32);
29347f89c10SDmitry Osipenko 			goto patch_reloc;
29447f89c10SDmitry Osipenko 		}
29547f89c10SDmitry Osipenko 
296961e3beaSThierry Reding 		if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
2976579324aSTerje Bergstrom 			if (cmdbuf_page_addr)
2986579324aSTerje Bergstrom 				host1x_bo_kunmap(cmdbuf, last_page,
2996579324aSTerje Bergstrom 						 cmdbuf_page_addr);
3006579324aSTerje Bergstrom 
3016579324aSTerje Bergstrom 			cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
302961e3beaSThierry Reding 					reloc->cmdbuf.offset >> PAGE_SHIFT);
303961e3beaSThierry Reding 			last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
3046579324aSTerje Bergstrom 
3056579324aSTerje Bergstrom 			if (unlikely(!cmdbuf_page_addr)) {
3066579324aSTerje Bergstrom 				pr_err("Could not map cmdbuf for relocation\n");
3076579324aSTerje Bergstrom 				return -ENOMEM;
3086579324aSTerje Bergstrom 			}
3096579324aSTerje Bergstrom 		}
3106579324aSTerje Bergstrom 
311961e3beaSThierry Reding 		target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
31247f89c10SDmitry Osipenko patch_reloc:
3136579324aSTerje Bergstrom 		*target = reloc_addr;
3146579324aSTerje Bergstrom 	}
3156579324aSTerje Bergstrom 
3166579324aSTerje Bergstrom 	if (cmdbuf_page_addr)
3176579324aSTerje Bergstrom 		host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
3186579324aSTerje Bergstrom 
3196579324aSTerje Bergstrom 	return 0;
3206579324aSTerje Bergstrom }
3216579324aSTerje Bergstrom 
3225060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
3236579324aSTerje Bergstrom 			unsigned int offset)
3246579324aSTerje Bergstrom {
3256579324aSTerje Bergstrom 	offset *= sizeof(u32);
3266579324aSTerje Bergstrom 
327961e3beaSThierry Reding 	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
3285060d8ecSArto Merilainen 		return false;
3296579324aSTerje Bergstrom 
330571cbf70SDmitry Osipenko 	/* relocation shift value validation isn't implemented yet */
331571cbf70SDmitry Osipenko 	if (reloc->shift)
332571cbf70SDmitry Osipenko 		return false;
333571cbf70SDmitry Osipenko 
3345060d8ecSArto Merilainen 	return true;
3356579324aSTerje Bergstrom }
3366579324aSTerje Bergstrom 
3376579324aSTerje Bergstrom struct host1x_firewall {
3386579324aSTerje Bergstrom 	struct host1x_job *job;
3396579324aSTerje Bergstrom 	struct device *dev;
3406579324aSTerje Bergstrom 
3416579324aSTerje Bergstrom 	unsigned int num_relocs;
3426579324aSTerje Bergstrom 	struct host1x_reloc *reloc;
3436579324aSTerje Bergstrom 
344d7fbcf47SThierry Reding 	struct host1x_bo *cmdbuf;
3456579324aSTerje Bergstrom 	unsigned int offset;
3466579324aSTerje Bergstrom 
3476579324aSTerje Bergstrom 	u32 words;
3486579324aSTerje Bergstrom 	u32 class;
3496579324aSTerje Bergstrom 	u32 reg;
3506579324aSTerje Bergstrom 	u32 mask;
3516579324aSTerje Bergstrom 	u32 count;
3526579324aSTerje Bergstrom };
3536579324aSTerje Bergstrom 
354d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset)
355d77563ffSThierry Reding {
3560f563a4bSDmitry Osipenko 	if (!fw->job->is_addr_reg)
3570f563a4bSDmitry Osipenko 		return 0;
3580f563a4bSDmitry Osipenko 
359d77563ffSThierry Reding 	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
360d77563ffSThierry Reding 		if (!fw->num_relocs)
361d77563ffSThierry Reding 			return -EINVAL;
362d77563ffSThierry Reding 
363d77563ffSThierry Reding 		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
364d77563ffSThierry Reding 			return -EINVAL;
365d77563ffSThierry Reding 
366d77563ffSThierry Reding 		fw->num_relocs--;
367d77563ffSThierry Reding 		fw->reloc++;
368d77563ffSThierry Reding 	}
369d77563ffSThierry Reding 
370d77563ffSThierry Reding 	return 0;
371d77563ffSThierry Reding }
372d77563ffSThierry Reding 
3730f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class)
3740f563a4bSDmitry Osipenko {
3750f563a4bSDmitry Osipenko 	if (!fw->job->is_valid_class) {
3760f563a4bSDmitry Osipenko 		if (fw->class != class)
3770f563a4bSDmitry Osipenko 			return -EINVAL;
3780f563a4bSDmitry Osipenko 	} else {
3790f563a4bSDmitry Osipenko 		if (!fw->job->is_valid_class(fw->class))
3800f563a4bSDmitry Osipenko 			return -EINVAL;
3810f563a4bSDmitry Osipenko 	}
3820f563a4bSDmitry Osipenko 
3830f563a4bSDmitry Osipenko 	return 0;
3840f563a4bSDmitry Osipenko }
3850f563a4bSDmitry Osipenko 
3866579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw)
3876579324aSTerje Bergstrom {
3886579324aSTerje Bergstrom 	u32 mask = fw->mask;
3896579324aSTerje Bergstrom 	u32 reg = fw->reg;
390d77563ffSThierry Reding 	int ret;
3916579324aSTerje Bergstrom 
3926579324aSTerje Bergstrom 	while (mask) {
3936579324aSTerje Bergstrom 		if (fw->words == 0)
3946579324aSTerje Bergstrom 			return -EINVAL;
3956579324aSTerje Bergstrom 
3966579324aSTerje Bergstrom 		if (mask & 1) {
397d77563ffSThierry Reding 			ret = check_register(fw, reg);
398d77563ffSThierry Reding 			if (ret < 0)
399d77563ffSThierry Reding 				return ret;
400d77563ffSThierry Reding 
4016579324aSTerje Bergstrom 			fw->words--;
4026579324aSTerje Bergstrom 			fw->offset++;
4036579324aSTerje Bergstrom 		}
4046579324aSTerje Bergstrom 		mask >>= 1;
4056579324aSTerje Bergstrom 		reg++;
4066579324aSTerje Bergstrom 	}
4076579324aSTerje Bergstrom 
4086579324aSTerje Bergstrom 	return 0;
4096579324aSTerje Bergstrom }
4106579324aSTerje Bergstrom 
4116579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw)
4126579324aSTerje Bergstrom {
4136579324aSTerje Bergstrom 	u32 count = fw->count;
4146579324aSTerje Bergstrom 	u32 reg = fw->reg;
415d77563ffSThierry Reding 	int ret;
4166579324aSTerje Bergstrom 
41764c173d3STerje Bergstrom 	while (count) {
4186579324aSTerje Bergstrom 		if (fw->words == 0)
4196579324aSTerje Bergstrom 			return -EINVAL;
4206579324aSTerje Bergstrom 
421d77563ffSThierry Reding 		ret = check_register(fw, reg);
422d77563ffSThierry Reding 		if (ret < 0)
423d77563ffSThierry Reding 			return ret;
424d77563ffSThierry Reding 
4256579324aSTerje Bergstrom 		reg++;
4266579324aSTerje Bergstrom 		fw->words--;
4276579324aSTerje Bergstrom 		fw->offset++;
4286579324aSTerje Bergstrom 		count--;
4296579324aSTerje Bergstrom 	}
4306579324aSTerje Bergstrom 
4316579324aSTerje Bergstrom 	return 0;
4326579324aSTerje Bergstrom }
4336579324aSTerje Bergstrom 
4346579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw)
4356579324aSTerje Bergstrom {
4366579324aSTerje Bergstrom 	u32 count = fw->count;
437d77563ffSThierry Reding 	int ret;
4386579324aSTerje Bergstrom 
4396579324aSTerje Bergstrom 	while (count) {
4406579324aSTerje Bergstrom 		if (fw->words == 0)
4416579324aSTerje Bergstrom 			return -EINVAL;
4426579324aSTerje Bergstrom 
443d77563ffSThierry Reding 		ret = check_register(fw, fw->reg);
444d77563ffSThierry Reding 		if (ret < 0)
445d77563ffSThierry Reding 			return ret;
446d77563ffSThierry Reding 
4476579324aSTerje Bergstrom 		fw->words--;
4486579324aSTerje Bergstrom 		fw->offset++;
4496579324aSTerje Bergstrom 		count--;
4506579324aSTerje Bergstrom 	}
4516579324aSTerje Bergstrom 
4526579324aSTerje Bergstrom 	return 0;
4536579324aSTerje Bergstrom }
4546579324aSTerje Bergstrom 
455afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
4566579324aSTerje Bergstrom {
4573364cd28SArto Merilainen 	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
4583364cd28SArto Merilainen 		(g->offset / sizeof(u32));
4590f563a4bSDmitry Osipenko 	u32 job_class = fw->class;
4606579324aSTerje Bergstrom 	int err = 0;
4616579324aSTerje Bergstrom 
462afac0e43STerje Bergstrom 	fw->words = g->words;
463d7fbcf47SThierry Reding 	fw->cmdbuf = g->bo;
464afac0e43STerje Bergstrom 	fw->offset = 0;
4656579324aSTerje Bergstrom 
466afac0e43STerje Bergstrom 	while (fw->words && !err) {
467afac0e43STerje Bergstrom 		u32 word = cmdbuf_base[fw->offset];
4686579324aSTerje Bergstrom 		u32 opcode = (word & 0xf0000000) >> 28;
4696579324aSTerje Bergstrom 
470afac0e43STerje Bergstrom 		fw->mask = 0;
471afac0e43STerje Bergstrom 		fw->reg = 0;
472afac0e43STerje Bergstrom 		fw->count = 0;
473afac0e43STerje Bergstrom 		fw->words--;
474afac0e43STerje Bergstrom 		fw->offset++;
4756579324aSTerje Bergstrom 
4766579324aSTerje Bergstrom 		switch (opcode) {
4776579324aSTerje Bergstrom 		case 0:
478afac0e43STerje Bergstrom 			fw->class = word >> 6 & 0x3ff;
479afac0e43STerje Bergstrom 			fw->mask = word & 0x3f;
480afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
4810f563a4bSDmitry Osipenko 			err = check_class(fw, job_class);
4820f563a4bSDmitry Osipenko 			if (!err)
483afac0e43STerje Bergstrom 				err = check_mask(fw);
4846579324aSTerje Bergstrom 			if (err)
4856579324aSTerje Bergstrom 				goto out;
4866579324aSTerje Bergstrom 			break;
4876579324aSTerje Bergstrom 		case 1:
488afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
489afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
490afac0e43STerje Bergstrom 			err = check_incr(fw);
4916579324aSTerje Bergstrom 			if (err)
4926579324aSTerje Bergstrom 				goto out;
4936579324aSTerje Bergstrom 			break;
4946579324aSTerje Bergstrom 
4956579324aSTerje Bergstrom 		case 2:
496afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
497afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
498afac0e43STerje Bergstrom 			err = check_nonincr(fw);
4996579324aSTerje Bergstrom 			if (err)
5006579324aSTerje Bergstrom 				goto out;
5016579324aSTerje Bergstrom 			break;
5026579324aSTerje Bergstrom 
5036579324aSTerje Bergstrom 		case 3:
504afac0e43STerje Bergstrom 			fw->mask = word & 0xffff;
505afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
506afac0e43STerje Bergstrom 			err = check_mask(fw);
5076579324aSTerje Bergstrom 			if (err)
5086579324aSTerje Bergstrom 				goto out;
5096579324aSTerje Bergstrom 			break;
5106579324aSTerje Bergstrom 		case 4:
5116579324aSTerje Bergstrom 		case 14:
5126579324aSTerje Bergstrom 			break;
5136579324aSTerje Bergstrom 		default:
5146579324aSTerje Bergstrom 			err = -EINVAL;
5156579324aSTerje Bergstrom 			break;
5166579324aSTerje Bergstrom 		}
5176579324aSTerje Bergstrom 	}
5186579324aSTerje Bergstrom 
5196579324aSTerje Bergstrom out:
5206579324aSTerje Bergstrom 	return err;
5216579324aSTerje Bergstrom }
5226579324aSTerje Bergstrom 
523b78e70c0SThierry Reding static inline int copy_gathers(struct device *host, struct host1x_job *job,
524b78e70c0SThierry Reding 			       struct device *dev)
5256579324aSTerje Bergstrom {
5263364cd28SArto Merilainen 	struct host1x_firewall fw;
5276579324aSTerje Bergstrom 	size_t size = 0;
5286579324aSTerje Bergstrom 	size_t offset = 0;
529d4ad3ad9SThierry Reding 	unsigned int i;
5306579324aSTerje Bergstrom 
5313364cd28SArto Merilainen 	fw.job = job;
5323364cd28SArto Merilainen 	fw.dev = dev;
53306490bb9SThierry Reding 	fw.reloc = job->relocs;
5343364cd28SArto Merilainen 	fw.num_relocs = job->num_relocs;
5353833d16fSDmitry Osipenko 	fw.class = job->class;
5363364cd28SArto Merilainen 
5376579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
5386579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
5396df633d0SThierry Reding 
5406579324aSTerje Bergstrom 		size += g->words * sizeof(u32);
5416579324aSTerje Bergstrom 	}
5426579324aSTerje Bergstrom 
54343240bbdSDmitry Osipenko 	/*
54443240bbdSDmitry Osipenko 	 * Try a non-blocking allocation from a higher priority pools first,
54543240bbdSDmitry Osipenko 	 * as awaiting for the allocation here is a major performance hit.
54643240bbdSDmitry Osipenko 	 */
547b78e70c0SThierry Reding 	job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
54843240bbdSDmitry Osipenko 					       GFP_NOWAIT);
54943240bbdSDmitry Osipenko 
55043240bbdSDmitry Osipenko 	/* the higher priority allocation failed, try the generic-blocking */
55143240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
552b78e70c0SThierry Reding 		job->gather_copy_mapped = dma_alloc_wc(host, size,
55343240bbdSDmitry Osipenko 						       &job->gather_copy,
5546579324aSTerje Bergstrom 						       GFP_KERNEL);
55543240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
556745cecc0SDan Carpenter 		return -ENOMEM;
5576579324aSTerje Bergstrom 
5586579324aSTerje Bergstrom 	job->gather_copy_size = size;
5596579324aSTerje Bergstrom 
5606579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
5616579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
5626579324aSTerje Bergstrom 		void *gather;
5636579324aSTerje Bergstrom 
5643364cd28SArto Merilainen 		/* Copy the gather */
5656579324aSTerje Bergstrom 		gather = host1x_bo_mmap(g->bo);
5666579324aSTerje Bergstrom 		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
5676579324aSTerje Bergstrom 		       g->words * sizeof(u32));
5686579324aSTerje Bergstrom 		host1x_bo_munmap(g->bo, gather);
5696579324aSTerje Bergstrom 
5703364cd28SArto Merilainen 		/* Store the location in the buffer */
5716579324aSTerje Bergstrom 		g->base = job->gather_copy;
5726579324aSTerje Bergstrom 		g->offset = offset;
5733364cd28SArto Merilainen 
5743364cd28SArto Merilainen 		/* Validate the job */
5753364cd28SArto Merilainen 		if (validate(&fw, g))
5763364cd28SArto Merilainen 			return -EINVAL;
5776579324aSTerje Bergstrom 
5786579324aSTerje Bergstrom 		offset += g->words * sizeof(u32);
5796579324aSTerje Bergstrom 	}
5806579324aSTerje Bergstrom 
58124c94e16SThierry Reding 	/* No relocs should remain at this point */
58224c94e16SThierry Reding 	if (fw.num_relocs)
583a9ff9995SErik Faye-Lund 		return -EINVAL;
584a9ff9995SErik Faye-Lund 
5856579324aSTerje Bergstrom 	return 0;
5866579324aSTerje Bergstrom }
5876579324aSTerje Bergstrom 
5886579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev)
5896579324aSTerje Bergstrom {
5906579324aSTerje Bergstrom 	int err;
5916579324aSTerje Bergstrom 	unsigned int i, j;
5926579324aSTerje Bergstrom 	struct host1x *host = dev_get_drvdata(dev->parent);
5936579324aSTerje Bergstrom 
5946579324aSTerje Bergstrom 	/* pin memory */
595404bfb78SMikko Perttunen 	err = pin_job(host, job);
596404bfb78SMikko Perttunen 	if (err)
5976579324aSTerje Bergstrom 		goto out;
5986579324aSTerje Bergstrom 
59947f89c10SDmitry Osipenko 	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
600b78e70c0SThierry Reding 		err = copy_gathers(host->dev, job, dev);
60147f89c10SDmitry Osipenko 		if (err)
60247f89c10SDmitry Osipenko 			goto out;
60347f89c10SDmitry Osipenko 	}
60447f89c10SDmitry Osipenko 
6056579324aSTerje Bergstrom 	/* patch gathers */
6066579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
6076579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
6086579324aSTerje Bergstrom 
6096579324aSTerje Bergstrom 		/* process each gather mem only once */
6106579324aSTerje Bergstrom 		if (g->handled)
6116579324aSTerje Bergstrom 			continue;
6126579324aSTerje Bergstrom 
61347f89c10SDmitry Osipenko 		/* copy_gathers() sets gathers base if firewall is enabled */
61447f89c10SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
6156579324aSTerje Bergstrom 			g->base = job->gather_addr_phys[i];
6166579324aSTerje Bergstrom 
617f08ef2d1SArto Merilainen 		for (j = i + 1; j < job->num_gathers; j++) {
618f08ef2d1SArto Merilainen 			if (job->gathers[j].bo == g->bo) {
6196579324aSTerje Bergstrom 				job->gathers[j].handled = true;
620f08ef2d1SArto Merilainen 				job->gathers[j].base = g->base;
621f08ef2d1SArto Merilainen 			}
622f08ef2d1SArto Merilainen 		}
6236579324aSTerje Bergstrom 
62447f89c10SDmitry Osipenko 		err = do_relocs(job, g);
6253364cd28SArto Merilainen 		if (err)
62647f89c10SDmitry Osipenko 			break;
6276579324aSTerje Bergstrom 	}
6286579324aSTerje Bergstrom 
6296579324aSTerje Bergstrom out:
630e5855aa3SDmitry Osipenko 	if (err)
631e5855aa3SDmitry Osipenko 		host1x_job_unpin(job);
6326579324aSTerje Bergstrom 	wmb();
6336579324aSTerje Bergstrom 
6346579324aSTerje Bergstrom 	return err;
6356579324aSTerje Bergstrom }
636fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin);
6376579324aSTerje Bergstrom 
6386579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job)
6396579324aSTerje Bergstrom {
640404bfb78SMikko Perttunen 	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
6416579324aSTerje Bergstrom 	unsigned int i;
6426579324aSTerje Bergstrom 
6436579324aSTerje Bergstrom 	for (i = 0; i < job->num_unpins; i++) {
6446579324aSTerje Bergstrom 		struct host1x_job_unpin_data *unpin = &job->unpins[i];
645af1cbfb9SThierry Reding 		struct device *dev = unpin->dev ?: host->dev;
646af1cbfb9SThierry Reding 		struct sg_table *sgt = unpin->sgt;
6476df633d0SThierry Reding 
648ec589232SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
649ec589232SDmitry Osipenko 		    unpin->size && host->domain) {
650404bfb78SMikko Perttunen 			iommu_unmap(host->domain, job->addr_phys[i],
651404bfb78SMikko Perttunen 				    unpin->size);
652404bfb78SMikko Perttunen 			free_iova(&host->iova,
653404bfb78SMikko Perttunen 				iova_pfn(&host->iova, job->addr_phys[i]));
654404bfb78SMikko Perttunen 		}
655404bfb78SMikko Perttunen 
656af1cbfb9SThierry Reding 		if (unpin->dev && sgt)
657af1cbfb9SThierry Reding 			dma_unmap_sg(unpin->dev, sgt->sgl, sgt->nents,
658af1cbfb9SThierry Reding 				     unpin->dir);
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