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"
29 #include "virtio-pci.h"
32 /* from Linux's linux/virtio_pci.h */
34 /* A 32-bit r/o bitmask of the features supported by the host */
35 #define VIRTIO_PCI_HOST_FEATURES 0
37 /* A 32-bit r/w bitmask of features activated by the guest */
38 #define VIRTIO_PCI_GUEST_FEATURES 4
40 /* A 32-bit r/w PFN for the currently selected queue */
41 #define VIRTIO_PCI_QUEUE_PFN 8
43 /* A 16-bit r/o queue size for the currently selected queue */
44 #define VIRTIO_PCI_QUEUE_NUM 12
46 /* A 16-bit r/w queue selector */
47 #define VIRTIO_PCI_QUEUE_SEL 14
49 /* A 16-bit r/w queue notifier */
50 #define VIRTIO_PCI_QUEUE_NOTIFY 16
52 /* An 8-bit device status register. */
53 #define VIRTIO_PCI_STATUS 18
55 /* An 8-bit r/o interrupt status register. Reading the value will return the
56 * current contents of the ISR and will also clear it. This is effectively
57 * a read-and-acknowledge. */
58 #define VIRTIO_PCI_ISR 19
60 /* MSI-X registers: only enabled if MSI-X is enabled. */
61 /* A 16-bit vector for configuration changes. */
62 #define VIRTIO_MSI_CONFIG_VECTOR 20
63 /* A 16-bit vector for selected queue notifications. */
64 #define VIRTIO_MSI_QUEUE_VECTOR 22
66 /* Config space size */
67 #define VIRTIO_PCI_CONFIG_NOMSI 20
68 #define VIRTIO_PCI_CONFIG_MSI 24
69 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
70 VIRTIO_PCI_CONFIG_MSI : \
71 VIRTIO_PCI_CONFIG_NOMSI)
73 /* The remaining space is defined by each driver as the per-driver
74 * configuration space */
75 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
76 VIRTIO_PCI_CONFIG_MSI : \
77 VIRTIO_PCI_CONFIG_NOMSI)
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)
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 static 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 if (vdev
->bad_features
)
294 val
= proxy
->host_features
& vdev
->bad_features(vdev
);
298 if (vdev
->set_features
)
299 vdev
->set_features(vdev
, val
);
300 vdev
->guest_features
= val
;
302 case VIRTIO_PCI_QUEUE_PFN
:
303 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
305 virtio_pci_stop_ioeventfd(proxy
);
306 virtio_reset(proxy
->vdev
);
307 msix_unuse_all_vectors(&proxy
->pci_dev
);
310 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
312 case VIRTIO_PCI_QUEUE_SEL
:
313 if (val
< VIRTIO_PCI_QUEUE_MAX
)
314 vdev
->queue_sel
= val
;
316 case VIRTIO_PCI_QUEUE_NOTIFY
:
317 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
318 virtio_queue_notify(vdev
, val
);
321 case VIRTIO_PCI_STATUS
:
322 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
323 virtio_pci_stop_ioeventfd(proxy
);
326 virtio_set_status(vdev
, val
& 0xFF);
328 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
329 virtio_pci_start_ioeventfd(proxy
);
332 if (vdev
->status
== 0) {
333 virtio_reset(proxy
->vdev
);
334 msix_unuse_all_vectors(&proxy
->pci_dev
);
337 /* Linux before 2.6.34 sets the device as OK without enabling
338 the PCI device bus master bit. In this case we need to disable
339 some safety checks. */
340 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
341 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
342 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
345 case VIRTIO_MSI_CONFIG_VECTOR
:
346 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
347 /* Make it possible for guest to discover an error took place. */
348 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
349 val
= VIRTIO_NO_VECTOR
;
350 vdev
->config_vector
= val
;
352 case VIRTIO_MSI_QUEUE_VECTOR
:
353 msix_vector_unuse(&proxy
->pci_dev
,
354 virtio_queue_vector(vdev
, vdev
->queue_sel
));
355 /* Make it possible for guest to discover an error took place. */
356 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
357 val
= VIRTIO_NO_VECTOR
;
358 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
361 error_report("%s: unexpected address 0x%x value 0x%x",
362 __func__
, addr
, val
);
367 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
369 VirtIODevice
*vdev
= proxy
->vdev
;
370 uint32_t ret
= 0xFFFFFFFF;
373 case VIRTIO_PCI_HOST_FEATURES
:
374 ret
= proxy
->host_features
;
376 case VIRTIO_PCI_GUEST_FEATURES
:
377 ret
= vdev
->guest_features
;
379 case VIRTIO_PCI_QUEUE_PFN
:
380 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
381 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
383 case VIRTIO_PCI_QUEUE_NUM
:
384 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
386 case VIRTIO_PCI_QUEUE_SEL
:
387 ret
= vdev
->queue_sel
;
389 case VIRTIO_PCI_STATUS
:
393 /* reading from the ISR also clears it. */
396 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
398 case VIRTIO_MSI_CONFIG_VECTOR
:
399 ret
= vdev
->config_vector
;
401 case VIRTIO_MSI_QUEUE_VECTOR
:
402 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
411 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
413 VirtIOPCIProxy
*proxy
= opaque
;
414 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
416 return virtio_ioport_read(proxy
, addr
);
418 return virtio_config_readb(proxy
->vdev
, addr
);
421 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
423 VirtIOPCIProxy
*proxy
= opaque
;
424 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
426 return virtio_ioport_read(proxy
, addr
);
428 return virtio_config_readw(proxy
->vdev
, addr
);
431 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
433 VirtIOPCIProxy
*proxy
= opaque
;
434 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
436 return virtio_ioport_read(proxy
, addr
);
438 return virtio_config_readl(proxy
->vdev
, addr
);
441 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
443 VirtIOPCIProxy
*proxy
= opaque
;
444 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
446 virtio_ioport_write(proxy
, addr
, val
);
450 virtio_config_writeb(proxy
->vdev
, addr
, val
);
453 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
455 VirtIOPCIProxy
*proxy
= opaque
;
456 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
458 virtio_ioport_write(proxy
, addr
, val
);
462 virtio_config_writew(proxy
->vdev
, addr
, val
);
465 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
467 VirtIOPCIProxy
*proxy
= opaque
;
468 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
470 virtio_ioport_write(proxy
, addr
, val
);
474 virtio_config_writel(proxy
->vdev
, addr
, val
);
477 const MemoryRegionPortio virtio_portio
[] = {
478 { 0, 0x10000, 1, .write
= virtio_pci_config_writeb
, },
479 { 0, 0x10000, 2, .write
= virtio_pci_config_writew
, },
480 { 0, 0x10000, 4, .write
= virtio_pci_config_writel
, },
481 { 0, 0x10000, 1, .read
= virtio_pci_config_readb
, },
482 { 0, 0x10000, 2, .read
= virtio_pci_config_readw
, },
483 { 0, 0x10000, 4, .read
= virtio_pci_config_readl
, },
487 static const MemoryRegionOps virtio_pci_config_ops
= {
488 .old_portio
= virtio_portio
,
489 .endianness
= DEVICE_LITTLE_ENDIAN
,
492 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
493 uint32_t val
, int len
)
495 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
497 pci_default_write_config(pci_dev
, address
, val
, len
);
499 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
500 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
501 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
502 virtio_pci_stop_ioeventfd(proxy
);
503 virtio_set_status(proxy
->vdev
,
504 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
507 msix_write_config(pci_dev
, address
, val
, len
);
510 static unsigned virtio_pci_get_features(void *opaque
)
512 VirtIOPCIProxy
*proxy
= opaque
;
513 return proxy
->host_features
;
516 static void virtio_pci_guest_notifier_read(void *opaque
)
518 VirtQueue
*vq
= opaque
;
519 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
520 if (event_notifier_test_and_clear(n
)) {
525 static int virtio_pci_set_guest_notifier(void *opaque
, int n
, bool assign
)
527 VirtIOPCIProxy
*proxy
= opaque
;
528 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
529 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
532 int r
= event_notifier_init(notifier
, 0);
536 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
537 virtio_pci_guest_notifier_read
, NULL
, vq
);
539 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
541 event_notifier_cleanup(notifier
);
547 static bool virtio_pci_query_guest_notifiers(void *opaque
)
549 VirtIOPCIProxy
*proxy
= opaque
;
550 return msix_enabled(&proxy
->pci_dev
);
553 static int virtio_pci_set_guest_notifiers(void *opaque
, bool assign
)
555 VirtIOPCIProxy
*proxy
= opaque
;
556 VirtIODevice
*vdev
= proxy
->vdev
;
559 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
560 if (!virtio_queue_get_num(vdev
, n
)) {
564 r
= virtio_pci_set_guest_notifier(opaque
, n
, assign
);
573 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
575 virtio_pci_set_guest_notifier(opaque
, n
, !assign
);
580 static int virtio_pci_set_host_notifier(void *opaque
, int n
, bool assign
)
582 VirtIOPCIProxy
*proxy
= opaque
;
584 /* Stop using ioeventfd for virtqueue kick if the device starts using host
585 * notifiers. This makes it easy to avoid stepping on each others' toes.
587 proxy
->ioeventfd_disabled
= assign
;
589 virtio_pci_stop_ioeventfd(proxy
);
591 /* We don't need to start here: it's not needed because backend
592 * currently only stops on status change away from ok,
593 * reset, vmstop and such. If we do add code to start here,
594 * need to check vmstate, device state etc. */
595 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
);
598 static void virtio_pci_vmstate_change(void *opaque
, bool running
)
600 VirtIOPCIProxy
*proxy
= opaque
;
603 /* Try to find out if the guest has bus master disabled, but is
604 in ready state. Then we have a buggy guest OS. */
605 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
606 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
607 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
609 virtio_pci_start_ioeventfd(proxy
);
611 virtio_pci_stop_ioeventfd(proxy
);
615 static const VirtIOBindings virtio_pci_bindings
= {
616 .notify
= virtio_pci_notify
,
617 .save_config
= virtio_pci_save_config
,
618 .load_config
= virtio_pci_load_config
,
619 .save_queue
= virtio_pci_save_queue
,
620 .load_queue
= virtio_pci_load_queue
,
621 .get_features
= virtio_pci_get_features
,
622 .query_guest_notifiers
= virtio_pci_query_guest_notifiers
,
623 .set_host_notifier
= virtio_pci_set_host_notifier
,
624 .set_guest_notifiers
= virtio_pci_set_guest_notifiers
,
625 .vmstate_change
= virtio_pci_vmstate_change
,
628 void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
)
635 config
= proxy
->pci_dev
.config
;
637 if (proxy
->class_code
) {
638 pci_config_set_class(config
, proxy
->class_code
);
640 pci_set_word(config
+ 0x2c, pci_get_word(config
+ PCI_VENDOR_ID
));
641 pci_set_word(config
+ 0x2e, vdev
->device_id
);
644 memory_region_init(&proxy
->msix_bar
, "virtio-msix", 4096);
645 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
,
646 &proxy
->msix_bar
, 1, 0)) {
647 pci_register_bar(&proxy
->pci_dev
, 1, PCI_BASE_ADDRESS_SPACE_MEMORY
,
652 proxy
->pci_dev
.config_write
= virtio_write_config
;
654 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
656 size
= 1 << qemu_fls(size
);
658 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
660 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
663 if (!kvm_has_many_ioeventfds()) {
664 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
667 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
668 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
669 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
670 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
673 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
675 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
678 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
679 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
680 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
682 vdev
= virtio_blk_init(&pci_dev
->qdev
, &proxy
->block
,
683 &proxy
->block_serial
);
687 vdev
->nvectors
= proxy
->nvectors
;
688 virtio_init_pci(proxy
, vdev
);
689 /* make the actual value visible */
690 proxy
->nvectors
= vdev
->nvectors
;
694 static int virtio_exit_pci(PCIDevice
*pci_dev
)
696 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
699 memory_region_destroy(&proxy
->bar
);
700 r
= msix_uninit(pci_dev
, &proxy
->msix_bar
);
701 memory_region_destroy(&proxy
->msix_bar
);
705 static int virtio_blk_exit_pci(PCIDevice
*pci_dev
)
707 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
709 virtio_pci_stop_ioeventfd(proxy
);
710 virtio_blk_exit(proxy
->vdev
);
711 blockdev_mark_auto_del(proxy
->block
.bs
);
712 return virtio_exit_pci(pci_dev
);
715 static int virtio_serial_init_pci(PCIDevice
*pci_dev
)
717 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
720 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
721 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
722 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
723 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
725 vdev
= virtio_serial_init(&pci_dev
->qdev
, &proxy
->serial
);
729 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
730 ? proxy
->serial
.max_virtserial_ports
+ 1
732 virtio_init_pci(proxy
, vdev
);
733 proxy
->nvectors
= vdev
->nvectors
;
737 static int virtio_serial_exit_pci(PCIDevice
*pci_dev
)
739 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
741 virtio_pci_stop_ioeventfd(proxy
);
742 virtio_serial_exit(proxy
->vdev
);
743 return virtio_exit_pci(pci_dev
);
746 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
748 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
751 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
, &proxy
->net
);
753 vdev
->nvectors
= proxy
->nvectors
;
754 virtio_init_pci(proxy
, vdev
);
756 /* make the actual value visible */
757 proxy
->nvectors
= vdev
->nvectors
;
761 static int virtio_net_exit_pci(PCIDevice
*pci_dev
)
763 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
765 virtio_pci_stop_ioeventfd(proxy
);
766 virtio_net_exit(proxy
->vdev
);
767 return virtio_exit_pci(pci_dev
);
770 static int virtio_balloon_init_pci(PCIDevice
*pci_dev
)
772 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
775 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
779 virtio_init_pci(proxy
, vdev
);
783 static int virtio_balloon_exit_pci(PCIDevice
*pci_dev
)
785 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
787 virtio_pci_stop_ioeventfd(proxy
);
788 virtio_balloon_exit(proxy
->vdev
);
789 return virtio_exit_pci(pci_dev
);
792 static PCIDeviceInfo virtio_info
[] = {
794 .qdev
.name
= "virtio-blk-pci",
795 .qdev
.alias
= "virtio-blk",
796 .qdev
.size
= sizeof(VirtIOPCIProxy
),
797 .init
= virtio_blk_init_pci
,
798 .exit
= virtio_blk_exit_pci
,
799 .vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
,
800 .device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
,
801 .revision
= VIRTIO_PCI_ABI_VERSION
,
802 .class_id
= PCI_CLASS_STORAGE_SCSI
,
803 .qdev
.props
= (Property
[]) {
804 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
805 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy
, block
),
806 DEFINE_PROP_STRING("serial", VirtIOPCIProxy
, block_serial
),
807 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
808 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
809 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
810 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
811 DEFINE_PROP_END_OF_LIST(),
813 .qdev
.reset
= virtio_pci_reset
,
815 .qdev
.name
= "virtio-net-pci",
816 .qdev
.alias
= "virtio-net",
817 .qdev
.size
= sizeof(VirtIOPCIProxy
),
818 .init
= virtio_net_init_pci
,
819 .exit
= virtio_net_exit_pci
,
820 .romfile
= "pxe-virtio.rom",
821 .vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
,
822 .device_id
= PCI_DEVICE_ID_VIRTIO_NET
,
823 .revision
= VIRTIO_PCI_ABI_VERSION
,
824 .class_id
= PCI_CLASS_NETWORK_ETHERNET
,
825 .qdev
.props
= (Property
[]) {
826 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
827 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
828 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
829 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
830 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
831 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy
,
832 net
.txtimer
, TX_TIMER_INTERVAL
),
833 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy
,
834 net
.txburst
, TX_BURST
),
835 DEFINE_PROP_STRING("tx", VirtIOPCIProxy
, net
.tx
),
836 DEFINE_PROP_END_OF_LIST(),
838 .qdev
.reset
= virtio_pci_reset
,
840 .qdev
.name
= "virtio-serial-pci",
841 .qdev
.alias
= "virtio-serial",
842 .qdev
.size
= sizeof(VirtIOPCIProxy
),
843 .init
= virtio_serial_init_pci
,
844 .exit
= virtio_serial_exit_pci
,
845 .vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
,
846 .device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
,
847 .revision
= VIRTIO_PCI_ABI_VERSION
,
848 .class_id
= PCI_CLASS_COMMUNICATION_OTHER
,
849 .qdev
.props
= (Property
[]) {
850 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
851 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
852 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
853 DEV_NVECTORS_UNSPECIFIED
),
854 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
855 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
856 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
,
857 serial
.max_virtserial_ports
, 31),
858 DEFINE_PROP_END_OF_LIST(),
860 .qdev
.reset
= virtio_pci_reset
,
862 .qdev
.name
= "virtio-balloon-pci",
863 .qdev
.alias
= "virtio-balloon",
864 .qdev
.size
= sizeof(VirtIOPCIProxy
),
865 .init
= virtio_balloon_init_pci
,
866 .exit
= virtio_balloon_exit_pci
,
867 .vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
,
868 .device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
,
869 .revision
= VIRTIO_PCI_ABI_VERSION
,
870 .class_id
= PCI_CLASS_MEMORY_RAM
,
871 .qdev
.props
= (Property
[]) {
872 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
873 DEFINE_PROP_END_OF_LIST(),
875 .qdev
.reset
= virtio_pci_reset
,
881 static void virtio_pci_register_devices(void)
883 pci_qdev_register_many(virtio_info
);
886 device_init(virtio_pci_register_devices
)