1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2010-2015, NVIDIA Corporation.
8 #include <linux/dma-mapping.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>
24 #define HOST1X_WAIT_SYNCPT_OFFSET 0x8
26 struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
27 u32 num_cmdbufs, u32 num_relocs)
29 struct host1x_job *job = NULL;
30 unsigned int num_unpins = num_cmdbufs + num_relocs;
34 /* Check that we're not going to overflow */
35 total = sizeof(struct host1x_job) +
36 (u64)num_relocs * sizeof(struct host1x_reloc) +
37 (u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
38 (u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
39 (u64)num_unpins * sizeof(dma_addr_t) +
40 (u64)num_unpins * sizeof(u32 *);
41 if (total > ULONG_MAX)
44 mem = job = kzalloc(total, GFP_KERNEL);
51 /* Redistribute memory to the structs */
52 mem += sizeof(struct host1x_job);
53 job->relocs = num_relocs ? mem : NULL;
54 mem += num_relocs * sizeof(struct host1x_reloc);
55 job->unpins = num_unpins ? mem : NULL;
56 mem += num_unpins * sizeof(struct host1x_job_unpin_data);
57 job->gathers = num_cmdbufs ? mem : NULL;
58 mem += num_cmdbufs * sizeof(struct host1x_job_gather);
59 job->addr_phys = num_unpins ? mem : NULL;
61 job->reloc_addr_phys = job->addr_phys;
62 job->gather_addr_phys = &job->addr_phys[num_relocs];
66 EXPORT_SYMBOL(host1x_job_alloc);
68 struct host1x_job *host1x_job_get(struct host1x_job *job)
73 EXPORT_SYMBOL(host1x_job_get);
75 static void job_free(struct kref *ref)
77 struct host1x_job *job = container_of(ref, struct host1x_job, ref);
82 void host1x_job_put(struct host1x_job *job)
84 kref_put(&job->ref, job_free);
86 EXPORT_SYMBOL(host1x_job_put);
88 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
89 unsigned int words, unsigned int offset)
91 struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
93 gather->words = words;
95 gather->offset = offset;
99 EXPORT_SYMBOL(host1x_job_add_gather);
101 static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
103 struct host1x_client *client = job->client;
104 struct device *dev = client->dev;
105 struct iommu_domain *domain;
109 domain = iommu_get_domain_for_dev(dev);
112 for (i = 0; i < job->num_relocs; i++) {
113 struct host1x_reloc *reloc = &job->relocs[i];
114 dma_addr_t phys_addr, *phys;
115 struct sg_table *sgt;
117 reloc->target.bo = host1x_bo_get(reloc->target.bo);
118 if (!reloc->target.bo) {
124 * If the client device is not attached to an IOMMU, the
125 * physical address of the buffer object can be used.
127 * Similarly, when an IOMMU domain is shared between all
128 * host1x clients, the IOVA is already available, so no
129 * need to map the buffer object again.
131 * XXX Note that this isn't always safe to do because it
132 * relies on an assumption that no cache maintenance is
133 * needed on the buffer objects.
135 if (!domain || client->group)
140 sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
147 unsigned long mask = HOST1X_RELOC_READ |
149 enum dma_data_direction dir;
151 switch (reloc->flags & mask) {
152 case HOST1X_RELOC_READ:
156 case HOST1X_RELOC_WRITE:
157 dir = DMA_FROM_DEVICE;
160 case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
161 dir = DMA_BIDIRECTIONAL;
169 err = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
175 job->unpins[job->num_unpins].dev = dev;
176 job->unpins[job->num_unpins].dir = dir;
177 phys_addr = sg_dma_address(sgt->sgl);
180 job->addr_phys[job->num_unpins] = phys_addr;
181 job->unpins[job->num_unpins].bo = reloc->target.bo;
182 job->unpins[job->num_unpins].sgt = sgt;
186 for (i = 0; i < job->num_gathers; i++) {
187 struct host1x_job_gather *g = &job->gathers[i];
188 size_t gather_size = 0;
189 struct scatterlist *sg;
190 struct sg_table *sgt;
191 dma_addr_t phys_addr;
197 g->bo = host1x_bo_get(g->bo);
204 * If the host1x is not attached to an IOMMU, there is no need
205 * to map the buffer object for the host1x, since the physical
206 * address can simply be used.
208 if (!iommu_get_domain_for_dev(host->dev))
213 sgt = host1x_bo_pin(host->dev, g->bo, phys);
219 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
220 for_each_sg(sgt->sgl, sg, sgt->nents, j)
221 gather_size += sg->length;
222 gather_size = iova_align(&host->iova, gather_size);
224 shift = iova_shift(&host->iova);
225 alloc = alloc_iova(&host->iova, gather_size >> shift,
226 host->iova_end >> shift, true);
232 err = iommu_map_sg(host->domain,
233 iova_dma_addr(&host->iova, alloc),
234 sgt->sgl, sgt->nents, IOMMU_READ);
236 __free_iova(&host->iova, alloc);
241 job->unpins[job->num_unpins].size = gather_size;
242 phys_addr = iova_dma_addr(&host->iova, alloc);
244 err = dma_map_sg(host->dev, sgt->sgl, sgt->nents,
251 job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
252 job->unpins[job->num_unpins].dev = host->dev;
253 phys_addr = sg_dma_address(sgt->sgl);
256 job->addr_phys[job->num_unpins] = phys_addr;
257 job->gather_addr_phys[i] = phys_addr;
259 job->unpins[job->num_unpins].bo = g->bo;
260 job->unpins[job->num_unpins].sgt = sgt;
267 host1x_job_unpin(job);
271 static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
273 void *cmdbuf_addr = NULL;
274 struct host1x_bo *cmdbuf = g->bo;
277 /* pin & patch the relocs for one gather */
278 for (i = 0; i < job->num_relocs; i++) {
279 struct host1x_reloc *reloc = &job->relocs[i];
280 u32 reloc_addr = (job->reloc_addr_phys[i] +
281 reloc->target.offset) >> reloc->shift;
284 /* skip all other gathers */
285 if (cmdbuf != reloc->cmdbuf.bo)
288 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
289 target = (u32 *)job->gather_copy_mapped +
290 reloc->cmdbuf.offset / sizeof(u32) +
291 g->offset / sizeof(u32);
296 cmdbuf_addr = host1x_bo_mmap(cmdbuf);
298 if (unlikely(!cmdbuf_addr)) {
299 pr_err("Could not map cmdbuf for relocation\n");
304 target = cmdbuf_addr + reloc->cmdbuf.offset;
306 *target = reloc_addr;
310 host1x_bo_munmap(cmdbuf, cmdbuf_addr);
315 static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
318 offset *= sizeof(u32);
320 if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
323 /* relocation shift value validation isn't implemented yet */
330 struct host1x_firewall {
331 struct host1x_job *job;
334 unsigned int num_relocs;
335 struct host1x_reloc *reloc;
337 struct host1x_bo *cmdbuf;
347 static int check_register(struct host1x_firewall *fw, unsigned long offset)
349 if (!fw->job->is_addr_reg)
352 if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
356 if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
366 static int check_class(struct host1x_firewall *fw, u32 class)
368 if (!fw->job->is_valid_class) {
369 if (fw->class != class)
372 if (!fw->job->is_valid_class(fw->class))
379 static int check_mask(struct host1x_firewall *fw)
390 ret = check_register(fw, reg);
404 static int check_incr(struct host1x_firewall *fw)
406 u32 count = fw->count;
414 ret = check_register(fw, reg);
427 static int check_nonincr(struct host1x_firewall *fw)
429 u32 count = fw->count;
436 ret = check_register(fw, fw->reg);
448 static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
450 u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
451 (g->offset / sizeof(u32));
452 u32 job_class = fw->class;
455 fw->words = g->words;
459 while (fw->words && !err) {
460 u32 word = cmdbuf_base[fw->offset];
461 u32 opcode = (word & 0xf0000000) >> 28;
471 fw->class = word >> 6 & 0x3ff;
472 fw->mask = word & 0x3f;
473 fw->reg = word >> 16 & 0xfff;
474 err = check_class(fw, job_class);
476 err = check_mask(fw);
481 fw->reg = word >> 16 & 0xfff;
482 fw->count = word & 0xffff;
483 err = check_incr(fw);
489 fw->reg = word >> 16 & 0xfff;
490 fw->count = word & 0xffff;
491 err = check_nonincr(fw);
497 fw->mask = word & 0xffff;
498 fw->reg = word >> 16 & 0xfff;
499 err = check_mask(fw);
516 static inline int copy_gathers(struct device *host, struct host1x_job *job,
519 struct host1x_firewall fw;
526 fw.reloc = job->relocs;
527 fw.num_relocs = job->num_relocs;
528 fw.class = job->class;
530 for (i = 0; i < job->num_gathers; i++) {
531 struct host1x_job_gather *g = &job->gathers[i];
533 size += g->words * sizeof(u32);
537 * Try a non-blocking allocation from a higher priority pools first,
538 * as awaiting for the allocation here is a major performance hit.
540 job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
543 /* the higher priority allocation failed, try the generic-blocking */
544 if (!job->gather_copy_mapped)
545 job->gather_copy_mapped = dma_alloc_wc(host, size,
548 if (!job->gather_copy_mapped)
551 job->gather_copy_size = size;
553 for (i = 0; i < job->num_gathers; i++) {
554 struct host1x_job_gather *g = &job->gathers[i];
557 /* Copy the gather */
558 gather = host1x_bo_mmap(g->bo);
559 memcpy(job->gather_copy_mapped + offset, gather + g->offset,
560 g->words * sizeof(u32));
561 host1x_bo_munmap(g->bo, gather);
563 /* Store the location in the buffer */
564 g->base = job->gather_copy;
567 /* Validate the job */
568 if (validate(&fw, g))
571 offset += g->words * sizeof(u32);
574 /* No relocs should remain at this point */
581 int host1x_job_pin(struct host1x_job *job, struct device *dev)
585 struct host1x *host = dev_get_drvdata(dev->parent);
588 err = pin_job(host, job);
592 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
593 err = copy_gathers(host->dev, job, dev);
599 for (i = 0; i < job->num_gathers; i++) {
600 struct host1x_job_gather *g = &job->gathers[i];
602 /* process each gather mem only once */
606 /* copy_gathers() sets gathers base if firewall is enabled */
607 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
608 g->base = job->gather_addr_phys[i];
610 for (j = i + 1; j < job->num_gathers; j++) {
611 if (job->gathers[j].bo == g->bo) {
612 job->gathers[j].handled = true;
613 job->gathers[j].base = g->base;
617 err = do_relocs(job, g);
624 host1x_job_unpin(job);
629 EXPORT_SYMBOL(host1x_job_pin);
631 void host1x_job_unpin(struct host1x_job *job)
633 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
636 for (i = 0; i < job->num_unpins; i++) {
637 struct host1x_job_unpin_data *unpin = &job->unpins[i];
638 struct device *dev = unpin->dev ?: host->dev;
639 struct sg_table *sgt = unpin->sgt;
641 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
642 unpin->size && host->domain) {
643 iommu_unmap(host->domain, job->addr_phys[i],
645 free_iova(&host->iova,
646 iova_pfn(&host->iova, job->addr_phys[i]));
649 if (unpin->dev && sgt)
650 dma_unmap_sg(unpin->dev, sgt->sgl, sgt->nents,
653 host1x_bo_unpin(dev, unpin->bo, sgt);
654 host1x_bo_put(unpin->bo);
659 if (job->gather_copy_size)
660 dma_free_wc(host->dev, job->gather_copy_size,
661 job->gather_copy_mapped, job->gather_copy);
663 EXPORT_SYMBOL(host1x_job_unpin);
666 * Debug routine used to dump job entries
668 void host1x_job_dump(struct device *dev, struct host1x_job *job)
670 dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id);
671 dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
672 dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
673 dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
674 dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
675 dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);