Skip to contentSkip to conversation

drm/xe/i2c: alerts and controller enabling modifications

PATCH Superseded Heikki Krogerus 4 participants 13 messages Jun 25 – Jul 9

Subsystems (5)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover drm/xe/i2c: alerts and controller enabling modifications

Hi,

Changed since v1:
- Global header for the DesignWare I2C registers which meant a bit of patch refactoring.
- Selecting CONFIG_SMBUS in CONFIG_XE and handling smbus in xe_i2c.c instead of separate file.
- Storing the alert device to the client array and providing enum for the clients.
- Allowing other fields in the IC_ENABLE register to be updated except the Enable bit.
- Can't sleep in xe_i2c_disable() so using udelay().

v1: https://lore.kernel.org/lkml/[email protected]/

This includes support for the SMBus alerts, and special handling for the IC_ENABLE register.

Thanks,

Heikki Krogerus (3):
i2c: designware: Global register definitions drm/xe/i2c: Handler for SMBus Alerts drm/xe/mcu_i2c: Take over control of the controller enabling

 MAINTAINERS                                |   1 +
 drivers/gpu/drm/xe/Kconfig                 |   1 +
 drivers/gpu/drm/xe/regs/xe_i2c_regs.h      |   2 +
 drivers/gpu/drm/xe/xe_i2c.c                | 108 +++++++++++++++++++--
 drivers/gpu/drm/xe/xe_i2c.h                |   6 ++
 drivers/i2c/busses/i2c-designware-common.c |   1 +
 drivers/i2c/busses/i2c-designware-core.h   |  84 +---------------
 drivers/i2c/busses/i2c-designware-master.c |   1 +
 include/linux/designware_i2c.h             | 107 ++++++++++++++++++++
 9 files changed, 222 insertions(+), 89 deletions(-)
 create mode 100644 include/linux/designware_i2c.h
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/3 i2c: designware: Global register definitions

Moving the register definitions to a global header file include/linux/designware_i2c.h. That removes the need to duplicate them in the adaptation layers for this driver outside of drivers/i2c/busses/. There is at least one of those in drivers/gpu/drm/xe/xe_i2c.c.

---
 MAINTAINERS                                |   1 +
 drivers/i2c/busses/i2c-designware-common.c |   1 +
 drivers/i2c/busses/i2c-designware-core.h   |  84 +---------------
 drivers/i2c/busses/i2c-designware-master.c |   1 +
 include/linux/designware_i2c.h             | 107 +++++++++++++++++++++
 5 files changed, 111 insertions(+), 83 deletions(-)
 create mode 100644 include/linux/designware_i2c.h
MAINTAINERS +1 -0
index 4f176bcaa09e4..ca1dd05e79c65 100644
@@ -25947,6 +25947,7 @@ R:	Andy Shevchenko <[email protected]>
 L:	[email protected]
 S:	Supported
 F:	drivers/i2c/busses/i2c-designware-*
+F:	include/linux/designware_i2c.h
 
 SYNOPSYS DESIGNWARE I2C DRIVER - AMDISP
 M:	Nirujogi Pratap <[email protected]>
drivers/i2c/busses/i2c-designware-common.c +1 -0
index 4dc57fd561703..1e5aba28e4942 100644
@@ -16,6 +16,7 @@
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/device.h>
+#include <linux/designware_i2c.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/export.h>
drivers/i2c/busses/i2c-designware-core.h +1 -83

111 lines not on this page · raw message

drivers/i2c/busses/i2c-designware-master.c +1 -0
index de929b91d5ead..3b113df0f28e0 100644
@@ -12,6 +12,7 @@
 #define DEFAULT_SYMBOL_NAMESPACE	"I2C_DW"
 
 #include <linux/delay.h>
+#include <linux/designware_i2c.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/export.h>
include/linux/designware_i2c.h new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
2.50.1
reviewer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] i2c: designware: Global register definitions

Quoted 5 lines · Heikki Krogerus

Moving the register definitions to a global header file include/linux/designware_i2c.h. That removes the need to duplicate them in the adaptation layers for this driver outside of drivers/i2c/busses/. There is at least one of those in drivers/gpu/drm/xe/xe_i2c.c.

