2009-11-15 23:58:48

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 0/10] PCI run-time PM support

Hi,

The following series of patches provides run-time power management support
through ACPI and the native PCIe PME.

[1/10] - Add flag for marking devices capable of generating wake-up events at run time

[2/10] - Add function for checking PME status of devices

[3/10] - Modify wake-up enable propagation so that it's done for PCIe devices too

[4/10] - PCIe PME root port service driver

[5/10] - ACPI GPE refcounting from Matthew

[6/10] - ACPI drivers support for GPE refcounting from Matthew

[7/10] - ACPI removal of the old GPE API from Matthew

[8/10] - ACPI add fields for handling run-wake devices

[9/10] - PCI ACPI platform support for run-time power management

[10/10] - Runtime PM callbacks for the PCI bus type

Comments welcome.

Best,
Rafael


2009-11-15 23:58:53

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 1/10] PM: Add flag for devices capable of generating run-time wake-up events

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

Apparently, there are devices that can wake up the system from sleep
states and yet are incapable of generating wake-up events at run
time. Thus, introduce a flag indicating if given device is capable
of generating run-time wake-up events.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
Documentation/power/runtime_pm.txt | 7 +++++--
include/linux/pm.h | 8 +++++---
include/linux/pm_runtime.h | 12 ++++++++++++
3 files changed, 22 insertions(+), 5 deletions(-)

Index: linux-2.6/include/linux/pm.h
===================================================================
--- linux-2.6.orig/include/linux/pm.h
+++ linux-2.6/include/linux/pm.h
@@ -179,9 +179,10 @@ typedef struct pm_message {
* This need not mean that the device should be put into a low power state.
* For example, if the device is behind a link which is about to be turned
* off, the device may remain at full power. If the device does go to low
- * power and if device_may_wakeup(dev) is true, remote wake-up (i.e., a
- * hardware mechanism allowing the device to request a change of its power
- * state, such as PCI PME) should be enabled for it.
+ * power and is capable of generating run-time wake-up events, remote
+ * wake-up (i.e., a hardware mechanism allowing the device to request a
+ * change of its power state via a wake-up event, such as PCI PME) should
+ * be enabled for it.
*
* @runtime_resume: Put the device into the fully active state in response to a
* wake-up event generated by hardware or at the request of software. If
@@ -434,6 +435,7 @@ struct dev_pm_info {
unsigned int idle_notification:1;
unsigned int request_pending:1;
unsigned int deferred_resume:1;
+ unsigned int run_wake:1;
enum rpm_request request;
enum rpm_status runtime_status;
int runtime_error;
Index: linux-2.6/Documentation/power/runtime_pm.txt
===================================================================
--- linux-2.6.orig/Documentation/power/runtime_pm.txt
+++ linux-2.6/Documentation/power/runtime_pm.txt
@@ -71,9 +71,9 @@ what to do to handle the device).
purpose).

In particular, if the driver requires remote wakeup capability for proper
-functioning and device_may_wakeup() returns 'false' for the device, then
+functioning and device_run_wake() returns 'false' for the device, then
->runtime_suspend() should return -EBUSY. On the other hand, if
-device_may_wakeup() returns 'true' for the device and the device is put
+device_run_wake() returns 'true' for the device and the device is put
into a low power state during the execution of its bus type's
->runtime_suspend(), it is expected that remote wake-up (i.e. hardware mechanism
allowing the device to request a change of its power state, such as PCI PME)
@@ -214,6 +214,9 @@ defined in include/linux/pm.h:
being executed for that device and it is not practical to wait for the
suspend to complete; means "start a resume as soon as you've suspended"

+ unsigned int run_wake;
+ - set if the device is capable of generating run-time wake-up events
+
enum rpm_status runtime_status;
- the run-time PM status of the device; this field's initial value is
RPM_SUSPENDED, which means that each device is initially regarded by the
Index: linux-2.6/include/linux/pm_runtime.h
===================================================================
--- linux-2.6.orig/include/linux/pm_runtime.h
+++ linux-2.6/include/linux/pm_runtime.h
@@ -50,6 +50,16 @@ static inline void pm_runtime_put_noidle
atomic_add_unless(&dev->power.usage_count, -1, 0);
}

+static inline bool device_run_wake(struct device *dev)
+{
+ return dev->power.run_wake;
+}
+
+static inline void device_set_run_wake(struct device *dev, bool enable)
+{
+ dev->power.run_wake = enable;
+}
+
#else /* !CONFIG_PM_RUNTIME */

static inline int pm_runtime_idle(struct device *dev) { return -ENOSYS; }
@@ -73,6 +83,8 @@ static inline bool pm_children_suspended
static inline void pm_suspend_ignore_children(struct device *dev, bool en) {}
static inline void pm_runtime_get_noresume(struct device *dev) {}
static inline void pm_runtime_put_noidle(struct device *dev) {}
+static inline bool device_run_wake(struct device *dev) { return false; }
+static inline void device_set_run_wake(struct device *dev, bool enable) {}

#endif /* !CONFIG_PM_RUNTIME */

2009-11-16 00:01:12

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 2/10] 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 used for PCI run-time power management.

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

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/pci/pci.c | 35 +++++++++++++++++++++++++++++++++++
drivers/pci/pci.h | 1 +
2 files changed, 36 insertions(+)

Index: linux-2.6/drivers/pci/pci.h
===================================================================
--- linux-2.6.orig/drivers/pci/pci.h
+++ linux-2.6/drivers/pci/pci.h
@@ -49,6 +49,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);
Index: linux-2.6/drivers/pci/pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci.c
+++ linux-2.6/drivers/pci/pci.c
@@ -1167,6 +1167,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 and if set, clear it and clear PME enable
+ * (if set). Return 'true' if PME status and PME enable were both set 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;
+ pci_read_config_word(dev, pmcsr_pos, &pmcsr);
+ if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
+ return false;
+
+ /* Clear PME status. */
+ pmcsr |= PCI_PM_CTRL_PME_STATUS;
+ if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
+ /* Disable PME to avoid interrupt flood. */
+ 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#.

2009-11-15 23:58:30

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 3/10] PCI / PM: Propagate wake-up enable for PCIe devices too

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

Having read the PM part of the PCIe 2.0 specification more carefully
I think that it was a mistake to restrict the wake-up enable
propagation to non-PCIe devices, because if we do not request
control of the root ports' PME registers via OSC, PCIe PME is
supposed to be handled by the platform, just like the non-PCIe PME.
Even if we do that, the wake-up propagation is done to allow the
devices to wake up the system from sleep states which involves the
platform anyway, so it won't hurt.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/pci/pci-acpi.c | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)

Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci-acpi.c
+++ linux-2.6/drivers/pci/pci-acpi.c
@@ -112,11 +112,7 @@ static bool acpi_pci_can_wakeup(struct p
static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
{
while (bus->parent) {
- struct pci_dev *bridge = bus->self;
- int ret;
-
- ret = acpi_pm_device_sleep_wake(&bridge->dev, enable);
- if (!ret || bridge->is_pcie)
+ if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
return;
bus = bus->parent;
}
@@ -131,9 +127,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)
- acpi_pci_propagate_wakeup_enable(dev->bus, enable);
-
+ acpi_pci_propagate_wakeup_enable(dev->bus, enable);
return 0;
}

2009-11-15 23:58:59

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 4/10] PCI PM: PCIe PME root port service driver (rev. 4)

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]>.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/pci/pcie/Kconfig | 4
drivers/pci/pcie/Makefile | 2
drivers/pci/pcie/pme/Makefile | 8
drivers/pci/pcie/pme/pcie_pme.c | 479 +++++++++++++++++++++++++++++++++++
drivers/pci/pcie/pme/pcie_pme.h | 28 ++
drivers/pci/pcie/pme/pcie_pme_acpi.c | 54 +++
include/linux/pci.h | 1
7 files changed, 576 insertions(+)

Index: linux-2.6/drivers/pci/pcie/Kconfig
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/Kconfig
+++ 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
Index: linux-2.6/drivers/pci/pcie/Makefile
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/Makefile
+++ 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/
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
@@ -0,0 +1,479 @@
+/*
+ * PCIe Native PME support
+ *
+ * Copyright (C) 2007 - 2009 Intel Corp
+ * Copyright (C) 2007 - 2009 Shaohua Li <[email protected]>
+ * Copyright (C) 2009 Rafael J. Wysocki <[email protected]>, Novell Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License V2. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/device.h>
+#include <linux/pcieport_if.h>
+#include <linux/acpi.h>
+#include <linux/pci-acpi.h>
+#include <linux/pm_runtime.h>
+
+#include "../../pci.h"
+#include "pcie_pme.h"
+
+#define PCI_EXP_RTSTA_PME 0x10000 /* PME status */
+#define PCI_EXP_RTSTA_PENDING 0x20000 /* PME pending */
+
+/*
+ * If set, this switch will prevent the PCIe root port PME service driver from
+ * being registered. Consequently, the interrupt-based PCIe PME signaling will
+ * not be used by any PCIe root ports in that case.
+ */
+static bool pcie_pme_disabled;
+
+/*
+ * The PCI Express Base Specification 2.0, Section 6.1.8, states the following:
+ * "In order to maintain compatibility with non-PCI Express-aware system
+ * software, system power management logic must be configured by firmware to use
+ * the legacy mechanism of signaling PME by default. PCI Express-aware system
+ * software must notify the firmware prior to enabling native, interrupt-based
+ * PME signaling." However, if the platform doesn't provide us with a suitable
+ * notification mechanism or the notification fails, it is not clear whether or
+ * not we are supposed to use the interrupt-based PCIe PME signaling. The
+ * switch below can be used to indicate the desired behaviour. When set, it
+ * will make the kernel use the interrupt-based PCIe PME signaling regardless of
+ * the platform notification status, although the kernel will attempt to notify
+ * the platform anyway. When unset, it will prevent the kernel from using the
+ * the interrupt-based PCIe PME signaling if the platform notification fails,
+ * which is the default.
+ */
+static bool pcie_pme_force_enable;
+
+static int __init pcie_pme_setup(char *str)
+{
+ if (!strcmp(str, "off"))
+ pcie_pme_disabled = true;
+ else if (!strcmp(str, "force"))
+ pcie_pme_force_enable = true;
+ return 1;
+}
+__setup("pcie_pme=", pcie_pme_setup);
+
+/**
+ * pcie_pme_platform_setup - Ensure that the kernel controls the PCIe PME.
+ * @srv: PCIe PME root port service to use for carrying out the check.
+ *
+ * Notify the platform that the native PCIe PME is going to be used and return
+ * 'true' if the control of the PCIe PME registers has been acquired from the
+ * platform.
+ */
+static bool pcie_pme_platform_setup(struct pcie_device *srv)
+{
+ return !pcie_pme_platform_notify(srv) || pcie_pme_force_enable;
+}
+
+struct pcie_pme_service_data {
+ spinlock_t lock;
+ struct pcie_device *srv;
+ struct work_struct work;
+ bool noirq; /* Don't enable the PME interrupt used by this service. */
+};
+
+/**
+ * pcie_pme_interrupt_enable - Enable/disable PCIe PME interrupt generation.
+ * @dev: PCIe root port or event collector.
+ * @enable: Enable or disable the interrupt.
+ */
+static void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
+{
+ int rtctl_pos;
+ u16 rtctl;
+
+ rtctl_pos = pci_find_capability(dev, PCI_CAP_ID_EXP) + PCI_EXP_RTCTL;
+
+ pci_read_config_word(dev, rtctl_pos, &rtctl);
+ if (enable)
+ rtctl |= PCI_EXP_RTCTL_PMEIE;
+ else
+ rtctl &= ~PCI_EXP_RTCTL_PMEIE;
+ pci_write_config_word(dev, rtctl_pos, rtctl);
+}
+
+/**
+ * pcie_pme_clear_status - Clear root port PME interrupt status.
+ * @dev: PCIe root port or event collector.
+ */
+static void pcie_pme_clear_status(struct pci_dev *dev)
+{
+ int rtsta_pos;
+ u32 rtsta;
+
+ rtsta_pos = pci_find_capability(dev, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ pci_read_config_dword(dev, rtsta_pos, &rtsta);
+ rtsta |= PCI_EXP_RTSTA_PME;
+ pci_write_config_dword(dev, rtsta_pos, rtsta);
+}
+
+/**
+ * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
+ * @bus: PCI bus to scan.
+ *
+ * Scan given PCI bus and all buses under it for devices asserting PME#.
+ */
+static bool pcie_pme_walk_bus(struct pci_bus *bus)
+{
+ struct pci_dev *dev;
+ bool ret = false;
+
+ list_for_each_entry(dev, &bus->devices, bus_list) {
+ /* Skip PCIe devices in case we started from a root port. */
+ if (!dev->is_pcie && pci_check_pme_status(dev)) {
+ pm_request_resume(&dev->dev);
+ ret = true;
+ }
+
+ if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
+ ret = true;
+ }
+
+ return ret;
+}
+
+/**
+ * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
+ * @bus: Secondary bus of the bridge.
+ * @devfn: Device/function number to check.
+ *
+ * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
+ * PCIe PME message. In such that case the bridge should use the Requester ID
+ * of device/function number 0 on its secondary bus.
+ */
+static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
+{
+ struct pci_dev *dev;
+ bool found = false;
+
+ if (devfn)
+ return false;
+
+ dev = pci_dev_get(bus->self);
+ if (!dev)
+ return false;
+
+ if (dev->is_pcie && dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
+ down_read(&pci_bus_sem);
+ if (pcie_pme_walk_bus(bus))
+ found = true;
+ up_read(&pci_bus_sem);
+ }
+
+ pci_dev_put(dev);
+ return found;
+}
+
+/**
+ * pcie_pme_handle_request - Find device that generated PME and handle it.
+ * @port: Root port or event collector that generated the PME interrupt.
+ * @req_id: PCIe Requester ID of the device that generated the PME.
+ */
+static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
+{
+ u8 busnr = req_id >> 8, devfn = req_id & 0xff;
+ struct pci_bus *bus;
+ struct pci_dev *dev;
+ bool found = false;
+
+ /* First, check if the PME is from the root port itself. */
+ if (port->devfn == devfn && port->bus->number == busnr) {
+ if (pci_check_pme_status(port)) {
+ pm_request_resume(&port->dev);
+ found = true;
+ } else {
+ /*
+ * Apparently, the root port generated the PME on behalf
+ * of a non-PCIe device downstream. If this is done by
+ * a root port, the Requester ID field in its status
+ * register may contain either the root port's, or the
+ * source device's information (PCI Express Base
+ * Specification, Rev. 2.0, Section 6.1.9).
+ */
+ down_read(&pci_bus_sem);
+ found = pcie_pme_walk_bus(port->subordinate);
+ up_read(&pci_bus_sem);
+ }
+ goto out;
+ }
+
+ /* Second, find the bus the source device is on. */
+ bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
+ if (!bus)
+ goto out;
+
+ /* Next, check if the PME is from a PCIe-PCI bridge. */
+ found = pcie_pme_from_pci_bridge(bus, devfn);
+ if (found)
+ goto out;
+
+ /* Finally, try to find the PME source on the bus. */
+ down_read(&pci_bus_sem);
+ list_for_each_entry(dev, &bus->devices, bus_list) {
+ pci_dev_get(dev);
+ if (dev->devfn == devfn) {
+ found = true;
+ break;
+ }
+ pci_dev_put(dev);
+ }
+ up_read(&pci_bus_sem);
+
+ if (found) {
+ /* The device is there, but we have to check its PME status. */
+ found = pci_check_pme_status(dev);
+ if (found)
+ pm_request_resume(&dev->dev);
+ pci_dev_put(dev);
+ } else if (devfn) {
+ /*
+ * The device is not there, but we can still try to recover by
+ * assuming that the PME was reported by a PCIe-PCI bridge that
+ * used devfn different from zero.
+ */
+ dev_dbg(&port->dev, "PME interrupt generated for "
+ "non-existent device %02x:%02x.%d\n",
+ busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
+ found = pcie_pme_from_pci_bridge(bus, 0);
+ }
+
+ out:
+ if (!found)
+ dev_dbg(&port->dev, "Spurious native PME interrupt!\n");
+}
+
+/**
+ * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
+ * @work: Work structure giving access to service data.
+ */
+static void pcie_pme_work_fn(struct work_struct *work)
+{
+ struct pcie_pme_service_data *data =
+ container_of(work, struct pcie_pme_service_data, work);
+ struct pci_dev *port = data->srv->port;
+ int rtsta_pos;
+ u32 rtsta;
+
+ rtsta_pos = pci_find_capability(port, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ spin_lock_irq(&data->lock);
+
+ for (;;) {
+ if (data->noirq)
+ break;
+
+ pci_read_config_dword(port, rtsta_pos, &rtsta);
+ if (rtsta & PCI_EXP_RTSTA_PME) {
+ /*
+ * Clear PME status of the port. If there are other
+ * pending PMEs, the status will be set again.
+ */
+ pcie_pme_clear_status(port);
+
+ spin_unlock_irq(&data->lock);
+ pcie_pme_handle_request(port, rtsta & 0xffff);
+ spin_lock_irq(&data->lock);
+
+ continue;
+ }
+
+ /* No need to loop if there are no more PMEs pending. */
+ if (!(rtsta & PCI_EXP_RTSTA_PENDING))
+ break;
+
+ spin_unlock_irq(&data->lock);
+ cpu_relax();
+ spin_lock_irq(&data->lock);
+ }
+
+ if (!data->noirq)
+ pcie_pme_interrupt_enable(port, true);
+
+ spin_unlock_irq(&data->lock);
+}
+
+/**
+ * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
+ * @irq: Interrupt vector.
+ * @context: Interrupt context pointer.
+ */
+static irqreturn_t pcie_pme_irq(int irq, void *context)
+{
+ struct pci_dev *port;
+ struct pcie_pme_service_data *data;
+ int rtsta_pos;
+ u32 rtsta;
+ unsigned long flags;
+
+ port = ((struct pcie_device *)context)->port;
+ data = get_service_data((struct pcie_device *)context);
+
+ rtsta_pos = pci_find_capability(port, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ spin_lock_irqsave(&data->lock, flags);
+ pci_read_config_dword(port, rtsta_pos, &rtsta);
+
+ if (!(rtsta & PCI_EXP_RTSTA_PME)) {
+ spin_unlock_irqrestore(&data->lock, flags);
+ return IRQ_NONE;
+ }
+
+ pcie_pme_interrupt_enable(port, false);
+ spin_unlock_irqrestore(&data->lock, flags);
+
+ /* We don't use pm_wq, because it's freezable. */
+ schedule_work(&data->work);
+
+ return IRQ_HANDLED;
+}
+
+/**
+ * pcie_pme_set_native - Set the PME interrupt flag for given device.
+ * @dev: PCI device to handle.
+ * @ign: Ignored.
+ */
+static int pcie_pme_set_native(struct pci_dev *dev, void *ign)
+{
+ dev_info(&dev->dev, "Signaling PME through PCIe PME interrupt\n");
+
+ device_set_run_wake(&dev->dev, true);
+ dev->pme_interrupt = true;
+ return 0;
+}
+
+/**
+ * pcie_pme_mark_devices - Set the PME interrupt flag for devices below a port.
+ * @port: PCIe root port or event collector to handle.
+ *
+ * For each device below given root port, including the port itself (or for each
+ * root complex integrated endpoint if @port is a root complex event collector)
+ * set the flag indicating that it can signal run-time wake-up events via PCIe
+ * PME interrupts.
+ */
+static void pcie_pme_mark_devices(struct pci_dev *port)
+{
+ pcie_pme_set_native(port, NULL);
+ if (port->subordinate) {
+ pci_walk_bus(port->subordinate, pcie_pme_set_native, NULL);
+ } else {
+ struct pci_bus *bus = port->bus;
+ struct pci_dev *dev;
+
+ /* Check if this is a root port event collector. */
+ if (port->pcie_type != PCI_EXP_TYPE_RC_EC || !bus)
+ return;
+
+ down_read(&pci_bus_sem);
+ list_for_each_entry(dev, &bus->devices, bus_list)
+ if (dev->is_pcie
+ && dev->pcie_type == PCI_EXP_TYPE_RC_END)
+ pcie_pme_set_native(dev, NULL);
+ up_read(&pci_bus_sem);
+ }
+}
+
+/**
+ * pcie_pme_probe - Initialize PCIe PME service for given root port.
+ * @srv: PCIe service to initialize.
+ */
+static int pcie_pme_probe(struct pcie_device *srv)
+{
+ struct pci_dev *port;
+ struct pcie_pme_service_data *data;
+ int ret;
+
+ if (!pcie_pme_platform_setup(srv))
+ return -EACCES;
+
+ data = kzalloc(sizeof(*data), GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
+ spin_lock_init(&data->lock);
+ INIT_WORK(&data->work, pcie_pme_work_fn);
+ data->srv = srv;
+ set_service_data(srv, data);
+
+ port = srv->port;
+ pcie_pme_interrupt_enable(port, false);
+ pcie_pme_clear_status(port);
+
+ ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
+ if (ret) {
+ kfree(data);
+ } else {
+ pcie_pme_mark_devices(port);
+ pcie_pme_interrupt_enable(port, true);
+ }
+
+ return ret;
+}
+
+/**
+ * pcie_pme_suspend - Suspend PCIe PME service device.
+ * @srv: PCIe service device to suspend.
+ */
+static int pcie_pme_suspend(struct pcie_device *srv)
+{
+ struct pcie_pme_service_data *data = get_service_data(srv);
+ struct pci_dev *port = srv->port;
+
+ spin_lock_irq(&data->lock);
+ pcie_pme_interrupt_enable(port, false);
+ pcie_pme_clear_status(port);
+ data->noirq = true;
+ spin_unlock_irq(&data->lock);
+
+ return 0;
+}
+
+/**
+ * pcie_pme_resume - Resume PCIe PME service device.
+ * @srv - PCIe service device to resume.
+ */
+static int pcie_pme_resume(struct pcie_device *srv)
+{
+ struct pcie_pme_service_data *data = get_service_data(srv);
+ struct pci_dev *port = srv->port;
+
+ spin_lock_irq(&data->lock);
+ data->noirq = false;
+ pcie_pme_clear_status(port);
+ pcie_pme_interrupt_enable(port, true);
+ spin_unlock_irq(&data->lock);
+
+ return 0;
+}
+
+static struct pcie_port_service_driver pcie_pme_driver = {
+ .name = "pcie_pme",
+ .port_type = PCIE_RC_PORT,
+ .service = PCIE_PORT_SERVICE_PME,
+
+ .probe = pcie_pme_probe,
+ .suspend = pcie_pme_suspend,
+ .resume = pcie_pme_resume,
+};
+
+/**
+ * pcie_pme_service_init - Register the PCIe PME service driver.
+ */
+static int __init pcie_pme_service_init(void)
+{
+ return pcie_pme_disabled ?
+ -ENODEV : pcie_port_service_register(&pcie_pme_driver);
+}
+
+module_init(pcie_pme_service_init);
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme_acpi.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/pci/pcie/pme/pcie_pme_acpi.c
@@ -0,0 +1,54 @@
+/*
+ * PCIe Native PME support, ACPI-related part
+ *
+ * Copyright (C) 2009 Rafael J. Wysocki <[email protected]>, Novell Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License V2. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#include <linux/pci.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/acpi.h>
+#include <linux/pci-acpi.h>
+#include <linux/pcieport_if.h>
+
+/**
+ * pcie_pme_acpi_setup - Request the ACPI BIOS to release control over PCIe PME.
+ * @srv - PCIe PME service for a root port or event collector.
+ *
+ * Invoked when the PCIe bus type loads PCIe PME service driver. To avoid
+ * conflict with the BIOS PCIe support requires the BIOS to yield PCIe PME
+ * control to the kernel.
+ */
+int pcie_pme_acpi_setup(struct pcie_device *srv)
+{
+ acpi_status status = AE_NOT_FOUND;
+ struct pci_dev *port = srv->port;
+ acpi_handle handle;
+ int error = 0;
+
+ if (acpi_pci_disabled)
+ return -ENOSYS;
+
+ dev_info(&port->dev, "Requesting control of PCIe PME from ACPI BIOS\n");
+
+ handle = acpi_find_root_bridge_handle(port);
+ if (!handle)
+ return -EINVAL;
+
+ status = acpi_pci_osc_control_set(handle,
+ OSC_PCI_EXPRESS_PME_CONTROL |
+ OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
+ if (ACPI_FAILURE(status)) {
+ dev_info(&port->dev,
+ "Failed to receive control of PCIe PME service: %s\n",
+ (status == AE_SUPPORT || status == AE_NOT_FOUND) ?
+ "no _OSC support" : "ACPI _OSC failed");
+ error = -ENODEV;
+ }
+
+ return error;
+}
Index: linux-2.6/drivers/pci/pcie/pme/Makefile
===================================================================
--- /dev/null
+++ 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
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.h
===================================================================
--- /dev/null
+++ 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
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -238,6 +238,7 @@ struct pci_dev {
configuration space */
unsigned int pme_support:5; /* Bitmask of states from which PME#
can be generated */
+ unsigned int pme_interrupt:1;
unsigned int d1_support:1; /* Low power state D1 is supported */
unsigned int d2_support:1; /* Low power state D2 is supported */
unsigned int no_d1d2:1; /* Only allow D0 and D3 */

2009-11-15 23:58:56

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 5/10] ACPI: Add infrastructure for refcounting GPE consumers

From: Matthew Garrett <[email protected]>

ACPI GPEs may map to multiple devices. The current GPE interface only
provides a mechanism for enabling and disabling GPEs, making it difficult
to change the state of GPEs at runtime without extensive cooperation
between devices. Add an API to allow devices to indicate whether or not
they want their device's GPE to be enabled for both runtime and wakeup
events.

Signed-off-by: Matthew Garrett <[email protected]>
Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/acpica/aclocal.h | 2 +
drivers/acpi/acpica/aclocal.h | 2
drivers/acpi/acpica/evxfevnt.c | 161 +++++++++++++++++++++++++++++++++++++++++
include/acpi/acpixf.h | 8 ++
3 files changed, 171 insertions(+)

Index: linux-2.6/drivers/acpi/acpica/aclocal.h
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/aclocal.h
+++ linux-2.6/drivers/acpi/acpica/aclocal.h
@@ -426,6 +426,8 @@ struct acpi_gpe_event_info {
struct acpi_gpe_register_info *register_info; /* Backpointer to register info */
u8 flags; /* Misc info about this GPE */
u8 gpe_number; /* This GPE */
+ u8 runtime_count;
+ u8 wakeup_count;
};

/* Information about a GPE register pair, one per each status/enable pair in an array */
Index: linux-2.6/drivers/acpi/acpica/evxfevnt.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evxfevnt.c
+++ linux-2.6/drivers/acpi/acpica/evxfevnt.c
@@ -201,6 +201,167 @@ ACPI_EXPORT_SYMBOL(acpi_enable_event)

/*******************************************************************************
*
+ * FUNCTION: acpi_ref_runtime_gpe
+ *
+ * PARAMETERS: gpe_device - Parent GPE Device
+ * gpe_number - GPE level within the GPE block
+ *
+ * RETURN: Status
+ *
+ * DESCRIPTION: Take a reference to a runtime GPE
+ *
+ ******************************************************************************/
+acpi_status acpi_ref_runtime_gpe(acpi_handle gpe_device, u32 gpe_number)
+{
+ acpi_status status = AE_OK;
+ acpi_cpu_flags flags;
+ struct acpi_gpe_event_info *gpe_event_info;
+
+ ACPI_FUNCTION_TRACE(acpi_ref_runtime_gpe);
+
+ flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
+
+ /* Ensure that we have a valid GPE number */
+
+ gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
+ if (!gpe_event_info) {
+ status = AE_BAD_PARAMETER;
+ goto unlock_and_exit;
+ }
+
+ if (++gpe_event_info->runtime_count == 1)
+ status = acpi_ev_enable_gpe(gpe_event_info, TRUE);
+
+ if (ACPI_FAILURE(status))
+ gpe_event_info->runtime_count--;
+
+unlock_and_exit:
+ acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
+ return_ACPI_STATUS(status);
+}
+ACPI_EXPORT_SYMBOL(acpi_ref_runtime_gpe)
+
+/*******************************************************************************
+ *
+ * FUNCTION: acpi_unref_runtime_gpe
+ *
+ * PARAMETERS: gpe_device - Parent GPE Device
+ * gpe_number - GPE level within the GPE block
+ *
+ * RETURN: Status
+ *
+ * DESCRIPTION: Release a reference to a runtime GPE
+ *
+ ******************************************************************************/
+acpi_status acpi_unref_runtime_gpe(acpi_handle gpe_device, u32 gpe_number)
+{
+ acpi_status status = AE_OK;
+ acpi_cpu_flags flags;
+ struct acpi_gpe_event_info *gpe_event_info;
+
+ ACPI_FUNCTION_TRACE(acpi_unref_runtime_gpe);
+
+ flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
+
+ /* Ensure that we have a valid GPE number */
+
+ gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
+ if (!gpe_event_info) {
+ status = AE_BAD_PARAMETER;
+ goto unlock_and_exit;
+ }
+
+ if (--gpe_event_info->runtime_count == 0)
+ acpi_ev_disable_gpe(gpe_event_info);
+
+unlock_and_exit:
+ acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
+ return_ACPI_STATUS(status);
+}
+ACPI_EXPORT_SYMBOL(acpi_unref_runtime_gpe)
+
+/*******************************************************************************
+ *
+ * FUNCTION: acpi_ref_wakeup_gpe
+ *
+ * PARAMETERS: gpe_device - Parent GPE Device
+ * gpe_number - GPE level within the GPE block
+ *
+ * RETURN: Status
+ *
+ * DESCRIPTION: Take a reference to a wakeup GPE
+ *
+ ******************************************************************************/
+acpi_status acpi_ref_wakeup_gpe(acpi_handle gpe_device, u32 gpe_number)
+{
+ acpi_status status = AE_OK;
+ acpi_cpu_flags flags;
+ struct acpi_gpe_event_info *gpe_event_info;
+
+ ACPI_FUNCTION_TRACE(acpi_ref_wakeup_gpe);
+
+ flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
+
+ /* Ensure that we have a valid GPE number */
+
+ gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
+ if (!gpe_event_info) {
+ status = AE_BAD_PARAMETER;
+ goto unlock_and_exit;
+ }
+
+ if (++gpe_event_info->wakeup_count == 1)
+ acpi_ev_update_gpe_enable_masks(gpe_event_info,
+ ACPI_GPE_ENABLE);
+
+unlock_and_exit:
+ acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
+ return_ACPI_STATUS(status);
+}
+ACPI_EXPORT_SYMBOL(acpi_ref_wakeup_gpe)
+
+/*******************************************************************************
+ *
+ * FUNCTION: acpi_unref_wakeup_gpe
+ *
+ * PARAMETERS: gpe_device - Parent GPE Device
+ * gpe_number - GPE level within the GPE block
+ *
+ * RETURN: Status
+ *
+ * DESCRIPTION: Release a reference to a wakeup GPE
+ *
+ ******************************************************************************/
+acpi_status acpi_unref_wakeup_gpe(acpi_handle gpe_device, u32 gpe_number)
+{
+ acpi_status status = AE_OK;
+ acpi_cpu_flags flags;
+ struct acpi_gpe_event_info *gpe_event_info;
+
+ ACPI_FUNCTION_TRACE(acpi_unref_wakeup_gpe);
+
+ flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
+
+ /* Ensure that we have a valid GPE number */
+
+ gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
+ if (!gpe_event_info) {
+ status = AE_BAD_PARAMETER;
+ goto unlock_and_exit;
+ }
+
+ if (--gpe_event_info->wakeup_count == 0)
+ acpi_ev_update_gpe_enable_masks(gpe_event_info,
+ ACPI_GPE_DISABLE);
+
+unlock_and_exit:
+ acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
+ return_ACPI_STATUS(status);
+}
+ACPI_EXPORT_SYMBOL(acpi_unref_wakeup_gpe)
+
+/*******************************************************************************
+ *
* FUNCTION: acpi_set_gpe_type
*
* PARAMETERS: gpe_device - Parent GPE Device
Index: linux-2.6/include/acpi/acpixf.h
===================================================================
--- linux-2.6.orig/include/acpi/acpixf.h
+++ linux-2.6/include/acpi/acpixf.h
@@ -282,6 +282,14 @@ acpi_status acpi_get_event_status(u32 ev
*/
acpi_status acpi_set_gpe_type(acpi_handle gpe_device, u32 gpe_number, u8 type);

+acpi_status acpi_ref_runtime_gpe(acpi_handle gpe_device, u32 gpe_number);
+
+acpi_status acpi_unref_runtime_gpe(acpi_handle gpe_device, u32 gpe_number);
+
+acpi_status acpi_ref_wakeup_gpe(acpi_handle gpe_device, u32 gpe_number);
+
+acpi_status acpi_unref_wakeup_gpe(acpi_handle gpe_device, u32 gpe_number);
+
acpi_status acpi_enable_gpe(acpi_handle gpe_device, u32 gpe_number);

acpi_status acpi_disable_gpe(acpi_handle gpe_device, u32 gpe_number);

2009-11-16 00:00:03

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 6/10] ACPI: Add support for new refcounted GPE API to drivers

From: Matthew Garrett <[email protected]>

Add GPE refcounting support to ACPI drivers that need it. This will
currently do little until the core is changed over to use the new
behaviour.

Signed-off-by: Matthew Garrett <[email protected]>
Signed-off-by; Rafael J. Wysocki <[email protected]>
---
drivers/acpi/button.c | 12 ++++++++++++
drivers/acpi/button.c | 12 ++++++++++++
drivers/acpi/ec.c | 4 +++-
drivers/acpi/wakeup.c | 6 ++++++
3 files changed, 21 insertions(+), 1 deletion(-)

Index: linux-2.6/drivers/acpi/button.c
===================================================================
--- linux-2.6.orig/drivers/acpi/button.c
+++ linux-2.6/drivers/acpi/button.c
@@ -420,6 +420,10 @@ static int acpi_button_add(struct acpi_d
ACPI_GPE_TYPE_WAKE_RUN);
acpi_enable_gpe(device->wakeup.gpe_device,
device->wakeup.gpe_number);
+ acpi_ref_runtime_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
+ acpi_ref_wakeup_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
device->wakeup.state.enabled = 1;
}

@@ -439,6 +443,14 @@ static int acpi_button_remove(struct acp
{
struct acpi_button *button = acpi_driver_data(device);

+ if (device->wakeup.flags.valid) {
+ acpi_unref_runtime_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
+ acpi_unref_wakeup_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
+ device->wakeup.state.enabled = 0;
+ }
+
acpi_button_remove_fs(device);
input_unregister_device(button->input);
kfree(button);
Index: linux-2.6/drivers/acpi/ec.c
===================================================================
--- linux-2.6.orig/drivers/acpi/ec.c
+++ linux-2.6/drivers/acpi/ec.c
@@ -755,7 +755,7 @@ static int ec_install_handlers(struct ac
if (ACPI_FAILURE(status))
return -ENODEV;
acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
- acpi_enable_gpe(NULL, ec->gpe);
+ acpi_ref_runtime_gpe(NULL, ec->gpe);
status = acpi_install_address_space_handler(ec->handle,
ACPI_ADR_SPACE_EC,
&acpi_ec_space_handler,
@@ -772,6 +772,7 @@ static int ec_install_handlers(struct ac
} else {
acpi_remove_gpe_handler(NULL, ec->gpe,
&acpi_ec_gpe_handler);
+ acpi_unref_runtime_gpe(NULL, ec->gpe);
return -ENODEV;
}
}
@@ -782,6 +783,7 @@ static int ec_install_handlers(struct ac

static void ec_remove_handlers(struct acpi_ec *ec)
{
+ acpi_unref_runtime_gpe(NULL, ec->gpe);
if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
pr_err(PREFIX "failed to remove space handler\n");
Index: linux-2.6/drivers/acpi/wakeup.c
===================================================================
--- linux-2.6.orig/drivers/acpi/wakeup.c
+++ linux-2.6/drivers/acpi/wakeup.c
@@ -81,6 +81,8 @@ void acpi_enable_wakeup_device(u8 sleep_
if (!dev->wakeup.flags.run_wake)
acpi_enable_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
+ acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
}
}

@@ -121,6 +123,8 @@ void acpi_disable_wakeup_device(u8 sleep
acpi_clear_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number, ACPI_NOT_ISR);
}
+ acpi_unref_wakeup_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
}
}

@@ -141,6 +145,8 @@ int __init acpi_wakeup_device_init(void)
ACPI_GPE_TYPE_WAKE_RUN);
acpi_enable_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
+ acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
dev->wakeup.state.enabled = 1;
}
mutex_unlock(&acpi_device_lock);

2009-11-15 23:59:22

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 7/10] ACPI: Remove old GPE API and transition code entirely to new one

From: Matthew Garrett <[email protected]>

Remove the old GPE type handling entirely, which gets rid of various quirks
like the implicit disabling with GPE type setting. This requires a small
amount of rework in order to ensure that non-wake GPEs are enabled by
default to preserve existing behaviour.

Signed-off-by: Matthew Garrett <[email protected]>
Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/acpica/acevents.h | 6 +--
drivers/acpi/acpica/acevents.h | 6 -
drivers/acpi/acpica/evgpe.c | 147 +++--------------------------------------
drivers/acpi/acpica/evgpeblk.c | 69 ++++++-------------
drivers/acpi/acpica/evxface.c | 14 ---
drivers/acpi/acpica/evxfevnt.c | 48 -------------
drivers/acpi/button.c | 5 -
drivers/acpi/ec.c | 2
drivers/acpi/wakeup.c | 55 ++-------------
include/acpi/actypes.h | 32 ++------
9 files changed, 59 insertions(+), 319 deletions(-)

Index: linux-2.6/drivers/acpi/acpica/acevents.h
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/acevents.h
+++ linux-2.6/drivers/acpi/acpica/acevents.h
@@ -76,8 +76,7 @@ acpi_ev_queue_notify_request(struct acpi
* evgpe - GPE handling and dispatch
*/
acpi_status
-acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info,
- u8 type);
+acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info);

acpi_status
acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info,
@@ -122,9 +121,6 @@ acpi_ev_gpe_dispatch(struct acpi_gpe_eve
u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info *gpe_xrupt_list);

acpi_status
-acpi_ev_set_gpe_type(struct acpi_gpe_event_info *gpe_event_info, u8 type);
-
-acpi_status
acpi_ev_check_for_wake_only_gpe(struct acpi_gpe_event_info *gpe_event_info);

acpi_status acpi_ev_gpe_initialize(void);
Index: linux-2.6/drivers/acpi/acpica/evgpe.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evgpe.c
+++ linux-2.6/drivers/acpi/acpica/evgpe.c
@@ -54,54 +54,9 @@ static void ACPI_SYSTEM_XFACE acpi_ev_as

/*******************************************************************************
*
- * FUNCTION: acpi_ev_set_gpe_type
- *
- * PARAMETERS: gpe_event_info - GPE to set
- * Type - New type
- *
- * RETURN: Status
- *
- * DESCRIPTION: Sets the new type for the GPE (wake, run, or wake/run)
- *
- ******************************************************************************/
-
-acpi_status
-acpi_ev_set_gpe_type(struct acpi_gpe_event_info *gpe_event_info, u8 type)
-{
- acpi_status status;
-
- ACPI_FUNCTION_TRACE(ev_set_gpe_type);
-
- /* Validate type and update register enable masks */
-
- switch (type) {
- case ACPI_GPE_TYPE_WAKE:
- case ACPI_GPE_TYPE_RUNTIME:
- case ACPI_GPE_TYPE_WAKE_RUN:
- break;
-
- default:
- return_ACPI_STATUS(AE_BAD_PARAMETER);
- }
-
- /* Disable the GPE if currently enabled */
-
- status = acpi_ev_disable_gpe(gpe_event_info);
-
- /* Clear the type bits and insert the new Type */
-
- gpe_event_info->flags &= ~ACPI_GPE_TYPE_MASK;
- gpe_event_info->flags |= type;
- return_ACPI_STATUS(status);
-}
-
-/*******************************************************************************
- *
* FUNCTION: acpi_ev_update_gpe_enable_masks
*
* PARAMETERS: gpe_event_info - GPE to update
- * Type - What to do: ACPI_GPE_DISABLE or
- * ACPI_GPE_ENABLE
*
* RETURN: Status
*
@@ -110,8 +65,7 @@ acpi_ev_set_gpe_type(struct acpi_gpe_eve
******************************************************************************/

acpi_status
-acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info,
- u8 type)
+acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info)
{
struct acpi_gpe_register_info *gpe_register_info;
u8 register_bit;
@@ -127,37 +81,13 @@ acpi_ev_update_gpe_enable_masks(struct a
(1 <<
(gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));

- /* 1) Disable case. Simply clear all enable bits */
-
- if (type == ACPI_GPE_DISABLE) {
- ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
- register_bit);
- ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
- return_ACPI_STATUS(AE_OK);
- }
-
- /* 2) Enable case. Set/Clear the appropriate enable bits */
-
- switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
- case ACPI_GPE_TYPE_WAKE:
- ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
- ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
- break;
+ ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake, register_bit);
+ ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);

