]> git.proxmox.com Git - mirror_qemu.git/blob - hw/virtio/virtio-mmio.c
Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20190522' into staging
[mirror_qemu.git] / hw / virtio / virtio-mmio.c
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/sysbus.h"
25 #include "hw/virtio/virtio.h"
26 #include "qemu/host-utils.h"
27 #include "sysemu/kvm.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "qemu/error-report.h"
30 #include "qemu/log.h"
31 #include "trace.h"
32
33 /* QOM macros */
34 /* virtio-mmio-bus */
35 #define TYPE_VIRTIO_MMIO_BUS "virtio-mmio-bus"
36 #define VIRTIO_MMIO_BUS(obj) \
37 OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_MMIO_BUS)
38 #define VIRTIO_MMIO_BUS_GET_CLASS(obj) \
39 OBJECT_GET_CLASS(VirtioBusClass, (obj), TYPE_VIRTIO_MMIO_BUS)
40 #define VIRTIO_MMIO_BUS_CLASS(klass) \
41 OBJECT_CLASS_CHECK(VirtioBusClass, (klass), TYPE_VIRTIO_MMIO_BUS)
42
43 /* virtio-mmio */
44 #define TYPE_VIRTIO_MMIO "virtio-mmio"
45 #define VIRTIO_MMIO(obj) \
46 OBJECT_CHECK(VirtIOMMIOProxy, (obj), TYPE_VIRTIO_MMIO)
47
48 #define VIRT_MAGIC 0x74726976 /* 'virt' */
49 #define VIRT_VERSION 1
50 #define VIRT_VENDOR 0x554D4551 /* 'QEMU' */
51
52 typedef struct {
53 /* Generic */
54 SysBusDevice parent_obj;
55 MemoryRegion iomem;
56 qemu_irq irq;
57 /* Guest accessible state needing migration and reset */
58 uint32_t host_features_sel;
59 uint32_t guest_features_sel;
60 uint32_t guest_page_shift;
61 /* virtio-bus */
62 VirtioBusState bus;
63 bool format_transport_address;
64 } VirtIOMMIOProxy;
65
66 static bool virtio_mmio_ioeventfd_enabled(DeviceState *d)
67 {
68 return kvm_eventfds_enabled();
69 }
70
71 static int virtio_mmio_ioeventfd_assign(DeviceState *d,
72 EventNotifier *notifier,
73 int n, bool assign)
74 {
75 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
76
77 if (assign) {
78 memory_region_add_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUE_NOTIFY, 4,
79 true, n, notifier);
80 } else {
81 memory_region_del_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUE_NOTIFY, 4,
82 true, n, notifier);
83 }
84 return 0;
85 }
86
87 static void virtio_mmio_start_ioeventfd(VirtIOMMIOProxy *proxy)
88 {
89 virtio_bus_start_ioeventfd(&proxy->bus);
90 }
91
92 static void virtio_mmio_stop_ioeventfd(VirtIOMMIOProxy *proxy)
93 {
94 virtio_bus_stop_ioeventfd(&proxy->bus);
95 }
96
97 static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size)
98 {
99 VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque;
100 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
101
102 trace_virtio_mmio_read(offset);
103
104 if (!vdev) {
105 /* If no backend is present, we treat most registers as
106 * read-as-zero, except for the magic number, version and
107 * vendor ID. This is not strictly sanctioned by the virtio
108 * spec, but it allows us to provide transports with no backend
109 * plugged in which don't confuse Linux's virtio code: the
110 * probe won't complain about the bad magic number, but the
111 * device ID of zero means no backend will claim it.
112 */
113 switch (offset) {
114 case VIRTIO_MMIO_MAGIC_VALUE:
115 return VIRT_MAGIC;
116 case VIRTIO_MMIO_VERSION:
117 return VIRT_VERSION;
118 case VIRTIO_MMIO_VENDOR_ID:
119 return VIRT_VENDOR;
120 default:
121 return 0;
122 }
123 }
124
125 if (offset >= VIRTIO_MMIO_CONFIG) {
126 offset -= VIRTIO_MMIO_CONFIG;
127 switch (size) {
128 case 1:
129 return virtio_config_readb(vdev, offset);
130 case 2:
131 return virtio_config_readw(vdev, offset);
132 case 4:
133 return virtio_config_readl(vdev, offset);
134 default:
135 abort();
136 }
137 }
138 if (size != 4) {
139 qemu_log_mask(LOG_GUEST_ERROR,
140 "%s: wrong size access to register!\n",
141 __func__);
142 return 0;
143 }
144 switch (offset) {
145 case VIRTIO_MMIO_MAGIC_VALUE:
146 return VIRT_MAGIC;
147 case VIRTIO_MMIO_VERSION:
148 return VIRT_VERSION;
149 case VIRTIO_MMIO_DEVICE_ID:
150 return vdev->device_id;
151 case VIRTIO_MMIO_VENDOR_ID:
152 return VIRT_VENDOR;
153 case VIRTIO_MMIO_DEVICE_FEATURES:
154 if (proxy->host_features_sel) {
155 return 0;
156 }
157 return vdev->host_features;
158 case VIRTIO_MMIO_QUEUE_NUM_MAX:
159 if (!virtio_queue_get_num(vdev, vdev->queue_sel)) {
160 return 0;
161 }
162 return VIRTQUEUE_MAX_SIZE;
163 case VIRTIO_MMIO_QUEUE_PFN:
164 return virtio_queue_get_addr(vdev, vdev->queue_sel)
165 >> proxy->guest_page_shift;
166 case VIRTIO_MMIO_INTERRUPT_STATUS:
167 return atomic_read(&vdev->isr);
168 case VIRTIO_MMIO_STATUS:
169 return vdev->status;
170 case VIRTIO_MMIO_DEVICE_FEATURES_SEL:
171 case VIRTIO_MMIO_DRIVER_FEATURES:
172 case VIRTIO_MMIO_DRIVER_FEATURES_SEL:
173 case VIRTIO_MMIO_GUEST_PAGE_SIZE:
174 case VIRTIO_MMIO_QUEUE_SEL:
175 case VIRTIO_MMIO_QUEUE_NUM:
176 case VIRTIO_MMIO_QUEUE_ALIGN:
177 case VIRTIO_MMIO_QUEUE_NOTIFY:
178 case VIRTIO_MMIO_INTERRUPT_ACK:
179 qemu_log_mask(LOG_GUEST_ERROR,
180 "%s: read of write-only register\n",
181 __func__);
182 return 0;
183 default:
184 qemu_log_mask(LOG_GUEST_ERROR, "%s: bad register offset\n", __func__);
185 return 0;
186 }
187 return 0;
188 }
189
190 static void virtio_mmio_write(void *opaque, hwaddr offset, uint64_t value,
191 unsigned size)
192 {
193 VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque;
194 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
195
196 trace_virtio_mmio_write_offset(offset, value);
197
198 if (!vdev) {
199 /* If no backend is present, we just make all registers
200 * write-ignored. This allows us to provide transports with
201 * no backend plugged in.
202 */
203 return;
204 }
205
206 if (offset >= VIRTIO_MMIO_CONFIG) {
207 offset -= VIRTIO_MMIO_CONFIG;
208 switch (size) {
209 case 1:
210 virtio_config_writeb(vdev, offset, value);
211 break;
212 case 2:
213 virtio_config_writew(vdev, offset, value);
214 break;
215 case 4:
216 virtio_config_writel(vdev, offset, value);
217 break;
218 default:
219 abort();
220 }
221 return;
222 }
223 if (size != 4) {
224 qemu_log_mask(LOG_GUEST_ERROR,
225 "%s: wrong size access to register!\n",
226 __func__);
227 return;
228 }
229 switch (offset) {
230 case VIRTIO_MMIO_DEVICE_FEATURES_SEL:
231 proxy->host_features_sel = value;
232 break;
233 case VIRTIO_MMIO_DRIVER_FEATURES:
234 if (!proxy->guest_features_sel) {
235 virtio_set_features(vdev, value);
236 }
237 break;
238 case VIRTIO_MMIO_DRIVER_FEATURES_SEL:
239 proxy->guest_features_sel = value;
240 break;
241 case VIRTIO_MMIO_GUEST_PAGE_SIZE:
242 proxy->guest_page_shift = ctz32(value);
243 if (proxy->guest_page_shift > 31) {
244 proxy->guest_page_shift = 0;
245 }
246 trace_virtio_mmio_guest_page(value, proxy->guest_page_shift);
247 break;
248 case VIRTIO_MMIO_QUEUE_SEL:
249 if (value < VIRTIO_QUEUE_MAX) {
250 vdev->queue_sel = value;
251 }
252 break;
253 case VIRTIO_MMIO_QUEUE_NUM:
254 trace_virtio_mmio_queue_write(value, VIRTQUEUE_MAX_SIZE);
255 virtio_queue_set_num(vdev, vdev->queue_sel, value);
256 /* Note: only call this function for legacy devices */
257 virtio_queue_update_rings(vdev, vdev->queue_sel);
258 break;
259 case VIRTIO_MMIO_QUEUE_ALIGN:
260 /* Note: this is only valid for legacy devices */
261 virtio_queue_set_align(vdev, vdev->queue_sel, value);
262 break;
263 case VIRTIO_MMIO_QUEUE_PFN:
264 if (value == 0) {
265 virtio_reset(vdev);
266 } else {
267 virtio_queue_set_addr(vdev, vdev->queue_sel,
268 value << proxy->guest_page_shift);
269 }
270 break;
271 case VIRTIO_MMIO_QUEUE_NOTIFY:
272 if (value < VIRTIO_QUEUE_MAX) {
273 virtio_queue_notify(vdev, value);
274 }
275 break;
276 case VIRTIO_MMIO_INTERRUPT_ACK:
277 atomic_and(&vdev->isr, ~value);
278 virtio_update_irq(vdev);
279 break;
280 case VIRTIO_MMIO_STATUS:
281 if (!(value & VIRTIO_CONFIG_S_DRIVER_OK)) {
282 virtio_mmio_stop_ioeventfd(proxy);
283 }
284
285 virtio_set_status(vdev, value & 0xff);
286
287 if (value & VIRTIO_CONFIG_S_DRIVER_OK) {
288 virtio_mmio_start_ioeventfd(proxy);
289 }
290
291 if (vdev->status == 0) {
292 virtio_reset(vdev);
293 }
294 break;
295 case VIRTIO_MMIO_MAGIC_VALUE:
296 case VIRTIO_MMIO_VERSION:
297 case VIRTIO_MMIO_DEVICE_ID:
298 case VIRTIO_MMIO_VENDOR_ID:
299 case VIRTIO_MMIO_DEVICE_FEATURES:
300 case VIRTIO_MMIO_QUEUE_NUM_MAX:
301 case VIRTIO_MMIO_INTERRUPT_STATUS:
302 qemu_log_mask(LOG_GUEST_ERROR,
303 "%s: write to readonly register\n",
304 __func__);
305 break;
306
307 default:
308 qemu_log_mask(LOG_GUEST_ERROR, "%s: bad register offset\n", __func__);
309 }
310 }
311
312 static const MemoryRegionOps virtio_mem_ops = {
313 .read = virtio_mmio_read,
314 .write = virtio_mmio_write,
315 .endianness = DEVICE_NATIVE_ENDIAN,
316 };
317
318 static void virtio_mmio_update_irq(DeviceState *opaque, uint16_t vector)
319 {
320 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
321 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
322 int level;
323
324 if (!vdev) {
325 return;
326 }
327 level = (atomic_read(&vdev->isr) != 0);
328 trace_virtio_mmio_setting_irq(level);
329 qemu_set_irq(proxy->irq, level);
330 }
331
332 static int virtio_mmio_load_config(DeviceState *opaque, QEMUFile *f)
333 {
334 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
335
336 proxy->host_features_sel = qemu_get_be32(f);
337 proxy->guest_features_sel = qemu_get_be32(f);
338 proxy->guest_page_shift = qemu_get_be32(f);
339 return 0;
340 }
341
342 static void virtio_mmio_save_config(DeviceState *opaque, QEMUFile *f)
343 {
344 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
345
346 qemu_put_be32(f, proxy->host_features_sel);
347 qemu_put_be32(f, proxy->guest_features_sel);
348 qemu_put_be32(f, proxy->guest_page_shift);
349 }
350
351 static void virtio_mmio_reset(DeviceState *d)
352 {
353 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
354
355 virtio_mmio_stop_ioeventfd(proxy);
356 virtio_bus_reset(&proxy->bus);
357 proxy->host_features_sel = 0;
358 proxy->guest_features_sel = 0;
359 proxy->guest_page_shift = 0;
360 }
361
362 static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
363 bool with_irqfd)
364 {
365 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
366 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
367 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
368 VirtQueue *vq = virtio_get_queue(vdev, n);
369 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
370
371 if (assign) {
372 int r = event_notifier_init(notifier, 0);
373 if (r < 0) {
374 return r;
375 }
376 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
377 } else {
378 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
379 event_notifier_cleanup(notifier);
380 }
381
382 if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) {
383 vdc->guest_notifier_mask(vdev, n, !assign);
384 }
385
386 return 0;
387 }
388
389 static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
390 bool assign)
391 {
392 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
393 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
394 /* TODO: need to check if kvm-arm supports irqfd */
395 bool with_irqfd = false;
396 int r, n;
397
398 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
399
400 for (n = 0; n < nvqs; n++) {
401 if (!virtio_queue_get_num(vdev, n)) {
402 break;
403 }
404
405 r = virtio_mmio_set_guest_notifier(d, n, assign, with_irqfd);
406 if (r < 0) {
407 goto assign_error;
408 }
409 }
410
411 return 0;
412
413 assign_error:
414 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
415 assert(assign);
416 while (--n >= 0) {
417 virtio_mmio_set_guest_notifier(d, n, !assign, false);
418 }
419 return r;
420 }
421
422 /* virtio-mmio device */
423
424 static Property virtio_mmio_properties[] = {
425 DEFINE_PROP_BOOL("format_transport_address", VirtIOMMIOProxy,
426 format_transport_address, true),
427 DEFINE_PROP_END_OF_LIST(),
428 };
429
430 static void virtio_mmio_realizefn(DeviceState *d, Error **errp)
431 {
432 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
433 SysBusDevice *sbd = SYS_BUS_DEVICE(d);
434
435 qbus_create_inplace(&proxy->bus, sizeof(proxy->bus), TYPE_VIRTIO_MMIO_BUS,
436 d, NULL);
437 sysbus_init_irq(sbd, &proxy->irq);
438 memory_region_init_io(&proxy->iomem, OBJECT(d), &virtio_mem_ops, proxy,
439 TYPE_VIRTIO_MMIO, 0x200);
440 sysbus_init_mmio(sbd, &proxy->iomem);
441 }
442
443 static void virtio_mmio_class_init(ObjectClass *klass, void *data)
444 {
445 DeviceClass *dc = DEVICE_CLASS(klass);
446
447 dc->realize = virtio_mmio_realizefn;
448 dc->reset = virtio_mmio_reset;
449 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
450 dc->props = virtio_mmio_properties;
451 }
452
453 static const TypeInfo virtio_mmio_info = {
454 .name = TYPE_VIRTIO_MMIO,
455 .parent = TYPE_SYS_BUS_DEVICE,
456 .instance_size = sizeof(VirtIOMMIOProxy),
457 .class_init = virtio_mmio_class_init,
458 };
459
460 /* virtio-mmio-bus. */
461
462 static char *virtio_mmio_bus_get_dev_path(DeviceState *dev)
463 {
464 BusState *virtio_mmio_bus;
465 VirtIOMMIOProxy *virtio_mmio_proxy;
466 char *proxy_path;
467 SysBusDevice *proxy_sbd;
468 char *path;
469
470 virtio_mmio_bus = qdev_get_parent_bus(dev);
471 virtio_mmio_proxy = VIRTIO_MMIO(virtio_mmio_bus->parent);
472 proxy_path = qdev_get_dev_path(DEVICE(virtio_mmio_proxy));
473
474 /*
475 * If @format_transport_address is false, then we just perform the same as
476 * virtio_bus_get_dev_path(): we delegate the address formatting for the
477 * device on the virtio-mmio bus to the bus that the virtio-mmio proxy
478 * (i.e., the device that implements the virtio-mmio bus) resides on. In
479 * this case the base address of the virtio-mmio transport will be
480 * invisible.
481 */
482 if (!virtio_mmio_proxy->format_transport_address) {
483 return proxy_path;
484 }
485
486 /* Otherwise, we append the base address of the transport. */
487 proxy_sbd = SYS_BUS_DEVICE(virtio_mmio_proxy);
488 assert(proxy_sbd->num_mmio == 1);
489 assert(proxy_sbd->mmio[0].memory == &virtio_mmio_proxy->iomem);
490
491 if (proxy_path) {
492 path = g_strdup_printf("%s/virtio-mmio@" TARGET_FMT_plx, proxy_path,
493 proxy_sbd->mmio[0].addr);
494 } else {
495 path = g_strdup_printf("virtio-mmio@" TARGET_FMT_plx,
496 proxy_sbd->mmio[0].addr);
497 }
498 g_free(proxy_path);
499 return path;
500 }
501
502 static void virtio_mmio_bus_class_init(ObjectClass *klass, void *data)
503 {
504 BusClass *bus_class = BUS_CLASS(klass);
505 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
506
507 k->notify = virtio_mmio_update_irq;
508 k->save_config = virtio_mmio_save_config;
509 k->load_config = virtio_mmio_load_config;
510 k->set_guest_notifiers = virtio_mmio_set_guest_notifiers;
511 k->ioeventfd_enabled = virtio_mmio_ioeventfd_enabled;
512 k->ioeventfd_assign = virtio_mmio_ioeventfd_assign;
513 k->has_variable_vring_alignment = true;
514 bus_class->max_dev = 1;
515 bus_class->get_dev_path = virtio_mmio_bus_get_dev_path;
516 }
517
518 static const TypeInfo virtio_mmio_bus_info = {
519 .name = TYPE_VIRTIO_MMIO_BUS,
520 .parent = TYPE_VIRTIO_BUS,
521 .instance_size = sizeof(VirtioBusState),
522 .class_init = virtio_mmio_bus_class_init,
523 };
524
525 static void virtio_mmio_register_types(void)
526 {
527 type_register_static(&virtio_mmio_bus_info);
528 type_register_static(&virtio_mmio_info);
529 }
530
531 type_init(virtio_mmio_register_types)