pme.c 11 KB

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