blob: 7ae5e51a0597812060774aaeaa34bb6617098a6f [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 Phillipsa8ea07c2012-06-22 19:48:55 -050062 while (rd_reg32(&jrp->rregs->outring_used)) {
Kim Phillips8e8ec592011-03-13 16:54:26 +080063
Kim Phillipsa8ea07c2012-06-22 19:48:55 -050064 head = ACCESS_ONCE(jrp->head);
Kim Phillips8e8ec592011-03-13 16:54:26 +080065
Kim Phillipsa8ea07c2012-06-22 19:48:55 -050066 spin_lock_bh(&jrp->outlock);
Kim Phillips8e8ec592011-03-13 16:54:26 +080067
Kim Phillipsa8ea07c2012-06-22 19:48:55 -050068 sw_idx = tail = jrp->tail;
Kim Phillips8e8ec592011-03-13 16:54:26 +080069 hw_idx = jrp->out_ring_read_index;
Kim Phillipsa8ea07c2012-06-22 19:48:55 -050070
Kim Phillips8e8ec592011-03-13 16:54:26 +080071 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);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800124 }
125
Kim Phillips8e8ec592011-03-13 16:54:26 +0800126 /* reenable / unmask IRQs */
127 clrbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
128}
129
130/**
131 * caam_jr_register() - Alloc a ring for someone to use as needed. Returns
132 * an ordinal of the rings allocated, else returns -ENODEV if no rings
133 * are available.
134 * @ctrldev: points to the controller level dev (parent) that
135 * owns rings available for use.
136 * @dev: points to where a pointer to the newly allocated queue's
137 * dev can be written to if successful.
138 **/
139int caam_jr_register(struct device *ctrldev, struct device **rdev)
140{
141 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
142 struct caam_drv_private_jr *jrpriv = NULL;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800143 int ring;
144
145 /* Lock, if free ring - assign, unlock */
Kim Phillips4bba1e92012-06-22 19:48:54 -0500146 spin_lock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800147 for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
148 jrpriv = dev_get_drvdata(ctrlpriv->jrdev[ring]);
149 if (jrpriv->assign == JOBR_UNASSIGNED) {
150 jrpriv->assign = JOBR_ASSIGNED;
151 *rdev = ctrlpriv->jrdev[ring];
Kim Phillips4bba1e92012-06-22 19:48:54 -0500152 spin_unlock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800153 return ring;
154 }
155 }
156
157 /* If assigned, write dev where caller needs it */
Kim Phillips4bba1e92012-06-22 19:48:54 -0500158 spin_unlock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800159 *rdev = NULL;
160
161 return -ENODEV;
162}
163EXPORT_SYMBOL(caam_jr_register);
164
165/**
166 * caam_jr_deregister() - Deregister an API and release the queue.
167 * Returns 0 if OK, -EBUSY if queue still contains pending entries
168 * or unprocessed results at the time of the call
169 * @dev - points to the dev that identifies the queue to
170 * be released.
171 **/
172int caam_jr_deregister(struct device *rdev)
173{
174 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(rdev);
175 struct caam_drv_private *ctrlpriv;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800176
177 /* Get the owning controller's private space */
178 ctrlpriv = dev_get_drvdata(jrpriv->parentdev);
179
180 /*
181 * Make sure ring empty before release
182 */
183 if (rd_reg32(&jrpriv->rregs->outring_used) ||
184 (rd_reg32(&jrpriv->rregs->inpring_avail) != JOBR_DEPTH))
185 return -EBUSY;
186
187 /* Release ring */
Kim Phillips4bba1e92012-06-22 19:48:54 -0500188 spin_lock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800189 jrpriv->assign = JOBR_UNASSIGNED;
Kim Phillips4bba1e92012-06-22 19:48:54 -0500190 spin_unlock(&ctrlpriv->jr_alloc_lock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800191
192 return 0;
193}
194EXPORT_SYMBOL(caam_jr_deregister);
195
196/**
197 * caam_jr_enqueue() - Enqueue a job descriptor head. Returns 0 if OK,
198 * -EBUSY if the queue is full, -EIO if it cannot map the caller's
199 * descriptor.
200 * @dev: device of the job ring to be used. This device should have
201 * been assigned prior by caam_jr_register().
202 * @desc: points to a job descriptor that execute our request. All
203 * descriptors (and all referenced data) must be in a DMAable
204 * region, and all data references must be physical addresses
205 * accessible to CAAM (i.e. within a PAMU window granted
206 * to it).
207 * @cbk: pointer to a callback function to be invoked upon completion
208 * of this request. This has the form:
209 * callback(struct device *dev, u32 *desc, u32 stat, void *arg)
210 * where:
211 * @dev: contains the job ring device that processed this
212 * response.
213 * @desc: descriptor that initiated the request, same as
214 * "desc" being argued to caam_jr_enqueue().
215 * @status: untranslated status received from CAAM. See the
216 * reference manual for a detailed description of
217 * error meaning, or see the JRSTA definitions in the
218 * register header file
219 * @areq: optional pointer to an argument passed with the
220 * original request
221 * @areq: optional pointer to a user argument for use at callback
222 * time.
223 **/
224int caam_jr_enqueue(struct device *dev, u32 *desc,
225 void (*cbk)(struct device *dev, u32 *desc,
226 u32 status, void *areq),
227 void *areq)
228{
229 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
230 struct caam_jrentry_info *head_entry;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800231 int head, tail, desc_size;
232 dma_addr_t desc_dma;
233
234 desc_size = (*desc & HDR_JD_LENGTH_MASK) * sizeof(u32);
235 desc_dma = dma_map_single(dev, desc, desc_size, DMA_TO_DEVICE);
236 if (dma_mapping_error(dev, desc_dma)) {
237 dev_err(dev, "caam_jr_enqueue(): can't map jobdesc\n");
238 return -EIO;
239 }
240
Kim Phillips4bba1e92012-06-22 19:48:54 -0500241 spin_lock(&jrp->inplock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800242
243 head = jrp->head;
244 tail = ACCESS_ONCE(jrp->tail);
245
246 if (!rd_reg32(&jrp->rregs->inpring_avail) ||
247 CIRC_SPACE(head, tail, JOBR_DEPTH) <= 0) {
Kim Phillips4bba1e92012-06-22 19:48:54 -0500248 spin_unlock(&jrp->inplock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800249 dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE);
250 return -EBUSY;
251 }
252
253 head_entry = &jrp->entinfo[head];
254 head_entry->desc_addr_virt = desc;
255 head_entry->desc_size = desc_size;
256 head_entry->callbk = (void *)cbk;
257 head_entry->cbkarg = areq;
258 head_entry->desc_addr_dma = desc_dma;
259
260 jrp->inpring[jrp->inp_ring_write_index] = desc_dma;
261
262 smp_wmb();
263
264 jrp->inp_ring_write_index = (jrp->inp_ring_write_index + 1) &
265 (JOBR_DEPTH - 1);
266 jrp->head = (head + 1) & (JOBR_DEPTH - 1);
267
268 wmb();
269
270 wr_reg32(&jrp->rregs->inpring_jobadd, 1);
271
Kim Phillips4bba1e92012-06-22 19:48:54 -0500272 spin_unlock(&jrp->inplock);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800273
274 return 0;
275}
276EXPORT_SYMBOL(caam_jr_enqueue);
277
278static int caam_reset_hw_jr(struct device *dev)
279{
280 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
281 unsigned int timeout = 100000;
282
283 /*
Kim Phillips9620fd92011-04-11 19:15:16 -0500284 * mask interrupts since we are going to poll
285 * for reset completion status
Kim Phillips8e8ec592011-03-13 16:54:26 +0800286 */
Kim Phillips9620fd92011-04-11 19:15:16 -0500287 setbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800288
289 /* initiate flush (required prior to reset) */
290 wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET);
291 while (((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) ==
292 JRINT_ERR_HALT_INPROGRESS) && --timeout)
293 cpu_relax();
294
295 if ((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) !=
296 JRINT_ERR_HALT_COMPLETE || timeout == 0) {
297 dev_err(dev, "failed to flush job ring %d\n", jrp->ridx);
298 return -EIO;
299 }
300
301 /* initiate reset */
302 timeout = 100000;
303 wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET);
304 while ((rd_reg32(&jrp->rregs->jrcommand) & JRCR_RESET) && --timeout)
305 cpu_relax();
306
307 if (timeout == 0) {
308 dev_err(dev, "failed to reset job ring %d\n", jrp->ridx);
309 return -EIO;
310 }
311
Kim Phillips9620fd92011-04-11 19:15:16 -0500312 /* unmask interrupts */
313 clrbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800314
315 return 0;
316}
317
318/*
319 * Init JobR independent of platform property detection
320 */
321static int caam_jr_init(struct device *dev)
322{
323 struct caam_drv_private_jr *jrp;
324 dma_addr_t inpbusaddr, outbusaddr;
325 int i, error;
326
327 jrp = dev_get_drvdata(dev);
328
Kim Phillips9620fd92011-04-11 19:15:16 -0500329 /* Connect job ring interrupt handler. */
330 for_each_possible_cpu(i)
331 tasklet_init(&jrp->irqtask[i], caam_jr_dequeue,
332 (unsigned long)dev);
333
334 error = request_irq(jrp->irq, caam_jr_interrupt, IRQF_SHARED,
335 "caam-jobr", dev);
336 if (error) {
337 dev_err(dev, "can't connect JobR %d interrupt (%d)\n",
338 jrp->ridx, jrp->irq);
339 irq_dispose_mapping(jrp->irq);
340 jrp->irq = 0;
341 return -EINVAL;
342 }
343
Kim Phillips8e8ec592011-03-13 16:54:26 +0800344 error = caam_reset_hw_jr(dev);
345 if (error)
346 return error;
347
348 jrp->inpring = kzalloc(sizeof(dma_addr_t) * JOBR_DEPTH,
349 GFP_KERNEL | GFP_DMA);
350 jrp->outring = kzalloc(sizeof(struct jr_outentry) *
351 JOBR_DEPTH, GFP_KERNEL | GFP_DMA);
352
353 jrp->entinfo = kzalloc(sizeof(struct caam_jrentry_info) * JOBR_DEPTH,
354 GFP_KERNEL);
355
356 if ((jrp->inpring == NULL) || (jrp->outring == NULL) ||
357 (jrp->entinfo == NULL)) {
358 dev_err(dev, "can't allocate job rings for %d\n",
359 jrp->ridx);
360 return -ENOMEM;
361 }
362
363 for (i = 0; i < JOBR_DEPTH; i++)
364 jrp->entinfo[i].desc_addr_dma = !0;
365
366 /* Setup rings */
367 inpbusaddr = dma_map_single(dev, jrp->inpring,
Kim Phillipsa68d2592012-06-22 19:42:36 -0500368 sizeof(dma_addr_t) * JOBR_DEPTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800369 DMA_BIDIRECTIONAL);
370 if (dma_mapping_error(dev, inpbusaddr)) {
371 dev_err(dev, "caam_jr_init(): can't map input ring\n");
372 kfree(jrp->inpring);
373 kfree(jrp->outring);
374 kfree(jrp->entinfo);
375 return -EIO;
376 }
377
378 outbusaddr = dma_map_single(dev, jrp->outring,
379 sizeof(struct jr_outentry) * JOBR_DEPTH,
380 DMA_BIDIRECTIONAL);
381 if (dma_mapping_error(dev, outbusaddr)) {
382 dev_err(dev, "caam_jr_init(): can't map output ring\n");
Kim Phillipsa68d2592012-06-22 19:42:36 -0500383 dma_unmap_single(dev, inpbusaddr,
384 sizeof(dma_addr_t) * JOBR_DEPTH,
385 DMA_BIDIRECTIONAL);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800386 kfree(jrp->inpring);
387 kfree(jrp->outring);
388 kfree(jrp->entinfo);
389 return -EIO;
390 }
391
392 jrp->inp_ring_write_index = 0;
393 jrp->out_ring_read_index = 0;
394 jrp->head = 0;
395 jrp->tail = 0;
396
397 wr_reg64(&jrp->rregs->inpring_base, inpbusaddr);
398 wr_reg64(&jrp->rregs->outring_base, outbusaddr);
399 wr_reg32(&jrp->rregs->inpring_size, JOBR_DEPTH);
400 wr_reg32(&jrp->rregs->outring_size, JOBR_DEPTH);
401
402 jrp->ringsize = JOBR_DEPTH;
403
404 spin_lock_init(&jrp->inplock);
405 spin_lock_init(&jrp->outlock);
406
407 /* Select interrupt coalescing parameters */
408 setbits32(&jrp->rregs->rconfig_lo, JOBR_INTC |
409 (JOBR_INTC_COUNT_THLD << JRCFG_ICDCT_SHIFT) |
410 (JOBR_INTC_TIME_THLD << JRCFG_ICTT_SHIFT));
411
Kim Phillips8e8ec592011-03-13 16:54:26 +0800412 jrp->assign = JOBR_UNASSIGNED;
413 return 0;
414}
415
416/*
417 * Shutdown JobR independent of platform property code
418 */
419int caam_jr_shutdown(struct device *dev)
420{
421 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
422 dma_addr_t inpbusaddr, outbusaddr;
423 int ret, i;
424
425 ret = caam_reset_hw_jr(dev);
426
427 for_each_possible_cpu(i)
428 tasklet_kill(&jrp->irqtask[i]);
429
430 /* Release interrupt */
431 free_irq(jrp->irq, dev);
432
433 /* Free rings */
434 inpbusaddr = rd_reg64(&jrp->rregs->inpring_base);
435 outbusaddr = rd_reg64(&jrp->rregs->outring_base);
436 dma_unmap_single(dev, outbusaddr,
437 sizeof(struct jr_outentry) * JOBR_DEPTH,
438 DMA_BIDIRECTIONAL);
Kim Phillipsa68d2592012-06-22 19:42:36 -0500439 dma_unmap_single(dev, inpbusaddr, sizeof(dma_addr_t) * JOBR_DEPTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800440 DMA_BIDIRECTIONAL);
441 kfree(jrp->outring);
442 kfree(jrp->inpring);
443 kfree(jrp->entinfo);
444
445 return ret;
446}
447
448/*
449 * Probe routine for each detected JobR subsystem. It assumes that
450 * property detection was picked up externally.
451 */
452int caam_jr_probe(struct platform_device *pdev, struct device_node *np,
453 int ring)
454{
455 struct device *ctrldev, *jrdev;
456 struct platform_device *jr_pdev;
457 struct caam_drv_private *ctrlpriv;
458 struct caam_drv_private_jr *jrpriv;
459 u32 *jroffset;
460 int error;
461
462 ctrldev = &pdev->dev;
463 ctrlpriv = dev_get_drvdata(ctrldev);
464
465 jrpriv = kmalloc(sizeof(struct caam_drv_private_jr),
466 GFP_KERNEL);
467 if (jrpriv == NULL) {
468 dev_err(ctrldev, "can't alloc private mem for job ring %d\n",
469 ring);
470 return -ENOMEM;
471 }
472 jrpriv->parentdev = ctrldev; /* point back to parent */
473 jrpriv->ridx = ring; /* save ring identity relative to detection */
474
475 /*
476 * Derive a pointer to the detected JobRs regs
477 * Driver has already iomapped the entire space, we just
478 * need to add in the offset to this JobR. Don't know if I
479 * like this long-term, but it'll run
480 */
481 jroffset = (u32 *)of_get_property(np, "reg", NULL);
482 jrpriv->rregs = (struct caam_job_ring __iomem *)((void *)ctrlpriv->ctrl
483 + *jroffset);
484
485 /* Build a local dev for each detected queue */
486 jr_pdev = of_platform_device_create(np, NULL, ctrldev);
487 if (jr_pdev == NULL) {
488 kfree(jrpriv);
489 return -EINVAL;
490 }
491 jrdev = &jr_pdev->dev;
492 dev_set_drvdata(jrdev, jrpriv);
493 ctrlpriv->jrdev[ring] = jrdev;
494
Kim Phillipse13af182012-06-22 19:48:51 -0500495 if (sizeof(dma_addr_t) == sizeof(u64))
496 if (of_device_is_compatible(np, "fsl,sec-v5.0-job-ring"))
497 dma_set_mask(jrdev, DMA_BIT_MASK(40));
498 else
499 dma_set_mask(jrdev, DMA_BIT_MASK(36));
500 else
501 dma_set_mask(jrdev, DMA_BIT_MASK(32));
502
Kim Phillips8e8ec592011-03-13 16:54:26 +0800503 /* Identify the interrupt */
504 jrpriv->irq = of_irq_to_resource(np, 0, NULL);
505
506 /* Now do the platform independent part */
507 error = caam_jr_init(jrdev); /* now turn on hardware */
508 if (error) {
509 kfree(jrpriv);
510 return error;
511 }
512
513 return error;
514}