ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
39 #define _COMPONENT              ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME                ("pci_bind")
41
42 struct acpi_pci_data {
43         struct acpi_pci_id      id;
44         struct pci_bus          *bus;
45         struct pci_dev          *dev;
46 };
47
48
49 void
50 acpi_pci_data_handler (
51         acpi_handle             handle,
52         u32                     function,
53         void                    *context)
54 {
55         ACPI_FUNCTION_TRACE("acpi_pci_data_handler");
56
57         /* TBD: Anything we need to do here? */
58
59         return_VOID;
60 }
61
62
63 /**
64  * acpi_os_get_pci_id
65  * ------------------
66  * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
67  * to resolve PCI information for ACPI-PCI devices defined in the namespace.
68  * This typically occurs when resolving PCI operation region information.
69  */
70 acpi_status
71 acpi_os_get_pci_id (
72         acpi_handle             handle,
73         struct acpi_pci_id      *id)
74 {
75         int                     result = 0;
76         acpi_status             status = AE_OK;
77         struct acpi_device      *device = NULL;
78         struct acpi_pci_data    *data = NULL;
79
80         ACPI_FUNCTION_TRACE("acpi_os_get_pci_id");
81
82         if (!id)
83                 return_ACPI_STATUS(AE_BAD_PARAMETER);
84
85         result = acpi_bus_get_device(handle, &device);
86         if (result) {
87                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
88                         "Invalid ACPI Bus context for device %s\n",
89                         acpi_device_bid(device)));
90                 return_ACPI_STATUS(AE_NOT_EXIST);
91         }
92
93         status = acpi_get_data(handle, acpi_pci_data_handler, (void**) &data);
94         if (ACPI_FAILURE(status) || !data || !data->dev) {
95                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
96                         "Invalid ACPI-PCI context for device %s\n",
97                         acpi_device_bid(device)));
98                 return_ACPI_STATUS(status);
99         }
100
101         *id = data->id;
102         
103         /*
104         id->segment = data->id.segment;
105         id->bus = data->id.bus;
106         id->device = data->id.device;
107         id->function = data->id.function;
108         */
109
110         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
111                 "Device %s has PCI address %02x:%02x:%02x.%02x\n", 
112                 acpi_device_bid(device), id->segment, id->bus, 
113                 id->device, id->function));
114
115         return_ACPI_STATUS(AE_OK);
116 }
117
118         
119 int
120 acpi_pci_bind (
121         struct acpi_device      *device)
122 {
123         int                     result = 0;
124         acpi_status             status = AE_OK;
125         struct acpi_pci_data    *data = NULL;
126         struct acpi_pci_data    *pdata = NULL;
127         char                    pathname[ACPI_PATHNAME_MAX] = {0};
128         struct acpi_buffer      buffer = {ACPI_PATHNAME_MAX, pathname};
129         acpi_handle             handle = NULL;
130
131         ACPI_FUNCTION_TRACE("acpi_pci_bind");
132
133         if (!device || !device->parent)
134                 return_VALUE(-EINVAL);
135
136         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
137         if (!data)
138                 return_VALUE(-ENOMEM);
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          */
186         data->dev = pci_find_slot(data->id.bus, PCI_DEVFN(data->id.device, data->id.function));
187         if (!data->dev) {
188                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
189                         "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
190                         data->id.segment, data->id.bus, 
191                         data->id.device, data->id.function));
192                 result = -ENODEV;
193                 goto end;
194         }
195         if (!data->dev->bus) {
196                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
197                         "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
198                         data->id.segment, data->id.bus, 
199                         data->id.device, data->id.function));
200                 result = -ENODEV;
201                 goto end;
202         }
203
204         /*
205          * PCI Bridge?
206          * -----------
207          * If so, set the 'bus' field and install the 'bind' function to 
208          * facilitate callbacks for all of its children.
209          */
210         if (data->dev->subordinate) {
211                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
212                         "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
213                         data->id.segment, data->id.bus, 
214                         data->id.device, data->id.function));
215                 data->bus = data->dev->subordinate;
216                 device->ops.bind = acpi_pci_bind;
217         }
218
219         /*
220          * Attach ACPI-PCI Context
221          * -----------------------
222          * Thus binding the ACPI and PCI devices.
223          */
224         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
225         if (ACPI_FAILURE(status)) {
226                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
227                         "Unable to attach ACPI-PCI context to device %s\n",
228                         acpi_device_bid(device)));
229                 result = -ENODEV;
230                 goto end;
231         }
232
233         /*
234          * PCI Routing Table
235          * -----------------
236          * Evaluate and parse _PRT, if exists.  This code is independent of 
237          * PCI bridges (above) to allow parsing of _PRT objects within the
238          * scope of non-bridge devices.  Note that _PRTs within the scope of
239          * a PCI bridge assume the bridge's subordinate bus number.
240          *
241          * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
242          */
243         status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
244         if (ACPI_SUCCESS(status)) {
245                 if (data->bus)                              /* PCI-PCI bridge */
246                         acpi_pci_irq_add_prt(device->handle, data->id.segment, 
247                                 data->bus->number);
248                 else                                 /* non-bridge PCI device */
249                         acpi_pci_irq_add_prt(device->handle, data->id.segment,
250                                 data->id.bus);
251         }
252
253 end:
254         if (result)
255                 kfree(data);
256
257         return_VALUE(result);
258 }
259
260
261 int 
262 acpi_pci_bind_root (
263         struct acpi_device      *device,
264         struct acpi_pci_id      *id,
265         struct pci_bus          *bus) 
266 {
267         int                     result = 0;
268         acpi_status             status = AE_OK;
269         struct acpi_pci_data    *data = NULL;
270         char                    pathname[ACPI_PATHNAME_MAX] = {0};
271         struct acpi_buffer      buffer = {ACPI_PATHNAME_MAX, pathname};
272
273         ACPI_FUNCTION_TRACE("acpi_pci_bind_root");
274
275         if (!device || !id || !bus)
276                 return_VALUE(-EINVAL);
277
278         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
279         if (!data)
280                 return_VALUE(-ENOMEM);
281         memset(data, 0, sizeof(struct acpi_pci_data));
282
283         data->id = *id;
284         data->bus = bus;
285         device->ops.bind = acpi_pci_bind;
286
287         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
288
289         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
290                 "%02x:%02x\n", pathname, id->segment, id->bus));
291
292         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
293         if (ACPI_FAILURE(status)) {
294                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
295                         "Unable to attach ACPI-PCI context to device %s\n",
296                         pathname));
297                 result = -ENODEV;
298                 goto end;
299         }
300
301 end:
302         if (result != 0)
303                 kfree(data);
304
305         return_VALUE(result);
306 }