Patchwork [RFC,v3,31/56] ac97: convert to memory API

login
register
mail settings
Submitter Avi Kivity
Date July 10, 2011, 6:14 p.m.
Message ID <1310321709-30770-32-git-send-email-avi@redhat.com>
Download mbox | patch
Permalink /patch/104070/
State New
Headers show

Comments

Avi Kivity - July 10, 2011, 6:14 p.m.
fixes BAR sizing as well.

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 hw/ac97.c |  126 ++++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 88 insertions(+), 38 deletions(-)
malc - July 10, 2011, 8:33 p.m.
On Sun, 10 Jul 2011, Avi Kivity wrote:

> fixes BAR sizing as well.

I find this patch disgusting, the read and write handlers in particular.

[..snip..]
Anthony Liguori - July 11, 2011, 1:42 a.m.
On 07/10/2011 03:33 PM, malc wrote:
> On Sun, 10 Jul 2011, Avi Kivity wrote:
>
>> fixes BAR sizing as well.
>
> I find this patch disgusting, the read and write handlers in particular.

Shouldn't it be possible to do something like:

typedef struct OldMemoryRegionOps {
     MemoryRegionOps parent_ops;
     CPUReadMemoryFunc *readfn[3];
     CPUWriteMemoryFunc *writefn[3];
     void *opaque;
} OldMemoryRegionOps;

That should allow old-style implementations to be converted without 
introducing trampoline functions everywhere.

Regards,

Anthony Liguori

>
> [..snip..]
>
Avi Kivity - July 11, 2011, 6:49 a.m.
> 
> Shouldn't it be possible to do something like:
> 
> typedef struct OldMemoryRegionOps {
> MemoryRegionOps parent_ops;
> CPUReadMemoryFunc *readfn[3];
> CPUWriteMemoryFunc *writefn[3];
> void *opaque;
> } OldMemoryRegionOps;
> 
> That should allow old-style implementations to be converted without
> introducing trampoline functions everywhere.
> 

I should, I'll give it a go.

Patch

