msi: Guard msi/msix_write_config with msi_present
[qemu-kvm.git] / hw / s390-virtio-bus.c
blob1d38a8f5c5f3b32b1a1003ef71417954f41f87a4
1 /*
2 * QEMU S390 virtio target
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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.
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.
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/>.
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"
34 #include "hw/s390-virtio-bus.h"
36 /* #define DEBUG_S390 */
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
46 #define VIRTIO_EXT_CODE 0x2603
48 struct BusInfo s390_virtio_bus_info = {
49 .name = "s390-virtio",
50 .size = sizeof(VirtIOS390Bus),
53 static const VirtIOBindings virtio_s390_bindings;
55 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
57 /* length of VirtIO device pages */
58 const target_phys_addr_t virtio_size = S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
60 static void s390_virtio_bus_reset(void *opaque)
62 VirtIOS390Bus *bus = opaque;
63 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
66 void s390_virtio_reset_idx(VirtIOS390Device *dev)
68 int i;
69 target_phys_addr_t idx_addr;
70 uint8_t num_vq;
72 num_vq = s390_virtio_device_num_vq(dev);
73 for (i = 0; i < num_vq; i++) {
74 idx_addr = virtio_queue_get_avail_addr(dev->vdev, i) +
75 VIRTIO_VRING_AVAIL_IDX_OFFS;
76 stw_phys(idx_addr, 0);
77 idx_addr = virtio_queue_get_used_addr(dev->vdev, i) +
78 VIRTIO_VRING_USED_IDX_OFFS;
79 stw_phys(idx_addr, 0);
83 VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
85 VirtIOS390Bus *bus;
86 BusState *_bus;
87 DeviceState *dev;
89 /* Create bridge device */
90 dev = qdev_create(NULL, "s390-virtio-bridge");
91 qdev_init_nofail(dev);
93 /* Create bus on bridge device */
95 _bus = qbus_create(&s390_virtio_bus_info, dev, "s390-virtio");
96 bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
98 bus->dev_page = *ram_size;
99 bus->dev_offs = bus->dev_page;
100 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
102 /* Enable hotplugging */
103 _bus->allow_hotplug = 1;
105 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
106 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
108 qemu_register_reset(s390_virtio_bus_reset, bus);
109 return bus;
112 static void s390_virtio_irq(CPUS390XState *env, int config_change, uint64_t token)
114 if (kvm_enabled()) {
115 kvm_s390_virtio_irq(env, config_change, token);
116 } else {
117 cpu_inject_ext(env, VIRTIO_EXT_CODE, config_change, token);
121 static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
123 VirtIOS390Bus *bus;
124 int dev_len;
126 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
127 dev->vdev = vdev;
128 dev->dev_offs = bus->dev_offs;
129 dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */
131 dev_len = VIRTIO_DEV_OFFS_CONFIG;
132 dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN;
133 dev_len += dev->feat_len * 2;
134 dev_len += vdev->config_len;
136 bus->dev_offs += dev_len;
138 virtio_bind_device(vdev, &virtio_s390_bindings, dev);
139 dev->host_features = vdev->get_features(vdev, dev->host_features);
140 s390_virtio_device_sync(dev);
141 s390_virtio_reset_idx(dev);
142 if (dev->qdev.hotplugged) {
143 CPUS390XState *env = s390_cpu_addr2state(0);
144 s390_virtio_irq(env, VIRTIO_PARAM_DEV_ADD, dev->dev_offs);
147 return 0;
150 static int s390_virtio_net_init(VirtIOS390Device *dev)
152 VirtIODevice *vdev;
154 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
155 if (!vdev) {
156 return -1;
159 return s390_virtio_device_init(dev, vdev);
162 static int s390_virtio_blk_init(VirtIOS390Device *dev)
164 VirtIODevice *vdev;
166 vdev = virtio_blk_init((DeviceState *)dev, &dev->blk);
167 if (!vdev) {
168 return -1;
171 return s390_virtio_device_init(dev, vdev);
174 static int s390_virtio_serial_init(VirtIOS390Device *dev)
176 VirtIOS390Bus *bus;
177 VirtIODevice *vdev;
178 int r;
180 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
182 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
183 if (!vdev) {
184 return -1;
187 r = s390_virtio_device_init(dev, vdev);
188 if (!r) {
189 bus->console = dev;
192 return r;
195 static int s390_virtio_scsi_init(VirtIOS390Device *dev)
197 VirtIODevice *vdev;
199 vdev = virtio_scsi_init((DeviceState *)dev, &dev->scsi);
200 if (!vdev) {
201 return -1;
204 return s390_virtio_device_init(dev, vdev);
207 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
209 ram_addr_t token_off;
211 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
212 (vq * VIRTIO_VQCONFIG_LEN) +
213 VIRTIO_VQCONFIG_OFFS_TOKEN;
215 return ldq_be_phys(token_off);
218 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
220 VirtIODevice *vdev = dev->vdev;
221 int num_vq;
223 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
224 if (!virtio_queue_get_num(vdev, num_vq)) {
225 break;
229 return num_vq;
232 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
234 ram_addr_t r = bus->next_ring;
236 bus->next_ring += VIRTIO_RING_LEN;
237 return r;
240 void s390_virtio_device_sync(VirtIOS390Device *dev)
242 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
243 ram_addr_t cur_offs;
244 uint8_t num_vq;
245 int i;
247 virtio_reset(dev->vdev);
249 /* Sync dev space */
250 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
252 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
253 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
255 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
257 num_vq = s390_virtio_device_num_vq(dev);
258 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
260 /* Sync virtqueues */
261 for (i = 0; i < num_vq; i++) {
262 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
263 (i * VIRTIO_VQCONFIG_LEN);
264 ram_addr_t vring;
266 vring = s390_virtio_next_ring(bus);
267 virtio_queue_set_addr(dev->vdev, i, vring);
268 virtio_queue_set_vector(dev->vdev, i, i);
269 stq_be_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
270 stw_be_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
273 cur_offs = dev->dev_offs;
274 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
275 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
277 /* Sync feature bitmap */
278 stl_le_phys(cur_offs, dev->host_features);
280 dev->feat_offs = cur_offs + dev->feat_len;
281 cur_offs += dev->feat_len * 2;
283 /* Sync config space */
284 if (dev->vdev->get_config) {
285 dev->vdev->get_config(dev->vdev, dev->vdev->config);
288 cpu_physical_memory_write(cur_offs,
289 dev->vdev->config, dev->vdev->config_len);
290 cur_offs += dev->vdev->config_len;
293 void s390_virtio_device_update_status(VirtIOS390Device *dev)
295 VirtIODevice *vdev = dev->vdev;
296 uint32_t features;
298 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
300 /* Update guest supported feature bitmap */
302 features = bswap32(ldl_be_phys(dev->feat_offs));
303 virtio_set_features(vdev, features);
306 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
308 return bus->console;
311 /* Find a device by vring address */
312 VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
313 ram_addr_t mem,
314 int *vq_num)
316 VirtIOS390Device *_dev;
317 DeviceState *dev;
318 int i;
320 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
321 _dev = (VirtIOS390Device *)dev;
322 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
323 if (!virtio_queue_get_addr(_dev->vdev, i))
324 break;
325 if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
326 if (vq_num) {
327 *vq_num = i;
329 return _dev;
334 return NULL;
337 /* Find a device by device descriptor location */
338 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
340 VirtIOS390Device *_dev;
341 DeviceState *dev;
343 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
344 _dev = (VirtIOS390Device *)dev;
345 if (_dev->dev_offs == mem) {
346 return _dev;
350 return NULL;
353 static void virtio_s390_notify(void *opaque, uint16_t vector)
355 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
356 uint64_t token = s390_virtio_device_vq_token(dev, vector);
357 CPUS390XState *env = s390_cpu_addr2state(0);
359 s390_virtio_irq(env, 0, token);
362 static unsigned virtio_s390_get_features(void *opaque)
364 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
365 return dev->host_features;
368 /**************** S390 Virtio Bus Device Descriptions *******************/
370 static const VirtIOBindings virtio_s390_bindings = {
371 .notify = virtio_s390_notify,
372 .get_features = virtio_s390_get_features,
375 static Property s390_virtio_net_properties[] = {
376 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
377 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
378 net.txtimer, TX_TIMER_INTERVAL),
379 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
380 net.txburst, TX_BURST),
381 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
382 DEFINE_PROP_END_OF_LIST(),
385 static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
387 DeviceClass *dc = DEVICE_CLASS(klass);
388 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
390 k->init = s390_virtio_net_init;
391 dc->props = s390_virtio_net_properties;
394 static TypeInfo s390_virtio_net = {
395 .name = "virtio-net-s390",
396 .parent = TYPE_VIRTIO_S390_DEVICE,
397 .instance_size = sizeof(VirtIOS390Device),
398 .class_init = s390_virtio_net_class_init,
401 static Property s390_virtio_blk_properties[] = {
402 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, blk.conf),
403 DEFINE_PROP_STRING("serial", VirtIOS390Device, blk.serial),
404 #ifdef __linux__
405 DEFINE_PROP_BIT("scsi", VirtIOS390Device, blk.scsi, 0, true),
406 #endif
407 DEFINE_PROP_END_OF_LIST(),
410 static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
412 DeviceClass *dc = DEVICE_CLASS(klass);
413 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
415 k->init = s390_virtio_blk_init;
416 dc->props = s390_virtio_blk_properties;
419 static TypeInfo s390_virtio_blk = {
420 .name = "virtio-blk-s390",
421 .parent = TYPE_VIRTIO_S390_DEVICE,
422 .instance_size = sizeof(VirtIOS390Device),
423 .class_init = s390_virtio_blk_class_init,
426 static Property s390_virtio_serial_properties[] = {
427 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
428 serial.max_virtserial_ports, 31),
429 DEFINE_PROP_END_OF_LIST(),
432 static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
434 DeviceClass *dc = DEVICE_CLASS(klass);
435 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
437 k->init = s390_virtio_serial_init;
438 dc->props = s390_virtio_serial_properties;
441 static TypeInfo s390_virtio_serial = {
442 .name = "virtio-serial-s390",
443 .parent = TYPE_VIRTIO_S390_DEVICE,
444 .instance_size = sizeof(VirtIOS390Device),
445 .class_init = s390_virtio_serial_class_init,
448 static int s390_virtio_busdev_init(DeviceState *dev)
450 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
451 VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev);
453 return _info->init(_dev);
456 static void virtio_s390_device_class_init(ObjectClass *klass, void *data)
458 DeviceClass *dc = DEVICE_CLASS(klass);
460 dc->init = s390_virtio_busdev_init;
461 dc->bus_info = &s390_virtio_bus_info;
462 dc->unplug = qdev_simple_unplug_cb;
465 static TypeInfo virtio_s390_device_info = {
466 .name = TYPE_VIRTIO_S390_DEVICE,
467 .parent = TYPE_DEVICE,
468 .instance_size = sizeof(VirtIOS390Device),
469 .class_init = virtio_s390_device_class_init,
470 .class_size = sizeof(VirtIOS390DeviceClass),
471 .abstract = true,
474 static Property s390_virtio_scsi_properties[] = {
475 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOS390Device, host_features, scsi),
476 DEFINE_PROP_END_OF_LIST(),
479 static void s390_virtio_scsi_class_init(ObjectClass *klass, void *data)
481 DeviceClass *dc = DEVICE_CLASS(klass);
482 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
484 k->init = s390_virtio_scsi_init;
485 dc->props = s390_virtio_scsi_properties;
488 static TypeInfo s390_virtio_scsi = {
489 .name = "virtio-scsi-s390",
490 .parent = TYPE_VIRTIO_S390_DEVICE,
491 .instance_size = sizeof(VirtIOS390Device),
492 .class_init = s390_virtio_scsi_class_init,
495 /***************** S390 Virtio Bus Bridge Device *******************/
496 /* Only required to have the virtio bus as child in the system bus */
498 static int s390_virtio_bridge_init(SysBusDevice *dev)
500 /* nothing */
501 return 0;
504 static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data)
506 DeviceClass *dc = DEVICE_CLASS(klass);
507 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
509 k->init = s390_virtio_bridge_init;
510 dc->no_user = 1;
513 static TypeInfo s390_virtio_bridge_info = {
514 .name = "s390-virtio-bridge",
515 .parent = TYPE_SYS_BUS_DEVICE,
516 .instance_size = sizeof(SysBusDevice),
517 .class_init = s390_virtio_bridge_class_init,
520 static void s390_virtio_register_types(void)
522 type_register_static(&virtio_s390_device_info);
523 type_register_static(&s390_virtio_serial);
524 type_register_static(&s390_virtio_blk);
525 type_register_static(&s390_virtio_net);
526 type_register_static(&s390_virtio_scsi);
527 type_register_static(&s390_virtio_bridge_info);
530 type_init(s390_virtio_register_types)