1 // SPDX-License-Identifier: GPL-2.0
3 * Access ACPI _OSC method
5 * Copyright (C) 2006 Intel Corp.
6 * Tom Long Nguyen (tom.l.nguyen@intel.com)
7 * Zhang Yanmin (yanmin.zhang@intel.com)
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
16 #include <linux/suspend.h>
17 #include <linux/acpi.h>
18 #include <linux/pci-acpi.h>
19 #include <linux/delay.h>
20 #include <acpi/apei.h>
23 #ifdef CONFIG_ACPI_APEI
24 static inline int hest_match_pci(struct acpi_hest_aer_common
*p
,
27 return ACPI_HEST_SEGMENT(p
->bus
) == pci_domain_nr(pci
->bus
) &&
28 ACPI_HEST_BUS(p
->bus
) == pci
->bus
->number
&&
29 p
->device
== PCI_SLOT(pci
->devfn
) &&
30 p
->function
== PCI_FUNC(pci
->devfn
);
33 static inline bool hest_match_type(struct acpi_hest_header
*hest_hdr
,
36 u16 hest_type
= hest_hdr
->type
;
37 u8 pcie_type
= pci_pcie_type(dev
);
39 if ((hest_type
== ACPI_HEST_TYPE_AER_ROOT_PORT
&&
40 pcie_type
== PCI_EXP_TYPE_ROOT_PORT
) ||
41 (hest_type
== ACPI_HEST_TYPE_AER_ENDPOINT
&&
42 pcie_type
== PCI_EXP_TYPE_ENDPOINT
) ||
43 (hest_type
== ACPI_HEST_TYPE_AER_BRIDGE
&&
44 (dev
->class >> 16) == PCI_BASE_CLASS_BRIDGE
))
49 struct aer_hest_parse_info
{
50 struct pci_dev
*pci_dev
;
54 static int hest_source_is_pcie_aer(struct acpi_hest_header
*hest_hdr
)
56 if (hest_hdr
->type
== ACPI_HEST_TYPE_AER_ROOT_PORT
||
57 hest_hdr
->type
== ACPI_HEST_TYPE_AER_ENDPOINT
||
58 hest_hdr
->type
== ACPI_HEST_TYPE_AER_BRIDGE
)
63 static int aer_hest_parse(struct acpi_hest_header
*hest_hdr
, void *data
)
65 struct aer_hest_parse_info
*info
= data
;
66 struct acpi_hest_aer_common
*p
;
69 if (!hest_source_is_pcie_aer(hest_hdr
))
72 p
= (struct acpi_hest_aer_common
*)(hest_hdr
+ 1);
73 ff
= !!(p
->flags
& ACPI_HEST_FIRMWARE_FIRST
);
76 * If no specific device is supplied, determine whether
77 * FIRMWARE_FIRST is set for *any* PCIe device.
80 info
->firmware_first
|= ff
;
84 /* Otherwise, check the specific device */
85 if (p
->flags
& ACPI_HEST_GLOBAL
) {
86 if (hest_match_type(hest_hdr
, info
->pci_dev
))
87 info
->firmware_first
= ff
;
89 if (hest_match_pci(p
, info
->pci_dev
))
90 info
->firmware_first
= ff
;
95 static void aer_set_firmware_first(struct pci_dev
*pci_dev
)
98 struct aer_hest_parse_info info
= {
103 rc
= apei_hest_parse(aer_hest_parse
, &info
);
106 pci_dev
->__aer_firmware_first
= 0;
108 pci_dev
->__aer_firmware_first
= info
.firmware_first
;
109 pci_dev
->__aer_firmware_first_valid
= 1;
112 int pcie_aer_get_firmware_first(struct pci_dev
*dev
)
114 if (!pci_is_pcie(dev
))
117 if (!dev
->__aer_firmware_first_valid
)
118 aer_set_firmware_first(dev
);
119 return dev
->__aer_firmware_first
;
122 static bool aer_firmware_first
;
125 * aer_acpi_firmware_first - Check if APEI should control AER.
127 bool aer_acpi_firmware_first(void)
129 static bool parsed
= false;
130 struct aer_hest_parse_info info
= {
131 .pci_dev
= NULL
, /* Check all PCIe devices */
136 apei_hest_parse(aer_hest_parse
, &info
);
137 aer_firmware_first
= info
.firmware_first
;
140 return aer_firmware_first
;