4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
21 #include "virtio-blk.h"
22 #include "virtio-net.h"
23 #include "virtio-serial.h"
24 #include "virtio-scsi.h"
26 #include "qemu-error.h"
32 #include "virtio-pci.h"
35 /* from Linux's linux/virtio_pci.h */
37 /* A 32-bit r/o bitmask of the features supported by the host */
38 #define VIRTIO_PCI_HOST_FEATURES 0
40 /* A 32-bit r/w bitmask of features activated by the guest */
41 #define VIRTIO_PCI_GUEST_FEATURES 4
43 /* A 32-bit r/w PFN for the currently selected queue */
44 #define VIRTIO_PCI_QUEUE_PFN 8
46 /* A 16-bit r/o queue size for the currently selected queue */
47 #define VIRTIO_PCI_QUEUE_NUM 12
49 /* A 16-bit r/w queue selector */
50 #define VIRTIO_PCI_QUEUE_SEL 14
52 /* A 16-bit r/w queue notifier */
53 #define VIRTIO_PCI_QUEUE_NOTIFY 16
55 /* An 8-bit device status register. */
56 #define VIRTIO_PCI_STATUS 18
58 /* An 8-bit r/o interrupt status register. Reading the value will return the
59 * current contents of the ISR and will also clear it. This is effectively
60 * a read-and-acknowledge. */
61 #define VIRTIO_PCI_ISR 19
63 /* MSI-X registers: only enabled if MSI-X is enabled. */
64 /* A 16-bit vector for configuration changes. */
65 #define VIRTIO_MSI_CONFIG_VECTOR 20
66 /* A 16-bit vector for selected queue notifications. */
67 #define VIRTIO_MSI_QUEUE_VECTOR 22
69 /* Config space size */
70 #define VIRTIO_PCI_CONFIG_NOMSI 20
71 #define VIRTIO_PCI_CONFIG_MSI 24
72 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
73 VIRTIO_PCI_CONFIG_MSI : \
74 VIRTIO_PCI_CONFIG_NOMSI)
76 /* The remaining space is defined by each driver as the per-driver
77 * configuration space */
78 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
79 VIRTIO_PCI_CONFIG_MSI : \
80 VIRTIO_PCI_CONFIG_NOMSI)
82 /* How many bits to shift physical queue address written to QUEUE_PFN.
83 * 12 is historical, and due to x86 page size. */
84 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
86 /* Flags track per-device state like workarounds for quirks in older guests. */
87 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
89 /* QEMU doesn't strictly need write barriers since everything runs in
90 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
91 * KVM or if kqemu gets SMP support.
93 #define wmb() do { } while (0)
95 /* HACK for virtio to determine if it's running a big endian guest */
96 bool virtio_is_big_endian(void);
100 static void virtio_pci_notify(void *opaque
, uint16_t vector
)
102 VirtIOPCIProxy
*proxy
= opaque
;
103 if (msix_enabled(&proxy
->pci_dev
))
104 msix_notify(&proxy
->pci_dev
, vector
);
106 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
109 static void virtio_pci_save_config(void * opaque
, QEMUFile
*f
)
111 VirtIOPCIProxy
*proxy
= opaque
;
112 pci_device_save(&proxy
->pci_dev
, f
);
113 msix_save(&proxy
->pci_dev
, f
);
114 if (msix_present(&proxy
->pci_dev
))
115 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
118 static void virtio_pci_save_queue(void * opaque
, int n
, QEMUFile
*f
)
120 VirtIOPCIProxy
*proxy
= opaque
;
121 if (msix_present(&proxy
->pci_dev
))
122 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
125 static int virtio_pci_load_config(void * opaque
, QEMUFile
*f
)
127 VirtIOPCIProxy
*proxy
= opaque
;
129 ret
= pci_device_load(&proxy
->pci_dev
, f
);
133 msix_load(&proxy
->pci_dev
, f
);
134 if (msix_present(&proxy
->pci_dev
)) {
135 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
137 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
139 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
140 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
145 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
147 VirtIOPCIProxy
*proxy
= opaque
;
149 if (msix_present(&proxy
->pci_dev
)) {
150 qemu_get_be16s(f
, &vector
);
152 vector
= VIRTIO_NO_VECTOR
;
154 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
155 if (vector
!= VIRTIO_NO_VECTOR
) {
156 return msix_vector_use(&proxy
->pci_dev
, vector
);
161 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
164 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
165 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
169 r
= event_notifier_init(notifier
, 1);
171 error_report("%s: unable to init event notifier: %d",
175 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
176 true, n
, event_notifier_get_fd(notifier
));
178 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
179 true, n
, event_notifier_get_fd(notifier
));
180 /* Handle the race condition where the guest kicked and we deassigned
181 * before we got around to handling the kick.
183 if (event_notifier_test_and_clear(notifier
)) {
184 virtio_queue_notify_vq(vq
);
187 event_notifier_cleanup(notifier
);
192 static void virtio_pci_host_notifier_read(void *opaque
)
194 VirtQueue
*vq
= opaque
;
195 EventNotifier
*n
= virtio_queue_get_host_notifier(vq
);
196 if (event_notifier_test_and_clear(n
)) {
197 virtio_queue_notify_vq(vq
);
201 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy
*proxy
,
204 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
205 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
207 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
208 virtio_pci_host_notifier_read
, NULL
, vq
);
210 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
215 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
219 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
220 proxy
->ioeventfd_disabled
||
221 proxy
->ioeventfd_started
) {
225 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
226 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
230 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true);
235 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, true);
237 proxy
->ioeventfd_started
= true;
242 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
246 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, false);
247 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false);
250 proxy
->ioeventfd_started
= false;
251 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
254 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
259 if (!proxy
->ioeventfd_started
) {
263 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
264 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
268 virtio_pci_set_host_notifier_fd_handler(proxy
, n
, false);
269 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false);
272 proxy
->ioeventfd_started
= false;
275 void virtio_pci_reset(DeviceState
*d
)
277 VirtIOPCIProxy
*proxy
= container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
278 virtio_pci_stop_ioeventfd(proxy
);
279 virtio_reset(proxy
->vdev
);
280 msix_reset(&proxy
->pci_dev
);
281 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
284 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
286 VirtIOPCIProxy
*proxy
= opaque
;
287 VirtIODevice
*vdev
= proxy
->vdev
;
288 target_phys_addr_t pa
;
291 case VIRTIO_PCI_GUEST_FEATURES
:
292 /* Guest does not negotiate properly? We have to assume nothing. */
293 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
294 val
= vdev
->bad_features
? vdev
->bad_features(vdev
) : 0;
296 virtio_set_features(vdev
, val
);
298 case VIRTIO_PCI_QUEUE_PFN
:
299 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
301 virtio_pci_stop_ioeventfd(proxy
);
302 virtio_reset(proxy
->vdev
);
303 msix_unuse_all_vectors(&proxy
->pci_dev
);
306 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
308 case VIRTIO_PCI_QUEUE_SEL
:
309 if (val
< VIRTIO_PCI_QUEUE_MAX
)
310 vdev
->queue_sel
= val
;
312 case VIRTIO_PCI_QUEUE_NOTIFY
:
313 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
314 virtio_queue_notify(vdev
, val
);
317 case VIRTIO_PCI_STATUS
:
318 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
319 virtio_pci_stop_ioeventfd(proxy
);
322 virtio_set_status(vdev
, val
& 0xFF);
324 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
325 virtio_pci_start_ioeventfd(proxy
);
328 if (vdev
->status
== 0) {
329 virtio_reset(proxy
->vdev
);
330 msix_unuse_all_vectors(&proxy
->pci_dev
);
333 /* Linux before 2.6.34 sets the device as OK without enabling
334 the PCI device bus master bit. In this case we need to disable
335 some safety checks. */
336 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
337 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
338 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
341 case VIRTIO_MSI_CONFIG_VECTOR
:
342 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
343 /* Make it possible for guest to discover an error took place. */
344 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
345 val
= VIRTIO_NO_VECTOR
;
346 vdev
->config_vector
= val
;
348 case VIRTIO_MSI_QUEUE_VECTOR
:
349 msix_vector_unuse(&proxy
->pci_dev
,
350 virtio_queue_vector(vdev
, vdev
->queue_sel
));
351 /* Make it possible for guest to discover an error took place. */
352 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
353 val
= VIRTIO_NO_VECTOR
;
354 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
357 error_report("%s: unexpected address 0x%x value 0x%x",
358 __func__
, addr
, val
);
363 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
365 VirtIODevice
*vdev
= proxy
->vdev
;
366 uint32_t ret
= 0xFFFFFFFF;
369 case VIRTIO_PCI_HOST_FEATURES
:
370 ret
= proxy
->host_features
;
372 case VIRTIO_PCI_GUEST_FEATURES
:
373 ret
= vdev
->guest_features
;
375 case VIRTIO_PCI_QUEUE_PFN
:
376 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
377 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
379 case VIRTIO_PCI_QUEUE_NUM
:
380 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
382 case VIRTIO_PCI_QUEUE_SEL
:
383 ret
= vdev
->queue_sel
;
385 case VIRTIO_PCI_STATUS
:
389 /* reading from the ISR also clears it. */
392 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
394 case VIRTIO_MSI_CONFIG_VECTOR
:
395 ret
= vdev
->config_vector
;
397 case VIRTIO_MSI_QUEUE_VECTOR
:
398 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
407 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
409 VirtIOPCIProxy
*proxy
= opaque
;
410 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
412 return virtio_ioport_read(proxy
, addr
);
414 return virtio_config_readb(proxy
->vdev
, addr
);
417 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
419 VirtIOPCIProxy
*proxy
= opaque
;
420 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
423 return virtio_ioport_read(proxy
, addr
);
425 val
= virtio_config_readw(proxy
->vdev
, addr
);
426 if (virtio_is_big_endian()) {
428 * virtio is odd, ioports are LE but config space is target native
429 * endian. However, in qemu, all PIO is LE, so we need to re-swap
437 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
439 VirtIOPCIProxy
*proxy
= opaque
;
440 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
443 return virtio_ioport_read(proxy
, addr
);
445 val
= virtio_config_readl(proxy
->vdev
, addr
);
446 if (virtio_is_big_endian()) {
452 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
454 VirtIOPCIProxy
*proxy
= opaque
;
455 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
457 virtio_ioport_write(proxy
, addr
, val
);
461 virtio_config_writeb(proxy
->vdev
, addr
, val
);
464 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
466 VirtIOPCIProxy
*proxy
= opaque
;
467 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
469 virtio_ioport_write(proxy
, addr
, val
);
473 if (virtio_is_big_endian()) {
476 virtio_config_writew(proxy
->vdev
, addr
, val
);
479 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
481 VirtIOPCIProxy
*proxy
= opaque
;
482 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
484 virtio_ioport_write(proxy
, addr
, val
);
488 if (virtio_is_big_endian()) {
491 virtio_config_writel(proxy
->vdev
, addr
, val
);
494 const MemoryRegionPortio virtio_portio
[] = {
495 { 0, 0x10000, 1, .write
= virtio_pci_config_writeb
, },
496 { 0, 0x10000, 2, .write
= virtio_pci_config_writew
, },
497 { 0, 0x10000, 4, .write
= virtio_pci_config_writel
, },
498 { 0, 0x10000, 1, .read
= virtio_pci_config_readb
, },
499 { 0, 0x10000, 2, .read
= virtio_pci_config_readw
, },
500 { 0, 0x10000, 4, .read
= virtio_pci_config_readl
, },
504 static const MemoryRegionOps virtio_pci_config_ops
= {
505 .old_portio
= virtio_portio
,
506 .endianness
= DEVICE_LITTLE_ENDIAN
,
509 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
510 uint32_t val
, int len
)
512 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
514 pci_default_write_config(pci_dev
, address
, val
, len
);
516 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
517 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
518 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
519 virtio_pci_stop_ioeventfd(proxy
);
520 virtio_set_status(proxy
->vdev
,
521 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
524 msix_write_config(pci_dev
, address
, val
, len
);
527 static unsigned virtio_pci_get_features(void *opaque
)
529 VirtIOPCIProxy
*proxy
= opaque
;
530 return proxy
->host_features
;
533 static void virtio_pci_guest_notifier_read(void *opaque
)
535 VirtQueue
*vq
= opaque
;
536 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
537 if (event_notifier_test_and_clear(n
)) {
542 static int virtio_pci_set_guest_notifier(void *opaque
, int n
, bool assign
)
544 VirtIOPCIProxy
*proxy
= opaque
;
545 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
546 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
549 int r
= event_notifier_init(notifier
, 0);
553 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
554 virtio_pci_guest_notifier_read
, NULL
, vq
);
556 qemu_set_fd_handler(event_notifier_get_fd(notifier
),
558 event_notifier_cleanup(notifier
);
564 static bool virtio_pci_query_guest_notifiers(void *opaque
)
566 VirtIOPCIProxy
*proxy
= opaque
;
567 return msix_enabled(&proxy
->pci_dev
);
570 static int virtio_pci_set_guest_notifiers(void *opaque
, bool assign
)
572 VirtIOPCIProxy
*proxy
= opaque
;
573 VirtIODevice
*vdev
= proxy
->vdev
;
576 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
577 if (!virtio_queue_get_num(vdev
, n
)) {
581 r
= virtio_pci_set_guest_notifier(opaque
, n
, assign
);
590 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
592 virtio_pci_set_guest_notifier(opaque
, n
, !assign
);
597 static int virtio_pci_set_host_notifier(void *opaque
, int n
, bool assign
)
599 VirtIOPCIProxy
*proxy
= opaque
;
601 /* Stop using ioeventfd for virtqueue kick if the device starts using host
602 * notifiers. This makes it easy to avoid stepping on each others' toes.
604 proxy
->ioeventfd_disabled
= assign
;
606 virtio_pci_stop_ioeventfd(proxy
);
608 /* We don't need to start here: it's not needed because backend
609 * currently only stops on status change away from ok,
610 * reset, vmstop and such. If we do add code to start here,
611 * need to check vmstate, device state etc. */
612 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
);
615 static void virtio_pci_vmstate_change(void *opaque
, bool running
)
617 VirtIOPCIProxy
*proxy
= opaque
;
620 /* Try to find out if the guest has bus master disabled, but is
621 in ready state. Then we have a buggy guest OS. */
622 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
623 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
624 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
626 virtio_pci_start_ioeventfd(proxy
);
628 virtio_pci_stop_ioeventfd(proxy
);
632 static const VirtIOBindings virtio_pci_bindings
= {
633 .notify
= virtio_pci_notify
,
634 .save_config
= virtio_pci_save_config
,
635 .load_config
= virtio_pci_load_config
,
636 .save_queue
= virtio_pci_save_queue
,
637 .load_queue
= virtio_pci_load_queue
,
638 .get_features
= virtio_pci_get_features
,
639 .query_guest_notifiers
= virtio_pci_query_guest_notifiers
,
640 .set_host_notifier
= virtio_pci_set_host_notifier
,
641 .set_guest_notifiers
= virtio_pci_set_guest_notifiers
,
642 .vmstate_change
= virtio_pci_vmstate_change
,
645 void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
)
652 config
= proxy
->pci_dev
.config
;
654 if (proxy
->class_code
) {
655 pci_config_set_class(config
, proxy
->class_code
);
657 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
658 pci_get_word(config
+ PCI_VENDOR_ID
));
659 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, vdev
->device_id
);
660 config
[PCI_INTERRUPT_PIN
] = 1;
662 memory_region_init(&proxy
->msix_bar
, "virtio-msix", 4096);
663 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
,
664 &proxy
->msix_bar
, 1, 0)) {
665 pci_register_bar(&proxy
->pci_dev
, 1, PCI_BASE_ADDRESS_SPACE_MEMORY
,
670 proxy
->pci_dev
.config_write
= virtio_write_config
;
672 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
674 size
= 1 << qemu_fls(size
);
676 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
678 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
681 if (!kvm_has_many_ioeventfds()) {
682 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
685 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
686 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
687 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
688 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
691 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
693 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
696 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
697 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
698 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
700 vdev
= virtio_blk_init(&pci_dev
->qdev
, &proxy
->block
,
701 &proxy
->block_serial
);
705 vdev
->nvectors
= proxy
->nvectors
;
706 virtio_init_pci(proxy
, vdev
);
707 /* make the actual value visible */
708 proxy
->nvectors
= vdev
->nvectors
;
712 static int virtio_exit_pci(PCIDevice
*pci_dev
)
714 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
717 memory_region_destroy(&proxy
->bar
);
718 r
= msix_uninit(pci_dev
, &proxy
->msix_bar
);
719 memory_region_destroy(&proxy
->msix_bar
);
723 static int virtio_blk_exit_pci(PCIDevice
*pci_dev
)
725 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
727 virtio_pci_stop_ioeventfd(proxy
);
728 virtio_blk_exit(proxy
->vdev
);
729 blockdev_mark_auto_del(proxy
->block
.bs
);
730 return virtio_exit_pci(pci_dev
);
733 static int virtio_serial_init_pci(PCIDevice
*pci_dev
)
735 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
738 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
739 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
740 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
741 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
743 vdev
= virtio_serial_init(&pci_dev
->qdev
, &proxy
->serial
);
747 vdev
->nvectors
= proxy
->nvectors
== DEV_NVECTORS_UNSPECIFIED
748 ? proxy
->serial
.max_virtserial_ports
+ 1
750 virtio_init_pci(proxy
, vdev
);
751 proxy
->nvectors
= vdev
->nvectors
;
755 static int virtio_serial_exit_pci(PCIDevice
*pci_dev
)
757 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
759 virtio_pci_stop_ioeventfd(proxy
);
760 virtio_serial_exit(proxy
->vdev
);
761 return virtio_exit_pci(pci_dev
);
764 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
766 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
769 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
, &proxy
->net
);
771 vdev
->nvectors
= proxy
->nvectors
;
772 virtio_init_pci(proxy
, vdev
);
774 /* make the actual value visible */
775 proxy
->nvectors
= vdev
->nvectors
;
779 static int virtio_net_exit_pci(PCIDevice
*pci_dev
)
781 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
783 virtio_pci_stop_ioeventfd(proxy
);
784 virtio_net_exit(proxy
->vdev
);
785 return virtio_exit_pci(pci_dev
);
788 static int virtio_balloon_init_pci(PCIDevice
*pci_dev
)
790 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
793 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
797 virtio_init_pci(proxy
, vdev
);
801 static int virtio_balloon_exit_pci(PCIDevice
*pci_dev
)
803 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
805 virtio_pci_stop_ioeventfd(proxy
);
806 virtio_balloon_exit(proxy
->vdev
);
807 return virtio_exit_pci(pci_dev
);
810 static Property virtio_blk_properties
[] = {
811 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
812 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy
, block
),
813 DEFINE_PROP_STRING("serial", VirtIOPCIProxy
, block_serial
),
814 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
815 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
816 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
817 DEFINE_PROP_END_OF_LIST(),
820 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
822 DeviceClass
*dc
= DEVICE_CLASS(klass
);
823 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
825 k
->init
= virtio_blk_init_pci
;
826 k
->exit
= virtio_blk_exit_pci
;
827 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
828 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
829 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
830 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
831 dc
->reset
= virtio_pci_reset
;
832 dc
->props
= virtio_blk_properties
;
835 static TypeInfo virtio_blk_info
= {
836 .name
= "virtio-blk-pci",
837 .parent
= TYPE_PCI_DEVICE
,
838 .instance_size
= sizeof(VirtIOPCIProxy
),
839 .class_init
= virtio_blk_class_init
,
842 static Property virtio_net_properties
[] = {
843 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
844 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
845 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
846 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
847 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy
, net
.txtimer
, TX_TIMER_INTERVAL
),
848 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy
, net
.txburst
, TX_BURST
),
849 DEFINE_PROP_STRING("tx", VirtIOPCIProxy
, net
.tx
),
850 DEFINE_PROP_END_OF_LIST(),
853 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
855 DeviceClass
*dc
= DEVICE_CLASS(klass
);
856 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
858 k
->init
= virtio_net_init_pci
;
859 k
->exit
= virtio_net_exit_pci
;
860 k
->romfile
= "pxe-virtio.rom";
861 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
862 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
863 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
864 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
865 dc
->reset
= virtio_pci_reset
;
866 dc
->props
= virtio_net_properties
;
869 static TypeInfo virtio_net_info
= {
870 .name
= "virtio-net-pci",
871 .parent
= TYPE_PCI_DEVICE
,
872 .instance_size
= sizeof(VirtIOPCIProxy
),
873 .class_init
= virtio_net_class_init
,
876 static Property virtio_serial_properties
[] = {
877 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
878 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, DEV_NVECTORS_UNSPECIFIED
),
879 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
880 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
881 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
, serial
.max_virtserial_ports
, 31),
882 DEFINE_PROP_END_OF_LIST(),
885 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
887 DeviceClass
*dc
= DEVICE_CLASS(klass
);
888 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
890 k
->init
= virtio_serial_init_pci
;
891 k
->exit
= virtio_serial_exit_pci
;
892 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
893 k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
894 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
895 k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
896 dc
->reset
= virtio_pci_reset
;
897 dc
->props
= virtio_serial_properties
;
900 static TypeInfo virtio_serial_info
= {
901 .name
= "virtio-serial-pci",
902 .parent
= TYPE_PCI_DEVICE
,
903 .instance_size
= sizeof(VirtIOPCIProxy
),
904 .class_init
= virtio_serial_class_init
,
907 static Property virtio_balloon_properties
[] = {
908 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
909 DEFINE_PROP_END_OF_LIST(),
912 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
914 DeviceClass
*dc
= DEVICE_CLASS(klass
);
915 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
917 k
->init
= virtio_balloon_init_pci
;
918 k
->exit
= virtio_balloon_exit_pci
;
919 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
920 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
921 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
922 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
923 dc
->reset
= virtio_pci_reset
;
924 dc
->props
= virtio_balloon_properties
;
927 static TypeInfo virtio_balloon_info
= {
928 .name
= "virtio-balloon-pci",
929 .parent
= TYPE_PCI_DEVICE
,
930 .instance_size
= sizeof(VirtIOPCIProxy
),
931 .class_init
= virtio_balloon_class_init
,
934 static int virtio_scsi_init_pci(PCIDevice
*pci_dev
)
936 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
939 vdev
= virtio_scsi_init(&pci_dev
->qdev
, &proxy
->scsi
);
944 vdev
->nvectors
= proxy
->nvectors
;
945 virtio_init_pci(proxy
, vdev
);
947 /* make the actual value visible */
948 proxy
->nvectors
= vdev
->nvectors
;
952 static int virtio_scsi_exit_pci(PCIDevice
*pci_dev
)
954 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
956 virtio_scsi_exit(proxy
->vdev
);
957 return virtio_exit_pci(pci_dev
);
960 static Property virtio_scsi_properties
[] = {
961 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
962 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy
, host_features
, scsi
),
963 DEFINE_PROP_END_OF_LIST(),
966 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
968 DeviceClass
*dc
= DEVICE_CLASS(klass
);
969 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
971 k
->init
= virtio_scsi_init_pci
;
972 k
->exit
= virtio_scsi_exit_pci
;
973 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
974 k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
976 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
977 dc
->reset
= virtio_pci_reset
;
978 dc
->props
= virtio_scsi_properties
;
981 static TypeInfo virtio_scsi_info
= {
982 .name
= "virtio-scsi-pci",
983 .parent
= TYPE_PCI_DEVICE
,
984 .instance_size
= sizeof(VirtIOPCIProxy
),
985 .class_init
= virtio_scsi_class_init
,
988 static void virtio_pci_register_types(void)
990 type_register_static(&virtio_blk_info
);
991 type_register_static(&virtio_net_info
);
992 type_register_static(&virtio_serial_info
);
993 type_register_static(&virtio_balloon_info
);
994 type_register_static(&virtio_scsi_info
);
997 type_init(virtio_pci_register_types
)