qdev: don't access name through info
[qemu.git] / hw / s390-virtio-bus.c
1 /*
2 * QEMU S390 virtio target
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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
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"
33
34 #include "hw/s390-virtio-bus.h"
35
36 /* #define DEBUG_S390 */
37
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
45
46 #define VIRTIO_EXT_CODE 0x2603
47
48 struct BusInfo s390_virtio_bus_info = {
49 .name = "s390-virtio",
50 .size = sizeof(VirtIOS390Bus),
51 };
52
53 typedef struct {
54 DeviceInfo qdev;
55 int (*init)(VirtIOS390Device *dev);
56 } VirtIOS390DeviceInfo;
57
58
59 static const VirtIOBindings virtio_s390_bindings;
60
61 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
62
63 /* length of VirtIO device pages */
64 const target_phys_addr_t virtio_size = S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
65
66 VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
67 {
68 VirtIOS390Bus *bus;
69 BusState *_bus;
70 DeviceState *dev;
71
72 /* Create bridge device */
73 dev = qdev_create(NULL, "s390-virtio-bridge");
74 qdev_init_nofail(dev);
75
76 /* Create bus on bridge device */
77
78 _bus = qbus_create(&s390_virtio_bus_info, dev, "s390-virtio");
79 bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
80
81 bus->dev_page = *ram_size;
82 bus->dev_offs = bus->dev_page;
83 bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
84
85 /* Enable hotplugging */
86 _bus->allow_hotplug = 1;
87
88 /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
89 *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
90
91 return bus;
92 }
93
94 static void s390_virtio_irq(CPUState *env, int config_change, uint64_t token)
95 {
96 if (kvm_enabled()) {
97 kvm_s390_virtio_irq(env, config_change, token);
98 } else {
99 cpu_inject_ext(env, VIRTIO_EXT_CODE, config_change, token);
100 }
101 }
102
103 static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
104 {
105 VirtIOS390Bus *bus;
106 int dev_len;
107
108 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
109 dev->vdev = vdev;
110 dev->dev_offs = bus->dev_offs;
111 dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */
112
113 dev_len = VIRTIO_DEV_OFFS_CONFIG;
114 dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN;
115 dev_len += dev->feat_len * 2;
116 dev_len += vdev->config_len;
117
118 bus->dev_offs += dev_len;
119
120 virtio_bind_device(vdev, &virtio_s390_bindings, dev);
121 dev->host_features = vdev->get_features(vdev, dev->host_features);
122 s390_virtio_device_sync(dev);
123
124 if (dev->qdev.hotplugged) {
125 CPUState *env = s390_cpu_addr2state(0);
126 s390_virtio_irq(env, VIRTIO_PARAM_DEV_ADD, dev->dev_offs);
127 }
128
129 return 0;
130 }
131
132 static int s390_virtio_net_init(VirtIOS390Device *dev)
133 {
134 VirtIODevice *vdev;
135
136 vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net);
137 if (!vdev) {
138 return -1;
139 }
140
141 return s390_virtio_device_init(dev, vdev);
142 }
143
144 static int s390_virtio_blk_init(VirtIOS390Device *dev)
145 {
146 VirtIODevice *vdev;
147
148 vdev = virtio_blk_init((DeviceState *)dev, &dev->block,
149 &dev->block_serial);
150 if (!vdev) {
151 return -1;
152 }
153
154 return s390_virtio_device_init(dev, vdev);
155 }
156
157 static int s390_virtio_serial_init(VirtIOS390Device *dev)
158 {
159 VirtIOS390Bus *bus;
160 VirtIODevice *vdev;
161 int r;
162
163 bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
164
165 vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
166 if (!vdev) {
167 return -1;
168 }
169
170 r = s390_virtio_device_init(dev, vdev);
171 if (!r) {
172 bus->console = dev;
173 }
174
175 return r;
176 }
177
178 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
179 {
180 ram_addr_t token_off;
181
182 token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
183 (vq * VIRTIO_VQCONFIG_LEN) +
184 VIRTIO_VQCONFIG_OFFS_TOKEN;
185
186 return ldq_be_phys(token_off);
187 }
188
189 static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
190 {
191 VirtIODevice *vdev = dev->vdev;
192 int num_vq;
193
194 for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
195 if (!virtio_queue_get_num(vdev, num_vq)) {
196 break;
197 }
198 }
199
200 return num_vq;
201 }
202
203 static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
204 {
205 ram_addr_t r = bus->next_ring;
206
207 bus->next_ring += VIRTIO_RING_LEN;
208 return r;
209 }
210
211 void s390_virtio_device_sync(VirtIOS390Device *dev)
212 {
213 VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
214 ram_addr_t cur_offs;
215 uint8_t num_vq;
216 int i;
217
218 virtio_reset(dev->vdev);
219
220 /* Sync dev space */
221 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
222
223 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
224 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
225
226 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
227
228 num_vq = s390_virtio_device_num_vq(dev);
229 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
230
231 /* Sync virtqueues */
232 for (i = 0; i < num_vq; i++) {
233 ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
234 (i * VIRTIO_VQCONFIG_LEN);
235 ram_addr_t vring;
236
237 vring = s390_virtio_next_ring(bus);
238 virtio_queue_set_addr(dev->vdev, i, vring);
239 virtio_queue_set_vector(dev->vdev, i, i);
240 stq_be_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
241 stw_be_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
242 }
243
244 cur_offs = dev->dev_offs;
245 cur_offs += VIRTIO_DEV_OFFS_CONFIG;
246 cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
247
248 /* Sync feature bitmap */
249 stl_le_phys(cur_offs, dev->host_features);
250
251 dev->feat_offs = cur_offs + dev->feat_len;
252 cur_offs += dev->feat_len * 2;
253
254 /* Sync config space */
255 if (dev->vdev->get_config) {
256 dev->vdev->get_config(dev->vdev, dev->vdev->config);
257 }
258
259 cpu_physical_memory_write(cur_offs,
260 dev->vdev->config, dev->vdev->config_len);
261 cur_offs += dev->vdev->config_len;
262 }
263
264 void s390_virtio_device_update_status(VirtIOS390Device *dev)
265 {
266 VirtIODevice *vdev = dev->vdev;
267 uint32_t features;
268
269 virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
270
271 /* Update guest supported feature bitmap */
272
273 features = bswap32(ldl_be_phys(dev->feat_offs));
274 virtio_set_features(vdev, features);
275 }
276
277 VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
278 {
279 return bus->console;
280 }
281
282 /* Find a device by vring address */
283 VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
284 ram_addr_t mem,
285 int *vq_num)
286 {
287 VirtIOS390Device *_dev;
288 DeviceState *dev;
289 int i;
290
291 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
292 _dev = (VirtIOS390Device *)dev;
293 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
294 if (!virtio_queue_get_addr(_dev->vdev, i))
295 break;
296 if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
297 if (vq_num) {
298 *vq_num = i;
299 }
300 return _dev;
301 }
302 }
303 }
304
305 return NULL;
306 }
307
308 /* Find a device by device descriptor location */
309 VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
310 {
311 VirtIOS390Device *_dev;
312 DeviceState *dev;
313
314 QTAILQ_FOREACH(dev, &bus->bus.children, sibling) {
315 _dev = (VirtIOS390Device *)dev;
316 if (_dev->dev_offs == mem) {
317 return _dev;
318 }
319 }
320
321 return NULL;
322 }
323
324 static void virtio_s390_notify(void *opaque, uint16_t vector)
325 {
326 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
327 uint64_t token = s390_virtio_device_vq_token(dev, vector);
328 CPUState *env = s390_cpu_addr2state(0);
329
330 s390_virtio_irq(env, 0, token);
331 }
332
333 static unsigned virtio_s390_get_features(void *opaque)
334 {
335 VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
336 return dev->host_features;
337 }
338
339 /**************** S390 Virtio Bus Device Descriptions *******************/
340
341 static const VirtIOBindings virtio_s390_bindings = {
342 .notify = virtio_s390_notify,
343 .get_features = virtio_s390_get_features,
344 };
345
346 static VirtIOS390DeviceInfo s390_virtio_net = {
347 .init = s390_virtio_net_init,
348 .qdev.name = "virtio-net-s390",
349 .qdev.alias = "virtio-net",
350 .qdev.size = sizeof(VirtIOS390Device),
351 .qdev.props = (Property[]) {
352 DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
353 DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device,
354 net.txtimer, TX_TIMER_INTERVAL),
355 DEFINE_PROP_INT32("x-txburst", VirtIOS390Device,
356 net.txburst, TX_BURST),
357 DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx),
358 DEFINE_PROP_END_OF_LIST(),
359 },
360 };
361
362 static VirtIOS390DeviceInfo s390_virtio_blk = {
363 .init = s390_virtio_blk_init,
364 .qdev.name = "virtio-blk-s390",
365 .qdev.alias = "virtio-blk",
366 .qdev.size = sizeof(VirtIOS390Device),
367 .qdev.props = (Property[]) {
368 DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, block),
369 DEFINE_PROP_STRING("serial", VirtIOS390Device, block_serial),
370 DEFINE_PROP_END_OF_LIST(),
371 },
372 };
373
374 static VirtIOS390DeviceInfo s390_virtio_serial = {
375 .init = s390_virtio_serial_init,
376 .qdev.name = "virtio-serial-s390",
377 .qdev.alias = "virtio-serial",
378 .qdev.size = sizeof(VirtIOS390Device),
379 .qdev.props = (Property[]) {
380 DEFINE_PROP_UINT32("max_ports", VirtIOS390Device,
381 serial.max_virtserial_ports, 31),
382 DEFINE_PROP_END_OF_LIST(),
383 },
384 };
385
386 static int s390_virtio_busdev_init(DeviceState *dev, DeviceInfo *info)
387 {
388 VirtIOS390DeviceInfo *_info = (VirtIOS390DeviceInfo *)info;
389 VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
390
391 return _info->init(_dev);
392 }
393
394 static void s390_virtio_bus_register_withprop(VirtIOS390DeviceInfo *info)
395 {
396 info->qdev.init = s390_virtio_busdev_init;
397 info->qdev.bus_info = &s390_virtio_bus_info;
398 info->qdev.unplug = qdev_simple_unplug_cb;
399
400 assert(info->qdev.size >= sizeof(VirtIOS390Device));
401 qdev_register(&info->qdev);
402 }
403
404 static void s390_virtio_register(void)
405 {
406 s390_virtio_bus_register_withprop(&s390_virtio_serial);
407 s390_virtio_bus_register_withprop(&s390_virtio_blk);
408 s390_virtio_bus_register_withprop(&s390_virtio_net);
409 }
410 device_init(s390_virtio_register);
411
412
413 /***************** S390 Virtio Bus Bridge Device *******************/
414 /* Only required to have the virtio bus as child in the system bus */
415
416 static int s390_virtio_bridge_init(SysBusDevice *dev)
417 {
418 /* nothing */
419 return 0;
420 }
421
422 static SysBusDeviceInfo s390_virtio_bridge_info = {
423 .init = s390_virtio_bridge_init,
424 .qdev.name = "s390-virtio-bridge",
425 .qdev.size = sizeof(SysBusDevice),
426 .qdev.no_user = 1,
427 };
428
429 static void s390_virtio_register_devices(void)
430 {
431 sysbus_register_withprop(&s390_virtio_bridge_info);
432 }
433
434 device_init(s390_virtio_register_devices)