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_unuse_all_vectors(&proxy
->pci_dev
);
135 msix_load(&proxy
->pci_dev
, f
);
136 if (msix_present(&proxy
->pci_dev
)) {
137 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
139 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
141 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
142 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
147 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
149 VirtIOPCIProxy
*proxy
= opaque
;
151 if (msix_present(&proxy
->pci_dev
)) {
152 qemu_get_be16s(f
, &vector
);
154 vector
= VIRTIO_NO_VECTOR
;
156 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
157 if (vector
!= VIRTIO_NO_VECTOR
) {
158 return msix_vector_use(&proxy
->pci_dev
, vector
);
163 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
164 int n
, bool assign
, bool set_handler
)
166 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
167 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
171 r
= event_notifier_init(notifier
, 1);
173 error_report("%s: unable to init event notifier: %d",
177 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
178 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
181 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
183 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
184 event_notifier_cleanup(notifier
);
189 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
193 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
194 proxy
->ioeventfd_disabled
||
195 proxy
->ioeventfd_started
) {
199 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
200 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
204 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
209 proxy
->ioeventfd_started
= true;
214 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
218 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
221 proxy
->ioeventfd_started
= false;
222 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
225 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
230 if (!proxy
->ioeventfd_started
) {
234 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
235 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
239 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
242 proxy
->ioeventfd_started
= false;
245 void virtio_pci_reset(DeviceState
*d
)
247 VirtIOPCIProxy
*proxy
= container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
248 virtio_pci_stop_ioeventfd(proxy
);
249 virtio_reset(proxy
->vdev
);
250 msix_unuse_all_vectors(&proxy
->pci_dev
);
251 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
254 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
256 VirtIOPCIProxy
*proxy
= opaque
;
257 VirtIODevice
*vdev
= proxy
->vdev
;
258 target_phys_addr_t pa
;
261 case VIRTIO_PCI_GUEST_FEATURES
:
262 /* Guest does not negotiate properly? We have to assume nothing. */
263 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
264 val
= vdev
->bad_features
? vdev
->bad_features(vdev
) : 0;
266 virtio_set_features(vdev
, val
);
268 case VIRTIO_PCI_QUEUE_PFN
:
269 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
271 virtio_pci_stop_ioeventfd(proxy
);
272 virtio_reset(proxy
->vdev
);
273 msix_unuse_all_vectors(&proxy
->pci_dev
);
276 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
278 case VIRTIO_PCI_QUEUE_SEL
:
279 if (val
< VIRTIO_PCI_QUEUE_MAX
)
280 vdev
->queue_sel
= val
;
282 case VIRTIO_PCI_QUEUE_NOTIFY
:
283 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
284 virtio_queue_notify(vdev
, val
);
287 case VIRTIO_PCI_STATUS
:
288 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
289 virtio_pci_stop_ioeventfd(proxy
);
292 virtio_set_status(vdev
, val
& 0xFF);
294 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
295 virtio_pci_start_ioeventfd(proxy
);
298 if (vdev
->status
== 0) {
299 virtio_reset(proxy
->vdev
);
300 msix_unuse_all_vectors(&proxy
->pci_dev
);
303 /* Linux before 2.6.34 sets the device as OK without enabling
304 the PCI device bus master bit. In this case we need to disable
305 some safety checks. */
306 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
307 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
308 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
311 case VIRTIO_MSI_CONFIG_VECTOR
:
312 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
313 /* Make it possible for guest to discover an error took place. */
314 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
315 val
= VIRTIO_NO_VECTOR
;
316 vdev
->config_vector
= val
;
318 case VIRTIO_MSI_QUEUE_VECTOR
:
319 msix_vector_unuse(&proxy
->pci_dev
,
320 virtio_queue_vector(vdev
, vdev
->queue_sel
));
321 /* Make it possible for guest to discover an error took place. */
322 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
323 val
= VIRTIO_NO_VECTOR
;
324 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
327 error_report("%s: unexpected address 0x%x value 0x%x",
328 __func__
, addr
, val
);
333 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
335 VirtIODevice
*vdev
= proxy
->vdev
;
336 uint32_t ret
= 0xFFFFFFFF;
339 case VIRTIO_PCI_HOST_FEATURES
:
340 ret
= proxy
->host_features
;
342 case VIRTIO_PCI_GUEST_FEATURES
:
343 ret
= vdev
->guest_features
;
345 case VIRTIO_PCI_QUEUE_PFN
:
346 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
347 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
349 case VIRTIO_PCI_QUEUE_NUM
:
350 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
352 case VIRTIO_PCI_QUEUE_SEL
:
353 ret
= vdev
->queue_sel
;
355 case VIRTIO_PCI_STATUS
:
359 /* reading from the ISR also clears it. */
362 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
364 case VIRTIO_MSI_CONFIG_VECTOR
:
365 ret
= vdev
->config_vector
;
367 case VIRTIO_MSI_QUEUE_VECTOR
:
368 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
377 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
379 VirtIOPCIProxy
*proxy
= opaque
;
380 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
382 return virtio_ioport_read(proxy
, addr
);
384 return virtio_config_readb(proxy
->vdev
, addr
);
387 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
389 VirtIOPCIProxy
*proxy
= opaque
;
390 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
393 return virtio_ioport_read(proxy
, addr
);
395 val
= virtio_config_readw(proxy
->vdev
, addr
);
396 if (virtio_is_big_endian()) {
398 * virtio is odd, ioports are LE but config space is target native
399 * endian. However, in qemu, all PIO is LE, so we need to re-swap
407 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
409 VirtIOPCIProxy
*proxy
= opaque
;
410 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
413 return virtio_ioport_read(proxy
, addr
);
415 val
= virtio_config_readl(proxy
->vdev
, addr
);
416 if (virtio_is_big_endian()) {
422 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
424 VirtIOPCIProxy
*proxy
= opaque
;
425 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
427 virtio_ioport_write(proxy
, addr
, val
);
431 virtio_config_writeb(proxy
->vdev
, addr
, val
);
434 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
436 VirtIOPCIProxy
*proxy
= opaque
;
437 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
439 virtio_ioport_write(proxy
, addr
, val
);
443 if (virtio_is_big_endian()) {
446 virtio_config_writew(proxy
->vdev
, addr
, val
);
449 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
451 VirtIOPCIProxy
*proxy
= opaque
;
452 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
454 virtio_ioport_write(proxy
, addr
, val
);
458 if (virtio_is_big_endian()) {
461 virtio_config_writel(proxy
->vdev
, addr
, val
);
464 static const MemoryRegionPortio virtio_portio
[] = {
465 { 0, 0x10000, 1, .write
= virtio_pci_config_writeb
, },
466 { 0, 0x10000, 2, .write
= virtio_pci_config_writew
, },
467 { 0, 0x10000, 4, .write
= virtio_pci_config_writel
, },
468 { 0, 0x10000, 1, .read
= virtio_pci_config_readb
, },
469 { 0, 0x10000, 2, .read
= virtio_pci_config_readw
, },
470 { 0, 0x10000, 4, .read
= virtio_pci_config_readl
, },
474 static const MemoryRegionOps virtio_pci_config_ops
= {
475 .old_portio
= virtio_portio
,
476 .endianness
= DEVICE_LITTLE_ENDIAN
,
479 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
480 uint32_t val
, int len
)
482 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
484 pci_default_write_config(pci_dev
, address
, val
, len
);
486 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
487 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
488 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
489 virtio_pci_stop_ioeventfd(proxy
);
490 virtio_set_status(proxy
->vdev
,
491 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
495 static unsigned virtio_pci_get_features(void *opaque
)
497 VirtIOPCIProxy
*proxy
= opaque
;
498 return proxy
->host_features
;
501 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
502 unsigned int queue_no
,
506 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
507 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
508 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
511 if (irqfd
->users
== 0) {
512 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
520 ret
= kvm_irqchip_add_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
522 if (--irqfd
->users
== 0) {
523 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
528 virtio_queue_set_guest_notifier_fd_handler(vq
, true, true);
532 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
533 unsigned int queue_no
,
536 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
537 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
538 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
541 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
544 if (--irqfd
->users
== 0) {
545 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
548 virtio_queue_set_guest_notifier_fd_handler(vq
, true, false);
551 static int kvm_virtio_pci_vector_use(PCIDevice
*dev
, unsigned vector
,
554 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
555 VirtIODevice
*vdev
= proxy
->vdev
;
558 for (queue_no
= 0; queue_no
< VIRTIO_PCI_QUEUE_MAX
; queue_no
++) {
559 if (!virtio_queue_get_num(vdev
, queue_no
)) {
562 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
565 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
573 while (--queue_no
>= 0) {
574 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
577 kvm_virtio_pci_vq_vector_release(proxy
, queue_no
, vector
);
582 static void kvm_virtio_pci_vector_release(PCIDevice
*dev
, unsigned vector
)
584 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
585 VirtIODevice
*vdev
= proxy
->vdev
;
588 for (queue_no
= 0; queue_no
< VIRTIO_PCI_QUEUE_MAX
; queue_no
++) {
589 if (!virtio_queue_get_num(vdev
, queue_no
)) {
592 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
595 kvm_virtio_pci_vq_vector_release(proxy
, queue_no
, vector
);
599 static int virtio_pci_set_guest_notifier(void *opaque
, int n
, bool assign
)
601 VirtIOPCIProxy
*proxy
= opaque
;
602 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
603 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
606 int r
= event_notifier_init(notifier
, 0);
610 virtio_queue_set_guest_notifier_fd_handler(vq
, true, false);
612 virtio_queue_set_guest_notifier_fd_handler(vq
, false, false);
613 event_notifier_cleanup(notifier
);
619 static bool virtio_pci_query_guest_notifiers(void *opaque
)
621 VirtIOPCIProxy
*proxy
= opaque
;
622 return msix_enabled(&proxy
->pci_dev
);
625 static int virtio_pci_set_guest_notifiers(void *opaque
, bool assign
)
627 VirtIOPCIProxy
*proxy
= opaque
;
628 VirtIODevice
*vdev
= proxy
->vdev
;
631 /* Must unset vector notifier while guest notifier is still assigned */
632 if (kvm_msi_via_irqfd_enabled() && !assign
) {
633 msix_unset_vector_notifiers(&proxy
->pci_dev
);
634 g_free(proxy
->vector_irqfd
);
635 proxy
->vector_irqfd
= NULL
;
638 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
639 if (!virtio_queue_get_num(vdev
, n
)) {
643 r
= virtio_pci_set_guest_notifier(opaque
, n
, assign
);
649 /* Must set vector notifier after guest notifier has been assigned */
650 if (kvm_msi_via_irqfd_enabled() && assign
) {
651 proxy
->vector_irqfd
=
652 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
653 msix_nr_vectors_allocated(&proxy
->pci_dev
));
654 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
655 kvm_virtio_pci_vector_use
,
656 kvm_virtio_pci_vector_release
);
665 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
668 virtio_pci_set_guest_notifier(opaque
, n
, !assign
);
673 static int virtio_pci_set_host_notifier(void *opaque
, int n
, bool assign
)
675 VirtIOPCIProxy
*proxy
= opaque
;
677 /* Stop using ioeventfd for virtqueue kick if the device starts using host
678 * notifiers. This makes it easy to avoid stepping on each others' toes.
680 proxy
->ioeventfd_disabled
= assign
;
682 virtio_pci_stop_ioeventfd(proxy
);
684 /* We don't need to start here: it's not needed because backend
685 * currently only stops on status change away from ok,
686 * reset, vmstop and such. If we do add code to start here,
687 * need to check vmstate, device state etc. */
688 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
691 static void virtio_pci_vmstate_change(void *opaque
, bool running
)
693 VirtIOPCIProxy
*proxy
= opaque
;
696 /* Try to find out if the guest has bus master disabled, but is
697 in ready state. Then we have a buggy guest OS. */
698 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
699 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
700 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
702 virtio_pci_start_ioeventfd(proxy
);
704 virtio_pci_stop_ioeventfd(proxy
);
708 static const VirtIOBindings virtio_pci_bindings
= {
709 .notify
= virtio_pci_notify
,
710 .save_config
= virtio_pci_save_config
,
711 .load_config
= virtio_pci_load_config
,
712 .save_queue
= virtio_pci_save_queue
,
713 .load_queue
= virtio_pci_load_queue
,
714 .get_features
= virtio_pci_get_features
,
715 .query_guest_notifiers
= virtio_pci_query_guest_notifiers
,
716 .set_host_notifier
= virtio_pci_set_host_notifier
,
717 .set_guest_notifiers
= virtio_pci_set_guest_notifiers
,
718 .vmstate_change
= virtio_pci_vmstate_change
,
721 void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
)
728 config
= proxy
->pci_dev
.config
;
730 if (proxy
->class_code
) {
731 pci_config_set_class(config
, proxy
->class_code
);
733 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
734 pci_get_word(config
+ PCI_VENDOR_ID
));
735 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, vdev
->device_id
);
736 config
[PCI_INTERRUPT_PIN
] = 1;
738 if (vdev
->nvectors
&&
739 msix_init_exclusive_bar(&proxy
->pci_dev
, vdev
->nvectors
, 1)) {
743 proxy
->pci_dev
.config_write
= virtio_write_config
;
745 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
747 size
= 1 << qemu_fls(size
);
749 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
751 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
754 if (!kvm_has_many_ioeventfds()) {
755 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
758 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
759 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
760 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
761 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
764 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
766 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
769 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
770 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
771 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
773 vdev
= virtio_blk_init(&pci_dev
->qdev
, &proxy
->blk
);
777 vdev
->nvectors
= proxy
->nvectors
;
778 virtio_init_pci(proxy
, vdev
);
779 /* make the actual value visible */
780 proxy
->nvectors
= vdev
->nvectors
;
784 static void virtio_exit_pci(PCIDevice
*pci_dev
)
786 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
788 memory_region_destroy(&proxy
->bar
);
789 msix_uninit_exclusive_bar(pci_dev
);
792 static void virtio_blk_exit_pci(PCIDevice
*pci_dev
)
794 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
796 virtio_pci_stop_ioeventfd(proxy
);
797 virtio_blk_exit(proxy
->vdev
);
798 virtio_exit_pci(pci_dev
);
801 static int virtio_serial_init_pci(PCIDevice
*pci_dev
)
803 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
806 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
807 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
808 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
809 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
811 vdev
= virtio_serial_init(&pci_dev
->qdev
, &proxy
->serial
);
815 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
816 ? proxy
->serial
.max_virtserial_ports
+ 1
818 virtio_init_pci(proxy
, vdev
);
819 proxy
->nvectors
= vdev
->nvectors
;
823 static void virtio_serial_exit_pci(PCIDevice
*pci_dev
)
825 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
827 virtio_pci_stop_ioeventfd(proxy
);
828 virtio_serial_exit(proxy
->vdev
);
829 virtio_exit_pci(pci_dev
);
832 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
834 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
837 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
, &proxy
->net
);
839 vdev
->nvectors
= proxy
->nvectors
;
840 virtio_init_pci(proxy
, vdev
);
842 /* make the actual value visible */
843 proxy
->nvectors
= vdev
->nvectors
;
847 static void virtio_net_exit_pci(PCIDevice
*pci_dev
)
849 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
851 virtio_pci_stop_ioeventfd(proxy
);
852 virtio_net_exit(proxy
->vdev
);
853 virtio_exit_pci(pci_dev
);
856 static int virtio_balloon_init_pci(PCIDevice
*pci_dev
)
858 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
861 if (proxy
->class_code
!= PCI_CLASS_OTHERS
&&
862 proxy
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
863 proxy
->class_code
= PCI_CLASS_OTHERS
;
866 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
870 virtio_init_pci(proxy
, vdev
);
874 static void virtio_balloon_exit_pci(PCIDevice
*pci_dev
)
876 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
878 virtio_pci_stop_ioeventfd(proxy
);
879 virtio_balloon_exit(proxy
->vdev
);
880 virtio_exit_pci(pci_dev
);
883 static Property virtio_blk_properties
[] = {
884 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
885 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy
, blk
.conf
),
886 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOPCIProxy
, blk
.conf
),
887 DEFINE_PROP_STRING("serial", VirtIOPCIProxy
, blk
.serial
),
889 DEFINE_PROP_BIT("scsi", VirtIOPCIProxy
, blk
.scsi
, 0, true),
891 DEFINE_PROP_BIT("config-wce", VirtIOPCIProxy
, blk
.config_wce
, 0, true),
892 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
893 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
894 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
895 DEFINE_PROP_END_OF_LIST(),
898 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
900 DeviceClass
*dc
= DEVICE_CLASS(klass
);
901 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
903 k
->init
= virtio_blk_init_pci
;
904 k
->exit
= virtio_blk_exit_pci
;
905 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
906 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
907 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
908 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
909 dc
->reset
= virtio_pci_reset
;
910 dc
->props
= virtio_blk_properties
;
913 static TypeInfo virtio_blk_info
= {
914 .name
= "virtio-blk-pci",
915 .parent
= TYPE_PCI_DEVICE
,
916 .instance_size
= sizeof(VirtIOPCIProxy
),
917 .class_init
= virtio_blk_class_init
,
920 static Property virtio_net_properties
[] = {
921 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
922 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
923 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
924 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
925 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy
, net
.txtimer
, TX_TIMER_INTERVAL
),
926 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy
, net
.txburst
, TX_BURST
),
927 DEFINE_PROP_STRING("tx", VirtIOPCIProxy
, net
.tx
),
928 DEFINE_PROP_END_OF_LIST(),
931 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
933 DeviceClass
*dc
= DEVICE_CLASS(klass
);
934 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
936 k
->init
= virtio_net_init_pci
;
937 k
->exit
= virtio_net_exit_pci
;
938 k
->romfile
= "pxe-virtio.rom";
939 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
940 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
941 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
942 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
943 dc
->reset
= virtio_pci_reset
;
944 dc
->props
= virtio_net_properties
;
947 static TypeInfo virtio_net_info
= {
948 .name
= "virtio-net-pci",
949 .parent
= TYPE_PCI_DEVICE
,
950 .instance_size
= sizeof(VirtIOPCIProxy
),
951 .class_init
= virtio_net_class_init
,
954 static Property virtio_serial_properties
[] = {
955 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
956 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, DEV_NVECTORS_UNSPECIFIED
),
957 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
958 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
959 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
, serial
.max_virtserial_ports
, 31),
960 DEFINE_PROP_END_OF_LIST(),
963 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
965 DeviceClass
*dc
= DEVICE_CLASS(klass
);
966 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
968 k
->init
= virtio_serial_init_pci
;
969 k
->exit
= virtio_serial_exit_pci
;
970 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
971 k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
972 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
973 k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
974 dc
->reset
= virtio_pci_reset
;
975 dc
->props
= virtio_serial_properties
;
978 static TypeInfo virtio_serial_info
= {
979 .name
= "virtio-serial-pci",
980 .parent
= TYPE_PCI_DEVICE
,
981 .instance_size
= sizeof(VirtIOPCIProxy
),
982 .class_init
= virtio_serial_class_init
,
985 static Property virtio_balloon_properties
[] = {
986 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
987 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
988 DEFINE_PROP_END_OF_LIST(),
991 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
993 DeviceClass
*dc
= DEVICE_CLASS(klass
);
994 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
996 k
->init
= virtio_balloon_init_pci
;
997 k
->exit
= virtio_balloon_exit_pci
;
998 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
999 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1000 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1001 k
->class_id
= PCI_CLASS_OTHERS
;
1002 dc
->reset
= virtio_pci_reset
;
1003 dc
->props
= virtio_balloon_properties
;
1006 static TypeInfo virtio_balloon_info
= {
1007 .name
= "virtio-balloon-pci",
1008 .parent
= TYPE_PCI_DEVICE
,
1009 .instance_size
= sizeof(VirtIOPCIProxy
),
1010 .class_init
= virtio_balloon_class_init
,
1013 static int virtio_scsi_init_pci(PCIDevice
*pci_dev
)
1015 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
1018 vdev
= virtio_scsi_init(&pci_dev
->qdev
, &proxy
->scsi
);
1023 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
1024 ? proxy
->scsi
.num_queues
+ 3
1026 virtio_init_pci(proxy
, vdev
);
1028 /* make the actual value visible */
1029 proxy
->nvectors
= vdev
->nvectors
;
1033 static void virtio_scsi_exit_pci(PCIDevice
*pci_dev
)
1035 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
1037 virtio_scsi_exit(proxy
->vdev
);
1038 virtio_exit_pci(pci_dev
);
1041 static Property virtio_scsi_properties
[] = {
1042 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1043 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, DEV_NVECTORS_UNSPECIFIED
),
1044 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy
, host_features
, scsi
),
1045 DEFINE_PROP_END_OF_LIST(),
1048 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
1050 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1051 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1053 k
->init
= virtio_scsi_init_pci
;
1054 k
->exit
= virtio_scsi_exit_pci
;
1055 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1056 k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1058 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1059 dc
->reset
= virtio_pci_reset
;
1060 dc
->props
= virtio_scsi_properties
;
1063 static TypeInfo virtio_scsi_info
= {
1064 .name
= "virtio-scsi-pci",
1065 .parent
= TYPE_PCI_DEVICE
,
1066 .instance_size
= sizeof(VirtIOPCIProxy
),
1067 .class_init
= virtio_scsi_class_init
,
1070 static void virtio_pci_register_types(void)
1072 type_register_static(&virtio_blk_info
);
1073 type_register_static(&virtio_net_info
);
1074 type_register_static(&virtio_serial_info
);
1075 type_register_static(&virtio_balloon_info
);
1076 type_register_static(&virtio_scsi_info
);
1079 type_init(virtio_pci_register_types
)