pci-thunder-pem.c 12 KB

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