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