pme.c 11 KB

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