pci-acpi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * File: pci-acpi.c
  3. * Purpose: Provide PCI support in ACPI
  4. *
  5. * Copyright (C) 2005 David Shaohua Li <shaohua.li@intel.com>
  6. * Copyright (C) 2004 Tom Long Nguyen <tom.l.nguyen@intel.com>
  7. * Copyright (C) 2004 Intel Corp.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/init.h>
  11. #include <linux/pci.h>
  12. #include <linux/module.h>
  13. #include <linux/pci-aspm.h>
  14. #include <linux/pci-acpi.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/pm_qos.h>
  17. #include "pci.h"
  18. /**
  19. * pci_acpi_wake_bus - Root bus wakeup notification fork function.
  20. * @work: Work item to handle.
  21. */
  22. static void pci_acpi_wake_bus(struct work_struct *work)
  23. {
  24. struct acpi_device *adev;
  25. struct acpi_pci_root *root;
  26. adev = container_of(work, struct acpi_device, wakeup.context.work);
  27. root = acpi_driver_data(adev);
  28. pci_pme_wakeup_bus(root->bus);
  29. }
  30. /**
  31. * pci_acpi_wake_dev - PCI device wakeup notification work function.
  32. * @handle: ACPI handle of a device the notification is for.
  33. * @work: Work item to handle.
  34. */
  35. static void pci_acpi_wake_dev(struct work_struct *work)
  36. {
  37. struct acpi_device_wakeup_context *context;
  38. struct pci_dev *pci_dev;
  39. context = container_of(work, struct acpi_device_wakeup_context, work);
  40. pci_dev = to_pci_dev(context->dev);
  41. if (pci_dev->pme_poll)
  42. pci_dev->pme_poll = false;
  43. if (pci_dev->current_state == PCI_D3cold) {
  44. pci_wakeup_event(pci_dev);
  45. pm_runtime_resume(&pci_dev->dev);
  46. return;
  47. }
  48. /* Clear PME Status if set. */
  49. if (pci_dev->pme_support)
  50. pci_check_pme_status(pci_dev);
  51. pci_wakeup_event(pci_dev);
  52. pm_runtime_resume(&pci_dev->dev);
  53. if (pci_dev->subordinate)
  54. pci_pme_wakeup_bus(pci_dev->subordinate);
  55. }
  56. /**
  57. * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
  58. * @dev: PCI root bridge ACPI device.
  59. */
  60. acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev)
  61. {
  62. return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus);
  63. }
  64. /**
  65. * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
  66. * @dev: ACPI device to add the notifier for.
  67. * @pci_dev: PCI device to check for the PME status if an event is signaled.
  68. */
  69. acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
  70. struct pci_dev *pci_dev)
  71. {
  72. return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev);
  73. }
  74. phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)
  75. {
  76. acpi_status status = AE_NOT_EXIST;
  77. unsigned long long mcfg_addr;
  78. if (handle)
  79. status = acpi_evaluate_integer(handle, METHOD_NAME__CBA,
  80. NULL, &mcfg_addr);
  81. if (ACPI_FAILURE(status))
  82. return 0;
  83. return (phys_addr_t)mcfg_addr;
  84. }
  85. /*
  86. * _SxD returns the D-state with the highest power
  87. * (lowest D-state number) supported in the S-state "x".
  88. *
  89. * If the devices does not have a _PRW
  90. * (Power Resources for Wake) supporting system wakeup from "x"
  91. * then the OS is free to choose a lower power (higher number
  92. * D-state) than the return value from _SxD.
  93. *
  94. * But if _PRW is enabled at S-state "x", the OS
  95. * must not choose a power lower than _SxD --
  96. * unless the device has an _SxW method specifying
  97. * the lowest power (highest D-state number) the device
  98. * may enter while still able to wake the system.
  99. *
  100. * ie. depending on global OS policy:
  101. *
  102. * if (_PRW at S-state x)
  103. * choose from highest power _SxD to lowest power _SxW
  104. * else // no _PRW at S-state x
  105. * choose highest power _SxD or any lower power
  106. */
  107. static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  108. {
  109. int acpi_state, d_max;
  110. if (pdev->no_d3cold)
  111. d_max = ACPI_STATE_D3_HOT;
  112. else
  113. d_max = ACPI_STATE_D3_COLD;
  114. acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
  115. if (acpi_state < 0)
  116. return PCI_POWER_ERROR;
  117. switch (acpi_state) {
  118. case ACPI_STATE_D0:
  119. return PCI_D0;
  120. case ACPI_STATE_D1:
  121. return PCI_D1;
  122. case ACPI_STATE_D2:
  123. return PCI_D2;
  124. case ACPI_STATE_D3_HOT:
  125. return PCI_D3hot;
  126. case ACPI_STATE_D3_COLD:
  127. return PCI_D3cold;
  128. }
  129. return PCI_POWER_ERROR;
  130. }
  131. static bool acpi_pci_power_manageable(struct pci_dev *dev)
  132. {
  133. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  134. return adev ? acpi_device_power_manageable(adev) : false;
  135. }
  136. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  137. {
  138. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  139. static const u8 state_conv[] = {
  140. [PCI_D0] = ACPI_STATE_D0,
  141. [PCI_D1] = ACPI_STATE_D1,
  142. [PCI_D2] = ACPI_STATE_D2,
  143. [PCI_D3hot] = ACPI_STATE_D3_COLD,
  144. [PCI_D3cold] = ACPI_STATE_D3_COLD,
  145. };
  146. int error = -EINVAL;
  147. /* If the ACPI device has _EJ0, ignore the device */
  148. if (!adev || acpi_has_method(adev->handle, "_EJ0"))
  149. return -ENODEV;
  150. switch (state) {
  151. case PCI_D3cold:
  152. if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
  153. PM_QOS_FLAGS_ALL) {
  154. error = -EBUSY;
  155. break;
  156. }
  157. case PCI_D0:
  158. case PCI_D1:
  159. case PCI_D2:
  160. case PCI_D3hot:
  161. error = acpi_device_set_power(adev, state_conv[state]);
  162. }
  163. if (!error)
  164. dev_dbg(&dev->dev, "power state changed by ACPI to %s\n",
  165. acpi_power_state_string(state_conv[state]));
  166. return error;
  167. }
  168. static bool acpi_pci_can_wakeup(struct pci_dev *dev)
  169. {
  170. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  171. return adev ? acpi_device_can_wakeup(adev) : false;
  172. }
  173. static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
  174. {
  175. while (bus->parent) {
  176. if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
  177. return;
  178. bus = bus->parent;
  179. }
  180. /* We have reached the root bus. */
  181. if (bus->bridge)
  182. acpi_pm_device_sleep_wake(bus->bridge, enable);
  183. }
  184. static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
  185. {
  186. if (acpi_pci_can_wakeup(dev))
  187. return acpi_pm_device_sleep_wake(&dev->dev, enable);
  188. acpi_pci_propagate_wakeup_enable(dev->bus, enable);
  189. return 0;
  190. }
  191. static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
  192. {
  193. while (bus->parent) {
  194. struct pci_dev *bridge = bus->self;
  195. if (bridge->pme_interrupt)
  196. return;
  197. if (!acpi_pm_device_run_wake(&bridge->dev, enable))
  198. return;
  199. bus = bus->parent;
  200. }
  201. /* We have reached the root bus. */
  202. if (bus->bridge)
  203. acpi_pm_device_run_wake(bus->bridge, enable);
  204. }
  205. static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
  206. {
  207. /*
  208. * Per PCI Express Base Specification Revision 2.0 section
  209. * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
  210. * waking up to power on the main link even if there is PME
  211. * support for D3cold
  212. */
  213. if (dev->pme_interrupt && !dev->runtime_d3cold)
  214. return 0;
  215. if (!acpi_pm_device_run_wake(&dev->dev, enable))
  216. return 0;
  217. acpi_pci_propagate_run_wake(dev->bus, enable);
  218. return 0;
  219. }
  220. static struct pci_platform_pm_ops acpi_pci_platform_pm = {
  221. .is_manageable = acpi_pci_power_manageable,
  222. .set_state = acpi_pci_set_power_state,
  223. .choose_state = acpi_pci_choose_state,
  224. .sleep_wake = acpi_pci_sleep_wake,
  225. .run_wake = acpi_pci_run_wake,
  226. };
  227. void acpi_pci_add_bus(struct pci_bus *bus)
  228. {
  229. if (acpi_pci_disabled || !bus->bridge)
  230. return;
  231. acpi_pci_slot_enumerate(bus);
  232. acpiphp_enumerate_slots(bus);
  233. }
  234. void acpi_pci_remove_bus(struct pci_bus *bus)
  235. {
  236. if (acpi_pci_disabled || !bus->bridge)
  237. return;
  238. acpiphp_remove_slots(bus);
  239. acpi_pci_slot_remove(bus);
  240. }
  241. /* ACPI bus type */
  242. static struct acpi_device *acpi_pci_find_companion(struct device *dev)
  243. {
  244. struct pci_dev *pci_dev = to_pci_dev(dev);
  245. bool check_children;
  246. u64 addr;
  247. check_children = pci_is_bridge(pci_dev);
  248. /* Please ref to ACPI spec for the syntax of _ADR */
  249. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  250. return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
  251. check_children);
  252. }
  253. static void pci_acpi_setup(struct device *dev)
  254. {
  255. struct pci_dev *pci_dev = to_pci_dev(dev);
  256. struct acpi_device *adev = ACPI_COMPANION(dev);
  257. if (!adev)
  258. return;
  259. pci_acpi_add_pm_notifier(adev, pci_dev);
  260. if (!adev->wakeup.flags.valid)
  261. return;
  262. device_set_wakeup_capable(dev, true);
  263. acpi_pci_sleep_wake(pci_dev, false);
  264. if (adev->wakeup.flags.run_wake)
  265. device_set_run_wake(dev, true);
  266. }
  267. static void pci_acpi_cleanup(struct device *dev)
  268. {
  269. struct acpi_device *adev = ACPI_COMPANION(dev);
  270. if (!adev)
  271. return;
  272. pci_acpi_remove_pm_notifier(adev);
  273. if (adev->wakeup.flags.valid) {
  274. device_set_wakeup_capable(dev, false);
  275. device_set_run_wake(dev, false);
  276. }
  277. }
  278. static bool pci_acpi_bus_match(struct device *dev)
  279. {
  280. return dev_is_pci(dev);
  281. }
  282. static struct acpi_bus_type acpi_pci_bus = {
  283. .name = "PCI",
  284. .match = pci_acpi_bus_match,
  285. .find_companion = acpi_pci_find_companion,
  286. .setup = pci_acpi_setup,
  287. .cleanup = pci_acpi_cleanup,
  288. };
  289. static int __init acpi_pci_init(void)
  290. {
  291. int ret;
  292. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
  293. pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
  294. pci_no_msi();
  295. }
  296. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  297. pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
  298. pcie_no_aspm();
  299. }
  300. ret = register_acpi_bus_type(&acpi_pci_bus);
  301. if (ret)
  302. return 0;
  303. pci_set_platform_pm(&acpi_pci_platform_pm);
  304. acpi_pci_slot_init();
  305. acpiphp_init();
  306. return 0;
  307. }
  308. arch_initcall(acpi_pci_init);