Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Tegra host1x Job
  3 *
  4 * Copyright (c) 2010-2013, NVIDIA Corporation.
  5 *
  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>
 21#include <linux/host1x.h>
 
 22#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"
 31#include "job.h"
 32#include "syncpt.h"
 33
 
 
 34struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
 35				    u32 num_cmdbufs, u32 num_relocs,
 36				    u32 num_waitchks)
 37{
 38	struct host1x_job *job = NULL;
 39	unsigned int num_unpins = num_cmdbufs + num_relocs;
 
 40	u64 total;
 41	void *mem;
 42
 
 
 
 
 
 43	/* Check that we're not going to overflow */
 44	total = sizeof(struct host1x_job) +
 45		(u64)num_relocs * sizeof(struct host1x_reloc) +
 46		(u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
 47		(u64)num_waitchks * sizeof(struct host1x_waitchk) +
 48		(u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
 49		(u64)num_unpins * sizeof(dma_addr_t) +
 50		(u64)num_unpins * sizeof(u32 *);
 51	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);
 67	job->waitchk = num_waitchks ? mem : NULL;
 68	mem += num_waitchks * sizeof(struct host1x_waitchk);
 69	job->gathers = num_cmdbufs ? mem : NULL;
 70	mem += num_cmdbufs * sizeof(struct host1x_job_gather);
 71	job->addr_phys = num_unpins ? mem : NULL;
 72
 73	job->reloc_addr_phys = job->addr_phys;
 74	job->gather_addr_phys = &job->addr_phys[num_relocs];
 75
 76	return job;
 77}
 78EXPORT_SYMBOL(host1x_job_alloc);
 79
 80struct host1x_job *host1x_job_get(struct host1x_job *job)
 81{
 82	kref_get(&job->ref);
 83	return job;
 84}
 85EXPORT_SYMBOL(host1x_job_get);
 86
 87static void job_free(struct kref *ref)
 88{
 89	struct host1x_job *job = container_of(ref, struct host1x_job, ref);
 90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 91	kfree(job);
 92}
 93
 94void host1x_job_put(struct host1x_job *job)
 95{
 96	kref_put(&job->ref, job_free);
 97}
 98EXPORT_SYMBOL(host1x_job_put);
 99
100void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
101			   u32 words, u32 offset)
102{
103	struct host1x_job_gather *cur_gather = &job->gathers[job->num_gathers];
 
 
 
 
104
105	cur_gather->words = words;
106	cur_gather->bo = bo;
107	cur_gather->offset = offset;
108	job->num_gathers++;
109}
110EXPORT_SYMBOL(host1x_job_add_gather);
111
112/*
113 * NULL an already satisfied WAIT_SYNCPT host method, by patching its
114 * args in the command stream. The method data is changed to reference
115 * a reserved (never given out or incr) HOST1X_SYNCPT_RESERVED syncpt
116 * with a matching threshold value of 0, so is guaranteed to be popped
117 * by the host HW.
118 */
119static void host1x_syncpt_patch_offset(struct host1x_syncpt *sp,
120				       struct host1x_bo *h, u32 offset)
121{
122	void *patch_addr = NULL;
 
 
 
 
 
 
123
124	/* patch the wait */
125	patch_addr = host1x_bo_kmap(h, offset >> PAGE_SHIFT);
126	if (patch_addr) {
127		host1x_syncpt_patch_wait(sp,
128					 patch_addr + (offset & ~PAGE_MASK));
129		host1x_bo_kunmap(h, offset >> PAGE_SHIFT, patch_addr);
130	} else
131		pr_err("Could not map cmdbuf for wait check\n");
132}
 
