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