[ACPI] PCI can now get suspend state from firmware
[linux-2.6/verdex.git] / drivers / pci / pci-acpi.c
blob8eb599708de8b9053bddeb88419da294eec8f9b8
1 /*
2 * File: pci-acpi.c
3 * Purpose: Provde PCI support in ACPI
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 */
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/pci.h>
13 #include <linux/module.h>
14 #include <acpi/acpi.h>
15 #include <acpi/acnamesp.h>
16 #include <acpi/acresrc.h>
17 #include <acpi/acpi_bus.h>
19 #include <linux/pci-acpi.h>
20 #include "pci.h"
22 static u32 ctrlset_buf[3] = {0, 0, 0};
23 static u32 global_ctrlsets = 0;
24 static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
26 static acpi_status
27 acpi_query_osc (
28 acpi_handle handle,
29 u32 level,
30 void *context,
31 void **retval )
33 acpi_status status;
34 struct acpi_object_list input;
35 union acpi_object in_params[4];
36 struct acpi_buffer output;
37 union acpi_object out_obj;
38 u32 osc_dw0;
40 /* Setting up output buffer */
41 output.length = sizeof(out_obj) + 3*sizeof(u32);
42 output.pointer = &out_obj;
44 /* Setting up input parameters */
45 input.count = 4;
46 input.pointer = in_params;
47 in_params[0].type = ACPI_TYPE_BUFFER;
48 in_params[0].buffer.length = 16;
49 in_params[0].buffer.pointer = OSC_UUID;
50 in_params[1].type = ACPI_TYPE_INTEGER;
51 in_params[1].integer.value = 1;
52 in_params[2].type = ACPI_TYPE_INTEGER;
53 in_params[2].integer.value = 3;
54 in_params[3].type = ACPI_TYPE_BUFFER;
55 in_params[3].buffer.length = 12;
56 in_params[3].buffer.pointer = (u8 *)context;
58 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
59 if (ACPI_FAILURE (status)) {
60 printk(KERN_DEBUG
61 "Evaluate _OSC Set fails. Status = 0x%04x\n", status);
62 return status;
64 if (out_obj.type != ACPI_TYPE_BUFFER) {
65 printk(KERN_DEBUG
66 "Evaluate _OSC returns wrong type\n");
67 return AE_TYPE;
69 osc_dw0 = *((u32 *) out_obj.buffer.pointer);
70 if (osc_dw0) {
71 if (osc_dw0 & OSC_REQUEST_ERROR)
72 printk(KERN_DEBUG "_OSC request fails\n");
73 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
74 printk(KERN_DEBUG "_OSC invalid UUID\n");
75 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
76 printk(KERN_DEBUG "_OSC invalid revision\n");
77 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
78 /* Update Global Control Set */
79 global_ctrlsets = *((u32 *)(out_obj.buffer.pointer+8));
80 return AE_OK;
82 return AE_ERROR;
85 /* Update Global Control Set */
86 global_ctrlsets = *((u32 *)(out_obj.buffer.pointer + 8));
87 return AE_OK;
91 static acpi_status
92 acpi_run_osc (
93 acpi_handle handle,
94 u32 level,
95 void *context,
96 void **retval )
98 acpi_status status;
99 struct acpi_object_list input;
100 union acpi_object in_params[4];
101 struct acpi_buffer output;
102 union acpi_object out_obj;
103 u32 osc_dw0;
105 /* Setting up output buffer */
106 output.length = sizeof(out_obj) + 3*sizeof(u32);
107 output.pointer = &out_obj;
109 /* Setting up input parameters */
110 input.count = 4;
111 input.pointer = in_params;
112 in_params[0].type = ACPI_TYPE_BUFFER;
113 in_params[0].buffer.length = 16;
114 in_params[0].buffer.pointer = OSC_UUID;
115 in_params[1].type = ACPI_TYPE_INTEGER;
116 in_params[1].integer.value = 1;
117 in_params[2].type = ACPI_TYPE_INTEGER;
118 in_params[2].integer.value = 3;
119 in_params[3].type = ACPI_TYPE_BUFFER;
120 in_params[3].buffer.length = 12;
121 in_params[3].buffer.pointer = (u8 *)context;
123 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
124 if (ACPI_FAILURE (status)) {
125 printk(KERN_DEBUG
126 "Evaluate _OSC Set fails. Status = 0x%04x\n", status);
127 return status;
129 if (out_obj.type != ACPI_TYPE_BUFFER) {
130 printk(KERN_DEBUG
131 "Evaluate _OSC returns wrong type\n");
132 return AE_TYPE;
134 osc_dw0 = *((u32 *) out_obj.buffer.pointer);
135 if (osc_dw0) {
136 if (osc_dw0 & OSC_REQUEST_ERROR)
137 printk(KERN_DEBUG "_OSC request fails\n");
138 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
139 printk(KERN_DEBUG "_OSC invalid UUID\n");
140 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
141 printk(KERN_DEBUG "_OSC invalid revision\n");
142 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
143 printk(KERN_DEBUG "_OSC FW not grant req. control\n");
144 return AE_SUPPORT;
146 return AE_ERROR;
148 return AE_OK;
152 * pci_osc_support_set - register OS support to Firmware
153 * @flags: OS support bits
155 * Update OS support fields and doing a _OSC Query to obtain an update
156 * from Firmware on supported control bits.
158 acpi_status pci_osc_support_set(u32 flags)
160 u32 temp;
162 if (!(flags & OSC_SUPPORT_MASKS)) {
163 return AE_TYPE;
165 ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
167 /* do _OSC query for all possible controls */
168 temp = ctrlset_buf[OSC_CONTROL_TYPE];
169 ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
170 ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
171 acpi_get_devices ( PCI_ROOT_HID_STRING,
172 acpi_query_osc,
173 ctrlset_buf,
174 NULL );
175 ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
176 ctrlset_buf[OSC_CONTROL_TYPE] = temp;
177 return AE_OK;
179 EXPORT_SYMBOL(pci_osc_support_set);
182 * pci_osc_control_set - commit requested control to Firmware
183 * @flags: driver's requested control bits
185 * Attempt to take control from Firmware on requested control bits.
187 acpi_status pci_osc_control_set(u32 flags)
189 acpi_status status;
190 u32 ctrlset;
192 ctrlset = (flags & OSC_CONTROL_MASKS);
193 if (!ctrlset) {
194 return AE_TYPE;
196 if (ctrlset_buf[OSC_SUPPORT_TYPE] &&
197 ((global_ctrlsets & ctrlset) != ctrlset)) {
198 return AE_SUPPORT;
200 ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
201 status = acpi_get_devices ( PCI_ROOT_HID_STRING,
202 acpi_run_osc,
203 ctrlset_buf,
204 NULL );
205 if (ACPI_FAILURE (status)) {
206 ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
209 return status;
211 EXPORT_SYMBOL(pci_osc_control_set);
214 * _SxD returns the D-state with the highest power
215 * (lowest D-state number) supported in the S-state "x".
217 * If the devices does not have a _PRW
218 * (Power Resources for Wake) supporting system wakeup from "x"
219 * then the OS is free to choose a lower power (higher number
220 * D-state) than the return value from _SxD.
222 * But if _PRW is enabled at S-state "x", the OS
223 * must not choose a power lower than _SxD --
224 * unless the device has an _SxW method specifying
225 * the lowest power (highest D-state number) the device
226 * may enter while still able to wake the system.
228 * ie. depending on global OS policy:
230 * if (_PRW at S-state x)
231 * choose from highest power _SxD to lowest power _SxW
232 * else // no _PRW at S-state x
233 * choose highest power _SxD or any lower power
235 * currently we simply return _SxD, if present.
238 static int acpi_pci_choose_state(struct pci_dev *pdev, pm_message_t state)
240 char dstate_str[] = "_S0D";
241 acpi_status status;
242 unsigned long val;
243 struct device *dev = &pdev->dev;
245 /* Fixme: the check is wrong after pm_message_t is a struct */
246 if ((state >= PM_SUSPEND_MAX) || !DEVICE_ACPI_HANDLE(dev))
247 return -EINVAL;
248 dstate_str[2] += state; /* _S1D, _S2D, _S3D, _S4D */
249 status = acpi_evaluate_integer(DEVICE_ACPI_HANDLE(dev), dstate_str,
250 NULL, &val);
251 if (ACPI_SUCCESS(status))
252 return val;
253 return -ENODEV;
256 /* ACPI bus type */
257 static int pci_acpi_find_device(struct device *dev, acpi_handle *handle)
259 struct pci_dev * pci_dev;
260 acpi_integer addr;
262 pci_dev = to_pci_dev(dev);
263 /* Please ref to ACPI spec for the syntax of _ADR */
264 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
265 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
266 if (!*handle)
267 return -ENODEV;
268 return 0;
271 static int pci_acpi_find_root_bridge(struct device *dev, acpi_handle *handle)
273 int num;
274 unsigned int seg, bus;
277 * The string should be the same as root bridge's name
278 * Please look at 'pci_scan_bus_parented'
280 num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
281 if (num != 2)
282 return -ENODEV;
283 *handle = acpi_get_pci_rootbridge_handle(seg, bus);
284 if (!*handle)
285 return -ENODEV;
286 return 0;
289 static struct acpi_bus_type pci_acpi_bus = {
290 .bus = &pci_bus_type,
291 .find_device = pci_acpi_find_device,
292 .find_bridge = pci_acpi_find_root_bridge,
295 static int __init pci_acpi_init(void)
297 int ret;
299 ret = register_acpi_bus_type(&pci_acpi_bus);
300 if (ret)
301 return 0;
302 platform_pci_choose_state = acpi_pci_choose_state;
303 return 0;
305 arch_initcall(pci_acpi_init);