From 340d96e7ce6e1c2e6d75504e9b4d8153896d5b42 Mon Sep 17 00:00:00 2001 From: Paul Brook Date: Mon, 18 May 2009 16:45:48 +0100 Subject: [PATCH] Syborg virtio bindings. Implement Syborg device bindings for virtio-net. Signed-off-by: Paul Brook --- Makefile.target | 1 + hw/syborg.c | 14 +++ hw/syborg_virtio.c | 278 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 293 insertions(+) create mode 100644 hw/syborg_virtio.c diff --git a/Makefile.target b/Makefile.target index fdf671847d..bbdef88310 100644 --- a/Makefile.target +++ b/Makefile.target @@ -658,6 +658,7 @@ OBJS+= musicpal.o pflash_cfi02.o OBJS+= framebuffer.o OBJS+= syborg.o syborg_fb.o syborg_interrupt.o syborg_keyboard.o OBJS+= syborg_serial.o syborg_timer.o syborg_pointer.o syborg_rtc.o +OBJS+= syborg_virtio.o CPPFLAGS += -DHAS_AUDIO endif ifeq ($(TARGET_BASE_ARCH), sh4) diff --git a/hw/syborg.c b/hw/syborg.c index e54fc95274..99f9dbbdba 100644 --- a/hw/syborg.c +++ b/hw/syborg.c @@ -26,6 +26,7 @@ #include "boards.h" #include "arm-misc.h" #include "sysemu.h" +#include "net.h" static struct arm_boot_info syborg_binfo; @@ -76,6 +77,19 @@ static void syborg_init(ram_addr_t ram_size, sysbus_create_simple("syborg,serial", 0xC0008000, pic[7]); sysbus_create_simple("syborg,serial", 0xC0009000, pic[8]); + if (nd_table[0].vlan) { + DeviceState *dev; + SysBusDevice *s; + + qemu_check_nic_model(&nd_table[0], "virtio"); + dev = qdev_create(NULL, "syborg,virtio-net"); + qdev_set_netdev(dev, &nd_table[0]); + qdev_init(dev); + s = sysbus_from_qdev(dev); + sysbus_mmio_map(s, 0, 0xc000c000); + sysbus_connect_irq(s, 0, pic[9]); + } + syborg_binfo.ram_size = ram_size; syborg_binfo.kernel_filename = kernel_filename; syborg_binfo.kernel_cmdline = kernel_cmdline; diff --git a/hw/syborg_virtio.c b/hw/syborg_virtio.c new file mode 100644 index 0000000000..37c219cda3 --- /dev/null +++ b/hw/syborg_virtio.c @@ -0,0 +1,278 @@ +/* + * Virtio Syborg bindings + * + * Copyright (c) 2009 CodeSourcery + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "syborg.h" +#include "sysbus.h" +#include "virtio.h" +#include "sysemu.h" + +//#define DEBUG_SYBORG_VIRTIO + +#ifdef DEBUG_SYBORG_VIRTIO +#define DPRINTF(fmt, ...) \ +do { printf("syborg_virtio: " fmt , ## __VA_ARGS__); } while (0) +#define BADF(fmt, ...) \ +do { fprintf(stderr, "syborg_virtio: error: " fmt , ## __VA_ARGS__); \ + exit(1);} while (0) +#else +#define DPRINTF(fmt, ...) do {} while(0) +#define BADF(fmt, ...) \ +do { fprintf(stderr, "syborg_virtio: error: " fmt , ## __VA_ARGS__);} while (0) +#endif + +enum { + SYBORG_VIRTIO_ID = 0, + SYBORG_VIRTIO_DEVTYPE = 1, + SYBORG_VIRTIO_HOST_FEATURES = 2, + SYBORG_VIRTIO_GUEST_FEATURES = 3, + SYBORG_VIRTIO_QUEUE_BASE = 4, + SYBORG_VIRTIO_QUEUE_NUM = 5, + SYBORG_VIRTIO_QUEUE_SEL = 6, + SYBORG_VIRTIO_QUEUE_NOTIFY = 7, + SYBORG_VIRTIO_STATUS = 8, + SYBORG_VIRTIO_INT_ENABLE = 9, + SYBORG_VIRTIO_INT_STATUS = 10 +}; + +#define SYBORG_VIRTIO_CONFIG 0x100 + +/* Device independent interface. */ + +typedef struct { + SysBusDevice busdev; + VirtIODevice *vdev; + qemu_irq irq; + uint32_t int_enable; + uint32_t id; +} SyborgVirtIOProxy; + +static uint32_t syborg_virtio_readl(void *opaque, target_phys_addr_t offset) +{ + SyborgVirtIOProxy *s = opaque; + VirtIODevice *vdev = s->vdev; + uint32_t ret; + + DPRINTF("readl 0x%x\n", (int)offset); + if (offset >= SYBORG_VIRTIO_CONFIG) { + return virtio_config_readl(vdev, offset - SYBORG_VIRTIO_CONFIG); + } + switch(offset >> 2) { + case SYBORG_VIRTIO_ID: + ret = SYBORG_ID_VIRTIO; + break; + case SYBORG_VIRTIO_DEVTYPE: + ret = s->id; + break; + case SYBORG_VIRTIO_HOST_FEATURES: + ret = vdev->get_features(vdev); + ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY); + break; + case SYBORG_VIRTIO_GUEST_FEATURES: + ret = vdev->features; + break; + case SYBORG_VIRTIO_QUEUE_BASE: + ret = virtio_queue_get_addr(vdev, vdev->queue_sel); + break; + case SYBORG_VIRTIO_QUEUE_NUM: + ret = virtio_queue_get_num(vdev, vdev->queue_sel); + break; + case SYBORG_VIRTIO_QUEUE_SEL: + ret = vdev->queue_sel; + break; + case SYBORG_VIRTIO_STATUS: + ret = vdev->status; + break; + case SYBORG_VIRTIO_INT_ENABLE: + ret = s->int_enable; + break; + case SYBORG_VIRTIO_INT_STATUS: + ret = vdev->isr; + break; + default: + BADF("Bad read offset 0x%x\n", (int)offset); + return 0; + } + return ret; +} + +static void syborg_virtio_writel(void *opaque, target_phys_addr_t offset, + uint32_t value) +{ + SyborgVirtIOProxy *s = opaque; + VirtIODevice *vdev = s->vdev; + + DPRINTF("writel 0x%x = 0x%x\n", (int)offset, value); + if (offset >= SYBORG_VIRTIO_CONFIG) { + return virtio_config_writel(vdev, offset - SYBORG_VIRTIO_CONFIG, + value); + } + switch (offset >> 2) { + case SYBORG_VIRTIO_GUEST_FEATURES: + if (vdev->set_features) + vdev->set_features(vdev, value); + vdev->features = value; + break; + case SYBORG_VIRTIO_QUEUE_BASE: + virtio_queue_set_addr(vdev, vdev->queue_sel, value); + break; + case SYBORG_VIRTIO_QUEUE_SEL: + if (value < VIRTIO_PCI_QUEUE_MAX) + vdev->queue_sel = value; + break; + case SYBORG_VIRTIO_QUEUE_NOTIFY: + virtio_queue_notify(vdev, value); + break; + case SYBORG_VIRTIO_STATUS: + vdev->status = value & 0xFF; + if (vdev->status == 0) + virtio_reset(vdev); + break; + case SYBORG_VIRTIO_INT_ENABLE: + s->int_enable = value; + virtio_update_irq(vdev); + break; + case SYBORG_VIRTIO_INT_STATUS: + vdev->isr &= ~value; + virtio_update_irq(vdev); + break; + default: + BADF("Bad write offset 0x%x\n", (int)offset); + break; + } +} + +static uint32_t syborg_virtio_readw(void *opaque, target_phys_addr_t offset) +{ + SyborgVirtIOProxy *s = opaque; + VirtIODevice *vdev = s->vdev; + + DPRINTF("readw 0x%x\n", (int)offset); + if (offset >= SYBORG_VIRTIO_CONFIG) { + return virtio_config_readw(vdev, offset - SYBORG_VIRTIO_CONFIG); + } + BADF("Bad halfword read offset 0x%x\n", (int)offset); + return -1; +} + +static void syborg_virtio_writew(void *opaque, target_phys_addr_t offset, + uint32_t value) +{ + SyborgVirtIOProxy *s = opaque; + VirtIODevice *vdev = s->vdev; + + DPRINTF("writew 0x%x = 0x%x\n", (int)offset, value); + if (offset >= SYBORG_VIRTIO_CONFIG) { + return virtio_config_writew(vdev, offset - SYBORG_VIRTIO_CONFIG, + value); + } + BADF("Bad halfword write offset 0x%x\n", (int)offset); +} + +static uint32_t syborg_virtio_readb(void *opaque, target_phys_addr_t offset) +{ + SyborgVirtIOProxy *s = opaque; + VirtIODevice *vdev = s->vdev; + + DPRINTF("readb 0x%x\n", (int)offset); + if (offset >= SYBORG_VIRTIO_CONFIG) { + return virtio_config_readb(vdev, offset - SYBORG_VIRTIO_CONFIG); + } + BADF("Bad byte read offset 0x%x\n", (int)offset); + return -1; +} + +static void syborg_virtio_writeb(void *opaque, target_phys_addr_t offset, + uint32_t value) +{ + SyborgVirtIOProxy *s = opaque; + VirtIODevice *vdev = s->vdev; + + DPRINTF("writeb 0x%x = 0x%x\n", (int)offset, value); + if (offset >= SYBORG_VIRTIO_CONFIG) { + return virtio_config_writeb(vdev, offset - SYBORG_VIRTIO_CONFIG, + value); + } + BADF("Bad byte write offset 0x%x\n", (int)offset); +} + +static CPUReadMemoryFunc *syborg_virtio_readfn[] = { + syborg_virtio_readb, + syborg_virtio_readw, + syborg_virtio_readl +}; + +static CPUWriteMemoryFunc *syborg_virtio_writefn[] = { + syborg_virtio_writeb, + syborg_virtio_writew, + syborg_virtio_writel +}; + +static void syborg_virtio_update_irq(void *opaque) +{ + SyborgVirtIOProxy *proxy = opaque; + int level; + + level = proxy->int_enable & proxy->vdev->isr; + DPRINTF("IRQ %d\n", level); + qemu_set_irq(proxy->irq, level != 0); +} + +static VirtIOBindings syborg_virtio_bindings = { + .update_irq = syborg_virtio_update_irq +}; + +static void syborg_virtio_init(SyborgVirtIOProxy *proxy, VirtIODevice *vdev) +{ + int iomemtype; + + proxy->vdev = vdev; + + sysbus_init_irq(&proxy->busdev, &proxy->irq); + iomemtype = cpu_register_io_memory(0, syborg_virtio_readfn, + syborg_virtio_writefn, proxy); + sysbus_init_mmio(&proxy->busdev, 0x1000, iomemtype); + + proxy->id = ((uint32_t)0x1af4 << 16) | vdev->device_id; + + virtio_bind_device(vdev, &syborg_virtio_bindings, proxy); +} + +/* Device specific bindings. */ + +static void syborg_virtio_net_init(SysBusDevice *dev) +{ + VirtIODevice *vdev; + SyborgVirtIOProxy *proxy = FROM_SYSBUS(SyborgVirtIOProxy, dev); + + vdev = virtio_net_init(&dev->qdev); + syborg_virtio_init(proxy, vdev); +} + +static void syborg_virtio_register_devices(void) +{ + sysbus_register_dev("syborg,virtio-net", sizeof(SyborgVirtIOProxy), + syborg_virtio_net_init); +} + +device_init(syborg_virtio_register_devices)