]>
Commit | Line | Data |
---|---|---|
f3304eea AG |
1 | /* |
2 | * QEMU S390 virtio target | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <[email protected]> | |
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 | ||
b73d3531 | 20 | #include "hw/hw.h" |
4be74634 | 21 | #include "sysemu/block-backend.h" |
9c17d615 | 22 | #include "sysemu/sysemu.h" |
b73d3531 | 23 | #include "hw/boards.h" |
b73d3531 | 24 | #include "hw/loader.h" |
f3304eea | 25 | #include "elf.h" |
0d09e41a PB |
26 | #include "hw/virtio/virtio.h" |
27 | #include "hw/virtio/virtio-rng.h" | |
28 | #include "hw/virtio/virtio-serial.h" | |
29 | #include "hw/virtio/virtio-net.h" | |
d6e51919 | 30 | #include "hw/virtio/vhost-scsi.h" |
f3304eea | 31 | #include "hw/sysbus.h" |
9c17d615 | 32 | #include "sysemu/kvm.h" |
f3304eea | 33 | |
b73d3531 | 34 | #include "hw/s390x/s390-virtio-bus.h" |
0d09e41a | 35 | #include "hw/virtio/virtio-bus.h" |
f3304eea AG |
36 | |
37 | /* #define DEBUG_S390 */ | |
38 | ||
39 | #ifdef DEBUG_S390 | |
e67137c6 | 40 | #define DPRINTF(fmt, ...) \ |
f3304eea AG |
41 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
42 | #else | |
e67137c6 | 43 | #define DPRINTF(fmt, ...) \ |
f3304eea AG |
44 | do { } while (0) |
45 | #endif | |
46 | ||
74c85296 JW |
47 | #define VIRTIO_S390_QUEUE_MAX 64 |
48 | ||
5d6c0c49 AF |
49 | static void virtio_s390_bus_new(VirtioBusState *bus, size_t bus_size, |
50 | VirtIOS390Device *dev); | |
d51fcfac | 51 | |
0d936928 AL |
52 | static const TypeInfo s390_virtio_bus_info = { |
53 | .name = TYPE_S390_VIRTIO_BUS, | |
54 | .parent = TYPE_BUS, | |
55 | .instance_size = sizeof(VirtIOS390Bus), | |
f3304eea AG |
56 | }; |
57 | ||
f3304eea | 58 | static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev); |
f3304eea | 59 | |
d1ff903c | 60 | /* length of VirtIO device pages */ |
a8170e5e | 61 | const hwaddr virtio_size = S390_DEVICE_PAGES * TARGET_PAGE_SIZE; |
d1ff903c | 62 | |
eb3caa44 JF |
63 | static void s390_virtio_bus_reset(void *opaque) |
64 | { | |
65 | VirtIOS390Bus *bus = opaque; | |
66 | bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE; | |
67 | } | |
68 | ||
4170aea1 JF |
69 | void s390_virtio_reset_idx(VirtIOS390Device *dev) |
70 | { | |
71 | int i; | |
a8170e5e | 72 | hwaddr idx_addr; |
4170aea1 JF |
73 | uint8_t num_vq; |
74 | ||
75 | num_vq = s390_virtio_device_num_vq(dev); | |
76 | for (i = 0; i < num_vq; i++) { | |
77 | idx_addr = virtio_queue_get_avail_addr(dev->vdev, i) + | |
78 | VIRTIO_VRING_AVAIL_IDX_OFFS; | |
42874d3a PM |
79 | address_space_stw(&address_space_memory, idx_addr, 0, |
80 | MEMTXATTRS_UNSPECIFIED, NULL); | |
77ae0b2a CB |
81 | idx_addr = virtio_queue_get_avail_addr(dev->vdev, i) + |
82 | virtio_queue_get_avail_size(dev->vdev, i); | |
83 | address_space_stw(&address_space_memory, idx_addr, 0, | |
84 | MEMTXATTRS_UNSPECIFIED, NULL); | |
4170aea1 JF |
85 | idx_addr = virtio_queue_get_used_addr(dev->vdev, i) + |
86 | VIRTIO_VRING_USED_IDX_OFFS; | |
42874d3a PM |
87 | address_space_stw(&address_space_memory, idx_addr, 0, |
88 | MEMTXATTRS_UNSPECIFIED, NULL); | |
77ae0b2a CB |
89 | idx_addr = virtio_queue_get_used_addr(dev->vdev, i) + |
90 | virtio_queue_get_used_size(dev->vdev, i); | |
91 | address_space_stw(&address_space_memory, idx_addr, 0, | |
92 | MEMTXATTRS_UNSPECIFIED, NULL); | |
4170aea1 JF |
93 | } |
94 | } | |
95 | ||
f3304eea AG |
96 | VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size) |
97 | { | |
98 | VirtIOS390Bus *bus; | |
99 | BusState *_bus; | |
100 | DeviceState *dev; | |
101 | ||
102 | /* Create bridge device */ | |
103 | dev = qdev_create(NULL, "s390-virtio-bridge"); | |
104 | qdev_init_nofail(dev); | |
105 | ||
106 | /* Create bus on bridge device */ | |
107 | ||
0d936928 | 108 | _bus = qbus_create(TYPE_S390_VIRTIO_BUS, dev, "s390-virtio"); |
f3304eea AG |
109 | bus = DO_UPCAST(VirtIOS390Bus, bus, _bus); |
110 | ||
111 | bus->dev_page = *ram_size; | |
112 | bus->dev_offs = bus->dev_page; | |
113 | bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE; | |
114 | ||
7fa41e53 | 115 | /* Enable hotplugging */ |
e98f8c36 | 116 | qbus_set_hotplug_handler(_bus, dev, &error_abort); |
7fa41e53 | 117 | |
f3304eea AG |
118 | /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */ |
119 | *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE; | |
120 | ||
eb3caa44 | 121 | qemu_register_reset(s390_virtio_bus_reset, bus); |
f3304eea AG |
122 | return bus; |
123 | } | |
124 | ||
e3e300d2 MA |
125 | static void s390_virtio_device_init(VirtIOS390Device *dev, |
126 | VirtIODevice *vdev) | |
f3304eea AG |
127 | { |
128 | VirtIOS390Bus *bus; | |
129 | int dev_len; | |
130 | ||
131 | bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus); | |
132 | dev->vdev = vdev; | |
133 | dev->dev_offs = bus->dev_offs; | |
134 | dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */ | |
135 | ||
136 | dev_len = VIRTIO_DEV_OFFS_CONFIG; | |
137 | dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN; | |
138 | dev_len += dev->feat_len * 2; | |
181103cd | 139 | dev_len += virtio_bus_get_vdev_config_len(&dev->bus); |
f3304eea AG |
140 | |
141 | bus->dev_offs += dev_len; | |
142 | ||
f3304eea | 143 | s390_virtio_device_sync(dev); |
4170aea1 | 144 | s390_virtio_reset_idx(dev); |
7fa41e53 | 145 | if (dev->qdev.hotplugged) { |
de13d216 | 146 | s390_virtio_irq(VIRTIO_PARAM_DEV_ADD, dev->dev_offs); |
7fa41e53 | 147 | } |
f3304eea AG |
148 | } |
149 | ||
f35dd566 | 150 | static void s390_virtio_net_realize(VirtIOS390Device *s390_dev, Error **errp) |
f3304eea | 151 | { |
800ced8c | 152 | DeviceState *qdev = DEVICE(s390_dev); |
74b4fe3d FK |
153 | VirtIONetS390 *dev = VIRTIO_NET_S390(s390_dev); |
154 | DeviceState *vdev = DEVICE(&dev->vdev); | |
f35dd566 | 155 | Error *err = NULL; |
f3304eea | 156 | |
800ced8c FK |
157 | virtio_net_set_netclient_name(&dev->vdev, qdev->id, |
158 | object_get_typename(OBJECT(qdev))); | |
74b4fe3d | 159 | qdev_set_parent_bus(vdev, BUS(&s390_dev->bus)); |
f35dd566 MA |
160 | object_property_set_bool(OBJECT(vdev), true, "realized", &err); |
161 | if (err) { | |
162 | error_propagate(errp, err); | |
163 | return; | |
f3304eea AG |
164 | } |
165 | ||
e3e300d2 | 166 | s390_virtio_device_init(s390_dev, VIRTIO_DEVICE(vdev)); |
74b4fe3d FK |
167 | } |
168 | ||
169 | static void s390_virtio_net_instance_init(Object *obj) | |
170 | { | |
171 | VirtIONetS390 *dev = VIRTIO_NET_S390(obj); | |
c8075caf GA |
172 | |
173 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), | |
174 | TYPE_VIRTIO_NET); | |
0cf63c3e GA |
175 | object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), |
176 | "bootindex", &error_abort); | |
f3304eea AG |
177 | } |
178 | ||
f35dd566 | 179 | static void s390_virtio_blk_realize(VirtIOS390Device *s390_dev, Error **errp) |
f3304eea | 180 | { |
55d11e01 FK |
181 | VirtIOBlkS390 *dev = VIRTIO_BLK_S390(s390_dev); |
182 | DeviceState *vdev = DEVICE(&dev->vdev); | |
f35dd566 MA |
183 | Error *err = NULL; |
184 | ||
55d11e01 | 185 | qdev_set_parent_bus(vdev, BUS(&s390_dev->bus)); |
f35dd566 MA |
186 | object_property_set_bool(OBJECT(vdev), true, "realized", &err); |
187 | if (err) { | |
188 | error_propagate(errp, err); | |
189 | return; | |
f3304eea | 190 | } |
e3e300d2 | 191 | s390_virtio_device_init(s390_dev, VIRTIO_DEVICE(vdev)); |
55d11e01 | 192 | } |
f3304eea | 193 | |
55d11e01 FK |
194 | static void s390_virtio_blk_instance_init(Object *obj) |
195 | { | |
196 | VirtIOBlkS390 *dev = VIRTIO_BLK_S390(obj); | |
c8075caf GA |
197 | |
198 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), | |
199 | TYPE_VIRTIO_BLK); | |
467b3f33 SH |
200 | object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread", |
201 | &error_abort); | |
aeb98ddc GA |
202 | object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), |
203 | "bootindex", &error_abort); | |
f3304eea AG |
204 | } |
205 | ||
f35dd566 | 206 | static void s390_virtio_serial_realize(VirtIOS390Device *s390_dev, Error **errp) |
f3304eea | 207 | { |
55169140 FK |
208 | VirtIOSerialS390 *dev = VIRTIO_SERIAL_S390(s390_dev); |
209 | DeviceState *vdev = DEVICE(&dev->vdev); | |
210 | DeviceState *qdev = DEVICE(s390_dev); | |
f35dd566 | 211 | Error *err = NULL; |
f3304eea | 212 | VirtIOS390Bus *bus; |
80270a19 | 213 | char *bus_name; |
f3304eea | 214 | |
55169140 | 215 | bus = DO_UPCAST(VirtIOS390Bus, bus, qdev->parent_bus); |
f3304eea | 216 | |
80270a19 FK |
217 | /* |
218 | * For command line compatibility, this sets the virtio-serial-device bus | |
219 | * name as before. | |
220 | */ | |
221 | if (qdev->id) { | |
222 | bus_name = g_strdup_printf("%s.0", qdev->id); | |
223 | virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); | |
224 | g_free(bus_name); | |
225 | } | |
226 | ||
55169140 | 227 | qdev_set_parent_bus(vdev, BUS(&s390_dev->bus)); |
f35dd566 MA |
228 | object_property_set_bool(OBJECT(vdev), true, "realized", &err); |
229 | if (err) { | |
230 | error_propagate(errp, err); | |
231 | return; | |
f3304eea AG |
232 | } |
233 | ||
e3e300d2 MA |
234 | s390_virtio_device_init(s390_dev, VIRTIO_DEVICE(vdev)); |
235 | bus->console = s390_dev; | |
f3304eea AG |
236 | } |
237 | ||
55169140 FK |
238 | static void s390_virtio_serial_instance_init(Object *obj) |
239 | { | |
240 | VirtIOSerialS390 *dev = VIRTIO_SERIAL_S390(obj); | |
c8075caf GA |
241 | |
242 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), | |
243 | TYPE_VIRTIO_SERIAL); | |
55169140 FK |
244 | } |
245 | ||
f35dd566 | 246 | static void s390_virtio_scsi_realize(VirtIOS390Device *s390_dev, Error **errp) |
973abc7f | 247 | { |
9ef13d8f FK |
248 | VirtIOSCSIS390 *dev = VIRTIO_SCSI_S390(s390_dev); |
249 | DeviceState *vdev = DEVICE(&dev->vdev); | |
6f32a6b4 | 250 | DeviceState *qdev = DEVICE(s390_dev); |
f35dd566 | 251 | Error *err = NULL; |
6f32a6b4 FK |
252 | char *bus_name; |
253 | ||
254 | /* | |
255 | * For command line compatibility, this sets the virtio-scsi-device bus | |
256 | * name as before. | |
257 | */ | |
258 | if (qdev->id) { | |
259 | bus_name = g_strdup_printf("%s.0", qdev->id); | |
260 | virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); | |
261 | g_free(bus_name); | |
262 | } | |
973abc7f | 263 | |
9ef13d8f | 264 | qdev_set_parent_bus(vdev, BUS(&s390_dev->bus)); |
f35dd566 MA |
265 | object_property_set_bool(OBJECT(vdev), true, "realized", &err); |
266 | if (err) { | |
267 | error_propagate(errp, err); | |
268 | return; | |
973abc7f SH |
269 | } |
270 | ||
e3e300d2 | 271 | s390_virtio_device_init(s390_dev, VIRTIO_DEVICE(vdev)); |
9ef13d8f FK |
272 | } |
273 | ||
274 | static void s390_virtio_scsi_instance_init(Object *obj) | |
275 | { | |
276 | VirtIOSCSIS390 *dev = VIRTIO_SCSI_S390(obj); | |
c8075caf GA |
277 | |
278 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), | |
279 | TYPE_VIRTIO_SCSI); | |
973abc7f SH |
280 | } |
281 | ||
d6e51919 | 282 | #ifdef CONFIG_VHOST_SCSI |
f35dd566 | 283 | static void s390_vhost_scsi_realize(VirtIOS390Device *s390_dev, Error **errp) |
d6e51919 PB |
284 | { |
285 | VHostSCSIS390 *dev = VHOST_SCSI_S390(s390_dev); | |
286 | DeviceState *vdev = DEVICE(&dev->vdev); | |
f35dd566 | 287 | Error *err = NULL; |
d6e51919 PB |
288 | |
289 | qdev_set_parent_bus(vdev, BUS(&s390_dev->bus)); | |
f35dd566 MA |
290 | object_property_set_bool(OBJECT(vdev), true, "realized", &err); |
291 | if (err) { | |
292 | error_propagate(errp, err); | |
293 | return; | |
d6e51919 PB |
294 | } |
295 | ||
e3e300d2 | 296 | s390_virtio_device_init(s390_dev, VIRTIO_DEVICE(vdev)); |
d6e51919 PB |
297 | } |
298 | ||
299 | static void s390_vhost_scsi_instance_init(Object *obj) | |
300 | { | |
301 | VHostSCSIS390 *dev = VHOST_SCSI_S390(obj); | |
c8075caf GA |
302 | |
303 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), | |
304 | TYPE_VHOST_SCSI); | |
d6e51919 PB |
305 | } |
306 | #endif | |
307 | ||
0bca1f53 | 308 | |
f35dd566 | 309 | static void s390_virtio_rng_realize(VirtIOS390Device *s390_dev, Error **errp) |
16c915ba | 310 | { |
0bca1f53 FK |
311 | VirtIORNGS390 *dev = VIRTIO_RNG_S390(s390_dev); |
312 | DeviceState *vdev = DEVICE(&dev->vdev); | |
f35dd566 | 313 | Error *err = NULL; |
16c915ba | 314 | |
0bca1f53 | 315 | qdev_set_parent_bus(vdev, BUS(&s390_dev->bus)); |
f35dd566 MA |
316 | object_property_set_bool(OBJECT(vdev), true, "realized", &err); |
317 | if (err) { | |
318 | error_propagate(errp, err); | |
319 | return; | |
16c915ba AS |
320 | } |
321 | ||
0bca1f53 | 322 | object_property_set_link(OBJECT(dev), |
5b456438 | 323 | OBJECT(dev->vdev.conf.rng), "rng", |
0bca1f53 FK |
324 | NULL); |
325 | ||
e3e300d2 | 326 | s390_virtio_device_init(s390_dev, VIRTIO_DEVICE(vdev)); |
0bca1f53 FK |
327 | } |
328 | ||
329 | static void s390_virtio_rng_instance_init(Object *obj) | |
330 | { | |
331 | VirtIORNGS390 *dev = VIRTIO_RNG_S390(obj); | |
c8075caf GA |
332 | |
333 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), | |
334 | TYPE_VIRTIO_RNG); | |
cbd5ac69 PB |
335 | object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), |
336 | "rng", &error_abort); | |
16c915ba AS |
337 | } |
338 | ||
f3304eea AG |
339 | static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq) |
340 | { | |
341 | ram_addr_t token_off; | |
342 | ||
343 | token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) + | |
344 | (vq * VIRTIO_VQCONFIG_LEN) + | |
345 | VIRTIO_VQCONFIG_OFFS_TOKEN; | |
346 | ||
42874d3a PM |
347 | return address_space_ldq_be(&address_space_memory, token_off, |
348 | MEMTXATTRS_UNSPECIFIED, NULL); | |
f3304eea AG |
349 | } |
350 | ||
351 | static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev) | |
352 | { | |
353 | VirtIODevice *vdev = dev->vdev; | |
354 | int num_vq; | |
355 | ||
74c85296 | 356 | for (num_vq = 0; num_vq < VIRTIO_S390_QUEUE_MAX; num_vq++) { |
f3304eea AG |
357 | if (!virtio_queue_get_num(vdev, num_vq)) { |
358 | break; | |
359 | } | |
360 | } | |
361 | ||
362 | return num_vq; | |
363 | } | |
364 | ||
365 | static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus) | |
366 | { | |
367 | ram_addr_t r = bus->next_ring; | |
368 | ||
369 | bus->next_ring += VIRTIO_RING_LEN; | |
370 | return r; | |
371 | } | |
372 | ||
baf0b55a | 373 | void s390_virtio_device_sync(VirtIOS390Device *dev) |
f3304eea AG |
374 | { |
375 | VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus); | |
376 | ram_addr_t cur_offs; | |
377 | uint8_t num_vq; | |
378 | int i; | |
379 | ||
380 | virtio_reset(dev->vdev); | |
381 | ||
382 | /* Sync dev space */ | |
42874d3a PM |
383 | address_space_stb(&address_space_memory, |
384 | dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, | |
385 | dev->vdev->device_id, | |
386 | MEMTXATTRS_UNSPECIFIED, | |
387 | NULL); | |
388 | ||
389 | address_space_stb(&address_space_memory, | |
390 | dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, | |
391 | s390_virtio_device_num_vq(dev), | |
392 | MEMTXATTRS_UNSPECIFIED, | |
393 | NULL); | |
394 | address_space_stb(&address_space_memory, | |
395 | dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, | |
396 | dev->feat_len, | |
397 | MEMTXATTRS_UNSPECIFIED, | |
398 | NULL); | |
399 | ||
400 | address_space_stb(&address_space_memory, | |
401 | dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, | |
402 | dev->vdev->config_len, | |
403 | MEMTXATTRS_UNSPECIFIED, | |
404 | NULL); | |
f3304eea AG |
405 | |
406 | num_vq = s390_virtio_device_num_vq(dev); | |
42874d3a PM |
407 | address_space_stb(&address_space_memory, |
408 | dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq, | |
409 | MEMTXATTRS_UNSPECIFIED, NULL); | |
f3304eea AG |
410 | |
411 | /* Sync virtqueues */ | |
412 | for (i = 0; i < num_vq; i++) { | |
413 | ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) + | |
414 | (i * VIRTIO_VQCONFIG_LEN); | |
415 | ram_addr_t vring; | |
416 | ||
417 | vring = s390_virtio_next_ring(bus); | |
418 | virtio_queue_set_addr(dev->vdev, i, vring); | |
419 | virtio_queue_set_vector(dev->vdev, i, i); | |
42874d3a PM |
420 | address_space_stq_be(&address_space_memory, |
421 | vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring, | |
422 | MEMTXATTRS_UNSPECIFIED, NULL); | |
423 | address_space_stw_be(&address_space_memory, | |
424 | vq + VIRTIO_VQCONFIG_OFFS_NUM, | |
425 | virtio_queue_get_num(dev->vdev, i), | |
426 | MEMTXATTRS_UNSPECIFIED, | |
427 | NULL); | |
f3304eea AG |
428 | } |
429 | ||
430 | cur_offs = dev->dev_offs; | |
431 | cur_offs += VIRTIO_DEV_OFFS_CONFIG; | |
432 | cur_offs += num_vq * VIRTIO_VQCONFIG_LEN; | |
433 | ||
434 | /* Sync feature bitmap */ | |
6b8f1020 CH |
435 | address_space_stl_le(&address_space_memory, cur_offs, |
436 | dev->vdev->host_features, | |
42874d3a | 437 | MEMTXATTRS_UNSPECIFIED, NULL); |
f3304eea AG |
438 | |
439 | dev->feat_offs = cur_offs + dev->feat_len; | |
440 | cur_offs += dev->feat_len * 2; | |
441 | ||
442 | /* Sync config space */ | |
181103cd | 443 | virtio_bus_get_vdev_config(&dev->bus, dev->vdev->config); |
f3304eea | 444 | |
54f7b4a3 SW |
445 | cpu_physical_memory_write(cur_offs, |
446 | dev->vdev->config, dev->vdev->config_len); | |
f3304eea AG |
447 | cur_offs += dev->vdev->config_len; |
448 | } | |
449 | ||
450 | void s390_virtio_device_update_status(VirtIOS390Device *dev) | |
451 | { | |
452 | VirtIODevice *vdev = dev->vdev; | |
453 | uint32_t features; | |
454 | ||
42874d3a PM |
455 | virtio_set_status(vdev, |
456 | address_space_ldub(&address_space_memory, | |
457 | dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, | |
458 | MEMTXATTRS_UNSPECIFIED, NULL)); | |
f3304eea AG |
459 | |
460 | /* Update guest supported feature bitmap */ | |
461 | ||
42874d3a PM |
462 | features = bswap32(address_space_ldl_be(&address_space_memory, |
463 | dev->feat_offs, | |
464 | MEMTXATTRS_UNSPECIFIED, NULL)); | |
ad0c9332 | 465 | virtio_set_features(vdev, features); |
f3304eea AG |
466 | } |
467 | ||
f3304eea AG |
468 | /* Find a device by vring address */ |
469 | VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus, | |
470 | ram_addr_t mem, | |
471 | int *vq_num) | |
472 | { | |
0866aca1 | 473 | BusChild *kid; |
f3304eea AG |
474 | int i; |
475 | ||
0866aca1 AL |
476 | QTAILQ_FOREACH(kid, &bus->bus.children, sibling) { |
477 | VirtIOS390Device *dev = (VirtIOS390Device *)kid->child; | |
478 | ||
74c85296 | 479 | for (i = 0; i < VIRTIO_S390_QUEUE_MAX; i++) { |
0866aca1 | 480 | if (!virtio_queue_get_addr(dev->vdev, i)) |
f3304eea | 481 | break; |
0866aca1 | 482 | if (virtio_queue_get_addr(dev->vdev, i) == mem) { |
f3304eea AG |
483 | if (vq_num) { |
484 | *vq_num = i; | |
485 | } | |
0866aca1 | 486 | return dev; |
f3304eea AG |
487 | } |
488 | } | |
489 | } | |
490 | ||
491 | return NULL; | |
492 | } | |
493 | ||
494 | /* Find a device by device descriptor location */ | |
495 | VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem) | |
496 | { | |
0866aca1 | 497 | BusChild *kid; |
f3304eea | 498 | |
0866aca1 AL |
499 | QTAILQ_FOREACH(kid, &bus->bus.children, sibling) { |
500 | VirtIOS390Device *dev = (VirtIOS390Device *)kid->child; | |
501 | if (dev->dev_offs == mem) { | |
502 | return dev; | |
f3304eea AG |
503 | } |
504 | } | |
505 | ||
506 | return NULL; | |
507 | } | |
508 | ||
d2a0ccc6 MT |
509 | /* DeviceState to VirtIOS390Device. Note: used on datapath, |
510 | * be careful and test performance if you change this. | |
511 | */ | |
512 | static inline VirtIOS390Device *to_virtio_s390_device_fast(DeviceState *d) | |
513 | { | |
514 | return container_of(d, VirtIOS390Device, qdev); | |
515 | } | |
516 | ||
517 | /* DeviceState to VirtIOS390Device. TODO: use QOM. */ | |
518 | static inline VirtIOS390Device *to_virtio_s390_device(DeviceState *d) | |
519 | { | |
520 | return container_of(d, VirtIOS390Device, qdev); | |
521 | } | |
522 | ||
523 | static void virtio_s390_notify(DeviceState *d, uint16_t vector) | |
f3304eea | 524 | { |
d2a0ccc6 | 525 | VirtIOS390Device *dev = to_virtio_s390_device_fast(d); |
f3304eea AG |
526 | uint64_t token = s390_virtio_device_vq_token(dev, vector); |
527 | ||
de13d216 | 528 | s390_virtio_irq(0, token); |
f3304eea AG |
529 | } |
530 | ||
d820331a JW |
531 | static void virtio_s390_device_plugged(DeviceState *d, Error **errp) |
532 | { | |
533 | VirtIOS390Device *dev = to_virtio_s390_device(d); | |
534 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); | |
535 | int n = virtio_get_num_queues(vdev); | |
536 | ||
537 | if (n > VIRTIO_S390_QUEUE_MAX) { | |
538 | error_setg(errp, "The nubmer of virtqueues %d " | |
539 | "exceeds s390 limit %d", n, | |
540 | VIRTIO_S390_QUEUE_MAX); | |
541 | } | |
542 | } | |
543 | ||
f3304eea AG |
544 | /**************** S390 Virtio Bus Device Descriptions *******************/ |
545 | ||
19b6914a AL |
546 | static void s390_virtio_net_class_init(ObjectClass *klass, void *data) |
547 | { | |
39bffca2 AL |
548 | DeviceClass *dc = DEVICE_CLASS(klass); |
549 | VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass); | |
19b6914a | 550 | |
f35dd566 | 551 | k->realize = s390_virtio_net_realize; |
4d1866de | 552 | set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); |
19b6914a AL |
553 | } |
554 | ||
8c43a6f0 | 555 | static const TypeInfo s390_virtio_net = { |
74b4fe3d | 556 | .name = TYPE_VIRTIO_NET_S390, |
39bffca2 | 557 | .parent = TYPE_VIRTIO_S390_DEVICE, |
74b4fe3d FK |
558 | .instance_size = sizeof(VirtIONetS390), |
559 | .instance_init = s390_virtio_net_instance_init, | |
39bffca2 AL |
560 | .class_init = s390_virtio_net_class_init, |
561 | }; | |
562 | ||
19b6914a AL |
563 | static void s390_virtio_blk_class_init(ObjectClass *klass, void *data) |
564 | { | |
39bffca2 | 565 | VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass); |
4d1866de | 566 | DeviceClass *dc = DEVICE_CLASS(klass); |
19b6914a | 567 | |
f35dd566 | 568 | k->realize = s390_virtio_blk_realize; |
4d1866de | 569 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
19b6914a AL |
570 | } |
571 | ||
8c43a6f0 | 572 | static const TypeInfo s390_virtio_blk = { |
39bffca2 AL |
573 | .name = "virtio-blk-s390", |
574 | .parent = TYPE_VIRTIO_S390_DEVICE, | |
55d11e01 FK |
575 | .instance_size = sizeof(VirtIOBlkS390), |
576 | .instance_init = s390_virtio_blk_instance_init, | |
39bffca2 AL |
577 | .class_init = s390_virtio_blk_class_init, |
578 | }; | |
579 | ||
580 | static Property s390_virtio_serial_properties[] = { | |
39bffca2 | 581 | DEFINE_PROP_END_OF_LIST(), |
f3304eea AG |
582 | }; |
583 | ||
19b6914a AL |
584 | static void s390_virtio_serial_class_init(ObjectClass *klass, void *data) |
585 | { | |
39bffca2 AL |
586 | DeviceClass *dc = DEVICE_CLASS(klass); |
587 | VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass); | |
19b6914a | 588 | |
f35dd566 | 589 | k->realize = s390_virtio_serial_realize; |
39bffca2 | 590 | dc->props = s390_virtio_serial_properties; |
4d1866de | 591 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
19b6914a AL |
592 | } |
593 | ||
8c43a6f0 | 594 | static const TypeInfo s390_virtio_serial = { |
55169140 | 595 | .name = TYPE_VIRTIO_SERIAL_S390, |
39bffca2 | 596 | .parent = TYPE_VIRTIO_S390_DEVICE, |
55169140 FK |
597 | .instance_size = sizeof(VirtIOSerialS390), |
598 | .instance_init = s390_virtio_serial_instance_init, | |
39bffca2 | 599 | .class_init = s390_virtio_serial_class_init, |
f3304eea AG |
600 | }; |
601 | ||
16c915ba AS |
602 | static void s390_virtio_rng_class_init(ObjectClass *klass, void *data) |
603 | { | |
75f6e8b0 | 604 | DeviceClass *dc = DEVICE_CLASS(klass); |
16c915ba AS |
605 | VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass); |
606 | ||
f35dd566 | 607 | k->realize = s390_virtio_rng_realize; |
4d1866de | 608 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
16c915ba AS |
609 | } |
610 | ||
8c43a6f0 | 611 | static const TypeInfo s390_virtio_rng = { |
0bca1f53 | 612 | .name = TYPE_VIRTIO_RNG_S390, |
16c915ba | 613 | .parent = TYPE_VIRTIO_S390_DEVICE, |
0bca1f53 FK |
614 | .instance_size = sizeof(VirtIORNGS390), |
615 | .instance_init = s390_virtio_rng_instance_init, | |
16c915ba AS |
616 | .class_init = s390_virtio_rng_class_init, |
617 | }; | |
618 | ||
f35dd566 | 619 | static void s390_virtio_busdev_realize(DeviceState *dev, Error **errp) |
f3304eea | 620 | { |
f3304eea | 621 | VirtIOS390Device *_dev = (VirtIOS390Device *)dev; |
19b6914a | 622 | VirtIOS390DeviceClass *_info = VIRTIO_S390_DEVICE_GET_CLASS(dev); |
f3304eea | 623 | |
5d6c0c49 | 624 | virtio_s390_bus_new(&_dev->bus, sizeof(_dev->bus), _dev); |
11e9235b | 625 | |
f35dd566 | 626 | _info->realize(_dev, errp); |
f3304eea AG |
627 | } |
628 | ||
93726cb3 PB |
629 | static void s390_virtio_busdev_reset(DeviceState *dev) |
630 | { | |
631 | VirtIOS390Device *_dev = (VirtIOS390Device *)dev; | |
632 | ||
633 | virtio_reset(_dev->vdev); | |
634 | } | |
635 | ||
39bffca2 | 636 | static void virtio_s390_device_class_init(ObjectClass *klass, void *data) |
f3304eea | 637 | { |
39bffca2 | 638 | DeviceClass *dc = DEVICE_CLASS(klass); |
f3304eea | 639 | |
f35dd566 | 640 | dc->realize = s390_virtio_busdev_realize; |
0d936928 | 641 | dc->bus_type = TYPE_S390_VIRTIO_BUS; |
93726cb3 | 642 | dc->reset = s390_virtio_busdev_reset; |
f3304eea AG |
643 | } |
644 | ||
8c43a6f0 | 645 | static const TypeInfo virtio_s390_device_info = { |
19b6914a AL |
646 | .name = TYPE_VIRTIO_S390_DEVICE, |
647 | .parent = TYPE_DEVICE, | |
648 | .instance_size = sizeof(VirtIOS390Device), | |
39bffca2 | 649 | .class_init = virtio_s390_device_class_init, |
e87f7fc6 | 650 | .class_size = sizeof(VirtIOS390DeviceClass), |
19b6914a AL |
651 | .abstract = true, |
652 | }; | |
653 | ||
973abc7f SH |
654 | static void s390_virtio_scsi_class_init(ObjectClass *klass, void *data) |
655 | { | |
656 | DeviceClass *dc = DEVICE_CLASS(klass); | |
657 | VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass); | |
658 | ||
f35dd566 | 659 | k->realize = s390_virtio_scsi_realize; |
4d1866de | 660 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
973abc7f SH |
661 | } |
662 | ||
8c43a6f0 | 663 | static const TypeInfo s390_virtio_scsi = { |
9ef13d8f | 664 | .name = TYPE_VIRTIO_SCSI_S390, |
973abc7f | 665 | .parent = TYPE_VIRTIO_S390_DEVICE, |
9ef13d8f FK |
666 | .instance_size = sizeof(VirtIOSCSIS390), |
667 | .instance_init = s390_virtio_scsi_instance_init, | |
973abc7f SH |
668 | .class_init = s390_virtio_scsi_class_init, |
669 | }; | |
f3304eea | 670 | |
d6e51919 | 671 | #ifdef CONFIG_VHOST_SCSI |
d6e51919 PB |
672 | static void s390_vhost_scsi_class_init(ObjectClass *klass, void *data) |
673 | { | |
674 | DeviceClass *dc = DEVICE_CLASS(klass); | |
675 | VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass); | |
676 | ||
f35dd566 | 677 | k->realize = s390_vhost_scsi_realize; |
4d1866de | 678 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
d6e51919 PB |
679 | } |
680 | ||
681 | static const TypeInfo s390_vhost_scsi = { | |
682 | .name = TYPE_VHOST_SCSI_S390, | |
683 | .parent = TYPE_VIRTIO_S390_DEVICE, | |
684 | .instance_size = sizeof(VHostSCSIS390), | |
685 | .instance_init = s390_vhost_scsi_instance_init, | |
686 | .class_init = s390_vhost_scsi_class_init, | |
687 | }; | |
688 | #endif | |
689 | ||
f3304eea AG |
690 | /***************** S390 Virtio Bus Bridge Device *******************/ |
691 | /* Only required to have the virtio bus as child in the system bus */ | |
692 | ||
693 | static int s390_virtio_bridge_init(SysBusDevice *dev) | |
694 | { | |
695 | /* nothing */ | |
696 | return 0; | |
697 | } | |
698 | ||
999e12bb AL |
699 | static void s390_virtio_bridge_class_init(ObjectClass *klass, void *data) |
700 | { | |
701 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); | |
4d1866de | 702 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb AL |
703 | |
704 | k->init = s390_virtio_bridge_init; | |
4d1866de | 705 | set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); |
999e12bb AL |
706 | } |
707 | ||
8c43a6f0 | 708 | static const TypeInfo s390_virtio_bridge_info = { |
39bffca2 AL |
709 | .name = "s390-virtio-bridge", |
710 | .parent = TYPE_SYS_BUS_DEVICE, | |
711 | .instance_size = sizeof(SysBusDevice), | |
712 | .class_init = s390_virtio_bridge_class_init, | |
e98f8c36 IM |
713 | .interfaces = (InterfaceInfo[]) { |
714 | { TYPE_HOTPLUG_HANDLER }, | |
715 | { } | |
716 | } | |
f3304eea AG |
717 | }; |
718 | ||
ea35d4f1 FK |
719 | /* virtio-s390-bus */ |
720 | ||
5d6c0c49 AF |
721 | static void virtio_s390_bus_new(VirtioBusState *bus, size_t bus_size, |
722 | VirtIOS390Device *dev) | |
ea35d4f1 FK |
723 | { |
724 | DeviceState *qdev = DEVICE(dev); | |
f4dd69aa FK |
725 | char virtio_bus_name[] = "virtio-bus"; |
726 | ||
fb17dfe0 AF |
727 | qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_S390_BUS, |
728 | qdev, virtio_bus_name); | |
ea35d4f1 FK |
729 | } |
730 | ||
731 | static void virtio_s390_bus_class_init(ObjectClass *klass, void *data) | |
732 | { | |
733 | VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); | |
734 | BusClass *bus_class = BUS_CLASS(klass); | |
735 | bus_class->max_dev = 1; | |
736 | k->notify = virtio_s390_notify; | |
d820331a | 737 | k->device_plugged = virtio_s390_device_plugged; |
ea35d4f1 FK |
738 | } |
739 | ||
740 | static const TypeInfo virtio_s390_bus_info = { | |
741 | .name = TYPE_VIRTIO_S390_BUS, | |
742 | .parent = TYPE_VIRTIO_BUS, | |
743 | .instance_size = sizeof(VirtioS390BusState), | |
744 | .class_init = virtio_s390_bus_class_init, | |
745 | }; | |
746 | ||
83f7d43a | 747 | static void s390_virtio_register_types(void) |
f3304eea | 748 | { |
ea35d4f1 | 749 | type_register_static(&virtio_s390_bus_info); |
0d936928 | 750 | type_register_static(&s390_virtio_bus_info); |
83f7d43a AF |
751 | type_register_static(&virtio_s390_device_info); |
752 | type_register_static(&s390_virtio_serial); | |
753 | type_register_static(&s390_virtio_blk); | |
754 | type_register_static(&s390_virtio_net); | |
973abc7f | 755 | type_register_static(&s390_virtio_scsi); |
b702d2ae | 756 | #ifdef CONFIG_VHOST_SCSI |
d6e51919 | 757 | type_register_static(&s390_vhost_scsi); |
b702d2ae | 758 | #endif |
16c915ba | 759 | type_register_static(&s390_virtio_rng); |
39bffca2 | 760 | type_register_static(&s390_virtio_bridge_info); |
f3304eea AG |
761 | } |
762 | ||
83f7d43a | 763 | type_init(s390_virtio_register_types) |