pci-acpi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. static DEFINE_MUTEX(pci_acpi_pm_notify_mtx);
  21. /**
  22. * pci_acpi_wake_bus - Wake-up notification handler for root buses.
  23. * @handle: ACPI handle of a device the notification is for.
  24. * @event: Type of the signaled event.
  25. * @context: PCI root bus to wake up devices on.
  26. */
  27. static void pci_acpi_wake_bus(acpi_handle handle, u32 event, void *context)
  28. {
  29. struct pci_bus *pci_bus = context;
  30. if (event == ACPI_NOTIFY_DEVICE_WAKE && pci_bus)
  31. pci_pme_wakeup_bus(pci_bus);
  32. }
  33. /**
  34. * pci_acpi_wake_dev - Wake-up notification handler for PCI devices.
  35. * @handle: ACPI handle of a device the notification is for.
  36. * @event: Type of the signaled event.
  37. * @context: PCI device object to wake up.
  38. */
  39. static void pci_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
  40. {
  41. struct pci_dev *pci_dev = context;
  42. if (event != ACPI_NOTIFY_DEVICE_WAKE || !pci_dev)
  43. return;
  44. if (!pci_dev->pm_cap || !pci_dev->pme_support
  45. || pci_check_pme_status(pci_dev)) {
  46. if (pci_dev->pme_poll)
  47. pci_dev->pme_poll = false;
  48. pci_wakeup_event(pci_dev);
  49. pm_runtime_resume(&pci_dev->dev);
  50. }
  51. if (pci_dev->subordinate)
  52. pci_pme_wakeup_bus(pci_dev->subordinate);
  53. }
  54. /**
  55. * add_pm_notifier - Register PM notifier for given ACPI device.
  56. * @dev: ACPI device to add the notifier for.
  57. * @context: PCI device or bus to check for PME status if an event is signaled.
  58. *
  59. * NOTE: @dev need not be a run-wake or wake-up device to be a valid source of
  60. * PM wake-up events. For example, wake-up events may be generated for bridges
  61. * if one of the devices below the bridge is signaling PME, even if the bridge
  62. * itself doesn't have a wake-up GPE associated with it.
  63. */
  64. static acpi_status add_pm_notifier(struct acpi_device *dev,
  65. acpi_notify_handler handler,
  66. void *context)
  67. {
  68. acpi_status status = AE_ALREADY_EXISTS;
  69. mutex_lock(&pci_acpi_pm_notify_mtx);
  70. if (dev->wakeup.flags.notifier_present)
  71. goto out;
  72. status = acpi_install_notify_handler(dev->handle,
  73. ACPI_SYSTEM_NOTIFY,
  74. handler, context);
  75. if (ACPI_FAILURE(status))
  76. goto out;
  77. dev->wakeup.flags.notifier_present = true;
  78. out:
  79. mutex_unlock(&pci_acpi_pm_notify_mtx);
  80. return status;
  81. }
  82. /**
  83. * remove_pm_notifier - Unregister PM notifier from given ACPI device.
  84. * @dev: ACPI device to remove the notifier from.
  85. */
  86. static acpi_status remove_pm_notifier(struct acpi_device *dev,
  87. acpi_notify_handler handler)
  88. {
  89. acpi_status status = AE_BAD_PARAMETER;
  90. mutex_lock(&pci_acpi_pm_notify_mtx);
  91. if (!dev->wakeup.flags.notifier_present)
  92. goto out;
  93. status = acpi_remove_notify_handler(dev->handle,
  94. ACPI_SYSTEM_NOTIFY,
  95. handler);
  96. if (ACPI_FAILURE(status))
  97. goto out;
  98. dev->wakeup.flags.notifier_present = false;
  99. out:
  100. mutex_unlock(&pci_acpi_pm_notify_mtx);
  101. return status;
  102. }
  103. /**
  104. * pci_acpi_add_bus_pm_notifier - Register PM notifier for given PCI bus.
  105. * @dev: ACPI device to add the notifier for.
  106. * @pci_bus: PCI bus to walk checking for PME status if an event is signaled.
  107. */
  108. acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev,
  109. struct pci_bus *pci_bus)
  110. {
  111. return add_pm_notifier(dev, pci_acpi_wake_bus, pci_bus);
  112. }
  113. /**
  114. * pci_acpi_remove_bus_pm_notifier - Unregister PCI bus PM notifier.
  115. * @dev: ACPI device to remove the notifier from.
  116. */
  117. acpi_status pci_acpi_remove_bus_pm_notifier(struct acpi_device *dev)
  118. {
  119. return remove_pm_notifier(dev, pci_acpi_wake_bus);
  120. }
  121. /**
  122. * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
  123. * @dev: ACPI device to add the notifier for.
  124. * @pci_dev: PCI device to check for the PME status if an event is signaled.
  125. */
  126. acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
  127. struct pci_dev *pci_dev)
  128. {
  129. return add_pm_notifier(dev, pci_acpi_wake_dev, pci_dev);
  130. }
  131. /**
  132. * pci_acpi_remove_pm_notifier - Unregister PCI device PM notifier.
  133. * @dev: ACPI device to remove the notifier from.
  134. */
  135. acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev)
  136. {
  137. return remove_pm_notifier(dev, pci_acpi_wake_dev);
  138. }
  139. /*
  140. * _SxD returns the D-state with the highest power
  141. * (lowest D-state number) supported in the S-state "x".
  142. *
  143. * If the devices does not have a _PRW
  144. * (Power Resources for Wake) supporting system wakeup from "x"
  145. * then the OS is free to choose a lower power (higher number
  146. * D-state) than the return value from _SxD.
  147. *
  148. * But if _PRW is enabled at S-state "x", the OS
  149. * must not choose a power lower than _SxD --
  150. * unless the device has an _SxW method specifying
  151. * the lowest power (highest D-state number) the device
  152. * may enter while still able to wake the system.
  153. *
  154. * ie. depending on global OS policy:
  155. *
  156. * if (_PRW at S-state x)
  157. * choose from highest power _SxD to lowest power _SxW
  158. * else // no _PRW at S-state x
  159. * choose highest power _SxD or any lower power
  160. */
  161. static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  162. {
  163. int acpi_state;
  164. acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL);
  165. if (acpi_state < 0)
  166. return PCI_POWER_ERROR;
  167. switch (acpi_state) {
  168. case ACPI_STATE_D0:
  169. return PCI_D0;
  170. case ACPI_STATE_D1:
  171. return PCI_D1;
  172. case ACPI_STATE_D2:
  173. return PCI_D2;
  174. case ACPI_STATE_D3_HOT:
  175. return PCI_D3hot;
  176. case ACPI_STATE_D3_COLD:
  177. return PCI_D3cold;
  178. }
  179. return PCI_POWER_ERROR;
  180. }
  181. static bool acpi_pci_power_manageable(struct pci_dev *dev)
  182. {
  183. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  184. return handle ? acpi_bus_power_manageable(handle) : false;
  185. }
  186. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  187. {
  188. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  189. acpi_handle tmp;
  190. static const u8 state_conv[] = {
  191. [PCI_D0] = ACPI_STATE_D0,
  192. [PCI_D1] = ACPI_STATE_D1,
  193. [PCI_D2] = ACPI_STATE_D2,
  194. [PCI_D3hot] = ACPI_STATE_D3,
  195. [PCI_D3cold] = ACPI_STATE_D3
  196. };
  197. int error = -EINVAL;
  198. /* If the ACPI device has _EJ0, ignore the device */
  199. if (!handle || ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  200. return -ENODEV;
  201. switch (state) {
  202. case PCI_D3cold:
  203. if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
  204. PM_QOS_FLAGS_ALL) {
  205. error = -EBUSY;
  206. break;
  207. }
  208. case PCI_D0:
  209. case PCI_D1:
  210. case PCI_D2:
  211. case PCI_D3hot:
  212. error = acpi_bus_set_power(handle, state_conv[state]);
  213. }
  214. if (!error)
  215. dev_printk(KERN_INFO, &dev->dev,
  216. "power state changed by ACPI to D%d\n", state);
  217. return error;
  218. }
  219. static bool acpi_pci_can_wakeup(struct pci_dev *dev)
  220. {
  221. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  222. return handle ? acpi_bus_can_wakeup(handle) : false;
  223. }
  224. static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
  225. {
  226. while (bus->parent) {
  227. if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
  228. return;
  229. bus = bus->parent;
  230. }
  231. /* We have reached the root bus. */
  232. if (bus->bridge)
  233. acpi_pm_device_sleep_wake(bus->bridge, enable);
  234. }
  235. static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
  236. {
  237. if (acpi_pci_can_wakeup(dev))
  238. return acpi_pm_device_sleep_wake(&dev->dev, enable);
  239. acpi_pci_propagate_wakeup_enable(dev->bus, enable);
  240. return 0;
  241. }
  242. static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
  243. {
  244. while (bus->parent) {
  245. struct pci_dev *bridge = bus->self;
  246. if (bridge->pme_interrupt)
  247. return;
  248. if (!acpi_pm_device_run_wake(&bridge->dev, enable))
  249. return;
  250. bus = bus->parent;
  251. }
  252. /* We have reached the root bus. */
  253. if (bus->bridge)
  254. acpi_pm_device_run_wake(bus->bridge, enable);
  255. }
  256. static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
  257. {
  258. if (dev->pme_interrupt)
  259. return 0;
  260. if (!acpi_pm_device_run_wake(&dev->dev, enable))
  261. return 0;
  262. acpi_pci_propagate_run_wake(dev->bus, enable);
  263. return 0;
  264. }
  265. static struct pci_platform_pm_ops acpi_pci_platform_pm = {
  266. .is_manageable = acpi_pci_power_manageable,
  267. .set_state = acpi_pci_set_power_state,
  268. .choose_state = acpi_pci_choose_state,
  269. .can_wakeup = acpi_pci_can_wakeup,
  270. .sleep_wake = acpi_pci_sleep_wake,
  271. .run_wake = acpi_pci_run_wake,
  272. };
  273. /* ACPI bus type */
  274. static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
  275. {
  276. struct pci_dev * pci_dev;
  277. u64 addr;
  278. pci_dev = to_pci_dev(dev);
  279. /* Please ref to ACPI spec for the syntax of _ADR */
  280. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  281. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
  282. if (!*handle)
  283. return -ENODEV;
  284. return 0;
  285. }
  286. static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
  287. {
  288. int num;
  289. unsigned int seg, bus;
  290. /*
  291. * The string should be the same as root bridge's name
  292. * Please look at 'pci_scan_bus_parented'
  293. */
  294. num = sscanf(dev_name(dev), "pci%04x:%02x", &seg, &bus);
  295. if (num != 2)
  296. return -ENODEV;
  297. *handle = acpi_get_pci_rootbridge_handle(seg, bus);
  298. if (!*handle)
  299. return -ENODEV;
  300. return 0;
  301. }
  302. static struct acpi_bus_type acpi_pci_bus = {
  303. .bus = &pci_bus_type,
  304. .find_device = acpi_pci_find_device,
  305. .find_bridge = acpi_pci_find_root_bridge,
  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. printk(KERN_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. printk(KERN_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. return 0;
  323. }
  324. arch_initcall(acpi_pci_init);