pci_mcfg.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Broadcom
  4. * Author: Jayachandran C <[email protected]>
  5. * Copyright (C) 2016 Semihalf
  6. * Author: Tomasz Nowicki <[email protected]>
  7. */
  8. #define pr_fmt(fmt) "ACPI: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <linux/pci-acpi.h>
  12. #include <linux/pci-ecam.h>
  13. /* Structure to hold entries from the MCFG table */
  14. struct mcfg_entry {
  15. struct list_head list;
  16. phys_addr_t addr;
  17. u16 segment;
  18. u8 bus_start;
  19. u8 bus_end;
  20. };
  21. #ifdef CONFIG_PCI_QUIRKS
  22. struct mcfg_fixup {
  23. char oem_id[ACPI_OEM_ID_SIZE + 1];
  24. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
  25. u32 oem_revision;
  26. u16 segment;
  27. struct resource bus_range;
  28. const struct pci_ecam_ops *ops;
  29. struct resource cfgres;
  30. };
  31. #define MCFG_BUS_RANGE(start, end) DEFINE_RES_NAMED((start), \
  32. ((end) - (start) + 1), \
  33. NULL, IORESOURCE_BUS)
  34. #define MCFG_BUS_ANY MCFG_BUS_RANGE(0x0, 0xff)
  35. static struct mcfg_fixup mcfg_quirks[] = {
  36. /* { OEM_ID, OEM_TABLE_ID, REV, SEGMENT, BUS_RANGE, ops, cfgres }, */
  37. #ifdef CONFIG_ARM64
  38. #define AL_ECAM(table_id, rev, seg, ops) \
  39. { "AMAZON", table_id, rev, seg, MCFG_BUS_ANY, ops }
  40. AL_ECAM("GRAVITON", 0, 0, &al_pcie_ops),
  41. AL_ECAM("GRAVITON", 0, 1, &al_pcie_ops),
  42. AL_ECAM("GRAVITON", 0, 2, &al_pcie_ops),
  43. AL_ECAM("GRAVITON", 0, 3, &al_pcie_ops),
  44. AL_ECAM("GRAVITON", 0, 4, &al_pcie_ops),
  45. AL_ECAM("GRAVITON", 0, 5, &al_pcie_ops),
  46. AL_ECAM("GRAVITON", 0, 6, &al_pcie_ops),
  47. AL_ECAM("GRAVITON", 0, 7, &al_pcie_ops),
  48. #define QCOM_ECAM32(seg) \
  49. { "QCOM ", "QDF2432 ", 1, seg, MCFG_BUS_ANY, &pci_32b_ops }
  50. QCOM_ECAM32(0),
  51. QCOM_ECAM32(1),
  52. QCOM_ECAM32(2),
  53. QCOM_ECAM32(3),
  54. QCOM_ECAM32(4),
  55. QCOM_ECAM32(5),
  56. QCOM_ECAM32(6),
  57. QCOM_ECAM32(7),
  58. #define HISI_QUAD_DOM(table_id, seg, ops) \
  59. { "HISI ", table_id, 0, (seg) + 0, MCFG_BUS_ANY, ops }, \
  60. { "HISI ", table_id, 0, (seg) + 1, MCFG_BUS_ANY, ops }, \
  61. { "HISI ", table_id, 0, (seg) + 2, MCFG_BUS_ANY, ops }, \
  62. { "HISI ", table_id, 0, (seg) + 3, MCFG_BUS_ANY, ops }
  63. HISI_QUAD_DOM("HIP05 ", 0, &hisi_pcie_ops),
  64. HISI_QUAD_DOM("HIP06 ", 0, &hisi_pcie_ops),
  65. HISI_QUAD_DOM("HIP07 ", 0, &hisi_pcie_ops),
  66. HISI_QUAD_DOM("HIP07 ", 4, &hisi_pcie_ops),
  67. HISI_QUAD_DOM("HIP07 ", 8, &hisi_pcie_ops),
  68. HISI_QUAD_DOM("HIP07 ", 12, &hisi_pcie_ops),
  69. #define THUNDER_PEM_RES(addr, node) \
  70. DEFINE_RES_MEM((addr) + ((u64) (node) << 44), 0x39 * SZ_16M)
  71. #define THUNDER_PEM_QUIRK(rev, node) \
  72. { "CAVIUM", "THUNDERX", rev, 4 + (10 * (node)), MCFG_BUS_ANY, \
  73. &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x88001f000000UL, node) }, \
  74. { "CAVIUM", "THUNDERX", rev, 5 + (10 * (node)), MCFG_BUS_ANY, \
  75. &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x884057000000UL, node) }, \
  76. { "CAVIUM", "THUNDERX", rev, 6 + (10 * (node)), MCFG_BUS_ANY, \
  77. &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x88808f000000UL, node) }, \
  78. { "CAVIUM", "THUNDERX", rev, 7 + (10 * (node)), MCFG_BUS_ANY, \
  79. &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x89001f000000UL, node) }, \
  80. { "CAVIUM", "THUNDERX", rev, 8 + (10 * (node)), MCFG_BUS_ANY, \
  81. &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x894057000000UL, node) }, \
  82. { "CAVIUM", "THUNDERX", rev, 9 + (10 * (node)), MCFG_BUS_ANY, \
  83. &thunder_pem_ecam_ops, THUNDER_PEM_RES(0x89808f000000UL, node) }
  84. #define THUNDER_ECAM_QUIRK(rev, seg) \
  85. { "CAVIUM", "THUNDERX", rev, seg, MCFG_BUS_ANY, \
  86. &pci_thunder_ecam_ops }
  87. /* SoC pass2.x */
  88. THUNDER_PEM_QUIRK(1, 0),
  89. THUNDER_PEM_QUIRK(1, 1),
  90. THUNDER_ECAM_QUIRK(1, 10),
  91. /* SoC pass1.x */
  92. THUNDER_PEM_QUIRK(2, 0), /* off-chip devices */
  93. THUNDER_PEM_QUIRK(2, 1), /* off-chip devices */
  94. THUNDER_ECAM_QUIRK(2, 0),
  95. THUNDER_ECAM_QUIRK(2, 1),
  96. THUNDER_ECAM_QUIRK(2, 2),
  97. THUNDER_ECAM_QUIRK(2, 3),
  98. THUNDER_ECAM_QUIRK(2, 10),
  99. THUNDER_ECAM_QUIRK(2, 11),
  100. THUNDER_ECAM_QUIRK(2, 12),
  101. THUNDER_ECAM_QUIRK(2, 13),
  102. { "NVIDIA", "TEGRA194", 1, 0, MCFG_BUS_ANY, &tegra194_pcie_ops},
  103. { "NVIDIA", "TEGRA194", 1, 1, MCFG_BUS_ANY, &tegra194_pcie_ops},
  104. { "NVIDIA", "TEGRA194", 1, 2, MCFG_BUS_ANY, &tegra194_pcie_ops},
  105. { "NVIDIA", "TEGRA194", 1, 3, MCFG_BUS_ANY, &tegra194_pcie_ops},
  106. { "NVIDIA", "TEGRA194", 1, 4, MCFG_BUS_ANY, &tegra194_pcie_ops},
  107. { "NVIDIA", "TEGRA194", 1, 5, MCFG_BUS_ANY, &tegra194_pcie_ops},
  108. #define XGENE_V1_ECAM_MCFG(rev, seg) \
  109. {"APM ", "XGENE ", rev, seg, MCFG_BUS_ANY, \
  110. &xgene_v1_pcie_ecam_ops }
  111. #define XGENE_V2_ECAM_MCFG(rev, seg) \
  112. {"APM ", "XGENE ", rev, seg, MCFG_BUS_ANY, \
  113. &xgene_v2_pcie_ecam_ops }
  114. /* X-Gene SoC with v1 PCIe controller */
  115. XGENE_V1_ECAM_MCFG(1, 0),
  116. XGENE_V1_ECAM_MCFG(1, 1),
  117. XGENE_V1_ECAM_MCFG(1, 2),
  118. XGENE_V1_ECAM_MCFG(1, 3),
  119. XGENE_V1_ECAM_MCFG(1, 4),
  120. XGENE_V1_ECAM_MCFG(2, 0),
  121. XGENE_V1_ECAM_MCFG(2, 1),
  122. XGENE_V1_ECAM_MCFG(2, 2),
  123. XGENE_V1_ECAM_MCFG(2, 3),
  124. XGENE_V1_ECAM_MCFG(2, 4),
  125. /* X-Gene SoC with v2.1 PCIe controller */
  126. XGENE_V2_ECAM_MCFG(3, 0),
  127. XGENE_V2_ECAM_MCFG(3, 1),
  128. /* X-Gene SoC with v2.2 PCIe controller */
  129. XGENE_V2_ECAM_MCFG(4, 0),
  130. XGENE_V2_ECAM_MCFG(4, 1),
  131. XGENE_V2_ECAM_MCFG(4, 2),
  132. #define ALTRA_ECAM_QUIRK(rev, seg) \
  133. { "Ampere", "Altra ", rev, seg, MCFG_BUS_ANY, &pci_32b_read_ops }
  134. ALTRA_ECAM_QUIRK(1, 0),
  135. ALTRA_ECAM_QUIRK(1, 1),
  136. ALTRA_ECAM_QUIRK(1, 2),
  137. ALTRA_ECAM_QUIRK(1, 3),
  138. ALTRA_ECAM_QUIRK(1, 4),
  139. ALTRA_ECAM_QUIRK(1, 5),
  140. ALTRA_ECAM_QUIRK(1, 6),
  141. ALTRA_ECAM_QUIRK(1, 7),
  142. ALTRA_ECAM_QUIRK(1, 8),
  143. ALTRA_ECAM_QUIRK(1, 9),
  144. ALTRA_ECAM_QUIRK(1, 10),
  145. ALTRA_ECAM_QUIRK(1, 11),
  146. ALTRA_ECAM_QUIRK(1, 12),
  147. ALTRA_ECAM_QUIRK(1, 13),
  148. ALTRA_ECAM_QUIRK(1, 14),
  149. ALTRA_ECAM_QUIRK(1, 15),
  150. #endif /* ARM64 */
  151. #ifdef CONFIG_LOONGARCH
  152. #define LOONGSON_ECAM_MCFG(table_id, seg) \
  153. { "LOONGS", table_id, 1, seg, MCFG_BUS_ANY, &loongson_pci_ecam_ops }
  154. LOONGSON_ECAM_MCFG("\0", 0),
  155. LOONGSON_ECAM_MCFG("LOONGSON", 0),
  156. LOONGSON_ECAM_MCFG("\0", 1),
  157. LOONGSON_ECAM_MCFG("LOONGSON", 1),
  158. #endif /* LOONGARCH */
  159. };
  160. static char mcfg_oem_id[ACPI_OEM_ID_SIZE];
  161. static char mcfg_oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
  162. static u32 mcfg_oem_revision;
  163. static int pci_mcfg_quirk_matches(struct mcfg_fixup *f, u16 segment,
  164. struct resource *bus_range)
  165. {
  166. if (!memcmp(f->oem_id, mcfg_oem_id, ACPI_OEM_ID_SIZE) &&
  167. !memcmp(f->oem_table_id, mcfg_oem_table_id,
  168. ACPI_OEM_TABLE_ID_SIZE) &&
  169. f->oem_revision == mcfg_oem_revision &&
  170. f->segment == segment &&
  171. resource_contains(&f->bus_range, bus_range))
  172. return 1;
  173. return 0;
  174. }
  175. #endif
  176. static void pci_mcfg_apply_quirks(struct acpi_pci_root *root,
  177. struct resource *cfgres,
  178. const struct pci_ecam_ops **ecam_ops)
  179. {
  180. #ifdef CONFIG_PCI_QUIRKS
  181. u16 segment = root->segment;
  182. struct resource *bus_range = &root->secondary;
  183. struct mcfg_fixup *f;
  184. int i;
  185. for (i = 0, f = mcfg_quirks; i < ARRAY_SIZE(mcfg_quirks); i++, f++) {
  186. if (pci_mcfg_quirk_matches(f, segment, bus_range)) {
  187. if (f->cfgres.start)
  188. *cfgres = f->cfgres;
  189. if (f->ops)
  190. *ecam_ops = f->ops;
  191. dev_info(&root->device->dev, "MCFG quirk: ECAM at %pR for %pR with %ps\n",
  192. cfgres, bus_range, *ecam_ops);
  193. return;
  194. }
  195. }
  196. #endif
  197. }
  198. /* List to save MCFG entries */
  199. static LIST_HEAD(pci_mcfg_list);
  200. int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres,
  201. const struct pci_ecam_ops **ecam_ops)
  202. {
  203. const struct pci_ecam_ops *ops = &pci_generic_ecam_ops;
  204. struct resource *bus_res = &root->secondary;
  205. u16 seg = root->segment;
  206. struct mcfg_entry *e;
  207. struct resource res;
  208. /* Use address from _CBA if present, otherwise lookup MCFG */
  209. if (root->mcfg_addr)
  210. goto skip_lookup;
  211. /*
  212. * We expect the range in bus_res in the coverage of MCFG bus range.
  213. */
  214. list_for_each_entry(e, &pci_mcfg_list, list) {
  215. if (e->segment == seg && e->bus_start <= bus_res->start &&
  216. e->bus_end >= bus_res->end) {
  217. root->mcfg_addr = e->addr;
  218. }
  219. }
  220. skip_lookup:
  221. memset(&res, 0, sizeof(res));
  222. if (root->mcfg_addr) {
  223. res.start = root->mcfg_addr + (bus_res->start << 20);
  224. res.end = res.start + (resource_size(bus_res) << 20) - 1;
  225. res.flags = IORESOURCE_MEM;
  226. }
  227. /*
  228. * Allow quirks to override default ECAM ops and CFG resource
  229. * range. This may even fabricate a CFG resource range in case
  230. * MCFG does not have it. Invalid CFG start address means MCFG
  231. * firmware bug or we need another quirk in array.
  232. */
  233. pci_mcfg_apply_quirks(root, &res, &ops);
  234. if (!res.start)
  235. return -ENXIO;
  236. *cfgres = res;
  237. *ecam_ops = ops;
  238. return 0;
  239. }
  240. static __init int pci_mcfg_parse(struct acpi_table_header *header)
  241. {
  242. struct acpi_table_mcfg *mcfg;
  243. struct acpi_mcfg_allocation *mptr;
  244. struct mcfg_entry *e, *arr;
  245. int i, n;
  246. if (header->length < sizeof(struct acpi_table_mcfg))
  247. return -EINVAL;
  248. n = (header->length - sizeof(struct acpi_table_mcfg)) /
  249. sizeof(struct acpi_mcfg_allocation);
  250. mcfg = (struct acpi_table_mcfg *)header;
  251. mptr = (struct acpi_mcfg_allocation *) &mcfg[1];
  252. arr = kcalloc(n, sizeof(*arr), GFP_KERNEL);
  253. if (!arr)
  254. return -ENOMEM;
  255. for (i = 0, e = arr; i < n; i++, mptr++, e++) {
  256. e->segment = mptr->pci_segment;
  257. e->addr = mptr->address;
  258. e->bus_start = mptr->start_bus_number;
  259. e->bus_end = mptr->end_bus_number;
  260. list_add(&e->list, &pci_mcfg_list);
  261. }
  262. #ifdef CONFIG_PCI_QUIRKS
  263. /* Save MCFG IDs and revision for quirks matching */
  264. memcpy(mcfg_oem_id, header->oem_id, ACPI_OEM_ID_SIZE);
  265. memcpy(mcfg_oem_table_id, header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
  266. mcfg_oem_revision = header->oem_revision;
  267. #endif
  268. pr_info("MCFG table detected, %d entries\n", n);
  269. return 0;
  270. }
  271. /* Interface called by ACPI - parse and save MCFG table */
  272. void __init pci_mmcfg_late_init(void)
  273. {
  274. int err = acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse);
  275. if (err)
  276. pr_debug("Failed to parse MCFG (%d)\n", err);
  277. }