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