2 * PCIe Native PME support
4 * Copyright (C) 2007 - 2009 Intel Corp
5 * Copyright (C) 2007 - 2009 Shaohua Li <shaohua.li@intel.com>
6 * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License V2. See the file "COPYING" in the main directory of this archive
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/device.h>
21 #include <linux/pcieport_if.h>
22 #include <linux/acpi.h>
23 #include <linux/pci-acpi.h>
24 #include <linux/pm_runtime.h>
26 #include "../../pci.h"
29 #define PCI_EXP_RTSTA_PME 0x10000 /* PME status */
30 #define PCI_EXP_RTSTA_PENDING 0x20000 /* PME pending */
33 * If set, this switch will prevent the PCIe root port PME service driver from
34 * being registered. Consequently, the interrupt-based PCIe PME signaling will
35 * not be used by any PCIe root ports in that case.
37 static bool pcie_pme_disabled = true;
40 * The PCI Express Base Specification 2.0, Section 6.1.8, states the following:
41 * "In order to maintain compatibility with non-PCI Express-aware system
42 * software, system power management logic must be configured by firmware to use
43 * the legacy mechanism of signaling PME by default. PCI Express-aware system
44 * software must notify the firmware prior to enabling native, interrupt-based
45 * PME signaling." However, if the platform doesn't provide us with a suitable
46 * notification mechanism or the notification fails, it is not clear whether or
47 * not we are supposed to use the interrupt-based PCIe PME signaling. The
48 * switch below can be used to indicate the desired behaviour. When set, it
49 * will make the kernel use the interrupt-based PCIe PME signaling regardless of
50 * the platform notification status, although the kernel will attempt to notify
51 * the platform anyway. When unset, it will prevent the kernel from using the
52 * the interrupt-based PCIe PME signaling if the platform notification fails,
53 * which is the default.
55 static bool pcie_pme_force_enable;
58 * If this switch is set, MSI will not be used for PCIe PME signaling. This
59 * causes the PCIe port driver to use INTx interrupts only, but it turns out
60 * that using MSI for PCIe PME signaling doesn't play well with PCIe PME-based
61 * wake-up from system sleep states.
63 bool pcie_pme_msi_disabled;
65 static int __init pcie_pme_setup(char *str)
67 if (!strncmp(str, "auto", 4))
68 pcie_pme_disabled = false;
69 else if (!strncmp(str, "force", 5))
70 pcie_pme_force_enable = true;
72 str = strchr(str, ',');
75 str += strspn(str, " \t");
76 if (*str && !strcmp(str, "nomsi"))
77 pcie_pme_msi_disabled = true;
82 __setup("pcie_pme=", pcie_pme_setup);
85 * pcie_pme_platform_setup - Ensure that the kernel controls the PCIe PME.
86 * @srv: PCIe PME root port service to use for carrying out the check.
88 * Notify the platform that the native PCIe PME is going to be used and return
89 * 'true' if the control of the PCIe PME registers has been acquired from the
92 static bool pcie_pme_platform_setup(struct pcie_device *srv)
94 if (!pcie_pme_platform_notify(srv))
96 return pcie_pme_force_enable;
99 struct pcie_pme_service_data {
101 struct pcie_device *srv;
102 struct work_struct work;
103 bool noirq; /* Don't enable the PME interrupt used by this service. */
107 * pcie_pme_interrupt_enable - Enable/disable PCIe PME interrupt generation.
108 * @dev: PCIe root port or event collector.
109 * @enable: Enable or disable the interrupt.
111 static void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
116 rtctl_pos = pci_pcie_cap(dev) + PCI_EXP_RTCTL;
118 pci_read_config_word(dev, rtctl_pos, &rtctl);
120 rtctl |= PCI_EXP_RTCTL_PMEIE;
122 rtctl &= ~PCI_EXP_RTCTL_PMEIE;
123 pci_write_config_word(dev, rtctl_pos, rtctl);
127 * pcie_pme_clear_status - Clear root port PME interrupt status.
128 * @dev: PCIe root port or event collector.
130 static void pcie_pme_clear_status(struct pci_dev *dev)
135 rtsta_pos = pci_pcie_cap(dev) + PCI_EXP_RTSTA;
137 pci_read_config_dword(dev, rtsta_pos, &rtsta);
138 rtsta |= PCI_EXP_RTSTA_PME;
139 pci_write_config_dword(dev, rtsta_pos, rtsta);
143 * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
144 * @bus: PCI bus to scan.
146 * Scan given PCI bus and all buses under it for devices asserting PME#.
148 static bool pcie_pme_walk_bus(struct pci_bus *bus)
153 list_for_each_entry(dev, &bus->devices, bus_list) {
154 /* Skip PCIe devices in case we started from a root port. */
155 if (!pci_is_pcie(dev) && pci_check_pme_status(dev)) {
156 pm_request_resume(&dev->dev);
160 if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
168 * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
169 * @bus: Secondary bus of the bridge.
170 * @devfn: Device/function number to check.
172 * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
173 * PCIe PME message. In such that case the bridge should use the Requester ID
174 * of device/function number 0 on its secondary bus.
176 static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
184 dev = pci_dev_get(bus->self);
188 if (pci_is_pcie(dev) && dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
189 down_read(&pci_bus_sem);
190 if (pcie_pme_walk_bus(bus))
192 up_read(&pci_bus_sem);
200 * pcie_pme_handle_request - Find device that generated PME and handle it.
201 * @port: Root port or event collector that generated the PME interrupt.
202 * @req_id: PCIe Requester ID of the device that generated the PME.
204 static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
206 u8 busnr = req_id >> 8, devfn = req_id & 0xff;
211 /* First, check if the PME is from the root port itself. */
212 if (port->devfn == devfn && port->bus->number == busnr) {
213 if (pci_check_pme_status(port)) {
214 pm_request_resume(&port->dev);
218 * Apparently, the root port generated the PME on behalf
219 * of a non-PCIe device downstream. If this is done by
220 * a root port, the Requester ID field in its status
221 * register may contain either the root port's, or the
222 * source device's information (PCI Express Base
223 * Specification, Rev. 2.0, Section 6.1.9).
225 down_read(&pci_bus_sem);
226 found = pcie_pme_walk_bus(port->subordinate);
227 up_read(&pci_bus_sem);
232 /* Second, find the bus the source device is on. */
233 bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
237 /* Next, check if the PME is from a PCIe-PCI bridge. */
238 found = pcie_pme_from_pci_bridge(bus, devfn);
242 /* Finally, try to find the PME source on the bus. */
243 down_read(&pci_bus_sem);
244 list_for_each_entry(dev, &bus->devices, bus_list) {
246 if (dev->devfn == devfn) {
252 up_read(&pci_bus_sem);
255 /* The device is there, but we have to check its PME status. */
256 found = pci_check_pme_status(dev);
258 pm_request_resume(&dev->dev);
262 * The device is not there, but we can still try to recover by
263 * assuming that the PME was reported by a PCIe-PCI bridge that
264 * used devfn different from zero.
266 dev_dbg(&port->dev, "PME interrupt generated for "
267 "non-existent device %02x:%02x.%d\n",
268 busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
269 found = pcie_pme_from_pci_bridge(bus, 0);
274 dev_dbg(&port->dev, "Spurious native PME interrupt!\n");
278 * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
279 * @work: Work structure giving access to service data.
281 static void pcie_pme_work_fn(struct work_struct *work)
283 struct pcie_pme_service_data *data =
284 container_of(work, struct pcie_pme_service_data, work);
285 struct pci_dev *port = data->srv->port;
289 rtsta_pos = pci_pcie_cap(port) + PCI_EXP_RTSTA;
291 spin_lock_irq(&data->lock);
297 pci_read_config_dword(port, rtsta_pos, &rtsta);
298 if (rtsta & PCI_EXP_RTSTA_PME) {
300 * Clear PME status of the port. If there are other
301 * pending PMEs, the status will be set again.
303 pcie_pme_clear_status(port);
305 spin_unlock_irq(&data->lock);
306 pcie_pme_handle_request(port, rtsta & 0xffff);
307 spin_lock_irq(&data->lock);
312 /* No need to loop if there are no more PMEs pending. */
313 if (!(rtsta & PCI_EXP_RTSTA_PENDING))
316 spin_unlock_irq(&data->lock);
318 spin_lock_irq(&data->lock);
322 pcie_pme_interrupt_enable(port, true);
324 spin_unlock_irq(&data->lock);
328 * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
329 * @irq: Interrupt vector.
330 * @context: Interrupt context pointer.
332 static irqreturn_t pcie_pme_irq(int irq, void *context)
334 struct pci_dev *port;
335 struct pcie_pme_service_data *data;
340 port = ((struct pcie_device *)context)->port;
341 data = get_service_data((struct pcie_device *)context);
343 rtsta_pos = pci_pcie_cap(port) + PCI_EXP_RTSTA;
345 spin_lock_irqsave(&data->lock, flags);
346 pci_read_config_dword(port, rtsta_pos, &rtsta);
348 if (!(rtsta & PCI_EXP_RTSTA_PME)) {
349 spin_unlock_irqrestore(&data->lock, flags);
353 pcie_pme_interrupt_enable(port, false);
354 spin_unlock_irqrestore(&data->lock, flags);
356 /* We don't use pm_wq, because it's freezable. */
357 schedule_work(&data->work);
363 * pcie_pme_set_native - Set the PME interrupt flag for given device.
364 * @dev: PCI device to handle.
367 static int pcie_pme_set_native(struct pci_dev *dev, void *ign)
369 dev_info(&dev->dev, "Signaling PME through PCIe PME interrupt\n");
371 device_set_run_wake(&dev->dev, true);
372 dev->pme_interrupt = true;
377 * pcie_pme_mark_devices - Set the PME interrupt flag for devices below a port.
378 * @port: PCIe root port or event collector to handle.
380 * For each device below given root port, including the port itself (or for each
381 * root complex integrated endpoint if @port is a root complex event collector)
382 * set the flag indicating that it can signal run-time wake-up events via PCIe
385 static void pcie_pme_mark_devices(struct pci_dev *port)
387 pcie_pme_set_native(port, NULL);
388 if (port->subordinate) {
389 pci_walk_bus(port->subordinate, pcie_pme_set_native, NULL);
391 struct pci_bus *bus = port->bus;
394 /* Check if this is a root port event collector. */
395 if (port->pcie_type != PCI_EXP_TYPE_RC_EC || !bus)
398 down_read(&pci_bus_sem);
399 list_for_each_entry(dev, &bus->devices, bus_list)
401 && dev->pcie_type == PCI_EXP_TYPE_RC_END)
402 pcie_pme_set_native(dev, NULL);
403 up_read(&pci_bus_sem);
408 * pcie_pme_probe - Initialize PCIe PME service for given root port.
409 * @srv: PCIe service to initialize.
411 static int pcie_pme_probe(struct pcie_device *srv)
413 struct pci_dev *port;
414 struct pcie_pme_service_data *data;
417 if (!pcie_pme_platform_setup(srv))
420 data = kzalloc(sizeof(*data), GFP_KERNEL);
424 spin_lock_init(&data->lock);
425 INIT_WORK(&data->work, pcie_pme_work_fn);
427 set_service_data(srv, data);
430 pcie_pme_interrupt_enable(port, false);
431 pcie_pme_clear_status(port);
433 ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
437 pcie_pme_mark_devices(port);
438 pcie_pme_interrupt_enable(port, true);
445 * pcie_pme_suspend - Suspend PCIe PME service device.
446 * @srv: PCIe service device to suspend.
448 static int pcie_pme_suspend(struct pcie_device *srv)
450 struct pcie_pme_service_data *data = get_service_data(srv);
451 struct pci_dev *port = srv->port;
453 spin_lock_irq(&data->lock);
454 pcie_pme_interrupt_enable(port, false);
455 pcie_pme_clear_status(port);
457 spin_unlock_irq(&data->lock);
459 synchronize_irq(srv->irq);
465 * pcie_pme_resume - Resume PCIe PME service device.
466 * @srv - PCIe service device to resume.
468 static int pcie_pme_resume(struct pcie_device *srv)
470 struct pcie_pme_service_data *data = get_service_data(srv);
471 struct pci_dev *port = srv->port;
473 spin_lock_irq(&data->lock);
475 pcie_pme_clear_status(port);
476 pcie_pme_interrupt_enable(port, true);
477 spin_unlock_irq(&data->lock);
483 * pcie_pme_remove - Prepare PCIe PME service device for removal.
484 * @srv - PCIe service device to resume.
486 static void pcie_pme_remove(struct pcie_device *srv)
488 pcie_pme_suspend(srv);
489 free_irq(srv->irq, srv);
490 kfree(get_service_data(srv));
493 static struct pcie_port_service_driver pcie_pme_driver = {
495 .port_type = PCI_EXP_TYPE_ROOT_PORT,
496 .service = PCIE_PORT_SERVICE_PME,
498 .probe = pcie_pme_probe,
499 .suspend = pcie_pme_suspend,
500 .resume = pcie_pme_resume,
501 .remove = pcie_pme_remove,
505 * pcie_pme_service_init - Register the PCIe PME service driver.
507 static int __init pcie_pme_service_init(void)
509 return pcie_pme_disabled ?
510 -ENODEV : pcie_port_service_register(&pcie_pme_driver);
513 module_init(pcie_pme_service_init);