virtio-blk: blockdev_mark_auto_del is transport-independent
[qemu/kevin.git] / hw / s390-virtio-bus.c
blob63ccd5c35a37cc04c0910f3ed062b3b09ee8d3d2
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->block,
167 &dev->block_serial);
168 if (!vdev) {
169 return -1;
172 return s390_virtio_device_init(dev, vdev);
175 static int s390_virtio_serial_init(VirtIOS390Device *dev)
177 VirtIOS390Bus *bus;
178 VirtIODevice *vdev;
179 int r;
181 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
183 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
184 if (!vdev) {
185 return -1;
188 r = s390_virtio_device_init(dev, vdev);
189 if (!r) {
190 bus->console = dev;
193 return r;
196 static int s390_virtio_scsi_init(VirtIOS390Device *dev)
198 VirtIODevice *vdev;
200 vdev = virtio_scsi_init((DeviceState *)dev, &dev->scsi);
201 if (!vdev) {
202 return -1;
205 return s390_virtio_device_init(dev, vdev);
208 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
210 ram_addr_t token_off;
212 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
213 (vq * VIRTIO_VQCONFIG_LEN) +
214 VIRTIO_VQCONFIG_OFFS_TOKEN;
216 return ldq_be_phys(token_off);
219 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
221 VirtIODevice *vdev = dev->vdev;
222 int num_vq;
224 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
225 if (!virtio_queue_get_num(vdev, num_vq)) {
226 break;
230 return num_vq;
233 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
235 ram_addr_t r = bus->next_ring;
237 bus->next_ring += VIRTIO_RING_LEN;
238 return r;
241 void s390_virtio_device_sync(VirtIOS390Device *dev)
243 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
244 ram_addr_t cur_offs;
245 uint8_t num_vq;
246 int i;
248 virtio_reset(dev->vdev);
250 /* Sync dev space */
251 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
253 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
254 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
256 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
258 num_vq = s390_virtio_device_num_vq(dev);
259 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
261 /* Sync virtqueues */
262 for (i = 0; i < num_vq; i++) {
263 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
264 (i * VIRTIO_VQCONFIG_LEN);
265 ram_addr_t vring;
267 vring = s390_virtio_next_ring(bus);
268 virtio_queue_set_addr(dev->vdev, i, vring);
269 virtio_queue_set_vector(dev->vdev, i, i);
270 stq_be_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
271 stw_be_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
274 cur_offs = dev->dev_offs;
275 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
276 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
278 /* Sync feature bitmap */
279 stl_le_phys(cur_offs, dev->host_features);
281 dev->feat_offs = cur_offs + dev->feat_len;
282 cur_offs += dev->feat_len * 2;
284 /* Sync config space */
285 if (dev->vdev->get_config) {
286 dev->vdev->get_config(dev->vdev, dev->vdev->config);
289 cpu_physical_memory_write(cur_offs,
290 dev->vdev->config, dev->vdev->config_len);
291 cur_offs += dev->vdev->config_len;
294 void s390_virtio_device_update_status(VirtIOS390Device *dev)
296 VirtIODevice *vdev = dev->vdev;
297 uint32_t features;
299 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
301 /* Update guest supported feature bitmap */
303 features = bswap32(ldl_be_phys(dev->feat_offs));
304 virtio_set_features(vdev, features);
307 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
309 return bus->console;
312 /* Find a device by vring address */
313 VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
314 ram_addr_t mem,
315 int *vq_num)
317 VirtIOS390Device *_dev;
318 DeviceState *dev;
319 int i;
321 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
322 _dev = (VirtIOS390Device *)dev;
323 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
324 if (!virtio_queue_get_addr(_dev->vdev, i))
325 break;
326 if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
327 if (vq_num) {
328 *vq_num = i;
330 return _dev;
335 return NULL;
338 /* Find a device by device descriptor location */
339 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
341 VirtIOS390Device *_dev;
342 DeviceState *dev;
344 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
345 _dev = (VirtIOS390Device *)dev;
346 if (_dev->dev_offs == mem) {
347 return _dev;
351 return NULL;
354 static void virtio_s390_notify(void *opaque, uint16_t vector)
356 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
357 uint64_t token = s390_virtio_device_vq_token(dev, vector);
358 CPUS390XState *env = s390_cpu_addr2state(0);
360 s390_virtio_irq(env, 0, token);
363 static unsigned virtio_s390_get_features(void *opaque)
365 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
366 return dev->host_features;
369 /**************** S390 Virtio Bus Device Descriptions *******************/
371 static const VirtIOBindings virtio_s390_bindings = {
372 .notify = virtio_s390_notify,
373 .get_features = virtio_s390_get_features,
376 static Property s390_virtio_net_properties[] = {
377 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
378 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
379 net.txtimer, TX_TIMER_INTERVAL),
380 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
381 net.txburst, TX_BURST),
382 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
383 DEFINE_PROP_END_OF_LIST(),
386 static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
388 DeviceClass *dc = DEVICE_CLASS(klass);
389 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
391 k->init = s390_virtio_net_init;
392 dc->props = s390_virtio_net_properties;
395 static TypeInfo s390_virtio_net = {
396 .name = "virtio-net-s390",
397 .parent = TYPE_VIRTIO_S390_DEVICE,
398 .instance_size = sizeof(VirtIOS390Device),
399 .class_init = s390_virtio_net_class_init,
402 static Property s390_virtio_blk_properties[] = {
403 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, block),
404 DEFINE_PROP_STRING("serial", VirtIOS390Device, block_serial),
405 DEFINE_PROP_END_OF_LIST(),
408 static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
410 DeviceClass *dc = DEVICE_CLASS(klass);
411 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
413 k->init = s390_virtio_blk_init;
414 dc->props = s390_virtio_blk_properties;
417 static TypeInfo s390_virtio_blk = {
418 .name = "virtio-blk-s390",
419 .parent = TYPE_VIRTIO_S390_DEVICE,
420 .instance_size = sizeof(VirtIOS390Device),
421 .class_init = s390_virtio_blk_class_init,
424 static Property s390_virtio_serial_properties[] = {
425 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
426 serial.max_virtserial_ports, 31),
427 DEFINE_PROP_END_OF_LIST(),
430 static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
432 DeviceClass *dc = DEVICE_CLASS(klass);
433 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
435 k->init = s390_virtio_serial_init;
436 dc->props = s390_virtio_serial_properties;
439 static TypeInfo s390_virtio_serial = {
440 .name = "virtio-serial-s390",
441 .parent = TYPE_VIRTIO_S390_DEVICE,
442 .instance_size = sizeof(VirtIOS390Device),
443 .class_init = s390_virtio_serial_class_init,
446 static int s390_virtio_busdev_init(DeviceState *dev)
448 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
449 VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev);
451 return _info->init(_dev);
454 static void virtio_s390_device_class_init(ObjectClass *klass, void *data)
456 DeviceClass *dc = DEVICE_CLASS(klass);
458 dc->init = s390_virtio_busdev_init;
459 dc->bus_info = &s390_virtio_bus_info;
460 dc->unplug = qdev_simple_unplug_cb;
463 static TypeInfo virtio_s390_device_info = {
464 .name = TYPE_VIRTIO_S390_DEVICE,
465 .parent = TYPE_DEVICE,
466 .instance_size = sizeof(VirtIOS390Device),
467 .class_init = virtio_s390_device_class_init,
468 .class_size = sizeof(VirtIOS390DeviceClass),
469 .abstract = true,
472 static Property s390_virtio_scsi_properties[] = {
473 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOS390Device, host_features, scsi),
474 DEFINE_PROP_END_OF_LIST(),
477 static void s390_virtio_scsi_class_init(ObjectClass *klass, void *data)
479 DeviceClass *dc = DEVICE_CLASS(klass);
480 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
482 k->init = s390_virtio_scsi_init;
483 dc->props = s390_virtio_scsi_properties;
486 static TypeInfo s390_virtio_scsi = {
487 .name = "virtio-scsi-s390",
488 .parent = TYPE_VIRTIO_S390_DEVICE,
489 .instance_size = sizeof(VirtIOS390Device),
490 .class_init = s390_virtio_scsi_class_init,
493 /***************** S390 Virtio Bus Bridge Device *******************/
494 /* Only required to have the virtio bus as child in the system bus */
496 static int s390_virtio_bridge_init(SysBusDevice *dev)
498 /* nothing */
499 return 0;
502 static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data)
504 DeviceClass *dc = DEVICE_CLASS(klass);
505 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
507 k->init = s390_virtio_bridge_init;
508 dc->no_user = 1;
511 static TypeInfo s390_virtio_bridge_info = {
512 .name = "s390-virtio-bridge",
513 .parent = TYPE_SYS_BUS_DEVICE,
514 .instance_size = sizeof(SysBusDevice),
515 .class_init = s390_virtio_bridge_class_init,
518 static void s390_virtio_register_types(void)
520 type_register_static(&virtio_s390_device_info);
521 type_register_static(&s390_virtio_serial);
522 type_register_static(&s390_virtio_blk);
523 type_register_static(&s390_virtio_net);
524 type_register_static(&s390_virtio_scsi);
525 type_register_static(&s390_virtio_bridge_info);
528 type_init(s390_virtio_register_types)