OK!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH v2 2/3 drm/xe/i2c: Handler for SMBus Alerts

In reply to Heikki Krogerus

Some devices that are attached to the I2C controller use the SMBus Alert signal for example to inform the host about thermal events, so registering the default SMBus Alert device for them. The alert device makes sure that the alert is processed and passed to the correct I2C client driver.

---
 drivers/gpu/drm/xe/Kconfig            |  1 +
 drivers/gpu/drm/xe/regs/xe_i2c_regs.h |  2 +
 drivers/gpu/drm/xe/xe_i2c.c           | 53 +++++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_i2c.h           |  5 +++
 4 files changed, 57 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/Kconfig +1 -0
index 4d7dcaff2b915..7045ba19943e3 100644
@@ -46,6 +46,7 @@ config DRM_XE
 	select WANT_DEV_COREDUMP
 	select AUXILIARY_BUS
 	select REGMAP if I2C
+	select I2C_SMBUS
 	help
 	  Driver for Intel Xe2 series GPUs and later. Experimental support
 	  for Xe series is also available.
drivers/gpu/drm/xe/regs/xe_i2c_regs.h +2 -0
index f2e455e2bfe45..37550e4a20f80 100644
@@ -20,4 +20,6 @@
 #define I2C_CONFIG_CMD			XE_REG(I2C_CONFIG_SPACE_OFFSET + PCI_COMMAND)
 #define I2C_CONFIG_PMCSR		XE_REG(I2C_CONFIG_SPACE_OFFSET + 0x84)
 
+#define I2C_REG(reg)			XE_REG((reg) + I2C_MEM_SPACE_OFFSET)
+
 #endif /* _XE_I2C_REGS_H_ */
drivers/gpu/drm/xe/xe_i2c.c +49 -4

105 lines not on this page · raw message

drivers/gpu/drm/xe/xe_i2c.h +5 -0
index 425d8160835f4..b3327db998708 100644
@@ -23,6 +23,11 @@ struct xe_mmio;
 /* Endpoint Capabilities */
 #define XE_I2C_EP_CAP_IRQ		BIT(0)
 
+enum XE_I2C_CLIENT {
+	XE_I2C_CLIENT_AMC,
+	XE_I2C_CLIENT_ALERT,
+};
+
 struct xe_i2c_endpoint {
 	u8 cookie;
 	u8 capabilities;
Signature · 1 line
2.50.1
#4 lore raw

Re: [PATCH v2 2/3] drm/xe/i2c: Handler for SMBus Alerts

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

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

New issues:
- [Critical] Unconditional dereference of a potentially NULL I2C client pointer when handling SMBus alerts.
- [High] Normal I2C adapter interrupts are not propagated to the adapter driver on the XE_CRESCENTISLAND platform, breaking standard I2C transfers.
- [High] Writing to a read-to-clear interrupt status register will fail to clear the interrupt, leading to an interrupt storm.
- [Low] DRM_XE unconditionally selects I2C_SMBUS, causing unmet dependencies and build failures when I2C is not enabled.

Pre-existing issues:
- [High] Missing cancel_work_sync() in the device cleanup path allows pending asynchronous work to access freed memory. --

commit 99e285e8af69e8587ef5c6dca5bee3189103c64e
Author: Heikki Krogerus <[email protected]>
Subject: drm/xe/i2c: Handler for SMBus Alerts

This commit adds support for handling SMBus alerts in the XE I2C driver. It introduces a new client for the alert device and an interrupt handling path to forward alerts to the SMBus core.

drivers/gpu/drm/xe/Kconfig config DRM_XE · line 49

Quoted 8 lines
drivers/gpu/drm/xe/Kconfig +1 -0
@@ -46,6 +46,7 @@ config DRM_XE
 	select WANT_DEV_COREDUMP
 	select AUXILIARY_BUS
 	select REGMAP if I2C
+	select I2C_SMBUS

[Severity: Low]
Does this select statement need to be guarded conditionally or have a dependency on I2C added to DRM_XE?

Selecting I2C_SMBUS unconditionally without inheriting its dependency on I2C can cause unmet dependency warnings and build failures when I2C is disabled.

Quoted 3 lines
drivers/gpu/drm/xe/xe_i2c.c +0 -0

[ ... ]

drivers/gpu/drm/xe/xe_i2c.c static inline void xe_i2c_read_endpoint(struct xe_mmio *mmio, void *ep) · line 76

Quoted 13 lines
drivers/gpu/drm/xe/xe_i2c.c +9 -0
@@ -61,6 +65,37 @@ static inline void xe_i2c_read_endpoint(struct xe_mmio *mmio, void *ep)
 	val[1] = xe_mmio_read32(mmio, REG_SG_REMAP_ADDR_POSTFIX);
 }
 
