cc_driver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2018 ARM Limited or its affiliates. */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/crypto.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/types.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/of.h>
  13. #include <linux/clk.h>
  14. #include <linux/of_address.h>
  15. #include "cc_driver.h"
  16. #include "cc_request_mgr.h"
  17. #include "cc_buffer_mgr.h"
  18. #include "cc_debugfs.h"
  19. #include "cc_cipher.h"
  20. #include "cc_aead.h"
  21. #include "cc_hash.h"
  22. #include "cc_ivgen.h"
  23. #include "cc_sram_mgr.h"
  24. #include "cc_pm.h"
  25. #include "cc_fips.h"
  26. bool cc_dump_desc;
  27. module_param_named(dump_desc, cc_dump_desc, bool, 0600);
  28. MODULE_PARM_DESC(cc_dump_desc, "Dump descriptors to kernel log as debugging aid");
  29. bool cc_dump_bytes;
  30. module_param_named(dump_bytes, cc_dump_bytes, bool, 0600);
  31. MODULE_PARM_DESC(cc_dump_bytes, "Dump buffers to kernel log as debugging aid");
  32. struct cc_hw_data {
  33. char *name;
  34. enum cc_hw_rev rev;
  35. u32 sig;
  36. };
  37. /* Hardware revisions defs. */
  38. static const struct cc_hw_data cc712_hw = {
  39. .name = "712", .rev = CC_HW_REV_712, .sig = 0xDCC71200U
  40. };
  41. static const struct cc_hw_data cc710_hw = {
  42. .name = "710", .rev = CC_HW_REV_710, .sig = 0xDCC63200U
  43. };
  44. static const struct cc_hw_data cc630p_hw = {
  45. .name = "630P", .rev = CC_HW_REV_630, .sig = 0xDCC63000U
  46. };
  47. static const struct of_device_id arm_ccree_dev_of_match[] = {
  48. { .compatible = "arm,cryptocell-712-ree", .data = &cc712_hw },
  49. { .compatible = "arm,cryptocell-710-ree", .data = &cc710_hw },
  50. { .compatible = "arm,cryptocell-630p-ree", .data = &cc630p_hw },
  51. {}
  52. };
  53. MODULE_DEVICE_TABLE(of, arm_ccree_dev_of_match);
  54. void __dump_byte_array(const char *name, const u8 *buf, size_t len)
  55. {
  56. char prefix[64];
  57. if (!buf)
  58. return;
  59. snprintf(prefix, sizeof(prefix), "%s[%zu]: ", name, len);
  60. print_hex_dump(KERN_DEBUG, prefix, DUMP_PREFIX_ADDRESS, 16, 1, buf,
  61. len, false);
  62. }
  63. static irqreturn_t cc_isr(int irq, void *dev_id)
  64. {
  65. struct cc_drvdata *drvdata = (struct cc_drvdata *)dev_id;
  66. struct device *dev = drvdata_to_dev(drvdata);
  67. u32 irr;
  68. u32 imr;
  69. /* STAT_OP_TYPE_GENERIC STAT_PHASE_0: Interrupt */
  70. /* read the interrupt status */
  71. irr = cc_ioread(drvdata, CC_REG(HOST_IRR));
  72. dev_dbg(dev, "Got IRR=0x%08X\n", irr);
  73. if (irr == 0) { /* Probably shared interrupt line */
  74. dev_err(dev, "Got interrupt with empty IRR\n");
  75. return IRQ_NONE;
  76. }
  77. imr = cc_ioread(drvdata, CC_REG(HOST_IMR));
  78. /* clear interrupt - must be before processing events */
  79. cc_iowrite(drvdata, CC_REG(HOST_ICR), irr);
  80. drvdata->irq = irr;
  81. /* Completion interrupt - most probable */
  82. if (irr & CC_COMP_IRQ_MASK) {
  83. /* Mask AXI completion interrupt - will be unmasked in
  84. * Deferred service handler
  85. */
  86. cc_iowrite(drvdata, CC_REG(HOST_IMR), imr | CC_COMP_IRQ_MASK);
  87. irr &= ~CC_COMP_IRQ_MASK;
  88. complete_request(drvdata);
  89. }
  90. #ifdef CONFIG_CRYPTO_FIPS
  91. /* TEE FIPS interrupt */
  92. if (irr & CC_GPR0_IRQ_MASK) {
  93. /* Mask interrupt - will be unmasked in Deferred service
  94. * handler
  95. */
  96. cc_iowrite(drvdata, CC_REG(HOST_IMR), imr | CC_GPR0_IRQ_MASK);
  97. irr &= ~CC_GPR0_IRQ_MASK;
  98. fips_handler(drvdata);
  99. }
  100. #endif
  101. /* AXI error interrupt */
  102. if (irr & CC_AXI_ERR_IRQ_MASK) {
  103. u32 axi_err;
  104. /* Read the AXI error ID */
  105. axi_err = cc_ioread(drvdata, CC_REG(AXIM_MON_ERR));
  106. dev_dbg(dev, "AXI completion error: axim_mon_err=0x%08X\n",
  107. axi_err);
  108. irr &= ~CC_AXI_ERR_IRQ_MASK;
  109. }
  110. if (irr) {
  111. dev_dbg_ratelimited(dev, "IRR includes unknown cause bits (0x%08X)\n",
  112. irr);
  113. /* Just warning */
  114. }
  115. return IRQ_HANDLED;
  116. }
  117. int init_cc_regs(struct cc_drvdata *drvdata, bool is_probe)
  118. {
  119. unsigned int val, cache_params;
  120. struct device *dev = drvdata_to_dev(drvdata);
  121. /* Unmask all AXI interrupt sources AXI_CFG1 register */
  122. val = cc_ioread(drvdata, CC_REG(AXIM_CFG));
  123. cc_iowrite(drvdata, CC_REG(AXIM_CFG), val & ~CC_AXI_IRQ_MASK);
  124. dev_dbg(dev, "AXIM_CFG=0x%08X\n",
  125. cc_ioread(drvdata, CC_REG(AXIM_CFG)));
  126. /* Clear all pending interrupts */
  127. val = cc_ioread(drvdata, CC_REG(HOST_IRR));
  128. dev_dbg(dev, "IRR=0x%08X\n", val);
  129. cc_iowrite(drvdata, CC_REG(HOST_ICR), val);
  130. /* Unmask relevant interrupt cause */
  131. val = CC_COMP_IRQ_MASK | CC_AXI_ERR_IRQ_MASK;
  132. if (drvdata->hw_rev >= CC_HW_REV_712)
  133. val |= CC_GPR0_IRQ_MASK;
  134. cc_iowrite(drvdata, CC_REG(HOST_IMR), ~val);
  135. cache_params = (drvdata->coherent ? CC_COHERENT_CACHE_PARAMS : 0x0);
  136. val = cc_ioread(drvdata, CC_REG(AXIM_CACHE_PARAMS));
  137. if (is_probe)
  138. dev_dbg(dev, "Cache params previous: 0x%08X\n", val);
  139. cc_iowrite(drvdata, CC_REG(AXIM_CACHE_PARAMS), cache_params);
  140. val = cc_ioread(drvdata, CC_REG(AXIM_CACHE_PARAMS));
  141. if (is_probe)
  142. dev_dbg(dev, "Cache params current: 0x%08X (expect: 0x%08X)\n",
  143. val, cache_params);
  144. return 0;
  145. }
  146. static int init_cc_resources(struct platform_device *plat_dev)
  147. {
  148. struct resource *req_mem_cc_regs = NULL;
  149. struct cc_drvdata *new_drvdata;
  150. struct device *dev = &plat_dev->dev;
  151. struct device_node *np = dev->of_node;
  152. u32 signature_val;
  153. u64 dma_mask;
  154. const struct cc_hw_data *hw_rev;
  155. const struct of_device_id *dev_id;
  156. struct clk *clk;
  157. int rc = 0;
  158. new_drvdata = devm_kzalloc(dev, sizeof(*new_drvdata), GFP_KERNEL);
  159. if (!new_drvdata)
  160. return -ENOMEM;
  161. dev_id = of_match_node(arm_ccree_dev_of_match, np);
  162. if (!dev_id)
  163. return -ENODEV;
  164. hw_rev = (struct cc_hw_data *)dev_id->data;
  165. new_drvdata->hw_rev_name = hw_rev->name;
  166. new_drvdata->hw_rev = hw_rev->rev;
  167. if (hw_rev->rev >= CC_HW_REV_712) {
  168. new_drvdata->hash_len_sz = HASH_LEN_SIZE_712;
  169. new_drvdata->axim_mon_offset = CC_REG(AXIM_MON_COMP);
  170. new_drvdata->sig_offset = CC_REG(HOST_SIGNATURE_712);
  171. new_drvdata->ver_offset = CC_REG(HOST_VERSION_712);
  172. } else {
  173. new_drvdata->hash_len_sz = HASH_LEN_SIZE_630;
  174. new_drvdata->axim_mon_offset = CC_REG(AXIM_MON_COMP8);
  175. new_drvdata->sig_offset = CC_REG(HOST_SIGNATURE_630);
  176. new_drvdata->ver_offset = CC_REG(HOST_VERSION_630);
  177. }
  178. platform_set_drvdata(plat_dev, new_drvdata);
  179. new_drvdata->plat_dev = plat_dev;
  180. clk = devm_clk_get(dev, NULL);
  181. if (IS_ERR(clk))
  182. switch (PTR_ERR(clk)) {
  183. /* Clock is optional so this might be fine */
  184. case -ENOENT:
  185. break;
  186. /* Clock not available, let's try again soon */
  187. case -EPROBE_DEFER:
  188. return -EPROBE_DEFER;
  189. default:
  190. dev_err(dev, "Error getting clock: %ld\n",
  191. PTR_ERR(clk));
  192. return PTR_ERR(clk);
  193. }
  194. new_drvdata->clk = clk;
  195. new_drvdata->coherent = of_dma_is_coherent(np);
  196. /* Get device resources */
  197. /* First CC registers space */
  198. req_mem_cc_regs = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
  199. /* Map registers space */
  200. new_drvdata->cc_base = devm_ioremap_resource(dev, req_mem_cc_regs);
  201. if (IS_ERR(new_drvdata->cc_base)) {
  202. dev_err(dev, "Failed to ioremap registers");
  203. return PTR_ERR(new_drvdata->cc_base);
  204. }
  205. dev_dbg(dev, "Got MEM resource (%s): %pR\n", req_mem_cc_regs->name,
  206. req_mem_cc_regs);
  207. dev_dbg(dev, "CC registers mapped from %pa to 0x%p\n",
  208. &req_mem_cc_regs->start, new_drvdata->cc_base);
  209. /* Then IRQ */
  210. new_drvdata->irq = platform_get_irq(plat_dev, 0);
  211. if (new_drvdata->irq < 0) {
  212. dev_err(dev, "Failed getting IRQ resource\n");
  213. return new_drvdata->irq;
  214. }
  215. rc = devm_request_irq(dev, new_drvdata->irq, cc_isr,
  216. IRQF_SHARED, "ccree", new_drvdata);
  217. if (rc) {
  218. dev_err(dev, "Could not register to interrupt %d\n",
  219. new_drvdata->irq);
  220. return rc;
  221. }
  222. dev_dbg(dev, "Registered to IRQ: %d\n", new_drvdata->irq);
  223. init_completion(&new_drvdata->hw_queue_avail);
  224. if (!plat_dev->dev.dma_mask)
  225. plat_dev->dev.dma_mask = &plat_dev->dev.coherent_dma_mask;
  226. dma_mask = DMA_BIT_MASK(DMA_BIT_MASK_LEN);
  227. while (dma_mask > 0x7fffffffUL) {
  228. if (dma_supported(&plat_dev->dev, dma_mask)) {
  229. rc = dma_set_coherent_mask(&plat_dev->dev, dma_mask);
  230. if (!rc)
  231. break;
  232. }
  233. dma_mask >>= 1;
  234. }
  235. if (rc) {
  236. dev_err(dev, "Failed in dma_set_mask, mask=%llx\n", dma_mask);
  237. return rc;
  238. }
  239. rc = cc_clk_on(new_drvdata);
  240. if (rc) {
  241. dev_err(dev, "Failed to enable clock");
  242. return rc;
  243. }
  244. /* Verify correct mapping */
  245. signature_val = cc_ioread(new_drvdata, new_drvdata->sig_offset);
  246. if (signature_val != hw_rev->sig) {
  247. dev_err(dev, "Invalid CC signature: SIGNATURE=0x%08X != expected=0x%08X\n",
  248. signature_val, hw_rev->sig);
  249. rc = -EINVAL;
  250. goto post_clk_err;
  251. }
  252. dev_dbg(dev, "CC SIGNATURE=0x%08X\n", signature_val);
  253. /* Display HW versions */
  254. dev_info(dev, "ARM CryptoCell %s Driver: HW version 0x%08X, Driver version %s\n",
  255. hw_rev->name, cc_ioread(new_drvdata, new_drvdata->ver_offset),
  256. DRV_MODULE_VERSION);
  257. rc = init_cc_regs(new_drvdata, true);
  258. if (rc) {
  259. dev_err(dev, "init_cc_regs failed\n");
  260. goto post_clk_err;
  261. }
  262. rc = cc_debugfs_init(new_drvdata);
  263. if (rc) {
  264. dev_err(dev, "Failed registering debugfs interface\n");
  265. goto post_regs_err;
  266. }
  267. rc = cc_fips_init(new_drvdata);
  268. if (rc) {
  269. dev_err(dev, "CC_FIPS_INIT failed 0x%x\n", rc);
  270. goto post_debugfs_err;
  271. }
  272. rc = cc_sram_mgr_init(new_drvdata);
  273. if (rc) {
  274. dev_err(dev, "cc_sram_mgr_init failed\n");
  275. goto post_fips_init_err;
  276. }
  277. new_drvdata->mlli_sram_addr =
  278. cc_sram_alloc(new_drvdata, MAX_MLLI_BUFF_SIZE);
  279. if (new_drvdata->mlli_sram_addr == NULL_SRAM_ADDR) {
  280. dev_err(dev, "Failed to alloc MLLI Sram buffer\n");
  281. rc = -ENOMEM;
  282. goto post_sram_mgr_err;
  283. }
  284. rc = cc_req_mgr_init(new_drvdata);
  285. if (rc) {
  286. dev_err(dev, "cc_req_mgr_init failed\n");
  287. goto post_sram_mgr_err;
  288. }
  289. rc = cc_buffer_mgr_init(new_drvdata);
  290. if (rc) {
  291. dev_err(dev, "buffer_mgr_init failed\n");
  292. goto post_req_mgr_err;
  293. }
  294. rc = cc_pm_init(new_drvdata);
  295. if (rc) {
  296. dev_err(dev, "ssi_power_mgr_init failed\n");
  297. goto post_buf_mgr_err;
  298. }
  299. rc = cc_ivgen_init(new_drvdata);
  300. if (rc) {
  301. dev_err(dev, "cc_ivgen_init failed\n");
  302. goto post_buf_mgr_err;
  303. }
  304. /* Allocate crypto algs */
  305. rc = cc_cipher_alloc(new_drvdata);
  306. if (rc) {
  307. dev_err(dev, "cc_cipher_alloc failed\n");
  308. goto post_ivgen_err;
  309. }
  310. /* hash must be allocated before aead since hash exports APIs */
  311. rc = cc_hash_alloc(new_drvdata);
  312. if (rc) {
  313. dev_err(dev, "cc_hash_alloc failed\n");
  314. goto post_cipher_err;
  315. }
  316. rc = cc_aead_alloc(new_drvdata);
  317. if (rc) {
  318. dev_err(dev, "cc_aead_alloc failed\n");
  319. goto post_hash_err;
  320. }
  321. /* All set, we can allow autosuspend */
  322. cc_pm_go(new_drvdata);
  323. /* If we got here and FIPS mode is enabled
  324. * it means all FIPS test passed, so let TEE
  325. * know we're good.
  326. */
  327. cc_set_ree_fips_status(new_drvdata, true);
  328. return 0;
  329. post_hash_err:
  330. cc_hash_free(new_drvdata);
  331. post_cipher_err:
  332. cc_cipher_free(new_drvdata);
  333. post_ivgen_err:
  334. cc_ivgen_fini(new_drvdata);
  335. post_buf_mgr_err:
  336. cc_buffer_mgr_fini(new_drvdata);
  337. post_req_mgr_err:
  338. cc_req_mgr_fini(new_drvdata);
  339. post_sram_mgr_err:
  340. cc_sram_mgr_fini(new_drvdata);
  341. post_fips_init_err:
  342. cc_fips_fini(new_drvdata);
  343. post_debugfs_err:
  344. cc_debugfs_fini(new_drvdata);
  345. post_regs_err:
  346. fini_cc_regs(new_drvdata);
  347. post_clk_err:
  348. cc_clk_off(new_drvdata);
  349. return rc;
  350. }
  351. void fini_cc_regs(struct cc_drvdata *drvdata)
  352. {
  353. /* Mask all interrupts */
  354. cc_iowrite(drvdata, CC_REG(HOST_IMR), 0xFFFFFFFF);
  355. }
  356. static void cleanup_cc_resources(struct platform_device *plat_dev)
  357. {
  358. struct cc_drvdata *drvdata =
  359. (struct cc_drvdata *)platform_get_drvdata(plat_dev);
  360. cc_aead_free(drvdata);
  361. cc_hash_free(drvdata);
  362. cc_cipher_free(drvdata);
  363. cc_ivgen_fini(drvdata);
  364. cc_pm_fini(drvdata);
  365. cc_buffer_mgr_fini(drvdata);
  366. cc_req_mgr_fini(drvdata);
  367. cc_sram_mgr_fini(drvdata);
  368. cc_fips_fini(drvdata);
  369. cc_debugfs_fini(drvdata);
  370. fini_cc_regs(drvdata);
  371. cc_clk_off(drvdata);
  372. }
  373. int cc_clk_on(struct cc_drvdata *drvdata)
  374. {
  375. struct clk *clk = drvdata->clk;
  376. int rc;
  377. if (IS_ERR(clk))
  378. /* Not all devices have a clock associated with CCREE */
  379. return 0;
  380. rc = clk_prepare_enable(clk);
  381. if (rc)
  382. return rc;
  383. return 0;
  384. }
  385. void cc_clk_off(struct cc_drvdata *drvdata)
  386. {
  387. struct clk *clk = drvdata->clk;
  388. if (IS_ERR(clk))
  389. /* Not all devices have a clock associated with CCREE */
  390. return;
  391. clk_disable_unprepare(clk);
  392. }
  393. static int ccree_probe(struct platform_device *plat_dev)
  394. {
  395. int rc;
  396. struct device *dev = &plat_dev->dev;
  397. /* Map registers space */
  398. rc = init_cc_resources(plat_dev);
  399. if (rc)
  400. return rc;
  401. dev_info(dev, "ARM ccree device initialized\n");
  402. return 0;
  403. }
  404. static int ccree_remove(struct platform_device *plat_dev)
  405. {
  406. struct device *dev = &plat_dev->dev;
  407. dev_dbg(dev, "Releasing ccree resources...\n");
  408. cleanup_cc_resources(plat_dev);
  409. dev_info(dev, "ARM ccree device terminated\n");
  410. return 0;
  411. }
  412. static struct platform_driver ccree_driver = {
  413. .driver = {
  414. .name = "ccree",
  415. .of_match_table = arm_ccree_dev_of_match,
  416. #ifdef CONFIG_PM
  417. .pm = &ccree_pm,
  418. #endif
  419. },
  420. .probe = ccree_probe,
  421. .remove = ccree_remove,
  422. };
  423. static int __init ccree_init(void)
  424. {
  425. int ret;
  426. cc_hash_global_init();
  427. ret = cc_debugfs_global_init();
  428. if (ret)
  429. return ret;
  430. return platform_driver_register(&ccree_driver);
  431. }
  432. module_init(ccree_init);
  433. static void __exit ccree_exit(void)
  434. {
  435. platform_driver_unregister(&ccree_driver);
  436. cc_debugfs_global_fini();
  437. }
  438. module_exit(ccree_exit);
  439. /* Module description */
  440. MODULE_DESCRIPTION("ARM TrustZone CryptoCell REE Driver");
  441. MODULE_VERSION(DRV_MODULE_VERSION);
  442. MODULE_AUTHOR("ARM");
  443. MODULE_LICENSE("GPL v2");