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