linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts
@ 2024-06-24  9:39 Bartosz Golaszewski
  2024-06-24  9:39 ` [PATCH v2 1/2] genirq/irq_sim: add an extended irq_sim initializer Bartosz Golaszewski
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Bartosz Golaszewski @ 2024-06-24  9:39 UTC (permalink / raw)
  To: Linus Walleij, Thomas Gleixner
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

I realized that the gpio-sim module doesn't lock the GPIOs as interrupts
when they are requested from the irq_sim. This leads to users being able
to change the direction of GPIOs that should remain as inputs to output.
This series extends the irq_sim interface and allows users to supply
callbacks that will be executed to inform users about interrupts being
requested and released so that they can act accordingly. The gpio-sim is
made to use this new API and lock GPIOs as interrupts when needed.

Thomas: if this is fine with you, can you Ack it so that I can take it
through the GPIO tree for the next merge window?

Changes since v1:
- drop the notifier in favor of specific callbacks

Bartosz Golaszewski (2):
  genirq/irq_sim: add an extended irq_sim initializer
  gpio: sim: lock GPIOs as interrupts when they are requested

 drivers/gpio/gpio-sim.c | 25 ++++++++++++++++-
 include/linux/irq_sim.h | 17 ++++++++++++
 kernel/irq/irq_sim.c    | 60 ++++++++++++++++++++++++++++++++++++++---
 3 files changed, 98 insertions(+), 4 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/2] genirq/irq_sim: add an extended irq_sim initializer
  2024-06-24  9:39 [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
@ 2024-06-24  9:39 ` Bartosz Golaszewski
  2024-06-26 11:48   ` Linus Walleij
  2024-06-24  9:39 ` [PATCH v2 2/2] gpio: sim: lock GPIOs as interrupts when they are requested Bartosz Golaszewski
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: Bartosz Golaszewski @ 2024-06-24  9:39 UTC (permalink / raw)
  To: Linus Walleij, Thomas Gleixner
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Currently users of the interrupt simulator don't have any way of being
notified about interrupts from the simulated domain being requested or
released. This causes a problem for one of the users - the GPIO
simulator - which is unable to lock the pins as interrupts.

Define a structure containing callbacks to be executed on various
irq_sim-related events (for now: irq request and release) and provide an
extended function for creating simulated interrupt domains that takes it
and a pointer to custom user data (to be passed to said callbacks) as
arguments.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 include/linux/irq_sim.h | 17 ++++++++++++
 kernel/irq/irq_sim.c    | 60 ++++++++++++++++++++++++++++++++++++++---
 2 files changed, 74 insertions(+), 3 deletions(-)

diff --git a/include/linux/irq_sim.h b/include/linux/irq_sim.h
index ab831e5ae748..89b4d8ff274b 100644
--- a/include/linux/irq_sim.h
+++ b/include/linux/irq_sim.h
@@ -16,11 +16,28 @@
  * requested like normal irqs and enqueued from process context.
  */
 
+struct irq_sim_ops {
+	int (*irq_sim_irq_requested)(struct irq_domain *domain,
+				     irq_hw_number_t hwirq, void *data);
+	void (*irq_sim_irq_released)(struct irq_domain *domain,
+				     irq_hw_number_t hwirq, void *data);
+};
+
 struct irq_domain *irq_domain_create_sim(struct fwnode_handle *fwnode,
 					 unsigned int num_irqs);
 struct irq_domain *devm_irq_domain_create_sim(struct device *dev,
 					      struct fwnode_handle *fwnode,
 					      unsigned int num_irqs);
+struct irq_domain *irq_domain_create_sim_full(struct fwnode_handle *fwnode,
+					      unsigned int num_irqs,
+					      const struct irq_sim_ops *ops,
+					      void *data);
+struct irq_domain *
+devm_irq_domain_create_sim_full(struct device *dev,
+				struct fwnode_handle *fwnode,
+				unsigned int num_irqs,
+				const struct irq_sim_ops *ops,
+				void *data);
 void irq_domain_remove_sim(struct irq_domain *domain);
 
 #endif /* _LINUX_IRQ_SIM_H */
