pci-thunder-ecam.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015, 2016 Cavium, Inc.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/ioport.h>
  8. #include <linux/of_pci.h>
  9. #include <linux/of.h>
  10. #include <linux/pci-ecam.h>
  11. #include <linux/platform_device.h>
  12. #if defined(CONFIG_PCI_HOST_THUNDER_ECAM) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
  13. static void set_val(u32 v, int where, int size, u32 *val)
  14. {
  15. int shift = (where & 3) * 8;
  16. pr_debug("set_val %04x: %08x\n", (unsigned)(where & ~3), v);
  17. v >>= shift;
  18. if (size == 1)
  19. v &= 0xff;
  20. else if (size == 2)
  21. v &= 0xffff;
  22. *val = v;
  23. }
  24. static int handle_ea_bar(u32 e0, int bar, struct pci_bus *bus,
  25. unsigned int devfn, int where, int size, u32 *val)
  26. {
  27. void __iomem *addr;
  28. u32 v;
  29. /* Entries are 16-byte aligned; bits[2,3] select word in entry */
  30. int where_a = where & 0xc;
  31. if (where_a == 0) {
  32. set_val(e0, where, size, val);
  33. return PCIBIOS_SUCCESSFUL;
  34. }
  35. if (where_a == 0x4) {
  36. addr = bus->ops->map_bus(bus, devfn, bar); /* BAR 0 */
  37. if (!addr) {
  38. *val = ~0;
  39. return PCIBIOS_DEVICE_NOT_FOUND;
  40. }
  41. v = readl(addr);
  42. v &= ~0xf;
  43. v |= 2; /* EA entry-1. Base-L */
  44. set_val(v, where, size, val);
  45. return PCIBIOS_SUCCESSFUL;
  46. }
  47. if (where_a == 0x8) {
  48. u32 barl_orig;
  49. u32 barl_rb;
  50. addr = bus->ops->map_bus(bus, devfn, bar); /* BAR 0 */
  51. if (!addr) {
  52. *val = ~0;
  53. return PCIBIOS_DEVICE_NOT_FOUND;
  54. }
  55. barl_orig = readl(addr + 0);
  56. writel(0xffffffff, addr + 0);
  57. barl_rb = readl(addr + 0);
  58. writel(barl_orig, addr + 0);
  59. /* zeros in unsettable bits */
  60. v = ~barl_rb & ~3;
  61. v |= 0xc; /* EA entry-2. Offset-L */
  62. set_val(v, where, size, val);
  63. return PCIBIOS_SUCCESSFUL;
  64. }
  65. if (where_a == 0xc) {
  66. addr = bus->ops->map_bus(bus, devfn, bar + 4); /* BAR 1 */
  67. if (!addr) {
  68. *val = ~0;
  69. return PCIBIOS_DEVICE_NOT_FOUND;
  70. }
  71. v = readl(addr); /* EA entry-3. Base-H */
  72. set_val(v, where, size, val);
  73. return PCIBIOS_SUCCESSFUL;
  74. }
  75. return PCIBIOS_DEVICE_NOT_FOUND;
  76. }
  77. static int thunder_ecam_p2_config_read(struct pci_bus *bus, unsigned int devfn,
  78. int where, int size, u32 *val)
  79. {
  80. struct pci_config_window *cfg = bus->sysdata;
  81. int where_a = where & ~3;
  82. void __iomem *addr;
  83. u32 node_bits;
  84. u32 v;
  85. /* EA Base[63:32] may be missing some bits ... */
  86. switch (where_a) {
  87. case 0xa8:
  88. case 0xbc:
  89. case 0xd0:
  90. case 0xe4:
  91. break;
  92. default:
  93. return pci_generic_config_read(bus, devfn, where, size, val);
  94. }
  95. addr = bus->ops->map_bus(bus, devfn, where_a);
  96. if (!addr) {
  97. *val = ~0;
  98. return PCIBIOS_DEVICE_NOT_FOUND;
  99. }
  100. v = readl(addr);
  101. /*
  102. * Bit 44 of the 64-bit Base must match the same bit in
  103. * the config space access window. Since we are working with
  104. * the high-order 32 bits, shift everything down by 32 bits.
  105. */
  106. node_bits = upper_32_bits(cfg->res.start) & (1 << 12);
  107. v |= node_bits;
  108. set_val(v, where, size, val);
  109. return PCIBIOS_SUCCESSFUL;
  110. }
  111. static int thunder_ecam_config_read(struct pci_bus *bus, unsigned int devfn,
  112. int where, int size, u32 *val)
  113. {
  114. u32 v;
  115. u32 vendor_device;
  116. u32 class_rev;
  117. void __iomem *addr;
  118. int cfg_type;
  119. int where_a = where & ~3;
  120. addr = bus->ops->map_bus(bus, devfn, 0xc);
  121. if (!addr) {
  122. *val = ~0;
  123. return PCIBIOS_DEVICE_NOT_FOUND;
  124. }
  125. v = readl(addr);
  126. /* Check for non type-00 header */
  127. cfg_type = (v >> 16) & 0x7f;
  128. addr = bus->ops->map_bus(bus, devfn, 8);
  129. if (!addr) {
  130. *val = ~0;
  131. return PCIBIOS_DEVICE_NOT_FOUND;
  132. }
  133. class_rev = readl(addr);
  134. if (class_rev == 0xffffffff)
  135. goto no_emulation;
  136. if ((class_rev & 0xff) >= 8) {
  137. /* Pass-2 handling */
  138. if (cfg_type)
  139. goto no_emulation;
  140. return thunder_ecam_p2_config_read(bus, devfn, where,
  141. size, val);
  142. }
  143. /*
  144. * All BARs have fixed addresses specified by the EA
  145. * capability; they must return zero on read.
  146. */
  147. if (cfg_type == 0 &&
  148. ((where >= 0x10 && where < 0x2c) ||
  149. (where >= 0x1a4 && where < 0x1bc))) {
  150. /* BAR or SR-IOV BAR */
  151. *val = 0;
  152. return PCIBIOS_SUCCESSFUL;
  153. }
  154. addr = bus->ops->map_bus(bus, devfn, 0);
  155. if (!addr) {
  156. *val = ~0;
  157. return PCIBIOS_DEVICE_NOT_FOUND;
  158. }
  159. vendor_device = readl(addr);
  160. if (vendor_device == 0xffffffff)
  161. goto no_emulation;
  162. pr_debug("%04x:%04x - Fix pass#: %08x, where: %03x, devfn: %03x\n",
  163. vendor_device & 0xffff, vendor_device >> 16, class_rev,
  164. (unsigned) where, devfn);
  165. /* Check for non type-00 header */
  166. if (cfg_type == 0) {
  167. bool has_msix;
  168. bool is_nic = (vendor_device == 0xa01e177d);
  169. bool is_tns = (vendor_device == 0xa01f177d);
  170. addr = bus->ops->map_bus(bus, devfn, 0x70);
  171. if (!addr) {
  172. *val = ~0;
  173. return PCIBIOS_DEVICE_NOT_FOUND;
  174. }
  175. /* E_CAP */
  176. v = readl(addr);
  177. has_msix = (v & 0xff00) != 0;
  178. if (!has_msix && where_a == 0x70) {
  179. v |= 0xbc00; /* next capability is EA at 0xbc */
  180. set_val(v, where, size, val);
  181. return PCIBIOS_SUCCESSFUL;
  182. }
  183. if (where_a == 0xb0) {
  184. addr = bus->ops->map_bus(bus, devfn, where_a);
  185. if (!addr) {
  186. *val = ~0;
  187. return PCIBIOS_DEVICE_NOT_FOUND;
  188. }
  189. v = readl(addr);
  190. if (v & 0xff00)
  191. pr_err("Bad MSIX cap header: %08x\n", v);
  192. v |= 0xbc00; /* next capability is EA at 0xbc */
  193. set_val(v, where, size, val);
  194. return PCIBIOS_SUCCESSFUL;
  195. }
  196. if (where_a == 0xbc) {
  197. if (is_nic)
  198. v = 0x40014; /* EA last in chain, 4 entries */
  199. else if (is_tns)
  200. v = 0x30014; /* EA last in chain, 3 entries */
  201. else if (has_msix)
  202. v = 0x20014; /* EA last in chain, 2 entries */
  203. else
  204. v = 0x10014; /* EA last in chain, 1 entry */
  205. set_val(v, where, size, val);
  206. return PCIBIOS_SUCCESSFUL;
  207. }
  208. if (where_a >= 0xc0 && where_a < 0xd0)
  209. /* EA entry-0. PP=0, BAR0 Size:3 */
  210. return handle_ea_bar(0x80ff0003,
  211. 0x10, bus, devfn, where,
  212. size, val);
  213. if (where_a >= 0xd0 && where_a < 0xe0 && has_msix)
  214. /* EA entry-1. PP=0, BAR4 Size:3 */
  215. return handle_ea_bar(0x80ff0043,
  216. 0x20, bus, devfn, where,
  217. size, val);
  218. if (where_a >= 0xe0 && where_a < 0xf0 && is_tns)
  219. /* EA entry-2. PP=0, BAR2, Size:3 */
  220. return handle_ea_bar(0x80ff0023,
  221. 0x18, bus, devfn, where,
  222. size, val);
  223. if (where_a >= 0xe0 && where_a < 0xf0 && is_nic)
  224. /* EA entry-2. PP=4, VF_BAR0 (9), Size:3 */
  225. return handle_ea_bar(0x80ff0493,
  226. 0x1a4, bus, devfn, where,
  227. size, val);
  228. if (where_a >= 0xf0 && where_a < 0x100 && is_nic)
  229. /* EA entry-3. PP=4, VF_BAR4 (d), Size:3 */
  230. return handle_ea_bar(0x80ff04d3,
  231. 0x1b4, bus, devfn, where,
  232. size, val);
  233. } else if (cfg_type == 1) {
  234. bool is_rsl_bridge = devfn == 0x08;
  235. bool is_rad_bridge = devfn == 0xa0;
  236. bool is_zip_bridge = devfn == 0xa8;
  237. bool is_dfa_bridge = devfn == 0xb0;
  238. bool is_nic_bridge = devfn == 0x10;
  239. if (where_a == 0x70) {
  240. addr = bus->ops->map_bus(bus, devfn, where_a);
  241. if (!addr) {
  242. *val = ~0;
  243. return PCIBIOS_DEVICE_NOT_FOUND;
  244. }
  245. v = readl(addr);
  246. if (v & 0xff00)
  247. pr_err("Bad PCIe cap header: %08x\n", v);
  248. v |= 0xbc00; /* next capability is EA at 0xbc */
  249. set_val(v, where, size, val);
  250. return PCIBIOS_SUCCESSFUL;
  251. }
  252. if (where_a == 0xbc) {
  253. if (is_nic_bridge)
  254. v = 0x10014; /* EA last in chain, 1 entry */
  255. else
  256. v = 0x00014; /* EA last in chain, no entries */
  257. set_val(v, where, size, val);
  258. return PCIBIOS_SUCCESSFUL;
  259. }
  260. if (where_a == 0xc0) {
  261. if (is_rsl_bridge || is_nic_bridge)
  262. v = 0x0101; /* subordinate:secondary = 1:1 */
  263. else if (is_rad_bridge)
  264. v = 0x0202; /* subordinate:secondary = 2:2 */
  265. else if (is_zip_bridge)
  266. v = 0x0303; /* subordinate:secondary = 3:3 */
  267. else if (is_dfa_bridge)
  268. v = 0x0404; /* subordinate:secondary = 4:4 */
  269. set_val(v, where, size, val);
  270. return PCIBIOS_SUCCESSFUL;
  271. }
  272. if (where_a == 0xc4 && is_nic_bridge) {
  273. /* Enabled, not-Write, SP=ff, PP=05, BEI=6, ES=4 */
  274. v = 0x80ff0564;
  275. set_val(v, where, size, val);
  276. return PCIBIOS_SUCCESSFUL;
  277. }
  278. if (where_a == 0xc8 && is_nic_bridge) {
  279. v = 0x00000002; /* Base-L 64-bit */
  280. set_val(v, where, size, val);
  281. return PCIBIOS_SUCCESSFUL;
  282. }
  283. if (where_a == 0xcc && is_nic_bridge) {
  284. v = 0xfffffffe; /* MaxOffset-L 64-bit */
  285. set_val(v, where, size, val);
  286. return PCIBIOS_SUCCESSFUL;
  287. }
  288. if (where_a == 0xd0 && is_nic_bridge) {
  289. v = 0x00008430; /* NIC Base-H */
  290. set_val(v, where, size, val);
  291. return PCIBIOS_SUCCESSFUL;
  292. }
  293. if (where_a == 0xd4 && is_nic_bridge) {
  294. v = 0x0000000f; /* MaxOffset-H */
  295. set_val(v, where, size, val);
  296. return PCIBIOS_SUCCESSFUL;
  297. }
  298. }
  299. no_emulation:
  300. return pci_generic_config_read(bus, devfn, where, size, val);
  301. }
  302. static int thunder_ecam_config_write(struct pci_bus *bus, unsigned int devfn,
  303. int where, int size, u32 val)
  304. {
  305. /*
  306. * All BARs have fixed addresses; ignore BAR writes so they
  307. * don't get corrupted.
  308. */
  309. if ((where >= 0x10 && where < 0x2c) ||
  310. (where >= 0x1a4 && where < 0x1bc))
  311. /* BAR or SR-IOV BAR */
  312. return PCIBIOS_SUCCESSFUL;
  313. return pci_generic_config_write(bus, devfn, where, size, val);
  314. }
  315. struct pci_ecam_ops pci_thunder_ecam_ops = {
  316. .bus_shift = 20,
  317. .pci_ops = {
  318. .map_bus = pci_ecam_map_bus,
  319. .read = thunder_ecam_config_read,
  320. .write = thunder_ecam_config_write,
  321. }
  322. };
  323. #ifdef CONFIG_PCI_HOST_THUNDER_ECAM
  324. static const struct of_device_id thunder_ecam_of_match[] = {
  325. { .compatible = "cavium,pci-host-thunder-ecam" },
  326. { },
  327. };
  328. static int thunder_ecam_probe(struct platform_device *pdev)
  329. {
  330. return pci_host_common_probe(pdev, &pci_thunder_ecam_ops);
  331. }
  332. static struct platform_driver thunder_ecam_driver = {
  333. .driver = {
  334. .name = KBUILD_MODNAME,
  335. .of_match_table = thunder_ecam_of_match,
  336. .suppress_bind_attrs = true,
  337. },
  338. .probe = thunder_ecam_probe,
  339. };
  340. builtin_platform_driver(thunder_ecam_driver);
  341. #endif
  342. #endif