qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Graf <agraf@suse.de>
To: QEMU-devel Developers <qemu-devel@nongnu.org>
Cc: Blue Swirl <blauwirbel@gmail.com>, Paul Brook <paul@codesourcery.com>
Subject: [Qemu-devel] [PATCH 01/15] exec: introduce endianness swapped mmio
Date: Tue, 30 Nov 2010 15:35:47 +0100	[thread overview]
Message-ID: <1291127761-16501-2-git-send-email-agraf@suse.de> (raw)
In-Reply-To: <1291127761-16501-1-git-send-email-agraf@suse.de>

The way we're currently modeling mmio is too simplified. We assume that
every device has the same endianness as the target CPU. In reality,
most devices are little endian (all PCI and ISA ones I'm aware of). Some
are big endian (special system devices) and a very little fraction is
target native endian (fw_cfg).

So instead of assuming every device to be native endianness, let's move
to a model where the device tells us which endianness it's in.

That way we can compile the devices only once and get rid of all the ugly
swap will be done by the underlying layer.

For the same of readability, this patch only introduces the helper framework
but doesn't allow the registering code to set its endianness yet.

Signed-off-by: Alexander Graf <agraf@suse.de>

---

v0 -> v1:

  - don't restrict to big endian targets
  -  move constants to enum
---
 cpu-common.h |    8 +++-
 exec.c       |  123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 128 insertions(+), 3 deletions(-)

diff --git a/cpu-common.h b/cpu-common.h
index a543b5d..8eeb0f6 100644
--- a/cpu-common.h
+++ b/cpu-common.h
@@ -20,6 +20,12 @@
 
 #if !defined(CONFIG_USER_ONLY)
 
+enum device_endian {
+    DEVICE_NATIVE_ENDIAN,
+    DEVICE_BIG_ENDIAN,
+    DEVICE_LITTLE_ENDIAN,
+};
+
 /* address in the RAM (different from a physical address) */
 typedef unsigned long ram_addr_t;
 
@@ -52,7 +58,7 @@ ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr);
 
 int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
                            CPUWriteMemoryFunc * const *mem_write,
-                           void *opaque);
+                           void *opaque, enum device_endian endian);
 void cpu_unregister_io_memory(int table_address);
 
 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
diff --git a/exec.c b/exec.c
index db9ff55..60db49f 100644
--- a/exec.c
+++ b/exec.c
@@ -3336,6 +3336,106 @@ static int get_free_io_mem_idx(void)
     return -1;
 }
 
