xref: /linux/drivers/crypto/caam/jr.c (revision 23d422a4f127901b2e58d925f130d4ce534a662a)
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 & JRINT_JR_INT))
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 	 * Under heavy DDR load, smp_wmb() or dma_wmb() fail to make the input
469 	 * ring be updated before the CAAM starts reading it. So, CAAM will
470 	 * process, again, an old descriptor address and will put it in the
471 	 * output ring. This will make caam_jr_dequeue() to fail, since this
472 	 * old descriptor is not in the software ring.
473 	 * To fix this, use wmb() which works on the full system instead of
474 	 * inner/outer shareable domains.
475 	 */
476 	wmb();
477 
478 	jrp->head = (head + 1) & (JOBR_DEPTH - 1);
479 
480 	/*
481 	 * Ensure that all job information has been written before
482 	 * notifying CAAM that a new job was added to the input ring
483 	 * using a memory barrier. The wr_reg32() uses api iowrite32()
484 	 * to do the register write. iowrite32() issues a memory barrier
485 	 * before the write operation.
486 	 */
487 
488 	wr_reg32(&jrp->rregs->inpring_jobadd, 1);
489 
490 	jrp->inpring_avail--;
491 	if (!jrp->inpring_avail)
492 		jrp->inpring_avail = rd_reg32(&jrp->rregs->inpring_avail);
493 
494 	spin_unlock_bh(&jrp->inplock);
495 
496 	return -EINPROGRESS;
497 }
498 EXPORT_SYMBOL(caam_jr_enqueue);
499 
500 static void caam_jr_init_hw(struct device *dev, dma_addr_t inpbusaddr,
501 			    dma_addr_t outbusaddr)
502 {
503 	struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
504 
505 	wr_reg64(&jrp->rregs->inpring_base, inpbusaddr);
506 	wr_reg64(&jrp->rregs->outring_base, outbusaddr);
507 	wr_reg32(&jrp->rregs->inpring_size, JOBR_DEPTH);
508 	wr_reg32(&jrp->rregs->outring_size, JOBR_DEPTH);
509 
510 	/* Select interrupt coalescing parameters */
511 	clrsetbits_32(&jrp->rregs->rconfig_lo, 0, JOBR_INTC |
512 		      (JOBR_INTC_COUNT_THLD << JRCFG_ICDCT_SHIFT) |
513 		      (JOBR_INTC_TIME_THLD << JRCFG_ICTT_SHIFT));
514 }
515 
516 static void caam_jr_reset_index(struct caam_drv_private_jr *jrp)
517 {
518 	jrp->out_ring_read_index = 0;
519 	jrp->head = 0;
520 	jrp->tail = 0;
521 }
522 
523 /*
524  * Init JobR independent of platform property detection
525  */
526 static int caam_jr_init(struct device *dev)
527 {
528 	struct caam_drv_private_jr *jrp;
529 	dma_addr_t inpbusaddr, outbusaddr;
530 	int i, error;
531 
532 	jrp = dev_get_drvdata(dev);
533 
534 	error = caam_reset_hw_jr(dev);
535 	if (error)
536 		return error;
537 
538 	jrp->inpring = dmam_alloc_coherent(dev, SIZEOF_JR_INPENTRY *
539 					   JOBR_DEPTH, &inpbusaddr,
540 					   GFP_KERNEL);
541 	if (!jrp->inpring)
542 		return -ENOMEM;
543 
544 	jrp->outring = dmam_alloc_coherent(dev, SIZEOF_JR_OUTENTRY *
545 					   JOBR_DEPTH, &outbusaddr,
546 					   GFP_KERNEL);
547 	if (!jrp->outring)
548 		return -ENOMEM;
549 
550 	jrp->entinfo = devm_kcalloc(dev, JOBR_DEPTH, sizeof(*jrp->entinfo),
551 				    GFP_KERNEL);
552 	if (!jrp->entinfo)
553 		return -ENOMEM;
554 
555 	for (i = 0; i < JOBR_DEPTH; i++)
556 		jrp->entinfo[i].desc_addr_dma = !0;
557 
558 	/* Setup rings */
559 	caam_jr_reset_index(jrp);
560 	jrp->inpring_avail = JOBR_DEPTH;
561 	caam_jr_init_hw(dev, inpbusaddr, outbusaddr);
562 
563 	spin_lock_init(&jrp->inplock);
564 
565 	jrp->tasklet_params.dev = dev;
566 	jrp->tasklet_params.enable_itr = 1;
567 	tasklet_init(&jrp->irqtask, caam_jr_dequeue,
568 		     (unsigned long)&jrp->tasklet_params);
569 
570 	/* Connect job ring interrupt handler. */
571 	error = devm_request_irq(dev, jrp->irq, caam_jr_interrupt, IRQF_SHARED,
572 				 dev_name(dev), dev);
573 	if (error) {
574 		dev_err(dev, "can't connect JobR %d interrupt (%d)\n",
575 			jrp->ridx, jrp->irq);
576 		tasklet_kill(&jrp->irqtask);
577 	}
578 
579 	return error;
580 }
581 
582 static void caam_jr_irq_dispose_mapping(void *data)
583 {
584 	irq_dispose_mapping((unsigned long)data);
585 }
586 
587 /*
588  * Probe routine for each detected JobR subsystem.
589  */
590 static int caam_jr_probe(struct platform_device *pdev)
591 {
592 	struct device *jrdev;
593 	struct device_node *nprop;
594 	struct caam_job_ring __iomem *ctrl;
595 	struct caam_drv_private_jr *jrpriv;
596 	static int total_jobrs;
597 	struct resource *r;
598 	int error;
599 
600 	jrdev = &pdev->dev;
601 	jrpriv = devm_kzalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL);
602 	if (!jrpriv)
603 		return -ENOMEM;
604 
605 	dev_set_drvdata(jrdev, jrpriv);
606 
607 	/* save ring identity relative to detection */
608 	jrpriv->ridx = total_jobrs++;
609 
610 	nprop = pdev->dev.of_node;
611 	/* Get configuration properties from device tree */
612 	/* First, get register page */
613 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
614 	if (!r) {
615 		dev_err(jrdev, "platform_get_resource() failed\n");
616 		return -ENOMEM;
617 	}
618 
619 	ctrl = devm_ioremap(jrdev, r->start, resource_size(r));
620 	if (!ctrl) {
621 		dev_err(jrdev, "devm_ioremap() failed\n");
622 		return -ENOMEM;
623 	}
624 
625 	jrpriv->rregs = (struct caam_job_ring __iomem __force *)ctrl;
626 
627 	error = dma_set_mask_and_coherent(jrdev, caam_get_dma_mask(jrdev));
628 	if (error) {
629 		dev_err(jrdev, "dma_set_mask_and_coherent failed (%d)\n",
630 			error);
631 		return error;
632 	}
633 
634 	/* Initialize crypto engine */
635 	jrpriv->engine = crypto_engine_alloc_init_and_set(jrdev, true, NULL,
636 							  false,
637 							  CRYPTO_ENGINE_MAX_QLEN);
638 	if (!jrpriv->engine) {
639 		dev_err(jrdev, "Could not init crypto-engine\n");
640 		return -ENOMEM;
641 	}
642 
643 	error = devm_add_action_or_reset(jrdev, caam_jr_crypto_engine_exit,
644 					 jrdev);
645 	if (error)
646 		return error;
647 
648 	/* Start crypto engine */
649 	error = crypto_engine_start(jrpriv->engine);
650 	if (error) {
651 		dev_err(jrdev, "Could not start crypto-engine\n");
652 		return error;
653 	}
654 
655 	/* Identify the interrupt */
656 	jrpriv->irq = irq_of_parse_and_map(nprop, 0);
657 	if (!jrpriv->irq) {
658 		dev_err(jrdev, "irq_of_parse_and_map failed\n");
659 		return -EINVAL;
660 	}
661 
662 	error = devm_add_action_or_reset(jrdev, caam_jr_irq_dispose_mapping,
663 					 (void *)(unsigned long)jrpriv->irq);
664 	if (error)
665 		return error;
666 
667 	/* Now do the platform independent part */
668 	error = caam_jr_init(jrdev); /* now turn on hardware */
669 	if (error)
670 		return error;
671 
672 	jrpriv->dev = jrdev;
673 	spin_lock(&driver_data.jr_alloc_lock);
674 	list_add_tail(&jrpriv->list_node, &driver_data.jr_list);
675 	spin_unlock(&driver_data.jr_alloc_lock);
676 
677 	atomic_set(&jrpriv->tfm_count, 0);
678 
679 	device_init_wakeup(&pdev->dev, 1);
680 	device_set_wakeup_enable(&pdev->dev, false);
681 
682 	register_algs(jrpriv, jrdev->parent);
683 
684 	return 0;
685 }
686 
687 static void caam_jr_get_hw_state(struct device *dev)
688 {
689 	struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
690 
691 	jrp->state.inpbusaddr = rd_reg64(&jrp->rregs->inpring_base);
692 	jrp->state.outbusaddr = rd_reg64(&jrp->rregs->outring_base);
693 }
694 
695 static int caam_jr_suspend(struct device *dev)
696 {
697 	struct platform_device *pdev = to_platform_device(dev);
698 	struct caam_drv_private_jr *jrpriv = platform_get_drvdata(pdev);
699 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev->parent);
700 	struct caam_jr_dequeue_params suspend_params = {
701 		.dev = dev,
702 		.enable_itr = 0,
703 	};
704 
705 	/* Remove the node from Physical JobR list maintained by driver */
706 	spin_lock(&driver_data.jr_alloc_lock);
707 	list_del(&jrpriv->list_node);
708 	spin_unlock(&driver_data.jr_alloc_lock);
709 
710 	if (jrpriv->hwrng)
711 		caam_rng_exit(dev->parent);
712 
713 	if (ctrlpriv->caam_off_during_pm) {
714 		int err;
715 
716 		tasklet_disable(&jrpriv->irqtask);
717 
718 		/* mask itr to call flush */
719 		clrsetbits_32(&jrpriv->rregs->rconfig_lo, 0, JRCFG_IMSK);
720 
721 		/* Invalid job in process */
722 		err = caam_jr_flush(dev);
723 		if (err) {
724 			dev_err(dev, "Failed to flush\n");
725 			return err;
726 		}
727 
728 		/* Dequeing jobs flushed */
729 		caam_jr_dequeue((unsigned long)&suspend_params);
730 
731 		/* Save state */
732 		caam_jr_get_hw_state(dev);
733 	} else if (device_may_wakeup(&pdev->dev)) {
734 		enable_irq_wake(jrpriv->irq);
735 	}
736 
737 	return 0;
738 }
739 
740 static int caam_jr_resume(struct device *dev)
741 {
742 	struct platform_device *pdev = to_platform_device(dev);
743 	struct caam_drv_private_jr *jrpriv = platform_get_drvdata(pdev);
744 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev->parent);
745 
746 	if (ctrlpriv->caam_off_during_pm) {
747 		u64 inp_addr;
748 		int err;
749 
750 		/*
751 		 * Check if the CAAM has been resetted checking the address of
752 		 * the input ring
753 		 */
754 		inp_addr = rd_reg64(&jrpriv->rregs->inpring_base);
755 		if (inp_addr != 0) {
756 			/* JR still has some configuration */
757 			if (inp_addr == jrpriv->state.inpbusaddr) {
758 				/* JR has not been resetted */
759 				err = caam_jr_restart_processing(dev);
760 				if (err) {
761 					dev_err(dev,
762 						"Restart processing failed\n");
763 					return err;
764 				}
765 
766 				tasklet_enable(&jrpriv->irqtask);
767 
768 				clrsetbits_32(&jrpriv->rregs->rconfig_lo,
769 					      JRCFG_IMSK, 0);
770 
771 				goto add_jr;
772 			} else if (ctrlpriv->optee_en) {
773 				/* JR has been used by OPTEE, reset it */
774 				err = caam_reset_hw_jr(dev);
775 				if (err) {
776 					dev_err(dev, "Failed to reset JR\n");
777 					return err;
778 				}
779 			} else {
780 				/* No explanation, return error */
781 				return -EIO;
782 			}
783 		}
784 
785 		caam_jr_reset_index(jrpriv);
786 		caam_jr_init_hw(dev, jrpriv->state.inpbusaddr,
787 				jrpriv->state.outbusaddr);
788 
789 		tasklet_enable(&jrpriv->irqtask);
790 	} else if (device_may_wakeup(&pdev->dev)) {
791 		disable_irq_wake(jrpriv->irq);
792 	}
793 
794 add_jr:
795 	spin_lock(&driver_data.jr_alloc_lock);
796 	list_add_tail(&jrpriv->list_node, &driver_data.jr_list);
797 	spin_unlock(&driver_data.jr_alloc_lock);
798 
799 	if (jrpriv->hwrng)
800 		jrpriv->hwrng = !caam_rng_init(dev->parent);
801 
802 	return 0;
803 }
804 
805 static DEFINE_SIMPLE_DEV_PM_OPS(caam_jr_pm_ops, caam_jr_suspend, caam_jr_resume);
806 
807 static const struct of_device_id caam_jr_match[] = {
808 	{
809 		.compatible = "fsl,sec-v4.0-job-ring",
810 	},
811 	{
812 		.compatible = "fsl,sec4.0-job-ring",
813 	},
814 	{},
815 };
816 MODULE_DEVICE_TABLE(of, caam_jr_match);
817 
818 static struct platform_driver caam_jr_driver = {
819 	.driver = {
820 		.name = "caam_jr",
821 		.of_match_table = caam_jr_match,
822 		.pm = pm_ptr(&caam_jr_pm_ops),
823 	},
824 	.probe       = caam_jr_probe,
825 	.remove      = caam_jr_remove,
826 	.shutdown    = caam_jr_platform_shutdown,
827 };
828 
829 static int __init jr_driver_init(void)
830 {
831 	spin_lock_init(&driver_data.jr_alloc_lock);
832 	INIT_LIST_HEAD(&driver_data.jr_list);
833 	return platform_driver_register(&caam_jr_driver);
834 }
835 
836 static void __exit jr_driver_exit(void)
837 {
838 	platform_driver_unregister(&caam_jr_driver);
839 }
840 
841 module_init(jr_driver_init);
842 module_exit(jr_driver_exit);
843 
844 MODULE_LICENSE("GPL");
845 MODULE_DESCRIPTION("FSL CAAM JR request backend");
846 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");
847