pme.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. #include <linux/module.h>
  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/acpi.h>
  22. #include <linux/pci-acpi.h>
  23. #include <linux/pm_runtime.h>
  24. #include "../pci.h"
  25. #include "portdrv.h"
  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. static int __init pcie_pme_setup(char *str)
  34. {
  35. if (!strncmp(str, "nomsi", 5))
  36. pcie_pme_msi_disabled = true;
  37. return 1;
  38. }
  39. __setup("pcie_pme=", pcie_pme_setup);
  40. struct pcie_pme_service_data {
  41. spinlock_t lock;
  42. struct pcie_device *srv;
  43. struct work_struct work;
  44. bool noirq; /* Don't enable the PME interrupt used by this service. */
  45. };
  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.
  50. */
  51. void pcie_pme_interrupt_enable(struct pci_dev *dev, bool enable)
  52. {
  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);
  59. }
  60. /**
  61. * pcie_pme_walk_bus - Scan a PCI bus for devices asserting PME#.
  62. * @bus: PCI bus to scan.
  63. *
  64. * Scan given PCI bus and all buses under it for devices asserting PME#.
  65. */
  66. static bool pcie_pme_walk_bus(struct pci_bus *bus)
  67. {
  68. struct pci_dev *dev;
  69. bool ret = false;
  70. list_for_each_entry(dev, &bus->devices, bus_list) {
  71. /* Skip PCIe devices in case we started from a root port. */
  72. if (!pci_is_pcie(dev) && pci_check_pme_status(dev)) {
  73. if (dev->pme_poll)
  74. dev->pme_poll = false;
  75. pci_wakeup_event(dev);
  76. pm_request_resume(&dev->dev);
  77. ret = true;
  78. }
  79. if (dev->subordinate && pcie_pme_walk_bus(dev->subordinate))
  80. ret = true;
  81. }
  82. return ret;
  83. }
  84. /**
  85. * pcie_pme_from_pci_bridge - Check if PCIe-PCI bridge generated a PME.
  86. * @bus: Secondary bus of the bridge.
  87. * @devfn: Device/function number to check.
  88. *
  89. * PME from PCI devices under a PCIe-PCI bridge may be converted to an in-band
  90. * PCIe PME message. In such that case the bridge should use the Requester ID
  91. * of device/function number 0 on its secondary bus.
  92. */
  93. static bool pcie_pme_from_pci_bridge(struct pci_bus *bus, u8 devfn)
  94. {
  95. struct pci_dev *dev;
  96. bool found = false;
  97. if (devfn)
  98. return false;
  99. dev = pci_dev_get(bus->self);
  100. if (!dev)
  101. return false;
  102. if (pci_is_pcie(dev) && pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
  103. down_read(&pci_bus_sem);
  104. if (pcie_pme_walk_bus(bus))
  105. found = true;
  106. up_read(&pci_bus_sem);
  107. }
  108. pci_dev_put(dev);
  109. return found;
  110. }
  111. /**
  112. * pcie_pme_handle_request - Find device that generated PME and handle it.
  113. * @port: Root port or event collector that generated the PME interrupt.
  114. * @req_id: PCIe Requester ID of the device that generated the PME.
  115. */
  116. static void pcie_pme_handle_request(struct pci_dev *port, u16 req_id)
  117. {
  118. u8 busnr = req_id >> 8, devfn = req_id & 0xff;
  119. struct pci_bus *bus;
  120. struct pci_dev *dev;
  121. bool found = false;
  122. /* First, check if the PME is from the root port itself. */
  123. if (port->devfn == devfn && port->bus->number == busnr) {
  124. if (port->pme_poll)
  125. port->pme_poll = false;
  126. if (pci_check_pme_status(port)) {
  127. pm_request_resume(&port->dev);
  128. found = true;
  129. } else {
  130. /*
  131. * Apparently, the root port generated the PME on behalf
  132. * of a non-PCIe device downstream. If this is done by
  133. * a root port, the Requester ID field in its status
  134. * register may contain either the root port's, or the
  135. * source device's information (PCI Express Base
  136. * Specification, Rev. 2.0, Section 6.1.9).
  137. */
  138. down_read(&pci_bus_sem);
  139. found = pcie_pme_walk_bus(port->subordinate);
  140. up_read(&pci_bus_sem);
  141. }
  142. goto out;
  143. }
  144. /* Second, find the bus the source device is on. */
  145. bus = pci_find_bus(pci_domain_nr(port->bus), busnr);
  146. if (!bus)
  147. goto out;
  148. /* Next, check if the PME is from a PCIe-PCI bridge. */
  149. found = pcie_pme_from_pci_bridge(bus, devfn);
  150. if (found)
  151. goto out;
  152. /* Finally, try to find the PME source on the bus. */
  153. down_read(&pci_bus_sem);
  154. list_for_each_entry(dev, &bus->devices, bus_list) {
  155. pci_dev_get(dev);
  156. if (dev->devfn == devfn) {
  157. found = true;
  158. break;
  159. }
  160. pci_dev_put(dev);
  161. }
  162. up_read(&pci_bus_sem);
  163. if (found) {
  164. /* The device is there, but we have to check its PME status. */
  165. found = pci_check_pme_status(dev);
  166. if (found) {
  167. if (dev->pme_poll)
  168. dev->pme_poll = false;
  169. pci_wakeup_event(dev);
  170. pm_request_resume(&dev->dev);
  171. }
  172. pci_dev_put(dev);
  173. } else if (devfn) {
  174. /*
  175. * The device is not there, but we can still try to recover by
  176. * assuming that the PME was reported by a PCIe-PCI bridge that
  177. * used devfn different from zero.
  178. */
  179. dev_dbg(&port->dev, "PME interrupt generated for "
  180. "non-existent device %02x:%02x.%d\n",
  181. busnr, PCI_SLOT(devfn), PCI_FUNC(devfn));
  182. found = pcie_pme_from_pci_bridge(bus, 0);
  183. }
  184. out:
  185. if (!found)
  186. dev_dbg(&port->dev, "Spurious native PME interrupt!\n");
  187. }
  188. /**
  189. * pcie_pme_work_fn - Work handler for PCIe PME interrupt.
  190. * @work: Work structure giving access to service data.
  191. */
  192. static void pcie_pme_work_fn(struct work_struct *work)
  193. {
  194. struct pcie_pme_service_data *data =
  195. container_of(work, struct pcie_pme_service_data, work);
  196. struct pci_dev *port = data->srv->port;
  197. u32 rtsta;
  198. spin_lock_irq(&data->lock);
  199. for (;;) {
  200. if (data->noirq)
  201. break;
  202. pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
  203. if (rtsta & PCI_EXP_RTSTA_PME) {
  204. /*
  205. * Clear PME status of the port. If there are other
  206. * pending PMEs, the status will be set again.
  207. */
  208. pcie_clear_root_pme_status(port);
  209. spin_unlock_irq(&data->lock);
  210. pcie_pme_handle_request(port, rtsta & 0xffff);
  211. spin_lock_irq(&data->lock);
  212. continue;
  213. }
  214. /* No need to loop if there are no more PMEs pending. */
  215. if (!(rtsta & PCI_EXP_RTSTA_PENDING))
  216. break;
  217. spin_unlock_irq(&data->lock);
  218. cpu_relax();
  219. spin_lock_irq(&data->lock);
  220. }
  221. if (!data->noirq)
  222. pcie_pme_interrupt_enable(port, true);
  223. spin_unlock_irq(&data->lock);
  224. }
  225. /**
  226. * pcie_pme_irq - Interrupt handler for PCIe root port PME interrupt.
  227. * @irq: Interrupt vector.
  228. * @context: Interrupt context pointer.
  229. */
  230. static irqreturn_t pcie_pme_irq(int irq, void *context)
  231. {
  232. struct pci_dev *port;
  233. struct pcie_pme_service_data *data;
  234. u32 rtsta;
  235. unsigned long flags;
  236. port = ((struct pcie_device *)context)->port;
  237. data = get_service_data((struct pcie_device *)context);
  238. spin_lock_irqsave(&data->lock, flags);
  239. pcie_capability_read_dword(port, PCI_EXP_RTSTA, &rtsta);
  240. if (!(rtsta & PCI_EXP_RTSTA_PME)) {
  241. spin_unlock_irqrestore(&data->lock, flags);
  242. return IRQ_NONE;
  243. }
  244. pcie_pme_interrupt_enable(port, false);
  245. spin_unlock_irqrestore(&data->lock, flags);
  246. /* We don't use pm_wq, because it's freezable. */
  247. schedule_work(&data->work);
  248. return IRQ_HANDLED;
  249. }
  250. /**
  251. * pcie_pme_set_native - Set the PME interrupt flag for given device.
  252. * @dev: PCI device to handle.
  253. * @ign: Ignored.
  254. */
  255. static int pcie_pme_set_native(struct pci_dev *dev, void *ign)
  256. {
  257. dev_info(&dev->dev, "Signaling PME through PCIe PME interrupt\n");
  258. device_set_run_wake(&dev->dev, true);
  259. dev->pme_interrupt = true;
  260. return 0;
  261. }
  262. /**
  263. * pcie_pme_mark_devices - Set the PME interrupt flag for devices below a port.
  264. * @port: PCIe root port or event collector to handle.
  265. *
  266. * For each device below given root port, including the port itself (or for each
  267. * root complex integrated endpoint if @port is a root complex event collector)
  268. * set the flag indicating that it can signal run-time wake-up events via PCIe
  269. * PME interrupts.
  270. */
  271. static void pcie_pme_mark_devices(struct pci_dev *port)
  272. {
  273. pcie_pme_set_native(port, NULL);
  274. if (port->subordinate) {
  275. pci_walk_bus(port->subordinate, pcie_pme_set_native, NULL);
  276. } else {
  277. struct pci_bus *bus = port->bus;
  278. struct pci_dev *dev;
  279. /* Check if this is a root port event collector. */
  280. if (pci_pcie_type(port) != PCI_EXP_TYPE_RC_EC || !bus)
  281. return;
  282. down_read(&pci_bus_sem);
  283. list_for_each_entry(dev, &bus->devices, bus_list)
  284. if (pci_is_pcie(dev)
  285. && pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
  286. pcie_pme_set_native(dev, NULL);
  287. up_read(&pci_bus_sem);
  288. }
  289. }
  290. /**
  291. * pcie_pme_probe - Initialize PCIe PME service for given root port.
  292. * @srv: PCIe service to initialize.
  293. */
  294. static int pcie_pme_probe(struct pcie_device *srv)
  295. {
  296. struct pci_dev *port;
  297. struct pcie_pme_service_data *data;
  298. int ret;
  299. data = kzalloc(sizeof(*data), GFP_KERNEL);
  300. if (!data)
  301. return -ENOMEM;
  302. spin_lock_init(&data->lock);
  303. INIT_WORK(&data->work, pcie_pme_work_fn);
  304. data->srv = srv;
  305. set_service_data(srv, data);
  306. port = srv->port;
  307. pcie_pme_interrupt_enable(port, false);
  308. pcie_clear_root_pme_status(port);
  309. ret = request_irq(srv->irq, pcie_pme_irq, IRQF_SHARED, "PCIe PME", srv);
  310. if (ret) {
  311. kfree(data);
  312. } else {
  313. pcie_pme_mark_devices(port);
  314. pcie_pme_interrupt_enable(port, true);
  315. }
  316. return ret;
  317. }
  318. /**
  319. * pcie_pme_suspend - Suspend PCIe PME service device.
  320. * @srv: PCIe service device to suspend.
  321. */
  322. static int pcie_pme_suspend(struct pcie_device *srv)
  323. {
  324. struct pcie_pme_service_data *data = get_service_data(srv);
  325. struct pci_dev *port = srv->port;
  326. spin_lock_irq(&data->lock);
  327. pcie_pme_interrupt_enable(port, false);
  328. pcie_clear_root_pme_status(port);
  329. data->noirq = true;
  330. spin_unlock_irq(&data->lock);
  331. synchronize_irq(srv->irq);
  332. return 0;
  333. }
  334. /**
  335. * pcie_pme_resume - Resume PCIe PME service device.
  336. * @srv - PCIe service device to resume.
  337. */
  338. static int pcie_pme_resume(struct pcie_device *srv)
  339. {
  340. struct pcie_pme_service_data *data = get_service_data(srv);
  341. struct pci_dev *port = srv->port;
  342. spin_lock_irq(&data->lock);
  343. data->noirq = false;
  344. pcie_clear_root_pme_status(port);
  345. pcie_pme_interrupt_enable(port, true);
  346. spin_unlock_irq(&data->lock);
  347. return 0;
  348. }
  349. /**
  350. * pcie_pme_remove - Prepare PCIe PME service device for removal.
  351. * @srv - PCIe service device to resume.
  352. */
  353. static void pcie_pme_remove(struct pcie_device *srv)
  354. {
  355. pcie_pme_suspend(srv);
  356. free_irq(srv->irq, srv);
  357. kfree(get_service_data(srv));
  358. }
  359. static struct pcie_port_service_driver pcie_pme_driver = {
  360. .name = "pcie_pme",
  361. .port_type = PCI_EXP_TYPE_ROOT_PORT,
  362. .service = PCIE_PORT_SERVICE_PME,
  363. .probe = pcie_pme_probe,
  364. .suspend = pcie_pme_suspend,
  365. .resume = pcie_pme_resume,
  366. .remove = pcie_pme_remove,
  367. };
  368. /**
  369. * pcie_pme_service_init - Register the PCIe PME service driver.
  370. */
  371. static int __init pcie_pme_service_init(void)
  372. {
  373. return pcie_port_service_register(&pcie_pme_driver);
  374. }
  375. module_init(pcie_pme_service_init);