pci-bridge-emul.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Marvell
  4. *
  5. * Author: Thomas Petazzoni <[email protected]>
  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_SSID_SIZEOF (PCI_SSVID_DEVICE_ID + 2)
  23. #define PCI_CAP_PCIE_SIZEOF (PCI_EXP_SLTSTA2 + 2)
  24. /**
  25. * struct pci_bridge_reg_behavior - register bits behaviors
  26. * @ro: Read-Only bits
  27. * @rw: Read-Write bits
  28. * @w1c: Write-1-to-Clear bits
  29. *
  30. * Reads and Writes will be filtered by specified behavior. All other bits not
  31. * declared are assumed 'Reserved' and will return 0 on reads, per PCIe 5.0:
  32. * "Reserved register fields must be read only and must return 0 (all 0's for
  33. * multi-bit fields) when read".
  34. */
  35. struct pci_bridge_reg_behavior {
  36. /* Read-only bits */
  37. u32 ro;
  38. /* Read-write bits */
  39. u32 rw;
  40. /* Write-1-to-clear bits */
  41. u32 w1c;
  42. };
  43. static const
  44. struct pci_bridge_reg_behavior pci_regs_behavior[PCI_STD_HEADER_SIZEOF / 4] = {
  45. [PCI_VENDOR_ID / 4] = { .ro = ~0 },
  46. [PCI_COMMAND / 4] = {
  47. .rw = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  48. PCI_COMMAND_MASTER | PCI_COMMAND_PARITY |
  49. PCI_COMMAND_SERR),
  50. .ro = ((PCI_COMMAND_SPECIAL | PCI_COMMAND_INVALIDATE |
  51. PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_WAIT |
  52. PCI_COMMAND_FAST_BACK) |
  53. (PCI_STATUS_CAP_LIST | PCI_STATUS_66MHZ |
  54. PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MASK) << 16),
  55. .w1c = PCI_STATUS_ERROR_BITS << 16,
  56. },
  57. [PCI_CLASS_REVISION / 4] = { .ro = ~0 },
  58. /*
  59. * Cache Line Size register: implement as read-only, we do not
  60. * pretend implementing "Memory Write and Invalidate"
  61. * transactions"
  62. *
  63. * Latency Timer Register: implemented as read-only, as "A
  64. * bridge that is not capable of a burst transfer of more than
  65. * two data phases on its primary interface is permitted to
  66. * hardwire the Latency Timer to a value of 16 or less"
  67. *
  68. * Header Type: always read-only
  69. *
  70. * BIST register: implemented as read-only, as "A bridge that
  71. * does not support BIST must implement this register as a
  72. * read-only register that returns 0 when read"
  73. */
  74. [PCI_CACHE_LINE_SIZE / 4] = { .ro = ~0 },
  75. /*
  76. * Base Address registers not used must be implemented as
  77. * read-only registers that return 0 when read.
  78. */
  79. [PCI_BASE_ADDRESS_0 / 4] = { .ro = ~0 },
  80. [PCI_BASE_ADDRESS_1 / 4] = { .ro = ~0 },
  81. [PCI_PRIMARY_BUS / 4] = {
  82. /* Primary, secondary and subordinate bus are RW */
  83. .rw = GENMASK(24, 0),
  84. /* Secondary latency is read-only */
  85. .ro = GENMASK(31, 24),
  86. },
  87. [PCI_IO_BASE / 4] = {
  88. /* The high four bits of I/O base/limit are RW */
  89. .rw = (GENMASK(15, 12) | GENMASK(7, 4)),
  90. /* The low four bits of I/O base/limit are RO */
  91. .ro = (((PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK |
  92. PCI_STATUS_DEVSEL_MASK) << 16) |
  93. GENMASK(11, 8) | GENMASK(3, 0)),
  94. .w1c = PCI_STATUS_ERROR_BITS << 16,
  95. },
  96. [PCI_MEMORY_BASE / 4] = {
  97. /* The high 12-bits of mem base/limit are RW */
  98. .rw = GENMASK(31, 20) | GENMASK(15, 4),
  99. /* The low four bits of mem base/limit are RO */
  100. .ro = GENMASK(19, 16) | GENMASK(3, 0),
  101. },
  102. [PCI_PREF_MEMORY_BASE / 4] = {
  103. /* The high 12-bits of pref mem base/limit are RW */
  104. .rw = GENMASK(31, 20) | GENMASK(15, 4),
  105. /* The low four bits of pref mem base/limit are RO */
  106. .ro = GENMASK(19, 16) | GENMASK(3, 0),
  107. },
  108. [PCI_PREF_BASE_UPPER32 / 4] = {
  109. .rw = ~0,
  110. },
  111. [PCI_PREF_LIMIT_UPPER32 / 4] = {
  112. .rw = ~0,
  113. },
  114. [PCI_IO_BASE_UPPER16 / 4] = {
  115. .rw = ~0,
  116. },
  117. [PCI_CAPABILITY_LIST / 4] = {
  118. .ro = GENMASK(7, 0),
  119. },
  120. /*
  121. * If expansion ROM is unsupported then ROM Base Address register must
  122. * be implemented as read-only register that return 0 when read, same
  123. * as for unused Base Address registers.
  124. */
  125. [PCI_ROM_ADDRESS1 / 4] = {
  126. .ro = ~0,
  127. },
  128. /*
  129. * Interrupt line (bits 7:0) are RW, interrupt pin (bits 15:8)
  130. * are RO, and bridge control (31:16) are a mix of RW, RO,
  131. * reserved and W1C bits
  132. */
  133. [PCI_INTERRUPT_LINE / 4] = {
  134. /* Interrupt line is RW */
  135. .rw = (GENMASK(7, 0) |
  136. ((PCI_BRIDGE_CTL_PARITY |
  137. PCI_BRIDGE_CTL_SERR |
  138. PCI_BRIDGE_CTL_ISA |
  139. PCI_BRIDGE_CTL_VGA |
  140. PCI_BRIDGE_CTL_MASTER_ABORT |
  141. PCI_BRIDGE_CTL_BUS_RESET |
  142. BIT(8) | BIT(9) | BIT(11)) << 16)),
  143. /* Interrupt pin is RO */
  144. .ro = (GENMASK(15, 8) | ((PCI_BRIDGE_CTL_FAST_BACK) << 16)),
  145. .w1c = BIT(10) << 16,
  146. },
  147. };
  148. static const
  149. struct pci_bridge_reg_behavior pcie_cap_regs_behavior[PCI_CAP_PCIE_SIZEOF / 4] = {
  150. [PCI_CAP_LIST_ID / 4] = {
  151. /*
  152. * Capability ID, Next Capability Pointer and
  153. * bits [14:0] of Capabilities register are all read-only.
  154. * Bit 15 of Capabilities register is reserved.
  155. */
  156. .ro = GENMASK(30, 0),
  157. },
  158. [PCI_EXP_DEVCAP / 4] = {
  159. /*
  160. * Bits [31:29] and [17:16] are reserved.
  161. * Bits [27:18] are reserved for non-upstream ports.
  162. * Bits 28 and [14:6] are reserved for non-endpoint devices.
  163. * Other bits are read-only.
  164. */
  165. .ro = BIT(15) | GENMASK(5, 0),
  166. },
  167. [PCI_EXP_DEVCTL / 4] = {
  168. /*
  169. * Device control register is RW, except bit 15 which is
  170. * reserved for non-endpoints or non-PCIe-to-PCI/X bridges.
  171. */
  172. .rw = GENMASK(14, 0),
  173. /*
  174. * Device status register has bits 6 and [3:0] W1C, [5:4] RO,
  175. * the rest is reserved. Also bit 6 is reserved for non-upstream
  176. * ports.
  177. */
  178. .w1c = GENMASK(3, 0) << 16,
  179. .ro = GENMASK(5, 4) << 16,
  180. },
  181. [PCI_EXP_LNKCAP / 4] = {
  182. /*
  183. * All bits are RO, except bit 23 which is reserved and
  184. * bit 18 which is reserved for non-upstream ports.
  185. */
  186. .ro = lower_32_bits(~(BIT(23) | PCI_EXP_LNKCAP_CLKPM)),
  187. },
  188. [PCI_EXP_LNKCTL / 4] = {
  189. /*
  190. * Link control has bits [15:14], [11:3] and [1:0] RW, the
  191. * rest is reserved. Bit 8 is reserved for non-upstream ports.
  192. *
  193. * Link status has bits [13:0] RO, and bits [15:14]
  194. * W1C.
  195. */
  196. .rw = GENMASK(15, 14) | GENMASK(11, 9) | GENMASK(7, 3) | GENMASK(1, 0),
  197. .ro = GENMASK(13, 0) << 16,
  198. .w1c = GENMASK(15, 14) << 16,
  199. },
  200. [PCI_EXP_SLTCAP / 4] = {
  201. .ro = ~0,
  202. },
  203. [PCI_EXP_SLTCTL / 4] = {
  204. /*
  205. * Slot control has bits [14:0] RW, the rest is
  206. * reserved.
  207. *
  208. * Slot status has bits 8 and [4:0] W1C, bits [7:5] RO, the
  209. * rest is reserved.
  210. */
  211. .rw = GENMASK(14, 0),
  212. .w1c = (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  213. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
  214. PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC) << 16,
  215. .ro = (PCI_EXP_SLTSTA_MRLSS | PCI_EXP_SLTSTA_PDS |
  216. PCI_EXP_SLTSTA_EIS) << 16,
  217. },
  218. [PCI_EXP_RTCTL / 4] = {
  219. /*
  220. * Root control has bits [4:0] RW, the rest is
  221. * reserved.
  222. *
  223. * Root capabilities has bit 0 RO, the rest is reserved.
  224. */
  225. .rw = (PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
  226. PCI_EXP_RTCTL_SEFEE | PCI_EXP_RTCTL_PMEIE |
  227. PCI_EXP_RTCTL_CRSSVE),
  228. .ro = PCI_EXP_RTCAP_CRSVIS << 16,
  229. },
  230. [PCI_EXP_RTSTA / 4] = {
  231. /*
  232. * Root status has bits 17 and [15:0] RO, bit 16 W1C, the rest
  233. * is reserved.
  234. */
  235. .ro = GENMASK(15, 0) | PCI_EXP_RTSTA_PENDING,
  236. .w1c = PCI_EXP_RTSTA_PME,
  237. },
  238. [PCI_EXP_DEVCAP2 / 4] = {
  239. /*
  240. * Device capabilities 2 register has reserved bits [30:27].
  241. * Also bits [26:24] are reserved for non-upstream ports.
  242. */
  243. .ro = BIT(31) | GENMASK(23, 0),
  244. },
  245. [PCI_EXP_DEVCTL2 / 4] = {
  246. /*
  247. * Device control 2 register is RW. Bit 11 is reserved for
  248. * non-upstream ports.
  249. *
  250. * Device status 2 register is reserved.
  251. */
  252. .rw = GENMASK(15, 12) | GENMASK(10, 0),
  253. },
  254. [PCI_EXP_LNKCAP2 / 4] = {
  255. /* Link capabilities 2 register has reserved bits [30:25] and 0. */
  256. .ro = BIT(31) | GENMASK(24, 1),
  257. },
  258. [PCI_EXP_LNKCTL2 / 4] = {
  259. /*
  260. * Link control 2 register is RW.
  261. *
  262. * Link status 2 register has bits 5, 15 W1C;
  263. * bits 10, 11 reserved and others are RO.
  264. */
  265. .rw = GENMASK(15, 0),
  266. .w1c = (BIT(15) | BIT(5)) << 16,
  267. .ro = (GENMASK(14, 12) | GENMASK(9, 6) | GENMASK(4, 0)) << 16,
  268. },
  269. [PCI_EXP_SLTCAP2 / 4] = {
  270. /* Slot capabilities 2 register is reserved. */
  271. },
  272. [PCI_EXP_SLTCTL2 / 4] = {
  273. /* Both Slot control 2 and Slot status 2 registers are reserved. */
  274. },
  275. };
  276. static pci_bridge_emul_read_status_t
  277. pci_bridge_emul_read_ssid(struct pci_bridge_emul *bridge, int reg, u32 *value)
  278. {
  279. switch (reg) {
  280. case PCI_CAP_LIST_ID:
  281. *value = PCI_CAP_ID_SSVID |
  282. ((bridge->pcie_start > bridge->ssid_start) ? (bridge->pcie_start << 8) : 0);
  283. return PCI_BRIDGE_EMUL_HANDLED;
  284. case PCI_SSVID_VENDOR_ID:
  285. *value = bridge->subsystem_vendor_id |
  286. (bridge->subsystem_id << 16);
  287. return PCI_BRIDGE_EMUL_HANDLED;
  288. default:
  289. return PCI_BRIDGE_EMUL_NOT_HANDLED;
  290. }
  291. }
  292. /*
  293. * Initialize a pci_bridge_emul structure to represent a fake PCI
  294. * bridge configuration space. The caller needs to have initialized
  295. * the PCI configuration space with whatever values make sense
  296. * (typically at least vendor, device, revision), the ->ops pointer,
  297. * and optionally ->data and ->has_pcie.
  298. */
  299. int pci_bridge_emul_init(struct pci_bridge_emul *bridge,
  300. unsigned int flags)
  301. {
  302. BUILD_BUG_ON(sizeof(bridge->conf) != PCI_BRIDGE_CONF_END);
  303. /*
  304. * class_revision: Class is high 24 bits and revision is low 8 bit
  305. * of this member, while class for PCI Bridge Normal Decode has the
  306. * 24-bit value: PCI_CLASS_BRIDGE_PCI_NORMAL
  307. */
  308. bridge->conf.class_revision |=
  309. cpu_to_le32(PCI_CLASS_BRIDGE_PCI_NORMAL << 8);
  310. bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE;
  311. bridge->conf.cache_line_size = 0x10;
  312. bridge->conf.status = cpu_to_le16(PCI_STATUS_CAP_LIST);
  313. bridge->pci_regs_behavior = kmemdup(pci_regs_behavior,
  314. sizeof(pci_regs_behavior),
  315. GFP_KERNEL);
  316. if (!bridge->pci_regs_behavior)
  317. return -ENOMEM;
  318. /* If ssid_start and pcie_start were not specified then choose the lowest possible value. */
  319. if (!bridge->ssid_start && !bridge->pcie_start) {
  320. if (bridge->subsystem_vendor_id)
  321. bridge->ssid_start = PCI_BRIDGE_CONF_END;
  322. if (bridge->has_pcie)
  323. bridge->pcie_start = bridge->ssid_start + PCI_CAP_SSID_SIZEOF;
  324. } else if (!bridge->ssid_start && bridge->subsystem_vendor_id) {
  325. if (bridge->pcie_start - PCI_BRIDGE_CONF_END >= PCI_CAP_SSID_SIZEOF)
  326. bridge->ssid_start = PCI_BRIDGE_CONF_END;
  327. else
  328. bridge->ssid_start = bridge->pcie_start + PCI_CAP_PCIE_SIZEOF;
  329. } else if (!bridge->pcie_start && bridge->has_pcie) {
  330. if (bridge->ssid_start - PCI_BRIDGE_CONF_END >= PCI_CAP_PCIE_SIZEOF)
  331. bridge->pcie_start = PCI_BRIDGE_CONF_END;
  332. else
  333. bridge->pcie_start = bridge->ssid_start + PCI_CAP_SSID_SIZEOF;
  334. }
  335. bridge->conf.capabilities_pointer = min(bridge->ssid_start, bridge->pcie_start);
  336. if (bridge->conf.capabilities_pointer)
  337. bridge->conf.status |= cpu_to_le16(PCI_STATUS_CAP_LIST);
  338. if (bridge->has_pcie) {
  339. bridge->pcie_conf.cap_id = PCI_CAP_ID_EXP;
  340. bridge->pcie_conf.next = (bridge->ssid_start > bridge->pcie_start) ?
  341. bridge->ssid_start : 0;
  342. bridge->pcie_conf.cap |= cpu_to_le16(PCI_EXP_TYPE_ROOT_PORT << 4);
  343. bridge->pcie_cap_regs_behavior =
  344. kmemdup(pcie_cap_regs_behavior,
  345. sizeof(pcie_cap_regs_behavior),
  346. GFP_KERNEL);
  347. if (!bridge->pcie_cap_regs_behavior) {
  348. kfree(bridge->pci_regs_behavior);
  349. return -ENOMEM;
  350. }
  351. /* These bits are applicable only for PCI and reserved on PCIe */
  352. bridge->pci_regs_behavior[PCI_CACHE_LINE_SIZE / 4].ro &=
  353. ~GENMASK(15, 8);
  354. bridge->pci_regs_behavior[PCI_COMMAND / 4].ro &=
  355. ~((PCI_COMMAND_SPECIAL | PCI_COMMAND_INVALIDATE |
  356. PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_WAIT |
  357. PCI_COMMAND_FAST_BACK) |
  358. (PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK |
  359. PCI_STATUS_DEVSEL_MASK) << 16);
  360. bridge->pci_regs_behavior[PCI_PRIMARY_BUS / 4].ro &=
  361. ~GENMASK(31, 24);
  362. bridge->pci_regs_behavior[PCI_IO_BASE / 4].ro &=
  363. ~((PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK |
  364. PCI_STATUS_DEVSEL_MASK) << 16);
  365. bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].rw &=
  366. ~((PCI_BRIDGE_CTL_MASTER_ABORT |
  367. BIT(8) | BIT(9) | BIT(11)) << 16);
  368. bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].ro &=
  369. ~((PCI_BRIDGE_CTL_FAST_BACK) << 16);
  370. bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].w1c &=
  371. ~(BIT(10) << 16);
  372. }
  373. if (flags & PCI_BRIDGE_EMUL_NO_PREFMEM_FORWARD) {
  374. bridge->pci_regs_behavior[PCI_PREF_MEMORY_BASE / 4].ro = ~0;
  375. bridge->pci_regs_behavior[PCI_PREF_MEMORY_BASE / 4].rw = 0;
  376. }
  377. if (flags & PCI_BRIDGE_EMUL_NO_IO_FORWARD) {
  378. bridge->pci_regs_behavior[PCI_COMMAND / 4].ro |= PCI_COMMAND_IO;
  379. bridge->pci_regs_behavior[PCI_COMMAND / 4].rw &= ~PCI_COMMAND_IO;
  380. bridge->pci_regs_behavior[PCI_IO_BASE / 4].ro |= GENMASK(15, 0);
  381. bridge->pci_regs_behavior[PCI_IO_BASE / 4].rw &= ~GENMASK(15, 0);
  382. bridge->pci_regs_behavior[PCI_IO_BASE_UPPER16 / 4].ro = ~0;
  383. bridge->pci_regs_behavior[PCI_IO_BASE_UPPER16 / 4].rw = 0;
  384. }
  385. return 0;
  386. }
  387. EXPORT_SYMBOL_GPL(pci_bridge_emul_init);
  388. /*
  389. * Cleanup a pci_bridge_emul structure that was previously initialized
  390. * using pci_bridge_emul_init().
  391. */
  392. void pci_bridge_emul_cleanup(struct pci_bridge_emul *bridge)
  393. {
  394. if (bridge->has_pcie)
  395. kfree(bridge->pcie_cap_regs_behavior);
  396. kfree(bridge->pci_regs_behavior);
  397. }
  398. EXPORT_SYMBOL_GPL(pci_bridge_emul_cleanup);
  399. /*
  400. * Should be called by the PCI controller driver when reading the PCI
  401. * configuration space of the fake bridge. It will call back the
  402. * ->ops->read_base or ->ops->read_pcie operations.
  403. */
  404. int pci_bridge_emul_conf_read(struct pci_bridge_emul *bridge, int where,
  405. int size, u32 *value)
  406. {
  407. int ret;
  408. int reg = where & ~3;
  409. pci_bridge_emul_read_status_t (*read_op)(struct pci_bridge_emul *bridge,
  410. int reg, u32 *value);
  411. __le32 *cfgspace;
  412. const struct pci_bridge_reg_behavior *behavior;
  413. if (reg < PCI_BRIDGE_CONF_END) {
  414. /* Emulated PCI space */
  415. read_op = bridge->ops->read_base;
  416. cfgspace = (__le32 *) &bridge->conf;
  417. behavior = bridge->pci_regs_behavior;
  418. } else if (reg >= bridge->ssid_start && reg < bridge->ssid_start + PCI_CAP_SSID_SIZEOF &&
  419. bridge->subsystem_vendor_id) {
  420. /* Emulated PCI Bridge Subsystem Vendor ID capability */
  421. reg -= bridge->ssid_start;
  422. read_op = pci_bridge_emul_read_ssid;
  423. cfgspace = NULL;
  424. behavior = NULL;
  425. } else if (reg >= bridge->pcie_start && reg < bridge->pcie_start + PCI_CAP_PCIE_SIZEOF &&
  426. bridge->has_pcie) {
  427. /* Our emulated PCIe capability */
  428. reg -= bridge->pcie_start;
  429. read_op = bridge->ops->read_pcie;
  430. cfgspace = (__le32 *) &bridge->pcie_conf;
  431. behavior = bridge->pcie_cap_regs_behavior;
  432. } else if (reg >= PCI_CFG_SPACE_SIZE && bridge->has_pcie) {
  433. /* PCIe extended capability space */
  434. reg -= PCI_CFG_SPACE_SIZE;
  435. read_op = bridge->ops->read_ext;
  436. cfgspace = NULL;
  437. behavior = NULL;
  438. } else {
  439. /* Not implemented */
  440. *value = 0;
  441. return PCIBIOS_SUCCESSFUL;
  442. }
  443. if (read_op)
  444. ret = read_op(bridge, reg, value);
  445. else
  446. ret = PCI_BRIDGE_EMUL_NOT_HANDLED;
  447. if (ret == PCI_BRIDGE_EMUL_NOT_HANDLED) {
  448. if (cfgspace)
  449. *value = le32_to_cpu(cfgspace[reg / 4]);
  450. else
  451. *value = 0;
  452. }
  453. /*
  454. * Make sure we never return any reserved bit with a value
  455. * different from 0.
  456. */
  457. if (behavior)
  458. *value &= behavior[reg / 4].ro | behavior[reg / 4].rw |
  459. behavior[reg / 4].w1c;
  460. if (size == 1)
  461. *value = (*value >> (8 * (where & 3))) & 0xff;
  462. else if (size == 2)
  463. *value = (*value >> (8 * (where & 3))) & 0xffff;
  464. else if (size != 4)
  465. return PCIBIOS_BAD_REGISTER_NUMBER;
  466. return PCIBIOS_SUCCESSFUL;
  467. }
  468. EXPORT_SYMBOL_GPL(pci_bridge_emul_conf_read);
  469. /*
  470. * Should be called by the PCI controller driver when writing the PCI
  471. * configuration space of the fake bridge. It will call back the
  472. * ->ops->write_base or ->ops->write_pcie operations.
  473. */
  474. int pci_bridge_emul_conf_write(struct pci_bridge_emul *bridge, int where,
  475. int size, u32 value)
  476. {
  477. int reg = where & ~3;
  478. int mask, ret, old, new, shift;
  479. void (*write_op)(struct pci_bridge_emul *bridge, int reg,
  480. u32 old, u32 new, u32 mask);
  481. __le32 *cfgspace;
  482. const struct pci_bridge_reg_behavior *behavior;
  483. ret = pci_bridge_emul_conf_read(bridge, reg, 4, &old);
  484. if (ret != PCIBIOS_SUCCESSFUL)
  485. return ret;
  486. if (reg < PCI_BRIDGE_CONF_END) {
  487. /* Emulated PCI space */
  488. write_op = bridge->ops->write_base;
  489. cfgspace = (__le32 *) &bridge->conf;
  490. behavior = bridge->pci_regs_behavior;
  491. } else if (reg >= bridge->pcie_start && reg < bridge->pcie_start + PCI_CAP_PCIE_SIZEOF &&
  492. bridge->has_pcie) {
  493. /* Our emulated PCIe capability */
  494. reg -= bridge->pcie_start;
  495. write_op = bridge->ops->write_pcie;
  496. cfgspace = (__le32 *) &bridge->pcie_conf;
  497. behavior = bridge->pcie_cap_regs_behavior;
  498. } else if (reg >= PCI_CFG_SPACE_SIZE && bridge->has_pcie) {
  499. /* PCIe extended capability space */
  500. reg -= PCI_CFG_SPACE_SIZE;
  501. write_op = bridge->ops->write_ext;
  502. cfgspace = NULL;
  503. behavior = NULL;
  504. } else {
  505. /* Not implemented */
  506. return PCIBIOS_SUCCESSFUL;
  507. }
  508. shift = (where & 0x3) * 8;
  509. if (size == 4)
  510. mask = 0xffffffff;
  511. else if (size == 2)
  512. mask = 0xffff << shift;
  513. else if (size == 1)
  514. mask = 0xff << shift;
  515. else
  516. return PCIBIOS_BAD_REGISTER_NUMBER;
  517. if (behavior) {
  518. /* Keep all bits, except the RW bits */
  519. new = old & (~mask | ~behavior[reg / 4].rw);
  520. /* Update the value of the RW bits */
  521. new |= (value << shift) & (behavior[reg / 4].rw & mask);
  522. /* Clear the W1C bits */
  523. new &= ~((value << shift) & (behavior[reg / 4].w1c & mask));
  524. } else {
  525. new = old & ~mask;
  526. new |= (value << shift) & mask;
  527. }
  528. if (cfgspace) {
  529. /* Save the new value with the cleared W1C bits into the cfgspace */
  530. cfgspace[reg / 4] = cpu_to_le32(new);
  531. }
  532. if (behavior) {
  533. /*
  534. * Clear the W1C bits not specified by the write mask, so that the
  535. * write_op() does not clear them.
  536. */
  537. new &= ~(behavior[reg / 4].w1c & ~mask);
  538. /*
  539. * Set the W1C bits specified by the write mask, so that write_op()
  540. * knows about that they are to be cleared.
  541. */
  542. new |= (value << shift) & (behavior[reg / 4].w1c & mask);
  543. }
  544. if (write_op)
  545. write_op(bridge, reg, old, new, mask);
  546. return PCIBIOS_SUCCESSFUL;
  547. }
  548. EXPORT_SYMBOL_GPL(pci_bridge_emul_conf_write);