ctrl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. if (ret)
  190. break;
  191. rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK;
  192. if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) ||
  193. !(rdsta_val & (1 << sh_idx))) {
  194. ret = -EAGAIN;
  195. break;
  196. }
  197. dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
  198. /* Clear the contents before recreating the descriptor */
  199. memset(desc, 0x00, CAAM_CMD_SZ * 7);
  200. }
  201. kfree(desc);
  202. return ret;
  203. }
  204. /*
  205. * deinstantiate_rng - builds and executes a descriptor on DECO0,
  206. * which deinitializes the RNG block.
  207. * @ctrldev - pointer to device
  208. * @state_handle_mask - bitmask containing the instantiation status
  209. * for the RNG4 state handles which exist in
  210. * the RNG4 block: 1 if it's been instantiated
  211. *
  212. * Return: - 0 if no error occurred
  213. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  214. * - -ENODEV if DECO0 couldn't be acquired
  215. * - -EAGAIN if an error occurred when executing the descriptor
  216. */
  217. static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
  218. {
  219. u32 *desc, status;
  220. int sh_idx, ret = 0;
  221. desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
  222. if (!desc)
  223. return -ENOMEM;
  224. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  225. /*
  226. * If the corresponding bit is set, then it means the state
  227. * handle was initialized by us, and thus it needs to be
  228. * deintialized as well
  229. */
  230. if ((1 << sh_idx) & state_handle_mask) {
  231. /*
  232. * Create the descriptor for deinstantating this state
  233. * handle
  234. */
  235. build_deinstantiation_desc(desc, sh_idx);
  236. /* Try to run it through DECO0 */
  237. ret = run_descriptor_deco0(ctrldev, desc, &status);
  238. if (ret ||
  239. (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
  240. dev_err(ctrldev,
  241. "Failed to deinstantiate RNG4 SH%d\n",
  242. sh_idx);
  243. break;
  244. }
  245. dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
  246. }
  247. }
  248. kfree(desc);
  249. return ret;
  250. }
  251. static int caam_remove(struct platform_device *pdev)
  252. {
  253. struct device *ctrldev;
  254. struct caam_drv_private *ctrlpriv;
  255. struct caam_ctrl __iomem *ctrl;
  256. int ring;
  257. ctrldev = &pdev->dev;
  258. ctrlpriv = dev_get_drvdata(ctrldev);
  259. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  260. /* Remove platform devices for JobRs */
  261. for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
  262. if (ctrlpriv->jrpdev[ring])
  263. of_device_unregister(ctrlpriv->jrpdev[ring]);
  264. }
  265. /* De-initialize RNG state handles initialized by this driver. */
  266. if (ctrlpriv->rng4_sh_init)
  267. deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
  268. /* Shut down debug views */
  269. #ifdef CONFIG_DEBUG_FS
  270. debugfs_remove_recursive(ctrlpriv->dfs_root);
  271. #endif
  272. /* Unmap controller region */
  273. iounmap(ctrl);
  274. /* shut clocks off before finalizing shutdown */
  275. clk_disable_unprepare(ctrlpriv->caam_ipg);
  276. clk_disable_unprepare(ctrlpriv->caam_mem);
  277. clk_disable_unprepare(ctrlpriv->caam_aclk);
  278. clk_disable_unprepare(ctrlpriv->caam_emi_slow);
  279. return 0;
  280. }
  281. /*
  282. * kick_trng - sets the various parameters for enabling the initialization
  283. * of the RNG4 block in CAAM
  284. * @pdev - pointer to the platform device
  285. * @ent_delay - Defines the length (in system clocks) of each entropy sample.
  286. */
  287. static void kick_trng(struct platform_device *pdev, int ent_delay)
  288. {
  289. struct device *ctrldev = &pdev->dev;
  290. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  291. struct caam_ctrl __iomem *ctrl;
  292. struct rng4tst __iomem *r4tst;
  293. u32 val;
  294. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  295. r4tst = &ctrl->r4tst[0];
  296. /* put RNG4 into program mode */
  297. setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  298. /*
  299. * Performance-wise, it does not make sense to
  300. * set the delay to a value that is lower
  301. * than the last one that worked (i.e. the state handles
  302. * were instantiated properly. Thus, instead of wasting
  303. * time trying to set the values controlling the sample
  304. * frequency, the function simply returns.
  305. */
  306. val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
  307. >> RTSDCTL_ENT_DLY_SHIFT;
  308. if (ent_delay <= val) {
  309. /* put RNG4 into run mode */
  310. clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
  311. return;
  312. }
  313. val = rd_reg32(&r4tst->rtsdctl);
  314. val = (val & ~RTSDCTL_ENT_DLY_MASK) |
  315. (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
  316. wr_reg32(&r4tst->rtsdctl, val);
  317. /* min. freq. count, equal to 1/4 of the entropy sample length */
  318. wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
  319. /* disable maximum frequency count */
  320. wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE);
  321. /* read the control register */
  322. val = rd_reg32(&r4tst->rtmctl);
  323. /*
  324. * select raw sampling in both entropy shifter
  325. * and statistical checker
  326. */
  327. setbits32(&val, RTMCTL_SAMP_MODE_RAW_ES_SC);
  328. /* put RNG4 into run mode */
  329. clrbits32(&val, RTMCTL_PRGM);
  330. /* write back the control register */
  331. wr_reg32(&r4tst->rtmctl, val);
  332. }
  333. /**
  334. * caam_get_era() - Return the ERA of the SEC on SoC, based
  335. * on "sec-era" propery in the DTS. This property is updated by u-boot.
  336. **/
  337. int caam_get_era(void)
  338. {
  339. struct device_node *caam_node;
  340. int ret;
  341. u32 prop;
  342. caam_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  343. ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop);
  344. of_node_put(caam_node);
  345. return IS_ERR_VALUE(ret) ? -ENOTSUPP : prop;
  346. }
  347. EXPORT_SYMBOL(caam_get_era);
  348. /* Probe routine for CAAM top (controller) level */
  349. static int caam_probe(struct platform_device *pdev)
  350. {
  351. int ret, ring, rspec, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
  352. u64 caam_id;
  353. struct device *dev;
  354. struct device_node *nprop, *np;
  355. struct caam_ctrl __iomem *ctrl;
  356. struct caam_drv_private *ctrlpriv;
  357. struct clk *clk;
  358. #ifdef CONFIG_DEBUG_FS
  359. struct caam_perfmon *perfmon;
  360. #endif
  361. u32 scfgr, comp_params;
  362. u32 cha_vid_ls;
  363. int pg_size;
  364. int BLOCK_OFFSET = 0;
  365. ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
  366. if (!ctrlpriv)
  367. return -ENOMEM;
  368. dev = &pdev->dev;
  369. dev_set_drvdata(dev, ctrlpriv);
  370. ctrlpriv->pdev = pdev;
  371. nprop = pdev->dev.of_node;
  372. /* Enable clocking */
  373. clk = caam_drv_identify_clk(&pdev->dev, "ipg");
  374. if (IS_ERR(clk)) {
  375. ret = PTR_ERR(clk);
  376. dev_err(&pdev->dev,
  377. "can't identify CAAM ipg clk: %d\n", ret);
  378. return ret;
  379. }
  380. ctrlpriv->caam_ipg = clk;
  381. clk = caam_drv_identify_clk(&pdev->dev, "mem");
  382. if (IS_ERR(clk)) {
  383. ret = PTR_ERR(clk);
  384. dev_err(&pdev->dev,
  385. "can't identify CAAM mem clk: %d\n", ret);
  386. return ret;
  387. }
  388. ctrlpriv->caam_mem = clk;
  389. clk = caam_drv_identify_clk(&pdev->dev, "aclk");
  390. if (IS_ERR(clk)) {
  391. ret = PTR_ERR(clk);
  392. dev_err(&pdev->dev,
  393. "can't identify CAAM aclk clk: %d\n", ret);
  394. return ret;
  395. }
  396. ctrlpriv->caam_aclk = clk;
  397. clk = caam_drv_identify_clk(&pdev->dev, "emi_slow");
  398. if (IS_ERR(clk)) {
  399. ret = PTR_ERR(clk);
  400. dev_err(&pdev->dev,
  401. "can't identify CAAM emi_slow clk: %d\n", ret);
  402. return ret;
  403. }
  404. ctrlpriv->caam_emi_slow = clk;
  405. ret = clk_prepare_enable(ctrlpriv->caam_ipg);
  406. if (ret < 0) {
  407. dev_err(&pdev->dev, "can't enable CAAM ipg clock: %d\n", ret);
  408. return ret;
  409. }
  410. ret = clk_prepare_enable(ctrlpriv->caam_mem);
  411. if (ret < 0) {
  412. dev_err(&pdev->dev, "can't enable CAAM secure mem clock: %d\n",
  413. ret);
  414. goto disable_caam_ipg;
  415. }
  416. ret = clk_prepare_enable(ctrlpriv->caam_aclk);
  417. if (ret < 0) {
  418. dev_err(&pdev->dev, "can't enable CAAM aclk clock: %d\n", ret);
  419. goto disable_caam_mem;
  420. }
  421. ret = clk_prepare_enable(ctrlpriv->caam_emi_slow);
  422. if (ret < 0) {
  423. dev_err(&pdev->dev, "can't enable CAAM emi slow clock: %d\n",
  424. ret);
  425. goto disable_caam_aclk;
  426. }
  427. /* Get configuration properties from device tree */
  428. /* First, get register page */
  429. ctrl = of_iomap(nprop, 0);
  430. if (ctrl == NULL) {
  431. dev_err(dev, "caam: of_iomap() failed\n");
  432. ret = -ENOMEM;
  433. goto disable_caam_emi_slow;
  434. }
  435. /* Finding the page size for using the CTPR_MS register */
  436. comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
  437. pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
  438. /* Allocating the BLOCK_OFFSET based on the supported page size on
  439. * the platform
  440. */
  441. if (pg_size == 0)
  442. BLOCK_OFFSET = PG_SIZE_4K;
  443. else
  444. BLOCK_OFFSET = PG_SIZE_64K;
  445. ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
  446. ctrlpriv->assure = (struct caam_assurance __force *)
  447. ((uint8_t *)ctrl +
  448. BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
  449. );
  450. ctrlpriv->deco = (struct caam_deco __force *)
  451. ((uint8_t *)ctrl +
  452. BLOCK_OFFSET * DECO_BLOCK_NUMBER
  453. );
  454. /* Get the IRQ of the controller (for security violations only) */
  455. ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
  456. /*
  457. * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
  458. * long pointers in master configuration register
  459. */
  460. clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK, MCFGR_AWCACHE_CACH |
  461. MCFGR_AWCACHE_BUFF | MCFGR_WDENABLE |
  462. (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
  463. /*
  464. * Read the Compile Time paramters and SCFGR to determine
  465. * if Virtualization is enabled for this platform
  466. */
  467. scfgr = rd_reg32(&ctrl->scfgr);
  468. ctrlpriv->virt_en = 0;
  469. if (comp_params & CTPR_MS_VIRT_EN_INCL) {
  470. /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
  471. * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
  472. */
  473. if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
  474. (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
  475. (scfgr & SCFGR_VIRT_EN)))
  476. ctrlpriv->virt_en = 1;
  477. } else {
  478. /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
  479. if (comp_params & CTPR_MS_VIRT_EN_POR)
  480. ctrlpriv->virt_en = 1;
  481. }
  482. if (ctrlpriv->virt_en == 1)
  483. setbits32(&ctrl->jrstart, JRSTART_JR0_START |
  484. JRSTART_JR1_START | JRSTART_JR2_START |
  485. JRSTART_JR3_START);
  486. if (sizeof(dma_addr_t) == sizeof(u64))
  487. if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
  488. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
  489. else
  490. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(36));
  491. else
  492. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  493. /*
  494. * Detect and enable JobRs
  495. * First, find out how many ring spec'ed, allocate references
  496. * for all, then go probe each one.
  497. */
  498. rspec = 0;
  499. for_each_available_child_of_node(nprop, np)
  500. if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
  501. of_device_is_compatible(np, "fsl,sec4.0-job-ring"))
  502. rspec++;
  503. ctrlpriv->jrpdev = devm_kcalloc(&pdev->dev, rspec,
  504. sizeof(*ctrlpriv->jrpdev), GFP_KERNEL);
  505. if (ctrlpriv->jrpdev == NULL) {
  506. ret = -ENOMEM;
  507. goto iounmap_ctrl;
  508. }
  509. ring = 0;
  510. ctrlpriv->total_jobrs = 0;
  511. for_each_available_child_of_node(nprop, np)
  512. if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
  513. of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
  514. ctrlpriv->jrpdev[ring] =
  515. of_platform_device_create(np, NULL, dev);
  516. if (!ctrlpriv->jrpdev[ring]) {
  517. pr_warn("JR%d Platform device creation error\n",
  518. ring);
  519. continue;
  520. }
  521. ctrlpriv->jr[ring] = (struct caam_job_ring __force *)
  522. ((uint8_t *)ctrl +
  523. (ring + JR_BLOCK_NUMBER) *
  524. BLOCK_OFFSET
  525. );
  526. ctrlpriv->total_jobrs++;
  527. ring++;
  528. }
  529. /* Check to see if QI present. If so, enable */
  530. ctrlpriv->qi_present =
  531. !!(rd_reg32(&ctrl->perfmon.comp_parms_ms) &
  532. CTPR_MS_QI_MASK);
  533. if (ctrlpriv->qi_present) {
  534. ctrlpriv->qi = (struct caam_queue_if __force *)
  535. ((uint8_t *)ctrl +
  536. BLOCK_OFFSET * QI_BLOCK_NUMBER
  537. );
  538. /* This is all that's required to physically enable QI */
  539. wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
  540. }
  541. /* If no QI and no rings specified, quit and go home */
  542. if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
  543. dev_err(dev, "no queues configured, terminating\n");
  544. ret = -ENOMEM;
  545. goto caam_remove;
  546. }
  547. cha_vid_ls = rd_reg32(&ctrl->perfmon.cha_id_ls);
  548. /*
  549. * If SEC has RNG version >= 4 and RNG state handle has not been
  550. * already instantiated, do RNG instantiation
  551. */
  552. if ((cha_vid_ls & CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT >= 4) {
  553. ctrlpriv->rng4_sh_init =
  554. rd_reg32(&ctrl->r4tst[0].rdsta);
  555. /*
  556. * If the secure keys (TDKEK, JDKEK, TDSK), were already
  557. * generated, signal this to the function that is instantiating
  558. * the state handles. An error would occur if RNG4 attempts
  559. * to regenerate these keys before the next POR.
  560. */
  561. gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
  562. ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
  563. do {
  564. int inst_handles =
  565. rd_reg32(&ctrl->r4tst[0].rdsta) &
  566. RDSTA_IFMASK;
  567. /*
  568. * If either SH were instantiated by somebody else
  569. * (e.g. u-boot) then it is assumed that the entropy
  570. * parameters are properly set and thus the function
  571. * setting these (kick_trng(...)) is skipped.
  572. * Also, if a handle was instantiated, do not change
  573. * the TRNG parameters.
  574. */
  575. if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
  576. dev_info(dev,
  577. "Entropy delay = %u\n",
  578. ent_delay);
  579. kick_trng(pdev, ent_delay);
  580. ent_delay += 400;
  581. }
  582. /*
  583. * if instantiate_rng(...) fails, the loop will rerun
  584. * and the kick_trng(...) function will modfiy the
  585. * upper and lower limits of the entropy sampling
  586. * interval, leading to a sucessful initialization of
  587. * the RNG.
  588. */
  589. ret = instantiate_rng(dev, inst_handles,
  590. gen_sk);
  591. if (ret == -EAGAIN)
  592. /*
  593. * if here, the loop will rerun,
  594. * so don't hog the CPU
  595. */
  596. cpu_relax();
  597. } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  598. if (ret) {
  599. dev_err(dev, "failed to instantiate RNG");
  600. goto caam_remove;
  601. }
  602. /*
  603. * Set handles init'ed by this module as the complement of the
  604. * already initialized ones
  605. */
  606. ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
  607. /* Enable RDB bit so that RNG works faster */
  608. setbits32(&ctrl->scfgr, SCFGR_RDBENABLE);
  609. }
  610. /* NOTE: RTIC detection ought to go here, around Si time */
  611. caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 |
  612. (u64)rd_reg32(&ctrl->perfmon.caam_id_ls);
  613. /* Report "alive" for developer to see */
  614. dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
  615. caam_get_era());
  616. dev_info(dev, "job rings = %d, qi = %d\n",
  617. ctrlpriv->total_jobrs, ctrlpriv->qi_present);
  618. #ifdef CONFIG_DEBUG_FS
  619. /*
  620. * FIXME: needs better naming distinction, as some amalgamation of
  621. * "caam" and nprop->full_name. The OF name isn't distinctive,
  622. * but does separate instances
  623. */
  624. perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
  625. ctrlpriv->dfs_root = debugfs_create_dir(dev_name(dev), NULL);
  626. ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
  627. /* Controller-level - performance monitor counters */
  628. ctrlpriv->ctl_rq_dequeued =
  629. debugfs_create_u64("rq_dequeued",
  630. S_IRUSR | S_IRGRP | S_IROTH,
  631. ctrlpriv->ctl, &perfmon->req_dequeued);
  632. ctrlpriv->ctl_ob_enc_req =
  633. debugfs_create_u64("ob_rq_encrypted",
  634. S_IRUSR | S_IRGRP | S_IROTH,
  635. ctrlpriv->ctl, &perfmon->ob_enc_req);
  636. ctrlpriv->ctl_ib_dec_req =
  637. debugfs_create_u64("ib_rq_decrypted",
  638. S_IRUSR | S_IRGRP | S_IROTH,
  639. ctrlpriv->ctl, &perfmon->ib_dec_req);
  640. ctrlpriv->ctl_ob_enc_bytes =
  641. debugfs_create_u64("ob_bytes_encrypted",
  642. S_IRUSR | S_IRGRP | S_IROTH,
  643. ctrlpriv->ctl, &perfmon->ob_enc_bytes);
  644. ctrlpriv->ctl_ob_prot_bytes =
  645. debugfs_create_u64("ob_bytes_protected",
  646. S_IRUSR | S_IRGRP | S_IROTH,
  647. ctrlpriv->ctl, &perfmon->ob_prot_bytes);
  648. ctrlpriv->ctl_ib_dec_bytes =
  649. debugfs_create_u64("ib_bytes_decrypted",
  650. S_IRUSR | S_IRGRP | S_IROTH,
  651. ctrlpriv->ctl, &perfmon->ib_dec_bytes);
  652. ctrlpriv->ctl_ib_valid_bytes =
  653. debugfs_create_u64("ib_bytes_validated",
  654. S_IRUSR | S_IRGRP | S_IROTH,
  655. ctrlpriv->ctl, &perfmon->ib_valid_bytes);
  656. /* Controller level - global status values */
  657. ctrlpriv->ctl_faultaddr =
  658. debugfs_create_u64("fault_addr",
  659. S_IRUSR | S_IRGRP | S_IROTH,
  660. ctrlpriv->ctl, &perfmon->faultaddr);
  661. ctrlpriv->ctl_faultdetail =
  662. debugfs_create_u32("fault_detail",
  663. S_IRUSR | S_IRGRP | S_IROTH,
  664. ctrlpriv->ctl, &perfmon->faultdetail);
  665. ctrlpriv->ctl_faultstatus =
  666. debugfs_create_u32("fault_status",
  667. S_IRUSR | S_IRGRP | S_IROTH,
  668. ctrlpriv->ctl, &perfmon->status);
  669. /* Internal covering keys (useful in non-secure mode only) */
  670. ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
  671. ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  672. ctrlpriv->ctl_kek = debugfs_create_blob("kek",
  673. S_IRUSR |
  674. S_IRGRP | S_IROTH,
  675. ctrlpriv->ctl,
  676. &ctrlpriv->ctl_kek_wrap);
  677. ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
  678. ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  679. ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
  680. S_IRUSR |
  681. S_IRGRP | S_IROTH,
  682. ctrlpriv->ctl,
  683. &ctrlpriv->ctl_tkek_wrap);
  684. ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
  685. ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  686. ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
  687. S_IRUSR |
  688. S_IRGRP | S_IROTH,
  689. ctrlpriv->ctl,
  690. &ctrlpriv->ctl_tdsk_wrap);
  691. #endif
  692. return 0;
  693. caam_remove:
  694. caam_remove(pdev);
  695. iounmap_ctrl:
  696. iounmap(ctrl);
  697. disable_caam_emi_slow:
  698. clk_disable_unprepare(ctrlpriv->caam_emi_slow);
  699. disable_caam_aclk:
  700. clk_disable_unprepare(ctrlpriv->caam_aclk);
  701. disable_caam_mem:
  702. clk_disable_unprepare(ctrlpriv->caam_mem);
  703. disable_caam_ipg:
  704. clk_disable_unprepare(ctrlpriv->caam_ipg);
  705. return ret;
  706. }
  707. static struct of_device_id caam_match[] = {
  708. {
  709. .compatible = "fsl,sec-v4.0",
  710. },
  711. {
  712. .compatible = "fsl,sec4.0",
  713. },
  714. {},
  715. };
  716. MODULE_DEVICE_TABLE(of, caam_match);
  717. static struct platform_driver caam_driver = {
  718. .driver = {
  719. .name = "caam",
  720. .of_match_table = caam_match,
  721. },
  722. .probe = caam_probe,
  723. .remove = caam_remove,
  724. };
  725. module_platform_driver(caam_driver);
  726. MODULE_LICENSE("GPL");
  727. MODULE_DESCRIPTION("FSL CAAM request backend");
  728. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");