pci-acpi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "pci.h"
  18. /*
  19. * _SxD returns the D-state with the highest power
  20. * (lowest D-state number) supported in the S-state "x".
  21. *
  22. * If the devices does not have a _PRW
  23. * (Power Resources for Wake) supporting system wakeup from "x"
  24. * then the OS is free to choose a lower power (higher number
  25. * D-state) than the return value from _SxD.
  26. *
  27. * But if _PRW is enabled at S-state "x", the OS
  28. * must not choose a power lower than _SxD --
  29. * unless the device has an _SxW method specifying
  30. * the lowest power (highest D-state number) the device
  31. * may enter while still able to wake the system.
  32. *
  33. * ie. depending on global OS policy:
  34. *
  35. * if (_PRW at S-state x)
  36. * choose from highest power _SxD to lowest power _SxW
  37. * else // no _PRW at S-state x
  38. * choose highest power _SxD or any lower power
  39. */
  40. static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  41. {
  42. int acpi_state;
  43. acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL);
  44. if (acpi_state < 0)
  45. return PCI_POWER_ERROR;
  46. switch (acpi_state) {
  47. case ACPI_STATE_D0:
  48. return PCI_D0;
  49. case ACPI_STATE_D1:
  50. return PCI_D1;
  51. case ACPI_STATE_D2:
  52. return PCI_D2;
  53. case ACPI_STATE_D3:
  54. return PCI_D3hot;
  55. }
  56. return PCI_POWER_ERROR;
  57. }
  58. static bool acpi_pci_power_manageable(struct pci_dev *dev)
  59. {
  60. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  61. return handle ? acpi_bus_power_manageable(handle) : false;
  62. }
  63. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  64. {
  65. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  66. acpi_handle tmp;
  67. static const u8 state_conv[] = {
  68. [PCI_D0] = ACPI_STATE_D0,
  69. [PCI_D1] = ACPI_STATE_D1,
  70. [PCI_D2] = ACPI_STATE_D2,
  71. [PCI_D3hot] = ACPI_STATE_D3,
  72. [PCI_D3cold] = ACPI_STATE_D3
  73. };
  74. int error = -EINVAL;
  75. /* If the ACPI device has _EJ0, ignore the device */
  76. if (!handle || ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  77. return -ENODEV;
  78. switch (state) {
  79. case PCI_D0:
  80. case PCI_D1:
  81. case PCI_D2:
  82. case PCI_D3hot:
  83. case PCI_D3cold:
  84. error = acpi_bus_set_power(handle, state_conv[state]);
  85. }
  86. if (!error)
  87. dev_printk(KERN_INFO, &dev->dev,
  88. "power state changed by ACPI to D%d\n", state);
  89. return error;
  90. }
  91. static bool acpi_pci_can_wakeup(struct pci_dev *dev)
  92. {
  93. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  94. return handle ? acpi_bus_can_wakeup(handle) : false;
  95. }
  96. static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
  97. {
  98. while (bus->parent) {
  99. if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
  100. return;
  101. bus = bus->parent;
  102. }
  103. /* We have reached the root bus. */
  104. if (bus->bridge)
  105. acpi_pm_device_sleep_wake(bus->bridge, enable);
  106. }
  107. static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
  108. {
  109. if (acpi_pci_can_wakeup(dev))
  110. return acpi_pm_device_sleep_wake(&dev->dev, enable);
  111. acpi_pci_propagate_wakeup_enable(dev->bus, enable);
  112. return 0;
  113. }
  114. static struct pci_platform_pm_ops acpi_pci_platform_pm = {
  115. .is_manageable = acpi_pci_power_manageable,
  116. .set_state = acpi_pci_set_power_state,
  117. .choose_state = acpi_pci_choose_state,
  118. .can_wakeup = acpi_pci_can_wakeup,
  119. .sleep_wake = acpi_pci_sleep_wake,
  120. };
  121. /* ACPI bus type */
  122. static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
  123. {
  124. struct pci_dev * pci_dev;
  125. acpi_integer addr;
  126. pci_dev = to_pci_dev(dev);
  127. /* Please ref to ACPI spec for the syntax of _ADR */
  128. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  129. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
  130. if (!*handle)
  131. return -ENODEV;
  132. return 0;
  133. }
  134. static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
  135. {
  136. int num;
  137. unsigned int seg, bus;
  138. /*
  139. * The string should be the same as root bridge's name
  140. * Please look at 'pci_scan_bus_parented'
  141. */
  142. num = sscanf(dev_name(dev), "pci%04x:%02x", &seg, &bus);
  143. if (num != 2)
  144. return -ENODEV;
  145. *handle = acpi_get_pci_rootbridge_handle(seg, bus);
  146. if (!*handle)
  147. return -ENODEV;
  148. return 0;
  149. }
  150. static struct acpi_bus_type acpi_pci_bus = {
  151. .bus = &pci_bus_type,
  152. .find_device = acpi_pci_find_device,
  153. .find_bridge = acpi_pci_find_root_bridge,
  154. };
  155. static int __init acpi_pci_init(void)
  156. {
  157. int ret;
  158. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
  159. printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
  160. pci_no_msi();
  161. }
  162. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  163. printk(KERN_INFO"ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
  164. pcie_no_aspm();
  165. }
  166. ret = register_acpi_bus_type(&acpi_pci_bus);
  167. if (ret)
  168. return 0;
  169. pci_set_platform_pm(&acpi_pci_platform_pm);
  170. return 0;
  171. }
  172. arch_initcall(acpi_pci_init);