[PATCH v2 3/5] platform/x86/amd/hsmp: Expose socket attributes from the platform driver

From: Erwan Velu

Date: Mon Nov 16 2026 - 10:52:51 EST


From: Erwan Velu <e.velu@xxxxxxxxxx>

The platform driver creates bare socketN attribute groups on the
amd_hsmp platform device, which only hold metrics_bin. On systems
without the HSMP ACPI object, attributes such as
pwr_current_active_freq_limit are therefore missing, although the
driver can send the same HSMP messages as the ACPI one.

Register the amd_hsmp class socketN devices from the platform driver,
as children of the amd_hsmp platform device, and drop the socketN
attribute groups they replace. The socket devices carry
hsmp_sock_groups, metrics_bin included, so all the per-socket
attributes are now available under /sys/class/amd_hsmp/socketN/.

As class devices, they are placed by the driver core in an amd_hsmp
glue directory below their parent, i.e. in
/sys/devices/platform/amd_hsmp/amd_hsmp/socketN/. To keep the existing
/sys/devices/platform/amd_hsmp/socketN/metrics_bin path working, add a
socketN symlink from the platform device to each socket device.

As the socket devices and their symlinks are now the only way
metrics_bin is exposed, failing to create one fails the probe. They are
torn down by a devres action added after hsmp_pltdrv_release(), so they
are gone before the socket array they reference is released.

Systems running HSMP protocol version 7 or later with the platform
driver (Family 1Ah model 0x00-0x0F without the HSMP ACPI object) now
see metrics_bin, as with the ACPI driver: reads return -EOPNOTSUPP and
point userspace to HSMP_IOCTL_GET_TELEMETRY_DATA, as the version 7
table does not fit in a sysfs binary attribute. It used to be hidden
there.

On a single socket Zen4 (Family 19h) server probed by the platform
driver, this gives:

$ ls -l /sys/devices/platform/amd_hsmp/
drwxr-xr-x amd_hsmp
...
lrwxrwxrwx socket0 -> amd_hsmp/socket0
$ ls -l /sys/class/amd_hsmp/
socket0 -> ../../devices/platform/amd_hsmp/amd_hsmp/socket0
$ cat /sys/class/amd_hsmp/socket0/pwr_current_active_freq_limit
3100

This system runs HSMP protocol version 5, so metrics_bin is not
visible, as before this change.

Assisted-by: claude-opus-5-5
Signed-off-by: Erwan Velu <e.velu@xxxxxxxxxx>
---
Documentation/arch/x86/amd_hsmp.rst | 8 +-
drivers/platform/x86/amd/hsmp/plat.c | 155 +++++++++++----------------
2 files changed, 67 insertions(+), 96 deletions(-)

diff --git a/Documentation/arch/x86/amd_hsmp.rst b/Documentation/arch/x86/amd_hsmp.rst
index f145ea12af99..c4459a9dddc6 100644
--- a/Documentation/arch/x86/amd_hsmp.rst
+++ b/Documentation/arch/x86/amd_hsmp.rst
@@ -75,14 +75,18 @@ e.g. /sys/devices/platform/AMDI0097:00/amd_hsmp/socket0/. For backward
compatibility, the files are also available directly under
/sys/devices/platform/AMDI0097:0X/.

+With the plat driver, the socket devices are children of the amd_hsmp
+platform device, i.e. /sys/devices/platform/amd_hsmp/amd_hsmp/socket%d/.
+For backward compatibility, /sys/devices/platform/amd_hsmp/socket%d is a
+symlink to it.
+
1. Metrics table binary sysfs

AMD MI300A MCM provides GET_METRICS_TABLE message to retrieve
most of the system management information from SMU in one go.

The metrics table is made available as hexadecimal sysfs binary file
-under per socket sysfs directory created at
-/sys/devices/platform/amd_hsmp/socket%d/metrics_bin
+/sys/class/amd_hsmp/socket%d/metrics_bin

Note: lseek() is not supported as entire metrics table is read.

