pci_mcfg.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (C) 2016 Broadcom
  3. * Author: Jayachandran C <jchandra@broadcom.com>
  4. * Copyright (C) 2016 Semihalf
  5. * Author: Tomasz Nowicki <tn@semihalf.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2, as
  9. * published by the Free Software Foundation (the "GPL").
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License version 2 (GPLv2) for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * version 2 (GPLv2) along with this source code.
  18. */
  19. #define pr_fmt(fmt) "ACPI: " fmt
  20. #include <linux/kernel.h>
  21. #include <linux/pci.h>
  22. #include <linux/pci-acpi.h>
  23. #include <linux/pci-ecam.h>
  24. /* Structure to hold entries from the MCFG table */
  25. struct mcfg_entry {
  26. struct list_head list;
  27. phys_addr_t addr;
  28. u16 segment;
  29. u8 bus_start;
  30. u8 bus_end;
  31. };
  32. #ifdef CONFIG_PCI_QUIRKS
  33. struct mcfg_fixup {
  34. char oem_id[ACPI_OEM_ID_SIZE + 1];
  35. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
  36. u32 oem_revision;
  37. u16 segment;
  38. struct resource bus_range;
  39. struct pci_ecam_ops *ops;
  40. struct resource cfgres;
  41. };
  42. #define MCFG_BUS_RANGE(start, end) DEFINE_RES_NAMED((start), \
  43. ((end) - (start) + 1), \
  44. NULL, IORESOURCE_BUS)
  45. #define MCFG_BUS_ANY MCFG_BUS_RANGE(0x0, 0xff)
  46. static struct mcfg_fixup mcfg_quirks[] = {
  47. /* { OEM_ID, OEM_TABLE_ID, REV, SEGMENT, BUS_RANGE, ops, cfgres }, */
  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. /* SoC pass2.x */
  85. THUNDER_PEM_QUIRK(1, 0),
  86. THUNDER_PEM_QUIRK(1, 1),
  87. #define THUNDER_ECAM_QUIRK(rev, seg) \
  88. { "CAVIUM", "THUNDERX", rev, seg, MCFG_BUS_ANY, \
  89. &pci_thunder_ecam_ops }
  90. /* SoC pass1.x */
  91. THUNDER_PEM_QUIRK(2, 0), /* off-chip devices */
  92. THUNDER_PEM_QUIRK(2, 1), /* off-chip devices */
  93. THUNDER_ECAM_QUIRK(2, 0),
  94. THUNDER_ECAM_QUIRK(2, 1),
  95. THUNDER_ECAM_QUIRK(2, 2),
  96. THUNDER_ECAM_QUIRK(2, 3),
  97. THUNDER_ECAM_QUIRK(2, 10),
  98. THUNDER_ECAM_QUIRK(2, 11),
  99. THUNDER_ECAM_QUIRK(2, 12),
  100. THUNDER_ECAM_QUIRK(2, 13),
  101. #define XGENE_V1_ECAM_MCFG(rev, seg) \
  102. {"APM ", "XGENE ", rev, seg, MCFG_BUS_ANY, \
  103. &xgene_v1_pcie_ecam_ops }
  104. #define XGENE_V2_ECAM_MCFG(rev, seg) \
  105. {"APM ", "XGENE ", rev, seg, MCFG_BUS_ANY, \
  106. &xgene_v2_pcie_ecam_ops }
  107. /* X-Gene SoC with v1 PCIe controller */
  108. XGENE_V1_ECAM_MCFG(1, 0),
  109. XGENE_V1_ECAM_MCFG(1, 1),
  110. XGENE_V1_ECAM_MCFG(1, 2),
  111. XGENE_V1_ECAM_MCFG(1, 3),
  112. XGENE_V1_ECAM_MCFG(1, 4),
  113. XGENE_V1_ECAM_MCFG(2, 0),
  114. XGENE_V1_ECAM_MCFG(2, 1),
  115. XGENE_V1_ECAM_MCFG(2, 2),
  116. XGENE_V1_ECAM_MCFG(2, 3),
  117. XGENE_V1_ECAM_MCFG(2, 4),
  118. /* X-Gene SoC with v2.1 PCIe controller */
  119. XGENE_V2_ECAM_MCFG(3, 0),
  120. XGENE_V2_ECAM_MCFG(3, 1),
  121. /* X-Gene SoC with v2.2 PCIe controller */
  122. XGENE_V2_ECAM_MCFG(4, 0),
  123. XGENE_V2_ECAM_MCFG(4, 1),
  124. XGENE_V2_ECAM_MCFG(4, 2),
  125. };
  126. static char mcfg_oem_id[ACPI_OEM_ID_SIZE];
  127. static char mcfg_oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
  128. static u32 mcfg_oem_revision;
  129. static int pci_mcfg_quirk_matches(struct mcfg_fixup *f, u16 segment,
  130. struct resource *bus_range)
  131. {
  132. if (!memcmp(f->oem_id, mcfg_oem_id, ACPI_OEM_ID_SIZE) &&
  133. !memcmp(f->oem_table_id, mcfg_oem_table_id,
  134. ACPI_OEM_TABLE_ID_SIZE) &&
  135. f->oem_revision == mcfg_oem_revision &&
  136. f->segment == segment &&
  137. resource_contains(&f->bus_range, bus_range))
  138. return 1;
  139. return 0;
  140. }
  141. #endif
  142. static void pci_mcfg_apply_quirks(struct acpi_pci_root *root,
  143. struct resource *cfgres,
  144. struct pci_ecam_ops **ecam_ops)
  145. {
  146. #ifdef CONFIG_PCI_QUIRKS
  147. u16 segment = root->segment;
  148. struct resource *bus_range = &root->secondary;
  149. struct mcfg_fixup *f;
  150. int i;
  151. for (i = 0, f = mcfg_quirks; i < ARRAY_SIZE(mcfg_quirks); i++, f++) {
  152. if (pci_mcfg_quirk_matches(f, segment, bus_range)) {
  153. if (f->cfgres.start)
  154. *cfgres = f->cfgres;
  155. if (f->ops)
  156. *ecam_ops = f->ops;
  157. dev_info(&root->device->dev, "MCFG quirk: ECAM at %pR for %pR with %ps\n",
  158. cfgres, bus_range, *ecam_ops);
  159. return;
  160. }
  161. }
  162. #endif
  163. }
  164. /* List to save MCFG entries */
  165. static LIST_HEAD(pci_mcfg_list);
  166. int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres,
  167. struct pci_ecam_ops **ecam_ops)
  168. {
  169. struct pci_ecam_ops *ops = &pci_generic_ecam_ops;
  170. struct resource *bus_res = &root->secondary;
  171. u16 seg = root->segment;
  172. struct mcfg_entry *e;
  173. struct resource res;
  174. /* Use address from _CBA if present, otherwise lookup MCFG */
  175. if (root->mcfg_addr)
  176. goto skip_lookup;
  177. /*
  178. * We expect exact match, unless MCFG entry end bus covers more than
  179. * specified by caller.
  180. */
  181. list_for_each_entry(e, &pci_mcfg_list, list) {
  182. if (e->segment == seg && e->bus_start == bus_res->start &&
  183. e->bus_end >= bus_res->end) {
  184. root->mcfg_addr = e->addr;
  185. }
  186. }
  187. skip_lookup:
  188. memset(&res, 0, sizeof(res));
  189. if (root->mcfg_addr) {
  190. res.start = root->mcfg_addr + (bus_res->start << 20);
  191. res.end = res.start + (resource_size(bus_res) << 20) - 1;
  192. res.flags = IORESOURCE_MEM;
  193. }
  194. /*
  195. * Allow quirks to override default ECAM ops and CFG resource
  196. * range. This may even fabricate a CFG resource range in case
  197. * MCFG does not have it. Invalid CFG start address means MCFG
  198. * firmware bug or we need another quirk in array.
  199. */
  200. pci_mcfg_apply_quirks(root, &res, &ops);
  201. if (!res.start)
  202. return -ENXIO;
  203. *cfgres = res;
  204. *ecam_ops = ops;
  205. return 0;
  206. }
  207. static __init int pci_mcfg_parse(struct acpi_table_header *header)
  208. {
  209. struct acpi_table_mcfg *mcfg;
  210. struct acpi_mcfg_allocation *mptr;
  211. struct mcfg_entry *e, *arr;
  212. int i, n;
  213. if (header->length < sizeof(struct acpi_table_mcfg))
  214. return -EINVAL;
  215. n = (header->length - sizeof(struct acpi_table_mcfg)) /
  216. sizeof(struct acpi_mcfg_allocation);
  217. mcfg = (struct acpi_table_mcfg *)header;
  218. mptr = (struct acpi_mcfg_allocation *) &mcfg[1];
  219. arr = kcalloc(n, sizeof(*arr), GFP_KERNEL);
  220. if (!arr)
  221. return -ENOMEM;
  222. for (i = 0, e = arr; i < n; i++, mptr++, e++) {
  223. e->segment = mptr->pci_segment;
  224. e->addr = mptr->address;
  225. e->bus_start = mptr->start_bus_number;
  226. e->bus_end = mptr->end_bus_number;
  227. list_add(&e->list, &pci_mcfg_list);
  228. }
  229. #ifdef CONFIG_PCI_QUIRKS
  230. /* Save MCFG IDs and revision for quirks matching */
  231. memcpy(mcfg_oem_id, header->oem_id, ACPI_OEM_ID_SIZE);
  232. memcpy(mcfg_oem_table_id, header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
  233. mcfg_oem_revision = header->oem_revision;
  234. #endif
  235. pr_info("MCFG table detected, %d entries\n", n);
  236. return 0;
  237. }
  238. /* Interface called by ACPI - parse and save MCFG table */
  239. void __init pci_mmcfg_late_init(void)
  240. {
  241. int err = acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse);
  242. if (err)
  243. pr_err("Failed to parse MCFG (%d)\n", err);
  244. }