blob: 0f661dd42d4151ff09c7f4fe6bb336f59ec7f348 [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;
61 acpi_handle handle;
62 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);
86 driver->add(root->handle);
87 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);
113 driver->remove(root->handle);
114 }
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
Len Brown4be44fc2005-08-05 00:44:28 -0400154static int acpi_pci_root_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Len Brown4be44fc2005-08-05 00:44:28 -0400156 int result = 0;
157 struct acpi_pci_root *root = NULL;
158 struct acpi_pci_root *tmp;
159 acpi_status status = AE_OK;
160 unsigned long value = 0;
161 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 ACPI_FUNCTION_TRACE("acpi_pci_root_add");
164
165 if (!device)
166 return_VALUE(-EINVAL);
167
168 root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
169 if (!root)
170 return_VALUE(-ENOMEM);
171 memset(root, 0, sizeof(struct acpi_pci_root));
Rajesh Shahc431ada2005-04-28 00:25:45 -0700172 INIT_LIST_HEAD(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 root->handle = device->handle;
175 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
176 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
177 acpi_driver_data(device) = root;
178
179 /*
180 * TBD: Doesn't the bus driver automatically set this?
181 */
182 device->ops.bind = acpi_pci_bind;
183
184 /*
185 * Segment
186 * -------
187 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
188 */
Len Brown4be44fc2005-08-05 00:44:28 -0400189 status = acpi_evaluate_integer(root->handle, METHOD_NAME__SEG, NULL,
190 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 switch (status) {
192 case AE_OK:
193 root->id.segment = (u16) value;
194 break;
195 case AE_NOT_FOUND:
Len Brown4be44fc2005-08-05 00:44:28 -0400196 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
197 "Assuming segment 0 (no _SEG)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 root->id.segment = 0;
199 break;
200 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400201 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 result = -ENODEV;
203 goto end;
204 }
205
206 /*
207 * Bus
208 * ---
209 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
210 */
Len Brown4be44fc2005-08-05 00:44:28 -0400211 status = acpi_evaluate_integer(root->handle, METHOD_NAME__BBN, NULL,
212 &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 switch (status) {
214 case AE_OK:
215 root->id.bus = (u16) value;
216 break;
217 case AE_NOT_FOUND:
218 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
219 root->id.bus = 0;
220 break;
221 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400222 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 result = -ENODEV;
224 goto end;
225 }
226
227 /* Some systems have wrong _BBN */
228 list_for_each_entry(tmp, &acpi_pci_roots, node) {
229 if ((tmp->id.segment == root->id.segment)
Len Brown4be44fc2005-08-05 00:44:28 -0400230 && (tmp->id.bus == root->id.bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int bus = 0;
232 acpi_status status;
233
Thomas Renningera6fc6722006-06-26 23:58:43 -0400234 ACPI_ERROR((AE_INFO,
235 "Wrong _BBN value, reboot"
236 " and use option 'pci=noacpi'"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 status = try_get_root_bridge_busnr(root->handle, &bus);
239 if (ACPI_FAILURE(status))
240 break;
241 if (bus != root->id.bus) {
Len Brown4be44fc2005-08-05 00:44:28 -0400242 printk(KERN_INFO PREFIX
243 "PCI _CRS %d overrides _BBN 0\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 root->id.bus = bus;
245 }
246 break;
247 }
248 }
249 /*
250 * Device & Function
251 * -----------------
252 * Obtained from _ADR (which has already been evaluated for us).
253 */
254 root->id.device = device->pnp.bus_address >> 16;
255 root->id.function = device->pnp.bus_address & 0xFFFF;
256
257 /*
258 * TBD: Need PCI interface for enumeration/configuration of roots.
259 */
260
Len Brown4be44fc2005-08-05 00:44:28 -0400261 /* TBD: Locking */
262 list_add_tail(&root->node, &acpi_pci_roots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Len Brown4be44fc2005-08-05 00:44:28 -0400264 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
265 acpi_device_name(device), acpi_device_bid(device),
266 root->id.segment, root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /*
269 * Scan the Root Bridge
270 * --------------------
271 * Must do this prior to any attempt to bind the root device, as the
272 * PCI namespace does not get created until this call is made (and
273 * thus the root bridge's pci_dev does not exist).
274 */
275 root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
276 if (!root->bus) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400277 ACPI_ERROR((AE_INFO,
278 "Bus %04x:%02x not present in PCI namespace",
279 root->id.segment, root->id.bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 result = -ENODEV;
281 goto end;
282 }
283
284 /*
285 * Attach ACPI-PCI Context
286 * -----------------------
287 * Thus binding the ACPI and PCI devices.
288 */
289 result = acpi_pci_bind_root(device, &root->id, root->bus);
290 if (result)
291 goto end;
292
293 /*
294 * PCI Routing Table
295 * -----------------
296 * Evaluate and parse _PRT, if exists.
297 */
298 status = acpi_get_handle(root->handle, METHOD_NAME__PRT, &handle);
299 if (ACPI_SUCCESS(status))
300 result = acpi_pci_irq_add_prt(root->handle, root->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400301 root->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Len Brown4be44fc2005-08-05 00:44:28 -0400303 end:
Rajesh Shahc431ada2005-04-28 00:25:45 -0700304 if (result) {
305 if (!list_empty(&root->node))
306 list_del(&root->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 kfree(root);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 return_VALUE(result);
311}
312
Len Brown4be44fc2005-08-05 00:44:28 -0400313static int acpi_pci_root_start(struct acpi_device *device)
Rajesh Shahc431ada2005-04-28 00:25:45 -0700314{
Len Brown4be44fc2005-08-05 00:44:28 -0400315 struct acpi_pci_root *root;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700316
317 ACPI_FUNCTION_TRACE("acpi_pci_root_start");
318
319 list_for_each_entry(root, &acpi_pci_roots, node) {
320 if (root->handle == device->handle) {
321 pci_bus_add_devices(root->bus);
322 return_VALUE(0);
323 }
324 }
325 return_VALUE(-ENODEV);
326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Len Brown4be44fc2005-08-05 00:44:28 -0400328static int acpi_pci_root_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Len Brown4be44fc2005-08-05 00:44:28 -0400330 struct acpi_pci_root *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 ACPI_FUNCTION_TRACE("acpi_pci_root_remove");
333
334 if (!device || !acpi_driver_data(device))
335 return_VALUE(-EINVAL);
336
Len Brown4be44fc2005-08-05 00:44:28 -0400337 root = (struct acpi_pci_root *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 kfree(root);
340
341 return_VALUE(0);
342}
343
Len Brown4be44fc2005-08-05 00:44:28 -0400344static int __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 ACPI_FUNCTION_TRACE("acpi_pci_root_init");
347
348 if (acpi_pci_disabled)
349 return_VALUE(0);
350
351 /* DEBUG:
Len Brown4be44fc2005-08-05 00:44:28 -0400352 acpi_dbg_layer = ACPI_PCI_COMPONENT;
353 acpi_dbg_level = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
355
356 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
357 return_VALUE(-ENODEV);
358
359 return_VALUE(0);
360}
361
362subsys_initcall(acpi_pci_root_init);