+static void xe_i2c_handle_smbus_alert(struct xe_i2c *i2c)
+{
+	u32 stat;
+
+	stat = xe_mmio_read32(i2c->mmio, I2C_REG(DW_IC_SMBUS_INTR_STAT));
+	if (!stat)
+		return;
+
+	xe_mmio_write32(i2c->mmio, I2C_REG(DW_IC_CLR_SMBUS_INTR), stat);

[Severity: High]
Will this successfully clear the interrupt?

Synopsys DesignWare I2C DW_IC_CLR_* registers typically use read-to-clear semantics. Writing to this register might not clear the hardware interrupt, potentially leading to an interrupt storm when the ISR returns.

drivers/gpu/drm/xe/xe_i2c.c static inline void xe_i2c_read_endpoint(struct xe_mmio *mmio, void *ep) · line 79

Quoted 3 lines
drivers/gpu/drm/xe/xe_i2c.c +3 -0
+
+	if (stat & DW_IC_SMBUS_INTR_ALERT)
+		i2c_handle_smbus_alert(i2c->client[XE_I2C_CLIENT_ALERT]);

[Severity: Critical]
Is it possible for i2c->client[XE_I2C_CLIENT_ALERT] to be NULL here?

The client registration happens asynchronously in xe_i2c_client_work(). If an SMBus alert hardware interrupt fires before the async workqueue executes, or if the client registration fails, passing a NULL pointer to i2c_handle_smbus_alert() will cause a NULL pointer dereference.

Quoted 1 line
drivers/gpu/drm/xe/xe_i2c.c +1 -0
+}

[ ... ]

drivers/gpu/drm/xe/xe_i2c.c void xe_i2c_irq_handler(struct xe_device *xe, u32 master_ctl) · line 228

Quoted 9 lines
drivers/gpu/drm/xe/xe_i2c.c +4 -1
@@ -182,7 +222,10 @@ void xe_i2c_irq_handler(struct xe_device *xe, u32 master_ctl)
 		return;
 
 	/* Forward interrupt to I2C adapter */
-	generic_handle_irq_safe(xe->i2c->adapter_irq);
+	if (xe->info.platform == XE_CRESCENTISLAND)
+		xe_i2c_handle_smbus_alert(xe->i2c);
+	else
+		generic_handle_irq_safe(xe->i2c->adapter_irq);

[Severity: High]
Does this prevent regular I2C adapter interrupts from being handled on the XE_CRESCENTISLAND platform?

By placing generic_handle_irq_safe() in the else branch, it seems normal I2C transfers might time out and fail because their hardware interrupts are entirely skipped on this platform.

Quoted 2 lines
drivers/gpu/drm/xe/xe_i2c.c +0 -0
 	/* Deassert after I2C adapter clears the interrupt */
 	xe_mmio_rmw32(mmio, I2C_CONFIG_CMD, 0, PCI_COMMAND_INTX_DISABLE);

[ ... ]

drivers/gpu/drm/xe/xe_i2c.c static void xe_i2c_remove(void *data) · line 359

Quoted 12 lines
drivers/gpu/drm/xe/xe_i2c.c +3 -1
@@ -307,8 +350,10 @@ static void xe_i2c_remove(void *data)
 	struct xe_i2c *i2c = data;
 	unsigned int i;
 
