16579324aSTerje Bergstrom /* 26579324aSTerje Bergstrom * Tegra host1x Job 36579324aSTerje Bergstrom * 4f08ef2d1SArto Merilainen * Copyright (c) 2010-2015, NVIDIA Corporation. 56579324aSTerje Bergstrom * 66579324aSTerje Bergstrom * This program is free software; you can redistribute it and/or modify it 76579324aSTerje Bergstrom * under the terms and conditions of the GNU General Public License, 86579324aSTerje Bergstrom * version 2, as published by the Free Software Foundation. 96579324aSTerje Bergstrom * 106579324aSTerje Bergstrom * This program is distributed in the hope it will be useful, but WITHOUT 116579324aSTerje Bergstrom * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 126579324aSTerje Bergstrom * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 136579324aSTerje Bergstrom * more details. 146579324aSTerje Bergstrom * 156579324aSTerje Bergstrom * You should have received a copy of the GNU General Public License 166579324aSTerje Bergstrom * along with this program. If not, see <http://www.gnu.org/licenses/>. 176579324aSTerje Bergstrom */ 186579324aSTerje Bergstrom 196579324aSTerje Bergstrom #include <linux/dma-mapping.h> 206579324aSTerje Bergstrom #include <linux/err.h> 2135d747a8SThierry Reding #include <linux/host1x.h> 226579324aSTerje Bergstrom #include <linux/kref.h> 236579324aSTerje Bergstrom #include <linux/module.h> 246579324aSTerje Bergstrom #include <linux/scatterlist.h> 256579324aSTerje Bergstrom #include <linux/slab.h> 266579324aSTerje Bergstrom #include <linux/vmalloc.h> 276579324aSTerje Bergstrom #include <trace/events/host1x.h> 286579324aSTerje Bergstrom 296579324aSTerje Bergstrom #include "channel.h" 306579324aSTerje Bergstrom #include "dev.h" 316579324aSTerje Bergstrom #include "job.h" 326579324aSTerje Bergstrom #include "syncpt.h" 336579324aSTerje Bergstrom 34a47ac10eSDmitry Osipenko #define HOST1X_WAIT_SYNCPT_OFFSET 0x8 35a47ac10eSDmitry Osipenko 366579324aSTerje Bergstrom struct host1x_job *host1x_job_alloc(struct host1x_channel *ch, 376579324aSTerje Bergstrom u32 num_cmdbufs, u32 num_relocs, 386579324aSTerje Bergstrom u32 num_waitchks) 396579324aSTerje Bergstrom { 406579324aSTerje Bergstrom struct host1x_job *job = NULL; 416579324aSTerje Bergstrom unsigned int num_unpins = num_cmdbufs + num_relocs; 426579324aSTerje Bergstrom u64 total; 436579324aSTerje Bergstrom void *mem; 446579324aSTerje Bergstrom 456579324aSTerje Bergstrom /* Check that we're not going to overflow */ 466579324aSTerje Bergstrom total = sizeof(struct host1x_job) + 47f5fda676SDan Carpenter (u64)num_relocs * sizeof(struct host1x_reloc) + 48f5fda676SDan Carpenter (u64)num_unpins * sizeof(struct host1x_job_unpin_data) + 49f5fda676SDan Carpenter (u64)num_waitchks * sizeof(struct host1x_waitchk) + 50f5fda676SDan Carpenter (u64)num_cmdbufs * sizeof(struct host1x_job_gather) + 51f5fda676SDan Carpenter (u64)num_unpins * sizeof(dma_addr_t) + 52f5fda676SDan Carpenter (u64)num_unpins * sizeof(u32 *); 536579324aSTerje Bergstrom if (total > ULONG_MAX) 546579324aSTerje Bergstrom return NULL; 556579324aSTerje Bergstrom 566579324aSTerje Bergstrom mem = job = kzalloc(total, GFP_KERNEL); 576579324aSTerje Bergstrom if (!job) 586579324aSTerje Bergstrom return NULL; 596579324aSTerje Bergstrom 606579324aSTerje Bergstrom kref_init(&job->ref); 616579324aSTerje Bergstrom job->channel = ch; 626579324aSTerje Bergstrom 636579324aSTerje Bergstrom /* Redistribute memory to the structs */ 646579324aSTerje Bergstrom mem += sizeof(struct host1x_job); 656579324aSTerje Bergstrom job->relocarray = num_relocs ? mem : NULL; 666579324aSTerje Bergstrom mem += num_relocs * sizeof(struct host1x_reloc); 676579324aSTerje Bergstrom job->unpins = num_unpins ? mem : NULL; 686579324aSTerje Bergstrom mem += num_unpins * sizeof(struct host1x_job_unpin_data); 696579324aSTerje Bergstrom job->waitchk = num_waitchks ? mem : NULL; 706579324aSTerje Bergstrom mem += num_waitchks * sizeof(struct host1x_waitchk); 716579324aSTerje Bergstrom job->gathers = num_cmdbufs ? mem : NULL; 726579324aSTerje Bergstrom mem += num_cmdbufs * sizeof(struct host1x_job_gather); 736579324aSTerje Bergstrom job->addr_phys = num_unpins ? mem : NULL; 746579324aSTerje Bergstrom 756579324aSTerje Bergstrom job->reloc_addr_phys = job->addr_phys; 766579324aSTerje Bergstrom job->gather_addr_phys = &job->addr_phys[num_relocs]; 776579324aSTerje Bergstrom 786579324aSTerje Bergstrom return job; 796579324aSTerje Bergstrom } 80fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_alloc); 816579324aSTerje Bergstrom 826579324aSTerje Bergstrom struct host1x_job *host1x_job_get(struct host1x_job *job) 836579324aSTerje Bergstrom { 846579324aSTerje Bergstrom kref_get(&job->ref); 856579324aSTerje Bergstrom return job; 866579324aSTerje Bergstrom } 87fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_get); 886579324aSTerje Bergstrom 896579324aSTerje Bergstrom static void job_free(struct kref *ref) 906579324aSTerje Bergstrom { 916579324aSTerje Bergstrom struct host1x_job *job = container_of(ref, struct host1x_job, ref); 926579324aSTerje Bergstrom 936579324aSTerje Bergstrom kfree(job); 946579324aSTerje Bergstrom } 956579324aSTerje Bergstrom 966579324aSTerje Bergstrom void host1x_job_put(struct host1x_job *job) 976579324aSTerje Bergstrom { 986579324aSTerje Bergstrom kref_put(&job->ref, job_free); 996579324aSTerje Bergstrom } 100fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_put); 1016579324aSTerje Bergstrom 1026579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo, 1036579324aSTerje Bergstrom u32 words, u32 offset) 1046579324aSTerje Bergstrom { 1056579324aSTerje Bergstrom struct host1x_job_gather *cur_gather = &job->gathers[job->num_gathers]; 1066579324aSTerje Bergstrom 1076579324aSTerje Bergstrom cur_gather->words = words; 1086579324aSTerje Bergstrom cur_gather->bo = bo; 1096579324aSTerje Bergstrom cur_gather->offset = offset; 1106579324aSTerje Bergstrom job->num_gathers++; 1116579324aSTerje Bergstrom } 112fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_add_gather); 1136579324aSTerje Bergstrom 1146579324aSTerje Bergstrom /* 1156579324aSTerje Bergstrom * NULL an already satisfied WAIT_SYNCPT host method, by patching its 1166579324aSTerje Bergstrom * args in the command stream. The method data is changed to reference 1176579324aSTerje Bergstrom * a reserved (never given out or incr) HOST1X_SYNCPT_RESERVED syncpt 1186579324aSTerje Bergstrom * with a matching threshold value of 0, so is guaranteed to be popped 1196579324aSTerje Bergstrom * by the host HW. 1206579324aSTerje Bergstrom */ 1216579324aSTerje Bergstrom static void host1x_syncpt_patch_offset(struct host1x_syncpt *sp, 1226579324aSTerje Bergstrom struct host1x_bo *h, u32 offset) 1236579324aSTerje Bergstrom { 1246579324aSTerje Bergstrom void *patch_addr = NULL; 1256579324aSTerje Bergstrom 1266579324aSTerje Bergstrom /* patch the wait */ 1276579324aSTerje Bergstrom patch_addr = host1x_bo_kmap(h, offset >> PAGE_SHIFT); 1286579324aSTerje Bergstrom if (patch_addr) { 1296579324aSTerje Bergstrom host1x_syncpt_patch_wait(sp, 1306579324aSTerje Bergstrom patch_addr + (offset & ~PAGE_MASK)); 1316579324aSTerje Bergstrom host1x_bo_kunmap(h, offset >> PAGE_SHIFT, patch_addr); 1326579324aSTerje Bergstrom } else 1336579324aSTerje Bergstrom pr_err("Could not map cmdbuf for wait check\n"); 1346579324aSTerje Bergstrom } 1356579324aSTerje Bergstrom 1366579324aSTerje Bergstrom /* 1376579324aSTerje Bergstrom * Check driver supplied waitchk structs for syncpt thresholds 1386579324aSTerje Bergstrom * that have already been satisfied and NULL the comparison (to 1396579324aSTerje Bergstrom * avoid a wrap condition in the HW). 1406579324aSTerje Bergstrom */ 1416579324aSTerje Bergstrom static int do_waitchks(struct host1x_job *job, struct host1x *host, 14247f89c10SDmitry Osipenko struct host1x_job_gather *g) 1436579324aSTerje Bergstrom { 14447f89c10SDmitry Osipenko struct host1x_bo *patch = g->bo; 1456579324aSTerje Bergstrom int i; 1466579324aSTerje Bergstrom 1476579324aSTerje Bergstrom /* compare syncpt vs wait threshold */ 1486579324aSTerje Bergstrom for (i = 0; i < job->num_waitchk; i++) { 1496579324aSTerje Bergstrom struct host1x_waitchk *wait = &job->waitchk[i]; 1506579324aSTerje Bergstrom struct host1x_syncpt *sp = 1516579324aSTerje Bergstrom host1x_syncpt_get(host, wait->syncpt_id); 1526579324aSTerje Bergstrom 1536579324aSTerje Bergstrom /* validate syncpt id */ 1546579324aSTerje Bergstrom if (wait->syncpt_id > host1x_syncpt_nb_pts(host)) 1556579324aSTerje Bergstrom continue; 1566579324aSTerje Bergstrom 1576579324aSTerje Bergstrom /* skip all other gathers */ 1586579324aSTerje Bergstrom if (patch != wait->bo) 1596579324aSTerje Bergstrom continue; 1606579324aSTerje Bergstrom 1616579324aSTerje Bergstrom trace_host1x_syncpt_wait_check(wait->bo, wait->offset, 1626579324aSTerje Bergstrom wait->syncpt_id, wait->thresh, 1636579324aSTerje Bergstrom host1x_syncpt_read_min(sp)); 1646579324aSTerje Bergstrom 1656579324aSTerje Bergstrom if (host1x_syncpt_is_expired(sp, wait->thresh)) { 1666579324aSTerje Bergstrom dev_dbg(host->dev, 1675c0d8d38SThierry Reding "drop WAIT id %u (%s) thresh 0x%x, min 0x%x\n", 1686579324aSTerje Bergstrom wait->syncpt_id, sp->name, wait->thresh, 1696579324aSTerje Bergstrom host1x_syncpt_read_min(sp)); 1706579324aSTerje Bergstrom 17147f89c10SDmitry Osipenko host1x_syncpt_patch_offset(sp, patch, 17247f89c10SDmitry Osipenko g->offset + wait->offset); 1736579324aSTerje Bergstrom } 1746579324aSTerje Bergstrom 1756579324aSTerje Bergstrom wait->bo = NULL; 1766579324aSTerje Bergstrom } 1776579324aSTerje Bergstrom 1786579324aSTerje Bergstrom return 0; 1796579324aSTerje Bergstrom } 1806579324aSTerje Bergstrom 181404bfb78SMikko Perttunen static unsigned int pin_job(struct host1x *host, struct host1x_job *job) 1826579324aSTerje Bergstrom { 1836579324aSTerje Bergstrom unsigned int i; 184404bfb78SMikko Perttunen int err; 1856579324aSTerje Bergstrom 1866579324aSTerje Bergstrom job->num_unpins = 0; 1876579324aSTerje Bergstrom 1886579324aSTerje Bergstrom for (i = 0; i < job->num_relocs; i++) { 1896579324aSTerje Bergstrom struct host1x_reloc *reloc = &job->relocarray[i]; 1906579324aSTerje Bergstrom struct sg_table *sgt; 1916579324aSTerje Bergstrom dma_addr_t phys_addr; 1926579324aSTerje Bergstrom 193961e3beaSThierry Reding reloc->target.bo = host1x_bo_get(reloc->target.bo); 194404bfb78SMikko Perttunen if (!reloc->target.bo) { 195404bfb78SMikko Perttunen err = -EINVAL; 1966579324aSTerje Bergstrom goto unpin; 197404bfb78SMikko Perttunen } 1986579324aSTerje Bergstrom 199961e3beaSThierry Reding phys_addr = host1x_bo_pin(reloc->target.bo, &sgt); 200404bfb78SMikko Perttunen if (!phys_addr) { 201404bfb78SMikko Perttunen err = -EINVAL; 2026579324aSTerje Bergstrom goto unpin; 203404bfb78SMikko Perttunen } 2046579324aSTerje Bergstrom 2056579324aSTerje Bergstrom job->addr_phys[job->num_unpins] = phys_addr; 206961e3beaSThierry Reding job->unpins[job->num_unpins].bo = reloc->target.bo; 2076579324aSTerje Bergstrom job->unpins[job->num_unpins].sgt = sgt; 2086579324aSTerje Bergstrom job->num_unpins++; 2096579324aSTerje Bergstrom } 2106579324aSTerje Bergstrom 2116579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 2126579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 213404bfb78SMikko Perttunen size_t gather_size = 0; 214404bfb78SMikko Perttunen struct scatterlist *sg; 2156579324aSTerje Bergstrom struct sg_table *sgt; 2166579324aSTerje Bergstrom dma_addr_t phys_addr; 217404bfb78SMikko Perttunen unsigned long shift; 218404bfb78SMikko Perttunen struct iova *alloc; 219404bfb78SMikko Perttunen unsigned int j; 2206579324aSTerje Bergstrom 2216579324aSTerje Bergstrom g->bo = host1x_bo_get(g->bo); 222404bfb78SMikko Perttunen if (!g->bo) { 223404bfb78SMikko Perttunen err = -EINVAL; 2246579324aSTerje Bergstrom goto unpin; 225404bfb78SMikko Perttunen } 2266579324aSTerje Bergstrom 2276579324aSTerje Bergstrom phys_addr = host1x_bo_pin(g->bo, &sgt); 228404bfb78SMikko Perttunen if (!phys_addr) { 229404bfb78SMikko Perttunen err = -EINVAL; 2306579324aSTerje Bergstrom goto unpin; 231404bfb78SMikko Perttunen } 2326579324aSTerje Bergstrom 233404bfb78SMikko Perttunen if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) { 234404bfb78SMikko Perttunen for_each_sg(sgt->sgl, sg, sgt->nents, j) 235404bfb78SMikko Perttunen gather_size += sg->length; 236404bfb78SMikko Perttunen gather_size = iova_align(&host->iova, gather_size); 237404bfb78SMikko Perttunen 238404bfb78SMikko Perttunen shift = iova_shift(&host->iova); 239404bfb78SMikko Perttunen alloc = alloc_iova(&host->iova, gather_size >> shift, 240404bfb78SMikko Perttunen host->iova_end >> shift, true); 241404bfb78SMikko Perttunen if (!alloc) { 242404bfb78SMikko Perttunen err = -ENOMEM; 243404bfb78SMikko Perttunen goto unpin; 244404bfb78SMikko Perttunen } 245404bfb78SMikko Perttunen 246404bfb78SMikko Perttunen err = iommu_map_sg(host->domain, 247404bfb78SMikko Perttunen iova_dma_addr(&host->iova, alloc), 248404bfb78SMikko Perttunen sgt->sgl, sgt->nents, IOMMU_READ); 249404bfb78SMikko Perttunen if (err == 0) { 250404bfb78SMikko Perttunen __free_iova(&host->iova, alloc); 251404bfb78SMikko Perttunen err = -EINVAL; 252404bfb78SMikko Perttunen goto unpin; 253404bfb78SMikko Perttunen } 254404bfb78SMikko Perttunen 255404bfb78SMikko Perttunen job->addr_phys[job->num_unpins] = 256404bfb78SMikko Perttunen iova_dma_addr(&host->iova, alloc); 257404bfb78SMikko Perttunen job->unpins[job->num_unpins].size = gather_size; 258404bfb78SMikko Perttunen } else { 2596579324aSTerje Bergstrom job->addr_phys[job->num_unpins] = phys_addr; 260404bfb78SMikko Perttunen } 261404bfb78SMikko Perttunen 262404bfb78SMikko Perttunen job->gather_addr_phys[i] = job->addr_phys[job->num_unpins]; 263404bfb78SMikko Perttunen 2646579324aSTerje Bergstrom job->unpins[job->num_unpins].bo = g->bo; 2656579324aSTerje Bergstrom job->unpins[job->num_unpins].sgt = sgt; 2666579324aSTerje Bergstrom job->num_unpins++; 2676579324aSTerje Bergstrom } 2686579324aSTerje Bergstrom 269404bfb78SMikko Perttunen return 0; 2706579324aSTerje Bergstrom 2716579324aSTerje Bergstrom unpin: 2726579324aSTerje Bergstrom host1x_job_unpin(job); 273404bfb78SMikko Perttunen return err; 2746579324aSTerje Bergstrom } 2756579324aSTerje Bergstrom 27647f89c10SDmitry Osipenko static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g) 2776579324aSTerje Bergstrom { 2786579324aSTerje Bergstrom int i = 0; 2796579324aSTerje Bergstrom u32 last_page = ~0; 2806579324aSTerje Bergstrom void *cmdbuf_page_addr = NULL; 28147f89c10SDmitry Osipenko struct host1x_bo *cmdbuf = g->bo; 2826579324aSTerje Bergstrom 2836579324aSTerje Bergstrom /* pin & patch the relocs for one gather */ 2843364cd28SArto Merilainen for (i = 0; i < job->num_relocs; i++) { 2856579324aSTerje Bergstrom struct host1x_reloc *reloc = &job->relocarray[i]; 2866579324aSTerje Bergstrom u32 reloc_addr = (job->reloc_addr_phys[i] + 287961e3beaSThierry Reding reloc->target.offset) >> reloc->shift; 2886579324aSTerje Bergstrom u32 *target; 2896579324aSTerje Bergstrom 2906579324aSTerje Bergstrom /* skip all other gathers */ 291961e3beaSThierry Reding if (cmdbuf != reloc->cmdbuf.bo) 2926579324aSTerje Bergstrom continue; 2936579324aSTerje Bergstrom 29447f89c10SDmitry Osipenko if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) { 29547f89c10SDmitry Osipenko target = (u32 *)job->gather_copy_mapped + 29647f89c10SDmitry Osipenko reloc->cmdbuf.offset / sizeof(u32) + 29747f89c10SDmitry Osipenko g->offset / sizeof(u32); 29847f89c10SDmitry Osipenko goto patch_reloc; 29947f89c10SDmitry Osipenko } 30047f89c10SDmitry Osipenko 301961e3beaSThierry Reding if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) { 3026579324aSTerje Bergstrom if (cmdbuf_page_addr) 3036579324aSTerje Bergstrom host1x_bo_kunmap(cmdbuf, last_page, 3046579324aSTerje Bergstrom cmdbuf_page_addr); 3056579324aSTerje Bergstrom 3066579324aSTerje Bergstrom cmdbuf_page_addr = host1x_bo_kmap(cmdbuf, 307961e3beaSThierry Reding reloc->cmdbuf.offset >> PAGE_SHIFT); 308961e3beaSThierry Reding last_page = reloc->cmdbuf.offset >> PAGE_SHIFT; 3096579324aSTerje Bergstrom 3106579324aSTerje Bergstrom if (unlikely(!cmdbuf_page_addr)) { 3116579324aSTerje Bergstrom pr_err("Could not map cmdbuf for relocation\n"); 3126579324aSTerje Bergstrom return -ENOMEM; 3136579324aSTerje Bergstrom } 3146579324aSTerje Bergstrom } 3156579324aSTerje Bergstrom 316961e3beaSThierry Reding target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK); 31747f89c10SDmitry Osipenko patch_reloc: 3186579324aSTerje Bergstrom *target = reloc_addr; 3196579324aSTerje Bergstrom } 3206579324aSTerje Bergstrom 3216579324aSTerje Bergstrom if (cmdbuf_page_addr) 3226579324aSTerje Bergstrom host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr); 3236579324aSTerje Bergstrom 3246579324aSTerje Bergstrom return 0; 3256579324aSTerje Bergstrom } 3266579324aSTerje Bergstrom 3275060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf, 3286579324aSTerje Bergstrom unsigned int offset) 3296579324aSTerje Bergstrom { 3306579324aSTerje Bergstrom offset *= sizeof(u32); 3316579324aSTerje Bergstrom 332961e3beaSThierry Reding if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset) 3335060d8ecSArto Merilainen return false; 3346579324aSTerje Bergstrom 335571cbf70SDmitry Osipenko /* relocation shift value validation isn't implemented yet */ 336571cbf70SDmitry Osipenko if (reloc->shift) 337571cbf70SDmitry Osipenko return false; 338571cbf70SDmitry Osipenko 3395060d8ecSArto Merilainen return true; 3406579324aSTerje Bergstrom } 3416579324aSTerje Bergstrom 342a47ac10eSDmitry Osipenko static bool check_wait(struct host1x_waitchk *wait, struct host1x_bo *cmdbuf, 343a47ac10eSDmitry Osipenko unsigned int offset) 344a47ac10eSDmitry Osipenko { 345a47ac10eSDmitry Osipenko offset *= sizeof(u32); 346a47ac10eSDmitry Osipenko 347a47ac10eSDmitry Osipenko if (wait->bo != cmdbuf || wait->offset != offset) 348a47ac10eSDmitry Osipenko return false; 349a47ac10eSDmitry Osipenko 350a47ac10eSDmitry Osipenko return true; 351a47ac10eSDmitry Osipenko } 352a47ac10eSDmitry Osipenko 3536579324aSTerje Bergstrom struct host1x_firewall { 3546579324aSTerje Bergstrom struct host1x_job *job; 3556579324aSTerje Bergstrom struct device *dev; 3566579324aSTerje Bergstrom 3576579324aSTerje Bergstrom unsigned int num_relocs; 3586579324aSTerje Bergstrom struct host1x_reloc *reloc; 3596579324aSTerje Bergstrom 360a47ac10eSDmitry Osipenko unsigned int num_waitchks; 361a47ac10eSDmitry Osipenko struct host1x_waitchk *waitchk; 362a47ac10eSDmitry Osipenko 363d7fbcf47SThierry Reding struct host1x_bo *cmdbuf; 3646579324aSTerje Bergstrom unsigned int offset; 3656579324aSTerje Bergstrom 3666579324aSTerje Bergstrom u32 words; 3676579324aSTerje Bergstrom u32 class; 3686579324aSTerje Bergstrom u32 reg; 3696579324aSTerje Bergstrom u32 mask; 3706579324aSTerje Bergstrom u32 count; 3716579324aSTerje Bergstrom }; 3726579324aSTerje Bergstrom 373d77563ffSThierry Reding static int check_register(struct host1x_firewall *fw, unsigned long offset) 374d77563ffSThierry Reding { 3750f563a4bSDmitry Osipenko if (!fw->job->is_addr_reg) 3760f563a4bSDmitry Osipenko return 0; 3770f563a4bSDmitry Osipenko 378d77563ffSThierry Reding if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) { 379d77563ffSThierry Reding if (!fw->num_relocs) 380d77563ffSThierry Reding return -EINVAL; 381d77563ffSThierry Reding 382d77563ffSThierry Reding if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset)) 383d77563ffSThierry Reding return -EINVAL; 384d77563ffSThierry Reding 385d77563ffSThierry Reding fw->num_relocs--; 386d77563ffSThierry Reding fw->reloc++; 387d77563ffSThierry Reding } 388d77563ffSThierry Reding 389a47ac10eSDmitry Osipenko if (offset == HOST1X_WAIT_SYNCPT_OFFSET) { 390a47ac10eSDmitry Osipenko if (fw->class != HOST1X_CLASS_HOST1X) 391a47ac10eSDmitry Osipenko return -EINVAL; 392a47ac10eSDmitry Osipenko 393a47ac10eSDmitry Osipenko if (!fw->num_waitchks) 394a47ac10eSDmitry Osipenko return -EINVAL; 395a47ac10eSDmitry Osipenko 396a47ac10eSDmitry Osipenko if (!check_wait(fw->waitchk, fw->cmdbuf, fw->offset)) 397a47ac10eSDmitry Osipenko return -EINVAL; 398a47ac10eSDmitry Osipenko 399a47ac10eSDmitry Osipenko fw->num_waitchks--; 400a47ac10eSDmitry Osipenko fw->waitchk++; 401a47ac10eSDmitry Osipenko } 402a47ac10eSDmitry Osipenko 403d77563ffSThierry Reding return 0; 404d77563ffSThierry Reding } 405d77563ffSThierry Reding 4060f563a4bSDmitry Osipenko static int check_class(struct host1x_firewall *fw, u32 class) 4070f563a4bSDmitry Osipenko { 4080f563a4bSDmitry Osipenko if (!fw->job->is_valid_class) { 4090f563a4bSDmitry Osipenko if (fw->class != class) 4100f563a4bSDmitry Osipenko return -EINVAL; 4110f563a4bSDmitry Osipenko } else { 4120f563a4bSDmitry Osipenko if (!fw->job->is_valid_class(fw->class)) 4130f563a4bSDmitry Osipenko return -EINVAL; 4140f563a4bSDmitry Osipenko } 4150f563a4bSDmitry Osipenko 4160f563a4bSDmitry Osipenko return 0; 4170f563a4bSDmitry Osipenko } 4180f563a4bSDmitry Osipenko 4196579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw) 4206579324aSTerje Bergstrom { 4216579324aSTerje Bergstrom u32 mask = fw->mask; 4226579324aSTerje Bergstrom u32 reg = fw->reg; 423d77563ffSThierry Reding int ret; 4246579324aSTerje Bergstrom 4256579324aSTerje Bergstrom while (mask) { 4266579324aSTerje Bergstrom if (fw->words == 0) 4276579324aSTerje Bergstrom return -EINVAL; 4286579324aSTerje Bergstrom 4296579324aSTerje Bergstrom if (mask & 1) { 430d77563ffSThierry Reding ret = check_register(fw, reg); 431d77563ffSThierry Reding if (ret < 0) 432d77563ffSThierry Reding return ret; 433d77563ffSThierry Reding 4346579324aSTerje Bergstrom fw->words--; 4356579324aSTerje Bergstrom fw->offset++; 4366579324aSTerje Bergstrom } 4376579324aSTerje Bergstrom mask >>= 1; 4386579324aSTerje Bergstrom reg++; 4396579324aSTerje Bergstrom } 4406579324aSTerje Bergstrom 4416579324aSTerje Bergstrom return 0; 4426579324aSTerje Bergstrom } 4436579324aSTerje Bergstrom 4446579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw) 4456579324aSTerje Bergstrom { 4466579324aSTerje Bergstrom u32 count = fw->count; 4476579324aSTerje Bergstrom u32 reg = fw->reg; 448d77563ffSThierry Reding int ret; 4496579324aSTerje Bergstrom 45064c173d3STerje Bergstrom while (count) { 4516579324aSTerje Bergstrom if (fw->words == 0) 4526579324aSTerje Bergstrom return -EINVAL; 4536579324aSTerje Bergstrom 454d77563ffSThierry Reding ret = check_register(fw, reg); 455d77563ffSThierry Reding if (ret < 0) 456d77563ffSThierry Reding return ret; 457d77563ffSThierry Reding 4586579324aSTerje Bergstrom reg++; 4596579324aSTerje Bergstrom fw->words--; 4606579324aSTerje Bergstrom fw->offset++; 4616579324aSTerje Bergstrom count--; 4626579324aSTerje Bergstrom } 4636579324aSTerje Bergstrom 4646579324aSTerje Bergstrom return 0; 4656579324aSTerje Bergstrom } 4666579324aSTerje Bergstrom 4676579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw) 4686579324aSTerje Bergstrom { 4696579324aSTerje Bergstrom u32 count = fw->count; 470d77563ffSThierry Reding int ret; 4716579324aSTerje Bergstrom 4726579324aSTerje Bergstrom while (count) { 4736579324aSTerje Bergstrom if (fw->words == 0) 4746579324aSTerje Bergstrom return -EINVAL; 4756579324aSTerje Bergstrom 476d77563ffSThierry Reding ret = check_register(fw, fw->reg); 477d77563ffSThierry Reding if (ret < 0) 478d77563ffSThierry Reding return ret; 479d77563ffSThierry Reding 4806579324aSTerje Bergstrom fw->words--; 4816579324aSTerje Bergstrom fw->offset++; 4826579324aSTerje Bergstrom count--; 4836579324aSTerje Bergstrom } 4846579324aSTerje Bergstrom 4856579324aSTerje Bergstrom return 0; 4866579324aSTerje Bergstrom } 4876579324aSTerje Bergstrom 488afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g) 4896579324aSTerje Bergstrom { 4903364cd28SArto Merilainen u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped + 4913364cd28SArto Merilainen (g->offset / sizeof(u32)); 4920f563a4bSDmitry Osipenko u32 job_class = fw->class; 4936579324aSTerje Bergstrom int err = 0; 4946579324aSTerje Bergstrom 495afac0e43STerje Bergstrom fw->words = g->words; 496d7fbcf47SThierry Reding fw->cmdbuf = g->bo; 497afac0e43STerje Bergstrom fw->offset = 0; 4986579324aSTerje Bergstrom 499afac0e43STerje Bergstrom while (fw->words && !err) { 500afac0e43STerje Bergstrom u32 word = cmdbuf_base[fw->offset]; 5016579324aSTerje Bergstrom u32 opcode = (word & 0xf0000000) >> 28; 5026579324aSTerje Bergstrom 503afac0e43STerje Bergstrom fw->mask = 0; 504afac0e43STerje Bergstrom fw->reg = 0; 505afac0e43STerje Bergstrom fw->count = 0; 506afac0e43STerje Bergstrom fw->words--; 507afac0e43STerje Bergstrom fw->offset++; 5086579324aSTerje Bergstrom 5096579324aSTerje Bergstrom switch (opcode) { 5106579324aSTerje Bergstrom case 0: 511afac0e43STerje Bergstrom fw->class = word >> 6 & 0x3ff; 512afac0e43STerje Bergstrom fw->mask = word & 0x3f; 513afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 5140f563a4bSDmitry Osipenko err = check_class(fw, job_class); 5150f563a4bSDmitry Osipenko if (!err) 516afac0e43STerje Bergstrom err = check_mask(fw); 5176579324aSTerje Bergstrom if (err) 5186579324aSTerje Bergstrom goto out; 5196579324aSTerje Bergstrom break; 5206579324aSTerje Bergstrom case 1: 521afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 522afac0e43STerje Bergstrom fw->count = word & 0xffff; 523afac0e43STerje Bergstrom err = check_incr(fw); 5246579324aSTerje Bergstrom if (err) 5256579324aSTerje Bergstrom goto out; 5266579324aSTerje Bergstrom break; 5276579324aSTerje Bergstrom 5286579324aSTerje Bergstrom case 2: 529afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 530afac0e43STerje Bergstrom fw->count = word & 0xffff; 531afac0e43STerje Bergstrom err = check_nonincr(fw); 5326579324aSTerje Bergstrom if (err) 5336579324aSTerje Bergstrom goto out; 5346579324aSTerje Bergstrom break; 5356579324aSTerje Bergstrom 5366579324aSTerje Bergstrom case 3: 537afac0e43STerje Bergstrom fw->mask = word & 0xffff; 538afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 539afac0e43STerje Bergstrom err = check_mask(fw); 5406579324aSTerje Bergstrom if (err) 5416579324aSTerje Bergstrom goto out; 5426579324aSTerje Bergstrom break; 5436579324aSTerje Bergstrom case 4: 5446579324aSTerje Bergstrom case 14: 5456579324aSTerje Bergstrom break; 5466579324aSTerje Bergstrom default: 5476579324aSTerje Bergstrom err = -EINVAL; 5486579324aSTerje Bergstrom break; 5496579324aSTerje Bergstrom } 5506579324aSTerje Bergstrom } 5516579324aSTerje Bergstrom 5526579324aSTerje Bergstrom out: 5536579324aSTerje Bergstrom return err; 5546579324aSTerje Bergstrom } 5556579324aSTerje Bergstrom 5566579324aSTerje Bergstrom static inline int copy_gathers(struct host1x_job *job, struct device *dev) 5576579324aSTerje Bergstrom { 5583364cd28SArto Merilainen struct host1x_firewall fw; 5596579324aSTerje Bergstrom size_t size = 0; 5606579324aSTerje Bergstrom size_t offset = 0; 5616579324aSTerje Bergstrom int i; 5626579324aSTerje Bergstrom 5633364cd28SArto Merilainen fw.job = job; 5643364cd28SArto Merilainen fw.dev = dev; 5653364cd28SArto Merilainen fw.reloc = job->relocarray; 5663364cd28SArto Merilainen fw.num_relocs = job->num_relocs; 567a47ac10eSDmitry Osipenko fw.waitchk = job->waitchk; 568a47ac10eSDmitry Osipenko fw.num_waitchks = job->num_waitchk; 5693833d16fSDmitry Osipenko fw.class = job->class; 5703364cd28SArto Merilainen 5716579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 5726579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 5736df633d0SThierry Reding 5746579324aSTerje Bergstrom size += g->words * sizeof(u32); 5756579324aSTerje Bergstrom } 5766579324aSTerje Bergstrom 577*43240bbdSDmitry Osipenko /* 578*43240bbdSDmitry Osipenko * Try a non-blocking allocation from a higher priority pools first, 579*43240bbdSDmitry Osipenko * as awaiting for the allocation here is a major performance hit. 580*43240bbdSDmitry Osipenko */ 581f6e45661SLuis R. Rodriguez job->gather_copy_mapped = dma_alloc_wc(dev, size, &job->gather_copy, 582*43240bbdSDmitry Osipenko GFP_NOWAIT); 583*43240bbdSDmitry Osipenko 584*43240bbdSDmitry Osipenko /* the higher priority allocation failed, try the generic-blocking */ 585*43240bbdSDmitry Osipenko if (!job->gather_copy_mapped) 586*43240bbdSDmitry Osipenko job->gather_copy_mapped = dma_alloc_wc(dev, size, 587*43240bbdSDmitry Osipenko &job->gather_copy, 5886579324aSTerje Bergstrom GFP_KERNEL); 589*43240bbdSDmitry Osipenko if (!job->gather_copy_mapped) 590745cecc0SDan Carpenter return -ENOMEM; 5916579324aSTerje Bergstrom 5926579324aSTerje Bergstrom job->gather_copy_size = size; 5936579324aSTerje Bergstrom 5946579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 5956579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 5966579324aSTerje Bergstrom void *gather; 5976579324aSTerje Bergstrom 5983364cd28SArto Merilainen /* Copy the gather */ 5996579324aSTerje Bergstrom gather = host1x_bo_mmap(g->bo); 6006579324aSTerje Bergstrom memcpy(job->gather_copy_mapped + offset, gather + g->offset, 6016579324aSTerje Bergstrom g->words * sizeof(u32)); 6026579324aSTerje Bergstrom host1x_bo_munmap(g->bo, gather); 6036579324aSTerje Bergstrom 6043364cd28SArto Merilainen /* Store the location in the buffer */ 6056579324aSTerje Bergstrom g->base = job->gather_copy; 6066579324aSTerje Bergstrom g->offset = offset; 6073364cd28SArto Merilainen 6083364cd28SArto Merilainen /* Validate the job */ 6093364cd28SArto Merilainen if (validate(&fw, g)) 6103364cd28SArto Merilainen return -EINVAL; 6116579324aSTerje Bergstrom 6126579324aSTerje Bergstrom offset += g->words * sizeof(u32); 6136579324aSTerje Bergstrom } 6146579324aSTerje Bergstrom 615a47ac10eSDmitry Osipenko /* No relocs and waitchks should remain at this point */ 616a47ac10eSDmitry Osipenko if (fw.num_relocs || fw.num_waitchks) 617a9ff9995SErik Faye-Lund return -EINVAL; 618a9ff9995SErik Faye-Lund 6196579324aSTerje Bergstrom return 0; 6206579324aSTerje Bergstrom } 6216579324aSTerje Bergstrom 6226579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev) 6236579324aSTerje Bergstrom { 6246579324aSTerje Bergstrom int err; 6256579324aSTerje Bergstrom unsigned int i, j; 6266579324aSTerje Bergstrom struct host1x *host = dev_get_drvdata(dev->parent); 6276579324aSTerje Bergstrom DECLARE_BITMAP(waitchk_mask, host1x_syncpt_nb_pts(host)); 6286579324aSTerje Bergstrom 6296579324aSTerje Bergstrom bitmap_zero(waitchk_mask, host1x_syncpt_nb_pts(host)); 6306579324aSTerje Bergstrom for (i = 0; i < job->num_waitchk; i++) { 6316579324aSTerje Bergstrom u32 syncpt_id = job->waitchk[i].syncpt_id; 6326df633d0SThierry Reding 6336579324aSTerje Bergstrom if (syncpt_id < host1x_syncpt_nb_pts(host)) 6346579324aSTerje Bergstrom set_bit(syncpt_id, waitchk_mask); 6356579324aSTerje Bergstrom } 6366579324aSTerje Bergstrom 6376579324aSTerje Bergstrom /* get current syncpt values for waitchk */ 6386579324aSTerje Bergstrom for_each_set_bit(i, waitchk_mask, host1x_syncpt_nb_pts(host)) 6396579324aSTerje Bergstrom host1x_syncpt_load(host->syncpt + i); 6406579324aSTerje Bergstrom 6416579324aSTerje Bergstrom /* pin memory */ 642404bfb78SMikko Perttunen err = pin_job(host, job); 643404bfb78SMikko Perttunen if (err) 6446579324aSTerje Bergstrom goto out; 6456579324aSTerje Bergstrom 64647f89c10SDmitry Osipenko if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) { 64747f89c10SDmitry Osipenko err = copy_gathers(job, dev); 64847f89c10SDmitry Osipenko if (err) 64947f89c10SDmitry Osipenko goto out; 65047f89c10SDmitry Osipenko } 65147f89c10SDmitry Osipenko 6526579324aSTerje Bergstrom /* patch gathers */ 6536579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 6546579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 6556579324aSTerje Bergstrom 6566579324aSTerje Bergstrom /* process each gather mem only once */ 6576579324aSTerje Bergstrom if (g->handled) 6586579324aSTerje Bergstrom continue; 6596579324aSTerje Bergstrom 66047f89c10SDmitry Osipenko /* copy_gathers() sets gathers base if firewall is enabled */ 66147f89c10SDmitry Osipenko if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) 6626579324aSTerje Bergstrom g->base = job->gather_addr_phys[i]; 6636579324aSTerje Bergstrom 664f08ef2d1SArto Merilainen for (j = i + 1; j < job->num_gathers; j++) { 665f08ef2d1SArto Merilainen if (job->gathers[j].bo == g->bo) { 6666579324aSTerje Bergstrom job->gathers[j].handled = true; 667f08ef2d1SArto Merilainen job->gathers[j].base = g->base; 668f08ef2d1SArto Merilainen } 669f08ef2d1SArto Merilainen } 6706579324aSTerje Bergstrom 67147f89c10SDmitry Osipenko err = do_relocs(job, g); 6723364cd28SArto Merilainen if (err) 67347f89c10SDmitry Osipenko break; 6746579324aSTerje Bergstrom 67547f89c10SDmitry Osipenko err = do_waitchks(job, host, g); 6766579324aSTerje Bergstrom if (err) 67747f89c10SDmitry Osipenko break; 6786579324aSTerje Bergstrom } 6796579324aSTerje Bergstrom 6806579324aSTerje Bergstrom out: 681e5855aa3SDmitry Osipenko if (err) 682e5855aa3SDmitry Osipenko host1x_job_unpin(job); 6836579324aSTerje Bergstrom wmb(); 6846579324aSTerje Bergstrom 6856579324aSTerje Bergstrom return err; 6866579324aSTerje Bergstrom } 687fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_pin); 6886579324aSTerje Bergstrom 6896579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job) 6906579324aSTerje Bergstrom { 691404bfb78SMikko Perttunen struct host1x *host = dev_get_drvdata(job->channel->dev->parent); 6926579324aSTerje Bergstrom unsigned int i; 6936579324aSTerje Bergstrom 6946579324aSTerje Bergstrom for (i = 0; i < job->num_unpins; i++) { 6956579324aSTerje Bergstrom struct host1x_job_unpin_data *unpin = &job->unpins[i]; 6966df633d0SThierry Reding 697404bfb78SMikko Perttunen if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) { 698404bfb78SMikko Perttunen iommu_unmap(host->domain, job->addr_phys[i], 699404bfb78SMikko Perttunen unpin->size); 700404bfb78SMikko Perttunen free_iova(&host->iova, 701404bfb78SMikko Perttunen iova_pfn(&host->iova, job->addr_phys[i])); 702404bfb78SMikko Perttunen } 703404bfb78SMikko Perttunen 7046579324aSTerje Bergstrom host1x_bo_unpin(unpin->bo, unpin->sgt); 7056579324aSTerje Bergstrom host1x_bo_put(unpin->bo); 7066579324aSTerje Bergstrom } 7070b8070d1SThierry Reding 7086579324aSTerje Bergstrom job->num_unpins = 0; 7096579324aSTerje Bergstrom 7106579324aSTerje Bergstrom if (job->gather_copy_size) 711f6e45661SLuis R. Rodriguez dma_free_wc(job->channel->dev, job->gather_copy_size, 712f6e45661SLuis R. Rodriguez job->gather_copy_mapped, job->gather_copy); 7136579324aSTerje Bergstrom } 714fae798a1SThierry Reding EXPORT_SYMBOL(host1x_job_unpin); 7156579324aSTerje Bergstrom 7166579324aSTerje Bergstrom /* 7176579324aSTerje Bergstrom * Debug routine used to dump job entries 7186579324aSTerje Bergstrom */ 7196579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job) 7206579324aSTerje Bergstrom { 7216579324aSTerje Bergstrom dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id); 7226579324aSTerje Bergstrom dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end); 7236579324aSTerje Bergstrom dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get); 7246579324aSTerje Bergstrom dev_dbg(dev, " TIMEOUT %d\n", job->timeout); 7256579324aSTerje Bergstrom dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots); 7266579324aSTerje Bergstrom dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins); 7276579324aSTerje Bergstrom } 728