GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / pci / 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/pci.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/device.h>
20 #include <linux/pcieport_if.h>
21 #include <linux/pm_runtime.h>
22
23 #include "../pci.h"
24 #include "portdrv.h"
25
26 /*
27  * If this switch is set, MSI will not be used for PCIe PME signaling.  This
28  * causes the PCIe port driver to use INTx interrupts only, but it turns out
29  * that using MSI for PCIe PME signaling doesn't play well with PCIe PME-based
30  * wake-up from system sleep states.
31  */
32 bool pcie_pme_msi_disabled;
33
34 static int __init pcie_pme_setup(char *str)
35 {
36         if (!strncmp(str, "nomsi", 5))
37                 pcie_pme_msi_disabled = true;
38
39         return 1;
40 }
41 __setup("pcie_pme=", pcie_pme_setup);
42
43 enum pme_suspend_level {
44         PME_SUSPEND_NONE = 0,
45         PME_SUSPEND_WAKEUP,
46         PME_SUSPEND_NOIRQ,
47 };
48
49 struct pcie_pme_service_data {
50         spinlock_t lock;
51         struct pcie_device *srv;
52         struct work_struct work;
53         enum pme_suspend_level suspend_level;
54 };
55
56 /**
57  * pcie_pme_interrupt_enable - Enable/disable PCIe PME interrupt generation.
58  * @dev: PCIe root port or event collector.
59  * @enable: Enable or disable the interrupt.
60  */
61 void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
62 {
63         if (enable)
64                 pcie_capability_set_word(dev, PCI_EXP_RTCTL,
65                                          PCI_EXP_RTCTL_PMEIE);
66         else
67                 pcie_capability_clear_word(dev, PCI_EXP_RTCTL,
68                                            PCI_EXP_RTCTL_PMEIE);
69 }
70
71 /**
72  * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
73  * @bus: PCI bus to scan.
74  *
75  * Scan given PCI bus and all buses under it for devices asserting PME#.
76  */
77 static bool pcie_pme_walk_bus(struct pci_bus *bus)
78 {
79         struct pci_dev *dev;
80         bool ret = false;
81
82         list_for_each_entry(dev, &bus->devices, bus_list) {
83                 /* Skip PCIe devices in case we started from a root port. */
84                 if (!pci_is_pcie(dev) && pci_check_pme_status(dev)) {
85                         if (dev->pme_poll)
86                                 dev->pme_poll = false;
87
88                         pci_wakeup_event(dev);
89                         pm_request_resume(&dev->dev);
90                         ret = true;
91                 }
92
93                 if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
94                         ret = true;
95         }
96
97         return ret;
98 }
99
100 /**
101  * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
102  * @bus: Secondary bus of the bridge.
103  * @devfn: Device/function number to check.
104  *
105  * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
106  * PCIe PME message.  In such that case the bridge should use the Requester ID
107  * of device/function number 0 on its secondary bus.
108  */
109 static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
110 {
111         struct pci_dev *dev;
112         bool found = false;
113
114         if (devfn)
115                 return false;
116
117         dev = pci_dev_get(bus->self);
118         if (!dev)
119                 return false;
120
121         if (pci_is_pcie(dev) && pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
122                 down_read(&pci_bus_sem);
123                 if (pcie_pme_walk_bus(bus))
124                         found = true;
125                 up_read(&pci_bus_sem);
126         }
127
128         pci_dev_put(dev);
129         return found;
130 }
131
132 /**
133  * pcie_pme_handle_request - Find device that generated PME and handle it.
134  * @port: Root port or event collector that generated the PME interrupt.
135  * @req_id: PCIe Requester ID of the device that generated the PME.
136  */
137 static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
138 {
139         u8 busnr = req_id >> 8, devfn = req_id & 0xff;
140         struct pci_bus *bus;
141         struct pci_dev *dev;
142         bool found = false;
143
144         /* First, check if the PME is from the root port itself. */
145         if (port->devfn == devfn && port->bus->number == busnr) {
146                 if (port->pme_poll)
147                         port->pme_poll = false;
148
149                 if (pci_check_pme_status(port)) {
150                         pm_request_resume(&port->dev);
151                         found = true;
152                 } else {
153                         /*
154                          * Apparently, the root port generated the PME on behalf
155                          * of a non-PCIe device downstream.  If this is done by
156                          * a root port, the Requester ID field in its status
157                          * register may contain either the root port's, or the
158                          * source device's information (PCI Express Base
159                          * Specification, Rev. 2.0, Section 6.1.9).
160                          */
161                         down_read(&pci_bus_sem);
162                         found = pcie_pme_walk_bus(port->subordinate);
163                         up_read(&pci_bus_sem);
164                 }
165                 goto out;
166         }
167
168         /* Second, find the bus the source device is on. */
169         bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
170         if (!bus)
171                 goto out;
172
173         /* Next, check if the PME is from a PCIe-PCI bridge. */
174         found = pcie_pme_from_pci_bridge(bus, devfn);
175         if (found)
176                 goto out;
177
178         /* Finally, try to find the PME source on the bus. */
179         down_read(&pci_bus_sem);
180         list_for_each_entry(dev, &bus->devices, bus_list) {
181                 pci_dev_get(dev);
182                 if (dev->devfn == devfn) {
183                         found = true;
184                         break;
185                 }
186                 pci_dev_put(dev);
187         }
188         up_read(&pci_bus_sem);
189
190         if (found) {
191                 /* The device is there, but we have to check its PME status. */
192                 found = pci_check_pme_status(dev);
193                 if (found) {
194                         if (dev->pme_poll)
195                                 dev->pme_poll = false;
196
197                         pci_wakeup_event(dev);
198                         pm_request_resume(&dev->dev);
199                 }
200                 pci_dev_put(dev);
201         } else if (devfn) {
202                 /*
203                  * The device is not there, but we can still try to recover by
204                  * assuming that the PME was reported by a PCIe-PCI bridge that
205                  * used devfn different from zero.
206                  */
207                 dev_dbg(&port->dev, "PME interrupt generated for non-existent device %02x:%02x.%d\n",
208                         busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
209                 found = pcie_pme_from_pci_bridge(bus, 0);
210         }
211
212  out:
213         if (!found)
214                 dev_dbg(&port->dev, "Spurious native PME interrupt!\n");
215 }
216
217 /**
218  * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
219  * @work: Work structure giving access to service data.
220  */
221 static void pcie_pme_work_fn(struct work_struct *work)
222 {
223         struct pcie_pme_service_data *data =
224                         container_of(work, struct pcie_pme_service_data, work);
225         struct pci_dev *port = data->srv->port;
226         u32 rtsta;
227
228         spin_lock_irq(&data->lock);
229
230         for (;;) {
231                 if (data->suspend_level != PME_SUSPEND_NONE)
232                         break;
233
234                 pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
235                 if (rtsta == (u32) ~0)
236                         break;
237
238                 if (rtsta & PCI_EXP_RTSTA_PME) {
239                         /*
240                          * Clear PME status of the port.  If there are other
241                          * pending PMEs, the status will be set again.
242                          */
243                         pcie_clear_root_pme_status(port);
244
245                         spin_unlock_irq(&data->lock);
246                         pcie_pme_handle_request(port, rtsta & 0xffff);
247                         spin_lock_irq(&data->lock);
248
249                         continue;
250                 }
251
252                 /* No need to loop if there are no more PMEs pending. */
253                 if (!(rtsta & PCI_EXP_RTSTA_PENDING))
254                         break;
255
256                 spin_unlock_irq(&data->lock);
257                 cpu_relax();
258                 spin_lock_irq(&data->lock);
259         }
260
261         if (data->suspend_level == PME_SUSPEND_NONE)
262                 pcie_pme_interrupt_enable(port, true);
263
264         spin_unlock_irq(&data->lock);
265 }
266
267 /**
268  * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
269  * @irq: Interrupt vector.
270  * @context: Interrupt context pointer.
271  */
272 static irqreturn_t pcie_pme_irq(int irq, void *context)
273 {
274         struct pci_dev *port;
275         struct pcie_pme_service_data *data;
276         u32 rtsta;
277         unsigned long flags;
278
279         port = ((struct pcie_device *)context)->port;
280         data = get_service_data((struct pcie_device *)context);
281
282         spin_lock_irqsave(&data->lock, flags);
283         pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
284
285         if (rtsta == (u32) ~0 || !(rtsta & PCI_EXP_RTSTA_PME)) {
286                 spin_unlock_irqrestore(&data->lock, flags);
287                 return IRQ_NONE;
288         }
289
290         pcie_pme_interrupt_enable(port, false);
291         spin_unlock_irqrestore(&data->lock, flags);
292
293         /* We don't use pm_wq, because it's freezable. */
294         schedule_work(&data->work);
295
296         return IRQ_HANDLED;
297 }
298
299 /**
300  * pcie_pme_set_native - Set the PME interrupt flag for given device.
301  * @dev: PCI device to handle.
302  * @ign: Ignored.
303  */
304 static int pcie_pme_set_native(struct pci_dev *dev, void *ign)
305 {
306         dev_info(&dev->dev, "Signaling PME through PCIe PME interrupt\n");
307
308         device_set_run_wake(&dev->dev, true);
309         dev->pme_interrupt = true;
310         return 0;
311 }
312
313 /**
314  * pcie_pme_mark_devices - Set the PME interrupt flag for devices below a port.
315  * @port: PCIe root port or event collector to handle.
316  *
317  * For each device below given root port, including the port itself (or for each
318  * root complex integrated endpoint if @port is a root complex event collector)
319  * set the flag indicating that it can signal run-time wake-up events via PCIe
320  * PME interrupts.
321  */
322 static void pcie_pme_mark_devices(struct pci_dev *port)
323 {
324         pcie_pme_set_native(port, NULL);
325         if (port->subordinate) {
326                 pci_walk_bus(port->subordinate, pcie_pme_set_native, NULL);
327         } else {
328                 struct pci_bus *bus = port->bus;
329                 struct pci_dev *dev;
330
331                 /* Check if this is a root port event collector. */
332                 if (pci_pcie_type(port) != PCI_EXP_TYPE_RC_EC || !bus)
333                         return;
334
335                 down_read(&pci_bus_sem);
336                 list_for_each_entry(dev, &bus->devices, bus_list)
337                         if (pci_is_pcie(dev)
338                             && pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
339                                 pcie_pme_set_native(dev, NULL);
340                 up_read(&pci_bus_sem);
341         }
342 }
343
344 /**
345  * pcie_pme_probe - Initialize PCIe PME service for given root port.
346  * @srv: PCIe service to initialize.
347  */
348 static int pcie_pme_probe(struct pcie_device *srv)
349 {
350         struct pci_dev *port;
351         struct pcie_pme_service_data *data;
352         int ret;
353
354         data = kzalloc(sizeof(*data), GFP_KERNEL);
355         if (!data)
356                 return -ENOMEM;
357
358         spin_lock_init(&data->lock);
359         INIT_WORK(&data->work, pcie_pme_work_fn);
360         data->srv = srv;
361         set_service_data(srv, data);
362
363         port = srv->port;
364         pcie_pme_interrupt_enable(port, false);
365         pcie_clear_root_pme_status(port);
366
367         ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
368         if (ret) {
369                 kfree(data);
370         } else {
371                 pcie_pme_mark_devices(port);
372                 pcie_pme_interrupt_enable(port, true);
373         }
374
375         return ret;
376 }
377
378 static bool pcie_pme_check_wakeup(struct pci_bus *bus)
379 {
380         struct pci_dev *dev;
381
382         if (!bus)
383                 return false;
384
385         list_for_each_entry(dev, &bus->devices, bus_list)
386                 if (device_may_wakeup(&dev->dev)
387                     || pcie_pme_check_wakeup(dev->subordinate))
388                         return true;
389
390         return false;
391 }
392
393 /**
394  * pcie_pme_suspend - Suspend PCIe PME service device.
395  * @srv: PCIe service device to suspend.
396  */
397 static int pcie_pme_suspend(struct pcie_device *srv)
398 {
399         struct pcie_pme_service_data *data = get_service_data(srv);
400         struct pci_dev *port = srv->port;
401         bool wakeup, wake_irq_enabled = false;
402         int ret;
403
404         if (device_may_wakeup(&port->dev)) {
405                 wakeup = true;
406         } else {
407                 down_read(&pci_bus_sem);
408                 wakeup = pcie_pme_check_wakeup(port->subordinate);
409                 up_read(&pci_bus_sem);
410         }
411         spin_lock_irq(&data->lock);
412         if (wakeup) {
413                 ret = enable_irq_wake(srv->irq);
414                 if (ret == 0) {
415                         data->suspend_level = PME_SUSPEND_WAKEUP;
416                         wake_irq_enabled = true;
417                 }
418         }
419         if (!wake_irq_enabled) {
420                 pcie_pme_interrupt_enable(port, false);
421                 pcie_clear_root_pme_status(port);
422                 data->suspend_level = PME_SUSPEND_NOIRQ;
423         }
424         spin_unlock_irq(&data->lock);
425
426         synchronize_irq(srv->irq);
427
428         return 0;
429 }
430
431 /**
432  * pcie_pme_resume - Resume PCIe PME service device.
433  * @srv - PCIe service device to resume.
434  */
435 static int pcie_pme_resume(struct pcie_device *srv)
436 {
437         struct pcie_pme_service_data *data = get_service_data(srv);
438
439         spin_lock_irq(&data->lock);
440         if (data->suspend_level == PME_SUSPEND_NOIRQ) {
441                 struct pci_dev *port = srv->port;
442
443                 pcie_clear_root_pme_status(port);
444                 pcie_pme_interrupt_enable(port, true);
445         } else {
446                 disable_irq_wake(srv->irq);
447         }
448         data->suspend_level = PME_SUSPEND_NONE;
449         spin_unlock_irq(&data->lock);
450
451         return 0;
452 }
453
454 /**
455  * pcie_pme_remove - Prepare PCIe PME service device for removal.
456  * @srv - PCIe service device to remove.
457  */
458 static void pcie_pme_remove(struct pcie_device *srv)
459 {
460         pcie_pme_suspend(srv);
461         free_irq(srv->irq, srv);
462         kfree(get_service_data(srv));
463 }
464
465 static struct pcie_port_service_driver pcie_pme_driver = {
466         .name           = "pcie_pme",
467         .port_type      = PCI_EXP_TYPE_ROOT_PORT,
468         .service        = PCIE_PORT_SERVICE_PME,
469
470         .probe          = pcie_pme_probe,
471         .suspend        = pcie_pme_suspend,
472         .resume         = pcie_pme_resume,
473         .remove         = pcie_pme_remove,
474 };
475
476 /**
477  * pcie_pme_service_init - Register the PCIe PME service driver.
478  */
479 static int __init pcie_pme_service_init(void)
480 {
481         return pcie_port_service_register(&pcie_pme_driver);
482 }
483 device_initcall(pcie_pme_service_init);