pci-thunder-pem.c 12 KB

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