pci-acpi.c 9.9 KB

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