ctrl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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(*ctrlpriv), GFP_KERNEL);
  363. if (!ctrlpriv)
  364. return -ENOMEM;
  365. dev = &pdev->dev;
  366. dev_set_drvdata(dev, ctrlpriv);
  367. ctrlpriv->pdev = pdev;
  368. nprop = pdev->dev.of_node;
  369. /* Enable clocking */
  370. clk = caam_drv_identify_clk(&pdev->dev, "ipg");
  371. if (IS_ERR(clk)) {
  372. ret = PTR_ERR(clk);
  373. dev_err(&pdev->dev,
  374. "can't identify CAAM ipg clk: %d\n", ret);
  375. return ret;
  376. }
  377. ctrlpriv->caam_ipg = clk;
  378. clk = caam_drv_identify_clk(&pdev->dev, "mem");
  379. if (IS_ERR(clk)) {
  380. ret = PTR_ERR(clk);
  381. dev_err(&pdev->dev,
  382. "can't identify CAAM mem clk: %d\n", ret);
  383. return ret;
  384. }
  385. ctrlpriv->caam_mem = clk;
  386. clk = caam_drv_identify_clk(&pdev->dev, "aclk");
  387. if (IS_ERR(clk)) {
  388. ret = PTR_ERR(clk);
  389. dev_err(&pdev->dev,
  390. "can't identify CAAM aclk clk: %d\n", ret);
  391. return ret;
  392. }
  393. ctrlpriv->caam_aclk = clk;
  394. clk = caam_drv_identify_clk(&pdev->dev, "emi_slow");
  395. if (IS_ERR(clk)) {
  396. ret = PTR_ERR(clk);
  397. dev_err(&pdev->dev,
  398. "can't identify CAAM emi_slow clk: %d\n", ret);
  399. return ret;
  400. }
  401. ctrlpriv->caam_emi_slow = clk;
  402. ret = clk_prepare_enable(ctrlpriv->caam_ipg);
  403. if (ret < 0) {
  404. dev_err(&pdev->dev, "can't enable CAAM ipg clock: %d\n", ret);
  405. return ret;
  406. }
  407. ret = clk_prepare_enable(ctrlpriv->caam_mem);
  408. if (ret < 0) {
  409. dev_err(&pdev->dev, "can't enable CAAM secure mem clock: %d\n",
  410. ret);
  411. goto disable_caam_ipg;
  412. }
  413. ret = clk_prepare_enable(ctrlpriv->caam_aclk);
  414. if (ret < 0) {
  415. dev_err(&pdev->dev, "can't enable CAAM aclk clock: %d\n", ret);
  416. goto disable_caam_mem;
  417. }
  418. ret = clk_prepare_enable(ctrlpriv->caam_emi_slow);
  419. if (ret < 0) {
  420. dev_err(&pdev->dev, "can't enable CAAM emi slow clock: %d\n",
  421. ret);
  422. goto disable_caam_aclk;
  423. }
  424. /* Get configuration properties from device tree */
  425. /* First, get register page */
  426. ctrl = of_iomap(nprop, 0);
  427. if (ctrl == NULL) {
  428. dev_err(dev, "caam: of_iomap() failed\n");
  429. ret = -ENOMEM;
  430. goto disable_caam_emi_slow;
  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_AWCACHE_BUFF | MCFGR_WDENABLE | MCFGR_LARGE_BURST |
  459. (sizeof(dma_addr_t) == sizeof(u64) ? 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_kcalloc(&pdev->dev, rspec,
  501. sizeof(*ctrlpriv->jrpdev), GFP_KERNEL);
  502. if (ctrlpriv->jrpdev == NULL) {
  503. ret = -ENOMEM;
  504. goto iounmap_ctrl;
  505. }
  506. ring = 0;
  507. ctrlpriv->total_jobrs = 0;
  508. for_each_available_child_of_node(nprop, np)
  509. if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
  510. of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
  511. ctrlpriv->jrpdev[ring] =
  512. of_platform_device_create(np, NULL, dev);
  513. if (!ctrlpriv->jrpdev[ring]) {
  514. pr_warn("JR%d Platform device creation error\n",
  515. ring);
  516. continue;
  517. }
  518. ctrlpriv->jr[ring] = (struct caam_job_ring __force *)
  519. ((uint8_t *)ctrl +
  520. (ring + JR_BLOCK_NUMBER) *
  521. BLOCK_OFFSET
  522. );
  523. ctrlpriv->total_jobrs++;
  524. ring++;
  525. }
  526. /* Check to see if QI present. If so, enable */
  527. ctrlpriv->qi_present =
  528. !!(rd_reg32(&ctrl->perfmon.comp_parms_ms) &
  529. CTPR_MS_QI_MASK);
  530. if (ctrlpriv->qi_present) {
  531. ctrlpriv->qi = (struct caam_queue_if __force *)
  532. ((uint8_t *)ctrl +
  533. BLOCK_OFFSET * QI_BLOCK_NUMBER
  534. );
  535. /* This is all that's required to physically enable QI */
  536. wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
  537. }
  538. /* If no QI and no rings specified, quit and go home */
  539. if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
  540. dev_err(dev, "no queues configured, terminating\n");
  541. ret = -ENOMEM;
  542. goto caam_remove;
  543. }
  544. cha_vid_ls = rd_reg32(&ctrl->perfmon.cha_id_ls);
  545. /*
  546. * If SEC has RNG version >= 4 and RNG state handle has not been
  547. * already instantiated, do RNG instantiation
  548. */
  549. if ((cha_vid_ls & CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT >= 4) {
  550. ctrlpriv->rng4_sh_init =
  551. rd_reg32(&ctrl->r4tst[0].rdsta);
  552. /*
  553. * If the secure keys (TDKEK, JDKEK, TDSK), were already
  554. * generated, signal this to the function that is instantiating
  555. * the state handles. An error would occur if RNG4 attempts
  556. * to regenerate these keys before the next POR.
  557. */
  558. gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
  559. ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
  560. do {
  561. int inst_handles =
  562. rd_reg32(&ctrl->r4tst[0].rdsta) &
  563. RDSTA_IFMASK;
  564. /*
  565. * If either SH were instantiated by somebody else
  566. * (e.g. u-boot) then it is assumed that the entropy
  567. * parameters are properly set and thus the function
  568. * setting these (kick_trng(...)) is skipped.
  569. * Also, if a handle was instantiated, do not change
  570. * the TRNG parameters.
  571. */
  572. if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
  573. dev_info(dev,
  574. "Entropy delay = %u\n",
  575. ent_delay);
  576. kick_trng(pdev, ent_delay);
  577. ent_delay += 400;
  578. }
  579. /*
  580. * if instantiate_rng(...) fails, the loop will rerun
  581. * and the kick_trng(...) function will modfiy the
  582. * upper and lower limits of the entropy sampling
  583. * interval, leading to a sucessful initialization of
  584. * the RNG.
  585. */
  586. ret = instantiate_rng(dev, inst_handles,
  587. gen_sk);
  588. if (ret == -EAGAIN)
  589. /*
  590. * if here, the loop will rerun,
  591. * so don't hog the CPU
  592. */
  593. cpu_relax();
  594. } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  595. if (ret) {
  596. dev_err(dev, "failed to instantiate RNG");
  597. goto caam_remove;
  598. }
  599. /*
  600. * Set handles init'ed by this module as the complement of the
  601. * already initialized ones
  602. */
  603. ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
  604. /* Enable RDB bit so that RNG works faster */
  605. setbits32(&ctrl->scfgr, SCFGR_RDBENABLE);
  606. }
  607. /* NOTE: RTIC detection ought to go here, around Si time */
  608. caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 |
  609. (u64)rd_reg32(&ctrl->perfmon.caam_id_ls);
  610. /* Report "alive" for developer to see */
  611. dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
  612. caam_get_era());
  613. dev_info(dev, "job rings = %d, qi = %d\n",
  614. ctrlpriv->total_jobrs, ctrlpriv->qi_present);
  615. #ifdef CONFIG_DEBUG_FS
  616. /*
  617. * FIXME: needs better naming distinction, as some amalgamation of
  618. * "caam" and nprop->full_name. The OF name isn't distinctive,
  619. * but does separate instances
  620. */
  621. perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
  622. ctrlpriv->dfs_root = debugfs_create_dir(dev_name(dev), NULL);
  623. ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
  624. /* Controller-level - performance monitor counters */
  625. ctrlpriv->ctl_rq_dequeued =
  626. debugfs_create_u64("rq_dequeued",
  627. S_IRUSR | S_IRGRP | S_IROTH,
  628. ctrlpriv->ctl, &perfmon->req_dequeued);
  629. ctrlpriv->ctl_ob_enc_req =
  630. debugfs_create_u64("ob_rq_encrypted",
  631. S_IRUSR | S_IRGRP | S_IROTH,
  632. ctrlpriv->ctl, &perfmon->ob_enc_req);
  633. ctrlpriv->ctl_ib_dec_req =
  634. debugfs_create_u64("ib_rq_decrypted",
  635. S_IRUSR | S_IRGRP | S_IROTH,
  636. ctrlpriv->ctl, &perfmon->ib_dec_req);
  637. ctrlpriv->ctl_ob_enc_bytes =
  638. debugfs_create_u64("ob_bytes_encrypted",
  639. S_IRUSR | S_IRGRP | S_IROTH,
  640. ctrlpriv->ctl, &perfmon->ob_enc_bytes);
  641. ctrlpriv->ctl_ob_prot_bytes =
  642. debugfs_create_u64("ob_bytes_protected",
  643. S_IRUSR | S_IRGRP | S_IROTH,
  644. ctrlpriv->ctl, &perfmon->ob_prot_bytes);
  645. ctrlpriv->ctl_ib_dec_bytes =
  646. debugfs_create_u64("ib_bytes_decrypted",
  647. S_IRUSR | S_IRGRP | S_IROTH,
  648. ctrlpriv->ctl, &perfmon->ib_dec_bytes);
  649. ctrlpriv->ctl_ib_valid_bytes =
  650. debugfs_create_u64("ib_bytes_validated",
  651. S_IRUSR | S_IRGRP | S_IROTH,
  652. ctrlpriv->ctl, &perfmon->ib_valid_bytes);
  653. /* Controller level - global status values */
  654. ctrlpriv->ctl_faultaddr =
  655. debugfs_create_u64("fault_addr",
  656. S_IRUSR | S_IRGRP | S_IROTH,
  657. ctrlpriv->ctl, &perfmon->faultaddr);
  658. ctrlpriv->ctl_faultdetail =
  659. debugfs_create_u32("fault_detail",
  660. S_IRUSR | S_IRGRP | S_IROTH,
  661. ctrlpriv->ctl, &perfmon->faultdetail);
  662. ctrlpriv->ctl_faultstatus =
  663. debugfs_create_u32("fault_status",
  664. S_IRUSR | S_IRGRP | S_IROTH,
  665. ctrlpriv->ctl, &perfmon->status);
  666. /* Internal covering keys (useful in non-secure mode only) */
  667. ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
  668. ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  669. ctrlpriv->ctl_kek = debugfs_create_blob("kek",
  670. S_IRUSR |
  671. S_IRGRP | S_IROTH,
  672. ctrlpriv->ctl,
  673. &ctrlpriv->ctl_kek_wrap);
  674. ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
  675. ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  676. ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
  677. S_IRUSR |
  678. S_IRGRP | S_IROTH,
  679. ctrlpriv->ctl,
  680. &ctrlpriv->ctl_tkek_wrap);
  681. ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
  682. ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  683. ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
  684. S_IRUSR |
  685. S_IRGRP | S_IROTH,
  686. ctrlpriv->ctl,
  687. &ctrlpriv->ctl_tdsk_wrap);
  688. #endif
  689. return 0;
  690. caam_remove:
  691. caam_remove(pdev);
  692. iounmap_ctrl:
  693. iounmap(ctrl);
  694. disable_caam_emi_slow:
  695. clk_disable_unprepare(ctrlpriv->caam_emi_slow);
  696. disable_caam_aclk:
  697. clk_disable_unprepare(ctrlpriv->caam_aclk);
  698. disable_caam_mem:
  699. clk_disable_unprepare(ctrlpriv->caam_mem);
  700. disable_caam_ipg:
  701. clk_disable_unprepare(ctrlpriv->caam_ipg);
  702. return ret;
  703. }
  704. static struct of_device_id caam_match[] = {
  705. {
  706. .compatible = "fsl,sec-v4.0",
  707. },
  708. {
  709. .compatible = "fsl,sec4.0",
  710. },
  711. {},
  712. };
  713. MODULE_DEVICE_TABLE(of, caam_match);
  714. static struct platform_driver caam_driver = {
  715. .driver = {
  716. .name = "caam",
  717. .of_match_table = caam_match,
  718. },
  719. .probe = caam_probe,
  720. .remove = caam_remove,
  721. };
  722. module_platform_driver(caam_driver);
  723. MODULE_LICENSE("GPL");
  724. MODULE_DESCRIPTION("FSL CAAM request backend");
  725. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");