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/mutex.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/pci.h>
34 #include <linux/pci-acpi.h>
35 #include <linux/pci-aspm.h>
36 #include <linux/acpi.h>
37 #include <linux/slab.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/apei.h>
42 #define PREFIX "ACPI: "
44 #define _COMPONENT ACPI_PCI_COMPONENT
45 ACPI_MODULE_NAME("pci_root");
46 #define ACPI_PCI_ROOT_CLASS "pci_bridge"
47 #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
48 static int acpi_pci_root_add(struct acpi_device *device);
49 static int acpi_pci_root_remove(struct acpi_device *device, int type);
51 #define ACPI_PCIE_REQ_SUPPORT (OSC_EXT_PCI_CONFIG_SUPPORT \
52 | OSC_ACTIVE_STATE_PWR_SUPPORT \
53 | OSC_CLOCK_PWR_CAPABILITY_SUPPORT \
56 static const struct acpi_device_id root_device_ids[] = {
60 MODULE_DEVICE_TABLE(acpi, root_device_ids);
62 static struct acpi_driver acpi_pci_root_driver = {
64 .class = ACPI_PCI_ROOT_CLASS,
65 .ids = root_device_ids,
67 .add = acpi_pci_root_add,
68 .remove = acpi_pci_root_remove,
72 /* Lock to protect both acpi_pci_roots and acpi_pci_drivers lists */
73 static DEFINE_MUTEX(acpi_pci_root_lock);
74 static LIST_HEAD(acpi_pci_roots);
75 static LIST_HEAD(acpi_pci_drivers);
77 static DEFINE_MUTEX(osc_lock);
79 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
82 struct acpi_pci_root *root;
84 mutex_lock(&acpi_pci_root_lock);
85 list_add_tail(&driver->node, &acpi_pci_drivers);
87 list_for_each_entry(root, &acpi_pci_roots, node) {
91 mutex_unlock(&acpi_pci_root_lock);
95 EXPORT_SYMBOL(acpi_pci_register_driver);
97 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
99 struct acpi_pci_root *root;
101 mutex_lock(&acpi_pci_root_lock);
102 list_del(&driver->node);
104 list_for_each_entry(root, &acpi_pci_roots, node)
105 driver->remove(root);
106 mutex_unlock(&acpi_pci_root_lock);
108 EXPORT_SYMBOL(acpi_pci_unregister_driver);
110 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
112 struct acpi_pci_root *root;
113 acpi_handle handle = NULL;
115 mutex_lock(&acpi_pci_root_lock);
116 list_for_each_entry(root, &acpi_pci_roots, node)
117 if ((root->segment == (u16) seg) &&
118 (root->secondary.start == (u16) bus)) {
119 handle = root->device->handle;
122 mutex_unlock(&acpi_pci_root_lock);
126 EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
129 * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
130 * @handle - the ACPI CA node in question.
132 * Note: we could make this API take a struct acpi_device * instead, but
133 * for now, it's more convenient to operate on an acpi_handle.
135 int acpi_is_root_bridge(acpi_handle handle)
138 struct acpi_device *device;
140 ret = acpi_bus_get_device(handle, &device);
144 ret = acpi_match_device_ids(device, root_device_ids);
150 EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
153 get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
155 struct resource *res = data;
156 struct acpi_resource_address64 address;
158 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
159 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
160 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
163 acpi_resource_to_address64(resource, &address);
164 if ((address.address_length > 0) &&
165 (address.resource_type == ACPI_BUS_NUMBER_RANGE)) {
166 res->start = address.minimum;
167 res->end = address.minimum + address.address_length - 1;
173 static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
174 struct resource *res)
180 acpi_walk_resources(handle, METHOD_NAME__CRS,
181 get_root_bridge_busnr_callback, res);
182 if (ACPI_FAILURE(status))
184 if (res->start == -1)
189 static void acpi_pci_bridge_scan(struct acpi_device *device)
192 struct acpi_device *child = NULL;
194 if (device->flags.bus_address)
195 if (device->parent && device->parent->ops.bind) {
196 status = device->parent->ops.bind(device);
198 list_for_each_entry(child, &device->children, node)
199 acpi_pci_bridge_scan(child);
204 static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
206 static acpi_status acpi_pci_run_osc(acpi_handle handle,
207 const u32 *capbuf, u32 *retval)
209 struct acpi_osc_context context = {
210 .uuid_str = pci_osc_uuid_str,
213 .cap.pointer = (void *)capbuf,
217 status = acpi_run_osc(handle, &context);
218 if (ACPI_SUCCESS(status)) {
219 *retval = *((u32 *)(context.ret.pointer + 8));
220 kfree(context.ret.pointer);
225 static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root,
230 u32 result, capbuf[3];
232 support &= OSC_PCI_SUPPORT_MASKS;
233 support |= root->osc_support_set;
235 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
236 capbuf[OSC_SUPPORT_TYPE] = support;
238 *control &= OSC_PCI_CONTROL_MASKS;
239 capbuf[OSC_CONTROL_TYPE] = *control | root->osc_control_set;
241 /* Run _OSC query for all possible controls. */
242 capbuf[OSC_CONTROL_TYPE] = OSC_PCI_CONTROL_MASKS;
245 status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
246 if (ACPI_SUCCESS(status)) {
247 root->osc_support_set = support;
254 static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
259 status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
260 if (ACPI_FAILURE(status))
262 mutex_lock(&osc_lock);
263 status = acpi_pci_query_osc(root, flags, NULL);
264 mutex_unlock(&osc_lock);
268 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
270 struct acpi_pci_root *root;
271 struct acpi_device *device;
273 if (acpi_bus_get_device(handle, &device) ||
274 acpi_match_device_ids(device, root_device_ids))
277 root = acpi_driver_data(device);
281 EXPORT_SYMBOL_GPL(acpi_pci_find_root);
283 struct acpi_handle_node {
284 struct list_head node;
289 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
290 * @handle: the handle in question
292 * Given an ACPI CA handle, the desired PCI device is located in the
293 * list of PCI devices.
295 * If the device is found, its reference count is increased and this
296 * function returns a pointer to its data structure. The caller must
297 * decrement the reference count by calling pci_dev_put().
298 * If no device is found, %NULL is returned.
300 struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
303 unsigned long long adr;
306 struct pci_bus *pbus;
307 struct pci_dev *pdev = NULL;
308 struct acpi_handle_node *node, *tmp;
309 struct acpi_pci_root *root;
310 LIST_HEAD(device_list);
313 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
316 while (!acpi_is_root_bridge(phandle)) {
317 node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
321 INIT_LIST_HEAD(&node->node);
322 node->handle = phandle;
323 list_add(&node->node, &device_list);
325 status = acpi_get_parent(phandle, &phandle);
326 if (ACPI_FAILURE(status))
330 root = acpi_pci_find_root(phandle);
337 * Now, walk back down the PCI device tree until we return to our
338 * original handle. Assumes that everything between the PCI root
339 * bridge and the device we're looking for must be a P2P bridge.
341 list_for_each_entry(node, &device_list, node) {
342 acpi_handle hnd = node->handle;
343 status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
344 if (ACPI_FAILURE(status))
346 dev = (adr >> 16) & 0xffff;
349 pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
350 if (!pdev || hnd == handle)
353 pbus = pdev->subordinate;
357 * This function may be called for a non-PCI device that has a
358 * PCI parent (eg. a disk under a PCI SATA controller). In that
359 * case pdev->subordinate will be NULL for the parent.
362 dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
368 list_for_each_entry_safe(node, tmp, &device_list, node)
373 EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
376 * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
377 * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
378 * @mask: Mask of _OSC bits to request control of, place to store control mask.
379 * @req: Mask of _OSC bits the control of is essential to the caller.
381 * Run _OSC query for @mask and if that is successful, compare the returned
382 * mask of control bits with @req. If all of the @req bits are set in the
383 * returned mask, run _OSC request for it.
385 * The variable at the @mask address may be modified regardless of whether or
386 * not the function returns success. On success it will contain the mask of
387 * _OSC bits the BIOS has granted control of, but its contents are meaningless
390 acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask, u32 req)
392 struct acpi_pci_root *root;
398 return AE_BAD_PARAMETER;
400 ctrl = *mask & OSC_PCI_CONTROL_MASKS;
401 if ((ctrl & req) != req)
404 root = acpi_pci_find_root(handle);
408 status = acpi_get_handle(handle, "_OSC", &tmp);
409 if (ACPI_FAILURE(status))
412 mutex_lock(&osc_lock);
414 *mask = ctrl | root->osc_control_set;
415 /* No need to evaluate _OSC if the control was already granted. */
416 if ((root->osc_control_set & ctrl) == ctrl)
419 /* Need to check the available controls bits before requesting them. */
421 status = acpi_pci_query_osc(root, root->osc_support_set, mask);
422 if (ACPI_FAILURE(status))
429 if ((ctrl & req) != req) {
434 capbuf[OSC_QUERY_TYPE] = 0;
435 capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
436 capbuf[OSC_CONTROL_TYPE] = ctrl;
437 status = acpi_pci_run_osc(handle, capbuf, mask);
438 if (ACPI_SUCCESS(status))
439 root->osc_control_set = *mask;
441 mutex_unlock(&osc_lock);
444 EXPORT_SYMBOL(acpi_pci_osc_control_set);
446 static int acpi_pci_root_add(struct acpi_device *device)
448 unsigned long long segment, bus;
451 struct acpi_pci_root *root;
453 struct acpi_device *child;
454 struct acpi_pci_driver *driver;
455 u32 flags, base_flags;
456 bool is_osc_granted = false;
458 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
463 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
465 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
466 printk(KERN_ERR PREFIX "can't evaluate _SEG\n");
471 /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
472 root->secondary.flags = IORESOURCE_BUS;
473 status = try_get_root_bridge_busnr(device->handle, &root->secondary);
474 if (ACPI_FAILURE(status)) {
476 * We need both the start and end of the downstream bus range
477 * to interpret _CBA (MMCONFIG base address), so it really is
478 * supposed to be in _CRS. If we don't find it there, all we
479 * can do is assume [_BBN-0xFF] or [0-0xFF].
481 root->secondary.end = 0xFF;
482 printk(KERN_WARNING FW_BUG PREFIX
483 "no secondary bus range in _CRS\n");
484 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN,
486 if (ACPI_SUCCESS(status))
487 root->secondary.start = bus;
488 else if (status == AE_NOT_FOUND)
489 root->secondary.start = 0;
491 printk(KERN_ERR PREFIX "can't evaluate _BBN\n");
497 INIT_LIST_HEAD(&root->node);
498 root->device = device;
499 root->segment = segment & 0xFFFF;
500 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
501 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
502 device->driver_data = root;
504 printk(KERN_INFO PREFIX "%s [%s] (domain %04x %pR)\n",
505 acpi_device_name(device), acpi_device_bid(device),
506 root->segment, &root->secondary);
511 * Evaluate and parse _PRT, if exists.
513 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
514 if (ACPI_SUCCESS(status))
515 result = acpi_pci_irq_add_prt(device->handle, root->segment,
516 root->secondary.start);
518 root->mcfg_addr = acpi_pci_root_get_mcfg_addr(device->handle);
521 * All supported architectures that use ACPI have support for
522 * PCI domains, so we indicate this in _OSC support capabilities.
524 flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
525 acpi_pci_osc_support(root, flags);
527 /* Indicate support for various _OSC capabilities. */
528 if (pci_ext_cfg_avail())
529 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
530 if (pcie_aspm_support_enabled()) {
531 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
532 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
534 if (pci_msi_enabled())
535 flags |= OSC_MSI_SUPPORT;
536 if (flags != base_flags) {
537 status = acpi_pci_osc_support(root, flags);
538 if (ACPI_FAILURE(status)) {
539 dev_info(&device->dev, "ACPI _OSC support "
540 "notification failed, disabling PCIe ASPM\n");
545 if (!pcie_ports_disabled
546 && (flags & ACPI_PCIE_REQ_SUPPORT) == ACPI_PCIE_REQ_SUPPORT) {
547 flags = OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL
548 | OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
549 | OSC_PCI_EXPRESS_PME_CONTROL;
551 if (pci_aer_available()) {
552 if (aer_acpi_firmware_first())
553 dev_dbg(&device->dev,
554 "PCIe errors handled by BIOS.\n");
556 flags |= OSC_PCI_EXPRESS_AER_CONTROL;
559 dev_info(&device->dev,
560 "Requesting ACPI _OSC control (0x%02x)\n", flags);
562 status = acpi_pci_osc_control_set(device->handle, &flags,
563 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
564 if (ACPI_SUCCESS(status)) {
565 is_osc_granted = true;
566 dev_info(&device->dev,
567 "ACPI _OSC control (0x%02x) granted\n", flags);
569 is_osc_granted = false;
570 dev_info(&device->dev,
571 "ACPI _OSC request failed (%s), "
572 "returned control mask: 0x%02x\n",
573 acpi_format_exception(status), flags);
576 dev_info(&device->dev,
577 "Unable to request _OSC control "
578 "(_OSC support mask: 0x%02x)\n", flags);
582 * TBD: Need PCI interface for enumeration/configuration of roots.
585 mutex_lock(&acpi_pci_root_lock);
586 list_add_tail(&root->node, &acpi_pci_roots);
587 mutex_unlock(&acpi_pci_root_lock);
590 * Scan the Root Bridge
591 * --------------------
592 * Must do this prior to any attempt to bind the root device, as the
593 * PCI namespace does not get created until this call is made (and
594 * thus the root bridge's pci_dev does not exist).
596 root->bus = pci_acpi_scan_root(root);
598 printk(KERN_ERR PREFIX
599 "Bus %04x:%02x not present in PCI namespace\n",
600 root->segment, (unsigned int)root->secondary.start);
606 * Attach ACPI-PCI Context
607 * -----------------------
608 * Thus binding the ACPI and PCI devices.
610 result = acpi_pci_bind_root(device);
615 * Scan and bind all _ADR-Based Devices
617 list_for_each_entry(child, &device->children, node)
618 acpi_pci_bridge_scan(child);
621 if (is_osc_granted) {
622 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM)
623 pcie_clear_aspm(root->bus);
625 pr_info("ACPI _OSC control for PCIe not granted, "
630 pci_acpi_add_bus_pm_notifier(device, root->bus);
631 if (device->wakeup.flags.run_wake)
632 device_set_run_wake(root->bus->bridge, true);
634 if (system_state != SYSTEM_BOOTING)
635 pci_assign_unassigned_bus_resources(root->bus);
637 mutex_lock(&acpi_pci_root_lock);
638 list_for_each_entry(driver, &acpi_pci_drivers, node)
641 mutex_unlock(&acpi_pci_root_lock);
643 /* need to after hot-added ioapic is registered */
644 if (system_state != SYSTEM_BOOTING)
645 pci_enable_bridges(root->bus);
647 pci_bus_add_devices(root->bus);
651 mutex_lock(&acpi_pci_root_lock);
652 list_del(&root->node);
653 mutex_unlock(&acpi_pci_root_lock);
655 acpi_pci_irq_del_prt(root->segment, root->secondary.start);
661 static int acpi_pci_root_remove(struct acpi_device *device, int type)
665 struct acpi_pci_root *root = acpi_driver_data(device);
666 struct acpi_pci_driver *driver;
668 pci_stop_root_bus(root->bus);
670 mutex_lock(&acpi_pci_root_lock);
671 list_for_each_entry_reverse(driver, &acpi_pci_drivers, node)
673 driver->remove(root);
674 mutex_unlock(&acpi_pci_root_lock);
676 device_set_run_wake(root->bus->bridge, false);
677 pci_acpi_remove_bus_pm_notifier(device);
679 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
680 if (ACPI_SUCCESS(status))
681 acpi_pci_irq_del_prt(root->segment, root->secondary.start);
683 pci_remove_root_bus(root->bus);
685 mutex_lock(&acpi_pci_root_lock);
686 list_del(&root->node);
687 mutex_unlock(&acpi_pci_root_lock);
692 int __init acpi_pci_root_init(void)
696 if (acpi_pci_disabled)
699 pci_acpi_crs_quirks();
700 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)