blob: 11d93f2437057184da0f935e3d3b9aa4d623c694 [file] [log] [blame]
Kim Phillips8e8ec592011-03-13 16:54:26 +08001/*
2 * CAAM/SEC 4.x transport/backend driver
3 * JobR backend functionality
4 *
Kim Phillips4bba1e92012-06-22 19:48:54 -05005 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kim Phillips8e8ec592011-03-13 16:54:26 +08006 */
7
8#include "compat.h"
9#include "regs.h"
10#include "jr.h"
11#include "desc.h"
12#include "intern.h"
13
14/* Main per-ring interrupt handler */
15static irqreturn_t caam_jr_interrupt(int irq, void *st_dev)
16{
17 struct device *dev = st_dev;
18 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
19 u32 irqstate;
20
21 /*
22 * Check the output ring for ready responses, kick
23 * tasklet if jobs done.
24 */
25 irqstate = rd_reg32(&jrp->rregs->jrintstatus);
26 if (!irqstate)
27 return IRQ_NONE;
28
29 /*
30 * If JobR error, we got more development work to do
31 * Flag a bug now, but we really need to shut down and
32 * restart the queue (and fix code).
33 */
34 if (irqstate & JRINT_JR_ERROR) {
35 dev_err(dev, "job ring error: irqstate: %08x\n", irqstate);
36 BUG();
37 }
38
39 /* mask valid interrupts */
40 setbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
41
42 /* Have valid interrupt at this point, just ACK and trigger */
43 wr_reg32(&jrp->rregs->jrintstatus, irqstate);
44
45 preempt_disable();
46 tasklet_schedule(&jrp->irqtask[smp_processor_id()]);
47 preempt_enable();
48
49 return IRQ_HANDLED;
50}
51
52/* Deferred service handler, run as interrupt-fired tasklet */
53static void caam_jr_dequeue(unsigned long devarg)
54{
55 int hw_idx, sw_idx, i, head, tail;
56 struct device *dev = (struct device *)devarg;
57 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
58 void (*usercall)(struct device *dev, u32 *desc, u32 status, void *arg);
59 u32 *userdesc, userstatus;
60 void *userarg;
Kim Phillips8e8ec592011-03-13 16:54:26 +080061
Kim Phillips4bba1e92012-06-22 19:48:54 -050062 spin_lock_bh(&jrp->outlock);
Kim Phillips8e8ec592011-03-13 16:54:26 +080063
64 head = ACCESS_ONCE(jrp->head);
65 sw_idx = tail = jrp->tail;
66
67 while (CIRC_CNT(head, tail, JOBR_DEPTH) >= 1 &&
68 rd_reg32(&jrp->rregs->outring_used)) {
69
70 hw_idx = jrp->out_ring_read_index;
71 for (i = 0; CIRC_CNT(head, tail + i, JOBR_DEPTH) >= 1; i++) {
72 sw_idx = (tail + i) & (JOBR_DEPTH - 1);
73
74 smp_read_barrier_depends();
75
76 if (jrp->outring[hw_idx].desc ==
77 jrp->entinfo[sw_idx].desc_addr_dma)
78 break; /* found */
79 }
80 /* we should never fail to find a matching descriptor */
81 BUG_ON(CIRC_CNT(head, tail + i, JOBR_DEPTH) <= 0);
82
83 /* Unmap just-run descriptor so we can post-process */
84 dma_unmap_single(dev, jrp->outring[hw_idx].desc,
85 jrp->entinfo[sw_idx].desc_size,
86 DMA_TO_DEVICE);
87
88 /* mark completed, avoid matching on a recycled desc addr */
89 jrp->entinfo[sw_idx].desc_addr_dma = 0;
90
91 /* Stash callback params for use outside of lock */
92 usercall = jrp->entinfo[sw_idx].callbk;
93 userarg = jrp->entinfo[sw_idx].cbkarg;
94 userdesc = jrp->entinfo[sw_idx].desc_addr_virt;
95 userstatus = jrp->outring[hw_idx].jrstatus;
96
97 smp_mb();
98
99 jrp->out_ring_read_index = (jrp->out_ring_read_index + 1) &
100 (JOBR_DEPTH - 1);
101
102 /*
103 * if this job completed out-of-order, do not increment
104 * the tail. Otherwise, increment tail by 1 plus the
105 * number of subsequent jobs already completed out-of-order
106 */
107 if (sw_idx == tail) {
108 do {
109 tail = (tail + 1) & (JOBR_DEPTH - 1);
110 smp_read_barrier_depends();
111 } while (CIRC_CNT(head, tail, JOBR_DEPTH) >= 1 &&
112 jrp->entinfo[tail].desc_addr_dma == 0);
113
114 jrp->tail = tail;
115 }
116
117 /* set done */
118 wr_reg32(&jrp->rregs->outring_rmvd, 1);
119
Kim Phillips4bba1e92012-06-22 19:48:54 -0500120 spin_unlock_bh(&jrp->outlock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800121
122 /* Finally, execute user's callback */
123 usercall(dev, userdesc, userstatus, userarg);
124
Kim Phillips4bba1e92012-06-22 19:48:54 -0500125 spin_lock_bh(&jrp->outlock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800126
127 head = ACCESS_ONCE(jrp->head);
128 sw_idx = tail = jrp->tail;
129 }
130
Kim Phillips4bba1e92012-06-22 19:48:54 -0500131 spin_unlock_bh(&jrp->outlock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800132
133 /* reenable / unmask IRQs */
134 clrbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
135}
136
137/**
138 * caam_jr_register() - Alloc a ring for someone to use as needed. Returns
139 * an ordinal of the rings allocated, else returns -ENODEV if no rings
140 * are available.
141 * @ctrldev: points to the controller level dev (parent) that
142 * owns rings available for use.
143 * @dev: points to where a pointer to the newly allocated queue's
144 * dev can be written to if successful.
145 **/
146int caam_jr_register(struct device *ctrldev, struct device **rdev)
147{
148 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
149 struct caam_drv_private_jr *jrpriv = NULL;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800150 int ring;
151
152 /* Lock, if free ring - assign, unlock */
Kim Phillips4bba1e92012-06-22 19:48:54 -0500153 spin_lock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800154 for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
155 jrpriv = dev_get_drvdata(ctrlpriv->jrdev[ring]);
156 if (jrpriv->assign == JOBR_UNASSIGNED) {
157 jrpriv->assign = JOBR_ASSIGNED;
158 *rdev = ctrlpriv->jrdev[ring];
Kim Phillips4bba1e92012-06-22 19:48:54 -0500159 spin_unlock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800160 return ring;
161 }
162 }
163
164 /* If assigned, write dev where caller needs it */
Kim Phillips4bba1e92012-06-22 19:48:54 -0500165 spin_unlock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800166 *rdev = NULL;
167
168 return -ENODEV;
169}
170EXPORT_SYMBOL(caam_jr_register);
171
172/**
173 * caam_jr_deregister() - Deregister an API and release the queue.
174 * Returns 0 if OK, -EBUSY if queue still contains pending entries
175 * or unprocessed results at the time of the call
176 * @dev - points to the dev that identifies the queue to
177 * be released.
178 **/
179int caam_jr_deregister(struct device *rdev)
180{
181 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(rdev);
182 struct caam_drv_private *ctrlpriv;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800183
184 /* Get the owning controller's private space */
185 ctrlpriv = dev_get_drvdata(jrpriv->parentdev);
186
187 /*
188 * Make sure ring empty before release
189 */
190 if (rd_reg32(&jrpriv->rregs->outring_used) ||
191 (rd_reg32(&jrpriv->rregs->inpring_avail) != JOBR_DEPTH))
192 return -EBUSY;
193
194 /* Release ring */
Kim Phillips4bba1e92012-06-22 19:48:54 -0500195 spin_lock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800196 jrpriv->assign = JOBR_UNASSIGNED;
Kim Phillips4bba1e92012-06-22 19:48:54 -0500197 spin_unlock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800198
199 return 0;
200}
201EXPORT_SYMBOL(caam_jr_deregister);
202
203/**
204 * caam_jr_enqueue() - Enqueue a job descriptor head. Returns 0 if OK,
205 * -EBUSY if the queue is full, -EIO if it cannot map the caller's
206 * descriptor.
207 * @dev: device of the job ring to be used. This device should have
208 * been assigned prior by caam_jr_register().
209 * @desc: points to a job descriptor that execute our request. All
210 * descriptors (and all referenced data) must be in a DMAable
211 * region, and all data references must be physical addresses
212 * accessible to CAAM (i.e. within a PAMU window granted
213 * to it).
214 * @cbk: pointer to a callback function to be invoked upon completion
215 * of this request. This has the form:
216 * callback(struct device *dev, u32 *desc, u32 stat, void *arg)
217 * where:
218 * @dev: contains the job ring device that processed this
219 * response.
220 * @desc: descriptor that initiated the request, same as
221 * "desc" being argued to caam_jr_enqueue().
222 * @status: untranslated status received from CAAM. See the
223 * reference manual for a detailed description of
224 * error meaning, or see the JRSTA definitions in the
225 * register header file
226 * @areq: optional pointer to an argument passed with the
227 * original request
228 * @areq: optional pointer to a user argument for use at callback
229 * time.
230 **/
231int caam_jr_enqueue(struct device *dev, u32 *desc,
232 void (*cbk)(struct device *dev, u32 *desc,
233 u32 status, void *areq),
234 void *areq)
235{
236 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
237 struct caam_jrentry_info *head_entry;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800238 int head, tail, desc_size;
239 dma_addr_t desc_dma;
240
241 desc_size = (*desc & HDR_JD_LENGTH_MASK) * sizeof(u32);
242 desc_dma = dma_map_single(dev, desc, desc_size, DMA_TO_DEVICE);
243 if (dma_mapping_error(dev, desc_dma)) {
244 dev_err(dev, "caam_jr_enqueue(): can't map jobdesc\n");
245 return -EIO;
246 }
247
Kim Phillips4bba1e92012-06-22 19:48:54 -0500248 spin_lock(&jrp->inplock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800249
250 head = jrp->head;
251 tail = ACCESS_ONCE(jrp->tail);
252
253 if (!rd_reg32(&jrp->rregs->inpring_avail) ||
254 CIRC_SPACE(head, tail, JOBR_DEPTH) <= 0) {
Kim Phillips4bba1e92012-06-22 19:48:54 -0500255 spin_unlock(&jrp->inplock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800256 dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE);
257 return -EBUSY;
258 }
259
260 head_entry = &jrp->entinfo[head];
261 head_entry->desc_addr_virt = desc;
262 head_entry->desc_size = desc_size;
263 head_entry->callbk = (void *)cbk;
264 head_entry->cbkarg = areq;
265 head_entry->desc_addr_dma = desc_dma;
266
267 jrp->inpring[jrp->inp_ring_write_index] = desc_dma;
268
269 smp_wmb();
270
271 jrp->inp_ring_write_index = (jrp->inp_ring_write_index + 1) &
272 (JOBR_DEPTH - 1);
273 jrp->head = (head + 1) & (JOBR_DEPTH - 1);
274
275 wmb();
276
277 wr_reg32(&jrp->rregs->inpring_jobadd, 1);
278
Kim Phillips4bba1e92012-06-22 19:48:54 -0500279 spin_unlock(&jrp->inplock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800280
281 return 0;
282}
283EXPORT_SYMBOL(caam_jr_enqueue);
284
285static int caam_reset_hw_jr(struct device *dev)
286{
287 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
288 unsigned int timeout = 100000;
289
290 /*
Kim Phillips9620fd92011-04-11 19:15:16 -0500291 * mask interrupts since we are going to poll
292 * for reset completion status
Kim Phillips8e8ec592011-03-13 16:54:26 +0800293 */
Kim Phillips9620fd92011-04-11 19:15:16 -0500294 setbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800295
296 /* initiate flush (required prior to reset) */
297 wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET);
298 while (((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) ==
299 JRINT_ERR_HALT_INPROGRESS) && --timeout)
300 cpu_relax();
301
302 if ((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) !=
303 JRINT_ERR_HALT_COMPLETE || timeout == 0) {
304 dev_err(dev, "failed to flush job ring %d\n", jrp->ridx);
305 return -EIO;
306 }
307
308 /* initiate reset */
309 timeout = 100000;
310 wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET);
311 while ((rd_reg32(&jrp->rregs->jrcommand) & JRCR_RESET) && --timeout)
312 cpu_relax();
313
314 if (timeout == 0) {
315 dev_err(dev, "failed to reset job ring %d\n", jrp->ridx);
316 return -EIO;
317 }
318
Kim Phillips9620fd92011-04-11 19:15:16 -0500319 /* unmask interrupts */
320 clrbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800321
322 return 0;
323}
324
325/*
326 * Init JobR independent of platform property detection
327 */
328static int caam_jr_init(struct device *dev)
329{
330 struct caam_drv_private_jr *jrp;
331 dma_addr_t inpbusaddr, outbusaddr;
332 int i, error;
333
334 jrp = dev_get_drvdata(dev);
335
Kim Phillips9620fd92011-04-11 19:15:16 -0500336 /* Connect job ring interrupt handler. */
337 for_each_possible_cpu(i)
338 tasklet_init(&jrp->irqtask[i], caam_jr_dequeue,
339 (unsigned long)dev);
340
341 error = request_irq(jrp->irq, caam_jr_interrupt, IRQF_SHARED,
342 "caam-jobr", dev);
343 if (error) {
344 dev_err(dev, "can't connect JobR %d interrupt (%d)\n",
345 jrp->ridx, jrp->irq);
346 irq_dispose_mapping(jrp->irq);
347 jrp->irq = 0;
348 return -EINVAL;
349 }
350
Kim Phillips8e8ec592011-03-13 16:54:26 +0800351 error = caam_reset_hw_jr(dev);
352 if (error)
353 return error;
354
355 jrp->inpring = kzalloc(sizeof(dma_addr_t) * JOBR_DEPTH,
356 GFP_KERNEL | GFP_DMA);
357 jrp->outring = kzalloc(sizeof(struct jr_outentry) *
358 JOBR_DEPTH, GFP_KERNEL | GFP_DMA);
359
360 jrp->entinfo = kzalloc(sizeof(struct caam_jrentry_info) * JOBR_DEPTH,
361 GFP_KERNEL);
362
363 if ((jrp->inpring == NULL) || (jrp->outring == NULL) ||
364 (jrp->entinfo == NULL)) {
365 dev_err(dev, "can't allocate job rings for %d\n",
366 jrp->ridx);
367 return -ENOMEM;
368 }
369
370 for (i = 0; i < JOBR_DEPTH; i++)
371 jrp->entinfo[i].desc_addr_dma = !0;
372
373 /* Setup rings */
374 inpbusaddr = dma_map_single(dev, jrp->inpring,
Kim Phillipsa68d2592012-06-22 19:42:36 -0500375 sizeof(dma_addr_t) * JOBR_DEPTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800376 DMA_BIDIRECTIONAL);
377 if (dma_mapping_error(dev, inpbusaddr)) {
378 dev_err(dev, "caam_jr_init(): can't map input ring\n");
379 kfree(jrp->inpring);
380 kfree(jrp->outring);
381 kfree(jrp->entinfo);
382 return -EIO;
383 }
384
385 outbusaddr = dma_map_single(dev, jrp->outring,
386 sizeof(struct jr_outentry) * JOBR_DEPTH,
387 DMA_BIDIRECTIONAL);
388 if (dma_mapping_error(dev, outbusaddr)) {
389 dev_err(dev, "caam_jr_init(): can't map output ring\n");
Kim Phillipsa68d2592012-06-22 19:42:36 -0500390 dma_unmap_single(dev, inpbusaddr,
391 sizeof(dma_addr_t) * JOBR_DEPTH,
392 DMA_BIDIRECTIONAL);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800393 kfree(jrp->inpring);
394 kfree(jrp->outring);
395 kfree(jrp->entinfo);
396 return -EIO;
397 }
398
399 jrp->inp_ring_write_index = 0;
400 jrp->out_ring_read_index = 0;
401 jrp->head = 0;
402 jrp->tail = 0;
403
404 wr_reg64(&jrp->rregs->inpring_base, inpbusaddr);
405 wr_reg64(&jrp->rregs->outring_base, outbusaddr);
406 wr_reg32(&jrp->rregs->inpring_size, JOBR_DEPTH);
407 wr_reg32(&jrp->rregs->outring_size, JOBR_DEPTH);
408
409 jrp->ringsize = JOBR_DEPTH;
410
411 spin_lock_init(&jrp->inplock);
412 spin_lock_init(&jrp->outlock);
413
414 /* Select interrupt coalescing parameters */
415 setbits32(&jrp->rregs->rconfig_lo, JOBR_INTC |
416 (JOBR_INTC_COUNT_THLD << JRCFG_ICDCT_SHIFT) |
417 (JOBR_INTC_TIME_THLD << JRCFG_ICTT_SHIFT));
418
Kim Phillips8e8ec592011-03-13 16:54:26 +0800419 jrp->assign = JOBR_UNASSIGNED;
420 return 0;
421}
422
423/*
424 * Shutdown JobR independent of platform property code
425 */
426int caam_jr_shutdown(struct device *dev)
427{
428 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
429 dma_addr_t inpbusaddr, outbusaddr;
430 int ret, i;
431
432 ret = caam_reset_hw_jr(dev);
433
434 for_each_possible_cpu(i)
435 tasklet_kill(&jrp->irqtask[i]);
436
437 /* Release interrupt */
438 free_irq(jrp->irq, dev);
439
440 /* Free rings */
441 inpbusaddr = rd_reg64(&jrp->rregs->inpring_base);
442 outbusaddr = rd_reg64(&jrp->rregs->outring_base);
443 dma_unmap_single(dev, outbusaddr,
444 sizeof(struct jr_outentry) * JOBR_DEPTH,
445 DMA_BIDIRECTIONAL);
Kim Phillipsa68d2592012-06-22 19:42:36 -0500446 dma_unmap_single(dev, inpbusaddr, sizeof(dma_addr_t) * JOBR_DEPTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800447 DMA_BIDIRECTIONAL);
448 kfree(jrp->outring);
449 kfree(jrp->inpring);
450 kfree(jrp->entinfo);
451
452 return ret;
453}
454
455/*
456 * Probe routine for each detected JobR subsystem. It assumes that
457 * property detection was picked up externally.
458 */
459int caam_jr_probe(struct platform_device *pdev, struct device_node *np,
460 int ring)
461{
462 struct device *ctrldev, *jrdev;
463 struct platform_device *jr_pdev;
464 struct caam_drv_private *ctrlpriv;
465 struct caam_drv_private_jr *jrpriv;
466 u32 *jroffset;
467 int error;
468
469 ctrldev = &pdev->dev;
470 ctrlpriv = dev_get_drvdata(ctrldev);
471
472 jrpriv = kmalloc(sizeof(struct caam_drv_private_jr),
473 GFP_KERNEL);
474 if (jrpriv == NULL) {
475 dev_err(ctrldev, "can't alloc private mem for job ring %d\n",
476 ring);
477 return -ENOMEM;
478 }
479 jrpriv->parentdev = ctrldev; /* point back to parent */
480 jrpriv->ridx = ring; /* save ring identity relative to detection */
481
482 /*
483 * Derive a pointer to the detected JobRs regs
484 * Driver has already iomapped the entire space, we just
485 * need to add in the offset to this JobR. Don't know if I
486 * like this long-term, but it'll run
487 */
488 jroffset = (u32 *)of_get_property(np, "reg", NULL);
489 jrpriv->rregs = (struct caam_job_ring __iomem *)((void *)ctrlpriv->ctrl
490 + *jroffset);
491
492 /* Build a local dev for each detected queue */
493 jr_pdev = of_platform_device_create(np, NULL, ctrldev);
494 if (jr_pdev == NULL) {
495 kfree(jrpriv);
496 return -EINVAL;
497 }
498 jrdev = &jr_pdev->dev;
499 dev_set_drvdata(jrdev, jrpriv);
500 ctrlpriv->jrdev[ring] = jrdev;
501
Kim Phillipse13af182012-06-22 19:48:51 -0500502 if (sizeof(dma_addr_t) == sizeof(u64))
503 if (of_device_is_compatible(np, "fsl,sec-v5.0-job-ring"))
504 dma_set_mask(jrdev, DMA_BIT_MASK(40));
505 else
506 dma_set_mask(jrdev, DMA_BIT_MASK(36));
507 else
508 dma_set_mask(jrdev, DMA_BIT_MASK(32));
509
Kim Phillips8e8ec592011-03-13 16:54:26 +0800510 /* Identify the interrupt */
511 jrpriv->irq = of_irq_to_resource(np, 0, NULL);
512
513 /* Now do the platform independent part */
514 error = caam_jr_init(jrdev); /* now turn on hardware */
515 if (error) {
516 kfree(jrpriv);
517 return error;
518 }
519
520 return error;
521}