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
;
64 u32 flags
= (unsigned long)context
, temp
;
67 status
= acpi_get_handle(handle
, "_OSC", &tmp
);
68 if (ACPI_FAILURE(status
))
71 osc_data
= acpi_get_osc_data(handle
);
73 printk(KERN_ERR
"acpi osc data array is full\n");
77 osc_data
->ctrlset_buf
[OSC_SUPPORT_TYPE
] |= (flags
& OSC_SUPPORT_MASKS
);
79 /* do _OSC query for all possible controls */
80 temp
= osc_data
->ctrlset_buf
[OSC_CONTROL_TYPE
];
81 osc_data
->ctrlset_buf
[OSC_QUERY_TYPE
] = OSC_QUERY_ENABLE
;
82 osc_data
->ctrlset_buf
[OSC_CONTROL_TYPE
] = OSC_CONTROL_MASKS
;
84 /* Setting up input parameters */
86 input
.pointer
= in_params
;
87 in_params
[0].type
= ACPI_TYPE_BUFFER
;
88 in_params
[0].buffer
.length
= 16;
89 in_params
[0].buffer
.pointer
= OSC_UUID
;
90 in_params
[1].type
= ACPI_TYPE_INTEGER
;
91 in_params
[1].integer
.value
= 1;
92 in_params
[2].type
= ACPI_TYPE_INTEGER
;
93 in_params
[2].integer
.value
= 3;
94 in_params
[3].type
= ACPI_TYPE_BUFFER
;
95 in_params
[3].buffer
.length
= 12;
96 in_params
[3].buffer
.pointer
= (u8
*)osc_data
->ctrlset_buf
;
98 status
= acpi_evaluate_object(handle
, "_OSC", &input
, &output
);
99 if (ACPI_FAILURE(status
))
101 out_obj
= output
.pointer
;
103 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
105 "Evaluate _OSC returns wrong type\n");
109 osc_dw0
= *((u32
*) out_obj
->buffer
.pointer
);
111 if (osc_dw0
& OSC_REQUEST_ERROR
)
112 printk(KERN_DEBUG
"_OSC request fails\n");
113 if (osc_dw0
& OSC_INVALID_UUID_ERROR
)
114 printk(KERN_DEBUG
"_OSC invalid UUID\n");
115 if (osc_dw0
& OSC_INVALID_REVISION_ERROR
)
116 printk(KERN_DEBUG
"_OSC invalid revision\n");
117 if (osc_dw0
& OSC_CAPABILITIES_MASK_ERROR
) {
118 /* Update Global Control Set */
119 osc_data
->global_ctrlsets
=
120 *((u32
*)(out_obj
->buffer
.pointer
+ 8));
128 /* Update Global Control Set */
129 osc_data
->global_ctrlsets
= *((u32
*)(out_obj
->buffer
.pointer
+ 8));
133 kfree(output
.pointer
);
135 *ret_status
= status
;
137 osc_data
->ctrlset_buf
[OSC_QUERY_TYPE
] = !OSC_QUERY_ENABLE
;
138 osc_data
->ctrlset_buf
[OSC_CONTROL_TYPE
] = temp
;
139 if (ACPI_FAILURE(status
)) {
140 /* no osc support at all */
141 osc_data
->ctrlset_buf
[OSC_SUPPORT_TYPE
] = 0;
154 struct acpi_object_list input
;
155 union acpi_object in_params
[4];
156 struct acpi_buffer output
= {ACPI_ALLOCATE_BUFFER
, NULL
};
157 union acpi_object
*out_obj
;
160 /* Setting up input parameters */
162 input
.pointer
= in_params
;
163 in_params
[0].type
= ACPI_TYPE_BUFFER
;
164 in_params
[0].buffer
.length
= 16;
165 in_params
[0].buffer
.pointer
= OSC_UUID
;
166 in_params
[1].type
= ACPI_TYPE_INTEGER
;
167 in_params
[1].integer
.value
= 1;
168 in_params
[2].type
= ACPI_TYPE_INTEGER
;
169 in_params
[2].integer
.value
= 3;
170 in_params
[3].type
= ACPI_TYPE_BUFFER
;
171 in_params
[3].buffer
.length
= 12;
172 in_params
[3].buffer
.pointer
= (u8
*)context
;
174 status
= acpi_evaluate_object(handle
, "_OSC", &input
, &output
);
175 if (ACPI_FAILURE (status
))
178 out_obj
= output
.pointer
;
179 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
181 "Evaluate _OSC returns wrong type\n");
185 osc_dw0
= *((u32
*) out_obj
->buffer
.pointer
);
187 if (osc_dw0
& OSC_REQUEST_ERROR
)
188 printk(KERN_DEBUG
"_OSC request fails\n");
189 if (osc_dw0
& OSC_INVALID_UUID_ERROR
)
190 printk(KERN_DEBUG
"_OSC invalid UUID\n");
191 if (osc_dw0
& OSC_INVALID_REVISION_ERROR
)
192 printk(KERN_DEBUG
"_OSC invalid revision\n");
193 if (osc_dw0
& OSC_CAPABILITIES_MASK_ERROR
) {
194 printk(KERN_DEBUG
"_OSC FW not grant req. control\n");
204 kfree(output
.pointer
);
209 * __pci_osc_support_set - register OS support to Firmware
210 * @flags: OS support bits
213 * Update OS support fields and doing a _OSC Query to obtain an update
214 * from Firmware on supported control bits.
216 acpi_status
__pci_osc_support_set(u32 flags
, const char *hid
)
218 acpi_status retval
= AE_NOT_FOUND
;
220 if (!(flags
& OSC_SUPPORT_MASKS
)) {
223 acpi_get_devices(hid
,
225 (void *)(unsigned long)flags
,
231 * pci_osc_control_set - commit requested control to Firmware
232 * @handle: acpi_handle for the target ACPI object
233 * @flags: driver's requested control bits
235 * Attempt to take control from Firmware on requested control bits.
237 acpi_status
pci_osc_control_set(acpi_handle handle
, u32 flags
)
242 struct acpi_osc_data
*osc_data
;
244 status
= acpi_get_handle(handle
, "_OSC", &tmp
);
245 if (ACPI_FAILURE(status
))
248 osc_data
= acpi_get_osc_data(handle
);
250 printk(KERN_ERR
"acpi osc data array is full\n");
254 ctrlset
= (flags
& OSC_CONTROL_MASKS
);
258 if (osc_data
->ctrlset_buf
[OSC_SUPPORT_TYPE
] &&
259 ((osc_data
->global_ctrlsets
& ctrlset
) != ctrlset
)) {
262 osc_data
->ctrlset_buf
[OSC_CONTROL_TYPE
] |= ctrlset
;
263 status
= acpi_run_osc(handle
, osc_data
->ctrlset_buf
);
264 if (ACPI_FAILURE (status
)) {
265 osc_data
->ctrlset_buf
[OSC_CONTROL_TYPE
] &= ~ctrlset
;
270 EXPORT_SYMBOL(pci_osc_control_set
);
272 #ifdef CONFIG_ACPI_SLEEP
274 * _SxD returns the D-state with the highest power
275 * (lowest D-state number) supported in the S-state "x".
277 * If the devices does not have a _PRW
278 * (Power Resources for Wake) supporting system wakeup from "x"
279 * then the OS is free to choose a lower power (higher number
280 * D-state) than the return value from _SxD.
282 * But if _PRW is enabled at S-state "x", the OS
283 * must not choose a power lower than _SxD --
284 * unless the device has an _SxW method specifying
285 * the lowest power (highest D-state number) the device
286 * may enter while still able to wake the system.
288 * ie. depending on global OS policy:
290 * if (_PRW at S-state x)
291 * choose from highest power _SxD to lowest power _SxW
292 * else // no _PRW at S-state x
293 * choose highest power _SxD or any lower power
296 static pci_power_t
acpi_pci_choose_state(struct pci_dev
*pdev
,
301 acpi_state
= acpi_pm_device_sleep_state(&pdev
->dev
,
302 device_may_wakeup(&pdev
->dev
), NULL
);
304 return PCI_POWER_ERROR
;
306 switch (acpi_state
) {
316 return PCI_POWER_ERROR
;
320 static int acpi_pci_set_power_state(struct pci_dev
*dev
, pci_power_t state
)
322 acpi_handle handle
= DEVICE_ACPI_HANDLE(&dev
->dev
);
324 static const u8 state_conv
[] = {
325 [PCI_D0
] = ACPI_STATE_D0
,
326 [PCI_D1
] = ACPI_STATE_D1
,
327 [PCI_D2
] = ACPI_STATE_D2
,
328 [PCI_D3hot
] = ACPI_STATE_D3
,
329 [PCI_D3cold
] = ACPI_STATE_D3
334 /* If the ACPI device has _EJ0, ignore the device */
335 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_EJ0", &tmp
)))
344 return acpi_bus_set_power(handle
, state_conv
[state
]);
351 static int acpi_pci_find_device(struct device
*dev
, acpi_handle
*handle
)
353 struct pci_dev
* pci_dev
;
356 pci_dev
= to_pci_dev(dev
);
357 /* Please ref to ACPI spec for the syntax of _ADR */
358 addr
= (PCI_SLOT(pci_dev
->devfn
) << 16) | PCI_FUNC(pci_dev
->devfn
);
359 *handle
= acpi_get_child(DEVICE_ACPI_HANDLE(dev
->parent
), addr
);
365 static int acpi_pci_find_root_bridge(struct device
*dev
, acpi_handle
*handle
)
368 unsigned int seg
, bus
;
371 * The string should be the same as root bridge's name
372 * Please look at 'pci_scan_bus_parented'
374 num
= sscanf(dev
->bus_id
, "pci%04x:%02x", &seg
, &bus
);
377 *handle
= acpi_get_pci_rootbridge_handle(seg
, bus
);
383 static struct acpi_bus_type acpi_pci_bus
= {
384 .bus
= &pci_bus_type
,
385 .find_device
= acpi_pci_find_device
,
386 .find_bridge
= acpi_pci_find_root_bridge
,
389 static int __init
acpi_pci_init(void)
393 if (acpi_gbl_FADT
.boot_flags
& BAF_MSI_NOT_SUPPORTED
) {
394 printk(KERN_INFO
"ACPI FADT declares the system doesn't support MSI, so disable it\n");
397 ret
= register_acpi_bus_type(&acpi_pci_bus
);
400 #ifdef CONFIG_ACPI_SLEEP
401 platform_pci_choose_state
= acpi_pci_choose_state
;
403 platform_pci_set_power_state
= acpi_pci_set_power_state
;
406 arch_initcall(acpi_pci_init
);