- case ACPI_GPE_TYPE_RUNTIME:
- ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
- register_bit);
+ if (gpe_event_info->runtime_count)
ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
- break;
-
- case ACPI_GPE_TYPE_WAKE_RUN:
+ if (gpe_event_info->wakeup_count)
ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
- ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
- break;
-
- default:
- return_ACPI_STATUS(AE_BAD_PARAMETER);
- }

return_ACPI_STATUS(AE_OK);
}
@@ -186,47 +116,21 @@ acpi_ev_enable_gpe(struct acpi_gpe_event

/* Make sure HW enable masks are updated */

- status =
- acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_ENABLE);
+ status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
if (ACPI_FAILURE(status)) {
return_ACPI_STATUS(status);
}

/* Mark wake-enabled or HW enable, or both */

- switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
- case ACPI_GPE_TYPE_WAKE:
-
- ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
- break;
-
- case ACPI_GPE_TYPE_WAKE_RUN:
-
- ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
-
- /*lint -fallthrough */
-
- case ACPI_GPE_TYPE_RUNTIME:
-
- ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
-
- if (write_to_hardware) {
-
- /* Clear the GPE (of stale events), then enable it */
-
- status = acpi_hw_clear_gpe(gpe_event_info);
- if (ACPI_FAILURE(status)) {
- return_ACPI_STATUS(status);
- }
-
- /* Enable the requested runtime GPE */
-
- status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
- }
- break;
+ if (gpe_event_info->runtime_count && write_to_hardware) {
+ /* Clear the GPE (of stale events), then enable it */
+ status = acpi_hw_clear_gpe(gpe_event_info);
+ if (ACPI_FAILURE(status))
+ return_ACPI_STATUS(status);

- default:
- return_ACPI_STATUS(AE_BAD_PARAMETER);
+ /* Enable the requested runtime GPE */
+ status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
}

return_ACPI_STATUS(AE_OK);
@@ -253,34 +157,11 @@ acpi_status acpi_ev_disable_gpe(struct a
/* Make sure HW enable masks are updated */

status =
- acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_DISABLE);
+ acpi_ev_update_gpe_enable_masks(gpe_event_info);
if (ACPI_FAILURE(status)) {
return_ACPI_STATUS(status);
}

- /* Clear the appropriate enabled flags for this GPE */
-
- switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
- case ACPI_GPE_TYPE_WAKE:
- ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
- break;
-
- case ACPI_GPE_TYPE_WAKE_RUN:
- ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
-
- /* fallthrough */
-
- case ACPI_GPE_TYPE_RUNTIME:
-
- /* Disable the requested runtime GPE */
-
- ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
- break;
-
- default:
- break;
- }
-
/*
* Even if we don't know the GPE type, make sure that we always
* disable it. low_disable_gpe will just clear the enable bit for this
Index: linux-2.6/drivers/acpi/acpica/evgpeblk.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evgpeblk.c
+++ linux-2.6/drivers/acpi/acpica/evgpeblk.c
@@ -325,22 +325,16 @@ acpi_ev_save_method_info(acpi_handle obj

/*
* Now we can add this information to the gpe_event_info block for use
- * during dispatch of this GPE. Default type is RUNTIME, although this may
- * change when the _PRW methods are executed later.
+ * during dispatch of this GPE.
*/
gpe_event_info =
&gpe_block->event_info[gpe_number - gpe_block->block_base_number];

- gpe_event_info->flags = (u8)
- (type | ACPI_GPE_DISPATCH_METHOD | ACPI_GPE_TYPE_RUNTIME);
+ gpe_event_info->flags = (u8) (type | ACPI_GPE_DISPATCH_METHOD);

gpe_event_info->dispatch.method_node =
(struct acpi_namespace_node *)obj_handle;

- /* Update enable mask, but don't enable the HW GPE as of yet */
-
- status = acpi_ev_enable_gpe(gpe_event_info, FALSE);
-
ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
"Registered GPE method %s as GPE number 0x%.2X\n",
name, gpe_number));
@@ -454,20 +448,7 @@ acpi_ev_match_prw_and_gpe(acpi_handle ob
gpe_block->
block_base_number];

- /* Mark GPE for WAKE-ONLY but WAKE_DISABLED */
-
- gpe_event_info->flags &=
- ~(ACPI_GPE_WAKE_ENABLED | ACPI_GPE_RUN_ENABLED);
-
- status =
- acpi_ev_set_gpe_type(gpe_event_info, ACPI_GPE_TYPE_WAKE);
- if (ACPI_FAILURE(status)) {
- goto cleanup;
- }
-
- status =
- acpi_ev_update_gpe_enable_masks(gpe_event_info,
- ACPI_GPE_DISABLE);
+ gpe_event_info->flags |= ACPI_GPE_CAN_WAKE;
}

cleanup:
@@ -1027,33 +1008,35 @@ acpi_ev_initialize_gpe_block(struct acpi
}

/*
- * Enable all GPEs in this block that have these attributes:
- * 1) are "runtime" or "run/wake" GPEs, and
- * 2) have a corresponding _Lxx or _Exx method
- *
- * Any other GPEs within this block must be enabled via the
- * acpi_enable_gpe() external interface.
+ * Enable all GPEs that have a corresponding method and aren't
+ * capable of generating wakeups. Any other GPEs within this block
+ * must be enabled via the acpi_ref_runtime_gpe interface.
*/
wake_gpe_count = 0;
gpe_enabled_count = 0;

for (i = 0; i < gpe_block->register_count; i++) {
for (j = 0; j < 8; j++) {
+ int gpe_number = i * ACPI_GPE_REGISTER_WIDTH + j;

/* Get the info block for this particular GPE */

- gpe_event_info = &gpe_block->event_info[((acpi_size) i *
- ACPI_GPE_REGISTER_WIDTH)
- + j];
-
- if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
- ACPI_GPE_DISPATCH_METHOD) &&
- (gpe_event_info->flags & ACPI_GPE_TYPE_RUNTIME)) {
- gpe_enabled_count++;
- }
+ gpe_event_info = &gpe_block->event_info[(acpi_size)
+ gpe_number];

- if (gpe_event_info->flags & ACPI_GPE_TYPE_WAKE) {
+ if (gpe_event_info->flags & ACPI_GPE_CAN_WAKE) {
wake_gpe_count++;
+ continue;
+ }
+
+ if (gpe_event_info->flags & ACPI_GPE_DISPATCH_METHOD) {
+ gpe_enabled_count++;
+ if (gpe_device == acpi_gbl_fadt_gpe_device)
+ status = acpi_ref_runtime_gpe(NULL,
+ gpe_number);
+ else
+ status = acpi_ref_runtime_gpe(gpe_device,
+ gpe_number);
}
}
}
@@ -1062,15 +1045,7 @@ acpi_ev_initialize_gpe_block(struct acpi
"Found %u Wake, Enabled %u Runtime GPEs in this block\n",
wake_gpe_count, gpe_enabled_count));

- /* Enable all valid runtime GPEs found above */
-
- status = acpi_hw_enable_runtime_gpe_block(NULL, gpe_block, NULL);
- if (ACPI_FAILURE(status)) {
- ACPI_ERROR((AE_INFO, "Could not enable GPEs in GpeBlock %p",
- gpe_block));
- }
-
- return_ACPI_STATUS(status);
+ return_ACPI_STATUS(AE_OK);
}

/*******************************************************************************
Index: linux-2.6/drivers/acpi/acpica/evxface.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evxface.c
+++ linux-2.6/drivers/acpi/acpica/evxface.c
@@ -617,13 +617,6 @@ acpi_install_gpe_handler(acpi_handle gpe
handler->context = context;
handler->method_node = gpe_event_info->dispatch.method_node;

- /* Disable the GPE before installing the handler */
-
- status = acpi_ev_disable_gpe(gpe_event_info);
- if (ACPI_FAILURE(status)) {
- goto unlock_and_exit;
- }
-
/* Install the handler */

flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
@@ -707,13 +700,6 @@ acpi_remove_gpe_handler(acpi_handle gpe_
goto unlock_and_exit;
}

- /* Disable the GPE before removing the handler */
-
- status = acpi_ev_disable_gpe(gpe_event_info);
- if (ACPI_FAILURE(status)) {
- goto unlock_and_exit;
- }
-
/* Make sure all deferred tasks are completed */

(void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Index: linux-2.6/drivers/acpi/acpica/evxfevnt.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evxfevnt.c
+++ linux-2.6/drivers/acpi/acpica/evxfevnt.c
@@ -311,8 +311,7 @@ acpi_status acpi_ref_wakeup_gpe(acpi_han
}

if (++gpe_event_info->wakeup_count == 1)
- acpi_ev_update_gpe_enable_masks(gpe_event_info,
- ACPI_GPE_ENABLE);
+ acpi_ev_update_gpe_enable_masks(gpe_event_info);

unlock_and_exit:
acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
@@ -351,8 +350,7 @@ acpi_status acpi_unref_wakeup_gpe(acpi_h
}

if (--gpe_event_info->wakeup_count == 0)
- acpi_ev_update_gpe_enable_masks(gpe_event_info,
- ACPI_GPE_DISABLE);
+ acpi_ev_update_gpe_enable_masks(gpe_event_info);

unlock_and_exit:
acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
@@ -362,48 +360,6 @@ ACPI_EXPORT_SYMBOL(acpi_unref_wakeup_gpe

/*******************************************************************************
*
- * FUNCTION: acpi_set_gpe_type
- *
- * PARAMETERS: gpe_device - Parent GPE Device
- * gpe_number - GPE level within the GPE block
- * Type - New GPE type
- *
- * RETURN: Status
- *
- * DESCRIPTION: Set the type of an individual GPE
- *
- ******************************************************************************/
-acpi_status acpi_set_gpe_type(acpi_handle gpe_device, u32 gpe_number, u8 type)
-{
- acpi_status status = AE_OK;
- struct acpi_gpe_event_info *gpe_event_info;
-
- ACPI_FUNCTION_TRACE(acpi_set_gpe_type);
-
- /* Ensure that we have a valid GPE number */
-
- gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
- if (!gpe_event_info) {
- status = AE_BAD_PARAMETER;
- goto unlock_and_exit;
- }
-
- if ((gpe_event_info->flags & ACPI_GPE_TYPE_MASK) == type) {
- return_ACPI_STATUS(AE_OK);
- }
-
- /* Set the new type (will disable GPE if currently enabled) */
-
- status = acpi_ev_set_gpe_type(gpe_event_info, type);
-
- unlock_and_exit:
- return_ACPI_STATUS(status);
-}
-
-ACPI_EXPORT_SYMBOL(acpi_set_gpe_type)
-
-/*******************************************************************************
- *
* FUNCTION: acpi_enable_gpe
*
* PARAMETERS: gpe_device - Parent GPE Device
Index: linux-2.6/drivers/acpi/button.c
===================================================================
--- linux-2.6.orig/drivers/acpi/button.c
+++ linux-2.6/drivers/acpi/button.c
@@ -415,11 +415,6 @@ static int acpi_button_add(struct acpi_d

if (device->wakeup.flags.valid) {
/* Button's GPE is run-wake GPE */
- acpi_set_gpe_type(device->wakeup.gpe_device,
- device->wakeup.gpe_number,
- ACPI_GPE_TYPE_WAKE_RUN);
- acpi_enable_gpe(device->wakeup.gpe_device,
- device->wakeup.gpe_number);
acpi_ref_runtime_gpe(device->wakeup.gpe_device,
device->wakeup.gpe_number);
acpi_ref_wakeup_gpe(device->wakeup.gpe_device,
Index: linux-2.6/drivers/acpi/ec.c
===================================================================
--- linux-2.6.orig/drivers/acpi/ec.c
+++ linux-2.6/drivers/acpi/ec.c
@@ -754,7 +754,7 @@ static int ec_install_handlers(struct ac
&acpi_ec_gpe_handler, ec);
if (ACPI_FAILURE(status))
return -ENODEV;
- acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
+
acpi_ref_runtime_gpe(NULL, ec->gpe);
status = acpi_install_address_space_handler(ec->handle,
ACPI_ADR_SPACE_EC,
Index: linux-2.6/drivers/acpi/wakeup.c
===================================================================
--- linux-2.6.orig/drivers/acpi/wakeup.c
+++ linux-2.6/drivers/acpi/wakeup.c
@@ -62,25 +62,12 @@ void acpi_enable_wakeup_device(u8 sleep_
struct acpi_device *dev =
container_of(node, struct acpi_device, wakeup_list);

- if (!dev->wakeup.flags.valid)
+ if (!dev->wakeup.flags.valid ||
+ !dev->wakeup.prepare_count ||
+ !dev->wakeup.state.enabled ||
+ (sleep_state > (u32) dev->wakeup.sleep_state))
continue;

- /* If users want to disable run-wake GPE,
- * we only disable it for wake and leave it for runtime
- */
- if ((!dev->wakeup.state.enabled && !dev->wakeup.prepare_count)
- || sleep_state > (u32) dev->wakeup.sleep_state) {
- if (dev->wakeup.flags.run_wake) {
- /* set_gpe_type will disable GPE, leave it like that */
- acpi_set_gpe_type(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number,
- ACPI_GPE_TYPE_RUNTIME);
- }
- continue;
- }
- if (!dev->wakeup.flags.run_wake)
- acpi_enable_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
}
@@ -99,32 +86,13 @@ void acpi_disable_wakeup_device(u8 sleep
struct acpi_device *dev =
container_of(node, struct acpi_device, wakeup_list);

- if (!dev->wakeup.flags.valid)
- continue;
-
- if ((!dev->wakeup.state.enabled && !dev->wakeup.prepare_count)
- || sleep_state > (u32) dev->wakeup.sleep_state) {
- if (dev->wakeup.flags.run_wake) {
- acpi_set_gpe_type(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number,
- ACPI_GPE_TYPE_WAKE_RUN);
- /* Re-enable it, since set_gpe_type will disable it */
- acpi_enable_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
- }
- continue;
- }
+ if (dev->wakeup.state.enabled &&
+ dev->wakeup.prepare_count &&
+ sleep_state <= (u32) dev->wakeup.sleep_state)
+ acpi_unref_wakeup_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);

acpi_disable_wakeup_device_power(dev);
- /* Never disable run-wake GPE */
- if (!dev->wakeup.flags.run_wake) {
- acpi_disable_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
- acpi_clear_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number, ACPI_NOT_ISR);
- }
- acpi_unref_wakeup_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
}
}

@@ -140,11 +108,6 @@ int __init acpi_wakeup_device_init(void)
/* In case user doesn't load button driver */
if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
continue;
- acpi_set_gpe_type(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number,
- ACPI_GPE_TYPE_WAKE_RUN);
- acpi_enable_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
dev->wakeup.state.enabled = 1;
Index: linux-2.6/include/acpi/actypes.h
===================================================================
--- linux-2.6.orig/include/acpi/actypes.h
+++ linux-2.6/include/acpi/actypes.h
@@ -668,41 +668,29 @@ typedef u32 acpi_event_status;

/*
* GPE info flags - Per GPE
- * +-+-+-+---+---+-+
- * |7|6|5|4:3|2:1|0|
- * +-+-+-+---+---+-+
- * | | | | | |
- * | | | | | +--- Interrupt type: Edge or Level Triggered
- * | | | | +--- Type: Wake-only, Runtime-only, or wake/runtime
+ * +-+-+-+---+-+-+-+
+ * |7|6|5|4:3|2|1|0|
+ * +-+-+-+---+-+-+-+
+ * | | | | | | |
+ * | | | | | | +--- Interrupt type: Edge or Level Triggered
+ * | | | | | +--- GPE can wake the system
+ * | | | | +--- Unused
* | | | +--- Type of dispatch -- to method, handler, or none
- * | | +--- Enabled for runtime?
- * | +--- Enabled for wake?
+ * | | +--- Unused
+ * | +--- Unused
* +--- Unused
*/
#define ACPI_GPE_XRUPT_TYPE_MASK (u8) 0x01
#define ACPI_GPE_LEVEL_TRIGGERED (u8) 0x01
#define ACPI_GPE_EDGE_TRIGGERED (u8) 0x00

-#define ACPI_GPE_TYPE_MASK (u8) 0x06
-#define ACPI_GPE_TYPE_WAKE_RUN (u8) 0x06
-#define ACPI_GPE_TYPE_WAKE (u8) 0x02
-#define ACPI_GPE_TYPE_RUNTIME (u8) 0x04 /* Default */
+#define ACPI_GPE_CAN_WAKE (u8) 0x02

#define ACPI_GPE_DISPATCH_MASK (u8) 0x18
#define ACPI_GPE_DISPATCH_HANDLER (u8) 0x08
#define ACPI_GPE_DISPATCH_METHOD (u8) 0x10
#define ACPI_GPE_DISPATCH_NOT_USED (u8) 0x00 /* Default */

-#define ACPI_GPE_RUN_ENABLE_MASK (u8) 0x20
-#define ACPI_GPE_RUN_ENABLED (u8) 0x20
-#define ACPI_GPE_RUN_DISABLED (u8) 0x00 /* Default */
-
-#define ACPI_GPE_WAKE_ENABLE_MASK (u8) 0x40
-#define ACPI_GPE_WAKE_ENABLED (u8) 0x40
-#define ACPI_GPE_WAKE_DISABLED (u8) 0x00 /* Default */
-
-#define ACPI_GPE_ENABLE_MASK (u8) 0x60 /* Both run/wake */
-
/*
* Flags for GPE and Lock interfaces
*/

2009-11-15 23:59:02

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 8/10] ACPI / PM: Add more run-time wake-up fields

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

Use the run_wake flag to mark all devices for which run-time wake-up
events may be generated by the platform. Introduce a new wake-up
flag, special, for marking devices that should be permanently enabled
to generate run-time events. Also, introduce a reference counter for
run-wake devices and a function that will initialize all of the
run-time wake-up fields for given device.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/button.c | 2 ++
drivers/acpi/scan.c | 37 +++++++++++++++++++++++++++----------
drivers/acpi/wakeup.c | 2 +-
include/acpi/acpi_bus.h | 2 ++
4 files changed, 32 insertions(+), 11 deletions(-)

Index: linux-2.6/drivers/acpi/wakeup.c
===================================================================
--- linux-2.6.orig/drivers/acpi/wakeup.c
+++ linux-2.6/drivers/acpi/wakeup.c
@@ -106,7 +106,7 @@ int __init acpi_wakeup_device_init(void)
struct acpi_device,
wakeup_list);
/* In case user doesn't load button driver */
- if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
+ if (!dev->wakeup.flags.special || dev->wakeup.state.enabled)
continue;
acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
--- linux-2.6.orig/include/acpi/acpi_bus.h
+++ linux-2.6/include/acpi/acpi_bus.h
@@ -242,6 +242,7 @@ struct acpi_device_perf {
struct acpi_device_wakeup_flags {
u8 valid:1; /* Can successfully enable wakeup? */
u8 run_wake:1; /* Run-Wake GPE devices */
+ u8 special:1; /* Run-wake devices that are always enabled */
};

struct acpi_device_wakeup_state {
@@ -256,6 +257,7 @@ struct acpi_device_wakeup {
struct acpi_device_wakeup_state state;
struct acpi_device_wakeup_flags flags;
int prepare_count;
+ int run_wake_count;
};

/* Device */
Index: linux-2.6/drivers/acpi/button.c
===================================================================
--- linux-2.6.orig/drivers/acpi/button.c
+++ linux-2.6/drivers/acpi/button.c
@@ -419,6 +419,7 @@ static int acpi_button_add(struct acpi_d
device->wakeup.gpe_number);
acpi_ref_wakeup_gpe(device->wakeup.gpe_device,
device->wakeup.gpe_number);
+ device->wakeup.run_wake_count++;
device->wakeup.state.enabled = 1;
}

@@ -443,6 +444,7 @@ static int acpi_button_remove(struct acp
device->wakeup.gpe_number);
acpi_unref_wakeup_gpe(device->wakeup.gpe_device,
device->wakeup.gpe_number);
+ device->wakeup.run_wake_count--;
device->wakeup.state.enabled = 0;
}

Index: linux-2.6/drivers/acpi/scan.c
===================================================================
--- linux-2.6.orig/drivers/acpi/scan.c
+++ linux-2.6/drivers/acpi/scan.c
@@ -742,19 +742,39 @@ acpi_bus_extract_wakeup_device_power_pac
return AE_OK;
}

-static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
+static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
{
- acpi_status status = 0;
- struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
- union acpi_object *package = NULL;
- int psw_error;
-
struct acpi_device_id button_device_ids[] = {
{"PNP0C0D", 0},
{"PNP0C0C", 0},
{"PNP0C0E", 0},
{"", 0},
};
+ acpi_status status;
+ acpi_event_status event_status;
+
+ device->wakeup.run_wake_count = 0;
+
+ /* Power button, Lid switch always enable wakeup */
+ if (!acpi_match_device_ids(device, button_device_ids)) {
+ device->wakeup.flags.run_wake = 1;
+ device->wakeup.flags.special = 1;
+ return;
+ }
+
+ status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
+ ACPI_NOT_ISR, &event_status);
+ if (status == AE_OK)
+ device->wakeup.flags.run_wake =
+ !!(event_status & ACPI_EVENT_FLAG_HANDLE);
+}
+
+static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
+{
+ acpi_status status = 0;
+ struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
+ union acpi_object *package = NULL;
+ int psw_error;

/* _PRW */
status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
@@ -774,6 +794,7 @@ static int acpi_bus_get_wakeup_device_fl

device->wakeup.flags.valid = 1;
device->wakeup.prepare_count = 0;
+ acpi_bus_set_run_wake_flags(device);
/* Call _PSW/_DSW object to disable its ability to wake the sleeping
* system for the ACPI device with the _PRW object.
* The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
@@ -785,10 +806,6 @@ static int acpi_bus_get_wakeup_device_fl
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"error in _DSW or _PSW evaluation\n"));

- /* Power button, Lid switch always enable wakeup */
- if (!acpi_match_device_ids(device, button_device_ids))
- device->wakeup.flags.run_wake = 1;
-
end:
if (ACPI_FAILURE(status))
device->flags.wake_capable = 0;

2009-11-15 23:59:13

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 9/10] PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 3)

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. Moreover, 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 new PCI platform callback ->test_run_wake() and ->run_wake() to
struct pci_platform_pm_ops allowing us, respectively, to check if
if the platform can generate run-time wake-up events for given
device and to enable/disable the platform to do that. Implemet
these callbacks 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_dev_run_wake() which can be used by PCI drivers to
check if given device is capable of generating wake-up events at
run time.

Developed in cooperation with Matthew Garrett <[email protected]>.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/pci_bind.c | 13 +
drivers/acpi/pci_root.c | 7
drivers/acpi/sleep.c | 22 +-
drivers/pci/hotplug/acpiphp_glue.c | 23 --
drivers/pci/pci-acpi.c | 399 +++++++++++++++++++++++++++++++++++++
drivers/pci/pci.c | 50 ++++
drivers/pci/pci.h | 12 +
include/acpi/acpi_bus.h | 11 -
include/linux/pci-acpi.h | 24 ++
include/linux/pci.h | 1
kernel/power/Kconfig | 5
11 files changed, 543 insertions(+), 24 deletions(-)

Index: linux-2.6/drivers/pci/pci.h
===================================================================
--- linux-2.6.orig/drivers/pci/pci.h
+++ linux-2.6/drivers/pci/pci.h
@@ -35,6 +35,10 @@ int pci_probe_reset_function(struct pci_
*
* @sleep_wake: enables/disables the system 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,24 @@ 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 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);
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci-acpi.c
+++ linux-2.6/drivers/pci/pci-acpi.c
@@ -16,9 +16,375 @@
#include <acpi/acpi_bus.h>

#include <linux/pci-acpi.h>
+#include <linux/pm_runtime.h>
#include "pci.h"

/*
+ * ACPI-based PCI run-time power management uses ACPI system notify handlers,
+ * which are also used by ACPI-based PCI hotplug. Unfortunately, however, there
+ * can be only one ACPI system notify handler installed for an ACPI device
+ * handle. For this reason there has to be a way to use the same notify handler
+ * for both ACPI-based hotplug and ACPI-based run-time PM.
+ *
+ * pci_acpi_runtime_notifiers is a list of struct pci_acpi_notifier_block
+ * objects representing PCI devices that have ACPI system notify handlers
+ * installed. For each of them, there is an ACPI-based hotplug notifier to
+ * execute for hotplug events, hp_cb, and a pointer to the data to pass to it
+ * hp_data, as well as poitners to a struct pci_bus object and a struct pci_dev
+ * object. If the device is a PCI-to-PCI bridge or root bridge, the struct
+ * pci_bus pointer is not NULL and it is assumed that the whole bus segment
+ * below the bridge has to be walked if a PME is reported for it. Otherwise,
+ * it is assumed that PME is generated only for the particular PCI device
+ * pointed to by the pci_dev field.
+ */
+
+static LIST_HEAD(pci_acpi_runtime_notifiers);
+static DEFINE_MUTEX(pci_acpi_notifier_mtx);
+
+struct pci_acpi_notifier_block
+{
+ struct list_head entry;
+ struct acpi_device *dev;
+ acpi_notify_handler hp_cb;
+ void *hp_data;
+ struct pci_bus *pci_bus;
+ struct pci_dev *pci_dev;
+};
+
+/**
+ * pci_acpi_event_fn - Universal system notification handler.
+ * @handle: ACPI handle of a device the notification is for.
+ * @event: Type of the signaled event.
+ * @data: Context data, should be a pointer to a notifier object.
+ *
+ * Take the address on a notifier object from @data and use it to extract the
+ * information needed for handling the event. If this is a wake-up event,
+ * check if PM notification is enabled for this notifier object and, if so,
+ * execute the appropriate PME handler for the bus or device represented by it.
+ * If this is not a wake-up event, execute the hotplug notify handler for
+ * @handle.
+ */
+static void pci_acpi_event_fn(acpi_handle handle, u32 event, void *data)
+{
+ struct pci_acpi_notifier_block *nb = data;
+
+ if (!nb)
+ return;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ if (event == ACPI_NOTIFY_DEVICE_WAKE) {
+ if (nb->dev->wakeup.run_wake_count > 0) {
+ if (nb->pci_bus)
+ pci_pme_wakeup_bus(nb->pci_bus);
+ if (nb->pci_dev)
+ pci_pme_wakeup(nb->pci_dev);
+ }
+ } else if (nb->hp_cb) {
+ nb->hp_cb(handle, event, nb->hp_data);
+ }
+
+ mutex_unlock(&pci_acpi_notifier_mtx);
+}
+
+/**
+ * new_notifier - Create a new notifier object for given ACPI device.
+ * @dev: Device to create the notifier object for.
+ */
+static struct pci_acpi_notifier_block *new_notifier(struct acpi_device *dev)
+{
+ struct pci_acpi_notifier_block *nb;
+
+ nb = kzalloc(sizeof(*nb), GFP_KERNEL);
+ if (!nb)
+ return NULL;
+
+ nb->dev = dev;
+ return nb;
+}
+
+/**
+ * pci_acpi_add_hp_notifier - Register a hotplug notifier for given device.
+ * @handle: ACPI handle of the device to register the notifier for.
+ * @handler: Callback to execute for hotplug events related to @handle.
+ * @context: Pointer to the context data to pass to @handler.
+ *
+ * Use @handle to get an ACPI device object and check if there is a notifier
+ * object for it. If this is the case, add @handler and @context to the
+ * existing notifier object, unless there already is a hotplug handler in this
+ * notifier object. Otherwise, create a new notifier object for the ACPI device
+ * associated with @handle and add @handler and @context to it.
+ */
+acpi_status pci_acpi_add_hp_notifier(acpi_handle handle,
+ acpi_notify_handler handler, void *context)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_status status = AE_OK;
+
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev)))
+ return AE_NOT_FOUND;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry) {
+ if (nb->dev != dev)
+ continue;
+
+ if (!nb->hp_cb) {
+ nb->hp_cb = handler;
+ nb->hp_data = context;
+ } else {
+ status = AE_ALREADY_EXISTS;
+ }
+ goto out;
+ }
+
+ nb = new_notifier(dev);
+ if (!nb) {
+ status = AE_NO_MEMORY;
+ goto out;
+ }
+ nb->hp_cb = handler;
+ nb->hp_data = context;
+
+ list_add_tail(&nb->entry, &pci_acpi_runtime_notifiers);
+
+ status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn, nb);
+ if (ACPI_FAILURE(status)) {
+ list_del(&nb->entry);
+ kfree(nb);
+ }
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+
+ return status;
+}
+EXPORT_SYMBOL_GPL(pci_acpi_add_hp_notifier);
+
+/**
+ * pci_acpi_remove_hp_notifier - Unregister a hotplug notifier for given device.
+ * @handle: ACPI handle of the device to unregister the notifier for.
+ * @handler: Callback executed for hotplug events related to @handle.
+ *
+ * Find the notifier object associated with @handle and remove the hotplug
+ * callback and the pointer to the hotplug context data from it. If the
+ * notifier object is not necessary any more, remove it altogether.
+ */
+acpi_status pci_acpi_remove_hp_notifier(acpi_handle handle,
+ acpi_notify_handler handler)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_status status = AE_NOT_FOUND;
+
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev)))
+ return AE_NOT_FOUND;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ status = AE_OK;
+ break;
+ }
+
+ if (status != AE_OK)
+ goto out;
+
+ nb->hp_data = NULL;
+ nb->hp_cb = NULL;
+
+ if (nb->pci_bus || nb->pci_dev)
+ goto out;
+
+ status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn);
+ list_del(&nb->entry);
+ kfree(nb);
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+EXPORT_SYMBOL_GPL(pci_acpi_remove_hp_notifier);
+
+/**
+ * pci_acpi_add_pm_notifier - Register PM notifier for given device.
+ * @dev: ACPI device to add the notifier for.
+ * @pci_dev: PCI device to check for the PME status if an event is signaled.
+ * @pci_bus: PCI bus to walk (checking PME status) if an event is signaled.
+ *
+ * Check if there is a notifier object for @dev and if that is the case, add
+ * @pci_dev to it as the device whose PME status should be checked if a PM
+ * event is signaled for @dev. Also, add @pci_bus to it as the bus to walk
+ * checking the PME status of all devices on it if a PM event is signaled for
+ * @dev. Otherwise, create a new notifier object for @dev and add both
+ * @pci_dev and @pci_bus to it.
+ */
+acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev,
+ struct pci_bus *pci_bus)
+{
+ struct pci_acpi_notifier_block *nb;
+ acpi_status status = AE_OK;
+
+ if (!dev->wakeup.flags.run_wake)
+ return AE_BAD_PARAMETER;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ if (nb->pci_dev || nb->pci_bus)
+ goto out;
+ else
+ goto add;
+ }
+
+ nb = new_notifier(dev);
+ if (!nb) {
+ status = AE_NO_MEMORY;
+ goto out;
+ }
+ list_add_tail(&nb->entry, &pci_acpi_runtime_notifiers);
+
+ status = acpi_install_notify_handler(dev->handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn, nb);
+ if (ACPI_FAILURE(status)) {
+ list_del(&nb->entry);
+ kfree(nb);
+ }
+
+ add:
+ nb->pci_dev = pci_dev;
+ nb->pci_bus = pci_bus;
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+
+/**
+ * pci_acpi_remove_pm_notifier - Unregister PM notifier for given device.
+ * @dev: ACPI device to remove the notifier from.
+ *
+ * Find the notifier object for @dev and clear its @pci_dev and @pci_bus fields.
+ * If the notifier object is not necessary any more after that, remove it too.
+ */
+acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev)
+{
+ struct pci_acpi_notifier_block *nb;
+ acpi_status status = AE_NOT_FOUND;
+
+ if (!dev->wakeup.flags.run_wake)
+ return AE_BAD_PARAMETER;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ status = AE_OK;
+ break;
+ }
+
+ if (status != AE_OK)
+ goto out;
+
+ if (dev->wakeup.run_wake_count) {
+ dev->wakeup.run_wake_count = 0;
+ acpi_unref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ acpi_pm_wake_up_power(dev, false);
+ }
+
+ nb->pci_dev = NULL;
+ nb->pci_bus = NULL;
+
+ if (nb->hp_cb)
+ goto out;
+
+ status = acpi_remove_notify_handler(nb->dev->handle,
+ ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn);
+ list_del(&nb->entry);
+ kfree(nb);
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+
+/**
+ * run_wake_enable - Enable/disable the GPE associated with given notifier.
+ * @nb: Notifier to enable/disable the GPE for.
+ * @enable: Whether to enable or disable the wake-up feature.
+ */
+static int run_wake_enable(struct pci_acpi_notifier_block *nb, bool enable)
+{
+ struct acpi_device *dev = nb->dev;
+ int error = 0;
+
+ if (enable) {
+ if (!dev->wakeup.run_wake_count++) {
+ acpi_pm_wake_up_power(dev, true);
+ acpi_ref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ }
+ } else if (dev->wakeup.run_wake_count > 0) {
+ if (!--dev->wakeup.run_wake_count) {
+ acpi_unref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ acpi_pm_wake_up_power(dev, false);
+ }
+ } else {
+ error = -EALREADY;
+ }
+
+ return error;
+}
+
+/**
+ * acpi_dev_run_wake_enable - Enable/disable wake-up for given device.
+ * @phys_dev: Device to enable/disable the platform to wake-up the system for.
+ * @enable: Whether enable or disable the wake-up functionality.
+ *
+ * Find the notifier object corresponding to @pci_dev and try to enable/disable
+ * the GPE associated with it.
+ */
+static int acpi_dev_run_wake_enable(struct device *phys_dev, bool enable)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_handle handle;
+ int error = -ENODEV;
+
+ if (!device_run_wake(phys_dev))
+ return -EINVAL;
+
+ handle = DEVICE_ACPI_HANDLE(phys_dev);
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
+ dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
+ __func__);
+ return -ENODEV;
+ }
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ error = run_wake_enable(nb, enable);
+ break;
+ }
+
+ mutex_unlock(&pci_acpi_notifier_mtx);
+
+ return error;
+}
+
+/*
* _SxD returns the D-state with the highest power
* (lowest D-state number) supported in the S-state "x".
*
@@ -131,12 +497,45 @@ static int acpi_pci_sleep_wake(struct pc
return 0;
}

+static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
+{
+ while (bus->parent) {
+ struct pci_dev *bridge = bus->self;
+
+ if (bridge->pme_interrupt)
+ return;
+ if (!acpi_dev_run_wake_enable(&bridge->dev, enable))
+ return;
+ bus = bus->parent;
+ }
+
+ /* We have reached the root bus. */
+ if (bus->bridge)
+ acpi_dev_run_wake_enable(bus->bridge, enable);
+}
+
+static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
+{
+ if (!device_run_wake(&dev->dev))
+ return -EINVAL;
+
+ if (dev->pme_interrupt)
+ return 0;
+
+ if (!acpi_dev_run_wake_enable(&dev->dev, enable))
+ return 0;
+
+ acpi_pci_propagate_run_wake(dev->bus, enable);
+ return 0;
+}
+
static struct pci_platform_pm_ops acpi_pci_platform_pm = {
.is_manageable = acpi_pci_power_manageable,
.set_state = acpi_pci_set_power_state,
.choose_state = acpi_pci_choose_state,
.can_wakeup = acpi_pci_can_wakeup,
.sleep_wake = acpi_pci_sleep_wake,
+ .run_wake = acpi_pci_run_wake,
};

/* ACPI bus type */
Index: linux-2.6/drivers/pci/pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci.c
+++ 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;
}

+static inline int platform_pci_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#.
@@ -1406,6 +1438,24 @@ int pci_back_from_sleep(struct pci_dev *
}

/**
+ * pci_dev_run_wake - Check if device can generate run-time wake-up events.
+ * @dev: Device to check.
+ *
+ * Return true if the device itself is cabable of generating wake-up events
+ * through the platform or native PCIe PME or if the device supports PME and
+ * its upstream bridge can generate wake-up events.
+ */
+bool pci_dev_run_wake(struct pci_dev *dev)
+{
+ if (device_run_wake(&dev->dev))
+ return true;
+
+ return dev->pme_support
+ && dev->bus->bridge && device_run_wake(dev->bus->bridge);
+}
+EXPORT_SYMBOL_GPL(pci_dev_run_wake);
+
+/**
* pci_pm_init - Initialize PM functions of given PCI device
* @dev: PCI device to handle.
*/
Index: linux-2.6/kernel/power/Kconfig
===================================================================
--- linux-2.6.orig/kernel/power/Kconfig
+++ 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
Index: linux-2.6/drivers/acpi/sleep.c
===================================================================
--- linux-2.6.orig/drivers/acpi/sleep.c
+++ linux-2.6/drivers/acpi/sleep.c
@@ -626,7 +626,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
@@ -712,6 +712,18 @@ int acpi_pm_device_sleep_state(struct de
}

/**
+ * acpi_pm_wake_up_power - Enable/disable device wake-up power.
+ * @dev: ACPI device to handle.
+ * @enable: Whether to enable or disable the wake-up power of the device.
+ */
+int acpi_pm_wake_up_power(struct acpi_device *dev, bool enable)
+{
+ return enable ?
+ acpi_enable_wakeup_device_power(dev, acpi_target_sleep_state) :
+ acpi_disable_wakeup_device_power(dev);
+}
+
+/**
* acpi_pm_device_sleep_wake - enable or disable the system wake-up
* capability of given device
* @dev: device to handle
@@ -732,16 +744,14 @@ int acpi_pm_device_sleep_wake(struct dev
return -ENODEV;
}

- error = enable ?
- acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
- acpi_disable_wakeup_device_power(adev);
+ error = acpi_pm_wake_up_power(adev, enable);
if (!error)
- dev_info(dev, "wake-up capability %s by ACPI\n",
+ dev_info(dev, "wake-up power %s by ACPI\n",
enable ? "enabled" : "disabled");

return error;
}
-#endif
+#endif /* CONFIG_PM_WAKEUP */

static void acpi_power_off_prepare(void)
{
Index: linux-2.6/drivers/acpi/pci_root.c
===================================================================
--- linux-2.6.orig/drivers/acpi/pci_root.c
+++ linux-2.6/drivers/acpi/pci_root.c
@@ -30,6 +30,7 @@
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/pm.h>
+#include <linux/pm_runtime.h>
#include <linux/pci.h>
#include <linux/pci-acpi.h>
#include <linux/acpi.h>
@@ -576,6 +577,9 @@ static int __devinit acpi_pci_root_add(s
if (flags != base_flags)
acpi_pci_osc_support(root, flags);

+ if (!pci_acpi_add_bus_pm_notifier(device, root->bus))
+ device_set_run_wake(root->bus->bridge, true);
+
return 0;

end:
@@ -597,6 +601,9 @@ static int acpi_pci_root_remove(struct a
{
struct acpi_pci_root *root = acpi_driver_data(device);

+ pci_acpi_remove_pm_notifier(device);
+ device_set_run_wake(root->bus->bridge, false);
+
kfree(root);
return 0;
}
Index: linux-2.6/include/linux/pci-acpi.h
===================================================================
--- linux-2.6.orig/include/linux/pci-acpi.h
+++ linux-2.6/include/linux/pci-acpi.h
@@ -11,6 +11,30 @@
#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_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev,
+ struct pci_bus *pci_bus);
+extern acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev);
+
+static inline
+acpi_status pci_acpi_add_device_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev)
+{
+ return pci_acpi_add_pm_notifier(dev, pci_dev, pci_dev->subordinate);
+}
+
+static inline
+acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev,
+ struct pci_bus *pci_bus)
+{
+ return pci_acpi_add_pm_notifier(dev, NULL, pci_bus);
+}
+
static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
{
struct pci_bus *pbus = pdev->bus;
Index: linux-2.6/drivers/acpi/pci_bind.c
===================================================================
--- linux-2.6.orig/drivers/acpi/pci_bind.c
+++ linux-2.6/drivers/acpi/pci_bind.c
@@ -26,7 +26,9 @@
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
+#include <linux/pci-acpi.h>
#include <linux/acpi.h>
+#include <linux/pm_runtime.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

@@ -38,7 +40,13 @@ 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;
+
+ pci_acpi_remove_pm_notifier(device);
+ device_set_run_wake(&dev->dev, false);
+
+ if (!dev->subordinate)
goto out;

acpi_pci_irq_del_prt(dev->subordinate);
@@ -62,6 +70,9 @@ static int acpi_pci_bind(struct acpi_dev
if (!dev)
return 0;

+ if (!pci_acpi_add_device_pm_notifier(device, dev))
+ device_set_run_wake(&dev->dev, true);
+
/*
* Install the 'bind' function to facilitate callbacks for
* children of the P2P bridge.
Index: linux-2.6/drivers/pci/hotplug/acpiphp_glue.c
===================================================================
--- linux-2.6.orig/drivers/pci/hotplug/acpiphp_glue.c
+++ 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 ;
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -743,6 +743,7 @@ int pci_wake_from_d3(struct pci_dev *dev
pci_power_t pci_target_state(struct pci_dev *dev);
int pci_prepare_to_sleep(struct pci_dev *dev);
int pci_back_from_sleep(struct pci_dev *dev);
+bool pci_dev_run_wake(struct pci_dev *dev);

/* Functions for PCI Hotplug drivers to use */
int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap);
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
--- linux-2.6.orig/include/acpi/acpi_bus.h
+++ linux-2.6/include/acpi/acpi_bus.h
@@ -388,21 +388,26 @@ 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_wake_up_power(struct acpi_device *, bool);
int acpi_pm_device_sleep_wake(struct device *, bool);
-#else /* !CONFIG_PM_SLEEP */
+#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_wake_up_power(struct acpi_device *dev, bool enable)
+{
+ return -ENODEV;
+}
static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
{
return -ENODEV;
}
-#endif /* !CONFIG_PM_SLEEP */
+#endif /* !CONFIG_PM_WAKEUP */

#endif /* CONFIG_ACPI */

2009-11-15 23:59:51

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 10/10] PCI PM: Run-time callbacks for PCI bus type

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.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/pci/pci-driver.c | 94 ++++++++++++++++++++++++++++++++++++++++++++---
drivers/pci/pci.c | 43 ++++++++++++++++++---
drivers/pci/pci.h | 1
include/linux/pci.h | 9 ++++
kernel/power/Kconfig | 5 ++
5 files changed, 140 insertions(+), 12 deletions(-)

Index: linux-2.6/kernel/power/Kconfig
===================================================================
--- linux-2.6.orig/kernel/power/Kconfig
+++ 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
Index: linux-2.6/drivers/pci/pci-driver.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci-driver.c
+++ linux-2.6/drivers/pci/pci-driver.c
@@ -17,6 +17,7 @@
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/cpu.h>
+#include <linux/pm_runtime.h>
#include "pci.h"

struct pci_dynid {
@@ -537,7 +538,7 @@ static int pci_restore_standard_config(s
return pci_restore_state(pci_dev);
}

-static void pci_pm_default_resume_noirq(struct pci_dev *pci_dev)
+static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
{
pci_restore_standard_config(pci_dev);
pci_fixup_device(pci_fixup_resume_early, pci_dev);
@@ -681,7 +682,7 @@ static int pci_pm_resume_noirq(struct de
struct device_driver *drv = dev->driver;
int error = 0;

- pci_pm_default_resume_noirq(pci_dev);
+ pci_pm_default_resume_early(pci_dev);

if (pci_has_legacy_pm_support(pci_dev))
return pci_legacy_resume_early(dev);
@@ -879,7 +880,7 @@ static int pci_pm_restore_noirq(struct d
struct device_driver *drv = dev->driver;
int error = 0;

- pci_pm_default_resume_noirq(pci_dev);
+ pci_pm_default_resume_early(pci_dev);

if (pci_has_legacy_pm_support(pci_dev))
return pci_legacy_resume_early(dev);
@@ -931,6 +932,86 @@ static int pci_pm_restore(struct device

#endif /* !CONFIG_HIBERNATION */

+#endif /* !CONFIG_PM_SLEEP */
+
+#ifdef CONFIG_PM_RUNTIME
+
+static int pci_pm_runtime_suspend(struct device *dev)
+{
+ struct pci_dev *pci_dev = to_pci_dev(dev);
+ const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
+ pci_power_t prev = pci_dev->current_state;
+ int error;
+
+ if (!pm || !pm->runtime_suspend)
+ return -ENOSYS;
+
+ error = pm->runtime_suspend(dev);
+ suspend_report_result(pm->runtime_suspend, error);
+ if (error)
+ return error;
+
+ pci_fixup_device(pci_fixup_suspend, pci_dev);
+
+ if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
+ && pci_dev->current_state != PCI_UNKNOWN) {
+ WARN_ONCE(pci_dev->current_state != prev,
+ "PCI PM: State of device not saved by %pF\n",
+ pm->runtime_suspend);
+ return 0;
+ }
+
+ if (!pci_dev->state_saved)
+ pci_save_state(pci_dev);
+
+ pci_finish_runtime_suspend(pci_dev);
+
+ return 0;
+}
+
+static int pci_pm_runtime_resume(struct device *dev)
+{
+ struct pci_dev *pci_dev = to_pci_dev(dev);
+ const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
+
+ if (!pm || !pm->runtime_resume)
+ return -ENOSYS;
+
+ __pci_enable_wake(pci_dev, PCI_D0, true, false);
+ pci_pm_default_resume_early(pci_dev);
+ pci_fixup_device(pci_fixup_resume, pci_dev);
+
+ return pm->runtime_resume(dev);
+}
+
+static int pci_pm_runtime_idle(struct device *dev)
+{
+ const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
+
+ if (!pm)
+ return -ENOSYS;
+
+ if (pm->runtime_idle) {
+ int ret = pm->runtime_idle(dev);
+ if (ret)
+ return ret;
+ }
+
+ pm_runtime_suspend(dev);
+
+ return 0;
+}
+
+#else /* !CONFIG_PM_RUNTIME */
+
+#define pci_pm_runtime_suspend NULL
+#define pci_pm_runtime_resume NULL
+#define pci_pm_runtime_idle NULL
+
+#endif /* !CONFIG_PM_RUNTIME */
+
+#ifdef CONFIG_PM_OPS
+
const struct dev_pm_ops pci_dev_pm_ops = {
.prepare = pci_pm_prepare,
.complete = pci_pm_complete,
@@ -946,15 +1027,18 @@ const struct dev_pm_ops pci_dev_pm_ops =
.thaw_noirq = pci_pm_thaw_noirq,
.poweroff_noirq = pci_pm_poweroff_noirq,
.restore_noirq = pci_pm_restore_noirq,
+ .runtime_suspend = pci_pm_runtime_suspend,
+ .runtime_resume = pci_pm_runtime_resume,
+ .runtime_idle = pci_pm_runtime_idle,
};

#define PCI_PM_OPS_PTR (&pci_dev_pm_ops)

-#else /* !CONFIG_PM_SLEEP */
+#else /* !COMFIG_PM_OPS */

#define PCI_PM_OPS_PTR NULL

-#endif /* !CONFIG_PM_SLEEP */
+#endif /* !COMFIG_PM_OPS */

/**
* __pci_register_driver - register a new pci driver
Index: linux-2.6/drivers/pci/pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci.c
+++ linux-2.6/drivers/pci/pci.c
@@ -1274,9 +1274,10 @@ void pci_pme_active(struct pci_dev *dev,
}

/**
- * pci_enable_wake - enable PCI device as wakeup event source
+ * __pci_enable_wake - enable PCI device as wakeup event source
* @dev: PCI device affected
* @state: PCI state from which device will issue wakeup events
+ * @runtime: True if the events are to be generated at run time
* @enable: True to enable event generation; false to disable
*
* This enables the device as a wakeup event source, or disables it.
@@ -1292,11 +1293,12 @@ void pci_pme_active(struct pci_dev *dev,
* Error code depending on the platform is returned if both the platform and
* the native mechanism fail to enable the generation of wake-up events
*/
-int pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
+int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
+ bool runtime, bool enable)
{
int ret = 0;

- if (enable && !device_may_wakeup(&dev->dev))
+ if (enable && runtime && !device_may_wakeup(&dev->dev))
return -EINVAL;

/* Don't do the same thing twice in a row for one device. */
@@ -1316,19 +1318,24 @@ int pci_enable_wake(struct pci_dev *dev,
pci_pme_active(dev, true);
else
ret = 1;
- error = platform_pci_sleep_wake(dev, true);
+ error = runtime ? platform_pci_run_wake(dev, true) :
+ platform_pci_sleep_wake(dev, true);
if (ret)
ret = error;
if (!ret)
dev->wakeup_prepared = true;
} else {
- platform_pci_sleep_wake(dev, false);
+ if (runtime)
+ platform_pci_run_wake(dev, false);
+ else
+ platform_pci_sleep_wake(dev, false);
pci_pme_active(dev, false);
dev->wakeup_prepared = false;
}

return ret;
}
+EXPORT_SYMBOL(__pci_enable_wake);

/**
* pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
@@ -1438,6 +1445,31 @@ int pci_back_from_sleep(struct pci_dev *
}

/**
+ * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
+ * @dev: PCI device being suspended.
+ *
+ * Prepare @dev to generate wake-up events at run time and put it into a low
+ * power state.
+ */
+int pci_finish_runtime_suspend(struct pci_dev *dev)
+{
+ pci_power_t target_state = pci_target_state(dev);
+ int error;
+
+ if (target_state == PCI_POWER_ERROR)
+ return -EIO;
+
+ __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
+
+ error = pci_set_power_state(dev, target_state);
+
+ if (error)
+ __pci_enable_wake(dev, target_state, true, false);
+
+ return error;
+}
+
+/**
* pci_dev_run_wake - Check if device can generate run-time wake-up events.
* @dev: Device to check.
*
@@ -2881,7 +2913,6 @@ EXPORT_SYMBOL(pci_save_state);
EXPORT_SYMBOL(pci_restore_state);
EXPORT_SYMBOL(pci_pme_capable);
EXPORT_SYMBOL(pci_pme_active);
-EXPORT_SYMBOL(pci_enable_wake);
EXPORT_SYMBOL(pci_wake_from_d3);
EXPORT_SYMBOL(pci_target_state);
EXPORT_SYMBOL(pci_prepare_to_sleep);
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -738,13 +738,20 @@ int pci_set_power_state(struct pci_dev *
pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state);
bool pci_pme_capable(struct pci_dev *dev, pci_power_t state);
void pci_pme_active(struct pci_dev *dev, bool enable);
-int pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable);
+int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
+ bool runtime, bool enable);
int pci_wake_from_d3(struct pci_dev *dev, bool enable);
pci_power_t pci_target_state(struct pci_dev *dev);
int pci_prepare_to_sleep(struct pci_dev *dev);
int pci_back_from_sleep(struct pci_dev *dev);
bool pci_dev_run_wake(struct pci_dev *dev);

+static inline int pci_enable_wake(struct pci_dev *dev, pci_power_t state,
+ bool enable)
+{
+ return __pci_enable_wake(dev, state, false, enable);
+}
+
/* Functions for PCI Hotplug drivers to use */
int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap);
#ifdef CONFIG_HOTPLUG
Index: linux-2.6/drivers/pci/pci.h
===================================================================
--- linux-2.6.orig/drivers/pci/pci.h
+++ linux-2.6/drivers/pci/pci.h
@@ -55,6 +55,7 @@ extern int pci_set_platform_pm(struct pc
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_finish_runtime_suspend(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);

2009-11-16 04:37:21

by Jin Dongming

[permalink] [raw]
Subject: Re: [RFC][PATCH 9/10] PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 3)

Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <[email protected]>
> +/**
> + * pci_acpi_add_pm_notifier - Register PM notifier for given device.
> + * @dev: ACPI device to add the notifier for.
> + * @pci_dev: PCI device to check for the PME status if an event is signaled.
> + * @pci_bus: PCI bus to walk (checking PME status) if an event is signaled.
> + *
> + * Check if there is a notifier object for @dev and if that is the case, add
> + * @pci_dev to it as the device whose PME status should be checked if a PM
> + * event is signaled for @dev. Also, add @pci_bus to it as the bus to walk
> + * checking the PME status of all devices on it if a PM event is signaled for
> + * @dev. Otherwise, create a new notifier object for @dev and add both
> + * @pci_dev and @pci_bus to it.
> + */
> +acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
> + struct pci_dev *pci_dev,
> + struct pci_bus *pci_bus)
> +{
> + struct pci_acpi_notifier_block *nb;
> + acpi_status status = AE_OK;
> +
> + if (!dev->wakeup.flags.run_wake)
> + return AE_BAD_PARAMETER;
> +
> + mutex_lock(&pci_acpi_notifier_mtx);
> +
> + list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
> + if (nb->dev == dev) {
> + if (nb->pci_dev || nb->pci_bus)
> + goto out;
> + else
> + goto add;
> + }
> +
> + nb = new_notifier(dev);
> + if (!nb) {
> + status = AE_NO_MEMORY;
> + goto out;
> + }
> + list_add_tail(&nb->entry, &pci_acpi_runtime_notifiers);
> +
> + status = acpi_install_notify_handler(dev->handle, ACPI_SYSTEM_NOTIFY,
> + pci_acpi_event_fn, nb);
> + if (ACPI_FAILURE(status)) {
> + list_del(&nb->entry);
> + kfree(nb);
> + }

I think when nb is freed, it should be go to "out:".

> +
> + add:
> + nb->pci_dev = pci_dev;
> + nb->pci_bus = pci_bus;
> +
> + out:
> + mutex_unlock(&pci_acpi_notifier_mtx);
> + return status;
> +}
> +
> +/**
> + * pci_acpi_remove_pm_notifier - Unregister PM notifier for given device.
> + * @dev: ACPI device to remove the notifier from.
> + *
> + * Find the notifier object for @dev and clear its @pci_dev and @pci_bus fields.
> + * If the notifier object is not necessary any more after that, remove it too.
> + */

2009-11-16 19:03:29

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 9/10] PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 3)

On Monday 16 November 2009, Jin Dongming wrote:
> Rafael J. Wysocki wrote:
> > From: Rafael J. Wysocki <[email protected]>
> > +/**
> > + * pci_acpi_add_pm_notifier - Register PM notifier for given device.
> > + * @dev: ACPI device to add the notifier for.
> > + * @pci_dev: PCI device to check for the PME status if an event is signaled.
> > + * @pci_bus: PCI bus to walk (checking PME status) if an event is signaled.
> > + *
> > + * Check if there is a notifier object for @dev and if that is the case, add
> > + * @pci_dev to it as the device whose PME status should be checked if a PM
> > + * event is signaled for @dev. Also, add @pci_bus to it as the bus to walk
> > + * checking the PME status of all devices on it if a PM event is signaled for
> > + * @dev. Otherwise, create a new notifier object for @dev and add both
> > + * @pci_dev and @pci_bus to it.
> > + */
> > +acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
> > + struct pci_dev *pci_dev,
> > + struct pci_bus *pci_bus)
> > +{
> > + struct pci_acpi_notifier_block *nb;
> > + acpi_status status = AE_OK;
> > +
> > + if (!dev->wakeup.flags.run_wake)
> > + return AE_BAD_PARAMETER;
> > +
> > + mutex_lock(&pci_acpi_notifier_mtx);
> > +
> > + list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
> > + if (nb->dev == dev) {
> > + if (nb->pci_dev || nb->pci_bus)
> > + goto out;
> > + else
> > + goto add;
> > + }
> > +
> > + nb = new_notifier(dev);
> > + if (!nb) {
> > + status = AE_NO_MEMORY;
> > + goto out;
> > + }
> > + list_add_tail(&nb->entry, &pci_acpi_runtime_notifiers);
> > +
> > + status = acpi_install_notify_handler(dev->handle, ACPI_SYSTEM_NOTIFY,
> > + pci_acpi_event_fn, nb);
> > + if (ACPI_FAILURE(status)) {
> > + list_del(&nb->entry);
> > + kfree(nb);
> > + }
>
> I think when nb is freed, it should be go to "out:".

You're absolutely right, thanks for catching this bug.

I'll fix it in the next version of the patch.

> > +
> > + add:
> > + nb->pci_dev = pci_dev;
> > + nb->pci_bus = pci_bus;
> > +
> > + out:
> > + mutex_unlock(&pci_acpi_notifier_mtx);
> > + return status;
> > +}
> > +
> > +/**
> > + * pci_acpi_remove_pm_notifier - Unregister PM notifier for given device.
> > + * @dev: ACPI device to remove the notifier from.
> > + *
> > + * Find the notifier object for @dev and clear its @pci_dev and @pci_bus fields.
> > + * If the notifier object is not necessary any more after that, remove it too.
> > + */

Best,
Rafael

2009-11-16 19:38:59

by Alan Stern

[permalink] [raw]
Subject: Re: [RFC][PATCH 1/10] PM: Add flag for devices capable of generating run-time wake-up events

On Mon, 16 Nov 2009, Rafael J. Wysocki wrote:

> From: Rafael J. Wysocki <[email protected]>
>
> Apparently, there are devices that can wake up the system from sleep
> states and yet are incapable of generating wake-up events at run
> time. Thus, introduce a flag indicating if given device is capable
> of generating run-time wake-up events.

This raises the question: Who is responsible for setting the new
flag? The code that registers the device?

What if the kernel can't tell whether or not the device can generate
runtime wake-up events?

What if the user wants to override the kernel's setting? Should there
be a sysfs attribute controlling the flag?

Alan Stern

2009-11-16 20:51:34

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 1/10] PM: Add flag for devices capable of generating run-time wake-up events

On Monday 16 November 2009, Alan Stern wrote:
> On Mon, 16 Nov 2009, Rafael J. Wysocki wrote:
>
> > From: Rafael J. Wysocki <[email protected]>
> >
> > Apparently, there are devices that can wake up the system from sleep
> > states and yet are incapable of generating wake-up events at run
> > time. Thus, introduce a flag indicating if given device is capable
> > of generating run-time wake-up events.
>
> This raises the question: Who is responsible for setting the new
> flag? The code that registers the device?

Yes, in general. The platform.

Actually, I needed it for PCI, but I thought it would be better to put it at
the core level.

> What if the kernel can't tell whether or not the device can generate
> runtime wake-up events?

Do you have any specific examples in mind?

> What if the user wants to override the kernel's setting? Should there
> be a sysfs attribute controlling the flag?

I have no plans for adding anything like that.

Rafael

2009-11-16 21:00:45

by Alan Stern

[permalink] [raw]
Subject: Re: [RFC][PATCH 1/10] PM: Add flag for devices capable of generating run-time wake-up events

On Mon, 16 Nov 2009, Rafael J. Wysocki wrote:

> On Monday 16 November 2009, Alan Stern wrote:
> > On Mon, 16 Nov 2009, Rafael J. Wysocki wrote:
> >
> > > From: Rafael J. Wysocki <[email protected]>
> > >
> > > Apparently, there are devices that can wake up the system from sleep
> > > states and yet are incapable of generating wake-up events at run
> > > time. Thus, introduce a flag indicating if given device is capable
> > > of generating run-time wake-up events.
> >
> > This raises the question: Who is responsible for setting the new
> > flag? The code that registers the device?
>
> Yes, in general. The platform.

And for non-platform devices (hot-pluggable, for example)? Presumably
you would want the driver that detects and registers the device to set
this flag.

> Actually, I needed it for PCI, but I thought it would be better to put it at
> the core level.
>
> > What if the kernel can't tell whether or not the device can generate
> > runtime wake-up events?
>
> Do you have any specific examples in mind?

What about Matthew's example of an ACPI GPE which might or might not
cause a runtime wake-up event, depending on the AML code in the BIOS?

> > What if the user wants to override the kernel's setting? Should there
> > be a sysfs attribute controlling the flag?
>
> I have no plans for adding anything like that.

So if the kernel makes a mistake here, the user won't be able to
correct it.

Alan Stern

2009-11-16 21:06:05

by Matthew Garrett

[permalink] [raw]
Subject: Re: [RFC][PATCH 1/10] PM: Add flag for devices capable of generating run-time wake-up events

On Mon, Nov 16, 2009 at 04:00:49PM -0500, Alan Stern wrote:
> On Mon, 16 Nov 2009, Rafael J. Wysocki wrote:
> > Yes, in general. The platform.
>
> And for non-platform devices (hot-pluggable, for example)? Presumably
> you would want the driver that detects and registers the device to set
> this flag.

Are there any cases where the bus code won't know this?

> > > What if the kernel can't tell whether or not the device can generate
> > > runtime wake-up events?
> >
> > Do you have any specific examples in mind?
>
> What about Matthew's example of an ACPI GPE which might or might not
> cause a runtime wake-up event, depending on the AML code in the BIOS?

The platform knows whether or not that's the case.

--
Matthew Garrett | [email protected]

2009-11-16 21:23:18

by Oliver Neukum

[permalink] [raw]
Subject: Re: [RFC][PATCH 1/10] PM: Add flag for devices capable of generating run-time wake-up events

Am Montag, 16. November 2009 22:00:49 schrieb Alan Stern:
> > > What if the user wants to override the kernel's setting? Should there
> > > be a sysfs attribute controlling the flag?
> >
> >
> > I have no plans for adding anything like that.
>
> So if the kernel makes a mistake here, the user won't be able to
> correct it.

If the kernel cannot tell whether a device can generate a request for
remote wakeup, how would it know the enable such requests?
Now we might come to a device that has a flatly wrong firmware,
but has that really been encountered in the wild?

Regards
Oliver

2009-11-16 21:31:19

by Alan Stern

[permalink] [raw]
Subject: Re: [RFC][PATCH 1/10] PM: Add flag for devices capable of generating run-time wake-up events

On Mon, 16 Nov 2009, Matthew Garrett wrote:

> On Mon, Nov 16, 2009 at 04:00:49PM -0500, Alan Stern wrote:
> > On Mon, 16 Nov 2009, Rafael J. Wysocki wrote:
> > > Yes, in general. The platform.
> >
> > And for non-platform devices (hot-pluggable, for example)? Presumably
> > you would want the driver that detects and registers the device to set
> > this flag.
>
> Are there any cases where the bus code won't know this?

None that I'm aware of. I merely wanted to clarify where the flag was
supposed to get set.

> > > > What if the kernel can't tell whether or not the device can generate
> > > > runtime wake-up events?
> > >
> > > Do you have any specific examples in mind?
> >
> > What about Matthew's example of an ACPI GPE which might or might not
> > cause a runtime wake-up event, depending on the AML code in the BIOS?
>
> The platform knows whether or not that's the case.

Okay, then it isn't a problem. Except perhaps in the case of buggy
devices which _should_ generate these events but don't.

Alan Stern

2009-11-29 15:45:59

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 0/12] PCI run-time PM support (updated)

Hi,

The following (updated) series of patches provides preliminary run-time power
management support for PCI devices through ACPI and/or the native PCIe PME.

Some patches have been modified since the previous iteration and two new
patches have been added. Also, I've tested this patchset with the native PCIe
PME mechanism using the r8169 driver on the MSI Wind U-100 (see the last patch
for details).

[1/12] - Add flag for marking devices capable of generating wake-up events at run time

[2/12] - Add function for checking PME status of devices

[3/12] - Modify wake-up enable propagation so that it's done for PCIe devices too

[4/12] - PCIe PME root port service driver

[5/12][New] - "Don't use MSIs for PME signaling" switch for PCIe

[6/12] - ACPI GPE refcounting, from Matthew

[7/12] - ACPI drivers support for GPE refcounting, from Matthew

[8/12] - ACPI removal of the old GPE API, from Matthew

[9/12] - ACPI add fields for handling run-wake devices

[10/12] - PCI ACPI platform support for run-time power management

[11/12] - Runtime PM callbacks for the PCI bus type

[12/12] - Runtime PM support for r8169 (experimental)

If there are no objections, I think the patches [1-11/12] are ready for merging
at this point.

Thanks,
Rafael

2009-11-29 15:48:43

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 1/12] PM: Add flag for devices capable of generating run-time wake-up events

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

Apparently, there are devices that can wake up the system from sleep
states and yet are incapable of generating wake-up events at run
time. Thus, introduce a flag indicating if given device is capable
of generating run-time wake-up events.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
Documentation/power/runtime_pm.txt | 7 +++++--
include/linux/pm.h | 8 +++++---
include/linux/pm_runtime.h | 12 ++++++++++++
3 files changed, 22 insertions(+), 5 deletions(-)

Index: linux-2.6/include/linux/pm.h
===================================================================
--- linux-2.6.orig/include/linux/pm.h
+++ linux-2.6/include/linux/pm.h
@@ -179,9 +179,10 @@ typedef struct pm_message {
* This need not mean that the device should be put into a low power state.
* For example, if the device is behind a link which is about to be turned
* off, the device may remain at full power. If the device does go to low
- * power and if device_may_wakeup(dev) is true, remote wake-up (i.e., a
- * hardware mechanism allowing the device to request a change of its power
- * state, such as PCI PME) should be enabled for it.
+ * power and is capable of generating run-time wake-up events, remote
+ * wake-up (i.e., a hardware mechanism allowing the device to request a
+ * change of its power state via a wake-up event, such as PCI PME) should
+ * be enabled for it.
*
* @runtime_resume: Put the device into the fully active state in response to a
* wake-up event generated by hardware or at the request of software. If
@@ -434,6 +435,7 @@ struct dev_pm_info {
unsigned int idle_notification:1;
unsigned int request_pending:1;
unsigned int deferred_resume:1;
+ unsigned int run_wake:1;
enum rpm_request request;
enum rpm_status runtime_status;
int runtime_error;
Index: linux-2.6/Documentation/power/runtime_pm.txt
===================================================================
--- linux-2.6.orig/Documentation/power/runtime_pm.txt
+++ linux-2.6/Documentation/power/runtime_pm.txt
@@ -71,9 +71,9 @@ what to do to handle the device).
purpose).

In particular, if the driver requires remote wakeup capability for proper
-functioning and device_may_wakeup() returns 'false' for the device, then
+functioning and device_run_wake() returns 'false' for the device, then
->runtime_suspend() should return -EBUSY. On the other hand, if
-device_may_wakeup() returns 'true' for the device and the device is put
+device_run_wake() returns 'true' for the device and the device is put
into a low power state during the execution of its bus type's
->runtime_suspend(), it is expected that remote wake-up (i.e. hardware mechanism
allowing the device to request a change of its power state, such as PCI PME)
@@ -214,6 +214,9 @@ defined in include/linux/pm.h:
being executed for that device and it is not practical to wait for the
suspend to complete; means "start a resume as soon as you've suspended"

+ unsigned int run_wake;
+ - set if the device is capable of generating run-time wake-up events
+
enum rpm_status runtime_status;
- the run-time PM status of the device; this field's initial value is
RPM_SUSPENDED, which means that each device is initially regarded by the
Index: linux-2.6/include/linux/pm_runtime.h
===================================================================
--- linux-2.6.orig/include/linux/pm_runtime.h
+++ linux-2.6/include/linux/pm_runtime.h
@@ -50,6 +50,16 @@ static inline void pm_runtime_put_noidle
atomic_add_unless(&dev->power.usage_count, -1, 0);
}

+static inline bool device_run_wake(struct device *dev)
+{
+ return dev->power.run_wake;
+}
+
+static inline void device_set_run_wake(struct device *dev, bool enable)
+{
+ dev->power.run_wake = enable;
+}
+
#else /* !CONFIG_PM_RUNTIME */

static inline int pm_runtime_idle(struct device *dev) { return -ENOSYS; }
@@ -73,6 +83,8 @@ static inline bool pm_children_suspended
static inline void pm_suspend_ignore_children(struct device *dev, bool en) {}
static inline void pm_runtime_get_noresume(struct device *dev) {}
static inline void pm_runtime_put_noidle(struct device *dev) {}
+static inline bool device_run_wake(struct device *dev) { return false; }
+static inline void device_set_run_wake(struct device *dev, bool enable) {}

#endif /* !CONFIG_PM_RUNTIME */

2009-11-29 15:45:53

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 2/12] 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]>

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/pci/pci.c | 35 +++++++++++++++++++++++++++++++++++
drivers/pci/pci.h | 1 +
2 files changed, 36 insertions(+)

Index: linux-2.6/drivers/pci/pci.h
===================================================================
--- linux-2.6.orig/drivers/pci/pci.h
+++ linux-2.6/drivers/pci/pci.h
@@ -49,6 +49,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);
Index: linux-2.6/drivers/pci/pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci.c
+++ linux-2.6/drivers/pci/pci.c
@@ -1167,6 +1167,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 and if set, clear it and clear PME enable
+ * (if set). Return 'true' if PME status and PME enable were both set 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;
+ pci_read_config_word(dev, pmcsr_pos, &pmcsr);
+ if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
+ return false;
+
+ /* Clear PME status. */
+ pmcsr |= PCI_PM_CTRL_PME_STATUS;
+ if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
+ /* Disable PME to avoid interrupt flood. */
+ 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#.

2009-11-29 15:46:08

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 3/12] PCI / PM: Propagate wake-up enable for PCIe devices too

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

Having read the PM part of the PCIe 2.0 specification more carefully
I think that it was a mistake to restrict the wake-up enable
propagation to non-PCIe devices, because if we do not request
control of the root ports' PME registers via OSC, PCIe PME is
supposed to be handled by the platform, just like the non-PCIe PME.
Even if we do that, the wake-up propagation is done to allow the
devices to wake up the system from sleep states which involves the
platform anyway, so it won't hurt.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/pci/pci-acpi.c | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)

Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci-acpi.c
+++ linux-2.6/drivers/pci/pci-acpi.c
@@ -112,11 +112,7 @@ static bool acpi_pci_can_wakeup(struct p
static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
{
while (bus->parent) {
- struct pci_dev *bridge = bus->self;
- int ret;
-
- ret = acpi_pm_device_sleep_wake(&bridge->dev, enable);
- if (!ret || bridge->is_pcie)
+ if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
return;
bus = bus->parent;
}
@@ -131,9 +127,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)
- acpi_pci_propagate_wakeup_enable(dev->bus, enable);
-
+ acpi_pci_propagate_wakeup_enable(dev->bus, enable);
return 0;
}

2009-11-29 15:46:09

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 4/12] PCI PM: PCIe PME root port service driver (rev. 5)

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]>.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
Documentation/kernel-parameters.txt | 6
drivers/pci/pcie/Kconfig | 4
drivers/pci/pcie/Makefile | 2
drivers/pci/pcie/pme/Makefile | 8
drivers/pci/pcie/pme/pcie_pme.c | 495 +++++++++++++++++++++++++++++++++++
drivers/pci/pcie/pme/pcie_pme.h | 28 +
drivers/pci/pcie/pme/pcie_pme_acpi.c | 54 +++
include/linux/pci.h | 1
8 files changed, 598 insertions(+)

Index: linux-2.6/drivers/pci/pcie/Kconfig
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/Kconfig
+++ 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
Index: linux-2.6/drivers/pci/pcie/Makefile
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/Makefile
+++ 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/
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
@@ -0,0 +1,495 @@
+/*
+ * PCIe Native PME support
+ *
+ * Copyright (C) 2007 - 2009 Intel Corp
+ * Copyright (C) 2007 - 2009 Shaohua Li <[email protected]>
+ * Copyright (C) 2009 Rafael J. Wysocki <[email protected]>, Novell Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License V2. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/device.h>
+#include <linux/pcieport_if.h>
+#include <linux/acpi.h>
+#include <linux/pci-acpi.h>
+#include <linux/pm_runtime.h>
+
+#include "../../pci.h"
+#include "pcie_pme.h"
+
+#define PCI_EXP_RTSTA_PME 0x10000 /* PME status */
+#define PCI_EXP_RTSTA_PENDING 0x20000 /* PME pending */
+
+/*
+ * If set, this switch will prevent the PCIe root port PME service driver from
+ * being registered. Consequently, the interrupt-based PCIe PME signaling will
+ * not be used by any PCIe root ports in that case.
+ */
+static bool pcie_pme_disabled;
+
+/*
+ * The PCI Express Base Specification 2.0, Section 6.1.8, states the following:
+ * "In order to maintain compatibility with non-PCI Express-aware system
+ * software, system power management logic must be configured by firmware to use
+ * the legacy mechanism of signaling PME by default. PCI Express-aware system
+ * software must notify the firmware prior to enabling native, interrupt-based
+ * PME signaling." However, if the platform doesn't provide us with a suitable
+ * notification mechanism or the notification fails, it is not clear whether or
+ * not we are supposed to use the interrupt-based PCIe PME signaling. The
+ * switch below can be used to indicate the desired behaviour. When set, it
+ * will make the kernel use the interrupt-based PCIe PME signaling regardless of
+ * the platform notification status, although the kernel will attempt to notify
+ * the platform anyway. When unset, it will prevent the kernel from using the
+ * the interrupt-based PCIe PME signaling if the platform notification fails,
+ * which is the default.
+ */
+static bool pcie_pme_force_enable;
+
+static int __init pcie_pme_setup(char *str)
+{
+ if (!strcmp(str, "off"))
+ pcie_pme_disabled = true;
+ else if (!strcmp(str, "force"))
+ pcie_pme_force_enable = true;
+ return 1;
+}
+__setup("pcie_pme=", pcie_pme_setup);
+
+/**
+ * pcie_pme_platform_setup - Ensure that the kernel controls the PCIe PME.
+ * @srv: PCIe PME root port service to use for carrying out the check.
+ *
+ * Notify the platform that the native PCIe PME is going to be used and return
+ * 'true' if the control of the PCIe PME registers has been acquired from the
+ * platform.
+ */
+static bool pcie_pme_platform_setup(struct pcie_device *srv)
+{
+ return !pcie_pme_platform_notify(srv) || pcie_pme_force_enable;
+}
+
+struct pcie_pme_service_data {
+ spinlock_t lock;
+ struct pcie_device *srv;
+ struct work_struct work;
+ bool noirq; /* Don't enable the PME interrupt used by this service. */
+};
+
+/**
+ * pcie_pme_interrupt_enable - Enable/disable PCIe PME interrupt generation.
+ * @dev: PCIe root port or event collector.
+ * @enable: Enable or disable the interrupt.
+ */
+static void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
+{
+ int rtctl_pos;
+ u16 rtctl;
+
+ rtctl_pos = pci_find_capability(dev, PCI_CAP_ID_EXP) + PCI_EXP_RTCTL;
+
+ pci_read_config_word(dev, rtctl_pos, &rtctl);
+ if (enable)
+ rtctl |= PCI_EXP_RTCTL_PMEIE;
+ else
+ rtctl &= ~PCI_EXP_RTCTL_PMEIE;
+ pci_write_config_word(dev, rtctl_pos, rtctl);
+}
+
+/**
+ * pcie_pme_clear_status - Clear root port PME interrupt status.
+ * @dev: PCIe root port or event collector.
+ */
+static void pcie_pme_clear_status(struct pci_dev *dev)
+{
+ int rtsta_pos;
+ u32 rtsta;
+
+ rtsta_pos = pci_find_capability(dev, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ pci_read_config_dword(dev, rtsta_pos, &rtsta);
+ rtsta |= PCI_EXP_RTSTA_PME;
+ pci_write_config_dword(dev, rtsta_pos, rtsta);
+}
+
+/**
+ * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
+ * @bus: PCI bus to scan.
+ *
+ * Scan given PCI bus and all buses under it for devices asserting PME#.
+ */
+static bool pcie_pme_walk_bus(struct pci_bus *bus)
+{
+ struct pci_dev *dev;
+ bool ret = false;
+
+ list_for_each_entry(dev, &bus->devices, bus_list) {
+ /* Skip PCIe devices in case we started from a root port. */
+ if (!dev->is_pcie && pci_check_pme_status(dev)) {
+ pm_request_resume(&dev->dev);
+ ret = true;
+ }
+
+ if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
+ ret = true;
+ }
+
+ return ret;
+}
+
+/**
+ * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
+ * @bus: Secondary bus of the bridge.
+ * @devfn: Device/function number to check.
+ *
+ * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
+ * PCIe PME message. In such that case the bridge should use the Requester ID
+ * of device/function number 0 on its secondary bus.
+ */
+static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
+{
+ struct pci_dev *dev;
+ bool found = false;
+
+ if (devfn)
+ return false;
+
+ dev = pci_dev_get(bus->self);
+ if (!dev)
+ return false;
+
+ if (dev->is_pcie && dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
+ down_read(&pci_bus_sem);
+ if (pcie_pme_walk_bus(bus))
+ found = true;
+ up_read(&pci_bus_sem);
+ }
+
+ pci_dev_put(dev);
+ return found;
+}
+
+/**
+ * pcie_pme_handle_request - Find device that generated PME and handle it.
+ * @port: Root port or event collector that generated the PME interrupt.
+ * @req_id: PCIe Requester ID of the device that generated the PME.
+ */
+static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
+{
+ u8 busnr = req_id >> 8, devfn = req_id & 0xff;
+ struct pci_bus *bus;
+ struct pci_dev *dev;
+ bool found = false;
+
+ /* First, check if the PME is from the root port itself. */
+ if (port->devfn == devfn && port->bus->number == busnr) {
+ if (pci_check_pme_status(port)) {
+ pm_request_resume(&port->dev);
+ found = true;
+ } else {
+ /*
+ * Apparently, the root port generated the PME on behalf
+ * of a non-PCIe device downstream. If this is done by
+ * a root port, the Requester ID field in its status
+ * register may contain either the root port's, or the
+ * source device's information (PCI Express Base
+ * Specification, Rev. 2.0, Section 6.1.9).
+ */
+ down_read(&pci_bus_sem);
+ found = pcie_pme_walk_bus(port->subordinate);
+ up_read(&pci_bus_sem);
+ }
+ goto out;
+ }
+
+ /* Second, find the bus the source device is on. */
+ bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
+ if (!bus)
+ goto out;
+
+ /* Next, check if the PME is from a PCIe-PCI bridge. */
+ found = pcie_pme_from_pci_bridge(bus, devfn);
+ if (found)
+ goto out;
+
+ /* Finally, try to find the PME source on the bus. */
+ down_read(&pci_bus_sem);
+ list_for_each_entry(dev, &bus->devices, bus_list) {
+ pci_dev_get(dev);
+ if (dev->devfn == devfn) {
+ found = true;
+ break;
+ }
+ pci_dev_put(dev);
+ }
+ up_read(&pci_bus_sem);
+
+ if (found) {
+ /* The device is there, but we have to check its PME status. */
+ found = pci_check_pme_status(dev);
+ if (found)
+ pm_request_resume(&dev->dev);
+ pci_dev_put(dev);
+ } else if (devfn) {
+ /*
+ * The device is not there, but we can still try to recover by
+ * assuming that the PME was reported by a PCIe-PCI bridge that
+ * used devfn different from zero.
+ */
+ dev_dbg(&port->dev, "PME interrupt generated for "
+ "non-existent device %02x:%02x.%d\n",
+ busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
+ found = pcie_pme_from_pci_bridge(bus, 0);
+ }
+
+ out:
+ if (!found)
+ dev_dbg(&port->dev, "Spurious native PME interrupt!\n");
+}
+
+/**
+ * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
+ * @work: Work structure giving access to service data.
+ */
+static void pcie_pme_work_fn(struct work_struct *work)
+{
+ struct pcie_pme_service_data *data =
+ container_of(work, struct pcie_pme_service_data, work);
+ struct pci_dev *port = data->srv->port;
+ int rtsta_pos;
+ u32 rtsta;
+
+ rtsta_pos = pci_find_capability(port, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ spin_lock_irq(&data->lock);
+
+ for (;;) {
+ if (data->noirq)
+ break;
+
+ pci_read_config_dword(port, rtsta_pos, &rtsta);
+ if (rtsta & PCI_EXP_RTSTA_PME) {
+ /*
+ * Clear PME status of the port. If there are other
+ * pending PMEs, the status will be set again.
+ */
+ pcie_pme_clear_status(port);
+
+ spin_unlock_irq(&data->lock);
+ pcie_pme_handle_request(port, rtsta & 0xffff);
+ spin_lock_irq(&data->lock);
+
+ continue;
+ }
+
+ /* No need to loop if there are no more PMEs pending. */
+ if (!(rtsta & PCI_EXP_RTSTA_PENDING))
+ break;
+
+ spin_unlock_irq(&data->lock);
+ cpu_relax();
+ spin_lock_irq(&data->lock);
+ }
+
+ if (!data->noirq)
+ pcie_pme_interrupt_enable(port, true);
+
+ spin_unlock_irq(&data->lock);
+}
+
+/**
+ * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
+ * @irq: Interrupt vector.
+ * @context: Interrupt context pointer.
+ */
+static irqreturn_t pcie_pme_irq(int irq, void *context)
+{
+ struct pci_dev *port;
+ struct pcie_pme_service_data *data;
+ int rtsta_pos;
+ u32 rtsta;
+ unsigned long flags;
+
+ port = ((struct pcie_device *)context)->port;
+ data = get_service_data((struct pcie_device *)context);
+
+ rtsta_pos = pci_find_capability(port, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ spin_lock_irqsave(&data->lock, flags);
+ pci_read_config_dword(port, rtsta_pos, &rtsta);
+
+ if (!(rtsta & PCI_EXP_RTSTA_PME)) {
+ spin_unlock_irqrestore(&data->lock, flags);
+ return IRQ_NONE;
+ }
+
+ pcie_pme_interrupt_enable(port, false);
+ spin_unlock_irqrestore(&data->lock, flags);
+
+ /* We don't use pm_wq, because it's freezable. */
+ schedule_work(&data->work);
+
+ return IRQ_HANDLED;
+}
+
+/**
+ * pcie_pme_set_native - Set the PME interrupt flag for given device.
+ * @dev: PCI device to handle.
+ * @ign: Ignored.
+ */
+static int pcie_pme_set_native(struct pci_dev *dev, void *ign)
+{
+ dev_info(&dev->dev, "Signaling PME through PCIe PME interrupt\n");
+
+ device_set_run_wake(&dev->dev, true);
+ dev->pme_interrupt = true;
+ return 0;
+}
+
+/**
+ * pcie_pme_mark_devices - Set the PME interrupt flag for devices below a port.
+ * @port: PCIe root port or event collector to handle.
+ *
+ * For each device below given root port, including the port itself (or for each
+ * root complex integrated endpoint if @port is a root complex event collector)
+ * set the flag indicating that it can signal run-time wake-up events via PCIe
+ * PME interrupts.
+ */
+static void pcie_pme_mark_devices(struct pci_dev *port)
+{
+ pcie_pme_set_native(port, NULL);
+ if (port->subordinate) {
+ pci_walk_bus(port->subordinate, pcie_pme_set_native, NULL);
+ } else {
+ struct pci_bus *bus = port->bus;
+ struct pci_dev *dev;
+
+ /* Check if this is a root port event collector. */
+ if (port->pcie_type != PCI_EXP_TYPE_RC_EC || !bus)
+ return;
+
+ down_read(&pci_bus_sem);
+ list_for_each_entry(dev, &bus->devices, bus_list)
+ if (dev->is_pcie
+ && dev->pcie_type == PCI_EXP_TYPE_RC_END)
+ pcie_pme_set_native(dev, NULL);
+ up_read(&pci_bus_sem);
+ }
+}
+
+/**
+ * pcie_pme_probe - Initialize PCIe PME service for given root port.
+ * @srv: PCIe service to initialize.
+ */
+static int pcie_pme_probe(struct pcie_device *srv)
+{
+ struct pci_dev *port;
+ struct pcie_pme_service_data *data;
+ int ret;
+
+ if (!pcie_pme_platform_setup(srv))
+ return -EACCES;
+
+ data = kzalloc(sizeof(*data), GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
+ spin_lock_init(&data->lock);
+ INIT_WORK(&data->work, pcie_pme_work_fn);
+ data->srv = srv;
+ set_service_data(srv, data);
+
+ port = srv->port;
+ pcie_pme_interrupt_enable(port, false);
+ pcie_pme_clear_status(port);
+
+ ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
+ if (ret) {
+ kfree(data);
+ } else {
+ pcie_pme_mark_devices(port);
+ pcie_pme_interrupt_enable(port, true);
+ }
+
+ return ret;
+}
+
+/**
+ * pcie_pme_suspend - Suspend PCIe PME service device.
+ * @srv: PCIe service device to suspend.
+ */
+static int pcie_pme_suspend(struct pcie_device *srv)
+{
+ struct pcie_pme_service_data *data = get_service_data(srv);
+ struct pci_dev *port = srv->port;
+
+ spin_lock_irq(&data->lock);
+ pcie_pme_interrupt_enable(port, false);
+ pcie_pme_clear_status(port);
+ data->noirq = true;
+ spin_unlock_irq(&data->lock);
+
+ synchronize_irq(srv->irq);
+
+ return 0;
+}
+
+/**
+ * pcie_pme_resume - Resume PCIe PME service device.
+ * @srv - PCIe service device to resume.
+ */
+static int pcie_pme_resume(struct pcie_device *srv)
+{
+ struct pcie_pme_service_data *data = get_service_data(srv);
+ struct pci_dev *port = srv->port;
+
+ spin_lock_irq(&data->lock);
+ data->noirq = false;
+ pcie_pme_clear_status(port);
+ pcie_pme_interrupt_enable(port, true);
+ spin_unlock_irq(&data->lock);
+
+ return 0;
+}
+
+/**
+ * pcie_pme_remove - Prepare PCIe PME service device for removal.
+ * @srv - PCIe service device to resume.
+ */
+static int pcie_pme_remove(struct pcie_device *srv)
+{
+ pcie_pme_suspend(srv);
+ free_irq(srv->irq, srv);
+ kfree(get_service_data(srv));
+
+ return 0;
+}
+
+static struct pcie_port_service_driver pcie_pme_driver = {
+ .name = "pcie_pme",
+ .port_type = PCIE_RC_PORT,
+ .service = PCIE_PORT_SERVICE_PME,
+
+ .probe = pcie_pme_probe,
+ .suspend = pcie_pme_suspend,
+ .resume = pcie_pme_resume,
+ .remove = pcie_pme_remove,
+};
+
+/**
+ * pcie_pme_service_init - Register the PCIe PME service driver.
+ */
+static int __init pcie_pme_service_init(void)
+{
+ return pcie_pme_disabled ?
+ -ENODEV : pcie_port_service_register(&pcie_pme_driver);
+}
+
+module_init(pcie_pme_service_init);
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme_acpi.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/pci/pcie/pme/pcie_pme_acpi.c
@@ -0,0 +1,54 @@
+/*
+ * PCIe Native PME support, ACPI-related part
+ *
+ * Copyright (C) 2009 Rafael J. Wysocki <[email protected]>, Novell Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License V2. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#include <linux/pci.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/acpi.h>
+#include <linux/pci-acpi.h>
+#include <linux/pcieport_if.h>
+
+/**
+ * pcie_pme_acpi_setup - Request the ACPI BIOS to release control over PCIe PME.
+ * @srv - PCIe PME service for a root port or event collector.
+ *
+ * Invoked when the PCIe bus type loads PCIe PME service driver. To avoid
+ * conflict with the BIOS PCIe support requires the BIOS to yield PCIe PME
+ * control to the kernel.
+ */
+int pcie_pme_acpi_setup(struct pcie_device *srv)
+{
+ acpi_status status = AE_NOT_FOUND;
+ struct pci_dev *port = srv->port;
+ acpi_handle handle;
+ int error = 0;
+
+ if (acpi_pci_disabled)
+ return -ENOSYS;
+
+ dev_info(&port->dev, "Requesting control of PCIe PME from ACPI BIOS\n");
+
+ handle = acpi_find_root_bridge_handle(port);
+ if (!handle)
+ return -EINVAL;
+
+ status = acpi_pci_osc_control_set(handle,
+ OSC_PCI_EXPRESS_PME_CONTROL |
+ OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
+ if (ACPI_FAILURE(status)) {
+ dev_info(&port->dev,
+ "Failed to receive control of PCIe PME service: %s\n",
+ (status == AE_SUPPORT || status == AE_NOT_FOUND) ?
+ "no _OSC support" : "ACPI _OSC failed");
+ error = -ENODEV;
+ }
+
+ return error;
+}
Index: linux-2.6/drivers/pci/pcie/pme/Makefile
===================================================================
--- /dev/null
+++ 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
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.h
===================================================================
--- /dev/null
+++ 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
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -238,6 +238,7 @@ struct pci_dev {
configuration space */
unsigned int pme_support:5; /* Bitmask of states from which PME#
can be generated */
+ unsigned int pme_interrupt:1;
unsigned int d1_support:1; /* Low power state D1 is supported */
unsigned int d2_support:1; /* Low power state D2 is supported */
unsigned int no_d1d2:1; /* Only allow D0 and D3 */
Index: linux-2.6/Documentation/kernel-parameters.txt
===================================================================
--- linux-2.6.orig/Documentation/kernel-parameters.txt
+++ linux-2.6/Documentation/kernel-parameters.txt
@@ -1966,6 +1966,12 @@ and is between 256 and 4096 characters.
force Enable ASPM even on devices that claim not to support it.
WARNING: Forcing ASPM on may cause system lockups.

+ pcie_pme= [PCIE,PM] Native PCIe PME signaling options:
+ off Do not use native PCIe PME signaling.
+ force Use native PCIe PME signaling even if the BIOS refuses
+ to allow the kernel to control the relevant PCIe config
+ registers.
+
pcmv= [HW,PCMCIA] BadgePAD 4

pd. [PARIDE]

2009-11-29 15:47:39

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 5/12] PCI PM: Make it possible to force using INTx for PCIe PME signaling

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

Apparently, some machines may have problems with PCI run-time power
management if MSIs are used for the hative PCIe PME signaling. In
particular, on the MSI Wind U-100 PCIe PME interrupts are not
generated by a PCIe root port after a resume from suspend to RAM, if
the system wake-up was triggered by a PME from the device attached to
this port. [It doesn't help to free the interrupt on suspend and
request it back on resume, even if that is done along with disabling
the MSI and re-enabling it, respectively.] However, if INTx
interrupts are used for this purpose on the same machine, everything
works just fine.

For this reason, add a kernel command line switch allowing one to
request that MSIs be not used for the native PCIe PME signaling,
introduce a DMI table allowing us to blacklist machines that need
this switch to be set by default and put the MSI Wind U-100 into this
table.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
Documentation/kernel-parameters.txt | 2 ++
drivers/pci/pcie/pme/pcie_pme.c | 14 +++++++++++++-
drivers/pci/pcie/portdrv.h | 17 +++++++++++++++++
drivers/pci/pcie/portdrv_core.c | 5 +++++
drivers/pci/pcie/portdrv_pci.c | 26 ++++++++++++++++++++++++++
5 files changed, 63 insertions(+), 1 deletion(-)

Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/pme/pcie_pme.c
+++ linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
@@ -53,12 +53,22 @@ static bool pcie_pme_disabled;
*/
static bool pcie_pme_force_enable;

+/*
+ * If this switch is set, MSI will not be used for PCIe PME signaling. This
+ * causes the PCIe port driver to use INTx interrupts only, but it turns out
+ * that using MSI for PCIe PME signaling doesn't play well with PCIe PME-based
+ * wake-up from system sleep states.
+ */
+bool pcie_pme_msi_disabled;
+
static int __init pcie_pme_setup(char *str)
{
if (!strcmp(str, "off"))
pcie_pme_disabled = true;
else if (!strcmp(str, "force"))
pcie_pme_force_enable = true;
+ else if (!strcmp(str, "nomsi"))
+ pcie_pme_msi_disabled = true;
return 1;
}
__setup("pcie_pme=", pcie_pme_setup);
@@ -73,7 +83,9 @@ __setup("pcie_pme=", pcie_pme_setup);
*/
static bool pcie_pme_platform_setup(struct pcie_device *srv)
{
- return !pcie_pme_platform_notify(srv) || pcie_pme_force_enable;
+ if (!pcie_pme_platform_notify(srv))
+ return true;
+ return pcie_pme_force_enable;
}

struct pcie_pme_service_data {
Index: linux-2.6/drivers/pci/pcie/portdrv_core.c
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/portdrv_core.c
+++ linux-2.6/drivers/pci/pcie/portdrv_core.c
@@ -190,6 +190,10 @@ static int assign_interrupt_mode(struct
int irq, interrupt_mode = PCIE_PORT_NO_IRQ;
int i;

+ /* We have to use INTx if MSI cannot be used for PCIe PME. */
+ if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
+ goto no_msi;
+
/* Try to use MSI-X if supported */
if (!pcie_port_enable_msix(dev, vectors, mask))
return PCIE_PORT_MSIX_MODE;
@@ -198,6 +202,7 @@ static int assign_interrupt_mode(struct
if (!pci_enable_msi(dev))
interrupt_mode = PCIE_PORT_MSI_MODE;

+ no_msi:
if (interrupt_mode == PCIE_PORT_NO_IRQ && dev->pin)
interrupt_mode = PCIE_PORT_INTx_MODE;

Index: linux-2.6/drivers/pci/pcie/portdrv_pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/portdrv_pci.c
+++ linux-2.6/drivers/pci/pcie/portdrv_pci.c
@@ -15,6 +15,7 @@
#include <linux/slab.h>
#include <linux/pcieport_if.h>
#include <linux/aer.h>
+#include <linux/dmi.h>

#include "portdrv.h"
#include "aer/aerdrv.h"
@@ -272,10 +273,35 @@ static struct pci_driver pcie_portdriver
.driver.pm = PCIE_PORTDRV_PM_OPS,
};

+static int __init dmi_pcie_pme_disable_msi(const struct dmi_system_id *d)
+{
+ pr_notice("%s detected: will not use MSI for PCIe PME signaling\n",
+ d->ident);
+ pcie_pme_disable_msi();
+ return 0;
+}
+
+static struct dmi_system_id __initdata pcie_portdrv_dmi_table[] = {
+ /*
+ * Boxes that should not use MSI for PCIe PME signaling.
+ */
+ {
+ .callback = dmi_pcie_pme_disable_msi,
+ .ident = "MSI Wind U-100",
+ .matches = {
+ DMI_MATCH(DMI_SYS_VENDOR,
+ "MICRO-STAR INTERNATIONAL CO., LTD"),
+ DMI_MATCH(DMI_PRODUCT_NAME, "U-100"),
+ },
+ },
+};
+
static int __init pcie_portdrv_init(void)
{
int retval;

+ dmi_check_system(pcie_portdrv_dmi_table);
+
retval = pcie_port_bus_register();
if (retval) {
printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
Index: linux-2.6/drivers/pci/pcie/portdrv.h
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/portdrv.h
+++ linux-2.6/drivers/pci/pcie/portdrv.h
@@ -45,4 +45,21 @@ extern void pcie_port_device_remove(stru
extern int __must_check pcie_port_bus_register(void);
extern void pcie_port_bus_unregister(void);

+#ifdef CONFIG_PCIE_PME
+extern bool pcie_pme_msi_disabled;
+
+static inline void pcie_pme_disable_msi(void)
+{
+ pcie_pme_msi_disabled = true;
+}
+
+static inline bool pcie_pme_no_msi(void)
+{
+ return pcie_pme_msi_disabled;
+}
+#else /* !CONFIG_PCIE_PME */
+static inline void pcie_pme_disable_msi(void) {}
+static inline bool pcie_pme_no_msi(void) { return false; }
+#endif /* !CONFIG_PCIE_PME */
+
#endif /* _PORTDRV_H_ */
Index: linux-2.6/Documentation/kernel-parameters.txt
===================================================================
--- linux-2.6.orig/Documentation/kernel-parameters.txt
+++ linux-2.6/Documentation/kernel-parameters.txt
@@ -1971,6 +1971,8 @@ and is between 256 and 4096 characters.
force Use native PCIe PME signaling even if the BIOS refuses
to allow the kernel to control the relevant PCIe config
registers.
+ nomsi Do not use MSI for native PCIe PME signaling (this makes
+ all PCIe root ports use INTx for everything).

pcmv= [HW,PCMCIA] BadgePAD 4

2009-11-29 15:46:29

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 6/12] ACPI: Add infrastructure for refcounting GPE consumers

From: Matthew Garrett <[email protected]>

ACPI GPEs may map to multiple devices. The current GPE interface only
provides a mechanism for enabling and disabling GPEs, making it difficult
to change the state of GPEs at runtime without extensive cooperation
between devices. Add an API to allow devices to indicate whether or not
they want their device's GPE to be enabled for both runtime and wakeup
events.

Signed-off-by: Matthew Garrett <[email protected]>
Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/acpica/aclocal.h | 2 +
drivers/acpi/acpica/aclocal.h | 2
drivers/acpi/acpica/evxfevnt.c | 161 +++++++++++++++++++++++++++++++++++++++++
include/acpi/acpixf.h | 8 ++
3 files changed, 171 insertions(+)

Index: linux-2.6/drivers/acpi/acpica/aclocal.h
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/aclocal.h
+++ linux-2.6/drivers/acpi/acpica/aclocal.h
@@ -426,6 +426,8 @@ struct acpi_gpe_event_info {
struct acpi_gpe_register_info *register_info; /* Backpointer to register info */
u8 flags; /* Misc info about this GPE */
u8 gpe_number; /* This GPE */
+ u8 runtime_count;
+ u8 wakeup_count;
};

/* Information about a GPE register pair, one per each status/enable pair in an array */
Index: linux-2.6/drivers/acpi/acpica/evxfevnt.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evxfevnt.c
+++ linux-2.6/drivers/acpi/acpica/evxfevnt.c
@@ -201,6 +201,167 @@ ACPI_EXPORT_SYMBOL(acpi_enable_event)

/*******************************************************************************
*
+ * FUNCTION: acpi_ref_runtime_gpe
+ *
+ * PARAMETERS: gpe_device - Parent GPE Device
+ * gpe_number - GPE level within the GPE block
+ *
+ * RETURN: Status
+ *
+ * DESCRIPTION: Take a reference to a runtime GPE
+ *
+ ******************************************************************************/
+acpi_status acpi_ref_runtime_gpe(acpi_handle gpe_device, u32 gpe_number)
+{
+ acpi_status status = AE_OK;
+ acpi_cpu_flags flags;
+ struct acpi_gpe_event_info *gpe_event_info;
+
+ ACPI_FUNCTION_TRACE(acpi_ref_runtime_gpe);
+
+ flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
+
+ /* Ensure that we have a valid GPE number */
+
+ gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
+ if (!gpe_event_info) {
+ status = AE_BAD_PARAMETER;
+ goto unlock_and_exit;
+ }
+
+ if (++gpe_event_info->runtime_count == 1)
+ status = acpi_ev_enable_gpe(gpe_event_info, TRUE);
+
+ if (ACPI_FAILURE(status))
+ gpe_event_info->runtime_count--;
+
+unlock_and_exit:
+ acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
+ return_ACPI_STATUS(status);
+}
+ACPI_EXPORT_SYMBOL(acpi_ref_runtime_gpe)
+
+/*******************************************************************************
+ *
+ * FUNCTION: acpi_unref_runtime_gpe
+ *
+ * PARAMETERS: gpe_device - Parent GPE Device
+ * gpe_number - GPE level within the GPE block
+ *
+ * RETURN: Status
+ *
+ * DESCRIPTION: Release a reference to a runtime GPE
+ *
+ ******************************************************************************/
+acpi_status acpi_unref_runtime_gpe(acpi_handle gpe_device, u32 gpe_number)
+{
+ acpi_status status = AE_OK;
+ acpi_cpu_flags flags;
+ struct acpi_gpe_event_info *gpe_event_info;
+
+ ACPI_FUNCTION_TRACE(acpi_unref_runtime_gpe);
+
+ flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
+
+ /* Ensure that we have a valid GPE number */
+
+ gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
+ if (!gpe_event_info) {
+ status = AE_BAD_PARAMETER;
+ goto unlock_and_exit;
+ }
+
+ if (--gpe_event_info->runtime_count == 0)
+ acpi_ev_disable_gpe(gpe_event_info);
+
+unlock_and_exit:
+ acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
+ return_ACPI_STATUS(status);
+}
+ACPI_EXPORT_SYMBOL(acpi_unref_runtime_gpe)
+
+/*******************************************************************************
+ *
+ * FUNCTION: acpi_ref_wakeup_gpe
+ *
+ * PARAMETERS: gpe_device - Parent GPE Device
+ * gpe_number - GPE level within the GPE block
+ *
+ * RETURN: Status
+ *
+ * DESCRIPTION: Take a reference to a wakeup GPE
+ *
+ ******************************************************************************/
+acpi_status acpi_ref_wakeup_gpe(acpi_handle gpe_device, u32 gpe_number)
+{
+ acpi_status status = AE_OK;
+ acpi_cpu_flags flags;
+ struct acpi_gpe_event_info *gpe_event_info;
+
+ ACPI_FUNCTION_TRACE(acpi_ref_wakeup_gpe);
+
+ flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
+
+ /* Ensure that we have a valid GPE number */
+
+ gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
+ if (!gpe_event_info) {
+ status = AE_BAD_PARAMETER;
+ goto unlock_and_exit;
+ }
+
+ if (++gpe_event_info->wakeup_count == 1)
+ acpi_ev_update_gpe_enable_masks(gpe_event_info,
+ ACPI_GPE_ENABLE);
+
+unlock_and_exit:
+ acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
+ return_ACPI_STATUS(status);
+}
+ACPI_EXPORT_SYMBOL(acpi_ref_wakeup_gpe)
+
+/*******************************************************************************
+ *
+ * FUNCTION: acpi_unref_wakeup_gpe
+ *
+ * PARAMETERS: gpe_device - Parent GPE Device
+ * gpe_number - GPE level within the GPE block
+ *
+ * RETURN: Status
+ *
+ * DESCRIPTION: Release a reference to a wakeup GPE
+ *
+ ******************************************************************************/
+acpi_status acpi_unref_wakeup_gpe(acpi_handle gpe_device, u32 gpe_number)
+{
+ acpi_status status = AE_OK;
+ acpi_cpu_flags flags;
+ struct acpi_gpe_event_info *gpe_event_info;
+
+ ACPI_FUNCTION_TRACE(acpi_unref_wakeup_gpe);
+
+ flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
+
+ /* Ensure that we have a valid GPE number */
+
+ gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
+ if (!gpe_event_info) {
+ status = AE_BAD_PARAMETER;
+ goto unlock_and_exit;
+ }
+
+ if (--gpe_event_info->wakeup_count == 0)
+ acpi_ev_update_gpe_enable_masks(gpe_event_info,
+ ACPI_GPE_DISABLE);
+
+unlock_and_exit:
+ acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
+ return_ACPI_STATUS(status);
+}
+ACPI_EXPORT_SYMBOL(acpi_unref_wakeup_gpe)
+
+/*******************************************************************************
+ *
* FUNCTION: acpi_set_gpe_type
*
* PARAMETERS: gpe_device - Parent GPE Device
Index: linux-2.6/include/acpi/acpixf.h
===================================================================
--- linux-2.6.orig/include/acpi/acpixf.h
+++ linux-2.6/include/acpi/acpixf.h
@@ -282,6 +282,14 @@ acpi_status acpi_get_event_status(u32 ev
*/
acpi_status acpi_set_gpe_type(acpi_handle gpe_device, u32 gpe_number, u8 type);

+acpi_status acpi_ref_runtime_gpe(acpi_handle gpe_device, u32 gpe_number);
+
+acpi_status acpi_unref_runtime_gpe(acpi_handle gpe_device, u32 gpe_number);
+
+acpi_status acpi_ref_wakeup_gpe(acpi_handle gpe_device, u32 gpe_number);
+
+acpi_status acpi_unref_wakeup_gpe(acpi_handle gpe_device, u32 gpe_number);
+
acpi_status acpi_enable_gpe(acpi_handle gpe_device, u32 gpe_number);

acpi_status acpi_disable_gpe(acpi_handle gpe_device, u32 gpe_number);

2009-11-29 15:48:05

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 7/12] ACPI: Add support for new refcounted GPE API to drivers

From: Matthew Garrett <[email protected]>

Add GPE refcounting support to ACPI drivers that need it. This will
currently do little until the core is changed over to use the new
behaviour.

Signed-off-by: Matthew Garrett <[email protected]>
Signed-off-by; Rafael J. Wysocki <[email protected]>
---
drivers/acpi/button.c | 12 ++++++++++++
drivers/acpi/button.c | 12 ++++++++++++
drivers/acpi/ec.c | 4 +++-
drivers/acpi/wakeup.c | 6 ++++++
3 files changed, 21 insertions(+), 1 deletion(-)

Index: linux-2.6/drivers/acpi/button.c
===================================================================
--- linux-2.6.orig/drivers/acpi/button.c
+++ linux-2.6/drivers/acpi/button.c
@@ -420,6 +420,10 @@ static int acpi_button_add(struct acpi_d
ACPI_GPE_TYPE_WAKE_RUN);
acpi_enable_gpe(device->wakeup.gpe_device,
device->wakeup.gpe_number);
+ acpi_ref_runtime_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
+ acpi_ref_wakeup_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
device->wakeup.state.enabled = 1;
}

@@ -439,6 +443,14 @@ static int acpi_button_remove(struct acp
{
struct acpi_button *button = acpi_driver_data(device);

+ if (device->wakeup.flags.valid) {
+ acpi_unref_runtime_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
+ acpi_unref_wakeup_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
+ device->wakeup.state.enabled = 0;
+ }
+
acpi_button_remove_fs(device);
input_unregister_device(button->input);
kfree(button);
Index: linux-2.6/drivers/acpi/ec.c
===================================================================
--- linux-2.6.orig/drivers/acpi/ec.c
+++ linux-2.6/drivers/acpi/ec.c
@@ -755,7 +755,7 @@ static int ec_install_handlers(struct ac
if (ACPI_FAILURE(status))
return -ENODEV;
acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
- acpi_enable_gpe(NULL, ec->gpe);
+ acpi_ref_runtime_gpe(NULL, ec->gpe);
status = acpi_install_address_space_handler(ec->handle,
ACPI_ADR_SPACE_EC,
&acpi_ec_space_handler,
@@ -772,6 +772,7 @@ static int ec_install_handlers(struct ac
} else {
acpi_remove_gpe_handler(NULL, ec->gpe,
&acpi_ec_gpe_handler);
+ acpi_unref_runtime_gpe(NULL, ec->gpe);
return -ENODEV;
}
}
@@ -782,6 +783,7 @@ static int ec_install_handlers(struct ac

static void ec_remove_handlers(struct acpi_ec *ec)
{
+ acpi_unref_runtime_gpe(NULL, ec->gpe);
if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
pr_err(PREFIX "failed to remove space handler\n");
Index: linux-2.6/drivers/acpi/wakeup.c
===================================================================
--- linux-2.6.orig/drivers/acpi/wakeup.c
+++ linux-2.6/drivers/acpi/wakeup.c
@@ -81,6 +81,8 @@ void acpi_enable_wakeup_device(u8 sleep_
if (!dev->wakeup.flags.run_wake)
acpi_enable_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
+ acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
}
}

@@ -121,6 +123,8 @@ void acpi_disable_wakeup_device(u8 sleep
acpi_clear_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number, ACPI_NOT_ISR);
}
+ acpi_unref_wakeup_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
}
}

@@ -141,6 +145,8 @@ int __init acpi_wakeup_device_init(void)
ACPI_GPE_TYPE_WAKE_RUN);
acpi_enable_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
+ acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
dev->wakeup.state.enabled = 1;
}
mutex_unlock(&acpi_device_lock);

2009-11-29 15:46:32

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 8/12] ACPI: Remove old GPE API and transition code entirely to new one

From: Matthew Garrett <[email protected]>

Remove the old GPE type handling entirely, which gets rid of various quirks
like the implicit disabling with GPE type setting. This requires a small
amount of rework in order to ensure that non-wake GPEs are enabled by
default to preserve existing behaviour.

Signed-off-by: Matthew Garrett <[email protected]>
Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/acpica/acevents.h | 6 +--
drivers/acpi/acpica/acevents.h | 6 -
drivers/acpi/acpica/evgpe.c | 147 +++--------------------------------------
drivers/acpi/acpica/evgpeblk.c | 69 ++++++-------------
drivers/acpi/acpica/evxface.c | 14 ---
drivers/acpi/acpica/evxfevnt.c | 48 -------------
drivers/acpi/button.c | 5 -
drivers/acpi/ec.c | 2
drivers/acpi/wakeup.c | 55 ++-------------
include/acpi/actypes.h | 32 ++------
9 files changed, 59 insertions(+), 319 deletions(-)

Index: linux-2.6/drivers/acpi/acpica/acevents.h
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/acevents.h
+++ linux-2.6/drivers/acpi/acpica/acevents.h
@@ -76,8 +76,7 @@ acpi_ev_queue_notify_request(struct acpi
* evgpe - GPE handling and dispatch
*/
acpi_status
-acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info,
- u8 type);
+acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info);

acpi_status
acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info,
@@ -122,9 +121,6 @@ acpi_ev_gpe_dispatch(struct acpi_gpe_eve
u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info *gpe_xrupt_list);

acpi_status
-acpi_ev_set_gpe_type(struct acpi_gpe_event_info *gpe_event_info, u8 type);
-
-acpi_status
acpi_ev_check_for_wake_only_gpe(struct acpi_gpe_event_info *gpe_event_info);

acpi_status acpi_ev_gpe_initialize(void);
Index: linux-2.6/drivers/acpi/acpica/evgpe.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evgpe.c
+++ linux-2.6/drivers/acpi/acpica/evgpe.c
@@ -54,54 +54,9 @@ static void ACPI_SYSTEM_XFACE acpi_ev_as

/*******************************************************************************
*
- * FUNCTION: acpi_ev_set_gpe_type
- *
- * PARAMETERS: gpe_event_info - GPE to set
- * Type - New type
- *
- * RETURN: Status
- *
- * DESCRIPTION: Sets the new type for the GPE (wake, run, or wake/run)
- *
- ******************************************************************************/
-
-acpi_status
-acpi_ev_set_gpe_type(struct acpi_gpe_event_info *gpe_event_info, u8 type)
-{
- acpi_status status;
-
- ACPI_FUNCTION_TRACE(ev_set_gpe_type);
-
- /* Validate type and update register enable masks */
-
- switch (type) {
- case ACPI_GPE_TYPE_WAKE:
- case ACPI_GPE_TYPE_RUNTIME:
- case ACPI_GPE_TYPE_WAKE_RUN:
- break;
-
- default:
- return_ACPI_STATUS(AE_BAD_PARAMETER);
- }
-
- /* Disable the GPE if currently enabled */
-
- status = acpi_ev_disable_gpe(gpe_event_info);
-
- /* Clear the type bits and insert the new Type */
-
- gpe_event_info->flags &= ~ACPI_GPE_TYPE_MASK;
- gpe_event_info->flags |= type;
- return_ACPI_STATUS(status);
-}
-
-/*******************************************************************************
- *
* FUNCTION: acpi_ev_update_gpe_enable_masks
*
* PARAMETERS: gpe_event_info - GPE to update
- * Type - What to do: ACPI_GPE_DISABLE or
- * ACPI_GPE_ENABLE
*
* RETURN: Status
*
@@ -110,8 +65,7 @@ acpi_ev_set_gpe_type(struct acpi_gpe_eve
******************************************************************************/

acpi_status
-acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info,
- u8 type)
+acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info)
{
struct acpi_gpe_register_info *gpe_register_info;
u8 register_bit;
@@ -127,37 +81,13 @@ acpi_ev_update_gpe_enable_masks(struct a
(1 <<
(gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));

- /* 1) Disable case. Simply clear all enable bits */
-
- if (type == ACPI_GPE_DISABLE) {
- ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
- register_bit);
- ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
- return_ACPI_STATUS(AE_OK);
- }
-
- /* 2) Enable case. Set/Clear the appropriate enable bits */
-
- switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
- case ACPI_GPE_TYPE_WAKE:
- ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
- ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
- break;
+ ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake, register_bit);
+ ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);

- case ACPI_GPE_TYPE_RUNTIME:
- ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
- register_bit);
+ if (gpe_event_info->runtime_count)
ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
- break;
-
- case ACPI_GPE_TYPE_WAKE_RUN:
+ if (gpe_event_info->wakeup_count)
ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
- ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
- break;
-
- default:
- return_ACPI_STATUS(AE_BAD_PARAMETER);
- }

return_ACPI_STATUS(AE_OK);
}
@@ -186,47 +116,21 @@ acpi_ev_enable_gpe(struct acpi_gpe_event

/* Make sure HW enable masks are updated */

- status =
- acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_ENABLE);
+ status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
if (ACPI_FAILURE(status)) {
return_ACPI_STATUS(status);
}

/* Mark wake-enabled or HW enable, or both */

- switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
- case ACPI_GPE_TYPE_WAKE:
-
- ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
- break;
-
- case ACPI_GPE_TYPE_WAKE_RUN:
-
- ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
-
- /*lint -fallthrough */
-
- case ACPI_GPE_TYPE_RUNTIME:
-
- ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
-
- if (write_to_hardware) {
-
- /* Clear the GPE (of stale events), then enable it */
-
- status = acpi_hw_clear_gpe(gpe_event_info);
- if (ACPI_FAILURE(status)) {
- return_ACPI_STATUS(status);
- }
-
- /* Enable the requested runtime GPE */
-
- status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
- }
- break;
+ if (gpe_event_info->runtime_count && write_to_hardware) {
+ /* Clear the GPE (of stale events), then enable it */
+ status = acpi_hw_clear_gpe(gpe_event_info);
+ if (ACPI_FAILURE(status))
+ return_ACPI_STATUS(status);

- default:
- return_ACPI_STATUS(AE_BAD_PARAMETER);
+ /* Enable the requested runtime GPE */
+ status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
}

return_ACPI_STATUS(AE_OK);
@@ -253,34 +157,11 @@ acpi_status acpi_ev_disable_gpe(struct a
/* Make sure HW enable masks are updated */

status =
- acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_DISABLE);
+ acpi_ev_update_gpe_enable_masks(gpe_event_info);
if (ACPI_FAILURE(status)) {
return_ACPI_STATUS(status);
}

- /* Clear the appropriate enabled flags for this GPE */
-
- switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
- case ACPI_GPE_TYPE_WAKE:
- ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
- break;
-
- case ACPI_GPE_TYPE_WAKE_RUN:
- ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
-
- /* fallthrough */
-
- case ACPI_GPE_TYPE_RUNTIME:
-
- /* Disable the requested runtime GPE */
-
- ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
- break;
-
- default:
- break;
- }
-
/*
* Even if we don't know the GPE type, make sure that we always
* disable it. low_disable_gpe will just clear the enable bit for this
Index: linux-2.6/drivers/acpi/acpica/evgpeblk.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evgpeblk.c
+++ linux-2.6/drivers/acpi/acpica/evgpeblk.c
@@ -325,22 +325,16 @@ acpi_ev_save_method_info(acpi_handle obj

/*
* Now we can add this information to the gpe_event_info block for use
- * during dispatch of this GPE. Default type is RUNTIME, although this may
- * change when the _PRW methods are executed later.
+ * during dispatch of this GPE.
*/
gpe_event_info =
&gpe_block->event_info[gpe_number - gpe_block->block_base_number];

- gpe_event_info->flags = (u8)
- (type | ACPI_GPE_DISPATCH_METHOD | ACPI_GPE_TYPE_RUNTIME);
+ gpe_event_info->flags = (u8) (type | ACPI_GPE_DISPATCH_METHOD);

gpe_event_info->dispatch.method_node =
(struct acpi_namespace_node *)obj_handle;

- /* Update enable mask, but don't enable the HW GPE as of yet */
-
- status = acpi_ev_enable_gpe(gpe_event_info, FALSE);
-
ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
"Registered GPE method %s as GPE number 0x%.2X\n",
name, gpe_number));
@@ -454,20 +448,7 @@ acpi_ev_match_prw_and_gpe(acpi_handle ob
gpe_block->
block_base_number];

- /* Mark GPE for WAKE-ONLY but WAKE_DISABLED */
-
- gpe_event_info->flags &=
- ~(ACPI_GPE_WAKE_ENABLED | ACPI_GPE_RUN_ENABLED);
-
- status =
- acpi_ev_set_gpe_type(gpe_event_info, ACPI_GPE_TYPE_WAKE);
- if (ACPI_FAILURE(status)) {
- goto cleanup;
- }
-
- status =
- acpi_ev_update_gpe_enable_masks(gpe_event_info,
- ACPI_GPE_DISABLE);
+ gpe_event_info->flags |= ACPI_GPE_CAN_WAKE;
}

cleanup:
@@ -1027,33 +1008,35 @@ acpi_ev_initialize_gpe_block(struct acpi
}

/*
- * Enable all GPEs in this block that have these attributes:
- * 1) are "runtime" or "run/wake" GPEs, and
- * 2) have a corresponding _Lxx or _Exx method
- *
- * Any other GPEs within this block must be enabled via the
- * acpi_enable_gpe() external interface.
+ * Enable all GPEs that have a corresponding method and aren't
+ * capable of generating wakeups. Any other GPEs within this block
+ * must be enabled via the acpi_ref_runtime_gpe interface.
*/
wake_gpe_count = 0;
gpe_enabled_count = 0;

for (i = 0; i < gpe_block->register_count; i++) {
for (j = 0; j < 8; j++) {
+ int gpe_number = i * ACPI_GPE_REGISTER_WIDTH + j;

/* Get the info block for this particular GPE */

- gpe_event_info = &gpe_block->event_info[((acpi_size) i *
- ACPI_GPE_REGISTER_WIDTH)
- + j];
-
- if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
- ACPI_GPE_DISPATCH_METHOD) &&
- (gpe_event_info->flags & ACPI_GPE_TYPE_RUNTIME)) {
- gpe_enabled_count++;
- }
+ gpe_event_info = &gpe_block->event_info[(acpi_size)
+ gpe_number];

- if (gpe_event_info->flags & ACPI_GPE_TYPE_WAKE) {
+ if (gpe_event_info->flags & ACPI_GPE_CAN_WAKE) {
wake_gpe_count++;
+ continue;
+ }
+
+ if (gpe_event_info->flags & ACPI_GPE_DISPATCH_METHOD) {
+ gpe_enabled_count++;
+ if (gpe_device == acpi_gbl_fadt_gpe_device)
+ status = acpi_ref_runtime_gpe(NULL,
+ gpe_number);
+ else
+ status = acpi_ref_runtime_gpe(gpe_device,
+ gpe_number);
}
}
}
@@ -1062,15 +1045,7 @@ acpi_ev_initialize_gpe_block(struct acpi
"Found %u Wake, Enabled %u Runtime GPEs in this block\n",
wake_gpe_count, gpe_enabled_count));

- /* Enable all valid runtime GPEs found above */
-
- status = acpi_hw_enable_runtime_gpe_block(NULL, gpe_block, NULL);
- if (ACPI_FAILURE(status)) {
- ACPI_ERROR((AE_INFO, "Could not enable GPEs in GpeBlock %p",
- gpe_block));
- }
-
- return_ACPI_STATUS(status);
+ return_ACPI_STATUS(AE_OK);
}

/*******************************************************************************
Index: linux-2.6/drivers/acpi/acpica/evxface.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evxface.c
+++ linux-2.6/drivers/acpi/acpica/evxface.c
@@ -617,13 +617,6 @@ acpi_install_gpe_handler(acpi_handle gpe
handler->context = context;
handler->method_node = gpe_event_info->dispatch.method_node;

- /* Disable the GPE before installing the handler */
-
- status = acpi_ev_disable_gpe(gpe_event_info);
- if (ACPI_FAILURE(status)) {
- goto unlock_and_exit;
- }
-
/* Install the handler */

flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
@@ -707,13 +700,6 @@ acpi_remove_gpe_handler(acpi_handle gpe_
goto unlock_and_exit;
}

- /* Disable the GPE before removing the handler */
-
- status = acpi_ev_disable_gpe(gpe_event_info);
- if (ACPI_FAILURE(status)) {
- goto unlock_and_exit;
- }
-
/* Make sure all deferred tasks are completed */

(void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Index: linux-2.6/drivers/acpi/acpica/evxfevnt.c
===================================================================
--- linux-2.6.orig/drivers/acpi/acpica/evxfevnt.c
+++ linux-2.6/drivers/acpi/acpica/evxfevnt.c
@@ -311,8 +311,7 @@ acpi_status acpi_ref_wakeup_gpe(acpi_han
}

if (++gpe_event_info->wakeup_count == 1)
- acpi_ev_update_gpe_enable_masks(gpe_event_info,
- ACPI_GPE_ENABLE);
+ acpi_ev_update_gpe_enable_masks(gpe_event_info);

unlock_and_exit:
acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
@@ -351,8 +350,7 @@ acpi_status acpi_unref_wakeup_gpe(acpi_h
}

if (--gpe_event_info->wakeup_count == 0)
- acpi_ev_update_gpe_enable_masks(gpe_event_info,
- ACPI_GPE_DISABLE);
+ acpi_ev_update_gpe_enable_masks(gpe_event_info);

unlock_and_exit:
acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
@@ -362,48 +360,6 @@ ACPI_EXPORT_SYMBOL(acpi_unref_wakeup_gpe

/*******************************************************************************
*
- * FUNCTION: acpi_set_gpe_type
- *
- * PARAMETERS: gpe_device - Parent GPE Device
- * gpe_number - GPE level within the GPE block
- * Type - New GPE type
- *
- * RETURN: Status
- *
- * DESCRIPTION: Set the type of an individual GPE
- *
- ******************************************************************************/
-acpi_status acpi_set_gpe_type(acpi_handle gpe_device, u32 gpe_number, u8 type)
-{
- acpi_status status = AE_OK;
- struct acpi_gpe_event_info *gpe_event_info;
-
- ACPI_FUNCTION_TRACE(acpi_set_gpe_type);
-
- /* Ensure that we have a valid GPE number */
-
- gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
- if (!gpe_event_info) {
- status = AE_BAD_PARAMETER;
- goto unlock_and_exit;
- }
-
- if ((gpe_event_info->flags & ACPI_GPE_TYPE_MASK) == type) {
- return_ACPI_STATUS(AE_OK);
- }
-
- /* Set the new type (will disable GPE if currently enabled) */
-
- status = acpi_ev_set_gpe_type(gpe_event_info, type);
-
- unlock_and_exit:
- return_ACPI_STATUS(status);
-}
-
-ACPI_EXPORT_SYMBOL(acpi_set_gpe_type)
-
-/*******************************************************************************
- *
* FUNCTION: acpi_enable_gpe
*
* PARAMETERS: gpe_device - Parent GPE Device
Index: linux-2.6/drivers/acpi/button.c
===================================================================
--- linux-2.6.orig/drivers/acpi/button.c
+++ linux-2.6/drivers/acpi/button.c
@@ -415,11 +415,6 @@ static int acpi_button_add(struct acpi_d

if (device->wakeup.flags.valid) {
/* Button's GPE is run-wake GPE */
- acpi_set_gpe_type(device->wakeup.gpe_device,
- device->wakeup.gpe_number,
- ACPI_GPE_TYPE_WAKE_RUN);
- acpi_enable_gpe(device->wakeup.gpe_device,
- device->wakeup.gpe_number);
acpi_ref_runtime_gpe(device->wakeup.gpe_device,
device->wakeup.gpe_number);
acpi_ref_wakeup_gpe(device->wakeup.gpe_device,
Index: linux-2.6/drivers/acpi/ec.c
===================================================================
--- linux-2.6.orig/drivers/acpi/ec.c
+++ linux-2.6/drivers/acpi/ec.c
@@ -754,7 +754,7 @@ static int ec_install_handlers(struct ac
&acpi_ec_gpe_handler, ec);
if (ACPI_FAILURE(status))
return -ENODEV;
- acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
+
acpi_ref_runtime_gpe(NULL, ec->gpe);
status = acpi_install_address_space_handler(ec->handle,
ACPI_ADR_SPACE_EC,
Index: linux-2.6/drivers/acpi/wakeup.c
===================================================================
--- linux-2.6.orig/drivers/acpi/wakeup.c
+++ linux-2.6/drivers/acpi/wakeup.c
@@ -62,25 +62,12 @@ void acpi_enable_wakeup_device(u8 sleep_
struct acpi_device *dev =
container_of(node, struct acpi_device, wakeup_list);

- if (!dev->wakeup.flags.valid)
+ if (!dev->wakeup.flags.valid ||
+ !dev->wakeup.prepare_count ||
+ !dev->wakeup.state.enabled ||
+ (sleep_state > (u32) dev->wakeup.sleep_state))
continue;

- /* If users want to disable run-wake GPE,
- * we only disable it for wake and leave it for runtime
- */
- if ((!dev->wakeup.state.enabled && !dev->wakeup.prepare_count)
- || sleep_state > (u32) dev->wakeup.sleep_state) {
- if (dev->wakeup.flags.run_wake) {
- /* set_gpe_type will disable GPE, leave it like that */
- acpi_set_gpe_type(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number,
- ACPI_GPE_TYPE_RUNTIME);
- }
- continue;
- }
- if (!dev->wakeup.flags.run_wake)
- acpi_enable_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
}
@@ -99,32 +86,13 @@ void acpi_disable_wakeup_device(u8 sleep
struct acpi_device *dev =
container_of(node, struct acpi_device, wakeup_list);

- if (!dev->wakeup.flags.valid)
- continue;
-
- if ((!dev->wakeup.state.enabled && !dev->wakeup.prepare_count)
- || sleep_state > (u32) dev->wakeup.sleep_state) {
- if (dev->wakeup.flags.run_wake) {
- acpi_set_gpe_type(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number,
- ACPI_GPE_TYPE_WAKE_RUN);
- /* Re-enable it, since set_gpe_type will disable it */
- acpi_enable_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
- }
- continue;
- }
+ if (dev->wakeup.state.enabled &&
+ dev->wakeup.prepare_count &&
+ sleep_state <= (u32) dev->wakeup.sleep_state)
+ acpi_unref_wakeup_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);

acpi_disable_wakeup_device_power(dev);
- /* Never disable run-wake GPE */
- if (!dev->wakeup.flags.run_wake) {
- acpi_disable_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
- acpi_clear_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number, ACPI_NOT_ISR);
- }
- acpi_unref_wakeup_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
}
}

@@ -140,11 +108,6 @@ int __init acpi_wakeup_device_init(void)
/* In case user doesn't load button driver */
if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
continue;
- acpi_set_gpe_type(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number,
- ACPI_GPE_TYPE_WAKE_RUN);
- acpi_enable_gpe(dev->wakeup.gpe_device,
- dev->wakeup.gpe_number);
acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
dev->wakeup.state.enabled = 1;
Index: linux-2.6/include/acpi/actypes.h
===================================================================
--- linux-2.6.orig/include/acpi/actypes.h
+++ linux-2.6/include/acpi/actypes.h
@@ -668,41 +668,29 @@ typedef u32 acpi_event_status;

/*
* GPE info flags - Per GPE
- * +-+-+-+---+---+-+
- * |7|6|5|4:3|2:1|0|
- * +-+-+-+---+---+-+
- * | | | | | |
- * | | | | | +--- Interrupt type: Edge or Level Triggered
- * | | | | +--- Type: Wake-only, Runtime-only, or wake/runtime
+ * +-+-+-+---+-+-+-+
+ * |7|6|5|4:3|2|1|0|
+ * +-+-+-+---+-+-+-+
+ * | | | | | | |
+ * | | | | | | +--- Interrupt type: Edge or Level Triggered
+ * | | | | | +--- GPE can wake the system
+ * | | | | +--- Unused
* | | | +--- Type of dispatch -- to method, handler, or none
- * | | +--- Enabled for runtime?
- * | +--- Enabled for wake?
+ * | | +--- Unused
+ * | +--- Unused
* +--- Unused
*/
#define ACPI_GPE_XRUPT_TYPE_MASK (u8) 0x01
#define ACPI_GPE_LEVEL_TRIGGERED (u8) 0x01
#define ACPI_GPE_EDGE_TRIGGERED (u8) 0x00

-#define ACPI_GPE_TYPE_MASK (u8) 0x06
-#define ACPI_GPE_TYPE_WAKE_RUN (u8) 0x06
-#define ACPI_GPE_TYPE_WAKE (u8) 0x02
-#define ACPI_GPE_TYPE_RUNTIME (u8) 0x04 /* Default */
+#define ACPI_GPE_CAN_WAKE (u8) 0x02

#define ACPI_GPE_DISPATCH_MASK (u8) 0x18
#define ACPI_GPE_DISPATCH_HANDLER (u8) 0x08
#define ACPI_GPE_DISPATCH_METHOD (u8) 0x10
#define ACPI_GPE_DISPATCH_NOT_USED (u8) 0x00 /* Default */

-#define ACPI_GPE_RUN_ENABLE_MASK (u8) 0x20
-#define ACPI_GPE_RUN_ENABLED (u8) 0x20
-#define ACPI_GPE_RUN_DISABLED (u8) 0x00 /* Default */
-
-#define ACPI_GPE_WAKE_ENABLE_MASK (u8) 0x40
-#define ACPI_GPE_WAKE_ENABLED (u8) 0x40
-#define ACPI_GPE_WAKE_DISABLED (u8) 0x00 /* Default */
-
-#define ACPI_GPE_ENABLE_MASK (u8) 0x60 /* Both run/wake */
-
/*
* Flags for GPE and Lock interfaces
*/

2009-11-29 15:46:42

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 9/12] ACPI / PM: Add more run-time wake-up fields

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

Use the run_wake flag to mark all devices for which run-time wake-up
events may be generated by the platform. Introduce a new wake-up
flag, special, for marking devices that should be permanently enabled
to generate run-time events. Also, introduce a reference counter for
run-wake devices and a function that will initialize all of the
run-time wake-up fields for given device.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/button.c | 2 ++
drivers/acpi/scan.c | 37 +++++++++++++++++++++++++++----------
drivers/acpi/wakeup.c | 2 +-
include/acpi/acpi_bus.h | 2 ++
4 files changed, 32 insertions(+), 11 deletions(-)

Index: linux-2.6/drivers/acpi/wakeup.c
===================================================================
--- linux-2.6.orig/drivers/acpi/wakeup.c
+++ linux-2.6/drivers/acpi/wakeup.c
@@ -106,7 +106,7 @@ int __init acpi_wakeup_device_init(void)
struct acpi_device,
wakeup_list);
/* In case user doesn't load button driver */
- if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
+ if (!dev->wakeup.flags.special || dev->wakeup.state.enabled)
continue;
acpi_ref_wakeup_gpe(dev->wakeup.gpe_device,
dev->wakeup.gpe_number);
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
--- linux-2.6.orig/include/acpi/acpi_bus.h
+++ linux-2.6/include/acpi/acpi_bus.h
@@ -242,6 +242,7 @@ struct acpi_device_perf {
struct acpi_device_wakeup_flags {
u8 valid:1; /* Can successfully enable wakeup? */
u8 run_wake:1; /* Run-Wake GPE devices */
+ u8 special:1; /* Run-wake devices that are always enabled */
};

struct acpi_device_wakeup_state {
@@ -256,6 +257,7 @@ struct acpi_device_wakeup {
struct acpi_device_wakeup_state state;
struct acpi_device_wakeup_flags flags;
int prepare_count;
+ int run_wake_count;
};

/* Device */
Index: linux-2.6/drivers/acpi/button.c
===================================================================
--- linux-2.6.orig/drivers/acpi/button.c
+++ linux-2.6/drivers/acpi/button.c
@@ -419,6 +419,7 @@ static int acpi_button_add(struct acpi_d
device->wakeup.gpe_number);
acpi_ref_wakeup_gpe(device->wakeup.gpe_device,
device->wakeup.gpe_number);
+ device->wakeup.run_wake_count++;
device->wakeup.state.enabled = 1;
}

@@ -443,6 +444,7 @@ static int acpi_button_remove(struct acp
device->wakeup.gpe_number);
acpi_unref_wakeup_gpe(device->wakeup.gpe_device,
device->wakeup.gpe_number);
+ device->wakeup.run_wake_count--;
device->wakeup.state.enabled = 0;
}

Index: linux-2.6/drivers/acpi/scan.c
===================================================================
--- linux-2.6.orig/drivers/acpi/scan.c
+++ linux-2.6/drivers/acpi/scan.c
@@ -742,19 +742,39 @@ acpi_bus_extract_wakeup_device_power_pac
return AE_OK;
}

-static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
+static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
{
- acpi_status status = 0;
- struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
- union acpi_object *package = NULL;
- int psw_error;
-
struct acpi_device_id button_device_ids[] = {
{"PNP0C0D", 0},
{"PNP0C0C", 0},
{"PNP0C0E", 0},
{"", 0},
};
+ acpi_status status;
+ acpi_event_status event_status;
+
+ device->wakeup.run_wake_count = 0;
+
+ /* Power button, Lid switch always enable wakeup */
+ if (!acpi_match_device_ids(device, button_device_ids)) {
+ device->wakeup.flags.run_wake = 1;
+ device->wakeup.flags.special = 1;
+ return;
+ }
+
+ status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
+ ACPI_NOT_ISR, &event_status);
+ if (status == AE_OK)
+ device->wakeup.flags.run_wake =
+ !!(event_status & ACPI_EVENT_FLAG_HANDLE);
+}
+
+static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
+{
+ acpi_status status = 0;
+ struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
+ union acpi_object *package = NULL;
+ int psw_error;

/* _PRW */
status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
@@ -774,6 +794,7 @@ static int acpi_bus_get_wakeup_device_fl

device->wakeup.flags.valid = 1;
device->wakeup.prepare_count = 0;
+ acpi_bus_set_run_wake_flags(device);
/* Call _PSW/_DSW object to disable its ability to wake the sleeping
* system for the ACPI device with the _PRW object.
* The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
@@ -785,10 +806,6 @@ static int acpi_bus_get_wakeup_device_fl
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
"error in _DSW or _PSW evaluation\n"));

- /* Power button, Lid switch always enable wakeup */
- if (!acpi_match_device_ids(device, button_device_ids))
- device->wakeup.flags.run_wake = 1;
-
end:
if (ACPI_FAILURE(status))
device->flags.wake_capable = 0;

2009-11-29 15:46:59

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 10/12] PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 4)

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. Moreover, 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 new PCI platform callback ->test_run_wake() and ->run_wake() to
struct pci_platform_pm_ops allowing us, respectively, to check if
if the platform can generate run-time wake-up events for given
device and to enable/disable the platform to do that. Implemet
these callbacks 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_dev_run_wake() which can be used by PCI drivers to
check if given device is capable of generating wake-up events at
run time.

