ice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Qualcomm ICE (Inline Crypto Engine) support.
  4. *
  5. * Copyright (c) 2013-2019, The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2019, Google LLC
  7. * Copyright (c) 2023, Linaro Limited
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/cleanup.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/firmware/qcom/qcom_scm.h>
  19. #include <soc/qcom/ice.h>
  20. #define AES_256_XTS_KEY_SIZE 64
  21. /* QCOM ICE registers */
  22. #define QCOM_ICE_REG_VERSION 0x0008
  23. #define QCOM_ICE_REG_FUSE_SETTING 0x0010
  24. #define QCOM_ICE_REG_BIST_STATUS 0x0070
  25. #define QCOM_ICE_REG_ADVANCED_CONTROL 0x1000
  26. /* BIST ("built-in self-test") status flags */
  27. #define QCOM_ICE_BIST_STATUS_MASK GENMASK(31, 28)
  28. #define QCOM_ICE_FUSE_SETTING_MASK 0x1
  29. #define QCOM_ICE_FORCE_HW_KEY0_SETTING_MASK 0x2
  30. #define QCOM_ICE_FORCE_HW_KEY1_SETTING_MASK 0x4
  31. #define qcom_ice_writel(engine, val, reg) \
  32. writel((val), (engine)->base + (reg))
  33. #define qcom_ice_readl(engine, reg) \
  34. readl((engine)->base + (reg))
  35. struct qcom_ice {
  36. struct device *dev;
  37. void __iomem *base;
  38. struct device_link *link;
  39. struct clk *core_clk;
  40. };
  41. static bool qcom_ice_check_supported(struct qcom_ice *ice)
  42. {
  43. u32 regval = qcom_ice_readl(ice, QCOM_ICE_REG_VERSION);
  44. struct device *dev = ice->dev;
  45. int major = FIELD_GET(GENMASK(31, 24), regval);
  46. int minor = FIELD_GET(GENMASK(23, 16), regval);
  47. int step = FIELD_GET(GENMASK(15, 0), regval);
  48. /* For now this driver only supports ICE version 3 and 4. */
  49. if (major != 3 && major != 4) {
  50. dev_warn(dev, "Unsupported ICE version: v%d.%d.%d\n",
  51. major, minor, step);
  52. return false;
  53. }
  54. dev_info(dev, "Found QC Inline Crypto Engine (ICE) v%d.%d.%d\n",
  55. major, minor, step);
  56. /* If fuses are blown, ICE might not work in the standard way. */
  57. regval = qcom_ice_readl(ice, QCOM_ICE_REG_FUSE_SETTING);
  58. if (regval & (QCOM_ICE_FUSE_SETTING_MASK |
  59. QCOM_ICE_FORCE_HW_KEY0_SETTING_MASK |
  60. QCOM_ICE_FORCE_HW_KEY1_SETTING_MASK)) {
  61. dev_warn(dev, "Fuses are blown; ICE is unusable!\n");
  62. return false;
  63. }
  64. return true;
  65. }
  66. static void qcom_ice_low_power_mode_enable(struct qcom_ice *ice)
  67. {
  68. u32 regval;
  69. regval = qcom_ice_readl(ice, QCOM_ICE_REG_ADVANCED_CONTROL);
  70. /* Enable low power mode sequence */
  71. regval |= 0x7000;
  72. qcom_ice_writel(ice, regval, QCOM_ICE_REG_ADVANCED_CONTROL);
  73. }
  74. static void qcom_ice_optimization_enable(struct qcom_ice *ice)
  75. {
  76. u32 regval;
  77. /* ICE Optimizations Enable Sequence */
  78. regval = qcom_ice_readl(ice, QCOM_ICE_REG_ADVANCED_CONTROL);
  79. regval |= 0xd807100;
  80. /* ICE HPG requires delay before writing */
  81. udelay(5);
  82. qcom_ice_writel(ice, regval, QCOM_ICE_REG_ADVANCED_CONTROL);
  83. udelay(5);
  84. }
  85. /*
  86. * Wait until the ICE BIST (built-in self-test) has completed.
  87. *
  88. * This may be necessary before ICE can be used.
  89. * Note that we don't really care whether the BIST passed or failed;
  90. * we really just want to make sure that it isn't still running. This is
  91. * because (a) the BIST is a FIPS compliance thing that never fails in
  92. * practice, (b) ICE is documented to reject crypto requests if the BIST
  93. * fails, so we needn't do it in software too, and (c) properly testing
  94. * storage encryption requires testing the full storage stack anyway,
  95. * and not relying on hardware-level self-tests.
  96. */
  97. static int qcom_ice_wait_bist_status(struct qcom_ice *ice)
  98. {
  99. u32 regval;
  100. int err;
  101. err = readl_poll_timeout(ice->base + QCOM_ICE_REG_BIST_STATUS,
  102. regval, !(regval & QCOM_ICE_BIST_STATUS_MASK),
  103. 50, 5000);
  104. if (err)
  105. dev_err(ice->dev, "Timed out waiting for ICE self-test to complete\n");
  106. return err;
  107. }
  108. int qcom_ice_enable(struct qcom_ice *ice)
  109. {
  110. qcom_ice_low_power_mode_enable(ice);
  111. qcom_ice_optimization_enable(ice);
  112. return qcom_ice_wait_bist_status(ice);
  113. }
  114. EXPORT_SYMBOL_GPL(qcom_ice_enable);
  115. int qcom_ice_resume(struct qcom_ice *ice)
  116. {
  117. struct device *dev = ice->dev;
  118. int err;
  119. err = clk_prepare_enable(ice->core_clk);
  120. if (err) {
  121. dev_err(dev, "failed to enable core clock (%d)\n",
  122. err);
  123. return err;
  124. }
  125. return qcom_ice_wait_bist_status(ice);
  126. }
  127. EXPORT_SYMBOL_GPL(qcom_ice_resume);
  128. int qcom_ice_suspend(struct qcom_ice *ice)
  129. {
  130. clk_disable_unprepare(ice->core_clk);
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(qcom_ice_suspend);
  134. int qcom_ice_program_key(struct qcom_ice *ice,
  135. u8 algorithm_id, u8 key_size,
  136. const u8 crypto_key[], u8 data_unit_size,
  137. int slot)
  138. {
  139. struct device *dev = ice->dev;
  140. union {
  141. u8 bytes[AES_256_XTS_KEY_SIZE];
  142. u32 words[AES_256_XTS_KEY_SIZE / sizeof(u32)];
  143. } key;
  144. int i;
  145. int err;
  146. /* Only AES-256-XTS has been tested so far. */
  147. if (algorithm_id != QCOM_ICE_CRYPTO_ALG_AES_XTS ||
  148. key_size != QCOM_ICE_CRYPTO_KEY_SIZE_256) {
  149. dev_err_ratelimited(dev,
  150. "Unhandled crypto capability; algorithm_id=%d, key_size=%d\n",
  151. algorithm_id, key_size);
  152. return -EINVAL;
  153. }
  154. memcpy(key.bytes, crypto_key, AES_256_XTS_KEY_SIZE);
  155. /* The SCM call requires that the key words are encoded in big endian */
  156. for (i = 0; i < ARRAY_SIZE(key.words); i++)
  157. __cpu_to_be32s(&key.words[i]);
  158. err = qcom_scm_ice_set_key(slot, key.bytes, AES_256_XTS_KEY_SIZE,
  159. QCOM_SCM_ICE_CIPHER_AES_256_XTS,
  160. data_unit_size);
  161. memzero_explicit(&key, sizeof(key));
  162. return err;
  163. }
  164. EXPORT_SYMBOL_GPL(qcom_ice_program_key);
  165. int qcom_ice_evict_key(struct qcom_ice *ice, int slot)
  166. {
  167. return qcom_scm_ice_invalidate_key(slot);
  168. }
  169. EXPORT_SYMBOL_GPL(qcom_ice_evict_key);
  170. static struct qcom_ice *qcom_ice_create(struct device *dev,
  171. void __iomem *base)
  172. {
  173. struct qcom_ice *engine;
  174. if (!qcom_scm_is_available())
  175. return ERR_PTR(-EPROBE_DEFER);
  176. if (!qcom_scm_ice_available()) {
  177. dev_warn(dev, "ICE SCM interface not found\n");
  178. return NULL;
  179. }
  180. engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL);
  181. if (!engine)
  182. return ERR_PTR(-ENOMEM);
  183. engine->dev = dev;
  184. engine->base = base;
  185. /*
  186. * Legacy DT binding uses different clk names for each consumer,
  187. * so lets try those first. If none of those are a match, it means
  188. * the we only have one clock and it is part of the dedicated DT node.
  189. * Also, enable the clock before we check what HW version the driver
  190. * supports.
  191. */
  192. engine->core_clk = devm_clk_get_optional_enabled(dev, "ice_core_clk");
  193. if (!engine->core_clk)
  194. engine->core_clk = devm_clk_get_optional_enabled(dev, "ice");
  195. if (!engine->core_clk)
  196. engine->core_clk = devm_clk_get_enabled(dev, NULL);
  197. if (IS_ERR(engine->core_clk))
  198. return ERR_CAST(engine->core_clk);
  199. if (!qcom_ice_check_supported(engine))
  200. return ERR_PTR(-EOPNOTSUPP);
  201. dev_dbg(dev, "Registered Qualcomm Inline Crypto Engine\n");
  202. return engine;
  203. }
  204. /**
  205. * of_qcom_ice_get() - get an ICE instance from a DT node
  206. * @dev: device pointer for the consumer device
  207. *
  208. * This function will provide an ICE instance either by creating one for the
  209. * consumer device if its DT node provides the 'ice' reg range and the 'ice'
  210. * clock (for legacy DT style). On the other hand, if consumer provides a
  211. * phandle via 'qcom,ice' property to an ICE DT, the ICE instance will already
  212. * be created and so this function will return that instead.
  213. *
  214. * Return: ICE pointer on success, NULL if there is no ICE data provided by the
  215. * consumer or ERR_PTR() on error.
  216. */
  217. struct qcom_ice *of_qcom_ice_get(struct device *dev)
  218. {
  219. struct platform_device *pdev = to_platform_device(dev);
  220. struct qcom_ice *ice;
  221. struct resource *res;
  222. void __iomem *base;
  223. if (!dev || !dev->of_node)
  224. return ERR_PTR(-ENODEV);
  225. /*
  226. * In order to support legacy style devicetree bindings, we need
  227. * to create the ICE instance using the consumer device and the reg
  228. * range called 'ice' it provides.
  229. */
  230. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ice");
  231. if (res) {
  232. base = devm_ioremap_resource(&pdev->dev, res);
  233. if (IS_ERR(base))
  234. return ERR_CAST(base);
  235. /* create ICE instance using consumer dev */
  236. return qcom_ice_create(&pdev->dev, base);
  237. }
  238. /*
  239. * If the consumer node does not provider an 'ice' reg range
  240. * (legacy DT binding), then it must at least provide a phandle
  241. * to the ICE devicetree node, otherwise ICE is not supported.
  242. */
  243. struct device_node *node __free(device_node) = of_parse_phandle(dev->of_node,
  244. "qcom,ice", 0);
  245. if (!node)
  246. return NULL;
  247. pdev = of_find_device_by_node(node);
  248. if (!pdev) {
  249. dev_err(dev, "Cannot find device node %s\n", node->name);
  250. return ERR_PTR(-EPROBE_DEFER);
  251. }
  252. ice = platform_get_drvdata(pdev);
  253. if (!ice) {
  254. dev_err(dev, "Cannot get ice instance from %s\n",
  255. dev_name(&pdev->dev));
  256. platform_device_put(pdev);
  257. return ERR_PTR(-EPROBE_DEFER);
  258. }
  259. ice->link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
  260. if (!ice->link) {
  261. dev_err(&pdev->dev,
  262. "Failed to create device link to consumer %s\n",
  263. dev_name(dev));
  264. platform_device_put(pdev);
  265. ice = ERR_PTR(-EINVAL);
  266. }
  267. return ice;
  268. }
  269. EXPORT_SYMBOL_GPL(of_qcom_ice_get);
  270. static void qcom_ice_put(const struct qcom_ice *ice)
  271. {
  272. struct platform_device *pdev = to_platform_device(ice->dev);
  273. if (!platform_get_resource_byname(pdev, IORESOURCE_MEM, "ice"))
  274. platform_device_put(pdev);
  275. }
  276. static void devm_of_qcom_ice_put(struct device *dev, void *res)
  277. {
  278. qcom_ice_put(*(struct qcom_ice **)res);
  279. }
  280. /**
  281. * devm_of_qcom_ice_get() - Devres managed helper to get an ICE instance from
  282. * a DT node.
  283. * @dev: device pointer for the consumer device.
  284. *
  285. * This function will provide an ICE instance either by creating one for the
  286. * consumer device if its DT node provides the 'ice' reg range and the 'ice'
  287. * clock (for legacy DT style). On the other hand, if consumer provides a
  288. * phandle via 'qcom,ice' property to an ICE DT, the ICE instance will already
  289. * be created and so this function will return that instead.
  290. *
  291. * Return: ICE pointer on success, NULL if there is no ICE data provided by the
  292. * consumer or ERR_PTR() on error.
  293. */
  294. struct qcom_ice *devm_of_qcom_ice_get(struct device *dev)
  295. {
  296. struct qcom_ice *ice, **dr;
  297. dr = devres_alloc(devm_of_qcom_ice_put, sizeof(*dr), GFP_KERNEL);
  298. if (!dr)
  299. return ERR_PTR(-ENOMEM);
  300. ice = of_qcom_ice_get(dev);
  301. if (!IS_ERR_OR_NULL(ice)) {
  302. *dr = ice;
  303. devres_add(dev, dr);
  304. } else {
  305. devres_free(dr);
  306. }
  307. return ice;
  308. }
  309. EXPORT_SYMBOL_GPL(devm_of_qcom_ice_get);
  310. static int qcom_ice_probe(struct platform_device *pdev)
  311. {
  312. struct qcom_ice *engine;
  313. void __iomem *base;
  314. base = devm_platform_ioremap_resource(pdev, 0);
  315. if (IS_ERR(base)) {
  316. dev_warn(&pdev->dev, "ICE registers not found\n");
  317. return PTR_ERR(base);
  318. }
  319. engine = qcom_ice_create(&pdev->dev, base);
  320. if (IS_ERR(engine))
  321. return PTR_ERR(engine);
  322. platform_set_drvdata(pdev, engine);
  323. return 0;
  324. }
  325. static const struct of_device_id qcom_ice_of_match_table[] = {
  326. { .compatible = "qcom,inline-crypto-engine" },
  327. { },
  328. };
  329. MODULE_DEVICE_TABLE(of, qcom_ice_of_match_table);
  330. static struct platform_driver qcom_ice_driver = {
  331. .probe = qcom_ice_probe,
  332. .driver = {
  333. .name = "qcom-ice",
  334. .of_match_table = qcom_ice_of_match_table,
  335. },
  336. };
  337. module_platform_driver(qcom_ice_driver);
  338. MODULE_DESCRIPTION("Qualcomm Inline Crypto Engine driver");
  339. MODULE_LICENSE("GPL");