Skip to contentSkip to conversation

gpio: regmap: IRQ domain cleanups

PATCH Superseded Michael Walle 5 participants 26 messages Nov 26 – Nov 30

Subsystems (12)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Michael Walle

author maintainer
#0 lore raw

cover gpio: regmap: IRQ domain cleanups

The gpio-regmap provides two different variants to attach an IRQ domain to the gpio-regmap instance, either via .irq_domain or via .regmap_irq_chip. The former even has a small window where a consumer requesting an interrupt leads to a NULL pointer dereference. This also came up during sashiko reviews [1].

It turns out, that it is impossible to create an external IRQ domain without a race while the gpio-regmap instance isn't created yet. That is because it will already use the gpio-regmap provided callbacks. Thus, with this patch series, gpio-regmap will now provide a callback for the individual drivers used to create an IRQ domain when all necessary fields are filled.

To move even further and cover the most common use case of having a regmap-irq domain, gpio-regmap now provides a helper that can be used in the .create_irq_domain() callback.

We need Long Zhao's patch [2] not only for ACPI but also for the regmap-irq handling. If the IRQ domain is removed before the gpio_chip, gpiochip_remove() would use the already freed domain to free the remaining interrupts.

All the users of regmap-irq are switched over to this new helper. Finally, the last patch, removes the old variants.

There is one change in behavior: because all regmap-irq domains use the gpio_regmap_irq_{reqres,relres}() helpers now, interrupt lines are now locked and cannot be switched to output. But that shouldn't be an issue anyway.

This was mostly only compile-time tested. So please test. I'll do a proper test on the sl28 board tomorrow, but since Long already asked, I wanted to have this out for comments and testing.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]

Long Zhao (1):
gpio: regmap: remove gpiochip before tearing down irqchip

Michael Walle (12):
gpio: regmap: add a callback to create the IRQ domain
gpio: rtd1625: create the IRQ domain from the gpio-regmap callback
gpio: axiado-sgpio: create the IRQ domain from the gpio-regmap
callback
gpio: regmap: add a helper to add a regmap-irq chip
gpio: max7360: create the regmap-irq chip from the gpio-regmap
callback
gpio: sl28cpld: create the regmap-irq chip from the gpio-regmap
callback
gpio: idio-16: create the regmap-irq chip from the gpio-regmap
callback
gpio: pcie-idio-24: create the regmap-irq chip from the gpio-regmap
callback
gpio: ws16c48: create the regmap-irq chip from the gpio-regmap
callback
gpio: 104-idi-48: create the regmap-irq chip from the gpio-regmap
callback
gpio: i8255: create the regmap-irq chip from the gpio-regmap callback
gpio: regmap: remove irq_domain and the regmap-irq configuration

 drivers/gpio/gpio-104-dio-48e.c  | 31 +++++++++----
 drivers/gpio/gpio-104-idi-48.c   | 38 +++++++++++----
 drivers/gpio/gpio-axiado-sgpio.c | 64 +++++++++++++++-----------
 drivers/gpio/gpio-i8255.c        |  3 +-
 drivers/gpio/gpio-i8255.h        |  8 +++-
 drivers/gpio/gpio-idio-16.c      | 28 +++++++----
 drivers/gpio/gpio-max7360.c      | 62 ++++++++++++++++---------
 drivers/gpio/gpio-pcie-idio-24.c | 36 ++++++++++-----
 drivers/gpio/gpio-regmap.c       | 79 ++++++++++++++++++++++++--------
 drivers/gpio/gpio-rtd1625.c      | 74 +++++++++++++++++-------------
 drivers/gpio/gpio-sl28cpld.c     | 34 +++++++++-----
 drivers/gpio/gpio-ws16c48.c      | 36 ++++++++++-----
 include/linux/gpio/regmap.h      | 34 ++++++++------
 13 files changed, 351 insertions(+), 176 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/13 gpio: regmap: remove gpiochip before tearing down irqchip

From: Long Zhao <[email protected]>

gpiochip_remove() calls acpi_gpiochip_free_interrupts() and gpiod_free_irqs(). Both still need the irq domain. regmap_del_irq_chip() removes that domain, so remove the gpiochip first.

Fixes 553b75d4bfe9 ("gpio: regmap: Allow to allocate regmap-irq device")
---
 drivers/gpio/gpio-regmap.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/gpio/gpio-regmap.c +8 -1
index ac6ad56f4479..10cf946dbfc4 100644
@@ -593,12 +593,19 @@ EXPORT_SYMBOL_GPL(gpio_regmap_register);
  */
 void gpio_regmap_unregister(struct gpio_regmap *gpio)
 {
+	/*
+	 * gpiochip_remove() runs acpi_gpiochip_free_interrupts() and
+	 * gpiod_free_irqs(). Both still need the irq domain.
+	 * regmap_del_irq_chip() removes that domain, so remove the
+	 * gpiochip first.
+	 */
+	gpiochip_remove(&gpio->gpio_chip);
+
 #ifdef CONFIG_REGMAP_IRQ
 	if (gpio->irq_chip_data)
 		regmap_del_irq_chip(gpio->regmap_irq_line, gpio->irq_chip_data);
 #endif
 
-	gpiochip_remove(&gpio->gpio_chip);
 	bitmap_free(gpio->fixed_direction_output);
 	bitmap_free(gpio->fixed_direction_mask);
 	kfree(gpio);
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/13 gpio: regmap: add a callback to create the IRQ domain

In reply to Michael Walle

At the moment, the an IRQ domain is attached via config->irg_domain. This means, that it has to be created before the gpio_regmap_register() call. The callbacks of the IRQ domain might use the gpio_regmap_*_irq() functions. Consumers can already use the domain as soon as it is created and this creates a small window where the callbacks dereference a NULL pointer.

To fix this, create a new create_irq_domain() callback, that is called after the gpio_chip is added.

---
 drivers/gpio/gpio-regmap.c  | 21 +++++++++++++++++++--
 include/linux/gpio/regmap.h | 10 +++++++++-
 2 files changed, 28 insertions(+), 3 deletions(-)
drivers/gpio/gpio-regmap.c +19 -2
index 10cf946dbfc4..e8c13434ea5b 100644
@@ -454,6 +454,14 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 	if (config->reg_dir_out_base && config->reg_dir_in_base)
 		return ERR_PTR(-EINVAL);
 
+	/* there is only one way to provide an IRQ domain */
+	if (config->create_irq_domain && config->irq_domain)
+		return ERR_PTR(-EINVAL);
+#ifdef CONFIG_REGMAP_IRQ
+	if (config->create_irq_domain && config->regmap_irq_chip)
+		return ERR_PTR(-EINVAL);
+#endif
+
 	gpio = kzalloc_obj(*gpio);
 	if (!gpio)
 		return ERR_PTR(-ENOMEM);
@@ -545,6 +553,16 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 	if (ret < 0)
 		goto err_free_bitmap_output;
 
+	irq_domain = config->irq_domain;
+
+	if (config->create_irq_domain) {
+		irq_domain = config->create_irq_domain(gpio);
+		if (IS_ERR(irq_domain)) {
+			ret = PTR_ERR(irq_domain);
+			goto err_remove_gpiochip;
+		}
+	}
+
 #ifdef CONFIG_REGMAP_IRQ
 	if (config->regmap_irq_chip) {
 		gpio->regmap_irq_line = config->regmap_irq_line;
@@ -558,9 +576,8 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 			goto err_remove_gpiochip;
 
 		irq_domain = regmap_irq_get_domain(gpio->irq_chip_data);
-	} else
+	}
 #endif
-	irq_domain = config->irq_domain;
 
 	if (irq_domain) {
 		ret = gpiochip_irqchip_add_domain(chip, irq_domain);
include/linux/gpio/regmap.h +9 -1
index 6fe0877717cb..eba0620c63d6 100644
@@ -62,7 +62,14 @@ enum gpio_regmap_operation {
  *			same type, dat, set, etc) are not consecutive.
  * @ngpio_per_reg:	(Optional) Number of GPIOs per register
  * @irq_domain:		(Optional) IRQ domain if the controller is
- *			interrupt-capable
+ *			interrupt-capable. Cannot be used together with the
+ *			gpio_regmap_*_irq() callbacks. Use @create_irq_domain
+ *			instead.
+ * @create_irq_domain:	(Optional) Callback to create the IRQ domain in the
+ *			controller driver. Called after the gpio_chip is added
+ *			so the gpio_regmap_*_irq() have a valid gpio_regmap
+ *			pointer.
+ *			Mutually exclusive with @irq_domain and @regmap_irq_chip.
  * @fixed_direction_mask:
  *			(Optional) Bitmap representing the GPIO lines that
  *			make use of the @fixed_direction_output list to
@@ -133,6 +140,7 @@ struct gpio_regmap_config {
 	int reg_stride;
 	int ngpio_per_reg;
 	struct irq_domain *irq_domain;
+	struct irq_domain *(*create_irq_domain)(struct gpio_regmap *gpio);
 	unsigned long *fixed_direction_mask;
 	unsigned long *fixed_direction_output;
 
Signature · 1 line
2.47.3
#3 lore raw

Re: [RFT PATCH 02/13] gpio: regmap: add a callback to create the IRQ domain

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] gpio: regmap: zero ngpio_per_reg…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] gpio: regmap: zero ngpio_per_reg fallback in gpio_regmap_register() [1]
--

commit fd10248876b372329d85c754bb2bf0c35ed1c5a7
Author: Michael Walle <[email protected]>

gpio: regmap: add a callback to create the IRQ domain

This commit adds a create_irq_domain() callback to struct gpio_regmap_config so controller drivers can create their IRQ domain after the gpio_chip has been registered.

Quoted 4 lines
drivers/gpio/gpio-regmap.c +0 -0
index 10cf946dbfc47..e8c13434ea5ba 100644

[ ... ]

Quoted 4 lines
drivers/gpio/gpio-regmap.c +0 -0
@@ -545,6 +553,16 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 	if (ret < 0)
 		goto err_free_bitmap_output;
 

drivers/gpio/gpio-regmap.c struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config · line 565

Quoted 10 lines
drivers/gpio/gpio-regmap.c +10 -0
+	irq_domain = config->irq_domain;
+
+	if (config->create_irq_domain) {
+		irq_domain = config->create_irq_domain(gpio);
+		if (IS_ERR(irq_domain)) {
+			ret = PTR_ERR(irq_domain);
+			goto err_remove_gpiochip;
+		}
+	}
+

[Severity: High]
This problem wasn't introduced by this patch, but can gpio->ngpio_per_reg remain 0 in gpio_regmap_register() [1]?

Just above this hunk, when config->ngpio is 0 (such as in ds4520_gpio_probe()), gpiochip_get_ngpios() populates chip->ngpio from the firmware ngpios property while config->ngpio stays 0:

drivers/gpio/gpio-regmap.c:gpio_regmap_register() {
    ...
	chip->ngpio = config->ngpio;
	if (!chip->ngpio) {
		ret = gpiochip_get_ngpios(chip, chip->parent);
		if (ret)
			goto err_free_gpio;
	}
    ...
	gpio->ngpio_per_reg = config->ngpio_per_reg;
	if (!gpio->ngpio_per_reg)
		gpio->ngpio_per_reg = config->ngpio;
    ...
}

If config->ngpio_per_reg is also unset, the fallback copies config->ngpio instead of chip->ngpio, leaving gpio->ngpio_per_reg as 0 and causing a divide-by-zero in gpio_regmap_simple_xlate() when gpiochip_add_data() queries the initial line directions.

