PNPACPI: Add support for remote wakeup
[deliverable/linux.git] / drivers / pci / pcie / pme / pcie_pme.c
CommitLineData
c7f48656
RW
1/*
2 * PCIe Native PME support
3 *
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.
7 *
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
10 * for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/pci.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
5a0e3ad6 17#include <linux/slab.h>
c7f48656
RW
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>
25
26#include "../../pci.h"
27#include "pcie_pme.h"
28
29#define PCI_EXP_RTSTA_PME 0x10000 /* PME status */
30#define PCI_EXP_RTSTA_PENDING 0x20000 /* PME pending */
31
32/*
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.
36 */
b27759f8 37static bool pcie_pme_disabled = true;
c7f48656
RW
38
39/*
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.
54 */
55static bool pcie_pme_force_enable;
56
c39fae14
RW
57/*
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.
62 */
63bool pcie_pme_msi_disabled;
64
c7f48656
RW
65static int __init pcie_pme_setup(char *str)
66{
b27759f8
RW
67 if (!strncmp(str, "auto", 4))
68 pcie_pme_disabled = false;
69 else if (!strncmp(str, "force", 5))
c7f48656 70 pcie_pme_force_enable = true;
b27759f8
RW
71
72 str = strchr(str, ',');
73 if (str) {
74 str++;
75 str += strspn(str, " \t");
76 if (*str && !strcmp(str, "nomsi"))
77 pcie_pme_msi_disabled = true;
78 }
79
c7f48656
RW
80 return 1;
81}
82__setup("pcie_pme=", pcie_pme_setup);
83
84/**
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.
87 *
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
90 * platform.
91 */
92static bool pcie_pme_platform_setup(struct pcie_device *srv)
93{
c39fae14
RW
94 if (!pcie_pme_platform_notify(srv))
95 return true;
96 return pcie_pme_force_enable;
c7f48656
RW
97}
98
99struct pcie_pme_service_data {
100 spinlock_t lock;
101 struct pcie_device *srv;
102 struct work_struct work;
103 bool noirq; /* Don't enable the PME interrupt used by this service. */
104};
105
106/**
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.
110 */
111static void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
112{
113 int rtctl_pos;
114 u16 rtctl;
115
b16694f7 116 rtctl_pos = pci_pcie_cap(dev) + PCI_EXP_RTCTL;
c7f48656
RW
117
118 pci_read_config_word(dev, rtctl_pos, &rtctl);
119 if (enable)
120 rtctl |= PCI_EXP_RTCTL_PMEIE;
121 else
122 rtctl &= ~PCI_EXP_RTCTL_PMEIE;
123 pci_write_config_word(dev, rtctl_pos, rtctl);
124}
125
126/**
127 * pcie_pme_clear_status - Clear root port PME interrupt status.
128 * @dev: PCIe root port or event collector.
129 */
130static void pcie_pme_clear_status(struct pci_dev *dev)
131{
132 int rtsta_pos;
133 u32 rtsta;
134
b16694f7 135 rtsta_pos = pci_pcie_cap(dev) + PCI_EXP_RTSTA;
c7f48656
RW
136
137 pci_read_config_dword(dev, rtsta_pos, &rtsta);
138 rtsta |= PCI_EXP_RTSTA_PME;
139 pci_write_config_dword(dev, rtsta_pos, rtsta);
140}
141
142/**
143 * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
144 * @bus: PCI bus to scan.
145 *
146 * Scan given PCI bus and all buses under it for devices asserting PME#.
147 */
148static bool pcie_pme_walk_bus(struct pci_bus *bus)
149{
150 struct pci_dev *dev;
151 bool ret = false;
152
153 list_for_each_entry(dev, &bus->devices, bus_list) {
154 /* Skip PCIe devices in case we started from a root port. */
552be54c 155 if (!pci_is_pcie(dev) && pci_check_pme_status(dev)) {
c7f48656
RW
156 pm_request_resume(&dev->dev);
157 ret = true;
158 }
159
160 if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
161 ret = true;
162 }
163
164 return ret;
165}
166
167/**
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.
171 *
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.
175 */
176static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
177{
178 struct pci_dev *dev;
179 bool found = false;
180
181 if (devfn)
182 return false;
183
184 dev = pci_dev_get(bus->self);
185 if (!dev)
186 return false;
187
552be54c 188 if (pci_is_pcie(dev) && dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
c7f48656
RW
189 down_read(&pci_bus_sem);
190 if (pcie_pme_walk_bus(bus))
191 found = true;
192 up_read(&pci_bus_sem);
193 }
194
195 pci_dev_put(dev);
196 return found;
197}
198
199/**
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.
203 */
204static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
205{
206 u8 busnr = req_id >> 8, devfn = req_id & 0xff;
207 struct pci_bus *bus;
208 struct pci_dev *dev;
209 bool found = false;
210
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);
215 found = true;
216 } else {
217 /*
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).
224 */
225 down_read(&pci_bus_sem);
226 found = pcie_pme_walk_bus(port->subordinate);
227 up_read(&pci_bus_sem);
228 }
229 goto out;
230 }
231
232 /* Second, find the bus the source device is on. */
233 bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
234 if (!bus)
235 goto out;
236
237 /* Next, check if the PME is from a PCIe-PCI bridge. */
238 found = pcie_pme_from_pci_bridge(bus, devfn);
239 if (found)
240 goto out;
241
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) {
245 pci_dev_get(dev);
246 if (dev->devfn == devfn) {
247 found = true;
248 break;
249 }
250 pci_dev_put(dev);
251 }
252 up_read(&pci_bus_sem);
253
254 if (found) {
255 /* The device is there, but we have to check its PME status. */
256 found = pci_check_pme_status(dev);
257 if (found)
258 pm_request_resume(&dev->dev);
259 pci_dev_put(dev);
260 } else if (devfn) {
261 /*
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.
265 */
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);
270 }
271
272 out:
273 if (!found)
274 dev_dbg(&port->dev, "Spurious native PME interrupt!\n");
275}
276
277/**
278 * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
279 * @work: Work structure giving access to service data.
280 */
281static void pcie_pme_work_fn(struct work_struct *work)
282{
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;
286 int rtsta_pos;
287 u32 rtsta;
288
b16694f7 289 rtsta_pos = pci_pcie_cap(port) + PCI_EXP_RTSTA;
c7f48656
RW
290
291 spin_lock_irq(&data->lock);
292
293 for (;;) {
294 if (data->noirq)
295 break;
296
297 pci_read_config_dword(port, rtsta_pos, &rtsta);
298 if (rtsta & PCI_EXP_RTSTA_PME) {
299 /*
300 * Clear PME status of the port. If there are other
301 * pending PMEs, the status will be set again.
302 */
303 pcie_pme_clear_status(port);
304
305 spin_unlock_irq(&data->lock);
306 pcie_pme_handle_request(port, rtsta & 0xffff);
307 spin_lock_irq(&data->lock);
308
309 continue;
310 }
311
312 /* No need to loop if there are no more PMEs pending. */
313 if (!(rtsta & PCI_EXP_RTSTA_PENDING))
314 break;
315
316 spin_unlock_irq(&data->lock);
317 cpu_relax();
318 spin_lock_irq(&data->lock);
319 }
320
321 if (!data->noirq)
322 pcie_pme_interrupt_enable(port, true);
323
324 spin_unlock_irq(&data->lock);
325}
326
327/**
328 * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
329 * @irq: Interrupt vector.
330 * @context: Interrupt context pointer.
331 */
332static irqreturn_t pcie_pme_irq(int irq, void *context)
333{
334 struct pci_dev *port;
335 struct pcie_pme_service_data *data;
336 int rtsta_pos;
337 u32 rtsta;
338 unsigned long flags;
339
340 port = ((struct pcie_device *)context)->port;
341 data = get_service_data((struct pcie_device *)context);
342
b16694f7 343 rtsta_pos = pci_pcie_cap(port) + PCI_EXP_RTSTA;
c7f48656
RW
344
345 spin_lock_irqsave(&data->lock, flags);
346 pci_read_config_dword(port, rtsta_pos, &rtsta);
347
348 if (!(rtsta & PCI_EXP_RTSTA_PME)) {
349 spin_unlock_irqrestore(&data->lock, flags);
350 return IRQ_NONE;
351 }
352
353 pcie_pme_interrupt_enable(port, false);
354 spin_unlock_irqrestore(&data->lock, flags);
355
356 /* We don't use pm_wq, because it's freezable. */
357 schedule_work(&data->work);
358
359 return IRQ_HANDLED;
360}
361
362/**
363 * pcie_pme_set_native - Set the PME interrupt flag for given device.
364 * @dev: PCI device to handle.
365 * @ign: Ignored.
366 */
367static int pcie_pme_set_native(struct pci_dev *dev, void *ign)
368{
369 dev_info(&dev->dev, "Signaling PME through PCIe PME interrupt\n");
370
371 device_set_run_wake(&dev->dev, true);
372 dev->pme_interrupt = true;
373 return 0;
374}
375
376/**
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.
379 *
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
383 * PME interrupts.
384 */
385static void pcie_pme_mark_devices(struct pci_dev *port)
386{
387 pcie_pme_set_native(port, NULL);
388 if (port->subordinate) {
389 pci_walk_bus(port->subordinate, pcie_pme_set_native, NULL);
390 } else {
391 struct pci_bus *bus = port->bus;
392 struct pci_dev *dev;
393
394 /* Check if this is a root port event collector. */
395 if (port->pcie_type != PCI_EXP_TYPE_RC_EC || !bus)
396 return;
397
398 down_read(&pci_bus_sem);
399 list_for_each_entry(dev, &bus->devices, bus_list)
552be54c 400 if (pci_is_pcie(dev)
c7f48656
RW
401 && dev->pcie_type == PCI_EXP_TYPE_RC_END)
402 pcie_pme_set_native(dev, NULL);
403 up_read(&pci_bus_sem);
404 }
405}
406
407/**
408 * pcie_pme_probe - Initialize PCIe PME service for given root port.
409 * @srv: PCIe service to initialize.
410 */
411static int pcie_pme_probe(struct pcie_device *srv)
412{
413 struct pci_dev *port;
414 struct pcie_pme_service_data *data;
415 int ret;
416
417 if (!pcie_pme_platform_setup(srv))
418 return -EACCES;
419
420 data = kzalloc(sizeof(*data), GFP_KERNEL);
421 if (!data)
422 return -ENOMEM;
423
424 spin_lock_init(&data->lock);
425 INIT_WORK(&data->work, pcie_pme_work_fn);
426 data->srv = srv;
427 set_service_data(srv, data);
428
429 port = srv->port;
430 pcie_pme_interrupt_enable(port, false);
431 pcie_pme_clear_status(port);
432
433 ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
434 if (ret) {
435 kfree(data);
436 } else {
437 pcie_pme_mark_devices(port);
438 pcie_pme_interrupt_enable(port, true);
439 }
440
441 return ret;
442}
443
444/**
445 * pcie_pme_suspend - Suspend PCIe PME service device.
446 * @srv: PCIe service device to suspend.
447 */
448static int pcie_pme_suspend(struct pcie_device *srv)
449{
450 struct pcie_pme_service_data *data = get_service_data(srv);
451 struct pci_dev *port = srv->port;
452
453 spin_lock_irq(&data->lock);
454 pcie_pme_interrupt_enable(port, false);
455 pcie_pme_clear_status(port);
456 data->noirq = true;
457 spin_unlock_irq(&data->lock);
458
459 synchronize_irq(srv->irq);
460
461 return 0;
462}
463
464/**
465 * pcie_pme_resume - Resume PCIe PME service device.
466 * @srv - PCIe service device to resume.
467 */
468static int pcie_pme_resume(struct pcie_device *srv)
469{
470 struct pcie_pme_service_data *data = get_service_data(srv);
471 struct pci_dev *port = srv->port;
472
473 spin_lock_irq(&data->lock);
474 data->noirq = false;
475 pcie_pme_clear_status(port);
476 pcie_pme_interrupt_enable(port, true);
477 spin_unlock_irq(&data->lock);
478
479 return 0;
480}
481
482/**
483 * pcie_pme_remove - Prepare PCIe PME service device for removal.
484 * @srv - PCIe service device to resume.
485 */
486static void pcie_pme_remove(struct pcie_device *srv)
487{
488 pcie_pme_suspend(srv);
489 free_irq(srv->irq, srv);
490 kfree(get_service_data(srv));
491}
492
493static struct pcie_port_service_driver pcie_pme_driver = {
494 .name = "pcie_pme",
495 .port_type = PCI_EXP_TYPE_ROOT_PORT,
496 .service = PCIE_PORT_SERVICE_PME,
497
498 .probe = pcie_pme_probe,
499 .suspend = pcie_pme_suspend,
500 .resume = pcie_pme_resume,
501 .remove = pcie_pme_remove,
502};
503
504/**
505 * pcie_pme_service_init - Register the PCIe PME service driver.
506 */
507static int __init pcie_pme_service_init(void)
508{
509 return pcie_pme_disabled ?
510 -ENODEV : pcie_port_service_register(&pcie_pme_driver);
511}
512
513module_init(pcie_pme_service_init);
This page took 0.073939 seconds and 5 git commands to generate.