Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Akshay Gupta <Akshay.Gupta@amd.com>
To: <linux-hwmon@vger.kernel.org>, <devicetree@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <linux-doc@vger.kernel.org>
Cc: <linux@roeck-us.net>, <robh@kernel.org>, <krzk+dt@kernel.org>,
	<conor+dt@kernel.org>, <corbet@lwn.net>,
	<skhan@linuxfoundation.org>, <rdunlap@infradead.org>,
	<arnd@arndb.de>, <gregkh@linuxfoundation.org>,
	<naveenkrishna.chatradhi@amd.com>, <Anand.Umarji@amd.com>,
	Prathima Lk <Prathima.Lk@amd.com>,
	Akshay Gupta <Akshay.Gupta@amd.com>
Subject: [PATCH v1 3/4] misc: amd-sbi: add DIMM thermal sensor support
Date: Mon, 16 Nov 2026 17:32:27 +0530	[thread overview]
Message-ID: <20261116120228.865196-4-Akshay.Gupta@amd.com> (raw)
In-Reply-To: <20261116120228.865196-1-Akshay.Gupta@amd.com>

From: Prathima Lk <Prathima.Lk@amd.com>

Report the TS0 and TS1 thermal sensors of up to 16 DIMMs as 32 hwmon
temperature channels using SB-RMI mailbox command 0x48 with the Mode 1
DIMM_ADDRESS encoding:

  bit[7]   1 (Mode 1)
  bit[6]   sensor select (0 = TS0, 1 = TS1)
  bit[5]   reserved
  bit[4]   DIMM slot
  bit[3:0] memory channel

Board DIMM n is reported on temp(n+1) (TS0) and temp(n+17) (TS1), where
n = memory channel * DIMMs-per-channel + slot. With one DIMM per channel
n is the memory channel; with two, the slots interleave (0x80, 0x90,
0x81, ...). Each channel is labelled DIMM_TS<t>_UMC<n>.

The layout comes from two optional firmware properties:

 - amd,dpc: DIMMs per channel, 1 or 2 (default 1).
 - amd,dimm-ids: the DIMM_ADDRESS of each sensor to report. Only the
   listed sensors are exposed. Entries without bit[7], with the
   reserved bit[5], with a slot bit when amd,dpc is 1, with a memory
   channel above 7 when amd,dpc is 2, or listed twice are rejected,
   as is an empty or unreadable property.

Without amd,dimm-ids all 32 channels are exposed, and reading a sensor
that is not fitted returns the firmware error.

The temperature is returned in DataOut[31:21] as an 11-bit two's
complement value in 0.25 degC steps and is reported in millidegrees
Celsius.

Signed-off-by: Prathima Lk <Prathima.Lk@amd.com>
Signed-off-by: Akshay Gupta <Akshay.Gupta@amd.com>
---
 drivers/misc/amd-sbi/rmi-core.h  |  10 ++
 drivers/misc/amd-sbi/rmi-hwmon.c | 236 ++++++++++++++++++++++++++++---
 2 files changed, 228 insertions(+), 18 deletions(-)

diff --git a/drivers/misc/amd-sbi/rmi-core.h b/drivers/misc/amd-sbi/rmi-core.h
index 975ae858e9fd..15a0efa5dd02 100644
--- a/drivers/misc/amd-sbi/rmi-core.h
+++ b/drivers/misc/amd-sbi/rmi-core.h
@@ -48,6 +48,14 @@ enum sbrmi_msg_id {
 	SBRMI_WRITE_PKG_PWR_LIMIT,
 	SBRMI_READ_PKG_PWR_LIMIT,
 	SBRMI_READ_PKG_MAX_PWR_LIMIT,
+	SBRMI_READ_DIMM_THERMAL_SENSOR = 0x48,
+};
+
+/* DIMM thermal config; dimm_ids[] is indexed by hwmon channel */
+struct sbrmi_dimm_config {
+	u8 *dimm_ids;
+	const char **labels;
+	u8 dpc;			/* DIMMs per memory channel */
 };
 
 /* Each client has this additional data */
