ce3f41b2487ca01a134a4094d3830ce4d3513f1b
[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 #ifdef ACPI_FUTURE_USAGE
71 acpi_status
72 acpi_os_get_pci_id (
73         acpi_handle             handle,
74         struct acpi_pci_id      *id)
75 {
76         int                     result = 0;
77         acpi_status             status = AE_OK;
78         struct acpi_device      *device = NULL;
79         struct acpi_pci_data    *data = NULL;
80
81         ACPI_FUNCTION_TRACE("acpi_os_get_pci_id");
82
83         if (!id)
84                 return_ACPI_STATUS(AE_BAD_PARAMETER);
85
86         result = acpi_bus_get_device(handle, &device);
87         if (result) {
88                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
89                         "Invalid ACPI Bus context for device %s\n",
90                         acpi_device_bid(device)));
91                 return_ACPI_STATUS(AE_NOT_EXIST);
92         }
93
94         status = acpi_get_data(handle, acpi_pci_data_handler, (void**) &data);
95         if (ACPI_FAILURE(status) || !data || !data->dev) {
96                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
97                         "Invalid ACPI-PCI context for device %s\n",
98                         acpi_device_bid(device)));
99                 return_ACPI_STATUS(status);
100         }
101
102         *id = data->id;
103         
104         /*
105         id->segment = data->id.segment;
106         id->bus = data->id.bus;
107         id->device = data->id.device;
108         id->function = data->id.function;
109         */
110
111         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
112                 "Device %s has PCI address %02x:%02x:%02x.%02x\n", 
113                 acpi_device_bid(device), id->segment, id->bus, 
114                 id->device, id->function));
115
116         return_ACPI_STATUS(AE_OK);
117 }
118 #endif  /*  ACPI_FUTURE_USAGE  */
119
120         
121 int
122 acpi_pci_bind (
123         struct acpi_device      *device)
124 {
125         int                     result = 0;
126         acpi_status             status = AE_OK;
127         struct acpi_pci_data    *data = NULL;
128         struct acpi_pci_data    *pdata = NULL;
129         char                    pathname[ACPI_PATHNAME_MAX] = {0};
130         struct acpi_buffer      buffer = {ACPI_PATHNAME_MAX, pathname};
131         acpi_handle             handle = NULL;
132
133         ACPI_FUNCTION_TRACE("acpi_pci_bind");
134
135         if (!device || !device->parent)
136                 return_VALUE(-EINVAL);
137
138         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
139         if (!data)
140                 return_VALUE(-ENOMEM);
141         memset(data, 0, sizeof(struct acpi_pci_data));
142
143         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
144         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n", 
145                 pathname));
146
147         /* 
148          * Segment & Bus
149          * -------------
150          * These are obtained via the parent device's ACPI-PCI context.
151          */
152         status = acpi_get_data(device->parent->handle, acpi_pci_data_handler, 
153                 (void**) &pdata);
154         if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
155                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
156                         "Invalid ACPI-PCI context for parent device %s\n",
157                         acpi_device_bid(device->parent)));
158                 result = -ENODEV;
159                 goto end;
160         }
161         data->id.segment = pdata->id.segment;
162         data->id.bus = pdata->bus->number;
163
164         /*
165          * Device & Function
166          * -----------------
167          * These are simply obtained from the device's _ADR method.  Note
168          * that a value of zero is valid.
169          */
170         data->id.device = device->pnp.bus_address >> 16;
171         data->id.function = device->pnp.bus_address & 0xFFFF;
172
173         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
174                 data->id.segment, data->id.bus, data->id.device, 
175                 data->id.function));
176
177         /*
178          * TBD: Support slot devices (e.g. function=0xFFFF).
179          */
180
181         /* 
182          * Locate PCI Device
183          * -----------------
184          * Locate matching device in PCI namespace.  If it doesn't exist
185          * this typically means that the device isn't currently inserted
186          * (e.g. docking station, port replicator, etc.).
187          */
188         data->dev = pci_find_slot(data->id.bus, PCI_DEVFN(data->id.device, data->id.function));
189         if (!data->dev) {
190                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
191                         "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
192                         data->id.segment, data->id.bus, 
193                         data->id.device, data->id.function));
194                 result = -ENODEV;
195                 goto end;
196         }
197         if (!data->dev->bus) {
198                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
199                         "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
200                         data->id.segment, data->id.bus, 
201                         data->id.device, data->id.function));
202                 result = -ENODEV;
203                 goto end;
204         }
205
206         /*
207          * PCI Bridge?
208          * -----------
209          * If so, set the 'bus' field and install the 'bind' function to 
210          * facilitate callbacks for all of its children.
211          */
212         if (data->dev->subordinate) {
213                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
214                         "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
215                         data->id.segment, data->id.bus, 
216                         data->id.device, data->id.function));
217                 data->bus = data->dev->subordinate;
218                 device->ops.bind = acpi_pci_bind;
219         }
220
221         /*
222          * Attach ACPI-PCI Context
223          * -----------------------
224          * Thus binding the ACPI and PCI devices.
225          */
226         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
227         if (ACPI_FAILURE(status)) {
228                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
229                         "Unable to attach ACPI-PCI context to device %s\n",
230                         acpi_device_bid(device)));
231                 result = -ENODEV;
232                 goto end;
233         }
234
235         /*
236          * PCI Routing Table
237          * -----------------
238          * Evaluate and parse _PRT, if exists.  This code is independent of 
239          * PCI bridges (above) to allow parsing of _PRT objects within the
240          * scope of non-bridge devices.  Note that _PRTs within the scope of
241          * a PCI bridge assume the bridge's subordinate bus number.
242          *
243          * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
244          */
245         status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
246         if (ACPI_SUCCESS(status)) {
247                 if (data->bus)                              /* PCI-PCI bridge */
248                         acpi_pci_irq_add_prt(device->handle, data->id.segment, 
249                                 data->bus->number);
250                 else                                 /* non-bridge PCI device */
251                         acpi_pci_irq_add_prt(device->handle, data->id.segment,
252                                 data->id.bus);
253         }
254
255 end:
256         if (result)
257                 kfree(data);
258
259         return_VALUE(result);
260 }
261
262
263 int 
264 acpi_pci_bind_root (
265         struct acpi_device      *device,
266         struct acpi_pci_id      *id,
267         struct pci_bus          *bus) 
268 {
269         int                     result = 0;
270         acpi_status             status = AE_OK;
271         struct acpi_pci_data    *data = NULL;
272         char                    pathname[ACPI_PATHNAME_MAX] = {0};
273         struct acpi_buffer      buffer = {ACPI_PATHNAME_MAX, pathname};
274
275         ACPI_FUNCTION_TRACE("acpi_pci_bind_root");
276
277         if (!device || !id || !bus)
278                 return_VALUE(-EINVAL);
279
280         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
281         if (!data)
282                 return_VALUE(-ENOMEM);
283         memset(data, 0, sizeof(struct acpi_pci_data));
284
285         data->id = *id;
286         data->bus = bus;
287         device->ops.bind = acpi_pci_bind;
288
289         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
290
291         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
292                 "%02x:%02x\n", pathname, id->segment, id->bus));
293
294         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
295         if (ACPI_FAILURE(status)) {
296                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
297                         "Unable to attach ACPI-PCI context to device %s\n",
298                         pathname));
299                 result = -ENODEV;
300                 goto end;
301         }
302
303 end:
304         if (result != 0)
305                 kfree(data);
306
307         return_VALUE(result);
308 }