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