pci-acpi.c 11 KB

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