2009-05-18 17:45:48 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
2010-01-10 12:52:53 +01:00
|
|
|
#include "virtio-net.h"
|
2009-05-18 17:45:48 +02:00
|
|
|
|
|
|
|
//#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;
|
2009-10-21 15:25:35 +02:00
|
|
|
NICConf nic;
|
2010-01-10 12:52:53 +01:00
|
|
|
uint32_t host_features;
|
2010-09-02 17:00:50 +02:00
|
|
|
virtio_net_conf net;
|
2009-05-18 17:45:48 +02:00
|
|
|
} SyborgVirtIOProxy;
|
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static uint32_t syborg_virtio_readl(void *opaque, target_phys_addr_t offset)
|
2009-05-18 17:45:48 +02:00
|
|
|
{
|
|
|
|
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:
|
2010-01-10 12:52:53 +01:00
|
|
|
ret = s->host_features;
|
2009-05-18 17:45:48 +02:00
|
|
|
break;
|
|
|
|
case SYBORG_VIRTIO_GUEST_FEATURES:
|
2010-01-10 12:52:47 +01:00
|
|
|
ret = vdev->guest_features;
|
2009-05-18 17:45:48 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static void syborg_virtio_writel(void *opaque, target_phys_addr_t offset,
|
2009-05-18 17:45:48 +02:00
|
|
|
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);
|
2010-01-10 12:52:47 +01:00
|
|
|
vdev->guest_features = value;
|
2009-05-18 17:45:48 +02:00
|
|
|
break;
|
|
|
|
case SYBORG_VIRTIO_QUEUE_BASE:
|
2009-06-21 18:50:13 +02:00
|
|
|
if (value == 0)
|
|
|
|
virtio_reset(vdev);
|
|
|
|
else
|
|
|
|
virtio_queue_set_addr(vdev, vdev->queue_sel, value);
|
2009-05-18 17:45:48 +02:00
|
|
|
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:
|
2010-03-17 12:08:05 +01:00
|
|
|
virtio_set_status(vdev, value & 0xFF);
|
2009-05-18 17:45:48 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static uint32_t syborg_virtio_readw(void *opaque, target_phys_addr_t offset)
|
2009-05-18 17:45:48 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static void syborg_virtio_writew(void *opaque, target_phys_addr_t offset,
|
2009-05-18 17:45:48 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static uint32_t syborg_virtio_readb(void *opaque, target_phys_addr_t offset)
|
2009-05-18 17:45:48 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static void syborg_virtio_writeb(void *opaque, target_phys_addr_t offset,
|
2009-05-18 17:45:48 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-08-25 20:29:31 +02:00
|
|
|
static CPUReadMemoryFunc * const syborg_virtio_readfn[] = {
|
2009-05-18 17:45:48 +02:00
|
|
|
syborg_virtio_readb,
|
|
|
|
syborg_virtio_readw,
|
|
|
|
syborg_virtio_readl
|
|
|
|
};
|
|
|
|
|
2009-08-25 20:29:31 +02:00
|
|
|
static CPUWriteMemoryFunc * const syborg_virtio_writefn[] = {
|
2009-05-18 17:45:48 +02:00
|
|
|
syborg_virtio_writeb,
|
|
|
|
syborg_virtio_writew,
|
|
|
|
syborg_virtio_writel
|
|
|
|
};
|
|
|
|
|
2009-06-21 18:50:13 +02:00
|
|
|
static void syborg_virtio_update_irq(void *opaque, uint16_t vector)
|
2009-05-18 17:45:48 +02:00
|
|
|
{
|
|
|
|
SyborgVirtIOProxy *proxy = opaque;
|
|
|
|
int level;
|
|
|
|
|
|
|
|
level = proxy->int_enable & proxy->vdev->isr;
|
|
|
|
DPRINTF("IRQ %d\n", level);
|
|
|
|
qemu_set_irq(proxy->irq, level != 0);
|
|
|
|
}
|
|
|
|
|
2009-12-08 19:07:48 +01:00
|
|
|
static unsigned syborg_virtio_get_features(void *opaque)
|
|
|
|
{
|
2010-01-10 12:52:53 +01:00
|
|
|
SyborgVirtIOProxy *proxy = opaque;
|
|
|
|
return proxy->host_features;
|
2009-12-08 19:07:48 +01:00
|
|
|
}
|
|
|
|
|
2009-05-18 17:45:48 +02:00
|
|
|
static VirtIOBindings syborg_virtio_bindings = {
|
2009-12-08 19:07:48 +01:00
|
|
|
.notify = syborg_virtio_update_irq,
|
|
|
|
.get_features = syborg_virtio_get_features,
|
2009-05-18 17:45:48 +02:00
|
|
|
};
|
|
|
|
|
2009-08-14 10:36:05 +02:00
|
|
|
static int syborg_virtio_init(SyborgVirtIOProxy *proxy, VirtIODevice *vdev)
|
2009-05-18 17:45:48 +02:00
|
|
|
{
|
|
|
|
int iomemtype;
|
|
|
|
|
|
|
|
proxy->vdev = vdev;
|
|
|
|
|
2009-06-21 18:50:13 +02:00
|
|
|
/* Don't support multiple vectors */
|
|
|
|
proxy->vdev->nvectors = 0;
|
2009-05-18 17:45:48 +02:00
|
|
|
sysbus_init_irq(&proxy->busdev, &proxy->irq);
|
2009-06-14 10:38:51 +02:00
|
|
|
iomemtype = cpu_register_io_memory(syborg_virtio_readfn,
|
2010-12-08 12:05:37 +01:00
|
|
|
syborg_virtio_writefn, proxy,
|
|
|
|
DEVICE_NATIVE_ENDIAN);
|
2009-05-18 17:45:48 +02:00
|
|
|
sysbus_init_mmio(&proxy->busdev, 0x1000, iomemtype);
|
|
|
|
|
|
|
|
proxy->id = ((uint32_t)0x1af4 << 16) | vdev->device_id;
|
|
|
|
|
2009-06-27 09:25:07 +02:00
|
|
|
qemu_register_reset(virtio_reset, vdev);
|
2009-06-21 18:50:13 +02:00
|
|
|
|
2009-05-18 17:45:48 +02:00
|
|
|
virtio_bind_device(vdev, &syborg_virtio_bindings, proxy);
|
2010-01-10 12:52:53 +01:00
|
|
|
proxy->host_features |= (0x1 << VIRTIO_F_NOTIFY_ON_EMPTY);
|
|
|
|
proxy->host_features = vdev->get_features(vdev, proxy->host_features);
|
2009-08-14 10:36:05 +02:00
|
|
|
return 0;
|
2009-05-18 17:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Device specific bindings. */
|
|
|
|
|
2009-08-14 10:36:05 +02:00
|
|
|
static int syborg_virtio_net_init(SysBusDevice *dev)
|
2009-05-18 17:45:48 +02:00
|
|
|
{
|
|
|
|
VirtIODevice *vdev;
|
|
|
|
SyborgVirtIOProxy *proxy = FROM_SYSBUS(SyborgVirtIOProxy, dev);
|
|
|
|
|
2010-09-02 17:00:50 +02:00
|
|
|
vdev = virtio_net_init(&dev->qdev, &proxy->nic, &proxy->net);
|
2009-08-14 10:36:05 +02:00
|
|
|
return syborg_virtio_init(proxy, vdev);
|
2009-05-18 17:45:48 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 15:25:35 +02:00
|
|
|
static SysBusDeviceInfo syborg_virtio_net_info = {
|
|
|
|
.init = syborg_virtio_net_init,
|
|
|
|
.qdev.name = "syborg,virtio-net",
|
|
|
|
.qdev.size = sizeof(SyborgVirtIOProxy),
|
|
|
|
.qdev.props = (Property[]) {
|
|
|
|
DEFINE_NIC_PROPERTIES(SyborgVirtIOProxy, nic),
|
2010-01-10 12:52:53 +01:00
|
|
|
DEFINE_VIRTIO_NET_FEATURES(SyborgVirtIOProxy, host_features),
|
2010-09-02 17:00:50 +02:00
|
|
|
DEFINE_PROP_UINT32("x-txtimer", SyborgVirtIOProxy,
|
|
|
|
net.txtimer, TX_TIMER_INTERVAL),
|
2010-09-02 17:00:57 +02:00
|
|
|
DEFINE_PROP_INT32("x-txburst", SyborgVirtIOProxy,
|
|
|
|
net.txburst, TX_BURST),
|
2010-09-02 17:01:10 +02:00
|
|
|
DEFINE_PROP_STRING("tx", SyborgVirtIOProxy, net.tx),
|
2009-10-21 15:25:35 +02:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-18 17:45:48 +02:00
|
|
|
static void syborg_virtio_register_devices(void)
|
|
|
|
{
|
2009-10-21 15:25:35 +02:00
|
|
|
sysbus_register_withprop(&syborg_virtio_net_info);
|
2009-05-18 17:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
device_init(syborg_virtio_register_devices)
|