Merge HEAD from ../from-linus
[pandora-kernel.git] / drivers / acpi / pci_bind.c
1 /*
2  *  pci_bind.c - ACPI PCI Device Binding ($Revision: 2 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm.h>
33 #include <linux/pci.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37
38 #define _COMPONENT              ACPI_PCI_COMPONENT
39 ACPI_MODULE_NAME("pci_bind")
40
41 struct acpi_pci_data {
42         struct acpi_pci_id id;
43         struct pci_bus *bus;
44         struct pci_dev *dev;
45 };
46
47 void acpi_pci_data_handler(acpi_handle handle, u32 function, void *context)
48 {
49         ACPI_FUNCTION_TRACE("acpi_pci_data_handler");
50
51         /* TBD: Anything we need to do here? */
52
53         return_VOID;
54 }
55
56 /**
57  * acpi_get_pci_id
58  * ------------------
59  * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
60  * to resolve PCI information for ACPI-PCI devices defined in the namespace.
61  * This typically occurs when resolving PCI operation region information.
62  */
63 acpi_status acpi_get_pci_id(acpi_handle handle, struct acpi_pci_id *id)
64 {
65         int result = 0;
66         acpi_status status = AE_OK;
67         struct acpi_device *device = NULL;
68         struct acpi_pci_data *data = NULL;
69
70         ACPI_FUNCTION_TRACE("acpi_get_pci_id");
71
72         if (!id)
73                 return_ACPI_STATUS(AE_BAD_PARAMETER);
74
75         result = acpi_bus_get_device(handle, &device);
76         if (result) {
77                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
78                                   "Invalid ACPI Bus context for device %s\n",
79                                   acpi_device_bid(device)));
80                 return_ACPI_STATUS(AE_NOT_EXIST);
81         }
82
83         status = acpi_get_data(handle, acpi_pci_data_handler, (void **)&data);
84         if (ACPI_FAILURE(status) || !data) {
85                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
86                                   "Invalid ACPI-PCI context for device %s\n",
87                                   acpi_device_bid(device)));
88                 return_ACPI_STATUS(status);
89         }
90
91         *id = data->id;
92
93         /*
94            id->segment = data->id.segment;
95            id->bus = data->id.bus;
96            id->device = data->id.device;
97            id->function = data->id.function;
98          */
99
100         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
101                           "Device %s has PCI address %02x:%02x:%02x.%02x\n",
102                           acpi_device_bid(device), id->segment, id->bus,
103                           id->device, id->function));
104
105         return_ACPI_STATUS(AE_OK);
106 }
107
108 EXPORT_SYMBOL(acpi_get_pci_id);
109
110 int acpi_pci_bind(struct acpi_device *device)
111 {
112         int result = 0;
113         acpi_status status = AE_OK;
114         struct acpi_pci_data *data = NULL;
115         struct acpi_pci_data *pdata = NULL;
116         char *pathname = NULL;
117         struct acpi_buffer buffer = { 0, NULL };
118         acpi_handle handle = NULL;
119         struct pci_dev *dev;
120         struct pci_bus *bus;
121
122         ACPI_FUNCTION_TRACE("acpi_pci_bind");
123
124         if (!device || !device->parent)
125                 return_VALUE(-EINVAL);
126
127         pathname = kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
128         if (!pathname)
129                 return_VALUE(-ENOMEM);
130         memset(pathname, 0, ACPI_PATHNAME_MAX);
131         buffer.length = ACPI_PATHNAME_MAX;
132         buffer.pointer = pathname;
133
134         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
135         if (!data) {
136                 kfree(pathname);
137                 return_VALUE(-ENOMEM);
138         }
139         memset(data, 0, sizeof(struct acpi_pci_data));
140
141         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
142         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
143                           pathname));
144
145         /* 
146          * Segment & Bus
147          * -------------
148          * These are obtained via the parent device's ACPI-PCI context.
149          */
150         status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
151                                (void **)&pdata);
152         if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
153                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
154                                   "Invalid ACPI-PCI context for parent device %s\n",
155                                   acpi_device_bid(device->parent)));
156                 result = -ENODEV;
157                 goto end;
158         }
159         data->id.segment = pdata->id.segment;
160         data->id.bus = pdata->bus->number;
161
162         /*
163          * Device & Function
164          * -----------------
165          * These are simply obtained from the device's _ADR method.  Note
166          * that a value of zero is valid.
167          */
168         data->id.device = device->pnp.bus_address >> 16;
169         data->id.function = device->pnp.bus_address & 0xFFFF;
170
171         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
172                           data->id.segment, data->id.bus, data->id.device,
173                           data->id.function));
174
175         /*
176          * TBD: Support slot devices (e.g. function=0xFFFF).
177          */
178
179         /* 
180          * Locate PCI Device
181          * -----------------
182          * Locate matching device in PCI namespace.  If it doesn't exist
183          * this typically means that the device isn't currently inserted
184          * (e.g. docking station, port replicator, etc.).
185          * We cannot simply search the global pci device list, since
186          * PCI devices are added to the global pci list when the root
187          * bridge start ops are run, which may not have happened yet.
188          */
189         bus = pci_find_bus(data->id.segment, data->id.bus);
190         if (bus) {
191                 list_for_each_entry(dev, &bus->devices, bus_list) {
192                         if (dev->devfn == PCI_DEVFN(data->id.device,
193                                                     data->id.function)) {
194                                 data->dev = dev;
195                                 break;
196                         }
197                 }
198         }
199         if (!data->dev) {
200                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
201                                   "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
202                                   data->id.segment, data->id.bus,
203                                   data->id.device, data->id.function));
204                 result = -ENODEV;
205                 goto end;
206         }
207         if (!data->dev->bus) {
208                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
209                                   "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
210                                   data->id.segment, data->id.bus,
211                                   data->id.device, data->id.function));
212                 result = -ENODEV;
213                 goto end;
214         }
215
216         /*
217          * PCI Bridge?
218          * -----------
219          * If so, set the 'bus' field and install the 'bind' function to 
220          * facilitate callbacks for all of its children.
221          */
222         if (data->dev->subordinate) {
223                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
224                                   "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
225                                   data->id.segment, data->id.bus,
226                                   data->id.device, data->id.function));
227                 data->bus = data->dev->subordinate;
228                 device->ops.bind = acpi_pci_bind;
229                 device->ops.unbind = acpi_pci_unbind;
230         }
231
232         /*
233          * Attach ACPI-PCI Context
234          * -----------------------
235          * Thus binding the ACPI and PCI devices.
236          */
237         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
238         if (ACPI_FAILURE(status)) {
239                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
240                                   "Unable to attach ACPI-PCI context to device %s\n",
241                                   acpi_device_bid(device)));
242                 result = -ENODEV;
243                 goto end;
244         }
245
246         /*
247          * PCI Routing Table
248          * -----------------
249          * Evaluate and parse _PRT, if exists.  This code is independent of 
250          * PCI bridges (above) to allow parsing of _PRT objects within the
251          * scope of non-bridge devices.  Note that _PRTs within the scope of
252          * a PCI bridge assume the bridge's subordinate bus number.
253          *
254          * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
255          */
256         status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
257         if (ACPI_SUCCESS(status)) {
258                 if (data->bus)  /* PCI-PCI bridge */
259                         acpi_pci_irq_add_prt(device->handle, data->id.segment,
260                                              data->bus->number);
261                 else            /* non-bridge PCI device */
262                         acpi_pci_irq_add_prt(device->handle, data->id.segment,
263                                              data->id.bus);
264         }
265
266       end:
267         kfree(pathname);
268         if (result)
269                 kfree(data);
270
271         return_VALUE(result);
272 }
273
274 int acpi_pci_unbind(struct acpi_device *device)
275 {
276         int result = 0;
277         acpi_status status = AE_OK;
278         struct acpi_pci_data *data = NULL;
279         char *pathname = NULL;
280         struct acpi_buffer buffer = { 0, NULL };
281
282         ACPI_FUNCTION_TRACE("acpi_pci_unbind");
283
284         if (!device || !device->parent)
285                 return_VALUE(-EINVAL);
286
287         pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
288         if (!pathname)
289                 return_VALUE(-ENOMEM);
290         memset(pathname, 0, ACPI_PATHNAME_MAX);
291
292         buffer.length = ACPI_PATHNAME_MAX;
293         buffer.pointer = pathname;
294         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
295         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
296                           pathname));
297         kfree(pathname);
298
299         status =
300             acpi_get_data(device->handle, acpi_pci_data_handler,
301                           (void **)&data);
302         if (ACPI_FAILURE(status)) {
303                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
304                                   "Unable to get data from device %s\n",
305                                   acpi_device_bid(device)));
306                 result = -ENODEV;
307                 goto end;
308         }
309
310         status = acpi_detach_data(device->handle, acpi_pci_data_handler);
311         if (ACPI_FAILURE(status)) {
312                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
313                                   "Unable to detach data from device %s\n",
314                                   acpi_device_bid(device)));
315                 result = -ENODEV;
316                 goto end;
317         }
318         if (data->dev->subordinate) {
319                 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
320         }
321         kfree(data);
322
323       end:
324         return_VALUE(result);
325 }
326
327 int
328 acpi_pci_bind_root(struct acpi_device *device,
329                    struct acpi_pci_id *id, struct pci_bus *bus)
330 {
331         int result = 0;
332         acpi_status status = AE_OK;
333         struct acpi_pci_data *data = NULL;
334         char *pathname = NULL;
335         struct acpi_buffer buffer = { 0, NULL };
336
337         ACPI_FUNCTION_TRACE("acpi_pci_bind_root");
338
339         pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
340         if (!pathname)
341                 return_VALUE(-ENOMEM);
342         memset(pathname, 0, ACPI_PATHNAME_MAX);
343
344         buffer.length = ACPI_PATHNAME_MAX;
345         buffer.pointer = pathname;
346
347         if (!device || !id || !bus) {
348                 kfree(pathname);
349                 return_VALUE(-EINVAL);
350         }
351
352         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
353         if (!data) {
354                 kfree(pathname);
355                 return_VALUE(-ENOMEM);
356         }
357         memset(data, 0, sizeof(struct acpi_pci_data));
358
359         data->id = *id;
360         data->bus = bus;
361         device->ops.bind = acpi_pci_bind;
362         device->ops.unbind = acpi_pci_unbind;
363
364         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
365
366         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
367                           "%02x:%02x\n", pathname, id->segment, id->bus));
368
369         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
370         if (ACPI_FAILURE(status)) {
371                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
372                                   "Unable to attach ACPI-PCI context to device %s\n",
373                                   pathname));
374                 result = -ENODEV;
375                 goto end;
376         }
377
378       end:
379         kfree(pathname);
380         if (result != 0)
381                 kfree(data);
382
383         return_VALUE(result);
384 }