1From 5aa5769af625c79589fd84b8afc06149c2362218 Mon Sep 17 00:00:00 2001
2From: Deepak Pandey <Deepak.Pandey@arm.com>
3Date: Fri, 31 May 2019 16:42:43 +0100
4Subject: [PATCH] pcie: Add quirk for the Arm Neoverse N1SDP platform
5
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>
27
28Change-Id: I1d3a4b9bf6b3b883d262e3c4ff1f88a0eb81c1fe
29Upstream-Status: Inappropriate [will not be submitted as its a workaround to address hardware issue]
30Signed-off-by: Deepak Pandey <Deepak.Pandey@arm.com>
31Signed-off-by: Vishnu Banavath <vishnu.banavath@arm.com>
32Signed-off-by: Adam Johnston <adam.johnston@arm.com>
33---
34 arch/arm64/configs/defconfig        |   1 +
35 drivers/acpi/pci_mcfg.c             |   7 +
36 drivers/pci/controller/Kconfig      |  11 ++
37 drivers/pci/controller/Makefile     |   2 +-
38 drivers/pci/controller/pcie-n1sdp.c | 198 ++++++++++++++++++++++++++++
39 include/linux/pci-ecam.h            |   2 +
40 6 files changed, 220 insertions(+), 1 deletion(-)
41 create mode 100644 drivers/pci/controller/pcie-n1sdp.c
42
43diff --git a/arch/arm64/configs/defconfig b/arch/arm64/configs/defconfig
44index bbbc31391a65..973aa3b4d407 100644
45--- a/arch/arm64/configs/defconfig
46+++ b/arch/arm64/configs/defconfig
47@@ -214,6 +214,7 @@ CONFIG_NFC_S3FWRN5_I2C=m
48 CONFIG_PCI=y
49 CONFIG_PCIEPORTBUS=y
50 CONFIG_PCIEAER=y
51+CONFIG_PCI_QUIRKS=y
52 CONFIG_PCI_IOV=y
53 CONFIG_PCI_PASID=y
54 CONFIG_HOTPLUG_PCI=y
55diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
56index 860014b89b8e..2d4c1c699ffe 100644
57--- a/drivers/acpi/pci_mcfg.c
58+++ b/drivers/acpi/pci_mcfg.c
59@@ -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+
64+#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),
70 #endif /* ARM64 */
71
72 #ifdef CONFIG_LOONGARCH
73diff --git a/drivers/pci/controller/Kconfig b/drivers/pci/controller/Kconfig
74index bfd9bac37e24..7a65799dded7 100644
75--- a/drivers/pci/controller/Kconfig
76+++ b/drivers/pci/controller/Kconfig
77@@ -50,6 +50,17 @@ config PCI_IXP4XX
78 	  Say Y here if you want support for the PCI host controller found
79 	  in the Intel IXP4xx XScale-based network processor SoC.
80
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+
92 config PCI_TEGRA
93 	bool "NVIDIA Tegra PCIe controller"
94 	depends on ARCH_TEGRA || COMPILE_TEST
95diff --git a/drivers/pci/controller/Makefile b/drivers/pci/controller/Makefile
96index 37c8663de7fe..08e5afcf6e86 100644
97--- a/drivers/pci/controller/Makefile
98+++ b/drivers/pci/controller/Makefile
99@@ -39,7 +39,7 @@ obj-$(CONFIG_PCI_LOONGSON) += pci-loongson.o
100 obj-$(CONFIG_PCIE_HISI_ERR) += pcie-hisi-error.o
101 obj-$(CONFIG_PCIE_APPLE) += pcie-apple.o
102 obj-$(CONFIG_PCIE_MT7621) += pcie-mt7621.o
103-
104+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/
107 obj-y				+= mobiveil/
108diff --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
313index 6b1301e2498e..b3cf3adeab28 100644
314--- a/include/linux/pci-ecam.h
315+++ b/include/linux/pci-ecam.h
316@@ -88,6 +88,8 @@ extern const struct pci_ecam_ops xgene_v2_pcie_ecam_ops; /* APM X-Gene PCIe v2.x
317 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 */
319 extern const struct pci_ecam_ops loongson_pci_ecam_ops; /* Loongson PCIe */
320+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)
325