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 static 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 if (proxy
->class_code
!= PCI_CLASS_OTHERS
&&
794 proxy
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
795 proxy
->class_code
= PCI_CLASS_OTHERS
;
798 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
802 virtio_init_pci(proxy
, vdev
);
806 static int virtio_balloon_exit_pci(PCIDevice
*pci_dev
)
808 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
810 virtio_pci_stop_ioeventfd(proxy
);
811 virtio_balloon_exit(proxy
->vdev
);
812 return virtio_exit_pci(pci_dev
);
815 static Property virtio_blk_properties
[] = {
816 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
817 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy
, block
),
818 DEFINE_PROP_STRING("serial", VirtIOPCIProxy
, block_serial
),
819 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
820 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
821 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
822 DEFINE_PROP_END_OF_LIST(),
825 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
827 DeviceClass
*dc
= DEVICE_CLASS(klass
);
828 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
830 k
->init
= virtio_blk_init_pci
;
831 k
->exit
= virtio_blk_exit_pci
;
832 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
833 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
834 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
835 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
836 dc
->reset
= virtio_pci_reset
;
837 dc
->props
= virtio_blk_properties
;
840 static TypeInfo virtio_blk_info
= {
841 .name
= "virtio-blk-pci",
842 .parent
= TYPE_PCI_DEVICE
,
843 .instance_size
= sizeof(VirtIOPCIProxy
),
844 .class_init
= virtio_blk_class_init
,
847 static Property virtio_net_properties
[] = {
848 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
849 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
850 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
851 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
852 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy
, net
.txtimer
, TX_TIMER_INTERVAL
),
853 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy
, net
.txburst
, TX_BURST
),
854 DEFINE_PROP_STRING("tx", VirtIOPCIProxy
, net
.tx
),
855 DEFINE_PROP_END_OF_LIST(),
858 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
860 DeviceClass
*dc
= DEVICE_CLASS(klass
);
861 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
863 k
->init
= virtio_net_init_pci
;
864 k
->exit
= virtio_net_exit_pci
;
865 k
->romfile
= "pxe-virtio.rom";
866 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
867 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
868 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
869 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
870 dc
->reset
= virtio_pci_reset
;
871 dc
->props
= virtio_net_properties
;
874 static TypeInfo virtio_net_info
= {
875 .name
= "virtio-net-pci",
876 .parent
= TYPE_PCI_DEVICE
,
877 .instance_size
= sizeof(VirtIOPCIProxy
),
878 .class_init
= virtio_net_class_init
,
881 static Property virtio_serial_properties
[] = {
882 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
883 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, DEV_NVECTORS_UNSPECIFIED
),
884 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
885 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
886 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
, serial
.max_virtserial_ports
, 31),
887 DEFINE_PROP_END_OF_LIST(),
890 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
892 DeviceClass
*dc
= DEVICE_CLASS(klass
);
893 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
895 k
->init
= virtio_serial_init_pci
;
896 k
->exit
= virtio_serial_exit_pci
;
897 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
898 k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
899 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
900 k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
901 dc
->reset
= virtio_pci_reset
;
902 dc
->props
= virtio_serial_properties
;
905 static TypeInfo virtio_serial_info
= {
906 .name
= "virtio-serial-pci",
907 .parent
= TYPE_PCI_DEVICE
,
908 .instance_size
= sizeof(VirtIOPCIProxy
),
909 .class_init
= virtio_serial_class_init
,
912 static Property virtio_balloon_properties
[] = {
913 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
914 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
915 DEFINE_PROP_END_OF_LIST(),
918 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
920 DeviceClass
*dc
= DEVICE_CLASS(klass
);
921 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
923 k
->init
= virtio_balloon_init_pci
;
924 k
->exit
= virtio_balloon_exit_pci
;
925 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
926 k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
927 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
928 k
->class_id
= PCI_CLASS_OTHERS
;
929 dc
->reset
= virtio_pci_reset
;
930 dc
->props
= virtio_balloon_properties
;
933 static TypeInfo virtio_balloon_info
= {
934 .name
= "virtio-balloon-pci",
935 .parent
= TYPE_PCI_DEVICE
,
936 .instance_size
= sizeof(VirtIOPCIProxy
),
937 .class_init
= virtio_balloon_class_init
,
940 static int virtio_scsi_init_pci(PCIDevice
*pci_dev
)
942 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
945 vdev
= virtio_scsi_init(&pci_dev
->qdev
, &proxy
->scsi
);
950 vdev
->nvectors
= proxy
->nvectors
;
951 virtio_init_pci(proxy
, vdev
);
953 /* make the actual value visible */
954 proxy
->nvectors
= vdev
->nvectors
;
958 static int virtio_scsi_exit_pci(PCIDevice
*pci_dev
)
960 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
962 virtio_scsi_exit(proxy
->vdev
);
963 return virtio_exit_pci(pci_dev
);
966 static Property virtio_scsi_properties
[] = {
967 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
968 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy
, host_features
, scsi
),
969 DEFINE_PROP_END_OF_LIST(),
972 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
974 DeviceClass
*dc
= DEVICE_CLASS(klass
);
975 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
977 k
->init
= virtio_scsi_init_pci
;
978 k
->exit
= virtio_scsi_exit_pci
;
979 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
980 k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
982 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
983 dc
->reset
= virtio_pci_reset
;
984 dc
->props
= virtio_scsi_properties
;
987 static TypeInfo virtio_scsi_info
= {
988 .name
= "virtio-scsi-pci",
989 .parent
= TYPE_PCI_DEVICE
,
990 .instance_size
= sizeof(VirtIOPCIProxy
),
991 .class_init
= virtio_scsi_class_init
,
994 static void virtio_pci_register_types(void)
996 type_register_static(&virtio_blk_info
);
997 type_register_static(&virtio_net_info
);
998 type_register_static(&virtio_serial_info
);
999 type_register_static(&virtio_balloon_info
);
1000 type_register_static(&virtio_scsi_info
);
1003 type_init(virtio_pci_register_types
)