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