Skip to contentSkip to conversation

ACPI: PM: s2idle: Fix some systems no longer waking up from suspend

PATCH In review Hans de Goede 4 participants 8 messages Mar 29, 2020 – Apr 3, 2020

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

cover ACPI: PM: s2idle: Fix some systems no longer waking up from suspend

Hi Rafael, Andy and Darren,

I've been debugging an issue I noticed on various Cherry Trail devices this weekend. The issue is that when trying to wakeup the laptop / 2-in-1 with the USB keyboard, then it not only not wakes up, after this wakeup attempt it will also no longer wakeup through other means such as the power-button, IOW the system hangs which is not so nice.

This is also happening in 5.5 since 5.5.5 .

Here is the explanation of the problem from the commit message of the first patch:

###

Since commit fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system") the SCI triggering without there being a wakeup cause recognized by the ACPI sleep code will no longer wakeup the system.

This works as intended, but this is a problem for devices where the SCI is shared with another device which is also a wakeup source.

In the past these from the pov of the ACPI sleep code spurious SCIs would still cause a wakeup so the wakeup from the device sharing the interrupt would actually wakeup the system. This now no longer works.

This is a problem on e.g. Bay Trail-T and Cherry Trail devices where some peripherals (typically the XHCI controller) can signal a Power Management Event (PME) to the Power Management Controller (PMC) to wakeup the system, this uses the same interrupt as the SCI. These wakeups are handled through a special INT0002 ACPI device which checks for events in the GPE0a_STS for this and takes care of acking the PME so that the shared interrupt stops triggering.

The change to the ACPI sleep code to ignore the spurious SCI, causes the system to no longer wakeup on these PME events. To make things worse this means that the INT0002 device driver interrupt handler will no longer run, causing the PME to not get cleared and resulting in the system hanging. Trying to wakeup the system after such a PME through e.g. the power button no longer works.

###

The first patch in this series adds an acpi_s2idle_register_wake_callback() function the ACPI sleep code which registers a callback to be called from acpi_s2idle_wake() and when the callback returns true, return true from acpi_s2idle_wake().

The second patch in this series makes the INT0002 driver use this mechanism to check the GPE0a_STS register from acpi_s2idle_wake() and to tell the system to wakeup if a PME is signaled in the register.

This is the cleanest solution I could come up with. I hope it is to your liking.

Since the INT0002 driver change depends on the changes from the first patch I suggest that we merge both patches to Rafael's tree. Andy or Darren, may we have your Acked-by for that?

Regards,

Hans

#1 lore raw

PATCH 1/2 ACPI: PM: Add acpi_s2idle_register_wake_callback()

Since commit fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system") the SCI triggering without there being a wakeup cause recognized by the ACPI sleep code will no longer wakeup the system.

This works as intended, but this is a problem for devices where the SCI is shared with another device which is also a wakeup source.

In the past these, from the pov of the ACPI sleep code, spurious SCIs would still cause a wakeup so the wakeup from the device sharing the interrupt would actually wakeup the system. This now no longer works.

This is a problem on e.g. Bay Trail-T and Cherry Trail devices where some peripherals (typically the XHCI controller) can signal a Power Management Event (PME) to the Power Management Controller (PMC) to wakeup the system, this uses the same interrupt as the SCI. These wakeups are handled through a special INT0002 ACPI device which checks for events in the GPE0a_STS for this and takes care of acking the PME so that the shared interrupt stops triggering.

The change to the ACPI sleep code to ignore the spurious SCI, causes the system to no longer wakeup on these PME events. To make things worse this means that the INT0002 device driver interrupt handler will no longer run, causing the PME to not get cleared and resulting in the system hanging. Trying to wakeup the system after such a PME through e.g. the power button no longer works.

Add an acpi_s2idle_register_wake_callback() function which registers a callback to be called from acpi_s2idle_wake() and when the callback returns true, return true from acpi_s2idle_wake().

The INT0002 driver will use this mechanism to check the GPE0a_STS register from acpi_s2idle_wake() and to tell the system to wakeup if a PME is signaled in the register.

Fixes fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system")
Cc 5.4+ <[email protected]> # 5.4+
---
 drivers/acpi/sleep.c | 70 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  7 +++++
 2 files changed, 77 insertions(+)
drivers/acpi/sleep.c +70 -0
index e5f95922bc21..e360e51afa8e 100644
@@ -943,6 +943,65 @@ static struct acpi_scan_handler lps0_handler = {
 	.attach = lps0_device_attach,
 };
 
