Skip to contentSkip to conversation

platform/x86/amd/hsmp: Per-platform enabled-message bitmaps

PATCH New Suma Hegde 1 participant 12 messages Nov 19

Subsystems (2)
lore
Share & tools
lore
author
#1 lore raw

PATCH v2 1/11 platform/x86/amd/hsmp: Fix sock_ind bounds check in hsmp_is_sock_attr_visible()

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

The bounds check in hsmp_is_sock_attr_visible() only ran when id == 0. SYSFS_GROUP_INVISIBLE is only valid for the group-visibility call (id == 0), but sock_ind must still be validated for every attribute so a later bin attribute cannot use an out-of-range socket index.

Always bounds-check sock_ind first; return SYSFS_GROUP_INVISIBLE for id == 0 and 0 otherwise.

---

Changes since v1:
- Update subject with name hsmp_is_sock_attr_visible() instead of is_bin_visible.

 drivers/platform/x86/amd/hsmp/plat.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/platform/x86/amd/hsmp/plat.c +7 -2
index e9b2b809c0f52..af8aee1fb22ba 100644
@@ -71,8 +71,13 @@ static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
 
 	sock_ind = (uintptr_t)battr->private;
 
-	if (id == 0 && sock_ind >= hsmp_pdev->num_sockets)
-		return SYSFS_GROUP_INVISIBLE;
+	/*
+	 * Always bounds-check sock_ind. SYSFS_GROUP_INVISIBLE is only
+	 * meaningful for id == 0 (group visibility); for later attributes
+	 * just hide the file.
+	 */
+	if (sock_ind >= hsmp_pdev->num_sockets)
+		return id == 0 ? SYSFS_GROUP_INVISIBLE : 0;
 
 	if (hsmp_pdev->proto_ver == HSMP_PROTO_VER6)
 		return battr->attr.mode;
Signature · 1 line
2.34.1

Suma Hegde

author
#0 lore raw

v2 cover platform/x86/amd/hsmp: Per-platform enabled-message bitmaps

From: Suma Hegde <[email protected]>

This series introduces per-socket read/write enabled-message bitmaps so the HSMP driver rejects unsupported messages before they reach the SMU. Prep patches fix sock_ind bounds checking, move proto_ver onto hsmp_socket, and reorder helpers for the validation path. Later patches populate the bitmaps statically and via HSMP_GET_ENABLED_HSMP_CMDS (6th Gen AMD EPYC Server and future platforms), and update sysfs/hwmon file modes from the bitmaps.

Client platforms skip bitmap init, populate, and validation until dedicated client tables exist; shared smu_fw_version and protocol_version sysfs attributes remain visible there.

Changes since v1:
- Named HSMP_MSG_BIT() static bitmaps; zen6 PQOS bits added and HSMP_SET_XGMI_PSTATE_RANGE dropped from the zen6 write bitmap.
- validate_message() runs only after sock->dev is ready.
- Safer dynamic bitmap fetch helpers and default-bitmap fallback.
- Skip hwmon registration on client platforms in the plat probe path.
- Keep the short client-attribute visibility comment in the modes patch.
- Commit message / tagging cleanups (Co-developed-by, defensive null-check wording, typos).
- See per-patch "Changes since v1" for details.

Patch summary:

  01/11  Fix sock_ind bounds check in hsmp_is_sock_attr_visible()
  02/11  Move proto_ver to hsmp_socket structure
  03/11  Null-check drvdata in hsmp_is_sock_attr_visible()
  04/11  Move is_get_msg() before validate_message()
  05/11  Use local sock pointer in init_acpi()
  06/11  Move the sock_ind bounds check before validate_message()
  07/11  Add per-platform bitmap to validate HSMP messages
  08/11  Populate the bitmaps dynamically on 6th Gen AMD EPYC Server
  09/11  Populate the bitmaps dynamically for future platforms
  10/11  Relocate to_hsmp_sys_attr() macro
  11/11  Update sysfs and hwmon file modes based on platform capabilities

Suma Hegde (11):
platform/x86/amd/hsmp: Fix sock_ind bounds check in
hsmp_is_sock_attr_visible()
platform/x86/amd/hsmp: Move proto_ver to hsmp_socket structure platform/x86/amd/hsmp: Null-check drvdata in
hsmp_is_sock_attr_visible()
platform/x86/amd/hsmp: Move is_get_msg() before validate_message() platform/x86/amd/hsmp: Use local sock pointer in init_acpi() platform/x86/amd/hsmp: Move the sock_ind bounds check before
validate_message()
platform/x86/amd/hsmp: Add per-platform bitmap to validate HSMP
messages
platform/x86/amd/hsmp: Populate the bitmaps dynamically on 6th Gen AMD
EPYC Server
platform/x86/amd/hsmp: Populate the bitmaps dynamically for future
platforms
platform/x86/amd/hsmp: Relocate to_hsmp_sys_attr() macro platform/x86/amd/hsmp: Update sysfs and hwmon file modes based on
platform capabilities

 drivers/platform/x86/amd/hsmp/acpi.c  |  94 +++++-
 drivers/platform/x86/amd/hsmp/hsmp.c  | 433 +++++++++++++++++++++++++-
 drivers/platform/x86/amd/hsmp/hsmp.h  |  29 +-
 drivers/platform/x86/amd/hsmp/hwmon.c |  24 +-
 drivers/platform/x86/amd/hsmp/plat.c  |  36 ++-
 5 files changed, 578 insertions(+), 38 deletions(-)

