]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/pci/pcie/pme/pcie_pme.c
bbdea18693d94048fc2444dcb388c0a18817fc6e
[net-next-2.6.git] / drivers / pci / pcie / pme / pcie_pme.c
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>
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>
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  */
37 static bool pcie_pme_disabled = true;
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  */
55 static bool pcie_pme_force_enable;
56
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  */
63 bool pcie_pme_msi_disabled;
64
65 static int __init pcie_pme_setup(char *str)
66 {
67         if (!strncmp(str, "auto", 4))
68                 pcie_pme_disabled = false;
69         else if (!strncmp(str, "force", 5))
70                 pcie_pme_force_enable = true;
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
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  */
92 static bool pcie_pme_platform_setup(struct pcie_device *srv)
93 {
94         if (!pcie_pme_platform_notify(srv))
95                 return true;
96         return pcie_pme_force_enable;
97 }
98
99 struct 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  */
111 static void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
112 {
113         int rtctl_pos;
114         u16 rtctl;
115
116         rtctl_pos = pci_pcie_cap(dev) + PCI_EXP_RTCTL;
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  */
130 static void pcie_pme_clear_status(struct pci_dev *dev)
131 {
132         int rtsta_pos;
133         u32 rtsta;
134
135         rtsta_pos = pci_pcie_cap(dev) + PCI_EXP_RTSTA;
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  */
148 static 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. */
155                 if (!pci_is_pcie(dev) && pci_check_pme_status(dev)) {
156                         pm_request_resume(&dev->dev);
157                         pci_wakeup_event(dev);
158                         ret = true;
159                 }
160
161                 if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
162                         ret = true;
163         }
164
165         return ret;
166 }
167
168 /**
169  * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
170  * @bus: Secondary bus of the bridge.
171  * @devfn: Device/function number to check.
172  *
173  * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
174  * PCIe PME message.  In such that case the bridge should use the Requester ID
175  * of device/function number 0 on its secondary bus.
176  */
177 static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
178 {
179         struct pci_dev *dev;
180         bool found = false;
181
182         if (devfn)
183                 return false;
184
185         dev = pci_dev_get(bus->self);
186         if (!dev)
187                 return false;
188
189         if (pci_is_pcie(dev) && dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
190                 down_read(&pci_bus_sem);
191                 if (pcie_pme_walk_bus(bus))
192                         found = true;
193                 up_read(&pci_bus_sem);
194         }
195
196         pci_dev_put(dev);
197         return found;
198 }
199
200 /**
201  * pcie_pme_handle_request - Find device that generated PME and handle it.
202  * @port: Root port or event collector that generated the PME interrupt.
203  * @req_id: PCIe Requester ID of the device that generated the PME.
204  */
205 static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
206 {
207         u8 busnr = req_id >> 8, devfn = req_id & 0xff;
208         struct pci_bus *bus;
209         struct pci_dev *dev;
210         bool found = false;
211
212         /* First, check if the PME is from the root port itself. */
213         if (port->devfn == devfn && port->bus->number == busnr) {
214                 if (pci_check_pme_status(port)) {
215                         pm_request_resume(&port->dev);
216                         found = true;
217                 } else {
218                         /*
219                          * Apparently, the root port generated the PME on behalf
220                          * of a non-PCIe device downstream.  If this is done by
221                          * a root port, the Requester ID field in its status
222                          * register may contain either the root port's, or the
223                          * source device's information (PCI Express Base
224                          * Specification, Rev. 2.0, Section 6.1.9).
225                          */
226                         down_read(&pci_bus_sem);
227                         found = pcie_pme_walk_bus(port->subordinate);
228                         up_read(&pci_bus_sem);
229                 }
230                 goto out;
231         }
232
233         /* Second, find the bus the source device is on. */
234         bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
235         if (!bus)
236                 goto out;
237
238         /* Next, check if the PME is from a PCIe-PCI bridge. */
239         found = pcie_pme_from_pci_bridge(bus, devfn);
240         if (found)
241                 goto out;
242
243         /* Finally, try to find the PME source on the bus. */
244         down_read(&pci_bus_sem);
245         list_for_each_entry(dev, &bus->devices, bus_list) {
246                 pci_dev_get(dev);
247                 if (dev->devfn == devfn) {
248                         found = true;
249                         break;
250                 }
251                 pci_dev_put(dev);
252         }
253         up_read(&pci_bus_sem);
254
255         if (found) {
256                 /* The device is there, but we have to check its PME status. */
257                 found = pci_check_pme_status(dev);
258                 if (found) {
259                         pm_request_resume(&dev->dev);
260                         pci_wakeup_event(dev);
261                 }
262                 pci_dev_put(dev);
263         } else if (devfn) {
264                 /*
265                  * The device is not there, but we can still try to recover by
266                  * assuming that the PME was reported by a PCIe-PCI bridge that
267                  * used devfn different from zero.
268                  */
269                 dev_dbg(&port->dev, "PME interrupt generated for "
270                         "non-existent device %02x:%02x.%d\n",
271                         busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
272                 found = pcie_pme_from_pci_bridge(bus, 0);
273         }
274
275  out:
276         if (!found)
277                 dev_dbg(&port->dev, "Spurious native PME interrupt!\n");
278 }
279
280 /**
281  * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
282  * @work: Work structure giving access to service data.
283  */
284 static void pcie_pme_work_fn(struct work_struct *work)
285 {
286         struct pcie_pme_service_data *data =
287                         container_of(work, struct pcie_pme_service_data, work);
288         struct pci_dev *port = data->srv->port;
289         int rtsta_pos;
290         u32 rtsta;
291
292         rtsta_pos = pci_pcie_cap(port) + PCI_EXP_RTSTA;
293
294         spin_lock_irq(&data->lock);
295
296         for (;;) {
297                 if (data->noirq)
298                         break;
299
300                 pci_read_config_dword(port, rtsta_pos, &rtsta);
301                 if (rtsta & PCI_EXP_RTSTA_PME) {
302                         /*
303                          * Clear PME status of the port.  If there are other
304                          * pending PMEs, the status will be set again.
305                          */
306                         pcie_pme_clear_status(port);
307
308                         spin_unlock_irq(&data->lock);
309                         pcie_pme_handle_request(port, rtsta & 0xffff);
310                         spin_lock_irq(&data->lock);
311
312                         continue;
313                 }
314
315                 /* No need to loop if there are no more PMEs pending. */
316                 if (!(rtsta & PCI_EXP_RTSTA_PENDING))
317                         break;
318
319                 spin_unlock_irq(&data->lock);
320                 cpu_relax();
321                 spin_lock_irq(&data->lock);
322         }
323
324         if (!data->noirq)
325                 pcie_pme_interrupt_enable(port, true);
326
327         spin_unlock_irq(&data->lock);
328 }
329
330 /**
331  * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
332  * @irq: Interrupt vector.
333  * @context: Interrupt context pointer.
334  */
335 static irqreturn_t pcie_pme_irq(int irq, void *context)
336 {
337         struct pci_dev *port;
338         struct pcie_pme_service_data *data;
339         int rtsta_pos;
340         u32 rtsta;
341         unsigned long flags;
342
343         port = ((struct pcie_device *)context)->port;
344         data = get_service_data((struct pcie_device *)context);
345
346         rtsta_pos = pci_pcie_cap(port) + PCI_EXP_RTSTA;
347
348         spin_lock_irqsave(&data->lock, flags);
349         pci_read_config_dword(port, rtsta_pos, &rtsta);
350
351         if (!(rtsta & PCI_EXP_RTSTA_PME)) {
352                 spin_unlock_irqrestore(&data->lock, flags);
353                 return IRQ_NONE;
354         }
355
356         pcie_pme_interrupt_enable(port, false);
357         spin_unlock_irqrestore(&data->lock, flags);
358
359         /* We don't use pm_wq, because it's freezable. */
360         schedule_work(&data->work);
361
362         return IRQ_HANDLED;
363 }
364
365 /**
366  * pcie_pme_set_native - Set the PME interrupt flag for given device.
367  * @dev: PCI device to handle.
368  * @ign: Ignored.
369  */
370 static int pcie_pme_set_native(struct pci_dev *dev, void *ign)
371 {
372         dev_info(&dev->dev, "Signaling PME through PCIe PME interrupt\n");
373
374         device_set_run_wake(&dev->dev, true);
375         dev->pme_interrupt = true;
376         return 0;
377 }
378
379 /**
380  * pcie_pme_mark_devices - Set the PME interrupt flag for devices below a port.
381  * @port: PCIe root port or event collector to handle.
382  *
383  * For each device below given root port, including the port itself (or for each
384  * root complex integrated endpoint if @port is a root complex event collector)
385  * set the flag indicating that it can signal run-time wake-up events via PCIe
386  * PME interrupts.
387  */
388 static void pcie_pme_mark_devices(struct pci_dev *port)
389 {
390         pcie_pme_set_native(port, NULL);
391         if (port->subordinate) {
392                 pci_walk_bus(port->subordinate, pcie_pme_set_native, NULL);
393         } else {
394                 struct pci_bus *bus = port->bus;
395                 struct pci_dev *dev;
396
397                 /* Check if this is a root port event collector. */
398                 if (port->pcie_type != PCI_EXP_TYPE_RC_EC || !bus)
399                         return;
400
401                 down_read(&pci_bus_sem);
402                 list_for_each_entry(dev, &bus->devices, bus_list)
403                         if (pci_is_pcie(dev)
404                             && dev->pcie_type == PCI_EXP_TYPE_RC_END)
405                                 pcie_pme_set_native(dev, NULL);
406                 up_read(&pci_bus_sem);
407         }
408 }
409
410 /**
411  * pcie_pme_probe - Initialize PCIe PME service for given root port.
412  * @srv: PCIe service to initialize.
413  */
414 static int pcie_pme_probe(struct pcie_device *srv)
415 {
416         struct pci_dev *port;
417         struct pcie_pme_service_data *data;
418         int ret;
419
420         if (!pcie_pme_platform_setup(srv))
421                 return -EACCES;
422
423         data = kzalloc(sizeof(*data), GFP_KERNEL);
424         if (!data)
425                 return -ENOMEM;
426
427         spin_lock_init(&data->lock);
428         INIT_WORK(&data->work, pcie_pme_work_fn);
429         data->srv = srv;
430         set_service_data(srv, data);
431
432         port = srv->port;
433         pcie_pme_interrupt_enable(port, false);
434         pcie_pme_clear_status(port);
435
436         ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
437         if (ret) {
438                 kfree(data);
439         } else {
440                 pcie_pme_mark_devices(port);
441                 pcie_pme_interrupt_enable(port, true);
442         }
443
444         return ret;
445 }
446
447 /**
448  * pcie_pme_suspend - Suspend PCIe PME service device.
449  * @srv: PCIe service device to suspend.
450  */
451 static int pcie_pme_suspend(struct pcie_device *srv)
452 {
453         struct pcie_pme_service_data *data = get_service_data(srv);
454         struct pci_dev *port = srv->port;
455
456         spin_lock_irq(&data->lock);
457         pcie_pme_interrupt_enable(port, false);
458         pcie_pme_clear_status(port);
459         data->noirq = true;
460         spin_unlock_irq(&data->lock);
461
462         synchronize_irq(srv->irq);
463
464         return 0;
465 }
466
467 /**
468  * pcie_pme_resume - Resume PCIe PME service device.
469  * @srv - PCIe service device to resume.
470  */
471 static int pcie_pme_resume(struct pcie_device *srv)
472 {
473         struct pcie_pme_service_data *data = get_service_data(srv);
474         struct pci_dev *port = srv->port;
475
476         spin_lock_irq(&data->lock);
477         data->noirq = false;
478         pcie_pme_clear_status(port);
479         pcie_pme_interrupt_enable(port, true);
480         spin_unlock_irq(&data->lock);
481
482         return 0;
483 }
484
485 /**
486  * pcie_pme_remove - Prepare PCIe PME service device for removal.
487  * @srv - PCIe service device to resume.
488  */
489 static void pcie_pme_remove(struct pcie_device *srv)
490 {
491         pcie_pme_suspend(srv);
492         free_irq(srv->irq, srv);
493         kfree(get_service_data(srv));
494 }
495
496 static struct pcie_port_service_driver pcie_pme_driver = {
497         .name           = "pcie_pme",
498         .port_type      = PCI_EXP_TYPE_ROOT_PORT,
499         .service        = PCIE_PORT_SERVICE_PME,
500
501         .probe          = pcie_pme_probe,
502         .suspend        = pcie_pme_suspend,
503         .resume         = pcie_pme_resume,
504         .remove         = pcie_pme_remove,
505 };
506
507 /**
508  * pcie_pme_service_init - Register the PCIe PME service driver.
509  */
510 static int __init pcie_pme_service_init(void)
511 {
512         return pcie_pme_disabled ?
513                 -ENODEV : pcie_port_service_register(&pcie_pme_driver);
514 }
515
516 module_init(pcie_pme_service_init);