0001
0002
0003
0004
0005
0006
0007
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
0026
0027
0028
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;
0046 };
0047
0048
0049
0050
0051
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
0065
0066
0067
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
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
0094
0095
0096
0097
0098
0099
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
0126
0127
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
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
0147
0148
0149
0150
0151
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
0161 bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
0162 if (!bus)
0163 goto out;
0164
0165
0166 found = pcie_pme_from_pci_bridge(bus, devfn);
0167 if (found)
0168 goto out;
0169
0170
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
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
0196
0197
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
0211
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
0233
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
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
0261
0262
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
0286 schedule_work(&data->work);
0287
0288 return IRQ_HANDLED;
0289 }
0290
0291
0292
0293
0294
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
0304
0305
0306
0307
0308
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
0322
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
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
0388
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
0419
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
0442
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
0467
0468 int __init pcie_pme_init(void)
0469 {
0470 return pcie_port_service_register(&pcie_pme_driver);
0471 }