All of lore.kernel.org
 help / color / mirror / Atom feed
From: Erwan Velu <erwanaliasr1@gmail.com>
To: "Naveen Krishna Chatradhi" <naveenkrishna.chatradhi@amd.com>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Hans de Goede" <hansg@kernel.org>
Cc: Carlos Bilbao <carlos.bilbao@kernel.org>,
	Muralidhara M K <muralidhara.mk@amd.com>,
	Suma Hegde <suma.hegde@amd.com>,
	Muthusamy Ramalingam <muthusamy.ramalingam@amd.com>,
	platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org, Erwan Velu <e.velu@criteo.com>
Subject: [PATCH v2 2/5] platform/x86/amd/hsmp: Add amd_hsmp class with per-socket devices
Date: Mon, 16 Nov 2026 16:51:44 +0100	[thread overview]
Message-ID: <20261116155147.769630-3-erwanaliasr1@gmail.com> (raw)
In-Reply-To: <20261116155147.769630-1-erwanaliasr1@gmail.com>

From: Erwan Velu <e.velu@criteo.com>

The per-socket attributes live in different places depending on the
driver that probed. With the ACPI driver, they are under
/sys/devices/platform/AMDI0097:0X/. With the platform driver, the
per-socket directories are /sys/devices/platform/amd_hsmp/socketN/.
"modprobe amd_hsmp" picks the right driver, but userspace cannot know
in advance which of these trees it has to read.

Add an amd_hsmp class, and a socketN device per socket in it that
carries hsmp_sock_groups. The device is a child of the device of the
driver that probed the socket, so /sys/class/amd_hsmp/socketN/ is the
same path whichever driver is in use.

Register the socket devices from the ACPI driver. They are children of
the AMDI0097:0X devices. The attributes on AMDI0097:0X are kept, as
they are documented.

The socket device is unregistered before remove takes hsmp_sock_rwsem:
device_unregister() waits for in-flight sysfs reads, which take the
rwsem for read.

On a dual socket Zen5 (Family 1Ah) server probed by the ACPI driver,
this gives:

  $ ls -l /sys/class/amd_hsmp/
  socket0 -> ../../devices/platform/AMDI0097:00/amd_hsmp/socket0
  socket1 -> ../../devices/platform/AMDI0097:01/amd_hsmp/socket1
  $ cat /sys/class/amd_hsmp/socket0/pwr_current_active_freq_limit
  3120

The amd_hsmp directory below AMDI0097:0X is the glue directory the
driver core creates for class devices whose parent is not a class
device. The attributes directly under AMDI0097:0X are unchanged.

Assisted-by: claude-opus-5-5
Signed-off-by: Erwan Velu <e.velu@criteo.com>
---
 Documentation/arch/x86/amd_hsmp.rst   | 12 ++++-
 drivers/platform/x86/amd/hsmp/acpi.c  | 14 ++++++
 drivers/platform/x86/amd/hsmp/hsmp.c  | 19 +++++++
 drivers/platform/x86/amd/hsmp/hsmp.h  |  6 +++
 drivers/platform/x86/amd/hsmp/sysfs.c | 71 ++++++++++++++++++++++++++-
 5 files changed, 120 insertions(+), 2 deletions(-)

diff --git a/Documentation/arch/x86/amd_hsmp.rst b/Documentation/arch/x86/amd_hsmp.rst
index c483c68dcfad..f145ea12af99 100644
--- a/Documentation/arch/x86/amd_hsmp.rst
+++ b/Documentation/arch/x86/amd_hsmp.rst
@@ -65,6 +65,16 @@ In-kernel integration:
 HSMP sysfs interface
 ====================
 
+Each socket has a socket%d device in the amd_hsmp class, whose parent is the
+device of the driver that probed the socket. Its per socket sysfs files are
+therefore available at /sys/class/amd_hsmp/socket%d/, whichever driver
+probed.
+
+With the ACPI driver, the socket devices are children of the ACPI devices,
+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/.
+
 1. Metrics table binary sysfs
 
 AMD MI300A MCM provides GET_METRICS_TABLE message to retrieve
@@ -88,7 +98,7 @@ The same is defined in the amd_hsmp.h header.
 
 2. HSMP telemetry sysfs files
 
-Following sysfs files are available at /sys/devices/platform/AMDI0097:0X/.
+Following sysfs files are available at /sys/class/amd_hsmp/socket%d/.
 
 * c0_residency_input: Percentage of cores in C0 state.
 * prochot_status: Reports 1 if the processor is at thermal threshold value,
