blob: 3cbfc6e37668372c76f86ce31be0b9836a566006 [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);
63 job->relocarray = num_relocs ? mem : NULL;
64 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,
99 u32 words, u32 offset)
100{
101 struct host1x_job_gather *cur_gather = &job->gathers[job->num_gathers];
102
103 cur_gather->words = words;
104 cur_gather->bo = bo;
105 cur_gather->offset = offset;
106 job->num_gathers++;
107}
Thierry Redingfae798a2013-11-08 11:41:42 +0100108EXPORT_SYMBOL(host1x_job_add_gather);
Terje Bergstrom65793242013-03-22 16:34:03 +0200109
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200110static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
Terje Bergstrom65793242013-03-22 16:34:03 +0200111{
112 unsigned int i;
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200113 int err;
Terje Bergstrom65793242013-03-22 16:34:03 +0200114
115 job->num_unpins = 0;
116
117 for (i = 0; i < job->num_relocs; i++) {
118 struct host1x_reloc *reloc = &job->relocarray[i];
119 struct sg_table *sgt;
120 dma_addr_t phys_addr;
121
Thierry Reding961e3be2014-06-10 10:25:00 +0200122 reloc->target.bo = host1x_bo_get(reloc->target.bo);
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200123 if (!reloc->target.bo) {
124 err = -EINVAL;
Terje Bergstrom65793242013-03-22 16:34:03 +0200125 goto unpin;
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200126 }
Terje Bergstrom65793242013-03-22 16:34:03 +0200127
Thierry Reding961e3be2014-06-10 10:25:00 +0200128 phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
Terje Bergstrom65793242013-03-22 16:34:03 +0200129
130 job->addr_phys[job->num_unpins] = phys_addr;
Thierry Reding961e3be2014-06-10 10:25:00 +0200131 job->unpins[job->num_unpins].bo = reloc->target.bo;
Terje Bergstrom65793242013-03-22 16:34:03 +0200132 job->unpins[job->num_unpins].sgt = sgt;
133 job->num_unpins++;
134 }
135
136 for (i = 0; i < job->num_gathers; i++) {
137 struct host1x_job_gather *g = &job->gathers[i];
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200138 size_t gather_size = 0;
139 struct scatterlist *sg;
Terje Bergstrom65793242013-03-22 16:34:03 +0200140 struct sg_table *sgt;
141 dma_addr_t phys_addr;
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200142 unsigned long shift;
143 struct iova *alloc;
144 unsigned int j;
Terje Bergstrom65793242013-03-22 16:34:03 +0200145
146 g->bo = host1x_bo_get(g->bo);
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200147 if (!g->bo) {
148 err = -EINVAL;
Terje Bergstrom65793242013-03-22 16:34:03 +0200149 goto unpin;
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200150 }
Terje Bergstrom65793242013-03-22 16:34:03 +0200151
152 phys_addr = host1x_bo_pin(g->bo, &sgt);
Terje Bergstrom65793242013-03-22 16:34:03 +0200153
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200154 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
155 for_each_sg(sgt->sgl, sg, sgt->nents, j)
156 gather_size += sg->length;
157 gather_size = iova_align(&host->iova, gather_size);
158
159 shift = iova_shift(&host->iova);
160 alloc = alloc_iova(&host->iova, gather_size >> shift,
161 host->iova_end >> shift, true);
162 if (!alloc) {
163 err = -ENOMEM;
164 goto unpin;
165 }
166
167 err = iommu_map_sg(host->domain,
168 iova_dma_addr(&host->iova, alloc),
169 sgt->sgl, sgt->nents, IOMMU_READ);
170 if (err == 0) {
171 __free_iova(&host->iova, alloc);
172 err = -EINVAL;
173 goto unpin;
174 }
175
176 job->addr_phys[job->num_unpins] =
177 iova_dma_addr(&host->iova, alloc);
178 job->unpins[job->num_unpins].size = gather_size;
179 } else {
180 job->addr_phys[job->num_unpins] = phys_addr;
181 }
182
183 job->gather_addr_phys[i] = job->addr_phys[job->num_unpins];
184
Terje Bergstrom65793242013-03-22 16:34:03 +0200185 job->unpins[job->num_unpins].bo = g->bo;
186 job->unpins[job->num_unpins].sgt = sgt;
187 job->num_unpins++;
188 }
189
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200190 return 0;
Terje Bergstrom65793242013-03-22 16:34:03 +0200191
192unpin:
193 host1x_job_unpin(job);
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200194 return err;
Terje Bergstrom65793242013-03-22 16:34:03 +0200195}
196
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300197static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
Terje Bergstrom65793242013-03-22 16:34:03 +0200198{
199 int i = 0;
200 u32 last_page = ~0;
201 void *cmdbuf_page_addr = NULL;
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300202 struct host1x_bo *cmdbuf = g->bo;
Terje Bergstrom65793242013-03-22 16:34:03 +0200203
204 /* pin & patch the relocs for one gather */
Arto Merilainen3364cd22013-05-29 13:26:05 +0300205 for (i = 0; i < job->num_relocs; i++) {
Terje Bergstrom65793242013-03-22 16:34:03 +0200206 struct host1x_reloc *reloc = &job->relocarray[i];
207 u32 reloc_addr = (job->reloc_addr_phys[i] +
Thierry Reding961e3be2014-06-10 10:25:00 +0200208 reloc->target.offset) >> reloc->shift;
Terje Bergstrom65793242013-03-22 16:34:03 +0200209 u32 *target;
210
211 /* skip all other gathers */
Thierry Reding961e3be2014-06-10 10:25:00 +0200212 if (cmdbuf != reloc->cmdbuf.bo)
Terje Bergstrom65793242013-03-22 16:34:03 +0200213 continue;
Terje Bergstrom65793242013-03-22 16:34:03 +0200214
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300215 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
216 target = (u32 *)job->gather_copy_mapped +
217 reloc->cmdbuf.offset / sizeof(u32) +
218 g->offset / sizeof(u32);
219 goto patch_reloc;
220 }
221
Thierry Reding961e3be2014-06-10 10:25:00 +0200222 if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
Terje Bergstrom65793242013-03-22 16:34:03 +0200223 if (cmdbuf_page_addr)
224 host1x_bo_kunmap(cmdbuf, last_page,
225 cmdbuf_page_addr);
226
227 cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
Thierry Reding961e3be2014-06-10 10:25:00 +0200228 reloc->cmdbuf.offset >> PAGE_SHIFT);
229 last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
Terje Bergstrom65793242013-03-22 16:34:03 +0200230
231 if (unlikely(!cmdbuf_page_addr)) {
232 pr_err("Could not map cmdbuf for relocation\n");
233 return -ENOMEM;
234 }
235 }
236
Thierry Reding961e3be2014-06-10 10:25:00 +0200237 target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300238patch_reloc:
Terje Bergstrom65793242013-03-22 16:34:03 +0200239 *target = reloc_addr;
Terje Bergstrom65793242013-03-22 16:34:03 +0200240 }
241
242 if (cmdbuf_page_addr)
243 host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
244
245 return 0;
246}
247
Arto Merilainen5060d8e2013-05-29 13:26:03 +0300248static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
Thierry Reding37857cd2013-10-10 10:17:45 +0200249 unsigned int offset)
Terje Bergstrom65793242013-03-22 16:34:03 +0200250{
251 offset *= sizeof(u32);
252
Thierry Reding961e3be2014-06-10 10:25:00 +0200253 if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
Arto Merilainen5060d8e2013-05-29 13:26:03 +0300254 return false;
Terje Bergstrom65793242013-03-22 16:34:03 +0200255
Dmitry Osipenko571cbf72017-06-15 02:18:35 +0300256 /* relocation shift value validation isn't implemented yet */
257 if (reloc->shift)
258 return false;
259
Arto Merilainen5060d8e2013-05-29 13:26:03 +0300260 return true;
Terje Bergstrom65793242013-03-22 16:34:03 +0200261}
262
263struct host1x_firewall {
264 struct host1x_job *job;
265 struct device *dev;
266
267 unsigned int num_relocs;
268 struct host1x_reloc *reloc;
269
Thierry Redingd7fbcf42013-10-10 10:21:58 +0200270 struct host1x_bo *cmdbuf;
Terje Bergstrom65793242013-03-22 16:34:03 +0200271 unsigned int offset;
272
273 u32 words;
274 u32 class;
275 u32 reg;
276 u32 mask;
277 u32 count;
278};
279
Thierry Redingd77563f2013-10-10 10:24:04 +0200280static int check_register(struct host1x_firewall *fw, unsigned long offset)
281{
Dmitry Osipenko0f563a42017-06-15 02:18:37 +0300282 if (!fw->job->is_addr_reg)
283 return 0;
284
Thierry Redingd77563f2013-10-10 10:24:04 +0200285 if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
286 if (!fw->num_relocs)
287 return -EINVAL;
288
289 if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
290 return -EINVAL;
291
292 fw->num_relocs--;
293 fw->reloc++;
294 }
295
296 return 0;
297}
298
Dmitry Osipenko0f563a42017-06-15 02:18:37 +0300299static int check_class(struct host1x_firewall *fw, u32 class)
300{
301 if (!fw->job->is_valid_class) {
302 if (fw->class != class)
303 return -EINVAL;
304 } else {
305 if (!fw->job->is_valid_class(fw->class))
306 return -EINVAL;
307 }
308
309 return 0;
310}
311
Terje Bergstrom65793242013-03-22 16:34:03 +0200312static int check_mask(struct host1x_firewall *fw)
313{
314 u32 mask = fw->mask;
315 u32 reg = fw->reg;
Thierry Redingd77563f2013-10-10 10:24:04 +0200316 int ret;
Terje Bergstrom65793242013-03-22 16:34:03 +0200317
318 while (mask) {
319 if (fw->words == 0)
320 return -EINVAL;
321
322 if (mask & 1) {
Thierry Redingd77563f2013-10-10 10:24:04 +0200323 ret = check_register(fw, reg);
324 if (ret < 0)
325 return ret;
326
Terje Bergstrom65793242013-03-22 16:34:03 +0200327 fw->words--;
328 fw->offset++;
329 }
330 mask >>= 1;
331 reg++;
332 }
333
334 return 0;
335}
336
337static int check_incr(struct host1x_firewall *fw)
338{
339 u32 count = fw->count;
340 u32 reg = fw->reg;
Thierry Redingd77563f2013-10-10 10:24:04 +0200341 int ret;
Terje Bergstrom65793242013-03-22 16:34:03 +0200342
Terje Bergstrom64c173d2013-05-29 13:26:02 +0300343 while (count) {
Terje Bergstrom65793242013-03-22 16:34:03 +0200344 if (fw->words == 0)
345 return -EINVAL;
346
Thierry Redingd77563f2013-10-10 10:24:04 +0200347 ret = check_register(fw, reg);
348 if (ret < 0)
349 return ret;
350
Terje Bergstrom65793242013-03-22 16:34:03 +0200351 reg++;
352 fw->words--;
353 fw->offset++;
354 count--;
355 }
356
357 return 0;
358}
359
360static int check_nonincr(struct host1x_firewall *fw)
361{
Terje Bergstrom65793242013-03-22 16:34:03 +0200362 u32 count = fw->count;
Thierry Redingd77563f2013-10-10 10:24:04 +0200363 int ret;
Terje Bergstrom65793242013-03-22 16:34:03 +0200364
365 while (count) {
366 if (fw->words == 0)
367 return -EINVAL;
368
Thierry Redingd77563f2013-10-10 10:24:04 +0200369 ret = check_register(fw, fw->reg);
370 if (ret < 0)
371 return ret;
372
Terje Bergstrom65793242013-03-22 16:34:03 +0200373 fw->words--;
374 fw->offset++;
375 count--;
376 }
377
378 return 0;
379}
380
Terje Bergstromafac0e42013-05-29 13:26:04 +0300381static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
Terje Bergstrom65793242013-03-22 16:34:03 +0200382{
Arto Merilainen3364cd22013-05-29 13:26:05 +0300383 u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
384 (g->offset / sizeof(u32));
Dmitry Osipenko0f563a42017-06-15 02:18:37 +0300385 u32 job_class = fw->class;
Terje Bergstrom65793242013-03-22 16:34:03 +0200386 int err = 0;
Terje Bergstrom65793242013-03-22 16:34:03 +0200387
Terje Bergstromafac0e42013-05-29 13:26:04 +0300388 fw->words = g->words;
Thierry Redingd7fbcf42013-10-10 10:21:58 +0200389 fw->cmdbuf = g->bo;
Terje Bergstromafac0e42013-05-29 13:26:04 +0300390 fw->offset = 0;
Terje Bergstrom65793242013-03-22 16:34:03 +0200391
Terje Bergstromafac0e42013-05-29 13:26:04 +0300392 while (fw->words && !err) {
393 u32 word = cmdbuf_base[fw->offset];
Terje Bergstrom65793242013-03-22 16:34:03 +0200394 u32 opcode = (word & 0xf0000000) >> 28;
395
Terje Bergstromafac0e42013-05-29 13:26:04 +0300396 fw->mask = 0;
397 fw->reg = 0;
398 fw->count = 0;
399 fw->words--;
400 fw->offset++;
Terje Bergstrom65793242013-03-22 16:34:03 +0200401
402 switch (opcode) {
403 case 0:
Terje Bergstromafac0e42013-05-29 13:26:04 +0300404 fw->class = word >> 6 & 0x3ff;
405 fw->mask = word & 0x3f;
406 fw->reg = word >> 16 & 0xfff;
Dmitry Osipenko0f563a42017-06-15 02:18:37 +0300407 err = check_class(fw, job_class);
408 if (!err)
409 err = check_mask(fw);
Terje Bergstrom65793242013-03-22 16:34:03 +0200410 if (err)
411 goto out;
412 break;
413 case 1:
Terje Bergstromafac0e42013-05-29 13:26:04 +0300414 fw->reg = word >> 16 & 0xfff;
415 fw->count = word & 0xffff;
416 err = check_incr(fw);
Terje Bergstrom65793242013-03-22 16:34:03 +0200417 if (err)
418 goto out;
419 break;
420
421 case 2:
Terje Bergstromafac0e42013-05-29 13:26:04 +0300422 fw->reg = word >> 16 & 0xfff;
423 fw->count = word & 0xffff;
424 err = check_nonincr(fw);
Terje Bergstrom65793242013-03-22 16:34:03 +0200425 if (err)
426 goto out;
427 break;
428
429 case 3:
Terje Bergstromafac0e42013-05-29 13:26:04 +0300430 fw->mask = word & 0xffff;
431 fw->reg = word >> 16 & 0xfff;
432 err = check_mask(fw);
Terje Bergstrom65793242013-03-22 16:34:03 +0200433 if (err)
434 goto out;
435 break;
436 case 4:
Terje Bergstrom65793242013-03-22 16:34:03 +0200437 case 14:
438 break;
439 default:
440 err = -EINVAL;
441 break;
442 }
443 }
444
Terje Bergstrom65793242013-03-22 16:34:03 +0200445out:
Terje Bergstrom65793242013-03-22 16:34:03 +0200446 return err;
447}
448
449static inline int copy_gathers(struct host1x_job *job, struct device *dev)
450{
Arto Merilainen3364cd22013-05-29 13:26:05 +0300451 struct host1x_firewall fw;
Terje Bergstrom65793242013-03-22 16:34:03 +0200452 size_t size = 0;
453 size_t offset = 0;
454 int i;
455
Arto Merilainen3364cd22013-05-29 13:26:05 +0300456 fw.job = job;
457 fw.dev = dev;
458 fw.reloc = job->relocarray;
459 fw.num_relocs = job->num_relocs;
Dmitry Osipenko3833d162017-06-15 02:18:32 +0300460 fw.class = job->class;
Arto Merilainen3364cd22013-05-29 13:26:05 +0300461
Terje Bergstrom65793242013-03-22 16:34:03 +0200462 for (i = 0; i < job->num_gathers; i++) {
463 struct host1x_job_gather *g = &job->gathers[i];
Thierry Reding6df633d2016-06-23 11:33:31 +0200464
Terje Bergstrom65793242013-03-22 16:34:03 +0200465 size += g->words * sizeof(u32);
466 }
467
Dmitry Osipenko43240bb2017-06-15 02:18:43 +0300468 /*
469 * Try a non-blocking allocation from a higher priority pools first,
470 * as awaiting for the allocation here is a major performance hit.
471 */
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800472 job->gather_copy_mapped = dma_alloc_wc(dev, size, &job->gather_copy,
Dmitry Osipenko43240bb2017-06-15 02:18:43 +0300473 GFP_NOWAIT);
474
475 /* the higher priority allocation failed, try the generic-blocking */
476 if (!job->gather_copy_mapped)
477 job->gather_copy_mapped = dma_alloc_wc(dev, size,
478 &job->gather_copy,
479 GFP_KERNEL);
480 if (!job->gather_copy_mapped)
Dan Carpenter745cecc2013-08-23 13:19:11 +0300481 return -ENOMEM;
Terje Bergstrom65793242013-03-22 16:34:03 +0200482
483 job->gather_copy_size = size;
484
485 for (i = 0; i < job->num_gathers; i++) {
486 struct host1x_job_gather *g = &job->gathers[i];
487 void *gather;
488
Arto Merilainen3364cd22013-05-29 13:26:05 +0300489 /* Copy the gather */
Terje Bergstrom65793242013-03-22 16:34:03 +0200490 gather = host1x_bo_mmap(g->bo);
491 memcpy(job->gather_copy_mapped + offset, gather + g->offset,
492 g->words * sizeof(u32));
493 host1x_bo_munmap(g->bo, gather);
494
Arto Merilainen3364cd22013-05-29 13:26:05 +0300495 /* Store the location in the buffer */
Terje Bergstrom65793242013-03-22 16:34:03 +0200496 g->base = job->gather_copy;
497 g->offset = offset;
Arto Merilainen3364cd22013-05-29 13:26:05 +0300498
499 /* Validate the job */
500 if (validate(&fw, g))
501 return -EINVAL;
Terje Bergstrom65793242013-03-22 16:34:03 +0200502
503 offset += g->words * sizeof(u32);
504 }
505
Thierry Reding24c94e12018-05-05 08:45:47 +0200506 /* No relocs should remain at this point */
507 if (fw.num_relocs)
Erik Faye-Lunda9ff9992013-10-04 20:18:33 +0000508 return -EINVAL;
509
Terje Bergstrom65793242013-03-22 16:34:03 +0200510 return 0;
511}
512
513int host1x_job_pin(struct host1x_job *job, struct device *dev)
514{
515 int err;
516 unsigned int i, j;
517 struct host1x *host = dev_get_drvdata(dev->parent);
Terje Bergstrom65793242013-03-22 16:34:03 +0200518
519 /* pin memory */
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200520 err = pin_job(host, job);
521 if (err)
Terje Bergstrom65793242013-03-22 16:34:03 +0200522 goto out;
523
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300524 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
525 err = copy_gathers(job, dev);
526 if (err)
527 goto out;
528 }
529
Terje Bergstrom65793242013-03-22 16:34:03 +0200530 /* patch gathers */
531 for (i = 0; i < job->num_gathers; i++) {
532 struct host1x_job_gather *g = &job->gathers[i];
533
534 /* process each gather mem only once */
535 if (g->handled)
536 continue;
537
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300538 /* copy_gathers() sets gathers base if firewall is enabled */
539 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
540 g->base = job->gather_addr_phys[i];
Terje Bergstrom65793242013-03-22 16:34:03 +0200541
Arto Merilainenf08ef2d2016-11-08 19:51:32 +0200542 for (j = i + 1; j < job->num_gathers; j++) {
543 if (job->gathers[j].bo == g->bo) {
Terje Bergstrom65793242013-03-22 16:34:03 +0200544 job->gathers[j].handled = true;
Arto Merilainenf08ef2d2016-11-08 19:51:32 +0200545 job->gathers[j].base = g->base;
546 }
547 }
Terje Bergstrom65793242013-03-22 16:34:03 +0200548
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300549 err = do_relocs(job, g);
Terje Bergstrom65793242013-03-22 16:34:03 +0200550 if (err)
Dmitry Osipenko47f89c12017-06-15 02:18:34 +0300551 break;
Terje Bergstrom65793242013-03-22 16:34:03 +0200552 }
553
Terje Bergstrom65793242013-03-22 16:34:03 +0200554out:
Dmitry Osipenkoe5855aa2017-06-15 02:18:33 +0300555 if (err)
556 host1x_job_unpin(job);
Terje Bergstrom65793242013-03-22 16:34:03 +0200557 wmb();
558
559 return err;
560}
Thierry Redingfae798a2013-11-08 11:41:42 +0100561EXPORT_SYMBOL(host1x_job_pin);
Terje Bergstrom65793242013-03-22 16:34:03 +0200562
563void host1x_job_unpin(struct host1x_job *job)
564{
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200565 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
Terje Bergstrom65793242013-03-22 16:34:03 +0200566 unsigned int i;
567
568 for (i = 0; i < job->num_unpins; i++) {
569 struct host1x_job_unpin_data *unpin = &job->unpins[i];
Thierry Reding6df633d2016-06-23 11:33:31 +0200570
Mikko Perttunen404bfb72016-12-14 13:16:14 +0200571 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
572 iommu_unmap(host->domain, job->addr_phys[i],
573 unpin->size);
574 free_iova(&host->iova,
575 iova_pfn(&host->iova, job->addr_phys[i]));
576 }
577
Terje Bergstrom65793242013-03-22 16:34:03 +0200578 host1x_bo_unpin(unpin->bo, unpin->sgt);
579 host1x_bo_put(unpin->bo);
580 }
Thierry Reding0b8070d12016-06-23 11:35:50 +0200581
Terje Bergstrom65793242013-03-22 16:34:03 +0200582 job->num_unpins = 0;
583
584 if (job->gather_copy_size)
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800585 dma_free_wc(job->channel->dev, job->gather_copy_size,
Thierry Reding0b8070d12016-06-23 11:35:50 +0200586 job->gather_copy_mapped, job->gather_copy);
Terje Bergstrom65793242013-03-22 16:34:03 +0200587}
Thierry Redingfae798a2013-11-08 11:41:42 +0100588EXPORT_SYMBOL(host1x_job_unpin);
Terje Bergstrom65793242013-03-22 16:34:03 +0200589
590/*
591 * Debug routine used to dump job entries
592 */
593void host1x_job_dump(struct device *dev, struct host1x_job *job)
594{
595 dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id);
596 dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
597 dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
598 dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
599 dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
600 dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
601}