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 "standard-headers/linux/virtio_mmio.h"
25 #include "hw/sysbus.h"
26 #include "hw/virtio/virtio.h"
27 #include "migration/qemu-file-types.h"
28 #include "qemu/host-utils.h"
29 #include "qemu/module.h"
30 #include "sysemu/kvm.h"
31 #include "hw/virtio/virtio-bus.h"
32 #include "qemu/error-report.h"
38 #define TYPE_VIRTIO_MMIO_BUS "virtio-mmio-bus"
39 #define VIRTIO_MMIO_BUS(obj) \
40 OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_MMIO_BUS)
41 #define VIRTIO_MMIO_BUS_GET_CLASS(obj) \
42 OBJECT_GET_CLASS(VirtioBusClass, (obj), TYPE_VIRTIO_MMIO_BUS)
43 #define VIRTIO_MMIO_BUS_CLASS(klass) \
44 OBJECT_CLASS_CHECK(VirtioBusClass, (klass), TYPE_VIRTIO_MMIO_BUS)
47 #define TYPE_VIRTIO_MMIO "virtio-mmio"
48 #define VIRTIO_MMIO(obj) \
49 OBJECT_CHECK(VirtIOMMIOProxy, (obj), TYPE_VIRTIO_MMIO)
51 #define VIRT_MAGIC 0x74726976 /* 'virt' */
52 #define VIRT_VERSION 1
53 #define VIRT_VENDOR 0x554D4551 /* 'QEMU' */
57 SysBusDevice parent_obj
;
60 /* Guest accessible state needing migration and reset */
61 uint32_t host_features_sel
;
62 uint32_t guest_features_sel
;
63 uint32_t guest_page_shift
;
66 bool format_transport_address
;
69 static bool virtio_mmio_ioeventfd_enabled(DeviceState
*d
)
71 return kvm_eventfds_enabled();
74 static int virtio_mmio_ioeventfd_assign(DeviceState
*d
,
75 EventNotifier
*notifier
,
78 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
81 memory_region_add_eventfd(&proxy
->iomem
, VIRTIO_MMIO_QUEUE_NOTIFY
, 4,
84 memory_region_del_eventfd(&proxy
->iomem
, VIRTIO_MMIO_QUEUE_NOTIFY
, 4,
90 static void virtio_mmio_start_ioeventfd(VirtIOMMIOProxy
*proxy
)
92 virtio_bus_start_ioeventfd(&proxy
->bus
);
95 static void virtio_mmio_stop_ioeventfd(VirtIOMMIOProxy
*proxy
)
97 virtio_bus_stop_ioeventfd(&proxy
->bus
);
100 static uint64_t virtio_mmio_read(void *opaque
, hwaddr offset
, unsigned size
)
102 VirtIOMMIOProxy
*proxy
= (VirtIOMMIOProxy
*)opaque
;
103 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
105 trace_virtio_mmio_read(offset
);
108 /* If no backend is present, we treat most registers as
109 * read-as-zero, except for the magic number, version and
110 * vendor ID. This is not strictly sanctioned by the virtio
111 * spec, but it allows us to provide transports with no backend
112 * plugged in which don't confuse Linux's virtio code: the
113 * probe won't complain about the bad magic number, but the
114 * device ID of zero means no backend will claim it.
117 case VIRTIO_MMIO_MAGIC_VALUE
:
119 case VIRTIO_MMIO_VERSION
:
121 case VIRTIO_MMIO_VENDOR_ID
:
128 if (offset
>= VIRTIO_MMIO_CONFIG
) {
129 offset
-= VIRTIO_MMIO_CONFIG
;
132 return virtio_config_readb(vdev
, offset
);
134 return virtio_config_readw(vdev
, offset
);
136 return virtio_config_readl(vdev
, offset
);
142 qemu_log_mask(LOG_GUEST_ERROR
,
143 "%s: wrong size access to register!\n",
148 case VIRTIO_MMIO_MAGIC_VALUE
:
150 case VIRTIO_MMIO_VERSION
:
152 case VIRTIO_MMIO_DEVICE_ID
:
153 return vdev
->device_id
;
154 case VIRTIO_MMIO_VENDOR_ID
:
156 case VIRTIO_MMIO_DEVICE_FEATURES
:
157 if (proxy
->host_features_sel
) {
160 return vdev
->host_features
;
161 case VIRTIO_MMIO_QUEUE_NUM_MAX
:
162 if (!virtio_queue_get_num(vdev
, vdev
->queue_sel
)) {
165 return VIRTQUEUE_MAX_SIZE
;
166 case VIRTIO_MMIO_QUEUE_PFN
:
167 return virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
168 >> proxy
->guest_page_shift
;
169 case VIRTIO_MMIO_INTERRUPT_STATUS
:
170 return atomic_read(&vdev
->isr
);
171 case VIRTIO_MMIO_STATUS
:
173 case VIRTIO_MMIO_DEVICE_FEATURES_SEL
:
174 case VIRTIO_MMIO_DRIVER_FEATURES
:
175 case VIRTIO_MMIO_DRIVER_FEATURES_SEL
:
176 case VIRTIO_MMIO_GUEST_PAGE_SIZE
:
177 case VIRTIO_MMIO_QUEUE_SEL
:
178 case VIRTIO_MMIO_QUEUE_NUM
:
179 case VIRTIO_MMIO_QUEUE_ALIGN
:
180 case VIRTIO_MMIO_QUEUE_NOTIFY
:
181 case VIRTIO_MMIO_INTERRUPT_ACK
:
182 qemu_log_mask(LOG_GUEST_ERROR
,
183 "%s: read of write-only register\n",
187 qemu_log_mask(LOG_GUEST_ERROR
, "%s: bad register offset\n", __func__
);
193 static void virtio_mmio_write(void *opaque
, hwaddr offset
, uint64_t value
,
196 VirtIOMMIOProxy
*proxy
= (VirtIOMMIOProxy
*)opaque
;
197 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
199 trace_virtio_mmio_write_offset(offset
, value
);
202 /* If no backend is present, we just make all registers
203 * write-ignored. This allows us to provide transports with
204 * no backend plugged in.
209 if (offset
>= VIRTIO_MMIO_CONFIG
) {
210 offset
-= VIRTIO_MMIO_CONFIG
;
213 virtio_config_writeb(vdev
, offset
, value
);
216 virtio_config_writew(vdev
, offset
, value
);
219 virtio_config_writel(vdev
, offset
, value
);
227 qemu_log_mask(LOG_GUEST_ERROR
,
228 "%s: wrong size access to register!\n",
233 case VIRTIO_MMIO_DEVICE_FEATURES_SEL
:
234 proxy
->host_features_sel
= value
;
236 case VIRTIO_MMIO_DRIVER_FEATURES
:
237 if (!proxy
->guest_features_sel
) {
238 virtio_set_features(vdev
, value
);
241 case VIRTIO_MMIO_DRIVER_FEATURES_SEL
:
242 proxy
->guest_features_sel
= value
;
244 case VIRTIO_MMIO_GUEST_PAGE_SIZE
:
245 proxy
->guest_page_shift
= ctz32(value
);
246 if (proxy
->guest_page_shift
> 31) {
247 proxy
->guest_page_shift
= 0;
249 trace_virtio_mmio_guest_page(value
, proxy
->guest_page_shift
);
251 case VIRTIO_MMIO_QUEUE_SEL
:
252 if (value
< VIRTIO_QUEUE_MAX
) {
253 vdev
->queue_sel
= value
;
256 case VIRTIO_MMIO_QUEUE_NUM
:
257 trace_virtio_mmio_queue_write(value
, VIRTQUEUE_MAX_SIZE
);
258 virtio_queue_set_num(vdev
, vdev
->queue_sel
, value
);
259 /* Note: only call this function for legacy devices */
260 virtio_queue_update_rings(vdev
, vdev
->queue_sel
);
262 case VIRTIO_MMIO_QUEUE_ALIGN
:
263 /* Note: this is only valid for legacy devices */
264 virtio_queue_set_align(vdev
, vdev
->queue_sel
, value
);
266 case VIRTIO_MMIO_QUEUE_PFN
:
270 virtio_queue_set_addr(vdev
, vdev
->queue_sel
,
271 value
<< proxy
->guest_page_shift
);
274 case VIRTIO_MMIO_QUEUE_NOTIFY
:
275 if (value
< VIRTIO_QUEUE_MAX
) {
276 virtio_queue_notify(vdev
, value
);
279 case VIRTIO_MMIO_INTERRUPT_ACK
:
280 atomic_and(&vdev
->isr
, ~value
);
281 virtio_update_irq(vdev
);
283 case VIRTIO_MMIO_STATUS
:
284 if (!(value
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
285 virtio_mmio_stop_ioeventfd(proxy
);
288 virtio_set_status(vdev
, value
& 0xff);
290 if (value
& VIRTIO_CONFIG_S_DRIVER_OK
) {
291 virtio_mmio_start_ioeventfd(proxy
);
294 if (vdev
->status
== 0) {
298 case VIRTIO_MMIO_MAGIC_VALUE
:
299 case VIRTIO_MMIO_VERSION
:
300 case VIRTIO_MMIO_DEVICE_ID
:
301 case VIRTIO_MMIO_VENDOR_ID
:
302 case VIRTIO_MMIO_DEVICE_FEATURES
:
303 case VIRTIO_MMIO_QUEUE_NUM_MAX
:
304 case VIRTIO_MMIO_INTERRUPT_STATUS
:
305 qemu_log_mask(LOG_GUEST_ERROR
,
306 "%s: write to readonly register\n",
311 qemu_log_mask(LOG_GUEST_ERROR
, "%s: bad register offset\n", __func__
);
315 static const MemoryRegionOps virtio_mem_ops
= {
316 .read
= virtio_mmio_read
,
317 .write
= virtio_mmio_write
,
318 .endianness
= DEVICE_NATIVE_ENDIAN
,
321 static void virtio_mmio_update_irq(DeviceState
*opaque
, uint16_t vector
)
323 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
324 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
330 level
= (atomic_read(&vdev
->isr
) != 0);
331 trace_virtio_mmio_setting_irq(level
);
332 qemu_set_irq(proxy
->irq
, level
);
335 static int virtio_mmio_load_config(DeviceState
*opaque
, QEMUFile
*f
)
337 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
339 proxy
->host_features_sel
= qemu_get_be32(f
);
340 proxy
->guest_features_sel
= qemu_get_be32(f
);
341 proxy
->guest_page_shift
= qemu_get_be32(f
);
345 static void virtio_mmio_save_config(DeviceState
*opaque
, QEMUFile
*f
)
347 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
349 qemu_put_be32(f
, proxy
->host_features_sel
);
350 qemu_put_be32(f
, proxy
->guest_features_sel
);
351 qemu_put_be32(f
, proxy
->guest_page_shift
);
354 static void virtio_mmio_reset(DeviceState
*d
)
356 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
358 virtio_mmio_stop_ioeventfd(proxy
);
359 virtio_bus_reset(&proxy
->bus
);
360 proxy
->host_features_sel
= 0;
361 proxy
->guest_features_sel
= 0;
362 proxy
->guest_page_shift
= 0;
365 static int virtio_mmio_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
368 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
369 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
370 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
371 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
372 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
375 int r
= event_notifier_init(notifier
, 0);
379 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
381 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
382 event_notifier_cleanup(notifier
);
385 if (vdc
->guest_notifier_mask
&& vdev
->use_guest_notifier_mask
) {
386 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
392 static int virtio_mmio_set_guest_notifiers(DeviceState
*d
, int nvqs
,
395 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
396 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
397 /* TODO: need to check if kvm-arm supports irqfd */
398 bool with_irqfd
= false;
401 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
403 for (n
= 0; n
< nvqs
; n
++) {
404 if (!virtio_queue_get_num(vdev
, n
)) {
408 r
= virtio_mmio_set_guest_notifier(d
, n
, assign
, with_irqfd
);
417 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
420 virtio_mmio_set_guest_notifier(d
, n
, !assign
, false);
425 /* virtio-mmio device */
427 static Property virtio_mmio_properties
[] = {
428 DEFINE_PROP_BOOL("format_transport_address", VirtIOMMIOProxy
,
429 format_transport_address
, true),
430 DEFINE_PROP_END_OF_LIST(),
433 static void virtio_mmio_realizefn(DeviceState
*d
, Error
**errp
)
435 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
436 SysBusDevice
*sbd
= SYS_BUS_DEVICE(d
);
438 qbus_create_inplace(&proxy
->bus
, sizeof(proxy
->bus
), TYPE_VIRTIO_MMIO_BUS
,
440 sysbus_init_irq(sbd
, &proxy
->irq
);
441 memory_region_init_io(&proxy
->iomem
, OBJECT(d
), &virtio_mem_ops
, proxy
,
442 TYPE_VIRTIO_MMIO
, 0x200);
443 sysbus_init_mmio(sbd
, &proxy
->iomem
);
446 static void virtio_mmio_class_init(ObjectClass
*klass
, void *data
)
448 DeviceClass
*dc
= DEVICE_CLASS(klass
);
450 dc
->realize
= virtio_mmio_realizefn
;
451 dc
->reset
= virtio_mmio_reset
;
452 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
453 dc
->props
= virtio_mmio_properties
;
456 static const TypeInfo virtio_mmio_info
= {
457 .name
= TYPE_VIRTIO_MMIO
,
458 .parent
= TYPE_SYS_BUS_DEVICE
,
459 .instance_size
= sizeof(VirtIOMMIOProxy
),
460 .class_init
= virtio_mmio_class_init
,
463 /* virtio-mmio-bus. */
465 static char *virtio_mmio_bus_get_dev_path(DeviceState
*dev
)
467 BusState
*virtio_mmio_bus
;
468 VirtIOMMIOProxy
*virtio_mmio_proxy
;
470 SysBusDevice
*proxy_sbd
;
473 virtio_mmio_bus
= qdev_get_parent_bus(dev
);
474 virtio_mmio_proxy
= VIRTIO_MMIO(virtio_mmio_bus
->parent
);
475 proxy_path
= qdev_get_dev_path(DEVICE(virtio_mmio_proxy
));
478 * If @format_transport_address is false, then we just perform the same as
479 * virtio_bus_get_dev_path(): we delegate the address formatting for the
480 * device on the virtio-mmio bus to the bus that the virtio-mmio proxy
481 * (i.e., the device that implements the virtio-mmio bus) resides on. In
482 * this case the base address of the virtio-mmio transport will be
485 if (!virtio_mmio_proxy
->format_transport_address
) {
489 /* Otherwise, we append the base address of the transport. */
490 proxy_sbd
= SYS_BUS_DEVICE(virtio_mmio_proxy
);
491 assert(proxy_sbd
->num_mmio
== 1);
492 assert(proxy_sbd
->mmio
[0].memory
== &virtio_mmio_proxy
->iomem
);
495 path
= g_strdup_printf("%s/virtio-mmio@" TARGET_FMT_plx
, proxy_path
,
496 proxy_sbd
->mmio
[0].addr
);
498 path
= g_strdup_printf("virtio-mmio@" TARGET_FMT_plx
,
499 proxy_sbd
->mmio
[0].addr
);
505 static void virtio_mmio_bus_class_init(ObjectClass
*klass
, void *data
)
507 BusClass
*bus_class
= BUS_CLASS(klass
);
508 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
510 k
->notify
= virtio_mmio_update_irq
;
511 k
->save_config
= virtio_mmio_save_config
;
512 k
->load_config
= virtio_mmio_load_config
;
513 k
->set_guest_notifiers
= virtio_mmio_set_guest_notifiers
;
514 k
->ioeventfd_enabled
= virtio_mmio_ioeventfd_enabled
;
515 k
->ioeventfd_assign
= virtio_mmio_ioeventfd_assign
;
516 k
->has_variable_vring_alignment
= true;
517 bus_class
->max_dev
= 1;
518 bus_class
->get_dev_path
= virtio_mmio_bus_get_dev_path
;
521 static const TypeInfo virtio_mmio_bus_info
= {
522 .name
= TYPE_VIRTIO_MMIO_BUS
,
523 .parent
= TYPE_VIRTIO_BUS
,
524 .instance_size
= sizeof(VirtioBusState
),
525 .class_init
= virtio_mmio_bus_class_init
,
528 static void virtio_mmio_register_types(void)
530 type_register_static(&virtio_mmio_bus_info
);
531 type_register_static(&virtio_mmio_info
);
534 type_init(virtio_mmio_register_types
)