pci-thunder-pem.c 12 KB

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