Statistics
| Branch: | Revision:

root / hw / syborg_virtio.c @ 57a46d05

History | View | Annotate | Download (8.7 kB)

1 340d96e7 Paul Brook
/*
2 340d96e7 Paul Brook
 * Virtio Syborg bindings
3 340d96e7 Paul Brook
 *
4 340d96e7 Paul Brook
 * Copyright (c) 2009 CodeSourcery
5 340d96e7 Paul Brook
 *
6 340d96e7 Paul Brook
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 340d96e7 Paul Brook
 * of this software and associated documentation files (the "Software"), to deal
8 340d96e7 Paul Brook
 * in the Software without restriction, including without limitation the rights
9 340d96e7 Paul Brook
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 340d96e7 Paul Brook
 * copies of the Software, and to permit persons to whom the Software is
11 340d96e7 Paul Brook
 * furnished to do so, subject to the following conditions:
12 340d96e7 Paul Brook
 *
13 340d96e7 Paul Brook
 * The above copyright notice and this permission notice shall be included in
14 340d96e7 Paul Brook
 * all copies or substantial portions of the Software.
15 340d96e7 Paul Brook
 *
16 340d96e7 Paul Brook
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 340d96e7 Paul Brook
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 340d96e7 Paul Brook
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 340d96e7 Paul Brook
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 340d96e7 Paul Brook
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 340d96e7 Paul Brook
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 340d96e7 Paul Brook
 * THE SOFTWARE.
23 340d96e7 Paul Brook
 */
