]> git.proxmox.com Git - mirror_qemu.git/blame - hw/s390-virtio-bus.c
pseries: Use the same interrupt swizzling for host bridges as p2p bridges
[mirror_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
f3304eea
AG
60VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
61{
62 VirtIOS390Bus *bus;
63 BusState *_bus;
64 DeviceState *dev;
65
66 /* Create bridge device */
67 dev = qdev_create(NULL, "s390-virtio-bridge");
68 qdev_init_nofail(dev);
69
70 /* Create bus on bridge device */
71
72 _bus = qbus_create(&s390_virtio_bus_info, dev, "s390-virtio");
73 bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
74
75 bus->dev_page = *ram_size;
76 bus->dev_offs = bus->dev_page;
77 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
78
7fa41e53
AG
79 /* Enable hotplugging */
80 _bus->allow_hotplug = 1;
81
f3304eea
AG
82 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
83 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
84
85 return bus;
86}
87
0e4213a7 88static void s390_virtio_irq(CPUS390XState *env, int config_change, uint64_t token)
7fa41e53
AG
89{
90 if (kvm_enabled()) {
91 kvm_s390_virtio_irq(env, config_change, token);
92 } else {
93 cpu_inject_ext(env, VIRTIO_EXT_CODE, config_change, token);
94 }
95}
96
f3304eea
AG
97static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
98{
99 VirtIOS390Bus *bus;
100 int dev_len;
101
102 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
103 dev->vdev = vdev;
104 dev->dev_offs = bus->dev_offs;
105 dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */
106
107 dev_len = VIRTIO_DEV_OFFS_CONFIG;
108 dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN;
109 dev_len += dev->feat_len * 2;
110 dev_len += vdev->config_len;
111
112 bus->dev_offs += dev_len;
113
114 virtio_bind_device(vdev, &virtio_s390_bindings, dev);
8172539d 115 dev->host_features = vdev->get_features(vdev, dev->host_features);
f3304eea
AG
116 s390_virtio_device_sync(dev);
117
7fa41e53 118 if (dev->qdev.hotplugged) {
0e4213a7 119 CPUS390XState *env = s390_cpu_addr2state(0);
7fa41e53
AG
120 s390_virtio_irq(env, VIRTIO_PARAM_DEV_ADD, dev->dev_offs);
121 }
122
f3304eea
AG
123 return 0;
124}
125
126static int s390_virtio_net_init(VirtIOS390Device *dev)
127{
128 VirtIODevice *vdev;
129
f0c07c7c 130 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
f3304eea
AG
131 if (!vdev) {
132 return -1;
133 }
134
135 return s390_virtio_device_init(dev, vdev);
136}
137
138static int s390_virtio_blk_init(VirtIOS390Device *dev)
139{
140 VirtIODevice *vdev;
141
a8686a9b
MA
142 vdev = virtio_blk_init((DeviceState *)dev, &dev->block,
143 &dev->block_serial);
f3304eea
AG
144 if (!vdev) {
145 return -1;
146 }
147
148 return s390_virtio_device_init(dev, vdev);
149}
150
98b19252 151static int s390_virtio_serial_init(VirtIOS390Device *dev)
f3304eea
AG
152{
153 VirtIOS390Bus *bus;
154 VirtIODevice *vdev;
155 int r;
156
157 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
158
6be9b414 159 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
f3304eea
AG
160 if (!vdev) {
161 return -1;
162 }
163
164 r = s390_virtio_device_init(dev, vdev);
165 if (!r) {
166 bus->console = dev;
167 }
168
169 return r;
170}
171
973abc7f
SH
172static int s390_virtio_scsi_init(VirtIOS390Device *dev)
173{
174 VirtIODevice *vdev;
175
176 vdev = virtio_scsi_init((DeviceState *)dev, &dev->scsi);
177 if (!vdev) {
178 return -1;
179 }
180
181 return s390_virtio_device_init(dev, vdev);
182}
183
f3304eea
AG
184static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
185{
186 ram_addr_t token_off;
187
188 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
189 (vq * VIRTIO_VQCONFIG_LEN) +
190 VIRTIO_VQCONFIG_OFFS_TOKEN;
191
04bc74ed 192 return ldq_be_phys(token_off);
f3304eea
AG
193}
194
195static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
196{
197 VirtIODevice *vdev = dev->vdev;
198 int num_vq;
199
200 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
201 if (!virtio_queue_get_num(vdev, num_vq)) {
202 break;
203 }
204 }
205
206 return num_vq;
207}
208
209static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
210{
211 ram_addr_t r = bus->next_ring;
212
213 bus->next_ring += VIRTIO_RING_LEN;
214 return r;
215}
216
baf0b55a 217void s390_virtio_device_sync(VirtIOS390Device *dev)
f3304eea
AG
218{
219 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
220 ram_addr_t cur_offs;
221 uint8_t num_vq;
222 int i;
223
224 virtio_reset(dev->vdev);
225
226 /* Sync dev space */
227 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
228
229 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
230 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
231
232 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
233
234 num_vq = s390_virtio_device_num_vq(dev);
235 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
236
237 /* Sync virtqueues */
238 for (i = 0; i < num_vq; i++) {
239 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
240 (i * VIRTIO_VQCONFIG_LEN);
241 ram_addr_t vring;
242
243 vring = s390_virtio_next_ring(bus);
244 virtio_queue_set_addr(dev->vdev, i, vring);
245 virtio_queue_set_vector(dev->vdev, i, i);
04bc74ed
AG
246 stq_be_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
247 stw_be_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
f3304eea
AG
248 }
249
250 cur_offs = dev->dev_offs;
251 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
252 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
253
254 /* Sync feature bitmap */
04bc74ed 255 stl_le_phys(cur_offs, dev->host_features);
f3304eea
AG
256
257 dev->feat_offs = cur_offs + dev->feat_len;
258 cur_offs += dev->feat_len * 2;
259
260 /* Sync config space */
261 if (dev->vdev->get_config) {
262 dev->vdev->get_config(dev->vdev, dev->vdev->config);
263 }
264
54f7b4a3
SW
265 cpu_physical_memory_write(cur_offs,
266 dev->vdev->config, dev->vdev->config_len);
f3304eea
AG
267 cur_offs += dev->vdev->config_len;
268}
269
270void s390_virtio_device_update_status(VirtIOS390Device *dev)
271{
272 VirtIODevice *vdev = dev->vdev;
273 uint32_t features;
274
3e607cb5 275 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
f3304eea
AG
276
277 /* Update guest supported feature bitmap */
278
04bc74ed 279 features = bswap32(ldl_be_phys(dev->feat_offs));
ad0c9332 280 virtio_set_features(vdev, features);
f3304eea
AG
281}
282
283VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
284{
285 return bus->console;
286}
287
288/* Find a device by vring address */
289VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
290 ram_addr_t mem,
291 int *vq_num)
292{
293 VirtIOS390Device *_dev;
294 DeviceState *dev;
295 int i;
296
d8bb00d6 297 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
f3304eea
AG
298 _dev = (VirtIOS390Device *)dev;
299 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
300 if (!virtio_queue_get_addr(_dev->vdev, i))
301 break;
302 if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
303 if (vq_num) {
304 *vq_num = i;
305 }
306 return _dev;
307 }
308 }
309 }
310
311 return NULL;
312}
313
314/* Find a device by device descriptor location */
315VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
316{
317 VirtIOS390Device *_dev;
318 DeviceState *dev;
319
d8bb00d6 320 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
f3304eea
AG
321 _dev = (VirtIOS390Device *)dev;
322 if (_dev->dev_offs == mem) {
323 return _dev;
324 }
325 }
326
327 return NULL;
328}
329
330static void virtio_s390_notify(void *opaque, uint16_t vector)
331{
332 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
333 uint64_t token = s390_virtio_device_vq_token(dev, vector);
0e4213a7 334 CPUS390XState *env = s390_cpu_addr2state(0);
f3304eea 335
7fa41e53 336 s390_virtio_irq(env, 0, token);
f3304eea
AG
337}
338
8172539d
MT
339static unsigned virtio_s390_get_features(void *opaque)
340{
341 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
342 return dev->host_features;
343}
344
f3304eea
AG
345/**************** S390 Virtio Bus Device Descriptions *******************/
346
347static const VirtIOBindings virtio_s390_bindings = {
348 .notify = virtio_s390_notify,
8172539d 349 .get_features = virtio_s390_get_features,
f3304eea
AG
350};
351
39bffca2
AL
352static Property s390_virtio_net_properties[] = {
353 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
354 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
355 net.txtimer, TX_TIMER_INTERVAL),
356 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
357 net.txburst, TX_BURST),
358 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
359 DEFINE_PROP_END_OF_LIST(),
360};
361
19b6914a
AL
362static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
363{
39bffca2
AL
364 DeviceClass *dc = DEVICE_CLASS(klass);
365 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
19b6914a 366
39bffca2
AL
367 k->init = s390_virtio_net_init;
368 dc->props = s390_virtio_net_properties;
19b6914a
AL
369}
370
39bffca2
AL
371static TypeInfo s390_virtio_net = {
372 .name = "virtio-net-s390",
373 .parent = TYPE_VIRTIO_S390_DEVICE,
374 .instance_size = sizeof(VirtIOS390Device),
375 .class_init = s390_virtio_net_class_init,
376};
377
378static Property s390_virtio_blk_properties[] = {
379 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, block),
380 DEFINE_PROP_STRING("serial", VirtIOS390Device, block_serial),
381 DEFINE_PROP_END_OF_LIST(),
f3304eea
AG
382};
383
19b6914a
AL
384static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
385{
39bffca2
AL
386 DeviceClass *dc = DEVICE_CLASS(klass);
387 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
19b6914a 388
39bffca2
AL
389 k->init = s390_virtio_blk_init;
390 dc->props = s390_virtio_blk_properties;
19b6914a
AL
391}
392
39bffca2
AL
393static TypeInfo s390_virtio_blk = {
394 .name = "virtio-blk-s390",
395 .parent = TYPE_VIRTIO_S390_DEVICE,
396 .instance_size = sizeof(VirtIOS390Device),
397 .class_init = s390_virtio_blk_class_init,
398};
399
400static Property s390_virtio_serial_properties[] = {
401 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
402 serial.max_virtserial_ports, 31),
403 DEFINE_PROP_END_OF_LIST(),
f3304eea
AG
404};
405
19b6914a
AL
406static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
407{
39bffca2
AL
408 DeviceClass *dc = DEVICE_CLASS(klass);
409 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
19b6914a 410
39bffca2
AL
411 k->init = s390_virtio_serial_init;
412 dc->props = s390_virtio_serial_properties;
19b6914a
AL
413}
414
39bffca2
AL
415static TypeInfo s390_virtio_serial = {
416 .name = "virtio-serial-s390",
417 .parent = TYPE_VIRTIO_S390_DEVICE,
418 .instance_size = sizeof(VirtIOS390Device),
419 .class_init = s390_virtio_serial_class_init,
f3304eea
AG
420};
421
d307af79 422static int s390_virtio_busdev_init(DeviceState *dev)
f3304eea 423{
f3304eea 424 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
19b6914a 425 VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev);
f3304eea
AG
426
427 return _info->init(_dev);
428}
429
39bffca2 430static void virtio_s390_device_class_init(ObjectClass *klass, void *data)
f3304eea 431{
39bffca2 432 DeviceClass *dc = DEVICE_CLASS(klass);
f3304eea 433
39bffca2
AL
434 dc->init = s390_virtio_busdev_init;
435 dc->bus_info = &s390_virtio_bus_info;
436 dc->unplug = qdev_simple_unplug_cb;
f3304eea
AG
437}
438
19b6914a
AL
439static TypeInfo virtio_s390_device_info = {
440 .name = TYPE_VIRTIO_S390_DEVICE,
441 .parent = TYPE_DEVICE,
442 .instance_size = sizeof(VirtIOS390Device),
39bffca2 443 .class_init = virtio_s390_device_class_init,
e87f7fc6 444 .class_size = sizeof(VirtIOS390DeviceClass),
19b6914a
AL
445 .abstract = true,
446};
447
973abc7f
SH
448static Property s390_virtio_scsi_properties[] = {
449 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOS390Device, host_features, scsi),
450 DEFINE_PROP_END_OF_LIST(),
451};
452
453static void s390_virtio_scsi_class_init(ObjectClass *klass, void *data)
454{
455 DeviceClass *dc = DEVICE_CLASS(klass);
456 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
457
458 k->init = s390_virtio_scsi_init;
459 dc->props = s390_virtio_scsi_properties;
460}
461
462static TypeInfo s390_virtio_scsi = {
463 .name = "virtio-scsi-s390",
464 .parent = TYPE_VIRTIO_S390_DEVICE,
465 .instance_size = sizeof(VirtIOS390Device),
466 .class_init = s390_virtio_scsi_class_init,
467};
f3304eea
AG
468
469/***************** S390 Virtio Bus Bridge Device *******************/
470/* Only required to have the virtio bus as child in the system bus */
471
472static int s390_virtio_bridge_init(SysBusDevice *dev)
473{
474 /* nothing */
475 return 0;
476}
477
999e12bb
AL
478static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data)
479{
39bffca2 480 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
481 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
482
483 k->init = s390_virtio_bridge_init;
39bffca2 484 dc->no_user = 1;
999e12bb
AL
485}
486
39bffca2
AL
487static TypeInfo s390_virtio_bridge_info = {
488 .name = "s390-virtio-bridge",
489 .parent = TYPE_SYS_BUS_DEVICE,
490 .instance_size = sizeof(SysBusDevice),
491 .class_init = s390_virtio_bridge_class_init,
f3304eea
AG
492};
493
83f7d43a 494static void s390_virtio_register_types(void)
f3304eea 495{
83f7d43a
AF
496 type_register_static(&virtio_s390_device_info);
497 type_register_static(&s390_virtio_serial);
498 type_register_static(&s390_virtio_blk);
499 type_register_static(&s390_virtio_net);
973abc7f 500 type_register_static(&s390_virtio_scsi);
39bffca2 501 type_register_static(&s390_virtio_bridge_info);
f3304eea
AG
502}
503
83f7d43a 504type_init(s390_virtio_register_types)