]> git.proxmox.com Git - qemu.git/blob - hw/s390-virtio-bus.c
Merge remote-tracking branch 'stefanha/tracing' into staging
[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.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"
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
46 #define VIRTIO_EXT_CODE 0x2603
47
48 struct BusInfo s390_virtio_bus_info = {
49 .name = "s390-virtio",
50 .size = sizeof(VirtIOS390Bus),
51 };
52
53 static const VirtIOBindings virtio_s390_bindings;
54
55 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
56
57 /* length of VirtIO device pages */
58 const target_phys_addr_t virtio_size = S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
59
60 static void s390_virtio_bus_reset(void *opaque)
61 {
62 VirtIOS390Bus *bus = opaque;
63 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
64 }
65
66 void 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
83 VirtIOS390Bus *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
102 /* Enable hotplugging */
103 _bus->allow_hotplug = 1;
104
105 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
106 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
107
108 qemu_register_reset(s390_virtio_bus_reset, bus);
109 return bus;
110 }
111
112 static void s390_virtio_irq(CPUS390XState *env, int config_change, uint64_t token)
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
121 static 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);
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);
145 }
146
147 return 0;
148 }
149
150 static int s390_virtio_net_init(VirtIOS390Device *dev)
151 {
152 VirtIODevice *vdev;
153
154 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
155 if (!vdev) {
156 return -1;
157 }
158
159 return s390_virtio_device_init(dev, vdev);
160 }
161
162 static int s390_virtio_blk_init(VirtIOS390Device *dev)
163 {
164 VirtIODevice *vdev;
165
166 vdev = virtio_blk_init((DeviceState *)dev, &dev->block,
167 &dev->block_serial);
168 if (!vdev) {
169 return -1;
170 }
171
172 return s390_virtio_device_init(dev, vdev);
173 }
174
175 static int s390_virtio_serial_init(VirtIOS390Device *dev)
176 {
177 VirtIOS390Bus *bus;
178 VirtIODevice *vdev;
179 int r;
180
181 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
182
183 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
184 if (!vdev) {
185 return -1;
186 }
187
188 r = s390_virtio_device_init(dev, vdev);
189 if (!r) {
190 bus->console = dev;
191 }
192
193 return r;
194 }
195
196 static int s390_virtio_scsi_init(VirtIOS390Device *dev)
197 {
198 VirtIODevice *vdev;
199
200 vdev = virtio_scsi_init((DeviceState *)dev, &dev->scsi);
201 if (!vdev) {
202 return -1;
203 }
204
205 return s390_virtio_device_init(dev, vdev);
206 }
207
208 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
209 {
210 ram_addr_t token_off;
211
212 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
213 (vq * VIRTIO_VQCONFIG_LEN) +
214 VIRTIO_VQCONFIG_OFFS_TOKEN;
215
216 return ldq_be_phys(token_off);
217 }
218
219 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
220 {
221 VirtIODevice *vdev = dev->vdev;
222 int num_vq;
223
224 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
225 if (!virtio_queue_get_num(vdev, num_vq)) {
226 break;
227 }
228 }
229
230 return num_vq;
231 }
232
233 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
234 {
235 ram_addr_t r = bus->next_ring;
236
237 bus->next_ring += VIRTIO_RING_LEN;
238 return r;
239 }
240
241 void s390_virtio_device_sync(VirtIOS390Device *dev)
242 {
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;
247
248 virtio_reset(dev->vdev);
249
250 /* Sync dev space */
251 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
252
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);
255
256 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
257
258 num_vq = s390_virtio_device_num_vq(dev);
259 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
260
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;
266
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));
272 }
273
274 cur_offs = dev->dev_offs;
275 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
276 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
277
278 /* Sync feature bitmap */
279 stl_le_phys(cur_offs, dev->host_features);
280
281 dev->feat_offs = cur_offs + dev->feat_len;
282 cur_offs += dev->feat_len * 2;
283
284 /* Sync config space */
285 if (dev->vdev->get_config) {
286 dev->vdev->get_config(dev->vdev, dev->vdev->config);
287 }
288
289 cpu_physical_memory_write(cur_offs,
290 dev->vdev->config, dev->vdev->config_len);
291 cur_offs += dev->vdev->config_len;
292 }
293
294 void s390_virtio_device_update_status(VirtIOS390Device *dev)
295 {
296 VirtIODevice *vdev = dev->vdev;
297 uint32_t features;
298
299 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
300
301 /* Update guest supported feature bitmap */
302
303 features = bswap32(ldl_be_phys(dev->feat_offs));
304 virtio_set_features(vdev, features);
305 }
306
307 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
308 {
309 return bus->console;
310 }
311
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)
316 {
317 VirtIOS390Device *_dev;
318 DeviceState *dev;
319 int i;
320
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;
329 }
330 return _dev;
331 }
332 }
333 }
334
335 return NULL;
336 }
337
338 /* Find a device by device descriptor location */
339 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
340 {
341 VirtIOS390Device *_dev;
342 DeviceState *dev;
343
344 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
345 _dev = (VirtIOS390Device *)dev;
346 if (_dev->dev_offs == mem) {
347 return _dev;
348 }
349 }
350
351 return NULL;
352 }
353
354 static void virtio_s390_notify(void *opaque, uint16_t vector)
355 {
356 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
357 uint64_t token = s390_virtio_device_vq_token(dev, vector);
358 CPUS390XState *env = s390_cpu_addr2state(0);
359
360 s390_virtio_irq(env, 0, token);
361 }
362
363 static unsigned virtio_s390_get_features(void *opaque)
364 {
365 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
366 return dev->host_features;
367 }
368
369 /**************** S390 Virtio Bus Device Descriptions *******************/
370
371 static const VirtIOBindings virtio_s390_bindings = {
372 .notify = virtio_s390_notify,
373 .get_features = virtio_s390_get_features,
374 };
375
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(),
384 };
385
386 static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
387 {
388 DeviceClass *dc = DEVICE_CLASS(klass);
389 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
390
391 k->init = s390_virtio_net_init;
392 dc->props = s390_virtio_net_properties;
393 }
394
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,
400 };
401
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(),
406 };
407
408 static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
409 {
410 DeviceClass *dc = DEVICE_CLASS(klass);
411 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
412
413 k->init = s390_virtio_blk_init;
414 dc->props = s390_virtio_blk_properties;
415 }
416
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,
422 };
423
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(),
428 };
429
430 static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
431 {
432 DeviceClass *dc = DEVICE_CLASS(klass);
433 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
434
435 k->init = s390_virtio_serial_init;
436 dc->props = s390_virtio_serial_properties;
437 }
438
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,
444 };
445
446 static int s390_virtio_busdev_init(DeviceState *dev)
447 {
448 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
449 VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev);
450
451 return _info->init(_dev);
452 }
453
454 static void virtio_s390_device_class_init(ObjectClass *klass, void *data)
455 {
456 DeviceClass *dc = DEVICE_CLASS(klass);
457
458 dc->init = s390_virtio_busdev_init;
459 dc->bus_info = &s390_virtio_bus_info;
460 dc->unplug = qdev_simple_unplug_cb;
461 }
462
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,
470 };
471
472 static Property s390_virtio_scsi_properties[] = {
473 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOS390Device, host_features, scsi),
474 DEFINE_PROP_END_OF_LIST(),
475 };
476
477 static void s390_virtio_scsi_class_init(ObjectClass *klass, void *data)
478 {
479 DeviceClass *dc = DEVICE_CLASS(klass);
480 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
481
482 k->init = s390_virtio_scsi_init;
483 dc->props = s390_virtio_scsi_properties;
484 }
485
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,
491 };
492
493 /***************** S390 Virtio Bus Bridge Device *******************/
494 /* Only required to have the virtio bus as child in the system bus */
495
496 static int s390_virtio_bridge_init(SysBusDevice *dev)
497 {
498 /* nothing */
499 return 0;
500 }
501
502 static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data)
503 {
504 DeviceClass *dc = DEVICE_CLASS(klass);
505 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
506
507 k->init = s390_virtio_bridge_init;
508 dc->no_user = 1;
509 }
510
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,
516 };
517
518 static void s390_virtio_register_types(void)
519 {
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);
526 }
527
528 type_init(s390_virtio_register_types)