2 * pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
33 #include <linux/pci.h>
34 #include <linux/pci-acpi.h>
35 #include <linux/acpi.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
39 #define _COMPONENT ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME("pci_root");
41 #define ACPI_PCI_ROOT_CLASS "pci_bridge"
42 #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
43 static int acpi_pci_root_add(struct acpi_device *device);
44 static int acpi_pci_root_remove(struct acpi_device *device, int type);
45 static int acpi_pci_root_start(struct acpi_device *device);
47 static struct acpi_device_id root_device_ids[] = {
51 MODULE_DEVICE_TABLE(acpi, root_device_ids);
53 static struct acpi_driver acpi_pci_root_driver = {
55 .class = ACPI_PCI_ROOT_CLASS,
56 .ids = root_device_ids,
58 .add = acpi_pci_root_add,
59 .remove = acpi_pci_root_remove,
60 .start = acpi_pci_root_start,
64 struct acpi_pci_root {
65 struct list_head node;
66 struct acpi_device * device;
67 struct acpi_pci_id id;
70 u32 osc_support_set; /* _OSC state of support bits */
71 u32 osc_control_set; /* _OSC state of control bits */
72 u32 osc_control_qry; /* the latest _OSC query result */
74 u32 osc_queried:1; /* has _OSC control been queried? */
77 static LIST_HEAD(acpi_pci_roots);
79 static struct acpi_pci_driver *sub_driver;
80 static DEFINE_MUTEX(osc_lock);
82 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
85 struct list_head *entry;
87 struct acpi_pci_driver **pptr = &sub_driver;
89 pptr = &(*pptr)->next;
95 list_for_each(entry, &acpi_pci_roots) {
96 struct acpi_pci_root *root;
97 root = list_entry(entry, struct acpi_pci_root, node);
98 driver->add(root->device->handle);
105 EXPORT_SYMBOL(acpi_pci_register_driver);
107 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
109 struct list_head *entry;
111 struct acpi_pci_driver **pptr = &sub_driver;
115 pptr = &(*pptr)->next;
118 *pptr = (*pptr)->next;
123 list_for_each(entry, &acpi_pci_roots) {
124 struct acpi_pci_root *root;
125 root = list_entry(entry, struct acpi_pci_root, node);
126 driver->remove(root->device->handle);
130 EXPORT_SYMBOL(acpi_pci_unregister_driver);
132 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
134 struct acpi_pci_root *tmp;
136 list_for_each_entry(tmp, &acpi_pci_roots, node) {
137 if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
138 return tmp->device->handle;
143 EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
146 get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
149 struct acpi_resource_address64 address;
151 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
152 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
153 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
156 acpi_resource_to_address64(resource, &address);
157 if ((address.address_length > 0) &&
158 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
159 *busnr = address.minimum;
164 static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
170 acpi_walk_resources(handle, METHOD_NAME__CRS,
171 get_root_bridge_busnr_callback, busnum);
172 if (ACPI_FAILURE(status))
174 /* Check if we really get a bus number from _CRS */
180 static void acpi_pci_bridge_scan(struct acpi_device *device)
183 struct acpi_device *child = NULL;
185 if (device->flags.bus_address)
186 if (device->parent && device->parent->ops.bind) {
187 status = device->parent->ops.bind(device);
189 list_for_each_entry(child, &device->children, node)
190 acpi_pci_bridge_scan(child);
195 static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
196 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
198 static acpi_status acpi_pci_run_osc(acpi_handle handle,
199 const u32 *capbuf, u32 *retval)
202 struct acpi_object_list input;
203 union acpi_object in_params[4];
204 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
205 union acpi_object *out_obj;
208 /* Setting up input parameters */
210 input.pointer = in_params;
211 in_params[0].type = ACPI_TYPE_BUFFER;
212 in_params[0].buffer.length = 16;
213 in_params[0].buffer.pointer = OSC_UUID;
214 in_params[1].type = ACPI_TYPE_INTEGER;
215 in_params[1].integer.value = 1;
216 in_params[2].type = ACPI_TYPE_INTEGER;
217 in_params[2].integer.value = 3;
218 in_params[3].type = ACPI_TYPE_BUFFER;
219 in_params[3].buffer.length = 12;
220 in_params[3].buffer.pointer = (u8 *)capbuf;
222 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
223 if (ACPI_FAILURE(status))
227 return AE_NULL_OBJECT;
229 out_obj = output.pointer;
230 if (out_obj->type != ACPI_TYPE_BUFFER) {
231 printk(KERN_DEBUG "_OSC evaluation returned wrong type\n");
235 /* Need to ignore the bit0 in result code */
236 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
238 if (errors & OSC_REQUEST_ERROR)
239 printk(KERN_DEBUG "_OSC request failed\n");
240 if (errors & OSC_INVALID_UUID_ERROR)
241 printk(KERN_DEBUG "_OSC invalid UUID\n");
242 if (errors & OSC_INVALID_REVISION_ERROR)
243 printk(KERN_DEBUG "_OSC invalid revision\n");
244 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
245 if (capbuf[OSC_QUERY_TYPE] & OSC_QUERY_ENABLE)
248 "Firmware did not grant requested _OSC control\n");
256 *retval = *((u32 *)(out_obj->buffer.pointer + 8));
260 kfree(output.pointer);
264 static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 flags)
267 u32 support_set, result, capbuf[3];
269 /* do _OSC query for all possible controls */
270 support_set = root->osc_support_set | (flags & OSC_SUPPORT_MASKS);
271 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
272 capbuf[OSC_SUPPORT_TYPE] = support_set;
273 capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
275 status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
276 if (ACPI_SUCCESS(status)) {
277 root->osc_support_set = support_set;
278 root->osc_control_qry = result;
279 root->osc_queried = 1;
284 static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
289 status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
290 if (ACPI_FAILURE(status))
292 mutex_lock(&osc_lock);
293 status = acpi_pci_query_osc(root, flags);
294 mutex_unlock(&osc_lock);
298 static struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
300 struct acpi_pci_root *root;
301 list_for_each_entry(root, &acpi_pci_roots, node) {
302 if (root->device->handle == handle)
309 * acpi_pci_osc_control_set - commit requested control to Firmware
310 * @handle: acpi_handle for the target ACPI object
311 * @flags: driver's requested control bits
313 * Attempt to take control from Firmware on requested control bits.
315 acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 flags)
318 u32 control_req, result, capbuf[3];
320 struct acpi_pci_root *root;
322 status = acpi_get_handle(handle, "_OSC", &tmp);
323 if (ACPI_FAILURE(status))
326 control_req = (flags & OSC_CONTROL_MASKS);
330 root = acpi_pci_find_root(handle);
334 mutex_lock(&osc_lock);
335 /* No need to evaluate _OSC if the control was already granted. */
336 if ((root->osc_control_set & control_req) == control_req)
339 /* Need to query controls first before requesting them */
340 if (!root->osc_queried) {
341 status = acpi_pci_query_osc(root, root->osc_support_set);
342 if (ACPI_FAILURE(status))
345 if ((root->osc_control_qry & control_req) != control_req) {
347 "Firmware did not grant requested _OSC control\n");
352 capbuf[OSC_QUERY_TYPE] = 0;
353 capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
354 capbuf[OSC_CONTROL_TYPE] = root->osc_control_set | control_req;
355 status = acpi_pci_run_osc(handle, capbuf, &result);
356 if (ACPI_SUCCESS(status))
357 root->osc_control_set = result;
359 mutex_unlock(&osc_lock);
362 EXPORT_SYMBOL(acpi_pci_osc_control_set);
364 static int __devinit acpi_pci_root_add(struct acpi_device *device)
367 struct acpi_pci_root *root = NULL;
368 struct acpi_pci_root *tmp;
369 acpi_status status = AE_OK;
370 unsigned long long value = 0;
371 acpi_handle handle = NULL;
372 struct acpi_device *child;
373 u32 flags, base_flags;
379 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
382 INIT_LIST_HEAD(&root->node);
384 root->device = device;
385 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
386 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
387 device->driver_data = root;
389 device->ops.bind = acpi_pci_bind;
392 * All supported architectures that use ACPI have support for
393 * PCI domains, so we indicate this in _OSC support capabilities.
395 flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
396 acpi_pci_osc_support(root, flags);
401 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
403 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
407 root->id.segment = (u16) value;
410 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
411 "Assuming segment 0 (no _SEG)\n"));
412 root->id.segment = 0;
415 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
423 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
425 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
429 root->id.bus = (u16) value;
432 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
436 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
441 /* Some systems have wrong _BBN */
442 list_for_each_entry(tmp, &acpi_pci_roots, node) {
443 if ((tmp->id.segment == root->id.segment)
444 && (tmp->id.bus == root->id.bus)) {
448 printk(KERN_ERR PREFIX
449 "Wrong _BBN value, reboot"
450 " and use option 'pci=noacpi'\n");
452 status = try_get_root_bridge_busnr(device->handle, &bus);
453 if (ACPI_FAILURE(status))
455 if (bus != root->id.bus) {
456 printk(KERN_INFO PREFIX
457 "PCI _CRS %d overrides _BBN 0\n", bus);
466 * Obtained from _ADR (which has already been evaluated for us).
468 root->id.device = device->pnp.bus_address >> 16;
469 root->id.function = device->pnp.bus_address & 0xFFFF;
472 * TBD: Need PCI interface for enumeration/configuration of roots.
476 list_add_tail(&root->node, &acpi_pci_roots);
478 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
479 acpi_device_name(device), acpi_device_bid(device),
480 root->id.segment, root->id.bus);
483 * Scan the Root Bridge
484 * --------------------
485 * Must do this prior to any attempt to bind the root device, as the
486 * PCI namespace does not get created until this call is made (and
487 * thus the root bridge's pci_dev does not exist).
489 root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
491 printk(KERN_ERR PREFIX
492 "Bus %04x:%02x not present in PCI namespace\n",
493 root->id.segment, root->id.bus);
499 * Attach ACPI-PCI Context
500 * -----------------------
501 * Thus binding the ACPI and PCI devices.
503 result = acpi_pci_bind_root(device, &root->id, root->bus);
510 * Evaluate and parse _PRT, if exists.
512 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
513 if (ACPI_SUCCESS(status))
514 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
518 * Scan and bind all _ADR-Based Devices
520 list_for_each_entry(child, &device->children, node)
521 acpi_pci_bridge_scan(child);
523 /* Indicate support for various _OSC capabilities. */
524 if (pci_ext_cfg_avail(root->bus->self))
525 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
526 if (pcie_aspm_enabled())
527 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
528 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
529 if (pci_msi_enabled())
530 flags |= OSC_MSI_SUPPORT;
531 if (flags != base_flags)
532 acpi_pci_osc_support(root, flags);
536 if (!list_empty(&root->node))
537 list_del(&root->node);
544 static int acpi_pci_root_start(struct acpi_device *device)
546 struct acpi_pci_root *root;
549 list_for_each_entry(root, &acpi_pci_roots, node) {
550 if (root->device == device) {
551 pci_bus_add_devices(root->bus);
558 static int acpi_pci_root_remove(struct acpi_device *device, int type)
560 struct acpi_pci_root *root = NULL;
563 if (!device || !acpi_driver_data(device))
566 root = acpi_driver_data(device);
573 static int __init acpi_pci_root_init(void)
575 if (acpi_pci_disabled)
578 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
584 subsys_initcall(acpi_pci_root_init);