-	for (i = 0; i < XE_I2C_MAX_CLIENTS; i++)
+	for (i = 0; i < XE_I2C_MAX_CLIENTS; i++) {
 		i2c_unregister_device(i2c->client[i]);
+		i2c->client[i] = NULL;
+	}
 
 	bus_unregister_notifier(&i2c_bus_type, &i2c->bus_notifier);
 	xe_i2c_unregister_adapter(i2c);

[Severity: High]
This is a pre-existing issue, but is there a missing call to cancel_work_sync() in this teardown path?

The asynchronous workqueue item i2c->work is scheduled when the I2C adapter is added. If this device is removed while the work is still pending or running, xe_i2c_client_work() could dereference freed memory after xe_i2c_remove() completes.

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

Re: [PATCH v2 2/3] drm/xe/i2c: Handler for SMBus Alerts

In reply to Heikki Krogerus
Quoted 5 lines · Heikki Krogerus

Some devices that are attached to the I2C controller use the SMBus Alert signal for example to inform the host about thermal events, so registering the default SMBus Alert device for them. The alert device makes sure that the alert is processed and passed to the correct I2C client driver.

...

drivers/gpu/drm/xe/xe_i2c.c static inline void xe_i2c_read_endpoint(struct xe_mmio *mmio, void *ep) · line 87

Quoted 6 lines
drivers/gpu/drm/xe/xe_i2c.c +6 -0
+static int xe_i2c_register_smbus_alert(struct xe_i2c *i2c)
+{
+	struct xe_device *xe = kdev_to_xe_device(i2c->drm_dev);
+	struct i2c_client *alert;
+
+	if (xe->info.platform != XE_CRESCENTISLAND)

Rather, introduce a has_i2c_smbus flag in xe_device_types.h and keep things platform agnostic. I think you'll also need it to early return from xe_i2c_create_irq().

Quoted 10 lines
drivers/gpu/drm/xe/xe_i2c.c +10 -0
+		return 0;
+
+	alert = i2c_new_smbus_alert_device(i2c->adapter, &xe_i2c_smbus_setup);
+	if (IS_ERR(alert))
+		return PTR_ERR(alert);
+
+	i2c->client[XE_I2C_CLIENT_ALERT] = alert;
+
+	return 0;
+}

...

drivers/gpu/drm/xe/xe_i2c.c void xe_i2c_irq_handler(struct xe_device *xe, u32 master_ctl) · line 225

Quoted 6 lines
drivers/gpu/drm/xe/xe_i2c.c +1 -1
@@ -182,7 +222,10 @@ void xe_i2c_irq_handler(struct xe_device *xe, u32 master_ctl)
 		return;
 
 	/* Forward interrupt to I2C adapter */
-	generic_handle_irq_safe(xe->i2c->adapter_irq);
+	if (xe->info.platform == XE_CRESCENTISLAND)

Ditto.

Raag

Quoted 3 lines
drivers/gpu/drm/xe/xe_i2c.c +3 -0
+		xe_i2c_handle_smbus_alert(xe->i2c);
+	else
+		generic_handle_irq_safe(xe->i2c->adapter_irq);
#6 lore raw

Re: [PATCH v2 2/3] drm/xe/i2c: Handler for SMBus Alerts

Quoted 9 lines · Raag Jadav
Quoted 5 lines · Heikki Krogerus

Some devices that are attached to the I2C controller use the SMBus Alert signal for example to inform the host about thermal events, so registering the default SMBus Alert device for them. The alert device makes sure that the alert is processed and passed to the correct I2C client driver.

...

Quoted 10 lines

drivers/gpu/drm/xe/xe_i2c.c static inline void xe_i2c_read_endpoint(struct xe_mmio *mmio, void *ep) · line 87

