3 * Purpose: Provide 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.
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>
22 struct acpi_osc_data {
26 struct list_head sibiling;
28 static LIST_HEAD(acpi_osc_data_list);
30 static struct acpi_osc_data *acpi_get_osc_data(acpi_handle handle)
32 struct acpi_osc_data *data;
34 list_for_each_entry(data, &acpi_osc_data_list, sibiling) {
35 if (data->handle == handle)
38 data = kzalloc(sizeof(*data), GFP_KERNEL);
41 INIT_LIST_HEAD(&data->sibiling);
42 data->handle = handle;
43 list_add_tail(&data->sibiling, &acpi_osc_data_list);
47 static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
57 struct acpi_object_list input;
58 union acpi_object in_params[4];
59 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
60 union acpi_object *out_obj;
62 acpi_status *ret_status = (acpi_status *)retval;
63 struct acpi_osc_data *osc_data = acpi_get_osc_data(handle);
64 u32 flags = (unsigned long)context, temp;
67 printk(KERN_ERR "acpi osc data array is full\n");
71 osc_data->ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
73 /* do _OSC query for all possible controls */
74 temp = osc_data->ctrlset_buf[OSC_CONTROL_TYPE];
75 osc_data->ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
76 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
78 /* Setting up input parameters */
80 input.pointer = in_params;
81 in_params[0].type = ACPI_TYPE_BUFFER;
82 in_params[0].buffer.length = 16;
83 in_params[0].buffer.pointer = OSC_UUID;
84 in_params[1].type = ACPI_TYPE_INTEGER;
85 in_params[1].integer.value = 1;
86 in_params[2].type = ACPI_TYPE_INTEGER;
87 in_params[2].integer.value = 3;
88 in_params[3].type = ACPI_TYPE_BUFFER;
89 in_params[3].buffer.length = 12;
90 in_params[3].buffer.pointer = (u8 *)osc_data->ctrlset_buf;
92 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
93 if (ACPI_FAILURE(status))
95 out_obj = output.pointer;
97 if (out_obj->type != ACPI_TYPE_BUFFER) {
99 "Evaluate _OSC returns wrong type\n");
103 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
105 if (osc_dw0 & OSC_REQUEST_ERROR)
106 printk(KERN_DEBUG "_OSC request fails\n");
107 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
108 printk(KERN_DEBUG "_OSC invalid UUID\n");
109 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
110 printk(KERN_DEBUG "_OSC invalid revision\n");
111 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
112 /* Update Global Control Set */
113 osc_data->global_ctrlsets =
114 *((u32 *)(out_obj->buffer.pointer + 8));
122 /* Update Global Control Set */
123 osc_data->global_ctrlsets = *((u32 *)(out_obj->buffer.pointer + 8));
127 kfree(output.pointer);
129 *ret_status = status;
131 osc_data->ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
132 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] = temp;
133 if (ACPI_FAILURE(status)) {
134 /* no osc support at all */
135 osc_data->ctrlset_buf[OSC_SUPPORT_TYPE] = 0;
148 struct acpi_object_list input;
149 union acpi_object in_params[4];
150 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
151 union acpi_object *out_obj;
154 /* Setting up input parameters */
156 input.pointer = in_params;
157 in_params[0].type = ACPI_TYPE_BUFFER;
158 in_params[0].buffer.length = 16;
159 in_params[0].buffer.pointer = OSC_UUID;
160 in_params[1].type = ACPI_TYPE_INTEGER;
161 in_params[1].integer.value = 1;
162 in_params[2].type = ACPI_TYPE_INTEGER;
163 in_params[2].integer.value = 3;
164 in_params[3].type = ACPI_TYPE_BUFFER;
165 in_params[3].buffer.length = 12;
166 in_params[3].buffer.pointer = (u8 *)context;
168 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
169 if (ACPI_FAILURE (status))
172 out_obj = output.pointer;
173 if (out_obj->type != ACPI_TYPE_BUFFER) {
175 "Evaluate _OSC returns wrong type\n");
179 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
181 if (osc_dw0 & OSC_REQUEST_ERROR)
182 printk(KERN_DEBUG "_OSC request fails\n");
183 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
184 printk(KERN_DEBUG "_OSC invalid UUID\n");
185 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
186 printk(KERN_DEBUG "_OSC invalid revision\n");
187 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
188 printk(KERN_DEBUG "_OSC FW not grant req. control\n");
198 kfree(output.pointer);
203 * __pci_osc_support_set - register OS support to Firmware
204 * @flags: OS support bits
207 * Update OS support fields and doing a _OSC Query to obtain an update
208 * from Firmware on supported control bits.
210 acpi_status __pci_osc_support_set(u32 flags, const char *hid)
212 acpi_status retval = AE_NOT_FOUND;
214 if (!(flags & OSC_SUPPORT_MASKS)) {
217 acpi_get_devices(hid,
219 (void *)(unsigned long)flags,
225 * pci_osc_control_set - commit requested control to Firmware
226 * @handle: acpi_handle for the target ACPI object
227 * @flags: driver's requested control bits
229 * Attempt to take control from Firmware on requested control bits.
231 acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
236 struct acpi_osc_data *osc_data;
238 status = acpi_get_handle(handle, "_OSC", &tmp);
239 if (ACPI_FAILURE(status))
242 osc_data = acpi_get_osc_data(handle);
244 printk(KERN_ERR "acpi osc data array is full\n");
248 ctrlset = (flags & OSC_CONTROL_MASKS);
252 if (osc_data->ctrlset_buf[OSC_SUPPORT_TYPE] &&
253 ((osc_data->global_ctrlsets & ctrlset) != ctrlset)) {
256 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
257 status = acpi_run_osc(handle, osc_data->ctrlset_buf);
258 if (ACPI_FAILURE (status)) {
259 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
264 EXPORT_SYMBOL(pci_osc_control_set);
266 #ifdef CONFIG_ACPI_SLEEP
268 * _SxD returns the D-state with the highest power
269 * (lowest D-state number) supported in the S-state "x".
271 * If the devices does not have a _PRW
272 * (Power Resources for Wake) supporting system wakeup from "x"
273 * then the OS is free to choose a lower power (higher number
274 * D-state) than the return value from _SxD.
276 * But if _PRW is enabled at S-state "x", the OS
277 * must not choose a power lower than _SxD --
278 * unless the device has an _SxW method specifying
279 * the lowest power (highest D-state number) the device
280 * may enter while still able to wake the system.
282 * ie. depending on global OS policy:
284 * if (_PRW at S-state x)
285 * choose from highest power _SxD to lowest power _SxW
286 * else // no _PRW at S-state x
287 * choose highest power _SxD or any lower power
290 static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev,
295 acpi_state = acpi_pm_device_sleep_state(&pdev->dev,
296 device_may_wakeup(&pdev->dev), NULL);
298 return PCI_POWER_ERROR;
300 switch (acpi_state) {
310 return PCI_POWER_ERROR;
314 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
316 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
318 static const u8 state_conv[] = {
319 [PCI_D0] = ACPI_STATE_D0,
320 [PCI_D1] = ACPI_STATE_D1,
321 [PCI_D2] = ACPI_STATE_D2,
322 [PCI_D3hot] = ACPI_STATE_D3,
323 [PCI_D3cold] = ACPI_STATE_D3
328 /* If the ACPI device has _EJ0, ignore the device */
329 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
338 return acpi_bus_set_power(handle, state_conv[state]);
345 static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
347 struct pci_dev * pci_dev;
350 pci_dev = to_pci_dev(dev);
351 /* Please ref to ACPI spec for the syntax of _ADR */
352 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
353 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
359 static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
362 unsigned int seg, bus;
365 * The string should be the same as root bridge's name
366 * Please look at 'pci_scan_bus_parented'
368 num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
371 *handle = acpi_get_pci_rootbridge_handle(seg, bus);
377 static struct acpi_bus_type acpi_pci_bus = {
378 .bus = &pci_bus_type,
379 .find_device = acpi_pci_find_device,
380 .find_bridge = acpi_pci_find_root_bridge,
383 static int __init acpi_pci_init(void)
387 if (acpi_gbl_FADT.boot_flags & BAF_MSI_NOT_SUPPORTED) {
388 printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
391 ret = register_acpi_bus_type(&acpi_pci_bus);
394 #ifdef CONFIG_ACPI_SLEEP
395 platform_pci_choose_state = acpi_pci_choose_state;
397 platform_pci_set_power_state = acpi_pci_set_power_state;
400 arch_initcall(acpi_pci_init);