1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Tegra host1x Job 4 * 5 * Copyright (c) 2010-2015, NVIDIA Corporation. 6 */ 7 8 #include <linux/dma-mapping.h> 9 #include <linux/err.h> 10 #include <linux/host1x.h> 11 #include <linux/iommu.h> 12 #include <linux/kref.h> 13 #include <linux/module.h> 14 #include <linux/scatterlist.h> 15 #include <linux/slab.h> 16 #include <linux/vmalloc.h> 17 #include <trace/events/host1x.h> 18 19 #include "channel.h" 20 #include "dev.h" 21 #include "job.h" 22 #include "syncpt.h" 23 24 #define HOST1X_WAIT_SYNCPT_OFFSET 0x8 25 26 struct host1x_job *host1x_job_alloc(struct host1x_channel *ch, 27 u32 num_cmdbufs, u32 num_relocs, 28 bool skip_firewall) 29 { 30 struct host1x_job *job = NULL; 31 unsigned int num_unpins = num_relocs; 32 bool enable_firewall; 33 u64 total; 34 void *mem; 35 36 enable_firewall = IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !skip_firewall; 37 38 if (!enable_firewall) 39 num_unpins += num_cmdbufs; 40 41 /* Check that we're not going to overflow */ 42 total = sizeof(struct host1x_job) + 43 (u64)num_relocs * sizeof(struct host1x_reloc) + 44 (u64)num_unpins * sizeof(struct host1x_job_unpin_data) + 45 (u64)num_cmdbufs * sizeof(struct host1x_job_cmd) + 46 (u64)num_unpins * sizeof(dma_addr_t) + 47 (u64)num_unpins * sizeof(u32 *); 48 if (total > ULONG_MAX) 49 return NULL; 50 51 mem = job = kzalloc(total, GFP_KERNEL); 52 if (!job) 53 return NULL; 54 55 job->enable_firewall = enable_firewall; 56 57 kref_init(&job->ref); 58 job->channel = ch; 59 60 /* Redistribute memory to the structs */ 61 mem += sizeof(struct host1x_job); 62 job->relocs = num_relocs ? mem : NULL; 63 mem += num_relocs * sizeof(struct host1x_reloc); 64 job->unpins = num_unpins ? mem : NULL; 65 mem += num_unpins * sizeof(struct host1x_job_unpin_data); 66 job->cmds = num_cmdbufs ? mem : NULL; 67 mem += num_cmdbufs * sizeof(struct host1x_job_cmd); 68 job->addr_phys = num_unpins ? mem : NULL; 69 70 job->reloc_addr_phys = job->addr_phys; 71 job->gather_addr_phys = &job->addr_phys[num_relocs]; 72 73 return job; 74 } 75 EXPORT_SYMBOL(host1x_job_alloc); 76 77 struct host1x_job *host1x_job_get(struct host1x_job *job) 78 { 79 kref_get(&job->ref); 80 return job; 81 } 82 EXPORT_SYMBOL(host1x_job_get); 83 84 static void job_free(struct kref *ref) 85 { 86 struct host1x_job *job = container_of(ref, struct host1x_job, ref); 87 88 if (job->release) 89 job->release(job); 90 91 if (job->fence) { 92 /* 93 * remove_callback is atomic w.r.t. fence signaling, so 94 * after the call returns, we know that the callback is not 95 * in execution, and the fence can be safely freed. 96 */ 97 dma_fence_remove_callback(job->fence, &job->fence_cb); 98 dma_fence_put(job->fence); 99 } 100 101 if (job->syncpt) 102 host1x_syncpt_put(job->syncpt); 103 104 kfree(job); 105 } 106 107 void host1x_job_put(struct host1x_job *job) 108 { 109 kref_put(&job->ref, job_free); 110 } 111 EXPORT_SYMBOL(host1x_job_put); 112 113 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo, 114 unsigned int words, unsigned int offset) 115 { 116 struct host1x_job_gather *gather = &job->cmds[job->num_cmds].gather; 117 118 gather->words = words; 119 gather->bo = bo; 120 gather->offset = offset; 121 122 job->num_cmds++; 123 } 124 EXPORT_SYMBOL(host1x_job_add_gather); 125 126 void host1x_job_add_wait(struct host1x_job *job, u32 id, u32 thresh, 127 bool relative, u32 next_class) 128 { 129 struct host1x_job_cmd *cmd = &job->cmds[job->num_cmds]; 130 131 cmd->is_wait = true; 132 cmd->wait.id = id; 133 cmd->wait.threshold = thresh; 134 cmd->wait.next_class = next_class; 135 cmd->wait.relative = relative; 136 137 job->num_cmds++; 138 } 139 EXPORT_SYMBOL(host1x_job_add_wait); 140 141 static unsigned int pin_job(struct host1x *host, struct host1x_job *job) 142 { 143 unsigned long mask = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE; 144 struct host1x_client *client = job->client; 145 struct device *dev = client->dev; 146 struct host1x_job_gather *g; 147 unsigned int i; 148 int err; 149 150 job->num_unpins = 0; 151 152 for (i = 0; i < job->num_relocs; i++) { 153 struct host1x_reloc *reloc = &job->relocs[i]; 154 enum dma_data_direction direction; 155 struct host1x_bo_mapping *map; 156 struct host1x_bo *bo; 157 158 reloc->target.bo = host1x_bo_get(reloc->target.bo); 159 if (!reloc->target.bo) { 160 err = -EINVAL; 161 goto unpin; 162 } 163 164 bo = reloc->target.bo; 165 166 switch (reloc->flags & mask) { 167 case HOST1X_RELOC_READ: 168 direction = DMA_TO_DEVICE; 169 break; 170 171 case HOST1X_RELOC_WRITE: 172 direction = DMA_FROM_DEVICE; 173 break; 174 175 case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE: 176 direction = DMA_BIDIRECTIONAL; 177 break; 178 179 default: 180 err = -EINVAL; 181 goto unpin; 182 } 183 184 map = host1x_bo_pin(dev, bo, direction, NULL); 185 if (IS_ERR(map)) { 186 err = PTR_ERR(map); 187 goto unpin; 188 } 189 190 /* 191 * host1x clients are generally not able to do scatter-gather themselves, so fail 192 * if the buffer is discontiguous and we fail to map its SG table to a single 193 * contiguous chunk of I/O virtual memory. 194 */ 195 if (map->chunks > 1) { 196 err = -EINVAL; 197 goto unpin; 198 } 199 200 job->addr_phys[job->num_unpins] = map->phys; 201 job->unpins[job->num_unpins].map = map; 202 job->num_unpins++; 203 } 204 205 /* 206 * We will copy gathers BO content later, so there is no need to 207 * hold and pin them. 208 */ 209 if (job->enable_firewall) 210 return 0; 211 212 for (i = 0; i < job->num_cmds; i++) { 213 struct host1x_bo_mapping *map; 214 size_t gather_size = 0; 215 struct scatterlist *sg; 216 unsigned long shift; 217 struct iova *alloc; 218 unsigned int j; 219 220 if (job->cmds[i].is_wait) 221 continue; 222 223 g = &job->cmds[i].gather; 224 225 g->bo = host1x_bo_get(g->bo); 226 if (!g->bo) { 227 err = -EINVAL; 228 goto unpin; 229 } 230 231 map = host1x_bo_pin(host->dev, g->bo, DMA_TO_DEVICE, NULL); 232 if (IS_ERR(map)) { 233 err = PTR_ERR(map); 234 goto unpin; 235 } 236 237 if (host->domain) { 238 ssize_t map_err; 239 240 for_each_sgtable_sg(map->sgt, sg, j) 241 gather_size += sg->length; 242 243 gather_size = iova_align(&host->iova, gather_size); 244 245 shift = iova_shift(&host->iova); 246 alloc = alloc_iova(&host->iova, gather_size >> shift, 247 host->iova_end >> shift, true); 248 if (!alloc) { 249 err = -ENOMEM; 250 goto put; 251 } 252 253 map_err = iommu_map_sgtable(host->domain, iova_dma_addr(&host->iova, alloc), 254 map->sgt, IOMMU_READ); 255 if (map_err < 0) { 256 __free_iova(&host->iova, alloc); 257 err = map_err; 258 goto put; 259 } 260 261 map->phys = iova_dma_addr(&host->iova, alloc); 262 map->size = gather_size; 263 } 264 265 job->addr_phys[job->num_unpins] = map->phys; 266 job->unpins[job->num_unpins].map = map; 267 job->num_unpins++; 268 269 job->gather_addr_phys[i] = map->phys; 270 } 271 272 return 0; 273 274 put: 275 host1x_bo_put(g->bo); 276 unpin: 277 host1x_job_unpin(job); 278 return err; 279 } 280 281 static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g) 282 { 283 void *cmdbuf_addr = NULL; 284 struct host1x_bo *cmdbuf = g->bo; 285 unsigned int i; 286 287 /* pin & patch the relocs for one gather */ 288 for (i = 0; i < job->num_relocs; i++) { 289 struct host1x_reloc *reloc = &job->relocs[i]; 290 u32 reloc_addr = (job->reloc_addr_phys[i] + 291 reloc->target.offset) >> reloc->shift; 292 u32 *target; 293 294 /* skip all other gathers */ 295 if (cmdbuf != reloc->cmdbuf.bo) 296 continue; 297 298 if (job->enable_firewall) { 299 target = (u32 *)job->gather_copy_mapped + 300 reloc->cmdbuf.offset / sizeof(u32) + 301 g->offset / sizeof(u32); 302 goto patch_reloc; 303 } 304 305 if (!cmdbuf_addr) { 306 cmdbuf_addr = host1x_bo_mmap(cmdbuf); 307 308 if (unlikely(!cmdbuf_addr)) { 309 pr_err("Could not map cmdbuf for relocation\n"); 310 return -ENOMEM; 311 } 312 } 313 314 target = cmdbuf_addr + reloc->cmdbuf.offset; 315 patch_reloc: 316 *target = reloc_addr; 317 } 318 319 if (cmdbuf_addr) 320 host1x_bo_munmap(cmdbuf, cmdbuf_addr); 321 322 return 0; 323 } 324 325 static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf, 326 unsigned int offset) 327 { 328 offset *= sizeof(u32); 329 330 if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset) 331 return false; 332 333 /* relocation shift value validation isn't implemented yet */ 334 if (reloc->shift) 335 return false; 336 337 return true; 338 } 339 340 struct host1x_firewall { 341 struct host1x_job *job; 342 struct device *dev; 343 344 unsigned int num_relocs; 345 struct host1x_reloc *reloc; 346 347 struct host1x_bo *cmdbuf; 348 unsigned int offset; 349 350 u32 words; 351 u32 class; 352 u32 reg; 353 u32 mask; 354 u32 count; 355 }; 356 357 static int check_register(struct host1x_firewall *fw, unsigned long offset) 358 { 359 if (!fw->job->is_addr_reg) 360 return 0; 361 362 if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) { 363 if (!fw->num_relocs) 364 return -EINVAL; 365 366 if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset)) 367 return -EINVAL; 368 369 fw->num_relocs--; 370 fw->reloc++; 371 } 372 373 return 0; 374 } 375 376 static int check_class(struct host1x_firewall *fw, u32 class) 377 { 378 if (!fw->job->is_valid_class) { 379 if (fw->class != class) 380 return -EINVAL; 381 } else { 382 if (!fw->job->is_valid_class(fw->class)) 383 return -EINVAL; 384 } 385 386 return 0; 387 } 388 389 static int check_mask(struct host1x_firewall *fw) 390 { 391 u32 mask = fw->mask; 392 u32 reg = fw->reg; 393 int ret; 394 395 while (mask) { 396 if (fw->words == 0) 397 return -EINVAL; 398 399 if (mask & 1) { 400 ret = check_register(fw, reg); 401 if (ret < 0) 402 return ret; 403 404 fw->words--; 405 fw->offset++; 406 } 407 mask >>= 1; 408 reg++; 409 } 410 411 return 0; 412 } 413 414 static int check_incr(struct host1x_firewall *fw) 415 { 416 u32 count = fw->count; 417 u32 reg = fw->reg; 418 int ret; 419 420 while (count) { 421 if (fw->words == 0) 422 return -EINVAL; 423 424 ret = check_register(fw, reg); 425 if (ret < 0) 426 return ret; 427 428 reg++; 429 fw->words--; 430 fw->offset++; 431 count--; 432 } 433 434 return 0; 435 } 436 437 static int check_nonincr(struct host1x_firewall *fw) 438 { 439 u32 count = fw->count; 440 int ret; 441 442 while (count) { 443 if (fw->words == 0) 444 return -EINVAL; 445 446 ret = check_register(fw, fw->reg); 447 if (ret < 0) 448 return ret; 449 450 fw->words--; 451 fw->offset++; 452 count--; 453 } 454 455 return 0; 456 } 457 458 static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g) 459 { 460 u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped + 461 (g->offset / sizeof(u32)); 462 u32 job_class = fw->class; 463 int err = 0; 464 465 fw->words = g->words; 466 fw->cmdbuf = g->bo; 467 fw->offset = 0; 468 469 while (fw->words && !err) { 470 u32 word = cmdbuf_base[fw->offset]; 471 u32 opcode = (word & 0xf0000000) >> 28; 472 473 fw->mask = 0; 474 fw->reg = 0; 475 fw->count = 0; 476 fw->words--; 477 fw->offset++; 478 479 switch (opcode) { 480 case 0: 481 fw->class = word >> 6 & 0x3ff; 482 fw->mask = word & 0x3f; 483 fw->reg = word >> 16 & 0xfff; 484 err = check_class(fw, job_class); 485 if (!err) 486 err = check_mask(fw); 487 if (err) 488 goto out; 489 break; 490 case 1: 491 fw->reg = word >> 16 & 0xfff; 492 fw->count = word & 0xffff; 493 err = check_incr(fw); 494 if (err) 495 goto out; 496 break; 497 498 case 2: 499 fw->reg = word >> 16 & 0xfff; 500 fw->count = word & 0xffff; 501 err = check_nonincr(fw); 502 if (err) 503 goto out; 504 break; 505 506 case 3: 507 fw->mask = word & 0xffff; 508 fw->reg = word >> 16 & 0xfff; 509 err = check_mask(fw); 510 if (err) 511 goto out; 512 break; 513 case 4: 514 case 14: 515 break; 516 default: 517 err = -EINVAL; 518 break; 519 } 520 } 521 522 out: 523 return err; 524 } 525 526 static inline int copy_gathers(struct device *host, struct host1x_job *job, 527 struct device *dev) 528 { 529 struct host1x_firewall fw; 530 size_t size = 0; 531 size_t offset = 0; 532 unsigned int i; 533 534 fw.job = job; 535 fw.dev = dev; 536 fw.reloc = job->relocs; 537 fw.num_relocs = job->num_relocs; 538 fw.class = job->class; 539 540 for (i = 0; i < job->num_cmds; i++) { 541 struct host1x_job_gather *g; 542 543 if (job->cmds[i].is_wait) 544 continue; 545 546 g = &job->cmds[i].gather; 547 548 size += g->words * sizeof(u32); 549 } 550 551 /* 552 * Try a non-blocking allocation from a higher priority pools first, 553 * as awaiting for the allocation here is a major performance hit. 554 */ 555 job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy, 556 GFP_NOWAIT); 557 558 /* the higher priority allocation failed, try the generic-blocking */ 559 if (!job->gather_copy_mapped) 560 job->gather_copy_mapped = dma_alloc_wc(host, size, 561 &job->gather_copy, 562 GFP_KERNEL); 563 if (!job->gather_copy_mapped) 564 return -ENOMEM; 565 566 job->gather_copy_size = size; 567 568 for (i = 0; i < job->num_cmds; i++) { 569 struct host1x_job_gather *g; 570 void *gather; 571 572 if (job->cmds[i].is_wait) 573 continue; 574 g = &job->cmds[i].gather; 575 576 /* Copy the gather */ 577 gather = host1x_bo_mmap(g->bo); 578 memcpy(job->gather_copy_mapped + offset, gather + g->offset, 579 g->words * sizeof(u32)); 580 host1x_bo_munmap(g->bo, gather); 581 582 /* Store the location in the buffer */ 583 g->base = job->gather_copy; 584 g->offset = offset; 585 586 /* Validate the job */ 587 if (validate(&fw, g)) 588 return -EINVAL; 589 590 offset += g->words * sizeof(u32); 591 } 592 593 /* No relocs should remain at this point */ 594 if (fw.num_relocs) 595 return -EINVAL; 596 597 return 0; 598 } 599 600 int host1x_job_pin(struct host1x_job *job, struct device *dev) 601 { 602 int err; 603 unsigned int i, j; 604 struct host1x *host = dev_get_drvdata(dev->parent); 605 606 /* pin memory */ 607 err = pin_job(host, job); 608 if (err) 609 goto out; 610 611 if (job->enable_firewall) { 612 err = copy_gathers(host->dev, job, dev); 613 if (err) 614 goto out; 615 } 616 617 /* patch gathers */ 618 for (i = 0; i < job->num_cmds; i++) { 619 struct host1x_job_gather *g; 620 621 if (job->cmds[i].is_wait) 622 continue; 623 g = &job->cmds[i].gather; 624 625 /* process each gather mem only once */ 626 if (g->handled) 627 continue; 628 629 /* copy_gathers() sets gathers base if firewall is enabled */ 630 if (!job->enable_firewall) 631 g->base = job->gather_addr_phys[i]; 632 633 for (j = i + 1; j < job->num_cmds; j++) { 634 if (!job->cmds[j].is_wait && 635 job->cmds[j].gather.bo == g->bo) { 636 job->cmds[j].gather.handled = true; 637 job->cmds[j].gather.base = g->base; 638 } 639 } 640 641 err = do_relocs(job, g); 642 if (err) 643 break; 644 } 645 646 out: 647 if (err) 648 host1x_job_unpin(job); 649 wmb(); 650 651 return err; 652 } 653 EXPORT_SYMBOL(host1x_job_pin); 654 655 void host1x_job_unpin(struct host1x_job *job) 656 { 657 struct host1x *host = dev_get_drvdata(job->channel->dev->parent); 658 unsigned int i; 659 660 for (i = 0; i < job->num_unpins; i++) { 661 struct host1x_bo_mapping *map = job->unpins[i].map; 662 struct host1x_bo *bo = map->bo; 663 664 if (!job->enable_firewall && map->size && host->domain) { 665 iommu_unmap(host->domain, job->addr_phys[i], map->size); 666 free_iova(&host->iova, iova_pfn(&host->iova, job->addr_phys[i])); 667 } 668 669 host1x_bo_unpin(map); 670 host1x_bo_put(bo); 671 } 672 673 job->num_unpins = 0; 674 675 if (job->gather_copy_size) 676 dma_free_wc(host->dev, job->gather_copy_size, 677 job->gather_copy_mapped, job->gather_copy); 678 } 679 EXPORT_SYMBOL(host1x_job_unpin); 680 681 /* 682 * Debug routine used to dump job entries 683 */ 684 void host1x_job_dump(struct device *dev, struct host1x_job *job) 685 { 686 dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt->id); 687 dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end); 688 dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get); 689 dev_dbg(dev, " TIMEOUT %d\n", job->timeout); 690 dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots); 691 dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins); 692 } 693