Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / platform / mellanox / mlxbf-bootctl.c
blob5d21c6adf1ab6de9647a25d3fb17452d5509b2c8
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Mellanox boot control driver
5 * This driver provides a sysfs interface for systems management
6 * software to manage reset-time actions.
8 * Copyright (C) 2019 Mellanox Technologies
9 */
11 #include <linux/acpi.h>
12 #include <linux/arm-smccc.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
16 #include "mlxbf-bootctl.h"
18 #define MLXBF_BOOTCTL_SB_SECURE_MASK 0x03
19 #define MLXBF_BOOTCTL_SB_TEST_MASK 0x0c
21 #define MLXBF_SB_KEY_NUM 4
23 /* UUID used to probe ATF service. */
24 static const char *mlxbf_bootctl_svc_uuid_str =
25 "89c036b4-e7d7-11e6-8797-001aca00bfc4";
27 struct mlxbf_bootctl_name {
28 u32 value;
29 const char *name;
32 static struct mlxbf_bootctl_name boot_names[] = {
33 { MLXBF_BOOTCTL_EXTERNAL, "external" },
34 { MLXBF_BOOTCTL_EMMC, "emmc" },
35 { MLNX_BOOTCTL_SWAP_EMMC, "swap_emmc" },
36 { MLXBF_BOOTCTL_EMMC_LEGACY, "emmc_legacy" },
37 { MLXBF_BOOTCTL_NONE, "none" },
40 static const char * const mlxbf_bootctl_lifecycle_states[] = {
41 [0] = "Production",
42 [1] = "GA Secured",
43 [2] = "GA Non-Secured",
44 [3] = "RMA",
47 /* ARM SMC call which is atomic and no need for lock. */
48 static int mlxbf_bootctl_smc(unsigned int smc_op, int smc_arg)
50 struct arm_smccc_res res;
52 arm_smccc_smc(smc_op, smc_arg, 0, 0, 0, 0, 0, 0, &res);
54 return res.a0;
57 /* Return the action in integer or an error code. */
58 static int mlxbf_bootctl_reset_action_to_val(const char *action)
60 int i;
62 for (i = 0; i < ARRAY_SIZE(boot_names); i++)
63 if (sysfs_streq(boot_names[i].name, action))
64 return boot_names[i].value;
66 return -EINVAL;
69 /* Return the action in string. */
70 static const char *mlxbf_bootctl_action_to_string(int action)
72 int i;
74 for (i = 0; i < ARRAY_SIZE(boot_names); i++)
75 if (boot_names[i].value == action)
76 return boot_names[i].name;
78 return "invalid action";
81 static ssize_t post_reset_wdog_show(struct device *dev,
82 struct device_attribute *attr, char *buf)
84 int ret;
86 ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_POST_RESET_WDOG, 0);
87 if (ret < 0)
88 return ret;
90 return sprintf(buf, "%d\n", ret);
93 static ssize_t post_reset_wdog_store(struct device *dev,
94 struct device_attribute *attr,
95 const char *buf, size_t count)
97 unsigned long value;
98 int ret;
100 ret = kstrtoul(buf, 10, &value);
101 if (ret)
102 return ret;
104 ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_SET_POST_RESET_WDOG, value);
105 if (ret < 0)
106 return ret;
108 return count;
111 static ssize_t mlxbf_bootctl_show(int smc_op, char *buf)
113 int action;
115 action = mlxbf_bootctl_smc(smc_op, 0);
116 if (action < 0)
117 return action;
119 return sprintf(buf, "%s\n", mlxbf_bootctl_action_to_string(action));
122 static int mlxbf_bootctl_store(int smc_op, const char *buf, size_t count)
124 int ret, action;
126 action = mlxbf_bootctl_reset_action_to_val(buf);
127 if (action < 0)
128 return action;
130 ret = mlxbf_bootctl_smc(smc_op, action);
131 if (ret < 0)
132 return ret;
134 return count;
137 static ssize_t reset_action_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
140 return mlxbf_bootctl_show(MLXBF_BOOTCTL_GET_RESET_ACTION, buf);
143 static ssize_t reset_action_store(struct device *dev,
144 struct device_attribute *attr,
145 const char *buf, size_t count)
147 return mlxbf_bootctl_store(MLXBF_BOOTCTL_SET_RESET_ACTION, buf, count);
150 static ssize_t second_reset_action_show(struct device *dev,
151 struct device_attribute *attr,
152 char *buf)
154 return mlxbf_bootctl_show(MLXBF_BOOTCTL_GET_SECOND_RESET_ACTION, buf);
157 static ssize_t second_reset_action_store(struct device *dev,
158 struct device_attribute *attr,
159 const char *buf, size_t count)
161 return mlxbf_bootctl_store(MLXBF_BOOTCTL_SET_SECOND_RESET_ACTION, buf,
162 count);
165 static ssize_t lifecycle_state_show(struct device *dev,
166 struct device_attribute *attr, char *buf)
168 int lc_state;
170 lc_state = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_TBB_FUSE_STATUS,
171 MLXBF_BOOTCTL_FUSE_STATUS_LIFECYCLE);
172 if (lc_state < 0)
173 return lc_state;
175 lc_state &=
176 MLXBF_BOOTCTL_SB_TEST_MASK | MLXBF_BOOTCTL_SB_SECURE_MASK;
179 * If the test bits are set, we specify that the current state may be
180 * due to using the test bits.
182 if (lc_state & MLXBF_BOOTCTL_SB_TEST_MASK) {
183 lc_state &= MLXBF_BOOTCTL_SB_SECURE_MASK;
185 return sprintf(buf, "%s(test)\n",
186 mlxbf_bootctl_lifecycle_states[lc_state]);
189 return sprintf(buf, "%s\n", mlxbf_bootctl_lifecycle_states[lc_state]);
192 static ssize_t secure_boot_fuse_state_show(struct device *dev,
193 struct device_attribute *attr,
194 char *buf)
196 int burnt, valid, key, key_state, buf_len = 0, upper_key_used = 0;
197 const char *status;
199 key_state = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_TBB_FUSE_STATUS,
200 MLXBF_BOOTCTL_FUSE_STATUS_KEYS);
201 if (key_state < 0)
202 return key_state;
205 * key_state contains the bits for 4 Key versions, loaded from eFuses
206 * after a hard reset. Lower 4 bits are a thermometer code indicating
207 * key programming has started for key n (0000 = none, 0001 = version 0,
208 * 0011 = version 1, 0111 = version 2, 1111 = version 3). Upper 4 bits
209 * are a thermometer code indicating key programming has completed for
210 * key n (same encodings as the start bits). This allows for detection
211 * of an interruption in the progamming process which has left the key
212 * partially programmed (and thus invalid). The process is to burn the
213 * eFuse for the new key start bit, burn the key eFuses, then burn the
214 * eFuse for the new key complete bit.
216 * For example 0000_0000: no key valid, 0001_0001: key version 0 valid,
217 * 0011_0011: key 1 version valid, 0011_0111: key version 2 started
218 * programming but did not complete, etc. The most recent key for which
219 * both start and complete bit is set is loaded. On soft reset, this
220 * register is not modified.
222 for (key = MLXBF_SB_KEY_NUM - 1; key >= 0; key--) {
223 burnt = key_state & BIT(key);
224 valid = key_state & BIT(key + MLXBF_SB_KEY_NUM);
226 if (burnt && valid)
227 upper_key_used = 1;
229 if (upper_key_used) {
230 if (burnt)
231 status = valid ? "Used" : "Wasted";
232 else
233 status = valid ? "Invalid" : "Skipped";
234 } else {
235 if (burnt)
236 status = valid ? "InUse" : "Incomplete";
237 else
238 status = valid ? "Invalid" : "Free";
240 buf_len += sprintf(buf + buf_len, "%d:%s ", key, status);
242 buf_len += sprintf(buf + buf_len, "\n");
244 return buf_len;
247 static DEVICE_ATTR_RW(post_reset_wdog);
248 static DEVICE_ATTR_RW(reset_action);
249 static DEVICE_ATTR_RW(second_reset_action);
250 static DEVICE_ATTR_RO(lifecycle_state);
251 static DEVICE_ATTR_RO(secure_boot_fuse_state);
253 static struct attribute *mlxbf_bootctl_attrs[] = {
254 &dev_attr_post_reset_wdog.attr,
255 &dev_attr_reset_action.attr,
256 &dev_attr_second_reset_action.attr,
257 &dev_attr_lifecycle_state.attr,
258 &dev_attr_secure_boot_fuse_state.attr,
259 NULL
262 ATTRIBUTE_GROUPS(mlxbf_bootctl);
264 static const struct acpi_device_id mlxbf_bootctl_acpi_ids[] = {
265 {"MLNXBF04", 0},
269 MODULE_DEVICE_TABLE(acpi, mlxbf_bootctl_acpi_ids);
271 static bool mlxbf_bootctl_guid_match(const guid_t *guid,
272 const struct arm_smccc_res *res)
274 guid_t id = GUID_INIT(res->a0, res->a1, res->a1 >> 16,
275 res->a2, res->a2 >> 8, res->a2 >> 16,
276 res->a2 >> 24, res->a3, res->a3 >> 8,
277 res->a3 >> 16, res->a3 >> 24);
279 return guid_equal(guid, &id);
282 static int mlxbf_bootctl_probe(struct platform_device *pdev)
284 struct arm_smccc_res res = { 0 };
285 guid_t guid;
286 int ret;
288 /* Ensure we have the UUID we expect for this service. */
289 arm_smccc_smc(MLXBF_BOOTCTL_SIP_SVC_UID, 0, 0, 0, 0, 0, 0, 0, &res);
290 guid_parse(mlxbf_bootctl_svc_uuid_str, &guid);
291 if (!mlxbf_bootctl_guid_match(&guid, &res))
292 return -ENODEV;
295 * When watchdog is used, it sets boot mode to MLXBF_BOOTCTL_SWAP_EMMC
296 * in case of boot failures. However it doesn't clear the state if there
297 * is no failure. Restore the default boot mode here to avoid any
298 * unnecessary boot partition swapping.
300 ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_SET_RESET_ACTION,
301 MLXBF_BOOTCTL_EMMC);
302 if (ret < 0)
303 dev_warn(&pdev->dev, "Unable to reset the EMMC boot mode\n");
305 return 0;
308 static struct platform_driver mlxbf_bootctl_driver = {
309 .probe = mlxbf_bootctl_probe,
310 .driver = {
311 .name = "mlxbf-bootctl",
312 .dev_groups = mlxbf_bootctl_groups,
313 .acpi_match_table = mlxbf_bootctl_acpi_ids,
317 module_platform_driver(mlxbf_bootctl_driver);
319 MODULE_DESCRIPTION("Mellanox boot control driver");
320 MODULE_LICENSE("GPL v2");
321 MODULE_AUTHOR("Mellanox Technologies");