pci-thunder-ecam.c 9.2 KB

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