Skip to contentSkip to conversation

hw/pci-host/gt64120.c: Fix PCI host bridge endianness handling

PATCH Superseded rakeshj 3 participants 5 messages Mar 29, 2025 – Mar 30, 2025

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

rakeshj

author
#0 lore raw

PATCH v2 1/1 hw/pci-host/gt64120.c: Fix PCI host bridge endianness handling

The GT-64120 PCI controller requires special handling where:
1. Host bridge (device 0) must use native endianness
2. Other devices follow MByteSwap bit in GT_PCI0_CMD

Previous implementation accidentally swapped all accesses, breaking host bridge detection (lspci -d 11ab:4620).

This patch:
- Adds custom read/write handlers to preserve native big-endian for the host bridge (phb->config_reg & 0x00fff800 == 0).
- Swaps non-bridge devices when MByteSwap = 0, using size-appropriate swaps (bswap16 for 2-byte, bswap32 for 4-byte) to convert PCI's little-endian data to match the MIPS guest's big-endian expectation; no swap occurs for the host bridge or when MByteSwap = 1 (little-endian mode).
- Removes gt64120_update_pci_cfgdata_mapping(), moving data_mem initialization to gt64120_realize()
- Removes unused pci_host_data_be_ops and a misleading comment in dino.h.

Fixes 145e2198 ("hw/mips/gt64xxx_pci: Endian-swap using PCI_HOST_BRIDGE MemoryRegionOps")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2826

---
 hw/pci-host/gt64120.c      | 83 ++++++++++++++++++++++----------------
 hw/pci/pci_host.c          |  6 ---
 include/hw/pci-host/dino.h |  5 +--
 include/hw/pci/pci_host.h  |  1 -
 4 files changed, 50 insertions(+), 45 deletions(-)
hw/pci-host/gt64120.c +49 -34
index d5c13a89b6..4e45d0aa53 100644
@@ -320,38 +320,6 @@ static void gt64120_isd_mapping(GT64120State *s)
     memory_region_transaction_commit();
 }
 
-static void gt64120_update_pci_cfgdata_mapping(GT64120State *s)
-{
-    /* Indexed on MByteSwap bit, see Table 158: PCI_0 Command, Offset: 0xc00 */
-    static const MemoryRegionOps *pci_host_data_ops[] = {
-        &pci_host_data_be_ops, &pci_host_data_le_ops
-    };
-    PCIHostState *phb = PCI_HOST_BRIDGE(s);
-
-    memory_region_transaction_begin();
-
-    /*
-     * The setting of the MByteSwap bit and MWordSwap bit in the PCI Internal
-     * Command Register determines how data transactions from the CPU to/from
-     * PCI are handled along with the setting of the Endianness bit in the CPU
-     * Configuration Register. See:
-     * - Table 16: 32-bit PCI Transaction Endianness
-     * - Table 158: PCI_0 Command, Offset: 0xc00
-     */
-
-    if (memory_region_is_mapped(&phb->data_mem)) {
-        memory_region_del_subregion(&s->ISD_mem, &phb->data_mem);
-        object_unparent(OBJECT(&phb->data_mem));
-    }
-    memory_region_init_io(&phb->data_mem, OBJECT(phb),
-                          pci_host_data_ops[s->regs[GT_PCI0_CMD] & 1],
-                          s, "pci-conf-data", 4);
-    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA << 2,
-                                        &phb->data_mem, 1);
-
-    memory_region_transaction_commit();
-}
-
 static void gt64120_pci_mapping(GT64120State *s)
 {
     memory_region_transaction_begin();
@@ -645,7 +613,6 @@ static void gt64120_writel(void *opaque, hwaddr addr,
     case GT_PCI0_CMD:
     case GT_PCI1_CMD:
         s->regs[saddr] = val & 0x0401fc0f;
-        gt64120_update_pci_cfgdata_mapping(s);
         break;
     case GT_PCI0_TOR:
     case GT_PCI0_BS_SCS10:
@@ -1024,6 +991,49 @@ static const MemoryRegionOps isd_mem_ops = {
     },
 };
 
+static uint64_t gt64120_pci_data_read(void *opaque, hwaddr addr, unsigned size)
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    uint32_t val = pci_data_read(phb->bus, phb->config_reg, size);
+    
+    /* Only swap for non-bridge devices in big-endian mode */
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800)) {
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val); 
+        }
+    }
+    return val;
+}
+
+static void gt64120_pci_data_write(void *opaque, hwaddr addr, 
+    uint64_t val, unsigned size)
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800)) {
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }
+    }
+    if (phb->config_reg & (1u << 31))
+        pci_data_write(phb->bus, phb->config_reg | (addr & 3), val, size);
+}
+
+static const MemoryRegionOps gt64120_pci_data_ops = {
+    .read = gt64120_pci_data_read,
+    .write = gt64120_pci_data_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};
+
 static void gt64120_reset(DeviceState *dev)
 {
     GT64120State *s = GT64120_PCI_HOST_BRIDGE(dev);
@@ -1178,7 +1188,6 @@ static void gt64120_reset(DeviceState *dev)
 
     gt64120_isd_mapping(s);
     gt64120_pci_mapping(s);
-    gt64120_update_pci_cfgdata_mapping(s);
 }
 
 static void gt64120_realize(DeviceState *dev, Error **errp)
@@ -1202,6 +1211,12 @@ static void gt64120_realize(DeviceState *dev, Error **errp)
     memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGADDR << 2,
                                         &phb->conf_mem, 1);
 
+    memory_region_init_io(&phb->data_mem, OBJECT(phb),
+                          &gt64120_pci_data_ops,
+                          s, "pci-conf-data", 4);
+    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA << 2,
+                                        &phb->data_mem, 1);
+
 
     /*
      * The whole address space decoded by the GT-64120A doesn't generate
hw/pci/pci_host.c +0 -6
index 80f91f409f..56f7f28a1a 100644
@@ -217,12 +217,6 @@ const MemoryRegionOps pci_host_data_le_ops = {
     .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
-const MemoryRegionOps pci_host_data_be_ops = {
-    .read = pci_host_data_read,
-    .write = pci_host_data_write,
-    .endianness = DEVICE_BIG_ENDIAN,
-};
-
 static bool pci_host_needed(void *opaque)
 {
     PCIHostState *s = opaque;
include/hw/pci-host/dino.h +1 -4
index fd7975c798..df509dbc18 100644
@@ -109,10 +109,7 @@ static const uint32_t reg800_keep_bits[DINO800_REGS] = {
 struct DinoState {
     PCIHostState parent_obj;
 
-    /*
-     * PCI_CONFIG_ADDR is parent_obj.config_reg, via pci_host_conf_be_ops,
-     * so that we can map PCI_CONFIG_DATA to pci_host_data_be_ops.
-     */
+
     uint32_t config_reg_dino; /* keep original copy, including 2 lowest bits */
 
     uint32_t iar0;
include/hw/pci/pci_host.h +0 -1
index e52d8ec2cd..954dd446fa 100644
@@ -68,6 +68,5 @@ uint32_t pci_data_read(PCIBus *s, uint32_t addr, unsigned len);
 extern const MemoryRegionOps pci_host_conf_le_ops;
 extern const MemoryRegionOps pci_host_conf_be_ops;
 extern const MemoryRegionOps pci_host_data_le_ops;
-extern const MemoryRegionOps pci_host_data_be_ops;
 
 #endif /* PCI_HOST_H */
Signature · 1 line
2.43.0
#1 lore raw

Hi Rakesh,

Quoted 10 lines · rakeshj

The GT-64120 PCI controller requires special handling where:
1. Host bridge (device 0) must use native endianness
2. Other devices follow MByteSwap bit in GT_PCI0_CMD

Previous implementation accidentally swapped all accesses, breaking host bridge detection (lspci -d 11ab:4620).

This patch:
- Adds custom read/write handlers to preserve native big-endian for the host bridge (phb->config_reg & 0x00fff800 == 0).

Here you check for bus == 0 && device == 0, is that what you want? (I'm confused because you only describe "for the host bridge").

If so I'd rather add a self-describing method:

static bool is_phb_dev0(const PCIHostState *phb)

 {
     return extract32(phb->config_reg, 11, 5 /* dev */ + 8 /* bus) == 0;

}

Quoted 70 lines

- Swaps non-bridge devices when MByteSwap = 0, using size-appropriate swaps (bswap16 for 2-byte, bswap32 for 4-byte) to convert PCI's little-endian data to match the MIPS guest's big-endian expectation; no swap occurs for the host bridge or when MByteSwap = 1 (little-endian mode).
- Removes gt64120_update_pci_cfgdata_mapping(), moving data_mem initialization to gt64120_realize()
- Removes unused pci_host_data_be_ops and a misleading comment in dino.h.

Fixes 145e2198 ("hw/mips/gt64xxx_pci: Endian-swap using PCI_HOST_BRIDGE MemoryRegionOps")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2826

---
  hw/pci-host/gt64120.c      | 83 ++++++++++++++++++++++----------------
  hw/pci/pci_host.c          |  6 ---
  include/hw/pci-host/dino.h |  5 +--
  include/hw/pci/pci_host.h  |  1 -
  4 files changed, 50 insertions(+), 45 deletions(-)
hw/pci-host/gt64120.c +0 -33
index d5c13a89b6..4e45d0aa53 100644
@@ -320,38 +320,6 @@ static void gt64120_isd_mapping(GT64120State *s)
      memory_region_transaction_commit();
  }
 
-static void gt64120_update_pci_cfgdata_mapping(GT64120State *s)
-{
-    /* Indexed on MByteSwap bit, see Table 158: PCI_0 Command, Offset: 0xc00 */
-    static const MemoryRegionOps *pci_host_data_ops[] = {
-        &pci_host_data_be_ops, &pci_host_data_le_ops
-    };
-    PCIHostState *phb = PCI_HOST_BRIDGE(s);
-
-    memory_region_transaction_begin();
-
-    /*
-     * The setting of the MByteSwap bit and MWordSwap bit in the PCI Internal
-     * Command Register determines how data transactions from the CPU to/from
-     * PCI are handled along with the setting of the Endianness bit in the CPU
-     * Configuration Register. See:
-     * - Table 16: 32-bit PCI Transaction Endianness
-     * - Table 158: PCI_0 Command, Offset: 0xc00
-     */
-
-    if (memory_region_is_mapped(&phb->data_mem)) {
-        memory_region_del_subregion(&s->ISD_mem, &phb->data_mem);
-        object_unparent(OBJECT(&phb->data_mem));
-    }
-    memory_region_init_io(&phb->data_mem, OBJECT(phb),
-                          pci_host_data_ops[s->regs[GT_PCI0_CMD] & 1],
-                          s, "pci-conf-data", 4);
-    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA << 2,
-                                        &phb->data_mem, 1);
-
-    memory_region_transaction_commit();
-}
-
  static void gt64120_pci_mapping(GT64120State *s)
  {
      memory_region_transaction_begin();
@@ -645,7 +613,6 @@ static void gt64120_writel(void *opaque, hwaddr addr,
      case GT_PCI0_CMD:
      case GT_PCI1_CMD:
          s->regs[saddr] = val & 0x0401fc0f;
-        gt64120_update_pci_cfgdata_mapping(s);
          break;
      case GT_PCI0_TOR:
      case GT_PCI0_BS_SCS10:

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 998

Quoted 8 lines
hw/pci-host/gt64120.c +5 -0
@@ -1024,6 +991,49 @@ static const MemoryRegionOps isd_mem_ops = {
      },
  };
 
+static uint64_t gt64120_pci_data_read(void *opaque, hwaddr addr, unsigned size)
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    uint32_t val = pci_data_read(phb->bus, phb->config_reg, size);

You check the Enable bit in the write path but not here, any reason?

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 1001

Quoted 3 lines
hw/pci-host/gt64120.c +3 -0
+
+    /* Only swap for non-bridge devices in big-endian mode */
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800)) {

Please use instead of (s->regs[GT_PCI0_CMD] & 1):

  bswap = FIELD_EX32(s->regs[GT_PCI0_CMD]m GT_PCI0_CMD, MByteSwap);
Quoted 24 lines
hw/pci-host/gt64120.c +24 -0
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }
+    }
+    return val;
+}
+
+static void gt64120_pci_data_write(void *opaque, hwaddr addr,
+    uint64_t val, unsigned size)
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800)) {
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }
+    }
+    if (phb->config_reg & (1u << 31))
+        pci_data_write(phb->bus, phb->config_reg | (addr & 3), val, size);
+}

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 1035

Quoted 10 lines
hw/pci-host/gt64120.c +10 -0
+
+static const MemoryRegionOps gt64120_pci_data_ops = {
+    .read = gt64120_pci_data_read,
+    .write = gt64120_pci_data_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

Per GT64120 rev 1.4, chapter "6.2.2 PCI Master CPU Byte Swapping":

When the GT–64120 is configured for 64-bit PCI mode, byte swapping occurs across all 8 byte lanes when the ByteSwap bit is set for PCI_0.

Quoted 14 lines
hw/pci-host/gt64120.c +0 -1
  static void gt64120_reset(DeviceState *dev)
  {
      GT64120State *s = GT64120_PCI_HOST_BRIDGE(dev);
@@ -1178,7 +1188,6 @@ static void gt64120_reset(DeviceState *dev)
 
      gt64120_isd_mapping(s);
      gt64120_pci_mapping(s);
-    gt64120_update_pci_cfgdata_mapping(s);
  }
 
  static void gt64120_realize(DeviceState *dev, Error **errp)
@@ -1202,6 +1211,12 @@ static void gt64120_realize(DeviceState *dev, Error **errp)
      memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGADDR << 2,
                                          &phb->conf_mem, 1);

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev, Error **errp) · line 1222

Quoted 10 lines
hw/pci-host/gt64120.c +6 -0
 
+    memory_region_init_io(&phb->data_mem, OBJECT(phb),
+                          &gt64120_pci_data_ops,
+                          s, "pci-conf-data", 4);
+    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA << 2,
+                                        &phb->data_mem, 1);
+
 
      /*
       * The whole address space decoded by the GT-64120A doesn't generate

Please split the changes below in a distinct cleanup patch.

Quoted 44 lines
hw/pci/pci_host.c +0 -6
index 80f91f409f..56f7f28a1a 100644
@@ -217,12 +217,6 @@ const MemoryRegionOps pci_host_data_le_ops = {
      .endianness = DEVICE_LITTLE_ENDIAN,
  };
 
-const MemoryRegionOps pci_host_data_be_ops = {
-    .read = pci_host_data_read,
-    .write = pci_host_data_write,
-    .endianness = DEVICE_BIG_ENDIAN,
-};
-
  static bool pci_host_needed(void *opaque)
  {
      PCIHostState *s = opaque;
include/hw/pci-host/dino.h +1 -4
index fd7975c798..df509dbc18 100644
@@ -109,10 +109,7 @@ static const uint32_t reg800_keep_bits[DINO800_REGS] = {
  struct DinoState {
      PCIHostState parent_obj;
 
-    /*
-     * PCI_CONFIG_ADDR is parent_obj.config_reg, via pci_host_conf_be_ops,
-     * so that we can map PCI_CONFIG_DATA to pci_host_data_be_ops.
-     */
+
      uint32_t config_reg_dino; /* keep original copy, including 2 lowest bits */
 
      uint32_t iar0;
include/hw/pci/pci_host.h +0 -1
index e52d8ec2cd..954dd446fa 100644
@@ -68,6 +68,5 @@ uint32_t pci_data_read(PCIBus *s, uint32_t addr, unsigned len);
  extern const MemoryRegionOps pci_host_conf_le_ops;
  extern const MemoryRegionOps pci_host_conf_be_ops;
  extern const MemoryRegionOps pci_host_data_le_ops;
-extern const MemoryRegionOps pci_host_data_be_ops;
 
  #endif /* PCI_HOST_H */
#2 lore raw
Quoted 18 lines · Philippe Mathieu-Daudé

Hi Rakesh,

Quoted 11 lines · rakeshj

The GT-64120 PCI controller requires special handling where:
1. Host bridge (device 0) must use native endianness
2. Other devices follow MByteSwap bit in GT_PCI0_CMD

Previous implementation accidentally swapped all accesses, breaking host bridge detection (lspci -d 11ab:4620).

This patch:
- Adds custom read/write handlers to preserve native big-endian for the host
bridge (phb->config_reg & 0x00fff800 == 0).

