blob: 4ffe347a17d5abe7fb11f7a521f8a40fd4cc3155 [file] [log] [blame]
Patrick Williams8dd68482022-10-04 07:57:18 -05001From 4877796976647a24a3a9102facd0577586f5ac9a Mon Sep 17 00:00:00 2001
Brad Bishopbec4ebc2022-08-03 09:55:16 -04002From: 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
28Upstream-Status: Inappropriate [will not be submitted as its a workaround to address hardware issue]
29Signed-off-by: Deepak Pandey <Deepak.Pandey@arm.com>
Brad Bishopbec4ebc2022-08-03 09:55:16 -040030---
31 arch/arm64/configs/defconfig | 1 +
32 drivers/acpi/pci_mcfg.c | 7 +
33 drivers/pci/controller/Kconfig | 11 ++
34 drivers/pci/controller/Makefile | 1 +
35 drivers/pci/controller/pcie-n1sdp.c | 198 ++++++++++++++++++++++++++++
36 include/linux/pci-ecam.h | 2 +
37 6 files changed, 220 insertions(+)
38 create mode 100644 drivers/pci/controller/pcie-n1sdp.c
39
40diff --git a/arch/arm64/configs/defconfig b/arch/arm64/configs/defconfig
Patrick Williams8dd68482022-10-04 07:57:18 -050041index c27d0fed2ce2..70ed38cecee9 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040042--- a/arch/arm64/configs/defconfig
43+++ b/arch/arm64/configs/defconfig
Patrick Williams8dd68482022-10-04 07:57:18 -050044@@ -209,6 +209,7 @@ CONFIG_NFC_NCI=m
Brad Bishopbec4ebc2022-08-03 09:55:16 -040045 CONFIG_NFC_S3FWRN5_I2C=m
46 CONFIG_PCI=y
47 CONFIG_PCIEPORTBUS=y
48+CONFIG_PCI_QUIRKS=y
49 CONFIG_PCI_IOV=y
50 CONFIG_PCI_PASID=y
51 CONFIG_HOTPLUG_PCI=y
52diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
Patrick Williams8dd68482022-10-04 07:57:18 -050053index 63b98eae5e75..67c34e6c24a7 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040054--- a/drivers/acpi/pci_mcfg.c
55+++ b/drivers/acpi/pci_mcfg.c
Patrick Williams8dd68482022-10-04 07:57:18 -050056@@ -152,6 +152,13 @@ static struct mcfg_fixup mcfg_quirks[] = {
57 XGENE_V2_ECAM_MCFG(4, 1),
58 XGENE_V2_ECAM_MCFG(4, 2),
59
Brad Bishopbec4ebc2022-08-03 09:55:16 -040060+#define N1SDP_ECAM_MCFG(rev, seg, ops) \
61+ {"ARMLTD", "ARMN1SDP", rev, seg, MCFG_BUS_ANY, ops }
62+
63+ /* N1SDP SoC with v1 PCIe controller */
64+ N1SDP_ECAM_MCFG(0x20181101, 0, &pci_n1sdp_pcie_ecam_ops),
65+ N1SDP_ECAM_MCFG(0x20181101, 1, &pci_n1sdp_ccix_ecam_ops),
Patrick Williams8dd68482022-10-04 07:57:18 -050066+
67 #define ALTRA_ECAM_QUIRK(rev, seg) \
68 { "Ampere", "Altra ", rev, seg, MCFG_BUS_ANY, &pci_32b_read_ops }
Brad Bishopbec4ebc2022-08-03 09:55:16 -040069
Brad Bishopbec4ebc2022-08-03 09:55:16 -040070diff --git a/drivers/pci/controller/Kconfig b/drivers/pci/controller/Kconfig
71index 326f7d13024f..f9700d037c46 100644
72--- a/drivers/pci/controller/Kconfig
73+++ b/drivers/pci/controller/Kconfig
74@@ -46,6 +46,17 @@ config PCI_IXP4XX
75 Say Y here if you want support for the PCI host controller found
76 in the Intel IXP4xx XScale-based network processor SoC.
77
78+config PCIE_HOST_N1SDP_ECAM
79+ bool "ARM N1SDP PCIe Controller"
80+ depends on ARM64
81+ depends on OF || (ACPI && PCI_QUIRKS)
82+ select PCI_HOST_COMMON
83+ default y if ARCH_VEXPRESS
84+ help
85+ Say Y here if you want PCIe support for the Arm N1SDP platform.
86+ The controller is ECAM compliant, but needs a quirk to workaround
87+ an integration issue.
88+
89 config PCI_TEGRA
90 bool "NVIDIA Tegra PCIe controller"
91 depends on ARCH_TEGRA || COMPILE_TEST
92diff --git a/drivers/pci/controller/Makefile b/drivers/pci/controller/Makefile
93index aaf30b3dcc14..2012ab2b7913 100644
94--- a/drivers/pci/controller/Makefile
95+++ b/drivers/pci/controller/Makefile
96@@ -37,6 +37,7 @@ obj-$(CONFIG_VMD) += vmd.o
97 obj-$(CONFIG_PCIE_BRCMSTB) += pcie-brcmstb.o
98 obj-$(CONFIG_PCI_LOONGSON) += pci-loongson.o
99 obj-$(CONFIG_PCIE_HISI_ERR) += pcie-hisi-error.o
100+obj-$(CONFIG_PCIE_HOST_N1SDP_ECAM) += pcie-n1sdp.o
101 # pcie-hisi.o quirks are needed even without CONFIG_PCIE_DW
102 obj-y += dwc/
103 obj-y += mobiveil/
104diff --git a/drivers/pci/controller/pcie-n1sdp.c b/drivers/pci/controller/pcie-n1sdp.c
105new file mode 100644
106index 000000000000..408699b9dcb1
107--- /dev/null
108+++ b/drivers/pci/controller/pcie-n1sdp.c
109@@ -0,0 +1,198 @@
110+// SPDX-License-Identifier: GPL-2.0
111+/*
112+ * Copyright (C) 2018/2019 ARM Ltd.
113+ *
114+ * This quirk is to mask the following issues:
115+ * - PCIE SLVERR: config space accesses to invalid PCIe BDFs cause a bus
116+ * error (signalled as an asynchronous SError)
117+ * - MCFG BDF mapping: the root complex is mapped separately from the device
118+ * config space
119+ * - Non 32-bit accesses to config space are not supported.
120+ *
121+ * At boot time the SCP board firmware creates a discovery table with
122+ * the root complex' base address and the valid BDF values, discovered while
123+ * scanning the config space and catching the SErrors.
124+ * Linux responds only to the EPs listed in this table, returning NULL
125+ * for the rest.
126+ */
127+
128+#include <linux/kernel.h>
129+#include <linux/init.h>
130+#include <linux/ioport.h>
131+#include <linux/sizes.h>
132+#include <linux/of_pci.h>
133+#include <linux/of.h>
134+#include <linux/pci-ecam.h>
135+#include <linux/platform_device.h>
136+#include <linux/module.h>
137+
138+#include "../pci.h"
139+
140+/* Platform specific values as hardcoded in the firmware. */
141+#define AP_NS_SHARED_MEM_BASE 0x06000000
142+#define MAX_SEGMENTS 2 /* Two PCIe root complexes. */
143+#define BDF_TABLE_SIZE SZ_16K
144+
145+/*
146+ * Shared memory layout as written by the SCP upon boot time:
147+ * ----
148+ * Discover data header --> RC base address
149+ * \-> BDF Count
150+ * Discover data --> BDF 0...n
151+ * ----
152+ */
153+struct pcie_discovery_data {
154+ u32 rc_base_addr;
155+ u32 nr_bdfs;
156+ u32 valid_bdfs[0];
157+} *pcie_discovery_data[MAX_SEGMENTS];
158+
159+void __iomem *rc_remapped_addr[MAX_SEGMENTS];
160+
161+/*
162+ * map_bus() is called before we do a config space access for a certain
163+ * device. We use this to check whether this device is valid, avoiding
164+ * config space accesses which would result in an SError otherwise.
165+ */
166+static void __iomem *pci_n1sdp_map_bus(struct pci_bus *bus, unsigned int devfn,
167+ int where)
168+{
169+ struct pci_config_window *cfg = bus->sysdata;
170+ unsigned int devfn_shift = cfg->ops->bus_shift - 8;
171+ unsigned int busn = bus->number;
172+ unsigned int segment = bus->domain_nr;
173+ unsigned int bdf_addr;
174+ unsigned int table_count, i;
175+ struct pci_dev *dev;
176+
177+ if (segment >= MAX_SEGMENTS ||
178+ busn < cfg->busr.start || busn > cfg->busr.end)
179+ return NULL;
180+
181+ /* The PCIe root complex has a separate config space mapping. */
182+ if (busn == 0 && devfn == 0)
183+ return rc_remapped_addr[segment] + where;
184+
185+ dev = pci_get_domain_bus_and_slot(segment, busn, devfn);
186+ if (dev && dev->is_virtfn)
187+ return pci_ecam_map_bus(bus, devfn, where);
188+
189+ /* Accesses beyond the vendor ID always go to existing devices. */
190+ if (where > 0)
191+ return pci_ecam_map_bus(bus, devfn, where);
192+
193+ busn -= cfg->busr.start;
194+ bdf_addr = (busn << cfg->ops->bus_shift) + (devfn << devfn_shift);
195+ table_count = pcie_discovery_data[segment]->nr_bdfs;
196+ for (i = 0; i < table_count; i++) {
197+ if (bdf_addr == pcie_discovery_data[segment]->valid_bdfs[i])
198+ return pci_ecam_map_bus(bus, devfn, where);
199+ }
200+
201+ return NULL;
202+}
203+
204+static int pci_n1sdp_init(struct pci_config_window *cfg, unsigned int segment)
205+{
206+ phys_addr_t table_base;
207+ struct device *dev = cfg->parent;
208+ struct pcie_discovery_data *shared_data;
209+ size_t bdfs_size;
210+
211+ if (segment >= MAX_SEGMENTS)
212+ return -ENODEV;
213+
214+ table_base = AP_NS_SHARED_MEM_BASE + segment * BDF_TABLE_SIZE;
215+
216+ if (!request_mem_region(table_base, BDF_TABLE_SIZE,
217+ "PCIe valid BDFs")) {
218+ dev_err(dev, "PCIe BDF shared region request failed\n");
219+ return -ENOMEM;
220+ }
221+
222+ shared_data = devm_ioremap(dev,
223+ table_base, BDF_TABLE_SIZE);
224+ if (!shared_data)
225+ return -ENOMEM;
226+
227+ /* Copy the valid BDFs structure to allocated normal memory. */
228+ bdfs_size = sizeof(struct pcie_discovery_data) +
229+ sizeof(u32) * shared_data->nr_bdfs;
230+ pcie_discovery_data[segment] = devm_kmalloc(dev, bdfs_size, GFP_KERNEL);
231+ if (!pcie_discovery_data[segment])
232+ return -ENOMEM;
233+
234+ memcpy_fromio(pcie_discovery_data[segment], shared_data, bdfs_size);
235+
236+ rc_remapped_addr[segment] = devm_ioremap(dev,
237+ shared_data->rc_base_addr,
238+ PCI_CFG_SPACE_EXP_SIZE);
239+ if (!rc_remapped_addr[segment]) {
240+ dev_err(dev, "Cannot remap root port base\n");
241+ return -ENOMEM;
242+ }
243+
244+ devm_iounmap(dev, shared_data);
245+
246+ return 0;
247+}
248+
249+/* Called for ACPI segment 0, and for all segments when using DT. */
250+static int pci_n1sdp_pcie_init(struct pci_config_window *cfg)
251+{
252+ struct platform_device *pdev = to_platform_device(cfg->parent);
253+ int segment = 0;
254+
255+ if (pdev->dev.of_node)
256+ segment = of_get_pci_domain_nr(pdev->dev.of_node);
257+ if (segment < 0 || segment > MAX_SEGMENTS) {
258+ dev_err(&pdev->dev, "N1SDP PCI controllers require linux,pci-domain property\n");
259+ dev_err(&pdev->dev, "Or invalid segment number, must be smaller than %d\n",
260+ MAX_SEGMENTS);
261+ return -EINVAL;
262+ }
263+
264+ return pci_n1sdp_init(cfg, segment);
265+}
266+
267+/* Called for ACPI segment 1. */
268+static int pci_n1sdp_ccix_init(struct pci_config_window *cfg)
269+{
270+ return pci_n1sdp_init(cfg, 1);
271+}
272+
273+const struct pci_ecam_ops pci_n1sdp_pcie_ecam_ops = {
274+ .bus_shift = 20,
275+ .init = pci_n1sdp_pcie_init,
276+ .pci_ops = {
277+ .map_bus = pci_n1sdp_map_bus,
278+ .read = pci_generic_config_read32,
279+ .write = pci_generic_config_write32,
280+ }
281+};
282+
283+const struct pci_ecam_ops pci_n1sdp_ccix_ecam_ops = {
284+ .bus_shift = 20,
285+ .init = pci_n1sdp_ccix_init,
286+ .pci_ops = {
287+ .map_bus = pci_n1sdp_map_bus,
288+ .read = pci_generic_config_read32,
289+ .write = pci_generic_config_write32,
290+ }
291+};
292+
293+static const struct of_device_id n1sdp_pcie_of_match[] = {
294+ { .compatible = "arm,n1sdp-pcie", .data = &pci_n1sdp_pcie_ecam_ops },
295+ { },
296+};
297+MODULE_DEVICE_TABLE(of, n1sdp_pcie_of_match);
298+
299+static struct platform_driver n1sdp_pcie_driver = {
300+ .driver = {
301+ .name = KBUILD_MODNAME,
302+ .of_match_table = n1sdp_pcie_of_match,
303+ .suppress_bind_attrs = true,
304+ },
305+ .probe = pci_host_common_probe,
306+};
307+builtin_platform_driver(n1sdp_pcie_driver);
308diff --git a/include/linux/pci-ecam.h b/include/linux/pci-ecam.h
309index adea5a4771cf..e6bbc037cef8 100644
310--- a/include/linux/pci-ecam.h
311+++ b/include/linux/pci-ecam.h
312@@ -87,6 +87,8 @@ extern const struct pci_ecam_ops xgene_v1_pcie_ecam_ops; /* APM X-Gene PCIe v1 *
313 extern const struct pci_ecam_ops xgene_v2_pcie_ecam_ops; /* APM X-Gene PCIe v2.x */
314 extern const struct pci_ecam_ops al_pcie_ops; /* Amazon Annapurna Labs PCIe */
315 extern const struct pci_ecam_ops tegra194_pcie_ops; /* Tegra194 PCIe */
316+extern const struct pci_ecam_ops pci_n1sdp_pcie_ecam_ops; /* Arm N1SDP PCIe */
317+extern const struct pci_ecam_ops pci_n1sdp_ccix_ecam_ops; /* Arm N1SDP PCIe */
318 #endif
319
320 #if IS_ENABLED(CONFIG_PCI_HOST_COMMON)