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 u32 outring_used = 0; 174 175 while (outring_used || 176 (outring_used = rd_reg32(&jrp->rregs->outring_used))) { 177 178 head = READ_ONCE(jrp->head); 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 */ 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 /* Finally, execute user's callback */ 236 usercall(dev, userdesc, userstatus, userarg); 237 outring_used--; 238 } 239 240 /* reenable / unmask IRQs */ 241 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0); 242 } 243 244 /** 245 * caam_jr_alloc() - Alloc a job ring for someone to use as needed. 246 * 247 * returns : pointer to the newly allocated physical 248 * JobR dev can be written to if successful. 249 **/ 250 struct device *caam_jr_alloc(void) 251 { 252 struct caam_drv_private_jr *jrpriv, *min_jrpriv = NULL; 253 struct device *dev = ERR_PTR(-ENODEV); 254 int min_tfm_cnt = INT_MAX; 255 int tfm_cnt; 256 257 spin_lock(&driver_data.jr_alloc_lock); 258 259 if (list_empty(&driver_data.jr_list)) { 260 spin_unlock(&driver_data.jr_alloc_lock); 261 return ERR_PTR(-ENODEV); 262 } 263 264 list_for_each_entry(jrpriv, &driver_data.jr_list, list_node) { 265 tfm_cnt = atomic_read(&jrpriv->tfm_count); 266 if (tfm_cnt < min_tfm_cnt) { 267 min_tfm_cnt = tfm_cnt; 268 min_jrpriv = jrpriv; 269 } 270 if (!min_tfm_cnt) 271 break; 272 } 273 274 if (min_jrpriv) { 275 atomic_inc(&min_jrpriv->tfm_count); 276 dev = min_jrpriv->dev; 277 } 278 spin_unlock(&driver_data.jr_alloc_lock); 279 280 return dev; 281 } 282 EXPORT_SYMBOL(caam_jr_alloc); 283 284 /** 285 * caam_jr_free() - Free the Job Ring 286 * @rdev - points to the dev that identifies the Job ring to 287 * be released. 288 **/ 289 void caam_jr_free(struct device *rdev) 290 { 291 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(rdev); 292 293 atomic_dec(&jrpriv->tfm_count); 294 } 295 EXPORT_SYMBOL(caam_jr_free); 296 297 /** 298 * caam_jr_enqueue() - Enqueue a job descriptor head. Returns 0 if OK, 299 * -EBUSY if the queue is full, -EIO if it cannot map the caller's 300 * descriptor. 301 * @dev: device of the job ring to be used. This device should have 302 * been assigned prior by caam_jr_register(). 303 * @desc: points to a job descriptor that execute our request. All 304 * descriptors (and all referenced data) must be in a DMAable 305 * region, and all data references must be physical addresses 306 * accessible to CAAM (i.e. within a PAMU window granted 307 * to it). 308 * @cbk: pointer to a callback function to be invoked upon completion 309 * of this request. This has the form: 310 * callback(struct device *dev, u32 *desc, u32 stat, void *arg) 311 * where: 312 * @dev: contains the job ring device that processed this 313 * response. 314 * @desc: descriptor that initiated the request, same as 315 * "desc" being argued to caam_jr_enqueue(). 316 * @status: untranslated status received from CAAM. See the 317 * reference manual for a detailed description of 318 * error meaning, or see the JRSTA definitions in the 319 * register header file 320 * @areq: optional pointer to an argument passed with the 321 * original request 322 * @areq: optional pointer to a user argument for use at callback 323 * time. 324 **/ 325 int caam_jr_enqueue(struct device *dev, u32 *desc, 326 void (*cbk)(struct device *dev, u32 *desc, 327 u32 status, void *areq), 328 void *areq) 329 { 330 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); 331 struct caam_jrentry_info *head_entry; 332 int head, tail, desc_size; 333 dma_addr_t desc_dma; 334 335 desc_size = (caam32_to_cpu(*desc) & HDR_JD_LENGTH_MASK) * sizeof(u32); 336 desc_dma = dma_map_single(dev, desc, desc_size, DMA_TO_DEVICE); 337 if (dma_mapping_error(dev, desc_dma)) { 338 dev_err(dev, "caam_jr_enqueue(): can't map jobdesc\n"); 339 return -EIO; 340 } 341 342 spin_lock_bh(&jrp->inplock); 343 344 head = jrp->head; 345 tail = READ_ONCE(jrp->tail); 346 347 if (!jrp->inpring_avail || 348 CIRC_SPACE(head, tail, JOBR_DEPTH) <= 0) { 349 spin_unlock_bh(&jrp->inplock); 350 dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE); 351 return -EBUSY; 352 } 353 354 head_entry = &jrp->entinfo[head]; 355 head_entry->desc_addr_virt = desc; 356 head_entry->desc_size = desc_size; 357 head_entry->callbk = (void *)cbk; 358 head_entry->cbkarg = areq; 359 head_entry->desc_addr_dma = desc_dma; 360 361 jrp->inpring[head] = cpu_to_caam_dma(desc_dma); 362 363 /* 364 * Guarantee that the descriptor's DMA address has been written to 365 * the next slot in the ring before the write index is updated, since 366 * other cores may update this index independently. 367 */ 368 smp_wmb(); 369 370 jrp->head = (head + 1) & (JOBR_DEPTH - 1); 371 372 /* 373 * Ensure that all job information has been written before 374 * notifying CAAM that a new job was added to the input ring 375 * using a memory barrier. The wr_reg32() uses api iowrite32() 376 * to do the register write. iowrite32() issues a memory barrier 377 * before the write operation. 378 */ 379 380 wr_reg32(&jrp->rregs->inpring_jobadd, 1); 381 382 jrp->inpring_avail--; 383 if (!jrp->inpring_avail) 384 jrp->inpring_avail = rd_reg32(&jrp->rregs->inpring_avail); 385 386 spin_unlock_bh(&jrp->inplock); 387 388 return 0; 389 } 390 EXPORT_SYMBOL(caam_jr_enqueue); 391 392 /* 393 * Init JobR independent of platform property detection 394 */ 395 static int caam_jr_init(struct device *dev) 396 { 397 struct caam_drv_private_jr *jrp; 398 dma_addr_t inpbusaddr, outbusaddr; 399 int i, error; 400 401 jrp = dev_get_drvdata(dev); 402 403 tasklet_init(&jrp->irqtask, caam_jr_dequeue, (unsigned long)dev); 404 405 /* Connect job ring interrupt handler. */ 406 error = request_irq(jrp->irq, caam_jr_interrupt, IRQF_SHARED, 407 dev_name(dev), dev); 408 if (error) { 409 dev_err(dev, "can't connect JobR %d interrupt (%d)\n", 410 jrp->ridx, jrp->irq); 411 goto out_kill_deq; 412 } 413 414 error = caam_reset_hw_jr(dev); 415 if (error) 416 goto out_free_irq; 417 418 error = -ENOMEM; 419 jrp->inpring = dma_alloc_coherent(dev, sizeof(*jrp->inpring) * 420 JOBR_DEPTH, &inpbusaddr, GFP_KERNEL); 421 if (!jrp->inpring) 422 goto out_free_irq; 423 424 jrp->outring = dma_alloc_coherent(dev, sizeof(*jrp->outring) * 425 JOBR_DEPTH, &outbusaddr, GFP_KERNEL); 426 if (!jrp->outring) 427 goto out_free_inpring; 428 429 jrp->entinfo = kcalloc(JOBR_DEPTH, sizeof(*jrp->entinfo), GFP_KERNEL); 430 if (!jrp->entinfo) 431 goto out_free_outring; 432 433 for (i = 0; i < JOBR_DEPTH; i++) 434 jrp->entinfo[i].desc_addr_dma = !0; 435 436 /* Setup rings */ 437 jrp->out_ring_read_index = 0; 438 jrp->head = 0; 439 jrp->tail = 0; 440 441 wr_reg64(&jrp->rregs->inpring_base, inpbusaddr); 442 wr_reg64(&jrp->rregs->outring_base, outbusaddr); 443 wr_reg32(&jrp->rregs->inpring_size, JOBR_DEPTH); 444 wr_reg32(&jrp->rregs->outring_size, JOBR_DEPTH); 445 446 jrp->inpring_avail = JOBR_DEPTH; 447 448 spin_lock_init(&jrp->inplock); 449 450 /* Select interrupt coalescing parameters */ 451 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JOBR_INTC | 452 (JOBR_INTC_COUNT_THLD << JRCFG_ICDCT_SHIFT) | 453 (JOBR_INTC_TIME_THLD << JRCFG_ICTT_SHIFT)); 454 455 return 0; 456 457 out_free_outring: 458 dma_free_coherent(dev, sizeof(struct jr_outentry) * JOBR_DEPTH, 459 jrp->outring, outbusaddr); 460 out_free_inpring: 461 dma_free_coherent(dev, sizeof(dma_addr_t) * JOBR_DEPTH, 462 jrp->inpring, inpbusaddr); 463 dev_err(dev, "can't allocate job rings for %d\n", jrp->ridx); 464 out_free_irq: 465 free_irq(jrp->irq, dev); 466 out_kill_deq: 467 tasklet_kill(&jrp->irqtask); 468 return error; 469 } 470 471 472 /* 473 * Probe routine for each detected JobR subsystem. 474 */ 475 static int caam_jr_probe(struct platform_device *pdev) 476 { 477 struct device *jrdev; 478 struct device_node *nprop; 479 struct caam_job_ring __iomem *ctrl; 480 struct caam_drv_private_jr *jrpriv; 481 static int total_jobrs; 482 int error; 483 484 jrdev = &pdev->dev; 485 jrpriv = devm_kmalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL); 486 if (!jrpriv) 487 return -ENOMEM; 488 489 dev_set_drvdata(jrdev, jrpriv); 490 491 /* save ring identity relative to detection */ 492 jrpriv->ridx = total_jobrs++; 493 494 nprop = pdev->dev.of_node; 495 /* Get configuration properties from device tree */ 496 /* First, get register page */ 497 ctrl = of_iomap(nprop, 0); 498 if (!ctrl) { 499 dev_err(jrdev, "of_iomap() failed\n"); 500 return -ENOMEM; 501 } 502 503 jrpriv->rregs = (struct caam_job_ring __iomem __force *)ctrl; 504 505 if (sizeof(dma_addr_t) == sizeof(u64)) { 506 if (caam_dpaa2) 507 error = dma_set_mask_and_coherent(jrdev, 508 DMA_BIT_MASK(49)); 509 else if (of_device_is_compatible(nprop, 510 "fsl,sec-v5.0-job-ring")) 511 error = dma_set_mask_and_coherent(jrdev, 512 DMA_BIT_MASK(40)); 513 else 514 error = dma_set_mask_and_coherent(jrdev, 515 DMA_BIT_MASK(36)); 516 } else { 517 error = dma_set_mask_and_coherent(jrdev, DMA_BIT_MASK(32)); 518 } 519 if (error) { 520 dev_err(jrdev, "dma_set_mask_and_coherent failed (%d)\n", 521 error); 522 iounmap(ctrl); 523 return error; 524 } 525 526 /* Identify the interrupt */ 527 jrpriv->irq = irq_of_parse_and_map(nprop, 0); 528 529 /* Now do the platform independent part */ 530 error = caam_jr_init(jrdev); /* now turn on hardware */ 531 if (error) { 532 irq_dispose_mapping(jrpriv->irq); 533 iounmap(ctrl); 534 return error; 535 } 536 537 jrpriv->dev = jrdev; 538 spin_lock(&driver_data.jr_alloc_lock); 539 list_add_tail(&jrpriv->list_node, &driver_data.jr_list); 540 spin_unlock(&driver_data.jr_alloc_lock); 541 542 atomic_set(&jrpriv->tfm_count, 0); 543 544 return 0; 545 } 546 547 static const struct of_device_id caam_jr_match[] = { 548 { 549 .compatible = "fsl,sec-v4.0-job-ring", 550 }, 551 { 552 .compatible = "fsl,sec4.0-job-ring", 553 }, 554 {}, 555 }; 556 MODULE_DEVICE_TABLE(of, caam_jr_match); 557 558 static struct platform_driver caam_jr_driver = { 559 .driver = { 560 .name = "caam_jr", 561 .of_match_table = caam_jr_match, 562 }, 563 .probe = caam_jr_probe, 564 .remove = caam_jr_remove, 565 }; 566 567 static int __init jr_driver_init(void) 568 { 569 spin_lock_init(&driver_data.jr_alloc_lock); 570 INIT_LIST_HEAD(&driver_data.jr_list); 571 return platform_driver_register(&caam_jr_driver); 572 } 573 574 static void __exit jr_driver_exit(void) 575 { 576 platform_driver_unregister(&caam_jr_driver); 577 } 578 579 module_init(jr_driver_init); 580 module_exit(jr_driver_exit); 581 582 MODULE_LICENSE("GPL"); 583 MODULE_DESCRIPTION("FSL CAAM JR request backend"); 584 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); 585