133
134/*
135 * Check driver supplied waitchk structs for syncpt thresholds
136 * that have already been satisfied and NULL the comparison (to
137 * avoid a wrap condition in the HW).
138 */
139static int do_waitchks(struct host1x_job *job, struct host1x *host,
140		       struct host1x_bo *patch)
141{
142	int i;
143
144	/* compare syncpt vs wait threshold */
145	for (i = 0; i < job->num_waitchk; i++) {
146		struct host1x_waitchk *wait = &job->waitchk[i];
147		struct host1x_syncpt *sp =
148			host1x_syncpt_get(host, wait->syncpt_id);
149
150		/* validate syncpt id */
151		if (wait->syncpt_id > host1x_syncpt_nb_pts(host))
152			continue;
153
154		/* skip all other gathers */
155		if (patch != wait->bo)
156			continue;
157
158		trace_host1x_syncpt_wait_check(wait->bo, wait->offset,
159					       wait->syncpt_id, wait->thresh,
160					       host1x_syncpt_read_min(sp));
161
162		if (host1x_syncpt_is_expired(sp, wait->thresh)) {
163			dev_dbg(host->dev,
164				"drop WAIT id %d (%s) thresh 0x%x, min 0x%x\n",
165				wait->syncpt_id, sp->name, wait->thresh,
166				host1x_syncpt_read_min(sp));
167
168			host1x_syncpt_patch_offset(sp, patch, wait->offset);
 
 
 
169		}
170
171		wait->bo = NULL;
172	}
173
174	return 0;
175}
 
 
176
177static unsigned int pin_job(struct host1x_job *job)
178{
179	unsigned int i;
180
181	job->num_unpins = 0;
 
 
182
183	for (i = 0; i < job->num_relocs; i++) {
184		struct host1x_reloc *reloc = &job->relocarray[i];
185		struct sg_table *sgt;
186		dma_addr_t phys_addr;
187
188		reloc->target.bo = host1x_bo_get(reloc->target.bo);
189		if (!reloc->target.bo)
 
190			goto unpin;
 
191
192		phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
193		if (!phys_addr)
 
 
 
 
 
194			goto unpin;
 
195
196		job->addr_phys[job->num_unpins] = phys_addr;
197		job->unpins[job->num_unpins].bo = reloc->target.bo;
198		job->unpins[job->num_unpins].sgt = sgt;
199		job->num_unpins++;
200	}
201
202	for (i = 0; i < job->num_gathers; i++) {
203		struct host1x_job_gather *g = &job->gathers[i];
204		struct sg_table *sgt;
205		dma_addr_t phys_addr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
207		g->bo = host1x_bo_get(g->bo);
208		if (!g->bo)
 
209			goto unpin;
 
210
211		phys_addr = host1x_bo_pin(g->bo, &sgt);
212		if (!phys_addr)
 
213			goto unpin;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
215		job->addr_phys[job->num_unpins] = phys_addr;
216		job->unpins[job->num_unpins].bo = g->bo;
217		job->unpins[job->num_unpins].sgt = sgt;
 
 
 
 
 
 
 
 
 
 
 
218		job->num_unpins++;
 
 
219	}
220
221	return job->num_unpins;
222
 
 
223unpin:
224	host1x_job_unpin(job);
225	return 0;
226}
227
228static int do_relocs(struct host1x_job *job, struct host1x_bo *cmdbuf)
229{
230	int i = 0;
231	u32 last_page = ~0;
232	void *cmdbuf_page_addr = NULL;
233
234	/* pin & patch the relocs for one gather */
235	for (i = 0; i < job->num_relocs; i++) {
236		struct host1x_reloc *reloc = &job->relocarray[i];
237		u32 reloc_addr = (job->reloc_addr_phys[i] +
238				  reloc->target.offset) >> reloc->shift;
239		u32 *target;
240
241		/* skip all other gathers */
242		if (cmdbuf != reloc->cmdbuf.bo)
243			continue;
244
245		if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
246			if (cmdbuf_page_addr)
247				host1x_bo_kunmap(cmdbuf, last_page,
248						 cmdbuf_page_addr);
249
250			cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
251					reloc->cmdbuf.offset >> PAGE_SHIFT);
252			last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
 
253
254			if (unlikely(!cmdbuf_page_addr)) {
255				pr_err("Could not map cmdbuf for relocation\n");
256				return -ENOMEM;
257			}
258		}
259
260		target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
 
261		*target = reloc_addr;
262	}
263
264	if (cmdbuf_page_addr)
265		host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
266
267	return 0;
268}
269
270static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
271			unsigned int offset)
272{
273	offset *= sizeof(u32);
274
275	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
276		return false;
277
 
 
 
 
278	return true;
279}
280
281struct host1x_firewall {
282	struct host1x_job *job;
283	struct device *dev;
284
285	unsigned int num_relocs;
286	struct host1x_reloc *reloc;
287
288	struct host1x_bo *cmdbuf;
289	unsigned int offset;
290
291	u32 words;
292	u32 class;
293	u32 reg;
294	u32 mask;
295	u32 count;
296};
297
298static int check_register(struct host1x_firewall *fw, unsigned long offset)
299{
 
 
 
300	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
301		if (!fw->num_relocs)
302			return -EINVAL;
303
304		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
305			return -EINVAL;
306
307		fw->num_relocs--;
308		fw->reloc++;
309	}
310
311	return 0;
312}
313
 
 
 
 
 
 
 
 
 
 
 
 
 
