Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * PCIe Native PME support
0004  *
0005  * Copyright (C) 2007 - 2009 Intel Corp
0006  * Copyright (C) 2007 - 2009 Shaohua Li <shaohua.li@intel.com>
0007  * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
0008  */
0009 
0010 #define dev_fmt(fmt) "PME: " fmt
0011 
0012 #include <linux/pci.h>
0013 #include <linux/kernel.h>
0014 #include <linux/errno.h>
0015 #include <linux/slab.h>
0016 #include <linux/init.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/device.h>
0019 #include <linux/pm_runtime.h>
0020 
0021 #include "../pci.h"
0022 #include "portdrv.h"
0023 
0024 /*
0025  * If this switch is set, MSI will not be used for PCIe PME signaling.  This
0026  * causes the PCIe port driver to use INTx interrupts only, but it turns out
0027  * that using MSI for PCIe PME signaling doesn't play well with PCIe PME-based
0028  * wake-up from system sleep states.
0029  */
0030 bool pcie_pme_msi_disabled;
0031 
0032 static int __init pcie_pme_setup(char *str)
0033 {
0034     if (!strncmp(str, "nomsi", 5))
0035         pcie_pme_msi_disabled = true;
0036 
0037     return 1;
0038 }
0039 __setup("pcie_pme=", pcie_pme_setup);
0040 
0041 struct pcie_pme_service_data {
0042     spinlock_t lock;
0043     struct pcie_device *srv;
0044     struct work_struct work;
0045     bool noirq; /* If set, keep the PME interrupt disabled. */
0046 };
0047 
0048 /**
0049  * pcie_pme_interrupt_enable - Enable/disable PCIe PME interrupt generation.
0050  * @dev: PCIe root port or event collector.
0051  * @enable: Enable or disable the interrupt.
0052  */
0053 void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
0054 {
0055     if (enable)
0056         pcie_capability_set_word(dev, PCI_EXP_RTCTL,
0057                      PCI_EXP_RTCTL_PMEIE);
0058     else
0059         pcie_capability_clear_word(dev, PCI_EXP_RTCTL,
0060                        PCI_EXP_RTCTL_PMEIE);
0061 }
0062 
0063 /**
0064  * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
0065  * @bus: PCI bus to scan.
0066  *
0067  * Scan given PCI bus and all buses under it for devices asserting PME#.
0068  */
0069 static bool pcie_pme_walk_bus(struct pci_bus *bus)
0070 {
0071     struct pci_dev *dev;
0072     bool ret = false;
0073 
0074     list_for_each_entry(dev, &bus->devices, bus_list) {
0075         /* Skip PCIe devices in case we started from a root port. */
0076         if (!pci_is_pcie(dev) && pci_check_pme_status(dev)) {
0077             if (dev->pme_poll)
0078                 dev->pme_poll = false;
0079 
0080             pci_wakeup_event(dev);
0081             pm_request_resume(&dev->dev);
0082             ret = true;
0083         }
0084 
0085         if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
0086             ret = true;
0087     }
0088 
0089     return ret;
0090 }
0091 
0092 /**
0093  * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
0094  * @bus: Secondary bus of the bridge.
0095  * @devfn: Device/function number to check.
0096  *
0097  * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
0098  * PCIe PME message.  In such that case the bridge should use the Requester ID
0099  * of device/function number 0 on its secondary bus.
0100  */
0101 static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
0102 {
0103     struct pci_dev *dev;
0104     bool found = false;
0105 
0106     if (devfn)
0107         return false;
0108 
0109     dev = pci_dev_get(bus->self);
0110     if (!dev)
0111         return false;
0112 
0113     if (pci_is_pcie(dev) && pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
0114         down_read(&pci_bus_sem);
0115         if (pcie_pme_walk_bus(bus))
0116             found = true;
0117         up_read(&pci_bus_sem);
0118     }
0119 
0120     pci_dev_put(dev);
0121     return found;
0122 }
0123 
0124 /**
0125  * pcie_pme_handle_request - Find device that generated PME and handle it.
0126  * @port: Root port or event collector that generated the PME interrupt.
0127  * @req_id: PCIe Requester ID of the device that generated the PME.
0128  */
0129 static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
0130 {
0131     u8 busnr = req_id >> 8, devfn = req_id & 0xff;
0132     struct pci_bus *bus;
0133     struct pci_dev *dev;
0134     bool found = false;
0135 
0136     /* First, check if the PME is from the root port itself. */
0137     if (port->devfn == devfn && port->bus->number == busnr) {
0138         if (port->pme_poll)
0139             port->pme_poll = false;
0140 
0141         if (pci_check_pme_status(port)) {
0142             pm_request_resume(&port->dev);
0143             found = true;
0144         } else {
0145             /*
0146              * Apparently, the root port generated the PME on behalf
0147              * of a non-PCIe device downstream.  If this is done by
0148              * a root port, the Requester ID field in its status
0149              * register may contain either the root port's, or the
0150              * source device's information (PCI Express Base
0151              * Specification, Rev. 2.0, Section 6.1.9).
0152              */
0153             down_read(&pci_bus_sem);
0154             found = pcie_pme_walk_bus(port->subordinate);
0155             up_read(&pci_bus_sem);
0156         }
0157         goto out;
0158     }
0159 
0160     /* Second, find the bus the source device is on. */
0161     bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
0162     if (!bus)
0163         goto out;
0164 
0165     /* Next, check if the PME is from a PCIe-PCI bridge. */
0166     found = pcie_pme_from_pci_bridge(bus, devfn);
0167     if (found)
0168         goto out;
0169 
0170     /* Finally, try to find the PME source on the bus. */
0171     down_read(&pci_bus_sem);
0172     list_for_each_entry(dev, &bus->devices, bus_list) {
0173         pci_dev_get(dev);
0174         if (dev->devfn == devfn) {
0175             found = true;
0176             break;
0177         }
0178         pci_dev_put(dev);
0179     }
0180     up_read(&pci_bus_sem);
0181 
0182     if (found) {
0183         /* The device is there, but we have to check its PME status. */
0184         found = pci_check_pme_status(dev);
0185         if (found) {
0186             if (dev->pme_poll)
0187                 dev->pme_poll = false;
0188 
0189             pci_wakeup_event(dev);
0190             pm_request_resume(&dev->dev);
0191         }
0192         pci_dev_put(dev);
0193     } else if (devfn) {
0194         /*
0195          * The device is not there, but we can still try to recover by
0196          * assuming that the PME was reported by a PCIe-PCI bridge that
0197          * used devfn different from zero.
0198          */
0199         pci_info(port, "interrupt generated for non-existent device %02x:%02x.%d\n",
0200              busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
0201         found = pcie_pme_from_pci_bridge(bus, 0);
0202     }
0203 
0204  out:
0205     if (!found)
0206         pci_info(port, "Spurious native interrupt!\n");
0207 }
0208 
0209 /**
0210  * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
0211  * @work: Work structure giving access to service data.
0212  */
0213 static void pcie_pme_work_fn(struct work_struct *work)
0214 {
0215     struct pcie_pme_service_data *data =
0216             container_of(work, struct pcie_pme_service_data, work);
0217     struct pci_dev *port = data->srv->port;
0218     u32 rtsta;
0219 
0220     spin_lock_irq(&data->lock);
0221 
0222     for (;;) {
0223         if (data->noirq)
0224             break;
0225 
0226         pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
0227         if (PCI_POSSIBLE_ERROR(rtsta))
0228             break;
0229 
0230         if (rtsta & PCI_EXP_RTSTA_PME) {
0231             /*
0232              * Clear PME status of the port.  If there are other
0233              * pending PMEs, the status will be set again.
0234              */
0235             pcie_clear_root_pme_status(port);
0236 
0237             spin_unlock_irq(&data->lock);
0238             pcie_pme_handle_request(port, rtsta & 0xffff);
0239             spin_lock_irq(&data->lock);
0240 
0241             continue;
0242         }
0243 
0244         /* No need to loop if there are no more PMEs pending. */
0245         if (!(rtsta & PCI_EXP_RTSTA_PENDING))
0246             break;
0247 
0248         spin_unlock_irq(&data->lock);
0249         cpu_relax();
0250         spin_lock_irq(&data->lock);
0251     }
0252 
0253     if (!data->noirq)
0254         pcie_pme_interrupt_enable(port, true);
0255 
0256     spin_unlock_irq(&data->lock);
0257 }
0258 
0259 /**
0260  * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
0261  * @irq: Interrupt vector.
0262  * @context: Interrupt context pointer.
0263  */
0264 static irqreturn_t pcie_pme_irq(int irq, void *context)
0265 {
0266     struct pci_dev *port;
0267     struct pcie_pme_service_data *data;
0268     u32 rtsta;
0269     unsigned long flags;
0270 
0271     port = ((struct pcie_device *)context)->port;
0272     data = get_service_data((struct pcie_device *)context);
0273 
0274     spin_lock_irqsave(&data->lock, flags);
0275     pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
0276 
0277     if (PCI_POSSIBLE_ERROR(rtsta) || !(rtsta & PCI_EXP_RTSTA_PME)) {
0278         spin_unlock_irqrestore(&data->lock, flags);
0279         return IRQ_NONE;
0280     }
0281 
0282     pcie_pme_interrupt_enable(port, false);
0283     spin_unlock_irqrestore(&data->lock, flags);
0284 
0285     /* We don't use pm_wq, because it's freezable. */
0286     schedule_work(&data->work);
0287 
0288     return IRQ_HANDLED;
0289 }
0290 
0291 /**
0292  * pcie_pme_can_wakeup - Set the wakeup capability flag.
0293  * @dev: PCI device to handle.
0294  * @ign: Ignored.
0295  */
0296 static int pcie_pme_can_wakeup(struct pci_dev *dev, void *ign)
0297 {
0298     device_set_wakeup_capable(&dev->dev, true);
0299     return 0;
0300 }
0301 
0302 /**
0303  * pcie_pme_mark_devices - Set the wakeup flag for devices below a port.
0304  * @port: PCIe root port or event collector to handle.
0305  *
0306  * For each device below given root port, including the port itself (or for each
0307  * root complex integrated endpoint if @port is a root complex event collector)
0308  * set the flag indicating that it can signal run-time wake-up events.
0309  */
0310 static void pcie_pme_mark_devices(struct pci_dev *port)
0311 {
0312     pcie_pme_can_wakeup(port, NULL);
0313 
0314     if (pci_pcie_type(port) == PCI_EXP_TYPE_RC_EC)
0315         pcie_walk_rcec(port, pcie_pme_can_wakeup, NULL);
0316     else if (port->subordinate)
0317         pci_walk_bus(port->subordinate, pcie_pme_can_wakeup, NULL);
0318 }
0319 
0320 /**
0321  * pcie_pme_probe - Initialize PCIe PME service for given root port.
0322  * @srv: PCIe service to initialize.
0323  */
0324 static int pcie_pme_probe(struct pcie_device *srv)
0325 {
0326     struct pci_dev *port = srv->port;
0327     struct pcie_pme_service_data *data;
0328     int type = pci_pcie_type(port);
0329     int ret;
0330 
0331     /* Limit to Root Ports or Root Complex Event Collectors */
0332     if (type != PCI_EXP_TYPE_RC_EC &&
0333         type != PCI_EXP_TYPE_ROOT_PORT)
0334         return -ENODEV;
0335 
0336     data = kzalloc(sizeof(*data), GFP_KERNEL);
0337     if (!data)
0338         return -ENOMEM;
0339 
0340     spin_lock_init(&data->lock);
0341     INIT_WORK(&data->work, pcie_pme_work_fn);
0342     data->srv = srv;
0343     set_service_data(srv, data);
0344 
0345     pcie_pme_interrupt_enable(port, false);
0346     pcie_clear_root_pme_status(port);
0347 
0348     ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
0349     if (ret) {
0350         kfree(data);
0351         return ret;
0352     }
0353 
0354     pci_info(port, "Signaling with IRQ %d\n", srv->irq);
0355 
0356     pcie_pme_mark_devices(port);
0357     pcie_pme_interrupt_enable(port, true);
0358     return 0;
0359 }
0360 
0361 static bool pcie_pme_check_wakeup(struct pci_bus *bus)
0362 {
0363     struct pci_dev *dev;
0364 
0365     if (!bus)
0366         return false;
0367 
0368     list_for_each_entry(dev, &bus->devices, bus_list)
0369         if (device_may_wakeup(&dev->dev)
0370             || pcie_pme_check_wakeup(dev->subordinate))
0371             return true;
0372 
0373     return false;
0374 }
0375 
0376 static void pcie_pme_disable_interrupt(struct pci_dev *port,
0377                        struct pcie_pme_service_data *data)
0378 {
0379     spin_lock_irq(&data->lock);
0380     pcie_pme_interrupt_enable(port, false);
0381     pcie_clear_root_pme_status(port);
0382     data->noirq = true;
0383     spin_unlock_irq(&data->lock);
0384 }
0385 
0386 /**
0387  * pcie_pme_suspend - Suspend PCIe PME service device.
0388  * @srv: PCIe service device to suspend.
0389  */
0390 static int pcie_pme_suspend(struct pcie_device *srv)
0391 {
0392     struct pcie_pme_service_data *data = get_service_data(srv);
0393     struct pci_dev *port = srv->port;
0394     bool wakeup;
0395     int ret;
0396 
0397     if (device_may_wakeup(&port->dev)) {
0398         wakeup = true;
0399     } else {
0400         down_read(&pci_bus_sem);
0401         wakeup = pcie_pme_check_wakeup(port->subordinate);
0402         up_read(&pci_bus_sem);
0403     }
0404     if (wakeup) {
0405         ret = enable_irq_wake(srv->irq);
0406         if (!ret)
0407             return 0;
0408     }
0409 
0410     pcie_pme_disable_interrupt(port, data);
0411 
0412     synchronize_irq(srv->irq);
0413 
0414     return 0;
0415 }
0416 
0417 /**
0418  * pcie_pme_resume - Resume PCIe PME service device.
0419  * @srv: PCIe service device to resume.
0420  */
0421 static int pcie_pme_resume(struct pcie_device *srv)
0422 {
0423     struct pcie_pme_service_data *data = get_service_data(srv);
0424 
0425     spin_lock_irq(&data->lock);
0426     if (data->noirq) {
0427         struct pci_dev *port = srv->port;
0428 
0429         pcie_clear_root_pme_status(port);
0430         pcie_pme_interrupt_enable(port, true);
0431         data->noirq = false;
0432     } else {
0433         disable_irq_wake(srv->irq);
0434     }
0435     spin_unlock_irq(&data->lock);
0436 
0437     return 0;
0438 }
0439 
0440 /**
0441  * pcie_pme_remove - Prepare PCIe PME service device for removal.
0442  * @srv: PCIe service device to remove.
0443  */
0444 static void pcie_pme_remove(struct pcie_device *srv)
0445 {
0446     struct pcie_pme_service_data *data = get_service_data(srv);
0447 
0448     pcie_pme_disable_interrupt(srv->port, data);
0449     free_irq(srv->irq, srv);
0450     cancel_work_sync(&data->work);
0451     kfree(data);
0452 }
0453 
0454 static struct pcie_port_service_driver pcie_pme_driver = {
0455     .name       = "pcie_pme",
0456     .port_type  = PCIE_ANY_PORT,
0457     .service    = PCIE_PORT_SERVICE_PME,
0458 
0459     .probe      = pcie_pme_probe,
0460     .suspend    = pcie_pme_suspend,
0461     .resume     = pcie_pme_resume,
0462     .remove     = pcie_pme_remove,
0463 };
0464 
0465 /**
0466  * pcie_pme_init - Register the PCIe PME service driver.
0467  */
0468 int __init pcie_pme_init(void)
0469 {
0470     return pcie_port_service_register(&pcie_pme_driver);
0471 }