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.
19 #include "virtio-blk.h"
20 #include "virtio-net.h"
21 #include "virtio-serial.h"
23 #include "qemu-error.h"
30 /* from Linux's linux/virtio_pci.h */
32 /* A 32-bit r/o bitmask of the features supported by the host */
33 #define VIRTIO_PCI_HOST_FEATURES 0
35 /* A 32-bit r/w bitmask of features activated by the guest */
36 #define VIRTIO_PCI_GUEST_FEATURES 4
38 /* A 32-bit r/w PFN for the currently selected queue */
39 #define VIRTIO_PCI_QUEUE_PFN 8
41 /* A 16-bit r/o queue size for the currently selected queue */
42 #define VIRTIO_PCI_QUEUE_NUM 12
44 /* A 16-bit r/w queue selector */
45 #define VIRTIO_PCI_QUEUE_SEL 14
47 /* A 16-bit r/w queue notifier */
48 #define VIRTIO_PCI_QUEUE_NOTIFY 16
50 /* An 8-bit device status register. */
51 #define VIRTIO_PCI_STATUS 18
53 /* An 8-bit r/o interrupt status register. Reading the value will return the
54 * current contents of the ISR and will also clear it. This is effectively
55 * a read-and-acknowledge. */
56 #define VIRTIO_PCI_ISR 19
58 /* MSI-X registers: only enabled if MSI-X is enabled. */
59 /* A 16-bit vector for configuration changes. */
60 #define VIRTIO_MSI_CONFIG_VECTOR 20
61 /* A 16-bit vector for selected queue notifications. */
62 #define VIRTIO_MSI_QUEUE_VECTOR 22
64 /* Config space size */
65 #define VIRTIO_PCI_CONFIG_NOMSI 20
66 #define VIRTIO_PCI_CONFIG_MSI 24
67 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
68 VIRTIO_PCI_CONFIG_MSI : \
69 VIRTIO_PCI_CONFIG_NOMSI)
71 /* The remaining space is defined by each driver as the per-driver
72 * configuration space */
73 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
74 VIRTIO_PCI_CONFIG_MSI : \
75 VIRTIO_PCI_CONFIG_NOMSI)
77 /* Virtio ABI version, if we increment this, we break the guest driver. */
78 #define VIRTIO_PCI_ABI_VERSION 0
80 /* How many bits to shift physical queue address written to QUEUE_PFN.
81 * 12 is historical, and due to x86 page size. */
82 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
84 /* Flags track per-device state like workarounds for quirks in older guests. */
85 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
87 /* Performance improves when virtqueue kick processing is decoupled from the
88 * vcpu thread using ioeventfd for some devices. */
89 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
90 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
92 /* QEMU doesn't strictly need write barriers since everything runs in
93 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
94 * KVM or if kqemu gets SMP support.
96 #define wmb() do { } while (0)
109 uint32_t host_features
;
113 virtio_serial_conf serial
;
115 bool ioeventfd_disabled
;
116 bool ioeventfd_started
;
121 static void virtio_pci_notify(void *opaque
, uint16_t vector
)
123 VirtIOPCIProxy
*proxy
= opaque
;
124 if (msix_enabled(&proxy
->pci_dev
))
125 msix_notify(&proxy
->pci_dev
, vector
);
127 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
130 static void virtio_pci_save_config(void * opaque
, QEMUFile
*f
)
132 VirtIOPCIProxy
*proxy
= opaque
;
133 pci_device_save(&proxy
->pci_dev
, f
);
134 msix_save(&proxy
->pci_dev
, f
);
135 if (msix_present(&proxy
->pci_dev
))
136 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
139 static void virtio_pci_save_queue(void * opaque
, int n
, QEMUFile
*f
)
141 VirtIOPCIProxy
*proxy
= opaque
;
142 if (msix_present(&proxy
->pci_dev
))
143 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
146 static int virtio_pci_load_config(void * opaque
, QEMUFile
*f
)
148 VirtIOPCIProxy
*proxy
= opaque
;
150 ret
= pci_device_load(&proxy
->pci_dev
, f
);
154 msix_load(&proxy
->pci_dev
, f
);
155 if (msix_present(&proxy
->pci_dev
)) {
156 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
158 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
160 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
161 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
166 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
168 VirtIOPCIProxy
*proxy
= opaque
;
170 if (msix_present(&proxy
->pci_dev
)) {
171 qemu_get_be16s(f
, &vector
);
173 vector
= VIRTIO_NO_VECTOR
;
175 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
176 if (vector
!= VIRTIO_NO_VECTOR
) {
177 return msix_vector_use(&proxy
->pci_dev
, vector
);
182 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
185 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
186 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
189 r
= event_notifier_init(notifier
, 1);
191 error_report("%s: unable to init event notifier: %d",
195 r
= kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier
),
196 proxy
->addr
+ VIRTIO_PCI_QUEUE_NOTIFY
,
199 error_report("%s: unable to map ioeventfd: %d",
201 event_notifier_cleanup(notifier
);
204 r
= kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier
),
205 proxy
->addr
+ VIRTIO_PCI_QUEUE_NOTIFY
,
208 error_report("%s: unable to unmap ioeventfd: %d",
213 /* Handle the race condition where the guest kicked and we deassigned
214 * before we got around to handling the kick.
216 if (event_notifier_test_and_clear(notifier
)) {
217 virtio_queue_notify_vq(vq
);
220 event_notifier_cleanup(notifier
);
225 static void virtio_pci_host_notifier_read(void *opaque
)
227 VirtQueue
*vq
= opaque
;
228 EventNotifier
*n
= virtio_queue_get_host_notifier(vq
);
229 if (event_notifier_test_and_clear(n
)) {
230 virtio_queue_notify_vq(vq
);
234 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy
*proxy
,
237 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
238 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
240 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
241 virtio_pci_host_notifier_read
, NULL
, vq
);
243 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
248 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
252 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
253 proxy
->ioeventfd_disabled
||
254 proxy
->ioeventfd_started
) {
258 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
259 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
263 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true);
268 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, true);
270 proxy
->ioeventfd_started
= true;
275 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
279 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, false);
280 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false);
283 proxy
->ioeventfd_started
= false;
284 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
287 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
292 if (!proxy
->ioeventfd_started
) {
296 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
297 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
301 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, false);
302 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false);
305 proxy
->ioeventfd_started
= false;
308 static void virtio_pci_reset(DeviceState
*d
)
310 VirtIOPCIProxy
*proxy
= container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
311 virtio_pci_stop_ioeventfd(proxy
);
312 virtio_reset(proxy
->vdev
);
313 msix_reset(&proxy
->pci_dev
);
314 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
317 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
319 VirtIOPCIProxy
*proxy
= opaque
;
320 VirtIODevice
*vdev
= proxy
->vdev
;
321 target_phys_addr_t pa
;
324 case VIRTIO_PCI_GUEST_FEATURES
:
325 /* Guest does not negotiate properly? We have to assume nothing. */
326 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
327 if (vdev
->bad_features
)
328 val
= proxy
->host_features
& vdev
->bad_features(vdev
);
332 if (vdev
->set_features
)
333 vdev
->set_features(vdev
, val
);
334 vdev
->guest_features
= val
;
336 case VIRTIO_PCI_QUEUE_PFN
:
337 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
339 virtio_pci_stop_ioeventfd(proxy
);
340 virtio_reset(proxy
->vdev
);
341 msix_unuse_all_vectors(&proxy
->pci_dev
);
344 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
346 case VIRTIO_PCI_QUEUE_SEL
:
347 if (val
< VIRTIO_PCI_QUEUE_MAX
)
348 vdev
->queue_sel
= val
;
350 case VIRTIO_PCI_QUEUE_NOTIFY
:
351 virtio_queue_notify(vdev
, val
);
353 case VIRTIO_PCI_STATUS
:
354 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
355 virtio_pci_stop_ioeventfd(proxy
);
358 virtio_set_status(vdev
, val
& 0xFF);
360 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
361 virtio_pci_start_ioeventfd(proxy
);
364 if (vdev
->status
== 0) {
365 virtio_reset(proxy
->vdev
);
366 msix_unuse_all_vectors(&proxy
->pci_dev
);
369 /* Linux before 2.6.34 sets the device as OK without enabling
370 the PCI device bus master bit. In this case we need to disable
371 some safety checks. */
372 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
373 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
374 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
377 case VIRTIO_MSI_CONFIG_VECTOR
:
378 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
379 /* Make it possible for guest to discover an error took place. */
380 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
381 val
= VIRTIO_NO_VECTOR
;
382 vdev
->config_vector
= val
;
384 case VIRTIO_MSI_QUEUE_VECTOR
:
385 msix_vector_unuse(&proxy
->pci_dev
,
386 virtio_queue_vector(vdev
, vdev
->queue_sel
));
387 /* Make it possible for guest to discover an error took place. */
388 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
389 val
= VIRTIO_NO_VECTOR
;
390 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
393 error_report("%s: unexpected address 0x%x value 0x%x",
394 __func__
, addr
, val
);
399 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
401 VirtIODevice
*vdev
= proxy
->vdev
;
402 uint32_t ret
= 0xFFFFFFFF;
405 case VIRTIO_PCI_HOST_FEATURES
:
406 ret
= proxy
->host_features
;
408 case VIRTIO_PCI_GUEST_FEATURES
:
409 ret
= vdev
->guest_features
;
411 case VIRTIO_PCI_QUEUE_PFN
:
412 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
413 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
415 case VIRTIO_PCI_QUEUE_NUM
:
416 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
418 case VIRTIO_PCI_QUEUE_SEL
:
419 ret
= vdev
->queue_sel
;
421 case VIRTIO_PCI_STATUS
:
425 /* reading from the ISR also clears it. */
428 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
430 case VIRTIO_MSI_CONFIG_VECTOR
:
431 ret
= vdev
->config_vector
;
433 case VIRTIO_MSI_QUEUE_VECTOR
:
434 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
443 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
445 VirtIOPCIProxy
*proxy
= opaque
;
446 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
449 return virtio_ioport_read(proxy
, addr
);
451 return virtio_config_readb(proxy
->vdev
, addr
);
454 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
456 VirtIOPCIProxy
*proxy
= opaque
;
457 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
460 return virtio_ioport_read(proxy
, addr
);
462 return virtio_config_readw(proxy
->vdev
, addr
);
465 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
467 VirtIOPCIProxy
*proxy
= opaque
;
468 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
471 return virtio_ioport_read(proxy
, addr
);
473 return virtio_config_readl(proxy
->vdev
, addr
);
476 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
478 VirtIOPCIProxy
*proxy
= opaque
;
479 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
482 virtio_ioport_write(proxy
, addr
, val
);
486 virtio_config_writeb(proxy
->vdev
, addr
, val
);
489 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
491 VirtIOPCIProxy
*proxy
= opaque
;
492 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
495 virtio_ioport_write(proxy
, addr
, val
);
499 virtio_config_writew(proxy
->vdev
, addr
, val
);
502 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
504 VirtIOPCIProxy
*proxy
= opaque
;
505 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
508 virtio_ioport_write(proxy
, addr
, val
);
512 virtio_config_writel(proxy
->vdev
, addr
, val
);
515 static void virtio_map(PCIDevice
*pci_dev
, int region_num
,
516 pcibus_t addr
, pcibus_t size
, int type
)
518 VirtIOPCIProxy
*proxy
= container_of(pci_dev
, VirtIOPCIProxy
, pci_dev
);
519 VirtIODevice
*vdev
= proxy
->vdev
;
520 unsigned config_len
= VIRTIO_PCI_REGION_SIZE(pci_dev
) + vdev
->config_len
;
524 register_ioport_write(addr
, config_len
, 1, virtio_pci_config_writeb
, proxy
);
525 register_ioport_write(addr
, config_len
, 2, virtio_pci_config_writew
, proxy
);
526 register_ioport_write(addr
, config_len
, 4, virtio_pci_config_writel
, proxy
);
527 register_ioport_read(addr
, config_len
, 1, virtio_pci_config_readb
, proxy
);
528 register_ioport_read(addr
, config_len
, 2, virtio_pci_config_readw
, proxy
);
529 register_ioport_read(addr
, config_len
, 4, virtio_pci_config_readl
, proxy
);
531 if (vdev
->config_len
)
532 vdev
->get_config(vdev
, vdev
->config
);
535 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
536 uint32_t val
, int len
)
538 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
540 if (PCI_COMMAND
== address
) {
541 if (!(val
& PCI_COMMAND_MASTER
)) {
542 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
543 virtio_pci_stop_ioeventfd(proxy
);
544 virtio_set_status(proxy
->vdev
,
545 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
550 pci_default_write_config(pci_dev
, address
, val
, len
);
551 msix_write_config(pci_dev
, address
, val
, len
);
554 static unsigned virtio_pci_get_features(void *opaque
)
556 VirtIOPCIProxy
*proxy
= opaque
;
557 return proxy
->host_features
;
560 static void virtio_pci_guest_notifier_read(void *opaque
)
562 VirtQueue
*vq
= opaque
;
563 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
564 if (event_notifier_test_and_clear(n
)) {
569 static int virtio_pci_set_guest_notifier(void *opaque
, int n
, bool assign
)
571 VirtIOPCIProxy
*proxy
= opaque
;
572 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
573 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
576 int r
= event_notifier_init(notifier
, 0);
580 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
581 virtio_pci_guest_notifier_read
, NULL
, vq
);
583 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
585 event_notifier_cleanup(notifier
);
591 static bool virtio_pci_query_guest_notifiers(void *opaque
)
593 VirtIOPCIProxy
*proxy
= opaque
;
594 return msix_enabled(&proxy
->pci_dev
);
597 static int virtio_pci_set_guest_notifiers(void *opaque
, bool assign
)
599 VirtIOPCIProxy
*proxy
= opaque
;
600 VirtIODevice
*vdev
= proxy
->vdev
;
603 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
604 if (!virtio_queue_get_num(vdev
, n
)) {
608 r
= virtio_pci_set_guest_notifier(opaque
, n
, assign
);
617 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
619 virtio_pci_set_guest_notifier(opaque
, n
, !assign
);
624 static int virtio_pci_set_host_notifier(void *opaque
, int n
, bool assign
)
626 VirtIOPCIProxy
*proxy
= opaque
;
628 /* Stop using ioeventfd for virtqueue kick if the device starts using host
629 * notifiers. This makes it easy to avoid stepping on each others' toes.
631 proxy
->ioeventfd_disabled
= assign
;
633 virtio_pci_stop_ioeventfd(proxy
);
635 /* We don't need to start here: it's not needed because backend
636 * currently only stops on status change away from ok,
637 * reset, vmstop and such. If we do add code to start here,
638 * need to check vmstate, device state etc. */
639 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
);
642 static void virtio_pci_vmstate_change(void *opaque
, bool running
)
644 VirtIOPCIProxy
*proxy
= opaque
;
647 /* Try to find out if the guest has bus master disabled, but is
648 in ready state. Then we have a buggy guest OS. */
649 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
650 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
651 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
653 virtio_pci_start_ioeventfd(proxy
);
655 virtio_pci_stop_ioeventfd(proxy
);
659 static const VirtIOBindings virtio_pci_bindings
= {
660 .notify
= virtio_pci_notify
,
661 .save_config
= virtio_pci_save_config
,
662 .load_config
= virtio_pci_load_config
,
663 .save_queue
= virtio_pci_save_queue
,
664 .load_queue
= virtio_pci_load_queue
,
665 .get_features
= virtio_pci_get_features
,
666 .query_guest_notifiers
= virtio_pci_query_guest_notifiers
,
667 .set_host_notifier
= virtio_pci_set_host_notifier
,
668 .set_guest_notifiers
= virtio_pci_set_guest_notifiers
,
669 .vmstate_change
= virtio_pci_vmstate_change
,
672 static void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
,
673 uint16_t vendor
, uint16_t device
,
674 uint16_t class_code
, uint8_t pif
)
681 config
= proxy
->pci_dev
.config
;
682 pci_config_set_vendor_id(config
, vendor
);
683 pci_config_set_device_id(config
, device
);
685 config
[0x08] = VIRTIO_PCI_ABI_VERSION
;
688 pci_config_set_class(config
, class_code
);
690 config
[0x2c] = vendor
& 0xFF;
691 config
[0x2d] = (vendor
>> 8) & 0xFF;
692 config
[0x2e] = vdev
->device_id
& 0xFF;
693 config
[0x2f] = (vdev
->device_id
>> 8) & 0xFF;
697 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
, 1, 0)) {
698 pci_register_bar(&proxy
->pci_dev
, 1,
699 msix_bar_size(&proxy
->pci_dev
),
700 PCI_BASE_ADDRESS_SPACE_MEMORY
,
705 proxy
->pci_dev
.config_write
= virtio_write_config
;
707 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
709 size
= 1 << qemu_fls(size
);
711 pci_register_bar(&proxy
->pci_dev
, 0, size
, PCI_BASE_ADDRESS_SPACE_IO
,
714 if (!kvm_has_many_ioeventfds()) {
715 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
718 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
719 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
720 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
721 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
724 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
726 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
729 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
730 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
731 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
733 vdev
= virtio_blk_init(&pci_dev
->qdev
, &proxy
->block
);
737 vdev
->nvectors
= proxy
->nvectors
;
738 virtio_init_pci(proxy
, vdev
,
739 PCI_VENDOR_ID_REDHAT_QUMRANET
,
740 PCI_DEVICE_ID_VIRTIO_BLOCK
,
741 proxy
->class_code
, 0x00);
742 /* make the actual value visible */
743 proxy
->nvectors
= vdev
->nvectors
;
747 static int virtio_exit_pci(PCIDevice
*pci_dev
)
749 return msix_uninit(pci_dev
);
752 static int virtio_blk_exit_pci(PCIDevice
*pci_dev
)
754 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
756 virtio_pci_stop_ioeventfd(proxy
);
757 virtio_blk_exit(proxy
->vdev
);
758 blockdev_mark_auto_del(proxy
->block
.bs
);
759 return virtio_exit_pci(pci_dev
);
762 static int virtio_serial_init_pci(PCIDevice
*pci_dev
)
764 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
767 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
768 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
769 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
770 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
772 vdev
= virtio_serial_init(&pci_dev
->qdev
, &proxy
->serial
);
776 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
777 ? proxy
->serial
.max_virtserial_ports
+ 1
779 virtio_init_pci(proxy
, vdev
,
780 PCI_VENDOR_ID_REDHAT_QUMRANET
,
781 PCI_DEVICE_ID_VIRTIO_CONSOLE
,
782 proxy
->class_code
, 0x00);
783 proxy
->nvectors
= vdev
->nvectors
;
787 static int virtio_serial_exit_pci(PCIDevice
*pci_dev
)
789 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
791 virtio_pci_stop_ioeventfd(proxy
);
792 virtio_serial_exit(proxy
->vdev
);
793 return virtio_exit_pci(pci_dev
);
796 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
798 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
801 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
, &proxy
->net
);
803 vdev
->nvectors
= proxy
->nvectors
;
804 virtio_init_pci(proxy
, vdev
,
805 PCI_VENDOR_ID_REDHAT_QUMRANET
,
806 PCI_DEVICE_ID_VIRTIO_NET
,
807 PCI_CLASS_NETWORK_ETHERNET
,
810 /* make the actual value visible */
811 proxy
->nvectors
= vdev
->nvectors
;
815 static int virtio_net_exit_pci(PCIDevice
*pci_dev
)
817 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
819 virtio_pci_stop_ioeventfd(proxy
);
820 virtio_net_exit(proxy
->vdev
);
821 return virtio_exit_pci(pci_dev
);
824 static int virtio_balloon_init_pci(PCIDevice
*pci_dev
)
826 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
829 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
830 virtio_init_pci(proxy
, vdev
,
831 PCI_VENDOR_ID_REDHAT_QUMRANET
,
832 PCI_DEVICE_ID_VIRTIO_BALLOON
,
833 PCI_CLASS_MEMORY_RAM
,
839 static int virtio_9p_init_pci(PCIDevice
*pci_dev
)
841 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
844 vdev
= virtio_9p_init(&pci_dev
->qdev
, &proxy
->fsconf
);
845 vdev
->nvectors
= proxy
->nvectors
;
846 virtio_init_pci(proxy
, vdev
,
847 PCI_VENDOR_ID_REDHAT_QUMRANET
,
851 /* make the actual value visible */
852 proxy
->nvectors
= vdev
->nvectors
;
857 static PCIDeviceInfo virtio_info
[] = {
859 .qdev
.name
= "virtio-blk-pci",
860 .qdev
.alias
= "virtio-blk",
861 .qdev
.size
= sizeof(VirtIOPCIProxy
),
862 .init
= virtio_blk_init_pci
,
863 .exit
= virtio_blk_exit_pci
,
864 .qdev
.props
= (Property
[]) {
865 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
866 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy
, block
),
867 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
868 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
869 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
870 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
871 DEFINE_PROP_END_OF_LIST(),
873 .qdev
.reset
= virtio_pci_reset
,
875 .qdev
.name
= "virtio-net-pci",
876 .qdev
.size
= sizeof(VirtIOPCIProxy
),
877 .init
= virtio_net_init_pci
,
878 .exit
= virtio_net_exit_pci
,
879 .romfile
= "pxe-virtio.bin",
880 .qdev
.props
= (Property
[]) {
881 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
882 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
883 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
884 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
885 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
886 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy
,
887 net
.txtimer
, TX_TIMER_INTERVAL
),
888 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy
,
889 net
.txburst
, TX_BURST
),
890 DEFINE_PROP_STRING("tx", VirtIOPCIProxy
, net
.tx
),
891 DEFINE_PROP_END_OF_LIST(),
893 .qdev
.reset
= virtio_pci_reset
,
895 .qdev
.name
= "virtio-serial-pci",
896 .qdev
.alias
= "virtio-serial",
897 .qdev
.size
= sizeof(VirtIOPCIProxy
),
898 .init
= virtio_serial_init_pci
,
899 .exit
= virtio_serial_exit_pci
,
900 .qdev
.props
= (Property
[]) {
901 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
902 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
903 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
904 DEV_NVECTORS_UNSPECIFIED
),
905 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
906 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
907 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
,
908 serial
.max_virtserial_ports
, 31),
909 DEFINE_PROP_END_OF_LIST(),
911 .qdev
.reset
= virtio_pci_reset
,
913 .qdev
.name
= "virtio-balloon-pci",
914 .qdev
.size
= sizeof(VirtIOPCIProxy
),
915 .init
= virtio_balloon_init_pci
,
916 .exit
= virtio_exit_pci
,
917 .qdev
.props
= (Property
[]) {
918 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
919 DEFINE_PROP_END_OF_LIST(),
921 .qdev
.reset
= virtio_pci_reset
,
924 .qdev
.name
= "virtio-9p-pci",
925 .qdev
.size
= sizeof(VirtIOPCIProxy
),
926 .init
= virtio_9p_init_pci
,
927 .qdev
.props
= (Property
[]) {
928 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
929 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
930 DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy
, fsconf
.tag
),
931 DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy
, fsconf
.fsdev_id
),
932 DEFINE_PROP_END_OF_LIST(),
940 static void virtio_pci_register_devices(void)
942 pci_qdev_register_many(virtio_info
);
945 device_init(virtio_pci_register_devices
)