lsi: pass lsi_request to lsi_reselect
[qemu/mdroth.git] / hw / s390-virtio-bus.c
blobdc154edb80381de549a14d561bd925919d3dbca0
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-console.h"
30 #include "hw/sysbus.h"
31 #include "kvm.h"
33 #include "hw/s390-virtio-bus.h"
35 /* #define DEBUG_S390 */
37 #ifdef DEBUG_S390
38 #define dprintf(fmt, ...) \
39 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
40 #else
41 #define dprintf(fmt, ...) \
42 do { } while (0)
43 #endif
45 struct BusInfo s390_virtio_bus_info = {
46 .name = "s390-virtio",
47 .size = sizeof(VirtIOS390Bus),
50 typedef struct {
51 DeviceInfo qdev;
52 int (*init)(VirtIOS390Device *dev);
53 } VirtIOS390DeviceInfo;
56 static const VirtIOBindings virtio_s390_bindings;
58 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
59 static void s390_virtio_device_sync(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 s390_virtio_device_sync(dev);
106 return 0;
109 static int s390_virtio_net_init(VirtIOS390Device *dev)
111 VirtIODevice *vdev;
113 vdev = virtio_net_init((DeviceState *)dev, &dev->nic);
114 if (!vdev) {
115 return -1;
118 return s390_virtio_device_init(dev, vdev);
121 static int s390_virtio_blk_init(VirtIOS390Device *dev)
123 VirtIODevice *vdev;
125 vdev = virtio_blk_init((DeviceState *)dev, dev->dinfo);
126 if (!vdev) {
127 return -1;
130 return s390_virtio_device_init(dev, vdev);
133 static int s390_virtio_console_init(VirtIOS390Device *dev)
135 VirtIOS390Bus *bus;
136 VirtIODevice *vdev;
137 int r;
139 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
141 vdev = virtio_console_init((DeviceState *)dev);
142 if (!vdev) {
143 return -1;
146 r = s390_virtio_device_init(dev, vdev);
147 if (!r) {
148 bus->console = dev;
151 return r;
154 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
156 ram_addr_t token_off;
158 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
159 (vq * VIRTIO_VQCONFIG_LEN) +
160 VIRTIO_VQCONFIG_OFFS_TOKEN;
162 return ldq_phys(token_off);
165 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
167 VirtIODevice *vdev = dev->vdev;
168 int num_vq;
170 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
171 if (!virtio_queue_get_num(vdev, num_vq)) {
172 break;
176 return num_vq;
179 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
181 ram_addr_t r = bus->next_ring;
183 bus->next_ring += VIRTIO_RING_LEN;
184 return r;
187 static void s390_virtio_device_sync(VirtIOS390Device *dev)
189 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
190 ram_addr_t cur_offs;
191 uint8_t num_vq;
192 int i;
194 virtio_reset(dev->vdev);
196 /* Sync dev space */
197 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
199 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
200 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
202 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
204 num_vq = s390_virtio_device_num_vq(dev);
205 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
207 /* Sync virtqueues */
208 for (i = 0; i < num_vq; i++) {
209 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
210 (i * VIRTIO_VQCONFIG_LEN);
211 ram_addr_t vring;
213 vring = s390_virtio_next_ring(bus);
214 virtio_queue_set_addr(dev->vdev, i, vring);
215 virtio_queue_set_vector(dev->vdev, i, i);
216 stq_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
217 stw_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
220 cur_offs = dev->dev_offs;
221 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
222 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
224 /* Sync feature bitmap */
225 if (dev->vdev->get_features) {
226 stl_phys(cur_offs, dev->vdev->get_features(dev->vdev));
229 dev->feat_offs = cur_offs + dev->feat_len;
230 cur_offs += dev->feat_len * 2;
232 /* Sync config space */
233 if (dev->vdev->get_config) {
234 dev->vdev->get_config(dev->vdev, dev->vdev->config);
237 cpu_physical_memory_rw(cur_offs, dev->vdev->config, dev->vdev->config_len, 1);
238 cur_offs += dev->vdev->config_len;
241 void s390_virtio_device_update_status(VirtIOS390Device *dev)
243 VirtIODevice *vdev = dev->vdev;
244 uint32_t features;
246 vdev->status = ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS);
248 /* Update guest supported feature bitmap */
250 features = ldl_phys(dev->feat_offs);
251 if (vdev->set_features) {
252 vdev->set_features(vdev, features);
254 vdev->features = features;
257 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
259 return bus->console;
262 /* Find a device by vring address */
263 VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
264 ram_addr_t mem,
265 int *vq_num)
267 VirtIOS390Device *_dev;
268 DeviceState *dev;
269 int i;
271 QLIST_FOREACH(dev, &bus->bus.children, sibling) {
272 _dev = (VirtIOS390Device *)dev;
273 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
274 if (!virtio_queue_get_addr(_dev->vdev, i))
275 break;
276 if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
277 if (vq_num) {
278 *vq_num = i;
280 return _dev;
285 return NULL;
288 /* Find a device by device descriptor location */
289 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
291 VirtIOS390Device *_dev;
292 DeviceState *dev;
294 QLIST_FOREACH(dev, &bus->bus.children, sibling) {
295 _dev = (VirtIOS390Device *)dev;
296 if (_dev->dev_offs == mem) {
297 return _dev;
301 return NULL;
304 static void virtio_s390_notify(void *opaque, uint16_t vector)
306 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
307 uint64_t token = s390_virtio_device_vq_token(dev, vector);
309 /* XXX kvm dependency! */
310 kvm_s390_virtio_irq(s390_cpu_addr2state(0), 0, token);
313 /**************** S390 Virtio Bus Device Descriptions *******************/
315 static const VirtIOBindings virtio_s390_bindings = {
316 .notify = virtio_s390_notify,
319 static VirtIOS390DeviceInfo s390_virtio_net = {
320 .init = s390_virtio_net_init,
321 .qdev.name = "virtio-net-s390",
322 .qdev.size = sizeof(VirtIOS390Device),
323 .qdev.props = (Property[]) {
324 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
325 DEFINE_PROP_END_OF_LIST(),
329 static VirtIOS390DeviceInfo s390_virtio_blk = {
330 .init = s390_virtio_blk_init,
331 .qdev.name = "virtio-blk-s390",
332 .qdev.size = sizeof(VirtIOS390Device),
333 .qdev.props = (Property[]) {
334 DEFINE_PROP_DRIVE("drive", VirtIOS390Device, dinfo),
335 DEFINE_PROP_END_OF_LIST(),
339 static VirtIOS390DeviceInfo s390_virtio_console = {
340 .init = s390_virtio_console_init,
341 .qdev.name = "virtio-console-s390",
342 .qdev.size = sizeof(VirtIOS390Device),
343 .qdev.props = (Property[]) {
344 DEFINE_PROP_END_OF_LIST(),
348 static int s390_virtio_busdev_init(DeviceState *dev, DeviceInfo *info)
350 VirtIOS390DeviceInfo *_info = (VirtIOS390DeviceInfo *)info;
351 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
353 return _info->init(_dev);
356 static void s390_virtio_bus_register_withprop(VirtIOS390DeviceInfo *info)
358 info->qdev.init = s390_virtio_busdev_init;
359 info->qdev.bus_info = &s390_virtio_bus_info;
361 assert(info->qdev.size >= sizeof(VirtIOS390Device));
362 qdev_register(&info->qdev);
365 static void s390_virtio_register(void)
367 s390_virtio_bus_register_withprop(&s390_virtio_console);
368 s390_virtio_bus_register_withprop(&s390_virtio_blk);
369 s390_virtio_bus_register_withprop(&s390_virtio_net);
371 device_init(s390_virtio_register);
374 /***************** S390 Virtio Bus Bridge Device *******************/
375 /* Only required to have the virtio bus as child in the system bus */
377 static int s390_virtio_bridge_init(SysBusDevice *dev)
379 /* nothing */
380 return 0;
383 static SysBusDeviceInfo s390_virtio_bridge_info = {
384 .init = s390_virtio_bridge_init,
385 .qdev.name = "s390-virtio-bridge",
386 .qdev.size = sizeof(SysBusDevice),
387 .qdev.no_user = 1,
390 static void s390_virtio_register_devices(void)
392 sysbus_register_withprop(&s390_virtio_bridge_info);
395 device_init(s390_virtio_register_devices)