diff --git a/drivers/platform/x86/amd/hsmp/acpi.c b/drivers/platform/x86/amd/hsmp/acpi.c
index a1e13aff9c86..2c9389b8f139 100644
--- a/drivers/platform/x86/amd/hsmp/acpi.c
+++ b/drivers/platform/x86/amd/hsmp/acpi.c
@@ -452,6 +452,14 @@ static int hsmp_acpi_probe(struct platform_device *pdev)
 		dev_dbg(&pdev->dev, "AMD HSMP ACPI misc device registered\n");
 	}
 
+	/*
+	 * The attributes stay available on the ACPI device itself, so a
+	 * failure here is not fatal to the probe.
+	 */
+	ret = hsmp_sock_dev_register(&pdev->dev, dev_get_drvdata(&pdev->dev));
+	if (ret)
+		dev_warn(&pdev->dev, "Failed to register HSMP socket device: %d\n", ret);
+
 	return 0;
 }
 
@@ -459,6 +467,12 @@ static void hsmp_acpi_remove(struct platform_device *pdev)
 {
 	struct hsmp_socket *sock = dev_get_drvdata(&pdev->dev);
 
+	/*
+	 * Must run before taking hsmp_sock_rwsem below: unregistering the
+	 * class device waits for in-flight sysfs reads, which take it for read.
+	 */
+	hsmp_sock_dev_unregister(sock);
+
 	/*
 	 * Serialize the kref_put() and any release it triggers against a
 	 * concurrent probe, and drain the data plane for the whole
diff --git a/drivers/platform/x86/amd/hsmp/hsmp.c b/drivers/platform/x86/amd/hsmp/hsmp.c
index 45d20beda946..a498f904f900 100644
--- a/drivers/platform/x86/amd/hsmp/hsmp.c
+++ b/drivers/platform/x86/amd/hsmp/hsmp.c
@@ -16,7 +16,9 @@
 #include <linux/compiler.h>
 #include <linux/delay.h>
 #include <linux/device.h>
+#include <linux/init.h>
 #include <linux/io.h>
+#include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/nospec.h>
 #include <linux/rwsem.h>
@@ -857,6 +859,23 @@ struct hsmp_plat_device *get_hsmp_pdev(void)
 }
 EXPORT_SYMBOL_NS_GPL(get_hsmp_pdev, "AMD_HSMP");
 
+/*
+ * The amd_hsmp class must exist before the ACPI or platform driver probes a
+ * socket. When built in, subsys_initcall() runs it ahead of their
+ * device_initcall()s.
+ */
+static int __init hsmp_common_init(void)
+{
+	return hsmp_sock_class_register();
+}
+subsys_initcall(hsmp_common_init);
+
+static void __exit hsmp_common_exit(void)
+{
+	hsmp_sock_class_unregister();
+}
+module_exit(hsmp_common_exit);
+
 MODULE_DESCRIPTION("AMD HSMP Common driver");
 MODULE_VERSION(DRIVER_VERSION);
 MODULE_LICENSE("GPL");
diff --git a/drivers/platform/x86/amd/hsmp/hsmp.h b/drivers/platform/x86/amd/hsmp/hsmp.h
index 7a0e3af67691..35b4e2312bf2 100644
--- a/drivers/platform/x86/amd/hsmp/hsmp.h
+++ b/drivers/platform/x86/amd/hsmp/hsmp.h
@@ -32,6 +32,7 @@
 
 #define HSMP_CDEV_NAME		"hsmp_cdev"
 #define HSMP_DEVNODE_NAME	"hsmp"
+#define HSMP_CLASS_NAME		"amd_hsmp"
 #define ACPI_HSMP_DEVICE_HID    "AMDI0097"
 
 #define DRIVER_VERSION		"2.6"
@@ -71,6 +72,7 @@ struct hsmp_socket {
 	/* Size of the region mapped at @metric_tbl_addr, as reported by SMU */
 	size_t metric_tbl_size;
 	void __iomem *virt_base_addr;
+	struct device *sysfs_dev;
 	struct semaphore hsmp_sem;
 	/* Serializes HSMP_GET_METRIC_TABLE fill-and-copy for this socket */
 	struct mutex metric_read_lock;
@@ -104,6 +106,10 @@ int hsmp_create_sensor(struct device *dev, u16 sock_ind);
 static inline int hsmp_create_sensor(struct device *dev, u16 sock_ind) { return 0; }
 #endif
 int hsmp_msg_get_nargs(u16 sock_ind, u32 msg_id, u32 *data, u8 num_args);
+int hsmp_sock_class_register(void);
+void hsmp_sock_class_unregister(void);
+int hsmp_sock_dev_register(struct device *parent, struct hsmp_socket *sock);
+void hsmp_sock_dev_unregister(struct hsmp_socket *sock);
 extern const struct attribute_group *hsmp_sock_groups[];
 
 /*
diff --git a/drivers/platform/x86/amd/hsmp/sysfs.c b/drivers/platform/x86/amd/hsmp/sysfs.c
index 06c0a5a02ca2..94670c8e689b 100644
--- a/drivers/platform/x86/amd/hsmp/sysfs.c
+++ b/drivers/platform/x86/amd/hsmp/sysfs.c
@@ -4,7 +4,8 @@
  * Copyright (c) 2024, AMD.
  * All Rights Reserved.
  *
- * This file provides the per-socket sysfs attributes of the HSMP interface.
+ * This file provides the per-socket sysfs attributes of the HSMP interface
+ * and the amd_hsmp class devices carrying them.
  */
 
 #include <asm/amd/hsmp.h>
@@ -13,6 +14,9 @@
 #include <linux/bitfield.h>
 #include <linux/bits.h>
 #include <linux/device.h>
+#include <linux/err.h>
+#include <linux/kdev_t.h>
+#include <linux/lockdep.h>
 #include <linux/sysfs.h>
 
 #include "hsmp.h"
@@ -346,3 +350,68 @@ const struct attribute_group *hsmp_sock_groups[] = {
 	NULL
 };
 EXPORT_SYMBOL_NS_GPL(hsmp_sock_groups, "AMD_HSMP");
+
+/*
+ * Every socket gets a socket%u device in this class, parented to the device of
+ * the driver that probed it, whichever that driver is. This gives userspace a
+ * single /sys/class/amd_hsmp/socket%u path to the per-socket attributes.
+ */
+static const struct class hsmp_class = {
+	.name = HSMP_CLASS_NAME,
+};
+
+int hsmp_sock_class_register(void)
+{
+	return class_register(&hsmp_class);
+}
+
+void hsmp_sock_class_unregister(void)
+{
+	class_unregister(&hsmp_class);
+}
+
+/**
+ * hsmp_sock_dev_register() - Create the amd_hsmp class device of @sock.
+ * @parent: device of the driver that probed @sock.
+ * @sock: fully probed HSMP socket.
+ *
+ * The device is named socket%u after @sock's index, is a child of @parent and
+ * carries hsmp_sock_groups with @sock as its drvdata, so @sock must stay valid
+ * until hsmp_sock_dev_unregister() returns.
+ *
+ * Return: 0 on success, a negative error code otherwise.
+ */
+int hsmp_sock_dev_register(struct device *parent, struct hsmp_socket *sock)
+{
+	struct device *dev;
+
+	dev = device_create_with_groups(&hsmp_class, parent, MKDEV(0, 0), sock,
+					hsmp_sock_groups, "socket%u", sock->sock_ind);
+	if (IS_ERR(dev))
+		return PTR_ERR(dev);
+
+	sock->sysfs_dev = dev;
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(hsmp_sock_dev_register, "AMD_HSMP");
+
+/**
+ * hsmp_sock_dev_unregister() - Remove the amd_hsmp class device of @sock.
+ * @sock: HSMP socket; a no-op if its device was never registered.
+ *
+ * device_unregister() waits for in-flight show/read handlers, which take
+ * hsmp_sock_rwsem for read. The caller must therefore not hold it, or a
+ * concurrent sysfs read would deadlock the teardown.
+ */
+void hsmp_sock_dev_unregister(struct hsmp_socket *sock)
+{
+	lockdep_assert_not_held(&hsmp_sock_rwsem);
+
+	if (!sock->sysfs_dev)
+		return;
+
+	device_unregister(sock->sysfs_dev);
+	sock->sysfs_dev = NULL;
+}
+EXPORT_SYMBOL_NS_GPL(hsmp_sock_dev_unregister, "AMD_HSMP");
-- 
2.53.0


  parent reply	other threads:[~2026-11-16 15:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 15:51 [PATCH v2 0/5] platform/x86/amd/hsmp: Unify per-socket sysfs metrics Erwan Velu
2026-11-16 15:51 ` [PATCH v2 1/5] platform/x86/amd/hsmp: Move per-socket sysfs attributes to common code Erwan Velu
2026-11-16 15:51 ` Erwan Velu [this message]
2026-11-16 15:51 ` [PATCH v2 3/5] platform/x86/amd/hsmp: Expose socket attributes from the platform driver Erwan Velu
2026-11-16 15:51 ` [PATCH v2 4/5] platform/x86/amd/hsmp: Add pwr_current_active_freq_limit_source_raw Erwan Velu
2026-11-16 15:51 ` [PATCH v2 5/5] Documentation/ABI: Add sysfs-class-amd_hsmp Erwan Velu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116155147.769630-3-erwanaliasr1@gmail.com \
    --to=erwanaliasr1@gmail.com \
    --cc=carlos.bilbao@kernel.org \
    --cc=e.velu@criteo.com \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=muralidhara.mk@amd.com \
    --cc=muthusamy.ramalingam@amd.com \
    --cc=naveenkrishna.chatradhi@amd.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=suma.hegde@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.