Skip to contentSkip to conversation

misc: amd-sbi: Refactor SBTSI driver with I3C support and ioctl interface

PATCH In review Akshay Gupta 3 participants 21 messages Jul 10

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover misc: amd-sbi: Refactor SBTSI driver with I3C support and ioctl interface

This series refactors the AMD SB-TSI (Side-Band Temperature Sensor Interface) driver by moving the core from the hwmon subsystem into the drivers/misc/amd-sbi framework, alongside the existing SB-RMI driver. Registers an auxiliary device keeping hwmon sensors functionality intact.

Background:
The SB-TSI driver currently lives under drivers/hwmon/sbtsi_temp.c and is limited to exposing temperature readings via the hwmon interface. As AMD platforms evolve, SB-TSI access is required from multiple consumers (hwmon, userspace via ioctl, I3C-attached devices), making the hwmon-only placement insufficient.

This series restructures the driver into a layered design:

- tsi-core.c : core register access and ioctl/miscdevice support
- tsi.c : I2C/I3C probe and glue
- sbtsi_temp.c : hwmon sensor layer built on top of the core using aux device

Changes in this series:
1. Move core SBTSI driver probe from drivers/hwmon into drivers/misc/amd-sbi, and registering an auxiliary device in core for hwmon subsystem probing

2. Register order follows the device ReadOrder bit so both parts latch atomically; limit registers (temp / temp1_max / temp1_min) use the same helpers instead of separate SMBus calls.

3. Move sbtsi register transfer to core abstraction to decouple the hwmon sensor driver from the underlying bus transport. Preparing for I3C support in a subsequent patch

4. Refactor I2C probe common functionality to new common helper function to prepare for I3C support

5. Extend the driver to support SB-TSI over I3C in addition to I2C. Both buses share the same core read/write path via sbtsi_xfer(); the is_i3c flag selects the underlying transport at probe time. Backward compatibility with existing I2C deployments is maintained.

6. Add a miscdevice (/dev/sbtsi-<addr>) and an ioctl interface (SBTSI_IOCTL_REG_XFER_CMD) that allows root userspace to perform SB-TSI register read/write operations through the APML protocol, consistent with the existing SBRMI ioctl interface.

7. Add mutex for SBTSI read/write in hwmon to prevent race condition with IOCTL.

8. Document the new SBTSI miscdevice and its ioctl in Documentation/misc-devices/amd-sbi.rst.

Testing:
Tested on AMD Genoa/Turin/Venice BMC platforms with both I2C and I3C-attached SB-TSI targets. hwmon sysfs attributes (tempX_input, tempX_max, etc.) and ioctl register transfers verified against hardware.

Prathima (8):
hwmon/misc: amd-sbi: Move core sbtsi support from hwmon to misc
hwmon: sbtsi_temp: Refactor temperature register access into helpers hwmon/misc: amd-sbi: Move sbtsi register transfer to core abstraction
misc: amd-sbi: Consolidate Common SBTSI Probe Path
misc: amd-sbi: Add support for SB-TSI over I3C
misc: amd-sbi: Add SBTSI ioctl register transfer interface
hwmon: Add mutex protecting for sbtsi read/write through hwmon
docs: misc: amd-sbi: Document SBTSI userspace interface

 Documentation/misc-devices/amd-sbi.rst |  74 ++++++++
 drivers/hwmon/Kconfig                  |   2 +-
 drivers/hwmon/sbtsi_temp.c             | 152 ++++++++--------
 drivers/misc/amd-sbi/Kconfig           |  13 ++
 drivers/misc/amd-sbi/Makefile          |   3 +
 drivers/misc/amd-sbi/tsi-core.c        | 207 ++++++++++++++++++++++
 drivers/misc/amd-sbi/tsi-core.h        |  29 +++
 drivers/misc/amd-sbi/tsi.c             | 234 +++++++++++++++++++++++++
 include/linux/misc/tsi.h               |  72 ++++++++
 include/uapi/misc/amd-apml.h           |  23 +++
 10 files changed, 727 insertions(+), 82 deletions(-)
 create mode 100644 drivers/misc/amd-sbi/tsi-core.c
 create mode 100644 drivers/misc/amd-sbi/tsi-core.h
 create mode 100644 drivers/misc/amd-sbi/tsi.c
 create mode 100644 include/linux/misc/tsi.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/8 hwmon/misc: amd-sbi: Move core sbtsi support from hwmon to misc

From: Prathima <[email protected]>

Move SBTSI(Side-Band Temperature Sensor Interface) core functionality out of the hwmon-only path and into drivers/misc/amd-sbi so it can be reused by non-hwmon consumers.

I2C probe parsing is moved from drivers/hwmon/sbtsi_temp.c into drivers/misc/amd-sbi/tsi.c under CONFIG_AMD_SBTSI. The core driver stores struct sbtsi_data on the bus device and registers an auxiliary device amd-sbtsi.temp-sensor.<addr> per target.
The current hwmon temp sensor will now require the CONFIG_AMD_SBTSI configuration as a new dependency.

This split prepares the driver for additional interfaces while keeping hwmon support in hwmon subsystem on top of common SBTSI core logic.

Add platform dependencies to clarify this driver is intended to run on the BMC and not on the managed node.

---
Changes since v3:
- Revised the comment to clarify the client address in a multi-socket system.
- Enhanced the commit message for better clarity on the changes.
- Reverted to using "depends on" instead of "select" to prevent bypassing dependencies.

Changes since v2:
- Change hwmon config symbol dependency, "depends on" to "select" as "depends on" could silently disable the hwmon driver

Changes since v1:
- Use auxiliary device to probe hwmon sensor instead of moving the hwmon functionality to misc subsystem. This change is as per feedback.

 drivers/hwmon/Kconfig         |   2 +-
 drivers/hwmon/sbtsi_temp.c    |  71 ++++--------------
 drivers/misc/amd-sbi/Kconfig  |  13 ++++
 drivers/misc/amd-sbi/Makefile |   3 +
 drivers/misc/amd-sbi/tsi.c    | 135 ++++++++++++++++++++++++++++++++++
 include/linux/misc/tsi.h      |  34 +++++++++
 6 files changed, 199 insertions(+), 59 deletions(-)
 create mode 100644 drivers/misc/amd-sbi/tsi.c
 create mode 100644 include/linux/misc/tsi.h
drivers/hwmon/Kconfig +1 -1
index 2bfbcc033d59..2b4342abc0a7 100644
@@ -1976,7 +1976,7 @@ config SENSORS_SL28CPLD
 
 config SENSORS_SBTSI
 	tristate "Emulated SB-TSI temperature sensor"
-	depends on I2C
+	depends on AMD_SBTSI
 	help
 	  If you say yes here you get support for emulated temperature
 	  sensors on AMD SoCs with SB-TSI interface connected to a BMC device.
drivers/hwmon/sbtsi_temp.c +13 -58

120 lines not on this page · raw message

drivers/misc/amd-sbi/Kconfig +13 -0
index 30e7fad7356c..512251690e0e 100644
@@ -20,3 +20,16 @@ config AMD_SBRMI_HWMON
 	  This provides support for RMI device hardware monitoring. If enabled,
 	  a hardware monitoring device will be created for each socket in
 	  the system.
+
+config AMD_SBTSI
+	tristate "AMD side band TSI support"
+	depends on I2C
+	depends on ARM || ARM64 || COMPILE_TEST
+	select AUXILIARY_BUS
+	help
+	  Enables support for the AMD SB-TSI (Side Band Temperature Sensor
+	  Interface) driver, which provides access to emulated CPU temperature
+	  sensors on AMD SoCs via an I2C connected BMC device.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called sbtsi.
drivers/misc/amd-sbi/Makefile +3 -0
index 38eaaa651fd9..28f95b9e204f 100644
@@ -2,3 +2,6 @@
 sbrmi-i2c-objs  		+= rmi-i2c.o rmi-core.o
 sbrmi-i2c-$(CONFIG_AMD_SBRMI_HWMON)	+= rmi-hwmon.o
 obj-$(CONFIG_AMD_SBRMI_I2C)	+= sbrmi-i2c.o
+# SBTSI Configuration
+sbtsi-objs	+= tsi.o
+obj-$(CONFIG_AMD_SBTSI)	+= sbtsi.o
drivers/misc/amd-sbi/tsi.c new file +135 -0

135 lines not on this page · raw message

include/linux/misc/tsi.h new file +34 -0
new file mode 100644
index 000000000000..befdc2d14160
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * AMD SBTSI shared data structure and auxiliary bus definitions.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _LINUX_MISC_TSI_H_
+#define _LINUX_MISC_TSI_H_
+
+#include <linux/i2c.h>
+#include <linux/types.h>
+
+/**
+ * struct sbtsi_data - driver private data for an AMD SB-TSI device
+ * @client:	underlying I2C client
+ * @ext_range_mode:	sensor uses extended temperature range
+ * @read_order:	if set, decimal part must be read before integer part
+ */
+struct sbtsi_data {
+	struct i2c_client *client;
+	bool ext_range_mode;
+	bool read_order;
+};
+
+/*
+ * Name of the auxiliary device published on the auxiliary bus by the core
+ * driver.  The full device name is "amd-sbtsi.temp-sensor.<id>". where
+ * <id> is the auxiliary device instance id.
+ */
+#define AMD_SBTSI_ADEV		"amd-sbtsi"
+#define AMD_SBTSI_AUX_HWMON	"temp-sensor"
+
+#endif /* _LINUX_MISC_TSI_H_ */
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v4 1/8] hwmon/misc: amd-sbi: Move core sbtsi support from hwmon to misc

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Using the I2C client address directly as the auxiliary device instance ID causes sysfs name…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Using the I2C client address directly as the auxiliary device instance ID causes sysfs name collisions and probe failures if identical devices reside at the same address on different I2C buses. --

