ctrl.c 23 KB

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