base-commit: c339f4010dea8b0d754f84ace70cd1b089878ef8 (rebased on platform-drivers-x86 master branch)

Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/11 platform/x86/amd/hsmp: Move proto_ver to hsmp_socket structure

From: Suma Hegde <[email protected]>

Move proto_ver from hsmp_plat_device to hsmp_socket, as the protocol version is fetched per socket. Update all users to read the per-socket proto_ver.

---

Changes since v1:
- Add missing Co-developed-by for Dhruv Gupta to match his Signed-off-by.

 drivers/platform/x86/amd/hsmp/acpi.c | 14 +++++++++-----
 drivers/platform/x86/amd/hsmp/hsmp.c |  2 +-
 drivers/platform/x86/amd/hsmp/hsmp.h |  2 +-
 drivers/platform/x86/amd/hsmp/plat.c |  4 ++--
 4 files changed, 13 insertions(+), 9 deletions(-)
drivers/platform/x86/amd/hsmp/acpi.c +9 -5
index 167c76c13ad07..44db76308ceb9 100644
@@ -286,7 +286,7 @@ static ssize_t hsmp_metric_tbl_acpi_read(struct file *filp, struct kobject *kobj
 	 * Surface the unsupported case here as -EOPNOTSUPP rather than
 	 * silently truncating the snapshot.
 	 */
-	if (hsmp_pdev->proto_ver != HSMP_PROTO_VER6)
+	if (sock->proto_ver != HSMP_PROTO_VER6)
 		return -EOPNOTSUPP;
 
 	return hsmp_metric_tbl_read(sock, buf, count);
@@ -302,7 +302,10 @@ static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
 	 * pointed at HSMP_IOCTL_GET_TELEMETRY_DATA as the supported path.
 	 * Hide metrics_bin on client platforms.
 	 */
-	if (!is_client_platform() && hsmp_pdev->proto_ver >= HSMP_PROTO_VER6)
+	struct device *dev = kobj_to_dev(kobj);
+	struct hsmp_socket *sock = dev_get_drvdata(dev);
+
+	if (!is_client_platform() && sock->proto_ver >= HSMP_PROTO_VER6)
 		return battr->attr.mode;
 
 	return 0;
@@ -574,8 +577,10 @@ static int init_acpi(struct device *dev)
 	 * version enum rather than relying on numeric coincidence
 	 * between the two.
 	 */
-	if ((is_client_platform() && hsmp_pdev->proto_ver >= RYZEN_MASTER_PROTO_VER1) ||
-	    (!is_client_platform() && hsmp_pdev->proto_ver >= HSMP_PROTO_VER6)) {
+	if ((is_client_platform() &&
+	     hsmp_pdev->sock[sock_ind].proto_ver >= RYZEN_MASTER_PROTO_VER1) ||
+	    (!is_client_platform() &&
+	     hsmp_pdev->sock[sock_ind].proto_ver >= HSMP_PROTO_VER6)) {
 		ret = hsmp_get_tbl_dram_base(sock_ind);
 		if (ret)
 			dev_info(dev, "Failed to init metric table\n");
@@ -684,7 +689,6 @@ static void hsmp_acpi_sock_release(struct kref *kref)
 	kfree(hsmp_pdev->sock);
 	hsmp_pdev->sock = NULL;
 	hsmp_pdev->num_sockets = 0;
-	hsmp_pdev->proto_ver = 0;
 }
 
 /**
drivers/platform/x86/amd/hsmp/hsmp.c +1 -1
index 45d20beda9464..3a8b8697e62b9 100644
@@ -805,7 +805,7 @@ int hsmp_cache_proto_ver(u16 sock_ind)
 
 	ret = hsmp_send_message_locked(&msg);
 	if (!ret)
-		hsmp_pdev.proto_ver = msg.args[0];
+		hsmp_pdev.sock[sock_ind].proto_ver = msg.args[0];
 
 	return ret;
 }
drivers/platform/x86/amd/hsmp/hsmp.h +1 -1
index 3302c15173695..5b05dcb074846 100644
@@ -77,13 +77,13 @@ struct hsmp_socket {
 	char name[HSMP_ATTR_GRP_NAME_SIZE];
 	struct device *dev;
 	u16 sock_ind;
+	u32 proto_ver;
 	int (*amd_hsmp_rdwr)(struct hsmp_socket *sock, u32 off, u32 *val, bool rw);
 };
 
 struct hsmp_plat_device {
 	struct miscdevice mdev;
 	struct hsmp_socket *sock;
-	u32 proto_ver;
 	u16 num_sockets;
 };
 
drivers/platform/x86/amd/hsmp/plat.c +2 -2
index af8aee1fb22ba..40c98d9c66d4f 100644
@@ -79,7 +79,7 @@ static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
 	if (sock_ind >= hsmp_pdev->num_sockets)
 		return id == 0 ? SYSFS_GROUP_INVISIBLE : 0;
 
-	if (hsmp_pdev->proto_ver == HSMP_PROTO_VER6)
+	if (hsmp_pdev->sock[sock_ind].proto_ver == HSMP_PROTO_VER6)
 		return battr->attr.mode;
 
 	return 0;
@@ -193,7 +193,7 @@ static int init_platform_device(struct device *dev)
 			return ret;
 		}
 
-		if (hsmp_pdev->proto_ver == HSMP_PROTO_VER6) {
+		if (sock->proto_ver == HSMP_PROTO_VER6) {
 			ret = hsmp_get_tbl_dram_base(i);
 			if (ret)
 				dev_info(dev, "Failed to init metric table\n");
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 3/11 platform/x86/amd/hsmp: Null-check drvdata in hsmp_is_sock_attr_visible()

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

dev_get_drvdata() is not expected to return NULL when this visibility callback runs: the driver core evaluates it from really_probe() via device_add_groups() after probe has set the socket pointer. Check for NULL anyway before reading sock->proto_ver.

---

Changes since v1:
- Align the commit message with the defensive null-check comment:
note that drvdata is not expected to be NULL when the visibility callback runs from really_probe()/device_add_groups(), but still check it.
- Assign sock from dev_get_drvdata() next to the null check instead of in the declaration as per review comment.

 drivers/platform/x86/amd/hsmp/acpi.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/platform/x86/amd/hsmp/acpi.c +10 -1
index 44db76308ceb9..223dece3b91e0 100644
@@ -303,7 +303,16 @@ static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
 	 * Hide metrics_bin on client platforms.
 	 */
 	struct device *dev = kobj_to_dev(kobj);
-	struct hsmp_socket *sock = dev_get_drvdata(dev);
+	struct hsmp_socket *sock;
+
+	/*
+	 * Defensive only: the driver core evaluates this callback from
+	 * really_probe() via device_add_groups(), after probe has already
+	 * set the driver data, so sock is never NULL here in practice.
+	 */
+	sock = dev_get_drvdata(dev);
+	if (!sock)
+		return 0;
 
 	if (!is_client_platform() && sock->proto_ver >= HSMP_PROTO_VER6)
 		return battr->attr.mode;
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v2 4/11 platform/x86/amd/hsmp: Move is_get_msg() before validate_message()

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

An upcoming patch invokes is_get_msg() inside validate_message().

---

Changes since v1:
- None.

 drivers/platform/x86/amd/hsmp/hsmp.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/platform/x86/amd/hsmp/hsmp.c +11 -11
index 3a8b8697e62b9..ad5b3fde54caf 100644
@@ -277,6 +277,17 @@ static int __hsmp_send_message(struct hsmp_socket *sock, struct hsmp_message *ms
 	return ret;
 }
 
+static bool is_get_msg(const struct hsmp_msg_desc *desc, struct hsmp_message *msg)
+{
+	if (desc->type == HSMP_GET)
+		return true;
+
+	if (desc->type == HSMP_SET_GET && (msg->args[0] & CHECK_GET_BIT))
+		return true;
+
+	return false;
+}
+
 static int validate_message(struct hsmp_message *msg)
 {
 	const struct hsmp_plat_desc *plat_desc = hsmp_desc();
@@ -449,17 +460,6 @@ int hsmp_test(u16 sock_ind, u32 value)
 }
 EXPORT_SYMBOL_NS_GPL(hsmp_test, "AMD_HSMP");
 
-static bool is_get_msg(const struct hsmp_msg_desc *desc, struct hsmp_message *msg)
-{
-	if (desc->type == HSMP_GET)
-		return true;
-
-	if (desc->type == HSMP_SET_GET && (msg->args[0] & CHECK_GET_BIT))
-		return true;
-
-	return false;
-}
-
 static long hsmp_ioctl_msg(struct file *fp, unsigned long arg)
 {
 	int __user *arguser = (int  __user *)arg;
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v2 5/11 platform/x86/amd/hsmp: Use local sock pointer in init_acpi()

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

Introduce a local struct hsmp_socket *sock pointer in init_acpi() to avoid repeated use of &hsmp_pdev->sock[sock_ind]. The subsequent patch adds more users of this pointer beyond dev_set_drvdata().

---

Changes since v1:
- None.

 drivers/platform/x86/amd/hsmp/acpi.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/platform/x86/amd/hsmp/acpi.c +6 -3
index 223dece3b91e0..eee66825eb904 100644
@@ -547,6 +547,7 @@ static ssize_t hsmp_freq_limit_source_show(struct device *dev, struct device_att
  */
 static int init_acpi(struct device *dev)
 {
+	struct hsmp_socket *sock;
 	u16 sock_ind;
 	int ret;
 
@@ -564,6 +565,8 @@ static int init_acpi(struct device *dev)
 		return ret;
 	}
 
+	sock = &hsmp_pdev->sock[sock_ind];
+
 	/* Test the hsmp interface */
 	ret = hsmp_test(sock_ind, 0xDEADBEEF);
 	if (ret) {
@@ -587,9 +590,9 @@ static int init_acpi(struct device *dev)
 	 * between the two.
 	 */
 	if ((is_client_platform() &&
-	     hsmp_pdev->sock[sock_ind].proto_ver >= RYZEN_MASTER_PROTO_VER1) ||
+	     sock->proto_ver >= RYZEN_MASTER_PROTO_VER1) ||
 	    (!is_client_platform() &&
-	     hsmp_pdev->sock[sock_ind].proto_ver >= HSMP_PROTO_VER6)) {
+	     sock->proto_ver >= HSMP_PROTO_VER6)) {
 		ret = hsmp_get_tbl_dram_base(sock_ind);
 		if (ret)
 			dev_info(dev, "Failed to init metric table\n");
@@ -602,7 +605,7 @@ static int init_acpi(struct device *dev)
 			dev_info(dev, "Failed to register HSMP sensors with hwmon\n");
 	}
 
-	dev_set_drvdata(dev, &hsmp_pdev->sock[sock_ind]);
+	dev_set_drvdata(dev, sock);
 
 	return 0;
 }
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v2 8/11 platform/x86/amd/hsmp: Populate the bitmaps dynamically on 6th Gen AMD EPYC Server

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

HSMP_GET_ENABLED_HSMP_CMDS(0x37) returns the read/get and write/set messages supported on the running platform, and correctly reflects BIOS-masked messages in its bitmap.

6th Gen AMD EPYC Server supports this HSMP_GET_ENABLED_HSMP_CMDS message. On this generation, SMU returns only BIOS-masked message info, so AND the firmware bitmap with the static table. If the firmware query fails, keep the static bitmap.

Use hsmp_send_message_locked() when fetching the bitmaps during probe, as the caller already holds hsmp_sock_rwsem for write.

---

Changes since v1:
- Replace separate read/write fetch helpers with a single hsmp_fetch_enabled_msgs() helper parameterized by mask select.
- Clarify the commit message: zen6 AND-with-static workaround, and keep the static bitmap if the firmware query fails.

 drivers/platform/x86/amd/hsmp/hsmp.c | 47 ++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
drivers/platform/x86/amd/hsmp/hsmp.c +47 -0
index 085edd2ed3d6f..ccd874334468e 100644
@@ -1075,6 +1075,52 @@ static const u32 write_bitmap_zen6_variants[] = {
 #define set_write_enabled_msg_bitmap(sock, src)	\
 	bitmap_from_arr32((sock)->enabled_write_msg_ids, (src), HSMP_MAX_MSGS)
 
+#define HSMP_GET_ENABLED_CMD_READ_MASK	1
+#define HSMP_GET_ENABLED_CMD_WRITE_MASK	0
+
+static int hsmp_fetch_enabled_msgs(u16 sock_ind, u32 mask_sel, unsigned long *msg_bmap)
+{
+	struct hsmp_message msg = {};
+	int ret;
+
+	msg.msg_id      = HSMP_GET_ENABLED_HSMP_CMDS;
+	msg.num_args    = hsmp_msg_desc_table[HSMP_GET_ENABLED_HSMP_CMDS].num_args;
+	msg.response_sz = hsmp_msg_desc_table[HSMP_GET_ENABLED_HSMP_CMDS].response_sz;
+	msg.args[0]     = mask_sel;
+	msg.sock_ind    = sock_ind;
+	ret = hsmp_send_message_locked(&msg);
+	if (!ret)
+		bitmap_from_arr32(msg_bmap, msg.args, HSMP_MAX_MSGS);
+
+	return ret;
+}
+
+static void hsmp_dynamic_bitmap_zen6(u16 sock_ind)
+{
+	struct hsmp_socket *sock = &hsmp_pdev.sock[sock_ind];
+	DECLARE_BITMAP(read_msgs_bmap, HSMP_MAX_MSGS);
+	DECLARE_BITMAP(write_msgs_bmap, HSMP_MAX_MSGS);
+
+	bitmap_zero(read_msgs_bmap, HSMP_MAX_MSGS);
+	bitmap_zero(write_msgs_bmap, HSMP_MAX_MSGS);
+	/*
+	 * On 6th Gen AMD EPYC Server variants, the SMU firmware only reports
+	 * which features are disabled by BIOS — all other bits are set to 1.
+	 * Combine the static bitmap with the SMU-returned bitmap using
+	 * bitwise AND to produce the correct feature mask.
+	 */
+	if (!hsmp_fetch_enabled_msgs(sock_ind, HSMP_GET_ENABLED_CMD_READ_MASK, read_msgs_bmap)) {
+		bitmap_and(read_msgs_bmap, read_msgs_bmap,
+			   sock->enabled_read_msg_ids, HSMP_MAX_MSGS);
+		bitmap_copy(sock->enabled_read_msg_ids, read_msgs_bmap, HSMP_MAX_MSGS);
+	}
+	if (!hsmp_fetch_enabled_msgs(sock_ind, HSMP_GET_ENABLED_CMD_WRITE_MASK, write_msgs_bmap)) {
+		bitmap_and(write_msgs_bmap, write_msgs_bmap,
+			   sock->enabled_write_msg_ids, HSMP_MAX_MSGS);
+		bitmap_copy(sock->enabled_write_msg_ids, write_msgs_bmap, HSMP_MAX_MSGS);
+	}
+}
+
 int hsmp_populate_msg_bitmaps(u16 sock_ind)
 {
 	struct hsmp_socket *sock = &hsmp_pdev.sock[sock_ind];
@@ -1117,6 +1163,7 @@ int hsmp_populate_msg_bitmaps(u16 sock_ind)
 				clear_bit(HSMP_PC6_ENABLE, sock->enabled_read_msg_ids);
 				clear_bit(HSMP_PC6_ENABLE, sock->enabled_write_msg_ids);
 			}
+			hsmp_dynamic_bitmap_zen6(sock_ind);
 			return 0;
 		}
 	}
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v2 6/11 platform/x86/amd/hsmp: Move the sock_ind bounds check before validate_message()

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

Move validate_message() in hsmp_send_message_locked() to after the socket index / hsmp_pdev.sock NULL checks and the existing sock->dev readiness check, so an out-of-range or unready socket is rejected with -ENODEV before validation runs.

The subsequent patch uses sock_ind inside validate_message().

---

Changes since v1:
- Run validate_message() after the existing sock->dev readiness check, so an unready socket is rejected with -ENODEV before validation.
- Clarify the commit message accordingly; fix "bound" -> "bounds" in the subject.

 drivers/platform/x86/amd/hsmp/hsmp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/platform/x86/amd/hsmp/hsmp.c +4 -3
index ad5b3fde54caf..9516cf182917b 100644
@@ -351,9 +351,6 @@ static int hsmp_send_message_locked(struct hsmp_message *msg)
 
 	if (!msg)
 		return -EINVAL;
-	ret = validate_message(msg);
-	if (ret)
-		return ret;
 
 	if (!hsmp_pdev.sock || msg->sock_ind >= hsmp_pdev.num_sockets)
 		return -ENODEV;
@@ -381,6 +378,10 @@ static int hsmp_send_message_locked(struct hsmp_message *msg)
 	if (!smp_load_acquire(&sock->dev))
 		return -ENODEV;
 
+	ret = validate_message(msg);
+	if (ret)
+		return ret;
+
 	ret = down_interruptible(&sock->hsmp_sem);
 	if (ret < 0)
 		return ret;
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH v2 7/11 platform/x86/amd/hsmp: Add per-platform bitmap to validate HSMP messages

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

The HSMP driver previously allowed sending any message defined in amd_hsmp.h, including ones not supported on the running platform.

Add read and write bitmaps per socket to store which HSMP messages are enabled on the platform. Add validation in validate_message() to reject unsupported messages before they reach the SMU.

Key changes:
- Introduce enabled_read_msg_ids and enabled_write_msg_ids bitmaps in struct hsmp_socket.
- Add is_read_msg_enabled() and is_write_msg_enabled() helpers in hsmp.h.
- Use is_get_msg() to distinguish read and write HSMP messages.
- Mark HSMP_TEST, HSMP_GET_PROTO_VER, and HSMP_GET_SMU_VER as always enabled during bitmap initialization, as these cannot be masked by BIOS.
- Skip bitmap init, populate, and validation on client platforms; the client message set has no per-platform bitmaps yet.
- Skip hwmon registration on client platforms in the plat probe path.

---

Changes since v1:
- Express static bitmaps with named HSMP_MSG_BIT()/BIT() entries instead of opaque hex constants, and add blank lines after the array initializers.
- Update the 6th Gen AMD EPYC Server bitmaps: add HSMP_PQOS_TRAFFIC_PRIORITY and HSMP_PQOS_FLOATING_BW; drop HSMP_SET_XGMI_PSTATE_RANGE from the write bitmap.
- Soften the static-table comment so it does not forward-reference hsmp_set_default_msg_bitmap() before that helper exists.
- Skip hwmon registration on client platforms in the plat probe path.

 drivers/platform/x86/amd/hsmp/acpi.c |  13 ++
 drivers/platform/x86/amd/hsmp/hsmp.c | 312 +++++++++++++++++++++++++++
 drivers/platform/x86/amd/hsmp/hsmp.h |  27 +++
 drivers/platform/x86/amd/hsmp/plat.c |  23 +-
 4 files changed, 371 insertions(+), 4 deletions(-)
drivers/platform/x86/amd/hsmp/acpi.c +13 -0
index eee66825eb904..893ae7be05b4b 100644
@@ -567,6 +567,11 @@ static int init_acpi(struct device *dev)
 
 	sock = &hsmp_pdev->sock[sock_ind];
 
+	if (!is_client_platform()) {
+		init_read_enabled_msg_bitmap(sock);
+		init_write_enabled_msg_bitmap(sock);
+	}
+
 	/* Test the hsmp interface */
 	ret = hsmp_test(sock_ind, 0xDEADBEEF);
 	if (ret) {
@@ -582,6 +587,14 @@ static int init_acpi(struct device *dev)
 		return ret;
 	}
 
+	if (!is_client_platform()) {
+		ret = hsmp_populate_msg_bitmaps(sock_ind);
+		if (ret) {
+			dev_err(dev, "Failed to get enabled HSMP messages on this platform\n");
+			return ret;
+		}
+	}
+
 	/*
 	 * proto_ver holds the Ryzen Master interface version on client
 	 * platforms, a separate numbering space from enum
drivers/platform/x86/amd/hsmp/hsmp.c +312 -0

336 lines not on this page · raw message

drivers/platform/x86/amd/hsmp/hsmp.h +27 -0
index 5b05dcb074846..ee2bb95b66221 100644
@@ -13,6 +13,8 @@
 #include <asm/amd/hsmp.h>
 
 #include <linux/acpi.h>
+#include <linux/bitmap.h>
+#include <linux/bitops.h>
 #include <linux/compiler_types.h>
 #include <linux/device.h>
 #include <linux/hwmon.h>
@@ -26,6 +28,8 @@
 #include <linux/sysfs.h>
 #include <linux/types.h>
 
+#define HSMP_MAX_MSGS			96
+
 #define HSMP_METRICS_TABLE_NAME	"metrics_bin"
 
 #define HSMP_ATTR_GRP_NAME_SIZE	10
@@ -78,6 +82,8 @@ struct hsmp_socket {
 	struct device *dev;
 	u16 sock_ind;
 	u32 proto_ver;
+	DECLARE_BITMAP(enabled_read_msg_ids, HSMP_MAX_MSGS);
+	DECLARE_BITMAP(enabled_write_msg_ids, HSMP_MAX_MSGS);
 	int (*amd_hsmp_rdwr)(struct hsmp_socket *sock, u32 off, u32 *val, bool rw);
 };
 
@@ -87,7 +93,28 @@ struct hsmp_plat_device {
 	u16 num_sockets;
 };
 
+/* HSMP_TEST, HSMP_GET_PROTO_VER, HSMP_GET_SMU_VER are always enabled in
+ * all the platforms and can't be masked by BIOS. Set those bits in the
+ * bitmap during bitmap initialization.
+ */
+#define init_read_enabled_msg_bitmap(sock)					\
+	do {									\
+		bitmap_zero((sock)->enabled_read_msg_ids, HSMP_MAX_MSGS);	\
+		bitmap_set((sock)->enabled_read_msg_ids, HSMP_TEST, 1);		\
+		bitmap_set((sock)->enabled_read_msg_ids, HSMP_GET_PROTO_VER, 1);\
+		bitmap_set((sock)->enabled_read_msg_ids, HSMP_GET_SMU_VER, 1);	\
+	} while (0)
+
+#define init_write_enabled_msg_bitmap(sock)	\
+	bitmap_zero((sock)->enabled_write_msg_ids, HSMP_MAX_MSGS)
+
+#define is_read_msg_enabled(sock, msg_id)	\
+	test_bit((msg_id), (sock)->enabled_read_msg_ids)
+#define is_write_msg_enabled(sock, msg_id)	\
+	test_bit((msg_id), (sock)->enabled_write_msg_ids)
+
 int hsmp_cache_proto_ver(u16 sock_ind);
+int hsmp_populate_msg_bitmaps(u16 sock_ind);
 int hsmp_test(u16 sock_ind, u32 value);
 long hsmp_ioctl(struct file *fp, unsigned int cmd, unsigned long arg);
 void hsmp_misc_deregister(void);
drivers/platform/x86/amd/hsmp/plat.c +19 -4
index 40c98d9c66d4f..a0b1cac8225bc 100644
@@ -178,6 +178,11 @@ static int init_platform_device(struct device *dev)
 		sock->mbinfo.msg_arg_off	= SMN_HSMP_MSG_DATA;
 		sema_init(&sock->hsmp_sem, 1);
 
+		if (!is_client_platform()) {
+			init_read_enabled_msg_bitmap(sock);
+			init_write_enabled_msg_bitmap(sock);
+		}
+
 		/* Test the hsmp interface on each socket */
 		ret = hsmp_test(i, 0xDEADBEEF);
 		if (ret) {
@@ -193,16 +198,26 @@ static int init_platform_device(struct device *dev)
 			return ret;
 		}
 
+		if (!is_client_platform()) {
+			ret = hsmp_populate_msg_bitmaps(i);
+			if (ret) {
+				dev_err(dev, "Failed to get enabled HSMP messages on this platform\n");
+				return ret;
+			}
+		}
+
 		if (sock->proto_ver == HSMP_PROTO_VER6) {
 			ret = hsmp_get_tbl_dram_base(i);
 			if (ret)
 				dev_info(dev, "Failed to init metric table\n");
 		}
 
-		/* Register with hwmon interface for reporting power */
-		ret = hsmp_create_sensor(dev, i);
-		if (ret)
-			dev_info(dev, "Failed to register HSMP sensors with hwmon\n");
+		/* Skip hwmon registration on client platforms */
+		if (!is_client_platform()) {
+			ret = hsmp_create_sensor(dev, i);
+			if (ret)
+				dev_info(dev, "Failed to register HSMP sensors with hwmon\n");
+		}
 	}
 
 	return 0;
