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,
30 struct host1x_job *job = NULL;
31 unsigned int num_unpins = num_relocs;
36 enable_firewall = IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !skip_firewall;
39 num_unpins += num_cmdbufs;
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)
51 mem = job = kzalloc(total, GFP_KERNEL);
55 job->enable_firewall = enable_firewall;
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;
70 job->reloc_addr_phys = job->addr_phys;
71 job->gather_addr_phys = &job->addr_phys[num_relocs];
75 EXPORT_SYMBOL(host1x_job_alloc);
77 struct host1x_job *host1x_job_get(struct host1x_job *job)
82 EXPORT_SYMBOL(host1x_job_get);
84 static void job_free(struct kref *ref)
86 struct host1x_job *job = container_of(ref, struct host1x_job, ref);
92 host1x_intr_put_ref(job->syncpt->host, job->syncpt->id,
96 host1x_syncpt_put(job->syncpt);
101 void host1x_job_put(struct host1x_job *job)
103 kref_put(&job->ref, job_free);
105 EXPORT_SYMBOL(host1x_job_put);
107 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
108 unsigned int words, unsigned int offset)
110 struct host1x_job_gather *gather = &job->cmds[job->num_cmds].gather;
112 gather->words = words;
114 gather->offset = offset;
118 EXPORT_SYMBOL(host1x_job_add_gather);
120 void host1x_job_add_wait(struct host1x_job *job, u32 id, u32 thresh,
121 bool relative, u32 next_class)
123 struct host1x_job_cmd *cmd = &job->cmds[job->num_cmds];
127 cmd->wait.threshold = thresh;
128 cmd->wait.next_class = next_class;
129 cmd->wait.relative = relative;
133 EXPORT_SYMBOL(host1x_job_add_wait);
135 static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
137 struct host1x_client *client = job->client;
138 struct device *dev = client->dev;
139 struct host1x_job_gather *g;
140 struct iommu_domain *domain;
141 struct sg_table *sgt;
145 domain = iommu_get_domain_for_dev(dev);
148 for (i = 0; i < job->num_relocs; i++) {
149 struct host1x_reloc *reloc = &job->relocs[i];
150 dma_addr_t phys_addr, *phys;
152 reloc->target.bo = host1x_bo_get(reloc->target.bo);
153 if (!reloc->target.bo) {
159 * If the client device is not attached to an IOMMU, the
160 * physical address of the buffer object can be used.
162 * Similarly, when an IOMMU domain is shared between all
163 * host1x clients, the IOVA is already available, so no
164 * need to map the buffer object again.
166 * XXX Note that this isn't always safe to do because it
167 * relies on an assumption that no cache maintenance is
168 * needed on the buffer objects.
170 if (!domain || client->group)
175 sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
182 unsigned long mask = HOST1X_RELOC_READ |
184 enum dma_data_direction dir;
186 switch (reloc->flags & mask) {
187 case HOST1X_RELOC_READ:
191 case HOST1X_RELOC_WRITE:
192 dir = DMA_FROM_DEVICE;
195 case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
196 dir = DMA_BIDIRECTIONAL;
204 err = dma_map_sgtable(dev, sgt, dir, 0);
208 job->unpins[job->num_unpins].dev = dev;
209 job->unpins[job->num_unpins].dir = dir;
210 phys_addr = sg_dma_address(sgt->sgl);
213 job->addr_phys[job->num_unpins] = phys_addr;
214 job->unpins[job->num_unpins].bo = reloc->target.bo;
215 job->unpins[job->num_unpins].sgt = sgt;
220 * We will copy gathers BO content later, so there is no need to
223 if (job->enable_firewall)
226 for (i = 0; i < job->num_cmds; i++) {
227 size_t gather_size = 0;
228 struct scatterlist *sg;
229 dma_addr_t phys_addr;
235 if (job->cmds[i].is_wait)
238 g = &job->cmds[i].gather;
240 g->bo = host1x_bo_get(g->bo);
247 * If the host1x is not attached to an IOMMU, there is no need
248 * to map the buffer object for the host1x, since the physical
249 * address can simply be used.
251 if (!iommu_get_domain_for_dev(host->dev))
256 sgt = host1x_bo_pin(host->dev, g->bo, phys);
263 for_each_sgtable_sg(sgt, sg, j)
264 gather_size += sg->length;
265 gather_size = iova_align(&host->iova, gather_size);
267 shift = iova_shift(&host->iova);
268 alloc = alloc_iova(&host->iova, gather_size >> shift,
269 host->iova_end >> shift, true);
275 err = iommu_map_sgtable(host->domain,
276 iova_dma_addr(&host->iova, alloc),
279 __free_iova(&host->iova, alloc);
284 job->unpins[job->num_unpins].size = gather_size;
285 phys_addr = iova_dma_addr(&host->iova, alloc);
287 err = dma_map_sgtable(host->dev, sgt, DMA_TO_DEVICE, 0);
291 job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
292 job->unpins[job->num_unpins].dev = host->dev;
293 phys_addr = sg_dma_address(sgt->sgl);
296 job->addr_phys[job->num_unpins] = phys_addr;
297 job->gather_addr_phys[i] = phys_addr;
299 job->unpins[job->num_unpins].bo = g->bo;
300 job->unpins[job->num_unpins].sgt = sgt;
307 host1x_bo_put(g->bo);
309 host1x_job_unpin(job);
313 static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
315 void *cmdbuf_addr = NULL;
316 struct host1x_bo *cmdbuf = g->bo;
319 /* pin & patch the relocs for one gather */
320 for (i = 0; i < job->num_relocs; i++) {
321 struct host1x_reloc *reloc = &job->relocs[i];
322 u32 reloc_addr = (job->reloc_addr_phys[i] +
323 reloc->target.offset) >> reloc->shift;
326 /* skip all other gathers */
327 if (cmdbuf != reloc->cmdbuf.bo)
330 if (job->enable_firewall) {
331 target = (u32 *)job->gather_copy_mapped +
332 reloc->cmdbuf.offset / sizeof(u32) +
333 g->offset / sizeof(u32);
338 cmdbuf_addr = host1x_bo_mmap(cmdbuf);
340 if (unlikely(!cmdbuf_addr)) {
341 pr_err("Could not map cmdbuf for relocation\n");
346 target = cmdbuf_addr + reloc->cmdbuf.offset;
348 *target = reloc_addr;
352 host1x_bo_munmap(cmdbuf, cmdbuf_addr);
357 static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
360 offset *= sizeof(u32);
362 if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
365 /* relocation shift value validation isn't implemented yet */
372 struct host1x_firewall {
373 struct host1x_job *job;
376 unsigned int num_relocs;
377 struct host1x_reloc *reloc;
379 struct host1x_bo *cmdbuf;
389 static int check_register(struct host1x_firewall *fw, unsigned long offset)
391 if (!fw->job->is_addr_reg)
394 if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
398 if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
408 static int check_class(struct host1x_firewall *fw, u32 class)
410 if (!fw->job->is_valid_class) {
411 if (fw->class != class)
414 if (!fw->job->is_valid_class(fw->class))
421 static int check_mask(struct host1x_firewall *fw)
432 ret = check_register(fw, reg);
446 static int check_incr(struct host1x_firewall *fw)
448 u32 count = fw->count;
456 ret = check_register(fw, reg);
469 static int check_nonincr(struct host1x_firewall *fw)
471 u32 count = fw->count;
478 ret = check_register(fw, fw->reg);
490 static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
492 u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
493 (g->offset / sizeof(u32));
494 u32 job_class = fw->class;
497 fw->words = g->words;
501 while (fw->words && !err) {
502 u32 word = cmdbuf_base[fw->offset];
503 u32 opcode = (word & 0xf0000000) >> 28;
513 fw->class = word >> 6 & 0x3ff;
514 fw->mask = word & 0x3f;
515 fw->reg = word >> 16 & 0xfff;
516 err = check_class(fw, job_class);
518 err = check_mask(fw);
523 fw->reg = word >> 16 & 0xfff;
524 fw->count = word & 0xffff;
525 err = check_incr(fw);
531 fw->reg = word >> 16 & 0xfff;
532 fw->count = word & 0xffff;
533 err = check_nonincr(fw);
539 fw->mask = word & 0xffff;
540 fw->reg = word >> 16 & 0xfff;
541 err = check_mask(fw);
558 static inline int copy_gathers(struct device *host, struct host1x_job *job,
561 struct host1x_firewall fw;
568 fw.reloc = job->relocs;
569 fw.num_relocs = job->num_relocs;
570 fw.class = job->class;
572 for (i = 0; i < job->num_cmds; i++) {
573 struct host1x_job_gather *g;
575 if (job->cmds[i].is_wait)
578 g = &job->cmds[i].gather;
580 size += g->words * sizeof(u32);
584 * Try a non-blocking allocation from a higher priority pools first,
585 * as awaiting for the allocation here is a major performance hit.
587 job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
590 /* the higher priority allocation failed, try the generic-blocking */
591 if (!job->gather_copy_mapped)
592 job->gather_copy_mapped = dma_alloc_wc(host, size,
595 if (!job->gather_copy_mapped)
598 job->gather_copy_size = size;
600 for (i = 0; i < job->num_cmds; i++) {
601 struct host1x_job_gather *g;
604 if (job->cmds[i].is_wait)
606 g = &job->cmds[i].gather;
608 /* Copy the gather */
609 gather = host1x_bo_mmap(g->bo);
610 memcpy(job->gather_copy_mapped + offset, gather + g->offset,
611 g->words * sizeof(u32));
612 host1x_bo_munmap(g->bo, gather);
614 /* Store the location in the buffer */
615 g->base = job->gather_copy;
618 /* Validate the job */
619 if (validate(&fw, g))
622 offset += g->words * sizeof(u32);
625 /* No relocs should remain at this point */
632 int host1x_job_pin(struct host1x_job *job, struct device *dev)
636 struct host1x *host = dev_get_drvdata(dev->parent);
639 err = pin_job(host, job);
643 if (job->enable_firewall) {
644 err = copy_gathers(host->dev, job, dev);
650 for (i = 0; i < job->num_cmds; i++) {
651 struct host1x_job_gather *g;
653 if (job->cmds[i].is_wait)
655 g = &job->cmds[i].gather;
657 /* process each gather mem only once */
661 /* copy_gathers() sets gathers base if firewall is enabled */
662 if (!job->enable_firewall)
663 g->base = job->gather_addr_phys[i];
665 for (j = i + 1; j < job->num_cmds; j++) {
666 if (!job->cmds[j].is_wait &&
667 job->cmds[j].gather.bo == g->bo) {
668 job->cmds[j].gather.handled = true;
669 job->cmds[j].gather.base = g->base;
673 err = do_relocs(job, g);
680 host1x_job_unpin(job);
685 EXPORT_SYMBOL(host1x_job_pin);
687 void host1x_job_unpin(struct host1x_job *job)
689 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
692 for (i = 0; i < job->num_unpins; i++) {
693 struct host1x_job_unpin_data *unpin = &job->unpins[i];
694 struct device *dev = unpin->dev ?: host->dev;
695 struct sg_table *sgt = unpin->sgt;
697 if (!job->enable_firewall && unpin->size && host->domain) {
698 iommu_unmap(host->domain, job->addr_phys[i],
700 free_iova(&host->iova,
701 iova_pfn(&host->iova, job->addr_phys[i]));
704 if (unpin->dev && sgt)
705 dma_unmap_sgtable(unpin->dev, sgt, unpin->dir, 0);
707 host1x_bo_unpin(dev, unpin->bo, sgt);
708 host1x_bo_put(unpin->bo);
713 if (job->gather_copy_size)
714 dma_free_wc(host->dev, job->gather_copy_size,
715 job->gather_copy_mapped, job->gather_copy);
717 EXPORT_SYMBOL(host1x_job_unpin);
720 * Debug routine used to dump job entries
722 void host1x_job_dump(struct device *dev, struct host1x_job *job)
724 dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt->id);
725 dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
726 dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
727 dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
728 dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
729 dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);