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