Signature · 1 line
2.34.1
author
#9 lore raw

PATCH v2 9/11 platform/x86/amd/hsmp: Populate the bitmaps dynamically for future platforms

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

Future AMD server platforms are expected to support the HSMP_GET_ENABLED_HSMP_CMDS (0x37) message for reporting which HSMP messages are available. This also handles detection of BIOS-enabled and BIOS-disabled messages. Instead of returning -ENODEV when no static bitmap matches the running CPU family/model, fall back to dynamically querying the enabled read and write message bitmaps via 0x37.

Add hsmp_set_default_msg_bitmap() which temporarily enables the HSMP_GET_ENABLED_HSMP_CMDS bit so that validate_message() allows the query, then fetches the read and write bitmaps from the firmware.

---

Changes since v1:
- In hsmp_set_default_msg_bitmap(), fetch data into temporary bitmaps, query the write mask before committing the read mask, and force-enable HSMP_TEST / PROTO_VER / SMU_VER / GET_ENABLED_HSMP_CMDS.

 drivers/platform/x86/amd/hsmp/hsmp.c | 51 +++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 4 deletions(-)
drivers/platform/x86/amd/hsmp/hsmp.c +47 -4
index ccd874334468e..937e7168adef9 100644
@@ -833,9 +833,8 @@ EXPORT_SYMBOL_NS_GPL(hsmp_cache_proto_ver, "AMD_HSMP");
  * HSMP_MSG_BIT(msg_id) for a supported message in that direction.
  *
  * Newer and future platforms that implement 37h do not need new entries
