1 // SPDX-License-Identifier: GPL-2.0+
3 * CAAM/SEC 4.x transport/backend driver
4 * JobR backend functionality
6 * Copyright 2008-2012 Freescale Semiconductor, Inc.
9 #include <linux/of_irq.h>
10 #include <linux/of_address.h>
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;
25 static struct jr_driver_data driver_data;
27 static int caam_reset_hw_jr(struct device *dev)
29 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
30 unsigned int timeout = 100000;
33 * mask interrupts since we are going to poll
34 * for reset completion status
36 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JRCFG_IMSK);
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)
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);
52 wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET);
53 while ((rd_reg32(&jrp->rregs->jrcommand) & JRCR_RESET) && --timeout)
57 dev_err(dev, "failed to reset job ring %d\n", jrp->ridx);
61 /* unmask interrupts */
62 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0);
68 * Shutdown JobR independent of platform property code
70 static int caam_jr_shutdown(struct device *dev)
72 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
73 dma_addr_t inpbusaddr, outbusaddr;
76 ret = caam_reset_hw_jr(dev);
78 tasklet_kill(&jrp->irqtask);
80 /* Release interrupt */
81 free_irq(jrp->irq, dev);
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);
95 static int caam_jr_remove(struct platform_device *pdev)
99 struct caam_drv_private_jr *jrpriv;
102 jrpriv = dev_get_drvdata(jrdev);
105 * Return EBUSY if job ring already allocated.
107 if (atomic_read(&jrpriv->tfm_count)) {
108 dev_err(jrdev, "Device is busy\n");
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);
118 ret = caam_jr_shutdown(jrdev);
120 dev_err(jrdev, "Failed to shut down job ring\n");
121 irq_dispose_mapping(jrpriv->irq);
126 /* Main per-ring interrupt handler */
127 static irqreturn_t caam_jr_interrupt(int irq, void *st_dev)
129 struct device *dev = st_dev;
130 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
134 * Check the output ring for ready responses, kick
135 * tasklet if jobs done.
137 irqstate = rd_reg32(&jrp->rregs->jrintstatus);
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).
146 if (irqstate & JRINT_JR_ERROR) {
147 dev_err(dev, "job ring error: irqstate: %08x\n", irqstate);
151 /* mask valid interrupts */
152 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JRCFG_IMSK);
154 /* Have valid interrupt at this point, just ACK and trigger */
155 wr_reg32(&jrp->rregs->jrintstatus, irqstate);
158 tasklet_schedule(&jrp->irqtask);
164 /* Deferred service handler, run as interrupt-fired tasklet */
165 static void caam_jr_dequeue(unsigned long devarg)
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;
174 while (rd_reg32(&jrp->rregs->outring_used)) {
176 head = READ_ONCE(jrp->head);
178 spin_lock(&jrp->outlock);
180 sw_idx = tail = jrp->tail;
181 hw_idx = jrp->out_ring_read_index;
183 for (i = 0; CIRC_CNT(head, tail + i, JOBR_DEPTH) >= 1; i++) {
184 sw_idx = (tail + i) & (JOBR_DEPTH - 1);
186 if (jrp->outring[hw_idx].desc ==
187 caam_dma_to_cpu(jrp->entinfo[sw_idx].desc_addr_dma))
190 /* we should never fail to find a matching descriptor */
191 BUG_ON(CIRC_CNT(head, tail + i, JOBR_DEPTH) <= 0);
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,
199 /* mark completed, avoid matching on a recycled desc addr */
200 jrp->entinfo[sw_idx].desc_addr_dma = 0;
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);
209 * Make sure all information from the job has been obtained
210 * before telling CAAM that the job has been removed from the
216 wr_reg32(&jrp->rregs->outring_rmvd, 1);
218 jrp->out_ring_read_index = (jrp->out_ring_read_index + 1) &
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
226 if (sw_idx == tail) {
228 tail = (tail + 1) & (JOBR_DEPTH - 1);
229 } while (CIRC_CNT(head, tail, JOBR_DEPTH) >= 1 &&
230 jrp->entinfo[tail].desc_addr_dma == 0);
235 spin_unlock(&jrp->outlock);
237 /* Finally, execute user's callback */
238 usercall(dev, userdesc, userstatus, userarg);
241 /* reenable / unmask IRQs */
242 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0);
246 * caam_jr_alloc() - Alloc a job ring for someone to use as needed.
248 * returns : pointer to the newly allocated physical
249 * JobR dev can be written to if successful.
251 struct device *caam_jr_alloc(void)
253 struct caam_drv_private_jr *jrpriv, *min_jrpriv = NULL;
254 struct device *dev = ERR_PTR(-ENODEV);
255 int min_tfm_cnt = INT_MAX;
258 spin_lock(&driver_data.jr_alloc_lock);
260 if (list_empty(&driver_data.jr_list)) {
261 spin_unlock(&driver_data.jr_alloc_lock);
262 return ERR_PTR(-ENODEV);
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;
276 atomic_inc(&min_jrpriv->tfm_count);
277 dev = min_jrpriv->dev;
279 spin_unlock(&driver_data.jr_alloc_lock);
283 EXPORT_SYMBOL(caam_jr_alloc);
286 * caam_jr_free() - Free the Job Ring
287 * @rdev - points to the dev that identifies the Job ring to
290 void caam_jr_free(struct device *rdev)
292 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(rdev);
294 atomic_dec(&jrpriv->tfm_count);
296 EXPORT_SYMBOL(caam_jr_free);
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
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
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)
313 * @dev: contains the job ring device that processed this
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
323 * @areq: optional pointer to a user argument for use at callback
326 int caam_jr_enqueue(struct device *dev, u32 *desc,
327 void (*cbk)(struct device *dev, u32 *desc,
328 u32 status, void *areq),
331 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
332 struct caam_jrentry_info *head_entry;
333 int head, tail, desc_size;
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");
343 spin_lock_bh(&jrp->inplock);
346 tail = READ_ONCE(jrp->tail);
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);
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;
362 jrp->inpring[jrp->inp_ring_write_index] = cpu_to_caam_dma(desc_dma);
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.
371 jrp->inp_ring_write_index = (jrp->inp_ring_write_index + 1) &
373 jrp->head = (head + 1) & (JOBR_DEPTH - 1);
376 * Ensure that all job information has been written before
377 * notifying CAAM that a new job was added to the input ring.
381 wr_reg32(&jrp->rregs->inpring_jobadd, 1);
383 spin_unlock_bh(&jrp->inplock);
387 EXPORT_SYMBOL(caam_jr_enqueue);
390 * Init JobR independent of platform property detection
392 static int caam_jr_init(struct device *dev)
394 struct caam_drv_private_jr *jrp;
395 dma_addr_t inpbusaddr, outbusaddr;
398 jrp = dev_get_drvdata(dev);
400 tasklet_init(&jrp->irqtask, caam_jr_dequeue, (unsigned long)dev);
402 /* Connect job ring interrupt handler. */
403 error = request_irq(jrp->irq, caam_jr_interrupt, IRQF_SHARED,
406 dev_err(dev, "can't connect JobR %d interrupt (%d)\n",
407 jrp->ridx, jrp->irq);
411 error = caam_reset_hw_jr(dev);
416 jrp->inpring = dma_alloc_coherent(dev, sizeof(*jrp->inpring) *
417 JOBR_DEPTH, &inpbusaddr, GFP_KERNEL);
421 jrp->outring = dma_alloc_coherent(dev, sizeof(*jrp->outring) *
422 JOBR_DEPTH, &outbusaddr, GFP_KERNEL);
424 goto out_free_inpring;
426 jrp->entinfo = kcalloc(JOBR_DEPTH, sizeof(*jrp->entinfo), GFP_KERNEL);
428 goto out_free_outring;
430 for (i = 0; i < JOBR_DEPTH; i++)
431 jrp->entinfo[i].desc_addr_dma = !0;
434 jrp->inp_ring_write_index = 0;
435 jrp->out_ring_read_index = 0;
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);
444 jrp->ringsize = JOBR_DEPTH;
446 spin_lock_init(&jrp->inplock);
447 spin_lock_init(&jrp->outlock);
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));
457 dma_free_coherent(dev, sizeof(struct jr_outentry) * JOBR_DEPTH,
458 jrp->outring, outbusaddr);
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);
464 free_irq(jrp->irq, dev);
466 tasklet_kill(&jrp->irqtask);
472 * Probe routine for each detected JobR subsystem.
474 static int caam_jr_probe(struct platform_device *pdev)
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;
484 jrpriv = devm_kmalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL);
488 dev_set_drvdata(jrdev, jrpriv);
490 /* save ring identity relative to detection */
491 jrpriv->ridx = total_jobrs++;
493 nprop = pdev->dev.of_node;
494 /* Get configuration properties from device tree */
495 /* First, get register page */
496 ctrl = of_iomap(nprop, 0);
498 dev_err(jrdev, "of_iomap() failed\n");
502 jrpriv->rregs = (struct caam_job_ring __iomem __force *)ctrl;
504 if (sizeof(dma_addr_t) == sizeof(u64)) {
506 error = dma_set_mask_and_coherent(jrdev,
508 else if (of_device_is_compatible(nprop,
509 "fsl,sec-v5.0-job-ring"))
510 error = dma_set_mask_and_coherent(jrdev,
513 error = dma_set_mask_and_coherent(jrdev,
516 error = dma_set_mask_and_coherent(jrdev, DMA_BIT_MASK(32));
519 dev_err(jrdev, "dma_set_mask_and_coherent failed (%d)\n",
525 /* Identify the interrupt */
526 jrpriv->irq = irq_of_parse_and_map(nprop, 0);
528 /* Now do the platform independent part */
529 error = caam_jr_init(jrdev); /* now turn on hardware */
531 irq_dispose_mapping(jrpriv->irq);
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);
541 atomic_set(&jrpriv->tfm_count, 0);
546 static const struct of_device_id caam_jr_match[] = {
548 .compatible = "fsl,sec-v4.0-job-ring",
551 .compatible = "fsl,sec4.0-job-ring",
555 MODULE_DEVICE_TABLE(of, caam_jr_match);
557 static struct platform_driver caam_jr_driver = {
560 .of_match_table = caam_jr_match,
562 .probe = caam_jr_probe,
563 .remove = caam_jr_remove,
566 static int __init jr_driver_init(void)
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);
573 static void __exit jr_driver_exit(void)
575 platform_driver_unregister(&caam_jr_driver);
578 module_init(jr_driver_init);
579 module_exit(jr_driver_exit);
581 MODULE_LICENSE("GPL");
582 MODULE_DESCRIPTION("FSL CAAM JR request backend");
583 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");