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