Skip to contentSkip to conversation

i2c: rcar: Clear interrupt registers in probe()

RFC PATCH In review Dirk Behme 3 participants 5 messages Jul 2, 2024 – Jul 3, 2024

Subsystems (3)
lore
Share & tools
lore

Dirk Behme

author
#0 lore raw

RFC PATCH 1/1 i2c: rcar: Clear interrupt registers in probe()

We are getting crash reports where irqhandler crashes because it uses priv->msg being NULL. This can happen if an interrupt is issued before rcar_i2c_master_xfer() has initialized priv->msg.

The rcar_i2c_probe() function assumes that the I2C hardware is uninitialized and connects the interrupt handler via devm_request_irq(). From this point in time irqhandler can be called. Normally, this is just prevented by the I2C hardware being in reset state.

However, there might be cases where rcar_i2c_probe() is called, but the I2C hardware is *not* in reset state. E.g. if just the Linux operating system was re-started by a supervisor. But the hardware didn't get a reset. For cases like this make sure that the I2C hardware interrupts are cleared properly before devm_request_irq() is called.

This is inspired by rcar_i2c_init().

---
 drivers/i2c/busses/i2c-rcar.c | 6 ++++++
 1 file changed, 6 insertions(+)

Notes: This is untested. Does this make sense and is acceptable?

This might be testable by shutting down Linux to e.g. suspend to RAM. And then, instead of resume, re-start (cold start) the kernel. So that probe() is called and uses a non-reset I2C peripheral.

drivers/i2c/busses/i2c-rcar.c +6 -0
index 828aa2ea0fe4..5f46955167c4 100644
@@ -1183,6 +1183,12 @@ static int rcar_i2c_probe(struct platform_device *pdev)
 	ret = platform_get_irq(pdev, 0);
 	if (ret < 0)
 		goto out_pm_put;
+
+	/* reset master mode */
+	rcar_i2c_write(priv, ICMIER, 0);
+	rcar_i2c_write(priv, ICMCR, MDBS);
+	rcar_i2c_write(priv, ICMSR, 0);
+
 	priv->irq = ret;
 	ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv);
 	if (ret < 0) {
Signature · 1 line
2.28.0
#1 lore raw

Hi Dirk,

Quoted 15 lines · Dirk Behme

We are getting crash reports where irqhandler crashes because it uses priv->msg being NULL. This can happen if an interrupt is issued before rcar_i2c_master_xfer() has initialized priv->msg.

The rcar_i2c_probe() function assumes that the I2C hardware is uninitialized and connects the interrupt handler via devm_request_irq(). From this point in time irqhandler can be called. Normally, this is just prevented by the I2C hardware being in reset state.

However, there might be cases where rcar_i2c_probe() is called, but the I2C hardware is *not* in reset state. E.g. if just the Linux operating system was re-started by a supervisor. But the hardware didn't get a reset. For cases like this make sure that the I2C hardware interrupts are cleared properly before devm_request_irq() is called.

Quoted 3 lines

This is inspired by rcar_i2c_init().

Thanks for your patch!

drivers/i2c/busses/i2c-rcar.c static int rcar_i2c_probe(struct platform_device *pdev) · line 1190

Quoted 11 lines
drivers/i2c/busses/i2c-rcar.c +5 -0
@@ -1183,6 +1183,12 @@ static int rcar_i2c_probe(struct platform_device *pdev)
        ret = platform_get_irq(pdev, 0);
        if (ret < 0)
                goto out_pm_put;
+
+       /* reset master mode */
+       rcar_i2c_write(priv, ICMIER, 0);
+       rcar_i2c_write(priv, ICMCR, MDBS);
+       rcar_i2c_write(priv, ICMSR, 0);

LGTM, but I think you want to do the same for slave mode:

    rcar_i2c_write(priv, ICSIER, 0);
    rcar_i2c_write(priv, ICSCR, SDBS);
    rcar_i2c_write(priv, ICSSR, 0);

drivers/i2c/busses/i2c-rcar.c static int rcar_i2c_probe(struct platform_device *pdev) · line 1194

Quoted 4 lines
drivers/i2c/busses/i2c-rcar.c +1 -0
+
        priv->irq = ret;
        ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv);
        if (ret < 0) {

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
maintainer
#2 lore raw
In reply to Dirk Behme

Hi Dirk,

thank you for another valuable bug report!

Quoted 15 lines · Dirk Behme

We are getting crash reports where irqhandler crashes because it uses priv->msg being NULL. This can happen if an interrupt is issued before rcar_i2c_master_xfer() has initialized priv->msg.

The rcar_i2c_probe() function assumes that the I2C hardware is uninitialized and connects the interrupt handler via devm_request_irq(). From this point in time irqhandler can be called. Normally, this is just prevented by the I2C hardware being in reset state.

However, there might be cases where rcar_i2c_probe() is called, but the I2C hardware is *not* in reset state. E.g. if just the Linux operating system was re-started by a supervisor. But the hardware didn't get a reset. For cases like this make sure that the I2C hardware interrupts are cleared properly before devm_request_irq() is called.

Quoted 8 lines

This is inspired by rcar_i2c_init().

---
 drivers/i2c/busses/i2c-rcar.c | 6 ++++++
 1 file changed, 6 insertions(+)

Notes: This is untested. Does this make sense and is acceptable?

Yes, it makes sense to me. HW should be put to a sane state first in probe. IIRC this slipped through during some refactorization. Maybe it is also the reason for this code in the irq handler?

547 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
548 if (!(msr & MDE))
549 return;

Although this code existed even before the refactorization. Also, I will add a WARN there, otherwise we never know if this issue still exists.

Anyhow, I think Geert's comment also makes sense. But instead of just adding the instructions, I'd prefer to reorganize the code. I hope it is okay for you if I send a "counterpatch". I can't really tell what outcome I actually prefer before I try out myself.

Happy hacking,

Wolfram

maintainer
#3 lore raw
Quoted 2 lines

I hope it is okay for you if I send a "counterpatch".

Sent this now. Please try it if you have a chance.

I think your patch here might not work because you write to registers when the module clock could be off in most cases. My patch should be good there. As I said, proper testing scheduled for tomorrow.

#4 lore raw

Hi Wolfram,

Quoted 3 lines · Wolfram Sang

I think your patch here might not work because you write to registers when the module clock could be off in most cases. My patch should be good there. As I said, proper testing scheduled for tomorrow.

Good point, I had checked that, but totally missed the conditional pm_runtime_put() :-(

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds