blob: 7bf24c39cebfa58e95c5e3ae5860c4985a02cb9b [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 Brownf52fd662007-02-12 22:42:12 -050039ACPI_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 Brownc2b67052007-02-12 23:33:40 -050049 .name = "pci_root",
Len Brown4be44fc2005-08-05 00:44:28 -040050 .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) {
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800101 if (*pptr == driver)
102 break;
103 pptr = &(*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800105 BUG_ON(!*pptr);
106 *pptr = (*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
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);
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400114 driver->remove(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116}
Len Brown4be44fc2005-08-05 00:44:28 -0400117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118EXPORT_SYMBOL(acpi_pci_unregister_driver);
119
Justin Chend91a0072006-12-06 10:17:10 -0700120acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
121{
122 struct acpi_pci_root *tmp;
123
124 list_for_each_entry(tmp, &acpi_pci_roots, node) {
125 if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
126 return tmp->device->handle;
127 }
128 return NULL;
129}
130
131EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400134get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200136 int *busnr = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct acpi_resource_address64 address;
138
Bob Moore50eca3e2005-09-30 19:03:00 -0400139 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
140 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
141 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return AE_OK;
143
144 acpi_resource_to_address64(resource, &address);
Len Brown4be44fc2005-08-05 00:44:28 -0400145 if ((address.address_length > 0) &&
146 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
Bob Moore50eca3e2005-09-30 19:03:00 -0400147 *busnr = address.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return AE_OK;
150}
151
Len Brown4be44fc2005-08-05 00:44:28 -0400152static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 acpi_status status;
155
156 *busnum = -1;
Len Brown4be44fc2005-08-05 00:44:28 -0400157 status =
158 acpi_walk_resources(handle, METHOD_NAME__CRS,
159 get_root_bridge_busnr_callback, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (ACPI_FAILURE(status))
161 return status;
162 /* Check if we really get a bus number from _CRS */
163 if (*busnum == -1)
164 return AE_ERROR;
165 return AE_OK;
166}
167
Rui Zhang2786f6e2006-12-21 02:21:13 -0500168static void acpi_pci_bridge_scan(struct acpi_device *device)
169{
170 int status;
171 struct acpi_device *child = NULL;
172
173 if (device->flags.bus_address)
174 if (device->parent && device->parent->ops.bind) {
175 status = device->parent->ops.bind(device);
176 if (!status) {
177 list_for_each_entry(child, &device->children, node)
178 acpi_pci_bridge_scan(child);
179 }
180 }
181}
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183static int acpi_pci_root_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Len Brown4be44fc2005-08-05 00:44:28 -0400185 int result = 0;
186 struct acpi_pci_root *root = NULL;
187 struct acpi_pci_root *tmp;
188 acpi_status status = AE_OK;
189 unsigned long value = 0;
190 acpi_handle handle = NULL;
Rui Zhang2786f6e2006-12-21 02:21:13 -0500191 struct acpi_device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Burman Yan36bcbec2006-12-19 12:56:11 -0800197 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -0400199 return -ENOMEM;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700200 INIT_LIST_HEAD(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Patrick Mochel32917e52006-05-19 16:54:39 -0400202 root->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
204 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
205 acpi_driver_data(device) = root;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 device->ops.bind = acpi_pci_bind;
208
209 /*
210 * Segment
211 * -------
212 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
213 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400214 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400215 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 switch (status) {
217 case AE_OK:
218 root->id.segment = (u16) value;
219 break;
220 case AE_NOT_FOUND:
Len Brown4be44fc2005-08-05 00:44:28 -0400221 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
222 "Assuming segment 0 (no _SEG)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 root->id.segment = 0;
224 break;
225 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400226 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 result = -ENODEV;
228 goto end;
229 }
230
231 /*
232 * Bus
233 * ---
234 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
235 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400236 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400237 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 switch (status) {
239 case AE_OK:
240 root->id.bus = (u16) value;
241 break;
242 case AE_NOT_FOUND:
243 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
244 root->id.bus = 0;
245 break;
246 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400247 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 result = -ENODEV;
249 goto end;
250 }
251
252 /* Some systems have wrong _BBN */
253 list_for_each_entry(tmp, &acpi_pci_roots, node) {
254 if ((tmp->id.segment == root->id.segment)
Len Brown4be44fc2005-08-05 00:44:28 -0400255 && (tmp->id.bus == root->id.bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int bus = 0;
257 acpi_status status;
258
Len Brown64684632006-06-26 23:41:38 -0400259 printk(KERN_ERR PREFIX
Thomas Renningera6fc6722006-06-26 23:58:43 -0400260 "Wrong _BBN value, reboot"
Len Brown64684632006-06-26 23:41:38 -0400261 " and use option 'pci=noacpi'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400263 status = try_get_root_bridge_busnr(device->handle, &bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (ACPI_FAILURE(status))
265 break;
266 if (bus != root->id.bus) {
Len Brown4be44fc2005-08-05 00:44:28 -0400267 printk(KERN_INFO PREFIX
268 "PCI _CRS %d overrides _BBN 0\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 root->id.bus = bus;
270 }
271 break;
272 }
273 }
274 /*
275 * Device & Function
276 * -----------------
277 * Obtained from _ADR (which has already been evaluated for us).
278 */
279 root->id.device = device->pnp.bus_address >> 16;
280 root->id.function = device->pnp.bus_address & 0xFFFF;
281
282 /*
283 * TBD: Need PCI interface for enumeration/configuration of roots.
284 */
285
Len Brown4be44fc2005-08-05 00:44:28 -0400286 /* TBD: Locking */
287 list_add_tail(&root->node, &acpi_pci_roots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Len Brown4be44fc2005-08-05 00:44:28 -0400289 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
290 acpi_device_name(device), acpi_device_bid(device),
291 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /*
294 * Scan the Root Bridge
295 * --------------------
296 * Must do this prior to any attempt to bind the root device, as the
297 * PCI namespace does not get created until this call is made (and
298 * thus the root bridge's pci_dev does not exist).
299 */
300 root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
301 if (!root->bus) {
Len Brown64684632006-06-26 23:41:38 -0400302 printk(KERN_ERR PREFIX
303 "Bus %04x:%02x not present in PCI namespace\n",
304 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 result = -ENODEV;
306 goto end;
307 }
308
309 /*
310 * Attach ACPI-PCI Context
311 * -----------------------
312 * Thus binding the ACPI and PCI devices.
313 */
314 result = acpi_pci_bind_root(device, &root->id, root->bus);
315 if (result)
316 goto end;
317
318 /*
319 * PCI Routing Table
320 * -----------------
321 * Evaluate and parse _PRT, if exists.
322 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400323 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (ACPI_SUCCESS(status))
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400325 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400326 root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Rui Zhang2786f6e2006-12-21 02:21:13 -0500328 /*
329 * Scan and bind all _ADR-Based Devices
330 */
331 list_for_each_entry(child, &device->children, node)
332 acpi_pci_bridge_scan(child);
333
Len Brown4be44fc2005-08-05 00:44:28 -0400334 end:
Rajesh Shahc431ada2005-04-28 00:25:45 -0700335 if (result) {
336 if (!list_empty(&root->node))
337 list_del(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 kfree(root);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Patrick Mocheld550d982006-06-27 00:41:40 -0400341 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Len Brown4be44fc2005-08-05 00:44:28 -0400344static int acpi_pci_root_start(struct acpi_device *device)
Rajesh Shahc431ada2005-04-28 00:25:45 -0700345{
Len Brown4be44fc2005-08-05 00:44:28 -0400346 struct acpi_pci_root *root;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700347
Rajesh Shahc431ada2005-04-28 00:25:45 -0700348
349 list_for_each_entry(root, &acpi_pci_roots, node) {
Patrick Mochel432bfab2006-05-19 16:54:51 -0400350 if (root->device == device) {
Rajesh Shahc431ada2005-04-28 00:25:45 -0700351 pci_bus_add_devices(root->bus);
Patrick Mocheld550d982006-06-27 00:41:40 -0400352 return 0;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700353 }
354 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400355 return -ENODEV;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Len Brown4be44fc2005-08-05 00:44:28 -0400358static int acpi_pci_root_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Len Brown4be44fc2005-08-05 00:44:28 -0400360 struct acpi_pci_root *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200366 root = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 kfree(root);
369
Patrick Mocheld550d982006-06-27 00:41:40 -0400370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Len Brown4be44fc2005-08-05 00:44:28 -0400373static int __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (acpi_pci_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* DEBUG:
Len Brown4be44fc2005-08-05 00:44:28 -0400380 acpi_dbg_layer = ACPI_PCI_COMPONENT;
381 acpi_dbg_level = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
383
384 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400385 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Patrick Mocheld550d982006-06-27 00:41:40 -0400387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390subsys_initcall(acpi_pci_root_init);