pci-bridge-emul.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Marvell
  4. *
  5. * Author: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  6. *
  7. * This file helps PCI controller drivers implement a fake root port
  8. * PCI bridge when the HW doesn't provide such a root port PCI
  9. * bridge.
  10. *
  11. * It emulates a PCI bridge by providing a fake PCI configuration
  12. * space (and optionally a PCIe capability configuration space) in
  13. * memory. By default the read/write operations simply read and update
  14. * this fake configuration space in memory. However, PCI controller
  15. * drivers can provide through the 'struct pci_sw_bridge_ops'
  16. * structure a set of operations to override or complement this
  17. * default behavior.
  18. */
  19. #include <linux/pci.h>
  20. #include "pci-bridge-emul.h"
  21. #define PCI_BRIDGE_CONF_END PCI_STD_HEADER_SIZEOF
  22. #define PCI_CAP_PCIE_START PCI_BRIDGE_CONF_END
  23. #define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_EXP_SLTSTA2 + 2)
  24. /*
  25. * Initialize a pci_bridge_emul structure to represent a fake PCI
  26. * bridge configuration space. The caller needs to have initialized
  27. * the PCI configuration space with whatever values make sense
  28. * (typically at least vendor, device, revision), the ->ops pointer,
  29. * and optionally ->data and ->has_pcie.
  30. */
  31. void pci_bridge_emul_init(struct pci_bridge_emul *bridge)
  32. {
  33. bridge->conf.class_revision |= PCI_CLASS_BRIDGE_PCI << 16;
  34. bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE;
  35. bridge->conf.cache_line_size = 0x10;
  36. bridge->conf.status = PCI_STATUS_CAP_LIST;
  37. if (bridge->has_pcie) {
  38. bridge->conf.capabilities_pointer = PCI_CAP_PCIE_START;
  39. bridge->pcie_conf.cap_id = PCI_CAP_ID_EXP;
  40. /* Set PCIe v2, root port, slot support */
  41. bridge->pcie_conf.cap = PCI_EXP_TYPE_ROOT_PORT << 4 | 2 |
  42. PCI_EXP_FLAGS_SLOT;
  43. }
  44. }
  45. struct pci_bridge_reg_behavior {
  46. /* Read-only bits */
  47. u32 ro;
  48. /* Read-write bits */
  49. u32 rw;
  50. /* Write-1-to-clear bits */
  51. u32 w1c;
  52. /* Reserved bits (hardwired to 0) */
  53. u32 rsvd;
  54. };
  55. const static struct pci_bridge_reg_behavior pci_regs_behavior[] = {
  56. [PCI_VENDOR_ID / 4] = { .ro = ~0 },
  57. [PCI_COMMAND / 4] = {
  58. .rw = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  59. PCI_COMMAND_MASTER | PCI_COMMAND_PARITY |
  60. PCI_COMMAND_SERR),
  61. .ro = ((PCI_COMMAND_SPECIAL | PCI_COMMAND_INVALIDATE |
  62. PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_WAIT |
  63. PCI_COMMAND_FAST_BACK) |
  64. (PCI_STATUS_CAP_LIST | PCI_STATUS_66MHZ |
  65. PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MASK) << 16),
  66. .rsvd = GENMASK(15, 10) | ((BIT(6) | GENMASK(3, 0)) << 16),
  67. .w1c = (PCI_STATUS_PARITY |
  68. PCI_STATUS_SIG_TARGET_ABORT |
  69. PCI_STATUS_REC_TARGET_ABORT |
  70. PCI_STATUS_REC_MASTER_ABORT |
  71. PCI_STATUS_SIG_SYSTEM_ERROR |
  72. PCI_STATUS_DETECTED_PARITY) << 16,
  73. },
  74. [PCI_CLASS_REVISION / 4] = { .ro = ~0 },
  75. /*
  76. * Cache Line Size register: implement as read-only, we do not
  77. * pretend implementing "Memory Write and Invalidate"
  78. * transactions"
  79. *
  80. * Latency Timer Register: implemented as read-only, as "A
  81. * bridge that is not capable of a burst transfer of more than
  82. * two data phases on its primary interface is permitted to
  83. * hardwire the Latency Timer to a value of 16 or less"
  84. *
  85. * Header Type: always read-only
  86. *
  87. * BIST register: implemented as read-only, as "A bridge that
  88. * does not support BIST must implement this register as a
  89. * read-only register that returns 0 when read"
  90. */
  91. [PCI_CACHE_LINE_SIZE / 4] = { .ro = ~0 },
  92. /*
  93. * Base Address registers not used must be implemented as
  94. * read-only registers that return 0 when read.
  95. */
  96. [PCI_BASE_ADDRESS_0 / 4] = { .ro = ~0 },
  97. [PCI_BASE_ADDRESS_1 / 4] = { .ro = ~0 },
  98. [PCI_PRIMARY_BUS / 4] = {
  99. /* Primary, secondary and subordinate bus are RW */
  100. .rw = GENMASK(24, 0),
  101. /* Secondary latency is read-only */
  102. .ro = GENMASK(31, 24),
  103. },
  104. [PCI_IO_BASE / 4] = {
  105. /* The high four bits of I/O base/limit are RW */
  106. .rw = (GENMASK(15, 12) | GENMASK(7, 4)),
  107. /* The low four bits of I/O base/limit are RO */
  108. .ro = (((PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK |
  109. PCI_STATUS_DEVSEL_MASK) << 16) |
  110. GENMASK(11, 8) | GENMASK(3, 0)),
  111. .w1c = (PCI_STATUS_PARITY |
  112. PCI_STATUS_SIG_TARGET_ABORT |
  113. PCI_STATUS_REC_TARGET_ABORT |
  114. PCI_STATUS_REC_MASTER_ABORT |
  115. PCI_STATUS_SIG_SYSTEM_ERROR |
  116. PCI_STATUS_DETECTED_PARITY) << 16,
  117. .rsvd = ((BIT(6) | GENMASK(4, 0)) << 16),
  118. },
  119. [PCI_MEMORY_BASE / 4] = {
  120. /* The high 12-bits of mem base/limit are RW */
  121. .rw = GENMASK(31, 20) | GENMASK(15, 4),
  122. /* The low four bits of mem base/limit are RO */
  123. .ro = GENMASK(19, 16) | GENMASK(3, 0),
  124. },
  125. [PCI_PREF_MEMORY_BASE / 4] = {
  126. /* The high 12-bits of pref mem base/limit are RW */
  127. .rw = GENMASK(31, 20) | GENMASK(15, 4),
  128. /* The low four bits of pref mem base/limit are RO */
  129. .ro = GENMASK(19, 16) | GENMASK(3, 0),
  130. },
  131. [PCI_PREF_BASE_UPPER32 / 4] = {
  132. .rw = ~0,
  133. },
  134. [PCI_PREF_LIMIT_UPPER32 / 4] = {
  135. .rw = ~0,
  136. },
  137. [PCI_IO_BASE_UPPER16 / 4] = {
  138. .rw = ~0,
  139. },
  140. [PCI_CAPABILITY_LIST / 4] = {
  141. .ro = GENMASK(7, 0),
  142. .rsvd = GENMASK(31, 8),
  143. },
  144. [PCI_ROM_ADDRESS1 / 4] = {
  145. .rw = GENMASK(31, 11) | BIT(0),
  146. .rsvd = GENMASK(10, 1),
  147. },
  148. /*
  149. * Interrupt line (bits 7:0) are RW, interrupt pin (bits 15:8)
  150. * are RO, and bridge control (31:16) are a mix of RW, RO,
  151. * reserved and W1C bits
  152. */
  153. [PCI_INTERRUPT_LINE / 4] = {
  154. /* Interrupt line is RW */
  155. .rw = (GENMASK(7, 0) |
  156. ((PCI_BRIDGE_CTL_PARITY |
  157. PCI_BRIDGE_CTL_SERR |
  158. PCI_BRIDGE_CTL_ISA |
  159. PCI_BRIDGE_CTL_VGA |
  160. PCI_BRIDGE_CTL_MASTER_ABORT |
  161. PCI_BRIDGE_CTL_BUS_RESET |
  162. BIT(8) | BIT(9) | BIT(11)) << 16)),
  163. /* Interrupt pin is RO */
  164. .ro = (GENMASK(15, 8) | ((PCI_BRIDGE_CTL_FAST_BACK) << 16)),
  165. .w1c = BIT(10) << 16,
  166. .rsvd = (GENMASK(15, 12) | BIT(4)) << 16,
  167. },
  168. };
  169. const static struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
  170. [PCI_CAP_LIST_ID / 4] = {
  171. /*
  172. * Capability ID, Next Capability Pointer and
  173. * Capabilities register are all read-only.
  174. */
  175. .ro = ~0,
  176. },
  177. [PCI_EXP_DEVCAP / 4] = {
  178. .ro = ~0,
  179. },
  180. [PCI_EXP_DEVCTL / 4] = {
  181. /* Device control register is RW */
  182. .rw = GENMASK(15, 0),
  183. /*
  184. * Device status register has 4 bits W1C, then 2 bits
  185. * RO, the rest is reserved
  186. */
  187. .w1c = GENMASK(19, 16),
  188. .ro = GENMASK(20, 19),
  189. .rsvd = GENMASK(31, 21),
  190. },
  191. [PCI_EXP_LNKCAP / 4] = {
  192. /* All bits are RO, except bit 23 which is reserved */
  193. .ro = lower_32_bits(~BIT(23)),
  194. .rsvd = BIT(23),
  195. },
  196. [PCI_EXP_LNKCTL / 4] = {
  197. /*
  198. * Link control has bits [1:0] and [11:3] RW, the
  199. * other bits are reserved.
  200. * Link status has bits [13:0] RO, and bits [14:15]
  201. * W1C.
  202. */
  203. .rw = GENMASK(11, 3) | GENMASK(1, 0),
  204. .ro = GENMASK(13, 0) << 16,
  205. .w1c = GENMASK(15, 14) << 16,
  206. .rsvd = GENMASK(15, 12) | BIT(2),
  207. },
  208. [PCI_EXP_SLTCAP / 4] = {
  209. .ro = ~0,
  210. },
  211. [PCI_EXP_SLTCTL / 4] = {
  212. /*
  213. * Slot control has bits [12:0] RW, the rest is
  214. * reserved.
  215. *
  216. * Slot status has a mix of W1C and RO bits, as well
  217. * as reserved bits.
  218. */
  219. .rw = GENMASK(12, 0),
  220. .w1c = (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  221. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
  222. PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC) << 16,
  223. .ro = (PCI_EXP_SLTSTA_MRLSS | PCI_EXP_SLTSTA_PDS |
  224. PCI_EXP_SLTSTA_EIS) << 16,
  225. .rsvd = GENMASK(15, 12) | (GENMASK(15, 9) << 16),
  226. },
  227. [PCI_EXP_RTCTL / 4] = {
  228. /*
  229. * Root control has bits [4:0] RW, the rest is
  230. * reserved.
  231. *
  232. * Root status has bit 0 RO, the rest is reserved.
  233. */
  234. .rw = (PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
  235. PCI_EXP_RTCTL_SEFEE | PCI_EXP_RTCTL_PMEIE |
  236. PCI_EXP_RTCTL_CRSSVE),
  237. .ro = PCI_EXP_RTCAP_CRSVIS << 16,
  238. .rsvd = GENMASK(15, 5) | (GENMASK(15, 1) << 16),
  239. },
  240. [PCI_EXP_RTSTA / 4] = {
  241. .ro = GENMASK(15, 0) | PCI_EXP_RTSTA_PENDING,
  242. .w1c = PCI_EXP_RTSTA_PME,
  243. .rsvd = GENMASK(31, 18),
  244. },
  245. };
  246. /*
  247. * Should be called by the PCI controller driver when reading the PCI
  248. * configuration space of the fake bridge. It will call back the
  249. * ->ops->read_base or ->ops->read_pcie operations.
  250. */
  251. int pci_bridge_emul_conf_read(struct pci_bridge_emul *bridge, int where,
  252. int size, u32 *value)
  253. {
  254. int ret;
  255. int reg = where & ~3;
  256. pci_bridge_emul_read_status_t (*read_op)(struct pci_bridge_emul *bridge,
  257. int reg, u32 *value);
  258. u32 *cfgspace;
  259. const struct pci_bridge_reg_behavior *behavior;
  260. if (bridge->has_pcie && reg >= PCI_CAP_PCIE_END) {
  261. *value = 0;
  262. return PCIBIOS_SUCCESSFUL;
  263. }
  264. if (!bridge->has_pcie && reg >= PCI_BRIDGE_CONF_END) {
  265. *value = 0;
  266. return PCIBIOS_SUCCESSFUL;
  267. }
  268. if (bridge->has_pcie && reg >= PCI_CAP_PCIE_START) {
  269. reg -= PCI_CAP_PCIE_START;
  270. read_op = bridge->ops->read_pcie;
  271. cfgspace = (u32 *) &bridge->pcie_conf;
  272. behavior = pcie_cap_regs_behavior;
  273. } else {
  274. read_op = bridge->ops->read_base;
  275. cfgspace = (u32 *) &bridge->conf;
  276. behavior = pci_regs_behavior;
  277. }
  278. if (read_op)
  279. ret = read_op(bridge, reg, value);
  280. else
  281. ret = PCI_BRIDGE_EMUL_NOT_HANDLED;
  282. if (ret == PCI_BRIDGE_EMUL_NOT_HANDLED)
  283. *value = cfgspace[reg / 4];
  284. /*
  285. * Make sure we never return any reserved bit with a value
  286. * different from 0.
  287. */
  288. *value &= ~behavior[reg / 4].rsvd;
  289. if (size == 1)
  290. *value = (*value >> (8 * (where & 3))) & 0xff;
  291. else if (size == 2)
  292. *value = (*value >> (8 * (where & 3))) & 0xffff;
  293. else if (size != 4)
  294. return PCIBIOS_BAD_REGISTER_NUMBER;
  295. return PCIBIOS_SUCCESSFUL;
  296. }
  297. /*
  298. * Should be called by the PCI controller driver when writing the PCI
  299. * configuration space of the fake bridge. It will call back the
  300. * ->ops->write_base or ->ops->write_pcie operations.
  301. */
  302. int pci_bridge_emul_conf_write(struct pci_bridge_emul *bridge, int where,
  303. int size, u32 value)
  304. {
  305. int reg = where & ~3;
  306. int mask, ret, old, new, shift;
  307. void (*write_op)(struct pci_bridge_emul *bridge, int reg,
  308. u32 old, u32 new, u32 mask);
  309. u32 *cfgspace;
  310. const struct pci_bridge_reg_behavior *behavior;
  311. if (bridge->has_pcie && reg >= PCI_CAP_PCIE_END)
  312. return PCIBIOS_SUCCESSFUL;
  313. if (!bridge->has_pcie && reg >= PCI_BRIDGE_CONF_END)
  314. return PCIBIOS_SUCCESSFUL;
  315. shift = (where & 0x3) * 8;
  316. if (size == 4)
  317. mask = 0xffffffff;
  318. else if (size == 2)
  319. mask = 0xffff << shift;
  320. else if (size == 1)
  321. mask = 0xff << shift;
  322. else
  323. return PCIBIOS_BAD_REGISTER_NUMBER;
  324. ret = pci_bridge_emul_conf_read(bridge, reg, 4, &old);
  325. if (ret != PCIBIOS_SUCCESSFUL)
  326. return ret;
  327. if (bridge->has_pcie && reg >= PCI_CAP_PCIE_START) {
  328. reg -= PCI_CAP_PCIE_START;
  329. write_op = bridge->ops->write_pcie;
  330. cfgspace = (u32 *) &bridge->pcie_conf;
  331. behavior = pcie_cap_regs_behavior;
  332. } else {
  333. write_op = bridge->ops->write_base;
  334. cfgspace = (u32 *) &bridge->conf;
  335. behavior = pci_regs_behavior;
  336. }
  337. /* Keep all bits, except the RW bits */
  338. new = old & (~mask | ~behavior[reg / 4].rw);
  339. /* Update the value of the RW bits */
  340. new |= (value << shift) & (behavior[reg / 4].rw & mask);
  341. /* Clear the W1C bits */
  342. new &= ~((value << shift) & (behavior[reg / 4].w1c & mask));
  343. cfgspace[reg / 4] = new;
  344. if (write_op)
  345. write_op(bridge, reg, old, new, mask);
  346. return PCIBIOS_SUCCESSFUL;
  347. }