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