diff --git a/drivers/platform/x86/amd/hsmp/plat.c b/drivers/platform/x86/amd/hsmp/plat.c
index c3dc7c9f45ac..edcfe82aab2a 100644
--- a/drivers/platform/x86/amd/hsmp/plat.c
+++ b/drivers/platform/x86/amd/hsmp/plat.c
@@ -12,7 +12,6 @@
#include <asm/amd/hsmp.h>

#include <linux/acpi.h>
-#include <linux/build_bug.h>
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
@@ -48,98 +47,6 @@ static int amd_hsmp_pci_rdwr(struct hsmp_socket *sock, u32 offset,
return amd_smn_hsmp_rdwr(sock->sock_ind, sock->mbinfo.base_addr + offset, value, write);
}

-static ssize_t hsmp_metric_tbl_plat_read(struct file *filp, struct kobject *kobj,
- const struct bin_attribute *bin_attr, char *buf,
- loff_t off, size_t count)
-{
- struct hsmp_socket *sock;
- u16 sock_ind;
-
- sock_ind = (uintptr_t)bin_attr->private;
- if (sock_ind >= hsmp_pdev->num_sockets)
- return -EINVAL;
-
- sock = &hsmp_pdev->sock[sock_ind];
-
- return hsmp_metric_tbl_read(sock, buf, count);
-}
-
-static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
- const struct bin_attribute *battr, int id)
-{
- u16 sock_ind;
-
- sock_ind = (uintptr_t)battr->private;
-
- if (id == 0 && sock_ind >= hsmp_pdev->num_sockets)
- return SYSFS_GROUP_INVISIBLE;
-
- if (hsmp_pdev->proto_ver == HSMP_PROTO_VER6)
- return battr->attr.mode;
-
- return 0;
-}
-
-/*
- * AMD supports maximum of 8 sockets in a system.
- * Static array of 8 + 1(for NULL) elements is created below
- * to create sysfs groups for sockets.
- * is_bin_visible function is used to show / hide the necessary groups.
- *
- * Validate the maximum number against MAX_AMD_NUM_NODES. If this changes,
- * then the attributes and groups below must be adjusted.
- */
-static_assert(MAX_AMD_NUM_NODES == 8);
-
-#define HSMP_BIN_ATTR(index, _list) \
-static const struct bin_attribute attr##index = { \
- .attr = { .name = HSMP_METRICS_TABLE_NAME, .mode = 0444}, \
- .private = (void *)index, \
- .read = hsmp_metric_tbl_plat_read, \
- .size = sizeof(struct hsmp_metric_table), \
-}; \
-static const struct bin_attribute _list[] = { \
- &attr##index, \
- NULL \
-}
-
-HSMP_BIN_ATTR(0, *sock0_attr_list);
-HSMP_BIN_ATTR(1, *sock1_attr_list);
-HSMP_BIN_ATTR(2, *sock2_attr_list);
-HSMP_BIN_ATTR(3, *sock3_attr_list);
-HSMP_BIN_ATTR(4, *sock4_attr_list);
-HSMP_BIN_ATTR(5, *sock5_attr_list);
-HSMP_BIN_ATTR(6, *sock6_attr_list);
-HSMP_BIN_ATTR(7, *sock7_attr_list);
-
-#define HSMP_BIN_ATTR_GRP(index, _list, _name) \
-static const struct attribute_group sock##index##_attr_grp = { \
- .bin_attrs = _list, \
- .is_bin_visible = hsmp_is_sock_attr_visible, \
- .name = #_name, \
-}
-
-HSMP_BIN_ATTR_GRP(0, sock0_attr_list, socket0);
-HSMP_BIN_ATTR_GRP(1, sock1_attr_list, socket1);
-HSMP_BIN_ATTR_GRP(2, sock2_attr_list, socket2);
-HSMP_BIN_ATTR_GRP(3, sock3_attr_list, socket3);
-HSMP_BIN_ATTR_GRP(4, sock4_attr_list, socket4);
-HSMP_BIN_ATTR_GRP(5, sock5_attr_list, socket5);
-HSMP_BIN_ATTR_GRP(6, sock6_attr_list, socket6);
-HSMP_BIN_ATTR_GRP(7, sock7_attr_list, socket7);
-
-static const struct attribute_group *hsmp_groups[] = {
- &sock0_attr_grp,
- &sock1_attr_grp,
- &sock2_attr_grp,
- &sock3_attr_grp,
- &sock4_attr_grp,
- &sock5_attr_grp,
- &sock6_attr_grp,
- &sock7_attr_grp,
- NULL
-};
-
static inline bool is_f1a_m0h(void)
{
if (boot_cpu_data.x86 == 0x1A && boot_cpu_data.x86_model <= 0x0F)
@@ -222,9 +129,49 @@ static void hsmp_pltdrv_release(void *data)
hsmp_pdev->sock = NULL;
}

