pci-thunder-pem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 - 2016 Cavium, Inc.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/of_address.h>
  9. #include <linux/of_pci.h>
  10. #include <linux/pci-acpi.h>
  11. #include <linux/pci-ecam.h>
  12. #include <linux/platform_device.h>
  13. #include "../pci.h"
  14. #if defined(CONFIG_PCI_HOST_THUNDER_PEM) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
  15. #define PEM_CFG_WR 0x28
  16. #define PEM_CFG_RD 0x30
  17. struct thunder_pem_pci {
  18. u32 ea_entry[3];
  19. void __iomem *pem_reg_base;
  20. };
  21. static int thunder_pem_bridge_read(struct pci_bus *bus, unsigned int devfn,
  22. int where, int size, u32 *val)
  23. {
  24. u64 read_val, tmp_val;
  25. struct pci_config_window *cfg = bus->sysdata;
  26. struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
  27. if (devfn != 0 || where >= 2048) {
  28. *val = ~0;
  29. return PCIBIOS_DEVICE_NOT_FOUND;
  30. }
  31. /*
  32. * 32-bit accesses only. Write the address to the low order
  33. * bits of PEM_CFG_RD, then trigger the read by reading back.
  34. * The config data lands in the upper 32-bits of PEM_CFG_RD.
  35. */
  36. read_val = where & ~3ull;
  37. writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
  38. read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
  39. read_val >>= 32;
  40. /*
  41. * The config space contains some garbage, fix it up. Also
  42. * synthesize an EA capability for the BAR used by MSI-X.
  43. */
  44. switch (where & ~3) {
  45. case 0x40:
  46. read_val &= 0xffff00ff;
  47. read_val |= 0x00007000; /* Skip MSI CAP */
  48. break;
  49. case 0x70: /* Express Cap */
  50. /*
  51. * Change PME interrupt to vector 2 on T88 where it
  52. * reads as 0, else leave it alone.
  53. */
  54. if (!(read_val & (0x1f << 25)))
  55. read_val |= (2u << 25);
  56. break;
  57. case 0xb0: /* MSI-X Cap */
  58. /* TableSize=2 or 4, Next Cap is EA */
  59. read_val &= 0xc00000ff;
  60. /*
  61. * If Express Cap(0x70) raw PME vector reads as 0 we are on
  62. * T88 and TableSize is reported as 4, else TableSize
  63. * is 2.
  64. */
  65. writeq(0x70, pem_pci->pem_reg_base + PEM_CFG_RD);
  66. tmp_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
  67. tmp_val >>= 32;
  68. if (!(tmp_val & (0x1f << 25)))
  69. read_val |= 0x0003bc00;
  70. else
  71. read_val |= 0x0001bc00;
  72. break;
  73. case 0xb4:
  74. /* Table offset=0, BIR=0 */
  75. read_val = 0x00000000;
  76. break;
  77. case 0xb8:
  78. /* BPA offset=0xf0000, BIR=0 */
  79. read_val = 0x000f0000;
  80. break;
  81. case 0xbc:
  82. /* EA, 1 entry, no next Cap */
  83. read_val = 0x00010014;
  84. break;
  85. case 0xc0:
  86. /* DW2 for type-1 */
  87. read_val = 0x00000000;
  88. break;
  89. case 0xc4:
  90. /* Entry BEI=0, PP=0x00, SP=0xff, ES=3 */
  91. read_val = 0x80ff0003;
  92. break;
  93. case 0xc8:
  94. read_val = pem_pci->ea_entry[0];
  95. break;
  96. case 0xcc:
  97. read_val = pem_pci->ea_entry[1];
  98. break;
  99. case 0xd0:
  100. read_val = pem_pci->ea_entry[2];
  101. break;
  102. default:
  103. break;
  104. }
  105. read_val >>= (8 * (where & 3));
  106. switch (size) {
  107. case 1:
  108. read_val &= 0xff;
  109. break;
  110. case 2:
  111. read_val &= 0xffff;
  112. break;
  113. default:
  114. break;
  115. }
  116. *val = read_val;
  117. return PCIBIOS_SUCCESSFUL;
  118. }
  119. static int thunder_pem_config_read(struct pci_bus *bus, unsigned int devfn,
  120. int where, int size, u32 *val)
  121. {
  122. struct pci_config_window *cfg = bus->sysdata;
  123. if (bus->number < cfg->busr.start ||
  124. bus->number > cfg->busr.end)
  125. return PCIBIOS_DEVICE_NOT_FOUND;
  126. /*
  127. * The first device on the bus is the PEM PCIe bridge.
  128. * Special case its config access.
  129. */
  130. if (bus->number == cfg->busr.start)
  131. return thunder_pem_bridge_read(bus, devfn, where, size, val);
  132. return pci_generic_config_read(bus, devfn, where, size, val);
  133. }
  134. /*
  135. * Some of the w1c_bits below also include read-only or non-writable
  136. * reserved bits, this makes the code simpler and is OK as the bits
  137. * are not affected by writing zeros to them.
  138. */
  139. static u32 thunder_pem_bridge_w1c_bits(u64 where_aligned)
  140. {
  141. u32 w1c_bits = 0;
  142. switch (where_aligned) {
  143. case 0x04: /* Command/Status */
  144. case 0x1c: /* Base and I/O Limit/Secondary Status */
  145. w1c_bits = 0xff000000;
  146. break;
  147. case 0x44: /* Power Management Control and Status */
  148. w1c_bits = 0xfffffe00;
  149. break;
  150. case 0x78: /* Device Control/Device Status */
  151. case 0x80: /* Link Control/Link Status */
  152. case 0x88: /* Slot Control/Slot Status */
  153. case 0x90: /* Root Status */
  154. case 0xa0: /* Link Control 2 Registers/Link Status 2 */
  155. w1c_bits = 0xffff0000;
  156. break;
  157. case 0x104: /* Uncorrectable Error Status */
  158. case 0x110: /* Correctable Error Status */
  159. case 0x130: /* Error Status */
  160. case 0x160: /* Link Control 4 */
  161. w1c_bits = 0xffffffff;
  162. break;
  163. default:
  164. break;
  165. }
  166. return w1c_bits;
  167. }
  168. /* Some bits must be written to one so they appear to be read-only. */
  169. static u32 thunder_pem_bridge_w1_bits(u64 where_aligned)
  170. {
  171. u32 w1_bits;
  172. switch (where_aligned) {
  173. case 0x1c: /* I/O Base / I/O Limit, Secondary Status */
  174. /* Force 32-bit I/O addressing. */
  175. w1_bits = 0x0101;
  176. break;
  177. case 0x24: /* Prefetchable Memory Base / Prefetchable Memory Limit */
  178. /* Force 64-bit addressing */
  179. w1_bits = 0x00010001;
  180. break;
  181. default:
  182. w1_bits = 0;
  183. break;
  184. }
  185. return w1_bits;
  186. }
  187. static int thunder_pem_bridge_write(struct pci_bus *bus, unsigned int devfn,
  188. int where, int size, u32 val)
  189. {
  190. struct pci_config_window *cfg = bus->sysdata;
  191. struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
  192. u64 write_val, read_val;
  193. u64 where_aligned = where & ~3ull;
  194. u32 mask = 0;
  195. if (devfn != 0 || where >= 2048)
  196. return PCIBIOS_DEVICE_NOT_FOUND;
  197. /*
  198. * 32-bit accesses only. If the write is for a size smaller
  199. * than 32-bits, we must first read the 32-bit value and merge
  200. * in the desired bits and then write the whole 32-bits back
  201. * out.
  202. */
  203. switch (size) {
  204. case 1:
  205. writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
  206. read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
  207. read_val >>= 32;
  208. mask = ~(0xff << (8 * (where & 3)));
  209. read_val &= mask;
  210. val = (val & 0xff) << (8 * (where & 3));
  211. val |= (u32)read_val;
  212. break;
  213. case 2:
  214. writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
  215. read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
  216. read_val >>= 32;
  217. mask = ~(0xffff << (8 * (where & 3)));
  218. read_val &= mask;
  219. val = (val & 0xffff) << (8 * (where & 3));
  220. val |= (u32)read_val;
  221. break;
  222. default:
  223. break;
  224. }
  225. /*
  226. * By expanding the write width to 32 bits, we may
  227. * inadvertently hit some W1C bits that were not intended to
  228. * be written. Calculate the mask that must be applied to the
  229. * data to be written to avoid these cases.
  230. */
  231. if (mask) {
  232. u32 w1c_bits = thunder_pem_bridge_w1c_bits(where);
  233. if (w1c_bits) {
  234. mask &= w1c_bits;
  235. val &= ~mask;
  236. }
  237. }
  238. /*
  239. * Some bits must be read-only with value of one. Since the
  240. * access method allows these to be cleared if a zero is
  241. * written, force them to one before writing.
  242. */
  243. val |= thunder_pem_bridge_w1_bits(where_aligned);
  244. /*
  245. * Low order bits are the config address, the high order 32
  246. * bits are the data to be written.
  247. */
  248. write_val = (((u64)val) << 32) | where_aligned;
  249. writeq(write_val, pem_pci->pem_reg_base + PEM_CFG_WR);
  250. return PCIBIOS_SUCCESSFUL;
  251. }
  252. static int thunder_pem_config_write(struct pci_bus *bus, unsigned int devfn,
  253. int where, int size, u32 val)
  254. {
  255. struct pci_config_window *cfg = bus->sysdata;
  256. if (bus->number < cfg->busr.start ||
  257. bus->number > cfg->busr.end)
  258. return PCIBIOS_DEVICE_NOT_FOUND;
  259. /*
  260. * The first device on the bus is the PEM PCIe bridge.
  261. * Special case its config access.
  262. */
  263. if (bus->number == cfg->busr.start)
  264. return thunder_pem_bridge_write(bus, devfn, where, size, val);
  265. return pci_generic_config_write(bus, devfn, where, size, val);
  266. }
  267. static int thunder_pem_init(struct device *dev, struct pci_config_window *cfg,
  268. struct resource *res_pem)
  269. {
  270. struct thunder_pem_pci *pem_pci;
  271. resource_size_t bar4_start;
  272. pem_pci = devm_kzalloc(dev, sizeof(*pem_pci), GFP_KERNEL);
  273. if (!pem_pci)
  274. return -ENOMEM;
  275. pem_pci->pem_reg_base = devm_ioremap(dev, res_pem->start, 0x10000);
  276. if (!pem_pci->pem_reg_base)
  277. return -ENOMEM;
  278. /*
  279. * The MSI-X BAR for the PEM and AER interrupts is located at
  280. * a fixed offset from the PEM register base. Generate a
  281. * fragment of the synthesized Enhanced Allocation capability
  282. * structure here for the BAR.
  283. */
  284. bar4_start = res_pem->start + 0xf00000;
  285. pem_pci->ea_entry[0] = (u32)bar4_start | 2;
  286. pem_pci->ea_entry[1] = (u32)(res_pem->end - bar4_start) & ~3u;
  287. pem_pci->ea_entry[2] = (u32)(bar4_start >> 32);
  288. cfg->priv = pem_pci;
  289. return 0;
  290. }
  291. #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
  292. #define PEM_RES_BASE 0x87e0c0000000UL
  293. #define PEM_NODE_MASK GENMASK(45, 44)
  294. #define PEM_INDX_MASK GENMASK(26, 24)
  295. #define PEM_MIN_DOM_IN_NODE 4
  296. #define PEM_MAX_DOM_IN_NODE 10
  297. static void thunder_pem_reserve_range(struct device *dev, int seg,
  298. struct resource *r)
  299. {
  300. resource_size_t start = r->start, end = r->end;
  301. struct resource *res;
  302. const char *regionid;
  303. regionid = kasprintf(GFP_KERNEL, "PEM RC:%d", seg);
  304. if (!regionid)
  305. return;
  306. res = request_mem_region(start, end - start + 1, regionid);
  307. if (res)
  308. res->flags &= ~IORESOURCE_BUSY;
  309. else
  310. kfree(regionid);
  311. dev_info(dev, "%pR %s reserved\n", r,
  312. res ? "has been" : "could not be");
  313. }
  314. static void thunder_pem_legacy_fw(struct acpi_pci_root *root,
  315. struct resource *res_pem)
  316. {
  317. int node = acpi_get_node(root->device->handle);
  318. int index;
  319. if (node == NUMA_NO_NODE)
  320. node = 0;
  321. index = root->segment - PEM_MIN_DOM_IN_NODE;
  322. index -= node * PEM_MAX_DOM_IN_NODE;
  323. res_pem->start = PEM_RES_BASE | FIELD_PREP(PEM_NODE_MASK, node) |
  324. FIELD_PREP(PEM_INDX_MASK, index);
  325. res_pem->flags = IORESOURCE_MEM;
  326. }
  327. static int thunder_pem_acpi_init(struct pci_config_window *cfg)
  328. {
  329. struct device *dev = cfg->parent;
  330. struct acpi_device *adev = to_acpi_device(dev);
  331. struct acpi_pci_root *root = acpi_driver_data(adev);
  332. struct resource *res_pem;
  333. int ret;
  334. res_pem = devm_kzalloc(&adev->dev, sizeof(*res_pem), GFP_KERNEL);
  335. if (!res_pem)
  336. return -ENOMEM;
  337. ret = acpi_get_rc_resources(dev, "CAVA02B", root->segment, res_pem);
  338. /*
  339. * If we fail to gather resources it means that we run with old
  340. * FW where we need to calculate PEM-specific resources manually.
  341. */
  342. if (ret) {
  343. thunder_pem_legacy_fw(root, res_pem);
  344. /*
  345. * Reserve 64K size PEM specific resources. The full 16M range
  346. * size is required for thunder_pem_init() call.
  347. */
  348. res_pem->end = res_pem->start + SZ_64K - 1;
  349. thunder_pem_reserve_range(dev, root->segment, res_pem);
  350. res_pem->end = res_pem->start + SZ_16M - 1;
  351. /* Reserve PCI configuration space as well. */
  352. thunder_pem_reserve_range(dev, root->segment, &cfg->res);
  353. }
  354. return thunder_pem_init(dev, cfg, res_pem);
  355. }
  356. struct pci_ecam_ops thunder_pem_ecam_ops = {
  357. .bus_shift = 24,
  358. .init = thunder_pem_acpi_init,
  359. .pci_ops = {
  360. .map_bus = pci_ecam_map_bus,
  361. .read = thunder_pem_config_read,
  362. .write = thunder_pem_config_write,
  363. }
  364. };
  365. #endif
  366. #ifdef CONFIG_PCI_HOST_THUNDER_PEM
  367. static int thunder_pem_platform_init(struct pci_config_window *cfg)
  368. {
  369. struct device *dev = cfg->parent;
  370. struct platform_device *pdev = to_platform_device(dev);
  371. struct resource *res_pem;
  372. if (!dev->of_node)
  373. return -EINVAL;
  374. /*
  375. * The second register range is the PEM bridge to the PCIe
  376. * bus. It has a different config access method than those
  377. * devices behind the bridge.
  378. */
  379. res_pem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  380. if (!res_pem) {
  381. dev_err(dev, "missing \"reg[1]\"property\n");
  382. return -EINVAL;
  383. }
  384. return thunder_pem_init(dev, cfg, res_pem);
  385. }
  386. static struct pci_ecam_ops pci_thunder_pem_ops = {
  387. .bus_shift = 24,
  388. .init = thunder_pem_platform_init,
  389. .pci_ops = {
  390. .map_bus = pci_ecam_map_bus,
  391. .read = thunder_pem_config_read,
  392. .write = thunder_pem_config_write,
  393. }
  394. };
  395. static const struct of_device_id thunder_pem_of_match[] = {
  396. { .compatible = "cavium,pci-host-thunder-pem" },
  397. { },
  398. };
  399. static int thunder_pem_probe(struct platform_device *pdev)
  400. {
  401. return pci_host_common_probe(pdev, &pci_thunder_pem_ops);
  402. }
  403. static struct platform_driver thunder_pem_driver = {
  404. .driver = {
  405. .name = KBUILD_MODNAME,
  406. .of_match_table = thunder_pem_of_match,
  407. .suppress_bind_attrs = true,
  408. },
  409. .probe = thunder_pem_probe,
  410. };
  411. builtin_platform_driver(thunder_pem_driver);
  412. #endif
  413. #endif