pci-acpi.c 9.6 KB

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