Linux 4.16.11
[linux/fpc-iii.git] / drivers / pci / pcie / aer / aerdrv_acpi.c
blobb2019440e8827788bf3af7684df28519e419704d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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)
9 */
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/pm.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>
21 #include "aerdrv.h"
23 #ifdef CONFIG_ACPI_APEI
24 static inline int hest_match_pci(struct acpi_hest_aer_common *p,
25 struct pci_dev *pci)
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,
34 struct pci_dev *dev)
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))
45 return true;
46 return false;
49 struct aer_hest_parse_info {
50 struct pci_dev *pci_dev;
51 int firmware_first;
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)
59 return 1;
60 return 0;
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;
67 int ff;
69 if (!hest_source_is_pcie_aer(hest_hdr))
70 return 0;
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.
79 if (!info->pci_dev) {
80 info->firmware_first |= ff;
81 return 0;
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;
88 } else
89 if (hest_match_pci(p, info->pci_dev))
90 info->firmware_first = ff;
92 return 0;
95 static void aer_set_firmware_first(struct pci_dev *pci_dev)
97 int rc;
98 struct aer_hest_parse_info info = {
99 .pci_dev = pci_dev,
100 .firmware_first = 0,
103 rc = apei_hest_parse(aer_hest_parse, &info);
105 if (rc)
106 pci_dev->__aer_firmware_first = 0;
107 else
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))
115 return 0;
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 */
132 .firmware_first = 0,
135 if (!parsed) {
136 apei_hest_parse(aer_hest_parse, &info);
137 aer_firmware_first = info.firmware_first;
138 parsed = true;
140 return aer_firmware_first;
142 #endif