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 * Copyright 2019, 2023 NXP
8 */
9
10 #include <linux/of_irq.h>
11 #include <linux/of_address.h>
12 #include <linux/platform_device.h>
13
14 #include "compat.h"
15 #include "ctrl.h"
16 #include "regs.h"
17 #include "jr.h"
18 #include "desc.h"
19 #include "intern.h"
20
21 struct jr_driver_data {
22 /* List of Physical JobR's with the Driver */
23 struct list_head jr_list;
24 spinlock_t jr_alloc_lock; /* jr_list lock */
25 } ____cacheline_aligned;
26
27 static struct jr_driver_data driver_data;
28 static DEFINE_MUTEX(algs_lock);
29 static unsigned int active_devs;
30
register_algs(struct caam_drv_private_jr * jrpriv,struct device * dev)31 static void register_algs(struct caam_drv_private_jr *jrpriv,
32 struct device *dev)
33 {
34 mutex_lock(&algs_lock);
35
36 if (++active_devs != 1)
37 goto algs_unlock;
38
39 caam_algapi_init(dev);
40 caam_algapi_hash_init(dev);
41 caam_pkc_init(dev);
42 jrpriv->hwrng = !caam_rng_init(dev);
43 caam_qi_algapi_init(dev);
44
45 algs_unlock:
46 mutex_unlock(&algs_lock);
47 }
48
unregister_algs(void)49 static void unregister_algs(void)
50 {
51 mutex_lock(&algs_lock);
52
53 if (--active_devs != 0)
54 goto algs_unlock;
55
56 caam_qi_algapi_exit();
57 caam_pkc_exit();
58 caam_algapi_hash_exit();
59 caam_algapi_exit();
60
61 algs_unlock:
62 mutex_unlock(&algs_lock);
63 }
64
caam_jr_crypto_engine_exit(void * data)65 static void caam_jr_crypto_engine_exit(void *data)
66 {
67 struct device *jrdev = data;
68 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev);
69
70 /* Free the resources of crypto-engine */
71 crypto_engine_exit(jrpriv->engine);
72 }
73
74 /*
75 * Put the CAAM in quiesce, ie stop
76 *
77 * Must be called with itr disabled
78 */
caam_jr_stop_processing(struct device * dev,u32 jrcr_bits)79 static int caam_jr_stop_processing(struct device *dev, u32 jrcr_bits)
80 {
81 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
82 unsigned int timeout = 100000;
83
84 /* Check the current status */
85 if (rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_INPROGRESS)
86 goto wait_quiesce_completion;
87
88 /* Reset the field */
89 clrsetbits_32(&jrp->rregs->jrintstatus, JRINT_ERR_HALT_MASK, 0);
90
91 /* initiate flush / park (required prior to reset) */
92 wr_reg32(&jrp->rregs->jrcommand, jrcr_bits);
93
94 wait_quiesce_completion:
95 while (((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) ==
96 JRINT_ERR_HALT_INPROGRESS) && --timeout)
97 cpu_relax();
98
99 if ((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) !=
100 JRINT_ERR_HALT_COMPLETE || timeout == 0) {
101 dev_err(dev, "failed to flush job ring %d\n", jrp->ridx);
102 return -EIO;
103 }
104
105 return 0;
106 }
107
108 /*
109 * Flush the job ring, so the jobs running will be stopped, jobs queued will be
110 * invalidated and the CAAM will no longer fetch fron input ring.
111 *
112 * Must be called with itr disabled
113 */
caam_jr_flush(struct device * dev)114 static int caam_jr_flush(struct device *dev)
115 {
116 return caam_jr_stop_processing(dev, JRCR_RESET);
117 }
118
119 /* The resume can be used after a park or a flush if CAAM has not been reset */
caam_jr_restart_processing(struct device * dev)120 static int caam_jr_restart_processing(struct device *dev)
121 {
122 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
123 u32 halt_status = rd_reg32(&jrp->rregs->jrintstatus) &
124 JRINT_ERR_HALT_MASK;
125
126 /* Check that the flush/park is completed */
127 if (halt_status != JRINT_ERR_HALT_COMPLETE)
128 return -1;
129
130 /* Resume processing of jobs */
131 clrsetbits_32(&jrp->rregs->jrintstatus, 0, JRINT_ERR_HALT_COMPLETE);
132
133 return 0;
134 }
135
caam_reset_hw_jr(struct device * dev)136 static int caam_reset_hw_jr(struct device *dev)
137 {
138 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
139 unsigned int timeout = 100000;
140 int err;
141 /*
142 * mask interrupts since we are going to poll
143 * for reset completion status
144 */
145 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JRCFG_IMSK);
146 err = caam_jr_flush(dev);
147 if (err)
148 return err;
149
150 /* initiate reset */
151 wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET);
152 while ((rd_reg32(&jrp->rregs->jrcommand) & JRCR_RESET) && --timeout)
153 cpu_relax();
154
155 if (timeout == 0) {
156 dev_err(dev, "failed to reset job ring %d\n", jrp->ridx);
157 return -EIO;
158 }
159
160 /* unmask interrupts */
161 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0);
162
163 return 0;
164 }
165
166 /*
167 * Shutdown JobR independent of platform property code
168 */
caam_jr_shutdown(struct device * dev)169 static int caam_jr_shutdown(struct device *dev)
170 {
171 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
172 int ret;
173
174 ret = caam_reset_hw_jr(dev);
175
176 tasklet_kill(&jrp->irqtask);
177
178 return ret;
179 }
180
caam_jr_remove(struct platform_device * pdev)181 static void caam_jr_remove(struct platform_device *pdev)
182 {
183 int ret;
184 struct device *jrdev;
185 struct caam_drv_private_jr *jrpriv;
186
187 jrdev = &pdev->dev;
188 jrpriv = dev_get_drvdata(jrdev);
189
190 if (jrpriv->hwrng)
191 caam_rng_exit(jrdev->parent);
192
193 /*
194 * If a job ring is still allocated there is trouble ahead. Once
195 * caam_jr_remove() returned, jrpriv will be freed and the registers
196 * will get unmapped. So any user of such a job ring will probably
197 * crash.
198 */
199 if (atomic_read(&jrpriv->tfm_count)) {
200 dev_alert(jrdev, "Device is busy; consumers might start to crash\n");
201 return;
202 }
203
204 /* Unregister JR-based RNG & crypto algorithms */
205 unregister_algs();
206
207 /* Remove the node from Physical JobR list maintained by driver */
208 spin_lock(&driver_data.jr_alloc_lock);
209 list_del(&jrpriv->list_node);
210 spin_unlock(&driver_data.jr_alloc_lock);
211
212 /* Release ring */
213 ret = caam_jr_shutdown(jrdev);
214 if (ret)
215 dev_err(jrdev, "Failed to shut down job ring\n");
216 }
217
218 /* Main per-ring interrupt handler */
caam_jr_interrupt(int irq,void * st_dev)219 static irqreturn_t caam_jr_interrupt(int irq, void *st_dev)
220 {
221 struct device *dev = st_dev;
222 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
223 u32 irqstate;
224
225 /*
226 * Check the output ring for ready responses, kick
227 * tasklet if jobs done.
228 */
229 irqstate = rd_reg32(&jrp->rregs->jrintstatus);
230 if (!(irqstate & JRINT_JR_INT))
231 return IRQ_NONE;
232
233 /*
234 * If JobR error, we got more development work to do
235 * Flag a bug now, but we really need to shut down and
236 * restart the queue (and fix code).
237 */
238 if (irqstate & JRINT_JR_ERROR) {
239 dev_err(dev, "job ring error: irqstate: %08x\n", irqstate);
240 BUG();
241 }
242
243 /* mask valid interrupts */
244 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JRCFG_IMSK);
245
246 /* Have valid interrupt at this point, just ACK and trigger */
247 wr_reg32(&jrp->rregs->jrintstatus, irqstate);
248
249 preempt_disable();
250 tasklet_schedule(&jrp->irqtask);
251 preempt_enable();
252
253 return IRQ_HANDLED;
254 }
255
256 /* Deferred service handler, run as interrupt-fired tasklet */
caam_jr_dequeue(unsigned long devarg)257 static void caam_jr_dequeue(unsigned long devarg)
258 {
259 int hw_idx, sw_idx, i, head, tail;
260 struct caam_jr_dequeue_params *params = (void *)devarg;
261 struct device *dev = params->dev;
262 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
263 void (*usercall)(struct device *dev, u32 *desc, u32 status, void *arg);
264 u32 *userdesc, userstatus;
265 void *userarg;
266 u32 outring_used = 0;
267
268 while (outring_used ||
269 (outring_used = rd_reg32(&jrp->rregs->outring_used))) {
270
271 head = READ_ONCE(jrp->head);
272
273 sw_idx = tail = jrp->tail;
274 hw_idx = jrp->out_ring_read_index;
275
276 for (i = 0; CIRC_CNT(head, tail + i, JOBR_DEPTH) >= 1; i++) {
277 sw_idx = (tail + i) & (JOBR_DEPTH - 1);
278
279 if (jr_outentry_desc(jrp->outring, hw_idx) ==
280 caam_dma_to_cpu(jrp->entinfo[sw_idx].desc_addr_dma))
281 break; /* found */
282 }
283 /* we should never fail to find a matching descriptor */
284 BUG_ON(CIRC_CNT(head, tail + i, JOBR_DEPTH) <= 0);
285
286 /* Unmap just-run descriptor so we can post-process */
287 dma_unmap_single(dev,
288 caam_dma_to_cpu(jr_outentry_desc(jrp->outring,
289 hw_idx)),
290 jrp->entinfo[sw_idx].desc_size,
291 DMA_TO_DEVICE);
292
293 /* mark completed, avoid matching on a recycled desc addr */
294 jrp->entinfo[sw_idx].desc_addr_dma = 0;
295
296 /* Stash callback params */
297 usercall = jrp->entinfo[sw_idx].callbk;
298 userarg = jrp->entinfo[sw_idx].cbkarg;
299 userdesc = jrp->entinfo[sw_idx].desc_addr_virt;
300 userstatus = caam32_to_cpu(jr_outentry_jrstatus(jrp->outring,
301 hw_idx));
302
303 /*
304 * Make sure all information from the job has been obtained
305 * before telling CAAM that the job has been removed from the
306 * output ring.
307 */
308 mb();
309
310 /* set done */
311 wr_reg32(&jrp->rregs->outring_rmvd, 1);
312
313 jrp->out_ring_read_index = (jrp->out_ring_read_index + 1) &
314 (JOBR_DEPTH - 1);
315
316 /*
317 * if this job completed out-of-order, do not increment
318 * the tail. Otherwise, increment tail by 1 plus the
319 * number of subsequent jobs already completed out-of-order
320 */
321 if (sw_idx == tail) {
322 do {
323 tail = (tail + 1) & (JOBR_DEPTH - 1);
324 } while (CIRC_CNT(head, tail, JOBR_DEPTH) >= 1 &&
325 jrp->entinfo[tail].desc_addr_dma == 0);
326
327 jrp->tail = tail;
328 }
329
330 /* Finally, execute user's callback */
331 usercall(dev, userdesc, userstatus, userarg);
332 outring_used--;
333 }
334
335 if (params->enable_itr)
336 /* reenable / unmask IRQs */
337 clrsetbits_32(&jrp->rregs->rconfig_lo, JRCFG_IMSK, 0);
338 }
339
340 /**
341 * caam_jr_alloc() - Alloc a job ring for someone to use as needed.
342 *
343 * returns : pointer to the newly allocated physical
344 * JobR dev can be written to if successful.
345 **/
caam_jr_alloc(void)346 struct device *caam_jr_alloc(void)
347 {
348 struct caam_drv_private_jr *jrpriv, *min_jrpriv = NULL;
349 struct device *dev = ERR_PTR(-ENODEV);
350 int min_tfm_cnt = INT_MAX;
351 int tfm_cnt;
352
353 spin_lock(&driver_data.jr_alloc_lock);
354
355 if (list_empty(&driver_data.jr_list)) {
356 spin_unlock(&driver_data.jr_alloc_lock);
357 return ERR_PTR(-ENODEV);
358 }
359
360 list_for_each_entry(jrpriv, &driver_data.jr_list, list_node) {
361 tfm_cnt = atomic_read(&jrpriv->tfm_count);
362 if (tfm_cnt < min_tfm_cnt) {
363 min_tfm_cnt = tfm_cnt;
364 min_jrpriv = jrpriv;
365 }
366 if (!min_tfm_cnt)
367 break;
368 }
369
370 if (min_jrpriv) {
371 atomic_inc(&min_jrpriv->tfm_count);
372 dev = min_jrpriv->dev;
373 }
374 spin_unlock(&driver_data.jr_alloc_lock);
375
376 return dev;
377 }
378 EXPORT_SYMBOL(caam_jr_alloc);
379
380 /**
381 * caam_jr_free() - Free the Job Ring
382 * @rdev: points to the dev that identifies the Job ring to
383 * be released.
384 **/
caam_jr_free(struct device * rdev)385 void caam_jr_free(struct device *rdev)
386 {
387 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(rdev);
388
389 atomic_dec(&jrpriv->tfm_count);
390 }
391 EXPORT_SYMBOL(caam_jr_free);
392
393 /**
394 * caam_jr_enqueue() - Enqueue a job descriptor head. Returns -EINPROGRESS
395 * if OK, -ENOSPC if the queue is full, -EIO if it cannot map the caller's
396 * descriptor.
397 * @dev: struct device of the job ring to be used
398 * @desc: points to a job descriptor that execute our request. All
399 * descriptors (and all referenced data) must be in a DMAable
400 * region, and all data references must be physical addresses
401 * accessible to CAAM (i.e. within a PAMU window granted
402 * to it).
403 * @cbk: pointer to a callback function to be invoked upon completion
404 * of this request. This has the form:
405 * callback(struct device *dev, u32 *desc, u32 stat, void *arg)
406 * where:
407 * dev: contains the job ring device that processed this
408 * response.
409 * desc: descriptor that initiated the request, same as
410 * "desc" being argued to caam_jr_enqueue().
411 * status: untranslated status received from CAAM. See the
412 * reference manual for a detailed description of
413 * error meaning, or see the JRSTA definitions in the
414 * register header file
415 * areq: optional pointer to an argument passed with the
416 * original request
417 * @areq: optional pointer to a user argument for use at callback
418 * time.
419 **/
caam_jr_enqueue(struct device * dev,u32 * desc,void (* cbk)(struct device * dev,u32 * desc,u32 status,void * areq),void * areq)420 int caam_jr_enqueue(struct device *dev, u32 *desc,
421 void (*cbk)(struct device *dev, u32 *desc,
422 u32 status, void *areq),
423 void *areq)
424 {
425 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
426 struct caam_jrentry_info *head_entry;
427 int head, tail, desc_size;
428 dma_addr_t desc_dma;
429
430 desc_size = (caam32_to_cpu(*desc) & HDR_JD_LENGTH_MASK) * sizeof(u32);
431 desc_dma = dma_map_single(dev, desc, desc_size, DMA_TO_DEVICE);
432 if (dma_mapping_error(dev, desc_dma)) {
433 dev_err(dev, "caam_jr_enqueue(): can't map jobdesc\n");
434 return -EIO;
435 }
436
437 spin_lock_bh(&jrp->inplock);
438
439 head = jrp->head;
440 tail = READ_ONCE(jrp->tail);
441
442 if (!jrp->inpring_avail ||
443 CIRC_SPACE(head, tail, JOBR_DEPTH) <= 0) {
444 spin_unlock_bh(&jrp->inplock);
445 dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE);
446 return -ENOSPC;
447 }
448
449 head_entry = &jrp->entinfo[head];
450 head_entry->desc_addr_virt = desc;
451 head_entry->desc_size = desc_size;
452 head_entry->callbk = (void *)cbk;
453 head_entry->cbkarg = areq;
454 head_entry->desc_addr_dma = desc_dma;
455
456 jr_inpentry_set(jrp->inpring, head, cpu_to_caam_dma(desc_dma));
457
458 /*
459 * Guarantee that the descriptor's DMA address has been written to
460 * the next slot in the ring before the write index is updated, since
461 * other cores may update this index independently.
462 *
463 * Under heavy DDR load, smp_wmb() or dma_wmb() fail to make the input
464 * ring be updated before the CAAM starts reading it. So, CAAM will
465 * process, again, an old descriptor address and will put it in the
466 * output ring. This will make caam_jr_dequeue() to fail, since this
467 * old descriptor is not in the software ring.
468 * To fix this, use wmb() which works on the full system instead of
469 * inner/outer shareable domains.
470 */
471 wmb();
472
473 jrp->head = (head + 1) & (JOBR_DEPTH - 1);
474
475 /*
476 * Ensure that all job information has been written before
477 * notifying CAAM that a new job was added to the input ring
478 * using a memory barrier. The wr_reg32() uses api iowrite32()
479 * to do the register write. iowrite32() issues a memory barrier
480 * before the write operation.
481 */
482
483 wr_reg32(&jrp->rregs->inpring_jobadd, 1);
484
485 jrp->inpring_avail--;
486 if (!jrp->inpring_avail)
487 jrp->inpring_avail = rd_reg32(&jrp->rregs->inpring_avail);
488
489 spin_unlock_bh(&jrp->inplock);
490
491 return -EINPROGRESS;
492 }
493 EXPORT_SYMBOL(caam_jr_enqueue);
494
caam_jr_init_hw(struct device * dev,dma_addr_t inpbusaddr,dma_addr_t outbusaddr)495 static void caam_jr_init_hw(struct device *dev, dma_addr_t inpbusaddr,
496 dma_addr_t outbusaddr)
497 {
498 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
499
500 wr_reg64(&jrp->rregs->inpring_base, inpbusaddr);
501 wr_reg64(&jrp->rregs->outring_base, outbusaddr);
502 wr_reg32(&jrp->rregs->inpring_size, JOBR_DEPTH);
503 wr_reg32(&jrp->rregs->outring_size, JOBR_DEPTH);
504
505 /* Select interrupt coalescing parameters */
506 clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JOBR_INTC |
507 (JOBR_INTC_COUNT_THLD << JRCFG_ICDCT_SHIFT) |
508 (JOBR_INTC_TIME_THLD << JRCFG_ICTT_SHIFT));
509 }
510
caam_jr_reset_index(struct caam_drv_private_jr * jrp)511 static void caam_jr_reset_index(struct caam_drv_private_jr *jrp)
512 {
513 jrp->out_ring_read_index = 0;
514 jrp->head = 0;
515 jrp->tail = 0;
516 }
517
518 /*
519 * Init JobR independent of platform property detection
520 */
caam_jr_init(struct device * dev)521 static int caam_jr_init(struct device *dev)
522 {
523 struct caam_drv_private_jr *jrp;
524 dma_addr_t inpbusaddr, outbusaddr;
525 int i, error;
526
527 jrp = dev_get_drvdata(dev);
528
529 error = caam_reset_hw_jr(dev);
530 if (error)
531 return error;
532
533 jrp->inpring = dmam_alloc_coherent(dev, SIZEOF_JR_INPENTRY *
534 JOBR_DEPTH, &inpbusaddr,
535 GFP_KERNEL);
536 if (!jrp->inpring)
537 return -ENOMEM;
538
539 jrp->outring = dmam_alloc_coherent(dev, SIZEOF_JR_OUTENTRY *
540 JOBR_DEPTH, &outbusaddr,
541 GFP_KERNEL);
542 if (!jrp->outring)
543 return -ENOMEM;
544
545 jrp->entinfo = devm_kcalloc(dev, JOBR_DEPTH, sizeof(*jrp->entinfo),
546 GFP_KERNEL);
547 if (!jrp->entinfo)
548 return -ENOMEM;
549
550 for (i = 0; i < JOBR_DEPTH; i++)
551 jrp->entinfo[i].desc_addr_dma = !0;
552
553 /* Setup rings */
554 caam_jr_reset_index(jrp);
555 jrp->inpring_avail = JOBR_DEPTH;
556 caam_jr_init_hw(dev, inpbusaddr, outbusaddr);
557
558 spin_lock_init(&jrp->inplock);
559
560 jrp->tasklet_params.dev = dev;
561 jrp->tasklet_params.enable_itr = 1;
562 tasklet_init(&jrp->irqtask, caam_jr_dequeue,
563 (unsigned long)&jrp->tasklet_params);
564
565 /* Connect job ring interrupt handler. */
566 error = devm_request_irq(dev, jrp->irq, caam_jr_interrupt, IRQF_SHARED,
567 dev_name(dev), dev);
568 if (error) {
569 dev_err(dev, "can't connect JobR %d interrupt (%d)\n",
570 jrp->ridx, jrp->irq);
571 tasklet_kill(&jrp->irqtask);
572 }
573
574 return error;
575 }
576
577 /*
578 * Probe routine for each detected JobR subsystem.
579 */
caam_jr_probe(struct platform_device * pdev)580 static int caam_jr_probe(struct platform_device *pdev)
581 {
582 struct device *jrdev;
583 struct caam_drv_private_jr *jrpriv;
584 static int total_jobrs;
585 void __iomem *ctrl;
586 struct resource *r;
587 int error;
588 int irq;
589
590 /*
591 * The job rings live inside the register window of their parent
592 * fsl,sec-v4.0 node, which caam_probe() already reserves (and maps)
593 * via devm_of_iomap(). A requested region that overlaps that
594 * reservation, e.g. from devm_platform_ioremap_resource(), would
595 * therefore fail with -EBUSY, so map the registers without claiming
596 * the region here.
597 */
598 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
599 if (!r) {
600 dev_err(&pdev->dev, "platform_get_resource() failed\n");
601 return -EINVAL;
602 }
603
604 ctrl = devm_ioremap(&pdev->dev, r->start, resource_size(r));
605 if (!ctrl) {
606 dev_err(&pdev->dev, "devm_ioremap() failed\n");
607 return -ENOMEM;
608 }
609
610 irq = platform_get_irq(pdev, 0);
611 if (irq < 0)
612 return irq;
613
614 jrdev = &pdev->dev;
615 jrpriv = devm_kzalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL);
616 if (!jrpriv)
617 return -ENOMEM;
618
619 dev_set_drvdata(jrdev, jrpriv);
620
621 /* save ring identity relative to detection */
622 jrpriv->ridx = total_jobrs++;
623
624 jrpriv->rregs = ctrl;
625
626 error = dma_set_mask_and_coherent(jrdev, caam_get_dma_mask(jrdev));
627 if (error) {
628 dev_err(jrdev, "dma_set_mask_and_coherent failed (%d)\n",
629 error);
630 return error;
631 }
632
633 /* Initialize crypto engine */
634 jrpriv->engine = crypto_engine_alloc_init_and_set(jrdev, true, false,
635 CRYPTO_ENGINE_MAX_QLEN);
636 if (!jrpriv->engine) {
637 dev_err(jrdev, "Could not init crypto-engine\n");
638 return -ENOMEM;
639 }
640
641 error = devm_add_action_or_reset(jrdev, caam_jr_crypto_engine_exit,
642 jrdev);
643 if (error)
644 return error;
645
646 /* Start crypto engine */
647 error = crypto_engine_start(jrpriv->engine);
648 if (error) {
649 dev_err(jrdev, "Could not start crypto-engine\n");
650 return error;
651 }
652
653 /* Identify the interrupt */
654 jrpriv->irq = irq;
655
656 /* Now do the platform independent part */
657 error = caam_jr_init(jrdev); /* now turn on hardware */
658 if (error)
659 return error;
660
661 jrpriv->dev = jrdev;
662 spin_lock(&driver_data.jr_alloc_lock);
663 list_add_tail(&jrpriv->list_node, &driver_data.jr_list);
664 spin_unlock(&driver_data.jr_alloc_lock);
665
666 atomic_set(&jrpriv->tfm_count, 0);
667
668 device_init_wakeup(&pdev->dev, 1);
669 device_set_wakeup_enable(&pdev->dev, false);
670
671 register_algs(jrpriv, jrdev->parent);
672
673 return 0;
674 }
675
caam_jr_get_hw_state(struct device * dev)676 static void caam_jr_get_hw_state(struct device *dev)
677 {
678 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
679
680 jrp->state.inpbusaddr = rd_reg64(&jrp->rregs->inpring_base);
681 jrp->state.outbusaddr = rd_reg64(&jrp->rregs->outring_base);
682 }
683
caam_jr_suspend(struct device * dev)684 static int caam_jr_suspend(struct device *dev)
685 {
686 struct platform_device *pdev = to_platform_device(dev);
687 struct caam_drv_private_jr *jrpriv = platform_get_drvdata(pdev);
688 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev->parent);
689 struct caam_jr_dequeue_params suspend_params = {
690 .dev = dev,
691 .enable_itr = 0,
692 };
693
694 /* Remove the node from Physical JobR list maintained by driver */
695 spin_lock(&driver_data.jr_alloc_lock);
696 list_del(&jrpriv->list_node);
697 spin_unlock(&driver_data.jr_alloc_lock);
698
699 if (jrpriv->hwrng)
700 caam_rng_exit(dev->parent);
701
702 if (ctrlpriv->caam_off_during_pm) {
703 int err;
704
705 tasklet_disable(&jrpriv->irqtask);
706
707 /* mask itr to call flush */
708 clrsetbits_32(&jrpriv->rregs->rconfig_lo, 0, JRCFG_IMSK);
709
710 /* Invalid job in process */
711 err = caam_jr_flush(dev);
712 if (err) {
713 dev_err(dev, "Failed to flush\n");
714 return err;
715 }
716
717 /* Dequeing jobs flushed */
718 caam_jr_dequeue((unsigned long)&suspend_params);
719
720 /* Save state */
721 caam_jr_get_hw_state(dev);
722 } else if (device_may_wakeup(&pdev->dev)) {
723 enable_irq_wake(jrpriv->irq);
724 }
725
726 return 0;
727 }
728
caam_jr_resume(struct device * dev)729 static int caam_jr_resume(struct device *dev)
730 {
731 struct platform_device *pdev = to_platform_device(dev);
732 struct caam_drv_private_jr *jrpriv = platform_get_drvdata(pdev);
733 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev->parent);
734
735 if (ctrlpriv->caam_off_during_pm) {
736 u64 inp_addr;
737 int err;
738
739 /*
740 * Check if the CAAM has been resetted checking the address of
741 * the input ring
742 */
743 inp_addr = rd_reg64(&jrpriv->rregs->inpring_base);
744 if (inp_addr != 0) {
745 /* JR still has some configuration */
746 if (inp_addr == jrpriv->state.inpbusaddr) {
747 /* JR has not been resetted */
748 err = caam_jr_restart_processing(dev);
749 if (err) {
750 dev_err(dev,
751 "Restart processing failed\n");
752 return err;
753 }
754
755 tasklet_enable(&jrpriv->irqtask);
756
757 clrsetbits_32(&jrpriv->rregs->rconfig_lo,
758 JRCFG_IMSK, 0);
759
760 goto add_jr;
761 } else if (ctrlpriv->optee_en) {
762 /* JR has been used by OPTEE, reset it */
763 err = caam_reset_hw_jr(dev);
764 if (err) {
765 dev_err(dev, "Failed to reset JR\n");
766 return err;
767 }
768 } else {
769 /* No explanation, return error */
770 return -EIO;
771 }
772 }
773
774 caam_jr_reset_index(jrpriv);
775 caam_jr_init_hw(dev, jrpriv->state.inpbusaddr,
776 jrpriv->state.outbusaddr);
777
778 tasklet_enable(&jrpriv->irqtask);
779 } else if (device_may_wakeup(&pdev->dev)) {
780 disable_irq_wake(jrpriv->irq);
781 }
782
783 add_jr:
784 spin_lock(&driver_data.jr_alloc_lock);
785 list_add_tail(&jrpriv->list_node, &driver_data.jr_list);
786 spin_unlock(&driver_data.jr_alloc_lock);
787
788 if (jrpriv->hwrng)
789 jrpriv->hwrng = !caam_rng_init(dev->parent);
790
791 return 0;
792 }
793
794 static DEFINE_SIMPLE_DEV_PM_OPS(caam_jr_pm_ops, caam_jr_suspend, caam_jr_resume);
795
796 static const struct of_device_id caam_jr_match[] = {
797 {
798 .compatible = "fsl,sec-v4.0-job-ring",
799 },
800 {
801 .compatible = "fsl,sec4.0-job-ring",
802 },
803 {},
804 };
805 MODULE_DEVICE_TABLE(of, caam_jr_match);
806
807 static struct platform_driver caam_jr_driver = {
808 .driver = {
809 .name = "caam_jr",
810 .of_match_table = caam_jr_match,
811 .pm = pm_ptr(&caam_jr_pm_ops),
812 },
813 .probe = caam_jr_probe,
814 .remove = caam_jr_remove,
815 .shutdown = caam_jr_remove,
816 };
817
jr_driver_init(void)818 static int __init jr_driver_init(void)
819 {
820 spin_lock_init(&driver_data.jr_alloc_lock);
821 INIT_LIST_HEAD(&driver_data.jr_list);
822 return platform_driver_register(&caam_jr_driver);
823 }
824
jr_driver_exit(void)825 static void __exit jr_driver_exit(void)
826 {
827 platform_driver_unregister(&caam_jr_driver);
828 }
829
830 module_init(jr_driver_init);
831 module_exit(jr_driver_exit);
832
833 MODULE_LICENSE("GPL");
834 MODULE_DESCRIPTION("FSL CAAM JR request backend");
835 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");
836