/src/qemu/hw/virtio/virtio-mmio.c
Line | Count | Source |
1 | | /* |
2 | | * Virtio MMIO bindings |
3 | | * |
4 | | * Copyright (c) 2011 Linaro Limited |
5 | | * |
6 | | * Author: |
7 | | * Peter Maydell <peter.maydell@linaro.org> |
8 | | * |
9 | | * This program is free software; you can redistribute it and/or modify |
10 | | * it under the terms of the GNU General Public License; either version 2 |
11 | | * of the License, or (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License along |
19 | | * with this program; if not, see <http://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include "qemu/osdep.h" |
23 | | #include "standard-headers/linux/virtio_mmio.h" |
24 | | #include "hw/core/irq.h" |
25 | | #include "hw/core/qdev-properties.h" |
26 | | #include "hw/core/sysbus.h" |
27 | | #include "hw/virtio/virtio.h" |
28 | | #include "migration/qemu-file-types.h" |
29 | | #include "qemu/host-utils.h" |
30 | | #include "qemu/module.h" |
31 | | #include "system/kvm.h" |
32 | | #include "system/replay.h" |
33 | | #include "hw/virtio/virtio-mmio.h" |
34 | | #include "qemu/error-report.h" |
35 | | #include "qemu/log.h" |
36 | | #include "trace.h" |
37 | | #include "qapi/error.h" |
38 | | |
39 | | static bool virtio_mmio_ioeventfd_enabled(DeviceState *d) |
40 | 0 | { |
41 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); |
42 | |
|
43 | 0 | return (proxy->flags & VIRTIO_IOMMIO_FLAG_USE_IOEVENTFD) != 0; |
44 | 0 | } |
45 | | |
46 | | static int virtio_mmio_ioeventfd_assign(DeviceState *d, |
47 | | EventNotifier *notifier, |
48 | | int n, bool assign) |
49 | 0 | { |
50 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); |
51 | |
|
52 | 0 | if (assign) { |
53 | 0 | memory_region_add_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUE_NOTIFY, 4, |
54 | 0 | true, n, notifier); |
55 | 0 | } else { |
56 | 0 | memory_region_del_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUE_NOTIFY, 4, |
57 | 0 | true, n, notifier); |
58 | 0 | } |
59 | 0 | return 0; |
60 | 0 | } |
61 | | |
62 | | static void virtio_mmio_start_ioeventfd(VirtIOMMIOProxy *proxy) |
63 | 0 | { |
64 | 0 | virtio_bus_start_ioeventfd(&proxy->bus); |
65 | 0 | } |
66 | | |
67 | | static void virtio_mmio_stop_ioeventfd(VirtIOMMIOProxy *proxy) |
68 | 0 | { |
69 | 0 | virtio_bus_stop_ioeventfd(&proxy->bus); |
70 | 0 | } |
71 | | |
72 | | static void virtio_mmio_soft_reset(VirtIOMMIOProxy *proxy) |
73 | 0 | { |
74 | 0 | int i; |
75 | |
|
76 | 0 | virtio_bus_reset(&proxy->bus); |
77 | |
|
78 | 0 | if (!proxy->legacy) { |
79 | 0 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
80 | 0 | proxy->vqs[i].enabled = 0; |
81 | 0 | } |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size) |
86 | 0 | { |
87 | 0 | VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque; |
88 | 0 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
89 | |
|
90 | 0 | trace_virtio_mmio_read(offset); |
91 | |
|
92 | 0 | if (!vdev) { |
93 | | /* If no backend is present, we treat most registers as |
94 | | * read-as-zero, except for the magic number, version and |
95 | | * vendor ID. This is not strictly sanctioned by the virtio |
96 | | * spec, but it allows us to provide transports with no backend |
97 | | * plugged in which don't confuse Linux's virtio code: the |
98 | | * probe won't complain about the bad magic number, but the |
99 | | * device ID of zero means no backend will claim it. |
100 | | */ |
101 | 0 | switch (offset) { |
102 | 0 | case VIRTIO_MMIO_MAGIC_VALUE: |
103 | 0 | return VIRT_MAGIC; |
104 | 0 | case VIRTIO_MMIO_VERSION: |
105 | 0 | if (proxy->legacy) { |
106 | 0 | return VIRT_VERSION_LEGACY; |
107 | 0 | } else { |
108 | 0 | return VIRT_VERSION; |
109 | 0 | } |
110 | 0 | case VIRTIO_MMIO_VENDOR_ID: |
111 | 0 | return VIRT_VENDOR; |
112 | 0 | default: |
113 | 0 | return 0; |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | if (offset >= VIRTIO_MMIO_CONFIG) { |
118 | 0 | offset -= VIRTIO_MMIO_CONFIG; |
119 | 0 | if (proxy->legacy) { |
120 | 0 | switch (size) { |
121 | 0 | case 1: |
122 | 0 | return virtio_config_readb(vdev, offset); |
123 | 0 | case 2: |
124 | 0 | return virtio_config_readw(vdev, offset); |
125 | 0 | case 4: |
126 | 0 | return virtio_config_readl(vdev, offset); |
127 | 0 | default: |
128 | 0 | abort(); |
129 | 0 | } |
130 | 0 | } else { |
131 | 0 | switch (size) { |
132 | 0 | case 1: |
133 | 0 | return virtio_config_modern_readb(vdev, offset); |
134 | 0 | case 2: |
135 | 0 | return virtio_config_modern_readw(vdev, offset); |
136 | 0 | case 4: |
137 | 0 | return virtio_config_modern_readl(vdev, offset); |
138 | 0 | default: |
139 | 0 | abort(); |
140 | 0 | } |
141 | 0 | } |
142 | 0 | } |
143 | 0 | if (size != 4) { |
144 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
145 | 0 | "%s: wrong size access to register!\n", |
146 | 0 | __func__); |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | switch (offset) { |
150 | 0 | case VIRTIO_MMIO_MAGIC_VALUE: |
151 | 0 | return VIRT_MAGIC; |
152 | 0 | case VIRTIO_MMIO_VERSION: |
153 | 0 | if (proxy->legacy) { |
154 | 0 | return VIRT_VERSION_LEGACY; |
155 | 0 | } else { |
156 | 0 | return VIRT_VERSION; |
157 | 0 | } |
158 | 0 | case VIRTIO_MMIO_DEVICE_ID: |
159 | 0 | return vdev->device_id; |
160 | 0 | case VIRTIO_MMIO_VENDOR_ID: |
161 | 0 | return VIRT_VENDOR; |
162 | 0 | case VIRTIO_MMIO_DEVICE_FEATURES: |
163 | 0 | if (proxy->legacy) { |
164 | 0 | if (proxy->host_features_sel) { |
165 | 0 | return 0; |
166 | 0 | } else { |
167 | 0 | return vdev->host_features; |
168 | 0 | } |
169 | 0 | } else { |
170 | 0 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
171 | 0 | return (vdev->host_features & ~vdc->legacy_features) |
172 | 0 | >> (32 * proxy->host_features_sel); |
173 | 0 | } |
174 | 0 | case VIRTIO_MMIO_QUEUE_NUM_MAX: |
175 | 0 | return virtio_queue_get_max_num(vdev, vdev->queue_sel); |
176 | 0 | case VIRTIO_MMIO_QUEUE_PFN: |
177 | 0 | if (!proxy->legacy) { |
178 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
179 | 0 | "%s: read from legacy register (0x%" |
180 | 0 | HWADDR_PRIx ") in non-legacy mode\n", |
181 | 0 | __func__, offset); |
182 | 0 | return 0; |
183 | 0 | } |
184 | 0 | return virtio_queue_get_addr(vdev, vdev->queue_sel) |
185 | 0 | >> proxy->guest_page_shift; |
186 | 0 | case VIRTIO_MMIO_QUEUE_READY: |
187 | 0 | if (proxy->legacy) { |
188 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
189 | 0 | "%s: read from non-legacy register (0x%" |
190 | 0 | HWADDR_PRIx ") in legacy mode\n", |
191 | 0 | __func__, offset); |
192 | 0 | return 0; |
193 | 0 | } |
194 | 0 | return proxy->vqs[vdev->queue_sel].enabled; |
195 | 0 | case VIRTIO_MMIO_INTERRUPT_STATUS: |
196 | 0 | return qatomic_read(&vdev->isr); |
197 | 0 | case VIRTIO_MMIO_STATUS: |
198 | 0 | return vdev->status; |
199 | 0 | case VIRTIO_MMIO_CONFIG_GENERATION: |
200 | 0 | if (proxy->legacy) { |
201 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
202 | 0 | "%s: read from non-legacy register (0x%" |
203 | 0 | HWADDR_PRIx ") in legacy mode\n", |
204 | 0 | __func__, offset); |
205 | 0 | return 0; |
206 | 0 | } |
207 | 0 | return vdev->generation; |
208 | 0 | case VIRTIO_MMIO_SHM_LEN_LOW: |
209 | 0 | case VIRTIO_MMIO_SHM_LEN_HIGH: |
210 | | /* |
211 | | * VIRTIO_MMIO_SHM_SEL is unimplemented |
212 | | * according to the linux driver, if region length is -1 |
213 | | * the shared memory doesn't exist |
214 | | */ |
215 | 0 | return -1; |
216 | 0 | case VIRTIO_MMIO_DEVICE_FEATURES_SEL: |
217 | 0 | case VIRTIO_MMIO_DRIVER_FEATURES: |
218 | 0 | case VIRTIO_MMIO_DRIVER_FEATURES_SEL: |
219 | 0 | case VIRTIO_MMIO_GUEST_PAGE_SIZE: |
220 | 0 | case VIRTIO_MMIO_QUEUE_SEL: |
221 | 0 | case VIRTIO_MMIO_QUEUE_NUM: |
222 | 0 | case VIRTIO_MMIO_QUEUE_ALIGN: |
223 | 0 | case VIRTIO_MMIO_QUEUE_NOTIFY: |
224 | 0 | case VIRTIO_MMIO_INTERRUPT_ACK: |
225 | 0 | case VIRTIO_MMIO_QUEUE_DESC_LOW: |
226 | 0 | case VIRTIO_MMIO_QUEUE_DESC_HIGH: |
227 | 0 | case VIRTIO_MMIO_QUEUE_AVAIL_LOW: |
228 | 0 | case VIRTIO_MMIO_QUEUE_AVAIL_HIGH: |
229 | 0 | case VIRTIO_MMIO_QUEUE_USED_LOW: |
230 | 0 | case VIRTIO_MMIO_QUEUE_USED_HIGH: |
231 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
232 | 0 | "%s: read of write-only register (0x%" HWADDR_PRIx ")\n", |
233 | 0 | __func__, offset); |
234 | 0 | return 0; |
235 | 0 | default: |
236 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
237 | 0 | "%s: bad register offset (0x%" HWADDR_PRIx ")\n", |
238 | 0 | __func__, offset); |
239 | 0 | return 0; |
240 | 0 | } |
241 | 0 | return 0; |
242 | 0 | } |
243 | | |
244 | | static void virtio_mmio_write(void *opaque, hwaddr offset, uint64_t value, |
245 | | unsigned size) |
246 | 0 | { |
247 | 0 | VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque; |
248 | 0 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
249 | 0 | uint16_t vq_idx; |
250 | |
|
251 | 0 | trace_virtio_mmio_write_offset(offset, value); |
252 | |
|
253 | 0 | if (!vdev) { |
254 | | /* If no backend is present, we just make all registers |
255 | | * write-ignored. This allows us to provide transports with |
256 | | * no backend plugged in. |
257 | | */ |
258 | 0 | return; |
259 | 0 | } |
260 | | |
261 | 0 | if (offset >= VIRTIO_MMIO_CONFIG) { |
262 | 0 | offset -= VIRTIO_MMIO_CONFIG; |
263 | 0 | if (proxy->legacy) { |
264 | 0 | switch (size) { |
265 | 0 | case 1: |
266 | 0 | virtio_config_writeb(vdev, offset, value); |
267 | 0 | break; |
268 | 0 | case 2: |
269 | 0 | virtio_config_writew(vdev, offset, value); |
270 | 0 | break; |
271 | 0 | case 4: |
272 | 0 | virtio_config_writel(vdev, offset, value); |
273 | 0 | break; |
274 | 0 | default: |
275 | 0 | abort(); |
276 | 0 | } |
277 | 0 | return; |
278 | 0 | } else { |
279 | 0 | switch (size) { |
280 | 0 | case 1: |
281 | 0 | virtio_config_modern_writeb(vdev, offset, value); |
282 | 0 | break; |
283 | 0 | case 2: |
284 | 0 | virtio_config_modern_writew(vdev, offset, value); |
285 | 0 | break; |
286 | 0 | case 4: |
287 | 0 | virtio_config_modern_writel(vdev, offset, value); |
288 | 0 | break; |
289 | 0 | default: |
290 | 0 | abort(); |
291 | 0 | } |
292 | 0 | return; |
293 | 0 | } |
294 | 0 | } |
295 | 0 | if (size != 4) { |
296 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
297 | 0 | "%s: wrong size access to register!\n", |
298 | 0 | __func__); |
299 | 0 | return; |
300 | 0 | } |
301 | 0 | switch (offset) { |
302 | 0 | case VIRTIO_MMIO_DEVICE_FEATURES_SEL: |
303 | 0 | if (value) { |
304 | 0 | proxy->host_features_sel = 1; |
305 | 0 | } else { |
306 | 0 | proxy->host_features_sel = 0; |
307 | 0 | } |
308 | 0 | break; |
309 | 0 | case VIRTIO_MMIO_DRIVER_FEATURES: |
310 | 0 | if (proxy->legacy) { |
311 | 0 | if (proxy->guest_features_sel) { |
312 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
313 | 0 | "%s: attempt to write guest features with " |
314 | 0 | "guest_features_sel > 0 in legacy mode\n", |
315 | 0 | __func__); |
316 | 0 | } else { |
317 | 0 | virtio_set_features(vdev, value); |
318 | 0 | } |
319 | 0 | } else { |
320 | 0 | proxy->guest_features[proxy->guest_features_sel] = value; |
321 | 0 | } |
322 | 0 | break; |
323 | 0 | case VIRTIO_MMIO_DRIVER_FEATURES_SEL: |
324 | 0 | if (value) { |
325 | 0 | proxy->guest_features_sel = 1; |
326 | 0 | } else { |
327 | 0 | proxy->guest_features_sel = 0; |
328 | 0 | } |
329 | 0 | break; |
330 | 0 | case VIRTIO_MMIO_GUEST_PAGE_SIZE: |
331 | 0 | if (!proxy->legacy) { |
332 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
333 | 0 | "%s: write to legacy register (0x%" |
334 | 0 | HWADDR_PRIx ") in non-legacy mode\n", |
335 | 0 | __func__, offset); |
336 | 0 | return; |
337 | 0 | } |
338 | 0 | proxy->guest_page_shift = ctz32(value); |
339 | 0 | if (proxy->guest_page_shift > 31) { |
340 | 0 | proxy->guest_page_shift = 0; |
341 | 0 | } |
342 | 0 | trace_virtio_mmio_guest_page(value, proxy->guest_page_shift); |
343 | 0 | break; |
344 | 0 | case VIRTIO_MMIO_QUEUE_SEL: |
345 | 0 | if (value < VIRTIO_QUEUE_MAX) { |
346 | 0 | vdev->queue_sel = value; |
347 | 0 | } |
348 | 0 | break; |
349 | 0 | case VIRTIO_MMIO_QUEUE_NUM: |
350 | 0 | trace_virtio_mmio_queue_write(value, VIRTQUEUE_MAX_SIZE); |
351 | 0 | virtio_queue_set_num(vdev, vdev->queue_sel, value); |
352 | |
|
353 | 0 | if (proxy->legacy) { |
354 | 0 | virtio_queue_update_rings(vdev, vdev->queue_sel); |
355 | 0 | } else { |
356 | 0 | virtio_init_region_cache(vdev, vdev->queue_sel); |
357 | 0 | proxy->vqs[vdev->queue_sel].num = value; |
358 | 0 | } |
359 | 0 | break; |
360 | 0 | case VIRTIO_MMIO_QUEUE_ALIGN: |
361 | 0 | if (!proxy->legacy) { |
362 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
363 | 0 | "%s: write to legacy register (0x%" |
364 | 0 | HWADDR_PRIx ") in non-legacy mode\n", |
365 | 0 | __func__, offset); |
366 | 0 | return; |
367 | 0 | } |
368 | 0 | virtio_queue_set_align(vdev, vdev->queue_sel, value); |
369 | 0 | break; |
370 | 0 | case VIRTIO_MMIO_QUEUE_PFN: |
371 | 0 | if (!proxy->legacy) { |
372 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
373 | 0 | "%s: write to legacy register (0x%" |
374 | 0 | HWADDR_PRIx ") in non-legacy mode\n", |
375 | 0 | __func__, offset); |
376 | 0 | return; |
377 | 0 | } |
378 | 0 | if (value == 0) { |
379 | 0 | virtio_mmio_soft_reset(proxy); |
380 | 0 | } else { |
381 | 0 | virtio_queue_set_addr(vdev, vdev->queue_sel, |
382 | 0 | value << proxy->guest_page_shift); |
383 | 0 | } |
384 | 0 | break; |
385 | 0 | case VIRTIO_MMIO_QUEUE_READY: |
386 | 0 | if (proxy->legacy) { |
387 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
388 | 0 | "%s: write to non-legacy register (0x%" |
389 | 0 | HWADDR_PRIx ") in legacy mode\n", |
390 | 0 | __func__, offset); |
391 | 0 | return; |
392 | 0 | } |
393 | 0 | if (value) { |
394 | 0 | virtio_queue_set_num(vdev, vdev->queue_sel, |
395 | 0 | proxy->vqs[vdev->queue_sel].num); |
396 | 0 | virtio_queue_set_rings(vdev, vdev->queue_sel, |
397 | 0 | ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 | |
398 | 0 | proxy->vqs[vdev->queue_sel].desc[0], |
399 | 0 | ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 | |
400 | 0 | proxy->vqs[vdev->queue_sel].avail[0], |
401 | 0 | ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 | |
402 | 0 | proxy->vqs[vdev->queue_sel].used[0]); |
403 | 0 | proxy->vqs[vdev->queue_sel].enabled = 1; |
404 | 0 | } else { |
405 | 0 | proxy->vqs[vdev->queue_sel].enabled = 0; |
406 | 0 | } |
407 | 0 | break; |
408 | 0 | case VIRTIO_MMIO_QUEUE_NOTIFY: |
409 | 0 | vq_idx = value; |
410 | 0 | if (vq_idx < VIRTIO_QUEUE_MAX && virtio_queue_get_num(vdev, vq_idx)) { |
411 | 0 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFICATION_DATA)) { |
412 | 0 | VirtQueue *vq = virtio_get_queue(vdev, vq_idx); |
413 | |
|
414 | 0 | virtio_queue_set_shadow_avail_idx(vq, (value >> 16) & 0xFFFF); |
415 | 0 | } |
416 | 0 | virtio_queue_notify(vdev, vq_idx); |
417 | 0 | } |
418 | 0 | break; |
419 | 0 | case VIRTIO_MMIO_INTERRUPT_ACK: |
420 | 0 | qatomic_and(&vdev->isr, ~value); |
421 | 0 | virtio_update_irq(vdev); |
422 | 0 | break; |
423 | 0 | case VIRTIO_MMIO_STATUS: |
424 | 0 | if (!(value & VIRTIO_CONFIG_S_DRIVER_OK)) { |
425 | 0 | virtio_mmio_stop_ioeventfd(proxy); |
426 | 0 | } |
427 | |
|
428 | 0 | if (!proxy->legacy && (value & VIRTIO_CONFIG_S_FEATURES_OK)) { |
429 | 0 | virtio_set_features(vdev, |
430 | 0 | ((uint64_t)proxy->guest_features[1]) << 32 | |
431 | 0 | proxy->guest_features[0]); |
432 | 0 | } |
433 | |
|
434 | 0 | virtio_set_status(vdev, value & 0xff); |
435 | |
|
436 | 0 | if (value & VIRTIO_CONFIG_S_DRIVER_OK) { |
437 | 0 | virtio_mmio_start_ioeventfd(proxy); |
438 | 0 | } |
439 | |
|
440 | 0 | if (vdev->status == 0) { |
441 | 0 | virtio_mmio_soft_reset(proxy); |
442 | 0 | } |
443 | 0 | break; |
444 | 0 | case VIRTIO_MMIO_QUEUE_DESC_LOW: |
445 | 0 | if (proxy->legacy) { |
446 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
447 | 0 | "%s: write to non-legacy register (0x%" |
448 | 0 | HWADDR_PRIx ") in legacy mode\n", |
449 | 0 | __func__, offset); |
450 | 0 | return; |
451 | 0 | } |
452 | 0 | proxy->vqs[vdev->queue_sel].desc[0] = value; |
453 | 0 | break; |
454 | 0 | case VIRTIO_MMIO_QUEUE_DESC_HIGH: |
455 | 0 | if (proxy->legacy) { |
456 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
457 | 0 | "%s: write to non-legacy register (0x%" |
458 | 0 | HWADDR_PRIx ") in legacy mode\n", |
459 | 0 | __func__, offset); |
460 | 0 | return; |
461 | 0 | } |
462 | 0 | proxy->vqs[vdev->queue_sel].desc[1] = value; |
463 | 0 | break; |
464 | 0 | case VIRTIO_MMIO_QUEUE_AVAIL_LOW: |
465 | 0 | if (proxy->legacy) { |
466 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
467 | 0 | "%s: write to non-legacy register (0x%" |
468 | 0 | HWADDR_PRIx ") in legacy mode\n", |
469 | 0 | __func__, offset); |
470 | 0 | return; |
471 | 0 | } |
472 | 0 | proxy->vqs[vdev->queue_sel].avail[0] = value; |
473 | 0 | break; |
474 | 0 | case VIRTIO_MMIO_QUEUE_AVAIL_HIGH: |
475 | 0 | if (proxy->legacy) { |
476 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
477 | 0 | "%s: write to non-legacy register (0x%" |
478 | 0 | HWADDR_PRIx ") in legacy mode\n", |
479 | 0 | __func__, offset); |
480 | 0 | return; |
481 | 0 | } |
482 | 0 | proxy->vqs[vdev->queue_sel].avail[1] = value; |
483 | 0 | break; |
484 | 0 | case VIRTIO_MMIO_QUEUE_USED_LOW: |
485 | 0 | if (proxy->legacy) { |
486 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
487 | 0 | "%s: write to non-legacy register (0x%" |
488 | 0 | HWADDR_PRIx ") in legacy mode\n", |
489 | 0 | __func__, offset); |
490 | 0 | return; |
491 | 0 | } |
492 | 0 | proxy->vqs[vdev->queue_sel].used[0] = value; |
493 | 0 | break; |
494 | 0 | case VIRTIO_MMIO_QUEUE_USED_HIGH: |
495 | 0 | if (proxy->legacy) { |
496 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
497 | 0 | "%s: write to non-legacy register (0x%" |
498 | 0 | HWADDR_PRIx ") in legacy mode\n", |
499 | 0 | __func__, offset); |
500 | 0 | return; |
501 | 0 | } |
502 | 0 | proxy->vqs[vdev->queue_sel].used[1] = value; |
503 | 0 | break; |
504 | 0 | case VIRTIO_MMIO_MAGIC_VALUE: |
505 | 0 | case VIRTIO_MMIO_VERSION: |
506 | 0 | case VIRTIO_MMIO_DEVICE_ID: |
507 | 0 | case VIRTIO_MMIO_VENDOR_ID: |
508 | 0 | case VIRTIO_MMIO_DEVICE_FEATURES: |
509 | 0 | case VIRTIO_MMIO_QUEUE_NUM_MAX: |
510 | 0 | case VIRTIO_MMIO_INTERRUPT_STATUS: |
511 | 0 | case VIRTIO_MMIO_CONFIG_GENERATION: |
512 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
513 | 0 | "%s: write to read-only register (0x%" HWADDR_PRIx ")\n", |
514 | 0 | __func__, offset); |
515 | 0 | break; |
516 | | |
517 | 0 | default: |
518 | 0 | qemu_log_mask(LOG_GUEST_ERROR, |
519 | 0 | "%s: bad register offset (0x%" HWADDR_PRIx ")\n", |
520 | 0 | __func__, offset); |
521 | 0 | } |
522 | 0 | } |
523 | | |
524 | | static const MemoryRegionOps virtio_legacy_mem_ops = { |
525 | | .read = virtio_mmio_read, |
526 | | .write = virtio_mmio_write, |
527 | | .endianness = DEVICE_NATIVE_ENDIAN, |
528 | | }; |
529 | | |
530 | | static const MemoryRegionOps virtio_mem_ops = { |
531 | | .read = virtio_mmio_read, |
532 | | .write = virtio_mmio_write, |
533 | | .endianness = DEVICE_LITTLE_ENDIAN, |
534 | | }; |
535 | | |
536 | | static void virtio_mmio_update_irq(DeviceState *opaque, uint16_t vector) |
537 | 0 | { |
538 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque); |
539 | 0 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
540 | 0 | int level; |
541 | |
|
542 | 0 | if (!vdev) { |
543 | 0 | return; |
544 | 0 | } |
545 | 0 | level = (qatomic_read(&vdev->isr) != 0); |
546 | 0 | trace_virtio_mmio_setting_irq(level); |
547 | 0 | qemu_set_irq(proxy->irq, level); |
548 | 0 | } |
549 | | |
550 | | static int virtio_mmio_load_config(DeviceState *opaque, QEMUFile *f) |
551 | 0 | { |
552 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque); |
553 | |
|
554 | 0 | proxy->host_features_sel = qemu_get_be32(f); |
555 | 0 | proxy->guest_features_sel = qemu_get_be32(f); |
556 | 0 | proxy->guest_page_shift = qemu_get_be32(f); |
557 | 0 | return 0; |
558 | 0 | } |
559 | | |
560 | | static void virtio_mmio_save_config(DeviceState *opaque, QEMUFile *f) |
561 | 0 | { |
562 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque); |
563 | |
|
564 | 0 | qemu_put_be32(f, proxy->host_features_sel); |
565 | 0 | qemu_put_be32(f, proxy->guest_features_sel); |
566 | 0 | qemu_put_be32(f, proxy->guest_page_shift); |
567 | 0 | } |
568 | | |
569 | | static const VMStateDescription vmstate_virtio_mmio_queue_state = { |
570 | | .name = "virtio_mmio/queue_state", |
571 | | .version_id = 1, |
572 | | .minimum_version_id = 1, |
573 | | .fields = (const VMStateField[]) { |
574 | | VMSTATE_UINT16(num, VirtIOMMIOQueue), |
575 | | VMSTATE_BOOL(enabled, VirtIOMMIOQueue), |
576 | | VMSTATE_UINT32_ARRAY(desc, VirtIOMMIOQueue, 2), |
577 | | VMSTATE_UINT32_ARRAY(avail, VirtIOMMIOQueue, 2), |
578 | | VMSTATE_UINT32_ARRAY(used, VirtIOMMIOQueue, 2), |
579 | | VMSTATE_END_OF_LIST() |
580 | | } |
581 | | }; |
582 | | |
583 | | static const VMStateDescription vmstate_virtio_mmio_state_sub = { |
584 | | .name = "virtio_mmio/state", |
585 | | .version_id = 1, |
586 | | .minimum_version_id = 1, |
587 | | .fields = (const VMStateField[]) { |
588 | | VMSTATE_UINT32_ARRAY(guest_features, VirtIOMMIOProxy, 2), |
589 | | VMSTATE_STRUCT_ARRAY(vqs, VirtIOMMIOProxy, VIRTIO_QUEUE_MAX, 0, |
590 | | vmstate_virtio_mmio_queue_state, |
591 | | VirtIOMMIOQueue), |
592 | | VMSTATE_END_OF_LIST() |
593 | | } |
594 | | }; |
595 | | |
596 | | static const VMStateDescription vmstate_virtio_mmio = { |
597 | | .name = "virtio_mmio", |
598 | | .version_id = 1, |
599 | | .minimum_version_id = 1, |
600 | | .fields = (const VMStateField[]) { |
601 | | VMSTATE_END_OF_LIST() |
602 | | }, |
603 | | .subsections = (const VMStateDescription * const []) { |
604 | | &vmstate_virtio_mmio_state_sub, |
605 | | NULL |
606 | | } |
607 | | }; |
608 | | |
609 | | static void virtio_mmio_save_extra_state(DeviceState *opaque, QEMUFile *f) |
610 | 0 | { |
611 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque); |
612 | 0 | Error *local_err = NULL; |
613 | 0 | int ret; |
614 | |
|
615 | 0 | ret = vmstate_save_state(f, &vmstate_virtio_mmio, proxy, NULL, &local_err); |
616 | 0 | if (ret < 0) { |
617 | 0 | error_report_err(local_err); |
618 | 0 | } |
619 | 0 | } |
620 | | |
621 | | static int virtio_mmio_load_extra_state(DeviceState *opaque, QEMUFile *f) |
622 | 0 | { |
623 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque); |
624 | 0 | Error *local_err = NULL; |
625 | 0 | int ret; |
626 | |
|
627 | 0 | ret = vmstate_load_state(f, &vmstate_virtio_mmio, proxy, 1, &local_err); |
628 | 0 | if (ret < 0) { |
629 | 0 | error_report_err(local_err); |
630 | 0 | } |
631 | 0 | return ret; |
632 | 0 | } |
633 | | |
634 | | static bool virtio_mmio_has_extra_state(DeviceState *opaque) |
635 | 0 | { |
636 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque); |
637 | |
|
638 | 0 | return !proxy->legacy; |
639 | 0 | } |
640 | | |
641 | | static void virtio_mmio_reset(DeviceState *d) |
642 | 0 | { |
643 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); |
644 | 0 | int i; |
645 | |
|
646 | 0 | virtio_mmio_soft_reset(proxy); |
647 | |
|
648 | 0 | proxy->host_features_sel = 0; |
649 | 0 | proxy->guest_features_sel = 0; |
650 | 0 | proxy->guest_page_shift = 0; |
651 | |
|
652 | 0 | if (!proxy->legacy) { |
653 | 0 | proxy->guest_features[0] = proxy->guest_features[1] = 0; |
654 | |
|
655 | 0 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
656 | 0 | proxy->vqs[i].num = 0; |
657 | 0 | proxy->vqs[i].desc[0] = proxy->vqs[i].desc[1] = 0; |
658 | 0 | proxy->vqs[i].avail[0] = proxy->vqs[i].avail[1] = 0; |
659 | 0 | proxy->vqs[i].used[0] = proxy->vqs[i].used[1] = 0; |
660 | 0 | } |
661 | 0 | } |
662 | 0 | } |
663 | | |
664 | | static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign, |
665 | | bool with_irqfd) |
666 | 0 | { |
667 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); |
668 | 0 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
669 | 0 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
670 | 0 | int r; |
671 | |
|
672 | 0 | r = virtio_set_guest_notifier(vdev, n, assign, with_irqfd); |
673 | 0 | if (r < 0) { |
674 | 0 | return r; |
675 | 0 | } |
676 | | |
677 | 0 | if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) { |
678 | 0 | vdc->guest_notifier_mask(vdev, n, !assign); |
679 | 0 | } |
680 | |
|
681 | 0 | return 0; |
682 | 0 | } |
683 | | |
684 | | static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs, |
685 | | bool assign) |
686 | 0 | { |
687 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); |
688 | 0 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
689 | | /* TODO: need to check if kvm-arm supports irqfd */ |
690 | 0 | bool with_irqfd = false; |
691 | 0 | int r, n; |
692 | |
|
693 | 0 | nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX); |
694 | |
|
695 | 0 | for (n = 0; n < nvqs; n++) { |
696 | 0 | if (!virtio_queue_get_num(vdev, n)) { |
697 | 0 | break; |
698 | 0 | } |
699 | | |
700 | 0 | r = virtio_mmio_set_guest_notifier(d, n, assign, with_irqfd); |
701 | 0 | if (r < 0) { |
702 | 0 | goto assign_error; |
703 | 0 | } |
704 | 0 | } |
705 | 0 | r = virtio_mmio_set_guest_notifier(d, VIRTIO_CONFIG_IRQ_IDX, assign, |
706 | 0 | with_irqfd); |
707 | 0 | if (r < 0) { |
708 | 0 | goto assign_error; |
709 | 0 | } |
710 | | |
711 | 0 | return 0; |
712 | | |
713 | 0 | assign_error: |
714 | | /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ |
715 | 0 | assert(assign); |
716 | 0 | while (--n >= 0) { |
717 | 0 | virtio_mmio_set_guest_notifier(d, n, !assign, false); |
718 | 0 | } |
719 | 0 | return r; |
720 | 0 | } |
721 | | |
722 | | static void virtio_mmio_pre_plugged(DeviceState *d, Error **errp) |
723 | 0 | { |
724 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); |
725 | 0 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
726 | |
|
727 | 0 | if (!proxy->legacy) { |
728 | 0 | virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); |
729 | 0 | } |
730 | 0 | } |
731 | | |
732 | | /* virtio-mmio device */ |
733 | | |
734 | | static const Property virtio_mmio_properties[] = { |
735 | | DEFINE_PROP_BOOL("force-legacy", VirtIOMMIOProxy, legacy, true), |
736 | | DEFINE_PROP_BIT("ioeventfd", VirtIOMMIOProxy, flags, |
737 | | VIRTIO_IOMMIO_FLAG_USE_IOEVENTFD_BIT, true), |
738 | | DEFINE_PROP_UINT16(VIRTIO_QUEUE_SIZE_OVERRIDE, VirtIOMMIOProxy, |
739 | | override_queue_size, 0), |
740 | | }; |
741 | | |
742 | | static void virtio_mmio_realizefn(DeviceState *d, Error **errp) |
743 | 0 | { |
744 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); |
745 | 0 | SysBusDevice *sbd = SYS_BUS_DEVICE(d); |
746 | |
|
747 | 0 | qbus_init(&proxy->bus, sizeof(proxy->bus), TYPE_VIRTIO_MMIO_BUS, d, NULL); |
748 | 0 | sysbus_init_irq(sbd, &proxy->irq); |
749 | | |
750 | | /* fd-based ioevents can't be synchronized in record/replay */ |
751 | 0 | if (replay_mode != REPLAY_MODE_NONE) { |
752 | 0 | proxy->flags &= ~VIRTIO_IOMMIO_FLAG_USE_IOEVENTFD; |
753 | 0 | } |
754 | |
|
755 | 0 | if (proxy->legacy) { |
756 | 0 | memory_region_init_io(&proxy->iomem, OBJECT(d), |
757 | 0 | &virtio_legacy_mem_ops, proxy, |
758 | 0 | TYPE_VIRTIO_MMIO, 0x200); |
759 | 0 | } else { |
760 | 0 | memory_region_init_io(&proxy->iomem, OBJECT(d), |
761 | 0 | &virtio_mem_ops, proxy, |
762 | 0 | TYPE_VIRTIO_MMIO, 0x200); |
763 | 0 | } |
764 | 0 | sysbus_init_mmio(sbd, &proxy->iomem); |
765 | 0 | } |
766 | | |
767 | | static void virtio_mmio_class_init(ObjectClass *klass, const void *data) |
768 | 61 | { |
769 | 61 | DeviceClass *dc = DEVICE_CLASS(klass); |
770 | | |
771 | 61 | dc->realize = virtio_mmio_realizefn; |
772 | 61 | device_class_set_legacy_reset(dc, virtio_mmio_reset); |
773 | 61 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
774 | 61 | device_class_set_props(dc, virtio_mmio_properties); |
775 | 61 | } |
776 | | |
777 | | static const TypeInfo virtio_mmio_info = { |
778 | | .name = TYPE_VIRTIO_MMIO, |
779 | | .parent = TYPE_SYS_BUS_DEVICE, |
780 | | .instance_size = sizeof(VirtIOMMIOProxy), |
781 | | .class_init = virtio_mmio_class_init, |
782 | | }; |
783 | | |
784 | | /* virtio-mmio-bus. */ |
785 | | |
786 | | static char *virtio_mmio_bus_get_dev_path(DeviceState *dev) |
787 | 0 | { |
788 | 0 | BusState *virtio_mmio_bus; |
789 | 0 | VirtIOMMIOProxy *virtio_mmio_proxy; |
790 | 0 | char *proxy_path; |
791 | 0 | char *path; |
792 | 0 | MemoryRegionSection section; |
793 | |
|
794 | 0 | virtio_mmio_bus = qdev_get_parent_bus(dev); |
795 | 0 | virtio_mmio_proxy = VIRTIO_MMIO(virtio_mmio_bus->parent); |
796 | 0 | proxy_path = qdev_get_dev_path(DEVICE(virtio_mmio_proxy)); |
797 | |
|
798 | 0 | section = memory_region_find(&virtio_mmio_proxy->iomem, 0, 0x200); |
799 | 0 | assert(section.mr); |
800 | |
|
801 | 0 | if (proxy_path) { |
802 | 0 | path = g_strdup_printf("%s/virtio-mmio@" HWADDR_FMT_plx, proxy_path, |
803 | 0 | section.offset_within_address_space); |
804 | 0 | } else { |
805 | 0 | path = g_strdup_printf("virtio-mmio@" HWADDR_FMT_plx, |
806 | 0 | section.offset_within_address_space); |
807 | 0 | } |
808 | 0 | memory_region_unref(section.mr); |
809 | |
|
810 | 0 | g_free(proxy_path); |
811 | 0 | return path; |
812 | 0 | } |
813 | | |
814 | | static void virtio_mmio_vmstate_change(DeviceState *d, bool running) |
815 | 0 | { |
816 | 0 | VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); |
817 | |
|
818 | 0 | if (running) { |
819 | 0 | virtio_mmio_start_ioeventfd(proxy); |
820 | 0 | } else { |
821 | 0 | virtio_mmio_stop_ioeventfd(proxy); |
822 | 0 | } |
823 | 0 | } |
824 | | |
825 | | static void virtio_mmio_bus_class_init(ObjectClass *klass, const void *data) |
826 | 61 | { |
827 | 61 | BusClass *bus_class = BUS_CLASS(klass); |
828 | 61 | VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); |
829 | | |
830 | 61 | k->notify = virtio_mmio_update_irq; |
831 | 61 | k->save_config = virtio_mmio_save_config; |
832 | 61 | k->load_config = virtio_mmio_load_config; |
833 | 61 | k->save_extra_state = virtio_mmio_save_extra_state; |
834 | 61 | k->load_extra_state = virtio_mmio_load_extra_state; |
835 | 61 | k->has_extra_state = virtio_mmio_has_extra_state; |
836 | 61 | k->set_guest_notifiers = virtio_mmio_set_guest_notifiers; |
837 | 61 | k->ioeventfd_enabled = virtio_mmio_ioeventfd_enabled; |
838 | 61 | k->ioeventfd_assign = virtio_mmio_ioeventfd_assign; |
839 | 61 | k->pre_plugged = virtio_mmio_pre_plugged; |
840 | 61 | k->vmstate_change = virtio_mmio_vmstate_change; |
841 | 61 | k->has_variable_vring_alignment = true; |
842 | 61 | bus_class->max_dev = 1; |
843 | 61 | bus_class->get_dev_path = virtio_mmio_bus_get_dev_path; |
844 | 61 | } |
845 | | |
846 | | static const TypeInfo virtio_mmio_bus_info = { |
847 | | .name = TYPE_VIRTIO_MMIO_BUS, |
848 | | .parent = TYPE_VIRTIO_BUS, |
849 | | .instance_size = sizeof(VirtioBusState), |
850 | | .class_init = virtio_mmio_bus_class_init, |
851 | | }; |
852 | | |
853 | | static void virtio_mmio_register_types(void) |
854 | 61 | { |
855 | 61 | type_register_static(&virtio_mmio_bus_info); |
856 | 61 | type_register_static(&virtio_mmio_info); |
857 | 61 | } |
858 | | |
859 | | type_init(virtio_mmio_register_types) |