Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Tegra host1x Job |
| 3 | * |
Arto Merilainen | f08ef2d | 2016-11-08 19:51:32 +0200 | [diff] [blame] | 4 | * Copyright (c) 2010-2015, NVIDIA Corporation. |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/err.h> |
Thierry Reding | 35d747a | 2013-09-24 16:30:32 +0200 | [diff] [blame] | 21 | #include <linux/host1x.h> |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 22 | #include <linux/kref.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <trace/events/host1x.h> |
| 28 | |
| 29 | #include "channel.h" |
| 30 | #include "dev.h" |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 31 | #include "job.h" |
| 32 | #include "syncpt.h" |
| 33 | |
Dmitry Osipenko | a47ac10 | 2017-06-15 02:18:39 +0300 | [diff] [blame] | 34 | #define HOST1X_WAIT_SYNCPT_OFFSET 0x8 |
| 35 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 36 | struct host1x_job *host1x_job_alloc(struct host1x_channel *ch, |
Thierry Reding | 24c94e1 | 2018-05-05 08:45:47 +0200 | [diff] [blame] | 37 | u32 num_cmdbufs, u32 num_relocs) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 38 | { |
| 39 | struct host1x_job *job = NULL; |
| 40 | unsigned int num_unpins = num_cmdbufs + num_relocs; |
| 41 | u64 total; |
| 42 | void *mem; |
| 43 | |
| 44 | /* Check that we're not going to overflow */ |
| 45 | total = sizeof(struct host1x_job) + |
Dan Carpenter | f5fda67 | 2013-08-23 13:18:25 +0300 | [diff] [blame] | 46 | (u64)num_relocs * sizeof(struct host1x_reloc) + |
| 47 | (u64)num_unpins * sizeof(struct host1x_job_unpin_data) + |
Dan Carpenter | f5fda67 | 2013-08-23 13:18:25 +0300 | [diff] [blame] | 48 | (u64)num_cmdbufs * sizeof(struct host1x_job_gather) + |
| 49 | (u64)num_unpins * sizeof(dma_addr_t) + |
| 50 | (u64)num_unpins * sizeof(u32 *); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 51 | if (total > ULONG_MAX) |
| 52 | return NULL; |
| 53 | |
| 54 | mem = job = kzalloc(total, GFP_KERNEL); |
| 55 | if (!job) |
| 56 | return NULL; |
| 57 | |
| 58 | kref_init(&job->ref); |
| 59 | job->channel = ch; |
| 60 | |
| 61 | /* Redistribute memory to the structs */ |
| 62 | mem += sizeof(struct host1x_job); |
Thierry Reding | 06490bb | 2018-05-16 16:58:44 +0200 | [diff] [blame] | 63 | job->relocs = num_relocs ? mem : NULL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 64 | mem += num_relocs * sizeof(struct host1x_reloc); |
| 65 | job->unpins = num_unpins ? mem : NULL; |
| 66 | mem += num_unpins * sizeof(struct host1x_job_unpin_data); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 67 | job->gathers = num_cmdbufs ? mem : NULL; |
| 68 | mem += num_cmdbufs * sizeof(struct host1x_job_gather); |
| 69 | job->addr_phys = num_unpins ? mem : NULL; |
| 70 | |
| 71 | job->reloc_addr_phys = job->addr_phys; |
| 72 | job->gather_addr_phys = &job->addr_phys[num_relocs]; |
| 73 | |
| 74 | return job; |
| 75 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 76 | EXPORT_SYMBOL(host1x_job_alloc); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 77 | |
| 78 | struct host1x_job *host1x_job_get(struct host1x_job *job) |
| 79 | { |
| 80 | kref_get(&job->ref); |
| 81 | return job; |
| 82 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 83 | EXPORT_SYMBOL(host1x_job_get); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 84 | |
| 85 | static void job_free(struct kref *ref) |
| 86 | { |
| 87 | struct host1x_job *job = container_of(ref, struct host1x_job, ref); |
| 88 | |
| 89 | kfree(job); |
| 90 | } |
| 91 | |
| 92 | void host1x_job_put(struct host1x_job *job) |
| 93 | { |
| 94 | kref_put(&job->ref, job_free); |
| 95 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 96 | EXPORT_SYMBOL(host1x_job_put); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 97 | |
| 98 | void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo, |
Thierry Reding | 326bbd7 | 2018-05-16 17:01:43 +0200 | [diff] [blame] | 99 | unsigned int words, unsigned int offset) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 100 | { |
Thierry Reding | 326bbd7 | 2018-05-16 17:01:43 +0200 | [diff] [blame] | 101 | struct host1x_job_gather *gather = &job->gathers[job->num_gathers]; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 102 | |
Thierry Reding | 326bbd7 | 2018-05-16 17:01:43 +0200 | [diff] [blame] | 103 | gather->words = words; |
| 104 | gather->bo = bo; |
| 105 | gather->offset = offset; |
| 106 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 107 | job->num_gathers++; |
| 108 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 109 | EXPORT_SYMBOL(host1x_job_add_gather); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 110 | |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 111 | static unsigned int pin_job(struct host1x *host, struct host1x_job *job) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 112 | { |
| 113 | unsigned int i; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 114 | int err; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 115 | |
| 116 | job->num_unpins = 0; |
| 117 | |
| 118 | for (i = 0; i < job->num_relocs; i++) { |
Thierry Reding | 06490bb | 2018-05-16 16:58:44 +0200 | [diff] [blame] | 119 | struct host1x_reloc *reloc = &job->relocs[i]; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 120 | struct sg_table *sgt; |
| 121 | dma_addr_t phys_addr; |
| 122 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 123 | reloc->target.bo = host1x_bo_get(reloc->target.bo); |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 124 | if (!reloc->target.bo) { |
| 125 | err = -EINVAL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 126 | goto unpin; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 127 | } |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 128 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 129 | phys_addr = host1x_bo_pin(reloc->target.bo, &sgt); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 130 | |
| 131 | job->addr_phys[job->num_unpins] = phys_addr; |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 132 | job->unpins[job->num_unpins].bo = reloc->target.bo; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 133 | job->unpins[job->num_unpins].sgt = sgt; |
| 134 | job->num_unpins++; |
| 135 | } |
| 136 | |
| 137 | for (i = 0; i < job->num_gathers; i++) { |
| 138 | struct host1x_job_gather *g = &job->gathers[i]; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 139 | size_t gather_size = 0; |
| 140 | struct scatterlist *sg; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 141 | struct sg_table *sgt; |
| 142 | dma_addr_t phys_addr; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 143 | unsigned long shift; |
| 144 | struct iova *alloc; |
| 145 | unsigned int j; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 146 | |
| 147 | g->bo = host1x_bo_get(g->bo); |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 148 | if (!g->bo) { |
| 149 | err = -EINVAL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 150 | goto unpin; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 151 | } |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 152 | |
| 153 | phys_addr = host1x_bo_pin(g->bo, &sgt); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 154 | |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 155 | if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) { |
| 156 | for_each_sg(sgt->sgl, sg, sgt->nents, j) |
| 157 | gather_size += sg->length; |
| 158 | gather_size = iova_align(&host->iova, gather_size); |
| 159 | |
| 160 | shift = iova_shift(&host->iova); |
| 161 | alloc = alloc_iova(&host->iova, gather_size >> shift, |
| 162 | host->iova_end >> shift, true); |
| 163 | if (!alloc) { |
| 164 | err = -ENOMEM; |
| 165 | goto unpin; |
| 166 | } |
| 167 | |
| 168 | err = iommu_map_sg(host->domain, |
| 169 | iova_dma_addr(&host->iova, alloc), |
| 170 | sgt->sgl, sgt->nents, IOMMU_READ); |
| 171 | if (err == 0) { |
| 172 | __free_iova(&host->iova, alloc); |
| 173 | err = -EINVAL; |
| 174 | goto unpin; |
| 175 | } |
| 176 | |
| 177 | job->addr_phys[job->num_unpins] = |
| 178 | iova_dma_addr(&host->iova, alloc); |
| 179 | job->unpins[job->num_unpins].size = gather_size; |
| 180 | } else { |
| 181 | job->addr_phys[job->num_unpins] = phys_addr; |
| 182 | } |
| 183 | |
| 184 | job->gather_addr_phys[i] = job->addr_phys[job->num_unpins]; |
| 185 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 186 | job->unpins[job->num_unpins].bo = g->bo; |
| 187 | job->unpins[job->num_unpins].sgt = sgt; |
| 188 | job->num_unpins++; |
| 189 | } |
| 190 | |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 191 | return 0; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 192 | |
| 193 | unpin: |
| 194 | host1x_job_unpin(job); |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 195 | return err; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Dmitry Osipenko | 47f89c1 | 2017-06-15 02:18:34 +0300 | [diff] [blame] | 198 | static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 199 | { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 200 | u32 last_page = ~0; |
| 201 | void *cmdbuf_page_addr = NULL; |
Dmitry Osipenko | 47f89c1 | 2017-06-15 02:18:34 +0300 | [diff] [blame] | 202 | struct host1x_bo *cmdbuf = g->bo; |
Thierry Reding | d4ad3ad | 2018-03-23 13:31:24 +0100 | [diff] [blame] | 203 | unsigned int i; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 204 | |
| 205 | /* pin & patch the relocs for one gather */ |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 206 | for (i = 0; i < job->num_relocs; i++) { |
Thierry Reding | 06490bb | 2018-05-16 16:58:44 +0200 | [diff] [blame] | 207 | struct host1x_reloc *reloc = &job->relocs[i]; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 208 | u32 reloc_addr = (job->reloc_addr_phys[i] + |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 209 | reloc->target.offset) >> reloc->shift; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 210 | u32 *target; |
| 211 | |
| 212 | /* skip all other gathers */ |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 213 | if (cmdbuf != reloc->cmdbuf.bo) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 214 | continue; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 215 | |
Dmitry Osipenko | 47f89c1 | 2017-06-15 02:18:34 +0300 | [diff] [blame] | 216 | if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) { |
| 217 | target = (u32 *)job->gather_copy_mapped + |
| 218 | reloc->cmdbuf.offset / sizeof(u32) + |
| 219 | g->offset / sizeof(u32); |
| 220 | goto patch_reloc; |
| 221 | } |
| 222 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 223 | if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 224 | if (cmdbuf_page_addr) |
| 225 | host1x_bo_kunmap(cmdbuf, last_page, |
| 226 | cmdbuf_page_addr); |
| 227 | |
| 228 | cmdbuf_page_addr = host1x_bo_kmap(cmdbuf, |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 229 | reloc->cmdbuf.offset >> PAGE_SHIFT); |
| 230 | last_page = reloc->cmdbuf.offset >> PAGE_SHIFT; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 231 | |
| 232 | if (unlikely(!cmdbuf_page_addr)) { |
| 233 | pr_err("Could not map cmdbuf for relocation\n"); |
| 234 | return -ENOMEM; |
| 235 | } |
| 236 | } |
| 237 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 238 | target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK); |
Dmitry Osipenko | 47f89c1 | 2017-06-15 02:18:34 +0300 | [diff] [blame] | 239 | patch_reloc: |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 240 | *target = reloc_addr; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (cmdbuf_page_addr) |
| 244 | host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Arto Merilainen | 5060d8e | 2013-05-29 13:26:03 +0300 | [diff] [blame] | 249 | static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf, |
Thierry Reding | 37857cd | 2013-10-10 10:17:45 +0200 | [diff] [blame] | 250 | unsigned int offset) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 251 | { |
| 252 | offset *= sizeof(u32); |
| 253 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 254 | if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset) |
Arto Merilainen | 5060d8e | 2013-05-29 13:26:03 +0300 | [diff] [blame] | 255 | return false; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 256 | |
Dmitry Osipenko | 571cbf7 | 2017-06-15 02:18:35 +0300 | [diff] [blame] | 257 | /* relocation shift value validation isn't implemented yet */ |
| 258 | if (reloc->shift) |
| 259 | return false; |
| 260 | |
Arto Merilainen | 5060d8e | 2013-05-29 13:26:03 +0300 | [diff] [blame] | 261 | return true; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | struct host1x_firewall { |
| 265 | struct host1x_job *job; |
| 266 | struct device *dev; |
| 267 | |
| 268 | unsigned int num_relocs; |
| 269 | struct host1x_reloc *reloc; |
| 270 | |
Thierry Reding | d7fbcf4 | 2013-10-10 10:21:58 +0200 | [diff] [blame] | 271 | struct host1x_bo *cmdbuf; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 272 | unsigned int offset; |
| 273 | |
| 274 | u32 words; |
| 275 | u32 class; |
| 276 | u32 reg; |
| 277 | u32 mask; |
| 278 | u32 count; |
| 279 | }; |
| 280 | |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 281 | static int check_register(struct host1x_firewall *fw, unsigned long offset) |
| 282 | { |
Dmitry Osipenko | 0f563a4 | 2017-06-15 02:18:37 +0300 | [diff] [blame] | 283 | if (!fw->job->is_addr_reg) |
| 284 | return 0; |
| 285 | |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 286 | if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) { |
| 287 | if (!fw->num_relocs) |
| 288 | return -EINVAL; |
| 289 | |
| 290 | if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset)) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | fw->num_relocs--; |
| 294 | fw->reloc++; |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Dmitry Osipenko | 0f563a4 | 2017-06-15 02:18:37 +0300 | [diff] [blame] | 300 | static int check_class(struct host1x_firewall *fw, u32 class) |
| 301 | { |
| 302 | if (!fw->job->is_valid_class) { |
| 303 | if (fw->class != class) |
| 304 | return -EINVAL; |
| 305 | } else { |
| 306 | if (!fw->job->is_valid_class(fw->class)) |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 313 | static int check_mask(struct host1x_firewall *fw) |
| 314 | { |
| 315 | u32 mask = fw->mask; |
| 316 | u32 reg = fw->reg; |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 317 | int ret; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 318 | |
| 319 | while (mask) { |
| 320 | if (fw->words == 0) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | if (mask & 1) { |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 324 | ret = check_register(fw, reg); |
| 325 | if (ret < 0) |
| 326 | return ret; |
| 327 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 328 | fw->words--; |
| 329 | fw->offset++; |
| 330 | } |
| 331 | mask >>= 1; |
| 332 | reg++; |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int check_incr(struct host1x_firewall *fw) |
| 339 | { |
| 340 | u32 count = fw->count; |
| 341 | u32 reg = fw->reg; |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 342 | int ret; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 343 | |
Terje Bergstrom | 64c173d | 2013-05-29 13:26:02 +0300 | [diff] [blame] | 344 | while (count) { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 345 | if (fw->words == 0) |
| 346 | return -EINVAL; |
| 347 | |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 348 | ret = check_register(fw, reg); |
| 349 | if (ret < 0) |
| 350 | return ret; |
| 351 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 352 | reg++; |
| 353 | fw->words--; |
| 354 | fw->offset++; |
| 355 | count--; |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int check_nonincr(struct host1x_firewall *fw) |
| 362 | { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 363 | u32 count = fw->count; |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 364 | int ret; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 365 | |
| 366 | while (count) { |
| 367 | if (fw->words == 0) |
| 368 | return -EINVAL; |
| 369 | |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 370 | ret = check_register(fw, fw->reg); |
| 371 | if (ret < 0) |
| 372 | return ret; |
| 373 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 374 | fw->words--; |
| 375 | fw->offset++; |
| 376 | count--; |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 382 | static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 383 | { |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 384 | u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped + |
| 385 | (g->offset / sizeof(u32)); |
Dmitry Osipenko | 0f563a4 | 2017-06-15 02:18:37 +0300 | [diff] [blame] | 386 | u32 job_class = fw->class; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 387 | int err = 0; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 388 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 389 | fw->words = g->words; |
Thierry Reding | d7fbcf4 | 2013-10-10 10:21:58 +0200 | [diff] [blame] | 390 | fw->cmdbuf = g->bo; |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 391 | fw->offset = 0; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 392 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 393 | while (fw->words && !err) { |
| 394 | u32 word = cmdbuf_base[fw->offset]; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 395 | u32 opcode = (word & 0xf0000000) >> 28; |
| 396 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 397 | fw->mask = 0; |
| 398 | fw->reg = 0; |
| 399 | fw->count = 0; |
| 400 | fw->words--; |
| 401 | fw->offset++; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 402 | |
| 403 | switch (opcode) { |
| 404 | case 0: |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 405 | fw->class = word >> 6 & 0x3ff; |
| 406 | fw->mask = word & 0x3f; |
| 407 | fw->reg = word >> 16 & 0xfff; |
Dmitry Osipenko | 0f563a4 | 2017-06-15 02:18:37 +0300 | [diff] [blame] | 408 | err = check_class(fw, job_class); |
| 409 | if (!err) |
| 410 | err = check_mask(fw); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 411 | if (err) |
| 412 | goto out; |
| 413 | break; |
| 414 | case 1: |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 415 | fw->reg = word >> 16 & 0xfff; |
| 416 | fw->count = word & 0xffff; |
| 417 | err = check_incr(fw); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 418 | if (err) |
| 419 | goto out; |
| 420 | break; |
| 421 | |
| 422 | case 2: |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 423 | fw->reg = word >> 16 & 0xfff; |
| 424 | fw->count = word & 0xffff; |
| 425 | err = check_nonincr(fw); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 426 | if (err) |
| 427 | goto out; |
| 428 | break; |
| 429 | |
| 430 | case 3: |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 431 | fw->mask = word & 0xffff; |
| 432 | fw->reg = word >> 16 & 0xfff; |
| 433 | err = check_mask(fw); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 434 | if (err) |
| 435 | goto out; |
| 436 | break; |
| 437 | case 4: |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 438 | case 14: |
| 439 | break; |
| 440 | default: |
| 441 | err = -EINVAL; |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 446 | out: |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 447 | return err; |
| 448 | } |
| 449 | |
| 450 | static inline int copy_gathers(struct host1x_job *job, struct device *dev) |
| 451 | { |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 452 | struct host1x_firewall fw; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 453 | size_t size = 0; |
| 454 | size_t offset = 0; |
Thierry Reding | d4ad3ad | 2018-03-23 13:31:24 +0100 | [diff] [blame] | 455 | unsigned int i; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 456 | |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 457 | fw.job = job; |
| 458 | fw.dev = dev; |
Thierry Reding | 06490bb | 2018-05-16 16:58:44 +0200 | [diff] [blame] | 459 | fw.reloc = job->relocs; |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 460 | fw.num_relocs = job->num_relocs; |
Dmitry Osipenko | 3833d16 | 2017-06-15 02:18:32 +0300 | [diff] [blame] | 461 | fw.class = job->class; |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 462 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 463 | for (i = 0; i < job->num_gathers; i++) { |
| 464 | struct host1x_job_gather *g = &job->gathers[i]; |
Thierry Reding | 6df633d | 2016-06-23 11:33:31 +0200 | [diff] [blame] | 465 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 466 | size += g->words * sizeof(u32); |
| 467 | } |
| 468 | |
Dmitry Osipenko | 43240bb | 2017-06-15 02:18:43 +0300 | [diff] [blame] | 469 | /* |
| 470 | * Try a non-blocking allocation from a higher priority pools first, |
| 471 | * as awaiting for the allocation here is a major performance hit. |
| 472 | */ |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 473 | job->gather_copy_mapped = dma_alloc_wc(dev, size, &job->gather_copy, |
Dmitry Osipenko | 43240bb | 2017-06-15 02:18:43 +0300 | [diff] [blame] | 474 | GFP_NOWAIT); |
| 475 | |
| 476 | /* the higher priority allocation failed, try the generic-blocking */ |
| 477 | if (!job->gather_copy_mapped) |
| 478 | job->gather_copy_mapped = dma_alloc_wc(dev, size, |
| 479 | &job->gather_copy, |
| 480 | GFP_KERNEL); |
| 481 | if (!job->gather_copy_mapped) |
Dan Carpenter | 745cecc | 2013-08-23 13:19:11 +0300 | [diff] [blame] | 482 | return -ENOMEM; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 483 | |
| 484 | job->gather_copy_size = size; |
| 485 | |
| 486 | for (i = 0; i < job->num_gathers; i++) { |
| 487 | struct host1x_job_gather *g = &job->gathers[i]; |
| 488 | void *gather; |
| 489 | |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 490 | /* Copy the gather */ |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 491 | gather = host1x_bo_mmap(g->bo); |
| 492 | memcpy(job->gather_copy_mapped + offset, gather + g->offset, |
| 493 | g->words * sizeof(u32)); |
| 494 | host1x_bo_munmap(g->bo, gather); |
| 495 | |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 496 | /* Store the location in the buffer */ |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 497 | g->base = job->gather_copy; |
| 498 | g->offset = offset; |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 499 | |
| 500 | /* Validate the job */ |
| 501 | if (validate(&fw, g)) |
| 502 | return -EINVAL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 503 | |
| 504 | offset += g->words * sizeof(u32); |
| 505 | } |
| 506 | |
Thierry Reding | 24c94e1 | 2018-05-05 08:45:47 +0200 | [diff] [blame] | 507 | /* No relocs should remain at this point */ |
| 508 | if (fw.num_relocs) |
Erik Faye-Lund | a9ff999 | 2013-10-04 20:18:33 +0000 | [diff] [blame] | 509 | return -EINVAL; |
| 510 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | int host1x_job_pin(struct host1x_job *job, struct device *dev) |
| 515 | { |
| 516 | int err; |
| 517 | unsigned int i, j; |
| 518 | struct host1x *host = dev_get_drvdata(dev->parent); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 519 | |
| 520 | /* pin memory */ |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 521 | err = pin_job(host, job); |
| 522 | if (err) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 523 | goto out; |
| 524 | |
Dmitry Osipenko | 47f89c1 | 2017-06-15 02:18:34 +0300 | [diff] [blame] | 525 | if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) { |
| 526 | err = copy_gathers(job, dev); |
| 527 | if (err) |
| 528 | goto out; |
| 529 | } |
| 530 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 531 | /* patch gathers */ |
| 532 | for (i = 0; i < job->num_gathers; i++) { |
| 533 | struct host1x_job_gather *g = &job->gathers[i]; |
| 534 | |
| 535 | /* process each gather mem only once */ |
| 536 | if (g->handled) |
| 537 | continue; |
| 538 | |
Dmitry Osipenko | 47f89c1 | 2017-06-15 02:18:34 +0300 | [diff] [blame] | 539 | /* copy_gathers() sets gathers base if firewall is enabled */ |
| 540 | if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) |
| 541 | g->base = job->gather_addr_phys[i]; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 542 | |
Arto Merilainen | f08ef2d | 2016-11-08 19:51:32 +0200 | [diff] [blame] | 543 | for (j = i + 1; j < job->num_gathers; j++) { |
| 544 | if (job->gathers[j].bo == g->bo) { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 545 | job->gathers[j].handled = true; |
Arto Merilainen | f08ef2d | 2016-11-08 19:51:32 +0200 | [diff] [blame] | 546 | job->gathers[j].base = g->base; |
| 547 | } |
| 548 | } |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 549 | |
Dmitry Osipenko | 47f89c1 | 2017-06-15 02:18:34 +0300 | [diff] [blame] | 550 | err = do_relocs(job, g); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 551 | if (err) |
Dmitry Osipenko | 47f89c1 | 2017-06-15 02:18:34 +0300 | [diff] [blame] | 552 | break; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 555 | out: |
Dmitry Osipenko | e5855aa | 2017-06-15 02:18:33 +0300 | [diff] [blame] | 556 | if (err) |
| 557 | host1x_job_unpin(job); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 558 | wmb(); |
| 559 | |
| 560 | return err; |
| 561 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 562 | EXPORT_SYMBOL(host1x_job_pin); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 563 | |
| 564 | void host1x_job_unpin(struct host1x_job *job) |
| 565 | { |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 566 | struct host1x *host = dev_get_drvdata(job->channel->dev->parent); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 567 | unsigned int i; |
| 568 | |
| 569 | for (i = 0; i < job->num_unpins; i++) { |
| 570 | struct host1x_job_unpin_data *unpin = &job->unpins[i]; |
Thierry Reding | 6df633d | 2016-06-23 11:33:31 +0200 | [diff] [blame] | 571 | |
Dmitry Osipenko | ec58923 | 2018-07-06 21:02:36 +0300 | [diff] [blame] | 572 | if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && |
| 573 | unpin->size && host->domain) { |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 574 | iommu_unmap(host->domain, job->addr_phys[i], |
| 575 | unpin->size); |
| 576 | free_iova(&host->iova, |
| 577 | iova_pfn(&host->iova, job->addr_phys[i])); |
| 578 | } |
| 579 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 580 | host1x_bo_unpin(unpin->bo, unpin->sgt); |
| 581 | host1x_bo_put(unpin->bo); |
| 582 | } |
Thierry Reding | 0b8070d1 | 2016-06-23 11:35:50 +0200 | [diff] [blame] | 583 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 584 | job->num_unpins = 0; |
| 585 | |
| 586 | if (job->gather_copy_size) |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 587 | dma_free_wc(job->channel->dev, job->gather_copy_size, |
Thierry Reding | 0b8070d1 | 2016-06-23 11:35:50 +0200 | [diff] [blame] | 588 | job->gather_copy_mapped, job->gather_copy); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 589 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 590 | EXPORT_SYMBOL(host1x_job_unpin); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 591 | |
| 592 | /* |
| 593 | * Debug routine used to dump job entries |
| 594 | */ |
| 595 | void host1x_job_dump(struct device *dev, struct host1x_job *job) |
| 596 | { |
| 597 | dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id); |
| 598 | dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end); |
| 599 | dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get); |
| 600 | dev_dbg(dev, " TIMEOUT %d\n", job->timeout); |
| 601 | dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots); |
| 602 | dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins); |
| 603 | } |