- * here. Unknown family/model currently returns -ENODEV from
- * hsmp_populate_msg_bitmaps(); a later patch adds a firmware query
- * fallback for those platforms.
+ * here: hsmp_populate_msg_bitmaps() falls back to querying the firmware
+ * via hsmp_set_default_msg_bitmap() when no static table matches.
  */
 #define HSMP_MSG_BIT(id)	BIT((unsigned int)(id) % 32)
 
@@ -1095,6 +1094,50 @@ static int hsmp_fetch_enabled_msgs(u16 sock_ind, u32 mask_sel, unsigned long *ms
 	return ret;
 }
 
+static int hsmp_set_default_msg_bitmap(u16 sock_ind)
+{
+	struct hsmp_socket *sock = &hsmp_pdev.sock[sock_ind];
+	DECLARE_BITMAP(read_msgs_bmap, HSMP_MAX_MSGS);
+	DECLARE_BITMAP(write_msgs_bmap, HSMP_MAX_MSGS);
+	int ret;
+
+	/* Set HSMP_GET_ENABLED_HSMP_CMDS bit to ensure validate_message() doesn't fail */
+	bitmap_set(sock->enabled_read_msg_ids, HSMP_GET_ENABLED_HSMP_CMDS, 1);
+
+	ret = hsmp_fetch_enabled_msgs(sock_ind, HSMP_GET_ENABLED_CMD_READ_MASK, read_msgs_bmap);
+	if (ret)
+		return ret;
+
+	/*
+	 * Fetch the write mask before committing the read mask to
+	 * sock->enabled_read_msg_ids: both queries use
+	 * HSMP_GET_ENABLED_HSMP_CMDS, so if the fetched read mask happened
+	 * to clear that message's own bit, committing it early would make
+	 * validate_message() reject this write query.
+	 */
+	ret = hsmp_fetch_enabled_msgs(sock_ind, HSMP_GET_ENABLED_CMD_WRITE_MASK, write_msgs_bmap);
+	if (ret)
+		return ret;
+
+	/*
+	 * HSMP_TEST, HSMP_GET_PROTO_VER and HSMP_GET_SMU_VER can't be masked
+	 * by BIOS (see init_read_enabled_msg_bitmap()), and this function
+	 * itself depends on HSMP_GET_ENABLED_HSMP_CMDS being read-enabled.
+	 * Keep all four set regardless of what firmware's reply says, so a
+	 * firmware that omits them from its reply doesn't break later
+	 * users of these messages.
+	 */
+	bitmap_set(read_msgs_bmap, HSMP_TEST, 1);
+	bitmap_set(read_msgs_bmap, HSMP_GET_PROTO_VER, 1);
+	bitmap_set(read_msgs_bmap, HSMP_GET_SMU_VER, 1);
+	bitmap_set(read_msgs_bmap, HSMP_GET_ENABLED_HSMP_CMDS, 1);
+
+	bitmap_copy(sock->enabled_read_msg_ids, read_msgs_bmap, HSMP_MAX_MSGS);
+	bitmap_copy(sock->enabled_write_msg_ids, write_msgs_bmap, HSMP_MAX_MSGS);
+
+	return 0;
+}
+
 static void hsmp_dynamic_bitmap_zen6(u16 sock_ind)
 {
 	struct hsmp_socket *sock = &hsmp_pdev.sock[sock_ind];
@@ -1167,7 +1210,7 @@ int hsmp_populate_msg_bitmaps(u16 sock_ind)
 			return 0;
 		}
 	}