diff --git a/hw/ac97.c b/hw/ac97.c
index 0b59896..72a0667 100644
--- a/hw/ac97.c
+++ b/hw/ac97.c
@@ -160,8 +160,9 @@  typedef struct AC97LinkState {
     SWVoiceIn *voice_mc;
     int invalid_freq[3];
     uint8_t silence[128];
-    uint32_t base[2];
     int bup_flag;
+    MemoryRegion io_nam;
+    MemoryRegion io_nabm;
 } AC97LinkState;
 
 enum {
@@ -583,7 +584,7 @@  static uint32_t nam_readw (void *opaque, uint32_t addr)
 {
     AC97LinkState *s = opaque;
     uint32_t val = ~0U;
-    uint32_t index = addr - s->base[0];
+    uint32_t index = addr;
     s->cas = 0;
     val = mixer_load (s, index);
     return val;
@@ -611,7 +612,7 @@  static void nam_writeb (void *opaque, uint32_t addr, uint32_t val)
 static void nam_writew (void *opaque, uint32_t addr, uint32_t val)
 {
     AC97LinkState *s = opaque;
-    uint32_t index = addr - s->base[0];
+    uint32_t index = addr;
     s->cas = 0;
     switch (index) {
     case AC97_Reset:
@@ -706,6 +707,37 @@  static void nam_writel (void *opaque, uint32_t addr, uint32_t val)
     s->cas = 0;
 }
 
+static uint64_t nam_read(void *opaque, target_phys_addr_t addr, unsigned size)
+{
+    AC97LinkState *s = opaque;
+
+    switch (size) {
+    case 1: return nam_readb(s, addr);
+    case 2: return nam_readw(s, addr);
+    case 4: return nam_readl(s, addr);
+    default: abort();
+    }
+}
+
+static void nam_write(void *opaque, target_phys_addr_t addr,
+                      uint64_t data, unsigned size)
+{
+    AC97LinkState *s = opaque;
+
+    switch (size) {
+    case 1: return nam_writeb(s, addr, data);
+    case 2: return nam_writew(s, addr, data);
+    case 4: return nam_writel(s, addr, data);
+    default: abort();
+    }
+}
+
+static MemoryRegionOps ac97_io_nam_ops = {
+    .read = nam_read,
+    .write = nam_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+};
+
 /**
  * Native audio bus master
  * I/O Reads
@@ -714,7 +746,7 @@  static uint32_t nabm_readb (void *opaque, uint32_t addr)
 {
     AC97LinkState *s = opaque;
     AC97BusMasterRegs *r = NULL;
-    uint32_t index = addr - s->base[1];
+    uint32_t index = addr;
     uint32_t val = ~0U;
 
     switch (index) {
@@ -769,7 +801,7 @@  static uint32_t nabm_readw (void *opaque, uint32_t addr)
 {
     AC97LinkState *s = opaque;
     AC97BusMasterRegs *r = NULL;
-    uint32_t index = addr - s->base[1];
+    uint32_t index = addr;
     uint32_t val = ~0U;
 
     switch (index) {
@@ -798,7 +830,7 @@  static uint32_t nabm_readl (void *opaque, uint32_t addr)
 {
     AC97LinkState *s = opaque;
     AC97BusMasterRegs *r = NULL;
-    uint32_t index = addr - s->base[1];
+    uint32_t index = addr;
     uint32_t val = ~0U;
 
     switch (index) {
@@ -848,7 +880,7 @@  static void nabm_writeb (void *opaque, uint32_t addr, uint32_t val)
 {
     AC97LinkState *s = opaque;
     AC97BusMasterRegs *r = NULL;
-    uint32_t index = addr - s->base[1];
+    uint32_t index = addr;
     switch (index) {
     case PI_LVI:
     case PO_LVI:
@@ -904,7 +936,7 @@  static void nabm_writew (void *opaque, uint32_t addr, uint32_t val)
 {
     AC97LinkState *s = opaque;
     AC97BusMasterRegs *r = NULL;
-    uint32_t index = addr - s->base[1];
+    uint32_t index = addr;
     switch (index) {
     case PI_SR:
     case PO_SR:
@@ -924,7 +956,7 @@  static void nabm_writel (void *opaque, uint32_t addr, uint32_t val)
 {
     AC97LinkState *s = opaque;
     AC97BusMasterRegs *r = NULL;
-    uint32_t index = addr - s->base[1];
+    uint32_t index = addr;
     switch (index) {
     case PI_BDBAR:
     case PO_BDBAR:
@@ -954,6 +986,38 @@  static void nabm_writel (void *opaque, uint32_t addr, uint32_t val)
     }
 }
 
+static uint64_t nabm_read(void *opaque, target_phys_addr_t addr,
+                         unsigned size)
+{
+    AC97LinkState *s = opaque;
+
+    switch (size) {
+    case 1: return nabm_readb(s, addr);
+    case 2: return nabm_readw(s, addr);
+    case 4: return nabm_readl(s, addr);
+    default: abort();
+    }
+}
+
+static void nabm_write(void *opaque, target_phys_addr_t addr,
+                       uint64_t data, unsigned size)
+{
+    AC97LinkState *s = opaque;
+
+    switch (size) {
+    case 1: return nabm_writeb(s, addr, data);
+    case 2: return nabm_writew(s, addr, data);
+    case 4: return nabm_writel(s, addr, data);
+    default: abort();
+    }
+}
+
+static MemoryRegionOps ac97_io_nabm_ops = {
+    .read = nabm_read,
+    .write = nabm_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+};
+
 static int write_audio (AC97LinkState *s, AC97BusMasterRegs *r,
                         int max, int *stop)
 {
@@ -1230,32 +1294,6 @@  static const VMStateDescription vmstate_ac97 = {
     }
 };
 
-static void ac97_map (PCIDevice *pci_dev, int region_num,
-                      pcibus_t addr, pcibus_t size, int type)
-{
-    AC97LinkState *s = DO_UPCAST (AC97LinkState, dev, pci_dev);
-    PCIDevice *d = &s->dev;
-
-    if (!region_num) {
-        s->base[0] = addr;
-        register_ioport_read (addr, 256 * 1, 1, nam_readb, d);
-        register_ioport_read (addr, 256 * 2, 2, nam_readw, d);
-        register_ioport_read (addr, 256 * 4, 4, nam_readl, d);
-        register_ioport_write (addr, 256 * 1, 1, nam_writeb, d);
-        register_ioport_write (addr, 256 * 2, 2, nam_writew, d);
-        register_ioport_write (addr, 256 * 4, 4, nam_writel, d);
-    }
-    else {
-        s->base[1] = addr;
-        register_ioport_read (addr, 64 * 1, 1, nabm_readb, d);
-        register_ioport_read (addr, 64 * 2, 2, nabm_readw, d);
-        register_ioport_read (addr, 64 * 4, 4, nabm_readl, d);
-        register_ioport_write (addr, 64 * 1, 1, nabm_writeb, d);
-        register_ioport_write (addr, 64 * 2, 2, nabm_writew, d);
-        register_ioport_write (addr, 64 * 4, 4, nabm_writel, d);
-    }
-}
-
 static void ac97_on_reset (void *opaque)
 {
     AC97LinkState *s = opaque;
@@ -1311,15 +1349,26 @@  static int ac97_initfn (PCIDevice *dev)
     /* TODO: RST# value should be 0. */
     c[PCI_INTERRUPT_PIN] = 0x01;      /* intr_pn interrupt pin ro */
 
-    pci_register_bar (&s->dev, 0, 256 * 4, PCI_BASE_ADDRESS_SPACE_IO,
-                      ac97_map);
-    pci_register_bar (&s->dev, 1, 64 * 4, PCI_BASE_ADDRESS_SPACE_IO, ac97_map);
+    memory_region_init_io(&s->io_nam, &ac97_io_nam_ops, s, "ac97-nam", 1024);
+    memory_region_init_io(&s->io_nabm, &ac97_io_nabm_ops, s, "ac97-nabm", 256);
+
+    pci_register_bar_region(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_nam);
+    pci_register_bar_region(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io_nabm);
     qemu_register_reset (ac97_on_reset, s);
     AUD_register_card ("ac97", &s->card);
     ac97_on_reset (s);
     return 0;
 }
 
+static int ac97_exitfn(PCIDevice *dev)
+{
+    AC97LinkState *s = DO_UPCAST(AC97LinkState, dev, dev);
+
+    memory_region_destroy(&s->io_nam);
+    memory_region_destroy(&s->io_nabm);
+    return 0;
+}
+
 int ac97_init (PCIBus *bus)
 {
     pci_create_simple (bus, -1, "AC97");
@@ -1332,6 +1381,7 @@  static PCIDeviceInfo ac97_info = {
     .qdev.size    = sizeof (AC97LinkState),
     .qdev.vmsd    = &vmstate_ac97,
     .init         = ac97_initfn,
+    .exit         = ac97_exitfn,
     .vendor_id    = PCI_VENDOR_ID_INTEL,
     .device_id    = PCI_DEVICE_ID_INTEL_82801AA_5,
     .revision     = 0x01,