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_kcalloc(dev, JOBR_DEPTH, sizeof(*jrp->entinfo), 546 GFP_KERNEL); 547 if (!jrp->entinfo) 548 return -ENOMEM; 549 550 for (i = 0; i < JOBR_DEPTH; i++) 551 jrp->entinfo[i].desc_addr_dma = !0; 552 553 /* Setup rings */ 554 caam_jr_reset_index(jrp); 555 jrp->inpring_avail = JOBR_DEPTH; 556 caam_jr_init_hw(dev, inpbusaddr, outbusaddr); 557 558 spin_lock_init(&jrp->inplock); 559 560 jrp->tasklet_params.dev = dev; 561 jrp->tasklet_params.enable_itr = 1; 562 tasklet_init(&jrp->irqtask, caam_jr_dequeue, 563 (unsigned long)&jrp->tasklet_params); 564 565 /* Connect job ring interrupt handler. */ 566 error = devm_request_irq(dev, jrp->irq, caam_jr_interrupt, IRQF_SHARED, 567 dev_name(dev), dev); 568 if (error) { 569 dev_err(dev, "can't connect JobR %d interrupt (%d)\n", 570 jrp->ridx, jrp->irq); 571 tasklet_kill(&jrp->irqtask); 572 } 573 574 return error; 575 } 576 577 /* 578 * Probe routine for each detected JobR subsystem. 579 */ 580 static int caam_jr_probe(struct platform_device *pdev) 581 { 582 struct device *jrdev; 583 struct caam_drv_private_jr *jrpriv; 584 static int total_jobrs; 585 void __iomem *ctrl; 586 int error; 587 int irq; 588 589 ctrl = devm_platform_ioremap_resource(pdev, 0); 590 if (IS_ERR(ctrl)) 591 return PTR_ERR(ctrl); 592 593 irq = platform_get_irq(pdev, 0); 594 if (irq < 0) 595 return irq; 596 597 jrdev = &pdev->dev; 598 jrpriv = devm_kzalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL); 599 if (!jrpriv) 600 return -ENOMEM; 601 602 dev_set_drvdata(jrdev, jrpriv); 603 604 /* save ring identity relative to detection */ 605 jrpriv->ridx = total_jobrs++; 606 607 jrpriv->rregs = ctrl; 608 609 error = dma_set_mask_and_coherent(jrdev, caam_get_dma_mask(jrdev)); 610 if (error) { 611 dev_err(jrdev, "dma_set_mask_and_coherent failed (%d)\n", 612 error); 613 return error; 614 } 615 616 /* Initialize crypto engine */ 617 jrpriv->engine = crypto_engine_alloc_init_and_set(jrdev, true, false, 618 CRYPTO_ENGINE_MAX_QLEN); 619 if (!jrpriv->engine) { 620 dev_err(jrdev, "Could not init crypto-engine\n"); 621 return -ENOMEM; 622 } 623 624 error = devm_add_action_or_reset(jrdev, caam_jr_crypto_engine_exit, 625 jrdev); 626 if (error) 627 return error; 628 629 /* Start crypto engine */ 630 error = crypto_engine_start(jrpriv->engine); 631 if (error) { 632 dev_err(jrdev, "Could not start crypto-engine\n"); 633 return error; 634 } 635 636 /* Identify the interrupt */ 637 jrpriv->irq = irq; 638 639 /* Now do the platform independent part */ 640 error = caam_jr_init(jrdev); /* now turn on hardware */ 641 if (error) 642 return error; 643 644 jrpriv->dev = jrdev; 645 spin_lock(&driver_data.jr_alloc_lock); 646 list_add_tail(&jrpriv->list_node, &driver_data.jr_list); 647 spin_unlock(&driver_data.jr_alloc_lock); 648 649 atomic_set(&jrpriv->tfm_count, 0); 650 651 device_init_wakeup(&pdev->dev, 1); 652 device_set_wakeup_enable(&pdev->dev, false); 653 654 register_algs(jrpriv, jrdev->parent); 655 656 return 0; 657 } 658 659 static void caam_jr_get_hw_state(struct device *dev) 660 { 661 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 662 663 jrp->state.inpbusaddr = rd_reg64(&jrp->rregs->inpring_base); 664 jrp->state.outbusaddr = rd_reg64(&jrp->rregs->outring_base); 665 } 666 667 static int caam_jr_suspend(struct device *dev) 668 { 669 struct platform_device *pdev = to_platform_device(dev); 670 struct caam_drv_private_jr *jrpriv = platform_get_drvdata(pdev); 671 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev->parent); 672 struct caam_jr_dequeue_params suspend_params = { 673 .dev = dev, 674 .enable_itr = 0, 675 }; 676 677 /* Remove the node from Physical JobR list maintained by driver */ 678 spin_lock(&driver_data.jr_alloc_lock); 679 list_del(&jrpriv->list_node); 680 spin_unlock(&driver_data.jr_alloc_lock); 681 682 if (jrpriv->hwrng) 683 caam_rng_exit(dev->parent); 684 685 if (ctrlpriv->caam_off_during_pm) { 686 int err; 687 688 tasklet_disable(&jrpriv->irqtask); 689 690 /* mask itr to call flush */ 691 clrsetbits_32(&jrpriv->rregs->rconfig_lo, 0, JRCFG_IMSK); 692 693 /* Invalid job in process */ 694 err = caam_jr_flush(dev); 695 if (err) { 696 dev_err(dev, "Failed to flush\n"); 697 return err; 698 } 699 700 /* Dequeing jobs flushed */ 701 caam_jr_dequeue((unsigned long)&suspend_params); 702 703 /* Save state */ 704 caam_jr_get_hw_state(dev); 705 } else if (device_may_wakeup(&pdev->dev)) { 706 enable_irq_wake(jrpriv->irq); 707 } 708 709 return 0; 710 } 711 712 static int caam_jr_resume(struct device *dev) 713 { 714 struct platform_device *pdev = to_platform_device(dev); 715 struct caam_drv_private_jr *jrpriv = platform_get_drvdata(pdev); 716 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev->parent); 717 718 if (ctrlpriv->caam_off_during_pm) { 719 u64 inp_addr; 720 int err; 721 722 /* 723 * Check if the CAAM has been resetted checking the address of 724 * the input ring 725 */ 726 inp_addr = rd_reg64(&jrpriv->rregs->inpring_base); 727 if (inp_addr != 0) { 728 /* JR still has some configuration */ 729 if (inp_addr == jrpriv->state.inpbusaddr) { 730 /* JR has not been resetted */ 731 err = caam_jr_restart_processing(dev); 732 if (err) { 733 dev_err(dev, 734 "Restart processing failed\n"); 735 return err; 736 } 737 738 tasklet_enable(&jrpriv->irqtask); 739 740 clrsetbits_32(&jrpriv->rregs->rconfig_lo, 741 JRCFG_IMSK, 0); 742 743 goto add_jr; 744 } else if (ctrlpriv->optee_en) { 745 /* JR has been used by OPTEE, reset it */ 746 err = caam_reset_hw_jr(dev); 747 if (err) { 748 dev_err(dev, "Failed to reset JR\n"); 749 return err; 750 } 751 } else { 752 /* No explanation, return error */ 753 return -EIO; 754 } 755 } 756 757 caam_jr_reset_index(jrpriv); 758 caam_jr_init_hw(dev, jrpriv->state.inpbusaddr, 759 jrpriv->state.outbusaddr); 760 761 tasklet_enable(&jrpriv->irqtask); 762 } else if (device_may_wakeup(&pdev->dev)) { 763 disable_irq_wake(jrpriv->irq); 764 } 765 766 add_jr: 767 spin_lock(&driver_data.jr_alloc_lock); 768 list_add_tail(&jrpriv->list_node, &driver_data.jr_list); 769 spin_unlock(&driver_data.jr_alloc_lock); 770 771 if (jrpriv->hwrng) 772 jrpriv->hwrng = !caam_rng_init(dev->parent); 773 774 return 0; 775 } 776 777 static DEFINE_SIMPLE_DEV_PM_OPS(caam_jr_pm_ops, caam_jr_suspend, caam_jr_resume); 778 779 static const struct of_device_id caam_jr_match[] = { 780 { 781 .compatible = "fsl,sec-v4.0-job-ring", 782 }, 783 { 784 .compatible = "fsl,sec4.0-job-ring", 785 }, 786 {}, 787 }; 788 MODULE_DEVICE_TABLE(of, caam_jr_match); 789 790 static struct platform_driver caam_jr_driver = { 791 .driver = { 792 .name = "caam_jr", 793 .of_match_table = caam_jr_match, 794 .pm = pm_ptr(&caam_jr_pm_ops), 795 }, 796 .probe = caam_jr_probe, 797 .remove = caam_jr_remove, 798 .shutdown = caam_jr_remove, 799 }; 800 801 static int __init jr_driver_init(void) 802 { 803 spin_lock_init(&driver_data.jr_alloc_lock); 804 INIT_LIST_HEAD(&driver_data.jr_list); 805 return platform_driver_register(&caam_jr_driver); 806 } 807 808 static void __exit jr_driver_exit(void) 809 { 810 platform_driver_unregister(&caam_jr_driver); 811 } 812 813 module_init(jr_driver_init); 814 module_exit(jr_driver_exit); 815 816 MODULE_LICENSE("GPL"); 817 MODULE_DESCRIPTION("FSL CAAM JR request backend"); 818 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); 819