portdrv_pci.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/aer.h>
  16. #include <linux/dmi.h>
  17. #include "../pci.h"
  18. #include "portdrv.h"
  19. /* If this switch is set, PCIe port native services should not be enabled. */
  20. bool pcie_ports_disabled;
  21. /*
  22. * If the user specified "pcie_ports=native", use the PCIe services regardless
  23. * of whether the platform has given us permission. On ACPI systems, this
  24. * means we ignore _OSC.
  25. */
  26. bool pcie_ports_native;
  27. static int __init pcie_port_setup(char *str)
  28. {
  29. if (!strncmp(str, "compat", 6))
  30. pcie_ports_disabled = true;
  31. else if (!strncmp(str, "native", 6))
  32. pcie_ports_native = true;
  33. return 1;
  34. }
  35. __setup("pcie_ports=", pcie_port_setup);
  36. /* global data */
  37. #ifdef CONFIG_PM
  38. static int pcie_port_runtime_suspend(struct device *dev)
  39. {
  40. return to_pci_dev(dev)->bridge_d3 ? 0 : -EBUSY;
  41. }
  42. static int pcie_port_runtime_resume(struct device *dev)
  43. {
  44. return 0;
  45. }
  46. static int pcie_port_runtime_idle(struct device *dev)
  47. {
  48. /*
  49. * Assume the PCI core has set bridge_d3 whenever it thinks the port
  50. * should be good to go to D3. Everything else, including moving
  51. * the port to D3, is handled by the PCI core.
  52. */
  53. return to_pci_dev(dev)->bridge_d3 ? 0 : -EBUSY;
  54. }
  55. static const struct dev_pm_ops pcie_portdrv_pm_ops = {
  56. .suspend = pcie_port_device_suspend,
  57. .resume_noirq = pcie_port_device_resume_noirq,
  58. .resume = pcie_port_device_resume,
  59. .freeze = pcie_port_device_suspend,
  60. .thaw = pcie_port_device_resume,
  61. .poweroff = pcie_port_device_suspend,
  62. .restore_noirq = pcie_port_device_resume_noirq,
  63. .restore = pcie_port_device_resume,
  64. .runtime_suspend = pcie_port_runtime_suspend,
  65. .runtime_resume = pcie_port_runtime_resume,
  66. .runtime_idle = pcie_port_runtime_idle,
  67. };
  68. #define PCIE_PORTDRV_PM_OPS (&pcie_portdrv_pm_ops)
  69. #else /* !PM */
  70. #define PCIE_PORTDRV_PM_OPS NULL
  71. #endif /* !PM */
  72. /*
  73. * pcie_portdrv_probe - Probe PCI-Express port devices
  74. * @dev: PCI-Express port device being probed
  75. *
  76. * If detected invokes the pcie_port_device_register() method for
  77. * this port device.
  78. *
  79. */
  80. static int pcie_portdrv_probe(struct pci_dev *dev,
  81. const struct pci_device_id *id)
  82. {
  83. int status;
  84. if (!pci_is_pcie(dev) ||
  85. ((pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT) &&
  86. (pci_pcie_type(dev) != PCI_EXP_TYPE_UPSTREAM) &&
  87. (pci_pcie_type(dev) != PCI_EXP_TYPE_DOWNSTREAM)))
  88. return -ENODEV;
  89. status = pcie_port_device_register(dev);
  90. if (status)
  91. return status;
  92. pci_save_state(dev);
  93. dev_pm_set_driver_flags(&dev->dev, DPM_FLAG_SMART_SUSPEND |
  94. DPM_FLAG_LEAVE_SUSPENDED);
  95. if (pci_bridge_d3_possible(dev)) {
  96. /*
  97. * Keep the port resumed 100ms to make sure things like
  98. * config space accesses from userspace (lspci) will not
  99. * cause the port to repeatedly suspend and resume.
  100. */
  101. pm_runtime_set_autosuspend_delay(&dev->dev, 100);
  102. pm_runtime_use_autosuspend(&dev->dev);
  103. pm_runtime_mark_last_busy(&dev->dev);
  104. pm_runtime_put_autosuspend(&dev->dev);
  105. pm_runtime_allow(&dev->dev);
  106. }
  107. return 0;
  108. }
  109. static void pcie_portdrv_remove(struct pci_dev *dev)
  110. {
  111. if (pci_bridge_d3_possible(dev)) {
  112. pm_runtime_forbid(&dev->dev);
  113. pm_runtime_get_noresume(&dev->dev);
  114. pm_runtime_dont_use_autosuspend(&dev->dev);
  115. }
  116. pcie_port_device_remove(dev);
  117. }
  118. static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
  119. enum pci_channel_state error)
  120. {
  121. /* Root Port has no impact. Always recovers. */
  122. return PCI_ERS_RESULT_CAN_RECOVER;
  123. }
  124. static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
  125. {
  126. return PCI_ERS_RESULT_RECOVERED;
  127. }
  128. static int resume_iter(struct device *device, void *data)
  129. {
  130. struct pcie_device *pcie_device;
  131. struct pcie_port_service_driver *driver;
  132. if (device->bus == &pcie_port_bus_type && device->driver) {
  133. driver = to_service_driver(device->driver);
  134. if (driver && driver->error_resume) {
  135. pcie_device = to_pcie_device(device);
  136. /* Forward error message to service drivers */
  137. driver->error_resume(pcie_device->port);
  138. }
  139. }
  140. return 0;
  141. }
  142. static void pcie_portdrv_err_resume(struct pci_dev *dev)
  143. {
  144. device_for_each_child(&dev->dev, NULL, resume_iter);
  145. }
  146. /*
  147. * LINUX Device Driver Model
  148. */
  149. static const struct pci_device_id port_pci_ids[] = { {
  150. /* handle any PCI-Express port */
  151. PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
  152. }, { /* end: all zeroes */ }
  153. };
  154. static const struct pci_error_handlers pcie_portdrv_err_handler = {
  155. .error_detected = pcie_portdrv_error_detected,
  156. .mmio_enabled = pcie_portdrv_mmio_enabled,
  157. .resume = pcie_portdrv_err_resume,
  158. };
  159. static struct pci_driver pcie_portdriver = {
  160. .name = "pcieport",
  161. .id_table = &port_pci_ids[0],
  162. .probe = pcie_portdrv_probe,
  163. .remove = pcie_portdrv_remove,
  164. .shutdown = pcie_portdrv_remove,
  165. .err_handler = &pcie_portdrv_err_handler,
  166. .driver.pm = PCIE_PORTDRV_PM_OPS,
  167. };
  168. static int __init dmi_pcie_pme_disable_msi(const struct dmi_system_id *d)
  169. {
  170. pr_notice("%s detected: will not use MSI for PCIe PME signaling\n",
  171. d->ident);
  172. pcie_pme_disable_msi();
  173. return 0;
  174. }
  175. static const struct dmi_system_id pcie_portdrv_dmi_table[] __initconst = {
  176. /*
  177. * Boxes that should not use MSI for PCIe PME signaling.
  178. */
  179. {
  180. .callback = dmi_pcie_pme_disable_msi,
  181. .ident = "MSI Wind U-100",
  182. .matches = {
  183. DMI_MATCH(DMI_SYS_VENDOR,
  184. "MICRO-STAR INTERNATIONAL CO., LTD"),
  185. DMI_MATCH(DMI_PRODUCT_NAME, "U-100"),
  186. },
  187. },
  188. {}
  189. };
  190. static void __init pcie_init_services(void)
  191. {
  192. pcie_aer_init();
  193. pcie_pme_init();
  194. pcie_dpc_init();
  195. pcie_hp_init();
  196. }
  197. static int __init pcie_portdrv_init(void)
  198. {
  199. if (pcie_ports_disabled)
  200. return -EACCES;
  201. pcie_init_services();
  202. dmi_check_system(pcie_portdrv_dmi_table);
  203. return pci_register_driver(&pcie_portdriver);
  204. }
  205. device_initcall(pcie_portdrv_init);