mlxbf-bootctl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Mellanox boot control driver
  4. *
  5. * This driver provides a sysfs interface for systems management
  6. * software to manage reset-time actions.
  7. *
  8. * Copyright (C) 2019 Mellanox Technologies
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/arm-smccc.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include "mlxbf-bootctl.h"
  15. #define MLXBF_BOOTCTL_SB_SECURE_MASK 0x03
  16. #define MLXBF_BOOTCTL_SB_TEST_MASK 0x0c
  17. #define MLXBF_BOOTCTL_SB_DEV_MASK BIT(4)
  18. #define MLXBF_SB_KEY_NUM 4
  19. /* UUID used to probe ATF service. */
  20. static const char *mlxbf_bootctl_svc_uuid_str =
  21. "89c036b4-e7d7-11e6-8797-001aca00bfc4";
  22. struct mlxbf_bootctl_name {
  23. u32 value;
  24. const char *name;
  25. };
  26. static struct mlxbf_bootctl_name boot_names[] = {
  27. { MLXBF_BOOTCTL_EXTERNAL, "external" },
  28. { MLXBF_BOOTCTL_EMMC, "emmc" },
  29. { MLNX_BOOTCTL_SWAP_EMMC, "swap_emmc" },
  30. { MLXBF_BOOTCTL_EMMC_LEGACY, "emmc_legacy" },
  31. { MLXBF_BOOTCTL_NONE, "none" },
  32. };
  33. enum {
  34. MLXBF_BOOTCTL_SB_LIFECYCLE_PRODUCTION = 0,
  35. MLXBF_BOOTCTL_SB_LIFECYCLE_GA_SECURE = 1,
  36. MLXBF_BOOTCTL_SB_LIFECYCLE_GA_NON_SECURE = 2,
  37. MLXBF_BOOTCTL_SB_LIFECYCLE_RMA = 3
  38. };
  39. static const char * const mlxbf_bootctl_lifecycle_states[] = {
  40. [MLXBF_BOOTCTL_SB_LIFECYCLE_PRODUCTION] = "Production",
  41. [MLXBF_BOOTCTL_SB_LIFECYCLE_GA_SECURE] = "GA Secured",
  42. [MLXBF_BOOTCTL_SB_LIFECYCLE_GA_NON_SECURE] = "GA Non-Secured",
  43. [MLXBF_BOOTCTL_SB_LIFECYCLE_RMA] = "RMA",
  44. };
  45. /* ARM SMC call which is atomic and no need for lock. */
  46. static int mlxbf_bootctl_smc(unsigned int smc_op, int smc_arg)
  47. {
  48. struct arm_smccc_res res;
  49. arm_smccc_smc(smc_op, smc_arg, 0, 0, 0, 0, 0, 0, &res);
  50. return res.a0;
  51. }
  52. /* Return the action in integer or an error code. */
  53. static int mlxbf_bootctl_reset_action_to_val(const char *action)
  54. {
  55. int i;
  56. for (i = 0; i < ARRAY_SIZE(boot_names); i++)
  57. if (sysfs_streq(boot_names[i].name, action))
  58. return boot_names[i].value;
  59. return -EINVAL;
  60. }
  61. /* Return the action in string. */
  62. static const char *mlxbf_bootctl_action_to_string(int action)
  63. {
  64. int i;
  65. for (i = 0; i < ARRAY_SIZE(boot_names); i++)
  66. if (boot_names[i].value == action)
  67. return boot_names[i].name;
  68. return "invalid action";
  69. }
  70. static ssize_t post_reset_wdog_show(struct device *dev,
  71. struct device_attribute *attr, char *buf)
  72. {
  73. int ret;
  74. ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_POST_RESET_WDOG, 0);
  75. if (ret < 0)
  76. return ret;
  77. return sprintf(buf, "%d\n", ret);
  78. }
  79. static ssize_t post_reset_wdog_store(struct device *dev,
  80. struct device_attribute *attr,
  81. const char *buf, size_t count)
  82. {
  83. unsigned long value;
  84. int ret;
  85. ret = kstrtoul(buf, 10, &value);
  86. if (ret)
  87. return ret;
  88. ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_SET_POST_RESET_WDOG, value);
  89. if (ret < 0)
  90. return ret;
  91. return count;
  92. }
  93. static ssize_t mlxbf_bootctl_show(int smc_op, char *buf)
  94. {
  95. int action;
  96. action = mlxbf_bootctl_smc(smc_op, 0);
  97. if (action < 0)
  98. return action;
  99. return sprintf(buf, "%s\n", mlxbf_bootctl_action_to_string(action));
  100. }
  101. static int mlxbf_bootctl_store(int smc_op, const char *buf, size_t count)
  102. {
  103. int ret, action;
  104. action = mlxbf_bootctl_reset_action_to_val(buf);
  105. if (action < 0)
  106. return action;
  107. ret = mlxbf_bootctl_smc(smc_op, action);
  108. if (ret < 0)
  109. return ret;
  110. return count;
  111. }
  112. static ssize_t reset_action_show(struct device *dev,
  113. struct device_attribute *attr, char *buf)
  114. {
  115. return mlxbf_bootctl_show(MLXBF_BOOTCTL_GET_RESET_ACTION, buf);
  116. }
  117. static ssize_t reset_action_store(struct device *dev,
  118. struct device_attribute *attr,
  119. const char *buf, size_t count)
  120. {
  121. return mlxbf_bootctl_store(MLXBF_BOOTCTL_SET_RESET_ACTION, buf, count);
  122. }
  123. static ssize_t second_reset_action_show(struct device *dev,
  124. struct device_attribute *attr,
  125. char *buf)
  126. {
  127. return mlxbf_bootctl_show(MLXBF_BOOTCTL_GET_SECOND_RESET_ACTION, buf);
  128. }
  129. static ssize_t second_reset_action_store(struct device *dev,
  130. struct device_attribute *attr,
  131. const char *buf, size_t count)
  132. {
  133. return mlxbf_bootctl_store(MLXBF_BOOTCTL_SET_SECOND_RESET_ACTION, buf,
  134. count);
  135. }
  136. static ssize_t lifecycle_state_show(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. int status_bits;
  140. int use_dev_key;
  141. int test_state;
  142. int lc_state;
  143. status_bits = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_TBB_FUSE_STATUS,
  144. MLXBF_BOOTCTL_FUSE_STATUS_LIFECYCLE);
  145. if (status_bits < 0)
  146. return status_bits;
  147. use_dev_key = status_bits & MLXBF_BOOTCTL_SB_DEV_MASK;
  148. test_state = status_bits & MLXBF_BOOTCTL_SB_TEST_MASK;
  149. lc_state = status_bits & MLXBF_BOOTCTL_SB_SECURE_MASK;
  150. /*
  151. * If the test bits are set, we specify that the current state may be
  152. * due to using the test bits.
  153. */
  154. if (test_state) {
  155. return sprintf(buf, "%s(test)\n",
  156. mlxbf_bootctl_lifecycle_states[lc_state]);
  157. } else if (use_dev_key &&
  158. (lc_state == MLXBF_BOOTCTL_SB_LIFECYCLE_GA_SECURE)) {
  159. return sprintf(buf, "Secured (development)\n");
  160. }
  161. return sprintf(buf, "%s\n", mlxbf_bootctl_lifecycle_states[lc_state]);
  162. }
  163. static ssize_t secure_boot_fuse_state_show(struct device *dev,
  164. struct device_attribute *attr,
  165. char *buf)
  166. {
  167. int burnt, valid, key, key_state, buf_len = 0, upper_key_used = 0;
  168. const char *status;
  169. key_state = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_TBB_FUSE_STATUS,
  170. MLXBF_BOOTCTL_FUSE_STATUS_KEYS);
  171. if (key_state < 0)
  172. return key_state;
  173. /*
  174. * key_state contains the bits for 4 Key versions, loaded from eFuses
  175. * after a hard reset. Lower 4 bits are a thermometer code indicating
  176. * key programming has started for key n (0000 = none, 0001 = version 0,
  177. * 0011 = version 1, 0111 = version 2, 1111 = version 3). Upper 4 bits
  178. * are a thermometer code indicating key programming has completed for
  179. * key n (same encodings as the start bits). This allows for detection
  180. * of an interruption in the programming process which has left the key
  181. * partially programmed (and thus invalid). The process is to burn the
  182. * eFuse for the new key start bit, burn the key eFuses, then burn the
  183. * eFuse for the new key complete bit.
  184. *
  185. * For example 0000_0000: no key valid, 0001_0001: key version 0 valid,
  186. * 0011_0011: key 1 version valid, 0011_0111: key version 2 started
  187. * programming but did not complete, etc. The most recent key for which
  188. * both start and complete bit is set is loaded. On soft reset, this
  189. * register is not modified.
  190. */
  191. for (key = MLXBF_SB_KEY_NUM - 1; key >= 0; key--) {
  192. burnt = key_state & BIT(key);
  193. valid = key_state & BIT(key + MLXBF_SB_KEY_NUM);
  194. if (burnt && valid)
  195. upper_key_used = 1;
  196. if (upper_key_used) {
  197. if (burnt)
  198. status = valid ? "Used" : "Wasted";
  199. else
  200. status = valid ? "Invalid" : "Skipped";
  201. } else {
  202. if (burnt)
  203. status = valid ? "InUse" : "Incomplete";
  204. else
  205. status = valid ? "Invalid" : "Free";
  206. }
  207. buf_len += sprintf(buf + buf_len, "%d:%s ", key, status);
  208. }
  209. buf_len += sprintf(buf + buf_len, "\n");
  210. return buf_len;
  211. }
  212. static DEVICE_ATTR_RW(post_reset_wdog);
  213. static DEVICE_ATTR_RW(reset_action);
  214. static DEVICE_ATTR_RW(second_reset_action);
  215. static DEVICE_ATTR_RO(lifecycle_state);
  216. static DEVICE_ATTR_RO(secure_boot_fuse_state);
  217. static struct attribute *mlxbf_bootctl_attrs[] = {
  218. &dev_attr_post_reset_wdog.attr,
  219. &dev_attr_reset_action.attr,
  220. &dev_attr_second_reset_action.attr,
  221. &dev_attr_lifecycle_state.attr,
  222. &dev_attr_secure_boot_fuse_state.attr,
  223. NULL
  224. };
  225. ATTRIBUTE_GROUPS(mlxbf_bootctl);
  226. static const struct acpi_device_id mlxbf_bootctl_acpi_ids[] = {
  227. {"MLNXBF04", 0},
  228. {}
  229. };
  230. MODULE_DEVICE_TABLE(acpi, mlxbf_bootctl_acpi_ids);
  231. static bool mlxbf_bootctl_guid_match(const guid_t *guid,
  232. const struct arm_smccc_res *res)
  233. {
  234. guid_t id = GUID_INIT(res->a0, res->a1, res->a1 >> 16,
  235. res->a2, res->a2 >> 8, res->a2 >> 16,
  236. res->a2 >> 24, res->a3, res->a3 >> 8,
  237. res->a3 >> 16, res->a3 >> 24);
  238. return guid_equal(guid, &id);
  239. }
  240. static int mlxbf_bootctl_probe(struct platform_device *pdev)
  241. {
  242. struct arm_smccc_res res = { 0 };
  243. guid_t guid;
  244. int ret;
  245. /* Ensure we have the UUID we expect for this service. */
  246. arm_smccc_smc(MLXBF_BOOTCTL_SIP_SVC_UID, 0, 0, 0, 0, 0, 0, 0, &res);
  247. guid_parse(mlxbf_bootctl_svc_uuid_str, &guid);
  248. if (!mlxbf_bootctl_guid_match(&guid, &res))
  249. return -ENODEV;
  250. /*
  251. * When watchdog is used, it sets boot mode to MLXBF_BOOTCTL_SWAP_EMMC
  252. * in case of boot failures. However it doesn't clear the state if there
  253. * is no failure. Restore the default boot mode here to avoid any
  254. * unnecessary boot partition swapping.
  255. */
  256. ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_SET_RESET_ACTION,
  257. MLXBF_BOOTCTL_EMMC);
  258. if (ret < 0)
  259. dev_warn(&pdev->dev, "Unable to reset the EMMC boot mode\n");
  260. return 0;
  261. }
  262. static struct platform_driver mlxbf_bootctl_driver = {
  263. .probe = mlxbf_bootctl_probe,
  264. .driver = {
  265. .name = "mlxbf-bootctl",
  266. .dev_groups = mlxbf_bootctl_groups,
  267. .acpi_match_table = mlxbf_bootctl_acpi_ids,
  268. }
  269. };
  270. module_platform_driver(mlxbf_bootctl_driver);
  271. MODULE_DESCRIPTION("Mellanox boot control driver");
  272. MODULE_LICENSE("GPL v2");
  273. MODULE_AUTHOR("Mellanox Technologies");