--- Patch [1]: [PATCH v4 1/8] hwmon/misc: amd-sbi: Move core sbtsi support from hwmon to misc --- commit 00ce49f593f00850072fde683389c8cb1954ee34
Author: Prathima <[email protected]>

hwmon/misc: amd-sbi: Move core sbtsi support from hwmon to misc

This commit moves the SBTSI core functionality out of the hwmon-only path and into drivers/misc/amd-sbi so it can be reused. It creates an auxiliary device for the hwmon driver to bind to and adds the appropriate build dependencies.

Quoted 5 lines
drivers/misc/amd-sbi/tsi.c new file +0 -0
new file mode 100644
index 0000000000000..67d08df28429d

[ ... ]

drivers/misc/amd-sbi/tsi.c line 63

Quoted 11 lines
drivers/misc/amd-sbi/tsi.c new file +11 -0
+static int sbtsi_create_hwmon_adev(struct device *dev, u8 dev_addr)
+{
+	struct auxiliary_device *adev;
+	int ret;
+
+	adev = kzalloc_obj(*adev);
+	if (!adev)
+		return -ENOMEM;
+
+	adev->name = AMD_SBTSI_AUX_HWMON;
+	adev->id = dev_addr;

[Severity: Medium]
Could using the I2C client address directly as the auxiliary device instance ID cause sysfs name collisions and probe failures if identical devices reside at the same address on different I2C buses?

If multiple sensors are present on different I2C buses but share the same I2C address, the driver assigns them identical auxiliary device IDs. The auxiliary bus subsystem aggregates all devices into a single virtual namespace (/sys/bus/auxiliary/devices/amd-sbtsi.temp-sensor.<id>), so device_add() could return -EEXIST and cause subsequent probes to fail.

While the comments below mention that multi-socket systems provide unique I2C addresses even across different buses, relying on platform-specific hardware strapping assumptions for globally unique software identifiers can be fragile.

Would it make sense to use the IDA framework here to dynamically allocate globally unique instance IDs?

Quoted 4 lines
drivers/misc/amd-sbi/tsi.c new file +4 -0
+	adev->dev.parent = dev;
+	adev->dev.release = sbtsi_adev_release;
+
+	ret = auxiliary_device_init(adev);

[ ... ]

Quoted 5 lines
drivers/misc/amd-sbi/tsi.c new file +5 -0
+static int sbtsi_i2c_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct sbtsi_data *data;
+	int err;

[ ... ]

Quoted 9 lines
drivers/misc/amd-sbi/tsi.c new file +9 -0
+	dev_set_drvdata(dev, data);
+	/* In a multi-socket system, devices that are otherwise identical do not
+	 * share the same static address; each instance resides at a unique I2C
+	 * client address on the same or different bus. Use the I2C client
+	 * address as the auxiliary device instance ID to ensure each socket
+	 * receives a distinct auxiliary device name.
+	 */
+	return sbtsi_create_hwmon_adev(dev, client->addr);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v4 1/8
#3 lore raw

Re: [PATCH v4 1/8] hwmon/misc: amd-sbi: Move core sbtsi support from hwmon to misc

In reply to Akshay Gupta
Quoted 13 lines · Akshay Gupta

From: Prathima <[email protected]>

Move SBTSI(Side-Band Temperature Sensor Interface) core functionality out of the hwmon-only path and into drivers/misc/amd-sbi so it can be reused by non-hwmon consumers.

I2C probe parsing is moved from drivers/hwmon/sbtsi_temp.c into drivers/misc/amd-sbi/tsi.c under CONFIG_AMD_SBTSI. The core driver stores struct sbtsi_data on the bus device and registers an auxiliary device amd-sbtsi.temp-sensor.<addr> per target.
The current hwmon temp sensor will now require the CONFIG_AMD_SBTSI configuration as a new dependency.

Quoted 8 lines

This split prepares the driver for additional interfaces while keeping hwmon support in hwmon subsystem on top of common SBTSI core logic.

Add platform dependencies to clarify this driver is intended to run on the BMC and not on the managed node.

Regarding Sashiko's comments, I asume there can only be one of these in a given system.

Given that,

[ Assuming that the series will be picked up together through some
other maintainer ]

Thanks,
Guenter

#4 lore raw

PATCH v4 2/8 hwmon: sbtsi_temp: Refactor temperature register access into helpers

In reply to Akshay Gupta

From: Prathima <[email protected]>

Extract the paired integer/decimal register reads and writes from the hwmon read/write callbacks into sbtsi_temp_read() and sbtsi_temp_write() helpers. This consolidates error handling and respects the ReadOrder bit for atomic temperature latching.

This keeps register access independent while preserving existing hwmon functionality.

---
Changes since v1:
- New patch

 drivers/hwmon/sbtsi_temp.c | 84 +++++++++++++++++++++++++++-----------
 1 file changed, 61 insertions(+), 23 deletions(-)
drivers/hwmon/sbtsi_temp.c +61 -23
index 28258bf49922..078f4ab25bde 100644
@@ -61,40 +61,82 @@ static inline void sbtsi_mc_to_reg(s32 temp, u8 *integer, u8 *decimal)
 	*decimal = (temp & 0x7) << 5;
 }
 
+/*
+ * Read integer and decimal parts of an SB-TSI temperature register pair
+ * The read order is determined by the ReadOrder bit to ensure atomic latching.
+ */
+static int sbtsi_temp_read(struct sbtsi_data *data, u8 reg1, u8 reg2,
+			   u8 *val1, u8 *val2)
+{
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, reg1);
+	if (ret < 0)
+		return ret;
+	*val1 = ret;
+	ret = i2c_smbus_read_byte_data(data->client, reg2);
+	if (ret < 0)
+		return ret;
+	*val2 = ret;
+	return 0;
+}
+
+/*
+ * Write integer and decimal parts of an SB-TSI temperature register pair.
+ */
+static int sbtsi_temp_write(struct sbtsi_data *data, u8 reg_int, u8 reg_dec,
+			    u8 val_int, u8 val_dec)
+{
+	int ret;
+
+	ret = i2c_smbus_write_byte_data(data->client, reg_int, val_int);
+	if (!ret)
+		ret = i2c_smbus_write_byte_data(data->client, reg_dec, val_dec);
+	return ret;
+}
+
 static int sbtsi_read(struct device *dev, enum hwmon_sensor_types type,
 		      u32 attr, int channel, long *val)
 {
 	struct sbtsi_data *data = dev_get_drvdata(dev);
 	s32 temp_int, temp_dec;
+	int err;
+	u8 val_int, val_dec;
 
 	switch (attr) {
 	case hwmon_temp_input:
-		if (data->read_order) {
-			temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_DEC);
-			temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_INT);
-		} else {
-			temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_INT);
-			temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_DEC);
-		}
+		if (data->read_order)
+			err = sbtsi_temp_read(data,
+					      SBTSI_REG_TEMP_DEC, SBTSI_REG_TEMP_INT,
+					      &val_dec, &val_int);
+		else
+			err = sbtsi_temp_read(data,
+					      SBTSI_REG_TEMP_INT, SBTSI_REG_TEMP_DEC,
+					      &val_int, &val_dec);
+		if (err < 0)
+			return err;
 		break;
 	case hwmon_temp_max:
-		temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_HIGH_INT);
-		temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_HIGH_DEC);
+		err = sbtsi_temp_read(data,
+				      SBTSI_REG_TEMP_HIGH_INT, SBTSI_REG_TEMP_HIGH_DEC,
+				      &val_int, &val_dec);
+		if (err < 0)
+			return err;
 		break;
 	case hwmon_temp_min:
-		temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_LOW_INT);
-		temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_LOW_DEC);
+		err = sbtsi_temp_read(data,
+				      SBTSI_REG_TEMP_LOW_INT, SBTSI_REG_TEMP_LOW_DEC,
+				      &val_int, &val_dec);
+
+		if (err < 0)
+			return err;
 		break;
 	default:
 		return -EINVAL;
 	}
 
-
-	if (temp_int < 0)
-		return temp_int;
-	if (temp_dec < 0)
-		return temp_dec;
-
+	temp_int = val_int;
+	temp_dec = val_dec;
 	*val = sbtsi_reg_to_mc(temp_int, temp_dec);
 	if (data->ext_range_mode)
 		*val -= SBTSI_TEMP_EXT_RANGE_ADJ;
