blob: 96d55ad8ff22e4b5bc7efdc02e16c3011b8fcf29 [file] [log] [blame]
Andrew Geissler220dafd2023-10-04 10:18:08 -05001From 268bfbcd8f16660bf5fc8e31b18b4090743c6dbe Mon Sep 17 00:00:00 2001
Andrew Geissler2daf84b2023-03-31 09:57:23 -05002From: Deepak Pandey <Deepak.Pandey@arm.com>
3Date: Fri, 31 May 2019 16:42:43 +0100
Patrick Williams8dd68482022-10-04 07:57:18 -05004Subject: [PATCH] pcie: Add quirk for the Arm Neoverse N1SDP platform
Brad Bishopbec4ebc2022-08-03 09:55:16 -04005
6The Arm N1SDP SoC suffers from some PCIe integration issues, most
7prominently config space accesses to not existing BDFs being answered
8with a bus abort, resulting in an SError.
9To mitigate this, the firmware scans the bus before boot (catching the
10SErrors) and creates a table with valid BDFs, which acts as a filter for
11Linux' config space accesses.
12
13Add code consulting the table as an ACPI PCIe quirk, also register the
14corresponding device tree based description of the host controller.
15Also fix the other two minor issues on the way, namely not being fully
16ECAM compliant and config space accesses being restricted to 32-bit
17accesses only.
18
19This allows the Arm Neoverse N1SDP board to boot Linux without crashing
20and to access *any* devices (there are no platform devices except UART).
21
22Signed-off-by: Deepak Pandey <Deepak.Pandey@arm.com>
23[Sudipto: extend to cover the CCIX root port as well]
24Signed-off-by: Sudipto Paul <sudipto.paul@arm.com>
25[Andre: fix coding style issues, rewrite some parts, add DT support]
26Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Patrick Williams8dd68482022-10-04 07:57:18 -050027
Andrew Geissler2daf84b2023-03-31 09:57:23 -050028Change-Id: I1d3a4b9bf6b3b883d262e3c4ff1f88a0eb81c1fe
Patrick Williams8dd68482022-10-04 07:57:18 -050029Upstream-Status: Inappropriate [will not be submitted as its a workaround to address hardware issue]
30Signed-off-by: Deepak Pandey <Deepak.Pandey@arm.com>
Patrick Williams2194f502022-10-16 14:26:09 -050031Signed-off-by: Vishnu Banavath <vishnu.banavath@arm.com>
32Signed-off-by: Adam Johnston <adam.johnston@arm.com>
Brad Bishopbec4ebc2022-08-03 09:55:16 -040033---
34 arch/arm64/configs/defconfig | 1 +
35 drivers/acpi/pci_mcfg.c | 7 +
36 drivers/pci/controller/Kconfig | 11 ++
Andrew Geissler2daf84b2023-03-31 09:57:23 -050037 drivers/pci/controller/Makefile | 2 +-
Brad Bishopbec4ebc2022-08-03 09:55:16 -040038 drivers/pci/controller/pcie-n1sdp.c | 198 ++++++++++++++++++++++++++++
39 include/linux/pci-ecam.h | 2 +
Andrew Geissler2daf84b2023-03-31 09:57:23 -050040 6 files changed, 220 insertions(+), 1 deletion(-)
Brad Bishopbec4ebc2022-08-03 09:55:16 -040041 create mode 100644 drivers/pci/controller/pcie-n1sdp.c
42
43diff --git a/arch/arm64/configs/defconfig b/arch/arm64/configs/defconfig
Andrew Geissler220dafd2023-10-04 10:18:08 -050044index a24609e14d50..cd73d1704dd2 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040045--- a/arch/arm64/configs/defconfig
46+++ b/arch/arm64/configs/defconfig
Andrew Geissler220dafd2023-10-04 10:18:08 -050047@@ -203,6 +203,7 @@ CONFIG_NFC_S3FWRN5_I2C=m
Andrew Geissler2daf84b2023-03-31 09:57:23 -050048 CONFIG_PCI=y
49 CONFIG_PCIEPORTBUS=y
50 CONFIG_PCIEAER=y
Brad Bishopbec4ebc2022-08-03 09:55:16 -040051+CONFIG_PCI_QUIRKS=y
Andrew Geissler2daf84b2023-03-31 09:57:23 -050052 CONFIG_PCI_IOV=y
53 CONFIG_PCI_PASID=y
54 CONFIG_HOTPLUG_PCI=y
Brad Bishopbec4ebc2022-08-03 09:55:16 -040055diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
Andrew Geissler2daf84b2023-03-31 09:57:23 -050056index 860014b89b8e..2d4c1c699ffe 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040057--- a/drivers/acpi/pci_mcfg.c
58+++ b/drivers/acpi/pci_mcfg.c
Patrick Williams2194f502022-10-16 14:26:09 -050059@@ -171,6 +171,13 @@ static struct mcfg_fixup mcfg_quirks[] = {
60 ALTRA_ECAM_QUIRK(1, 13),
61 ALTRA_ECAM_QUIRK(1, 14),
62 ALTRA_ECAM_QUIRK(1, 15),
63+
Brad Bishopbec4ebc2022-08-03 09:55:16 -040064+#define N1SDP_ECAM_MCFG(rev, seg, ops) \
65+ {"ARMLTD", "ARMN1SDP", rev, seg, MCFG_BUS_ANY, ops }
66+
67+ /* N1SDP SoC with v1 PCIe controller */
68+ N1SDP_ECAM_MCFG(0x20181101, 0, &pci_n1sdp_pcie_ecam_ops),
69+ N1SDP_ECAM_MCFG(0x20181101, 1, &pci_n1sdp_ccix_ecam_ops),
Patrick Williams2194f502022-10-16 14:26:09 -050070 #endif /* ARM64 */
Brad Bishopbec4ebc2022-08-03 09:55:16 -040071
Andrew Geissler2daf84b2023-03-31 09:57:23 -050072 #ifdef CONFIG_LOONGARCH
Brad Bishopbec4ebc2022-08-03 09:55:16 -040073diff --git a/drivers/pci/controller/Kconfig b/drivers/pci/controller/Kconfig
Andrew Geissler220dafd2023-10-04 10:18:08 -050074index 8d49bad7f847..7bb49afbcd5b 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040075--- a/drivers/pci/controller/Kconfig
76+++ b/drivers/pci/controller/Kconfig
Andrew Geissler220dafd2023-10-04 10:18:08 -050077@@ -21,6 +21,17 @@ config PCIE_ALTERA
78 Say Y here if you want to enable PCIe controller support on Altera
79 FPGA.
Brad Bishopbec4ebc2022-08-03 09:55:16 -040080
81+config PCIE_HOST_N1SDP_ECAM
82+ bool "ARM N1SDP PCIe Controller"
83+ depends on ARM64
84+ depends on OF || (ACPI && PCI_QUIRKS)
85+ select PCI_HOST_COMMON
86+ default y if ARCH_VEXPRESS
87+ help
88+ Say Y here if you want PCIe support for the Arm N1SDP platform.
89+ The controller is ECAM compliant, but needs a quirk to workaround
90+ an integration issue.
91+
Andrew Geissler220dafd2023-10-04 10:18:08 -050092 config PCIE_ALTERA_MSI
93 tristate "Altera PCIe MSI feature"
94 depends on PCIE_ALTERA
Brad Bishopbec4ebc2022-08-03 09:55:16 -040095diff --git a/drivers/pci/controller/Makefile b/drivers/pci/controller/Makefile
Andrew Geissler2daf84b2023-03-31 09:57:23 -050096index 37c8663de7fe..08e5afcf6e86 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040097--- a/drivers/pci/controller/Makefile
98+++ b/drivers/pci/controller/Makefile
Andrew Geissler2daf84b2023-03-31 09:57:23 -050099@@ -39,7 +39,7 @@ obj-$(CONFIG_PCI_LOONGSON) += pci-loongson.o
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400100 obj-$(CONFIG_PCIE_HISI_ERR) += pcie-hisi-error.o
Patrick Williams2194f502022-10-16 14:26:09 -0500101 obj-$(CONFIG_PCIE_APPLE) += pcie-apple.o
102 obj-$(CONFIG_PCIE_MT7621) += pcie-mt7621.o
Andrew Geissler2daf84b2023-03-31 09:57:23 -0500103-
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400104+obj-$(CONFIG_PCIE_HOST_N1SDP_ECAM) += pcie-n1sdp.o
105 # pcie-hisi.o quirks are needed even without CONFIG_PCIE_DW
106 obj-y += dwc/
Andrew Geissler2daf84b2023-03-31 09:57:23 -0500107 obj-y += mobiveil/
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400108diff --git a/drivers/pci/controller/pcie-n1sdp.c b/drivers/pci/controller/pcie-n1sdp.c
109new file mode 100644
110index 000000000000..408699b9dcb1
111--- /dev/null
112+++ b/drivers/pci/controller/pcie-n1sdp.c
113@@ -0,0 +1,198 @@
114+// SPDX-License-Identifier: GPL-2.0
115+/*
116+ * Copyright (C) 2018/2019 ARM Ltd.
117+ *
118+ * This quirk is to mask the following issues:
119+ * - PCIE SLVERR: config space accesses to invalid PCIe BDFs cause a bus
120+ * error (signalled as an asynchronous SError)
121+ * - MCFG BDF mapping: the root complex is mapped separately from the device
122+ * config space
123+ * - Non 32-bit accesses to config space are not supported.
124+ *
125+ * At boot time the SCP board firmware creates a discovery table with
126+ * the root complex' base address and the valid BDF values, discovered while
127+ * scanning the config space and catching the SErrors.
128+ * Linux responds only to the EPs listed in this table, returning NULL
129+ * for the rest.
130+ */
131+
132+#include <linux/kernel.h>
133+#include <linux/init.h>
134+#include <linux/ioport.h>
135+#include <linux/sizes.h>
136+#include <linux/of_pci.h>
137+#include <linux/of.h>
138+#include <linux/pci-ecam.h>
139+#include <linux/platform_device.h>
140+#include <linux/module.h>
141+
142+#include "../pci.h"
143+
144+/* Platform specific values as hardcoded in the firmware. */
145+#define AP_NS_SHARED_MEM_BASE 0x06000000
146+#define MAX_SEGMENTS 2 /* Two PCIe root complexes. */
147+#define BDF_TABLE_SIZE SZ_16K
148+
149+/*
150+ * Shared memory layout as written by the SCP upon boot time:
151+ * ----
152+ * Discover data header --> RC base address
153+ * \-> BDF Count
154+ * Discover data --> BDF 0...n
155+ * ----
156+ */
157+struct pcie_discovery_data {
158+ u32 rc_base_addr;
159+ u32 nr_bdfs;
160+ u32 valid_bdfs[0];
161+} *pcie_discovery_data[MAX_SEGMENTS];
162+
163+void __iomem *rc_remapped_addr[MAX_SEGMENTS];
164+
165+/*
166+ * map_bus() is called before we do a config space access for a certain
167+ * device. We use this to check whether this device is valid, avoiding
168+ * config space accesses which would result in an SError otherwise.
169+ */
170+static void __iomem *pci_n1sdp_map_bus(struct pci_bus *bus, unsigned int devfn,
171+ int where)
172+{
173+ struct pci_config_window *cfg = bus->sysdata;
174+ unsigned int devfn_shift = cfg->ops->bus_shift - 8;
175+ unsigned int busn = bus->number;
176+ unsigned int segment = bus->domain_nr;
177+ unsigned int bdf_addr;
178+ unsigned int table_count, i;
179+ struct pci_dev *dev;
180+
181+ if (segment >= MAX_SEGMENTS ||
182+ busn < cfg->busr.start || busn > cfg->busr.end)
183+ return NULL;
184+
185+ /* The PCIe root complex has a separate config space mapping. */
186+ if (busn == 0 && devfn == 0)
187+ return rc_remapped_addr[segment] + where;
188+
189+ dev = pci_get_domain_bus_and_slot(segment, busn, devfn);
190+ if (dev && dev->is_virtfn)
191+ return pci_ecam_map_bus(bus, devfn, where);
192+
193+ /* Accesses beyond the vendor ID always go to existing devices. */
194+ if (where > 0)
195+ return pci_ecam_map_bus(bus, devfn, where);
196+
197+ busn -= cfg->busr.start;
198+ bdf_addr = (busn << cfg->ops->bus_shift) + (devfn << devfn_shift);
199+ table_count = pcie_discovery_data[segment]->nr_bdfs;
200+ for (i = 0; i < table_count; i++) {
201+ if (bdf_addr == pcie_discovery_data[segment]->valid_bdfs[i])
202+ return pci_ecam_map_bus(bus, devfn, where);
203+ }
204+
205+ return NULL;
206+}
207+
208+static int pci_n1sdp_init(struct pci_config_window *cfg, unsigned int segment)
209+{
210+ phys_addr_t table_base;
211+ struct device *dev = cfg->parent;
212+ struct pcie_discovery_data *shared_data;
213+ size_t bdfs_size;
214+
215+ if (segment >= MAX_SEGMENTS)
216+ return -ENODEV;
217+
218+ table_base = AP_NS_SHARED_MEM_BASE + segment * BDF_TABLE_SIZE;
219+
220+ if (!request_mem_region(table_base, BDF_TABLE_SIZE,
221+ "PCIe valid BDFs")) {
222+ dev_err(dev, "PCIe BDF shared region request failed\n");
223+ return -ENOMEM;
224+ }
225+
226+ shared_data = devm_ioremap(dev,
227+ table_base, BDF_TABLE_SIZE);
228+ if (!shared_data)
229+ return -ENOMEM;
230+
231+ /* Copy the valid BDFs structure to allocated normal memory. */
232+ bdfs_size = sizeof(struct pcie_discovery_data) +
233+ sizeof(u32) * shared_data->nr_bdfs;
234+ pcie_discovery_data[segment] = devm_kmalloc(dev, bdfs_size, GFP_KERNEL);
235+ if (!pcie_discovery_data[segment])
236+ return -ENOMEM;
237+
238+ memcpy_fromio(pcie_discovery_data[segment], shared_data, bdfs_size);
239+
240+ rc_remapped_addr[segment] = devm_ioremap(dev,
241+ shared_data->rc_base_addr,
242+ PCI_CFG_SPACE_EXP_SIZE);
243+ if (!rc_remapped_addr[segment]) {
244+ dev_err(dev, "Cannot remap root port base\n");
245+ return -ENOMEM;
246+ }
247+
248+ devm_iounmap(dev, shared_data);
249+
250+ return 0;
251+}
252+
253+/* Called for ACPI segment 0, and for all segments when using DT. */
254+static int pci_n1sdp_pcie_init(struct pci_config_window *cfg)
255+{
256+ struct platform_device *pdev = to_platform_device(cfg->parent);
257+ int segment = 0;
258+
259+ if (pdev->dev.of_node)
260+ segment = of_get_pci_domain_nr(pdev->dev.of_node);
261+ if (segment < 0 || segment > MAX_SEGMENTS) {
262+ dev_err(&pdev->dev, "N1SDP PCI controllers require linux,pci-domain property\n");
263+ dev_err(&pdev->dev, "Or invalid segment number, must be smaller than %d\n",
264+ MAX_SEGMENTS);
265+ return -EINVAL;
266+ }
267+
268+ return pci_n1sdp_init(cfg, segment);
269+}
270+
271+/* Called for ACPI segment 1. */
272+static int pci_n1sdp_ccix_init(struct pci_config_window *cfg)
273+{
274+ return pci_n1sdp_init(cfg, 1);
275+}
276+
277+const struct pci_ecam_ops pci_n1sdp_pcie_ecam_ops = {
278+ .bus_shift = 20,
279+ .init = pci_n1sdp_pcie_init,
280+ .pci_ops = {
281+ .map_bus = pci_n1sdp_map_bus,
282+ .read = pci_generic_config_read32,
283+ .write = pci_generic_config_write32,
284+ }
285+};
286+
287+const struct pci_ecam_ops pci_n1sdp_ccix_ecam_ops = {
288+ .bus_shift = 20,
289+ .init = pci_n1sdp_ccix_init,
290+ .pci_ops = {
291+ .map_bus = pci_n1sdp_map_bus,
292+ .read = pci_generic_config_read32,
293+ .write = pci_generic_config_write32,
294+ }
295+};
296+
297+static const struct of_device_id n1sdp_pcie_of_match[] = {
298+ { .compatible = "arm,n1sdp-pcie", .data = &pci_n1sdp_pcie_ecam_ops },
299+ { },
300+};
301+MODULE_DEVICE_TABLE(of, n1sdp_pcie_of_match);
302+
303+static struct platform_driver n1sdp_pcie_driver = {
304+ .driver = {
305+ .name = KBUILD_MODNAME,
306+ .of_match_table = n1sdp_pcie_of_match,
307+ .suppress_bind_attrs = true,
308+ },
309+ .probe = pci_host_common_probe,
310+};
311+builtin_platform_driver(n1sdp_pcie_driver);
312diff --git a/include/linux/pci-ecam.h b/include/linux/pci-ecam.h
Andrew Geissler2daf84b2023-03-31 09:57:23 -0500313index 6b1301e2498e..b3cf3adeab28 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400314--- a/include/linux/pci-ecam.h
315+++ b/include/linux/pci-ecam.h
Andrew Geissler2daf84b2023-03-31 09:57:23 -0500316@@ -88,6 +88,8 @@ extern const struct pci_ecam_ops xgene_v2_pcie_ecam_ops; /* APM X-Gene PCIe v2.x
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400317 extern const struct pci_ecam_ops al_pcie_ops; /* Amazon Annapurna Labs PCIe */
318 extern const struct pci_ecam_ops tegra194_pcie_ops; /* Tegra194 PCIe */
Andrew Geissler2daf84b2023-03-31 09:57:23 -0500319 extern const struct pci_ecam_ops loongson_pci_ecam_ops; /* Loongson PCIe */
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400320+extern const struct pci_ecam_ops pci_n1sdp_pcie_ecam_ops; /* Arm N1SDP PCIe */
321+extern const struct pci_ecam_ops pci_n1sdp_ccix_ecam_ops; /* Arm N1SDP PCIe */
322 #endif
323
324 #if IS_ENABLED(CONFIG_PCI_HOST_COMMON)