ACPI: Optimize acpi_get_pci_rootbridge_handle() to boot faster
[safe/jmp/linux-2.6] / drivers / acpi / pci_root.c
1 /*
2  *  pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
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_root")
40 #define ACPI_PCI_ROOT_CLASS             "pci_bridge"
41 #define ACPI_PCI_ROOT_HID               "PNP0A03"
42 #define ACPI_PCI_ROOT_DRIVER_NAME       "ACPI PCI Root Bridge Driver"
43 #define ACPI_PCI_ROOT_DEVICE_NAME       "PCI Root Bridge"
44 static int acpi_pci_root_add(struct acpi_device *device);
45 static int acpi_pci_root_remove(struct acpi_device *device, int type);
46 static int acpi_pci_root_start(struct acpi_device *device);
47
48 static struct acpi_driver acpi_pci_root_driver = {
49         .name = ACPI_PCI_ROOT_DRIVER_NAME,
50         .class = ACPI_PCI_ROOT_CLASS,
51         .ids = ACPI_PCI_ROOT_HID,
52         .ops = {
53                 .add = acpi_pci_root_add,
54                 .remove = acpi_pci_root_remove,
55                 .start = acpi_pci_root_start,
56                 },
57 };
58
59 struct acpi_pci_root {
60         struct list_head node;
61         struct acpi_device * device;
62         struct acpi_pci_id id;
63         struct pci_bus *bus;
64 };
65
66 static LIST_HEAD(acpi_pci_roots);
67
68 static struct acpi_pci_driver *sub_driver;
69
70 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
71 {
72         int n = 0;
73         struct list_head *entry;
74
75         struct acpi_pci_driver **pptr = &sub_driver;
76         while (*pptr)
77                 pptr = &(*pptr)->next;
78         *pptr = driver;
79
80         if (!driver->add)
81                 return 0;
82
83         list_for_each(entry, &acpi_pci_roots) {
84                 struct acpi_pci_root *root;
85                 root = list_entry(entry, struct acpi_pci_root, node);
86                 driver->add(root->device->handle);
87                 n++;
88         }
89
90         return n;
91 }
92
93 EXPORT_SYMBOL(acpi_pci_register_driver);
94
95 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
96 {
97         struct list_head *entry;
98
99         struct acpi_pci_driver **pptr = &sub_driver;
100         while (*pptr) {
101                 if (*pptr == driver)
102                         break;
103                 pptr = &(*pptr)->next;
104         }
105         BUG_ON(!*pptr);
106         *pptr = (*pptr)->next;
107
108         if (!driver->remove)
109                 return;
110
111         list_for_each(entry, &acpi_pci_roots) {
112                 struct acpi_pci_root *root;
113                 root = list_entry(entry, struct acpi_pci_root, node);
114                 driver->remove(root->device->handle);
115         }
116 }
117
118 EXPORT_SYMBOL(acpi_pci_unregister_driver);
119
120 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
121 {
122         struct acpi_pci_root *tmp;
123         
124         list_for_each_entry(tmp, &acpi_pci_roots, node) {
125                 if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
126                         return tmp->device->handle;
127         }
128         return NULL;            
129 }
130
131 EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
132
133 static acpi_status
134 get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
135 {
136         int *busnr = data;
137         struct acpi_resource_address64 address;
138
139         if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
140             resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
141             resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
142                 return AE_OK;
143
144         acpi_resource_to_address64(resource, &address);
145         if ((address.address_length > 0) &&
146             (address.resource_type == ACPI_BUS_NUMBER_RANGE))
147                 *busnr = address.minimum;
148
149         return AE_OK;
150 }
151
152 static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
153 {
154         acpi_status status;
155
156         *busnum = -1;
157         status =
158             acpi_walk_resources(handle, METHOD_NAME__CRS,
159                                 get_root_bridge_busnr_callback, busnum);
160         if (ACPI_FAILURE(status))
161                 return status;
162         /* Check if we really get a bus number from _CRS */
163         if (*busnum == -1)
164                 return AE_ERROR;
165         return AE_OK;
166 }
167
168 static int acpi_pci_root_add(struct acpi_device *device)
169 {
170         int result = 0;
171         struct acpi_pci_root *root = NULL;
172         struct acpi_pci_root *tmp;
173         acpi_status status = AE_OK;
174         unsigned long value = 0;
175         acpi_handle handle = NULL;
176
177
178         if (!device)
179                 return -EINVAL;
180
181         root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
182         if (!root)
183                 return -ENOMEM;
184         INIT_LIST_HEAD(&root->node);
185
186         root->device = device;
187         strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
188         strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
189         acpi_driver_data(device) = root;
190
191         /*
192          * TBD: Doesn't the bus driver automatically set this?
193          */
194         device->ops.bind = acpi_pci_bind;
195
196         /* 
197          * Segment
198          * -------
199          * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
200          */
201         status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
202                                        &value);
203         switch (status) {
204         case AE_OK:
205                 root->id.segment = (u16) value;
206                 break;
207         case AE_NOT_FOUND:
208                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
209                                   "Assuming segment 0 (no _SEG)\n"));
210                 root->id.segment = 0;
211                 break;
212         default:
213                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
214                 result = -ENODEV;
215                 goto end;
216         }
217
218         /* 
219          * Bus
220          * ---
221          * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
222          */
223         status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
224                                        &value);
225         switch (status) {
226         case AE_OK:
227                 root->id.bus = (u16) value;
228                 break;
229         case AE_NOT_FOUND:
230                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
231                 root->id.bus = 0;
232                 break;
233         default:
234                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
235                 result = -ENODEV;
236                 goto end;
237         }
238
239         /* Some systems have wrong _BBN */
240         list_for_each_entry(tmp, &acpi_pci_roots, node) {
241                 if ((tmp->id.segment == root->id.segment)
242                     && (tmp->id.bus == root->id.bus)) {
243                         int bus = 0;
244                         acpi_status status;
245
246                         printk(KERN_ERR PREFIX
247                                     "Wrong _BBN value, reboot"
248                                     " and use option 'pci=noacpi'\n");
249
250                         status = try_get_root_bridge_busnr(device->handle, &bus);
251                         if (ACPI_FAILURE(status))
252                                 break;
253                         if (bus != root->id.bus) {
254                                 printk(KERN_INFO PREFIX
255                                        "PCI _CRS %d overrides _BBN 0\n", bus);
256                                 root->id.bus = bus;
257                         }
258                         break;
259                 }
260         }
261         /*
262          * Device & Function
263          * -----------------
264          * Obtained from _ADR (which has already been evaluated for us).
265          */
266         root->id.device = device->pnp.bus_address >> 16;
267         root->id.function = device->pnp.bus_address & 0xFFFF;
268
269         /*
270          * TBD: Need PCI interface for enumeration/configuration of roots.
271          */
272
273         /* TBD: Locking */
274         list_add_tail(&root->node, &acpi_pci_roots);
275
276         printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
277                acpi_device_name(device), acpi_device_bid(device),
278                root->id.segment, root->id.bus);
279
280         /*
281          * Scan the Root Bridge
282          * --------------------
283          * Must do this prior to any attempt to bind the root device, as the
284          * PCI namespace does not get created until this call is made (and 
285          * thus the root bridge's pci_dev does not exist).
286          */
287         root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
288         if (!root->bus) {
289                 printk(KERN_ERR PREFIX
290                             "Bus %04x:%02x not present in PCI namespace\n",
291                             root->id.segment, root->id.bus);
292                 result = -ENODEV;
293                 goto end;
294         }
295
296         /*
297          * Attach ACPI-PCI Context
298          * -----------------------
299          * Thus binding the ACPI and PCI devices.
300          */
301         result = acpi_pci_bind_root(device, &root->id, root->bus);
302         if (result)
303                 goto end;
304
305         /*
306          * PCI Routing Table
307          * -----------------
308          * Evaluate and parse _PRT, if exists.
309          */
310         status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
311         if (ACPI_SUCCESS(status))
312                 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
313                                               root->id.bus);
314
315       end:
316         if (result) {
317                 if (!list_empty(&root->node))
318                         list_del(&root->node);
319                 kfree(root);
320         }
321
322         return result;
323 }
324
325 static int acpi_pci_root_start(struct acpi_device *device)
326 {
327         struct acpi_pci_root *root;
328
329
330         list_for_each_entry(root, &acpi_pci_roots, node) {
331                 if (root->device == device) {
332                         pci_bus_add_devices(root->bus);
333                         return 0;
334                 }
335         }
336         return -ENODEV;
337 }
338
339 static int acpi_pci_root_remove(struct acpi_device *device, int type)
340 {
341         struct acpi_pci_root *root = NULL;
342
343
344         if (!device || !acpi_driver_data(device))
345                 return -EINVAL;
346
347         root = acpi_driver_data(device);
348
349         kfree(root);
350
351         return 0;
352 }
353
354 static int __init acpi_pci_root_init(void)
355 {
356
357         if (acpi_pci_disabled)
358                 return 0;
359
360         /* DEBUG:
361            acpi_dbg_layer = ACPI_PCI_COMPONENT;
362            acpi_dbg_level = 0xFFFFFFFF;
363          */
364
365         if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
366                 return -ENODEV;
367
368         return 0;
369 }
370
371 subsys_initcall(acpi_pci_root_init);