4 * Copyright (c) 2011 Linaro Limited
7 * Peter Maydell <peter.maydell@linaro.org>
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.
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.
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/>.
22 #include "qemu/osdep.h"
23 #include "hw/sysbus.h"
24 #include "hw/virtio/virtio.h"
25 #include "qemu/host-utils.h"
26 #include "sysemu/kvm.h"
27 #include "hw/virtio/virtio-bus.h"
28 #include "qemu/error-report.h"
30 /* #define DEBUG_VIRTIO_MMIO */
32 #ifdef DEBUG_VIRTIO_MMIO
34 #define DPRINTF(fmt, ...) \
35 do { printf("virtio_mmio: " fmt , ## __VA_ARGS__); } while (0)
37 #define DPRINTF(fmt, ...) do {} while (0)
42 #define TYPE_VIRTIO_MMIO_BUS "virtio-mmio-bus"
43 #define VIRTIO_MMIO_BUS(obj) \
44 OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_MMIO_BUS)
45 #define VIRTIO_MMIO_BUS_GET_CLASS(obj) \
46 OBJECT_GET_CLASS(VirtioBusClass, (obj), TYPE_VIRTIO_MMIO_BUS)
47 #define VIRTIO_MMIO_BUS_CLASS(klass) \
48 OBJECT_CLASS_CHECK(VirtioBusClass, (klass), TYPE_VIRTIO_MMIO_BUS)
51 #define TYPE_VIRTIO_MMIO "virtio-mmio"
52 #define VIRTIO_MMIO(obj) \
53 OBJECT_CHECK(VirtIOMMIOProxy, (obj), TYPE_VIRTIO_MMIO)
55 /* Memory mapped register offsets */
56 #define VIRTIO_MMIO_MAGIC 0x0
57 #define VIRTIO_MMIO_VERSION 0x4
58 #define VIRTIO_MMIO_DEVICEID 0x8
59 #define VIRTIO_MMIO_VENDORID 0xc
60 #define VIRTIO_MMIO_HOSTFEATURES 0x10
61 #define VIRTIO_MMIO_HOSTFEATURESSEL 0x14
62 #define VIRTIO_MMIO_GUESTFEATURES 0x20
63 #define VIRTIO_MMIO_GUESTFEATURESSEL 0x24
64 #define VIRTIO_MMIO_GUESTPAGESIZE 0x28
65 #define VIRTIO_MMIO_QUEUESEL 0x30
66 #define VIRTIO_MMIO_QUEUENUMMAX 0x34
67 #define VIRTIO_MMIO_QUEUENUM 0x38
68 #define VIRTIO_MMIO_QUEUEALIGN 0x3c
69 #define VIRTIO_MMIO_QUEUEPFN 0x40
70 #define VIRTIO_MMIO_QUEUENOTIFY 0x50
71 #define VIRTIO_MMIO_INTERRUPTSTATUS 0x60
72 #define VIRTIO_MMIO_INTERRUPTACK 0x64
73 #define VIRTIO_MMIO_STATUS 0x70
74 /* Device specific config space starts here */
75 #define VIRTIO_MMIO_CONFIG 0x100
77 #define VIRT_MAGIC 0x74726976 /* 'virt' */
78 #define VIRT_VERSION 1
79 #define VIRT_VENDOR 0x554D4551 /* 'QEMU' */
83 SysBusDevice parent_obj
;
86 /* Guest accessible state needing migration and reset */
87 uint32_t host_features_sel
;
88 uint32_t guest_features_sel
;
89 uint32_t guest_page_shift
;
92 bool ioeventfd_disabled
;
93 bool ioeventfd_started
;
96 static int virtio_mmio_set_host_notifier_internal(VirtIOMMIOProxy
*proxy
,
100 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
101 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
102 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
106 r
= event_notifier_init(notifier
, 1);
108 error_report("%s: unable to init event notifier: %d",
112 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
113 memory_region_add_eventfd(&proxy
->iomem
, VIRTIO_MMIO_QUEUENOTIFY
, 4,
116 memory_region_del_eventfd(&proxy
->iomem
, VIRTIO_MMIO_QUEUENOTIFY
, 4,
118 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
119 event_notifier_cleanup(notifier
);
124 static void virtio_mmio_start_ioeventfd(VirtIOMMIOProxy
*proxy
)
126 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
129 if (!kvm_eventfds_enabled() ||
130 proxy
->ioeventfd_disabled
||
131 proxy
->ioeventfd_started
) {
135 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
136 if (!virtio_queue_get_num(vdev
, n
)) {
140 r
= virtio_mmio_set_host_notifier_internal(proxy
, n
, true, true);
145 proxy
->ioeventfd_started
= true;
150 if (!virtio_queue_get_num(vdev
, n
)) {
154 r
= virtio_mmio_set_host_notifier_internal(proxy
, n
, false, false);
157 proxy
->ioeventfd_started
= false;
158 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
161 static void virtio_mmio_stop_ioeventfd(VirtIOMMIOProxy
*proxy
)
165 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
167 if (!proxy
->ioeventfd_started
) {
171 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
172 if (!virtio_queue_get_num(vdev
, n
)) {
176 r
= virtio_mmio_set_host_notifier_internal(proxy
, n
, false, false);
179 proxy
->ioeventfd_started
= false;
182 static uint64_t virtio_mmio_read(void *opaque
, hwaddr offset
, unsigned size
)
184 VirtIOMMIOProxy
*proxy
= (VirtIOMMIOProxy
*)opaque
;
185 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
187 DPRINTF("virtio_mmio_read offset 0x%x\n", (int)offset
);
190 /* If no backend is present, we treat most registers as
191 * read-as-zero, except for the magic number, version and
192 * vendor ID. This is not strictly sanctioned by the virtio
193 * spec, but it allows us to provide transports with no backend
194 * plugged in which don't confuse Linux's virtio code: the
195 * probe won't complain about the bad magic number, but the
196 * device ID of zero means no backend will claim it.
199 case VIRTIO_MMIO_MAGIC
:
201 case VIRTIO_MMIO_VERSION
:
203 case VIRTIO_MMIO_VENDORID
:
210 if (offset
>= VIRTIO_MMIO_CONFIG
) {
211 offset
-= VIRTIO_MMIO_CONFIG
;
214 return virtio_config_readb(vdev
, offset
);
216 return virtio_config_readw(vdev
, offset
);
218 return virtio_config_readl(vdev
, offset
);
224 DPRINTF("wrong size access to register!\n");
228 case VIRTIO_MMIO_MAGIC
:
230 case VIRTIO_MMIO_VERSION
:
232 case VIRTIO_MMIO_DEVICEID
:
233 return vdev
->device_id
;
234 case VIRTIO_MMIO_VENDORID
:
236 case VIRTIO_MMIO_HOSTFEATURES
:
237 if (proxy
->host_features_sel
) {
240 return vdev
->host_features
;
241 case VIRTIO_MMIO_QUEUENUMMAX
:
242 if (!virtio_queue_get_num(vdev
, vdev
->queue_sel
)) {
245 return VIRTQUEUE_MAX_SIZE
;
246 case VIRTIO_MMIO_QUEUEPFN
:
247 return virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
248 >> proxy
->guest_page_shift
;
249 case VIRTIO_MMIO_INTERRUPTSTATUS
:
251 case VIRTIO_MMIO_STATUS
:
253 case VIRTIO_MMIO_HOSTFEATURESSEL
:
254 case VIRTIO_MMIO_GUESTFEATURES
:
255 case VIRTIO_MMIO_GUESTFEATURESSEL
:
256 case VIRTIO_MMIO_GUESTPAGESIZE
:
257 case VIRTIO_MMIO_QUEUESEL
:
258 case VIRTIO_MMIO_QUEUENUM
:
259 case VIRTIO_MMIO_QUEUEALIGN
:
260 case VIRTIO_MMIO_QUEUENOTIFY
:
261 case VIRTIO_MMIO_INTERRUPTACK
:
262 DPRINTF("read of write-only register\n");
265 DPRINTF("bad register offset\n");
271 static void virtio_mmio_write(void *opaque
, hwaddr offset
, uint64_t value
,
274 VirtIOMMIOProxy
*proxy
= (VirtIOMMIOProxy
*)opaque
;
275 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
277 DPRINTF("virtio_mmio_write offset 0x%x value 0x%" PRIx64
"\n",
281 /* If no backend is present, we just make all registers
282 * write-ignored. This allows us to provide transports with
283 * no backend plugged in.
288 if (offset
>= VIRTIO_MMIO_CONFIG
) {
289 offset
-= VIRTIO_MMIO_CONFIG
;
292 virtio_config_writeb(vdev
, offset
, value
);
295 virtio_config_writew(vdev
, offset
, value
);
298 virtio_config_writel(vdev
, offset
, value
);
306 DPRINTF("wrong size access to register!\n");
310 case VIRTIO_MMIO_HOSTFEATURESSEL
:
311 proxy
->host_features_sel
= value
;
313 case VIRTIO_MMIO_GUESTFEATURES
:
314 if (!proxy
->guest_features_sel
) {
315 virtio_set_features(vdev
, value
);
318 case VIRTIO_MMIO_GUESTFEATURESSEL
:
319 proxy
->guest_features_sel
= value
;
321 case VIRTIO_MMIO_GUESTPAGESIZE
:
322 proxy
->guest_page_shift
= ctz32(value
);
323 if (proxy
->guest_page_shift
> 31) {
324 proxy
->guest_page_shift
= 0;
326 DPRINTF("guest page size %" PRIx64
" shift %d\n", value
,
327 proxy
->guest_page_shift
);
329 case VIRTIO_MMIO_QUEUESEL
:
330 if (value
< VIRTIO_QUEUE_MAX
) {
331 vdev
->queue_sel
= value
;
334 case VIRTIO_MMIO_QUEUENUM
:
335 DPRINTF("mmio_queue write %d max %d\n", (int)value
, VIRTQUEUE_MAX_SIZE
);
336 virtio_queue_set_num(vdev
, vdev
->queue_sel
, value
);
337 /* Note: only call this function for legacy devices */
338 virtio_queue_update_rings(vdev
, vdev
->queue_sel
);
340 case VIRTIO_MMIO_QUEUEALIGN
:
341 /* Note: this is only valid for legacy devices */
342 virtio_queue_set_align(vdev
, vdev
->queue_sel
, value
);
344 case VIRTIO_MMIO_QUEUEPFN
:
348 virtio_queue_set_addr(vdev
, vdev
->queue_sel
,
349 value
<< proxy
->guest_page_shift
);
352 case VIRTIO_MMIO_QUEUENOTIFY
:
353 if (value
< VIRTIO_QUEUE_MAX
) {
354 virtio_queue_notify(vdev
, value
);
357 case VIRTIO_MMIO_INTERRUPTACK
:
359 virtio_update_irq(vdev
);
361 case VIRTIO_MMIO_STATUS
:
362 if (!(value
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
363 virtio_mmio_stop_ioeventfd(proxy
);
366 virtio_set_status(vdev
, value
& 0xff);
368 if (value
& VIRTIO_CONFIG_S_DRIVER_OK
) {
369 virtio_mmio_start_ioeventfd(proxy
);
372 if (vdev
->status
== 0) {
376 case VIRTIO_MMIO_MAGIC
:
377 case VIRTIO_MMIO_VERSION
:
378 case VIRTIO_MMIO_DEVICEID
:
379 case VIRTIO_MMIO_VENDORID
:
380 case VIRTIO_MMIO_HOSTFEATURES
:
381 case VIRTIO_MMIO_QUEUENUMMAX
:
382 case VIRTIO_MMIO_INTERRUPTSTATUS
:
383 DPRINTF("write to readonly register\n");
387 DPRINTF("bad register offset\n");
391 static const MemoryRegionOps virtio_mem_ops
= {
392 .read
= virtio_mmio_read
,
393 .write
= virtio_mmio_write
,
394 .endianness
= DEVICE_NATIVE_ENDIAN
,
397 static void virtio_mmio_update_irq(DeviceState
*opaque
, uint16_t vector
)
399 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
400 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
406 level
= (vdev
->isr
!= 0);
407 DPRINTF("virtio_mmio setting IRQ %d\n", level
);
408 qemu_set_irq(proxy
->irq
, level
);
411 static int virtio_mmio_load_config(DeviceState
*opaque
, QEMUFile
*f
)
413 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
415 proxy
->host_features_sel
= qemu_get_be32(f
);
416 proxy
->guest_features_sel
= qemu_get_be32(f
);
417 proxy
->guest_page_shift
= qemu_get_be32(f
);
421 static void virtio_mmio_save_config(DeviceState
*opaque
, QEMUFile
*f
)
423 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
425 qemu_put_be32(f
, proxy
->host_features_sel
);
426 qemu_put_be32(f
, proxy
->guest_features_sel
);
427 qemu_put_be32(f
, proxy
->guest_page_shift
);
430 static void virtio_mmio_reset(DeviceState
*d
)
432 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
434 virtio_mmio_stop_ioeventfd(proxy
);
435 virtio_bus_reset(&proxy
->bus
);
436 proxy
->host_features_sel
= 0;
437 proxy
->guest_features_sel
= 0;
438 proxy
->guest_page_shift
= 0;
441 static int virtio_mmio_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
444 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
445 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
446 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
447 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
448 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
451 int r
= event_notifier_init(notifier
, 0);
455 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
457 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
458 event_notifier_cleanup(notifier
);
461 if (vdc
->guest_notifier_mask
) {
462 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
468 static int virtio_mmio_set_guest_notifiers(DeviceState
*d
, int nvqs
,
471 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
472 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
473 /* TODO: need to check if kvm-arm supports irqfd */
474 bool with_irqfd
= false;
477 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
479 for (n
= 0; n
< nvqs
; n
++) {
480 if (!virtio_queue_get_num(vdev
, n
)) {
484 r
= virtio_mmio_set_guest_notifier(d
, n
, assign
, with_irqfd
);
493 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
496 virtio_mmio_set_guest_notifier(d
, n
, !assign
, false);
501 static int virtio_mmio_set_host_notifier(DeviceState
*opaque
, int n
,
504 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
506 /* Stop using ioeventfd for virtqueue kick if the device starts using host
507 * notifiers. This makes it easy to avoid stepping on each others' toes.
509 proxy
->ioeventfd_disabled
= assign
;
511 virtio_mmio_stop_ioeventfd(proxy
);
513 /* We don't need to start here: it's not needed because backend
514 * currently only stops on status change away from ok,
515 * reset, vmstop and such. If we do add code to start here,
516 * need to check vmstate, device state etc. */
517 return virtio_mmio_set_host_notifier_internal(proxy
, n
, assign
, false);
520 /* virtio-mmio device */
522 static void virtio_mmio_realizefn(DeviceState
*d
, Error
**errp
)
524 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
525 SysBusDevice
*sbd
= SYS_BUS_DEVICE(d
);
527 qbus_create_inplace(&proxy
->bus
, sizeof(proxy
->bus
), TYPE_VIRTIO_MMIO_BUS
,
529 sysbus_init_irq(sbd
, &proxy
->irq
);
530 memory_region_init_io(&proxy
->iomem
, OBJECT(d
), &virtio_mem_ops
, proxy
,
531 TYPE_VIRTIO_MMIO
, 0x200);
532 sysbus_init_mmio(sbd
, &proxy
->iomem
);
535 static void virtio_mmio_class_init(ObjectClass
*klass
, void *data
)
537 DeviceClass
*dc
= DEVICE_CLASS(klass
);
539 dc
->realize
= virtio_mmio_realizefn
;
540 dc
->reset
= virtio_mmio_reset
;
541 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
544 static const TypeInfo virtio_mmio_info
= {
545 .name
= TYPE_VIRTIO_MMIO
,
546 .parent
= TYPE_SYS_BUS_DEVICE
,
547 .instance_size
= sizeof(VirtIOMMIOProxy
),
548 .class_init
= virtio_mmio_class_init
,
551 /* virtio-mmio-bus. */
553 static void virtio_mmio_bus_class_init(ObjectClass
*klass
, void *data
)
555 BusClass
*bus_class
= BUS_CLASS(klass
);
556 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
558 k
->notify
= virtio_mmio_update_irq
;
559 k
->save_config
= virtio_mmio_save_config
;
560 k
->load_config
= virtio_mmio_load_config
;
561 k
->set_host_notifier
= virtio_mmio_set_host_notifier
;
562 k
->set_guest_notifiers
= virtio_mmio_set_guest_notifiers
;
563 k
->has_variable_vring_alignment
= true;
564 bus_class
->max_dev
= 1;
567 static const TypeInfo virtio_mmio_bus_info
= {
568 .name
= TYPE_VIRTIO_MMIO_BUS
,
569 .parent
= TYPE_VIRTIO_BUS
,
570 .instance_size
= sizeof(VirtioBusState
),
571 .class_init
= virtio_mmio_bus_class_init
,
574 static void virtio_mmio_register_types(void)
576 type_register_static(&virtio_mmio_bus_info
);
577 type_register_static(&virtio_mmio_info
);
580 type_init(virtio_mmio_register_types
)