pci-thunder-pem.c 10 KB

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