pci-thunder-pem.c 12 KB

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