diff --git a/kernel/irq/irq_sim.c b/kernel/irq/irq_sim.c
index 38d6ae651ac7..3d4036db15ac 100644
--- a/kernel/irq/irq_sim.c
+++ b/kernel/irq/irq_sim.c
@@ -17,6 +17,8 @@ struct irq_sim_work_ctx {
 	unsigned int		irq_count;
 	unsigned long		*pending;
 	struct irq_domain	*domain;
+	struct irq_sim_ops	ops;
+	void			*user_data;
 };
 
 struct irq_sim_irq_ctx {
@@ -88,6 +90,31 @@ static int irq_sim_set_irqchip_state(struct irq_data *data,
 	return 0;
 }
 
+static int irq_sim_request_resources(struct irq_data *data)
+{
+	struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
+	struct irq_sim_work_ctx *work_ctx = irq_ctx->work_ctx;
+	irq_hw_number_t hwirq = irqd_to_hwirq(data);
+
+	if (work_ctx->ops.irq_sim_irq_requested)
+		return work_ctx->ops.irq_sim_irq_requested(work_ctx->domain,
+							   hwirq,
+							   work_ctx->user_data);
+
+	return 0;
+}
+
+static void irq_sim_release_resources(struct irq_data *data)
+{
+	struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
+	struct irq_sim_work_ctx *work_ctx = irq_ctx->work_ctx;
+	irq_hw_number_t hwirq = irqd_to_hwirq(data);
+
+	if (work_ctx->ops.irq_sim_irq_released)
+		work_ctx->ops.irq_sim_irq_released(work_ctx->domain, hwirq,
+						   work_ctx->user_data);
+}
+
 static struct irq_chip irq_sim_irqchip = {
 	.name			= "irq_sim",
 	.irq_mask		= irq_sim_irqmask,
@@ -95,6 +122,8 @@ static struct irq_chip irq_sim_irqchip = {
 	.irq_set_type		= irq_sim_set_type,
 	.irq_get_irqchip_state	= irq_sim_get_irqchip_state,
 	.irq_set_irqchip_state	= irq_sim_set_irqchip_state,
+	.irq_request_resources	= irq_sim_request_resources,
+	.irq_release_resources	= irq_sim_release_resources,
 };
 
 static void irq_sim_handle_irq(struct irq_work *work)
@@ -163,6 +192,15 @@ static const struct irq_domain_ops irq_sim_domain_ops = {
  */
 struct irq_domain *irq_domain_create_sim(struct fwnode_handle *fwnode,
 					 unsigned int num_irqs)
+{
+	return irq_domain_create_sim_full(fwnode, num_irqs, NULL, NULL);
+}
+EXPORT_SYMBOL_GPL(irq_domain_create_sim);
+
+struct irq_domain *irq_domain_create_sim_full(struct fwnode_handle *fwnode,
+					      unsigned int num_irqs,
+					      const struct irq_sim_ops *ops,
+					      void *data)
 {
 	struct irq_sim_work_ctx *work_ctx __free(kfree) =
 				kmalloc(sizeof(*work_ctx), GFP_KERNEL);
@@ -183,10 +221,14 @@ struct irq_domain *irq_domain_create_sim(struct fwnode_handle *fwnode,
 	work_ctx->irq_count = num_irqs;
 	work_ctx->work = IRQ_WORK_INIT_HARD(irq_sim_handle_irq);
 	work_ctx->pending = no_free_ptr(pending);
+	work_ctx->user_data = data;
+
+	if (ops)
+		memcpy(&work_ctx->ops, ops, sizeof(*ops));
 
 	return no_free_ptr(work_ctx)->domain;
 }
-EXPORT_SYMBOL_GPL(irq_domain_create_sim);
+EXPORT_SYMBOL_GPL(irq_domain_create_sim_full);
 
 /**
  * irq_domain_remove_sim - Deinitialize the interrupt simulator domain: free
@@ -227,11 +269,23 @@ static void devm_irq_domain_remove_sim(void *data)
 struct irq_domain *devm_irq_domain_create_sim(struct device *dev,
 					      struct fwnode_handle *fwnode,
 					      unsigned int num_irqs)
+{
+	return devm_irq_domain_create_sim_full(dev, fwnode, num_irqs,
+					       NULL, NULL);
+}
+EXPORT_SYMBOL_GPL(devm_irq_domain_create_sim);
+
+struct irq_domain *
+devm_irq_domain_create_sim_full(struct device *dev,
+				struct fwnode_handle *fwnode,
+				unsigned int num_irqs,
+				const struct irq_sim_ops *ops,
+				void *data)
 {
 	struct irq_domain *domain;
 	int ret;
 
-	domain = irq_domain_create_sim(fwnode, num_irqs);
+	domain = irq_domain_create_sim_full(fwnode, num_irqs, ops, data);
 	if (IS_ERR(domain))
 		return domain;
 
@@ -241,4 +295,4 @@ struct irq_domain *devm_irq_domain_create_sim(struct device *dev,
 
 	return domain;
 }
-EXPORT_SYMBOL_GPL(devm_irq_domain_create_sim);
+EXPORT_SYMBOL_GPL(devm_irq_domain_create_sim_full);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 2/2] gpio: sim: lock GPIOs as interrupts when they are requested
  2024-06-24  9:39 [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
  2024-06-24  9:39 ` [PATCH v2 1/2] genirq/irq_sim: add an extended irq_sim initializer Bartosz Golaszewski
@ 2024-06-24  9:39 ` Bartosz Golaszewski
  2024-06-26 11:48   ` Linus Walleij
  2024-07-01  8:41 ` [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: Bartosz Golaszewski @ 2024-06-24  9:39 UTC (permalink / raw)
  To: Linus Walleij, Thomas Gleixner
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Use the extended irq_sim interface to supply the simulated interrupt
domain with callbacks allowing the GPIO sim to lock/unlock GPIOs
requested as interrupts.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpio-sim.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index 4157735ea791..dcca1d7f173e 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -227,6 +227,27 @@ static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset)
 	}
 }
 
+static int gpio_sim_irq_requested(struct irq_domain *domain,
+				  irq_hw_number_t hwirq, void *data)
+{
+	struct gpio_sim_chip *chip = data;
+
+	return gpiochip_lock_as_irq(&chip->gc, hwirq);
+}
+
+static void gpio_sim_irq_released(struct irq_domain *domain,
+				  irq_hw_number_t hwirq, void *data)
+{
+	struct gpio_sim_chip *chip = data;
+
+	gpiochip_unlock_as_irq(&chip->gc, hwirq);
+}
+
+static const struct irq_sim_ops gpio_sim_irq_sim_ops = {
+	.irq_sim_irq_requested = gpio_sim_irq_requested,
+	.irq_sim_irq_released = gpio_sim_irq_released,
+};
+
 static void gpio_sim_dbg_show(struct seq_file *seq, struct gpio_chip *gc)
 {
 	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
@@ -443,7 +464,9 @@ static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev)
 	if (!chip->pull_map)
 		return -ENOMEM;
 
-	chip->irq_sim = devm_irq_domain_create_sim(dev, swnode, num_lines);
+	chip->irq_sim = devm_irq_domain_create_sim_full(dev, swnode, num_lines,
+							&gpio_sim_irq_sim_ops,
+							chip);
 	if (IS_ERR(chip->irq_sim))
 		return PTR_ERR(chip->irq_sim);
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/2] gpio: sim: lock GPIOs as interrupts when they are requested
  2024-06-24  9:39 ` [PATCH v2 2/2] gpio: sim: lock GPIOs as interrupts when they are requested Bartosz Golaszewski
@ 2024-06-26 11:48   ` Linus Walleij
  0 siblings, 0 replies; 8+ messages in thread
From: Linus Walleij @ 2024-06-26 11:48 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Thomas Gleixner, linux-gpio, linux-kernel, Bartosz Golaszewski

On Mon, Jun 24, 2024 at 11:39 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:

> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> Use the extended irq_sim interface to supply the simulated interrupt
> domain with callbacks allowing the GPIO sim to lock/unlock GPIOs
> requested as interrupts.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Looks good.

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] genirq/irq_sim: add an extended irq_sim initializer
  2024-06-24  9:39 ` [PATCH v2 1/2] genirq/irq_sim: add an extended irq_sim initializer Bartosz Golaszewski
@ 2024-06-26 11:48   ` Linus Walleij
  0 siblings, 0 replies; 8+ messages in thread
From: Linus Walleij @ 2024-06-26 11:48 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Thomas Gleixner, linux-gpio, linux-kernel, Bartosz Golaszewski

On Mon, Jun 24, 2024 at 11:39 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:

> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> Currently users of the interrupt simulator don't have any way of being
> notified about interrupts from the simulated domain being requested or
> released. This causes a problem for one of the users - the GPIO
> simulator - which is unable to lock the pins as interrupts.
>
> Define a structure containing callbacks to be executed on various
> irq_sim-related events (for now: irq request and release) and provide an
> extended function for creating simulated interrupt domains that takes it
> and a pointer to custom user data (to be passed to said callbacks) as
> arguments.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Fair enough, I see you agreed with tglx on this layout so:
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts
  2024-06-24  9:39 [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
  2024-06-24  9:39 ` [PATCH v2 1/2] genirq/irq_sim: add an extended irq_sim initializer Bartosz Golaszewski
  2024-06-24  9:39 ` [PATCH v2 2/2] gpio: sim: lock GPIOs as interrupts when they are requested Bartosz Golaszewski
@ 2024-07-01  8:41 ` Bartosz Golaszewski
  2024-07-03 12:48 ` Linus Walleij
  2024-07-04  7:26 ` Bartosz Golaszewski
  4 siblings, 0 replies; 8+ messages in thread
From: Bartosz Golaszewski @ 2024-07-01  8:41 UTC (permalink / raw)
  To: Linus Walleij, Thomas Gleixner
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

On Mon, Jun 24, 2024 at 11:39 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> I realized that the gpio-sim module doesn't lock the GPIOs as interrupts
> when they are requested from the irq_sim. This leads to users being able
> to change the direction of GPIOs that should remain as inputs to output.
> This series extends the irq_sim interface and allows users to supply
> callbacks that will be executed to inform users about interrupts being
> requested and released so that they can act accordingly. The gpio-sim is
> made to use this new API and lock GPIOs as interrupts when needed.
>
> Thomas: if this is fine with you, can you Ack it so that I can take it
> through the GPIO tree for the next merge window?
>
> Changes since v1:
> - drop the notifier in favor of specific callbacks
>
> Bartosz Golaszewski (2):
>   genirq/irq_sim: add an extended irq_sim initializer
>   gpio: sim: lock GPIOs as interrupts when they are requested
>
>  drivers/gpio/gpio-sim.c | 25 ++++++++++++++++-
>  include/linux/irq_sim.h | 17 ++++++++++++
>  kernel/irq/irq_sim.c    | 60 ++++++++++++++++++++++++++++++++++++++---
>  3 files changed, 98 insertions(+), 4 deletions(-)
>
> --
> 2.43.0
>

Without any objections, I'd like to take it through the GPIO tree by
the end of this week.

Bart

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts
  2024-06-24  9:39 [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
                   ` (2 preceding siblings ...)
  2024-07-01  8:41 ` [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
@ 2024-07-03 12:48 ` Linus Walleij
  2024-07-04  7:26 ` Bartosz Golaszewski
  4 siblings, 0 replies; 8+ messages in thread
From: Linus Walleij @ 2024-07-03 12:48 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Thomas Gleixner, linux-gpio, linux-kernel, Bartosz Golaszewski

On Mon, Jun 24, 2024 at 11:39 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:

> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> I realized that the gpio-sim module doesn't lock the GPIOs as interrupts
> when they are requested from the irq_sim. This leads to users being able
> to change the direction of GPIOs that should remain as inputs to output.
> This series extends the irq_sim interface and allows users to supply
> callbacks that will be executed to inform users about interrupts being
> requested and released so that they can act accordingly. The gpio-sim is
> made to use this new API and lock GPIOs as interrupts when needed.
>
> Thomas: if this is fine with you, can you Ack it so that I can take it
> through the GPIO tree for the next merge window?
>
> Changes since v1:
> - drop the notifier in favor of specific callbacks

Acked-by: Linus Walleij <linus.walleij@linaro.org>

It sure seems consistent to me and in line with how I once
thought about locking lines for IRQ.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts
  2024-06-24  9:39 [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
                   ` (3 preceding siblings ...)
  2024-07-03 12:48 ` Linus Walleij
@ 2024-07-04  7:26 ` Bartosz Golaszewski
  4 siblings, 0 replies; 8+ messages in thread
From: Bartosz Golaszewski @ 2024-07-04  7:26 UTC (permalink / raw)
  To: Linus Walleij, Thomas Gleixner, Bartosz Golaszewski
  Cc: Bartosz Golaszewski, linux-gpio, linux-kernel

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


On Mon, 24 Jun 2024 11:39:31 +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> I realized that the gpio-sim module doesn't lock the GPIOs as interrupts
> when they are requested from the irq_sim. This leads to users being able
> to change the direction of GPIOs that should remain as inputs to output.
> This series extends the irq_sim interface and allows users to supply
> callbacks that will be executed to inform users about interrupts being
> requested and released so that they can act accordingly. The gpio-sim is
> made to use this new API and lock GPIOs as interrupts when needed.
> 
> [...]

Applied, thanks!

[1/2] genirq/irq_sim: add an extended irq_sim initializer
      commit: 011f583781fa46699f1d4c4e9c39ad68f05ced2d
[2/2] gpio: sim: lock GPIOs as interrupts when they are requested
      commit: 9d9c1796a6ae70290c2e013fe4d79e99039a1015

Best regards,
-- 
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-07-04  7:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-24  9:39 [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
2024-06-24  9:39 ` [PATCH v2 1/2] genirq/irq_sim: add an extended irq_sim initializer Bartosz Golaszewski
2024-06-26 11:48   ` Linus Walleij
2024-06-24  9:39 ` [PATCH v2 2/2] gpio: sim: lock GPIOs as interrupts when they are requested Bartosz Golaszewski
2024-06-26 11:48   ` Linus Walleij
2024-07-01  8:41 ` [PATCH v2 0/2] gpio: sim: lock simulated GPIOs as interrupts Bartosz Golaszewski
2024-07-03 12:48 ` Linus Walleij
2024-07-04  7:26 ` Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).