@@ -106,7 +148,7 @@ static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type,
 		       u32 attr, int channel, long val)
 {
 	struct sbtsi_data *data = dev_get_drvdata(dev);
-	int reg_int, reg_dec, err;
+	int reg_int, reg_dec;
 	u8 temp_int, temp_dec;
 
 	switch (attr) {
@@ -127,11 +169,7 @@ static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type,
 	val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX);
 	sbtsi_mc_to_reg(val, &temp_int, &temp_dec);
 
-	err = i2c_smbus_write_byte_data(data->client, reg_int, temp_int);
-	if (err)
-		return err;
-
-	return i2c_smbus_write_byte_data(data->client, reg_dec, temp_dec);
+	return sbtsi_temp_write(data, reg_int, reg_dec, temp_int, temp_dec);
 }
 
 static umode_t sbtsi_is_visible(const void *data,
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v4 2/8] hwmon: sbtsi_temp: Refactor temperature register access into helpers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Setting an extremely high temperature limit in extended range mode causes a signed integer overflow, resulting in the limit being incorrectly clamped to the minimum temperature. --

--- Patch [2]: [PATCH v4 2/8] hwmon: sbtsi_temp: Refactor temperature register access into helpers --- commit 64811eaa450f0343a3d3098341243ec7d1df489c
Author: Prathima <[email protected]>

hwmon: sbtsi_temp: Refactor temperature register access into helpers

This commit extracts the paired integer and decimal register reads and writes from the hwmon callbacks into helper functions. It consolidates error handling and respects the ReadOrder bit to ensure atomic latching.

Quoted 4 lines
drivers/hwmon/sbtsi_temp.c +0 -0
index 28258bf499223..078f4ab25bde5 100644

[ ... ]

drivers/hwmon/sbtsi_temp.c static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type, · line 173

Quoted 11 lines
drivers/hwmon/sbtsi_temp.c +1 -5
@@ -127,11 +169,7 @@ static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type,
 	val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX);
 	sbtsi_mc_to_reg(val, &temp_int, &temp_dec);
 
-	err = i2c_smbus_write_byte_data(data->client, reg_int, temp_int);
-	if (err)
-		return err;
-
-	return i2c_smbus_write_byte_data(data->client, reg_dec, temp_dec);
+	return sbtsi_temp_write(data, reg_int, reg_dec, temp_int, temp_dec);
 }

[Severity: High]
This isn't a bug introduced by this patch, but can val overflow right before the clamping happens in sbtsi_write()?

Looking at drivers/hwmon/sbtsi_temp.c around line 168, just above this diff context:

	if (data->ext_range_mode)
		val += SBTSI_TEMP_EXT_RANGE_ADJ;
	val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX);

If a privileged user writes an extremely high value to the sysfs temperature limit file (for example, a value near LONG_MAX), adding SBTSI_TEMP_EXT_RANGE_ADJ to val could cause a signed integer overflow.

This would wrap to a negative value, causing clamp_val() to incorrectly evaluate it as being below the lower bound and clamp it to SBTSI_TEMP_MIN instead of SBTSI_TEMP_MAX.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Acked-by on v4 2/8
#6 lore raw

Re: [PATCH v4 2/8] hwmon: sbtsi_temp: Refactor temperature register access into helpers

In reply to Akshay Gupta
Quoted 7 lines · Akshay Gupta

From: Prathima <[email protected]>

Extract the paired integer/decimal register reads and writes from the hwmon read/write callbacks into sbtsi_temp_read() and sbtsi_temp_write() helpers. This consolidates error handling and respects the ReadOrder bit for atomic temperature latching.

Quoted 5 lines

This keeps register access independent while preserving existing hwmon functionality.

The overflow problem raised by Sashiko should probably be addressed, but that does not have to be done with this series.

Thanks,
Guenter

#7 lore raw

PATCH v4 3/8 hwmon/misc: amd-sbi: Move sbtsi register transfer to core abstraction

In reply to Akshay Gupta

From: Prathima <[email protected]>

Move the I2C read/write byte operations from the sbtsi hwmon driver into a common sbtsi_xfer() function in tsi-core.c.
This decouples the hwmon sensor driver from the underlying bus transport, preparing for I3C support in a subsequent patch.
This patch does not introduce any functional changes. The updates are limited to code organization/cleanup and should not affect the runtime behavior of the driver

---
Changes since v2:
- Name change for header inclusion guard to misc specific

Changes since v1:
- New patch

 drivers/hwmon/sbtsi_temp.c      | 17 ++++++-----------
 drivers/misc/amd-sbi/Makefile   |  2 +-
 drivers/misc/amd-sbi/tsi-core.c | 30 ++++++++++++++++++++++++++++++
 include/linux/misc/tsi.h        | 13 +++++++++++++
 4 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 drivers/misc/amd-sbi/tsi-core.c
drivers/hwmon/sbtsi_temp.c +6 -11
index 078f4ab25bde..d7ae986d824c 100644
@@ -70,15 +70,10 @@ static int sbtsi_temp_read(struct sbtsi_data *data, u8 reg1, u8 reg2,
 {
 	int ret;
 
-	ret = i2c_smbus_read_byte_data(data->client, reg1);
-	if (ret < 0)
-		return ret;
-	*val1 = ret;
-	ret = i2c_smbus_read_byte_data(data->client, reg2);
-	if (ret < 0)
-		return ret;
-	*val2 = ret;
-	return 0;
+	ret = sbtsi_xfer(data, reg1, val1, true);
+	if (!ret)
+		ret = sbtsi_xfer(data, reg2, val2, true);
+	return ret;
 }
 
 /*
@@ -89,9 +84,9 @@ static int sbtsi_temp_write(struct sbtsi_data *data, u8 reg_int, u8 reg_dec,
 {
 	int ret;
 
-	ret = i2c_smbus_write_byte_data(data->client, reg_int, val_int);
+	ret = sbtsi_xfer(data, reg_int, &val_int, false);
 	if (!ret)
-		ret = i2c_smbus_write_byte_data(data->client, reg_dec, val_dec);
+		ret = sbtsi_xfer(data, reg_dec, &val_dec, false);
 	return ret;
 }
 
drivers/misc/amd-sbi/Makefile +1 -1
index 28f95b9e204f..ce9321f5c601 100644
@@ -3,5 +3,5 @@ sbrmi-i2c-objs  		+= rmi-i2c.o rmi-core.o
 sbrmi-i2c-$(CONFIG_AMD_SBRMI_HWMON)	+= rmi-hwmon.o
 obj-$(CONFIG_AMD_SBRMI_I2C)	+= sbrmi-i2c.o
 # SBTSI Configuration
-sbtsi-objs	+= tsi.o
+sbtsi-objs	+= tsi.o tsi-core.o
 obj-$(CONFIG_AMD_SBTSI)	+= sbtsi.o
drivers/misc/amd-sbi/tsi-core.c new file +30 -0
new file mode 100644
index 000000000000..6ef1831515bb
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * tsi-core.c - file defining SB-TSI protocols compliant
+ *              AMD SoC device.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/module.h>
+#include <linux/misc/tsi.h>
+
+/* I2C transfer function */
+static int sbtsi_i2c_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
+{
+	if (is_read) {
+		int ret = i2c_smbus_read_byte_data(data->client, reg);
+
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		return 0;
+	}
+	return i2c_smbus_write_byte_data(data->client, reg, *val);
+}
+
+int sbtsi_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
+{
+	return sbtsi_i2c_xfer(data, reg, val, is_read);
+}
+EXPORT_SYMBOL_GPL(sbtsi_xfer);
include/linux/misc/tsi.h +13 -0
index befdc2d14160..2d2709f1ff32 100644
@@ -31,4 +31,17 @@ struct sbtsi_data {
 #define AMD_SBTSI_ADEV		"amd-sbtsi"
 #define AMD_SBTSI_AUX_HWMON	"temp-sensor"
 
+/**
+ * sbtsi_xfer - Perform a register read or write transfer on an AMD SB-TSI device.
+ *
+ * @data:    Pointer to the sbtsi_data structure containing the device context
+ * @reg:     Register address to access.
+ * @val:     Pointer to the value to read into or write from.
+ * @is_read: If true, performs a read transfer and stores the result in @val.
+ *           If false, performs a write transfer using the value in @val.
+ *
+ * Returns 0 on success, or a negative error code on failure.
+ */
+int sbtsi_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read);
+
 #endif /* _LINUX_MISC_TSI_H_ */
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v4 3/8] hwmon/misc: amd-sbi: Move sbtsi register transfer to core abstraction

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

From: Prathima <[email protected]>

Move the I2C read/write byte operations from the sbtsi hwmon driver into a common sbtsi_xfer() function in tsi-core.c.
This decouples the hwmon sensor driver from the underlying bus transport, preparing for I3C support in a subsequent patch.
This patch does not introduce any functional changes. The updates are limited to code organization/cleanup and should not affect the runtime behavior of the driver

Quoted 2 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Acked-by on v4 3/8
#9 lore raw

Re: [PATCH v4 3/8] hwmon/misc: amd-sbi: Move sbtsi register transfer to core abstraction

In reply to Akshay Gupta
Quoted 10 lines · Akshay Gupta

