pci-acpi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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->current_state == PCI_D3cold) {
  44. pci_wakeup_event(pci_dev);
  45. pm_runtime_resume(&pci_dev->dev);
  46. return;
  47. }
  48. if (!pci_dev->pm_cap || !pci_dev->pme_support
  49. || pci_check_pme_status(pci_dev)) {
  50. if (pci_dev->pme_poll)
  51. pci_dev->pme_poll = false;
  52. pci_wakeup_event(pci_dev);
  53. pm_runtime_resume(&pci_dev->dev);
  54. }
  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. acpi_handle tmp;
  160. static const u8 state_conv[] = {
  161. [PCI_D0] = ACPI_STATE_D0,
  162. [PCI_D1] = ACPI_STATE_D1,
  163. [PCI_D2] = ACPI_STATE_D2,
  164. [PCI_D3hot] = ACPI_STATE_D3,
  165. [PCI_D3cold] = ACPI_STATE_D3
  166. };
  167. int error = -EINVAL;
  168. /* If the ACPI device has _EJ0, ignore the device */
  169. if (!handle || ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  170. return -ENODEV;
  171. switch (state) {
  172. case PCI_D3cold:
  173. if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
  174. PM_QOS_FLAGS_ALL) {
  175. error = -EBUSY;
  176. break;
  177. }
  178. case PCI_D0:
  179. case PCI_D1:
  180. case PCI_D2:
  181. case PCI_D3hot:
  182. error = acpi_bus_set_power(handle, state_conv[state]);
  183. }
  184. if (!error)
  185. dev_info(&dev->dev, "power state changed by ACPI to %s\n",
  186. pci_power_name(state));
  187. return error;
  188. }
  189. static bool acpi_pci_can_wakeup(struct pci_dev *dev)
  190. {
  191. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  192. return handle ? acpi_bus_can_wakeup(handle) : false;
  193. }
  194. static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
  195. {
  196. while (bus->parent) {
  197. if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
  198. return;
  199. bus = bus->parent;
  200. }
  201. /* We have reached the root bus. */
  202. if (bus->bridge)
  203. acpi_pm_device_sleep_wake(bus->bridge, enable);
  204. }
  205. static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
  206. {
  207. if (acpi_pci_can_wakeup(dev))
  208. return acpi_pm_device_sleep_wake(&dev->dev, enable);
  209. acpi_pci_propagate_wakeup_enable(dev->bus, enable);
  210. return 0;
  211. }
  212. static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
  213. {
  214. while (bus->parent) {
  215. struct pci_dev *bridge = bus->self;
  216. if (bridge->pme_interrupt)
  217. return;
  218. if (!acpi_pm_device_run_wake(&bridge->dev, enable))
  219. return;
  220. bus = bus->parent;
  221. }
  222. /* We have reached the root bus. */
  223. if (bus->bridge)
  224. acpi_pm_device_run_wake(bus->bridge, enable);
  225. }
  226. static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
  227. {
  228. /*
  229. * Per PCI Express Base Specification Revision 2.0 section
  230. * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
  231. * waking up to power on the main link even if there is PME
  232. * support for D3cold
  233. */
  234. if (dev->pme_interrupt && !dev->runtime_d3cold)
  235. return 0;
  236. if (!acpi_pm_device_run_wake(&dev->dev, enable))
  237. return 0;
  238. acpi_pci_propagate_run_wake(dev->bus, enable);
  239. return 0;
  240. }
  241. static struct pci_platform_pm_ops acpi_pci_platform_pm = {
  242. .is_manageable = acpi_pci_power_manageable,
  243. .set_state = acpi_pci_set_power_state,
  244. .choose_state = acpi_pci_choose_state,
  245. .sleep_wake = acpi_pci_sleep_wake,
  246. .run_wake = acpi_pci_run_wake,
  247. };
  248. /* ACPI bus type */
  249. static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
  250. {
  251. struct pci_dev * pci_dev;
  252. u64 addr;
  253. pci_dev = to_pci_dev(dev);
  254. /* Please ref to ACPI spec for the syntax of _ADR */
  255. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  256. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
  257. if (!*handle)
  258. return -ENODEV;
  259. return 0;
  260. }
  261. static void pci_acpi_setup(struct device *dev)
  262. {
  263. struct pci_dev *pci_dev = to_pci_dev(dev);
  264. acpi_handle handle = ACPI_HANDLE(dev);
  265. struct acpi_device *adev;
  266. if (acpi_bus_get_device(handle, &adev) || !adev->wakeup.flags.valid)
  267. return;
  268. device_set_wakeup_capable(dev, true);
  269. acpi_pci_sleep_wake(pci_dev, false);
  270. pci_acpi_add_pm_notifier(adev, pci_dev);
  271. if (adev->wakeup.flags.run_wake)
  272. device_set_run_wake(dev, true);
  273. }
  274. static void pci_acpi_cleanup(struct device *dev)
  275. {
  276. acpi_handle handle = ACPI_HANDLE(dev);
  277. struct acpi_device *adev;
  278. if (!acpi_bus_get_device(handle, &adev) && adev->wakeup.flags.valid) {
  279. device_set_wakeup_capable(dev, false);
  280. device_set_run_wake(dev, false);
  281. pci_acpi_remove_pm_notifier(adev);
  282. }
  283. }
  284. static bool pci_acpi_bus_match(struct device *dev)
  285. {
  286. return dev->bus == &pci_bus_type;
  287. }
  288. static struct acpi_bus_type acpi_pci_bus = {
  289. .name = "PCI",
  290. .match = pci_acpi_bus_match,
  291. .find_device = acpi_pci_find_device,
  292. .setup = pci_acpi_setup,
  293. .cleanup = pci_acpi_cleanup,
  294. };
  295. static int __init acpi_pci_init(void)
  296. {
  297. int ret;
  298. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
  299. printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
  300. pci_no_msi();
  301. }
  302. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  303. printk(KERN_INFO"ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
  304. pcie_no_aspm();
  305. }
  306. ret = register_acpi_bus_type(&acpi_pci_bus);
  307. if (ret)
  308. return 0;
  309. pci_set_platform_pm(&acpi_pci_platform_pm);
  310. return 0;
  311. }
  312. arch_initcall(acpi_pci_init);