Skip to contentSkip to conversation

i2c: designware: add atomic transfer support for IRQ-off contexts

RFC PATCH In review Baineng Shou 3 participants 3 messages Aug 7 – Aug 31

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

RFC PATCH 1/1 i2c: designware: add atomic transfer support for IRQ-off contexts

The DesignWare I2C controller driver cannot perform transfers when IRQs are disabled, e.g. during noirq system resume where an I2C client (GPIO expander, PMIC) must be accessed before IRQs are
re-enabled: the interrupt-driven path calls wait_for_completion_timeout() which deadlocks with IRQs off.

The i2c core already routes to master_xfer_atomic() when i2c_in_atomic_xfer_mode() is true, but that gate requires system_state > SYSTEM_RUNNING, which does not hold during resume_noirq (system_state is already SYSTEM_RUNNING there). So the framework atomic path does not cover resume_noirq either, and designware does not implement master_xfer_atomic at all.

Implement i2c_dw_xfer_atomic() and register it as ->xfer_atomic. It reuses the existing i2c_dw_process_transfer() state machine (the TX/RX/STOP/ABRT handling is identical to the interrupt path) but:

- drives the clock directly via i2c_dw_prepare_clk() instead of pm_runtime (which may sleep),
- sets ACCESS_POLLING for the duration of the transfer so register reads use IC_RAW_INTR_STAT and the hardware interrupt is masked,
- polls with udelay() + a retry count instead of usleep_range() + jiffies, neither of which is safe with IRQs disabled (the tick is frozen so jiffies does not advance, and usleep_range() may sleep).

Also fall back to i2c_dw_xfer_atomic() from i2c_dw_xfer() when IRQs are disabled, since i2c_in_atomic_xfer_mode() does not cover resume_noirq.

This is an RFC: the resume_noirq coverage relies on the driver-side irqs_disabled() fallback because the framework gate is closed there. I'd like feedback on whether that fallback is acceptable or whether the gate should be widened in the i2c core instead.

---
 drivers/i2c/busses/i2c-designware-common.c |   1 +
 drivers/i2c/busses/i2c-designware-core.h   |   1 +
 drivers/i2c/busses/i2c-designware-master.c | 109 +++++++++++++++++++++
 3 files changed, 111 insertions(+)
