1 // SPDX-License-Identifier: GPL-2.0
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.
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>
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;
37 __setup("pcie_pme=", pcie_pme_setup
);
39 struct pcie_pme_service_data
{
41 struct pcie_device
*srv
;
42 struct work_struct work
;
43 bool noirq
; /* If set, keep the PME interrupt disabled. */
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
)
54 pcie_capability_set_word(dev
, PCI_EXP_RTCTL
,
57 pcie_capability_clear_word(dev
, PCI_EXP_RTCTL
,
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
)
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
)) {
76 dev
->pme_poll
= false;
78 pci_wakeup_event(dev
);
79 pm_request_resume(&dev
->dev
);
83 if (dev
->subordinate
&& pcie_pme_walk_bus(dev
->subordinate
))
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
)
107 dev
= pci_dev_get(bus
->self
);
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
))
115 up_read(&pci_bus_sem
);
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;
134 /* First, check if the PME is from the root port itself. */
135 if (port
->devfn
== devfn
&& port
->bus
->number
== busnr
) {
137 port
->pme_poll
= false;
139 if (pci_check_pme_status(port
)) {
140 pm_request_resume(&port
->dev
);
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
);
158 /* Second, find the bus the source device is on. */
159 bus
= pci_find_bus(pci_domain_nr(port
->bus
), busnr
);
163 /* Next, check if the PME is from a PCIe-PCI bridge. */
164 found
= pcie_pme_from_pci_bridge(bus
, devfn
);
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
) {
172 if (dev
->devfn
== devfn
) {
178 up_read(&pci_bus_sem
);
181 /* The device is there, but we have to check its PME status. */
182 found
= pci_check_pme_status(dev
);
185 dev
->pme_poll
= false;
187 pci_wakeup_event(dev
);
188 pm_request_resume(&dev
->dev
);
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);
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
;
218 spin_lock_irq(&data
->lock
);
224 pcie_capability_read_dword(port
, PCI_EXP_RTSTA
, &rtsta
);
225 if (rtsta
== (u32
) ~0)
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
);
242 /* No need to loop if there are no more PMEs pending. */
243 if (!(rtsta
& PCI_EXP_RTSTA_PENDING
))
246 spin_unlock_irq(&data
->lock
);
248 spin_lock_irq(&data
->lock
);
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
;
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
);
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
);
290 * pcie_pme_can_wakeup - Set the wakeup capability flag.
291 * @dev: PCI device to handle.
294 static int pcie_pme_can_wakeup(struct pci_dev
*dev
, void *ign
)
296 device_set_wakeup_capable(&dev
->dev
, true);
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
;
325 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
329 spin_lock_init(&data
->lock
);
330 INIT_WORK(&data
->work
, pcie_pme_work_fn
);
332 set_service_data(srv
, data
);
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
);
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);
351 static bool pcie_pme_check_wakeup(struct pci_bus
*bus
)
358 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
359 if (device_may_wakeup(&dev
->dev
)
360 || pcie_pme_check_wakeup(dev
->subordinate
))
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
);
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
;
387 if (device_may_wakeup(&port
->dev
)) {
390 down_read(&pci_bus_sem
);
391 wakeup
= pcie_pme_check_wakeup(port
->subordinate
);
392 up_read(&pci_bus_sem
);
395 ret
= enable_irq_wake(srv
->irq
);
400 pcie_pme_disable_interrupt(port
, data
);
402 synchronize_irq(srv
->irq
);
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
);
417 struct pci_dev
*port
= srv
->port
;
419 pcie_clear_root_pme_status(port
);
420 pcie_pme_interrupt_enable(port
, true);
423 disable_irq_wake(srv
->irq
);
425 spin_unlock_irq(&data
->lock
);
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
);
444 static struct pcie_port_service_driver pcie_pme_driver
= {
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
);