Here you check for bus == 0 && device == 0, is that what you want? (I'm confused because you only describe "for the host bridge").

Quoted 6 lines

If so I'd rather add a self-describing method:

static bool is_phb_dev0(const PCIHostState *phb)

{
    return extract32(phb->config_reg, 11, 5 /* dev */ + 8 /* bus) == 0;
}

There are already macros such as PCI_BUS_NUM PCI_FUNC. Are they any use instead of another function?

Regards,
BALATON Zoltan

Quoted 205 lines
Quoted 76 lines

- Swaps non-bridge devices when MByteSwap = 0, using size-appropriate swaps (bswap16 for 2-byte, bswap32 for 4-byte) to convert PCI's little-endian
data
to match the MIPS guest's big-endian expectation; no swap occurs for the
host
bridge or when MByteSwap = 1 (little-endian mode).
- Removes gt64120_update_pci_cfgdata_mapping(), moving data_mem initialization
to gt64120_realize()
- Removes unused pci_host_data_be_ops and a misleading comment in dino.h.

Fixes 145e2198 ("hw/mips/gt64xxx_pci: Endian-swap using PCI_HOST_BRIDGE MemoryRegionOps")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2826

---
  hw/pci-host/gt64120.c      | 83 ++++++++++++++++++++++----------------
  hw/pci/pci_host.c          |  6 ---
  include/hw/pci-host/dino.h |  5 +--
  include/hw/pci/pci_host.h  |  1 -
  4 files changed, 50 insertions(+), 45 deletions(-)
hw/pci-host/gt64120.c +0 -2
index d5c13a89b6..4e45d0aa53 100644
@@ -320,38 +320,6 @@ static void gt64120_isd_mapping(GT64120State *s)
      memory_region_transaction_commit();
  }
  -static void gt64120_update_pci_cfgdata_mapping(GT64120State *s)
-{
-    /* Indexed on MByteSwap bit, see Table 158: PCI_0 Command, Offset: 

0xc00 */

hw/pci-host/gt64120.c +0 -9
-    static const MemoryRegionOps *pci_host_data_ops[] = {
-        &pci_host_data_be_ops, &pci_host_data_le_ops
-    };
-    PCIHostState *phb = PCI_HOST_BRIDGE(s);
-
-    memory_region_transaction_begin();
-
-    /*
-     * The setting of the MByteSwap bit and MWordSwap bit in the PCI 

Internal
- * Command Register determines how data transactions from the CPU
to/from
- * PCI are handled along with the setting of the Endianness bit in the
CPU

hw/pci-host/gt64120.c +0 -19
-     * Configuration Register. See:
-     * - Table 16: 32-bit PCI Transaction Endianness
-     * - Table 158: PCI_0 Command, Offset: 0xc00
-     */
-
-    if (memory_region_is_mapped(&phb->data_mem)) {
-        memory_region_del_subregion(&s->ISD_mem, &phb->data_mem);
-        object_unparent(OBJECT(&phb->data_mem));
-    }
-    memory_region_init_io(&phb->data_mem, OBJECT(phb),
-                          pci_host_data_ops[s->regs[GT_PCI0_CMD] & 1],
-                          s, "pci-conf-data", 4);
-    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA << 2,
-                                        &phb->data_mem, 1);
-
-    memory_region_transaction_commit();
-}
-
  static void gt64120_pci_mapping(GT64120State *s)
  {
      memory_region_transaction_begin();
@@ -645,7 +613,6 @@ static void gt64120_writel(void *opaque, hwaddr addr,
      case GT_PCI0_CMD:
      case GT_PCI1_CMD:
          s->regs[saddr] = val & 0x0401fc0f;
-        gt64120_update_pci_cfgdata_mapping(s);
          break;
      case GT_PCI0_TOR:
      case GT_PCI0_BS_SCS10:

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 998

Quoted 8 lines
hw/pci-host/gt64120.c +4 -0
@@ -1024,6 +991,49 @@ static const MemoryRegionOps isd_mem_ops = {
      },
  };
  +static uint64_t gt64120_pci_data_read(void *opaque, hwaddr addr, 
 unsigned size)
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    uint32_t val = pci_data_read(phb->bus, phb->config_reg, size);

You check the Enable bit in the write path but not here, any reason?

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 1001

Quoted 3 lines
hw/pci-host/gt64120.c +3 -0
+
+    /* Only swap for non-bridge devices in big-endian mode */
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800)) {

Please use instead of (s->regs[GT_PCI0_CMD] & 1):

bswap = FIELD_EX32(s->regs[GT_PCI0_CMD]m GT_PCI0_CMD, MByteSwap);

Quoted 24 lines
hw/pci-host/gt64120.c +24 -0
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }
+    }
+    return val;
+}
+
+static void gt64120_pci_data_write(void *opaque, hwaddr addr,
+    uint64_t val, unsigned size)
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800)) {
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }
+    }
+    if (phb->config_reg & (1u << 31))
+        pci_data_write(phb->bus, phb->config_reg | (addr & 3), val, size);
+}

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 1035

Quoted 10 lines
hw/pci-host/gt64120.c +10 -0
+
+static const MemoryRegionOps gt64120_pci_data_ops = {
+    .read = gt64120_pci_data_read,
+    .write = gt64120_pci_data_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

Per GT64120 rev 1.4, chapter "6.2.2 PCI Master CPU Byte Swapping":

When the GT–64120 is configured for 64-bit PCI mode, byte swapping occurs across all 8 byte lanes when the ByteSwap bit is set for PCI_0.

Quoted 12 lines
hw/pci-host/gt64120.c +0 -1
  static void gt64120_reset(DeviceState *dev)
  {
      GT64120State *s = GT64120_PCI_HOST_BRIDGE(dev);
@@ -1178,7 +1188,6 @@ static void gt64120_reset(DeviceState *dev)
        gt64120_isd_mapping(s);
      gt64120_pci_mapping(s);
-    gt64120_update_pci_cfgdata_mapping(s);
  }
    static void gt64120_realize(DeviceState *dev, Error **errp)
@@ -1202,6 +1211,12 @@ static void gt64120_realize(DeviceState *dev, Error 
 **errp)
      memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGADDR << 

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev, Error · line 1222

Quoted 10 lines
hw/pci-host/gt64120.c +5 -0
 2,
                                          &phb->conf_mem, 1);
  +    memory_region_init_io(&phb->data_mem, OBJECT(phb),
+                          &gt64120_pci_data_ops,
+                          s, "pci-conf-data", 4);
+    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA << 2,
+                                        &phb->data_mem, 1);
+
        /*
       * The whole address space decoded by the GT-64120A doesn't generate

Please split the changes below in a distinct cleanup patch.

Quoted 43 lines
hw/pci/pci_host.c +0 -5
index 80f91f409f..56f7f28a1a 100644
@@ -217,12 +217,6 @@ const MemoryRegionOps pci_host_data_le_ops = {
      .endianness = DEVICE_LITTLE_ENDIAN,
  };
  -const MemoryRegionOps pci_host_data_be_ops = {
-    .read = pci_host_data_read,
-    .write = pci_host_data_write,
-    .endianness = DEVICE_BIG_ENDIAN,
-};
-
  static bool pci_host_needed(void *opaque)
  {
      PCIHostState *s = opaque;
include/hw/pci-host/dino.h +1 -3
index fd7975c798..df509dbc18 100644
@@ -109,10 +109,7 @@ static const uint32_t reg800_keep_bits[DINO800_REGS] = 
 {
  struct DinoState {
      PCIHostState parent_obj;
  -    /*
-     * PCI_CONFIG_ADDR is parent_obj.config_reg, via pci_host_conf_be_ops,
-     * so that we can map PCI_CONFIG_DATA to pci_host_data_be_ops.
-     */
+
      uint32_t config_reg_dino; /* keep original copy, including 2 lowest 

bits */

        uint32_t iar0;
