blob: 5c6a5d043007ebec1418fe2d9a70cf5071cc06eb [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
22static u32 ctrlset_buf[3] = {0, 0, 0};
23static u32 global_ctrlsets = 0;
Greg KHbc56b9e2005-04-08 14:53:31 +090024static 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 -070025
26static acpi_status
27acpi_query_osc (
28 acpi_handle handle,
29 u32 level,
30 void *context,
31 void **retval )
32{
33 acpi_status status;
34 struct acpi_object_list input;
35 union acpi_object in_params[4];
Kristen Accardi593ee202006-05-20 15:00:08 -070036 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
37 union acpi_object *out_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 u32 osc_dw0;
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -080039 acpi_status *ret_status = (acpi_status *)retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 /* Setting up input parameters */
43 input.count = 4;
44 input.pointer = in_params;
45 in_params[0].type = ACPI_TYPE_BUFFER;
46 in_params[0].buffer.length = 16;
47 in_params[0].buffer.pointer = OSC_UUID;
48 in_params[1].type = ACPI_TYPE_INTEGER;
49 in_params[1].integer.value = 1;
50 in_params[2].type = ACPI_TYPE_INTEGER;
51 in_params[2].integer.value = 3;
52 in_params[3].type = ACPI_TYPE_BUFFER;
53 in_params[3].buffer.length = 12;
54 in_params[3].buffer.pointer = (u8 *)context;
55
56 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
57 if (ACPI_FAILURE (status)) {
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -080058 *ret_status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return status;
60 }
Kristen Accardi593ee202006-05-20 15:00:08 -070061 out_obj = output.pointer;
62
63 if (out_obj->type != ACPI_TYPE_BUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 printk(KERN_DEBUG
65 "Evaluate _OSC returns wrong type\n");
Kristen Accardi593ee202006-05-20 15:00:08 -070066 status = AE_TYPE;
67 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Kristen Accardi593ee202006-05-20 15:00:08 -070069 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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 */
Kristen Accardi593ee202006-05-20 15:00:08 -070079 global_ctrlsets = *((u32 *)(out_obj->buffer.pointer+8));
80 status = AE_OK;
81 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Kristen Accardi593ee202006-05-20 15:00:08 -070083 status = AE_ERROR;
84 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87 /* Update Global Control Set */
Kristen Accardi593ee202006-05-20 15:00:08 -070088 global_ctrlsets = *((u32 *)(out_obj->buffer.pointer + 8));
89 status = AE_OK;
90
91query_osc_out:
92 kfree(output.pointer);
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -080093 *ret_status = status;
Kristen Accardi593ee202006-05-20 15:00:08 -070094 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97
98static acpi_status
99acpi_run_osc (
100 acpi_handle handle,
rajesh.shah@intel.com427bf532005-10-31 16:20:11 -0800101 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 acpi_status status;
104 struct acpi_object_list input;
105 union acpi_object in_params[4];
Kristen Accardi593ee202006-05-20 15:00:08 -0700106 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
107 union acpi_object *out_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 u32 osc_dw0;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* Setting up input parameters */
111 input.count = 4;
112 input.pointer = in_params;
113 in_params[0].type = ACPI_TYPE_BUFFER;
114 in_params[0].buffer.length = 16;
115 in_params[0].buffer.pointer = OSC_UUID;
116 in_params[1].type = ACPI_TYPE_INTEGER;
117 in_params[1].integer.value = 1;
118 in_params[2].type = ACPI_TYPE_INTEGER;
119 in_params[2].integer.value = 3;
120 in_params[3].type = ACPI_TYPE_BUFFER;
121 in_params[3].buffer.length = 12;
122 in_params[3].buffer.pointer = (u8 *)context;
123
124 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
Zhang, Yanmin8d29bfb2007-06-06 11:44:16 +0800125 if (ACPI_FAILURE (status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return status;
Zhang, Yanmin8d29bfb2007-06-06 11:44:16 +0800127
Kristen Accardi593ee202006-05-20 15:00:08 -0700128 out_obj = output.pointer;
129 if (out_obj->type != ACPI_TYPE_BUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 printk(KERN_DEBUG
131 "Evaluate _OSC returns wrong type\n");
Kristen Accardi593ee202006-05-20 15:00:08 -0700132 status = AE_TYPE;
133 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700135 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (osc_dw0) {
137 if (osc_dw0 & OSC_REQUEST_ERROR)
138 printk(KERN_DEBUG "_OSC request fails\n");
139 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
140 printk(KERN_DEBUG "_OSC invalid UUID\n");
141 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
142 printk(KERN_DEBUG "_OSC invalid revision\n");
143 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
144 printk(KERN_DEBUG "_OSC FW not grant req. control\n");
Kristen Accardi593ee202006-05-20 15:00:08 -0700145 status = AE_SUPPORT;
146 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700148 status = AE_ERROR;
149 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700151 status = AE_OK;
152
153run_osc_out:
154 kfree(output.pointer);
155 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/**
159 * pci_osc_support_set - register OS support to Firmware
160 * @flags: OS support bits
161 *
162 * Update OS support fields and doing a _OSC Query to obtain an update
163 * from Firmware on supported control bits.
164 **/
165acpi_status pci_osc_support_set(u32 flags)
166{
167 u32 temp;
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -0800168 acpi_status retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 if (!(flags & OSC_SUPPORT_MASKS)) {
171 return AE_TYPE;
172 }
173 ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
174
175 /* do _OSC query for all possible controls */
176 temp = ctrlset_buf[OSC_CONTROL_TYPE];
177 ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
178 ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
179 acpi_get_devices ( PCI_ROOT_HID_STRING,
180 acpi_query_osc,
181 ctrlset_buf,
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -0800182 (void **) &retval );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
184 ctrlset_buf[OSC_CONTROL_TYPE] = temp;
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -0800185 if (ACPI_FAILURE(retval)) {
186 /* no osc support at all */
187 ctrlset_buf[OSC_SUPPORT_TYPE] = 0;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return AE_OK;
190}
191EXPORT_SYMBOL(pci_osc_support_set);
192
193/**
194 * pci_osc_control_set - commit requested control to Firmware
Randy Dunlapf3666332005-11-23 15:45:04 -0800195 * @handle: acpi_handle for the target ACPI object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * @flags: driver's requested control bits
197 *
198 * Attempt to take control from Firmware on requested control bits.
199 **/
rajesh.shah@intel.com427bf532005-10-31 16:20:11 -0800200acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 acpi_status status;
203 u32 ctrlset;
204
205 ctrlset = (flags & OSC_CONTROL_MASKS);
206 if (!ctrlset) {
207 return AE_TYPE;
208 }
209 if (ctrlset_buf[OSC_SUPPORT_TYPE] &&
210 ((global_ctrlsets & ctrlset) != ctrlset)) {
211 return AE_SUPPORT;
212 }
213 ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
rajesh.shah@intel.com427bf532005-10-31 16:20:11 -0800214 status = acpi_run_osc(handle, ctrlset_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (ACPI_FAILURE (status)) {
216 ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
217 }
218
219 return status;
220}
221EXPORT_SYMBOL(pci_osc_control_set);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500222
Len Brown673d5b42007-07-28 03:33:16 -0400223#ifdef CONFIG_ACPI_SLEEP
David Shaohua Li0f644742005-03-19 00:15:48 -0500224/*
225 * _SxD returns the D-state with the highest power
226 * (lowest D-state number) supported in the S-state "x".
227 *
228 * If the devices does not have a _PRW
229 * (Power Resources for Wake) supporting system wakeup from "x"
230 * then the OS is free to choose a lower power (higher number
231 * D-state) than the return value from _SxD.
232 *
233 * But if _PRW is enabled at S-state "x", the OS
234 * must not choose a power lower than _SxD --
235 * unless the device has an _SxW method specifying
236 * the lowest power (highest D-state number) the device
237 * may enter while still able to wake the system.
238 *
239 * ie. depending on global OS policy:
240 *
241 * if (_PRW at S-state x)
242 * choose from highest power _SxD to lowest power _SxW
243 * else // no _PRW at S-state x
244 * choose highest power _SxD or any lower power
245 *
246 * currently we simply return _SxD, if present.
247 */
248
Shaohua Liab826ca2007-07-20 10:03:22 +0800249static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev,
250 pm_message_t state)
David Shaohua Li0f644742005-03-19 00:15:48 -0500251{
Shaohua Liab826ca2007-07-20 10:03:22 +0800252 int acpi_state;
David Shaohua Li0f644742005-03-19 00:15:48 -0500253
Shaohua Liab826ca2007-07-20 10:03:22 +0800254 acpi_state = acpi_pm_device_sleep_state(&pdev->dev,
255 device_may_wakeup(&pdev->dev), NULL);
256 if (acpi_state < 0)
257 return PCI_POWER_ERROR;
258
259 switch (acpi_state) {
260 case ACPI_STATE_D0:
261 return PCI_D0;
262 case ACPI_STATE_D1:
263 return PCI_D1;
264 case ACPI_STATE_D2:
265 return PCI_D2;
266 case ACPI_STATE_D3:
267 return PCI_D3hot;
268 }
269 return PCI_POWER_ERROR;
David Shaohua Li0f644742005-03-19 00:15:48 -0500270}
Len Brown673d5b42007-07-28 03:33:16 -0400271#endif
David Shaohua Li0f644742005-03-19 00:15:48 -0500272
David Shaohua Lib9131002005-03-19 00:16:18 -0500273static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
274{
275 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
Shaohua Li10b3dca2007-07-20 10:03:25 +0800276 acpi_handle tmp;
David Shaohua Lib9131002005-03-19 00:16:18 -0500277 static int state_conv[] = {
278 [0] = 0,
279 [1] = 1,
280 [2] = 2,
281 [3] = 3,
282 [4] = 3
283 };
284 int acpi_state = state_conv[(int __force) state];
285
286 if (!handle)
287 return -ENODEV;
Shaohua Li10b3dca2007-07-20 10:03:25 +0800288 /* If the ACPI device has _EJ0, ignore the device */
289 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
290 return 0;
David Shaohua Lib9131002005-03-19 00:16:18 -0500291 return acpi_bus_set_power(handle, acpi_state);
292}
293
294
David Shaohua Li84df749f2005-03-18 18:53:36 -0500295/* ACPI bus type */
Muthu Kumar9c273b92006-04-28 00:42:21 -0700296static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500297{
298 struct pci_dev * pci_dev;
299 acpi_integer addr;
300
301 pci_dev = to_pci_dev(dev);
302 /* Please ref to ACPI spec for the syntax of _ADR */
303 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
304 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
305 if (!*handle)
306 return -ENODEV;
307 return 0;
308}
309
Muthu Kumar9c273b92006-04-28 00:42:21 -0700310static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500311{
312 int num;
313 unsigned int seg, bus;
314
315 /*
316 * The string should be the same as root bridge's name
317 * Please look at 'pci_scan_bus_parented'
318 */
319 num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
320 if (num != 2)
321 return -ENODEV;
322 *handle = acpi_get_pci_rootbridge_handle(seg, bus);
323 if (!*handle)
324 return -ENODEV;
325 return 0;
326}
327
Muthu Kumar9c273b92006-04-28 00:42:21 -0700328static struct acpi_bus_type acpi_pci_bus = {
David Shaohua Li84df749f2005-03-18 18:53:36 -0500329 .bus = &pci_bus_type,
Muthu Kumar9c273b92006-04-28 00:42:21 -0700330 .find_device = acpi_pci_find_device,
331 .find_bridge = acpi_pci_find_root_bridge,
David Shaohua Li84df749f2005-03-18 18:53:36 -0500332};
333
Muthu Kumar9c273b92006-04-28 00:42:21 -0700334static int __init acpi_pci_init(void)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500335{
336 int ret;
337
Shaohua Lif8993af2007-04-25 11:05:12 +0800338 if (acpi_gbl_FADT.boot_flags & BAF_MSI_NOT_SUPPORTED) {
339 printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
340 pci_no_msi();
341 }
Muthu Kumar9c273b92006-04-28 00:42:21 -0700342 ret = register_acpi_bus_type(&acpi_pci_bus);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500343 if (ret)
344 return 0;
Len Brown673d5b42007-07-28 03:33:16 -0400345#ifdef CONFIG_ACPI_SLEEP
David Shaohua Li0f644742005-03-19 00:15:48 -0500346 platform_pci_choose_state = acpi_pci_choose_state;
Len Brown673d5b42007-07-28 03:33:16 -0400347#endif
David Shaohua Lib9131002005-03-19 00:16:18 -0500348 platform_pci_set_power_state = acpi_pci_set_power_state;
David Shaohua Li84df749f2005-03-18 18:53:36 -0500349 return 0;
350}
Muthu Kumar9c273b92006-04-28 00:42:21 -0700351arch_initcall(acpi_pci_init);