xref: /openbmc/linux/drivers/gpu/host1x/job.c (revision 9952f6918daa4ab5fc81307a9f90e31a4df3b200)
1*9952f691SThomas 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>
116579324aSTerje Bergstrom #include <linux/kref.h>
126579324aSTerje Bergstrom #include <linux/module.h>
136579324aSTerje Bergstrom #include <linux/scatterlist.h>
146579324aSTerje Bergstrom #include <linux/slab.h>
156579324aSTerje Bergstrom #include <linux/vmalloc.h>
166579324aSTerje Bergstrom #include <trace/events/host1x.h>
176579324aSTerje Bergstrom 
186579324aSTerje Bergstrom #include "channel.h"
196579324aSTerje Bergstrom #include "dev.h"
206579324aSTerje Bergstrom #include "job.h"
216579324aSTerje Bergstrom #include "syncpt.h"
226579324aSTerje Bergstrom 
23a47ac10eSDmitry Osipenko #define HOST1X_WAIT_SYNCPT_OFFSET 0x8
24a47ac10eSDmitry Osipenko 
256579324aSTerje Bergstrom struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
2624c94e16SThierry Reding 				    u32 num_cmdbufs, u32 num_relocs)
276579324aSTerje Bergstrom {
286579324aSTerje Bergstrom 	struct host1x_job *job = NULL;
296579324aSTerje Bergstrom 	unsigned int num_unpins = num_cmdbufs + num_relocs;
306579324aSTerje Bergstrom 	u64 total;
316579324aSTerje Bergstrom 	void *mem;
326579324aSTerje Bergstrom 
336579324aSTerje Bergstrom 	/* Check that we're not going to overflow */
346579324aSTerje Bergstrom 	total = sizeof(struct host1x_job) +
35f5fda676SDan Carpenter 		(u64)num_relocs * sizeof(struct host1x_reloc) +
36f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
37f5fda676SDan Carpenter 		(u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
38f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(dma_addr_t) +
39f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(u32 *);
406579324aSTerje Bergstrom 	if (total > ULONG_MAX)
416579324aSTerje Bergstrom 		return NULL;
426579324aSTerje Bergstrom 
436579324aSTerje Bergstrom 	mem = job = kzalloc(total, GFP_KERNEL);
446579324aSTerje Bergstrom 	if (!job)
456579324aSTerje Bergstrom 		return NULL;
466579324aSTerje Bergstrom 
476579324aSTerje Bergstrom 	kref_init(&job->ref);
486579324aSTerje Bergstrom 	job->channel = ch;
496579324aSTerje Bergstrom 
506579324aSTerje Bergstrom 	/* Redistribute memory to the structs  */
516579324aSTerje Bergstrom 	mem += sizeof(struct host1x_job);
5206490bb9SThierry Reding 	job->relocs = num_relocs ? mem : NULL;
536579324aSTerje Bergstrom 	mem += num_relocs * sizeof(struct host1x_reloc);
546579324aSTerje Bergstrom 	job->unpins = num_unpins ? mem : NULL;
556579324aSTerje Bergstrom 	mem += num_unpins * sizeof(struct host1x_job_unpin_data);
566579324aSTerje Bergstrom 	job->gathers = num_cmdbufs ? mem : NULL;
576579324aSTerje Bergstrom 	mem += num_cmdbufs * sizeof(struct host1x_job_gather);
586579324aSTerje Bergstrom 	job->addr_phys = num_unpins ? mem : NULL;
596579324aSTerje Bergstrom 
606579324aSTerje Bergstrom 	job->reloc_addr_phys = job->addr_phys;
616579324aSTerje Bergstrom 	job->gather_addr_phys = &job->addr_phys[num_relocs];
626579324aSTerje Bergstrom 
636579324aSTerje Bergstrom 	return job;
646579324aSTerje Bergstrom }
65fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_alloc);
666579324aSTerje Bergstrom 
676579324aSTerje Bergstrom struct host1x_job *host1x_job_get(struct host1x_job *job)
686579324aSTerje Bergstrom {
696579324aSTerje Bergstrom 	kref_get(&job->ref);
706579324aSTerje Bergstrom 	return job;
716579324aSTerje Bergstrom }
72fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_get);
736579324aSTerje Bergstrom 
746579324aSTerje Bergstrom static void job_free(struct kref *ref)
756579324aSTerje Bergstrom {
766579324aSTerje Bergstrom 	struct host1x_job *job = container_of(ref, struct host1x_job, ref);
776579324aSTerje Bergstrom 
786579324aSTerje Bergstrom 	kfree(job);
796579324aSTerje Bergstrom }
806579324aSTerje Bergstrom 
816579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job)
826579324aSTerje Bergstrom {
836579324aSTerje Bergstrom 	kref_put(&job->ref, job_free);
846579324aSTerje Bergstrom }
85fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put);
866579324aSTerje Bergstrom 
876579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
88326bbd79SThierry Reding 			   unsigned int words, unsigned int offset)
896579324aSTerje Bergstrom {
90326bbd79SThierry Reding 	struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
916579324aSTerje Bergstrom 
92326bbd79SThierry Reding 	gather->words = words;
93326bbd79SThierry Reding 	gather->bo = bo;
94326bbd79SThierry Reding 	gather->offset = offset;
95326bbd79SThierry Reding 
966579324aSTerje Bergstrom 	job->num_gathers++;
976579324aSTerje Bergstrom }
98fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather);
996579324aSTerje Bergstrom 
100404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
1016579324aSTerje Bergstrom {
1026579324aSTerje Bergstrom 	unsigned int i;
103404bfb78SMikko Perttunen 	int err;
1046579324aSTerje Bergstrom 
1056579324aSTerje Bergstrom 	job->num_unpins = 0;
1066579324aSTerje Bergstrom 
1076579324aSTerje Bergstrom 	for (i = 0; i < job->num_relocs; i++) {
10806490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
1096579324aSTerje Bergstrom 		struct sg_table *sgt;
1106579324aSTerje Bergstrom 		dma_addr_t phys_addr;
1116579324aSTerje Bergstrom 
112961e3beaSThierry Reding 		reloc->target.bo = host1x_bo_get(reloc->target.bo);
113404bfb78SMikko Perttunen 		if (!reloc->target.bo) {
114404bfb78SMikko Perttunen 			err = -EINVAL;
1156579324aSTerje Bergstrom 			goto unpin;
116404bfb78SMikko Perttunen 		}
1176579324aSTerje Bergstrom 
118961e3beaSThierry Reding 		phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
1196579324aSTerje Bergstrom 
1206579324aSTerje Bergstrom 		job->addr_phys[job->num_unpins] = phys_addr;
121961e3beaSThierry Reding 		job->unpins[job->num_unpins].bo = reloc->target.bo;
1226579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
1236579324aSTerje Bergstrom 		job->num_unpins++;
1246579324aSTerje Bergstrom 	}
1256579324aSTerje Bergstrom 
1266579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
1276579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
128404bfb78SMikko Perttunen 		size_t gather_size = 0;
129404bfb78SMikko Perttunen 		struct scatterlist *sg;
1306579324aSTerje Bergstrom 		struct sg_table *sgt;
1316579324aSTerje Bergstrom 		dma_addr_t phys_addr;
132404bfb78SMikko Perttunen 		unsigned long shift;
133404bfb78SMikko Perttunen 		struct iova *alloc;
134404bfb78SMikko Perttunen 		unsigned int j;
1356579324aSTerje Bergstrom 
1366579324aSTerje Bergstrom 		g->bo = host1x_bo_get(g->bo);
137404bfb78SMikko Perttunen 		if (!g->bo) {
138404bfb78SMikko Perttunen 			err = -EINVAL;
1396579324aSTerje Bergstrom 			goto unpin;
140404bfb78SMikko Perttunen 		}
1416579324aSTerje Bergstrom 
1426579324aSTerje Bergstrom 		phys_addr = host1x_bo_pin(g->bo, &sgt);
1436579324aSTerje Bergstrom 
144404bfb78SMikko Perttunen 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
145404bfb78SMikko Perttunen 			for_each_sg(sgt->sgl, sg, sgt->nents, j)
146404bfb78SMikko Perttunen 				gather_size += sg->length;
147404bfb78SMikko Perttunen 			gather_size = iova_align(&host->iova, gather_size);
148404bfb78SMikko Perttunen 
149404bfb78SMikko Perttunen 			shift = iova_shift(&host->iova);
150404bfb78SMikko Perttunen 			alloc = alloc_iova(&host->iova, gather_size >> shift,
151404bfb78SMikko Perttunen 					   host->iova_end >> shift, true);
152404bfb78SMikko Perttunen 			if (!alloc) {
153404bfb78SMikko Perttunen 				err = -ENOMEM;
154404bfb78SMikko Perttunen 				goto unpin;
155404bfb78SMikko Perttunen 			}
156404bfb78SMikko Perttunen 
157404bfb78SMikko Perttunen 			err = iommu_map_sg(host->domain,
158404bfb78SMikko Perttunen 					iova_dma_addr(&host->iova, alloc),
159404bfb78SMikko Perttunen 					sgt->sgl, sgt->nents, IOMMU_READ);
160404bfb78SMikko Perttunen 			if (err == 0) {
161404bfb78SMikko Perttunen 				__free_iova(&host->iova, alloc);
162404bfb78SMikko Perttunen 				err = -EINVAL;
163404bfb78SMikko Perttunen 				goto unpin;
164404bfb78SMikko Perttunen 			}
165404bfb78SMikko Perttunen 
166404bfb78SMikko Perttunen 			job->addr_phys[job->num_unpins] =
167404bfb78SMikko Perttunen 				iova_dma_addr(&host->iova, alloc);
168404bfb78SMikko Perttunen 			job->unpins[job->num_unpins].size = gather_size;
169404bfb78SMikko Perttunen 		} else {
1706579324aSTerje Bergstrom 			job->addr_phys[job->num_unpins] = phys_addr;
171404bfb78SMikko Perttunen 		}
172404bfb78SMikko Perttunen 
173404bfb78SMikko Perttunen 		job->gather_addr_phys[i] = job->addr_phys[job->num_unpins];
174404bfb78SMikko Perttunen 
1756579324aSTerje Bergstrom 		job->unpins[job->num_unpins].bo = g->bo;
1766579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
1776579324aSTerje Bergstrom 		job->num_unpins++;
1786579324aSTerje Bergstrom 	}
1796579324aSTerje Bergstrom 
180404bfb78SMikko Perttunen 	return 0;
1816579324aSTerje Bergstrom 
1826579324aSTerje Bergstrom unpin:
1836579324aSTerje Bergstrom 	host1x_job_unpin(job);
184404bfb78SMikko Perttunen 	return err;
1856579324aSTerje Bergstrom }
1866579324aSTerje Bergstrom 
18747f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
1886579324aSTerje Bergstrom {
1896579324aSTerje Bergstrom 	u32 last_page = ~0;
1906579324aSTerje Bergstrom 	void *cmdbuf_page_addr = NULL;
19147f89c10SDmitry Osipenko 	struct host1x_bo *cmdbuf = g->bo;
192d4ad3ad9SThierry Reding 	unsigned int i;
1936579324aSTerje Bergstrom 
1946579324aSTerje Bergstrom 	/* pin & patch the relocs for one gather */
1953364cd28SArto Merilainen 	for (i = 0; i < job->num_relocs; i++) {
19606490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
1976579324aSTerje Bergstrom 		u32 reloc_addr = (job->reloc_addr_phys[i] +
198961e3beaSThierry Reding 				  reloc->target.offset) >> reloc->shift;
1996579324aSTerje Bergstrom 		u32 *target;
2006579324aSTerje Bergstrom 
2016579324aSTerje Bergstrom 		/* skip all other gathers */
202961e3beaSThierry Reding 		if (cmdbuf != reloc->cmdbuf.bo)
2036579324aSTerje Bergstrom 			continue;
2046579324aSTerje Bergstrom 
20547f89c10SDmitry Osipenko 		if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
20647f89c10SDmitry Osipenko 			target = (u32 *)job->gather_copy_mapped +
20747f89c10SDmitry Osipenko 					reloc->cmdbuf.offset / sizeof(u32) +
20847f89c10SDmitry Osipenko 						g->offset / sizeof(u32);
20947f89c10SDmitry Osipenko 			goto patch_reloc;
21047f89c10SDmitry Osipenko 		}
21147f89c10SDmitry Osipenko 
212961e3beaSThierry Reding 		if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
2136579324aSTerje Bergstrom 			if (cmdbuf_page_addr)
2146579324aSTerje Bergstrom 				host1x_bo_kunmap(cmdbuf, last_page,
2156579324aSTerje Bergstrom 						 cmdbuf_page_addr);
2166579324aSTerje Bergstrom 
2176579324aSTerje Bergstrom 			cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
218961e3beaSThierry Reding 					reloc->cmdbuf.offset >> PAGE_SHIFT);
219961e3beaSThierry Reding 			last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
2206579324aSTerje Bergstrom 
2216579324aSTerje Bergstrom 			if (unlikely(!cmdbuf_page_addr)) {
2226579324aSTerje Bergstrom 				pr_err("Could not map cmdbuf for relocation\n");
2236579324aSTerje Bergstrom 				return -ENOMEM;
2246579324aSTerje Bergstrom 			}
2256579324aSTerje Bergstrom 		}
2266579324aSTerje Bergstrom 
227961e3beaSThierry Reding 		target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
22847f89c10SDmitry Osipenko patch_reloc:
2296579324aSTerje Bergstrom 		*target = reloc_addr;
2306579324aSTerje Bergstrom 	}
2316579324aSTerje Bergstrom 
2326579324aSTerje Bergstrom 	if (cmdbuf_page_addr)
2336579324aSTerje Bergstrom 		host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
2346579324aSTerje Bergstrom 
2356579324aSTerje Bergstrom 	return 0;
2366579324aSTerje Bergstrom }
2376579324aSTerje Bergstrom 
2385060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
2396579324aSTerje Bergstrom 			unsigned int offset)
2406579324aSTerje Bergstrom {
2416579324aSTerje Bergstrom 	offset *= sizeof(u32);
2426579324aSTerje Bergstrom 
243961e3beaSThierry Reding 	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
2445060d8ecSArto Merilainen 		return false;
2456579324aSTerje Bergstrom 
246571cbf70SDmitry Osipenko 	/* relocation shift value validation isn't implemented yet */
247571cbf70SDmitry Osipenko 	if (reloc->shift)
248571cbf70SDmitry Osipenko 		return false;
249571cbf70SDmitry Osipenko 
2505060d8ecSArto Merilainen 	return true;
2516579324aSTerje Bergstrom }
2526579324aSTerje Bergstrom 
2536579324aSTerje Bergstrom struct host1x_firewall {
2546579324aSTerje Bergstrom 	struct host1x_job *job;
2556579324aSTerje Bergstrom 	struct device *dev;
2566579324aSTerje Bergstrom 
2576579324aSTerje Bergstrom 	unsigned int num_relocs;
2586579324aSTerje Bergstrom 	struct host1x_reloc *reloc;
2596579324aSTerje Bergstrom 
260d7fbcf47SThierry Reding 	struct host1x_bo *cmdbuf;
2616579324aSTerje Bergstrom 	unsigned int offset;
2626579324aSTerje Bergstrom 
2636579324aSTerje Bergstrom 	u32 words;
2646579324aSTerje Bergstrom 	u32 class;
2656579324aSTerje Bergstrom 	u32 reg;
2666579324aSTerje Bergstrom 	u32 mask;
2676579324aSTerje Bergstrom 	u32 count;
2686579324aSTerje Bergstrom };
2696579324aSTerje Bergstrom 
270d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset)
271d77563ffSThierry Reding {
2720f563a4bSDmitry Osipenko 	if (!fw->job->is_addr_reg)
2730f563a4bSDmitry Osipenko 		return 0;
2740f563a4bSDmitry Osipenko 
275d77563ffSThierry Reding 	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
276d77563ffSThierry Reding 		if (!fw->num_relocs)
277d77563ffSThierry Reding 			return -EINVAL;
278d77563ffSThierry Reding 
279d77563ffSThierry Reding 		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
280d77563ffSThierry Reding 			return -EINVAL;
281d77563ffSThierry Reding 
282d77563ffSThierry Reding 		fw->num_relocs--;
283d77563ffSThierry Reding 		fw->reloc++;
284d77563ffSThierry Reding 	}
285d77563ffSThierry Reding 
286d77563ffSThierry Reding 	return 0;
287d77563ffSThierry Reding }
288d77563ffSThierry Reding 
2890f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class)
2900f563a4bSDmitry Osipenko {
2910f563a4bSDmitry Osipenko 	if (!fw->job->is_valid_class) {
2920f563a4bSDmitry Osipenko 		if (fw->class != class)
2930f563a4bSDmitry Osipenko 			return -EINVAL;
2940f563a4bSDmitry Osipenko 	} else {
2950f563a4bSDmitry Osipenko 		if (!fw->job->is_valid_class(fw->class))
2960f563a4bSDmitry Osipenko 			return -EINVAL;
2970f563a4bSDmitry Osipenko 	}
2980f563a4bSDmitry Osipenko 
2990f563a4bSDmitry Osipenko 	return 0;
3000f563a4bSDmitry Osipenko }
3010f563a4bSDmitry Osipenko 
3026579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw)
3036579324aSTerje Bergstrom {
3046579324aSTerje Bergstrom 	u32 mask = fw->mask;
3056579324aSTerje Bergstrom 	u32 reg = fw->reg;
306d77563ffSThierry Reding 	int ret;
3076579324aSTerje Bergstrom 
3086579324aSTerje Bergstrom 	while (mask) {
3096579324aSTerje Bergstrom 		if (fw->words == 0)
3106579324aSTerje Bergstrom 			return -EINVAL;
3116579324aSTerje Bergstrom 
3126579324aSTerje Bergstrom 		if (mask & 1) {
313d77563ffSThierry Reding 			ret = check_register(fw, reg);
314d77563ffSThierry Reding 			if (ret < 0)
315d77563ffSThierry Reding 				return ret;
316d77563ffSThierry Reding 
3176579324aSTerje Bergstrom 			fw->words--;
3186579324aSTerje Bergstrom 			fw->offset++;
3196579324aSTerje Bergstrom 		}
3206579324aSTerje Bergstrom 		mask >>= 1;
3216579324aSTerje Bergstrom 		reg++;
3226579324aSTerje Bergstrom 	}
3236579324aSTerje Bergstrom 
3246579324aSTerje Bergstrom 	return 0;
3256579324aSTerje Bergstrom }
3266579324aSTerje Bergstrom 
3276579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw)
3286579324aSTerje Bergstrom {
3296579324aSTerje Bergstrom 	u32 count = fw->count;
3306579324aSTerje Bergstrom 	u32 reg = fw->reg;
331d77563ffSThierry Reding 	int ret;
3326579324aSTerje Bergstrom 
33364c173d3STerje Bergstrom 	while (count) {
3346579324aSTerje Bergstrom 		if (fw->words == 0)
3356579324aSTerje Bergstrom 			return -EINVAL;
3366579324aSTerje Bergstrom 
337d77563ffSThierry Reding 		ret = check_register(fw, reg);
338d77563ffSThierry Reding 		if (ret < 0)
339d77563ffSThierry Reding 			return ret;
340d77563ffSThierry Reding 
3416579324aSTerje Bergstrom 		reg++;
3426579324aSTerje Bergstrom 		fw->words--;
3436579324aSTerje Bergstrom 		fw->offset++;
3446579324aSTerje Bergstrom 		count--;
3456579324aSTerje Bergstrom 	}
3466579324aSTerje Bergstrom 
3476579324aSTerje Bergstrom 	return 0;
3486579324aSTerje Bergstrom }
3496579324aSTerje Bergstrom 
3506579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw)
3516579324aSTerje Bergstrom {
3526579324aSTerje Bergstrom 	u32 count = fw->count;
353d77563ffSThierry Reding 	int ret;
3546579324aSTerje Bergstrom 
3556579324aSTerje Bergstrom 	while (count) {
3566579324aSTerje Bergstrom 		if (fw->words == 0)
3576579324aSTerje Bergstrom 			return -EINVAL;
3586579324aSTerje Bergstrom 
359d77563ffSThierry Reding 		ret = check_register(fw, fw->reg);
360d77563ffSThierry Reding 		if (ret < 0)
361d77563ffSThierry Reding 			return ret;
362d77563ffSThierry Reding 
3636579324aSTerje Bergstrom 		fw->words--;
3646579324aSTerje Bergstrom 		fw->offset++;
3656579324aSTerje Bergstrom 		count--;
3666579324aSTerje Bergstrom 	}
3676579324aSTerje Bergstrom 
3686579324aSTerje Bergstrom 	return 0;
3696579324aSTerje Bergstrom }
3706579324aSTerje Bergstrom 
371afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
3726579324aSTerje Bergstrom {
3733364cd28SArto Merilainen 	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
3743364cd28SArto Merilainen 		(g->offset / sizeof(u32));
3750f563a4bSDmitry Osipenko 	u32 job_class = fw->class;
3766579324aSTerje Bergstrom 	int err = 0;
3776579324aSTerje Bergstrom 
378afac0e43STerje Bergstrom 	fw->words = g->words;
379d7fbcf47SThierry Reding 	fw->cmdbuf = g->bo;
380afac0e43STerje Bergstrom 	fw->offset = 0;
3816579324aSTerje Bergstrom 
382afac0e43STerje Bergstrom 	while (fw->words && !err) {
383afac0e43STerje Bergstrom 		u32 word = cmdbuf_base[fw->offset];
3846579324aSTerje Bergstrom 		u32 opcode = (word & 0xf0000000) >> 28;
3856579324aSTerje Bergstrom 
386afac0e43STerje Bergstrom 		fw->mask = 0;
387afac0e43STerje Bergstrom 		fw->reg = 0;
388afac0e43STerje Bergstrom 		fw->count = 0;
389afac0e43STerje Bergstrom 		fw->words--;
390afac0e43STerje Bergstrom 		fw->offset++;
3916579324aSTerje Bergstrom 
3926579324aSTerje Bergstrom 		switch (opcode) {
3936579324aSTerje Bergstrom 		case 0:
394afac0e43STerje Bergstrom 			fw->class = word >> 6 & 0x3ff;
395afac0e43STerje Bergstrom 			fw->mask = word & 0x3f;
396afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
3970f563a4bSDmitry Osipenko 			err = check_class(fw, job_class);
3980f563a4bSDmitry Osipenko 			if (!err)
399afac0e43STerje Bergstrom 				err = check_mask(fw);
4006579324aSTerje Bergstrom 			if (err)
4016579324aSTerje Bergstrom 				goto out;
4026579324aSTerje Bergstrom 			break;
4036579324aSTerje Bergstrom 		case 1:
404afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
405afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
406afac0e43STerje Bergstrom 			err = check_incr(fw);
4076579324aSTerje Bergstrom 			if (err)
4086579324aSTerje Bergstrom 				goto out;
4096579324aSTerje Bergstrom 			break;
4106579324aSTerje Bergstrom 
4116579324aSTerje Bergstrom 		case 2:
412afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
413afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
414afac0e43STerje Bergstrom 			err = check_nonincr(fw);
4156579324aSTerje Bergstrom 			if (err)
4166579324aSTerje Bergstrom 				goto out;
4176579324aSTerje Bergstrom 			break;
4186579324aSTerje Bergstrom 
4196579324aSTerje Bergstrom 		case 3:
420afac0e43STerje Bergstrom 			fw->mask = word & 0xffff;
421afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
422afac0e43STerje Bergstrom 			err = check_mask(fw);
4236579324aSTerje Bergstrom 			if (err)
4246579324aSTerje Bergstrom 				goto out;
4256579324aSTerje Bergstrom 			break;
4266579324aSTerje Bergstrom 		case 4:
4276579324aSTerje Bergstrom 		case 14:
4286579324aSTerje Bergstrom 			break;
4296579324aSTerje Bergstrom 		default:
4306579324aSTerje Bergstrom 			err = -EINVAL;
4316579324aSTerje Bergstrom 			break;
4326579324aSTerje Bergstrom 		}
4336579324aSTerje Bergstrom 	}
4346579324aSTerje Bergstrom 
4356579324aSTerje Bergstrom out:
4366579324aSTerje Bergstrom 	return err;
4376579324aSTerje Bergstrom }
4386579324aSTerje Bergstrom 
4396579324aSTerje Bergstrom static inline int copy_gathers(struct host1x_job *job, struct device *dev)
4406579324aSTerje Bergstrom {
4413364cd28SArto Merilainen 	struct host1x_firewall fw;
4426579324aSTerje Bergstrom 	size_t size = 0;
4436579324aSTerje Bergstrom 	size_t offset = 0;
444d4ad3ad9SThierry Reding 	unsigned int i;
4456579324aSTerje Bergstrom 
4463364cd28SArto Merilainen 	fw.job = job;
4473364cd28SArto Merilainen 	fw.dev = dev;
44806490bb9SThierry Reding 	fw.reloc = job->relocs;
4493364cd28SArto Merilainen 	fw.num_relocs = job->num_relocs;
4503833d16fSDmitry Osipenko 	fw.class = job->class;
4513364cd28SArto Merilainen 
4526579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
4536579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
4546df633d0SThierry Reding 
4556579324aSTerje Bergstrom 		size += g->words * sizeof(u32);
4566579324aSTerje Bergstrom 	}
4576579324aSTerje Bergstrom 
45843240bbdSDmitry Osipenko 	/*
45943240bbdSDmitry Osipenko 	 * Try a non-blocking allocation from a higher priority pools first,
46043240bbdSDmitry Osipenko 	 * as awaiting for the allocation here is a major performance hit.
46143240bbdSDmitry Osipenko 	 */
462f6e45661SLuis R. Rodriguez 	job->gather_copy_mapped = dma_alloc_wc(dev, size, &job->gather_copy,
46343240bbdSDmitry Osipenko 					       GFP_NOWAIT);
46443240bbdSDmitry Osipenko 
46543240bbdSDmitry Osipenko 	/* the higher priority allocation failed, try the generic-blocking */
46643240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
46743240bbdSDmitry Osipenko 		job->gather_copy_mapped = dma_alloc_wc(dev, size,
46843240bbdSDmitry Osipenko 						       &job->gather_copy,
4696579324aSTerje Bergstrom 						       GFP_KERNEL);
47043240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
471745cecc0SDan Carpenter 		return -ENOMEM;
4726579324aSTerje Bergstrom 
4736579324aSTerje Bergstrom 	job->gather_copy_size = size;
4746579324aSTerje Bergstrom 
4756579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
4766579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
4776579324aSTerje Bergstrom 		void *gather;
4786579324aSTerje Bergstrom 
4793364cd28SArto Merilainen 		/* Copy the gather */
4806579324aSTerje Bergstrom 		gather = host1x_bo_mmap(g->bo);
4816579324aSTerje Bergstrom 		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
4826579324aSTerje Bergstrom 		       g->words * sizeof(u32));
4836579324aSTerje Bergstrom 		host1x_bo_munmap(g->bo, gather);
4846579324aSTerje Bergstrom 
4853364cd28SArto Merilainen 		/* Store the location in the buffer */
4866579324aSTerje Bergstrom 		g->base = job->gather_copy;
4876579324aSTerje Bergstrom 		g->offset = offset;
4883364cd28SArto Merilainen 
4893364cd28SArto Merilainen 		/* Validate the job */
4903364cd28SArto Merilainen 		if (validate(&fw, g))
4913364cd28SArto Merilainen 			return -EINVAL;
4926579324aSTerje Bergstrom 
4936579324aSTerje Bergstrom 		offset += g->words * sizeof(u32);
4946579324aSTerje Bergstrom 	}
4956579324aSTerje Bergstrom 
49624c94e16SThierry Reding 	/* No relocs should remain at this point */
49724c94e16SThierry Reding 	if (fw.num_relocs)
498a9ff9995SErik Faye-Lund 		return -EINVAL;
499a9ff9995SErik Faye-Lund 
5006579324aSTerje Bergstrom 	return 0;
5016579324aSTerje Bergstrom }
5026579324aSTerje Bergstrom 
5036579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev)
5046579324aSTerje Bergstrom {
5056579324aSTerje Bergstrom 	int err;
5066579324aSTerje Bergstrom 	unsigned int i, j;
5076579324aSTerje Bergstrom 	struct host1x *host = dev_get_drvdata(dev->parent);
5086579324aSTerje Bergstrom 
5096579324aSTerje Bergstrom 	/* pin memory */
510404bfb78SMikko Perttunen 	err = pin_job(host, job);
511404bfb78SMikko Perttunen 	if (err)
5126579324aSTerje Bergstrom 		goto out;
5136579324aSTerje Bergstrom 
51447f89c10SDmitry Osipenko 	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
51547f89c10SDmitry Osipenko 		err = copy_gathers(job, dev);
51647f89c10SDmitry Osipenko 		if (err)
51747f89c10SDmitry Osipenko 			goto out;
51847f89c10SDmitry Osipenko 	}
51947f89c10SDmitry Osipenko 
5206579324aSTerje Bergstrom 	/* patch gathers */
5216579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
5226579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
5236579324aSTerje Bergstrom 
5246579324aSTerje Bergstrom 		/* process each gather mem only once */
5256579324aSTerje Bergstrom 		if (g->handled)
5266579324aSTerje Bergstrom 			continue;
5276579324aSTerje Bergstrom 
52847f89c10SDmitry Osipenko 		/* copy_gathers() sets gathers base if firewall is enabled */
52947f89c10SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
5306579324aSTerje Bergstrom 			g->base = job->gather_addr_phys[i];
5316579324aSTerje Bergstrom 
532f08ef2d1SArto Merilainen 		for (j = i + 1; j < job->num_gathers; j++) {
533f08ef2d1SArto Merilainen 			if (job->gathers[j].bo == g->bo) {
5346579324aSTerje Bergstrom 				job->gathers[j].handled = true;
535f08ef2d1SArto Merilainen 				job->gathers[j].base = g->base;
536f08ef2d1SArto Merilainen 			}
537f08ef2d1SArto Merilainen 		}
5386579324aSTerje Bergstrom 
53947f89c10SDmitry Osipenko 		err = do_relocs(job, g);
5403364cd28SArto Merilainen 		if (err)
54147f89c10SDmitry Osipenko 			break;
5426579324aSTerje Bergstrom 	}
5436579324aSTerje Bergstrom 
5446579324aSTerje Bergstrom out:
545e5855aa3SDmitry Osipenko 	if (err)
546e5855aa3SDmitry Osipenko 		host1x_job_unpin(job);
5476579324aSTerje Bergstrom 	wmb();
5486579324aSTerje Bergstrom 
5496579324aSTerje Bergstrom 	return err;
5506579324aSTerje Bergstrom }
551fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin);
5526579324aSTerje Bergstrom 
5536579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job)
5546579324aSTerje Bergstrom {
555404bfb78SMikko Perttunen 	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
5566579324aSTerje Bergstrom 	unsigned int i;
5576579324aSTerje Bergstrom 
5586579324aSTerje Bergstrom 	for (i = 0; i < job->num_unpins; i++) {
5596579324aSTerje Bergstrom 		struct host1x_job_unpin_data *unpin = &job->unpins[i];
5606df633d0SThierry Reding 
561ec589232SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
562ec589232SDmitry Osipenko 		    unpin->size && host->domain) {
563404bfb78SMikko Perttunen 			iommu_unmap(host->domain, job->addr_phys[i],
564404bfb78SMikko Perttunen 				    unpin->size);
565404bfb78SMikko Perttunen 			free_iova(&host->iova,
566404bfb78SMikko Perttunen 				iova_pfn(&host->iova, job->addr_phys[i]));
567404bfb78SMikko Perttunen 		}
568404bfb78SMikko Perttunen 
5696579324aSTerje Bergstrom 		host1x_bo_unpin(unpin->bo, unpin->sgt);
5706579324aSTerje Bergstrom 		host1x_bo_put(unpin->bo);
5716579324aSTerje Bergstrom 	}
5720b8070d1SThierry Reding 
5736579324aSTerje Bergstrom 	job->num_unpins = 0;
5746579324aSTerje Bergstrom 
5756579324aSTerje Bergstrom 	if (job->gather_copy_size)
576f6e45661SLuis R. Rodriguez 		dma_free_wc(job->channel->dev, job->gather_copy_size,
577f6e45661SLuis R. Rodriguez 			    job->gather_copy_mapped, job->gather_copy);
5786579324aSTerje Bergstrom }
579fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_unpin);
5806579324aSTerje Bergstrom 
5816579324aSTerje Bergstrom /*
5826579324aSTerje Bergstrom  * Debug routine used to dump job entries
5836579324aSTerje Bergstrom  */
5846579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job)
5856579324aSTerje Bergstrom {
5866579324aSTerje Bergstrom 	dev_dbg(dev, "    SYNCPT_ID   %d\n", job->syncpt_id);
5876579324aSTerje Bergstrom 	dev_dbg(dev, "    SYNCPT_VAL  %d\n", job->syncpt_end);
5886579324aSTerje Bergstrom 	dev_dbg(dev, "    FIRST_GET   0x%x\n", job->first_get);
5896579324aSTerje Bergstrom 	dev_dbg(dev, "    TIMEOUT     %d\n", job->timeout);
5906579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_SLOTS   %d\n", job->num_slots);
5916579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_HANDLES %d\n", job->num_unpins);
5926579324aSTerje Bergstrom }
593