ctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * CAAM control-plane driver backend
  3. * Controller-level driver, kernel property detection, initialization
  4. *
  5. * Copyright 2008-2012 Freescale Semiconductor, Inc.
  6. */
  7. #include "compat.h"
  8. #include "regs.h"
  9. #include "intern.h"
  10. #include "jr.h"
  11. #include "desc_constr.h"
  12. #include "error.h"
  13. #include "ctrl.h"
  14. static int caam_remove(struct platform_device *pdev)
  15. {
  16. struct device *ctrldev;
  17. struct caam_drv_private *ctrlpriv;
  18. struct caam_drv_private_jr *jrpriv;
  19. struct caam_full __iomem *topregs;
  20. int ring, ret = 0;
  21. ctrldev = &pdev->dev;
  22. ctrlpriv = dev_get_drvdata(ctrldev);
  23. topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
  24. /* shut down JobRs */
  25. for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
  26. ret |= caam_jr_shutdown(ctrlpriv->jrdev[ring]);
  27. jrpriv = dev_get_drvdata(ctrlpriv->jrdev[ring]);
  28. irq_dispose_mapping(jrpriv->irq);
  29. }
  30. /* Shut down debug views */
  31. #ifdef CONFIG_DEBUG_FS
  32. debugfs_remove_recursive(ctrlpriv->dfs_root);
  33. #endif
  34. /* Unmap controller region */
  35. iounmap(&topregs->ctrl);
  36. kfree(ctrlpriv->jrdev);
  37. kfree(ctrlpriv);
  38. return ret;
  39. }
  40. /*
  41. * Descriptor to instantiate RNG State Handle 0 in normal mode and
  42. * load the JDKEK, TDKEK and TDSK registers
  43. */
  44. static void build_instantiation_desc(u32 *desc)
  45. {
  46. u32 *jump_cmd;
  47. init_job_desc(desc, 0);
  48. /* INIT RNG in non-test mode */
  49. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  50. OP_ALG_AS_INIT);
  51. /* wait for done */
  52. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
  53. set_jump_tgt_here(desc, jump_cmd);
  54. /*
  55. * load 1 to clear written reg:
  56. * resets the done interrrupt and returns the RNG to idle.
  57. */
  58. append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
  59. /* generate secure keys (non-test) */
  60. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  61. OP_ALG_RNG4_SK);
  62. }
  63. struct instantiate_result {
  64. struct completion completion;
  65. int err;
  66. };
  67. static void rng4_init_done(struct device *dev, u32 *desc, u32 err,
  68. void *context)
  69. {
  70. struct instantiate_result *instantiation = context;
  71. if (err) {
  72. char tmp[CAAM_ERROR_STR_MAX];
  73. dev_err(dev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  74. }
  75. instantiation->err = err;
  76. complete(&instantiation->completion);
  77. }
  78. static int instantiate_rng(struct device *jrdev)
  79. {
  80. struct instantiate_result instantiation;
  81. dma_addr_t desc_dma;
  82. u32 *desc;
  83. int ret;
  84. desc = kmalloc(CAAM_CMD_SZ * 6, GFP_KERNEL | GFP_DMA);
  85. if (!desc) {
  86. dev_err(jrdev, "cannot allocate RNG init descriptor memory\n");
  87. return -ENOMEM;
  88. }
  89. build_instantiation_desc(desc);
  90. desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc), DMA_TO_DEVICE);
  91. init_completion(&instantiation.completion);
  92. ret = caam_jr_enqueue(jrdev, desc, rng4_init_done, &instantiation);
  93. if (!ret) {
  94. wait_for_completion_interruptible(&instantiation.completion);
  95. ret = instantiation.err;
  96. if (ret)
  97. dev_err(jrdev, "unable to instantiate RNG\n");
  98. }
  99. dma_unmap_single(jrdev, desc_dma, desc_bytes(desc), DMA_TO_DEVICE);
  100. kfree(desc);
  101. return ret;
  102. }
  103. /*
  104. * By default, the TRNG runs for 200 clocks per sample;
  105. * 1600 clocks per sample generates better entropy.
  106. */
  107. static void kick_trng(struct platform_device *pdev)
  108. {
  109. struct device *ctrldev = &pdev->dev;
  110. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  111. struct caam_full __iomem *topregs;
  112. struct rng4tst __iomem *r4tst;
  113. u32 val;
  114. topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
  115. r4tst = &topregs->ctrl.r4tst[0];
  116. /* put RNG4 into program mode */
  117. setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  118. /* 1600 clocks per sample */
  119. val = rd_reg32(&r4tst->rtsdctl);
  120. val = (val & ~RTSDCTL_ENT_DLY_MASK) | (1600 << RTSDCTL_ENT_DLY_SHIFT);
  121. wr_reg32(&r4tst->rtsdctl, val);
  122. /* min. freq. count */
  123. wr_reg32(&r4tst->rtfrqmin, 400);
  124. /* max. freq. count */
  125. wr_reg32(&r4tst->rtfrqmax, 6400);
  126. /* put RNG4 into run mode */
  127. clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  128. }
  129. /**
  130. * caam_get_era() - Return the ERA of the SEC on SoC, based
  131. * on the SEC_VID register.
  132. * Returns the ERA number (1..4) or -ENOTSUPP if the ERA is unknown.
  133. * @caam_id - the value of the SEC_VID register
  134. **/
  135. int caam_get_era(u64 caam_id)
  136. {
  137. struct sec_vid *sec_vid = (struct sec_vid *)&caam_id;
  138. static const struct {
  139. u16 ip_id;
  140. u8 maj_rev;
  141. u8 era;
  142. } caam_eras[] = {
  143. {0x0A10, 1, 1},
  144. {0x0A10, 2, 2},
  145. {0x0A12, 1, 3},
  146. {0x0A14, 1, 3},
  147. {0x0A14, 2, 4},
  148. {0x0A16, 1, 4},
  149. {0x0A11, 1, 4}
  150. };
  151. int i;
  152. for (i = 0; i < ARRAY_SIZE(caam_eras); i++)
  153. if (caam_eras[i].ip_id == sec_vid->ip_id &&
  154. caam_eras[i].maj_rev == sec_vid->maj_rev)
  155. return caam_eras[i].era;
  156. return -ENOTSUPP;
  157. }
  158. EXPORT_SYMBOL(caam_get_era);
  159. /* Probe routine for CAAM top (controller) level */
  160. static int caam_probe(struct platform_device *pdev)
  161. {
  162. int ret, ring, rspec;
  163. u64 caam_id;
  164. struct device *dev;
  165. struct device_node *nprop, *np;
  166. struct caam_ctrl __iomem *ctrl;
  167. struct caam_full __iomem *topregs;
  168. struct caam_drv_private *ctrlpriv;
  169. #ifdef CONFIG_DEBUG_FS
  170. struct caam_perfmon *perfmon;
  171. #endif
  172. ctrlpriv = kzalloc(sizeof(struct caam_drv_private), GFP_KERNEL);
  173. if (!ctrlpriv)
  174. return -ENOMEM;
  175. dev = &pdev->dev;
  176. dev_set_drvdata(dev, ctrlpriv);
  177. ctrlpriv->pdev = pdev;
  178. nprop = pdev->dev.of_node;
  179. /* Get configuration properties from device tree */
  180. /* First, get register page */
  181. ctrl = of_iomap(nprop, 0);
  182. if (ctrl == NULL) {
  183. dev_err(dev, "caam: of_iomap() failed\n");
  184. return -ENOMEM;
  185. }
  186. ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
  187. /* topregs used to derive pointers to CAAM sub-blocks only */
  188. topregs = (struct caam_full __iomem *)ctrl;
  189. /* Get the IRQ of the controller (for security violations only) */
  190. ctrlpriv->secvio_irq = of_irq_to_resource(nprop, 0, NULL);
  191. /*
  192. * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
  193. * long pointers in master configuration register
  194. */
  195. setbits32(&topregs->ctrl.mcr, MCFGR_WDENABLE |
  196. (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
  197. if (sizeof(dma_addr_t) == sizeof(u64))
  198. if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
  199. dma_set_mask(dev, DMA_BIT_MASK(40));
  200. else
  201. dma_set_mask(dev, DMA_BIT_MASK(36));
  202. else
  203. dma_set_mask(dev, DMA_BIT_MASK(32));
  204. /*
  205. * Detect and enable JobRs
  206. * First, find out how many ring spec'ed, allocate references
  207. * for all, then go probe each one.
  208. */
  209. rspec = 0;
  210. for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring")
  211. rspec++;
  212. if (!rspec) {
  213. /* for backward compatible with device trees */
  214. for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring")
  215. rspec++;
  216. }
  217. ctrlpriv->jrdev = kzalloc(sizeof(struct device *) * rspec, GFP_KERNEL);
  218. if (ctrlpriv->jrdev == NULL) {
  219. iounmap(&topregs->ctrl);
  220. return -ENOMEM;
  221. }
  222. ring = 0;
  223. ctrlpriv->total_jobrs = 0;
  224. for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring") {
  225. caam_jr_probe(pdev, np, ring);
  226. ctrlpriv->total_jobrs++;
  227. ring++;
  228. }
  229. if (!ring) {
  230. for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring") {
  231. caam_jr_probe(pdev, np, ring);
  232. ctrlpriv->total_jobrs++;
  233. ring++;
  234. }
  235. }
  236. /* Check to see if QI present. If so, enable */
  237. ctrlpriv->qi_present = !!(rd_reg64(&topregs->ctrl.perfmon.comp_parms) &
  238. CTPR_QI_MASK);
  239. if (ctrlpriv->qi_present) {
  240. ctrlpriv->qi = (struct caam_queue_if __force *)&topregs->qi;
  241. /* This is all that's required to physically enable QI */
  242. wr_reg32(&topregs->qi.qi_control_lo, QICTL_DQEN);
  243. }
  244. /* If no QI and no rings specified, quit and go home */
  245. if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
  246. dev_err(dev, "no queues configured, terminating\n");
  247. caam_remove(pdev);
  248. return -ENOMEM;
  249. }
  250. /*
  251. * RNG4 based SECs (v5+) need special initialization prior
  252. * to executing any descriptors
  253. */
  254. if (of_device_is_compatible(nprop, "fsl,sec-v5.0")) {
  255. kick_trng(pdev);
  256. ret = instantiate_rng(ctrlpriv->jrdev[0]);
  257. if (ret) {
  258. caam_remove(pdev);
  259. return ret;
  260. }
  261. }
  262. /* NOTE: RTIC detection ought to go here, around Si time */
  263. /* Initialize queue allocator lock */
  264. spin_lock_init(&ctrlpriv->jr_alloc_lock);
  265. caam_id = rd_reg64(&topregs->ctrl.perfmon.caam_id);
  266. /* Report "alive" for developer to see */
  267. dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
  268. caam_get_era(caam_id));
  269. dev_info(dev, "job rings = %d, qi = %d\n",
  270. ctrlpriv->total_jobrs, ctrlpriv->qi_present);
  271. #ifdef CONFIG_DEBUG_FS
  272. /*
  273. * FIXME: needs better naming distinction, as some amalgamation of
  274. * "caam" and nprop->full_name. The OF name isn't distinctive,
  275. * but does separate instances
  276. */
  277. perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
  278. ctrlpriv->dfs_root = debugfs_create_dir("caam", NULL);
  279. ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
  280. /* Controller-level - performance monitor counters */
  281. ctrlpriv->ctl_rq_dequeued =
  282. debugfs_create_u64("rq_dequeued",
  283. S_IRUSR | S_IRGRP | S_IROTH,
  284. ctrlpriv->ctl, &perfmon->req_dequeued);
  285. ctrlpriv->ctl_ob_enc_req =
  286. debugfs_create_u64("ob_rq_encrypted",
  287. S_IRUSR | S_IRGRP | S_IROTH,
  288. ctrlpriv->ctl, &perfmon->ob_enc_req);
  289. ctrlpriv->ctl_ib_dec_req =
  290. debugfs_create_u64("ib_rq_decrypted",
  291. S_IRUSR | S_IRGRP | S_IROTH,
  292. ctrlpriv->ctl, &perfmon->ib_dec_req);
  293. ctrlpriv->ctl_ob_enc_bytes =
  294. debugfs_create_u64("ob_bytes_encrypted",
  295. S_IRUSR | S_IRGRP | S_IROTH,
  296. ctrlpriv->ctl, &perfmon->ob_enc_bytes);
  297. ctrlpriv->ctl_ob_prot_bytes =
  298. debugfs_create_u64("ob_bytes_protected",
  299. S_IRUSR | S_IRGRP | S_IROTH,
  300. ctrlpriv->ctl, &perfmon->ob_prot_bytes);
  301. ctrlpriv->ctl_ib_dec_bytes =
  302. debugfs_create_u64("ib_bytes_decrypted",
  303. S_IRUSR | S_IRGRP | S_IROTH,
  304. ctrlpriv->ctl, &perfmon->ib_dec_bytes);
  305. ctrlpriv->ctl_ib_valid_bytes =
  306. debugfs_create_u64("ib_bytes_validated",
  307. S_IRUSR | S_IRGRP | S_IROTH,
  308. ctrlpriv->ctl, &perfmon->ib_valid_bytes);
  309. /* Controller level - global status values */
  310. ctrlpriv->ctl_faultaddr =
  311. debugfs_create_u64("fault_addr",
  312. S_IRUSR | S_IRGRP | S_IROTH,
  313. ctrlpriv->ctl, &perfmon->faultaddr);
  314. ctrlpriv->ctl_faultdetail =
  315. debugfs_create_u32("fault_detail",
  316. S_IRUSR | S_IRGRP | S_IROTH,
  317. ctrlpriv->ctl, &perfmon->faultdetail);
  318. ctrlpriv->ctl_faultstatus =
  319. debugfs_create_u32("fault_status",
  320. S_IRUSR | S_IRGRP | S_IROTH,
  321. ctrlpriv->ctl, &perfmon->status);
  322. /* Internal covering keys (useful in non-secure mode only) */
  323. ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
  324. ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  325. ctrlpriv->ctl_kek = debugfs_create_blob("kek",
  326. S_IRUSR |
  327. S_IRGRP | S_IROTH,
  328. ctrlpriv->ctl,
  329. &ctrlpriv->ctl_kek_wrap);
  330. ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
  331. ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  332. ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
  333. S_IRUSR |
  334. S_IRGRP | S_IROTH,
  335. ctrlpriv->ctl,
  336. &ctrlpriv->ctl_tkek_wrap);
  337. ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
  338. ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  339. ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
  340. S_IRUSR |
  341. S_IRGRP | S_IROTH,
  342. ctrlpriv->ctl,
  343. &ctrlpriv->ctl_tdsk_wrap);
  344. #endif
  345. return 0;
  346. }
  347. static struct of_device_id caam_match[] = {
  348. {
  349. .compatible = "fsl,sec-v4.0",
  350. },
  351. {
  352. .compatible = "fsl,sec4.0",
  353. },
  354. {},
  355. };
  356. MODULE_DEVICE_TABLE(of, caam_match);
  357. static struct platform_driver caam_driver = {
  358. .driver = {
  359. .name = "caam",
  360. .owner = THIS_MODULE,
  361. .of_match_table = caam_match,
  362. },
  363. .probe = caam_probe,
  364. .remove = caam_remove,
  365. };
  366. module_platform_driver(caam_driver);
  367. MODULE_LICENSE("GPL");
  368. MODULE_DESCRIPTION("FSL CAAM request backend");
  369. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");