314static int check_mask(struct host1x_firewall *fw)
315{
316	u32 mask = fw->mask;
317	u32 reg = fw->reg;
318	int ret;
319
320	while (mask) {
321		if (fw->words == 0)
322			return -EINVAL;
323
324		if (mask & 1) {
325			ret = check_register(fw, reg);
326			if (ret < 0)
327				return ret;
328
329			fw->words--;
330			fw->offset++;
331		}
332		mask >>= 1;
333		reg++;
334	}
335
336	return 0;
337}
338
339static int check_incr(struct host1x_firewall *fw)
340{
341	u32 count = fw->count;
342	u32 reg = fw->reg;
343	int ret;
344
345	while (count) {
346		if (fw->words == 0)
347			return -EINVAL;
348
349		ret = check_register(fw, reg);
350		if (ret < 0)
351			return ret;
352
353		reg++;
354		fw->words--;
355		fw->offset++;
356		count--;
357	}
358
359	return 0;
360}
361
362static int check_nonincr(struct host1x_firewall *fw)
363{
364	u32 count = fw->count;
365	int ret;
366
367	while (count) {
368		if (fw->words == 0)
369			return -EINVAL;
370
371		ret = check_register(fw, fw->reg);
372		if (ret < 0)
373			return ret;
374
375		fw->words--;
376		fw->offset++;
377		count--;
378	}
379
380	return 0;
381}
382
383static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
384{
385	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
386		(g->offset / sizeof(u32));
 
387	int err = 0;
388
389	if (!fw->job->is_addr_reg)
390		return 0;
391
392	fw->words = g->words;
393	fw->cmdbuf = g->bo;
394	fw->offset = 0;
395
396	while (fw->words && !err) {
397		u32 word = cmdbuf_base[fw->offset];
398		u32 opcode = (word & 0xf0000000) >> 28;
399
400		fw->mask = 0;
401		fw->reg = 0;
402		fw->count = 0;
403		fw->words--;
404		fw->offset++;
405
406		switch (opcode) {
407		case 0:
408			fw->class = word >> 6 & 0x3ff;
409			fw->mask = word & 0x3f;
410			fw->reg = word >> 16 & 0xfff;
411			err = check_mask(fw);
 
 
412			if (err)
413				goto out;
414			break;
415		case 1:
416			fw->reg = word >> 16 & 0xfff;
417			fw->count = word & 0xffff;
418			err = check_incr(fw);
419			if (err)
420				goto out;
421			break;
422
423		case 2:
424			fw->reg = word >> 16 & 0xfff;
425			fw->count = word & 0xffff;
426			err = check_nonincr(fw);
427			if (err)
428				goto out;
429			break;
430
431		case 3:
432			fw->mask = word & 0xffff;
433			fw->reg = word >> 16 & 0xfff;
434			err = check_mask(fw);
435			if (err)
436				goto out;
437			break;
438		case 4:
439		case 5:
440		case 14:
441			break;
442		default:
443			err = -EINVAL;
444			break;
445		}
446	}
447
448out:
449	return err;
450}
451
452static inline int copy_gathers(struct host1x_job *job, struct device *dev)
 
