Skip to contentSkip to conversation

gt64xxx: access right I/O port when activating byte swapping

PATCH In review Hervé Poussineau 2 participants 4 messages May 20, 2016 – Jul 22, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gt64xxx: access right I/O port when activating byte swapping

Incidentally, this fixes YAMON on big endian guest.

---
 hw/mips/gt64xxx_pci.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)
hw/mips/gt64xxx_pci.c +60 -2
index 3f4523d..c76ee88 100644
@@ -177,6 +177,7 @@
 
 /* PCI Internal */
 #define GT_PCI0_CMD    		(0xc00 >> 2)
+#define   GT_CMD_MWORDSWAP      (1 << 10)
 #define GT_PCI0_TOR    		(0xc04 >> 2)
 #define GT_PCI0_BS_SCS10    	(0xc08 >> 2)
 #define GT_PCI0_BS_SCS32    	(0xc0c >> 2)
@@ -294,6 +295,62 @@ static void gt64120_isd_mapping(GT64120State *s)
     memory_region_add_subregion(get_system_memory(), s->ISD_start, &s->ISD_mem);
 }
 
+static uint64_t gt64120_pci_io_read(void *opaque, hwaddr addr,
+                                    unsigned int size)
+{
+    GT64120State *s = opaque;
+    uint8_t buf[4];
+
+    if (s->regs[GT_PCI0_CMD] & GT_CMD_MWORDSWAP) {
+        addr = (addr & ~3) + 4 - size - (addr & 3);
+    }
+
+    address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED,
+                       buf, size);
+
+    if (size == 1) {
+        return buf[0];
+    } else if (size == 2) {
+        return lduw_le_p(buf);
+    } else if (size == 4) {
+        return ldl_le_p(buf);
+    } else {
+        g_assert_not_reached();
+    }
+}
+
+static void gt64120_pci_io_write(void *opaque, hwaddr addr, uint64_t data,
+                                 unsigned int size)
+{
+    GT64120State *s = opaque;
+    uint8_t buf[4];
+
+    if (s->regs[GT_PCI0_CMD] & GT_CMD_MWORDSWAP) {
+        addr = (addr & ~3) + 4 - size - (addr & 3);
+    }
+
+    if (size == 1) {
+        buf[0] = data;
+    } else if (size == 2) {
+        stw_le_p(buf, data);
+    } else if (size == 4) {
+        stl_le_p(buf, data);
+    } else {
+        g_assert_not_reached();
+    }
+
+    address_space_write(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED,
+                        buf, size);
+}
+
+static const MemoryRegionOps gt64120_pci_io_ops = {
+    .read = gt64120_pci_io_read,
+    .write = gt64120_pci_io_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .impl.max_access_size = 4,
+    .valid.unaligned = true,
+};
+
 static void gt64120_pci_mapping(GT64120State *s)
 {
     /* Update PCI0IO mapping */
@@ -308,8 +365,9 @@ static void gt64120_pci_mapping(GT64120State *s)
         s->PCI0IO_length = ((s->regs[GT_PCI0IOHD] + 1) -
                             (s->regs[GT_PCI0IOLD] & 0x7f)) << 21;
         if (s->PCI0IO_length) {
-            memory_region_init_alias(&s->PCI0IO_mem, OBJECT(s), "pci0-io",
-                                     get_system_io(), 0, s->PCI0IO_length);
+            memory_region_init_io(&s->PCI0IO_mem, OBJECT(s),
+                                  &gt64120_pci_io_ops,
+                                  s, "pci0-io", s->PCI0IO_length);
             memory_region_add_subregion(get_system_memory(), s->PCI0IO_start,
                                         &s->PCI0IO_mem);
         }
Signature · 1 line
2.1.4
#1 lore raw
Quoted 20 lines · Hervé Poussineau

Incidentally, this fixes YAMON on big endian guest.

---
 hw/mips/gt64xxx_pci.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)
hw/mips/gt64xxx_pci.c +1 -0
index 3f4523d..c76ee88 100644
@@ -177,6 +177,7 @@
 
 /* PCI Internal */
 #define GT_PCI0_CMD    		(0xc00 >> 2)
+#define   GT_CMD_MWORDSWAP      (1 << 10)
 #define GT_PCI0_TOR    		(0xc04 >> 2)
 #define GT_PCI0_BS_SCS10    	(0xc08 >> 2)
 #define GT_PCI0_BS_SCS32    	(0xc0c >> 2)

hw/mips/gt64xxx_pci.c static void gt64120_isd_mapping(GT64120State *s) · line 304