From: Prathima <[email protected]>

Move the I2C read/write byte operations from the sbtsi hwmon driver into a common sbtsi_xfer() function in tsi-core.c.
This decouples the hwmon sensor driver from the underlying bus transport, preparing for I3C support in a subsequent patch.
This patch does not introduce any functional changes. The updates are limited to code organization/cleanup and should not affect the runtime behavior of the driver

Quoted 2 lines
#10 lore raw

PATCH v4 4/8 misc: amd-sbi: Consolidate Common SBTSI Probe Path

In reply to Akshay Gupta

From: Prathima <[email protected]>

Refactor shared probe procedures into sbtsi_probe_common() to ensure that I2C and I3C probes focus solely on bus-specific allocation and device configuration.
The utility function reads the configuration register via sbtsi_xfer(), initializes ext_range_mode and read_order, assigns the driver data, and registers the hwmon auxiliary device.
Routing register access through sbtsi_xfer() keeps the probe path bus-agnostic, so no transfer logic has to be duplicated when SB-TSI over I3C support is added in a later patch.

---
Changes since v3:
- No functional changes.
- Revised the commit message to clearly detail the patch modifications.

Changes since v2:
- New patch to refactor SBTSI common probe

 drivers/misc/amd-sbi/tsi.c | 26 ++++++++++++++++++--------
 include/linux/misc/tsi.h   |  2 ++
 2 files changed, 20 insertions(+), 8 deletions(-)
drivers/misc/amd-sbi/tsi.c +18 -8
index 67d08df28429..35b9f40741e7 100644
@@ -79,31 +79,41 @@ static int sbtsi_create_hwmon_adev(struct device *dev, u8 dev_addr)
 	return devm_add_action_or_reset(dev, sbtsi_unregister_hwmon_adev, adev);
 }
 
+static int sbtsi_probe_common(struct device *dev, struct sbtsi_data *data)
+{
+	u8 val;
+	int err;
+
+	err = sbtsi_xfer(data, SBTSI_REG_CONFIG, &val, true);
+	if (err)
+		return err;
+
+	data->ext_range_mode = FIELD_GET(BIT(SBTSI_CONFIG_EXT_RANGE_SHIFT), val);
+	data->read_order = FIELD_GET(BIT(SBTSI_CONFIG_READ_ORDER_SHIFT), val);
+
+	dev_set_drvdata(dev, data);
+	return sbtsi_create_hwmon_adev(dev, data->dev_addr);
+}
+
 static int sbtsi_i2c_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
 	struct sbtsi_data *data;
-	int err;
 
 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
 	if (!data)
 		return -ENOMEM;
 
 	data->client = client;
-	err = i2c_smbus_read_byte_data(data->client, SBTSI_REG_CONFIG);
-	if (err < 0)
-		return err;
-	data->ext_range_mode = FIELD_GET(BIT(SBTSI_CONFIG_EXT_RANGE_SHIFT), err);
-	data->read_order = FIELD_GET(BIT(SBTSI_CONFIG_READ_ORDER_SHIFT), err);
 
-	dev_set_drvdata(dev, data);
 	/* In a multi-socket system, devices that are otherwise identical do not
 	 * share the same static address; each instance resides at a unique I2C
 	 * client address on the same or different bus. Use the I2C client
 	 * address as the auxiliary device instance ID to ensure each socket
 	 * receives a distinct auxiliary device name.
 	 */
-	return sbtsi_create_hwmon_adev(dev, client->addr);
+	data->dev_addr = client->addr;
+	return sbtsi_probe_common(dev, data);
 }
 
 static const struct i2c_device_id sbtsi_id[] = {
include/linux/misc/tsi.h +2 -0
index 2d2709f1ff32..6533879cc358 100644
@@ -14,11 +14,13 @@
 /**
  * struct sbtsi_data - driver private data for an AMD SB-TSI device
  * @client:	underlying I2C client
+ * @dev_addr:	I2C device address, used as the auxiliary device instance id
  * @ext_range_mode:	sensor uses extended temperature range
  * @read_order:	if set, decimal part must be read before integer part
  */
 struct sbtsi_data {
 	struct i2c_client *client;
+	u8 dev_addr;
 	bool ext_range_mode;
 	bool read_order;
 };
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v4 4/8] misc: amd-sbi: Consolidate Common SBTSI Probe Path

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 12 lines

From: Prathima <[email protected]>

Refactor shared probe procedures into sbtsi_probe_common() to ensure that I2C and I3C probes focus solely on bus-specific allocation and device configuration.
The utility function reads the configuration register via sbtsi_xfer(), initializes ext_range_mode and read_order, assigns the driver data, and registers the hwmon auxiliary device.
Routing register access through sbtsi_xfer() keeps the probe path bus-agnostic, so no transfer logic has to be duplicated when SB-TSI over I3C support is added in a later patch.

Quoted 2 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#12 lore raw

PATCH v4 5/8 misc: amd-sbi: Add support for SB-TSI over I3C

In reply to Akshay Gupta

From: Prathima <[email protected]>

AMD SB-TSI temperature sensors can be accessed over both I2C and I3C buses depending on the platform configuration. Extend the SB-TSI driver to support both I2C and I3C bus interfaces by selecting the appropriate transport based on the probed bus type. The driver maintains backward compatibility with existing I2C deployments while enabling support for systems using the I3C bus. Register both I2C and I3C drivers using module_i3c_i2c_driver() and update the Kconfig dependency from I2C to I3C_OR_I2C.

---
Changes since v3:
- Resolved the issue with DMA cache alignment.
- Added a comment regarding the assigned address in a multi-socket system.
- Update comment for "struct sbtsi_data" element

Changes since v2:
- Fix DMA mapping issue to prevent memory corruption or kernel panics when CONFIG_VMAP_STACK is enabled
- Use i3c_device_get_info() to populate i3c_device_info structure fields
- Remove unused header file inclusion from "include/linux/misc/tsi.h"

Changes since v1:
- Changes in accordance with usage of auxiliary device

 drivers/misc/amd-sbi/Kconfig    |  4 +-
 drivers/misc/amd-sbi/tsi-core.c | 55 +++++++++++++++++++++++++++-
 drivers/misc/amd-sbi/tsi-core.h | 26 +++++++++++++
 drivers/misc/amd-sbi/tsi.c      | 65 +++++++++++++++++++++++++++++++--
 include/linux/misc/tsi.h        | 11 +++++-
 5 files changed, 152 insertions(+), 9 deletions(-)
 create mode 100644 drivers/misc/amd-sbi/tsi-core.h
drivers/misc/amd-sbi/Kconfig +2 -2
index 512251690e0e..1a96b71f8506 100644
@@ -23,13 +23,13 @@ config AMD_SBRMI_HWMON
 
 config AMD_SBTSI
 	tristate "AMD side band TSI support"
-	depends on I2C
+	depends on I3C_OR_I2C
 	depends on ARM || ARM64 || COMPILE_TEST
 	select AUXILIARY_BUS
 	help
 	  Enables support for the AMD SB-TSI (Side Band Temperature Sensor
 	  Interface) driver, which provides access to emulated CPU temperature
-	  sensors on AMD SoCs via an I2C connected BMC device.
+	  sensors on AMD SoCs via an I2C/I3C connected BMC device.
 
 	  This driver can also be built as a module. If so, the module will
 	  be called sbtsi.
drivers/misc/amd-sbi/tsi-core.c +54 -1

69 lines not on this page · raw message

drivers/misc/amd-sbi/tsi-core.h new file +26 -0
new file mode 100644
index 000000000000..7e8c0e7c3bcf
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * AMD SBTSI core driver private definitions.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _LINUX_TSI_CORE_H_
+#define _LINUX_TSI_CORE_H_
+
+#include <linux/cache.h>
+#include <linux/misc/tsi.h>
+
+/**
+ * struct sbtsi_i3c_priv - per-device state for I3C SBTSI (includes DMA-safe buffers)
+ * @data: public device state exposed via dev_set_drvdata()
+ * @tx:   outgoing I3C bytes (DMA_TO_DEVICE); [0] register address, [1] value
+ * @rx:   incoming I3C data byte (DMA_FROM_DEVICE)
+ */
+struct sbtsi_i3c_priv {
+	struct sbtsi_data data;
+	u8 tx[2];
+	u8 rx __aligned(ARCH_DMA_MINALIGN);
+};
+
+#endif /* _LINUX_TSI_CORE_H_ */
drivers/misc/amd-sbi/tsi.c +61 -4

88 lines not on this page · raw message

include/linux/misc/tsi.h +9 -2
index 6533879cc358..0bdd9d923f92 100644
@@ -9,20 +9,27 @@
 #define _LINUX_MISC_TSI_H_
 
 #include <linux/i2c.h>
