root / hw / syborg_virtio.c @ 3180d772
History | View | Annotate | Download (8.4 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 | 340d96e7 | Paul Brook | } SyborgVirtIOProxy; |
69 | 340d96e7 | Paul Brook | |
70 | 340d96e7 | Paul Brook | static uint32_t syborg_virtio_readl(void *opaque, target_phys_addr_t offset) |
71 | 340d96e7 | Paul Brook | { |
72 | 340d96e7 | Paul Brook | SyborgVirtIOProxy *s = opaque; |
73 | 340d96e7 | Paul Brook | VirtIODevice *vdev = s->vdev; |
74 | 340d96e7 | Paul Brook | uint32_t ret; |
75 | 340d96e7 | Paul Brook | |
76 | 340d96e7 | Paul Brook | DPRINTF("readl 0x%x\n", (int)offset); |
77 | 340d96e7 | Paul Brook | if (offset >= SYBORG_VIRTIO_CONFIG) {
|
78 | 340d96e7 | Paul Brook | return virtio_config_readl(vdev, offset - SYBORG_VIRTIO_CONFIG);
|
79 | 340d96e7 | Paul Brook | } |
80 | 340d96e7 | Paul Brook | switch(offset >> 2) { |
81 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_ID:
|
82 | 340d96e7 | Paul Brook | ret = SYBORG_ID_VIRTIO; |
83 | 340d96e7 | Paul Brook | break;
|
84 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_DEVTYPE:
|
85 | 340d96e7 | Paul Brook | ret = s->id; |
86 | 340d96e7 | Paul Brook | break;
|
87 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_HOST_FEATURES:
|
88 | 340d96e7 | Paul Brook | ret = vdev->get_features(vdev); |
89 | 340d96e7 | Paul Brook | ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
|
90 | 340d96e7 | Paul Brook | break;
|
91 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_GUEST_FEATURES:
|
92 | 340d96e7 | Paul Brook | ret = vdev->features; |
93 | 340d96e7 | Paul Brook | break;
|
94 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_QUEUE_BASE:
|
95 | 340d96e7 | Paul Brook | ret = virtio_queue_get_addr(vdev, vdev->queue_sel); |
96 | 340d96e7 | Paul Brook | break;
|
97 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_QUEUE_NUM:
|
98 | 340d96e7 | Paul Brook | ret = virtio_queue_get_num(vdev, vdev->queue_sel); |
99 | 340d96e7 | Paul Brook | break;
|
100 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_QUEUE_SEL:
|
101 | 340d96e7 | Paul Brook | ret = vdev->queue_sel; |
102 | 340d96e7 | Paul Brook | break;
|
103 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_STATUS:
|
104 | 340d96e7 | Paul Brook | ret = vdev->status; |
105 | 340d96e7 | Paul Brook | break;
|
106 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_INT_ENABLE:
|
107 | 340d96e7 | Paul Brook | ret = s->int_enable; |
108 | 340d96e7 | Paul Brook | break;
|
109 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_INT_STATUS:
|
110 | 340d96e7 | Paul Brook | ret = vdev->isr; |
111 | 340d96e7 | Paul Brook | break;
|
112 | 340d96e7 | Paul Brook | default:
|
113 | 340d96e7 | Paul Brook | BADF("Bad read offset 0x%x\n", (int)offset); |
114 | 340d96e7 | Paul Brook | return 0; |
115 | 340d96e7 | Paul Brook | } |
116 | 340d96e7 | Paul Brook | return ret;
|
117 | 340d96e7 | Paul Brook | } |
118 | 340d96e7 | Paul Brook | |
119 | 340d96e7 | Paul Brook | static void syborg_virtio_writel(void *opaque, target_phys_addr_t offset, |
120 | 340d96e7 | Paul Brook | uint32_t value) |
121 | 340d96e7 | Paul Brook | { |
122 | 340d96e7 | Paul Brook | SyborgVirtIOProxy *s = opaque; |
123 | 340d96e7 | Paul Brook | VirtIODevice *vdev = s->vdev; |
124 | 340d96e7 | Paul Brook | |
125 | 340d96e7 | Paul Brook | DPRINTF("writel 0x%x = 0x%x\n", (int)offset, value); |
126 | 340d96e7 | Paul Brook | if (offset >= SYBORG_VIRTIO_CONFIG) {
|
127 | 340d96e7 | Paul Brook | return virtio_config_writel(vdev, offset - SYBORG_VIRTIO_CONFIG,
|
128 | 340d96e7 | Paul Brook | value); |
129 | 340d96e7 | Paul Brook | } |
130 | 340d96e7 | Paul Brook | switch (offset >> 2) { |
131 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_GUEST_FEATURES:
|
132 | 340d96e7 | Paul Brook | if (vdev->set_features)
|
133 | 340d96e7 | Paul Brook | vdev->set_features(vdev, value); |
134 | 340d96e7 | Paul Brook | vdev->features = value; |
135 | 340d96e7 | Paul Brook | break;
|
136 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_QUEUE_BASE:
|
137 | 7055e687 | Michael S. Tsirkin | if (value == 0) |
138 | 7055e687 | Michael S. Tsirkin | virtio_reset(vdev); |
139 | 7055e687 | Michael S. Tsirkin | else
|
140 | 7055e687 | Michael S. Tsirkin | virtio_queue_set_addr(vdev, vdev->queue_sel, value); |
141 | 340d96e7 | Paul Brook | break;
|
142 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_QUEUE_SEL:
|
143 | 340d96e7 | Paul Brook | if (value < VIRTIO_PCI_QUEUE_MAX)
|
144 | 340d96e7 | Paul Brook | vdev->queue_sel = value; |
145 | 340d96e7 | Paul Brook | break;
|
146 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_QUEUE_NOTIFY:
|
147 | 340d96e7 | Paul Brook | virtio_queue_notify(vdev, value); |
148 | 340d96e7 | Paul Brook | break;
|
149 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_STATUS:
|
150 | 340d96e7 | Paul Brook | vdev->status = value & 0xFF;
|
151 | 340d96e7 | Paul Brook | if (vdev->status == 0) |
152 | 340d96e7 | Paul Brook | virtio_reset(vdev); |
153 | 340d96e7 | Paul Brook | break;
|
154 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_INT_ENABLE:
|
155 | 340d96e7 | Paul Brook | s->int_enable = value; |
156 | 340d96e7 | Paul Brook | virtio_update_irq(vdev); |
157 | 340d96e7 | Paul Brook | break;
|
158 | 340d96e7 | Paul Brook | case SYBORG_VIRTIO_INT_STATUS:
|
159 | 340d96e7 | Paul Brook | vdev->isr &= ~value; |
160 | 340d96e7 | Paul Brook | virtio_update_irq(vdev); |
161 | 340d96e7 | Paul Brook | break;
|
162 | 340d96e7 | Paul Brook | default:
|
163 | 340d96e7 | Paul Brook | BADF("Bad write offset 0x%x\n", (int)offset); |
164 | 340d96e7 | Paul Brook | break;
|
165 | 340d96e7 | Paul Brook | } |
166 | 340d96e7 | Paul Brook | } |
167 | 340d96e7 | Paul Brook | |
168 | 340d96e7 | Paul Brook | static uint32_t syborg_virtio_readw(void *opaque, target_phys_addr_t offset) |
169 | 340d96e7 | Paul Brook | { |
170 | 340d96e7 | Paul Brook | SyborgVirtIOProxy *s = opaque; |
171 | 340d96e7 | Paul Brook | VirtIODevice *vdev = s->vdev; |
172 | 340d96e7 | Paul Brook | |
173 | 340d96e7 | Paul Brook | DPRINTF("readw 0x%x\n", (int)offset); |
174 | 340d96e7 | Paul Brook | if (offset >= SYBORG_VIRTIO_CONFIG) {
|
175 | 340d96e7 | Paul Brook | return virtio_config_readw(vdev, offset - SYBORG_VIRTIO_CONFIG);
|
176 | 340d96e7 | Paul Brook | } |
177 | 340d96e7 | Paul Brook | BADF("Bad halfword read offset 0x%x\n", (int)offset); |
178 | 340d96e7 | Paul Brook | return -1; |
179 | 340d96e7 | Paul Brook | } |
180 | 340d96e7 | Paul Brook | |
181 | 340d96e7 | Paul Brook | static void syborg_virtio_writew(void *opaque, target_phys_addr_t offset, |
182 | 340d96e7 | Paul Brook | uint32_t value) |
183 | 340d96e7 | Paul Brook | { |
184 | 340d96e7 | Paul Brook | SyborgVirtIOProxy *s = opaque; |
185 | 340d96e7 | Paul Brook | VirtIODevice *vdev = s->vdev; |
186 | 340d96e7 | Paul Brook | |
187 | 340d96e7 | Paul Brook | DPRINTF("writew 0x%x = 0x%x\n", (int)offset, value); |
188 | 340d96e7 | Paul Brook | if (offset >= SYBORG_VIRTIO_CONFIG) {
|
189 | 340d96e7 | Paul Brook | return virtio_config_writew(vdev, offset - SYBORG_VIRTIO_CONFIG,
|
190 | 340d96e7 | Paul Brook | value); |
191 | 340d96e7 | Paul Brook | } |
192 | 340d96e7 | Paul Brook | BADF("Bad halfword write offset 0x%x\n", (int)offset); |
193 | 340d96e7 | Paul Brook | } |
194 | 340d96e7 | Paul Brook | |
195 | 340d96e7 | Paul Brook | static uint32_t syborg_virtio_readb(void *opaque, target_phys_addr_t offset) |
196 | 340d96e7 | Paul Brook | { |
197 | 340d96e7 | Paul Brook | SyborgVirtIOProxy *s = opaque; |
198 | 340d96e7 | Paul Brook | VirtIODevice *vdev = s->vdev; |
199 | 340d96e7 | Paul Brook | |
200 | 340d96e7 | Paul Brook | DPRINTF("readb 0x%x\n", (int)offset); |
201 | 340d96e7 | Paul Brook | if (offset >= SYBORG_VIRTIO_CONFIG) {
|
202 | 340d96e7 | Paul Brook | return virtio_config_readb(vdev, offset - SYBORG_VIRTIO_CONFIG);
|
203 | 340d96e7 | Paul Brook | } |
204 | 340d96e7 | Paul Brook | BADF("Bad byte read offset 0x%x\n", (int)offset); |
205 | 340d96e7 | Paul Brook | return -1; |
206 | 340d96e7 | Paul Brook | } |
207 | 340d96e7 | Paul Brook | |
208 | 340d96e7 | Paul Brook | static void syborg_virtio_writeb(void *opaque, target_phys_addr_t offset, |
209 | 340d96e7 | Paul Brook | uint32_t value) |
210 | 340d96e7 | Paul Brook | { |
211 | 340d96e7 | Paul Brook | SyborgVirtIOProxy *s = opaque; |
212 | 340d96e7 | Paul Brook | VirtIODevice *vdev = s->vdev; |
213 | 340d96e7 | Paul Brook | |
214 | 340d96e7 | Paul Brook | DPRINTF("writeb 0x%x = 0x%x\n", (int)offset, value); |
215 | 340d96e7 | Paul Brook | if (offset >= SYBORG_VIRTIO_CONFIG) {
|
216 | 340d96e7 | Paul Brook | return virtio_config_writeb(vdev, offset - SYBORG_VIRTIO_CONFIG,
|
217 | 340d96e7 | Paul Brook | value); |
218 | 340d96e7 | Paul Brook | } |
219 | 340d96e7 | Paul Brook | BADF("Bad byte write offset 0x%x\n", (int)offset); |
220 | 340d96e7 | Paul Brook | } |
221 | 340d96e7 | Paul Brook | |
222 | 340d96e7 | Paul Brook | static CPUReadMemoryFunc *syborg_virtio_readfn[] = {
|
223 | 340d96e7 | Paul Brook | syborg_virtio_readb, |
224 | 340d96e7 | Paul Brook | syborg_virtio_readw, |
225 | 340d96e7 | Paul Brook | syborg_virtio_readl |
226 | 340d96e7 | Paul Brook | }; |
227 | 340d96e7 | Paul Brook | |
228 | 340d96e7 | Paul Brook | static CPUWriteMemoryFunc *syborg_virtio_writefn[] = {
|
229 | 340d96e7 | Paul Brook | syborg_virtio_writeb, |
230 | 340d96e7 | Paul Brook | syborg_virtio_writew, |
231 | 340d96e7 | Paul Brook | syborg_virtio_writel |
232 | 340d96e7 | Paul Brook | }; |
233 | 340d96e7 | Paul Brook | |
234 | 7055e687 | Michael S. Tsirkin | static void syborg_virtio_update_irq(void *opaque, uint16_t vector) |
235 | 340d96e7 | Paul Brook | { |
236 | 340d96e7 | Paul Brook | SyborgVirtIOProxy *proxy = opaque; |
237 | 340d96e7 | Paul Brook | int level;
|
238 | 340d96e7 | Paul Brook | |
239 | 340d96e7 | Paul Brook | level = proxy->int_enable & proxy->vdev->isr; |
240 | 340d96e7 | Paul Brook | DPRINTF("IRQ %d\n", level);
|
241 | 340d96e7 | Paul Brook | qemu_set_irq(proxy->irq, level != 0);
|
242 | 340d96e7 | Paul Brook | } |
243 | 340d96e7 | Paul Brook | |
244 | 340d96e7 | Paul Brook | static VirtIOBindings syborg_virtio_bindings = {
|
245 | 7055e687 | Michael S. Tsirkin | .notify = syborg_virtio_update_irq |
246 | 340d96e7 | Paul Brook | }; |
247 | 340d96e7 | Paul Brook | |
248 | 340d96e7 | Paul Brook | static void syborg_virtio_init(SyborgVirtIOProxy *proxy, VirtIODevice *vdev) |
249 | 340d96e7 | Paul Brook | { |
250 | 340d96e7 | Paul Brook | int iomemtype;
|
251 | 340d96e7 | Paul Brook | |
252 | 340d96e7 | Paul Brook | proxy->vdev = vdev; |
253 | 340d96e7 | Paul Brook | |
254 | 7055e687 | Michael S. Tsirkin | /* Don't support multiple vectors */
|
255 | 7055e687 | Michael S. Tsirkin | proxy->vdev->nvectors = 0;
|
256 | 340d96e7 | Paul Brook | sysbus_init_irq(&proxy->busdev, &proxy->irq); |
257 | 1eed09cb | Avi Kivity | iomemtype = cpu_register_io_memory(syborg_virtio_readfn, |
258 | 340d96e7 | Paul Brook | syborg_virtio_writefn, proxy); |
259 | 340d96e7 | Paul Brook | sysbus_init_mmio(&proxy->busdev, 0x1000, iomemtype);
|
260 | 340d96e7 | Paul Brook | |
261 | 340d96e7 | Paul Brook | proxy->id = ((uint32_t)0x1af4 << 16) | vdev->device_id; |
262 | 340d96e7 | Paul Brook | |
263 | a08d4367 | Jan Kiszka | qemu_register_reset(virtio_reset, vdev); |
264 | 7055e687 | Michael S. Tsirkin | |
265 | 340d96e7 | Paul Brook | virtio_bind_device(vdev, &syborg_virtio_bindings, proxy); |
266 | 340d96e7 | Paul Brook | } |
267 | 340d96e7 | Paul Brook | |
268 | 340d96e7 | Paul Brook | /* Device specific bindings. */
|
269 | 340d96e7 | Paul Brook | |
270 | 340d96e7 | Paul Brook | static void syborg_virtio_net_init(SysBusDevice *dev) |
271 | 340d96e7 | Paul Brook | { |
272 | 340d96e7 | Paul Brook | VirtIODevice *vdev; |
273 | 340d96e7 | Paul Brook | SyborgVirtIOProxy *proxy = FROM_SYSBUS(SyborgVirtIOProxy, dev); |
274 | 340d96e7 | Paul Brook | |
275 | 340d96e7 | Paul Brook | vdev = virtio_net_init(&dev->qdev); |
276 | 340d96e7 | Paul Brook | syborg_virtio_init(proxy, vdev); |
277 | 340d96e7 | Paul Brook | } |
278 | 340d96e7 | Paul Brook | |
279 | 340d96e7 | Paul Brook | static void syborg_virtio_register_devices(void) |
280 | 340d96e7 | Paul Brook | { |
281 | 340d96e7 | Paul Brook | sysbus_register_dev("syborg,virtio-net", sizeof(SyborgVirtIOProxy), |
282 | 340d96e7 | Paul Brook | syborg_virtio_net_init); |
283 | 340d96e7 | Paul Brook | } |
284 | 340d96e7 | Paul Brook | |
285 | 340d96e7 | Paul Brook | device_init(syborg_virtio_register_devices) |