aa05e92464e6a0545b16bdd804b83527927003b3
[linux-2.6.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 static void acpi_pci_data_handler(acpi_handle handle, u32 function,
48                                   void *context)
49 {
50
51         /* TBD: Anything we need to do here? */
52
53         return;
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
71         if (!id)
72                 return AE_BAD_PARAMETER;
73
74         result = acpi_bus_get_device(handle, &device);
75         if (result) {
76                 printk(KERN_ERR PREFIX
77                             "Invalid ACPI Bus context for device %s\n",
78                             acpi_device_bid(device));
79                 return AE_NOT_EXIST;
80         }
81
82         status = acpi_get_data(handle, acpi_pci_data_handler, (void **)&data);
83         if (ACPI_FAILURE(status) || !data) {
84                 ACPI_EXCEPTION((AE_INFO, status,
85                                 "Invalid ACPI-PCI context for device %s",
86                                 acpi_device_bid(device)));
87                 return status;
88         }
89
90         *id = data->id;
91
92         /*
93            id->segment = data->id.segment;
94            id->bus = data->id.bus;
95            id->device = data->id.device;
96            id->function = data->id.function;
97          */
98
99         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
100                           "Device %s has PCI address %02x:%02x:%02x.%02x\n",
101                           acpi_device_bid(device), id->segment, id->bus,
102                           id->device, id->function));
103
104         return AE_OK;
105 }
106
107 EXPORT_SYMBOL(acpi_get_pci_id);
108
109 int acpi_pci_bind(struct acpi_device *device)
110 {
111         int result = 0;
112         acpi_status status = AE_OK;
113         struct acpi_pci_data *data = NULL;
114         struct acpi_pci_data *pdata = NULL;
115         char *pathname = NULL;
116         struct acpi_buffer buffer = { 0, NULL };
117         acpi_handle handle = NULL;
118         struct pci_dev *dev;
119         struct pci_bus *bus;
120
121
122         if (!device || !device->parent)
123                 return -EINVAL;
124
125         pathname = kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
126         if (!pathname)
127                 return -ENOMEM;
128         memset(pathname, 0, ACPI_PATHNAME_MAX);
129         buffer.length = ACPI_PATHNAME_MAX;
130         buffer.pointer = pathname;
131
132         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
133         if (!data) {
134                 kfree(pathname);
135                 return -ENOMEM;
136         }
137         memset(data, 0, sizeof(struct acpi_pci_data));
138
139         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
140         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
141                           pathname));
142
143         /* 
144          * Segment & Bus
145          * -------------
146          * These are obtained via the parent device's ACPI-PCI context.
147          */
148         status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
149                                (void **)&pdata);
150         if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
151                 ACPI_EXCEPTION((AE_INFO, status,
152                                 "Invalid ACPI-PCI context for parent device %s",
153                                 acpi_device_bid(device->parent)));
154                 result = -ENODEV;
155                 goto end;
156         }
157         data->id.segment = pdata->id.segment;
158         data->id.bus = pdata->bus->number;
159
160         /*
161          * Device & Function
162          * -----------------
163          * These are simply obtained from the device's _ADR method.  Note
164          * that a value of zero is valid.
165          */
166         data->id.device = device->pnp.bus_address >> 16;
167         data->id.function = device->pnp.bus_address & 0xFFFF;
168
169         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
170                           data->id.segment, data->id.bus, data->id.device,
171                           data->id.function));
172
173         /*
174          * TBD: Support slot devices (e.g. function=0xFFFF).
175          */
176
177         /* 
178          * Locate PCI Device
179          * -----------------
180          * Locate matching device in PCI namespace.  If it doesn't exist
181          * this typically means that the device isn't currently inserted
182          * (e.g. docking station, port replicator, etc.).
183          * We cannot simply search the global pci device list, since
184          * PCI devices are added to the global pci list when the root
185          * bridge start ops are run, which may not have happened yet.
186          */
187         bus = pci_find_bus(data->id.segment, data->id.bus);
188         if (bus) {
189                 list_for_each_entry(dev, &bus->devices, bus_list) {
190                         if (dev->devfn == PCI_DEVFN(data->id.device,
191                                                     data->id.function)) {
192                                 data->dev = dev;
193                                 break;
194                         }
195                 }
196         }
197         if (!data->dev) {
198                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
199                                   "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
200                                   data->id.segment, data->id.bus,
201                                   data->id.device, data->id.function));
202                 result = -ENODEV;
203                 goto end;
204         }
205         if (!data->dev->bus) {
206                 printk(KERN_ERR PREFIX
207                             "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
208                             data->id.segment, data->id.bus,
209                             data->id.device, data->id.function);
210                 result = -ENODEV;
211                 goto end;
212         }
213
214         /*
215          * PCI Bridge?
216          * -----------
217          * If so, set the 'bus' field and install the 'bind' function to 
218          * facilitate callbacks for all of its children.
219          */
220         if (data->dev->subordinate) {
221                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
222                                   "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
223                                   data->id.segment, data->id.bus,
224                                   data->id.device, data->id.function));
225                 data->bus = data->dev->subordinate;
226         }
227
228         /*
229          * Attach ACPI-PCI Context
230          * -----------------------
231          * Thus binding the ACPI and PCI devices.
232          */
233         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
234         if (ACPI_FAILURE(status)) {
235                 ACPI_EXCEPTION((AE_INFO, status,
236                                 "Unable to attach ACPI-PCI context to device %s",
237                                 acpi_device_bid(device)));
238                 result = -ENODEV;
239                 goto end;
240         }
241
242         /*
243          * PCI Routing Table
244          * -----------------
245          * Evaluate and parse _PRT, if exists.  This code is independent of 
246          * PCI bridges (above) to allow parsing of _PRT objects within the
247          * scope of non-bridge devices.  Note that _PRTs within the scope of
248          * a PCI bridge assume the bridge's subordinate bus number.
249          *
250          * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
251          */
252         status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
253         if (ACPI_SUCCESS(status)) {
254                 if (data->bus)  /* PCI-PCI bridge */
255                         acpi_pci_irq_add_prt(device->handle, data->id.segment,
256                                              data->bus->number);
257                 else            /* non-bridge PCI device */
258                         acpi_pci_irq_add_prt(device->handle, data->id.segment,
259                                              data->id.bus);
260         }
261
262       end:
263         kfree(pathname);
264         if (result)
265                 kfree(data);
266
267         return result;
268 }
269
270 int acpi_pci_unbind(struct acpi_device *device)
271 {
272         int result = 0;
273         acpi_status status = AE_OK;
274         struct acpi_pci_data *data = NULL;
275         char *pathname = NULL;
276         struct acpi_buffer buffer = { 0, NULL };
277
278
279         if (!device || !device->parent)
280                 return -EINVAL;
281
282         pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
283         if (!pathname)
284                 return -ENOMEM;
285         memset(pathname, 0, ACPI_PATHNAME_MAX);
286
287         buffer.length = ACPI_PATHNAME_MAX;
288         buffer.pointer = pathname;
289         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
290         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
291                           pathname));
292         kfree(pathname);
293
294         status =
295             acpi_get_data(device->handle, acpi_pci_data_handler,
296                           (void **)&data);
297         if (ACPI_FAILURE(status)) {
298                 ACPI_EXCEPTION((AE_INFO, status,
299                                 "Unable to get data from device %s",
300                                 acpi_device_bid(device)));
301                 result = -ENODEV;
302                 goto end;
303         }
304
305         status = acpi_detach_data(device->handle, acpi_pci_data_handler);
306         if (ACPI_FAILURE(status)) {
307                 ACPI_EXCEPTION((AE_INFO, status,
308                                 "Unable to detach data from device %s",
309                                 acpi_device_bid(device)));
310                 result = -ENODEV;
311                 goto end;
312         }
313         if (data->dev->subordinate) {
314                 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
315         }
316         kfree(data);
317
318       end:
319         return result;
320 }
321
322 int
323 acpi_pci_bind_root(struct acpi_device *device,
324                    struct acpi_pci_id *id, struct pci_bus *bus)
325 {
326         int result = 0;
327         acpi_status status = AE_OK;
328         struct acpi_pci_data *data = NULL;
329         char *pathname = NULL;
330         struct acpi_buffer buffer = { 0, NULL };
331
332
333         pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
334         if (!pathname)
335                 return -ENOMEM;
336         memset(pathname, 0, ACPI_PATHNAME_MAX);
337
338         buffer.length = ACPI_PATHNAME_MAX;
339         buffer.pointer = pathname;
340
341         if (!device || !id || !bus) {
342                 kfree(pathname);
343                 return -EINVAL;
344         }
345
346         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
347         if (!data) {
348                 kfree(pathname);
349                 return -ENOMEM;
350         }
351         memset(data, 0, sizeof(struct acpi_pci_data));
352
353         data->id = *id;
354         data->bus = bus;
355
356         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
357
358         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
359                           "%02x:%02x\n", pathname, id->segment, id->bus));
360
361         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
362         if (ACPI_FAILURE(status)) {
363                 ACPI_EXCEPTION((AE_INFO, status,
364                                 "Unable to attach ACPI-PCI context to device %s",
365                                 pathname));
366                 result = -ENODEV;
367                 goto end;
368         }
369
370       end:
371         kfree(pathname);
372         if (result != 0)
373                 kfree(data);
374
375         return result;
376 }
377
378 #define ACPI_PCI_BRIDGE_DRIVER_NAME "ACPI PCI Bridge Driver"
379
380 static int acpi_pci_bridge_add(struct acpi_device *device);
381 static int acpi_pci_bridge_remove(struct acpi_device *device, int type);
382
383 static struct acpi_driver acpi_pci_bridge_driver = {
384        .name = ACPI_PCI_BRIDGE_DRIVER_NAME,
385         .ids = ACPI_PCI_BRIDGE_HID,
386        .ops = {
387                .add = acpi_pci_bridge_add,
388                .remove = acpi_pci_bridge_remove,
389        },
390 };
391
392 static int acpi_pci_bridge_add(struct acpi_device *device)
393 {
394        return acpi_pci_bind(device);
395 }
396
397 static int acpi_pci_bridge_remove(struct acpi_device *device, int type)
398 {
399        return acpi_pci_unbind(device);
400 }
401
402 static int __init acpi_pci_bridge_init(void)
403 {
404        if (acpi_pci_disabled)
405                return 0;
406        if (acpi_bus_register_driver(&acpi_pci_bridge_driver) < 0)
407                return -ENODEV;
408        return 0;
409 }
410
411 /* Should be called after ACPI pci root driver */
412 subsys_initcall(acpi_pci_bridge_init);