+#include <linux/i3c/device.h>
 #include <linux/types.h>
 
 /**
  * struct sbtsi_data - driver private data for an AMD SB-TSI device
  * @client:	underlying I2C client
- * @dev_addr:	I2C device address, used as the auxiliary device instance id
+ * @i3cdev:	underlying I3C device (when using I3C bus)
+ * @dev_addr:	I2C/I3C device address, used as the auxiliary device instance id
  * @ext_range_mode:	sensor uses extended temperature range
  * @read_order:	if set, decimal part must be read before integer part
+ * @is_i3c:	true when the device is accessed over I3C
  */
 struct sbtsi_data {
-	struct i2c_client *client;
+	union {
+		struct i2c_client *client;
+		struct i3c_device *i3cdev;
+	};
 	u8 dev_addr;
 	bool ext_range_mode;
 	bool read_order;
+	bool is_i3c;
 };
 
 /*
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH v4 5/8] misc: amd-sbi: Add support for SB-TSI over I3C

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Concurrent hwmon sysfs accesses cause data races on the shared I3C DMA buffers (priv->tx and priv->rx) due to missing locking.
- [Medium] The I3C transmit buffer tx in struct sbtsi_i3c_priv is not cacheline aligned, violating DMA mapping requirements.

Pre-existing issues:
- [High] Temperature register reads are performed non-atomically without a device lock, breaking the SB-TSI hardware latching mechanism on concurrent accesses. --

--- Patch [5]: [PATCH v4 5/8] misc: amd-sbi: Add support for SB-TSI over I3C --- commit f466305c592ff454f903d27fc9a9ef283f005d66
Author: Prathima <[email protected]>

misc: amd-sbi: Add support for SB-TSI over I3C

This commit extends the AMD SB-TSI driver to support both I2C and I3C bus interfaces. It selects the appropriate transport based on the probed bus type and registers both drivers using module_i3c_i2c_driver() while maintaining backward compatibility with existing I2C deployments.

Quoted 4 lines
drivers/misc/amd-sbi/tsi-core.c +0 -0
index 6ef1831515bbb..1c6f37f26d945 100644

[ ... ]

drivers/misc/amd-sbi/tsi-core.c static int sbtsi_i2c_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read · line 38

Quoted 12 lines
drivers/misc/amd-sbi/tsi-core.c +8 -0
@@ -23,8 +28,56 @@ static int sbtsi_i2c_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read
 	return i2c_smbus_write_byte_data(data->client, reg, *val);
 }
 
+/* I3C read transfer function */
+static int sbtsi_i3c_read(struct sbtsi_data *data, u8 reg, u8 *val)
+{
+	struct sbtsi_i3c_priv *priv = to_sbtsi_i3c_priv(data);
+	struct i3c_xfer xfers[2] = { };
+	int ret;
+
+	priv->tx[0] = reg;

[Severity: High]
Does this sequence need synchronization to protect the shared priv->tx and priv->rx buffers?

The hwmon subsystem does not serialize reads and writes to sysfs attributes. Without a lock protecting these buffers during the bus transfer, concurrent sysfs reads from multiple threads could race, causing threads to overwrite the transmit payload or read incorrect response values before or after i3c_device_do_xfers() completes.

Quoted 5 lines
drivers/misc/amd-sbi/tsi-core.c +5 -0
+
+	/* Write the register address (DMA_TO_DEVICE). */
+	xfers[0].rnw = false;
+	xfers[0].len = 1;
+	xfers[0].data.out = priv->tx;

[ ... ]

drivers/misc/amd-sbi/tsi-core.c static int sbtsi_i2c_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read · line 69

Quoted 12 lines
drivers/misc/amd-sbi/tsi-core.c +12 -0
+/* I3C write transfer function */
+static int sbtsi_i3c_write(struct sbtsi_data *data, u8 reg, u8 val)
+{
+	struct sbtsi_i3c_priv *priv = to_sbtsi_i3c_priv(data);
+	struct i3c_xfer xfers = {
+		.rnw = false,
+		.len = 2,
+		.data.out = priv->tx,
+	};
+
+	priv->tx[0] = reg;
+	priv->tx[1] = val;

[Severity: High]
Similarly, could concurrent sysfs writes overwrite priv->tx here before the i3c_device_do_xfers() call finishes?

Quoted 9 lines
drivers/misc/amd-sbi/tsi-core.c +4 -0
+
+	return i3c_device_do_xfers(data->i3cdev, &xfers, 1, I3C_SDR);
+}
+
drivers/misc/amd-sbi/tsi-core.h new file +0 -0
new file mode 100644
index 0000000000000..7e8c0e7c3bcfa

[ ... ]

drivers/misc/amd-sbi/tsi-core.h line 23

Quoted 4 lines
drivers/misc/amd-sbi/tsi-core.h new file +4 -0
+struct sbtsi_i3c_priv {
+	struct sbtsi_data data;
+	u8 tx[2];
+	u8 rx __aligned(ARCH_DMA_MINALIGN);

[Severity: Medium]
Should the tx array also have the __aligned(ARCH_DMA_MINALIGN) attribute?

Without it, tx might share a cacheline with the preceding struct sbtsi_data fields. When tx is mapped for DMA to device transfers during I3C operations, this violates the DMA mapping rules preventing cacheline sharing and can trigger kernel warnings.

Quoted 1 line

+};

[Severity: High]
This isn't a bug introduced by this patch, but does the lack of a device-level lock around temperature register reads break the hardware latching mechanism?

Looking at drivers/hwmon/sbtsi_temp.c in sbtsi_temp_read(), the integer and decimal registers are read sequentially:

    ret = sbtsi_xfer(data, reg1, val1, true);
    if (!ret)
        ret = sbtsi_xfer(data, reg2, val2, true);

If multiple threads concurrently access sysfs, these non-atomic transfers could interleave on the bus. This might cause one thread to receive a mismatched latched decimal value triggered by another thread's integer read, resulting in corrupted temperature reports.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#14 lore raw

PATCH v4 6/8 misc: amd-sbi: Add SBTSI ioctl register transfer interface

In reply to Akshay Gupta

From: Prathima <[email protected]>

Implement IOCTL interface for SB-TSI driver to enable userspace access to TSI register read/write operations through the AMD Advanced Platform Management Link (APML) protocol.
Add an ioctl command (SBTSI_IOCTL_REG_XFER_CMD) that accepts a register address, data byte, and direction flag.
The mutex is taken on the ioctl path here; the hwmon path is placed under the same lock in the next patch, which completes serialization between the hwmon and ioctl paths.

---
Changes since v3:
- Implement a check on the read flag.
- Introduce a mutex lock on "detached" in the sbtsi_misc_unregister() function.
- Relocate the "detached" check to sbtsi_xfer_ioctl to prevent a time-of-check to time-of-use race condition if the device is unbound during this ioctl operation.
- Use devm_add_action_or_reset() in place of devm_add_action(), as in event of probe abort, it can leave the misc device registered

Changes since v2:
- Address feedback to hide mutex with common guard function
- Separate out mutex patch in hwmon file to next patch, 7/8
- Use file operation, open/release and kref to prevent use-after-free if the device is unbound in IOCTL
- Add check for msg.pad

Changes since v1:
- Use of devm_mutex_init in place of mutex_init
- Use of guard_mutex in place of mutex_lock()/mutex_unlock()
- Use of devm_add_action_or_reset() for clean removal

 drivers/misc/amd-sbi/tsi-core.c | 126 +++++++++++++++++++++++++++++++-
 drivers/misc/amd-sbi/tsi-core.h |   3 +
 drivers/misc/amd-sbi/tsi.c      |  38 +++++++++-
 include/linux/misc/tsi.h        |  16 ++++
 include/uapi/misc/amd-apml.h    |  23 ++++++
 5 files changed, 202 insertions(+), 4 deletions(-)
drivers/misc/amd-sbi/tsi-core.c +125 -1
index 1c6f37f26d94..5c178702c67a 100644
@@ -6,7 +6,11 @@
  * Copyright (C) 2026 Advanced Micro Devices, Inc.
  */
 
+#include <linux/fs.h>
+#include <linux/ioctl.h>
 #include <linux/module.h>
+#include <linux/uaccess.h>
+#include <uapi/misc/amd-apml.h>
 #include "tsi-core.h"
 
 static inline struct sbtsi_i3c_priv *to_sbtsi_i3c_priv(struct sbtsi_data *data)
@@ -14,6 +18,17 @@ static inline struct sbtsi_i3c_priv *to_sbtsi_i3c_priv(struct sbtsi_data *data)
 	return container_of(data, struct sbtsi_i3c_priv, data);
 }
 
+void sbtsi_data_release(struct kref *kref)
+{
+	struct sbtsi_data *data = container_of(kref, struct sbtsi_data, kref);
+
+	mutex_destroy(&data->lock);
+	if (data->is_i3c)
+		kfree(to_sbtsi_i3c_priv(data));
+	else
+		kfree(data);
+}
+
 /* I2C transfer function */
 static int sbtsi_i2c_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
 {
@@ -77,7 +92,116 @@ int sbtsi_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
 	if (data->is_i3c)
 		return is_read ? sbtsi_i3c_read(data, reg, val)
 			       : sbtsi_i3c_write(data, reg, *val);
-
 	return sbtsi_i2c_xfer(data, reg, val, is_read);
 }
 EXPORT_SYMBOL_GPL(sbtsi_xfer);
