]> git.proxmox.com Git - qemu.git/blob - hw/s390-virtio-bus.c
virtio-s390-bus: add virtio-s390-bus.
[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/block.h"
22 #include "sysemu/sysemu.h"
23 #include "boards.h"
24 #include "monitor/monitor.h"
25 #include "loader.h"
26 #include "elf.h"
27 #include "hw/virtio.h"
28 #include "hw/virtio-rng.h"
29 #include "hw/virtio-serial.h"
30 #include "hw/virtio-net.h"
31 #include "hw/sysbus.h"
32 #include "sysemu/kvm.h"
33
34 #include "hw/s390-virtio-bus.h"
35 #include "hw/virtio-bus.h"
36
37 /* #define DEBUG_S390 */
38
39 #ifdef DEBUG_S390
40 #define dprintf(fmt, ...) \
41 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
42 #else
43 #define dprintf(fmt, ...) \
44 do { } while (0)
45 #endif
46
47 #define VIRTIO_EXT_CODE 0x2603
48
49 static const TypeInfo s390_virtio_bus_info = {
50 .name = TYPE_S390_VIRTIO_BUS,
51 .parent = TYPE_BUS,
52 .instance_size = sizeof(VirtIOS390Bus),
53 };
54
55 static const VirtIOBindings virtio_s390_bindings;
56
57 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
58
59 /* length of VirtIO device pages */
60 const hwaddr virtio_size = S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
61
62 static void s390_virtio_bus_reset(void *opaque)
63 {
64 VirtIOS390Bus *bus = opaque;
65 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
66 }
67
68 void s390_virtio_reset_idx(VirtIOS390Device *dev)
69 {
70 int i;
71 hwaddr idx_addr;
72 uint8_t num_vq;
73
74 num_vq = s390_virtio_device_num_vq(dev);
75 for (i = 0; i < num_vq; i++) {
76 idx_addr = virtio_queue_get_avail_addr(dev->vdev, i) +
77 VIRTIO_VRING_AVAIL_IDX_OFFS;
78 stw_phys(idx_addr, 0);
79 idx_addr = virtio_queue_get_used_addr(dev->vdev, i) +
80 VIRTIO_VRING_USED_IDX_OFFS;
81 stw_phys(idx_addr, 0);
82 }
83 }
84
85 VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
86 {
87 VirtIOS390Bus *bus;
88 BusState *_bus;
89 DeviceState *dev;
90
91 /* Create bridge device */
92 dev = qdev_create(NULL, "s390-virtio-bridge");
93 qdev_init_nofail(dev);
94
95 /* Create bus on bridge device */
96
97 _bus = qbus_create(TYPE_S390_VIRTIO_BUS, dev, "s390-virtio");
98 bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
99
100 bus->dev_page = *ram_size;
101 bus->dev_offs = bus->dev_page;
102 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
103
104 /* Enable hotplugging */
105 _bus->allow_hotplug = 1;
106
107 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
108 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
109
110 qemu_register_reset(s390_virtio_bus_reset, bus);
111 return bus;
112 }
113
114 static void s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
115 {
116 CPUS390XState *env = &cpu->env;
117
118 if (kvm_enabled()) {
119 kvm_s390_virtio_irq(cpu, config_change, token);
120 } else {
121 cpu_inject_ext(env, VIRTIO_EXT_CODE, config_change, token);
122 }
123 }
124
125 static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
126 {
127 VirtIOS390Bus *bus;
128 int dev_len;
129
130 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
131 dev->vdev = vdev;
132 dev->dev_offs = bus->dev_offs;
133 dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */
134
135 dev_len = VIRTIO_DEV_OFFS_CONFIG;
136 dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN;
137 dev_len += dev->feat_len * 2;
138 dev_len += vdev->config_len;
139
140 bus->dev_offs += dev_len;
141
142 virtio_bind_device(vdev, &virtio_s390_bindings, DEVICE(dev));
143 dev->host_features = vdev->get_features(vdev, dev->host_features);
144 s390_virtio_device_sync(dev);
145 s390_virtio_reset_idx(dev);
146 if (dev->qdev.hotplugged) {
147 S390CPU *cpu = s390_cpu_addr2state(0);
148 s390_virtio_irq(cpu, VIRTIO_PARAM_DEV_ADD, dev->dev_offs);
149 }
150
151 return 0;
152 }
153
154 static int s390_virtio_net_init(VirtIOS390Device *dev)
155 {
156 VirtIODevice *vdev;
157
158 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
159 if (!vdev) {
160 return -1;
161 }
162
163 return s390_virtio_device_init(dev, vdev);
164 }
165
166 static int s390_virtio_blk_init(VirtIOS390Device *dev)
167 {
168 VirtIODevice *vdev;
169
170 vdev = virtio_blk_init((DeviceState *)dev, &dev->blk);
171 if (!vdev) {
172 return -1;
173 }
174
175 return s390_virtio_device_init(dev, vdev);
176 }
177
178 static int s390_virtio_serial_init(VirtIOS390Device *dev)
179 {
180 VirtIOS390Bus *bus;
181 VirtIODevice *vdev;
182 int r;
183
184 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
185
186 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
187 if (!vdev) {
188 return -1;
189 }
190
191 r = s390_virtio_device_init(dev, vdev);
192 if (!r) {
193 bus->console = dev;
194 }
195
196 return r;
197 }
198
199 static int s390_virtio_scsi_init(VirtIOS390Device *dev)
200 {
201 VirtIODevice *vdev;
202
203 vdev = virtio_scsi_init((DeviceState *)dev, &dev->scsi);
204 if (!vdev) {
205 return -1;
206 }
207
208 return s390_virtio_device_init(dev, vdev);
209 }
210
211 static int s390_virtio_rng_init(VirtIOS390Device *dev)
212 {
213 VirtIODevice *vdev;
214
215 vdev = virtio_rng_init((DeviceState *)dev, &dev->rng);
216 if (!vdev) {
217 return -1;
218 }
219
220 return s390_virtio_device_init(dev, vdev);
221 }
222
223 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
224 {
225 ram_addr_t token_off;
226
227 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
228 (vq * VIRTIO_VQCONFIG_LEN) +
229 VIRTIO_VQCONFIG_OFFS_TOKEN;
230
231 return ldq_be_phys(token_off);
232 }
233
234 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
235 {
236 VirtIODevice *vdev = dev->vdev;
237 int num_vq;
238
239 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
240 if (!virtio_queue_get_num(vdev, num_vq)) {
241 break;
242 }
243 }
244
245 return num_vq;
246 }
247
248 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
249 {
250 ram_addr_t r = bus->next_ring;
251
252 bus->next_ring += VIRTIO_RING_LEN;
253 return r;
254 }
255
256 void s390_virtio_device_sync(VirtIOS390Device *dev)
257 {
258 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
259 ram_addr_t cur_offs;
260 uint8_t num_vq;
261 int i;
262
263 virtio_reset(dev->vdev);
264
265 /* Sync dev space */
266 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
267
268 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
269 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
270
271 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
272
273 num_vq = s390_virtio_device_num_vq(dev);
274 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
275
276 /* Sync virtqueues */
277 for (i = 0; i < num_vq; i++) {
278 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
279 (i * VIRTIO_VQCONFIG_LEN);
280 ram_addr_t vring;
281
282 vring = s390_virtio_next_ring(bus);
283 virtio_queue_set_addr(dev->vdev, i, vring);
284 virtio_queue_set_vector(dev->vdev, i, i);
285 stq_be_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
286 stw_be_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
287 }
288
289 cur_offs = dev->dev_offs;
290 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
291 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
292
293 /* Sync feature bitmap */
294 stl_le_phys(cur_offs, dev->host_features);
295
296 dev->feat_offs = cur_offs + dev->feat_len;
297 cur_offs += dev->feat_len * 2;
298
299 /* Sync config space */
300 if (dev->vdev->get_config) {
301 dev->vdev->get_config(dev->vdev, dev->vdev->config);
302 }
303
304 cpu_physical_memory_write(cur_offs,
305 dev->vdev->config, dev->vdev->config_len);
306 cur_offs += dev->vdev->config_len;
307 }
308
309 void s390_virtio_device_update_status(VirtIOS390Device *dev)
310 {
311 VirtIODevice *vdev = dev->vdev;
312 uint32_t features;
313
314 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
315
316 /* Update guest supported feature bitmap */
317
318 features = bswap32(ldl_be_phys(dev->feat_offs));
319 virtio_set_features(vdev, features);
320 }
321
322 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
323 {
324 return bus->console;
325 }
326
327 /* Find a device by vring address */
328 VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
329 ram_addr_t mem,
330 int *vq_num)
331 {
332 BusChild *kid;
333 int i;
334
335 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
336 VirtIOS390Device *dev = (VirtIOS390Device *)kid->child;
337
338 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
339 if (!virtio_queue_get_addr(dev->vdev, i))
340 break;
341 if (virtio_queue_get_addr(dev->vdev, i) == mem) {
342 if (vq_num) {
343 *vq_num = i;
344 }
345 return dev;
346 }
347 }
348 }
349
350 return NULL;
351 }
352
353 /* Find a device by device descriptor location */
354 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
355 {
356 BusChild *kid;
357
358 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
359 VirtIOS390Device *dev = (VirtIOS390Device *)kid->child;
360 if (dev->dev_offs == mem) {
361 return dev;
362 }
363 }
364
365 return NULL;
366 }
367
368 /* DeviceState to VirtIOS390Device. Note: used on datapath,
369 * be careful and test performance if you change this.
370 */
371 static inline VirtIOS390Device *to_virtio_s390_device_fast(DeviceState *d)
372 {
373 return container_of(d, VirtIOS390Device, qdev);
374 }
375
376 /* DeviceState to VirtIOS390Device. TODO: use QOM. */
377 static inline VirtIOS390Device *to_virtio_s390_device(DeviceState *d)
378 {
379 return container_of(d, VirtIOS390Device, qdev);
380 }
381
382 static void virtio_s390_notify(DeviceState *d, uint16_t vector)
383 {
384 VirtIOS390Device *dev = to_virtio_s390_device_fast(d);
385 uint64_t token = s390_virtio_device_vq_token(dev, vector);
386 S390CPU *cpu = s390_cpu_addr2state(0);
387
388 s390_virtio_irq(cpu, 0, token);
389 }
390
391 static unsigned virtio_s390_get_features(DeviceState *d)
392 {
393 VirtIOS390Device *dev = to_virtio_s390_device(d);
394 return dev->host_features;
395 }
396
397 /**************** S390 Virtio Bus Device Descriptions *******************/
398
399 static const VirtIOBindings virtio_s390_bindings = {
400 .notify = virtio_s390_notify,
401 .get_features = virtio_s390_get_features,
402 };
403
404 static Property s390_virtio_net_properties[] = {
405 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
406 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
407 net.txtimer, TX_TIMER_INTERVAL),
408 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
409 net.txburst, TX_BURST),
410 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
411 DEFINE_PROP_END_OF_LIST(),
412 };
413
414 static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
415 {
416 DeviceClass *dc = DEVICE_CLASS(klass);
417 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
418
419 k->init = s390_virtio_net_init;
420 dc->props = s390_virtio_net_properties;
421 }
422
423 static const TypeInfo s390_virtio_net = {
424 .name = "virtio-net-s390",
425 .parent = TYPE_VIRTIO_S390_DEVICE,
426 .instance_size = sizeof(VirtIOS390Device),
427 .class_init = s390_virtio_net_class_init,
428 };
429
430 static Property s390_virtio_blk_properties[] = {
431 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, blk.conf),
432 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOS390Device, blk.conf),
433 DEFINE_PROP_STRING("serial", VirtIOS390Device, blk.serial),
434 #ifdef __linux__
435 DEFINE_PROP_BIT("scsi", VirtIOS390Device, blk.scsi, 0, true),
436 #endif
437 DEFINE_PROP_END_OF_LIST(),
438 };
439
440 static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
441 {
442 DeviceClass *dc = DEVICE_CLASS(klass);
443 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
444
445 k->init = s390_virtio_blk_init;
446 dc->props = s390_virtio_blk_properties;
447 }
448
449 static const TypeInfo s390_virtio_blk = {
450 .name = "virtio-blk-s390",
451 .parent = TYPE_VIRTIO_S390_DEVICE,
452 .instance_size = sizeof(VirtIOS390Device),
453 .class_init = s390_virtio_blk_class_init,
454 };
455
456 static Property s390_virtio_serial_properties[] = {
457 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
458 serial.max_virtserial_ports, 31),
459 DEFINE_PROP_END_OF_LIST(),
460 };
461
462 static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
463 {
464 DeviceClass *dc = DEVICE_CLASS(klass);
465 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
466
467 k->init = s390_virtio_serial_init;
468 dc->props = s390_virtio_serial_properties;
469 }
470
471 static const TypeInfo s390_virtio_serial = {
472 .name = "virtio-serial-s390",
473 .parent = TYPE_VIRTIO_S390_DEVICE,
474 .instance_size = sizeof(VirtIOS390Device),
475 .class_init = s390_virtio_serial_class_init,
476 };
477
478 static void s390_virtio_rng_initfn(Object *obj)
479 {
480 VirtIOS390Device *dev = VIRTIO_S390_DEVICE(obj);
481
482 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
483 (Object **)&dev->rng.rng, NULL);
484 }
485
486 static void s390_virtio_rng_class_init(ObjectClass *klass, void *data)
487 {
488 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
489
490 k->init = s390_virtio_rng_init;
491 }
492
493 static const TypeInfo s390_virtio_rng = {
494 .name = "virtio-rng-s390",
495 .parent = TYPE_VIRTIO_S390_DEVICE,
496 .instance_size = sizeof(VirtIOS390Device),
497 .instance_init = s390_virtio_rng_initfn,
498 .class_init = s390_virtio_rng_class_init,
499 };
500
501 static int s390_virtio_busdev_init(DeviceState *dev)
502 {
503 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
504 VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev);
505
506 return _info->init(_dev);
507 }
508
509 static void virtio_s390_device_class_init(ObjectClass *klass, void *data)
510 {
511 DeviceClass *dc = DEVICE_CLASS(klass);
512
513 dc->init = s390_virtio_busdev_init;
514 dc->bus_type = TYPE_S390_VIRTIO_BUS;
515 dc->unplug = qdev_simple_unplug_cb;
516 }
517
518 static const TypeInfo virtio_s390_device_info = {
519 .name = TYPE_VIRTIO_S390_DEVICE,
520 .parent = TYPE_DEVICE,
521 .instance_size = sizeof(VirtIOS390Device),
522 .class_init = virtio_s390_device_class_init,
523 .class_size = sizeof(VirtIOS390DeviceClass),
524 .abstract = true,
525 };
526
527 static Property s390_virtio_scsi_properties[] = {
528 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOS390Device, host_features, scsi),
529 DEFINE_PROP_END_OF_LIST(),
530 };
531
532 static void s390_virtio_scsi_class_init(ObjectClass *klass, void *data)
533 {
534 DeviceClass *dc = DEVICE_CLASS(klass);
535 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
536
537 k->init = s390_virtio_scsi_init;
538 dc->props = s390_virtio_scsi_properties;
539 }
540
541 static const TypeInfo s390_virtio_scsi = {
542 .name = "virtio-scsi-s390",
543 .parent = TYPE_VIRTIO_S390_DEVICE,
544 .instance_size = sizeof(VirtIOS390Device),
545 .class_init = s390_virtio_scsi_class_init,
546 };
547
548 /***************** S390 Virtio Bus Bridge Device *******************/
549 /* Only required to have the virtio bus as child in the system bus */
550
551 static int s390_virtio_bridge_init(SysBusDevice *dev)
552 {
553 /* nothing */
554 return 0;
555 }
556
557 static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data)
558 {
559 DeviceClass *dc = DEVICE_CLASS(klass);
560 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
561
562 k->init = s390_virtio_bridge_init;
563 dc->no_user = 1;
564 }
565
566 static const TypeInfo s390_virtio_bridge_info = {
567 .name = "s390-virtio-bridge",
568 .parent = TYPE_SYS_BUS_DEVICE,
569 .instance_size = sizeof(SysBusDevice),
570 .class_init = s390_virtio_bridge_class_init,
571 };
572
573 /* virtio-s390-bus */
574
575 void virtio_s390_bus_new(VirtioBusState *bus, VirtIOS390Device *dev)
576 {
577 DeviceState *qdev = DEVICE(dev);
578 BusState *qbus;
579 qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_S390_BUS, qdev, NULL);
580 qbus = BUS(bus);
581 qbus->allow_hotplug = 0;
582 }
583
584 static void virtio_s390_bus_class_init(ObjectClass *klass, void *data)
585 {
586 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
587 BusClass *bus_class = BUS_CLASS(klass);
588 bus_class->max_dev = 1;
589 k->notify = virtio_s390_notify;
590 k->get_features = virtio_s390_get_features;
591 }
592
593 static const TypeInfo virtio_s390_bus_info = {
594 .name = TYPE_VIRTIO_S390_BUS,
595 .parent = TYPE_VIRTIO_BUS,
596 .instance_size = sizeof(VirtioS390BusState),
597 .class_init = virtio_s390_bus_class_init,
598 };
599
600 static void s390_virtio_register_types(void)
601 {
602 type_register_static(&virtio_s390_bus_info);
603 type_register_static(&s390_virtio_bus_info);
604 type_register_static(&virtio_s390_device_info);
605 type_register_static(&s390_virtio_serial);
606 type_register_static(&s390_virtio_blk);
607 type_register_static(&s390_virtio_net);
608 type_register_static(&s390_virtio_scsi);
609 type_register_static(&s390_virtio_rng);
610 type_register_static(&s390_virtio_bridge_info);
611 }
612
613 type_init(s390_virtio_register_types)