[PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq

Mark Hasemeyer posted 24 patches 10 months, 2 weeks ago
[PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Mark Hasemeyer 10 months, 2 weeks ago
The cros ec driver is manually managing the wake IRQ by calling
enable_irq_wake()/disable_irq_wake() during suspend/resume.

Modify the driver to use the power management subsystem to manage the
wakeirq.

Rather than assuming that the IRQ is wake capable, use the underlying
firmware/device tree to determine whether or not to enable it as a wake
source. Some Chromebooks rely solely on the ec_sync pin to wake the AP
but do not specify the interrupt as wake capable in the ACPI _CRS. For
LPC/ACPI based systems a DMI quirk is introduced listing boards whose
firmware should not be trusted to provide correct wake capable values.
For device tree base systems, it is not an issue as the relevant device
tree entries have been updated and DTS is built from source for each
ChromeOS update.

Acked-by: Tzung-Bi Shih <tzungbi@kernel.org>
Signed-off-by: Mark Hasemeyer <markhas@chromium.org>
---

Changes in v4:
-Rebase on linux-next
-See each patch for patch specific changes
-Add Tzung-Bi's Ack tag
-Drop dev_err() during cros_ec_uart_probe()
-Initalize struct resource on stack
-Update error handling for platform_get_irq_resource_optional()

Changes in v3:
-Rebase on linux-next
-See each patch for patch specific changes
-Remove MODULE_DEVICE_TABLE
-Drop "cros_ec _" prefix from should_force_irq_wake_capable()
-Drop use of dev_err_probe() to be consistent with existing conventions
in the driver
-Drop *spi argument from cros_ec_spi_dt_probe()
-Drop null device_node check from cros_ec_spi_dt_probe()
-Add trailing commas to DMI table
-Drop redundant "!= NULL" in should_force_irq_wake_capable()
-Use str_yes_no() to print irq wake capability
-Move irqwake handling from the interface specific modules to cros_ec.c

Changes in v2:
-Rebase on linux-next
-Add cover letter
-See each patch for patch specific changes
-Look for 'wakeup-source' property in cros_ec_spi.c

 drivers/platform/chrome/cros_ec.c           | 48 +++++++++++++++++----
 drivers/platform/chrome/cros_ec_lpc.c       | 40 ++++++++++++++---
 drivers/platform/chrome/cros_ec_spi.c       | 15 ++++---
 drivers/platform/chrome/cros_ec_uart.c      | 14 ++++--
 include/linux/platform_data/cros_ec_proto.h |  4 +-
 5 files changed, 94 insertions(+), 27 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
index badc68bbae8cc..080b479f39a94 100644
--- a/drivers/platform/chrome/cros_ec.c
+++ b/drivers/platform/chrome/cros_ec.c
@@ -15,6 +15,7 @@
 #include <linux/platform_device.h>
 #include <linux/platform_data/cros_ec_commands.h>
 #include <linux/platform_data/cros_ec_proto.h>
+#include <linux/pm_wakeirq.h>
 #include <linux/slab.h>
 #include <linux/suspend.h>
 
@@ -168,6 +169,35 @@ static int cros_ec_ready_event(struct notifier_block *nb,
 	return NOTIFY_DONE;
 }
 
+static int enable_irq_for_wake(struct cros_ec_device *ec_dev)
+{
+	struct device *dev = ec_dev->dev;
+	int ret = device_init_wakeup(dev, true);
+
+	if (ret) {
+		dev_err(dev, "Failed to enable device for wakeup");
+		return ret;
+	}
+	ret = dev_pm_set_wake_irq(dev, ec_dev->irq);
+	if (ret)
+		device_init_wakeup(dev, false);
+
+	return ret;
+}
+
+static int disable_irq_for_wake(struct cros_ec_device *ec_dev)
+{
+	int ret;
+	struct device *dev = ec_dev->dev;
+
+	dev_pm_clear_wake_irq(dev);
+	ret = device_init_wakeup(dev, false);
+	if (ret)
+		dev_err(dev, "Failed to disable device for wakeup");
+
+	return ret;
+}
+
 /**
  * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
  * @ec_dev: Device to register.
@@ -221,6 +251,13 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
 				ec_dev->irq, err);
 			goto exit;
 		}
+		dev_dbg(dev, "IRQ: %i, wake_capable: %s\n", ec_dev->irq,
+			str_yes_no(ec_dev->irq_wake));
+		if (ec_dev->irq_wake) {
+			err = enable_irq_for_wake(ec_dev);
+			if (err)
+				goto exit;
+		}
 	}
 
 	/* Register a platform device for the main EC instance */
@@ -313,6 +350,8 @@ EXPORT_SYMBOL(cros_ec_register);
  */
 void cros_ec_unregister(struct cros_ec_device *ec_dev)
 {
+	if (ec_dev->irq_wake)
+		disable_irq_for_wake(ec_dev);
 	platform_device_unregister(ec_dev->pd);
 	platform_device_unregister(ec_dev->ec);
 	mutex_destroy(&ec_dev->lock);
@@ -353,12 +392,6 @@ EXPORT_SYMBOL(cros_ec_suspend_prepare);
 
 static void cros_ec_disable_irq(struct cros_ec_device *ec_dev)
 {
-	struct device *dev = ec_dev->dev;
-	if (device_may_wakeup(dev))
-		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
-	else
-		ec_dev->wake_enabled = false;
-
 	disable_irq(ec_dev->irq);
 	ec_dev->suspended = true;
 }
@@ -440,9 +473,6 @@ static void cros_ec_enable_irq(struct cros_ec_device *ec_dev)
 	ec_dev->suspended = false;
 	enable_irq(ec_dev->irq);
 
-	if (ec_dev->wake_enabled)
-		disable_irq_wake(ec_dev->irq);
-
 	/*
 	 * Let the mfd devices know about events that occur during
 	 * suspend. This way the clients know what to do with them.
diff --git a/drivers/platform/chrome/cros_ec_lpc.c b/drivers/platform/chrome/cros_ec_lpc.c
index f0f3d3d561572..0204593d7b1c9 100644
--- a/drivers/platform/chrome/cros_ec_lpc.c
+++ b/drivers/platform/chrome/cros_ec_lpc.c
@@ -48,6 +48,27 @@ struct lpc_driver_ops {
 
 static struct lpc_driver_ops cros_ec_lpc_ops = { };
 
+static const struct dmi_system_id untrusted_fw_irq_wake_capable[] = {
+	{
+		.ident = "Brya",
+		.matches = {
+			DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
+		},
+	},
+	{
+		.ident = "Brask",
+		.matches = {
+			DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brask"),
+		},
+	},
+	{ }
+};
+
+static bool should_force_irq_wake_capable(void)
+{
+	return dmi_first_match(untrusted_fw_irq_wake_capable);
+}
+
 /*
  * A generic instance of the read function of struct lpc_driver_ops, used for
  * the LPC EC.
@@ -353,8 +374,9 @@ static int cros_ec_lpc_probe(struct platform_device *pdev)
 	struct acpi_device *adev;
 	acpi_status status;
 	struct cros_ec_device *ec_dev;
+	struct resource r = {};
 	u8 buf[2] = {};
-	int irq, ret;
+	int ret;
 
 	/*
 	 * The Framework Laptop (and possibly other non-ChromeOS devices)
@@ -428,12 +450,16 @@ static int cros_ec_lpc_probe(struct platform_device *pdev)
 	 * Some boards do not have an IRQ allotted for cros_ec_lpc,
 	 * which makes ENXIO an expected (and safe) scenario.
 	 */
-	irq = platform_get_irq_optional(pdev, 0);
-	if (irq > 0)
-		ec_dev->irq = irq;
-	else if (irq != -ENXIO) {
-		dev_err(dev, "couldn't retrieve IRQ number (%d)\n", irq);
-		return irq;
+	ret = platform_get_irq_resource_optional(pdev, 0, &r);
+	if (!ret) {
+		ec_dev->irq = r.start;
+		if (should_force_irq_wake_capable())
+			ec_dev->irq_wake = true;
+		else
+			ec_dev->irq_wake = r.flags & IORESOURCE_IRQ_WAKECAPABLE;
+	} else if (ret != -ENXIO) {
+		dev_err(dev, "couldn't retrieve IRQ number (%d)\n", ret);
+		return ret;
 	}
 
 	ret = cros_ec_register(ec_dev);
diff --git a/drivers/platform/chrome/cros_ec_spi.c b/drivers/platform/chrome/cros_ec_spi.c
index 3e88cc92e8192..102cdc3d1956d 100644
--- a/drivers/platform/chrome/cros_ec_spi.c
+++ b/drivers/platform/chrome/cros_ec_spi.c
@@ -7,6 +7,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/of_irq.h>
 #include <linux/platform_data/cros_ec_commands.h>
 #include <linux/platform_data/cros_ec_proto.h>
 #include <linux/platform_device.h>
@@ -70,6 +71,7 @@
  * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
  *      is sent when we want to turn off CS at the end of a transaction.
  * @high_pri_worker: Used to schedule high priority work.
+ * @irq_wake: Whether or not irq assertion should wake the system.
  */
 struct cros_ec_spi {
 	struct spi_device *spi;
@@ -77,6 +79,7 @@ struct cros_ec_spi {
 	unsigned int start_of_msg_delay;
 	unsigned int end_of_msg_delay;
 	struct kthread_worker *high_pri_worker;
+	bool irq_wake;
 };
 
 typedef int (*cros_ec_xfer_fn_t) (struct cros_ec_device *ec_dev,
@@ -689,9 +692,10 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
 	return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_cmd_xfer_spi);
 }
 
-static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
+static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi)
 {
-	struct device_node *np = dev->of_node;
+	struct spi_device *spi = ec_spi->spi;
+	struct device_node *np = spi->dev.of_node;
 	u32 val;
 	int ret;
 
@@ -702,6 +706,8 @@ static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
 	ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
 	if (!ret)
 		ec_spi->end_of_msg_delay = val;
+
+	ec_spi->irq_wake = spi->irq > 0 && of_property_present(np, "wakeup-source");
 }
 
 static void cros_ec_spi_high_pri_release(void *worker)
@@ -754,12 +760,13 @@ static int cros_ec_spi_probe(struct spi_device *spi)
 		return -ENOMEM;
 
 	/* Check for any DT properties */
-	cros_ec_spi_dt_probe(ec_spi, dev);
+	cros_ec_spi_dt_probe(ec_spi);
 
 	spi_set_drvdata(spi, ec_dev);
 	ec_dev->dev = dev;
 	ec_dev->priv = ec_spi;
 	ec_dev->irq = spi->irq;
+	ec_dev->irq_wake = ec_spi->irq_wake;
 	ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
 	ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi;
 	ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
@@ -780,8 +787,6 @@ static int cros_ec_spi_probe(struct spi_device *spi)
 		return err;
 	}
 
-	device_init_wakeup(&spi->dev, true);
-
 	return 0;
 }
 
diff --git a/drivers/platform/chrome/cros_ec_uart.c b/drivers/platform/chrome/cros_ec_uart.c
index 68d80559fddc2..5de346a078745 100644
--- a/drivers/platform/chrome/cros_ec_uart.c
+++ b/drivers/platform/chrome/cros_ec_uart.c
@@ -69,6 +69,7 @@ struct response_info {
  * @serdev:		serdev uart device we are connected to.
  * @baudrate:		UART baudrate of attached EC device.
  * @flowcontrol:	UART flowcontrol of attached device.
+ * @irq_wake:		Whether or not irq assertion should wake the system.
  * @irq:		Linux IRQ number of associated serial device.
  * @response:		Response info passing between cros_ec_uart_pkt_xfer()
  *			and cros_ec_uart_rx_bytes()
@@ -77,6 +78,7 @@ struct cros_ec_uart {
 	struct serdev_device *serdev;
 	u32 baudrate;
 	u8 flowcontrol;
+	bool irq_wake;
 	u32 irq;
 	struct response_info response;
 };
@@ -224,8 +226,10 @@ static int cros_ec_uart_resource(struct acpi_resource *ares, void *data)
 static int cros_ec_uart_acpi_probe(struct cros_ec_uart *ec_uart)
 {
 	int ret;
+	struct resource irqres;
 	LIST_HEAD(resources);
-	struct acpi_device *adev = ACPI_COMPANION(&ec_uart->serdev->dev);
+	struct device *dev = &ec_uart->serdev->dev;
+	struct acpi_device *adev = ACPI_COMPANION(dev);
 
 	ret = acpi_dev_get_resources(adev, &resources, cros_ec_uart_resource, ec_uart);
 	if (ret < 0)
@@ -234,12 +238,12 @@ static int cros_ec_uart_acpi_probe(struct cros_ec_uart *ec_uart)
 	acpi_dev_free_resource_list(&resources);
 
 	/* Retrieve GpioInt and translate it to Linux IRQ number */
-	ret = acpi_dev_gpio_irq_get(adev, 0);
+	ret = acpi_dev_get_gpio_irq_resource(adev, NULL, 0, &irqres);
 	if (ret < 0)
 		return ret;
 
-	ec_uart->irq = ret;
-	dev_dbg(&ec_uart->serdev->dev, "IRQ number %d\n", ec_uart->irq);
+	ec_uart->irq = irqres.start;
+	ec_uart->irq_wake = irqres.flags & IORESOURCE_IRQ_WAKECAPABLE;
 
 	return 0;
 }
@@ -293,6 +297,7 @@ static int cros_ec_uart_probe(struct serdev_device *serdev)
 	ec_dev->dev = dev;
 	ec_dev->priv = ec_uart;
 	ec_dev->irq = ec_uart->irq;
+	ec_dev->irq_wake = ec_uart->irq_wake;
 	ec_dev->cmd_xfer = NULL;
 	ec_dev->pkt_xfer = cros_ec_uart_pkt_xfer;
 	ec_dev->din_size = sizeof(struct ec_host_response) +
@@ -301,6 +306,7 @@ static int cros_ec_uart_probe(struct serdev_device *serdev)
 
 	serdev_device_set_client_ops(serdev, &cros_ec_uart_client_ops);
 
+	/* Register a new cros_ec device */
 	return cros_ec_register(ec_dev);
 }
 
diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
index 8865e350c12a5..0fb2781b602d6 100644
--- a/include/linux/platform_data/cros_ec_proto.h
+++ b/include/linux/platform_data/cros_ec_proto.h
@@ -100,6 +100,7 @@ struct cros_ec_command {
  * @proto_version: The protocol version used for this device.
  * @priv: Private data.
  * @irq: Interrupt to use.
+ * @irq_wake: Whether or not irq assertion should wake the system.
  * @id: Device id.
  * @din: Input buffer (for data from EC). This buffer will always be
  *       dword-aligned and include enough space for up to 7 word-alignment
@@ -115,7 +116,6 @@ struct cros_ec_command {
  *        performance advantage to using dword.
  * @din_size: Size of din buffer to allocate (zero to use static din).
  * @dout_size: Size of dout buffer to allocate (zero to use static dout).
- * @wake_enabled: True if this device can wake the system from sleep.
  * @suspended: True if this device had been suspended.
  * @cmd_xfer: Send command to EC and get response.
  *            Returns the number of bytes received if the communication
@@ -169,11 +169,11 @@ struct cros_ec_device {
 	u16 proto_version;
 	void *priv;
 	int irq;
+	bool irq_wake;
 	u8 *din;
 	u8 *dout;
 	int din_size;
 	int dout_size;
-	bool wake_enabled;
 	bool suspended;
 	int (*cmd_xfer)(struct cros_ec_device *ec,
 			struct cros_ec_command *msg);
-- 
2.43.0.472.g3155946c3a-goog
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Stephen Boyd 10 months, 2 weeks ago
Quoting Mark Hasemeyer (2024-01-02 13:07:48)
> The cros ec driver is manually managing the wake IRQ by calling
> enable_irq_wake()/disable_irq_wake() during suspend/resume.
>
> Modify the driver to use the power management subsystem to manage the
> wakeirq.
>
> Rather than assuming that the IRQ is wake capable, use the underlying
> firmware/device tree to determine whether or not to enable it as a wake
> source. Some Chromebooks rely solely on the ec_sync pin to wake the AP
> but do not specify the interrupt as wake capable in the ACPI _CRS. For
> LPC/ACPI based systems a DMI quirk is introduced listing boards whose
> firmware should not be trusted to provide correct wake capable values.

The DMI quirk looks to be fixing something. Most likely that should be
backported to stable kernels as well? Please split the DMI match table
part out of this so that it isn't mixed together with migrating the
driver to use the pm wakeirq logic.

> For device tree base systems, it is not an issue as the relevant device
> tree entries have been updated and DTS is built from source for each
> ChromeOS update.

It is still sorta an issue. It means that we can't backport this patch
without also backporting the DTS patch to the kernel. Furthermore, DTS
changes go through different maintainer trees, so having this patch in
the kernel without the DTS patch means the bisection hole is possibly
quite large.

Does using the pm wakeirq logic require the use of 'wakeup-source'
property in DT? A quick glance makes me believe it isn't needed, so
please split that part out of this patch as well. We should see a patch
for the DMI quirk, a patch to use wakeup-source (doubtful that we need
it at all though), and a patch to use the pm wakeirq logic instead of
hand rolling it again.

>
> Acked-by: Tzung-Bi Shih <tzungbi@kernel.org>
> Signed-off-by: Mark Hasemeyer <markhas@chromium.org>
> ---
[...]
> diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
> index badc68bbae8cc..080b479f39a94 100644
> --- a/drivers/platform/chrome/cros_ec.c
> +++ b/drivers/platform/chrome/cros_ec.c
> @@ -15,6 +15,7 @@
>  #include <linux/platform_device.h>
>  #include <linux/platform_data/cros_ec_commands.h>
>  #include <linux/platform_data/cros_ec_proto.h>
> +#include <linux/pm_wakeirq.h>
>  #include <linux/slab.h>
>  #include <linux/suspend.h>
>
> @@ -168,6 +169,35 @@ static int cros_ec_ready_event(struct notifier_block *nb,
>         return NOTIFY_DONE;
>  }
>
> +static int enable_irq_for_wake(struct cros_ec_device *ec_dev)
> +{
> +       struct device *dev = ec_dev->dev;
> +       int ret = device_init_wakeup(dev, true);
> +
> +       if (ret) {
> +               dev_err(dev, "Failed to enable device for wakeup");

Missing newline on printk message.

> +               return ret;
> +       }
> +       ret = dev_pm_set_wake_irq(dev, ec_dev->irq);
> +       if (ret)
> +               device_init_wakeup(dev, false);
> +
> +       return ret;

I'd rather see the code formatted like this:

	int ret;

	ret = device_init_wakeup(dev, true);
	if (ret) {
		dev_err(...);
		return ret;
	}

	ret = dev_pm_set_wake_irq(...);
	if (ret)
		device_init_wakeup(dev, false);
	
	return ret;

Mostly because the first 'if (ret)' requires me to hunt in the variable
declaration part of the function to figure out what it is set to instead
of looking at the line directly above.

> +}
> +
> +static int disable_irq_for_wake(struct cros_ec_device *ec_dev)
> +{
> +       int ret;
> +       struct device *dev = ec_dev->dev;
> +
> +       dev_pm_clear_wake_irq(dev);
> +       ret = device_init_wakeup(dev, false);
> +       if (ret)
> +               dev_err(dev, "Failed to disable device for wakeup");
> +
> +       return ret;
> +}
> +
>  /**
>   * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
>   * @ec_dev: Device to register.
> @@ -221,6 +251,13 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
>                                 ec_dev->irq, err);
>                         goto exit;
>                 }
> +               dev_dbg(dev, "IRQ: %i, wake_capable: %s\n", ec_dev->irq,

This one has a newline :)

> +                       str_yes_no(ec_dev->irq_wake));
> +               if (ec_dev->irq_wake) {
> +                       err = enable_irq_for_wake(ec_dev);
> +                       if (err)
> +                               goto exit;
> +               }
>         }
[...]
> diff --git a/drivers/platform/chrome/cros_ec_spi.c b/drivers/platform/chrome/cros_ec_spi.c
> index 3e88cc92e8192..102cdc3d1956d 100644
> --- a/drivers/platform/chrome/cros_ec_spi.c
> +++ b/drivers/platform/chrome/cros_ec_spi.c
> @@ -7,6 +7,7 @@
>  #include <linux/kernel.h>
>  #include <linux/module.h>
>  #include <linux/of.h>
> +#include <linux/of_irq.h>
>  #include <linux/platform_data/cros_ec_commands.h>
>  #include <linux/platform_data/cros_ec_proto.h>
>  #include <linux/platform_device.h>
> @@ -70,6 +71,7 @@
>   * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
>   *      is sent when we want to turn off CS at the end of a transaction.
>   * @high_pri_worker: Used to schedule high priority work.
> + * @irq_wake: Whether or not irq assertion should wake the system.
>   */
>  struct cros_ec_spi {
>         struct spi_device *spi;
> @@ -77,6 +79,7 @@ struct cros_ec_spi {
>         unsigned int start_of_msg_delay;
>         unsigned int end_of_msg_delay;
>         struct kthread_worker *high_pri_worker;
> +       bool irq_wake;

This is only used in probe. Make it a local variable instead of another
struct member to save memory.

>  };
>
>  typedef int (*cros_ec_xfer_fn_t) (struct cros_ec_device *ec_dev,
> @@ -689,9 +692,10 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
>         return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_cmd_xfer_spi);
>  }
>
> -static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
> +static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi)
>  {
> -       struct device_node *np = dev->of_node;
> +       struct spi_device *spi = ec_spi->spi;
> +       struct device_node *np = spi->dev.of_node;
>         u32 val;
>         int ret;
>
> @@ -702,6 +706,8 @@ static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
>         ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
>         if (!ret)
>                 ec_spi->end_of_msg_delay = val;
> +
> +       ec_spi->irq_wake = spi->irq > 0 && of_property_present(np, "wakeup-source");

Is there any EC SPI device that doesn't want to support wakeup? I'd
prefer we introduce a new property or compatible string to indicate that
wakeup isn't supported and otherwise always set irq_wake to true. That
way DT can change in parallel with the driver instead of in lockstep.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Mark Hasemeyer 10 months, 2 weeks ago
> The DMI quirk looks to be fixing something. Most likely that should be
> backported to stable kernels as well? Please split the DMI match table
> part out of this so that it isn't mixed together with migrating the
> driver to use the pm wakeirq logic.

The DMI quirk is used to list boards whose IRQ should be enabled for
wake, regardless of what their firmware says. Currently the driver
always enables the EC IRQ for wake anyway, so there shouldn't be a
need to backport the DMI quirk on its own. Splitting out the DMI quirk
into its own patch would break Brya/Brask's ability to wake if they
were to run a kernel w/o the DMI patch. I chose not to split it out to
keep the change atomic, and avoid introducing any regressions.

> > For device tree base systems, it is not an issue as the relevant device
> > tree entries have been updated and DTS is built from source for each
> > ChromeOS update.
>
> It is still sorta an issue. It means that we can't backport this patch
> without also backporting the DTS patch to the kernel. Furthermore, DTS
> changes go through different maintainer trees, so having this patch in
> the kernel without the DTS patch means the bisection hole is possibly
> quite large.

Correct, this patch doesn't make sense to backport on its own. It is
dependent on the entire patch series (more than just the DTS changes).
I'm not super familiar with how patches flow through different
maintainer trees. But I'd imagine this patch series makes its way to
torvalds/master in some sort of sane manner where earlier patches land
before later (dependent) ones?

> Does using the pm wakeirq logic require the use of 'wakeup-source'
> property in DT? A quick glance makes me believe it isn't needed, so
> please split that part out of this patch as well.

No, 'wakeup-source' is not required, it provides an indication to the
driver that the IRQ should be used for wake, which then enables the pm
subsystem accordingly. If 'wakup-source' is not used, we're back to
square one of making assumptions. Specifically in this case, it would
be assumed that all SPI based EC's are wake capable.

> We should see a patch
> for the DMI quirk, a patch to use wakeup-source (doubtful that we need
> it at all though), and a patch to use the pm wakeirq logic instead of
> hand rolling it again.

I don't know if I'm convinced this should happen. I'm open to it, but
see my previous comments.

> > +static int enable_irq_for_wake(struct cros_ec_device *ec_dev)
> > +{
> > +       struct device *dev = ec_dev->dev;
> > +       int ret = device_init_wakeup(dev, true);
> > +
> > +       if (ret) {
> > +               dev_err(dev, "Failed to enable device for wakeup");
>
> Missing newline on printk message.

Ack.

>
> > +               return ret;
> > +       }
> > +       ret = dev_pm_set_wake_irq(dev, ec_dev->irq);
> > +       if (ret)
> > +               device_init_wakeup(dev, false);
> > +
> > +       return ret;
>
> I'd rather see the code formatted like this:
>
>         int ret;
>
>         ret = device_init_wakeup(dev, true);
>         if (ret) {
>                 dev_err(...);
>                 return ret;
>         }
>
>         ret = dev_pm_set_wake_irq(...);
>         if (ret)
>                 device_init_wakeup(dev, false);
>
>         return ret;
>
> Mostly because the first 'if (ret)' requires me to hunt in the variable
> declaration part of the function to figure out what it is set to instead
> of looking at the line directly above.

Sounds good :-)


> [...]
> > diff --git a/drivers/platform/chrome/cros_ec_spi.c b/drivers/platform/chrome/cros_ec_spi.c
> >  struct cros_ec_spi {
> >         struct spi_device *spi;
> > @@ -77,6 +79,7 @@ struct cros_ec_spi {
> >         unsigned int start_of_msg_delay;
> >         unsigned int end_of_msg_delay;
> >         struct kthread_worker *high_pri_worker;
> > +       bool irq_wake;
>
> This is only used in probe. Make it a local variable instead of another
> struct member to save memory.

Ack.

>
> >  };
> >
> >  typedef int (*cros_ec_xfer_fn_t) (struct cros_ec_device *ec_dev,
> > @@ -689,9 +692,10 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
> >         return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_cmd_xfer_spi);
> >  }
> >
> > -static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
> > +static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi)
> >  {
> > -       struct device_node *np = dev->of_node;
> > +       struct spi_device *spi = ec_spi->spi;
> > +       struct device_node *np = spi->dev.of_node;
> >         u32 val;
> >         int ret;
> >
> > @@ -702,6 +706,8 @@ static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
> >         ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
> >         if (!ret)
> >                 ec_spi->end_of_msg_delay = val;
> > +
> > +       ec_spi->irq_wake = spi->irq > 0 && of_property_present(np, "wakeup-source");
>
> Is there any EC SPI device that doesn't want to support wakeup?

I don't know for sure. If so, the EC driver doesn't currently support
it because it assumes wake capability.

> I'd
> prefer we introduce a new property or compatible string to indicate that
> wakeup isn't supported and otherwise always set irq_wake to true. That
> way DT can change in parallel with the driver instead of in lockstep.

We could introduce a custom binding? IMHO, using inverted logic like
that kind of goes against the grain with how ACPI and kernel are
currently structured. And I don't love how it would make the SPI EC
driver inverted from the other interfaces. I'd rather go back to just
assuming all SPI based EC's are wake capable. But even then, why
assume? This gives us flexibility to disable wakeirqs and drops
unnecessary assumptions. It also lays the groundwork for new boards
that may behave differently. For example, an ACPI based SPI EC.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Stephen Boyd 10 months, 2 weeks ago
Quoting Mark Hasemeyer (2024-01-03 09:47:17)
> > The DMI quirk looks to be fixing something. Most likely that should be
> > backported to stable kernels as well? Please split the DMI match table
> > part out of this so that it isn't mixed together with migrating the
> > driver to use the pm wakeirq logic.
>
> The DMI quirk is used to list boards whose IRQ should be enabled for
> wake, regardless of what their firmware says. Currently the driver
> always enables the EC IRQ for wake anyway, so there shouldn't be a
> need to backport the DMI quirk on its own. Splitting out the DMI quirk
> into its own patch would break Brya/Brask's ability to wake if they
> were to run a kernel w/o the DMI patch. I chose not to split it out to
> keep the change atomic, and avoid introducing any regressions.

Ok, thanks for clarifying. I understand now that it is to workaround the
firmware on those boards where the driver didn't care before.

>
> > > For device tree base systems, it is not an issue as the relevant device
> > > tree entries have been updated and DTS is built from source for each
> > > ChromeOS update.
> >
> > It is still sorta an issue. It means that we can't backport this patch
> > without also backporting the DTS patch to the kernel. Furthermore, DTS
> > changes go through different maintainer trees, so having this patch in
> > the kernel without the DTS patch means the bisection hole is possibly
> > quite large.
>
> Correct, this patch doesn't make sense to backport on its own. It is
> dependent on the entire patch series (more than just the DTS changes).
> I'm not super familiar with how patches flow through different
> maintainer trees. But I'd imagine this patch series makes its way to
> torvalds/master in some sort of sane manner where earlier patches land
> before later (dependent) ones?

The DTS patch would go through the platform maintainer tree and be
pulled into the soc tree and sent up to mainline from there. The
platform/chrome patch would go through chrome platform tree and then to
mainline. The bisection hole will be real.

>
> > Does using the pm wakeirq logic require the use of 'wakeup-source'
> > property in DT? A quick glance makes me believe it isn't needed, so
> > please split that part out of this patch as well.
>
> No, 'wakeup-source' is not required, it provides an indication to the
> driver that the IRQ should be used for wake, which then enables the pm
> subsystem accordingly. If 'wakup-source' is not used, we're back to
> square one of making assumptions. Specifically in this case, it would
> be assumed that all SPI based EC's are wake capable.

Is that the wrong assumption to make? My understanding of the DT
property is that it is used to signal that this device should be treated
as a wakeup source, when otherwise it isn't treated as one. In this
case, the device has always been treated as a wakeup source so adding
the property is redundant. Making the patch series dependent on the
property being present makes the driver break without the DT change. We
try to make drivers work with older DT files, sometimes by adding
backwards compatibility code, so the presence of the wakeup-source
property should not be required to make this work.

>
> > We should see a patch
> > for the DMI quirk, a patch to use wakeup-source (doubtful that we need
> > it at all though), and a patch to use the pm wakeirq logic instead of
> > hand rolling it again.
>
> I don't know if I'm convinced this should happen. I'm open to it, but
> see my previous comments.
>
> > >                 ec_spi->end_of_msg_delay = val;
> > > +
> > > +       ec_spi->irq_wake = spi->irq > 0 && of_property_present(np, "wakeup-source");
> >
> > Is there any EC SPI device that doesn't want to support wakeup?
>
> I don't know for sure. If so, the EC driver doesn't currently support
> it because it assumes wake capability.
>
> > I'd
> > prefer we introduce a new property or compatible string to indicate that
> > wakeup isn't supported and otherwise always set irq_wake to true. That
> > way DT can change in parallel with the driver instead of in lockstep.
>
> We could introduce a custom binding? IMHO, using inverted logic like
> that kind of goes against the grain with how ACPI and kernel are
> currently structured. And I don't love how it would make the SPI EC
> driver inverted from the other interfaces. I'd rather go back to just
> assuming all SPI based EC's are wake capable. But even then, why
> assume? This gives us flexibility to disable wakeirqs and drops
> unnecessary assumptions. It also lays the groundwork for new boards
> that may behave differently. For example, an ACPI based SPI EC.

What is the goal of this patch series? Is it to allow disabling the
wakeup capability of the EC wake irq from userspace? I can see a
possible problem where we want to disable wakeup for the EC dynamically
because either it has no child devices that are wakeup sources (e.g. no
power button, no keyboard on ARM) or userspace has disabled all the
wakeup sources for those child devices at runtime. In that case, we
would want to keep the EC irq from waking up the system from suspend. Is
that what you're doing here?
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Mark Hasemeyer 10 months, 2 weeks ago
> The DTS patch would go through the platform maintainer tree and be
> pulled into the soc tree and sent up to mainline from there. The
> platform/chrome patch would go through chrome platform tree and then to
> mainline. The bisection hole will be real.

I thought it would all get merged in the next merge window. How are
bifurcations like this normally dealt with? Does one wait for the
first series of patches to land in mainline before submitting
dependent patches? That could take two merge windows.

> >
> > > Does using the pm wakeirq logic require the use of 'wakeup-source'
> > > property in DT? A quick glance makes me believe it isn't needed, so
> > > please split that part out of this patch as well.
> >
> > No, 'wakeup-source' is not required, it provides an indication to the
> > driver that the IRQ should be used for wake, which then enables the pm
> > subsystem accordingly. If 'wakup-source' is not used, we're back to
> > square one of making assumptions. Specifically in this case, it would
> > be assumed that all SPI based EC's are wake capable.
>
> Is that the wrong assumption to make? My understanding of the DT
> property is that it is used to signal that this device should be treated
> as a wakeup source, when otherwise it isn't treated as one. In this
> case, the device has always been treated as a wakeup source so adding
> the property is redundant.

For SPI, it's not the wrong assumption. I was trying to drop the
assumption though to match ACPI/LPC behavior.

> Making the patch series dependent on the
> property being present makes the driver break without the DT change. We
> try to make drivers work with older DT files, sometimes by adding
> backwards compatibility code, so the presence of the wakeup-source
> property should not be required to make this work.

Do we have use cases where Chromebooks are running older DTBs? I get
the idea in theory, but don't grasp why it's needed here. Regardless,
I can update the SPI code to assume a wake capable IRQ. But I'd like
to keep the prerequisite device tree patches unless there's a good
reason to drop them. Specifying 'wake-source' certainly doesn't hurt
anything, and there are other improvements to the OF
subsystem/documentation.

> What is the goal of this patch series? Is it to allow disabling the
> wakeup capability of the EC wake irq from userspace? I can see a
> possible problem where we want to disable wakeup for the EC dynamically
> because either it has no child devices that are wakeup sources (e.g. no
> power button, no keyboard on ARM) or userspace has disabled all the
> wakeup sources for those child devices at runtime. In that case, we
> would want to keep the EC irq from waking up the system from suspend. Is
> that what you're doing here?

The root of this patch series stems from a bug where spurious wakes
are seen on Skyrim. Copying some wording from the DTS patches:
"Some Chromebooks use a separate wake pin, while others overload the
interrupt for wake and IO. With the current assumption, spurious wakes
can occur on systems that use a separate wake pin. It is planned to
update the driver to no longer assume that the EC interrupt pin should
be enabled for wake."

This patch series will allow us to disable the ec_sync pin as a wake
source on Skyrim as it already uses a dedicated wake gpio.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Stephen Boyd 10 months, 2 weeks ago
Quoting Mark Hasemeyer (2024-01-03 14:25:25)
> > The DTS patch would go through the platform maintainer tree and be
> > pulled into the soc tree and sent up to mainline from there. The
> > platform/chrome patch would go through chrome platform tree and then to
> > mainline. The bisection hole will be real.
>
> I thought it would all get merged in the next merge window. How are
> bifurcations like this normally dealt with? Does one wait for the
> first series of patches to land in mainline before submitting
> dependent patches? That could take two merge windows.

It's usually fine to land in the respective maintainer trees because the
driver is written to be compatible with either version of the DT.

>
> > >
> > > > Does using the pm wakeirq logic require the use of 'wakeup-source'
> > > > property in DT? A quick glance makes me believe it isn't needed, so
> > > > please split that part out of this patch as well.
> > >
> > > No, 'wakeup-source' is not required, it provides an indication to the
> > > driver that the IRQ should be used for wake, which then enables the pm
> > > subsystem accordingly. If 'wakup-source' is not used, we're back to
> > > square one of making assumptions. Specifically in this case, it would
> > > be assumed that all SPI based EC's are wake capable.
> >
> > Is that the wrong assumption to make? My understanding of the DT
> > property is that it is used to signal that this device should be treated
> > as a wakeup source, when otherwise it isn't treated as one. In this
> > case, the device has always been treated as a wakeup source so adding
> > the property is redundant.
>
> For SPI, it's not the wrong assumption. I was trying to drop the
> assumption though to match ACPI/LPC behavior.

Ok. Is the EC always a wakeup source? Not the EC irq, the EC device.

>
> > Making the patch series dependent on the
> > property being present makes the driver break without the DT change. We
> > try to make drivers work with older DT files, sometimes by adding
> > backwards compatibility code, so the presence of the wakeup-source
> > property should not be required to make this work.
>
> Do we have use cases where Chromebooks are running older DTBs? I get
> the idea in theory, but don't grasp why it's needed here.

It's to make the kernel bisectable while the DTB and driver patches are
merged through different trees.

> Regardless,
> I can update the SPI code to assume a wake capable IRQ. But I'd like
> to keep the prerequisite device tree patches unless there's a good
> reason to drop them. Specifying 'wake-source' certainly doesn't hurt
> anything, and there are other improvements to the OF
> subsystem/documentation.

I don't see any harm in having wakeup-source in the binding, but I'd
prefer it is behind a different compatible string as optional, and
introduced when we need to have an EC that doesn't wake up the system.
Otherwise it's all future coding for a device that doesn't exist. It's
also a potential landmine if the driver patch is backported somewhere
without the DTS patch (maybe the DTS is not upstream?). Someone will
have to debug why wakeups aren't working anymore.

>
> > What is the goal of this patch series? Is it to allow disabling the
> > wakeup capability of the EC wake irq from userspace? I can see a
> > possible problem where we want to disable wakeup for the EC dynamically
> > because either it has no child devices that are wakeup sources (e.g. no
> > power button, no keyboard on ARM) or userspace has disabled all the
> > wakeup sources for those child devices at runtime. In that case, we
> > would want to keep the EC irq from waking up the system from suspend. Is
> > that what you're doing here?
>
> The root of this patch series stems from a bug where spurious wakes
> are seen on Skyrim.

Are all 24 patches needed to fix spurious wakeups? Why can't we do a DMI
match table for Skyrim devices and disable the wakeirq logic on that
platform? That would be a much more focused and targeted fix, no?

> Copying some wording from the DTS patches:
> "Some Chromebooks use a separate wake pin, while others overload the
> interrupt for wake and IO. With the current assumption, spurious wakes
> can occur on systems that use a separate wake pin. It is planned to
> update the driver to no longer assume that the EC interrupt pin should
> be enabled for wake."
>
> This patch series will allow us to disable the ec_sync pin as a wake
> source on Skyrim as it already uses a dedicated wake gpio.

Aha! This last sentence is the detail I've been looking for. Please put
these details in the commit text.

"Skyrim devices are experiencing spurious wakeups due to the EC driver
always enabling the irq as a wakeup source but on Skyrim devices the EC
wakeup signal is a dedicated gpio separate from the irq."

Please be direct and specific instead of writing in general terms.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Mark Hasemeyer 10 months, 1 week ago
> > > > > Does using the pm wakeirq logic require the use of 'wakeup-source'
> > > > > property in DT? A quick glance makes me believe it isn't needed, so
> > > > > please split that part out of this patch as well.
> > > >
> > > > No, 'wakeup-source' is not required, it provides an indication to the
> > > > driver that the IRQ should be used for wake, which then enables the pm
> > > > subsystem accordingly. If 'wakup-source' is not used, we're back to
> > > > square one of making assumptions. Specifically in this case, it would
> > > > be assumed that all SPI based EC's are wake capable.
> > >
> > > Is that the wrong assumption to make? My understanding of the DT
> > > property is that it is used to signal that this device should be treated
> > > as a wakeup source, when otherwise it isn't treated as one. In this
> > > case, the device has always been treated as a wakeup source so adding
> > > the property is redundant.
> >
> > For SPI, it's not the wrong assumption. I was trying to drop the
> > assumption though to match ACPI/LPC behavior.
>
> Ok. Is the EC always a wakeup source? Not the EC irq, the EC device.

Yes. The powerd daemon enables the EC for wake [1]. At least on ACPI systems.
[1] https://source.chromium.org/chromiumos/chromiumos/codesearch/+/81b23aeac510655f27e1d87b99b12c78146e7c37:src/platform2/power_manager/powerd/daemon.cc;l=611

> > Regardless,
> > I can update the SPI code to assume a wake capable IRQ. But I'd like
> > to keep the prerequisite device tree patches unless there's a good
> > reason to drop them. Specifying 'wake-source' certainly doesn't hurt
> > anything, and there are other improvements to the OF
> > subsystem/documentation.
>
> I don't see any harm in having wakeup-source in the binding, but I'd
> prefer it is behind a different compatible string as optional, and
> introduced when we need to have an EC that doesn't wake up the system.

The 'wakeup-source' property is already optional. See patch 04 in this
version of the series which updates the documentation surrounding the
property. Or are you suggesting a completely new string so that we can
be forever backward compatible? If that's the case, the property would
indeed have to have inverted logic signifying a device that is *not*
wakeup capable. It feels wrong to have to do something like that.

> Otherwise it's all future coding for a device that doesn't exist.

I agree adding 'wakeup-source' is future coding for a device that
doesn't exist. It does provide (pseudo) feature parity with ACPI
systems though. See my comment below about ACPI GpioInt/Interrupt
resources.

> It's
> also a potential landmine if the driver patch is backported somewhere
> without the DTS patch (maybe the DTS is not upstream?). Someone will
> have to debug why wakeups aren't working anymore.

I can change the SPI driver so it doesn't look for the 'wakeup-source'
property, keeping existing behavior where wakeirq is assumed. So there
should be no issues with backporting.

> > > What is the goal of this patch series? Is it to allow disabling the
> > > wakeup capability of the EC wake irq from userspace? I can see a
> > > possible problem where we want to disable wakeup for the EC dynamically
> > > because either it has no child devices that are wakeup sources (e.g. no
> > > power button, no keyboard on ARM) or userspace has disabled all the
> > > wakeup sources for those child devices at runtime. In that case, we
> > > would want to keep the EC irq from waking up the system from suspend. Is
> > > that what you're doing here?
> >
> > The root of this patch series stems from a bug where spurious wakes
> > are seen on Skyrim.
>
> Are all 24 patches needed to fix spurious wakeups? Why can't we do a DMI
> match table for Skyrim devices and disable the wakeirq logic on that
> platform? That would be a much more focused and targeted fix, no?

It would be more focused and targeted, but I don't think it's the
correct fix. Skyrim is not the quirk. The driver is incorrectly
enabling the IRQ for wake even though a GPE exists for the EC to wake the AP.
ACPI defines keywords to specify GpioInt and Interrupt
resources as wake capable, and until recently [2], we were not
flagging the respective resources correctly. Most ACPI Chromebooks
have a dedicated GPE for wake, and enabling the IRQ for wake is
unintentional IMHO.
[2] https://review.coreboot.org/c/coreboot/+/79373

> > Copying some wording from the DTS patches:
> > "Some Chromebooks use a separate wake pin, while others overload the
> > interrupt for wake and IO. With the current assumption, spurious wakes
> > can occur on systems that use a separate wake pin. It is planned to
> > update the driver to no longer assume that the EC interrupt pin should
> > be enabled for wake."
> >
> > This patch series will allow us to disable the ec_sync pin as a wake
> > source on Skyrim as it already uses a dedicated wake gpio.
>
> Aha! This last sentence is the detail I've been looking for. Please put
> these details in the commit text.
>
> "Skyrim devices are experiencing spurious wakeups due to the EC driver
> always enabling the irq as a wakeup source but on Skyrim devices the EC
> wakeup signal is a dedicated gpio separate from the irq."
>
> Please be direct and specific instead of writing in general terms.

Sure, I can update the commit text :-)

In summary, there are a lot of comments that suggest different
solutions. Here are the options I see:
1. Skyrim DMI quirk
2a. Update EC SPI driver to assume wake capable regardless of
'wakeup-source' property being present
2b. Remove 'wakeup-source' entries from DTS
3. Leave the existing solution

I'm arguing for 2a (without 2b). This keeps backward compatibility
while adding an indication that the EC is wake capable and keeps
closer feature parity with the ACPI/LPC interface. FWIW, others have
already reviewed/ack'd the dts patches.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Stephen Boyd 10 months, 1 week ago
Quoting Mark Hasemeyer (2024-01-03 20:55:41)
> > > > > > Does using the pm wakeirq logic require the use of 'wakeup-source'
> > > > > > property in DT? A quick glance makes me believe it isn't needed, so
> > > > > > please split that part out of this patch as well.
> > > > >
> > > > > No, 'wakeup-source' is not required, it provides an indication to the
> > > > > driver that the IRQ should be used for wake, which then enables the pm
> > > > > subsystem accordingly. If 'wakup-source' is not used, we're back to
> > > > > square one of making assumptions. Specifically in this case, it would
> > > > > be assumed that all SPI based EC's are wake capable.
> > > >
> > > > Is that the wrong assumption to make? My understanding of the DT
> > > > property is that it is used to signal that this device should be treated
> > > > as a wakeup source, when otherwise it isn't treated as one. In this
> > > > case, the device has always been treated as a wakeup source so adding
> > > > the property is redundant.
> > >
> > > For SPI, it's not the wrong assumption. I was trying to drop the
> > > assumption though to match ACPI/LPC behavior.
> >
> > Ok. Is the EC always a wakeup source? Not the EC irq, the EC device.
>
> Yes. The powerd daemon enables the EC for wake [1]. At least on ACPI systems.
> [1] https://source.chromium.org/chromiumos/chromiumos/codesearch/+/81b23aeac510655f27e1d87b99b12c78146e7c37:src/platform2/power_manager/powerd/daemon.cc;l=611
>
> > > Regardless,
> > > I can update the SPI code to assume a wake capable IRQ. But I'd like
> > > to keep the prerequisite device tree patches unless there's a good
> > > reason to drop them. Specifying 'wake-source' certainly doesn't hurt
> > > anything, and there are other improvements to the OF
> > > subsystem/documentation.
> >
> > I don't see any harm in having wakeup-source in the binding, but I'd
> > prefer it is behind a different compatible string as optional, and
> > introduced when we need to have an EC that doesn't wake up the system.
>
> The 'wakeup-source' property is already optional. See patch 04 in this
> version of the series which updates the documentation surrounding the
> property. Or are you suggesting a completely new string so that we can
> be forever backward compatible? If that's the case, the property would
> indeed have to have inverted logic signifying a device that is *not*
> wakeup capable. It feels wrong to have to do something like that.

Isn't this patch series making the wakeup-source DT property required
for all existing DT nodes? I'm saying that the property is implicit
based on the compatible string "google,cros-ec-{spi,rpmsg,uart}", so
we shouldn't add the property explicitly. Just rely on the compatible
string to convey the property's existence.

>
> > Otherwise it's all future coding for a device that doesn't exist.
>
> I agree adding 'wakeup-source' is future coding for a device that
> doesn't exist. It does provide (pseudo) feature parity with ACPI
> systems though. See my comment below about ACPI GpioInt/Interrupt
> resources.
>
> > It's
> > also a potential landmine if the driver patch is backported somewhere
> > without the DTS patch (maybe the DTS is not upstream?). Someone will
> > have to debug why wakeups aren't working anymore.
>
> I can change the SPI driver so it doesn't look for the 'wakeup-source'
> property, keeping existing behavior where wakeirq is assumed. So there
> should be no issues with backporting.
>
> > > > What is the goal of this patch series? Is it to allow disabling the
> > > > wakeup capability of the EC wake irq from userspace? I can see a
> > > > possible problem where we want to disable wakeup for the EC dynamically
> > > > because either it has no child devices that are wakeup sources (e.g. no
> > > > power button, no keyboard on ARM) or userspace has disabled all the
> > > > wakeup sources for those child devices at runtime. In that case, we
> > > > would want to keep the EC irq from waking up the system from suspend. Is
> > > > that what you're doing here?
> > >
> > > The root of this patch series stems from a bug where spurious wakes
> > > are seen on Skyrim.
> >
> > Are all 24 patches needed to fix spurious wakeups? Why can't we do a DMI
> > match table for Skyrim devices and disable the wakeirq logic on that
> > platform? That would be a much more focused and targeted fix, no?
>
> It would be more focused and targeted, but I don't think it's the
> correct fix. Skyrim is not the quirk. The driver is incorrectly
> enabling the IRQ for wake even though a GPE exists for the EC to wake the AP.
> ACPI defines keywords to specify GpioInt and Interrupt
> resources as wake capable, and until recently [2], we were not
> flagging the respective resources correctly. Most ACPI Chromebooks
> have a dedicated GPE for wake, and enabling the IRQ for wake is
> unintentional IMHO.

I'm no expert in ACPI so sorry if I'm misunderstanding. The driver
unconditionally enables wake on the irq. Most other chromebooks have
added some other interrupt (GPE?) for wakeup purposes, which is
different from the irq used for IO? And this patch series tries to
figure out if enable_irq_wake() is going to fail on those devices so it
can only enable irq wake if the irq supports it? When does calling
enable_irq_wake() not return an error to properly indicate that the irq
can't wake? On skyrim devices, where presumably it needed to be marked
in ACPI differently? Or does that platform really support wake on the
irq, but we also have a GPE so enabling wake on the irq is not failing?

Having to backport 24 patches to fix a bug is not good. Can the driver
look for both an IO interrupt and a GPE and then assume the GPE is for
wakeup and the interrupt is for IO?

> [2] https://review.coreboot.org/c/coreboot/+/79373
>
> > > Copying some wording from the DTS patches:
> > > "Some Chromebooks use a separate wake pin, while others overload the
> > > interrupt for wake and IO. With the current assumption, spurious wakes
> > > can occur on systems that use a separate wake pin. It is planned to
> > > update the driver to no longer assume that the EC interrupt pin should
> > > be enabled for wake."
> > >
> > > This patch series will allow us to disable the ec_sync pin as a wake
> > > source on Skyrim as it already uses a dedicated wake gpio.
> >
> > Aha! This last sentence is the detail I've been looking for. Please put
> > these details in the commit text.
> >
> > "Skyrim devices are experiencing spurious wakeups due to the EC driver
> > always enabling the irq as a wakeup source but on Skyrim devices the EC
> > wakeup signal is a dedicated gpio separate from the irq."
> >
> > Please be direct and specific instead of writing in general terms.
>
> Sure, I can update the commit text :-)
>
> In summary, there are a lot of comments that suggest different
> solutions. Here are the options I see:
> 1. Skyrim DMI quirk
> 2a. Update EC SPI driver to assume wake capable regardless of
> 'wakeup-source' property being present
> 2b. Remove 'wakeup-source' entries from DTS

One disconnect I have is that 'wakeup-source' isn't only talking about
the interrupt in DT. It's indicating that the device itself is a wakeup
source. The interrupt controller hierarchy decides which interrupts are
wakeup capable. It sounds like in ACPI the wakeup capability is encoded
in the interrupt descriptor? DT is different here. As I stated earlier,
the EC device has always been a wakeup source, so having the DT property
there is redundant.

> 3. Leave the existing solution

How is 3 an option? I thought this patch series was fixing a bug.

>
> I'm arguing for 2a (without 2b). This keeps backward compatibility
> while adding an indication that the EC is wake capable and keeps
> closer feature parity with the ACPI/LPC interface. FWIW, others have
> already reviewed/ack'd the dts patches.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Mark Hasemeyer 10 months, 1 week ago
> Isn't this patch series making the wakeup-source DT property required
> for all existing DT nodes? I'm saying that the property is implicit
> based on the compatible string "google,cros-ec-{spi,rpmsg,uart}", so
> we shouldn't add the property explicitly. Just rely on the compatible
> string to convey the property's existence.

The current wording in 'wakeup-source.txt' states: "Nodes that
describe devices which has wakeup capability must contain a
"wakeup-source" boolean property." According to that wording, the
existing DTS does not match the expectation. This is what led me to
add the property. However, feedback from KML mentioned the wording may
be a little strong and it should be updated. Hence patch 04 in this
series.

I can revert the SPI driver to assume wake capability, which will no
longer make the wakeup-source property required. At that point,
leaving the property in the DTS simply provides an indication. Considering it
won't be required, I can drop the DTS patches that add the property.

> I'm no expert in ACPI so sorry if I'm misunderstanding. The driver
> unconditionally enables wake on the irq.

Yes.

> Most other chromebooks have
> added some other interrupt (GPE?) for wakeup purposes, which is
> different from the irq used for IO?

The GPE is used for wake and IO (It processes ACPI notify alerts).
AFAIK, the separate IRQ was introduced for latency reasons as the GPE
path was too slow.

> And this patch series tries to
> figure out if enable_irq_wake() is going to fail on those devices so it
> can only enable irq wake if the irq supports it? When does calling
> enable_irq_wake() not return an error to properly indicate that the irq
> can't wake? On skyrim devices, where presumably it needed to be marked
> in ACPI differently? Or does that platform really support wake on the
> irq, but we also have a GPE so enabling wake on the irq is not failing?

The patch series does two things:
1. Determines whether the irq should be enabled for wake, as opposed
to assuming (at least for LPC/ACPI).
2. Moves enable_irq_wake() logic to the PM subsystem.

Skyrim does _not_ support wake on irq. It uses a GPE. So the patch
series drops the assumption that irqwake should be enabled. Instead,
it polls the ACPI tables to determine whether or not the IRQ should be
enabled for wake.

> Having to backport 24 patches to fix a bug is not good.

Some of the patches were DTS related as a result of my interpretation
of 'wakeup-source.txt' (see above comment). Other patches are
tangential based on KML feedback to fix things that are orthogonal to
the bug itself.

> Can the driver
> look for both an IO interrupt and a GPE and then assume the GPE is for
> wakeup and the interrupt is for IO?

No, some boards need the IO based irq to wake, and may use both.

> > 3. Leave the existing solution
>
> How is 3 an option? I thought this patch series was fixing a bug.

I meant the solution in the existing patch train.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Stephen Boyd 10 months, 1 week ago
Quoting Mark Hasemeyer (2024-01-08 11:56:44)
> > Isn't this patch series making the wakeup-source DT property required
> > for all existing DT nodes? I'm saying that the property is implicit
> > based on the compatible string "google,cros-ec-{spi,rpmsg,uart}", so
> > we shouldn't add the property explicitly. Just rely on the compatible
> > string to convey the property's existence.
>
> The current wording in 'wakeup-source.txt' states: "Nodes that
> describe devices which has wakeup capability must contain a
> "wakeup-source" boolean property." According to that wording, the
> existing DTS does not match the expectation. This is what led me to
> add the property. However, feedback from KML mentioned the wording may
> be a little strong and it should be updated. Hence patch 04 in this
> series.
>
> I can revert the SPI driver to assume wake capability, which will no
> longer make the wakeup-source property required. At that point,
> leaving the property in the DTS simply provides an indication. Considering it
> won't be required, I can drop the DTS patches that add the property.
>
> > I'm no expert in ACPI so sorry if I'm misunderstanding. The driver
> > unconditionally enables wake on the irq.
>
> Yes.
>
> > Most other chromebooks have
> > added some other interrupt (GPE?) for wakeup purposes, which is
> > different from the irq used for IO?
>
> The GPE is used for wake and IO (It processes ACPI notify alerts).
> AFAIK, the separate IRQ was introduced for latency reasons as the GPE
> path was too slow.

Alright, I don't know what ACPI notify alerts are so most likely that is
causing me confusion.

>
> > And this patch series tries to
> > figure out if enable_irq_wake() is going to fail on those devices so it
> > can only enable irq wake if the irq supports it? When does calling
> > enable_irq_wake() not return an error to properly indicate that the irq
> > can't wake? On skyrim devices, where presumably it needed to be marked
> > in ACPI differently? Or does that platform really support wake on the
> > irq, but we also have a GPE so enabling wake on the irq is not failing?
>
> The patch series does two things:
> 1. Determines whether the irq should be enabled for wake, as opposed
> to assuming (at least for LPC/ACPI).
> 2. Moves enable_irq_wake() logic to the PM subsystem.
>
> Skyrim does _not_ support wake on irq. It uses a GPE. So the patch
> series drops the assumption that irqwake should be enabled.

Does the call to enable_irq_wake() on skyrim succeed? It seems like the
driver considers failure to enable wake on the irq as the way to figure
out if the irq supports wakeup or not. I'm trying to understand why
anything needs to be changed.

> Instead,
> it polls the ACPI tables to determine whether or not the IRQ should be
> enabled for wake.
>
> > Having to backport 24 patches to fix a bug is not good.
>
> Some of the patches were DTS related as a result of my interpretation
> of 'wakeup-source.txt' (see above comment). Other patches are
> tangential based on KML feedback to fix things that are orthogonal to
> the bug itself.

Fair enough. The fix should be isolated and be early in the series so
that we don't need to backport the whole stack to fix a bug.

>
> > Can the driver
> > look for both an IO interrupt and a GPE and then assume the GPE is for
> > wakeup and the interrupt is for IO?
>
> No, some boards need the IO based irq to wake, and may use both.

Ok.

>
> > > 3. Leave the existing solution
> >
> > How is 3 an option? I thought this patch series was fixing a bug.
>
> I meant the solution in the existing patch train.

Got it.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Mark Hasemeyer 10 months, 2 weeks ago
On Wed, Jan 3, 2024 at 3:25 PM Mark Hasemeyer <markhas@chromium.org> wrote:
>
> > The DTS patch would go through the platform maintainer tree and be
> > pulled into the soc tree and sent up to mainline from there. The
> > platform/chrome patch would go through chrome platform tree and then to
> > mainline. The bisection hole will be real.
>
> I thought it would all get merged in the next merge window. How are
> bifurcations like this normally dealt with? Does one wait for the
> first series of patches to land in mainline before submitting
> dependent patches? That could take two merge windows.

The DTS dependency problem must be an exception? How are other
dependency problems resolved? For example, this patch relies on
changes to 'platform_get_irq()' which is in drivers/base/platform.c,
which I imagine is in a different subsystem and gets merged into a
different maintainer's tree.
Re: [PATCH v4 24/24] platform/chrome: cros_ec: Use PM subsystem to manage wakeirq
Posted by Stephen Boyd 10 months, 2 weeks ago
Quoting Mark Hasemeyer (2024-01-03 14:45:06)
> On Wed, Jan 3, 2024 at 3:25 PM Mark Hasemeyer <markhas@chromium.org> wrote:
> >
> > > The DTS patch would go through the platform maintainer tree and be
> > > pulled into the soc tree and sent up to mainline from there. The
> > > platform/chrome patch would go through chrome platform tree and then to
> > > mainline. The bisection hole will be real.
> >
> > I thought it would all get merged in the next merge window. How are
> > bifurcations like this normally dealt with? Does one wait for the
> > first series of patches to land in mainline before submitting
> > dependent patches? That could take two merge windows.
>
> The DTS dependency problem must be an exception? How are other
> dependency problems resolved? For example, this patch relies on
> changes to 'platform_get_irq()' which is in drivers/base/platform.c,
> which I imagine is in a different subsystem and gets merged into a
> different maintainer's tree.

Cross tree code dependencies like that are usually resolved by having a
maintainer ack the patch and another maintainer take the code parts.

DT bindings are not supposed to be changed in a way that would break
compatibility with existing code, hence the compatible property. It's a
backwards incompatible change to add the wakeup-source property to the
EC binding and make the driver rely on that property to maintain the
previous code behavior. That's why I keep saying that if you want to add
a wakeup-source property and make the driver act the same as before it
must be done with a new compatible string. Because the driver has always
set the device to wakeup, the compatible has implicitly conveyed that
the wakeup-source property is present.