@@ -56,6 +64,8 @@ struct sbrmi_data {
 	struct regmap *regmap;
 	/* Mutex locking */
 	struct mutex lock;
+	/* DIMM thermal config (hwmon) */
+	struct sbrmi_dimm_config dimm;
 	u32 pwr_limit_max;
 	u8 dev_static_addr;
 	u8 rev;
diff --git a/drivers/misc/amd-sbi/rmi-hwmon.c b/drivers/misc/amd-sbi/rmi-hwmon.c
index f4f015605daa..f4a64a4a33a8 100644
--- a/drivers/misc/amd-sbi/rmi-hwmon.c
+++ b/drivers/misc/amd-sbi/rmi-hwmon.c
@@ -4,14 +4,109 @@
  *
  * Copyright (C) 2025 Advanced Micro Devices, Inc.
  */
+#include <linux/bitfield.h>
+#include <linux/bits.h>
 #include <linux/err.h>
 #include <linux/hwmon.h>
+#include <linux/property.h>
 #include <uapi/misc/amd-apml.h>
 #include "rmi-core.h"
 
 /* Do not allow setting negative power limit */
 #define SBRMI_PWR_MIN  0
 
+/* DIMM temperature (SB-RMI Mode 1 DIMM_ADDRESS) */
+#define DIMM_BASE_ID		0x80	/* Mode 1: Bit[7]=1 */
+#define DIMM_TS_SELECT_BIT	BIT(6)	/* 0 = TS0, 1 = TS1 */
+#define DIMM_RSVD_BIT		BIT(5)
+#define DIMM_SLOT_BIT		BIT(4)
+#define DIMM_CHAN_MASK		GENMASK(3, 0)
+#define DIMM_TEMP_MASK		GENMASK(31, 21)
+#define DIMM_CHANNELS_PER_TS	16
+#define SBRMI_MAX_DIMMS		DIMM_CHANNELS_PER_TS
+#define SBRMI_NUM_DIMM_CHANNELS	(DIMM_CHANNELS_PER_TS * 2)	/* TS0 + TS1 */
+
+static bool sbrmi_dimm_channel_valid(const struct sbrmi_data *data, int channel)
+{
+	return data->dimm.dimm_ids && data->dimm.dimm_ids[channel];
+}
+
+/* Fill dimm_ids[] from the "amd,dimm-ids" entries, one sensor per entry */
+static int sbrmi_read_dimm_ids(struct device *dev, struct sbrmi_data *data)
+{
+	u8 raw_ids[SBRMI_NUM_DIMM_CHANNELS];
+	int count, i, ret, ch;
+	u8 addr;
+
+	count = device_property_count_u8(dev, "amd,dimm-ids");
+	if (count <= 0 || count > SBRMI_NUM_DIMM_CHANNELS) {
+		dev_err(dev, "amd,dimm-ids must have 1..%d entries, got %d\n",
+			SBRMI_NUM_DIMM_CHANNELS, count);
+		return -EINVAL;
+	}
+
+	ret = device_property_read_u8_array(dev, "amd,dimm-ids", raw_ids, count);
+	if (ret) {
+		dev_err(dev, "failed to read amd,dimm-ids: %d\n", ret);
+		return ret;
+	}
+
+	for (i = 0; i < count; i++) {
+		addr = raw_ids[i];
+		/* Board DIMM n = memory channel * dpc + slot */
+		ch = (addr & DIMM_CHAN_MASK) * data->dimm.dpc + FIELD_GET(DIMM_SLOT_BIT, addr);
+
+		if (!(addr & DIMM_BASE_ID) || (addr & DIMM_RSVD_BIT) ||
+		    ch >= SBRMI_MAX_DIMMS ||
+		    (data->dimm.dpc == 1 && (addr & DIMM_SLOT_BIT))) {
+			dev_err(dev, "invalid amd,dimm-ids[%d] = 0x%x for %u DIMM(s) per channel\n",
+				i, addr, data->dimm.dpc);
+			return -EINVAL;
+		}
+
+		if (addr & DIMM_TS_SELECT_BIT)
+			ch += DIMM_CHANNELS_PER_TS;
+		if (data->dimm.dimm_ids[ch]) {
+			dev_err(dev, "amd,dimm-ids[%d] = 0x%x duplicates 0x%x\n",
+				i, addr, data->dimm.dimm_ids[ch]);
+			return -EINVAL;
+		}
+
+		data->dimm.dimm_ids[ch] = addr;
+	}
+
+	return 0;
+}
+
+/*
+ * Build dimm_ids[], indexed by hwmon channel (TS0 0-15, TS1 16-31), from
+ * "amd,dimm-ids" or, when absent, for all 16 DIMMs.
+ */
+static int sbrmi_init_dimm_config(struct device *dev, struct sbrmi_data *data)
+{
+	int i;
+	u8 addr;
+
+	data->dimm.dimm_ids = devm_kcalloc(dev, SBRMI_NUM_DIMM_CHANNELS,
+					   sizeof(*data->dimm.dimm_ids), GFP_KERNEL);
+	if (!data->dimm.dimm_ids)
+		return -ENOMEM;
+
+	if (device_property_present(dev, "amd,dimm-ids"))
+		return sbrmi_read_dimm_ids(dev, data);
+
+	for (i = 0; i < SBRMI_MAX_DIMMS; i++) {
+		addr = DIMM_BASE_ID | i;
+		if (data->dimm.dpc == 2)
+			addr = DIMM_BASE_ID | (i & 1 ? DIMM_SLOT_BIT : 0) | (i / 2);
+
+		data->dimm.dimm_ids[i] = addr;
+		data->dimm.dimm_ids[i + DIMM_CHANNELS_PER_TS] = addr | DIMM_TS_SELECT_BIT;
+	}
+
+	return 0;
+}
+
 static int sbrmi_read(struct device *dev, enum hwmon_sensor_types type,
 		      u32 attr, int channel, long *val)
 {
@@ -22,29 +117,52 @@ static int sbrmi_read(struct device *dev, enum hwmon_sensor_types type,
 	if (!data)
 		return -ENODEV;
 
-	if (type != hwmon_power)
+	if (type != hwmon_power && type != hwmon_temp)
 		return -EINVAL;
 
-	switch (attr) {
-	case hwmon_power_input:
-		msg.cmd = SBRMI_READ_PKG_PWR_CONSUMPTION;
-		ret = rmi_mailbox_xfer(data, &msg);
-		break;
-	case hwmon_power_cap:
-		msg.cmd = SBRMI_READ_PKG_PWR_LIMIT;
-		ret = rmi_mailbox_xfer(data, &msg);
-		break;
-	case hwmon_power_cap_max:
-		msg.mb_in_out = data->pwr_limit_max;
-		ret = 0;
-		break;
-	default:
-		return -EINVAL;
+	if (type == hwmon_power) {
+		switch (attr) {
+		case hwmon_power_input:
+			msg.cmd = SBRMI_READ_PKG_PWR_CONSUMPTION;
+			ret = rmi_mailbox_xfer(data, &msg);
+			break;
+		case hwmon_power_cap:
+			msg.cmd = SBRMI_READ_PKG_PWR_LIMIT;
+			ret = rmi_mailbox_xfer(data, &msg);
+			break;
+		case hwmon_power_cap_max:
+			msg.mb_in_out = data->pwr_limit_max;
+			ret = 0;
+			break;
+		default:
+			return -EINVAL;
+		}
+	} else {
+		switch (attr) {
+		case hwmon_temp_input:
+			if (!sbrmi_dimm_channel_valid(data, channel))
+				return -EINVAL;
+			msg.cmd = SBRMI_READ_DIMM_THERMAL_SENSOR;
+			msg.mb_in_out = data->dimm.dimm_ids[channel];
+			ret = rmi_mailbox_xfer(data, &msg);
+			if (ret == -EPROTOTYPE)
+				dev_dbg(dev, "FW error code:0x%x\n",
+					msg.fw_ret_code);
+			break;
+		default:
+			return -EINVAL;
+		}
 	}
 	if (ret < 0)
 		return ret;
-	/* hwmon power attributes are in microWatt */
-	*val = (long)msg.mb_in_out * 1000;
+
+	if (type == hwmon_power) {
+		/* hwmon power attributes are in microWatt */
+		*val = (long)msg.mb_in_out * 1000;
+	} else {
+		/* 11-bit two's complement in bits 31:21, 0.25 degC per LSB */
+		*val = sign_extend32(FIELD_GET(DIMM_TEMP_MASK, msg.mb_in_out), 10) * 250;
+	}
 	return ret;
 }
 
@@ -87,21 +205,55 @@ static umode_t sbrmi_is_visible(const void *data,
 			return 0644;
 		}
 		break;
+	case hwmon_temp:
+		if (!sbrmi_dimm_channel_valid(data, channel))
+			return 0;
+		switch (attr) {
+		case hwmon_temp_input:
+		case hwmon_temp_label:
+			return 0444;
+		}
+		break;
 	default:
 		break;
 	}
 	return 0;
 }
 
+static const u32 sbrmi_temp_config[SBRMI_NUM_DIMM_CHANNELS + 1] = {
+	[0 ... (SBRMI_NUM_DIMM_CHANNELS - 1)] = HWMON_T_INPUT | HWMON_T_LABEL,
+};
+
+static const struct hwmon_channel_info sbrmi_temp_channel_info = {
+	.type = hwmon_temp,
+	.config = sbrmi_temp_config,
+};
+
 static const struct hwmon_channel_info * const sbrmi_info[] = {
 	HWMON_CHANNEL_INFO(power,
 			   HWMON_P_INPUT | HWMON_P_CAP | HWMON_P_CAP_MAX),
+	&sbrmi_temp_channel_info,
 	NULL
 };
 
+static int sbrmi_read_string(struct device *dev,
+			     enum hwmon_sensor_types type,
+			     u32 attr, int channel, const char **str)
+{
+	struct sbrmi_data *data = dev_get_drvdata(dev);
+
+	if (type != hwmon_temp || attr != hwmon_temp_label)
+		return -EOPNOTSUPP;
+	if (!sbrmi_dimm_channel_valid(data, channel))
+		return -EINVAL;
+	*str = data->dimm.labels[channel];
+	return 0;
+}
+
 static const struct hwmon_ops sbrmi_hwmon_ops = {
 	.is_visible = sbrmi_is_visible,
 	.read = sbrmi_read,
+	.read_string = sbrmi_read_string,
 	.write = sbrmi_write,
 };
 
@@ -110,9 +262,57 @@ static const struct hwmon_chip_info sbrmi_chip_info = {
 	.info = sbrmi_info,
 };
 
+/* Label each channel by its DIMM number, not by the address it queries */
+static int sbrmi_init_dimm_labels(struct device *dev, struct sbrmi_data *data)
+{
+	int ch;
+
+	data->dimm.labels = devm_kcalloc(dev, SBRMI_NUM_DIMM_CHANNELS,
+					 sizeof(*data->dimm.labels), GFP_KERNEL);
+	if (!data->dimm.labels)
+		return -ENOMEM;
+
+	for (ch = 0; ch < SBRMI_NUM_DIMM_CHANNELS; ch++) {
+		if (!data->dimm.dimm_ids[ch])
+			continue;
+		data->dimm.labels[ch] =
+			devm_kasprintf(dev, GFP_KERNEL, "DIMM_TS%d_UMC%d",
+				       ch / DIMM_CHANNELS_PER_TS,
+				       ch % DIMM_CHANNELS_PER_TS);
+		if (!data->dimm.labels[ch])
+			return -ENOMEM;
+	}
+
+	return 0;
+}
+
 int create_hwmon_sensor_device(struct device *dev, struct sbrmi_data *data)
 {
 	struct device *hwmon_dev;
+	int ret;
+
+	/* amd,dpc: dimms-per-channel, default 1 */
+	data->dimm.dpc = 1;
+	if (device_property_present(dev, "amd,dpc")) {
+		ret = device_property_read_u8(dev, "amd,dpc", &data->dimm.dpc);
+		if (ret) {
+			dev_err(dev, "failed to read amd,dpc: %d\n", ret);
+			return ret;
+		}
+	}
+	if (data->dimm.dpc != 1 && data->dimm.dpc != 2) {
+		dev_err(dev, "amd,dpc must be 1 or 2, got %u\n",
+			data->dimm.dpc);
+		return -EINVAL;
+	}
+
+	ret = sbrmi_init_dimm_config(dev, data);
+	if (ret)
+		return ret;
+
+	ret = sbrmi_init_dimm_labels(dev, data);
+	if (ret)
+		return ret;
 
 	hwmon_dev = devm_hwmon_device_register_with_info(dev, "sbrmi", data,
 							 &sbrmi_chip_info, NULL);
-- 
2.34.1


  parent reply	other threads:[~2026-11-16 12:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 12:02 [PATCH v1 0/4] misc: amd-sbi: add DIMM thermal sensors to sbrmi Akshay Gupta
2026-11-16 12:02 ` [PATCH v1 1/4] misc: amd-sbi: use guard() for data->lock mutex Akshay Gupta
2026-11-16 12:15   ` sashiko-bot
2026-11-16 12:02 ` [PATCH v1 2/4] dt-bindings: hwmon: add DIMM thermal sensor properties Akshay Gupta
2026-11-16 12:11   ` sashiko-bot
2026-11-16 12:02 ` Akshay Gupta [this message]
2026-11-16 12:13   ` [PATCH v1 3/4] misc: amd-sbi: add DIMM thermal sensor support sashiko-bot
2026-11-16 12:02 ` [PATCH v1 4/4] Documentation: hwmon: document DIMM thermal sensor channels Akshay Gupta
2026-11-16 12:08   ` sashiko-bot

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=20261116120228.865196-4-Akshay.Gupta@amd.com \
    --to=akshay.gupta@amd.com \
    --cc=Anand.Umarji@amd.com \
    --cc=Prathima.Lk@amd.com \
    --cc=arnd@arndb.de \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=naveenkrishna.chatradhi@amd.com \
    --cc=rdunlap@infradead.org \
    --cc=robh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox