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.
20 #include "standard-headers/linux/virtio_pci.h"
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-blk.h"
23 #include "hw/virtio/virtio-net.h"
24 #include "hw/virtio/virtio-serial.h"
25 #include "hw/virtio/virtio-scsi.h"
26 #include "hw/virtio/virtio-balloon.h"
27 #include "hw/virtio/virtio-input.h"
28 #include "hw/pci/pci.h"
29 #include "qemu/error-report.h"
30 #include "hw/pci/msi.h"
31 #include "hw/pci/msix.h"
32 #include "hw/loader.h"
33 #include "sysemu/kvm.h"
34 #include "sysemu/block-backend.h"
35 #include "virtio-pci.h"
36 #include "qemu/range.h"
37 #include "hw/virtio/virtio-bus.h"
38 #include "qapi/visitor.h"
40 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
42 #undef VIRTIO_PCI_CONFIG
44 /* The remaining space is defined by each driver as the per-driver
45 * configuration space */
46 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
48 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
52 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
53 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
55 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
58 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
59 * be careful and test performance if you change this.
61 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
63 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
66 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
68 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
70 if (msix_enabled(&proxy
->pci_dev
))
71 msix_notify(&proxy
->pci_dev
, vector
);
73 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
74 pci_set_irq(&proxy
->pci_dev
, vdev
->isr
& 1);
78 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
80 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
81 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
83 pci_device_save(&proxy
->pci_dev
, f
);
84 msix_save(&proxy
->pci_dev
, f
);
85 if (msix_present(&proxy
->pci_dev
))
86 qemu_put_be16(f
, vdev
->config_vector
);
89 static void virtio_pci_load_modern_queue_state(VirtIOPCIQueue
*vq
,
92 vq
->num
= qemu_get_be16(f
);
93 vq
->enabled
= qemu_get_be16(f
);
94 vq
->desc
[0] = qemu_get_be32(f
);
95 vq
->desc
[1] = qemu_get_be32(f
);
96 vq
->avail
[0] = qemu_get_be32(f
);
97 vq
->avail
[1] = qemu_get_be32(f
);
98 vq
->used
[0] = qemu_get_be32(f
);
99 vq
->used
[1] = qemu_get_be32(f
);
102 static bool virtio_pci_has_extra_state(DeviceState
*d
)
104 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
106 return proxy
->flags
& VIRTIO_PCI_FLAG_MIGRATE_EXTRA
;
109 static int get_virtio_pci_modern_state(QEMUFile
*f
, void *pv
, size_t size
)
111 VirtIOPCIProxy
*proxy
= pv
;
114 proxy
->dfselect
= qemu_get_be32(f
);
115 proxy
->gfselect
= qemu_get_be32(f
);
116 proxy
->guest_features
[0] = qemu_get_be32(f
);
117 proxy
->guest_features
[1] = qemu_get_be32(f
);
118 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
119 virtio_pci_load_modern_queue_state(&proxy
->vqs
[i
], f
);
125 static void virtio_pci_save_modern_queue_state(VirtIOPCIQueue
*vq
,
128 qemu_put_be16(f
, vq
->num
);
129 qemu_put_be16(f
, vq
->enabled
);
130 qemu_put_be32(f
, vq
->desc
[0]);
131 qemu_put_be32(f
, vq
->desc
[1]);
132 qemu_put_be32(f
, vq
->avail
[0]);
133 qemu_put_be32(f
, vq
->avail
[1]);
134 qemu_put_be32(f
, vq
->used
[0]);
135 qemu_put_be32(f
, vq
->used
[1]);
138 static void put_virtio_pci_modern_state(QEMUFile
*f
, void *pv
, size_t size
)
140 VirtIOPCIProxy
*proxy
= pv
;
143 qemu_put_be32(f
, proxy
->dfselect
);
144 qemu_put_be32(f
, proxy
->gfselect
);
145 qemu_put_be32(f
, proxy
->guest_features
[0]);
146 qemu_put_be32(f
, proxy
->guest_features
[1]);
147 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
148 virtio_pci_save_modern_queue_state(&proxy
->vqs
[i
], f
);
152 static const VMStateInfo vmstate_info_virtio_pci_modern_state
= {
153 .name
= "virtqueue_state",
154 .get
= get_virtio_pci_modern_state
,
155 .put
= put_virtio_pci_modern_state
,
158 static bool virtio_pci_modern_state_needed(void *opaque
)
160 VirtIOPCIProxy
*proxy
= opaque
;
162 return !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
165 static const VMStateDescription vmstate_virtio_pci_modern_state
= {
166 .name
= "virtio_pci/modern_state",
168 .minimum_version_id
= 1,
169 .needed
= &virtio_pci_modern_state_needed
,
170 .fields
= (VMStateField
[]) {
172 .name
= "modern_state",
174 .field_exists
= NULL
,
176 .info
= &vmstate_info_virtio_pci_modern_state
,
180 VMSTATE_END_OF_LIST()
184 static const VMStateDescription vmstate_virtio_pci
= {
185 .name
= "virtio_pci",
187 .minimum_version_id
= 1,
188 .minimum_version_id_old
= 1,
189 .fields
= (VMStateField
[]) {
190 VMSTATE_END_OF_LIST()
192 .subsections
= (const VMStateDescription
*[]) {
193 &vmstate_virtio_pci_modern_state
,
198 static void virtio_pci_save_extra_state(DeviceState
*d
, QEMUFile
*f
)
200 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
202 vmstate_save_state(f
, &vmstate_virtio_pci
, proxy
, NULL
);
205 static int virtio_pci_load_extra_state(DeviceState
*d
, QEMUFile
*f
)
207 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
209 return vmstate_load_state(f
, &vmstate_virtio_pci
, proxy
, 1);
212 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
214 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
215 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
217 if (msix_present(&proxy
->pci_dev
))
218 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
221 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
223 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
224 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
227 ret
= pci_device_load(&proxy
->pci_dev
, f
);
231 msix_unuse_all_vectors(&proxy
->pci_dev
);
232 msix_load(&proxy
->pci_dev
, f
);
233 if (msix_present(&proxy
->pci_dev
)) {
234 qemu_get_be16s(f
, &vdev
->config_vector
);
236 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
238 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
239 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
244 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
246 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
247 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
250 if (msix_present(&proxy
->pci_dev
)) {
251 qemu_get_be16s(f
, &vector
);
253 vector
= VIRTIO_NO_VECTOR
;
255 virtio_queue_set_vector(vdev
, n
, vector
);
256 if (vector
!= VIRTIO_NO_VECTOR
) {
257 return msix_vector_use(&proxy
->pci_dev
, vector
);
263 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
265 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
266 int n
, bool assign
, bool set_handler
)
268 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
269 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
270 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
271 bool legacy
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_LEGACY
);
272 bool modern
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
273 MemoryRegion
*modern_mr
= &proxy
->notify
.mr
;
274 MemoryRegion
*legacy_mr
= &proxy
->bar
;
275 hwaddr modern_addr
= QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
*
276 virtio_get_queue_index(vq
);
277 hwaddr legacy_addr
= VIRTIO_PCI_QUEUE_NOTIFY
;
281 r
= event_notifier_init(notifier
, 1);
283 error_report("%s: unable to init event notifier: %d",
287 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
289 memory_region_add_eventfd(modern_mr
, modern_addr
, 2,
293 memory_region_add_eventfd(legacy_mr
, legacy_addr
, 2,
298 memory_region_del_eventfd(modern_mr
, modern_addr
, 2,
302 memory_region_del_eventfd(legacy_mr
, legacy_addr
, 2,
305 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
306 event_notifier_cleanup(notifier
);
311 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
313 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
316 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
317 proxy
->ioeventfd_disabled
||
318 proxy
->ioeventfd_started
) {
322 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
323 if (!virtio_queue_get_num(vdev
, n
)) {
327 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
332 proxy
->ioeventfd_started
= true;
337 if (!virtio_queue_get_num(vdev
, n
)) {
341 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
344 proxy
->ioeventfd_started
= false;
345 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
348 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
350 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
354 if (!proxy
->ioeventfd_started
) {
358 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
359 if (!virtio_queue_get_num(vdev
, n
)) {
363 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
366 proxy
->ioeventfd_started
= false;
369 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
371 VirtIOPCIProxy
*proxy
= opaque
;
372 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
376 case VIRTIO_PCI_GUEST_FEATURES
:
377 /* Guest does not negotiate properly? We have to assume nothing. */
378 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
379 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
381 virtio_set_features(vdev
, val
);
383 case VIRTIO_PCI_QUEUE_PFN
:
384 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
386 virtio_pci_stop_ioeventfd(proxy
);
388 msix_unuse_all_vectors(&proxy
->pci_dev
);
391 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
393 case VIRTIO_PCI_QUEUE_SEL
:
394 if (val
< VIRTIO_QUEUE_MAX
)
395 vdev
->queue_sel
= val
;
397 case VIRTIO_PCI_QUEUE_NOTIFY
:
398 if (val
< VIRTIO_QUEUE_MAX
) {
399 virtio_queue_notify(vdev
, val
);
402 case VIRTIO_PCI_STATUS
:
403 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
404 virtio_pci_stop_ioeventfd(proxy
);
407 virtio_set_status(vdev
, val
& 0xFF);
409 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
410 virtio_pci_start_ioeventfd(proxy
);
413 if (vdev
->status
== 0) {
415 msix_unuse_all_vectors(&proxy
->pci_dev
);
418 /* Linux before 2.6.34 drives the device without enabling
419 the PCI device bus master bit. Enable it automatically
420 for the guest. This is a PCI spec violation but so is
421 initiating DMA with bus master bit clear. */
422 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
423 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
424 proxy
->pci_dev
.config
[PCI_COMMAND
] |
425 PCI_COMMAND_MASTER
, 1);
428 case VIRTIO_MSI_CONFIG_VECTOR
:
429 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
430 /* Make it possible for guest to discover an error took place. */
431 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
432 val
= VIRTIO_NO_VECTOR
;
433 vdev
->config_vector
= val
;
435 case VIRTIO_MSI_QUEUE_VECTOR
:
436 msix_vector_unuse(&proxy
->pci_dev
,
437 virtio_queue_vector(vdev
, vdev
->queue_sel
));
438 /* Make it possible for guest to discover an error took place. */
439 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
440 val
= VIRTIO_NO_VECTOR
;
441 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
444 error_report("%s: unexpected address 0x%x value 0x%x",
445 __func__
, addr
, val
);
450 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
452 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
453 uint32_t ret
= 0xFFFFFFFF;
456 case VIRTIO_PCI_HOST_FEATURES
:
457 ret
= vdev
->host_features
;
459 case VIRTIO_PCI_GUEST_FEATURES
:
460 ret
= vdev
->guest_features
;
462 case VIRTIO_PCI_QUEUE_PFN
:
463 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
464 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
466 case VIRTIO_PCI_QUEUE_NUM
:
467 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
469 case VIRTIO_PCI_QUEUE_SEL
:
470 ret
= vdev
->queue_sel
;
472 case VIRTIO_PCI_STATUS
:
476 /* reading from the ISR also clears it. */
479 pci_irq_deassert(&proxy
->pci_dev
);
481 case VIRTIO_MSI_CONFIG_VECTOR
:
482 ret
= vdev
->config_vector
;
484 case VIRTIO_MSI_QUEUE_VECTOR
:
485 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
494 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
497 VirtIOPCIProxy
*proxy
= opaque
;
498 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
499 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
502 return virtio_ioport_read(proxy
, addr
);
508 val
= virtio_config_readb(vdev
, addr
);
511 val
= virtio_config_readw(vdev
, addr
);
512 if (virtio_is_big_endian(vdev
)) {
517 val
= virtio_config_readl(vdev
, addr
);
518 if (virtio_is_big_endian(vdev
)) {
526 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
527 uint64_t val
, unsigned size
)
529 VirtIOPCIProxy
*proxy
= opaque
;
530 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
531 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
533 virtio_ioport_write(proxy
, addr
, val
);
538 * Virtio-PCI is odd. Ioports are LE but config space is target native
543 virtio_config_writeb(vdev
, addr
, val
);
546 if (virtio_is_big_endian(vdev
)) {
549 virtio_config_writew(vdev
, addr
, val
);
552 if (virtio_is_big_endian(vdev
)) {
555 virtio_config_writel(vdev
, addr
, val
);
560 static const MemoryRegionOps virtio_pci_config_ops
= {
561 .read
= virtio_pci_config_read
,
562 .write
= virtio_pci_config_write
,
564 .min_access_size
= 1,
565 .max_access_size
= 4,
567 .endianness
= DEVICE_LITTLE_ENDIAN
,
570 /* Below are generic functions to do memcpy from/to an address space,
571 * without byteswaps, with input validation.
573 * As regular address_space_* APIs all do some kind of byteswap at least for
574 * some host/target combinations, we are forced to explicitly convert to a
575 * known-endianness integer value.
576 * It doesn't really matter which endian format to go through, so the code
577 * below selects the endian that causes the least amount of work on the given
580 * Note: host pointer must be aligned.
583 void virtio_address_space_write(AddressSpace
*as
, hwaddr addr
,
584 const uint8_t *buf
, int len
)
588 /* address_space_* APIs assume an aligned address.
589 * As address is under guest control, handle illegal values.
593 /* Make sure caller aligned buf properly */
594 assert(!(((uintptr_t)buf
) & (len
- 1)));
598 val
= pci_get_byte(buf
);
599 address_space_stb(as
, addr
, val
, MEMTXATTRS_UNSPECIFIED
, NULL
);
602 val
= pci_get_word(buf
);
603 address_space_stw_le(as
, addr
, val
, MEMTXATTRS_UNSPECIFIED
, NULL
);
606 val
= pci_get_long(buf
);
607 address_space_stl_le(as
, addr
, val
, MEMTXATTRS_UNSPECIFIED
, NULL
);
610 /* As length is under guest control, handle illegal values. */
616 virtio_address_space_read(AddressSpace
*as
, hwaddr addr
, uint8_t *buf
, int len
)
620 /* address_space_* APIs assume an aligned address.
621 * As address is under guest control, handle illegal values.
625 /* Make sure caller aligned buf properly */
626 assert(!(((uintptr_t)buf
) & (len
- 1)));
630 val
= address_space_ldub(as
, addr
, MEMTXATTRS_UNSPECIFIED
, NULL
);
631 pci_set_byte(buf
, val
);
634 val
= address_space_lduw_le(as
, addr
, MEMTXATTRS_UNSPECIFIED
, NULL
);
635 pci_set_word(buf
, val
);
638 val
= address_space_ldl_le(as
, addr
, MEMTXATTRS_UNSPECIFIED
, NULL
);
639 pci_set_long(buf
, val
);
642 /* As length is under guest control, handle illegal values. */
647 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
648 uint32_t val
, int len
)
650 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
651 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
652 struct virtio_pci_cfg_cap
*cfg
;
654 pci_default_write_config(pci_dev
, address
, val
, len
);
656 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
657 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
658 virtio_pci_stop_ioeventfd(proxy
);
659 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
662 if (proxy
->config_cap
&&
663 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
665 sizeof cfg
->pci_cfg_data
)) {
669 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
670 off
= le32_to_cpu(cfg
->cap
.offset
);
671 len
= le32_to_cpu(cfg
->cap
.length
);
673 if (len
== 1 || len
== 2 || len
== 4) {
674 assert(len
<= sizeof cfg
->pci_cfg_data
);
675 virtio_address_space_write(&proxy
->modern_as
, off
,
676 cfg
->pci_cfg_data
, len
);
681 static uint32_t virtio_read_config(PCIDevice
*pci_dev
,
682 uint32_t address
, int len
)
684 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
685 struct virtio_pci_cfg_cap
*cfg
;
687 if (proxy
->config_cap
&&
688 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
690 sizeof cfg
->pci_cfg_data
)) {
694 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
695 off
= le32_to_cpu(cfg
->cap
.offset
);
696 len
= le32_to_cpu(cfg
->cap
.length
);
698 if (len
== 1 || len
== 2 || len
== 4) {
699 assert(len
<= sizeof cfg
->pci_cfg_data
);
700 virtio_address_space_read(&proxy
->modern_as
, off
,
701 cfg
->pci_cfg_data
, len
);
705 return pci_default_read_config(pci_dev
, address
, len
);
708 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
709 unsigned int queue_no
,
713 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
716 if (irqfd
->users
== 0) {
717 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
, &proxy
->pci_dev
);
727 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
730 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
731 if (--irqfd
->users
== 0) {
732 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
736 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
737 unsigned int queue_no
,
740 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
741 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
742 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
743 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
745 ret
= kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, irqfd
->virq
);
749 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
750 unsigned int queue_no
,
753 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
754 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
755 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
756 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
759 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, irqfd
->virq
);
763 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
765 PCIDevice
*dev
= &proxy
->pci_dev
;
766 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
767 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
772 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
773 if (!virtio_queue_get_num(vdev
, queue_no
)) {
776 vector
= virtio_queue_vector(vdev
, queue_no
);
777 if (vector
>= msix_nr_vectors_allocated(dev
)) {
780 msg
= msix_get_message(dev
, vector
);
781 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
785 /* If guest supports masking, set up irqfd now.
786 * Otherwise, delay until unmasked in the frontend.
788 if (k
->guest_notifier_mask
) {
789 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
791 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
799 while (--queue_no
>= 0) {
800 vector
= virtio_queue_vector(vdev
, queue_no
);
801 if (vector
>= msix_nr_vectors_allocated(dev
)) {
804 if (k
->guest_notifier_mask
) {
805 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
807 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
812 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
814 PCIDevice
*dev
= &proxy
->pci_dev
;
815 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
818 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
820 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
821 if (!virtio_queue_get_num(vdev
, queue_no
)) {
824 vector
= virtio_queue_vector(vdev
, queue_no
);
825 if (vector
>= msix_nr_vectors_allocated(dev
)) {
828 /* If guest supports masking, clean up irqfd now.
829 * Otherwise, it was cleaned when masked in the frontend.
831 if (k
->guest_notifier_mask
) {
832 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
834 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
838 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
839 unsigned int queue_no
,
843 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
844 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
845 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
846 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
850 if (proxy
->vector_irqfd
) {
851 irqfd
= &proxy
->vector_irqfd
[vector
];
852 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
853 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
,
861 /* If guest supports masking, irqfd is already setup, unmask it.
862 * Otherwise, set it up now.
864 if (k
->guest_notifier_mask
) {
865 k
->guest_notifier_mask(vdev
, queue_no
, false);
866 /* Test after unmasking to avoid losing events. */
867 if (k
->guest_notifier_pending
&&
868 k
->guest_notifier_pending(vdev
, queue_no
)) {
869 event_notifier_set(n
);
872 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
877 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
878 unsigned int queue_no
,
881 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
882 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
884 /* If guest supports masking, keep irqfd but mask it.
885 * Otherwise, clean it up now.
887 if (k
->guest_notifier_mask
) {
888 k
->guest_notifier_mask(vdev
, queue_no
, true);
890 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
894 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
897 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
898 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
899 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
900 int ret
, index
, unmasked
= 0;
903 index
= virtio_get_queue_index(vq
);
904 if (!virtio_queue_get_num(vdev
, index
)) {
907 if (index
< proxy
->nvqs_with_notifiers
) {
908 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
914 vq
= virtio_vector_next_queue(vq
);
920 vq
= virtio_vector_first_queue(vdev
, vector
);
921 while (vq
&& unmasked
>= 0) {
922 index
= virtio_get_queue_index(vq
);
923 if (index
< proxy
->nvqs_with_notifiers
) {
924 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
927 vq
= virtio_vector_next_queue(vq
);
932 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
934 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
935 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
936 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
940 index
= virtio_get_queue_index(vq
);
941 if (!virtio_queue_get_num(vdev
, index
)) {
944 if (index
< proxy
->nvqs_with_notifiers
) {
945 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
947 vq
= virtio_vector_next_queue(vq
);
951 static void virtio_pci_vector_poll(PCIDevice
*dev
,
952 unsigned int vector_start
,
953 unsigned int vector_end
)
955 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
956 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
957 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
960 EventNotifier
*notifier
;
963 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
964 if (!virtio_queue_get_num(vdev
, queue_no
)) {
967 vector
= virtio_queue_vector(vdev
, queue_no
);
968 if (vector
< vector_start
|| vector
>= vector_end
||
969 !msix_is_masked(dev
, vector
)) {
972 vq
= virtio_get_queue(vdev
, queue_no
);
973 notifier
= virtio_queue_get_guest_notifier(vq
);
974 if (k
->guest_notifier_pending
) {
975 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
976 msix_set_pending(dev
, vector
);
978 } else if (event_notifier_test_and_clear(notifier
)) {
979 msix_set_pending(dev
, vector
);
984 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
987 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
988 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
989 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
990 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
991 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
994 int r
= event_notifier_init(notifier
, 0);
998 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
1000 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
1001 event_notifier_cleanup(notifier
);
1004 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
1005 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
1011 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
1013 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1014 return msix_enabled(&proxy
->pci_dev
);
1017 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
1019 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1020 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1021 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1023 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
1024 kvm_msi_via_irqfd_enabled();
1026 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
1028 /* When deassigning, pass a consistent nvqs value
1029 * to avoid leaking notifiers.
1031 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
1033 proxy
->nvqs_with_notifiers
= nvqs
;
1035 /* Must unset vector notifier while guest notifier is still assigned */
1036 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
1037 msix_unset_vector_notifiers(&proxy
->pci_dev
);
1038 if (proxy
->vector_irqfd
) {
1039 kvm_virtio_pci_vector_release(proxy
, nvqs
);
1040 g_free(proxy
->vector_irqfd
);
1041 proxy
->vector_irqfd
= NULL
;
1045 for (n
= 0; n
< nvqs
; n
++) {
1046 if (!virtio_queue_get_num(vdev
, n
)) {
1050 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
1056 /* Must set vector notifier after guest notifier has been assigned */
1057 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
1059 proxy
->vector_irqfd
=
1060 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
1061 msix_nr_vectors_allocated(&proxy
->pci_dev
));
1062 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
1067 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
1068 virtio_pci_vector_unmask
,
1069 virtio_pci_vector_mask
,
1070 virtio_pci_vector_poll
);
1072 goto notifiers_error
;
1081 kvm_virtio_pci_vector_release(proxy
, nvqs
);
1085 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1088 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
1093 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
1095 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1097 /* Stop using ioeventfd for virtqueue kick if the device starts using host
1098 * notifiers. This makes it easy to avoid stepping on each others' toes.
1100 proxy
->ioeventfd_disabled
= assign
;
1102 virtio_pci_stop_ioeventfd(proxy
);
1104 /* We don't need to start here: it's not needed because backend
1105 * currently only stops on status change away from ok,
1106 * reset, vmstop and such. If we do add code to start here,
1107 * need to check vmstate, device state etc. */
1108 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
1111 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
1113 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1114 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1117 /* Old QEMU versions did not set bus master enable on status write.
1118 * Detect DRIVER set and enable it.
1120 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
1121 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
1122 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
1123 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
1124 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1125 PCI_COMMAND_MASTER
, 1);
1127 virtio_pci_start_ioeventfd(proxy
);
1129 virtio_pci_stop_ioeventfd(proxy
);
1133 #ifdef CONFIG_VIRTFS
1134 static void virtio_9p_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1136 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
1137 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1139 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1140 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1143 static Property virtio_9p_pci_properties
[] = {
1144 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1145 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1146 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1147 DEFINE_PROP_END_OF_LIST(),
1150 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
1152 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1153 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1154 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1156 k
->realize
= virtio_9p_pci_realize
;
1157 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1158 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
1159 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1160 pcidev_k
->class_id
= 0x2;
1161 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1162 dc
->props
= virtio_9p_pci_properties
;
1165 static void virtio_9p_pci_instance_init(Object
*obj
)
1167 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
1169 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1173 static const TypeInfo virtio_9p_pci_info
= {
1174 .name
= TYPE_VIRTIO_9P_PCI
,
1175 .parent
= TYPE_VIRTIO_PCI
,
1176 .instance_size
= sizeof(V9fsPCIState
),
1177 .instance_init
= virtio_9p_pci_instance_init
,
1178 .class_init
= virtio_9p_pci_class_init
,
1180 #endif /* CONFIG_VIRTFS */
1183 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1186 static int virtio_pci_query_nvectors(DeviceState
*d
)
1188 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1190 return proxy
->nvectors
;
1193 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1194 struct virtio_pci_cap
*cap
)
1196 PCIDevice
*dev
= &proxy
->pci_dev
;
1199 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0, cap
->cap_len
);
1202 assert(cap
->cap_len
>= sizeof *cap
);
1203 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1204 cap
->cap_len
- PCI_CAP_FLAGS
);
1209 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1212 VirtIOPCIProxy
*proxy
= opaque
;
1213 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1218 case VIRTIO_PCI_COMMON_DFSELECT
:
1219 val
= proxy
->dfselect
;
1221 case VIRTIO_PCI_COMMON_DF
:
1222 if (proxy
->dfselect
<= 1) {
1223 val
= (vdev
->host_features
& ~VIRTIO_LEGACY_FEATURES
) >>
1224 (32 * proxy
->dfselect
);
1227 case VIRTIO_PCI_COMMON_GFSELECT
:
1228 val
= proxy
->gfselect
;
1230 case VIRTIO_PCI_COMMON_GF
:
1231 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1232 val
= proxy
->guest_features
[proxy
->gfselect
];
1235 case VIRTIO_PCI_COMMON_MSIX
:
1236 val
= vdev
->config_vector
;
1238 case VIRTIO_PCI_COMMON_NUMQ
:
1239 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1240 if (virtio_queue_get_num(vdev
, i
)) {
1245 case VIRTIO_PCI_COMMON_STATUS
:
1248 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1249 val
= vdev
->generation
;
1251 case VIRTIO_PCI_COMMON_Q_SELECT
:
1252 val
= vdev
->queue_sel
;
1254 case VIRTIO_PCI_COMMON_Q_SIZE
:
1255 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1257 case VIRTIO_PCI_COMMON_Q_MSIX
:
1258 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1260 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1261 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1263 case VIRTIO_PCI_COMMON_Q_NOFF
:
1264 /* Simply map queues in order */
1265 val
= vdev
->queue_sel
;
1267 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1268 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1270 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1271 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1273 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1274 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1276 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1277 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1279 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1280 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1282 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1283 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1292 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1293 uint64_t val
, unsigned size
)
1295 VirtIOPCIProxy
*proxy
= opaque
;
1296 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1299 case VIRTIO_PCI_COMMON_DFSELECT
:
1300 proxy
->dfselect
= val
;
1302 case VIRTIO_PCI_COMMON_GFSELECT
:
1303 proxy
->gfselect
= val
;
1305 case VIRTIO_PCI_COMMON_GF
:
1306 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1307 proxy
->guest_features
[proxy
->gfselect
] = val
;
1308 virtio_set_features(vdev
,
1309 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1310 proxy
->guest_features
[0]);
1313 case VIRTIO_PCI_COMMON_MSIX
:
1314 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1315 /* Make it possible for guest to discover an error took place. */
1316 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1317 val
= VIRTIO_NO_VECTOR
;
1319 vdev
->config_vector
= val
;
1321 case VIRTIO_PCI_COMMON_STATUS
:
1322 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1323 virtio_pci_stop_ioeventfd(proxy
);
1326 virtio_set_status(vdev
, val
& 0xFF);
1328 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1329 virtio_pci_start_ioeventfd(proxy
);
1332 if (vdev
->status
== 0) {
1334 msix_unuse_all_vectors(&proxy
->pci_dev
);
1338 case VIRTIO_PCI_COMMON_Q_SELECT
:
1339 if (val
< VIRTIO_QUEUE_MAX
) {
1340 vdev
->queue_sel
= val
;
1343 case VIRTIO_PCI_COMMON_Q_SIZE
:
1344 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1346 case VIRTIO_PCI_COMMON_Q_MSIX
:
1347 msix_vector_unuse(&proxy
->pci_dev
,
1348 virtio_queue_vector(vdev
, vdev
->queue_sel
));
1349 /* Make it possible for guest to discover an error took place. */
1350 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1351 val
= VIRTIO_NO_VECTOR
;
1353 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1355 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1356 /* TODO: need a way to put num back on reset. */
1357 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1358 proxy
->vqs
[vdev
->queue_sel
].num
);
1359 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1360 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1361 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1362 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1363 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1364 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1365 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1367 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1368 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1370 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1371 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1373 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1374 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1376 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1377 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1379 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1380 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1382 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1383 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1391 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1397 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1398 uint64_t val
, unsigned size
)
1400 VirtIODevice
*vdev
= opaque
;
1401 unsigned queue
= addr
/ QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
;
1403 if (queue
< VIRTIO_QUEUE_MAX
) {
1404 virtio_queue_notify(vdev
, queue
);
1408 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1411 VirtIOPCIProxy
*proxy
= opaque
;
1412 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1413 uint64_t val
= vdev
->isr
;
1416 pci_irq_deassert(&proxy
->pci_dev
);
1421 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1422 uint64_t val
, unsigned size
)
1426 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1429 VirtIODevice
*vdev
= opaque
;
1434 val
= virtio_config_modern_readb(vdev
, addr
);
1437 val
= virtio_config_modern_readw(vdev
, addr
);
1440 val
= virtio_config_modern_readl(vdev
, addr
);
1446 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1447 uint64_t val
, unsigned size
)
1449 VirtIODevice
*vdev
= opaque
;
1452 virtio_config_modern_writeb(vdev
, addr
, val
);
1455 virtio_config_modern_writew(vdev
, addr
, val
);
1458 virtio_config_modern_writel(vdev
, addr
, val
);
1463 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
)
1465 static const MemoryRegionOps common_ops
= {
1466 .read
= virtio_pci_common_read
,
1467 .write
= virtio_pci_common_write
,
1469 .min_access_size
= 1,
1470 .max_access_size
= 4,
1472 .endianness
= DEVICE_LITTLE_ENDIAN
,
1474 static const MemoryRegionOps isr_ops
= {
1475 .read
= virtio_pci_isr_read
,
1476 .write
= virtio_pci_isr_write
,
1478 .min_access_size
= 1,
1479 .max_access_size
= 4,
1481 .endianness
= DEVICE_LITTLE_ENDIAN
,
1483 static const MemoryRegionOps device_ops
= {
1484 .read
= virtio_pci_device_read
,
1485 .write
= virtio_pci_device_write
,
1487 .min_access_size
= 1,
1488 .max_access_size
= 4,
1490 .endianness
= DEVICE_LITTLE_ENDIAN
,
1492 static const MemoryRegionOps notify_ops
= {
1493 .read
= virtio_pci_notify_read
,
1494 .write
= virtio_pci_notify_write
,
1496 .min_access_size
= 1,
1497 .max_access_size
= 4,
1499 .endianness
= DEVICE_LITTLE_ENDIAN
,
1502 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1505 "virtio-pci-common",
1506 proxy
->common
.size
);
1508 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1514 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1516 virtio_bus_get_device(&proxy
->bus
),
1517 "virtio-pci-device",
1518 proxy
->device
.size
);
1520 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1522 virtio_bus_get_device(&proxy
->bus
),
1523 "virtio-pci-notify",
1524 proxy
->notify
.size
);
1527 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1528 VirtIOPCIRegion
*region
,
1529 struct virtio_pci_cap
*cap
)
1531 memory_region_add_subregion(&proxy
->modern_bar
,
1535 cap
->cfg_type
= region
->type
;
1536 cap
->bar
= proxy
->modern_mem_bar
;
1537 cap
->offset
= cpu_to_le32(region
->offset
);
1538 cap
->length
= cpu_to_le32(region
->size
);
1539 virtio_pci_add_mem_cap(proxy
, cap
);
1542 static void virtio_pci_modern_region_unmap(VirtIOPCIProxy
*proxy
,
1543 VirtIOPCIRegion
*region
)
1545 memory_region_del_subregion(&proxy
->modern_bar
,
1549 /* This is called by virtio-bus just after the device is plugged. */
1550 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1552 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1553 VirtioBusState
*bus
= &proxy
->bus
;
1554 bool legacy
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_LEGACY
);
1555 bool modern
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
1558 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1560 config
= proxy
->pci_dev
.config
;
1561 if (proxy
->class_code
) {
1562 pci_config_set_class(config
, proxy
->class_code
);
1566 /* legacy and transitional */
1567 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
1568 pci_get_word(config
+ PCI_VENDOR_ID
));
1569 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1571 /* pure virtio-1.0 */
1572 pci_set_word(config
+ PCI_VENDOR_ID
,
1573 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1574 pci_set_word(config
+ PCI_DEVICE_ID
,
1575 0x1040 + virtio_bus_get_vdev_id(bus
));
1576 pci_config_set_revision(config
, 1);
1578 config
[PCI_INTERRUPT_PIN
] = 1;
1582 struct virtio_pci_cap cap
= {
1583 .cap_len
= sizeof cap
,
1585 struct virtio_pci_notify_cap notify
= {
1586 .cap
.cap_len
= sizeof notify
,
1587 .notify_off_multiplier
=
1588 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
),
1590 struct virtio_pci_cfg_cap cfg
= {
1591 .cap
.cap_len
= sizeof cfg
,
1592 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
1594 struct virtio_pci_cfg_cap
*cfg_mask
;
1596 /* TODO: add io access for speed */
1598 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1599 virtio_pci_modern_regions_init(proxy
);
1600 virtio_pci_modern_region_map(proxy
, &proxy
->common
, &cap
);
1601 virtio_pci_modern_region_map(proxy
, &proxy
->isr
, &cap
);
1602 virtio_pci_modern_region_map(proxy
, &proxy
->device
, &cap
);
1603 virtio_pci_modern_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1605 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar
,
1606 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1607 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1608 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1609 &proxy
->modern_bar
);
1611 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
1612 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
1613 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
1614 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
1615 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
1616 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
1619 if (proxy
->nvectors
) {
1620 int err
= msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1623 /* Notice when a system that supports MSIx can't initialize it. */
1624 if (err
!= -ENOTSUP
) {
1625 error_report("unable to init msix vectors to %" PRIu32
,
1628 proxy
->nvectors
= 0;
1632 proxy
->pci_dev
.config_write
= virtio_write_config
;
1633 proxy
->pci_dev
.config_read
= virtio_read_config
;
1636 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1637 + virtio_bus_get_vdev_config_len(bus
);
1638 size
= pow2ceil(size
);
1640 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1641 &virtio_pci_config_ops
,
1642 proxy
, "virtio-pci", size
);
1644 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar
,
1645 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1648 if (!kvm_has_many_ioeventfds()) {
1649 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1652 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1655 static void virtio_pci_device_unplugged(DeviceState
*d
)
1657 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1658 bool modern
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
1660 virtio_pci_stop_ioeventfd(proxy
);
1663 virtio_pci_modern_region_unmap(proxy
, &proxy
->common
);
1664 virtio_pci_modern_region_unmap(proxy
, &proxy
->isr
);
1665 virtio_pci_modern_region_unmap(proxy
, &proxy
->device
);
1666 virtio_pci_modern_region_unmap(proxy
, &proxy
->notify
);
1670 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1672 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1673 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1676 * virtio pci bar layout used by default.
1677 * subclasses can re-arrange things if needed.
1679 * region 0 -- virtio legacy io bar
1680 * region 1 -- msi-x bar
1681 * region 4+5 -- virtio modern memory (64bit) bar
1684 proxy
->legacy_io_bar
= 0;
1685 proxy
->msix_bar
= 1;
1686 proxy
->modern_mem_bar
= 4;
1688 proxy
->common
.offset
= 0x0;
1689 proxy
->common
.size
= 0x1000;
1690 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1692 proxy
->isr
.offset
= 0x1000;
1693 proxy
->isr
.size
= 0x1000;
1694 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1696 proxy
->device
.offset
= 0x2000;
1697 proxy
->device
.size
= 0x1000;
1698 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1700 proxy
->notify
.offset
= 0x3000;
1701 proxy
->notify
.size
=
1702 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
* VIRTIO_QUEUE_MAX
;
1703 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1705 /* subclasses can enforce modern, so do this unconditionally */
1706 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1707 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
*
1710 memory_region_init_alias(&proxy
->modern_cfg
,
1715 memory_region_size(&proxy
->modern_bar
));
1717 address_space_init(&proxy
->modern_as
, &proxy
->modern_cfg
, "virtio-pci-cfg-as");
1719 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1721 k
->realize(proxy
, errp
);
1725 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1727 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1729 msix_uninit_exclusive_bar(pci_dev
);
1730 address_space_destroy(&proxy
->modern_as
);
1733 static void virtio_pci_reset(DeviceState
*qdev
)
1735 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1736 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1737 virtio_pci_stop_ioeventfd(proxy
);
1738 virtio_bus_reset(bus
);
1739 msix_unuse_all_vectors(&proxy
->pci_dev
);
1742 static Property virtio_pci_properties
[] = {
1743 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
1744 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
1745 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy
, flags
,
1746 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT
, false),
1747 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy
, flags
,
1748 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT
, true),
1749 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy
, flags
,
1750 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT
, true),
1751 DEFINE_PROP_END_OF_LIST(),
1754 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1756 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1757 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1759 dc
->props
= virtio_pci_properties
;
1760 k
->realize
= virtio_pci_realize
;
1761 k
->exit
= virtio_pci_exit
;
1762 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1763 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1764 k
->class_id
= PCI_CLASS_OTHERS
;
1765 dc
->reset
= virtio_pci_reset
;
1768 static const TypeInfo virtio_pci_info
= {
1769 .name
= TYPE_VIRTIO_PCI
,
1770 .parent
= TYPE_PCI_DEVICE
,
1771 .instance_size
= sizeof(VirtIOPCIProxy
),
1772 .class_init
= virtio_pci_class_init
,
1773 .class_size
= sizeof(VirtioPCIClass
),
1777 /* virtio-blk-pci */
1779 static Property virtio_blk_pci_properties
[] = {
1780 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1781 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1782 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1783 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1784 DEFINE_PROP_END_OF_LIST(),
1787 static void virtio_blk_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1789 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1790 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1792 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1793 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1796 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1798 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1799 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1800 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1802 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1803 dc
->props
= virtio_blk_pci_properties
;
1804 k
->realize
= virtio_blk_pci_realize
;
1805 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1806 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1807 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1808 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1811 static void virtio_blk_pci_instance_init(Object
*obj
)
1813 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1815 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1817 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
1819 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1820 "bootindex", &error_abort
);
1823 static const TypeInfo virtio_blk_pci_info
= {
1824 .name
= TYPE_VIRTIO_BLK_PCI
,
1825 .parent
= TYPE_VIRTIO_PCI
,
1826 .instance_size
= sizeof(VirtIOBlkPCI
),
1827 .instance_init
= virtio_blk_pci_instance_init
,
1828 .class_init
= virtio_blk_pci_class_init
,
1831 /* virtio-scsi-pci */
1833 static Property virtio_scsi_pci_properties
[] = {
1834 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1835 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1836 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1837 DEV_NVECTORS_UNSPECIFIED
),
1838 DEFINE_PROP_END_OF_LIST(),
1841 static void virtio_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1843 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1844 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1845 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1846 DeviceState
*proxy
= DEVICE(vpci_dev
);
1849 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1850 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1854 * For command line compatibility, this sets the virtio-scsi-device bus
1858 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1859 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1863 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1864 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1867 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1869 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1870 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1871 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1873 k
->realize
= virtio_scsi_pci_realize
;
1874 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1875 dc
->props
= virtio_scsi_pci_properties
;
1876 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1877 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1878 pcidev_k
->revision
= 0x00;
1879 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1882 static void virtio_scsi_pci_instance_init(Object
*obj
)
1884 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1886 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1888 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
1892 static const TypeInfo virtio_scsi_pci_info
= {
1893 .name
= TYPE_VIRTIO_SCSI_PCI
,
1894 .parent
= TYPE_VIRTIO_PCI
,
1895 .instance_size
= sizeof(VirtIOSCSIPCI
),
1896 .instance_init
= virtio_scsi_pci_instance_init
,
1897 .class_init
= virtio_scsi_pci_class_init
,
1900 /* vhost-scsi-pci */
1902 #ifdef CONFIG_VHOST_SCSI
1903 static Property vhost_scsi_pci_properties
[] = {
1904 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1905 DEV_NVECTORS_UNSPECIFIED
),
1906 DEFINE_PROP_END_OF_LIST(),
1909 static void vhost_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1911 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1912 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1913 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1915 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1916 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1919 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1920 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1923 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1925 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1926 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1927 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1928 k
->realize
= vhost_scsi_pci_realize
;
1929 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1930 dc
->props
= vhost_scsi_pci_properties
;
1931 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1932 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1933 pcidev_k
->revision
= 0x00;
1934 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1937 static void vhost_scsi_pci_instance_init(Object
*obj
)
1939 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1941 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1943 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1944 "bootindex", &error_abort
);
1947 static const TypeInfo vhost_scsi_pci_info
= {
1948 .name
= TYPE_VHOST_SCSI_PCI
,
1949 .parent
= TYPE_VIRTIO_PCI
,
1950 .instance_size
= sizeof(VHostSCSIPCI
),
1951 .instance_init
= vhost_scsi_pci_instance_init
,
1952 .class_init
= vhost_scsi_pci_class_init
,
1956 /* virtio-balloon-pci */
1958 static Property virtio_balloon_pci_properties
[] = {
1959 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1960 DEFINE_PROP_END_OF_LIST(),
1963 static void virtio_balloon_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1965 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1966 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1968 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1969 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1970 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1973 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1974 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1977 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1979 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1980 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1981 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1982 k
->realize
= virtio_balloon_pci_realize
;
1983 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1984 dc
->props
= virtio_balloon_pci_properties
;
1985 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1986 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1987 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1988 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1991 static void virtio_balloon_pci_instance_init(Object
*obj
)
1993 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1995 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1996 TYPE_VIRTIO_BALLOON
);
1997 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
1998 "guest-stats", &error_abort
);
1999 object_property_add_alias(obj
, "guest-stats-polling-interval",
2001 "guest-stats-polling-interval", &error_abort
);
2004 static const TypeInfo virtio_balloon_pci_info
= {
2005 .name
= TYPE_VIRTIO_BALLOON_PCI
,
2006 .parent
= TYPE_VIRTIO_PCI
,
2007 .instance_size
= sizeof(VirtIOBalloonPCI
),
2008 .instance_init
= virtio_balloon_pci_instance_init
,
2009 .class_init
= virtio_balloon_pci_class_init
,
2012 /* virtio-serial-pci */
2014 static void virtio_serial_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2016 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
2017 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2018 DeviceState
*proxy
= DEVICE(vpci_dev
);
2021 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
2022 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
2023 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
2024 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
2027 /* backwards-compatibility with machines that were created with
2028 DEV_NVECTORS_UNSPECIFIED */
2029 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2030 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
2034 * For command line compatibility, this sets the virtio-serial-device bus
2038 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
2039 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
2043 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2044 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2047 static Property virtio_serial_pci_properties
[] = {
2048 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
2049 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
2050 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
2051 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
2052 DEFINE_PROP_END_OF_LIST(),
2055 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
2057 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2058 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2059 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2060 k
->realize
= virtio_serial_pci_realize
;
2061 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
2062 dc
->props
= virtio_serial_pci_properties
;
2063 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2064 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
2065 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2066 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
2069 static void virtio_serial_pci_instance_init(Object
*obj
)
2071 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
2073 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2074 TYPE_VIRTIO_SERIAL
);
2077 static const TypeInfo virtio_serial_pci_info
= {
2078 .name
= TYPE_VIRTIO_SERIAL_PCI
,
2079 .parent
= TYPE_VIRTIO_PCI
,
2080 .instance_size
= sizeof(VirtIOSerialPCI
),
2081 .instance_init
= virtio_serial_pci_instance_init
,
2082 .class_init
= virtio_serial_pci_class_init
,
2085 /* virtio-net-pci */
2087 static Property virtio_net_properties
[] = {
2088 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
2089 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
2090 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
2091 DEFINE_PROP_END_OF_LIST(),
2094 static void virtio_net_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2096 DeviceState
*qdev
= DEVICE(vpci_dev
);
2097 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
2098 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2100 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
2101 object_get_typename(OBJECT(qdev
)));
2102 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2103 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2106 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
2108 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2109 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2110 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
2112 k
->romfile
= "efi-virtio.rom";
2113 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2114 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
2115 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2116 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
2117 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
2118 dc
->props
= virtio_net_properties
;
2119 vpciklass
->realize
= virtio_net_pci_realize
;
2122 static void virtio_net_pci_instance_init(Object
*obj
)
2124 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
2126 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2128 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2129 "bootindex", &error_abort
);
2132 static const TypeInfo virtio_net_pci_info
= {
2133 .name
= TYPE_VIRTIO_NET_PCI
,
2134 .parent
= TYPE_VIRTIO_PCI
,
2135 .instance_size
= sizeof(VirtIONetPCI
),
2136 .instance_init
= virtio_net_pci_instance_init
,
2137 .class_init
= virtio_net_pci_class_init
,
2140 /* virtio-rng-pci */
2142 static void virtio_rng_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2144 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
2145 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
2148 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2149 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
2151 error_propagate(errp
, err
);
2155 object_property_set_link(OBJECT(vrng
),
2156 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
2160 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
2162 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2163 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2164 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2166 k
->realize
= virtio_rng_pci_realize
;
2167 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2169 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2170 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
2171 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2172 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
2175 static void virtio_rng_initfn(Object
*obj
)
2177 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
2179 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2181 object_property_add_alias(obj
, "rng", OBJECT(&dev
->vdev
), "rng",
2185 static const TypeInfo virtio_rng_pci_info
= {
2186 .name
= TYPE_VIRTIO_RNG_PCI
,
2187 .parent
= TYPE_VIRTIO_PCI
,
2188 .instance_size
= sizeof(VirtIORngPCI
),
2189 .instance_init
= virtio_rng_initfn
,
2190 .class_init
= virtio_rng_pci_class_init
,
2193 /* virtio-input-pci */
2195 static Property virtio_input_pci_properties
[] = {
2196 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
2197 DEFINE_PROP_END_OF_LIST(),
2200 static void virtio_input_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2202 VirtIOInputPCI
*vinput
= VIRTIO_INPUT_PCI(vpci_dev
);
2203 DeviceState
*vdev
= DEVICE(&vinput
->vdev
);
2205 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2206 /* force virtio-1.0 */
2207 vpci_dev
->flags
&= ~VIRTIO_PCI_FLAG_DISABLE_MODERN
;
2208 vpci_dev
->flags
|= VIRTIO_PCI_FLAG_DISABLE_LEGACY
;
2209 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2212 static void virtio_input_pci_class_init(ObjectClass
*klass
, void *data
)
2214 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2215 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2216 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2218 dc
->props
= virtio_input_pci_properties
;
2219 k
->realize
= virtio_input_pci_realize
;
2220 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
2222 pcidev_k
->class_id
= PCI_CLASS_INPUT_OTHER
;
2225 static void virtio_input_hid_kbd_pci_class_init(ObjectClass
*klass
, void *data
)
2227 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2229 pcidev_k
->class_id
= PCI_CLASS_INPUT_KEYBOARD
;
2232 static void virtio_input_hid_mouse_pci_class_init(ObjectClass
*klass
,
2235 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2237 pcidev_k
->class_id
= PCI_CLASS_INPUT_MOUSE
;
2240 static void virtio_keyboard_initfn(Object
*obj
)
2242 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2244 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2245 TYPE_VIRTIO_KEYBOARD
);
2248 static void virtio_mouse_initfn(Object
*obj
)
2250 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2252 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2256 static void virtio_tablet_initfn(Object
*obj
)
2258 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2260 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2261 TYPE_VIRTIO_TABLET
);
2264 static const TypeInfo virtio_input_pci_info
= {
2265 .name
= TYPE_VIRTIO_INPUT_PCI
,
2266 .parent
= TYPE_VIRTIO_PCI
,
2267 .instance_size
= sizeof(VirtIOInputPCI
),
2268 .class_init
= virtio_input_pci_class_init
,
2272 static const TypeInfo virtio_input_hid_pci_info
= {
2273 .name
= TYPE_VIRTIO_INPUT_HID_PCI
,
2274 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2275 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2279 static const TypeInfo virtio_keyboard_pci_info
= {
2280 .name
= TYPE_VIRTIO_KEYBOARD_PCI
,
2281 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2282 .class_init
= virtio_input_hid_kbd_pci_class_init
,
2283 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2284 .instance_init
= virtio_keyboard_initfn
,
2287 static const TypeInfo virtio_mouse_pci_info
= {
2288 .name
= TYPE_VIRTIO_MOUSE_PCI
,
2289 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2290 .class_init
= virtio_input_hid_mouse_pci_class_init
,
2291 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2292 .instance_init
= virtio_mouse_initfn
,
2295 static const TypeInfo virtio_tablet_pci_info
= {
2296 .name
= TYPE_VIRTIO_TABLET_PCI
,
2297 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2298 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2299 .instance_init
= virtio_tablet_initfn
,
2303 static void virtio_host_initfn(Object
*obj
)
2305 VirtIOInputHostPCI
*dev
= VIRTIO_INPUT_HOST_PCI(obj
);
2307 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2308 TYPE_VIRTIO_INPUT_HOST
);
2311 static const TypeInfo virtio_host_pci_info
= {
2312 .name
= TYPE_VIRTIO_INPUT_HOST_PCI
,
2313 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2314 .instance_size
= sizeof(VirtIOInputHostPCI
),
2315 .instance_init
= virtio_host_initfn
,
2319 /* virtio-pci-bus */
2321 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2322 VirtIOPCIProxy
*dev
)
2324 DeviceState
*qdev
= DEVICE(dev
);
2325 char virtio_bus_name
[] = "virtio-bus";
2327 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
2331 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2333 BusClass
*bus_class
= BUS_CLASS(klass
);
2334 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2335 bus_class
->max_dev
= 1;
2336 k
->notify
= virtio_pci_notify
;
2337 k
->save_config
= virtio_pci_save_config
;
2338 k
->load_config
= virtio_pci_load_config
;
2339 k
->save_queue
= virtio_pci_save_queue
;
2340 k
->load_queue
= virtio_pci_load_queue
;
2341 k
->save_extra_state
= virtio_pci_save_extra_state
;
2342 k
->load_extra_state
= virtio_pci_load_extra_state
;
2343 k
->has_extra_state
= virtio_pci_has_extra_state
;
2344 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2345 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
2346 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2347 k
->vmstate_change
= virtio_pci_vmstate_change
;
2348 k
->device_plugged
= virtio_pci_device_plugged
;
2349 k
->device_unplugged
= virtio_pci_device_unplugged
;
2350 k
->query_nvectors
= virtio_pci_query_nvectors
;
2353 static const TypeInfo virtio_pci_bus_info
= {
2354 .name
= TYPE_VIRTIO_PCI_BUS
,
2355 .parent
= TYPE_VIRTIO_BUS
,
2356 .instance_size
= sizeof(VirtioPCIBusState
),
2357 .class_init
= virtio_pci_bus_class_init
,
2360 static void virtio_pci_register_types(void)
2362 type_register_static(&virtio_rng_pci_info
);
2363 type_register_static(&virtio_input_pci_info
);
2364 type_register_static(&virtio_input_hid_pci_info
);
2365 type_register_static(&virtio_keyboard_pci_info
);
2366 type_register_static(&virtio_mouse_pci_info
);
2367 type_register_static(&virtio_tablet_pci_info
);
2369 type_register_static(&virtio_host_pci_info
);
2371 type_register_static(&virtio_pci_bus_info
);
2372 type_register_static(&virtio_pci_info
);
2373 #ifdef CONFIG_VIRTFS
2374 type_register_static(&virtio_9p_pci_info
);
2376 type_register_static(&virtio_blk_pci_info
);
2377 type_register_static(&virtio_scsi_pci_info
);
2378 type_register_static(&virtio_balloon_pci_info
);
2379 type_register_static(&virtio_serial_pci_info
);
2380 type_register_static(&virtio_net_pci_info
);
2381 #ifdef CONFIG_VHOST_SCSI
2382 type_register_static(&vhost_scsi_pci_info
);
2386 type_init(virtio_pci_register_types
)