Developed in cooperation with Matthew Garrett <[email protected]>.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/pci_bind.c | 13 +
drivers/acpi/pci_root.c | 7
drivers/acpi/sleep.c | 22 +-
drivers/pci/hotplug/acpiphp_glue.c | 23 --
drivers/pci/pci-acpi.c | 397 +++++++++++++++++++++++++++++++++++++
drivers/pci/pci.c | 67 ++++++
drivers/pci/pci.h | 12 +
include/acpi/acpi_bus.h | 11 -
include/linux/pci-acpi.h | 24 ++
include/linux/pci.h | 1
kernel/power/Kconfig | 5
11 files changed, 558 insertions(+), 24 deletions(-)

Index: linux-2.6/drivers/pci/pci.h
===================================================================
--- linux-2.6.orig/drivers/pci/pci.h
+++ linux-2.6/drivers/pci/pci.h
@@ -35,6 +35,10 @@ int pci_probe_reset_function(struct pci_
*
* @sleep_wake: enables/disables the system 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,24 @@ 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 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);
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci-acpi.c
+++ linux-2.6/drivers/pci/pci-acpi.c
@@ -16,9 +16,376 @@
#include <acpi/acpi_bus.h>

#include <linux/pci-acpi.h>
+#include <linux/pm_runtime.h>
#include "pci.h"

/*
+ * ACPI-based PCI run-time power management uses ACPI system notify handlers,
+ * which are also used by ACPI-based PCI hotplug. Unfortunately, however, there
+ * can be only one ACPI system notify handler installed for an ACPI device
+ * handle. For this reason there has to be a way to use the same notify handler
+ * for both ACPI-based hotplug and ACPI-based run-time PM.
+ *
+ * pci_acpi_runtime_notifiers is a list of struct pci_acpi_notifier_block
+ * objects representing PCI devices that have ACPI system notify handlers
+ * installed. For each of them, there is an ACPI-based hotplug notifier to
+ * execute for hotplug events, hp_cb, and a pointer to the data to pass to it
+ * hp_data, as well as poitners to a struct pci_bus object and a struct pci_dev
+ * object. If the device is a PCI-to-PCI bridge or root bridge, the struct
+ * pci_bus pointer is not NULL and it is assumed that the whole bus segment
+ * below the bridge has to be walked if a PME is reported for it. Otherwise,
+ * it is assumed that PME is generated only for the particular PCI device
+ * pointed to by the pci_dev field.
+ */
+
+static LIST_HEAD(pci_acpi_runtime_notifiers);
+static DEFINE_MUTEX(pci_acpi_notifier_mtx);
+
+struct pci_acpi_notifier_block
+{
+ struct list_head entry;
+ struct acpi_device *dev;
+ acpi_notify_handler hp_cb;
+ void *hp_data;
+ struct pci_bus *pci_bus;
+ struct pci_dev *pci_dev;
+};
+
+/**
+ * pci_acpi_event_fn - Universal system notification handler.
+ * @handle: ACPI handle of a device the notification is for.
+ * @event: Type of the signaled event.
+ * @data: Context data, should be a pointer to a notifier object.
+ *
+ * Take the address on a notifier object from @data and use it to extract the
+ * information needed for handling the event. If this is a wake-up event,
+ * check if PM notification is enabled for this notifier object and, if so,
+ * execute the appropriate PME handler for the bus or device represented by it.
+ * If this is not a wake-up event, execute the hotplug notify handler for
+ * @handle.
+ */
+static void pci_acpi_event_fn(acpi_handle handle, u32 event, void *data)
+{
+ struct pci_acpi_notifier_block *nb = data;
+
+ if (!nb)
+ return;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ if (event == ACPI_NOTIFY_DEVICE_WAKE) {
+ if (nb->dev->wakeup.run_wake_count > 0) {
+ if (nb->pci_bus)
+ pci_pme_wakeup_bus(nb->pci_bus);
+ if (nb->pci_dev)
+ pci_pme_wakeup(nb->pci_dev);
+ }
+ } else if (nb->hp_cb) {
+ nb->hp_cb(handle, event, nb->hp_data);
+ }
+
+ mutex_unlock(&pci_acpi_notifier_mtx);
+}
+
+/**
+ * new_notifier - Create a new notifier object for given ACPI device.
+ * @dev: Device to create the notifier object for.
+ */
+static struct pci_acpi_notifier_block *new_notifier(struct acpi_device *dev)
+{
+ struct pci_acpi_notifier_block *nb;
+
+ nb = kzalloc(sizeof(*nb), GFP_KERNEL);
+ if (!nb)
+ return NULL;
+
+ nb->dev = dev;
+ return nb;
+}
+
+/**
+ * pci_acpi_add_hp_notifier - Register a hotplug notifier for given device.
+ * @handle: ACPI handle of the device to register the notifier for.
+ * @handler: Callback to execute for hotplug events related to @handle.
+ * @context: Pointer to the context data to pass to @handler.
+ *
+ * Use @handle to get an ACPI device object and check if there is a notifier
+ * object for it. If this is the case, add @handler and @context to the
+ * existing notifier object, unless there already is a hotplug handler in this
+ * notifier object. Otherwise, create a new notifier object for the ACPI device
+ * associated with @handle and add @handler and @context to it.
+ */
+acpi_status pci_acpi_add_hp_notifier(acpi_handle handle,
+ acpi_notify_handler handler, void *context)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_status status = AE_OK;
+
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev)))
+ return AE_NOT_FOUND;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry) {
+ if (nb->dev != dev)
+ continue;
+
+ if (!nb->hp_cb) {
+ nb->hp_cb = handler;
+ nb->hp_data = context;
+ } else {
+ status = AE_ALREADY_EXISTS;
+ }
+ goto out;
+ }
+
+ nb = new_notifier(dev);
+ if (!nb) {
+ status = AE_NO_MEMORY;
+ goto out;
+ }
+ nb->hp_cb = handler;
+ nb->hp_data = context;
+
+ list_add_tail(&nb->entry, &pci_acpi_runtime_notifiers);
+
+ status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn, nb);
+ if (ACPI_FAILURE(status)) {
+ list_del(&nb->entry);
+ kfree(nb);
+ }
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+
+ return status;
+}
+EXPORT_SYMBOL_GPL(pci_acpi_add_hp_notifier);
+
+/**
+ * pci_acpi_remove_hp_notifier - Unregister a hotplug notifier for given device.
+ * @handle: ACPI handle of the device to unregister the notifier for.
+ * @handler: Callback executed for hotplug events related to @handle.
+ *
+ * Find the notifier object associated with @handle and remove the hotplug
+ * callback and the pointer to the hotplug context data from it. If the
+ * notifier object is not necessary any more, remove it altogether.
+ */
+acpi_status pci_acpi_remove_hp_notifier(acpi_handle handle,
+ acpi_notify_handler handler)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_status status = AE_NOT_FOUND;
+
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev)))
+ return AE_NOT_FOUND;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ status = AE_OK;
+ break;
+ }
+
+ if (status != AE_OK)
+ goto out;
+
+ nb->hp_data = NULL;
+ nb->hp_cb = NULL;
+
+ if (nb->pci_bus || nb->pci_dev)
+ goto out;
+
+ status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn);
+ list_del(&nb->entry);
+ kfree(nb);
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+EXPORT_SYMBOL_GPL(pci_acpi_remove_hp_notifier);
+
+/**
+ * pci_acpi_add_pm_notifier - Register PM notifier for given device.
+ * @dev: ACPI device to add the notifier for.
+ * @pci_dev: PCI device to check for the PME status if an event is signaled.
+ * @pci_bus: PCI bus to walk (checking PME status) if an event is signaled.
+ *
+ * Check if there is a notifier object for @dev and if that is the case, add
+ * @pci_dev to it as the device whose PME status should be checked if a PM
+ * event is signaled for @dev. Also, add @pci_bus to it as the bus to walk
+ * checking the PME status of all devices on it if a PM event is signaled for
+ * @dev. Otherwise, create a new notifier object for @dev and add both
+ * @pci_dev and @pci_bus to it.
+ */
+acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev,
+ struct pci_bus *pci_bus)
+{
+ struct pci_acpi_notifier_block *nb;
+ acpi_status status = AE_OK;
+
+ if (!dev->wakeup.flags.run_wake)
+ return AE_BAD_PARAMETER;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ if (nb->pci_dev || nb->pci_bus)
+ goto out;
+ else
+ goto add;
+ }
+
+ nb = new_notifier(dev);
+ if (!nb) {
+ status = AE_NO_MEMORY;
+ goto out;
+ }
+ list_add_tail(&nb->entry, &pci_acpi_runtime_notifiers);
+
+ status = acpi_install_notify_handler(dev->handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn, nb);
+ if (ACPI_FAILURE(status)) {
+ list_del(&nb->entry);
+ kfree(nb);
+ goto out;
+ }
+
+ add:
+ nb->pci_dev = pci_dev;
+ nb->pci_bus = pci_bus;
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+
+/**
+ * pci_acpi_remove_pm_notifier - Unregister PM notifier for given device.
+ * @dev: ACPI device to remove the notifier from.
+ *
+ * Find the notifier object for @dev and clear its @pci_dev and @pci_bus fields.
+ * If the notifier object is not necessary any more after that, remove it too.
+ */
+acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev)
+{
+ struct pci_acpi_notifier_block *nb;
+ acpi_status status = AE_NOT_FOUND;
+
+ if (!dev->wakeup.flags.run_wake)
+ return AE_BAD_PARAMETER;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ status = AE_OK;
+ break;
+ }
+
+ if (status != AE_OK)
+ goto out;
+
+ if (dev->wakeup.run_wake_count) {
+ dev->wakeup.run_wake_count = 0;
+ acpi_unref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ acpi_pm_wake_up_power(dev, false);
+ }
+
+ nb->pci_dev = NULL;
+ nb->pci_bus = NULL;
+
+ if (nb->hp_cb)
+ goto out;
+
+ status = acpi_remove_notify_handler(nb->dev->handle,
+ ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn);
+ list_del(&nb->entry);
+ kfree(nb);
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+
+/**
+ * run_wake_enable - Enable/disable the GPE associated with given notifier.
+ * @nb: Notifier to enable/disable the GPE for.
+ * @enable: Whether to enable or disable the wake-up feature.
+ */
+static int run_wake_enable(struct pci_acpi_notifier_block *nb, bool enable)
+{
+ struct acpi_device *dev = nb->dev;
+ int error = 0;
+
+ if (enable) {
+ if (!dev->wakeup.run_wake_count++) {
+ acpi_pm_wake_up_power(dev, true);
+ acpi_ref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ }
+ } else if (dev->wakeup.run_wake_count > 0) {
+ if (!--dev->wakeup.run_wake_count) {
+ acpi_unref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ acpi_pm_wake_up_power(dev, false);
+ }
+ } else {
+ error = -EALREADY;
+ }
+
+ return error;
+}
+
+/**
+ * acpi_dev_run_wake_enable - Enable/disable wake-up for given device.
+ * @phys_dev: Device to enable/disable the platform to wake-up the system for.
+ * @enable: Whether enable or disable the wake-up functionality.
+ *
+ * Find the notifier object corresponding to @pci_dev and try to enable/disable
+ * the GPE associated with it.
+ */
+static int acpi_dev_run_wake_enable(struct device *phys_dev, bool enable)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_handle handle;
+ int error = -ENODEV;
+
+ if (!device_run_wake(phys_dev))
+ return -EINVAL;
+
+ handle = DEVICE_ACPI_HANDLE(phys_dev);
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
+ dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
+ __func__);
+ return -ENODEV;
+ }
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ error = run_wake_enable(nb, enable);
+ break;
+ }
+
+ mutex_unlock(&pci_acpi_notifier_mtx);
+
+ return error;
+}
+
+/*
* _SxD returns the D-state with the highest power
* (lowest D-state number) supported in the S-state "x".
*
@@ -131,12 +498,42 @@ static int acpi_pci_sleep_wake(struct pc
return 0;
}

+static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
+{
+ while (bus->parent) {
+ struct pci_dev *bridge = bus->self;
+
+ if (bridge->pme_interrupt)
+ return;
+ if (!acpi_dev_run_wake_enable(&bridge->dev, enable))
+ return;
+ bus = bus->parent;
+ }
+
+ /* We have reached the root bus. */
+ if (bus->bridge)
+ acpi_dev_run_wake_enable(bus->bridge, enable);
+}
+
+static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
+{
+ if (dev->pme_interrupt)
+ return 0;
+
+ if (!acpi_dev_run_wake_enable(&dev->dev, enable))
+ return 0;
+
+ acpi_pci_propagate_run_wake(dev->bus, enable);
+ return 0;
+}
+
static struct pci_platform_pm_ops acpi_pci_platform_pm = {
.is_manageable = acpi_pci_power_manageable,
.set_state = acpi_pci_set_power_state,
.choose_state = acpi_pci_choose_state,
.can_wakeup = acpi_pci_can_wakeup,
.sleep_wake = acpi_pci_sleep_wake,
+ .run_wake = acpi_pci_run_wake,
};

/* ACPI bus type */
Index: linux-2.6/drivers/pci/pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci.c
+++ 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;
}

+static inline int platform_pci_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#.
@@ -1406,6 +1438,41 @@ int pci_back_from_sleep(struct pci_dev *
}

/**
+ * pci_dev_run_wake - Check if device can generate run-time wake-up events.
+ * @dev: Device to check.
+ *
+ * Return true if the device itself is cabable of generating wake-up events
+ * (through the platform or using the native PCIe PME) or if the device supports
+ * PME and one of its upstream bridges can generate wake-up events.
+ */
+bool pci_dev_run_wake(struct pci_dev *dev)
+{
+ struct pci_bus *bus = dev->bus;
+
+ if (device_run_wake(&dev->dev))
+ return true;
+
+ if (!dev->pme_support)
+ return false;
+
+ while (bus->parent) {
+ struct pci_dev *bridge = bus->self;
+
+ if (device_run_wake(&bridge->dev))
+ return true;
+
+ bus = bus->parent;
+ }
+
+ /* We have reached the root bus. */
+ if (bus->bridge)
+ return device_run_wake(bus->bridge);
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(pci_dev_run_wake);
+
+/**
* pci_pm_init - Initialize PM functions of given PCI device
* @dev: PCI device to handle.
*/
Index: linux-2.6/kernel/power/Kconfig
===================================================================
--- linux-2.6.orig/kernel/power/Kconfig
+++ 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
Index: linux-2.6/drivers/acpi/sleep.c
===================================================================
--- linux-2.6.orig/drivers/acpi/sleep.c
+++ linux-2.6/drivers/acpi/sleep.c
@@ -626,7 +626,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
@@ -712,6 +712,18 @@ int acpi_pm_device_sleep_state(struct de
}

/**
+ * acpi_pm_wake_up_power - Enable/disable device wake-up power.
+ * @dev: ACPI device to handle.
+ * @enable: Whether to enable or disable the wake-up power of the device.
+ */
+int acpi_pm_wake_up_power(struct acpi_device *dev, bool enable)
+{
+ return enable ?
+ acpi_enable_wakeup_device_power(dev, acpi_target_sleep_state) :
+ acpi_disable_wakeup_device_power(dev);
+}
+
+/**
* acpi_pm_device_sleep_wake - enable or disable the system wake-up
* capability of given device
* @dev: device to handle
@@ -732,16 +744,14 @@ int acpi_pm_device_sleep_wake(struct dev
return -ENODEV;
}

- error = enable ?
- acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
- acpi_disable_wakeup_device_power(adev);
+ error = acpi_pm_wake_up_power(adev, enable);
if (!error)
- dev_info(dev, "wake-up capability %s by ACPI\n",
+ dev_info(dev, "wake-up power %s by ACPI\n",
enable ? "enabled" : "disabled");

return error;
}
-#endif
+#endif /* CONFIG_PM_WAKEUP */

static void acpi_power_off_prepare(void)
{
Index: linux-2.6/drivers/acpi/pci_root.c
===================================================================
--- linux-2.6.orig/drivers/acpi/pci_root.c
+++ linux-2.6/drivers/acpi/pci_root.c
@@ -30,6 +30,7 @@
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/pm.h>
+#include <linux/pm_runtime.h>
#include <linux/pci.h>
#include <linux/pci-acpi.h>
#include <linux/acpi.h>
@@ -576,6 +577,9 @@ static int __devinit acpi_pci_root_add(s
if (flags != base_flags)
acpi_pci_osc_support(root, flags);

+ if (!pci_acpi_add_bus_pm_notifier(device, root->bus))
+ device_set_run_wake(root->bus->bridge, true);
+
return 0;

end:
@@ -597,6 +601,9 @@ static int acpi_pci_root_remove(struct a
{
struct acpi_pci_root *root = acpi_driver_data(device);

+ pci_acpi_remove_pm_notifier(device);
+ device_set_run_wake(root->bus->bridge, false);
+
kfree(root);
return 0;
}
Index: linux-2.6/include/linux/pci-acpi.h
===================================================================
--- linux-2.6.orig/include/linux/pci-acpi.h
+++ linux-2.6/include/linux/pci-acpi.h
@@ -11,6 +11,30 @@
#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_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev,
+ struct pci_bus *pci_bus);
+extern acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev);
+
+static inline
+acpi_status pci_acpi_add_device_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev)
+{
+ return pci_acpi_add_pm_notifier(dev, pci_dev, pci_dev->subordinate);
+}
+
+static inline
+acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev,
+ struct pci_bus *pci_bus)
+{
+ return pci_acpi_add_pm_notifier(dev, NULL, pci_bus);
+}
+
static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
{
struct pci_bus *pbus = pdev->bus;
Index: linux-2.6/drivers/acpi/pci_bind.c
===================================================================
--- linux-2.6.orig/drivers/acpi/pci_bind.c
+++ linux-2.6/drivers/acpi/pci_bind.c
@@ -26,7 +26,9 @@
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
+#include <linux/pci-acpi.h>
#include <linux/acpi.h>
+#include <linux/pm_runtime.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

@@ -38,7 +40,13 @@ 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;
+
+ pci_acpi_remove_pm_notifier(device);
+ device_set_run_wake(&dev->dev, false);
+
+ if (!dev->subordinate)
goto out;

acpi_pci_irq_del_prt(dev->subordinate);
@@ -62,6 +70,9 @@ static int acpi_pci_bind(struct acpi_dev
if (!dev)
return 0;

+ if (!pci_acpi_add_device_pm_notifier(device, dev))
+ device_set_run_wake(&dev->dev, true);
+
/*
* Install the 'bind' function to facilitate callbacks for
* children of the P2P bridge.
Index: linux-2.6/drivers/pci/hotplug/acpiphp_glue.c
===================================================================
--- linux-2.6.orig/drivers/pci/hotplug/acpiphp_glue.c
+++ 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 ;
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -743,6 +743,7 @@ int pci_wake_from_d3(struct pci_dev *dev
pci_power_t pci_target_state(struct pci_dev *dev);
int pci_prepare_to_sleep(struct pci_dev *dev);
int pci_back_from_sleep(struct pci_dev *dev);
+bool pci_dev_run_wake(struct pci_dev *dev);

/* Functions for PCI Hotplug drivers to use */
int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap);
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
--- linux-2.6.orig/include/acpi/acpi_bus.h
+++ linux-2.6/include/acpi/acpi_bus.h
@@ -388,21 +388,26 @@ 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_wake_up_power(struct acpi_device *, bool);
int acpi_pm_device_sleep_wake(struct device *, bool);
-#else /* !CONFIG_PM_SLEEP */
+#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_wake_up_power(struct acpi_device *dev, bool enable)
+{
+ return -ENODEV;
+}
static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
{
return -ENODEV;
}
-#endif /* !CONFIG_PM_SLEEP */
+#endif /* !CONFIG_PM_WAKEUP */

#endif /* CONFIG_ACPI */

2009-11-29 15:46:39

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 11/12] PCI PM: Run-time callbacks for PCI bus type (rev. 2)

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.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/pci/pci-driver.c | 105 ++++++++++++++++++++++++++++++++++++++++++++---
drivers/pci/pci.c | 43 ++++++++++++++++---
drivers/pci/pci.h | 1
include/linux/pci.h | 9 +++-
kernel/power/Kconfig | 5 ++
5 files changed, 151 insertions(+), 12 deletions(-)

Index: linux-2.6/kernel/power/Kconfig
===================================================================
--- linux-2.6.orig/kernel/power/Kconfig
+++ 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
Index: linux-2.6/drivers/pci/pci-driver.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci-driver.c
+++ linux-2.6/drivers/pci/pci-driver.c
@@ -17,6 +17,7 @@
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/cpu.h>
+#include <linux/pm_runtime.h>
#include "pci.h"

