xref: /linux/drivers/gpu/host1x/job.c (revision c6aeaf56f468a565f6d2f27325fc07d35cdcd3cb)
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,
270fddaa85SMikko Perttunen 				    u32 num_cmdbufs, u32 num_relocs,
280fddaa85SMikko Perttunen 				    bool skip_firewall)
296579324aSTerje Bergstrom {
306579324aSTerje Bergstrom 	struct host1x_job *job = NULL;
3126c8de5eSDmitry Osipenko 	unsigned int num_unpins = num_relocs;
320fddaa85SMikko Perttunen 	bool enable_firewall;
336579324aSTerje Bergstrom 	u64 total;
346579324aSTerje Bergstrom 	void *mem;
356579324aSTerje Bergstrom 
360fddaa85SMikko Perttunen 	enable_firewall = IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !skip_firewall;
370fddaa85SMikko Perttunen 
380fddaa85SMikko Perttunen 	if (!enable_firewall)
3926c8de5eSDmitry Osipenko 		num_unpins += num_cmdbufs;
4026c8de5eSDmitry Osipenko 
416579324aSTerje Bergstrom 	/* Check that we're not going to overflow */
426579324aSTerje Bergstrom 	total = sizeof(struct host1x_job) +
43f5fda676SDan Carpenter 		(u64)num_relocs * sizeof(struct host1x_reloc) +
44f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
45e902585fSMikko Perttunen 		(u64)num_cmdbufs * sizeof(struct host1x_job_cmd) +
46f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(dma_addr_t) +
47f5fda676SDan Carpenter 		(u64)num_unpins * sizeof(u32 *);
486579324aSTerje Bergstrom 	if (total > ULONG_MAX)
496579324aSTerje Bergstrom 		return NULL;
506579324aSTerje Bergstrom 
516579324aSTerje Bergstrom 	mem = job = kzalloc(total, GFP_KERNEL);
526579324aSTerje Bergstrom 	if (!job)
536579324aSTerje Bergstrom 		return NULL;
546579324aSTerje Bergstrom 
550fddaa85SMikko Perttunen 	job->enable_firewall = enable_firewall;
560fddaa85SMikko Perttunen 
576579324aSTerje Bergstrom 	kref_init(&job->ref);
586579324aSTerje Bergstrom 	job->channel = ch;
596579324aSTerje Bergstrom 
606579324aSTerje Bergstrom 	/* Redistribute memory to the structs  */
616579324aSTerje Bergstrom 	mem += sizeof(struct host1x_job);
6206490bb9SThierry Reding 	job->relocs = num_relocs ? mem : NULL;
636579324aSTerje Bergstrom 	mem += num_relocs * sizeof(struct host1x_reloc);
646579324aSTerje Bergstrom 	job->unpins = num_unpins ? mem : NULL;
656579324aSTerje Bergstrom 	mem += num_unpins * sizeof(struct host1x_job_unpin_data);
66e902585fSMikko Perttunen 	job->cmds = num_cmdbufs ? mem : NULL;
67e902585fSMikko Perttunen 	mem += num_cmdbufs * sizeof(struct host1x_job_cmd);
686579324aSTerje Bergstrom 	job->addr_phys = num_unpins ? mem : NULL;
696579324aSTerje Bergstrom 
706579324aSTerje Bergstrom 	job->reloc_addr_phys = job->addr_phys;
716579324aSTerje Bergstrom 	job->gather_addr_phys = &job->addr_phys[num_relocs];
726579324aSTerje Bergstrom 
736579324aSTerje Bergstrom 	return job;
746579324aSTerje Bergstrom }
75fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_alloc);
766579324aSTerje Bergstrom 
776579324aSTerje Bergstrom struct host1x_job *host1x_job_get(struct host1x_job *job)
786579324aSTerje Bergstrom {
796579324aSTerje Bergstrom 	kref_get(&job->ref);
806579324aSTerje Bergstrom 	return job;
816579324aSTerje Bergstrom }
82fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_get);
836579324aSTerje Bergstrom 
846579324aSTerje Bergstrom static void job_free(struct kref *ref)
856579324aSTerje Bergstrom {
866579324aSTerje Bergstrom 	struct host1x_job *job = container_of(ref, struct host1x_job, ref);
876579324aSTerje Bergstrom 
8817a298e9SMikko Perttunen 	if (job->release)
8917a298e9SMikko Perttunen 		job->release(job);
9017a298e9SMikko Perttunen 
91c78f837aSMikko Perttunen 	if (job->waiter)
92c78f837aSMikko Perttunen 		host1x_intr_put_ref(job->syncpt->host, job->syncpt->id,
93c78f837aSMikko Perttunen 				    job->waiter, false);
94c78f837aSMikko Perttunen 
952aed4f5aSMikko Perttunen 	if (job->syncpt)
962aed4f5aSMikko Perttunen 		host1x_syncpt_put(job->syncpt);
972aed4f5aSMikko Perttunen 
986579324aSTerje Bergstrom 	kfree(job);
996579324aSTerje Bergstrom }
1006579324aSTerje Bergstrom 
1016579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job)
1026579324aSTerje Bergstrom {
1036579324aSTerje Bergstrom 	kref_put(&job->ref, job_free);
1046579324aSTerje Bergstrom }
105fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put);
1066579324aSTerje Bergstrom 
1076579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
108326bbd79SThierry Reding 			   unsigned int words, unsigned int offset)
1096579324aSTerje Bergstrom {
110e902585fSMikko Perttunen 	struct host1x_job_gather *gather = &job->cmds[job->num_cmds].gather;
1116579324aSTerje Bergstrom 
112326bbd79SThierry Reding 	gather->words = words;
113326bbd79SThierry Reding 	gather->bo = bo;
114326bbd79SThierry Reding 	gather->offset = offset;
115326bbd79SThierry Reding 
116e902585fSMikko Perttunen 	job->num_cmds++;
1176579324aSTerje Bergstrom }
118fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather);
1196579324aSTerje Bergstrom 
120e902585fSMikko Perttunen void host1x_job_add_wait(struct host1x_job *job, u32 id, u32 thresh,
121e902585fSMikko Perttunen 			 bool relative, u32 next_class)
122e902585fSMikko Perttunen {
123e902585fSMikko Perttunen 	struct host1x_job_cmd *cmd = &job->cmds[job->num_cmds];
124e902585fSMikko Perttunen 
125e902585fSMikko Perttunen 	cmd->is_wait = true;
126e902585fSMikko Perttunen 	cmd->wait.id = id;
127e902585fSMikko Perttunen 	cmd->wait.threshold = thresh;
128e902585fSMikko Perttunen 	cmd->wait.next_class = next_class;
129e902585fSMikko Perttunen 	cmd->wait.relative = relative;
130e902585fSMikko Perttunen 
131e902585fSMikko Perttunen 	job->num_cmds++;
132e902585fSMikko Perttunen }
133e902585fSMikko Perttunen EXPORT_SYMBOL(host1x_job_add_wait);
134e902585fSMikko Perttunen 
135404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
1366579324aSTerje Bergstrom {
137*c6aeaf56SThierry Reding 	unsigned long mask = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
138af1cbfb9SThierry Reding 	struct host1x_client *client = job->client;
139af1cbfb9SThierry Reding 	struct device *dev = client->dev;
140fd323e9eSDmitry Osipenko 	struct host1x_job_gather *g;
1416579324aSTerje Bergstrom 	unsigned int i;
142404bfb78SMikko Perttunen 	int err;
1436579324aSTerje Bergstrom 
1446579324aSTerje Bergstrom 	job->num_unpins = 0;
1456579324aSTerje Bergstrom 
1466579324aSTerje Bergstrom 	for (i = 0; i < job->num_relocs; i++) {
14706490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
148*c6aeaf56SThierry Reding 		enum dma_data_direction direction;
149*c6aeaf56SThierry Reding 		struct host1x_bo_mapping *map;
150*c6aeaf56SThierry Reding 		struct host1x_bo *bo;
1516579324aSTerje Bergstrom 
152961e3beaSThierry Reding 		reloc->target.bo = host1x_bo_get(reloc->target.bo);
153404bfb78SMikko Perttunen 		if (!reloc->target.bo) {
154404bfb78SMikko Perttunen 			err = -EINVAL;
1556579324aSTerje Bergstrom 			goto unpin;
156404bfb78SMikko Perttunen 		}
1576579324aSTerje Bergstrom 
158*c6aeaf56SThierry Reding 		bo = reloc->target.bo;
159af1cbfb9SThierry Reding 
160af1cbfb9SThierry Reding 		switch (reloc->flags & mask) {
161af1cbfb9SThierry Reding 		case HOST1X_RELOC_READ:
162*c6aeaf56SThierry Reding 			direction = DMA_TO_DEVICE;
163af1cbfb9SThierry Reding 			break;
164af1cbfb9SThierry Reding 
165af1cbfb9SThierry Reding 		case HOST1X_RELOC_WRITE:
166*c6aeaf56SThierry Reding 			direction = DMA_FROM_DEVICE;
167af1cbfb9SThierry Reding 			break;
168af1cbfb9SThierry Reding 
169af1cbfb9SThierry Reding 		case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
170*c6aeaf56SThierry Reding 			direction = DMA_BIDIRECTIONAL;
171af1cbfb9SThierry Reding 			break;
172af1cbfb9SThierry Reding 
173af1cbfb9SThierry Reding 		default:
174af1cbfb9SThierry Reding 			err = -EINVAL;
175af1cbfb9SThierry Reding 			goto unpin;
176af1cbfb9SThierry Reding 		}
177af1cbfb9SThierry Reding 
178*c6aeaf56SThierry Reding 		map = host1x_bo_pin(dev, bo, direction);
179*c6aeaf56SThierry Reding 		if (IS_ERR(map)) {
180*c6aeaf56SThierry Reding 			err = PTR_ERR(map);
181af1cbfb9SThierry Reding 			goto unpin;
182af1cbfb9SThierry Reding 		}
183af1cbfb9SThierry Reding 
184*c6aeaf56SThierry Reding 		/*
185*c6aeaf56SThierry Reding 		 * host1x clients are generally not able to do scatter-gather themselves, so fail
186*c6aeaf56SThierry Reding 		 * if the buffer is discontiguous and we fail to map its SG table to a single
187*c6aeaf56SThierry Reding 		 * contiguous chunk of I/O virtual memory.
188*c6aeaf56SThierry Reding 		 */
189*c6aeaf56SThierry Reding 		if (map->chunks > 1) {
190*c6aeaf56SThierry Reding 			err = -EINVAL;
191*c6aeaf56SThierry Reding 			goto unpin;
192*c6aeaf56SThierry Reding 		}
193*c6aeaf56SThierry Reding 
194*c6aeaf56SThierry Reding 		job->addr_phys[job->num_unpins] = map->phys;
195*c6aeaf56SThierry Reding 		job->unpins[job->num_unpins].map = map;
1966579324aSTerje Bergstrom 		job->num_unpins++;
1976579324aSTerje Bergstrom 	}
1986579324aSTerje Bergstrom 
19926c8de5eSDmitry Osipenko 	/*
20026c8de5eSDmitry Osipenko 	 * We will copy gathers BO content later, so there is no need to
20126c8de5eSDmitry Osipenko 	 * hold and pin them.
20226c8de5eSDmitry Osipenko 	 */
2030fddaa85SMikko Perttunen 	if (job->enable_firewall)
20426c8de5eSDmitry Osipenko 		return 0;
20526c8de5eSDmitry Osipenko 
206e902585fSMikko Perttunen 	for (i = 0; i < job->num_cmds; i++) {
207*c6aeaf56SThierry Reding 		struct host1x_bo_mapping *map;
208404bfb78SMikko Perttunen 		size_t gather_size = 0;
209404bfb78SMikko Perttunen 		struct scatterlist *sg;
210404bfb78SMikko Perttunen 		unsigned long shift;
211404bfb78SMikko Perttunen 		struct iova *alloc;
212404bfb78SMikko Perttunen 		unsigned int j;
2136579324aSTerje Bergstrom 
214e902585fSMikko Perttunen 		if (job->cmds[i].is_wait)
215e902585fSMikko Perttunen 			continue;
216e902585fSMikko Perttunen 
217e902585fSMikko Perttunen 		g = &job->cmds[i].gather;
218e902585fSMikko Perttunen 
2196579324aSTerje Bergstrom 		g->bo = host1x_bo_get(g->bo);
220404bfb78SMikko Perttunen 		if (!g->bo) {
221404bfb78SMikko Perttunen 			err = -EINVAL;
2226579324aSTerje Bergstrom 			goto unpin;
223404bfb78SMikko Perttunen 		}
2246579324aSTerje Bergstrom 
225*c6aeaf56SThierry Reding 		map = host1x_bo_pin(host->dev, g->bo, DMA_TO_DEVICE);
226*c6aeaf56SThierry Reding 		if (IS_ERR(map)) {
227*c6aeaf56SThierry Reding 			err = PTR_ERR(map);
228*c6aeaf56SThierry Reding 			goto unpin;
22980327ce3SThierry Reding 		}
2306579324aSTerje Bergstrom 
23126c8de5eSDmitry Osipenko 		if (host->domain) {
232*c6aeaf56SThierry Reding 			for_each_sgtable_sg(map->sgt, sg, j)
233404bfb78SMikko Perttunen 				gather_size += sg->length;
234*c6aeaf56SThierry Reding 
235404bfb78SMikko Perttunen 			gather_size = iova_align(&host->iova, gather_size);
236404bfb78SMikko Perttunen 
237404bfb78SMikko Perttunen 			shift = iova_shift(&host->iova);
238404bfb78SMikko Perttunen 			alloc = alloc_iova(&host->iova, gather_size >> shift,
239404bfb78SMikko Perttunen 					   host->iova_end >> shift, true);
240404bfb78SMikko Perttunen 			if (!alloc) {
241404bfb78SMikko Perttunen 				err = -ENOMEM;
242fd323e9eSDmitry Osipenko 				goto put;
243404bfb78SMikko Perttunen 			}
244404bfb78SMikko Perttunen 
245*c6aeaf56SThierry Reding 			err = iommu_map_sgtable(host->domain, iova_dma_addr(&host->iova, alloc),
246*c6aeaf56SThierry Reding 						map->sgt, IOMMU_READ);
247404bfb78SMikko Perttunen 			if (err == 0) {
248404bfb78SMikko Perttunen 				__free_iova(&host->iova, alloc);
249404bfb78SMikko Perttunen 				err = -EINVAL;
250fd323e9eSDmitry Osipenko 				goto put;
251404bfb78SMikko Perttunen 			}
252404bfb78SMikko Perttunen 
253*c6aeaf56SThierry Reding 			map->phys = iova_dma_addr(&host->iova, alloc);
254*c6aeaf56SThierry Reding 			map->size = gather_size;
255af1cbfb9SThierry Reding 		}
256404bfb78SMikko Perttunen 
257*c6aeaf56SThierry Reding 		job->addr_phys[job->num_unpins] = map->phys;
258*c6aeaf56SThierry Reding 		job->unpins[job->num_unpins].map = map;
2596579324aSTerje Bergstrom 		job->num_unpins++;
260*c6aeaf56SThierry Reding 
261*c6aeaf56SThierry Reding 		job->gather_addr_phys[i] = map->phys;
2626579324aSTerje Bergstrom 	}
2636579324aSTerje Bergstrom 
264404bfb78SMikko Perttunen 	return 0;
2656579324aSTerje Bergstrom 
266fd323e9eSDmitry Osipenko put:
267fd323e9eSDmitry Osipenko 	host1x_bo_put(g->bo);
2686579324aSTerje Bergstrom unpin:
2696579324aSTerje Bergstrom 	host1x_job_unpin(job);
270404bfb78SMikko Perttunen 	return err;
2716579324aSTerje Bergstrom }
2726579324aSTerje Bergstrom 
27347f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
2746579324aSTerje Bergstrom {
2757a8139c5SDaniel Vetter 	void *cmdbuf_addr = NULL;
27647f89c10SDmitry Osipenko 	struct host1x_bo *cmdbuf = g->bo;
277d4ad3ad9SThierry Reding 	unsigned int i;
2786579324aSTerje Bergstrom 
2796579324aSTerje Bergstrom 	/* pin & patch the relocs for one gather */
2803364cd28SArto Merilainen 	for (i = 0; i < job->num_relocs; i++) {
28106490bb9SThierry Reding 		struct host1x_reloc *reloc = &job->relocs[i];
2826579324aSTerje Bergstrom 		u32 reloc_addr = (job->reloc_addr_phys[i] +
283961e3beaSThierry Reding 				  reloc->target.offset) >> reloc->shift;
2846579324aSTerje Bergstrom 		u32 *target;
2856579324aSTerje Bergstrom 
2866579324aSTerje Bergstrom 		/* skip all other gathers */
287961e3beaSThierry Reding 		if (cmdbuf != reloc->cmdbuf.bo)
2886579324aSTerje Bergstrom 			continue;
2896579324aSTerje Bergstrom 
2900fddaa85SMikko Perttunen 		if (job->enable_firewall) {
29147f89c10SDmitry Osipenko 			target = (u32 *)job->gather_copy_mapped +
29247f89c10SDmitry Osipenko 					reloc->cmdbuf.offset / sizeof(u32) +
29347f89c10SDmitry Osipenko 						g->offset / sizeof(u32);
29447f89c10SDmitry Osipenko 			goto patch_reloc;
29547f89c10SDmitry Osipenko 		}
29647f89c10SDmitry Osipenko 
2977a8139c5SDaniel Vetter 		if (!cmdbuf_addr) {
2987a8139c5SDaniel Vetter 			cmdbuf_addr = host1x_bo_mmap(cmdbuf);
2996579324aSTerje Bergstrom 
3007a8139c5SDaniel Vetter 			if (unlikely(!cmdbuf_addr)) {
3016579324aSTerje Bergstrom 				pr_err("Could not map cmdbuf for relocation\n");
3026579324aSTerje Bergstrom 				return -ENOMEM;
3036579324aSTerje Bergstrom 			}
3046579324aSTerje Bergstrom 		}
3056579324aSTerje Bergstrom 
3067a8139c5SDaniel Vetter 		target = cmdbuf_addr + reloc->cmdbuf.offset;
30747f89c10SDmitry Osipenko patch_reloc:
3086579324aSTerje Bergstrom 		*target = reloc_addr;
3096579324aSTerje Bergstrom 	}
3106579324aSTerje Bergstrom 
3117a8139c5SDaniel Vetter 	if (cmdbuf_addr)
3127a8139c5SDaniel Vetter 		host1x_bo_munmap(cmdbuf, cmdbuf_addr);
3136579324aSTerje Bergstrom 
3146579324aSTerje Bergstrom 	return 0;
3156579324aSTerje Bergstrom }
3166579324aSTerje Bergstrom 
3175060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
3186579324aSTerje Bergstrom 			unsigned int offset)
3196579324aSTerje Bergstrom {
3206579324aSTerje Bergstrom 	offset *= sizeof(u32);
3216579324aSTerje Bergstrom 
322961e3beaSThierry Reding 	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
3235060d8ecSArto Merilainen 		return false;
3246579324aSTerje Bergstrom 
325571cbf70SDmitry Osipenko 	/* relocation shift value validation isn't implemented yet */
326571cbf70SDmitry Osipenko 	if (reloc->shift)
327571cbf70SDmitry Osipenko 		return false;
328571cbf70SDmitry Osipenko 
3295060d8ecSArto Merilainen 	return true;
3306579324aSTerje Bergstrom }
3316579324aSTerje Bergstrom 
3326579324aSTerje Bergstrom struct host1x_firewall {
3336579324aSTerje Bergstrom 	struct host1x_job *job;
3346579324aSTerje Bergstrom 	struct device *dev;
3356579324aSTerje Bergstrom 
3366579324aSTerje Bergstrom 	unsigned int num_relocs;
3376579324aSTerje Bergstrom 	struct host1x_reloc *reloc;
3386579324aSTerje Bergstrom 
339d7fbcf47SThierry Reding 	struct host1x_bo *cmdbuf;
3406579324aSTerje Bergstrom 	unsigned int offset;
3416579324aSTerje Bergstrom 
3426579324aSTerje Bergstrom 	u32 words;
3436579324aSTerje Bergstrom 	u32 class;
3446579324aSTerje Bergstrom 	u32 reg;
3456579324aSTerje Bergstrom 	u32 mask;
3466579324aSTerje Bergstrom 	u32 count;
3476579324aSTerje Bergstrom };
3486579324aSTerje Bergstrom 
349d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset)
350d77563ffSThierry Reding {
3510f563a4bSDmitry Osipenko 	if (!fw->job->is_addr_reg)
3520f563a4bSDmitry Osipenko 		return 0;
3530f563a4bSDmitry Osipenko 
354d77563ffSThierry Reding 	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
355d77563ffSThierry Reding 		if (!fw->num_relocs)
356d77563ffSThierry Reding 			return -EINVAL;
357d77563ffSThierry Reding 
358d77563ffSThierry Reding 		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
359d77563ffSThierry Reding 			return -EINVAL;
360d77563ffSThierry Reding 
361d77563ffSThierry Reding 		fw->num_relocs--;
362d77563ffSThierry Reding 		fw->reloc++;
363d77563ffSThierry Reding 	}
364d77563ffSThierry Reding 
365d77563ffSThierry Reding 	return 0;
366d77563ffSThierry Reding }
367d77563ffSThierry Reding 
3680f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class)
3690f563a4bSDmitry Osipenko {
3700f563a4bSDmitry Osipenko 	if (!fw->job->is_valid_class) {
3710f563a4bSDmitry Osipenko 		if (fw->class != class)
3720f563a4bSDmitry Osipenko 			return -EINVAL;
3730f563a4bSDmitry Osipenko 	} else {
3740f563a4bSDmitry Osipenko 		if (!fw->job->is_valid_class(fw->class))
3750f563a4bSDmitry Osipenko 			return -EINVAL;
3760f563a4bSDmitry Osipenko 	}
3770f563a4bSDmitry Osipenko 
3780f563a4bSDmitry Osipenko 	return 0;
3790f563a4bSDmitry Osipenko }
3800f563a4bSDmitry Osipenko 
3816579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw)
3826579324aSTerje Bergstrom {
3836579324aSTerje Bergstrom 	u32 mask = fw->mask;
3846579324aSTerje Bergstrom 	u32 reg = fw->reg;
385d77563ffSThierry Reding 	int ret;
3866579324aSTerje Bergstrom 
3876579324aSTerje Bergstrom 	while (mask) {
3886579324aSTerje Bergstrom 		if (fw->words == 0)
3896579324aSTerje Bergstrom 			return -EINVAL;
3906579324aSTerje Bergstrom 
3916579324aSTerje Bergstrom 		if (mask & 1) {
392d77563ffSThierry Reding 			ret = check_register(fw, reg);
393d77563ffSThierry Reding 			if (ret < 0)
394d77563ffSThierry Reding 				return ret;
395d77563ffSThierry Reding 
3966579324aSTerje Bergstrom 			fw->words--;
3976579324aSTerje Bergstrom 			fw->offset++;
3986579324aSTerje Bergstrom 		}
3996579324aSTerje Bergstrom 		mask >>= 1;
4006579324aSTerje Bergstrom 		reg++;
4016579324aSTerje Bergstrom 	}
4026579324aSTerje Bergstrom 
4036579324aSTerje Bergstrom 	return 0;
4046579324aSTerje Bergstrom }
4056579324aSTerje Bergstrom 
4066579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw)
4076579324aSTerje Bergstrom {
4086579324aSTerje Bergstrom 	u32 count = fw->count;
4096579324aSTerje Bergstrom 	u32 reg = fw->reg;
410d77563ffSThierry Reding 	int ret;
4116579324aSTerje Bergstrom 
41264c173d3STerje Bergstrom 	while (count) {
4136579324aSTerje Bergstrom 		if (fw->words == 0)
4146579324aSTerje Bergstrom 			return -EINVAL;
4156579324aSTerje Bergstrom 
416d77563ffSThierry Reding 		ret = check_register(fw, reg);
417d77563ffSThierry Reding 		if (ret < 0)
418d77563ffSThierry Reding 			return ret;
419d77563ffSThierry Reding 
4206579324aSTerje Bergstrom 		reg++;
4216579324aSTerje Bergstrom 		fw->words--;
4226579324aSTerje Bergstrom 		fw->offset++;
4236579324aSTerje Bergstrom 		count--;
4246579324aSTerje Bergstrom 	}
4256579324aSTerje Bergstrom 
4266579324aSTerje Bergstrom 	return 0;
4276579324aSTerje Bergstrom }
4286579324aSTerje Bergstrom 
4296579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw)
4306579324aSTerje Bergstrom {
4316579324aSTerje Bergstrom 	u32 count = fw->count;
432d77563ffSThierry Reding 	int ret;
4336579324aSTerje Bergstrom 
4346579324aSTerje Bergstrom 	while (count) {
4356579324aSTerje Bergstrom 		if (fw->words == 0)
4366579324aSTerje Bergstrom 			return -EINVAL;
4376579324aSTerje Bergstrom 
438d77563ffSThierry Reding 		ret = check_register(fw, fw->reg);
439d77563ffSThierry Reding 		if (ret < 0)
440d77563ffSThierry Reding 			return ret;
441d77563ffSThierry Reding 
4426579324aSTerje Bergstrom 		fw->words--;
4436579324aSTerje Bergstrom 		fw->offset++;
4446579324aSTerje Bergstrom 		count--;
4456579324aSTerje Bergstrom 	}
4466579324aSTerje Bergstrom 
4476579324aSTerje Bergstrom 	return 0;
4486579324aSTerje Bergstrom }
4496579324aSTerje Bergstrom 
450afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
4516579324aSTerje Bergstrom {
4523364cd28SArto Merilainen 	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
4533364cd28SArto Merilainen 		(g->offset / sizeof(u32));
4540f563a4bSDmitry Osipenko 	u32 job_class = fw->class;
4556579324aSTerje Bergstrom 	int err = 0;
4566579324aSTerje Bergstrom 
457afac0e43STerje Bergstrom 	fw->words = g->words;
458d7fbcf47SThierry Reding 	fw->cmdbuf = g->bo;
459afac0e43STerje Bergstrom 	fw->offset = 0;
4606579324aSTerje Bergstrom 
461afac0e43STerje Bergstrom 	while (fw->words && !err) {
462afac0e43STerje Bergstrom 		u32 word = cmdbuf_base[fw->offset];
4636579324aSTerje Bergstrom 		u32 opcode = (word & 0xf0000000) >> 28;
4646579324aSTerje Bergstrom 
465afac0e43STerje Bergstrom 		fw->mask = 0;
466afac0e43STerje Bergstrom 		fw->reg = 0;
467afac0e43STerje Bergstrom 		fw->count = 0;
468afac0e43STerje Bergstrom 		fw->words--;
469afac0e43STerje Bergstrom 		fw->offset++;
4706579324aSTerje Bergstrom 
4716579324aSTerje Bergstrom 		switch (opcode) {
4726579324aSTerje Bergstrom 		case 0:
473afac0e43STerje Bergstrom 			fw->class = word >> 6 & 0x3ff;
474afac0e43STerje Bergstrom 			fw->mask = word & 0x3f;
475afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
4760f563a4bSDmitry Osipenko 			err = check_class(fw, job_class);
4770f563a4bSDmitry Osipenko 			if (!err)
478afac0e43STerje Bergstrom 				err = check_mask(fw);
4796579324aSTerje Bergstrom 			if (err)
4806579324aSTerje Bergstrom 				goto out;
4816579324aSTerje Bergstrom 			break;
4826579324aSTerje Bergstrom 		case 1:
483afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
484afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
485afac0e43STerje Bergstrom 			err = check_incr(fw);
4866579324aSTerje Bergstrom 			if (err)
4876579324aSTerje Bergstrom 				goto out;
4886579324aSTerje Bergstrom 			break;
4896579324aSTerje Bergstrom 
4906579324aSTerje Bergstrom 		case 2:
491afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
492afac0e43STerje Bergstrom 			fw->count = word & 0xffff;
493afac0e43STerje Bergstrom 			err = check_nonincr(fw);
4946579324aSTerje Bergstrom 			if (err)
4956579324aSTerje Bergstrom 				goto out;
4966579324aSTerje Bergstrom 			break;
4976579324aSTerje Bergstrom 
4986579324aSTerje Bergstrom 		case 3:
499afac0e43STerje Bergstrom 			fw->mask = word & 0xffff;
500afac0e43STerje Bergstrom 			fw->reg = word >> 16 & 0xfff;
501afac0e43STerje Bergstrom 			err = check_mask(fw);
5026579324aSTerje Bergstrom 			if (err)
5036579324aSTerje Bergstrom 				goto out;
5046579324aSTerje Bergstrom 			break;
5056579324aSTerje Bergstrom 		case 4:
5066579324aSTerje Bergstrom 		case 14:
5076579324aSTerje Bergstrom 			break;
5086579324aSTerje Bergstrom 		default:
5096579324aSTerje Bergstrom 			err = -EINVAL;
5106579324aSTerje Bergstrom 			break;
5116579324aSTerje Bergstrom 		}
5126579324aSTerje Bergstrom 	}
5136579324aSTerje Bergstrom 
5146579324aSTerje Bergstrom out:
5156579324aSTerje Bergstrom 	return err;
5166579324aSTerje Bergstrom }
5176579324aSTerje Bergstrom 
518b78e70c0SThierry Reding static inline int copy_gathers(struct device *host, struct host1x_job *job,
519b78e70c0SThierry Reding 			       struct device *dev)
5206579324aSTerje Bergstrom {
5213364cd28SArto Merilainen 	struct host1x_firewall fw;
5226579324aSTerje Bergstrom 	size_t size = 0;
5236579324aSTerje Bergstrom 	size_t offset = 0;
524d4ad3ad9SThierry Reding 	unsigned int i;
5256579324aSTerje Bergstrom 
5263364cd28SArto Merilainen 	fw.job = job;
5273364cd28SArto Merilainen 	fw.dev = dev;
52806490bb9SThierry Reding 	fw.reloc = job->relocs;
5293364cd28SArto Merilainen 	fw.num_relocs = job->num_relocs;
5303833d16fSDmitry Osipenko 	fw.class = job->class;
5313364cd28SArto Merilainen 
532e902585fSMikko Perttunen 	for (i = 0; i < job->num_cmds; i++) {
533e902585fSMikko Perttunen 		struct host1x_job_gather *g;
534e902585fSMikko Perttunen 
535e902585fSMikko Perttunen 		if (job->cmds[i].is_wait)
536e902585fSMikko Perttunen 			continue;
537e902585fSMikko Perttunen 
538e902585fSMikko Perttunen 		g = &job->cmds[i].gather;
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 
560e902585fSMikko Perttunen 	for (i = 0; i < job->num_cmds; i++) {
561e902585fSMikko Perttunen 		struct host1x_job_gather *g;
5626579324aSTerje Bergstrom 		void *gather;
5636579324aSTerje Bergstrom 
564e902585fSMikko Perttunen 		if (job->cmds[i].is_wait)
565e902585fSMikko Perttunen 			continue;
566e902585fSMikko Perttunen 		g = &job->cmds[i].gather;
567e902585fSMikko Perttunen 
5683364cd28SArto Merilainen 		/* Copy the gather */
5696579324aSTerje Bergstrom 		gather = host1x_bo_mmap(g->bo);
5706579324aSTerje Bergstrom 		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
5716579324aSTerje Bergstrom 		       g->words * sizeof(u32));
5726579324aSTerje Bergstrom 		host1x_bo_munmap(g->bo, gather);
5736579324aSTerje Bergstrom 
5743364cd28SArto Merilainen 		/* Store the location in the buffer */
5756579324aSTerje Bergstrom 		g->base = job->gather_copy;
5766579324aSTerje Bergstrom 		g->offset = offset;
5773364cd28SArto Merilainen 
5783364cd28SArto Merilainen 		/* Validate the job */
5793364cd28SArto Merilainen 		if (validate(&fw, g))
5803364cd28SArto Merilainen 			return -EINVAL;
5816579324aSTerje Bergstrom 
5826579324aSTerje Bergstrom 		offset += g->words * sizeof(u32);
5836579324aSTerje Bergstrom 	}
5846579324aSTerje Bergstrom 
58524c94e16SThierry Reding 	/* No relocs should remain at this point */
58624c94e16SThierry Reding 	if (fw.num_relocs)
587a9ff9995SErik Faye-Lund 		return -EINVAL;
588a9ff9995SErik Faye-Lund 
5896579324aSTerje Bergstrom 	return 0;
5906579324aSTerje Bergstrom }
5916579324aSTerje Bergstrom 
5926579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev)
5936579324aSTerje Bergstrom {
5946579324aSTerje Bergstrom 	int err;
5956579324aSTerje Bergstrom 	unsigned int i, j;
5966579324aSTerje Bergstrom 	struct host1x *host = dev_get_drvdata(dev->parent);
5976579324aSTerje Bergstrom 
5986579324aSTerje Bergstrom 	/* pin memory */
599404bfb78SMikko Perttunen 	err = pin_job(host, job);
600404bfb78SMikko Perttunen 	if (err)
6016579324aSTerje Bergstrom 		goto out;
6026579324aSTerje Bergstrom 
6030fddaa85SMikko Perttunen 	if (job->enable_firewall) {
604b78e70c0SThierry Reding 		err = copy_gathers(host->dev, job, dev);
60547f89c10SDmitry Osipenko 		if (err)
60647f89c10SDmitry Osipenko 			goto out;
60747f89c10SDmitry Osipenko 	}
60847f89c10SDmitry Osipenko 
6096579324aSTerje Bergstrom 	/* patch gathers */
610e902585fSMikko Perttunen 	for (i = 0; i < job->num_cmds; i++) {
611e902585fSMikko Perttunen 		struct host1x_job_gather *g;
612e902585fSMikko Perttunen 
613e902585fSMikko Perttunen 		if (job->cmds[i].is_wait)
614e902585fSMikko Perttunen 			continue;
615e902585fSMikko Perttunen 		g = &job->cmds[i].gather;
6166579324aSTerje Bergstrom 
6176579324aSTerje Bergstrom 		/* process each gather mem only once */
6186579324aSTerje Bergstrom 		if (g->handled)
6196579324aSTerje Bergstrom 			continue;
6206579324aSTerje Bergstrom 
62147f89c10SDmitry Osipenko 		/* copy_gathers() sets gathers base if firewall is enabled */
6220fddaa85SMikko Perttunen 		if (!job->enable_firewall)
6236579324aSTerje Bergstrom 			g->base = job->gather_addr_phys[i];
6246579324aSTerje Bergstrom 
625e902585fSMikko Perttunen 		for (j = i + 1; j < job->num_cmds; j++) {
626e902585fSMikko Perttunen 			if (!job->cmds[j].is_wait &&
627e902585fSMikko Perttunen 			    job->cmds[j].gather.bo == g->bo) {
628e902585fSMikko Perttunen 				job->cmds[j].gather.handled = true;
629e902585fSMikko Perttunen 				job->cmds[j].gather.base = g->base;
630f08ef2d1SArto Merilainen 			}
631f08ef2d1SArto Merilainen 		}
6326579324aSTerje Bergstrom 
63347f89c10SDmitry Osipenko 		err = do_relocs(job, g);
6343364cd28SArto Merilainen 		if (err)
63547f89c10SDmitry Osipenko 			break;
6366579324aSTerje Bergstrom 	}
6376579324aSTerje Bergstrom 
6386579324aSTerje Bergstrom out:
639e5855aa3SDmitry Osipenko 	if (err)
640e5855aa3SDmitry Osipenko 		host1x_job_unpin(job);
6416579324aSTerje Bergstrom 	wmb();
6426579324aSTerje Bergstrom 
6436579324aSTerje Bergstrom 	return err;
6446579324aSTerje Bergstrom }
645fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin);
6466579324aSTerje Bergstrom 
6476579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job)
6486579324aSTerje Bergstrom {
649404bfb78SMikko Perttunen 	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
6506579324aSTerje Bergstrom 	unsigned int i;
6516579324aSTerje Bergstrom 
6526579324aSTerje Bergstrom 	for (i = 0; i < job->num_unpins; i++) {
653*c6aeaf56SThierry Reding 		struct host1x_bo_mapping *map = job->unpins[i].map;
654*c6aeaf56SThierry Reding 		struct host1x_bo *bo = map->bo;
6556df633d0SThierry Reding 
656*c6aeaf56SThierry Reding 		if (!job->enable_firewall && map->size && host->domain) {
657*c6aeaf56SThierry Reding 			iommu_unmap(host->domain, job->addr_phys[i], map->size);
658*c6aeaf56SThierry Reding 			free_iova(&host->iova, iova_pfn(&host->iova, job->addr_phys[i]));
659404bfb78SMikko Perttunen 		}
660404bfb78SMikko Perttunen 
661*c6aeaf56SThierry Reding 		host1x_bo_unpin(map);
662*c6aeaf56SThierry Reding 		host1x_bo_put(bo);
6636579324aSTerje Bergstrom 	}
6640b8070d1SThierry Reding 
6656579324aSTerje Bergstrom 	job->num_unpins = 0;
6666579324aSTerje Bergstrom 
6676579324aSTerje Bergstrom 	if (job->gather_copy_size)
668b78e70c0SThierry Reding 		dma_free_wc(host->dev, job->gather_copy_size,
669f6e45661SLuis R. Rodriguez 			    job->gather_copy_mapped, job->gather_copy);
6706579324aSTerje Bergstrom }
671fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_unpin);
6726579324aSTerje Bergstrom 
6736579324aSTerje Bergstrom /*
6746579324aSTerje Bergstrom  * Debug routine used to dump job entries
6756579324aSTerje Bergstrom  */
6766579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job)
6776579324aSTerje Bergstrom {
6782aed4f5aSMikko Perttunen 	dev_dbg(dev, "    SYNCPT_ID   %d\n", job->syncpt->id);
6796579324aSTerje Bergstrom 	dev_dbg(dev, "    SYNCPT_VAL  %d\n", job->syncpt_end);
6806579324aSTerje Bergstrom 	dev_dbg(dev, "    FIRST_GET   0x%x\n", job->first_get);
6816579324aSTerje Bergstrom 	dev_dbg(dev, "    TIMEOUT     %d\n", job->timeout);
6826579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_SLOTS   %d\n", job->num_slots);
6836579324aSTerje Bergstrom 	dev_dbg(dev, "    NUM_HANDLES %d\n", job->num_unpins);
6846579324aSTerje Bergstrom }
685