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/qdev-properties.h"
26 #include "hw/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 "sysemu/kvm.h"
32 #include "sysemu/replay.h"
33 #include "hw/virtio/virtio-mmio.h"
34 #include "qemu/error-report.h"
38 static bool virtio_mmio_ioeventfd_enabled(DeviceState
*d
)
40 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
42 return (proxy
->flags
& VIRTIO_IOMMIO_FLAG_USE_IOEVENTFD
) != 0;
45 static int virtio_mmio_ioeventfd_assign(DeviceState
*d
,
46 EventNotifier
*notifier
,
49 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
52 memory_region_add_eventfd(&proxy
->iomem
, VIRTIO_MMIO_QUEUE_NOTIFY
, 4,
55 memory_region_del_eventfd(&proxy
->iomem
, VIRTIO_MMIO_QUEUE_NOTIFY
, 4,
61 static void virtio_mmio_start_ioeventfd(VirtIOMMIOProxy
*proxy
)
63 virtio_bus_start_ioeventfd(&proxy
->bus
);
66 static void virtio_mmio_stop_ioeventfd(VirtIOMMIOProxy
*proxy
)
68 virtio_bus_stop_ioeventfd(&proxy
->bus
);
71 static void virtio_mmio_soft_reset(VirtIOMMIOProxy
*proxy
)
79 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
80 proxy
->vqs
[i
].enabled
= 0;
84 static uint64_t virtio_mmio_read(void *opaque
, hwaddr offset
, unsigned size
)
86 VirtIOMMIOProxy
*proxy
= (VirtIOMMIOProxy
*)opaque
;
87 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
89 trace_virtio_mmio_read(offset
);
92 /* If no backend is present, we treat most registers as
93 * read-as-zero, except for the magic number, version and
94 * vendor ID. This is not strictly sanctioned by the virtio
95 * spec, but it allows us to provide transports with no backend
96 * plugged in which don't confuse Linux's virtio code: the
97 * probe won't complain about the bad magic number, but the
98 * device ID of zero means no backend will claim it.
101 case VIRTIO_MMIO_MAGIC_VALUE
:
103 case VIRTIO_MMIO_VERSION
:
105 return VIRT_VERSION_LEGACY
;
109 case VIRTIO_MMIO_VENDOR_ID
:
116 if (offset
>= VIRTIO_MMIO_CONFIG
) {
117 offset
-= VIRTIO_MMIO_CONFIG
;
121 return virtio_config_readb(vdev
, offset
);
123 return virtio_config_readw(vdev
, offset
);
125 return virtio_config_readl(vdev
, offset
);
132 return virtio_config_modern_readb(vdev
, offset
);
134 return virtio_config_modern_readw(vdev
, offset
);
136 return virtio_config_modern_readl(vdev
, offset
);
143 qemu_log_mask(LOG_GUEST_ERROR
,
144 "%s: wrong size access to register!\n",
149 case VIRTIO_MMIO_MAGIC_VALUE
:
151 case VIRTIO_MMIO_VERSION
:
153 return VIRT_VERSION_LEGACY
;
157 case VIRTIO_MMIO_DEVICE_ID
:
158 return vdev
->device_id
;
159 case VIRTIO_MMIO_VENDOR_ID
:
161 case VIRTIO_MMIO_DEVICE_FEATURES
:
163 if (proxy
->host_features_sel
) {
166 return vdev
->host_features
;
169 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
170 return (vdev
->host_features
& ~vdc
->legacy_features
)
171 >> (32 * proxy
->host_features_sel
);
173 case VIRTIO_MMIO_QUEUE_NUM_MAX
:
174 if (!virtio_queue_get_num(vdev
, vdev
->queue_sel
)) {
177 return VIRTQUEUE_MAX_SIZE
;
178 case VIRTIO_MMIO_QUEUE_PFN
:
179 if (!proxy
->legacy
) {
180 qemu_log_mask(LOG_GUEST_ERROR
,
181 "%s: read from legacy register (0x%"
182 HWADDR_PRIx
") in non-legacy mode\n",
186 return virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
187 >> proxy
->guest_page_shift
;
188 case VIRTIO_MMIO_QUEUE_READY
:
190 qemu_log_mask(LOG_GUEST_ERROR
,
191 "%s: read from non-legacy register (0x%"
192 HWADDR_PRIx
") in legacy mode\n",
196 return proxy
->vqs
[vdev
->queue_sel
].enabled
;
197 case VIRTIO_MMIO_INTERRUPT_STATUS
:
198 return qatomic_read(&vdev
->isr
);
199 case VIRTIO_MMIO_STATUS
:
201 case VIRTIO_MMIO_CONFIG_GENERATION
:
203 qemu_log_mask(LOG_GUEST_ERROR
,
204 "%s: read from non-legacy register (0x%"
205 HWADDR_PRIx
") in legacy mode\n",
209 return vdev
->generation
;
210 case VIRTIO_MMIO_SHM_LEN_LOW
:
211 case VIRTIO_MMIO_SHM_LEN_HIGH
:
213 * VIRTIO_MMIO_SHM_SEL is unimplemented
214 * according to the linux driver, if region length is -1
215 * the shared memory doesn't exist
218 case VIRTIO_MMIO_DEVICE_FEATURES_SEL
:
219 case VIRTIO_MMIO_DRIVER_FEATURES
:
220 case VIRTIO_MMIO_DRIVER_FEATURES_SEL
:
221 case VIRTIO_MMIO_GUEST_PAGE_SIZE
:
222 case VIRTIO_MMIO_QUEUE_SEL
:
223 case VIRTIO_MMIO_QUEUE_NUM
:
224 case VIRTIO_MMIO_QUEUE_ALIGN
:
225 case VIRTIO_MMIO_QUEUE_NOTIFY
:
226 case VIRTIO_MMIO_INTERRUPT_ACK
:
227 case VIRTIO_MMIO_QUEUE_DESC_LOW
:
228 case VIRTIO_MMIO_QUEUE_DESC_HIGH
:
229 case VIRTIO_MMIO_QUEUE_AVAIL_LOW
:
230 case VIRTIO_MMIO_QUEUE_AVAIL_HIGH
:
231 case VIRTIO_MMIO_QUEUE_USED_LOW
:
232 case VIRTIO_MMIO_QUEUE_USED_HIGH
:
233 qemu_log_mask(LOG_GUEST_ERROR
,
234 "%s: read of write-only register (0x%" HWADDR_PRIx
")\n",
238 qemu_log_mask(LOG_GUEST_ERROR
,
239 "%s: bad register offset (0x%" HWADDR_PRIx
")\n",
246 static void virtio_mmio_write(void *opaque
, hwaddr offset
, uint64_t value
,
249 VirtIOMMIOProxy
*proxy
= (VirtIOMMIOProxy
*)opaque
;
250 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
252 trace_virtio_mmio_write_offset(offset
, value
);
255 /* If no backend is present, we just make all registers
256 * write-ignored. This allows us to provide transports with
257 * no backend plugged in.
262 if (offset
>= VIRTIO_MMIO_CONFIG
) {
263 offset
-= VIRTIO_MMIO_CONFIG
;
267 virtio_config_writeb(vdev
, offset
, value
);
270 virtio_config_writew(vdev
, offset
, value
);
273 virtio_config_writel(vdev
, offset
, value
);
282 virtio_config_modern_writeb(vdev
, offset
, value
);
285 virtio_config_modern_writew(vdev
, offset
, value
);
288 virtio_config_modern_writel(vdev
, offset
, value
);
297 qemu_log_mask(LOG_GUEST_ERROR
,
298 "%s: wrong size access to register!\n",
303 case VIRTIO_MMIO_DEVICE_FEATURES_SEL
:
305 proxy
->host_features_sel
= 1;
307 proxy
->host_features_sel
= 0;
310 case VIRTIO_MMIO_DRIVER_FEATURES
:
312 if (proxy
->guest_features_sel
) {
313 qemu_log_mask(LOG_GUEST_ERROR
,
314 "%s: attempt to write guest features with "
315 "guest_features_sel > 0 in legacy mode\n",
318 virtio_set_features(vdev
, value
);
321 proxy
->guest_features
[proxy
->guest_features_sel
] = value
;
324 case VIRTIO_MMIO_DRIVER_FEATURES_SEL
:
326 proxy
->guest_features_sel
= 1;
328 proxy
->guest_features_sel
= 0;
331 case VIRTIO_MMIO_GUEST_PAGE_SIZE
:
332 if (!proxy
->legacy
) {
333 qemu_log_mask(LOG_GUEST_ERROR
,
334 "%s: write to legacy register (0x%"
335 HWADDR_PRIx
") in non-legacy mode\n",
339 proxy
->guest_page_shift
= ctz32(value
);
340 if (proxy
->guest_page_shift
> 31) {
341 proxy
->guest_page_shift
= 0;
343 trace_virtio_mmio_guest_page(value
, proxy
->guest_page_shift
);
345 case VIRTIO_MMIO_QUEUE_SEL
:
346 if (value
< VIRTIO_QUEUE_MAX
) {
347 vdev
->queue_sel
= value
;
350 case VIRTIO_MMIO_QUEUE_NUM
:
351 trace_virtio_mmio_queue_write(value
, VIRTQUEUE_MAX_SIZE
);
352 virtio_queue_set_num(vdev
, vdev
->queue_sel
, value
);
355 virtio_queue_update_rings(vdev
, vdev
->queue_sel
);
357 proxy
->vqs
[vdev
->queue_sel
].num
= value
;
360 case VIRTIO_MMIO_QUEUE_ALIGN
:
361 if (!proxy
->legacy
) {
362 qemu_log_mask(LOG_GUEST_ERROR
,
363 "%s: write to legacy register (0x%"
364 HWADDR_PRIx
") in non-legacy mode\n",
368 virtio_queue_set_align(vdev
, vdev
->queue_sel
, value
);
370 case VIRTIO_MMIO_QUEUE_PFN
:
371 if (!proxy
->legacy
) {
372 qemu_log_mask(LOG_GUEST_ERROR
,
373 "%s: write to legacy register (0x%"
374 HWADDR_PRIx
") in non-legacy mode\n",
381 virtio_queue_set_addr(vdev
, vdev
->queue_sel
,
382 value
<< proxy
->guest_page_shift
);
385 case VIRTIO_MMIO_QUEUE_READY
:
387 qemu_log_mask(LOG_GUEST_ERROR
,
388 "%s: write to non-legacy register (0x%"
389 HWADDR_PRIx
") in legacy mode\n",
394 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
395 proxy
->vqs
[vdev
->queue_sel
].num
);
396 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
397 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
398 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
399 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
400 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
401 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
402 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
403 proxy
->vqs
[vdev
->queue_sel
].enabled
= 1;
405 proxy
->vqs
[vdev
->queue_sel
].enabled
= 0;
408 case VIRTIO_MMIO_QUEUE_NOTIFY
:
409 if (value
< VIRTIO_QUEUE_MAX
) {
410 virtio_queue_notify(vdev
, value
);
413 case VIRTIO_MMIO_INTERRUPT_ACK
:
414 qatomic_and(&vdev
->isr
, ~value
);
415 virtio_update_irq(vdev
);
417 case VIRTIO_MMIO_STATUS
:
418 if (!(value
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
419 virtio_mmio_stop_ioeventfd(proxy
);
422 if (!proxy
->legacy
&& (value
& VIRTIO_CONFIG_S_FEATURES_OK
)) {
423 virtio_set_features(vdev
,
424 ((uint64_t)proxy
->guest_features
[1]) << 32 |
425 proxy
->guest_features
[0]);
428 virtio_set_status(vdev
, value
& 0xff);
430 if (value
& VIRTIO_CONFIG_S_DRIVER_OK
) {
431 virtio_mmio_start_ioeventfd(proxy
);
434 if (vdev
->status
== 0) {
436 virtio_mmio_soft_reset(proxy
);
439 case VIRTIO_MMIO_QUEUE_DESC_LOW
:
441 qemu_log_mask(LOG_GUEST_ERROR
,
442 "%s: write to non-legacy register (0x%"
443 HWADDR_PRIx
") in legacy mode\n",
447 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = value
;
449 case VIRTIO_MMIO_QUEUE_DESC_HIGH
:
451 qemu_log_mask(LOG_GUEST_ERROR
,
452 "%s: write to non-legacy register (0x%"
453 HWADDR_PRIx
") in legacy mode\n",
457 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = value
;
459 case VIRTIO_MMIO_QUEUE_AVAIL_LOW
:
461 qemu_log_mask(LOG_GUEST_ERROR
,
462 "%s: write to non-legacy register (0x%"
463 HWADDR_PRIx
") in legacy mode\n",
467 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = value
;
469 case VIRTIO_MMIO_QUEUE_AVAIL_HIGH
:
471 qemu_log_mask(LOG_GUEST_ERROR
,
472 "%s: write to non-legacy register (0x%"
473 HWADDR_PRIx
") in legacy mode\n",
477 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = value
;
479 case VIRTIO_MMIO_QUEUE_USED_LOW
:
481 qemu_log_mask(LOG_GUEST_ERROR
,
482 "%s: write to non-legacy register (0x%"
483 HWADDR_PRIx
") in legacy mode\n",
487 proxy
->vqs
[vdev
->queue_sel
].used
[0] = value
;
489 case VIRTIO_MMIO_QUEUE_USED_HIGH
:
491 qemu_log_mask(LOG_GUEST_ERROR
,
492 "%s: write to non-legacy register (0x%"
493 HWADDR_PRIx
") in legacy mode\n",
497 proxy
->vqs
[vdev
->queue_sel
].used
[1] = value
;
499 case VIRTIO_MMIO_MAGIC_VALUE
:
500 case VIRTIO_MMIO_VERSION
:
501 case VIRTIO_MMIO_DEVICE_ID
:
502 case VIRTIO_MMIO_VENDOR_ID
:
503 case VIRTIO_MMIO_DEVICE_FEATURES
:
504 case VIRTIO_MMIO_QUEUE_NUM_MAX
:
505 case VIRTIO_MMIO_INTERRUPT_STATUS
:
506 case VIRTIO_MMIO_CONFIG_GENERATION
:
507 qemu_log_mask(LOG_GUEST_ERROR
,
508 "%s: write to read-only register (0x%" HWADDR_PRIx
")\n",
513 qemu_log_mask(LOG_GUEST_ERROR
,
514 "%s: bad register offset (0x%" HWADDR_PRIx
")\n",
519 static const MemoryRegionOps virtio_legacy_mem_ops
= {
520 .read
= virtio_mmio_read
,
521 .write
= virtio_mmio_write
,
522 .endianness
= DEVICE_NATIVE_ENDIAN
,
525 static const MemoryRegionOps virtio_mem_ops
= {
526 .read
= virtio_mmio_read
,
527 .write
= virtio_mmio_write
,
528 .endianness
= DEVICE_LITTLE_ENDIAN
,
531 static void virtio_mmio_update_irq(DeviceState
*opaque
, uint16_t vector
)
533 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
534 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
540 level
= (qatomic_read(&vdev
->isr
) != 0);
541 trace_virtio_mmio_setting_irq(level
);
542 qemu_set_irq(proxy
->irq
, level
);
545 static int virtio_mmio_load_config(DeviceState
*opaque
, QEMUFile
*f
)
547 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
549 proxy
->host_features_sel
= qemu_get_be32(f
);
550 proxy
->guest_features_sel
= qemu_get_be32(f
);
551 proxy
->guest_page_shift
= qemu_get_be32(f
);
555 static void virtio_mmio_save_config(DeviceState
*opaque
, QEMUFile
*f
)
557 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
559 qemu_put_be32(f
, proxy
->host_features_sel
);
560 qemu_put_be32(f
, proxy
->guest_features_sel
);
561 qemu_put_be32(f
, proxy
->guest_page_shift
);
564 static const VMStateDescription vmstate_virtio_mmio_queue_state
= {
565 .name
= "virtio_mmio/queue_state",
567 .minimum_version_id
= 1,
568 .fields
= (VMStateField
[]) {
569 VMSTATE_UINT16(num
, VirtIOMMIOQueue
),
570 VMSTATE_BOOL(enabled
, VirtIOMMIOQueue
),
571 VMSTATE_UINT32_ARRAY(desc
, VirtIOMMIOQueue
, 2),
572 VMSTATE_UINT32_ARRAY(avail
, VirtIOMMIOQueue
, 2),
573 VMSTATE_UINT32_ARRAY(used
, VirtIOMMIOQueue
, 2),
574 VMSTATE_END_OF_LIST()
578 static const VMStateDescription vmstate_virtio_mmio_state_sub
= {
579 .name
= "virtio_mmio/state",
581 .minimum_version_id
= 1,
582 .fields
= (VMStateField
[]) {
583 VMSTATE_UINT32_ARRAY(guest_features
, VirtIOMMIOProxy
, 2),
584 VMSTATE_STRUCT_ARRAY(vqs
, VirtIOMMIOProxy
, VIRTIO_QUEUE_MAX
, 0,
585 vmstate_virtio_mmio_queue_state
,
587 VMSTATE_END_OF_LIST()
591 static const VMStateDescription vmstate_virtio_mmio
= {
592 .name
= "virtio_mmio",
594 .minimum_version_id
= 1,
595 .minimum_version_id_old
= 1,
596 .fields
= (VMStateField
[]) {
597 VMSTATE_END_OF_LIST()
599 .subsections
= (const VMStateDescription
* []) {
600 &vmstate_virtio_mmio_state_sub
,
605 static void virtio_mmio_save_extra_state(DeviceState
*opaque
, QEMUFile
*f
)
607 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
609 vmstate_save_state(f
, &vmstate_virtio_mmio
, proxy
, NULL
);
612 static int virtio_mmio_load_extra_state(DeviceState
*opaque
, QEMUFile
*f
)
614 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
616 return vmstate_load_state(f
, &vmstate_virtio_mmio
, proxy
, 1);
619 static bool virtio_mmio_has_extra_state(DeviceState
*opaque
)
621 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
623 return !proxy
->legacy
;
626 static void virtio_mmio_reset(DeviceState
*d
)
628 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
631 virtio_mmio_stop_ioeventfd(proxy
);
632 virtio_bus_reset(&proxy
->bus
);
633 proxy
->host_features_sel
= 0;
634 proxy
->guest_features_sel
= 0;
635 proxy
->guest_page_shift
= 0;
637 if (!proxy
->legacy
) {
638 proxy
->guest_features
[0] = proxy
->guest_features
[1] = 0;
640 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
641 proxy
->vqs
[i
].enabled
= 0;
642 proxy
->vqs
[i
].num
= 0;
643 proxy
->vqs
[i
].desc
[0] = proxy
->vqs
[i
].desc
[1] = 0;
644 proxy
->vqs
[i
].avail
[0] = proxy
->vqs
[i
].avail
[1] = 0;
645 proxy
->vqs
[i
].used
[0] = proxy
->vqs
[i
].used
[1] = 0;
650 static int virtio_mmio_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
653 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
654 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
655 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
656 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
657 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
660 int r
= event_notifier_init(notifier
, 0);
664 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
666 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
667 event_notifier_cleanup(notifier
);
670 if (vdc
->guest_notifier_mask
&& vdev
->use_guest_notifier_mask
) {
671 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
677 static int virtio_mmio_set_guest_notifiers(DeviceState
*d
, int nvqs
,
680 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
681 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
682 /* TODO: need to check if kvm-arm supports irqfd */
683 bool with_irqfd
= false;
686 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
688 for (n
= 0; n
< nvqs
; n
++) {
689 if (!virtio_queue_get_num(vdev
, n
)) {
693 r
= virtio_mmio_set_guest_notifier(d
, n
, assign
, with_irqfd
);
702 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
705 virtio_mmio_set_guest_notifier(d
, n
, !assign
, false);
710 static void virtio_mmio_pre_plugged(DeviceState
*d
, Error
**errp
)
712 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
713 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
715 if (!proxy
->legacy
) {
716 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
720 /* virtio-mmio device */
722 static Property virtio_mmio_properties
[] = {
723 DEFINE_PROP_BOOL("format_transport_address", VirtIOMMIOProxy
,
724 format_transport_address
, true),
725 DEFINE_PROP_BOOL("force-legacy", VirtIOMMIOProxy
, legacy
, true),
726 DEFINE_PROP_BIT("ioeventfd", VirtIOMMIOProxy
, flags
,
727 VIRTIO_IOMMIO_FLAG_USE_IOEVENTFD_BIT
, true),
728 DEFINE_PROP_END_OF_LIST(),
731 static void virtio_mmio_realizefn(DeviceState
*d
, Error
**errp
)
733 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
734 SysBusDevice
*sbd
= SYS_BUS_DEVICE(d
);
736 qbus_create_inplace(&proxy
->bus
, sizeof(proxy
->bus
), TYPE_VIRTIO_MMIO_BUS
,
738 sysbus_init_irq(sbd
, &proxy
->irq
);
740 if (!kvm_eventfds_enabled()) {
741 proxy
->flags
&= ~VIRTIO_IOMMIO_FLAG_USE_IOEVENTFD
;
744 /* fd-based ioevents can't be synchronized in record/replay */
745 if (replay_mode
!= REPLAY_MODE_NONE
) {
746 proxy
->flags
&= ~VIRTIO_IOMMIO_FLAG_USE_IOEVENTFD
;
750 memory_region_init_io(&proxy
->iomem
, OBJECT(d
),
751 &virtio_legacy_mem_ops
, proxy
,
752 TYPE_VIRTIO_MMIO
, 0x200);
754 memory_region_init_io(&proxy
->iomem
, OBJECT(d
),
755 &virtio_mem_ops
, proxy
,
756 TYPE_VIRTIO_MMIO
, 0x200);
758 sysbus_init_mmio(sbd
, &proxy
->iomem
);
761 static void virtio_mmio_class_init(ObjectClass
*klass
, void *data
)
763 DeviceClass
*dc
= DEVICE_CLASS(klass
);
765 dc
->realize
= virtio_mmio_realizefn
;
766 dc
->reset
= virtio_mmio_reset
;
767 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
768 device_class_set_props(dc
, virtio_mmio_properties
);
771 static const TypeInfo virtio_mmio_info
= {
772 .name
= TYPE_VIRTIO_MMIO
,
773 .parent
= TYPE_SYS_BUS_DEVICE
,
774 .instance_size
= sizeof(VirtIOMMIOProxy
),
775 .class_init
= virtio_mmio_class_init
,
778 /* virtio-mmio-bus. */
780 static char *virtio_mmio_bus_get_dev_path(DeviceState
*dev
)
782 BusState
*virtio_mmio_bus
;
783 VirtIOMMIOProxy
*virtio_mmio_proxy
;
786 MemoryRegionSection section
;
788 virtio_mmio_bus
= qdev_get_parent_bus(dev
);
789 virtio_mmio_proxy
= VIRTIO_MMIO(virtio_mmio_bus
->parent
);
790 proxy_path
= qdev_get_dev_path(DEVICE(virtio_mmio_proxy
));
793 * If @format_transport_address is false, then we just perform the same as
794 * virtio_bus_get_dev_path(): we delegate the address formatting for the
795 * device on the virtio-mmio bus to the bus that the virtio-mmio proxy
796 * (i.e., the device that implements the virtio-mmio bus) resides on. In
797 * this case the base address of the virtio-mmio transport will be
800 if (!virtio_mmio_proxy
->format_transport_address
) {
804 /* Otherwise, we append the base address of the transport. */
805 section
= memory_region_find(&virtio_mmio_proxy
->iomem
, 0, 0x200);
809 path
= g_strdup_printf("%s/virtio-mmio@" TARGET_FMT_plx
, proxy_path
,
810 section
.offset_within_address_space
);
812 path
= g_strdup_printf("virtio-mmio@" TARGET_FMT_plx
,
813 section
.offset_within_address_space
);
815 memory_region_unref(section
.mr
);
821 static void virtio_mmio_bus_class_init(ObjectClass
*klass
, void *data
)
823 BusClass
*bus_class
= BUS_CLASS(klass
);
824 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
826 k
->notify
= virtio_mmio_update_irq
;
827 k
->save_config
= virtio_mmio_save_config
;
828 k
->load_config
= virtio_mmio_load_config
;
829 k
->save_extra_state
= virtio_mmio_save_extra_state
;
830 k
->load_extra_state
= virtio_mmio_load_extra_state
;
831 k
->has_extra_state
= virtio_mmio_has_extra_state
;
832 k
->set_guest_notifiers
= virtio_mmio_set_guest_notifiers
;
833 k
->ioeventfd_enabled
= virtio_mmio_ioeventfd_enabled
;
834 k
->ioeventfd_assign
= virtio_mmio_ioeventfd_assign
;
835 k
->pre_plugged
= virtio_mmio_pre_plugged
;
836 k
->has_variable_vring_alignment
= true;
837 bus_class
->max_dev
= 1;
838 bus_class
->get_dev_path
= virtio_mmio_bus_get_dev_path
;
841 static const TypeInfo virtio_mmio_bus_info
= {
842 .name
= TYPE_VIRTIO_MMIO_BUS
,
843 .parent
= TYPE_VIRTIO_BUS
,
844 .instance_size
= sizeof(VirtioBusState
),
845 .class_init
= virtio_mmio_bus_class_init
,
848 static void virtio_mmio_register_types(void)
850 type_register_static(&virtio_mmio_bus_info
);
851 type_register_static(&virtio_mmio_info
);
854 type_init(virtio_mmio_register_types
)