9d976d51d406ddc5a55d414da87fd382c7b504ec
[safe/jmp/linux-2.6] / 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 <linux/pci-aspm.h>
15 #include <acpi/acpi.h>
16 #include <acpi/acpi_bus.h>
17
18 #include <linux/pci-acpi.h>
19 #include "pci.h"
20
21 struct acpi_osc_data {
22         acpi_handle handle;
23         u32 support_set;
24         u32 control_set;
25         struct list_head sibiling;
26 };
27 static LIST_HEAD(acpi_osc_data_list);
28
29 struct acpi_osc_args {
30         u32 capbuf[3];
31         u32 ctrl_result;
32 };
33
34 static DEFINE_MUTEX(pci_acpi_lock);
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,
54                           0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
55
56 static acpi_status acpi_run_osc(acpi_handle handle,
57                                 struct acpi_osc_args *osc_args)
58 {
59         acpi_status status;
60         struct acpi_object_list input;
61         union acpi_object in_params[4];
62         struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
63         union acpi_object *out_obj;
64         u32 errors, flags = osc_args->capbuf[OSC_QUERY_TYPE];
65
66         /* Setting up input parameters */
67         input.count = 4;
68         input.pointer = in_params;
69         in_params[0].type               = ACPI_TYPE_BUFFER;
70         in_params[0].buffer.length      = 16;
71         in_params[0].buffer.pointer     = OSC_UUID;
72         in_params[1].type               = ACPI_TYPE_INTEGER;
73         in_params[1].integer.value      = 1;
74         in_params[2].type               = ACPI_TYPE_INTEGER;
75         in_params[2].integer.value      = 3;
76         in_params[3].type               = ACPI_TYPE_BUFFER;
77         in_params[3].buffer.length      = 12;
78         in_params[3].buffer.pointer     = (u8 *)osc_args->capbuf;
79
80         status = acpi_evaluate_object(handle, "_OSC", &input, &output);
81         if (ACPI_FAILURE(status))
82                 return status;
83
84         if (!output.length)
85                 return AE_NULL_OBJECT;
86
87         out_obj = output.pointer;
88         if (out_obj->type != ACPI_TYPE_BUFFER) {
89                 printk(KERN_DEBUG "Evaluate _OSC returns wrong type\n");
90                 status = AE_TYPE;
91                 goto out_kfree;
92         }
93         /* Need to ignore the bit0 in result code */
94         errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
95         if (errors) {
96                 if (errors & OSC_REQUEST_ERROR)
97                         printk(KERN_DEBUG "_OSC request fails\n"); 
98                 if (errors & OSC_INVALID_UUID_ERROR)
99                         printk(KERN_DEBUG "_OSC invalid UUID\n"); 
100                 if (errors & OSC_INVALID_REVISION_ERROR)
101                         printk(KERN_DEBUG "_OSC invalid revision\n"); 
102                 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
103                         if (flags & OSC_QUERY_ENABLE)
104                                 goto out_success;
105                         printk(KERN_DEBUG "_OSC FW not grant req. control\n");
106                         status = AE_SUPPORT;
107                         goto out_kfree;
108                 }
109                 status = AE_ERROR;
110                 goto out_kfree;
111         }
112 out_success:
113         osc_args->ctrl_result =
114                 *((u32 *)(out_obj->buffer.pointer + 8));
115         status = AE_OK;
116
117 out_kfree:
118         kfree(output.pointer);
119         return status;
120 }
121
122 static acpi_status __acpi_query_osc(u32 flags, struct acpi_osc_data *osc_data,
123                                     u32 *result)
124 {
125         acpi_status status;
126         u32 support_set;
127         struct acpi_osc_args osc_args;
128
129         /* do _OSC query for all possible controls */
130         support_set = osc_data->support_set | (flags & OSC_SUPPORT_MASKS);
131         osc_args.capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
132         osc_args.capbuf[OSC_SUPPORT_TYPE] = support_set;
133         osc_args.capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
134
135         status = acpi_run_osc(osc_data->handle, &osc_args);
136         if (ACPI_SUCCESS(status)) {
137                 osc_data->support_set = support_set;
138                 *result = osc_args.ctrl_result;
139         }
140
141         return status;
142 }
143
144 static acpi_status acpi_query_osc(acpi_handle handle,
145                                   u32 level, void *context, void **retval)
146 {
147         acpi_status status;
148         struct acpi_osc_data *osc_data;
149         u32 flags = (unsigned long)context, dummy;
150         acpi_handle tmp;
151
152         status = acpi_get_handle(handle, "_OSC", &tmp);
153         if (ACPI_FAILURE(status))
154                 return AE_OK;
155
156         mutex_lock(&pci_acpi_lock);
157         osc_data = acpi_get_osc_data(handle);
158         if (!osc_data) {
159                 printk(KERN_ERR "acpi osc data array is full\n");
160                 goto out;
161         }
162
163         __acpi_query_osc(flags, osc_data, &dummy);
164 out:
165         mutex_unlock(&pci_acpi_lock);
166         return AE_OK;
167 }
168
169 /**
170  * __pci_osc_support_set - register OS support to Firmware
171  * @flags: OS support bits
172  * @hid: hardware ID
173  *
174  * Update OS support fields and doing a _OSC Query to obtain an update
175  * from Firmware on supported control bits.
176  **/
177 acpi_status __pci_osc_support_set(u32 flags, const char *hid)
178 {
179         if (!(flags & OSC_SUPPORT_MASKS))
180                 return AE_TYPE;
181
182         acpi_get_devices(hid, acpi_query_osc,
183                          (void *)(unsigned long)flags, NULL);
184         return AE_OK;
185 }
186
187 /**
188  * pci_osc_control_set - commit requested control to Firmware
189  * @handle: acpi_handle for the target ACPI object
190  * @flags: driver's requested control bits
191  *
192  * Attempt to take control from Firmware on requested control bits.
193  **/
194 acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
195 {
196         acpi_status status;
197         u32 ctrlset, control_set, result;
198         acpi_handle tmp;
199         struct acpi_osc_data *osc_data;
200         struct acpi_osc_args osc_args;
201
202         status = acpi_get_handle(handle, "_OSC", &tmp);
203         if (ACPI_FAILURE(status))
204                 return status;
205
206         mutex_lock(&pci_acpi_lock);
207         osc_data = acpi_get_osc_data(handle);
208         if (!osc_data) {
209                 printk(KERN_ERR "acpi osc data array is full\n");
210                 status = AE_ERROR;
211                 goto out;
212         }
213
214         ctrlset = (flags & OSC_CONTROL_MASKS);
215         if (!ctrlset) {
216                 status = AE_TYPE;
217                 goto out;
218         }
219
220         status = __acpi_query_osc(osc_data->support_set, osc_data, &result);
221         if (ACPI_FAILURE(status))
222                 goto out;
223
224         if ((result & ctrlset) != ctrlset) {
225                 status = AE_SUPPORT;
226                 goto out;
227         }
228
229         control_set = osc_data->control_set | ctrlset;
230         osc_args.capbuf[OSC_QUERY_TYPE] = 0;
231         osc_args.capbuf[OSC_SUPPORT_TYPE] = osc_data->support_set;
232         osc_args.capbuf[OSC_CONTROL_TYPE] = control_set;
233         status = acpi_run_osc(handle, &osc_args);
234         if (ACPI_SUCCESS(status))
235                 osc_data->control_set = control_set;
236 out:
237         mutex_unlock(&pci_acpi_lock);
238         return status;
239 }
240 EXPORT_SYMBOL(pci_osc_control_set);
241
242 /*
243  * _SxD returns the D-state with the highest power
244  * (lowest D-state number) supported in the S-state "x".
245  *
246  * If the devices does not have a _PRW
247  * (Power Resources for Wake) supporting system wakeup from "x"
248  * then the OS is free to choose a lower power (higher number
249  * D-state) than the return value from _SxD.
250  *
251  * But if _PRW is enabled at S-state "x", the OS
252  * must not choose a power lower than _SxD --
253  * unless the device has an _SxW method specifying
254  * the lowest power (highest D-state number) the device
255  * may enter while still able to wake the system.
256  *
257  * ie. depending on global OS policy:
258  *
259  * if (_PRW at S-state x)
260  *      choose from highest power _SxD to lowest power _SxW
261  * else // no _PRW at S-state x
262  *      choose highest power _SxD or any lower power
263  */
264
265 static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
266 {
267         int acpi_state;
268
269         acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL);
270         if (acpi_state < 0)
271                 return PCI_POWER_ERROR;
272
273         switch (acpi_state) {
274         case ACPI_STATE_D0:
275                 return PCI_D0;
276         case ACPI_STATE_D1:
277                 return PCI_D1;
278         case ACPI_STATE_D2:
279                 return PCI_D2;
280         case ACPI_STATE_D3:
281                 return PCI_D3hot;
282         }
283         return PCI_POWER_ERROR;
284 }
285
286 static bool acpi_pci_power_manageable(struct pci_dev *dev)
287 {
288         acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
289
290         return handle ? acpi_bus_power_manageable(handle) : false;
291 }
292
293 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
294 {
295         acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
296         acpi_handle tmp;
297         static const u8 state_conv[] = {
298                 [PCI_D0] = ACPI_STATE_D0,
299                 [PCI_D1] = ACPI_STATE_D1,
300                 [PCI_D2] = ACPI_STATE_D2,
301                 [PCI_D3hot] = ACPI_STATE_D3,
302                 [PCI_D3cold] = ACPI_STATE_D3
303         };
304         int error = -EINVAL;
305
306         /* If the ACPI device has _EJ0, ignore the device */
307         if (!handle || ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
308                 return -ENODEV;
309
310         switch (state) {
311         case PCI_D0:
312         case PCI_D1:
313         case PCI_D2:
314         case PCI_D3hot:
315         case PCI_D3cold:
316                 error = acpi_bus_set_power(handle, state_conv[state]);
317         }
318
319         if (!error)
320                 dev_printk(KERN_INFO, &dev->dev,
321                                 "power state changed by ACPI to D%d\n", state);
322
323         return error;
324 }
325
326 static bool acpi_pci_can_wakeup(struct pci_dev *dev)
327 {
328         acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
329
330         return handle ? acpi_bus_can_wakeup(handle) : false;
331 }
332
333 static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
334 {
335         int error = acpi_pm_device_sleep_wake(&dev->dev, enable);
336
337         if (!error)
338                 dev_printk(KERN_INFO, &dev->dev,
339                                 "wake-up capability %s by ACPI\n",
340                                 enable ? "enabled" : "disabled");
341         return error;
342 }
343
344 static struct pci_platform_pm_ops acpi_pci_platform_pm = {
345         .is_manageable = acpi_pci_power_manageable,
346         .set_state = acpi_pci_set_power_state,
347         .choose_state = acpi_pci_choose_state,
348         .can_wakeup = acpi_pci_can_wakeup,
349         .sleep_wake = acpi_pci_sleep_wake,
350 };
351
352 /* ACPI bus type */
353 static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
354 {
355         struct pci_dev * pci_dev;
356         acpi_integer    addr;
357
358         pci_dev = to_pci_dev(dev);
359         /* Please ref to ACPI spec for the syntax of _ADR */
360         addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
361         *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
362         if (!*handle)
363                 return -ENODEV;
364         return 0;
365 }
366
367 static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
368 {
369         int num;
370         unsigned int seg, bus;
371
372         /*
373          * The string should be the same as root bridge's name
374          * Please look at 'pci_scan_bus_parented'
375          */
376         num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
377         if (num != 2)
378                 return -ENODEV;
379         *handle = acpi_get_pci_rootbridge_handle(seg, bus);
380         if (!*handle)
381                 return -ENODEV;
382         return 0;
383 }
384
385 static struct acpi_bus_type acpi_pci_bus = {
386         .bus = &pci_bus_type,
387         .find_device = acpi_pci_find_device,
388         .find_bridge = acpi_pci_find_root_bridge,
389 };
390
391 static int __init acpi_pci_init(void)
392 {
393         int ret;
394
395         if (acpi_gbl_FADT.boot_flags & BAF_MSI_NOT_SUPPORTED) {
396                 printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
397                 pci_no_msi();
398         }
399
400         if (acpi_gbl_FADT.boot_flags & BAF_PCIE_ASPM_CONTROL) {
401                 printk(KERN_INFO"ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
402                 pcie_no_aspm();
403         }
404
405         ret = register_acpi_bus_type(&acpi_pci_bus);
406         if (ret)
407                 return 0;
408         pci_set_platform_pm(&acpi_pci_platform_pm);
409         return 0;
410 }
411 arch_initcall(acpi_pci_init);