ctrl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /* * CAAM control-plane driver backend
  2. * Controller-level driver, kernel property detection, initialization
  3. *
  4. * Copyright 2008-2012 Freescale Semiconductor, Inc.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/of_address.h>
  8. #include <linux/of_irq.h>
  9. #include "compat.h"
  10. #include "regs.h"
  11. #include "intern.h"
  12. #include "jr.h"
  13. #include "desc_constr.h"
  14. #include "error.h"
  15. /*
  16. * Descriptor to instantiate RNG State Handle 0 in normal mode and
  17. * load the JDKEK, TDKEK and TDSK registers
  18. */
  19. static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
  20. {
  21. u32 *jump_cmd, op_flags;
  22. init_job_desc(desc, 0);
  23. op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  24. (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT;
  25. /* INIT RNG in non-test mode */
  26. append_operation(desc, op_flags);
  27. if (!handle && do_sk) {
  28. /*
  29. * For SH0, Secure Keys must be generated as well
  30. */
  31. /* wait for done */
  32. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
  33. set_jump_tgt_here(desc, jump_cmd);
  34. /*
  35. * load 1 to clear written reg:
  36. * resets the done interrrupt and returns the RNG to idle.
  37. */
  38. append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
  39. /* Initialize State Handle */
  40. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  41. OP_ALG_AAI_RNG4_SK);
  42. }
  43. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  44. }
  45. /* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
  46. static void build_deinstantiation_desc(u32 *desc, int handle)
  47. {
  48. init_job_desc(desc, 0);
  49. /* Uninstantiate State Handle 0 */
  50. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  51. (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
  52. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  53. }
  54. /*
  55. * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
  56. * the software (no JR/QI used).
  57. * @ctrldev - pointer to device
  58. * @status - descriptor status, after being run
  59. *
  60. * Return: - 0 if no error occurred
  61. * - -ENODEV if the DECO couldn't be acquired
  62. * - -EAGAIN if an error occurred while executing the descriptor
  63. */
  64. static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
  65. u32 *status)
  66. {
  67. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  68. struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
  69. struct caam_deco __iomem *deco = ctrlpriv->deco;
  70. unsigned int timeout = 100000;
  71. u32 deco_dbg_reg, flags;
  72. int i;
  73. if (ctrlpriv->virt_en == 1) {
  74. setbits32(&ctrl->deco_rsr, DECORSR_JR0);
  75. while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) &&
  76. --timeout)
  77. cpu_relax();
  78. timeout = 100000;
  79. }
  80. setbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
  81. while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) &&
  82. --timeout)
  83. cpu_relax();
  84. if (!timeout) {
  85. dev_err(ctrldev, "failed to acquire DECO 0\n");
  86. clrbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
  87. return -ENODEV;
  88. }
  89. for (i = 0; i < desc_len(desc); i++)
  90. wr_reg32(&deco->descbuf[i], *(desc + i));
  91. flags = DECO_JQCR_WHL;
  92. /*
  93. * If the descriptor length is longer than 4 words, then the
  94. * FOUR bit in JRCTRL register must be set.
  95. */
  96. if (desc_len(desc) >= 4)
  97. flags |= DECO_JQCR_FOUR;
  98. /* Instruct the DECO to execute it */
  99. wr_reg32(&deco->jr_ctl_hi, flags);
  100. timeout = 10000000;
  101. do {
  102. deco_dbg_reg = rd_reg32(&deco->desc_dbg);
  103. /*
  104. * If an error occured in the descriptor, then
  105. * the DECO status field will be set to 0x0D
  106. */
  107. if ((deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) ==
  108. DESC_DBG_DECO_STAT_HOST_ERR)
  109. break;
  110. cpu_relax();
  111. } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
  112. *status = rd_reg32(&deco->op_status_hi) &
  113. DECO_OP_STATUS_HI_ERR_MASK;
  114. if (ctrlpriv->virt_en == 1)
  115. clrbits32(&ctrl->deco_rsr, DECORSR_JR0);
  116. /* Mark the DECO as free */
  117. clrbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
  118. if (!timeout)
  119. return -EAGAIN;
  120. return 0;
  121. }
  122. /*
  123. * instantiate_rng - builds and executes a descriptor on DECO0,
  124. * which initializes the RNG block.
  125. * @ctrldev - pointer to device
  126. * @state_handle_mask - bitmask containing the instantiation status
  127. * for the RNG4 state handles which exist in
  128. * the RNG4 block: 1 if it's been instantiated
  129. * by an external entry, 0 otherwise.
  130. * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
  131. * Caution: this can be done only once; if the keys need to be
  132. * regenerated, a POR is required
  133. *
  134. * Return: - 0 if no error occurred
  135. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  136. * - -ENODEV if DECO0 couldn't be acquired
  137. * - -EAGAIN if an error occurred when executing the descriptor
  138. * f.i. there was a RNG hardware error due to not "good enough"
  139. * entropy being aquired.
  140. */
  141. static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
  142. int gen_sk)
  143. {
  144. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  145. struct caam_ctrl __iomem *ctrl;
  146. u32 *desc, status, rdsta_val;
  147. int ret = 0, sh_idx;
  148. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  149. desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
  150. if (!desc)
  151. return -ENOMEM;
  152. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  153. /*
  154. * If the corresponding bit is set, this state handle
  155. * was initialized by somebody else, so it's left alone.
  156. */
  157. if ((1 << sh_idx) & state_handle_mask)
  158. continue;
  159. /* Create the descriptor for instantiating RNG State Handle */
  160. build_instantiation_desc(desc, sh_idx, gen_sk);
  161. /* Try to run it through DECO0 */
  162. ret = run_descriptor_deco0(ctrldev, desc, &status);
  163. /*
  164. * If ret is not 0, or descriptor status is not 0, then
  165. * something went wrong. No need to try the next state
  166. * handle (if available), bail out here.
  167. * Also, if for some reason, the State Handle didn't get
  168. * instantiated although the descriptor has finished
  169. * without any error (HW optimizations for later
  170. * CAAM eras), then try again.
  171. */
  172. rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK;
  173. if (status || !(rdsta_val & (1 << sh_idx)))
  174. ret = -EAGAIN;
  175. if (ret)
  176. break;
  177. dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
  178. /* Clear the contents before recreating the descriptor */
  179. memset(desc, 0x00, CAAM_CMD_SZ * 7);
  180. }
  181. kfree(desc);
  182. return ret;
  183. }
  184. /*
  185. * deinstantiate_rng - builds and executes a descriptor on DECO0,
  186. * which deinitializes the RNG block.
  187. * @ctrldev - pointer to device
  188. * @state_handle_mask - bitmask containing the instantiation status
  189. * for the RNG4 state handles which exist in
  190. * the RNG4 block: 1 if it's been instantiated
  191. *
  192. * Return: - 0 if no error occurred
  193. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  194. * - -ENODEV if DECO0 couldn't be acquired
  195. * - -EAGAIN if an error occurred when executing the descriptor
  196. */
  197. static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
  198. {
  199. u32 *desc, status;
  200. int sh_idx, ret = 0;
  201. desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
  202. if (!desc)
  203. return -ENOMEM;
  204. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  205. /*
  206. * If the corresponding bit is set, then it means the state
  207. * handle was initialized by us, and thus it needs to be
  208. * deintialized as well
  209. */
  210. if ((1 << sh_idx) & state_handle_mask) {
  211. /*
  212. * Create the descriptor for deinstantating this state
  213. * handle
  214. */
  215. build_deinstantiation_desc(desc, sh_idx);
  216. /* Try to run it through DECO0 */
  217. ret = run_descriptor_deco0(ctrldev, desc, &status);
  218. if (ret || status) {
  219. dev_err(ctrldev,
  220. "Failed to deinstantiate RNG4 SH%d\n",
  221. sh_idx);
  222. break;
  223. }
  224. dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
  225. }
  226. }
  227. kfree(desc);
  228. return ret;
  229. }
  230. static int caam_remove(struct platform_device *pdev)
  231. {
  232. struct device *ctrldev;
  233. struct caam_drv_private *ctrlpriv;
  234. struct caam_ctrl __iomem *ctrl;
  235. int ring, ret = 0;
  236. ctrldev = &pdev->dev;
  237. ctrlpriv = dev_get_drvdata(ctrldev);
  238. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  239. /* Remove platform devices for JobRs */
  240. for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
  241. if (ctrlpriv->jrpdev[ring])
  242. of_device_unregister(ctrlpriv->jrpdev[ring]);
  243. }
  244. /* De-initialize RNG state handles initialized by this driver. */
  245. if (ctrlpriv->rng4_sh_init)
  246. deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
  247. /* Shut down debug views */
  248. #ifdef CONFIG_DEBUG_FS
  249. debugfs_remove_recursive(ctrlpriv->dfs_root);
  250. #endif
  251. /* Unmap controller region */
  252. iounmap(ctrl);
  253. return ret;
  254. }
  255. /*
  256. * kick_trng - sets the various parameters for enabling the initialization
  257. * of the RNG4 block in CAAM
  258. * @pdev - pointer to the platform device
  259. * @ent_delay - Defines the length (in system clocks) of each entropy sample.
  260. */
  261. static void kick_trng(struct platform_device *pdev, int ent_delay)
  262. {
  263. struct device *ctrldev = &pdev->dev;
  264. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  265. struct caam_ctrl __iomem *ctrl;
  266. struct rng4tst __iomem *r4tst;
  267. u32 val;
  268. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  269. r4tst = &ctrl->r4tst[0];
  270. /* put RNG4 into program mode */
  271. setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  272. /*
  273. * Performance-wise, it does not make sense to
  274. * set the delay to a value that is lower
  275. * than the last one that worked (i.e. the state handles
  276. * were instantiated properly. Thus, instead of wasting
  277. * time trying to set the values controlling the sample
  278. * frequency, the function simply returns.
  279. */
  280. val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
  281. >> RTSDCTL_ENT_DLY_SHIFT;
  282. if (ent_delay <= val) {
  283. /* put RNG4 into run mode */
  284. clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  285. return;
  286. }
  287. val = rd_reg32(&r4tst->rtsdctl);
  288. val = (val & ~RTSDCTL_ENT_DLY_MASK) |
  289. (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
  290. wr_reg32(&r4tst->rtsdctl, val);
  291. /* min. freq. count, equal to 1/4 of the entropy sample length */
  292. wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
  293. /* disable maximum frequency count */
  294. wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE);
  295. /* read the control register */
  296. val = rd_reg32(&r4tst->rtmctl);
  297. /*
  298. * select raw sampling in both entropy shifter
  299. * and statistical checker
  300. */
  301. setbits32(&val, RTMCTL_SAMP_MODE_RAW_ES_SC);
  302. /* put RNG4 into run mode */
  303. clrbits32(&val, RTMCTL_PRGM);
  304. /* write back the control register */
  305. wr_reg32(&r4tst->rtmctl, val);
  306. }
  307. /**
  308. * caam_get_era() - Return the ERA of the SEC on SoC, based
  309. * on "sec-era" propery in the DTS. This property is updated by u-boot.
  310. **/
  311. int caam_get_era(void)
  312. {
  313. struct device_node *caam_node;
  314. for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
  315. const uint32_t *prop = (uint32_t *)of_get_property(caam_node,
  316. "fsl,sec-era",
  317. NULL);
  318. return prop ? *prop : -ENOTSUPP;
  319. }
  320. return -ENOTSUPP;
  321. }
  322. EXPORT_SYMBOL(caam_get_era);
  323. /* Probe routine for CAAM top (controller) level */
  324. static int caam_probe(struct platform_device *pdev)
  325. {
  326. int ret, ring, rspec, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
  327. u64 caam_id;
  328. struct device *dev;
  329. struct device_node *nprop, *np;
  330. struct caam_ctrl __iomem *ctrl;
  331. struct caam_drv_private *ctrlpriv;
  332. #ifdef CONFIG_DEBUG_FS
  333. struct caam_perfmon *perfmon;
  334. #endif
  335. u32 scfgr, comp_params;
  336. u32 cha_vid_ls;
  337. int pg_size;
  338. int BLOCK_OFFSET = 0;
  339. ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(struct caam_drv_private),
  340. GFP_KERNEL);
  341. if (!ctrlpriv)
  342. return -ENOMEM;
  343. dev = &pdev->dev;
  344. dev_set_drvdata(dev, ctrlpriv);
  345. ctrlpriv->pdev = pdev;
  346. nprop = pdev->dev.of_node;
  347. /* Get configuration properties from device tree */
  348. /* First, get register page */
  349. ctrl = of_iomap(nprop, 0);
  350. if (ctrl == NULL) {
  351. dev_err(dev, "caam: of_iomap() failed\n");
  352. return -ENOMEM;
  353. }
  354. /* Finding the page size for using the CTPR_MS register */
  355. comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
  356. pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
  357. /* Allocating the BLOCK_OFFSET based on the supported page size on
  358. * the platform
  359. */
  360. if (pg_size == 0)
  361. BLOCK_OFFSET = PG_SIZE_4K;
  362. else
  363. BLOCK_OFFSET = PG_SIZE_64K;
  364. ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
  365. ctrlpriv->assure = (struct caam_assurance __force *)
  366. ((uint8_t *)ctrl +
  367. BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
  368. );
  369. ctrlpriv->deco = (struct caam_deco __force *)
  370. ((uint8_t *)ctrl +
  371. BLOCK_OFFSET * DECO_BLOCK_NUMBER
  372. );
  373. /* Get the IRQ of the controller (for security violations only) */
  374. ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
  375. /*
  376. * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
  377. * long pointers in master configuration register
  378. */
  379. setbits32(&ctrl->mcr, MCFGR_WDENABLE |
  380. (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
  381. /*
  382. * Read the Compile Time paramters and SCFGR to determine
  383. * if Virtualization is enabled for this platform
  384. */
  385. scfgr = rd_reg32(&ctrl->scfgr);
  386. ctrlpriv->virt_en = 0;
  387. if (comp_params & CTPR_MS_VIRT_EN_INCL) {
  388. /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
  389. * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
  390. */
  391. if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
  392. (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
  393. (scfgr & SCFGR_VIRT_EN)))
  394. ctrlpriv->virt_en = 1;
  395. } else {
  396. /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
  397. if (comp_params & CTPR_MS_VIRT_EN_POR)
  398. ctrlpriv->virt_en = 1;
  399. }
  400. if (ctrlpriv->virt_en == 1)
  401. setbits32(&ctrl->jrstart, JRSTART_JR0_START |
  402. JRSTART_JR1_START | JRSTART_JR2_START |
  403. JRSTART_JR3_START);
  404. if (sizeof(dma_addr_t) == sizeof(u64))
  405. if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
  406. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
  407. else
  408. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(36));
  409. else
  410. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  411. /*
  412. * Detect and enable JobRs
  413. * First, find out how many ring spec'ed, allocate references
  414. * for all, then go probe each one.
  415. */
  416. rspec = 0;
  417. for_each_available_child_of_node(nprop, np)
  418. if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
  419. of_device_is_compatible(np, "fsl,sec4.0-job-ring"))
  420. rspec++;
  421. ctrlpriv->jrpdev = devm_kzalloc(&pdev->dev,
  422. sizeof(struct platform_device *) * rspec,
  423. GFP_KERNEL);
  424. if (ctrlpriv->jrpdev == NULL) {
  425. iounmap(ctrl);
  426. return -ENOMEM;
  427. }
  428. ring = 0;
  429. ctrlpriv->total_jobrs = 0;
  430. for_each_available_child_of_node(nprop, np)
  431. if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
  432. of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
  433. ctrlpriv->jrpdev[ring] =
  434. of_platform_device_create(np, NULL, dev);
  435. if (!ctrlpriv->jrpdev[ring]) {
  436. pr_warn("JR%d Platform device creation error\n",
  437. ring);
  438. continue;
  439. }
  440. ctrlpriv->jr[ring] = (struct caam_job_ring __force *)
  441. ((uint8_t *)ctrl +
  442. (ring + JR_BLOCK_NUMBER) *
  443. BLOCK_OFFSET
  444. );
  445. ctrlpriv->total_jobrs++;
  446. ring++;
  447. }
  448. /* Check to see if QI present. If so, enable */
  449. ctrlpriv->qi_present =
  450. !!(rd_reg32(&ctrl->perfmon.comp_parms_ms) &
  451. CTPR_MS_QI_MASK);
  452. if (ctrlpriv->qi_present) {
  453. ctrlpriv->qi = (struct caam_queue_if __force *)
  454. ((uint8_t *)ctrl +
  455. BLOCK_OFFSET * QI_BLOCK_NUMBER
  456. );
  457. /* This is all that's required to physically enable QI */
  458. wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
  459. }
  460. /* If no QI and no rings specified, quit and go home */
  461. if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
  462. dev_err(dev, "no queues configured, terminating\n");
  463. caam_remove(pdev);
  464. return -ENOMEM;
  465. }
  466. cha_vid_ls = rd_reg32(&ctrl->perfmon.cha_id_ls);
  467. /*
  468. * If SEC has RNG version >= 4 and RNG state handle has not been
  469. * already instantiated, do RNG instantiation
  470. */
  471. if ((cha_vid_ls & CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT >= 4) {
  472. ctrlpriv->rng4_sh_init =
  473. rd_reg32(&ctrl->r4tst[0].rdsta);
  474. /*
  475. * If the secure keys (TDKEK, JDKEK, TDSK), were already
  476. * generated, signal this to the function that is instantiating
  477. * the state handles. An error would occur if RNG4 attempts
  478. * to regenerate these keys before the next POR.
  479. */
  480. gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
  481. ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
  482. do {
  483. int inst_handles =
  484. rd_reg32(&ctrl->r4tst[0].rdsta) &
  485. RDSTA_IFMASK;
  486. /*
  487. * If either SH were instantiated by somebody else
  488. * (e.g. u-boot) then it is assumed that the entropy
  489. * parameters are properly set and thus the function
  490. * setting these (kick_trng(...)) is skipped.
  491. * Also, if a handle was instantiated, do not change
  492. * the TRNG parameters.
  493. */
  494. if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
  495. dev_info(dev,
  496. "Entropy delay = %u\n",
  497. ent_delay);
  498. kick_trng(pdev, ent_delay);
  499. ent_delay += 400;
  500. }
  501. /*
  502. * if instantiate_rng(...) fails, the loop will rerun
  503. * and the kick_trng(...) function will modfiy the
  504. * upper and lower limits of the entropy sampling
  505. * interval, leading to a sucessful initialization of
  506. * the RNG.
  507. */
  508. ret = instantiate_rng(dev, inst_handles,
  509. gen_sk);
  510. if (ret == -EAGAIN)
  511. /*
  512. * if here, the loop will rerun,
  513. * so don't hog the CPU
  514. */
  515. cpu_relax();
  516. } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  517. if (ret) {
  518. dev_err(dev, "failed to instantiate RNG");
  519. caam_remove(pdev);
  520. return ret;
  521. }
  522. /*
  523. * Set handles init'ed by this module as the complement of the
  524. * already initialized ones
  525. */
  526. ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
  527. /* Enable RDB bit so that RNG works faster */
  528. setbits32(&ctrl->scfgr, SCFGR_RDBENABLE);
  529. }
  530. /* NOTE: RTIC detection ought to go here, around Si time */
  531. caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 |
  532. (u64)rd_reg32(&ctrl->perfmon.caam_id_ls);
  533. /* Report "alive" for developer to see */
  534. dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
  535. caam_get_era());
  536. dev_info(dev, "job rings = %d, qi = %d\n",
  537. ctrlpriv->total_jobrs, ctrlpriv->qi_present);
  538. #ifdef CONFIG_DEBUG_FS
  539. /*
  540. * FIXME: needs better naming distinction, as some amalgamation of
  541. * "caam" and nprop->full_name. The OF name isn't distinctive,
  542. * but does separate instances
  543. */
  544. perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
  545. ctrlpriv->dfs_root = debugfs_create_dir(dev_name(dev), NULL);
  546. ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
  547. /* Controller-level - performance monitor counters */
  548. ctrlpriv->ctl_rq_dequeued =
  549. debugfs_create_u64("rq_dequeued",
  550. S_IRUSR | S_IRGRP | S_IROTH,
  551. ctrlpriv->ctl, &perfmon->req_dequeued);
  552. ctrlpriv->ctl_ob_enc_req =
  553. debugfs_create_u64("ob_rq_encrypted",
  554. S_IRUSR | S_IRGRP | S_IROTH,
  555. ctrlpriv->ctl, &perfmon->ob_enc_req);
  556. ctrlpriv->ctl_ib_dec_req =
  557. debugfs_create_u64("ib_rq_decrypted",
  558. S_IRUSR | S_IRGRP | S_IROTH,
  559. ctrlpriv->ctl, &perfmon->ib_dec_req);
  560. ctrlpriv->ctl_ob_enc_bytes =
  561. debugfs_create_u64("ob_bytes_encrypted",
  562. S_IRUSR | S_IRGRP | S_IROTH,
  563. ctrlpriv->ctl, &perfmon->ob_enc_bytes);
  564. ctrlpriv->ctl_ob_prot_bytes =
  565. debugfs_create_u64("ob_bytes_protected",
  566. S_IRUSR | S_IRGRP | S_IROTH,
  567. ctrlpriv->ctl, &perfmon->ob_prot_bytes);
  568. ctrlpriv->ctl_ib_dec_bytes =
  569. debugfs_create_u64("ib_bytes_decrypted",
  570. S_IRUSR | S_IRGRP | S_IROTH,
  571. ctrlpriv->ctl, &perfmon->ib_dec_bytes);
  572. ctrlpriv->ctl_ib_valid_bytes =
  573. debugfs_create_u64("ib_bytes_validated",
  574. S_IRUSR | S_IRGRP | S_IROTH,
  575. ctrlpriv->ctl, &perfmon->ib_valid_bytes);
  576. /* Controller level - global status values */
  577. ctrlpriv->ctl_faultaddr =
  578. debugfs_create_u64("fault_addr",
  579. S_IRUSR | S_IRGRP | S_IROTH,
  580. ctrlpriv->ctl, &perfmon->faultaddr);
  581. ctrlpriv->ctl_faultdetail =
  582. debugfs_create_u32("fault_detail",
  583. S_IRUSR | S_IRGRP | S_IROTH,
  584. ctrlpriv->ctl, &perfmon->faultdetail);
  585. ctrlpriv->ctl_faultstatus =
  586. debugfs_create_u32("fault_status",
  587. S_IRUSR | S_IRGRP | S_IROTH,
  588. ctrlpriv->ctl, &perfmon->status);
  589. /* Internal covering keys (useful in non-secure mode only) */
  590. ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
  591. ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  592. ctrlpriv->ctl_kek = debugfs_create_blob("kek",
  593. S_IRUSR |
  594. S_IRGRP | S_IROTH,
  595. ctrlpriv->ctl,
  596. &ctrlpriv->ctl_kek_wrap);
  597. ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
  598. ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  599. ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
  600. S_IRUSR |
  601. S_IRGRP | S_IROTH,
  602. ctrlpriv->ctl,
  603. &ctrlpriv->ctl_tkek_wrap);
  604. ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
  605. ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  606. ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
  607. S_IRUSR |
  608. S_IRGRP | S_IROTH,
  609. ctrlpriv->ctl,
  610. &ctrlpriv->ctl_tdsk_wrap);
  611. #endif
  612. return 0;
  613. }
  614. static struct of_device_id caam_match[] = {
  615. {
  616. .compatible = "fsl,sec-v4.0",
  617. },
  618. {
  619. .compatible = "fsl,sec4.0",
  620. },
  621. {},
  622. };
  623. MODULE_DEVICE_TABLE(of, caam_match);
  624. static struct platform_driver caam_driver = {
  625. .driver = {
  626. .name = "caam",
  627. .of_match_table = caam_match,
  628. },
  629. .probe = caam_probe,
  630. .remove = caam_remove,
  631. };
  632. module_platform_driver(caam_driver);
  633. MODULE_LICENSE("GPL");
  634. MODULE_DESCRIPTION("FSL CAAM request backend");
  635. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");