blob: 642554b1b60c4918f018890f16bdccd68a31b629 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
Len Brown4be44fc2005-08-05 00:44:28 -040042static int acpi_pci_root_add(struct acpi_device *device);
43static int acpi_pci_root_remove(struct acpi_device *device, int type);
44static int acpi_pci_root_start(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Thomas Renninger1ba90e32007-07-23 14:44:41 +020046static struct acpi_device_id root_device_ids[] = {
47 {"PNP0A03", 0},
48 {"", 0},
49};
50MODULE_DEVICE_TABLE(acpi, root_device_ids);
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static struct acpi_driver acpi_pci_root_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050053 .name = "pci_root",
Len Brown4be44fc2005-08-05 00:44:28 -040054 .class = ACPI_PCI_ROOT_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020055 .ids = root_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040056 .ops = {
57 .add = acpi_pci_root_add,
58 .remove = acpi_pci_root_remove,
59 .start = acpi_pci_root_start,
60 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63struct acpi_pci_root {
Len Brown4be44fc2005-08-05 00:44:28 -040064 struct list_head node;
Patrick Mochel32917e52006-05-19 16:54:39 -040065 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040066 struct acpi_pci_id id;
67 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70static LIST_HEAD(acpi_pci_roots);
71
72static struct acpi_pci_driver *sub_driver;
73
74int acpi_pci_register_driver(struct acpi_pci_driver *driver)
75{
76 int n = 0;
77 struct list_head *entry;
78
79 struct acpi_pci_driver **pptr = &sub_driver;
80 while (*pptr)
81 pptr = &(*pptr)->next;
82 *pptr = driver;
83
84 if (!driver->add)
85 return 0;
86
87 list_for_each(entry, &acpi_pci_roots) {
88 struct acpi_pci_root *root;
89 root = list_entry(entry, struct acpi_pci_root, node);
Patrick Mochel2d1e0a02006-05-19 16:54:43 -040090 driver->add(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 n++;
92 }
93
94 return n;
95}
Len Brown4be44fc2005-08-05 00:44:28 -040096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097EXPORT_SYMBOL(acpi_pci_register_driver);
98
99void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
100{
101 struct list_head *entry;
102
103 struct acpi_pci_driver **pptr = &sub_driver;
104 while (*pptr) {
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800105 if (*pptr == driver)
106 break;
107 pptr = &(*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800109 BUG_ON(!*pptr);
110 *pptr = (*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 if (!driver->remove)
113 return;
114
115 list_for_each(entry, &acpi_pci_roots) {
116 struct acpi_pci_root *root;
117 root = list_entry(entry, struct acpi_pci_root, node);
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400118 driver->remove(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120}
Len Brown4be44fc2005-08-05 00:44:28 -0400121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122EXPORT_SYMBOL(acpi_pci_unregister_driver);
123
Justin Chend91a0072006-12-06 10:17:10 -0700124acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
125{
126 struct acpi_pci_root *tmp;
127
128 list_for_each_entry(tmp, &acpi_pci_roots, node) {
129 if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
130 return tmp->device->handle;
131 }
132 return NULL;
133}
134
135EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400138get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200140 int *busnr = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct acpi_resource_address64 address;
142
Bob Moore50eca3e2005-09-30 19:03:00 -0400143 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
144 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
145 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return AE_OK;
147
148 acpi_resource_to_address64(resource, &address);
Len Brown4be44fc2005-08-05 00:44:28 -0400149 if ((address.address_length > 0) &&
150 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
Bob Moore50eca3e2005-09-30 19:03:00 -0400151 *busnr = address.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 return AE_OK;
154}
155
Len Brown4be44fc2005-08-05 00:44:28 -0400156static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 acpi_status status;
159
160 *busnum = -1;
Len Brown4be44fc2005-08-05 00:44:28 -0400161 status =
162 acpi_walk_resources(handle, METHOD_NAME__CRS,
163 get_root_bridge_busnr_callback, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ACPI_FAILURE(status))
165 return status;
166 /* Check if we really get a bus number from _CRS */
167 if (*busnum == -1)
168 return AE_ERROR;
169 return AE_OK;
170}
171
Rui Zhang2786f6e2006-12-21 02:21:13 -0500172static void acpi_pci_bridge_scan(struct acpi_device *device)
173{
174 int status;
175 struct acpi_device *child = NULL;
176
177 if (device->flags.bus_address)
178 if (device->parent && device->parent->ops.bind) {
179 status = device->parent->ops.bind(device);
180 if (!status) {
181 list_for_each_entry(child, &device->children, node)
182 acpi_pci_bridge_scan(child);
183 }
184 }
185}
186
Sam Ravnborgb5678a32008-02-17 13:23:03 +0100187static int __devinit acpi_pci_root_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Len Brown4be44fc2005-08-05 00:44:28 -0400189 int result = 0;
190 struct acpi_pci_root *root = NULL;
191 struct acpi_pci_root *tmp;
192 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400193 unsigned long long value = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400194 acpi_handle handle = NULL;
Rui Zhang2786f6e2006-12-21 02:21:13 -0500195 struct acpi_device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400199 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Burman Yan36bcbec2006-12-19 12:56:11 -0800201 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -0400203 return -ENOMEM;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700204 INIT_LIST_HEAD(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Patrick Mochel32917e52006-05-19 16:54:39 -0400206 root->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
208 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700209 device->driver_data = root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 device->ops.bind = acpi_pci_bind;
212
213 /*
214 * Segment
215 * -------
216 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
217 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400218 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400219 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 switch (status) {
221 case AE_OK:
222 root->id.segment = (u16) value;
223 break;
224 case AE_NOT_FOUND:
Len Brown4be44fc2005-08-05 00:44:28 -0400225 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
226 "Assuming segment 0 (no _SEG)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 root->id.segment = 0;
228 break;
229 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400230 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 result = -ENODEV;
232 goto end;
233 }
234
235 /*
236 * Bus
237 * ---
238 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
239 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400240 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400241 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 switch (status) {
243 case AE_OK:
244 root->id.bus = (u16) value;
245 break;
246 case AE_NOT_FOUND:
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
248 root->id.bus = 0;
249 break;
250 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400251 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 result = -ENODEV;
253 goto end;
254 }
255
256 /* Some systems have wrong _BBN */
257 list_for_each_entry(tmp, &acpi_pci_roots, node) {
258 if ((tmp->id.segment == root->id.segment)
Len Brown4be44fc2005-08-05 00:44:28 -0400259 && (tmp->id.bus == root->id.bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int bus = 0;
261 acpi_status status;
262
Len Brown64684632006-06-26 23:41:38 -0400263 printk(KERN_ERR PREFIX
Thomas Renningera6fc6722006-06-26 23:58:43 -0400264 "Wrong _BBN value, reboot"
Len Brown64684632006-06-26 23:41:38 -0400265 " and use option 'pci=noacpi'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400267 status = try_get_root_bridge_busnr(device->handle, &bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (ACPI_FAILURE(status))
269 break;
270 if (bus != root->id.bus) {
Len Brown4be44fc2005-08-05 00:44:28 -0400271 printk(KERN_INFO PREFIX
272 "PCI _CRS %d overrides _BBN 0\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 root->id.bus = bus;
274 }
275 break;
276 }
277 }
278 /*
279 * Device & Function
280 * -----------------
281 * Obtained from _ADR (which has already been evaluated for us).
282 */
283 root->id.device = device->pnp.bus_address >> 16;
284 root->id.function = device->pnp.bus_address & 0xFFFF;
285
286 /*
287 * TBD: Need PCI interface for enumeration/configuration of roots.
288 */
289
Len Brown4be44fc2005-08-05 00:44:28 -0400290 /* TBD: Locking */
291 list_add_tail(&root->node, &acpi_pci_roots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Len Brown4be44fc2005-08-05 00:44:28 -0400293 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
294 acpi_device_name(device), acpi_device_bid(device),
295 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 /*
298 * Scan the Root Bridge
299 * --------------------
300 * Must do this prior to any attempt to bind the root device, as the
301 * PCI namespace does not get created until this call is made (and
302 * thus the root bridge's pci_dev does not exist).
303 */
304 root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
305 if (!root->bus) {
Len Brown64684632006-06-26 23:41:38 -0400306 printk(KERN_ERR PREFIX
307 "Bus %04x:%02x not present in PCI namespace\n",
308 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 result = -ENODEV;
310 goto end;
311 }
312
313 /*
314 * Attach ACPI-PCI Context
315 * -----------------------
316 * Thus binding the ACPI and PCI devices.
317 */
318 result = acpi_pci_bind_root(device, &root->id, root->bus);
319 if (result)
320 goto end;
321
322 /*
323 * PCI Routing Table
324 * -----------------
325 * Evaluate and parse _PRT, if exists.
326 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400327 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (ACPI_SUCCESS(status))
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400329 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400330 root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Rui Zhang2786f6e2006-12-21 02:21:13 -0500332 /*
333 * Scan and bind all _ADR-Based Devices
334 */
335 list_for_each_entry(child, &device->children, node)
336 acpi_pci_bridge_scan(child);
337
Len Brown4be44fc2005-08-05 00:44:28 -0400338 end:
Rajesh Shahc431ada2005-04-28 00:25:45 -0700339 if (result) {
340 if (!list_empty(&root->node))
341 list_del(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 kfree(root);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Patrick Mocheld550d982006-06-27 00:41:40 -0400345 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348static int acpi_pci_root_start(struct acpi_device *device)
Rajesh Shahc431ada2005-04-28 00:25:45 -0700349{
Len Brown4be44fc2005-08-05 00:44:28 -0400350 struct acpi_pci_root *root;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700351
Rajesh Shahc431ada2005-04-28 00:25:45 -0700352
353 list_for_each_entry(root, &acpi_pci_roots, node) {
Patrick Mochel432bfab2006-05-19 16:54:51 -0400354 if (root->device == device) {
Rajesh Shahc431ada2005-04-28 00:25:45 -0700355 pci_bus_add_devices(root->bus);
Patrick Mocheld550d982006-06-27 00:41:40 -0400356 return 0;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700357 }
358 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400359 return -ENODEV;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Len Brown4be44fc2005-08-05 00:44:28 -0400362static int acpi_pci_root_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Len Brown4be44fc2005-08-05 00:44:28 -0400364 struct acpi_pci_root *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400368 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200370 root = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 kfree(root);
373
Patrick Mocheld550d982006-06-27 00:41:40 -0400374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Len Brown4be44fc2005-08-05 00:44:28 -0400377static int __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (acpi_pci_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400383 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Patrick Mocheld550d982006-06-27 00:41:40 -0400385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388subsys_initcall(acpi_pci_root_init);