blob: de4d57114fe45712a9e493ada6cc25aeec25d219 [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>
Andrew Patterson990a7ac2008-11-10 15:30:45 -070034#include <linux/pci-acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/acpi.h>
36#include <acpi/acpi_bus.h>
37#include <acpi/acpi_drivers.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050040ACPI_MODULE_NAME("pci_root");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define ACPI_PCI_ROOT_CLASS "pci_bridge"
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
Thomas Renninger1ba90e32007-07-23 14:44:41 +020047static struct acpi_device_id root_device_ids[] = {
48 {"PNP0A03", 0},
49 {"", 0},
50};
51MODULE_DEVICE_TABLE(acpi, root_device_ids);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct acpi_driver acpi_pci_root_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050054 .name = "pci_root",
Len Brown4be44fc2005-08-05 00:44:28 -040055 .class = ACPI_PCI_ROOT_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020056 .ids = root_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040057 .ops = {
58 .add = acpi_pci_root_add,
59 .remove = acpi_pci_root_remove,
60 .start = acpi_pci_root_start,
61 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64struct acpi_pci_root {
Len Brown4be44fc2005-08-05 00:44:28 -040065 struct list_head node;
Patrick Mochel32917e52006-05-19 16:54:39 -040066 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040067 struct acpi_pci_id id;
68 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71static LIST_HEAD(acpi_pci_roots);
72
73static struct acpi_pci_driver *sub_driver;
74
75int acpi_pci_register_driver(struct acpi_pci_driver *driver)
76{
77 int n = 0;
78 struct list_head *entry;
79
80 struct acpi_pci_driver **pptr = &sub_driver;
81 while (*pptr)
82 pptr = &(*pptr)->next;
83 *pptr = driver;
84
85 if (!driver->add)
86 return 0;
87
88 list_for_each(entry, &acpi_pci_roots) {
89 struct acpi_pci_root *root;
90 root = list_entry(entry, struct acpi_pci_root, node);
Patrick Mochel2d1e0a02006-05-19 16:54:43 -040091 driver->add(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 n++;
93 }
94
95 return n;
96}
Len Brown4be44fc2005-08-05 00:44:28 -040097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098EXPORT_SYMBOL(acpi_pci_register_driver);
99
100void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
101{
102 struct list_head *entry;
103
104 struct acpi_pci_driver **pptr = &sub_driver;
105 while (*pptr) {
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800106 if (*pptr == driver)
107 break;
108 pptr = &(*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800110 BUG_ON(!*pptr);
111 *pptr = (*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (!driver->remove)
114 return;
115
116 list_for_each(entry, &acpi_pci_roots) {
117 struct acpi_pci_root *root;
118 root = list_entry(entry, struct acpi_pci_root, node);
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400119 driver->remove(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121}
Len Brown4be44fc2005-08-05 00:44:28 -0400122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123EXPORT_SYMBOL(acpi_pci_unregister_driver);
124
Justin Chend91a0072006-12-06 10:17:10 -0700125acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
126{
127 struct acpi_pci_root *tmp;
128
129 list_for_each_entry(tmp, &acpi_pci_roots, node) {
130 if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
131 return tmp->device->handle;
132 }
133 return NULL;
134}
135
136EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400139get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200141 int *busnr = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct acpi_resource_address64 address;
143
Bob Moore50eca3e2005-09-30 19:03:00 -0400144 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
145 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
146 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return AE_OK;
148
149 acpi_resource_to_address64(resource, &address);
Len Brown4be44fc2005-08-05 00:44:28 -0400150 if ((address.address_length > 0) &&
151 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
Bob Moore50eca3e2005-09-30 19:03:00 -0400152 *busnr = address.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return AE_OK;
155}
156
Len Brown4be44fc2005-08-05 00:44:28 -0400157static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 acpi_status status;
160
161 *busnum = -1;
Len Brown4be44fc2005-08-05 00:44:28 -0400162 status =
163 acpi_walk_resources(handle, METHOD_NAME__CRS,
164 get_root_bridge_busnr_callback, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (ACPI_FAILURE(status))
166 return status;
167 /* Check if we really get a bus number from _CRS */
168 if (*busnum == -1)
169 return AE_ERROR;
170 return AE_OK;
171}
172
Rui Zhang2786f6e2006-12-21 02:21:13 -0500173static void acpi_pci_bridge_scan(struct acpi_device *device)
174{
175 int status;
176 struct acpi_device *child = NULL;
177
178 if (device->flags.bus_address)
179 if (device->parent && device->parent->ops.bind) {
180 status = device->parent->ops.bind(device);
181 if (!status) {
182 list_for_each_entry(child, &device->children, node)
183 acpi_pci_bridge_scan(child);
184 }
185 }
186}
187
Sam Ravnborgb5678a32008-02-17 13:23:03 +0100188static int __devinit acpi_pci_root_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Len Brown4be44fc2005-08-05 00:44:28 -0400190 int result = 0;
191 struct acpi_pci_root *root = NULL;
192 struct acpi_pci_root *tmp;
193 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400194 unsigned long long value = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400195 acpi_handle handle = NULL;
Rui Zhang2786f6e2006-12-21 02:21:13 -0500196 struct acpi_device *child;
Andrew Patterson990a7ac2008-11-10 15:30:45 -0700197 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Burman Yan36bcbec2006-12-19 12:56:11 -0800203 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -0400205 return -ENOMEM;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700206 INIT_LIST_HEAD(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Patrick Mochel32917e52006-05-19 16:54:39 -0400208 root->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
210 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700211 device->driver_data = root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 device->ops.bind = acpi_pci_bind;
214
Andrew Patterson990a7ac2008-11-10 15:30:45 -0700215 /*
216 * All supported architectures that use ACPI have support for
217 * PCI domains, so we indicate this in _OSC support capabilities.
218 */
219 flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
220 pci_acpi_osc_support(device->handle, flags);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
223 * Segment
224 * -------
225 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
226 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400227 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400228 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 switch (status) {
230 case AE_OK:
231 root->id.segment = (u16) value;
232 break;
233 case AE_NOT_FOUND:
Len Brown4be44fc2005-08-05 00:44:28 -0400234 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
235 "Assuming segment 0 (no _SEG)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 root->id.segment = 0;
237 break;
238 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400239 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 result = -ENODEV;
241 goto end;
242 }
243
244 /*
245 * Bus
246 * ---
247 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
248 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400249 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400250 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 switch (status) {
252 case AE_OK:
253 root->id.bus = (u16) value;
254 break;
255 case AE_NOT_FOUND:
256 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
257 root->id.bus = 0;
258 break;
259 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400260 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 result = -ENODEV;
262 goto end;
263 }
264
265 /* Some systems have wrong _BBN */
266 list_for_each_entry(tmp, &acpi_pci_roots, node) {
267 if ((tmp->id.segment == root->id.segment)
Len Brown4be44fc2005-08-05 00:44:28 -0400268 && (tmp->id.bus == root->id.bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int bus = 0;
270 acpi_status status;
271
Len Brown64684632006-06-26 23:41:38 -0400272 printk(KERN_ERR PREFIX
Thomas Renningera6fc6722006-06-26 23:58:43 -0400273 "Wrong _BBN value, reboot"
Len Brown64684632006-06-26 23:41:38 -0400274 " and use option 'pci=noacpi'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400276 status = try_get_root_bridge_busnr(device->handle, &bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (ACPI_FAILURE(status))
278 break;
279 if (bus != root->id.bus) {
Len Brown4be44fc2005-08-05 00:44:28 -0400280 printk(KERN_INFO PREFIX
281 "PCI _CRS %d overrides _BBN 0\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 root->id.bus = bus;
283 }
284 break;
285 }
286 }
287 /*
288 * Device & Function
289 * -----------------
290 * Obtained from _ADR (which has already been evaluated for us).
291 */
292 root->id.device = device->pnp.bus_address >> 16;
293 root->id.function = device->pnp.bus_address & 0xFFFF;
294
295 /*
296 * TBD: Need PCI interface for enumeration/configuration of roots.
297 */
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 /* TBD: Locking */
300 list_add_tail(&root->node, &acpi_pci_roots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Len Brown4be44fc2005-08-05 00:44:28 -0400302 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
303 acpi_device_name(device), acpi_device_bid(device),
304 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /*
307 * Scan the Root Bridge
308 * --------------------
309 * Must do this prior to any attempt to bind the root device, as the
310 * PCI namespace does not get created until this call is made (and
311 * thus the root bridge's pci_dev does not exist).
312 */
313 root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
314 if (!root->bus) {
Len Brown64684632006-06-26 23:41:38 -0400315 printk(KERN_ERR PREFIX
316 "Bus %04x:%02x not present in PCI namespace\n",
317 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 result = -ENODEV;
319 goto end;
320 }
321
322 /*
323 * Attach ACPI-PCI Context
324 * -----------------------
325 * Thus binding the ACPI and PCI devices.
326 */
327 result = acpi_pci_bind_root(device, &root->id, root->bus);
328 if (result)
329 goto end;
330
331 /*
332 * PCI Routing Table
333 * -----------------
334 * Evaluate and parse _PRT, if exists.
335 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400336 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (ACPI_SUCCESS(status))
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400338 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400339 root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Rui Zhang2786f6e2006-12-21 02:21:13 -0500341 /*
342 * Scan and bind all _ADR-Based Devices
343 */
344 list_for_each_entry(child, &device->children, node)
345 acpi_pci_bridge_scan(child);
346
Len Brown4be44fc2005-08-05 00:44:28 -0400347 end:
Rajesh Shahc431ada2005-04-28 00:25:45 -0700348 if (result) {
349 if (!list_empty(&root->node))
350 list_del(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 kfree(root);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Patrick Mocheld550d982006-06-27 00:41:40 -0400354 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Len Brown4be44fc2005-08-05 00:44:28 -0400357static int acpi_pci_root_start(struct acpi_device *device)
Rajesh Shahc431ada2005-04-28 00:25:45 -0700358{
Len Brown4be44fc2005-08-05 00:44:28 -0400359 struct acpi_pci_root *root;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700360
Rajesh Shahc431ada2005-04-28 00:25:45 -0700361
362 list_for_each_entry(root, &acpi_pci_roots, node) {
Patrick Mochel432bfab2006-05-19 16:54:51 -0400363 if (root->device == device) {
Rajesh Shahc431ada2005-04-28 00:25:45 -0700364 pci_bus_add_devices(root->bus);
Patrick Mocheld550d982006-06-27 00:41:40 -0400365 return 0;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700366 }
367 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400368 return -ENODEV;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Len Brown4be44fc2005-08-05 00:44:28 -0400371static int acpi_pci_root_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Len Brown4be44fc2005-08-05 00:44:28 -0400373 struct acpi_pci_root *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200379 root = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 kfree(root);
382
Patrick Mocheld550d982006-06-27 00:41:40 -0400383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Len Brown4be44fc2005-08-05 00:44:28 -0400386static int __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (acpi_pci_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400392 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Patrick Mocheld550d982006-06-27 00:41:40 -0400394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397subsys_initcall(acpi_pci_root_init);