pci_mcfg.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. };
  102. static char mcfg_oem_id[ACPI_OEM_ID_SIZE];
  103. static char mcfg_oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
  104. static u32 mcfg_oem_revision;
  105. static int pci_mcfg_quirk_matches(struct mcfg_fixup *f, u16 segment,
  106. struct resource *bus_range)
  107. {
  108. if (!memcmp(f->oem_id, mcfg_oem_id, ACPI_OEM_ID_SIZE) &&
  109. !memcmp(f->oem_table_id, mcfg_oem_table_id,
  110. ACPI_OEM_TABLE_ID_SIZE) &&
  111. f->oem_revision == mcfg_oem_revision &&
  112. f->segment == segment &&
  113. resource_contains(&f->bus_range, bus_range))
  114. return 1;
  115. return 0;
  116. }
  117. #endif
  118. static void pci_mcfg_apply_quirks(struct acpi_pci_root *root,
  119. struct resource *cfgres,
  120. struct pci_ecam_ops **ecam_ops)
  121. {
  122. #ifdef CONFIG_PCI_QUIRKS
  123. u16 segment = root->segment;
  124. struct resource *bus_range = &root->secondary;
  125. struct mcfg_fixup *f;
  126. int i;
  127. for (i = 0, f = mcfg_quirks; i < ARRAY_SIZE(mcfg_quirks); i++, f++) {
  128. if (pci_mcfg_quirk_matches(f, segment, bus_range)) {
  129. if (f->cfgres.start)
  130. *cfgres = f->cfgres;
  131. if (f->ops)
  132. *ecam_ops = f->ops;
  133. dev_info(&root->device->dev, "MCFG quirk: ECAM at %pR for %pR with %ps\n",
  134. cfgres, bus_range, *ecam_ops);
  135. return;
  136. }
  137. }
  138. #endif
  139. }
  140. /* List to save MCFG entries */
  141. static LIST_HEAD(pci_mcfg_list);
  142. int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres,
  143. struct pci_ecam_ops **ecam_ops)
  144. {
  145. struct pci_ecam_ops *ops = &pci_generic_ecam_ops;
  146. struct resource *bus_res = &root->secondary;
  147. u16 seg = root->segment;
  148. struct mcfg_entry *e;
  149. struct resource res;
  150. /* Use address from _CBA if present, otherwise lookup MCFG */
  151. if (root->mcfg_addr)
  152. goto skip_lookup;
  153. /*
  154. * We expect exact match, unless MCFG entry end bus covers more than
  155. * specified by caller.
  156. */
  157. list_for_each_entry(e, &pci_mcfg_list, list) {
  158. if (e->segment == seg && e->bus_start == bus_res->start &&
  159. e->bus_end >= bus_res->end) {
  160. root->mcfg_addr = e->addr;
  161. }
  162. }
  163. skip_lookup:
  164. memset(&res, 0, sizeof(res));
  165. if (root->mcfg_addr) {
  166. res.start = root->mcfg_addr + (bus_res->start << 20);
  167. res.end = res.start + (resource_size(bus_res) << 20) - 1;
  168. res.flags = IORESOURCE_MEM;
  169. }
  170. /*
  171. * Allow quirks to override default ECAM ops and CFG resource
  172. * range. This may even fabricate a CFG resource range in case
  173. * MCFG does not have it. Invalid CFG start address means MCFG
  174. * firmware bug or we need another quirk in array.
  175. */
  176. pci_mcfg_apply_quirks(root, &res, &ops);
  177. if (!res.start)
  178. return -ENXIO;
  179. *cfgres = res;
  180. *ecam_ops = ops;
  181. return 0;
  182. }
  183. static __init int pci_mcfg_parse(struct acpi_table_header *header)
  184. {
  185. struct acpi_table_mcfg *mcfg;
  186. struct acpi_mcfg_allocation *mptr;
  187. struct mcfg_entry *e, *arr;
  188. int i, n;
  189. if (header->length < sizeof(struct acpi_table_mcfg))
  190. return -EINVAL;
  191. n = (header->length - sizeof(struct acpi_table_mcfg)) /
  192. sizeof(struct acpi_mcfg_allocation);
  193. mcfg = (struct acpi_table_mcfg *)header;
  194. mptr = (struct acpi_mcfg_allocation *) &mcfg[1];
  195. arr = kcalloc(n, sizeof(*arr), GFP_KERNEL);
  196. if (!arr)
  197. return -ENOMEM;
  198. for (i = 0, e = arr; i < n; i++, mptr++, e++) {
  199. e->segment = mptr->pci_segment;
  200. e->addr = mptr->address;
  201. e->bus_start = mptr->start_bus_number;
  202. e->bus_end = mptr->end_bus_number;
  203. list_add(&e->list, &pci_mcfg_list);
  204. }
  205. #ifdef CONFIG_PCI_QUIRKS
  206. /* Save MCFG IDs and revision for quirks matching */
  207. memcpy(mcfg_oem_id, header->oem_id, ACPI_OEM_ID_SIZE);
  208. memcpy(mcfg_oem_table_id, header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
  209. mcfg_oem_revision = header->oem_revision;
  210. #endif
  211. pr_info("MCFG table detected, %d entries\n", n);
  212. return 0;
  213. }
  214. /* Interface called by ACPI - parse and save MCFG table */
  215. void __init pci_mmcfg_late_init(void)
  216. {
  217. int err = acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse);
  218. if (err)
  219. pr_err("Failed to parse MCFG (%d)\n", err);
  220. }