Skip to contentSkip to conversation

platform/x86: Add Driver to set up lid GPEs on MS Surface device

PATCH Superseded Maximilian Luz 3 participants 6 messages Sep 8, 2020 – Sep 15, 2020 1 message not in the archive

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: Add Driver to set up lid GPEs on MS Surface device

Conventionally, wake-up events for a specific device, in our case the lid device, are managed via the ACPI _PRW field. While this does not seem strictly necessary based on ACPI spec, the kernel disables GPE wakeups to avoid non-wakeup interrupts preventing suspend by default and only enables GPEs associated via the _PRW field with a wake-up capable device. This behavior has been introduced in commit f941d3e41da7 ("ACPI:
EC / PM: Disable non-wakeup GPEs for suspend-to-idle") and is described in more detail in its commit message.

Unfortunately, on MS Surface devices, there is no _PRW field present on the lid device, thus no GPE is associated with it, and therefore the GPE responsible for sending the status-change notification to the lid gets disabled during suspend, making it impossible to wake the device via the lid.

This patch introduces a pseudo-device and respective driver which, based on some DMI matching, marks the corresponding GPE of the lid device for wake and enables it during suspend. The behavior of this driver models the behavior of the ACPI/PM core for normal wakeup GPEs, properly declared via the _PRW field.

---

This driver has been tested (and is currently in use) on various Surface models via the linux-surface project [1].

[1]: https://github.com/linux-surface/linux-surface

---
 MAINTAINERS                        |   6 +
 drivers/platform/x86/Kconfig       |  10 +
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/surface_gpe.c | 298 +++++++++++++++++++++++++++++
 4 files changed, 315 insertions(+)
 create mode 100644 drivers/platform/x86/surface_gpe.c
MAINTAINERS +6 -0
index b5cfab015bd61..a9f8400096e16 100644
@@ -11549,6 +11549,12 @@ F:	drivers/scsi/smartpqi/smartpqi*.[ch]
 F:	include/linux/cciss*.h
 F:	include/uapi/linux/cciss*.h
 
+MICROSOFT SURFACE GPE LID SUPPORT DRIVER
+M:	Maximilian Luz <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/platform/x86/surface_gpe.c
+
 MICROSOFT SURFACE PRO 3 BUTTON DRIVER
 M:	Chen Yu <[email protected]>
 L:	[email protected]
drivers/platform/x86/Kconfig +10 -0
index 40219bba68011..cd29ab65f8b15 100644
@@ -894,6 +894,16 @@ config SURFACE_3_POWER_OPREGION
 	  This driver provides support for ACPI operation
 	  region of the Surface 3 battery platform driver.
 
+config SURFACE_GPE
+	tristate "Surface GPE/Lid Support Driver"
+	depends on ACPI
+	depends on DMI
+	help
+	  This driver marks the GPEs related to the ACPI lid device found on
+	  Microsoft Surface devices as wakeup sources and prepares them
+	  accordingly. It is required on those devices to allow wake-ups from
+	  suspend by opening the lid.
+
 config SURFACE_PRO3_BUTTON
 	tristate "Power/home/volume buttons driver for Microsoft Surface Pro 3/4 tablet"
 	depends on ACPI && INPUT
drivers/platform/x86/Makefile +1 -0
index 5f823f7eff452..58c2a6f52e394 100644
@@ -85,6 +85,7 @@ obj-$(CONFIG_INTEL_VBTN)		+= intel-vbtn.o
 obj-$(CONFIG_SURFACE3_WMI)		+= surface3-wmi.o
 obj-$(CONFIG_SURFACE_3_BUTTON)		+= surface3_button.o
 obj-$(CONFIG_SURFACE_3_POWER_OPREGION)	+= surface3_power.o
+obj-$(CONFIG_SURFACE_GPE)		+= surface_gpe.o
 obj-$(CONFIG_SURFACE_PRO3_BUTTON)	+= surfacepro3_button.o
 
 # MSI
drivers/platform/x86/surface_gpe.c new file +298 -0

298 lines not on this page · raw message

Signature · 1 line
2.28.0
#1 lore raw
Quoted 21 lines · Maximilian Luz

Conventionally, wake-up events for a specific device, in our case the lid device, are managed via the ACPI _PRW field. While this does not seem strictly necessary based on ACPI spec, the kernel disables GPE wakeups to avoid non-wakeup interrupts preventing suspend by default and only enables GPEs associated via the _PRW field with a wake-up capable device. This behavior has been introduced in commit f941d3e41da7 ("ACPI:
EC / PM: Disable non-wakeup GPEs for suspend-to-idle") and is described in more detail in its commit message.

Unfortunately, on MS Surface devices, there is no _PRW field present on the lid device, thus no GPE is associated with it, and therefore the GPE responsible for sending the status-change notification to the lid gets disabled during suspend, making it impossible to wake the device via the lid.

This patch introduces a pseudo-device and respective driver which, based on some DMI matching, marks the corresponding GPE of the lid device for wake and enables it during suspend. The behavior of this driver models the behavior of the ACPI/PM core for normal wakeup GPEs, properly declared via the _PRW field.

...

drivers/platform/x86/surface_gpe.c line 13

Quoted 3 lines
drivers/platform/x86/surface_gpe.c new file +3 -0
+#include <linux/platform_device.h>
+
+

One blank line is enough.

...

drivers/platform/x86/surface_gpe.c line 31

Quoted 4 lines
drivers/platform/x86/surface_gpe.c new file +4 -0
+       .gpe_number = 0x17,
+       .gpe_number = 0x4D,
+       .gpe_number = 0x4F,
+       .gpe_number = 0x57,

From where these numbers come from? Can we get them from firmware (ACPI)?

...

drivers/platform/x86/surface_gpe.c line 139

Quoted 4 lines
drivers/platform/x86/surface_gpe.c new file +4 -0
+       { }
+};
+
+

One is enough. Same for other places.

...

drivers/platform/x86/surface_gpe.c line 161

Quoted 5 lines
drivers/platform/x86/surface_gpe.c new file +5 -0
+static int surface_gpe_suspend(struct device *dev)
+{
+       const struct surface_lid_device *lid;
+
+       lid = dev_get_platdata(dev);

There is enough room to put this assignment directly into definition.

drivers/platform/x86/surface_gpe.c line 169

Quoted 8 lines
drivers/platform/x86/surface_gpe.c new file +8 -0
+       return surface_lid_enable_wakeup(dev, lid, true);
+}
+
+static int surface_gpe_resume(struct device *dev)
+{
+       const struct surface_lid_device *lid;
+
+       lid = dev_get_platdata(dev);

Ditto.

Quoted 2 lines
(quoted diff) +2 -0
+       return surface_lid_enable_wakeup(dev, lid, false);
+}

...

drivers/platform/x86/surface_gpe.c line 183

Quoted 9 lines
drivers/platform/x86/surface_gpe.c new file +8 -0
+static int surface_gpe_probe(struct platform_device *pdev)
+{
+       const struct surface_lid_device *lid;
+       int status;
+
 
+       lid = dev_get_platdata(&pdev->dev);
+       if (!lid)
+               return -ENODEV;

Can we use software nodes?

drivers/platform/x86/surface_gpe.c line 191

Quoted 8 lines
drivers/platform/x86/surface_gpe.c new file +7 -0
+       status = acpi_mark_gpe_for_wake(NULL, lid->gpe_number);
+       if (status) {
+               dev_err(&pdev->dev, "failed to mark GPE for wake: %d\n", status);
+               return -EINVAL;
+       }
+
 
+       status = acpi_enable_gpe(NULL, lid->gpe_number);

Did I miss anything or all calls of enable / disable GPE are using NULL as a first parameter? What the point in such case?

Quoted 13 lines
drivers/platform/x86/surface_gpe.c new file +13 -0
+       if (status) {
+               dev_err(&pdev->dev, "failed to enable GPE: %d\n", status);
+               return -EINVAL;
+       }
+
+       status = surface_lid_enable_wakeup(&pdev->dev, lid, false);
+       if (status) {
+               acpi_disable_gpe(NULL, lid->gpe_number);
+               return status;
+       }
+
+       return 0;
+}

...

drivers/platform/x86/surface_gpe.c line 276

Quoted 5 lines
drivers/platform/x86/surface_gpe.c new file +4 -0
+static void __exit surface_gpe_exit(void)
+{
 
+       if (!surface_gpe_device)
+               return;

This is redundant check.

drivers/platform/x86/surface_gpe.c line 283

Quoted 7 lines
drivers/platform/x86/surface_gpe.c new file +6 -0
+       platform_device_unregister(surface_gpe_device);
+       platform_driver_unregister(&surface_gpe_driver);
+}
+
 
+module_init(surface_gpe_init);
+module_exit(surface_gpe_exit);

Attach each to the corresponding method w/o blank line in between.

...

drivers/platform/x86/surface_gpe.c line 290

Quoted 2 lines
drivers/platform/x86/surface_gpe.c new file +2 -0
+MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfacePro:*");
+MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfacePro4:*");

Can simply

MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurface*:*");

work?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 1 line · Andy Shevchenko

On Tue, Sep 8, 2020 at 8:20 PM Maximilian Luz <[email protected]> wrote:

...

Quoted 6 lines

drivers/platform/x86/surface_gpe.c line 31

Quoted 4 lines
drivers/platform/x86/surface_gpe.c new file +4 -0
+       .gpe_number = 0x17,
+       .gpe_number = 0x4D,
+       .gpe_number = 0x4F,
+       .gpe_number = 0x57,

From where these numbers come from? Can we get them from firmware (ACPI)?

Yes, they are obtained from ACPI/the DSDT. Specifically from the name of the GPE handler notifying the lid device. See [1] for a repo full of Surface ACPI dumps (source for this). I'll add a comment pointing this out in v2.

[1]: https://github.com/linux-surface/acpidumps

...

Quoted 11 lines

drivers/platform/x86/surface_gpe.c line 183

Quoted 9 lines
drivers/platform/x86/surface_gpe.c new file +8 -0
+static int surface_gpe_probe(struct platform_device *pdev)
+{
+       const struct surface_lid_device *lid;
+       int status;
+
 
+       lid = dev_get_platdata(&pdev->dev);
+       if (!lid)
+               return -ENODEV;

Can we use software nodes?

As far as I can tell this would work via fwnode_create_software_node / fwnode_remove_software_node and device properties? I don't seem to find much documentation on this (there doesn't seem to be an entry for software nodes in the official docs?), but I think I should be able to make this work.

Quoted 11 lines

drivers/platform/x86/surface_gpe.c line 191

Quoted 8 lines
drivers/platform/x86/surface_gpe.c new file +7 -0
+       status = acpi_mark_gpe_for_wake(NULL, lid->gpe_number);
+       if (status) {
+               dev_err(&pdev->dev, "failed to mark GPE for wake: %d\n", status);
+               return -EINVAL;
+       }
+
 
+       status = acpi_enable_gpe(NULL, lid->gpe_number);

Did I miss anything or all calls of enable / disable GPE are using NULL as a first parameter? What the point in such case?

As far as I can tell, some of the more generic uses have a non-NULL gpe_device parameter (acpi/device_pm.c, acpi/wakeup.c) and NULL just means index-0/main device? Not an expert on that though, so probably just ignore me here and let the ACPI guys answer this.

...

Quoted 8 lines

drivers/platform/x86/surface_gpe.c line 290

Quoted 2 lines
drivers/platform/x86/surface_gpe.c new file +2 -0
+MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfacePro:*");
+MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfacePro4:*");

Can simply

MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurface*:*");

work?

Depends on your preference, really. That would also auto-load the module on Surface Pro 3 and earlier devices (just won't do anything on those). So it's a trade-off between unnecessary loading of the module and maintainability/readability. Let me know what you prefer and I'll switch to that.

Style and other issues are noted, I'll fix them for v2.

Regards,
Max

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 2 lines · mark gross

Surface devices are tablets with detachable keyboards. they don't really have a "lid" as the tablet is the "lid".

The Surface Laptop series doesn't have a detachable keyboard, yet still requires this. Arguably, the Surface Books are also more laptop than tablet (at least that's the way I use mine...). Finally, on the actual tablets (Surface Pro series) the lid switch detects when the keyboard cover is opened (or at least that's what I have been told, I don't own/have access to a Pro series device).

Regardless of that, this patch is intended to provide the same behavior as found on Windows, for all devices included in this patch, which is:
When you open the lid, or in case of the Pro series fold away the keyboard cover, the device wakes from suspend/s2idle. Without this patch, that doesn't work.

Quoted 2 lines

I'm just questioning if the creator of the device designed it the way they did maybe we should think twice about doing this.

As far as I can tell, the intended behavior is to wake the device when the lid is opened, which on the Laptops and Books is a more conventional lid and on the Pros constitutes opening the cover.

I'm open for any alternative though.

Also please note that I've already sent a v2 of this patch with Andy's comments addressed: https://lore.kernel.org/patchwork/patch/1303997/

--
Regards,
Max

#6 lore raw
Quoted 20 lines · Maximilian Luz
Quoted 2 lines · mark gross

Surface devices are tablets with detachable keyboards. they don't really have a "lid" as the tablet is the "lid".

The Surface Laptop series doesn't have a detachable keyboard, yet still requires this. Arguably, the Surface Books are also more laptop than tablet (at least that's the way I use mine...). Finally, on the actual tablets (Surface Pro series) the lid switch detects when the keyboard cover is opened (or at least that's what I have been told, I don't own/have access to a Pro series device).

Regardless of that, this patch is intended to provide the same behavior as found on Windows, for all devices included in this patch, which is:
When you open the lid, or in case of the Pro series fold away the keyboard cover, the device wakes from suspend/s2idle. Without this patch, that doesn't work.

Quoted 2 lines

I'm just questioning if the creator of the device designed it the way they did maybe we should think twice about doing this.

Quoted 8 lines

As far as I can tell, the intended behavior is to wake the device when the lid is opened, which on the Laptops and Books is a more conventional lid and on the Pros constitutes opening the cover.

I'm open for any alternative though.

Also please note that I've already sent a v2 of this patch with Andy's comments addressed: https://lore.kernel.org/patchwork/patch/1303997/

never mind then.
--mark