portdrv_pci.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Purpose: PCI Express Port Bus Driver
  4. * Author: Tom Nguyen <tom.l.nguyen@intel.com>
  5. *
  6. * Copyright (C) 2004 Intel
  7. * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
  8. */
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/pm.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/init.h>
  15. #include <linux/pcieport_if.h>
  16. #include <linux/aer.h>
  17. #include <linux/dmi.h>
  18. #include <linux/pci-aspm.h>
  19. #include "../pci.h"
  20. #include "portdrv.h"
  21. /* If this switch is set, PCIe port native services should not be enabled. */
  22. bool pcie_ports_disabled;
  23. /*
  24. * If this switch is set, ACPI _OSC will be used to determine whether or not to
  25. * enable PCIe port native services.
  26. */
  27. bool pcie_ports_auto = true;
  28. static int __init pcie_port_setup(char *str)
  29. {
  30. if (!strncmp(str, "compat", 6)) {
  31. pcie_ports_disabled = true;
  32. } else if (!strncmp(str, "native", 6)) {
  33. pcie_ports_disabled = false;
  34. pcie_ports_auto = false;
  35. } else if (!strncmp(str, "auto", 4)) {
  36. pcie_ports_disabled = false;
  37. pcie_ports_auto = true;
  38. }
  39. return 1;
  40. }
  41. __setup("pcie_ports=", pcie_port_setup);
  42. /* global data */
  43. /**
  44. * pcie_clear_root_pme_status - Clear root port PME interrupt status.
  45. * @dev: PCIe root port or event collector.
  46. */
  47. void pcie_clear_root_pme_status(struct pci_dev *dev)
  48. {
  49. pcie_capability_set_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME);
  50. }
  51. static int pcie_portdrv_restore_config(struct pci_dev *dev)
  52. {
  53. int retval;
  54. retval = pci_enable_device(dev);
  55. if (retval)
  56. return retval;
  57. pci_set_master(dev);
  58. return 0;
  59. }
  60. #ifdef CONFIG_PM
  61. static int pcie_port_resume_noirq(struct device *dev)
  62. {
  63. struct pci_dev *pdev = to_pci_dev(dev);
  64. /*
  65. * Some BIOSes forget to clear Root PME Status bits after system wakeup
  66. * which breaks ACPI-based runtime wakeup on PCI Express, so clear those
  67. * bits now just in case (shouldn't hurt).
  68. */
  69. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT)
  70. pcie_clear_root_pme_status(pdev);
  71. return 0;
  72. }
  73. static int pcie_port_runtime_suspend(struct device *dev)
  74. {
  75. return to_pci_dev(dev)->bridge_d3 ? 0 : -EBUSY;
  76. }
  77. static int pcie_port_runtime_resume(struct device *dev)
  78. {
  79. return 0;
  80. }
  81. static int pcie_port_runtime_idle(struct device *dev)
  82. {
  83. /*
  84. * Assume the PCI core has set bridge_d3 whenever it thinks the port
  85. * should be good to go to D3. Everything else, including moving
  86. * the port to D3, is handled by the PCI core.
  87. */
  88. return to_pci_dev(dev)->bridge_d3 ? 0 : -EBUSY;
  89. }
  90. static const struct dev_pm_ops pcie_portdrv_pm_ops = {
  91. .suspend = pcie_port_device_suspend,
  92. .resume = pcie_port_device_resume,
  93. .freeze = pcie_port_device_suspend,
  94. .thaw = pcie_port_device_resume,
  95. .poweroff = pcie_port_device_suspend,
  96. .restore = pcie_port_device_resume,
  97. .resume_noirq = pcie_port_resume_noirq,
  98. .runtime_suspend = pcie_port_runtime_suspend,
  99. .runtime_resume = pcie_port_runtime_resume,
  100. .runtime_idle = pcie_port_runtime_idle,
  101. };
  102. #define PCIE_PORTDRV_PM_OPS (&pcie_portdrv_pm_ops)
  103. #else /* !PM */
  104. #define PCIE_PORTDRV_PM_OPS NULL
  105. #endif /* !PM */
  106. /*
  107. * pcie_portdrv_probe - Probe PCI-Express port devices
  108. * @dev: PCI-Express port device being probed
  109. *
  110. * If detected invokes the pcie_port_device_register() method for
  111. * this port device.
  112. *
  113. */
  114. static int pcie_portdrv_probe(struct pci_dev *dev,
  115. const struct pci_device_id *id)
  116. {
  117. int status;
  118. if (!pci_is_pcie(dev) ||
  119. ((pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT) &&
  120. (pci_pcie_type(dev) != PCI_EXP_TYPE_UPSTREAM) &&
  121. (pci_pcie_type(dev) != PCI_EXP_TYPE_DOWNSTREAM)))
  122. return -ENODEV;
  123. status = pcie_port_device_register(dev);
  124. if (status)
  125. return status;
  126. pci_save_state(dev);
  127. dev_pm_set_driver_flags(&dev->dev, DPM_FLAG_SMART_SUSPEND |
  128. DPM_FLAG_LEAVE_SUSPENDED);
  129. if (pci_bridge_d3_possible(dev)) {
  130. /*
  131. * Keep the port resumed 100ms to make sure things like
  132. * config space accesses from userspace (lspci) will not
  133. * cause the port to repeatedly suspend and resume.
  134. */
  135. pm_runtime_set_autosuspend_delay(&dev->dev, 100);
  136. pm_runtime_use_autosuspend(&dev->dev);
  137. pm_runtime_mark_last_busy(&dev->dev);
  138. pm_runtime_put_autosuspend(&dev->dev);
  139. pm_runtime_allow(&dev->dev);
  140. }
  141. return 0;
  142. }
  143. static void pcie_portdrv_remove(struct pci_dev *dev)
  144. {
  145. if (pci_bridge_d3_possible(dev)) {
  146. pm_runtime_forbid(&dev->dev);
  147. pm_runtime_get_noresume(&dev->dev);
  148. pm_runtime_dont_use_autosuspend(&dev->dev);
  149. }
  150. pcie_port_device_remove(dev);
  151. }
  152. static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
  153. enum pci_channel_state error)
  154. {
  155. /* Root Port has no impact. Always recovers. */
  156. return PCI_ERS_RESULT_CAN_RECOVER;
  157. }
  158. static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
  159. {
  160. return PCI_ERS_RESULT_RECOVERED;
  161. }
  162. static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
  163. {
  164. /* If fatal, restore cfg space for possible link reset at upstream */
  165. if (dev->error_state == pci_channel_io_frozen) {
  166. dev->state_saved = true;
  167. pci_restore_state(dev);
  168. pcie_portdrv_restore_config(dev);
  169. pci_enable_pcie_error_reporting(dev);
  170. }
  171. return PCI_ERS_RESULT_RECOVERED;
  172. }
  173. static int resume_iter(struct device *device, void *data)
  174. {
  175. struct pcie_device *pcie_device;
  176. struct pcie_port_service_driver *driver;
  177. if (device->bus == &pcie_port_bus_type && device->driver) {
  178. driver = to_service_driver(device->driver);
  179. if (driver && driver->error_resume) {
  180. pcie_device = to_pcie_device(device);
  181. /* Forward error message to service drivers */
  182. driver->error_resume(pcie_device->port);
  183. }
  184. }
  185. return 0;
  186. }
  187. static void pcie_portdrv_err_resume(struct pci_dev *dev)
  188. {
  189. device_for_each_child(&dev->dev, NULL, resume_iter);
  190. }
  191. /*
  192. * LINUX Device Driver Model
  193. */
  194. static const struct pci_device_id port_pci_ids[] = { {
  195. /* handle any PCI-Express port */
  196. PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
  197. }, { /* end: all zeroes */ }
  198. };
  199. static const struct pci_error_handlers pcie_portdrv_err_handler = {
  200. .error_detected = pcie_portdrv_error_detected,
  201. .mmio_enabled = pcie_portdrv_mmio_enabled,
  202. .slot_reset = pcie_portdrv_slot_reset,
  203. .resume = pcie_portdrv_err_resume,
  204. };
  205. static struct pci_driver pcie_portdriver = {
  206. .name = "pcieport",
  207. .id_table = &port_pci_ids[0],
  208. .probe = pcie_portdrv_probe,
  209. .remove = pcie_portdrv_remove,
  210. .shutdown = pcie_portdrv_remove,
  211. .err_handler = &pcie_portdrv_err_handler,
  212. .driver.pm = PCIE_PORTDRV_PM_OPS,
  213. };
  214. static int __init dmi_pcie_pme_disable_msi(const struct dmi_system_id *d)
  215. {
  216. pr_notice("%s detected: will not use MSI for PCIe PME signaling\n",
  217. d->ident);
  218. pcie_pme_disable_msi();
  219. return 0;
  220. }
  221. static const struct dmi_system_id pcie_portdrv_dmi_table[] __initconst = {
  222. /*
  223. * Boxes that should not use MSI for PCIe PME signaling.
  224. */
  225. {
  226. .callback = dmi_pcie_pme_disable_msi,
  227. .ident = "MSI Wind U-100",
  228. .matches = {
  229. DMI_MATCH(DMI_SYS_VENDOR,
  230. "MICRO-STAR INTERNATIONAL CO., LTD"),
  231. DMI_MATCH(DMI_PRODUCT_NAME, "U-100"),
  232. },
  233. },
  234. {}
  235. };
  236. static int __init pcie_portdrv_init(void)
  237. {
  238. int retval;
  239. if (pcie_ports_disabled)
  240. return pci_register_driver(&pcie_portdriver);
  241. dmi_check_system(pcie_portdrv_dmi_table);
  242. retval = pcie_port_bus_register();
  243. if (retval) {
  244. printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
  245. goto out;
  246. }
  247. retval = pci_register_driver(&pcie_portdriver);
  248. if (retval)
  249. pcie_port_bus_unregister();
  250. out:
  251. return retval;
  252. }
  253. device_initcall(pcie_portdrv_init);