4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
21 #include "virtio-blk.h"
22 #include "virtio-net.h"
23 #include "virtio-serial.h"
24 #include "virtio-scsi.h"
26 #include "qemu-error.h"
33 #include "virtio-pci.h"
36 /* from Linux's linux/virtio_pci.h */
38 /* A 32-bit r/o bitmask of the features supported by the host */
39 #define VIRTIO_PCI_HOST_FEATURES 0
41 /* A 32-bit r/w bitmask of features activated by the guest */
42 #define VIRTIO_PCI_GUEST_FEATURES 4
44 /* A 32-bit r/w PFN for the currently selected queue */
45 #define VIRTIO_PCI_QUEUE_PFN 8
47 /* A 16-bit r/o queue size for the currently selected queue */
48 #define VIRTIO_PCI_QUEUE_NUM 12
50 /* A 16-bit r/w queue selector */
51 #define VIRTIO_PCI_QUEUE_SEL 14
53 /* A 16-bit r/w queue notifier */
54 #define VIRTIO_PCI_QUEUE_NOTIFY 16
56 /* An 8-bit device status register. */
57 #define VIRTIO_PCI_STATUS 18
59 /* An 8-bit r/o interrupt status register. Reading the value will return the
60 * current contents of the ISR and will also clear it. This is effectively
61 * a read-and-acknowledge. */
62 #define VIRTIO_PCI_ISR 19
64 /* MSI-X registers: only enabled if MSI-X is enabled. */
65 /* A 16-bit vector for configuration changes. */
66 #define VIRTIO_MSI_CONFIG_VECTOR 20
67 /* A 16-bit vector for selected queue notifications. */
68 #define VIRTIO_MSI_QUEUE_VECTOR 22
70 /* Config space size */
71 #define VIRTIO_PCI_CONFIG_NOMSI 20
72 #define VIRTIO_PCI_CONFIG_MSI 24
73 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
74 VIRTIO_PCI_CONFIG_MSI : \
75 VIRTIO_PCI_CONFIG_NOMSI)
77 /* The remaining space is defined by each driver as the per-driver
78 * configuration space */
79 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
80 VIRTIO_PCI_CONFIG_MSI : \
81 VIRTIO_PCI_CONFIG_NOMSI)
83 /* How many bits to shift physical queue address written to QUEUE_PFN.
84 * 12 is historical, and due to x86 page size. */
85 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
87 /* Flags track per-device state like workarounds for quirks in older guests. */
88 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
90 /* QEMU doesn't strictly need write barriers since everything runs in
91 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
92 * KVM or if kqemu gets SMP support.
94 #define wmb() do { } while (0)
96 /* HACK for virtio to determine if it's running a big endian guest */
97 bool virtio_is_big_endian(void);
101 static void virtio_pci_notify(void *opaque
, uint16_t vector
)
103 VirtIOPCIProxy
*proxy
= opaque
;
104 if (msix_enabled(&proxy
->pci_dev
))
105 msix_notify(&proxy
->pci_dev
, vector
);
107 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
110 static void virtio_pci_save_config(void * opaque
, QEMUFile
*f
)
112 VirtIOPCIProxy
*proxy
= opaque
;
113 pci_device_save(&proxy
->pci_dev
, f
);
114 msix_save(&proxy
->pci_dev
, f
);
115 if (msix_present(&proxy
->pci_dev
))
116 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
119 static void virtio_pci_save_queue(void * opaque
, int n
, QEMUFile
*f
)
121 VirtIOPCIProxy
*proxy
= opaque
;
122 if (msix_present(&proxy
->pci_dev
))
123 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
126 static int virtio_pci_load_config(void * opaque
, QEMUFile
*f
)
128 VirtIOPCIProxy
*proxy
= opaque
;
130 ret
= pci_device_load(&proxy
->pci_dev
, f
);
134 msix_load(&proxy
->pci_dev
, f
);
135 if (msix_present(&proxy
->pci_dev
)) {
136 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
138 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
140 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
141 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
146 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
148 VirtIOPCIProxy
*proxy
= opaque
;
150 if (msix_present(&proxy
->pci_dev
)) {
151 qemu_get_be16s(f
, &vector
);
153 vector
= VIRTIO_NO_VECTOR
;
155 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
156 if (vector
!= VIRTIO_NO_VECTOR
) {
157 return msix_vector_use(&proxy
->pci_dev
, vector
);
162 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
163 int n
, bool assign
, bool set_handler
)
165 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
166 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
170 r
= event_notifier_init(notifier
, 1);
172 error_report("%s: unable to init event notifier: %d",
176 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
177 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
180 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
182 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
183 event_notifier_cleanup(notifier
);
188 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
192 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
193 proxy
->ioeventfd_disabled
||
194 proxy
->ioeventfd_started
) {
198 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
199 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
203 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
208 proxy
->ioeventfd_started
= true;
213 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
217 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
220 proxy
->ioeventfd_started
= false;
221 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
224 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
229 if (!proxy
->ioeventfd_started
) {
233 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
234 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
238 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
241 proxy
->ioeventfd_started
= false;
244 void virtio_pci_reset(DeviceState
*d
)
246 VirtIOPCIProxy
*proxy
= container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
247 virtio_pci_stop_ioeventfd(proxy
);
248 virtio_reset(proxy
->vdev
);
249 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
252 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
254 VirtIOPCIProxy
*proxy
= opaque
;
255 VirtIODevice
*vdev
= proxy
->vdev
;
256 target_phys_addr_t pa
;
259 case VIRTIO_PCI_GUEST_FEATURES
:
260 /* Guest does not negotiate properly? We have to assume nothing. */
261 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
262 val
= vdev
->bad_features
? vdev
->bad_features(vdev
) : 0;
264 virtio_set_features(vdev
, val
);
266 case VIRTIO_PCI_QUEUE_PFN
:
267 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
269 virtio_pci_stop_ioeventfd(proxy
);
270 virtio_reset(proxy
->vdev
);
271 msix_unuse_all_vectors(&proxy
->pci_dev
);
274 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
276 case VIRTIO_PCI_QUEUE_SEL
:
277 if (val
< VIRTIO_PCI_QUEUE_MAX
)
278 vdev
->queue_sel
= val
;
280 case VIRTIO_PCI_QUEUE_NOTIFY
:
281 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
282 virtio_queue_notify(vdev
, val
);
285 case VIRTIO_PCI_STATUS
:
286 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
287 virtio_pci_stop_ioeventfd(proxy
);
290 virtio_set_status(vdev
, val
& 0xFF);
292 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
293 virtio_pci_start_ioeventfd(proxy
);
296 if (vdev
->status
== 0) {
297 virtio_reset(proxy
->vdev
);
298 msix_unuse_all_vectors(&proxy
->pci_dev
);
301 /* Linux before 2.6.34 sets the device as OK without enabling
302 the PCI device bus master bit. In this case we need to disable
303 some safety checks. */
304 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
305 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
306 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
309 case VIRTIO_MSI_CONFIG_VECTOR
:
310 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
311 /* Make it possible for guest to discover an error took place. */
312 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
313 val
= VIRTIO_NO_VECTOR
;
314 vdev
->config_vector
= val
;
316 case VIRTIO_MSI_QUEUE_VECTOR
:
317 msix_vector_unuse(&proxy
->pci_dev
,
318 virtio_queue_vector(vdev
, vdev
->queue_sel
));
319 /* Make it possible for guest to discover an error took place. */
320 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
321 val
= VIRTIO_NO_VECTOR
;
322 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
325 error_report("%s: unexpected address 0x%x value 0x%x",
326 __func__
, addr
, val
);
331 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
333 VirtIODevice
*vdev
= proxy
->vdev
;
334 uint32_t ret
= 0xFFFFFFFF;
337 case VIRTIO_PCI_HOST_FEATURES
:
338 ret
= proxy
->host_features
;
340 case VIRTIO_PCI_GUEST_FEATURES
:
341 ret
= vdev
->guest_features
;
343 case VIRTIO_PCI_QUEUE_PFN
:
344 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
345 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
347 case VIRTIO_PCI_QUEUE_NUM
:
348 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
350 case VIRTIO_PCI_QUEUE_SEL
:
351 ret
= vdev
->queue_sel
;
353 case VIRTIO_PCI_STATUS
:
357 /* reading from the ISR also clears it. */
360 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
362 case VIRTIO_MSI_CONFIG_VECTOR
:
363 ret
= vdev
->config_vector
;
365 case VIRTIO_MSI_QUEUE_VECTOR
:
366 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
375 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
377 VirtIOPCIProxy
*proxy
= opaque
;
378 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
380 return virtio_ioport_read(proxy
, addr
);
382 return virtio_config_readb(proxy
->vdev
, addr
);
385 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
387 VirtIOPCIProxy
*proxy
= opaque
;
388 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
391 return virtio_ioport_read(proxy
, addr
);
393 val
= virtio_config_readw(proxy
->vdev
, addr
);
394 if (virtio_is_big_endian()) {
396 * virtio is odd, ioports are LE but config space is target native
397 * endian. However, in qemu, all PIO is LE, so we need to re-swap
405 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
407 VirtIOPCIProxy
*proxy
= opaque
;
408 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
411 return virtio_ioport_read(proxy
, addr
);
413 val
= virtio_config_readl(proxy
->vdev
, addr
);
414 if (virtio_is_big_endian()) {
420 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
422 VirtIOPCIProxy
*proxy
= opaque
;
423 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
425 virtio_ioport_write(proxy
, addr
, val
);
429 virtio_config_writeb(proxy
->vdev
, addr
, val
);
432 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
434 VirtIOPCIProxy
*proxy
= opaque
;
435 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
437 virtio_ioport_write(proxy
, addr
, val
);
441 if (virtio_is_big_endian()) {
444 virtio_config_writew(proxy
->vdev
, addr
, val
);
447 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
449 VirtIOPCIProxy
*proxy
= opaque
;
450 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
452 virtio_ioport_write(proxy
, addr
, val
);
456 if (virtio_is_big_endian()) {
459 virtio_config_writel(proxy
->vdev
, addr
, val
);
462 static const MemoryRegionPortio virtio_portio
[] = {
463 { 0, 0x10000, 1, .write
= virtio_pci_config_writeb
, },
464 { 0, 0x10000, 2, .write
= virtio_pci_config_writew
, },
465 { 0, 0x10000, 4, .write
= virtio_pci_config_writel
, },
466 { 0, 0x10000, 1, .read
= virtio_pci_config_readb
, },
467 { 0, 0x10000, 2, .read
= virtio_pci_config_readw
, },
468 { 0, 0x10000, 4, .read
= virtio_pci_config_readl
, },
472 static const MemoryRegionOps virtio_pci_config_ops
= {
473 .old_portio
= virtio_portio
,
474 .endianness
= DEVICE_LITTLE_ENDIAN
,
477 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
478 uint32_t val
, int len
)
480 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
482 pci_default_write_config(pci_dev
, address
, val
, len
);
484 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
485 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
486 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
487 virtio_pci_stop_ioeventfd(proxy
);
488 virtio_set_status(proxy
->vdev
,
489 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
493 static unsigned virtio_pci_get_features(void *opaque
)
495 VirtIOPCIProxy
*proxy
= opaque
;
496 return proxy
->host_features
;
499 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
500 unsigned int queue_no
,
504 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
505 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
506 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
509 if (irqfd
->users
== 0) {
510 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
518 ret
= kvm_irqchip_add_irq_notifier(kvm_state
, n
, irqfd
->virq
);
520 if (--irqfd
->users
== 0) {
521 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
526 virtio_queue_set_guest_notifier_fd_handler(vq
, true, true);
530 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
531 unsigned int queue_no
,
534 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
535 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
536 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
539 ret
= kvm_irqchip_remove_irq_notifier(kvm_state
, n
, irqfd
->virq
);
542 if (--irqfd
->users
== 0) {
543 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
546 virtio_queue_set_guest_notifier_fd_handler(vq
, true, false);
549 static int kvm_virtio_pci_vector_use(PCIDevice
*dev
, unsigned vector
,
552 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
553 VirtIODevice
*vdev
= proxy
->vdev
;
556 for (queue_no
= 0; queue_no
< VIRTIO_PCI_QUEUE_MAX
; queue_no
++) {
557 if (!virtio_queue_get_num(vdev
, queue_no
)) {
560 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
563 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
571 while (--queue_no
>= 0) {
572 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
575 kvm_virtio_pci_vq_vector_release(proxy
, queue_no
, vector
);
580 static void kvm_virtio_pci_vector_release(PCIDevice
*dev
, unsigned vector
)
582 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
583 VirtIODevice
*vdev
= proxy
->vdev
;
586 for (queue_no
= 0; queue_no
< VIRTIO_PCI_QUEUE_MAX
; queue_no
++) {
587 if (!virtio_queue_get_num(vdev
, queue_no
)) {
590 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
593 kvm_virtio_pci_vq_vector_release(proxy
, queue_no
, vector
);
597 static int virtio_pci_set_guest_notifier(void *opaque
, int n
, bool assign
)
599 VirtIOPCIProxy
*proxy
= opaque
;
600 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
601 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
604 int r
= event_notifier_init(notifier
, 0);
608 virtio_queue_set_guest_notifier_fd_handler(vq
, true, false);
610 virtio_queue_set_guest_notifier_fd_handler(vq
, false, false);
611 event_notifier_cleanup(notifier
);
617 static bool virtio_pci_query_guest_notifiers(void *opaque
)
619 VirtIOPCIProxy
*proxy
= opaque
;
620 return msix_enabled(&proxy
->pci_dev
);
623 static int virtio_pci_set_guest_notifiers(void *opaque
, bool assign
)
625 VirtIOPCIProxy
*proxy
= opaque
;
626 VirtIODevice
*vdev
= proxy
->vdev
;
629 /* Must unset vector notifier while guest notifier is still assigned */
630 if (kvm_msi_via_irqfd_enabled() && !assign
) {
631 msix_unset_vector_notifiers(&proxy
->pci_dev
);
632 g_free(proxy
->vector_irqfd
);
633 proxy
->vector_irqfd
= NULL
;
636 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
637 if (!virtio_queue_get_num(vdev
, n
)) {
641 r
= virtio_pci_set_guest_notifier(opaque
, n
, assign
);
647 /* Must set vector notifier after guest notifier has been assigned */
648 if (kvm_msi_via_irqfd_enabled() && assign
) {
649 proxy
->vector_irqfd
=
650 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
651 msix_nr_vectors_allocated(&proxy
->pci_dev
));
652 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
653 kvm_virtio_pci_vector_use
,
654 kvm_virtio_pci_vector_release
);
663 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
666 virtio_pci_set_guest_notifier(opaque
, n
, !assign
);
671 static int virtio_pci_set_host_notifier(void *opaque
, int n
, bool assign
)
673 VirtIOPCIProxy
*proxy
= opaque
;
675 /* Stop using ioeventfd for virtqueue kick if the device starts using host
676 * notifiers. This makes it easy to avoid stepping on each others' toes.
678 proxy
->ioeventfd_disabled
= assign
;
680 virtio_pci_stop_ioeventfd(proxy
);
682 /* We don't need to start here: it's not needed because backend
683 * currently only stops on status change away from ok,
684 * reset, vmstop and such. If we do add code to start here,
685 * need to check vmstate, device state etc. */
686 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
689 static void virtio_pci_vmstate_change(void *opaque
, bool running
)
691 VirtIOPCIProxy
*proxy
= opaque
;
694 /* Try to find out if the guest has bus master disabled, but is
695 in ready state. Then we have a buggy guest OS. */
696 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
697 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
698 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
700 virtio_pci_start_ioeventfd(proxy
);
702 virtio_pci_stop_ioeventfd(proxy
);
706 static const VirtIOBindings virtio_pci_bindings
= {
707 .notify
= virtio_pci_notify
,
708 .save_config
= virtio_pci_save_config
,
709 .load_config
= virtio_pci_load_config
,
710 .save_queue
= virtio_pci_save_queue
,
711 .load_queue
= virtio_pci_load_queue
,
712 .get_features
= virtio_pci_get_features
,
713 .query_guest_notifiers
= virtio_pci_query_guest_notifiers
,
714 .set_host_notifier
= virtio_pci_set_host_notifier
,
715 .set_guest_notifiers
= virtio_pci_set_guest_notifiers
,
716 .vmstate_change
= virtio_pci_vmstate_change
,
719 void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
)
726 config
= proxy
->pci_dev
.config
;
728 if (proxy
->class_code
) {
729 pci_config_set_class(config
, proxy
->class_code
);
731 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
732 pci_get_word(config
+ PCI_VENDOR_ID
));
733 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, vdev
->device_id
);
734 config
[PCI_INTERRUPT_PIN
] = 1;
736 if (vdev
->nvectors
&&
737 msix_init_exclusive_bar(&proxy
->pci_dev
, vdev
->nvectors
, 1)) {
741 proxy
->pci_dev
.config_write
= virtio_write_config
;
743 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
745 size
= 1 << qemu_fls(size
);
747 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
749 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
752 if (!kvm_has_many_ioeventfds()) {
753 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
756 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
757 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
758 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
759 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
762 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
764 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
767 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
768 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
769 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
771 vdev
= virtio_blk_init(&pci_dev
->qdev
, &proxy
->blk
);
775 vdev
->nvectors
= proxy
->nvectors
;
776 virtio_init_pci(proxy
, vdev
);
777 /* make the actual value visible */
778 proxy
->nvectors
= vdev
->nvectors
;
782 static void virtio_exit_pci(PCIDevice
*pci_dev
)
784 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
786 memory_region_destroy(&proxy
->bar
);
787 msix_uninit_exclusive_bar(pci_dev
);
790 static void virtio_blk_exit_pci(PCIDevice
*pci_dev
)
792 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
794 virtio_pci_stop_ioeventfd(proxy
);
795 virtio_blk_exit(proxy
->vdev
);
796 virtio_exit_pci(pci_dev
);
799 static int virtio_serial_init_pci(PCIDevice
*pci_dev
)
801 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
804 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
805 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
806 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
807 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
809 vdev
= virtio_serial_init(&pci_dev
->qdev
, &proxy
->serial
);
813 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
814 ? proxy
->serial
.max_virtserial_ports
+ 1
816 virtio_init_pci(proxy
, vdev
);
817 proxy
->nvectors
= vdev
->nvectors
;
821 static void virtio_serial_exit_pci(PCIDevice
*pci_dev
)
823 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
825 virtio_pci_stop_ioeventfd(proxy
);
826 virtio_serial_exit(proxy
->vdev
);
827 virtio_exit_pci(pci_dev
);
830 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
832 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
835 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
, &proxy
->net
);
837 vdev
->nvectors
= proxy
->nvectors
;
838 virtio_init_pci(proxy
, vdev
);
840 /* make the actual value visible */
841 proxy
->nvectors
= vdev
->nvectors
;
845 static void virtio_net_exit_pci(PCIDevice
*pci_dev
)
847 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
849 virtio_pci_stop_ioeventfd(proxy
);
850 virtio_net_exit(proxy
->vdev
);
851 virtio_exit_pci(pci_dev
);
854 static int virtio_balloon_init_pci(PCIDevice
*pci_dev
)
856 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
859 if (proxy
->class_code
!= PCI_CLASS_OTHERS
&&
860 proxy
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
861 proxy
->class_code
= PCI_CLASS_OTHERS
;
864 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
868 virtio_init_pci(proxy
, vdev
);
872 static void virtio_balloon_exit_pci(PCIDevice
*pci_dev
)
874 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
876 virtio_pci_stop_ioeventfd(proxy
);
877 virtio_balloon_exit(proxy
->vdev
);
878 virtio_exit_pci(pci_dev
);
881 static Property virtio_blk_properties
[] = {
882 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
883 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy
, blk
.conf
),
884 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOPCIProxy
, blk
.conf
),
885 DEFINE_PROP_STRING("serial", VirtIOPCIProxy
, blk
.serial
),
887 DEFINE_PROP_BIT("scsi", VirtIOPCIProxy
, blk
.scsi
, 0, true),
889 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
890 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
891 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
892 DEFINE_PROP_END_OF_LIST(),
895 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
897 DeviceClass
*dc
= DEVICE_CLASS(klass
);
898 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
900 k
->init
= virtio_blk_init_pci
;
901 k
->exit
= virtio_blk_exit_pci
;
902 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
903 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
904 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
905 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
906 dc
->reset
= virtio_pci_reset
;
907 dc
->props
= virtio_blk_properties
;
910 static TypeInfo virtio_blk_info
= {
911 .name
= "virtio-blk-pci",
912 .parent
= TYPE_PCI_DEVICE
,
913 .instance_size
= sizeof(VirtIOPCIProxy
),
914 .class_init
= virtio_blk_class_init
,
917 static Property virtio_net_properties
[] = {
918 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
919 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
920 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
921 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
922 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy
, net
.txtimer
, TX_TIMER_INTERVAL
),
923 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy
, net
.txburst
, TX_BURST
),
924 DEFINE_PROP_STRING("tx", VirtIOPCIProxy
, net
.tx
),
925 DEFINE_PROP_END_OF_LIST(),
928 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
930 DeviceClass
*dc
= DEVICE_CLASS(klass
);
931 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
933 k
->init
= virtio_net_init_pci
;
934 k
->exit
= virtio_net_exit_pci
;
935 k
->romfile
= "pxe-virtio.rom";
936 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
937 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
938 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
939 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
940 dc
->reset
= virtio_pci_reset
;
941 dc
->props
= virtio_net_properties
;
944 static TypeInfo virtio_net_info
= {
945 .name
= "virtio-net-pci",
946 .parent
= TYPE_PCI_DEVICE
,
947 .instance_size
= sizeof(VirtIOPCIProxy
),
948 .class_init
= virtio_net_class_init
,
951 static Property virtio_serial_properties
[] = {
952 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
953 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, DEV_NVECTORS_UNSPECIFIED
),
954 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
955 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
956 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
, serial
.max_virtserial_ports
, 31),
957 DEFINE_PROP_END_OF_LIST(),
960 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
962 DeviceClass
*dc
= DEVICE_CLASS(klass
);
963 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
965 k
->init
= virtio_serial_init_pci
;
966 k
->exit
= virtio_serial_exit_pci
;
967 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
968 k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
969 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
970 k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
971 dc
->reset
= virtio_pci_reset
;
972 dc
->props
= virtio_serial_properties
;
975 static TypeInfo virtio_serial_info
= {
976 .name
= "virtio-serial-pci",
977 .parent
= TYPE_PCI_DEVICE
,
978 .instance_size
= sizeof(VirtIOPCIProxy
),
979 .class_init
= virtio_serial_class_init
,
982 static Property virtio_balloon_properties
[] = {
983 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
984 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
985 DEFINE_PROP_END_OF_LIST(),
988 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
990 DeviceClass
*dc
= DEVICE_CLASS(klass
);
991 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
993 k
->init
= virtio_balloon_init_pci
;
994 k
->exit
= virtio_balloon_exit_pci
;
995 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
996 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
997 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
998 k
->class_id
= PCI_CLASS_OTHERS
;
999 dc
->reset
= virtio_pci_reset
;
1000 dc
->props
= virtio_balloon_properties
;
1003 static TypeInfo virtio_balloon_info
= {
1004 .name
= "virtio-balloon-pci",
1005 .parent
= TYPE_PCI_DEVICE
,
1006 .instance_size
= sizeof(VirtIOPCIProxy
),
1007 .class_init
= virtio_balloon_class_init
,
1010 static int virtio_scsi_init_pci(PCIDevice
*pci_dev
)
1012 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
1015 vdev
= virtio_scsi_init(&pci_dev
->qdev
, &proxy
->scsi
);
1020 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
1021 ? proxy
->scsi
.num_queues
+ 3
1023 virtio_init_pci(proxy
, vdev
);
1025 /* make the actual value visible */
1026 proxy
->nvectors
= vdev
->nvectors
;
1030 static void virtio_scsi_exit_pci(PCIDevice
*pci_dev
)
1032 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
1034 virtio_scsi_exit(proxy
->vdev
);
1035 virtio_exit_pci(pci_dev
);
1038 static Property virtio_scsi_properties
[] = {
1039 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1040 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, DEV_NVECTORS_UNSPECIFIED
),
1041 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy
, host_features
, scsi
),
1042 DEFINE_PROP_END_OF_LIST(),
1045 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
1047 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1048 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1050 k
->init
= virtio_scsi_init_pci
;
1051 k
->exit
= virtio_scsi_exit_pci
;
1052 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1053 k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1055 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1056 dc
->reset
= virtio_pci_reset
;
1057 dc
->props
= virtio_scsi_properties
;
1060 static TypeInfo virtio_scsi_info
= {
1061 .name
= "virtio-scsi-pci",
1062 .parent
= TYPE_PCI_DEVICE
,
1063 .instance_size
= sizeof(VirtIOPCIProxy
),
1064 .class_init
= virtio_scsi_class_init
,
1067 static void virtio_pci_register_types(void)
1069 type_register_static(&virtio_blk_info
);
1070 type_register_static(&virtio_net_info
);
1071 type_register_static(&virtio_serial_info
);
1072 type_register_static(&virtio_balloon_info
);
1073 type_register_static(&virtio_scsi_info
);
1076 type_init(virtio_pci_register_types
)