Skip to contentSkip to conversation

PCI / ACPI PM: Run-time PM, PME handling and PCI bus type callbacks

RFC PATCH In review Rafael J. Wysocki 3 participants 23 messages Sep 13, 2009 – Oct 6, 2009

Subsystems (5)
ACPI PCI Suspend to RAM Hibernation +1 more subsystems
lore
Share & tools
lore
#0 lore raw

cover PCI / ACPI PM: Run-time PM, PME handling and PCI bus type callbacks

Hi,

The following patches implement experimental run-time PM support for PCI bus type.

[1/4] - add PME status checking/clearing
[2/4] - Implement native PCIe PME driver for root ports [3/4] - Add ACPI platform support for PCI PME signaling [4/4] - Implement run-time PM callbacks for the PCI bus type

All of this is highly theoretical, because I didn't have the time to add runtime PM callbacks to any drivers I can test, but at least the patchset compiles and doesn't break things outright.

Comments welcome.

Thanks,
Rafael

#1 lore raw

RFC PATCH 1/4 PCI PM: Add function for checking PME status of devices

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

Add function pci_check_pme_status() that will check the PME status bit of given device and clear it along with the PME enable bit. It will be necessary for PCI run-time power management.

Based on a patch from Shaohua Li <[email protected]>

---
 drivers/pci/pci.c |   35 +++++++++++++++++++++++++++++++++++
 drivers/pci/pci.h |    1 +
 2 files changed, 36 insertions(+)
drivers/pci/pci.h +1 -0
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -48,6 +48,7 @@ struct pci_platform_pm_ops {
 extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
 extern void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
 extern void pci_disable_enabled_device(struct pci_dev *dev);
+extern bool pci_check_pme_status(struct pci_dev *dev);
 extern void pci_pm_init(struct pci_dev *dev);
 extern void platform_pci_wakeup_init(struct pci_dev *dev);
 extern void pci_allocate_cap_save_buffers(struct pci_dev *dev);
drivers/pci/pci.c +35 -0
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -1161,6 +1161,41 @@ int pci_set_pcie_reset_state(struct pci_
 }
 
 /**
+ * pci_check_pme_status - Check if given device has generated PME.
+ * @dev: Device to check.
+ *
+ * Check the PME status of the device, clear PME status and PME enable.  Return
+ * 'true' if PME has been generated by the device (and hasn't been spurious) or
+ * 'false' otherwise.
+ */
+bool pci_check_pme_status(struct pci_dev *dev)
+{
+	int pmcsr_pos;
+	u16 pmcsr;
+	bool ret = false;
+
+	if (!dev->pm_cap)
+		return false;
+
+	pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
+	/* clear PME status and disable PME to avoid interrupt flood */
+	pci_read_config_word(dev, pmcsr_pos, &pmcsr);
+	if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
+		return false;
+
+	pmcsr |= PCI_PM_CTRL_PME_STATUS;
+	/* Ignore spurious PME or clear PME enable if it's not spurious. */
+	if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
+		pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
+		ret = true;
+	}
+
+	pci_write_config_word(dev, pmcsr_pos, pmcsr);
+
+	return ret;
+}
+
+/**
  * pci_pme_capable - check the capability of PCI device to generate PME#
  * @dev: PCI device to handle.
  * @state: PCI state from which device will issue PME#.
#2 lore raw

Re: [RFC][PATCH 1/4] PCI PM: Add function for checking PME status of devices

Quoted 3 lines · Rafael J. Wysocki

Add function pci_check_pme_status() that will check the PME status bit of given device and clear it along with the PME enable bit. It will be necessary for PCI run-time power management.

I think the comment's slightly misleading - some PCI run-time power management will work without any PME code. But that's not terribly important.

Signature · 1 line
Matthew Garrett | [email protected]
#3 lore raw

Re: [RFC][PATCH 1/4] PCI PM: Add function for checking PME status of devices

In reply to Rafael J. Wysocki
Quoted 3 lines · Rafael J. Wysocki

Add function pci_check_pme_status() that will check the PME status bit of given device and clear it along with the PME enable bit. It will be necessary for PCI run-time power management.

I think the comment's slightly misleading - some PCI run-time power management will work without any PME code. But that's not terribly important.

Signature · 1 line
Matthew Garrett | [email protected]
#4 lore raw

RFC PATCH 1/4 PCI PM: Add function for checking PME status of devices

In reply to Rafael J. Wysocki

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

Add function pci_check_pme_status() that will check the PME status bit of given device and clear it along with the PME enable bit. It will be necessary for PCI run-time power management.

Based on a patch from Shaohua Li <[email protected]>

---
 drivers/pci/pci.c |   35 +++++++++++++++++++++++++++++++++++
 drivers/pci/pci.h |    1 +
 2 files changed, 36 insertions(+)
drivers/pci/pci.h +1 -0
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -48,6 +48,7 @@ struct pci_platform_pm_ops {
 extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
 extern void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
 extern void pci_disable_enabled_device(struct pci_dev *dev);
+extern bool pci_check_pme_status(struct pci_dev *dev);
 extern void pci_pm_init(struct pci_dev *dev);
 extern void platform_pci_wakeup_init(struct pci_dev *dev);
 extern void pci_allocate_cap_save_buffers(struct pci_dev *dev);
drivers/pci/pci.c +35 -0
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -1161,6 +1161,41 @@ int pci_set_pcie_reset_state(struct pci_
 }
 
 /**
+ * pci_check_pme_status - Check if given device has generated PME.
+ * @dev: Device to check.
+ *
+ * Check the PME status of the device, clear PME status and PME enable.  Return
+ * 'true' if PME has been generated by the device (and hasn't been spurious) or
+ * 'false' otherwise.
+ */
+bool pci_check_pme_status(struct pci_dev *dev)
+{
+	int pmcsr_pos;
+	u16 pmcsr;
+	bool ret = false;
+
+	if (!dev->pm_cap)
+		return false;
+
+	pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
+	/* clear PME status and disable PME to avoid interrupt flood */
+	pci_read_config_word(dev, pmcsr_pos, &pmcsr);
+	if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
+		return false;
+
+	pmcsr |= PCI_PM_CTRL_PME_STATUS;
+	/* Ignore spurious PME or clear PME enable if it's not spurious. */
+	if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
+		pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
+		ret = true;
+	}
+
+	pci_write_config_word(dev, pmcsr_pos, pmcsr);
+
+	return ret;
+}
+
+/**
  * pci_pme_capable - check the capability of PCI device to generate PME#
  * @dev: PCI device to handle.
  * @state: PCI state from which device will issue PME#.
#5 lore raw

RFC PATCH 2/4 PCI PM: PCIe PME root port service driver (rev. 3)

In reply to Rafael J. Wysocki

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

PCIe native PME detection mechanism is based on interrupts generated by root ports or event collectors every time a PCIe device sends a PME message upstream.

Once a PME message has been sent by an endpoint device and received by its root port (or event collector in the case of root complex integrated endpoints), the Requester ID from the message header is registered in the root port's Root Status register. At the same time, the PME Status bit of the Root Status register is set to indicate that there's a PME to handle. If PCIe PME interrupt is enabled for the root port, it generates an interrupt once the PME Status has been set. After receiving the interrupt, the kernel can identify the PCIe device that generated the PME using the Requester ID from the root port's Root Status register. [For details, see PCI Express Base Specification, Rev. 2.0.]

Implement a driver for the PCIe PME root port service working in accordance with the above description.

Based on a patch from Shaohua Li <[email protected]>.

---
 drivers/pci/pci-acpi.c               |    4 
 drivers/pci/pcie/Kconfig             |    4 
 drivers/pci/pcie/Makefile            |    2 
 drivers/pci/pcie/pme/Makefile        |    8 
 drivers/pci/pcie/pme/pcie_pme.c      |  491 +++++++++++++++++++++++++++++++++++
 drivers/pci/pcie/pme/pcie_pme.h      |   28 +
 drivers/pci/pcie/pme/pcie_pme_acpi.c |   54 +++
 include/linux/pci.h                  |    6 
 8 files changed, 595 insertions(+), 2 deletions(-)
drivers/pci/pcie/Kconfig +4 -0
Index: linux-2.6/drivers/pci/pcie/Kconfig
===================================================================
@@ -46,3 +46,7 @@ config PCIEASPM_DEBUG
 	help
 	  This enables PCI Express ASPM debug support. It will add per-device
 	  interface to control ASPM.
+
+config PCIE_PME
+	def_bool y
+	depends on PCIEPORTBUS && PM_RUNTIME && EXPERIMENTAL
drivers/pci/pcie/Makefile +2 -0
Index: linux-2.6/drivers/pci/pcie/Makefile
===================================================================
@@ -11,3 +11,5 @@ obj-$(CONFIG_PCIEPORTBUS)	+= pcieportdrv
 
 # Build PCI Express AER if needed
 obj-$(CONFIG_PCIEAER)		+= aer/
+
+obj-$(CONFIG_PCIE_PME) += pme/
drivers/pci/pcie/pme/pcie_pme.c new file +491 -0

491 lines not on this page · raw message

drivers/pci/pcie/pme/pcie_pme_acpi.c new file +54 -0

54 lines not on this page · raw message

drivers/pci/pcie/pme/Makefile new file +8 -0
Index: linux-2.6/drivers/pci/pcie/pme/Makefile
===================================================================
@@ -0,0 +1,8 @@
+#
+# Makefile for PCI-Express Root Port PME signaling driver
+#
+
+obj-$(CONFIG_PCIE_PME) += pmedriver.o
+
+pmedriver-objs := pcie_pme.o
+pmedriver-$(CONFIG_ACPI) += pcie_pme_acpi.o
drivers/pci/pcie/pme/pcie_pme.h new file +28 -0
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.h
===================================================================
@@ -0,0 +1,28 @@
+/*
+ * drivers/pci/pcie/pme/pcie_pme.h
+ *
+ * PCI Express Root Port PME signaling support
+ *
+ * Copyright (C) 2009 Rafael J. Wysocki <[email protected]>, Novell Inc.
+ */
+
+#ifndef _PCIE_PME_H_
+#define _PCIE_PME_H_
+
+struct pcie_device;
+
+#ifdef CONFIG_ACPI
+extern int pcie_pme_acpi_setup(struct pcie_device *srv);
+
+static inline int pcie_pme_platform_notify(struct pcie_device *srv)
+{
+	return pcie_pme_acpi_setup(srv);
+}
+#else /* !CONFIG_ACPI */
+static inline int pcie_pme_platform_notify(struct pcie_device *srv)
+{
+	return 0;
+}
+#endif /* !CONFIG_ACPI */
+
+#endif
include/linux/pci.h +6 -0
Index: linux-2.6/include/linux/pci.h
===================================================================
@@ -901,6 +901,12 @@ extern void pcie_set_ecrc_checking(struc
 extern void pcie_ecrc_get_policy(char *str);
 #endif
 
+#ifndef CONFIG_PCIE_PME
+static inline bool pcie_pme_enabled(struct pci_dev *dev) { return false; }
+#else
+extern bool pcie_pme_enabled(struct pci_dev *dev);
+#endif
+
 #define pci_enable_msi(pdev)	pci_enable_msi_block(pdev, 1)
 
 #ifdef CONFIG_HT_IRQ
drivers/pci/pci-acpi.c +2 -2
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
@@ -116,7 +116,7 @@ static void acpi_pci_propagate_wakeup_en
 		int ret;
 
 		ret = acpi_pm_device_sleep_wake(&bridge->dev, enable);
-		if (!ret || bridge->is_pcie)
+		if (!ret || pcie_pme_enabled(bridge))
 			return;
 		bus = bus->parent;
 	}
