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