struct pci_dynid {
@@ -537,7 +538,7 @@ static int pci_restore_standard_config(s
return pci_restore_state(pci_dev);
}

-static void pci_pm_default_resume_noirq(struct pci_dev *pci_dev)
+static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
{
pci_restore_standard_config(pci_dev);
pci_fixup_device(pci_fixup_resume_early, pci_dev);
@@ -581,6 +582,17 @@ static int pci_pm_prepare(struct device
struct device_driver *drv = dev->driver;
int error = 0;

+ /*
+ * PCI devices suspended at run time need to be resumed at this
+ * point, because in general it is necessary to reconfigure them for
+ * system suspend. Namely, if the device is supposed to wake up the
+ * system from the sleep state, we may need to reconfigure it for this
+ * purpose. In turn, if the device is not supposed to wake up the
+ * system from the sleep state, we'll have to prevent it from signaling
+ * wake-up.
+ */
+ pm_runtime_resume(dev);
+
if (drv && drv->pm && drv->pm->prepare)
error = drv->pm->prepare(dev);

@@ -681,7 +693,7 @@ static int pci_pm_resume_noirq(struct de
struct device_driver *drv = dev->driver;
int error = 0;

- pci_pm_default_resume_noirq(pci_dev);
+ pci_pm_default_resume_early(pci_dev);

if (pci_has_legacy_pm_support(pci_dev))
return pci_legacy_resume_early(dev);
@@ -879,7 +891,7 @@ static int pci_pm_restore_noirq(struct d
struct device_driver *drv = dev->driver;
int error = 0;

- pci_pm_default_resume_noirq(pci_dev);
+ pci_pm_default_resume_early(pci_dev);

if (pci_has_legacy_pm_support(pci_dev))
return pci_legacy_resume_early(dev);
@@ -931,6 +943,86 @@ static int pci_pm_restore(struct device

#endif /* !CONFIG_HIBERNATION */

+#endif /* !CONFIG_PM_SLEEP */
+
+#ifdef CONFIG_PM_RUNTIME
+
+static int pci_pm_runtime_suspend(struct device *dev)
+{
+ struct pci_dev *pci_dev = to_pci_dev(dev);
+ const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
+ pci_power_t prev = pci_dev->current_state;
+ int error;
+
+ if (!pm || !pm->runtime_suspend)
+ return -ENOSYS;
+
+ error = pm->runtime_suspend(dev);
+ suspend_report_result(pm->runtime_suspend, error);
+ if (error)
+ return error;
+
+ pci_fixup_device(pci_fixup_suspend, pci_dev);
+
+ if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
+ && pci_dev->current_state != PCI_UNKNOWN) {
+ WARN_ONCE(pci_dev->current_state != prev,
+ "PCI PM: State of device not saved by %pF\n",
+ pm->runtime_suspend);
+ return 0;
+ }
+
+ if (!pci_dev->state_saved)
+ pci_save_state(pci_dev);
+
+ pci_finish_runtime_suspend(pci_dev);
+
+ return 0;
+}
+
+static int pci_pm_runtime_resume(struct device *dev)
+{
+ struct pci_dev *pci_dev = to_pci_dev(dev);
+ const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
+
+ if (!pm || !pm->runtime_resume)
+ return -ENOSYS;
+
+ pci_pm_default_resume_early(pci_dev);
+ __pci_enable_wake(pci_dev, PCI_D0, true, false);
+ pci_fixup_device(pci_fixup_resume, pci_dev);
+
+ return pm->runtime_resume(dev);
+}
+
+static int pci_pm_runtime_idle(struct device *dev)
+{
+ const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
+
+ if (!pm)
+ return -ENOSYS;
+
+ if (pm->runtime_idle) {
+ int ret = pm->runtime_idle(dev);
+ if (ret)
+ return ret;
+ }
+
+ pm_runtime_suspend(dev);
+
+ return 0;
+}
+
+#else /* !CONFIG_PM_RUNTIME */
+
+#define pci_pm_runtime_suspend NULL
+#define pci_pm_runtime_resume NULL
+#define pci_pm_runtime_idle NULL
+
+#endif /* !CONFIG_PM_RUNTIME */
+
+#ifdef CONFIG_PM_OPS
+
const struct dev_pm_ops pci_dev_pm_ops = {
.prepare = pci_pm_prepare,
.complete = pci_pm_complete,
@@ -946,15 +1038,18 @@ const struct dev_pm_ops pci_dev_pm_ops =
.thaw_noirq = pci_pm_thaw_noirq,
.poweroff_noirq = pci_pm_poweroff_noirq,
.restore_noirq = pci_pm_restore_noirq,
+ .runtime_suspend = pci_pm_runtime_suspend,
+ .runtime_resume = pci_pm_runtime_resume,
+ .runtime_idle = pci_pm_runtime_idle,
};

#define PCI_PM_OPS_PTR (&pci_dev_pm_ops)

-#else /* !CONFIG_PM_SLEEP */
+#else /* !COMFIG_PM_OPS */

#define PCI_PM_OPS_PTR NULL

-#endif /* !CONFIG_PM_SLEEP */
+#endif /* !COMFIG_PM_OPS */

/**
* __pci_register_driver - register a new pci driver
Index: linux-2.6/drivers/pci/pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci.c
+++ linux-2.6/drivers/pci/pci.c
@@ -1274,9 +1274,10 @@ void pci_pme_active(struct pci_dev *dev,
}

/**
- * pci_enable_wake - enable PCI device as wakeup event source
+ * __pci_enable_wake - enable PCI device as wakeup event source
* @dev: PCI device affected
* @state: PCI state from which device will issue wakeup events
+ * @runtime: True if the events are to be generated at run time
* @enable: True to enable event generation; false to disable
*
* This enables the device as a wakeup event source, or disables it.
@@ -1292,11 +1293,12 @@ void pci_pme_active(struct pci_dev *dev,
* Error code depending on the platform is returned if both the platform and
* the native mechanism fail to enable the generation of wake-up events
*/
-int pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
+int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
+ bool runtime, bool enable)
{
int ret = 0;

- if (enable && !device_may_wakeup(&dev->dev))
+ if (enable && runtime && !device_may_wakeup(&dev->dev))
return -EINVAL;

/* Don't do the same thing twice in a row for one device. */
@@ -1316,19 +1318,24 @@ int pci_enable_wake(struct pci_dev *dev,
pci_pme_active(dev, true);
else
ret = 1;
- error = platform_pci_sleep_wake(dev, true);
+ error = runtime ? platform_pci_run_wake(dev, true) :
+ platform_pci_sleep_wake(dev, true);
if (ret)
ret = error;
if (!ret)
dev->wakeup_prepared = true;
} else {
- platform_pci_sleep_wake(dev, false);
+ if (runtime)
+ platform_pci_run_wake(dev, false);
+ else
+ platform_pci_sleep_wake(dev, false);
pci_pme_active(dev, false);
dev->wakeup_prepared = false;
}

return ret;
}
+EXPORT_SYMBOL(__pci_enable_wake);

/**
* pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
@@ -1438,6 +1445,31 @@ int pci_back_from_sleep(struct pci_dev *
}

/**
+ * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
+ * @dev: PCI device being suspended.
+ *
+ * Prepare @dev to generate wake-up events at run time and put it into a low
+ * power state.
+ */
+int pci_finish_runtime_suspend(struct pci_dev *dev)
+{
+ pci_power_t target_state = pci_target_state(dev);
+ int error;
+
+ if (target_state == PCI_POWER_ERROR)
+ return -EIO;
+
+ __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
+
+ error = pci_set_power_state(dev, target_state);
+
+ if (error)
+ __pci_enable_wake(dev, target_state, true, false);
+
+ return error;
+}
+
+/**
* pci_dev_run_wake - Check if device can generate run-time wake-up events.
* @dev: Device to check.
*
@@ -2898,7 +2930,6 @@ EXPORT_SYMBOL(pci_save_state);
EXPORT_SYMBOL(pci_restore_state);
EXPORT_SYMBOL(pci_pme_capable);
EXPORT_SYMBOL(pci_pme_active);
-EXPORT_SYMBOL(pci_enable_wake);
EXPORT_SYMBOL(pci_wake_from_d3);
EXPORT_SYMBOL(pci_target_state);
EXPORT_SYMBOL(pci_prepare_to_sleep);
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -738,13 +738,20 @@ int pci_set_power_state(struct pci_dev *
pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state);
bool pci_pme_capable(struct pci_dev *dev, pci_power_t state);
void pci_pme_active(struct pci_dev *dev, bool enable);
-int pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable);
+int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
+ bool runtime, bool enable);
int pci_wake_from_d3(struct pci_dev *dev, bool enable);
pci_power_t pci_target_state(struct pci_dev *dev);
int pci_prepare_to_sleep(struct pci_dev *dev);
int pci_back_from_sleep(struct pci_dev *dev);
bool pci_dev_run_wake(struct pci_dev *dev);

+static inline int pci_enable_wake(struct pci_dev *dev, pci_power_t state,
+ bool enable)
+{
+ return __pci_enable_wake(dev, state, false, enable);
+}
+
/* Functions for PCI Hotplug drivers to use */
int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap);
#ifdef CONFIG_HOTPLUG
Index: linux-2.6/drivers/pci/pci.h
===================================================================
--- linux-2.6.orig/drivers/pci/pci.h
+++ linux-2.6/drivers/pci/pci.h
@@ -55,6 +55,7 @@ extern int pci_set_platform_pm(struct pc
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_finish_runtime_suspend(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);

2009-11-29 15:47:42

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

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

Use the PCI run-time power management framework to add simplified
run-time PM support to the r8169 driver. Namely, make the driver
suspend the device when the link is off and set it up for generating
wake-up event after the link has been detected again.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/net/r8169.c | 133 +++++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 106 insertions(+), 27 deletions(-)

Index: linux-2.6/drivers/net/r8169.c
===================================================================
--- linux-2.6.orig/drivers/net/r8169.c
+++ linux-2.6/drivers/net/r8169.c
@@ -23,6 +23,7 @@
#include <linux/tcp.h>
#include <linux/init.h>
#include <linux/dma-mapping.h>
+#include <linux/pm_runtime.h>

#include <asm/system.h>
#include <asm/io.h>
@@ -504,6 +505,8 @@ struct rtl8169_private {

struct mii_if_info mii;
struct rtl8169_counters counters;
+ u32 saved_wolopts;
+ bool exiting;
};

MODULE_AUTHOR("Realtek and the Linux r8169 crew <[email protected]>");
@@ -750,48 +753,54 @@ static void rtl8169_check_link_status(st
if (netif_msg_ifdown(tp))
printk(KERN_INFO PFX "%s: link down\n", dev->name);
netif_carrier_off(dev);
+ pm_schedule_suspend(&tp->pci_dev->dev, 100);
}
spin_unlock_irqrestore(&tp->lock, flags);
}

-static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
+#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
+
+static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
{
- struct rtl8169_private *tp = netdev_priv(dev);
void __iomem *ioaddr = tp->mmio_addr;
u8 options;
-
- wol->wolopts = 0;
-
-#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
- wol->supported = WAKE_ANY;
-
- spin_lock_irq(&tp->lock);
+ u32 wolopts = 0;

options = RTL_R8(Config1);
if (!(options & PMEnable))
- goto out_unlock;
+ return 0;

options = RTL_R8(Config3);
if (options & LinkUp)
- wol->wolopts |= WAKE_PHY;
+ wolopts |= WAKE_PHY;
if (options & MagicPacket)
- wol->wolopts |= WAKE_MAGIC;
+ wolopts |= WAKE_MAGIC;

options = RTL_R8(Config5);
if (options & UWF)
- wol->wolopts |= WAKE_UCAST;
+ wolopts |= WAKE_UCAST;
if (options & BWF)
- wol->wolopts |= WAKE_BCAST;
+ wolopts |= WAKE_BCAST;
if (options & MWF)
- wol->wolopts |= WAKE_MCAST;
+ wolopts |= WAKE_MCAST;

-out_unlock:
- spin_unlock_irq(&tp->lock);
+ return wolopts;
}

-static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
+static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
struct rtl8169_private *tp = netdev_priv(dev);
+
+ spin_lock_irq(&tp->lock);
+
+ wol->supported = WAKE_ANY;
+ wol->wolopts = __rtl8169_get_wol(tp);
+
+ spin_unlock_irq(&tp->lock);
+}
+
+static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
+{
void __iomem *ioaddr = tp->mmio_addr;
unsigned int i;
static struct {
@@ -808,23 +817,29 @@ static int rtl8169_set_wol(struct net_de
{ WAKE_ANY, Config5, LanWake }
};

- spin_lock_irq(&tp->lock);
-
RTL_W8(Cfg9346, Cfg9346_Unlock);

for (i = 0; i < ARRAY_SIZE(cfg); i++) {
u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
- if (wol->wolopts & cfg[i].opt)
+ if (wolopts & cfg[i].opt)
options |= cfg[i].mask;
RTL_W8(cfg[i].reg, options);
}

RTL_W8(Cfg9346, Cfg9346_Lock);
+}
+
+static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
+{
+ struct rtl8169_private *tp = netdev_priv(dev);
+
+ spin_lock_irq(&tp->lock);

if (wol->wolopts)
tp->features |= RTL_FEATURE_WOL;
else
tp->features &= ~RTL_FEATURE_WOL;
+ __rtl8169_set_wol(tp, wol->wolopts);
device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);

spin_unlock_irq(&tp->lock);
@@ -3000,6 +3015,7 @@ rtl8169_init_one(struct pci_dev *pdev, c
tp->dev = dev;
tp->pci_dev = pdev;
tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
+ tp->exiting = false;

mii = &tp->mii;
mii->dev = dev;
@@ -3232,6 +3248,8 @@ static void __devexit rtl8169_remove_one
struct net_device *dev = pci_get_drvdata(pdev);
struct rtl8169_private *tp = netdev_priv(dev);

+ tp->exiting = true;
+
flush_scheduled_work();

unregister_netdev(dev);
@@ -3254,7 +3272,6 @@ static int rtl8169_open(struct net_devic
struct pci_dev *pdev = tp->pci_dev;
int retval = -ENOMEM;

-
rtl8169_set_rxbufsize(tp, dev);

/*
@@ -3291,6 +3308,13 @@ static int rtl8169_open(struct net_devic

rtl8169_request_timer(dev);

+ tp->saved_wolopts = 0;
+
+ if (pci_dev_run_wake(pdev)) {
+ pm_runtime_set_active(&pdev->dev);
+ pm_runtime_enable(&pdev->dev);
+ }
+
rtl8169_check_link_status(dev, tp, tp->mmio_addr);
out:
return retval;
@@ -4724,6 +4748,14 @@ static int rtl8169_close(struct net_devi
struct rtl8169_private *tp = netdev_priv(dev);
struct pci_dev *pdev = tp->pci_dev;

+ if (pci_dev_run_wake(pdev)) {
+ pm_runtime_get_noresume(&pdev->dev);
+ pm_runtime_resume(&pdev->dev);
+ pm_runtime_disable(&pdev->dev);
+ pm_runtime_set_suspended(&pdev->dev);
+ pm_runtime_put_noidle(&pdev->dev);
+ }
+
/* update counters before going down */
rtl8169_update_counters(dev);

@@ -4841,21 +4873,65 @@ static int rtl8169_suspend(struct device
return 0;
}

+static void __rtl8169_resume(struct net_device *dev)
+{
+ netif_device_attach(dev);
+ rtl8169_schedule_work(dev, rtl8169_reset_task);
+}
+
static int rtl8169_resume(struct device *device)
{
struct pci_dev *pdev = to_pci_dev(device);
struct net_device *dev = pci_get_drvdata(pdev);

- if (!netif_running(dev))
- goto out;
+ if (netif_running(dev))
+ __rtl8169_resume(dev);

- netif_device_attach(dev);
+ return 0;
+}
+
+static int rtl8169_runtime_suspend(struct device *device)
+{
+ struct pci_dev *pdev = to_pci_dev(device);
+ struct net_device *dev = pci_get_drvdata(pdev);
+ struct rtl8169_private *tp = netdev_priv(dev);
+
+ dev_dbg(&pdev->dev, "suspending\n");
+
+ spin_lock_irq(&tp->lock);
+ tp->saved_wolopts = __rtl8169_get_wol(tp);
+ __rtl8169_set_wol(tp, WAKE_ANY);
+ spin_unlock_irq(&tp->lock);
+
+ rtl8169_net_suspend(dev);
+
+ return 0;
+}
+
+static int rtl8169_runtime_resume(struct device *device)
+{
+ struct pci_dev *pdev = to_pci_dev(device);
+ struct net_device *dev = pci_get_drvdata(pdev);
+ struct rtl8169_private *tp = netdev_priv(dev);
+
+ dev_dbg(&pdev->dev, "resuming\n");
+
+ spin_lock_irq(&tp->lock);
+ __rtl8169_set_wol(tp, tp->saved_wolopts);
+ tp->saved_wolopts = 0;
+ spin_unlock_irq(&tp->lock);
+
+ if (!tp->exiting)
+ __rtl8169_resume(dev);

- rtl8169_schedule_work(dev, rtl8169_reset_task);
-out:
return 0;
}

+static int rtl8169_runtime_idle(struct device *device)
+{
+ return -EBUSY;
+}
+
static struct dev_pm_ops rtl8169_pm_ops = {
.suspend = rtl8169_suspend,
.resume = rtl8169_resume,
@@ -4863,6 +4939,9 @@ static struct dev_pm_ops rtl8169_pm_ops
.thaw = rtl8169_resume,
.poweroff = rtl8169_suspend,
.restore = rtl8169_resume,
+ .runtime_suspend = rtl8169_runtime_suspend,
+ .runtime_resume = rtl8169_runtime_resume,
+ .runtime_idle = rtl8169_runtime_idle,
};

#define RTL8169_PM_OPS (&rtl8169_pm_ops)

2009-12-01 22:01:06

by Matthew Garrett

[permalink] [raw]
Subject: Re: [RFC][PATCH 11/12] PCI PM: Run-time callbacks for PCI bus type (rev. 2)

On Sun, Nov 29, 2009 at 04:43:15PM +0100, Rafael J. Wysocki wrote:

> - if (enable && !device_may_wakeup(&dev->dev))
> + if (enable && runtime && !device_may_wakeup(&dev->dev))

Should this be !runtime? We only care about device_may_wakeup() in the
system suspend case.

--
Matthew Garrett | [email protected]

2009-12-01 22:49:40

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 11/12] PCI PM: Run-time callbacks for PCI bus type (rev. 2)

On Tuesday 01 December 2009, Matthew Garrett wrote:
> On Sun, Nov 29, 2009 at 04:43:15PM +0100, Rafael J. Wysocki wrote:
>
> > - if (enable && !device_may_wakeup(&dev->dev))
> > + if (enable && runtime && !device_may_wakeup(&dev->dev))
>
> Should this be !runtime? We only care about device_may_wakeup() in the
> system suspend case.

Yes, it should, thanks!

2009-12-04 16:21:35

by Matthew Garrett

[permalink] [raw]
Subject: Re: [RFC][PATCH 10/12] PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 4)

On Sun, Nov 29, 2009 at 04:42:20PM +0100, Rafael J. Wysocki wrote:

> + if (event == ACPI_NOTIFY_DEVICE_WAKE) {
> + if (nb->dev->wakeup.run_wake_count > 0) {
> + if (nb->pci_bus)
> + pci_pme_wakeup_bus(nb->pci_bus);
> + if (nb->pci_dev)
> + pci_pme_wakeup(nb->pci_dev);

We may receive wakeup events on devices that aren't PME capable, which
is the case for uhci on my test box. In that case we probably want to
wake them up unconditionally.

+ if (nb->pci_dev) {
+ if (nb->pci_dev->pm_cap)
+ pci_pme_wakeup(nb->pci_dev);
+ else
+ pm_request_resume(&nb->pci_dev->dev);

seems to work, though possibly we should assume that the firmware knows
best and always schedule a wake in respose to a resume request?

--
Matthew Garrett | [email protected]

2009-12-04 21:14:42

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 10/12] PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 4)

On Friday 04 December 2009, Matthew Garrett wrote:
> On Sun, Nov 29, 2009 at 04:42:20PM +0100, Rafael J. Wysocki wrote:
>
> > + if (event == ACPI_NOTIFY_DEVICE_WAKE) {
> > + if (nb->dev->wakeup.run_wake_count > 0) {
> > + if (nb->pci_bus)
> > + pci_pme_wakeup_bus(nb->pci_bus);
> > + if (nb->pci_dev)
> > + pci_pme_wakeup(nb->pci_dev);
>
> We may receive wakeup events on devices that aren't PME capable, which
> is the case for uhci on my test box. In that case we probably want to
> wake them up unconditionally.
>
> + if (nb->pci_dev) {
> + if (nb->pci_dev->pm_cap)
> + pci_pme_wakeup(nb->pci_dev);
> + else
> + pm_request_resume(&nb->pci_dev->dev);
>
> seems to work, though possibly we should assume that the firmware knows
> best and always schedule a wake in respose to a resume request?

I think we can simply do:

+ if (nb->pci_dev) {
+ pci_pme_wakeup(nb->pci_dev);
+ pm_request_resume(&nb->pci_dev->dev);
+ }

If the pci_pme_wakeup() spawns a resume request, the other call will just
return.

Thanks,
Rafael

2009-12-04 21:16:55

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 5/12] PCI PM: Make it possible to force using INTx for PCIe PME signaling (updated)

On Sunday 29 November 2009, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <[email protected]>
>
> Apparently, some machines may have problems with PCI run-time power
> management if MSIs are used for the hative PCIe PME signaling. In
> particular, on the MSI Wind U-100 PCIe PME interrupts are not
> generated by a PCIe root port after a resume from suspend to RAM, if
> the system wake-up was triggered by a PME from the device attached to
> this port. [It doesn't help to free the interrupt on suspend and
> request it back on resume, even if that is done along with disabling
> the MSI and re-enabling it, respectively.] However, if INTx
> interrupts are used for this purpose on the same machine, everything
> works just fine.
>
> For this reason, add a kernel command line switch allowing one to
> request that MSIs be not used for the native PCIe PME signaling,
> introduce a DMI table allowing us to blacklist machines that need
> this switch to be set by default and put the MSI Wind U-100 into this
> table.

The DMI table has to be terminated appropriately. Fixed version is appended.

---
From: Rafael J. Wysocki <[email protected]>
Subject: PCI PM: Make it possible to force using INTx for PCIe PME signaling

Apparently, some machines may have problems with PCI run-time power
management if MSIs are used for the hative PCIe PME signaling. In
particular, on the MSI Wind U-100 PCIe PME interrupts are not
generated by a PCIe root port after a resume from suspend to RAM, if
the system wake-up was triggered by a PME from the device attached to
this port. [It doesn't help to free the interrupt on suspend and
request it back on resume, even if that is done along with disabling
the MSI and re-enabling it, respectively.] However, if INTx
interrupts are used for this purpose on the same machine, everything
works just fine.

For this reason, add a kernel command line switch allowing one to
request that MSIs be not used for the native PCIe PME signaling,
introduce a DMI table allowing us to blacklist machines that need
this switch to be set by default and put the MSI Wind U-100 into this
table.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
Documentation/kernel-parameters.txt | 2 ++
drivers/pci/pcie/pme/pcie_pme.c | 14 +++++++++++++-
drivers/pci/pcie/portdrv.h | 17 +++++++++++++++++
drivers/pci/pcie/portdrv_core.c | 5 +++++
drivers/pci/pcie/portdrv_pci.c | 27 +++++++++++++++++++++++++++
5 files changed, 64 insertions(+), 1 deletion(-)

Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/pme/pcie_pme.c
+++ linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
@@ -53,12 +53,22 @@ static bool pcie_pme_disabled;
*/
static bool pcie_pme_force_enable;

+/*
+ * If this switch is set, MSI will not be used for PCIe PME signaling. This
+ * causes the PCIe port driver to use INTx interrupts only, but it turns out
+ * that using MSI for PCIe PME signaling doesn't play well with PCIe PME-based
+ * wake-up from system sleep states.
+ */
+bool pcie_pme_msi_disabled;
+
static int __init pcie_pme_setup(char *str)
{
if (!strcmp(str, "off"))
pcie_pme_disabled = true;
else if (!strcmp(str, "force"))
pcie_pme_force_enable = true;
+ else if (!strcmp(str, "nomsi"))
+ pcie_pme_msi_disabled = true;
return 1;
}
__setup("pcie_pme=", pcie_pme_setup);
@@ -73,7 +83,9 @@ __setup("pcie_pme=", pcie_pme_setup);
*/
static bool pcie_pme_platform_setup(struct pcie_device *srv)
{
- return !pcie_pme_platform_notify(srv) || pcie_pme_force_enable;
+ if (!pcie_pme_platform_notify(srv))
+ return true;
+ return pcie_pme_force_enable;
}

struct pcie_pme_service_data {
Index: linux-2.6/drivers/pci/pcie/portdrv_core.c
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/portdrv_core.c
+++ linux-2.6/drivers/pci/pcie/portdrv_core.c
@@ -190,6 +190,10 @@ static int assign_interrupt_mode(struct
int irq, interrupt_mode = PCIE_PORT_NO_IRQ;
int i;

+ /* We have to use INTx if MSI cannot be used for PCIe PME. */
+ if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
+ goto no_msi;
+
/* Try to use MSI-X if supported */
if (!pcie_port_enable_msix(dev, vectors, mask))
return PCIE_PORT_MSIX_MODE;
@@ -198,6 +202,7 @@ static int assign_interrupt_mode(struct
if (!pci_enable_msi(dev))
interrupt_mode = PCIE_PORT_MSI_MODE;

+ no_msi:
if (interrupt_mode == PCIE_PORT_NO_IRQ && dev->pin)
interrupt_mode = PCIE_PORT_INTx_MODE;

Index: linux-2.6/drivers/pci/pcie/portdrv_pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/portdrv_pci.c
+++ linux-2.6/drivers/pci/pcie/portdrv_pci.c
@@ -15,6 +15,7 @@
#include <linux/slab.h>
#include <linux/pcieport_if.h>
#include <linux/aer.h>
+#include <linux/dmi.h>

#include "portdrv.h"
#include "aer/aerdrv.h"
@@ -272,10 +273,36 @@ static struct pci_driver pcie_portdriver
.driver.pm = PCIE_PORTDRV_PM_OPS,
};

+static int __init dmi_pcie_pme_disable_msi(const struct dmi_system_id *d)
+{
+ pr_notice("%s detected: will not use MSI for PCIe PME signaling\n",
+ d->ident);
+ pcie_pme_disable_msi();
+ return 0;
+}
+
+static struct dmi_system_id __initdata pcie_portdrv_dmi_table[] = {
+ /*
+ * Boxes that should not use MSI for PCIe PME signaling.
+ */
+ {
+ .callback = dmi_pcie_pme_disable_msi,
+ .ident = "MSI Wind U-100",
+ .matches = {
+ DMI_MATCH(DMI_SYS_VENDOR,
+ "MICRO-STAR INTERNATIONAL CO., LTD"),
+ DMI_MATCH(DMI_PRODUCT_NAME, "U-100"),
+ },
+ },
+ {}
+};
+
static int __init pcie_portdrv_init(void)
{
int retval;

+ dmi_check_system(pcie_portdrv_dmi_table);
+
retval = pcie_port_bus_register();
if (retval) {
printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
Index: linux-2.6/drivers/pci/pcie/portdrv.h
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/portdrv.h
+++ linux-2.6/drivers/pci/pcie/portdrv.h
@@ -45,4 +45,21 @@ extern void pcie_port_device_remove(stru
extern int __must_check pcie_port_bus_register(void);
extern void pcie_port_bus_unregister(void);

+#ifdef CONFIG_PCIE_PME
+extern bool pcie_pme_msi_disabled;
+
+static inline void pcie_pme_disable_msi(void)
+{
+ pcie_pme_msi_disabled = true;
+}
+
+static inline bool pcie_pme_no_msi(void)
+{
+ return pcie_pme_msi_disabled;
+}
+#else /* !CONFIG_PCIE_PME */
+static inline void pcie_pme_disable_msi(void) {}
+static inline bool pcie_pme_no_msi(void) { return false; }
+#endif /* !CONFIG_PCIE_PME */
+
#endif /* _PORTDRV_H_ */
Index: linux-2.6/Documentation/kernel-parameters.txt
===================================================================
--- linux-2.6.orig/Documentation/kernel-parameters.txt
+++ linux-2.6/Documentation/kernel-parameters.txt
@@ -1971,6 +1971,8 @@ and is between 256 and 4096 characters.
force Use native PCIe PME signaling even if the BIOS refuses
to allow the kernel to control the relevant PCIe config
registers.
+ nomsi Do not use MSI for native PCIe PME signaling (this makes
+ all PCIe root ports use INTx for everything).

pcmv= [HW,PCMCIA] BadgePAD 4

2009-12-04 23:21:46

by Jesse Barnes

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/12] PCI run-time PM support (updated)

On Sun, 29 Nov 2009 16:32:44 +0100
"Rafael J. Wysocki" <[email protected]> wrote:

> Hi,
>
> The following (updated) series of patches provides preliminary
> run-time power management support for PCI devices through ACPI and/or
> the native PCIe PME.
>
> Some patches have been modified since the previous iteration and two
> new patches have been added. Also, I've tested this patchset with
> the native PCIe PME mechanism using the r8169 driver on the MSI Wind
> U-100 (see the last patch for details).
>
> [1/12] - Add flag for marking devices capable of generating wake-up
> events at run time
> [2/12] - Add function for checking PME status of devices
>
> [3/12] - Modify wake-up enable propagation so that it's done for PCIe
> devices too
> [4/12] - PCIe PME root port service driver
>
> [5/12][New] - "Don't use MSIs for PME signaling" switch for PCIe
>
> [6/12] - ACPI GPE refcounting, from Matthew
>
> [7/12] - ACPI drivers support for GPE refcounting, from Matthew
>
> [8/12] - ACPI removal of the old GPE API, from Matthew
>
> [9/12] - ACPI add fields for handling run-wake devices
>
> [10/12] - PCI ACPI platform support for run-time power management
>
> [11/12] - Runtime PM callbacks for the PCI bus type
>
> [12/12] - Runtime PM support for r8169 (experimental)
>
> If there are no objections, I think the patches [1-11/12] are ready
> for merging at this point.

Len, it sounds like this patchset is settling down. Have you or one of
the ACPI folks had a chance to check out the ACPI GPE changes? I can
take it all through my PCI tree if you're ok with it.

--
Jesse Barnes, Intel Open Source Technology Center

2009-12-05 00:35:39

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 10/12] PCI / ACPI PM: Platform support for PCI PME wake-up (rev. 5)

On Friday 04 December 2009, Rafael J. Wysocki wrote:
> On Friday 04 December 2009, Matthew Garrett wrote:
> > On Sun, Nov 29, 2009 at 04:42:20PM +0100, Rafael J. Wysocki wrote:
> >
> > > + if (event == ACPI_NOTIFY_DEVICE_WAKE) {
> > > + if (nb->dev->wakeup.run_wake_count > 0) {
> > > + if (nb->pci_bus)
> > > + pci_pme_wakeup_bus(nb->pci_bus);
> > > + if (nb->pci_dev)
> > > + pci_pme_wakeup(nb->pci_dev);
> >
> > We may receive wakeup events on devices that aren't PME capable, which
> > is the case for uhci on my test box. In that case we probably want to
> > wake them up unconditionally.
> >
> > + if (nb->pci_dev) {
> > + if (nb->pci_dev->pm_cap)
> > + pci_pme_wakeup(nb->pci_dev);
> > + else
> > + pm_request_resume(&nb->pci_dev->dev);
> >
> > seems to work, though possibly we should assume that the firmware knows
> > best and always schedule a wake in respose to a resume request?
>
> I think we can simply do:
>
> + if (nb->pci_dev) {
> + pci_pme_wakeup(nb->pci_dev);
> + pm_request_resume(&nb->pci_dev->dev);
> + }
>
> If the pci_pme_wakeup() spawns a resume request, the other call will just
> return.

Or better, make it

pci_check_pme_status(nb->pci_dev);
pm_request_resume(&nb->pci_dev->dev);

which avoids calling pm_request_resume() twice and allows us to make
pci_pme_wakeup a static function.

Rev. 5 of the patch implementing this is appended.

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

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-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 new PCI platform callback ->test_run_wake() and ->run_wake() to
struct pci_platform_pm_ops allowing us, respectively, to check if
if the platform can generate run-time wake-up events for given
device and to enable/disable the platform to do that. Implemet
these callbacks 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_dev_run_wake() which can be used by PCI drivers to
check if given device is capable of generating wake-up events at
run time.

Developed in cooperation with Matthew Garrett <[email protected]>.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
drivers/acpi/pci_bind.c | 13 +
drivers/acpi/pci_root.c | 7
drivers/acpi/sleep.c | 22 +-
drivers/pci/hotplug/acpiphp_glue.c | 23 --
drivers/pci/pci-acpi.c | 399 +++++++++++++++++++++++++++++++++++++
drivers/pci/pci.c | 67 ++++++
drivers/pci/pci.h | 7
include/acpi/acpi_bus.h | 11 -
include/linux/pci-acpi.h | 24 ++
include/linux/pci.h | 1
kernel/power/Kconfig | 5
11 files changed, 555 insertions(+), 24 deletions(-)

Index: linux-2.6/drivers/pci/pci.h
===================================================================
--- linux-2.6.orig/drivers/pci/pci.h
+++ linux-2.6/drivers/pci/pci.h
@@ -35,6 +35,10 @@ int pci_probe_reset_function(struct pci_
*
* @sleep_wake: enables/disables the system 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,12 +48,15 @@ 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 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);
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci-acpi.c
+++ linux-2.6/drivers/pci/pci-acpi.c
@@ -16,9 +16,378 @@
#include <acpi/acpi_bus.h>

#include <linux/pci-acpi.h>
+#include <linux/pm_runtime.h>
#include "pci.h"

/*
+ * ACPI-based PCI run-time power management uses ACPI system notify handlers,
+ * which are also used by ACPI-based PCI hotplug. Unfortunately, however, there
+ * can be only one ACPI system notify handler installed for an ACPI device
+ * handle. For this reason there has to be a way to use the same notify handler
+ * for both ACPI-based hotplug and ACPI-based run-time PM.
+ *
+ * pci_acpi_runtime_notifiers is a list of struct pci_acpi_notifier_block
+ * objects representing PCI devices that have ACPI system notify handlers
+ * installed. For each of them, there is an ACPI-based hotplug notifier to
+ * execute for hotplug events, hp_cb, and a pointer to the data to pass to it
+ * hp_data, as well as poitners to a struct pci_bus object and a struct pci_dev
+ * object. If the device is a PCI-to-PCI bridge or root bridge, the struct
+ * pci_bus pointer is not NULL and it is assumed that the whole bus segment
+ * below the bridge has to be walked if a PME is reported for it. Otherwise,
+ * it is assumed that PME is generated only for the particular PCI device
+ * pointed to by the pci_dev field.
+ */
+
+static LIST_HEAD(pci_acpi_runtime_notifiers);
+static DEFINE_MUTEX(pci_acpi_notifier_mtx);
+
+struct pci_acpi_notifier_block
+{
+ struct list_head entry;
+ struct acpi_device *dev;
+ acpi_notify_handler hp_cb;
+ void *hp_data;
+ struct pci_bus *pci_bus;
+ struct pci_dev *pci_dev;
+};
+
+/**
+ * pci_acpi_event_fn - Universal system notification handler.
+ * @handle: ACPI handle of a device the notification is for.
+ * @event: Type of the signaled event.
+ * @data: Context data, should be a pointer to a notifier object.
+ *
+ * Take the address on a notifier object from @data and use it to extract the
+ * information needed for handling the event. If this is a wake-up event,
+ * check if PM notification is enabled for this notifier object and, if so,
+ * execute the appropriate PME handler for the bus or device represented by it.
+ * If this is not a wake-up event, execute the hotplug notify handler for
+ * @handle.
+ */
+static void pci_acpi_event_fn(acpi_handle handle, u32 event, void *data)
+{
+ struct pci_acpi_notifier_block *nb = data;
+
+ if (!nb)
+ return;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ if (event == ACPI_NOTIFY_DEVICE_WAKE) {
+ if (nb->dev->wakeup.run_wake_count > 0) {
+ if (nb->pci_bus) {
+ pci_pme_wakeup_bus(nb->pci_bus);
+ } if (nb->pci_dev) {
+ pci_check_pme_status(nb->pci_dev);
+ pm_request_resume(&nb->pci_dev->dev);
+ }
+ }
+ } else if (nb->hp_cb) {
+ nb->hp_cb(handle, event, nb->hp_data);
+ }
+
+ mutex_unlock(&pci_acpi_notifier_mtx);
+}
+
+/**
+ * new_notifier - Create a new notifier object for given ACPI device.
+ * @dev: Device to create the notifier object for.
+ */
+static struct pci_acpi_notifier_block *new_notifier(struct acpi_device *dev)
+{
+ struct pci_acpi_notifier_block *nb;
+
+ nb = kzalloc(sizeof(*nb), GFP_KERNEL);
+ if (!nb)
+ return NULL;
+
+ nb->dev = dev;
+ return nb;
+}
+
+/**
+ * pci_acpi_add_hp_notifier - Register a hotplug notifier for given device.
+ * @handle: ACPI handle of the device to register the notifier for.
+ * @handler: Callback to execute for hotplug events related to @handle.
+ * @context: Pointer to the context data to pass to @handler.
+ *
+ * Use @handle to get an ACPI device object and check if there is a notifier
+ * object for it. If this is the case, add @handler and @context to the
+ * existing notifier object, unless there already is a hotplug handler in this
+ * notifier object. Otherwise, create a new notifier object for the ACPI device
+ * associated with @handle and add @handler and @context to it.
+ */
+acpi_status pci_acpi_add_hp_notifier(acpi_handle handle,
+ acpi_notify_handler handler, void *context)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_status status = AE_OK;
+
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev)))
+ return AE_NOT_FOUND;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry) {
+ if (nb->dev != dev)
+ continue;
+
+ if (!nb->hp_cb) {
+ nb->hp_cb = handler;
+ nb->hp_data = context;
+ } else {
+ status = AE_ALREADY_EXISTS;
+ }
+ goto out;
+ }
+
+ nb = new_notifier(dev);
+ if (!nb) {
+ status = AE_NO_MEMORY;
+ goto out;
+ }
+ nb->hp_cb = handler;
+ nb->hp_data = context;
+
+ list_add_tail(&nb->entry, &pci_acpi_runtime_notifiers);
+
+ status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn, nb);
+ if (ACPI_FAILURE(status)) {
+ list_del(&nb->entry);
+ kfree(nb);
+ }
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+
+ return status;
+}
+EXPORT_SYMBOL_GPL(pci_acpi_add_hp_notifier);
+
+/**
+ * pci_acpi_remove_hp_notifier - Unregister a hotplug notifier for given device.
+ * @handle: ACPI handle of the device to unregister the notifier for.
+ * @handler: Callback executed for hotplug events related to @handle.
+ *
+ * Find the notifier object associated with @handle and remove the hotplug
+ * callback and the pointer to the hotplug context data from it. If the
+ * notifier object is not necessary any more, remove it altogether.
+ */
+acpi_status pci_acpi_remove_hp_notifier(acpi_handle handle,
+ acpi_notify_handler handler)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_status status = AE_NOT_FOUND;
+
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev)))
+ return AE_NOT_FOUND;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ status = AE_OK;
+ break;
+ }
+
+ if (status != AE_OK)
+ goto out;
+
+ nb->hp_data = NULL;
+ nb->hp_cb = NULL;
+
+ if (nb->pci_bus || nb->pci_dev)
+ goto out;
+
+ status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn);
+ list_del(&nb->entry);
+ kfree(nb);
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+EXPORT_SYMBOL_GPL(pci_acpi_remove_hp_notifier);
+
+/**
+ * pci_acpi_add_pm_notifier - Register PM notifier for given device.
+ * @dev: ACPI device to add the notifier for.
+ * @pci_dev: PCI device to check for the PME status if an event is signaled.
+ * @pci_bus: PCI bus to walk (checking PME status) if an event is signaled.
+ *
+ * Check if there is a notifier object for @dev and if that is the case, add
+ * @pci_dev to it as the device whose PME status should be checked if a PM
+ * event is signaled for @dev. Also, add @pci_bus to it as the bus to walk
+ * checking the PME status of all devices on it if a PM event is signaled for
+ * @dev. Otherwise, create a new notifier object for @dev and add both
+ * @pci_dev and @pci_bus to it.
+ */
+acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev,
+ struct pci_bus *pci_bus)
+{
+ struct pci_acpi_notifier_block *nb;
+ acpi_status status = AE_OK;
+
+ if (!dev->wakeup.flags.run_wake)
+ return AE_BAD_PARAMETER;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ if (nb->pci_dev || nb->pci_bus)
+ goto out;
+ else
+ goto add;
+ }
+
+ nb = new_notifier(dev);
+ if (!nb) {
+ status = AE_NO_MEMORY;
+ goto out;
+ }
+ list_add_tail(&nb->entry, &pci_acpi_runtime_notifiers);
+
+ status = acpi_install_notify_handler(dev->handle, ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn, nb);
+ if (ACPI_FAILURE(status)) {
+ list_del(&nb->entry);
+ kfree(nb);
+ goto out;
+ }
+
+ add:
+ nb->pci_dev = pci_dev;
+ nb->pci_bus = pci_bus;
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+
+/**
+ * pci_acpi_remove_pm_notifier - Unregister PM notifier for given device.
+ * @dev: ACPI device to remove the notifier from.
+ *
+ * Find the notifier object for @dev and clear its @pci_dev and @pci_bus fields.
+ * If the notifier object is not necessary any more after that, remove it too.
+ */
+acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev)
+{
+ struct pci_acpi_notifier_block *nb;
+ acpi_status status = AE_NOT_FOUND;
+
+ if (!dev->wakeup.flags.run_wake)
+ return AE_BAD_PARAMETER;
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ status = AE_OK;
+ break;
+ }
+
+ if (status != AE_OK)
+ goto out;
+
+ if (dev->wakeup.run_wake_count) {
+ dev->wakeup.run_wake_count = 0;
+ acpi_unref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ acpi_pm_wake_up_power(dev, false);
+ }
+
+ nb->pci_dev = NULL;
+ nb->pci_bus = NULL;
+
+ if (nb->hp_cb)
+ goto out;
+
+ status = acpi_remove_notify_handler(nb->dev->handle,
+ ACPI_SYSTEM_NOTIFY,
+ pci_acpi_event_fn);
+ list_del(&nb->entry);
+ kfree(nb);
+
+ out:
+ mutex_unlock(&pci_acpi_notifier_mtx);
+ return status;
+}
+
+/**
+ * run_wake_enable - Enable/disable the GPE associated with given notifier.
+ * @nb: Notifier to enable/disable the GPE for.
+ * @enable: Whether to enable or disable the wake-up feature.
+ */
+static int run_wake_enable(struct pci_acpi_notifier_block *nb, bool enable)
+{
+ struct acpi_device *dev = nb->dev;
+ int error = 0;
+
+ if (enable) {
+ if (!dev->wakeup.run_wake_count++) {
+ acpi_pm_wake_up_power(dev, true);
+ acpi_ref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ }
+ } else if (dev->wakeup.run_wake_count > 0) {
+ if (!--dev->wakeup.run_wake_count) {
+ acpi_unref_runtime_gpe(dev->wakeup.gpe_device,
+ dev->wakeup.gpe_number);
+ acpi_pm_wake_up_power(dev, false);
+ }
+ } else {
+ error = -EALREADY;
+ }
+
+ return error;
+}
+
+/**
+ * acpi_dev_run_wake_enable - Enable/disable wake-up for given device.
+ * @phys_dev: Device to enable/disable the platform to wake-up the system for.
+ * @enable: Whether enable or disable the wake-up functionality.
+ *
+ * Find the notifier object corresponding to @pci_dev and try to enable/disable
+ * the GPE associated with it.
+ */
+static int acpi_dev_run_wake_enable(struct device *phys_dev, bool enable)
+{
+ struct pci_acpi_notifier_block *nb;
+ struct acpi_device *dev;
+ acpi_handle handle;
+ int error = -ENODEV;
+
+ if (!device_run_wake(phys_dev))
+ return -EINVAL;
+
+ handle = DEVICE_ACPI_HANDLE(phys_dev);
+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
+ dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
+ __func__);
+ return -ENODEV;
+ }
+
+ mutex_lock(&pci_acpi_notifier_mtx);
+
+ list_for_each_entry(nb, &pci_acpi_runtime_notifiers, entry)
+ if (nb->dev == dev) {
+ error = run_wake_enable(nb, enable);
+ break;
+ }
+
+ mutex_unlock(&pci_acpi_notifier_mtx);
+
+ return error;
+}
+
+/*
* _SxD returns the D-state with the highest power
* (lowest D-state number) supported in the S-state "x".
*
@@ -131,12 +500,42 @@ static int acpi_pci_sleep_wake(struct pc
return 0;
}

+static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
+{
+ while (bus->parent) {
+ struct pci_dev *bridge = bus->self;
+
+ if (bridge->pme_interrupt)
+ return;
+ if (!acpi_dev_run_wake_enable(&bridge->dev, enable))
+ return;
+ bus = bus->parent;
+ }
+
+ /* We have reached the root bus. */
+ if (bus->bridge)
+ acpi_dev_run_wake_enable(bus->bridge, enable);
+}
+
+static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
+{
+ if (dev->pme_interrupt)
+ return 0;
+
+ if (!acpi_dev_run_wake_enable(&dev->dev, enable))
+ return 0;
+
+ acpi_pci_propagate_run_wake(dev->bus, enable);
+ return 0;
+}
+
static struct pci_platform_pm_ops acpi_pci_platform_pm = {
.is_manageable = acpi_pci_power_manageable,
.set_state = acpi_pci_set_power_state,
.choose_state = acpi_pci_choose_state,
.can_wakeup = acpi_pci_can_wakeup,
.sleep_wake = acpi_pci_sleep_wake,
+ .run_wake = acpi_pci_run_wake,
};

/* ACPI bus type */
Index: linux-2.6/drivers/pci/pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci.c
+++ 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;
}

+static inline int platform_pci_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.
+ */
+static 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#.
@@ -1406,6 +1438,41 @@ int pci_back_from_sleep(struct pci_dev *
}

