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 u8 pci_osc_uuid_str
[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
191 static acpi_status
acpi_pci_run_osc(acpi_handle handle
,
192 const u32
*capbuf
, u32
*retval
)
194 struct acpi_osc_context context
= {
195 .uuid_str
= pci_osc_uuid_str
,
198 .cap
.pointer
= (void *)capbuf
,
202 status
= acpi_run_osc(handle
, &context
);
203 if (ACPI_SUCCESS(status
)) {
204 *retval
= *((u32
*)(context
.ret
.pointer
+ 8));
205 kfree(context
.ret
.pointer
);
210 static acpi_status
acpi_pci_query_osc(struct acpi_pci_root
*root
,
215 u32 result
, capbuf
[3];
217 support
&= OSC_PCI_SUPPORT_MASKS
;
218 support
|= root
->osc_support_set
;
220 capbuf
[OSC_QUERY_TYPE
] = OSC_QUERY_ENABLE
;
221 capbuf
[OSC_SUPPORT_TYPE
] = support
;
223 *control
&= OSC_PCI_CONTROL_MASKS
;
224 capbuf
[OSC_CONTROL_TYPE
] = *control
| root
->osc_control_set
;
226 /* Run _OSC query for all possible controls. */
227 capbuf
[OSC_CONTROL_TYPE
] = OSC_PCI_CONTROL_MASKS
;
230 status
= acpi_pci_run_osc(root
->device
->handle
, capbuf
, &result
);
231 if (ACPI_SUCCESS(status
)) {
232 root
->osc_support_set
= support
;
239 static acpi_status
acpi_pci_osc_support(struct acpi_pci_root
*root
, u32 flags
)
244 status
= acpi_get_handle(root
->device
->handle
, "_OSC", &tmp
);
245 if (ACPI_FAILURE(status
))
247 mutex_lock(&osc_lock
);
248 status
= acpi_pci_query_osc(root
, flags
, NULL
);
249 mutex_unlock(&osc_lock
);
253 struct acpi_pci_root
*acpi_pci_find_root(acpi_handle handle
)
255 struct acpi_pci_root
*root
;
256 struct acpi_device
*device
;
258 if (acpi_bus_get_device(handle
, &device
) ||
259 acpi_match_device_ids(device
, root_device_ids
))
262 root
= acpi_driver_data(device
);
266 EXPORT_SYMBOL_GPL(acpi_pci_find_root
);
268 struct acpi_handle_node
{
269 struct list_head node
;
274 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
275 * @handle: the handle in question
277 * Given an ACPI CA handle, the desired PCI device is located in the
278 * list of PCI devices.
280 * If the device is found, its reference count is increased and this
281 * function returns a pointer to its data structure. The caller must
282 * decrement the reference count by calling pci_dev_put().
283 * If no device is found, %NULL is returned.
285 struct pci_dev
*acpi_get_pci_dev(acpi_handle handle
)
288 unsigned long long adr
;
291 struct pci_bus
*pbus
;
292 struct pci_dev
*pdev
= NULL
;
293 struct acpi_handle_node
*node
, *tmp
;
294 struct acpi_pci_root
*root
;
295 LIST_HEAD(device_list
);
298 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
301 while (!acpi_is_root_bridge(phandle
)) {
302 node
= kzalloc(sizeof(struct acpi_handle_node
), GFP_KERNEL
);
306 INIT_LIST_HEAD(&node
->node
);
307 node
->handle
= phandle
;
308 list_add(&node
->node
, &device_list
);
310 status
= acpi_get_parent(phandle
, &phandle
);
311 if (ACPI_FAILURE(status
))
315 root
= acpi_pci_find_root(phandle
);
322 * Now, walk back down the PCI device tree until we return to our
323 * original handle. Assumes that everything between the PCI root
324 * bridge and the device we're looking for must be a P2P bridge.
326 list_for_each_entry(node
, &device_list
, node
) {
327 acpi_handle hnd
= node
->handle
;
328 status
= acpi_evaluate_integer(hnd
, "_ADR", NULL
, &adr
);
329 if (ACPI_FAILURE(status
))
331 dev
= (adr
>> 16) & 0xffff;
334 pdev
= pci_get_slot(pbus
, PCI_DEVFN(dev
, fn
));
335 if (!pdev
|| hnd
== handle
)
338 pbus
= pdev
->subordinate
;
342 * This function may be called for a non-PCI device that has a
343 * PCI parent (eg. a disk under a PCI SATA controller). In that
344 * case pdev->subordinate will be NULL for the parent.
347 dev_dbg(&pdev
->dev
, "Not a PCI-to-PCI bridge\n");
353 list_for_each_entry_safe(node
, tmp
, &device_list
, node
)
358 EXPORT_SYMBOL_GPL(acpi_get_pci_dev
);
361 * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
362 * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
363 * @mask: Mask of _OSC bits to request control of, place to store control mask.
364 * @req: Mask of _OSC bits the control of is essential to the caller.
366 * Run _OSC query for @mask and if that is successful, compare the returned
367 * mask of control bits with @req. If all of the @req bits are set in the
368 * returned mask, run _OSC request for it.
370 * The variable at the @mask address may be modified regardless of whether or
371 * not the function returns success. On success it will contain the mask of
372 * _OSC bits the BIOS has granted control of, but its contents are meaningless
375 acpi_status
acpi_pci_osc_control_set(acpi_handle handle
, u32
*mask
, u32 req
)
377 struct acpi_pci_root
*root
;
383 return AE_BAD_PARAMETER
;
385 ctrl
= *mask
& OSC_PCI_CONTROL_MASKS
;
386 if ((ctrl
& req
) != req
)
389 root
= acpi_pci_find_root(handle
);
393 status
= acpi_get_handle(handle
, "_OSC", &tmp
);
394 if (ACPI_FAILURE(status
))
397 mutex_lock(&osc_lock
);
399 *mask
= ctrl
| root
->osc_control_set
;
400 /* No need to evaluate _OSC if the control was already granted. */
401 if ((root
->osc_control_set
& ctrl
) == ctrl
)
404 /* Need to check the available controls bits before requesting them. */
406 status
= acpi_pci_query_osc(root
, root
->osc_support_set
, mask
);
407 if (ACPI_FAILURE(status
))
414 if ((ctrl
& req
) != req
) {
419 capbuf
[OSC_QUERY_TYPE
] = 0;
420 capbuf
[OSC_SUPPORT_TYPE
] = root
->osc_support_set
;
421 capbuf
[OSC_CONTROL_TYPE
] = ctrl
;
422 status
= acpi_pci_run_osc(handle
, capbuf
, mask
);
423 if (ACPI_SUCCESS(status
))
424 root
->osc_control_set
= *mask
;
426 mutex_unlock(&osc_lock
);
429 EXPORT_SYMBOL(acpi_pci_osc_control_set
);
431 static int acpi_pci_root_add(struct acpi_device
*device
)
433 unsigned long long segment
, bus
;
436 struct acpi_pci_root
*root
;
438 struct acpi_pci_driver
*driver
;
439 u32 flags
, base_flags
;
440 bool is_osc_granted
= false;
442 root
= kzalloc(sizeof(struct acpi_pci_root
), GFP_KERNEL
);
447 status
= acpi_evaluate_integer(device
->handle
, METHOD_NAME__SEG
, NULL
,
449 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
450 printk(KERN_ERR PREFIX
"can't evaluate _SEG\n");
455 /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
456 root
->secondary
.flags
= IORESOURCE_BUS
;
457 status
= try_get_root_bridge_busnr(device
->handle
, &root
->secondary
);
458 if (ACPI_FAILURE(status
)) {
460 * We need both the start and end of the downstream bus range
461 * to interpret _CBA (MMCONFIG base address), so it really is
462 * supposed to be in _CRS. If we don't find it there, all we
463 * can do is assume [_BBN-0xFF] or [0-0xFF].
465 root
->secondary
.end
= 0xFF;
466 printk(KERN_WARNING FW_BUG PREFIX
467 "no secondary bus range in _CRS\n");
468 status
= acpi_evaluate_integer(device
->handle
, METHOD_NAME__BBN
,
470 if (ACPI_SUCCESS(status
))
471 root
->secondary
.start
= bus
;
472 else if (status
== AE_NOT_FOUND
)
473 root
->secondary
.start
= 0;
475 printk(KERN_ERR PREFIX
"can't evaluate _BBN\n");
481 INIT_LIST_HEAD(&root
->node
);
482 root
->device
= device
;
483 root
->segment
= segment
& 0xFFFF;
484 strcpy(acpi_device_name(device
), ACPI_PCI_ROOT_DEVICE_NAME
);
485 strcpy(acpi_device_class(device
), ACPI_PCI_ROOT_CLASS
);
486 device
->driver_data
= root
;
488 printk(KERN_INFO PREFIX
"%s [%s] (domain %04x %pR)\n",
489 acpi_device_name(device
), acpi_device_bid(device
),
490 root
->segment
, &root
->secondary
);
495 * Evaluate and parse _PRT, if exists.
497 status
= acpi_get_handle(device
->handle
, METHOD_NAME__PRT
, &handle
);
498 if (ACPI_SUCCESS(status
))
499 result
= acpi_pci_irq_add_prt(device
->handle
, root
->segment
,
500 root
->secondary
.start
);
502 root
->mcfg_addr
= acpi_pci_root_get_mcfg_addr(device
->handle
);
505 * All supported architectures that use ACPI have support for
506 * PCI domains, so we indicate this in _OSC support capabilities.
508 flags
= base_flags
= OSC_PCI_SEGMENT_GROUPS_SUPPORT
;
509 acpi_pci_osc_support(root
, flags
);
511 /* Indicate support for various _OSC capabilities. */
512 if (pci_ext_cfg_avail())
513 flags
|= OSC_EXT_PCI_CONFIG_SUPPORT
;
514 if (pcie_aspm_support_enabled()) {
515 flags
|= OSC_ACTIVE_STATE_PWR_SUPPORT
|
516 OSC_CLOCK_PWR_CAPABILITY_SUPPORT
;
518 if (pci_msi_enabled())
519 flags
|= OSC_MSI_SUPPORT
;
520 if (flags
!= base_flags
) {
521 status
= acpi_pci_osc_support(root
, flags
);
522 if (ACPI_FAILURE(status
)) {
523 dev_info(&device
->dev
, "ACPI _OSC support "
524 "notification failed, disabling PCIe ASPM\n");
529 if (!pcie_ports_disabled
530 && (flags
& ACPI_PCIE_REQ_SUPPORT
) == ACPI_PCIE_REQ_SUPPORT
) {
531 flags
= OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL
532 | OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
533 | OSC_PCI_EXPRESS_PME_CONTROL
;
535 if (pci_aer_available()) {
536 if (aer_acpi_firmware_first())
537 dev_dbg(&device
->dev
,
538 "PCIe errors handled by BIOS.\n");
540 flags
|= OSC_PCI_EXPRESS_AER_CONTROL
;
543 dev_info(&device
->dev
,
544 "Requesting ACPI _OSC control (0x%02x)\n", flags
);
546 status
= acpi_pci_osc_control_set(device
->handle
, &flags
,
547 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL
);
548 if (ACPI_SUCCESS(status
)) {
549 is_osc_granted
= true;
550 dev_info(&device
->dev
,
551 "ACPI _OSC control (0x%02x) granted\n", flags
);
553 is_osc_granted
= false;
554 dev_info(&device
->dev
,
555 "ACPI _OSC request failed (%s), "
556 "returned control mask: 0x%02x\n",
557 acpi_format_exception(status
), flags
);
560 dev_info(&device
->dev
,
561 "Unable to request _OSC control "
562 "(_OSC support mask: 0x%02x)\n", flags
);
566 * TBD: Need PCI interface for enumeration/configuration of roots.
569 mutex_lock(&acpi_pci_root_lock
);
570 list_add_tail(&root
->node
, &acpi_pci_roots
);
571 mutex_unlock(&acpi_pci_root_lock
);
574 * Scan the Root Bridge
575 * --------------------
576 * Must do this prior to any attempt to bind the root device, as the
577 * PCI namespace does not get created until this call is made (and
578 * thus the root bridge's pci_dev does not exist).
580 root
->bus
= pci_acpi_scan_root(root
);
582 printk(KERN_ERR PREFIX
583 "Bus %04x:%02x not present in PCI namespace\n",
584 root
->segment
, (unsigned int)root
->secondary
.start
);
590 if (is_osc_granted
) {
591 if (acpi_gbl_FADT
.boot_flags
& ACPI_FADT_NO_ASPM
)
592 pcie_clear_aspm(root
->bus
);
594 pr_info("ACPI _OSC control for PCIe not granted, "
599 pci_acpi_add_bus_pm_notifier(device
, root
->bus
);
600 if (device
->wakeup
.flags
.run_wake
)
601 device_set_run_wake(root
->bus
->bridge
, true);
603 if (system_state
!= SYSTEM_BOOTING
) {
604 pcibios_resource_survey_bus(root
->bus
);
605 pci_assign_unassigned_bus_resources(root
->bus
);
608 mutex_lock(&acpi_pci_root_lock
);
609 list_for_each_entry(driver
, &acpi_pci_drivers
, node
)
612 mutex_unlock(&acpi_pci_root_lock
);
614 /* need to after hot-added ioapic is registered */
615 if (system_state
!= SYSTEM_BOOTING
)
616 pci_enable_bridges(root
->bus
);
618 pci_bus_add_devices(root
->bus
);
622 mutex_lock(&acpi_pci_root_lock
);
623 list_del(&root
->node
);
624 mutex_unlock(&acpi_pci_root_lock
);
626 acpi_pci_irq_del_prt(root
->segment
, root
->secondary
.start
);
632 static int acpi_pci_root_remove(struct acpi_device
*device
, int type
)
636 struct acpi_pci_root
*root
= acpi_driver_data(device
);
637 struct acpi_pci_driver
*driver
;
639 pci_stop_root_bus(root
->bus
);
641 mutex_lock(&acpi_pci_root_lock
);
642 list_for_each_entry_reverse(driver
, &acpi_pci_drivers
, node
)
644 driver
->remove(root
);
645 mutex_unlock(&acpi_pci_root_lock
);
647 device_set_run_wake(root
->bus
->bridge
, false);
648 pci_acpi_remove_bus_pm_notifier(device
);
650 status
= acpi_get_handle(device
->handle
, METHOD_NAME__PRT
, &handle
);
651 if (ACPI_SUCCESS(status
))
652 acpi_pci_irq_del_prt(root
->segment
, root
->secondary
.start
);
654 pci_remove_root_bus(root
->bus
);
656 mutex_lock(&acpi_pci_root_lock
);
657 list_del(&root
->node
);
658 mutex_unlock(&acpi_pci_root_lock
);
663 int __init
acpi_pci_root_init(void)
667 if (acpi_pci_disabled
)
670 pci_acpi_crs_quirks();
671 if (acpi_bus_register_driver(&acpi_pci_root_driver
) < 0)