Skip to content

Commit e0d9d30

Browse files
jaszczyk-grzegorzLorenzo Pieralisi
authored andcommitted
PCI: pci-bridge-emul: Fix big-endian support
Perform conversion to little-endian before every write to configuration space and convert it back to CPU endianness on reads. Additionally, initialise every multiple byte field of config space with the cpu_to_le* macro, which is required since the structure describing config space of emulated bridge assumes little-endian convention. Signed-off-by: Grzegorz Jaszczyk <[email protected]> Signed-off-by: Lorenzo Pieralisi <[email protected]>
1 parent e078723 commit e0d9d30

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

drivers/pci/pci-bridge-emul.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ static const struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
270270
int pci_bridge_emul_init(struct pci_bridge_emul *bridge,
271271
unsigned int flags)
272272
{
273-
bridge->conf.class_revision |= PCI_CLASS_BRIDGE_PCI << 16;
273+
bridge->conf.class_revision |= cpu_to_le32(PCI_CLASS_BRIDGE_PCI << 16);
274274
bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE;
275275
bridge->conf.cache_line_size = 0x10;
276-
bridge->conf.status = PCI_STATUS_CAP_LIST;
276+
bridge->conf.status = cpu_to_le16(PCI_STATUS_CAP_LIST);
277277
bridge->pci_regs_behavior = kmemdup(pci_regs_behavior,
278278
sizeof(pci_regs_behavior),
279279
GFP_KERNEL);
@@ -284,8 +284,9 @@ int pci_bridge_emul_init(struct pci_bridge_emul *bridge,
284284
bridge->conf.capabilities_pointer = PCI_CAP_PCIE_START;
285285
bridge->pcie_conf.cap_id = PCI_CAP_ID_EXP;
286286
/* Set PCIe v2, root port, slot support */
287-
bridge->pcie_conf.cap = PCI_EXP_TYPE_ROOT_PORT << 4 | 2 |
288-
PCI_EXP_FLAGS_SLOT;
287+
bridge->pcie_conf.cap =
288+
cpu_to_le16(PCI_EXP_TYPE_ROOT_PORT << 4 | 2 |
289+
PCI_EXP_FLAGS_SLOT);
289290
bridge->pcie_cap_regs_behavior =
290291
kmemdup(pcie_cap_regs_behavior,
291292
sizeof(pcie_cap_regs_behavior),
@@ -327,7 +328,7 @@ int pci_bridge_emul_conf_read(struct pci_bridge_emul *bridge, int where,
327328
int reg = where & ~3;
328329
pci_bridge_emul_read_status_t (*read_op)(struct pci_bridge_emul *bridge,
329330
int reg, u32 *value);
330-
u32 *cfgspace;
331+
__le32 *cfgspace;
331332
const struct pci_bridge_reg_behavior *behavior;
332333

333334
if (bridge->has_pcie && reg >= PCI_CAP_PCIE_END) {
@@ -343,11 +344,11 @@ int pci_bridge_emul_conf_read(struct pci_bridge_emul *bridge, int where,
343344
if (bridge->has_pcie && reg >= PCI_CAP_PCIE_START) {
344345
reg -= PCI_CAP_PCIE_START;
345346
read_op = bridge->ops->read_pcie;
346-
cfgspace = (u32 *) &bridge->pcie_conf;
347+
cfgspace = (__le32 *) &bridge->pcie_conf;
347348
behavior = bridge->pcie_cap_regs_behavior;
348349
} else {
349350
read_op = bridge->ops->read_base;
350-
cfgspace = (u32 *) &bridge->conf;
351+
cfgspace = (__le32 *) &bridge->conf;
351352
behavior = bridge->pci_regs_behavior;
352353
}
353354

@@ -357,7 +358,7 @@ int pci_bridge_emul_conf_read(struct pci_bridge_emul *bridge, int where,
357358
ret = PCI_BRIDGE_EMUL_NOT_HANDLED;
358359

359360
if (ret == PCI_BRIDGE_EMUL_NOT_HANDLED)
360-
*value = cfgspace[reg / 4];
361+
*value = le32_to_cpu(cfgspace[reg / 4]);
361362

362363
/*
363364
* Make sure we never return any reserved bit with a value
@@ -387,7 +388,7 @@ int pci_bridge_emul_conf_write(struct pci_bridge_emul *bridge, int where,
387388
int mask, ret, old, new, shift;
388389
void (*write_op)(struct pci_bridge_emul *bridge, int reg,
389390
u32 old, u32 new, u32 mask);
390-
u32 *cfgspace;
391+
__le32 *cfgspace;
391392
const struct pci_bridge_reg_behavior *behavior;
392393

393394
if (bridge->has_pcie && reg >= PCI_CAP_PCIE_END)
@@ -414,11 +415,11 @@ int pci_bridge_emul_conf_write(struct pci_bridge_emul *bridge, int where,
414415
if (bridge->has_pcie && reg >= PCI_CAP_PCIE_START) {
415416
reg -= PCI_CAP_PCIE_START;
416417
write_op = bridge->ops->write_pcie;
417-
cfgspace = (u32 *) &bridge->pcie_conf;
418+
cfgspace = (__le32 *) &bridge->pcie_conf;
418419
behavior = bridge->pcie_cap_regs_behavior;
419420
} else {
420421
write_op = bridge->ops->write_base;
421-
cfgspace = (u32 *) &bridge->conf;
422+
cfgspace = (__le32 *) &bridge->conf;
422423
behavior = bridge->pci_regs_behavior;
423424
}
424425

@@ -431,7 +432,7 @@ int pci_bridge_emul_conf_write(struct pci_bridge_emul *bridge, int where,
431432
/* Clear the W1C bits */
432433
new &= ~((value << shift) & (behavior[reg / 4].w1c & mask));
433434

434-
cfgspace[reg / 4] = new;
435+
cfgspace[reg / 4] = cpu_to_le32(new);
435436

436437
if (write_op)
437438
write_op(bridge, reg, old, new, mask);

drivers/pci/pci-bridge-emul.h

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,65 @@
66

77
/* PCI configuration space of a PCI-to-PCI bridge. */
88
struct pci_bridge_emul_conf {
9-
u16 vendor;
10-
u16 device;
11-
u16 command;
12-
u16 status;
13-
u32 class_revision;
9+
__le16 vendor;
10+
__le16 device;
11+
__le16 command;
12+
__le16 status;
13+
__le32 class_revision;
1414
u8 cache_line_size;
1515
u8 latency_timer;
1616
u8 header_type;
1717
u8 bist;
18-
u32 bar[2];
18+
__le32 bar[2];
1919
u8 primary_bus;
2020
u8 secondary_bus;
2121
u8 subordinate_bus;
2222
u8 secondary_latency_timer;
2323
u8 iobase;
2424
u8 iolimit;
25-
u16 secondary_status;
26-
u16 membase;
27-
u16 memlimit;
28-
u16 pref_mem_base;
29-
u16 pref_mem_limit;
30-
u32 prefbaseupper;
31-
u32 preflimitupper;
32-
u16 iobaseupper;
33-
u16 iolimitupper;
25+
__le16 secondary_status;
26+
__le16 membase;
27+
__le16 memlimit;
28+
__le16 pref_mem_base;
29+
__le16 pref_mem_limit;
30+
__le32 prefbaseupper;
31+
__le32 preflimitupper;
32+
__le16 iobaseupper;
33+
__le16 iolimitupper;
3434
u8 capabilities_pointer;
3535
u8 reserve[3];
36-
u32 romaddr;
36+
__le32 romaddr;
3737
u8 intline;
3838
u8 intpin;
39-
u16 bridgectrl;
39+
__le16 bridgectrl;
4040
};
4141

4242
/* PCI configuration space of the PCIe capabilities */
4343
struct pci_bridge_emul_pcie_conf {
4444
u8 cap_id;
4545
u8 next;
46-
u16 cap;
47-
u32 devcap;
48-
u16 devctl;
49-
u16 devsta;
50-
u32 lnkcap;
51-
u16 lnkctl;
52-
u16 lnksta;
53-
u32 slotcap;
54-
u16 slotctl;
55-
u16 slotsta;
56-
u16 rootctl;
57-
u16 rsvd;
58-
u32 rootsta;
59-
u32 devcap2;
60-
u16 devctl2;
61-
u16 devsta2;
62-
u32 lnkcap2;
63-
u16 lnkctl2;
64-
u16 lnksta2;
65-
u32 slotcap2;
66-
u16 slotctl2;
67-
u16 slotsta2;
46+
__le16 cap;
47+
__le32 devcap;
48+
__le16 devctl;
49+
__le16 devsta;
50+
__le32 lnkcap;
51+
__le16 lnkctl;
52+
__le16 lnksta;
53+
__le32 slotcap;
54+
__le16 slotctl;
55+
__le16 slotsta;
56+
__le16 rootctl;
57+
__le16 rsvd;
58+
__le32 rootsta;
59+
__le32 devcap2;
60+
__le16 devctl2;
61+
__le16 devsta2;
62+
__le32 lnkcap2;
63+
__le16 lnkctl2;
64+
__le16 lnksta2;
65+
__le32 slotcap2;
66+
__le16 slotctl2;
67+
__le16 slotsta2;
6868
};
6969

7070
struct pci_bridge_emul;

0 commit comments

Comments
 (0)