drivers/i2c/busses/i2c-designware-common.c +1 -0
index e4dfa2ec58bb..561efcd39c7f 100644
@@ -873,6 +873,7 @@ static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
 
 static const struct i2c_algorithm i2c_dw_algo = {
 	.xfer = i2c_dw_xfer,
+	.xfer_atomic = i2c_dw_xfer_atomic,
 	.functionality = i2c_dw_func,
 #if IS_ENABLED(CONFIG_I2C_SLAVE)
 	.reg_slave = i2c_dw_reg_slave,
drivers/i2c/busses/i2c-designware-core.h +1 -0
index c71aa2dd368d..4bc22b53d470 100644
@@ -398,6 +398,7 @@ extern void i2c_dw_configure_master(struct dw_i2c_dev *dev);
 extern int i2c_dw_probe_master(struct dw_i2c_dev *dev);
 
 int i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
+int i2c_dw_xfer_atomic(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
 
 #if IS_ENABLED(CONFIG_I2C_SLAVE)
 extern void i2c_dw_configure_slave(struct dw_i2c_dev *dev);
drivers/i2c/busses/i2c-designware-master.c +109 -0

121 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

+Cc: @bigeasy (the question below)

Quoted 25 lines · Baineng Shou

The DesignWare I2C controller driver cannot perform transfers when IRQs are disabled, e.g. during noirq system resume where an I2C client (GPIO expander, PMIC) must be accessed before IRQs are
re-enabled: the interrupt-driven path calls wait_for_completion_timeout() which deadlocks with IRQs off.

The i2c core already routes to master_xfer_atomic() when i2c_in_atomic_xfer_mode() is true, but that gate requires system_state > SYSTEM_RUNNING, which does not hold during resume_noirq (system_state is already SYSTEM_RUNNING there). So the framework atomic path does not cover resume_noirq either, and designware does not implement master_xfer_atomic at all.

Implement i2c_dw_xfer_atomic() and register it as ->xfer_atomic. It reuses the existing i2c_dw_process_transfer() state machine (the TX/RX/STOP/ABRT handling is identical to the interrupt path) but:

- drives the clock directly via i2c_dw_prepare_clk() instead of pm_runtime (which may sleep),
- sets ACCESS_POLLING for the duration of the transfer so register reads use IC_RAW_INTR_STAT and the hardware interrupt is masked,
- polls with udelay() + a retry count instead of usleep_range() + jiffies, neither of which is safe with IRQs disabled (the tick is frozen so jiffies does not advance, and usleep_range() may sleep).

Quoted 3 lines

Also fall back to i2c_dw_xfer_atomic() from i2c_dw_xfer() when IRQs are disabled, since i2c_in_atomic_xfer_mode() does not cover resume_noirq.

The below paragraph should go...

Quoted 7 lines

This is an RFC: the resume_noirq coverage relies on the driver-side irqs_disabled() fallback because the framework gate is closed there. I'd like feedback on whether that fallback is acceptable or whether the gate should be widened in the i2c core instead.

---

...here as a comment / question.

drivers/i2c/busses/i2c-designware-master.c i2c_dw_xfer_common(struct dw_i2c_dev *dev, struct i2c_msg msgs[], int num) · line 922

Quoted 2 lines
drivers/i2c/busses/i2c-designware-master.c +2 -0
+/* Poll up to ~1s in 10us steps; bounded fallback for the IRQ-off path. */
+#define I2C_DESIGNWARE_ATOMIC_POLL_RETRIES	100000

Just add a constant for the step time, this will give some clarification to the above comment.

#define I2C_DESIGNWARE_ATOMIC_POLL_STEP_US 10

Quoted 15 lines
drivers/i2c/busses/i2c-designware-master.c +15 -0
+/*
+ * Atomic-context variant of i2c_dw_wait_transfer().  The normal polling
+ * path (ACCESS_POLLING branch in i2c_dw_wait_transfer()) uses usleep_range()
+ * and a jiffies deadline, neither of which is safe when IRQs are disabled
+ * (noirq system resume, shutdown): the tick is frozen so jiffies does not
+ * advance, and usleep_range() may sleep.  Poll IC_RAW_INTR_STAT with
+ * udelay() and a retry count instead, while reusing the shared
+ * i2c_dw_process_transfer() state machine so TX/RX/STOP/ABRT handling is
+ * identical to the interrupt path.  Caller must have set ACCESS_POLLING.
+ */
+static int i2c_dw_wait_transfer_atomic(struct dw_i2c_dev *dev)
+{
+	unsigned int stat;
+	int retries = I2C_DESIGNWARE_ATOMIC_POLL_RETRIES;
+

drivers/i2c/busses/i2c-designware-master.c i2c_dw_xfer_common(struct dw_i2c_dev *dev, struct i2c_msg msgs[], int num) · line 948

Quoted 10 lines
drivers/i2c/busses/i2c-designware-master.c +10 -0
+	do {
+		if (try_wait_for_completion(&dev->cmd_complete))
+			return 0;
+
+		stat = i2c_dw_read_clear_intrbits(dev);
+		if (stat)
+			i2c_dw_process_transfer(dev, stat);
+		else
+			udelay(10);
+	} while (--retries > 0);

' > 0' is redundant and gives actually off-by-one (the amount of retries).

Quoted 6 lines
drivers/i2c/busses/i2c-designware-master.c +6 -0
+	return -ETIMEDOUT;
+}
+
+/*
+ * i2c_dw_xfer_atomic - transfer messages in atomic context.
+ *

drivers/i2c/busses/i2c-designware-master.c i2c_dw_xfer_common(struct dw_i2c_dev *dev, struct i2c_msg msgs[], int num) · line 964

Quoted 10 lines
drivers/i2c/busses/i2c-designware-master.c +9 -0
+ * Used when IRQs are disabled, e.g. during noirq system resume where an
+ * I2C client (GPIO expander, PMIC) must be accessed before IRQs are
+ * re-enabled.  pm_runtime and mutexes may sleep, so drive the clock
+ * directly via i2c_dw_prepare_clk(); ACCESS_POLLING makes register reads
+ * use IC_RAW_INTR_STAT and routes the wait through
+ * i2c_dw_wait_transfer_atomic().
+ */
 
+int
+i2c_dw_xfer_atomic(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)

Make it a single line.

Quoted 28 lines
drivers/i2c/busses/i2c-designware-master.c +28 -0
+{
+	struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
+	unsigned int flags = dev->flags;
+	int ret;
+
+	dev->flags |= ACCESS_POLLING;
+
+	ret = i2c_dw_prepare_clk(dev, true);
+	if (ret)
+		goto out_flags;
+
+	ret = i2c_dw_acquire_lock(dev);
+	if (ret)
+		goto out_clk;
+
+	reinit_completion(&dev->cmd_complete);
+	dev->msgs = msgs;
+	dev->msgs_num = num;
+	dev->cmd_err = 0;
+	dev->msg_write_idx = 0;
+	dev->msg_read_idx = 0;
+	dev->msg_err = 0;
+	dev->status = 0;
+	dev->abort_source = 0;
+	dev->rx_outstanding = 0;
+
+	i2c_dw_xfer_init(dev);
+

drivers/i2c/busses/i2c-designware-master.c i2c_dw_xfer_common(struct dw_i2c_dev *dev, struct i2c_msg msgs[], int num) · line 1002

Quoted 10 lines
drivers/i2c/busses/i2c-designware-master.c +9 -0
+	ret = i2c_dw_wait_transfer_atomic(dev);
+
+	if (i2c_dw_is_controller_active(dev)) {
+		i2c_recover_bus(&dev->adapter);
+		i2c_dw_init(dev);
+	} else {
+		__i2c_dw_disable_nowait(dev);
+	}
 
+	if (!ret) {

Use traditional pattern, id est

	if (ret)
		goto out_release_lock;

drivers/i2c/busses/i2c-designware-master.c i2c_dw_xfer_common(struct dw_i2c_dev *dev, struct i2c_msg msgs[], int num) · line 1004

Quoted 2 lines
drivers/i2c/busses/i2c-designware-master.c +2 -0
+		if (likely(!dev->cmd_err && !dev->status))
+			ret = 0;
		goto out_release_lock;
Quoted 13 lines
drivers/i2c/busses/i2c-designware-master.c +13 -0
+		else if (dev->cmd_err == DW_IC_ERR_TX_ABRT)
+			ret = i2c_dw_handle_tx_abort(dev);
+		else
+			ret = -EIO;
+	}
+
+	i2c_dw_release_lock(dev);
+out_clk:
+	i2c_dw_prepare_clk(dev, false);
+out_flags:
+	dev->flags = flags;
+	return ret < 0 ? ret : num;
+}

...

drivers/i2c/busses/i2c-designware-master.c int i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) · line 1034

Quoted 9 lines
drivers/i2c/busses/i2c-designware-master.c +9 -0
+	/*
+	 * Fall back to the atomic path when IRQs are disabled, e.g. during
+	 * noirq system resume where an I2C client (GPIO expander, PMIC)
+	 * must be accessed before IRQs are re-enabled.  The i2c core's
+	 * i2c_in_atomic_xfer_mode() gate does not cover resume_noirq
+	 * (system_state is already SYSTEM_RUNNING there), so the driver has
+	 * to route the transfer itself.
+	 */
+	if (IS_ENABLED(CONFIG_PREEMPT_COUNT) ? !preemptible() : irqs_disabled())

I don't like this. Do we have something better for this? Perhaps @bigeasy knows?

Quoted 1 line
drivers/i2c/busses/i2c-designware-master.c +1 -0
+		return i2c_dw_xfer_atomic(adap, msgs, num);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 4 lines
drivers/i2c/busses/i2c-designware-master.c +4 -0
+	/*
+	 * Fall back to the atomic path when IRQs are disabled, e.g. during
+	 * noirq system resume where an I2C client (GPIO expander, PMIC)
+	 * must be accessed before IRQs are re-enabled.  The i2c core's
Quoted 8 lines

drivers/i2c/busses/i2c-designware-master.c int i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) · line 1034

Quoted 5 lines
drivers/i2c/busses/i2c-designware-master.c +5 -0
+	 * i2c_in_atomic_xfer_mode() gate does not cover resume_noirq
+	 * (system_state is already SYSTEM_RUNNING there), so the driver has
+	 * to route the transfer itself.
+	 */
+	if (IS_ENABLED(CONFIG_PREEMPT_COUNT) ? !preemptible() : irqs_disabled())

I don't like this. Do we have something better for this? Perhaps @bigeasy knows?

This looks hackish indeed. Couldn't this take an argument from the caller to learn if it is called from the atomic path or the regular? And could this be tested on PREEMPT_RT to figure out if it works as intended? I do remember that some driver creates a warning on PREEMPT_RT due to its atomic call path which is tested from probe. I don't remember anyone tested it on RT other than just shutting down the warning, claiming it is okay…

Quoted 2 lines

drivers/i2c/busses/i2c-designware-master.c int i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) · line 1035

Quoted 1 line
drivers/i2c/busses/i2c-designware-master.c +1 -0
+		return i2c_dw_xfer_atomic(adap, msgs, num);

Sebastian