blob: ad4145a37786d93a974455575a411b080c4aad63 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
Len Brown4be44fc2005-08-05 00:44:28 -040043static int acpi_pci_root_add(struct acpi_device *device);
44static int acpi_pci_root_remove(struct acpi_device *device, int type);
45static int acpi_pci_root_start(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static struct acpi_driver acpi_pci_root_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050048 .name = "pci_root",
Len Brown4be44fc2005-08-05 00:44:28 -040049 .class = ACPI_PCI_ROOT_CLASS,
50 .ids = ACPI_PCI_ROOT_HID,
51 .ops = {
52 .add = acpi_pci_root_add,
53 .remove = acpi_pci_root_remove,
54 .start = acpi_pci_root_start,
55 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58struct acpi_pci_root {
Len Brown4be44fc2005-08-05 00:44:28 -040059 struct list_head node;
Patrick Mochel32917e52006-05-19 16:54:39 -040060 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040061 struct acpi_pci_id id;
62 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65static LIST_HEAD(acpi_pci_roots);
66
67static struct acpi_pci_driver *sub_driver;
68
69int acpi_pci_register_driver(struct acpi_pci_driver *driver)
70{
71 int n = 0;
72 struct list_head *entry;
73
74 struct acpi_pci_driver **pptr = &sub_driver;
75 while (*pptr)
76 pptr = &(*pptr)->next;
77 *pptr = driver;
78
79 if (!driver->add)
80 return 0;
81
82 list_for_each(entry, &acpi_pci_roots) {
83 struct acpi_pci_root *root;
84 root = list_entry(entry, struct acpi_pci_root, node);
Patrick Mochel2d1e0a02006-05-19 16:54:43 -040085 driver->add(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 n++;
87 }
88
89 return n;
90}
Len Brown4be44fc2005-08-05 00:44:28 -040091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092EXPORT_SYMBOL(acpi_pci_register_driver);
93
94void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
95{
96 struct list_head *entry;
97
98 struct acpi_pci_driver **pptr = &sub_driver;
99 while (*pptr) {
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800100 if (*pptr == driver)
101 break;
102 pptr = &(*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800104 BUG_ON(!*pptr);
105 *pptr = (*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
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
Justin Chend91a0072006-12-06 10:17:10 -0700119acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
120{
121 struct acpi_pci_root *tmp;
122
123 list_for_each_entry(tmp, &acpi_pci_roots, node) {
124 if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
125 return tmp->device->handle;
126 }
127 return NULL;
128}
129
130EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400133get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200135 int *busnr = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct acpi_resource_address64 address;
137
Bob Moore50eca3e2005-09-30 19:03:00 -0400138 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
139 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
140 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return AE_OK;
142
143 acpi_resource_to_address64(resource, &address);
Len Brown4be44fc2005-08-05 00:44:28 -0400144 if ((address.address_length > 0) &&
145 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
Bob Moore50eca3e2005-09-30 19:03:00 -0400146 *busnr = address.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return AE_OK;
149}
150
Len Brown4be44fc2005-08-05 00:44:28 -0400151static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 acpi_status status;
154
155 *busnum = -1;
Len Brown4be44fc2005-08-05 00:44:28 -0400156 status =
157 acpi_walk_resources(handle, METHOD_NAME__CRS,
158 get_root_bridge_busnr_callback, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (ACPI_FAILURE(status))
160 return status;
161 /* Check if we really get a bus number from _CRS */
162 if (*busnum == -1)
163 return AE_ERROR;
164 return AE_OK;
165}
166
Rui Zhang2786f6e2006-12-21 02:21:13 -0500167static void acpi_pci_bridge_scan(struct acpi_device *device)
168{
169 int status;
170 struct acpi_device *child = NULL;
171
172 if (device->flags.bus_address)
173 if (device->parent && device->parent->ops.bind) {
174 status = device->parent->ops.bind(device);
175 if (!status) {
176 list_for_each_entry(child, &device->children, node)
177 acpi_pci_bridge_scan(child);
178 }
179 }
180}
181
Len Brown4be44fc2005-08-05 00:44:28 -0400182static int acpi_pci_root_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Len Brown4be44fc2005-08-05 00:44:28 -0400184 int result = 0;
185 struct acpi_pci_root *root = NULL;
186 struct acpi_pci_root *tmp;
187 acpi_status status = AE_OK;
188 unsigned long value = 0;
189 acpi_handle handle = NULL;
Rui Zhang2786f6e2006-12-21 02:21:13 -0500190 struct acpi_device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Burman Yan36bcbec2006-12-19 12:56:11 -0800196 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -0400198 return -ENOMEM;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700199 INIT_LIST_HEAD(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Patrick Mochel32917e52006-05-19 16:54:39 -0400201 root->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
203 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
204 acpi_driver_data(device) = root;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 device->ops.bind = acpi_pci_bind;
207
208 /*
209 * Segment
210 * -------
211 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
212 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400213 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400214 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 switch (status) {
216 case AE_OK:
217 root->id.segment = (u16) value;
218 break;
219 case AE_NOT_FOUND:
Len Brown4be44fc2005-08-05 00:44:28 -0400220 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
221 "Assuming segment 0 (no _SEG)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 root->id.segment = 0;
223 break;
224 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400225 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 result = -ENODEV;
227 goto end;
228 }
229
230 /*
231 * Bus
232 * ---
233 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
234 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400235 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400236 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 switch (status) {
238 case AE_OK:
239 root->id.bus = (u16) value;
240 break;
241 case AE_NOT_FOUND:
242 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
243 root->id.bus = 0;
244 break;
245 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400246 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 result = -ENODEV;
248 goto end;
249 }
250
251 /* Some systems have wrong _BBN */
252 list_for_each_entry(tmp, &acpi_pci_roots, node) {
253 if ((tmp->id.segment == root->id.segment)
Len Brown4be44fc2005-08-05 00:44:28 -0400254 && (tmp->id.bus == root->id.bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int bus = 0;
256 acpi_status status;
257
Len Brown64684632006-06-26 23:41:38 -0400258 printk(KERN_ERR PREFIX
Thomas Renningera6fc6722006-06-26 23:58:43 -0400259 "Wrong _BBN value, reboot"
Len Brown64684632006-06-26 23:41:38 -0400260 " and use option 'pci=noacpi'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400262 status = try_get_root_bridge_busnr(device->handle, &bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (ACPI_FAILURE(status))
264 break;
265 if (bus != root->id.bus) {
Len Brown4be44fc2005-08-05 00:44:28 -0400266 printk(KERN_INFO PREFIX
267 "PCI _CRS %d overrides _BBN 0\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 root->id.bus = bus;
269 }
270 break;
271 }
272 }
273 /*
274 * Device & Function
275 * -----------------
276 * Obtained from _ADR (which has already been evaluated for us).
277 */
278 root->id.device = device->pnp.bus_address >> 16;
279 root->id.function = device->pnp.bus_address & 0xFFFF;
280
281 /*
282 * TBD: Need PCI interface for enumeration/configuration of roots.
283 */
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285 /* TBD: Locking */
286 list_add_tail(&root->node, &acpi_pci_roots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Len Brown4be44fc2005-08-05 00:44:28 -0400288 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
289 acpi_device_name(device), acpi_device_bid(device),
290 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /*
293 * Scan the Root Bridge
294 * --------------------
295 * Must do this prior to any attempt to bind the root device, as the
296 * PCI namespace does not get created until this call is made (and
297 * thus the root bridge's pci_dev does not exist).
298 */
299 root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
300 if (!root->bus) {
Len Brown64684632006-06-26 23:41:38 -0400301 printk(KERN_ERR PREFIX
302 "Bus %04x:%02x not present in PCI namespace\n",
303 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 result = -ENODEV;
305 goto end;
306 }
307
308 /*
309 * Attach ACPI-PCI Context
310 * -----------------------
311 * Thus binding the ACPI and PCI devices.
312 */
313 result = acpi_pci_bind_root(device, &root->id, root->bus);
314 if (result)
315 goto end;
316
317 /*
318 * PCI Routing Table
319 * -----------------
320 * Evaluate and parse _PRT, if exists.
321 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400322 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (ACPI_SUCCESS(status))
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400324 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400325 root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Rui Zhang2786f6e2006-12-21 02:21:13 -0500327 /*
328 * Scan and bind all _ADR-Based Devices
329 */
330 list_for_each_entry(child, &device->children, node)
331 acpi_pci_bridge_scan(child);
332
Len Brown4be44fc2005-08-05 00:44:28 -0400333 end:
Rajesh Shahc431ada2005-04-28 00:25:45 -0700334 if (result) {
335 if (!list_empty(&root->node))
336 list_del(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 kfree(root);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Patrick Mocheld550d982006-06-27 00:41:40 -0400340 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Len Brown4be44fc2005-08-05 00:44:28 -0400343static int acpi_pci_root_start(struct acpi_device *device)
Rajesh Shahc431ada2005-04-28 00:25:45 -0700344{
Len Brown4be44fc2005-08-05 00:44:28 -0400345 struct acpi_pci_root *root;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700346
Rajesh Shahc431ada2005-04-28 00:25:45 -0700347
348 list_for_each_entry(root, &acpi_pci_roots, node) {
Patrick Mochel432bfab2006-05-19 16:54:51 -0400349 if (root->device == device) {
Rajesh Shahc431ada2005-04-28 00:25:45 -0700350 pci_bus_add_devices(root->bus);
Patrick Mocheld550d982006-06-27 00:41:40 -0400351 return 0;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700352 }
353 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400354 return -ENODEV;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Len Brown4be44fc2005-08-05 00:44:28 -0400357static int acpi_pci_root_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Len Brown4be44fc2005-08-05 00:44:28 -0400359 struct acpi_pci_root *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200365 root = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 kfree(root);
368
Patrick Mocheld550d982006-06-27 00:41:40 -0400369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Len Brown4be44fc2005-08-05 00:44:28 -0400372static int __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 if (acpi_pci_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* DEBUG:
Len Brown4be44fc2005-08-05 00:44:28 -0400379 acpi_dbg_layer = ACPI_PCI_COMPONENT;
380 acpi_dbg_level = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
382
383 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400384 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Patrick Mocheld550d982006-06-27 00:41:40 -0400386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389subsys_initcall(acpi_pci_root_init);