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 {
27 struct list_head sibiling;
29 static LIST_HEAD(acpi_osc_data_list);
31 struct acpi_osc_args {
36 static struct acpi_osc_data *acpi_get_osc_data(acpi_handle handle)
38 struct acpi_osc_data *data;
40 list_for_each_entry(data, &acpi_osc_data_list, sibiling) {
41 if (data->handle == handle)
44 data = kzalloc(sizeof(*data), GFP_KERNEL);
47 INIT_LIST_HEAD(&data->sibiling);
48 data->handle = handle;
49 list_add_tail(&data->sibiling, &acpi_osc_data_list);
53 static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
55 static acpi_status acpi_run_osc(acpi_handle handle,
56 struct acpi_osc_args *osc_args)
59 struct acpi_object_list input;
60 union acpi_object in_params[4];
61 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
62 union acpi_object *out_obj;
63 u32 osc_dw0, flags = osc_args->capbuf[OSC_QUERY_TYPE];
65 /* Setting up input parameters */
67 input.pointer = in_params;
68 in_params[0].type = ACPI_TYPE_BUFFER;
69 in_params[0].buffer.length = 16;
70 in_params[0].buffer.pointer = OSC_UUID;
71 in_params[1].type = ACPI_TYPE_INTEGER;
72 in_params[1].integer.value = 1;
73 in_params[2].type = ACPI_TYPE_INTEGER;
74 in_params[2].integer.value = 3;
75 in_params[3].type = ACPI_TYPE_BUFFER;
76 in_params[3].buffer.length = 12;
77 in_params[3].buffer.pointer = (u8 *)osc_args->capbuf;
79 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
80 if (ACPI_FAILURE(status))
83 out_obj = output.pointer;
84 if (out_obj->type != ACPI_TYPE_BUFFER) {
85 printk(KERN_DEBUG "Evaluate _OSC returns wrong type\n");
89 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
91 if (osc_dw0 & OSC_REQUEST_ERROR)
92 printk(KERN_DEBUG "_OSC request fails\n");
93 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
94 printk(KERN_DEBUG "_OSC invalid UUID\n");
95 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
96 printk(KERN_DEBUG "_OSC invalid revision\n");
97 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
98 if (flags & OSC_QUERY_ENABLE)
100 printk(KERN_DEBUG "_OSC FW not grant req. control\n");
108 if (flags & OSC_QUERY_ENABLE)
109 osc_args->query_result =
110 *((u32 *)(out_obj->buffer.pointer + 8));
114 kfree(output.pointer);
118 static acpi_status acpi_query_osc(acpi_handle handle,
119 u32 level, void *context, void **retval)
122 acpi_status *ret_status = (acpi_status *)retval;
123 struct acpi_osc_data *osc_data;
124 u32 flags = (unsigned long)context, support_set;
126 struct acpi_osc_args osc_args;
128 status = acpi_get_handle(handle, "_OSC", &tmp);
129 if (ACPI_FAILURE(status))
132 osc_data = acpi_get_osc_data(handle);
134 printk(KERN_ERR "acpi osc data array is full\n");
138 /* do _OSC query for all possible controls */
139 support_set = osc_data->support_set | (flags & OSC_SUPPORT_MASKS);
140 osc_args.capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
141 osc_args.capbuf[OSC_SUPPORT_TYPE] = support_set;
142 osc_args.capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
144 status = acpi_run_osc(handle, &osc_args);
145 *ret_status = status;
147 if (ACPI_SUCCESS(status)) {
148 osc_data->support_set = support_set;
149 osc_data->query_result = osc_args.query_result;
156 * __pci_osc_support_set - register OS support to Firmware
157 * @flags: OS support bits
160 * Update OS support fields and doing a _OSC Query to obtain an update
161 * from Firmware on supported control bits.
163 acpi_status __pci_osc_support_set(u32 flags, const char *hid)
165 acpi_status retval = AE_NOT_FOUND;
167 if (!(flags & OSC_SUPPORT_MASKS)) {
170 acpi_get_devices(hid,
172 (void *)(unsigned long)flags,
178 * pci_osc_control_set - commit requested control to Firmware
179 * @handle: acpi_handle for the target ACPI object
180 * @flags: driver's requested control bits
182 * Attempt to take control from Firmware on requested control bits.
184 acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
187 u32 ctrlset, control_set;
189 struct acpi_osc_data *osc_data;
190 struct acpi_osc_args osc_args;
192 status = acpi_get_handle(handle, "_OSC", &tmp);
193 if (ACPI_FAILURE(status))
196 osc_data = acpi_get_osc_data(handle);
198 printk(KERN_ERR "acpi osc data array is full\n");
202 ctrlset = (flags & OSC_CONTROL_MASKS);
206 if (osc_data->support_set &&
207 ((osc_data->query_result & ctrlset) != ctrlset))
210 control_set = osc_data->control_set | ctrlset;
211 osc_args.capbuf[OSC_QUERY_TYPE] = 0;
212 osc_args.capbuf[OSC_SUPPORT_TYPE] = osc_data->support_set;
213 osc_args.capbuf[OSC_CONTROL_TYPE] = control_set;
214 status = acpi_run_osc(handle, &osc_args);
215 if (ACPI_SUCCESS(status))
216 osc_data->control_set = control_set;
220 EXPORT_SYMBOL(pci_osc_control_set);
222 #ifdef CONFIG_ACPI_SLEEP
224 * _SxD returns the D-state with the highest power
225 * (lowest D-state number) supported in the S-state "x".
227 * If the devices does not have a _PRW
228 * (Power Resources for Wake) supporting system wakeup from "x"
229 * then the OS is free to choose a lower power (higher number
230 * D-state) than the return value from _SxD.
232 * But if _PRW is enabled at S-state "x", the OS
233 * must not choose a power lower than _SxD --
234 * unless the device has an _SxW method specifying
235 * the lowest power (highest D-state number) the device
236 * may enter while still able to wake the system.
238 * ie. depending on global OS policy:
240 * if (_PRW at S-state x)
241 * choose from highest power _SxD to lowest power _SxW
242 * else // no _PRW at S-state x
243 * choose highest power _SxD or any lower power
246 static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev,
251 acpi_state = acpi_pm_device_sleep_state(&pdev->dev,
252 device_may_wakeup(&pdev->dev), NULL);
254 return PCI_POWER_ERROR;
256 switch (acpi_state) {
266 return PCI_POWER_ERROR;
270 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
272 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
274 static const u8 state_conv[] = {
275 [PCI_D0] = ACPI_STATE_D0,
276 [PCI_D1] = ACPI_STATE_D1,
277 [PCI_D2] = ACPI_STATE_D2,
278 [PCI_D3hot] = ACPI_STATE_D3,
279 [PCI_D3cold] = ACPI_STATE_D3
284 /* If the ACPI device has _EJ0, ignore the device */
285 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
294 return acpi_bus_set_power(handle, state_conv[state]);
301 static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
303 struct pci_dev * pci_dev;
306 pci_dev = to_pci_dev(dev);
307 /* Please ref to ACPI spec for the syntax of _ADR */
308 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
309 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
315 static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
318 unsigned int seg, bus;
321 * The string should be the same as root bridge's name
322 * Please look at 'pci_scan_bus_parented'
324 num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
327 *handle = acpi_get_pci_rootbridge_handle(seg, bus);
333 static struct acpi_bus_type acpi_pci_bus = {
334 .bus = &pci_bus_type,
335 .find_device = acpi_pci_find_device,
336 .find_bridge = acpi_pci_find_root_bridge,
339 static int __init acpi_pci_init(void)
343 if (acpi_gbl_FADT.boot_flags & BAF_MSI_NOT_SUPPORTED) {
344 printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
347 ret = register_acpi_bus_type(&acpi_pci_bus);
350 #ifdef CONFIG_ACPI_SLEEP
351 platform_pci_choose_state = acpi_pci_choose_state;
353 platform_pci_set_power_state = acpi_pci_set_power_state;
356 arch_initcall(acpi_pci_init);