0003-pcie-Add-quirk-for-the-Arm-Neoverse-N1SDP-platform.patch 11 KB

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