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 #include <linux/platform_device.h> 13 14 #include "compat.h" 15 #include "ctrl.h" 16 #include "regs.h" 17 #include "jr.h" 18 #include "desc.h" 19 #include "intern.h" 20 21 struct jr_driver_data { 22 /* List of Physical JobR's with the Driver */ 23 struct list_head jr_list; 24 spinlock_t jr_alloc_lock; /* jr_list lock */ 25 } ____cacheline_aligned; 26 27 static struct jr_driver_data driver_data; 28 static DEFINE_MUTEX(algs_lock); 29 static unsigned int active_devs; 30 31 static void register_algs(struct caam_drv_private_jr *jrpriv, 32 struct device *dev) 33 { 34 mutex_lock(&algs_lock); 35 36 if (++active_devs != 1) 37 goto algs_unlock; 38 39 caam_algapi_init(dev); 40 caam_algapi_hash_init(dev); 41 caam_pkc_init(dev); 42 jrpriv->hwrng = !caam_rng_init(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_pkc_exit(); 58 caam_algapi_hash_exit(); 59 caam_algapi_exit(); 60 61 algs_unlock: 62 mutex_unlock(&algs_lock); 63 } 64 65 static void caam_jr_crypto_engine_exit(void *data) 66 { 67 struct device *jrdev = data; 68 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev); 69 70 /* Free the resources of crypto-engine */ 71 crypto_engine_exit(jrpriv->engine); 72 } 73 74 /* 75 * Put the CAAM in quiesce, ie stop 76 * 77 * Must be called with itr disabled 78 */ 79 static int caam_jr_stop_processing(struct device *dev, u32 jrcr_bits) 80 { 81 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 82 unsigned int timeout = 100000; 83 84 /* Check the current status */ 85 if (rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_INPROGRESS) 86 goto wait_quiesce_completion; 87 88 /* Reset the field */ 89 clrsetbits_32(&jrp->rregs->jrintstatus, JRINT_ERR_HALT_MASK, 0); 90 91 /* initiate flush / park (required prior to reset) */ 92 wr_reg32(&jrp->rregs->jrcommand, jrcr_bits); 93 94 wait_quiesce_completion: 95 while (((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) == 96 JRINT_ERR_HALT_INPROGRESS) && --timeout) 97 cpu_relax(); 98 99 if ((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) != 100 JRINT_ERR_HALT_COMPLETE || timeout == 0) { 101 dev_err(dev, "failed to flush job ring %d\n", jrp->ridx); 102 return -EIO; 103 } 104 105 return 0; 106 } 107 108 /* 109 * Flush the job ring, so the jobs running will be stopped, jobs queued will be 110 * invalidated and the CAAM will no longer fetch fron input ring. 111 * 112 * Must be called with itr disabled 113 */ 114 static int caam_jr_flush(struct device *dev) 115 { 116 return caam_jr_stop_processing(dev, JRCR_RESET); 117 } 118 119 /* The resume can be used after a park or a flush if CAAM has not been reset */ 120 static int caam_jr_restart_processing(struct device *dev) 121 { 122 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 123 u32 halt_status = rd_reg32(&jrp->rregs->jrintstatus) & 124 JRINT_ERR_HALT_MASK; 125 126 /* Check that the flush/park is completed */ 127 if (halt_status != JRINT_ERR_HALT_COMPLETE) 128 return -1; 129 130 /* Resume processing of jobs */ 131 clrsetbits_32(&jrp->rregs->jrintstatus, 0, JRINT_ERR_HALT_COMPLETE); 132 133 return 0; 134 } 135 136 static int caam_reset_hw_jr(struct device *dev) 137 { 138 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 139 unsigned int timeout = 100000; 140 int err; 141 /* 142 * mask interrupts since we are going to poll 143 * for reset completion status 144 */ 145 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JRCFG_IMSK); 146 err = caam_jr_flush(dev); 147 if (err) 148 return err; 149 150 /* initiate reset */ 151 wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET); 152 while ((rd_reg32(&jrp->rregs->jrcommand) & JRCR_RESET) && --timeout) 153 cpu_relax(); 154 155 if (timeout == 0) { 156 dev_err(dev, "failed to reset job ring %d\n", jrp->ridx); 157 return -EIO; 158 } 159 160 /* unmask interrupts */ 161 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0); 162 163 return 0; 164 } 165 166 /* 167 * Shutdown JobR independent of platform property code 168 */ 169 static int caam_jr_shutdown(struct device *dev) 170 { 171 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 172 int ret; 173 174 ret = caam_reset_hw_jr(dev); 175 176 tasklet_kill(&jrp->irqtask); 177 178 return ret; 179 } 180 181 static void caam_jr_remove(struct platform_device *pdev) 182 { 183 int ret; 184 struct device *jrdev; 185 struct caam_drv_private_jr *jrpriv; 186 187 jrdev = &pdev->dev; 188 jrpriv = dev_get_drvdata(jrdev); 189 190 if (jrpriv->hwrng) 191 caam_rng_exit(jrdev->parent); 192 193 /* 194 * If a job ring is still allocated there is trouble ahead. Once 195 * caam_jr_remove() returned, jrpriv will be freed and the registers 196 * will get unmapped. So any user of such a job ring will probably 197 * crash. 198 */ 199 if (atomic_read(&jrpriv->tfm_count)) { 200 dev_alert(jrdev, "Device is busy; consumers might start to crash\n"); 201 return; 202 } 203 204 /* Unregister JR-based RNG & crypto algorithms */ 205 unregister_algs(); 206 207 /* Remove the node from Physical JobR list maintained by driver */ 208 spin_lock(&driver_data.jr_alloc_lock); 209 list_del(&jrpriv->list_node); 210 spin_unlock(&driver_data.jr_alloc_lock); 211 212 /* Release ring */ 213 ret = caam_jr_shutdown(jrdev); 214 if (ret) 215 dev_err(jrdev, "Failed to shut down job ring\n"); 216 } 217 218 /* Main per-ring interrupt handler */ 219 static irqreturn_t caam_jr_interrupt(int irq, void *st_dev) 220 { 221 struct device *dev = st_dev; 222 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 223 u32 irqstate; 224 225 /* 226 * Check the output ring for ready responses, kick 227 * tasklet if jobs done. 228 */ 229 irqstate = rd_reg32(&jrp->rregs->jrintstatus); 230 if (!(irqstate & JRINT_JR_INT)) 231 return IRQ_NONE; 232 233 /* 234 * If JobR error, we got more development work to do 235 * Flag a bug now, but we really need to shut down and 236 * restart the queue (and fix code). 237 */ 238 if (irqstate & JRINT_JR_ERROR) { 239 dev_err(dev, "job ring error: irqstate: %08x\n", irqstate); 240 BUG(); 241 } 242 243 /* mask valid interrupts */ 244 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JRCFG_IMSK); 245 246 /* Have valid interrupt at this point, just ACK and trigger */ 247 wr_reg32(&jrp->rregs->jrintstatus, irqstate); 248 249 preempt_disable(); 250 tasklet_schedule(&jrp->irqtask); 251 preempt_enable(); 252 253 return IRQ_HANDLED; 254 } 255 256 /* Deferred service handler, run as interrupt-fired tasklet */ 257 static void caam_jr_dequeue(unsigned long devarg) 258 { 259 int hw_idx, sw_idx, i, head, tail; 260 struct caam_jr_dequeue_params *params = (void *)devarg; 261 struct device *dev = params->dev; 262 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 263 void (*usercall)(struct device *dev, u32 *desc, u32 status, void *arg); 264 u32 *userdesc, userstatus; 265 void *userarg; 266 u32 outring_used = 0; 267 268 while (outring_used || 269 (outring_used = rd_reg32(&jrp->rregs->outring_used))) { 270 271 head = READ_ONCE(jrp->head); 272 273 sw_idx = tail = jrp->tail; 274 hw_idx = jrp->out_ring_read_index; 275 276 for (i = 0; CIRC_CNT(head, tail + i, JOBR_DEPTH) >= 1; i++) { 277 sw_idx = (tail + i) & (JOBR_DEPTH - 1); 278 279 if (jr_outentry_desc(jrp->outring, hw_idx) == 280 caam_dma_to_cpu(jrp->entinfo[sw_idx].desc_addr_dma)) 281 break; /* found */ 282 } 283 /* we should never fail to find a matching descriptor */ 284 BUG_ON(CIRC_CNT(head, tail + i, JOBR_DEPTH) <= 0); 285 286 /* Unmap just-run descriptor so we can post-process */ 287 dma_unmap_single(dev, 288 caam_dma_to_cpu(jr_outentry_desc(jrp->outring, 289 hw_idx)), 290 jrp->entinfo[sw_idx].desc_size, 291 DMA_TO_DEVICE); 292 293 /* mark completed, avoid matching on a recycled desc addr */ 294 jrp->entinfo[sw_idx].desc_addr_dma = 0; 295 296 /* Stash callback params */ 297 usercall = jrp->entinfo[sw_idx].callbk; 298 userarg = jrp->entinfo[sw_idx].cbkarg; 299 userdesc = jrp->entinfo[sw_idx].desc_addr_virt; 300 userstatus = caam32_to_cpu(jr_outentry_jrstatus(jrp->outring, 301 hw_idx)); 302 303 /* 304 * Make sure all information from the job has been obtained 305 * before telling CAAM that the job has been removed from the 306 * output ring. 307 */ 308 mb(); 309 310 /* set done */ 311 wr_reg32(&jrp->rregs->outring_rmvd, 1); 312 313 jrp->out_ring_read_index = (jrp->out_ring_read_index + 1) & 314 (JOBR_DEPTH - 1); 315 316 /* 317 * if this job completed out-of-order, do not increment 318 * the tail. Otherwise, increment tail by 1 plus the 319 * number of subsequent jobs already completed out-of-order 320 */ 321 if (sw_idx == tail) { 322 do { 323 tail = (tail + 1) & (JOBR_DEPTH - 1); 324 } while (CIRC_CNT(head, tail, JOBR_DEPTH) >= 1 && 325 jrp->entinfo[tail].desc_addr_dma == 0); 326 327 jrp->tail = tail; 328 } 329 330 /* Finally, execute user's callback */ 331 usercall(dev, userdesc, userstatus, userarg); 332 outring_used--; 333 } 334 335 if (params->enable_itr) 336 /* reenable / unmask IRQs */ 337 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0); 338 } 339 340 /** 341 * caam_jr_alloc() - Alloc a job ring for someone to use as needed. 342 * 343 * returns : pointer to the newly allocated physical 344 * JobR dev can be written to if successful. 345 **/ 346 struct device *caam_jr_alloc(void) 347 { 348 struct caam_drv_private_jr *jrpriv, *min_jrpriv = NULL; 349 struct device *dev = ERR_PTR(-ENODEV); 350 int min_tfm_cnt = INT_MAX; 351 int tfm_cnt; 352 353 spin_lock(&driver_data.jr_alloc_lock); 354 355 if (list_empty(&driver_data.jr_list)) { 356 spin_unlock(&driver_data.jr_alloc_lock); 357 return ERR_PTR(-ENODEV); 358 } 359 360 list_for_each_entry(jrpriv, &driver_data.jr_list, list_node) { 361 tfm_cnt = atomic_read(&jrpriv->tfm_count); 362 if (tfm_cnt < min_tfm_cnt) { 363 min_tfm_cnt = tfm_cnt; 364 min_jrpriv = jrpriv; 365 } 366 if (!min_tfm_cnt) 367 break; 368 } 369 370 if (min_jrpriv) { 371 atomic_inc(&min_jrpriv->tfm_count); 372 dev = min_jrpriv->dev; 373 } 374 spin_unlock(&driver_data.jr_alloc_lock); 375 376 return dev; 377 } 378 EXPORT_SYMBOL(caam_jr_alloc); 379 380 /** 381 * caam_jr_free() - Free the Job Ring 382 * @rdev: points to the dev that identifies the Job ring to 383 * be released. 384 **/ 385 void caam_jr_free(struct device *rdev) 386 { 387 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(rdev); 388 389 atomic_dec(&jrpriv->tfm_count); 390 } 391 EXPORT_SYMBOL(caam_jr_free); 392 393 /** 394 * caam_jr_enqueue() - Enqueue a job descriptor head. Returns -EINPROGRESS 395 * if OK, -ENOSPC if the queue is full, -EIO if it cannot map the caller's 396 * descriptor. 397 * @dev: struct device of the job ring to be used 398 * @desc: points to a job descriptor that execute our request. All 399 * descriptors (and all referenced data) must be in a DMAable 400 * region, and all data references must be physical addresses 401 * accessible to CAAM (i.e. within a PAMU window granted 402 * to it). 403 * @cbk: pointer to a callback function to be invoked upon completion 404 * of this request. This has the form: 405 * callback(struct device *dev, u32 *desc, u32 stat, void *arg) 406 * where: 407 * dev: contains the job ring device that processed this 408 * response. 409 * desc: descriptor that initiated the request, same as 410 * "desc" being argued to caam_jr_enqueue(). 411 * status: untranslated status received from CAAM. See the 412 * reference manual for a detailed description of 413 * error meaning, or see the JRSTA definitions in the 414 * register header file 415 * areq: optional pointer to an argument passed with the 416 * original request 417 * @areq: optional pointer to a user argument for use at callback 418 * time. 419 **/ 420 int caam_jr_enqueue(struct device *dev, u32 *desc, 421 void (*cbk)(struct device *dev, u32 *desc, 422 u32 status, void *areq), 423 void *areq) 424 { 425 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 426 struct caam_jrentry_info *head_entry; 427 int head, tail, desc_size; 428 dma_addr_t desc_dma; 429 430 desc_size = (caam32_to_cpu(*desc) & HDR_JD_LENGTH_MASK) * sizeof(u32); 431 desc_dma = dma_map_single(dev, desc, desc_size, DMA_TO_DEVICE); 432 if (dma_mapping_error(dev, desc_dma)) { 433 dev_err(dev, "caam_jr_enqueue(): can't map jobdesc\n"); 434 return -EIO; 435 } 436 437 spin_lock_bh(&jrp->inplock); 438 439 head = jrp->head; 440 tail = READ_ONCE(jrp->tail); 441 442 if (!jrp->inpring_avail || 443 CIRC_SPACE(head, tail, JOBR_DEPTH) <= 0) { 444 spin_unlock_bh(&jrp->inplock); 445 dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE); 446 return -ENOSPC; 447 } 448 449 head_entry = &jrp->entinfo[head]; 450 head_entry->desc_addr_virt = desc; 451 head_entry->desc_size = desc_size; 452 head_entry->callbk = (void *)cbk; 453 head_entry->cbkarg = areq; 454 head_entry->desc_addr_dma = desc_dma; 455 456 jr_inpentry_set(jrp->inpring, head, cpu_to_caam_dma(desc_dma)); 457 458 /* 459 * Guarantee that the descriptor's DMA address has been written to 460 * the next slot in the ring before the write index is updated, since 461 * other cores may update this index independently. 462 * 463 * Under heavy DDR load, smp_wmb() or dma_wmb() fail to make the input 464 * ring be updated before the CAAM starts reading it. So, CAAM will 465 * process, again, an old descriptor address and will put it in the 466 * output ring. This will make caam_jr_dequeue() to fail, since this 467 * old descriptor is not in the software ring. 468 * To fix this, use wmb() which works on the full system instead of 469 * inner/outer shareable domains. 470 */ 471 wmb(); 472 473 jrp->head = (head + 1) & (JOBR_DEPTH - 1); 474 475 /* 476 * Ensure that all job information has been written before 477 * notifying CAAM that a new job was added to the input ring 478 * using a memory barrier. The wr_reg32() uses api iowrite32() 479 * to do the register write. iowrite32() issues a memory barrier 480 * before the write operation. 481 */ 482 483 wr_reg32(&jrp->rregs->inpring_jobadd, 1); 484 485 jrp->inpring_avail--; 486 if (!jrp->inpring_avail) 487 jrp->inpring_avail = rd_reg32(&jrp->rregs->inpring_avail); 488 489 spin_unlock_bh(&jrp->inplock); 490 491 return -EINPROGRESS; 492 } 493 EXPORT_SYMBOL(caam_jr_enqueue); 494 495 static void caam_jr_init_hw(struct device *dev, dma_addr_t inpbusaddr, 496 dma_addr_t outbusaddr) 497 { 498 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 499 500 wr_reg64(&jrp->rregs->inpring_base, inpbusaddr); 501 wr_reg64(&jrp->rregs->outring_base, outbusaddr); 502 wr_reg32(&jrp->rregs->inpring_size, JOBR_DEPTH); 503 wr_reg32(&jrp->rregs->outring_size, JOBR_DEPTH); 504 505 /* Select interrupt coalescing parameters */ 506 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JOBR_INTC | 507 (JOBR_INTC_COUNT_THLD << JRCFG_ICDCT_SHIFT) | 508 (JOBR_INTC_TIME_THLD << JRCFG_ICTT_SHIFT)); 509 } 510 511 static void caam_jr_reset_index(struct caam_drv_private_jr *jrp) 512 { 513 jrp->out_ring_read_index = 0; 514 jrp->head = 0; 515 jrp->tail = 0; 516 } 517 518 /* 519 * Init JobR independent of platform property detection 520 */ 521 static int caam_jr_init(struct device *dev) 522 { 523 struct caam_drv_private_jr *jrp; 524 dma_addr_t inpbusaddr, outbusaddr; 525 int i, error; 526 527 jrp = dev_get_drvdata(dev); 528 529 error = caam_reset_hw_jr(dev); 530 if (error) 531 return error; 532 533 jrp->inpring = dmam_alloc_coherent(dev, SIZEOF_JR_INPENTRY * 534 JOBR_DEPTH, &inpbusaddr, 535 GFP_KERNEL); 536 if (!jrp->inpring) 537 return -ENOMEM; 538 539 jrp->outring = dmam_alloc_coherent(dev, SIZEOF_JR_OUTENTRY * 540 JOBR_DEPTH, &outbusaddr, 541 GFP_KERNEL); 542 if (!jrp->outring) 543 return -ENOMEM; 544 545 jrp->entinfo = devm_kzalloc_objs(dev, *jrp->entinfo, JOBR_DEPTH); 546 if (!jrp->entinfo) 547 return -ENOMEM; 548 549 for (i = 0; i < JOBR_DEPTH; i++) 550 jrp->entinfo[i].desc_addr_dma = !0; 551 552 /* Setup rings */ 553 caam_jr_reset_index(jrp); 554 jrp->inpring_avail = JOBR_DEPTH; 555 caam_jr_init_hw(dev, inpbusaddr, outbusaddr); 556 557 spin_lock_init(&jrp->inplock); 558 559 jrp->tasklet_params.dev = dev; 560 jrp->tasklet_params.enable_itr = 1; 561 tasklet_init(&jrp->irqtask, caam_jr_dequeue, 562 (unsigned long)&jrp->tasklet_params); 563 564 /* Connect job ring interrupt handler. */ 565 error = devm_request_irq(dev, jrp->irq, caam_jr_interrupt, IRQF_SHARED, 566 dev_name(dev), dev); 567 if (error) { 568 dev_err(dev, "can't connect JobR %d interrupt (%d)\n", 569 jrp->ridx, jrp->irq); 570 tasklet_kill(&jrp->irqtask); 571 } 572 573 return error; 574 } 575 576 /* 577 * Probe routine for each detected JobR subsystem. 578 */ 579 static int caam_jr_probe(struct platform_device *pdev) 580 { 581 struct device *jrdev; 582 struct caam_drv_private_jr *jrpriv; 583 static int total_jobrs; 584 void __iomem *ctrl; 585 struct resource *r; 586 int error; 587 int irq; 588 589 /* 590 * The job rings live inside the register window of their parent 591 * fsl,sec-v4.0 node, which caam_probe() already reserves (and maps) 592 * via devm_of_iomap(). A requested region that overlaps that 593 * reservation, e.g. from devm_platform_ioremap_resource(), would 594 * therefore fail with -EBUSY, so map the registers without claiming 595 * the region here. 596 */ 597 r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 598 if (!r) { 599 dev_err(&pdev->dev, "platform_get_resource() failed\n"); 600 return -EINVAL; 601 } 602 603 ctrl = devm_ioremap(&pdev->dev, r->start, resource_size(r)); 604 if (!ctrl) { 605 dev_err(&pdev->dev, "devm_ioremap() failed\n"); 606 return -ENOMEM; 607 } 608 609 irq = platform_get_irq(pdev, 0); 610 if (irq < 0) 611 return irq; 612 613 jrdev = &pdev->dev; 614 jrpriv = devm_kzalloc_obj(jrdev, *jrpriv); 615 if (!jrpriv) 616 return -ENOMEM; 617 618 dev_set_drvdata(jrdev, jrpriv); 619 620 /* save ring identity relative to detection */ 621 jrpriv->ridx = total_jobrs++; 622 623 jrpriv->rregs = ctrl; 624 625 error = dma_set_mask_and_coherent(jrdev, caam_get_dma_mask(jrdev)); 626 if (error) { 627 dev_err(jrdev, "dma_set_mask_and_coherent failed (%d)\n", 628 error); 629 return error; 630 } 631 632 /* Initialize crypto engine */ 633 jrpriv->engine = crypto_engine_alloc_init_and_set(jrdev, true, false, 634 CRYPTO_ENGINE_MAX_QLEN); 635 if (!jrpriv->engine) { 636 dev_err(jrdev, "Could not init crypto-engine\n"); 637 return -ENOMEM; 638 } 639 640 error = devm_add_action_or_reset(jrdev, caam_jr_crypto_engine_exit, 641 jrdev); 642 if (error) 643 return error; 644 645 /* Start crypto engine */ 646 error = crypto_engine_start(jrpriv->engine); 647 if (error) { 648 dev_err(jrdev, "Could not start crypto-engine\n"); 649 return error; 650 } 651 652 /* Identify the interrupt */ 653 jrpriv->irq = irq; 654 655 /* Now do the platform independent part */ 656 error = caam_jr_init(jrdev); /* now turn on hardware */ 657 if (error) 658 return error; 659 660 jrpriv->dev = jrdev; 661 spin_lock(&driver_data.jr_alloc_lock); 662 list_add_tail(&jrpriv->list_node, &driver_data.jr_list); 663 spin_unlock(&driver_data.jr_alloc_lock); 664 665 atomic_set(&jrpriv->tfm_count, 0); 666 667 device_init_wakeup(&pdev->dev, 1); 668 device_set_wakeup_enable(&pdev->dev, false); 669 670 register_algs(jrpriv, jrdev->parent); 671 672 return 0; 673 } 674 675 static void caam_jr_get_hw_state(struct device *dev) 676 { 677 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 678 679 jrp->state.inpbusaddr = rd_reg64(&jrp->rregs->inpring_base); 680 jrp->state.outbusaddr = rd_reg64(&jrp->rregs->outring_base); 681 } 682 683 static int caam_jr_suspend(struct device *dev) 684 { 685 struct platform_device *pdev = to_platform_device(dev); 686 struct caam_drv_private_jr *jrpriv = platform_get_drvdata(pdev); 687 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev->parent); 688 struct caam_jr_dequeue_params suspend_params = { 689 .dev = dev, 690 .enable_itr = 0, 691 }; 692 693 /* Remove the node from Physical JobR list maintained by driver */ 694 spin_lock(&driver_data.jr_alloc_lock); 695 list_del(&jrpriv->list_node); 696 spin_unlock(&driver_data.jr_alloc_lock); 697 698 if (jrpriv->hwrng) 699 caam_rng_exit(dev->parent); 700 701 if (ctrlpriv->caam_off_during_pm) { 702 int err; 703 704 tasklet_disable(&jrpriv->irqtask); 705 706 /* mask itr to call flush */ 707 clrsetbits_32(&jrpriv->rregs->rconfig_lo, 0, JRCFG_IMSK); 708 709 /* Invalid job in process */ 710 err = caam_jr_flush(dev); 711 if (err) { 712 dev_err(dev, "Failed to flush\n"); 713 return err; 714 } 715 716 /* Dequeing jobs flushed */ 717 caam_jr_dequeue((unsigned long)&suspend_params); 718 719 /* Save state */ 720 caam_jr_get_hw_state(dev); 721 } else if (device_may_wakeup(&pdev->dev)) { 722 enable_irq_wake(jrpriv->irq); 723 } 724 725 return 0; 726 } 727 728 static int caam_jr_resume(struct device *dev) 729 { 730 struct platform_device *pdev = to_platform_device(dev); 731 struct caam_drv_private_jr *jrpriv = platform_get_drvdata(pdev); 732 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev->parent); 733 734 if (ctrlpriv->caam_off_during_pm) { 735 u64 inp_addr; 736 int err; 737 738 /* 739 * Check if the CAAM has been resetted checking the address of 740 * the input ring 741 */ 742 inp_addr = rd_reg64(&jrpriv->rregs->inpring_base); 743 if (inp_addr != 0) { 744 /* JR still has some configuration */ 745 if (inp_addr == jrpriv->state.inpbusaddr) { 746 /* JR has not been resetted */ 747 err = caam_jr_restart_processing(dev); 748 if (err) { 749 dev_err(dev, 750 "Restart processing failed\n"); 751 return err; 752 } 753 754 tasklet_enable(&jrpriv->irqtask); 755 756 clrsetbits_32(&jrpriv->rregs->rconfig_lo, 757 JRCFG_IMSK, 0); 758 759 goto add_jr; 760 } else if (ctrlpriv->optee_en) { 761 /* JR has been used by OPTEE, reset it */ 762 err = caam_reset_hw_jr(dev); 763 if (err) { 764 dev_err(dev, "Failed to reset JR\n"); 765 return err; 766 } 767 } else { 768 /* No explanation, return error */ 769 return -EIO; 770 } 771 } 772 773 caam_jr_reset_index(jrpriv); 774 caam_jr_init_hw(dev, jrpriv->state.inpbusaddr, 775 jrpriv->state.outbusaddr); 776 777 tasklet_enable(&jrpriv->irqtask); 778 } else if (device_may_wakeup(&pdev->dev)) { 779 disable_irq_wake(jrpriv->irq); 780 } 781 782 add_jr: 783 spin_lock(&driver_data.jr_alloc_lock); 784 list_add_tail(&jrpriv->list_node, &driver_data.jr_list); 785 spin_unlock(&driver_data.jr_alloc_lock); 786 787 if (jrpriv->hwrng) 788 jrpriv->hwrng = !caam_rng_init(dev->parent); 789 790 return 0; 791 } 792 793 static DEFINE_SIMPLE_DEV_PM_OPS(caam_jr_pm_ops, caam_jr_suspend, caam_jr_resume); 794 795 static const struct of_device_id caam_jr_match[] = { 796 { 797 .compatible = "fsl,sec-v4.0-job-ring", 798 }, 799 { 800 .compatible = "fsl,sec4.0-job-ring", 801 }, 802 {}, 803 }; 804 MODULE_DEVICE_TABLE(of, caam_jr_match); 805 806 static struct platform_driver caam_jr_driver = { 807 .driver = { 808 .name = "caam_jr", 809 .of_match_table = caam_jr_match, 810 .pm = pm_ptr(&caam_jr_pm_ops), 811 }, 812 .probe = caam_jr_probe, 813 .remove = caam_jr_remove, 814 .shutdown = caam_jr_remove, 815 }; 816 817 static int __init jr_driver_init(void) 818 { 819 spin_lock_init(&driver_data.jr_alloc_lock); 820 INIT_LIST_HEAD(&driver_data.jr_list); 821 return platform_driver_register(&caam_jr_driver); 822 } 823 824 static void __exit jr_driver_exit(void) 825 { 826 platform_driver_unregister(&caam_jr_driver); 827 } 828 829 module_init(jr_driver_init); 830 module_exit(jr_driver_exit); 831 832 MODULE_LICENSE("GPL"); 833 MODULE_DESCRIPTION("FSL CAAM JR request backend"); 834 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); 835