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/kref.h>
12 #include <linux/module.h>
13 #include <linux/scatterlist.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <trace/events/host1x.h>
23 #define HOST1X_WAIT_SYNCPT_OFFSET 0x8
25 struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
26 u32 num_cmdbufs, u32 num_relocs)
28 struct host1x_job *job = NULL;
29 unsigned int num_unpins = num_cmdbufs + num_relocs;
33 /* Check that we're not going to overflow */
34 total = sizeof(struct host1x_job) +
35 (u64)num_relocs * sizeof(struct host1x_reloc) +
36 (u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
37 (u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
38 (u64)num_unpins * sizeof(dma_addr_t) +
39 (u64)num_unpins * sizeof(u32 *);
40 if (total > ULONG_MAX)
43 mem = job = kzalloc(total, GFP_KERNEL);
50 /* Redistribute memory to the structs */
51 mem += sizeof(struct host1x_job);
52 job->relocs = num_relocs ? mem : NULL;
53 mem += num_relocs * sizeof(struct host1x_reloc);
54 job->unpins = num_unpins ? mem : NULL;
55 mem += num_unpins * sizeof(struct host1x_job_unpin_data);
56 job->gathers = num_cmdbufs ? mem : NULL;
57 mem += num_cmdbufs * sizeof(struct host1x_job_gather);
58 job->addr_phys = num_unpins ? mem : NULL;
60 job->reloc_addr_phys = job->addr_phys;
61 job->gather_addr_phys = &job->addr_phys[num_relocs];
65 EXPORT_SYMBOL(host1x_job_alloc);
67 struct host1x_job *host1x_job_get(struct host1x_job *job)
72 EXPORT_SYMBOL(host1x_job_get);
74 static void job_free(struct kref *ref)
76 struct host1x_job *job = container_of(ref, struct host1x_job, ref);
81 void host1x_job_put(struct host1x_job *job)
83 kref_put(&job->ref, job_free);
85 EXPORT_SYMBOL(host1x_job_put);
87 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
88 unsigned int words, unsigned int offset)
90 struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
92 gather->words = words;
94 gather->offset = offset;
98 EXPORT_SYMBOL(host1x_job_add_gather);
100 static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
102 struct host1x_client *client = job->client;
103 struct device *dev = client->dev;
109 for (i = 0; i < job->num_relocs; i++) {
110 struct host1x_reloc *reloc = &job->relocs[i];
111 dma_addr_t phys_addr, *phys;
112 struct sg_table *sgt;
114 reloc->target.bo = host1x_bo_get(reloc->target.bo);
115 if (!reloc->target.bo) {
125 sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
132 unsigned long mask = HOST1X_RELOC_READ |
134 enum dma_data_direction dir;
136 switch (reloc->flags & mask) {
137 case HOST1X_RELOC_READ:
141 case HOST1X_RELOC_WRITE:
142 dir = DMA_FROM_DEVICE;
145 case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
146 dir = DMA_BIDIRECTIONAL;
154 err = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
160 job->unpins[job->num_unpins].dev = dev;
161 job->unpins[job->num_unpins].dir = dir;
162 phys_addr = sg_dma_address(sgt->sgl);
165 job->addr_phys[job->num_unpins] = phys_addr;
166 job->unpins[job->num_unpins].bo = reloc->target.bo;
167 job->unpins[job->num_unpins].sgt = sgt;
171 for (i = 0; i < job->num_gathers; i++) {
172 struct host1x_job_gather *g = &job->gathers[i];
173 size_t gather_size = 0;
174 struct scatterlist *sg;
175 struct sg_table *sgt;
176 dma_addr_t phys_addr;
181 g->bo = host1x_bo_get(g->bo);
187 sgt = host1x_bo_pin(host->dev, g->bo, NULL);
193 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
194 for_each_sg(sgt->sgl, sg, sgt->nents, j)
195 gather_size += sg->length;
196 gather_size = iova_align(&host->iova, gather_size);
198 shift = iova_shift(&host->iova);
199 alloc = alloc_iova(&host->iova, gather_size >> shift,
200 host->iova_end >> shift, true);
206 err = iommu_map_sg(host->domain,
207 iova_dma_addr(&host->iova, alloc),
208 sgt->sgl, sgt->nents, IOMMU_READ);
210 __free_iova(&host->iova, alloc);
215 job->unpins[job->num_unpins].size = gather_size;
216 phys_addr = iova_dma_addr(&host->iova, alloc);
218 err = dma_map_sg(host->dev, sgt->sgl, sgt->nents,
225 job->unpins[job->num_unpins].dev = host->dev;
226 phys_addr = sg_dma_address(sgt->sgl);
229 job->addr_phys[job->num_unpins] = phys_addr;
230 job->gather_addr_phys[i] = phys_addr;
232 job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
233 job->unpins[job->num_unpins].bo = g->bo;
234 job->unpins[job->num_unpins].sgt = sgt;
241 host1x_job_unpin(job);
245 static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
248 void *cmdbuf_page_addr = NULL;
249 struct host1x_bo *cmdbuf = g->bo;
252 /* pin & patch the relocs for one gather */
253 for (i = 0; i < job->num_relocs; i++) {
254 struct host1x_reloc *reloc = &job->relocs[i];
255 u32 reloc_addr = (job->reloc_addr_phys[i] +
256 reloc->target.offset) >> reloc->shift;
259 /* skip all other gathers */
260 if (cmdbuf != reloc->cmdbuf.bo)
263 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
264 target = (u32 *)job->gather_copy_mapped +
265 reloc->cmdbuf.offset / sizeof(u32) +
266 g->offset / sizeof(u32);
270 if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
271 if (cmdbuf_page_addr)
272 host1x_bo_kunmap(cmdbuf, last_page,
275 cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
276 reloc->cmdbuf.offset >> PAGE_SHIFT);
277 last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
279 if (unlikely(!cmdbuf_page_addr)) {
280 pr_err("Could not map cmdbuf for relocation\n");
285 target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
287 *target = reloc_addr;
290 if (cmdbuf_page_addr)
291 host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
296 static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
299 offset *= sizeof(u32);
301 if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
304 /* relocation shift value validation isn't implemented yet */
311 struct host1x_firewall {
312 struct host1x_job *job;
315 unsigned int num_relocs;
316 struct host1x_reloc *reloc;
318 struct host1x_bo *cmdbuf;
328 static int check_register(struct host1x_firewall *fw, unsigned long offset)
330 if (!fw->job->is_addr_reg)
333 if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
337 if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
347 static int check_class(struct host1x_firewall *fw, u32 class)
349 if (!fw->job->is_valid_class) {
350 if (fw->class != class)
353 if (!fw->job->is_valid_class(fw->class))
360 static int check_mask(struct host1x_firewall *fw)
371 ret = check_register(fw, reg);
385 static int check_incr(struct host1x_firewall *fw)
387 u32 count = fw->count;
395 ret = check_register(fw, reg);
408 static int check_nonincr(struct host1x_firewall *fw)
410 u32 count = fw->count;
417 ret = check_register(fw, fw->reg);
429 static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
431 u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
432 (g->offset / sizeof(u32));
433 u32 job_class = fw->class;
436 fw->words = g->words;
440 while (fw->words && !err) {
441 u32 word = cmdbuf_base[fw->offset];
442 u32 opcode = (word & 0xf0000000) >> 28;
452 fw->class = word >> 6 & 0x3ff;
453 fw->mask = word & 0x3f;
454 fw->reg = word >> 16 & 0xfff;
455 err = check_class(fw, job_class);
457 err = check_mask(fw);
462 fw->reg = word >> 16 & 0xfff;
463 fw->count = word & 0xffff;
464 err = check_incr(fw);
470 fw->reg = word >> 16 & 0xfff;
471 fw->count = word & 0xffff;
472 err = check_nonincr(fw);
478 fw->mask = word & 0xffff;
479 fw->reg = word >> 16 & 0xfff;
480 err = check_mask(fw);
497 static inline int copy_gathers(struct device *host, struct host1x_job *job,
500 struct host1x_firewall fw;
507 fw.reloc = job->relocs;
508 fw.num_relocs = job->num_relocs;
509 fw.class = job->class;
511 for (i = 0; i < job->num_gathers; i++) {
512 struct host1x_job_gather *g = &job->gathers[i];
514 size += g->words * sizeof(u32);
518 * Try a non-blocking allocation from a higher priority pools first,
519 * as awaiting for the allocation here is a major performance hit.
521 job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
524 /* the higher priority allocation failed, try the generic-blocking */
525 if (!job->gather_copy_mapped)
526 job->gather_copy_mapped = dma_alloc_wc(host, size,
529 if (!job->gather_copy_mapped)
532 job->gather_copy_size = size;
534 for (i = 0; i < job->num_gathers; i++) {
535 struct host1x_job_gather *g = &job->gathers[i];
538 /* Copy the gather */
539 gather = host1x_bo_mmap(g->bo);
540 memcpy(job->gather_copy_mapped + offset, gather + g->offset,
541 g->words * sizeof(u32));
542 host1x_bo_munmap(g->bo, gather);
544 /* Store the location in the buffer */
545 g->base = job->gather_copy;
548 /* Validate the job */
549 if (validate(&fw, g))
552 offset += g->words * sizeof(u32);
555 /* No relocs should remain at this point */
562 int host1x_job_pin(struct host1x_job *job, struct device *dev)
566 struct host1x *host = dev_get_drvdata(dev->parent);
569 err = pin_job(host, job);
573 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
574 err = copy_gathers(host->dev, job, dev);
580 for (i = 0; i < job->num_gathers; i++) {
581 struct host1x_job_gather *g = &job->gathers[i];
583 /* process each gather mem only once */
587 /* copy_gathers() sets gathers base if firewall is enabled */
588 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
589 g->base = job->gather_addr_phys[i];
591 for (j = i + 1; j < job->num_gathers; j++) {
592 if (job->gathers[j].bo == g->bo) {
593 job->gathers[j].handled = true;
594 job->gathers[j].base = g->base;
598 err = do_relocs(job, g);
605 host1x_job_unpin(job);
610 EXPORT_SYMBOL(host1x_job_pin);
612 void host1x_job_unpin(struct host1x_job *job)
614 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
617 for (i = 0; i < job->num_unpins; i++) {
618 struct host1x_job_unpin_data *unpin = &job->unpins[i];
619 struct device *dev = unpin->dev ?: host->dev;
620 struct sg_table *sgt = unpin->sgt;
622 if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
623 unpin->size && host->domain) {
624 iommu_unmap(host->domain, job->addr_phys[i],
626 free_iova(&host->iova,
627 iova_pfn(&host->iova, job->addr_phys[i]));
630 if (unpin->dev && sgt)
631 dma_unmap_sg(unpin->dev, sgt->sgl, sgt->nents,
634 host1x_bo_unpin(dev, unpin->bo, sgt);
635 host1x_bo_put(unpin->bo);
640 if (job->gather_copy_size)
641 dma_free_wc(host->dev, job->gather_copy_size,
642 job->gather_copy_mapped, job->gather_copy);
644 EXPORT_SYMBOL(host1x_job_unpin);
647 * Debug routine used to dump job entries
649 void host1x_job_dump(struct device *dev, struct host1x_job *job)
651 dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id);
652 dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
653 dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
654 dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
655 dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
656 dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);