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
host1x_job_alloc(struct host1x_channel * ch,u32 num_cmdbufs,u32 num_relocs,bool skip_firewall)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
host1x_job_get(struct host1x_job * job)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
job_free(struct kref * ref)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
91*c24973edSMikko Perttunen if (job->fence) {
92*c24973edSMikko Perttunen /*
93*c24973edSMikko Perttunen * remove_callback is atomic w.r.t. fence signaling, so
94*c24973edSMikko Perttunen * after the call returns, we know that the callback is not
95*c24973edSMikko Perttunen * in execution, and the fence can be safely freed.
96*c24973edSMikko Perttunen */
97*c24973edSMikko Perttunen dma_fence_remove_callback(job->fence, &job->fence_cb);
98*c24973edSMikko Perttunen dma_fence_put(job->fence);
99*c24973edSMikko Perttunen }
100c78f837aSMikko Perttunen
1012aed4f5aSMikko Perttunen if (job->syncpt)
1022aed4f5aSMikko Perttunen host1x_syncpt_put(job->syncpt);
1032aed4f5aSMikko Perttunen
1046579324aSTerje Bergstrom kfree(job);
1056579324aSTerje Bergstrom }
1066579324aSTerje Bergstrom
host1x_job_put(struct host1x_job * job)1076579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job)
1086579324aSTerje Bergstrom {
1096579324aSTerje Bergstrom kref_put(&job->ref, job_free);
1106579324aSTerje Bergstrom }
111fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put);
1126579324aSTerje Bergstrom
host1x_job_add_gather(struct host1x_job * job,struct host1x_bo * bo,unsigned int words,unsigned int offset)1136579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
114326bbd79SThierry Reding unsigned int words, unsigned int offset)
1156579324aSTerje Bergstrom {
116e902585fSMikko Perttunen struct host1x_job_gather *gather = &job->cmds[job->num_cmds].gather;
1176579324aSTerje Bergstrom
118326bbd79SThierry Reding gather->words = words;
119326bbd79SThierry Reding gather->bo = bo;
120326bbd79SThierry Reding gather->offset = offset;
121326bbd79SThierry Reding
122e902585fSMikko Perttunen job->num_cmds++;
1236579324aSTerje Bergstrom }
124fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather);
1256579324aSTerje Bergstrom
host1x_job_add_wait(struct host1x_job * job,u32 id,u32 thresh,bool relative,u32 next_class)126e902585fSMikko Perttunen void host1x_job_add_wait(struct host1x_job *job, u32 id, u32 thresh,
127e902585fSMikko Perttunen bool relative, u32 next_class)
128e902585fSMikko Perttunen {
129e902585fSMikko Perttunen struct host1x_job_cmd *cmd = &job->cmds[job->num_cmds];
130e902585fSMikko Perttunen
131e902585fSMikko Perttunen cmd->is_wait = true;
132e902585fSMikko Perttunen cmd->wait.id = id;
133e902585fSMikko Perttunen cmd->wait.threshold = thresh;
134e902585fSMikko Perttunen cmd->wait.next_class = next_class;
135e902585fSMikko Perttunen cmd->wait.relative = relative;
136e902585fSMikko Perttunen
137e902585fSMikko Perttunen job->num_cmds++;
138e902585fSMikko Perttunen }
139e902585fSMikko Perttunen EXPORT_SYMBOL(host1x_job_add_wait);
140e902585fSMikko Perttunen
pin_job(struct host1x * host,struct host1x_job * job)141404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
1426579324aSTerje Bergstrom {
143c6aeaf56SThierry Reding unsigned long mask = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
144af1cbfb9SThierry Reding struct host1x_client *client = job->client;
145af1cbfb9SThierry Reding struct device *dev = client->dev;
146fd323e9eSDmitry Osipenko struct host1x_job_gather *g;
1476579324aSTerje Bergstrom unsigned int i;
148404bfb78SMikko Perttunen int err;
1496579324aSTerje Bergstrom
1506579324aSTerje Bergstrom job->num_unpins = 0;
1516579324aSTerje Bergstrom
1526579324aSTerje Bergstrom for (i = 0; i < job->num_relocs; i++) {
15306490bb9SThierry Reding struct host1x_reloc *reloc = &job->relocs[i];
154c6aeaf56SThierry Reding enum dma_data_direction direction;
155c6aeaf56SThierry Reding struct host1x_bo_mapping *map;
156c6aeaf56SThierry Reding struct host1x_bo *bo;
1576579324aSTerje Bergstrom
158961e3beaSThierry Reding reloc->target.bo = host1x_bo_get(reloc->target.bo);
159404bfb78SMikko Perttunen if (!reloc->target.bo) {
160404bfb78SMikko Perttunen err = -EINVAL;
1616579324aSTerje Bergstrom goto unpin;
162404bfb78SMikko Perttunen }
1636579324aSTerje Bergstrom
164c6aeaf56SThierry Reding bo = reloc->target.bo;
165af1cbfb9SThierry Reding
166af1cbfb9SThierry Reding switch (reloc->flags & mask) {
167af1cbfb9SThierry Reding case HOST1X_RELOC_READ:
168c6aeaf56SThierry Reding direction = DMA_TO_DEVICE;
169af1cbfb9SThierry Reding break;
170af1cbfb9SThierry Reding
171af1cbfb9SThierry Reding case HOST1X_RELOC_WRITE:
172c6aeaf56SThierry Reding direction = DMA_FROM_DEVICE;
173af1cbfb9SThierry Reding break;
174af1cbfb9SThierry Reding
175af1cbfb9SThierry Reding case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
176c6aeaf56SThierry Reding direction = DMA_BIDIRECTIONAL;
177af1cbfb9SThierry Reding break;
178af1cbfb9SThierry Reding
179af1cbfb9SThierry Reding default:
180af1cbfb9SThierry Reding err = -EINVAL;
181af1cbfb9SThierry Reding goto unpin;
182af1cbfb9SThierry Reding }
183af1cbfb9SThierry Reding
1843e9c4584SThierry Reding map = host1x_bo_pin(dev, bo, direction, NULL);
185c6aeaf56SThierry Reding if (IS_ERR(map)) {
186c6aeaf56SThierry Reding err = PTR_ERR(map);
187af1cbfb9SThierry Reding goto unpin;
188af1cbfb9SThierry Reding }
189af1cbfb9SThierry Reding
190c6aeaf56SThierry Reding /*
191c6aeaf56SThierry Reding * host1x clients are generally not able to do scatter-gather themselves, so fail
192c6aeaf56SThierry Reding * if the buffer is discontiguous and we fail to map its SG table to a single
193c6aeaf56SThierry Reding * contiguous chunk of I/O virtual memory.
194c6aeaf56SThierry Reding */
195c6aeaf56SThierry Reding if (map->chunks > 1) {
196c6aeaf56SThierry Reding err = -EINVAL;
197c6aeaf56SThierry Reding goto unpin;
198c6aeaf56SThierry Reding }
199c6aeaf56SThierry Reding
200c6aeaf56SThierry Reding job->addr_phys[job->num_unpins] = map->phys;
201c6aeaf56SThierry Reding job->unpins[job->num_unpins].map = map;
2026579324aSTerje Bergstrom job->num_unpins++;
2036579324aSTerje Bergstrom }
2046579324aSTerje Bergstrom
20526c8de5eSDmitry Osipenko /*
20626c8de5eSDmitry Osipenko * We will copy gathers BO content later, so there is no need to
20726c8de5eSDmitry Osipenko * hold and pin them.
20826c8de5eSDmitry Osipenko */
2090fddaa85SMikko Perttunen if (job->enable_firewall)
21026c8de5eSDmitry Osipenko return 0;
21126c8de5eSDmitry Osipenko
212e902585fSMikko Perttunen for (i = 0; i < job->num_cmds; i++) {
213c6aeaf56SThierry Reding struct host1x_bo_mapping *map;
214404bfb78SMikko Perttunen size_t gather_size = 0;
215404bfb78SMikko Perttunen struct scatterlist *sg;
216404bfb78SMikko Perttunen unsigned long shift;
217404bfb78SMikko Perttunen struct iova *alloc;
218404bfb78SMikko Perttunen unsigned int j;
2196579324aSTerje Bergstrom
220e902585fSMikko Perttunen if (job->cmds[i].is_wait)
221e902585fSMikko Perttunen continue;
222e902585fSMikko Perttunen
223e902585fSMikko Perttunen g = &job->cmds[i].gather;
224e902585fSMikko Perttunen
2256579324aSTerje Bergstrom g->bo = host1x_bo_get(g->bo);
226404bfb78SMikko Perttunen if (!g->bo) {
227404bfb78SMikko Perttunen err = -EINVAL;
2286579324aSTerje Bergstrom goto unpin;
229404bfb78SMikko Perttunen }
2306579324aSTerje Bergstrom
2313e9c4584SThierry Reding map = host1x_bo_pin(host->dev, g->bo, DMA_TO_DEVICE, NULL);
232c6aeaf56SThierry Reding if (IS_ERR(map)) {
233c6aeaf56SThierry Reding err = PTR_ERR(map);
234c6aeaf56SThierry Reding goto unpin;
23580327ce3SThierry Reding }
2366579324aSTerje Bergstrom
23726c8de5eSDmitry Osipenko if (host->domain) {
238c6aeaf56SThierry Reding for_each_sgtable_sg(map->sgt, sg, j)
239404bfb78SMikko Perttunen gather_size += sg->length;
240c6aeaf56SThierry Reding
241404bfb78SMikko Perttunen gather_size = iova_align(&host->iova, gather_size);
242404bfb78SMikko Perttunen
243404bfb78SMikko Perttunen shift = iova_shift(&host->iova);
244404bfb78SMikko Perttunen alloc = alloc_iova(&host->iova, gather_size >> shift,
245404bfb78SMikko Perttunen host->iova_end >> shift, true);
246404bfb78SMikko Perttunen if (!alloc) {
247404bfb78SMikko Perttunen err = -ENOMEM;
248fd323e9eSDmitry Osipenko goto put;
249404bfb78SMikko Perttunen }
250404bfb78SMikko Perttunen
251c6aeaf56SThierry Reding err = iommu_map_sgtable(host->domain, iova_dma_addr(&host->iova, alloc),
252c6aeaf56SThierry Reding map->sgt, IOMMU_READ);
253404bfb78SMikko Perttunen if (err == 0) {
254404bfb78SMikko Perttunen __free_iova(&host->iova, alloc);
255404bfb78SMikko Perttunen err = -EINVAL;
256fd323e9eSDmitry Osipenko goto put;
257404bfb78SMikko Perttunen }
258404bfb78SMikko Perttunen
259c6aeaf56SThierry Reding map->phys = iova_dma_addr(&host->iova, alloc);
260c6aeaf56SThierry Reding map->size = gather_size;
261af1cbfb9SThierry Reding }
262404bfb78SMikko Perttunen
263c6aeaf56SThierry Reding job->addr_phys[job->num_unpins] = map->phys;
264c6aeaf56SThierry Reding job->unpins[job->num_unpins].map = map;
2656579324aSTerje Bergstrom job->num_unpins++;
266c6aeaf56SThierry Reding
267c6aeaf56SThierry Reding job->gather_addr_phys[i] = map->phys;
2686579324aSTerje Bergstrom }
2696579324aSTerje Bergstrom
270404bfb78SMikko Perttunen return 0;
2716579324aSTerje Bergstrom
272fd323e9eSDmitry Osipenko put:
273fd323e9eSDmitry Osipenko host1x_bo_put(g->bo);
2746579324aSTerje Bergstrom unpin:
2756579324aSTerje Bergstrom host1x_job_unpin(job);
276404bfb78SMikko Perttunen return err;
2776579324aSTerje Bergstrom }
2786579324aSTerje Bergstrom
do_relocs(struct host1x_job * job,struct host1x_job_gather * g)27947f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
2806579324aSTerje Bergstrom {
2817a8139c5SDaniel Vetter void *cmdbuf_addr = NULL;
28247f89c10SDmitry Osipenko struct host1x_bo *cmdbuf = g->bo;
283d4ad3ad9SThierry Reding unsigned int i;
2846579324aSTerje Bergstrom
2856579324aSTerje Bergstrom /* pin & patch the relocs for one gather */
2863364cd28SArto Merilainen for (i = 0; i < job->num_relocs; i++) {
28706490bb9SThierry Reding struct host1x_reloc *reloc = &job->relocs[i];
2886579324aSTerje Bergstrom u32 reloc_addr = (job->reloc_addr_phys[i] +
289961e3beaSThierry Reding reloc->target.offset) >> reloc->shift;
2906579324aSTerje Bergstrom u32 *target;
2916579324aSTerje Bergstrom
2926579324aSTerje Bergstrom /* skip all other gathers */
293961e3beaSThierry Reding if (cmdbuf != reloc->cmdbuf.bo)
2946579324aSTerje Bergstrom continue;
2956579324aSTerje Bergstrom
2960fddaa85SMikko Perttunen if (job->enable_firewall) {
29747f89c10SDmitry Osipenko target = (u32 *)job->gather_copy_mapped +
29847f89c10SDmitry Osipenko reloc->cmdbuf.offset / sizeof(u32) +
29947f89c10SDmitry Osipenko g->offset / sizeof(u32);
30047f89c10SDmitry Osipenko goto patch_reloc;
30147f89c10SDmitry Osipenko }
30247f89c10SDmitry Osipenko
3037a8139c5SDaniel Vetter if (!cmdbuf_addr) {
3047a8139c5SDaniel Vetter cmdbuf_addr = host1x_bo_mmap(cmdbuf);
3056579324aSTerje Bergstrom
3067a8139c5SDaniel Vetter if (unlikely(!cmdbuf_addr)) {
3076579324aSTerje Bergstrom pr_err("Could not map cmdbuf for relocation\n");
3086579324aSTerje Bergstrom return -ENOMEM;
3096579324aSTerje Bergstrom }
3106579324aSTerje Bergstrom }
3116579324aSTerje Bergstrom
3127a8139c5SDaniel Vetter target = cmdbuf_addr + reloc->cmdbuf.offset;
31347f89c10SDmitry Osipenko patch_reloc:
3146579324aSTerje Bergstrom *target = reloc_addr;
3156579324aSTerje Bergstrom }
3166579324aSTerje Bergstrom
3177a8139c5SDaniel Vetter if (cmdbuf_addr)
3187a8139c5SDaniel Vetter host1x_bo_munmap(cmdbuf, cmdbuf_addr);
3196579324aSTerje Bergstrom
3206579324aSTerje Bergstrom return 0;
3216579324aSTerje Bergstrom }
3226579324aSTerje Bergstrom
check_reloc(struct host1x_reloc * reloc,struct host1x_bo * cmdbuf,unsigned int offset)3235060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
3246579324aSTerje Bergstrom unsigned int offset)
3256579324aSTerje Bergstrom {
3266579324aSTerje Bergstrom offset *= sizeof(u32);
3276579324aSTerje Bergstrom
328961e3beaSThierry Reding if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
3295060d8ecSArto Merilainen return false;
3306579324aSTerje Bergstrom
331571cbf70SDmitry Osipenko /* relocation shift value validation isn't implemented yet */
332571cbf70SDmitry Osipenko if (reloc->shift)
333571cbf70SDmitry Osipenko return false;
334571cbf70SDmitry Osipenko
3355060d8ecSArto Merilainen return true;
3366579324aSTerje Bergstrom }
3376579324aSTerje Bergstrom
3386579324aSTerje Bergstrom struct host1x_firewall {
3396579324aSTerje Bergstrom struct host1x_job *job;
3406579324aSTerje Bergstrom struct device *dev;
3416579324aSTerje Bergstrom
3426579324aSTerje Bergstrom unsigned int num_relocs;
3436579324aSTerje Bergstrom struct host1x_reloc *reloc;
3446579324aSTerje Bergstrom
345d7fbcf47SThierry Reding struct host1x_bo *cmdbuf;
3466579324aSTerje Bergstrom unsigned int offset;
3476579324aSTerje Bergstrom
3486579324aSTerje Bergstrom u32 words;
3496579324aSTerje Bergstrom u32 class;
3506579324aSTerje Bergstrom u32 reg;
3516579324aSTerje Bergstrom u32 mask;
3526579324aSTerje Bergstrom u32 count;
3536579324aSTerje Bergstrom };
3546579324aSTerje Bergstrom
check_register(struct host1x_firewall * fw,unsigned long offset)355d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset)
356d77563ffSThierry Reding {
3570f563a4bSDmitry Osipenko if (!fw->job->is_addr_reg)
3580f563a4bSDmitry Osipenko return 0;
3590f563a4bSDmitry Osipenko
360d77563ffSThierry Reding if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
361d77563ffSThierry Reding if (!fw->num_relocs)
362d77563ffSThierry Reding return -EINVAL;
363d77563ffSThierry Reding
364d77563ffSThierry Reding if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
365d77563ffSThierry Reding return -EINVAL;
366d77563ffSThierry Reding
367d77563ffSThierry Reding fw->num_relocs--;
368d77563ffSThierry Reding fw->reloc++;
369d77563ffSThierry Reding }
370d77563ffSThierry Reding
371d77563ffSThierry Reding return 0;
372d77563ffSThierry Reding }
373d77563ffSThierry Reding
check_class(struct host1x_firewall * fw,u32 class)3740f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class)
3750f563a4bSDmitry Osipenko {
3760f563a4bSDmitry Osipenko if (!fw->job->is_valid_class) {
3770f563a4bSDmitry Osipenko if (fw->class != class)
3780f563a4bSDmitry Osipenko return -EINVAL;
3790f563a4bSDmitry Osipenko } else {
3800f563a4bSDmitry Osipenko if (!fw->job->is_valid_class(fw->class))
3810f563a4bSDmitry Osipenko return -EINVAL;
3820f563a4bSDmitry Osipenko }
3830f563a4bSDmitry Osipenko
3840f563a4bSDmitry Osipenko return 0;
3850f563a4bSDmitry Osipenko }
3860f563a4bSDmitry Osipenko
check_mask(struct host1x_firewall * fw)3876579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw)
3886579324aSTerje Bergstrom {
3896579324aSTerje Bergstrom u32 mask = fw->mask;
3906579324aSTerje Bergstrom u32 reg = fw->reg;
391d77563ffSThierry Reding int ret;
3926579324aSTerje Bergstrom
3936579324aSTerje Bergstrom while (mask) {
3946579324aSTerje Bergstrom if (fw->words == 0)
3956579324aSTerje Bergstrom return -EINVAL;
3966579324aSTerje Bergstrom
3976579324aSTerje Bergstrom if (mask & 1) {
398d77563ffSThierry Reding ret = check_register(fw, reg);
399d77563ffSThierry Reding if (ret < 0)
400d77563ffSThierry Reding return ret;
401d77563ffSThierry Reding
4026579324aSTerje Bergstrom fw->words--;
4036579324aSTerje Bergstrom fw->offset++;
4046579324aSTerje Bergstrom }
4056579324aSTerje Bergstrom mask >>= 1;
4066579324aSTerje Bergstrom reg++;
4076579324aSTerje Bergstrom }
4086579324aSTerje Bergstrom
4096579324aSTerje Bergstrom return 0;
4106579324aSTerje Bergstrom }
4116579324aSTerje Bergstrom
check_incr(struct host1x_firewall * fw)4126579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw)
4136579324aSTerje Bergstrom {
4146579324aSTerje Bergstrom u32 count = fw->count;
4156579324aSTerje Bergstrom u32 reg = fw->reg;
416d77563ffSThierry Reding int ret;
4176579324aSTerje Bergstrom
41864c173d3STerje Bergstrom while (count) {
4196579324aSTerje Bergstrom if (fw->words == 0)
4206579324aSTerje Bergstrom return -EINVAL;
4216579324aSTerje Bergstrom
422d77563ffSThierry Reding ret = check_register(fw, reg);
423d77563ffSThierry Reding if (ret < 0)
424d77563ffSThierry Reding return ret;
425d77563ffSThierry Reding
4266579324aSTerje Bergstrom reg++;
4276579324aSTerje Bergstrom fw->words--;
4286579324aSTerje Bergstrom fw->offset++;
4296579324aSTerje Bergstrom count--;
4306579324aSTerje Bergstrom }
4316579324aSTerje Bergstrom
4326579324aSTerje Bergstrom return 0;
4336579324aSTerje Bergstrom }
4346579324aSTerje Bergstrom
check_nonincr(struct host1x_firewall * fw)4356579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw)
4366579324aSTerje Bergstrom {
4376579324aSTerje Bergstrom u32 count = fw->count;
438d77563ffSThierry Reding int ret;
4396579324aSTerje Bergstrom
4406579324aSTerje Bergstrom while (count) {
4416579324aSTerje Bergstrom if (fw->words == 0)
4426579324aSTerje Bergstrom return -EINVAL;
4436579324aSTerje Bergstrom
444d77563ffSThierry Reding ret = check_register(fw, fw->reg);
445d77563ffSThierry Reding if (ret < 0)
446d77563ffSThierry Reding return ret;
447d77563ffSThierry Reding
4486579324aSTerje Bergstrom fw->words--;
4496579324aSTerje Bergstrom fw->offset++;
4506579324aSTerje Bergstrom count--;
4516579324aSTerje Bergstrom }
4526579324aSTerje Bergstrom
4536579324aSTerje Bergstrom return 0;
4546579324aSTerje Bergstrom }
4556579324aSTerje Bergstrom
validate(struct host1x_firewall * fw,struct host1x_job_gather * g)456afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
4576579324aSTerje Bergstrom {
4583364cd28SArto Merilainen u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
4593364cd28SArto Merilainen (g->offset / sizeof(u32));
4600f563a4bSDmitry Osipenko u32 job_class = fw->class;
4616579324aSTerje Bergstrom int err = 0;
4626579324aSTerje Bergstrom
463afac0e43STerje Bergstrom fw->words = g->words;
464d7fbcf47SThierry Reding fw->cmdbuf = g->bo;
465afac0e43STerje Bergstrom fw->offset = 0;
4666579324aSTerje Bergstrom
467afac0e43STerje Bergstrom while (fw->words && !err) {
468afac0e43STerje Bergstrom u32 word = cmdbuf_base[fw->offset];
4696579324aSTerje Bergstrom u32 opcode = (word & 0xf0000000) >> 28;
4706579324aSTerje Bergstrom
471afac0e43STerje Bergstrom fw->mask = 0;
472afac0e43STerje Bergstrom fw->reg = 0;
473afac0e43STerje Bergstrom fw->count = 0;
474afac0e43STerje Bergstrom fw->words--;
475afac0e43STerje Bergstrom fw->offset++;
4766579324aSTerje Bergstrom
4776579324aSTerje Bergstrom switch (opcode) {
4786579324aSTerje Bergstrom case 0:
479afac0e43STerje Bergstrom fw->class = word >> 6 & 0x3ff;
480afac0e43STerje Bergstrom fw->mask = word & 0x3f;
481afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff;
4820f563a4bSDmitry Osipenko err = check_class(fw, job_class);
4830f563a4bSDmitry Osipenko if (!err)
484afac0e43STerje Bergstrom err = check_mask(fw);
4856579324aSTerje Bergstrom if (err)
4866579324aSTerje Bergstrom goto out;
4876579324aSTerje Bergstrom break;
4886579324aSTerje Bergstrom case 1:
489afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff;
490afac0e43STerje Bergstrom fw->count = word & 0xffff;
491afac0e43STerje Bergstrom err = check_incr(fw);
4926579324aSTerje Bergstrom if (err)
4936579324aSTerje Bergstrom goto out;
4946579324aSTerje Bergstrom break;
4956579324aSTerje Bergstrom
4966579324aSTerje Bergstrom case 2:
497afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff;
498afac0e43STerje Bergstrom fw->count = word & 0xffff;
499afac0e43STerje Bergstrom err = check_nonincr(fw);
5006579324aSTerje Bergstrom if (err)
5016579324aSTerje Bergstrom goto out;
5026579324aSTerje Bergstrom break;
5036579324aSTerje Bergstrom
5046579324aSTerje Bergstrom case 3:
505afac0e43STerje Bergstrom fw->mask = word & 0xffff;
506afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff;
507afac0e43STerje Bergstrom err = check_mask(fw);
5086579324aSTerje Bergstrom if (err)
5096579324aSTerje Bergstrom goto out;
5106579324aSTerje Bergstrom break;
5116579324aSTerje Bergstrom case 4:
5126579324aSTerje Bergstrom case 14:
5136579324aSTerje Bergstrom break;
5146579324aSTerje Bergstrom default:
5156579324aSTerje Bergstrom err = -EINVAL;
5166579324aSTerje Bergstrom break;
5176579324aSTerje Bergstrom }
5186579324aSTerje Bergstrom }
5196579324aSTerje Bergstrom
5206579324aSTerje Bergstrom out:
5216579324aSTerje Bergstrom return err;
5226579324aSTerje Bergstrom }
5236579324aSTerje Bergstrom
copy_gathers(struct device * host,struct host1x_job * job,struct device * dev)524b78e70c0SThierry Reding static inline int copy_gathers(struct device *host, struct host1x_job *job,
525b78e70c0SThierry Reding struct device *dev)
5266579324aSTerje Bergstrom {
5273364cd28SArto Merilainen struct host1x_firewall fw;
5286579324aSTerje Bergstrom size_t size = 0;
5296579324aSTerje Bergstrom size_t offset = 0;
530d4ad3ad9SThierry Reding unsigned int i;
5316579324aSTerje Bergstrom
5323364cd28SArto Merilainen fw.job = job;
5333364cd28SArto Merilainen fw.dev = dev;
53406490bb9SThierry Reding fw.reloc = job->relocs;
5353364cd28SArto Merilainen fw.num_relocs = job->num_relocs;
5363833d16fSDmitry Osipenko fw.class = job->class;
5373364cd28SArto Merilainen
538e902585fSMikko Perttunen for (i = 0; i < job->num_cmds; i++) {
539e902585fSMikko Perttunen struct host1x_job_gather *g;
540e902585fSMikko Perttunen
541e902585fSMikko Perttunen if (job->cmds[i].is_wait)
542e902585fSMikko Perttunen continue;
543e902585fSMikko Perttunen
544e902585fSMikko Perttunen g = &job->cmds[i].gather;
5456df633d0SThierry Reding
5466579324aSTerje Bergstrom size += g->words * sizeof(u32);
5476579324aSTerje Bergstrom }
5486579324aSTerje Bergstrom
54943240bbdSDmitry Osipenko /*
55043240bbdSDmitry Osipenko * Try a non-blocking allocation from a higher priority pools first,
55143240bbdSDmitry Osipenko * as awaiting for the allocation here is a major performance hit.
55243240bbdSDmitry Osipenko */
553b78e70c0SThierry Reding job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
55443240bbdSDmitry Osipenko GFP_NOWAIT);
55543240bbdSDmitry Osipenko
55643240bbdSDmitry Osipenko /* the higher priority allocation failed, try the generic-blocking */
55743240bbdSDmitry Osipenko if (!job->gather_copy_mapped)
558b78e70c0SThierry Reding job->gather_copy_mapped = dma_alloc_wc(host, size,
55943240bbdSDmitry Osipenko &job->gather_copy,
5606579324aSTerje Bergstrom GFP_KERNEL);
56143240bbdSDmitry Osipenko if (!job->gather_copy_mapped)
562745cecc0SDan Carpenter return -ENOMEM;
5636579324aSTerje Bergstrom
5646579324aSTerje Bergstrom job->gather_copy_size = size;
5656579324aSTerje Bergstrom
566e902585fSMikko Perttunen for (i = 0; i < job->num_cmds; i++) {
567e902585fSMikko Perttunen struct host1x_job_gather *g;
5686579324aSTerje Bergstrom void *gather;
5696579324aSTerje Bergstrom
570e902585fSMikko Perttunen if (job->cmds[i].is_wait)
571e902585fSMikko Perttunen continue;
572e902585fSMikko Perttunen g = &job->cmds[i].gather;
573e902585fSMikko Perttunen
5743364cd28SArto Merilainen /* Copy the gather */
5756579324aSTerje Bergstrom gather = host1x_bo_mmap(g->bo);
5766579324aSTerje Bergstrom memcpy(job->gather_copy_mapped + offset, gather + g->offset,
5776579324aSTerje Bergstrom g->words * sizeof(u32));
5786579324aSTerje Bergstrom host1x_bo_munmap(g->bo, gather);
5796579324aSTerje Bergstrom
5803364cd28SArto Merilainen /* Store the location in the buffer */
5816579324aSTerje Bergstrom g->base = job->gather_copy;
5826579324aSTerje Bergstrom g->offset = offset;
5833364cd28SArto Merilainen
5843364cd28SArto Merilainen /* Validate the job */
5853364cd28SArto Merilainen if (validate(&fw, g))
5863364cd28SArto Merilainen return -EINVAL;
5876579324aSTerje Bergstrom
5886579324aSTerje Bergstrom offset += g->words * sizeof(u32);
5896579324aSTerje Bergstrom }
5906579324aSTerje Bergstrom
59124c94e16SThierry Reding /* No relocs should remain at this point */
59224c94e16SThierry Reding if (fw.num_relocs)
593a9ff9995SErik Faye-Lund return -EINVAL;
594a9ff9995SErik Faye-Lund
5956579324aSTerje Bergstrom return 0;
5966579324aSTerje Bergstrom }
5976579324aSTerje Bergstrom
host1x_job_pin(struct host1x_job * job,struct device * dev)5986579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev)
5996579324aSTerje Bergstrom {
6006579324aSTerje Bergstrom int err;
6016579324aSTerje Bergstrom unsigned int i, j;
6026579324aSTerje Bergstrom struct host1x *host = dev_get_drvdata(dev->parent);
6036579324aSTerje Bergstrom
6046579324aSTerje Bergstrom /* pin memory */
605404bfb78SMikko Perttunen err = pin_job(host, job);
606404bfb78SMikko Perttunen if (err)
6076579324aSTerje Bergstrom goto out;
6086579324aSTerje Bergstrom
6090fddaa85SMikko Perttunen if (job->enable_firewall) {
610b78e70c0SThierry Reding err = copy_gathers(host->dev, job, dev);
61147f89c10SDmitry Osipenko if (err)
61247f89c10SDmitry Osipenko goto out;
61347f89c10SDmitry Osipenko }
61447f89c10SDmitry Osipenko
6156579324aSTerje Bergstrom /* patch gathers */
616e902585fSMikko Perttunen for (i = 0; i < job->num_cmds; i++) {
617e902585fSMikko Perttunen struct host1x_job_gather *g;
618e902585fSMikko Perttunen
619e902585fSMikko Perttunen if (job->cmds[i].is_wait)
620e902585fSMikko Perttunen continue;
621e902585fSMikko Perttunen g = &job->cmds[i].gather;
6226579324aSTerje Bergstrom
6236579324aSTerje Bergstrom /* process each gather mem only once */
6246579324aSTerje Bergstrom if (g->handled)
6256579324aSTerje Bergstrom continue;
6266579324aSTerje Bergstrom
62747f89c10SDmitry Osipenko /* copy_gathers() sets gathers base if firewall is enabled */
6280fddaa85SMikko Perttunen if (!job->enable_firewall)
6296579324aSTerje Bergstrom g->base = job->gather_addr_phys[i];
6306579324aSTerje Bergstrom
631e902585fSMikko Perttunen for (j = i + 1; j < job->num_cmds; j++) {
632e902585fSMikko Perttunen if (!job->cmds[j].is_wait &&
633e902585fSMikko Perttunen job->cmds[j].gather.bo == g->bo) {
634e902585fSMikko Perttunen job->cmds[j].gather.handled = true;
635e902585fSMikko Perttunen job->cmds[j].gather.base = g->base;
636f08ef2d1SArto Merilainen }
637f08ef2d1SArto Merilainen }
6386579324aSTerje Bergstrom
63947f89c10SDmitry Osipenko err = do_relocs(job, g);
6403364cd28SArto Merilainen if (err)
64147f89c10SDmitry Osipenko break;
6426579324aSTerje Bergstrom }
6436579324aSTerje Bergstrom
6446579324aSTerje Bergstrom out:
645e5855aa3SDmitry Osipenko if (err)
646e5855aa3SDmitry Osipenko host1x_job_unpin(job);
6476579324aSTerje Bergstrom wmb();
6486579324aSTerje Bergstrom
6496579324aSTerje Bergstrom return err;
6506579324aSTerje Bergstrom }
651fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin);
6526579324aSTerje Bergstrom
host1x_job_unpin(struct host1x_job * job)6536579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job)
6546579324aSTerje Bergstrom {
655404bfb78SMikko Perttunen struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
6566579324aSTerje Bergstrom unsigned int i;
6576579324aSTerje Bergstrom
6586579324aSTerje Bergstrom for (i = 0; i < job->num_unpins; i++) {
659c6aeaf56SThierry Reding struct host1x_bo_mapping *map = job->unpins[i].map;
660c6aeaf56SThierry Reding struct host1x_bo *bo = map->bo;
6616df633d0SThierry Reding
662c6aeaf56SThierry Reding if (!job->enable_firewall && map->size && host->domain) {
663c6aeaf56SThierry Reding iommu_unmap(host->domain, job->addr_phys[i], map->size);
664c6aeaf56SThierry Reding free_iova(&host->iova, iova_pfn(&host->iova, job->addr_phys[i]));
665404bfb78SMikko Perttunen }
666404bfb78SMikko Perttunen
667c6aeaf56SThierry Reding host1x_bo_unpin(map);
668c6aeaf56SThierry Reding host1x_bo_put(bo);
6696579324aSTerje Bergstrom }
6700b8070d1SThierry Reding
6716579324aSTerje Bergstrom job->num_unpins = 0;
6726579324aSTerje Bergstrom
6736579324aSTerje Bergstrom if (job->gather_copy_size)
674b78e70c0SThierry Reding dma_free_wc(host->dev, job->gather_copy_size,
675f6e45661SLuis R. Rodriguez job->gather_copy_mapped, job->gather_copy);
6766579324aSTerje Bergstrom }
677fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_unpin);
6786579324aSTerje Bergstrom
6796579324aSTerje Bergstrom /*
6806579324aSTerje Bergstrom * Debug routine used to dump job entries
6816579324aSTerje Bergstrom */
host1x_job_dump(struct device * dev,struct host1x_job * job)6826579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job)
6836579324aSTerje Bergstrom {
6842aed4f5aSMikko Perttunen dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt->id);
6856579324aSTerje Bergstrom dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
6866579324aSTerje Bergstrom dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
6876579324aSTerje Bergstrom dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
6886579324aSTerje Bergstrom dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
6896579324aSTerje Bergstrom dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
6906579324aSTerje Bergstrom }
691