+
+/*
+ * The mutex protects against concurrent register transfers to the device
+ * over the shared bus.
+ */
+static int sbtsi_xfer_ioctl(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
+{
+	guard(sbtsi)(data);
+
+	if (data->detached)
+		return -ENODEV;
+
+	return sbtsi_xfer(data, reg, val, is_read);
+}
+
+static int apml_tsi_reg_xfer(struct sbtsi_data *data,
+			     struct apml_tsi_xfer_msg __user *arg)
+{
+	struct apml_tsi_xfer_msg msg = { 0 };
+	int ret;
+
+	if (copy_from_user(&msg, arg, sizeof(struct apml_tsi_xfer_msg)))
+		return -EFAULT;
+
+	/*
+	 * rflag is a boolean direction flag (0 = write, 1 = read). Reject
+	 * any other value so the upper values stay reserved for future
+	 * extensions instead of being silently treated as a read.
+	 */
+	if (msg.pad || msg.rflag > 1)
+		return -EINVAL;
+
+	ret = sbtsi_xfer_ioctl(data, msg.reg_addr, &msg.data_in_out, msg.rflag);
+
+	if (msg.rflag && !ret) {
+		if (copy_to_user(arg, &msg, sizeof(struct apml_tsi_xfer_msg)))
+			return -EFAULT;
+	}
+	return ret;
+}
+
+static int sbtsi_open(struct inode *inode, struct file *fp)
+{
+	struct sbtsi_data *data;
+
+	data = container_of(fp->private_data, struct sbtsi_data, sbtsi_misc_dev);
+	scoped_guard(sbtsi, data) {
+		if (data->detached)
+			return -ENODEV;
+	}
+
+	kref_get(&data->kref);
+
+	return 0;
+}
+
+static int sbtsi_release(struct inode *inode, struct file *fp)
+{
+	struct sbtsi_data *data;
+
+	data = container_of(fp->private_data, struct sbtsi_data, sbtsi_misc_dev);
+	kref_put(&data->kref, sbtsi_data_release);
+	return 0;
+}
+
+static long sbtsi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+	struct sbtsi_data *data;
+
+	data = container_of(fp->private_data, struct sbtsi_data, sbtsi_misc_dev);
+	switch (cmd) {
+	case SBTSI_IOCTL_REG_XFER_CMD:
+		return apml_tsi_reg_xfer(data, argp);
+	default:
+		return -ENOTTY;
+	}
+}
+
+static const struct file_operations sbtsi_fops = {
+	.owner		= THIS_MODULE,
+	.open		= sbtsi_open,
+	.release	= sbtsi_release,
+	.unlocked_ioctl	= sbtsi_ioctl,
+	.compat_ioctl	= compat_ptr_ioctl,
+};
+
+int create_misc_tsi_device(struct sbtsi_data *data, struct device *dev)
+{
+	int ret;
+
+	data->sbtsi_misc_dev.name = devm_kasprintf(dev, GFP_KERNEL,
+						   "sbtsi-%x", data->dev_addr);
+	if (!data->sbtsi_misc_dev.name)
+		return -ENOMEM;
+	data->sbtsi_misc_dev.minor    = MISC_DYNAMIC_MINOR;
+	data->sbtsi_misc_dev.fops     = &sbtsi_fops;
+	data->sbtsi_misc_dev.parent   = dev;
+	data->sbtsi_misc_dev.nodename = devm_kasprintf(dev, GFP_KERNEL,
+						       "sbtsi-%x", data->dev_addr);
+	if (!data->sbtsi_misc_dev.nodename)
+		return -ENOMEM;
+	data->sbtsi_misc_dev.mode = 0600;
+
+	ret = misc_register(&data->sbtsi_misc_dev);
+	if (ret)
+		return ret;
+
+	return 0;
+}
drivers/misc/amd-sbi/tsi-core.h +3 -0
index 7e8c0e7c3bcf..4cf55c46230e 100644
@@ -23,4 +23,7 @@ struct sbtsi_i3c_priv {
 	u8 rx __aligned(ARCH_DMA_MINALIGN);
 };
 
+int create_misc_tsi_device(struct sbtsi_data *data, struct device *dev);
+
+void sbtsi_data_release(struct kref *kref);
 #endif /* _LINUX_TSI_CORE_H_ */
drivers/misc/amd-sbi/tsi.c +35 -3
index 1530f440a020..f06f417f451c 100644
@@ -42,6 +42,23 @@ static void sbtsi_unregister_hwmon_adev(void *_adev)
 	auxiliary_device_uninit(adev);
 }
 
+static void sbtsi_misc_unregister(void *arg)
+{
+	struct sbtsi_data *data = arg;
+
+	misc_deregister(&data->sbtsi_misc_dev);
+
+	guard(sbtsi)(data);
+	data->detached = true;
+}
+
+static void sbtsi_driver_unref(void *arg)
+{
+	struct sbtsi_data *data = arg;
+
+	kref_put(&data->kref, sbtsi_data_release);
+}
+
 /*
  * Create and publish an auxiliary device. The hwmon driver in
  * drivers/hwmon/sbtsi_temp.c binds to this device.
@@ -84,6 +101,13 @@ static int sbtsi_probe_common(struct device *dev, struct sbtsi_data *data)
 	u8 val;
 	int err;
 
+	mutex_init(&data->lock);
+	kref_init(&data->kref);
+
+	err = devm_add_action_or_reset(dev, sbtsi_driver_unref, data);
+	if (err)
+		return err;
+
 	err = sbtsi_xfer(data, SBTSI_REG_CONFIG, &val, true);
 	if (err)
 		return err;
@@ -92,7 +116,15 @@ static int sbtsi_probe_common(struct device *dev, struct sbtsi_data *data)
 	data->read_order = FIELD_GET(BIT(SBTSI_CONFIG_READ_ORDER_SHIFT), val);
 
 	dev_set_drvdata(dev, data);
-	return sbtsi_create_hwmon_adev(dev, data->dev_addr);
+	err = sbtsi_create_hwmon_adev(dev, data->dev_addr);
+	if (err < 0)
+		return err;
+
+	err = create_misc_tsi_device(data, dev);
+	if (err)
+		return err;
+
+	return devm_add_action_or_reset(dev, sbtsi_misc_unregister, data);
 }
 
 static int sbtsi_i2c_probe(struct i2c_client *client)
@@ -100,7 +132,7 @@ static int sbtsi_i2c_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct sbtsi_data *data;
 
-	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
+	data = kzalloc_obj(*data);
 	if (!data)
 		return -ENOMEM;
 
@@ -158,7 +190,7 @@ static int sbtsi_i3c_probe(struct i3c_device *i3cdev)
 	if (I3C_PID_INSTANCE_ID(devinfo.pid) != 0)
 		return -ENXIO;
 
-	i3c_priv = devm_kzalloc(dev, sizeof(*i3c_priv), GFP_KERNEL);
+	i3c_priv = kzalloc_obj(*i3c_priv);
 	if (!i3c_priv)
 		return -ENOMEM;
 
include/linux/misc/tsi.h +16 -0
index 0bdd9d923f92..5273c44688f0 100644
@@ -8,30 +8,46 @@
 #ifndef _LINUX_MISC_TSI_H_
 #define _LINUX_MISC_TSI_H_
 
+#include <linux/cleanup.h>
 #include <linux/i2c.h>
 #include <linux/i3c/device.h>
+#include <linux/kref.h>
+#include <linux/miscdevice.h>
+#include <linux/mutex.h>
 #include <linux/types.h>
 
 /**
  * struct sbtsi_data - driver private data for an AMD SB-TSI device
  * @client:	underlying I2C client
  * @i3cdev:	underlying I3C device (when using I3C bus)
+ * @sbtsi_misc_dev: miscdevice exposing ioctl interface at /dev/sbtsi-<addr>
+ * @lock:           mutex protecting concurrent access to the device
+ * @kref:      reference count; keeps @sbtsi_data alive while misc fds are open
  * @dev_addr:	I2C/I3C device address, used as the auxiliary device instance id
+ *		and name the misc device node
  * @ext_range_mode:	sensor uses extended temperature range
  * @read_order:	if set, decimal part must be read before integer part
  * @is_i3c:	true when the device is accessed over I3C
+ * @detached:  set on driver unbind; open/ioctl return -ENODEV afterward
  */
 struct sbtsi_data {
 	union {
 		struct i2c_client *client;
 		struct i3c_device *i3cdev;
 	};
+	struct miscdevice sbtsi_misc_dev;
+	struct mutex lock;	/* protects concurrent access to the device */
+	struct kref kref;
 	u8 dev_addr;
 	bool ext_range_mode;
 	bool read_order;
 	bool is_i3c;
+	bool detached;
 };
 
+DEFINE_GUARD(sbtsi, struct sbtsi_data *, mutex_lock(&_T->lock),
+	     mutex_unlock(&_T->lock))
+
 /*
  * Name of the auxiliary device published on the auxiliary bus by the core
  * driver.  The full device name is "amd-sbtsi.temp-sensor.<id>". where
include/uapi/misc/amd-apml.h +23 -0
index 745b3338fc06..8a85f79b0938 100644
@@ -73,6 +73,13 @@ struct apml_reg_xfer_msg {
 	__u8 rflag;
 };
 
+struct apml_tsi_xfer_msg {
+	__u8 reg_addr;		/* TSI register address offset */
+	__u8 data_in_out;	/* Register data for read/write */
+	__u8 rflag;		/* Register read or write */
+	__u8 pad;		/* Explicit padding */
+};
+
 /*
  * AMD sideband interface base IOCTL
  */
