Skip to contentSkip to conversation

gpio / ACPI: Handle ACPI events in accordance with the spec

PATCH In review Rafael J. Wysocki 4 participants 9 messages Apr 9, 2013 – Apr 10, 2013

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

PATCH 1/1 gpio / ACPI: Handle ACPI events in accordance with the spec

From: Rafael J. Wysocki <[email protected]>

Commit 0d1c28a (gpiolib-acpi: Add ACPI5 event model support to gpio.) that added support for ACPI events signalled through GPIO interrupts covered only GPIO pins whose numbers are less than or equal to 255. However, there may be GPIO pins with numbers greater than 255 and the ACPI spec (ACPI 5.0, Section 5.6.5.1) requires the _EVT method to be used for handling events corresponding to those pins.

Moreover, according to the spec, _EVT is the default mechanism for handling all ACPI events signalled through GPIO interrupts, so if the _Exx/_Lxx method is not present for the given pin, _EVT should be used instead. If present, though, _Exx/_Lxx take precedence over _EVT which shouldn't be executed in that case (ACPI 5.0, Section 5.6.5.3).

Modify acpi_gpiochip_request_interrupts() to follow the spec as described above and add acpi_gpiochip_free_interrupts() needed to free interrupts associated with _EVT.

---
 drivers/gpio/gpiolib-acpi.c |  140 +++++++++++++++++++++++++++++++++++++-------
 include/linux/acpi_gpio.h   |    2 
 2 files changed, 122 insertions(+), 20 deletions(-)
drivers/gpio/gpiolib-acpi.c +120 -20
Index: linux-pm/drivers/gpio/gpiolib-acpi.c
===================================================================
@@ -17,6 +17,13 @@
 #include <linux/acpi.h>
 #include <linux/interrupt.h>
 
+struct acpi_gpio_evt_pin {
+	struct list_head node;
+	acpi_handle *evt_handle;
+	unsigned int pin;
+	unsigned int irq;
+};
+
 static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
 {
 	if (!gc->dev)
@@ -54,7 +61,6 @@ int acpi_get_gpio(char *path, int pin)
 }
 EXPORT_SYMBOL_GPL(acpi_get_gpio);
 
