ctrl.c 25 KB

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