[1] https://sashiko.dev/#/bug/linux-85577bd9-c441-47ec-b415-cd40acf50e01

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

PATCH 3/13 gpio: rtd1625: create the IRQ domain from the gpio-regmap callback

In reply to Michael Walle

Use the new .create_irq_domain() to actually create the domain. This way, there is no window where a NULL pointer could be dereferenced.

Switch the irq host_data to the gpio_regmap pointer because it will already be needed in the irq domain callbacks. Otherwise the .create_irq_domain() callback would have to set the data->gpio_reg pointer which looks odd.

---
 drivers/gpio/gpio-rtd1625.c | 74 ++++++++++++++++++++-----------------
 1 file changed, 41 insertions(+), 33 deletions(-)
drivers/gpio/gpio-rtd1625.c +41 -33
index cf4f61603dfb..0713692804e2 100644
@@ -79,7 +79,6 @@ struct rtd1625_gpio_info {
 };
 
 struct rtd1625_gpio {
-	struct gpio_regmap *gpio_reg;
 	const struct rtd1625_gpio_info *info;
 	struct regmap *regmap;
 	unsigned int irqs[RTD1625_MAX_IRQS];
@@ -290,7 +289,7 @@ static void rtd1625_gpio_irq_handle(struct irq_desc *desc)
 
 static void rtd1625_gpio_ack_irq(struct irq_data *d)
 {
-	struct rtd1625_gpio *data = irq_data_get_irq_chip_data(d);
+	struct rtd1625_gpio *data = gpio_regmap_get_drvdata(irq_data_get_irq_chip_data(d));
 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
 	u32 irq_type = irqd_get_trigger_type(d);
 	u32 bit_mask = BIT(hwirq % 32);
@@ -352,11 +351,12 @@ static void rtd1625_gpio_disable_level_irq(struct rtd1625_gpio *data, irq_hw_num
 
 static void rtd1625_gpio_enable_irq(struct irq_data *d)
 {
-	struct rtd1625_gpio *data = irq_data_get_irq_chip_data(d);
+	struct gpio_regmap *gpio_reg = irq_data_get_irq_chip_data(d);
+	struct rtd1625_gpio *data = gpio_regmap_get_drvdata(gpio_reg);
 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
 	u32 irq_type = irqd_get_trigger_type(d);
 
-	gpio_regmap_enable_irq(data->gpio_reg, hwirq);
+	gpio_regmap_enable_irq(gpio_reg, hwirq);
 
 	if (irq_type & IRQ_TYPE_EDGE_BOTH)
 		rtd1625_gpio_enable_edge_irq(data, hwirq);
@@ -366,7 +366,8 @@ static void rtd1625_gpio_enable_irq(struct irq_data *d)
 
 static void rtd1625_gpio_disable_irq(struct irq_data *d)
 {
-	struct rtd1625_gpio *data = irq_data_get_irq_chip_data(d);
+	struct gpio_regmap *gpio_reg = irq_data_get_irq_chip_data(d);
+	struct rtd1625_gpio *data = gpio_regmap_get_drvdata(gpio_reg);
 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
 	u32 irq_type = irqd_get_trigger_type(d);
 
@@ -375,7 +376,7 @@ static void rtd1625_gpio_disable_irq(struct irq_data *d)
 	else if (irq_type & IRQ_TYPE_LEVEL_MASK)
 		rtd1625_gpio_disable_level_irq(data, hwirq);
 
-	gpio_regmap_disable_irq(data->gpio_reg, hwirq);
+	gpio_regmap_disable_irq(gpio_reg, hwirq);
 }
 
 static int rtd1625_gpio_irq_set_level_type(struct irq_data *d, bool level)
@@ -385,7 +386,7 @@ static int rtd1625_gpio_irq_set_level_type(struct irq_data *d, bool level)
 	struct rtd1625_gpio *data;
 	int ret;
 
-	data = irq_data_get_irq_chip_data(d);
+	data = gpio_regmap_get_drvdata(irq_data_get_irq_chip_data(d));
 	if (!(data->info->irq_type_support & IRQ_TYPE_LEVEL_MASK))
 		return -EINVAL;
 
@@ -406,7 +407,7 @@ static int rtd1625_gpio_irq_set_level_type(struct irq_data *d, bool level)
 
 static int rtd1625_gpio_irq_set_edge_type(struct irq_data *d, bool polarity)
 {
-	struct rtd1625_gpio *data = irq_data_get_irq_chip_data(d);
+	struct rtd1625_gpio *data = gpio_regmap_get_drvdata(irq_data_get_irq_chip_data(d));
 	u32 val = RTD1625_GPIO_WREN(RTD1625_GPIO_EDGE_INT_DP);
 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
 	int ret;
@@ -454,16 +455,16 @@ static int rtd1625_gpio_irq_set_type(struct irq_data *d, unsigned int type)
 
 static int rtd1625_gpio_irq_request_resources(struct irq_data *d)
 {
-	struct rtd1625_gpio *data = irq_data_get_irq_chip_data(d);
+	struct gpio_regmap *gpio_reg = irq_data_get_irq_chip_data(d);
 
-	return gpio_regmap_reqres_irq(data->gpio_reg, d->hwirq);
+	return gpio_regmap_reqres_irq(gpio_reg, d->hwirq);
 }
 
 static void rtd1625_gpio_irq_release_resources(struct irq_data *d)
 {
-	struct rtd1625_gpio *data = irq_data_get_irq_chip_data(d);
+	struct gpio_regmap *gpio_reg = irq_data_get_irq_chip_data(d);
 
-	gpio_regmap_relres_irq(data->gpio_reg, d->hwirq);
+	gpio_regmap_relres_irq(gpio_reg, d->hwirq);
 }
 
 static struct irq_chip rtd1625_iso_gpio_irq_chip = {
@@ -505,9 +506,9 @@ static int rtd1625_gpio_setup_irq(struct platform_device *pdev, struct rtd1625_g
 static int rtd1625_gpio_irq_map(struct irq_domain *domain, unsigned int irq,
 				irq_hw_number_t hwirq)
 {
-	struct rtd1625_gpio *data = domain->host_data;
+	struct gpio_regmap *gpio_reg = domain->host_data;
 
-	irq_set_chip_data(irq, data);
+	irq_set_chip_data(irq, gpio_reg);
 
 	irq_set_chip_and_handler(irq, &rtd1625_iso_gpio_irq_chip, handle_bad_irq);
 
@@ -524,6 +525,31 @@ static const struct irq_domain_ops rtd1625_gpio_irq_domain_ops = {
 	.xlate = irq_domain_xlate_twocell,
 };
 
+static struct irq_domain *rtd1625_gpio_create_irq_domain(struct gpio_regmap *gpio_reg)
+{
+	struct rtd1625_gpio *data = gpio_regmap_get_drvdata(gpio_reg);
+	struct device *dev = regmap_get_device(data->regmap);
+	struct platform_device *pdev = to_platform_device(dev);
+	struct irq_domain_info d_info = {};
+	int ret;
+
+	d_info.fwnode = dev_fwnode(&pdev->dev);
+	d_info.size = data->info->num_gpios;
+	d_info.hwirq_max = data->info->num_gpios;
+	d_info.ops = &rtd1625_gpio_irq_domain_ops;
+	d_info.host_data = gpio_reg;
+
+	data->domain = devm_irq_domain_instantiate(dev, &d_info);
+	if (IS_ERR(data->domain))
+		return data->domain;
+
+	ret = rtd1625_gpio_setup_irq(pdev, data);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return data->domain;
+}
+
 static const struct regmap_config rtd1625_gpio_regmap_config = {
 	.reg_bits = 32,
 	.reg_stride = 4,
@@ -536,12 +562,10 @@ static const struct regmap_config rtd1625_gpio_regmap_config = {
 static int rtd1625_gpio_probe(struct platform_device *pdev)
 {
 	struct gpio_regmap_config config = {};
-	struct irq_domain_info d_info = {};
 	struct device *dev = &pdev->dev;
 	struct gpio_regmap *gpio_reg;
 	struct rtd1625_gpio *data;
 	void __iomem *irq_base;
-	int ret;
 
 	data = devm_kzalloc_obj(dev, *data);
 	if (!data)
@@ -578,29 +602,13 @@ static int rtd1625_gpio_probe(struct platform_device *pdev)
 	config.set_config = rtd1625_gpio_set_config;
 	config.value_xlate = rtd1625_value_xlate;
 
-	d_info.fwnode = dev_fwnode(&pdev->dev);
-	d_info.size = data->info->num_gpios;
-	d_info.hwirq_max = data->info->num_gpios;
-	d_info.ops = &rtd1625_gpio_irq_domain_ops;
-	d_info.host_data = data;
-
-	data->domain = devm_irq_domain_instantiate(dev, &d_info);
-	if (IS_ERR(data->domain))
-		return PTR_ERR(data->domain);
-
-	ret = rtd1625_gpio_setup_irq(pdev, data);
-	if (ret)
-		return ret;
-
-	config.irq_domain = data->domain;
 	config.drvdata = data;
+	config.create_irq_domain = rtd1625_gpio_create_irq_domain;
 
 	gpio_reg = devm_gpio_regmap_register(dev, &config);
 	if (IS_ERR(gpio_reg))
 		return PTR_ERR(gpio_reg);
 
-	data->gpio_reg = gpio_reg;
-
 	return 0;
 }
 
Signature · 1 line
2.47.3
#6 lore raw

Re: [RFT PATCH 03/13] gpio: rtd1625: create the IRQ domain from the gpio-regmap callback

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] gpio: rtd1625: use-after-free of gpio_reg due to devres lifetime mismatch - [Low] gpio: rtd1625…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] gpio: rtd1625: use-after-free of gpio_reg due to devres lifetime mismatch
- [Low] gpio: rtd1625: missing Fixes trailer for probe NULL dereference fix
--

commit 836b597a055624fd11d643d1081b9d412a5134af
Author: Michael Walle <[email protected]>

Quoted 11 lines

gpio: rtd1625: create the IRQ domain from the gpio-regmap callback

Use the new .create_irq_domain() to actually create the domain. This way, there is no window where a NULL pointer could be dereferenced.

Switch the irq host_data to the gpio_regmap pointer because it will already be needed in the irq domain callbacks. Otherwise the .create_irq_domain() callback would have to set the data->gpio_reg pointer which looks odd.

[Severity: Low]
Since this fixes a probe NULL pointer dereference window, should it carry:

Fixes 6435d21cbd42 ("gpio: realtek: Add driver for Realtek DHC RTD1625 SoC")
Quoted 4 lines
drivers/gpio/gpio-rtd1625.c +0 -0
index cf4f61603dfbf..0713692804e21 100644

[ ... ]

Quoted 11 lines
drivers/gpio/gpio-rtd1625.c +2 -2
@@ -505,9 +506,9 @@ static int rtd1625_gpio_setup_irq(struct platform_device *pdev, struct rtd1625_g
 static int rtd1625_gpio_irq_map(struct irq_domain *domain, unsigned int irq,
 				irq_hw_number_t hwirq)
 {
-	struct rtd1625_gpio *data = domain->host_data;
+	struct gpio_regmap *gpio_reg = domain->host_data;
 
-	irq_set_chip_data(irq, data);
+	irq_set_chip_data(irq, gpio_reg);
 
 	irq_set_chip_and_handler(irq, &rtd1625_iso_gpio_irq_chip, handle_bad_irq);

[ ... ]

Quoted 18 lines
drivers/gpio/gpio-rtd1625.c +14 -0
@@ -524,6 +525,31 @@ static const struct irq_domain_ops rtd1625_gpio_irq_domain_ops = {
 	.xlate = irq_domain_xlate_twocell,
 };
 
+static struct irq_domain *rtd1625_gpio_create_irq_domain(struct gpio_regmap *gpio_reg)
+{
+	struct rtd1625_gpio *data = gpio_regmap_get_drvdata(gpio_reg);
+	struct device *dev = regmap_get_device(data->regmap);
+	struct platform_device *pdev = to_platform_device(dev);
+	struct irq_domain_info d_info = {};
+	int ret;
+
+	d_info.fwnode = dev_fwnode(&pdev->dev);
+	d_info.size = data->info->num_gpios;
+	d_info.hwirq_max = data->info->num_gpios;
+	d_info.ops = &rtd1625_gpio_irq_domain_ops;
+	d_info.host_data = gpio_reg;
+

drivers/gpio/gpio-rtd1625.c static const struct irq_domain_ops rtd1625_gpio_irq_domain_ops = { · line 551

Quoted 10 lines
drivers/gpio/gpio-rtd1625.c +10 -0
+	data->domain = devm_irq_domain_instantiate(dev, &d_info);
+	if (IS_ERR(data->domain))
+		return data->domain;
+
+	ret = rtd1625_gpio_setup_irq(pdev, data);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return data->domain;
+}

[Severity: Medium]
Can storing gpio_reg in domain->host_data and irq_data->chip_data cause a use-after-free of gpio_reg on probe failure or driver unbind?

rtd1625_gpio_create_irq_domain() runs inside gpio_regmap_register(), so devm_irq_domain_instantiate() queues devm_irq_domain_remove() on dev's devres list before devm_gpio_regmap_register() queues devm_gpio_regmap_unregister():

drivers/gpio/gpio-regmap.c:devm_gpio_regmap_register() {
	gpio = gpio_regmap_register(config);
	if (IS_ERR(gpio))
		return gpio;

	ret = devm_add_action_or_reset(dev, devm_gpio_regmap_unregister, gpio);

...
}

Because gpio_reg is allocated with kzalloc_obj() rather than devm and freed in gpio_regmap_unregister(), devres teardown on unbind runs devm_gpio_regmap_unregister() first and calls kfree(gpio) while data->domain is still published with host_data and mapped chip_data pointing to freed gpio_reg until devm_irq_domain_remove() runs.

The same mismatch occurs if rtd1625_gpio_setup_irq() fails after devm_irq_domain_instantiate() succeeds: gpio_regmap_register() immediately calls kfree(gpio) in its error path while data->domain remains live until rtd1625_gpio_probe() returns and devres cleans up.

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

PATCH 4/13 gpio: axiado-sgpio: create the IRQ domain from the gpio-regmap callback

In reply to Michael Walle

Use the new .create_irq_domain() to actually create the domain. This way, there is no window where a NULL pointer could be dereferenced in the irq callbacks.

Switch the irq host_data to the gpio_regmap pointer because it will already be needed in the irq domain callbacks. Otherwise, the .create_irq_domain() callback would have to set the sgpio->gpio pointer which looks odd.

---
 drivers/gpio/gpio-axiado-sgpio.c | 64 ++++++++++++++++++--------------
 1 file changed, 37 insertions(+), 27 deletions(-)
drivers/gpio/gpio-axiado-sgpio.c +37 -27
index 3af222b718e6..2785d0cfb403 100644
@@ -155,7 +155,6 @@ struct axiado_sgpio {
 	u32 nsignals;
 	u32 din_bank_shift;
 	unsigned long *dir_out;
-	struct gpio_regmap *gpio;
 	struct irq_domain *domain;
 	struct regmap *regmap;
 	const struct axiado_sgpio_reg_offsets *regs;
@@ -376,7 +375,7 @@ static void axiado_sgpio_disable(void *data)
 
 static int axiado_sgpio_set_irq_type(struct irq_data *d, unsigned int type)
 {
-	struct axiado_sgpio *sgpio = irq_data_get_irq_chip_data(d);
+	struct axiado_sgpio *sgpio = gpio_regmap_get_drvdata(irq_data_get_irq_chip_data(d));
 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
 	unsigned int position;
 
@@ -407,34 +406,36 @@ static int axiado_sgpio_set_irq_type(struct irq_data *d, unsigned int type)
 
 static void axiado_sgpio_mask_irq(struct irq_data *d)
 {
-	struct axiado_sgpio *sgpio = irq_data_get_irq_chip_data(d);
+	struct gpio_regmap *gpio = irq_data_get_irq_chip_data(d);
+	struct axiado_sgpio *sgpio = gpio_regmap_get_drvdata(gpio);
 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
 
 	clear_bit(hwirq / 2, sgpio->irq_unmasked);
-	gpio_regmap_disable_irq(sgpio->gpio, hwirq);
+	gpio_regmap_disable_irq(gpio, hwirq);
 }
 
 static void axiado_sgpio_unmask_irq(struct irq_data *d)
 {
-	struct axiado_sgpio *sgpio = irq_data_get_irq_chip_data(d);
+	struct gpio_regmap *gpio = irq_data_get_irq_chip_data(d);
+	struct axiado_sgpio *sgpio = gpio_regmap_get_drvdata(gpio);
 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
 
-	gpio_regmap_enable_irq(sgpio->gpio, hwirq);
+	gpio_regmap_enable_irq(gpio, hwirq);
 	set_bit(hwirq / 2, sgpio->irq_unmasked);
 }
 
 static int axiado_sgpio_irq_request_resources(struct irq_data *d)
 {
-	struct axiado_sgpio *sgpio = irq_data_get_irq_chip_data(d);
+	struct gpio_regmap *gpio = irq_data_get_irq_chip_data(d);
 
-	return gpio_regmap_reqres_irq(sgpio->gpio, d->hwirq);
+	return gpio_regmap_reqres_irq(gpio, d->hwirq);
 }
 
 static void axiado_sgpio_irq_release_resources(struct irq_data *d)
 {
-	struct axiado_sgpio *sgpio = irq_data_get_irq_chip_data(d);
+	struct gpio_regmap *gpio = irq_data_get_irq_chip_data(d);
 
-	gpio_regmap_relres_irq(sgpio->gpio, d->hwirq);
+	gpio_regmap_relres_irq(gpio, d->hwirq);
 }
 
 static const struct irq_chip axiado_sgpio_irqchip = {
@@ -450,13 +451,13 @@ static const struct irq_chip axiado_sgpio_irqchip = {
 static int axiado_sgpio_irq_map(struct irq_domain *domain, unsigned int virq,
 				irq_hw_number_t hwirq)
 {
-	struct axiado_sgpio *sgpio = domain->host_data;
+	struct gpio_regmap *gpio = domain->host_data;
 
 	/* Only even offsets represent input GPIOs. */
 	if (hwirq % 2)
 		return -EINVAL;
 
-	irq_set_chip_data(virq, sgpio);
+	irq_set_chip_data(virq, gpio);
 	irq_set_lockdep_class(virq, &axiado_sgpio_irq_lock_class,
 			      &axiado_sgpio_irq_request_class);
 	irq_set_chip_and_handler(virq, &axiado_sgpio_irqchip, handle_simple_irq);
@@ -471,6 +472,25 @@ static const struct irq_domain_ops axiado_sgpio_domain_ops = {
 	.xlate = irq_domain_xlate_twocell,
 };
 
+static struct irq_domain *axiado_sgpio_create_irq_domain(struct gpio_regmap *gpio)
+{
+	struct axiado_sgpio *sgpio = gpio_regmap_get_drvdata(gpio);
+	struct device *dev = regmap_get_device(sgpio->regmap);
+	struct platform_device *pdev = to_platform_device(dev);
+	unsigned int ngpio = sgpio->nsignals * 2;
+	struct irq_domain_info d_info = { };
+
+	d_info.fwnode = dev_fwnode(&pdev->dev);
+	d_info.size = ngpio;
+	d_info.hwirq_max = ngpio;
+	d_info.ops = &axiado_sgpio_domain_ops;
+	d_info.host_data = gpio;
+
+	sgpio->domain = devm_irq_domain_instantiate(&pdev->dev, &d_info);
+
+	return sgpio->domain;
+}
+
 static int axiado_sgpio_init_input_cache(struct axiado_sgpio *sgpio)
 {
 	unsigned int num_banks = DIV_ROUND_UP(sgpio->nsignals,
@@ -548,8 +568,8 @@ static const struct regmap_config axiado_sgpio_regmap_config = {
 static int axiado_sgpio_probe(struct platform_device *pdev)
 {
 	struct gpio_regmap_config config = { };
-	struct irq_domain_info d_info = { };
 	struct axiado_sgpio *sgpio;
+	struct gpio_regmap *gpio;
 	unsigned int ngpio, i;
 	unsigned long apb_freq;
 	struct clk *apb_clk;
@@ -658,16 +678,6 @@ static int axiado_sgpio_probe(struct platform_device *pdev)
 	for (i = 1; i < ngpio; i += 2)
 		__set_bit(i, sgpio->dir_out);
 
-	d_info.fwnode = dev_fwnode(&pdev->dev);
-	d_info.size = ngpio;
-	d_info.hwirq_max = ngpio;
-	d_info.ops = &axiado_sgpio_domain_ops;
-	d_info.host_data = sgpio;
-
-	sgpio->domain = devm_irq_domain_instantiate(&pdev->dev, &d_info);
-	if (IS_ERR(sgpio->domain))
-		return PTR_ERR(sgpio->domain);
-
 	config.parent = &pdev->dev;
 	config.regmap = sgpio->regmap;
 	config.ngpio = ngpio;
@@ -675,12 +685,12 @@ static int axiado_sgpio_probe(struct platform_device *pdev)
 	config.reg_set_base = GPIO_REGMAP_ADDR(sgpio->regs->dout_ss);
 	config.reg_mask_xlate = axiado_sgpio_reg_mask_xlate;
 	config.fixed_direction_output = sgpio->dir_out;
-	config.irq_domain = sgpio->domain;
 	config.drvdata = sgpio;
+	config.create_irq_domain = axiado_sgpio_create_irq_domain;
 
-	sgpio->gpio = devm_gpio_regmap_register(&pdev->dev, &config);
-	if (IS_ERR(sgpio->gpio))
-		return dev_err_probe(&pdev->dev, PTR_ERR(sgpio->gpio),
+	gpio = devm_gpio_regmap_register(&pdev->dev, &config);
+	if (IS_ERR(gpio))
+		return dev_err_probe(&pdev->dev, PTR_ERR(gpio),
 				     "Could not register gpiochip\n");
 
 	irq = platform_get_irq(pdev, 0);
Signature · 1 line
2.47.3
maintainer Tested-by on v1 4/13
#8 lore raw

Re: [RFT PATCH 04/13] gpio: axiado-sgpio: create the IRQ domain from the gpio-regmap callback

Quoted 12 lines · Michael Walle

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

Use the new .create_irq_domain() to actually create the domain. This way, there is no window where a NULL pointer could be dereferenced in the irq callbacks.

Switch the irq host_data to the gpio_regmap pointer because it will already be needed in the irq domain callbacks. Otherwise, the .create_irq_domain() callback would have to set the sgpio->gpio pointer which looks odd.

Tested on the Axiado AX3005 SoC using an SGPIO hardware loopback setup.

GPIO read/write operations and rising/falling edge interrupts work as expected. No regressions observed.

Thanks,
Petar

author maintainer
#9 lore raw

Re: [RFT PATCH 04/13] gpio: axiado-sgpio: create the IRQ domain from the gpio-regmap callback

Quoted 15 lines · Petar Stepanovic
Quoted 12 lines · Michael Walle

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

Use the new .create_irq_domain() to actually create the domain. This way, there is no window where a NULL pointer could be dereferenced in the irq callbacks.

Switch the irq host_data to the gpio_regmap pointer because it will already be needed in the irq domain callbacks. Otherwise, the .create_irq_domain() callback would have to set the sgpio->gpio pointer which looks odd.

Quoted 5 lines

Tested on the Axiado AX3005 SoC using an SGPIO hardware loopback setup.

GPIO read/write operations and rising/falling edge interrupts work as expected. No regressions observed.

Hi,

thanks for the test! In the meantime there was a new version:

https://lore.kernel.org/r/[email protected]/

Could you give that one a test, too? FWIW, I'll address Andy's comment and replace the open coded domain instantiation with irq_domain_create_linear(). Basically this one on top of the v2:

---snip---

drivers/gpio/gpio-axiado-sgpio.c +6 -10
@@ -476,17 +476,13 @@ static struct irq_domain *axiado_sgpio_create_irq_domain(struct gpio_regmap *gpi
 {
 	struct axiado_sgpio *sgpio = gpio_regmap_get_drvdata(gpio);
 	struct device *dev = regmap_get_device(sgpio->regmap);
-	struct platform_device *pdev = to_platform_device(dev);
-	unsigned int ngpio = sgpio->nsignals * 2;
-	struct irq_domain_info d_info = { };
 
-	d_info.fwnode = dev_fwnode(&pdev->dev);
-	d_info.size = ngpio;
-	d_info.hwirq_max = ngpio;
-	d_info.ops = &axiado_sgpio_domain_ops;
-	d_info.host_data = gpio;
-
-	sgpio->domain = irq_domain_instantiate(&d_info);
+	sgpio->domain = irq_domain_create_linear(dev_fwnode(dev),
+						 sgpio->nsignals * 2,
+						 &axiado_sgpio_domain_ops,
+						 gpio);
+	if (!sgpio->domain)
+		return ERR_PTR(-ENOMEM);
 
 	return sgpio->domain;
 }

---snip---

-michael

author maintainer
#10 lore raw

PATCH 5/13 gpio: regmap: add a helper to add a regmap-irq chip

In reply to Michael Walle

The most common use-case is to use regmap-irq together with gpio-regmap. To make life easier for that and to avoid code duplication, add a helper to create and attach a regmap-irq domain. That helper is intended to be called by the new .create_irq_domain() callback.

---
 drivers/gpio/gpio-regmap.c  | 43 +++++++++++++++++++++++++++++++++++++
 include/linux/gpio/regmap.h | 15 +++++++++++++
 2 files changed, 58 insertions(+)
drivers/gpio/gpio-regmap.c +43 -0
index e8c13434ea5b..99a86efe8deb 100644
@@ -407,6 +407,49 @@ static void __maybe_unused gpio_regmap_irq_relres(void *irq_drv_data, irq_hw_num
 	gpio_regmap_relres_irq(irq_drv_data, hwirq);
 }
 
+#ifdef CONFIG_REGMAP_IRQ
+/**
+ * gpio_regmap_add_irq_chip() - Add a regmap-irq chip for the GPIO lines
+ * @gpio: gpio_regmap device
+ * @irq: the parent interrupt
+ * @irq_flags: the IRQF_ flags to use for @irq
+ * @chip: the regmap-irq chip description
+ *
+ * Add a regmap-irq chip on the regmap of @gpio. Should be called from
+ * create_irq_domain() callback.
+ *
+ * The regmap-irq chip is automatically removed when the gpio_regmap is
+ * unregistered.
+ *
+ * Return: irq_domain or an ERR_PTR() error value.
+ */
+struct irq_domain *gpio_regmap_add_irq_chip(struct gpio_regmap *gpio, int irq,
+					    int irq_flags,
+					    struct regmap_irq_chip *chip)
+{
+	int ret;
+
+	/* shouldn't be called more than once */
+	if (WARN_ON(gpio->irq_chip_data))
+		return ERR_PTR(-EBUSY);
+
+	chip->irq_reqres = gpio_regmap_irq_reqres;
+	chip->irq_relres = gpio_regmap_irq_relres;
+	chip->irq_drv_data = gpio;
+
+	ret = regmap_add_irq_chip_fwnode(dev_fwnode(gpio->parent), gpio->regmap,
+					 irq, irq_flags, 0, chip,
+					 &gpio->irq_chip_data);
+	if (ret)
+		return ERR_PTR(ret);
+
+	gpio->regmap_irq_line = irq;
+
+	return regmap_irq_get_domain(gpio->irq_chip_data);
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_add_irq_chip);
+#endif
+
 void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio)
 {
 	return gpio->driver_data;
include/linux/gpio/regmap.h +15 -0
index eba0620c63d6..7f578b9a82e6 100644
@@ -3,6 +3,7 @@
 #ifndef _LINUX_GPIO_REGMAP_H
 #define _LINUX_GPIO_REGMAP_H
 
+#include <linux/err.h>
 #include <linux/types.h>
 
 struct device;
@@ -11,6 +12,7 @@ struct gpio_regmap;
 struct gpio_chip;
 struct irq_domain;
 struct regmap;
+struct regmap_irq_chip;
 
 #define GPIO_REGMAP_ADDR_ZERO ((unsigned int)(-1))
 #define GPIO_REGMAP_ADDR(addr) ((addr) ? : GPIO_REGMAP_ADDR_ZERO)
@@ -180,4 +182,17 @@ void gpio_regmap_relres_irq(struct gpio_regmap *gpio, unsigned int offset);
 void gpio_regmap_enable_irq(struct gpio_regmap *gpio, irq_hw_number_t hwirq);
 void gpio_regmap_disable_irq(struct gpio_regmap *gpio, irq_hw_number_t hwirq);
 
+#ifdef CONFIG_REGMAP_IRQ
+struct irq_domain *gpio_regmap_add_irq_chip(struct gpio_regmap *gpio, int irq,
+					    int irq_flags,
+					    struct regmap_irq_chip *chip);
+#else
+static inline struct irq_domain *
+gpio_regmap_add_irq_chip(struct gpio_regmap *gpio, int irq, int irq_flags,
+			 struct regmap_irq_chip *chip)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+#endif
+
 #endif /* _LINUX_GPIO_REGMAP_H */
Signature · 1 line
2.47.3
author maintainer
#11 lore raw

PATCH 6/13 gpio: max7360: create the regmap-irq chip from the gpio-regmap callback

In reply to Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

---
 drivers/gpio/gpio-max7360.c | 62 ++++++++++++++++++++++++-------------
 1 file changed, 41 insertions(+), 21 deletions(-)
drivers/gpio/gpio-max7360.c +41 -21
index b77a51092dcb..87f9442181d7 100644
@@ -27,6 +27,11 @@ struct max7360_gpio_plat_data {
 	unsigned int function;
 };
 
+struct max7360_gpio {
+	struct device *dev;
+	struct regmap *regmap;
+};
+
 static struct max7360_gpio_plat_data max7360_gpio_port_plat = { .function = MAX7360_GPIO_PORT };
 static struct max7360_gpio_plat_data max7360_gpio_col_plat = { .function = MAX7360_GPIO_COL };
 
@@ -128,7 +133,8 @@ static int max7360_handle_mask_sync(const int index,
 				    const unsigned int mask_buf,
 				    void *const irq_drv_data)
 {
-	struct regmap *regmap = gpio_regmap_get_drvdata(irq_drv_data);
+	struct max7360_gpio *max7360_gpio = gpio_regmap_get_drvdata(irq_drv_data);
+	struct regmap *regmap = max7360_gpio->regmap;
 	int ret;
 
 	for (unsigned int i = 0; i < MAX7360_MAX_GPIO; i++) {
@@ -141,11 +147,38 @@ static int max7360_handle_mask_sync(const int index,
 	return 0;
 }
 
+static struct irq_domain *max7360_gpio_create_irq_domain(struct gpio_regmap *gpio)
+{
+	struct max7360_gpio *max7360_gpio = gpio_regmap_get_drvdata(gpio);
+	struct device *dev = max7360_gpio->dev;
+	struct regmap_irq_chip *irq_chip;
+	int irq;
+
+	irq = fwnode_irq_get_byname(dev_fwnode(dev->parent), "inti");
+	if (irq < 0)
+		return ERR_PTR(dev_err_probe(dev, irq, "Failed to get IRQ\n"));
+
+	/* Create custom IRQ configuration. */
+	irq_chip = devm_kzalloc_obj(dev, *irq_chip);
+	if (!irq_chip)
+		return ERR_PTR(-ENOMEM);
+
+	irq_chip->name = dev_name(dev);
+	irq_chip->status_base = MAX7360_REG_GPIOIN;
+	irq_chip->status_is_level = true;
+	irq_chip->num_regs = 1;
+	irq_chip->num_irqs = MAX7360_MAX_GPIO;
+	irq_chip->irqs = max7360_regmap_irqs;
+	irq_chip->handle_mask_sync = max7360_handle_mask_sync;
+
+	return gpio_regmap_add_irq_chip(gpio, irq, IRQF_ONESHOT | IRQF_SHARED, irq_chip);
+}
+
 static int max7360_gpio_probe(struct platform_device *pdev)
 {
 	const struct max7360_gpio_plat_data *plat_data;
 	struct gpio_regmap_config gpio_config = { };
-	struct regmap_irq_chip *irq_chip;
+	struct max7360_gpio *max7360_gpio;
 	struct device *dev = &pdev->dev;
 	struct regmap *regmap;
 	unsigned int outconf;
@@ -162,28 +195,15 @@ static int max7360_gpio_probe(struct platform_device *pdev)
 			 * Port GPIOs with interrupt-controller property: add IRQ
 			 * controller.
 			 */
-			gpio_config.regmap_irq_flags = IRQF_ONESHOT | IRQF_SHARED;
-			gpio_config.regmap_irq_line =
-				fwnode_irq_get_byname(dev_fwnode(dev->parent), "inti");
-			if (gpio_config.regmap_irq_line < 0)
-				return dev_err_probe(dev, gpio_config.regmap_irq_line,
-						     "Failed to get IRQ\n");
-
-			/* Create custom IRQ configuration. */
-			irq_chip = devm_kzalloc_obj(dev, *irq_chip);
-			if (!irq_chip)
+			max7360_gpio = devm_kzalloc_obj(dev, *max7360_gpio);
+			if (!max7360_gpio)
 				return -ENOMEM;
 
-			irq_chip->name = dev_name(dev);
-			irq_chip->status_base = MAX7360_REG_GPIOIN;
-			irq_chip->status_is_level = true;
-			irq_chip->num_regs = 1;
-			irq_chip->num_irqs = MAX7360_MAX_GPIO;
-			irq_chip->irqs = max7360_regmap_irqs;
-			irq_chip->handle_mask_sync = max7360_handle_mask_sync;
+			max7360_gpio->dev = dev;
+			max7360_gpio->regmap = regmap;
 
-			gpio_config.regmap_irq_chip = irq_chip;
-			gpio_config.drvdata = regmap;
+			gpio_config.drvdata = max7360_gpio;
+			gpio_config.create_irq_domain = max7360_gpio_create_irq_domain;
 
 			for (unsigned int i = 0; i < MAX7360_MAX_GPIO; i++) {
 				ret = regmap_write_bits(regmap, MAX7360_REG_PWMCFG(i),
Signature · 1 line
2.47.3
#12 lore raw

Re: [RFT PATCH 06/13] gpio: max7360: create the regmap-irq chip from the gpio-regmap callback

Quoted 2 lines · Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

...

drivers/gpio/gpio-max7360.c struct max7360_gpio_plat_data { · line 33

Quoted 4 lines
drivers/gpio/gpio-max7360.c +4 -0
+struct max7360_gpio {
+	struct device *dev;
+	struct regmap *regmap;
+};

Why do we need both? Is regmap created with difference 'dev'?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#13 lore raw

Re: [RFT PATCH 06/13] gpio: max7360: create the regmap-irq chip from the gpio-regmap callback

Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

Quoted 8 lines

...

drivers/gpio/gpio-max7360.c struct max7360_gpio_plat_data { · line 33

Quoted 4 lines
drivers/gpio/gpio-max7360.c +4 -0
+struct max7360_gpio {
+	struct device *dev;
+	struct regmap *regmap;
+};

Why do we need both? Is regmap created with difference 'dev'?

Because the regmap is fetched from the parent, i.e the MFD. But the callback still needs the GPIO device for allocations.

I wanted to keep the change as simple as possible and just moved the whole init section over. But it's even simpler to just set up everything in probe and then just call gpio_regmap_add_irq_chip() in the callback, though that brings more fields to struct max7360. But it makes the code a nicer example.

-michael

author maintainer
#14 lore raw

PATCH 7/13 gpio: sl28cpld: create the regmap-irq chip from the gpio-regmap callback

In reply to Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

---
 drivers/gpio/gpio-sl28cpld.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)
drivers/gpio/gpio-sl28cpld.c +23 -11
index 57cadeb07fde..46141da2d2f7 100644
@@ -44,14 +44,28 @@ static const struct regmap_irq sl28cpld_gpio_irqs[] = {
 	REGMAP_IRQ_REG_LINE(7, 8),
 };
 
+struct sl28cpld_gpio {
+	struct regmap_irq_chip irq_chip;
+	int irq;
+};
+
+static struct irq_domain *sl28cpld_gpio_create_irq_domain(struct gpio_regmap *gpio)
+{
+	struct sl28cpld_gpio *sl28cpld_gpio = gpio_regmap_get_drvdata(gpio);
+
+	return gpio_regmap_add_irq_chip(gpio, sl28cpld_gpio->irq,
+					IRQF_SHARED | IRQF_ONESHOT,
+					&sl28cpld_gpio->irq_chip);
+}
+
 static int sl28cpld_gpio_irq_init(struct platform_device *pdev,
 				  unsigned int base,
 				  struct gpio_regmap_config *config)
 {
-	struct regmap_irq_chip_data *irq_data;
+	struct sl28cpld_gpio *sl28cpld_gpio;
 	struct regmap_irq_chip *irq_chip;
 	struct device *dev = &pdev->dev;
-	int irq, ret;
+	int irq;
 
 	if (!device_property_read_bool(dev, "interrupt-controller"))
 		return 0;
@@ -60,10 +74,12 @@ static int sl28cpld_gpio_irq_init(struct platform_device *pdev,
 	if (irq < 0)
 		return irq;
 
-	irq_chip = devm_kzalloc_obj(dev, *irq_chip);
-	if (!irq_chip)
+	sl28cpld_gpio = devm_kzalloc_obj(dev, *sl28cpld_gpio);
+	if (!sl28cpld_gpio)
 		return -ENOMEM;
 
+	irq_chip = &sl28cpld_gpio->irq_chip;
+
 	irq_chip->name = "sl28cpld-gpio-irq";
 	irq_chip->irqs = sl28cpld_gpio_irqs;
 	irq_chip->num_irqs = ARRAY_SIZE(sl28cpld_gpio_irqs);
@@ -72,14 +88,10 @@ static int sl28cpld_gpio_irq_init(struct platform_device *pdev,
 	irq_chip->unmask_base = base + GPIO_REG_IE;
 	irq_chip->ack_base = base + GPIO_REG_IP;
 
-	ret = devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(dev),
-					      config->regmap, irq,
-					      IRQF_SHARED | IRQF_ONESHOT,
-					      0, irq_chip, &irq_data);
-	if (ret)
-		return ret;
+	sl28cpld_gpio->irq = irq;
 
-	config->irq_domain = regmap_irq_get_domain(irq_data);
+	config->drvdata = sl28cpld_gpio;
+	config->create_irq_domain = sl28cpld_gpio_create_irq_domain;
 
 	return 0;
 }
Signature · 1 line
2.47.3
author maintainer
#15 lore raw

PATCH 8/13 gpio: idio-16: create the regmap-irq chip from the gpio-regmap callback

In reply to Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

---
 drivers/gpio/gpio-idio-16.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/gpio/gpio-idio-16.c +20 -8
index b9aa8bb9347a..b8ea1c0aa2a5 100644
@@ -34,12 +34,14 @@
 struct idio_16_data {
 	struct regmap *map;
 	unsigned int irq_mask;
+	struct regmap_irq_chip *chip;
+	unsigned int irq;
 };
 
 static int idio_16_handle_mask_sync(const int index, const unsigned int mask_buf_def,
 				    const unsigned int mask_buf, void *const irq_drv_data)
 {
-	struct idio_16_data *const data = irq_drv_data;
+	struct idio_16_data *const data = gpio_regmap_get_drvdata(irq_drv_data);
 	const unsigned int prev_mask = data->irq_mask;
 	int err;
 	unsigned int val;
@@ -66,6 +68,19 @@ static int idio_16_handle_mask_sync(const int index, const unsigned int mask_buf
 	return 0;
 }
 
+static struct irq_domain *idio_16_create_irq_domain(struct gpio_regmap *gpio)
+{
+	struct idio_16_data *data = gpio_regmap_get_drvdata(gpio);
+	struct irq_domain *domain;
+
+	domain = gpio_regmap_add_irq_chip(gpio, data->irq, 0, data->chip);
+	if (IS_ERR(domain))
+		return dev_err_cast_probe(regmap_get_device(data->map), domain,
+					  "IRQ registration failed\n");
+
+	return domain;
+}
+
 static int idio_16_reg_mask_xlate(struct gpio_regmap *const gpio, enum gpio_regmap_operation op,
 				  const unsigned int base, const unsigned int offset,
 				  unsigned int *const reg, unsigned int *const mask)
@@ -107,7 +122,6 @@ int devm_idio_16_regmap_register(struct device *const dev,
 	int err;
 	struct idio_16_data *data;
 	struct regmap_irq_chip *chip;
-	struct regmap_irq_chip_data *chip_data;
 	DECLARE_BITMAP(fixed_direction_output, IDIO_16_NGPIO);
 
 	if (!config->parent)
@@ -137,17 +151,14 @@ int devm_idio_16_regmap_register(struct device *const dev,
 	chip->irqs = config->regmap_irqs;
 	chip->num_irqs = config->num_regmap_irqs;
 	chip->handle_mask_sync = idio_16_handle_mask_sync;
-	chip->irq_drv_data = data;
+	data->chip = chip;
+	data->irq = config->irq;
 
 	/* Disable IRQ to prevent spurious interrupts before we're ready */
 	err = regmap_write(data->map, IDIO_16_DISABLE_IRQ, 0x00);
 	if (err)
 		return err;
 
-	err = devm_regmap_add_irq_chip(dev, data->map, config->irq, 0, 0, chip, &chip_data);
-	if (err)
-		return dev_err_probe(dev, err, "IRQ registration failed\n");
-
 	if (config->filters) {
 		/* Deactivate input filters */
 		err = regmap_write(data->map, IDIO_16_DEACTIVATE_INPUT_FILTERS, 0x00);
@@ -163,7 +174,8 @@ int devm_idio_16_regmap_register(struct device *const dev,
 	gpio_config.reg_set_base = GPIO_REGMAP_ADDR(IDIO_16_DAT_BASE);
 	gpio_config.ngpio_per_reg = IDIO_16_NGPIO_PER_REG;
 	gpio_config.reg_stride = IDIO_16_REG_STRIDE;
-	gpio_config.irq_domain = regmap_irq_get_domain(chip_data);
+	gpio_config.drvdata = data;
+	gpio_config.create_irq_domain = idio_16_create_irq_domain;
 	gpio_config.reg_mask_xlate = idio_16_reg_mask_xlate;
 
 	bitmap_from_u64(fixed_direction_output, GENMASK_U64(15, 0));
Signature · 1 line
2.47.3
author maintainer
#16 lore raw

PATCH 9/13 gpio: pcie-idio-24: create the regmap-irq chip from the gpio-regmap callback

In reply to Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

---
 drivers/gpio/gpio-pcie-idio-24.c | 36 ++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 11 deletions(-)
drivers/gpio/gpio-pcie-idio-24.c +25 -11
index 8fb34398053d..3be7aad266e0 100644
@@ -148,18 +148,22 @@ static const struct regmap_irq idio_24_regmap_irqs[] = {
  * @map:	regmap for the device
  * @lock:	synchronization lock to prevent I/O race conditions
  * @irq_type:	type configuration for IRQs
+ * @chip:	regmap_irq_chip for the device
+ * @irq:	parent IRQ
  */
 struct idio_24_gpio {
 	struct regmap *map;
 	raw_spinlock_t lock;
 	u8 irq_type;
+	struct regmap_irq_chip *chip;
+	int irq;
 };
 
 static int idio_24_handle_mask_sync(const int index, const unsigned int mask_buf_def,
 				    const unsigned int mask_buf, void *const irq_drv_data)
 {
 	const unsigned int type_mask = COS_ENABLE_BOTH << index;
-	struct idio_24_gpio *const idio24gpio = irq_drv_data;
+	struct idio_24_gpio *const idio24gpio = gpio_regmap_get_drvdata(irq_drv_data);
 	u8 type;
 	int ret;
 
@@ -183,7 +187,7 @@ static int idio_24_set_type_config(unsigned int **const buf, const unsigned int
 	const unsigned int rising = COS_ENABLE_RISING << offset;
 	const unsigned int falling = COS_ENABLE_FALLING << offset;
 	const unsigned int mask = COS_ENABLE_BOTH << offset;
-	struct idio_24_gpio *const idio24gpio = irq_drv_data;
+	struct idio_24_gpio *const idio24gpio = gpio_regmap_get_drvdata(irq_drv_data);
 	unsigned int new;
 	unsigned int cos_enable;
 	int ret;
@@ -225,13 +229,27 @@ static int idio_24_set_type_config(unsigned int **const buf, const unsigned int
 	return ret;
 }
 
+static struct irq_domain *idio_24_create_irq_domain(struct gpio_regmap *gpio)
+{
+	struct idio_24_gpio *const idio24gpio = gpio_regmap_get_drvdata(gpio);
+	struct irq_domain *domain;
+
+	domain = gpio_regmap_add_irq_chip(gpio, idio24gpio->irq, 0, idio24gpio->chip);
+	if (IS_ERR(domain))
+		return dev_err_cast_probe(regmap_get_device(idio24gpio->map), domain,
+					  "IRQ registration failed\n");
+
+	return domain;
+}
+
 static int idio_24_reg_mask_xlate(struct gpio_regmap *const gpio, enum gpio_regmap_operation op,
 				  const unsigned int base, const unsigned int offset,
 				  unsigned int *const reg, unsigned int *const mask)
 {
 	const unsigned int out_stride = offset / IDIO_24_NGPIO_PER_REG;
 	const unsigned int in_stride = (offset - 24) / IDIO_24_NGPIO_PER_REG;
-	struct regmap *const map = gpio_regmap_get_drvdata(gpio);
+	struct idio_24_gpio *const idio24gpio = gpio_regmap_get_drvdata(gpio);
+	struct regmap *const map = idio24gpio->map;
 	int err;
 	unsigned int ctrl_reg;
 
@@ -302,7 +320,6 @@ static int idio_24_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	void __iomem *idio_24_regs;
 	struct regmap *intcsr_map;
 	struct regmap_irq_chip *chip;
-	struct regmap_irq_chip_data *chip_data;
 
 	err = pcim_enable_device(pdev);
 	if (err)
@@ -348,7 +365,8 @@ static int idio_24_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	chip->num_irqs = ARRAY_SIZE(idio_24_regmap_irqs);
 	chip->handle_mask_sync = idio_24_handle_mask_sync;
 	chip->set_type_config = idio_24_set_type_config;
-	chip->irq_drv_data = idio24gpio;
+	idio24gpio->chip = chip;
+	idio24gpio->irq = pdev->irq;
 
 	/* Software board reset */
 	err = regmap_write(idio24gpio->map, IDIO_24_SOFT_RESET, 0);
@@ -362,10 +380,6 @@ static int idio_24_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (err)
 		return err;
 
-	err = devm_regmap_add_irq_chip(dev, idio24gpio->map, pdev->irq, 0, 0, chip, &chip_data);
-	if (err)
-		return dev_err_probe(dev, err, "IRQ registration failed\n");
-
 	gpio_config.parent = dev;
 	gpio_config.regmap = idio24gpio->map;
 	gpio_config.ngpio = IDIO_24_NGPIO;
@@ -374,9 +388,9 @@ static int idio_24_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	gpio_config.reg_set_base = GPIO_REGMAP_ADDR(IDIO_24_OUT_BASE);
 	gpio_config.reg_dir_out_base = GPIO_REGMAP_ADDR(IDIO_24_CONTROL_REG);
 	gpio_config.ngpio_per_reg = IDIO_24_NGPIO_PER_REG;
-	gpio_config.irq_domain = regmap_irq_get_domain(chip_data);
 	gpio_config.reg_mask_xlate = idio_24_reg_mask_xlate;
-	gpio_config.drvdata = idio24gpio->map;
+	gpio_config.drvdata = idio24gpio;
+	gpio_config.create_irq_domain = idio_24_create_irq_domain;
 
 	return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config));
 }
Signature · 1 line
2.47.3
#17 lore raw

Re: [RFT PATCH 09/13] gpio: pcie-idio-24: create the regmap-irq chip from the gpio-regmap callback

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] gpio: pcie-idio-24: gpio_regmap_add_irq_chip() breaks IIN0..23 IRQ requests --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] gpio: pcie-idio-24: gpio_regmap_add_irq_chip() breaks IIN0..23 IRQ requests
--

commit c95318f918a77c064e6fbf9f0881933abda168a3
Author: Michael Walle <[email protected]>

gpio: pcie-idio-24: create the regmap-irq chip from the gpio-regmap callback

This commit converts the pcie-idio-24 GPIO driver to register its regmap IRQ chip through the gpio_regmap_config.create_irq_domain callback using gpio_regmap_add_irq_chip().

Quoted 4 lines
drivers/gpio/gpio-pcie-idio-24.c +0 -0
index 8fb34398053dc..3be7aad266e0d 100644

[ ... ]

Quoted 6 lines
drivers/gpio/gpio-pcie-idio-24.c +2 -0
@@ -225,13 +229,27 @@ static int idio_24_set_type_config(unsigned int **const buf, const unsigned int
 	return ret;
 }
 
+static struct irq_domain *idio_24_create_irq_domain(struct gpio_regmap *gpio)
+{

drivers/gpio/gpio-pcie-idio-24.c static int idio_24_set_type_config(unsigned int **const buf, const unsigned int · line 243

Quoted 10 lines
drivers/gpio/gpio-pcie-idio-24.c +10 -0
+	struct idio_24_gpio *const idio24gpio = gpio_regmap_get_drvdata(gpio);
+	struct irq_domain *domain;
+
+	domain = gpio_regmap_add_irq_chip(gpio, idio24gpio->irq, 0, idio24gpio->chip);
+	if (IS_ERR(domain))
+		return dev_err_cast_probe(regmap_get_device(idio24gpio->map), domain,
+					  "IRQ registration failed\n");
+
+	return domain;
+}

[Severity: High]
Does switching to gpio_regmap_add_irq_chip() cause IRQ requests on the Isolated Input lines (IIN0..IIN23, offsets 24..47) to fail with -ENOTSUPP?

Unlike devm_regmap_add_irq_chip(), gpio_regmap_add_irq_chip() installs gpio_regmap_irq_reqres(), which calls gpiochip_reqres_irq() -> gpiochip_lock_as_irq(). Because gc->can_sleep is false and gc->get_direction is set, gpiochip_lock_as_irq() queries gpiod_get_direction(). Without fixed_direction_mask configured in gpio_config, gpio_regmap_get_direction() calls idio_24_reg_mask_xlate() with IDIO_24_CONTROL_REG, which rejects offsets below 48:

drivers/gpio/gpio-pcie-idio-24.c:idio_24_reg_mask_xlate() {
    ...
	case IDIO_24_CONTROL_REG:
		/* We can only set direction for TTL/CMOS lines */
		if (offset < 48)
			return -ENOTSUPP;
    ...
}

As a result, requesting an interrupt on any of the 24 Isolated Input lines fails. Setting gpio_config.fixed_direction_mask (and fixed_direction_output for the FET outputs at offsets 0..23) or returning the fixed directions in idio_24_reg_mask_xlate() would allow gpiod_get_direction() to succeed.

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

Re: [RFT PATCH 09/13] gpio: pcie-idio-24: create the regmap-irq chip from the gpio-regmap callback

Quoted 4 lines

[Severity: High]
Does switching to gpio_regmap_add_irq_chip() cause IRQ requests on the Isolated Input lines (IIN0..IIN23, offsets 24..47) to fail with -ENOTSUPP?

Yes, so installing our interrupt locking helpers doesn't work for all drivers after all.

-michael

author maintainer
#19 lore raw

PATCH 10/13 gpio: ws16c48: create the regmap-irq chip from the gpio-regmap callback

In reply to Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

---
 drivers/gpio/gpio-ws16c48.c | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)
drivers/gpio/gpio-ws16c48.c +25 -11
index cd0c35c057cb..6a5e74fc3395 100644
@@ -104,16 +104,20 @@ static const struct regmap_irq ws16c48_regmap_irqs[WS16C48_NUM_IRQS] = {
  * @map:	regmap for the device
  * @lock:	synchronization lock to prevent I/O race conditions
  * @irq_mask:	I/O bits affected by interrupts
+ * @chip:	regmap_irq_chip for the device
+ * @irq:	parent IRQ
  */
 struct ws16c48_gpio {
 	struct regmap *map;
 	raw_spinlock_t lock;
 	u8 irq_mask[WS16C48_NUM_IRQS / WS16C48_NGPIO_PER_REG];
+	struct regmap_irq_chip *chip;
+	unsigned int irq;
 };
 
 static int ws16c48_handle_pre_irq(void *const irq_drv_data) __acquires(&ws16c48gpio->lock)
 {
-	struct ws16c48_gpio *const ws16c48gpio = irq_drv_data;
+	struct ws16c48_gpio *const ws16c48gpio = gpio_regmap_get_drvdata(irq_drv_data);
 
 	/* Lock to prevent Page/Lock register change while we handle IRQ */
 	raw_spin_lock(&ws16c48gpio->lock);
@@ -123,7 +127,7 @@ static int ws16c48_handle_pre_irq(void *const irq_drv_data) __acquires(&ws16c48g
 
 static int ws16c48_handle_post_irq(void *const irq_drv_data) __releases(&ws16c48gpio->lock)
 {
-	struct ws16c48_gpio *const ws16c48gpio = irq_drv_data;
+	struct ws16c48_gpio *const ws16c48gpio = gpio_regmap_get_drvdata(irq_drv_data);
 
 	raw_spin_unlock(&ws16c48gpio->lock);
 
@@ -133,7 +137,7 @@ static int ws16c48_handle_post_irq(void *const irq_drv_data) __releases(&ws16c48
 static int ws16c48_handle_mask_sync(const int index, const unsigned int mask_buf_def,
 				    const unsigned int mask_buf, void *const irq_drv_data)
 {
-	struct ws16c48_gpio *const ws16c48gpio = irq_drv_data;
+	struct ws16c48_gpio *const ws16c48gpio = gpio_regmap_get_drvdata(irq_drv_data);
 	unsigned long flags;
 	int ret = 0;
 
@@ -167,7 +171,7 @@ static int ws16c48_set_type_config(unsigned int **const buf, const unsigned int
 				   const struct regmap_irq *const irq_data, const int idx,
 				   void *const irq_drv_data)
 {
-	struct ws16c48_gpio *const ws16c48gpio = irq_drv_data;
+	struct ws16c48_gpio *const ws16c48gpio = gpio_regmap_get_drvdata(irq_drv_data);
 	unsigned int polarity;
 	unsigned long flags;
 	int ret;
@@ -242,6 +246,19 @@ static int ws16c48_irq_init_hw(struct regmap *const map)
 	return regmap_write(map, WS16C48_PAGE_LOCK, INT_ID_PAGE);
 }
 
+static struct irq_domain *ws16c48_create_irq_domain(struct gpio_regmap *gpio)
+{
+	struct ws16c48_gpio *const ws16c48gpio = gpio_regmap_get_drvdata(gpio);
+	struct irq_domain *domain;
+
+	domain = gpio_regmap_add_irq_chip(gpio, ws16c48gpio->irq, 0, ws16c48gpio->chip);
+	if (IS_ERR(domain))
+		return dev_err_cast_probe(regmap_get_device(ws16c48gpio->map), domain,
+					  "IRQ registration failed\n");
+
+	return domain;
+}
+
 static int ws16c48_probe(struct device *dev, unsigned int id)
 {
 	struct ws16c48_gpio *ws16c48gpio;
@@ -250,7 +267,6 @@ static int ws16c48_probe(struct device *dev, unsigned int id)
 	struct gpio_regmap_config gpio_config = {};
 	void __iomem *regs;
 	struct regmap_irq_chip *chip;
-	struct regmap_irq_chip_data *chip_data;
 
 	ws16c48gpio = devm_kzalloc_obj(dev, *ws16c48gpio);
 	if (!ws16c48gpio)
@@ -286,7 +302,8 @@ static int ws16c48_probe(struct device *dev, unsigned int id)
 	chip->handle_post_irq = ws16c48_handle_post_irq;
 	chip->handle_mask_sync = ws16c48_handle_mask_sync;
 	chip->set_type_config = ws16c48_set_type_config;
-	chip->irq_drv_data = ws16c48gpio;
+	ws16c48gpio->chip = chip;
+	ws16c48gpio->irq = irq[id];
 
 	raw_spin_lock_init(&ws16c48gpio->lock);
 
@@ -295,10 +312,6 @@ static int ws16c48_probe(struct device *dev, unsigned int id)
 	if (err)
 		return err;
 
-	err = devm_regmap_add_irq_chip(dev, ws16c48gpio->map, irq[id], 0, 0, chip, &chip_data);
-	if (err)
-		return dev_err_probe(dev, err, "IRQ registration failed\n");
-
 	gpio_config.parent = dev;
 	gpio_config.regmap = ws16c48gpio->map;
 	gpio_config.ngpio = WS16C48_NGPIO;
@@ -308,7 +321,8 @@ static int ws16c48_probe(struct device *dev, unsigned int id)
 	/* Setting a GPIO to 0 allows it to be used as an input */
 	gpio_config.reg_dir_out_base = GPIO_REGMAP_ADDR(WS16C48_DAT_BASE);
 	gpio_config.ngpio_per_reg = WS16C48_NGPIO_PER_REG;
-	gpio_config.irq_domain = regmap_irq_get_domain(chip_data);
+	gpio_config.drvdata = ws16c48gpio;
+	gpio_config.create_irq_domain = ws16c48_create_irq_domain;
 
 	return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config));
 }
Signature · 1 line
2.47.3
author maintainer
#20 lore raw

PATCH 11/13 gpio: 104-idi-48: create the regmap-irq chip from the gpio-regmap callback

In reply to Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

---
 drivers/gpio/gpio-104-idi-48.c | 38 +++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 10 deletions(-)
drivers/gpio/gpio-104-idi-48.c +28 -10
index 93d7ccaded83..434704c06d7e 100644
@@ -123,15 +123,33 @@ static const char *idi48_names[IDI48_NGPIO] = {
 	"Bit 18 B", "Bit 19 B", "Bit 20 B", "Bit 21 B", "Bit 22 B", "Bit 23 B"
 };
 
+struct idi_48_gpio {
+	struct regmap_irq_chip chip;
+	struct device *dev;
+	unsigned int irq;
+};
+
+static struct irq_domain *idi_48_create_irq_domain(struct gpio_regmap *gpio)
+{
+	struct idi_48_gpio *const idi48gpio = gpio_regmap_get_drvdata(gpio);
+	struct irq_domain *domain;
+
+	domain = gpio_regmap_add_irq_chip(gpio, idi48gpio->irq, IRQF_SHARED, &idi48gpio->chip);
+	if (IS_ERR(domain))
+		return dev_err_cast_probe(idi48gpio->dev, domain,
+					  "IRQ registration failed\n");
+
+	return domain;
+}
+
 static int idi_48_probe(struct device *dev, unsigned int id)
 {
 	const char *const name = dev_name(dev);
 	struct gpio_regmap_config config = {};
+	struct idi_48_gpio *idi48gpio;
 	void __iomem *regs;
 	struct regmap *map;
 	struct regmap_irq_chip *chip;
-	struct regmap_irq_chip_data *chip_data;
-	int err;
 
 	if (!devm_request_region(dev, base[id], IDI_48_EXTENT, name)) {
 		dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
@@ -148,10 +166,14 @@ static int idi_48_probe(struct device *dev, unsigned int id)
 		return dev_err_probe(dev, PTR_ERR(map),
 				     "Unable to initialize register map\n");
 
-	chip = devm_kzalloc_obj(dev, *chip);
-	if (!chip)
+	idi48gpio = devm_kzalloc_obj(dev, *idi48gpio);
+	if (!idi48gpio)
 		return -ENOMEM;
 
+	idi48gpio->dev = dev;
+	idi48gpio->irq = irq[id];
+	chip = &idi48gpio->chip;
+
 	chip->name = name;
 	chip->status_base = IDI48_IRQ_STATUS;
 	chip->unmask_base = IDI48_IRQ_ENABLE;
@@ -160,11 +182,6 @@ static int idi_48_probe(struct device *dev, unsigned int id)
 	chip->irqs = idi48_regmap_irqs;
 	chip->num_irqs = ARRAY_SIZE(idi48_regmap_irqs);
 
-	err = devm_regmap_add_irq_chip(dev, map, irq[id], IRQF_SHARED, 0, chip,
-				       &chip_data);
-	if (err)
-		return dev_err_probe(dev, err, "IRQ registration failed\n");
-
 	config.parent = dev;
 	config.regmap = map;
 	config.ngpio = IDI48_NGPIO;
@@ -172,7 +189,8 @@ static int idi_48_probe(struct device *dev, unsigned int id)
 	config.reg_dat_base = GPIO_REGMAP_ADDR(0x0);
 	config.ngpio_per_reg = 8;
 	config.reg_mask_xlate = idi_48_reg_mask_xlate;
-	config.irq_domain = regmap_irq_get_domain(chip_data);
+	config.drvdata = idi48gpio;
+	config.create_irq_domain = idi_48_create_irq_domain;
 
 	return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &config));
 }
Signature · 1 line
2.47.3
#21 lore raw

Re: [RFT PATCH 11/13] gpio: 104-idi-48: create the regmap-irq chip from the gpio-regmap callback

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] gpio: 104-idi-48: skipped IRQ mask…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] gpio: 104-idi-48: skipped IRQ mask write and status clear in idi_48_probe() [1]
--

commit 36940f74396bbd63f657b4578ebd69175712902d
Author: Michael Walle <[email protected]>

gpio: 104-idi-48: create the regmap-irq chip from the gpio-regmap callback

This commit converts the 104-idi-48 driver to create its regmap-irq domain via the gpio_regmap_config.create_irq_domain callback using gpio_regmap_add_irq_chip().

Quoted 4 lines
drivers/gpio/gpio-104-idi-48.c +0 -0
index 93d7ccaded83e..434704c06d7ef 100644

[ ... ]

Quoted 7 lines
drivers/gpio/gpio-104-idi-48.c +1 -2
@@ -148,10 +166,14 @@ static int idi_48_probe(struct device *dev, unsigned int id)
 		return dev_err_probe(dev, PTR_ERR(map),
 				     "Unable to initialize register map\n");
 
-	chip = devm_kzalloc_obj(dev, *chip);
-	if (!chip)
+	idi48gpio = devm_kzalloc_obj(dev, *idi48gpio);

drivers/gpio/gpio-104-idi-48.c static int idi_48_probe(struct device *dev, unsigned int id) · line 179

Quoted 10 lines
drivers/gpio/gpio-104-idi-48.c +5 -0
+	if (!idi48gpio)
 		return -ENOMEM;
 
+	idi48gpio->dev = dev;
+	idi48gpio->irq = irq[id];
+	chip = &idi48gpio->chip;
+
 	chip->name = name;
 	chip->status_base = IDI48_IRQ_STATUS;
 	chip->unmask_base = IDI48_IRQ_ENABLE;

[Severity: High]
This problem wasn't introduced by this patch, but configuring chip->unmask_base on offset 0x7 without a register cache or handle_mask_sync callback causes regmap_update_bits() to perform a physical read-modify-write on offset 0x7 [1].

On the 104-idi-48, reading offset 0x7 returns and clears pending interrupt status bits rather than the active enable mask, while writing it programs the interrupt enable register. The read in _regmap_update_bits() clears pending interrupts before regmap_irq_thread() can service them, and when all banks are masked (~mask_buf[0] & 0x3f == 0) while no status bits are pending (orig & 0x3f == 0), _regmap_update_bits() sees no change and skips the write, leaving device interrupts enabled during probe and after masking all banks.

[1] https://sashiko.dev/#/bug/linux-84cdae6c-00ca-4070-8100-3b16d7660350

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

PATCH 12/13 gpio: i8255: create the regmap-irq chip from the gpio-regmap callback

In reply to Michael Walle

Use the new gpio_regmap_add_irq_chip() helper to create the regmap-irq domain.

---
 drivers/gpio/gpio-104-dio-48e.c | 31 +++++++++++++++++++++++--------
 drivers/gpio/gpio-i8255.c       |  3 ++-
 drivers/gpio/gpio-i8255.h       |  8 ++++++--
 3 files changed, 31 insertions(+), 11 deletions(-)
drivers/gpio/gpio-104-dio-48e.c +23 -8
index 7ddf578ba530..0efaa654274d 100644
@@ -9,6 +9,7 @@
 #include <linux/bits.h>
 #include <linux/device.h>
 #include <linux/err.h>
+#include <linux/gpio/regmap.h>
 #include <linux/i8254.h>
 #include <linux/ioport.h>
 #include <linux/irq.h>
@@ -113,6 +114,8 @@ static const struct regmap_irq dio48e_regmap_irqs[] = {
  * @regs:	virtual mapping for device registers
  * @flags:	IRQ flags saved during locking
  * @irq_mask:	Current IRQ mask state on the device
+ * @chip:	regmap_irq_chip for the device
+ * @irq:	Parent IRQ
  */
 struct dio48e_gpio {
 	raw_spinlock_t lock;
@@ -120,6 +123,8 @@ struct dio48e_gpio {
 	void __iomem *regs;
 	unsigned long flags;
 	unsigned int irq_mask;
+	struct regmap_irq_chip *chip;
+	unsigned int irq;
 };
 
 static void dio48e_regmap_lock(void *lock_arg) __acquires(&dio48egpio->lock)
@@ -163,7 +168,7 @@ static int dio48e_handle_mask_sync(const int index,
 				   const unsigned int mask_buf,
 				   void *const irq_drv_data)
 {
-	struct dio48e_gpio *const dio48egpio = irq_drv_data;
+	struct dio48e_gpio *const dio48egpio = gpio_regmap_get_drvdata(irq_drv_data);
 	const unsigned int prev_mask = dio48egpio->irq_mask;
 	int err;
 	unsigned int val;
@@ -218,6 +223,19 @@ static int dio48e_irq_init_hw(struct regmap *const map)
 	return regmap_read(map, DIO48E_DISABLE_INTERRUPT, &val);
 }
 
+static struct irq_domain *dio48e_create_irq_domain(struct gpio_regmap *gpio)
+{
+	struct dio48e_gpio *const dio48egpio = gpio_regmap_get_drvdata(gpio);
+	struct irq_domain *domain;
+
+	domain = gpio_regmap_add_irq_chip(gpio, dio48egpio->irq, 0, dio48egpio->chip);
+	if (IS_ERR(domain))
+		return dev_err_cast_probe(regmap_get_device(dio48egpio->map), domain,
+					  "IRQ registration failed\n");
+
+	return domain;
+}
+
 static int dio48e_probe(struct device *dev, unsigned int id)
 {
 	const char *const name = dev_name(dev);
@@ -230,7 +248,6 @@ static int dio48e_probe(struct device *dev, unsigned int id)
 	int err;
 	struct regmap_irq_chip *chip;
 	struct dio48e_gpio *dio48egpio;
-	struct regmap_irq_chip_data *chip_data;
 
 	if (!devm_request_region(dev, base[id], DIO48E_EXTENT, name)) {
 		dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
@@ -302,17 +319,14 @@ static int dio48e_probe(struct device *dev, unsigned int id)
 	chip->irqs = dio48e_regmap_irqs;
 	chip->num_irqs = ARRAY_SIZE(dio48e_regmap_irqs);
 	chip->handle_mask_sync = dio48e_handle_mask_sync;
-	chip->irq_drv_data = dio48egpio;
+	dio48egpio->chip = chip;
+	dio48egpio->irq = irq[id];
 
 	/* Initialize to prevent spurious interrupts before we're ready */
 	err = dio48e_irq_init_hw(map);
 	if (err)
 		return err;
 
-	err = devm_regmap_add_irq_chip(dev, map, irq[id], 0, 0, chip, &chip_data);
-	if (err)
-		return dev_err_probe(dev, err, "IRQ registration failed\n");
-
 	pit_config.parent = dev;
 
 	err = devm_i8254_regmap_register(dev, &pit_config);
@@ -323,7 +337,8 @@ static int dio48e_probe(struct device *dev, unsigned int id)
 	config.map = map;
 	config.num_ppi = DIO48E_NUM_PPI;
 	config.names = dio48e_names;
-	config.domain = regmap_irq_get_domain(chip_data);
+	config.drvdata = dio48egpio;
+	config.create_irq_domain = dio48e_create_irq_domain;
 
 	return devm_i8255_regmap_register(dev, &config);
 }
drivers/gpio/gpio-i8255.c +2 -1
index 541a348447cd..8ac46bb2c235 100644
@@ -129,7 +129,8 @@ int devm_i8255_regmap_register(struct device *const dev,
 	gpio_config.reg_set_base = GPIO_REGMAP_ADDR(I8255_REG_DAT_BASE);
 	gpio_config.reg_dir_in_base = GPIO_REGMAP_ADDR(I8255_REG_DIR_IN_BASE);
 	gpio_config.ngpio_per_reg = I8255_NGPIO_PER_REG;
-	gpio_config.irq_domain = config->domain;
+	gpio_config.drvdata = config->drvdata;
+	gpio_config.create_irq_domain = config->create_irq_domain;
 	gpio_config.reg_mask_xlate = i8255_reg_mask_xlate;
 
 	return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config));
drivers/gpio/gpio-i8255.h +6 -2
index 9dcf639b94df..fa90044c9cd5 100644
@@ -4,6 +4,7 @@
 #define _I8255_H_
 
 struct device;
+struct gpio_regmap;
 struct irq_domain;
 struct regmap;
 
@@ -15,7 +16,9 @@ struct regmap;
  * @map:	regmap for the i8255
  * @num_ppi:	number of i8255 Programmable Peripheral Interface
  * @names:	(optional) array of names for gpios
- * @domain:	(optional) IRQ domain if the controller is interrupt-capable
+ * @drvdata:	(optional) driver data, see gpio_regmap_get_drvdata()
+ * @create_irq_domain:	(optional) create the IRQ domain if the controller is
+ *			interrupt-capable, see struct gpio_regmap_config
  *
  * Note: The regmap is expected to have cache enabled and i8255 control
  * registers not marked as volatile.
@@ -25,7 +28,8 @@ struct i8255_regmap_config {
 	struct regmap *map;
 	int num_ppi;
 	const char *const *names;
-	struct irq_domain *domain;
+	void *drvdata;
+	struct irq_domain *(*create_irq_domain)(struct gpio_regmap *gpio);
 };
 
 int devm_i8255_regmap_register(struct device *dev,
Signature · 1 line
2.47.3
author maintainer
#23 lore raw

PATCH 13/13 gpio: regmap: remove irq_domain and the regmap-irq configuration

In reply to Michael Walle

Now that all users have been converted to the new .create_irq_domain() callback, remove the old methods.

Also remove the __maybe_unused annotations because the helpers are now guarded with CONFIG_REGMAP_IRQ.

---
 drivers/gpio/gpio-regmap.c  | 34 ++++------------------------------
 include/linux/gpio/regmap.h | 17 -----------------
 2 files changed, 4 insertions(+), 47 deletions(-)
drivers/gpio/gpio-regmap.c +4 -30
index 99a86efe8deb..fadc4565bb9d 100644
@@ -397,17 +397,17 @@ void gpio_regmap_relres_irq(struct gpio_regmap *gpio, unsigned int offset)
 }
 EXPORT_SYMBOL_GPL(gpio_regmap_relres_irq);
 
-static int __maybe_unused gpio_regmap_irq_reqres(void *irq_drv_data, irq_hw_number_t hwirq)
+#ifdef CONFIG_REGMAP_IRQ
+static int gpio_regmap_irq_reqres(void *irq_drv_data, irq_hw_number_t hwirq)
 {
 	return gpio_regmap_reqres_irq(irq_drv_data, hwirq);
 }
 
-static void __maybe_unused gpio_regmap_irq_relres(void *irq_drv_data, irq_hw_number_t hwirq)
+static void gpio_regmap_irq_relres(void *irq_drv_data, irq_hw_number_t hwirq)
 {
 	gpio_regmap_relres_irq(irq_drv_data, hwirq);
 }
 
-#ifdef CONFIG_REGMAP_IRQ
 /**
  * gpio_regmap_add_irq_chip() - Add a regmap-irq chip for the GPIO lines
  * @gpio: gpio_regmap device
@@ -476,7 +476,7 @@ EXPORT_SYMBOL_GPL(gpio_regmap_disable_irq);
  */
 struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config)
 {
-	struct irq_domain *irq_domain;
+	struct irq_domain *irq_domain = NULL;
 	struct gpio_regmap *gpio;
 	struct gpio_chip *chip;
 	int ret;
@@ -497,14 +497,6 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 	if (config->reg_dir_out_base && config->reg_dir_in_base)
 		return ERR_PTR(-EINVAL);
 
-	/* there is only one way to provide an IRQ domain */
-	if (config->create_irq_domain && config->irq_domain)
-		return ERR_PTR(-EINVAL);
-#ifdef CONFIG_REGMAP_IRQ
-	if (config->create_irq_domain && config->regmap_irq_chip)
-		return ERR_PTR(-EINVAL);
-#endif
-
 	gpio = kzalloc_obj(*gpio);
 	if (!gpio)
 		return ERR_PTR(-ENOMEM);
@@ -596,8 +588,6 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 	if (ret < 0)
 		goto err_free_bitmap_output;
 
-	irq_domain = config->irq_domain;
-
 	if (config->create_irq_domain) {
 		irq_domain = config->create_irq_domain(gpio);
 		if (IS_ERR(irq_domain)) {
@@ -606,22 +596,6 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 		}
 	}
 
-#ifdef CONFIG_REGMAP_IRQ
-	if (config->regmap_irq_chip) {
-		gpio->regmap_irq_line = config->regmap_irq_line;
-		config->regmap_irq_chip->irq_reqres = gpio_regmap_irq_reqres;
-		config->regmap_irq_chip->irq_relres = gpio_regmap_irq_relres;
-		config->regmap_irq_chip->irq_drv_data = gpio;
-		ret = regmap_add_irq_chip_fwnode(dev_fwnode(config->parent), config->regmap,
-						 config->regmap_irq_line, config->regmap_irq_flags,
-						 0, config->regmap_irq_chip, &gpio->irq_chip_data);
-		if (ret)
-			goto err_remove_gpiochip;
-
-		irq_domain = regmap_irq_get_domain(gpio->irq_chip_data);
-	}
-#endif
-
 	if (irq_domain) {
 		ret = gpiochip_irqchip_add_domain(chip, irq_domain);
 		if (ret)
include/linux/gpio/regmap.h +0 -17
index 7f578b9a82e6..fa67a29cecd8 100644
@@ -63,15 +63,10 @@ enum gpio_regmap_operation {
  * @reg_stride:		(Optional) May be set if the registers (of the
  *			same type, dat, set, etc) are not consecutive.
  * @ngpio_per_reg:	(Optional) Number of GPIOs per register
- * @irq_domain:		(Optional) IRQ domain if the controller is
- *			interrupt-capable. Cannot be used together with the
- *			gpio_regmap_*_irq() callbacks. Use @create_irq_domain
- *			instead.
  * @create_irq_domain:	(Optional) Callback to create the IRQ domain in the
  *			controller driver. Called after the gpio_chip is added
  *			so the gpio_regmap_*_irq() have a valid gpio_regmap
  *			pointer.
- *			Mutually exclusive with @irq_domain and @regmap_irq_chip.
  * @fixed_direction_mask:
  *			(Optional) Bitmap representing the GPIO lines that
  *			make use of the @fixed_direction_output list to
@@ -82,11 +77,6 @@ enum gpio_regmap_operation {
  *			(Optional) Bitmap representing the fixed direction of
  *			the GPIO lines. Useful when there are GPIO lines with a
  *			fixed direction mixed together in the same register.
- * @regmap_irq_chip:	(Optional) Pointer on an regmap_irq_chip structure. If
- *			set, a regmap-irq device will be created and the IRQ
- *			domain will be set accordingly.
- * @regmap_irq_line:	(Optional) The IRQ the device uses to signal interrupts.
- * @regmap_irq_flags:	(Optional) The IRQF_ flags to use for the interrupt.
  * @reg_mask_xlate:	(Optional) Translates base address and GPIO
  *			offset to a register/bitmask pair. If not
  *			given the default gpio_regmap_simple_xlate()
@@ -141,17 +131,10 @@ struct gpio_regmap_config {
 	unsigned int reg_dir_out_base;
 	int reg_stride;
 	int ngpio_per_reg;
-	struct irq_domain *irq_domain;
 	struct irq_domain *(*create_irq_domain)(struct gpio_regmap *gpio);
 	unsigned long *fixed_direction_mask;
 	unsigned long *fixed_direction_output;
 
-#ifdef CONFIG_REGMAP_IRQ
-	struct regmap_irq_chip *regmap_irq_chip;
-	int regmap_irq_line;
-	unsigned long regmap_irq_flags;
-#endif
-
 	int (*reg_mask_xlate)(struct gpio_regmap *gpio, enum gpio_regmap_operation,
 			      unsigned int base, unsigned int offset,
 			      unsigned int *reg, unsigned int *mask);
Signature · 1 line
2.47.3
Reviewed-by on the v1 series
#25 lore raw
In reply to Michael Walle
Quoted 22 lines · Michael Walle

The gpio-regmap provides two different variants to attach an IRQ domain to the gpio-regmap instance, either via .irq_domain or via .regmap_irq_chip. The former even has a small window where a consumer requesting an interrupt leads to a NULL pointer dereference. This also came up during sashiko reviews [1].

It turns out, that it is impossible to create an external IRQ domain without a race while the gpio-regmap instance isn't created yet. That is because it will already use the gpio-regmap provided callbacks. Thus, with this patch series, gpio-regmap will now provide a callback for the individual drivers used to create an IRQ domain when all necessary fields are filled.

To move even further and cover the most common use case of having a regmap-irq domain, gpio-regmap now provides a helper that can be used in the .create_irq_domain() callback.

We need Long Zhao's patch [2] not only for ACPI but also for the regmap-irq handling. If the IRQ domain is removed before the gpio_chip, gpiochip_remove() would use the already freed domain to free the remaining interrupts.

Quoted 7 lines

All the users of regmap-irq are switched over to this new helper. Finally, the last patch, removes the old variants.

There is one change in behavior: because all regmap-irq domains use the gpio_regmap_irq_{reqres,relres}() helpers now, interrupt lines are now locked and cannot be switched to output. But that shouldn't be an issue anyway.

It's so elegant solution, my full support!

(I had something more complicated in mind.)

Quoted 3 lines

This was mostly only compile-time tested. So please test. I'll do a proper test on the sl28 board tomorrow, but since Long already asked, I wanted to have this out for comments and testing.

I'm not sure I can help with testing (all our HW seems do not use this). Only regression test for other users might be done, but I need to check if we have platform that has an HW with the driver that uses gpio-regmap.

Quoted 2 lines

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]

Signature · 2 lines
With Best Regards,
Andy Shevchenko