include/hw/pci/pci_host.h +0 -1
index e52d8ec2cd..954dd446fa 100644
@@ -68,6 +68,5 @@ uint32_t pci_data_read(PCIBus *s, uint32_t addr, unsigned 
 len);
  extern const MemoryRegionOps pci_host_conf_le_ops;
  extern const MemoryRegionOps pci_host_conf_be_ops;
  extern const MemoryRegionOps pci_host_data_le_ops;
-extern const MemoryRegionOps pci_host_data_be_ops;
    #endif /* PCI_HOST_H */
author
#3 lore raw

Thanks, BALATON
I looked into PCI_BUS_NUM and PCI_SLOT from include/hw/pci/pci.h (L15-24):
- PCI_BUS_NUM(x) (((x) >> 8) & 0xff)) --> bits 15-8.
- PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)),
which don’t align properly with the 32-bit phb->config_reg layout used in your GT-64120 . Since these macros are intended for a 16-bit BDF (Bus-Device-Function) format rather than the full 32-bit PCI config address, For phb->config_reg (32-bit: bus 23-16, device 15-11) these extract the wrong bits

Quoted 257 lines · BALATON Zoltan
Quoted 18 lines · Philippe Mathieu-Daudé

Hi Rakesh,

Quoted 11 lines · rakeshj

The GT-64120 PCI controller requires special handling where:
1. Host bridge (device 0) must use native endianness
2. Other devices follow MByteSwap bit in GT_PCI0_CMD

Previous implementation accidentally swapped all accesses, breaking host bridge detection (lspci -d 11ab:4620).

This patch:
- Adds custom read/write handlers to preserve native big-endian for the host
bridge (phb->config_reg & 0x00fff800 == 0).

Here you check for bus == 0 && device == 0, is that what you want? (I'm confused because you only describe "for the host bridge").

Quoted 6 lines

If so I'd rather add a self-describing method:

static bool is_phb_dev0(const PCIHostState *phb)

{
    return extract32(phb->config_reg, 11, 5 /* dev */ + 8 /* bus) == 0;
}

There are already macros such as PCI_BUS_NUM PCI_FUNC. Are they any use instead of another function?

Regards,
BALATON Zoltan

Quoted 1 line
Quoted 1 line

- Swaps non-bridge devices when MByteSwap = 0, using size-appropriate

swaps

Quoted 1 line
Quoted 1 line

(bswap16 for 2-byte, bswap32 for 4-byte) to convert PCI's

little-endian

Quoted 2 lines
Quoted 2 lines

data
to match the MIPS guest's big-endian expectation; no swap occurs for

the

Quoted 6 lines
Quoted 6 lines

host
bridge or when MByteSwap = 1 (little-endian mode).
- Removes gt64120_update_pci_cfgdata_mapping(), moving data_mem initialization
to gt64120_realize()
- Removes unused pci_host_data_be_ops and a misleading comment in

dino.h.

Quoted 2 lines
Quoted 2 lines
Fixes 145e2198 ("hw/mips/gt64xxx_pci: Endian-swap using

PCI_HOST_BRIDGE

Quoted 26 lines
Quoted 26 lines

MemoryRegionOps")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2826

---
  hw/pci-host/gt64120.c      | 83 ++++++++++++++++++++++----------------
  hw/pci/pci_host.c          |  6 ---
  include/hw/pci-host/dino.h |  5 +--
  include/hw/pci/pci_host.h  |  1 -
  4 files changed, 50 insertions(+), 45 deletions(-)
hw/pci-host/gt64120.c +0 -2
index d5c13a89b6..4e45d0aa53 100644
@@ -320,38 +320,6 @@ static void gt64120_isd_mapping(GT64120State *s)
      memory_region_transaction_commit();
  }
  -static void gt64120_update_pci_cfgdata_mapping(GT64120State *s)
-{
-    /* Indexed on MByteSwap bit, see Table 158: PCI_0 Command, Offset:

0xc00 */

hw/pci-host/gt64120.c +0 -5
-    static const MemoryRegionOps *pci_host_data_ops[] = {
-        &pci_host_data_be_ops, &pci_host_data_le_ops
-    };
-    PCIHostState *phb = PCI_HOST_BRIDGE(s);
-
Quoted 8 lines
Quoted 8 lines
hw/pci-host/gt64120.c +0 -4
-    memory_region_transaction_begin();
-
-    /*
-     * The setting of the MByteSwap bit and MWordSwap bit in the PCI

Internal
- * Command Register determines how data transactions from the CPU
to/from
- * PCI are handled along with the setting of the Endianness bit in

the

Quoted 6 lines
Quoted 6 lines

CPU

hw/pci-host/gt64120.c +0 -5
-     * Configuration Register. See:
-     * - Table 16: 32-bit PCI Transaction Endianness
-     * - Table 158: PCI_0 Command, Offset: 0xc00
-     */
-
Quoted 8 lines

hw/pci-host/gt64120.c static void gt64120_isd_mapping(GT64120State *s)

Quoted 8 lines
hw/pci-host/gt64120.c +0 -8
-    if (memory_region_is_mapped(&phb->data_mem)) {
-        memory_region_del_subregion(&s->ISD_mem, &phb->data_mem);
-        object_unparent(OBJECT(&phb->data_mem));
-    }
-    memory_region_init_io(&phb->data_mem, OBJECT(phb),
-                          pci_host_data_ops[s->regs[GT_PCI0_CMD] & 1],
-                          s, "pci-conf-data", 4);
-    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA

<< 2,

Quoted 9 lines

hw/pci-host/gt64120.c static void gt64120_writel(void *opaque, hwaddr

Quoted 9 lines
hw/pci-host/gt64120.c +0 -5
-                                        &phb->data_mem, 1);
-
-    memory_region_transaction_commit();
-}
-
  static void gt64120_pci_mapping(GT64120State *s)
  {
      memory_region_transaction_begin();
@@ -645,7 +613,6 @@ static void gt64120_writel(void *opaque, hwaddr

addr,

Quoted 10 lines
Quoted 10 lines
hw/pci-host/gt64120.c +0 -1
      case GT_PCI0_CMD:
      case GT_PCI1_CMD:
          s->regs[saddr] = val & 0x0401fc0f;
-        gt64120_update_pci_cfgdata_mapping(s);
          break;
      case GT_PCI0_TOR:
      case GT_PCI0_BS_SCS10:
@@ -1024,6 +991,49 @@ static const MemoryRegionOps isd_mem_ops = {
      },
  };
Quoted 11 lines

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 998

Quoted 6 lines
hw/pci-host/gt64120.c +4 -0
  +static uint64_t gt64120_pci_data_read(void *opaque, hwaddr addr,
 unsigned size)
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    uint32_t val = pci_data_read(phb->bus, phb->config_reg, size);

You check the Enable bit in the write path but not here, any reason?

Quoted 3 lines
hw/pci-host/gt64120.c +3 -0
+
+    /* Only swap for non-bridge devices in big-endian mode */
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800))

{

Quoted 5 lines

Please use instead of (s->regs[GT_PCI0_CMD] & 1):

bswap = FIELD_EX32(s->regs[GT_PCI0_CMD]m GT_PCI0_CMD, MByteSwap);

Quoted 15 lines
Quoted 5 lines
hw/pci-host/gt64120.c +5 -0
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = {

Quoted 10 lines
hw/pci-host/gt64120.c +10 -0
+    }
+    return val;
+}
+
+static void gt64120_pci_data_write(void *opaque, hwaddr addr,
+    uint64_t val, unsigned size)
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800))

