blob: 38fc8b1ff8814456a09754f12668efd3ba2a0b62 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File: pci-acpi.c
Len Browna406d9e2005-03-23 16:16:03 -05003 * Purpose: Provide PCI support in ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
David Shaohua Li84df749f2005-03-18 18:53:36 -05005 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
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>
18
19#include <linux/pci-acpi.h>
David Shaohua Li0f644742005-03-19 00:15:48 -050020#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Shaohua Lia5d1c872008-05-12 10:48:10 +080022struct acpi_osc_data {
23 acpi_handle handle;
24 u32 ctrlset_buf[3];
25 u32 global_ctrlsets;
26 struct list_head sibiling;
27};
28static LIST_HEAD(acpi_osc_data_list);
29
30static struct acpi_osc_data *acpi_get_osc_data(acpi_handle handle)
31{
32 struct acpi_osc_data *data;
33
34 list_for_each_entry(data, &acpi_osc_data_list, sibiling) {
35 if (data->handle == handle)
36 return data;
37 }
38 data = kzalloc(sizeof(*data), GFP_KERNEL);
39 if (!data)
40 return NULL;
41 INIT_LIST_HEAD(&data->sibiling);
42 data->handle = handle;
43 list_add_tail(&data->sibiling, &acpi_osc_data_list);
44 return data;
45}
46
Greg KHbc56b9e2005-04-08 14:53:31 +090047static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static acpi_status
50acpi_query_osc (
51 acpi_handle handle,
52 u32 level,
53 void *context,
54 void **retval )
55{
56 acpi_status status;
57 struct acpi_object_list input;
58 union acpi_object in_params[4];
Kristen Accardi593ee202006-05-20 15:00:08 -070059 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
60 union acpi_object *out_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 u32 osc_dw0;
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -080062 acpi_status *ret_status = (acpi_status *)retval;
Shaohua Lia5d1c872008-05-12 10:48:10 +080063 struct acpi_osc_data *osc_data = acpi_get_osc_data(handle);
64 u32 flags = (unsigned long)context, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Shaohua Lia5d1c872008-05-12 10:48:10 +080066 if (!osc_data) {
67 printk(KERN_ERR "acpi osc data array is full\n");
68 return AE_ERROR;
69 }
70
71 osc_data->ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
72
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;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* Setting up input parameters */
79 input.count = 4;
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;
Shaohua Lia5d1c872008-05-12 10:48:10 +080090 in_params[3].buffer.pointer = (u8 *)osc_data->ctrlset_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
Shaohua Lia5d1c872008-05-12 10:48:10 +080093 if (ACPI_FAILURE(status))
94 goto out_nofree;
Kristen Accardi593ee202006-05-20 15:00:08 -070095 out_obj = output.pointer;
96
97 if (out_obj->type != ACPI_TYPE_BUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 printk(KERN_DEBUG
99 "Evaluate _OSC returns wrong type\n");
Kristen Accardi593ee202006-05-20 15:00:08 -0700100 status = AE_TYPE;
101 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700103 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (osc_dw0) {
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 */
Shaohua Lia5d1c872008-05-12 10:48:10 +0800113 osc_data->global_ctrlsets =
114 *((u32 *)(out_obj->buffer.pointer + 8));
Kristen Accardi593ee202006-05-20 15:00:08 -0700115 status = AE_OK;
116 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700118 status = AE_ERROR;
119 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
122 /* Update Global Control Set */
Shaohua Lia5d1c872008-05-12 10:48:10 +0800123 osc_data->global_ctrlsets = *((u32 *)(out_obj->buffer.pointer + 8));
Kristen Accardi593ee202006-05-20 15:00:08 -0700124 status = AE_OK;
125
126query_osc_out:
127 kfree(output.pointer);
Shaohua Lia5d1c872008-05-12 10:48:10 +0800128out_nofree:
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -0800129 *ret_status = status;
Shaohua Lia5d1c872008-05-12 10:48:10 +0800130
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;
136 }
137
Kristen Accardi593ee202006-05-20 15:00:08 -0700138 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141
142static acpi_status
143acpi_run_osc (
144 acpi_handle handle,
rajesh.shah@intel.com427bf532005-10-31 16:20:11 -0800145 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 acpi_status status;
148 struct acpi_object_list input;
149 union acpi_object in_params[4];
Kristen Accardi593ee202006-05-20 15:00:08 -0700150 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
151 union acpi_object *out_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 u32 osc_dw0;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* Setting up input parameters */
155 input.count = 4;
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;
167
168 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
Zhang, Yanmin8d29bfb2007-06-06 11:44:16 +0800169 if (ACPI_FAILURE (status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return status;
Zhang, Yanmin8d29bfb2007-06-06 11:44:16 +0800171
Kristen Accardi593ee202006-05-20 15:00:08 -0700172 out_obj = output.pointer;
173 if (out_obj->type != ACPI_TYPE_BUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 printk(KERN_DEBUG
175 "Evaluate _OSC returns wrong type\n");
Kristen Accardi593ee202006-05-20 15:00:08 -0700176 status = AE_TYPE;
177 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700179 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (osc_dw0) {
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");
Kristen Accardi593ee202006-05-20 15:00:08 -0700189 status = AE_SUPPORT;
190 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700192 status = AE_ERROR;
193 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700195 status = AE_OK;
196
197run_osc_out:
198 kfree(output.pointer);
199 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/**
Andrew Pattersonc2778352008-01-22 17:18:12 -0700203 * __pci_osc_support_set - register OS support to Firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * @flags: OS support bits
Randy Dunlap5958f1a2008-02-03 15:06:25 -0800205 * @hid: hardware ID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
207 * Update OS support fields and doing a _OSC Query to obtain an update
208 * from Firmware on supported control bits.
209 **/
Andrew Pattersonc2778352008-01-22 17:18:12 -0700210acpi_status __pci_osc_support_set(u32 flags, const char *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Kenji Kaneshige21e2b0a2008-05-08 14:37:25 +0900212 acpi_status retval = AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if (!(flags & OSC_SUPPORT_MASKS)) {
215 return AE_TYPE;
216 }
Andrew Pattersonc2778352008-01-22 17:18:12 -0700217 acpi_get_devices(hid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 acpi_query_osc,
Shaohua Lia5d1c872008-05-12 10:48:10 +0800219 (void *)(unsigned long)flags,
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -0800220 (void **) &retval );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return AE_OK;
222}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224/**
225 * pci_osc_control_set - commit requested control to Firmware
Randy Dunlapf3666332005-11-23 15:45:04 -0800226 * @handle: acpi_handle for the target ACPI object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * @flags: driver's requested control bits
228 *
229 * Attempt to take control from Firmware on requested control bits.
230 **/
rajesh.shah@intel.com427bf532005-10-31 16:20:11 -0800231acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 acpi_status status;
234 u32 ctrlset;
Kenji Kaneshige34a65052008-05-12 22:55:45 +0900235 acpi_handle tmp;
236 struct acpi_osc_data *osc_data;
Shaohua Lia5d1c872008-05-12 10:48:10 +0800237
Kenji Kaneshige34a65052008-05-12 22:55:45 +0900238 status = acpi_get_handle(handle, "_OSC", &tmp);
239 if (ACPI_FAILURE(status))
240 return status;
241
242 osc_data = acpi_get_osc_data(handle);
Shaohua Lia5d1c872008-05-12 10:48:10 +0800243 if (!osc_data) {
244 printk(KERN_ERR "acpi osc data array is full\n");
245 return AE_ERROR;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 ctrlset = (flags & OSC_CONTROL_MASKS);
249 if (!ctrlset) {
250 return AE_TYPE;
251 }
Shaohua Lia5d1c872008-05-12 10:48:10 +0800252 if (osc_data->ctrlset_buf[OSC_SUPPORT_TYPE] &&
253 ((osc_data->global_ctrlsets & ctrlset) != ctrlset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return AE_SUPPORT;
255 }
Shaohua Lia5d1c872008-05-12 10:48:10 +0800256 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
257 status = acpi_run_osc(handle, osc_data->ctrlset_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (ACPI_FAILURE (status)) {
Shaohua Lia5d1c872008-05-12 10:48:10 +0800259 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
262 return status;
263}
264EXPORT_SYMBOL(pci_osc_control_set);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500265
Len Brown673d5b42007-07-28 03:33:16 -0400266#ifdef CONFIG_ACPI_SLEEP
David Shaohua Li0f644742005-03-19 00:15:48 -0500267/*
268 * _SxD returns the D-state with the highest power
269 * (lowest D-state number) supported in the S-state "x".
270 *
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.
275 *
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.
281 *
282 * ie. depending on global OS policy:
283 *
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
David Shaohua Li0f644742005-03-19 00:15:48 -0500288 */
289
Shaohua Liab826ca2007-07-20 10:03:22 +0800290static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev,
291 pm_message_t state)
David Shaohua Li0f644742005-03-19 00:15:48 -0500292{
Shaohua Liab826ca2007-07-20 10:03:22 +0800293 int acpi_state;
David Shaohua Li0f644742005-03-19 00:15:48 -0500294
Shaohua Liab826ca2007-07-20 10:03:22 +0800295 acpi_state = acpi_pm_device_sleep_state(&pdev->dev,
296 device_may_wakeup(&pdev->dev), NULL);
297 if (acpi_state < 0)
298 return PCI_POWER_ERROR;
299
300 switch (acpi_state) {
301 case ACPI_STATE_D0:
302 return PCI_D0;
303 case ACPI_STATE_D1:
304 return PCI_D1;
305 case ACPI_STATE_D2:
306 return PCI_D2;
307 case ACPI_STATE_D3:
308 return PCI_D3hot;
309 }
310 return PCI_POWER_ERROR;
David Shaohua Li0f644742005-03-19 00:15:48 -0500311}
Len Brown673d5b42007-07-28 03:33:16 -0400312#endif
David Shaohua Li0f644742005-03-19 00:15:48 -0500313
David Shaohua Lib9131002005-03-19 00:16:18 -0500314static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
315{
316 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
Shaohua Li10b3dca2007-07-20 10:03:25 +0800317 acpi_handle tmp;
David Brownell583c3772008-02-22 21:41:51 -0800318 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
David Shaohua Lib9131002005-03-19 00:16:18 -0500324 };
David Shaohua Lib9131002005-03-19 00:16:18 -0500325
326 if (!handle)
327 return -ENODEV;
Shaohua Li10b3dca2007-07-20 10:03:25 +0800328 /* If the ACPI device has _EJ0, ignore the device */
329 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
330 return 0;
David Brownell583c3772008-02-22 21:41:51 -0800331
332 switch (state) {
333 case PCI_D0:
334 case PCI_D1:
335 case PCI_D2:
336 case PCI_D3hot:
337 case PCI_D3cold:
338 return acpi_bus_set_power(handle, state_conv[state]);
339 }
340 return -EINVAL;
David Shaohua Lib9131002005-03-19 00:16:18 -0500341}
342
343
David Shaohua Li84df749f2005-03-18 18:53:36 -0500344/* ACPI bus type */
Muthu Kumar9c273b92006-04-28 00:42:21 -0700345static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500346{
347 struct pci_dev * pci_dev;
348 acpi_integer addr;
349
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);
354 if (!*handle)
355 return -ENODEV;
356 return 0;
357}
358
Muthu Kumar9c273b92006-04-28 00:42:21 -0700359static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500360{
361 int num;
362 unsigned int seg, bus;
363
364 /*
365 * The string should be the same as root bridge's name
366 * Please look at 'pci_scan_bus_parented'
367 */
368 num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
369 if (num != 2)
370 return -ENODEV;
371 *handle = acpi_get_pci_rootbridge_handle(seg, bus);
372 if (!*handle)
373 return -ENODEV;
374 return 0;
375}
376
Muthu Kumar9c273b92006-04-28 00:42:21 -0700377static struct acpi_bus_type acpi_pci_bus = {
David Shaohua Li84df749f2005-03-18 18:53:36 -0500378 .bus = &pci_bus_type,
Muthu Kumar9c273b92006-04-28 00:42:21 -0700379 .find_device = acpi_pci_find_device,
380 .find_bridge = acpi_pci_find_root_bridge,
David Shaohua Li84df749f2005-03-18 18:53:36 -0500381};
382
Muthu Kumar9c273b92006-04-28 00:42:21 -0700383static int __init acpi_pci_init(void)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500384{
385 int ret;
386
Shaohua Lif8993af2007-04-25 11:05:12 +0800387 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");
389 pci_no_msi();
390 }
Muthu Kumar9c273b92006-04-28 00:42:21 -0700391 ret = register_acpi_bus_type(&acpi_pci_bus);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500392 if (ret)
393 return 0;
Len Brown673d5b42007-07-28 03:33:16 -0400394#ifdef CONFIG_ACPI_SLEEP
David Shaohua Li0f644742005-03-19 00:15:48 -0500395 platform_pci_choose_state = acpi_pci_choose_state;
Len Brown673d5b42007-07-28 03:33:16 -0400396#endif
David Shaohua Lib9131002005-03-19 00:16:18 -0500397 platform_pci_set_power_state = acpi_pci_set_power_state;
David Shaohua Li84df749f2005-03-18 18:53:36 -0500398 return 0;
399}
Muthu Kumar9c273b92006-04-28 00:42:21 -0700400arch_initcall(acpi_pci_init);