ctrl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* * CAAM control-plane driver backend
  3. * Controller-level driver, kernel property detection, initialization
  4. *
  5. * Copyright 2008-2012 Freescale Semiconductor, Inc.
  6. * Copyright 2018-2019 NXP
  7. */
  8. #include <linux/device.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/sys_soc.h>
  12. #include "compat.h"
  13. #include "regs.h"
  14. #include "intern.h"
  15. #include "jr.h"
  16. #include "desc_constr.h"
  17. #include "ctrl.h"
  18. bool caam_dpaa2;
  19. EXPORT_SYMBOL(caam_dpaa2);
  20. #ifdef CONFIG_CAAM_QI
  21. #include "qi.h"
  22. #endif
  23. /*
  24. * Descriptor to instantiate RNG State Handle 0 in normal mode and
  25. * load the JDKEK, TDKEK and TDSK registers
  26. */
  27. static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
  28. {
  29. u32 *jump_cmd, op_flags;
  30. init_job_desc(desc, 0);
  31. op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  32. (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT;
  33. /* INIT RNG in non-test mode */
  34. append_operation(desc, op_flags);
  35. if (!handle && do_sk) {
  36. /*
  37. * For SH0, Secure Keys must be generated as well
  38. */
  39. /* wait for done */
  40. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
  41. set_jump_tgt_here(desc, jump_cmd);
  42. /*
  43. * load 1 to clear written reg:
  44. * resets the done interrrupt and returns the RNG to idle.
  45. */
  46. append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
  47. /* Initialize State Handle */
  48. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  49. OP_ALG_AAI_RNG4_SK);
  50. }
  51. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  52. }
  53. /* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
  54. static void build_deinstantiation_desc(u32 *desc, int handle)
  55. {
  56. init_job_desc(desc, 0);
  57. /* Uninstantiate State Handle 0 */
  58. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  59. (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
  60. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  61. }
  62. /*
  63. * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
  64. * the software (no JR/QI used).
  65. * @ctrldev - pointer to device
  66. * @status - descriptor status, after being run
  67. *
  68. * Return: - 0 if no error occurred
  69. * - -ENODEV if the DECO couldn't be acquired
  70. * - -EAGAIN if an error occurred while executing the descriptor
  71. */
  72. static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
  73. u32 *status)
  74. {
  75. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  76. struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
  77. struct caam_deco __iomem *deco = ctrlpriv->deco;
  78. unsigned int timeout = 100000;
  79. u32 deco_dbg_reg, deco_state, flags;
  80. int i;
  81. if (ctrlpriv->virt_en == 1 ||
  82. /*
  83. * Apparently on i.MX8MQ it doesn't matter if virt_en == 1
  84. * and the following steps should be performed regardless
  85. */
  86. of_machine_is_compatible("fsl,imx8mq")) {
  87. clrsetbits_32(&ctrl->deco_rsr, 0, DECORSR_JR0);
  88. while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) &&
  89. --timeout)
  90. cpu_relax();
  91. timeout = 100000;
  92. }
  93. clrsetbits_32(&ctrl->deco_rq, 0, DECORR_RQD0ENABLE);
  94. while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) &&
  95. --timeout)
  96. cpu_relax();
  97. if (!timeout) {
  98. dev_err(ctrldev, "failed to acquire DECO 0\n");
  99. clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
  100. return -ENODEV;
  101. }
  102. for (i = 0; i < desc_len(desc); i++)
  103. wr_reg32(&deco->descbuf[i], caam32_to_cpu(*(desc + i)));
  104. flags = DECO_JQCR_WHL;
  105. /*
  106. * If the descriptor length is longer than 4 words, then the
  107. * FOUR bit in JRCTRL register must be set.
  108. */
  109. if (desc_len(desc) >= 4)
  110. flags |= DECO_JQCR_FOUR;
  111. /* Instruct the DECO to execute it */
  112. clrsetbits_32(&deco->jr_ctl_hi, 0, flags);
  113. timeout = 10000000;
  114. do {
  115. deco_dbg_reg = rd_reg32(&deco->desc_dbg);
  116. if (ctrlpriv->era < 10)
  117. deco_state = (deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) >>
  118. DESC_DBG_DECO_STAT_SHIFT;
  119. else
  120. deco_state = (rd_reg32(&deco->dbg_exec) &
  121. DESC_DER_DECO_STAT_MASK) >>
  122. DESC_DER_DECO_STAT_SHIFT;
  123. /*
  124. * If an error occured in the descriptor, then
  125. * the DECO status field will be set to 0x0D
  126. */
  127. if (deco_state == 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. if (ret)
  192. break;
  193. rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK;
  194. if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) ||
  195. !(rdsta_val & (1 << sh_idx))) {
  196. ret = -EAGAIN;
  197. break;
  198. }
  199. dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
  200. /* Clear the contents before recreating the descriptor */
  201. memset(desc, 0x00, CAAM_CMD_SZ * 7);
  202. }
  203. kfree(desc);
  204. return ret;
  205. }
  206. /*
  207. * deinstantiate_rng - builds and executes a descriptor on DECO0,
  208. * which deinitializes the RNG block.
  209. * @ctrldev - pointer to device
  210. * @state_handle_mask - bitmask containing the instantiation status
  211. * for the RNG4 state handles which exist in
  212. * the RNG4 block: 1 if it's been instantiated
  213. *
  214. * Return: - 0 if no error occurred
  215. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  216. * - -ENODEV if DECO0 couldn't be acquired
  217. * - -EAGAIN if an error occurred when executing the descriptor
  218. */
  219. static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
  220. {
  221. u32 *desc, status;
  222. int sh_idx, ret = 0;
  223. desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
  224. if (!desc)
  225. return -ENOMEM;
  226. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  227. /*
  228. * If the corresponding bit is set, then it means the state
  229. * handle was initialized by us, and thus it needs to be
  230. * deinitialized as well
  231. */
  232. if ((1 << sh_idx) & state_handle_mask) {
  233. /*
  234. * Create the descriptor for deinstantating this state
  235. * handle
  236. */
  237. build_deinstantiation_desc(desc, sh_idx);
  238. /* Try to run it through DECO0 */
  239. ret = run_descriptor_deco0(ctrldev, desc, &status);
  240. if (ret ||
  241. (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
  242. dev_err(ctrldev,
  243. "Failed to deinstantiate RNG4 SH%d\n",
  244. sh_idx);
  245. break;
  246. }
  247. dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
  248. }
  249. }
  250. kfree(desc);
  251. return ret;
  252. }
  253. static int caam_remove(struct platform_device *pdev)
  254. {
  255. struct device *ctrldev;
  256. struct caam_drv_private *ctrlpriv;
  257. struct caam_ctrl __iomem *ctrl;
  258. ctrldev = &pdev->dev;
  259. ctrlpriv = dev_get_drvdata(ctrldev);
  260. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  261. /* Remove platform devices under the crypto node */
  262. of_platform_depopulate(ctrldev);
  263. #ifdef CONFIG_CAAM_QI
  264. if (ctrlpriv->qi_init)
  265. caam_qi_shutdown(ctrldev);
  266. #endif
  267. /*
  268. * De-initialize RNG state handles initialized by this driver.
  269. * In case of SoCs with Management Complex, RNG is managed by MC f/w.
  270. */
  271. if (!ctrlpriv->mc_en && ctrlpriv->rng4_sh_init)
  272. deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
  273. /* Shut down debug views */
  274. #ifdef CONFIG_DEBUG_FS
  275. debugfs_remove_recursive(ctrlpriv->dfs_root);
  276. #endif
  277. /* Unmap controller region */
  278. iounmap(ctrl);
  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. clrsetbits_32(&r4tst->rtmctl, 0, 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. goto start_rng;
  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. start_rng:
  321. /*
  322. * select raw sampling in both entropy shifter
  323. * and statistical checker; ; put RNG4 into run mode
  324. */
  325. clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM, RTMCTL_SAMP_MODE_RAW_ES_SC);
  326. }
  327. static int caam_get_era_from_hw(struct caam_ctrl __iomem *ctrl)
  328. {
  329. static const struct {
  330. u16 ip_id;
  331. u8 maj_rev;
  332. u8 era;
  333. } id[] = {
  334. {0x0A10, 1, 1},
  335. {0x0A10, 2, 2},
  336. {0x0A12, 1, 3},
  337. {0x0A14, 1, 3},
  338. {0x0A14, 2, 4},
  339. {0x0A16, 1, 4},
  340. {0x0A10, 3, 4},
  341. {0x0A11, 1, 4},
  342. {0x0A18, 1, 4},
  343. {0x0A11, 2, 5},
  344. {0x0A12, 2, 5},
  345. {0x0A13, 1, 5},
  346. {0x0A1C, 1, 5}
  347. };
  348. u32 ccbvid, id_ms;
  349. u8 maj_rev, era;
  350. u16 ip_id;
  351. int i;
  352. ccbvid = rd_reg32(&ctrl->perfmon.ccb_id);
  353. era = (ccbvid & CCBVID_ERA_MASK) >> CCBVID_ERA_SHIFT;
  354. if (era) /* This is '0' prior to CAAM ERA-6 */
  355. return era;
  356. id_ms = rd_reg32(&ctrl->perfmon.caam_id_ms);
  357. ip_id = (id_ms & SECVID_MS_IPID_MASK) >> SECVID_MS_IPID_SHIFT;
  358. maj_rev = (id_ms & SECVID_MS_MAJ_REV_MASK) >> SECVID_MS_MAJ_REV_SHIFT;
  359. for (i = 0; i < ARRAY_SIZE(id); i++)
  360. if (id[i].ip_id == ip_id && id[i].maj_rev == maj_rev)
  361. return id[i].era;
  362. return -ENOTSUPP;
  363. }
  364. /**
  365. * caam_get_era() - Return the ERA of the SEC on SoC, based
  366. * on "sec-era" optional property in the DTS. This property is updated
  367. * by u-boot.
  368. * In case this property is not passed an attempt to retrieve the CAAM
  369. * era via register reads will be made.
  370. **/
  371. static int caam_get_era(struct caam_ctrl __iomem *ctrl)
  372. {
  373. struct device_node *caam_node;
  374. int ret;
  375. u32 prop;
  376. caam_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  377. ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop);
  378. of_node_put(caam_node);
  379. if (!ret)
  380. return prop;
  381. else
  382. return caam_get_era_from_hw(ctrl);
  383. }
  384. /*
  385. * ERRATA: imx6 devices (imx6D, imx6Q, imx6DL, imx6S, imx6DP and imx6QP)
  386. * have an issue wherein AXI bus transactions may not occur in the correct
  387. * order. This isn't a problem running single descriptors, but can be if
  388. * running multiple concurrent descriptors. Reworking the driver to throttle
  389. * to single requests is impractical, thus the workaround is to limit the AXI
  390. * pipeline to a depth of 1 (from it's default of 4) to preclude this situation
  391. * from occurring.
  392. */
  393. static void handle_imx6_err005766(u32 *mcr)
  394. {
  395. if (of_machine_is_compatible("fsl,imx6q") ||
  396. of_machine_is_compatible("fsl,imx6dl") ||
  397. of_machine_is_compatible("fsl,imx6qp"))
  398. clrsetbits_32(mcr, MCFGR_AXIPIPE_MASK,
  399. 1 << MCFGR_AXIPIPE_SHIFT);
  400. }
  401. static const struct of_device_id caam_match[] = {
  402. {
  403. .compatible = "fsl,sec-v4.0",
  404. },
  405. {
  406. .compatible = "fsl,sec4.0",
  407. },
  408. {},
  409. };
  410. MODULE_DEVICE_TABLE(of, caam_match);
  411. struct caam_imx_data {
  412. const struct clk_bulk_data *clks;
  413. int num_clks;
  414. };
  415. static const struct clk_bulk_data caam_imx6_clks[] = {
  416. { .id = "ipg" },
  417. { .id = "mem" },
  418. { .id = "aclk" },
  419. { .id = "emi_slow" },
  420. };
  421. static const struct caam_imx_data caam_imx6_data = {
  422. .clks = caam_imx6_clks,
  423. .num_clks = ARRAY_SIZE(caam_imx6_clks),
  424. };
  425. static const struct clk_bulk_data caam_imx7_clks[] = {
  426. { .id = "ipg" },
  427. { .id = "aclk" },
  428. };
  429. static const struct caam_imx_data caam_imx7_data = {
  430. .clks = caam_imx7_clks,
  431. .num_clks = ARRAY_SIZE(caam_imx7_clks),
  432. };
  433. static const struct clk_bulk_data caam_imx6ul_clks[] = {
  434. { .id = "ipg" },
  435. { .id = "mem" },
  436. { .id = "aclk" },
  437. };
  438. static const struct caam_imx_data caam_imx6ul_data = {
  439. .clks = caam_imx6ul_clks,
  440. .num_clks = ARRAY_SIZE(caam_imx6ul_clks),
  441. };
  442. static const struct soc_device_attribute caam_imx_soc_table[] = {
  443. { .soc_id = "i.MX6UL", .data = &caam_imx6ul_data },
  444. { .soc_id = "i.MX6*", .data = &caam_imx6_data },
  445. { .soc_id = "i.MX7*", .data = &caam_imx7_data },
  446. { .soc_id = "i.MX8MQ", .data = &caam_imx7_data },
  447. { .family = "Freescale i.MX" },
  448. { /* sentinel */ }
  449. };
  450. static void disable_clocks(void *data)
  451. {
  452. struct caam_drv_private *ctrlpriv = data;
  453. clk_bulk_disable_unprepare(ctrlpriv->num_clks, ctrlpriv->clks);
  454. }
  455. static int init_clocks(struct device *dev, const struct caam_imx_data *data)
  456. {
  457. struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
  458. int ret;
  459. ctrlpriv->num_clks = data->num_clks;
  460. ctrlpriv->clks = devm_kmemdup(dev, data->clks,
  461. data->num_clks * sizeof(data->clks[0]),
  462. GFP_KERNEL);
  463. if (!ctrlpriv->clks)
  464. return -ENOMEM;
  465. ret = devm_clk_bulk_get(dev, ctrlpriv->num_clks, ctrlpriv->clks);
  466. if (ret) {
  467. dev_err(dev,
  468. "Failed to request all necessary clocks\n");
  469. return ret;
  470. }
  471. ret = clk_bulk_prepare_enable(ctrlpriv->num_clks, ctrlpriv->clks);
  472. if (ret) {
  473. dev_err(dev,
  474. "Failed to prepare/enable all necessary clocks\n");
  475. return ret;
  476. }
  477. return devm_add_action_or_reset(dev, disable_clocks, ctrlpriv);
  478. }
  479. /* Probe routine for CAAM top (controller) level */
  480. static int caam_probe(struct platform_device *pdev)
  481. {
  482. int ret, ring, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
  483. u64 caam_id;
  484. const struct soc_device_attribute *imx_soc_match;
  485. struct device *dev;
  486. struct device_node *nprop, *np;
  487. struct caam_ctrl __iomem *ctrl;
  488. struct caam_drv_private *ctrlpriv;
  489. #ifdef CONFIG_DEBUG_FS
  490. struct caam_perfmon *perfmon;
  491. #endif
  492. u32 scfgr, comp_params;
  493. u8 rng_vid;
  494. int pg_size;
  495. int BLOCK_OFFSET = 0;
  496. ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
  497. if (!ctrlpriv)
  498. return -ENOMEM;
  499. dev = &pdev->dev;
  500. dev_set_drvdata(dev, ctrlpriv);
  501. nprop = pdev->dev.of_node;
  502. imx_soc_match = soc_device_match(caam_imx_soc_table);
  503. caam_imx = (bool)imx_soc_match;
  504. if (imx_soc_match) {
  505. if (!imx_soc_match->data) {
  506. dev_err(dev, "No clock data provided for i.MX SoC");
  507. return -EINVAL;
  508. }
  509. ret = init_clocks(dev, imx_soc_match->data);
  510. if (ret)
  511. return ret;
  512. }
  513. /* Get configuration properties from device tree */
  514. /* First, get register page */
  515. ctrl = of_iomap(nprop, 0);
  516. if (!ctrl) {
  517. dev_err(dev, "caam: of_iomap() failed\n");
  518. return -ENOMEM;
  519. }
  520. caam_little_end = !(bool)(rd_reg32(&ctrl->perfmon.status) &
  521. (CSTA_PLEND | CSTA_ALT_PLEND));
  522. comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
  523. if (comp_params & CTPR_MS_PS && rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR)
  524. caam_ptr_sz = sizeof(u64);
  525. else
  526. caam_ptr_sz = sizeof(u32);
  527. caam_dpaa2 = !!(comp_params & CTPR_MS_DPAA2);
  528. ctrlpriv->qi_present = !!(comp_params & CTPR_MS_QI_MASK);
  529. #ifdef CONFIG_CAAM_QI
  530. /* If (DPAA 1.x) QI present, check whether dependencies are available */
  531. if (ctrlpriv->qi_present && !caam_dpaa2) {
  532. ret = qman_is_probed();
  533. if (!ret) {
  534. ret = -EPROBE_DEFER;
  535. goto iounmap_ctrl;
  536. } else if (ret < 0) {
  537. dev_err(dev, "failing probe due to qman probe error\n");
  538. ret = -ENODEV;
  539. goto iounmap_ctrl;
  540. }
  541. ret = qman_portals_probed();
  542. if (!ret) {
  543. ret = -EPROBE_DEFER;
  544. goto iounmap_ctrl;
  545. } else if (ret < 0) {
  546. dev_err(dev, "failing probe due to qman portals probe error\n");
  547. ret = -ENODEV;
  548. goto iounmap_ctrl;
  549. }
  550. }
  551. #endif
  552. /* Allocating the BLOCK_OFFSET based on the supported page size on
  553. * the platform
  554. */
  555. pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
  556. if (pg_size == 0)
  557. BLOCK_OFFSET = PG_SIZE_4K;
  558. else
  559. BLOCK_OFFSET = PG_SIZE_64K;
  560. ctrlpriv->ctrl = (struct caam_ctrl __iomem __force *)ctrl;
  561. ctrlpriv->assure = (struct caam_assurance __iomem __force *)
  562. ((__force uint8_t *)ctrl +
  563. BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
  564. );
  565. ctrlpriv->deco = (struct caam_deco __iomem __force *)
  566. ((__force uint8_t *)ctrl +
  567. BLOCK_OFFSET * DECO_BLOCK_NUMBER
  568. );
  569. /* Get the IRQ of the controller (for security violations only) */
  570. ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
  571. /*
  572. * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
  573. * long pointers in master configuration register.
  574. * In case of SoCs with Management Complex, MC f/w performs
  575. * the configuration.
  576. */
  577. np = of_find_compatible_node(NULL, NULL, "fsl,qoriq-mc");
  578. ctrlpriv->mc_en = !!np;
  579. of_node_put(np);
  580. if (!ctrlpriv->mc_en)
  581. clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK,
  582. MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF |
  583. MCFGR_WDENABLE | MCFGR_LARGE_BURST);
  584. handle_imx6_err005766(&ctrl->mcr);
  585. /*
  586. * Read the Compile Time paramters and SCFGR to determine
  587. * if Virtualization is enabled for this platform
  588. */
  589. scfgr = rd_reg32(&ctrl->scfgr);
  590. ctrlpriv->virt_en = 0;
  591. if (comp_params & CTPR_MS_VIRT_EN_INCL) {
  592. /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
  593. * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
  594. */
  595. if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
  596. (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
  597. (scfgr & SCFGR_VIRT_EN)))
  598. ctrlpriv->virt_en = 1;
  599. } else {
  600. /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
  601. if (comp_params & CTPR_MS_VIRT_EN_POR)
  602. ctrlpriv->virt_en = 1;
  603. }
  604. if (ctrlpriv->virt_en == 1)
  605. clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
  606. JRSTART_JR1_START | JRSTART_JR2_START |
  607. JRSTART_JR3_START);
  608. ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev));
  609. if (ret) {
  610. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
  611. goto iounmap_ctrl;
  612. }
  613. ctrlpriv->era = caam_get_era(ctrl);
  614. ctrlpriv->domain = iommu_get_domain_for_dev(dev);
  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. #endif
  625. /* Check to see if (DPAA 1.x) QI present. If so, enable */
  626. if (ctrlpriv->qi_present && !caam_dpaa2) {
  627. ctrlpriv->qi = (struct caam_queue_if __iomem __force *)
  628. ((__force uint8_t *)ctrl +
  629. BLOCK_OFFSET * QI_BLOCK_NUMBER
  630. );
  631. /* This is all that's required to physically enable QI */
  632. wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
  633. /* If QMAN driver is present, init CAAM-QI backend */
  634. #ifdef CONFIG_CAAM_QI
  635. ret = caam_qi_init(pdev);
  636. if (ret)
  637. dev_err(dev, "caam qi i/f init failed: %d\n", ret);
  638. #endif
  639. }
  640. ret = of_platform_populate(nprop, caam_match, NULL, dev);
  641. if (ret) {
  642. dev_err(dev, "JR platform devices creation error\n");
  643. goto shutdown_qi;
  644. }
  645. ring = 0;
  646. for_each_available_child_of_node(nprop, np)
  647. if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
  648. of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
  649. ctrlpriv->jr[ring] = (struct caam_job_ring __iomem __force *)
  650. ((__force uint8_t *)ctrl +
  651. (ring + JR_BLOCK_NUMBER) *
  652. BLOCK_OFFSET
  653. );
  654. ctrlpriv->total_jobrs++;
  655. ring++;
  656. }
  657. /* If no QI and no rings specified, quit and go home */
  658. if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
  659. dev_err(dev, "no queues configured, terminating\n");
  660. ret = -ENOMEM;
  661. goto caam_remove;
  662. }
  663. if (ctrlpriv->era < 10)
  664. rng_vid = (rd_reg32(&ctrl->perfmon.cha_id_ls) &
  665. CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT;
  666. else
  667. rng_vid = (rd_reg32(&ctrl->vreg.rng) & CHA_VER_VID_MASK) >>
  668. CHA_VER_VID_SHIFT;
  669. /*
  670. * If SEC has RNG version >= 4 and RNG state handle has not been
  671. * already instantiated, do RNG instantiation
  672. * In case of SoCs with Management Complex, RNG is managed by MC f/w.
  673. */
  674. if (!ctrlpriv->mc_en && rng_vid >= 4) {
  675. ctrlpriv->rng4_sh_init =
  676. rd_reg32(&ctrl->r4tst[0].rdsta);
  677. /*
  678. * If the secure keys (TDKEK, JDKEK, TDSK), were already
  679. * generated, signal this to the function that is instantiating
  680. * the state handles. An error would occur if RNG4 attempts
  681. * to regenerate these keys before the next POR.
  682. */
  683. gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
  684. ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
  685. do {
  686. int inst_handles =
  687. rd_reg32(&ctrl->r4tst[0].rdsta) &
  688. RDSTA_IFMASK;
  689. /*
  690. * If either SH were instantiated by somebody else
  691. * (e.g. u-boot) then it is assumed that the entropy
  692. * parameters are properly set and thus the function
  693. * setting these (kick_trng(...)) is skipped.
  694. * Also, if a handle was instantiated, do not change
  695. * the TRNG parameters.
  696. */
  697. if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
  698. dev_info(dev,
  699. "Entropy delay = %u\n",
  700. ent_delay);
  701. kick_trng(pdev, ent_delay);
  702. ent_delay += 400;
  703. }
  704. /*
  705. * if instantiate_rng(...) fails, the loop will rerun
  706. * and the kick_trng(...) function will modfiy the
  707. * upper and lower limits of the entropy sampling
  708. * interval, leading to a sucessful initialization of
  709. * the RNG.
  710. */
  711. ret = instantiate_rng(dev, inst_handles,
  712. gen_sk);
  713. if (ret == -EAGAIN)
  714. /*
  715. * if here, the loop will rerun,
  716. * so don't hog the CPU
  717. */
  718. cpu_relax();
  719. } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  720. if (ret) {
  721. dev_err(dev, "failed to instantiate RNG");
  722. goto caam_remove;
  723. }
  724. /*
  725. * Set handles init'ed by this module as the complement of the
  726. * already initialized ones
  727. */
  728. ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
  729. /* Enable RDB bit so that RNG works faster */
  730. clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
  731. }
  732. /* NOTE: RTIC detection ought to go here, around Si time */
  733. caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 |
  734. (u64)rd_reg32(&ctrl->perfmon.caam_id_ls);
  735. /* Report "alive" for developer to see */
  736. dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
  737. ctrlpriv->era);
  738. dev_info(dev, "job rings = %d, qi = %d\n",
  739. ctrlpriv->total_jobrs, ctrlpriv->qi_present);
  740. #ifdef CONFIG_DEBUG_FS
  741. debugfs_create_file("rq_dequeued", S_IRUSR | S_IRGRP | S_IROTH,
  742. ctrlpriv->ctl, &perfmon->req_dequeued,
  743. &caam_fops_u64_ro);
  744. debugfs_create_file("ob_rq_encrypted", S_IRUSR | S_IRGRP | S_IROTH,
  745. ctrlpriv->ctl, &perfmon->ob_enc_req,
  746. &caam_fops_u64_ro);
  747. debugfs_create_file("ib_rq_decrypted", S_IRUSR | S_IRGRP | S_IROTH,
  748. ctrlpriv->ctl, &perfmon->ib_dec_req,
  749. &caam_fops_u64_ro);
  750. debugfs_create_file("ob_bytes_encrypted", S_IRUSR | S_IRGRP | S_IROTH,
  751. ctrlpriv->ctl, &perfmon->ob_enc_bytes,
  752. &caam_fops_u64_ro);
  753. debugfs_create_file("ob_bytes_protected", S_IRUSR | S_IRGRP | S_IROTH,
  754. ctrlpriv->ctl, &perfmon->ob_prot_bytes,
  755. &caam_fops_u64_ro);
  756. debugfs_create_file("ib_bytes_decrypted", S_IRUSR | S_IRGRP | S_IROTH,
  757. ctrlpriv->ctl, &perfmon->ib_dec_bytes,
  758. &caam_fops_u64_ro);
  759. debugfs_create_file("ib_bytes_validated", S_IRUSR | S_IRGRP | S_IROTH,
  760. ctrlpriv->ctl, &perfmon->ib_valid_bytes,
  761. &caam_fops_u64_ro);
  762. /* Controller level - global status values */
  763. debugfs_create_file("fault_addr", S_IRUSR | S_IRGRP | S_IROTH,
  764. ctrlpriv->ctl, &perfmon->faultaddr,
  765. &caam_fops_u32_ro);
  766. debugfs_create_file("fault_detail", S_IRUSR | S_IRGRP | S_IROTH,
  767. ctrlpriv->ctl, &perfmon->faultdetail,
  768. &caam_fops_u32_ro);
  769. debugfs_create_file("fault_status", S_IRUSR | S_IRGRP | S_IROTH,
  770. ctrlpriv->ctl, &perfmon->status,
  771. &caam_fops_u32_ro);
  772. /* Internal covering keys (useful in non-secure mode only) */
  773. ctrlpriv->ctl_kek_wrap.data = (__force void *)&ctrlpriv->ctrl->kek[0];
  774. ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  775. debugfs_create_blob("kek", S_IRUSR | S_IRGRP | S_IROTH, ctrlpriv->ctl,
  776. &ctrlpriv->ctl_kek_wrap);
  777. ctrlpriv->ctl_tkek_wrap.data = (__force void *)&ctrlpriv->ctrl->tkek[0];
  778. ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  779. debugfs_create_blob("tkek", S_IRUSR | S_IRGRP | S_IROTH, ctrlpriv->ctl,
  780. &ctrlpriv->ctl_tkek_wrap);
  781. ctrlpriv->ctl_tdsk_wrap.data = (__force void *)&ctrlpriv->ctrl->tdsk[0];
  782. ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  783. debugfs_create_blob("tdsk", S_IRUSR | S_IRGRP | S_IROTH, ctrlpriv->ctl,
  784. &ctrlpriv->ctl_tdsk_wrap);
  785. #endif
  786. return 0;
  787. caam_remove:
  788. caam_remove(pdev);
  789. return ret;
  790. shutdown_qi:
  791. #ifdef CONFIG_CAAM_QI
  792. if (ctrlpriv->qi_init)
  793. caam_qi_shutdown(dev);
  794. #endif
  795. iounmap_ctrl:
  796. iounmap(ctrl);
  797. return ret;
  798. }
  799. static struct platform_driver caam_driver = {
  800. .driver = {
  801. .name = "caam",
  802. .of_match_table = caam_match,
  803. },
  804. .probe = caam_probe,
  805. .remove = caam_remove,
  806. };
  807. module_platform_driver(caam_driver);
  808. MODULE_LICENSE("GPL");
  809. MODULE_DESCRIPTION("FSL CAAM request backend");
  810. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");