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"
22 #include "qemu-error.h"
29 /* from Linux's linux/virtio_pci.h */
31 /* A 32-bit r/o bitmask of the features supported by the host */
32 #define VIRTIO_PCI_HOST_FEATURES 0
34 /* A 32-bit r/w bitmask of features activated by the guest */
35 #define VIRTIO_PCI_GUEST_FEATURES 4
37 /* A 32-bit r/w PFN for the currently selected queue */
38 #define VIRTIO_PCI_QUEUE_PFN 8
40 /* A 16-bit r/o queue size for the currently selected queue */
41 #define VIRTIO_PCI_QUEUE_NUM 12
43 /* A 16-bit r/w queue selector */
44 #define VIRTIO_PCI_QUEUE_SEL 14
46 /* A 16-bit r/w queue notifier */
47 #define VIRTIO_PCI_QUEUE_NOTIFY 16
49 /* An 8-bit device status register. */
50 #define VIRTIO_PCI_STATUS 18
52 /* An 8-bit r/o interrupt status register. Reading the value will return the
53 * current contents of the ISR and will also clear it. This is effectively
54 * a read-and-acknowledge. */
55 #define VIRTIO_PCI_ISR 19
57 /* MSI-X registers: only enabled if MSI-X is enabled. */
58 /* A 16-bit vector for configuration changes. */
59 #define VIRTIO_MSI_CONFIG_VECTOR 20
60 /* A 16-bit vector for selected queue notifications. */
61 #define VIRTIO_MSI_QUEUE_VECTOR 22
63 /* Config space size */
64 #define VIRTIO_PCI_CONFIG_NOMSI 20
65 #define VIRTIO_PCI_CONFIG_MSI 24
66 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
67 VIRTIO_PCI_CONFIG_MSI : \
68 VIRTIO_PCI_CONFIG_NOMSI)
70 /* The remaining space is defined by each driver as the per-driver
71 * configuration space */
72 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
73 VIRTIO_PCI_CONFIG_MSI : \
74 VIRTIO_PCI_CONFIG_NOMSI)
76 /* Virtio ABI version, if we increment this, we break the guest driver. */
77 #define VIRTIO_PCI_ABI_VERSION 0
79 /* How many bits to shift physical queue address written to QUEUE_PFN.
80 * 12 is historical, and due to x86 page size. */
81 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
83 /* Flags track per-device state like workarounds for quirks in older guests. */
84 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
86 /* Performance improves when virtqueue kick processing is decoupled from the
87 * vcpu thread using ioeventfd for some devices. */
88 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
89 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
91 /* QEMU doesn't strictly need write barriers since everything runs in
92 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
93 * KVM or if kqemu gets SMP support.
95 #define wmb() do { } while (0)
108 uint32_t host_features
;
112 /* Max. number of ports we can have for a the virtio-serial device */
113 uint32_t max_virtserial_ports
;
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
);
164 /* Try to find out if the guest has bus master disabled, but is
165 in ready state. Then we have a buggy guest OS. */
166 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
167 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
168 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
173 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
175 VirtIOPCIProxy
*proxy
= opaque
;
177 if (msix_present(&proxy
->pci_dev
)) {
178 qemu_get_be16s(f
, &vector
);
180 vector
= VIRTIO_NO_VECTOR
;
182 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
183 if (vector
!= VIRTIO_NO_VECTOR
) {
184 return msix_vector_use(&proxy
->pci_dev
, vector
);
189 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
192 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
193 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
196 r
= event_notifier_init(notifier
, 1);
198 error_report("%s: unable to init event notifier: %d",
202 r
= kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier
),
203 proxy
->addr
+ VIRTIO_PCI_QUEUE_NOTIFY
,
206 error_report("%s: unable to map ioeventfd: %d",
208 event_notifier_cleanup(notifier
);
211 r
= kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier
),
212 proxy
->addr
+ VIRTIO_PCI_QUEUE_NOTIFY
,
215 error_report("%s: unable to unmap ioeventfd: %d",
220 /* Handle the race condition where the guest kicked and we deassigned
221 * before we got around to handling the kick.
223 if (event_notifier_test_and_clear(notifier
)) {
224 virtio_queue_notify_vq(vq
);
227 event_notifier_cleanup(notifier
);
232 static void virtio_pci_host_notifier_read(void *opaque
)
234 VirtQueue
*vq
= opaque
;
235 EventNotifier
*n
= virtio_queue_get_host_notifier(vq
);
236 if (event_notifier_test_and_clear(n
)) {
237 virtio_queue_notify_vq(vq
);
241 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy
*proxy
,
244 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
245 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
247 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
248 virtio_pci_host_notifier_read
, NULL
, vq
);
250 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
255 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
259 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
260 proxy
->ioeventfd_disabled
||
261 proxy
->ioeventfd_started
) {
265 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
266 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
270 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true);
275 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, true);
277 proxy
->ioeventfd_started
= true;
282 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
286 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, false);
287 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false);
290 proxy
->ioeventfd_started
= false;
291 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
294 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
299 if (!proxy
->ioeventfd_started
) {
303 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
304 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
308 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, false);
309 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false);
312 proxy
->ioeventfd_started
= false;
315 static void virtio_pci_reset(DeviceState
*d
)
317 VirtIOPCIProxy
*proxy
= container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
318 virtio_pci_stop_ioeventfd(proxy
);
319 virtio_reset(proxy
->vdev
);
320 msix_reset(&proxy
->pci_dev
);
321 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
324 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
326 VirtIOPCIProxy
*proxy
= opaque
;
327 VirtIODevice
*vdev
= proxy
->vdev
;
328 target_phys_addr_t pa
;
331 case VIRTIO_PCI_GUEST_FEATURES
:
332 /* Guest does not negotiate properly? We have to assume nothing. */
333 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
334 if (vdev
->bad_features
)
335 val
= proxy
->host_features
& vdev
->bad_features(vdev
);
339 if (vdev
->set_features
)
340 vdev
->set_features(vdev
, val
);
341 vdev
->guest_features
= val
;
343 case VIRTIO_PCI_QUEUE_PFN
:
344 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
346 virtio_pci_stop_ioeventfd(proxy
);
347 virtio_reset(proxy
->vdev
);
348 msix_unuse_all_vectors(&proxy
->pci_dev
);
351 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
353 case VIRTIO_PCI_QUEUE_SEL
:
354 if (val
< VIRTIO_PCI_QUEUE_MAX
)
355 vdev
->queue_sel
= val
;
357 case VIRTIO_PCI_QUEUE_NOTIFY
:
358 virtio_queue_notify(vdev
, val
);
360 case VIRTIO_PCI_STATUS
:
361 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
362 virtio_pci_stop_ioeventfd(proxy
);
365 virtio_set_status(vdev
, val
& 0xFF);
367 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
368 virtio_pci_start_ioeventfd(proxy
);
371 if (vdev
->status
== 0) {
372 virtio_reset(proxy
->vdev
);
373 msix_unuse_all_vectors(&proxy
->pci_dev
);
376 /* Linux before 2.6.34 sets the device as OK without enabling
377 the PCI device bus master bit. In this case we need to disable
378 some safety checks. */
379 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
380 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
381 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
384 case VIRTIO_MSI_CONFIG_VECTOR
:
385 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
386 /* Make it possible for guest to discover an error took place. */
387 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
388 val
= VIRTIO_NO_VECTOR
;
389 vdev
->config_vector
= val
;
391 case VIRTIO_MSI_QUEUE_VECTOR
:
392 msix_vector_unuse(&proxy
->pci_dev
,
393 virtio_queue_vector(vdev
, vdev
->queue_sel
));
394 /* Make it possible for guest to discover an error took place. */
395 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
396 val
= VIRTIO_NO_VECTOR
;
397 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
400 error_report("%s: unexpected address 0x%x value 0x%x",
401 __func__
, addr
, val
);
406 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
408 VirtIODevice
*vdev
= proxy
->vdev
;
409 uint32_t ret
= 0xFFFFFFFF;
412 case VIRTIO_PCI_HOST_FEATURES
:
413 ret
= proxy
->host_features
;
415 case VIRTIO_PCI_GUEST_FEATURES
:
416 ret
= vdev
->guest_features
;
418 case VIRTIO_PCI_QUEUE_PFN
:
419 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
420 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
422 case VIRTIO_PCI_QUEUE_NUM
:
423 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
425 case VIRTIO_PCI_QUEUE_SEL
:
426 ret
= vdev
->queue_sel
;
428 case VIRTIO_PCI_STATUS
:
432 /* reading from the ISR also clears it. */
435 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
437 case VIRTIO_MSI_CONFIG_VECTOR
:
438 ret
= vdev
->config_vector
;
440 case VIRTIO_MSI_QUEUE_VECTOR
:
441 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
450 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
452 VirtIOPCIProxy
*proxy
= opaque
;
453 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
456 return virtio_ioport_read(proxy
, addr
);
458 return virtio_config_readb(proxy
->vdev
, addr
);
461 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
463 VirtIOPCIProxy
*proxy
= opaque
;
464 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
467 return virtio_ioport_read(proxy
, addr
);
469 return virtio_config_readw(proxy
->vdev
, addr
);
472 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
474 VirtIOPCIProxy
*proxy
= opaque
;
475 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
478 return virtio_ioport_read(proxy
, addr
);
480 return virtio_config_readl(proxy
->vdev
, addr
);
483 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
485 VirtIOPCIProxy
*proxy
= opaque
;
486 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
489 virtio_ioport_write(proxy
, addr
, val
);
493 virtio_config_writeb(proxy
->vdev
, addr
, val
);
496 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
498 VirtIOPCIProxy
*proxy
= opaque
;
499 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
502 virtio_ioport_write(proxy
, addr
, val
);
506 virtio_config_writew(proxy
->vdev
, addr
, val
);
509 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
511 VirtIOPCIProxy
*proxy
= opaque
;
512 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
515 virtio_ioport_write(proxy
, addr
, val
);
519 virtio_config_writel(proxy
->vdev
, addr
, val
);
522 static void virtio_map(PCIDevice
*pci_dev
, int region_num
,
523 pcibus_t addr
, pcibus_t size
, int type
)
525 VirtIOPCIProxy
*proxy
= container_of(pci_dev
, VirtIOPCIProxy
, pci_dev
);
526 VirtIODevice
*vdev
= proxy
->vdev
;
527 unsigned config_len
= VIRTIO_PCI_REGION_SIZE(pci_dev
) + vdev
->config_len
;
531 register_ioport_write(addr
, config_len
, 1, virtio_pci_config_writeb
, proxy
);
532 register_ioport_write(addr
, config_len
, 2, virtio_pci_config_writew
, proxy
);
533 register_ioport_write(addr
, config_len
, 4, virtio_pci_config_writel
, proxy
);
534 register_ioport_read(addr
, config_len
, 1, virtio_pci_config_readb
, proxy
);
535 register_ioport_read(addr
, config_len
, 2, virtio_pci_config_readw
, proxy
);
536 register_ioport_read(addr
, config_len
, 4, virtio_pci_config_readl
, proxy
);
538 if (vdev
->config_len
)
539 vdev
->get_config(vdev
, vdev
->config
);
542 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
543 uint32_t val
, int len
)
545 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
547 if (PCI_COMMAND
== address
) {
548 if (!(val
& PCI_COMMAND_MASTER
)) {
549 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
550 virtio_pci_stop_ioeventfd(proxy
);
551 virtio_set_status(proxy
->vdev
,
552 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
557 pci_default_write_config(pci_dev
, address
, val
, len
);
558 msix_write_config(pci_dev
, address
, val
, len
);
561 static unsigned virtio_pci_get_features(void *opaque
)
563 VirtIOPCIProxy
*proxy
= opaque
;
564 return proxy
->host_features
;
567 static void virtio_pci_guest_notifier_read(void *opaque
)
569 VirtQueue
*vq
= opaque
;
570 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
571 if (event_notifier_test_and_clear(n
)) {
576 static int virtio_pci_set_guest_notifier(void *opaque
, int n
, bool assign
)
578 VirtIOPCIProxy
*proxy
= opaque
;
579 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
580 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
583 int r
= event_notifier_init(notifier
, 0);
587 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
588 virtio_pci_guest_notifier_read
, NULL
, vq
);
590 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
592 event_notifier_cleanup(notifier
);
598 static bool virtio_pci_query_guest_notifiers(void *opaque
)
600 VirtIOPCIProxy
*proxy
= opaque
;
601 return msix_enabled(&proxy
->pci_dev
);
604 static int virtio_pci_set_guest_notifiers(void *opaque
, bool assign
)
606 VirtIOPCIProxy
*proxy
= opaque
;
607 VirtIODevice
*vdev
= proxy
->vdev
;
610 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
611 if (!virtio_queue_get_num(vdev
, n
)) {
615 r
= virtio_pci_set_guest_notifier(opaque
, n
, assign
);
624 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
626 virtio_pci_set_guest_notifier(opaque
, n
, !assign
);
631 static int virtio_pci_set_host_notifier(void *opaque
, int n
, bool assign
)
633 VirtIOPCIProxy
*proxy
= opaque
;
635 /* Stop using ioeventfd for virtqueue kick if the device starts using host
636 * notifiers. This makes it easy to avoid stepping on each others' toes.
638 proxy
->ioeventfd_disabled
= assign
;
640 virtio_pci_stop_ioeventfd(proxy
);
642 /* We don't need to start here: it's not needed because backend
643 * currently only stops on status change away from ok,
644 * reset, vmstop and such. If we do add code to start here,
645 * need to check vmstate, device state etc. */
646 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
);
649 static void virtio_pci_vmstate_change(void *opaque
, bool running
)
651 VirtIOPCIProxy
*proxy
= opaque
;
654 virtio_pci_start_ioeventfd(proxy
);
656 virtio_pci_stop_ioeventfd(proxy
);
660 static const VirtIOBindings virtio_pci_bindings
= {
661 .notify
= virtio_pci_notify
,
662 .save_config
= virtio_pci_save_config
,
663 .load_config
= virtio_pci_load_config
,
664 .save_queue
= virtio_pci_save_queue
,
665 .load_queue
= virtio_pci_load_queue
,
666 .get_features
= virtio_pci_get_features
,
667 .query_guest_notifiers
= virtio_pci_query_guest_notifiers
,
668 .set_host_notifier
= virtio_pci_set_host_notifier
,
669 .set_guest_notifiers
= virtio_pci_set_guest_notifiers
,
670 .vmstate_change
= virtio_pci_vmstate_change
,
673 static void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
,
674 uint16_t vendor
, uint16_t device
,
675 uint16_t class_code
, uint8_t pif
)
682 config
= proxy
->pci_dev
.config
;
683 pci_config_set_vendor_id(config
, vendor
);
684 pci_config_set_device_id(config
, device
);
686 config
[0x08] = VIRTIO_PCI_ABI_VERSION
;
689 pci_config_set_class(config
, class_code
);
691 config
[0x2c] = vendor
& 0xFF;
692 config
[0x2d] = (vendor
>> 8) & 0xFF;
693 config
[0x2e] = vdev
->device_id
& 0xFF;
694 config
[0x2f] = (vdev
->device_id
>> 8) & 0xFF;
698 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
, 1, 0)) {
699 pci_register_bar(&proxy
->pci_dev
, 1,
700 msix_bar_size(&proxy
->pci_dev
),
701 PCI_BASE_ADDRESS_SPACE_MEMORY
,
706 proxy
->pci_dev
.config_write
= virtio_write_config
;
708 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
710 size
= 1 << qemu_fls(size
);
712 pci_register_bar(&proxy
->pci_dev
, 0, size
, PCI_BASE_ADDRESS_SPACE_IO
,
715 if (!kvm_has_many_ioeventfds()) {
716 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
719 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
720 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
721 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
722 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
725 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
727 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
730 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
731 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
732 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
734 vdev
= virtio_blk_init(&pci_dev
->qdev
, &proxy
->block
);
738 vdev
->nvectors
= proxy
->nvectors
;
739 virtio_init_pci(proxy
, vdev
,
740 PCI_VENDOR_ID_REDHAT_QUMRANET
,
741 PCI_DEVICE_ID_VIRTIO_BLOCK
,
742 proxy
->class_code
, 0x00);
743 /* make the actual value visible */
744 proxy
->nvectors
= vdev
->nvectors
;
748 static int virtio_exit_pci(PCIDevice
*pci_dev
)
750 return msix_uninit(pci_dev
);
753 static int virtio_blk_exit_pci(PCIDevice
*pci_dev
)
755 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
757 virtio_pci_stop_ioeventfd(proxy
);
758 virtio_blk_exit(proxy
->vdev
);
759 blockdev_mark_auto_del(proxy
->block
.bs
);
760 return virtio_exit_pci(pci_dev
);
763 static int virtio_serial_init_pci(PCIDevice
*pci_dev
)
765 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
768 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
769 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
770 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
771 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
773 vdev
= virtio_serial_init(&pci_dev
->qdev
, proxy
->max_virtserial_ports
);
777 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
778 ? proxy
->max_virtserial_ports
+ 1
780 virtio_init_pci(proxy
, vdev
,
781 PCI_VENDOR_ID_REDHAT_QUMRANET
,
782 PCI_DEVICE_ID_VIRTIO_CONSOLE
,
783 proxy
->class_code
, 0x00);
784 proxy
->nvectors
= vdev
->nvectors
;
788 static int virtio_serial_exit_pci(PCIDevice
*pci_dev
)
790 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
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_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
902 DEV_NVECTORS_UNSPECIFIED
),
903 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
904 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
905 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
, max_virtserial_ports
,
907 DEFINE_PROP_END_OF_LIST(),
909 .qdev
.reset
= virtio_pci_reset
,
911 .qdev
.name
= "virtio-balloon-pci",
912 .qdev
.size
= sizeof(VirtIOPCIProxy
),
913 .init
= virtio_balloon_init_pci
,
914 .exit
= virtio_exit_pci
,
915 .qdev
.props
= (Property
[]) {
916 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
917 DEFINE_PROP_END_OF_LIST(),
919 .qdev
.reset
= virtio_pci_reset
,
922 .qdev
.name
= "virtio-9p-pci",
923 .qdev
.size
= sizeof(VirtIOPCIProxy
),
924 .init
= virtio_9p_init_pci
,
925 .qdev
.props
= (Property
[]) {
926 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
927 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
928 DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy
, fsconf
.tag
),
929 DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy
, fsconf
.fsdev_id
),
930 DEFINE_PROP_END_OF_LIST(),
938 static void virtio_pci_register_devices(void)
940 pci_qdev_register_many(virtio_info
);
943 device_init(virtio_pci_register_devices
)