1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Marvell
5 * Author: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
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
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
20 #include <linux/pci.h>
21 #include "pci-bridge-emul.h"
23 #define PCI_BRIDGE_CONF_END PCI_STD_HEADER_SIZEOF
24 #define PCI_CAP_PCIE_START PCI_BRIDGE_CONF_END
25 #define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_EXP_SLTSTA2 + 2)
28 * Initialize a pci_bridge_emul structure to represent a fake PCI
29 * bridge configuration space. The caller needs to have initialized
30 * the PCI configuration space with whatever values make sense
31 * (typically at least vendor, device, revision), the ->ops pointer,
32 * and optionally ->data and ->has_pcie.
34 void pci_bridge_emul_init(struct pci_bridge_emul
*bridge
)
36 bridge
->conf
.class_revision
|= PCI_CLASS_BRIDGE_PCI
<< 16;
37 bridge
->conf
.header_type
= PCI_HEADER_TYPE_BRIDGE
;
38 bridge
->conf
.cache_line_size
= 0x10;
39 bridge
->conf
.status
= PCI_STATUS_CAP_LIST
;
41 if (bridge
->has_pcie
) {
42 bridge
->conf
.capabilities_pointer
= PCI_CAP_PCIE_START
;
43 bridge
->pcie_conf
.cap_id
= PCI_CAP_ID_EXP
;
44 /* Set PCIe v2, root port, slot support */
45 bridge
->pcie_conf
.cap
= PCI_EXP_TYPE_ROOT_PORT
<< 4 | 2 |
50 struct pci_bridge_reg_behavior
{
57 /* Write-1-to-clear bits */
60 /* Reserved bits (hardwired to 0) */
64 const static struct pci_bridge_reg_behavior pci_regs_behavior
[] = {
65 [PCI_VENDOR_ID
/ 4] = { .ro
= ~0 },
67 .rw
= (PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
68 PCI_COMMAND_MASTER
| PCI_COMMAND_PARITY
|
70 .ro
= ((PCI_COMMAND_SPECIAL
| PCI_COMMAND_INVALIDATE
|
71 PCI_COMMAND_VGA_PALETTE
| PCI_COMMAND_WAIT
|
72 PCI_COMMAND_FAST_BACK
) |
73 (PCI_STATUS_CAP_LIST
| PCI_STATUS_66MHZ
|
74 PCI_STATUS_FAST_BACK
| PCI_STATUS_DEVSEL_MASK
) << 16),
75 .rsvd
= GENMASK(15, 10) | ((BIT(6) | GENMASK(3, 0)) << 16),
76 .w1c
= (PCI_STATUS_PARITY
|
77 PCI_STATUS_SIG_TARGET_ABORT
|
78 PCI_STATUS_REC_TARGET_ABORT
|
79 PCI_STATUS_REC_MASTER_ABORT
|
80 PCI_STATUS_SIG_SYSTEM_ERROR
|
81 PCI_STATUS_DETECTED_PARITY
) << 16,
83 [PCI_CLASS_REVISION
/ 4] = { .ro
= ~0 },
86 * Cache Line Size register: implement as read-only, we do not
87 * pretend implementing "Memory Write and Invalidate"
90 * Latency Timer Register: implemented as read-only, as "A
91 * bridge that is not capable of a burst transfer of more than
92 * two data phases on its primary interface is permitted to
93 * hardwire the Latency Timer to a value of 16 or less"
95 * Header Type: always read-only
97 * BIST register: implemented as read-only, as "A bridge that
98 * does not support BIST must implement this register as a
99 * read-only register that returns 0 when read"
101 [PCI_CACHE_LINE_SIZE
/ 4] = { .ro
= ~0 },
104 * Base Address registers not used must be implemented as
105 * read-only registers that return 0 when read.
107 [PCI_BASE_ADDRESS_0
/ 4] = { .ro
= ~0 },
108 [PCI_BASE_ADDRESS_1
/ 4] = { .ro
= ~0 },
110 [PCI_PRIMARY_BUS
/ 4] = {
111 /* Primary, secondary and subordinate bus are RW */
112 .rw
= GENMASK(24, 0),
113 /* Secondary latency is read-only */
114 .ro
= GENMASK(31, 24),
117 [PCI_IO_BASE
/ 4] = {
118 /* The high four bits of I/O base/limit are RW */
119 .rw
= (GENMASK(15, 12) | GENMASK(7, 4)),
121 /* The low four bits of I/O base/limit are RO */
122 .ro
= (((PCI_STATUS_66MHZ
| PCI_STATUS_FAST_BACK
|
123 PCI_STATUS_DEVSEL_MASK
) << 16) |
124 GENMASK(11, 8) | GENMASK(3, 0)),
126 .w1c
= (PCI_STATUS_PARITY
|
127 PCI_STATUS_SIG_TARGET_ABORT
|
128 PCI_STATUS_REC_TARGET_ABORT
|
129 PCI_STATUS_REC_MASTER_ABORT
|
130 PCI_STATUS_SIG_SYSTEM_ERROR
|
131 PCI_STATUS_DETECTED_PARITY
) << 16,
133 .rsvd
= ((BIT(6) | GENMASK(4, 0)) << 16),
136 [PCI_MEMORY_BASE
/ 4] = {
137 /* The high 12-bits of mem base/limit are RW */
138 .rw
= GENMASK(31, 20) | GENMASK(15, 4),
140 /* The low four bits of mem base/limit are RO */
141 .ro
= GENMASK(19, 16) | GENMASK(3, 0),
144 [PCI_PREF_MEMORY_BASE
/ 4] = {
145 /* The high 12-bits of pref mem base/limit are RW */
146 .rw
= GENMASK(31, 20) | GENMASK(15, 4),
148 /* The low four bits of pref mem base/limit are RO */
149 .ro
= GENMASK(19, 16) | GENMASK(3, 0),
152 [PCI_PREF_BASE_UPPER32
/ 4] = {
156 [PCI_PREF_LIMIT_UPPER32
/ 4] = {
160 [PCI_IO_BASE_UPPER16
/ 4] = {
164 [PCI_CAPABILITY_LIST
/ 4] = {
166 .rsvd
= GENMASK(31, 8),
169 [PCI_ROM_ADDRESS1
/ 4] = {
170 .rw
= GENMASK(31, 11) | BIT(0),
171 .rsvd
= GENMASK(10, 1),
175 * Interrupt line (bits 7:0) are RW, interrupt pin (bits 15:8)
176 * are RO, and bridge control (31:16) are a mix of RW, RO,
177 * reserved and W1C bits
179 [PCI_INTERRUPT_LINE
/ 4] = {
180 /* Interrupt line is RW */
181 .rw
= (GENMASK(7, 0) |
182 ((PCI_BRIDGE_CTL_PARITY
|
183 PCI_BRIDGE_CTL_SERR
|
186 PCI_BRIDGE_CTL_MASTER_ABORT
|
187 PCI_BRIDGE_CTL_BUS_RESET
|
188 BIT(8) | BIT(9) | BIT(11)) << 16)),
190 /* Interrupt pin is RO */
191 .ro
= (GENMASK(15, 8) | ((PCI_BRIDGE_CTL_FAST_BACK
) << 16)),
193 .w1c
= BIT(10) << 16,
195 .rsvd
= (GENMASK(15, 12) | BIT(4)) << 16,
199 const static struct pci_bridge_reg_behavior pcie_cap_regs_behavior
[] = {
200 [PCI_CAP_LIST_ID
/ 4] = {
202 * Capability ID, Next Capability Pointer and
203 * Capabilities register are all read-only.
208 [PCI_EXP_DEVCAP
/ 4] = {
212 [PCI_EXP_DEVCTL
/ 4] = {
213 /* Device control register is RW */
214 .rw
= GENMASK(15, 0),
217 * Device status register has 4 bits W1C, then 2 bits
218 * RO, the rest is reserved
220 .w1c
= GENMASK(19, 16),
221 .ro
= GENMASK(20, 19),
222 .rsvd
= GENMASK(31, 21),
225 [PCI_EXP_LNKCAP
/ 4] = {
226 /* All bits are RO, except bit 23 which is reserved */
227 .ro
= lower_32_bits(~BIT(23)),
231 [PCI_EXP_LNKCTL
/ 4] = {
233 * Link control has bits [1:0] and [11:3] RW, the
234 * other bits are reserved.
235 * Link status has bits [13:0] RO, and bits [14:15]
238 .rw
= GENMASK(11, 3) | GENMASK(1, 0),
239 .ro
= GENMASK(13, 0) << 16,
240 .w1c
= GENMASK(15, 14) << 16,
241 .rsvd
= GENMASK(15, 12) | BIT(2),
244 [PCI_EXP_SLTCAP
/ 4] = {
248 [PCI_EXP_SLTCTL
/ 4] = {
250 * Slot control has bits [12:0] RW, the rest is
253 * Slot status has a mix of W1C and RO bits, as well
256 .rw
= GENMASK(12, 0),
257 .w1c
= (PCI_EXP_SLTSTA_ABP
| PCI_EXP_SLTSTA_PFD
|
258 PCI_EXP_SLTSTA_MRLSC
| PCI_EXP_SLTSTA_PDC
|
259 PCI_EXP_SLTSTA_CC
| PCI_EXP_SLTSTA_DLLSC
) << 16,
260 .ro
= (PCI_EXP_SLTSTA_MRLSS
| PCI_EXP_SLTSTA_PDS
|
261 PCI_EXP_SLTSTA_EIS
) << 16,
262 .rsvd
= GENMASK(15, 12) | (GENMASK(15, 9) << 16),
265 [PCI_EXP_RTCTL
/ 4] = {
267 * Root control has bits [4:0] RW, the rest is
270 * Root status has bit 0 RO, the rest is reserved.
272 .rw
= (PCI_EXP_RTCTL_SECEE
| PCI_EXP_RTCTL_SENFEE
|
273 PCI_EXP_RTCTL_SEFEE
| PCI_EXP_RTCTL_PMEIE
|
274 PCI_EXP_RTCTL_CRSSVE
),
275 .ro
= PCI_EXP_RTCAP_CRSVIS
<< 16,
276 .rsvd
= GENMASK(15, 5) | (GENMASK(15, 1) << 16),
279 [PCI_EXP_RTSTA
/ 4] = {
280 .ro
= GENMASK(15, 0) | PCI_EXP_RTSTA_PENDING
,
281 .w1c
= PCI_EXP_RTSTA_PME
,
282 .rsvd
= GENMASK(31, 18),
287 * Should be called by the PCI controller driver when reading the PCI
288 * configuration space of the fake bridge. It will call back the
289 * ->ops->read_base or ->ops->read_pcie operations.
291 int pci_bridge_emul_conf_read(struct pci_bridge_emul
*bridge
, int where
,
292 int size
, u32
*value
)
295 int reg
= where
& ~3;
296 pci_bridge_emul_read_status_t (*read_op
)(struct pci_bridge_emul
*bridge
,
297 int reg
, u32
*value
);
299 const struct pci_bridge_reg_behavior
*behavior
;
301 if (bridge
->has_pcie
&& reg
>= PCI_CAP_PCIE_END
) {
303 return PCIBIOS_SUCCESSFUL
;
306 if (!bridge
->has_pcie
&& reg
>= PCI_BRIDGE_CONF_END
) {
308 return PCIBIOS_SUCCESSFUL
;
311 if (bridge
->has_pcie
&& reg
>= PCI_CAP_PCIE_START
) {
312 reg
-= PCI_CAP_PCIE_START
;
313 read_op
= bridge
->ops
->read_pcie
;
314 cfgspace
= (u32
*) &bridge
->pcie_conf
;
315 behavior
= pcie_cap_regs_behavior
;
317 read_op
= bridge
->ops
->read_base
;
318 cfgspace
= (u32
*) &bridge
->conf
;
319 behavior
= pci_regs_behavior
;
323 ret
= read_op(bridge
, reg
, value
);
325 ret
= PCI_BRIDGE_EMUL_NOT_HANDLED
;
327 if (ret
== PCI_BRIDGE_EMUL_NOT_HANDLED
)
328 *value
= cfgspace
[reg
/ 4];
331 * Make sure we never return any reserved bit with a value
334 *value
&= ~behavior
[reg
/ 4].rsvd
;
337 *value
= (*value
>> (8 * (where
& 3))) & 0xff;
339 *value
= (*value
>> (8 * (where
& 3))) & 0xffff;
341 return PCIBIOS_BAD_REGISTER_NUMBER
;
343 return PCIBIOS_SUCCESSFUL
;
347 * Should be called by the PCI controller driver when writing the PCI
348 * configuration space of the fake bridge. It will call back the
349 * ->ops->write_base or ->ops->write_pcie operations.
351 int pci_bridge_emul_conf_write(struct pci_bridge_emul
*bridge
, int where
,
354 int reg
= where
& ~3;
355 int mask
, ret
, old
, new, shift
;
356 void (*write_op
)(struct pci_bridge_emul
*bridge
, int reg
,
357 u32 old
, u32
new, u32 mask
);
359 const struct pci_bridge_reg_behavior
*behavior
;
361 if (bridge
->has_pcie
&& reg
>= PCI_CAP_PCIE_END
)
362 return PCIBIOS_SUCCESSFUL
;
364 if (!bridge
->has_pcie
&& reg
>= PCI_BRIDGE_CONF_END
)
365 return PCIBIOS_SUCCESSFUL
;
367 shift
= (where
& 0x3) * 8;
372 mask
= 0xffff << shift
;
374 mask
= 0xff << shift
;
376 return PCIBIOS_BAD_REGISTER_NUMBER
;
378 ret
= pci_bridge_emul_conf_read(bridge
, reg
, 4, &old
);
379 if (ret
!= PCIBIOS_SUCCESSFUL
)
382 if (bridge
->has_pcie
&& reg
>= PCI_CAP_PCIE_START
) {
383 reg
-= PCI_CAP_PCIE_START
;
384 write_op
= bridge
->ops
->write_pcie
;
385 cfgspace
= (u32
*) &bridge
->pcie_conf
;
386 behavior
= pcie_cap_regs_behavior
;
388 write_op
= bridge
->ops
->write_base
;
389 cfgspace
= (u32
*) &bridge
->conf
;
390 behavior
= pci_regs_behavior
;
393 /* Keep all bits, except the RW bits */
394 new = old
& (~mask
| ~behavior
[reg
/ 4].rw
);
396 /* Update the value of the RW bits */
397 new |= (value
<< shift
) & (behavior
[reg
/ 4].rw
& mask
);
399 /* Clear the W1C bits */
400 new &= ~((value
<< shift
) & (behavior
[reg
/ 4].w1c
& mask
));
402 cfgspace
[reg
/ 4] = new;
405 write_op(bridge
, reg
, old
, new, mask
);
407 return PCIBIOS_SUCCESSFUL
;