Quoted 10 lines
hw/mips/gt64xxx_pci.c +7 -0
@@ -294,6 +295,62 @@ static void gt64120_isd_mapping(GT64120State *s)
     memory_region_add_subregion(get_system_memory(), s->ISD_start, &s->ISD_mem);
 }
 
+static uint64_t gt64120_pci_io_read(void *opaque, hwaddr addr,
+                                    unsigned int size)
+{
+    GT64120State *s = opaque;
+    uint8_t buf[4];
+
+    if (s->regs[GT_PCI0_CMD] & GT_CMD_MWORDSWAP) {

First of all, it should be noted that this bit doesn't control byte swapping, but swaps the 2 4-byte words in a 8-byte word.

hw/mips/gt64xxx_pci.c static void gt64120_isd_mapping(GT64120State *s) · line 305

Quoted 1 line
hw/mips/gt64xxx_pci.c +1 -0
+        addr = (addr & ~3) + 4 - size - (addr & 3);

This looks complicated, and I don't think it is correct. In addition this doesn't behave correctly at the edges of the address space. For example a 2 byte access at address 0x3 would access address 0xffffffffffffffff.

For sizes <= 4, swapping the 2 words should be done with addr ^= 4. Maybe you should also check for MBYTESWAP which also swaps the bytes within a 8-byte word.

Quoted 4 lines
hw/mips/gt64xxx_pci.c +4 -0
+    }
+
+    address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED,
+                       buf, size);

hw/mips/gt64xxx_pci.c static void gt64120_isd_mapping(GT64120State *s) · line 319

Quoted 10 lines
hw/mips/gt64xxx_pci.c +10 -0
+
+    if (size == 1) {
+        return buf[0];
+    } else if (size == 2) {
+        return lduw_le_p(buf);
+    } else if (size == 4) {
+        return ldl_le_p(buf);
+    } else {
+        g_assert_not_reached();
+    }

The device is configured is little endian, and then the little endian value converted into native endianness. Wouldn't it be simple to declare it as DEVICE_NATIVE_ENDIAN?

Aurelien

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#2 lore raw

Hi Aurélien,

Quoted 37 lines · Aurelien Jarno
Quoted 20 lines · Hervé Poussineau

Incidentally, this fixes YAMON on big endian guest.

---
 hw/mips/gt64xxx_pci.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)
hw/mips/gt64xxx_pci.c +1 -0
index 3f4523d..c76ee88 100644
@@ -177,6 +177,7 @@
 
 /* PCI Internal */
 #define GT_PCI0_CMD    		(0xc00 >> 2)
+#define   GT_CMD_MWORDSWAP      (1 << 10)
 #define GT_PCI0_TOR    		(0xc04 >> 2)
 #define GT_PCI0_BS_SCS10    	(0xc08 >> 2)
 #define GT_PCI0_BS_SCS32    	(0xc0c >> 2)

hw/mips/gt64xxx_pci.c static void gt64120_isd_mapping(GT64120State *s) · line 304

Quoted 10 lines
hw/mips/gt64xxx_pci.c +7 -0
@@ -294,6 +295,62 @@ static void gt64120_isd_mapping(GT64120State *s)
     memory_region_add_subregion(get_system_memory(), s->ISD_start, &s->ISD_mem);
 }
 
+static uint64_t gt64120_pci_io_read(void *opaque, hwaddr addr,
+                                    unsigned int size)
+{
+    GT64120State *s = opaque;
+    uint8_t buf[4];
+
+    if (s->regs[GT_PCI0_CMD] & GT_CMD_MWORDSWAP) {

First of all, it should be noted that this bit doesn't control byte swapping, but swaps the 2 4-byte words in a 8-byte word.

hw/mips/gt64xxx_pci.c static void gt64120_isd_mapping(GT64120State *s) · line 305

Quoted 1 line
hw/mips/gt64xxx_pci.c +1 -0
+        addr = (addr & ~3) + 4 - size - (addr & 3);
Quoted 8 lines

This looks complicated, and I don't think it is correct. In addition this doesn't behave correctly at the edges of the address space. For example a 2 byte access at address 0x3 would access address 0xffffffffffffffff.

For sizes <= 4, swapping the 2 words should be done with addr ^= 4. Maybe you should also check for MBYTESWAP which also swaps the bytes within a 8-byte word.

The real word problem (ie the one from Yamon) is:
In LE Yamon, there is a read a 0x4d1 (len = 1). MWORDSWAP and MBYTESWAP are disabled In BE Yamon, the same read is at address 0x4d2. MWORDSWAP is enabled while MBYTESWAP is disabled.

MWORDSWAP documentation is:
"The GT-64120 PCI master swaps the words of the incoming and outgoing PCI data (swap the 2 words of a long word)"

Do we have to ignore it, as QEMU only handles 4-bytes accesses?

Then, how to change this address 0x4d2 to 0x4d1, address where is located the i8259 ELCR register? Next accesses are for the RTC, at address 0x72 in BE and address 0x71 in LE.
I think I'm missing something.

Quoted 11 lines
Quoted 11 lines
hw/mips/gt64xxx_pci.c +10 -0
+    }
+
+    address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED,
+                       buf, size);
+
+    if (size == 1) {
+        return buf[0];
+    } else if (size == 2) {
+        return lduw_le_p(buf);
+    } else if (size == 4) {
Quoted 8 lines

hw/mips/gt64xxx_pci.c static void gt64120_isd_mapping(GT64120State *s) · line 319

Quoted 4 lines
hw/mips/gt64xxx_pci.c +4 -0
+        return ldl_le_p(buf);
+    } else {
+        g_assert_not_reached();
+    }

