blob: 17cbb07ce16c5eef2587271b892f61b2a5e753eb [file] [log] [blame]
Tomasz Nowicki935c7602016-06-10 21:55:13 +02001/*
2 * Copyright (C) 2016 Broadcom
3 * Author: Jayachandran C <jchandra@broadcom.com>
4 * Copyright (C) 2016 Semihalf
5 * Author: Tomasz Nowicki <tn@semihalf.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2, as
9 * published by the Free Software Foundation (the "GPL").
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License version 2 (GPLv2) for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 (GPLv2) along with this source code.
18 */
19
20#define pr_fmt(fmt) "ACPI: " fmt
21
22#include <linux/kernel.h>
23#include <linux/pci.h>
24#include <linux/pci-acpi.h>
Tomasz Nowicki13983eb2016-09-09 21:24:03 +020025#include <linux/pci-ecam.h>
Tomasz Nowicki935c7602016-06-10 21:55:13 +020026
27/* Structure to hold entries from the MCFG table */
28struct mcfg_entry {
29 struct list_head list;
30 phys_addr_t addr;
31 u16 segment;
32 u8 bus_start;
33 u8 bus_end;
34};
35
Tomasz Nowicki5b69b852016-09-09 21:24:04 +020036#ifdef CONFIG_PCI_QUIRKS
37struct mcfg_fixup {
38 char oem_id[ACPI_OEM_ID_SIZE + 1];
39 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
40 u32 oem_revision;
41 u16 segment;
42 struct resource bus_range;
43 struct pci_ecam_ops *ops;
44 struct resource cfgres;
45};
46
47#define MCFG_BUS_RANGE(start, end) DEFINE_RES_NAMED((start), \
48 ((end) - (start) + 1), \
49 NULL, IORESOURCE_BUS)
50#define MCFG_BUS_ANY MCFG_BUS_RANGE(0x0, 0xff)
51
52static struct mcfg_fixup mcfg_quirks[] = {
53/* { OEM_ID, OEM_TABLE_ID, REV, SEGMENT, BUS_RANGE, ops, cfgres }, */
Christopher Covington2ca5b8d2016-11-02 11:11:27 -050054
55#define QCOM_ECAM32(seg) \
56 { "QCOM ", "QDF2432 ", 1, seg, MCFG_BUS_ANY, &pci_32b_ops }
57 QCOM_ECAM32(0),
58 QCOM_ECAM32(1),
59 QCOM_ECAM32(2),
60 QCOM_ECAM32(3),
61 QCOM_ECAM32(4),
62 QCOM_ECAM32(5),
63 QCOM_ECAM32(6),
64 QCOM_ECAM32(7),
Dongdong Liu5f00f1a2016-12-01 00:45:35 -060065
66#define HISI_QUAD_DOM(table_id, seg, ops) \
67 { "HISI ", table_id, 0, (seg) + 0, MCFG_BUS_ANY, ops }, \
68 { "HISI ", table_id, 0, (seg) + 1, MCFG_BUS_ANY, ops }, \
69 { "HISI ", table_id, 0, (seg) + 2, MCFG_BUS_ANY, ops }, \
70 { "HISI ", table_id, 0, (seg) + 3, MCFG_BUS_ANY, ops }
71 HISI_QUAD_DOM("HIP05 ", 0, &hisi_pcie_ops),
72 HISI_QUAD_DOM("HIP06 ", 0, &hisi_pcie_ops),
73 HISI_QUAD_DOM("HIP07 ", 0, &hisi_pcie_ops),
74 HISI_QUAD_DOM("HIP07 ", 4, &hisi_pcie_ops),
75 HISI_QUAD_DOM("HIP07 ", 8, &hisi_pcie_ops),
76 HISI_QUAD_DOM("HIP07 ", 12, &hisi_pcie_ops),
Tomasz Nowicki44f22bd2016-12-01 00:07:56 -060077
78#define THUNDER_PEM_RES(addr, node) \
79 DEFINE_RES_MEM((addr) + ((u64) (node) << 44), 0x39 * SZ_16M)
80#define THUNDER_PEM_QUIRK(rev, node) \
81 { "CAVIUM", "THUNDERX", rev, 4 + (10 * (node)), MCFG_BUS_ANY, \
82 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x88001f000000UL, node) }, \
83 { "CAVIUM", "THUNDERX", rev, 5 + (10 * (node)), MCFG_BUS_ANY, \
84 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x884057000000UL, node) }, \
85 { "CAVIUM", "THUNDERX", rev, 6 + (10 * (node)), MCFG_BUS_ANY, \
86 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x88808f000000UL, node) }, \
87 { "CAVIUM", "THUNDERX", rev, 7 + (10 * (node)), MCFG_BUS_ANY, \
88 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x89001f000000UL, node) }, \
89 { "CAVIUM", "THUNDERX", rev, 8 + (10 * (node)), MCFG_BUS_ANY, \
90 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x894057000000UL, node) }, \
91 { "CAVIUM", "THUNDERX", rev, 9 + (10 * (node)), MCFG_BUS_ANY, \
92 &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x89808f000000UL, node) }
93 /* SoC pass2.x */
94 THUNDER_PEM_QUIRK(1, 0),
95 THUNDER_PEM_QUIRK(1, 1),
Tomasz Nowicki5b69b852016-09-09 21:24:04 +020096};
97
98static char mcfg_oem_id[ACPI_OEM_ID_SIZE];
99static char mcfg_oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
100static u32 mcfg_oem_revision;
101
102static int pci_mcfg_quirk_matches(struct mcfg_fixup *f, u16 segment,
103 struct resource *bus_range)
104{
105 if (!memcmp(f->oem_id, mcfg_oem_id, ACPI_OEM_ID_SIZE) &&
106 !memcmp(f->oem_table_id, mcfg_oem_table_id,
107 ACPI_OEM_TABLE_ID_SIZE) &&
108 f->oem_revision == mcfg_oem_revision &&
109 f->segment == segment &&
110 resource_contains(&f->bus_range, bus_range))
111 return 1;
112
113 return 0;
114}
115#endif
116
117static void pci_mcfg_apply_quirks(struct acpi_pci_root *root,
118 struct resource *cfgres,
119 struct pci_ecam_ops **ecam_ops)
120{
121#ifdef CONFIG_PCI_QUIRKS
122 u16 segment = root->segment;
123 struct resource *bus_range = &root->secondary;
124 struct mcfg_fixup *f;
125 int i;
126
127 for (i = 0, f = mcfg_quirks; i < ARRAY_SIZE(mcfg_quirks); i++, f++) {
128 if (pci_mcfg_quirk_matches(f, segment, bus_range)) {
129 if (f->cfgres.start)
130 *cfgres = f->cfgres;
131 if (f->ops)
132 *ecam_ops = f->ops;
133 dev_info(&root->device->dev, "MCFG quirk: ECAM at %pR for %pR with %ps\n",
134 cfgres, bus_range, *ecam_ops);
135 return;
136 }
137 }
138#endif
139}
140
Tomasz Nowicki935c7602016-06-10 21:55:13 +0200141/* List to save MCFG entries */
142static LIST_HEAD(pci_mcfg_list);
143
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200144int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres,
145 struct pci_ecam_ops **ecam_ops)
Tomasz Nowicki935c7602016-06-10 21:55:13 +0200146{
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200147 struct pci_ecam_ops *ops = &pci_generic_ecam_ops;
148 struct resource *bus_res = &root->secondary;
149 u16 seg = root->segment;
Tomasz Nowicki935c7602016-06-10 21:55:13 +0200150 struct mcfg_entry *e;
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200151 struct resource res;
152
153 /* Use address from _CBA if present, otherwise lookup MCFG */
154 if (root->mcfg_addr)
155 goto skip_lookup;
Tomasz Nowicki935c7602016-06-10 21:55:13 +0200156
157 /*
158 * We expect exact match, unless MCFG entry end bus covers more than
159 * specified by caller.
160 */
161 list_for_each_entry(e, &pci_mcfg_list, list) {
162 if (e->segment == seg && e->bus_start == bus_res->start &&
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200163 e->bus_end >= bus_res->end) {
164 root->mcfg_addr = e->addr;
165 }
166
Tomasz Nowicki935c7602016-06-10 21:55:13 +0200167 }
168
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200169skip_lookup:
170 memset(&res, 0, sizeof(res));
Tomasz Nowicki5b69b852016-09-09 21:24:04 +0200171 if (root->mcfg_addr) {
172 res.start = root->mcfg_addr + (bus_res->start << 20);
173 res.end = res.start + (resource_size(bus_res) << 20) - 1;
174 res.flags = IORESOURCE_MEM;
175 }
176
177 /*
178 * Allow quirks to override default ECAM ops and CFG resource
179 * range. This may even fabricate a CFG resource range in case
180 * MCFG does not have it. Invalid CFG start address means MCFG
181 * firmware bug or we need another quirk in array.
182 */
183 pci_mcfg_apply_quirks(root, &res, &ops);
184 if (!res.start)
185 return -ENXIO;
186
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200187 *cfgres = res;
188 *ecam_ops = ops;
Tomasz Nowicki935c7602016-06-10 21:55:13 +0200189 return 0;
190}
191
192static __init int pci_mcfg_parse(struct acpi_table_header *header)
193{
194 struct acpi_table_mcfg *mcfg;
195 struct acpi_mcfg_allocation *mptr;
196 struct mcfg_entry *e, *arr;
197 int i, n;
198
199 if (header->length < sizeof(struct acpi_table_mcfg))
200 return -EINVAL;
201
202 n = (header->length - sizeof(struct acpi_table_mcfg)) /
203 sizeof(struct acpi_mcfg_allocation);
204 mcfg = (struct acpi_table_mcfg *)header;
205 mptr = (struct acpi_mcfg_allocation *) &mcfg[1];
206
207 arr = kcalloc(n, sizeof(*arr), GFP_KERNEL);
208 if (!arr)
209 return -ENOMEM;
210
211 for (i = 0, e = arr; i < n; i++, mptr++, e++) {
212 e->segment = mptr->pci_segment;
213 e->addr = mptr->address;
214 e->bus_start = mptr->start_bus_number;
215 e->bus_end = mptr->end_bus_number;
216 list_add(&e->list, &pci_mcfg_list);
217 }
218
Tomasz Nowicki5b69b852016-09-09 21:24:04 +0200219#ifdef CONFIG_PCI_QUIRKS
220 /* Save MCFG IDs and revision for quirks matching */
221 memcpy(mcfg_oem_id, header->oem_id, ACPI_OEM_ID_SIZE);
222 memcpy(mcfg_oem_table_id, header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
223 mcfg_oem_revision = header->oem_revision;
224#endif
225
Tomasz Nowicki935c7602016-06-10 21:55:13 +0200226 pr_info("MCFG table detected, %d entries\n", n);
227 return 0;
228}
229
230/* Interface called by ACPI - parse and save MCFG table */
231void __init pci_mmcfg_late_init(void)
232{
233 int err = acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse);
234 if (err)
235 pr_err("Failed to parse MCFG (%d)\n", err);
236}