Quoted 6 lines
drivers/gpu/drm/xe/xe_i2c.c +6 -0
+static int xe_i2c_register_smbus_alert(struct xe_i2c *i2c)
+{
+	struct xe_device *xe = kdev_to_xe_device(i2c->drm_dev);
+	struct i2c_client *alert;
+
+	if (xe->info.platform != XE_CRESCENTISLAND)

Rather, introduce a has_i2c_smbus flag in xe_device_types.h and keep things platform agnostic. I think you'll also need it to early return from xe_i2c_create_irq().

OK. Feature flags are nice.

Thanks!

Signature · 1 line
heikki
#7 lore raw

Re: [PATCH v2 2/3] drm/xe/i2c: Handler for SMBus Alerts

Hi Raag,

Quoted 21 lines · Heikki Krogerus
Quoted 9 lines · Raag Jadav
Quoted 5 lines · Heikki Krogerus

Some devices that are attached to the I2C controller use the SMBus Alert signal for example to inform the host about thermal events, so registering the default SMBus Alert device for them. The alert device makes sure that the alert is processed and passed to the correct I2C client driver.

...

Quoted 10 lines

drivers/gpu/drm/xe/xe_i2c.c static inline void xe_i2c_read_endpoint(struct xe_mmio *mmio, void *ep) · line 87

Quoted 6 lines
drivers/gpu/drm/xe/xe_i2c.c +6 -0
+static int xe_i2c_register_smbus_alert(struct xe_i2c *i2c)
+{
+	struct xe_device *xe = kdev_to_xe_device(i2c->drm_dev);
+	struct i2c_client *alert;
+
+	if (xe->info.platform != XE_CRESCENTISLAND)

Rather, introduce a has_i2c_smbus flag in xe_device_types.h and keep things platform agnostic. I think you'll also need it to early return from xe_i2c_create_irq().

Quoted 1 line

OK. Feature flags are nice.

There is an issue with the interrupts. Right now with mainline the i2c-designware enables the interrupt because it is supplied with the adapter_irq, even though the interupt can only be used for these alerts. So that really has to be fixed.

I'm going to drop this alert handler, and instead propose that we process the alerts from AMC directly here in Xe, and at the same time remove the problematic adapter_irq completely like you proposed earlier.

Thanks,

Signature · 1 line
heikki
#8 lore raw

Re: [PATCH v2 2/3] drm/xe/i2c: Handler for SMBus Alerts

In reply to Heikki Krogerus
Quoted 5 lines · Heikki Krogerus

Some devices that are attached to the I2C controller use the SMBus Alert signal for example to inform the host about thermal events, so registering the default SMBus Alert device for them. The alert device makes sure that the alert is processed and passed to the correct I2C client driver.

...

drivers/gpu/drm/xe/xe_i2c.h struct xe_mmio; · line 28

Quoted 3 lines
drivers/gpu/drm/xe/xe_i2c.h +3 -0
+enum XE_I2C_CLIENT {
+	XE_I2C_CLIENT_AMC,
+	XE_I2C_CLIENT_ALERT,

Forgot to add, with this I think you can move XE_I2C_MAX_CLIENTS define here.

Raag

Quoted 1 line

+};

#9 lore raw

Re: [PATCH v2 2/3] drm/xe/i2c: Handler for SMBus Alerts

Quoted 7 lines · Raag Jadav
Quoted 5 lines · Heikki Krogerus

Some devices that are attached to the I2C controller use the SMBus Alert signal for example to inform the host about thermal events, so registering the default SMBus Alert device for them. The alert device makes sure that the alert is processed and passed to the correct I2C client driver.

Quoted 8 lines

...

drivers/gpu/drm/xe/xe_i2c.h struct xe_mmio; · line 28

Quoted 3 lines
drivers/gpu/drm/xe/xe_i2c.h +3 -0
+enum XE_I2C_CLIENT {
+	XE_I2C_CLIENT_AMC,
+	XE_I2C_CLIENT_ALERT,

Forgot to add, with this I think you can move XE_I2C_MAX_CLIENTS define here.

Ah, makes sense. I'll fix that.

Thanks!

Signature · 1 line
heikki
#10 lore raw

PATCH v2 3/3 drm/xe/mcu_i2c: Take over control of the controller enabling

In reply to Heikki Krogerus

Some platforms make an assumption that the i2c controller's enabled state indicates also the power state of the controller. This can create a problem when the controller is in disabled state, because the hardware may assume incorrectly that it is then also in low-power state.

To fix this, the controller is kept enabled by taking over the IC_ENABLE register. The controller has to be disabled when the configuration is updated and when the target address or the slave address are assigned, so disabling it when IC_CON, IC_TAR or IC_SAR registers are programmed, and then re-enabling it again.

---
 drivers/gpu/drm/xe/xe_i2c.c | 55 +++++++++++++++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_i2c.h |  1 +
 2 files changed, 54 insertions(+), 2 deletions(-)
drivers/gpu/drm/xe/xe_i2c.c +53 -2
index 0495b561957a2..e2d8e77653cf2 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_print.h>
 #include <linux/array_size.h>
 #include <linux/container_of.h>
+#include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/designware_i2c.h>
 #include <linux/err.h>
@@ -294,11 +295,40 @@ static void xe_i2c_remove_irq(struct xe_i2c *i2c)
 	irq_domain_remove(i2c->irqdomain);
 }
 
+/* See "Disabling DW_apb_i2c" in the DesignWare DW_abp_i2c databook. */
+static void xe_i2c_disable(struct xe_i2c *i2c)
+{
+	int timeout = 100;
+	u32 status;
+
+	xe_mmio_rmw32(i2c->mmio, I2C_REG(DW_IC_ENABLE), 1, 0);
+
+	do {
+		status = xe_mmio_read32(i2c->mmio, I2C_REG(DW_IC_ENABLE_STATUS));
+		if (!(status & 1))
+			return;
+		/* Can't sleep here. */
+		udelay(25);
+	} while (timeout--);
+
+	dev_warn(&i2c->adapter->dev, "timeout in disabling adapter\n");
+}
+
 static int xe_i2c_read(void *context, unsigned int reg, unsigned int *val)
 {
 	struct xe_i2c *i2c = context;
 
-	*val = xe_mmio_read32(i2c->mmio, XE_REG(reg + I2C_MEM_SPACE_OFFSET));
+	*val = xe_mmio_read32(i2c->mmio, I2C_REG(reg));
+
+	switch (reg) {
+	case DW_IC_ENABLE:
+	case DW_IC_ENABLE_STATUS:
+		FIELD_MODIFY(DW_IC_ENABLE_ENABLE, val,
+			     i2c->ic_enable & DW_IC_ENABLE_ENABLE);
+		break;
+	default:
+		break;
+	}
 
 	return 0;
 }
@@ -307,7 +337,28 @@ static int xe_i2c_write(void *context, unsigned int reg, unsigned int val)
 {
 	struct xe_i2c *i2c = context;
 
-	xe_mmio_write32(i2c->mmio, XE_REG(reg + I2C_MEM_SPACE_OFFSET), val);
+	switch (reg) {
+	case DW_IC_CON:
+	case DW_IC_TAR:
+	case DW_IC_SAR:
+		/* Disable the controller. */
+		xe_i2c_disable(i2c);
+
+		/* Write the register. */
+		xe_mmio_write32(i2c->mmio, I2C_REG(reg), val);
+
+		/* Enable the controller. */
+		xe_mmio_rmw32(i2c->mmio, I2C_REG(DW_IC_ENABLE), 0, 1);
+		break;
+	case DW_IC_ENABLE:
+		i2c->ic_enable = val;
+		/* Other fields can be updated except the enable bit. */
+		val |= DW_IC_ENABLE_ENABLE;
+		fallthrough;
+	default:
+		xe_mmio_write32(i2c->mmio, I2C_REG(reg), val);
+		break;
+	}
 
 	return 0;
 }
drivers/gpu/drm/xe/xe_i2c.h +1 -0
index b3327db998708..231e36df09017 100644
@@ -39,6 +39,7 @@ struct xe_i2c {
 	struct platform_device *pdev;
 	struct i2c_adapter *adapter;
 	struct i2c_client *client[XE_I2C_MAX_CLIENTS];
+	unsigned int ic_enable;
 
 	struct notifier_block bus_notifier;
 	struct work_struct work;
Signature · 1 line
2.50.1
#11 lore raw

Re: [PATCH v2 3/3] drm/xe/mcu_i2c: Take over control of the controller enabling

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] Faking the disabled state of the I2C controller while keeping it physically enabled causes an…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Faking the disabled state of the I2C controller while keeping it physically enabled causes an unhandled IRQ storm if the hardware generates an unmasked interrupt.
- [Critical] Bypassing the core driver's wait loop shifts the wait into a much shorter atomic context, causing Target Address (TAR) writes to be silently dropped if the bus is active.
- [High] I2C timing and FIFO configuration registers are silently ignored by the hardware because the controller is forcibly kept enabled and the regmap interceptor fails to temporarily disable it.
- [High] Dereferencing i2c->adapter in xe_i2c_disable causes a NULL pointer dereference if a timeout occurs during device probe.
--

commit ef1d705e3f064c441441c723d0ee6ac370743a6b
Author: Heikki Krogerus <[email protected]>

drm/xe/mcu_i2c: Take over control of the controller enabling

This commit modifies the mcu_i2c driver to maintain the I2C controller in an enabled state to prevent incorrect low-power state assumptions by the hardware. It intercepts writes to the IC_ENABLE register and temporarily disables the controller when programming configuration and target address registers.

Quoted 4 lines
drivers/gpu/drm/xe/xe_i2c.c +0 -0
index 0495b561957a2..e2d8e77653cf2 100644

[ ... ]

Quoted 11 lines
drivers/gpu/drm/xe/xe_i2c.c +7 -0
@@ -294,11 +295,40 @@ static void xe_i2c_remove_irq(struct xe_i2c *i2c)
 	irq_domain_remove(i2c->irqdomain);
 }
 
+/* See "Disabling DW_apb_i2c" in the DesignWare DW_abp_i2c databook. */
+static void xe_i2c_disable(struct xe_i2c *i2c)
+{
+	int timeout = 100;
+	u32 status;
+
+	xe_mmio_rmw32(i2c->mmio, I2C_REG(DW_IC_ENABLE), 1, 0);

drivers/gpu/drm/xe/xe_i2c.c static void xe_i2c_remove_irq(struct xe_i2c *i2c) · line 314

Quoted 10 lines
drivers/gpu/drm/xe/xe_i2c.c +10 -0
+
+	do {
+		status = xe_mmio_read32(i2c->mmio, I2C_REG(DW_IC_ENABLE_STATUS));
+		if (!(status & 1))
+			return;
+		/* Can't sleep here. */
+		udelay(25);
+	} while (timeout--);
+
+	dev_warn(&i2c->adapter->dev, "timeout in disabling adapter\n");

[Severity: High]
If a timeout occurs during device probe when i2c_dw_init() writes to DW_IC_CON, does this dereference a NULL pointer?

During probe, xe_i2c_write() temporarily disables the hardware via xe_i2c_disable(). If this times out, it logs a warning using i2c->adapter, but i2c->adapter appears to only be assigned later during the BUS_NOTIFY_ADD_DEVICE notifier.

Quoted 5 lines
drivers/gpu/drm/xe/xe_i2c.c +2 -0
+}
+
 static int xe_i2c_read(void *context, unsigned int reg, unsigned int *val)
 {
 	struct xe_i2c *i2c = context;

drivers/gpu/drm/xe/xe_i2c.c static void xe_i2c_remove_irq(struct xe_i2c *i2c) · line 328

Quoted 10 lines
drivers/gpu/drm/xe/xe_i2c.c +8 -1
 
-	*val = xe_mmio_read32(i2c->mmio, XE_REG(reg + I2C_MEM_SPACE_OFFSET));
+	*val = xe_mmio_read32(i2c->mmio, I2C_REG(reg));
+
+	switch (reg) {
+	case DW_IC_ENABLE:
+	case DW_IC_ENABLE_STATUS:
+		FIELD_MODIFY(DW_IC_ENABLE_ENABLE, val,
+			     i2c->ic_enable & DW_IC_ENABLE_ENABLE);
+		break;

[Severity: Critical]
Will this spoofed status cause an unhandled interrupt storm if the hardware generates an unmasked interrupt?

Because the physical controller remains enabled while xe_i2c_read() returns a faked disabled status, there is a window where an interrupt can fire. When i2c_dw_isr_master() runs, it reads the spoofed disabled status, assumes the interrupt is spurious, and immediately returns IRQ_NONE without clearing it. Does this lead to the hardware continuously asserting the interrupt?

Quoted 10 lines
drivers/gpu/drm/xe/xe_i2c.c +3 -0
+	default:
+		break;
+	}
 
 	return 0;
 }
@@ -307,7 +337,28 @@ static int xe_i2c_write(void *context, unsigned int reg, unsigned int val)
 {
 	struct xe_i2c *i2c = context;
 

drivers/gpu/drm/xe/xe_i2c.c static int xe_i2c_write(void *context, unsigned int reg, unsigned int val) · line 348

Quoted 10 lines
drivers/gpu/drm/xe/xe_i2c.c +9 -1
-	xe_mmio_write32(i2c->mmio, XE_REG(reg + I2C_MEM_SPACE_OFFSET), val);
+	switch (reg) {
+	case DW_IC_CON:
+	case DW_IC_TAR:
+	case DW_IC_SAR:
+		/* Disable the controller. */
+		xe_i2c_disable(i2c);
+
+		/* Write the register. */
+		xe_mmio_write32(i2c->mmio, I2C_REG(reg), val);

[Severity: Critical]
Is it possible for Target Address writes to be silently dropped if the bus is active and disabling takes longer than 2.5ms?

Since xe_i2c_read() spoofs DW_IC_ENABLE_STATUS, the core dw_i2c driver's sleep-capable wait loop in __i2c_dw_disable() exits immediately. This shifts the wait burden to xe_i2c_disable() which runs under a regmap spinlock with a 2.5ms maximum timeout (100 iterations of udelay(25)).

If xe_i2c_disable() times out and returns silently, xe_i2c_write() blindly writes the new target address (DW_IC_TAR) while the hardware is still physically enabled. Because the hardware discards this write, could subsequent data payloads be erroneously sent to the previously addressed slave device?

drivers/gpu/drm/xe/xe_i2c.c static int xe_i2c_write(void *context, unsigned int reg, unsigned int val) · line 360

Quoted 12 lines
drivers/gpu/drm/xe/xe_i2c.c +12 -0
+
+		/* Enable the controller. */
+		xe_mmio_rmw32(i2c->mmio, I2C_REG(DW_IC_ENABLE), 0, 1);
+		break;
+	case DW_IC_ENABLE:
+		i2c->ic_enable = val;
+		/* Other fields can be updated except the enable bit. */
+		val |= DW_IC_ENABLE_ENABLE;
+		fallthrough;
+	default:
+		xe_mmio_write32(i2c->mmio, I2C_REG(reg), val);
+		break;

[Severity: High]
Are timing and FIFO registers silently ignored by the hardware when they fall through to this default case?

The DesignWare I2C Databook requires that timing registers (e.g., DW_IC_SS_SCL_HCNT) and FIFO thresholds are written when the controller is disabled. Since this patch forces the hardware to remain enabled, any writes to registers that fall through to the default case are executed while the hardware is still enabled, which causes the hardware to silently discard them.

Quoted 4 lines
drivers/gpu/drm/xe/xe_i2c.c +1 -0
+	}
 
 	return 0;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#12 lore raw

Re: [PATCH v2 3/3] drm/xe/mcu_i2c: Take over control of the controller enabling

In reply to Heikki Krogerus
Quoted 6 lines · Heikki Krogerus

Some platforms make an assumption that the i2c controller's enabled state indicates also the power state of the controller. This can create a problem when the controller is in disabled state, because the hardware may assume incorrectly that it is then also in low-power state.

Quoted 6 lines

To fix this, the controller is kept enabled by taking over the IC_ENABLE register. The controller has to be disabled when the configuration is updated and when the target address or the slave address are assigned, so disabling it when IC_CON, IC_TAR or IC_SAR registers are programmed, and then re-enabling it again.

I don't have enough context here, so I'll leave this one to the maintainers.

Raag