Skip to contentSkip to conversation

gpio: mxc: Always set GPIOs used as interrupt source to INPUT mode

PATCH In review Marek Vasut 2 participants 3 messages Jul 24, 2022

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

PATCH v2 1/1 gpio: mxc: Always set GPIOs used as interrupt source to INPUT mode

Always configure GPIO pins which are used as interrupt source as INPUTs. In case the default pin configuration is OUTPUT, or the prior stage does configure the pins as OUTPUT, then Linux will not reconfigure the pin as INPUT and no interrupts are received.

Always configure interrupt source GPIO pin as input to fix the above case.

Cc Bartosz Golaszewski <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Loic Poulain <[email protected]>
Cc Marc Zyngier <[email protected]>

---
V2: Actually update and clear bits in GDIR register

---
 drivers/gpio/gpio-mxc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mxc.c +5 -1
index c871602fc5ba9..ed58441627d92 100644
@@ -147,7 +147,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
 {
 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
 	struct mxc_gpio_port *port = gc->private;
-	u32 bit, val;
+	u32 bit, val, dir;
 	u32 gpio_idx = d->hwirq;
 	int edge;
 	void __iomem *reg = port->base;
@@ -204,6 +204,10 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
 
 	writel(1 << gpio_idx, port->base + GPIO_ISR);
 
+	dir = readl(port->base + GPIO_GDIR);
+	dir &= ~BIT(gpio_idx);
+	writel(dir, port->base + GPIO_GDIR);
+
 	return 0;
 }
 
Signature · 1 line
2.35.1
#1 lore raw
Quoted 33 lines · Marek Vasut

Always configure GPIO pins which are used as interrupt source as INPUTs. In case the default pin configuration is OUTPUT, or the prior stage does configure the pins as OUTPUT, then Linux will not reconfigure the pin as INPUT and no interrupts are received.

Always configure interrupt source GPIO pin as input to fix the above case.

Cc Bartosz Golaszewski <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Loic Poulain <[email protected]>
Cc Marc Zyngier <[email protected]>

---
V2: Actually update and clear bits in GDIR register

---
 drivers/gpio/gpio-mxc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mxc.c +1 -1
index c871602fc5ba9..ed58441627d92 100644
@@ -147,7 +147,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
 {
 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
 	struct mxc_gpio_port *port = gc->private;
-	u32 bit, val;
+	u32 bit, val, dir;
 	u32 gpio_idx = d->hwirq;
 	int edge;
 	void __iomem *reg = port->base;

drivers/gpio/gpio-mxc.c static int gpio_set_irq_type(struct irq_data *d, u32 type) · line 210

Quoted 7 lines
drivers/gpio/gpio-mxc.c +4 -0
@@ -204,6 +204,10 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
 
 	writel(1 << gpio_idx, port->base + GPIO_ISR);
 
+	dir = readl(port->base + GPIO_GDIR);
+	dir &= ~BIT(gpio_idx);
+	writel(dir, port->base + GPIO_GDIR);
+

This is obviously extremely racy, as another CPU could be doing the same thing in parallel for a line that shares the same register. Looking at the driver, it is clear that it was all written at a time when SMP was only a pretty distant concern (i.e. the authors never considered it a possibility).

I'd be fine with it if there was no SMP platform using this, but there is at least one (imx7d).

So please fix this first (placing all RMW sequences behind a lock), and only then add this change.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#2 lore raw
Quoted 49 lines · Marc Zyngier
Quoted 33 lines · Marek Vasut

Always configure GPIO pins which are used as interrupt source as INPUTs. In case the default pin configuration is OUTPUT, or the prior stage does configure the pins as OUTPUT, then Linux will not reconfigure the pin as INPUT and no interrupts are received.

Always configure interrupt source GPIO pin as input to fix the above case.

Cc Bartosz Golaszewski <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Loic Poulain <[email protected]>
Cc Marc Zyngier <[email protected]>

---
V2: Actually update and clear bits in GDIR register

---
  drivers/gpio/gpio-mxc.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mxc.c +1 -1
index c871602fc5ba9..ed58441627d92 100644
@@ -147,7 +147,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
  {
  	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  	struct mxc_gpio_port *port = gc->private;
-	u32 bit, val;
+	u32 bit, val, dir;
  	u32 gpio_idx = d->hwirq;
  	int edge;
  	void __iomem *reg = port->base;

drivers/gpio/gpio-mxc.c static int gpio_set_irq_type(struct irq_data *d, u32 type) · line 210

Quoted 7 lines
drivers/gpio/gpio-mxc.c +4 -0
@@ -204,6 +204,10 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
 
  	writel(1 << gpio_idx, port->base + GPIO_ISR);
 
+	dir = readl(port->base + GPIO_GDIR);
+	dir &= ~BIT(gpio_idx);
+	writel(dir, port->base + GPIO_GDIR);
+

This is obviously extremely racy, as another CPU could be doing the same thing in parallel for a line that shares the same register. Looking at the driver, it is clear that it was all written at a time when SMP was only a pretty distant concern (i.e. the authors never considered it a possibility).

Quoted 5 lines

I'd be fine with it if there was no SMP platform using this, but there is at least one (imx7d).

So please fix this first (placing all RMW sequences behind a lock), and only then add this change.

Done in separate (new) patch for V3.