This source file includes following definitions.
- pci_bridge_emul_init
- pci_bridge_emul_cleanup
- pci_bridge_emul_conf_read
- pci_bridge_emul_conf_write
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <linux/pci.h>
21 #include "pci-bridge-emul.h"
22
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)
26
27 struct pci_bridge_reg_behavior {
28
29 u32 ro;
30
31
32 u32 rw;
33
34
35 u32 w1c;
36
37
38 u32 rsvd;
39 };
40
41 static const struct pci_bridge_reg_behavior pci_regs_behavior[] = {
42 [PCI_VENDOR_ID / 4] = { .ro = ~0 },
43 [PCI_COMMAND / 4] = {
44 .rw = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
45 PCI_COMMAND_MASTER | PCI_COMMAND_PARITY |
46 PCI_COMMAND_SERR),
47 .ro = ((PCI_COMMAND_SPECIAL | PCI_COMMAND_INVALIDATE |
48 PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_WAIT |
49 PCI_COMMAND_FAST_BACK) |
50 (PCI_STATUS_CAP_LIST | PCI_STATUS_66MHZ |
51 PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MASK) << 16),
52 .rsvd = GENMASK(15, 10) | ((BIT(6) | GENMASK(3, 0)) << 16),
53 .w1c = (PCI_STATUS_PARITY |
54 PCI_STATUS_SIG_TARGET_ABORT |
55 PCI_STATUS_REC_TARGET_ABORT |
56 PCI_STATUS_REC_MASTER_ABORT |
57 PCI_STATUS_SIG_SYSTEM_ERROR |
58 PCI_STATUS_DETECTED_PARITY) << 16,
59 },
60 [PCI_CLASS_REVISION / 4] = { .ro = ~0 },
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 [PCI_CACHE_LINE_SIZE / 4] = { .ro = ~0 },
79
80
81
82
83
84 [PCI_BASE_ADDRESS_0 / 4] = { .ro = ~0 },
85 [PCI_BASE_ADDRESS_1 / 4] = { .ro = ~0 },
86
87 [PCI_PRIMARY_BUS / 4] = {
88
89 .rw = GENMASK(24, 0),
90
91 .ro = GENMASK(31, 24),
92 },
93
94 [PCI_IO_BASE / 4] = {
95
96 .rw = (GENMASK(15, 12) | GENMASK(7, 4)),
97
98
99 .ro = (((PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK |
100 PCI_STATUS_DEVSEL_MASK) << 16) |
101 GENMASK(11, 8) | GENMASK(3, 0)),
102
103 .w1c = (PCI_STATUS_PARITY |
104 PCI_STATUS_SIG_TARGET_ABORT |
105 PCI_STATUS_REC_TARGET_ABORT |
106 PCI_STATUS_REC_MASTER_ABORT |
107 PCI_STATUS_SIG_SYSTEM_ERROR |
108 PCI_STATUS_DETECTED_PARITY) << 16,
109
110 .rsvd = ((BIT(6) | GENMASK(4, 0)) << 16),
111 },
112
113 [PCI_MEMORY_BASE / 4] = {
114
115 .rw = GENMASK(31, 20) | GENMASK(15, 4),
116
117
118 .ro = GENMASK(19, 16) | GENMASK(3, 0),
119 },
120
121 [PCI_PREF_MEMORY_BASE / 4] = {
122
123 .rw = GENMASK(31, 20) | GENMASK(15, 4),
124
125
126 .ro = GENMASK(19, 16) | GENMASK(3, 0),
127 },
128
129 [PCI_PREF_BASE_UPPER32 / 4] = {
130 .rw = ~0,
131 },
132
133 [PCI_PREF_LIMIT_UPPER32 / 4] = {
134 .rw = ~0,
135 },
136
137 [PCI_IO_BASE_UPPER16 / 4] = {
138 .rw = ~0,
139 },
140
141 [PCI_CAPABILITY_LIST / 4] = {
142 .ro = GENMASK(7, 0),
143 .rsvd = GENMASK(31, 8),
144 },
145
146 [PCI_ROM_ADDRESS1 / 4] = {
147 .rw = GENMASK(31, 11) | BIT(0),
148 .rsvd = GENMASK(10, 1),
149 },
150
151
152
153
154
155
156 [PCI_INTERRUPT_LINE / 4] = {
157
158 .rw = (GENMASK(7, 0) |
159 ((PCI_BRIDGE_CTL_PARITY |
160 PCI_BRIDGE_CTL_SERR |
161 PCI_BRIDGE_CTL_ISA |
162 PCI_BRIDGE_CTL_VGA |
163 PCI_BRIDGE_CTL_MASTER_ABORT |
164 PCI_BRIDGE_CTL_BUS_RESET |
165 BIT(8) | BIT(9) | BIT(11)) << 16)),
166
167
168 .ro = (GENMASK(15, 8) | ((PCI_BRIDGE_CTL_FAST_BACK) << 16)),
169
170 .w1c = BIT(10) << 16,
171
172 .rsvd = (GENMASK(15, 12) | BIT(4)) << 16,
173 },
174 };
175
176 static const struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
177 [PCI_CAP_LIST_ID / 4] = {
178
179
180
181
182 .ro = ~0,
183 },
184
185 [PCI_EXP_DEVCAP / 4] = {
186 .ro = ~0,
187 },
188
189 [PCI_EXP_DEVCTL / 4] = {
190
191 .rw = GENMASK(15, 0),
192
193
194
195
196
197 .w1c = GENMASK(19, 16),
198 .ro = GENMASK(20, 19),
199 .rsvd = GENMASK(31, 21),
200 },
201
202 [PCI_EXP_LNKCAP / 4] = {
203
204 .ro = lower_32_bits(~BIT(23)),
205 .rsvd = BIT(23),
206 },
207
208 [PCI_EXP_LNKCTL / 4] = {
209
210
211
212
213
214
215 .rw = GENMASK(11, 3) | GENMASK(1, 0),
216 .ro = GENMASK(13, 0) << 16,
217 .w1c = GENMASK(15, 14) << 16,
218 .rsvd = GENMASK(15, 12) | BIT(2),
219 },
220
221 [PCI_EXP_SLTCAP / 4] = {
222 .ro = ~0,
223 },
224
225 [PCI_EXP_SLTCTL / 4] = {
226
227
228
229
230
231
232
233 .rw = GENMASK(12, 0),
234 .w1c = (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
235 PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
236 PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC) << 16,
237 .ro = (PCI_EXP_SLTSTA_MRLSS | PCI_EXP_SLTSTA_PDS |
238 PCI_EXP_SLTSTA_EIS) << 16,
239 .rsvd = GENMASK(15, 12) | (GENMASK(15, 9) << 16),
240 },
241
242 [PCI_EXP_RTCTL / 4] = {
243
244
245
246
247
248
249 .rw = (PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
250 PCI_EXP_RTCTL_SEFEE | PCI_EXP_RTCTL_PMEIE |
251 PCI_EXP_RTCTL_CRSSVE),
252 .ro = PCI_EXP_RTCAP_CRSVIS << 16,
253 .rsvd = GENMASK(15, 5) | (GENMASK(15, 1) << 16),
254 },
255
256 [PCI_EXP_RTSTA / 4] = {
257 .ro = GENMASK(15, 0) | PCI_EXP_RTSTA_PENDING,
258 .w1c = PCI_EXP_RTSTA_PME,
259 .rsvd = GENMASK(31, 18),
260 },
261 };
262
263
264
265
266
267
268
269
270 int pci_bridge_emul_init(struct pci_bridge_emul *bridge,
271 unsigned int flags)
272 {
273 bridge->conf.class_revision |= PCI_CLASS_BRIDGE_PCI << 16;
274 bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE;
275 bridge->conf.cache_line_size = 0x10;
276 bridge->conf.status = PCI_STATUS_CAP_LIST;
277 bridge->pci_regs_behavior = kmemdup(pci_regs_behavior,
278 sizeof(pci_regs_behavior),
279 GFP_KERNEL);
280 if (!bridge->pci_regs_behavior)
281 return -ENOMEM;
282
283 if (bridge->has_pcie) {
284 bridge->conf.capabilities_pointer = PCI_CAP_PCIE_START;
285 bridge->pcie_conf.cap_id = PCI_CAP_ID_EXP;
286
287 bridge->pcie_conf.cap = PCI_EXP_TYPE_ROOT_PORT << 4 | 2 |
288 PCI_EXP_FLAGS_SLOT;
289 bridge->pcie_cap_regs_behavior =
290 kmemdup(pcie_cap_regs_behavior,
291 sizeof(pcie_cap_regs_behavior),
292 GFP_KERNEL);
293 if (!bridge->pcie_cap_regs_behavior) {
294 kfree(bridge->pci_regs_behavior);
295 return -ENOMEM;
296 }
297 }
298
299 if (flags & PCI_BRIDGE_EMUL_NO_PREFETCHABLE_BAR) {
300 bridge->pci_regs_behavior[PCI_PREF_MEMORY_BASE / 4].ro = ~0;
301 bridge->pci_regs_behavior[PCI_PREF_MEMORY_BASE / 4].rw = 0;
302 }
303
304 return 0;
305 }
306
307
308
309
310
311 void pci_bridge_emul_cleanup(struct pci_bridge_emul *bridge)
312 {
313 if (bridge->has_pcie)
314 kfree(bridge->pcie_cap_regs_behavior);
315 kfree(bridge->pci_regs_behavior);
316 }
317
318
319
320
321
322
323 int pci_bridge_emul_conf_read(struct pci_bridge_emul *bridge, int where,
324 int size, u32 *value)
325 {
326 int ret;
327 int reg = where & ~3;
328 pci_bridge_emul_read_status_t (*read_op)(struct pci_bridge_emul *bridge,
329 int reg, u32 *value);
330 u32 *cfgspace;
331 const struct pci_bridge_reg_behavior *behavior;
332
333 if (bridge->has_pcie && reg >= PCI_CAP_PCIE_END) {
334 *value = 0;
335 return PCIBIOS_SUCCESSFUL;
336 }
337
338 if (!bridge->has_pcie && reg >= PCI_BRIDGE_CONF_END) {
339 *value = 0;
340 return PCIBIOS_SUCCESSFUL;
341 }
342
343 if (bridge->has_pcie && reg >= PCI_CAP_PCIE_START) {
344 reg -= PCI_CAP_PCIE_START;
345 read_op = bridge->ops->read_pcie;
346 cfgspace = (u32 *) &bridge->pcie_conf;
347 behavior = bridge->pcie_cap_regs_behavior;
348 } else {
349 read_op = bridge->ops->read_base;
350 cfgspace = (u32 *) &bridge->conf;
351 behavior = bridge->pci_regs_behavior;
352 }
353
354 if (read_op)
355 ret = read_op(bridge, reg, value);
356 else
357 ret = PCI_BRIDGE_EMUL_NOT_HANDLED;
358
359 if (ret == PCI_BRIDGE_EMUL_NOT_HANDLED)
360 *value = cfgspace[reg / 4];
361
362
363
364
365
366 *value &= ~behavior[reg / 4].rsvd;
367
368 if (size == 1)
369 *value = (*value >> (8 * (where & 3))) & 0xff;
370 else if (size == 2)
371 *value = (*value >> (8 * (where & 3))) & 0xffff;
372 else if (size != 4)
373 return PCIBIOS_BAD_REGISTER_NUMBER;
374
375 return PCIBIOS_SUCCESSFUL;
376 }
377
378
379
380
381
382
383 int pci_bridge_emul_conf_write(struct pci_bridge_emul *bridge, int where,
384 int size, u32 value)
385 {
386 int reg = where & ~3;
387 int mask, ret, old, new, shift;
388 void (*write_op)(struct pci_bridge_emul *bridge, int reg,
389 u32 old, u32 new, u32 mask);
390 u32 *cfgspace;
391 const struct pci_bridge_reg_behavior *behavior;
392
393 if (bridge->has_pcie && reg >= PCI_CAP_PCIE_END)
394 return PCIBIOS_SUCCESSFUL;
395
396 if (!bridge->has_pcie && reg >= PCI_BRIDGE_CONF_END)
397 return PCIBIOS_SUCCESSFUL;
398
399 shift = (where & 0x3) * 8;
400
401 if (size == 4)
402 mask = 0xffffffff;
403 else if (size == 2)
404 mask = 0xffff << shift;
405 else if (size == 1)
406 mask = 0xff << shift;
407 else
408 return PCIBIOS_BAD_REGISTER_NUMBER;
409
410 ret = pci_bridge_emul_conf_read(bridge, reg, 4, &old);
411 if (ret != PCIBIOS_SUCCESSFUL)
412 return ret;
413
414 if (bridge->has_pcie && reg >= PCI_CAP_PCIE_START) {
415 reg -= PCI_CAP_PCIE_START;
416 write_op = bridge->ops->write_pcie;
417 cfgspace = (u32 *) &bridge->pcie_conf;
418 behavior = bridge->pcie_cap_regs_behavior;
419 } else {
420 write_op = bridge->ops->write_base;
421 cfgspace = (u32 *) &bridge->conf;
422 behavior = bridge->pci_regs_behavior;
423 }
424
425
426 new = old & (~mask | ~behavior[reg / 4].rw);
427
428
429 new |= (value << shift) & (behavior[reg / 4].rw & mask);
430
431
432 new &= ~((value << shift) & (behavior[reg / 4].w1c & mask));
433
434 cfgspace[reg / 4] = new;
435
436 if (write_op)
437 write_op(bridge, reg, old, new, mask);
438
439 return PCIBIOS_SUCCESSFUL;
440 }