pci-thunder-ecam.c 9.6 KB

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