pci-thunder-ecam.c 10 KB

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