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