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_relocs;
34 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
35 num_unpins += num_cmdbufs;
37 /* Check that we're not going to overflow */
38 total = sizeof(struct host1x_job) +
39 (u64)num_relocs * sizeof(struct host1x_reloc) +
40 (u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
41 (u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
42 (u64)num_unpins * sizeof(dma_addr_t) +
43 (u64)num_unpins * sizeof(u32 *);
44 if (total > ULONG_MAX)
47 mem = job = kzalloc(total, GFP_KERNEL);
54 /* Redistribute memory to the structs */
55 mem += sizeof(struct host1x_job);
56 job->relocs = num_relocs ? mem : NULL;
57 mem += num_relocs * sizeof(struct host1x_reloc);
58 job->unpins = num_unpins ? mem : NULL;
59 mem += num_unpins * sizeof(struct host1x_job_unpin_data);
60 job->gathers = num_cmdbufs ? mem : NULL;
61 mem += num_cmdbufs * sizeof(struct host1x_job_gather);
62 job->addr_phys = num_unpins ? mem : NULL;
64 job->reloc_addr_phys = job->addr_phys;
65 job->gather_addr_phys = &job->addr_phys[num_relocs];
69 EXPORT_SYMBOL(host1x_job_alloc);
71 struct host1x_job *host1x_job_get(struct host1x_job *job)
76 EXPORT_SYMBOL(host1x_job_get);
78 static void job_free(struct kref *ref)
80 struct host1x_job *job = container_of(ref, struct host1x_job, ref);
83 host1x_syncpt_put(job->syncpt);
88 void host1x_job_put(struct host1x_job *job)
90 kref_put(&job->ref, job_free);
92 EXPORT_SYMBOL(host1x_job_put);
94 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
95 unsigned int words, unsigned int offset)
97 struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
99 gather->words = words;
101 gather->offset = offset;
105 EXPORT_SYMBOL(host1x_job_add_gather);
107 static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
109 struct host1x_client *client = job->client;
110 struct device *dev = client->dev;
111 struct host1x_job_gather *g;
112 struct iommu_domain *domain;
116 domain = iommu_get_domain_for_dev(dev);
119 for (i = 0; i < job->num_relocs; i++) {
120 struct host1x_reloc *reloc = &job->relocs[i];
121 dma_addr_t phys_addr, *phys;
122 struct sg_table *sgt;
124 reloc->target.bo = host1x_bo_get(reloc->target.bo);
125 if (!reloc->target.bo) {
131 * If the client device is not attached to an IOMMU, the
132 * physical address of the buffer object can be used.
134 * Similarly, when an IOMMU domain is shared between all
135 * host1x clients, the IOVA is already available, so no
136 * need to map the buffer object again.
138 * XXX Note that this isn't always safe to do because it
139 * relies on an assumption that no cache maintenance is
140 * needed on the buffer objects.
142 if (!domain || client->group)
147 sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
154 unsigned long mask = HOST1X_RELOC_READ |
156 enum dma_data_direction dir;
158 switch (reloc->flags & mask) {
159 case HOST1X_RELOC_READ:
163 case HOST1X_RELOC_WRITE:
164 dir = DMA_FROM_DEVICE;
167 case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
168 dir = DMA_BIDIRECTIONAL;
176 err = dma_map_sgtable(dev, sgt, dir, 0);
180 job->unpins[job->num_unpins].dev = dev;
181 job->unpins[job->num_unpins].dir = dir;
182 phys_addr = sg_dma_address(sgt->sgl);
185 job->addr_phys[job->num_unpins] = phys_addr;
186 job->unpins[job->num_unpins].bo = reloc->target.bo;
187 job->unpins[job->num_unpins].sgt = sgt;
192 * We will copy gathers BO content later, so there is no need to
195 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
198 for (i = 0; i < job->num_gathers; i++) {
199 size_t gather_size = 0;
200 struct scatterlist *sg;
201 struct sg_table *sgt;
202 dma_addr_t phys_addr;
208 g = &job->gathers[i];
209 g->bo = host1x_bo_get(g->bo);
216 * If the host1x is not attached to an IOMMU, there is no need
217 * to map the buffer object for the host1x, since the physical
218 * address can simply be used.
220 if (!iommu_get_domain_for_dev(host->dev))
225 sgt = host1x_bo_pin(host->dev, g->bo, phys);
232 for_each_sgtable_sg(sgt, sg, j)
233 gather_size += sg->length;
234 gather_size = iova_align(&host->iova, gather_size);
236 shift = iova_shift(&host->iova);
237 alloc = alloc_iova(&host->iova, gather_size >> shift,
238 host->iova_end >> shift, true);
244 err = iommu_map_sgtable(host->domain,
245 iova_dma_addr(&host->iova, alloc),
248 __free_iova(&host->iova, alloc);
253 job->unpins[job->num_unpins].size = gather_size;
254 phys_addr = iova_dma_addr(&host->iova, alloc);
256 err = dma_map_sgtable(host->dev, sgt, DMA_TO_DEVICE, 0);
260 job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
261 job->unpins[job->num_unpins].dev = host->dev;
262 phys_addr = sg_dma_address(sgt->sgl);
265 job->addr_phys[job->num_unpins] = phys_addr;
266 job->gather_addr_phys[i] = phys_addr;
268 job->unpins[job->num_unpins].bo = g->bo;
269 job->unpins[job->num_unpins].sgt = sgt;
276 host1x_bo_put(g->bo);
278 host1x_job_unpin(job);
282 static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
284 void *cmdbuf_addr = NULL;
285 struct host1x_bo *cmdbuf = g->bo;
288 /* pin & patch the relocs for one gather */
289 for (i = 0; i < job->num_relocs; i++) {
290 struct host1x_reloc *reloc = &job->relocs[i];
291 u32 reloc_addr = (job->reloc_addr_phys[i] +
292 reloc->target.offset) >> reloc->shift;
295 /* skip all other gathers */
296 if (cmdbuf != reloc->cmdbuf.bo)
299 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
300 target = (u32 *)job->gather_copy_mapped +
301 reloc->cmdbuf.offset / sizeof(u32) +
302 g->offset / sizeof(u32);
307 cmdbuf_addr = host1x_bo_mmap(cmdbuf);
309 if (unlikely(!cmdbuf_addr)) {
310 pr_err("Could not map cmdbuf for relocation\n");
315 target = cmdbuf_addr + reloc->cmdbuf.offset;
317 *target = reloc_addr;
321 host1x_bo_munmap(cmdbuf, cmdbuf_addr);
326 static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
329 offset *= sizeof(u32);
331 if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
334 /* relocation shift value validation isn't implemented yet */
341 struct host1x_firewall {
342 struct host1x_job *job;
345 unsigned int num_relocs;
346 struct host1x_reloc *reloc;
348 struct host1x_bo *cmdbuf;
358 static int check_register(struct host1x_firewall *fw, unsigned long offset)
360 if (!fw->job->is_addr_reg)
363 if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
367 if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
377 static int check_class(struct host1x_firewall *fw, u32 class)
379 if (!fw->job->is_valid_class) {
380 if (fw->class != class)
383 if (!fw->job->is_valid_class(fw->class))
390 static int check_mask(struct host1x_firewall *fw)
401 ret = check_register(fw, reg);
415 static int check_incr(struct host1x_firewall *fw)
417 u32 count = fw->count;
425 ret = check_register(fw, reg);
438 static int check_nonincr(struct host1x_firewall *fw)
440 u32 count = fw->count;
447 ret = check_register(fw, fw->reg);
459 static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
461 u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
462 (g->offset / sizeof(u32));
463 u32 job_class = fw->class;
466 fw->words = g->words;
470 while (fw->words && !err) {
471 u32 word = cmdbuf_base[fw->offset];
472 u32 opcode = (word & 0xf0000000) >> 28;
482 fw->class = word >> 6 & 0x3ff;
483 fw->mask = word & 0x3f;
484 fw->reg = word >> 16 & 0xfff;
485 err = check_class(fw, job_class);
487 err = check_mask(fw);
492 fw->reg = word >> 16 & 0xfff;
493 fw->count = word & 0xffff;
494 err = check_incr(fw);
500 fw->reg = word >> 16 & 0xfff;
501 fw->count = word & 0xffff;
502 err = check_nonincr(fw);
508 fw->mask = word & 0xffff;
509 fw->reg = word >> 16 & 0xfff;
510 err = check_mask(fw);
527 static inline int copy_gathers(struct device *host, struct host1x_job *job,
530 struct host1x_firewall fw;
537 fw.reloc = job->relocs;
538 fw.num_relocs = job->num_relocs;
539 fw.class = job->class;
541 for (i = 0; i < job->num_gathers; i++) {
542 struct host1x_job_gather *g = &job->gathers[i];
544 size += g->words * sizeof(u32);
548 * Try a non-blocking allocation from a higher priority pools first,
549 * as awaiting for the allocation here is a major performance hit.
551 job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
554 /* the higher priority allocation failed, try the generic-blocking */
555 if (!job->gather_copy_mapped)
556 job->gather_copy_mapped = dma_alloc_wc(host, size,
559 if (!job->gather_copy_mapped)
562 job->gather_copy_size = size;
564 for (i = 0; i < job->num_gathers; i++) {
565 struct host1x_job_gather *g = &job->gathers[i];
568 /* Copy the gather */
569 gather = host1x_bo_mmap(g->bo);
570 memcpy(job->gather_copy_mapped + offset, gather + g->offset,
571 g->words * sizeof(u32));
572 host1x_bo_munmap(g->bo, gather);
574 /* Store the location in the buffer */
575 g->base = job->gather_copy;
578 /* Validate the job */
579 if (validate(&fw, g))
582 offset += g->words * sizeof(u32);
585 /* No relocs should remain at this point */
592 int host1x_job_pin(struct host1x_job *job, struct device *dev)
596 struct host1x *host = dev_get_drvdata(dev->parent);
599 err = pin_job(host, job);
603 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
604 err = copy_gathers(host->dev, job, dev);
610 for (i = 0; i < job->num_gathers; i++) {
611 struct host1x_job_gather *g = &job->gathers[i];
613 /* process each gather mem only once */
617 /* copy_gathers() sets gathers base if firewall is enabled */
618 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
619 g->base = job->gather_addr_phys[i];
621 for (j = i + 1; j < job->num_gathers; j++) {
622 if (job->gathers[j].bo == g->bo) {
623 job->gathers[j].handled = true;
624 job->gathers[j].base = g->base;
628 err = do_relocs(job, g);
635 host1x_job_unpin(job);
640 EXPORT_SYMBOL(host1x_job_pin);
642 void host1x_job_unpin(struct host1x_job *job)
644 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
647 for (i = 0; i < job->num_unpins; i++) {
648 struct host1x_job_unpin_data *unpin = &job->unpins[i];
649 struct device *dev = unpin->dev ?: host->dev;
650 struct sg_table *sgt = unpin->sgt;
652 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
653 unpin->size && host->domain) {
654 iommu_unmap(host->domain, job->addr_phys[i],
656 free_iova(&host->iova,
657 iova_pfn(&host->iova, job->addr_phys[i]));
660 if (unpin->dev && sgt)
661 dma_unmap_sgtable(unpin->dev, sgt, unpin->dir, 0);
663 host1x_bo_unpin(dev, unpin->bo, sgt);
664 host1x_bo_put(unpin->bo);
669 if (job->gather_copy_size)
670 dma_free_wc(host->dev, job->gather_copy_size,
671 job->gather_copy_mapped, job->gather_copy);
673 EXPORT_SYMBOL(host1x_job_unpin);
676 * Debug routine used to dump job entries
678 void host1x_job_dump(struct device *dev, struct host1x_job *job)
680 dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt->id);
681 dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
682 dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
683 dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
684 dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
685 dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);