24 340d96e7 Paul Brook
25 340d96e7 Paul Brook
#include "syborg.h"
26 340d96e7 Paul Brook
#include "sysbus.h"
27 340d96e7 Paul Brook
#include "virtio.h"
28 340d96e7 Paul Brook
#include "sysemu.h"
29 340d96e7 Paul Brook
30 340d96e7 Paul Brook
//#define DEBUG_SYBORG_VIRTIO
31 340d96e7 Paul Brook
32 340d96e7 Paul Brook
#ifdef DEBUG_SYBORG_VIRTIO
33 340d96e7 Paul Brook
#define DPRINTF(fmt, ...) \
34 340d96e7 Paul Brook
do { printf("syborg_virtio: " fmt , ## __VA_ARGS__); } while (0)
35 340d96e7 Paul Brook
#define BADF(fmt, ...) \
36 340d96e7 Paul Brook
do { fprintf(stderr, "syborg_virtio: error: " fmt , ## __VA_ARGS__); \
37 340d96e7 Paul Brook
    exit(1);} while (0)
38 340d96e7 Paul Brook
#else
39 340d96e7 Paul Brook
#define DPRINTF(fmt, ...) do {} while(0)
40 340d96e7 Paul Brook
#define BADF(fmt, ...) \
41 340d96e7 Paul Brook
do { fprintf(stderr, "syborg_virtio: error: " fmt , ## __VA_ARGS__);} while (0)
42 340d96e7 Paul Brook
#endif
43 340d96e7 Paul Brook
44 340d96e7 Paul Brook
enum {
45 340d96e7 Paul Brook
    SYBORG_VIRTIO_ID             = 0,
46 340d96e7 Paul Brook
    SYBORG_VIRTIO_DEVTYPE        = 1,
47 340d96e7 Paul Brook
    SYBORG_VIRTIO_HOST_FEATURES  = 2,
48 340d96e7 Paul Brook
    SYBORG_VIRTIO_GUEST_FEATURES = 3,
49 340d96e7 Paul Brook
    SYBORG_VIRTIO_QUEUE_BASE     = 4,
50 340d96e7 Paul Brook
    SYBORG_VIRTIO_QUEUE_NUM      = 5,
51 340d96e7 Paul Brook
    SYBORG_VIRTIO_QUEUE_SEL      = 6,
52 340d96e7 Paul Brook
    SYBORG_VIRTIO_QUEUE_NOTIFY   = 7,
53 340d96e7 Paul Brook
    SYBORG_VIRTIO_STATUS         = 8,
54 340d96e7 Paul Brook
    SYBORG_VIRTIO_INT_ENABLE     = 9,
55 340d96e7 Paul Brook
    SYBORG_VIRTIO_INT_STATUS     = 10
56 340d96e7 Paul Brook
};
57 340d96e7 Paul Brook
58 340d96e7 Paul Brook
#define SYBORG_VIRTIO_CONFIG 0x100
59 340d96e7 Paul Brook
60 340d96e7 Paul Brook
/* Device independent interface.  */
61 340d96e7 Paul Brook
62 340d96e7 Paul Brook
typedef struct {
63 340d96e7 Paul Brook
    SysBusDevice busdev;
64 340d96e7 Paul Brook
    VirtIODevice *vdev;
65 340d96e7 Paul Brook
    qemu_irq irq;
66 340d96e7 Paul Brook
    uint32_t int_enable;
67 340d96e7 Paul Brook
    uint32_t id;
68 97b15621 Gerd Hoffmann
    NICConf nic;
69 340d96e7 Paul Brook
} SyborgVirtIOProxy;
70 340d96e7 Paul Brook
71 c227f099 Anthony Liguori
static uint32_t syborg_virtio_readl(void *opaque, target_phys_addr_t offset)
72 340d96e7 Paul Brook
{
73 340d96e7 Paul Brook
    SyborgVirtIOProxy *s = opaque;
74 340d96e7 Paul Brook
    VirtIODevice *vdev = s->vdev;
75 340d96e7 Paul Brook
    uint32_t ret;
76 340d96e7 Paul Brook
77 340d96e7 Paul Brook
    DPRINTF("readl 0x%x\n", (int)offset);
78 340d96e7 Paul Brook
    if (offset >= SYBORG_VIRTIO_CONFIG) {
79 340d96e7 Paul Brook
        return virtio_config_readl(vdev, offset - SYBORG_VIRTIO_CONFIG);
80 340d96e7 Paul Brook
    }
81 340d96e7 Paul Brook
    switch(offset >> 2) {
82 340d96e7 Paul Brook
    case SYBORG_VIRTIO_ID:
83 340d96e7 Paul Brook
        ret = SYBORG_ID_VIRTIO;
84 340d96e7 Paul Brook
        break;
85 340d96e7 Paul Brook
    case SYBORG_VIRTIO_DEVTYPE:
86 340d96e7 Paul Brook
        ret = s->id;
87 340d96e7 Paul Brook
        break;
88 340d96e7 Paul Brook
    case SYBORG_VIRTIO_HOST_FEATURES:
89 340d96e7 Paul Brook
        ret = vdev->get_features(vdev);
90 340d96e7 Paul Brook
        ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
91 340d96e7 Paul Brook
        break;
92 340d96e7 Paul Brook
    case SYBORG_VIRTIO_GUEST_FEATURES:
93 340d96e7 Paul Brook
        ret = vdev->features;
94 340d96e7 Paul Brook
        break;
95 340d96e7 Paul Brook
    case SYBORG_VIRTIO_QUEUE_BASE:
96 340d96e7 Paul Brook
        ret = virtio_queue_get_addr(vdev, vdev->queue_sel);
97 340d96e7 Paul Brook
        break;
98 340d96e7 Paul Brook
    case SYBORG_VIRTIO_QUEUE_NUM:
99 340d96e7 Paul Brook
        ret = virtio_queue_get_num(vdev, vdev->queue_sel);
100 340d96e7 Paul Brook
        break;
101 340d96e7 Paul Brook
    case SYBORG_VIRTIO_QUEUE_SEL:
102 340d96e7 Paul Brook
        ret = vdev->queue_sel;
103 340d96e7 Paul Brook
        break;
104 340d96e7 Paul Brook
    case SYBORG_VIRTIO_STATUS:
105 340d96e7 Paul Brook
        ret = vdev->status;
106 340d96e7 Paul Brook
        break;
107 340d96e7 Paul Brook
    case SYBORG_VIRTIO_INT_ENABLE:
108 340d96e7 Paul Brook
        ret = s->int_enable;
109 340d96e7 Paul Brook
        break;
110 340d96e7 Paul Brook
    case SYBORG_VIRTIO_INT_STATUS:
111 340d96e7 Paul Brook
        ret = vdev->isr;
112 340d96e7 Paul Brook
        break;
113 340d96e7 Paul Brook
    default:
114 340d96e7 Paul Brook
        BADF("Bad read offset 0x%x\n", (int)offset);
115 340d96e7 Paul Brook
        return 0;
116 340d96e7 Paul Brook
    }
117 340d96e7 Paul Brook
    return ret;
118 340d96e7 Paul Brook
}
119 340d96e7 Paul Brook
120 c227f099 Anthony Liguori
static void syborg_virtio_writel(void *opaque, target_phys_addr_t offset,
121 340d96e7 Paul Brook
                                 uint32_t value)
122 340d96e7 Paul Brook
{
123 340d96e7 Paul Brook
    SyborgVirtIOProxy *s = opaque;
124 340d96e7 Paul Brook
    VirtIODevice *vdev = s->vdev;
125 340d96e7 Paul Brook
126 340d96e7 Paul Brook
    DPRINTF("writel 0x%x = 0x%x\n", (int)offset, value);
127 340d96e7 Paul Brook
    if (offset >= SYBORG_VIRTIO_CONFIG) {
128 340d96e7 Paul Brook
        return virtio_config_writel(vdev, offset - SYBORG_VIRTIO_CONFIG,
129 340d96e7 Paul Brook
                                    value);
130 340d96e7 Paul Brook
    }
131 340d96e7 Paul Brook
    switch (offset >> 2) {
132 340d96e7 Paul Brook
    case SYBORG_VIRTIO_GUEST_FEATURES:
133 340d96e7 Paul Brook
        if (vdev->set_features)
134 340d96e7 Paul Brook
            vdev->set_features(vdev, value);
135 340d96e7 Paul Brook
        vdev->features = value;
136 340d96e7 Paul Brook
        break;
137 340d96e7 Paul Brook
    case SYBORG_VIRTIO_QUEUE_BASE:
138 7055e687 Michael S. Tsirkin
        if (value == 0)
139 7055e687 Michael S. Tsirkin
            virtio_reset(vdev);
140 7055e687 Michael S. Tsirkin
        else
141 7055e687 Michael S. Tsirkin
            virtio_queue_set_addr(vdev, vdev->queue_sel, value);
142 340d96e7 Paul Brook
        break;
143 340d96e7 Paul Brook
    case SYBORG_VIRTIO_QUEUE_SEL:
144 340d96e7 Paul Brook
        if (value < VIRTIO_PCI_QUEUE_MAX)
145 340d96e7 Paul Brook
            vdev->queue_sel = value;
146 340d96e7 Paul Brook
        break;
147 340d96e7 Paul Brook
    case SYBORG_VIRTIO_QUEUE_NOTIFY:
148 340d96e7 Paul Brook
        virtio_queue_notify(vdev, value);
149 340d96e7 Paul Brook
        break;
150 340d96e7 Paul Brook
    case SYBORG_VIRTIO_STATUS:
151 340d96e7 Paul Brook
        vdev->status = value & 0xFF;
152 340d96e7 Paul Brook
        if (vdev->status == 0)
153 340d96e7 Paul Brook
            virtio_reset(vdev);
154 340d96e7 Paul Brook
        break;
155 340d96e7 Paul Brook
    case SYBORG_VIRTIO_INT_ENABLE:
156 340d96e7 Paul Brook
        s->int_enable = value;
157 340d96e7 Paul Brook
        virtio_update_irq(vdev);
158 340d96e7 Paul Brook
        break;
159 340d96e7 Paul Brook
    case SYBORG_VIRTIO_INT_STATUS:
160 340d96e7 Paul Brook
        vdev->isr &= ~value;
161 340d96e7 Paul Brook
        virtio_update_irq(vdev);
162 340d96e7 Paul Brook
        break;
163 340d96e7 Paul Brook
    default:
164 340d96e7 Paul Brook
        BADF("Bad write offset 0x%x\n", (int)offset);
165 340d96e7 Paul Brook
        break;
166 340d96e7 Paul Brook
    }
167 340d96e7 Paul Brook
}
168 340d96e7 Paul Brook
169 c227f099 Anthony Liguori
static uint32_t syborg_virtio_readw(void *opaque, target_phys_addr_t offset)
170 340d96e7 Paul Brook
{
171 340d96e7 Paul Brook
    SyborgVirtIOProxy *s = opaque;
172 340d96e7 Paul Brook
    VirtIODevice *vdev = s->vdev;
173 340d96e7 Paul Brook
174 340d96e7 Paul Brook
    DPRINTF("readw 0x%x\n", (int)offset);
175 340d96e7 Paul Brook
    if (offset >= SYBORG_VIRTIO_CONFIG) {
176 340d96e7 Paul Brook
        return virtio_config_readw(vdev, offset - SYBORG_VIRTIO_CONFIG);
177 340d96e7 Paul Brook
    }
178 340d96e7 Paul Brook
    BADF("Bad halfword read offset 0x%x\n", (int)offset);
179 340d96e7 Paul Brook
    return -1;
180 340d96e7 Paul Brook
}
181 340d96e7 Paul Brook
182 c227f099 Anthony Liguori
static void syborg_virtio_writew(void *opaque, target_phys_addr_t offset,
183 340d96e7 Paul Brook
                                 uint32_t value)
184 340d96e7 Paul Brook
{
185 340d96e7 Paul Brook
    SyborgVirtIOProxy *s = opaque;
186 340d96e7 Paul Brook
    VirtIODevice *vdev = s->vdev;
187 340d96e7 Paul Brook
188 340d96e7 Paul Brook
    DPRINTF("writew 0x%x = 0x%x\n", (int)offset, value);
189 340d96e7 Paul Brook
    if (offset >= SYBORG_VIRTIO_CONFIG) {
190 340d96e7 Paul Brook
        return virtio_config_writew(vdev, offset - SYBORG_VIRTIO_CONFIG,
191 340d96e7 Paul Brook
                                    value);
192 340d96e7 Paul Brook
    }
193 340d96e7 Paul Brook
    BADF("Bad halfword write offset 0x%x\n", (int)offset);
194 340d96e7 Paul Brook
}
195 340d96e7 Paul Brook
196 c227f099 Anthony Liguori
static uint32_t syborg_virtio_readb(void *opaque, target_phys_addr_t offset)
197 340d96e7 Paul Brook
{
198 340d96e7 Paul Brook
    SyborgVirtIOProxy *s = opaque;
199 340d96e7 Paul Brook
    VirtIODevice *vdev = s->vdev;
200 340d96e7 Paul Brook
201 340d96e7 Paul Brook
    DPRINTF("readb 0x%x\n", (int)offset);
202 340d96e7 Paul Brook
    if (offset >= SYBORG_VIRTIO_CONFIG) {
203 340d96e7 Paul Brook
        return virtio_config_readb(vdev, offset - SYBORG_VIRTIO_CONFIG);
204 340d96e7 Paul Brook
    }
205 340d96e7 Paul Brook
    BADF("Bad byte read offset 0x%x\n", (int)offset);
206 340d96e7 Paul Brook
    return -1;
207 340d96e7 Paul Brook
}
208 340d96e7 Paul Brook
209 c227f099 Anthony Liguori
static void syborg_virtio_writeb(void *opaque, target_phys_addr_t offset,
210 340d96e7 Paul Brook
                                 uint32_t value)
211 340d96e7 Paul Brook
{
212 340d96e7 Paul Brook
    SyborgVirtIOProxy *s = opaque;
213 340d96e7 Paul Brook
    VirtIODevice *vdev = s->vdev;
214 340d96e7 Paul Brook
215 340d96e7 Paul Brook
    DPRINTF("writeb 0x%x = 0x%x\n", (int)offset, value);
216 340d96e7 Paul Brook
    if (offset >= SYBORG_VIRTIO_CONFIG) {
217 340d96e7 Paul Brook
        return virtio_config_writeb(vdev, offset - SYBORG_VIRTIO_CONFIG,
218 340d96e7 Paul Brook
                                    value);
219 340d96e7 Paul Brook
    }
220 340d96e7 Paul Brook
    BADF("Bad byte write offset 0x%x\n", (int)offset);
221 340d96e7 Paul Brook
}
222 340d96e7 Paul Brook
223 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const syborg_virtio_readfn[] = {
224 340d96e7 Paul Brook
     syborg_virtio_readb,
225 340d96e7 Paul Brook
     syborg_virtio_readw,
226 340d96e7 Paul Brook
     syborg_virtio_readl
227 340d96e7 Paul Brook
};
228 340d96e7 Paul Brook
229 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const syborg_virtio_writefn[] = {
230 340d96e7 Paul Brook
     syborg_virtio_writeb,
231 340d96e7 Paul Brook
     syborg_virtio_writew,
232 340d96e7 Paul Brook
     syborg_virtio_writel
233 340d96e7 Paul Brook
};
234 340d96e7 Paul Brook
235 7055e687 Michael S. Tsirkin
static void syborg_virtio_update_irq(void *opaque, uint16_t vector)
236 340d96e7 Paul Brook
{
237 340d96e7 Paul Brook
    SyborgVirtIOProxy *proxy = opaque;
238 340d96e7 Paul Brook
    int level;
239 340d96e7 Paul Brook
240 340d96e7 Paul Brook
    level = proxy->int_enable & proxy->vdev->isr;
241 340d96e7 Paul Brook
    DPRINTF("IRQ %d\n", level);
242 340d96e7 Paul Brook
    qemu_set_irq(proxy->irq, level != 0);
243 340d96e7 Paul Brook
}
244 340d96e7 Paul Brook
245 340d96e7 Paul Brook
static VirtIOBindings syborg_virtio_bindings = {
246 7055e687 Michael S. Tsirkin
    .notify = syborg_virtio_update_irq
247 340d96e7 Paul Brook
};
248 340d96e7 Paul Brook
249 81a322d4 Gerd Hoffmann
static int syborg_virtio_init(SyborgVirtIOProxy *proxy, VirtIODevice *vdev)
250 340d96e7 Paul Brook
{
251 340d96e7 Paul Brook
    int iomemtype;
252 340d96e7 Paul Brook
253 340d96e7 Paul Brook
    proxy->vdev = vdev;
254 340d96e7 Paul Brook
255 7055e687 Michael S. Tsirkin
    /* Don't support multiple vectors */
256 7055e687 Michael S. Tsirkin
    proxy->vdev->nvectors = 0;
257 340d96e7 Paul Brook
    sysbus_init_irq(&proxy->busdev, &proxy->irq);
258 1eed09cb Avi Kivity
    iomemtype = cpu_register_io_memory(syborg_virtio_readfn,
259 340d96e7 Paul Brook
                                       syborg_virtio_writefn, proxy);
260 340d96e7 Paul Brook
    sysbus_init_mmio(&proxy->busdev, 0x1000, iomemtype);
261 340d96e7 Paul Brook
262 340d96e7 Paul Brook
    proxy->id = ((uint32_t)0x1af4 << 16) | vdev->device_id;
263 340d96e7 Paul Brook
264 a08d4367 Jan Kiszka
    qemu_register_reset(virtio_reset, vdev);
265 7055e687 Michael S. Tsirkin
266 340d96e7 Paul Brook
    virtio_bind_device(vdev, &syborg_virtio_bindings, proxy);
267 81a322d4 Gerd Hoffmann
    return 0;
268 340d96e7 Paul Brook
}
269 340d96e7 Paul Brook
270 340d96e7 Paul Brook
/* Device specific bindings.  */
271 340d96e7 Paul Brook
272 81a322d4 Gerd Hoffmann
static int syborg_virtio_net_init(SysBusDevice *dev)
273 340d96e7 Paul Brook
{
274 340d96e7 Paul Brook
    VirtIODevice *vdev;
275 340d96e7 Paul Brook
    SyborgVirtIOProxy *proxy = FROM_SYSBUS(SyborgVirtIOProxy, dev);
276 340d96e7 Paul Brook
277 97b15621 Gerd Hoffmann
    vdev = virtio_net_init(&dev->qdev, &proxy->nic);
278 81a322d4 Gerd Hoffmann
    return syborg_virtio_init(proxy, vdev);
279 340d96e7 Paul Brook
}
280 340d96e7 Paul Brook
281 97b15621 Gerd Hoffmann
static SysBusDeviceInfo syborg_virtio_net_info = {
282 97b15621 Gerd Hoffmann
    .init = syborg_virtio_net_init,
283 97b15621 Gerd Hoffmann
    .qdev.name  = "syborg,virtio-net",
284 97b15621 Gerd Hoffmann
    .qdev.size  = sizeof(SyborgVirtIOProxy),
285 97b15621 Gerd Hoffmann
    .qdev.props = (Property[]) {
286 97b15621 Gerd Hoffmann
        DEFINE_NIC_PROPERTIES(SyborgVirtIOProxy, nic),
287 97b15621 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
288 97b15621 Gerd Hoffmann
    }
289 97b15621 Gerd Hoffmann
};
290 97b15621 Gerd Hoffmann
291 340d96e7 Paul Brook
static void syborg_virtio_register_devices(void)
292 340d96e7 Paul Brook
{
293 97b15621 Gerd Hoffmann
    sysbus_register_withprop(&syborg_virtio_net_info);
294 340d96e7 Paul Brook
}
295 340d96e7 Paul Brook
296 340d96e7 Paul Brook
device_init(syborg_virtio_register_devices)