pci-acpi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
  97. {
  98. int error = acpi_pm_device_sleep_wake(&dev->dev, enable);
  99. if (!error)
  100. dev_printk(KERN_INFO, &dev->dev,
  101. "wake-up capability %s by ACPI\n",
  102. enable ? "enabled" : "disabled");
  103. return error;
  104. }
  105. static struct pci_platform_pm_ops acpi_pci_platform_pm = {
  106. .is_manageable = acpi_pci_power_manageable,
  107. .set_state = acpi_pci_set_power_state,
  108. .choose_state = acpi_pci_choose_state,
  109. .can_wakeup = acpi_pci_can_wakeup,
  110. .sleep_wake = acpi_pci_sleep_wake,
  111. };
  112. /* ACPI bus type */
  113. static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
  114. {
  115. struct pci_dev * pci_dev;
  116. acpi_integer addr;
  117. pci_dev = to_pci_dev(dev);
  118. /* Please ref to ACPI spec for the syntax of _ADR */
  119. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  120. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
  121. if (!*handle)
  122. return -ENODEV;
  123. return 0;
  124. }
  125. static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
  126. {
  127. int num;
  128. unsigned int seg, bus;
  129. /*
  130. * The string should be the same as root bridge's name
  131. * Please look at 'pci_scan_bus_parented'
  132. */
  133. num = sscanf(dev_name(dev), "pci%04x:%02x", &seg, &bus);
  134. if (num != 2)
  135. return -ENODEV;
  136. *handle = acpi_get_pci_rootbridge_handle(seg, bus);
  137. if (!*handle)
  138. return -ENODEV;
  139. return 0;
  140. }
  141. static struct acpi_bus_type acpi_pci_bus = {
  142. .bus = &pci_bus_type,
  143. .find_device = acpi_pci_find_device,
  144. .find_bridge = acpi_pci_find_root_bridge,
  145. };
  146. static int __init acpi_pci_init(void)
  147. {
  148. int ret;
  149. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
  150. printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
  151. pci_no_msi();
  152. }
  153. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  154. printk(KERN_INFO"ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
  155. pcie_no_aspm();
  156. }
  157. ret = register_acpi_bus_type(&acpi_pci_bus);
  158. if (ret)
  159. return 0;
  160. pci_set_platform_pm(&acpi_pci_platform_pm);
  161. return 0;
  162. }
  163. arch_initcall(acpi_pci_init);