From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Date: Mon, 9 May 2022 16:04:08 +0200
Subject: [PATCH] genirq: Provide generic_handle_domain_irq_safe().

Provide generic_handle_domain_irq_safe() which can used from any context.
This similar to commit
   509853f9e1e7b ("genirq: Provide generic_handle_irq_safe()")

but this time for the irq-domains interface. It has been reported for
the amd-pinctrl driver via bugzilla
   https://bugzilla.kernel.org/show_bug.cgi?id=215954

I looked around and added a few users so it is not just one user API :)
Instead of generic_handle_irq(irq_find_mapping)) one can use
generic_handle_domain_irq().

The problem with generic_handle_domain_irq() is that with `threadirqs'
it will trigger "WARN_ON_ONCE(!in_hardirq())". That interrupt handler
can't be marked non-threaded because it is a shared handler (it is
marked as such and I can't tell the interrupt can be really shared on
the system).
Ignoring the just mentioned warning, on PREEMPT_RT the threaded handler
is invoked with enabled interrupts leading other problems.

Do we do this?

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Link: https://lore.kernel.org/r/YnkfWFzvusFFktSt@linutronix.de
---
 drivers/bcma/driver_gpio.c                 |    2 +-
 drivers/gpio/gpio-mlxbf2.c                 |    6 ++----
 drivers/pinctrl/pinctrl-amd.c              |    2 +-
 drivers/platform/x86/intel/int0002_vgpio.c |    3 +--
 drivers/ssb/driver_gpio.c                  |    6 ++++--
 include/linux/irqdesc.h                    |    1 +
 kernel/irq/irqdesc.c                       |   24 ++++++++++++++++++++++++
 7 files changed, 34 insertions(+), 10 deletions(-)

Index: linux-5.18.0-rt11/drivers/bcma/driver_gpio.c
===================================================================
--- linux-5.18.0-rt11.orig/drivers/bcma/driver_gpio.c
+++ linux-5.18.0-rt11/drivers/bcma/driver_gpio.c
@@ -113,7 +113,7 @@ static irqreturn_t bcma_gpio_irq_handler
 		return IRQ_NONE;
 
 	for_each_set_bit(gpio, &irqs, gc->ngpio)
-		generic_handle_irq(irq_find_mapping(gc->irq.domain, gpio));
+		generic_handle_domain_irq_safe(gc->irq.domain, gpio);
 	bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
 
 	return IRQ_HANDLED;
Index: linux-5.18.0-rt11/drivers/gpio/gpio-mlxbf2.c
===================================================================
--- linux-5.18.0-rt11.orig/drivers/gpio/gpio-mlxbf2.c
+++ linux-5.18.0-rt11/drivers/gpio/gpio-mlxbf2.c
@@ -273,10 +273,8 @@ static irqreturn_t mlxbf2_gpio_irq_handl
 	pending = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_CAUSE_EVTEN0);
 	writel(pending, gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
 
-	for_each_set_bit(level, &pending, gc->ngpio) {
-		int gpio_irq = irq_find_mapping(gc->irq.domain, level);
-		generic_handle_irq(gpio_irq);
-	}
+	for_each_set_bit(level, &pending, gc->ngpio)
+		generic_handle_domain_irq_safe(gc->irq.domain, level);
 
 	return IRQ_RETVAL(pending);
 }
Index: linux-5.18.0-rt11/drivers/pinctrl/pinctrl-amd.c
===================================================================
--- linux-5.18.0-rt11.orig/drivers/pinctrl/pinctrl-amd.c
+++ linux-5.18.0-rt11/drivers/pinctrl/pinctrl-amd.c
@@ -638,7 +638,7 @@ static bool do_amd_gpio_irq_handler(int
 			if (!(regval & PIN_IRQ_PENDING) ||
 			    !(regval & BIT(INTERRUPT_MASK_OFF)))
 				continue;
-			generic_handle_domain_irq(gc->irq.domain, irqnr + i);
+			generic_handle_domain_irq_safe(gc->irq.domain, irqnr + i);
 
 			/* Clear interrupt.
 			 * We must read the pin register again, in case the
Index: linux-5.18.0-rt11/drivers/platform/x86/intel/int0002_vgpio.c
===================================================================
--- linux-5.18.0-rt11.orig/drivers/platform/x86/intel/int0002_vgpio.c
+++ linux-5.18.0-rt11/drivers/platform/x86/intel/int0002_vgpio.c
@@ -125,8 +125,7 @@ static irqreturn_t int0002_irq(int irq,
 	if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT))
 		return IRQ_NONE;
 
-	generic_handle_irq(irq_find_mapping(chip->irq.domain,
-					    GPE0A_PME_B0_VIRT_GPIO_PIN));
+	generic_handle_domain_irq_safe(chip->irq.domain, GPE0A_PME_B0_VIRT_GPIO_PIN);
 
 	pm_wakeup_hard_event(chip->parent);
 
Index: linux-5.18.0-rt11/drivers/ssb/driver_gpio.c
===================================================================
--- linux-5.18.0-rt11.orig/drivers/ssb/driver_gpio.c
+++ linux-5.18.0-rt11/drivers/ssb/driver_gpio.c
@@ -132,7 +132,8 @@ static irqreturn_t ssb_gpio_irq_chipco_h
 		return IRQ_NONE;
 
 	for_each_set_bit(gpio, &irqs, bus->gpio.ngpio)
-		generic_handle_irq(ssb_gpio_to_irq(&bus->gpio, gpio));
+		generic_handle_domain_irq_safe(bus->irq_domain, gpio);
+
 	ssb_chipco_gpio_polarity(chipco, irqs, val & irqs);
 
 	return IRQ_HANDLED;
@@ -330,7 +331,8 @@ static irqreturn_t ssb_gpio_irq_extif_ha
 		return IRQ_NONE;
 
 	for_each_set_bit(gpio, &irqs, bus->gpio.ngpio)
-		generic_handle_irq(ssb_gpio_to_irq(&bus->gpio, gpio));
+		generic_handle_domain_irq_safe(bus->irq_domain, gpio);
+
 	ssb_extif_gpio_polarity(extif, irqs, val & irqs);
 
 	return IRQ_HANDLED;
Index: linux-5.18.0-rt11/include/linux/irqdesc.h
===================================================================
--- linux-5.18.0-rt11.orig/include/linux/irqdesc.h
+++ linux-5.18.0-rt11/include/linux/irqdesc.h
@@ -169,6 +169,7 @@ int generic_handle_irq_safe(unsigned int
  * conversion failed.
  */
 int generic_handle_domain_irq(struct irq_domain *domain, unsigned int hwirq);
+int generic_handle_domain_irq_safe(struct irq_domain *domain, unsigned int hwirq);
 int generic_handle_domain_nmi(struct irq_domain *domain, unsigned int hwirq);
 #endif
 
Index: linux-5.18.0-rt11/kernel/irq/irqdesc.c
===================================================================
--- linux-5.18.0-rt11.orig/kernel/irq/irqdesc.c
+++ linux-5.18.0-rt11/kernel/irq/irqdesc.c
@@ -705,6 +705,30 @@ int generic_handle_domain_irq(struct irq
 }
 EXPORT_SYMBOL_GPL(generic_handle_domain_irq);
 
+ /**
+ * generic_handle_irq_safe - Invoke the handler for a HW irq belonging
+ *			     to a domain from any context.
+ * @domain:	The domain where to perform the lookup
+ * @hwirq:	The HW irq number to convert to a logical one
+ *
+ * Returns:	0 on success, a negative value on error.
+ *
+ * This function can be called from any context (IRQ or process context). It
+ * will report an error if not invoked from IRQ context and the irq has been
+ * marked to enforce IRQ-context only.
+ */
+int generic_handle_domain_irq_safe(struct irq_domain *domain, unsigned int hwirq)
+{
+	unsigned long flags;
+	int ret;
+
+	local_irq_save(flags);
+	ret = handle_irq_desc(irq_resolve_mapping(domain, hwirq));
+	local_irq_restore(flags);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(generic_handle_domain_irq_safe);
+
 /**
  * generic_handle_domain_nmi - Invoke the handler for a HW nmi belonging
  *                             to a domain.