]> git.proxmox.com Git - qemu.git/blame - hw/s390-virtio-bus.c
msi: Guard msi/msix_write_config with msi_present
[qemu.git] / hw / s390-virtio-bus.c
CommitLineData
f3304eea
AG
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"
98b19252 29#include "hw/virtio-serial.h"
f0c07c7c 30#include "hw/virtio-net.h"
f3304eea
AG
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
8103b4d1
AG
46#define VIRTIO_EXT_CODE 0x2603
47
f3304eea
AG
48struct BusInfo s390_virtio_bus_info = {
49 .name = "s390-virtio",
50 .size = sizeof(VirtIOS390Bus),
51};
52
f3304eea
AG
53static const VirtIOBindings virtio_s390_bindings;
54
55static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
f3304eea 56
d1ff903c
AG
57/* length of VirtIO device pages */
58const target_phys_addr_t virtio_size = S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
59
eb3caa44
JF
60static void s390_virtio_bus_reset(void *opaque)
61{
62 VirtIOS390Bus *bus = opaque;
63 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
64}
65
4170aea1
JF
66void s390_virtio_reset_idx(VirtIOS390Device *dev)
67{
68 int i;
69 target_phys_addr_t idx_addr;
70 uint8_t num_vq;
71
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);
80 }
81}
82
f3304eea
AG
83VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
84{
85 VirtIOS390Bus *bus;
86 BusState *_bus;
87 DeviceState *dev;
88
89 /* Create bridge device */
90 dev = qdev_create(NULL, "s390-virtio-bridge");
91 qdev_init_nofail(dev);
92
93 /* Create bus on bridge device */
94
95 _bus = qbus_create(&s390_virtio_bus_info, dev, "s390-virtio");
96 bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
97
98 bus->dev_page = *ram_size;
99 bus->dev_offs = bus->dev_page;
100 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
101
7fa41e53
AG
102 /* Enable hotplugging */
103 _bus->allow_hotplug = 1;
104
f3304eea
AG
105 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
106 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
107
eb3caa44 108 qemu_register_reset(s390_virtio_bus_reset, bus);
f3304eea
AG
109 return bus;
110}
111
0e4213a7 112static void s390_virtio_irq(CPUS390XState *env, int config_change, uint64_t token)
7fa41e53
AG
113{
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);
118 }
119}
120
f3304eea
AG
121static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
122{
123 VirtIOS390Bus *bus;
124 int dev_len;
125
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 */
130
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;
135
136 bus->dev_offs += dev_len;
137
138 virtio_bind_device(vdev, &virtio_s390_bindings, dev);
8172539d 139 dev->host_features = vdev->get_features(vdev, dev->host_features);
f3304eea 140 s390_virtio_device_sync(dev);
4170aea1 141 s390_virtio_reset_idx(dev);
7fa41e53 142 if (dev->qdev.hotplugged) {
0e4213a7 143 CPUS390XState *env = s390_cpu_addr2state(0);
7fa41e53
AG
144 s390_virtio_irq(env, VIRTIO_PARAM_DEV_ADD, dev->dev_offs);
145 }
146
f3304eea
AG
147 return 0;
148}
149
150static int s390_virtio_net_init(VirtIOS390Device *dev)
151{
152 VirtIODevice *vdev;
153
f0c07c7c 154 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
f3304eea
AG
155 if (!vdev) {
156 return -1;
157 }
158
159 return s390_virtio_device_init(dev, vdev);
160}
161
162static int s390_virtio_blk_init(VirtIOS390Device *dev)
163{
164 VirtIODevice *vdev;
165
12c5674b 166 vdev = virtio_blk_init((DeviceState *)dev, &dev->blk);
f3304eea
AG
167 if (!vdev) {
168 return -1;
169 }
170
171 return s390_virtio_device_init(dev, vdev);
172}
173
98b19252 174static int s390_virtio_serial_init(VirtIOS390Device *dev)
f3304eea
AG
175{
176 VirtIOS390Bus *bus;
177 VirtIODevice *vdev;
178 int r;
179
180 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
181
6be9b414 182 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
f3304eea
AG
183 if (!vdev) {
184 return -1;
185 }
186
187 r = s390_virtio_device_init(dev, vdev);
188 if (!r) {
189 bus->console = dev;
190 }
191
192 return r;
193}
194
973abc7f
SH
195static int s390_virtio_scsi_init(VirtIOS390Device *dev)
196{
197 VirtIODevice *vdev;
198
199 vdev = virtio_scsi_init((DeviceState *)dev, &dev->scsi);
200 if (!vdev) {
201 return -1;
202 }
203
204 return s390_virtio_device_init(dev, vdev);
205}
206
f3304eea
AG
207static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
208{
209 ram_addr_t token_off;
210
211 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
212 (vq * VIRTIO_VQCONFIG_LEN) +
213 VIRTIO_VQCONFIG_OFFS_TOKEN;
214
04bc74ed 215 return ldq_be_phys(token_off);
f3304eea
AG
216}
217
218static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
219{
220 VirtIODevice *vdev = dev->vdev;
221 int num_vq;
222
223 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
224 if (!virtio_queue_get_num(vdev, num_vq)) {
225 break;
226 }
227 }
228
229 return num_vq;
230}
231
232static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
233{
234 ram_addr_t r = bus->next_ring;
235
236 bus->next_ring += VIRTIO_RING_LEN;
237 return r;
238}
239
baf0b55a 240void s390_virtio_device_sync(VirtIOS390Device *dev)
f3304eea
AG
241{
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;
246
247 virtio_reset(dev->vdev);
248
249 /* Sync dev space */
250 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
251
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);
254
255 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
256
257 num_vq = s390_virtio_device_num_vq(dev);
258 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
259
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;
265
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);
04bc74ed
AG
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));
f3304eea
AG
271 }
272
273 cur_offs = dev->dev_offs;
274 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
275 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
276
277 /* Sync feature bitmap */
04bc74ed 278 stl_le_phys(cur_offs, dev->host_features);
f3304eea
AG
279
280 dev->feat_offs = cur_offs + dev->feat_len;
281 cur_offs += dev->feat_len * 2;
282
283 /* Sync config space */
284 if (dev->vdev->get_config) {
285 dev->vdev->get_config(dev->vdev, dev->vdev->config);
286 }
287
54f7b4a3
SW
288 cpu_physical_memory_write(cur_offs,
289 dev->vdev->config, dev->vdev->config_len);
f3304eea
AG
290 cur_offs += dev->vdev->config_len;
291}
292
293void s390_virtio_device_update_status(VirtIOS390Device *dev)
294{
295 VirtIODevice *vdev = dev->vdev;
296 uint32_t features;
297
3e607cb5 298 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
f3304eea
AG
299
300 /* Update guest supported feature bitmap */
301
04bc74ed 302 features = bswap32(ldl_be_phys(dev->feat_offs));
ad0c9332 303 virtio_set_features(vdev, features);
f3304eea
AG
304}
305
306VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
307{
308 return bus->console;
309}
310
311/* Find a device by vring address */
312VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
313 ram_addr_t mem,
314 int *vq_num)
315{
316 VirtIOS390Device *_dev;
317 DeviceState *dev;
318 int i;
319
d8bb00d6 320 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
f3304eea
AG
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;
328 }
329 return _dev;
330 }
331 }
332 }
333
334 return NULL;
335}
336
337/* Find a device by device descriptor location */
338VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
339{
340 VirtIOS390Device *_dev;
341 DeviceState *dev;
342
d8bb00d6 343 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
f3304eea
AG
344 _dev = (VirtIOS390Device *)dev;
345 if (_dev->dev_offs == mem) {
346 return _dev;
347 }
348 }
349
350 return NULL;
351}
352
353static void virtio_s390_notify(void *opaque, uint16_t vector)
354{
355 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
356 uint64_t token = s390_virtio_device_vq_token(dev, vector);
0e4213a7 357 CPUS390XState *env = s390_cpu_addr2state(0);
f3304eea 358
7fa41e53 359 s390_virtio_irq(env, 0, token);
f3304eea
AG
360}
361
8172539d
MT
362static unsigned virtio_s390_get_features(void *opaque)
363{
364 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
365 return dev->host_features;
366}
367
f3304eea
AG
368/**************** S390 Virtio Bus Device Descriptions *******************/
369
370static const VirtIOBindings virtio_s390_bindings = {
371 .notify = virtio_s390_notify,
8172539d 372 .get_features = virtio_s390_get_features,
f3304eea
AG
373};
374
39bffca2
AL
375static 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(),
383};
384
19b6914a
AL
385static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
386{
39bffca2
AL
387 DeviceClass *dc = DEVICE_CLASS(klass);
388 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
19b6914a 389
39bffca2
AL
390 k->init = s390_virtio_net_init;
391 dc->props = s390_virtio_net_properties;
19b6914a
AL
392}
393
39bffca2
AL
394static 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,
399};
400
401static Property s390_virtio_blk_properties[] = {
12c5674b
PB
402 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, blk.conf),
403 DEFINE_PROP_STRING("serial", VirtIOS390Device, blk.serial),
a6c5c84a
PB
404#ifdef __linux__
405 DEFINE_PROP_BIT("scsi", VirtIOS390Device, blk.scsi, 0, true),
406#endif
39bffca2 407 DEFINE_PROP_END_OF_LIST(),
f3304eea
AG
408};
409
19b6914a
AL
410static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
411{
39bffca2
AL
412 DeviceClass *dc = DEVICE_CLASS(klass);
413 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
19b6914a 414
39bffca2
AL
415 k->init = s390_virtio_blk_init;
416 dc->props = s390_virtio_blk_properties;
19b6914a
AL
417}
418
39bffca2
AL
419static 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,
424};
425
426static Property s390_virtio_serial_properties[] = {
427 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
428 serial.max_virtserial_ports, 31),
429 DEFINE_PROP_END_OF_LIST(),
f3304eea
AG
430};
431
19b6914a
AL
432static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
433{
39bffca2
AL
434 DeviceClass *dc = DEVICE_CLASS(klass);
435 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
19b6914a 436
39bffca2
AL
437 k->init = s390_virtio_serial_init;
438 dc->props = s390_virtio_serial_properties;
19b6914a
AL
439}
440
39bffca2
AL
441static 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,
f3304eea
AG
446};
447
d307af79 448static int s390_virtio_busdev_init(DeviceState *dev)
f3304eea 449{
f3304eea 450 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
19b6914a 451 VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev);
f3304eea
AG
452
453 return _info->init(_dev);
454}
455
39bffca2 456static void virtio_s390_device_class_init(ObjectClass *klass, void *data)
f3304eea 457{
39bffca2 458 DeviceClass *dc = DEVICE_CLASS(klass);
f3304eea 459
39bffca2
AL
460 dc->init = s390_virtio_busdev_init;
461 dc->bus_info = &s390_virtio_bus_info;
462 dc->unplug = qdev_simple_unplug_cb;
f3304eea
AG
463}
464
19b6914a
AL
465static TypeInfo virtio_s390_device_info = {
466 .name = TYPE_VIRTIO_S390_DEVICE,
467 .parent = TYPE_DEVICE,
468 .instance_size = sizeof(VirtIOS390Device),
39bffca2 469 .class_init = virtio_s390_device_class_init,
e87f7fc6 470 .class_size = sizeof(VirtIOS390DeviceClass),
19b6914a
AL
471 .abstract = true,
472};
473
973abc7f
SH
474static Property s390_virtio_scsi_properties[] = {
475 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOS390Device, host_features, scsi),
476 DEFINE_PROP_END_OF_LIST(),
477};
478
479static void s390_virtio_scsi_class_init(ObjectClass *klass, void *data)
480{
481 DeviceClass *dc = DEVICE_CLASS(klass);
482 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
483
484 k->init = s390_virtio_scsi_init;
485 dc->props = s390_virtio_scsi_properties;
486}
487
488static 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,
493};
f3304eea
AG
494
495/***************** S390 Virtio Bus Bridge Device *******************/
496/* Only required to have the virtio bus as child in the system bus */
497
498static int s390_virtio_bridge_init(SysBusDevice *dev)
499{
500 /* nothing */
501 return 0;
502}
503
999e12bb
AL
504static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data)
505{
39bffca2 506 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
507 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
508
509 k->init = s390_virtio_bridge_init;
39bffca2 510 dc->no_user = 1;
999e12bb
AL
511}
512
39bffca2
AL
513static 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,
f3304eea
AG
518};
519
83f7d43a 520static void s390_virtio_register_types(void)
f3304eea 521{
83f7d43a
AF
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);
973abc7f 526 type_register_static(&s390_virtio_scsi);
39bffca2 527 type_register_static(&s390_virtio_bridge_info);
f3304eea
AG
528}
529
83f7d43a 530type_init(s390_virtio_register_types)