linux-user: convert ioctl(SIOCGIFCONF, ...) result.
[qemu/ar7.git] / hw / s390-virtio-bus.c
blobbb49e393ec8bdfa0c2e5447606a58abd1edf5c4b
1 /*
2 * QEMU S390 virtio target
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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.
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.
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/>.
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"
34 #include "hw/s390-virtio-bus.h"
36 /* #define DEBUG_S390 */
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
46 #define VIRTIO_EXT_CODE 0x2603
48 struct BusInfo s390_virtio_bus_info = {
49 .name = "s390-virtio",
50 .size = sizeof(VirtIOS390Bus),
53 typedef struct {
54 DeviceInfo qdev;
55 int (*init)(VirtIOS390Device *dev);
56 } VirtIOS390DeviceInfo;
59 static const VirtIOBindings virtio_s390_bindings;
61 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
63 VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
65 VirtIOS390Bus *bus;
66 BusState *_bus;
67 DeviceState *dev;
69 /* Create bridge device */
70 dev = qdev_create(NULL, "s390-virtio-bridge");
71 qdev_init_nofail(dev);
73 /* Create bus on bridge device */
75 _bus = qbus_create(&s390_virtio_bus_info, dev, "s390-virtio");
76 bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
78 bus->dev_page = *ram_size;
79 bus->dev_offs = bus->dev_page;
80 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
82 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
83 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
85 return bus;
88 static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
90 VirtIOS390Bus *bus;
91 int dev_len;
93 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
94 dev->vdev = vdev;
95 dev->dev_offs = bus->dev_offs;
96 dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */
98 dev_len = VIRTIO_DEV_OFFS_CONFIG;
99 dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN;
100 dev_len += dev->feat_len * 2;
101 dev_len += vdev->config_len;
103 bus->dev_offs += dev_len;
105 virtio_bind_device(vdev, &virtio_s390_bindings, dev);
106 dev->host_features = vdev->get_features(vdev, dev->host_features);
107 s390_virtio_device_sync(dev);
109 return 0;
112 static int s390_virtio_net_init(VirtIOS390Device *dev)
114 VirtIODevice *vdev;
116 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
117 if (!vdev) {
118 return -1;
121 return s390_virtio_device_init(dev, vdev);
124 static int s390_virtio_blk_init(VirtIOS390Device *dev)
126 VirtIODevice *vdev;
128 vdev = virtio_blk_init((DeviceState *)dev, &dev->block);
129 if (!vdev) {
130 return -1;
133 return s390_virtio_device_init(dev, vdev);
136 static int s390_virtio_serial_init(VirtIOS390Device *dev)
138 VirtIOS390Bus *bus;
139 VirtIODevice *vdev;
140 int r;
142 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
144 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
145 if (!vdev) {
146 return -1;
149 r = s390_virtio_device_init(dev, vdev);
150 if (!r) {
151 bus->console = dev;
154 return r;
157 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
159 ram_addr_t token_off;
161 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
162 (vq * VIRTIO_VQCONFIG_LEN) +
163 VIRTIO_VQCONFIG_OFFS_TOKEN;
165 return ldq_phys(token_off);
168 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
170 VirtIODevice *vdev = dev->vdev;
171 int num_vq;
173 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
174 if (!virtio_queue_get_num(vdev, num_vq)) {
175 break;
179 return num_vq;
182 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
184 ram_addr_t r = bus->next_ring;
186 bus->next_ring += VIRTIO_RING_LEN;
187 return r;
190 void s390_virtio_device_sync(VirtIOS390Device *dev)
192 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
193 ram_addr_t cur_offs;
194 uint8_t num_vq;
195 int i;
197 virtio_reset(dev->vdev);
199 /* Sync dev space */
200 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
202 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
203 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
205 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
207 num_vq = s390_virtio_device_num_vq(dev);
208 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
210 /* Sync virtqueues */
211 for (i = 0; i < num_vq; i++) {
212 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
213 (i * VIRTIO_VQCONFIG_LEN);
214 ram_addr_t vring;
216 vring = s390_virtio_next_ring(bus);
217 virtio_queue_set_addr(dev->vdev, i, vring);
218 virtio_queue_set_vector(dev->vdev, i, i);
219 stq_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
220 stw_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
223 cur_offs = dev->dev_offs;
224 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
225 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
227 /* Sync feature bitmap */
228 stl_phys(cur_offs, bswap32(dev->host_features));
230 dev->feat_offs = cur_offs + dev->feat_len;
231 cur_offs += dev->feat_len * 2;
233 /* Sync config space */
234 if (dev->vdev->get_config) {
235 dev->vdev->get_config(dev->vdev, dev->vdev->config);
238 cpu_physical_memory_write(cur_offs,
239 dev->vdev->config, dev->vdev->config_len);
240 cur_offs += dev->vdev->config_len;
243 void s390_virtio_device_update_status(VirtIOS390Device *dev)
245 VirtIODevice *vdev = dev->vdev;
246 uint32_t features;
248 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
250 /* Update guest supported feature bitmap */
252 features = bswap32(ldl_phys(dev->feat_offs));
253 if (vdev->set_features) {
254 vdev->set_features(vdev, features);
256 vdev->guest_features = features;
259 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
261 return bus->console;
264 /* Find a device by vring address */
265 VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
266 ram_addr_t mem,
267 int *vq_num)
269 VirtIOS390Device *_dev;
270 DeviceState *dev;
271 int i;
273 QLIST_FOREACH(dev, &bus->bus.children, sibling) {
274 _dev = (VirtIOS390Device *)dev;
275 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
276 if (!virtio_queue_get_addr(_dev->vdev, i))
277 break;
278 if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
279 if (vq_num) {
280 *vq_num = i;
282 return _dev;
287 return NULL;
290 /* Find a device by device descriptor location */
291 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
293 VirtIOS390Device *_dev;
294 DeviceState *dev;
296 QLIST_FOREACH(dev, &bus->bus.children, sibling) {
297 _dev = (VirtIOS390Device *)dev;
298 if (_dev->dev_offs == mem) {
299 return _dev;
303 return NULL;
306 static void virtio_s390_notify(void *opaque, uint16_t vector)
308 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
309 uint64_t token = s390_virtio_device_vq_token(dev, vector);
310 CPUState *env = s390_cpu_addr2state(0);
312 if (kvm_enabled()) {
313 kvm_s390_virtio_irq(env, 0, token);
314 } else {
315 cpu_inject_ext(env, VIRTIO_EXT_CODE, 0, token);
319 static unsigned virtio_s390_get_features(void *opaque)
321 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
322 return dev->host_features;
325 /**************** S390 Virtio Bus Device Descriptions *******************/
327 static const VirtIOBindings virtio_s390_bindings = {
328 .notify = virtio_s390_notify,
329 .get_features = virtio_s390_get_features,
332 static VirtIOS390DeviceInfo s390_virtio_net = {
333 .init = s390_virtio_net_init,
334 .qdev.name = "virtio-net-s390",
335 .qdev.alias = "virtio-net",
336 .qdev.size = sizeof(VirtIOS390Device),
337 .qdev.props = (Property[]) {
338 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
339 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
340 net.txtimer, TX_TIMER_INTERVAL),
341 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
342 net.txburst, TX_BURST),
343 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
344 DEFINE_PROP_END_OF_LIST(),
348 static VirtIOS390DeviceInfo s390_virtio_blk = {
349 .init = s390_virtio_blk_init,
350 .qdev.name = "virtio-blk-s390",
351 .qdev.alias = "virtio-blk",
352 .qdev.size = sizeof(VirtIOS390Device),
353 .qdev.props = (Property[]) {
354 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, block),
355 DEFINE_PROP_END_OF_LIST(),
359 static VirtIOS390DeviceInfo s390_virtio_serial = {
360 .init = s390_virtio_serial_init,
361 .qdev.name = "virtio-serial-s390",
362 .qdev.alias = "virtio-serial",
363 .qdev.size = sizeof(VirtIOS390Device),
364 .qdev.props = (Property[]) {
365 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
366 serial.max_virtserial_ports, 31),
367 DEFINE_PROP_END_OF_LIST(),
371 static int s390_virtio_busdev_init(DeviceState *dev, DeviceInfo *info)
373 VirtIOS390DeviceInfo *_info = (VirtIOS390DeviceInfo *)info;
374 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
376 return _info->init(_dev);
379 static void s390_virtio_bus_register_withprop(VirtIOS390DeviceInfo *info)
381 info->qdev.init = s390_virtio_busdev_init;
382 info->qdev.bus_info = &s390_virtio_bus_info;
384 assert(info->qdev.size >= sizeof(VirtIOS390Device));
385 qdev_register(&info->qdev);
388 static void s390_virtio_register(void)
390 s390_virtio_bus_register_withprop(&s390_virtio_serial);
391 s390_virtio_bus_register_withprop(&s390_virtio_blk);
392 s390_virtio_bus_register_withprop(&s390_virtio_net);
394 device_init(s390_virtio_register);
397 /***************** S390 Virtio Bus Bridge Device *******************/
398 /* Only required to have the virtio bus as child in the system bus */
400 static int s390_virtio_bridge_init(SysBusDevice *dev)
402 /* nothing */
403 return 0;
406 static SysBusDeviceInfo s390_virtio_bridge_info = {
407 .init = s390_virtio_bridge_init,
408 .qdev.name = "s390-virtio-bridge",
409 .qdev.size = sizeof(SysBusDevice),
410 .qdev.no_user = 1,
413 static void s390_virtio_register_devices(void)
415 sysbus_register_withprop(&s390_virtio_bridge_info);
418 device_init(s390_virtio_register_devices)