]> git.proxmox.com Git - qemu.git/blob - hw/s390-virtio-bus.c
s390-virtio: Let s390_cpu_addr2state() return S390CPU
[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 S390CPU *cpu = s390_cpu_addr2state(0);
144 CPUS390XState *env = &cpu->env;
145 s390_virtio_irq(env, VIRTIO_PARAM_DEV_ADD, dev->dev_offs);
146 }
147
148 return 0;
149 }
150
151 static int s390_virtio_net_init(VirtIOS390Device *dev)
152 {
153 VirtIODevice *vdev;
154
155 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
156 if (!vdev) {
157 return -1;
158 }
159
160 return s390_virtio_device_init(dev, vdev);
161 }
162
163 static int s390_virtio_blk_init(VirtIOS390Device *dev)
164 {
165 VirtIODevice *vdev;
166
167 vdev = virtio_blk_init((DeviceState *)dev, &dev->blk);
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 S390CPU *cpu = s390_cpu_addr2state(0);
359 CPUS390XState *env = &cpu->env;
360
361 s390_virtio_irq(env, 0, token);
362 }
363
364 static unsigned virtio_s390_get_features(void *opaque)
365 {
366 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
367 return dev->host_features;
368 }
369
370 /**************** S390 Virtio Bus Device Descriptions *******************/
371
372 static const VirtIOBindings virtio_s390_bindings = {
373 .notify = virtio_s390_notify,
374 .get_features = virtio_s390_get_features,
375 };
376
377 static Property s390_virtio_net_properties[] = {
378 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
379 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
380 net.txtimer, TX_TIMER_INTERVAL),
381 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
382 net.txburst, TX_BURST),
383 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
384 DEFINE_PROP_END_OF_LIST(),
385 };
386
387 static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
388 {
389 DeviceClass *dc = DEVICE_CLASS(klass);
390 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
391
392 k->init = s390_virtio_net_init;
393 dc->props = s390_virtio_net_properties;
394 }
395
396 static TypeInfo s390_virtio_net = {
397 .name = "virtio-net-s390",
398 .parent = TYPE_VIRTIO_S390_DEVICE,
399 .instance_size = sizeof(VirtIOS390Device),
400 .class_init = s390_virtio_net_class_init,
401 };
402
403 static Property s390_virtio_blk_properties[] = {
404 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, blk.conf),
405 DEFINE_PROP_STRING("serial", VirtIOS390Device, blk.serial),
406 #ifdef __linux__
407 DEFINE_PROP_BIT("scsi", VirtIOS390Device, blk.scsi, 0, true),
408 #endif
409 DEFINE_PROP_END_OF_LIST(),
410 };
411
412 static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
413 {
414 DeviceClass *dc = DEVICE_CLASS(klass);
415 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
416
417 k->init = s390_virtio_blk_init;
418 dc->props = s390_virtio_blk_properties;
419 }
420
421 static TypeInfo s390_virtio_blk = {
422 .name = "virtio-blk-s390",
423 .parent = TYPE_VIRTIO_S390_DEVICE,
424 .instance_size = sizeof(VirtIOS390Device),
425 .class_init = s390_virtio_blk_class_init,
426 };
427
428 static Property s390_virtio_serial_properties[] = {
429 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
430 serial.max_virtserial_ports, 31),
431 DEFINE_PROP_END_OF_LIST(),
432 };
433
434 static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
435 {
436 DeviceClass *dc = DEVICE_CLASS(klass);
437 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
438
439 k->init = s390_virtio_serial_init;
440 dc->props = s390_virtio_serial_properties;
441 }
442
443 static TypeInfo s390_virtio_serial = {
444 .name = "virtio-serial-s390",
445 .parent = TYPE_VIRTIO_S390_DEVICE,
446 .instance_size = sizeof(VirtIOS390Device),
447 .class_init = s390_virtio_serial_class_init,
448 };
449
450 static int s390_virtio_busdev_init(DeviceState *dev)
451 {
452 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
453 VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev);
454
455 return _info->init(_dev);
456 }
457
458 static void virtio_s390_device_class_init(ObjectClass *klass, void *data)
459 {
460 DeviceClass *dc = DEVICE_CLASS(klass);
461
462 dc->init = s390_virtio_busdev_init;
463 dc->bus_info = &s390_virtio_bus_info;
464 dc->unplug = qdev_simple_unplug_cb;
465 }
466
467 static TypeInfo virtio_s390_device_info = {
468 .name = TYPE_VIRTIO_S390_DEVICE,
469 .parent = TYPE_DEVICE,
470 .instance_size = sizeof(VirtIOS390Device),
471 .class_init = virtio_s390_device_class_init,
472 .class_size = sizeof(VirtIOS390DeviceClass),
473 .abstract = true,
474 };
475
476 static Property s390_virtio_scsi_properties[] = {
477 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOS390Device, host_features, scsi),
478 DEFINE_PROP_END_OF_LIST(),
479 };
480
481 static void s390_virtio_scsi_class_init(ObjectClass *klass, void *data)
482 {
483 DeviceClass *dc = DEVICE_CLASS(klass);
484 VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
485
486 k->init = s390_virtio_scsi_init;
487 dc->props = s390_virtio_scsi_properties;
488 }
489
490 static TypeInfo s390_virtio_scsi = {
491 .name = "virtio-scsi-s390",
492 .parent = TYPE_VIRTIO_S390_DEVICE,
493 .instance_size = sizeof(VirtIOS390Device),
494 .class_init = s390_virtio_scsi_class_init,
495 };
496
497 /***************** S390 Virtio Bus Bridge Device *******************/
498 /* Only required to have the virtio bus as child in the system bus */
499
500 static int s390_virtio_bridge_init(SysBusDevice *dev)
501 {
502 /* nothing */
503 return 0;
504 }
505
506 static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data)
507 {
508 DeviceClass *dc = DEVICE_CLASS(klass);
509 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
510
511 k->init = s390_virtio_bridge_init;
512 dc->no_user = 1;
513 }
514
515 static TypeInfo s390_virtio_bridge_info = {
516 .name = "s390-virtio-bridge",
517 .parent = TYPE_SYS_BUS_DEVICE,
518 .instance_size = sizeof(SysBusDevice),
519 .class_init = s390_virtio_bridge_class_init,
520 };
521
522 static void s390_virtio_register_types(void)
523 {
524 type_register_static(&virtio_s390_device_info);
525 type_register_static(&s390_virtio_serial);
526 type_register_static(&s390_virtio_blk);
527 type_register_static(&s390_virtio_net);
528 type_register_static(&s390_virtio_scsi);
529 type_register_static(&s390_virtio_bridge_info);
530 }
531
532 type_init(s390_virtio_register_types)