Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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