[ACPI] Bind ACPI and PCI devices
[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 <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
21 static u32 ctrlset_buf[3] = {0, 0, 0};
22 static u32 global_ctrlsets = 0;
23 static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
24
25 static acpi_status  
26 acpi_query_osc (
27         acpi_handle     handle,
28         u32             level,
29         void            *context,
30         void            **retval )
31 {
32         acpi_status             status;
33         struct acpi_object_list input;
34         union acpi_object       in_params[4];
35         struct acpi_buffer      output;
36         union acpi_object       out_obj;        
37         u32                     osc_dw0;
38
39         /* Setting up output buffer */
40         output.length = sizeof(out_obj) + 3*sizeof(u32);  
41         output.pointer = &out_obj;
42         
43         /* Setting up input parameters */
44         input.count = 4;
45         input.pointer = in_params;
46         in_params[0].type               = ACPI_TYPE_BUFFER;
47         in_params[0].buffer.length      = 16;
48         in_params[0].buffer.pointer     = OSC_UUID;
49         in_params[1].type               = ACPI_TYPE_INTEGER;
50         in_params[1].integer.value      = 1;
51         in_params[2].type               = ACPI_TYPE_INTEGER;
52         in_params[2].integer.value      = 3;
53         in_params[3].type               = ACPI_TYPE_BUFFER;
54         in_params[3].buffer.length      = 12;
55         in_params[3].buffer.pointer     = (u8 *)context;
56
57         status = acpi_evaluate_object(handle, "_OSC", &input, &output);
58         if (ACPI_FAILURE (status)) {
59                 printk(KERN_DEBUG  
60                         "Evaluate _OSC Set fails. Status = 0x%04x\n", status);
61                 return status;
62         }
63         if (out_obj.type != ACPI_TYPE_BUFFER) {
64                 printk(KERN_DEBUG  
65                         "Evaluate _OSC returns wrong type\n");
66                 return AE_TYPE;
67         }
68         osc_dw0 = *((u32 *) out_obj.buffer.pointer);
69         if (osc_dw0) {
70                 if (osc_dw0 & OSC_REQUEST_ERROR)
71                         printk(KERN_DEBUG "_OSC request fails\n"); 
72                 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
73                         printk(KERN_DEBUG "_OSC invalid UUID\n"); 
74                 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
75                         printk(KERN_DEBUG "_OSC invalid revision\n"); 
76                 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
77                         /* Update Global Control Set */
78                         global_ctrlsets = *((u32 *)(out_obj.buffer.pointer+8));
79                         return AE_OK;
80                 }
81                 return AE_ERROR;
82         }
83
84         /* Update Global Control Set */
85         global_ctrlsets = *((u32 *)(out_obj.buffer.pointer + 8));
86         return AE_OK;
87 }
88
89
90 static acpi_status  
91 acpi_run_osc (
92         acpi_handle     handle,
93         u32             level,
94         void            *context,
95         void            **retval )
96 {
97         acpi_status             status;
98         struct acpi_object_list input;
99         union acpi_object       in_params[4];
100         struct acpi_buffer      output;
101         union acpi_object       out_obj;        
102         u32                     osc_dw0;
103
104         /* Setting up output buffer */
105         output.length = sizeof(out_obj) + 3*sizeof(u32);  
106         output.pointer = &out_obj;
107         
108         /* Setting up input parameters */
109         input.count = 4;
110         input.pointer = in_params;
111         in_params[0].type               = ACPI_TYPE_BUFFER;
112         in_params[0].buffer.length      = 16;
113         in_params[0].buffer.pointer     = OSC_UUID;
114         in_params[1].type               = ACPI_TYPE_INTEGER;
115         in_params[1].integer.value      = 1;
116         in_params[2].type               = ACPI_TYPE_INTEGER;
117         in_params[2].integer.value      = 3;
118         in_params[3].type               = ACPI_TYPE_BUFFER;
119         in_params[3].buffer.length      = 12;
120         in_params[3].buffer.pointer     = (u8 *)context;
121
122         status = acpi_evaluate_object(handle, "_OSC", &input, &output);
123         if (ACPI_FAILURE (status)) {
124                 printk(KERN_DEBUG  
125                         "Evaluate _OSC Set fails. Status = 0x%04x\n", status);
126                 return status;
127         }
128         if (out_obj.type != ACPI_TYPE_BUFFER) {
129                 printk(KERN_DEBUG  
130                         "Evaluate _OSC returns wrong type\n");
131                 return AE_TYPE;
132         }
133         osc_dw0 = *((u32 *) out_obj.buffer.pointer);
134         if (osc_dw0) {
135                 if (osc_dw0 & OSC_REQUEST_ERROR)
136                         printk(KERN_DEBUG "_OSC request fails\n"); 
137                 if (osc_dw0 & OSC_INVALID_UUID_ERROR)
138                         printk(KERN_DEBUG "_OSC invalid UUID\n"); 
139                 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
140                         printk(KERN_DEBUG "_OSC invalid revision\n"); 
141                 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
142                         printk(KERN_DEBUG "_OSC FW not grant req. control\n");
143                         return AE_SUPPORT;
144                 }
145                 return AE_ERROR;
146         }
147         return AE_OK;
148 }
149
150 /**
151  * pci_osc_support_set - register OS support to Firmware
152  * @flags: OS support bits
153  *
154  * Update OS support fields and doing a _OSC Query to obtain an update
155  * from Firmware on supported control bits.
156  **/
157 acpi_status pci_osc_support_set(u32 flags)
158 {
159         u32 temp;
160
161         if (!(flags & OSC_SUPPORT_MASKS)) {
162                 return AE_TYPE;
163         }
164         ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
165
166         /* do _OSC query for all possible controls */
167         temp = ctrlset_buf[OSC_CONTROL_TYPE];
168         ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
169         ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
170         acpi_get_devices ( PCI_ROOT_HID_STRING,
171                         acpi_query_osc,
172                         ctrlset_buf,
173                         NULL );
174         ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
175         ctrlset_buf[OSC_CONTROL_TYPE] = temp;
176         return AE_OK;
177 }
178 EXPORT_SYMBOL(pci_osc_support_set);
179
180 /**
181  * pci_osc_control_set - commit requested control to Firmware
182  * @flags: driver's requested control bits
183  *
184  * Attempt to take control from Firmware on requested control bits.
185  **/
186 acpi_status pci_osc_control_set(u32 flags)
187 {
188         acpi_status     status;
189         u32             ctrlset;
190
191         ctrlset = (flags & OSC_CONTROL_MASKS);
192         if (!ctrlset) {
193                 return AE_TYPE;
194         }
195         if (ctrlset_buf[OSC_SUPPORT_TYPE] && 
196                 ((global_ctrlsets & ctrlset) != ctrlset)) {
197                 return AE_SUPPORT;
198         }
199         ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
200         status = acpi_get_devices ( PCI_ROOT_HID_STRING,
201                                 acpi_run_osc,
202                                 ctrlset_buf,
203                                 NULL );
204         if (ACPI_FAILURE (status)) {
205                 ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
206         }
207         
208         return status;
209 }
210 EXPORT_SYMBOL(pci_osc_control_set);
211
212 /* ACPI bus type */
213 static int pci_acpi_find_device(struct device *dev, acpi_handle *handle)
214 {
215         struct pci_dev * pci_dev;
216         acpi_integer    addr;
217
218         pci_dev = to_pci_dev(dev);
219         /* Please ref to ACPI spec for the syntax of _ADR */
220         addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
221         *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
222         if (!*handle)
223                 return -ENODEV;
224         return 0;
225 }
226
227 static int pci_acpi_find_root_bridge(struct device *dev, acpi_handle *handle)
228 {
229         int num;
230         unsigned int seg, bus;
231
232         /*
233          * The string should be the same as root bridge's name
234          * Please look at 'pci_scan_bus_parented'
235          */
236         num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
237         if (num != 2)
238                 return -ENODEV;
239         *handle = acpi_get_pci_rootbridge_handle(seg, bus);
240         if (!*handle)
241                 return -ENODEV;
242         return 0;
243 }
244
245 static struct acpi_bus_type pci_acpi_bus = {
246         .bus = &pci_bus_type,
247         .find_device = pci_acpi_find_device,
248         .find_bridge = pci_acpi_find_root_bridge,
249 };
250
251 static int __init pci_acpi_init(void)
252 {
253         int ret;
254
255         ret = register_acpi_bus_type(&pci_acpi_bus);
256         if (ret)
257                 return 0;
258         return 0;
259 }
260 arch_initcall(pci_acpi_init);