-
 static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
 {
 	acpi_handle handle = data;
@@ -64,6 +70,27 @@ static irqreturn_t acpi_gpio_irq_handler
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
+{
+	struct acpi_gpio_evt_pin *evt_pin = data;
+	struct acpi_object_list args;
+	union acpi_object arg;
+
+	arg.type = ACPI_TYPE_INTEGER;
+	arg.integer.value = evt_pin->pin;
+	args.count = 1;
+	args.pointer = &arg;
+
+	acpi_evaluate_object(evt_pin->evt_handle, NULL, &args, NULL);
+
+	return IRQ_HANDLED;
+}
+
+static void acpi_gpio_evt_dh(acpi_handle handle, void *data)
+{
+	/* The address of this function is used as a key. */
+}
+
 /**
  * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  * @chip:      gpio chip
@@ -73,15 +100,13 @@ static irqreturn_t acpi_gpio_irq_handler
  * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  * gpio pins have acpi event methods and assigns interrupt handlers that calls
  * the acpi event methods for those pins.
- *
- * Interrupts are automatically freed on driver detach
  */
-
 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
 {
 	struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
 	struct acpi_resource *res;
-	acpi_handle handle, ev_handle;
+	acpi_handle handle, evt_handle;
+	struct list_head *evt_pins = NULL;
 	acpi_status status;
 	unsigned int pin;
 	int irq, ret;
@@ -98,13 +123,30 @@ void acpi_gpiochip_request_interrupts(st
 	if (ACPI_FAILURE(status))
 		return;
 
-	/* If a gpio interrupt has an acpi event handler method, then
-	 * set up an interrupt handler that calls the acpi event handler
-	 */
+	status = acpi_get_handle(handle, "_EVT", &evt_handle);
+	if (ACPI_SUCCESS(status)) {
+		evt_pins = kzalloc(sizeof(*evt_pins), GFP_KERNEL);
+		if (evt_pins) {
+			INIT_LIST_HEAD(evt_pins);
+			status = acpi_attach_data(handle, acpi_gpio_evt_dh,
+						  evt_pins);
+			if (ACPI_FAILURE(status)) {
+				kfree(evt_pins);
+				evt_pins = NULL;
+			}
+		}
+	}
 
+	/*
+	 * If a GPIO interrupt has an ACPI event handler method, or _EVT is
+	 * present, set up an interrupt handler that calls the ACPI event
+	 * handler.
+	 */
 	for (res = buf.pointer;
 	     res && (res->type != ACPI_RESOURCE_TYPE_END_TAG);
 	     res = ACPI_NEXT_RESOURCE(res)) {
+		irq_handler_t handler = NULL;
+		void *data;
 
 		if (res->type != ACPI_RESOURCE_TYPE_GPIO ||
 		    res->data.gpio.connection_type !=
@@ -115,23 +157,42 @@ void acpi_gpiochip_request_interrupts(st
 		if (pin > chip->ngpio)
 			continue;
 
-		sprintf(ev_name, "_%c%02X",
-		res->data.gpio.triggering ? 'E' : 'L', pin);
-
-		status = acpi_get_handle(handle, ev_name, &ev_handle);
-		if (ACPI_FAILURE(status))
-			continue;
-
 		irq = chip->to_irq(chip, pin);
 		if (irq < 0)
 			continue;
 
+		if (pin <= 255) {
+			acpi_handle ev_handle;
+
+			sprintf(ev_name, "_%c%02X",
+				res->data.gpio.triggering ? 'E' : 'L', pin);
+			status = acpi_get_handle(handle, ev_name, &ev_handle);
+			if (ACPI_SUCCESS(status)) {
+				handler = acpi_gpio_irq_handler;
+				data = ev_handle;
+			}
+		}
+		if (!handler && evt_pins) {
+			struct acpi_gpio_evt_pin *evt_pin;
+
+			evt_pin = kzalloc(sizeof(*evt_pin), GFP_KERNEL);
+			if (!evt_pin)
+				continue;
+
+			list_add_tail(&evt_pin->node, evt_pins);
+			evt_pin->evt_handle = evt_handle;
+			evt_pin->pin = pin;
+			evt_pin->irq = irq;
+			handler = acpi_gpio_irq_handler_evt;
+			data = evt_pin;
+		}
+		if (!handler)
+			continue;
+
 		/* Assume BIOS sets the triggering, so no flags */
-		ret = devm_request_threaded_irq(chip->dev, irq, NULL,
-					  acpi_gpio_irq_handler,
-					  0,
-					  "GPIO-signaled-ACPI-event",
-					  ev_handle);
+		ret = devm_request_threaded_irq(chip->dev, irq, NULL, handler,
+						0, "GPIO-signaled-ACPI-event",
+						data);
 		if (ret)
 			dev_err(chip->dev,
 				"Failed to request IRQ %d ACPI event handler\n",
@@ -139,3 +200,42 @@ void acpi_gpiochip_request_interrupts(st
 	}
 }
 EXPORT_SYMBOL(acpi_gpiochip_request_interrupts);
+
+
+/**
+ * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
+ * @chip:      gpio chip
+ *
+ * Free interrupts associated with the _EVT method for the given GPIO chip.
+ *
+ * The remaining ACPI event interrupts associated with the chip are freed
+ * automatically.
+ */
+void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
+{
+	acpi_handle handle;
+	acpi_status status;
+	struct list_head *evt_pins;
+	struct acpi_gpio_evt_pin *evt_pin, *ep;
+
+	if (!chip->dev || !chip->to_irq)
+		return;
+
+	handle = ACPI_HANDLE(chip->dev);
+	if (!handle)
+		return;
+
+	status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
+	if (ACPI_FAILURE(status))
+		return;
+
+	list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
+		devm_free_irq(chip->dev, evt_pin->irq, evt_pin);
+		list_del(&evt_pin->node);
+		kfree(evt_pin);
+	}
+
+	acpi_detach_data(handle, acpi_gpio_evt_dh);
+	kfree(evt_pins);
+}
+EXPORT_SYMBOL(acpi_gpiochip_free_interrupts);
include/linux/acpi_gpio.h +2 -0
Index: linux-pm/include/linux/acpi_gpio.h
===================================================================
@@ -8,6 +8,7 @@
 
 int acpi_get_gpio(char *path, int pin);
 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
+void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
 
 #else /* CONFIG_GPIO_ACPI */
 
@@ -17,6 +18,7 @@ static inline int acpi_get_gpio(char *pa
 }
 
 static inline void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
+static inline void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
 
 #endif /* CONFIG_GPIO_ACPI */
 
#1 lore raw
Quoted 10 lines · Rafael J. Wysocki
drivers/gpio/gpiolib-acpi.c +10 -0
+void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
+{
+	acpi_handle handle;
+	acpi_status status;
+	struct list_head *evt_pins;
+	struct acpi_gpio_evt_pin *evt_pin, *ep;
+
+	if (!chip->dev || !chip->to_irq)
+		return;
+

drivers/gpio/gpiolib-acpi.c void acpi_gpiochip_request_interrupts(st · line 233

Quoted 10 lines
drivers/gpio/gpiolib-acpi.c +10 -0
+	handle = ACPI_HANDLE(chip->dev);
+	if (!handle)
+		return;
+
+	status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
+	if (ACPI_FAILURE(status))
+		return;
+
+	list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
+		devm_free_irq(chip->dev, evt_pin->irq, evt_pin);

How about using normal request/free_irq() functions for both _EVT and non-_EVT events? Since we now need to call acpi_gpiochip_free_interrupts() anyway, I don't see the point using devm_* functions here.

Quoted 8 lines
drivers/gpio/gpiolib-acpi.c +8 -0
+		list_del(&evt_pin->node);
+		kfree(evt_pin);
+	}
+
+	acpi_detach_data(handle, acpi_gpio_evt_dh);
+	kfree(evt_pins);
+}
+EXPORT_SYMBOL(acpi_gpiochip_free_interrupts);
#2 lore raw
Quoted 17 lines · Mika Westerberg
Quoted 16 lines · Rafael J. Wysocki
drivers/gpio/gpiolib-acpi.c +16 -0
+void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
+{
+	acpi_handle handle;
+	acpi_status status;
+	struct list_head *evt_pins;
+	struct acpi_gpio_evt_pin *evt_pin, *ep;
+
+	if (!chip->dev || !chip->to_irq)
+		return;
+
+	handle = ACPI_HANDLE(chip->dev);
+	if (!handle)
+		return;
+
+	status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
+	if (ACPI_FAILURE(status))
Quoted 9 lines

drivers/gpio/gpiolib-acpi.c void acpi_gpiochip_request_interrupts(st · line 233

Quoted 4 lines
drivers/gpio/gpiolib-acpi.c +4 -0
+		return;
+
+	list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
+		devm_free_irq(chip->dev, evt_pin->irq, evt_pin);

How about using normal request/free_irq() functions for both _EVT and non-_EVT events? Since we now need to call acpi_gpiochip_free_interrupts() anyway, I don't see the point using devm_* functions here.

Then we need to create a list of non-_EVT events, or add them to the evt_pins list.

#3 lore raw
Quoted 28 lines · Mathias Nyman
Quoted 17 lines · Mika Westerberg
Quoted 16 lines · Rafael J. Wysocki
drivers/gpio/gpiolib-acpi.c +16 -0
+void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
+{
+	acpi_handle handle;
+	acpi_status status;
+	struct list_head *evt_pins;
+	struct acpi_gpio_evt_pin *evt_pin, *ep;
+
+	if (!chip->dev || !chip->to_irq)
+		return;
+
+	handle = ACPI_HANDLE(chip->dev);
+	if (!handle)
+		return;
+
+	status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
+	if (ACPI_FAILURE(status))
Quoted 9 lines

drivers/gpio/gpiolib-acpi.c void acpi_gpiochip_request_interrupts(st · line 233

Quoted 4 lines
drivers/gpio/gpiolib-acpi.c +4 -0
+		return;
+
+	list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
+		devm_free_irq(chip->dev, evt_pin->irq, evt_pin);

How about using normal request/free_irq() functions for both _EVT and non-_EVT events? Since we now need to call acpi_gpiochip_free_interrupts() anyway, I don't see the point using devm_* functions here.

Quoted 2 lines

Then we need to create a list of non-_EVT events, or add them to the evt_pins list.

Good point. Maybe we can add them to the evt_pins list and handle the same way as _EVT (except that we need to call _Exx and _Lxx methods instead of _EVT)?

#4 lore raw
Quoted 29 lines · Mika Westerberg
Quoted 28 lines · Mathias Nyman
Quoted 26 lines · Mika Westerberg
Quoted 10 lines · Rafael J. Wysocki
drivers/gpio/gpiolib-acpi.c +10 -0
+void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
+{
+	acpi_handle handle;
+	acpi_status status;
+	struct list_head *evt_pins;
+	struct acpi_gpio_evt_pin *evt_pin, *ep;
+
+	if (!chip->dev || !chip->to_irq)
+		return;
+

drivers/gpio/gpiolib-acpi.c void acpi_gpiochip_request_interrupts(st · line 233

Quoted 10 lines
drivers/gpio/gpiolib-acpi.c +10 -0
+	handle = ACPI_HANDLE(chip->dev);
+	if (!handle)
+		return;
+
+	status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
+	if (ACPI_FAILURE(status))
+		return;
+
+	list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
+		devm_free_irq(chip->dev, evt_pin->irq, evt_pin);

How about using normal request/free_irq() functions for both _EVT and non-_EVT events? Since we now need to call acpi_gpiochip_free_interrupts() anyway, I don't see the point using devm_* functions here.

Quoted 6 lines
Quoted 2 lines

Then we need to create a list of non-_EVT events, or add them to the evt_pins list.

Good point. Maybe we can add them to the evt_pins list and handle the same way as _EVT (except that we need to call _Exx and _Lxx methods instead of _EVT)?

The difference is that the evt_pins data is only needed for _EVT execution, because _EVT takes the pin argument. _Lxx/_Exx don't take arguments and there's no need to add extra data structures for them, as devm_ does what's needed.

Of course, plain request/free_irq may be used for the _EVT events only at the expense of a little more complexity in acpi_gpiochip_request_interrupts().

Thanks,
Rafael

#5 lore raw
Quoted 33 lines · Rafael J. Wysocki
Quoted 32 lines · Mika Westerberg
Quoted 30 lines · Mathias Nyman
Quoted 17 lines · Mika Westerberg
Quoted 16 lines · Rafael J. Wysocki
drivers/gpio/gpiolib-acpi.c +16 -0
+void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
+{
+	acpi_handle handle;
+	acpi_status status;
+	struct list_head *evt_pins;
+	struct acpi_gpio_evt_pin *evt_pin, *ep;
+
+	if (!chip->dev || !chip->to_irq)
+		return;
+
+	handle = ACPI_HANDLE(chip->dev);
+	if (!handle)
+		return;
+
+	status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
+	if (ACPI_FAILURE(status))
Quoted 9 lines

drivers/gpio/gpiolib-acpi.c void acpi_gpiochip_request_interrupts(st · line 233

Quoted 4 lines
drivers/gpio/gpiolib-acpi.c +4 -0
+		return;
+
+	list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
+		devm_free_irq(chip->dev, evt_pin->irq, evt_pin);

How about using normal request/free_irq() functions for both _EVT and non-_EVT events? Since we now need to call acpi_gpiochip_free_interrupts() anyway, I don't see the point using devm_* functions here.

Then we need to create a list of non-_EVT events, or add them to the evt_pins list.

Quoted 8 lines
Quoted 3 lines

Good point. Maybe we can add them to the evt_pins list and handle the same way as _EVT (except that we need to call _Exx and _Lxx methods instead of _EVT)?

The difference is that the evt_pins data is only needed for _EVT execution, because _EVT takes the pin argument. _Lxx/_Exx don't take arguments and there's no need to add extra data structures for them, as devm_ does what's needed.

OK, thanks for the explanation.

Quoted 2 lines

Of course, plain request/free_irq may be used for the _EVT events only at the expense of a little more complexity in acpi_gpiochip_request_interrupts().

I'm not sure whether it is a good thing to mix devm_ and plain request/free here. And more complexity is always bad so I guess we can stay with this implementation now.

Feel free to add my

#6 lore raw
Quoted 43 lines · Mika Westerberg
Quoted 33 lines · Rafael J. Wysocki
Quoted 32 lines · Mika Westerberg
Quoted 30 lines · Mathias Nyman
Quoted 17 lines · Mika Westerberg
Quoted 16 lines · Rafael J. Wysocki
drivers/gpio/gpiolib-acpi.c +16 -0
+void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
+{
+	acpi_handle handle;
+	acpi_status status;
+	struct list_head *evt_pins;
+	struct acpi_gpio_evt_pin *evt_pin, *ep;
+
+	if (!chip->dev || !chip->to_irq)
+		return;
+
+	handle = ACPI_HANDLE(chip->dev);
+	if (!handle)
+		return;
+
+	status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
+	if (ACPI_FAILURE(status))
Quoted 9 lines

drivers/gpio/gpiolib-acpi.c void acpi_gpiochip_request_interrupts(st · line 233

Quoted 4 lines
drivers/gpio/gpiolib-acpi.c +4 -0
+		return;
+
+	list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
+		devm_free_irq(chip->dev, evt_pin->irq, evt_pin);

How about using normal request/free_irq() functions for both _EVT and non-_EVT events? Since we now need to call acpi_gpiochip_free_interrupts() anyway, I don't see the point using devm_* functions here.

Then we need to create a list of non-_EVT events, or add them to the evt_pins list.

Quoted 8 lines
Quoted 3 lines

Good point. Maybe we can add them to the evt_pins list and handle the same way as _EVT (except that we need to call _Exx and _Lxx methods instead of _EVT)?

The difference is that the evt_pins data is only needed for _EVT execution, because _EVT takes the pin argument. _Lxx/_Exx don't take arguments and there's no need to add extra data structures for them, as devm_ does what's needed.

Quoted 7 lines

OK, thanks for the explanation.

Quoted 2 lines

Of course, plain request/free_irq may be used for the _EVT events only at the expense of a little more complexity in acpi_gpiochip_request_interrupts().

I'm not sure whether it is a good thing to mix devm_ and plain request/free here.

Yes, that was my motivation for doing it the way I did it.

Quoted 6 lines

And more complexity is always bad so I guess we can stay with this implementation now.

Feel free to add my

Cool, thanks!

I wonder if Linus or Grant have any objections, though. Guys?

The patch has more to do with ACPI than with GPIO and I think it would be good to follow the spec from the outset here, so I'd like to push it for v3.10.

Thanks,
Rafael

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
#7 lore raw
In reply to Rafael J. Wysocki
Quoted 16 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit 0d1c28a (gpiolib-acpi: Add ACPI5 event model support to gpio.) that added support for ACPI events signalled through GPIO interrupts covered only GPIO pins whose numbers are less than or equal to 255. However, there may be GPIO pins with numbers greater than 255 and the ACPI spec (ACPI 5.0, Section 5.6.5.1) requires the _EVT method to be used for handling events corresponding to those pins.

Moreover, according to the spec, _EVT is the default mechanism for handling all ACPI events signalled through GPIO interrupts, so if the _Exx/_Lxx method is not present for the given pin, _EVT should be used instead. If present, though, _Exx/_Lxx take precedence over _EVT which shouldn't be executed in that case (ACPI 5.0, Section 5.6.5.3).

Quoted 5 lines

Modify acpi_gpiochip_request_interrupts() to follow the spec as described above and add acpi_gpiochip_free_interrupts() needed to free interrupts associated with _EVT.

Patch applied with Mika's ACK, thanks!

It's not like I fully understand it, but I totally trust you guys.

Yours,
Linus Walleij

#8 lore raw
Quoted 22 lines · Linus Walleij
Quoted 20 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit 0d1c28a (gpiolib-acpi: Add ACPI5 event model support to gpio.) that added support for ACPI events signalled through GPIO interrupts covered only GPIO pins whose numbers are less than or equal to 255. However, there may be GPIO pins with numbers greater than 255 and the ACPI spec (ACPI 5.0, Section 5.6.5.1) requires the _EVT method to be used for handling events corresponding to those pins.

Moreover, according to the spec, _EVT is the default mechanism for handling all ACPI events signalled through GPIO interrupts, so if the _Exx/_Lxx method is not present for the given pin, _EVT should be used instead. If present, though, _Exx/_Lxx take precedence over _EVT which shouldn't be executed in that case (ACPI 5.0, Section 5.6.5.3).

Modify acpi_gpiochip_request_interrupts() to follow the spec as described above and add acpi_gpiochip_free_interrupts() needed to free interrupts associated with _EVT.

Quoted 5 lines
Quoted 1 line

Patch applied with Mika's ACK, thanks!

It's not like I fully understand it, but I totally trust you guys.

Well, thanks! :-)

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.