xref: /linux/drivers/gpu/host1x/job.c (revision ec58923215dbbeef59ee82923ee94d745f73db58)
16579324aSTerje Bergstrom /*
26579324aSTerje Bergstrom  * Tegra host1x Job
36579324aSTerje Bergstrom  *
4f08ef2d1SArto Merilainen  * Copyright (c) 2010-2015, NVIDIA Corporation.
56579324aSTerje Bergstrom  *
66579324aSTerje Bergstrom  * This program is free software; you can redistribute it and/or modify it
76579324aSTerje Bergstrom  * under the terms and conditions of the GNU General Public License,
86579324aSTerje Bergstrom  * version 2, as published by the Free Software Foundation.
96579324aSTerje Bergstrom  *
106579324aSTerje Bergstrom  * This program is distributed in the hope it will be useful, but WITHOUT
116579324aSTerje Bergstrom  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
126579324aSTerje Bergstrom  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
136579324aSTerje Bergstrom  * more details.
146579324aSTerje Bergstrom  *
156579324aSTerje Bergstrom  * You should have received a copy of the GNU General Public License
166579324aSTerje Bergstrom  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
176579324aSTerje Bergstrom  */
186579324aSTerje Bergstrom 
196579324aSTerje Bergstrom #include <linux/dma-mapping.h>
206579324aSTerje Bergstrom #include <linux/err.h>
2135d747a8SThierry Reding #include <linux/host1x.h>
226579324aSTerje Bergstrom #include <linux/kref.h>
236579324aSTerje Bergstrom #include <linux/module.h>
246579324aSTerje Bergstrom #include <linux/scatterlist.h>
256579324aSTerje Bergstrom #include <linux/slab.h>
266579324aSTerje Bergstrom #include <linux/vmalloc.h>
276579324aSTerje Bergstrom #include <trace/events/host1x.h>
286579324aSTerje Bergstrom 
296579324aSTerje Bergstrom #include "channel.h"
306579324aSTerje Bergstrom #include "dev.h"
316579324aSTerje Bergstrom #include "job.h"
326579324aSTerje Bergstrom #include "syncpt.h"
336579324aSTerje Bergstrom 
34a47ac10eSDmitry Osipenko #define HOST1X_WAIT_SYNCPT_OFFSET 0x8
35a47ac10eSDmitry Osipenko 
366579324aSTerje Bergstrom struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
3724c94e16SThierry Reding 				    u32 num_cmdbufs, u32 num_relocs)
386579324aSTerje Bergstrom {
396579324aSTerje Bergstrom 	struct host1x_job *job = NULL;
406579324aSTerje Bergstrom 	unsigned int num_unpins = num_cmdbufs + num_relocs;
416579324aSTerje Bergstrom 	u64 total;
426579324aSTerje Bergstrom 	void *mem;
436579324aSTerje Bergstrom 
446579324aSTerje Bergstrom 	/* Check that we're not going to overflow */
456579324aSTerje Bergstrom 	total = sizeof(struct host1x_job) +
46f5fda676SDan Carpenter 		(u64)num_relocs * sizeof(struct host1x_reloc) +
47f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
48f5fda676SDan Carpenter 		(u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
49f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(dma_addr_t) +
50f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(u32 *);
516579324aSTerje Bergstrom 	if (total > ULONG_MAX)
526579324aSTerje Bergstrom 		return NULL;
536579324aSTerje Bergstrom 
546579324aSTerje Bergstrom 	mem = job = kzalloc(total, GFP_KERNEL);
556579324aSTerje Bergstrom 	if (!job)
566579324aSTerje Bergstrom 		return NULL;
576579324aSTerje Bergstrom 
586579324aSTerje Bergstrom 	kref_init(&job->ref);
596579324aSTerje Bergstrom 	job->channel = ch;
606579324aSTerje Bergstrom 
616579324aSTerje Bergstrom 	/* Redistribute memory to the structs  */
626579324aSTerje Bergstrom 	mem += sizeof(struct host1x_job);
6306490bb9SThierry Reding 	job->relocs = num_relocs ? mem : NULL;
646579324aSTerje Bergstrom 	mem += num_relocs * sizeof(struct host1x_reloc);
656579324aSTerje Bergstrom 	job->unpins = num_unpins ? mem : NULL;
666579324aSTerje Bergstrom 	mem += num_unpins * sizeof(struct host1x_job_unpin_data);
676579324aSTerje Bergstrom 	job->gathers = num_cmdbufs ? mem : NULL;
686579324aSTerje Bergstrom 	mem += num_cmdbufs * sizeof(struct host1x_job_gather);
696579324aSTerje Bergstrom 	job->addr_phys = num_unpins ? mem : NULL;
706579324aSTerje Bergstrom 
716579324aSTerje Bergstrom 	job->reloc_addr_phys = job->addr_phys;
726579324aSTerje Bergstrom 	job->gather_addr_phys = &job->addr_phys[num_relocs];
736579324aSTerje Bergstrom 
746579324aSTerje Bergstrom 	return job;
756579324aSTerje Bergstrom }
76fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_alloc);
776579324aSTerje Bergstrom 
786579324aSTerje Bergstrom struct host1x_job *host1x_job_get(struct host1x_job *job)
796579324aSTerje Bergstrom {
806579324aSTerje Bergstrom 	kref_get(&job->ref);
816579324aSTerje Bergstrom 	return job;
826579324aSTerje Bergstrom }
83fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_get);
846579324aSTerje Bergstrom 
856579324aSTerje Bergstrom static void job_free(struct kref *ref)
866579324aSTerje Bergstrom {
876579324aSTerje Bergstrom 	struct host1x_job *job = container_of(ref, struct host1x_job, ref);
886579324aSTerje Bergstrom 
896579324aSTerje Bergstrom 	kfree(job);
906579324aSTerje Bergstrom }
916579324aSTerje Bergstrom 
926579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job)
936579324aSTerje Bergstrom {
946579324aSTerje Bergstrom 	kref_put(&job->ref, job_free);
956579324aSTerje Bergstrom }
96fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put);
976579324aSTerje Bergstrom 
986579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
99326bbd79SThierry Reding 			   unsigned int words, unsigned int offset)
1006579324aSTerje Bergstrom {
101326bbd79SThierry Reding 	struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
1026579324aSTerje Bergstrom 
103326bbd79SThierry Reding 	gather->words = words;
104326bbd79SThierry Reding 	gather->bo = bo;
105326bbd79SThierry Reding 	gather->offset = offset;
106326bbd79SThierry Reding 
1076579324aSTerje Bergstrom 	job->num_gathers++;
1086579324aSTerje Bergstrom }
109fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather);
1106579324aSTerje Bergstrom 
111404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
1126579324aSTerje Bergstrom {
1136579324aSTerje Bergstrom 	unsigned int i;
114404bfb78SMikko Perttunen 	int err;
1156579324aSTerje Bergstrom 
1166579324aSTerje Bergstrom 	job->num_unpins = 0;
1176579324aSTerje Bergstrom 
1186579324aSTerje Bergstrom 	for (i = 0; i < job->num_relocs; i++) {
11906490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
1206579324aSTerje Bergstrom 		struct sg_table *sgt;
1216579324aSTerje Bergstrom 		dma_addr_t phys_addr;
1226579324aSTerje Bergstrom 
123961e3beaSThierry Reding 		reloc->target.bo = host1x_bo_get(reloc->target.bo);
124404bfb78SMikko Perttunen 		if (!reloc->target.bo) {
125404bfb78SMikko Perttunen 			err = -EINVAL;
1266579324aSTerje Bergstrom 			goto unpin;
127404bfb78SMikko Perttunen 		}
1286579324aSTerje Bergstrom 
129961e3beaSThierry Reding 		phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
1306579324aSTerje Bergstrom 
1316579324aSTerje Bergstrom 		job->addr_phys[job->num_unpins] = phys_addr;
132961e3beaSThierry Reding 		job->unpins[job->num_unpins].bo = reloc->target.bo;
1336579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
1346579324aSTerje Bergstrom 		job->num_unpins++;
1356579324aSTerje Bergstrom 	}
1366579324aSTerje Bergstrom 
1376579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
1386579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
139404bfb78SMikko Perttunen 		size_t gather_size = 0;
140404bfb78SMikko Perttunen 		struct scatterlist *sg;
1416579324aSTerje Bergstrom 		struct sg_table *sgt;
1426579324aSTerje Bergstrom 		dma_addr_t phys_addr;
143404bfb78SMikko Perttunen 		unsigned long shift;
144404bfb78SMikko Perttunen 		struct iova *alloc;
145404bfb78SMikko Perttunen 		unsigned int j;
1466579324aSTerje Bergstrom 
1476579324aSTerje Bergstrom 		g->bo = host1x_bo_get(g->bo);
148404bfb78SMikko Perttunen 		if (!g->bo) {
149404bfb78SMikko Perttunen 			err = -EINVAL;
1506579324aSTerje Bergstrom 			goto unpin;
151404bfb78SMikko Perttunen 		}
1526579324aSTerje Bergstrom 
1536579324aSTerje Bergstrom 		phys_addr = host1x_bo_pin(g->bo, &sgt);
1546579324aSTerje Bergstrom 
155404bfb78SMikko Perttunen 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
156404bfb78SMikko Perttunen 			for_each_sg(sgt->sgl, sg, sgt->nents, j)
157404bfb78SMikko Perttunen 				gather_size += sg->length;
158404bfb78SMikko Perttunen 			gather_size = iova_align(&host->iova, gather_size);
159404bfb78SMikko Perttunen 
160404bfb78SMikko Perttunen 			shift = iova_shift(&host->iova);
161404bfb78SMikko Perttunen 			alloc = alloc_iova(&host->iova, gather_size >> shift,
162404bfb78SMikko Perttunen 					   host->iova_end >> shift, true);
163404bfb78SMikko Perttunen 			if (!alloc) {
164404bfb78SMikko Perttunen 				err = -ENOMEM;
165404bfb78SMikko Perttunen 				goto unpin;
166404bfb78SMikko Perttunen 			}
167404bfb78SMikko Perttunen 
168404bfb78SMikko Perttunen 			err = iommu_map_sg(host->domain,
169404bfb78SMikko Perttunen 					iova_dma_addr(&host->iova, alloc),
170404bfb78SMikko Perttunen 					sgt->sgl, sgt->nents, IOMMU_READ);
171404bfb78SMikko Perttunen 			if (err == 0) {
172404bfb78SMikko Perttunen 				__free_iova(&host->iova, alloc);
173404bfb78SMikko Perttunen 				err = -EINVAL;
174404bfb78SMikko Perttunen 				goto unpin;
175404bfb78SMikko Perttunen 			}
176404bfb78SMikko Perttunen 
177404bfb78SMikko Perttunen 			job->addr_phys[job->num_unpins] =
178404bfb78SMikko Perttunen 				iova_dma_addr(&host->iova, alloc);
179404bfb78SMikko Perttunen 			job->unpins[job->num_unpins].size = gather_size;
180404bfb78SMikko Perttunen 		} else {
1816579324aSTerje Bergstrom 			job->addr_phys[job->num_unpins] = phys_addr;
182404bfb78SMikko Perttunen 		}
183404bfb78SMikko Perttunen 
184404bfb78SMikko Perttunen 		job->gather_addr_phys[i] = job->addr_phys[job->num_unpins];
185404bfb78SMikko Perttunen 
1866579324aSTerje Bergstrom 		job->unpins[job->num_unpins].bo = g->bo;
1876579324aSTerje Bergstrom 		job->unpins[job->num_unpins].sgt = sgt;
1886579324aSTerje Bergstrom 		job->num_unpins++;
1896579324aSTerje Bergstrom 	}
1906579324aSTerje Bergstrom 
191404bfb78SMikko Perttunen 	return 0;
1926579324aSTerje Bergstrom 
1936579324aSTerje Bergstrom unpin:
1946579324aSTerje Bergstrom 	host1x_job_unpin(job);
195404bfb78SMikko Perttunen 	return err;
1966579324aSTerje Bergstrom }
1976579324aSTerje Bergstrom 
19847f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
1996579324aSTerje Bergstrom {
2006579324aSTerje Bergstrom 	u32 last_page = ~0;
2016579324aSTerje Bergstrom 	void *cmdbuf_page_addr = NULL;
20247f89c10SDmitry Osipenko 	struct host1x_bo *cmdbuf = g->bo;
203d4ad3ad9SThierry Reding 	unsigned int i;
2046579324aSTerje Bergstrom 
2056579324aSTerje Bergstrom 	/* pin & patch the relocs for one gather */
2063364cd28SArto Merilainen 	for (i = 0; i < job->num_relocs; i++) {
20706490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
2086579324aSTerje Bergstrom 		u32 reloc_addr = (job->reloc_addr_phys[i] +
209961e3beaSThierry Reding 				  reloc->target.offset) >> reloc->shift;
2106579324aSTerje Bergstrom 		u32 *target;
2116579324aSTerje Bergstrom 
2126579324aSTerje Bergstrom 		/* skip all other gathers */
213961e3beaSThierry Reding 		if (cmdbuf != reloc->cmdbuf.bo)
2146579324aSTerje Bergstrom 			continue;
2156579324aSTerje Bergstrom 
21647f89c10SDmitry Osipenko 		if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
21747f89c10SDmitry Osipenko 			target = (u32 *)job->gather_copy_mapped +
21847f89c10SDmitry Osipenko 					reloc->cmdbuf.offset / sizeof(u32) +
21947f89c10SDmitry Osipenko 						g->offset / sizeof(u32);
22047f89c10SDmitry Osipenko 			goto patch_reloc;
22147f89c10SDmitry Osipenko 		}
22247f89c10SDmitry Osipenko 
223961e3beaSThierry Reding 		if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
2246579324aSTerje Bergstrom 			if (cmdbuf_page_addr)
2256579324aSTerje Bergstrom 				host1x_bo_kunmap(cmdbuf, last_page,
2266579324aSTerje Bergstrom 						 cmdbuf_page_addr);
2276579324aSTerje Bergstrom 
2286579324aSTerje Bergstrom 			cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
229961e3beaSThierry Reding 					reloc->cmdbuf.offset >> PAGE_SHIFT);
230961e3beaSThierry Reding 			last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
2316579324aSTerje Bergstrom 
2326579324aSTerje Bergstrom 			if (unlikely(!cmdbuf_page_addr)) {
2336579324aSTerje Bergstrom 				pr_err("Could not map cmdbuf for relocation\n");
2346579324aSTerje Bergstrom 				return -ENOMEM;
2356579324aSTerje Bergstrom 			}
2366579324aSTerje Bergstrom 		}
2376579324aSTerje Bergstrom 
238961e3beaSThierry Reding 		target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
23947f89c10SDmitry Osipenko patch_reloc:
2406579324aSTerje Bergstrom 		*target = reloc_addr;
2416579324aSTerje Bergstrom 	}
2426579324aSTerje Bergstrom 
2436579324aSTerje Bergstrom 	if (cmdbuf_page_addr)
2446579324aSTerje Bergstrom 		host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
2456579324aSTerje Bergstrom 
2466579324aSTerje Bergstrom 	return 0;
2476579324aSTerje Bergstrom }
2486579324aSTerje Bergstrom 
2495060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
2506579324aSTerje Bergstrom 			unsigned int offset)
2516579324aSTerje Bergstrom {
2526579324aSTerje Bergstrom 	offset *= sizeof(u32);
2536579324aSTerje Bergstrom 
254961e3beaSThierry Reding 	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
2555060d8ecSArto Merilainen 		return false;
2566579324aSTerje Bergstrom 
257571cbf70SDmitry Osipenko 	/* relocation shift value validation isn't implemented yet */
258571cbf70SDmitry Osipenko 	if (reloc->shift)
259571cbf70SDmitry Osipenko 		return false;
260571cbf70SDmitry Osipenko 
2615060d8ecSArto Merilainen 	return true;
2626579324aSTerje Bergstrom }
2636579324aSTerje Bergstrom 
2646579324aSTerje Bergstrom struct host1x_firewall {
2656579324aSTerje Bergstrom 	struct host1x_job *job;
2666579324aSTerje Bergstrom 	struct device *dev;
2676579324aSTerje Bergstrom 
2686579324aSTerje Bergstrom 	unsigned int num_relocs;
2696579324aSTerje Bergstrom 	struct host1x_reloc *reloc;
2706579324aSTerje Bergstrom 
271d7fbcf47SThierry Reding 	struct host1x_bo *cmdbuf;
2726579324aSTerje Bergstrom 	unsigned int offset;
2736579324aSTerje Bergstrom 
2746579324aSTerje Bergstrom 	u32 words;
2756579324aSTerje Bergstrom 	u32 class;
2766579324aSTerje Bergstrom 	u32 reg;
2776579324aSTerje Bergstrom 	u32 mask;
2786579324aSTerje Bergstrom 	u32 count;
2796579324aSTerje Bergstrom };
2806579324aSTerje Bergstrom 
281d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset)
282d77563ffSThierry Reding {
2830f563a4bSDmitry Osipenko 	if (!fw->job->is_addr_reg)
2840f563a4bSDmitry Osipenko 		return 0;
2850f563a4bSDmitry Osipenko 
286d77563ffSThierry Reding 	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
287d77563ffSThierry Reding 		if (!fw->num_relocs)
288d77563ffSThierry Reding 			return -EINVAL;
289d77563ffSThierry Reding 
290d77563ffSThierry Reding 		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
291d77563ffSThierry Reding 			return -EINVAL;
292d77563ffSThierry Reding 
293d77563ffSThierry Reding 		fw->num_relocs--;
294d77563ffSThierry Reding 		fw->reloc++;
295d77563ffSThierry Reding 	}
296d77563ffSThierry Reding 
297d77563ffSThierry Reding 	return 0;
298d77563ffSThierry Reding }
299d77563ffSThierry Reding 
3000f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class)
3010f563a4bSDmitry Osipenko {
3020f563a4bSDmitry Osipenko 	if (!fw->job->is_valid_class) {
3030f563a4bSDmitry Osipenko 		if (fw->class != class)
3040f563a4bSDmitry Osipenko 			return -EINVAL;
3050f563a4bSDmitry Osipenko 	} else {
3060f563a4bSDmitry Osipenko 		if (!fw->job->is_valid_class(fw->class))
3070f563a4bSDmitry Osipenko 			return -EINVAL;
3080f563a4bSDmitry Osipenko 	}
3090f563a4bSDmitry Osipenko 
3100f563a4bSDmitry Osipenko 	return 0;
3110f563a4bSDmitry Osipenko }
3120f563a4bSDmitry Osipenko 
3136579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw)
3146579324aSTerje Bergstrom {
3156579324aSTerje Bergstrom 	u32 mask = fw->mask;
3166579324aSTerje Bergstrom 	u32 reg = fw->reg;
317d77563ffSThierry Reding 	int ret;
3186579324aSTerje Bergstrom 
3196579324aSTerje Bergstrom 	while (mask) {
3206579324aSTerje Bergstrom 		if (fw->words == 0)
3216579324aSTerje Bergstrom 			return -EINVAL;
3226579324aSTerje Bergstrom 
3236579324aSTerje Bergstrom 		if (mask & 1) {
324d77563ffSThierry Reding 			ret = check_register(fw, reg);
325d77563ffSThierry Reding 			if (ret < 0)
326d77563ffSThierry Reding 				return ret;
327d77563ffSThierry Reding 
3286579324aSTerje Bergstrom 			fw->words--;
3296579324aSTerje Bergstrom 			fw->offset++;
3306579324aSTerje Bergstrom 		}
3316579324aSTerje Bergstrom 		mask >>= 1;
3326579324aSTerje Bergstrom 		reg++;
3336579324aSTerje Bergstrom 	}
3346579324aSTerje Bergstrom 
3356579324aSTerje Bergstrom 	return 0;
3366579324aSTerje Bergstrom }
3376579324aSTerje Bergstrom 
3386579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw)
3396579324aSTerje Bergstrom {
3406579324aSTerje Bergstrom 	u32 count = fw->count;
3416579324aSTerje Bergstrom 	u32 reg = fw->reg;
342d77563ffSThierry Reding 	int ret;
3436579324aSTerje Bergstrom 
34464c173d3STerje Bergstrom 	while (count) {
3456579324aSTerje Bergstrom 		if (fw->words == 0)
3466579324aSTerje Bergstrom 			return -EINVAL;
3476579324aSTerje Bergstrom 
348d77563ffSThierry Reding 		ret = check_register(fw, reg);
349d77563ffSThierry Reding 		if (ret < 0)
350d77563ffSThierry Reding 			return ret;
351d77563ffSThierry Reding 
3526579324aSTerje Bergstrom 		reg++;
3536579324aSTerje Bergstrom 		fw->words--;
3546579324aSTerje Bergstrom 		fw->offset++;
3556579324aSTerje Bergstrom 		count--;
3566579324aSTerje Bergstrom 	}
3576579324aSTerje Bergstrom 
3586579324aSTerje Bergstrom 	return 0;
3596579324aSTerje Bergstrom }
3606579324aSTerje Bergstrom 
3616579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw)
3626579324aSTerje Bergstrom {
3636579324aSTerje Bergstrom 	u32 count = fw->count;
364d77563ffSThierry Reding 	int ret;
3656579324aSTerje Bergstrom 
3666579324aSTerje Bergstrom 	while (count) {
3676579324aSTerje Bergstrom 		if (fw->words == 0)
3686579324aSTerje Bergstrom 			return -EINVAL;
3696579324aSTerje Bergstrom 
370d77563ffSThierry Reding 		ret = check_register(fw, fw->reg);
371d77563ffSThierry Reding 		if (ret < 0)
372d77563ffSThierry Reding 			return ret;
373d77563ffSThierry Reding 
3746579324aSTerje Bergstrom 		fw->words--;
3756579324aSTerje Bergstrom 		fw->offset++;
3766579324aSTerje Bergstrom 		count--;
3776579324aSTerje Bergstrom 	}
3786579324aSTerje Bergstrom 
3796579324aSTerje Bergstrom 	return 0;
3806579324aSTerje Bergstrom }
3816579324aSTerje Bergstrom 
382afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
3836579324aSTerje Bergstrom {
3843364cd28SArto Merilainen 	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
3853364cd28SArto Merilainen 		(g->offset / sizeof(u32));
3860f563a4bSDmitry Osipenko 	u32 job_class = fw->class;
3876579324aSTerje Bergstrom 	int err = 0;
3886579324aSTerje Bergstrom 
389afac0e43STerje Bergstrom 	fw->words = g->words;
390d7fbcf47SThierry Reding 	fw->cmdbuf = g->bo;
391afac0e43STerje Bergstrom 	fw->offset = 0;
3926579324aSTerje Bergstrom 
393afac0e43STerje Bergstrom 	while (fw->words && !err) {
394afac0e43STerje Bergstrom 		u32 word = cmdbuf_base[fw->offset];
3956579324aSTerje Bergstrom 		u32 opcode = (word & 0xf0000000) >> 28;
3966579324aSTerje Bergstrom 
397afac0e43STerje Bergstrom 		fw->mask = 0;
398afac0e43STerje Bergstrom 		fw->reg = 0;
399afac0e43STerje Bergstrom 		fw->count = 0;
400afac0e43STerje Bergstrom 		fw->words--;
401afac0e43STerje Bergstrom 		fw->offset++;
4026579324aSTerje Bergstrom 
4036579324aSTerje Bergstrom 		switch (opcode) {
4046579324aSTerje Bergstrom 		case 0:
405afac0e43STerje Bergstrom 			fw->class = word >> 6 & 0x3ff;
406afac0e43STerje Bergstrom 			fw->mask = word & 0x3f;
407afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
4080f563a4bSDmitry Osipenko 			err = check_class(fw, job_class);
4090f563a4bSDmitry Osipenko 			if (!err)
410afac0e43STerje Bergstrom 				err = check_mask(fw);
4116579324aSTerje Bergstrom 			if (err)
4126579324aSTerje Bergstrom 				goto out;
4136579324aSTerje Bergstrom 			break;
4146579324aSTerje Bergstrom 		case 1:
415afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
416afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
417afac0e43STerje Bergstrom 			err = check_incr(fw);
4186579324aSTerje Bergstrom 			if (err)
4196579324aSTerje Bergstrom 				goto out;
4206579324aSTerje Bergstrom 			break;
4216579324aSTerje Bergstrom 
4226579324aSTerje Bergstrom 		case 2:
423afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
424afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
425afac0e43STerje Bergstrom 			err = check_nonincr(fw);
4266579324aSTerje Bergstrom 			if (err)
4276579324aSTerje Bergstrom 				goto out;
4286579324aSTerje Bergstrom 			break;
4296579324aSTerje Bergstrom 
4306579324aSTerje Bergstrom 		case 3:
431afac0e43STerje Bergstrom 			fw->mask = word & 0xffff;
432afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
433afac0e43STerje Bergstrom 			err = check_mask(fw);
4346579324aSTerje Bergstrom 			if (err)
4356579324aSTerje Bergstrom 				goto out;
4366579324aSTerje Bergstrom 			break;
4376579324aSTerje Bergstrom 		case 4:
4386579324aSTerje Bergstrom 		case 14:
4396579324aSTerje Bergstrom 			break;
4406579324aSTerje Bergstrom 		default:
4416579324aSTerje Bergstrom 			err = -EINVAL;
4426579324aSTerje Bergstrom 			break;
4436579324aSTerje Bergstrom 		}
4446579324aSTerje Bergstrom 	}
4456579324aSTerje Bergstrom 
4466579324aSTerje Bergstrom out:
4476579324aSTerje Bergstrom 	return err;
4486579324aSTerje Bergstrom }
4496579324aSTerje Bergstrom 
4506579324aSTerje Bergstrom static inline int copy_gathers(struct host1x_job *job, struct device *dev)
4516579324aSTerje Bergstrom {
4523364cd28SArto Merilainen 	struct host1x_firewall fw;
4536579324aSTerje Bergstrom 	size_t size = 0;
4546579324aSTerje Bergstrom 	size_t offset = 0;
455d4ad3ad9SThierry Reding 	unsigned int i;
4566579324aSTerje Bergstrom 
4573364cd28SArto Merilainen 	fw.job = job;
4583364cd28SArto Merilainen 	fw.dev = dev;
45906490bb9SThierry Reding 	fw.reloc = job->relocs;
4603364cd28SArto Merilainen 	fw.num_relocs = job->num_relocs;
4613833d16fSDmitry Osipenko 	fw.class = job->class;
4623364cd28SArto Merilainen 
4636579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
4646579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
4656df633d0SThierry Reding 
4666579324aSTerje Bergstrom 		size += g->words * sizeof(u32);
4676579324aSTerje Bergstrom 	}
4686579324aSTerje Bergstrom 
46943240bbdSDmitry Osipenko 	/*
47043240bbdSDmitry Osipenko 	 * Try a non-blocking allocation from a higher priority pools first,
47143240bbdSDmitry Osipenko 	 * as awaiting for the allocation here is a major performance hit.
47243240bbdSDmitry Osipenko 	 */
473f6e45661SLuis R. Rodriguez 	job->gather_copy_mapped = dma_alloc_wc(dev, size, &job->gather_copy,
47443240bbdSDmitry Osipenko 					       GFP_NOWAIT);
47543240bbdSDmitry Osipenko 
47643240bbdSDmitry Osipenko 	/* the higher priority allocation failed, try the generic-blocking */
47743240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
47843240bbdSDmitry Osipenko 		job->gather_copy_mapped = dma_alloc_wc(dev, size,
47943240bbdSDmitry Osipenko 						       &job->gather_copy,
4806579324aSTerje Bergstrom 						       GFP_KERNEL);
48143240bbdSDmitry Osipenko 	if (!job->gather_copy_mapped)
482745cecc0SDan Carpenter 		return -ENOMEM;
4836579324aSTerje Bergstrom 
4846579324aSTerje Bergstrom 	job->gather_copy_size = size;
4856579324aSTerje Bergstrom 
4866579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
4876579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
4886579324aSTerje Bergstrom 		void *gather;
4896579324aSTerje Bergstrom 
4903364cd28SArto Merilainen 		/* Copy the gather */
4916579324aSTerje Bergstrom 		gather = host1x_bo_mmap(g->bo);
4926579324aSTerje Bergstrom 		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
4936579324aSTerje Bergstrom 		       g->words * sizeof(u32));
4946579324aSTerje Bergstrom 		host1x_bo_munmap(g->bo, gather);
4956579324aSTerje Bergstrom 
4963364cd28SArto Merilainen 		/* Store the location in the buffer */
4976579324aSTerje Bergstrom 		g->base = job->gather_copy;
4986579324aSTerje Bergstrom 		g->offset = offset;
4993364cd28SArto Merilainen 
5003364cd28SArto Merilainen 		/* Validate the job */
5013364cd28SArto Merilainen 		if (validate(&fw, g))
5023364cd28SArto Merilainen 			return -EINVAL;
5036579324aSTerje Bergstrom 
5046579324aSTerje Bergstrom 		offset += g->words * sizeof(u32);
5056579324aSTerje Bergstrom 	}
5066579324aSTerje Bergstrom 
50724c94e16SThierry Reding 	/* No relocs should remain at this point */
50824c94e16SThierry Reding 	if (fw.num_relocs)
509a9ff9995SErik Faye-Lund 		return -EINVAL;
510a9ff9995SErik Faye-Lund 
5116579324aSTerje Bergstrom 	return 0;
5126579324aSTerje Bergstrom }
5136579324aSTerje Bergstrom 
5146579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev)
5156579324aSTerje Bergstrom {
5166579324aSTerje Bergstrom 	int err;
5176579324aSTerje Bergstrom 	unsigned int i, j;
5186579324aSTerje Bergstrom 	struct host1x *host = dev_get_drvdata(dev->parent);
5196579324aSTerje Bergstrom 
5206579324aSTerje Bergstrom 	/* pin memory */
521404bfb78SMikko Perttunen 	err = pin_job(host, job);
522404bfb78SMikko Perttunen 	if (err)
5236579324aSTerje Bergstrom 		goto out;
5246579324aSTerje Bergstrom 
52547f89c10SDmitry Osipenko 	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
52647f89c10SDmitry Osipenko 		err = copy_gathers(job, dev);
52747f89c10SDmitry Osipenko 		if (err)
52847f89c10SDmitry Osipenko 			goto out;
52947f89c10SDmitry Osipenko 	}
53047f89c10SDmitry Osipenko 
5316579324aSTerje Bergstrom 	/* patch gathers */
5326579324aSTerje Bergstrom 	for (i = 0; i < job->num_gathers; i++) {
5336579324aSTerje Bergstrom 		struct host1x_job_gather *g = &job->gathers[i];
5346579324aSTerje Bergstrom 
5356579324aSTerje Bergstrom 		/* process each gather mem only once */
5366579324aSTerje Bergstrom 		if (g->handled)
5376579324aSTerje Bergstrom 			continue;
5386579324aSTerje Bergstrom 
53947f89c10SDmitry Osipenko 		/* copy_gathers() sets gathers base if firewall is enabled */
54047f89c10SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
5416579324aSTerje Bergstrom 			g->base = job->gather_addr_phys[i];
5426579324aSTerje Bergstrom 
543f08ef2d1SArto Merilainen 		for (j = i + 1; j < job->num_gathers; j++) {
544f08ef2d1SArto Merilainen 			if (job->gathers[j].bo == g->bo) {
5456579324aSTerje Bergstrom 				job->gathers[j].handled = true;
546f08ef2d1SArto Merilainen 				job->gathers[j].base = g->base;
547f08ef2d1SArto Merilainen 			}
548f08ef2d1SArto Merilainen 		}
5496579324aSTerje Bergstrom 
55047f89c10SDmitry Osipenko 		err = do_relocs(job, g);
5513364cd28SArto Merilainen 		if (err)
55247f89c10SDmitry Osipenko 			break;
5536579324aSTerje Bergstrom 	}
5546579324aSTerje Bergstrom 
5556579324aSTerje Bergstrom out:
556e5855aa3SDmitry Osipenko 	if (err)
557e5855aa3SDmitry Osipenko 		host1x_job_unpin(job);
5586579324aSTerje Bergstrom 	wmb();
5596579324aSTerje Bergstrom 
5606579324aSTerje Bergstrom 	return err;
5616579324aSTerje Bergstrom }
562fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin);
5636579324aSTerje Bergstrom 
5646579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job)
5656579324aSTerje Bergstrom {
566404bfb78SMikko Perttunen 	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
5676579324aSTerje Bergstrom 	unsigned int i;
5686579324aSTerje Bergstrom 
5696579324aSTerje Bergstrom 	for (i = 0; i < job->num_unpins; i++) {
5706579324aSTerje Bergstrom 		struct host1x_job_unpin_data *unpin = &job->unpins[i];
5716df633d0SThierry Reding 
572*ec589232SDmitry Osipenko 		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
573*ec589232SDmitry Osipenko 		    unpin->size && host->domain) {
574404bfb78SMikko Perttunen 			iommu_unmap(host->domain, job->addr_phys[i],
575404bfb78SMikko Perttunen 				    unpin->size);
576404bfb78SMikko Perttunen 			free_iova(&host->iova,
577404bfb78SMikko Perttunen 				iova_pfn(&host->iova, job->addr_phys[i]));
578404bfb78SMikko Perttunen 		}
579404bfb78SMikko Perttunen 
5806579324aSTerje Bergstrom 		host1x_bo_unpin(unpin->bo, unpin->sgt);
5816579324aSTerje Bergstrom 		host1x_bo_put(unpin->bo);
5826579324aSTerje Bergstrom 	}
5830b8070d1SThierry Reding 
5846579324aSTerje Bergstrom 	job->num_unpins = 0;
5856579324aSTerje Bergstrom 
5866579324aSTerje Bergstrom 	if (job->gather_copy_size)
587f6e45661SLuis R. Rodriguez 		dma_free_wc(job->channel->dev, job->gather_copy_size,
588f6e45661SLuis R. Rodriguez 			    job->gather_copy_mapped, job->gather_copy);
5896579324aSTerje Bergstrom }
590fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_unpin);
5916579324aSTerje Bergstrom 
5926579324aSTerje Bergstrom /*
5936579324aSTerje Bergstrom  * Debug routine used to dump job entries
5946579324aSTerje Bergstrom  */
5956579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job)
5966579324aSTerje Bergstrom {
5976579324aSTerje Bergstrom 	dev_dbg(dev, "    SYNCPT_ID   %d\n", job->syncpt_id);
5986579324aSTerje Bergstrom 	dev_dbg(dev, "    SYNCPT_VAL  %d\n", job->syncpt_end);
5996579324aSTerje Bergstrom 	dev_dbg(dev, "    FIRST_GET   0x%x\n", job->first_get);
6006579324aSTerje Bergstrom 	dev_dbg(dev, "    TIMEOUT     %d\n", job->timeout);
6016579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_SLOTS   %d\n", job->num_slots);
6026579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_HANDLES %d\n", job->num_unpins);
6036579324aSTerje Bergstrom }
604