]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/pci/pci-acpi.c
pci-acpi: use local buffer for _OSC
[linux-2.6-omap-h63xx.git] / drivers / pci / pci-acpi.c
1 /*
2  * File:        pci-acpi.c
3  * Purpose:     Provide PCI support in ACPI
4  *
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.
8  */
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>
20 #include "pci.h"
21
22 struct acpi_osc_data {
23         acpi_handle handle;
24         u32 support_set;
25         u32 control_set;
26         u32 query_result;
27         struct list_head sibiling;
28 };
29 static LIST_HEAD(acpi_osc_data_list);
30
31 struct acpi_osc_args {
32         u32 capbuf[3];
33         u32 query_result;
34 };
35
36 static struct acpi_osc_data *acpi_get_osc_data(acpi_handle handle)
37 {
38         struct acpi_osc_data *data;
39
40         list_for_each_entry(data, &acpi_osc_data_list, sibiling) {
41                 if (data->handle == handle)
42                         return data;
43         }
44         data = kzalloc(sizeof(*data), GFP_KERNEL);
45         if (!data)
46                 return NULL;
47         INIT_LIST_HEAD(&data->sibiling);
48         data->handle = handle;
49         list_add_tail(&data->sibiling, &acpi_osc_data_list);
50         return data;
51 }
52
53 static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
54
55 static acpi_status acpi_run_osc(acpi_handle handle,
56                                 struct acpi_osc_args *osc_args)
57 {
58         acpi_status status;
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];
64
65         /* Setting up input parameters */
66         input.count = 4;
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;
78
79         status = acpi_evaluate_object(handle, "_OSC", &input, &output);
80         if (ACPI_FAILURE(status))
81                 return status;
82
83         out_obj = output.pointer;
84         if (out_obj->type != ACPI_TYPE_BUFFER) {
85                 printk(KERN_DEBUG "Evaluate _OSC returns wrong type\n");
86                 status = AE_TYPE;
87                 goto out_kfree;
88         }
89         osc_dw0 = *((u32 *) out_obj->buffer.pointer);
90         if (osc_dw0) {
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)
99                                 goto out_success;
100                         printk(KERN_DEBUG "_OSC FW not grant req. control\n");
101                         status = AE_SUPPORT;
102                         goto out_kfree;
103                 }
104                 status = AE_ERROR;
105                 goto out_kfree;
106         }
107 out_success:
108         if (flags & OSC_QUERY_ENABLE)
109                 osc_args->query_result =
110                         *((u32 *)(out_obj->buffer.pointer + 8));
111         status = AE_OK;
112
113 out_kfree:
114         kfree(output.pointer);
115         return status;
116 }
117
118 static acpi_status acpi_query_osc(acpi_handle handle,
119                                   u32 level, void *context, void **retval)
120 {
121         acpi_status status;
122         acpi_status *ret_status = (acpi_status *)retval;
123         struct acpi_osc_data *osc_data;
124         u32 flags = (unsigned long)context, support_set;
125         acpi_handle tmp;
126         struct acpi_osc_args osc_args;
127
128         status = acpi_get_handle(handle, "_OSC", &tmp);
129         if (ACPI_FAILURE(status))
130                 return status;
131
132         osc_data = acpi_get_osc_data(handle);
133         if (!osc_data) {
134                 printk(KERN_ERR "acpi osc data array is full\n");
135                 return AE_ERROR;
136         }
137
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;
143
144         status = acpi_run_osc(handle, &osc_args);
145         *ret_status = status;
146
147         if (ACPI_SUCCESS(status)) {
148                 osc_data->support_set = support_set;
149                 osc_data->query_result = osc_args.query_result;
150         }
151
152         return status;
153 }
154
155 /**
156  * __pci_osc_support_set - register OS support to Firmware
157  * @flags: OS support bits
158  * @hid: hardware ID
159  *
160  * Update OS support fields and doing a _OSC Query to obtain an update
161  * from Firmware on supported control bits.
162  **/
163 acpi_status __pci_osc_support_set(u32 flags, const char *hid)
164 {
165         acpi_status retval = AE_NOT_FOUND;
166
167         if (!(flags & OSC_SUPPORT_MASKS)) {
168                 return AE_TYPE;
169         }
170         acpi_get_devices(hid,
171                         acpi_query_osc,
172                         (void *)(unsigned long)flags,
173                         (void **) &retval );
174         return AE_OK;
175 }
176
177 /**
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
181  *
182  * Attempt to take control from Firmware on requested control bits.
183  **/
184 acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
185 {
186         acpi_status status;
187         u32 ctrlset, control_set;
188         acpi_handle tmp;
189         struct acpi_osc_data *osc_data;
190         struct acpi_osc_args osc_args;
191
192         status = acpi_get_handle(handle, "_OSC", &tmp);
193         if (ACPI_FAILURE(status))
194                 return status;
195
196         osc_data = acpi_get_osc_data(handle);
197         if (!osc_data) {
198                 printk(KERN_ERR "acpi osc data array is full\n");
199                 return AE_ERROR;
200         }
201
202         ctrlset = (flags & OSC_CONTROL_MASKS);
203         if (!ctrlset)
204                 return AE_TYPE;
205
206         if (osc_data->support_set &&
207             ((osc_data->query_result & ctrlset) != ctrlset))
208                 return AE_SUPPORT;
209
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;
217
218         return status;
219 }
220 EXPORT_SYMBOL(pci_osc_control_set);
221
222 #ifdef CONFIG_ACPI_SLEEP
223 /*
224  * _SxD returns the D-state with the highest power
225  * (lowest D-state number) supported in the S-state "x".
226  *
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.
231  *
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.
237  *
238  * ie. depending on global OS policy:
239  *
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
244  */
245
246 static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev,
247         pm_message_t state)
248 {
249         int acpi_state;
250
251         acpi_state = acpi_pm_device_sleep_state(&pdev->dev,
252                 device_may_wakeup(&pdev->dev), NULL);
253         if (acpi_state < 0)
254                 return PCI_POWER_ERROR;
255
256         switch (acpi_state) {
257         case ACPI_STATE_D0:
258                 return PCI_D0;
259         case ACPI_STATE_D1:
260                 return PCI_D1;
261         case ACPI_STATE_D2:
262                 return PCI_D2;
263         case ACPI_STATE_D3:
264                 return PCI_D3hot;
265         }
266         return PCI_POWER_ERROR;
267 }
268 #endif
269
270 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
271 {
272         acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
273         acpi_handle tmp;
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
280         };
281
282         if (!handle)
283                 return -ENODEV;
284         /* If the ACPI device has _EJ0, ignore the device */
285         if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
286                 return 0;
287
288         switch (state) {
289         case PCI_D0:
290         case PCI_D1:
291         case PCI_D2:
292         case PCI_D3hot:
293         case PCI_D3cold:
294                 return acpi_bus_set_power(handle, state_conv[state]);
295         }
296         return -EINVAL;
297 }
298
299
300 /* ACPI bus type */
301 static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
302 {
303         struct pci_dev * pci_dev;
304         acpi_integer    addr;
305
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);
310         if (!*handle)
311                 return -ENODEV;
312         return 0;
313 }
314
315 static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
316 {
317         int num;
318         unsigned int seg, bus;
319
320         /*
321          * The string should be the same as root bridge's name
322          * Please look at 'pci_scan_bus_parented'
323          */
324         num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
325         if (num != 2)
326                 return -ENODEV;
327         *handle = acpi_get_pci_rootbridge_handle(seg, bus);
328         if (!*handle)
329                 return -ENODEV;
330         return 0;
331 }
332
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,
337 };
338
339 static int __init acpi_pci_init(void)
340 {
341         int ret;
342
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");
345                 pci_no_msi();
346         }
347         ret = register_acpi_bus_type(&acpi_pci_bus);
348         if (ret)
349                 return 0;
350 #ifdef  CONFIG_ACPI_SLEEP
351         platform_pci_choose_state = acpi_pci_choose_state;
352 #endif
353         platform_pci_set_power_state = acpi_pci_set_power_state;
354         return 0;
355 }
356 arch_initcall(acpi_pci_init);