1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Broadcom
4 * Author: Jayachandran C <jchandra@broadcom.com>
5 * Copyright (C) 2016 Semihalf
6 * Author: Tomasz Nowicki <tn@semihalf.com>
9 #define pr_fmt(fmt) "ACPI: " fmt
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/pci-acpi.h>
14 #include <linux/pci-ecam.h>
16 /* Structure to hold entries from the MCFG table */
18 struct list_head list;
25 #ifdef CONFIG_PCI_QUIRKS
27 char oem_id[ACPI_OEM_ID_SIZE + 1];
28 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
31 struct resource bus_range;
32 const struct pci_ecam_ops *ops;
33 struct resource cfgres;
36 #define MCFG_BUS_RANGE(start, end) DEFINE_RES_NAMED((start), \
37 ((end) - (start) + 1), \
39 #define MCFG_BUS_ANY MCFG_BUS_RANGE(0x0, 0xff)
41 static struct mcfg_fixup mcfg_quirks[] = {
42 /* { OEM_ID, OEM_TABLE_ID, REV, SEGMENT, BUS_RANGE, ops, cfgres }, */
46 #define AL_ECAM(table_id, rev, seg, ops) \
47 { "AMAZON", table_id, rev, seg, MCFG_BUS_ANY, ops }
49 AL_ECAM("GRAVITON", 0, 0, &al_pcie_ops),
50 AL_ECAM("GRAVITON", 0, 1, &al_pcie_ops),
51 AL_ECAM("GRAVITON", 0, 2, &al_pcie_ops),
52 AL_ECAM("GRAVITON", 0, 3, &al_pcie_ops),
53 AL_ECAM("GRAVITON", 0, 4, &al_pcie_ops),
54 AL_ECAM("GRAVITON", 0, 5, &al_pcie_ops),
55 AL_ECAM("GRAVITON", 0, 6, &al_pcie_ops),
56 AL_ECAM("GRAVITON", 0, 7, &al_pcie_ops),
58 #define QCOM_ECAM32(seg) \
59 { "QCOM ", "QDF2432 ", 1, seg, MCFG_BUS_ANY, &pci_32b_ops }
70 #define HISI_QUAD_DOM(table_id, seg, ops) \
71 { "HISI ", table_id, 0, (seg) + 0, MCFG_BUS_ANY, ops }, \
72 { "HISI ", table_id, 0, (seg) + 1, MCFG_BUS_ANY, ops }, \
73 { "HISI ", table_id, 0, (seg) + 2, MCFG_BUS_ANY, ops }, \
74 { "HISI ", table_id, 0, (seg) + 3, MCFG_BUS_ANY, ops }
76 HISI_QUAD_DOM("HIP05 ", 0, &hisi_pcie_ops),
77 HISI_QUAD_DOM("HIP06 ", 0, &hisi_pcie_ops),
78 HISI_QUAD_DOM("HIP07 ", 0, &hisi_pcie_ops),
79 HISI_QUAD_DOM("HIP07 ", 4, &hisi_pcie_ops),
80 HISI_QUAD_DOM("HIP07 ", 8, &hisi_pcie_ops),
81 HISI_QUAD_DOM("HIP07 ", 12, &hisi_pcie_ops),
83 #define THUNDER_PEM_RES(addr, node) \
84 DEFINE_RES_MEM((addr) + ((u64) (node) << 44), 0x39 * SZ_16M)
86 #define THUNDER_PEM_QUIRK(rev, node) \
87 { "CAVIUM", "THUNDERX", rev, 4 + (10 * (node)), MCFG_BUS_ANY, \
88 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x88001f000000UL, node) }, \
89 { "CAVIUM", "THUNDERX", rev, 5 + (10 * (node)), MCFG_BUS_ANY, \
90 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x884057000000UL, node) }, \
91 { "CAVIUM", "THUNDERX", rev, 6 + (10 * (node)), MCFG_BUS_ANY, \
92 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x88808f000000UL, node) }, \
93 { "CAVIUM", "THUNDERX", rev, 7 + (10 * (node)), MCFG_BUS_ANY, \
94 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x89001f000000UL, node) }, \
95 { "CAVIUM", "THUNDERX", rev, 8 + (10 * (node)), MCFG_BUS_ANY, \
96 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x894057000000UL, node) }, \
97 { "CAVIUM", "THUNDERX", rev, 9 + (10 * (node)), MCFG_BUS_ANY, \
98 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x89808f000000UL, node) }
100 #define THUNDER_ECAM_QUIRK(rev, seg) \
101 { "CAVIUM", "THUNDERX", rev, seg, MCFG_BUS_ANY, \
102 &pci_thunder_ecam_ops }
105 THUNDER_PEM_QUIRK(1, 0),
106 THUNDER_PEM_QUIRK(1, 1),
107 THUNDER_ECAM_QUIRK(1, 10),
110 THUNDER_PEM_QUIRK(2, 0), /* off-chip devices */
111 THUNDER_PEM_QUIRK(2, 1), /* off-chip devices */
112 THUNDER_ECAM_QUIRK(2, 0),
113 THUNDER_ECAM_QUIRK(2, 1),
114 THUNDER_ECAM_QUIRK(2, 2),
115 THUNDER_ECAM_QUIRK(2, 3),
116 THUNDER_ECAM_QUIRK(2, 10),
117 THUNDER_ECAM_QUIRK(2, 11),
118 THUNDER_ECAM_QUIRK(2, 12),
119 THUNDER_ECAM_QUIRK(2, 13),
121 #define XGENE_V1_ECAM_MCFG(rev, seg) \
122 {"APM ", "XGENE ", rev, seg, MCFG_BUS_ANY, \
123 &xgene_v1_pcie_ecam_ops }
125 #define XGENE_V2_ECAM_MCFG(rev, seg) \
126 {"APM ", "XGENE ", rev, seg, MCFG_BUS_ANY, \
127 &xgene_v2_pcie_ecam_ops }
129 /* X-Gene SoC with v1 PCIe controller */
130 XGENE_V1_ECAM_MCFG(1, 0),
131 XGENE_V1_ECAM_MCFG(1, 1),
132 XGENE_V1_ECAM_MCFG(1, 2),
133 XGENE_V1_ECAM_MCFG(1, 3),
134 XGENE_V1_ECAM_MCFG(1, 4),
135 XGENE_V1_ECAM_MCFG(2, 0),
136 XGENE_V1_ECAM_MCFG(2, 1),
137 XGENE_V1_ECAM_MCFG(2, 2),
138 XGENE_V1_ECAM_MCFG(2, 3),
139 XGENE_V1_ECAM_MCFG(2, 4),
140 /* X-Gene SoC with v2.1 PCIe controller */
141 XGENE_V2_ECAM_MCFG(3, 0),
142 XGENE_V2_ECAM_MCFG(3, 1),
143 /* X-Gene SoC with v2.2 PCIe controller */
144 XGENE_V2_ECAM_MCFG(4, 0),
145 XGENE_V2_ECAM_MCFG(4, 1),
146 XGENE_V2_ECAM_MCFG(4, 2),
148 #define ALTRA_ECAM_QUIRK(rev, seg) \
149 { "Ampere", "Altra ", rev, seg, MCFG_BUS_ANY, &pci_32b_read_ops }
151 ALTRA_ECAM_QUIRK(1, 0),
152 ALTRA_ECAM_QUIRK(1, 1),
153 ALTRA_ECAM_QUIRK(1, 2),
154 ALTRA_ECAM_QUIRK(1, 3),
155 ALTRA_ECAM_QUIRK(1, 4),
156 ALTRA_ECAM_QUIRK(1, 5),
157 ALTRA_ECAM_QUIRK(1, 6),
158 ALTRA_ECAM_QUIRK(1, 7),
159 ALTRA_ECAM_QUIRK(1, 8),
160 ALTRA_ECAM_QUIRK(1, 9),
161 ALTRA_ECAM_QUIRK(1, 10),
162 ALTRA_ECAM_QUIRK(1, 11),
163 ALTRA_ECAM_QUIRK(1, 12),
164 ALTRA_ECAM_QUIRK(1, 13),
165 ALTRA_ECAM_QUIRK(1, 14),
166 ALTRA_ECAM_QUIRK(1, 15),
170 static char mcfg_oem_id[ACPI_OEM_ID_SIZE];
171 static char mcfg_oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
172 static u32 mcfg_oem_revision;
174 static int pci_mcfg_quirk_matches(struct mcfg_fixup *f, u16 segment,
175 struct resource *bus_range)
177 if (!memcmp(f->oem_id, mcfg_oem_id, ACPI_OEM_ID_SIZE) &&
178 !memcmp(f->oem_table_id, mcfg_oem_table_id,
179 ACPI_OEM_TABLE_ID_SIZE) &&
180 f->oem_revision == mcfg_oem_revision &&
181 f->segment == segment &&
182 resource_contains(&f->bus_range, bus_range))
189 static void pci_mcfg_apply_quirks(struct acpi_pci_root *root,
190 struct resource *cfgres,
191 const struct pci_ecam_ops **ecam_ops)
193 #ifdef CONFIG_PCI_QUIRKS
194 u16 segment = root->segment;
195 struct resource *bus_range = &root->secondary;
196 struct mcfg_fixup *f;
199 for (i = 0, f = mcfg_quirks; i < ARRAY_SIZE(mcfg_quirks); i++, f++) {
200 if (pci_mcfg_quirk_matches(f, segment, bus_range)) {
205 dev_info(&root->device->dev, "MCFG quirk: ECAM at %pR for %pR with %ps\n",
206 cfgres, bus_range, *ecam_ops);
213 /* List to save MCFG entries */
214 static LIST_HEAD(pci_mcfg_list);
216 int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres,
217 const struct pci_ecam_ops **ecam_ops)
219 const struct pci_ecam_ops *ops = &pci_generic_ecam_ops;
220 struct resource *bus_res = &root->secondary;
221 u16 seg = root->segment;
222 struct mcfg_entry *e;
225 /* Use address from _CBA if present, otherwise lookup MCFG */
230 * We expect the range in bus_res in the coverage of MCFG bus range.
232 list_for_each_entry(e, &pci_mcfg_list, list) {
233 if (e->segment == seg && e->bus_start <= bus_res->start &&
234 e->bus_end >= bus_res->end) {
235 root->mcfg_addr = e->addr;
241 memset(&res, 0, sizeof(res));
242 if (root->mcfg_addr) {
243 res.start = root->mcfg_addr + (bus_res->start << 20);
244 res.end = res.start + (resource_size(bus_res) << 20) - 1;
245 res.flags = IORESOURCE_MEM;
249 * Allow quirks to override default ECAM ops and CFG resource
250 * range. This may even fabricate a CFG resource range in case
251 * MCFG does not have it. Invalid CFG start address means MCFG
252 * firmware bug or we need another quirk in array.
254 pci_mcfg_apply_quirks(root, &res, &ops);
263 static __init int pci_mcfg_parse(struct acpi_table_header *header)
265 struct acpi_table_mcfg *mcfg;
266 struct acpi_mcfg_allocation *mptr;
267 struct mcfg_entry *e, *arr;
270 if (header->length < sizeof(struct acpi_table_mcfg))
273 n = (header->length - sizeof(struct acpi_table_mcfg)) /
274 sizeof(struct acpi_mcfg_allocation);
275 mcfg = (struct acpi_table_mcfg *)header;
276 mptr = (struct acpi_mcfg_allocation *) &mcfg[1];
278 arr = kcalloc(n, sizeof(*arr), GFP_KERNEL);
282 for (i = 0, e = arr; i < n; i++, mptr++, e++) {
283 e->segment = mptr->pci_segment;
284 e->addr = mptr->address;
285 e->bus_start = mptr->start_bus_number;
286 e->bus_end = mptr->end_bus_number;
287 list_add(&e->list, &pci_mcfg_list);
290 #ifdef CONFIG_PCI_QUIRKS
291 /* Save MCFG IDs and revision for quirks matching */
292 memcpy(mcfg_oem_id, header->oem_id, ACPI_OEM_ID_SIZE);
293 memcpy(mcfg_oem_table_id, header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
294 mcfg_oem_revision = header->oem_revision;
297 pr_info("MCFG table detected, %d entries\n", n);
301 /* Interface called by ACPI - parse and save MCFG table */
302 void __init pci_mmcfg_late_init(void)
304 int err = acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse);
306 pr_debug("Failed to parse MCFG (%d)\n", err);