blob: 527a1cddb14fd5d2a23fa96559ed0e33c54bd690 [file] [log] [blame]
Terje Bergstrom65793242013-03-22 16:34:03 +02001/*
2 * Tegra host1x Job
3 *
Arto Merilainenf08ef2d2016-11-08 19:51:32 +02004 * Copyright (c) 2010-2015, NVIDIA Corporation.
Terje Bergstrom65793242013-03-22 16:34:03 +02005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/dma-mapping.h>
20#include <linux/err.h>
Thierry Reding35d747a2013-09-24 16:30:32 +020021#include <linux/host1x.h>
Terje Bergstrom65793242013-03-22 16:34:03 +020022#include <linux/kref.h>
23#include <linux/module.h>
24#include <linux/scatterlist.h>
25#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <trace/events/host1x.h>
28
29#include "channel.h"
30#include "dev.h"
Terje Bergstrom65793242013-03-22 16:34:03 +020031#include "job.h"
32#include "syncpt.h"
33
Dmitry Osipenkoa47ac102017-06-15 02:18:39 +030034#define HOST1X_WAIT_SYNCPT_OFFSET 0x8
35
Terje Bergstrom65793242013-03-22 16:34:03 +020036struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
Thierry Reding24c94e12018-05-05 08:45:47 +020037 u32 num_cmdbufs, u32 num_relocs)
Terje Bergstrom65793242013-03-22 16:34:03 +020038{
39 struct host1x_job *job = NULL;
40 unsigned int num_unpins = num_cmdbufs + num_relocs;
41 u64 total;
42 void *mem;
43
44 /* Check that we're not going to overflow */
45 total = sizeof(struct host1x_job) +
Dan Carpenterf5fda672013-08-23 13:18:25 +030046 (u64)num_relocs * sizeof(struct host1x_reloc) +
47 (u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
Dan Carpenterf5fda672013-08-23 13:18:25 +030048 (u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
49 (u64)num_unpins * sizeof(dma_addr_t) +
50 (u64)num_unpins * sizeof(u32 *);
Terje Bergstrom65793242013-03-22 16:34:03 +020051 if (total > ULONG_MAX)
52 return NULL;
53
54 mem = job = kzalloc(total, GFP_KERNEL);
55 if (!job)
56 return NULL;
57
58 kref_init(&job->ref);
59 job->channel = ch;
60
61 /* Redistribute memory to the structs */
62 mem += sizeof(struct host1x_job);
Thierry Reding06490bb2018-05-16 16:58:44 +020063 job->relocs = num_relocs ? mem : NULL;
Terje Bergstrom65793242013-03-22 16:34:03 +020064 mem += num_relocs * sizeof(struct host1x_reloc);
65 job->unpins = num_unpins ? mem : NULL;
66 mem += num_unpins * sizeof(struct host1x_job_unpin_data);
Terje Bergstrom65793242013-03-22 16:34:03 +020067 job->gathers = num_cmdbufs ? mem : NULL;
68 mem += num_cmdbufs * sizeof(struct host1x_job_gather);
69 job->addr_phys = num_unpins ? mem : NULL;
70
71 job->reloc_addr_phys = job->addr_phys;
72 job->gather_addr_phys = &job->addr_phys[num_relocs];
73
74 return job;
75}
Thierry Redingfae798a2013-11-08 11:41:42 +010076EXPORT_SYMBOL(host1x_job_alloc);
Terje Bergstrom65793242013-03-22 16:34:03 +020077
78struct host1x_job *host1x_job_get(struct host1x_job *job)
79{
80 kref_get(&job->ref);
81 return job;
82}
Thierry Redingfae798a2013-11-08 11:41:42 +010083EXPORT_SYMBOL(host1x_job_get);
Terje Bergstrom65793242013-03-22 16:34:03 +020084
85static void job_free(struct kref *ref)
86{
87 struct host1x_job *job = container_of(ref, struct host1x_job, ref);
88
89 kfree(job);
90}
91
92void host1x_job_put(struct host1x_job *job)
93{
94 kref_put(&job->ref, job_free);
95}
Thierry Redingfae798a2013-11-08 11:41:42 +010096EXPORT_SYMBOL(host1x_job_put);
Terje Bergstrom65793242013-03-22 16:34:03 +020097
98void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
Thierry Reding326bbd72018-05-16 17:01:43 +020099 unsigned int words, unsigned int offset)
Terje Bergstrom65793242013-03-22 16:34:03 +0200100{
Thierry Reding326bbd72018-05-16 17:01:43 +0200101 struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
Terje Bergstrom65793242013-03-22 16:34:03 +0200102
Thierry Reding326bbd72018-05-16 17:01:43 +0200103 gather->words = words;
104 gather->bo = bo;
105 gather->offset = offset;
106
Terje Bergstrom65793242013-03-22 16:34:03 +0200107 job->num_gathers++;
108}
Thierry Redingfae798a2013-11-08 11:41:42 +0100109EXPORT_SYMBOL(host1x_job_add_gather);
Terje Bergstrom65793242013-03-22 16:34:03 +0200110
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200111static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
Terje Bergstrom65793242013-03-22 16:34:03 +0200112{
113 unsigned int i;
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200114 int err;
Terje Bergstrom65793242013-03-22 16:34:03 +0200115
116 job->num_unpins = 0;
117
118 for (i = 0; i < job->num_relocs; i++) {
Thierry Reding06490bb2018-05-16 16:58:44 +0200119 struct host1x_reloc *reloc = &job->relocs[i];
Terje Bergstrom65793242013-03-22 16:34:03 +0200120 struct sg_table *sgt;
121 dma_addr_t phys_addr;
122
Thierry Reding961e3be2014-06-10 10:25:00 +0200123 reloc->target.bo = host1x_bo_get(reloc->target.bo);
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200124 if (!reloc->target.bo) {
125 err = -EINVAL;
Terje Bergstrom65793242013-03-22 16:34:03 +0200126 goto unpin;
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200127 }
Terje Bergstrom65793242013-03-22 16:34:03 +0200128
Thierry Reding961e3be2014-06-10 10:25:00 +0200129 phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
Terje Bergstrom65793242013-03-22 16:34:03 +0200130
131 job->addr_phys[job->num_unpins] = phys_addr;
Thierry Reding961e3be2014-06-10 10:25:00 +0200132 job->unpins[job->num_unpins].bo = reloc->target.bo;
Terje Bergstrom65793242013-03-22 16:34:03 +0200133 job->unpins[job->num_unpins].sgt = sgt;
134 job->num_unpins++;
135 }
136
137 for (i = 0; i < job->num_gathers; i++) {
138 struct host1x_job_gather *g = &job->gathers[i];
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200139 size_t gather_size = 0;
140 struct scatterlist *sg;
Terje Bergstrom65793242013-03-22 16:34:03 +0200141 struct sg_table *sgt;
142 dma_addr_t phys_addr;
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200143 unsigned long shift;
144 struct iova *alloc;
145 unsigned int j;
Terje Bergstrom65793242013-03-22 16:34:03 +0200146
147 g->bo = host1x_bo_get(g->bo);
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200148 if (!g->bo) {
149 err = -EINVAL;
Terje Bergstrom65793242013-03-22 16:34:03 +0200150 goto unpin;
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200151 }
Terje Bergstrom65793242013-03-22 16:34:03 +0200152
153 phys_addr = host1x_bo_pin(g->bo, &sgt);
Terje Bergstrom65793242013-03-22 16:34:03 +0200154
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200155 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
156 for_each_sg(sgt->sgl, sg, sgt->nents, j)
157 gather_size += sg->length;
158 gather_size = iova_align(&host->iova, gather_size);
159
160 shift = iova_shift(&host->iova);
161 alloc = alloc_iova(&host->iova, gather_size >> shift,
162 host->iova_end >> shift, true);
163 if (!alloc) {
164 err = -ENOMEM;
165 goto unpin;
166 }
167
168 err = iommu_map_sg(host->domain,
169 iova_dma_addr(&host->iova, alloc),
170 sgt->sgl, sgt->nents, IOMMU_READ);
171 if (err == 0) {
172 __free_iova(&host->iova, alloc);
173 err = -EINVAL;
174 goto unpin;
175 }
176
177 job->addr_phys[job->num_unpins] =
178 iova_dma_addr(&host->iova, alloc);
179 job->unpins[job->num_unpins].size = gather_size;
180 } else {
181 job->addr_phys[job->num_unpins] = phys_addr;
182 }
183
184 job->gather_addr_phys[i] = job->addr_phys[job->num_unpins];
185
Terje Bergstrom65793242013-03-22 16:34:03 +0200186 job->unpins[job->num_unpins].bo = g->bo;
187 job->unpins[job->num_unpins].sgt = sgt;
188 job->num_unpins++;
189 }
190
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200191 return 0;
Terje Bergstrom65793242013-03-22 16:34:03 +0200192
193unpin:
194 host1x_job_unpin(job);
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200195 return err;
Terje Bergstrom65793242013-03-22 16:34:03 +0200196}
197
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300198static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
Terje Bergstrom65793242013-03-22 16:34:03 +0200199{
Terje Bergstrom65793242013-03-22 16:34:03 +0200200 u32 last_page = ~0;
201 void *cmdbuf_page_addr = NULL;
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300202 struct host1x_bo *cmdbuf = g->bo;
Thierry Redingd4ad3ad2018-03-23 13:31:24 +0100203 unsigned int i;
Terje Bergstrom65793242013-03-22 16:34:03 +0200204
205 /* pin & patch the relocs for one gather */
Arto Merilainen3364cd22013-05-29 13:26:05 +0300206 for (i = 0; i < job->num_relocs; i++) {
Thierry Reding06490bb2018-05-16 16:58:44 +0200207 struct host1x_reloc *reloc = &job->relocs[i];
Terje Bergstrom65793242013-03-22 16:34:03 +0200208 u32 reloc_addr = (job->reloc_addr_phys[i] +
Thierry Reding961e3be2014-06-10 10:25:00 +0200209 reloc->target.offset) >> reloc->shift;
Terje Bergstrom65793242013-03-22 16:34:03 +0200210 u32 *target;
211
212 /* skip all other gathers */
Thierry Reding961e3be2014-06-10 10:25:00 +0200213 if (cmdbuf != reloc->cmdbuf.bo)
Terje Bergstrom65793242013-03-22 16:34:03 +0200214 continue;
Terje Bergstrom65793242013-03-22 16:34:03 +0200215
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300216 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
217 target = (u32 *)job->gather_copy_mapped +
218 reloc->cmdbuf.offset / sizeof(u32) +
219 g->offset / sizeof(u32);
220 goto patch_reloc;
221 }
222
Thierry Reding961e3be2014-06-10 10:25:00 +0200223 if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
Terje Bergstrom65793242013-03-22 16:34:03 +0200224 if (cmdbuf_page_addr)
225 host1x_bo_kunmap(cmdbuf, last_page,
226 cmdbuf_page_addr);
227
228 cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
Thierry Reding961e3be2014-06-10 10:25:00 +0200229 reloc->cmdbuf.offset >> PAGE_SHIFT);
230 last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
Terje Bergstrom65793242013-03-22 16:34:03 +0200231
232 if (unlikely(!cmdbuf_page_addr)) {
233 pr_err("Could not map cmdbuf for relocation\n");
234 return -ENOMEM;
235 }
236 }
237
Thierry Reding961e3be2014-06-10 10:25:00 +0200238 target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300239patch_reloc:
Terje Bergstrom65793242013-03-22 16:34:03 +0200240 *target = reloc_addr;
Terje Bergstrom65793242013-03-22 16:34:03 +0200241 }
242
243 if (cmdbuf_page_addr)
244 host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
245
246 return 0;
247}
248
Arto Merilainen5060d8e2013-05-29 13:26:03 +0300249static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
Thierry Reding37857cd2013-10-10 10:17:45 +0200250 unsigned int offset)
Terje Bergstrom65793242013-03-22 16:34:03 +0200251{
252 offset *= sizeof(u32);
253
Thierry Reding961e3be2014-06-10 10:25:00 +0200254 if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
Arto Merilainen5060d8e2013-05-29 13:26:03 +0300255 return false;
Terje Bergstrom65793242013-03-22 16:34:03 +0200256
Dmitry Osipenko571cbf72017-06-15 02:18:35 +0300257 /* relocation shift value validation isn't implemented yet */
258 if (reloc->shift)
259 return false;
260
Arto Merilainen5060d8e2013-05-29 13:26:03 +0300261 return true;
Terje Bergstrom65793242013-03-22 16:34:03 +0200262}
263
264struct host1x_firewall {
265 struct host1x_job *job;
266 struct device *dev;
267
268 unsigned int num_relocs;
269 struct host1x_reloc *reloc;
270
Thierry Redingd7fbcf42013-10-10 10:21:58 +0200271 struct host1x_bo *cmdbuf;
Terje Bergstrom65793242013-03-22 16:34:03 +0200272 unsigned int offset;
273
274 u32 words;
275 u32 class;
276 u32 reg;
277 u32 mask;
278 u32 count;
279};
280
Thierry Redingd77563f2013-10-10 10:24:04 +0200281static int check_register(struct host1x_firewall *fw, unsigned long offset)
282{
Dmitry Osipenko0f563a42017-06-15 02:18:37 +0300283 if (!fw->job->is_addr_reg)
284 return 0;
285
Thierry Redingd77563f2013-10-10 10:24:04 +0200286 if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
287 if (!fw->num_relocs)
288 return -EINVAL;
289
290 if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
291 return -EINVAL;
292
293 fw->num_relocs--;
294 fw->reloc++;
295 }
296
297 return 0;
298}
299
Dmitry Osipenko0f563a42017-06-15 02:18:37 +0300300static int check_class(struct host1x_firewall *fw, u32 class)
301{
302 if (!fw->job->is_valid_class) {
303 if (fw->class != class)
304 return -EINVAL;
305 } else {
306 if (!fw->job->is_valid_class(fw->class))
307 return -EINVAL;
308 }
309
310 return 0;
311}
312
Terje Bergstrom65793242013-03-22 16:34:03 +0200313static int check_mask(struct host1x_firewall *fw)
314{
315 u32 mask = fw->mask;
316 u32 reg = fw->reg;
Thierry Redingd77563f2013-10-10 10:24:04 +0200317 int ret;
Terje Bergstrom65793242013-03-22 16:34:03 +0200318
319 while (mask) {
320 if (fw->words == 0)
321 return -EINVAL;
322
323 if (mask & 1) {
Thierry Redingd77563f2013-10-10 10:24:04 +0200324 ret = check_register(fw, reg);
325 if (ret < 0)
326 return ret;
327
Terje Bergstrom65793242013-03-22 16:34:03 +0200328 fw->words--;
329 fw->offset++;
330 }
331 mask >>= 1;
332 reg++;
333 }
334
335 return 0;
336}
337
338static int check_incr(struct host1x_firewall *fw)
339{
340 u32 count = fw->count;
341 u32 reg = fw->reg;
Thierry Redingd77563f2013-10-10 10:24:04 +0200342 int ret;
Terje Bergstrom65793242013-03-22 16:34:03 +0200343
Terje Bergstrom64c173d2013-05-29 13:26:02 +0300344 while (count) {
Terje Bergstrom65793242013-03-22 16:34:03 +0200345 if (fw->words == 0)
346 return -EINVAL;
347
Thierry Redingd77563f2013-10-10 10:24:04 +0200348 ret = check_register(fw, reg);
349 if (ret < 0)
350 return ret;
351
Terje Bergstrom65793242013-03-22 16:34:03 +0200352 reg++;
353 fw->words--;
354 fw->offset++;
355 count--;
356 }
357
358 return 0;
359}
360
361static int check_nonincr(struct host1x_firewall *fw)
362{
Terje Bergstrom65793242013-03-22 16:34:03 +0200363 u32 count = fw->count;
Thierry Redingd77563f2013-10-10 10:24:04 +0200364 int ret;
Terje Bergstrom65793242013-03-22 16:34:03 +0200365
366 while (count) {
367 if (fw->words == 0)
368 return -EINVAL;
369
Thierry Redingd77563f2013-10-10 10:24:04 +0200370 ret = check_register(fw, fw->reg);
371 if (ret < 0)
372 return ret;
373
Terje Bergstrom65793242013-03-22 16:34:03 +0200374 fw->words--;
375 fw->offset++;
376 count--;
377 }
378
379 return 0;
380}
381
Terje Bergstromafac0e42013-05-29 13:26:04 +0300382static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
Terje Bergstrom65793242013-03-22 16:34:03 +0200383{
Arto Merilainen3364cd22013-05-29 13:26:05 +0300384 u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
385 (g->offset / sizeof(u32));
Dmitry Osipenko0f563a42017-06-15 02:18:37 +0300386 u32 job_class = fw->class;
Terje Bergstrom65793242013-03-22 16:34:03 +0200387 int err = 0;
Terje Bergstrom65793242013-03-22 16:34:03 +0200388
Terje Bergstromafac0e42013-05-29 13:26:04 +0300389 fw->words = g->words;
Thierry Redingd7fbcf42013-10-10 10:21:58 +0200390 fw->cmdbuf = g->bo;
Terje Bergstromafac0e42013-05-29 13:26:04 +0300391 fw->offset = 0;
Terje Bergstrom65793242013-03-22 16:34:03 +0200392
Terje Bergstromafac0e42013-05-29 13:26:04 +0300393 while (fw->words && !err) {
394 u32 word = cmdbuf_base[fw->offset];
Terje Bergstrom65793242013-03-22 16:34:03 +0200395 u32 opcode = (word & 0xf0000000) >> 28;
396
Terje Bergstromafac0e42013-05-29 13:26:04 +0300397 fw->mask = 0;
398 fw->reg = 0;
399 fw->count = 0;
400 fw->words--;
401 fw->offset++;
Terje Bergstrom65793242013-03-22 16:34:03 +0200402
403 switch (opcode) {
404 case 0:
Terje Bergstromafac0e42013-05-29 13:26:04 +0300405 fw->class = word >> 6 & 0x3ff;
406 fw->mask = word & 0x3f;
407 fw->reg = word >> 16 & 0xfff;
Dmitry Osipenko0f563a42017-06-15 02:18:37 +0300408 err = check_class(fw, job_class);
409 if (!err)
410 err = check_mask(fw);
Terje Bergstrom65793242013-03-22 16:34:03 +0200411 if (err)
412 goto out;
413 break;
414 case 1:
Terje Bergstromafac0e42013-05-29 13:26:04 +0300415 fw->reg = word >> 16 & 0xfff;
416 fw->count = word & 0xffff;
417 err = check_incr(fw);
Terje Bergstrom65793242013-03-22 16:34:03 +0200418 if (err)
419 goto out;
420 break;
421
422 case 2:
Terje Bergstromafac0e42013-05-29 13:26:04 +0300423 fw->reg = word >> 16 & 0xfff;
424 fw->count = word & 0xffff;
425 err = check_nonincr(fw);
Terje Bergstrom65793242013-03-22 16:34:03 +0200426 if (err)
427 goto out;
428 break;
429
430 case 3:
Terje Bergstromafac0e42013-05-29 13:26:04 +0300431 fw->mask = word & 0xffff;
432 fw->reg = word >> 16 & 0xfff;
433 err = check_mask(fw);
Terje Bergstrom65793242013-03-22 16:34:03 +0200434 if (err)
435 goto out;
436 break;
437 case 4:
Terje Bergstrom65793242013-03-22 16:34:03 +0200438 case 14:
439 break;
440 default:
441 err = -EINVAL;
442 break;
443 }
444 }
445
Terje Bergstrom65793242013-03-22 16:34:03 +0200446out:
Terje Bergstrom65793242013-03-22 16:34:03 +0200447 return err;
448}
449
450static inline int copy_gathers(struct host1x_job *job, struct device *dev)
451{
Arto Merilainen3364cd22013-05-29 13:26:05 +0300452 struct host1x_firewall fw;
Terje Bergstrom65793242013-03-22 16:34:03 +0200453 size_t size = 0;
454 size_t offset = 0;
Thierry Redingd4ad3ad2018-03-23 13:31:24 +0100455 unsigned int i;
Terje Bergstrom65793242013-03-22 16:34:03 +0200456
Arto Merilainen3364cd22013-05-29 13:26:05 +0300457 fw.job = job;
458 fw.dev = dev;
Thierry Reding06490bb2018-05-16 16:58:44 +0200459 fw.reloc = job->relocs;
Arto Merilainen3364cd22013-05-29 13:26:05 +0300460 fw.num_relocs = job->num_relocs;
Dmitry Osipenko3833d162017-06-15 02:18:32 +0300461 fw.class = job->class;
Arto Merilainen3364cd22013-05-29 13:26:05 +0300462
Terje Bergstrom65793242013-03-22 16:34:03 +0200463 for (i = 0; i < job->num_gathers; i++) {
464 struct host1x_job_gather *g = &job->gathers[i];
Thierry Reding6df633d2016-06-23 11:33:31 +0200465
Terje Bergstrom65793242013-03-22 16:34:03 +0200466 size += g->words * sizeof(u32);
467 }
468
Dmitry Osipenko43240bb2017-06-15 02:18:43 +0300469 /*
470 * Try a non-blocking allocation from a higher priority pools first,
471 * as awaiting for the allocation here is a major performance hit.
472 */
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800473 job->gather_copy_mapped = dma_alloc_wc(dev, size, &job->gather_copy,
Dmitry Osipenko43240bb2017-06-15 02:18:43 +0300474 GFP_NOWAIT);
475
476 /* the higher priority allocation failed, try the generic-blocking */
477 if (!job->gather_copy_mapped)
478 job->gather_copy_mapped = dma_alloc_wc(dev, size,
479 &job->gather_copy,
480 GFP_KERNEL);
481 if (!job->gather_copy_mapped)
Dan Carpenter745cecc2013-08-23 13:19:11 +0300482 return -ENOMEM;
Terje Bergstrom65793242013-03-22 16:34:03 +0200483
484 job->gather_copy_size = size;
485
486 for (i = 0; i < job->num_gathers; i++) {
487 struct host1x_job_gather *g = &job->gathers[i];
488 void *gather;
489
Arto Merilainen3364cd22013-05-29 13:26:05 +0300490 /* Copy the gather */
Terje Bergstrom65793242013-03-22 16:34:03 +0200491 gather = host1x_bo_mmap(g->bo);
492 memcpy(job->gather_copy_mapped + offset, gather + g->offset,
493 g->words * sizeof(u32));
494 host1x_bo_munmap(g->bo, gather);
495
Arto Merilainen3364cd22013-05-29 13:26:05 +0300496 /* Store the location in the buffer */
Terje Bergstrom65793242013-03-22 16:34:03 +0200497 g->base = job->gather_copy;
498 g->offset = offset;
Arto Merilainen3364cd22013-05-29 13:26:05 +0300499
500 /* Validate the job */
501 if (validate(&fw, g))
502 return -EINVAL;
Terje Bergstrom65793242013-03-22 16:34:03 +0200503
504 offset += g->words * sizeof(u32);
505 }
506
Thierry Reding24c94e12018-05-05 08:45:47 +0200507 /* No relocs should remain at this point */
508 if (fw.num_relocs)
Erik Faye-Lunda9ff9992013-10-04 20:18:33 +0000509 return -EINVAL;
510
Terje Bergstrom65793242013-03-22 16:34:03 +0200511 return 0;
512}
513
514int host1x_job_pin(struct host1x_job *job, struct device *dev)
515{
516 int err;
517 unsigned int i, j;
518 struct host1x *host = dev_get_drvdata(dev->parent);
Terje Bergstrom65793242013-03-22 16:34:03 +0200519
520 /* pin memory */
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200521 err = pin_job(host, job);
522 if (err)
Terje Bergstrom65793242013-03-22 16:34:03 +0200523 goto out;
524
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300525 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
526 err = copy_gathers(job, dev);
527 if (err)
528 goto out;
529 }
530
Terje Bergstrom65793242013-03-22 16:34:03 +0200531 /* patch gathers */
532 for (i = 0; i < job->num_gathers; i++) {
533 struct host1x_job_gather *g = &job->gathers[i];
534
535 /* process each gather mem only once */
536 if (g->handled)
537 continue;
538
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300539 /* copy_gathers() sets gathers base if firewall is enabled */
540 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
541 g->base = job->gather_addr_phys[i];
Terje Bergstrom65793242013-03-22 16:34:03 +0200542
Arto Merilainenf08ef2d2016-11-08 19:51:32 +0200543 for (j = i + 1; j < job->num_gathers; j++) {
544 if (job->gathers[j].bo == g->bo) {
Terje Bergstrom65793242013-03-22 16:34:03 +0200545 job->gathers[j].handled = true;
Arto Merilainenf08ef2d2016-11-08 19:51:32 +0200546 job->gathers[j].base = g->base;
547 }
548 }
Terje Bergstrom65793242013-03-22 16:34:03 +0200549
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300550 err = do_relocs(job, g);
Terje Bergstrom65793242013-03-22 16:34:03 +0200551 if (err)
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300552 break;
Terje Bergstrom65793242013-03-22 16:34:03 +0200553 }
554
Terje Bergstrom65793242013-03-22 16:34:03 +0200555out:
Dmitry Osipenkoe5855aa2017-06-15 02:18:33 +0300556 if (err)
557 host1x_job_unpin(job);
Terje Bergstrom65793242013-03-22 16:34:03 +0200558 wmb();
559
560 return err;
561}
Thierry Redingfae798a2013-11-08 11:41:42 +0100562EXPORT_SYMBOL(host1x_job_pin);
Terje Bergstrom65793242013-03-22 16:34:03 +0200563
564void host1x_job_unpin(struct host1x_job *job)
565{
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200566 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
Terje Bergstrom65793242013-03-22 16:34:03 +0200567 unsigned int i;
568
569 for (i = 0; i < job->num_unpins; i++) {
570 struct host1x_job_unpin_data *unpin = &job->unpins[i];
Thierry Reding6df633d2016-06-23 11:33:31 +0200571
Dmitry Osipenkoec589232018-07-06 21:02:36 +0300572 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
573 unpin->size && host->domain) {
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200574 iommu_unmap(host->domain, job->addr_phys[i],
575 unpin->size);
576 free_iova(&host->iova,
577 iova_pfn(&host->iova, job->addr_phys[i]));
578 }
579
Terje Bergstrom65793242013-03-22 16:34:03 +0200580 host1x_bo_unpin(unpin->bo, unpin->sgt);
581 host1x_bo_put(unpin->bo);
582 }
Thierry Reding0b8070d12016-06-23 11:35:50 +0200583
Terje Bergstrom65793242013-03-22 16:34:03 +0200584 job->num_unpins = 0;
585
586 if (job->gather_copy_size)
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800587 dma_free_wc(job->channel->dev, job->gather_copy_size,
Thierry Reding0b8070d12016-06-23 11:35:50 +0200588 job->gather_copy_mapped, job->gather_copy);
Terje Bergstrom65793242013-03-22 16:34:03 +0200589}
Thierry Redingfae798a2013-11-08 11:41:42 +0100590EXPORT_SYMBOL(host1x_job_unpin);
Terje Bergstrom65793242013-03-22 16:34:03 +0200591
592/*
593 * Debug routine used to dump job entries
594 */
595void host1x_job_dump(struct device *dev, struct host1x_job *job)
596{
597 dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id);
598 dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
599 dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
600 dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
601 dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
602 dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
603}