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 | |
| 34 | struct host1x_job *host1x_job_alloc(struct host1x_channel *ch, |
| 35 | u32 num_cmdbufs, u32 num_relocs, |
| 36 | u32 num_waitchks) |
| 37 | { |
| 38 | struct host1x_job *job = NULL; |
| 39 | unsigned int num_unpins = num_cmdbufs + num_relocs; |
| 40 | u64 total; |
| 41 | void *mem; |
| 42 | |
| 43 | /* Check that we're not going to overflow */ |
| 44 | total = sizeof(struct host1x_job) + |
Dan Carpenter | f5fda67 | 2013-08-23 13:18:25 +0300 | [diff] [blame] | 45 | (u64)num_relocs * sizeof(struct host1x_reloc) + |
| 46 | (u64)num_unpins * sizeof(struct host1x_job_unpin_data) + |
| 47 | (u64)num_waitchks * sizeof(struct host1x_waitchk) + |
| 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); |
| 63 | job->relocarray = num_relocs ? mem : NULL; |
| 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); |
| 67 | job->waitchk = num_waitchks ? mem : NULL; |
| 68 | mem += num_waitchks * sizeof(struct host1x_waitchk); |
| 69 | job->gathers = num_cmdbufs ? mem : NULL; |
| 70 | mem += num_cmdbufs * sizeof(struct host1x_job_gather); |
| 71 | job->addr_phys = num_unpins ? mem : NULL; |
| 72 | |
| 73 | job->reloc_addr_phys = job->addr_phys; |
| 74 | job->gather_addr_phys = &job->addr_phys[num_relocs]; |
| 75 | |
| 76 | return job; |
| 77 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 78 | EXPORT_SYMBOL(host1x_job_alloc); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 79 | |
| 80 | struct host1x_job *host1x_job_get(struct host1x_job *job) |
| 81 | { |
| 82 | kref_get(&job->ref); |
| 83 | return job; |
| 84 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 85 | EXPORT_SYMBOL(host1x_job_get); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 86 | |
| 87 | static void job_free(struct kref *ref) |
| 88 | { |
| 89 | struct host1x_job *job = container_of(ref, struct host1x_job, ref); |
| 90 | |
| 91 | kfree(job); |
| 92 | } |
| 93 | |
| 94 | void host1x_job_put(struct host1x_job *job) |
| 95 | { |
| 96 | kref_put(&job->ref, job_free); |
| 97 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 98 | EXPORT_SYMBOL(host1x_job_put); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 99 | |
| 100 | void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo, |
| 101 | u32 words, u32 offset) |
| 102 | { |
| 103 | struct host1x_job_gather *cur_gather = &job->gathers[job->num_gathers]; |
| 104 | |
| 105 | cur_gather->words = words; |
| 106 | cur_gather->bo = bo; |
| 107 | cur_gather->offset = offset; |
| 108 | job->num_gathers++; |
| 109 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 110 | EXPORT_SYMBOL(host1x_job_add_gather); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * NULL an already satisfied WAIT_SYNCPT host method, by patching its |
| 114 | * args in the command stream. The method data is changed to reference |
| 115 | * a reserved (never given out or incr) HOST1X_SYNCPT_RESERVED syncpt |
| 116 | * with a matching threshold value of 0, so is guaranteed to be popped |
| 117 | * by the host HW. |
| 118 | */ |
| 119 | static void host1x_syncpt_patch_offset(struct host1x_syncpt *sp, |
| 120 | struct host1x_bo *h, u32 offset) |
| 121 | { |
| 122 | void *patch_addr = NULL; |
| 123 | |
| 124 | /* patch the wait */ |
| 125 | patch_addr = host1x_bo_kmap(h, offset >> PAGE_SHIFT); |
| 126 | if (patch_addr) { |
| 127 | host1x_syncpt_patch_wait(sp, |
| 128 | patch_addr + (offset & ~PAGE_MASK)); |
| 129 | host1x_bo_kunmap(h, offset >> PAGE_SHIFT, patch_addr); |
| 130 | } else |
| 131 | pr_err("Could not map cmdbuf for wait check\n"); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Check driver supplied waitchk structs for syncpt thresholds |
| 136 | * that have already been satisfied and NULL the comparison (to |
| 137 | * avoid a wrap condition in the HW). |
| 138 | */ |
| 139 | static int do_waitchks(struct host1x_job *job, struct host1x *host, |
| 140 | struct host1x_bo *patch) |
| 141 | { |
| 142 | int i; |
| 143 | |
| 144 | /* compare syncpt vs wait threshold */ |
| 145 | for (i = 0; i < job->num_waitchk; i++) { |
| 146 | struct host1x_waitchk *wait = &job->waitchk[i]; |
| 147 | struct host1x_syncpt *sp = |
| 148 | host1x_syncpt_get(host, wait->syncpt_id); |
| 149 | |
| 150 | /* validate syncpt id */ |
| 151 | if (wait->syncpt_id > host1x_syncpt_nb_pts(host)) |
| 152 | continue; |
| 153 | |
| 154 | /* skip all other gathers */ |
| 155 | if (patch != wait->bo) |
| 156 | continue; |
| 157 | |
| 158 | trace_host1x_syncpt_wait_check(wait->bo, wait->offset, |
| 159 | wait->syncpt_id, wait->thresh, |
| 160 | host1x_syncpt_read_min(sp)); |
| 161 | |
| 162 | if (host1x_syncpt_is_expired(sp, wait->thresh)) { |
| 163 | dev_dbg(host->dev, |
Thierry Reding | 5c0d8d3 | 2016-06-23 11:19:00 +0200 | [diff] [blame] | 164 | "drop WAIT id %u (%s) thresh 0x%x, min 0x%x\n", |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 165 | wait->syncpt_id, sp->name, wait->thresh, |
| 166 | host1x_syncpt_read_min(sp)); |
| 167 | |
| 168 | host1x_syncpt_patch_offset(sp, patch, wait->offset); |
| 169 | } |
| 170 | |
| 171 | wait->bo = NULL; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 177 | static unsigned int pin_job(struct host1x *host, struct host1x_job *job) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 178 | { |
| 179 | unsigned int i; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 180 | int err; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 181 | |
| 182 | job->num_unpins = 0; |
| 183 | |
| 184 | for (i = 0; i < job->num_relocs; i++) { |
| 185 | struct host1x_reloc *reloc = &job->relocarray[i]; |
| 186 | struct sg_table *sgt; |
| 187 | dma_addr_t phys_addr; |
| 188 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 189 | reloc->target.bo = host1x_bo_get(reloc->target.bo); |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 190 | if (!reloc->target.bo) { |
| 191 | err = -EINVAL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 192 | goto unpin; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 193 | } |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 194 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 195 | phys_addr = host1x_bo_pin(reloc->target.bo, &sgt); |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 196 | if (!phys_addr) { |
| 197 | err = -EINVAL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 198 | goto unpin; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 199 | } |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 200 | |
| 201 | job->addr_phys[job->num_unpins] = phys_addr; |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 202 | job->unpins[job->num_unpins].bo = reloc->target.bo; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 203 | job->unpins[job->num_unpins].sgt = sgt; |
| 204 | job->num_unpins++; |
| 205 | } |
| 206 | |
| 207 | for (i = 0; i < job->num_gathers; i++) { |
| 208 | struct host1x_job_gather *g = &job->gathers[i]; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 209 | size_t gather_size = 0; |
| 210 | struct scatterlist *sg; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 211 | struct sg_table *sgt; |
| 212 | dma_addr_t phys_addr; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 213 | unsigned long shift; |
| 214 | struct iova *alloc; |
| 215 | unsigned int j; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 216 | |
| 217 | g->bo = host1x_bo_get(g->bo); |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 218 | if (!g->bo) { |
| 219 | err = -EINVAL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 220 | goto unpin; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 221 | } |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 222 | |
| 223 | phys_addr = host1x_bo_pin(g->bo, &sgt); |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 224 | if (!phys_addr) { |
| 225 | err = -EINVAL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 226 | goto unpin; |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 227 | } |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 228 | |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 229 | if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) { |
| 230 | for_each_sg(sgt->sgl, sg, sgt->nents, j) |
| 231 | gather_size += sg->length; |
| 232 | gather_size = iova_align(&host->iova, gather_size); |
| 233 | |
| 234 | shift = iova_shift(&host->iova); |
| 235 | alloc = alloc_iova(&host->iova, gather_size >> shift, |
| 236 | host->iova_end >> shift, true); |
| 237 | if (!alloc) { |
| 238 | err = -ENOMEM; |
| 239 | goto unpin; |
| 240 | } |
| 241 | |
| 242 | err = iommu_map_sg(host->domain, |
| 243 | iova_dma_addr(&host->iova, alloc), |
| 244 | sgt->sgl, sgt->nents, IOMMU_READ); |
| 245 | if (err == 0) { |
| 246 | __free_iova(&host->iova, alloc); |
| 247 | err = -EINVAL; |
| 248 | goto unpin; |
| 249 | } |
| 250 | |
| 251 | job->addr_phys[job->num_unpins] = |
| 252 | iova_dma_addr(&host->iova, alloc); |
| 253 | job->unpins[job->num_unpins].size = gather_size; |
| 254 | } else { |
| 255 | job->addr_phys[job->num_unpins] = phys_addr; |
| 256 | } |
| 257 | |
| 258 | job->gather_addr_phys[i] = job->addr_phys[job->num_unpins]; |
| 259 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 260 | job->unpins[job->num_unpins].bo = g->bo; |
| 261 | job->unpins[job->num_unpins].sgt = sgt; |
| 262 | job->num_unpins++; |
| 263 | } |
| 264 | |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 265 | return 0; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 266 | |
| 267 | unpin: |
| 268 | host1x_job_unpin(job); |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 269 | return err; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Markus Elfring | 341917f | 2015-12-19 12:48:31 +0100 | [diff] [blame] | 272 | static int do_relocs(struct host1x_job *job, struct host1x_bo *cmdbuf) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 273 | { |
| 274 | int i = 0; |
| 275 | u32 last_page = ~0; |
| 276 | void *cmdbuf_page_addr = NULL; |
| 277 | |
| 278 | /* pin & patch the relocs for one gather */ |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 279 | for (i = 0; i < job->num_relocs; i++) { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 280 | struct host1x_reloc *reloc = &job->relocarray[i]; |
| 281 | u32 reloc_addr = (job->reloc_addr_phys[i] + |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 282 | reloc->target.offset) >> reloc->shift; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 283 | u32 *target; |
| 284 | |
| 285 | /* skip all other gathers */ |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 286 | if (cmdbuf != reloc->cmdbuf.bo) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 287 | continue; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 288 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 289 | if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 290 | if (cmdbuf_page_addr) |
| 291 | host1x_bo_kunmap(cmdbuf, last_page, |
| 292 | cmdbuf_page_addr); |
| 293 | |
| 294 | cmdbuf_page_addr = host1x_bo_kmap(cmdbuf, |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 295 | reloc->cmdbuf.offset >> PAGE_SHIFT); |
| 296 | last_page = reloc->cmdbuf.offset >> PAGE_SHIFT; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 297 | |
| 298 | if (unlikely(!cmdbuf_page_addr)) { |
| 299 | pr_err("Could not map cmdbuf for relocation\n"); |
| 300 | return -ENOMEM; |
| 301 | } |
| 302 | } |
| 303 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 304 | target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 305 | *target = reloc_addr; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (cmdbuf_page_addr) |
| 309 | host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Arto Merilainen | 5060d8e | 2013-05-29 13:26:03 +0300 | [diff] [blame] | 314 | static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf, |
Thierry Reding | 37857cd | 2013-10-10 10:17:45 +0200 | [diff] [blame] | 315 | unsigned int offset) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 316 | { |
| 317 | offset *= sizeof(u32); |
| 318 | |
Thierry Reding | 961e3be | 2014-06-10 10:25:00 +0200 | [diff] [blame] | 319 | if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset) |
Arto Merilainen | 5060d8e | 2013-05-29 13:26:03 +0300 | [diff] [blame] | 320 | return false; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 321 | |
Arto Merilainen | 5060d8e | 2013-05-29 13:26:03 +0300 | [diff] [blame] | 322 | return true; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | struct host1x_firewall { |
| 326 | struct host1x_job *job; |
| 327 | struct device *dev; |
| 328 | |
| 329 | unsigned int num_relocs; |
| 330 | struct host1x_reloc *reloc; |
| 331 | |
Thierry Reding | d7fbcf4 | 2013-10-10 10:21:58 +0200 | [diff] [blame] | 332 | struct host1x_bo *cmdbuf; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 333 | unsigned int offset; |
| 334 | |
| 335 | u32 words; |
| 336 | u32 class; |
| 337 | u32 reg; |
| 338 | u32 mask; |
| 339 | u32 count; |
| 340 | }; |
| 341 | |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 342 | static int check_register(struct host1x_firewall *fw, unsigned long offset) |
| 343 | { |
| 344 | if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) { |
| 345 | if (!fw->num_relocs) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset)) |
| 349 | return -EINVAL; |
| 350 | |
| 351 | fw->num_relocs--; |
| 352 | fw->reloc++; |
| 353 | } |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 358 | static int check_mask(struct host1x_firewall *fw) |
| 359 | { |
| 360 | u32 mask = fw->mask; |
| 361 | u32 reg = fw->reg; |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 362 | int ret; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 363 | |
| 364 | while (mask) { |
| 365 | if (fw->words == 0) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | if (mask & 1) { |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 369 | ret = check_register(fw, reg); |
| 370 | if (ret < 0) |
| 371 | return ret; |
| 372 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 373 | fw->words--; |
| 374 | fw->offset++; |
| 375 | } |
| 376 | mask >>= 1; |
| 377 | reg++; |
| 378 | } |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int check_incr(struct host1x_firewall *fw) |
| 384 | { |
| 385 | u32 count = fw->count; |
| 386 | u32 reg = fw->reg; |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 387 | int ret; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 388 | |
Terje Bergstrom | 64c173d | 2013-05-29 13:26:02 +0300 | [diff] [blame] | 389 | while (count) { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 390 | if (fw->words == 0) |
| 391 | return -EINVAL; |
| 392 | |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 393 | ret = check_register(fw, reg); |
| 394 | if (ret < 0) |
| 395 | return ret; |
| 396 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 397 | reg++; |
| 398 | fw->words--; |
| 399 | fw->offset++; |
| 400 | count--; |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int check_nonincr(struct host1x_firewall *fw) |
| 407 | { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 408 | u32 count = fw->count; |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 409 | int ret; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 410 | |
| 411 | while (count) { |
| 412 | if (fw->words == 0) |
| 413 | return -EINVAL; |
| 414 | |
Thierry Reding | d77563f | 2013-10-10 10:24:04 +0200 | [diff] [blame] | 415 | ret = check_register(fw, fw->reg); |
| 416 | if (ret < 0) |
| 417 | return ret; |
| 418 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 419 | fw->words--; |
| 420 | fw->offset++; |
| 421 | count--; |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 427 | static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 428 | { |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 429 | u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped + |
| 430 | (g->offset / sizeof(u32)); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 431 | int err = 0; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 432 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 433 | if (!fw->job->is_addr_reg) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 434 | return 0; |
| 435 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 436 | fw->words = g->words; |
Thierry Reding | d7fbcf4 | 2013-10-10 10:21:58 +0200 | [diff] [blame] | 437 | fw->cmdbuf = g->bo; |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 438 | fw->offset = 0; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 439 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 440 | while (fw->words && !err) { |
| 441 | u32 word = cmdbuf_base[fw->offset]; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 442 | u32 opcode = (word & 0xf0000000) >> 28; |
| 443 | |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 444 | fw->mask = 0; |
| 445 | fw->reg = 0; |
| 446 | fw->count = 0; |
| 447 | fw->words--; |
| 448 | fw->offset++; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 449 | |
| 450 | switch (opcode) { |
| 451 | case 0: |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 452 | fw->class = word >> 6 & 0x3ff; |
| 453 | fw->mask = word & 0x3f; |
| 454 | fw->reg = word >> 16 & 0xfff; |
| 455 | err = check_mask(fw); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 456 | if (err) |
| 457 | goto out; |
| 458 | break; |
| 459 | case 1: |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 460 | fw->reg = word >> 16 & 0xfff; |
| 461 | fw->count = word & 0xffff; |
| 462 | err = check_incr(fw); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 463 | if (err) |
| 464 | goto out; |
| 465 | break; |
| 466 | |
| 467 | case 2: |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 468 | fw->reg = word >> 16 & 0xfff; |
| 469 | fw->count = word & 0xffff; |
| 470 | err = check_nonincr(fw); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 471 | if (err) |
| 472 | goto out; |
| 473 | break; |
| 474 | |
| 475 | case 3: |
Terje Bergstrom | afac0e4 | 2013-05-29 13:26:04 +0300 | [diff] [blame] | 476 | fw->mask = word & 0xffff; |
| 477 | fw->reg = word >> 16 & 0xfff; |
| 478 | err = check_mask(fw); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 479 | if (err) |
| 480 | goto out; |
| 481 | break; |
| 482 | case 4: |
| 483 | case 5: |
| 484 | case 14: |
| 485 | break; |
| 486 | default: |
| 487 | err = -EINVAL; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 492 | out: |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 493 | return err; |
| 494 | } |
| 495 | |
| 496 | static inline int copy_gathers(struct host1x_job *job, struct device *dev) |
| 497 | { |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 498 | struct host1x_firewall fw; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 499 | size_t size = 0; |
| 500 | size_t offset = 0; |
| 501 | int i; |
| 502 | |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 503 | fw.job = job; |
| 504 | fw.dev = dev; |
| 505 | fw.reloc = job->relocarray; |
| 506 | fw.num_relocs = job->num_relocs; |
| 507 | fw.class = 0; |
| 508 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 509 | for (i = 0; i < job->num_gathers; i++) { |
| 510 | struct host1x_job_gather *g = &job->gathers[i]; |
Thierry Reding | 6df633d | 2016-06-23 11:33:31 +0200 | [diff] [blame] | 511 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 512 | size += g->words * sizeof(u32); |
| 513 | } |
| 514 | |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 515 | job->gather_copy_mapped = dma_alloc_wc(dev, size, &job->gather_copy, |
| 516 | GFP_KERNEL); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 517 | if (!job->gather_copy_mapped) { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 518 | job->gather_copy_mapped = NULL; |
Dan Carpenter | 745cecc | 2013-08-23 13:19:11 +0300 | [diff] [blame] | 519 | return -ENOMEM; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | job->gather_copy_size = size; |
| 523 | |
| 524 | for (i = 0; i < job->num_gathers; i++) { |
| 525 | struct host1x_job_gather *g = &job->gathers[i]; |
| 526 | void *gather; |
| 527 | |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 528 | /* Copy the gather */ |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 529 | gather = host1x_bo_mmap(g->bo); |
| 530 | memcpy(job->gather_copy_mapped + offset, gather + g->offset, |
| 531 | g->words * sizeof(u32)); |
| 532 | host1x_bo_munmap(g->bo, gather); |
| 533 | |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 534 | /* Store the location in the buffer */ |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 535 | g->base = job->gather_copy; |
| 536 | g->offset = offset; |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 537 | |
| 538 | /* Validate the job */ |
| 539 | if (validate(&fw, g)) |
| 540 | return -EINVAL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 541 | |
| 542 | offset += g->words * sizeof(u32); |
| 543 | } |
| 544 | |
Erik Faye-Lund | a9ff999 | 2013-10-04 20:18:33 +0000 | [diff] [blame] | 545 | /* No relocs should remain at this point */ |
| 546 | if (fw.num_relocs) |
| 547 | return -EINVAL; |
| 548 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | int host1x_job_pin(struct host1x_job *job, struct device *dev) |
| 553 | { |
| 554 | int err; |
| 555 | unsigned int i, j; |
| 556 | struct host1x *host = dev_get_drvdata(dev->parent); |
| 557 | DECLARE_BITMAP(waitchk_mask, host1x_syncpt_nb_pts(host)); |
| 558 | |
| 559 | bitmap_zero(waitchk_mask, host1x_syncpt_nb_pts(host)); |
| 560 | for (i = 0; i < job->num_waitchk; i++) { |
| 561 | u32 syncpt_id = job->waitchk[i].syncpt_id; |
Thierry Reding | 6df633d | 2016-06-23 11:33:31 +0200 | [diff] [blame] | 562 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 563 | if (syncpt_id < host1x_syncpt_nb_pts(host)) |
| 564 | set_bit(syncpt_id, waitchk_mask); |
| 565 | } |
| 566 | |
| 567 | /* get current syncpt values for waitchk */ |
| 568 | for_each_set_bit(i, waitchk_mask, host1x_syncpt_nb_pts(host)) |
| 569 | host1x_syncpt_load(host->syncpt + i); |
| 570 | |
| 571 | /* pin memory */ |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 572 | err = pin_job(host, job); |
| 573 | if (err) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 574 | goto out; |
| 575 | |
| 576 | /* patch gathers */ |
| 577 | for (i = 0; i < job->num_gathers; i++) { |
| 578 | struct host1x_job_gather *g = &job->gathers[i]; |
| 579 | |
| 580 | /* process each gather mem only once */ |
| 581 | if (g->handled) |
| 582 | continue; |
| 583 | |
| 584 | g->base = job->gather_addr_phys[i]; |
| 585 | |
Arto Merilainen | f08ef2d | 2016-11-08 19:51:32 +0200 | [diff] [blame] | 586 | for (j = i + 1; j < job->num_gathers; j++) { |
| 587 | if (job->gathers[j].bo == g->bo) { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 588 | job->gathers[j].handled = true; |
Arto Merilainen | f08ef2d | 2016-11-08 19:51:32 +0200 | [diff] [blame] | 589 | job->gathers[j].base = g->base; |
| 590 | } |
| 591 | } |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 592 | |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 593 | err = do_relocs(job, g->bo); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 594 | if (err) |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 595 | break; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 596 | |
Arto Merilainen | 3364cd2 | 2013-05-29 13:26:05 +0300 | [diff] [blame] | 597 | err = do_waitchks(job, host, g->bo); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 598 | if (err) |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !err) { |
| 603 | err = copy_gathers(job, dev); |
| 604 | if (err) { |
| 605 | host1x_job_unpin(job); |
| 606 | return err; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | out: |
| 611 | wmb(); |
| 612 | |
| 613 | return err; |
| 614 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 615 | EXPORT_SYMBOL(host1x_job_pin); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 616 | |
| 617 | void host1x_job_unpin(struct host1x_job *job) |
| 618 | { |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 619 | struct host1x *host = dev_get_drvdata(job->channel->dev->parent); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 620 | unsigned int i; |
| 621 | |
| 622 | for (i = 0; i < job->num_unpins; i++) { |
| 623 | struct host1x_job_unpin_data *unpin = &job->unpins[i]; |
Thierry Reding | 6df633d | 2016-06-23 11:33:31 +0200 | [diff] [blame] | 624 | |
Mikko Perttunen | 404bfb7 | 2016-12-14 13:16:14 +0200 | [diff] [blame] | 625 | if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) { |
| 626 | iommu_unmap(host->domain, job->addr_phys[i], |
| 627 | unpin->size); |
| 628 | free_iova(&host->iova, |
| 629 | iova_pfn(&host->iova, job->addr_phys[i])); |
| 630 | } |
| 631 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 632 | host1x_bo_unpin(unpin->bo, unpin->sgt); |
| 633 | host1x_bo_put(unpin->bo); |
| 634 | } |
Thierry Reding | 0b8070d1 | 2016-06-23 11:35:50 +0200 | [diff] [blame] | 635 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 636 | job->num_unpins = 0; |
| 637 | |
| 638 | if (job->gather_copy_size) |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 639 | dma_free_wc(job->channel->dev, job->gather_copy_size, |
Thierry Reding | 0b8070d1 | 2016-06-23 11:35:50 +0200 | [diff] [blame] | 640 | job->gather_copy_mapped, job->gather_copy); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 641 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 642 | EXPORT_SYMBOL(host1x_job_unpin); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Debug routine used to dump job entries |
| 646 | */ |
| 647 | void host1x_job_dump(struct device *dev, struct host1x_job *job) |
| 648 | { |
| 649 | dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id); |
| 650 | dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end); |
| 651 | dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get); |
| 652 | dev_dbg(dev, " TIMEOUT %d\n", job->timeout); |
| 653 | dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots); |
| 654 | dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins); |
| 655 | } |