linux-user: stack_base is now mandatory on all targets
[qemu.git] / hw / s390-virtio-bus.c
1 /*
2 * QEMU S390 virtio target
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "hw.h"
21 #include "block.h"
22 #include "sysemu.h"
23 #include "net.h"
24 #include "boards.h"
25 #include "monitor.h"
26 #include "loader.h"
27 #include "elf.h"
28 #include "hw/virtio.h"
29 #include "hw/virtio-serial.h"
30 #include "hw/virtio-net.h"
31 #include "hw/sysbus.h"
32 #include "kvm.h"
33
34 #include "hw/s390-virtio-bus.h"
35
36 /* #define DEBUG_S390 */
37
38 #ifdef DEBUG_S390
39 #define dprintf(fmt, ...) \
40 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
41 #else
42 #define dprintf(fmt, ...) \
43 do { } while (0)
44 #endif
45
46 #define VIRTIO_EXT_CODE 0x2603
47
48 struct BusInfo s390_virtio_bus_info = {
49 .name = "s390-virtio",
50 .size = sizeof(VirtIOS390Bus),
51 };
52
53 typedef struct VirtIOS390DeviceClass
54 {
55 DeviceClass parent_class;
56 int (*init)(VirtIOS390Device *dev);
57 } VirtIOS390DeviceClass;
58
59 #define TYPE_VIRTIO_S390_DEVICE "virtio-s390-device"
60 #define VIRTIO_S390_DEVICE(obj) \
61 OBJECT_CHECK(VirtIOS390Device, (obj), TYPE_VIRTIO_S390_DEVICE)
62 #define VIRTIO_S390_DEVICE_CLASS(klass) \
63 OBJECT_CLASS_CHECK(VirtIOS390DeviceClass, (klass), TYPE_VIRTIO_S390_DEVICE)
64 #define VIRTIO_S390_DEVICE_GET_CLASS(obj) \
65 OBJECT_GET_CLASS(VirtIOS390DeviceClass, (obj), TYPE_VIRTIO_S390_DEVICE)
66
67 static const VirtIOBindings virtio_s390_bindings;
68
69 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
70
71 /* length of VirtIO device pages */
72 const target_phys_addr_t virtio_size = S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
73
74 VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
75 {
76 VirtIOS390Bus *bus;
77 BusState *_bus;
78 DeviceState *dev;
79
80 /* Create bridge device */
81 dev = qdev_create(NULL, "s390-virtio-bridge");
82 qdev_init_nofail(dev);
83
84 /* Create bus on bridge device */
85
86 _bus = qbus_create(&s390_virtio_bus_info, dev, "s390-virtio");
87 bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
88
89 bus->dev_page = *ram_size;
90 bus->dev_offs = bus->dev_page;
91 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
92
93 /* Enable hotplugging */
94 _bus->allow_hotplug = 1;
95
96 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
97 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
98
99 return bus;
100 }
101
102 static void s390_virtio_irq(CPUState *env, int config_change, uint64_t token)
103 {
104 if (kvm_enabled()) {
105 kvm_s390_virtio_irq(env, config_change, token);
106 } else {
107 cpu_inject_ext(env, VIRTIO_EXT_CODE, config_change, token);
108 }
109 }
110
111 static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
112 {
113 VirtIOS390Bus *bus;
114 int dev_len;
115
116 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
117 dev->vdev = vdev;
118 dev->dev_offs = bus->dev_offs;
119 dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */
120
121 dev_len = VIRTIO_DEV_OFFS_CONFIG;
122 dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN;
123 dev_len += dev->feat_len * 2;
124 dev_len += vdev->config_len;
125
126 bus->dev_offs += dev_len;
127
128 virtio_bind_device(vdev, &virtio_s390_bindings, dev);
129 dev->host_features = vdev->get_features(vdev, dev->host_features);
130 s390_virtio_device_sync(dev);
131
132 if (dev->qdev.hotplugged) {
133 CPUState *env = s390_cpu_addr2state(0);
134 s390_virtio_irq(env, VIRTIO_PARAM_DEV_ADD, dev->dev_offs);
135 }
136
137 return 0;
138 }
139
140 static int s390_virtio_net_init(VirtIOS390Device *dev)
141 {
142 VirtIODevice *vdev;
143
144 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
145 if (!vdev) {
146 return -1;
147 }
148
149 return s390_virtio_device_init(dev, vdev);
150 }
151
152 static int s390_virtio_blk_init(VirtIOS390Device *dev)
153 {
154 VirtIODevice *vdev;
155
156 vdev = virtio_blk_init((DeviceState *)dev, &dev->block,
157 &dev->block_serial);
158 if (!vdev) {
159 return -1;
160 }
161
162 return s390_virtio_device_init(dev, vdev);
163 }
164
165 static int s390_virtio_serial_init(VirtIOS390Device *dev)
166 {
167 VirtIOS390Bus *bus;
168 VirtIODevice *vdev;
169 int r;
170
171 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
172
173 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
174 if (!vdev) {
175 return -1;
176 }
177
178 r = s390_virtio_device_init(dev, vdev);
179 if (!r) {
180 bus->console = dev;
181 }
182
183 return r;
184 }
185
186 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
187 {
188 ram_addr_t token_off;
189
190 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
191 (vq * VIRTIO_VQCONFIG_LEN) +
192 VIRTIO_VQCONFIG_OFFS_TOKEN;
193
194 return ldq_be_phys(token_off);
195 }
196
197 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
198 {
199 VirtIODevice *vdev = dev->vdev;
200 int num_vq;
201
202 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
203 if (!virtio_queue_get_num(vdev, num_vq)) {
204 break;
205 }
206 }
207
208 return num_vq;
209 }
210
211 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
212 {
213 ram_addr_t r = bus->next_ring;
214
215 bus->next_ring += VIRTIO_RING_LEN;
216 return r;
217 }
218
219 void s390_virtio_device_sync(VirtIOS390Device *dev)
220 {
221 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
222 ram_addr_t cur_offs;
223 uint8_t num_vq;
224 int i;
225
226 virtio_reset(dev->vdev);
227
228 /* Sync dev space */
229 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
230
231 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
232 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
233
234 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
235
236 num_vq = s390_virtio_device_num_vq(dev);
237 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
238
239 /* Sync virtqueues */
240 for (i = 0; i < num_vq; i++) {
241 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
242 (i * VIRTIO_VQCONFIG_LEN);
243 ram_addr_t vring;
244
245 vring = s390_virtio_next_ring(bus);
246 virtio_queue_set_addr(dev->vdev, i, vring);
247 virtio_queue_set_vector(dev->vdev, i, i);
248 stq_be_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
249 stw_be_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
250 }
251
252 cur_offs = dev->dev_offs;
253 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
254 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
255
256 /* Sync feature bitmap */
257 stl_le_phys(cur_offs, dev->host_features);
258
259 dev->feat_offs = cur_offs + dev->feat_len;
260 cur_offs += dev->feat_len * 2;
261
262 /* Sync config space */
263 if (dev->vdev->get_config) {
264 dev->vdev->get_config(dev->vdev, dev->vdev->config);
265 }
266
267 cpu_physical_memory_write(cur_offs,
268 dev->vdev->config, dev->vdev->config_len);
269 cur_offs += dev->vdev->config_len;
270 }
271
272 void s390_virtio_device_update_status(VirtIOS390Device *dev)
273 {
274 VirtIODevice *vdev = dev->vdev;
275 uint32_t features;
276
277 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
278
279 /* Update guest supported feature bitmap */
280
281 features = bswap32(ldl_be_phys(dev->feat_offs));
282 virtio_set_features(vdev, features);
283 }
284
285 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
286 {
287 return bus->console;
288 }
289
290 /* Find a device by vring address */
291 VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
292 ram_addr_t mem,
293 int *vq_num)
294 {
295 VirtIOS390Device *_dev;
296 DeviceState *dev;
297 int i;
298
299 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
300 _dev = (VirtIOS390Device *)dev;
301 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
302 if (!virtio_queue_get_addr(_dev->vdev, i))
303 break;
304 if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
305 if (vq_num) {
306 *vq_num = i;
307 }
308 return _dev;
309 }
310 }
311 }
312
313 return NULL;
314 }
315
316 /* Find a device by device descriptor location */
317 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
318 {
319 VirtIOS390Device *_dev;
320 DeviceState *dev;
321
322 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
323 _dev = (VirtIOS390Device *)dev;
324 if (_dev->dev_offs == mem) {
325 return _dev;
326 }
327 }
328
329 return NULL;
330 }
331
332 static void virtio_s390_notify(void *opaque, uint16_t vector)
333 {
334 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
335 uint64_t token = s390_virtio_device_vq_token(dev, vector);
336 CPUState *env = s390_cpu_addr2state(0);
337
338 s390_virtio_irq(env, 0, token);
339 }
340
341 static unsigned virtio_s390_get_features(void *opaque)
342 {
343 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
344 return dev->host_features;
345 }
346
347 /**************** S390 Virtio Bus Device Descriptions *******************/
348
349 static const VirtIOBindings virtio_s390_bindings = {
350 .notify = virtio_s390_notify,
351 .get_features = virtio_s390_get_features,
352 };
353
354 static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
355 {
356 VirtIOS390DeviceClass *dc = VIRTIO_S390_DEVICE_CLASS(klass);
357
358 dc->init = s390_virtio_net_init;
359 }
360
361 static DeviceInfo s390_virtio_net = {
362 .name = "virtio-net-s390",
363 .alias = "virtio-net",
364 .size = sizeof(VirtIOS390Device),
365 .class_init = s390_virtio_net_class_init,
366 .props = (Property[]) {
367 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
368 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
369 net.txtimer, TX_TIMER_INTERVAL),
370 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
371 net.txburst, TX_BURST),
372 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
373 DEFINE_PROP_END_OF_LIST(),
374 },
375 };
376
377 static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
378 {
379 VirtIOS390DeviceClass *dc = VIRTIO_S390_DEVICE_CLASS(klass);
380
381 dc->init = s390_virtio_blk_init;
382 }
383
384 static DeviceInfo s390_virtio_blk = {
385 .name = "virtio-blk-s390",
386 .alias = "virtio-blk",
387 .size = sizeof(VirtIOS390Device),
388 .class_init = s390_virtio_blk_class_init,
389 .props = (Property[]) {
390 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, block),
391 DEFINE_PROP_STRING("serial", VirtIOS390Device, block_serial),
392 DEFINE_PROP_END_OF_LIST(),
393 },
394 };
395
396 static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
397 {
398 VirtIOS390DeviceClass *dc = VIRTIO_S390_DEVICE_CLASS(klass);
399
400 dc->init = s390_virtio_serial_init;
401 }
402
403 static DeviceInfo s390_virtio_serial = {
404 .name = "virtio-serial-s390",
405 .alias = "virtio-serial",
406 .size = sizeof(VirtIOS390Device),
407 .class_init = s390_virtio_serial_class_init,
408 .props = (Property[]) {
409 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
410 serial.max_virtserial_ports, 31),
411 DEFINE_PROP_END_OF_LIST(),
412 },
413 };
414
415 static int s390_virtio_busdev_init(DeviceState *dev, DeviceInfo *info)
416 {
417 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
418 VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev);
419
420 return _info->init(_dev);
421 }
422
423 static void s390_virtio_bus_register_withprop(DeviceInfo *info)
424 {
425 info->init = s390_virtio_busdev_init;
426 info->bus_info = &s390_virtio_bus_info;
427 info->unplug = qdev_simple_unplug_cb;
428
429 assert(info->size >= sizeof(VirtIOS390Device));
430 qdev_register_subclass(info, TYPE_VIRTIO_S390_DEVICE);
431 }
432
433 static TypeInfo virtio_s390_device_info = {
434 .name = TYPE_VIRTIO_S390_DEVICE,
435 .parent = TYPE_DEVICE,
436 .instance_size = sizeof(VirtIOS390Device),
437 .abstract = true,
438 };
439
440 static void s390_virtio_register(void)
441 {
442 type_register_static(&virtio_s390_device_info);
443 s390_virtio_bus_register_withprop(&s390_virtio_serial);
444 s390_virtio_bus_register_withprop(&s390_virtio_blk);
445 s390_virtio_bus_register_withprop(&s390_virtio_net);
446 }
447 device_init(s390_virtio_register);
448
449
450 /***************** S390 Virtio Bus Bridge Device *******************/
451 /* Only required to have the virtio bus as child in the system bus */
452
453 static int s390_virtio_bridge_init(SysBusDevice *dev)
454 {
455 /* nothing */
456 return 0;
457 }
458
459 static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data)
460 {
461 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
462
463 k->init = s390_virtio_bridge_init;
464 }
465
466 static DeviceInfo s390_virtio_bridge_info = {
467 .name = "s390-virtio-bridge",
468 .size = sizeof(SysBusDevice),
469 .no_user = 1,
470 .class_init = s390_virtio_bridge_class_init,
471 };
472
473 static void s390_virtio_register_devices(void)
474 {
475 sysbus_register_withprop(&s390_virtio_bridge_info);
476 }
477
478 device_init(s390_virtio_register_devices)