pci-acpi.c 9.5 KB

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