pci-thunder-ecam.c 9.4 KB

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