@@ -131,7 +131,7 @@ static int acpi_pci_sleep_wake(struct pc
 	if (acpi_pci_can_wakeup(dev))
 		return acpi_pm_device_sleep_wake(&dev->dev, enable);
 
-	if (!dev->is_pcie)
+	if (!pcie_pme_enabled(dev))
 		acpi_pci_propagate_wakeup_enable(dev->bus, enable);
 
 	return 0;
#6 lore raw

RFC PATCH 2/4 PCI PM: PCIe PME root port service driver (rev. 3)

In reply to Rafael J. Wysocki

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

PCIe native PME detection mechanism is based on interrupts generated by root ports or event collectors every time a PCIe device sends a PME message upstream.

Once a PME message has been sent by an endpoint device and received by its root port (or event collector in the case of root complex integrated endpoints), the Requester ID from the message header is registered in the root port's Root Status register. At the same time, the PME Status bit of the Root Status register is set to indicate that there's a PME to handle. If PCIe PME interrupt is enabled for the root port, it generates an interrupt once the PME Status has been set. After receiving the interrupt, the kernel can identify the PCIe device that generated the PME using the Requester ID from the root port's Root Status register. [For details, see PCI Express Base Specification, Rev. 2.0.]

Implement a driver for the PCIe PME root port service working in accordance with the above description.

Based on a patch from Shaohua Li <[email protected]>.

---
 drivers/pci/pci-acpi.c               |    4 
 drivers/pci/pcie/Kconfig             |    4 
 drivers/pci/pcie/Makefile            |    2 
 drivers/pci/pcie/pme/Makefile        |    8 
 drivers/pci/pcie/pme/pcie_pme.c      |  491 +++++++++++++++++++++++++++++++++++
 drivers/pci/pcie/pme/pcie_pme.h      |   28 +
 drivers/pci/pcie/pme/pcie_pme_acpi.c |   54 +++
 include/linux/pci.h                  |    6 
 8 files changed, 595 insertions(+), 2 deletions(-)
drivers/pci/pcie/Kconfig +4 -0
Index: linux-2.6/drivers/pci/pcie/Kconfig
===================================================================
@@ -46,3 +46,7 @@ config PCIEASPM_DEBUG
 	help
 	  This enables PCI Express ASPM debug support. It will add per-device
 	  interface to control ASPM.
+
+config PCIE_PME
+	def_bool y
+	depends on PCIEPORTBUS && PM_RUNTIME && EXPERIMENTAL
drivers/pci/pcie/Makefile +2 -0
Index: linux-2.6/drivers/pci/pcie/Makefile
===================================================================
@@ -11,3 +11,5 @@ obj-$(CONFIG_PCIEPORTBUS)	+= pcieportdrv
 
 # Build PCI Express AER if needed
 obj-$(CONFIG_PCIEAER)		+= aer/
+
+obj-$(CONFIG_PCIE_PME) += pme/
drivers/pci/pcie/pme/pcie_pme.c new file +491 -0

491 lines not on this page · raw message

drivers/pci/pcie/pme/pcie_pme_acpi.c new file +54 -0

54 lines not on this page · raw message

drivers/pci/pcie/pme/Makefile new file +8 -0
Index: linux-2.6/drivers/pci/pcie/pme/Makefile
===================================================================
@@ -0,0 +1,8 @@
+#
+# Makefile for PCI-Express Root Port PME signaling driver
+#
+
+obj-$(CONFIG_PCIE_PME) += pmedriver.o
+
+pmedriver-objs := pcie_pme.o
+pmedriver-$(CONFIG_ACPI) += pcie_pme_acpi.o
drivers/pci/pcie/pme/pcie_pme.h new file +28 -0
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.h
===================================================================
@@ -0,0 +1,28 @@
+/*
+ * drivers/pci/pcie/pme/pcie_pme.h
+ *
+ * PCI Express Root Port PME signaling support
+ *
+ * Copyright (C) 2009 Rafael J. Wysocki <[email protected]>, Novell Inc.
+ */
+
+#ifndef _PCIE_PME_H_
+#define _PCIE_PME_H_
+
+struct pcie_device;
+
+#ifdef CONFIG_ACPI
+extern int pcie_pme_acpi_setup(struct pcie_device *srv);
+
+static inline int pcie_pme_platform_notify(struct pcie_device *srv)
+{
+	return pcie_pme_acpi_setup(srv);
+}
+#else /* !CONFIG_ACPI */
+static inline int pcie_pme_platform_notify(struct pcie_device *srv)
+{
+	return 0;
+}
+#endif /* !CONFIG_ACPI */
+
+#endif
include/linux/pci.h +6 -0
Index: linux-2.6/include/linux/pci.h
===================================================================
@@ -901,6 +901,12 @@ extern void pcie_set_ecrc_checking(struc
 extern void pcie_ecrc_get_policy(char *str);
 #endif
 
+#ifndef CONFIG_PCIE_PME
+static inline bool pcie_pme_enabled(struct pci_dev *dev) { return false; }
+#else
+extern bool pcie_pme_enabled(struct pci_dev *dev);
+#endif
+
 #define pci_enable_msi(pdev)	pci_enable_msi_block(pdev, 1)
 
 #ifdef CONFIG_HT_IRQ
drivers/pci/pci-acpi.c +2 -2
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
@@ -116,7 +116,7 @@ static void acpi_pci_propagate_wakeup_en
 		int ret;
 
 		ret = acpi_pm_device_sleep_wake(&bridge->dev, enable);
-		if (!ret || bridge->is_pcie)
+		if (!ret || pcie_pme_enabled(bridge))
 			return;
 		bus = bus->parent;
 	}
@@ -131,7 +131,7 @@ static int acpi_pci_sleep_wake(struct pc
 	if (acpi_pci_can_wakeup(dev))
 		return acpi_pm_device_sleep_wake(&dev->dev, enable);
 
-	if (!dev->is_pcie)
+	if (!pcie_pme_enabled(dev))
 		acpi_pci_propagate_wakeup_enable(dev->bus, enable);
 
 	return 0;
#7 lore raw

RFC PATCH 3/4 PCI / ACPI PM: Platform support for PCI PME wake-up

In reply to Rafael J. Wysocki

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

Although the majority of PCI devices can generate PMEs that in principle may be used to wake up devices suspended at run time, platform support is generally necessary to convert PMEs into wake-up events that can be delivered to the kernel. If ACPI is used for this purpose, a PME generated by a PCI device will trigger the ACPI GPE associated with the device to generate an ACPI wake-up event that we can set up a handler for, provided that everything is configured correctly.

Unfortunately, the subset of PCI devices that have GPEs associated with them is quite limited and the other devices have to rely on the GPEs associated with their upstream bridges and, possibly, the root bridge to generate ACPI wake-up events in response to PMEs from them.

Add ACPI platform support for PCI PME wake-up:
o Add function acpi_device_run_wake() allowing us to enable or disable the GPE associated with given device to generate run-time wake-up events.
o Add a new PCI platform callback ->run_wake() to struct pci_platform_pm_ops allowing us to enable the platform to generate wake-up events for given device.
o Implemet that callback for the ACPI platform using acpi_device_run_wake().
o Define ACPI wake-up handlers for PCI devices and PCI buses and make the PCI-ACPI binding code install wake-up notifiers for devices associated with GPEs that can be used for wake-up.
o Add function pci_platform_run_wake() that can be used to enable (or disable) the platform to generate wake-up events for given PCI device using the ->run_wake() platform callback.

Based on a patch from Matthew Garrett.

---
 drivers/acpi/pci_bind.c  |    6 ++++++
 drivers/acpi/pci_root.c  |    5 +++++
 drivers/acpi/sleep.c     |    4 ++--
 drivers/acpi/wakeup.c    |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/pci/pci-acpi.c   |   42 ++++++++++++++++++++++++++++++++++++++++++
 drivers/pci/pci.c        |   41 +++++++++++++++++++++++++++++++++++++++++
 drivers/pci/pci.h        |   14 +++++++++++---
 include/acpi/acpi_bus.h  |   13 ++++++++++---
 include/linux/pci-acpi.h |    3 +++
 kernel/power/Kconfig     |    5 +++++
 10 files changed, 172 insertions(+), 8 deletions(-)
drivers/pci/pci.h +11 -3
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -29,10 +29,14 @@ extern int pci_mmap_fits(struct pci_dev 
  *                platform; to be used during system-wide transitions from a
  *                sleeping state to the working state and vice versa
  *
- * @can_wakeup: returns 'true' if given device is capable of waking up the
- *              system from a sleeping state
+ * @can_wakeup: returns 'true' if the plaform can generate wake-up events for
+ *		given device.
  *
- * @sleep_wake: enables/disables the system wake up capability of given device
+ * @sleep_wake: enables/disables the wake-up capability of given device
+ *
+ * @run_wake: enables/disables the platform to generate run-time wake-up events
+ *		for given device (the device's wake-up capability has to be
+ *		enabled by @sleep_wake for this feature to work)
  *
  * If given platform is generally capable of power managing PCI devices, all of
  * these callbacks are mandatory.
@@ -43,12 +47,16 @@ struct pci_platform_pm_ops {
 	pci_power_t (*choose_state)(struct pci_dev *dev);
 	bool (*can_wakeup)(struct pci_dev *dev);
 	int (*sleep_wake)(struct pci_dev *dev, bool enable);
+	int (*run_wake)(struct pci_dev *dev, bool enable);
 };
 
 extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
+extern int pci_platform_run_wake(struct pci_dev *dev, bool enable);
 extern void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
 extern void pci_disable_enabled_device(struct pci_dev *dev);
 extern bool pci_check_pme_status(struct pci_dev *dev);
+extern void pci_pme_wakeup_bus(struct pci_bus *bus);
+extern void pci_pme_wakeup(struct pci_dev *dev);
 extern void pci_pm_init(struct pci_dev *dev);
 extern void platform_pci_wakeup_init(struct pci_dev *dev);
 extern void pci_allocate_cap_save_buffers(struct pci_dev *dev);
drivers/pci/pci-acpi.c +42 -0

60 lines not on this page · raw message

drivers/pci/pci.c +41 -0

59 lines not on this page · raw message

drivers/acpi/wakeup.c +47 -0

53 lines not on this page · raw message

include/acpi/acpi_bus.h +10 -3
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
@@ -375,21 +375,28 @@ int acpi_is_root_bridge(acpi_handle);
 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int, unsigned int);
 #define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)((dev)->archdata.acpi_handle))
 
-#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_PM_WAKEUP
 int acpi_pm_device_sleep_state(struct device *, int *);
 int acpi_pm_device_sleep_wake(struct device *, bool);
-#else /* !CONFIG_PM_SLEEP */
+int acpi_device_run_wake(struct device *, bool);
+#else /* !CONFIG_PM_WAKEUP */
 static inline int acpi_pm_device_sleep_state(struct device *d, int *p)
 {
 	if (p)
 		*p = ACPI_STATE_D0;
 	return ACPI_STATE_D3;
 }
+
 static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
 {
 	return -ENODEV;
 }
-#endif /* !CONFIG_PM_SLEEP */
+
+static inline int acpi_device_run_wake(struct device *dev, bool enable)
+{
+	return -ENODEV;
+}
+#endif /* !CONFIG_PM_WAKEUP */
 
 #endif				/* CONFIG_ACPI */
 
kernel/power/Kconfig +5 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -236,3 +236,8 @@ config PM_RUNTIME
 	  and the bus type drivers of the buses the devices are on are
 	  responsible for the actual handling of the autosuspend requests and
 	  wake-up events.
+
+config PM_WAKEUP
+	bool
+	depends on SUSPEND || HIBERNATION || PM_RUNTIME
+	default y
drivers/acpi/sleep.c +2 -2
Index: linux-2.6/drivers/acpi/sleep.c
===================================================================
@@ -594,7 +594,7 @@ int acpi_suspend(u32 acpi_state)
 	return -EINVAL;
 }
 
-#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_PM_WAKEUP
 /**
  *	acpi_pm_device_sleep_state - return preferred power state of ACPI device
  *		in the system sleep state given by %acpi_target_sleep_state
@@ -709,7 +709,7 @@ int acpi_pm_device_sleep_wake(struct dev
 
 	return error;
 }
-#endif
+#endif /* CONFIG_PM_WAKEUP */
 
 static void acpi_power_off_prepare(void)
 {
drivers/acpi/pci_root.c +5 -0
Index: linux-2.6/drivers/acpi/pci_root.c
===================================================================
@@ -576,6 +576,11 @@ static int __devinit acpi_pci_root_add(s
 	if (flags != base_flags)
 		acpi_pci_osc_support(root, flags);
 
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_bus_wakeup,
+						&root->bus->dev);
+
 	return 0;
 
 end:
include/linux/pci-acpi.h +3 -0
Index: linux-2.6/include/linux/pci-acpi.h
===================================================================
@@ -10,6 +10,9 @@
 
 #include <linux/acpi.h>
 
+extern void pci_acpi_bus_wakeup(acpi_handle handle, u32 event, void *data);
+extern void pci_acpi_device_wakeup(acpi_handle handle, u32 event, void *data);
+
 #ifdef CONFIG_ACPI
 static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
 {
drivers/acpi/pci_bind.c +6 -0
Index: linux-2.6/drivers/acpi/pci_bind.c
===================================================================
@@ -26,6 +26,7 @@
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/pci.h>
+#include <linux/pci-acpi.h>
 #include <linux/acpi.h>
 #include <acpi/acpi_bus.h>
 #include <acpi/acpi_drivers.h>
@@ -94,6 +95,11 @@ static int acpi_pci_bind(struct acpi_dev
 
 	acpi_pci_irq_add_prt(device->handle, bus);
 
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+
 out:
 	pci_dev_put(dev);
 	return 0;
#8 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 163

Quoted 4 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

drivers/acpi/pci_bind.c static int acpi_pci_bind(struct acpi_dev · line 102

Quoted 5 lines
drivers/acpi/pci_bind.c +5 -0
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+

I think this will fail for the root bridge if acpiphp has already grabbed it to check for hotplug events.

Signature · 1 line
Matthew Garrett | [email protected]
#9 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

Quoted 10 lines · Matthew Garrett

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 163

Quoted 4 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

You're right, I overlooked that. Some kind of refcounting is needed here.

Quoted 8 lines

drivers/acpi/pci_bind.c static int acpi_pci_bind(struct acpi_dev · line 102

Quoted 5 lines
drivers/acpi/pci_bind.c +5 -0
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+

I think this will fail for the root bridge if acpiphp has already grabbed it to check for hotplug events.

The root bridge is not a struct pci_dev and this is called only for these.

Thanks,
Rafael

#10 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

Quoted 12 lines · Rafael J. Wysocki
Quoted 10 lines · Matthew Garrett

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 163

Quoted 4 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

Quoted 1 line

You're right, I overlooked that. Some kind of refcounting is needed here.

I've sent patches to implement this at the GPE level, which also change the API for requesting them. I'm waiting on feedback from Bob Moore.

Quoted 10 lines
Quoted 8 lines

drivers/acpi/pci_bind.c static int acpi_pci_bind(struct acpi_dev · line 102

Quoted 5 lines
drivers/acpi/pci_bind.c +5 -0
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+

I think this will fail for the root bridge if acpiphp has already grabbed it to check for hotplug events.

The root bridge is not a struct pci_dev and this is called only for these.

Are you sure? When I did this I saw conflicts with acpiphp. I may have my terminology confused. I sent another patch that abstract that out.

Signature · 1 line
Matthew Garrett | [email protected]
#11 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

In reply to Rafael J. Wysocki
Quoted 12 lines · Rafael J. Wysocki
Quoted 10 lines · Matthew Garrett

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 163

Quoted 4 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

Quoted 1 line

You're right, I overlooked that. Some kind of refcounting is needed here.

I've sent patches to implement this at the GPE level, which also change the API for requesting them. I'm waiting on feedback from Bob Moore.

Quoted 10 lines
Quoted 8 lines

drivers/acpi/pci_bind.c static int acpi_pci_bind(struct acpi_dev · line 102

Quoted 5 lines
drivers/acpi/pci_bind.c +5 -0
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+

I think this will fail for the root bridge if acpiphp has already grabbed it to check for hotplug events.

The root bridge is not a struct pci_dev and this is called only for these.

Are you sure? When I did this I saw conflicts with acpiphp. I may have my terminology confused. I sent another patch that abstract that out.

Signature · 1 line
Matthew Garrett | [email protected]
#12 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

Quoted 27 lines · Matthew Garrett
Quoted 12 lines · Rafael J. Wysocki
Quoted 10 lines · Matthew Garrett

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 163

Quoted 4 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

Quoted 1 line

You're right, I overlooked that. Some kind of refcounting is needed here.

I've sent patches to implement this at the GPE level, which also change the API for requesting them. I'm waiting on feedback from Bob Moore.

Quoted 9 lines
Quoted 8 lines

drivers/acpi/pci_bind.c static int acpi_pci_bind(struct acpi_dev · line 102

Quoted 5 lines
drivers/acpi/pci_bind.c +5 -0
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+

I think this will fail for the root bridge if acpiphp has already grabbed it to check for hotplug events.

Quoted 3 lines
Quoted 1 line

The root bridge is not a struct pci_dev and this is called only for these.

Are you sure?

Yup. ACPI associates the root bridge handle with the struct device created by pci_create_bus() as the bridge of the root bus.

Quoted 2 lines

When I did this I saw conflicts with acpiphp. I may have my terminology confused. I sent another patch that abstract that out.

I'll have a look at acpihp, then.

Thanks,
Rafael

#13 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

In reply to Matthew Garrett
Quoted 27 lines · Matthew Garrett
Quoted 12 lines · Rafael J. Wysocki
Quoted 10 lines · Matthew Garrett

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 163

Quoted 4 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

Quoted 1 line

You're right, I overlooked that. Some kind of refcounting is needed here.

I've sent patches to implement this at the GPE level, which also change the API for requesting them. I'm waiting on feedback from Bob Moore.

Quoted 9 lines
Quoted 8 lines

drivers/acpi/pci_bind.c static int acpi_pci_bind(struct acpi_dev · line 102

Quoted 5 lines
drivers/acpi/pci_bind.c +5 -0
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+

I think this will fail for the root bridge if acpiphp has already grabbed it to check for hotplug events.

Quoted 3 lines
Quoted 1 line

The root bridge is not a struct pci_dev and this is called only for these.

Are you sure?

Yup. ACPI associates the root bridge handle with the struct device created by pci_create_bus() as the bridge of the root bus.

Quoted 2 lines

When I did this I saw conflicts with acpiphp. I may have my terminology confused. I sent another patch that abstract that out.

I'll have a look at acpihp, then.

Thanks,
Rafael

#14 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

In reply to Matthew Garrett
Quoted 13 lines · Matthew Garrett
Quoted 12 lines · Rafael J. Wysocki
Quoted 10 lines · Matthew Garrett

Review

Quoted 4 lines · Rafael J. Wysocki
(quoted diff) +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

Quoted 4 lines
Quoted 1 line

You're right, I overlooked that. Some kind of refcounting is needed here.

I've sent patches to implement this at the GPE level, which also change the API for requesting them. I'm waiting on feedback from Bob Moore.

In the meantime I realized there's one more thing we need to take care of. Namely, if a wake-up GPE is shared between multiple devices, it need not be necessary to install notify handlers for all of them. For example, if one of these devices is the root bridge, we will walk all of the hierarchy under it looking for devices that have PME set, so we need not install notify handlers for any devices that share the wake-up GPE with the root bridge. Similarly, there's no need to install a notify handler for a device that shares the wake-up GPE with a bridge (non-root) it is under.

Taking that into account I have prepared another version of the @subject patch which is appended below. It also takes the PM vs hotplug issue into account. The idea is pretty straightforward, everything should be clear from the changelog and the comments within the patch.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 2)

Although the majority of PCI devices can generate PMEs that in principle may be used to wake up devices suspended at run time, platform support is generally necessary to convert PMEs into wake-up events that can be delivered to the kernel. If ACPI is used for this purpose, a PME generated by a PCI device will trigger the ACPI GPE associated with the device to generate an ACPI wake-up event that we can set up a handler for, provided that everything is configured correctly.

Unfortunately, the subset of PCI devices that have GPEs associated with them is quite limited and the other devices have to rely on the GPEs associated with their upstream bridges and, possibly, the root bridge to generate ACPI wake-up events in response to PMEs from them. Moreover, ACPI devices tend to share wake-up GPEs, in which cases it makes sense to install an ACPI notify handler for only one of them and "bind" the other devices to it. Furthermore, ACPI-based PCI hotplug also uses ACPI notify handlers that in general may conflict with the PM notify handlers, unless this issue is specifically taken care of.

Add ACPI platform support for PCI PME wake-up:
o Add a framework making is possible to use ACPI system notify handlers for both PM and hotplug at the same time and to take the wake-up GPE sharing into account.
o Add function acpi_device_run_wake() allowing us to enable or disable the GPE associated with given device to generate run-time wake-up events.
o Add a new PCI platform callback ->run_wake() to struct pci_platform_pm_ops allowing us to enable the platform to generate wake-up events for given device and implemet that callback for the ACPI platform.
o Define ACPI wake-up handlers for PCI devices and PCI buses and make the PCI-ACPI binding code register wake-up notifiers for devices associated with wake-up GPEs.
o Add function pci_platform_run_wake() that can be used to enable (or disable) the platform to generate wake-up events for given PCI device using the ->run_wake() platform callback.

Based on a patch from Matthew Garrett.

---
 drivers/acpi/pci_bind.c            |   19 
 drivers/acpi/pci_root.c            |    5 
 drivers/acpi/sleep.c               |    4 
 drivers/acpi/wakeup.c              |   50 ++
 drivers/pci/hotplug/acpiphp_glue.c |   23 -
 drivers/pci/pci-acpi.c             |  706 +++++++++++++++++++++++++++++++++++++
 drivers/pci/pci.c                  |   32 +
 drivers/pci/pci.h                  |   19 
 include/acpi/acpi_bus.h            |   20 -
 include/linux/pci-acpi.h           |   14 
 kernel/power/Kconfig               |    5 
 11 files changed, 874 insertions(+), 23 deletions(-)
drivers/pci/pci.h +16 -3
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -30,10 +30,14 @@ int pci_probe_reset_function(struct pci_
  *                platform; to be used during system-wide transitions from a
  *                sleeping state to the working state and vice versa
  *
- * @can_wakeup: returns 'true' if given device is capable of waking up the
- *              system from a sleeping state
+ * @can_wakeup: returns 'true' if the plaform can generate wake-up events for
+ *		given device.
  *
- * @sleep_wake: enables/disables the system wake up capability of given device
+ * @sleep_wake: enables/disables the wake-up capability of given device
+ *
+ * @run_wake: enables/disables the platform to generate run-time wake-up events
+ *		for given device (the device's wake-up capability has to be
+ *		enabled by @sleep_wake for this feature to work)
  *
  * If given platform is generally capable of power managing PCI devices, all of
  * these callbacks are mandatory.
@@ -44,16 +48,25 @@ struct pci_platform_pm_ops {
 	pci_power_t (*choose_state)(struct pci_dev *dev);
 	bool (*can_wakeup)(struct pci_dev *dev);
 	int (*sleep_wake)(struct pci_dev *dev, bool enable);
+	int (*run_wake)(struct pci_dev *dev, bool enable);
 };
 
 extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
+extern int pci_platform_run_wake(struct pci_dev *dev, bool enable);
 extern void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
 extern void pci_disable_enabled_device(struct pci_dev *dev);
 extern bool pci_check_pme_status(struct pci_dev *dev);
+extern int __pci_pme_wakeup(struct pci_dev *dev, void *ign);
+extern void pci_pme_wakeup_bus(struct pci_bus *bus);
 extern void pci_pm_init(struct pci_dev *dev);
 extern void platform_pci_wakeup_init(struct pci_dev *dev);
 extern void pci_allocate_cap_save_buffers(struct pci_dev *dev);
 
+static inline void pci_pme_wakeup(struct pci_dev *dev)
+{
+	__pci_pme_wakeup(dev, NULL);
+}
+
 static inline bool pci_is_bridge(struct pci_dev *pci_dev)
 {
 	return !!(pci_dev->subordinate);
drivers/pci/pci-acpi.c +706 -0

724 lines not on this page · raw message

drivers/pci/pci.c +32 -0
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -21,6 +21,7 @@
 #include <linux/interrupt.h>
 #include <asm/dma.h>	/* isa_dma_bridge_buggy */
 #include <linux/device.h>
+#include <linux/pm_runtime.h>
 #include <asm/setup.h>
 #include "pci.h"
 
@@ -434,6 +435,12 @@ static inline int platform_pci_sleep_wak
 			pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
 }
 
+int pci_platform_run_wake(struct pci_dev *dev, bool enable)
+{
+	return pci_platform_pm ?
+			pci_platform_pm->run_wake(dev, enable) : -ENODEV;
+}
+
 /**
  * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  *                           given PCI device
@@ -1202,6 +1209,31 @@ bool pci_check_pme_status(struct pci_dev
 }
 
 /**
+ * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
+ * @dev: Device to handle.
+ * @ign: Ignored.
+ *
+ * Check if @dev has generated PME and queue a resume request for it in that
+ * case.
+ */
+int __pci_pme_wakeup(struct pci_dev *dev, void *ign)
+{
+	if (pci_check_pme_status(dev))
+		pm_request_resume(&dev->dev);
+	return 0;
+}
+
+/**
+ * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
+ * @bus: Top bus of the subtree to walk.
+ */
+void pci_pme_wakeup_bus(struct pci_bus *bus)
+{
+	if (bus)
+		pci_walk_bus(bus, __pci_pme_wakeup, NULL);
+}
+
+/**
  * pci_pme_capable - check the capability of PCI device to generate PME#
  * @dev: PCI device to handle.
  * @state: PCI state from which device will issue PME#.
drivers/acpi/wakeup.c +50 -0

56 lines not on this page · raw message

include/acpi/acpi_bus.h +17 -3
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
@@ -391,21 +391,35 @@ acpi_handle acpi_get_pci_rootbridge_hand
 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle);
 #define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)((dev)->archdata.acpi_handle))
 
-#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_PM_WAKEUP
 int acpi_pm_device_sleep_state(struct device *, int *);
 int acpi_pm_device_sleep_wake(struct device *, bool);
-#else /* !CONFIG_PM_SLEEP */
+int acpi_device_run_wake(struct acpi_device *, bool);
+bool acpi_wakeup_gpe_shared(struct acpi_device *, struct acpi_device *);
+#else /* !CONFIG_PM_WAKEUP */
 static inline int acpi_pm_device_sleep_state(struct device *d, int *p)
 {
 	if (p)
 		*p = ACPI_STATE_D0;
 	return ACPI_STATE_D3;
 }
+
 static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
 {
 	return -ENODEV;
 }
-#endif /* !CONFIG_PM_SLEEP */
+
+static inline int acpi_device_run_wake(struct device *dev, bool enable)
+{
+	return -ENODEV;
+}
+
+static inline bool acpi_wakeup_gpe_shared(struct acpi_device *a,
+					   struct acpi_device *b)
+{
+	return false;
+}
+#endif /* !CONFIG_PM_WAKEUP */
 
 #endif				/* CONFIG_ACPI */
 
kernel/power/Kconfig +5 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -236,3 +236,8 @@ config PM_RUNTIME
 	  and the bus type drivers of the buses the devices are on are
 	  responsible for the actual handling of the autosuspend requests and
 	  wake-up events.
+
+config PM_WAKEUP
+	bool
+	depends on SUSPEND || HIBERNATION || PM_RUNTIME
+	default y
drivers/acpi/sleep.c +2 -2
Index: linux-2.6/drivers/acpi/sleep.c
===================================================================
@@ -594,7 +594,7 @@ int acpi_suspend(u32 acpi_state)
 	return -EINVAL;
 }
 
-#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_PM_WAKEUP
 /**
  *	acpi_pm_device_sleep_state - return preferred power state of ACPI device
  *		in the system sleep state given by %acpi_target_sleep_state
@@ -709,7 +709,7 @@ int acpi_pm_device_sleep_wake(struct dev
 
 	return error;
 }
-#endif
+#endif /* CONFIG_PM_WAKEUP */
 
 static void acpi_power_off_prepare(void)
 {
drivers/acpi/pci_root.c +5 -0
Index: linux-2.6/drivers/acpi/pci_root.c
===================================================================
@@ -563,6 +563,9 @@ static int __devinit acpi_pci_root_add(s
 	if (flags != base_flags)
 		acpi_pci_osc_support(root, flags);
 
+	if (device->wakeup.flags.valid)
+		pci_acpi_add_bus_pm_notifier(device, root->bus);
+
 	return 0;
 
 end:
@@ -584,6 +587,8 @@ static int acpi_pci_root_remove(struct a
 {
 	struct acpi_pci_root *root = acpi_driver_data(device);
 
+	if (device->wakeup.flags.valid)
+		pci_acpi_remove_bus_pm_notifier(device, root->bus);
 	kfree(root);
 	return 0;
 }
include/linux/pci-acpi.h +14 -0
Index: linux-2.6/include/linux/pci-acpi.h
===================================================================
@@ -11,6 +11,20 @@
 #include <linux/acpi.h>
 
 #ifdef CONFIG_ACPI
+extern acpi_status pci_acpi_add_hp_notifier(acpi_handle handle,
+					     acpi_notify_handler handler,
+					     void *context);
+extern acpi_status pci_acpi_remove_hp_notifier(acpi_handle handle,
+					       acpi_notify_handler handler);
+extern acpi_status pci_acpi_add_device_pm_notifier(struct acpi_device *dev,
+						   struct pci_dev *pci_dev);
+extern acpi_status pci_acpi_remove_device_pm_notifier(struct acpi_device *dev,
+						      struct pci_dev *pci_dev);
+extern acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev,
+						struct pci_bus *bus);
+extern acpi_status pci_acpi_remove_bus_pm_notifier(struct acpi_device *dev,
+						   struct pci_bus *bus);
+
 static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
 {
 	struct pci_bus *pbus = pdev->bus;
drivers/acpi/pci_bind.c +18 -1
Index: linux-2.6/drivers/acpi/pci_bind.c
===================================================================
@@ -26,6 +26,7 @@
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/pci.h>
+#include <linux/pci-acpi.h>
 #include <linux/acpi.h>
 #include <acpi/acpi_bus.h>
 #include <acpi/acpi_drivers.h>
@@ -38,7 +39,17 @@ static int acpi_pci_unbind(struct acpi_d
 	struct pci_dev *dev;
 
 	dev = acpi_get_pci_dev(device->handle);
-	if (!dev || !dev->subordinate)
+	if (!dev)
+		goto out;
+
+	if (device->wakeup.flags.valid) {
+		if (dev->subordinate)
+			pci_acpi_remove_bus_pm_notifier(device,
+							dev->subordinate);
+		pci_acpi_remove_device_pm_notifier(device, dev);
+	}
+
+	if (!dev->subordinate)
 		goto out;
 
 	acpi_pci_irq_del_prt(dev->subordinate);
@@ -94,6 +105,12 @@ static int acpi_pci_bind(struct acpi_dev
 
 	acpi_pci_irq_add_prt(device->handle, bus);
 
+	if (device->wakeup.flags.valid) {
+		pci_acpi_add_device_pm_notifier(device, dev);
+		if (dev->subordinate)
+			pci_acpi_add_bus_pm_notifier(device, dev->subordinate);
+	}
+
 out:
 	pci_dev_put(dev);
 	return 0;
drivers/pci/hotplug/acpiphp_glue.c +9 -14
Index: linux-2.6/drivers/pci/hotplug/acpiphp_glue.c
===================================================================
@@ -238,8 +238,7 @@ register_slot(acpi_handle handle, u32 lv
 
 	/* install notify handler */
 	if (!(newfunc->flags & FUNC_HAS_DCK)) {
-		status = acpi_install_notify_handler(handle,
-					     ACPI_SYSTEM_NOTIFY,
+		status = pci_acpi_add_hp_notifier(handle,
 					     handle_hotplug_event_func,
 					     newfunc);
 
@@ -290,14 +289,12 @@ static void init_bridge_misc(struct acpi
 	/* install notify handler */
 	if (bridge->type != BRIDGE_TYPE_HOST) {
 		if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
-			status = acpi_remove_notify_handler(bridge->func->handle,
-						ACPI_SYSTEM_NOTIFY,
+			status = pci_acpi_remove_hp_notifier(bridge->func->handle,
 						handle_hotplug_event_func);
 			if (ACPI_FAILURE(status))
 				err("failed to remove notify handler\n");
 		}
-		status = acpi_install_notify_handler(bridge->handle,
-					     ACPI_SYSTEM_NOTIFY,
+		status = pci_acpi_add_hp_notifier(bridge->handle,
 					     handle_hotplug_event_bridge,
 					     bridge);
 
@@ -513,15 +510,14 @@ static void cleanup_bridge(struct acpiph
 	acpi_status status;
 	acpi_handle handle = bridge->handle;
 
-	status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+	status = pci_acpi_remove_hp_notifier(handle,
 					    handle_hotplug_event_bridge);
 	if (ACPI_FAILURE(status))
 		err("failed to remove notify handler\n");
 
 	if ((bridge->type != BRIDGE_TYPE_HOST) &&
 	    ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
-		status = acpi_install_notify_handler(bridge->func->handle,
-						ACPI_SYSTEM_NOTIFY,
+		status = pci_acpi_add_hp_notifier(bridge->func->handle,
 						handle_hotplug_event_func,
 						bridge->func);
 		if (ACPI_FAILURE(status))
@@ -539,8 +535,7 @@ static void cleanup_bridge(struct acpiph
 				unregister_dock_notifier(&func->nb);
 			}
 			if (!(func->flags & FUNC_HAS_DCK)) {
-				status = acpi_remove_notify_handler(func->handle,
-						ACPI_SYSTEM_NOTIFY,
+				status = pci_acpi_remove_hp_notifier(func->handle,
 						handle_hotplug_event_func);
 				if (ACPI_FAILURE(status))
 					err("failed to remove notify handler\n");
@@ -602,7 +597,7 @@ static void remove_bridge(acpi_handle ha
 	if (bridge)
 		cleanup_bridge(bridge);
 	else
-		acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+		pci_acpi_remove_hp_notifier(handle,
 					   handle_hotplug_event_bridge);
 }
 
@@ -1492,8 +1487,8 @@ find_root_bridges(acpi_handle handle, u3
 	int *count = (int *)context;
 
 	if (acpi_is_root_bridge(handle)) {
-		acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
-				handle_hotplug_event_bridge, NULL);
+		pci_acpi_add_hp_notifier(handle,
+					handle_hotplug_event_bridge, NULL);
 			(*count)++;
 	}
 	return AE_OK ;
#15 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

Quoted 34 lines · On Mon, 21 Sep 2009 02:26:10 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Quoted 16 lines · Matthew Garrett
Quoted 15 lines · Rafael J. Wysocki
Quoted 13 lines · Matthew Garrett

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 154

Quoted 5 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +3 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+

dev->wakeup.gpe_number,

drivers/acpi/wakeup.c +1 -0
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

Quoted 6 lines
Quoted 2 lines

You're right, I overlooked that. Some kind of refcounting is needed here.

I've sent patches to implement this at the GPE level, which also change the API for requesting them. I'm waiting on feedback from Bob Moore.

In the meantime I realized there's one more thing we need to take care of. Namely, if a wake-up GPE is shared between multiple devices, it need not be necessary to install notify handlers for all of them. For example, if one of these devices is the root bridge, we will walk all of the hierarchy under it looking for devices that have PME set, so we need not install notify handlers for any devices that share the wake-up GPE with the root bridge. Similarly, there's no need to install a notify handler for a device that shares the wake-up GPE with a bridge (non-root) it is under.

Quoted 6 lines

Taking that into account I have prepared another version of the @subject patch which is appended below. It also takes the PM vs hotplug issue into account. The idea is pretty straightforward, everything should be clear from the changelog and the comments within the patch.

What's the latest on this set? Is this the final version? Anyone have issues with this version?

Thanks,

Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center
#16 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

Quoted 44 lines · Jesse Barnes
Quoted 34 lines · On Mon, 21 Sep 2009 02:26:10 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Quoted 16 lines · Matthew Garrett
Quoted 15 lines · Rafael J. Wysocki
Quoted 13 lines · Matthew Garrett

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 154

Quoted 5 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +3 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+

dev->wakeup.gpe_number,

drivers/acpi/wakeup.c +1 -0
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

Quoted 6 lines
Quoted 2 lines

You're right, I overlooked that. Some kind of refcounting is needed here.

I've sent patches to implement this at the GPE level, which also change the API for requesting them. I'm waiting on feedback from Bob Moore.

In the meantime I realized there's one more thing we need to take care of. Namely, if a wake-up GPE is shared between multiple devices, it need not be necessary to install notify handlers for all of them. For example, if one of these devices is the root bridge, we will walk all of the hierarchy under it looking for devices that have PME set, so we need not install notify handlers for any devices that share the wake-up GPE with the root bridge. Similarly, there's no need to install a notify handler for a device that shares the wake-up GPE with a bridge (non-root) it is under.

Quoted 6 lines

Taking that into account I have prepared another version of the @subject patch which is appended below. It also takes the PM vs hotplug issue into account. The idea is pretty straightforward, everything should be clear from the changelog and the comments within the patch.

Quoted 2 lines

What's the latest on this set? Is this the final version? Anyone have issues with this version?

This was the last one I sent, haven't received any feedback whatever.

Perhaps I'll send the entire patchset as is again for completness?

Thanks,
Rafael

#17 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

In reply to Matthew Garrett
Quoted 13 lines · Matthew Garrett
Quoted 12 lines · Rafael J. Wysocki
Quoted 10 lines · Matthew Garrett

Review

Quoted 4 lines · Rafael J. Wysocki
(quoted diff) +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

Quoted 4 lines
Quoted 1 line

You're right, I overlooked that. Some kind of refcounting is needed here.

I've sent patches to implement this at the GPE level, which also change the API for requesting them. I'm waiting on feedback from Bob Moore.

In the meantime I realized there's one more thing we need to take care of. Namely, if a wake-up GPE is shared between multiple devices, it need not be necessary to install notify handlers for all of them. For example, if one of these devices is the root bridge, we will walk all of the hierarchy under it looking for devices that have PME set, so we need not install notify handlers for any devices that share the wake-up GPE with the root bridge. Similarly, there's no need to install a notify handler for a device that shares the wake-up GPE with a bridge (non-root) it is under.

Taking that into account I have prepared another version of the @subject patch which is appended below. It also takes the PM vs hotplug issue into account. The idea is pretty straightforward, everything should be clear from the changelog and the comments within the patch.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 2)

Although the majority of PCI devices can generate PMEs that in principle may be used to wake up devices suspended at run time, platform support is generally necessary to convert PMEs into wake-up events that can be delivered to the kernel. If ACPI is used for this purpose, a PME generated by a PCI device will trigger the ACPI GPE associated with the device to generate an ACPI wake-up event that we can set up a handler for, provided that everything is configured correctly.

Unfortunately, the subset of PCI devices that have GPEs associated with them is quite limited and the other devices have to rely on the GPEs associated with their upstream bridges and, possibly, the root bridge to generate ACPI wake-up events in response to PMEs from them. Moreover, ACPI devices tend to share wake-up GPEs, in which cases it makes sense to install an ACPI notify handler for only one of them and "bind" the other devices to it. Furthermore, ACPI-based PCI hotplug also uses ACPI notify handlers that in general may conflict with the PM notify handlers, unless this issue is specifically taken care of.

Add ACPI platform support for PCI PME wake-up:
o Add a framework making is possible to use ACPI system notify handlers for both PM and hotplug at the same time and to take the wake-up GPE sharing into account.
o Add function acpi_device_run_wake() allowing us to enable or disable the GPE associated with given device to generate run-time wake-up events.
o Add a new PCI platform callback ->run_wake() to struct pci_platform_pm_ops allowing us to enable the platform to generate wake-up events for given device and implemet that callback for the ACPI platform.
o Define ACPI wake-up handlers for PCI devices and PCI buses and make the PCI-ACPI binding code register wake-up notifiers for devices associated with wake-up GPEs.
o Add function pci_platform_run_wake() that can be used to enable (or disable) the platform to generate wake-up events for given PCI device using the ->run_wake() platform callback.

Based on a patch from Matthew Garrett.

---
 drivers/acpi/pci_bind.c            |   19 
 drivers/acpi/pci_root.c            |    5 
 drivers/acpi/sleep.c               |    4 
 drivers/acpi/wakeup.c              |   50 ++
 drivers/pci/hotplug/acpiphp_glue.c |   23 -
 drivers/pci/pci-acpi.c             |  706 +++++++++++++++++++++++++++++++++++++
 drivers/pci/pci.c                  |   32 +
 drivers/pci/pci.h                  |   19 
 include/acpi/acpi_bus.h            |   20 -
 include/linux/pci-acpi.h           |   14 
 kernel/power/Kconfig               |    5 
 11 files changed, 874 insertions(+), 23 deletions(-)
drivers/pci/pci.h +16 -3
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -30,10 +30,14 @@ int pci_probe_reset_function(struct pci_
  *                platform; to be used during system-wide transitions from a
  *                sleeping state to the working state and vice versa
  *
- * @can_wakeup: returns 'true' if given device is capable of waking up the
- *              system from a sleeping state
+ * @can_wakeup: returns 'true' if the plaform can generate wake-up events for
+ *		given device.
  *
- * @sleep_wake: enables/disables the system wake up capability of given device
+ * @sleep_wake: enables/disables the wake-up capability of given device
+ *
+ * @run_wake: enables/disables the platform to generate run-time wake-up events
+ *		for given device (the device's wake-up capability has to be
+ *		enabled by @sleep_wake for this feature to work)
  *
  * If given platform is generally capable of power managing PCI devices, all of
  * these callbacks are mandatory.
@@ -44,16 +48,25 @@ struct pci_platform_pm_ops {
 	pci_power_t (*choose_state)(struct pci_dev *dev);
 	bool (*can_wakeup)(struct pci_dev *dev);
 	int (*sleep_wake)(struct pci_dev *dev, bool enable);
+	int (*run_wake)(struct pci_dev *dev, bool enable);
 };
 
 extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
+extern int pci_platform_run_wake(struct pci_dev *dev, bool enable);
 extern void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
 extern void pci_disable_enabled_device(struct pci_dev *dev);
 extern bool pci_check_pme_status(struct pci_dev *dev);
+extern int __pci_pme_wakeup(struct pci_dev *dev, void *ign);
+extern void pci_pme_wakeup_bus(struct pci_bus *bus);
 extern void pci_pm_init(struct pci_dev *dev);
 extern void platform_pci_wakeup_init(struct pci_dev *dev);
 extern void pci_allocate_cap_save_buffers(struct pci_dev *dev);
 
+static inline void pci_pme_wakeup(struct pci_dev *dev)
+{
+	__pci_pme_wakeup(dev, NULL);
+}
+
 static inline bool pci_is_bridge(struct pci_dev *pci_dev)
 {
 	return !!(pci_dev->subordinate);
drivers/pci/pci-acpi.c +706 -0

724 lines not on this page · raw message

drivers/pci/pci.c +32 -0
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -21,6 +21,7 @@
 #include <linux/interrupt.h>
 #include <asm/dma.h>	/* isa_dma_bridge_buggy */
 #include <linux/device.h>
+#include <linux/pm_runtime.h>
 #include <asm/setup.h>
 #include "pci.h"
 
@@ -434,6 +435,12 @@ static inline int platform_pci_sleep_wak
 			pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
 }
 
+int pci_platform_run_wake(struct pci_dev *dev, bool enable)
+{
+	return pci_platform_pm ?
+			pci_platform_pm->run_wake(dev, enable) : -ENODEV;
+}
+
 /**
  * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  *                           given PCI device
@@ -1202,6 +1209,31 @@ bool pci_check_pme_status(struct pci_dev
 }
 
 /**
+ * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
+ * @dev: Device to handle.
+ * @ign: Ignored.
+ *
+ * Check if @dev has generated PME and queue a resume request for it in that
+ * case.
+ */
+int __pci_pme_wakeup(struct pci_dev *dev, void *ign)
+{
+	if (pci_check_pme_status(dev))
+		pm_request_resume(&dev->dev);
+	return 0;
+}
+
+/**
+ * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
+ * @bus: Top bus of the subtree to walk.
+ */
+void pci_pme_wakeup_bus(struct pci_bus *bus)
+{
+	if (bus)
+		pci_walk_bus(bus, __pci_pme_wakeup, NULL);
+}
+
+/**
  * pci_pme_capable - check the capability of PCI device to generate PME#
  * @dev: PCI device to handle.
  * @state: PCI state from which device will issue PME#.
drivers/acpi/wakeup.c +50 -0

56 lines not on this page · raw message

include/acpi/acpi_bus.h +17 -3
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
@@ -391,21 +391,35 @@ acpi_handle acpi_get_pci_rootbridge_hand
 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle);
 #define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)((dev)->archdata.acpi_handle))
 
-#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_PM_WAKEUP
 int acpi_pm_device_sleep_state(struct device *, int *);
 int acpi_pm_device_sleep_wake(struct device *, bool);
-#else /* !CONFIG_PM_SLEEP */
+int acpi_device_run_wake(struct acpi_device *, bool);
+bool acpi_wakeup_gpe_shared(struct acpi_device *, struct acpi_device *);
+#else /* !CONFIG_PM_WAKEUP */
 static inline int acpi_pm_device_sleep_state(struct device *d, int *p)
 {
 	if (p)
 		*p = ACPI_STATE_D0;
 	return ACPI_STATE_D3;
 }
+
 static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
 {
 	return -ENODEV;
 }
-#endif /* !CONFIG_PM_SLEEP */
+
+static inline int acpi_device_run_wake(struct device *dev, bool enable)
+{
+	return -ENODEV;
+}
+
+static inline bool acpi_wakeup_gpe_shared(struct acpi_device *a,
+					   struct acpi_device *b)
+{
+	return false;
+}
+#endif /* !CONFIG_PM_WAKEUP */
 
 #endif				/* CONFIG_ACPI */
 
kernel/power/Kconfig +5 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -236,3 +236,8 @@ config PM_RUNTIME
 	  and the bus type drivers of the buses the devices are on are
 	  responsible for the actual handling of the autosuspend requests and
 	  wake-up events.
+
+config PM_WAKEUP
+	bool
+	depends on SUSPEND || HIBERNATION || PM_RUNTIME
+	default y
drivers/acpi/sleep.c +2 -2
Index: linux-2.6/drivers/acpi/sleep.c
===================================================================
@@ -594,7 +594,7 @@ int acpi_suspend(u32 acpi_state)
 	return -EINVAL;
 }
 
-#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_PM_WAKEUP
 /**
  *	acpi_pm_device_sleep_state - return preferred power state of ACPI device
  *		in the system sleep state given by %acpi_target_sleep_state
@@ -709,7 +709,7 @@ int acpi_pm_device_sleep_wake(struct dev
 
 	return error;
 }
-#endif
+#endif /* CONFIG_PM_WAKEUP */
 
 static void acpi_power_off_prepare(void)
 {
drivers/acpi/pci_root.c +5 -0
Index: linux-2.6/drivers/acpi/pci_root.c
===================================================================
@@ -563,6 +563,9 @@ static int __devinit acpi_pci_root_add(s
 	if (flags != base_flags)
 		acpi_pci_osc_support(root, flags);
 
+	if (device->wakeup.flags.valid)
+		pci_acpi_add_bus_pm_notifier(device, root->bus);
+
 	return 0;
 
 end:
@@ -584,6 +587,8 @@ static int acpi_pci_root_remove(struct a
 {
 	struct acpi_pci_root *root = acpi_driver_data(device);
 
+	if (device->wakeup.flags.valid)
+		pci_acpi_remove_bus_pm_notifier(device, root->bus);
 	kfree(root);
 	return 0;
 }
include/linux/pci-acpi.h +14 -0
Index: linux-2.6/include/linux/pci-acpi.h
===================================================================
@@ -11,6 +11,20 @@
 #include <linux/acpi.h>
 
 #ifdef CONFIG_ACPI
+extern acpi_status pci_acpi_add_hp_notifier(acpi_handle handle,
+					     acpi_notify_handler handler,
+					     void *context);
+extern acpi_status pci_acpi_remove_hp_notifier(acpi_handle handle,
+					       acpi_notify_handler handler);
+extern acpi_status pci_acpi_add_device_pm_notifier(struct acpi_device *dev,
+						   struct pci_dev *pci_dev);
+extern acpi_status pci_acpi_remove_device_pm_notifier(struct acpi_device *dev,
+						      struct pci_dev *pci_dev);
+extern acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev,
+						struct pci_bus *bus);
+extern acpi_status pci_acpi_remove_bus_pm_notifier(struct acpi_device *dev,
+						   struct pci_bus *bus);
+
 static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
 {
 	struct pci_bus *pbus = pdev->bus;
drivers/acpi/pci_bind.c +18 -1
Index: linux-2.6/drivers/acpi/pci_bind.c
===================================================================
@@ -26,6 +26,7 @@
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/pci.h>
+#include <linux/pci-acpi.h>
 #include <linux/acpi.h>
 #include <acpi/acpi_bus.h>
 #include <acpi/acpi_drivers.h>
@@ -38,7 +39,17 @@ static int acpi_pci_unbind(struct acpi_d
 	struct pci_dev *dev;
 
 	dev = acpi_get_pci_dev(device->handle);
-	if (!dev || !dev->subordinate)
+	if (!dev)
+		goto out;
+
+	if (device->wakeup.flags.valid) {
+		if (dev->subordinate)
+			pci_acpi_remove_bus_pm_notifier(device,
+							dev->subordinate);
+		pci_acpi_remove_device_pm_notifier(device, dev);
+	}
+
+	if (!dev->subordinate)
 		goto out;
 
 	acpi_pci_irq_del_prt(dev->subordinate);
@@ -94,6 +105,12 @@ static int acpi_pci_bind(struct acpi_dev
 
 	acpi_pci_irq_add_prt(device->handle, bus);
 
+	if (device->wakeup.flags.valid) {
+		pci_acpi_add_device_pm_notifier(device, dev);
+		if (dev->subordinate)
+			pci_acpi_add_bus_pm_notifier(device, dev->subordinate);
+	}
+
 out:
 	pci_dev_put(dev);
 	return 0;
drivers/pci/hotplug/acpiphp_glue.c +9 -14
Index: linux-2.6/drivers/pci/hotplug/acpiphp_glue.c
===================================================================
@@ -238,8 +238,7 @@ register_slot(acpi_handle handle, u32 lv
 
 	/* install notify handler */
 	if (!(newfunc->flags & FUNC_HAS_DCK)) {
-		status = acpi_install_notify_handler(handle,
-					     ACPI_SYSTEM_NOTIFY,
+		status = pci_acpi_add_hp_notifier(handle,
 					     handle_hotplug_event_func,
 					     newfunc);
 
@@ -290,14 +289,12 @@ static void init_bridge_misc(struct acpi
 	/* install notify handler */
 	if (bridge->type != BRIDGE_TYPE_HOST) {
 		if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
-			status = acpi_remove_notify_handler(bridge->func->handle,
-						ACPI_SYSTEM_NOTIFY,
+			status = pci_acpi_remove_hp_notifier(bridge->func->handle,
 						handle_hotplug_event_func);
 			if (ACPI_FAILURE(status))
 				err("failed to remove notify handler\n");
 		}
-		status = acpi_install_notify_handler(bridge->handle,
-					     ACPI_SYSTEM_NOTIFY,
+		status = pci_acpi_add_hp_notifier(bridge->handle,
 					     handle_hotplug_event_bridge,
 					     bridge);
 
@@ -513,15 +510,14 @@ static void cleanup_bridge(struct acpiph
 	acpi_status status;
 	acpi_handle handle = bridge->handle;
 
-	status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+	status = pci_acpi_remove_hp_notifier(handle,
 					    handle_hotplug_event_bridge);
 	if (ACPI_FAILURE(status))
 		err("failed to remove notify handler\n");
 
 	if ((bridge->type != BRIDGE_TYPE_HOST) &&
 	    ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
-		status = acpi_install_notify_handler(bridge->func->handle,
-						ACPI_SYSTEM_NOTIFY,
+		status = pci_acpi_add_hp_notifier(bridge->func->handle,
 						handle_hotplug_event_func,
 						bridge->func);
 		if (ACPI_FAILURE(status))
@@ -539,8 +535,7 @@ static void cleanup_bridge(struct acpiph
 				unregister_dock_notifier(&func->nb);
 			}
 			if (!(func->flags & FUNC_HAS_DCK)) {
-				status = acpi_remove_notify_handler(func->handle,
-						ACPI_SYSTEM_NOTIFY,
+				status = pci_acpi_remove_hp_notifier(func->handle,
 						handle_hotplug_event_func);
 				if (ACPI_FAILURE(status))
 					err("failed to remove notify handler\n");
@@ -602,7 +597,7 @@ static void remove_bridge(acpi_handle ha
 	if (bridge)
 		cleanup_bridge(bridge);
 	else
-		acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+		pci_acpi_remove_hp_notifier(handle,
 					   handle_hotplug_event_bridge);
 }
 
@@ -1492,8 +1487,8 @@ find_root_bridges(acpi_handle handle, u3
 	int *count = (int *)context;
 
 	if (acpi_is_root_bridge(handle)) {
-		acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
-				handle_hotplug_event_bridge, NULL);
+		pci_acpi_add_hp_notifier(handle,
+					handle_hotplug_event_bridge, NULL);
 			(*count)++;
 	}
 	return AE_OK ;
#18 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

In reply to Matthew Garrett
Quoted 10 lines · Matthew Garrett

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 163

Quoted 4 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

You're right, I overlooked that. Some kind of refcounting is needed here.

Quoted 8 lines

drivers/acpi/pci_bind.c static int acpi_pci_bind(struct acpi_dev · line 102

Quoted 5 lines
drivers/acpi/pci_bind.c +5 -0
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+

I think this will fail for the root bridge if acpiphp has already grabbed it to check for hotplug events.

The root bridge is not a struct pci_dev and this is called only for these.

Thanks,
Rafael

#19 lore raw

Re: [RFC][PATCH 3/4] PCI / ACPI PM: Platform support for PCI PME wake-up

In reply to Rafael J. Wysocki

drivers/acpi/wakeup.c void acpi_disable_wakeup_device(u8 sleep · line 163

Quoted 4 lines · Rafael J. Wysocki
drivers/acpi/wakeup.c +4 -0
+	} else if (!dev->wakeup.flags.run_wake) {
+		acpi_set_gpe_type(dev->wakeup.gpe_device,
+					dev->wakeup.gpe_number,
+					ACPI_GPE_TYPE_WAKE);

Is this going to work for cases where we have multiple devices attached to the same GPE? The common one is EHCI, where both EHCI HCDs will be one a single GPE. If we wake one, that'll then disable the GPE for the other. Further wakeup events will then be lost.

drivers/acpi/pci_bind.c static int acpi_pci_bind(struct acpi_dev · line 102

Quoted 5 lines
drivers/acpi/pci_bind.c +5 -0
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+

I think this will fail for the root bridge if acpiphp has already grabbed it to check for hotplug events.

Signature · 1 line
Matthew Garrett | [email protected]
#20 lore raw

RFC PATCH 3/4 PCI / ACPI PM: Platform support for PCI PME wake-up

In reply to Rafael J. Wysocki

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

Although the majority of PCI devices can generate PMEs that in principle may be used to wake up devices suspended at run time, platform support is generally necessary to convert PMEs into wake-up events that can be delivered to the kernel. If ACPI is used for this purpose, a PME generated by a PCI device will trigger the ACPI GPE associated with the device to generate an ACPI wake-up event that we can set up a handler for, provided that everything is configured correctly.

Unfortunately, the subset of PCI devices that have GPEs associated with them is quite limited and the other devices have to rely on the GPEs associated with their upstream bridges and, possibly, the root bridge to generate ACPI wake-up events in response to PMEs from them.

Add ACPI platform support for PCI PME wake-up:
o Add function acpi_device_run_wake() allowing us to enable or disable the GPE associated with given device to generate run-time wake-up events.
o Add a new PCI platform callback ->run_wake() to struct pci_platform_pm_ops allowing us to enable the platform to generate wake-up events for given device.
o Implemet that callback for the ACPI platform using acpi_device_run_wake().
o Define ACPI wake-up handlers for PCI devices and PCI buses and make the PCI-ACPI binding code install wake-up notifiers for devices associated with GPEs that can be used for wake-up.
o Add function pci_platform_run_wake() that can be used to enable (or disable) the platform to generate wake-up events for given PCI device using the ->run_wake() platform callback.

Based on a patch from Matthew Garrett.

---
 drivers/acpi/pci_bind.c  |    6 ++++++
 drivers/acpi/pci_root.c  |    5 +++++
 drivers/acpi/sleep.c     |    4 ++--
 drivers/acpi/wakeup.c    |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/pci/pci-acpi.c   |   42 ++++++++++++++++++++++++++++++++++++++++++
 drivers/pci/pci.c        |   41 +++++++++++++++++++++++++++++++++++++++++
 drivers/pci/pci.h        |   14 +++++++++++---
 include/acpi/acpi_bus.h  |   13 ++++++++++---
 include/linux/pci-acpi.h |    3 +++
 kernel/power/Kconfig     |    5 +++++
 10 files changed, 172 insertions(+), 8 deletions(-)
drivers/pci/pci.h +11 -3
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -29,10 +29,14 @@ extern int pci_mmap_fits(struct pci_dev 
  *                platform; to be used during system-wide transitions from a
  *                sleeping state to the working state and vice versa
  *
- * @can_wakeup: returns 'true' if given device is capable of waking up the
- *              system from a sleeping state
+ * @can_wakeup: returns 'true' if the plaform can generate wake-up events for
+ *		given device.
  *
- * @sleep_wake: enables/disables the system wake up capability of given device
+ * @sleep_wake: enables/disables the wake-up capability of given device
+ *
+ * @run_wake: enables/disables the platform to generate run-time wake-up events
+ *		for given device (the device's wake-up capability has to be
+ *		enabled by @sleep_wake for this feature to work)
  *
  * If given platform is generally capable of power managing PCI devices, all of
  * these callbacks are mandatory.
@@ -43,12 +47,16 @@ struct pci_platform_pm_ops {
 	pci_power_t (*choose_state)(struct pci_dev *dev);
 	bool (*can_wakeup)(struct pci_dev *dev);
 	int (*sleep_wake)(struct pci_dev *dev, bool enable);
+	int (*run_wake)(struct pci_dev *dev, bool enable);
 };
 
 extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
+extern int pci_platform_run_wake(struct pci_dev *dev, bool enable);
 extern void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
 extern void pci_disable_enabled_device(struct pci_dev *dev);
 extern bool pci_check_pme_status(struct pci_dev *dev);
+extern void pci_pme_wakeup_bus(struct pci_bus *bus);
+extern void pci_pme_wakeup(struct pci_dev *dev);
 extern void pci_pm_init(struct pci_dev *dev);
 extern void platform_pci_wakeup_init(struct pci_dev *dev);
 extern void pci_allocate_cap_save_buffers(struct pci_dev *dev);
drivers/pci/pci-acpi.c +42 -0

60 lines not on this page · raw message

drivers/pci/pci.c +41 -0

59 lines not on this page · raw message

drivers/acpi/wakeup.c +47 -0

53 lines not on this page · raw message

include/acpi/acpi_bus.h +10 -3
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
@@ -375,21 +375,28 @@ int acpi_is_root_bridge(acpi_handle);
 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int, unsigned int);
 #define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)((dev)->archdata.acpi_handle))
 
-#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_PM_WAKEUP
 int acpi_pm_device_sleep_state(struct device *, int *);
 int acpi_pm_device_sleep_wake(struct device *, bool);
-#else /* !CONFIG_PM_SLEEP */
+int acpi_device_run_wake(struct device *, bool);
+#else /* !CONFIG_PM_WAKEUP */
 static inline int acpi_pm_device_sleep_state(struct device *d, int *p)
 {
 	if (p)
 		*p = ACPI_STATE_D0;
 	return ACPI_STATE_D3;
 }
+
 static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
 {
 	return -ENODEV;
 }
-#endif /* !CONFIG_PM_SLEEP */
+
+static inline int acpi_device_run_wake(struct device *dev, bool enable)
+{
+	return -ENODEV;
+}
+#endif /* !CONFIG_PM_WAKEUP */
 
 #endif				/* CONFIG_ACPI */
 
kernel/power/Kconfig +5 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -236,3 +236,8 @@ config PM_RUNTIME
 	  and the bus type drivers of the buses the devices are on are
 	  responsible for the actual handling of the autosuspend requests and
 	  wake-up events.
+
+config PM_WAKEUP
+	bool
+	depends on SUSPEND || HIBERNATION || PM_RUNTIME
+	default y
drivers/acpi/sleep.c +2 -2
Index: linux-2.6/drivers/acpi/sleep.c
===================================================================
@@ -594,7 +594,7 @@ int acpi_suspend(u32 acpi_state)
 	return -EINVAL;
 }
 
-#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_PM_WAKEUP
 /**
  *	acpi_pm_device_sleep_state - return preferred power state of ACPI device
  *		in the system sleep state given by %acpi_target_sleep_state
@@ -709,7 +709,7 @@ int acpi_pm_device_sleep_wake(struct dev
 
 	return error;
 }
-#endif
+#endif /* CONFIG_PM_WAKEUP */
 
 static void acpi_power_off_prepare(void)
 {
drivers/acpi/pci_root.c +5 -0
Index: linux-2.6/drivers/acpi/pci_root.c
===================================================================
@@ -576,6 +576,11 @@ static int __devinit acpi_pci_root_add(s
 	if (flags != base_flags)
 		acpi_pci_osc_support(root, flags);
 
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_bus_wakeup,
+						&root->bus->dev);
+
 	return 0;
 
 end:
include/linux/pci-acpi.h +3 -0
Index: linux-2.6/include/linux/pci-acpi.h
===================================================================
@@ -10,6 +10,9 @@
 
 #include <linux/acpi.h>
 
+extern void pci_acpi_bus_wakeup(acpi_handle handle, u32 event, void *data);
+extern void pci_acpi_device_wakeup(acpi_handle handle, u32 event, void *data);
+
 #ifdef CONFIG_ACPI
 static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
 {
drivers/acpi/pci_bind.c +6 -0
Index: linux-2.6/drivers/acpi/pci_bind.c
===================================================================
@@ -26,6 +26,7 @@
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/pci.h>
+#include <linux/pci-acpi.h>
 #include <linux/acpi.h>
 #include <acpi/acpi_bus.h>
 #include <acpi/acpi_drivers.h>
@@ -94,6 +95,11 @@ static int acpi_pci_bind(struct acpi_dev
 
 	acpi_pci_irq_add_prt(device->handle, bus);
 
+	if (device->wakeup.flags.valid)
+		acpi_install_notify_handler(device->handle, ACPI_SYSTEM_NOTIFY,
+						pci_acpi_device_wakeup,
+						&dev->dev);
+
 out:
 	pci_dev_put(dev);
 	return 0;
#21 lore raw

RFC PATCH 4/4 PCI PM: Run-time callbacks for PCI bus type

In reply to Rafael J. Wysocki

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

Introduce run-time PM callbacks for the PCI bus type. Make the new callbacks work in analogy with the existing system sleep PM callbacks, so that the drivers already converted to struct dev_pm_ops can use their suspend and resume routines for run-time PM without modifications.

---
 drivers/pci/pci-driver.c |   97 ++++++++++++++++++++++++++++++++++++++++++++---
 kernel/power/Kconfig     |    5 ++
 2 files changed, 97 insertions(+), 5 deletions(-)
kernel/power/Kconfig +5 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -241,3 +241,8 @@ config PM_WAKEUP
 	bool
 	depends on SUSPEND || HIBERNATION || PM_RUNTIME
 	default y
+
+config PM_OPS
+	bool
+	depends on PM_SLEEP || PM_RUNTIME
+	default y
drivers/pci/pci-driver.c +92 -5

140 lines not on this page · raw message

#22 lore raw

RFC PATCH 4/4 PCI PM: Run-time callbacks for PCI bus type

In reply to Rafael J. Wysocki

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

Introduce run-time PM callbacks for the PCI bus type. Make the new callbacks work in analogy with the existing system sleep PM callbacks, so that the drivers already converted to struct dev_pm_ops can use their suspend and resume routines for run-time PM without modifications.

---
 drivers/pci/pci-driver.c |   97 ++++++++++++++++++++++++++++++++++++++++++++---
 kernel/power/Kconfig     |    5 ++
 2 files changed, 97 insertions(+), 5 deletions(-)
kernel/power/Kconfig +5 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -241,3 +241,8 @@ config PM_WAKEUP
 	bool
 	depends on SUSPEND || HIBERNATION || PM_RUNTIME
 	default y
+
+config PM_OPS
+	bool
+	depends on PM_SLEEP || PM_RUNTIME
+	default y
drivers/pci/pci-driver.c +92 -5

140 lines not on this page · raw message