jr.c 15 KB

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