jr.c 14 KB

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