@@ -149,4 +156,20 @@ struct apml_reg_xfer_msg {
  */
 #define SBRMI_IOCTL_REG_XFER_CMD	_IOWR(SB_BASE_IOCTL_NR, 3, struct apml_reg_xfer_msg)
 
+/**
+ * DOC: SBTSI_IOCTL_REG_XFER_CMD
+ *
+ * @Parameters
+ *
+ * @struct apml_tsi_xfer_msg
+ *	Pointer to the &struct apml_tsi_xfer_msg that will contain the protocol
+ *	information
+ *
+ * @Description
+ * IOCTL command for APML TSI messages using generic _IOWR
+ * The IOCTL provides userspace access to AMD sideband TSI register xfer protocol
+ * - TSI protocol to read/write temperature sensor registers
+ */
+#define SBTSI_IOCTL_REG_XFER_CMD	_IOWR(SB_BASE_IOCTL_NR, 4, struct apml_tsi_xfer_msg)
+
 #endif /*_AMD_APML_H_*/
Signature · 1 line
2.34.1
#15 lore raw

Re: [PATCH v4 6/8] misc: amd-sbi: Add SBTSI ioctl register transfer interface

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Data race, DMA buffer corruption, and TOCTOU vulnerability due to concurrent hwmon and ioctl…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Data race, DMA buffer corruption, and TOCTOU vulnerability due to concurrent hwmon and ioctl access to shared I3C buffers. --

--- Patch [6]: [PATCH v4 6/8] misc: amd-sbi: Add SBTSI ioctl register transfer interface --- commit 01a49ea8ef63627c7743953c52073396818f7f3a
Author: Prathima <[email protected]>

misc: amd-sbi: Add SBTSI ioctl register transfer interface

Implement IOCTL interface for SB-TSI driver to enable userspace access to TSI register read/write operations through the AMD Advanced Platform Management Link (APML) protocol.

Quoted 4 lines
drivers/misc/amd-sbi/tsi-core.c +0 -0
index 1c6f37f26d945..5c178702c67ab 100644

[ ... ]

Quoted 12 lines
drivers/misc/amd-sbi/tsi-core.c +4 -1
@@ -77,7 +92,116 @@ int sbtsi_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
 	if (data->is_i3c)
 		return is_read ? sbtsi_i3c_read(data, reg, val)
 			       : sbtsi_i3c_write(data, reg, *val);
-
 	return sbtsi_i2c_xfer(data, reg, val, is_read);
 }
 EXPORT_SYMBOL_GPL(sbtsi_xfer);