/**
+ * pci_dev_run_wake - Check if device can generate run-time wake-up events.
+ * @dev: Device to check.
+ *
+ * Return true if the device itself is cabable of generating wake-up events
+ * (through the platform or using the native PCIe PME) or if the device supports
+ * PME and one of its upstream bridges can generate wake-up events.
+ */
+bool pci_dev_run_wake(struct pci_dev *dev)
+{
+ struct pci_bus *bus = dev->bus;
+
+ if (device_run_wake(&dev->dev))
+ return true;
+
+ if (!dev->pme_support)
+ return false;
+
+ while (bus->parent) {
+ struct pci_dev *bridge = bus->self;
+
+ if (device_run_wake(&bridge->dev))
+ return true;
+
+ bus = bus->parent;
+ }
+
+ /* We have reached the root bus. */
+ if (bus->bridge)
+ return device_run_wake(bus->bridge);
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(pci_dev_run_wake);
+
+/**
* pci_pm_init - Initialize PM functions of given PCI device
* @dev: PCI device to handle.
*/
Index: linux-2.6/kernel/power/Kconfig
===================================================================
--- linux-2.6.orig/kernel/power/Kconfig
+++ 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
Index: linux-2.6/drivers/acpi/sleep.c
===================================================================
--- linux-2.6.orig/drivers/acpi/sleep.c
+++ linux-2.6/drivers/acpi/sleep.c
@@ -634,7 +634,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
@@ -720,6 +720,18 @@ int acpi_pm_device_sleep_state(struct de
}

/**
+ * acpi_pm_wake_up_power - Enable/disable device wake-up power.
+ * @dev: ACPI device to handle.
+ * @enable: Whether to enable or disable the wake-up power of the device.
+ */
+int acpi_pm_wake_up_power(struct acpi_device *dev, bool enable)
+{
+ return enable ?
+ acpi_enable_wakeup_device_power(dev, acpi_target_sleep_state) :
+ acpi_disable_wakeup_device_power(dev);
+}
+
+/**
* acpi_pm_device_sleep_wake - enable or disable the system wake-up
* capability of given device
* @dev: device to handle
@@ -740,16 +752,14 @@ int acpi_pm_device_sleep_wake(struct dev
return -ENODEV;
}

- error = enable ?
- acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
- acpi_disable_wakeup_device_power(adev);
+ error = acpi_pm_wake_up_power(adev, enable);
if (!error)
- dev_info(dev, "wake-up capability %s by ACPI\n",
+ dev_info(dev, "wake-up power %s by ACPI\n",
enable ? "enabled" : "disabled");

return error;
}
-#endif
+#endif /* CONFIG_PM_WAKEUP */

static void acpi_power_off_prepare(void)
{
Index: linux-2.6/drivers/acpi/pci_root.c
===================================================================
--- linux-2.6.orig/drivers/acpi/pci_root.c
+++ linux-2.6/drivers/acpi/pci_root.c
@@ -30,6 +30,7 @@
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/pm.h>
+#include <linux/pm_runtime.h>
#include <linux/pci.h>
#include <linux/pci-acpi.h>
#include <linux/acpi.h>
@@ -576,6 +577,9 @@ static int __devinit acpi_pci_root_add(s
if (flags != base_flags)
acpi_pci_osc_support(root, flags);

+ if (!pci_acpi_add_bus_pm_notifier(device, root->bus))
+ device_set_run_wake(root->bus->bridge, true);
+
return 0;

end:
@@ -597,6 +601,9 @@ static int acpi_pci_root_remove(struct a
{
struct acpi_pci_root *root = acpi_driver_data(device);

+ pci_acpi_remove_pm_notifier(device);
+ device_set_run_wake(root->bus->bridge, false);
+
kfree(root);
return 0;
}
Index: linux-2.6/include/linux/pci-acpi.h
===================================================================
--- linux-2.6.orig/include/linux/pci-acpi.h
+++ linux-2.6/include/linux/pci-acpi.h
@@ -11,6 +11,30 @@
#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_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev,
+ struct pci_bus *pci_bus);
+extern acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev);
+
+static inline
+acpi_status pci_acpi_add_device_pm_notifier(struct acpi_device *dev,
+ struct pci_dev *pci_dev)
+{
+ return pci_acpi_add_pm_notifier(dev, pci_dev, pci_dev->subordinate);
+}
+
+static inline
+acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev,
+ struct pci_bus *pci_bus)
+{
+ return pci_acpi_add_pm_notifier(dev, NULL, pci_bus);
+}
+
static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
{
struct pci_bus *pbus = pdev->bus;
Index: linux-2.6/drivers/acpi/pci_bind.c
===================================================================
--- linux-2.6.orig/drivers/acpi/pci_bind.c
+++ linux-2.6/drivers/acpi/pci_bind.c
@@ -26,7 +26,9 @@
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
+#include <linux/pci-acpi.h>
#include <linux/acpi.h>
+#include <linux/pm_runtime.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

@@ -38,7 +40,13 @@ 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;
+
+ pci_acpi_remove_pm_notifier(device);
+ device_set_run_wake(&dev->dev, false);
+
+ if (!dev->subordinate)
goto out;

acpi_pci_irq_del_prt(dev->subordinate);
@@ -62,6 +70,9 @@ static int acpi_pci_bind(struct acpi_dev
if (!dev)
return 0;

+ if (!pci_acpi_add_device_pm_notifier(device, dev))
+ device_set_run_wake(&dev->dev, true);
+
/*
* Install the 'bind' function to facilitate callbacks for
* children of the P2P bridge.
Index: linux-2.6/drivers/pci/hotplug/acpiphp_glue.c
===================================================================
--- linux-2.6.orig/drivers/pci/hotplug/acpiphp_glue.c
+++ 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 ;
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -743,6 +743,7 @@ int pci_wake_from_d3(struct pci_dev *dev
pci_power_t pci_target_state(struct pci_dev *dev);
int pci_prepare_to_sleep(struct pci_dev *dev);
int pci_back_from_sleep(struct pci_dev *dev);
+bool pci_dev_run_wake(struct pci_dev *dev);

/* Functions for PCI Hotplug drivers to use */
int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap);
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
--- linux-2.6.orig/include/acpi/acpi_bus.h
+++ linux-2.6/include/acpi/acpi_bus.h
@@ -388,21 +388,26 @@ 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_wake_up_power(struct acpi_device *, bool);
int acpi_pm_device_sleep_wake(struct device *, bool);
-#else /* !CONFIG_PM_SLEEP */
+#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_wake_up_power(struct acpi_device *dev, bool enable)
+{
+ return -ENODEV;
+}
static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
{
return -ENODEV;
}
-#endif /* !CONFIG_PM_SLEEP */
+#endif /* !CONFIG_PM_WAKEUP */

#endif /* CONFIG_ACPI */

2009-12-05 00:37:35

by Rafael J. Wysocki

[permalink] [raw]
Subject: [RFC][PATCH 4/12] PCI PM: PCIe PME root port service driver (rev. 5) (updated)

On Sunday 29 November 2009, Rafael J. Wysocki wrote:
> 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.
...
> +/**
> + * pcie_pme_remove - Prepare PCIe PME service device for removal.
> + * @srv - PCIe service device to resume.
> + */
> +static int pcie_pme_remove(struct pcie_device *srv)

This should be a void function. Fixed patch is appended.

> +{
> + pcie_pme_suspend(srv);
> + free_irq(srv->irq, srv);
> + kfree(get_service_data(srv));
> +
> + return 0;
> +}

---
From: Rafael J. Wysocki <[email protected]>
Subject: PCI PM: PCIe PME root port service driver (rev. 5)

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]>.

Signed-off-by: Rafael J. Wysocki <[email protected]>
---
Documentation/kernel-parameters.txt | 6
drivers/pci/pcie/Kconfig | 4
drivers/pci/pcie/Makefile | 2
drivers/pci/pcie/pme/Makefile | 8
drivers/pci/pcie/pme/pcie_pme.c | 493 +++++++++++++++++++++++++++++++++++
drivers/pci/pcie/pme/pcie_pme.h | 28 +
drivers/pci/pcie/pme/pcie_pme_acpi.c | 54 +++
include/linux/pci.h | 1
8 files changed, 596 insertions(+)

Index: linux-2.6/drivers/pci/pcie/Kconfig
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/Kconfig
+++ 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
Index: linux-2.6/drivers/pci/pcie/Makefile
===================================================================
--- linux-2.6.orig/drivers/pci/pcie/Makefile
+++ 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/
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/pci/pcie/pme/pcie_pme.c
@@ -0,0 +1,493 @@
+/*
+ * PCIe Native PME support
+ *
+ * Copyright (C) 2007 - 2009 Intel Corp
+ * Copyright (C) 2007 - 2009 Shaohua Li <[email protected]>
+ * Copyright (C) 2009 Rafael J. Wysocki <[email protected]>, Novell Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License V2. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/device.h>
+#include <linux/pcieport_if.h>
+#include <linux/acpi.h>
+#include <linux/pci-acpi.h>
+#include <linux/pm_runtime.h>
+
+#include "../../pci.h"
+#include "pcie_pme.h"
+
+#define PCI_EXP_RTSTA_PME 0x10000 /* PME status */
+#define PCI_EXP_RTSTA_PENDING 0x20000 /* PME pending */
+
+/*
+ * If set, this switch will prevent the PCIe root port PME service driver from
+ * being registered. Consequently, the interrupt-based PCIe PME signaling will
+ * not be used by any PCIe root ports in that case.
+ */
+static bool pcie_pme_disabled;
+
+/*
+ * The PCI Express Base Specification 2.0, Section 6.1.8, states the following:
+ * "In order to maintain compatibility with non-PCI Express-aware system
+ * software, system power management logic must be configured by firmware to use
+ * the legacy mechanism of signaling PME by default. PCI Express-aware system
+ * software must notify the firmware prior to enabling native, interrupt-based
+ * PME signaling." However, if the platform doesn't provide us with a suitable
+ * notification mechanism or the notification fails, it is not clear whether or
+ * not we are supposed to use the interrupt-based PCIe PME signaling. The
+ * switch below can be used to indicate the desired behaviour. When set, it
+ * will make the kernel use the interrupt-based PCIe PME signaling regardless of
+ * the platform notification status, although the kernel will attempt to notify
+ * the platform anyway. When unset, it will prevent the kernel from using the
+ * the interrupt-based PCIe PME signaling if the platform notification fails,
+ * which is the default.
+ */
+static bool pcie_pme_force_enable;
+
+static int __init pcie_pme_setup(char *str)
+{
+ if (!strcmp(str, "off"))
+ pcie_pme_disabled = true;
+ else if (!strcmp(str, "force"))
+ pcie_pme_force_enable = true;
+ return 1;
+}
+__setup("pcie_pme=", pcie_pme_setup);
+
+/**
+ * pcie_pme_platform_setup - Ensure that the kernel controls the PCIe PME.
+ * @srv: PCIe PME root port service to use for carrying out the check.
+ *
+ * Notify the platform that the native PCIe PME is going to be used and return
+ * 'true' if the control of the PCIe PME registers has been acquired from the
+ * platform.
+ */
+static bool pcie_pme_platform_setup(struct pcie_device *srv)
+{
+ return !pcie_pme_platform_notify(srv) || pcie_pme_force_enable;
+}
+
+struct pcie_pme_service_data {
+ spinlock_t lock;
+ struct pcie_device *srv;
+ struct work_struct work;
+ bool noirq; /* Don't enable the PME interrupt used by this service. */
+};
+
+/**
+ * pcie_pme_interrupt_enable - Enable/disable PCIe PME interrupt generation.
+ * @dev: PCIe root port or event collector.
+ * @enable: Enable or disable the interrupt.
+ */
+static void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
+{
+ int rtctl_pos;
+ u16 rtctl;
+
+ rtctl_pos = pci_find_capability(dev, PCI_CAP_ID_EXP) + PCI_EXP_RTCTL;
+
+ pci_read_config_word(dev, rtctl_pos, &rtctl);
+ if (enable)
+ rtctl |= PCI_EXP_RTCTL_PMEIE;
+ else
+ rtctl &= ~PCI_EXP_RTCTL_PMEIE;
+ pci_write_config_word(dev, rtctl_pos, rtctl);
+}
+
+/**
+ * pcie_pme_clear_status - Clear root port PME interrupt status.
+ * @dev: PCIe root port or event collector.
+ */
+static void pcie_pme_clear_status(struct pci_dev *dev)
+{
+ int rtsta_pos;
+ u32 rtsta;
+
+ rtsta_pos = pci_find_capability(dev, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ pci_read_config_dword(dev, rtsta_pos, &rtsta);
+ rtsta |= PCI_EXP_RTSTA_PME;
+ pci_write_config_dword(dev, rtsta_pos, rtsta);
+}
+
+/**
+ * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
+ * @bus: PCI bus to scan.
+ *
+ * Scan given PCI bus and all buses under it for devices asserting PME#.
+ */
+static bool pcie_pme_walk_bus(struct pci_bus *bus)
+{
+ struct pci_dev *dev;
+ bool ret = false;
+
+ list_for_each_entry(dev, &bus->devices, bus_list) {
+ /* Skip PCIe devices in case we started from a root port. */
+ if (!dev->is_pcie && pci_check_pme_status(dev)) {
+ pm_request_resume(&dev->dev);
+ ret = true;
+ }
+
+ if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
+ ret = true;
+ }
+
+ return ret;
+}
+
+/**
+ * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
+ * @bus: Secondary bus of the bridge.
+ * @devfn: Device/function number to check.
+ *
+ * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
+ * PCIe PME message. In such that case the bridge should use the Requester ID
+ * of device/function number 0 on its secondary bus.
+ */
+static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
+{
+ struct pci_dev *dev;
+ bool found = false;
+
+ if (devfn)
+ return false;
+
+ dev = pci_dev_get(bus->self);
+ if (!dev)
+ return false;
+
+ if (dev->is_pcie && dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
+ down_read(&pci_bus_sem);
+ if (pcie_pme_walk_bus(bus))
+ found = true;
+ up_read(&pci_bus_sem);
+ }
+
+ pci_dev_put(dev);
+ return found;
+}
+
+/**
+ * pcie_pme_handle_request - Find device that generated PME and handle it.
+ * @port: Root port or event collector that generated the PME interrupt.
+ * @req_id: PCIe Requester ID of the device that generated the PME.
+ */
+static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
+{
+ u8 busnr = req_id >> 8, devfn = req_id & 0xff;
+ struct pci_bus *bus;
+ struct pci_dev *dev;
+ bool found = false;
+
+ /* First, check if the PME is from the root port itself. */
+ if (port->devfn == devfn && port->bus->number == busnr) {
+ if (pci_check_pme_status(port)) {
+ pm_request_resume(&port->dev);
+ found = true;
+ } else {
+ /*
+ * Apparently, the root port generated the PME on behalf
+ * of a non-PCIe device downstream. If this is done by
+ * a root port, the Requester ID field in its status
+ * register may contain either the root port's, or the
+ * source device's information (PCI Express Base
+ * Specification, Rev. 2.0, Section 6.1.9).
+ */
+ down_read(&pci_bus_sem);
+ found = pcie_pme_walk_bus(port->subordinate);
+ up_read(&pci_bus_sem);
+ }
+ goto out;
+ }
+
+ /* Second, find the bus the source device is on. */
+ bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
+ if (!bus)
+ goto out;
+
+ /* Next, check if the PME is from a PCIe-PCI bridge. */
+ found = pcie_pme_from_pci_bridge(bus, devfn);
+ if (found)
+ goto out;
+
+ /* Finally, try to find the PME source on the bus. */
+ down_read(&pci_bus_sem);
+ list_for_each_entry(dev, &bus->devices, bus_list) {
+ pci_dev_get(dev);
+ if (dev->devfn == devfn) {
+ found = true;
+ break;
+ }
+ pci_dev_put(dev);
+ }
+ up_read(&pci_bus_sem);
+
+ if (found) {
+ /* The device is there, but we have to check its PME status. */
+ found = pci_check_pme_status(dev);
+ if (found)
+ pm_request_resume(&dev->dev);
+ pci_dev_put(dev);
+ } else if (devfn) {
+ /*
+ * The device is not there, but we can still try to recover by
+ * assuming that the PME was reported by a PCIe-PCI bridge that
+ * used devfn different from zero.
+ */
+ dev_dbg(&port->dev, "PME interrupt generated for "
+ "non-existent device %02x:%02x.%d\n",
+ busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
+ found = pcie_pme_from_pci_bridge(bus, 0);
+ }
+
+ out:
+ if (!found)
+ dev_dbg(&port->dev, "Spurious native PME interrupt!\n");
+}
+
+/**
+ * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
+ * @work: Work structure giving access to service data.
+ */
+static void pcie_pme_work_fn(struct work_struct *work)
+{
+ struct pcie_pme_service_data *data =
+ container_of(work, struct pcie_pme_service_data, work);
+ struct pci_dev *port = data->srv->port;
+ int rtsta_pos;
+ u32 rtsta;
+
+ rtsta_pos = pci_find_capability(port, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ spin_lock_irq(&data->lock);
+
+ for (;;) {
+ if (data->noirq)
+ break;
+
+ pci_read_config_dword(port, rtsta_pos, &rtsta);
+ if (rtsta & PCI_EXP_RTSTA_PME) {
+ /*
+ * Clear PME status of the port. If there are other
+ * pending PMEs, the status will be set again.
+ */
+ pcie_pme_clear_status(port);
+
+ spin_unlock_irq(&data->lock);
+ pcie_pme_handle_request(port, rtsta & 0xffff);
+ spin_lock_irq(&data->lock);
+
+ continue;
+ }
+
+ /* No need to loop if there are no more PMEs pending. */
+ if (!(rtsta & PCI_EXP_RTSTA_PENDING))
+ break;
+
+ spin_unlock_irq(&data->lock);
+ cpu_relax();
+ spin_lock_irq(&data->lock);
+ }
+
+ if (!data->noirq)
+ pcie_pme_interrupt_enable(port, true);
+
+ spin_unlock_irq(&data->lock);
+}
+
+/**
+ * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
+ * @irq: Interrupt vector.
+ * @context: Interrupt context pointer.
+ */
+static irqreturn_t pcie_pme_irq(int irq, void *context)
+{
+ struct pci_dev *port;
+ struct pcie_pme_service_data *data;
+ int rtsta_pos;
+ u32 rtsta;
+ unsigned long flags;
+
+ port = ((struct pcie_device *)context)->port;
+ data = get_service_data((struct pcie_device *)context);
+
+ rtsta_pos = pci_find_capability(port, PCI_CAP_ID_EXP) + PCI_EXP_RTSTA;
+
+ spin_lock_irqsave(&data->lock, flags);
+ pci_read_config_dword(port, rtsta_pos, &rtsta);
+
+ if (!(rtsta & PCI_EXP_RTSTA_PME)) {
+ spin_unlock_irqrestore(&data->lock, flags);
+ return IRQ_NONE;
+ }
+
+ pcie_pme_interrupt_enable(port, false);
+ spin_unlock_irqrestore(&data->lock, flags);
+
+ /* We don't use pm_wq, because it's freezable. */
+ schedule_work(&data->work);
+
+ return IRQ_HANDLED;
+}
+
+/**
+ * pcie_pme_set_native - Set the PME interrupt flag for given device.
+ * @dev: PCI device to handle.
+ * @ign: Ignored.
+ */
+static int pcie_pme_set_native(struct pci_dev *dev, void *ign)
+{
+ dev_info(&dev->dev, "Signaling PME through PCIe PME interrupt\n");
+
+ device_set_run_wake(&dev->dev, true);
+ dev->pme_interrupt = true;
+ return 0;
+}
+
+/**
+ * pcie_pme_mark_devices - Set the PME interrupt flag for devices below a port.
+ * @port: PCIe root port or event collector to handle.
+ *
+ * For each device below given root port, including the port itself (or for each
+ * root complex integrated endpoint if @port is a root complex event collector)
+ * set the flag indicating that it can signal run-time wake-up events via PCIe
+ * PME interrupts.
+ */
+static void pcie_pme_mark_devices(struct pci_dev *port)
+{
+ pcie_pme_set_native(port, NULL);
+ if (port->subordinate) {
+ pci_walk_bus(port->subordinate, pcie_pme_set_native, NULL);
+ } else {
+ struct pci_bus *bus = port->bus;
+ struct pci_dev *dev;
+
+ /* Check if this is a root port event collector. */
+ if (port->pcie_type != PCI_EXP_TYPE_RC_EC || !bus)
+ return;
+
+ down_read(&pci_bus_sem);
+ list_for_each_entry(dev, &bus->devices, bus_list)
+ if (dev->is_pcie
+ && dev->pcie_type == PCI_EXP_TYPE_RC_END)
+ pcie_pme_set_native(dev, NULL);
+ up_read(&pci_bus_sem);
+ }
+}
+
+/**
+ * pcie_pme_probe - Initialize PCIe PME service for given root port.
+ * @srv: PCIe service to initialize.
+ */
+static int pcie_pme_probe(struct pcie_device *srv)
+{
+ struct pci_dev *port;
+ struct pcie_pme_service_data *data;
+ int ret;
+
+ if (!pcie_pme_platform_setup(srv))
+ return -EACCES;
+
+ data = kzalloc(sizeof(*data), GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
+ spin_lock_init(&data->lock);
+ INIT_WORK(&data->work, pcie_pme_work_fn);
+ data->srv = srv;
+ set_service_data(srv, data);
+
+ port = srv->port;
+ pcie_pme_interrupt_enable(port, false);
+ pcie_pme_clear_status(port);
+
+ ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
+ if (ret) {
+ kfree(data);
+ } else {
+ pcie_pme_mark_devices(port);
+ pcie_pme_interrupt_enable(port, true);
+ }
+
+ return ret;
+}
+
+/**
+ * pcie_pme_suspend - Suspend PCIe PME service device.
+ * @srv: PCIe service device to suspend.
+ */
+static int pcie_pme_suspend(struct pcie_device *srv)
+{
+ struct pcie_pme_service_data *data = get_service_data(srv);
+ struct pci_dev *port = srv->port;
+
+ spin_lock_irq(&data->lock);
+ pcie_pme_interrupt_enable(port, false);
+ pcie_pme_clear_status(port);
+ data->noirq = true;
+ spin_unlock_irq(&data->lock);
+
+ synchronize_irq(srv->irq);
+
+ return 0;
+}
+
+/**
+ * pcie_pme_resume - Resume PCIe PME service device.
+ * @srv - PCIe service device to resume.
+ */
+static int pcie_pme_resume(struct pcie_device *srv)
+{
+ struct pcie_pme_service_data *data = get_service_data(srv);
+ struct pci_dev *port = srv->port;
+
+ spin_lock_irq(&data->lock);
+ data->noirq = false;
+ pcie_pme_clear_status(port);
+ pcie_pme_interrupt_enable(port, true);
+ spin_unlock_irq(&data->lock);
+
+ return 0;
+}
+
+/**
+ * pcie_pme_remove - Prepare PCIe PME service device for removal.
+ * @srv - PCIe service device to resume.
+ */
+static void pcie_pme_remove(struct pcie_device *srv)
+{
+ pcie_pme_suspend(srv);
+ free_irq(srv->irq, srv);
+ kfree(get_service_data(srv));
+}
+
+static struct pcie_port_service_driver pcie_pme_driver = {
+ .name = "pcie_pme",
+ .port_type = PCIE_RC_PORT,
+ .service = PCIE_PORT_SERVICE_PME,
+
+ .probe = pcie_pme_probe,
+ .suspend = pcie_pme_suspend,
+ .resume = pcie_pme_resume,
+ .remove = pcie_pme_remove,
+};
+
+/**
+ * pcie_pme_service_init - Register the PCIe PME service driver.
+ */
+static int __init pcie_pme_service_init(void)
+{
+ return pcie_pme_disabled ?
+ -ENODEV : pcie_port_service_register(&pcie_pme_driver);
+}
+
+module_init(pcie_pme_service_init);
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme_acpi.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/pci/pcie/pme/pcie_pme_acpi.c
@@ -0,0 +1,54 @@
+/*
+ * PCIe Native PME support, ACPI-related part
+ *
+ * Copyright (C) 2009 Rafael J. Wysocki <[email protected]>, Novell Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License V2. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#include <linux/pci.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/acpi.h>
+#include <linux/pci-acpi.h>
+#include <linux/pcieport_if.h>
+
+/**
+ * pcie_pme_acpi_setup - Request the ACPI BIOS to release control over PCIe PME.
+ * @srv - PCIe PME service for a root port or event collector.
+ *
+ * Invoked when the PCIe bus type loads PCIe PME service driver. To avoid
+ * conflict with the BIOS PCIe support requires the BIOS to yield PCIe PME
+ * control to the kernel.
+ */
+int pcie_pme_acpi_setup(struct pcie_device *srv)
+{
+ acpi_status status = AE_NOT_FOUND;
+ struct pci_dev *port = srv->port;
+ acpi_handle handle;
+ int error = 0;
+
+ if (acpi_pci_disabled)
+ return -ENOSYS;
+
+ dev_info(&port->dev, "Requesting control of PCIe PME from ACPI BIOS\n");
+
+ handle = acpi_find_root_bridge_handle(port);
+ if (!handle)
+ return -EINVAL;
+
+ status = acpi_pci_osc_control_set(handle,
+ OSC_PCI_EXPRESS_PME_CONTROL |
+ OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
+ if (ACPI_FAILURE(status)) {
+ dev_info(&port->dev,
+ "Failed to receive control of PCIe PME service: %s\n",
+ (status == AE_SUPPORT || status == AE_NOT_FOUND) ?
+ "no _OSC support" : "ACPI _OSC failed");
+ error = -ENODEV;
+ }
+
+ return error;
+}
Index: linux-2.6/drivers/pci/pcie/pme/Makefile
===================================================================
--- /dev/null
+++ 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
Index: linux-2.6/drivers/pci/pcie/pme/pcie_pme.h
===================================================================
--- /dev/null
+++ 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
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -238,6 +238,7 @@ struct pci_dev {
configuration space */
unsigned int pme_support:5; /* Bitmask of states from which PME#
can be generated */
+ unsigned int pme_interrupt:1;
unsigned int d1_support:1; /* Low power state D1 is supported */
unsigned int d2_support:1; /* Low power state D2 is supported */
unsigned int no_d1d2:1; /* Only allow D0 and D3 */
Index: linux-2.6/Documentation/kernel-parameters.txt
===================================================================
--- linux-2.6.orig/Documentation/kernel-parameters.txt
+++ linux-2.6/Documentation/kernel-parameters.txt
@@ -1966,6 +1966,12 @@ and is between 256 and 4096 characters.
force Enable ASPM even on devices that claim not to support it.
WARNING: Forcing ASPM on may cause system lockups.

+ pcie_pme= [PCIE,PM] Native PCIe PME signaling options:
+ off Do not use native PCIe PME signaling.
+ force Use native PCIe PME signaling even if the BIOS refuses
+ to allow the kernel to control the relevant PCIe config
+ registers.
+
pcmv= [HW,PCMCIA] BadgePAD 4

pd. [PARIDE]

2009-12-15 11:50:17

by Pavel Machek

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

Hi!

> Use the PCI run-time power management framework to add simplified
> run-time PM support to the r8169 driver. Namely, make the driver
> suspend the device when the link is off and set it up for generating
> wake-up event after the link has been detected again.

Nice!

Do you have any idea how much power it saves?
Pavel

--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2009-12-16 19:20:41

by Jesse Barnes

[permalink] [raw]
Subject: Re: [RFC][PATCH 3/12] PCI / PM: Propagate wake-up enable for PCIe devices too

On Sun, 29 Nov 2009 16:35:54 +0100
"Rafael J. Wysocki" <[email protected]> wrote:

> From: Rafael J. Wysocki <[email protected]>
>
> Having read the PM part of the PCIe 2.0 specification more carefully
> I think that it was a mistake to restrict the wake-up enable
> propagation to non-PCIe devices, because if we do not request
> control of the root ports' PME registers via OSC, PCIe PME is
> supposed to be handled by the platform, just like the non-PCIe PME.
> Even if we do that, the wake-up propagation is done to allow the
> devices to wake up the system from sleep states which involves the
> platform anyway, so it won't hurt.
>
> Signed-off-by: Rafael J. Wysocki <[email protected]>
> ---
> drivers/pci/pci-acpi.c | 10 ++--------
> 1 file changed, 2 insertions(+), 8 deletions(-)

Applied after fixing up conflicts related to dev->is_pcie.

Thanks,
--
Jesse Barnes, Intel Open Source Technology Center

2009-12-16 19:27:33

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 3/12] PCI / PM: Propagate wake-up enable for PCIe devices too

On Wednesday 16 December 2009, Jesse Barnes wrote:
> On Sun, 29 Nov 2009 16:35:54 +0100
> "Rafael J. Wysocki" <[email protected]> wrote:
>
> > From: Rafael J. Wysocki <[email protected]>
> >
> > Having read the PM part of the PCIe 2.0 specification more carefully
> > I think that it was a mistake to restrict the wake-up enable
> > propagation to non-PCIe devices, because if we do not request
> > control of the root ports' PME registers via OSC, PCIe PME is
> > supposed to be handled by the platform, just like the non-PCIe PME.
> > Even if we do that, the wake-up propagation is done to allow the
> > devices to wake up the system from sleep states which involves the
> > platform anyway, so it won't hurt.
> >
> > Signed-off-by: Rafael J. Wysocki <[email protected]>
> > ---
> > drivers/pci/pci-acpi.c | 10 ++--------
> > 1 file changed, 2 insertions(+), 8 deletions(-)
>
> Applied after fixing up conflicts related to dev->is_pcie.

Thanks!

2009-12-27 19:48:13

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

On Tuesday 15 December 2009, Pavel Machek wrote:
> Hi!
>
> > Use the PCI run-time power management framework to add simplified
> > run-time PM support to the r8169 driver. Namely, make the driver
> > suspend the device when the link is off and set it up for generating
> > wake-up event after the link has been detected again.
>
> Nice!
>
> Do you have any idea how much power it saves?

No, but there should be a power meter somewhere here ...

I'll let you know if I can find it.

Rafael

2009-12-27 20:02:06

by Pavel Machek

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

On Sun 2009-12-27 20:48:37, Rafael J. Wysocki wrote:
> On Tuesday 15 December 2009, Pavel Machek wrote:
> > Hi!
> >
> > > Use the PCI run-time power management framework to add simplified
> > > run-time PM support to the r8169 driver. Namely, make the driver
> > > suspend the device when the link is off and set it up for generating
> > > wake-up event after the link has been detected again.
> >
> > Nice!
> >
> > Do you have any idea how much power it saves?
>
> No, but there should be a power meter somewhere here ...
>
> I'll let you know if I can find it.

On notebooks, you can often use

pavel@amd:/data/l/linux-msm/arch/arm/mach-msm$ cat
/proc/acpi/battery/BAT0/state
present: yes
capacity state: ok
charging state: charged
present rate: 0 mW
remaining capacity: 71830 mWh
present voltage: 16277 mV

...present rate is often usable-enough.
Pavel

--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2010-01-02 20:48:50

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

On Sunday 27 December 2009, Pavel Machek wrote:
> On Sun 2009-12-27 20:48:37, Rafael J. Wysocki wrote:
> > On Tuesday 15 December 2009, Pavel Machek wrote:
> > > Hi!
> > >
> > > > Use the PCI run-time power management framework to add simplified
> > > > run-time PM support to the r8169 driver. Namely, make the driver
> > > > suspend the device when the link is off and set it up for generating
> > > > wake-up event after the link has been detected again.
> > >
> > > Nice!
> > >
> > > Do you have any idea how much power it saves?
> >
> > No, but there should be a power meter somewhere here ...
> >
> > I'll let you know if I can find it.

Well, I found it, but it has a watt resolution and measures energy in kWh,
so it's not very useful in this case (the box draws 14 - 20 W total at full
power).

> On notebooks, you can often use
>
> pavel@amd:/data/l/linux-msm/arch/arm/mach-msm$ cat
> /proc/acpi/battery/BAT0/state
> present: yes
> capacity state: ok
> charging state: charged
> present rate: 0 mW
> remaining capacity: 71830 mWh
> present voltage: 16277 mV
>
> ...present rate is often usable-enough.

Using /proc/acpi/battery/BAT1/state I measured the energy drawn in 10 minutes
both with the network adapter in D0 and D3hot. The results were that with the
network adapter in D0 the box drew 2476 mWh, while with the network adapter in
D3hot it drew 2361 mWh. The difference is 115 mWh, or about 5% on this
particular box.

This means about 0.1 Wh in 10 minutes, so we can save about 0.6 Wh per hour.

Rafael

2010-01-03 19:55:55

by Pavel Machek

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support


> > On notebooks, you can often use
> >
> > pavel@amd:/data/l/linux-msm/arch/arm/mach-msm$ cat
> > /proc/acpi/battery/BAT0/state
> > present: yes
> > capacity state: ok
> > charging state: charged
> > present rate: 0 mW
> > remaining capacity: 71830 mWh
> > present voltage: 16277 mV
> >
> > ...present rate is often usable-enough.
>
> Using /proc/acpi/battery/BAT1/state I measured the energy drawn in 10 minutes
> both with the network adapter in D0 and D3hot. The results were that with the
> network adapter in D0 the box drew 2476 mWh, while with the network adapter in
> D3hot it drew 2361 mWh. The difference is 115 mWh, or about 5% on this
> particular box.
>
> This means about 0.1 Wh in 10 minutes, so we can save about 0.6 Wh per hour.

Which means it saves 0.6W :-). Measuring power in Wh/h is
"interesting". Ok, that's consistent with my experiments, ethernet
transciever was around 1W... Thanks.
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2010-01-03 21:01:12

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

On Sunday 03 January 2010, Pavel Machek wrote:
>
> > > On notebooks, you can often use
> > >
> > > pavel@amd:/data/l/linux-msm/arch/arm/mach-msm$ cat
> > > /proc/acpi/battery/BAT0/state
> > > present: yes
> > > capacity state: ok
> > > charging state: charged
> > > present rate: 0 mW
> > > remaining capacity: 71830 mWh
> > > present voltage: 16277 mV
> > >
> > > ...present rate is often usable-enough.
> >
> > Using /proc/acpi/battery/BAT1/state I measured the energy drawn in 10 minutes
> > both with the network adapter in D0 and D3hot. The results were that with the
> > network adapter in D0 the box drew 2476 mWh, while with the network adapter in
> > D3hot it drew 2361 mWh. The difference is 115 mWh, or about 5% on this
> > particular box.
> >
> > This means about 0.1 Wh in 10 minutes, so we can save about 0.6 Wh per hour.
>
> Which means it saves 0.6W :-). Measuring power in Wh/h is "interesting".

You can't save power, you can only save energy. Wh is just an energy unit.

> Ok, that's consistent with my experiments, ethernet transciever was around 1W... Thanks.

No big deal.

Rafael

2010-01-04 08:32:13

by Pavel Machek

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support


> > > Using /proc/acpi/battery/BAT1/state I measured the energy drawn in 10 minutes
> > > both with the network adapter in D0 and D3hot. The results were that with the
> > > network adapter in D0 the box drew 2476 mWh, while with the network adapter in
> > > D3hot it drew 2361 mWh. The difference is 115 mWh, or about 5% on this
> > > particular box.
> > >
> > > This means about 0.1 Wh in 10 minutes, so we can save about 0.6 Wh per hour.
> >
> > Which means it saves 0.6W :-). Measuring power in Wh/h is "interesting".
>
> You can't save power, you can only save energy. Wh is just an energy unit.

Of course we can save power, you just did :-).

(And in some cases -- data centers, limited cooling -- you are limited
by power, not energy. You are right that on notebooks, energy is more
important.)


Pavel

--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2010-01-04 09:53:59

by Oliver Neukum

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

Am Montag, 4. Januar 2010 09:31:08 schrieb Pavel Machek:
> > You can't save power, you can only save energy. Wh is just an energy unit.
>
> Of course we can save power, you just did :-).
>
> (And in some cases -- data centers, limited cooling -- you are limited
> by power, not energy. You are right that on notebooks, energy is more
> important.)

If possible less power is better on a laptop, fans are loud and take
energy to run.

Regards
Oliver

2010-01-04 19:50:35

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

On Monday 04 January 2010, Pavel Machek wrote:
>
> > > > Using /proc/acpi/battery/BAT1/state I measured the energy drawn in 10 minutes
> > > > both with the network adapter in D0 and D3hot. The results were that with the
> > > > network adapter in D0 the box drew 2476 mWh, while with the network adapter in
> > > > D3hot it drew 2361 mWh. The difference is 115 mWh, or about 5% on this
> > > > particular box.
> > > >
> > > > This means about 0.1 Wh in 10 minutes, so we can save about 0.6 Wh per hour.
> > >
> > > Which means it saves 0.6W :-). Measuring power in Wh/h is "interesting".
> >
> > You can't save power, you can only save energy. Wh is just an energy unit.
>
> Of course we can save power, you just did :-).
>
> (And in some cases -- data centers, limited cooling -- you are limited
> by power, not energy.

But still energy is what you pay for, isn't it?

> You are right that on notebooks, energy is more important.)

You may require less power to run, so if that's what you mean by "saving", then
I agree. :-)

Rafael

2010-01-04 21:00:51

by Pavel Machek

[permalink] [raw]
Subject: Re: [RFC][PATCH 12/12] PM / r8169: Add simplified run-time PM support

> > > > Which means it saves 0.6W :-). Measuring power in Wh/h is "interesting".
> > >
> > > You can't save power, you can only save energy. Wh is just an energy unit.
> >
> > Of course we can save power, you just did :-).
> >
> > (And in some cases -- data centers, limited cooling -- you are limited
> > by power, not energy.
>
> But still energy is what you pay for, isn't it?

Energy is what you pay for, but if you eat too much power, data centre
overheats and goes down.

> > You are right that on notebooks, energy is more important.)
>
> You may require less power to run, so if that's what you mean by "saving", then
> I agree. :-)

Yes, that's what I meant by saving :-), and yes, they are very
related.
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html