{

Quoted 8 lines

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = {

Quoted 8 lines
hw/pci-host/gt64120.c +8 -0
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }
+    }
+    if (phb->config_reg & (1u << 31))
+        pci_data_write(phb->bus, phb->config_reg | (addr & 3), val,

size);

Quoted 18 lines

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 1035

Quoted 11 lines
hw/pci-host/gt64120.c +11 -0
+}
+
+static const MemoryRegionOps gt64120_pci_data_ops = {
+    .read = gt64120_pci_data_read,
+    .write = gt64120_pci_data_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

Per GT64120 rev 1.4, chapter "6.2.2 PCI Master CPU Byte Swapping":

When the GT–64120 is configured for 64-bit PCI mode, byte swapping occurs across all 8 byte lanes when the ByteSwap bit is set for PCI_0.

Quoted 10 lines

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev,

Quoted 10 lines
hw/pci-host/gt64120.c +0 -1
  static void gt64120_reset(DeviceState *dev)
  {
      GT64120State *s = GT64120_PCI_HOST_BRIDGE(dev);
@@ -1178,7 +1188,6 @@ static void gt64120_reset(DeviceState *dev)
        gt64120_isd_mapping(s);
      gt64120_pci_mapping(s);
-    gt64120_update_pci_cfgdata_mapping(s);
  }
    static void gt64120_realize(DeviceState *dev, Error **errp)
@@ -1202,6 +1211,12 @@ static void gt64120_realize(DeviceState *dev,

Error

Quoted 2 lines

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev, · line 1212

Quoted 2 lines
hw/pci-host/gt64120.c +0 -0
 **errp)
      memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGADDR

<<

Quoted 6 lines

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev, Error **errp)

Quoted 6 lines
hw/pci-host/gt64120.c +3 -0
 2,
                                          &phb->conf_mem, 1);
  +    memory_region_init_io(&phb->data_mem, OBJECT(phb),
+                          &gt64120_pci_data_ops,
+                          s, "pci-conf-data", 4);
+    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA

<< 2,

Quoted 4 lines

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev, Error **errp)

Quoted 4 lines
hw/pci-host/gt64120.c +2 -0
+                                        &phb->data_mem, 1);
+
        /*
       * The whole address space decoded by the GT-64120A doesn't

generate

Quoted 16 lines

Please split the changes below in a distinct cleanup patch.

Quoted 13 lines
hw/pci/pci_host.c +0 -5
index 80f91f409f..56f7f28a1a 100644
@@ -217,12 +217,6 @@ const MemoryRegionOps pci_host_data_le_ops = {
      .endianness = DEVICE_LITTLE_ENDIAN,
  };
  -const MemoryRegionOps pci_host_data_be_ops = {
-    .read = pci_host_data_read,
-    .write = pci_host_data_write,
-    .endianness = DEVICE_BIG_ENDIAN,
-};
-
Quoted 8 lines

include/hw/pci-host/dino.h static const uint32_t

Quoted 8 lines
hw/pci/pci_host.c +0 -0
  static bool pci_host_needed(void *opaque)
  {
      PCIHostState *s = opaque;
include/hw/pci-host/dino.h +0 -0
index fd7975c798..df509dbc18 100644
@@ -109,10 +109,7 @@ static const uint32_t

reg800_keep_bits[DINO800_REGS] =

Quoted 5 lines

include/hw/pci-host/dino.h static const uint32_t · line 113

Quoted 5 lines
include/hw/pci-host/dino.h +0 -1
 {
  struct DinoState {
      PCIHostState parent_obj;
  -    /*
-     * PCI_CONFIG_ADDR is parent_obj.config_reg, via

pci_host_conf_be_ops,

Quoted 4 lines

include/hw/pci-host/dino.h static const uint32_t reg800_keep_bits[DINO800_REGS] = {

Quoted 4 lines
include/hw/pci-host/dino.h +1 -2
-     * so that we can map PCI_CONFIG_DATA to pci_host_data_be_ops.
-     */
+
      uint32_t config_reg_dino; /* keep original copy, including 2

lowest

Quoted 7 lines

include/hw/pci/pci_host.h uint32_t pci_data_read(PCIBus *s, uint32_t addr,

Quoted 7 lines

bits */

        uint32_t iar0;
include/hw/pci/pci_host.h +0 -0
index e52d8ec2cd..954dd446fa 100644
@@ -68,6 +68,5 @@ uint32_t pci_data_read(PCIBus *s, uint32_t addr,

unsigned

Quoted 8 lines
Quoted 6 lines
include/hw/pci/pci_host.h +0 -1
 len);
  extern const MemoryRegionOps pci_host_conf_le_ops;
  extern const MemoryRegionOps pci_host_conf_be_ops;
  extern const MemoryRegionOps pci_host_data_le_ops;
