16579324aSTerje Bergstrom /* 26579324aSTerje Bergstrom * Tegra host1x Job 36579324aSTerje Bergstrom * 46579324aSTerje Bergstrom * Copyright (c) 2010-2013, 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> 216579324aSTerje Bergstrom #include <linux/kref.h> 226579324aSTerje Bergstrom #include <linux/module.h> 236579324aSTerje Bergstrom #include <linux/scatterlist.h> 246579324aSTerje Bergstrom #include <linux/slab.h> 256579324aSTerje Bergstrom #include <linux/vmalloc.h> 266579324aSTerje Bergstrom #include <trace/events/host1x.h> 276579324aSTerje Bergstrom 286579324aSTerje Bergstrom #include "channel.h" 296579324aSTerje Bergstrom #include "dev.h" 306579324aSTerje Bergstrom #include "host1x_bo.h" 316579324aSTerje Bergstrom #include "job.h" 326579324aSTerje Bergstrom #include "syncpt.h" 336579324aSTerje Bergstrom 346579324aSTerje Bergstrom struct host1x_job *host1x_job_alloc(struct host1x_channel *ch, 356579324aSTerje Bergstrom u32 num_cmdbufs, u32 num_relocs, 366579324aSTerje Bergstrom u32 num_waitchks) 376579324aSTerje Bergstrom { 386579324aSTerje Bergstrom struct host1x_job *job = NULL; 396579324aSTerje Bergstrom unsigned int num_unpins = num_cmdbufs + num_relocs; 406579324aSTerje Bergstrom u64 total; 416579324aSTerje Bergstrom void *mem; 426579324aSTerje Bergstrom 436579324aSTerje Bergstrom /* Check that we're not going to overflow */ 446579324aSTerje Bergstrom total = sizeof(struct host1x_job) + 456579324aSTerje Bergstrom num_relocs * sizeof(struct host1x_reloc) + 466579324aSTerje Bergstrom num_unpins * sizeof(struct host1x_job_unpin_data) + 476579324aSTerje Bergstrom num_waitchks * sizeof(struct host1x_waitchk) + 486579324aSTerje Bergstrom num_cmdbufs * sizeof(struct host1x_job_gather) + 496579324aSTerje Bergstrom num_unpins * sizeof(dma_addr_t) + 506579324aSTerje Bergstrom num_unpins * sizeof(u32 *); 516579324aSTerje Bergstrom if (total > ULONG_MAX) 526579324aSTerje Bergstrom return NULL; 536579324aSTerje Bergstrom 546579324aSTerje Bergstrom mem = job = kzalloc(total, GFP_KERNEL); 556579324aSTerje Bergstrom if (!job) 566579324aSTerje Bergstrom return NULL; 576579324aSTerje Bergstrom 586579324aSTerje Bergstrom kref_init(&job->ref); 596579324aSTerje Bergstrom job->channel = ch; 606579324aSTerje Bergstrom 616579324aSTerje Bergstrom /* Redistribute memory to the structs */ 626579324aSTerje Bergstrom mem += sizeof(struct host1x_job); 636579324aSTerje Bergstrom job->relocarray = num_relocs ? mem : NULL; 646579324aSTerje Bergstrom mem += num_relocs * sizeof(struct host1x_reloc); 656579324aSTerje Bergstrom job->unpins = num_unpins ? mem : NULL; 666579324aSTerje Bergstrom mem += num_unpins * sizeof(struct host1x_job_unpin_data); 676579324aSTerje Bergstrom job->waitchk = num_waitchks ? mem : NULL; 686579324aSTerje Bergstrom mem += num_waitchks * sizeof(struct host1x_waitchk); 696579324aSTerje Bergstrom job->gathers = num_cmdbufs ? mem : NULL; 706579324aSTerje Bergstrom mem += num_cmdbufs * sizeof(struct host1x_job_gather); 716579324aSTerje Bergstrom job->addr_phys = num_unpins ? mem : NULL; 726579324aSTerje Bergstrom 736579324aSTerje Bergstrom job->reloc_addr_phys = job->addr_phys; 746579324aSTerje Bergstrom job->gather_addr_phys = &job->addr_phys[num_relocs]; 756579324aSTerje Bergstrom 766579324aSTerje Bergstrom return job; 776579324aSTerje Bergstrom } 786579324aSTerje Bergstrom 796579324aSTerje Bergstrom struct host1x_job *host1x_job_get(struct host1x_job *job) 806579324aSTerje Bergstrom { 816579324aSTerje Bergstrom kref_get(&job->ref); 826579324aSTerje Bergstrom return job; 836579324aSTerje Bergstrom } 846579324aSTerje Bergstrom 856579324aSTerje Bergstrom static void job_free(struct kref *ref) 866579324aSTerje Bergstrom { 876579324aSTerje Bergstrom struct host1x_job *job = container_of(ref, struct host1x_job, ref); 886579324aSTerje Bergstrom 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 } 966579324aSTerje Bergstrom 976579324aSTerje Bergstrom void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo, 986579324aSTerje Bergstrom u32 words, u32 offset) 996579324aSTerje Bergstrom { 1006579324aSTerje Bergstrom struct host1x_job_gather *cur_gather = &job->gathers[job->num_gathers]; 1016579324aSTerje Bergstrom 1026579324aSTerje Bergstrom cur_gather->words = words; 1036579324aSTerje Bergstrom cur_gather->bo = bo; 1046579324aSTerje Bergstrom cur_gather->offset = offset; 1056579324aSTerje Bergstrom job->num_gathers++; 1066579324aSTerje Bergstrom } 1076579324aSTerje Bergstrom 1086579324aSTerje Bergstrom /* 1096579324aSTerje Bergstrom * NULL an already satisfied WAIT_SYNCPT host method, by patching its 1106579324aSTerje Bergstrom * args in the command stream. The method data is changed to reference 1116579324aSTerje Bergstrom * a reserved (never given out or incr) HOST1X_SYNCPT_RESERVED syncpt 1126579324aSTerje Bergstrom * with a matching threshold value of 0, so is guaranteed to be popped 1136579324aSTerje Bergstrom * by the host HW. 1146579324aSTerje Bergstrom */ 1156579324aSTerje Bergstrom static void host1x_syncpt_patch_offset(struct host1x_syncpt *sp, 1166579324aSTerje Bergstrom struct host1x_bo *h, u32 offset) 1176579324aSTerje Bergstrom { 1186579324aSTerje Bergstrom void *patch_addr = NULL; 1196579324aSTerje Bergstrom 1206579324aSTerje Bergstrom /* patch the wait */ 1216579324aSTerje Bergstrom patch_addr = host1x_bo_kmap(h, offset >> PAGE_SHIFT); 1226579324aSTerje Bergstrom if (patch_addr) { 1236579324aSTerje Bergstrom host1x_syncpt_patch_wait(sp, 1246579324aSTerje Bergstrom patch_addr + (offset & ~PAGE_MASK)); 1256579324aSTerje Bergstrom host1x_bo_kunmap(h, offset >> PAGE_SHIFT, patch_addr); 1266579324aSTerje Bergstrom } else 1276579324aSTerje Bergstrom pr_err("Could not map cmdbuf for wait check\n"); 1286579324aSTerje Bergstrom } 1296579324aSTerje Bergstrom 1306579324aSTerje Bergstrom /* 1316579324aSTerje Bergstrom * Check driver supplied waitchk structs for syncpt thresholds 1326579324aSTerje Bergstrom * that have already been satisfied and NULL the comparison (to 1336579324aSTerje Bergstrom * avoid a wrap condition in the HW). 1346579324aSTerje Bergstrom */ 1356579324aSTerje Bergstrom static int do_waitchks(struct host1x_job *job, struct host1x *host, 1366579324aSTerje Bergstrom struct host1x_bo *patch) 1376579324aSTerje Bergstrom { 1386579324aSTerje Bergstrom int i; 1396579324aSTerje Bergstrom 1406579324aSTerje Bergstrom /* compare syncpt vs wait threshold */ 1416579324aSTerje Bergstrom for (i = 0; i < job->num_waitchk; i++) { 1426579324aSTerje Bergstrom struct host1x_waitchk *wait = &job->waitchk[i]; 1436579324aSTerje Bergstrom struct host1x_syncpt *sp = 1446579324aSTerje Bergstrom host1x_syncpt_get(host, wait->syncpt_id); 1456579324aSTerje Bergstrom 1466579324aSTerje Bergstrom /* validate syncpt id */ 1476579324aSTerje Bergstrom if (wait->syncpt_id > host1x_syncpt_nb_pts(host)) 1486579324aSTerje Bergstrom continue; 1496579324aSTerje Bergstrom 1506579324aSTerje Bergstrom /* skip all other gathers */ 1516579324aSTerje Bergstrom if (patch != wait->bo) 1526579324aSTerje Bergstrom continue; 1536579324aSTerje Bergstrom 1546579324aSTerje Bergstrom trace_host1x_syncpt_wait_check(wait->bo, wait->offset, 1556579324aSTerje Bergstrom wait->syncpt_id, wait->thresh, 1566579324aSTerje Bergstrom host1x_syncpt_read_min(sp)); 1576579324aSTerje Bergstrom 1586579324aSTerje Bergstrom if (host1x_syncpt_is_expired(sp, wait->thresh)) { 1596579324aSTerje Bergstrom dev_dbg(host->dev, 1606579324aSTerje Bergstrom "drop WAIT id %d (%s) thresh 0x%x, min 0x%x\n", 1616579324aSTerje Bergstrom wait->syncpt_id, sp->name, wait->thresh, 1626579324aSTerje Bergstrom host1x_syncpt_read_min(sp)); 1636579324aSTerje Bergstrom 1646579324aSTerje Bergstrom host1x_syncpt_patch_offset(sp, patch, wait->offset); 1656579324aSTerje Bergstrom } 1666579324aSTerje Bergstrom 1676579324aSTerje Bergstrom wait->bo = NULL; 1686579324aSTerje Bergstrom } 1696579324aSTerje Bergstrom 1706579324aSTerje Bergstrom return 0; 1716579324aSTerje Bergstrom } 1726579324aSTerje Bergstrom 1736579324aSTerje Bergstrom static unsigned int pin_job(struct host1x_job *job) 1746579324aSTerje Bergstrom { 1756579324aSTerje Bergstrom unsigned int i; 1766579324aSTerje Bergstrom 1776579324aSTerje Bergstrom job->num_unpins = 0; 1786579324aSTerje Bergstrom 1796579324aSTerje Bergstrom for (i = 0; i < job->num_relocs; i++) { 1806579324aSTerje Bergstrom struct host1x_reloc *reloc = &job->relocarray[i]; 1816579324aSTerje Bergstrom struct sg_table *sgt; 1826579324aSTerje Bergstrom dma_addr_t phys_addr; 1836579324aSTerje Bergstrom 1846579324aSTerje Bergstrom reloc->target = host1x_bo_get(reloc->target); 1856579324aSTerje Bergstrom if (!reloc->target) 1866579324aSTerje Bergstrom goto unpin; 1876579324aSTerje Bergstrom 1886579324aSTerje Bergstrom phys_addr = host1x_bo_pin(reloc->target, &sgt); 1896579324aSTerje Bergstrom if (!phys_addr) 1906579324aSTerje Bergstrom goto unpin; 1916579324aSTerje Bergstrom 1926579324aSTerje Bergstrom job->addr_phys[job->num_unpins] = phys_addr; 1936579324aSTerje Bergstrom job->unpins[job->num_unpins].bo = reloc->target; 1946579324aSTerje Bergstrom job->unpins[job->num_unpins].sgt = sgt; 1956579324aSTerje Bergstrom job->num_unpins++; 1966579324aSTerje Bergstrom } 1976579324aSTerje Bergstrom 1986579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 1996579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 2006579324aSTerje Bergstrom struct sg_table *sgt; 2016579324aSTerje Bergstrom dma_addr_t phys_addr; 2026579324aSTerje Bergstrom 2036579324aSTerje Bergstrom g->bo = host1x_bo_get(g->bo); 2046579324aSTerje Bergstrom if (!g->bo) 2056579324aSTerje Bergstrom goto unpin; 2066579324aSTerje Bergstrom 2076579324aSTerje Bergstrom phys_addr = host1x_bo_pin(g->bo, &sgt); 2086579324aSTerje Bergstrom if (!phys_addr) 2096579324aSTerje Bergstrom goto unpin; 2106579324aSTerje Bergstrom 2116579324aSTerje Bergstrom job->addr_phys[job->num_unpins] = phys_addr; 2126579324aSTerje Bergstrom job->unpins[job->num_unpins].bo = g->bo; 2136579324aSTerje Bergstrom job->unpins[job->num_unpins].sgt = sgt; 2146579324aSTerje Bergstrom job->num_unpins++; 2156579324aSTerje Bergstrom } 2166579324aSTerje Bergstrom 2176579324aSTerje Bergstrom return job->num_unpins; 2186579324aSTerje Bergstrom 2196579324aSTerje Bergstrom unpin: 2206579324aSTerje Bergstrom host1x_job_unpin(job); 2216579324aSTerje Bergstrom return 0; 2226579324aSTerje Bergstrom } 2236579324aSTerje Bergstrom 2246579324aSTerje Bergstrom static unsigned int do_relocs(struct host1x_job *job, struct host1x_bo *cmdbuf) 2256579324aSTerje Bergstrom { 2266579324aSTerje Bergstrom int i = 0; 2276579324aSTerje Bergstrom u32 last_page = ~0; 2286579324aSTerje Bergstrom void *cmdbuf_page_addr = NULL; 2296579324aSTerje Bergstrom 2306579324aSTerje Bergstrom /* pin & patch the relocs for one gather */ 231*3364cd28SArto Merilainen for (i = 0; i < job->num_relocs; i++) { 2326579324aSTerje Bergstrom struct host1x_reloc *reloc = &job->relocarray[i]; 2336579324aSTerje Bergstrom u32 reloc_addr = (job->reloc_addr_phys[i] + 2346579324aSTerje Bergstrom reloc->target_offset) >> reloc->shift; 2356579324aSTerje Bergstrom u32 *target; 2366579324aSTerje Bergstrom 2376579324aSTerje Bergstrom /* skip all other gathers */ 238*3364cd28SArto Merilainen if (cmdbuf != reloc->cmdbuf) 2396579324aSTerje Bergstrom continue; 2406579324aSTerje Bergstrom 2416579324aSTerje Bergstrom if (last_page != reloc->cmdbuf_offset >> PAGE_SHIFT) { 2426579324aSTerje Bergstrom if (cmdbuf_page_addr) 2436579324aSTerje Bergstrom host1x_bo_kunmap(cmdbuf, last_page, 2446579324aSTerje Bergstrom cmdbuf_page_addr); 2456579324aSTerje Bergstrom 2466579324aSTerje Bergstrom cmdbuf_page_addr = host1x_bo_kmap(cmdbuf, 2476579324aSTerje Bergstrom reloc->cmdbuf_offset >> PAGE_SHIFT); 2486579324aSTerje Bergstrom last_page = reloc->cmdbuf_offset >> PAGE_SHIFT; 2496579324aSTerje Bergstrom 2506579324aSTerje Bergstrom if (unlikely(!cmdbuf_page_addr)) { 2516579324aSTerje Bergstrom pr_err("Could not map cmdbuf for relocation\n"); 2526579324aSTerje Bergstrom return -ENOMEM; 2536579324aSTerje Bergstrom } 2546579324aSTerje Bergstrom } 2556579324aSTerje Bergstrom 2566579324aSTerje Bergstrom target = cmdbuf_page_addr + (reloc->cmdbuf_offset & ~PAGE_MASK); 2576579324aSTerje Bergstrom *target = reloc_addr; 2586579324aSTerje Bergstrom } 2596579324aSTerje Bergstrom 2606579324aSTerje Bergstrom if (cmdbuf_page_addr) 2616579324aSTerje Bergstrom host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr); 2626579324aSTerje Bergstrom 2636579324aSTerje Bergstrom return 0; 2646579324aSTerje Bergstrom } 2656579324aSTerje Bergstrom 2665060d8ecSArto Merilainen static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf, 2676579324aSTerje Bergstrom unsigned int offset) 2686579324aSTerje Bergstrom { 2696579324aSTerje Bergstrom offset *= sizeof(u32); 2706579324aSTerje Bergstrom 2716579324aSTerje Bergstrom if (reloc->cmdbuf != cmdbuf || reloc->cmdbuf_offset != offset) 2725060d8ecSArto Merilainen return false; 2736579324aSTerje Bergstrom 2745060d8ecSArto Merilainen return true; 2756579324aSTerje Bergstrom } 2766579324aSTerje Bergstrom 2776579324aSTerje Bergstrom struct host1x_firewall { 2786579324aSTerje Bergstrom struct host1x_job *job; 2796579324aSTerje Bergstrom struct device *dev; 2806579324aSTerje Bergstrom 2816579324aSTerje Bergstrom unsigned int num_relocs; 2826579324aSTerje Bergstrom struct host1x_reloc *reloc; 2836579324aSTerje Bergstrom 2846579324aSTerje Bergstrom struct host1x_bo *cmdbuf_id; 2856579324aSTerje Bergstrom unsigned int offset; 2866579324aSTerje Bergstrom 2876579324aSTerje Bergstrom u32 words; 2886579324aSTerje Bergstrom u32 class; 2896579324aSTerje Bergstrom u32 reg; 2906579324aSTerje Bergstrom u32 mask; 2916579324aSTerje Bergstrom u32 count; 2926579324aSTerje Bergstrom }; 2936579324aSTerje Bergstrom 2946579324aSTerje Bergstrom static int check_mask(struct host1x_firewall *fw) 2956579324aSTerje Bergstrom { 2966579324aSTerje Bergstrom u32 mask = fw->mask; 2976579324aSTerje Bergstrom u32 reg = fw->reg; 2986579324aSTerje Bergstrom 2996579324aSTerje Bergstrom while (mask) { 3006579324aSTerje Bergstrom if (fw->words == 0) 3016579324aSTerje Bergstrom return -EINVAL; 3026579324aSTerje Bergstrom 3036579324aSTerje Bergstrom if (mask & 1) { 3046579324aSTerje Bergstrom if (fw->job->is_addr_reg(fw->dev, fw->class, reg)) { 3055060d8ecSArto Merilainen if (!fw->num_relocs) 3065060d8ecSArto Merilainen return -EINVAL; 3075060d8ecSArto Merilainen if (!check_reloc(fw->reloc, fw->cmdbuf_id, 3085060d8ecSArto Merilainen fw->offset)) 3096579324aSTerje Bergstrom return -EINVAL; 3106579324aSTerje Bergstrom fw->reloc++; 3116579324aSTerje Bergstrom fw->num_relocs--; 3126579324aSTerje Bergstrom } 3136579324aSTerje Bergstrom fw->words--; 3146579324aSTerje Bergstrom fw->offset++; 3156579324aSTerje Bergstrom } 3166579324aSTerje Bergstrom mask >>= 1; 3176579324aSTerje Bergstrom reg++; 3186579324aSTerje Bergstrom } 3196579324aSTerje Bergstrom 3206579324aSTerje Bergstrom return 0; 3216579324aSTerje Bergstrom } 3226579324aSTerje Bergstrom 3236579324aSTerje Bergstrom static int check_incr(struct host1x_firewall *fw) 3246579324aSTerje Bergstrom { 3256579324aSTerje Bergstrom u32 count = fw->count; 3266579324aSTerje Bergstrom u32 reg = fw->reg; 3276579324aSTerje Bergstrom 32864c173d3STerje Bergstrom while (count) { 3296579324aSTerje Bergstrom if (fw->words == 0) 3306579324aSTerje Bergstrom return -EINVAL; 3316579324aSTerje Bergstrom 3326579324aSTerje Bergstrom if (fw->job->is_addr_reg(fw->dev, fw->class, reg)) { 3335060d8ecSArto Merilainen if (!fw->num_relocs) 3345060d8ecSArto Merilainen return -EINVAL; 3355060d8ecSArto Merilainen if (!check_reloc(fw->reloc, fw->cmdbuf_id, fw->offset)) 3366579324aSTerje Bergstrom return -EINVAL; 3376579324aSTerje Bergstrom fw->reloc++; 3386579324aSTerje Bergstrom fw->num_relocs--; 3396579324aSTerje Bergstrom } 3406579324aSTerje Bergstrom reg++; 3416579324aSTerje Bergstrom fw->words--; 3426579324aSTerje Bergstrom fw->offset++; 3436579324aSTerje Bergstrom count--; 3446579324aSTerje Bergstrom } 3456579324aSTerje Bergstrom 3466579324aSTerje Bergstrom return 0; 3476579324aSTerje Bergstrom } 3486579324aSTerje Bergstrom 3496579324aSTerje Bergstrom static int check_nonincr(struct host1x_firewall *fw) 3506579324aSTerje Bergstrom { 3516579324aSTerje Bergstrom int is_addr_reg = fw->job->is_addr_reg(fw->dev, fw->class, fw->reg); 3526579324aSTerje Bergstrom u32 count = fw->count; 3536579324aSTerje Bergstrom 3546579324aSTerje Bergstrom while (count) { 3556579324aSTerje Bergstrom if (fw->words == 0) 3566579324aSTerje Bergstrom return -EINVAL; 3576579324aSTerje Bergstrom 3586579324aSTerje Bergstrom if (is_addr_reg) { 3595060d8ecSArto Merilainen if (!fw->num_relocs) 3605060d8ecSArto Merilainen return -EINVAL; 3615060d8ecSArto Merilainen if (!check_reloc(fw->reloc, fw->cmdbuf_id, fw->offset)) 3626579324aSTerje Bergstrom return -EINVAL; 3636579324aSTerje Bergstrom fw->reloc++; 3646579324aSTerje Bergstrom fw->num_relocs--; 3656579324aSTerje Bergstrom } 3666579324aSTerje Bergstrom fw->words--; 3676579324aSTerje Bergstrom fw->offset++; 3686579324aSTerje Bergstrom count--; 3696579324aSTerje Bergstrom } 3706579324aSTerje Bergstrom 3716579324aSTerje Bergstrom return 0; 3726579324aSTerje Bergstrom } 3736579324aSTerje Bergstrom 374afac0e43STerje Bergstrom static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g) 3756579324aSTerje Bergstrom { 376*3364cd28SArto Merilainen u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped + 377*3364cd28SArto Merilainen (g->offset / sizeof(u32)); 3786579324aSTerje Bergstrom int err = 0; 3796579324aSTerje Bergstrom 380afac0e43STerje Bergstrom if (!fw->job->is_addr_reg) 3816579324aSTerje Bergstrom return 0; 3826579324aSTerje Bergstrom 383afac0e43STerje Bergstrom fw->words = g->words; 384afac0e43STerje Bergstrom fw->cmdbuf_id = g->bo; 385afac0e43STerje Bergstrom fw->offset = 0; 3866579324aSTerje Bergstrom 387afac0e43STerje Bergstrom while (fw->words && !err) { 388afac0e43STerje Bergstrom u32 word = cmdbuf_base[fw->offset]; 3896579324aSTerje Bergstrom u32 opcode = (word & 0xf0000000) >> 28; 3906579324aSTerje Bergstrom 391afac0e43STerje Bergstrom fw->mask = 0; 392afac0e43STerje Bergstrom fw->reg = 0; 393afac0e43STerje Bergstrom fw->count = 0; 394afac0e43STerje Bergstrom fw->words--; 395afac0e43STerje Bergstrom fw->offset++; 3966579324aSTerje Bergstrom 3976579324aSTerje Bergstrom switch (opcode) { 3986579324aSTerje Bergstrom case 0: 399afac0e43STerje Bergstrom fw->class = word >> 6 & 0x3ff; 400afac0e43STerje Bergstrom fw->mask = word & 0x3f; 401afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 402afac0e43STerje Bergstrom err = check_mask(fw); 4036579324aSTerje Bergstrom if (err) 4046579324aSTerje Bergstrom goto out; 4056579324aSTerje Bergstrom break; 4066579324aSTerje Bergstrom case 1: 407afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 408afac0e43STerje Bergstrom fw->count = word & 0xffff; 409afac0e43STerje Bergstrom err = check_incr(fw); 4106579324aSTerje Bergstrom if (err) 4116579324aSTerje Bergstrom goto out; 4126579324aSTerje Bergstrom break; 4136579324aSTerje Bergstrom 4146579324aSTerje Bergstrom case 2: 415afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 416afac0e43STerje Bergstrom fw->count = word & 0xffff; 417afac0e43STerje Bergstrom err = check_nonincr(fw); 4186579324aSTerje Bergstrom if (err) 4196579324aSTerje Bergstrom goto out; 4206579324aSTerje Bergstrom break; 4216579324aSTerje Bergstrom 4226579324aSTerje Bergstrom case 3: 423afac0e43STerje Bergstrom fw->mask = word & 0xffff; 424afac0e43STerje Bergstrom fw->reg = word >> 16 & 0xfff; 425afac0e43STerje Bergstrom err = check_mask(fw); 4266579324aSTerje Bergstrom if (err) 4276579324aSTerje Bergstrom goto out; 4286579324aSTerje Bergstrom break; 4296579324aSTerje Bergstrom case 4: 4306579324aSTerje Bergstrom case 5: 4316579324aSTerje Bergstrom case 14: 4326579324aSTerje Bergstrom break; 4336579324aSTerje Bergstrom default: 4346579324aSTerje Bergstrom err = -EINVAL; 4356579324aSTerje Bergstrom break; 4366579324aSTerje Bergstrom } 4376579324aSTerje Bergstrom } 4386579324aSTerje Bergstrom 4396579324aSTerje Bergstrom /* No relocs should remain at this point */ 440afac0e43STerje Bergstrom if (fw->num_relocs) 4416579324aSTerje Bergstrom err = -EINVAL; 4426579324aSTerje Bergstrom 4436579324aSTerje Bergstrom out: 4446579324aSTerje Bergstrom return err; 4456579324aSTerje Bergstrom } 4466579324aSTerje Bergstrom 4476579324aSTerje Bergstrom static inline int copy_gathers(struct host1x_job *job, struct device *dev) 4486579324aSTerje Bergstrom { 449*3364cd28SArto Merilainen struct host1x_firewall fw; 4506579324aSTerje Bergstrom size_t size = 0; 4516579324aSTerje Bergstrom size_t offset = 0; 4526579324aSTerje Bergstrom int i; 4536579324aSTerje Bergstrom 454*3364cd28SArto Merilainen fw.job = job; 455*3364cd28SArto Merilainen fw.dev = dev; 456*3364cd28SArto Merilainen fw.reloc = job->relocarray; 457*3364cd28SArto Merilainen fw.num_relocs = job->num_relocs; 458*3364cd28SArto Merilainen fw.class = 0; 459*3364cd28SArto Merilainen 4606579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 4616579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 4626579324aSTerje Bergstrom size += g->words * sizeof(u32); 4636579324aSTerje Bergstrom } 4646579324aSTerje Bergstrom 4656579324aSTerje Bergstrom job->gather_copy_mapped = dma_alloc_writecombine(dev, size, 4666579324aSTerje Bergstrom &job->gather_copy, 4676579324aSTerje Bergstrom GFP_KERNEL); 4686579324aSTerje Bergstrom if (!job->gather_copy_mapped) { 4696579324aSTerje Bergstrom int err = PTR_ERR(job->gather_copy_mapped); 4706579324aSTerje Bergstrom job->gather_copy_mapped = NULL; 4716579324aSTerje Bergstrom return err; 4726579324aSTerje Bergstrom } 4736579324aSTerje Bergstrom 4746579324aSTerje Bergstrom job->gather_copy_size = size; 4756579324aSTerje Bergstrom 4766579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 4776579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 4786579324aSTerje Bergstrom void *gather; 4796579324aSTerje Bergstrom 480*3364cd28SArto Merilainen /* Copy the gather */ 4816579324aSTerje Bergstrom gather = host1x_bo_mmap(g->bo); 4826579324aSTerje Bergstrom memcpy(job->gather_copy_mapped + offset, gather + g->offset, 4836579324aSTerje Bergstrom g->words * sizeof(u32)); 4846579324aSTerje Bergstrom host1x_bo_munmap(g->bo, gather); 4856579324aSTerje Bergstrom 486*3364cd28SArto Merilainen /* Store the location in the buffer */ 4876579324aSTerje Bergstrom g->base = job->gather_copy; 4886579324aSTerje Bergstrom g->offset = offset; 489*3364cd28SArto Merilainen 490*3364cd28SArto Merilainen /* Validate the job */ 491*3364cd28SArto Merilainen if (validate(&fw, g)) 492*3364cd28SArto Merilainen return -EINVAL; 4936579324aSTerje Bergstrom 4946579324aSTerje Bergstrom offset += g->words * sizeof(u32); 4956579324aSTerje Bergstrom } 4966579324aSTerje Bergstrom 4976579324aSTerje Bergstrom return 0; 4986579324aSTerje Bergstrom } 4996579324aSTerje Bergstrom 5006579324aSTerje Bergstrom int host1x_job_pin(struct host1x_job *job, struct device *dev) 5016579324aSTerje Bergstrom { 5026579324aSTerje Bergstrom int err; 5036579324aSTerje Bergstrom unsigned int i, j; 5046579324aSTerje Bergstrom struct host1x *host = dev_get_drvdata(dev->parent); 5056579324aSTerje Bergstrom DECLARE_BITMAP(waitchk_mask, host1x_syncpt_nb_pts(host)); 5066579324aSTerje Bergstrom 5076579324aSTerje Bergstrom bitmap_zero(waitchk_mask, host1x_syncpt_nb_pts(host)); 5086579324aSTerje Bergstrom for (i = 0; i < job->num_waitchk; i++) { 5096579324aSTerje Bergstrom u32 syncpt_id = job->waitchk[i].syncpt_id; 5106579324aSTerje Bergstrom if (syncpt_id < host1x_syncpt_nb_pts(host)) 5116579324aSTerje Bergstrom set_bit(syncpt_id, waitchk_mask); 5126579324aSTerje Bergstrom } 5136579324aSTerje Bergstrom 5146579324aSTerje Bergstrom /* get current syncpt values for waitchk */ 5156579324aSTerje Bergstrom for_each_set_bit(i, waitchk_mask, host1x_syncpt_nb_pts(host)) 5166579324aSTerje Bergstrom host1x_syncpt_load(host->syncpt + i); 5176579324aSTerje Bergstrom 5186579324aSTerje Bergstrom /* pin memory */ 5196579324aSTerje Bergstrom err = pin_job(job); 5206579324aSTerje Bergstrom if (!err) 5216579324aSTerje Bergstrom goto out; 5226579324aSTerje Bergstrom 5236579324aSTerje Bergstrom /* patch gathers */ 5246579324aSTerje Bergstrom for (i = 0; i < job->num_gathers; i++) { 5256579324aSTerje Bergstrom struct host1x_job_gather *g = &job->gathers[i]; 5266579324aSTerje Bergstrom 5276579324aSTerje Bergstrom /* process each gather mem only once */ 5286579324aSTerje Bergstrom if (g->handled) 5296579324aSTerje Bergstrom continue; 5306579324aSTerje Bergstrom 5316579324aSTerje Bergstrom g->base = job->gather_addr_phys[i]; 5326579324aSTerje Bergstrom 5336579324aSTerje Bergstrom for (j = 0; j < job->num_gathers; j++) 5346579324aSTerje Bergstrom if (job->gathers[j].bo == g->bo) 5356579324aSTerje Bergstrom job->gathers[j].handled = true; 5366579324aSTerje Bergstrom 5376579324aSTerje Bergstrom err = do_relocs(job, g->bo); 538*3364cd28SArto Merilainen if (err) 539*3364cd28SArto Merilainen break; 5406579324aSTerje Bergstrom 5416579324aSTerje Bergstrom err = do_waitchks(job, host, g->bo); 5426579324aSTerje Bergstrom if (err) 5436579324aSTerje Bergstrom break; 5446579324aSTerje Bergstrom } 5456579324aSTerje Bergstrom 5466579324aSTerje Bergstrom if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !err) { 5476579324aSTerje Bergstrom err = copy_gathers(job, dev); 5486579324aSTerje Bergstrom if (err) { 5496579324aSTerje Bergstrom host1x_job_unpin(job); 5506579324aSTerje Bergstrom return err; 5516579324aSTerje Bergstrom } 5526579324aSTerje Bergstrom } 5536579324aSTerje Bergstrom 5546579324aSTerje Bergstrom out: 5556579324aSTerje Bergstrom wmb(); 5566579324aSTerje Bergstrom 5576579324aSTerje Bergstrom return err; 5586579324aSTerje Bergstrom } 5596579324aSTerje Bergstrom 5606579324aSTerje Bergstrom void host1x_job_unpin(struct host1x_job *job) 5616579324aSTerje Bergstrom { 5626579324aSTerje Bergstrom unsigned int i; 5636579324aSTerje Bergstrom 5646579324aSTerje Bergstrom for (i = 0; i < job->num_unpins; i++) { 5656579324aSTerje Bergstrom struct host1x_job_unpin_data *unpin = &job->unpins[i]; 5666579324aSTerje Bergstrom host1x_bo_unpin(unpin->bo, unpin->sgt); 5676579324aSTerje Bergstrom host1x_bo_put(unpin->bo); 5686579324aSTerje Bergstrom } 5696579324aSTerje Bergstrom job->num_unpins = 0; 5706579324aSTerje Bergstrom 5716579324aSTerje Bergstrom if (job->gather_copy_size) 5726579324aSTerje Bergstrom dma_free_writecombine(job->channel->dev, job->gather_copy_size, 5736579324aSTerje Bergstrom job->gather_copy_mapped, 5746579324aSTerje Bergstrom job->gather_copy); 5756579324aSTerje Bergstrom } 5766579324aSTerje Bergstrom 5776579324aSTerje Bergstrom /* 5786579324aSTerje Bergstrom * Debug routine used to dump job entries 5796579324aSTerje Bergstrom */ 5806579324aSTerje Bergstrom void host1x_job_dump(struct device *dev, struct host1x_job *job) 5816579324aSTerje Bergstrom { 5826579324aSTerje Bergstrom dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id); 5836579324aSTerje Bergstrom dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end); 5846579324aSTerje Bergstrom dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get); 5856579324aSTerje Bergstrom dev_dbg(dev, " TIMEOUT %d\n", job->timeout); 5866579324aSTerje Bergstrom dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots); 5876579324aSTerje Bergstrom dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins); 5886579324aSTerje Bergstrom } 589