ctrl.c 24 KB

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