]>
Commit | Line | Data |
---|---|---|
f3304eea AG |
1 | /* |
2 | * QEMU S390 virtio target | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <[email protected]> | |
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" | |
98b19252 | 29 | #include "hw/virtio-serial.h" |
f0c07c7c | 30 | #include "hw/virtio-net.h" |
f3304eea AG |
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 | ||
8103b4d1 AG |
46 | #define VIRTIO_EXT_CODE 0x2603 |
47 | ||
f3304eea AG |
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); | |
f3304eea | 62 | |
d1ff903c AG |
63 | /* length of VirtIO device pages */ |
64 | const target_phys_addr_t virtio_size = S390_DEVICE_PAGES * TARGET_PAGE_SIZE; | |
65 | ||
f3304eea AG |
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); | |
8172539d | 109 | dev->host_features = vdev->get_features(vdev, dev->host_features); |
f3304eea AG |
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 | ||
f0c07c7c | 119 | vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net); |
f3304eea AG |
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 | ||
ad509737 | 131 | vdev = virtio_blk_init((DeviceState *)dev, &dev->block); |
f3304eea AG |
132 | if (!vdev) { |
133 | return -1; | |
134 | } | |
135 | ||
136 | return s390_virtio_device_init(dev, vdev); | |
137 | } | |
138 | ||
98b19252 | 139 | static int s390_virtio_serial_init(VirtIOS390Device *dev) |
f3304eea AG |
140 | { |
141 | VirtIOS390Bus *bus; | |
142 | VirtIODevice *vdev; | |
143 | int r; | |
144 | ||
145 | bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus); | |
146 | ||
6be9b414 | 147 | vdev = virtio_serial_init((DeviceState *)dev, &dev->serial); |
f3304eea AG |
148 | if (!vdev) { |
149 | return -1; | |
150 | } | |
151 | ||
152 | r = s390_virtio_device_init(dev, vdev); | |
153 | if (!r) { | |
154 | bus->console = dev; | |
155 | } | |
156 | ||
157 | return r; | |
158 | } | |
159 | ||
160 | static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq) | |
161 | { | |
162 | ram_addr_t token_off; | |
163 | ||
164 | token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) + | |
165 | (vq * VIRTIO_VQCONFIG_LEN) + | |
166 | VIRTIO_VQCONFIG_OFFS_TOKEN; | |
167 | ||
168 | return ldq_phys(token_off); | |
169 | } | |
170 | ||
171 | static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev) | |
172 | { | |
173 | VirtIODevice *vdev = dev->vdev; | |
174 | int num_vq; | |
175 | ||
176 | for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) { | |
177 | if (!virtio_queue_get_num(vdev, num_vq)) { | |
178 | break; | |
179 | } | |
180 | } | |
181 | ||
182 | return num_vq; | |
183 | } | |
184 | ||
185 | static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus) | |
186 | { | |
187 | ram_addr_t r = bus->next_ring; | |
188 | ||
189 | bus->next_ring += VIRTIO_RING_LEN; | |
190 | return r; | |
191 | } | |
192 | ||
baf0b55a | 193 | void s390_virtio_device_sync(VirtIOS390Device *dev) |
f3304eea AG |
194 | { |
195 | VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus); | |
196 | ram_addr_t cur_offs; | |
197 | uint8_t num_vq; | |
198 | int i; | |
199 | ||
200 | virtio_reset(dev->vdev); | |
201 | ||
202 | /* Sync dev space */ | |
203 | stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id); | |
204 | ||
205 | stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev)); | |
206 | stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len); | |
207 | ||
208 | stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len); | |
209 | ||
210 | num_vq = s390_virtio_device_num_vq(dev); | |
211 | stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq); | |
212 | ||
213 | /* Sync virtqueues */ | |
214 | for (i = 0; i < num_vq; i++) { | |
215 | ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) + | |
216 | (i * VIRTIO_VQCONFIG_LEN); | |
217 | ram_addr_t vring; | |
218 | ||
219 | vring = s390_virtio_next_ring(bus); | |
220 | virtio_queue_set_addr(dev->vdev, i, vring); | |
221 | virtio_queue_set_vector(dev->vdev, i, i); | |
222 | stq_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring); | |
223 | stw_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i)); | |
224 | } | |
225 | ||
226 | cur_offs = dev->dev_offs; | |
227 | cur_offs += VIRTIO_DEV_OFFS_CONFIG; | |
228 | cur_offs += num_vq * VIRTIO_VQCONFIG_LEN; | |
229 | ||
230 | /* Sync feature bitmap */ | |
8f16753f | 231 | stl_phys(cur_offs, bswap32(dev->host_features)); |
f3304eea AG |
232 | |
233 | dev->feat_offs = cur_offs + dev->feat_len; | |
234 | cur_offs += dev->feat_len * 2; | |
235 | ||
236 | /* Sync config space */ | |
237 | if (dev->vdev->get_config) { | |
238 | dev->vdev->get_config(dev->vdev, dev->vdev->config); | |
239 | } | |
240 | ||
54f7b4a3 SW |
241 | cpu_physical_memory_write(cur_offs, |
242 | dev->vdev->config, dev->vdev->config_len); | |
f3304eea AG |
243 | cur_offs += dev->vdev->config_len; |
244 | } | |
245 | ||
246 | void s390_virtio_device_update_status(VirtIOS390Device *dev) | |
247 | { | |
248 | VirtIODevice *vdev = dev->vdev; | |
249 | uint32_t features; | |
250 | ||
3e607cb5 | 251 | virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS)); |
f3304eea AG |
252 | |
253 | /* Update guest supported feature bitmap */ | |
254 | ||
8f16753f | 255 | features = bswap32(ldl_phys(dev->feat_offs)); |
f3304eea AG |
256 | if (vdev->set_features) { |
257 | vdev->set_features(vdev, features); | |
258 | } | |
704a76fc | 259 | vdev->guest_features = features; |
f3304eea AG |
260 | } |
261 | ||
262 | VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus) | |
263 | { | |
264 | return bus->console; | |
265 | } | |
266 | ||
267 | /* Find a device by vring address */ | |
268 | VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus, | |
269 | ram_addr_t mem, | |
270 | int *vq_num) | |
271 | { | |
272 | VirtIOS390Device *_dev; | |
273 | DeviceState *dev; | |
274 | int i; | |
275 | ||
276 | QLIST_FOREACH(dev, &bus->bus.children, sibling) { | |
277 | _dev = (VirtIOS390Device *)dev; | |
278 | for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
279 | if (!virtio_queue_get_addr(_dev->vdev, i)) | |
280 | break; | |
281 | if (virtio_queue_get_addr(_dev->vdev, i) == mem) { | |
282 | if (vq_num) { | |
283 | *vq_num = i; | |
284 | } | |
285 | return _dev; | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | return NULL; | |
291 | } | |
292 | ||
293 | /* Find a device by device descriptor location */ | |
294 | VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem) | |
295 | { | |
296 | VirtIOS390Device *_dev; | |
297 | DeviceState *dev; | |
298 | ||
299 | QLIST_FOREACH(dev, &bus->bus.children, sibling) { | |
300 | _dev = (VirtIOS390Device *)dev; | |
301 | if (_dev->dev_offs == mem) { | |
302 | return _dev; | |
303 | } | |
304 | } | |
305 | ||
306 | return NULL; | |
307 | } | |
308 | ||
309 | static void virtio_s390_notify(void *opaque, uint16_t vector) | |
310 | { | |
311 | VirtIOS390Device *dev = (VirtIOS390Device*)opaque; | |
312 | uint64_t token = s390_virtio_device_vq_token(dev, vector); | |
8103b4d1 | 313 | CPUState *env = s390_cpu_addr2state(0); |
f3304eea | 314 | |
8103b4d1 AG |
315 | if (kvm_enabled()) { |
316 | kvm_s390_virtio_irq(env, 0, token); | |
317 | } else { | |
318 | cpu_inject_ext(env, VIRTIO_EXT_CODE, 0, token); | |
319 | } | |
f3304eea AG |
320 | } |
321 | ||
8172539d MT |
322 | static unsigned virtio_s390_get_features(void *opaque) |
323 | { | |
324 | VirtIOS390Device *dev = (VirtIOS390Device*)opaque; | |
325 | return dev->host_features; | |
326 | } | |
327 | ||
f3304eea AG |
328 | /**************** S390 Virtio Bus Device Descriptions *******************/ |
329 | ||
330 | static const VirtIOBindings virtio_s390_bindings = { | |
331 | .notify = virtio_s390_notify, | |
8172539d | 332 | .get_features = virtio_s390_get_features, |
f3304eea AG |
333 | }; |
334 | ||
335 | static VirtIOS390DeviceInfo s390_virtio_net = { | |
336 | .init = s390_virtio_net_init, | |
337 | .qdev.name = "virtio-net-s390", | |
29f82b37 | 338 | .qdev.alias = "virtio-net", |
f3304eea AG |
339 | .qdev.size = sizeof(VirtIOS390Device), |
340 | .qdev.props = (Property[]) { | |
341 | DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic), | |
f0c07c7c AW |
342 | DEFINE_PROP_UINT32("x-txtimer", VirtIOS390Device, |
343 | net.txtimer, TX_TIMER_INTERVAL), | |
e3f30488 AW |
344 | DEFINE_PROP_INT32("x-txburst", VirtIOS390Device, |
345 | net.txburst, TX_BURST), | |
a697a334 | 346 | DEFINE_PROP_STRING("tx", VirtIOS390Device, net.tx), |
f3304eea AG |
347 | DEFINE_PROP_END_OF_LIST(), |
348 | }, | |
349 | }; | |
350 | ||
351 | static VirtIOS390DeviceInfo s390_virtio_blk = { | |
352 | .init = s390_virtio_blk_init, | |
353 | .qdev.name = "virtio-blk-s390", | |
29f82b37 | 354 | .qdev.alias = "virtio-blk", |
f3304eea AG |
355 | .qdev.size = sizeof(VirtIOS390Device), |
356 | .qdev.props = (Property[]) { | |
428c149b | 357 | DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, block), |
f3304eea AG |
358 | DEFINE_PROP_END_OF_LIST(), |
359 | }, | |
360 | }; | |
361 | ||
98b19252 AS |
362 | static VirtIOS390DeviceInfo s390_virtio_serial = { |
363 | .init = s390_virtio_serial_init, | |
364 | .qdev.name = "virtio-serial-s390", | |
365 | .qdev.alias = "virtio-serial", | |
f3304eea AG |
366 | .qdev.size = sizeof(VirtIOS390Device), |
367 | .qdev.props = (Property[]) { | |
6be9b414 AG |
368 | DEFINE_PROP_UINT32("max_ports", VirtIOS390Device, |
369 | serial.max_virtserial_ports, 31), | |
f3304eea AG |
370 | DEFINE_PROP_END_OF_LIST(), |
371 | }, | |
372 | }; | |
373 | ||
374 | static int s390_virtio_busdev_init(DeviceState *dev, DeviceInfo *info) | |
375 | { | |
376 | VirtIOS390DeviceInfo *_info = (VirtIOS390DeviceInfo *)info; | |
377 | VirtIOS390Device *_dev = (VirtIOS390Device *)dev; | |
378 | ||
379 | return _info->init(_dev); | |
380 | } | |
381 | ||
382 | static void s390_virtio_bus_register_withprop(VirtIOS390DeviceInfo *info) | |
383 | { | |
384 | info->qdev.init = s390_virtio_busdev_init; | |
385 | info->qdev.bus_info = &s390_virtio_bus_info; | |
386 | ||
387 | assert(info->qdev.size >= sizeof(VirtIOS390Device)); | |
388 | qdev_register(&info->qdev); | |
389 | } | |
390 | ||
391 | static void s390_virtio_register(void) | |
392 | { | |
98b19252 | 393 | s390_virtio_bus_register_withprop(&s390_virtio_serial); |
f3304eea AG |
394 | s390_virtio_bus_register_withprop(&s390_virtio_blk); |
395 | s390_virtio_bus_register_withprop(&s390_virtio_net); | |
396 | } | |
397 | device_init(s390_virtio_register); | |
398 | ||
399 | ||
400 | /***************** S390 Virtio Bus Bridge Device *******************/ | |
401 | /* Only required to have the virtio bus as child in the system bus */ | |
402 | ||
403 | static int s390_virtio_bridge_init(SysBusDevice *dev) | |
404 | { | |
405 | /* nothing */ | |
406 | return 0; | |
407 | } | |
408 | ||
409 | static SysBusDeviceInfo s390_virtio_bridge_info = { | |
410 | .init = s390_virtio_bridge_init, | |
411 | .qdev.name = "s390-virtio-bridge", | |
412 | .qdev.size = sizeof(SysBusDevice), | |
413 | .qdev.no_user = 1, | |
414 | }; | |
415 | ||
416 | static void s390_virtio_register_devices(void) | |
417 | { | |
418 | sysbus_register_withprop(&s390_virtio_bridge_info); | |
419 | } | |
420 | ||
421 | device_init(s390_virtio_register_devices) |