ctrl.c 24 KB

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