453{
454	struct host1x_firewall fw;
455	size_t size = 0;
456	size_t offset = 0;
457	int i;
458
459	fw.job = job;
460	fw.dev = dev;
461	fw.reloc = job->relocarray;
462	fw.num_relocs = job->num_relocs;
463	fw.class = 0;
 
 
 
 
 
 
 
 
464
465	for (i = 0; i < job->num_gathers; i++) {
466		struct host1x_job_gather *g = &job->gathers[i];
467		size += g->words * sizeof(u32);
468	}
469
470	job->gather_copy_mapped = dma_alloc_wc(dev, size, &job->gather_copy,
471					       GFP_KERNEL);
472	if (!job->gather_copy_mapped) {
473		job->gather_copy_mapped = NULL;
 
 
 
 
 
 
 
 
 
474		return -ENOMEM;
475	}
476
477	job->gather_copy_size = size;
478
479	for (i = 0; i < job->num_gathers; i++) {
480		struct host1x_job_gather *g = &job->gathers[i];
481		void *gather;
482
 
 
 
 
483		/* Copy the gather */
484		gather = host1x_bo_mmap(g->bo);
485		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
486		       g->words * sizeof(u32));
487		host1x_bo_munmap(g->bo, gather);
488
489		/* Store the location in the buffer */
490		g->base = job->gather_copy;
491		g->offset = offset;
492
493		/* Validate the job */
494		if (validate(&fw, g))
495			return -EINVAL;
496
497		offset += g->words * sizeof(u32);
498	}
499
500	/* No relocs should remain at this point */
501	if (fw.num_relocs)
502		return -EINVAL;
503
504	return 0;
505}
506
507int host1x_job_pin(struct host1x_job *job, struct device *dev)
508{
509	int err;
510	unsigned int i, j;
511	struct host1x *host = dev_get_drvdata(dev->parent);
512	DECLARE_BITMAP(waitchk_mask, host1x_syncpt_nb_pts(host));
513
514	bitmap_zero(waitchk_mask, host1x_syncpt_nb_pts(host));
515	for (i = 0; i < job->num_waitchk; i++) {
516		u32 syncpt_id = job->waitchk[i].syncpt_id;
517		if (syncpt_id < host1x_syncpt_nb_pts(host))
518			set_bit(syncpt_id, waitchk_mask);
519	}
520
521	/* get current syncpt values for waitchk */
522	for_each_set_bit(i, waitchk_mask, host1x_syncpt_nb_pts(host))
523		host1x_syncpt_load(host->syncpt + i);
524
525	/* pin memory */
526	err = pin_job(job);
527	if (!err)
528		goto out;
529
 
 
 
 
 
 
530	/* 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
538		g->base = job->gather_addr_phys[i];
539
540		for (j = i + 1; j < job->num_gathers; j++)
541			if (job->gathers[j].bo == g->bo)
542				job->gathers[j].handled = true;
543
544		err = do_relocs(job, g->bo);
545		if (err)
546			break;
 
 
547
548		err = do_waitchks(job, host, g->bo);
549		if (err)
550			break;
551	}
552
553	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !err) {
554		err = copy_gathers(job, dev);
555		if (err) {
556			host1x_job_unpin(job);
557			return err;
558		}
559	}
560
561out:
 
 
562	wmb();
563
564	return err;
565}
566EXPORT_SYMBOL(host1x_job_pin);
567
568void host1x_job_unpin(struct host1x_job *job)
569{
 
570	unsigned int i;
571
572	for (i = 0; i < job->num_unpins; i++) {
573		struct host1x_job_unpin_data *unpin = &job->unpins[i];
574		host1x_bo_unpin(unpin->bo, unpin->sgt);
575		host1x_bo_put(unpin->bo);
 
 
 
 
 
 
 
576	}
 
577	job->num_unpins = 0;
578
579	if (job->gather_copy_size)
580		dma_free_wc(job->channel->dev, job->gather_copy_size,
581		            job->gather_copy_mapped, job->gather_copy);
582}
583EXPORT_SYMBOL(host1x_job_unpin);
584
585/*
586 * Debug routine used to dump job entries
587 */
588void host1x_job_dump(struct device *dev, struct host1x_job *job)
589{
590	dev_dbg(dev, "    SYNCPT_ID   %d\n", job->syncpt_id);
591	dev_dbg(dev, "    SYNCPT_VAL  %d\n", job->syncpt_end);
592	dev_dbg(dev, "    FIRST_GET   0x%x\n", job->first_get);
593	dev_dbg(dev, "    TIMEOUT     %d\n", job->timeout);
594	dev_dbg(dev, "    NUM_SLOTS   %d\n", job->num_slots);
595	dev_dbg(dev, "    NUM_HANDLES %d\n", job->num_unpins);
596}
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Tegra host1x Job
  4 *
  5 * Copyright (c) 2010-2015, NVIDIA Corporation.
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/dma-mapping.h>
  9#include <linux/err.h>
 10#include <linux/host1x.h>
 11#include <linux/iommu.h>
 12#include <linux/kref.h>
 13#include <linux/module.h>
 14#include <linux/scatterlist.h>
 15#include <linux/slab.h>
 16#include <linux/vmalloc.h>
 17#include <trace/events/host1x.h>
 18
 19#include "channel.h"
 20#include "dev.h"
 21#include "job.h"
 22#include "syncpt.h"
 23
 24#define HOST1X_WAIT_SYNCPT_OFFSET 0x8
 25
 26struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
 27				    u32 num_cmdbufs, u32 num_relocs,
 28				    bool skip_firewall)
 29{
 30	struct host1x_job *job = NULL;
 31	unsigned int num_unpins = num_relocs;
 32	bool enable_firewall;
 33	u64 total;
 34	void *mem;
 35
 36	enable_firewall = IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !skip_firewall;
 37
 38	if (!enable_firewall)
 39		num_unpins += num_cmdbufs;
 40
 41	/* Check that we're not going to overflow */
 42	total = sizeof(struct host1x_job) +
 43		(u64)num_relocs * sizeof(struct host1x_reloc) +
 44		(u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
 45		(u64)num_cmdbufs * sizeof(struct host1x_job_cmd) +
 
 46		(u64)num_unpins * sizeof(dma_addr_t) +
 47		(u64)num_unpins * sizeof(u32 *);
 48	if (total > ULONG_MAX)
 49		return NULL;
 50
 51	mem = job = kzalloc(total, GFP_KERNEL);
 52	if (!job)
 53		return NULL;
 54
 55	job->enable_firewall = enable_firewall;
 56
 57	kref_init(&job->ref);
 58	job->channel = ch;
 59
 60	/* Redistribute memory to the structs  */
 61	mem += sizeof(struct host1x_job);
 62	job->relocs = num_relocs ? mem : NULL;
 63	mem += num_relocs * sizeof(struct host1x_reloc);
 64	job->unpins = num_unpins ? mem : NULL;
 65	mem += num_unpins * sizeof(struct host1x_job_unpin_data);
 66	job->cmds = num_cmdbufs ? mem : NULL;
 67	mem += num_cmdbufs * sizeof(struct host1x_job_cmd);
 
 
 68	job->addr_phys = num_unpins ? mem : NULL;
 69
 70	job->reloc_addr_phys = job->addr_phys;
 71	job->gather_addr_phys = &job->addr_phys[num_relocs];
 72
 73	return job;
 74}
 75EXPORT_SYMBOL(host1x_job_alloc);
 76
 77struct host1x_job *host1x_job_get(struct host1x_job *job)
 78{
 79	kref_get(&job->ref);
 80	return job;
 81}
 82EXPORT_SYMBOL(host1x_job_get);
 83
 84static void job_free(struct kref *ref)
 85{
 86	struct host1x_job *job = container_of(ref, struct host1x_job, ref);
 87
 88	if (job->release)
 89		job->release(job);
 90
 91	if (job->fence) {
 92		/*
 93		 * remove_callback is atomic w.r.t. fence signaling, so
 94		 * after the call returns, we know that the callback is not
 95		 * in execution, and the fence can be safely freed.
 96		 */
 97		dma_fence_remove_callback(job->fence, &job->fence_cb);
 98		dma_fence_put(job->fence);
 99	}
100
101	if (job->syncpt)
102		host1x_syncpt_put(job->syncpt);
103
104	kfree(job);
105}
106
107void host1x_job_put(struct host1x_job *job)
108{
109	kref_put(&job->ref, job_free);
110}
111EXPORT_SYMBOL(host1x_job_put);
112
113void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
114			   unsigned int words, unsigned int offset)
115{
116	struct host1x_job_gather *gather = &job->cmds[job->num_cmds].gather;
117
118	gather->words = words;
119	gather->bo = bo;
120	gather->offset = offset;
121
122	job->num_cmds++;
 
 
 
123}
124EXPORT_SYMBOL(host1x_job_add_gather);
125
126void host1x_job_add_wait(struct host1x_job *job, u32 id, u32 thresh,
127			 bool relative, u32 next_class)
 
 
 
 
 
 
 
