blob: b5ac810404c0f19dc98318dfe8dde6212d04c2bb [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)) {
58 printk(KERN_DEBUG
59 "Evaluate _OSC Set fails. Status = 0x%04x\n", status);
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -080060 *ret_status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return status;
62 }
Kristen Accardi593ee202006-05-20 15:00:08 -070063 out_obj = output.pointer;
64
65 if (out_obj->type != ACPI_TYPE_BUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 printk(KERN_DEBUG
67 "Evaluate _OSC returns wrong type\n");
Kristen Accardi593ee202006-05-20 15:00:08 -070068 status = AE_TYPE;
69 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
Kristen Accardi593ee202006-05-20 15:00:08 -070071 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (osc_dw0) {
73 if (osc_dw0 & OSC_REQUEST_ERROR)
74 printk(KERN_DEBUG "_OSC request fails\n");
75 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
76 printk(KERN_DEBUG "_OSC invalid UUID\n");
77 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
78 printk(KERN_DEBUG "_OSC invalid revision\n");
79 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
80 /* Update Global Control Set */
Kristen Accardi593ee202006-05-20 15:00:08 -070081 global_ctrlsets = *((u32 *)(out_obj->buffer.pointer+8));
82 status = AE_OK;
83 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Kristen Accardi593ee202006-05-20 15:00:08 -070085 status = AE_ERROR;
86 goto query_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88
89 /* Update Global Control Set */
Kristen Accardi593ee202006-05-20 15:00:08 -070090 global_ctrlsets = *((u32 *)(out_obj->buffer.pointer + 8));
91 status = AE_OK;
92
93query_osc_out:
94 kfree(output.pointer);
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -080095 *ret_status = status;
Kristen Accardi593ee202006-05-20 15:00:08 -070096 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99
100static acpi_status
101acpi_run_osc (
102 acpi_handle handle,
rajesh.shah@intel.com427bf532005-10-31 16:20:11 -0800103 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 acpi_status status;
106 struct acpi_object_list input;
107 union acpi_object in_params[4];
Kristen Accardi593ee202006-05-20 15:00:08 -0700108 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
109 union acpi_object *out_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 u32 osc_dw0;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* Setting up input parameters */
113 input.count = 4;
114 input.pointer = in_params;
115 in_params[0].type = ACPI_TYPE_BUFFER;
116 in_params[0].buffer.length = 16;
117 in_params[0].buffer.pointer = OSC_UUID;
118 in_params[1].type = ACPI_TYPE_INTEGER;
119 in_params[1].integer.value = 1;
120 in_params[2].type = ACPI_TYPE_INTEGER;
121 in_params[2].integer.value = 3;
122 in_params[3].type = ACPI_TYPE_BUFFER;
123 in_params[3].buffer.length = 12;
124 in_params[3].buffer.pointer = (u8 *)context;
125
126 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
127 if (ACPI_FAILURE (status)) {
128 printk(KERN_DEBUG
129 "Evaluate _OSC Set fails. Status = 0x%04x\n", status);
130 return status;
131 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700132 out_obj = output.pointer;
133 if (out_obj->type != ACPI_TYPE_BUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 printk(KERN_DEBUG
135 "Evaluate _OSC returns wrong type\n");
Kristen Accardi593ee202006-05-20 15:00:08 -0700136 status = AE_TYPE;
137 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700139 osc_dw0 = *((u32 *) out_obj->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (osc_dw0) {
141 if (osc_dw0 & OSC_REQUEST_ERROR)
142 printk(KERN_DEBUG "_OSC request fails\n");
143 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
144 printk(KERN_DEBUG "_OSC invalid UUID\n");
145 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
146 printk(KERN_DEBUG "_OSC invalid revision\n");
147 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
148 printk(KERN_DEBUG "_OSC FW not grant req. control\n");
Kristen Accardi593ee202006-05-20 15:00:08 -0700149 status = AE_SUPPORT;
150 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700152 status = AE_ERROR;
153 goto run_osc_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Kristen Accardi593ee202006-05-20 15:00:08 -0700155 status = AE_OK;
156
157run_osc_out:
158 kfree(output.pointer);
159 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162/**
163 * pci_osc_support_set - register OS support to Firmware
164 * @flags: OS support bits
165 *
166 * Update OS support fields and doing a _OSC Query to obtain an update
167 * from Firmware on supported control bits.
168 **/
169acpi_status pci_osc_support_set(u32 flags)
170{
171 u32 temp;
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -0800172 acpi_status retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (!(flags & OSC_SUPPORT_MASKS)) {
175 return AE_TYPE;
176 }
177 ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
178
179 /* do _OSC query for all possible controls */
180 temp = ctrlset_buf[OSC_CONTROL_TYPE];
181 ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
182 ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
183 acpi_get_devices ( PCI_ROOT_HID_STRING,
184 acpi_query_osc,
185 ctrlset_buf,
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -0800186 (void **) &retval );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
188 ctrlset_buf[OSC_CONTROL_TYPE] = temp;
Kristen Carlson Accardi0dcb2b72006-10-30 13:08:12 -0800189 if (ACPI_FAILURE(retval)) {
190 /* no osc support at all */
191 ctrlset_buf[OSC_SUPPORT_TYPE] = 0;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return AE_OK;
194}
195EXPORT_SYMBOL(pci_osc_support_set);
196
197/**
198 * pci_osc_control_set - commit requested control to Firmware
Randy Dunlapf3666332005-11-23 15:45:04 -0800199 * @handle: acpi_handle for the target ACPI object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * @flags: driver's requested control bits
201 *
202 * Attempt to take control from Firmware on requested control bits.
203 **/
rajesh.shah@intel.com427bf532005-10-31 16:20:11 -0800204acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 acpi_status status;
207 u32 ctrlset;
208
209 ctrlset = (flags & OSC_CONTROL_MASKS);
210 if (!ctrlset) {
211 return AE_TYPE;
212 }
213 if (ctrlset_buf[OSC_SUPPORT_TYPE] &&
214 ((global_ctrlsets & ctrlset) != ctrlset)) {
215 return AE_SUPPORT;
216 }
217 ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
rajesh.shah@intel.com427bf532005-10-31 16:20:11 -0800218 status = acpi_run_osc(handle, ctrlset_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (ACPI_FAILURE (status)) {
220 ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
221 }
222
223 return status;
224}
225EXPORT_SYMBOL(pci_osc_control_set);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500226
David Shaohua Li0f644742005-03-19 00:15:48 -0500227/*
228 * _SxD returns the D-state with the highest power
229 * (lowest D-state number) supported in the S-state "x".
230 *
231 * If the devices does not have a _PRW
232 * (Power Resources for Wake) supporting system wakeup from "x"
233 * then the OS is free to choose a lower power (higher number
234 * D-state) than the return value from _SxD.
235 *
236 * But if _PRW is enabled at S-state "x", the OS
237 * must not choose a power lower than _SxD --
238 * unless the device has an _SxW method specifying
239 * the lowest power (highest D-state number) the device
240 * may enter while still able to wake the system.
241 *
242 * ie. depending on global OS policy:
243 *
244 * if (_PRW at S-state x)
245 * choose from highest power _SxD to lowest power _SxW
246 * else // no _PRW at S-state x
247 * choose highest power _SxD or any lower power
248 *
249 * currently we simply return _SxD, if present.
250 */
251
252static int acpi_pci_choose_state(struct pci_dev *pdev, pm_message_t state)
253{
Len Browna406d9e2005-03-23 16:16:03 -0500254 /* TBD */
David Shaohua Li0f644742005-03-19 00:15:48 -0500255
David Shaohua Li0f644742005-03-19 00:15:48 -0500256 return -ENODEV;
257}
258
David Shaohua Lib9131002005-03-19 00:16:18 -0500259static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
260{
261 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
262 static int state_conv[] = {
263 [0] = 0,
264 [1] = 1,
265 [2] = 2,
266 [3] = 3,
267 [4] = 3
268 };
269 int acpi_state = state_conv[(int __force) state];
270
271 if (!handle)
272 return -ENODEV;
273 return acpi_bus_set_power(handle, acpi_state);
274}
275
276
David Shaohua Li84df749f2005-03-18 18:53:36 -0500277/* ACPI bus type */
Muthu Kumar9c273b92006-04-28 00:42:21 -0700278static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500279{
280 struct pci_dev * pci_dev;
281 acpi_integer addr;
282
283 pci_dev = to_pci_dev(dev);
284 /* Please ref to ACPI spec for the syntax of _ADR */
285 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
286 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
287 if (!*handle)
288 return -ENODEV;
289 return 0;
290}
291
Muthu Kumar9c273b92006-04-28 00:42:21 -0700292static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500293{
294 int num;
295 unsigned int seg, bus;
296
297 /*
298 * The string should be the same as root bridge's name
299 * Please look at 'pci_scan_bus_parented'
300 */
301 num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
302 if (num != 2)
303 return -ENODEV;
304 *handle = acpi_get_pci_rootbridge_handle(seg, bus);
305 if (!*handle)
306 return -ENODEV;
307 return 0;
308}
309
Muthu Kumar9c273b92006-04-28 00:42:21 -0700310static struct acpi_bus_type acpi_pci_bus = {
David Shaohua Li84df749f2005-03-18 18:53:36 -0500311 .bus = &pci_bus_type,
Muthu Kumar9c273b92006-04-28 00:42:21 -0700312 .find_device = acpi_pci_find_device,
313 .find_bridge = acpi_pci_find_root_bridge,
David Shaohua Li84df749f2005-03-18 18:53:36 -0500314};
315
Muthu Kumar9c273b92006-04-28 00:42:21 -0700316static int __init acpi_pci_init(void)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500317{
318 int ret;
319
Shaohua Lif8993af2007-04-25 11:05:12 +0800320 if (acpi_gbl_FADT.boot_flags & BAF_MSI_NOT_SUPPORTED) {
321 printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
322 pci_no_msi();
323 }
Muthu Kumar9c273b92006-04-28 00:42:21 -0700324 ret = register_acpi_bus_type(&acpi_pci_bus);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500325 if (ret)
326 return 0;
David Shaohua Li0f644742005-03-19 00:15:48 -0500327 platform_pci_choose_state = acpi_pci_choose_state;
David Shaohua Lib9131002005-03-19 00:16:18 -0500328 platform_pci_set_power_state = acpi_pci_set_power_state;
David Shaohua Li84df749f2005-03-18 18:53:36 -0500329 return 0;
330}
Muthu Kumar9c273b92006-04-28 00:42:21 -0700331arch_initcall(acpi_pci_init);