Linux 4.19.133
[linux/fpc-iii.git] / drivers / pci / pcie / pme.c
blob54d593d10396ff9716c76a0b01eecb5a632f984c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCIe Native PME support
5 * Copyright (C) 2007 - 2009 Intel Corp
6 * Copyright (C) 2007 - 2009 Shaohua Li <shaohua.li@intel.com>
7 * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
8 */
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/device.h>
17 #include <linux/pm_runtime.h>
19 #include "../pci.h"
20 #include "portdrv.h"
23 * If this switch is set, MSI will not be used for PCIe PME signaling. This
24 * causes the PCIe port driver to use INTx interrupts only, but it turns out
25 * that using MSI for PCIe PME signaling doesn't play well with PCIe PME-based
26 * wake-up from system sleep states.
28 bool pcie_pme_msi_disabled;
30 static int __init pcie_pme_setup(char *str)
32 if (!strncmp(str, "nomsi", 5))
33 pcie_pme_msi_disabled = true;
35 return 1;
37 __setup("pcie_pme=", pcie_pme_setup);
39 struct pcie_pme_service_data {
40 spinlock_t lock;
41 struct pcie_device *srv;
42 struct work_struct work;
43 bool noirq; /* If set, keep the PME interrupt disabled. */
46 /**
47 * pcie_pme_interrupt_enable - Enable/disable PCIe PME interrupt generation.
48 * @dev: PCIe root port or event collector.
49 * @enable: Enable or disable the interrupt.
51 void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
53 if (enable)
54 pcie_capability_set_word(dev, PCI_EXP_RTCTL,
55 PCI_EXP_RTCTL_PMEIE);
56 else
57 pcie_capability_clear_word(dev, PCI_EXP_RTCTL,
58 PCI_EXP_RTCTL_PMEIE);
61 /**
62 * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
63 * @bus: PCI bus to scan.
65 * Scan given PCI bus and all buses under it for devices asserting PME#.
67 static bool pcie_pme_walk_bus(struct pci_bus *bus)
69 struct pci_dev *dev;
70 bool ret = false;
72 list_for_each_entry(dev, &bus->devices, bus_list) {
73 /* Skip PCIe devices in case we started from a root port. */
74 if (!pci_is_pcie(dev) && pci_check_pme_status(dev)) {
75 if (dev->pme_poll)
76 dev->pme_poll = false;
78 pci_wakeup_event(dev);
79 pm_request_resume(&dev->dev);
80 ret = true;
83 if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
84 ret = true;
87 return ret;
90 /**
91 * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
92 * @bus: Secondary bus of the bridge.
93 * @devfn: Device/function number to check.
95 * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
96 * PCIe PME message. In such that case the bridge should use the Requester ID
97 * of device/function number 0 on its secondary bus.
99 static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
101 struct pci_dev *dev;
102 bool found = false;
104 if (devfn)
105 return false;
107 dev = pci_dev_get(bus->self);
108 if (!dev)
109 return false;
111 if (pci_is_pcie(dev) && pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
112 down_read(&pci_bus_sem);
113 if (pcie_pme_walk_bus(bus))
114 found = true;
115 up_read(&pci_bus_sem);
118 pci_dev_put(dev);
119 return found;
123 * pcie_pme_handle_request - Find device that generated PME and handle it.
124 * @port: Root port or event collector that generated the PME interrupt.
125 * @req_id: PCIe Requester ID of the device that generated the PME.
127 static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
129 u8 busnr = req_id >> 8, devfn = req_id & 0xff;
130 struct pci_bus *bus;
131 struct pci_dev *dev;
132 bool found = false;
134 /* First, check if the PME is from the root port itself. */
135 if (port->devfn == devfn && port->bus->number == busnr) {
136 if (port->pme_poll)
137 port->pme_poll = false;
139 if (pci_check_pme_status(port)) {
140 pm_request_resume(&port->dev);
141 found = true;
142 } else {
144 * Apparently, the root port generated the PME on behalf
145 * of a non-PCIe device downstream. If this is done by
146 * a root port, the Requester ID field in its status
147 * register may contain either the root port's, or the
148 * source device's information (PCI Express Base
149 * Specification, Rev. 2.0, Section 6.1.9).
151 down_read(&pci_bus_sem);
152 found = pcie_pme_walk_bus(port->subordinate);
153 up_read(&pci_bus_sem);
155 goto out;
158 /* Second, find the bus the source device is on. */
159 bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
160 if (!bus)
161 goto out;
163 /* Next, check if the PME is from a PCIe-PCI bridge. */
164 found = pcie_pme_from_pci_bridge(bus, devfn);
165 if (found)
166 goto out;
168 /* Finally, try to find the PME source on the bus. */
169 down_read(&pci_bus_sem);
170 list_for_each_entry(dev, &bus->devices, bus_list) {
171 pci_dev_get(dev);
172 if (dev->devfn == devfn) {
173 found = true;
174 break;
176 pci_dev_put(dev);
178 up_read(&pci_bus_sem);
180 if (found) {
181 /* The device is there, but we have to check its PME status. */
182 found = pci_check_pme_status(dev);
183 if (found) {
184 if (dev->pme_poll)
185 dev->pme_poll = false;
187 pci_wakeup_event(dev);
188 pm_request_resume(&dev->dev);
190 pci_dev_put(dev);
191 } else if (devfn) {
193 * The device is not there, but we can still try to recover by
194 * assuming that the PME was reported by a PCIe-PCI bridge that
195 * used devfn different from zero.
197 pci_dbg(port, "PME interrupt generated for non-existent device %02x:%02x.%d\n",
198 busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
199 found = pcie_pme_from_pci_bridge(bus, 0);
202 out:
203 if (!found)
204 pci_dbg(port, "Spurious native PME interrupt!\n");
208 * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
209 * @work: Work structure giving access to service data.
211 static void pcie_pme_work_fn(struct work_struct *work)
213 struct pcie_pme_service_data *data =
214 container_of(work, struct pcie_pme_service_data, work);
215 struct pci_dev *port = data->srv->port;
216 u32 rtsta;
218 spin_lock_irq(&data->lock);
220 for (;;) {
221 if (data->noirq)
222 break;
224 pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
225 if (rtsta == (u32) ~0)
226 break;
228 if (rtsta & PCI_EXP_RTSTA_PME) {
230 * Clear PME status of the port. If there are other
231 * pending PMEs, the status will be set again.
233 pcie_clear_root_pme_status(port);
235 spin_unlock_irq(&data->lock);
236 pcie_pme_handle_request(port, rtsta & 0xffff);
237 spin_lock_irq(&data->lock);
239 continue;
242 /* No need to loop if there are no more PMEs pending. */
243 if (!(rtsta & PCI_EXP_RTSTA_PENDING))
244 break;
246 spin_unlock_irq(&data->lock);
247 cpu_relax();
248 spin_lock_irq(&data->lock);
251 if (!data->noirq)
252 pcie_pme_interrupt_enable(port, true);
254 spin_unlock_irq(&data->lock);
258 * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
259 * @irq: Interrupt vector.
260 * @context: Interrupt context pointer.
262 static irqreturn_t pcie_pme_irq(int irq, void *context)
264 struct pci_dev *port;
265 struct pcie_pme_service_data *data;
266 u32 rtsta;
267 unsigned long flags;
269 port = ((struct pcie_device *)context)->port;
270 data = get_service_data((struct pcie_device *)context);
272 spin_lock_irqsave(&data->lock, flags);
273 pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
275 if (rtsta == (u32) ~0 || !(rtsta & PCI_EXP_RTSTA_PME)) {
276 spin_unlock_irqrestore(&data->lock, flags);
277 return IRQ_NONE;
280 pcie_pme_interrupt_enable(port, false);
281 spin_unlock_irqrestore(&data->lock, flags);
283 /* We don't use pm_wq, because it's freezable. */
284 schedule_work(&data->work);
286 return IRQ_HANDLED;
290 * pcie_pme_can_wakeup - Set the wakeup capability flag.
291 * @dev: PCI device to handle.
292 * @ign: Ignored.
294 static int pcie_pme_can_wakeup(struct pci_dev *dev, void *ign)
296 device_set_wakeup_capable(&dev->dev, true);
297 return 0;
301 * pcie_pme_mark_devices - Set the wakeup flag for devices below a port.
302 * @port: PCIe root port or event collector to handle.
304 * For each device below given root port, including the port itself (or for each
305 * root complex integrated endpoint if @port is a root complex event collector)
306 * set the flag indicating that it can signal run-time wake-up events.
308 static void pcie_pme_mark_devices(struct pci_dev *port)
310 pcie_pme_can_wakeup(port, NULL);
311 if (port->subordinate)
312 pci_walk_bus(port->subordinate, pcie_pme_can_wakeup, NULL);
316 * pcie_pme_probe - Initialize PCIe PME service for given root port.
317 * @srv: PCIe service to initialize.
319 static int pcie_pme_probe(struct pcie_device *srv)
321 struct pci_dev *port;
322 struct pcie_pme_service_data *data;
323 int ret;
325 data = kzalloc(sizeof(*data), GFP_KERNEL);
326 if (!data)
327 return -ENOMEM;
329 spin_lock_init(&data->lock);
330 INIT_WORK(&data->work, pcie_pme_work_fn);
331 data->srv = srv;
332 set_service_data(srv, data);
334 port = srv->port;
335 pcie_pme_interrupt_enable(port, false);
336 pcie_clear_root_pme_status(port);
338 ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
339 if (ret) {
340 kfree(data);
341 return ret;
344 pci_info(port, "Signaling PME with IRQ %d\n", srv->irq);
346 pcie_pme_mark_devices(port);
347 pcie_pme_interrupt_enable(port, true);
348 return 0;
351 static bool pcie_pme_check_wakeup(struct pci_bus *bus)
353 struct pci_dev *dev;
355 if (!bus)
356 return false;
358 list_for_each_entry(dev, &bus->devices, bus_list)
359 if (device_may_wakeup(&dev->dev)
360 || pcie_pme_check_wakeup(dev->subordinate))
361 return true;
363 return false;
366 static void pcie_pme_disable_interrupt(struct pci_dev *port,
367 struct pcie_pme_service_data *data)
369 spin_lock_irq(&data->lock);
370 pcie_pme_interrupt_enable(port, false);
371 pcie_clear_root_pme_status(port);
372 data->noirq = true;
373 spin_unlock_irq(&data->lock);
377 * pcie_pme_suspend - Suspend PCIe PME service device.
378 * @srv: PCIe service device to suspend.
380 static int pcie_pme_suspend(struct pcie_device *srv)
382 struct pcie_pme_service_data *data = get_service_data(srv);
383 struct pci_dev *port = srv->port;
384 bool wakeup;
385 int ret;
387 if (device_may_wakeup(&port->dev)) {
388 wakeup = true;
389 } else {
390 down_read(&pci_bus_sem);
391 wakeup = pcie_pme_check_wakeup(port->subordinate);
392 up_read(&pci_bus_sem);
394 if (wakeup) {
395 ret = enable_irq_wake(srv->irq);
396 if (!ret)
397 return 0;
400 pcie_pme_disable_interrupt(port, data);
402 synchronize_irq(srv->irq);
404 return 0;
408 * pcie_pme_resume - Resume PCIe PME service device.
409 * @srv - PCIe service device to resume.
411 static int pcie_pme_resume(struct pcie_device *srv)
413 struct pcie_pme_service_data *data = get_service_data(srv);
415 spin_lock_irq(&data->lock);
416 if (data->noirq) {
417 struct pci_dev *port = srv->port;
419 pcie_clear_root_pme_status(port);
420 pcie_pme_interrupt_enable(port, true);
421 data->noirq = false;
422 } else {
423 disable_irq_wake(srv->irq);
425 spin_unlock_irq(&data->lock);
427 return 0;
431 * pcie_pme_remove - Prepare PCIe PME service device for removal.
432 * @srv - PCIe service device to remove.
434 static void pcie_pme_remove(struct pcie_device *srv)
436 struct pcie_pme_service_data *data = get_service_data(srv);
438 pcie_pme_disable_interrupt(srv->port, data);
439 free_irq(srv->irq, srv);
440 cancel_work_sync(&data->work);
441 kfree(data);
444 static struct pcie_port_service_driver pcie_pme_driver = {
445 .name = "pcie_pme",
446 .port_type = PCI_EXP_TYPE_ROOT_PORT,
447 .service = PCIE_PORT_SERVICE_PME,
449 .probe = pcie_pme_probe,
450 .suspend = pcie_pme_suspend,
451 .resume = pcie_pme_resume,
452 .remove = pcie_pme_remove,
456 * pcie_pme_service_init - Register the PCIe PME service driver.
458 int __init pcie_pme_init(void)
460 return pcie_port_service_register(&pcie_pme_driver);