blob: 2e1a74a967bb114d288a0216d8e80c30d54ecf75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define _COMPONENT ACPI_PCI_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040039ACPI_MODULE_NAME("pci_root")
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#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"
Len Brown4be44fc2005-08-05 00:44:28 -040044static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static struct acpi_driver acpi_pci_root_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -040049 .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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59struct acpi_pci_root {
Len Brown4be44fc2005-08-05 00:44:28 -040060 struct list_head node;
Patrick Mochel32917e52006-05-19 16:54:39 -040061 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040062 struct acpi_pci_id id;
63 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
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);
Patrick Mochel2d1e0a02006-05-19 16:54:43 -040086 driver->add(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 n++;
88 }
89
90 return n;
91}
Len Brown4be44fc2005-08-05 00:44:28 -040092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093EXPORT_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) {
101 if (*pptr != driver)
102 continue;
103 *pptr = (*pptr)->next;
104 break;
105 }
106
107 if (!driver->remove)
108 return;
109
110 list_for_each(entry, &acpi_pci_roots) {
111 struct acpi_pci_root *root;
112 root = list_entry(entry, struct acpi_pci_root, node);
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400113 driver->remove(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115}
Len Brown4be44fc2005-08-05 00:44:28 -0400116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117EXPORT_SYMBOL(acpi_pci_unregister_driver);
118
119static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400120get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 int *busnr = (int *)data;
123 struct acpi_resource_address64 address;
124
Bob Moore50eca3e2005-09-30 19:03:00 -0400125 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
126 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
127 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return AE_OK;
129
130 acpi_resource_to_address64(resource, &address);
Len Brown4be44fc2005-08-05 00:44:28 -0400131 if ((address.address_length > 0) &&
132 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
Bob Moore50eca3e2005-09-30 19:03:00 -0400133 *busnr = address.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 return AE_OK;
136}
137
Len Brown4be44fc2005-08-05 00:44:28 -0400138static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 acpi_status status;
141
142 *busnum = -1;
Len Brown4be44fc2005-08-05 00:44:28 -0400143 status =
144 acpi_walk_resources(handle, METHOD_NAME__CRS,
145 get_root_bridge_busnr_callback, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (ACPI_FAILURE(status))
147 return status;
148 /* Check if we really get a bus number from _CRS */
149 if (*busnum == -1)
150 return AE_ERROR;
151 return AE_OK;
152}
153
Rui Zhang2786f6e2006-12-21 02:21:13 -0500154static void acpi_pci_bridge_scan(struct acpi_device *device)
155{
156 int status;
157 struct acpi_device *child = NULL;
158
159 if (device->flags.bus_address)
160 if (device->parent && device->parent->ops.bind) {
161 status = device->parent->ops.bind(device);
162 if (!status) {
163 list_for_each_entry(child, &device->children, node)
164 acpi_pci_bridge_scan(child);
165 }
166 }
167}
168
Len Brown4be44fc2005-08-05 00:44:28 -0400169static int acpi_pci_root_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Len Brown4be44fc2005-08-05 00:44:28 -0400171 int result = 0;
172 struct acpi_pci_root *root = NULL;
173 struct acpi_pci_root *tmp;
174 acpi_status status = AE_OK;
175 unsigned long value = 0;
176 acpi_handle handle = NULL;
Rui Zhang2786f6e2006-12-21 02:21:13 -0500177 struct acpi_device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400181 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
184 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -0400185 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 memset(root, 0, sizeof(struct acpi_pci_root));
Rajesh Shahc431ada2005-04-28 00:25:45 -0700187 INIT_LIST_HEAD(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Patrick Mochel32917e52006-05-19 16:54:39 -0400189 root->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
191 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
192 acpi_driver_data(device) = root;
193
Rui Zhang2786f6e2006-12-21 02:21:13 -0500194 device->ops.bind = acpi_pci_bind;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * Segment
198 * -------
199 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
200 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400201 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400202 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 switch (status) {
204 case AE_OK:
205 root->id.segment = (u16) value;
206 break;
207 case AE_NOT_FOUND:
Len Brown4be44fc2005-08-05 00:44:28 -0400208 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
209 "Assuming segment 0 (no _SEG)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 root->id.segment = 0;
211 break;
212 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400213 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 result = -ENODEV;
215 goto end;
216 }
217
218 /*
219 * Bus
220 * ---
221 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
222 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400223 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400224 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 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:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400234 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 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)
Len Brown4be44fc2005-08-05 00:44:28 -0400242 && (tmp->id.bus == root->id.bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int bus = 0;
244 acpi_status status;
245
Len Brown64684632006-06-26 23:41:38 -0400246 printk(KERN_ERR PREFIX
Thomas Renningera6fc6722006-06-26 23:58:43 -0400247 "Wrong _BBN value, reboot"
Len Brown64684632006-06-26 23:41:38 -0400248 " and use option 'pci=noacpi'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400250 status = try_get_root_bridge_busnr(device->handle, &bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (ACPI_FAILURE(status))
252 break;
253 if (bus != root->id.bus) {
Len Brown4be44fc2005-08-05 00:44:28 -0400254 printk(KERN_INFO PREFIX
255 "PCI _CRS %d overrides _BBN 0\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 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
Len Brown4be44fc2005-08-05 00:44:28 -0400273 /* TBD: Locking */
274 list_add_tail(&root->node, &acpi_pci_roots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Len Brown4be44fc2005-08-05 00:44:28 -0400276 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
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) {
Len Brown64684632006-06-26 23:41:38 -0400289 printk(KERN_ERR PREFIX
290 "Bus %04x:%02x not present in PCI namespace\n",
291 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 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 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400310 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (ACPI_SUCCESS(status))
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400312 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400313 root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Rui Zhang2786f6e2006-12-21 02:21:13 -0500315 /*
316 * Scan and bind all _ADR-Based Devices
317 */
318 list_for_each_entry(child, &device->children, node)
319 acpi_pci_bridge_scan(child);
320
Len Brown4be44fc2005-08-05 00:44:28 -0400321 end:
Rajesh Shahc431ada2005-04-28 00:25:45 -0700322 if (result) {
323 if (!list_empty(&root->node))
324 list_del(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 kfree(root);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Patrick Mocheld550d982006-06-27 00:41:40 -0400328 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Len Brown4be44fc2005-08-05 00:44:28 -0400331static int acpi_pci_root_start(struct acpi_device *device)
Rajesh Shahc431ada2005-04-28 00:25:45 -0700332{
Len Brown4be44fc2005-08-05 00:44:28 -0400333 struct acpi_pci_root *root;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700334
Rajesh Shahc431ada2005-04-28 00:25:45 -0700335
336 list_for_each_entry(root, &acpi_pci_roots, node) {
Patrick Mochel432bfab2006-05-19 16:54:51 -0400337 if (root->device == device) {
Rajesh Shahc431ada2005-04-28 00:25:45 -0700338 pci_bus_add_devices(root->bus);
Patrick Mocheld550d982006-06-27 00:41:40 -0400339 return 0;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700340 }
341 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400342 return -ENODEV;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700343}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Len Brown4be44fc2005-08-05 00:44:28 -0400345static int acpi_pci_root_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Len Brown4be44fc2005-08-05 00:44:28 -0400347 struct acpi_pci_root *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Len Brown4be44fc2005-08-05 00:44:28 -0400353 root = (struct acpi_pci_root *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 kfree(root);
356
Patrick Mocheld550d982006-06-27 00:41:40 -0400357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Len Brown4be44fc2005-08-05 00:44:28 -0400360static int __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (acpi_pci_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* DEBUG:
Len Brown4be44fc2005-08-05 00:44:28 -0400367 acpi_dbg_layer = ACPI_PCI_COMPONENT;
368 acpi_dbg_level = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
370
371 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400372 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Patrick Mocheld550d982006-06-27 00:41:40 -0400374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377subsys_initcall(acpi_pci_root_init);