+struct s2idle_wake_callback {
+	struct list_head list;
+	bool (*function)(void *data);
+	void *user_data;
+};
+
+static LIST_HEAD(s2idle_wake_callback_head);
+static DEFINE_MUTEX(s2idle_wake_callback_mutex);
+
+/*
+ * Drivers which may share an IRQ with the SCI can use this to register
+ * a callback which returns true when the device they are managing wants
+ * to trigger a wakeup.
+ */
+int acpi_s2idle_register_wake_callback(
+	int wake_irq, bool (*function)(void *data), void *user_data)
+{
+	struct s2idle_wake_callback *callback;
+
+	/*
+	 * If the device is not sharing its IRQ with the SCI, there is no
+	 * need to register the callback.
+	 */
+	if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
+		return 0;
+
+	callback = kmalloc(sizeof(*callback), GFP_KERNEL);
+	if (!callback)
+		return -ENOMEM;
+
+	callback->function = function;
+	callback->user_data = user_data;
+
+	mutex_lock(&s2idle_wake_callback_mutex);
+	list_add(&callback->list, &s2idle_wake_callback_head);
+	mutex_unlock(&s2idle_wake_callback_mutex);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_register_wake_callback);
+
+void acpi_s2idle_unregister_wake_callback(
+	bool (*function)(void *data), void *user_data)
+{
+	struct s2idle_wake_callback *cb;
+
+	mutex_lock(&s2idle_wake_callback_mutex);
+	list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+		if (cb->function == function &&
+		    cb->user_data == user_data) {
+			list_del(&cb->list);
+			kfree(cb);
+			break;
+		}
+	}
+	mutex_unlock(&s2idle_wake_callback_mutex);
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_unregister_wake_callback);
+
 static int acpi_s2idle_begin(void)
 {
 	acpi_scan_lock_acquire();
@@ -992,6 +1051,8 @@ static void acpi_s2idle_sync(void)
 
 static bool acpi_s2idle_wake(void)
 {
+	struct s2idle_wake_callback *cb;
+
 	if (!acpi_sci_irq_valid())
 		return pm_wakeup_pending();
 
@@ -1025,6 +1086,15 @@ static bool acpi_s2idle_wake(void)
 		if (acpi_any_gpe_status_set() && !acpi_ec_dispatch_gpe())
 			return true;
 
+		/*
+		 * Check callbacks registered by drivers sharing the SCI.
+		 * Note no need to lock, nothing else is running.
+		 */
+		list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+			if (cb->function(cb->user_data))
+				return true;
+		}
+
 		/*
 		 * Cancel the wakeup and process all pending events in case
 		 * there are any wakeup ones in there.
include/linux/acpi.h +7 -0
index 0f24d701fbdc..9f06e1dc79c1 100644
@@ -488,6 +488,13 @@ void __init acpi_nvs_nosave_s3(void);
 void __init acpi_sleep_no_blacklist(void);
 #endif /* CONFIG_PM_SLEEP */
 
+#ifdef CONFIG_ACPI_SYSTEM_POWER_STATES_SUPPORT
+int acpi_s2idle_register_wake_callback(
+	int wake_irq, bool (*function)(void *data), void *user_data);
+void acpi_s2idle_unregister_wake_callback(
+	bool (*function)(void *data), void *user_data);
+#endif
+
 struct acpi_osc_context {
 	char *uuid_str;			/* UUID string */
 	int rev;
Signature · 1 line
2.26.0
#2 lore raw

Re: [PATCH 5.6 regression fix 1/2] ACPI: PM: Add acpi_s2idle_register_wake_callback()

Quoted 31 lines · Hans de Goede

Since commit fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system") the SCI triggering without there being a wakeup cause recognized by the ACPI sleep code will no longer wakeup the system.

This works as intended, but this is a problem for devices where the SCI is shared with another device which is also a wakeup source.

In the past these, from the pov of the ACPI sleep code, spurious SCIs would still cause a wakeup so the wakeup from the device sharing the interrupt would actually wakeup the system. This now no longer works.

This is a problem on e.g. Bay Trail-T and Cherry Trail devices where some peripherals (typically the XHCI controller) can signal a Power Management Event (PME) to the Power Management Controller (PMC) to wakeup the system, this uses the same interrupt as the SCI. These wakeups are handled through a special INT0002 ACPI device which checks for events in the GPE0a_STS for this and takes care of acking the PME so that the shared interrupt stops triggering.

The change to the ACPI sleep code to ignore the spurious SCI, causes the system to no longer wakeup on these PME events. To make things worse this means that the INT0002 device driver interrupt handler will no longer run, causing the PME to not get cleared and resulting in the system hanging. Trying to wakeup the system after such a PME through e.g. the power button no longer works.

Add an acpi_s2idle_register_wake_callback() function which registers a callback to be called from acpi_s2idle_wake() and when the callback returns true, return true from acpi_s2idle_wake().

Quoted 7 lines

The INT0002 driver will use this mechanism to check the GPE0a_STS register from acpi_s2idle_wake() and to tell the system to wakeup if a PME is signaled in the register.

Fixes fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system")
Cc 5.4+ <[email protected]> # 5.4+

I generally agree with the approach, but I would make some, mostly cosmetic, changes.

First off, I'd put the new code into drivers/acpi/wakeup.c.

I'd export one function from there to be called from acpi_s2idle_wake() and the install/uninstall routines for the users.

Quoted 10 lines
---
 drivers/acpi/sleep.c | 70 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/acpi.h |  7 +++++
 2 files changed, 77 insertions(+)
drivers/acpi/sleep.c +0 -0
index e5f95922bc21..e360e51afa8e 100644

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 946

Quoted 4 lines
drivers/acpi/sleep.c +1 -0
@@ -943,6 +943,65 @@ static struct acpi_scan_handler lps0_handler = {
        .attach = lps0_device_attach,
 };
 
+struct s2idle_wake_callback {

I'd call this acpi_wakeup_handler.

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 947

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       struct list_head list;

list_node?

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 948

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       bool (*function)(void *data);

bool (*wakeup)(void *context)?

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 949

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       void *user_data;

context?

Quoted 69 lines
drivers/acpi/sleep.c +57 -0
+};
+
+static LIST_HEAD(s2idle_wake_callback_head);
+static DEFINE_MUTEX(s2idle_wake_callback_mutex);
+
+/*
+ * Drivers which may share an IRQ with the SCI can use this to register
+ * a callback which returns true when the device they are managing wants
+ * to trigger a wakeup.
+ */
+int acpi_s2idle_register_wake_callback(
+       int wake_irq, bool (*function)(void *data), void *user_data)
+{
+       struct s2idle_wake_callback *callback;
+
+       /*
+        * If the device is not sharing its IRQ with the SCI, there is no
+        * need to register the callback.
+        */
+       if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
+               return 0;
+
+       callback = kmalloc(sizeof(*callback), GFP_KERNEL);
+       if (!callback)
+               return -ENOMEM;
+
+       callback->function = function;
+       callback->user_data = user_data;
+
+       mutex_lock(&s2idle_wake_callback_mutex);
+       list_add(&callback->list, &s2idle_wake_callback_head);
+       mutex_unlock(&s2idle_wake_callback_mutex);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_register_wake_callback);
+
+void acpi_s2idle_unregister_wake_callback(
+       bool (*function)(void *data), void *user_data)
+{
+       struct s2idle_wake_callback *cb;
+
+       mutex_lock(&s2idle_wake_callback_mutex);
+       list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+               if (cb->function == function &&
+                   cb->user_data == user_data) {
+                       list_del(&cb->list);
+                       kfree(cb);
+                       break;
+               }
+       }
+       mutex_unlock(&s2idle_wake_callback_mutex);
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_unregister_wake_callback);
+
 static int acpi_s2idle_begin(void)
 {
        acpi_scan_lock_acquire();
@@ -992,6 +1051,8 @@ static void acpi_s2idle_sync(void)
 
 static bool acpi_s2idle_wake(void)
 {
+       struct s2idle_wake_callback *cb;
+
        if (!acpi_sci_irq_valid())
                return pm_wakeup_pending();
 
@@ -1025,6 +1086,15 @@ static bool acpi_s2idle_wake(void)
                if (acpi_any_gpe_status_set() && !acpi_ec_dispatch_gpe())

drivers/acpi/sleep.c static bool acpi_s2idle_wake(void) · line 1096

Quoted 10 lines
drivers/acpi/sleep.c +8 -0
                        return true;
 
+               /*
+                * Check callbacks registered by drivers sharing the SCI.
+                * Note no need to lock, nothing else is running.
+                */
+               list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+                       if (cb->function(cb->user_data))
+                               return true;
+               }

AFAICS this needs to be done in acpi_s2idle_restore() too to clear the status bits in case one of these wakeup sources triggers along with a GPE or a fixed event and the other one wins the race.

Quoted 13 lines
drivers/acpi/sleep.c +1 -0
+
                /*
                 * Cancel the wakeup and process all pending events in case
                 * there are any wakeup ones in there.
include/linux/acpi.h +1 -0
index 0f24d701fbdc..9f06e1dc79c1 100644
@@ -488,6 +488,13 @@ void __init acpi_nvs_nosave_s3(void);
 void __init acpi_sleep_no_blacklist(void);
 #endif /* CONFIG_PM_SLEEP */
 
+#ifdef CONFIG_ACPI_SYSTEM_POWER_STATES_SUPPORT

include/linux/acpi.h void __init acpi_nvs_nosave_s3(void);

Quoted 10 lines
include/linux/acpi.h +6 -1
+int acpi_s2idle_register_wake_callback(
+       int wake_irq, bool (*function)(void *data), void *user_data);
+void acpi_s2idle_unregister_wake_callback(
+       bool (*function)(void *data), void *user_data);
+#endif
+
 struct acpi_osc_context {
        char *uuid_str;                 /* UUID string */
        int rev;
--

Thanks!

#3 lore raw

Re: [PATCH 5.6 regression fix 1/2] ACPI: PM: Add acpi_s2idle_register_wake_callback()

Hi,

Quoted 40 lines · Rafael J. Wysocki
Quoted 31 lines · Hans de Goede

Since commit fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system") the SCI triggering without there being a wakeup cause recognized by the ACPI sleep code will no longer wakeup the system.

This works as intended, but this is a problem for devices where the SCI is shared with another device which is also a wakeup source.

In the past these, from the pov of the ACPI sleep code, spurious SCIs would still cause a wakeup so the wakeup from the device sharing the interrupt would actually wakeup the system. This now no longer works.

This is a problem on e.g. Bay Trail-T and Cherry Trail devices where some peripherals (typically the XHCI controller) can signal a Power Management Event (PME) to the Power Management Controller (PMC) to wakeup the system, this uses the same interrupt as the SCI. These wakeups are handled through a special INT0002 ACPI device which checks for events in the GPE0a_STS for this and takes care of acking the PME so that the shared interrupt stops triggering.

The change to the ACPI sleep code to ignore the spurious SCI, causes the system to no longer wakeup on these PME events. To make things worse this means that the INT0002 device driver interrupt handler will no longer run, causing the PME to not get cleared and resulting in the system hanging. Trying to wakeup the system after such a PME through e.g. the power button no longer works.

Add an acpi_s2idle_register_wake_callback() function which registers a callback to be called from acpi_s2idle_wake() and when the callback returns true, return true from acpi_s2idle_wake().

Quoted 7 lines

The INT0002 driver will use this mechanism to check the GPE0a_STS register from acpi_s2idle_wake() and to tell the system to wakeup if a PME is signaled in the register.

Fixes fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system")
Cc 5.4+ <[email protected]> # 5.4+
Quoted 7 lines

I generally agree with the approach, but I would make some, mostly cosmetic, changes.

First off, I'd put the new code into drivers/acpi/wakeup.c.

I'd export one function from there to be called from acpi_s2idle_wake() and the install/uninstall routines for the users.

Ok.

Quoted 21 lines
Quoted 10 lines
---
  drivers/acpi/sleep.c | 70 ++++++++++++++++++++++++++++++++++++++++++++
  include/linux/acpi.h |  7 +++++
  2 files changed, 77 insertions(+)
drivers/acpi/sleep.c +0 -0
index e5f95922bc21..e360e51afa8e 100644

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 946

Quoted 4 lines
drivers/acpi/sleep.c +1 -0
@@ -943,6 +943,65 @@ static struct acpi_scan_handler lps0_handler = {
         .attach = lps0_device_attach,
  };
 
+struct s2idle_wake_callback {

I'd call this acpi_wakeup_handler.

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 947

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       struct list_head list;

list_node?

Quoted 7 lines

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 948

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       bool (*function)(void *data);

bool (*wakeup)(void *context)?

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 949

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       void *user_data;

context?

Sure (for all of the above).

Quoted 72 lines
Quoted 72 lines
drivers/acpi/sleep.c +57 -0
+};
+
+static LIST_HEAD(s2idle_wake_callback_head);
+static DEFINE_MUTEX(s2idle_wake_callback_mutex);
+
+/*
+ * Drivers which may share an IRQ with the SCI can use this to register
+ * a callback which returns true when the device they are managing wants
+ * to trigger a wakeup.
+ */
+int acpi_s2idle_register_wake_callback(
+       int wake_irq, bool (*function)(void *data), void *user_data)
+{
+       struct s2idle_wake_callback *callback;
+
+       /*
+        * If the device is not sharing its IRQ with the SCI, there is no
+        * need to register the callback.
+        */
+       if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
+               return 0;
+
+       callback = kmalloc(sizeof(*callback), GFP_KERNEL);
+       if (!callback)
+               return -ENOMEM;
+
+       callback->function = function;
+       callback->user_data = user_data;
+
+       mutex_lock(&s2idle_wake_callback_mutex);
+       list_add(&callback->list, &s2idle_wake_callback_head);
+       mutex_unlock(&s2idle_wake_callback_mutex);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_register_wake_callback);
+
+void acpi_s2idle_unregister_wake_callback(
+       bool (*function)(void *data), void *user_data)
+{
+       struct s2idle_wake_callback *cb;
+
+       mutex_lock(&s2idle_wake_callback_mutex);
+       list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+               if (cb->function == function &&
+                   cb->user_data == user_data) {
+                       list_del(&cb->list);
+                       kfree(cb);
+                       break;
+               }
+       }
+       mutex_unlock(&s2idle_wake_callback_mutex);
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_unregister_wake_callback);
+
  static int acpi_s2idle_begin(void)
  {
         acpi_scan_lock_acquire();
@@ -992,6 +1051,8 @@ static void acpi_s2idle_sync(void)
 
  static bool acpi_s2idle_wake(void)
  {
+       struct s2idle_wake_callback *cb;
+
         if (!acpi_sci_irq_valid())
                 return pm_wakeup_pending();
 
@@ -1025,6 +1086,15 @@ static bool acpi_s2idle_wake(void)
                 if (acpi_any_gpe_status_set() && !acpi_ec_dispatch_gpe())
                         return true;
 
Quoted 12 lines

drivers/acpi/sleep.c static bool acpi_s2idle_wake(void) · line 1096

Quoted 8 lines
drivers/acpi/sleep.c +8 -0
+               /*
+                * Check callbacks registered by drivers sharing the SCI.
+                * Note no need to lock, nothing else is running.
+                */
+               list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+                       if (cb->function(cb->user_data))
+                               return true;
+               }

AFAICS this needs to be done in acpi_s2idle_restore() too to clear the status bits in case one of these wakeup sources triggers along with a GPE or a fixed event and the other one wins the race.

The "wakeup" callback does not actually clear the interrupt source, just like for normal interrupts it relies on the actual interrupt handling (which at this point is still suspended) to do this.

All the wakeup callback does is check if the flag which the shared IRQ handler checks to see if it should handle the IRQ (so if it should return IRQ_HANDLED vs IRQ_NONE) is set and if the flag is set so that the actual IRQ handler would return IRQ_HANDLED (IOW the shared IRQ is relevant for the device) then it returns true from the wakeup callback.

The actual handling of the IRQ and clearing of the PME status bit is then done by normal IRQ handling.

The idea here is to have the IRQ be handled as any other IRQ even though it happens to be shared with the SCI, so in essence we want to behave as if the first if check here:

while (pm_wakeup_pending()) {
/*
* If IRQD_WAKEUP_ARMED is set for the SCI at this point, the
* SCI has not triggered while suspended, so bail out (the
* wakeup is pending anyway and the SCI is not the source of
* it).
*/
if (irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq)))
return true;

Is triggering, the idea here is that yes the SCI IRQ is no longer armed, but the cause for that is (*) not an actual SCI but another device which shares the IRQ line. If any other IRQ is the cause for the wakeup then we would exit the loop here, so in this case we want to treat the non SCI IRQ event on the shared line the same as any other IRQ.

*) potentially, there could be multiple causes

Does this make sense ? Note you know this code a lot better then me, so you may be right we need to do something extra in s2idle_restore here but ATM I'm not seeing what we should do there. As I've tried to explain the wakeup callback only checks for events, it does not actually process them and as such also does not ack them / clear any flags.

Regards,

Hans

Quoted 27 lines
Quoted 14 lines
drivers/acpi/sleep.c +1 -0
+
                 /*
                  * Cancel the wakeup and process all pending events in case
                  * there are any wakeup ones in there.
include/linux/acpi.h +1 -0
index 0f24d701fbdc..9f06e1dc79c1 100644
@@ -488,6 +488,13 @@ void __init acpi_nvs_nosave_s3(void);
  void __init acpi_sleep_no_blacklist(void);
  #endif /* CONFIG_PM_SLEEP */
 
+#ifdef CONFIG_ACPI_SYSTEM_POWER_STATES_SUPPORT

include/linux/acpi.h void __init acpi_nvs_nosave_s3(void);

Quoted 10 lines
include/linux/acpi.h +6 -1
+int acpi_s2idle_register_wake_callback(
+       int wake_irq, bool (*function)(void *data), void *user_data);
+void acpi_s2idle_unregister_wake_callback(
+       bool (*function)(void *data), void *user_data);
+#endif
+
  struct acpi_osc_context {
         char *uuid_str;                 /* UUID string */
         int rev;
--

Thanks!

#4 lore raw

Re: [PATCH 5.6 regression fix 1/2] ACPI: PM: Add acpi_s2idle_register_wake_callback()

Quoted 169 lines · CEST Hans de Goede

Hi,

Quoted 40 lines · Rafael J. Wysocki
Quoted 31 lines · Hans de Goede

Since commit fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system") the SCI triggering without there being a wakeup cause recognized by the ACPI sleep code will no longer wakeup the system.

This works as intended, but this is a problem for devices where the SCI is shared with another device which is also a wakeup source.

In the past these, from the pov of the ACPI sleep code, spurious SCIs would still cause a wakeup so the wakeup from the device sharing the interrupt would actually wakeup the system. This now no longer works.

This is a problem on e.g. Bay Trail-T and Cherry Trail devices where some peripherals (typically the XHCI controller) can signal a Power Management Event (PME) to the Power Management Controller (PMC) to wakeup the system, this uses the same interrupt as the SCI. These wakeups are handled through a special INT0002 ACPI device which checks for events in the GPE0a_STS for this and takes care of acking the PME so that the shared interrupt stops triggering.

The change to the ACPI sleep code to ignore the spurious SCI, causes the system to no longer wakeup on these PME events. To make things worse this means that the INT0002 device driver interrupt handler will no longer run, causing the PME to not get cleared and resulting in the system hanging. Trying to wakeup the system after such a PME through e.g. the power button no longer works.

Add an acpi_s2idle_register_wake_callback() function which registers a callback to be called from acpi_s2idle_wake() and when the callback returns true, return true from acpi_s2idle_wake().

Quoted 7 lines

The INT0002 driver will use this mechanism to check the GPE0a_STS register from acpi_s2idle_wake() and to tell the system to wakeup if a PME is signaled in the register.

Fixes fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system")
Cc 5.4+ <[email protected]> # 5.4+
Quoted 7 lines

I generally agree with the approach, but I would make some, mostly cosmetic, changes.

First off, I'd put the new code into drivers/acpi/wakeup.c.

I'd export one function from there to be called from acpi_s2idle_wake() and the install/uninstall routines for the users.

Ok.

Quoted 21 lines
Quoted 10 lines
---
  drivers/acpi/sleep.c | 70 ++++++++++++++++++++++++++++++++++++++++++++
  include/linux/acpi.h |  7 +++++
  2 files changed, 77 insertions(+)
drivers/acpi/sleep.c +0 -0
index e5f95922bc21..e360e51afa8e 100644

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 946

Quoted 4 lines
drivers/acpi/sleep.c +1 -0
@@ -943,6 +943,65 @@ static struct acpi_scan_handler lps0_handler = {
         .attach = lps0_device_attach,
  };
 
+struct s2idle_wake_callback {

I'd call this acpi_wakeup_handler.

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 947

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       struct list_head list;

list_node?

Quoted 7 lines

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 948

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       bool (*function)(void *data);

bool (*wakeup)(void *context)?

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 949

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       void *user_data;

context?

Sure (for all of the above).

Quoted 72 lines
Quoted 72 lines
drivers/acpi/sleep.c +57 -0
+};
+
+static LIST_HEAD(s2idle_wake_callback_head);
+static DEFINE_MUTEX(s2idle_wake_callback_mutex);
+
+/*
+ * Drivers which may share an IRQ with the SCI can use this to register
+ * a callback which returns true when the device they are managing wants
+ * to trigger a wakeup.
+ */
+int acpi_s2idle_register_wake_callback(
+       int wake_irq, bool (*function)(void *data), void *user_data)
+{
+       struct s2idle_wake_callback *callback;
+
+       /*
+        * If the device is not sharing its IRQ with the SCI, there is no
+        * need to register the callback.
+        */
+       if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
+               return 0;
+
+       callback = kmalloc(sizeof(*callback), GFP_KERNEL);
+       if (!callback)
+               return -ENOMEM;
+
+       callback->function = function;
+       callback->user_data = user_data;
+
+       mutex_lock(&s2idle_wake_callback_mutex);
+       list_add(&callback->list, &s2idle_wake_callback_head);
+       mutex_unlock(&s2idle_wake_callback_mutex);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_register_wake_callback);
+
+void acpi_s2idle_unregister_wake_callback(
+       bool (*function)(void *data), void *user_data)
+{
+       struct s2idle_wake_callback *cb;
+
+       mutex_lock(&s2idle_wake_callback_mutex);
+       list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+               if (cb->function == function &&
+                   cb->user_data == user_data) {
+                       list_del(&cb->list);
+                       kfree(cb);
+                       break;
+               }
+       }
+       mutex_unlock(&s2idle_wake_callback_mutex);
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_unregister_wake_callback);
+
  static int acpi_s2idle_begin(void)
  {
         acpi_scan_lock_acquire();
@@ -992,6 +1051,8 @@ static void acpi_s2idle_sync(void)
 
  static bool acpi_s2idle_wake(void)
  {
+       struct s2idle_wake_callback *cb;
+
         if (!acpi_sci_irq_valid())
                 return pm_wakeup_pending();
 
@@ -1025,6 +1086,15 @@ static bool acpi_s2idle_wake(void)
                 if (acpi_any_gpe_status_set() && !acpi_ec_dispatch_gpe())
                         return true;
 
Quoted 12 lines

drivers/acpi/sleep.c static bool acpi_s2idle_wake(void) · line 1096

Quoted 8 lines
drivers/acpi/sleep.c +8 -0
+               /*
+                * Check callbacks registered by drivers sharing the SCI.
+                * Note no need to lock, nothing else is running.
+                */
+               list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+                       if (cb->function(cb->user_data))
+                               return true;
+               }

AFAICS this needs to be done in acpi_s2idle_restore() too to clear the status bits in case one of these wakeup sources triggers along with a GPE or a fixed event and the other one wins the race.

Quoted 3 lines

The "wakeup" callback does not actually clear the interrupt source, just like for normal interrupts it relies on the actual interrupt handling (which at this point is still suspended) to do this.

Of course, you are right, sorry for the confusion.

What I meant was that the interrupt handler needed to run in acpi_s2idle_restore(), but that should be taken care of the acpi_os_wait_events_complete() in there which synchronizes the SCI among other things.

Thanks!

#5 lore raw

Re: [PATCH 5.6 regression fix 1/2] ACPI: PM: Add acpi_s2idle_register_wake_callback()

Hi,

Quoted 174 lines · Rafael J. Wysocki
Quoted 169 lines · CEST Hans de Goede

Hi,

Quoted 40 lines · Rafael J. Wysocki
Quoted 31 lines · Hans de Goede

Since commit fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system") the SCI triggering without there being a wakeup cause recognized by the ACPI sleep code will no longer wakeup the system.

This works as intended, but this is a problem for devices where the SCI is shared with another device which is also a wakeup source.

In the past these, from the pov of the ACPI sleep code, spurious SCIs would still cause a wakeup so the wakeup from the device sharing the interrupt would actually wakeup the system. This now no longer works.

This is a problem on e.g. Bay Trail-T and Cherry Trail devices where some peripherals (typically the XHCI controller) can signal a Power Management Event (PME) to the Power Management Controller (PMC) to wakeup the system, this uses the same interrupt as the SCI. These wakeups are handled through a special INT0002 ACPI device which checks for events in the GPE0a_STS for this and takes care of acking the PME so that the shared interrupt stops triggering.

The change to the ACPI sleep code to ignore the spurious SCI, causes the system to no longer wakeup on these PME events. To make things worse this means that the INT0002 device driver interrupt handler will no longer run, causing the PME to not get cleared and resulting in the system hanging. Trying to wakeup the system after such a PME through e.g. the power button no longer works.

Add an acpi_s2idle_register_wake_callback() function which registers a callback to be called from acpi_s2idle_wake() and when the callback returns true, return true from acpi_s2idle_wake().

Quoted 7 lines

The INT0002 driver will use this mechanism to check the GPE0a_STS register from acpi_s2idle_wake() and to tell the system to wakeup if a PME is signaled in the register.

Fixes fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system")
Cc 5.4+ <[email protected]> # 5.4+
Quoted 7 lines

I generally agree with the approach, but I would make some, mostly cosmetic, changes.

First off, I'd put the new code into drivers/acpi/wakeup.c.

I'd export one function from there to be called from acpi_s2idle_wake() and the install/uninstall routines for the users.

Ok.

Quoted 21 lines
Quoted 10 lines
---
   drivers/acpi/sleep.c | 70 ++++++++++++++++++++++++++++++++++++++++++++
   include/linux/acpi.h |  7 +++++
   2 files changed, 77 insertions(+)
drivers/acpi/sleep.c +0 -0
index e5f95922bc21..e360e51afa8e 100644

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 946

Quoted 4 lines
drivers/acpi/sleep.c +1 -0
@@ -943,6 +943,65 @@ static struct acpi_scan_handler lps0_handler = {
          .attach = lps0_device_attach,
   };
 
+struct s2idle_wake_callback {

I'd call this acpi_wakeup_handler.

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 947

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       struct list_head list;

list_node?

Quoted 7 lines

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 948

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       bool (*function)(void *data);

bool (*wakeup)(void *context)?

drivers/acpi/sleep.c static struct acpi_scan_handler lps0_handler = { · line 949

Quoted 1 line
drivers/acpi/sleep.c +1 -0
+       void *user_data;

context?

Sure (for all of the above).

Quoted 72 lines
Quoted 72 lines
drivers/acpi/sleep.c +57 -0
+};
+
+static LIST_HEAD(s2idle_wake_callback_head);
+static DEFINE_MUTEX(s2idle_wake_callback_mutex);
+
+/*
+ * Drivers which may share an IRQ with the SCI can use this to register
+ * a callback which returns true when the device they are managing wants
+ * to trigger a wakeup.
+ */
+int acpi_s2idle_register_wake_callback(
+       int wake_irq, bool (*function)(void *data), void *user_data)
+{
+       struct s2idle_wake_callback *callback;
+
+       /*
+        * If the device is not sharing its IRQ with the SCI, there is no
+        * need to register the callback.
+        */
+       if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
+               return 0;
+
+       callback = kmalloc(sizeof(*callback), GFP_KERNEL);
+       if (!callback)
+               return -ENOMEM;
+
+       callback->function = function;
+       callback->user_data = user_data;
+
+       mutex_lock(&s2idle_wake_callback_mutex);
+       list_add(&callback->list, &s2idle_wake_callback_head);
+       mutex_unlock(&s2idle_wake_callback_mutex);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_register_wake_callback);
+
+void acpi_s2idle_unregister_wake_callback(
+       bool (*function)(void *data), void *user_data)
+{
+       struct s2idle_wake_callback *cb;
+
+       mutex_lock(&s2idle_wake_callback_mutex);
+       list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+               if (cb->function == function &&
+                   cb->user_data == user_data) {
+                       list_del(&cb->list);
+                       kfree(cb);
+                       break;
+               }
+       }
+       mutex_unlock(&s2idle_wake_callback_mutex);
+}
+EXPORT_SYMBOL_GPL(acpi_s2idle_unregister_wake_callback);
+
   static int acpi_s2idle_begin(void)
   {
          acpi_scan_lock_acquire();
@@ -992,6 +1051,8 @@ static void acpi_s2idle_sync(void)
 
   static bool acpi_s2idle_wake(void)
   {
+       struct s2idle_wake_callback *cb;
+
          if (!acpi_sci_irq_valid())
                  return pm_wakeup_pending();
 
@@ -1025,6 +1086,15 @@ static bool acpi_s2idle_wake(void)
                  if (acpi_any_gpe_status_set() && !acpi_ec_dispatch_gpe())
                          return true;
 
Quoted 12 lines

drivers/acpi/sleep.c static bool acpi_s2idle_wake(void) · line 1096

Quoted 8 lines
drivers/acpi/sleep.c +8 -0
+               /*
+                * Check callbacks registered by drivers sharing the SCI.
+                * Note no need to lock, nothing else is running.
+                */
+               list_for_each_entry(cb, &s2idle_wake_callback_head, list) {
+                       if (cb->function(cb->user_data))
+                               return true;
+               }

AFAICS this needs to be done in acpi_s2idle_restore() too to clear the status bits in case one of these wakeup sources triggers along with a GPE or a fixed event and the other one wins the race.

Quoted 3 lines

The "wakeup" callback does not actually clear the interrupt source, just like for normal interrupts it relies on the actual interrupt handling (which at this point is still suspended) to do this.

Quoted 5 lines

Of course, you are right, sorry for the confusion.

What I meant was that the interrupt handler needed to run in acpi_s2idle_restore(), but that should be taken care of the acpi_os_wait_events_complete() in there which synchronizes the SCI among other things.

Ok, I've prepared a v2 with the other discussed changes. I'll give it a quick test and then send it out.

Regards,

Hans

#6 lore raw

PATCH 2/2 platform/x86: intel_int0002_vgpio: Use acpi_s2idle_register_wake_callback

In reply to Hans de Goede

The Power Management Events (PMEs) the INT0002 driver listens for get signalled by the Power Management Controller (PMC) using the same IRQ as used for the ACPI SCI.

Since commit fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system") the SCI triggering without there being a wakeup cause recognized by the ACPI sleep code will no longer wakeup the system.

This breaks PMEs / wakeups signalled to the INT0002 driver, the system never leaves the s2idle_loop() now.

Use acpi_s2idle_register_wake_callback to register a function which checks the GPE0a_STS register for a PME and trigger a wakeup when a PME has been signalled.

With this new mechanism the pm_wakeup_hard_event() call is no longer necessary, so remove it and also remove the matching device_init_wakeup() calls.

Fixes fdde0ff8590b ("ACPI: PM: s2idle: Prevent spurious SCIs from waking up the system")
Cc 5.4+ <[email protected]> # 5.4+
---
 drivers/platform/x86/intel_int0002_vgpio.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/platform/x86/intel_int0002_vgpio.c +10 -4
index f14e2c5f9da5..3c70694188fe 100644
@@ -122,11 +122,17 @@ static irqreturn_t int0002_irq(int irq, void *data)
 	generic_handle_irq(irq_find_mapping(chip->irq.domain,
 					    GPE0A_PME_B0_VIRT_GPIO_PIN));
 
-	pm_wakeup_hard_event(chip->parent);
-
 	return IRQ_HANDLED;
 }
 
+static bool int0002_check_wake(void *data)
+{
+	u32 gpe_sts_reg;
+
+	gpe_sts_reg = inl(GPE0A_STS_PORT);
+	return (gpe_sts_reg & GPE0A_PME_B0_STS_BIT);
+}
+
 static struct irq_chip int0002_byt_irqchip = {
 	.name			= DRV_NAME,
 	.irq_ack		= int0002_irq_ack,
@@ -220,13 +226,13 @@ static int int0002_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	device_init_wakeup(dev, true);
+	acpi_s2idle_register_wake_callback(irq, int0002_check_wake, NULL);
 	return 0;
 }
 
 static int int0002_remove(struct platform_device *pdev)
 {
-	device_init_wakeup(&pdev->dev, false);
+	acpi_s2idle_unregister_wake_callback(int0002_check_wake, NULL);
 	return 0;
 }
 
Signature · 1 line
2.26.0
#7 lore raw

Re: [PATCH 5.6 regression fix 2/2] platform/x86: intel_int0002_vgpio: Use acpi_s2idle_register_wake_callback

I love your patch! Yet something to improve:

Hi Hans,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.6]
[cannot apply to pm/linux-next platform-drivers-x86/for-next next-20200327] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Hans-de-Goede/ACPI-PM-s2idle-Fix-some-systems-no-longer-waking-up-from-suspend/20200330-063537
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 7111951b8d4973bda27ff663f2cf18b663d15b48
config: i386-randconfig-a001-20200329 (attached as .config)
compiler: gcc-6 (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

ld: drivers/platform/x86/intel_int0002_vgpio.o: in function `int0002_remove':

>> drivers/platform/x86/intel_int0002_vgpio.c:235: undefined reference to `acpi_s2idle_unregister_wake_callback'
   ld: drivers/platform/x86/intel_int0002_vgpio.o: in function `int0002_probe':
>> drivers/platform/x86/intel_int0002_vgpio.c:229: undefined reference to `acpi_s2idle_register_wake_callback'

vim +235 drivers/platform/x86/intel_int0002_vgpio.c

168
169 static int int0002_probe(struct platform_device *pdev)
170 {
171 struct device *dev = &pdev->dev;
172 const struct x86_cpu_id *cpu_id;
173 struct gpio_chip *chip;
174 struct gpio_irq_chip *girq;
175 int irq, ret;
176
177 /* Menlow has a different INT0002 device? <sigh> */
178 cpu_id = x86_match_cpu(int0002_cpu_ids);
179 if (!cpu_id)
180 return -ENODEV;
181
182 irq = platform_get_irq(pdev, 0);
183 if (irq < 0)
184 return irq;
185
186 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
187 if (!chip)
188 return -ENOMEM;
189
190 chip->label = DRV_NAME;
191 chip->parent = dev;
192 chip->owner = THIS_MODULE;
193 chip->get = int0002_gpio_get;
194 chip->set = int0002_gpio_set;
195 chip->direction_input = int0002_gpio_get;
196 chip->direction_output = int0002_gpio_direction_output;
197 chip->base = -1;
198 chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1;
199 chip->irq.init_valid_mask = int0002_init_irq_valid_mask;
200
201 /*
202 * We directly request the irq here instead of passing a flow-handler
203 * to gpiochip_set_chained_irqchip, because the irq is shared.
204 * FIXME: augment this if we managed to pull handling of shared
205 * IRQs into gpiolib.
206 */
207 ret = devm_request_irq(dev, irq, int0002_irq,
208 IRQF_SHARED, "INT0002", chip);
209 if (ret) {
210 dev_err(dev, "Error requesting IRQ %d: %d\n", irq, ret);
211 return ret;
212 }
213
214 girq = &chip->irq;
215 girq->chip = (struct irq_chip *)cpu_id->driver_data;
216 /* This let us handle the parent IRQ in the driver */
217 girq->parent_handler = NULL;
218 girq->num_parents = 0;
219 girq->parents = NULL;
220 girq->default_type = IRQ_TYPE_NONE;
221 girq->handler = handle_edge_irq;
222
223 ret = devm_gpiochip_add_data(dev, chip, NULL);
224 if (ret) {
225 dev_err(dev, "Error adding gpio chip: %d\n", ret);
226 return ret;
227 }
228

 > 229		acpi_s2idle_register_wake_callback(irq, int0002_check_wake, NULL);
   230		return 0;
   231	}
   232	
   233	static int int0002_remove(struct platform_device *pdev)
   234	{
 > 235		acpi_s2idle_unregister_wake_callback(int0002_check_wake, NULL);

236 return 0;
237 }
238

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org