initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / acpi / pci_root.c
blobdd08b633a9801c22ce3edbb952f775d3b9d95e0f
1 /*
2 * pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
39 #define _COMPONENT ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME ("pci_root")
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"
47 static int acpi_pci_root_add (struct acpi_device *device);
48 static int acpi_pci_root_remove (struct acpi_device *device, int type);
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,
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;
67 static LIST_HEAD(acpi_pci_roots);
69 static struct acpi_pci_driver *sub_driver;
71 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
73 int n = 0;
74 struct list_head *entry;
76 struct acpi_pci_driver **pptr = &sub_driver;
77 while (*pptr)
78 pptr = &(*pptr)->next;
79 *pptr = driver;
81 if (!driver->add)
82 return 0;
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++;
91 return n;
94 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
96 struct list_head *entry;
98 struct acpi_pci_driver **pptr = &sub_driver;
99 while (*pptr) {
100 if (*pptr != driver)
101 continue;
102 *pptr = (*pptr)->next;
103 break;
106 if (!driver->remove)
107 return;
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);
116 static acpi_status
117 get_root_bridge_busnr_callback (struct acpi_resource *resource, void *data)
119 int *busnr = (int *)data;
120 struct acpi_resource_address64 address;
122 if (resource->id != ACPI_RSTYPE_ADDRESS16 &&
123 resource->id != ACPI_RSTYPE_ADDRESS32 &&
124 resource->id != ACPI_RSTYPE_ADDRESS64)
125 return AE_OK;
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;
132 return AE_OK;
135 static acpi_status
136 try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
138 acpi_status status;
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;
150 static int
151 acpi_pci_root_add (
152 struct acpi_device *device)
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;
161 ACPI_FUNCTION_TRACE("acpi_pci_root_add");
163 if (!device)
164 return_VALUE(-EINVAL);
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));
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;
177 * TBD: Doesn't the bus driver automatically set this?
179 device->ops.bind = acpi_pci_bind;
182 * Segment
183 * -------
184 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
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;
204 * Bus
205 * ---
206 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
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;
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;
231 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
232 "Wrong _BBN value, please reboot and using option 'pci=noacpi'\n"));
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;
241 break;
245 * Device & Function
246 * -----------------
247 * Obtained from _ADR (which has already been evaluated for us).
249 root->id.device = device->pnp.bus_address >> 16;
250 root->id.function = device->pnp.bus_address & 0xFFFF;
253 * TBD: Need PCI interface for enumeration/configuration of roots.
256 /* TBD: Locking */
257 list_add_tail(&root->node, &acpi_pci_roots);
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);
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).
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;
280 * Attach ACPI-PCI Context
281 * -----------------------
282 * Thus binding the ACPI and PCI devices.
284 result = acpi_pci_bind_root(device, &root->id, root->bus);
285 if (result)
286 goto end;
289 * PCI Routing Table
290 * -----------------
291 * Evaluate and parse _PRT, if exists.
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);
298 end:
299 if (result)
300 kfree(root);
302 return_VALUE(result);
306 static int
307 acpi_pci_root_remove (
308 struct acpi_device *device,
309 int type)
311 struct acpi_pci_root *root = NULL;
313 ACPI_FUNCTION_TRACE("acpi_pci_root_remove");
315 if (!device || !acpi_driver_data(device))
316 return_VALUE(-EINVAL);
318 root = (struct acpi_pci_root *) acpi_driver_data(device);
320 kfree(root);
322 return_VALUE(0);
326 static int __init acpi_pci_root_init (void)
328 ACPI_FUNCTION_TRACE("acpi_pci_root_init");
330 if (acpi_pci_disabled)
331 return_VALUE(0);
333 /* DEBUG:
334 acpi_dbg_layer = ACPI_PCI_COMPONENT;
335 acpi_dbg_level = 0xFFFFFFFF;
338 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
339 return_VALUE(-ENODEV);
341 return_VALUE(0);
344 subsys_initcall(acpi_pci_root_init);