xref: /openbmc/qemu/hw/virtio/virtio-pci.c (revision 10358b6a)
1 /*
2  * Virtio PCI Bindings
3  *
4  * Copyright IBM, Corp. 2007
5  * Copyright (c) 2009 CodeSourcery
6  *
7  * Authors:
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *  Paul Brook        <paul@codesourcery.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  *
14  * Contributions after 2012-01-13 are licensed under the terms of the
15  * GNU GPL, version 2 or (at your option) any later version.
16  */
17 
18 #include <inttypes.h>
19 
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-blk.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "hw/virtio/virtio-serial.h"
24 #include "hw/virtio/virtio-scsi.h"
25 #include "hw/virtio/virtio-balloon.h"
26 #include "hw/pci/pci.h"
27 #include "qemu/error-report.h"
28 #include "hw/pci/msi.h"
29 #include "hw/pci/msix.h"
30 #include "hw/loader.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/blockdev.h"
33 #include "virtio-pci.h"
34 #include "qemu/range.h"
35 #include "hw/virtio/virtio-bus.h"
36 #include "qapi/visitor.h"
37 
38 /* from Linux's linux/virtio_pci.h */
39 
40 /* A 32-bit r/o bitmask of the features supported by the host */
41 #define VIRTIO_PCI_HOST_FEATURES        0
42 
43 /* A 32-bit r/w bitmask of features activated by the guest */
44 #define VIRTIO_PCI_GUEST_FEATURES       4
45 
46 /* A 32-bit r/w PFN for the currently selected queue */
47 #define VIRTIO_PCI_QUEUE_PFN            8
48 
49 /* A 16-bit r/o queue size for the currently selected queue */
50 #define VIRTIO_PCI_QUEUE_NUM            12
51 
52 /* A 16-bit r/w queue selector */
53 #define VIRTIO_PCI_QUEUE_SEL            14
54 
55 /* A 16-bit r/w queue notifier */
56 #define VIRTIO_PCI_QUEUE_NOTIFY         16
57 
58 /* An 8-bit device status register.  */
59 #define VIRTIO_PCI_STATUS               18
60 
61 /* An 8-bit r/o interrupt status register.  Reading the value will return the
62  * current contents of the ISR and will also clear it.  This is effectively
63  * a read-and-acknowledge. */
64 #define VIRTIO_PCI_ISR                  19
65 
66 /* MSI-X registers: only enabled if MSI-X is enabled. */
67 /* A 16-bit vector for configuration changes. */
68 #define VIRTIO_MSI_CONFIG_VECTOR        20
69 /* A 16-bit vector for selected queue notifications. */
70 #define VIRTIO_MSI_QUEUE_VECTOR         22
71 
72 /* Config space size */
73 #define VIRTIO_PCI_CONFIG_NOMSI         20
74 #define VIRTIO_PCI_CONFIG_MSI           24
75 #define VIRTIO_PCI_REGION_SIZE(dev)     (msix_present(dev) ? \
76                                          VIRTIO_PCI_CONFIG_MSI : \
77                                          VIRTIO_PCI_CONFIG_NOMSI)
78 
79 /* The remaining space is defined by each driver as the per-driver
80  * configuration space */
81 #define VIRTIO_PCI_CONFIG(dev)          (msix_enabled(dev) ? \
82                                          VIRTIO_PCI_CONFIG_MSI : \
83                                          VIRTIO_PCI_CONFIG_NOMSI)
84 
85 /* How many bits to shift physical queue address written to QUEUE_PFN.
86  * 12 is historical, and due to x86 page size. */
87 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
88 
89 /* Flags track per-device state like workarounds for quirks in older guests. */
90 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
91 
92 /* HACK for virtio to determine if it's running a big endian guest */
93 bool virtio_is_big_endian(void);
94 
95 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
96                                VirtIOPCIProxy *dev);
97 
98 /* virtio device */
99 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
100 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
101 {
102     return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
103 }
104 
105 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
106  * be careful and test performance if you change this.
107  */
108 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
109 {
110     return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
111 }
112 
113 static void virtio_pci_notify(DeviceState *d, uint16_t vector)
114 {
115     VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
116 
117     if (msix_enabled(&proxy->pci_dev))
118         msix_notify(&proxy->pci_dev, vector);
119     else {
120         VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
121         pci_set_irq(&proxy->pci_dev, vdev->isr & 1);
122     }
123 }
124 
125 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
126 {
127     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
128     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
129 
130     pci_device_save(&proxy->pci_dev, f);
131     msix_save(&proxy->pci_dev, f);
132     if (msix_present(&proxy->pci_dev))
133         qemu_put_be16(f, vdev->config_vector);
134 }
135 
136 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
137 {
138     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
139     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
140 
141     if (msix_present(&proxy->pci_dev))
142         qemu_put_be16(f, virtio_queue_vector(vdev, n));
143 }
144 
145 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
146 {
147     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
148     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
149 
150     int ret;
151     ret = pci_device_load(&proxy->pci_dev, f);
152     if (ret) {
153         return ret;
154     }
155     msix_unuse_all_vectors(&proxy->pci_dev);
156     msix_load(&proxy->pci_dev, f);
157     if (msix_present(&proxy->pci_dev)) {
158         qemu_get_be16s(f, &vdev->config_vector);
159     } else {
160         vdev->config_vector = VIRTIO_NO_VECTOR;
161     }
162     if (vdev->config_vector != VIRTIO_NO_VECTOR) {
163         return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
164     }
165     return 0;
166 }
167 
168 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
169 {
170     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
171     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
172 
173     uint16_t vector;
174     if (msix_present(&proxy->pci_dev)) {
175         qemu_get_be16s(f, &vector);
176     } else {
177         vector = VIRTIO_NO_VECTOR;
178     }
179     virtio_queue_set_vector(vdev, n, vector);
180     if (vector != VIRTIO_NO_VECTOR) {
181         return msix_vector_use(&proxy->pci_dev, vector);
182     }
183     return 0;
184 }
185 
186 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
187                                                  int n, bool assign, bool set_handler)
188 {
189     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
190     VirtQueue *vq = virtio_get_queue(vdev, n);
191     EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
192     int r = 0;
193 
194     if (assign) {
195         r = event_notifier_init(notifier, 1);
196         if (r < 0) {
197             error_report("%s: unable to init event notifier: %d",
198                          __func__, r);
199             return r;
200         }
201         virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
202         memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
203                                   true, n, notifier);
204     } else {
205         memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
206                                   true, n, notifier);
207         virtio_queue_set_host_notifier_fd_handler(vq, false, false);
208         event_notifier_cleanup(notifier);
209     }
210     return r;
211 }
212 
213 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
214 {
215     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
216     int n, r;
217 
218     if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
219         proxy->ioeventfd_disabled ||
220         proxy->ioeventfd_started) {
221         return;
222     }
223 
224     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
225         if (!virtio_queue_get_num(vdev, n)) {
226             continue;
227         }
228 
229         r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
230         if (r < 0) {
231             goto assign_error;
232         }
233     }
234     proxy->ioeventfd_started = true;
235     return;
236 
237 assign_error:
238     while (--n >= 0) {
239         if (!virtio_queue_get_num(vdev, n)) {
240             continue;
241         }
242 
243         r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
244         assert(r >= 0);
245     }
246     proxy->ioeventfd_started = false;
247     error_report("%s: failed. Fallback to a userspace (slower).", __func__);
248 }
249 
250 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
251 {
252     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
253     int r;
254     int n;
255 
256     if (!proxy->ioeventfd_started) {
257         return;
258     }
259 
260     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
261         if (!virtio_queue_get_num(vdev, n)) {
262             continue;
263         }
264 
265         r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
266         assert(r >= 0);
267     }
268     proxy->ioeventfd_started = false;
269 }
270 
271 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
272 {
273     VirtIOPCIProxy *proxy = opaque;
274     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
275     hwaddr pa;
276 
277     switch (addr) {
278     case VIRTIO_PCI_GUEST_FEATURES:
279         /* Guest does not negotiate properly?  We have to assume nothing. */
280         if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
281             val = virtio_bus_get_vdev_bad_features(&proxy->bus);
282         }
283         virtio_set_features(vdev, val);
284         break;
285     case VIRTIO_PCI_QUEUE_PFN:
286         pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
287         if (pa == 0) {
288             virtio_pci_stop_ioeventfd(proxy);
289             virtio_reset(vdev);
290             msix_unuse_all_vectors(&proxy->pci_dev);
291         }
292         else
293             virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
294         break;
295     case VIRTIO_PCI_QUEUE_SEL:
296         if (val < VIRTIO_PCI_QUEUE_MAX)
297             vdev->queue_sel = val;
298         break;
299     case VIRTIO_PCI_QUEUE_NOTIFY:
300         if (val < VIRTIO_PCI_QUEUE_MAX) {
301             virtio_queue_notify(vdev, val);
302         }
303         break;
304     case VIRTIO_PCI_STATUS:
305         if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
306             virtio_pci_stop_ioeventfd(proxy);
307         }
308 
309         virtio_set_status(vdev, val & 0xFF);
310 
311         if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
312             virtio_pci_start_ioeventfd(proxy);
313         }
314 
315         if (vdev->status == 0) {
316             virtio_reset(vdev);
317             msix_unuse_all_vectors(&proxy->pci_dev);
318         }
319 
320         /* Linux before 2.6.34 sets the device as OK without enabling
321            the PCI device bus master bit. In this case we need to disable
322            some safety checks. */
323         if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
324             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
325             proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
326         }
327         break;
328     case VIRTIO_MSI_CONFIG_VECTOR:
329         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
330         /* Make it possible for guest to discover an error took place. */
331         if (msix_vector_use(&proxy->pci_dev, val) < 0)
332             val = VIRTIO_NO_VECTOR;
333         vdev->config_vector = val;
334         break;
335     case VIRTIO_MSI_QUEUE_VECTOR:
336         msix_vector_unuse(&proxy->pci_dev,
337                           virtio_queue_vector(vdev, vdev->queue_sel));
338         /* Make it possible for guest to discover an error took place. */
339         if (msix_vector_use(&proxy->pci_dev, val) < 0)
340             val = VIRTIO_NO_VECTOR;
341         virtio_queue_set_vector(vdev, vdev->queue_sel, val);
342         break;
343     default:
344         error_report("%s: unexpected address 0x%x value 0x%x",
345                      __func__, addr, val);
346         break;
347     }
348 }
349 
350 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
351 {
352     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
353     uint32_t ret = 0xFFFFFFFF;
354 
355     switch (addr) {
356     case VIRTIO_PCI_HOST_FEATURES:
357         ret = proxy->host_features;
358         break;
359     case VIRTIO_PCI_GUEST_FEATURES:
360         ret = vdev->guest_features;
361         break;
362     case VIRTIO_PCI_QUEUE_PFN:
363         ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
364               >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
365         break;
366     case VIRTIO_PCI_QUEUE_NUM:
367         ret = virtio_queue_get_num(vdev, vdev->queue_sel);
368         break;
369     case VIRTIO_PCI_QUEUE_SEL:
370         ret = vdev->queue_sel;
371         break;
372     case VIRTIO_PCI_STATUS:
373         ret = vdev->status;
374         break;
375     case VIRTIO_PCI_ISR:
376         /* reading from the ISR also clears it. */
377         ret = vdev->isr;
378         vdev->isr = 0;
379         pci_irq_deassert(&proxy->pci_dev);
380         break;
381     case VIRTIO_MSI_CONFIG_VECTOR:
382         ret = vdev->config_vector;
383         break;
384     case VIRTIO_MSI_QUEUE_VECTOR:
385         ret = virtio_queue_vector(vdev, vdev->queue_sel);
386         break;
387     default:
388         break;
389     }
390 
391     return ret;
392 }
393 
394 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
395                                        unsigned size)
396 {
397     VirtIOPCIProxy *proxy = opaque;
398     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
399     uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
400     uint64_t val = 0;
401     if (addr < config) {
402         return virtio_ioport_read(proxy, addr);
403     }
404     addr -= config;
405 
406     switch (size) {
407     case 1:
408         val = virtio_config_readb(vdev, addr);
409         break;
410     case 2:
411         val = virtio_config_readw(vdev, addr);
412         if (virtio_is_big_endian()) {
413             val = bswap16(val);
414         }
415         break;
416     case 4:
417         val = virtio_config_readl(vdev, addr);
418         if (virtio_is_big_endian()) {
419             val = bswap32(val);
420         }
421         break;
422     }
423     return val;
424 }
425 
426 static void virtio_pci_config_write(void *opaque, hwaddr addr,
427                                     uint64_t val, unsigned size)
428 {
429     VirtIOPCIProxy *proxy = opaque;
430     uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
431     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
432     if (addr < config) {
433         virtio_ioport_write(proxy, addr, val);
434         return;
435     }
436     addr -= config;
437     /*
438      * Virtio-PCI is odd. Ioports are LE but config space is target native
439      * endian.
440      */
441     switch (size) {
442     case 1:
443         virtio_config_writeb(vdev, addr, val);
444         break;
445     case 2:
446         if (virtio_is_big_endian()) {
447             val = bswap16(val);
448         }
449         virtio_config_writew(vdev, addr, val);
450         break;
451     case 4:
452         if (virtio_is_big_endian()) {
453             val = bswap32(val);
454         }
455         virtio_config_writel(vdev, addr, val);
456         break;
457     }
458 }
459 
460 static const MemoryRegionOps virtio_pci_config_ops = {
461     .read = virtio_pci_config_read,
462     .write = virtio_pci_config_write,
463     .impl = {
464         .min_access_size = 1,
465         .max_access_size = 4,
466     },
467     .endianness = DEVICE_LITTLE_ENDIAN,
468 };
469 
470 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
471                                 uint32_t val, int len)
472 {
473     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
474     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
475 
476     pci_default_write_config(pci_dev, address, val, len);
477 
478     if (range_covers_byte(address, len, PCI_COMMAND) &&
479         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
480         !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
481         virtio_pci_stop_ioeventfd(proxy);
482         virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
483     }
484 }
485 
486 static unsigned virtio_pci_get_features(DeviceState *d)
487 {
488     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
489     return proxy->host_features;
490 }
491 
492 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
493                                         unsigned int queue_no,
494                                         unsigned int vector,
495                                         MSIMessage msg)
496 {
497     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
498     int ret;
499 
500     if (irqfd->users == 0) {
501         ret = kvm_irqchip_add_msi_route(kvm_state, msg);
502         if (ret < 0) {
503             return ret;
504         }
505         irqfd->virq = ret;
506     }
507     irqfd->users++;
508     return 0;
509 }
510 
511 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
512                                              unsigned int vector)
513 {
514     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
515     if (--irqfd->users == 0) {
516         kvm_irqchip_release_virq(kvm_state, irqfd->virq);
517     }
518 }
519 
520 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
521                                  unsigned int queue_no,
522                                  unsigned int vector)
523 {
524     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
525     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
526     VirtQueue *vq = virtio_get_queue(vdev, queue_no);
527     EventNotifier *n = virtio_queue_get_guest_notifier(vq);
528     int ret;
529     ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq);
530     return ret;
531 }
532 
533 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
534                                       unsigned int queue_no,
535                                       unsigned int vector)
536 {
537     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
538     VirtQueue *vq = virtio_get_queue(vdev, queue_no);
539     EventNotifier *n = virtio_queue_get_guest_notifier(vq);
540     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
541     int ret;
542 
543     ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
544     assert(ret == 0);
545 }
546 
547 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
548 {
549     PCIDevice *dev = &proxy->pci_dev;
550     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
551     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
552     unsigned int vector;
553     int ret, queue_no;
554     MSIMessage msg;
555 
556     for (queue_no = 0; queue_no < nvqs; queue_no++) {
557         if (!virtio_queue_get_num(vdev, queue_no)) {
558             break;
559         }
560         vector = virtio_queue_vector(vdev, queue_no);
561         if (vector >= msix_nr_vectors_allocated(dev)) {
562             continue;
563         }
564         msg = msix_get_message(dev, vector);
565         ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
566         if (ret < 0) {
567             goto undo;
568         }
569         /* If guest supports masking, set up irqfd now.
570          * Otherwise, delay until unmasked in the frontend.
571          */
572         if (k->guest_notifier_mask) {
573             ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
574             if (ret < 0) {
575                 kvm_virtio_pci_vq_vector_release(proxy, vector);
576                 goto undo;
577             }
578         }
579     }
580     return 0;
581 
582 undo:
583     while (--queue_no >= 0) {
584         vector = virtio_queue_vector(vdev, queue_no);
585         if (vector >= msix_nr_vectors_allocated(dev)) {
586             continue;
587         }
588         if (k->guest_notifier_mask) {
589             kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
590         }
591         kvm_virtio_pci_vq_vector_release(proxy, vector);
592     }
593     return ret;
594 }
595 
596 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
597 {
598     PCIDevice *dev = &proxy->pci_dev;
599     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
600     unsigned int vector;
601     int queue_no;
602     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
603 
604     for (queue_no = 0; queue_no < nvqs; queue_no++) {
605         if (!virtio_queue_get_num(vdev, queue_no)) {
606             break;
607         }
608         vector = virtio_queue_vector(vdev, queue_no);
609         if (vector >= msix_nr_vectors_allocated(dev)) {
610             continue;
611         }
612         /* If guest supports masking, clean up irqfd now.
613          * Otherwise, it was cleaned when masked in the frontend.
614          */
615         if (k->guest_notifier_mask) {
616             kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
617         }
618         kvm_virtio_pci_vq_vector_release(proxy, vector);
619     }
620 }
621 
622 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
623                                        unsigned int queue_no,
624                                        unsigned int vector,
625                                        MSIMessage msg)
626 {
627     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
628     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
629     VirtQueue *vq = virtio_get_queue(vdev, queue_no);
630     EventNotifier *n = virtio_queue_get_guest_notifier(vq);
631     VirtIOIRQFD *irqfd;
632     int ret = 0;
633 
634     if (proxy->vector_irqfd) {
635         irqfd = &proxy->vector_irqfd[vector];
636         if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
637             ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg);
638             if (ret < 0) {
639                 return ret;
640             }
641         }
642     }
643 
644     /* If guest supports masking, irqfd is already setup, unmask it.
645      * Otherwise, set it up now.
646      */
647     if (k->guest_notifier_mask) {
648         k->guest_notifier_mask(vdev, queue_no, false);
649         /* Test after unmasking to avoid losing events. */
650         if (k->guest_notifier_pending &&
651             k->guest_notifier_pending(vdev, queue_no)) {
652             event_notifier_set(n);
653         }
654     } else {
655         ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
656     }
657     return ret;
658 }
659 
660 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
661                                              unsigned int queue_no,
662                                              unsigned int vector)
663 {
664     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
665     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
666 
667     /* If guest supports masking, keep irqfd but mask it.
668      * Otherwise, clean it up now.
669      */
670     if (k->guest_notifier_mask) {
671         k->guest_notifier_mask(vdev, queue_no, true);
672     } else {
673         kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
674     }
675 }
676 
677 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
678                                     MSIMessage msg)
679 {
680     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
681     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
682     int ret, queue_no;
683 
684     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
685         if (!virtio_queue_get_num(vdev, queue_no)) {
686             break;
687         }
688         if (virtio_queue_vector(vdev, queue_no) != vector) {
689             continue;
690         }
691         ret = virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg);
692         if (ret < 0) {
693             goto undo;
694         }
695     }
696     return 0;
697 
698 undo:
699     while (--queue_no >= 0) {
700         if (virtio_queue_vector(vdev, queue_no) != vector) {
701             continue;
702         }
703         virtio_pci_vq_vector_mask(proxy, queue_no, vector);
704     }
705     return ret;
706 }
707 
708 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
709 {
710     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
711     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
712     int queue_no;
713 
714     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
715         if (!virtio_queue_get_num(vdev, queue_no)) {
716             break;
717         }
718         if (virtio_queue_vector(vdev, queue_no) != vector) {
719             continue;
720         }
721         virtio_pci_vq_vector_mask(proxy, queue_no, vector);
722     }
723 }
724 
725 static void virtio_pci_vector_poll(PCIDevice *dev,
726                                    unsigned int vector_start,
727                                    unsigned int vector_end)
728 {
729     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
730     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
731     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
732     int queue_no;
733     unsigned int vector;
734     EventNotifier *notifier;
735     VirtQueue *vq;
736 
737     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
738         if (!virtio_queue_get_num(vdev, queue_no)) {
739             break;
740         }
741         vector = virtio_queue_vector(vdev, queue_no);
742         if (vector < vector_start || vector >= vector_end ||
743             !msix_is_masked(dev, vector)) {
744             continue;
745         }
746         vq = virtio_get_queue(vdev, queue_no);
747         notifier = virtio_queue_get_guest_notifier(vq);
748         if (k->guest_notifier_pending) {
749             if (k->guest_notifier_pending(vdev, queue_no)) {
750                 msix_set_pending(dev, vector);
751             }
752         } else if (event_notifier_test_and_clear(notifier)) {
753             msix_set_pending(dev, vector);
754         }
755     }
756 }
757 
758 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
759                                          bool with_irqfd)
760 {
761     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
762     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
763     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
764     VirtQueue *vq = virtio_get_queue(vdev, n);
765     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
766 
767     if (assign) {
768         int r = event_notifier_init(notifier, 0);
769         if (r < 0) {
770             return r;
771         }
772         virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
773     } else {
774         virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
775         event_notifier_cleanup(notifier);
776     }
777 
778     if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
779         vdc->guest_notifier_mask(vdev, n, !assign);
780     }
781 
782     return 0;
783 }
784 
785 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
786 {
787     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
788     return msix_enabled(&proxy->pci_dev);
789 }
790 
791 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
792 {
793     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
794     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
795     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
796     int r, n;
797     bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
798         kvm_msi_via_irqfd_enabled();
799 
800     nvqs = MIN(nvqs, VIRTIO_PCI_QUEUE_MAX);
801 
802     /* When deassigning, pass a consistent nvqs value
803      * to avoid leaking notifiers.
804      */
805     assert(assign || nvqs == proxy->nvqs_with_notifiers);
806 
807     proxy->nvqs_with_notifiers = nvqs;
808 
809     /* Must unset vector notifier while guest notifier is still assigned */
810     if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
811         msix_unset_vector_notifiers(&proxy->pci_dev);
812         if (proxy->vector_irqfd) {
813             kvm_virtio_pci_vector_release(proxy, nvqs);
814             g_free(proxy->vector_irqfd);
815             proxy->vector_irqfd = NULL;
816         }
817     }
818 
819     for (n = 0; n < nvqs; n++) {
820         if (!virtio_queue_get_num(vdev, n)) {
821             break;
822         }
823 
824         r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
825         if (r < 0) {
826             goto assign_error;
827         }
828     }
829 
830     /* Must set vector notifier after guest notifier has been assigned */
831     if ((with_irqfd || k->guest_notifier_mask) && assign) {
832         if (with_irqfd) {
833             proxy->vector_irqfd =
834                 g_malloc0(sizeof(*proxy->vector_irqfd) *
835                           msix_nr_vectors_allocated(&proxy->pci_dev));
836             r = kvm_virtio_pci_vector_use(proxy, nvqs);
837             if (r < 0) {
838                 goto assign_error;
839             }
840         }
841         r = msix_set_vector_notifiers(&proxy->pci_dev,
842                                       virtio_pci_vector_unmask,
843                                       virtio_pci_vector_mask,
844                                       virtio_pci_vector_poll);
845         if (r < 0) {
846             goto notifiers_error;
847         }
848     }
849 
850     return 0;
851 
852 notifiers_error:
853     if (with_irqfd) {
854         assert(assign);
855         kvm_virtio_pci_vector_release(proxy, nvqs);
856     }
857 
858 assign_error:
859     /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
860     assert(assign);
861     while (--n >= 0) {
862         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
863     }
864     return r;
865 }
866 
867 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
868 {
869     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
870 
871     /* Stop using ioeventfd for virtqueue kick if the device starts using host
872      * notifiers.  This makes it easy to avoid stepping on each others' toes.
873      */
874     proxy->ioeventfd_disabled = assign;
875     if (assign) {
876         virtio_pci_stop_ioeventfd(proxy);
877     }
878     /* We don't need to start here: it's not needed because backend
879      * currently only stops on status change away from ok,
880      * reset, vmstop and such. If we do add code to start here,
881      * need to check vmstate, device state etc. */
882     return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
883 }
884 
885 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
886 {
887     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
888     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
889 
890     if (running) {
891         /* Try to find out if the guest has bus master disabled, but is
892            in ready state. Then we have a buggy guest OS. */
893         if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
894             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
895             proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
896         }
897         virtio_pci_start_ioeventfd(proxy);
898     } else {
899         virtio_pci_stop_ioeventfd(proxy);
900     }
901 }
902 
903 #ifdef CONFIG_VIRTFS
904 static int virtio_9p_init_pci(VirtIOPCIProxy *vpci_dev)
905 {
906     V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
907     DeviceState *vdev = DEVICE(&dev->vdev);
908 
909     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
910     if (qdev_init(vdev) < 0) {
911         return -1;
912     }
913     return 0;
914 }
915 
916 static Property virtio_9p_pci_properties[] = {
917     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
918                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
919     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
920     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
921     DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState, vdev.fsconf),
922     DEFINE_PROP_END_OF_LIST(),
923 };
924 
925 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
926 {
927     DeviceClass *dc = DEVICE_CLASS(klass);
928     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
929     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
930 
931     k->init = virtio_9p_init_pci;
932     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
933     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
934     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
935     pcidev_k->class_id = 0x2;
936     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
937     dc->props = virtio_9p_pci_properties;
938 }
939 
940 static void virtio_9p_pci_instance_init(Object *obj)
941 {
942     V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
943     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_9P);
944     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
945 }
946 
947 static const TypeInfo virtio_9p_pci_info = {
948     .name          = TYPE_VIRTIO_9P_PCI,
949     .parent        = TYPE_VIRTIO_PCI,
950     .instance_size = sizeof(V9fsPCIState),
951     .instance_init = virtio_9p_pci_instance_init,
952     .class_init    = virtio_9p_pci_class_init,
953 };
954 #endif /* CONFIG_VIRTFS */
955 
956 /*
957  * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
958  */
959 
960 /* This is called by virtio-bus just after the device is plugged. */
961 static void virtio_pci_device_plugged(DeviceState *d)
962 {
963     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
964     VirtioBusState *bus = &proxy->bus;
965     uint8_t *config;
966     uint32_t size;
967 
968     config = proxy->pci_dev.config;
969     if (proxy->class_code) {
970         pci_config_set_class(config, proxy->class_code);
971     }
972     pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
973                  pci_get_word(config + PCI_VENDOR_ID));
974     pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
975     config[PCI_INTERRUPT_PIN] = 1;
976 
977     if (proxy->nvectors &&
978         msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
979         error_report("unable to init msix vectors to %" PRIu32,
980                      proxy->nvectors);
981         proxy->nvectors = 0;
982     }
983 
984     proxy->pci_dev.config_write = virtio_write_config;
985 
986     size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
987          + virtio_bus_get_vdev_config_len(bus);
988     if (size & (size - 1)) {
989         size = 1 << qemu_fls(size);
990     }
991 
992     memory_region_init_io(&proxy->bar, OBJECT(proxy), &virtio_pci_config_ops,
993                           proxy, "virtio-pci", size);
994     pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
995                      &proxy->bar);
996 
997     if (!kvm_has_many_ioeventfds()) {
998         proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
999     }
1000 
1001     proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
1002     proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
1003     proxy->host_features = virtio_bus_get_vdev_features(bus,
1004                                                       proxy->host_features);
1005 }
1006 
1007 static void virtio_pci_device_unplugged(DeviceState *d)
1008 {
1009     PCIDevice *pci_dev = PCI_DEVICE(d);
1010     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1011 
1012     virtio_pci_stop_ioeventfd(proxy);
1013     msix_uninit_exclusive_bar(pci_dev);
1014 }
1015 
1016 static int virtio_pci_init(PCIDevice *pci_dev)
1017 {
1018     VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
1019     VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1020     virtio_pci_bus_new(&dev->bus, sizeof(dev->bus), dev);
1021     if (k->init != NULL) {
1022         return k->init(dev);
1023     }
1024     return 0;
1025 }
1026 
1027 static void virtio_pci_exit(PCIDevice *pci_dev)
1028 {
1029     VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1030     memory_region_destroy(&proxy->bar);
1031 }
1032 
1033 static void virtio_pci_reset(DeviceState *qdev)
1034 {
1035     VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1036     VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1037     virtio_pci_stop_ioeventfd(proxy);
1038     virtio_bus_reset(bus);
1039     msix_unuse_all_vectors(&proxy->pci_dev);
1040     proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
1041 }
1042 
1043 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1044 {
1045     DeviceClass *dc = DEVICE_CLASS(klass);
1046     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1047 
1048     k->init = virtio_pci_init;
1049     k->exit = virtio_pci_exit;
1050     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1051     k->revision = VIRTIO_PCI_ABI_VERSION;
1052     k->class_id = PCI_CLASS_OTHERS;
1053     dc->reset = virtio_pci_reset;
1054 }
1055 
1056 static const TypeInfo virtio_pci_info = {
1057     .name          = TYPE_VIRTIO_PCI,
1058     .parent        = TYPE_PCI_DEVICE,
1059     .instance_size = sizeof(VirtIOPCIProxy),
1060     .class_init    = virtio_pci_class_init,
1061     .class_size    = sizeof(VirtioPCIClass),
1062     .abstract      = true,
1063 };
1064 
1065 /* virtio-blk-pci */
1066 
1067 static Property virtio_blk_pci_properties[] = {
1068     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1069     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1070                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1071     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1072 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1073     DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI, blk.data_plane, 0, false),
1074 #endif
1075     DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
1076     DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI, blk),
1077     DEFINE_PROP_END_OF_LIST(),
1078 };
1079 
1080 static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev)
1081 {
1082     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1083     DeviceState *vdev = DEVICE(&dev->vdev);
1084     virtio_blk_set_conf(vdev, &(dev->blk));
1085     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1086     if (qdev_init(vdev) < 0) {
1087         return -1;
1088     }
1089     return 0;
1090 }
1091 
1092 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1093 {
1094     DeviceClass *dc = DEVICE_CLASS(klass);
1095     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1096     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1097 
1098     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1099     dc->props = virtio_blk_pci_properties;
1100     k->init = virtio_blk_pci_init;
1101     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1102     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1103     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1104     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1105 }
1106 
1107 static void virtio_blk_pci_instance_init(Object *obj)
1108 {
1109     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1110     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BLK);
1111     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1112 }
1113 
1114 static const TypeInfo virtio_blk_pci_info = {
1115     .name          = TYPE_VIRTIO_BLK_PCI,
1116     .parent        = TYPE_VIRTIO_PCI,
1117     .instance_size = sizeof(VirtIOBlkPCI),
1118     .instance_init = virtio_blk_pci_instance_init,
1119     .class_init    = virtio_blk_pci_class_init,
1120 };
1121 
1122 /* virtio-scsi-pci */
1123 
1124 static Property virtio_scsi_pci_properties[] = {
1125     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1126                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1127     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1128                        DEV_NVECTORS_UNSPECIFIED),
1129     DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features),
1130     DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf),
1131     DEFINE_PROP_END_OF_LIST(),
1132 };
1133 
1134 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1135 {
1136     VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1137     DeviceState *vdev = DEVICE(&dev->vdev);
1138     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1139     DeviceState *proxy = DEVICE(vpci_dev);
1140     char *bus_name;
1141 
1142     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1143         vpci_dev->nvectors = vs->conf.num_queues + 3;
1144     }
1145 
1146     /*
1147      * For command line compatibility, this sets the virtio-scsi-device bus
1148      * name as before.
1149      */
1150     if (proxy->id) {
1151         bus_name = g_strdup_printf("%s.0", proxy->id);
1152         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1153         g_free(bus_name);
1154     }
1155 
1156     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1157     if (qdev_init(vdev) < 0) {
1158         return -1;
1159     }
1160     return 0;
1161 }
1162 
1163 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1164 {
1165     DeviceClass *dc = DEVICE_CLASS(klass);
1166     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1167     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1168     k->init = virtio_scsi_pci_init_pci;
1169     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1170     dc->props = virtio_scsi_pci_properties;
1171     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1172     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1173     pcidev_k->revision = 0x00;
1174     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1175 }
1176 
1177 static void virtio_scsi_pci_instance_init(Object *obj)
1178 {
1179     VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1180     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SCSI);
1181     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1182 }
1183 
1184 static const TypeInfo virtio_scsi_pci_info = {
1185     .name          = TYPE_VIRTIO_SCSI_PCI,
1186     .parent        = TYPE_VIRTIO_PCI,
1187     .instance_size = sizeof(VirtIOSCSIPCI),
1188     .instance_init = virtio_scsi_pci_instance_init,
1189     .class_init    = virtio_scsi_pci_class_init,
1190 };
1191 
1192 /* vhost-scsi-pci */
1193 
1194 #ifdef CONFIG_VHOST_SCSI
1195 static Property vhost_scsi_pci_properties[] = {
1196     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1197                        DEV_NVECTORS_UNSPECIFIED),
1198     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1199     DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI, vdev.parent_obj.conf),
1200     DEFINE_PROP_END_OF_LIST(),
1201 };
1202 
1203 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1204 {
1205     VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1206     DeviceState *vdev = DEVICE(&dev->vdev);
1207     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1208 
1209     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1210         vpci_dev->nvectors = vs->conf.num_queues + 3;
1211     }
1212 
1213     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1214     if (qdev_init(vdev) < 0) {
1215         return -1;
1216     }
1217     return 0;
1218 }
1219 
1220 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1221 {
1222     DeviceClass *dc = DEVICE_CLASS(klass);
1223     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1224     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1225     k->init = vhost_scsi_pci_init_pci;
1226     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1227     dc->props = vhost_scsi_pci_properties;
1228     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1229     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1230     pcidev_k->revision = 0x00;
1231     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1232 }
1233 
1234 static void vhost_scsi_pci_instance_init(Object *obj)
1235 {
1236     VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
1237     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VHOST_SCSI);
1238     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1239 }
1240 
1241 static const TypeInfo vhost_scsi_pci_info = {
1242     .name          = TYPE_VHOST_SCSI_PCI,
1243     .parent        = TYPE_VIRTIO_PCI,
1244     .instance_size = sizeof(VHostSCSIPCI),
1245     .instance_init = vhost_scsi_pci_instance_init,
1246     .class_init    = vhost_scsi_pci_class_init,
1247 };
1248 #endif
1249 
1250 /* virtio-balloon-pci */
1251 
1252 static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v,
1253                                       void *opaque, const char *name,
1254                                       Error **errp)
1255 {
1256     VirtIOBalloonPCI *dev = opaque;
1257     object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
1258 }
1259 
1260 static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v,
1261                                                 void *opaque, const char *name,
1262                                                 Error **errp)
1263 {
1264     VirtIOBalloonPCI *dev = opaque;
1265     object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1266                         errp);
1267 }
1268 
1269 static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v,
1270                                                 void *opaque, const char *name,
1271                                                 Error **errp)
1272 {
1273     VirtIOBalloonPCI *dev = opaque;
1274     object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1275                         errp);
1276 }
1277 
1278 static Property virtio_balloon_pci_properties[] = {
1279     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1280     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1281     DEFINE_PROP_END_OF_LIST(),
1282 };
1283 
1284 static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev)
1285 {
1286     VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1287     DeviceState *vdev = DEVICE(&dev->vdev);
1288 
1289     if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1290         vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1291         vpci_dev->class_code = PCI_CLASS_OTHERS;
1292     }
1293 
1294     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1295     if (qdev_init(vdev) < 0) {
1296         return -1;
1297     }
1298     return 0;
1299 }
1300 
1301 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1302 {
1303     DeviceClass *dc = DEVICE_CLASS(klass);
1304     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1305     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1306     k->init = virtio_balloon_pci_init;
1307     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1308     dc->props = virtio_balloon_pci_properties;
1309     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1310     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1311     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1312     pcidev_k->class_id = PCI_CLASS_OTHERS;
1313 }
1314 
1315 static void virtio_balloon_pci_instance_init(Object *obj)
1316 {
1317     VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
1318     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON);
1319     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1320 
1321     object_property_add(obj, "guest-stats", "guest statistics",
1322                         balloon_pci_stats_get_all, NULL, NULL, dev,
1323                         NULL);
1324 
1325     object_property_add(obj, "guest-stats-polling-interval", "int",
1326                         balloon_pci_stats_get_poll_interval,
1327                         balloon_pci_stats_set_poll_interval,
1328                         NULL, dev, NULL);
1329 }
1330 
1331 static const TypeInfo virtio_balloon_pci_info = {
1332     .name          = TYPE_VIRTIO_BALLOON_PCI,
1333     .parent        = TYPE_VIRTIO_PCI,
1334     .instance_size = sizeof(VirtIOBalloonPCI),
1335     .instance_init = virtio_balloon_pci_instance_init,
1336     .class_init    = virtio_balloon_pci_class_init,
1337 };
1338 
1339 /* virtio-serial-pci */
1340 
1341 static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev)
1342 {
1343     VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
1344     DeviceState *vdev = DEVICE(&dev->vdev);
1345     DeviceState *proxy = DEVICE(vpci_dev);
1346     char *bus_name;
1347 
1348     if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
1349         vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
1350         vpci_dev->class_code != PCI_CLASS_OTHERS) {        /* qemu-kvm  */
1351             vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
1352     }
1353 
1354     /* backwards-compatibility with machines that were created with
1355        DEV_NVECTORS_UNSPECIFIED */
1356     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1357         vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
1358     }
1359 
1360     /*
1361      * For command line compatibility, this sets the virtio-serial-device bus
1362      * name as before.
1363      */
1364     if (proxy->id) {
1365         bus_name = g_strdup_printf("%s.0", proxy->id);
1366         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1367         g_free(bus_name);
1368     }
1369 
1370     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1371     if (qdev_init(vdev) < 0) {
1372         return -1;
1373     }
1374     return 0;
1375 }
1376 
1377 static Property virtio_serial_pci_properties[] = {
1378     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1379                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1380     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1381     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1382     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1383     DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial),
1384     DEFINE_PROP_END_OF_LIST(),
1385 };
1386 
1387 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
1388 {
1389     DeviceClass *dc = DEVICE_CLASS(klass);
1390     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1391     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1392     k->init = virtio_serial_pci_init;
1393     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1394     dc->props = virtio_serial_pci_properties;
1395     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1396     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1397     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1398     pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1399 }
1400 
1401 static void virtio_serial_pci_instance_init(Object *obj)
1402 {
1403     VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
1404     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SERIAL);
1405     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1406 }
1407 
1408 static const TypeInfo virtio_serial_pci_info = {
1409     .name          = TYPE_VIRTIO_SERIAL_PCI,
1410     .parent        = TYPE_VIRTIO_PCI,
1411     .instance_size = sizeof(VirtIOSerialPCI),
1412     .instance_init = virtio_serial_pci_instance_init,
1413     .class_init    = virtio_serial_pci_class_init,
1414 };
1415 
1416 /* virtio-net-pci */
1417 
1418 static Property virtio_net_properties[] = {
1419     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1420                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1421     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
1422     DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
1423     DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf),
1424     DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf),
1425     DEFINE_PROP_END_OF_LIST(),
1426 };
1427 
1428 static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev)
1429 {
1430     DeviceState *qdev = DEVICE(vpci_dev);
1431     VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
1432     DeviceState *vdev = DEVICE(&dev->vdev);
1433 
1434     virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features);
1435     virtio_net_set_netclient_name(&dev->vdev, qdev->id,
1436                                   object_get_typename(OBJECT(qdev)));
1437     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1438     if (qdev_init(vdev) < 0) {
1439         return -1;
1440     }
1441     return 0;
1442 }
1443 
1444 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
1445 {
1446     DeviceClass *dc = DEVICE_CLASS(klass);
1447     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1448     VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1449 
1450     k->romfile = "efi-virtio.rom";
1451     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1452     k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1453     k->revision = VIRTIO_PCI_ABI_VERSION;
1454     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1455     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1456     dc->props = virtio_net_properties;
1457     vpciklass->init = virtio_net_pci_init;
1458 }
1459 
1460 static void virtio_net_pci_instance_init(Object *obj)
1461 {
1462     VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
1463     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_NET);
1464     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1465 }
1466 
1467 static const TypeInfo virtio_net_pci_info = {
1468     .name          = TYPE_VIRTIO_NET_PCI,
1469     .parent        = TYPE_VIRTIO_PCI,
1470     .instance_size = sizeof(VirtIONetPCI),
1471     .instance_init = virtio_net_pci_instance_init,
1472     .class_init    = virtio_net_pci_class_init,
1473 };
1474 
1475 /* virtio-rng-pci */
1476 
1477 static Property virtio_rng_pci_properties[] = {
1478     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1479     DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI, vdev.conf),
1480     DEFINE_PROP_END_OF_LIST(),
1481 };
1482 
1483 static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev)
1484 {
1485     VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
1486     DeviceState *vdev = DEVICE(&vrng->vdev);
1487 
1488     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1489     if (qdev_init(vdev) < 0) {
1490         return -1;
1491     }
1492 
1493     object_property_set_link(OBJECT(vrng),
1494                              OBJECT(vrng->vdev.conf.rng), "rng",
1495                              NULL);
1496 
1497     return 0;
1498 }
1499 
1500 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
1501 {
1502     DeviceClass *dc = DEVICE_CLASS(klass);
1503     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1504     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1505 
1506     k->init = virtio_rng_pci_init;
1507     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1508     dc->props = virtio_rng_pci_properties;
1509 
1510     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1511     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
1512     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1513     pcidev_k->class_id = PCI_CLASS_OTHERS;
1514 }
1515 
1516 static void virtio_rng_initfn(Object *obj)
1517 {
1518     VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
1519     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_RNG);
1520     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1521     object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
1522                              (Object **)&dev->vdev.conf.rng,
1523                              qdev_prop_allow_set_link_before_realize,
1524                              OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
1525 
1526 }
1527 
1528 static const TypeInfo virtio_rng_pci_info = {
1529     .name          = TYPE_VIRTIO_RNG_PCI,
1530     .parent        = TYPE_VIRTIO_PCI,
1531     .instance_size = sizeof(VirtIORngPCI),
1532     .instance_init = virtio_rng_initfn,
1533     .class_init    = virtio_rng_pci_class_init,
1534 };
1535 
1536 /* virtio-pci-bus */
1537 
1538 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
1539                                VirtIOPCIProxy *dev)
1540 {
1541     DeviceState *qdev = DEVICE(dev);
1542     BusState *qbus;
1543     char virtio_bus_name[] = "virtio-bus";
1544 
1545     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
1546                         virtio_bus_name);
1547     qbus = BUS(bus);
1548     qbus->allow_hotplug = 1;
1549 }
1550 
1551 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
1552 {
1553     BusClass *bus_class = BUS_CLASS(klass);
1554     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1555     bus_class->max_dev = 1;
1556     k->notify = virtio_pci_notify;
1557     k->save_config = virtio_pci_save_config;
1558     k->load_config = virtio_pci_load_config;
1559     k->save_queue = virtio_pci_save_queue;
1560     k->load_queue = virtio_pci_load_queue;
1561     k->get_features = virtio_pci_get_features;
1562     k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
1563     k->set_host_notifier = virtio_pci_set_host_notifier;
1564     k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
1565     k->vmstate_change = virtio_pci_vmstate_change;
1566     k->device_plugged = virtio_pci_device_plugged;
1567     k->device_unplugged = virtio_pci_device_unplugged;
1568 }
1569 
1570 static const TypeInfo virtio_pci_bus_info = {
1571     .name          = TYPE_VIRTIO_PCI_BUS,
1572     .parent        = TYPE_VIRTIO_BUS,
1573     .instance_size = sizeof(VirtioPCIBusState),
1574     .class_init    = virtio_pci_bus_class_init,
1575 };
1576 
1577 static void virtio_pci_register_types(void)
1578 {
1579     type_register_static(&virtio_rng_pci_info);
1580     type_register_static(&virtio_pci_bus_info);
1581     type_register_static(&virtio_pci_info);
1582 #ifdef CONFIG_VIRTFS
1583     type_register_static(&virtio_9p_pci_info);
1584 #endif
1585     type_register_static(&virtio_blk_pci_info);
1586     type_register_static(&virtio_scsi_pci_info);
1587     type_register_static(&virtio_balloon_pci_info);
1588     type_register_static(&virtio_serial_pci_info);
1589     type_register_static(&virtio_net_pci_info);
1590 #ifdef CONFIG_VHOST_SCSI
1591     type_register_static(&vhost_scsi_pci_info);
1592 #endif
1593 }
1594 
1595 type_init(virtio_pci_register_types)
1596