+/*
+ * Usually, devices operate in little endian mode. There are devices out
+ * there that operate in big endian too. Each device gets byte swapped
+ * mmio if plugged onto a CPU that does the other endianness.
+ *
+ * CPU          Device           swap?
+ *
+ * little       little           no
+ * little       big              yes
+ * big          little           yes
+ * big          big              no
+ */
+
+typedef struct SwapEndianContainer {
+    CPUReadMemoryFunc *read[3];
+    CPUWriteMemoryFunc *write[3];
+    void *opaque;
+} SwapEndianContainer;
+
+static uint32_t swapendian_mem_readb (void *opaque, target_phys_addr_t addr)
+{
+    uint32_t val;
+    SwapEndianContainer *c = opaque;
+    val = c->read[0](c->opaque, addr);
+    return val;
+}
+
+static uint32_t swapendian_mem_readw(void *opaque, target_phys_addr_t addr)
+{
+    uint32_t val;
+    SwapEndianContainer *c = opaque;
+    val = bswap16(c->read[1](c->opaque, addr));
+    return val;
+}
+
+static uint32_t swapendian_mem_readl(void *opaque, target_phys_addr_t addr)
+{
+    uint32_t val;
+    SwapEndianContainer *c = opaque;
+    val = bswap32(c->read[2](c->opaque, addr));
+    return val;
+}
+
+static CPUReadMemoryFunc * const swapendian_readfn[3]={
+    swapendian_mem_readb,
+    swapendian_mem_readw,
+    swapendian_mem_readl
+};
+
+static void swapendian_mem_writeb(void *opaque, target_phys_addr_t addr,
+                                  uint32_t val)
+{
+    SwapEndianContainer *c = opaque;
+    c->write[0](c->opaque, addr, val);
+}
+
+static void swapendian_mem_writew(void *opaque, target_phys_addr_t addr,
+                                  uint32_t val)
+{
+    SwapEndianContainer *c = opaque;
+    c->write[1](c->opaque, addr, bswap16(val));
+}
+
+static void swapendian_mem_writel(void *opaque, target_phys_addr_t addr,
+                                  uint32_t val)
+{
+    SwapEndianContainer *c = opaque;
+    c->write[2](c->opaque, addr, bswap32(val));
+}
+
+static CPUWriteMemoryFunc * const swapendian_writefn[3]={
+    swapendian_mem_writeb,
+    swapendian_mem_writew,
+    swapendian_mem_writel
+};
+
+static void swapendian_init(int io_index)
+{
+    SwapEndianContainer *c = qemu_malloc(sizeof(SwapEndianContainer));
+    int i;
+
+    /* Swap mmio for big endian targets */
+    c->opaque = io_mem_opaque[io_index];
+    for (i = 0; i < 3; i++) {
+        c->read[i] = io_mem_read[io_index][i];
+        c->write[i] = io_mem_write[io_index][i];
+
+        io_mem_read[io_index][i] = swapendian_readfn[i];
+        io_mem_write[io_index][i] = swapendian_writefn[i];
+    }
+    io_mem_opaque[io_index] = c;
+}
+
+static void swapendian_del(int io_index)
+{
+    if (io_mem_read[io_index][0] == swapendian_readfn[0]) {
+        qemu_free(io_mem_opaque[io_index]);
+    }
+}
+
 /* mem_read and mem_write are arrays of functions containing the
    function to access byte (index 0), word (index 1) and dword (index
    2). Functions can be omitted with a NULL function pointer.
@@ -3346,9 +3446,10 @@ static int get_free_io_mem_idx(void)
 static int cpu_register_io_memory_fixed(int io_index,
                                         CPUReadMemoryFunc * const *mem_read,
                                         CPUWriteMemoryFunc * const *mem_write,
-                                        void *opaque)
+                                        void *opaque, enum device_endian endian)
 {
     int i;
+    int endian = DEVICE_NATIVE_ENDIAN;
 
     if (io_index <= 0) {
         io_index = get_free_io_mem_idx();
@@ -3370,12 +3471,28 @@ static int cpu_register_io_memory_fixed(int io_index,
     }
     io_mem_opaque[io_index] = opaque;
 
+    switch (endian) {
+    case DEVICE_BIG_ENDIAN:
+#ifndef TARGET_WORDS_BIGENDIAN
+        swapendian_init(io_index);
+#endif
+        break;
+    case DEVICE_LITTLE_ENDIAN:
+#ifdef TARGET_WORDS_BIGENDIAN
+        swapendian_init(io_index);
+#endif
+        break;
+    case DEVICE_NATIVE_ENDIAN:
+    default:
+        break;
+    }
+
     return (io_index << IO_MEM_SHIFT);
 }
 
 int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
                            CPUWriteMemoryFunc * const *mem_write,
-                           void *opaque)
+                           void *opaque, enum device_endian endian)
 {
     return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque);
 }
@@ -3385,6 +3502,8 @@ void cpu_unregister_io_memory(int io_table_address)
     int i;
     int io_index = io_table_address >> IO_MEM_SHIFT;
 
+    swapendian_del(io_index);
+
     for (i=0;i < 3; i++) {
         io_mem_read[io_index][i] = unassigned_mem_read[i];
         io_mem_write[io_index][i] = unassigned_mem_write[i];
-- 
1.6.0.2

  reply	other threads:[~2010-12-01  4:41 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-30 14:35 [Qemu-devel] [PATCH 00/15] [PATCH] MMIO endianness cleanup v1 Alexander Graf
2010-11-30 14:35 ` Alexander Graf [this message]
2010-11-30 14:35 ` [Qemu-devel] [PATCH 02/15] Add endianness as io mem parameter Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 03/15] Make simple io mem handler endian aware Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 04/15] dbdma: Make little endian Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 05/15] pci-host: Delegate bswap to mmio layer Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 06/15] uninorth: Get rid of bswap Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 07/15] e1000: Make little endian Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 08/15] prep: Declare as " Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 09/15] versatile_pci: " Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 10/15] ppc4xx_pci: " Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 11/15] openpic: Replace explicit byte swap with endian hints Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 12/15] rtl8139: Declare as little endian Alexander Graf
2010-11-30 14:35 ` [Qemu-devel] [PATCH 13/15] heathrow_pic: " Alexander Graf
2010-11-30 14:36 ` [Qemu-devel] [PATCH 14/15] isa_mmio: Always use " Alexander Graf
2010-11-30 14:36 ` [Qemu-devel] [PATCH 15/15] usb_ohci: " Alexander Graf
  -- strict thread matches above, loose matches on Subject: below --
2010-12-08 11:05 [Qemu-devel] [PATCH 00/15] MMIO endianness cleanup v2 Alexander Graf
2010-12-08 11:05 ` [Qemu-devel] [PATCH 01/15] exec: introduce endianness swapped mmio Alexander Graf
2010-11-25  7:35 [Qemu-devel] [PATCH 00/15] [RFC] MMIO endianness cleanup Alexander Graf
2010-11-25  7:35 ` [Qemu-devel] [PATCH 01/15] exec: introduce endianness swapped mmio Alexander Graf
2010-11-26 23:00   ` Andreas Färber
2010-11-26 23:06     ` Alexander Graf
2010-11-26 23:18     ` Peter Maydell
2010-11-26 23:47     ` Paul Brook
2010-11-27 10:05       ` Blue Swirl
2010-11-27 10:24         ` Paul Brook
2010-11-28  8:12   ` Gleb Natapov
2010-11-28 11:05     ` Alexander Graf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1291127761-16501-2-git-send-email-agraf@suse.de \
    --to=agraf@suse.de \
    --cc=blauwirbel@gmail.com \
    --cc=paul@codesourcery.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).