+
+/*
+ * The mutex protects against concurrent register transfers to the device
+ * over the shared bus.

drivers/misc/amd-sbi/tsi-core.c int sbtsi_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read) · line 111

Quoted 10 lines
drivers/misc/amd-sbi/tsi-core.c +10 -0
+ */
+static int sbtsi_xfer_ioctl(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
+{
+	guard(sbtsi)(data);
+
+	if (data->detached)
+		return -ENODEV;
+
+	return sbtsi_xfer(data, reg, val, is_read);
+}

[Severity: High]
The commit message mentions that the hwmon path is placed under the same lock in the next patch, completing the serialization.

Since this patch introduces the ioctl path before the hwmon path is locked, can an unprivileged user reading the hwmon sysfs attribute (temp1_input) concurrently with the new SBTSI_IOCTL_REG_XFER_CMD ioctl cause data corruption?

For I3C devices, sbtsi_xfer() calls sbtsi_i3c_read() and sbtsi_i3c_write(), which use the shared priv->tx and priv->rx arrays for DMA transfers:

drivers/misc/amd-sbi/tsi-core.c:sbtsi_i3c_read() {
	...
	priv->tx[0] = reg;

	/* Write the register address (DMA_TO_DEVICE). */
	xfers[0].rnw = false;
	xfers[0].len = 1;
	xfers[0].data.out = priv->tx;
	...
}

Will this allow concurrent access to overwrite priv->tx while a privileged ioctl is preparing or executing a DMA transfer, leading to buffer corruption during git bisect?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#16 lore raw

PATCH v4 7/8 hwmon: Add mutex protecting for sbtsi read/write through hwmon

In reply to Akshay Gupta

From: Prathima <[email protected]>

Add a mutex and take it around SBTSI read/write paths so that only one transaction runs at a time. The lock is held only for the duration of the bus transfer and associated driver bookkeeping, not across blocking work unrelated to SBTSI.

This is a concurrency hardening fix.

---
Changes since v2:
- New patch

 drivers/hwmon/sbtsi_temp.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/hwmon/sbtsi_temp.c +2 -0
index d7ae986d824c..11c8108d69b2 100644
@@ -70,6 +70,7 @@ static int sbtsi_temp_read(struct sbtsi_data *data, u8 reg1, u8 reg2,
 {
 	int ret;
 
+	guard(sbtsi)(data);
 	ret = sbtsi_xfer(data, reg1, val1, true);
 	if (!ret)
 		ret = sbtsi_xfer(data, reg2, val2, true);
@@ -84,6 +85,7 @@ static int sbtsi_temp_write(struct sbtsi_data *data, u8 reg_int, u8 reg_dec,
 {
 	int ret;
 
+	guard(sbtsi)(data);
 	ret = sbtsi_xfer(data, reg_int, &val_int, false);
 	if (!ret)
 		ret = sbtsi_xfer(data, reg_dec, &val_dec, false);
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH v4 7/8] hwmon: Add mutex protecting for sbtsi read/write through hwmon

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Integer overflow in sbtsi_write when ext_range_mode is enabled, causing large inputs to be incorrectly clamped to minimum. --

--- Patch [7]: [PATCH v4 7/8] hwmon: Add mutex protecting for sbtsi read/write through hwmon --- commit 8772051459eabab96983f02f6316165d79c69002
Author: Prathima <[email protected]>

hwmon: Add mutex protecting for sbtsi read/write through hwmon

This commit adds a mutex and takes it around SBTSI read/write paths so that only one transaction runs at a time. The lock is held only for the duration of the bus transfer and associated driver bookkeeping.

Quoted 4 lines
drivers/hwmon/sbtsi_temp.c +0 -0
index d7ae986d824c4..11c8108d69b2a 100644

[ ... ]

drivers/hwmon/sbtsi_temp.c static int sbtsi_temp_write(struct sbtsi_data *data, u8 reg_int, u8 reg_dec, · line 91

Quoted 8 lines
drivers/hwmon/sbtsi_temp.c +1 -0
@@ -84,6 +85,7 @@ static int sbtsi_temp_write(struct sbtsi_data *data, u8 reg_int, u8 reg_dec,
 {
 	int ret;
 
+	guard(sbtsi)(data);
 	ret = sbtsi_xfer(data, reg_int, &val_int, false);
 	if (!ret)
 		ret = sbtsi_xfer(data, reg_dec, &val_dec, false);

[Severity: High]
This isn't a bug introduced by this patch, but does this code overflow val in the caller sbtsi_write()?

If data->ext_range_mode is enabled, a large user input val is increased without a prior upper-bound check:

drivers/hwmon/sbtsi_temp.c:sbtsi_write() {
    ...
	if (data->ext_range_mode)
		val += SBTSI_TEMP_EXT_RANGE_ADJ;
	val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX);
    ...
}

Writing a large maximum value to the hwmon sysfs limit causes a signed integer overflow when adding the adjustment value.

The resulting negative number is passed to clamp_val, which incorrectly clamps it to zero.

This sets the hardware temperature limit to zero degrees Celsius, which can instantly trigger thermal throttling or a hardware thermal shutdown.

A user or management script legitimately trying to disable or maximize the limit by writing the maximum possible integer will unintentionally trigger a thermal alert instead.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer Acked-by on v4 7/8
#18 lore raw

Re: [PATCH v4 7/8] hwmon: Add mutex protecting for sbtsi read/write through hwmon

In reply to Akshay Gupta
Quoted 11 lines · Akshay Gupta

From: Prathima <[email protected]>

Add a mutex and take it around SBTSI read/write paths so that only one transaction runs at a time. The lock is held only for the duration of the bus transfer and associated driver bookkeeping, not across blocking work unrelated to SBTSI.

This is a concurrency hardening fix.

#19 lore raw

PATCH v4 8/8 docs: misc: amd-sbi: Document SBTSI userspace interface

In reply to Akshay Gupta

From: Prathima <[email protected]>

- Document AMD sideband IOCTL description defined
for SBTSI and its usage.
User space C-APIs are made available by esmi_oob_library [1], which is provided by the E-SMS project [2].

Include a user-space open example for /dev/sbtsi-* and list auxiliary bus sysfs paths.

---
Changes since v3:
- Address maintainer's comment by changing "xfer" to "transfer"
- Revise the document to clarify the client address for I2C and the assigned address for I3C

Changes since v2:
- Update misc node names info as per socket

Changes since v1:
- Elaborate the document

 Documentation/misc-devices/amd-sbi.rst | 74 ++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
Documentation/misc-devices/amd-sbi.rst +74 -0
index f91ddadefe48..648d743903b7 100644
@@ -48,6 +48,66 @@ Access restrictions:
  * APML Mailbox messages and Register xfer access are read-write,
  * CPUID and MCA_MSR access is read-only.
 
+SBTSI device
+============
+
+sbtsi driver under the drivers/misc/amd-sbi creates miscdevice
+/dev/sbtsi-* to let user space programs run APML TSI register transfer
+commands.
+
+The driver supports both I2C and I3C transports for SB-TSI targets.
+The transport is selected by the bus where the device is enumerated.
+
+Misc device:
+ * In 1P socket 0: /dev/sbtsi-4c
+ * In 2P socket 0: /dev/sbtsi-4c, socket 1: /dev/sbtsi-48
+
+.. code-block:: bash
+
+   $ ls -al /dev/sbtsi-4c
+   crw-------    1 root     root       10, 116 Apr  2 05:22 /dev/sbtsi-4c
+
+
+Access restrictions:
+ * Only root user is allowed to open the file.
+ * APML TSI Register transfer access is read-write.
+
+SBTSI hwmon interface
+=====================
+
+The sbtsi_temp auxiliary driver binds to the auxiliary device published
+by the core sbtsi driver on the auxiliary bus. The auxiliary device is
+named amd-sbtsi.temp-sensor.<id>, where <id> is the device's transfer
+address: the client address for I2C, or the assigned-address for I3C.
+
+Note that the auxiliary bus formats <id> in decimal, whereas the
+/dev/sbtsi-* misc node formats its address in hex. The two therefore
+differ for the same device: an I2C/I3C sensor at address 0x4c appears as the
+misc node /dev/sbtsi-4c and the auxiliary device
+amd-sbtsi.temp-sensor.76.
+
+It registers a hwmon device, providing a standard Linux hwmon interface
+for reading CPU temperature and managing temperature limits.
+
+The hwmon device appears under ``/sys/class/hwmon/`` when both ``sbtsi.ko``
+and ``sbtsi_temp.ko`` are loaded.
+
+Verify auxiliary bus device::
+
+  ls /sys/bus/auxiliary/devices/
+  # e.g. amd-sbtsi.temp-sensor.76 for an I2C/I3C sensor at address 0x4c
+
+Example usage::
+
+  # Read current temperature
+  cat /sys/class/hwmon/hwmon<N>/temp1_input
+
+  # Set high temperature limit to 70 °C
+  echo 70000 > /sys/class/hwmon/hwmon<N>/temp1_max
+
+  # Verify
+  cat /sys/class/hwmon/hwmon<N>/temp1_max
+
 Driver IOCTLs
 =============
 
@@ -63,6 +123,9 @@ Driver IOCTLs
 .. c:macro:: SBRMI_IOCTL_REG_XFER_CMD
 .. kernel-doc:: include/uapi/misc/amd-apml.h
    :doc: SBRMI_IOCTL_REG_XFER_CMD
+.. c:macro:: SBTSI_IOCTL_REG_XFER_CMD
+.. kernel-doc:: include/uapi/misc/amd-apml.h
+   :doc: SBTSI_IOCTL_REG_XFER_CMD
 
 User-space usage
 ================
@@ -85,6 +148,16 @@ Next thing, open the device file, as follows::
     exit(1);
   }
 
+To open SB-TSI device::
+
+  int file;
+
+  file = open("/dev/sbtsi-4c", O_RDWR);
+  if (file < 0) {
+    /* ERROR HANDLING */
+    exit(1);
+  }
+
 The following IOCTLs are defined:
 
 ``#define SB_BASE_IOCTL_NR      	0xF9``
@@ -92,6 +165,7 @@ The following IOCTLs are defined:
 ``#define SBRMI_IOCTL_CPUID_CMD		_IOWR(SB_BASE_IOCTL_NR, 1, struct apml_cpuid_msg)``
 ``#define SBRMI_IOCTL_MCAMSR_CMD	_IOWR(SB_BASE_IOCTL_NR, 2, struct apml_mcamsr_msg)``
 ``#define SBRMI_IOCTL_REG_XFER_CMD	_IOWR(SB_BASE_IOCTL_NR, 3, struct apml_reg_xfer_msg)``
+``#define SBTSI_IOCTL_REG_XFER_CMD      _IOWR(SB_BASE_IOCTL_NR, 4, struct apml_tsi_xfer_msg)``
 
 
 User space C-APIs are made available by esmi_oob_library, hosted at
Signature · 1 line
2.34.1
#20 lore raw

Re: [PATCH v4 8/8] docs: misc: amd-sbi: Document SBTSI userspace interface

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The I3C transmit buffer (`tx`) is used for DMA but is not cacheline aligned, leading to DMA false…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The I3C transmit buffer (tx) is used for DMA but is not cacheline aligned, leading to DMA false sharing with adjacent fields in struct sbtsi_data.
- [High] A race condition between misc_open() and devres driver unbind causes a use-after-free of struct sbtsi_data.
--

--- Patch [8]: [PATCH v4 8/8] docs: misc: amd-sbi: Document SBTSI userspace interface --- commit 0d1e3dd1fee7d42c8bd4c97352ebe1b9dc763f55
Author: Prathima <[email protected]>
Subject: docs: misc: amd-sbi: Document SBTSI userspace interface

This commit documents the SBTSI userspace interface and implements the SB-TSI driver for AMD SoCs. It adds an ioctl interface to perform I2C/I3C register transfers and creates a miscdevice node for each sensor. It also registers an auxiliary device to bind with the hwmon driver.

Quoted 6 lines
drivers/misc/amd-sbi/tsi-core.c new file +0 -0
new file mode 100644
index 0000000000000..5c178702c67ab
@@ -0,0 +1,207 @@

[ ... ]

Quoted 10 lines
drivers/misc/amd-sbi/tsi-core.c new file +10 -0
+/* I3C read transfer function */
+static int sbtsi_i3c_read(struct sbtsi_data *data, u8 reg, u8 *val)
+{
+	struct sbtsi_i3c_priv *priv = to_sbtsi_i3c_priv(data);
+	struct i3c_xfer xfers[2] = { };
+	int ret;
+
+	priv->tx[0] = reg;
+
+	/* Write the register address (DMA_TO_DEVICE). */

drivers/misc/amd-sbi/tsi-core.c

Quoted 10 lines
drivers/misc/amd-sbi/tsi-core.c new file +10 -0
+	xfers[0].rnw = false;
+	xfers[0].len = 1;
+	xfers[0].data.out = priv->tx;
+
+	/* Read the data byte into a separate buffer (DMA_FROM_DEVICE). */
+	xfers[1].rnw = true;
+	xfers[1].len = 1;
+	xfers[1].data.in = &priv->rx;
+
+	ret = i3c_device_do_xfers(data->i3cdev, xfers, 2, I3C_SDR);

[Severity: High]
Does this code lack cacheline alignment for the tx buffer?

Looking at the structure definition in drivers/misc/amd-sbi/tsi-core.h:

struct sbtsi_i3c_priv {
	struct sbtsi_data data;
	u8 tx[2];
	u8 rx __aligned(ARCH_DMA_MINALIGN);
};

Since tx lacks __aligned(ARCH_DMA_MINALIGN), it shares a cacheline with the preceding fields in struct sbtsi_data. Because tx is used here as a DMA_TO_DEVICE buffer, couldn't DMA cache maintenance operations on tx corrupt adjacent fields like kref or lock on non-cache-coherent architectures?

Quoted 6 lines
drivers/misc/amd-sbi/tsi-core.c new file +6 -0
+	if (ret)
+		return ret;
+
+	*val = priv->rx;
+	return ret;
+}

[ ... ]

drivers/misc/amd-sbi/tsi-core.c

Quoted 6 lines
drivers/misc/amd-sbi/tsi-core.c new file +6 -0
+static int sbtsi_open(struct inode *inode, struct file *fp)
+{
+	struct sbtsi_data *data;
+
+	data = container_of(fp->private_data, struct sbtsi_data, sbtsi_misc_dev);
+	scoped_guard(sbtsi, data) {

[Severity: High]
Is it possible for a concurrent device unbind to free struct sbtsi_data before sbtsi_open() acquires the mutex?

If misc_open() sets fp->private_data and drops the core misc_mtx, and then devres executes sbtsi_driver_unref() (which drops the final reference and frees data), would sbtsi_open() accessing data->lock result in a use-after-free?

drivers/misc/amd-sbi/tsi.c:sbtsi_driver_unref() {
	struct sbtsi_data *data = arg;

	kref_put(&data->kref, sbtsi_data_release);
}
Quoted 8 lines
drivers/misc/amd-sbi/tsi-core.c new file +8 -0
+		if (data->detached)
+			return -ENODEV;
+	}
+
+	kref_get(&data->kref);
+
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8