PageRenderTime 67ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/crypto/caam/jr.c

https://github.com/huangrui/linux
C | 566 lines | 345 code | 105 blank | 116 comment | 38 complexity | 206b05a8c94dc9ef3a226caf95e6af19 MD5 | raw file
  1. /*
  2. * CAAM/SEC 4.x transport/backend driver
  3. * JobR backend functionality
  4. *
  5. * Copyright 2008-2012 Freescale Semiconductor, Inc.
  6. */
  7. #include <linux/of_irq.h>
  8. #include <linux/of_address.h>
  9. #include "compat.h"
  10. #include "regs.h"
  11. #include "jr.h"
  12. #include "desc.h"
  13. #include "intern.h"
  14. struct jr_driver_data {
  15. /* List of Physical JobR's with the Driver */
  16. struct list_head jr_list;
  17. spinlock_t jr_alloc_lock; /* jr_list lock */
  18. } ____cacheline_aligned;
  19. static struct jr_driver_data driver_data;
  20. static int caam_reset_hw_jr(struct device *dev)
  21. {
  22. struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
  23. unsigned int timeout = 100000;
  24. /*
  25. * mask interrupts since we are going to poll
  26. * for reset completion status
  27. */
  28. setbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
  29. /* initiate flush (required prior to reset) */
  30. wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET);
  31. while (((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) ==
  32. JRINT_ERR_HALT_INPROGRESS) && --timeout)
  33. cpu_relax();
  34. if ((rd_reg32(&jrp->rregs->jrintstatus) & JRINT_ERR_HALT_MASK) !=
  35. JRINT_ERR_HALT_COMPLETE || timeout == 0) {
  36. dev_err(dev, "failed to flush job ring %d\n", jrp->ridx);
  37. return -EIO;
  38. }
  39. /* initiate reset */
  40. timeout = 100000;
  41. wr_reg32(&jrp->rregs->jrcommand, JRCR_RESET);
  42. while ((rd_reg32(&jrp->rregs->jrcommand) & JRCR_RESET) && --timeout)
  43. cpu_relax();
  44. if (timeout == 0) {
  45. dev_err(dev, "failed to reset job ring %d\n", jrp->ridx);
  46. return -EIO;
  47. }
  48. /* unmask interrupts */
  49. clrbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
  50. return 0;
  51. }
  52. /*
  53. * Shutdown JobR independent of platform property code
  54. */
  55. static int caam_jr_shutdown(struct device *dev)
  56. {
  57. struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
  58. dma_addr_t inpbusaddr, outbusaddr;
  59. int ret;
  60. ret = caam_reset_hw_jr(dev);
  61. tasklet_kill(&jrp->irqtask);
  62. /* Release interrupt */
  63. free_irq(jrp->irq, dev);
  64. /* Free rings */
  65. inpbusaddr = rd_reg64(&jrp->rregs->inpring_base);
  66. outbusaddr = rd_reg64(&jrp->rregs->outring_base);
  67. dma_free_coherent(dev, sizeof(dma_addr_t) * JOBR_DEPTH,
  68. jrp->inpring, inpbusaddr);
  69. dma_free_coherent(dev, sizeof(struct jr_outentry) * JOBR_DEPTH,
  70. jrp->outring, outbusaddr);
  71. kfree(jrp->entinfo);
  72. return ret;
  73. }
  74. static int caam_jr_remove(struct platform_device *pdev)
  75. {
  76. int ret;
  77. struct device *jrdev;
  78. struct caam_drv_private_jr *jrpriv;
  79. jrdev = &pdev->dev;
  80. jrpriv = dev_get_drvdata(jrdev);
  81. /*
  82. * Return EBUSY if job ring already allocated.
  83. */
  84. if (atomic_read(&jrpriv->tfm_count)) {
  85. dev_err(jrdev, "Device is busy\n");
  86. return -EBUSY;
  87. }
  88. /* Remove the node from Physical JobR list maintained by driver */
  89. spin_lock(&driver_data.jr_alloc_lock);
  90. list_del(&jrpriv->list_node);
  91. spin_unlock(&driver_data.jr_alloc_lock);
  92. /* Release ring */
  93. ret = caam_jr_shutdown(jrdev);
  94. if (ret)
  95. dev_err(jrdev, "Failed to shut down job ring\n");
  96. irq_dispose_mapping(jrpriv->irq);
  97. return ret;
  98. }
  99. /* Main per-ring interrupt handler */
  100. static irqreturn_t caam_jr_interrupt(int irq, void *st_dev)
  101. {
  102. struct device *dev = st_dev;
  103. struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
  104. u32 irqstate;
  105. /*
  106. * Check the output ring for ready responses, kick
  107. * tasklet if jobs done.
  108. */
  109. irqstate = rd_reg32(&jrp->rregs->jrintstatus);
  110. if (!irqstate)
  111. return IRQ_NONE;
  112. /*
  113. * If JobR error, we got more development work to do
  114. * Flag a bug now, but we really need to shut down and
  115. * restart the queue (and fix code).
  116. */
  117. if (irqstate & JRINT_JR_ERROR) {
  118. dev_err(dev, "job ring error: irqstate: %08x\n", irqstate);
  119. BUG();
  120. }
  121. /* mask valid interrupts */
  122. setbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
  123. /* Have valid interrupt at this point, just ACK and trigger */
  124. wr_reg32(&jrp->rregs->jrintstatus, irqstate);
  125. preempt_disable();
  126. tasklet_schedule(&jrp->irqtask);
  127. preempt_enable();
  128. return IRQ_HANDLED;
  129. }
  130. /* Deferred service handler, run as interrupt-fired tasklet */
  131. static void caam_jr_dequeue(unsigned long devarg)
  132. {
  133. int hw_idx, sw_idx, i, head, tail;
  134. struct device *dev = (struct device *)devarg;
  135. struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
  136. void (*usercall)(struct device *dev, u32 *desc, u32 status, void *arg);
  137. u32 *userdesc, userstatus;
  138. void *userarg;
  139. while (rd_reg32(&jrp->rregs->outring_used)) {
  140. head = ACCESS_ONCE(jrp->head);
  141. spin_lock(&jrp->outlock);
  142. sw_idx = tail = jrp->tail;
  143. hw_idx = jrp->out_ring_read_index;
  144. for (i = 0; CIRC_CNT(head, tail + i, JOBR_DEPTH) >= 1; i++) {
  145. sw_idx = (tail + i) & (JOBR_DEPTH - 1);
  146. if (jrp->outring[hw_idx].desc ==
  147. jrp->entinfo[sw_idx].desc_addr_dma)
  148. break; /* found */
  149. }
  150. /* we should never fail to find a matching descriptor */
  151. BUG_ON(CIRC_CNT(head, tail + i, JOBR_DEPTH) <= 0);
  152. /* Unmap just-run descriptor so we can post-process */
  153. dma_unmap_single(dev, jrp->outring[hw_idx].desc,
  154. jrp->entinfo[sw_idx].desc_size,
  155. DMA_TO_DEVICE);
  156. /* mark completed, avoid matching on a recycled desc addr */
  157. jrp->entinfo[sw_idx].desc_addr_dma = 0;
  158. /* Stash callback params for use outside of lock */
  159. usercall = jrp->entinfo[sw_idx].callbk;
  160. userarg = jrp->entinfo[sw_idx].cbkarg;
  161. userdesc = jrp->entinfo[sw_idx].desc_addr_virt;
  162. userstatus = jrp->outring[hw_idx].jrstatus;
  163. /*
  164. * Make sure all information from the job has been obtained
  165. * before telling CAAM that the job has been removed from the
  166. * output ring.
  167. */
  168. mb();
  169. /* set done */
  170. wr_reg32(&jrp->rregs->outring_rmvd, 1);
  171. jrp->out_ring_read_index = (jrp->out_ring_read_index + 1) &
  172. (JOBR_DEPTH - 1);
  173. /*
  174. * if this job completed out-of-order, do not increment
  175. * the tail. Otherwise, increment tail by 1 plus the
  176. * number of subsequent jobs already completed out-of-order
  177. */
  178. if (sw_idx == tail) {
  179. do {
  180. tail = (tail + 1) & (JOBR_DEPTH - 1);
  181. } while (CIRC_CNT(head, tail, JOBR_DEPTH) >= 1 &&
  182. jrp->entinfo[tail].desc_addr_dma == 0);
  183. jrp->tail = tail;
  184. }
  185. spin_unlock(&jrp->outlock);
  186. /* Finally, execute user's callback */
  187. usercall(dev, userdesc, userstatus, userarg);
  188. }
  189. /* reenable / unmask IRQs */
  190. clrbits32(&jrp->rregs->rconfig_lo, JRCFG_IMSK);
  191. }
  192. /**
  193. * caam_jr_alloc() - Alloc a job ring for someone to use as needed.
  194. *
  195. * returns : pointer to the newly allocated physical
  196. * JobR dev can be written to if successful.
  197. **/
  198. struct device *caam_jr_alloc(void)
  199. {
  200. struct caam_drv_private_jr *jrpriv, *min_jrpriv = NULL;
  201. struct device *dev = NULL;
  202. int min_tfm_cnt = INT_MAX;
  203. int tfm_cnt;
  204. spin_lock(&driver_data.jr_alloc_lock);
  205. if (list_empty(&driver_data.jr_list)) {
  206. spin_unlock(&driver_data.jr_alloc_lock);
  207. return ERR_PTR(-ENODEV);
  208. }
  209. list_for_each_entry(jrpriv, &driver_data.jr_list, list_node) {
  210. tfm_cnt = atomic_read(&jrpriv->tfm_count);
  211. if (tfm_cnt < min_tfm_cnt) {
  212. min_tfm_cnt = tfm_cnt;
  213. min_jrpriv = jrpriv;
  214. }
  215. if (!min_tfm_cnt)
  216. break;
  217. }
  218. if (min_jrpriv) {
  219. atomic_inc(&min_jrpriv->tfm_count);
  220. dev = min_jrpriv->dev;
  221. }
  222. spin_unlock(&driver_data.jr_alloc_lock);
  223. return dev;
  224. }
  225. EXPORT_SYMBOL(caam_jr_alloc);
  226. /**
  227. * caam_jr_free() - Free the Job Ring
  228. * @rdev - points to the dev that identifies the Job ring to
  229. * be released.
  230. **/
  231. void caam_jr_free(struct device *rdev)
  232. {
  233. struct caam_drv_private_jr *jrpriv = dev_get_drvdata(rdev);
  234. atomic_dec(&jrpriv->tfm_count);
  235. }
  236. EXPORT_SYMBOL(caam_jr_free);
  237. /**
  238. * caam_jr_enqueue() - Enqueue a job descriptor head. Returns 0 if OK,
  239. * -EBUSY if the queue is full, -EIO if it cannot map the caller's
  240. * descriptor.
  241. * @dev: device of the job ring to be used. This device should have
  242. * been assigned prior by caam_jr_register().
  243. * @desc: points to a job descriptor that execute our request. All
  244. * descriptors (and all referenced data) must be in a DMAable
  245. * region, and all data references must be physical addresses
  246. * accessible to CAAM (i.e. within a PAMU window granted
  247. * to it).
  248. * @cbk: pointer to a callback function to be invoked upon completion
  249. * of this request. This has the form:
  250. * callback(struct device *dev, u32 *desc, u32 stat, void *arg)
  251. * where:
  252. * @dev: contains the job ring device that processed this
  253. * response.
  254. * @desc: descriptor that initiated the request, same as
  255. * "desc" being argued to caam_jr_enqueue().
  256. * @status: untranslated status received from CAAM. See the
  257. * reference manual for a detailed description of
  258. * error meaning, or see the JRSTA definitions in the
  259. * register header file
  260. * @areq: optional pointer to an argument passed with the
  261. * original request
  262. * @areq: optional pointer to a user argument for use at callback
  263. * time.
  264. **/
  265. int caam_jr_enqueue(struct device *dev, u32 *desc,
  266. void (*cbk)(struct device *dev, u32 *desc,
  267. u32 status, void *areq),
  268. void *areq)
  269. {
  270. struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
  271. struct caam_jrentry_info *head_entry;
  272. int head, tail, desc_size;
  273. dma_addr_t desc_dma;
  274. desc_size = (*desc & HDR_JD_LENGTH_MASK) * sizeof(u32);
  275. desc_dma = dma_map_single(dev, desc, desc_size, DMA_TO_DEVICE);
  276. if (dma_mapping_error(dev, desc_dma)) {
  277. dev_err(dev, "caam_jr_enqueue(): can't map jobdesc\n");
  278. return -EIO;
  279. }
  280. spin_lock_bh(&jrp->inplock);
  281. head = jrp->head;
  282. tail = ACCESS_ONCE(jrp->tail);
  283. if (!rd_reg32(&jrp->rregs->inpring_avail) ||
  284. CIRC_SPACE(head, tail, JOBR_DEPTH) <= 0) {
  285. spin_unlock_bh(&jrp->inplock);
  286. dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE);
  287. return -EBUSY;
  288. }
  289. head_entry = &jrp->entinfo[head];
  290. head_entry->desc_addr_virt = desc;
  291. head_entry->desc_size = desc_size;
  292. head_entry->callbk = (void *)cbk;
  293. head_entry->cbkarg = areq;
  294. head_entry->desc_addr_dma = desc_dma;
  295. jrp->inpring[jrp->inp_ring_write_index] = desc_dma;
  296. /*
  297. * Guarantee that the descriptor's DMA address has been written to
  298. * the next slot in the ring before the write index is updated, since
  299. * other cores may update this index independently.
  300. */
  301. smp_wmb();
  302. jrp->inp_ring_write_index = (jrp->inp_ring_write_index + 1) &
  303. (JOBR_DEPTH - 1);
  304. jrp->head = (head + 1) & (JOBR_DEPTH - 1);
  305. /*
  306. * Ensure that all job information has been written before
  307. * notifying CAAM that a new job was added to the input ring.
  308. */
  309. wmb();
  310. wr_reg32(&jrp->rregs->inpring_jobadd, 1);
  311. spin_unlock_bh(&jrp->inplock);
  312. return 0;
  313. }
  314. EXPORT_SYMBOL(caam_jr_enqueue);
  315. /*
  316. * Init JobR independent of platform property detection
  317. */
  318. static int caam_jr_init(struct device *dev)
  319. {
  320. struct caam_drv_private_jr *jrp;
  321. dma_addr_t inpbusaddr, outbusaddr;
  322. int i, error;
  323. jrp = dev_get_drvdata(dev);
  324. tasklet_init(&jrp->irqtask, caam_jr_dequeue, (unsigned long)dev);
  325. /* Connect job ring interrupt handler. */
  326. error = request_irq(jrp->irq, caam_jr_interrupt, IRQF_SHARED,
  327. dev_name(dev), dev);
  328. if (error) {
  329. dev_err(dev, "can't connect JobR %d interrupt (%d)\n",
  330. jrp->ridx, jrp->irq);
  331. goto out_kill_deq;
  332. }
  333. error = caam_reset_hw_jr(dev);
  334. if (error)
  335. goto out_free_irq;
  336. error = -ENOMEM;
  337. jrp->inpring = dma_alloc_coherent(dev, sizeof(*jrp->inpring) *
  338. JOBR_DEPTH, &inpbusaddr, GFP_KERNEL);
  339. if (!jrp->inpring)
  340. goto out_free_irq;
  341. jrp->outring = dma_alloc_coherent(dev, sizeof(*jrp->outring) *
  342. JOBR_DEPTH, &outbusaddr, GFP_KERNEL);
  343. if (!jrp->outring)
  344. goto out_free_inpring;
  345. jrp->entinfo = kcalloc(JOBR_DEPTH, sizeof(*jrp->entinfo), GFP_KERNEL);
  346. if (!jrp->entinfo)
  347. goto out_free_outring;
  348. for (i = 0; i < JOBR_DEPTH; i++)
  349. jrp->entinfo[i].desc_addr_dma = !0;
  350. /* Setup rings */
  351. jrp->inp_ring_write_index = 0;
  352. jrp->out_ring_read_index = 0;
  353. jrp->head = 0;
  354. jrp->tail = 0;
  355. wr_reg64(&jrp->rregs->inpring_base, inpbusaddr);
  356. wr_reg64(&jrp->rregs->outring_base, outbusaddr);
  357. wr_reg32(&jrp->rregs->inpring_size, JOBR_DEPTH);
  358. wr_reg32(&jrp->rregs->outring_size, JOBR_DEPTH);
  359. jrp->ringsize = JOBR_DEPTH;
  360. spin_lock_init(&jrp->inplock);
  361. spin_lock_init(&jrp->outlock);
  362. /* Select interrupt coalescing parameters */
  363. setbits32(&jrp->rregs->rconfig_lo, JOBR_INTC |
  364. (JOBR_INTC_COUNT_THLD << JRCFG_ICDCT_SHIFT) |
  365. (JOBR_INTC_TIME_THLD << JRCFG_ICTT_SHIFT));
  366. return 0;
  367. out_free_outring:
  368. dma_free_coherent(dev, sizeof(struct jr_outentry) * JOBR_DEPTH,
  369. jrp->outring, outbusaddr);
  370. out_free_inpring:
  371. dma_free_coherent(dev, sizeof(dma_addr_t) * JOBR_DEPTH,
  372. jrp->inpring, inpbusaddr);
  373. dev_err(dev, "can't allocate job rings for %d\n", jrp->ridx);
  374. out_free_irq:
  375. free_irq(jrp->irq, dev);
  376. out_kill_deq:
  377. tasklet_kill(&jrp->irqtask);
  378. return error;
  379. }
  380. /*
  381. * Probe routine for each detected JobR subsystem.
  382. */
  383. static int caam_jr_probe(struct platform_device *pdev)
  384. {
  385. struct device *jrdev;
  386. struct device_node *nprop;
  387. struct caam_job_ring __iomem *ctrl;
  388. struct caam_drv_private_jr *jrpriv;
  389. static int total_jobrs;
  390. int error;
  391. jrdev = &pdev->dev;
  392. jrpriv = devm_kmalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL);
  393. if (!jrpriv)
  394. return -ENOMEM;
  395. dev_set_drvdata(jrdev, jrpriv);
  396. /* save ring identity relative to detection */
  397. jrpriv->ridx = total_jobrs++;
  398. nprop = pdev->dev.of_node;
  399. /* Get configuration properties from device tree */
  400. /* First, get register page */
  401. ctrl = of_iomap(nprop, 0);
  402. if (!ctrl) {
  403. dev_err(jrdev, "of_iomap() failed\n");
  404. return -ENOMEM;
  405. }
  406. jrpriv->rregs = (struct caam_job_ring __force *)ctrl;
  407. if (sizeof(dma_addr_t) == sizeof(u64))
  408. if (of_device_is_compatible(nprop, "fsl,sec-v5.0-job-ring"))
  409. dma_set_mask_and_coherent(jrdev, DMA_BIT_MASK(40));
  410. else
  411. dma_set_mask_and_coherent(jrdev, DMA_BIT_MASK(36));
  412. else
  413. dma_set_mask_and_coherent(jrdev, DMA_BIT_MASK(32));
  414. /* Identify the interrupt */
  415. jrpriv->irq = irq_of_parse_and_map(nprop, 0);
  416. /* Now do the platform independent part */
  417. error = caam_jr_init(jrdev); /* now turn on hardware */
  418. if (error) {
  419. irq_dispose_mapping(jrpriv->irq);
  420. return error;
  421. }
  422. jrpriv->dev = jrdev;
  423. spin_lock(&driver_data.jr_alloc_lock);
  424. list_add_tail(&jrpriv->list_node, &driver_data.jr_list);
  425. spin_unlock(&driver_data.jr_alloc_lock);
  426. atomic_set(&jrpriv->tfm_count, 0);
  427. return 0;
  428. }
  429. static struct of_device_id caam_jr_match[] = {
  430. {
  431. .compatible = "fsl,sec-v4.0-job-ring",
  432. },
  433. {
  434. .compatible = "fsl,sec4.0-job-ring",
  435. },
  436. {},
  437. };
  438. MODULE_DEVICE_TABLE(of, caam_jr_match);
  439. static struct platform_driver caam_jr_driver = {
  440. .driver = {
  441. .name = "caam_jr",
  442. .of_match_table = caam_jr_match,
  443. },
  444. .probe = caam_jr_probe,
  445. .remove = caam_jr_remove,
  446. };
  447. static int __init jr_driver_init(void)
  448. {
  449. spin_lock_init(&driver_data.jr_alloc_lock);
  450. INIT_LIST_HEAD(&driver_data.jr_list);
  451. return platform_driver_register(&caam_jr_driver);
  452. }
  453. static void __exit jr_driver_exit(void)
  454. {
  455. platform_driver_unregister(&caam_jr_driver);
  456. }
  457. module_init(jr_driver_init);
  458. module_exit(jr_driver_exit);
  459. MODULE_LICENSE("GPL");
  460. MODULE_DESCRIPTION("FSL CAAM JR request backend");
  461. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");