The device is configured is little endian, and then the little endian value converted into native endianness. Wouldn't it be simple to declare it as DEVICE_NATIVE_ENDIAN?

OK for this part, DEVICE_NATIVE_ENDIAN is indeed better and simplifies code.

Hervé

#3 lore raw
Quoted 49 lines · Hervé Poussineau

Hi Aurélien,

Quoted 37 lines · Aurelien Jarno
Quoted 20 lines · Hervé Poussineau

Incidentally, this fixes YAMON on big endian guest.

---
 hw/mips/gt64xxx_pci.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)
hw/mips/gt64xxx_pci.c +1 -0
index 3f4523d..c76ee88 100644
@@ -177,6 +177,7 @@
 
 /* PCI Internal */
 #define GT_PCI0_CMD    		(0xc00 >> 2)
+#define   GT_CMD_MWORDSWAP      (1 << 10)
 #define GT_PCI0_TOR    		(0xc04 >> 2)
 #define GT_PCI0_BS_SCS10    	(0xc08 >> 2)
 #define GT_PCI0_BS_SCS32    	(0xc0c >> 2)

hw/mips/gt64xxx_pci.c static void gt64120_isd_mapping(GT64120State *s) · line 304

Quoted 10 lines
hw/mips/gt64xxx_pci.c +7 -0
@@ -294,6 +295,62 @@ static void gt64120_isd_mapping(GT64120State *s)
     memory_region_add_subregion(get_system_memory(), s->ISD_start, &s->ISD_mem);
 }
 
+static uint64_t gt64120_pci_io_read(void *opaque, hwaddr addr,
+                                    unsigned int size)
+{
+    GT64120State *s = opaque;
+    uint8_t buf[4];
+
+    if (s->regs[GT_PCI0_CMD] & GT_CMD_MWORDSWAP) {

First of all, it should be noted that this bit doesn't control byte swapping, but swaps the 2 4-byte words in a 8-byte word.

hw/mips/gt64xxx_pci.c static void gt64120_isd_mapping(GT64120State *s) · line 305

Quoted 1 line
hw/mips/gt64xxx_pci.c +1 -0
+        addr = (addr & ~3) + 4 - size - (addr & 3);
Quoted 8 lines

This looks complicated, and I don't think it is correct. In addition this doesn't behave correctly at the edges of the address space. For example a 2 byte access at address 0x3 would access address 0xffffffffffffffff.

For sizes <= 4, swapping the 2 words should be done with addr ^= 4. Maybe you should also check for MBYTESWAP which also swaps the bytes within a 8-byte word.

Quoted 8 lines

The real word problem (ie the one from Yamon) is:
In LE Yamon, there is a read a 0x4d1 (len = 1). MWORDSWAP and MBYTESWAP are disabled In BE Yamon, the same read is at address 0x4d2. MWORDSWAP is enabled while MBYTESWAP is disabled.

MWORDSWAP documentation is:
"The GT-64120 PCI master swaps the words of the incoming and outgoing PCI data (swap the 2 words of a long word)"

Do we have to ignore it, as QEMU only handles 4-bytes accesses?

I think indeed that it should be ignored.

Quoted 3 lines

Then, how to change this address 0x4d2 to 0x4d1, address where is located the i8259 ELCR register? Next accesses are for the RTC, at address 0x72 in BE and address 0x71 in LE.
I think I'm missing something.

If you talk about byte accesses, it really looks like a simple byteswap. 0x4d2 ^ 3 = 0x4d1 and 0x72 ^ 3 = 0x71. This could be there is a byteswap that is missing somewhere. It would be interesting to see how 16-bit and 32-bit accesses are changed between big and little endian.

Aurelien

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net