ctrl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 <linux/fsl/mc.h>
  13. #include "compat.h"
  14. #include "debugfs.h"
  15. #include "regs.h"
  16. #include "intern.h"
  17. #include "jr.h"
  18. #include "desc_constr.h"
  19. #include "ctrl.h"
  20. bool caam_dpaa2;
  21. EXPORT_SYMBOL(caam_dpaa2);
  22. #ifdef CONFIG_CAAM_QI
  23. #include "qi.h"
  24. #endif
  25. /*
  26. * Descriptor to instantiate RNG State Handle 0 in normal mode and
  27. * load the JDKEK, TDKEK and TDSK registers
  28. */
  29. static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
  30. {
  31. u32 *jump_cmd, op_flags;
  32. init_job_desc(desc, 0);
  33. op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  34. (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT |
  35. OP_ALG_PR_ON;
  36. /* INIT RNG in non-test mode */
  37. append_operation(desc, op_flags);
  38. if (!handle && do_sk) {
  39. /*
  40. * For SH0, Secure Keys must be generated as well
  41. */
  42. /* wait for done */
  43. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
  44. set_jump_tgt_here(desc, jump_cmd);
  45. /*
  46. * load 1 to clear written reg:
  47. * resets the done interrupt and returns the RNG to idle.
  48. */
  49. append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
  50. /* Initialize State Handle */
  51. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  52. OP_ALG_AAI_RNG4_SK);
  53. }
  54. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  55. }
  56. /* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
  57. static void build_deinstantiation_desc(u32 *desc, int handle)
  58. {
  59. init_job_desc(desc, 0);
  60. /* Uninstantiate State Handle 0 */
  61. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  62. (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
  63. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  64. }
  65. /*
  66. * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
  67. * the software (no JR/QI used).
  68. * @ctrldev - pointer to device
  69. * @status - descriptor status, after being run
  70. *
  71. * Return: - 0 if no error occurred
  72. * - -ENODEV if the DECO couldn't be acquired
  73. * - -EAGAIN if an error occurred while executing the descriptor
  74. */
  75. static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
  76. u32 *status)
  77. {
  78. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  79. struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
  80. struct caam_deco __iomem *deco = ctrlpriv->deco;
  81. unsigned int timeout = 100000;
  82. u32 deco_dbg_reg, deco_state, flags;
  83. int i;
  84. if (ctrlpriv->virt_en == 1 ||
  85. /*
  86. * Apparently on i.MX8M{Q,M,N,P} it doesn't matter if virt_en == 1
  87. * and the following steps should be performed regardless
  88. */
  89. of_machine_is_compatible("fsl,imx8mq") ||
  90. of_machine_is_compatible("fsl,imx8mm") ||
  91. of_machine_is_compatible("fsl,imx8mn") ||
  92. of_machine_is_compatible("fsl,imx8mp")) {
  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. if (ctrlpriv->era < 10)
  123. deco_state = (deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) >>
  124. DESC_DBG_DECO_STAT_SHIFT;
  125. else
  126. deco_state = (rd_reg32(&deco->dbg_exec) &
  127. DESC_DER_DECO_STAT_MASK) >>
  128. DESC_DER_DECO_STAT_SHIFT;
  129. /*
  130. * If an error occurred in the descriptor, then
  131. * the DECO status field will be set to 0x0D
  132. */
  133. if (deco_state == DECO_STAT_HOST_ERR)
  134. break;
  135. cpu_relax();
  136. } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
  137. *status = rd_reg32(&deco->op_status_hi) &
  138. DECO_OP_STATUS_HI_ERR_MASK;
  139. if (ctrlpriv->virt_en == 1)
  140. clrsetbits_32(&ctrl->deco_rsr, DECORSR_JR0, 0);
  141. /* Mark the DECO as free */
  142. clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
  143. if (!timeout)
  144. return -EAGAIN;
  145. return 0;
  146. }
  147. /*
  148. * deinstantiate_rng - builds and executes a descriptor on DECO0,
  149. * which deinitializes the RNG block.
  150. * @ctrldev - pointer to device
  151. * @state_handle_mask - bitmask containing the instantiation status
  152. * for the RNG4 state handles which exist in
  153. * the RNG4 block: 1 if it's been instantiated
  154. *
  155. * Return: - 0 if no error occurred
  156. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  157. * - -ENODEV if DECO0 couldn't be acquired
  158. * - -EAGAIN if an error occurred when executing the descriptor
  159. */
  160. static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
  161. {
  162. u32 *desc, status;
  163. int sh_idx, ret = 0;
  164. desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL | GFP_DMA);
  165. if (!desc)
  166. return -ENOMEM;
  167. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  168. /*
  169. * If the corresponding bit is set, then it means the state
  170. * handle was initialized by us, and thus it needs to be
  171. * deinitialized as well
  172. */
  173. if ((1 << sh_idx) & state_handle_mask) {
  174. /*
  175. * Create the descriptor for deinstantating this state
  176. * handle
  177. */
  178. build_deinstantiation_desc(desc, sh_idx);
  179. /* Try to run it through DECO0 */
  180. ret = run_descriptor_deco0(ctrldev, desc, &status);
  181. if (ret ||
  182. (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
  183. dev_err(ctrldev,
  184. "Failed to deinstantiate RNG4 SH%d\n",
  185. sh_idx);
  186. break;
  187. }
  188. dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
  189. }
  190. }
  191. kfree(desc);
  192. return ret;
  193. }
  194. static void devm_deinstantiate_rng(void *data)
  195. {
  196. struct device *ctrldev = data;
  197. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  198. /*
  199. * De-initialize RNG state handles initialized by this driver.
  200. * In case of SoCs with Management Complex, RNG is managed by MC f/w.
  201. */
  202. if (ctrlpriv->rng4_sh_init)
  203. deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
  204. }
  205. /*
  206. * instantiate_rng - builds and executes a descriptor on DECO0,
  207. * which initializes the RNG block.
  208. * @ctrldev - pointer to device
  209. * @state_handle_mask - bitmask containing the instantiation status
  210. * for the RNG4 state handles which exist in
  211. * the RNG4 block: 1 if it's been instantiated
  212. * by an external entry, 0 otherwise.
  213. * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
  214. * Caution: this can be done only once; if the keys need to be
  215. * regenerated, a POR is required
  216. *
  217. * Return: - 0 if no error occurred
  218. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  219. * - -ENODEV if DECO0 couldn't be acquired
  220. * - -EAGAIN if an error occurred when executing the descriptor
  221. * f.i. there was a RNG hardware error due to not "good enough"
  222. * entropy being acquired.
  223. */
  224. static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
  225. int gen_sk)
  226. {
  227. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  228. struct caam_ctrl __iomem *ctrl;
  229. u32 *desc, status = 0, rdsta_val;
  230. int ret = 0, sh_idx;
  231. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  232. desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL | GFP_DMA);
  233. if (!desc)
  234. return -ENOMEM;
  235. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  236. const u32 rdsta_if = RDSTA_IF0 << sh_idx;
  237. const u32 rdsta_pr = RDSTA_PR0 << sh_idx;
  238. const u32 rdsta_mask = rdsta_if | rdsta_pr;
  239. /* Clear the contents before using the descriptor */
  240. memset(desc, 0x00, CAAM_CMD_SZ * 7);
  241. /*
  242. * If the corresponding bit is set, this state handle
  243. * was initialized by somebody else, so it's left alone.
  244. */
  245. if (rdsta_if & state_handle_mask) {
  246. if (rdsta_pr & state_handle_mask)
  247. continue;
  248. dev_info(ctrldev,
  249. "RNG4 SH%d was previously instantiated without prediction resistance. Tearing it down\n",
  250. sh_idx);
  251. ret = deinstantiate_rng(ctrldev, rdsta_if);
  252. if (ret)
  253. break;
  254. }
  255. /* Create the descriptor for instantiating RNG State Handle */
  256. build_instantiation_desc(desc, sh_idx, gen_sk);
  257. /* Try to run it through DECO0 */
  258. ret = run_descriptor_deco0(ctrldev, desc, &status);
  259. /*
  260. * If ret is not 0, or descriptor status is not 0, then
  261. * something went wrong. No need to try the next state
  262. * handle (if available), bail out here.
  263. * Also, if for some reason, the State Handle didn't get
  264. * instantiated although the descriptor has finished
  265. * without any error (HW optimizations for later
  266. * CAAM eras), then try again.
  267. */
  268. if (ret)
  269. break;
  270. rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_MASK;
  271. if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) ||
  272. (rdsta_val & rdsta_mask) != rdsta_mask) {
  273. ret = -EAGAIN;
  274. break;
  275. }
  276. dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
  277. }
  278. kfree(desc);
  279. if (ret)
  280. return ret;
  281. return devm_add_action_or_reset(ctrldev, devm_deinstantiate_rng, ctrldev);
  282. }
  283. /*
  284. * kick_trng - sets the various parameters for enabling the initialization
  285. * of the RNG4 block in CAAM
  286. * @pdev - pointer to the platform device
  287. * @ent_delay - Defines the length (in system clocks) of each entropy sample.
  288. */
  289. static void kick_trng(struct platform_device *pdev, int ent_delay)
  290. {
  291. struct device *ctrldev = &pdev->dev;
  292. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  293. struct caam_ctrl __iomem *ctrl;
  294. struct rng4tst __iomem *r4tst;
  295. u32 val;
  296. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  297. r4tst = &ctrl->r4tst[0];
  298. /*
  299. * Setting both RTMCTL:PRGM and RTMCTL:TRNG_ACC causes TRNG to
  300. * properly invalidate the entropy in the entropy register and
  301. * force re-generation.
  302. */
  303. clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM | RTMCTL_ACC);
  304. /*
  305. * Performance-wise, it does not make sense to
  306. * set the delay to a value that is lower
  307. * than the last one that worked (i.e. the state handles
  308. * were instantiated properly. Thus, instead of wasting
  309. * time trying to set the values controlling the sample
  310. * frequency, the function simply returns.
  311. */
  312. val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
  313. >> RTSDCTL_ENT_DLY_SHIFT;
  314. if (ent_delay <= val)
  315. goto start_rng;
  316. val = rd_reg32(&r4tst->rtsdctl);
  317. val = (val & ~RTSDCTL_ENT_DLY_MASK) |
  318. (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
  319. wr_reg32(&r4tst->rtsdctl, val);
  320. /* min. freq. count, equal to 1/4 of the entropy sample length */
  321. wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
  322. /* disable maximum frequency count */
  323. wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE);
  324. /* read the control register */
  325. val = rd_reg32(&r4tst->rtmctl);
  326. start_rng:
  327. /*
  328. * select raw sampling in both entropy shifter
  329. * and statistical checker; ; put RNG4 into run mode
  330. */
  331. clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM | RTMCTL_ACC,
  332. RTMCTL_SAMP_MODE_RAW_ES_SC);
  333. }
  334. static int caam_get_era_from_hw(struct caam_ctrl __iomem *ctrl)
  335. {
  336. static const struct {
  337. u16 ip_id;
  338. u8 maj_rev;
  339. u8 era;
  340. } id[] = {
  341. {0x0A10, 1, 1},
  342. {0x0A10, 2, 2},
  343. {0x0A12, 1, 3},
  344. {0x0A14, 1, 3},
  345. {0x0A14, 2, 4},
  346. {0x0A16, 1, 4},
  347. {0x0A10, 3, 4},
  348. {0x0A11, 1, 4},
  349. {0x0A18, 1, 4},
  350. {0x0A11, 2, 5},
  351. {0x0A12, 2, 5},
  352. {0x0A13, 1, 5},
  353. {0x0A1C, 1, 5}
  354. };
  355. u32 ccbvid, id_ms;
  356. u8 maj_rev, era;
  357. u16 ip_id;
  358. int i;
  359. ccbvid = rd_reg32(&ctrl->perfmon.ccb_id);
  360. era = (ccbvid & CCBVID_ERA_MASK) >> CCBVID_ERA_SHIFT;
  361. if (era) /* This is '0' prior to CAAM ERA-6 */
  362. return era;
  363. id_ms = rd_reg32(&ctrl->perfmon.caam_id_ms);
  364. ip_id = (id_ms & SECVID_MS_IPID_MASK) >> SECVID_MS_IPID_SHIFT;
  365. maj_rev = (id_ms & SECVID_MS_MAJ_REV_MASK) >> SECVID_MS_MAJ_REV_SHIFT;
  366. for (i = 0; i < ARRAY_SIZE(id); i++)
  367. if (id[i].ip_id == ip_id && id[i].maj_rev == maj_rev)
  368. return id[i].era;
  369. return -ENOTSUPP;
  370. }
  371. /**
  372. * caam_get_era() - Return the ERA of the SEC on SoC, based
  373. * on "sec-era" optional property in the DTS. This property is updated
  374. * by u-boot.
  375. * In case this property is not passed an attempt to retrieve the CAAM
  376. * era via register reads will be made.
  377. *
  378. * @ctrl: controller region
  379. */
  380. static int caam_get_era(struct caam_ctrl __iomem *ctrl)
  381. {
  382. struct device_node *caam_node;
  383. int ret;
  384. u32 prop;
  385. caam_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  386. ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop);
  387. of_node_put(caam_node);
  388. if (!ret)
  389. return prop;
  390. else
  391. return caam_get_era_from_hw(ctrl);
  392. }
  393. /*
  394. * ERRATA: imx6 devices (imx6D, imx6Q, imx6DL, imx6S, imx6DP and imx6QP)
  395. * have an issue wherein AXI bus transactions may not occur in the correct
  396. * order. This isn't a problem running single descriptors, but can be if
  397. * running multiple concurrent descriptors. Reworking the driver to throttle
  398. * to single requests is impractical, thus the workaround is to limit the AXI
  399. * pipeline to a depth of 1 (from it's default of 4) to preclude this situation
  400. * from occurring.
  401. */
  402. static void handle_imx6_err005766(u32 __iomem *mcr)
  403. {
  404. if (of_machine_is_compatible("fsl,imx6q") ||
  405. of_machine_is_compatible("fsl,imx6dl") ||
  406. of_machine_is_compatible("fsl,imx6qp"))
  407. clrsetbits_32(mcr, MCFGR_AXIPIPE_MASK,
  408. 1 << MCFGR_AXIPIPE_SHIFT);
  409. }
  410. static const struct of_device_id caam_match[] = {
  411. {
  412. .compatible = "fsl,sec-v4.0",
  413. },
  414. {
  415. .compatible = "fsl,sec4.0",
  416. },
  417. {},
  418. };
  419. MODULE_DEVICE_TABLE(of, caam_match);
  420. struct caam_imx_data {
  421. const struct clk_bulk_data *clks;
  422. int num_clks;
  423. };
  424. static const struct clk_bulk_data caam_imx6_clks[] = {
  425. { .id = "ipg" },
  426. { .id = "mem" },
  427. { .id = "aclk" },
  428. { .id = "emi_slow" },
  429. };
  430. static const struct caam_imx_data caam_imx6_data = {
  431. .clks = caam_imx6_clks,
  432. .num_clks = ARRAY_SIZE(caam_imx6_clks),
  433. };
  434. static const struct clk_bulk_data caam_imx7_clks[] = {
  435. { .id = "ipg" },
  436. { .id = "aclk" },
  437. };
  438. static const struct caam_imx_data caam_imx7_data = {
  439. .clks = caam_imx7_clks,
  440. .num_clks = ARRAY_SIZE(caam_imx7_clks),
  441. };
  442. static const struct clk_bulk_data caam_imx6ul_clks[] = {
  443. { .id = "ipg" },
  444. { .id = "mem" },
  445. { .id = "aclk" },
  446. };
  447. static const struct caam_imx_data caam_imx6ul_data = {
  448. .clks = caam_imx6ul_clks,
  449. .num_clks = ARRAY_SIZE(caam_imx6ul_clks),
  450. };
  451. static const struct clk_bulk_data caam_vf610_clks[] = {
  452. { .id = "ipg" },
  453. };
  454. static const struct caam_imx_data caam_vf610_data = {
  455. .clks = caam_vf610_clks,
  456. .num_clks = ARRAY_SIZE(caam_vf610_clks),
  457. };
  458. static const struct soc_device_attribute caam_imx_soc_table[] = {
  459. { .soc_id = "i.MX6UL", .data = &caam_imx6ul_data },
  460. { .soc_id = "i.MX6*", .data = &caam_imx6_data },
  461. { .soc_id = "i.MX7*", .data = &caam_imx7_data },
  462. { .soc_id = "i.MX8M*", .data = &caam_imx7_data },
  463. { .soc_id = "VF*", .data = &caam_vf610_data },
  464. { .family = "Freescale i.MX" },
  465. { /* sentinel */ }
  466. };
  467. static void disable_clocks(void *data)
  468. {
  469. struct caam_drv_private *ctrlpriv = data;
  470. clk_bulk_disable_unprepare(ctrlpriv->num_clks, ctrlpriv->clks);
  471. }
  472. static int init_clocks(struct device *dev, const struct caam_imx_data *data)
  473. {
  474. struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
  475. int ret;
  476. ctrlpriv->num_clks = data->num_clks;
  477. ctrlpriv->clks = devm_kmemdup(dev, data->clks,
  478. data->num_clks * sizeof(data->clks[0]),
  479. GFP_KERNEL);
  480. if (!ctrlpriv->clks)
  481. return -ENOMEM;
  482. ret = devm_clk_bulk_get(dev, ctrlpriv->num_clks, ctrlpriv->clks);
  483. if (ret) {
  484. dev_err(dev,
  485. "Failed to request all necessary clocks\n");
  486. return ret;
  487. }
  488. ret = clk_bulk_prepare_enable(ctrlpriv->num_clks, ctrlpriv->clks);
  489. if (ret) {
  490. dev_err(dev,
  491. "Failed to prepare/enable all necessary clocks\n");
  492. return ret;
  493. }
  494. return devm_add_action_or_reset(dev, disable_clocks, ctrlpriv);
  495. }
  496. static void caam_remove_debugfs(void *root)
  497. {
  498. debugfs_remove_recursive(root);
  499. }
  500. #ifdef CONFIG_FSL_MC_BUS
  501. static bool check_version(struct fsl_mc_version *mc_version, u32 major,
  502. u32 minor, u32 revision)
  503. {
  504. if (mc_version->major > major)
  505. return true;
  506. if (mc_version->major == major) {
  507. if (mc_version->minor > minor)
  508. return true;
  509. if (mc_version->minor == minor &&
  510. mc_version->revision > revision)
  511. return true;
  512. }
  513. return false;
  514. }
  515. #endif
  516. static bool needs_entropy_delay_adjustment(void)
  517. {
  518. if (of_machine_is_compatible("fsl,imx6sx"))
  519. return true;
  520. return false;
  521. }
  522. /* Probe routine for CAAM top (controller) level */
  523. static int caam_probe(struct platform_device *pdev)
  524. {
  525. int ret, ring, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
  526. u64 caam_id;
  527. const struct soc_device_attribute *imx_soc_match;
  528. struct device *dev;
  529. struct device_node *nprop, *np;
  530. struct caam_ctrl __iomem *ctrl;
  531. struct caam_drv_private *ctrlpriv;
  532. struct dentry *dfs_root;
  533. u32 scfgr, comp_params;
  534. u8 rng_vid;
  535. int pg_size;
  536. int BLOCK_OFFSET = 0;
  537. bool pr_support = false;
  538. ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
  539. if (!ctrlpriv)
  540. return -ENOMEM;
  541. dev = &pdev->dev;
  542. dev_set_drvdata(dev, ctrlpriv);
  543. nprop = pdev->dev.of_node;
  544. imx_soc_match = soc_device_match(caam_imx_soc_table);
  545. caam_imx = (bool)imx_soc_match;
  546. if (imx_soc_match) {
  547. if (!imx_soc_match->data) {
  548. dev_err(dev, "No clock data provided for i.MX SoC");
  549. return -EINVAL;
  550. }
  551. ret = init_clocks(dev, imx_soc_match->data);
  552. if (ret)
  553. return ret;
  554. }
  555. /* Get configuration properties from device tree */
  556. /* First, get register page */
  557. ctrl = devm_of_iomap(dev, nprop, 0, NULL);
  558. ret = PTR_ERR_OR_ZERO(ctrl);
  559. if (ret) {
  560. dev_err(dev, "caam: of_iomap() failed\n");
  561. return ret;
  562. }
  563. caam_little_end = !(bool)(rd_reg32(&ctrl->perfmon.status) &
  564. (CSTA_PLEND | CSTA_ALT_PLEND));
  565. comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
  566. if (comp_params & CTPR_MS_PS && rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR)
  567. caam_ptr_sz = sizeof(u64);
  568. else
  569. caam_ptr_sz = sizeof(u32);
  570. caam_dpaa2 = !!(comp_params & CTPR_MS_DPAA2);
  571. ctrlpriv->qi_present = !!(comp_params & CTPR_MS_QI_MASK);
  572. #ifdef CONFIG_CAAM_QI
  573. /* If (DPAA 1.x) QI present, check whether dependencies are available */
  574. if (ctrlpriv->qi_present && !caam_dpaa2) {
  575. ret = qman_is_probed();
  576. if (!ret) {
  577. return -EPROBE_DEFER;
  578. } else if (ret < 0) {
  579. dev_err(dev, "failing probe due to qman probe error\n");
  580. return -ENODEV;
  581. }
  582. ret = qman_portals_probed();
  583. if (!ret) {
  584. return -EPROBE_DEFER;
  585. } else if (ret < 0) {
  586. dev_err(dev, "failing probe due to qman portals probe error\n");
  587. return -ENODEV;
  588. }
  589. }
  590. #endif
  591. /* Allocating the BLOCK_OFFSET based on the supported page size on
  592. * the platform
  593. */
  594. pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
  595. if (pg_size == 0)
  596. BLOCK_OFFSET = PG_SIZE_4K;
  597. else
  598. BLOCK_OFFSET = PG_SIZE_64K;
  599. ctrlpriv->ctrl = (struct caam_ctrl __iomem __force *)ctrl;
  600. ctrlpriv->assure = (struct caam_assurance __iomem __force *)
  601. ((__force uint8_t *)ctrl +
  602. BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
  603. );
  604. ctrlpriv->deco = (struct caam_deco __iomem __force *)
  605. ((__force uint8_t *)ctrl +
  606. BLOCK_OFFSET * DECO_BLOCK_NUMBER
  607. );
  608. /* Get the IRQ of the controller (for security violations only) */
  609. ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
  610. np = of_find_compatible_node(NULL, NULL, "fsl,qoriq-mc");
  611. ctrlpriv->mc_en = !!np;
  612. of_node_put(np);
  613. #ifdef CONFIG_FSL_MC_BUS
  614. if (ctrlpriv->mc_en) {
  615. struct fsl_mc_version *mc_version;
  616. mc_version = fsl_mc_get_version();
  617. if (mc_version)
  618. pr_support = check_version(mc_version, 10, 20, 0);
  619. else
  620. return -EPROBE_DEFER;
  621. }
  622. #endif
  623. /*
  624. * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
  625. * long pointers in master configuration register.
  626. * In case of SoCs with Management Complex, MC f/w performs
  627. * the configuration.
  628. */
  629. if (!ctrlpriv->mc_en)
  630. clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK,
  631. MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF |
  632. MCFGR_WDENABLE | MCFGR_LARGE_BURST);
  633. handle_imx6_err005766(&ctrl->mcr);
  634. /*
  635. * Read the Compile Time parameters and SCFGR to determine
  636. * if virtualization is enabled for this platform
  637. */
  638. scfgr = rd_reg32(&ctrl->scfgr);
  639. ctrlpriv->virt_en = 0;
  640. if (comp_params & CTPR_MS_VIRT_EN_INCL) {
  641. /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
  642. * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
  643. */
  644. if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
  645. (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
  646. (scfgr & SCFGR_VIRT_EN)))
  647. ctrlpriv->virt_en = 1;
  648. } else {
  649. /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
  650. if (comp_params & CTPR_MS_VIRT_EN_POR)
  651. ctrlpriv->virt_en = 1;
  652. }
  653. if (ctrlpriv->virt_en == 1)
  654. clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
  655. JRSTART_JR1_START | JRSTART_JR2_START |
  656. JRSTART_JR3_START);
  657. ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev));
  658. if (ret) {
  659. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
  660. return ret;
  661. }
  662. ctrlpriv->era = caam_get_era(ctrl);
  663. ctrlpriv->domain = iommu_get_domain_for_dev(dev);
  664. dfs_root = debugfs_create_dir(dev_name(dev), NULL);
  665. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  666. ret = devm_add_action_or_reset(dev, caam_remove_debugfs,
  667. dfs_root);
  668. if (ret)
  669. return ret;
  670. }
  671. caam_debugfs_init(ctrlpriv, dfs_root);
  672. /* Check to see if (DPAA 1.x) QI present. If so, enable */
  673. if (ctrlpriv->qi_present && !caam_dpaa2) {
  674. ctrlpriv->qi = (struct caam_queue_if __iomem __force *)
  675. ((__force uint8_t *)ctrl +
  676. BLOCK_OFFSET * QI_BLOCK_NUMBER
  677. );
  678. /* This is all that's required to physically enable QI */
  679. wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
  680. /* If QMAN driver is present, init CAAM-QI backend */
  681. #ifdef CONFIG_CAAM_QI
  682. ret = caam_qi_init(pdev);
  683. if (ret)
  684. dev_err(dev, "caam qi i/f init failed: %d\n", ret);
  685. #endif
  686. }
  687. ring = 0;
  688. for_each_available_child_of_node(nprop, np)
  689. if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
  690. of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
  691. ctrlpriv->jr[ring] = (struct caam_job_ring __iomem __force *)
  692. ((__force uint8_t *)ctrl +
  693. (ring + JR_BLOCK_NUMBER) *
  694. BLOCK_OFFSET
  695. );
  696. ctrlpriv->total_jobrs++;
  697. ring++;
  698. }
  699. /* If no QI and no rings specified, quit and go home */
  700. if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
  701. dev_err(dev, "no queues configured, terminating\n");
  702. return -ENOMEM;
  703. }
  704. comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ls);
  705. ctrlpriv->blob_present = !!(comp_params & CTPR_LS_BLOB);
  706. /*
  707. * Some SoCs like the LS1028A (non-E) indicate CTPR_LS_BLOB support,
  708. * but fail when actually using it due to missing AES support, so
  709. * check both here.
  710. */
  711. if (ctrlpriv->era < 10) {
  712. rng_vid = (rd_reg32(&ctrl->perfmon.cha_id_ls) &
  713. CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT;
  714. ctrlpriv->blob_present = ctrlpriv->blob_present &&
  715. (rd_reg32(&ctrl->perfmon.cha_num_ls) & CHA_ID_LS_AES_MASK);
  716. } else {
  717. rng_vid = (rd_reg32(&ctrl->vreg.rng) & CHA_VER_VID_MASK) >>
  718. CHA_VER_VID_SHIFT;
  719. ctrlpriv->blob_present = ctrlpriv->blob_present &&
  720. (rd_reg32(&ctrl->vreg.aesa) & CHA_VER_MISC_AES_NUM_MASK);
  721. }
  722. /*
  723. * If SEC has RNG version >= 4 and RNG state handle has not been
  724. * already instantiated, do RNG instantiation
  725. * In case of SoCs with Management Complex, RNG is managed by MC f/w.
  726. */
  727. if (!(ctrlpriv->mc_en && pr_support) && rng_vid >= 4) {
  728. ctrlpriv->rng4_sh_init =
  729. rd_reg32(&ctrl->r4tst[0].rdsta);
  730. /*
  731. * If the secure keys (TDKEK, JDKEK, TDSK), were already
  732. * generated, signal this to the function that is instantiating
  733. * the state handles. An error would occur if RNG4 attempts
  734. * to regenerate these keys before the next POR.
  735. */
  736. gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
  737. ctrlpriv->rng4_sh_init &= RDSTA_MASK;
  738. do {
  739. int inst_handles =
  740. rd_reg32(&ctrl->r4tst[0].rdsta) &
  741. RDSTA_MASK;
  742. /*
  743. * If either SH were instantiated by somebody else
  744. * (e.g. u-boot) then it is assumed that the entropy
  745. * parameters are properly set and thus the function
  746. * setting these (kick_trng(...)) is skipped.
  747. * Also, if a handle was instantiated, do not change
  748. * the TRNG parameters.
  749. */
  750. if (needs_entropy_delay_adjustment())
  751. ent_delay = 12000;
  752. if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
  753. dev_info(dev,
  754. "Entropy delay = %u\n",
  755. ent_delay);
  756. kick_trng(pdev, ent_delay);
  757. ent_delay += 400;
  758. }
  759. /*
  760. * if instantiate_rng(...) fails, the loop will rerun
  761. * and the kick_trng(...) function will modify the
  762. * upper and lower limits of the entropy sampling
  763. * interval, leading to a successful initialization of
  764. * the RNG.
  765. */
  766. ret = instantiate_rng(dev, inst_handles,
  767. gen_sk);
  768. /*
  769. * Entropy delay is determined via TRNG characterization.
  770. * TRNG characterization is run across different voltages
  771. * and temperatures.
  772. * If worst case value for ent_dly is identified,
  773. * the loop can be skipped for that platform.
  774. */
  775. if (needs_entropy_delay_adjustment())
  776. break;
  777. if (ret == -EAGAIN)
  778. /*
  779. * if here, the loop will rerun,
  780. * so don't hog the CPU
  781. */
  782. cpu_relax();
  783. } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  784. if (ret) {
  785. dev_err(dev, "failed to instantiate RNG");
  786. return ret;
  787. }
  788. /*
  789. * Set handles initialized by this module as the complement of
  790. * the already initialized ones
  791. */
  792. ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_MASK;
  793. /* Enable RDB bit so that RNG works faster */
  794. clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
  795. }
  796. /* NOTE: RTIC detection ought to go here, around Si time */
  797. caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 |
  798. (u64)rd_reg32(&ctrl->perfmon.caam_id_ls);
  799. /* Report "alive" for developer to see */
  800. dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
  801. ctrlpriv->era);
  802. dev_info(dev, "job rings = %d, qi = %d\n",
  803. ctrlpriv->total_jobrs, ctrlpriv->qi_present);
  804. ret = devm_of_platform_populate(dev);
  805. if (ret)
  806. dev_err(dev, "JR platform devices creation error\n");
  807. return ret;
  808. }
  809. static struct platform_driver caam_driver = {
  810. .driver = {
  811. .name = "caam",
  812. .of_match_table = caam_match,
  813. },
  814. .probe = caam_probe,
  815. };
  816. module_platform_driver(caam_driver);
  817. MODULE_LICENSE("GPL");
  818. MODULE_DESCRIPTION("FSL CAAM request backend");
  819. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");