portdrv_pci.c 6.2 KB

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