-	return -ENODEV;
+	return hsmp_set_default_msg_bitmap(sock_ind);
 }
 EXPORT_SYMBOL_NS_GPL(hsmp_populate_msg_bitmaps, "AMD_HSMP");
 
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH v2 10/11 platform/x86/amd/hsmp: Relocate to_hsmp_sys_attr() macro

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

Move the to_hsmp_sys_attr() macro definition above hsmp_is_sock_attr_visible() so it is available before its first use in a subsequent patch that adds bitmap based visibility checks.

---

Changes since v1:
- None.

 drivers/platform/x86/amd/hsmp/acpi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/platform/x86/amd/hsmp/acpi.c +2 -2
index 893ae7be05b4b..a9d6da5572a39 100644
@@ -292,6 +292,8 @@ static ssize_t hsmp_metric_tbl_acpi_read(struct file *filp, struct kobject *kobj
 	return hsmp_metric_tbl_read(sock, buf, count);
 }
 
+#define to_hsmp_sys_attr(_attr) container_of(_attr, struct hsmp_sys_attr, dattr)
+
 static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
 					 const struct bin_attribute *battr, int id)
 {
@@ -335,8 +337,6 @@ static umode_t hsmp_is_sock_dev_attr_visible(struct kobject *kobj,
 	return attr->mode;
 }
 
-#define to_hsmp_sys_attr(_attr) container_of(_attr, struct hsmp_sys_attr, dattr)
-
 static ssize_t hsmp_msg_resp32_show(struct device *dev, struct device_attribute *attr,
 				    char *buf)
 {
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH v2 11/11 platform/x86/amd/hsmp: Update sysfs and hwmon file modes based on platform capabilities

In reply to Suma Hegde

From: Suma Hegde <[email protected]>

HSMP messages can be read-only, write-only, or read-write. The set of supported operations varies across AMD platforms. Older platforms may only support write, while newer ones support both read and write.

Currently, sysfs and hwmon files are created with a fixed mode based on what the latest platform supports, regardless of what the running platform actually provides.

Update the sysfs visibility callback hsmp_is_sock_dev_attr_visible() to check the per-socket read/write enabled message bitmaps and return only the permission bits corresponding to operations supported on the running platform. For files that are both readable and writable, each direction is checked independently so the mode reflects actual support.

Apply the same logic to the hwmon visibility callback hsmp_hwmon_is_visble(). Check the enabled bitmaps and return the updated mode for each power attribute.

On client platforms, keep fixed modes for the shared smu_fw_version and protocol_version attributes and hide the rest; client platforms have no enabled-message bitmaps yet.

---

Changes since v1:
- Keep the existing short client-attribute visibility comment instead of replacing it with the longer message-table comment.

 drivers/platform/x86/amd/hsmp/acpi.c  | 49 +++++++++++++++++++++++++--
 drivers/platform/x86/amd/hsmp/hwmon.c | 24 +++++++++++--
 2 files changed, 67 insertions(+), 6 deletions(-)
drivers/platform/x86/amd/hsmp/acpi.c +46 -3
index a9d6da5572a39..52e77c8bda58b 100644
@@ -16,6 +16,7 @@
 #include <linux/bits.h>
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
+#include <linux/container_of.h>
 #include <linux/device.h>
 #include <linux/dev_printk.h>
 #include <linux/ioport.h>
@@ -329,12 +330,54 @@ static struct hsmp_sys_attr hattr_protocol_version;
 static umode_t hsmp_is_sock_dev_attr_visible(struct kobject *kobj,
 					     struct attribute *attr, int id)
 {
+	struct device_attribute *dattr = container_of(attr, struct device_attribute, attr);
+	struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(dattr);
+	struct device *dev = kobj_to_dev(kobj);
+	umode_t mode = attr->mode;
+	umode_t mode_updated = 0;
+	struct hsmp_socket *sock;
+	bool readable, writable;
+
 	/* Hide every attribute except smu_fw_version and protocol_version on client platforms */
-	if (is_client_platform() && attr != &hattr_smu_fw_version.dattr.attr &&
-	    attr != &hattr_protocol_version.dattr.attr)
+	if (is_client_platform()) {
+		if (attr == &hattr_smu_fw_version.dattr.attr ||
+		    attr == &hattr_protocol_version.dattr.attr)
+			return mode;
 		return 0;
+	}
+
+	sock = dev_get_drvdata(dev);
+	if (!sock)
+		return 0;
+
+	readable = !!(mode & 0444);
+	writable = !!(mode & 0200);
 
-	return attr->mode;
+	/*
+	 * File is both readable and writeable. Some platforms support only set/write operation.
+	 * Some platforms support only get/read operation. Update the file mode accordingly.
+	 */
+	if (readable && writable) {
+		if (is_read_msg_enabled(sock, hattr->msg_id))
+			mode_updated |= mode & 0444;
+		if (is_write_msg_enabled(sock, hattr->msg_id))
+			mode_updated |= 0200;
+		return mode_updated;
+	}
+
+	/* Read-only attribute */
+	if (readable) {
+		if (is_read_msg_enabled(sock, hattr->msg_id))
+			return mode;
+	}
+
+	/* Write-only attribute */
+	if (writable) {
+		if (is_write_msg_enabled(sock, hattr->msg_id))
+			return mode;
+	}
+
+	return 0;
 }
 
 static ssize_t hsmp_msg_resp32_show(struct device *dev, struct device_attribute *attr,
drivers/platform/x86/amd/hsmp/hwmon.c +21 -3
index c8314eee06f4f..b0da511f03774 100644
@@ -80,16 +80,34 @@ static umode_t hsmp_hwmon_is_visble(const void *data,
 				    enum hwmon_sensor_types type,
 				    u32 attr, int channel)
 {
+	struct hsmp_plat_device *pdev = get_hsmp_pdev();
+	u16 sock_ind = (uintptr_t)data;
+	struct hsmp_socket *sock;
+	umode_t mode_updated = 0;
+
+	if (!pdev || !pdev->sock || sock_ind >= pdev->num_sockets)
+		return 0;
+
+	sock = &pdev->sock[sock_ind];
+
 	if (type != hwmon_power)
 		return 0;
 
 	switch (attr) {
 	case hwmon_power_input:
-		return 0444;
+		if (is_read_msg_enabled(sock, HSMP_GET_SOCKET_POWER))
+			mode_updated |= 0444;
+		return mode_updated;
 	case hwmon_power_cap:
-		return 0644;
+		if (is_read_msg_enabled(sock, HSMP_GET_SOCKET_POWER_LIMIT))
+			mode_updated |= 0444;
+		if (is_write_msg_enabled(sock, HSMP_SET_SOCKET_POWER_LIMIT))
+			mode_updated |= 0200;
+		return mode_updated;
 	case hwmon_power_cap_max:
-		return 0444;
+		if (is_read_msg_enabled(sock, HSMP_GET_SOCKET_POWER_LIMIT_MAX))
+			mode_updated |= 0444;
+		return mode_updated;
 	default:
 		return 0;
 	}
Signature · 1 line
2.34.1