ufs-qcom-ice.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Qualcomm ICE (Inline Crypto Engine) support.
  4. *
  5. * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  7. * Copyright 2019 Google LLC
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/qcom_scm.h>
  12. #include <linux/qtee_shmbridge.h>
  13. #include <ufs/ufshcd-crypto.h>
  14. #include <linux/crypto-qti-common.h>
  15. #include "ufs-qcom.h"
  16. #define AES_256_XTS_KEY_SIZE 64
  17. /* QCOM ICE registers */
  18. #define QCOM_ICE_REG_CONTROL 0x0000
  19. #define QCOM_ICE_REG_RESET 0x0004
  20. #define QCOM_ICE_REG_VERSION 0x0008
  21. #define QCOM_ICE_REG_FUSE_SETTING 0x0010
  22. #define QCOM_ICE_REG_PARAMETERS_1 0x0014
  23. #define QCOM_ICE_REG_PARAMETERS_2 0x0018
  24. #define QCOM_ICE_REG_PARAMETERS_3 0x001C
  25. #define QCOM_ICE_REG_PARAMETERS_4 0x0020
  26. #define QCOM_ICE_REG_PARAMETERS_5 0x0024
  27. /* QCOM ICE v3.X only */
  28. #define QCOM_ICE_GENERAL_ERR_STTS 0x0040
  29. #define QCOM_ICE_INVALID_CCFG_ERR_STTS 0x0030
  30. #define QCOM_ICE_GENERAL_ERR_MASK 0x0044
  31. /* QCOM ICE v2.X only */
  32. #define QCOM_ICE_REG_NON_SEC_IRQ_STTS 0x0040
  33. #define QCOM_ICE_REG_NON_SEC_IRQ_MASK 0x0044
  34. #define QCOM_ICE_REG_NON_SEC_IRQ_CLR 0x0048
  35. #define QCOM_ICE_REG_STREAM1_ERROR_SYNDROME1 0x0050
  36. #define QCOM_ICE_REG_STREAM1_ERROR_SYNDROME2 0x0054
  37. #define QCOM_ICE_REG_STREAM2_ERROR_SYNDROME1 0x0058
  38. #define QCOM_ICE_REG_STREAM2_ERROR_SYNDROME2 0x005C
  39. #define QCOM_ICE_REG_STREAM1_BIST_ERROR_VEC 0x0060
  40. #define QCOM_ICE_REG_STREAM2_BIST_ERROR_VEC 0x0064
  41. #define QCOM_ICE_REG_STREAM1_BIST_FINISH_VEC 0x0068
  42. #define QCOM_ICE_REG_STREAM2_BIST_FINISH_VEC 0x006C
  43. #define QCOM_ICE_REG_BIST_STATUS 0x0070
  44. #define QCOM_ICE_REG_BYPASS_STATUS 0x0074
  45. #define QCOM_ICE_REG_ADVANCED_CONTROL 0x1000
  46. #define QCOM_ICE_REG_ENDIAN_SWAP 0x1004
  47. #define QCOM_ICE_REG_TEST_BUS_CONTROL 0x1010
  48. #define QCOM_ICE_REG_TEST_BUS_REG 0x1014
  49. /* BIST ("built-in self-test"?) status flags */
  50. #define QCOM_ICE_BIST_STATUS_MASK 0xF0000000
  51. #define QCOM_ICE_FUSE_SETTING_MASK 0x1
  52. #define QCOM_ICE_FORCE_HW_KEY0_SETTING_MASK 0x2
  53. #define QCOM_ICE_FORCE_HW_KEY1_SETTING_MASK 0x4
  54. #define qcom_ice_writel(host, val, reg) \
  55. writel((val), (host)->ice_mmio + (reg))
  56. #define qcom_ice_readl(host, reg) \
  57. readl((host)->ice_mmio + (reg))
  58. static bool qcom_ice_supported(struct ufs_qcom_host *host)
  59. {
  60. struct device *dev = host->hba->dev;
  61. u32 regval = qcom_ice_readl(host, QCOM_ICE_REG_VERSION);
  62. int major = regval >> 24;
  63. int minor = (regval >> 16) & 0xFF;
  64. int step = regval & 0xFFFF;
  65. /* For now this driver only supports ICE version 3. */
  66. if (major < 3) {
  67. dev_warn(dev, "Unsupported ICE version: v%d.%d.%d\n",
  68. major, minor, step);
  69. return false;
  70. }
  71. dev_info(dev, "Found QC Inline Crypto Engine (ICE) v%d.%d.%d\n",
  72. major, minor, step);
  73. /* If fuses are blown, ICE might not work in the standard way. */
  74. regval = qcom_ice_readl(host, QCOM_ICE_REG_FUSE_SETTING);
  75. if (regval & (QCOM_ICE_FUSE_SETTING_MASK |
  76. QCOM_ICE_FORCE_HW_KEY0_SETTING_MASK |
  77. QCOM_ICE_FORCE_HW_KEY1_SETTING_MASK)) {
  78. dev_warn(dev, "Fuses are blown; ICE is unusable!\n");
  79. return false;
  80. }
  81. return true;
  82. }
  83. int ufs_qcom_ice_init(struct ufs_qcom_host *host)
  84. {
  85. struct ufs_hba *hba = host->hba;
  86. struct device *dev = hba->dev;
  87. struct platform_device *pdev = to_platform_device(dev);
  88. struct resource *ice_base_res;
  89. #if (IS_ENABLED(CONFIG_QTI_HW_KEY_MANAGER) || IS_ENABLED(CONFIG_QTI_HW_KEY_MANAGER_V1))
  90. struct resource *ice_hwkm_res;
  91. #endif
  92. int err;
  93. if (!(ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES) &
  94. MASK_CRYPTO_SUPPORT))
  95. return 0;
  96. ice_base_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ufs_ice");
  97. if (!ice_base_res) {
  98. dev_warn(dev, "ICE registers not found\n");
  99. goto disable;
  100. }
  101. if (!qcom_scm_ice_available()) {
  102. dev_warn(dev, "ICE SCM interface not found\n");
  103. goto disable;
  104. }
  105. host->ice_mmio = devm_ioremap_resource(dev, ice_base_res);
  106. if (IS_ERR(host->ice_mmio)) {
  107. err = PTR_ERR(host->ice_mmio);
  108. return err;
  109. }
  110. #if (IS_ENABLED(CONFIG_QTI_HW_KEY_MANAGER) || IS_ENABLED(CONFIG_QTI_HW_KEY_MANAGER_V1))
  111. ice_hwkm_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ufs_ice_hwkm");
  112. if (!ice_hwkm_res) {
  113. dev_warn(dev, "ICE HWKM registers not found\n");
  114. goto disable;
  115. }
  116. host->ice_hwkm_mmio = devm_ioremap_resource(dev, ice_hwkm_res);
  117. if (IS_ERR(host->ice_hwkm_mmio)) {
  118. err = PTR_ERR(host->ice_hwkm_mmio);
  119. dev_err(dev, "Failed to map ICE HWKM registers; err=%d\n", err);
  120. return err;
  121. }
  122. #endif
  123. if (!qcom_ice_supported(host))
  124. goto disable;
  125. return 0;
  126. disable:
  127. dev_warn(dev, "Disabling inline encryption support\n");
  128. hba->caps &= ~UFSHCD_CAP_CRYPTO;
  129. return 0;
  130. }
  131. static void qcom_ice_low_power_mode_enable(struct ufs_qcom_host *host)
  132. {
  133. u32 regval;
  134. regval = qcom_ice_readl(host, QCOM_ICE_REG_ADVANCED_CONTROL);
  135. /*
  136. * Enable low power mode sequence
  137. * [0]-0, [1]-0, [2]-0, [3]-E, [4]-0, [5]-0, [6]-0, [7]-0
  138. */
  139. regval |= 0xF000;
  140. qcom_ice_writel(host, regval, QCOM_ICE_REG_ADVANCED_CONTROL);
  141. }
  142. static void qcom_ice_optimization_enable(struct ufs_qcom_host *host)
  143. {
  144. u32 regval;
  145. /* ICE Optimizations Enable Sequence */
  146. regval = qcom_ice_readl(host, QCOM_ICE_REG_ADVANCED_CONTROL);
  147. regval |= 0xD80F100;
  148. /* ICE HPG requires delay before writing */
  149. udelay(5);
  150. qcom_ice_writel(host, regval, QCOM_ICE_REG_ADVANCED_CONTROL);
  151. udelay(5);
  152. }
  153. int ufs_qcom_ice_enable(struct ufs_qcom_host *host)
  154. {
  155. if (!(host->hba->caps & UFSHCD_CAP_CRYPTO))
  156. return 0;
  157. qcom_ice_low_power_mode_enable(host);
  158. qcom_ice_optimization_enable(host);
  159. return ufs_qcom_ice_resume(host);
  160. }
  161. void ufs_qcom_ice_disable(struct ufs_qcom_host *host)
  162. {
  163. if (!(host->hba->caps & UFSHCD_CAP_CRYPTO))
  164. return;
  165. if (host->hba->android_quirks & UFSHCD_ANDROID_QUIRK_CUSTOM_CRYPTO_PROFILE)
  166. return crypto_qti_disable();
  167. }
  168. void ufs_qcom_ice_debug(struct ufs_qcom_host *host)
  169. {
  170. struct ice_mmio_data mmio_data;
  171. if (!host || !((host->hba->caps & UFSHCD_CAP_CRYPTO)))
  172. return;
  173. mmio_data.ice_base_mmio = host->ice_mmio;
  174. #if (IS_ENABLED(CONFIG_QTI_HW_KEY_MANAGER) || IS_ENABLED(CONFIG_QTI_HW_KEY_MANAGER_V1))
  175. mmio_data.ice_hwkm_mmio = host->ice_hwkm_mmio;
  176. #endif
  177. crypto_qti_debug(&mmio_data);
  178. }
  179. /* Poll until all BIST bits are reset */
  180. static int qcom_ice_wait_bist_status(struct ufs_qcom_host *host)
  181. {
  182. int count;
  183. u32 reg;
  184. for (count = 0; count < 100; count++) {
  185. reg = qcom_ice_readl(host, QCOM_ICE_REG_BIST_STATUS);
  186. if (!(reg & QCOM_ICE_BIST_STATUS_MASK))
  187. break;
  188. udelay(50);
  189. }
  190. if (reg)
  191. return -ETIMEDOUT;
  192. return 0;
  193. }
  194. int ufs_qcom_ice_resume(struct ufs_qcom_host *host)
  195. {
  196. int err;
  197. if (!(host->hba->caps & UFSHCD_CAP_CRYPTO))
  198. return 0;
  199. err = qcom_ice_wait_bist_status(host);
  200. if (err) {
  201. dev_err(host->hba->dev, "BIST status error (%d)\n", err);
  202. return err;
  203. }
  204. return 0;
  205. }
  206. /*
  207. * Program a key into a QC ICE keyslot, or evict a keyslot. QC ICE requires
  208. * vendor-specific SCM calls for this; it doesn't support the standard way.
  209. */
  210. int ufs_qcom_ice_program_key(struct ufs_hba *hba,
  211. const union ufs_crypto_cfg_entry *cfg, int slot)
  212. {
  213. union ufs_crypto_cap_entry cap;
  214. union {
  215. u8 bytes[AES_256_XTS_KEY_SIZE];
  216. u32 words[AES_256_XTS_KEY_SIZE / sizeof(u32)];
  217. } key;
  218. int i;
  219. int err;
  220. struct qtee_shm shm;
  221. err = qtee_shmbridge_allocate_shm(AES_256_XTS_KEY_SIZE, &shm);
  222. if (err)
  223. return -ENOMEM;
  224. if (!(cfg->config_enable & UFS_CRYPTO_CONFIGURATION_ENABLE))
  225. return qcom_scm_ice_invalidate_key(slot);
  226. /* Only AES-256-XTS has been tested so far. */
  227. cap = hba->crypto_cap_array[cfg->crypto_cap_idx];
  228. if (cap.algorithm_id != UFS_CRYPTO_ALG_AES_XTS ||
  229. cap.key_size != UFS_CRYPTO_KEY_SIZE_256) {
  230. dev_err_ratelimited(hba->dev,
  231. "Unhandled crypto capability; algorithm_id=%d, key_size=%d\n",
  232. cap.algorithm_id, cap.key_size);
  233. return -EINVAL;
  234. }
  235. memcpy(key.bytes, cfg->crypto_key, AES_256_XTS_KEY_SIZE);
  236. /*
  237. * The SCM call byte-swaps the 32-bit words of the key. So we have to
  238. * do the same, in order for the final key be correct.
  239. */
  240. for (i = 0; i < ARRAY_SIZE(key.words); i++)
  241. __cpu_to_be32s(&key.words[i]);
  242. memcpy(shm.vaddr, key.bytes, AES_256_XTS_KEY_SIZE);
  243. qtee_shmbridge_flush_shm_buf(&shm);
  244. err = qcom_scm_config_set_ice_key(slot, shm.paddr,
  245. AES_256_XTS_KEY_SIZE,
  246. QCOM_SCM_ICE_CIPHER_AES_256_XTS,
  247. cfg->data_unit_size, UFS_CE);
  248. if (err)
  249. pr_err("%s:SCM call Error: 0x%x slot %d\n",
  250. __func__, err, slot);
  251. qtee_shmbridge_inv_shm_buf(&shm);
  252. qtee_shmbridge_free_shm(&shm);
  253. memzero_explicit(&key, sizeof(key));
  254. return err;
  255. }