1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * CAAM/SEC 4.x transport/backend driver 4 * JobR backend functionality 5 * 6 * Copyright 2008-2012 Freescale Semiconductor, Inc. 7 * Copyright 2019, 2023 NXP 8 */ 9 10 #include <linux/of_irq.h> 11 #include <linux/of_address.h> 12 13 #include "compat.h" 14 #include "ctrl.h" 15 #include "regs.h" 16 #include "jr.h" 17 #include "desc.h" 18 #include "intern.h" 19 20 struct jr_driver_data { 21 /* List of Physical JobR's with the Driver */ 22 struct list_head jr_list; 23 spinlock_t jr_alloc_lock; /* jr_list lock */ 24 } ____cacheline_aligned; 25 26 static struct jr_driver_data driver_data; 27 static DEFINE_MUTEX(algs_lock); 28 static unsigned int active_devs; 29 30 static void register_algs(struct caam_drv_private_jr *jrpriv, 31 struct device *dev) 32 { 33 mutex_lock(&algs_lock); 34 35 if (++active_devs != 1) 36 goto algs_unlock; 37 38 caam_algapi_init(dev); 39 caam_algapi_hash_init(dev); 40 caam_pkc_init(dev); 41 jrpriv->hwrng = !caam_rng_init(dev); 42 caam_prng_register(dev); 43 caam_qi_algapi_init(dev); 44 45 algs_unlock: 46 mutex_unlock(&algs_lock); 47 } 48 49 static void unregister_algs(void) 50 { 51 mutex_lock(&algs_lock); 52 53 if (--active_devs != 0) 54 goto algs_unlock; 55 56 caam_qi_algapi_exit(); 57 caam_prng_unregister(NULL); 58 caam_pkc_exit(); 59 caam_algapi_hash_exit(); 60 caam_algapi_exit(); 61 62 algs_unlock: 63 mutex_unlock(&algs_lock); 64 } 65 66 static void caam_jr_crypto_engine_exit(void *data) 67 { 68 struct device *jrdev = data; 69 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev); 70 71 /* Free the resources of crypto-engine */ 72 crypto_engine_exit(jrpriv->engine); 73 } 74 75 /* 76 * Put the CAAM in quiesce, ie stop 77 * 78 * Must be called with itr disabled 79 */ 80 static int caam_jr_stop_processing(struct device *dev, u32 jrcr_bits) 81 { 82 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 83 unsigned int timeout = 100000; 84 85 /* Check the current status */ 86 if (rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_INPROGRESS) 87 goto wait_quiesce_completion; 88 89 /* Reset the field */ 90 clrsetbits_32(&jrp->rregs->jrintstatus, JRINT_ERR_HALT_MASK, 0); 91 92 /* initiate flush / park (required prior to reset) */ 93 wr_reg32(&jrp->rregs->jrcommand, jrcr_bits); 94 95 wait_quiesce_completion: 96 while (((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) == 97 JRINT_ERR_HALT_INPROGRESS) && --timeout) 98 cpu_relax(); 99 100 if ((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) != 101 JRINT_ERR_HALT_COMPLETE || timeout == 0) { 102 dev_err(dev, "failed to flush job ring %d\n", jrp->ridx); 103 return -EIO; 104 } 105 106 return 0; 107 } 108 109 /* 110 * Flush the job ring, so the jobs running will be stopped, jobs queued will be 111 * invalidated and the CAAM will no longer fetch fron input ring. 112 * 113 * Must be called with itr disabled 114 */ 115 static int caam_jr_flush(struct device *dev) 116 { 117 return caam_jr_stop_processing(dev, JRCR_RESET); 118 } 119 120 static int caam_reset_hw_jr(struct device *dev) 121 { 122 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 123 unsigned int timeout = 100000; 124 int err; 125 /* 126 * mask interrupts since we are going to poll 127 * for reset completion status 128 */ 129 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JRCFG_IMSK); 130 err = caam_jr_flush(dev); 131 if (err) 132 return err; 133 134 /* initiate reset */ 135 wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET); 136 while ((rd_reg32(&jrp->rregs->jrcommand) & JRCR_RESET) && --timeout) 137 cpu_relax(); 138 139 if (timeout == 0) { 140 dev_err(dev, "failed to reset job ring %d\n", jrp->ridx); 141 return -EIO; 142 } 143 144 /* unmask interrupts */ 145 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0); 146 147 return 0; 148 } 149 150 /* 151 * Shutdown JobR independent of platform property code 152 */ 153 static int caam_jr_shutdown(struct device *dev) 154 { 155 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 156 int ret; 157 158 ret = caam_reset_hw_jr(dev); 159 160 tasklet_kill(&jrp->irqtask); 161 162 return ret; 163 } 164 165 static int caam_jr_remove(struct platform_device *pdev) 166 { 167 int ret; 168 struct device *jrdev; 169 struct caam_drv_private_jr *jrpriv; 170 171 jrdev = &pdev->dev; 172 jrpriv = dev_get_drvdata(jrdev); 173 174 if (jrpriv->hwrng) 175 caam_rng_exit(jrdev->parent); 176 177 /* 178 * Return EBUSY if job ring already allocated. 179 */ 180 if (atomic_read(&jrpriv->tfm_count)) { 181 dev_err(jrdev, "Device is busy\n"); 182 return -EBUSY; 183 } 184 185 /* Unregister JR-based RNG & crypto algorithms */ 186 unregister_algs(); 187 188 /* Remove the node from Physical JobR list maintained by driver */ 189 spin_lock(&driver_data.jr_alloc_lock); 190 list_del(&jrpriv->list_node); 191 spin_unlock(&driver_data.jr_alloc_lock); 192 193 /* Release ring */ 194 ret = caam_jr_shutdown(jrdev); 195 if (ret) 196 dev_err(jrdev, "Failed to shut down job ring\n"); 197 198 return ret; 199 } 200 201 /* Main per-ring interrupt handler */ 202 static irqreturn_t caam_jr_interrupt(int irq, void *st_dev) 203 { 204 struct device *dev = st_dev; 205 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 206 u32 irqstate; 207 208 /* 209 * Check the output ring for ready responses, kick 210 * tasklet if jobs done. 211 */ 212 irqstate = rd_reg32(&jrp->rregs->jrintstatus); 213 if (!irqstate) 214 return IRQ_NONE; 215 216 /* 217 * If JobR error, we got more development work to do 218 * Flag a bug now, but we really need to shut down and 219 * restart the queue (and fix code). 220 */ 221 if (irqstate & JRINT_JR_ERROR) { 222 dev_err(dev, "job ring error: irqstate: %08x\n", irqstate); 223 BUG(); 224 } 225 226 /* mask valid interrupts */ 227 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JRCFG_IMSK); 228 229 /* Have valid interrupt at this point, just ACK and trigger */ 230 wr_reg32(&jrp->rregs->jrintstatus, irqstate); 231 232 preempt_disable(); 233 tasklet_schedule(&jrp->irqtask); 234 preempt_enable(); 235 236 return IRQ_HANDLED; 237 } 238 239 /* Deferred service handler, run as interrupt-fired tasklet */ 240 static void caam_jr_dequeue(unsigned long devarg) 241 { 242 int hw_idx, sw_idx, i, head, tail; 243 struct device *dev = (struct device *)devarg; 244 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 245 void (*usercall)(struct device *dev, u32 *desc, u32 status, void *arg); 246 u32 *userdesc, userstatus; 247 void *userarg; 248 u32 outring_used = 0; 249 250 while (outring_used || 251 (outring_used = rd_reg32(&jrp->rregs->outring_used))) { 252 253 head = READ_ONCE(jrp->head); 254 255 sw_idx = tail = jrp->tail; 256 hw_idx = jrp->out_ring_read_index; 257 258 for (i = 0; CIRC_CNT(head, tail + i, JOBR_DEPTH) >= 1; i++) { 259 sw_idx = (tail + i) & (JOBR_DEPTH - 1); 260 261 if (jr_outentry_desc(jrp->outring, hw_idx) == 262 caam_dma_to_cpu(jrp->entinfo[sw_idx].desc_addr_dma)) 263 break; /* found */ 264 } 265 /* we should never fail to find a matching descriptor */ 266 BUG_ON(CIRC_CNT(head, tail + i, JOBR_DEPTH) <= 0); 267 268 /* Unmap just-run descriptor so we can post-process */ 269 dma_unmap_single(dev, 270 caam_dma_to_cpu(jr_outentry_desc(jrp->outring, 271 hw_idx)), 272 jrp->entinfo[sw_idx].desc_size, 273 DMA_TO_DEVICE); 274 275 /* mark completed, avoid matching on a recycled desc addr */ 276 jrp->entinfo[sw_idx].desc_addr_dma = 0; 277 278 /* Stash callback params */ 279 usercall = jrp->entinfo[sw_idx].callbk; 280 userarg = jrp->entinfo[sw_idx].cbkarg; 281 userdesc = jrp->entinfo[sw_idx].desc_addr_virt; 282 userstatus = caam32_to_cpu(jr_outentry_jrstatus(jrp->outring, 283 hw_idx)); 284 285 /* 286 * Make sure all information from the job has been obtained 287 * before telling CAAM that the job has been removed from the 288 * output ring. 289 */ 290 mb(); 291 292 /* set done */ 293 wr_reg32(&jrp->rregs->outring_rmvd, 1); 294 295 jrp->out_ring_read_index = (jrp->out_ring_read_index + 1) & 296 (JOBR_DEPTH - 1); 297 298 /* 299 * if this job completed out-of-order, do not increment 300 * the tail. Otherwise, increment tail by 1 plus the 301 * number of subsequent jobs already completed out-of-order 302 */ 303 if (sw_idx == tail) { 304 do { 305 tail = (tail + 1) & (JOBR_DEPTH - 1); 306 } while (CIRC_CNT(head, tail, JOBR_DEPTH) >= 1 && 307 jrp->entinfo[tail].desc_addr_dma == 0); 308 309 jrp->tail = tail; 310 } 311 312 /* Finally, execute user's callback */ 313 usercall(dev, userdesc, userstatus, userarg); 314 outring_used--; 315 } 316 317 /* reenable / unmask IRQs */ 318 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0); 319 } 320 321 /** 322 * caam_jr_alloc() - Alloc a job ring for someone to use as needed. 323 * 324 * returns : pointer to the newly allocated physical 325 * JobR dev can be written to if successful. 326 **/ 327 struct device *caam_jr_alloc(void) 328 { 329 struct caam_drv_private_jr *jrpriv, *min_jrpriv = NULL; 330 struct device *dev = ERR_PTR(-ENODEV); 331 int min_tfm_cnt = INT_MAX; 332 int tfm_cnt; 333 334 spin_lock(&driver_data.jr_alloc_lock); 335 336 if (list_empty(&driver_data.jr_list)) { 337 spin_unlock(&driver_data.jr_alloc_lock); 338 return ERR_PTR(-ENODEV); 339 } 340 341 list_for_each_entry(jrpriv, &driver_data.jr_list, list_node) { 342 tfm_cnt = atomic_read(&jrpriv->tfm_count); 343 if (tfm_cnt < min_tfm_cnt) { 344 min_tfm_cnt = tfm_cnt; 345 min_jrpriv = jrpriv; 346 } 347 if (!min_tfm_cnt) 348 break; 349 } 350 351 if (min_jrpriv) { 352 atomic_inc(&min_jrpriv->tfm_count); 353 dev = min_jrpriv->dev; 354 } 355 spin_unlock(&driver_data.jr_alloc_lock); 356 357 return dev; 358 } 359 EXPORT_SYMBOL(caam_jr_alloc); 360 361 /** 362 * caam_jr_free() - Free the Job Ring 363 * @rdev: points to the dev that identifies the Job ring to 364 * be released. 365 **/ 366 void caam_jr_free(struct device *rdev) 367 { 368 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(rdev); 369 370 atomic_dec(&jrpriv->tfm_count); 371 } 372 EXPORT_SYMBOL(caam_jr_free); 373 374 /** 375 * caam_jr_enqueue() - Enqueue a job descriptor head. Returns -EINPROGRESS 376 * if OK, -ENOSPC if the queue is full, -EIO if it cannot map the caller's 377 * descriptor. 378 * @dev: struct device of the job ring to be used 379 * @desc: points to a job descriptor that execute our request. All 380 * descriptors (and all referenced data) must be in a DMAable 381 * region, and all data references must be physical addresses 382 * accessible to CAAM (i.e. within a PAMU window granted 383 * to it). 384 * @cbk: pointer to a callback function to be invoked upon completion 385 * of this request. This has the form: 386 * callback(struct device *dev, u32 *desc, u32 stat, void *arg) 387 * where: 388 * dev: contains the job ring device that processed this 389 * response. 390 * desc: descriptor that initiated the request, same as 391 * "desc" being argued to caam_jr_enqueue(). 392 * status: untranslated status received from CAAM. See the 393 * reference manual for a detailed description of 394 * error meaning, or see the JRSTA definitions in the 395 * register header file 396 * areq: optional pointer to an argument passed with the 397 * original request 398 * @areq: optional pointer to a user argument for use at callback 399 * time. 400 **/ 401 int caam_jr_enqueue(struct device *dev, u32 *desc, 402 void (*cbk)(struct device *dev, u32 *desc, 403 u32 status, void *areq), 404 void *areq) 405 { 406 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 407 struct caam_jrentry_info *head_entry; 408 int head, tail, desc_size; 409 dma_addr_t desc_dma; 410 411 desc_size = (caam32_to_cpu(*desc) & HDR_JD_LENGTH_MASK) * sizeof(u32); 412 desc_dma = dma_map_single(dev, desc, desc_size, DMA_TO_DEVICE); 413 if (dma_mapping_error(dev, desc_dma)) { 414 dev_err(dev, "caam_jr_enqueue(): can't map jobdesc\n"); 415 return -EIO; 416 } 417 418 spin_lock_bh(&jrp->inplock); 419 420 head = jrp->head; 421 tail = READ_ONCE(jrp->tail); 422 423 if (!jrp->inpring_avail || 424 CIRC_SPACE(head, tail, JOBR_DEPTH) <= 0) { 425 spin_unlock_bh(&jrp->inplock); 426 dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE); 427 return -ENOSPC; 428 } 429 430 head_entry = &jrp->entinfo[head]; 431 head_entry->desc_addr_virt = desc; 432 head_entry->desc_size = desc_size; 433 head_entry->callbk = (void *)cbk; 434 head_entry->cbkarg = areq; 435 head_entry->desc_addr_dma = desc_dma; 436 437 jr_inpentry_set(jrp->inpring, head, cpu_to_caam_dma(desc_dma)); 438 439 /* 440 * Guarantee that the descriptor's DMA address has been written to 441 * the next slot in the ring before the write index is updated, since 442 * other cores may update this index independently. 443 */ 444 smp_wmb(); 445 446 jrp->head = (head + 1) & (JOBR_DEPTH - 1); 447 448 /* 449 * Ensure that all job information has been written before 450 * notifying CAAM that a new job was added to the input ring 451 * using a memory barrier. The wr_reg32() uses api iowrite32() 452 * to do the register write. iowrite32() issues a memory barrier 453 * before the write operation. 454 */ 455 456 wr_reg32(&jrp->rregs->inpring_jobadd, 1); 457 458 jrp->inpring_avail--; 459 if (!jrp->inpring_avail) 460 jrp->inpring_avail = rd_reg32(&jrp->rregs->inpring_avail); 461 462 spin_unlock_bh(&jrp->inplock); 463 464 return -EINPROGRESS; 465 } 466 EXPORT_SYMBOL(caam_jr_enqueue); 467 468 /* 469 * Init JobR independent of platform property detection 470 */ 471 static int caam_jr_init(struct device *dev) 472 { 473 struct caam_drv_private_jr *jrp; 474 dma_addr_t inpbusaddr, outbusaddr; 475 int i, error; 476 477 jrp = dev_get_drvdata(dev); 478 479 error = caam_reset_hw_jr(dev); 480 if (error) 481 return error; 482 483 jrp->inpring = dmam_alloc_coherent(dev, SIZEOF_JR_INPENTRY * 484 JOBR_DEPTH, &inpbusaddr, 485 GFP_KERNEL); 486 if (!jrp->inpring) 487 return -ENOMEM; 488 489 jrp->outring = dmam_alloc_coherent(dev, SIZEOF_JR_OUTENTRY * 490 JOBR_DEPTH, &outbusaddr, 491 GFP_KERNEL); 492 if (!jrp->outring) 493 return -ENOMEM; 494 495 jrp->entinfo = devm_kcalloc(dev, JOBR_DEPTH, sizeof(*jrp->entinfo), 496 GFP_KERNEL); 497 if (!jrp->entinfo) 498 return -ENOMEM; 499 500 for (i = 0; i < JOBR_DEPTH; i++) 501 jrp->entinfo[i].desc_addr_dma = !0; 502 503 /* Setup rings */ 504 jrp->out_ring_read_index = 0; 505 jrp->head = 0; 506 jrp->tail = 0; 507 508 wr_reg64(&jrp->rregs->inpring_base, inpbusaddr); 509 wr_reg64(&jrp->rregs->outring_base, outbusaddr); 510 wr_reg32(&jrp->rregs->inpring_size, JOBR_DEPTH); 511 wr_reg32(&jrp->rregs->outring_size, JOBR_DEPTH); 512 513 jrp->inpring_avail = JOBR_DEPTH; 514 515 spin_lock_init(&jrp->inplock); 516 517 /* Select interrupt coalescing parameters */ 518 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JOBR_INTC | 519 (JOBR_INTC_COUNT_THLD << JRCFG_ICDCT_SHIFT) | 520 (JOBR_INTC_TIME_THLD << JRCFG_ICTT_SHIFT)); 521 522 tasklet_init(&jrp->irqtask, caam_jr_dequeue, (unsigned long)dev); 523 524 /* Connect job ring interrupt handler. */ 525 error = devm_request_irq(dev, jrp->irq, caam_jr_interrupt, IRQF_SHARED, 526 dev_name(dev), dev); 527 if (error) { 528 dev_err(dev, "can't connect JobR %d interrupt (%d)\n", 529 jrp->ridx, jrp->irq); 530 tasklet_kill(&jrp->irqtask); 531 } 532 533 return error; 534 } 535 536 static void caam_jr_irq_dispose_mapping(void *data) 537 { 538 irq_dispose_mapping((unsigned long)data); 539 } 540 541 /* 542 * Probe routine for each detected JobR subsystem. 543 */ 544 static int caam_jr_probe(struct platform_device *pdev) 545 { 546 struct device *jrdev; 547 struct device_node *nprop; 548 struct caam_job_ring __iomem *ctrl; 549 struct caam_drv_private_jr *jrpriv; 550 static int total_jobrs; 551 struct resource *r; 552 int error; 553 554 jrdev = &pdev->dev; 555 jrpriv = devm_kzalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL); 556 if (!jrpriv) 557 return -ENOMEM; 558 559 dev_set_drvdata(jrdev, jrpriv); 560 561 /* save ring identity relative to detection */ 562 jrpriv->ridx = total_jobrs++; 563 564 nprop = pdev->dev.of_node; 565 /* Get configuration properties from device tree */ 566 /* First, get register page */ 567 r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 568 if (!r) { 569 dev_err(jrdev, "platform_get_resource() failed\n"); 570 return -ENOMEM; 571 } 572 573 ctrl = devm_ioremap(jrdev, r->start, resource_size(r)); 574 if (!ctrl) { 575 dev_err(jrdev, "devm_ioremap() failed\n"); 576 return -ENOMEM; 577 } 578 579 jrpriv->rregs = (struct caam_job_ring __iomem __force *)ctrl; 580 581 error = dma_set_mask_and_coherent(jrdev, caam_get_dma_mask(jrdev)); 582 if (error) { 583 dev_err(jrdev, "dma_set_mask_and_coherent failed (%d)\n", 584 error); 585 return error; 586 } 587 588 /* Initialize crypto engine */ 589 jrpriv->engine = crypto_engine_alloc_init_and_set(jrdev, true, NULL, 590 false, 591 CRYPTO_ENGINE_MAX_QLEN); 592 if (!jrpriv->engine) { 593 dev_err(jrdev, "Could not init crypto-engine\n"); 594 return -ENOMEM; 595 } 596 597 error = devm_add_action_or_reset(jrdev, caam_jr_crypto_engine_exit, 598 jrdev); 599 if (error) 600 return error; 601 602 /* Start crypto engine */ 603 error = crypto_engine_start(jrpriv->engine); 604 if (error) { 605 dev_err(jrdev, "Could not start crypto-engine\n"); 606 return error; 607 } 608 609 /* Identify the interrupt */ 610 jrpriv->irq = irq_of_parse_and_map(nprop, 0); 611 if (!jrpriv->irq) { 612 dev_err(jrdev, "irq_of_parse_and_map failed\n"); 613 return -EINVAL; 614 } 615 616 error = devm_add_action_or_reset(jrdev, caam_jr_irq_dispose_mapping, 617 (void *)(unsigned long)jrpriv->irq); 618 if (error) 619 return error; 620 621 /* Now do the platform independent part */ 622 error = caam_jr_init(jrdev); /* now turn on hardware */ 623 if (error) 624 return error; 625 626 jrpriv->dev = jrdev; 627 spin_lock(&driver_data.jr_alloc_lock); 628 list_add_tail(&jrpriv->list_node, &driver_data.jr_list); 629 spin_unlock(&driver_data.jr_alloc_lock); 630 631 atomic_set(&jrpriv->tfm_count, 0); 632 633 register_algs(jrpriv, jrdev->parent); 634 635 return 0; 636 } 637 638 static const struct of_device_id caam_jr_match[] = { 639 { 640 .compatible = "fsl,sec-v4.0-job-ring", 641 }, 642 { 643 .compatible = "fsl,sec4.0-job-ring", 644 }, 645 {}, 646 }; 647 MODULE_DEVICE_TABLE(of, caam_jr_match); 648 649 static struct platform_driver caam_jr_driver = { 650 .driver = { 651 .name = "caam_jr", 652 .of_match_table = caam_jr_match, 653 }, 654 .probe = caam_jr_probe, 655 .remove = caam_jr_remove, 656 }; 657 658 static int __init jr_driver_init(void) 659 { 660 spin_lock_init(&driver_data.jr_alloc_lock); 661 INIT_LIST_HEAD(&driver_data.jr_list); 662 return platform_driver_register(&caam_jr_driver); 663 } 664 665 static void __exit jr_driver_exit(void) 666 { 667 platform_driver_unregister(&caam_jr_driver); 668 } 669 670 module_init(jr_driver_init); 671 module_exit(jr_driver_exit); 672 673 MODULE_LICENSE("GPL"); 674 MODULE_DESCRIPTION("FSL CAAM JR request backend"); 675 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); 676