19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 26579324aSTerje Bergstrom /* 36579324aSTerje Bergstrom * Tegra host1x Job 46579324aSTerje Bergstrom * 5f08ef2d1SArto Merilainen * Copyright (c) 2010-2015, NVIDIA Corporation. 66579324aSTerje Bergstrom */ 76579324aSTerje Bergstrom 86579324aSTerje Bergstrom #include <linux/dma-mapping.h> 96579324aSTerje Bergstrom #include <linux/err.h> 1035d747a8SThierry Reding #include <linux/host1x.h> 11273da5a0SThierry Reding #include <linux/iommu.h> 126579324aSTerje Bergstrom #include <linux/kref.h> 136579324aSTerje Bergstrom #include <linux/module.h> 146579324aSTerje Bergstrom #include <linux/scatterlist.h> 156579324aSTerje Bergstrom #include <linux/slab.h> 166579324aSTerje Bergstrom #include <linux/vmalloc.h> 176579324aSTerje Bergstrom #include <trace/events/host1x.h> 186579324aSTerje Bergstrom 196579324aSTerje Bergstrom #include "channel.h" 206579324aSTerje Bergstrom #include "dev.h" 216579324aSTerje Bergstrom #include "job.h" 226579324aSTerje Bergstrom #include "syncpt.h" 236579324aSTerje Bergstrom 24a47ac10eSDmitry Osipenko #define HOST1X_WAIT_SYNCPT_OFFSET 0x8 25a47ac10eSDmitry Osipenko 266579324aSTerje Bergstrom struct host1x_job *host1x_job_alloc(struct host1x_channel *ch, 2724c94e16SThierry Reding u32 num_cmdbufs, u32 num_relocs) 286579324aSTerje Bergstrom { 296579324aSTerje Bergstrom struct host1x_job *job = NULL; 3026c8de5eSDmitry Osipenko unsigned int num_unpins = num_relocs; 316579324aSTerje Bergstrom u64 total; 326579324aSTerje Bergstrom void *mem; 336579324aSTerje Bergstrom 3426c8de5eSDmitry Osipenko if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) 3526c8de5eSDmitry Osipenko num_unpins += num_cmdbufs; 3626c8de5eSDmitry Osipenko 376579324aSTerje Bergstrom /* Check that we're not going to overflow */ 386579324aSTerje Bergstrom total = sizeof(struct host1x_job) + 39f5fda676SDan Carpenter (u64)num_relocs * sizeof(struct host1x_reloc) + 40f5fda676SDan Carpenter (u64)num_unpins * sizeof(struct host1x_job_unpin_data) + 41f5fda676SDan Carpenter (u64)num_cmdbufs * sizeof(struct host1x_job_gather) + 42f5fda676SDan Carpenter (u64)num_unpins * sizeof(dma_addr_t) + 43f5fda676SDan Carpenter (u64)num_unpins * sizeof(u32 *); 446579324aSTerje Bergstrom if (total > ULONG_MAX) 456579324aSTerje Bergstrom return NULL; 466579324aSTerje Bergstrom 476579324aSTerje Bergstrom mem = job = kzalloc(total, GFP_KERNEL); 486579324aSTerje Bergstrom if (!job) 496579324aSTerje Bergstrom return NULL; 506579324aSTerje Bergstrom 516579324aSTerje Bergstrom kref_init(&job->ref); 526579324aSTerje Bergstrom job->channel = ch; 536579324aSTerje Bergstrom 546579324aSTerje Bergstrom /* Redistribute memory to the structs */ 556579324aSTerje Bergstrom mem += sizeof(struct host1x_job); 5606490bb9SThierry Reding job->relocs = num_relocs ? mem : NULL; 576579324aSTerje Bergstrom mem += num_relocs * sizeof(struct host1x_reloc); 586579324aSTerje Bergstrom job->unpins = num_unpins ? mem : NULL; 596579324aSTerje Bergstrom mem += num_unpins * sizeof(struct host1x_job_unpin_data); 606579324aSTerje Bergstrom job->gathers = num_cmdbufs ? mem : NULL; 616579324aSTerje Bergstrom mem += num_cmdbufs * sizeof(struct host1x_job_gather); 626579324aSTerje Bergstrom job->addr_phys = num_unpins ? mem : NULL; 636579324aSTerje Bergstrom 646579324aSTerje Bergstrom job->reloc_addr_phys = job->addr_phys; 656579324aSTerje Bergstrom job->gather_addr_phys = &job->addr_phys[num_relocs]; 666579324aSTerje Bergstrom 676579324aSTerje Bergstrom return job; 686579324aSTerje Bergstrom } 69fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_alloc); 706579324aSTerje Bergstrom 716579324aSTerje Bergstrom struct host1x_job *host1x_job_get(struct host1x_job *job) 726579324aSTerje Bergstrom { 736579324aSTerje Bergstrom kref_get(&job->ref); 746579324aSTerje Bergstrom return job; 756579324aSTerje Bergstrom } 76fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_get); 776579324aSTerje Bergstrom 786579324aSTerje Bergstrom static void job_free(struct kref *ref) 796579324aSTerje Bergstrom { 806579324aSTerje Bergstrom struct host1x_job *job = container_of(ref, struct host1x_job, ref); 816579324aSTerje Bergstrom 82*c78f837aSMikko Perttunen if (job->waiter) 83*c78f837aSMikko Perttunen host1x_intr_put_ref(job->syncpt->host, job->syncpt->id, 84*c78f837aSMikko Perttunen job->waiter, false); 85*c78f837aSMikko Perttunen 862aed4f5aSMikko Perttunen if (job->syncpt) 872aed4f5aSMikko Perttunen host1x_syncpt_put(job->syncpt); 882aed4f5aSMikko Perttunen 896579324aSTerje Bergstrom kfree(job); 906579324aSTerje Bergstrom } 916579324aSTerje Bergstrom 926579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job) 936579324aSTerje Bergstrom { 946579324aSTerje Bergstrom kref_put(&job->ref, job_free); 956579324aSTerje Bergstrom } 96fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put); 976579324aSTerje Bergstrom 986579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo, 99326bbd79SThierry Reding unsigned int words, unsigned int offset) 1006579324aSTerje Bergstrom { 101326bbd79SThierry Reding struct host1x_job_gather *gather = &job->gathers[job->num_gathers]; 1026579324aSTerje Bergstrom 103326bbd79SThierry Reding gather->words = words; 104326bbd79SThierry Reding gather->bo = bo; 105326bbd79SThierry Reding gather->offset = offset; 106326bbd79SThierry Reding 1076579324aSTerje Bergstrom job->num_gathers++; 1086579324aSTerje Bergstrom } 109fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather); 1106579324aSTerje Bergstrom 111404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job) 1126579324aSTerje Bergstrom { 113af1cbfb9SThierry Reding struct host1x_client *client = job->client; 114af1cbfb9SThierry Reding struct device *dev = client->dev; 115fd323e9eSDmitry Osipenko struct host1x_job_gather *g; 116273da5a0SThierry Reding struct iommu_domain *domain; 1176579324aSTerje Bergstrom unsigned int i; 118404bfb78SMikko Perttunen int err; 1196579324aSTerje Bergstrom 120273da5a0SThierry Reding domain = iommu_get_domain_for_dev(dev); 1216579324aSTerje Bergstrom job->num_unpins = 0; 1226579324aSTerje Bergstrom 1236579324aSTerje Bergstrom for (i = 0; i < job->num_relocs; i++) { 12406490bb9SThierry Reding struct host1x_reloc *reloc = &job->relocs[i]; 125af1cbfb9SThierry Reding dma_addr_t phys_addr, *phys; 1266579324aSTerje Bergstrom struct sg_table *sgt; 1276579324aSTerje Bergstrom 128961e3beaSThierry Reding reloc->target.bo = host1x_bo_get(reloc->target.bo); 129404bfb78SMikko Perttunen if (!reloc->target.bo) { 130404bfb78SMikko Perttunen err = -EINVAL; 1316579324aSTerje Bergstrom goto unpin; 132404bfb78SMikko Perttunen } 1336579324aSTerje Bergstrom 134273da5a0SThierry Reding /* 135273da5a0SThierry Reding * If the client device is not attached to an IOMMU, the 136273da5a0SThierry Reding * physical address of the buffer object can be used. 137273da5a0SThierry Reding * 138273da5a0SThierry Reding * Similarly, when an IOMMU domain is shared between all 139273da5a0SThierry Reding * host1x clients, the IOVA is already available, so no 140273da5a0SThierry Reding * need to map the buffer object again. 141273da5a0SThierry Reding * 142273da5a0SThierry Reding * XXX Note that this isn't always safe to do because it 143273da5a0SThierry Reding * relies on an assumption that no cache maintenance is 144273da5a0SThierry Reding * needed on the buffer objects. 145273da5a0SThierry Reding */ 146273da5a0SThierry Reding if (!domain || client->group) 147af1cbfb9SThierry Reding phys = &phys_addr; 148af1cbfb9SThierry Reding else 149af1cbfb9SThierry Reding phys = NULL; 150af1cbfb9SThierry Reding 151af1cbfb9SThierry Reding sgt = host1x_bo_pin(dev, reloc->target.bo, phys); 15280327ce3SThierry Reding if (IS_ERR(sgt)) { 15380327ce3SThierry Reding err = PTR_ERR(sgt); 15480327ce3SThierry Reding goto unpin; 15580327ce3SThierry Reding } 1566579324aSTerje Bergstrom 157af1cbfb9SThierry Reding if (sgt) { 158af1cbfb9SThierry Reding unsigned long mask = HOST1X_RELOC_READ | 159af1cbfb9SThierry Reding HOST1X_RELOC_WRITE; 160af1cbfb9SThierry Reding enum dma_data_direction dir; 161af1cbfb9SThierry Reding 162af1cbfb9SThierry Reding switch (reloc->flags & mask) { 163af1cbfb9SThierry Reding case HOST1X_RELOC_READ: 164af1cbfb9SThierry Reding dir = DMA_TO_DEVICE; 165af1cbfb9SThierry Reding break; 166af1cbfb9SThierry Reding 167af1cbfb9SThierry Reding case HOST1X_RELOC_WRITE: 168af1cbfb9SThierry Reding dir = DMA_FROM_DEVICE; 169af1cbfb9SThierry Reding break; 170af1cbfb9SThierry Reding 171af1cbfb9SThierry Reding case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE: 172af1cbfb9SThierry Reding dir = DMA_BIDIRECTIONAL; 173af1cbfb9SThierry Reding break; 174af1cbfb9SThierry Reding 175af1cbfb9SThierry Reding default: 176af1cbfb9SThierry Reding err = -EINVAL; 177af1cbfb9SThierry Reding goto unpin; 178af1cbfb9SThierry Reding } 179af1cbfb9SThierry Reding 18067ed9f9dSMarek Szyprowski err = dma_map_sgtable(dev, sgt, dir, 0); 18167ed9f9dSMarek Szyprowski if (err) 182af1cbfb9SThierry Reding goto unpin; 183af1cbfb9SThierry Reding 184af1cbfb9SThierry Reding job->unpins[job->num_unpins].dev = dev; 185af1cbfb9SThierry Reding job->unpins[job->num_unpins].dir = dir; 186af1cbfb9SThierry Reding phys_addr = sg_dma_address(sgt->sgl); 187af1cbfb9SThierry Reding } 188af1cbfb9SThierry Reding 1896579324aSTerje Bergstrom job->addr_phys[job->num_unpins] = phys_addr; 190961e3beaSThierry Reding job->unpins[job->num_unpins].bo = reloc->target.bo; 1916579324aSTerje Bergstrom job->unpins[job->num_unpins].sgt = sgt; 1926579324aSTerje Bergstrom job->num_unpins++; 1936579324aSTerje Bergstrom } 1946579324aSTerje Bergstrom 19526c8de5eSDmitry Osipenko /* 19626c8de5eSDmitry Osipenko * We will copy gathers BO content later, so there is no need to 19726c8de5eSDmitry Osipenko * hold and pin them. 19826c8de5eSDmitry Osipenko */ 19926c8de5eSDmitry Osipenko if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) 20026c8de5eSDmitry Osipenko return 0; 20126c8de5eSDmitry Osipenko 2026579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 203404bfb78SMikko Perttunen size_t gather_size = 0; 204404bfb78SMikko Perttunen struct scatterlist *sg; 2056579324aSTerje Bergstrom struct sg_table *sgt; 2066579324aSTerje Bergstrom dma_addr_t phys_addr; 207404bfb78SMikko Perttunen unsigned long shift; 208404bfb78SMikko Perttunen struct iova *alloc; 209273da5a0SThierry Reding dma_addr_t *phys; 210404bfb78SMikko Perttunen unsigned int j; 2116579324aSTerje Bergstrom 212fd323e9eSDmitry Osipenko g = &job->gathers[i]; 2136579324aSTerje Bergstrom g->bo = host1x_bo_get(g->bo); 214404bfb78SMikko Perttunen if (!g->bo) { 215404bfb78SMikko Perttunen err = -EINVAL; 2166579324aSTerje Bergstrom goto unpin; 217404bfb78SMikko Perttunen } 2186579324aSTerje Bergstrom 219273da5a0SThierry Reding /** 220273da5a0SThierry Reding * If the host1x is not attached to an IOMMU, there is no need 221273da5a0SThierry Reding * to map the buffer object for the host1x, since the physical 222273da5a0SThierry Reding * address can simply be used. 223273da5a0SThierry Reding */ 224273da5a0SThierry Reding if (!iommu_get_domain_for_dev(host->dev)) 225273da5a0SThierry Reding phys = &phys_addr; 226273da5a0SThierry Reding else 227273da5a0SThierry Reding phys = NULL; 228273da5a0SThierry Reding 229273da5a0SThierry Reding sgt = host1x_bo_pin(host->dev, g->bo, phys); 23080327ce3SThierry Reding if (IS_ERR(sgt)) { 23180327ce3SThierry Reding err = PTR_ERR(sgt); 232fd323e9eSDmitry Osipenko goto put; 23380327ce3SThierry Reding } 2346579324aSTerje Bergstrom 23526c8de5eSDmitry Osipenko if (host->domain) { 23667ed9f9dSMarek Szyprowski for_each_sgtable_sg(sgt, sg, j) 237404bfb78SMikko Perttunen gather_size += sg->length; 238404bfb78SMikko Perttunen gather_size = iova_align(&host->iova, gather_size); 239404bfb78SMikko Perttunen 240404bfb78SMikko Perttunen shift = iova_shift(&host->iova); 241404bfb78SMikko Perttunen alloc = alloc_iova(&host->iova, gather_size >> shift, 242404bfb78SMikko Perttunen host->iova_end >> shift, true); 243404bfb78SMikko Perttunen if (!alloc) { 244404bfb78SMikko Perttunen err = -ENOMEM; 245fd323e9eSDmitry Osipenko goto put; 246404bfb78SMikko Perttunen } 247404bfb78SMikko Perttunen 24867ed9f9dSMarek Szyprowski err = iommu_map_sgtable(host->domain, 249404bfb78SMikko Perttunen iova_dma_addr(&host->iova, alloc), 25067ed9f9dSMarek Szyprowski sgt, IOMMU_READ); 251404bfb78SMikko Perttunen if (err == 0) { 252404bfb78SMikko Perttunen __free_iova(&host->iova, alloc); 253404bfb78SMikko Perttunen err = -EINVAL; 254fd323e9eSDmitry Osipenko goto put; 255404bfb78SMikko Perttunen } 256404bfb78SMikko Perttunen 257404bfb78SMikko Perttunen job->unpins[job->num_unpins].size = gather_size; 258af1cbfb9SThierry Reding phys_addr = iova_dma_addr(&host->iova, alloc); 259273da5a0SThierry Reding } else if (sgt) { 26067ed9f9dSMarek Szyprowski err = dma_map_sgtable(host->dev, sgt, DMA_TO_DEVICE, 0); 26167ed9f9dSMarek Szyprowski if (err) 262fd323e9eSDmitry Osipenko goto put; 263404bfb78SMikko Perttunen 26498ae41adSThierry Reding job->unpins[job->num_unpins].dir = DMA_TO_DEVICE; 265af1cbfb9SThierry Reding job->unpins[job->num_unpins].dev = host->dev; 266af1cbfb9SThierry Reding phys_addr = sg_dma_address(sgt->sgl); 267af1cbfb9SThierry Reding } 268404bfb78SMikko Perttunen 269af1cbfb9SThierry Reding job->addr_phys[job->num_unpins] = phys_addr; 270af1cbfb9SThierry Reding job->gather_addr_phys[i] = phys_addr; 271af1cbfb9SThierry Reding 2726579324aSTerje Bergstrom job->unpins[job->num_unpins].bo = g->bo; 2736579324aSTerje Bergstrom job->unpins[job->num_unpins].sgt = sgt; 2746579324aSTerje Bergstrom job->num_unpins++; 2756579324aSTerje Bergstrom } 2766579324aSTerje Bergstrom 277404bfb78SMikko Perttunen return 0; 2786579324aSTerje Bergstrom 279fd323e9eSDmitry Osipenko put: 280fd323e9eSDmitry Osipenko host1x_bo_put(g->bo); 2816579324aSTerje Bergstrom unpin: 2826579324aSTerje Bergstrom host1x_job_unpin(job); 283404bfb78SMikko Perttunen return err; 2846579324aSTerje Bergstrom } 2856579324aSTerje Bergstrom 28647f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g) 2876579324aSTerje Bergstrom { 2887a8139c5SDaniel Vetter void *cmdbuf_addr = NULL; 28947f89c10SDmitry Osipenko struct host1x_bo *cmdbuf = g->bo; 290d4ad3ad9SThierry Reding unsigned int i; 2916579324aSTerje Bergstrom 2926579324aSTerje Bergstrom /* pin & patch the relocs for one gather */ 2933364cd28SArto Merilainen for (i = 0; i < job->num_relocs; i++) { 29406490bb9SThierry Reding struct host1x_reloc *reloc = &job->relocs[i]; 2956579324aSTerje Bergstrom u32 reloc_addr = (job->reloc_addr_phys[i] + 296961e3beaSThierry Reding reloc->target.offset) >> reloc->shift; 2976579324aSTerje Bergstrom u32 *target; 2986579324aSTerje Bergstrom 2996579324aSTerje Bergstrom /* skip all other gathers */ 300961e3beaSThierry Reding if (cmdbuf != reloc->cmdbuf.bo) 3016579324aSTerje Bergstrom continue; 3026579324aSTerje Bergstrom 30347f89c10SDmitry Osipenko if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) { 30447f89c10SDmitry Osipenko target = (u32 *)job->gather_copy_mapped + 30547f89c10SDmitry Osipenko reloc->cmdbuf.offset / sizeof(u32) + 30647f89c10SDmitry Osipenko g->offset / sizeof(u32); 30747f89c10SDmitry Osipenko goto patch_reloc; 30847f89c10SDmitry Osipenko } 30947f89c10SDmitry Osipenko 3107a8139c5SDaniel Vetter if (!cmdbuf_addr) { 3117a8139c5SDaniel Vetter cmdbuf_addr = host1x_bo_mmap(cmdbuf); 3126579324aSTerje Bergstrom 3137a8139c5SDaniel Vetter if (unlikely(!cmdbuf_addr)) { 3146579324aSTerje Bergstrom pr_err("Could not map cmdbuf for relocation\n"); 3156579324aSTerje Bergstrom return -ENOMEM; 3166579324aSTerje Bergstrom } 3176579324aSTerje Bergstrom } 3186579324aSTerje Bergstrom 3197a8139c5SDaniel Vetter target = cmdbuf_addr + reloc->cmdbuf.offset; 32047f89c10SDmitry Osipenko patch_reloc: 3216579324aSTerje Bergstrom *target = reloc_addr; 3226579324aSTerje Bergstrom } 3236579324aSTerje Bergstrom 3247a8139c5SDaniel Vetter if (cmdbuf_addr) 3257a8139c5SDaniel Vetter host1x_bo_munmap(cmdbuf, cmdbuf_addr); 3266579324aSTerje Bergstrom 3276579324aSTerje Bergstrom return 0; 3286579324aSTerje Bergstrom } 3296579324aSTerje Bergstrom 3305060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf, 3316579324aSTerje Bergstrom unsigned int offset) 3326579324aSTerje Bergstrom { 3336579324aSTerje Bergstrom offset *= sizeof(u32); 3346579324aSTerje Bergstrom 335961e3beaSThierry Reding if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset) 3365060d8ecSArto Merilainen return false; 3376579324aSTerje Bergstrom 338571cbf70SDmitry Osipenko /* relocation shift value validation isn't implemented yet */ 339571cbf70SDmitry Osipenko if (reloc->shift) 340571cbf70SDmitry Osipenko return false; 341571cbf70SDmitry Osipenko 3425060d8ecSArto Merilainen return true; 3436579324aSTerje Bergstrom } 3446579324aSTerje Bergstrom 3456579324aSTerje Bergstrom struct host1x_firewall { 3466579324aSTerje Bergstrom struct host1x_job *job; 3476579324aSTerje Bergstrom struct device *dev; 3486579324aSTerje Bergstrom 3496579324aSTerje Bergstrom unsigned int num_relocs; 3506579324aSTerje Bergstrom struct host1x_reloc *reloc; 3516579324aSTerje Bergstrom 352d7fbcf47SThierry Reding struct host1x_bo *cmdbuf; 3536579324aSTerje Bergstrom unsigned int offset; 3546579324aSTerje Bergstrom 3556579324aSTerje Bergstrom u32 words; 3566579324aSTerje Bergstrom u32 class; 3576579324aSTerje Bergstrom u32 reg; 3586579324aSTerje Bergstrom u32 mask; 3596579324aSTerje Bergstrom u32 count; 3606579324aSTerje Bergstrom }; 3616579324aSTerje Bergstrom 362d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset) 363d77563ffSThierry Reding { 3640f563a4bSDmitry Osipenko if (!fw->job->is_addr_reg) 3650f563a4bSDmitry Osipenko return 0; 3660f563a4bSDmitry Osipenko 367d77563ffSThierry Reding if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) { 368d77563ffSThierry Reding if (!fw->num_relocs) 369d77563ffSThierry Reding return -EINVAL; 370d77563ffSThierry Reding 371d77563ffSThierry Reding if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset)) 372d77563ffSThierry Reding return -EINVAL; 373d77563ffSThierry Reding 374d77563ffSThierry Reding fw->num_relocs--; 375d77563ffSThierry Reding fw->reloc++; 376d77563ffSThierry Reding } 377d77563ffSThierry Reding 378d77563ffSThierry Reding return 0; 379d77563ffSThierry Reding } 380d77563ffSThierry Reding 3810f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class) 3820f563a4bSDmitry Osipenko { 3830f563a4bSDmitry Osipenko if (!fw->job->is_valid_class) { 3840f563a4bSDmitry Osipenko if (fw->class != class) 3850f563a4bSDmitry Osipenko return -EINVAL; 3860f563a4bSDmitry Osipenko } else { 3870f563a4bSDmitry Osipenko if (!fw->job->is_valid_class(fw->class)) 3880f563a4bSDmitry Osipenko return -EINVAL; 3890f563a4bSDmitry Osipenko } 3900f563a4bSDmitry Osipenko 3910f563a4bSDmitry Osipenko return 0; 3920f563a4bSDmitry Osipenko } 3930f563a4bSDmitry Osipenko 3946579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw) 3956579324aSTerje Bergstrom { 3966579324aSTerje Bergstrom u32 mask = fw->mask; 3976579324aSTerje Bergstrom u32 reg = fw->reg; 398d77563ffSThierry Reding int ret; 3996579324aSTerje Bergstrom 4006579324aSTerje Bergstrom while (mask) { 4016579324aSTerje Bergstrom if (fw->words == 0) 4026579324aSTerje Bergstrom return -EINVAL; 4036579324aSTerje Bergstrom 4046579324aSTerje Bergstrom if (mask & 1) { 405d77563ffSThierry Reding ret = check_register(fw, reg); 406d77563ffSThierry Reding if (ret < 0) 407d77563ffSThierry Reding return ret; 408d77563ffSThierry Reding 4096579324aSTerje Bergstrom fw->words--; 4106579324aSTerje Bergstrom fw->offset++; 4116579324aSTerje Bergstrom } 4126579324aSTerje Bergstrom mask >>= 1; 4136579324aSTerje Bergstrom reg++; 4146579324aSTerje Bergstrom } 4156579324aSTerje Bergstrom 4166579324aSTerje Bergstrom return 0; 4176579324aSTerje Bergstrom } 4186579324aSTerje Bergstrom 4196579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw) 4206579324aSTerje Bergstrom { 4216579324aSTerje Bergstrom u32 count = fw->count; 4226579324aSTerje Bergstrom u32 reg = fw->reg; 423d77563ffSThierry Reding int ret; 4246579324aSTerje Bergstrom 42564c173d3STerje Bergstrom while (count) { 4266579324aSTerje Bergstrom if (fw->words == 0) 4276579324aSTerje Bergstrom return -EINVAL; 4286579324aSTerje Bergstrom 429d77563ffSThierry Reding ret = check_register(fw, reg); 430d77563ffSThierry Reding if (ret < 0) 431d77563ffSThierry Reding return ret; 432d77563ffSThierry Reding 4336579324aSTerje Bergstrom reg++; 4346579324aSTerje Bergstrom fw->words--; 4356579324aSTerje Bergstrom fw->offset++; 4366579324aSTerje Bergstrom count--; 4376579324aSTerje Bergstrom } 4386579324aSTerje Bergstrom 4396579324aSTerje Bergstrom return 0; 4406579324aSTerje Bergstrom } 4416579324aSTerje Bergstrom 4426579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw) 4436579324aSTerje Bergstrom { 4446579324aSTerje Bergstrom u32 count = fw->count; 445d77563ffSThierry Reding int ret; 4466579324aSTerje Bergstrom 4476579324aSTerje Bergstrom while (count) { 4486579324aSTerje Bergstrom if (fw->words == 0) 4496579324aSTerje Bergstrom return -EINVAL; 4506579324aSTerje Bergstrom 451d77563ffSThierry Reding ret = check_register(fw, fw->reg); 452d77563ffSThierry Reding if (ret < 0) 453d77563ffSThierry Reding return ret; 454d77563ffSThierry Reding 4556579324aSTerje Bergstrom fw->words--; 4566579324aSTerje Bergstrom fw->offset++; 4576579324aSTerje Bergstrom count--; 4586579324aSTerje Bergstrom } 4596579324aSTerje Bergstrom 4606579324aSTerje Bergstrom return 0; 4616579324aSTerje Bergstrom } 4626579324aSTerje Bergstrom 463afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g) 4646579324aSTerje Bergstrom { 4653364cd28SArto Merilainen u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped + 4663364cd28SArto Merilainen (g->offset / sizeof(u32)); 4670f563a4bSDmitry Osipenko u32 job_class = fw->class; 4686579324aSTerje Bergstrom int err = 0; 4696579324aSTerje Bergstrom 470afac0e43STerje Bergstrom fw->words = g->words; 471d7fbcf47SThierry Reding fw->cmdbuf = g->bo; 472afac0e43STerje Bergstrom fw->offset = 0; 4736579324aSTerje Bergstrom 474afac0e43STerje Bergstrom while (fw->words && !err) { 475afac0e43STerje Bergstrom u32 word = cmdbuf_base[fw->offset]; 4766579324aSTerje Bergstrom u32 opcode = (word & 0xf0000000) >> 28; 4776579324aSTerje Bergstrom 478afac0e43STerje Bergstrom fw->mask = 0; 479afac0e43STerje Bergstrom fw->reg = 0; 480afac0e43STerje Bergstrom fw->count = 0; 481afac0e43STerje Bergstrom fw->words--; 482afac0e43STerje Bergstrom fw->offset++; 4836579324aSTerje Bergstrom 4846579324aSTerje Bergstrom switch (opcode) { 4856579324aSTerje Bergstrom case 0: 486afac0e43STerje Bergstrom fw->class = word >> 6 & 0x3ff; 487afac0e43STerje Bergstrom fw->mask = word & 0x3f; 488afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 4890f563a4bSDmitry Osipenko err = check_class(fw, job_class); 4900f563a4bSDmitry Osipenko if (!err) 491afac0e43STerje Bergstrom err = check_mask(fw); 4926579324aSTerje Bergstrom if (err) 4936579324aSTerje Bergstrom goto out; 4946579324aSTerje Bergstrom break; 4956579324aSTerje Bergstrom case 1: 496afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 497afac0e43STerje Bergstrom fw->count = word & 0xffff; 498afac0e43STerje Bergstrom err = check_incr(fw); 4996579324aSTerje Bergstrom if (err) 5006579324aSTerje Bergstrom goto out; 5016579324aSTerje Bergstrom break; 5026579324aSTerje Bergstrom 5036579324aSTerje Bergstrom case 2: 504afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 505afac0e43STerje Bergstrom fw->count = word & 0xffff; 506afac0e43STerje Bergstrom err = check_nonincr(fw); 5076579324aSTerje Bergstrom if (err) 5086579324aSTerje Bergstrom goto out; 5096579324aSTerje Bergstrom break; 5106579324aSTerje Bergstrom 5116579324aSTerje Bergstrom case 3: 512afac0e43STerje Bergstrom fw->mask = word & 0xffff; 513afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 514afac0e43STerje Bergstrom err = check_mask(fw); 5156579324aSTerje Bergstrom if (err) 5166579324aSTerje Bergstrom goto out; 5176579324aSTerje Bergstrom break; 5186579324aSTerje Bergstrom case 4: 5196579324aSTerje Bergstrom case 14: 5206579324aSTerje Bergstrom break; 5216579324aSTerje Bergstrom default: 5226579324aSTerje Bergstrom err = -EINVAL; 5236579324aSTerje Bergstrom break; 5246579324aSTerje Bergstrom } 5256579324aSTerje Bergstrom } 5266579324aSTerje Bergstrom 5276579324aSTerje Bergstrom out: 5286579324aSTerje Bergstrom return err; 5296579324aSTerje Bergstrom } 5306579324aSTerje Bergstrom 531b78e70c0SThierry Reding static inline int copy_gathers(struct device *host, struct host1x_job *job, 532b78e70c0SThierry Reding struct device *dev) 5336579324aSTerje Bergstrom { 5343364cd28SArto Merilainen struct host1x_firewall fw; 5356579324aSTerje Bergstrom size_t size = 0; 5366579324aSTerje Bergstrom size_t offset = 0; 537d4ad3ad9SThierry Reding unsigned int i; 5386579324aSTerje Bergstrom 5393364cd28SArto Merilainen fw.job = job; 5403364cd28SArto Merilainen fw.dev = dev; 54106490bb9SThierry Reding fw.reloc = job->relocs; 5423364cd28SArto Merilainen fw.num_relocs = job->num_relocs; 5433833d16fSDmitry Osipenko fw.class = job->class; 5443364cd28SArto Merilainen 5456579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 5466579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 5476df633d0SThierry Reding 5486579324aSTerje Bergstrom size += g->words * sizeof(u32); 5496579324aSTerje Bergstrom } 5506579324aSTerje Bergstrom 55143240bbdSDmitry Osipenko /* 55243240bbdSDmitry Osipenko * Try a non-blocking allocation from a higher priority pools first, 55343240bbdSDmitry Osipenko * as awaiting for the allocation here is a major performance hit. 55443240bbdSDmitry Osipenko */ 555b78e70c0SThierry Reding job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy, 55643240bbdSDmitry Osipenko GFP_NOWAIT); 55743240bbdSDmitry Osipenko 55843240bbdSDmitry Osipenko /* the higher priority allocation failed, try the generic-blocking */ 55943240bbdSDmitry Osipenko if (!job->gather_copy_mapped) 560b78e70c0SThierry Reding job->gather_copy_mapped = dma_alloc_wc(host, size, 56143240bbdSDmitry Osipenko &job->gather_copy, 5626579324aSTerje Bergstrom GFP_KERNEL); 56343240bbdSDmitry Osipenko if (!job->gather_copy_mapped) 564745cecc0SDan Carpenter return -ENOMEM; 5656579324aSTerje Bergstrom 5666579324aSTerje Bergstrom job->gather_copy_size = size; 5676579324aSTerje Bergstrom 5686579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 5696579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 5706579324aSTerje Bergstrom void *gather; 5716579324aSTerje Bergstrom 5723364cd28SArto Merilainen /* Copy the gather */ 5736579324aSTerje Bergstrom gather = host1x_bo_mmap(g->bo); 5746579324aSTerje Bergstrom memcpy(job->gather_copy_mapped + offset, gather + g->offset, 5756579324aSTerje Bergstrom g->words * sizeof(u32)); 5766579324aSTerje Bergstrom host1x_bo_munmap(g->bo, gather); 5776579324aSTerje Bergstrom 5783364cd28SArto Merilainen /* Store the location in the buffer */ 5796579324aSTerje Bergstrom g->base = job->gather_copy; 5806579324aSTerje Bergstrom g->offset = offset; 5813364cd28SArto Merilainen 5823364cd28SArto Merilainen /* Validate the job */ 5833364cd28SArto Merilainen if (validate(&fw, g)) 5843364cd28SArto Merilainen return -EINVAL; 5856579324aSTerje Bergstrom 5866579324aSTerje Bergstrom offset += g->words * sizeof(u32); 5876579324aSTerje Bergstrom } 5886579324aSTerje Bergstrom 58924c94e16SThierry Reding /* No relocs should remain at this point */ 59024c94e16SThierry Reding if (fw.num_relocs) 591a9ff9995SErik Faye-Lund return -EINVAL; 592a9ff9995SErik Faye-Lund 5936579324aSTerje Bergstrom return 0; 5946579324aSTerje Bergstrom } 5956579324aSTerje Bergstrom 5966579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev) 5976579324aSTerje Bergstrom { 5986579324aSTerje Bergstrom int err; 5996579324aSTerje Bergstrom unsigned int i, j; 6006579324aSTerje Bergstrom struct host1x *host = dev_get_drvdata(dev->parent); 6016579324aSTerje Bergstrom 6026579324aSTerje Bergstrom /* pin memory */ 603404bfb78SMikko Perttunen err = pin_job(host, job); 604404bfb78SMikko Perttunen if (err) 6056579324aSTerje Bergstrom goto out; 6066579324aSTerje Bergstrom 60747f89c10SDmitry Osipenko if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) { 608b78e70c0SThierry Reding err = copy_gathers(host->dev, job, dev); 60947f89c10SDmitry Osipenko if (err) 61047f89c10SDmitry Osipenko goto out; 61147f89c10SDmitry Osipenko } 61247f89c10SDmitry Osipenko 6136579324aSTerje Bergstrom /* patch gathers */ 6146579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 6156579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 6166579324aSTerje Bergstrom 6176579324aSTerje Bergstrom /* process each gather mem only once */ 6186579324aSTerje Bergstrom if (g->handled) 6196579324aSTerje Bergstrom continue; 6206579324aSTerje Bergstrom 62147f89c10SDmitry Osipenko /* copy_gathers() sets gathers base if firewall is enabled */ 62247f89c10SDmitry Osipenko if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) 6236579324aSTerje Bergstrom g->base = job->gather_addr_phys[i]; 6246579324aSTerje Bergstrom 625f08ef2d1SArto Merilainen for (j = i + 1; j < job->num_gathers; j++) { 626f08ef2d1SArto Merilainen if (job->gathers[j].bo == g->bo) { 6276579324aSTerje Bergstrom job->gathers[j].handled = true; 628f08ef2d1SArto Merilainen job->gathers[j].base = g->base; 629f08ef2d1SArto Merilainen } 630f08ef2d1SArto Merilainen } 6316579324aSTerje Bergstrom 63247f89c10SDmitry Osipenko err = do_relocs(job, g); 6333364cd28SArto Merilainen if (err) 63447f89c10SDmitry Osipenko break; 6356579324aSTerje Bergstrom } 6366579324aSTerje Bergstrom 6376579324aSTerje Bergstrom out: 638e5855aa3SDmitry Osipenko if (err) 639e5855aa3SDmitry Osipenko host1x_job_unpin(job); 6406579324aSTerje Bergstrom wmb(); 6416579324aSTerje Bergstrom 6426579324aSTerje Bergstrom return err; 6436579324aSTerje Bergstrom } 644fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin); 6456579324aSTerje Bergstrom 6466579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job) 6476579324aSTerje Bergstrom { 648404bfb78SMikko Perttunen struct host1x *host = dev_get_drvdata(job->channel->dev->parent); 6496579324aSTerje Bergstrom unsigned int i; 6506579324aSTerje Bergstrom 6516579324aSTerje Bergstrom for (i = 0; i < job->num_unpins; i++) { 6526579324aSTerje Bergstrom struct host1x_job_unpin_data *unpin = &job->unpins[i]; 653af1cbfb9SThierry Reding struct device *dev = unpin->dev ?: host->dev; 654af1cbfb9SThierry Reding struct sg_table *sgt = unpin->sgt; 6556df633d0SThierry Reding 656ec589232SDmitry Osipenko if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && 657ec589232SDmitry Osipenko unpin->size && host->domain) { 658404bfb78SMikko Perttunen iommu_unmap(host->domain, job->addr_phys[i], 659404bfb78SMikko Perttunen unpin->size); 660404bfb78SMikko Perttunen free_iova(&host->iova, 661404bfb78SMikko Perttunen iova_pfn(&host->iova, job->addr_phys[i])); 662404bfb78SMikko Perttunen } 663404bfb78SMikko Perttunen 664af1cbfb9SThierry Reding if (unpin->dev && sgt) 66567ed9f9dSMarek Szyprowski dma_unmap_sgtable(unpin->dev, sgt, unpin->dir, 0); 666af1cbfb9SThierry Reding 667af1cbfb9SThierry Reding host1x_bo_unpin(dev, unpin->bo, sgt); 6686579324aSTerje Bergstrom host1x_bo_put(unpin->bo); 6696579324aSTerje Bergstrom } 6700b8070d1SThierry Reding 6716579324aSTerje Bergstrom job->num_unpins = 0; 6726579324aSTerje Bergstrom 6736579324aSTerje Bergstrom if (job->gather_copy_size) 674b78e70c0SThierry Reding dma_free_wc(host->dev, job->gather_copy_size, 675f6e45661SLuis R. Rodriguez job->gather_copy_mapped, job->gather_copy); 6766579324aSTerje Bergstrom } 677fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_unpin); 6786579324aSTerje Bergstrom 6796579324aSTerje Bergstrom /* 6806579324aSTerje Bergstrom * Debug routine used to dump job entries 6816579324aSTerje Bergstrom */ 6826579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job) 6836579324aSTerje Bergstrom { 6842aed4f5aSMikko Perttunen dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt->id); 6856579324aSTerje Bergstrom dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end); 6866579324aSTerje Bergstrom dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get); 6876579324aSTerje Bergstrom dev_dbg(dev, " TIMEOUT %d\n", job->timeout); 6886579324aSTerje Bergstrom dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots); 6896579324aSTerje Bergstrom dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins); 6906579324aSTerje Bergstrom } 691