ctrl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. /*
  15. * Descriptor to instantiate RNG State Handle 0 in normal mode and
  16. * load the JDKEK, TDKEK and TDSK registers
  17. */
  18. static void build_instantiation_desc(u32 *desc)
  19. {
  20. u32 *jump_cmd;
  21. init_job_desc(desc, 0);
  22. /* INIT RNG in non-test mode */
  23. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  24. OP_ALG_AS_INIT);
  25. /* wait for done */
  26. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
  27. set_jump_tgt_here(desc, jump_cmd);
  28. /*
  29. * load 1 to clear written reg:
  30. * resets the done interrupt and returns the RNG to idle.
  31. */
  32. append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
  33. /* generate secure keys (non-test) */
  34. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  35. OP_ALG_RNG4_SK);
  36. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  37. }
  38. /* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
  39. static void build_deinstantiation_desc(u32 *desc)
  40. {
  41. init_job_desc(desc, 0);
  42. /* Uninstantiate State Handle 0 */
  43. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  44. OP_ALG_AS_INITFINAL);
  45. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  46. }
  47. /*
  48. * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
  49. * the software (no JR/QI used).
  50. * @ctrldev - pointer to device
  51. * Return: - 0 if no error occurred
  52. * - -ENODEV if the DECO couldn't be acquired
  53. * - -EAGAIN if an error occurred while executing the descriptor
  54. */
  55. static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc)
  56. {
  57. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  58. struct caam_full __iomem *topregs;
  59. unsigned int timeout = 100000;
  60. u32 deco_dbg_reg, flags;
  61. int i;
  62. /* Set the bit to request direct access to DECO0 */
  63. topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
  64. setbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
  65. while (!(rd_reg32(&topregs->ctrl.deco_rq) & DECORR_DEN0) &&
  66. --timeout)
  67. cpu_relax();
  68. if (!timeout) {
  69. dev_err(ctrldev, "failed to acquire DECO 0\n");
  70. clrbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
  71. return -ENODEV;
  72. }
  73. for (i = 0; i < desc_len(desc); i++)
  74. wr_reg32(&topregs->deco.descbuf[i], *(desc + i));
  75. flags = DECO_JQCR_WHL;
  76. /*
  77. * If the descriptor length is longer than 4 words, then the
  78. * FOUR bit in JRCTRL register must be set.
  79. */
  80. if (desc_len(desc) >= 4)
  81. flags |= DECO_JQCR_FOUR;
  82. /* Instruct the DECO to execute it */
  83. wr_reg32(&topregs->deco.jr_ctl_hi, flags);
  84. timeout = 10000000;
  85. do {
  86. deco_dbg_reg = rd_reg32(&topregs->deco.desc_dbg);
  87. /*
  88. * If an error occured in the descriptor, then
  89. * the DECO status field will be set to 0x0D
  90. */
  91. if ((deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) ==
  92. DESC_DBG_DECO_STAT_HOST_ERR)
  93. break;
  94. cpu_relax();
  95. } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
  96. /* Mark the DECO as free */
  97. clrbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
  98. if (!timeout)
  99. return -EAGAIN;
  100. return 0;
  101. }
  102. /*
  103. * instantiate_rng - builds and executes a descriptor on DECO0,
  104. * which initializes the RNG block.
  105. * @ctrldev - pointer to device
  106. * Return: - 0 if no error occurred
  107. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  108. * - -ENODEV if DECO0 couldn't be acquired
  109. * - -EAGAIN if an error occurred when executing the descriptor
  110. * f.i. there was a RNG hardware error due to not "good enough"
  111. * entropy being aquired.
  112. */
  113. static int instantiate_rng(struct device *ctrldev)
  114. {
  115. u32 *desc;
  116. int ret = 0;
  117. desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
  118. if (!desc)
  119. return -ENOMEM;
  120. /* Create the descriptor for instantiating RNG State Handle 0 */
  121. build_instantiation_desc(desc);
  122. /* Try to run it through DECO0 */
  123. ret = run_descriptor_deco0(ctrldev, desc);
  124. kfree(desc);
  125. return ret;
  126. }
  127. /*
  128. * deinstantiate_rng - builds and executes a descriptor on DECO0,
  129. * which deinitializes the RNG block.
  130. * @ctrldev - pointer to device
  131. *
  132. * Return: - 0 if no error occurred
  133. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  134. * - -ENODEV if DECO0 couldn't be acquired
  135. * - -EAGAIN if an error occurred when executing the descriptor
  136. */
  137. static int deinstantiate_rng(struct device *ctrldev)
  138. {
  139. u32 *desc;
  140. int i, ret = 0;
  141. desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
  142. if (!desc)
  143. return -ENOMEM;
  144. /* Create the descriptor for deinstantating RNG State Handle 0 */
  145. build_deinstantiation_desc(desc);
  146. /* Try to run it through DECO0 */
  147. ret = run_descriptor_deco0(ctrldev, desc);
  148. if (ret)
  149. dev_err(ctrldev, "failed to deinstantiate RNG\n");
  150. kfree(desc);
  151. return ret;
  152. }
  153. static int caam_remove(struct platform_device *pdev)
  154. {
  155. struct device *ctrldev;
  156. struct caam_drv_private *ctrlpriv;
  157. struct caam_drv_private_jr *jrpriv;
  158. struct caam_full __iomem *topregs;
  159. int ring, ret = 0;
  160. ctrldev = &pdev->dev;
  161. ctrlpriv = dev_get_drvdata(ctrldev);
  162. topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
  163. /* shut down JobRs */
  164. for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
  165. ret |= caam_jr_shutdown(ctrlpriv->jrdev[ring]);
  166. jrpriv = dev_get_drvdata(ctrlpriv->jrdev[ring]);
  167. irq_dispose_mapping(jrpriv->irq);
  168. }
  169. /* De-initialize RNG if it was initialized by this driver. */
  170. if (ctrlpriv->rng4_init)
  171. deinstantiate_rng(ctrldev);
  172. /* Shut down debug views */
  173. #ifdef CONFIG_DEBUG_FS
  174. debugfs_remove_recursive(ctrlpriv->dfs_root);
  175. #endif
  176. /* Unmap controller region */
  177. iounmap(&topregs->ctrl);
  178. kfree(ctrlpriv->jrdev);
  179. kfree(ctrlpriv);
  180. return ret;
  181. }
  182. /*
  183. * kick_trng - sets the various parameters for enabling the initialization
  184. * of the RNG4 block in CAAM
  185. * @pdev - pointer to the platform device
  186. * @ent_delay - Defines the length (in system clocks) of each entropy sample.
  187. */
  188. static void kick_trng(struct platform_device *pdev, int ent_delay)
  189. {
  190. struct device *ctrldev = &pdev->dev;
  191. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  192. struct caam_full __iomem *topregs;
  193. struct rng4tst __iomem *r4tst;
  194. u32 val;
  195. topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
  196. r4tst = &topregs->ctrl.r4tst[0];
  197. /* put RNG4 into program mode */
  198. setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  199. /*
  200. * Performance-wise, it does not make sense to
  201. * set the delay to a value that is lower
  202. * than the last one that worked (i.e. the state handles
  203. * were instantiated properly. Thus, instead of wasting
  204. * time trying to set the values controlling the sample
  205. * frequency, the function simply returns.
  206. */
  207. val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
  208. >> RTSDCTL_ENT_DLY_SHIFT;
  209. if (ent_delay <= val) {
  210. /* put RNG4 into run mode */
  211. clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  212. return;
  213. }
  214. val = rd_reg32(&r4tst->rtsdctl);
  215. val = (val & ~RTSDCTL_ENT_DLY_MASK) |
  216. (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
  217. wr_reg32(&r4tst->rtsdctl, val);
  218. /* min. freq. count, equal to 1/4 of the entropy sample length */
  219. wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
  220. /* max. freq. count, equal to 8 times the entropy sample length */
  221. wr_reg32(&r4tst->rtfrqmax, ent_delay << 3);
  222. /* put RNG4 into run mode */
  223. clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  224. }
  225. /**
  226. * caam_get_era() - Return the ERA of the SEC on SoC, based
  227. * on the SEC_VID register.
  228. * Returns the ERA number (1..4) or -ENOTSUPP if the ERA is unknown.
  229. * @caam_id - the value of the SEC_VID register
  230. **/
  231. int caam_get_era(u64 caam_id)
  232. {
  233. struct sec_vid *sec_vid = (struct sec_vid *)&caam_id;
  234. static const struct {
  235. u16 ip_id;
  236. u8 maj_rev;
  237. u8 era;
  238. } caam_eras[] = {
  239. {0x0A10, 1, 1},
  240. {0x0A10, 2, 2},
  241. {0x0A12, 1, 3},
  242. {0x0A14, 1, 3},
  243. {0x0A14, 2, 4},
  244. {0x0A16, 1, 4},
  245. {0x0A11, 1, 4}
  246. };
  247. int i;
  248. for (i = 0; i < ARRAY_SIZE(caam_eras); i++)
  249. if (caam_eras[i].ip_id == sec_vid->ip_id &&
  250. caam_eras[i].maj_rev == sec_vid->maj_rev)
  251. return caam_eras[i].era;
  252. return -ENOTSUPP;
  253. }
  254. EXPORT_SYMBOL(caam_get_era);
  255. /* Probe routine for CAAM top (controller) level */
  256. static int caam_probe(struct platform_device *pdev)
  257. {
  258. int ret, ring, rspec, ent_delay = RTSDCTL_ENT_DLY_MIN;
  259. u64 caam_id;
  260. struct device *dev;
  261. struct device_node *nprop, *np;
  262. struct caam_ctrl __iomem *ctrl;
  263. struct caam_full __iomem *topregs;
  264. struct caam_drv_private *ctrlpriv;
  265. #ifdef CONFIG_DEBUG_FS
  266. struct caam_perfmon *perfmon;
  267. #endif
  268. u64 cha_vid;
  269. ctrlpriv = kzalloc(sizeof(struct caam_drv_private), GFP_KERNEL);
  270. if (!ctrlpriv)
  271. return -ENOMEM;
  272. dev = &pdev->dev;
  273. dev_set_drvdata(dev, ctrlpriv);
  274. ctrlpriv->pdev = pdev;
  275. nprop = pdev->dev.of_node;
  276. /* Get configuration properties from device tree */
  277. /* First, get register page */
  278. ctrl = of_iomap(nprop, 0);
  279. if (ctrl == NULL) {
  280. dev_err(dev, "caam: of_iomap() failed\n");
  281. return -ENOMEM;
  282. }
  283. ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
  284. /* topregs used to derive pointers to CAAM sub-blocks only */
  285. topregs = (struct caam_full __iomem *)ctrl;
  286. /* Get the IRQ of the controller (for security violations only) */
  287. ctrlpriv->secvio_irq = of_irq_to_resource(nprop, 0, NULL);
  288. /*
  289. * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
  290. * long pointers in master configuration register
  291. */
  292. setbits32(&topregs->ctrl.mcr, MCFGR_WDENABLE |
  293. (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
  294. if (sizeof(dma_addr_t) == sizeof(u64))
  295. if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
  296. dma_set_mask(dev, DMA_BIT_MASK(40));
  297. else
  298. dma_set_mask(dev, DMA_BIT_MASK(36));
  299. else
  300. dma_set_mask(dev, DMA_BIT_MASK(32));
  301. /*
  302. * Detect and enable JobRs
  303. * First, find out how many ring spec'ed, allocate references
  304. * for all, then go probe each one.
  305. */
  306. rspec = 0;
  307. for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring")
  308. rspec++;
  309. if (!rspec) {
  310. /* for backward compatible with device trees */
  311. for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring")
  312. rspec++;
  313. }
  314. ctrlpriv->jrdev = kzalloc(sizeof(struct device *) * rspec, GFP_KERNEL);
  315. if (ctrlpriv->jrdev == NULL) {
  316. iounmap(&topregs->ctrl);
  317. return -ENOMEM;
  318. }
  319. ring = 0;
  320. ctrlpriv->total_jobrs = 0;
  321. for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring") {
  322. caam_jr_probe(pdev, np, ring);
  323. ctrlpriv->total_jobrs++;
  324. ring++;
  325. }
  326. if (!ring) {
  327. for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring") {
  328. caam_jr_probe(pdev, np, ring);
  329. ctrlpriv->total_jobrs++;
  330. ring++;
  331. }
  332. }
  333. /* Check to see if QI present. If so, enable */
  334. ctrlpriv->qi_present = !!(rd_reg64(&topregs->ctrl.perfmon.comp_parms) &
  335. CTPR_QI_MASK);
  336. if (ctrlpriv->qi_present) {
  337. ctrlpriv->qi = (struct caam_queue_if __force *)&topregs->qi;
  338. /* This is all that's required to physically enable QI */
  339. wr_reg32(&topregs->qi.qi_control_lo, QICTL_DQEN);
  340. }
  341. /* If no QI and no rings specified, quit and go home */
  342. if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
  343. dev_err(dev, "no queues configured, terminating\n");
  344. caam_remove(pdev);
  345. return -ENOMEM;
  346. }
  347. cha_vid = rd_reg64(&topregs->ctrl.perfmon.cha_id);
  348. /*
  349. * If SEC has RNG version >= 4 and RNG state handle has not been
  350. * already instantiated, do RNG instantiation
  351. */
  352. if ((cha_vid & CHA_ID_RNG_MASK) >> CHA_ID_RNG_SHIFT >= 4 &&
  353. !(rd_reg32(&topregs->ctrl.r4tst[0].rdsta) & RDSTA_IF0)) {
  354. do {
  355. kick_trng(pdev, ent_delay);
  356. ret = instantiate_rng(dev);
  357. ent_delay += 400;
  358. } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  359. if (ret) {
  360. dev_err(dev, "failed to instantiate RNG");
  361. caam_remove(pdev);
  362. return ret;
  363. }
  364. ctrlpriv->rng4_init = 1;
  365. /* Enable RDB bit so that RNG works faster */
  366. setbits32(&topregs->ctrl.scfgr, SCFGR_RDBENABLE);
  367. }
  368. /* NOTE: RTIC detection ought to go here, around Si time */
  369. caam_id = rd_reg64(&topregs->ctrl.perfmon.caam_id);
  370. /* Report "alive" for developer to see */
  371. dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
  372. caam_get_era(caam_id));
  373. dev_info(dev, "job rings = %d, qi = %d\n",
  374. ctrlpriv->total_jobrs, ctrlpriv->qi_present);
  375. #ifdef CONFIG_DEBUG_FS
  376. /*
  377. * FIXME: needs better naming distinction, as some amalgamation of
  378. * "caam" and nprop->full_name. The OF name isn't distinctive,
  379. * but does separate instances
  380. */
  381. perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
  382. ctrlpriv->dfs_root = debugfs_create_dir("caam", NULL);
  383. ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
  384. /* Controller-level - performance monitor counters */
  385. ctrlpriv->ctl_rq_dequeued =
  386. debugfs_create_u64("rq_dequeued",
  387. S_IRUSR | S_IRGRP | S_IROTH,
  388. ctrlpriv->ctl, &perfmon->req_dequeued);
  389. ctrlpriv->ctl_ob_enc_req =
  390. debugfs_create_u64("ob_rq_encrypted",
  391. S_IRUSR | S_IRGRP | S_IROTH,
  392. ctrlpriv->ctl, &perfmon->ob_enc_req);
  393. ctrlpriv->ctl_ib_dec_req =
  394. debugfs_create_u64("ib_rq_decrypted",
  395. S_IRUSR | S_IRGRP | S_IROTH,
  396. ctrlpriv->ctl, &perfmon->ib_dec_req);
  397. ctrlpriv->ctl_ob_enc_bytes =
  398. debugfs_create_u64("ob_bytes_encrypted",
  399. S_IRUSR | S_IRGRP | S_IROTH,
  400. ctrlpriv->ctl, &perfmon->ob_enc_bytes);
  401. ctrlpriv->ctl_ob_prot_bytes =
  402. debugfs_create_u64("ob_bytes_protected",
  403. S_IRUSR | S_IRGRP | S_IROTH,
  404. ctrlpriv->ctl, &perfmon->ob_prot_bytes);
  405. ctrlpriv->ctl_ib_dec_bytes =
  406. debugfs_create_u64("ib_bytes_decrypted",
  407. S_IRUSR | S_IRGRP | S_IROTH,
  408. ctrlpriv->ctl, &perfmon->ib_dec_bytes);
  409. ctrlpriv->ctl_ib_valid_bytes =
  410. debugfs_create_u64("ib_bytes_validated",
  411. S_IRUSR | S_IRGRP | S_IROTH,
  412. ctrlpriv->ctl, &perfmon->ib_valid_bytes);
  413. /* Controller level - global status values */
  414. ctrlpriv->ctl_faultaddr =
  415. debugfs_create_u64("fault_addr",
  416. S_IRUSR | S_IRGRP | S_IROTH,
  417. ctrlpriv->ctl, &perfmon->faultaddr);
  418. ctrlpriv->ctl_faultdetail =
  419. debugfs_create_u32("fault_detail",
  420. S_IRUSR | S_IRGRP | S_IROTH,
  421. ctrlpriv->ctl, &perfmon->faultdetail);
  422. ctrlpriv->ctl_faultstatus =
  423. debugfs_create_u32("fault_status",
  424. S_IRUSR | S_IRGRP | S_IROTH,
  425. ctrlpriv->ctl, &perfmon->status);
  426. /* Internal covering keys (useful in non-secure mode only) */
  427. ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
  428. ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  429. ctrlpriv->ctl_kek = debugfs_create_blob("kek",
  430. S_IRUSR |
  431. S_IRGRP | S_IROTH,
  432. ctrlpriv->ctl,
  433. &ctrlpriv->ctl_kek_wrap);
  434. ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
  435. ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  436. ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
  437. S_IRUSR |
  438. S_IRGRP | S_IROTH,
  439. ctrlpriv->ctl,
  440. &ctrlpriv->ctl_tkek_wrap);
  441. ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
  442. ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  443. ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
  444. S_IRUSR |
  445. S_IRGRP | S_IROTH,
  446. ctrlpriv->ctl,
  447. &ctrlpriv->ctl_tdsk_wrap);
  448. #endif
  449. return 0;
  450. }
  451. static struct of_device_id caam_match[] = {
  452. {
  453. .compatible = "fsl,sec-v4.0",
  454. },
  455. {
  456. .compatible = "fsl,sec4.0",
  457. },
  458. {},
  459. };
  460. MODULE_DEVICE_TABLE(of, caam_match);
  461. static struct platform_driver caam_driver = {
  462. .driver = {
  463. .name = "caam",
  464. .owner = THIS_MODULE,
  465. .of_match_table = caam_match,
  466. },
  467. .probe = caam_probe,
  468. .remove = caam_remove,
  469. };
  470. module_platform_driver(caam_driver);
  471. MODULE_LICENSE("GPL");
  472. MODULE_DESCRIPTION("FSL CAAM request backend");
  473. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");