+/*
+ * The amd_hsmp class devices live in an amd_hsmp glue directory below the
+ * platform device, i.e. /sys/devices/platform/amd_hsmp/amd_hsmp/socket%u.
+ * /sys/devices/platform/amd_hsmp/socket%u used to be a plain attribute group
+ * holding metrics_bin, so keep that path working with a symlink to the socket
+ * device.
+ */
+static int hsmp_pltdrv_sock_dev_register(struct device *dev, struct hsmp_socket *sock)
+{
+ int ret;
+
+ ret = hsmp_sock_dev_register(dev, sock);
+ if (ret)
+ return ret;
+
+ ret = sysfs_create_link(&dev->kobj, &sock->sysfs_dev->kobj,
+ dev_name(sock->sysfs_dev));
+ if (ret)
+ hsmp_sock_dev_unregister(sock);
+
+ return ret;
+}
+
+static void hsmp_pltdrv_sock_devs_release(void *data)
+{
+ struct device *dev = data;
+ struct hsmp_socket *sock;
+ u16 i;
+
+ for (i = 0; i < hsmp_pdev->num_sockets; i++) {
+ sock = &hsmp_pdev->sock[i];
+ if (!sock->sysfs_dev)
+ continue;
+
+ sysfs_remove_link(&dev->kobj, dev_name(sock->sysfs_dev));
+ hsmp_sock_dev_unregister(sock);
+ }
+}
+
static int hsmp_pltdrv_probe(struct platform_device *pdev)
{
int ret;
+ u16 i;

hsmp_pdev->sock = devm_kzalloc_objs(&pdev->dev, *hsmp_pdev->sock,
hsmp_pdev->num_sockets);
@@ -252,6 +199,27 @@ static int hsmp_pltdrv_probe(struct platform_device *pdev)
return ret;
}

+ /*
+ * Added after hsmp_pltdrv_release() so that devres removes the socket
+ * devices before the socket array they reference is released.
+ */
+ ret = devm_add_action_or_reset(&pdev->dev, hsmp_pltdrv_sock_devs_release, &pdev->dev);
+ if (ret)
+ return ret;
+
+ /*
+ * The socket devices and their symlinks are the only way this driver
+ * exposes metrics_bin, so failing to create one fails the probe.
+ */
+ for (i = 0; i < hsmp_pdev->num_sockets; i++) {
+ ret = hsmp_pltdrv_sock_dev_register(&pdev->dev, &hsmp_pdev->sock[i]);
+ if (ret) {
+ dev_err(&pdev->dev, "Failed to register HSMP socket%u device: %d\n",
+ i, ret);
+ return ret;
+ }
+ }
+
ret = hsmp_misc_register(&pdev->dev);
if (ret) {
dev_err(&pdev->dev, "Failed to register misc device\n");
@@ -272,7 +240,6 @@ static struct platform_driver amd_hsmp_driver = {
.remove = hsmp_pltdrv_remove,
.driver = {
.name = DRIVER_NAME,
- .dev_groups = hsmp_groups,
},
};

--
2.53.0