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