128{
129	struct host1x_job_cmd *cmd = &job->cmds[job->num_cmds];
130
131	cmd->is_wait = true;
132	cmd->wait.id = id;
133	cmd->wait.threshold = thresh;
134	cmd->wait.next_class = next_class;
135	cmd->wait.relative = relative;
136
137	job->num_cmds++;
 
 
 
 
 
 
 
138}
139EXPORT_SYMBOL(host1x_job_add_wait);
140
141static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
 
 
 
 
 
 
142{
143	unsigned long mask = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
144	struct host1x_client *client = job->client;
145	struct device *dev = client->dev;
146	struct host1x_job_gather *g;
147	unsigned int i;
148	int err;
 
 
 
 
 
 
 
 
 
149
150	job->num_unpins = 0;
 
 
151
152	for (i = 0; i < job->num_relocs; i++) {
153		struct host1x_reloc *reloc = &job->relocs[i];
154		enum dma_data_direction direction;
155		struct host1x_bo_mapping *map;
156		struct host1x_bo *bo;
157
158		reloc->target.bo = host1x_bo_get(reloc->target.bo);
159		if (!reloc->target.bo) {
160			err = -EINVAL;
161			goto unpin;
162		}
163
164		bo = reloc->target.bo;
 
165
166		switch (reloc->flags & mask) {
167		case HOST1X_RELOC_READ:
168			direction = DMA_TO_DEVICE;
169			break;
170
171		case HOST1X_RELOC_WRITE:
172			direction = DMA_FROM_DEVICE;
173			break;
174
175		case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
176			direction = DMA_BIDIRECTIONAL;
177			break;
178
179		default:
180			err = -EINVAL;
181			goto unpin;
182		}
183
184		map = host1x_bo_pin(dev, bo, direction, NULL);
185		if (IS_ERR(map)) {
186			err = PTR_ERR(map);
187			goto unpin;
188		}
189
190		/*
191		 * host1x clients are generally not able to do scatter-gather themselves, so fail
192		 * if the buffer is discontiguous and we fail to map its SG table to a single
193		 * contiguous chunk of I/O virtual memory.
194		 */
195		if (map->chunks > 1) {
196			err = -EINVAL;
197			goto unpin;
198		}
199
200		job->addr_phys[job->num_unpins] = map->phys;
201		job->unpins[job->num_unpins].map = map;
 
202		job->num_unpins++;
203	}
204
205	/*
206	 * We will copy gathers BO content later, so there is no need to
207	 * hold and pin them.
208	 */
209	if (job->enable_firewall)
210		return 0;
211
212	for (i = 0; i < job->num_cmds; i++) {
213		struct host1x_bo_mapping *map;
214		size_t gather_size = 0;
215		struct scatterlist *sg;
216		unsigned long shift;
217		struct iova *alloc;
218		unsigned int j;
219
220		if (job->cmds[i].is_wait)
221			continue;
222
223		g = &job->cmds[i].gather;
224
225		g->bo = host1x_bo_get(g->bo);
226		if (!g->bo) {
227			err = -EINVAL;
228			goto unpin;
229		}
230
231		map = host1x_bo_pin(host->dev, g->bo, DMA_TO_DEVICE, NULL);
232		if (IS_ERR(map)) {
233			err = PTR_ERR(map);
234			goto unpin;
235		}
236
237		if (host->domain) {
238			for_each_sgtable_sg(map->sgt, sg, j)
239				gather_size += sg->length;
240
241			gather_size = iova_align(&host->iova, gather_size);
242
243			shift = iova_shift(&host->iova);
244			alloc = alloc_iova(&host->iova, gather_size >> shift,
245					   host->iova_end >> shift, true);
246			if (!alloc) {
247				err = -ENOMEM;
248				goto put;
249			}
250
251			err = iommu_map_sgtable(host->domain, iova_dma_addr(&host->iova, alloc),
252						map->sgt, IOMMU_READ);
253			if (err == 0) {
254				__free_iova(&host->iova, alloc);
255				err = -EINVAL;
256				goto put;
257			}
258
259			map->phys = iova_dma_addr(&host->iova, alloc);
260			map->size = gather_size;
261		}
262
263		job->addr_phys[job->num_unpins] = map->phys;
264		job->unpins[job->num_unpins].map = map;
265		job->num_unpins++;
266
267		job->gather_addr_phys[i] = map->phys;
268	}
269
270	return 0;
271
272put:
273	host1x_bo_put(g->bo);
274unpin:
275	host1x_job_unpin(job);
276	return err;
277}
278
279static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
280{
281	void *cmdbuf_addr = NULL;
282	struct host1x_bo *cmdbuf = g->bo;
283	unsigned int i;
284
285	/* pin & patch the relocs for one gather */
286	for (i = 0; i < job->num_relocs; i++) {
287		struct host1x_reloc *reloc = &job->relocs[i];
288		u32 reloc_addr = (job->reloc_addr_phys[i] +
289				  reloc->target.offset) >> reloc->shift;
290		u32 *target;
291
292		/* skip all other gathers */
293		if (cmdbuf != reloc->cmdbuf.bo)
294			continue;
295
296		if (job->enable_firewall) {
297			target = (u32 *)job->gather_copy_mapped +
298					reloc->cmdbuf.offset / sizeof(u32) +
299						g->offset / sizeof(u32);
300			goto patch_reloc;
301		}
302
303		if (!cmdbuf_addr) {
304			cmdbuf_addr = host1x_bo_mmap(cmdbuf);
305
306			if (unlikely(!cmdbuf_addr)) {
307				pr_err("Could not map cmdbuf for relocation\n");
308				return -ENOMEM;
309			}
310		}
311
312		target = cmdbuf_addr + reloc->cmdbuf.offset;
313patch_reloc:
314		*target = reloc_addr;
315	}
316
317	if (cmdbuf_addr)
318		host1x_bo_munmap(cmdbuf, cmdbuf_addr);
319
320	return 0;
321}
322
323static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
324			unsigned int offset)
325{
326	offset *= sizeof(u32);
327
328	if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
329		return false;
330
331	/* relocation shift value validation isn't implemented yet */
332	if (reloc->shift)
333		return false;
334
335	return true;
336}
337
338struct host1x_firewall {
339	struct host1x_job *job;
340	struct device *dev;
341
342	unsigned int num_relocs;
343	struct host1x_reloc *reloc;
344
345	struct host1x_bo *cmdbuf;
346	unsigned int offset;
347
348	u32 words;
349	u32 class;
350	u32 reg;
351	u32 mask;
352	u32 count;
353};
354
355static int check_register(struct host1x_firewall *fw, unsigned long offset)
356{
357	if (!fw->job->is_addr_reg)
358		return 0;
359
360	if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
361		if (!fw->num_relocs)
362			return -EINVAL;
363
364		if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
365			return -EINVAL;
366
367		fw->num_relocs--;
368		fw->reloc++;
369	}
370
371	return 0;
372}
373
374static int check_class(struct host1x_firewall *fw, u32 class)
375{
376	if (!fw->job->is_valid_class) {
377		if (fw->class != class)
378			return -EINVAL;
379	} else {
380		if (!fw->job->is_valid_class(fw->class))
381			return -EINVAL;
382	}
383
384	return 0;
385}
386
387static int check_mask(struct host1x_firewall *fw)
388{
389	u32 mask = fw->mask;
390	u32 reg = fw->reg;
391	int ret;
392
393	while (mask) {
394		if (fw->words == 0)
395			return -EINVAL;
396
397		if (mask & 1) {
398			ret = check_register(fw, reg);
399			if (ret < 0)
400				return ret;
401
402			fw->words--;
403			fw->offset++;
404		}
405		mask >>= 1;
406		reg++;
407	}
408
409	return 0;
410}
411
412static int check_incr(struct host1x_firewall *fw)
413{
414	u32 count = fw->count;
415	u32 reg = fw->reg;
416	int ret;
417
418	while (count) {
419		if (fw->words == 0)
420			return -EINVAL;
421
422		ret = check_register(fw, reg);
423		if (ret < 0)
424			return ret;
425
426		reg++;
427		fw->words--;
428		fw->offset++;
429		count--;
430	}
431
432	return 0;
433}
434
435static int check_nonincr(struct host1x_firewall *fw)
436{
437	u32 count = fw->count;
438	int ret;
439
440	while (count) {
441		if (fw->words == 0)
442			return -EINVAL;
443
444		ret = check_register(fw, fw->reg);
445		if (ret < 0)
446			return ret;
447
448		fw->words--;
449		fw->offset++;
450		count--;
451	}
452
453	return 0;
454}
455
456static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
457{
458	u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
459		(g->offset / sizeof(u32));
460	u32 job_class = fw->class;
461	int err = 0;
462
 
 
 
463	fw->words = g->words;
464	fw->cmdbuf = g->bo;
465	fw->offset = 0;
466
467	while (fw->words && !err) {
468		u32 word = cmdbuf_base[fw->offset];
469		u32 opcode = (word & 0xf0000000) >> 28;
470
471		fw->mask = 0;
472		fw->reg = 0;
473		fw->count = 0;
474		fw->words--;
475		fw->offset++;
476
477		switch (opcode) {
478		case 0:
479			fw->class = word >> 6 & 0x3ff;
480			fw->mask = word & 0x3f;
481			fw->reg = word >> 16 & 0xfff;
482			err = check_class(fw, job_class);
483			if (!err)
484				err = check_mask(fw);
485			if (err)
486				goto out;
487			break;
488		case 1:
489			fw->reg = word >> 16 & 0xfff;
490			fw->count = word & 0xffff;
491			err = check_incr(fw);
492			if (err)
493				goto out;
494			break;
495
496		case 2:
497			fw->reg = word >> 16 & 0xfff;
498			fw->count = word & 0xffff;
499			err = check_nonincr(fw);
500			if (err)
501				goto out;
502			break;
503
504		case 3:
505			fw->mask = word & 0xffff;
506			fw->reg = word >> 16 & 0xfff;
507			err = check_mask(fw);
508			if (err)
509				goto out;
510			break;
511		case 4:
 
512		case 14:
513			break;
514		default:
515			err = -EINVAL;
516			break;
517		}
518	}
519
520out:
521	return err;
522}
523
524static inline int copy_gathers(struct device *host, struct host1x_job *job,
525			       struct device *dev)
526{
527	struct host1x_firewall fw;
528	size_t size = 0;
529	size_t offset = 0;
530	unsigned int i;
531
532	fw.job = job;
533	fw.dev = dev;
534	fw.reloc = job->relocs;
535	fw.num_relocs = job->num_relocs;
536	fw.class = job->class;
537
538	for (i = 0; i < job->num_cmds; i++) {
539		struct host1x_job_gather *g;
540
541		if (job->cmds[i].is_wait)
542			continue;
543
544		g = &job->cmds[i].gather;
545
 
 
546		size += g->words * sizeof(u32);
547	}
548
549	/*
550	 * Try a non-blocking allocation from a higher priority pools first,
551	 * as awaiting for the allocation here is a major performance hit.
552	 */
553	job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
554					       GFP_NOWAIT);
555
556	/* the higher priority allocation failed, try the generic-blocking */
557	if (!job->gather_copy_mapped)
558		job->gather_copy_mapped = dma_alloc_wc(host, size,
559						       &job->gather_copy,
560						       GFP_KERNEL);
561	if (!job->gather_copy_mapped)
562		return -ENOMEM;
 
