ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 int
117 acpi_pci_root_add (
118         struct acpi_device      *device)
119 {
120         int                     result = 0;
121         struct acpi_pci_root    *root = NULL;
122         struct acpi_pci_root    *tmp;
123         acpi_status             status = AE_OK;
124         unsigned long           value = 0;
125         acpi_handle             handle = NULL;
126
127         ACPI_FUNCTION_TRACE("acpi_pci_root_add");
128
129         if (!device)
130                 return_VALUE(-EINVAL);
131
132         root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
133         if (!root)
134                 return_VALUE(-ENOMEM);
135         memset(root, 0, sizeof(struct acpi_pci_root));
136
137         root->handle = device->handle;
138         strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
139         strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
140         acpi_driver_data(device) = root;
141
142         /*
143          * TBD: Doesn't the bus driver automatically set this?
144          */
145         device->ops.bind = acpi_pci_bind;
146
147         /* 
148          * Segment
149          * -------
150          * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
151          */
152         status = acpi_evaluate_integer(root->handle, METHOD_NAME__SEG, NULL, 
153                 &value);
154         switch (status) {
155         case AE_OK:
156                 root->id.segment = (u16) value;
157                 break;
158         case AE_NOT_FOUND:
159                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
160                         "Assuming segment 0 (no _SEG)\n"));
161                 root->id.segment = 0;
162                 break;
163         default:
164                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SEG\n"));
165                 result = -ENODEV;
166                 goto end;
167         }
168
169         /* 
170          * Bus
171          * ---
172          * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
173          */
174         status = acpi_evaluate_integer(root->handle, METHOD_NAME__BBN, NULL, 
175                 &value);
176         switch (status) {
177         case AE_OK:
178                 root->id.bus = (u16) value;
179                 break;
180         case AE_NOT_FOUND:
181                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
182                 root->id.bus = 0;
183                 break;
184         default:
185                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BBN\n"));
186                 result = -ENODEV;
187                 goto end;
188         }
189
190         /* Some systems have wrong _BBN */
191         list_for_each_entry(tmp, &acpi_pci_roots, node) {
192                 if ((tmp->id.segment == root->id.segment)
193                                 && (tmp->id.bus == root->id.bus))
194                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
195                                 "Wrong _BBN value, please reboot and using option 'pci=noacpi'\n"));
196         }
197         /*
198          * Device & Function
199          * -----------------
200          * Obtained from _ADR (which has already been evaluated for us).
201          */
202         root->id.device = device->pnp.bus_address >> 16;
203         root->id.function = device->pnp.bus_address & 0xFFFF;
204
205         /*
206          * TBD: Need PCI interface for enumeration/configuration of roots.
207          */
208
209         /* TBD: Locking */
210         list_add_tail(&root->node, &acpi_pci_roots);
211
212         printk(KERN_INFO PREFIX "%s [%s] (%02x:%02x)\n", 
213                 acpi_device_name(device), acpi_device_bid(device),
214                 root->id.segment, root->id.bus);
215
216         /*
217          * Scan the Root Bridge
218          * --------------------
219          * Must do this prior to any attempt to bind the root device, as the
220          * PCI namespace does not get created until this call is made (and 
221          * thus the root bridge's pci_dev does not exist).
222          */
223         root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
224         if (!root->bus) {
225                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
226                         "Bus %02x:%02x not present in PCI namespace\n", 
227                         root->id.segment, root->id.bus));
228                 result = -ENODEV;
229                 goto end;
230         }
231
232         /*
233          * Attach ACPI-PCI Context
234          * -----------------------
235          * Thus binding the ACPI and PCI devices.
236          */
237         result = acpi_pci_bind_root(device, &root->id, root->bus);
238         if (result)
239                 goto end;
240
241         /*
242          * PCI Routing Table
243          * -----------------
244          * Evaluate and parse _PRT, if exists.
245          */
246         status = acpi_get_handle(root->handle, METHOD_NAME__PRT, &handle);
247         if (ACPI_SUCCESS(status))
248                 result = acpi_pci_irq_add_prt(root->handle, root->id.segment,
249                         root->id.bus);
250
251 end:
252         if (result)
253                 kfree(root);
254
255         return_VALUE(result);
256 }
257
258
259 static int
260 acpi_pci_root_remove (
261         struct acpi_device      *device,
262         int                     type)
263 {
264         struct acpi_pci_root    *root = NULL;
265
266         ACPI_FUNCTION_TRACE("acpi_pci_root_remove");
267
268         if (!device || !acpi_driver_data(device))
269                 return_VALUE(-EINVAL);
270
271         root = (struct acpi_pci_root *) acpi_driver_data(device);
272
273         kfree(root);
274
275         return_VALUE(0);
276 }
277
278
279 static int __init acpi_pci_root_init (void)
280 {
281         ACPI_FUNCTION_TRACE("acpi_pci_root_init");
282
283         if (acpi_pci_disabled)
284                 return_VALUE(0);
285
286         /* DEBUG:
287         acpi_dbg_layer = ACPI_PCI_COMPONENT;
288         acpi_dbg_level = 0xFFFFFFFF;
289          */
290
291         if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
292                 return_VALUE(-ENODEV);
293
294         return_VALUE(0);
295 }
296
297 subsys_initcall(acpi_pci_root_init);
298