[PATCH] acpi bridge hotadd: ACPI based root bridge hot-add
[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 = NULL;
130         struct acpi_buffer      buffer = {0, NULL};
131         acpi_handle             handle = NULL;
132         struct pci_dev          *dev;
133         struct pci_bus          *bus;
134
135         ACPI_FUNCTION_TRACE("acpi_pci_bind");
136
137         if (!device || !device->parent)
138                 return_VALUE(-EINVAL);
139
140         pathname = kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
141         if(!pathname)
142                 return_VALUE(-ENOMEM);
143         memset(pathname, 0, ACPI_PATHNAME_MAX);
144         buffer.length = ACPI_PATHNAME_MAX;
145         buffer.pointer = pathname;
146
147         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
148         if (!data){
149                 kfree (pathname);
150                 return_VALUE(-ENOMEM);
151         }
152         memset(data, 0, sizeof(struct acpi_pci_data));
153
154         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
155         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n", 
156                 pathname));
157
158         /* 
159          * Segment & Bus
160          * -------------
161          * These are obtained via the parent device's ACPI-PCI context.
162          */
163         status = acpi_get_data(device->parent->handle, acpi_pci_data_handler, 
164                 (void**) &pdata);
165         if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
166                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
167                         "Invalid ACPI-PCI context for parent device %s\n",
168                         acpi_device_bid(device->parent)));
169                 result = -ENODEV;
170                 goto end;
171         }
172         data->id.segment = pdata->id.segment;
173         data->id.bus = pdata->bus->number;
174
175         /*
176          * Device & Function
177          * -----------------
178          * These are simply obtained from the device's _ADR method.  Note
179          * that a value of zero is valid.
180          */
181         data->id.device = device->pnp.bus_address >> 16;
182         data->id.function = device->pnp.bus_address & 0xFFFF;
183
184         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
185                 data->id.segment, data->id.bus, data->id.device, 
186                 data->id.function));
187
188         /*
189          * TBD: Support slot devices (e.g. function=0xFFFF).
190          */
191
192         /* 
193          * Locate PCI Device
194          * -----------------
195          * Locate matching device in PCI namespace.  If it doesn't exist
196          * this typically means that the device isn't currently inserted
197          * (e.g. docking station, port replicator, etc.).
198          * We cannot simply search the global pci device list, since
199          * PCI devices are added to the global pci list when the root
200          * bridge start ops are run, which may not have happened yet.
201          */
202         bus = pci_find_bus(data->id.segment, data->id.bus);
203         if (bus) {
204                 list_for_each_entry(dev, &bus->devices, bus_list) {
205                         if (dev->devfn == PCI_DEVFN(data->id.device,
206                                                 data->id.function)) {
207                                 data->dev = dev;
208                                 break;
209                         }
210                 }
211         }
212         if (!data->dev) {
213                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
214                         "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
215                         data->id.segment, data->id.bus, 
216                         data->id.device, data->id.function));
217                 result = -ENODEV;
218                 goto end;
219         }
220         if (!data->dev->bus) {
221                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
222                         "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
223                         data->id.segment, data->id.bus, 
224                         data->id.device, data->id.function));
225                 result = -ENODEV;
226                 goto end;
227         }
228
229         /*
230          * PCI Bridge?
231          * -----------
232          * If so, set the 'bus' field and install the 'bind' function to 
233          * facilitate callbacks for all of its children.
234          */
235         if (data->dev->subordinate) {
236                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
237                         "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
238                         data->id.segment, data->id.bus, 
239                         data->id.device, data->id.function));
240                 data->bus = data->dev->subordinate;
241                 device->ops.bind = acpi_pci_bind;
242                 device->ops.unbind = acpi_pci_unbind;
243         }
244
245         /*
246          * Attach ACPI-PCI Context
247          * -----------------------
248          * Thus binding the ACPI and PCI devices.
249          */
250         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
251         if (ACPI_FAILURE(status)) {
252                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
253                         "Unable to attach ACPI-PCI context to device %s\n",
254                         acpi_device_bid(device)));
255                 result = -ENODEV;
256                 goto end;
257         }
258
259         /*
260          * PCI Routing Table
261          * -----------------
262          * Evaluate and parse _PRT, if exists.  This code is independent of 
263          * PCI bridges (above) to allow parsing of _PRT objects within the
264          * scope of non-bridge devices.  Note that _PRTs within the scope of
265          * a PCI bridge assume the bridge's subordinate bus number.
266          *
267          * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
268          */
269         status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
270         if (ACPI_SUCCESS(status)) {
271                 if (data->bus)                              /* PCI-PCI bridge */
272                         acpi_pci_irq_add_prt(device->handle, data->id.segment, 
273                                 data->bus->number);
274                 else                                 /* non-bridge PCI device */
275                         acpi_pci_irq_add_prt(device->handle, data->id.segment,
276                                 data->id.bus);
277         }
278
279 end:
280         kfree(pathname);
281         if (result)
282                 kfree(data);
283
284         return_VALUE(result);
285 }
286
287 int acpi_pci_unbind(
288         struct acpi_device      *device)
289 {
290         int                     result = 0;
291         acpi_status             status = AE_OK;
292         struct acpi_pci_data    *data = NULL;
293         char                    *pathname = NULL;
294         struct acpi_buffer      buffer = {0, NULL};
295
296         ACPI_FUNCTION_TRACE("acpi_pci_unbind");
297
298         if (!device || !device->parent)
299                 return_VALUE(-EINVAL);
300
301         pathname = (char *) kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
302         if(!pathname)
303                 return_VALUE(-ENOMEM);
304         memset(pathname, 0, ACPI_PATHNAME_MAX);
305
306         buffer.length = ACPI_PATHNAME_MAX;
307         buffer.pointer = pathname;
308         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
309         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
310                 pathname));
311         kfree(pathname);
312
313         status = acpi_get_data(device->handle, acpi_pci_data_handler, (void**)&data);
314         if (ACPI_FAILURE(status)) {
315                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
316                         "Unable to get data from device %s\n",
317                         acpi_device_bid(device)));
318                 result = -ENODEV;
319                 goto end;
320         }
321
322         status = acpi_detach_data(device->handle, acpi_pci_data_handler);
323         if (ACPI_FAILURE(status)) {
324                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
325                         "Unable to detach data from device %s\n",
326                         acpi_device_bid(device)));
327                 result = -ENODEV;
328                 goto end;
329         }
330         if (data->dev->subordinate) {
331                 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
332         }
333         kfree(data);
334
335 end:
336         return_VALUE(result);
337 }
338
339 int 
340 acpi_pci_bind_root (
341         struct acpi_device      *device,
342         struct acpi_pci_id      *id,
343         struct pci_bus          *bus) 
344 {
345         int                     result = 0;
346         acpi_status             status = AE_OK;
347         struct acpi_pci_data    *data = NULL;
348         char                    *pathname = NULL;
349         struct acpi_buffer      buffer = {0, NULL};
350
351         ACPI_FUNCTION_TRACE("acpi_pci_bind_root");
352
353         pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
354         if(!pathname)
355                 return_VALUE(-ENOMEM);
356         memset(pathname, 0, ACPI_PATHNAME_MAX);
357
358         buffer.length = ACPI_PATHNAME_MAX;
359         buffer.pointer = pathname;
360
361         if (!device || !id || !bus){
362                 kfree(pathname);
363                 return_VALUE(-EINVAL);
364         }
365
366         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
367         if (!data){
368                 kfree(pathname);
369                 return_VALUE(-ENOMEM);
370         }
371         memset(data, 0, sizeof(struct acpi_pci_data));
372
373         data->id = *id;
374         data->bus = bus;
375         device->ops.bind = acpi_pci_bind;
376         device->ops.unbind = acpi_pci_unbind;
377
378         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
379
380         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
381                 "%02x:%02x\n", pathname, id->segment, id->bus));
382
383         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
384         if (ACPI_FAILURE(status)) {
385                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
386                         "Unable to attach ACPI-PCI context to device %s\n",
387                         pathname));
388                 result = -ENODEV;
389                 goto end;
390         }
391
392 end:
393         kfree(pathname);
394         if (result != 0)
395                 kfree(data);
396
397         return_VALUE(result);
398 }