-extern const MemoryRegionOps pci_host_data_be_ops;
    #endif /* PCI_HOST_H */
author
#4 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé

Hi Rakesh,

Quoted 7 lines · rakeshj

The GT-64120 PCI controller requires special handling where:
1. Host bridge (device 0) must use native endianness
2. Other devices follow MByteSwap bit in GT_PCI0_CMD

Previous implementation accidentally swapped all accesses, breaking host bridge detection (lspci -d 11ab:4620).

Quoted 7 lines
Quoted 2 lines

This patch:
- Adds custom read/write handlers to preserve native big-endian for the

host

Quoted 1 line
   bridge (phb->config_reg & 0x00fff800 == 0).

Here you check for bus == 0 && device == 0, is that what you want? (I'm confused because you only describe "for the host bridge").

yes, I meant bus 0, device 0

Quoted 1 line

If so I'd rather add a self-describing method:

Quoted 89 lines
  static bool is_phb_dev0(const PCIHostState *phb)
  {
      return extract32(phb->config_reg, 11, 5 /* dev */ + 8 /* bus) == 0;
  }
Quoted 1 line

- Swaps non-bridge devices when MByteSwap = 0, using size-appropriate

swaps

Quoted 1 line

(bswap16 for 2-byte, bswap32 for 4-byte) to convert PCI's

little-endian data

Quoted 1 line

to match the MIPS guest's big-endian expectation; no swap occurs for

the host

Quoted 2 lines

bridge or when MByteSwap = 1 (little-endian mode).
- Removes gt64120_update_pci_cfgdata_mapping(), moving data_mem

initialization

Quoted 4 lines
   to gt64120_realize()

- Removes unused pci_host_data_be_ops and a misleading comment in dino.h.

Fixes 145e2198 ("hw/mips/gt64xxx_pci: Endian-swap using PCI_HOST_BRIDGE

MemoryRegionOps")

Quoted 14 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2826

---
  hw/pci-host/gt64120.c      | 83 ++++++++++++++++++++++----------------
  hw/pci/pci_host.c          |  6 ---
  include/hw/pci-host/dino.h |  5 +--
  include/hw/pci/pci_host.h  |  1 -
  4 files changed, 50 insertions(+), 45 deletions(-)
hw/pci-host/gt64120.c +0 -0
index d5c13a89b6..4e45d0aa53 100644

hw/pci-host/gt64120.c static void gt64120_isd_mapping(GT64120State *s) · line 325

Quoted 6 lines
hw/pci-host/gt64120.c +0 -3
@@ -320,38 +320,6 @@ static void gt64120_isd_mapping(GT64120State *s)
      memory_region_transaction_commit();
  }
 
-static void gt64120_update_pci_cfgdata_mapping(GT64120State *s)
-{
-    /* Indexed on MByteSwap bit, see Table 158: PCI_0 Command, Offset:

0xc00 */

hw/pci-host/gt64120.c static void gt64120_isd_mapping(GT64120State *s)

Quoted 9 lines
hw/pci-host/gt64120.c +0 -9
-    static const MemoryRegionOps *pci_host_data_ops[] = {
-        &pci_host_data_be_ops, &pci_host_data_le_ops
-    };
-    PCIHostState *phb = PCI_HOST_BRIDGE(s);
-
-    memory_region_transaction_begin();
-
-    /*
-     * The setting of the MByteSwap bit and MWordSwap bit in the PCI

Internal

hw/pci-host/gt64120.c static void gt64120_isd_mapping(GT64120State *s) · line 335

Quoted 1 line
hw/pci-host/gt64120.c +0 -1
-     * Command Register determines how data transactions from the CPU

to/from

hw/pci-host/gt64120.c static void gt64120_isd_mapping(GT64120State *s) · line 336

Quoted 1 line
hw/pci-host/gt64120.c +0 -1
-     * PCI are handled along with the setting of the Endianness bit in

the CPU

hw/pci-host/gt64120.c static void gt64120_isd_mapping(GT64120State *s)

Quoted 13 lines
hw/pci-host/gt64120.c +0 -13
-     * Configuration Register. See:
-     * - Table 16: 32-bit PCI Transaction Endianness
-     * - Table 158: PCI_0 Command, Offset: 0xc00
-     */
-
-    if (memory_region_is_mapped(&phb->data_mem)) {
-        memory_region_del_subregion(&s->ISD_mem, &phb->data_mem);
-        object_unparent(OBJECT(&phb->data_mem));
-    }
-    memory_region_init_io(&phb->data_mem, OBJECT(phb),
-                          pci_host_data_ops[s->regs[GT_PCI0_CMD] & 1],
-                          s, "pci-conf-data", 4);
-    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA <<

2,

Quoted 20 lines
hw/pci-host/gt64120.c +0 -6
-                                        &phb->data_mem, 1);
-
-    memory_region_transaction_commit();
-}
-
  static void gt64120_pci_mapping(GT64120State *s)
  {
      memory_region_transaction_begin();
@@ -645,7 +613,6 @@ static void gt64120_writel(void *opaque, hwaddr addr,
      case GT_PCI0_CMD:
      case GT_PCI1_CMD:
          s->regs[saddr] = val & 0x0401fc0f;
-        gt64120_update_pci_cfgdata_mapping(s);
          break;
      case GT_PCI0_TOR:
      case GT_PCI0_BS_SCS10:
@@ -1024,6 +991,49 @@ static const MemoryRegionOps isd_mem_ops = {
      },
  };
 
Quoted 9 lines

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 994

Quoted 1 line
hw/pci-host/gt64120.c +1 -0
+static uint64_t gt64120_pci_data_read(void *opaque, hwaddr addr,

unsigned size)

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 998

Quoted 4 lines
hw/pci-host/gt64120.c +4 -0
+{
+    GT64120State *s = opaque;
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    uint32_t val = pci_data_read(phb->bus, phb->config_reg, size);

You check the Enable bit in the write path but not here, any reason?

thanks for pointing it out, I missed to notice it

Quoted 116 lines

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 1001

Quoted 3 lines
hw/pci-host/gt64120.c +3 -0
+
+    /* Only swap for non-bridge devices in big-endian mode */
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800)) {

Please use instead of (s->regs[GT_PCI0_CMD] & 1):

   bswap = FIELD_EX32(s->regs[GT_PCI0_CMD]m GT_PCI0_CMD, MByteSwap);
Quoted 13 lines
hw/pci-host/gt64120.c +13 -0
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }
+    }
+    return val;
+}
+
+static void gt64120_pci_data_write(void *opaque, hwaddr addr,
+    uint64_t val, unsigned size)
+{
+    GT64120State *s = opaque;

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = {

Quoted 10 lines
hw/pci-host/gt64120.c +10 -0
+    PCIHostState *phb = PCI_HOST_BRIDGE(s);
+    if (!(s->regs[GT_PCI0_CMD] & 1) && (phb->config_reg & 0x00fff800)) {
+        if (size == 2) {
+            val = bswap16(val);
+        } else if (size == 4) {
+            val = bswap32(val);
+        }
+    }
+    if (phb->config_reg & (1u << 31))
+        pci_data_write(phb->bus, phb->config_reg | (addr & 3), val,

size);

hw/pci-host/gt64120.c static const MemoryRegionOps isd_mem_ops = { · line 1035

Quoted 11 lines
hw/pci-host/gt64120.c +11 -0
+}
+
+static const MemoryRegionOps gt64120_pci_data_ops = {
+    .read = gt64120_pci_data_read,
+    .write = gt64120_pci_data_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

Per GT64120 rev 1.4, chapter "6.2.2 PCI Master CPU Byte Swapping":

When the GT–64120 is configured for 64-bit PCI mode, byte swapping occurs across all 8 byte lanes when the ByteSwap bit is set for PCI_0.

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev,

Quoted 12 lines
hw/pci-host/gt64120.c +0 -1
  static void gt64120_reset(DeviceState *dev)
  {
      GT64120State *s = GT64120_PCI_HOST_BRIDGE(dev);
@@ -1178,7 +1188,6 @@ static void gt64120_reset(DeviceState *dev)
 
      gt64120_isd_mapping(s);
      gt64120_pci_mapping(s);
-    gt64120_update_pci_cfgdata_mapping(s);
  }
 
  static void gt64120_realize(DeviceState *dev, Error **errp)
@@ -1202,6 +1211,12 @@ static void gt64120_realize(DeviceState *dev,

Error **errp)

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev, · line 1211

Quoted 1 line
hw/pci-host/gt64120.c +0 -0
      memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGADDR

<< 2,

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev, Error **errp)

Quoted 6 lines
hw/pci-host/gt64120.c +4 -0
                                          &phb->conf_mem, 1);
 
+    memory_region_init_io(&phb->data_mem, OBJECT(phb),
+                          &gt64120_pci_data_ops,
+                          s, "pci-conf-data", 4);
+    memory_region_add_subregion_overlap(&s->ISD_mem, GT_PCI0_CFGDATA <<

2,

hw/pci-host/gt64120.c static void gt64120_realize(DeviceState *dev, Error **errp)

Quoted 5 lines
hw/pci-host/gt64120.c +2 -0
+                                        &phb->data_mem, 1);
+
 
      /*
       * The whole address space decoded by the GT-64120A doesn't

generate

Please split the changes below in a distinct cleanup patch.

include/hw/pci-host/dino.h static const uint32_t

Quoted 22 lines
hw/pci/pci_host.c +0 -6
index 80f91f409f..56f7f28a1a 100644
@@ -217,12 +217,6 @@ const MemoryRegionOps pci_host_data_le_ops = {
      .endianness = DEVICE_LITTLE_ENDIAN,
  };
 
-const MemoryRegionOps pci_host_data_be_ops = {
-    .read = pci_host_data_read,
-    .write = pci_host_data_write,
-    .endianness = DEVICE_BIG_ENDIAN,
-};
-
  static bool pci_host_needed(void *opaque)
  {
      PCIHostState *s = opaque;
include/hw/pci-host/dino.h +0 -0
index fd7975c798..df509dbc18 100644
@@ -109,10 +109,7 @@ static const uint32_t

reg800_keep_bits[DINO800_REGS] = {

include/hw/pci-host/dino.h static const uint32_t reg800_keep_bits[DINO800_REGS] = {

Quoted 5 lines
include/hw/pci-host/dino.h +0 -2
  struct DinoState {
      PCIHostState parent_obj;
 
-    /*
-     * PCI_CONFIG_ADDR is parent_obj.config_reg, via

pci_host_conf_be_ops,

include/hw/pci-host/dino.h static const uint32_t reg800_keep_bits[DINO800_REGS] = {

Quoted 4 lines
include/hw/pci-host/dino.h +1 -2
-     * so that we can map PCI_CONFIG_DATA to pci_host_data_be_ops.
-     */
+
      uint32_t config_reg_dino; /* keep original copy, including 2

lowest bits */

Quoted 1 line
Quoted 14 lines

include/hw/pci/pci_host.h uint32_t pci_data_read(PCIBus *s, uint32_t addr,

Quoted 6 lines
      uint32_t iar0;
include/hw/pci/pci_host.h +0 -0
index e52d8ec2cd..954dd446fa 100644
@@ -68,6 +68,5 @@ uint32_t pci_data_read(PCIBus *s, uint32_t addr,

unsigned len);

Quoted 6 lines
include/hw/pci/pci_host.h +0 -1
  extern const MemoryRegionOps pci_host_conf_le_ops;
  extern const MemoryRegionOps pci_host_conf_be_ops;
  extern const MemoryRegionOps pci_host_data_le_ops;
-extern const MemoryRegionOps pci_host_data_be_ops;
 
  #endif /* PCI_HOST_H */

Proposed v3 Changes:
1. Use extract32 in is_phb_dev0() for bus 0, device 0 check (Philippe).
2. FIELD_EX32 instead of (s->regs[GT_PCI0_CMD] & 1) (Philippe).
3. Size-specific swaps: bswap16 for 2-byte, bswap32 for 4-byte (Paolo).
4. check the Enable bit in the read path (Philippe).
5. Split cleanup into [PATCH v3 2/2].