scsi-disk: propagate the required alignment
[qemu/kevin.git] / hw / s390-virtio-bus.c
blob784dc01b97d2e5a7f7fc48e8b4967cbc81e94b3c
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 struct BusInfo s390_virtio_bus_info = {
47 .name = "s390-virtio",
48 .size = sizeof(VirtIOS390Bus),
51 typedef struct {
52 DeviceInfo qdev;
53 int (*init)(VirtIOS390Device *dev);
54 } VirtIOS390DeviceInfo;
57 static const VirtIOBindings virtio_s390_bindings;
59 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
61 VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
63 VirtIOS390Bus *bus;
64 BusState *_bus;
65 DeviceState *dev;
67 /* Create bridge device */
68 dev = qdev_create(NULL, "s390-virtio-bridge");
69 qdev_init_nofail(dev);
71 /* Create bus on bridge device */
73 _bus = qbus_create(&s390_virtio_bus_info, dev, "s390-virtio");
74 bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
76 bus->dev_page = *ram_size;
77 bus->dev_offs = bus->dev_page;
78 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
80 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
81 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
83 return bus;
86 static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
88 VirtIOS390Bus *bus;
89 int dev_len;
91 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
92 dev->vdev = vdev;
93 dev->dev_offs = bus->dev_offs;
94 dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */
96 dev_len = VIRTIO_DEV_OFFS_CONFIG;
97 dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN;
98 dev_len += dev->feat_len * 2;
99 dev_len += vdev->config_len;
101 bus->dev_offs += dev_len;
103 virtio_bind_device(vdev, &virtio_s390_bindings, dev);
104 dev->host_features = vdev->get_features(vdev, dev->host_features);
105 s390_virtio_device_sync(dev);
107 return 0;
110 static int s390_virtio_net_init(VirtIOS390Device *dev)
112 VirtIODevice *vdev;
114 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
115 if (!vdev) {
116 return -1;
119 return s390_virtio_device_init(dev, vdev);
122 static int s390_virtio_blk_init(VirtIOS390Device *dev)
124 VirtIODevice *vdev;
126 vdev = virtio_blk_init((DeviceState *)dev, &dev->block);
127 if (!vdev) {
128 return -1;
131 return s390_virtio_device_init(dev, vdev);
134 static int s390_virtio_serial_init(VirtIOS390Device *dev)
136 VirtIOS390Bus *bus;
137 VirtIODevice *vdev;
138 int r;
140 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
142 vdev = virtio_serial_init((DeviceState *)dev, dev->max_virtserial_ports);
143 if (!vdev) {
144 return -1;
147 r = s390_virtio_device_init(dev, vdev);
148 if (!r) {
149 bus->console = dev;
152 return r;
155 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
157 ram_addr_t token_off;
159 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
160 (vq * VIRTIO_VQCONFIG_LEN) +
161 VIRTIO_VQCONFIG_OFFS_TOKEN;
163 return ldq_phys(token_off);
166 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
168 VirtIODevice *vdev = dev->vdev;
169 int num_vq;
171 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
172 if (!virtio_queue_get_num(vdev, num_vq)) {
173 break;
177 return num_vq;
180 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
182 ram_addr_t r = bus->next_ring;
184 bus->next_ring += VIRTIO_RING_LEN;
185 return r;
188 void s390_virtio_device_sync(VirtIOS390Device *dev)
190 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
191 ram_addr_t cur_offs;
192 uint8_t num_vq;
193 int i;
195 virtio_reset(dev->vdev);
197 /* Sync dev space */
198 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
200 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
201 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
203 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
205 num_vq = s390_virtio_device_num_vq(dev);
206 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
208 /* Sync virtqueues */
209 for (i = 0; i < num_vq; i++) {
210 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
211 (i * VIRTIO_VQCONFIG_LEN);
212 ram_addr_t vring;
214 vring = s390_virtio_next_ring(bus);
215 virtio_queue_set_addr(dev->vdev, i, vring);
216 virtio_queue_set_vector(dev->vdev, i, i);
217 stq_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
218 stw_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
221 cur_offs = dev->dev_offs;
222 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
223 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
225 /* Sync feature bitmap */
226 stl_phys(cur_offs, dev->host_features);
228 dev->feat_offs = cur_offs + dev->feat_len;
229 cur_offs += dev->feat_len * 2;
231 /* Sync config space */
232 if (dev->vdev->get_config) {
233 dev->vdev->get_config(dev->vdev, dev->vdev->config);
236 cpu_physical_memory_rw(cur_offs, dev->vdev->config, dev->vdev->config_len, 1);
237 cur_offs += dev->vdev->config_len;
240 void s390_virtio_device_update_status(VirtIOS390Device *dev)
242 VirtIODevice *vdev = dev->vdev;
243 uint32_t features;
245 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
247 /* Update guest supported feature bitmap */
249 features = ldl_phys(dev->feat_offs);
250 if (vdev->set_features) {
251 vdev->set_features(vdev, features);
253 vdev->guest_features = features;
256 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
258 return bus->console;
261 /* Find a device by vring address */
262 VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
263 ram_addr_t mem,
264 int *vq_num)
266 VirtIOS390Device *_dev;
267 DeviceState *dev;
268 int i;
270 QLIST_FOREACH(dev, &bus->bus.children, sibling) {
271 _dev = (VirtIOS390Device *)dev;
272 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
273 if (!virtio_queue_get_addr(_dev->vdev, i))
274 break;
275 if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
276 if (vq_num) {
277 *vq_num = i;
279 return _dev;
284 return NULL;
287 /* Find a device by device descriptor location */
288 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
290 VirtIOS390Device *_dev;
291 DeviceState *dev;
293 QLIST_FOREACH(dev, &bus->bus.children, sibling) {
294 _dev = (VirtIOS390Device *)dev;
295 if (_dev->dev_offs == mem) {
296 return _dev;
300 return NULL;
303 static void virtio_s390_notify(void *opaque, uint16_t vector)
305 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
306 uint64_t token = s390_virtio_device_vq_token(dev, vector);
308 /* XXX kvm dependency! */
309 kvm_s390_virtio_irq(s390_cpu_addr2state(0), 0, token);
312 static unsigned virtio_s390_get_features(void *opaque)
314 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
315 return dev->host_features;
318 /**************** S390 Virtio Bus Device Descriptions *******************/
320 static const VirtIOBindings virtio_s390_bindings = {
321 .notify = virtio_s390_notify,
322 .get_features = virtio_s390_get_features,
325 static VirtIOS390DeviceInfo s390_virtio_net = {
326 .init = s390_virtio_net_init,
327 .qdev.name = "virtio-net-s390",
328 .qdev.size = sizeof(VirtIOS390Device),
329 .qdev.props = (Property[]) {
330 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
331 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
332 net.txtimer, TX_TIMER_INTERVAL),
333 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
334 net.txburst, TX_BURST),
335 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
336 DEFINE_PROP_END_OF_LIST(),
340 static VirtIOS390DeviceInfo s390_virtio_blk = {
341 .init = s390_virtio_blk_init,
342 .qdev.name = "virtio-blk-s390",
343 .qdev.size = sizeof(VirtIOS390Device),
344 .qdev.props = (Property[]) {
345 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, block),
346 DEFINE_PROP_END_OF_LIST(),
350 static VirtIOS390DeviceInfo s390_virtio_serial = {
351 .init = s390_virtio_serial_init,
352 .qdev.name = "virtio-serial-s390",
353 .qdev.alias = "virtio-serial",
354 .qdev.size = sizeof(VirtIOS390Device),
355 .qdev.props = (Property[]) {
356 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device, max_virtserial_ports,
357 31),
358 DEFINE_PROP_END_OF_LIST(),
362 static int s390_virtio_busdev_init(DeviceState *dev, DeviceInfo *info)
364 VirtIOS390DeviceInfo *_info = (VirtIOS390DeviceInfo *)info;
365 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
367 return _info->init(_dev);
370 static void s390_virtio_bus_register_withprop(VirtIOS390DeviceInfo *info)
372 info->qdev.init = s390_virtio_busdev_init;
373 info->qdev.bus_info = &s390_virtio_bus_info;
375 assert(info->qdev.size >= sizeof(VirtIOS390Device));
376 qdev_register(&info->qdev);
379 static void s390_virtio_register(void)
381 s390_virtio_bus_register_withprop(&s390_virtio_serial);
382 s390_virtio_bus_register_withprop(&s390_virtio_blk);
383 s390_virtio_bus_register_withprop(&s390_virtio_net);
385 device_init(s390_virtio_register);
388 /***************** S390 Virtio Bus Bridge Device *******************/
389 /* Only required to have the virtio bus as child in the system bus */
391 static int s390_virtio_bridge_init(SysBusDevice *dev)
393 /* nothing */
394 return 0;
397 static SysBusDeviceInfo s390_virtio_bridge_info = {
398 .init = s390_virtio_bridge_init,
399 .qdev.name = "s390-virtio-bridge",
400 .qdev.size = sizeof(SysBusDevice),
401 .qdev.no_user = 1,
404 static void s390_virtio_register_devices(void)
406 sysbus_register_withprop(&s390_virtio_bridge_info);
409 device_init(s390_virtio_register_devices)