563
564	job->gather_copy_size = size;
565
566	for (i = 0; i < job->num_cmds; i++) {
567		struct host1x_job_gather *g;
568		void *gather;
569
570		if (job->cmds[i].is_wait)
571			continue;
572		g = &job->cmds[i].gather;
573
574		/* Copy the gather */
575		gather = host1x_bo_mmap(g->bo);
576		memcpy(job->gather_copy_mapped + offset, gather + g->offset,
577		       g->words * sizeof(u32));
578		host1x_bo_munmap(g->bo, gather);
579
580		/* Store the location in the buffer */
581		g->base = job->gather_copy;
582		g->offset = offset;
583
584		/* Validate the job */
585		if (validate(&fw, g))
586			return -EINVAL;
587
588		offset += g->words * sizeof(u32);
589	}
590
591	/* No relocs should remain at this point */
592	if (fw.num_relocs)
593		return -EINVAL;
594
595	return 0;
596}
597
598int host1x_job_pin(struct host1x_job *job, struct device *dev)
599{
600	int err;
601	unsigned int i, j;
602	struct host1x *host = dev_get_drvdata(dev->parent);
 
 
 
 
 
 
 
 
 
 
 
 
603
604	/* pin memory */
605	err = pin_job(host, job);
606	if (err)
607		goto out;
608
609	if (job->enable_firewall) {
610		err = copy_gathers(host->dev, job, dev);
611		if (err)
612			goto out;
613	}
614
615	/* patch gathers */
616	for (i = 0; i < job->num_cmds; i++) {
617		struct host1x_job_gather *g;
618
619		if (job->cmds[i].is_wait)
620			continue;
621		g = &job->cmds[i].gather;
622
623		/* process each gather mem only once */
624		if (g->handled)
625			continue;
626
627		/* copy_gathers() sets gathers base if firewall is enabled */
628		if (!job->enable_firewall)
629			g->base = job->gather_addr_phys[i];
630
631		for (j = i + 1; j < job->num_cmds; j++) {
632			if (!job->cmds[j].is_wait &&
633			    job->cmds[j].gather.bo == g->bo) {
634				job->cmds[j].gather.handled = true;
635				job->cmds[j].gather.base = g->base;
636			}
637		}
638
639		err = do_relocs(job, g);
640		if (err)
641			break;
642	}
643
 
 
 
 
 
 
 
 
644out:
645	if (err)
646		host1x_job_unpin(job);
647	wmb();
648
649	return err;
650}
651EXPORT_SYMBOL(host1x_job_pin);
652
653void host1x_job_unpin(struct host1x_job *job)
654{
655	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
656	unsigned int i;
657
658	for (i = 0; i < job->num_unpins; i++) {
659		struct host1x_bo_mapping *map = job->unpins[i].map;
660		struct host1x_bo *bo = map->bo;
661
662		if (!job->enable_firewall && map->size && host->domain) {
663			iommu_unmap(host->domain, job->addr_phys[i], map->size);
664			free_iova(&host->iova, iova_pfn(&host->iova, job->addr_phys[i]));
665		}
666
667		host1x_bo_unpin(map);
668		host1x_bo_put(bo);
669	}
670
671	job->num_unpins = 0;
672
673	if (job->gather_copy_size)
674		dma_free_wc(host->dev, job->gather_copy_size,
675			    job->gather_copy_mapped, job->gather_copy);
676}
677EXPORT_SYMBOL(host1x_job_unpin);
678
679/*
680 * Debug routine used to dump job entries
681 */
682void host1x_job_dump(struct device *dev, struct host1x_job *job)
683{
684	dev_dbg(dev, "    SYNCPT_ID   %d\n", job->syncpt->id);
685	dev_dbg(dev, "    SYNCPT_VAL  %d\n", job->syncpt_end);
686	dev_dbg(dev, "    FIRST_GET   0x%x\n", job->first_get);
687	dev_dbg(dev, "    TIMEOUT     %d\n", job->timeout);
688	dev_dbg(dev, "    NUM_SLOTS   %d\n", job->num_slots);
689	dev_dbg(dev, "    NUM_HANDLES %d\n", job->num_unpins);
690}