jr.c 22 KB

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