149ab747fSPaolo Bonzini /*
249ab747fSPaolo Bonzini * Virtio PCI Bindings
349ab747fSPaolo Bonzini *
449ab747fSPaolo Bonzini * Copyright IBM, Corp. 2007
549ab747fSPaolo Bonzini * Copyright (c) 2009 CodeSourcery
649ab747fSPaolo Bonzini *
749ab747fSPaolo Bonzini * Authors:
849ab747fSPaolo Bonzini * Anthony Liguori <aliguori@us.ibm.com>
949ab747fSPaolo Bonzini * Paul Brook <paul@codesourcery.com>
1049ab747fSPaolo Bonzini *
1149ab747fSPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2. See
1249ab747fSPaolo Bonzini * the COPYING file in the top-level directory.
1349ab747fSPaolo Bonzini *
1449ab747fSPaolo Bonzini * Contributions after 2012-01-13 are licensed under the terms of the
1549ab747fSPaolo Bonzini * GNU GPL, version 2 or (at your option) any later version.
1649ab747fSPaolo Bonzini */
1749ab747fSPaolo Bonzini
189b8bfe21SPeter Maydell #include "qemu/osdep.h"
1949ab747fSPaolo Bonzini
20062c08d1STony Nguyen #include "exec/memop.h"
21cbbe4f50SMichael S. Tsirkin #include "standard-headers/linux/virtio_pci.h"
2222733245SLongpeng #include "standard-headers/linux/virtio_ids.h"
231436f32aSStefan Hajnoczi #include "hw/boards.h"
2449ab747fSPaolo Bonzini #include "hw/virtio/virtio.h"
25ca77ee28SMarkus Armbruster #include "migration/qemu-file-types.h"
2649ab747fSPaolo Bonzini #include "hw/pci/pci.h"
27b0e5196aSDavid Gibson #include "hw/pci/pci_bus.h"
28a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
29da34e65cSMarkus Armbruster #include "qapi/error.h"
3049ab747fSPaolo Bonzini #include "qemu/error-report.h"
31a8218588SPhilippe Mathieu-Daudé #include "qemu/log.h"
320b8fa32fSMarkus Armbruster #include "qemu/module.h"
3349ab747fSPaolo Bonzini #include "hw/pci/msi.h"
3449ab747fSPaolo Bonzini #include "hw/pci/msix.h"
3549ab747fSPaolo Bonzini #include "hw/loader.h"
3649ab747fSPaolo Bonzini #include "sysemu/kvm.h"
37e1b1f534SAlex Bennée #include "hw/virtio/virtio-pci.h"
3849ab747fSPaolo Bonzini #include "qemu/range.h"
3949ab747fSPaolo Bonzini #include "hw/virtio/virtio-bus.h"
4024a6e7f4SKONRAD Frederic #include "qapi/visitor.h"
413909c079SPavel Dovgalyuk #include "sysemu/replay.h"
4231cc62bbSAlex Bennée #include "trace.h"
4349ab747fSPaolo Bonzini
44cbbe4f50SMichael S. Tsirkin #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
4549ab747fSPaolo Bonzini
46c17bef33SMichael S. Tsirkin #undef VIRTIO_PCI_CONFIG
47c17bef33SMichael S. Tsirkin
4849ab747fSPaolo Bonzini /* The remaining space is defined by each driver as the per-driver
4949ab747fSPaolo Bonzini * configuration space */
50cbbe4f50SMichael S. Tsirkin #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
5149ab747fSPaolo Bonzini
52ac7af112SAndreas Färber static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
53ac7af112SAndreas Färber VirtIOPCIProxy *dev);
5475fd6f13SGerd Hoffmann static void virtio_pci_reset(DeviceState *qdev);
55d51fcfacSKONRAD Frederic
5649ab747fSPaolo Bonzini /* virtio device */
5749ab747fSPaolo Bonzini /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
to_virtio_pci_proxy(DeviceState * d)5849ab747fSPaolo Bonzini static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
5949ab747fSPaolo Bonzini {
6049ab747fSPaolo Bonzini return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
6149ab747fSPaolo Bonzini }
6249ab747fSPaolo Bonzini
6349ab747fSPaolo Bonzini /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
6449ab747fSPaolo Bonzini * be careful and test performance if you change this.
6549ab747fSPaolo Bonzini */
to_virtio_pci_proxy_fast(DeviceState * d)6649ab747fSPaolo Bonzini static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
6749ab747fSPaolo Bonzini {
6849ab747fSPaolo Bonzini return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
6949ab747fSPaolo Bonzini }
7049ab747fSPaolo Bonzini
virtio_pci_notify(DeviceState * d,uint16_t vector)7149ab747fSPaolo Bonzini static void virtio_pci_notify(DeviceState *d, uint16_t vector)
7249ab747fSPaolo Bonzini {
7349ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
74a3fc66d9SPaolo Bonzini
7515377f6eSAkihiko Odaki if (msix_enabled(&proxy->pci_dev)) {
7615377f6eSAkihiko Odaki if (vector != VIRTIO_NO_VECTOR) {
7749ab747fSPaolo Bonzini msix_notify(&proxy->pci_dev, vector);
7815377f6eSAkihiko Odaki }
7915377f6eSAkihiko Odaki } else {
80a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
81d73415a3SStefan Hajnoczi pci_set_irq(&proxy->pci_dev, qatomic_read(&vdev->isr) & 1);
82a3fc66d9SPaolo Bonzini }
8349ab747fSPaolo Bonzini }
8449ab747fSPaolo Bonzini
virtio_pci_save_config(DeviceState * d,QEMUFile * f)8549ab747fSPaolo Bonzini static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
8649ab747fSPaolo Bonzini {
8749ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
88a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
89a3fc66d9SPaolo Bonzini
9049ab747fSPaolo Bonzini pci_device_save(&proxy->pci_dev, f);
9149ab747fSPaolo Bonzini msix_save(&proxy->pci_dev, f);
9249ab747fSPaolo Bonzini if (msix_present(&proxy->pci_dev))
93a3fc66d9SPaolo Bonzini qemu_put_be16(f, vdev->config_vector);
9449ab747fSPaolo Bonzini }
9549ab747fSPaolo Bonzini
96b81b948eSDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_pci_modern_queue_state = {
97b81b948eSDr. David Alan Gilbert .name = "virtio_pci/modern_queue_state",
98b81b948eSDr. David Alan Gilbert .version_id = 1,
99b81b948eSDr. David Alan Gilbert .minimum_version_id = 1,
100ca02a170SRichard Henderson .fields = (const VMStateField[]) {
101b81b948eSDr. David Alan Gilbert VMSTATE_UINT16(num, VirtIOPCIQueue),
102b81b948eSDr. David Alan Gilbert VMSTATE_UNUSED(1), /* enabled was stored as be16 */
103b81b948eSDr. David Alan Gilbert VMSTATE_BOOL(enabled, VirtIOPCIQueue),
104b81b948eSDr. David Alan Gilbert VMSTATE_UINT32_ARRAY(desc, VirtIOPCIQueue, 2),
105b81b948eSDr. David Alan Gilbert VMSTATE_UINT32_ARRAY(avail, VirtIOPCIQueue, 2),
106b81b948eSDr. David Alan Gilbert VMSTATE_UINT32_ARRAY(used, VirtIOPCIQueue, 2),
107b81b948eSDr. David Alan Gilbert VMSTATE_END_OF_LIST()
108a6df8adfSJason Wang }
109a6df8adfSJason Wang };
110a6df8adfSJason Wang
virtio_pci_modern_state_needed(void * opaque)111a6df8adfSJason Wang static bool virtio_pci_modern_state_needed(void *opaque)
112a6df8adfSJason Wang {
113a6df8adfSJason Wang VirtIOPCIProxy *proxy = opaque;
114a6df8adfSJason Wang
1159a4c0e22SMarcel Apfelbaum return virtio_pci_modern(proxy);
116a6df8adfSJason Wang }
117a6df8adfSJason Wang
118b81b948eSDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_pci_modern_state_sub = {
119a6df8adfSJason Wang .name = "virtio_pci/modern_state",
120a6df8adfSJason Wang .version_id = 1,
121a6df8adfSJason Wang .minimum_version_id = 1,
122a6df8adfSJason Wang .needed = &virtio_pci_modern_state_needed,
123ca02a170SRichard Henderson .fields = (const VMStateField[]) {
124b81b948eSDr. David Alan Gilbert VMSTATE_UINT32(dfselect, VirtIOPCIProxy),
125b81b948eSDr. David Alan Gilbert VMSTATE_UINT32(gfselect, VirtIOPCIProxy),
126b81b948eSDr. David Alan Gilbert VMSTATE_UINT32_ARRAY(guest_features, VirtIOPCIProxy, 2),
127b81b948eSDr. David Alan Gilbert VMSTATE_STRUCT_ARRAY(vqs, VirtIOPCIProxy, VIRTIO_QUEUE_MAX, 0,
128b81b948eSDr. David Alan Gilbert vmstate_virtio_pci_modern_queue_state,
129b81b948eSDr. David Alan Gilbert VirtIOPCIQueue),
130a6df8adfSJason Wang VMSTATE_END_OF_LIST()
131a6df8adfSJason Wang }
132a6df8adfSJason Wang };
133a6df8adfSJason Wang
134a6df8adfSJason Wang static const VMStateDescription vmstate_virtio_pci = {
135a6df8adfSJason Wang .name = "virtio_pci",
136a6df8adfSJason Wang .version_id = 1,
137a6df8adfSJason Wang .minimum_version_id = 1,
138ca02a170SRichard Henderson .fields = (const VMStateField[]) {
139a6df8adfSJason Wang VMSTATE_END_OF_LIST()
140a6df8adfSJason Wang },
141ca02a170SRichard Henderson .subsections = (const VMStateDescription * const []) {
142b81b948eSDr. David Alan Gilbert &vmstate_virtio_pci_modern_state_sub,
143a6df8adfSJason Wang NULL
144a6df8adfSJason Wang }
145a6df8adfSJason Wang };
146a6df8adfSJason Wang
virtio_pci_has_extra_state(DeviceState * d)147b81b948eSDr. David Alan Gilbert static bool virtio_pci_has_extra_state(DeviceState *d)
148b81b948eSDr. David Alan Gilbert {
149b81b948eSDr. David Alan Gilbert VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
150b81b948eSDr. David Alan Gilbert
151b81b948eSDr. David Alan Gilbert return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA;
152b81b948eSDr. David Alan Gilbert }
153b81b948eSDr. David Alan Gilbert
virtio_pci_save_extra_state(DeviceState * d,QEMUFile * f)154a6df8adfSJason Wang static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f)
155a6df8adfSJason Wang {
156a6df8adfSJason Wang VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
157a6df8adfSJason Wang
158a6df8adfSJason Wang vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL);
159a6df8adfSJason Wang }
160a6df8adfSJason Wang
virtio_pci_load_extra_state(DeviceState * d,QEMUFile * f)161a6df8adfSJason Wang static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f)
162a6df8adfSJason Wang {
163a6df8adfSJason Wang VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
164a6df8adfSJason Wang
165a6df8adfSJason Wang return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1);
166a6df8adfSJason Wang }
167a6df8adfSJason Wang
virtio_pci_save_queue(DeviceState * d,int n,QEMUFile * f)16849ab747fSPaolo Bonzini static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
16949ab747fSPaolo Bonzini {
17049ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
171a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
172a3fc66d9SPaolo Bonzini
17349ab747fSPaolo Bonzini if (msix_present(&proxy->pci_dev))
174a3fc66d9SPaolo Bonzini qemu_put_be16(f, virtio_queue_vector(vdev, n));
17549ab747fSPaolo Bonzini }
17649ab747fSPaolo Bonzini
virtio_pci_load_config(DeviceState * d,QEMUFile * f)17749ab747fSPaolo Bonzini static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
17849ab747fSPaolo Bonzini {
17949ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
180a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
18115377f6eSAkihiko Odaki uint16_t vector;
182a3fc66d9SPaolo Bonzini
18349ab747fSPaolo Bonzini int ret;
18449ab747fSPaolo Bonzini ret = pci_device_load(&proxy->pci_dev, f);
18549ab747fSPaolo Bonzini if (ret) {
18649ab747fSPaolo Bonzini return ret;
18749ab747fSPaolo Bonzini }
18849ab747fSPaolo Bonzini msix_unuse_all_vectors(&proxy->pci_dev);
18949ab747fSPaolo Bonzini msix_load(&proxy->pci_dev, f);
19049ab747fSPaolo Bonzini if (msix_present(&proxy->pci_dev)) {
19115377f6eSAkihiko Odaki qemu_get_be16s(f, &vector);
19215377f6eSAkihiko Odaki
19315377f6eSAkihiko Odaki if (vector != VIRTIO_NO_VECTOR && vector >= proxy->nvectors) {
19415377f6eSAkihiko Odaki return -EINVAL;
19549ab747fSPaolo Bonzini }
19615377f6eSAkihiko Odaki } else {
19715377f6eSAkihiko Odaki vector = VIRTIO_NO_VECTOR;
19815377f6eSAkihiko Odaki }
19915377f6eSAkihiko Odaki vdev->config_vector = vector;
20015377f6eSAkihiko Odaki if (vector != VIRTIO_NO_VECTOR) {
20115377f6eSAkihiko Odaki msix_vector_use(&proxy->pci_dev, vector);
20249ab747fSPaolo Bonzini }
20349ab747fSPaolo Bonzini return 0;
20449ab747fSPaolo Bonzini }
20549ab747fSPaolo Bonzini
virtio_pci_load_queue(DeviceState * d,int n,QEMUFile * f)20649ab747fSPaolo Bonzini static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
20749ab747fSPaolo Bonzini {
20849ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
209a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
210a3fc66d9SPaolo Bonzini
21149ab747fSPaolo Bonzini uint16_t vector;
21249ab747fSPaolo Bonzini if (msix_present(&proxy->pci_dev)) {
21349ab747fSPaolo Bonzini qemu_get_be16s(f, &vector);
21415377f6eSAkihiko Odaki if (vector != VIRTIO_NO_VECTOR && vector >= proxy->nvectors) {
21515377f6eSAkihiko Odaki return -EINVAL;
21615377f6eSAkihiko Odaki }
21749ab747fSPaolo Bonzini } else {
21849ab747fSPaolo Bonzini vector = VIRTIO_NO_VECTOR;
21949ab747fSPaolo Bonzini }
220a3fc66d9SPaolo Bonzini virtio_queue_set_vector(vdev, n, vector);
22149ab747fSPaolo Bonzini if (vector != VIRTIO_NO_VECTOR) {
22215377f6eSAkihiko Odaki msix_vector_use(&proxy->pci_dev, vector);
22349ab747fSPaolo Bonzini }
224a6df8adfSJason Wang
22549ab747fSPaolo Bonzini return 0;
22649ab747fSPaolo Bonzini }
22749ab747fSPaolo Bonzini
22822733245SLongpeng typedef struct VirtIOPCIIDInfo {
22922733245SLongpeng /* virtio id */
23022733245SLongpeng uint16_t vdev_id;
23122733245SLongpeng /* pci device id for the transitional device */
23222733245SLongpeng uint16_t trans_devid;
23322733245SLongpeng uint16_t class_id;
23422733245SLongpeng } VirtIOPCIIDInfo;
23522733245SLongpeng
23622733245SLongpeng static const VirtIOPCIIDInfo virtio_pci_id_info[] = {
23722733245SLongpeng {
23822733245SLongpeng .vdev_id = VIRTIO_ID_CRYPTO,
23922733245SLongpeng .class_id = PCI_CLASS_OTHERS,
24022733245SLongpeng }, {
24122733245SLongpeng .vdev_id = VIRTIO_ID_FS,
24222733245SLongpeng .class_id = PCI_CLASS_STORAGE_OTHER,
24322733245SLongpeng }, {
24422733245SLongpeng .vdev_id = VIRTIO_ID_NET,
24522733245SLongpeng .trans_devid = PCI_DEVICE_ID_VIRTIO_NET,
24622733245SLongpeng .class_id = PCI_CLASS_NETWORK_ETHERNET,
24722733245SLongpeng }, {
24822733245SLongpeng .vdev_id = VIRTIO_ID_BLOCK,
24922733245SLongpeng .trans_devid = PCI_DEVICE_ID_VIRTIO_BLOCK,
25022733245SLongpeng .class_id = PCI_CLASS_STORAGE_SCSI,
25122733245SLongpeng }, {
25222733245SLongpeng .vdev_id = VIRTIO_ID_CONSOLE,
25322733245SLongpeng .trans_devid = PCI_DEVICE_ID_VIRTIO_CONSOLE,
25422733245SLongpeng .class_id = PCI_CLASS_COMMUNICATION_OTHER,
25522733245SLongpeng }, {
25622733245SLongpeng .vdev_id = VIRTIO_ID_SCSI,
25722733245SLongpeng .trans_devid = PCI_DEVICE_ID_VIRTIO_SCSI,
25822733245SLongpeng .class_id = PCI_CLASS_STORAGE_SCSI
25922733245SLongpeng }, {
26022733245SLongpeng .vdev_id = VIRTIO_ID_9P,
26122733245SLongpeng .trans_devid = PCI_DEVICE_ID_VIRTIO_9P,
26222733245SLongpeng .class_id = PCI_BASE_CLASS_NETWORK,
26322733245SLongpeng }, {
26422733245SLongpeng .vdev_id = VIRTIO_ID_BALLOON,
26522733245SLongpeng .trans_devid = PCI_DEVICE_ID_VIRTIO_BALLOON,
26622733245SLongpeng .class_id = PCI_CLASS_OTHERS,
26722733245SLongpeng }, {
26822733245SLongpeng .vdev_id = VIRTIO_ID_RNG,
26922733245SLongpeng .trans_devid = PCI_DEVICE_ID_VIRTIO_RNG,
27022733245SLongpeng .class_id = PCI_CLASS_OTHERS,
27122733245SLongpeng },
27222733245SLongpeng };
27322733245SLongpeng
virtio_pci_get_id_info(uint16_t vdev_id)27422733245SLongpeng static const VirtIOPCIIDInfo *virtio_pci_get_id_info(uint16_t vdev_id)
27522733245SLongpeng {
27622733245SLongpeng const VirtIOPCIIDInfo *info = NULL;
27722733245SLongpeng int i;
27822733245SLongpeng
27922733245SLongpeng for (i = 0; i < ARRAY_SIZE(virtio_pci_id_info); i++) {
28022733245SLongpeng if (virtio_pci_id_info[i].vdev_id == vdev_id) {
28122733245SLongpeng info = &virtio_pci_id_info[i];
28222733245SLongpeng break;
28322733245SLongpeng }
28422733245SLongpeng }
28522733245SLongpeng
28622733245SLongpeng if (!info) {
28722733245SLongpeng /* The device id is invalid or not added to the id_info yet. */
28822733245SLongpeng error_report("Invalid virtio device(id %u)", vdev_id);
28922733245SLongpeng abort();
29022733245SLongpeng }
29122733245SLongpeng
29222733245SLongpeng return info;
29322733245SLongpeng }
29422733245SLongpeng
29522733245SLongpeng /*
29622733245SLongpeng * Get the Transitional Device ID for the specific device, return
29722733245SLongpeng * zero if the device is non-transitional.
29822733245SLongpeng */
virtio_pci_get_trans_devid(uint16_t device_id)29922733245SLongpeng uint16_t virtio_pci_get_trans_devid(uint16_t device_id)
30022733245SLongpeng {
30122733245SLongpeng return virtio_pci_get_id_info(device_id)->trans_devid;
30222733245SLongpeng }
30322733245SLongpeng
30422733245SLongpeng /*
30522733245SLongpeng * Get the Class ID for the specific device.
30622733245SLongpeng */
virtio_pci_get_class_id(uint16_t device_id)30722733245SLongpeng uint16_t virtio_pci_get_class_id(uint16_t device_id)
30822733245SLongpeng {
30922733245SLongpeng return virtio_pci_get_id_info(device_id)->class_id;
31022733245SLongpeng }
31122733245SLongpeng
virtio_pci_ioeventfd_enabled(DeviceState * d)3128e93cef1SPaolo Bonzini static bool virtio_pci_ioeventfd_enabled(DeviceState *d)
3139f06e71aSCornelia Huck {
3149f06e71aSCornelia Huck VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
3159f06e71aSCornelia Huck
3168e93cef1SPaolo Bonzini return (proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) != 0;
3179f06e71aSCornelia Huck }
3189f06e71aSCornelia Huck
319975acc0aSJason Wang #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
320975acc0aSJason Wang
virtio_pci_queue_mem_mult(struct VirtIOPCIProxy * proxy)321d9997d89SMarcel Apfelbaum static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy *proxy)
322d9997d89SMarcel Apfelbaum {
323d9997d89SMarcel Apfelbaum return (proxy->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ) ?
324d9997d89SMarcel Apfelbaum QEMU_VIRTIO_PCI_QUEUE_MEM_MULT : 4;
325d9997d89SMarcel Apfelbaum }
326d9997d89SMarcel Apfelbaum
virtio_pci_ioeventfd_assign(DeviceState * d,EventNotifier * notifier,int n,bool assign)3279f06e71aSCornelia Huck static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
3289f06e71aSCornelia Huck int n, bool assign)
32949ab747fSPaolo Bonzini {
3309f06e71aSCornelia Huck VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
331a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
332a3fc66d9SPaolo Bonzini VirtQueue *vq = virtio_get_queue(vdev, n);
3339a4c0e22SMarcel Apfelbaum bool legacy = virtio_pci_legacy(proxy);
3349a4c0e22SMarcel Apfelbaum bool modern = virtio_pci_modern(proxy);
3359824d2a3SJason Wang bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
336588255adSGerd Hoffmann MemoryRegion *modern_mr = &proxy->notify.mr;
3379824d2a3SJason Wang MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
338975acc0aSJason Wang MemoryRegion *legacy_mr = &proxy->bar;
339d9997d89SMarcel Apfelbaum hwaddr modern_addr = virtio_pci_queue_mem_mult(proxy) *
340975acc0aSJason Wang virtio_get_queue_index(vq);
341975acc0aSJason Wang hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
34249ab747fSPaolo Bonzini
34349ab747fSPaolo Bonzini if (assign) {
344975acc0aSJason Wang if (modern) {
345bc85ccfdSJason Wang memory_region_add_eventfd(modern_mr, modern_addr, 0,
346bc85ccfdSJason Wang false, n, notifier);
3479824d2a3SJason Wang if (modern_pio) {
3489824d2a3SJason Wang memory_region_add_eventfd(modern_notify_mr, 0, 2,
3499824d2a3SJason Wang true, n, notifier);
3509824d2a3SJason Wang }
351975acc0aSJason Wang }
352975acc0aSJason Wang if (legacy) {
353975acc0aSJason Wang memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
354975acc0aSJason Wang true, n, notifier);
355975acc0aSJason Wang }
35649ab747fSPaolo Bonzini } else {
357975acc0aSJason Wang if (modern) {
358bc85ccfdSJason Wang memory_region_del_eventfd(modern_mr, modern_addr, 0,
359bc85ccfdSJason Wang false, n, notifier);
3609824d2a3SJason Wang if (modern_pio) {
3619824d2a3SJason Wang memory_region_del_eventfd(modern_notify_mr, 0, 2,
3629824d2a3SJason Wang true, n, notifier);
3639824d2a3SJason Wang }
364975acc0aSJason Wang }
365975acc0aSJason Wang if (legacy) {
366975acc0aSJason Wang memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
367975acc0aSJason Wang true, n, notifier);
368975acc0aSJason Wang }
36949ab747fSPaolo Bonzini }
3709f06e71aSCornelia Huck return 0;
37149ab747fSPaolo Bonzini }
37249ab747fSPaolo Bonzini
virtio_pci_start_ioeventfd(VirtIOPCIProxy * proxy)37349ab747fSPaolo Bonzini static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
37449ab747fSPaolo Bonzini {
3759f06e71aSCornelia Huck virtio_bus_start_ioeventfd(&proxy->bus);
37649ab747fSPaolo Bonzini }
37749ab747fSPaolo Bonzini
virtio_pci_stop_ioeventfd(VirtIOPCIProxy * proxy)37849ab747fSPaolo Bonzini static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
37949ab747fSPaolo Bonzini {
3809f06e71aSCornelia Huck virtio_bus_stop_ioeventfd(&proxy->bus);
38149ab747fSPaolo Bonzini }
38249ab747fSPaolo Bonzini
virtio_ioport_write(void * opaque,uint32_t addr,uint32_t val)38349ab747fSPaolo Bonzini static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
38449ab747fSPaolo Bonzini {
38549ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = opaque;
386a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
387cf39b828SJonah Palmer uint16_t vector, vq_idx;
38849ab747fSPaolo Bonzini hwaddr pa;
38949ab747fSPaolo Bonzini
39049ab747fSPaolo Bonzini switch (addr) {
39149ab747fSPaolo Bonzini case VIRTIO_PCI_GUEST_FEATURES:
39249ab747fSPaolo Bonzini /* Guest does not negotiate properly? We have to assume nothing. */
39349ab747fSPaolo Bonzini if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
394181103cdSKONRAD Frederic val = virtio_bus_get_vdev_bad_features(&proxy->bus);
39549ab747fSPaolo Bonzini }
39649ab747fSPaolo Bonzini virtio_set_features(vdev, val);
39749ab747fSPaolo Bonzini break;
39849ab747fSPaolo Bonzini case VIRTIO_PCI_QUEUE_PFN:
39949ab747fSPaolo Bonzini pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
40049ab747fSPaolo Bonzini if (pa == 0) {
40175fd6f13SGerd Hoffmann virtio_pci_reset(DEVICE(proxy));
40249ab747fSPaolo Bonzini }
40349ab747fSPaolo Bonzini else
40449ab747fSPaolo Bonzini virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
40549ab747fSPaolo Bonzini break;
40649ab747fSPaolo Bonzini case VIRTIO_PCI_QUEUE_SEL:
40787b3bd1cSJason Wang if (val < VIRTIO_QUEUE_MAX)
40849ab747fSPaolo Bonzini vdev->queue_sel = val;
40949ab747fSPaolo Bonzini break;
41049ab747fSPaolo Bonzini case VIRTIO_PCI_QUEUE_NOTIFY:
411cf39b828SJonah Palmer vq_idx = val;
412cf39b828SJonah Palmer if (vq_idx < VIRTIO_QUEUE_MAX && virtio_queue_get_num(vdev, vq_idx)) {
413cf39b828SJonah Palmer if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFICATION_DATA)) {
414cf39b828SJonah Palmer VirtQueue *vq = virtio_get_queue(vdev, vq_idx);
415cf39b828SJonah Palmer
416cf39b828SJonah Palmer virtio_queue_set_shadow_avail_idx(vq, val >> 16);
417cf39b828SJonah Palmer }
418cf39b828SJonah Palmer virtio_queue_notify(vdev, vq_idx);
41949ab747fSPaolo Bonzini }
42049ab747fSPaolo Bonzini break;
42149ab747fSPaolo Bonzini case VIRTIO_PCI_STATUS:
42249ab747fSPaolo Bonzini if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
42349ab747fSPaolo Bonzini virtio_pci_stop_ioeventfd(proxy);
42449ab747fSPaolo Bonzini }
42549ab747fSPaolo Bonzini
42649ab747fSPaolo Bonzini virtio_set_status(vdev, val & 0xFF);
42749ab747fSPaolo Bonzini
42849ab747fSPaolo Bonzini if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
42949ab747fSPaolo Bonzini virtio_pci_start_ioeventfd(proxy);
43049ab747fSPaolo Bonzini }
43149ab747fSPaolo Bonzini
43249ab747fSPaolo Bonzini if (vdev->status == 0) {
43375fd6f13SGerd Hoffmann virtio_pci_reset(DEVICE(proxy));
43449ab747fSPaolo Bonzini }
43549ab747fSPaolo Bonzini
436e43c0b2eSMichael S. Tsirkin /* Linux before 2.6.34 drives the device without enabling
437e43c0b2eSMichael S. Tsirkin the PCI device bus master bit. Enable it automatically
438e43c0b2eSMichael S. Tsirkin for the guest. This is a PCI spec violation but so is
439e43c0b2eSMichael S. Tsirkin initiating DMA with bus master bit clear. */
440e43c0b2eSMichael S. Tsirkin if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
441e43c0b2eSMichael S. Tsirkin pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
442e43c0b2eSMichael S. Tsirkin proxy->pci_dev.config[PCI_COMMAND] |
443e43c0b2eSMichael S. Tsirkin PCI_COMMAND_MASTER, 1);
444e43c0b2eSMichael S. Tsirkin }
44549ab747fSPaolo Bonzini break;
44649ab747fSPaolo Bonzini case VIRTIO_MSI_CONFIG_VECTOR:
44715377f6eSAkihiko Odaki if (vdev->config_vector != VIRTIO_NO_VECTOR) {
44849ab747fSPaolo Bonzini msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
44915377f6eSAkihiko Odaki }
45049ab747fSPaolo Bonzini /* Make it possible for guest to discover an error took place. */
45115377f6eSAkihiko Odaki if (val < proxy->nvectors) {
45215377f6eSAkihiko Odaki msix_vector_use(&proxy->pci_dev, val);
45315377f6eSAkihiko Odaki } else {
45449ab747fSPaolo Bonzini val = VIRTIO_NO_VECTOR;
45515377f6eSAkihiko Odaki }
45649ab747fSPaolo Bonzini vdev->config_vector = val;
45749ab747fSPaolo Bonzini break;
45849ab747fSPaolo Bonzini case VIRTIO_MSI_QUEUE_VECTOR:
45915377f6eSAkihiko Odaki vector = virtio_queue_vector(vdev, vdev->queue_sel);
46015377f6eSAkihiko Odaki if (vector != VIRTIO_NO_VECTOR) {
46115377f6eSAkihiko Odaki msix_vector_unuse(&proxy->pci_dev, vector);
46215377f6eSAkihiko Odaki }
46349ab747fSPaolo Bonzini /* Make it possible for guest to discover an error took place. */
46415377f6eSAkihiko Odaki if (val < proxy->nvectors) {
46515377f6eSAkihiko Odaki msix_vector_use(&proxy->pci_dev, val);
46615377f6eSAkihiko Odaki } else {
46749ab747fSPaolo Bonzini val = VIRTIO_NO_VECTOR;
46815377f6eSAkihiko Odaki }
46949ab747fSPaolo Bonzini virtio_queue_set_vector(vdev, vdev->queue_sel, val);
47049ab747fSPaolo Bonzini break;
47149ab747fSPaolo Bonzini default:
472a8218588SPhilippe Mathieu-Daudé qemu_log_mask(LOG_GUEST_ERROR,
473a8218588SPhilippe Mathieu-Daudé "%s: unexpected address 0x%x value 0x%x\n",
47449ab747fSPaolo Bonzini __func__, addr, val);
47549ab747fSPaolo Bonzini break;
47649ab747fSPaolo Bonzini }
47749ab747fSPaolo Bonzini }
47849ab747fSPaolo Bonzini
virtio_ioport_read(VirtIOPCIProxy * proxy,uint32_t addr)47949ab747fSPaolo Bonzini static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
48049ab747fSPaolo Bonzini {
481a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
48249ab747fSPaolo Bonzini uint32_t ret = 0xFFFFFFFF;
48349ab747fSPaolo Bonzini
48449ab747fSPaolo Bonzini switch (addr) {
48549ab747fSPaolo Bonzini case VIRTIO_PCI_HOST_FEATURES:
4866b8f1020SCornelia Huck ret = vdev->host_features;
48749ab747fSPaolo Bonzini break;
48849ab747fSPaolo Bonzini case VIRTIO_PCI_GUEST_FEATURES:
48949ab747fSPaolo Bonzini ret = vdev->guest_features;
49049ab747fSPaolo Bonzini break;
49149ab747fSPaolo Bonzini case VIRTIO_PCI_QUEUE_PFN:
49249ab747fSPaolo Bonzini ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
49349ab747fSPaolo Bonzini >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
49449ab747fSPaolo Bonzini break;
49549ab747fSPaolo Bonzini case VIRTIO_PCI_QUEUE_NUM:
49649ab747fSPaolo Bonzini ret = virtio_queue_get_num(vdev, vdev->queue_sel);
49749ab747fSPaolo Bonzini break;
49849ab747fSPaolo Bonzini case VIRTIO_PCI_QUEUE_SEL:
49949ab747fSPaolo Bonzini ret = vdev->queue_sel;
50049ab747fSPaolo Bonzini break;
50149ab747fSPaolo Bonzini case VIRTIO_PCI_STATUS:
50249ab747fSPaolo Bonzini ret = vdev->status;
50349ab747fSPaolo Bonzini break;
50449ab747fSPaolo Bonzini case VIRTIO_PCI_ISR:
50549ab747fSPaolo Bonzini /* reading from the ISR also clears it. */
506d73415a3SStefan Hajnoczi ret = qatomic_xchg(&vdev->isr, 0);
5079e64f8a3SMarcel Apfelbaum pci_irq_deassert(&proxy->pci_dev);
50849ab747fSPaolo Bonzini break;
50949ab747fSPaolo Bonzini case VIRTIO_MSI_CONFIG_VECTOR:
51049ab747fSPaolo Bonzini ret = vdev->config_vector;
51149ab747fSPaolo Bonzini break;
51249ab747fSPaolo Bonzini case VIRTIO_MSI_QUEUE_VECTOR:
51349ab747fSPaolo Bonzini ret = virtio_queue_vector(vdev, vdev->queue_sel);
51449ab747fSPaolo Bonzini break;
51549ab747fSPaolo Bonzini default:
51649ab747fSPaolo Bonzini break;
51749ab747fSPaolo Bonzini }
51849ab747fSPaolo Bonzini
51949ab747fSPaolo Bonzini return ret;
52049ab747fSPaolo Bonzini }
52149ab747fSPaolo Bonzini
virtio_pci_config_read(void * opaque,hwaddr addr,unsigned size)52249ab747fSPaolo Bonzini static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
52349ab747fSPaolo Bonzini unsigned size)
52449ab747fSPaolo Bonzini {
52549ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = opaque;
526a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
527cbbe4f50SMichael S. Tsirkin uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
52849ab747fSPaolo Bonzini uint64_t val = 0;
529bf697371SAndrew Melnychenko
530bf697371SAndrew Melnychenko if (vdev == NULL) {
531bf697371SAndrew Melnychenko return UINT64_MAX;
532bf697371SAndrew Melnychenko }
533bf697371SAndrew Melnychenko
53449ab747fSPaolo Bonzini if (addr < config) {
53549ab747fSPaolo Bonzini return virtio_ioport_read(proxy, addr);
53649ab747fSPaolo Bonzini }
53749ab747fSPaolo Bonzini addr -= config;
53849ab747fSPaolo Bonzini
53949ab747fSPaolo Bonzini switch (size) {
54049ab747fSPaolo Bonzini case 1:
541a3fc66d9SPaolo Bonzini val = virtio_config_readb(vdev, addr);
54249ab747fSPaolo Bonzini break;
54349ab747fSPaolo Bonzini case 2:
544a3fc66d9SPaolo Bonzini val = virtio_config_readw(vdev, addr);
545616a6552SGreg Kurz if (virtio_is_big_endian(vdev)) {
54649ab747fSPaolo Bonzini val = bswap16(val);
54749ab747fSPaolo Bonzini }
54849ab747fSPaolo Bonzini break;
54949ab747fSPaolo Bonzini case 4:
550a3fc66d9SPaolo Bonzini val = virtio_config_readl(vdev, addr);
551616a6552SGreg Kurz if (virtio_is_big_endian(vdev)) {
55249ab747fSPaolo Bonzini val = bswap32(val);
55349ab747fSPaolo Bonzini }
55449ab747fSPaolo Bonzini break;
55549ab747fSPaolo Bonzini }
55649ab747fSPaolo Bonzini return val;
55749ab747fSPaolo Bonzini }
55849ab747fSPaolo Bonzini
virtio_pci_config_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)55949ab747fSPaolo Bonzini static void virtio_pci_config_write(void *opaque, hwaddr addr,
56049ab747fSPaolo Bonzini uint64_t val, unsigned size)
56149ab747fSPaolo Bonzini {
56249ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = opaque;
563cbbe4f50SMichael S. Tsirkin uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
564a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
565bf697371SAndrew Melnychenko
566bf697371SAndrew Melnychenko if (vdev == NULL) {
567bf697371SAndrew Melnychenko return;
568bf697371SAndrew Melnychenko }
569bf697371SAndrew Melnychenko
57049ab747fSPaolo Bonzini if (addr < config) {
57149ab747fSPaolo Bonzini virtio_ioport_write(proxy, addr, val);
57249ab747fSPaolo Bonzini return;
57349ab747fSPaolo Bonzini }
57449ab747fSPaolo Bonzini addr -= config;
57549ab747fSPaolo Bonzini /*
57649ab747fSPaolo Bonzini * Virtio-PCI is odd. Ioports are LE but config space is target native
57749ab747fSPaolo Bonzini * endian.
57849ab747fSPaolo Bonzini */
57949ab747fSPaolo Bonzini switch (size) {
58049ab747fSPaolo Bonzini case 1:
581a3fc66d9SPaolo Bonzini virtio_config_writeb(vdev, addr, val);
58249ab747fSPaolo Bonzini break;
58349ab747fSPaolo Bonzini case 2:
584616a6552SGreg Kurz if (virtio_is_big_endian(vdev)) {
58549ab747fSPaolo Bonzini val = bswap16(val);
58649ab747fSPaolo Bonzini }
587a3fc66d9SPaolo Bonzini virtio_config_writew(vdev, addr, val);
58849ab747fSPaolo Bonzini break;
58949ab747fSPaolo Bonzini case 4:
590616a6552SGreg Kurz if (virtio_is_big_endian(vdev)) {
59149ab747fSPaolo Bonzini val = bswap32(val);
59249ab747fSPaolo Bonzini }
593a3fc66d9SPaolo Bonzini virtio_config_writel(vdev, addr, val);
59449ab747fSPaolo Bonzini break;
59549ab747fSPaolo Bonzini }
59649ab747fSPaolo Bonzini }
59749ab747fSPaolo Bonzini
59849ab747fSPaolo Bonzini static const MemoryRegionOps virtio_pci_config_ops = {
59949ab747fSPaolo Bonzini .read = virtio_pci_config_read,
60049ab747fSPaolo Bonzini .write = virtio_pci_config_write,
60149ab747fSPaolo Bonzini .impl = {
60249ab747fSPaolo Bonzini .min_access_size = 1,
60349ab747fSPaolo Bonzini .max_access_size = 4,
60449ab747fSPaolo Bonzini },
60549ab747fSPaolo Bonzini .endianness = DEVICE_LITTLE_ENDIAN,
60649ab747fSPaolo Bonzini };
60749ab747fSPaolo Bonzini
virtio_address_space_lookup(VirtIOPCIProxy * proxy,hwaddr * off,int len)608a93c8d82SAlexey Kardashevskiy static MemoryRegion *virtio_address_space_lookup(VirtIOPCIProxy *proxy,
609a93c8d82SAlexey Kardashevskiy hwaddr *off, int len)
610a93c8d82SAlexey Kardashevskiy {
611a93c8d82SAlexey Kardashevskiy int i;
612a93c8d82SAlexey Kardashevskiy VirtIOPCIRegion *reg;
613a93c8d82SAlexey Kardashevskiy
614a93c8d82SAlexey Kardashevskiy for (i = 0; i < ARRAY_SIZE(proxy->regs); ++i) {
615a93c8d82SAlexey Kardashevskiy reg = &proxy->regs[i];
616a93c8d82SAlexey Kardashevskiy if (*off >= reg->offset &&
617a93c8d82SAlexey Kardashevskiy *off + len <= reg->offset + reg->size) {
618ffa8a3e3SGao Shiyuan MemoryRegionSection mrs = memory_region_find(®->mr,
619ffa8a3e3SGao Shiyuan *off - reg->offset, len);
620ffa8a3e3SGao Shiyuan assert(mrs.mr);
621ffa8a3e3SGao Shiyuan *off = mrs.offset_within_region;
622ffa8a3e3SGao Shiyuan memory_region_unref(mrs.mr);
623ffa8a3e3SGao Shiyuan return mrs.mr;
624a93c8d82SAlexey Kardashevskiy }
625a93c8d82SAlexey Kardashevskiy }
626a93c8d82SAlexey Kardashevskiy
627a93c8d82SAlexey Kardashevskiy return NULL;
628a93c8d82SAlexey Kardashevskiy }
629a93c8d82SAlexey Kardashevskiy
6301e40356cSMichael S. Tsirkin /* Below are generic functions to do memcpy from/to an address space,
6311e40356cSMichael S. Tsirkin * without byteswaps, with input validation.
6321e40356cSMichael S. Tsirkin *
6331e40356cSMichael S. Tsirkin * As regular address_space_* APIs all do some kind of byteswap at least for
6341e40356cSMichael S. Tsirkin * some host/target combinations, we are forced to explicitly convert to a
6351e40356cSMichael S. Tsirkin * known-endianness integer value.
6361e40356cSMichael S. Tsirkin * It doesn't really matter which endian format to go through, so the code
6371e40356cSMichael S. Tsirkin * below selects the endian that causes the least amount of work on the given
6381e40356cSMichael S. Tsirkin * host.
6391e40356cSMichael S. Tsirkin *
6401e40356cSMichael S. Tsirkin * Note: host pointer must be aligned.
6411e40356cSMichael S. Tsirkin */
6421e40356cSMichael S. Tsirkin static
virtio_address_space_write(VirtIOPCIProxy * proxy,hwaddr addr,const uint8_t * buf,int len)643a93c8d82SAlexey Kardashevskiy void virtio_address_space_write(VirtIOPCIProxy *proxy, hwaddr addr,
6441e40356cSMichael S. Tsirkin const uint8_t *buf, int len)
6451e40356cSMichael S. Tsirkin {
646a93c8d82SAlexey Kardashevskiy uint64_t val;
647a93c8d82SAlexey Kardashevskiy MemoryRegion *mr;
6481e40356cSMichael S. Tsirkin
6491e40356cSMichael S. Tsirkin /* address_space_* APIs assume an aligned address.
6501e40356cSMichael S. Tsirkin * As address is under guest control, handle illegal values.
6511e40356cSMichael S. Tsirkin */
6521e40356cSMichael S. Tsirkin addr &= ~(len - 1);
6531e40356cSMichael S. Tsirkin
654a93c8d82SAlexey Kardashevskiy mr = virtio_address_space_lookup(proxy, &addr, len);
655a93c8d82SAlexey Kardashevskiy if (!mr) {
656a93c8d82SAlexey Kardashevskiy return;
657a93c8d82SAlexey Kardashevskiy }
658a93c8d82SAlexey Kardashevskiy
6591e40356cSMichael S. Tsirkin /* Make sure caller aligned buf properly */
6601e40356cSMichael S. Tsirkin assert(!(((uintptr_t)buf) & (len - 1)));
6611e40356cSMichael S. Tsirkin
6621e40356cSMichael S. Tsirkin switch (len) {
6631e40356cSMichael S. Tsirkin case 1:
6641e40356cSMichael S. Tsirkin val = pci_get_byte(buf);
6651e40356cSMichael S. Tsirkin break;
6661e40356cSMichael S. Tsirkin case 2:
6679bf825bfSTony Nguyen val = pci_get_word(buf);
6681e40356cSMichael S. Tsirkin break;
6691e40356cSMichael S. Tsirkin case 4:
6709bf825bfSTony Nguyen val = pci_get_long(buf);
6711e40356cSMichael S. Tsirkin break;
6721e40356cSMichael S. Tsirkin default:
6731e40356cSMichael S. Tsirkin /* As length is under guest control, handle illegal values. */
674a93c8d82SAlexey Kardashevskiy return;
6751e40356cSMichael S. Tsirkin }
676d5d680caSTony Nguyen memory_region_dispatch_write(mr, addr, val, size_memop(len) | MO_LE,
677062c08d1STony Nguyen MEMTXATTRS_UNSPECIFIED);
6781e40356cSMichael S. Tsirkin }
6791e40356cSMichael S. Tsirkin
6801e40356cSMichael S. Tsirkin static void
virtio_address_space_read(VirtIOPCIProxy * proxy,hwaddr addr,uint8_t * buf,int len)681a93c8d82SAlexey Kardashevskiy virtio_address_space_read(VirtIOPCIProxy *proxy, hwaddr addr,
682a93c8d82SAlexey Kardashevskiy uint8_t *buf, int len)
6831e40356cSMichael S. Tsirkin {
684a93c8d82SAlexey Kardashevskiy uint64_t val;
685a93c8d82SAlexey Kardashevskiy MemoryRegion *mr;
6861e40356cSMichael S. Tsirkin
6871e40356cSMichael S. Tsirkin /* address_space_* APIs assume an aligned address.
6881e40356cSMichael S. Tsirkin * As address is under guest control, handle illegal values.
6891e40356cSMichael S. Tsirkin */
6901e40356cSMichael S. Tsirkin addr &= ~(len - 1);
6911e40356cSMichael S. Tsirkin
692a93c8d82SAlexey Kardashevskiy mr = virtio_address_space_lookup(proxy, &addr, len);
693a93c8d82SAlexey Kardashevskiy if (!mr) {
694a93c8d82SAlexey Kardashevskiy return;
695a93c8d82SAlexey Kardashevskiy }
696a93c8d82SAlexey Kardashevskiy
6971e40356cSMichael S. Tsirkin /* Make sure caller aligned buf properly */
6981e40356cSMichael S. Tsirkin assert(!(((uintptr_t)buf) & (len - 1)));
6991e40356cSMichael S. Tsirkin
700d5d680caSTony Nguyen memory_region_dispatch_read(mr, addr, &val, size_memop(len) | MO_LE,
701062c08d1STony Nguyen MEMTXATTRS_UNSPECIFIED);
7021e40356cSMichael S. Tsirkin switch (len) {
7031e40356cSMichael S. Tsirkin case 1:
7041e40356cSMichael S. Tsirkin pci_set_byte(buf, val);
7051e40356cSMichael S. Tsirkin break;
7061e40356cSMichael S. Tsirkin case 2:
7079bf825bfSTony Nguyen pci_set_word(buf, val);
7081e40356cSMichael S. Tsirkin break;
7091e40356cSMichael S. Tsirkin case 4:
7109bf825bfSTony Nguyen pci_set_long(buf, val);
7111e40356cSMichael S. Tsirkin break;
7121e40356cSMichael S. Tsirkin default:
7131e40356cSMichael S. Tsirkin /* As length is under guest control, handle illegal values. */
7141e40356cSMichael S. Tsirkin break;
7151e40356cSMichael S. Tsirkin }
7161e40356cSMichael S. Tsirkin }
7171e40356cSMichael S. Tsirkin
virtio_pci_ats_ctrl_trigger(PCIDevice * pci_dev,bool enable)718206e91d1SViktor Prutyanov static void virtio_pci_ats_ctrl_trigger(PCIDevice *pci_dev, bool enable)
719206e91d1SViktor Prutyanov {
720206e91d1SViktor Prutyanov VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
721206e91d1SViktor Prutyanov VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
722206e91d1SViktor Prutyanov VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
723206e91d1SViktor Prutyanov
724206e91d1SViktor Prutyanov vdev->device_iotlb_enabled = enable;
725206e91d1SViktor Prutyanov
726206e91d1SViktor Prutyanov if (k->toggle_device_iotlb) {
727206e91d1SViktor Prutyanov k->toggle_device_iotlb(vdev);
728206e91d1SViktor Prutyanov }
729206e91d1SViktor Prutyanov }
730206e91d1SViktor Prutyanov
pcie_ats_config_write(PCIDevice * dev,uint32_t address,uint32_t val,int len)731206e91d1SViktor Prutyanov static void pcie_ats_config_write(PCIDevice *dev, uint32_t address,
732206e91d1SViktor Prutyanov uint32_t val, int len)
733206e91d1SViktor Prutyanov {
734206e91d1SViktor Prutyanov uint32_t off;
735206e91d1SViktor Prutyanov uint16_t ats_cap = dev->exp.ats_cap;
736206e91d1SViktor Prutyanov
737206e91d1SViktor Prutyanov if (!ats_cap || address < ats_cap) {
738206e91d1SViktor Prutyanov return;
739206e91d1SViktor Prutyanov }
740206e91d1SViktor Prutyanov off = address - ats_cap;
741206e91d1SViktor Prutyanov if (off >= PCI_EXT_CAP_ATS_SIZEOF) {
742206e91d1SViktor Prutyanov return;
743206e91d1SViktor Prutyanov }
744206e91d1SViktor Prutyanov
745206e91d1SViktor Prutyanov if (range_covers_byte(off, len, PCI_ATS_CTRL + 1)) {
746206e91d1SViktor Prutyanov virtio_pci_ats_ctrl_trigger(dev, !!(val & PCI_ATS_CTRL_ENABLE));
747206e91d1SViktor Prutyanov }
748206e91d1SViktor Prutyanov }
749206e91d1SViktor Prutyanov
virtio_write_config(PCIDevice * pci_dev,uint32_t address,uint32_t val,int len)75049ab747fSPaolo Bonzini static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
75149ab747fSPaolo Bonzini uint32_t val, int len)
75249ab747fSPaolo Bonzini {
7533f262b26SLi Qiang VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
754a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
755ada434cdSMichael S. Tsirkin struct virtio_pci_cfg_cap *cfg;
75649ab747fSPaolo Bonzini
75749ab747fSPaolo Bonzini pci_default_write_config(pci_dev, address, val, len);
75849ab747fSPaolo Bonzini
759eb1556c4SJulia Suvorova if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) {
760eb1556c4SJulia Suvorova pcie_cap_flr_write_config(pci_dev, address, val, len);
761eb1556c4SJulia Suvorova }
762eb1556c4SJulia Suvorova
763206e91d1SViktor Prutyanov if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
764206e91d1SViktor Prutyanov pcie_ats_config_write(pci_dev, address, val, len);
765206e91d1SViktor Prutyanov }
766206e91d1SViktor Prutyanov
7679d7bd082SMichael Roth if (range_covers_byte(address, len, PCI_COMMAND)) {
7689d7bd082SMichael Roth if (!(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
7699d7bd082SMichael Roth virtio_set_disabled(vdev, true);
77049ab747fSPaolo Bonzini virtio_pci_stop_ioeventfd(proxy);
77145363e46SMichael S. Tsirkin virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
7729d7bd082SMichael Roth } else {
7739d7bd082SMichael Roth virtio_set_disabled(vdev, false);
7749d7bd082SMichael Roth }
77549ab747fSPaolo Bonzini }
776ada434cdSMichael S. Tsirkin
777ada434cdSMichael S. Tsirkin if (proxy->config_cap &&
778ada434cdSMichael S. Tsirkin ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
779ada434cdSMichael S. Tsirkin pci_cfg_data),
780ada434cdSMichael S. Tsirkin sizeof cfg->pci_cfg_data)) {
781ada434cdSMichael S. Tsirkin uint32_t off;
7825ae80e62SThomas Huth uint32_t caplen;
783ada434cdSMichael S. Tsirkin
784ada434cdSMichael S. Tsirkin cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
785ada434cdSMichael S. Tsirkin off = le32_to_cpu(cfg->cap.offset);
7865ae80e62SThomas Huth caplen = le32_to_cpu(cfg->cap.length);
787ada434cdSMichael S. Tsirkin
7885ae80e62SThomas Huth if (caplen == 1 || caplen == 2 || caplen == 4) {
7895ae80e62SThomas Huth assert(caplen <= sizeof cfg->pci_cfg_data);
7905ae80e62SThomas Huth virtio_address_space_write(proxy, off, cfg->pci_cfg_data, caplen);
791ada434cdSMichael S. Tsirkin }
792ada434cdSMichael S. Tsirkin }
793ada434cdSMichael S. Tsirkin }
794ada434cdSMichael S. Tsirkin
virtio_read_config(PCIDevice * pci_dev,uint32_t address,int len)795ada434cdSMichael S. Tsirkin static uint32_t virtio_read_config(PCIDevice *pci_dev,
796ada434cdSMichael S. Tsirkin uint32_t address, int len)
797ada434cdSMichael S. Tsirkin {
7983f262b26SLi Qiang VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
799ada434cdSMichael S. Tsirkin struct virtio_pci_cfg_cap *cfg;
800ada434cdSMichael S. Tsirkin
801ada434cdSMichael S. Tsirkin if (proxy->config_cap &&
802ada434cdSMichael S. Tsirkin ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
803ada434cdSMichael S. Tsirkin pci_cfg_data),
804ada434cdSMichael S. Tsirkin sizeof cfg->pci_cfg_data)) {
805ada434cdSMichael S. Tsirkin uint32_t off;
8065ae80e62SThomas Huth uint32_t caplen;
807ada434cdSMichael S. Tsirkin
808ada434cdSMichael S. Tsirkin cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
809ada434cdSMichael S. Tsirkin off = le32_to_cpu(cfg->cap.offset);
8105ae80e62SThomas Huth caplen = le32_to_cpu(cfg->cap.length);
811ada434cdSMichael S. Tsirkin
8125ae80e62SThomas Huth if (caplen == 1 || caplen == 2 || caplen == 4) {
8135ae80e62SThomas Huth assert(caplen <= sizeof cfg->pci_cfg_data);
8145ae80e62SThomas Huth virtio_address_space_read(proxy, off, cfg->pci_cfg_data, caplen);
815ada434cdSMichael S. Tsirkin }
816ada434cdSMichael S. Tsirkin }
817ada434cdSMichael S. Tsirkin
818ada434cdSMichael S. Tsirkin return pci_default_read_config(pci_dev, address, len);
81949ab747fSPaolo Bonzini }
82049ab747fSPaolo Bonzini
kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy * proxy,unsigned int vector)82149ab747fSPaolo Bonzini static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
822d1f6af6aSPeter Xu unsigned int vector)
82349ab747fSPaolo Bonzini {
82449ab747fSPaolo Bonzini VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
82549ab747fSPaolo Bonzini int ret;
82649ab747fSPaolo Bonzini
82749ab747fSPaolo Bonzini if (irqfd->users == 0) {
828def4c557SLongpeng(Mike) KVMRouteChange c = kvm_irqchip_begin_route_changes(kvm_state);
829def4c557SLongpeng(Mike) ret = kvm_irqchip_add_msi_route(&c, vector, &proxy->pci_dev);
83049ab747fSPaolo Bonzini if (ret < 0) {
83149ab747fSPaolo Bonzini return ret;
83249ab747fSPaolo Bonzini }
833def4c557SLongpeng(Mike) kvm_irqchip_commit_route_changes(&c);
83449ab747fSPaolo Bonzini irqfd->virq = ret;
83549ab747fSPaolo Bonzini }
83649ab747fSPaolo Bonzini irqfd->users++;
83749ab747fSPaolo Bonzini return 0;
83849ab747fSPaolo Bonzini }
83949ab747fSPaolo Bonzini
kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy * proxy,unsigned int vector)84049ab747fSPaolo Bonzini static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
84149ab747fSPaolo Bonzini unsigned int vector)
84249ab747fSPaolo Bonzini {
84349ab747fSPaolo Bonzini VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
84449ab747fSPaolo Bonzini if (--irqfd->users == 0) {
84549ab747fSPaolo Bonzini kvm_irqchip_release_virq(kvm_state, irqfd->virq);
84649ab747fSPaolo Bonzini }
84749ab747fSPaolo Bonzini }
84849ab747fSPaolo Bonzini
kvm_virtio_pci_irqfd_use(VirtIOPCIProxy * proxy,EventNotifier * n,unsigned int vector)84949ab747fSPaolo Bonzini static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
8502e07f69dSCindy Lu EventNotifier *n,
85149ab747fSPaolo Bonzini unsigned int vector)
85249ab747fSPaolo Bonzini {
85349ab747fSPaolo Bonzini VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
8549be38598SEduardo Habkost return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
85549ab747fSPaolo Bonzini }
85649ab747fSPaolo Bonzini
kvm_virtio_pci_irqfd_release(VirtIOPCIProxy * proxy,EventNotifier * n,unsigned int vector)85749ab747fSPaolo Bonzini static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
8582e07f69dSCindy Lu EventNotifier *n ,
85949ab747fSPaolo Bonzini unsigned int vector)
86049ab747fSPaolo Bonzini {
86149ab747fSPaolo Bonzini VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
86249ab747fSPaolo Bonzini int ret;
86349ab747fSPaolo Bonzini
8641c9b71a7SEric Auger ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
86549ab747fSPaolo Bonzini assert(ret == 0);
86649ab747fSPaolo Bonzini }
virtio_pci_get_notifier(VirtIOPCIProxy * proxy,int queue_no,EventNotifier ** n,unsigned int * vector)8672e07f69dSCindy Lu static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
8682e07f69dSCindy Lu EventNotifier **n, unsigned int *vector)
8692e07f69dSCindy Lu {
8702e07f69dSCindy Lu VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
8712e07f69dSCindy Lu VirtQueue *vq;
8722e07f69dSCindy Lu
873a8e63ff2SCindy Lu if (!proxy->vector_irqfd && vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)
874a8e63ff2SCindy Lu return -1;
875a8e63ff2SCindy Lu
8762e07f69dSCindy Lu if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
87716805428SCindy Lu *n = virtio_config_get_guest_notifier(vdev);
87816805428SCindy Lu *vector = vdev->config_vector;
8792e07f69dSCindy Lu } else {
8802e07f69dSCindy Lu if (!virtio_queue_get_num(vdev, queue_no)) {
8812e07f69dSCindy Lu return -1;
8822e07f69dSCindy Lu }
8832e07f69dSCindy Lu *vector = virtio_queue_vector(vdev, queue_no);
8842e07f69dSCindy Lu vq = virtio_get_queue(vdev, queue_no);
8852e07f69dSCindy Lu *n = virtio_queue_get_guest_notifier(vq);
8862e07f69dSCindy Lu }
8872e07f69dSCindy Lu return 0;
8882e07f69dSCindy Lu }
88949ab747fSPaolo Bonzini
kvm_virtio_pci_vector_use_one(VirtIOPCIProxy * proxy,int queue_no)890ee3b8dc6SCindy Lu static int kvm_virtio_pci_vector_use_one(VirtIOPCIProxy *proxy, int queue_no)
89149ab747fSPaolo Bonzini {
892ee3b8dc6SCindy Lu unsigned int vector;
893ee3b8dc6SCindy Lu int ret;
894ee3b8dc6SCindy Lu EventNotifier *n;
89549ab747fSPaolo Bonzini PCIDevice *dev = &proxy->pci_dev;
896a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
897181103cdSKONRAD Frederic VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
898ee3b8dc6SCindy Lu
8992e07f69dSCindy Lu ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
9002e07f69dSCindy Lu if (ret < 0) {
901ee3b8dc6SCindy Lu return ret;
9022e07f69dSCindy Lu }
90349ab747fSPaolo Bonzini if (vector >= msix_nr_vectors_allocated(dev)) {
904ee3b8dc6SCindy Lu return 0;
90549ab747fSPaolo Bonzini }
906ee3b8dc6SCindy Lu ret = kvm_virtio_pci_vq_vector_use(proxy, vector);
90749ab747fSPaolo Bonzini if (ret < 0) {
908a113d041SCindy Lu return ret;
90949ab747fSPaolo Bonzini }
910ee3b8dc6SCindy Lu /*
911ee3b8dc6SCindy Lu * If guest supports masking, set up irqfd now.
91249ab747fSPaolo Bonzini * Otherwise, delay until unmasked in the frontend.
91349ab747fSPaolo Bonzini */
9145669655aSVictor Kaplansky if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
9152e07f69dSCindy Lu ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
91649ab747fSPaolo Bonzini if (ret < 0) {
91749ab747fSPaolo Bonzini kvm_virtio_pci_vq_vector_release(proxy, vector);
918a113d041SCindy Lu return ret;
91949ab747fSPaolo Bonzini }
92049ab747fSPaolo Bonzini }
921316011b8SCindy Lu
922ee3b8dc6SCindy Lu return 0;
923ee3b8dc6SCindy Lu }
kvm_virtio_pci_vector_vq_use(VirtIOPCIProxy * proxy,int nvqs)92416805428SCindy Lu static int kvm_virtio_pci_vector_vq_use(VirtIOPCIProxy *proxy, int nvqs)
925ee3b8dc6SCindy Lu {
926ee3b8dc6SCindy Lu int queue_no;
927ee3b8dc6SCindy Lu int ret = 0;
928ee3b8dc6SCindy Lu VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
929ee3b8dc6SCindy Lu
930ee3b8dc6SCindy Lu for (queue_no = 0; queue_no < nvqs; queue_no++) {
931ee3b8dc6SCindy Lu if (!virtio_queue_get_num(vdev, queue_no)) {
932ee3b8dc6SCindy Lu return -1;
933ee3b8dc6SCindy Lu }
934ee3b8dc6SCindy Lu ret = kvm_virtio_pci_vector_use_one(proxy, queue_no);
93549ab747fSPaolo Bonzini }
93638ce4051SMichael S. Tsirkin return ret;
93738ce4051SMichael S. Tsirkin }
938316011b8SCindy Lu
kvm_virtio_pci_vector_config_use(VirtIOPCIProxy * proxy)93916805428SCindy Lu static int kvm_virtio_pci_vector_config_use(VirtIOPCIProxy *proxy)
94016805428SCindy Lu {
94116805428SCindy Lu return kvm_virtio_pci_vector_use_one(proxy, VIRTIO_CONFIG_IRQ_IDX);
94216805428SCindy Lu }
943ee3b8dc6SCindy Lu
kvm_virtio_pci_vector_release_one(VirtIOPCIProxy * proxy,int queue_no)944ee3b8dc6SCindy Lu static void kvm_virtio_pci_vector_release_one(VirtIOPCIProxy *proxy,
945ee3b8dc6SCindy Lu int queue_no)
946316011b8SCindy Lu {
947316011b8SCindy Lu VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
94838ce4051SMichael S. Tsirkin unsigned int vector;
9492e07f69dSCindy Lu EventNotifier *n;
9502e07f69dSCindy Lu int ret;
951ee3b8dc6SCindy Lu VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
952ee3b8dc6SCindy Lu PCIDevice *dev = &proxy->pci_dev;
953ee3b8dc6SCindy Lu
9542e07f69dSCindy Lu ret = virtio_pci_get_notifier(proxy, queue_no, &n, &vector);
9552e07f69dSCindy Lu if (ret < 0) {
956ee3b8dc6SCindy Lu return;
9572e07f69dSCindy Lu }
95838ce4051SMichael S. Tsirkin if (vector >= msix_nr_vectors_allocated(dev)) {
959ee3b8dc6SCindy Lu return;
96038ce4051SMichael S. Tsirkin }
96138ce4051SMichael S. Tsirkin if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
9622e07f69dSCindy Lu kvm_virtio_pci_irqfd_release(proxy, n, vector);
96338ce4051SMichael S. Tsirkin }
96438ce4051SMichael S. Tsirkin kvm_virtio_pci_vq_vector_release(proxy, vector);
965316011b8SCindy Lu }
966ee3b8dc6SCindy Lu
kvm_virtio_pci_vector_vq_release(VirtIOPCIProxy * proxy,int nvqs)96716805428SCindy Lu static void kvm_virtio_pci_vector_vq_release(VirtIOPCIProxy *proxy, int nvqs)
968ee3b8dc6SCindy Lu {
969ee3b8dc6SCindy Lu int queue_no;
970ee3b8dc6SCindy Lu VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
971ee3b8dc6SCindy Lu
972ee3b8dc6SCindy Lu for (queue_no = 0; queue_no < nvqs; queue_no++) {
973ee3b8dc6SCindy Lu if (!virtio_queue_get_num(vdev, queue_no)) {
974ee3b8dc6SCindy Lu break;
975ee3b8dc6SCindy Lu }
976ee3b8dc6SCindy Lu kvm_virtio_pci_vector_release_one(proxy, queue_no);
977ee3b8dc6SCindy Lu }
97849ab747fSPaolo Bonzini }
97949ab747fSPaolo Bonzini
kvm_virtio_pci_vector_config_release(VirtIOPCIProxy * proxy)98016805428SCindy Lu static void kvm_virtio_pci_vector_config_release(VirtIOPCIProxy *proxy)
98116805428SCindy Lu {
98216805428SCindy Lu kvm_virtio_pci_vector_release_one(proxy, VIRTIO_CONFIG_IRQ_IDX);
98316805428SCindy Lu }
98416805428SCindy Lu
virtio_pci_one_vector_unmask(VirtIOPCIProxy * proxy,unsigned int queue_no,unsigned int vector,MSIMessage msg,EventNotifier * n)9852e07f69dSCindy Lu static int virtio_pci_one_vector_unmask(VirtIOPCIProxy *proxy,
98649ab747fSPaolo Bonzini unsigned int queue_no,
98749ab747fSPaolo Bonzini unsigned int vector,
9882e07f69dSCindy Lu MSIMessage msg,
9892e07f69dSCindy Lu EventNotifier *n)
99049ab747fSPaolo Bonzini {
991a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
992a3fc66d9SPaolo Bonzini VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
99349ab747fSPaolo Bonzini VirtIOIRQFD *irqfd;
99449ab747fSPaolo Bonzini int ret = 0;
99549ab747fSPaolo Bonzini
99649ab747fSPaolo Bonzini if (proxy->vector_irqfd) {
99749ab747fSPaolo Bonzini irqfd = &proxy->vector_irqfd[vector];
99849ab747fSPaolo Bonzini if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
999dc9f06caSPavel Fedin ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
1000dc9f06caSPavel Fedin &proxy->pci_dev);
100149ab747fSPaolo Bonzini if (ret < 0) {
100249ab747fSPaolo Bonzini return ret;
100349ab747fSPaolo Bonzini }
10043f1fea0fSPeter Xu kvm_irqchip_commit_routes(kvm_state);
100549ab747fSPaolo Bonzini }
100649ab747fSPaolo Bonzini }
100749ab747fSPaolo Bonzini
100849ab747fSPaolo Bonzini /* If guest supports masking, irqfd is already setup, unmask it.
100949ab747fSPaolo Bonzini * Otherwise, set it up now.
101049ab747fSPaolo Bonzini */
10115669655aSVictor Kaplansky if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
1012a3fc66d9SPaolo Bonzini k->guest_notifier_mask(vdev, queue_no, false);
101349ab747fSPaolo Bonzini /* Test after unmasking to avoid losing events. */
1014181103cdSKONRAD Frederic if (k->guest_notifier_pending &&
1015a3fc66d9SPaolo Bonzini k->guest_notifier_pending(vdev, queue_no)) {
101649ab747fSPaolo Bonzini event_notifier_set(n);
101749ab747fSPaolo Bonzini }
101849ab747fSPaolo Bonzini } else {
10192e07f69dSCindy Lu ret = kvm_virtio_pci_irqfd_use(proxy, n, vector);
102049ab747fSPaolo Bonzini }
102149ab747fSPaolo Bonzini return ret;
102249ab747fSPaolo Bonzini }
102349ab747fSPaolo Bonzini
virtio_pci_one_vector_mask(VirtIOPCIProxy * proxy,unsigned int queue_no,unsigned int vector,EventNotifier * n)10242e07f69dSCindy Lu static void virtio_pci_one_vector_mask(VirtIOPCIProxy *proxy,
102549ab747fSPaolo Bonzini unsigned int queue_no,
10262e07f69dSCindy Lu unsigned int vector,
10272e07f69dSCindy Lu EventNotifier *n)
102849ab747fSPaolo Bonzini {
1029a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1030a3fc66d9SPaolo Bonzini VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1031181103cdSKONRAD Frederic
103249ab747fSPaolo Bonzini /* If guest supports masking, keep irqfd but mask it.
103349ab747fSPaolo Bonzini * Otherwise, clean it up now.
103449ab747fSPaolo Bonzini */
10355669655aSVictor Kaplansky if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
1036a3fc66d9SPaolo Bonzini k->guest_notifier_mask(vdev, queue_no, true);
103749ab747fSPaolo Bonzini } else {
10382e07f69dSCindy Lu kvm_virtio_pci_irqfd_release(proxy, n, vector);
103949ab747fSPaolo Bonzini }
104049ab747fSPaolo Bonzini }
104149ab747fSPaolo Bonzini
virtio_pci_vector_unmask(PCIDevice * dev,unsigned vector,MSIMessage msg)104249ab747fSPaolo Bonzini static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
104349ab747fSPaolo Bonzini MSIMessage msg)
104449ab747fSPaolo Bonzini {
104549ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
1046a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1047851c2a75SJason Wang VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
10482e07f69dSCindy Lu EventNotifier *n;
1049851c2a75SJason Wang int ret, index, unmasked = 0;
105049ab747fSPaolo Bonzini
1051851c2a75SJason Wang while (vq) {
1052851c2a75SJason Wang index = virtio_get_queue_index(vq);
1053851c2a75SJason Wang if (!virtio_queue_get_num(vdev, index)) {
105449ab747fSPaolo Bonzini break;
105549ab747fSPaolo Bonzini }
10566652d081SJason Wang if (index < proxy->nvqs_with_notifiers) {
10572e07f69dSCindy Lu n = virtio_queue_get_guest_notifier(vq);
10582e07f69dSCindy Lu ret = virtio_pci_one_vector_unmask(proxy, index, vector, msg, n);
105949ab747fSPaolo Bonzini if (ret < 0) {
106049ab747fSPaolo Bonzini goto undo;
106149ab747fSPaolo Bonzini }
1062851c2a75SJason Wang ++unmasked;
106349ab747fSPaolo Bonzini }
10646652d081SJason Wang vq = virtio_vector_next_queue(vq);
10656652d081SJason Wang }
106616805428SCindy Lu /* unmask config intr */
106716805428SCindy Lu if (vector == vdev->config_vector) {
106816805428SCindy Lu n = virtio_config_get_guest_notifier(vdev);
106916805428SCindy Lu ret = virtio_pci_one_vector_unmask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector,
107016805428SCindy Lu msg, n);
107116805428SCindy Lu if (ret < 0) {
107216805428SCindy Lu goto undo_config;
107316805428SCindy Lu }
107416805428SCindy Lu }
107549ab747fSPaolo Bonzini return 0;
107616805428SCindy Lu undo_config:
107716805428SCindy Lu n = virtio_config_get_guest_notifier(vdev);
107816805428SCindy Lu virtio_pci_one_vector_mask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector, n);
107949ab747fSPaolo Bonzini undo:
1080851c2a75SJason Wang vq = virtio_vector_first_queue(vdev, vector);
10816652d081SJason Wang while (vq && unmasked >= 0) {
1082851c2a75SJason Wang index = virtio_get_queue_index(vq);
10836652d081SJason Wang if (index < proxy->nvqs_with_notifiers) {
10842e07f69dSCindy Lu n = virtio_queue_get_guest_notifier(vq);
10852e07f69dSCindy Lu virtio_pci_one_vector_mask(proxy, index, vector, n);
10866652d081SJason Wang --unmasked;
10876652d081SJason Wang }
1088851c2a75SJason Wang vq = virtio_vector_next_queue(vq);
108949ab747fSPaolo Bonzini }
109049ab747fSPaolo Bonzini return ret;
109149ab747fSPaolo Bonzini }
109249ab747fSPaolo Bonzini
virtio_pci_vector_mask(PCIDevice * dev,unsigned vector)109349ab747fSPaolo Bonzini static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
109449ab747fSPaolo Bonzini {
109549ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
1096a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1097851c2a75SJason Wang VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
10982e07f69dSCindy Lu EventNotifier *n;
1099851c2a75SJason Wang int index;
110049ab747fSPaolo Bonzini
1101851c2a75SJason Wang while (vq) {
1102851c2a75SJason Wang index = virtio_get_queue_index(vq);
11032e07f69dSCindy Lu n = virtio_queue_get_guest_notifier(vq);
1104851c2a75SJason Wang if (!virtio_queue_get_num(vdev, index)) {
110549ab747fSPaolo Bonzini break;
110649ab747fSPaolo Bonzini }
11076652d081SJason Wang if (index < proxy->nvqs_with_notifiers) {
11082e07f69dSCindy Lu virtio_pci_one_vector_mask(proxy, index, vector, n);
11096652d081SJason Wang }
1110851c2a75SJason Wang vq = virtio_vector_next_queue(vq);
111149ab747fSPaolo Bonzini }
111216805428SCindy Lu
111316805428SCindy Lu if (vector == vdev->config_vector) {
111416805428SCindy Lu n = virtio_config_get_guest_notifier(vdev);
111516805428SCindy Lu virtio_pci_one_vector_mask(proxy, VIRTIO_CONFIG_IRQ_IDX, vector, n);
111616805428SCindy Lu }
111749ab747fSPaolo Bonzini }
111849ab747fSPaolo Bonzini
virtio_pci_vector_poll(PCIDevice * dev,unsigned int vector_start,unsigned int vector_end)111949ab747fSPaolo Bonzini static void virtio_pci_vector_poll(PCIDevice *dev,
112049ab747fSPaolo Bonzini unsigned int vector_start,
112149ab747fSPaolo Bonzini unsigned int vector_end)
112249ab747fSPaolo Bonzini {
112349ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
1124a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1125181103cdSKONRAD Frederic VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
112649ab747fSPaolo Bonzini int queue_no;
112749ab747fSPaolo Bonzini unsigned int vector;
112849ab747fSPaolo Bonzini EventNotifier *notifier;
11292e07f69dSCindy Lu int ret;
113049ab747fSPaolo Bonzini
113149ab747fSPaolo Bonzini for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
11322e07f69dSCindy Lu ret = virtio_pci_get_notifier(proxy, queue_no, ¬ifier, &vector);
11332e07f69dSCindy Lu if (ret < 0) {
113449ab747fSPaolo Bonzini break;
113549ab747fSPaolo Bonzini }
113649ab747fSPaolo Bonzini if (vector < vector_start || vector >= vector_end ||
113749ab747fSPaolo Bonzini !msix_is_masked(dev, vector)) {
113849ab747fSPaolo Bonzini continue;
113949ab747fSPaolo Bonzini }
1140181103cdSKONRAD Frederic if (k->guest_notifier_pending) {
1141181103cdSKONRAD Frederic if (k->guest_notifier_pending(vdev, queue_no)) {
114249ab747fSPaolo Bonzini msix_set_pending(dev, vector);
114349ab747fSPaolo Bonzini }
114449ab747fSPaolo Bonzini } else if (event_notifier_test_and_clear(notifier)) {
114549ab747fSPaolo Bonzini msix_set_pending(dev, vector);
114649ab747fSPaolo Bonzini }
114749ab747fSPaolo Bonzini }
114816805428SCindy Lu /* poll the config intr */
114916805428SCindy Lu ret = virtio_pci_get_notifier(proxy, VIRTIO_CONFIG_IRQ_IDX, ¬ifier,
115016805428SCindy Lu &vector);
115116805428SCindy Lu if (ret < 0) {
115216805428SCindy Lu return;
115316805428SCindy Lu }
115416805428SCindy Lu if (vector < vector_start || vector >= vector_end ||
115516805428SCindy Lu !msix_is_masked(dev, vector)) {
115616805428SCindy Lu return;
115716805428SCindy Lu }
115816805428SCindy Lu if (k->guest_notifier_pending) {
115916805428SCindy Lu if (k->guest_notifier_pending(vdev, VIRTIO_CONFIG_IRQ_IDX)) {
116016805428SCindy Lu msix_set_pending(dev, vector);
116116805428SCindy Lu }
116216805428SCindy Lu } else if (event_notifier_test_and_clear(notifier)) {
116316805428SCindy Lu msix_set_pending(dev, vector);
116416805428SCindy Lu }
116516805428SCindy Lu }
116616805428SCindy Lu
virtio_pci_set_guest_notifier_fd_handler(VirtIODevice * vdev,VirtQueue * vq,int n,bool assign,bool with_irqfd)116716805428SCindy Lu void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq,
116816805428SCindy Lu int n, bool assign,
116916805428SCindy Lu bool with_irqfd)
117016805428SCindy Lu {
117116805428SCindy Lu if (n == VIRTIO_CONFIG_IRQ_IDX) {
117216805428SCindy Lu virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd);
117316805428SCindy Lu } else {
117416805428SCindy Lu virtio_queue_set_guest_notifier_fd_handler(vq, assign, with_irqfd);
117516805428SCindy Lu }
117649ab747fSPaolo Bonzini }
117749ab747fSPaolo Bonzini
virtio_pci_set_guest_notifier(DeviceState * d,int n,bool assign,bool with_irqfd)117849ab747fSPaolo Bonzini static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
117949ab747fSPaolo Bonzini bool with_irqfd)
118049ab747fSPaolo Bonzini {
118149ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1182a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1183a3fc66d9SPaolo Bonzini VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
118416805428SCindy Lu VirtQueue *vq = NULL;
118516805428SCindy Lu EventNotifier *notifier = NULL;
118616805428SCindy Lu
118716805428SCindy Lu if (n == VIRTIO_CONFIG_IRQ_IDX) {
118816805428SCindy Lu notifier = virtio_config_get_guest_notifier(vdev);
118916805428SCindy Lu } else {
119016805428SCindy Lu vq = virtio_get_queue(vdev, n);
119116805428SCindy Lu notifier = virtio_queue_get_guest_notifier(vq);
119216805428SCindy Lu }
119349ab747fSPaolo Bonzini
119449ab747fSPaolo Bonzini if (assign) {
119549ab747fSPaolo Bonzini int r = event_notifier_init(notifier, 0);
119649ab747fSPaolo Bonzini if (r < 0) {
119749ab747fSPaolo Bonzini return r;
119849ab747fSPaolo Bonzini }
119916805428SCindy Lu virtio_pci_set_guest_notifier_fd_handler(vdev, vq, n, true, with_irqfd);
120049ab747fSPaolo Bonzini } else {
120116805428SCindy Lu virtio_pci_set_guest_notifier_fd_handler(vdev, vq, n, false,
120216805428SCindy Lu with_irqfd);
120349ab747fSPaolo Bonzini event_notifier_cleanup(notifier);
120449ab747fSPaolo Bonzini }
120549ab747fSPaolo Bonzini
12065669655aSVictor Kaplansky if (!msix_enabled(&proxy->pci_dev) &&
12075669655aSVictor Kaplansky vdev->use_guest_notifier_mask &&
12085669655aSVictor Kaplansky vdc->guest_notifier_mask) {
1209a3fc66d9SPaolo Bonzini vdc->guest_notifier_mask(vdev, n, !assign);
121062c96360SMichael S. Tsirkin }
121162c96360SMichael S. Tsirkin
121249ab747fSPaolo Bonzini return 0;
121349ab747fSPaolo Bonzini }
121449ab747fSPaolo Bonzini
virtio_pci_query_guest_notifiers(DeviceState * d)121549ab747fSPaolo Bonzini static bool virtio_pci_query_guest_notifiers(DeviceState *d)
121649ab747fSPaolo Bonzini {
121749ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
121849ab747fSPaolo Bonzini return msix_enabled(&proxy->pci_dev);
121949ab747fSPaolo Bonzini }
122049ab747fSPaolo Bonzini
virtio_pci_set_guest_notifiers(DeviceState * d,int nvqs,bool assign)122149ab747fSPaolo Bonzini static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
122249ab747fSPaolo Bonzini {
122349ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1224a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1225181103cdSKONRAD Frederic VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
122649ab747fSPaolo Bonzini int r, n;
122749ab747fSPaolo Bonzini bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
122849ab747fSPaolo Bonzini kvm_msi_via_irqfd_enabled();
122949ab747fSPaolo Bonzini
123087b3bd1cSJason Wang nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
123149ab747fSPaolo Bonzini
12325a9d5f09SAlex Bennée /*
12335a9d5f09SAlex Bennée * When deassigning, pass a consistent nvqs value to avoid leaking
12345a9d5f09SAlex Bennée * notifiers. But first check we've actually been configured, exit
12355a9d5f09SAlex Bennée * early if we haven't.
123649ab747fSPaolo Bonzini */
12375a9d5f09SAlex Bennée if (!assign && !proxy->nvqs_with_notifiers) {
12385a9d5f09SAlex Bennée return 0;
12395a9d5f09SAlex Bennée }
124049ab747fSPaolo Bonzini assert(assign || nvqs == proxy->nvqs_with_notifiers);
124149ab747fSPaolo Bonzini
124249ab747fSPaolo Bonzini proxy->nvqs_with_notifiers = nvqs;
124349ab747fSPaolo Bonzini
124449ab747fSPaolo Bonzini /* Must unset vector notifier while guest notifier is still assigned */
124516805428SCindy Lu if ((proxy->vector_irqfd ||
124616805428SCindy Lu (vdev->use_guest_notifier_mask && k->guest_notifier_mask)) &&
124716805428SCindy Lu !assign) {
124849ab747fSPaolo Bonzini msix_unset_vector_notifiers(&proxy->pci_dev);
124949ab747fSPaolo Bonzini if (proxy->vector_irqfd) {
125016805428SCindy Lu kvm_virtio_pci_vector_vq_release(proxy, nvqs);
125116805428SCindy Lu kvm_virtio_pci_vector_config_release(proxy);
125249ab747fSPaolo Bonzini g_free(proxy->vector_irqfd);
125349ab747fSPaolo Bonzini proxy->vector_irqfd = NULL;
125449ab747fSPaolo Bonzini }
125549ab747fSPaolo Bonzini }
125649ab747fSPaolo Bonzini
125749ab747fSPaolo Bonzini for (n = 0; n < nvqs; n++) {
125849ab747fSPaolo Bonzini if (!virtio_queue_get_num(vdev, n)) {
125949ab747fSPaolo Bonzini break;
126049ab747fSPaolo Bonzini }
126149ab747fSPaolo Bonzini
126223fe2b3fSMichael S. Tsirkin r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
126349ab747fSPaolo Bonzini if (r < 0) {
126449ab747fSPaolo Bonzini goto assign_error;
126549ab747fSPaolo Bonzini }
126649ab747fSPaolo Bonzini }
126716805428SCindy Lu r = virtio_pci_set_guest_notifier(d, VIRTIO_CONFIG_IRQ_IDX, assign,
126816805428SCindy Lu with_irqfd);
126916805428SCindy Lu if (r < 0) {
127016805428SCindy Lu goto config_assign_error;
127116805428SCindy Lu }
127249ab747fSPaolo Bonzini /* Must set vector notifier after guest notifier has been assigned */
127316805428SCindy Lu if ((with_irqfd ||
127416805428SCindy Lu (vdev->use_guest_notifier_mask && k->guest_notifier_mask)) &&
127516805428SCindy Lu assign) {
127649ab747fSPaolo Bonzini if (with_irqfd) {
127749ab747fSPaolo Bonzini proxy->vector_irqfd =
127849ab747fSPaolo Bonzini g_malloc0(sizeof(*proxy->vector_irqfd) *
127949ab747fSPaolo Bonzini msix_nr_vectors_allocated(&proxy->pci_dev));
128016805428SCindy Lu r = kvm_virtio_pci_vector_vq_use(proxy, nvqs);
128149ab747fSPaolo Bonzini if (r < 0) {
128216805428SCindy Lu goto config_assign_error;
128316805428SCindy Lu }
128416805428SCindy Lu r = kvm_virtio_pci_vector_config_use(proxy);
128516805428SCindy Lu if (r < 0) {
128616805428SCindy Lu goto config_error;
128749ab747fSPaolo Bonzini }
128849ab747fSPaolo Bonzini }
128916805428SCindy Lu
129016805428SCindy Lu r = msix_set_vector_notifiers(&proxy->pci_dev, virtio_pci_vector_unmask,
129149ab747fSPaolo Bonzini virtio_pci_vector_mask,
129249ab747fSPaolo Bonzini virtio_pci_vector_poll);
129349ab747fSPaolo Bonzini if (r < 0) {
129449ab747fSPaolo Bonzini goto notifiers_error;
129549ab747fSPaolo Bonzini }
129649ab747fSPaolo Bonzini }
129749ab747fSPaolo Bonzini
129849ab747fSPaolo Bonzini return 0;
129949ab747fSPaolo Bonzini
130049ab747fSPaolo Bonzini notifiers_error:
130149ab747fSPaolo Bonzini if (with_irqfd) {
130249ab747fSPaolo Bonzini assert(assign);
130316805428SCindy Lu kvm_virtio_pci_vector_vq_release(proxy, nvqs);
130449ab747fSPaolo Bonzini }
130516805428SCindy Lu config_error:
130616805428SCindy Lu if (with_irqfd) {
130716805428SCindy Lu kvm_virtio_pci_vector_config_release(proxy);
130816805428SCindy Lu }
130916805428SCindy Lu config_assign_error:
131016805428SCindy Lu virtio_pci_set_guest_notifier(d, VIRTIO_CONFIG_IRQ_IDX, !assign,
131116805428SCindy Lu with_irqfd);
131249ab747fSPaolo Bonzini assign_error:
131349ab747fSPaolo Bonzini /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
131449ab747fSPaolo Bonzini assert(assign);
131549ab747fSPaolo Bonzini while (--n >= 0) {
131649ab747fSPaolo Bonzini virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
131749ab747fSPaolo Bonzini }
13184396d4bdSleixiang g_free(proxy->vector_irqfd);
13194396d4bdSleixiang proxy->vector_irqfd = NULL;
132049ab747fSPaolo Bonzini return r;
132149ab747fSPaolo Bonzini }
132249ab747fSPaolo Bonzini
virtio_pci_set_host_notifier_mr(DeviceState * d,int n,MemoryRegion * mr,bool assign)13236f80e617STiwei Bie static int virtio_pci_set_host_notifier_mr(DeviceState *d, int n,
13246f80e617STiwei Bie MemoryRegion *mr, bool assign)
13256f80e617STiwei Bie {
13266f80e617STiwei Bie VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
13276f80e617STiwei Bie int offset;
13286f80e617STiwei Bie
13296f80e617STiwei Bie if (n >= VIRTIO_QUEUE_MAX || !virtio_pci_modern(proxy) ||
13306f80e617STiwei Bie virtio_pci_queue_mem_mult(proxy) != memory_region_size(mr)) {
13316f80e617STiwei Bie return -1;
13326f80e617STiwei Bie }
13336f80e617STiwei Bie
13346f80e617STiwei Bie if (assign) {
13356f80e617STiwei Bie offset = virtio_pci_queue_mem_mult(proxy) * n;
13366f80e617STiwei Bie memory_region_add_subregion_overlap(&proxy->notify.mr, offset, mr, 1);
13376f80e617STiwei Bie } else {
13386f80e617STiwei Bie memory_region_del_subregion(&proxy->notify.mr, mr);
13396f80e617STiwei Bie }
13406f80e617STiwei Bie
13416f80e617STiwei Bie return 0;
13426f80e617STiwei Bie }
13436f80e617STiwei Bie
virtio_pci_vmstate_change(DeviceState * d,bool running)134449ab747fSPaolo Bonzini static void virtio_pci_vmstate_change(DeviceState *d, bool running)
134549ab747fSPaolo Bonzini {
134649ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1347a3fc66d9SPaolo Bonzini VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
134849ab747fSPaolo Bonzini
134949ab747fSPaolo Bonzini if (running) {
135068a27b20SMichael S. Tsirkin /* Old QEMU versions did not set bus master enable on status write.
135168a27b20SMichael S. Tsirkin * Detect DRIVER set and enable it.
135268a27b20SMichael S. Tsirkin */
135368a27b20SMichael S. Tsirkin if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
135468a27b20SMichael S. Tsirkin (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
135545363e46SMichael S. Tsirkin !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
135668a27b20SMichael S. Tsirkin pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
135768a27b20SMichael S. Tsirkin proxy->pci_dev.config[PCI_COMMAND] |
135868a27b20SMichael S. Tsirkin PCI_COMMAND_MASTER, 1);
135949ab747fSPaolo Bonzini }
136049ab747fSPaolo Bonzini virtio_pci_start_ioeventfd(proxy);
136149ab747fSPaolo Bonzini } else {
136249ab747fSPaolo Bonzini virtio_pci_stop_ioeventfd(proxy);
136349ab747fSPaolo Bonzini }
136449ab747fSPaolo Bonzini }
136549ab747fSPaolo Bonzini
136649ab747fSPaolo Bonzini /*
136749ab747fSPaolo Bonzini * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
136849ab747fSPaolo Bonzini */
136949ab747fSPaolo Bonzini
virtio_pci_query_nvectors(DeviceState * d)1370e0d686bfSJason Wang static int virtio_pci_query_nvectors(DeviceState *d)
1371e0d686bfSJason Wang {
1372e0d686bfSJason Wang VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1373e0d686bfSJason Wang
1374e0d686bfSJason Wang return proxy->nvectors;
1375e0d686bfSJason Wang }
1376e0d686bfSJason Wang
virtio_pci_get_dma_as(DeviceState * d)13778607f5c3SJason Wang static AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
13788607f5c3SJason Wang {
13798607f5c3SJason Wang VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
13808607f5c3SJason Wang PCIDevice *dev = &proxy->pci_dev;
13818607f5c3SJason Wang
1382f0edf239SJason Wang return pci_get_address_space(dev);
13838607f5c3SJason Wang }
13848607f5c3SJason Wang
virtio_pci_iommu_enabled(DeviceState * d)13853d1e5d86SJason Wang static bool virtio_pci_iommu_enabled(DeviceState *d)
13863d1e5d86SJason Wang {
13873d1e5d86SJason Wang VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
13883d1e5d86SJason Wang PCIDevice *dev = &proxy->pci_dev;
13893d1e5d86SJason Wang AddressSpace *dma_as = pci_device_iommu_address_space(dev);
13903d1e5d86SJason Wang
13913d1e5d86SJason Wang if (dma_as == &address_space_memory) {
13923d1e5d86SJason Wang return false;
13933d1e5d86SJason Wang }
13943d1e5d86SJason Wang
13953d1e5d86SJason Wang return true;
13963d1e5d86SJason Wang }
13973d1e5d86SJason Wang
virtio_pci_queue_enabled(DeviceState * d,int n)1398f19bcdfeSJason Wang static bool virtio_pci_queue_enabled(DeviceState *d, int n)
1399f19bcdfeSJason Wang {
1400f19bcdfeSJason Wang VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1401f19bcdfeSJason Wang VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1402f19bcdfeSJason Wang
1403f19bcdfeSJason Wang if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1404a48aaf88SYuri Benditovich return proxy->vqs[n].enabled;
1405f19bcdfeSJason Wang }
1406f19bcdfeSJason Wang
14070c9753ebSLaurent Vivier return virtio_queue_enabled_legacy(vdev, n);
1408f19bcdfeSJason Wang }
1409f19bcdfeSJason Wang
virtio_pci_add_mem_cap(VirtIOPCIProxy * proxy,struct virtio_pci_cap * cap)1410ada434cdSMichael S. Tsirkin static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1411dfb8e184SMichael S. Tsirkin struct virtio_pci_cap *cap)
1412dfb8e184SMichael S. Tsirkin {
1413dfb8e184SMichael S. Tsirkin PCIDevice *dev = &proxy->pci_dev;
1414dfb8e184SMichael S. Tsirkin int offset;
1415dfb8e184SMichael S. Tsirkin
14169a7c2a59SMao Zhongyi offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0,
14179a7c2a59SMao Zhongyi cap->cap_len, &error_abort);
1418dfb8e184SMichael S. Tsirkin
1419dfb8e184SMichael S. Tsirkin assert(cap->cap_len >= sizeof *cap);
1420dfb8e184SMichael S. Tsirkin memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1421dfb8e184SMichael S. Tsirkin cap->cap_len - PCI_CAP_FLAGS);
1422ada434cdSMichael S. Tsirkin
1423ada434cdSMichael S. Tsirkin return offset;
1424dfb8e184SMichael S. Tsirkin }
1425dfb8e184SMichael S. Tsirkin
virtio_pci_set_vector(VirtIODevice * vdev,VirtIOPCIProxy * proxy,int queue_no,uint16_t old_vector,uint16_t new_vector)14262ce6cff9SCindy Lu static void virtio_pci_set_vector(VirtIODevice *vdev,
14272ce6cff9SCindy Lu VirtIOPCIProxy *proxy,
14282ce6cff9SCindy Lu int queue_no, uint16_t old_vector,
14292ce6cff9SCindy Lu uint16_t new_vector)
14302ce6cff9SCindy Lu {
14312ce6cff9SCindy Lu bool kvm_irqfd = (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
14322ce6cff9SCindy Lu msix_enabled(&proxy->pci_dev) && kvm_msi_via_irqfd_enabled();
14332ce6cff9SCindy Lu
14342ce6cff9SCindy Lu if (new_vector == old_vector) {
14352ce6cff9SCindy Lu return;
14362ce6cff9SCindy Lu }
14372ce6cff9SCindy Lu
14382ce6cff9SCindy Lu /*
14392ce6cff9SCindy Lu * If the device uses irqfd and the vector changes after DRIVER_OK is
14402ce6cff9SCindy Lu * set, we need to release the old vector and set up the new one.
14412ce6cff9SCindy Lu * Otherwise just need to set the new vector on the device.
14422ce6cff9SCindy Lu */
14432ce6cff9SCindy Lu if (kvm_irqfd && old_vector != VIRTIO_NO_VECTOR) {
14442ce6cff9SCindy Lu kvm_virtio_pci_vector_release_one(proxy, queue_no);
14452ce6cff9SCindy Lu }
14462ce6cff9SCindy Lu /* Set the new vector on the device. */
14472ce6cff9SCindy Lu if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
14482ce6cff9SCindy Lu vdev->config_vector = new_vector;
14492ce6cff9SCindy Lu } else {
14502ce6cff9SCindy Lu virtio_queue_set_vector(vdev, queue_no, new_vector);
14512ce6cff9SCindy Lu }
14522ce6cff9SCindy Lu /* If the new vector changed need to set it up. */
14532ce6cff9SCindy Lu if (kvm_irqfd && new_vector != VIRTIO_NO_VECTOR) {
14542ce6cff9SCindy Lu kvm_virtio_pci_vector_use_one(proxy, queue_no);
14552ce6cff9SCindy Lu }
14562ce6cff9SCindy Lu }
14572ce6cff9SCindy Lu
virtio_pci_add_shm_cap(VirtIOPCIProxy * proxy,uint8_t bar,uint64_t offset,uint64_t length,uint8_t id)1458605a16a7SDr. David Alan Gilbert int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy,
1459605a16a7SDr. David Alan Gilbert uint8_t bar, uint64_t offset, uint64_t length,
1460605a16a7SDr. David Alan Gilbert uint8_t id)
1461605a16a7SDr. David Alan Gilbert {
1462605a16a7SDr. David Alan Gilbert struct virtio_pci_cap64 cap = {
1463605a16a7SDr. David Alan Gilbert .cap.cap_len = sizeof cap,
1464605a16a7SDr. David Alan Gilbert .cap.cfg_type = VIRTIO_PCI_CAP_SHARED_MEMORY_CFG,
1465605a16a7SDr. David Alan Gilbert };
1466605a16a7SDr. David Alan Gilbert
1467605a16a7SDr. David Alan Gilbert cap.cap.bar = bar;
1468605a16a7SDr. David Alan Gilbert cap.cap.length = cpu_to_le32(length);
1469605a16a7SDr. David Alan Gilbert cap.length_hi = cpu_to_le32(length >> 32);
1470605a16a7SDr. David Alan Gilbert cap.cap.offset = cpu_to_le32(offset);
1471605a16a7SDr. David Alan Gilbert cap.offset_hi = cpu_to_le32(offset >> 32);
1472605a16a7SDr. David Alan Gilbert cap.cap.id = id;
1473605a16a7SDr. David Alan Gilbert return virtio_pci_add_mem_cap(proxy, &cap.cap);
1474605a16a7SDr. David Alan Gilbert }
1475605a16a7SDr. David Alan Gilbert
virtio_pci_common_read(void * opaque,hwaddr addr,unsigned size)1476dfb8e184SMichael S. Tsirkin static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1477dfb8e184SMichael S. Tsirkin unsigned size)
1478dfb8e184SMichael S. Tsirkin {
1479dfb8e184SMichael S. Tsirkin VirtIOPCIProxy *proxy = opaque;
1480dfb8e184SMichael S. Tsirkin VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1481dfb8e184SMichael S. Tsirkin uint32_t val = 0;
1482dfb8e184SMichael S. Tsirkin int i;
1483dfb8e184SMichael S. Tsirkin
148480ebfd69SAndrew Melnychenko if (vdev == NULL) {
148580ebfd69SAndrew Melnychenko return UINT64_MAX;
148680ebfd69SAndrew Melnychenko }
148780ebfd69SAndrew Melnychenko
1488dfb8e184SMichael S. Tsirkin switch (addr) {
1489dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_DFSELECT:
1490dfb8e184SMichael S. Tsirkin val = proxy->dfselect;
1491dfb8e184SMichael S. Tsirkin break;
1492dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_DF:
1493dfb8e184SMichael S. Tsirkin if (proxy->dfselect <= 1) {
14949b706dbbSMichael S. Tsirkin VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
14959b706dbbSMichael S. Tsirkin
14969b706dbbSMichael S. Tsirkin val = (vdev->host_features & ~vdc->legacy_features) >>
14975f456073SMichael S. Tsirkin (32 * proxy->dfselect);
1498dfb8e184SMichael S. Tsirkin }
1499dfb8e184SMichael S. Tsirkin break;
1500dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_GFSELECT:
1501dfb8e184SMichael S. Tsirkin val = proxy->gfselect;
1502dfb8e184SMichael S. Tsirkin break;
1503dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_GF:
15043750dabcSGonglei if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1505dfb8e184SMichael S. Tsirkin val = proxy->guest_features[proxy->gfselect];
1506dfb8e184SMichael S. Tsirkin }
1507dfb8e184SMichael S. Tsirkin break;
1508dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_MSIX:
1509dfb8e184SMichael S. Tsirkin val = vdev->config_vector;
1510dfb8e184SMichael S. Tsirkin break;
1511dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_NUMQ:
1512dfb8e184SMichael S. Tsirkin for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1513dfb8e184SMichael S. Tsirkin if (virtio_queue_get_num(vdev, i)) {
1514dfb8e184SMichael S. Tsirkin val = i + 1;
1515dfb8e184SMichael S. Tsirkin }
1516dfb8e184SMichael S. Tsirkin }
1517dfb8e184SMichael S. Tsirkin break;
1518dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_STATUS:
1519dfb8e184SMichael S. Tsirkin val = vdev->status;
1520dfb8e184SMichael S. Tsirkin break;
1521dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_CFGGENERATION:
1522b8f05908SMichael S. Tsirkin val = vdev->generation;
1523dfb8e184SMichael S. Tsirkin break;
1524dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_SELECT:
1525dfb8e184SMichael S. Tsirkin val = vdev->queue_sel;
1526dfb8e184SMichael S. Tsirkin break;
1527dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_SIZE:
1528dfb8e184SMichael S. Tsirkin val = virtio_queue_get_num(vdev, vdev->queue_sel);
1529dfb8e184SMichael S. Tsirkin break;
1530dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_MSIX:
1531dfb8e184SMichael S. Tsirkin val = virtio_queue_vector(vdev, vdev->queue_sel);
1532dfb8e184SMichael S. Tsirkin break;
1533dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_ENABLE:
1534dfb8e184SMichael S. Tsirkin val = proxy->vqs[vdev->queue_sel].enabled;
1535dfb8e184SMichael S. Tsirkin break;
1536dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_NOFF:
1537dfb8e184SMichael S. Tsirkin /* Simply map queues in order */
1538dfb8e184SMichael S. Tsirkin val = vdev->queue_sel;
1539dfb8e184SMichael S. Tsirkin break;
1540dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_DESCLO:
1541dfb8e184SMichael S. Tsirkin val = proxy->vqs[vdev->queue_sel].desc[0];
1542dfb8e184SMichael S. Tsirkin break;
1543dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_DESCHI:
1544dfb8e184SMichael S. Tsirkin val = proxy->vqs[vdev->queue_sel].desc[1];
1545dfb8e184SMichael S. Tsirkin break;
1546dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_AVAILLO:
1547dfb8e184SMichael S. Tsirkin val = proxy->vqs[vdev->queue_sel].avail[0];
1548dfb8e184SMichael S. Tsirkin break;
1549dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_AVAILHI:
1550dfb8e184SMichael S. Tsirkin val = proxy->vqs[vdev->queue_sel].avail[1];
1551dfb8e184SMichael S. Tsirkin break;
1552dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_USEDLO:
1553dfb8e184SMichael S. Tsirkin val = proxy->vqs[vdev->queue_sel].used[0];
1554dfb8e184SMichael S. Tsirkin break;
1555dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_USEDHI:
1556dfb8e184SMichael S. Tsirkin val = proxy->vqs[vdev->queue_sel].used[1];
1557dfb8e184SMichael S. Tsirkin break;
1558805d782dSXuan Zhuo case VIRTIO_PCI_COMMON_Q_RESET:
1559805d782dSXuan Zhuo val = proxy->vqs[vdev->queue_sel].reset;
1560805d782dSXuan Zhuo break;
1561dfb8e184SMichael S. Tsirkin default:
1562dfb8e184SMichael S. Tsirkin val = 0;
1563dfb8e184SMichael S. Tsirkin }
1564dfb8e184SMichael S. Tsirkin
1565dfb8e184SMichael S. Tsirkin return val;
1566dfb8e184SMichael S. Tsirkin }
1567dfb8e184SMichael S. Tsirkin
virtio_pci_common_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)1568dfb8e184SMichael S. Tsirkin static void virtio_pci_common_write(void *opaque, hwaddr addr,
1569dfb8e184SMichael S. Tsirkin uint64_t val, unsigned size)
1570dfb8e184SMichael S. Tsirkin {
1571dfb8e184SMichael S. Tsirkin VirtIOPCIProxy *proxy = opaque;
1572dfb8e184SMichael S. Tsirkin VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
157315377f6eSAkihiko Odaki uint16_t vector;
1574dfb8e184SMichael S. Tsirkin
157580ebfd69SAndrew Melnychenko if (vdev == NULL) {
157680ebfd69SAndrew Melnychenko return;
157780ebfd69SAndrew Melnychenko }
157880ebfd69SAndrew Melnychenko
1579dfb8e184SMichael S. Tsirkin switch (addr) {
1580dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_DFSELECT:
1581dfb8e184SMichael S. Tsirkin proxy->dfselect = val;
1582dfb8e184SMichael S. Tsirkin break;
1583dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_GFSELECT:
1584dfb8e184SMichael S. Tsirkin proxy->gfselect = val;
1585dfb8e184SMichael S. Tsirkin break;
1586dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_GF:
15873750dabcSGonglei if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1588dfb8e184SMichael S. Tsirkin proxy->guest_features[proxy->gfselect] = val;
1589dfb8e184SMichael S. Tsirkin virtio_set_features(vdev,
1590dfb8e184SMichael S. Tsirkin (((uint64_t)proxy->guest_features[1]) << 32) |
1591dfb8e184SMichael S. Tsirkin proxy->guest_features[0]);
1592dfb8e184SMichael S. Tsirkin }
1593dfb8e184SMichael S. Tsirkin break;
1594dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_MSIX:
159515377f6eSAkihiko Odaki if (vdev->config_vector != VIRTIO_NO_VECTOR) {
1596dfb8e184SMichael S. Tsirkin msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
159715377f6eSAkihiko Odaki }
1598dfb8e184SMichael S. Tsirkin /* Make it possible for guest to discover an error took place. */
159915377f6eSAkihiko Odaki if (val < proxy->nvectors) {
160015377f6eSAkihiko Odaki msix_vector_use(&proxy->pci_dev, val);
160115377f6eSAkihiko Odaki } else {
1602dfb8e184SMichael S. Tsirkin val = VIRTIO_NO_VECTOR;
1603dfb8e184SMichael S. Tsirkin }
16042ce6cff9SCindy Lu virtio_pci_set_vector(vdev, proxy, VIRTIO_CONFIG_IRQ_IDX,
16052ce6cff9SCindy Lu vdev->config_vector, val);
1606dfb8e184SMichael S. Tsirkin break;
1607dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_STATUS:
1608dfb8e184SMichael S. Tsirkin if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1609dfb8e184SMichael S. Tsirkin virtio_pci_stop_ioeventfd(proxy);
1610dfb8e184SMichael S. Tsirkin }
1611dfb8e184SMichael S. Tsirkin
1612dfb8e184SMichael S. Tsirkin virtio_set_status(vdev, val & 0xFF);
1613dfb8e184SMichael S. Tsirkin
1614dfb8e184SMichael S. Tsirkin if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1615dfb8e184SMichael S. Tsirkin virtio_pci_start_ioeventfd(proxy);
1616dfb8e184SMichael S. Tsirkin }
1617dfb8e184SMichael S. Tsirkin
1618dfb8e184SMichael S. Tsirkin if (vdev->status == 0) {
161975fd6f13SGerd Hoffmann virtio_pci_reset(DEVICE(proxy));
1620dfb8e184SMichael S. Tsirkin }
1621dfb8e184SMichael S. Tsirkin
1622dfb8e184SMichael S. Tsirkin break;
1623dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_SELECT:
1624dfb8e184SMichael S. Tsirkin if (val < VIRTIO_QUEUE_MAX) {
1625dfb8e184SMichael S. Tsirkin vdev->queue_sel = val;
1626dfb8e184SMichael S. Tsirkin }
1627dfb8e184SMichael S. Tsirkin break;
1628dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_SIZE:
1629dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].num = val;
1630d0c5f643SMichael S. Tsirkin virtio_queue_set_num(vdev, vdev->queue_sel,
1631d0c5f643SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].num);
1632f0d634eaSCarlos López virtio_init_region_cache(vdev, vdev->queue_sel);
1633dfb8e184SMichael S. Tsirkin break;
1634dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_MSIX:
163515377f6eSAkihiko Odaki vector = virtio_queue_vector(vdev, vdev->queue_sel);
163615377f6eSAkihiko Odaki if (vector != VIRTIO_NO_VECTOR) {
163715377f6eSAkihiko Odaki msix_vector_unuse(&proxy->pci_dev, vector);
163815377f6eSAkihiko Odaki }
1639dfb8e184SMichael S. Tsirkin /* Make it possible for guest to discover an error took place. */
164015377f6eSAkihiko Odaki if (val < proxy->nvectors) {
164115377f6eSAkihiko Odaki msix_vector_use(&proxy->pci_dev, val);
164215377f6eSAkihiko Odaki } else {
1643dfb8e184SMichael S. Tsirkin val = VIRTIO_NO_VECTOR;
1644dfb8e184SMichael S. Tsirkin }
16452ce6cff9SCindy Lu virtio_pci_set_vector(vdev, proxy, vdev->queue_sel, vector, val);
1646dfb8e184SMichael S. Tsirkin break;
1647dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_ENABLE:
164810d35e58SJason Wang if (val == 1) {
1649dfb8e184SMichael S. Tsirkin virtio_queue_set_num(vdev, vdev->queue_sel,
1650dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].num);
1651dfb8e184SMichael S. Tsirkin virtio_queue_set_rings(vdev, vdev->queue_sel,
1652dfb8e184SMichael S. Tsirkin ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1653dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].desc[0],
1654dfb8e184SMichael S. Tsirkin ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1655dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].avail[0],
1656dfb8e184SMichael S. Tsirkin ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1657dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].used[0]);
1658393f04d3SJason Wang proxy->vqs[vdev->queue_sel].enabled = 1;
1659805d782dSXuan Zhuo proxy->vqs[vdev->queue_sel].reset = 0;
1660d1060e3dSKangjie Xu virtio_queue_enable(vdev, vdev->queue_sel);
166110d35e58SJason Wang } else {
166210d35e58SJason Wang virtio_error(vdev, "wrong value for queue_enable %"PRIx64, val);
166310d35e58SJason Wang }
1664dfb8e184SMichael S. Tsirkin break;
1665dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_DESCLO:
1666dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].desc[0] = val;
1667dfb8e184SMichael S. Tsirkin break;
1668dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_DESCHI:
1669dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].desc[1] = val;
1670dfb8e184SMichael S. Tsirkin break;
1671dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_AVAILLO:
1672dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].avail[0] = val;
1673dfb8e184SMichael S. Tsirkin break;
1674dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_AVAILHI:
1675dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].avail[1] = val;
1676dfb8e184SMichael S. Tsirkin break;
1677dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_USEDLO:
1678dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].used[0] = val;
1679dfb8e184SMichael S. Tsirkin break;
1680dfb8e184SMichael S. Tsirkin case VIRTIO_PCI_COMMON_Q_USEDHI:
1681dfb8e184SMichael S. Tsirkin proxy->vqs[vdev->queue_sel].used[1] = val;
1682dfb8e184SMichael S. Tsirkin break;
1683805d782dSXuan Zhuo case VIRTIO_PCI_COMMON_Q_RESET:
1684805d782dSXuan Zhuo if (val == 1) {
1685805d782dSXuan Zhuo proxy->vqs[vdev->queue_sel].reset = 1;
1686805d782dSXuan Zhuo
1687805d782dSXuan Zhuo virtio_queue_reset(vdev, vdev->queue_sel);
1688805d782dSXuan Zhuo
1689805d782dSXuan Zhuo proxy->vqs[vdev->queue_sel].reset = 0;
1690805d782dSXuan Zhuo proxy->vqs[vdev->queue_sel].enabled = 0;
1691805d782dSXuan Zhuo }
1692805d782dSXuan Zhuo break;
1693dfb8e184SMichael S. Tsirkin default:
1694dfb8e184SMichael S. Tsirkin break;
1695dfb8e184SMichael S. Tsirkin }
1696dfb8e184SMichael S. Tsirkin }
1697dfb8e184SMichael S. Tsirkin
1698dfb8e184SMichael S. Tsirkin
virtio_pci_notify_read(void * opaque,hwaddr addr,unsigned size)1699dfb8e184SMichael S. Tsirkin static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1700dfb8e184SMichael S. Tsirkin unsigned size)
1701dfb8e184SMichael S. Tsirkin {
1702df07a8f8SAndrew Melnychenko VirtIOPCIProxy *proxy = opaque;
1703df07a8f8SAndrew Melnychenko if (virtio_bus_get_device(&proxy->bus) == NULL) {
1704df07a8f8SAndrew Melnychenko return UINT64_MAX;
1705df07a8f8SAndrew Melnychenko }
1706df07a8f8SAndrew Melnychenko
1707dfb8e184SMichael S. Tsirkin return 0;
1708dfb8e184SMichael S. Tsirkin }
1709dfb8e184SMichael S. Tsirkin
virtio_pci_notify_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)1710dfb8e184SMichael S. Tsirkin static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1711dfb8e184SMichael S. Tsirkin uint64_t val, unsigned size)
1712dfb8e184SMichael S. Tsirkin {
1713ccec7e96SAndrew Melnychenko VirtIOPCIProxy *proxy = opaque;
1714ccec7e96SAndrew Melnychenko VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1715ccec7e96SAndrew Melnychenko
1716d9997d89SMarcel Apfelbaum unsigned queue = addr / virtio_pci_queue_mem_mult(proxy);
1717dfb8e184SMichael S. Tsirkin
1718ccec7e96SAndrew Melnychenko if (vdev != NULL && queue < VIRTIO_QUEUE_MAX) {
171931cc62bbSAlex Bennée trace_virtio_pci_notify_write(addr, val, size);
1720dfb8e184SMichael S. Tsirkin virtio_queue_notify(vdev, queue);
1721dfb8e184SMichael S. Tsirkin }
1722dfb8e184SMichael S. Tsirkin }
1723dfb8e184SMichael S. Tsirkin
virtio_pci_notify_write_pio(void * opaque,hwaddr addr,uint64_t val,unsigned size)17249824d2a3SJason Wang static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr,
17259824d2a3SJason Wang uint64_t val, unsigned size)
17269824d2a3SJason Wang {
1727ccec7e96SAndrew Melnychenko VirtIOPCIProxy *proxy = opaque;
1728ccec7e96SAndrew Melnychenko VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1729ccec7e96SAndrew Melnychenko
17309824d2a3SJason Wang unsigned queue = val;
17319824d2a3SJason Wang
1732ccec7e96SAndrew Melnychenko if (vdev != NULL && queue < VIRTIO_QUEUE_MAX) {
173331cc62bbSAlex Bennée trace_virtio_pci_notify_write_pio(addr, val, size);
17349824d2a3SJason Wang virtio_queue_notify(vdev, queue);
17359824d2a3SJason Wang }
17369824d2a3SJason Wang }
17379824d2a3SJason Wang
virtio_pci_isr_read(void * opaque,hwaddr addr,unsigned size)1738dfb8e184SMichael S. Tsirkin static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1739dfb8e184SMichael S. Tsirkin unsigned size)
1740dfb8e184SMichael S. Tsirkin {
1741dfb8e184SMichael S. Tsirkin VirtIOPCIProxy *proxy = opaque;
1742dfb8e184SMichael S. Tsirkin VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1743c3fd7061SYuri Benditovich uint64_t val;
1744dfb8e184SMichael S. Tsirkin
1745c3fd7061SYuri Benditovich if (vdev == NULL) {
1746df07a8f8SAndrew Melnychenko return UINT64_MAX;
1747c3fd7061SYuri Benditovich }
1748c3fd7061SYuri Benditovich
1749c3fd7061SYuri Benditovich val = qatomic_xchg(&vdev->isr, 0);
1750c3fd7061SYuri Benditovich pci_irq_deassert(&proxy->pci_dev);
1751dfb8e184SMichael S. Tsirkin return val;
1752dfb8e184SMichael S. Tsirkin }
1753dfb8e184SMichael S. Tsirkin
virtio_pci_isr_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)1754dfb8e184SMichael S. Tsirkin static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1755dfb8e184SMichael S. Tsirkin uint64_t val, unsigned size)
1756dfb8e184SMichael S. Tsirkin {
1757dfb8e184SMichael S. Tsirkin }
1758dfb8e184SMichael S. Tsirkin
virtio_pci_device_read(void * opaque,hwaddr addr,unsigned size)1759dfb8e184SMichael S. Tsirkin static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1760dfb8e184SMichael S. Tsirkin unsigned size)
1761dfb8e184SMichael S. Tsirkin {
1762ccec7e96SAndrew Melnychenko VirtIOPCIProxy *proxy = opaque;
1763ccec7e96SAndrew Melnychenko VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
176451e0e42cSYuri Benditovich uint64_t val;
1765dfb8e184SMichael S. Tsirkin
1766ccec7e96SAndrew Melnychenko if (vdev == NULL) {
1767df07a8f8SAndrew Melnychenko return UINT64_MAX;
1768ccec7e96SAndrew Melnychenko }
1769ccec7e96SAndrew Melnychenko
1770dfb8e184SMichael S. Tsirkin switch (size) {
1771dfb8e184SMichael S. Tsirkin case 1:
177254c720d4SMichael S. Tsirkin val = virtio_config_modern_readb(vdev, addr);
1773dfb8e184SMichael S. Tsirkin break;
1774dfb8e184SMichael S. Tsirkin case 2:
177554c720d4SMichael S. Tsirkin val = virtio_config_modern_readw(vdev, addr);
1776dfb8e184SMichael S. Tsirkin break;
1777dfb8e184SMichael S. Tsirkin case 4:
177854c720d4SMichael S. Tsirkin val = virtio_config_modern_readl(vdev, addr);
1779dfb8e184SMichael S. Tsirkin break;
178051e0e42cSYuri Benditovich default:
178151e0e42cSYuri Benditovich val = 0;
178251e0e42cSYuri Benditovich break;
1783dfb8e184SMichael S. Tsirkin }
1784dfb8e184SMichael S. Tsirkin return val;
1785dfb8e184SMichael S. Tsirkin }
1786dfb8e184SMichael S. Tsirkin
virtio_pci_device_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)1787dfb8e184SMichael S. Tsirkin static void virtio_pci_device_write(void *opaque, hwaddr addr,
1788dfb8e184SMichael S. Tsirkin uint64_t val, unsigned size)
1789dfb8e184SMichael S. Tsirkin {
1790ccec7e96SAndrew Melnychenko VirtIOPCIProxy *proxy = opaque;
1791ccec7e96SAndrew Melnychenko VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1792ccec7e96SAndrew Melnychenko
1793ccec7e96SAndrew Melnychenko if (vdev == NULL) {
1794ccec7e96SAndrew Melnychenko return;
1795ccec7e96SAndrew Melnychenko }
1796ccec7e96SAndrew Melnychenko
1797dfb8e184SMichael S. Tsirkin switch (size) {
1798dfb8e184SMichael S. Tsirkin case 1:
179954c720d4SMichael S. Tsirkin virtio_config_modern_writeb(vdev, addr, val);
1800dfb8e184SMichael S. Tsirkin break;
1801dfb8e184SMichael S. Tsirkin case 2:
180254c720d4SMichael S. Tsirkin virtio_config_modern_writew(vdev, addr, val);
1803dfb8e184SMichael S. Tsirkin break;
1804dfb8e184SMichael S. Tsirkin case 4:
180554c720d4SMichael S. Tsirkin virtio_config_modern_writel(vdev, addr, val);
1806dfb8e184SMichael S. Tsirkin break;
1807dfb8e184SMichael S. Tsirkin }
1808dfb8e184SMichael S. Tsirkin }
1809dfb8e184SMichael S. Tsirkin
virtio_pci_modern_regions_init(VirtIOPCIProxy * proxy,const char * vdev_name)1810b74259e3SAlex Bennée static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy,
1811b74259e3SAlex Bennée const char *vdev_name)
18121141ce21SGerd Hoffmann {
18131141ce21SGerd Hoffmann static const MemoryRegionOps common_ops = {
18141141ce21SGerd Hoffmann .read = virtio_pci_common_read,
18151141ce21SGerd Hoffmann .write = virtio_pci_common_write,
18161141ce21SGerd Hoffmann .impl = {
18171141ce21SGerd Hoffmann .min_access_size = 1,
18181141ce21SGerd Hoffmann .max_access_size = 4,
18191141ce21SGerd Hoffmann },
18201141ce21SGerd Hoffmann .endianness = DEVICE_LITTLE_ENDIAN,
18211141ce21SGerd Hoffmann };
18221141ce21SGerd Hoffmann static const MemoryRegionOps isr_ops = {
18231141ce21SGerd Hoffmann .read = virtio_pci_isr_read,
18241141ce21SGerd Hoffmann .write = virtio_pci_isr_write,
18251141ce21SGerd Hoffmann .impl = {
18261141ce21SGerd Hoffmann .min_access_size = 1,
18271141ce21SGerd Hoffmann .max_access_size = 4,
18281141ce21SGerd Hoffmann },
18291141ce21SGerd Hoffmann .endianness = DEVICE_LITTLE_ENDIAN,
18301141ce21SGerd Hoffmann };
18311141ce21SGerd Hoffmann static const MemoryRegionOps device_ops = {
18321141ce21SGerd Hoffmann .read = virtio_pci_device_read,
18331141ce21SGerd Hoffmann .write = virtio_pci_device_write,
18341141ce21SGerd Hoffmann .impl = {
18351141ce21SGerd Hoffmann .min_access_size = 1,
18361141ce21SGerd Hoffmann .max_access_size = 4,
18371141ce21SGerd Hoffmann },
18381141ce21SGerd Hoffmann .endianness = DEVICE_LITTLE_ENDIAN,
18391141ce21SGerd Hoffmann };
18401141ce21SGerd Hoffmann static const MemoryRegionOps notify_ops = {
18411141ce21SGerd Hoffmann .read = virtio_pci_notify_read,
18421141ce21SGerd Hoffmann .write = virtio_pci_notify_write,
18431141ce21SGerd Hoffmann .impl = {
18441141ce21SGerd Hoffmann .min_access_size = 1,
18451141ce21SGerd Hoffmann .max_access_size = 4,
18461141ce21SGerd Hoffmann },
18471141ce21SGerd Hoffmann .endianness = DEVICE_LITTLE_ENDIAN,
18481141ce21SGerd Hoffmann };
18499824d2a3SJason Wang static const MemoryRegionOps notify_pio_ops = {
18509824d2a3SJason Wang .read = virtio_pci_notify_read,
18519824d2a3SJason Wang .write = virtio_pci_notify_write_pio,
18529824d2a3SJason Wang .impl = {
18539824d2a3SJason Wang .min_access_size = 1,
18549824d2a3SJason Wang .max_access_size = 4,
18559824d2a3SJason Wang },
18569824d2a3SJason Wang .endianness = DEVICE_LITTLE_ENDIAN,
18579824d2a3SJason Wang };
1858b74259e3SAlex Bennée g_autoptr(GString) name = g_string_new(NULL);
18599824d2a3SJason Wang
1860b74259e3SAlex Bennée g_string_printf(name, "virtio-pci-common-%s", vdev_name);
18611141ce21SGerd Hoffmann memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
18621141ce21SGerd Hoffmann &common_ops,
18631141ce21SGerd Hoffmann proxy,
1864b74259e3SAlex Bennée name->str,
1865b6ce27a5SGerd Hoffmann proxy->common.size);
1866a3cc2e81SGerd Hoffmann
1867b74259e3SAlex Bennée g_string_printf(name, "virtio-pci-isr-%s", vdev_name);
18681141ce21SGerd Hoffmann memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
18691141ce21SGerd Hoffmann &isr_ops,
18701141ce21SGerd Hoffmann proxy,
1871b74259e3SAlex Bennée name->str,
1872b6ce27a5SGerd Hoffmann proxy->isr.size);
1873a3cc2e81SGerd Hoffmann
1874b74259e3SAlex Bennée g_string_printf(name, "virtio-pci-device-%s", vdev_name);
18751141ce21SGerd Hoffmann memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
18761141ce21SGerd Hoffmann &device_ops,
1877ccec7e96SAndrew Melnychenko proxy,
1878b74259e3SAlex Bennée name->str,
1879b6ce27a5SGerd Hoffmann proxy->device.size);
1880a3cc2e81SGerd Hoffmann
1881b74259e3SAlex Bennée g_string_printf(name, "virtio-pci-notify-%s", vdev_name);
18821141ce21SGerd Hoffmann memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
18831141ce21SGerd Hoffmann ¬ify_ops,
1884ccec7e96SAndrew Melnychenko proxy,
1885b74259e3SAlex Bennée name->str,
1886b6ce27a5SGerd Hoffmann proxy->notify.size);
18879824d2a3SJason Wang
1888b74259e3SAlex Bennée g_string_printf(name, "virtio-pci-notify-pio-%s", vdev_name);
18899824d2a3SJason Wang memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
18909824d2a3SJason Wang ¬ify_pio_ops,
1891ccec7e96SAndrew Melnychenko proxy,
1892b74259e3SAlex Bennée name->str,
1893e3aab6c7SMichael S. Tsirkin proxy->notify_pio.size);
1894a3cc2e81SGerd Hoffmann }
1895a3cc2e81SGerd Hoffmann
virtio_pci_modern_region_map(VirtIOPCIProxy * proxy,VirtIOPCIRegion * region,struct virtio_pci_cap * cap,MemoryRegion * mr,uint8_t bar)1896a3cc2e81SGerd Hoffmann static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
189754790d71SGerd Hoffmann VirtIOPCIRegion *region,
18989824d2a3SJason Wang struct virtio_pci_cap *cap,
18999824d2a3SJason Wang MemoryRegion *mr,
19009824d2a3SJason Wang uint8_t bar)
1901a3cc2e81SGerd Hoffmann {
19029824d2a3SJason Wang memory_region_add_subregion(mr, region->offset, ®ion->mr);
190354790d71SGerd Hoffmann
1904fc004905SGerd Hoffmann cap->cfg_type = region->type;
19059824d2a3SJason Wang cap->bar = bar;
190654790d71SGerd Hoffmann cap->offset = cpu_to_le32(region->offset);
1907b6ce27a5SGerd Hoffmann cap->length = cpu_to_le32(region->size);
190854790d71SGerd Hoffmann virtio_pci_add_mem_cap(proxy, cap);
19099824d2a3SJason Wang
19101141ce21SGerd Hoffmann }
1911dfb8e184SMichael S. Tsirkin
virtio_pci_modern_mem_region_map(VirtIOPCIProxy * proxy,VirtIOPCIRegion * region,struct virtio_pci_cap * cap)19129824d2a3SJason Wang static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,
19139824d2a3SJason Wang VirtIOPCIRegion *region,
19149824d2a3SJason Wang struct virtio_pci_cap *cap)
19159824d2a3SJason Wang {
19169824d2a3SJason Wang virtio_pci_modern_region_map(proxy, region, cap,
19177a25126dSChen Fan &proxy->modern_bar, proxy->modern_mem_bar_idx);
19189824d2a3SJason Wang }
19199824d2a3SJason Wang
virtio_pci_modern_io_region_map(VirtIOPCIProxy * proxy,VirtIOPCIRegion * region,struct virtio_pci_cap * cap)19209824d2a3SJason Wang static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy,
19219824d2a3SJason Wang VirtIOPCIRegion *region,
19229824d2a3SJason Wang struct virtio_pci_cap *cap)
19239824d2a3SJason Wang {
19249824d2a3SJason Wang virtio_pci_modern_region_map(proxy, region, cap,
19257a25126dSChen Fan &proxy->io_bar, proxy->modern_io_bar_idx);
19269824d2a3SJason Wang }
19279824d2a3SJason Wang
virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy * proxy,VirtIOPCIRegion * region)19289824d2a3SJason Wang static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
192927462695SMichael S. Tsirkin VirtIOPCIRegion *region)
193027462695SMichael S. Tsirkin {
193127462695SMichael S. Tsirkin memory_region_del_subregion(&proxy->modern_bar,
193227462695SMichael S. Tsirkin ®ion->mr);
193327462695SMichael S. Tsirkin }
193427462695SMichael S. Tsirkin
virtio_pci_modern_io_region_unmap(VirtIOPCIProxy * proxy,VirtIOPCIRegion * region)19359824d2a3SJason Wang static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
19369824d2a3SJason Wang VirtIOPCIRegion *region)
19379824d2a3SJason Wang {
19389824d2a3SJason Wang memory_region_del_subregion(&proxy->io_bar,
19399824d2a3SJason Wang ®ion->mr);
19409824d2a3SJason Wang }
19419824d2a3SJason Wang
virtio_pci_pre_plugged(DeviceState * d,Error ** errp)1942d1b4259fSMaxime Coquelin static void virtio_pci_pre_plugged(DeviceState *d, Error **errp)
1943d1b4259fSMaxime Coquelin {
1944d1b4259fSMaxime Coquelin VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1945d1b4259fSMaxime Coquelin VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1946d1b4259fSMaxime Coquelin
1947d1b4259fSMaxime Coquelin if (virtio_pci_modern(proxy)) {
1948d1b4259fSMaxime Coquelin virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1949d1b4259fSMaxime Coquelin }
1950d1b4259fSMaxime Coquelin
1951d1b4259fSMaxime Coquelin virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1952d1b4259fSMaxime Coquelin }
1953d1b4259fSMaxime Coquelin
195449ab747fSPaolo Bonzini /* This is called by virtio-bus just after the device is plugged. */
virtio_pci_device_plugged(DeviceState * d,Error ** errp)1955e8398045SJason Wang static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
195649ab747fSPaolo Bonzini {
195749ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
195849ab747fSPaolo Bonzini VirtioBusState *bus = &proxy->bus;
19599a4c0e22SMarcel Apfelbaum bool legacy = virtio_pci_legacy(proxy);
1960d1b4259fSMaxime Coquelin bool modern;
19619824d2a3SJason Wang bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
196249ab747fSPaolo Bonzini uint8_t *config;
196349ab747fSPaolo Bonzini uint32_t size;
1964ee1004bbSPhilippe Mathieu-Daudé VirtIODevice *vdev = virtio_bus_get_device(bus);
196549ab747fSPaolo Bonzini
1966d1b4259fSMaxime Coquelin /*
1967d1b4259fSMaxime Coquelin * Virtio capabilities present without
1968d1b4259fSMaxime Coquelin * VIRTIO_F_VERSION_1 confuses guests
1969d1b4259fSMaxime Coquelin */
197066d1c4c1SMaxime Coquelin if (!proxy->ignore_backend_features &&
197166d1c4c1SMaxime Coquelin !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1972d1b4259fSMaxime Coquelin virtio_pci_disable_modern(proxy);
1973d1b4259fSMaxime Coquelin
1974d1b4259fSMaxime Coquelin if (!legacy) {
1975d1b4259fSMaxime Coquelin error_setg(errp, "Device doesn't support modern mode, and legacy"
1976d1b4259fSMaxime Coquelin " mode is disabled");
1977d1b4259fSMaxime Coquelin error_append_hint(errp, "Set disable-legacy to off\n");
1978d1b4259fSMaxime Coquelin
1979d1b4259fSMaxime Coquelin return;
1980d1b4259fSMaxime Coquelin }
1981d1b4259fSMaxime Coquelin }
1982d1b4259fSMaxime Coquelin
1983d1b4259fSMaxime Coquelin modern = virtio_pci_modern(proxy);
1984d1b4259fSMaxime Coquelin
198549ab747fSPaolo Bonzini config = proxy->pci_dev.config;
198649ab747fSPaolo Bonzini if (proxy->class_code) {
198749ab747fSPaolo Bonzini pci_config_set_class(config, proxy->class_code);
198849ab747fSPaolo Bonzini }
1989e266d421SGerd Hoffmann
1990e266d421SGerd Hoffmann if (legacy) {
19919b3a35ecSCornelia Huck if (!virtio_legacy_allowed(vdev)) {
1992d55f5182SStefano Garzarella /*
1993d55f5182SStefano Garzarella * To avoid migration issues, we allow legacy mode when legacy
1994d55f5182SStefano Garzarella * check is disabled in the old machine types (< 5.1).
1995d55f5182SStefano Garzarella */
1996d55f5182SStefano Garzarella if (virtio_legacy_check_disabled(vdev)) {
1997d55f5182SStefano Garzarella warn_report("device is modern-only, but for backward "
1998d55f5182SStefano Garzarella "compatibility legacy is allowed");
1999d55f5182SStefano Garzarella } else {
2000d55f5182SStefano Garzarella error_setg(errp,
2001d55f5182SStefano Garzarella "device is modern-only, use disable-legacy=on");
20029b3a35ecSCornelia Huck return;
20039b3a35ecSCornelia Huck }
2004d55f5182SStefano Garzarella }
20058607f5c3SJason Wang if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
20068607f5c3SJason Wang error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by"
20072080a29fSGreg Kurz " neither legacy nor transitional device");
20088607f5c3SJason Wang return;
20098607f5c3SJason Wang }
2010f2bc54deSLadi Prosek /*
2011f2bc54deSLadi Prosek * Legacy and transitional devices use specific subsystem IDs.
2012f2bc54deSLadi Prosek * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)
2013f2bc54deSLadi Prosek * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.
2014f2bc54deSLadi Prosek */
201549ab747fSPaolo Bonzini pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
201622733245SLongpeng if (proxy->trans_devid) {
201722733245SLongpeng pci_config_set_device_id(config, proxy->trans_devid);
201822733245SLongpeng }
2019e266d421SGerd Hoffmann } else {
2020e266d421SGerd Hoffmann /* pure virtio-1.0 */
2021e266d421SGerd Hoffmann pci_set_word(config + PCI_VENDOR_ID,
2022e266d421SGerd Hoffmann PCI_VENDOR_ID_REDHAT_QUMRANET);
2023e266d421SGerd Hoffmann pci_set_word(config + PCI_DEVICE_ID,
20240468fe82SGerd Hoffmann PCI_DEVICE_ID_VIRTIO_10_BASE + virtio_bus_get_vdev_id(bus));
2025e266d421SGerd Hoffmann pci_config_set_revision(config, 1);
2026e266d421SGerd Hoffmann }
202749ab747fSPaolo Bonzini config[PCI_INTERRUPT_PIN] = 1;
202849ab747fSPaolo Bonzini
2029dfb8e184SMichael S. Tsirkin
2030e266d421SGerd Hoffmann if (modern) {
2031cc52ea90SGerd Hoffmann struct virtio_pci_cap cap = {
2032cc52ea90SGerd Hoffmann .cap_len = sizeof cap,
2033dfb8e184SMichael S. Tsirkin };
2034dfb8e184SMichael S. Tsirkin struct virtio_pci_notify_cap notify = {
2035dfb8e184SMichael S. Tsirkin .cap.cap_len = sizeof notify,
2036dfb8e184SMichael S. Tsirkin .notify_off_multiplier =
2037d9997d89SMarcel Apfelbaum cpu_to_le32(virtio_pci_queue_mem_mult(proxy)),
2038dfb8e184SMichael S. Tsirkin };
2039ada434cdSMichael S. Tsirkin struct virtio_pci_cfg_cap cfg = {
2040ada434cdSMichael S. Tsirkin .cap.cap_len = sizeof cfg,
2041ada434cdSMichael S. Tsirkin .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
2042ada434cdSMichael S. Tsirkin };
20439824d2a3SJason Wang struct virtio_pci_notify_cap notify_pio = {
20449824d2a3SJason Wang .cap.cap_len = sizeof notify,
20459824d2a3SJason Wang .notify_off_multiplier = cpu_to_le32(0x0),
20469824d2a3SJason Wang };
2047dfb8e184SMichael S. Tsirkin
20489824d2a3SJason Wang struct virtio_pci_cfg_cap *cfg_mask;
2049dfb8e184SMichael S. Tsirkin
2050b74259e3SAlex Bennée virtio_pci_modern_regions_init(proxy, vdev->name);
20519824d2a3SJason Wang
20529824d2a3SJason Wang virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);
20539824d2a3SJason Wang virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);
20549824d2a3SJason Wang virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);
20559824d2a3SJason Wang virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap);
20569824d2a3SJason Wang
20579824d2a3SJason Wang if (modern_pio) {
20589824d2a3SJason Wang memory_region_init(&proxy->io_bar, OBJECT(proxy),
20599824d2a3SJason Wang "virtio-pci-io", 0x4);
2060*55fa4be6SGao Shiyuan address_space_init(&proxy->modern_cfg_io_as, &proxy->io_bar,
2061*55fa4be6SGao Shiyuan "virtio-pci-cfg-io-as");
20629824d2a3SJason Wang
20637a25126dSChen Fan pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,
20649824d2a3SJason Wang PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);
20659824d2a3SJason Wang
20669824d2a3SJason Wang virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,
20679824d2a3SJason Wang ¬ify_pio.cap);
20689824d2a3SJason Wang }
2069ada434cdSMichael S. Tsirkin
20707a25126dSChen Fan pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx,
20714e93a68eSGerd Hoffmann PCI_BASE_ADDRESS_SPACE_MEMORY |
20724e93a68eSGerd Hoffmann PCI_BASE_ADDRESS_MEM_PREFETCH |
20734e93a68eSGerd Hoffmann PCI_BASE_ADDRESS_MEM_TYPE_64,
2074dfb8e184SMichael S. Tsirkin &proxy->modern_bar);
2075ada434cdSMichael S. Tsirkin
2076ada434cdSMichael S. Tsirkin proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
2077ada434cdSMichael S. Tsirkin cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
2078ada434cdSMichael S. Tsirkin pci_set_byte(&cfg_mask->cap.bar, ~0x0);
2079ada434cdSMichael S. Tsirkin pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
2080ada434cdSMichael S. Tsirkin pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
2081ada434cdSMichael S. Tsirkin pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
2082dfb8e184SMichael S. Tsirkin }
2083dfb8e184SMichael S. Tsirkin
20840d583647SRichard Henderson if (proxy->nvectors) {
20850d583647SRichard Henderson int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
2086ee640c62SCao jin proxy->msix_bar_idx, NULL);
20870d583647SRichard Henderson if (err) {
2088ee640c62SCao jin /* Notice when a system that supports MSIx can't initialize it */
20890d583647SRichard Henderson if (err != -ENOTSUP) {
20900765691eSMarkus Armbruster warn_report("unable to init msix vectors to %" PRIu32,
2091c7ff5482SFam Zheng proxy->nvectors);
20920d583647SRichard Henderson }
209349ab747fSPaolo Bonzini proxy->nvectors = 0;
209449ab747fSPaolo Bonzini }
20950d583647SRichard Henderson }
209649ab747fSPaolo Bonzini
209749ab747fSPaolo Bonzini proxy->pci_dev.config_write = virtio_write_config;
2098ada434cdSMichael S. Tsirkin proxy->pci_dev.config_read = virtio_read_config;
209949ab747fSPaolo Bonzini
2100e266d421SGerd Hoffmann if (legacy) {
210149ab747fSPaolo Bonzini size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
210249ab747fSPaolo Bonzini + virtio_bus_get_vdev_config_len(bus);
21031d0148feSPeter Maydell size = pow2ceil(size);
210449ab747fSPaolo Bonzini
2105e266d421SGerd Hoffmann memory_region_init_io(&proxy->bar, OBJECT(proxy),
2106e266d421SGerd Hoffmann &virtio_pci_config_ops,
210722fc860bSPaolo Bonzini proxy, "virtio-pci", size);
2108dfb8e184SMichael S. Tsirkin
21097a25126dSChen Fan pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx,
211023c5e397SGerd Hoffmann PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
2111e266d421SGerd Hoffmann }
211249ab747fSPaolo Bonzini }
211349ab747fSPaolo Bonzini
virtio_pci_device_unplugged(DeviceState * d)211406a13073SPaolo Bonzini static void virtio_pci_device_unplugged(DeviceState *d)
211506a13073SPaolo Bonzini {
211606a13073SPaolo Bonzini VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
21179a4c0e22SMarcel Apfelbaum bool modern = virtio_pci_modern(proxy);
21189824d2a3SJason Wang bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
211906a13073SPaolo Bonzini
212006a13073SPaolo Bonzini virtio_pci_stop_ioeventfd(proxy);
212127462695SMichael S. Tsirkin
212227462695SMichael S. Tsirkin if (modern) {
21239824d2a3SJason Wang virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);
21249824d2a3SJason Wang virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);
21259824d2a3SJason Wang virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);
21269824d2a3SJason Wang virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);
21279824d2a3SJason Wang if (modern_pio) {
21289824d2a3SJason Wang virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);
21299824d2a3SJason Wang }
213027462695SMichael S. Tsirkin }
213106a13073SPaolo Bonzini }
213206a13073SPaolo Bonzini
virtio_pci_realize(PCIDevice * pci_dev,Error ** errp)2133fc079951SMarkus Armbruster static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
213449ab747fSPaolo Bonzini {
2135b6ce27a5SGerd Hoffmann VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
213649ab747fSPaolo Bonzini VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
2137fd56e061SDavid Gibson bool pcie_port = pci_bus_is_express(pci_get_bus(pci_dev)) &&
2138fd56e061SDavid Gibson !pci_bus_is_root(pci_get_bus(pci_dev));
2139fc079951SMarkus Armbruster
21403909c079SPavel Dovgalyuk /* fd-based ioevents can't be synchronized in record/replay */
21413909c079SPavel Dovgalyuk if (replay_mode != REPLAY_MODE_NONE) {
21423909c079SPavel Dovgalyuk proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
21433909c079SPavel Dovgalyuk }
21443909c079SPavel Dovgalyuk
2145b6ce27a5SGerd Hoffmann /*
2146b6ce27a5SGerd Hoffmann * virtio pci bar layout used by default.
2147b6ce27a5SGerd Hoffmann * subclasses can re-arrange things if needed.
2148b6ce27a5SGerd Hoffmann *
2149b6ce27a5SGerd Hoffmann * region 0 -- virtio legacy io bar
2150b6ce27a5SGerd Hoffmann * region 1 -- msi-x bar
2151e6779156SAnthoine Bourgeois * region 2 -- virtio modern io bar (off by default)
2152b6ce27a5SGerd Hoffmann * region 4+5 -- virtio modern memory (64bit) bar
2153b6ce27a5SGerd Hoffmann *
2154b6ce27a5SGerd Hoffmann */
21557a25126dSChen Fan proxy->legacy_io_bar_idx = 0;
21567a25126dSChen Fan proxy->msix_bar_idx = 1;
21577a25126dSChen Fan proxy->modern_io_bar_idx = 2;
21587a25126dSChen Fan proxy->modern_mem_bar_idx = 4;
2159b6ce27a5SGerd Hoffmann
2160b6ce27a5SGerd Hoffmann proxy->common.offset = 0x0;
2161b6ce27a5SGerd Hoffmann proxy->common.size = 0x1000;
2162b6ce27a5SGerd Hoffmann proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
2163b6ce27a5SGerd Hoffmann
2164b6ce27a5SGerd Hoffmann proxy->isr.offset = 0x1000;
2165b6ce27a5SGerd Hoffmann proxy->isr.size = 0x1000;
2166b6ce27a5SGerd Hoffmann proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
2167b6ce27a5SGerd Hoffmann
2168b6ce27a5SGerd Hoffmann proxy->device.offset = 0x2000;
2169b6ce27a5SGerd Hoffmann proxy->device.size = 0x1000;
2170b6ce27a5SGerd Hoffmann proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
2171b6ce27a5SGerd Hoffmann
2172b6ce27a5SGerd Hoffmann proxy->notify.offset = 0x3000;
2173d9997d89SMarcel Apfelbaum proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
2174b6ce27a5SGerd Hoffmann proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
2175b6ce27a5SGerd Hoffmann
21769824d2a3SJason Wang proxy->notify_pio.offset = 0x0;
21779824d2a3SJason Wang proxy->notify_pio.size = 0x4;
21789824d2a3SJason Wang proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
21799824d2a3SJason Wang
2180b6ce27a5SGerd Hoffmann /* subclasses can enforce modern, so do this unconditionally */
2181b6ce27a5SGerd Hoffmann memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
2182d9997d89SMarcel Apfelbaum /* PCI BAR regions must be powers of 2 */
2183d9997d89SMarcel Apfelbaum pow2ceil(proxy->notify.offset + proxy->notify.size));
2184b6ce27a5SGerd Hoffmann
2185*55fa4be6SGao Shiyuan address_space_init(&proxy->modern_cfg_mem_as, &proxy->modern_bar,
2186*55fa4be6SGao Shiyuan "virtio-pci-cfg-mem-as");
2187*55fa4be6SGao Shiyuan
2188dd56040dSDr. David Alan Gilbert if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
2189dd56040dSDr. David Alan Gilbert proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
2190dd56040dSDr. David Alan Gilbert }
2191dd56040dSDr. David Alan Gilbert
2192dd56040dSDr. David Alan Gilbert if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
2193dd56040dSDr. David Alan Gilbert error_setg(errp, "device cannot work as neither modern nor legacy mode"
2194dd56040dSDr. David Alan Gilbert " is enabled");
2195dd56040dSDr. David Alan Gilbert error_append_hint(errp, "Set either disable-modern or disable-legacy"
2196dd56040dSDr. David Alan Gilbert " to off\n");
21973eff3769SGreg Kurz return;
21983eff3769SGreg Kurz }
21993eff3769SGreg Kurz
22009a4c0e22SMarcel Apfelbaum if (pcie_port && pci_is_express(pci_dev)) {
22011811e64cSMarcel Apfelbaum int pos;
220267f5b279SMichael S. Tsirkin uint16_t last_pcie_cap_offset = PCI_CONFIG_SPACE_SIZE;
22031811e64cSMarcel Apfelbaum
22041811e64cSMarcel Apfelbaum pos = pcie_endpoint_cap_init(pci_dev, 0);
22051811e64cSMarcel Apfelbaum assert(pos > 0);
22061811e64cSMarcel Apfelbaum
22079a7c2a59SMao Zhongyi pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0,
22089a7c2a59SMao Zhongyi PCI_PM_SIZEOF, errp);
22099a7c2a59SMao Zhongyi if (pos < 0) {
22109a7c2a59SMao Zhongyi return;
22119a7c2a59SMao Zhongyi }
22129a7c2a59SMao Zhongyi
221327ce0f3aSMarcel Apfelbaum pci_dev->exp.pm_cap = pos;
22141811e64cSMarcel Apfelbaum
22151811e64cSMarcel Apfelbaum /*
22161811e64cSMarcel Apfelbaum * Indicates that this function complies with revision 1.2 of the
22171811e64cSMarcel Apfelbaum * PCI Power Management Interface Specification.
22181811e64cSMarcel Apfelbaum */
22191811e64cSMarcel Apfelbaum pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
2220615c4ed2SJason Wang
2221fdfa3b1dSAndrew Melnychenko if (proxy->flags & VIRTIO_PCI_FLAG_AER) {
222267f5b279SMichael S. Tsirkin pcie_aer_init(pci_dev, PCI_ERR_VER, last_pcie_cap_offset,
2223fdfa3b1dSAndrew Melnychenko PCI_ERR_SIZEOF, NULL);
222467f5b279SMichael S. Tsirkin last_pcie_cap_offset += PCI_ERR_SIZEOF;
2225fdfa3b1dSAndrew Melnychenko }
2226fdfa3b1dSAndrew Melnychenko
2227c2cabb34SMarcel Apfelbaum if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) {
2228c2cabb34SMarcel Apfelbaum /* Init error enabling flags */
2229c2cabb34SMarcel Apfelbaum pcie_cap_deverr_init(pci_dev);
2230c2cabb34SMarcel Apfelbaum }
2231c2cabb34SMarcel Apfelbaum
2232d584f1b9SMarcel Apfelbaum if (proxy->flags & VIRTIO_PCI_FLAG_INIT_LNKCTL) {
2233d584f1b9SMarcel Apfelbaum /* Init Link Control Register */
2234d584f1b9SMarcel Apfelbaum pcie_cap_lnkctl_init(pci_dev);
2235d584f1b9SMarcel Apfelbaum }
2236d584f1b9SMarcel Apfelbaum
22375d98e188SJiqian Chen if (proxy->flags & VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET) {
22385d98e188SJiqian Chen pci_set_word(pci_dev->config + pos + PCI_PM_CTRL,
22395d98e188SJiqian Chen PCI_PM_CTRL_NO_SOFT_RESET);
22405d98e188SJiqian Chen }
22415d98e188SJiqian Chen
224227ce0f3aSMarcel Apfelbaum if (proxy->flags & VIRTIO_PCI_FLAG_INIT_PM) {
224327ce0f3aSMarcel Apfelbaum /* Init Power Management Control Register */
224427ce0f3aSMarcel Apfelbaum pci_set_word(pci_dev->wmask + pos + PCI_PM_CTRL,
224527ce0f3aSMarcel Apfelbaum PCI_PM_CTRL_STATE_MASK);
224627ce0f3aSMarcel Apfelbaum }
224727ce0f3aSMarcel Apfelbaum
2248615c4ed2SJason Wang if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
224967f5b279SMichael S. Tsirkin pcie_ats_init(pci_dev, last_pcie_cap_offset,
2250d83f46d1SJason Wang proxy->flags & VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED);
225167f5b279SMichael S. Tsirkin last_pcie_cap_offset += PCI_EXT_CAP_ATS_SIZEOF;
2252615c4ed2SJason Wang }
2253615c4ed2SJason Wang
2254eb1556c4SJulia Suvorova if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) {
2255eb1556c4SJulia Suvorova /* Set Function Level Reset capability bit */
2256eb1556c4SJulia Suvorova pcie_cap_flr_init(pci_dev);
2257eb1556c4SJulia Suvorova }
22580560b0e9SShmulik Ladkani } else {
22590560b0e9SShmulik Ladkani /*
22600560b0e9SShmulik Ladkani * make future invocations of pci_is_express() return false
22610560b0e9SShmulik Ladkani * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
22620560b0e9SShmulik Ladkani */
22630560b0e9SShmulik Ladkani pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
22641811e64cSMarcel Apfelbaum }
22651811e64cSMarcel Apfelbaum
2266b6ce27a5SGerd Hoffmann virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
2267fc079951SMarkus Armbruster if (k->realize) {
2268b6ce27a5SGerd Hoffmann k->realize(proxy, errp);
226949ab747fSPaolo Bonzini }
227049ab747fSPaolo Bonzini }
227149ab747fSPaolo Bonzini
virtio_pci_exit(PCIDevice * pci_dev)227249ab747fSPaolo Bonzini static void virtio_pci_exit(PCIDevice *pci_dev)
227349ab747fSPaolo Bonzini {
2274fdfa3b1dSAndrew Melnychenko VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
2275fdfa3b1dSAndrew Melnychenko bool pcie_port = pci_bus_is_express(pci_get_bus(pci_dev)) &&
2276fdfa3b1dSAndrew Melnychenko !pci_bus_is_root(pci_get_bus(pci_dev));
2277*55fa4be6SGao Shiyuan bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
2278fdfa3b1dSAndrew Melnychenko
22798b81bb3bSPaolo Bonzini msix_uninit_exclusive_bar(pci_dev);
2280fdfa3b1dSAndrew Melnychenko if (proxy->flags & VIRTIO_PCI_FLAG_AER && pcie_port &&
2281fdfa3b1dSAndrew Melnychenko pci_is_express(pci_dev)) {
2282fdfa3b1dSAndrew Melnychenko pcie_aer_exit(pci_dev);
2283fdfa3b1dSAndrew Melnychenko }
2284*55fa4be6SGao Shiyuan address_space_destroy(&proxy->modern_cfg_mem_as);
2285*55fa4be6SGao Shiyuan if (modern_pio) {
2286*55fa4be6SGao Shiyuan address_space_destroy(&proxy->modern_cfg_io_as);
2287*55fa4be6SGao Shiyuan }
228849ab747fSPaolo Bonzini }
228949ab747fSPaolo Bonzini
virtio_pci_reset(DeviceState * qdev)229059ccd20aSKONRAD Frederic static void virtio_pci_reset(DeviceState *qdev)
229149ab747fSPaolo Bonzini {
229249ab747fSPaolo Bonzini VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
229349ab747fSPaolo Bonzini VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
2294393f04d3SJason Wang int i;
2295393f04d3SJason Wang
229649ab747fSPaolo Bonzini virtio_bus_reset(bus);
229749ab747fSPaolo Bonzini msix_unuse_all_vectors(&proxy->pci_dev);
2298393f04d3SJason Wang
2299393f04d3SJason Wang for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
2300393f04d3SJason Wang proxy->vqs[i].enabled = 0;
2301805d782dSXuan Zhuo proxy->vqs[i].reset = 0;
230260a8d802SJason Wang proxy->vqs[i].num = 0;
230360a8d802SJason Wang proxy->vqs[i].desc[0] = proxy->vqs[i].desc[1] = 0;
230460a8d802SJason Wang proxy->vqs[i].avail[0] = proxy->vqs[i].avail[1] = 0;
230560a8d802SJason Wang proxy->vqs[i].used[0] = proxy->vqs[i].used[1] = 0;
2306393f04d3SJason Wang }
23079afb4177SMichael S. Tsirkin }
23089afb4177SMichael S. Tsirkin
virtio_pci_no_soft_reset(PCIDevice * dev)23095d98e188SJiqian Chen static bool virtio_pci_no_soft_reset(PCIDevice *dev)
23105d98e188SJiqian Chen {
23115d98e188SJiqian Chen uint16_t pmcsr;
23125d98e188SJiqian Chen
23135d98e188SJiqian Chen if (!pci_is_express(dev) || !dev->exp.pm_cap) {
23145d98e188SJiqian Chen return false;
23155d98e188SJiqian Chen }
23165d98e188SJiqian Chen
23175d98e188SJiqian Chen pmcsr = pci_get_word(dev->config + dev->exp.pm_cap + PCI_PM_CTRL);
23185d98e188SJiqian Chen
23195d98e188SJiqian Chen /*
23205d98e188SJiqian Chen * When No_Soft_Reset bit is set and the device
23215d98e188SJiqian Chen * is in D3hot state, don't reset device
23225d98e188SJiqian Chen */
23235d98e188SJiqian Chen return (pmcsr & PCI_PM_CTRL_NO_SOFT_RESET) &&
23245d98e188SJiqian Chen (pmcsr & PCI_PM_CTRL_STATE_MASK) == 3;
23255d98e188SJiqian Chen }
23265d98e188SJiqian Chen
virtio_pci_bus_reset_hold(Object * obj,ResetType type)2327ad80e367SPeter Maydell static void virtio_pci_bus_reset_hold(Object *obj, ResetType type)
23289afb4177SMichael S. Tsirkin {
232954da4183SPeter Maydell PCIDevice *dev = PCI_DEVICE(obj);
233054da4183SPeter Maydell DeviceState *qdev = DEVICE(obj);
23319afb4177SMichael S. Tsirkin
23325d98e188SJiqian Chen if (virtio_pci_no_soft_reset(dev)) {
23335d98e188SJiqian Chen return;
23345d98e188SJiqian Chen }
23355d98e188SJiqian Chen
23369afb4177SMichael S. Tsirkin virtio_pci_reset(qdev);
2337c2cabb34SMarcel Apfelbaum
2338c2cabb34SMarcel Apfelbaum if (pci_is_express(dev)) {
233984b58169SJiqian Chen VirtIOPCIProxy *proxy = VIRTIO_PCI(dev);
234084b58169SJiqian Chen
2341c2cabb34SMarcel Apfelbaum pcie_cap_deverr_reset(dev);
2342d584f1b9SMarcel Apfelbaum pcie_cap_lnkctl_reset(dev);
234327ce0f3aSMarcel Apfelbaum
234484b58169SJiqian Chen if (proxy->flags & VIRTIO_PCI_FLAG_INIT_PM) {
234584b58169SJiqian Chen pci_word_test_and_clear_mask(
234684b58169SJiqian Chen dev->config + dev->exp.pm_cap + PCI_PM_CTRL,
234784b58169SJiqian Chen PCI_PM_CTRL_STATE_MASK);
234884b58169SJiqian Chen }
2349c2cabb34SMarcel Apfelbaum }
235049ab747fSPaolo Bonzini }
235149ab747fSPaolo Bonzini
235285d1277eSMing Lei static Property virtio_pci_properties[] = {
235368a27b20SMichael S. Tsirkin DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
235468a27b20SMichael S. Tsirkin VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
2355a6df8adfSJason Wang DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
2356a6df8adfSJason Wang VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
23579824d2a3SJason Wang DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
23589824d2a3SJason Wang VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
23591811e64cSMarcel Apfelbaum DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
23601811e64cSMarcel Apfelbaum VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
2361d9997d89SMarcel Apfelbaum DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags,
2362d9997d89SMarcel Apfelbaum VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),
236366d1c4c1SMaxime Coquelin DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy,
236466d1c4c1SMaxime Coquelin ignore_backend_features, false),
2365615c4ed2SJason Wang DEFINE_PROP_BIT("ats", VirtIOPCIProxy, flags,
2366615c4ed2SJason Wang VIRTIO_PCI_FLAG_ATS_BIT, false),
2367d83f46d1SJason Wang DEFINE_PROP_BIT("x-ats-page-aligned", VirtIOPCIProxy, flags,
2368d83f46d1SJason Wang VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT, true),
2369c2cabb34SMarcel Apfelbaum DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy, flags,
2370c2cabb34SMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, true),
2371d584f1b9SMarcel Apfelbaum DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy, flags,
2372d584f1b9SMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, true),
237327ce0f3aSMarcel Apfelbaum DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy, flags,
237427ce0f3aSMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_PM_BIT, true),
23755d98e188SJiqian Chen DEFINE_PROP_BIT("x-pcie-pm-no-soft-reset", VirtIOPCIProxy, flags,
23765d98e188SJiqian Chen VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET_BIT, false),
2377eb1556c4SJulia Suvorova DEFINE_PROP_BIT("x-pcie-flr-init", VirtIOPCIProxy, flags,
2378eb1556c4SJulia Suvorova VIRTIO_PCI_FLAG_INIT_FLR_BIT, true),
2379fdfa3b1dSAndrew Melnychenko DEFINE_PROP_BIT("aer", VirtIOPCIProxy, flags,
2380fdfa3b1dSAndrew Melnychenko VIRTIO_PCI_FLAG_AER_BIT, false),
238185d1277eSMing Lei DEFINE_PROP_END_OF_LIST(),
238285d1277eSMing Lei };
238385d1277eSMing Lei
virtio_pci_dc_realize(DeviceState * qdev,Error ** errp)23840560b0e9SShmulik Ladkani static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
23850560b0e9SShmulik Ladkani {
23860560b0e9SShmulik Ladkani VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
23870560b0e9SShmulik Ladkani VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
23880560b0e9SShmulik Ladkani PCIDevice *pci_dev = &proxy->pci_dev;
23890560b0e9SShmulik Ladkani
23900560b0e9SShmulik Ladkani if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
23919a4c0e22SMarcel Apfelbaum virtio_pci_modern(proxy)) {
23920560b0e9SShmulik Ladkani pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
23930560b0e9SShmulik Ladkani }
23940560b0e9SShmulik Ladkani
23950560b0e9SShmulik Ladkani vpciklass->parent_dc_realize(qdev, errp);
23960560b0e9SShmulik Ladkani }
23970560b0e9SShmulik Ladkani
virtio_pci_sync_config(DeviceState * dev,Error ** errp)23983f98408eSVladimir Sementsov-Ogievskiy static int virtio_pci_sync_config(DeviceState *dev, Error **errp)
23993f98408eSVladimir Sementsov-Ogievskiy {
24003f98408eSVladimir Sementsov-Ogievskiy VirtIOPCIProxy *proxy = VIRTIO_PCI(dev);
24013f98408eSVladimir Sementsov-Ogievskiy VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
24023f98408eSVladimir Sementsov-Ogievskiy
24033f98408eSVladimir Sementsov-Ogievskiy return qdev_sync_config(DEVICE(vdev), errp);
24043f98408eSVladimir Sementsov-Ogievskiy }
24053f98408eSVladimir Sementsov-Ogievskiy
virtio_pci_class_init(ObjectClass * klass,void * data)240649ab747fSPaolo Bonzini static void virtio_pci_class_init(ObjectClass *klass, void *data)
240749ab747fSPaolo Bonzini {
240849ab747fSPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass);
240949ab747fSPaolo Bonzini PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
24100560b0e9SShmulik Ladkani VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
241154da4183SPeter Maydell ResettableClass *rc = RESETTABLE_CLASS(klass);
241249ab747fSPaolo Bonzini
24134f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_pci_properties);
2414fc079951SMarkus Armbruster k->realize = virtio_pci_realize;
241549ab747fSPaolo Bonzini k->exit = virtio_pci_exit;
241649ab747fSPaolo Bonzini k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
241749ab747fSPaolo Bonzini k->revision = VIRTIO_PCI_ABI_VERSION;
241849ab747fSPaolo Bonzini k->class_id = PCI_CLASS_OTHERS;
2419bf853881SPhilippe Mathieu-Daudé device_class_set_parent_realize(dc, virtio_pci_dc_realize,
2420bf853881SPhilippe Mathieu-Daudé &vpciklass->parent_dc_realize);
242154da4183SPeter Maydell rc->phases.hold = virtio_pci_bus_reset_hold;
24223f98408eSVladimir Sementsov-Ogievskiy dc->sync_config = virtio_pci_sync_config;
242349ab747fSPaolo Bonzini }
242449ab747fSPaolo Bonzini
242549ab747fSPaolo Bonzini static const TypeInfo virtio_pci_info = {
242649ab747fSPaolo Bonzini .name = TYPE_VIRTIO_PCI,
242749ab747fSPaolo Bonzini .parent = TYPE_PCI_DEVICE,
242849ab747fSPaolo Bonzini .instance_size = sizeof(VirtIOPCIProxy),
242949ab747fSPaolo Bonzini .class_init = virtio_pci_class_init,
243049ab747fSPaolo Bonzini .class_size = sizeof(VirtioPCIClass),
243149ab747fSPaolo Bonzini .abstract = true,
2432a4ee4c8bSEduardo Habkost };
2433a4ee4c8bSEduardo Habkost
2434a4ee4c8bSEduardo Habkost static Property virtio_pci_generic_properties[] = {
2435a4ee4c8bSEduardo Habkost DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy,
2436a4ee4c8bSEduardo Habkost ON_OFF_AUTO_AUTO),
2437a4ee4c8bSEduardo Habkost DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false),
2438a4ee4c8bSEduardo Habkost DEFINE_PROP_END_OF_LIST(),
2439a4ee4c8bSEduardo Habkost };
2440a4ee4c8bSEduardo Habkost
virtio_pci_base_class_init(ObjectClass * klass,void * data)2441a4ee4c8bSEduardo Habkost static void virtio_pci_base_class_init(ObjectClass *klass, void *data)
2442a4ee4c8bSEduardo Habkost {
2443a4ee4c8bSEduardo Habkost const VirtioPCIDeviceTypeInfo *t = data;
2444a4ee4c8bSEduardo Habkost if (t->class_init) {
2445a4ee4c8bSEduardo Habkost t->class_init(klass, NULL);
2446a4ee4c8bSEduardo Habkost }
2447a4ee4c8bSEduardo Habkost }
2448a4ee4c8bSEduardo Habkost
virtio_pci_generic_class_init(ObjectClass * klass,void * data)2449a4ee4c8bSEduardo Habkost static void virtio_pci_generic_class_init(ObjectClass *klass, void *data)
2450a4ee4c8bSEduardo Habkost {
2451a4ee4c8bSEduardo Habkost DeviceClass *dc = DEVICE_CLASS(klass);
2452a4ee4c8bSEduardo Habkost
24534f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_pci_generic_properties);
2454a4ee4c8bSEduardo Habkost }
2455a4ee4c8bSEduardo Habkost
virtio_pci_transitional_instance_init(Object * obj)2456a4ee4c8bSEduardo Habkost static void virtio_pci_transitional_instance_init(Object *obj)
2457a4ee4c8bSEduardo Habkost {
2458a4ee4c8bSEduardo Habkost VirtIOPCIProxy *proxy = VIRTIO_PCI(obj);
2459a4ee4c8bSEduardo Habkost
2460a4ee4c8bSEduardo Habkost proxy->disable_legacy = ON_OFF_AUTO_OFF;
2461a4ee4c8bSEduardo Habkost proxy->disable_modern = false;
2462a4ee4c8bSEduardo Habkost }
2463a4ee4c8bSEduardo Habkost
virtio_pci_non_transitional_instance_init(Object * obj)2464a4ee4c8bSEduardo Habkost static void virtio_pci_non_transitional_instance_init(Object *obj)
2465a4ee4c8bSEduardo Habkost {
2466a4ee4c8bSEduardo Habkost VirtIOPCIProxy *proxy = VIRTIO_PCI(obj);
2467a4ee4c8bSEduardo Habkost
2468a4ee4c8bSEduardo Habkost proxy->disable_legacy = ON_OFF_AUTO_ON;
2469a4ee4c8bSEduardo Habkost proxy->disable_modern = false;
2470a4ee4c8bSEduardo Habkost }
2471a4ee4c8bSEduardo Habkost
virtio_pci_types_register(const VirtioPCIDeviceTypeInfo * t)2472a4ee4c8bSEduardo Habkost void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t)
2473a4ee4c8bSEduardo Habkost {
2474683c1d89SMarc-André Lureau char *base_name = NULL;
2475a4ee4c8bSEduardo Habkost TypeInfo base_type_info = {
2476a4ee4c8bSEduardo Habkost .name = t->base_name,
2477a4ee4c8bSEduardo Habkost .parent = t->parent ? t->parent : TYPE_VIRTIO_PCI,
2478a4ee4c8bSEduardo Habkost .instance_size = t->instance_size,
2479a4ee4c8bSEduardo Habkost .instance_init = t->instance_init,
2480837053a7SPhilippe Mathieu-Daudé .instance_finalize = t->instance_finalize,
24818ea90ee6SGerd Hoffmann .class_size = t->class_size,
2482a4ee4c8bSEduardo Habkost .abstract = true,
24831e33b513SDavid Hildenbrand .interfaces = t->interfaces,
2484a4ee4c8bSEduardo Habkost };
2485a4ee4c8bSEduardo Habkost TypeInfo generic_type_info = {
2486a4ee4c8bSEduardo Habkost .name = t->generic_name,
2487a4ee4c8bSEduardo Habkost .parent = base_type_info.name,
2488a4ee4c8bSEduardo Habkost .class_init = virtio_pci_generic_class_init,
2489a5fa336fSEduardo Habkost .interfaces = (InterfaceInfo[]) {
2490a5fa336fSEduardo Habkost { INTERFACE_PCIE_DEVICE },
2491a5fa336fSEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE },
2492a5fa336fSEduardo Habkost { }
2493a5fa336fSEduardo Habkost },
249449ab747fSPaolo Bonzini };
249549ab747fSPaolo Bonzini
2496a4ee4c8bSEduardo Habkost if (!base_type_info.name) {
2497a4ee4c8bSEduardo Habkost /* No base type -> register a single generic device type */
2498683c1d89SMarc-André Lureau /* use intermediate %s-base-type to add generic device props */
2499683c1d89SMarc-André Lureau base_name = g_strdup_printf("%s-base-type", t->generic_name);
2500683c1d89SMarc-André Lureau base_type_info.name = base_name;
2501683c1d89SMarc-André Lureau base_type_info.class_init = virtio_pci_generic_class_init;
2502683c1d89SMarc-André Lureau
2503683c1d89SMarc-André Lureau generic_type_info.parent = base_name;
2504683c1d89SMarc-André Lureau generic_type_info.class_init = virtio_pci_base_class_init;
2505683c1d89SMarc-André Lureau generic_type_info.class_data = (void *)t;
2506683c1d89SMarc-André Lureau
2507a4ee4c8bSEduardo Habkost assert(!t->non_transitional_name);
2508a4ee4c8bSEduardo Habkost assert(!t->transitional_name);
2509683c1d89SMarc-André Lureau } else {
2510683c1d89SMarc-André Lureau base_type_info.class_init = virtio_pci_base_class_init;
2511683c1d89SMarc-André Lureau base_type_info.class_data = (void *)t;
2512a4ee4c8bSEduardo Habkost }
2513a4ee4c8bSEduardo Habkost
2514a4ee4c8bSEduardo Habkost type_register(&base_type_info);
2515a4ee4c8bSEduardo Habkost if (generic_type_info.name) {
2516a4ee4c8bSEduardo Habkost type_register(&generic_type_info);
2517a4ee4c8bSEduardo Habkost }
2518a4ee4c8bSEduardo Habkost
2519a4ee4c8bSEduardo Habkost if (t->non_transitional_name) {
2520a4ee4c8bSEduardo Habkost const TypeInfo non_transitional_type_info = {
2521a4ee4c8bSEduardo Habkost .name = t->non_transitional_name,
2522a4ee4c8bSEduardo Habkost .parent = base_type_info.name,
2523a4ee4c8bSEduardo Habkost .instance_init = virtio_pci_non_transitional_instance_init,
2524a4ee4c8bSEduardo Habkost .interfaces = (InterfaceInfo[]) {
2525a4ee4c8bSEduardo Habkost { INTERFACE_PCIE_DEVICE },
2526a4ee4c8bSEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE },
2527a4ee4c8bSEduardo Habkost { }
2528a4ee4c8bSEduardo Habkost },
2529a4ee4c8bSEduardo Habkost };
2530a4ee4c8bSEduardo Habkost type_register(&non_transitional_type_info);
2531a4ee4c8bSEduardo Habkost }
2532a4ee4c8bSEduardo Habkost
2533a4ee4c8bSEduardo Habkost if (t->transitional_name) {
2534a4ee4c8bSEduardo Habkost const TypeInfo transitional_type_info = {
2535a4ee4c8bSEduardo Habkost .name = t->transitional_name,
2536a4ee4c8bSEduardo Habkost .parent = base_type_info.name,
2537a4ee4c8bSEduardo Habkost .instance_init = virtio_pci_transitional_instance_init,
2538a4ee4c8bSEduardo Habkost .interfaces = (InterfaceInfo[]) {
2539a4ee4c8bSEduardo Habkost /*
2540a4ee4c8bSEduardo Habkost * Transitional virtio devices work only as Conventional PCI
2541a4ee4c8bSEduardo Habkost * devices because they require PIO ports.
2542a4ee4c8bSEduardo Habkost */
2543a4ee4c8bSEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE },
2544a4ee4c8bSEduardo Habkost { }
2545a4ee4c8bSEduardo Habkost },
2546a4ee4c8bSEduardo Habkost };
2547a4ee4c8bSEduardo Habkost type_register(&transitional_type_info);
2548a4ee4c8bSEduardo Habkost }
2549683c1d89SMarc-André Lureau g_free(base_name);
2550a4ee4c8bSEduardo Habkost }
2551a4ee4c8bSEduardo Habkost
virtio_pci_optimal_num_queues(unsigned fixed_queues)25521436f32aSStefan Hajnoczi unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues)
25531436f32aSStefan Hajnoczi {
25541436f32aSStefan Hajnoczi /*
25551436f32aSStefan Hajnoczi * 1:1 vq to vCPU mapping is ideal because the same vCPU that submitted
25561436f32aSStefan Hajnoczi * virtqueue buffers can handle their completion. When a different vCPU
25571436f32aSStefan Hajnoczi * handles completion it may need to IPI the vCPU that submitted the
25581436f32aSStefan Hajnoczi * request and this adds overhead.
25591436f32aSStefan Hajnoczi *
25601436f32aSStefan Hajnoczi * Virtqueues consume guest RAM and MSI-X vectors. This is wasteful in
25611436f32aSStefan Hajnoczi * guests with very many vCPUs and a device that is only used by a few
25621436f32aSStefan Hajnoczi * vCPUs. Unfortunately optimizing that case requires manual pinning inside
25631436f32aSStefan Hajnoczi * the guest, so those users might as well manually set the number of
25641436f32aSStefan Hajnoczi * queues. There is no upper limit that can be applied automatically and
25651436f32aSStefan Hajnoczi * doing so arbitrarily would result in a sudden performance drop once the
25661436f32aSStefan Hajnoczi * threshold number of vCPUs is exceeded.
25671436f32aSStefan Hajnoczi */
25681436f32aSStefan Hajnoczi unsigned num_queues = current_machine->smp.cpus;
25691436f32aSStefan Hajnoczi
25701436f32aSStefan Hajnoczi /*
25711436f32aSStefan Hajnoczi * The maximum number of MSI-X vectors is PCI_MSIX_FLAGS_QSIZE + 1, but the
25721436f32aSStefan Hajnoczi * config change interrupt and the fixed virtqueues must be taken into
25731436f32aSStefan Hajnoczi * account too.
25741436f32aSStefan Hajnoczi */
25751436f32aSStefan Hajnoczi num_queues = MIN(num_queues, PCI_MSIX_FLAGS_QSIZE - fixed_queues);
25761436f32aSStefan Hajnoczi
25771436f32aSStefan Hajnoczi /*
25781436f32aSStefan Hajnoczi * There is a limit to how many virtqueues a device can have.
25791436f32aSStefan Hajnoczi */
25801436f32aSStefan Hajnoczi return MIN(num_queues, VIRTIO_QUEUE_MAX - fixed_queues);
25811436f32aSStefan Hajnoczi }
25821436f32aSStefan Hajnoczi
258349ab747fSPaolo Bonzini /* virtio-pci-bus */
258449ab747fSPaolo Bonzini
virtio_pci_bus_new(VirtioBusState * bus,size_t bus_size,VirtIOPCIProxy * dev)2585ac7af112SAndreas Färber static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2586ac7af112SAndreas Färber VirtIOPCIProxy *dev)
258749ab747fSPaolo Bonzini {
258849ab747fSPaolo Bonzini DeviceState *qdev = DEVICE(dev);
2589f4dd69aaSKONRAD Frederic char virtio_bus_name[] = "virtio-bus";
2590f4dd69aaSKONRAD Frederic
2591d637e1dcSPeter Maydell qbus_init(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, virtio_bus_name);
259249ab747fSPaolo Bonzini }
259349ab747fSPaolo Bonzini
virtio_pci_bus_class_init(ObjectClass * klass,void * data)259449ab747fSPaolo Bonzini static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
259549ab747fSPaolo Bonzini {
259649ab747fSPaolo Bonzini BusClass *bus_class = BUS_CLASS(klass);
259749ab747fSPaolo Bonzini VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
259849ab747fSPaolo Bonzini bus_class->max_dev = 1;
259949ab747fSPaolo Bonzini k->notify = virtio_pci_notify;
260049ab747fSPaolo Bonzini k->save_config = virtio_pci_save_config;
260149ab747fSPaolo Bonzini k->load_config = virtio_pci_load_config;
260249ab747fSPaolo Bonzini k->save_queue = virtio_pci_save_queue;
260349ab747fSPaolo Bonzini k->load_queue = virtio_pci_load_queue;
2604a6df8adfSJason Wang k->save_extra_state = virtio_pci_save_extra_state;
2605a6df8adfSJason Wang k->load_extra_state = virtio_pci_load_extra_state;
2606a6df8adfSJason Wang k->has_extra_state = virtio_pci_has_extra_state;
260749ab747fSPaolo Bonzini k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
260849ab747fSPaolo Bonzini k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
26096f80e617STiwei Bie k->set_host_notifier_mr = virtio_pci_set_host_notifier_mr;
261049ab747fSPaolo Bonzini k->vmstate_change = virtio_pci_vmstate_change;
2611d1b4259fSMaxime Coquelin k->pre_plugged = virtio_pci_pre_plugged;
261249ab747fSPaolo Bonzini k->device_plugged = virtio_pci_device_plugged;
261306a13073SPaolo Bonzini k->device_unplugged = virtio_pci_device_unplugged;
2614e0d686bfSJason Wang k->query_nvectors = virtio_pci_query_nvectors;
26158e93cef1SPaolo Bonzini k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled;
26169f06e71aSCornelia Huck k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
26178607f5c3SJason Wang k->get_dma_as = virtio_pci_get_dma_as;
26183d1e5d86SJason Wang k->iommu_enabled = virtio_pci_iommu_enabled;
2619f19bcdfeSJason Wang k->queue_enabled = virtio_pci_queue_enabled;
262049ab747fSPaolo Bonzini }
262149ab747fSPaolo Bonzini
262249ab747fSPaolo Bonzini static const TypeInfo virtio_pci_bus_info = {
262349ab747fSPaolo Bonzini .name = TYPE_VIRTIO_PCI_BUS,
262449ab747fSPaolo Bonzini .parent = TYPE_VIRTIO_BUS,
262549ab747fSPaolo Bonzini .instance_size = sizeof(VirtioPCIBusState),
262674ded8b4SCornelia Huck .class_size = sizeof(VirtioPCIBusClass),
262749ab747fSPaolo Bonzini .class_init = virtio_pci_bus_class_init,
262849ab747fSPaolo Bonzini };
262949ab747fSPaolo Bonzini
virtio_pci_register_types(void)263049ab747fSPaolo Bonzini static void virtio_pci_register_types(void)
263149ab747fSPaolo Bonzini {
2632a4ee4c8bSEduardo Habkost /* Base types: */
263349ab747fSPaolo Bonzini type_register_static(&virtio_pci_bus_info);
263449ab747fSPaolo Bonzini type_register_static(&virtio_pci_info);
263549ab747fSPaolo Bonzini }
263649ab747fSPaolo Bonzini
263749ab747fSPaolo Bonzini type_init(virtio_pci_register_types)
2638271458d7SJuan Quintela
2639