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"
25 #include "qemu-error.h"
31 #include "virtio-pci.h"
34 /* from Linux's linux/virtio_pci.h */
36 /* A 32-bit r/o bitmask of the features supported by the host */
37 #define VIRTIO_PCI_HOST_FEATURES 0
39 /* A 32-bit r/w bitmask of features activated by the guest */
40 #define VIRTIO_PCI_GUEST_FEATURES 4
42 /* A 32-bit r/w PFN for the currently selected queue */
43 #define VIRTIO_PCI_QUEUE_PFN 8
45 /* A 16-bit r/o queue size for the currently selected queue */
46 #define VIRTIO_PCI_QUEUE_NUM 12
48 /* A 16-bit r/w queue selector */
49 #define VIRTIO_PCI_QUEUE_SEL 14
51 /* A 16-bit r/w queue notifier */
52 #define VIRTIO_PCI_QUEUE_NOTIFY 16
54 /* An 8-bit device status register. */
55 #define VIRTIO_PCI_STATUS 18
57 /* An 8-bit r/o interrupt status register. Reading the value will return the
58 * current contents of the ISR and will also clear it. This is effectively
59 * a read-and-acknowledge. */
60 #define VIRTIO_PCI_ISR 19
62 /* MSI-X registers: only enabled if MSI-X is enabled. */
63 /* A 16-bit vector for configuration changes. */
64 #define VIRTIO_MSI_CONFIG_VECTOR 20
65 /* A 16-bit vector for selected queue notifications. */
66 #define VIRTIO_MSI_QUEUE_VECTOR 22
68 /* Config space size */
69 #define VIRTIO_PCI_CONFIG_NOMSI 20
70 #define VIRTIO_PCI_CONFIG_MSI 24
71 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
72 VIRTIO_PCI_CONFIG_MSI : \
73 VIRTIO_PCI_CONFIG_NOMSI)
75 /* The remaining space is defined by each driver as the per-driver
76 * configuration space */
77 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
78 VIRTIO_PCI_CONFIG_MSI : \
79 VIRTIO_PCI_CONFIG_NOMSI)
81 /* How many bits to shift physical queue address written to QUEUE_PFN.
82 * 12 is historical, and due to x86 page size. */
83 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
85 /* Flags track per-device state like workarounds for quirks in older guests. */
86 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
88 /* QEMU doesn't strictly need write barriers since everything runs in
89 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
90 * KVM or if kqemu gets SMP support.
92 #define wmb() do { } while (0)
94 /* HACK for virtio to determine if it's running a big endian guest */
95 bool virtio_is_big_endian(void);
99 static void virtio_pci_notify(void *opaque
, uint16_t vector
)
101 VirtIOPCIProxy
*proxy
= opaque
;
102 if (msix_enabled(&proxy
->pci_dev
))
103 msix_notify(&proxy
->pci_dev
, vector
);
105 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
108 static void virtio_pci_save_config(void * opaque
, QEMUFile
*f
)
110 VirtIOPCIProxy
*proxy
= opaque
;
111 pci_device_save(&proxy
->pci_dev
, f
);
112 msix_save(&proxy
->pci_dev
, f
);
113 if (msix_present(&proxy
->pci_dev
))
114 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
117 static void virtio_pci_save_queue(void * opaque
, int n
, QEMUFile
*f
)
119 VirtIOPCIProxy
*proxy
= opaque
;
120 if (msix_present(&proxy
->pci_dev
))
121 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
124 static int virtio_pci_load_config(void * opaque
, QEMUFile
*f
)
126 VirtIOPCIProxy
*proxy
= opaque
;
128 ret
= pci_device_load(&proxy
->pci_dev
, f
);
132 msix_load(&proxy
->pci_dev
, f
);
133 if (msix_present(&proxy
->pci_dev
)) {
134 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
136 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
138 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
139 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
144 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
146 VirtIOPCIProxy
*proxy
= opaque
;
148 if (msix_present(&proxy
->pci_dev
)) {
149 qemu_get_be16s(f
, &vector
);
151 vector
= VIRTIO_NO_VECTOR
;
153 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
154 if (vector
!= VIRTIO_NO_VECTOR
) {
155 return msix_vector_use(&proxy
->pci_dev
, vector
);
160 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
163 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
164 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
168 r
= event_notifier_init(notifier
, 1);
170 error_report("%s: unable to init event notifier: %d",
174 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
175 true, n
, event_notifier_get_fd(notifier
));
177 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
178 true, n
, event_notifier_get_fd(notifier
));
179 /* Handle the race condition where the guest kicked and we deassigned
180 * before we got around to handling the kick.
182 if (event_notifier_test_and_clear(notifier
)) {
183 virtio_queue_notify_vq(vq
);
186 event_notifier_cleanup(notifier
);
191 static void virtio_pci_host_notifier_read(void *opaque
)
193 VirtQueue
*vq
= opaque
;
194 EventNotifier
*n
= virtio_queue_get_host_notifier(vq
);
195 if (event_notifier_test_and_clear(n
)) {
196 virtio_queue_notify_vq(vq
);
200 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy
*proxy
,
203 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
204 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
206 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
207 virtio_pci_host_notifier_read
, NULL
, vq
);
209 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
214 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
218 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
219 proxy
->ioeventfd_disabled
||
220 proxy
->ioeventfd_started
) {
224 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
225 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
229 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true);
234 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, true);
236 proxy
->ioeventfd_started
= true;
241 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
245 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, false);
246 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false);
249 proxy
->ioeventfd_started
= false;
250 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
253 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
258 if (!proxy
->ioeventfd_started
) {
262 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
263 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
267 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, false);
268 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false);
271 proxy
->ioeventfd_started
= false;
274 void virtio_pci_reset(DeviceState
*d
)
276 VirtIOPCIProxy
*proxy
= container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
277 virtio_pci_stop_ioeventfd(proxy
);
278 virtio_reset(proxy
->vdev
);
279 msix_reset(&proxy
->pci_dev
);
280 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
283 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
285 VirtIOPCIProxy
*proxy
= opaque
;
286 VirtIODevice
*vdev
= proxy
->vdev
;
287 target_phys_addr_t pa
;
290 case VIRTIO_PCI_GUEST_FEATURES
:
291 /* Guest does not negotiate properly? We have to assume nothing. */
292 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
293 val
= vdev
->bad_features
? vdev
->bad_features(vdev
) : 0;
295 virtio_set_features(vdev
, val
);
297 case VIRTIO_PCI_QUEUE_PFN
:
298 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
300 virtio_pci_stop_ioeventfd(proxy
);
301 virtio_reset(proxy
->vdev
);
302 msix_unuse_all_vectors(&proxy
->pci_dev
);
305 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
307 case VIRTIO_PCI_QUEUE_SEL
:
308 if (val
< VIRTIO_PCI_QUEUE_MAX
)
309 vdev
->queue_sel
= val
;
311 case VIRTIO_PCI_QUEUE_NOTIFY
:
312 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
313 virtio_queue_notify(vdev
, val
);
316 case VIRTIO_PCI_STATUS
:
317 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
318 virtio_pci_stop_ioeventfd(proxy
);
321 virtio_set_status(vdev
, val
& 0xFF);
323 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
324 virtio_pci_start_ioeventfd(proxy
);
327 if (vdev
->status
== 0) {
328 virtio_reset(proxy
->vdev
);
329 msix_unuse_all_vectors(&proxy
->pci_dev
);
332 /* Linux before 2.6.34 sets the device as OK without enabling
333 the PCI device bus master bit. In this case we need to disable
334 some safety checks. */
335 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
336 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
337 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
340 case VIRTIO_MSI_CONFIG_VECTOR
:
341 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
342 /* Make it possible for guest to discover an error took place. */
343 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
344 val
= VIRTIO_NO_VECTOR
;
345 vdev
->config_vector
= val
;
347 case VIRTIO_MSI_QUEUE_VECTOR
:
348 msix_vector_unuse(&proxy
->pci_dev
,
349 virtio_queue_vector(vdev
, vdev
->queue_sel
));
350 /* Make it possible for guest to discover an error took place. */
351 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
352 val
= VIRTIO_NO_VECTOR
;
353 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
356 error_report("%s: unexpected address 0x%x value 0x%x",
357 __func__
, addr
, val
);
362 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
364 VirtIODevice
*vdev
= proxy
->vdev
;
365 uint32_t ret
= 0xFFFFFFFF;
368 case VIRTIO_PCI_HOST_FEATURES
:
369 ret
= proxy
->host_features
;
371 case VIRTIO_PCI_GUEST_FEATURES
:
372 ret
= vdev
->guest_features
;
374 case VIRTIO_PCI_QUEUE_PFN
:
375 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
376 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
378 case VIRTIO_PCI_QUEUE_NUM
:
379 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
381 case VIRTIO_PCI_QUEUE_SEL
:
382 ret
= vdev
->queue_sel
;
384 case VIRTIO_PCI_STATUS
:
388 /* reading from the ISR also clears it. */
391 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
393 case VIRTIO_MSI_CONFIG_VECTOR
:
394 ret
= vdev
->config_vector
;
396 case VIRTIO_MSI_QUEUE_VECTOR
:
397 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
406 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
408 VirtIOPCIProxy
*proxy
= opaque
;
409 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
411 return virtio_ioport_read(proxy
, addr
);
413 return virtio_config_readb(proxy
->vdev
, addr
);
416 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
418 VirtIOPCIProxy
*proxy
= opaque
;
419 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
422 return virtio_ioport_read(proxy
, addr
);
424 val
= virtio_config_readw(proxy
->vdev
, addr
);
425 if (virtio_is_big_endian()) {
427 * virtio is odd, ioports are LE but config space is target native
428 * endian. However, in qemu, all PIO is LE, so we need to re-swap
436 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
438 VirtIOPCIProxy
*proxy
= opaque
;
439 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
442 return virtio_ioport_read(proxy
, addr
);
444 val
= virtio_config_readl(proxy
->vdev
, addr
);
445 if (virtio_is_big_endian()) {
451 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
453 VirtIOPCIProxy
*proxy
= opaque
;
454 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
456 virtio_ioport_write(proxy
, addr
, val
);
460 virtio_config_writeb(proxy
->vdev
, addr
, val
);
463 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
465 VirtIOPCIProxy
*proxy
= opaque
;
466 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
468 virtio_ioport_write(proxy
, addr
, val
);
472 if (virtio_is_big_endian()) {
475 virtio_config_writew(proxy
->vdev
, addr
, val
);
478 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
480 VirtIOPCIProxy
*proxy
= opaque
;
481 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
483 virtio_ioport_write(proxy
, addr
, val
);
487 if (virtio_is_big_endian()) {
490 virtio_config_writel(proxy
->vdev
, addr
, val
);
493 const MemoryRegionPortio virtio_portio
[] = {
494 { 0, 0x10000, 1, .write
= virtio_pci_config_writeb
, },
495 { 0, 0x10000, 2, .write
= virtio_pci_config_writew
, },
496 { 0, 0x10000, 4, .write
= virtio_pci_config_writel
, },
497 { 0, 0x10000, 1, .read
= virtio_pci_config_readb
, },
498 { 0, 0x10000, 2, .read
= virtio_pci_config_readw
, },
499 { 0, 0x10000, 4, .read
= virtio_pci_config_readl
, },
503 static const MemoryRegionOps virtio_pci_config_ops
= {
504 .old_portio
= virtio_portio
,
505 .endianness
= DEVICE_LITTLE_ENDIAN
,
508 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
509 uint32_t val
, int len
)
511 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
513 pci_default_write_config(pci_dev
, address
, val
, len
);
515 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
516 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
517 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
518 virtio_pci_stop_ioeventfd(proxy
);
519 virtio_set_status(proxy
->vdev
,
520 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
523 msix_write_config(pci_dev
, address
, val
, len
);
526 static unsigned virtio_pci_get_features(void *opaque
)
528 VirtIOPCIProxy
*proxy
= opaque
;
529 return proxy
->host_features
;
532 static void virtio_pci_guest_notifier_read(void *opaque
)
534 VirtQueue
*vq
= opaque
;
535 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
536 if (event_notifier_test_and_clear(n
)) {
541 static int virtio_pci_mask_vq(PCIDevice
*dev
, unsigned vector
,
542 VirtQueue
*vq
, int masked
)
544 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
545 int r
= kvm_set_irqfd(dev
->msix_irq_entries
[vector
].gsi
,
546 event_notifier_get_fd(notifier
),
549 return (r
== -ENOSYS
) ? 0 : r
;
552 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
553 virtio_pci_guest_notifier_read
, NULL
, vq
);
555 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
561 static int virtio_pci_mask_notifier(PCIDevice
*dev
, unsigned vector
,
564 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
565 VirtIODevice
*vdev
= proxy
->vdev
;
568 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
569 if (!virtio_queue_get_num(vdev
, n
)) {
572 if (virtio_queue_vector(vdev
, n
) != vector
) {
575 r
= virtio_pci_mask_vq(dev
, vector
, virtio_get_queue(vdev
, n
), masked
);
583 if (virtio_queue_vector(vdev
, n
) != vector
) {
586 virtio_pci_mask_vq(dev
, vector
, virtio_get_queue(vdev
, n
), !masked
);
592 static int virtio_pci_set_guest_notifier(void *opaque
, int n
, bool assign
)
594 VirtIOPCIProxy
*proxy
= opaque
;
595 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
596 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
599 int r
= event_notifier_init(notifier
, 0);
603 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
604 virtio_pci_guest_notifier_read
, NULL
, vq
);
606 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
608 /* Test and clear notifier before closing it,
609 * in case poll callback didn't have time to run. */
610 virtio_pci_guest_notifier_read(vq
);
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 mask notifier while guest notifier
630 * is still assigned */
632 r
= msix_unset_mask_notifier(&proxy
->pci_dev
);
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 mask notifier after guest notifier
648 * has been assigned */
650 r
= msix_set_mask_notifier(&proxy
->pci_dev
,
651 virtio_pci_mask_notifier
);
660 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
662 virtio_pci_set_guest_notifier(opaque
, n
, !assign
);
666 msix_set_mask_notifier(&proxy
->pci_dev
,
667 virtio_pci_mask_notifier
);
672 static int virtio_pci_set_host_notifier(void *opaque
, int n
, bool assign
)
674 VirtIOPCIProxy
*proxy
= opaque
;
676 /* Stop using ioeventfd for virtqueue kick if the device starts using host
677 * notifiers. This makes it easy to avoid stepping on each others' toes.
679 proxy
->ioeventfd_disabled
= assign
;
681 virtio_pci_stop_ioeventfd(proxy
);
683 /* We don't need to start here: it's not needed because backend
684 * currently only stops on status change away from ok,
685 * reset, vmstop and such. If we do add code to start here,
686 * need to check vmstate, device state etc. */
687 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
);
690 static void virtio_pci_vmstate_change(void *opaque
, bool running
)
692 VirtIOPCIProxy
*proxy
= opaque
;
695 /* Try to find out if the guest has bus master disabled, but is
696 in ready state. Then we have a buggy guest OS. */
697 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
698 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
699 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
701 virtio_pci_start_ioeventfd(proxy
);
703 virtio_pci_stop_ioeventfd(proxy
);
707 static const VirtIOBindings virtio_pci_bindings
= {
708 .notify
= virtio_pci_notify
,
709 .save_config
= virtio_pci_save_config
,
710 .load_config
= virtio_pci_load_config
,
711 .save_queue
= virtio_pci_save_queue
,
712 .load_queue
= virtio_pci_load_queue
,
713 .get_features
= virtio_pci_get_features
,
714 .query_guest_notifiers
= virtio_pci_query_guest_notifiers
,
715 .set_host_notifier
= virtio_pci_set_host_notifier
,
716 .set_guest_notifiers
= virtio_pci_set_guest_notifiers
,
717 .vmstate_change
= virtio_pci_vmstate_change
,
720 void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
)
727 config
= proxy
->pci_dev
.config
;
729 if (proxy
->class_code
) {
730 pci_config_set_class(config
, proxy
->class_code
);
732 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
733 pci_get_word(config
+ PCI_VENDOR_ID
));
734 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, vdev
->device_id
);
735 config
[PCI_INTERRUPT_PIN
] = 1;
737 memory_region_init(&proxy
->msix_bar
, "virtio-msix", 4096);
738 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
,
739 &proxy
->msix_bar
, 1, 0)) {
740 pci_register_bar(&proxy
->pci_dev
, 1, PCI_BASE_ADDRESS_SPACE_MEMORY
,
745 proxy
->pci_dev
.config_write
= virtio_write_config
;
747 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
749 size
= 1 << qemu_fls(size
);
751 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
753 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
756 if (!kvm_has_many_ioeventfds()) {
757 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
760 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
761 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
762 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
763 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
766 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
768 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
771 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
772 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
773 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
775 vdev
= virtio_blk_init(&pci_dev
->qdev
, &proxy
->block
,
776 &proxy
->block_serial
);
780 vdev
->nvectors
= proxy
->nvectors
;
781 virtio_init_pci(proxy
, vdev
);
782 /* make the actual value visible */
783 proxy
->nvectors
= vdev
->nvectors
;
787 static int virtio_exit_pci(PCIDevice
*pci_dev
)
789 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
792 memory_region_destroy(&proxy
->bar
);
793 r
= msix_uninit(pci_dev
, &proxy
->msix_bar
);
794 memory_region_destroy(&proxy
->msix_bar
);
798 static int virtio_blk_exit_pci(PCIDevice
*pci_dev
)
800 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
802 virtio_pci_stop_ioeventfd(proxy
);
803 virtio_blk_exit(proxy
->vdev
);
804 blockdev_mark_auto_del(proxy
->block
.bs
);
805 return virtio_exit_pci(pci_dev
);
808 static int virtio_serial_init_pci(PCIDevice
*pci_dev
)
810 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
813 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
814 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
815 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
816 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
818 vdev
= virtio_serial_init(&pci_dev
->qdev
, &proxy
->serial
);
822 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
823 ? proxy
->serial
.max_virtserial_ports
+ 1
825 virtio_init_pci(proxy
, vdev
);
826 proxy
->nvectors
= vdev
->nvectors
;
830 static int virtio_serial_exit_pci(PCIDevice
*pci_dev
)
832 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
834 virtio_pci_stop_ioeventfd(proxy
);
835 virtio_serial_exit(proxy
->vdev
);
836 return virtio_exit_pci(pci_dev
);
839 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
841 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
844 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
, &proxy
->net
);
846 vdev
->nvectors
= proxy
->nvectors
;
847 virtio_init_pci(proxy
, vdev
);
849 /* make the actual value visible */
850 proxy
->nvectors
= vdev
->nvectors
;
854 static int virtio_net_exit_pci(PCIDevice
*pci_dev
)
856 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
858 virtio_pci_stop_ioeventfd(proxy
);
859 virtio_net_exit(proxy
->vdev
);
860 return virtio_exit_pci(pci_dev
);
863 static int virtio_balloon_init_pci(PCIDevice
*pci_dev
)
865 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
868 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
872 virtio_init_pci(proxy
, vdev
);
876 static int virtio_balloon_exit_pci(PCIDevice
*pci_dev
)
878 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
880 virtio_pci_stop_ioeventfd(proxy
);
881 virtio_balloon_exit(proxy
->vdev
);
882 return virtio_exit_pci(pci_dev
);
885 static Property virtio_blk_properties
[] = {
886 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
887 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy
, block
),
888 DEFINE_PROP_STRING("serial", VirtIOPCIProxy
, block_serial
),
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 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
899 k
->init
= virtio_blk_init_pci
;
900 k
->exit
= virtio_blk_exit_pci
;
901 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
902 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
903 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
904 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
907 static DeviceInfo virtio_blk_info
= {
908 .name
= "virtio-blk-pci",
909 .alias
= "virtio-blk",
910 .size
= sizeof(VirtIOPCIProxy
),
911 .props
= virtio_blk_properties
,
912 .reset
= virtio_pci_reset
,
913 .class_init
= virtio_blk_class_init
,
916 static Property virtio_net_properties
[] = {
917 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
918 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
919 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
920 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
921 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy
, net
.txtimer
, TX_TIMER_INTERVAL
),
922 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy
, net
.txburst
, TX_BURST
),
923 DEFINE_PROP_STRING("tx", VirtIOPCIProxy
, net
.tx
),
924 DEFINE_PROP_END_OF_LIST(),
927 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
929 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
931 k
->init
= virtio_net_init_pci
;
932 k
->exit
= virtio_net_exit_pci
;
933 k
->romfile
= "pxe-virtio.rom";
934 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
935 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
936 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
937 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
940 static DeviceInfo virtio_net_info
= {
941 .name
= "virtio-net-pci",
942 .alias
= "virtio-net",
943 .size
= sizeof(VirtIOPCIProxy
),
944 .props
= virtio_net_properties
,
945 .reset
= virtio_pci_reset
,
946 .class_init
= virtio_net_class_init
,
949 static Property virtio_serial_properties
[] = {
950 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
951 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, DEV_NVECTORS_UNSPECIFIED
),
952 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
953 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
954 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
, serial
.max_virtserial_ports
, 31),
955 DEFINE_PROP_END_OF_LIST(),
958 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
960 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
962 k
->init
= virtio_serial_init_pci
;
963 k
->exit
= virtio_serial_exit_pci
;
964 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
965 k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
966 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
967 k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
970 static DeviceInfo virtio_serial_info
= {
971 .name
= "virtio-serial-pci",
972 .alias
= "virtio-serial",
973 .size
= sizeof(VirtIOPCIProxy
),
974 .props
= virtio_serial_properties
,
975 .reset
= virtio_pci_reset
,
976 .class_init
= virtio_serial_class_init
,
979 static Property virtio_balloon_properties
[] = {
980 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
981 DEFINE_PROP_END_OF_LIST(),
984 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
986 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
988 k
->init
= virtio_balloon_init_pci
;
989 k
->exit
= virtio_balloon_exit_pci
;
990 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
991 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
992 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
993 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
996 static DeviceInfo virtio_balloon_info
= {
997 .name
= "virtio-balloon-pci",
998 .alias
= "virtio-balloon",
999 .size
= sizeof(VirtIOPCIProxy
),
1000 .props
= virtio_balloon_properties
,
1001 .reset
= virtio_pci_reset
,
1002 .class_init
= virtio_balloon_class_init
,
1005 static void virtio_pci_register_devices(void)
1007 pci_qdev_register(&virtio_blk_info
);
1008 pci_qdev_register(&virtio_net_info
);
1009 pci_qdev_register(&virtio_serial_info
);
1010 pci_qdev_register(&virtio_balloon_info
);
1013 device_init(virtio_pci_register_devices
)