OSDN Git Service

virtio-pci: fix 1.0 virtqueue migration
[qmiga/qemu.git] / hw / virtio / virtio-pci.c
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/virtio/virtio-input.h"
28 #include "hw/pci/pci.h"
29 #include "qemu/error-report.h"
30 #include "hw/pci/msi.h"
31 #include "hw/pci/msix.h"
32 #include "hw/loader.h"
33 #include "sysemu/kvm.h"
34 #include "sysemu/block-backend.h"
35 #include "virtio-pci.h"
36 #include "qemu/range.h"
37 #include "hw/virtio/virtio-bus.h"
38 #include "qapi/visitor.h"
39
40 #define VIRTIO_PCI_REGION_SIZE(dev)     VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
41
42 #undef VIRTIO_PCI_CONFIG
43
44 /* The remaining space is defined by each driver as the per-driver
45  * configuration space */
46 #define VIRTIO_PCI_CONFIG_SIZE(dev)     VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
47
48 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
49                                VirtIOPCIProxy *dev);
50
51 /* virtio device */
52 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
53 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
54 {
55     return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
56 }
57
58 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
59  * be careful and test performance if you change this.
60  */
61 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
62 {
63     return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
64 }
65
66 static void virtio_pci_notify(DeviceState *d, uint16_t vector)
67 {
68     VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
69
70     if (msix_enabled(&proxy->pci_dev))
71         msix_notify(&proxy->pci_dev, vector);
72     else {
73         VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
74         pci_set_irq(&proxy->pci_dev, vdev->isr & 1);
75     }
76 }
77
78 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
79 {
80     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
81     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
82
83     pci_device_save(&proxy->pci_dev, f);
84     msix_save(&proxy->pci_dev, f);
85     if (msix_present(&proxy->pci_dev))
86         qemu_put_be16(f, vdev->config_vector);
87 }
88
89 static void virtio_pci_load_modern_queue_state(VirtIOPCIQueue *vq,
90                                                QEMUFile *f)
91 {
92     vq->num = qemu_get_be16(f);
93     vq->enabled = qemu_get_be16(f);
94     vq->desc[0] = qemu_get_be32(f);
95     vq->desc[1] = qemu_get_be32(f);
96     vq->avail[0] = qemu_get_be32(f);
97     vq->avail[1] = qemu_get_be32(f);
98     vq->used[0] = qemu_get_be32(f);
99     vq->used[1] = qemu_get_be32(f);
100 }
101
102 static bool virtio_pci_has_extra_state(DeviceState *d)
103 {
104     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
105
106     return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA;
107 }
108
109 static int get_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size)
110 {
111     VirtIOPCIProxy *proxy = pv;
112     int i;
113
114     proxy->dfselect = qemu_get_be32(f);
115     proxy->gfselect = qemu_get_be32(f);
116     proxy->guest_features[0] = qemu_get_be32(f);
117     proxy->guest_features[1] = qemu_get_be32(f);
118     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
119         virtio_pci_load_modern_queue_state(&proxy->vqs[i], f);
120     }
121
122     return 0;
123 }
124
125 static void virtio_pci_save_modern_queue_state(VirtIOPCIQueue *vq,
126                                                QEMUFile *f)
127 {
128     qemu_put_be16(f, vq->num);
129     qemu_put_be16(f, vq->enabled);
130     qemu_put_be32(f, vq->desc[0]);
131     qemu_put_be32(f, vq->desc[1]);
132     qemu_put_be32(f, vq->avail[0]);
133     qemu_put_be32(f, vq->avail[1]);
134     qemu_put_be32(f, vq->used[0]);
135     qemu_put_be32(f, vq->used[1]);
136 }
137
138 static void put_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size)
139 {
140     VirtIOPCIProxy *proxy = pv;
141     int i;
142
143     qemu_put_be32(f, proxy->dfselect);
144     qemu_put_be32(f, proxy->gfselect);
145     qemu_put_be32(f, proxy->guest_features[0]);
146     qemu_put_be32(f, proxy->guest_features[1]);
147     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
148         virtio_pci_save_modern_queue_state(&proxy->vqs[i], f);
149     }
150 }
151
152 static const VMStateInfo vmstate_info_virtio_pci_modern_state = {
153     .name = "virtqueue_state",
154     .get = get_virtio_pci_modern_state,
155     .put = put_virtio_pci_modern_state,
156 };
157
158 static bool virtio_pci_modern_state_needed(void *opaque)
159 {
160     VirtIOPCIProxy *proxy = opaque;
161
162     return !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
163 }
164
165 static const VMStateDescription vmstate_virtio_pci_modern_state = {
166     .name = "virtio_pci/modern_state",
167     .version_id = 1,
168     .minimum_version_id = 1,
169     .needed = &virtio_pci_modern_state_needed,
170     .fields = (VMStateField[]) {
171         {
172             .name         = "modern_state",
173             .version_id   = 0,
174             .field_exists = NULL,
175             .size         = 0,
176             .info         = &vmstate_info_virtio_pci_modern_state,
177             .flags        = VMS_SINGLE,
178             .offset       = 0,
179         },
180         VMSTATE_END_OF_LIST()
181     }
182 };
183
184 static const VMStateDescription vmstate_virtio_pci = {
185     .name = "virtio_pci",
186     .version_id = 1,
187     .minimum_version_id = 1,
188     .minimum_version_id_old = 1,
189     .fields = (VMStateField[]) {
190         VMSTATE_END_OF_LIST()
191     },
192     .subsections = (const VMStateDescription*[]) {
193         &vmstate_virtio_pci_modern_state,
194         NULL
195     }
196 };
197
198 static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f)
199 {
200     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
201
202     vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL);
203 }
204
205 static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f)
206 {
207     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
208
209     return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1);
210 }
211
212 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
213 {
214     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
215     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
216
217     if (msix_present(&proxy->pci_dev))
218         qemu_put_be16(f, virtio_queue_vector(vdev, n));
219 }
220
221 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
222 {
223     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
224     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
225
226     int ret;
227     ret = pci_device_load(&proxy->pci_dev, f);
228     if (ret) {
229         return ret;
230     }
231     msix_unuse_all_vectors(&proxy->pci_dev);
232     msix_load(&proxy->pci_dev, f);
233     if (msix_present(&proxy->pci_dev)) {
234         qemu_get_be16s(f, &vdev->config_vector);
235     } else {
236         vdev->config_vector = VIRTIO_NO_VECTOR;
237     }
238     if (vdev->config_vector != VIRTIO_NO_VECTOR) {
239         return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
240     }
241     return 0;
242 }
243
244 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
245 {
246     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
247     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
248
249     uint16_t vector;
250     if (msix_present(&proxy->pci_dev)) {
251         qemu_get_be16s(f, &vector);
252     } else {
253         vector = VIRTIO_NO_VECTOR;
254     }
255     virtio_queue_set_vector(vdev, n, vector);
256     if (vector != VIRTIO_NO_VECTOR) {
257         return msix_vector_use(&proxy->pci_dev, vector);
258     }
259
260     return 0;
261 }
262
263 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
264
265 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
266                                                  int n, bool assign, bool set_handler)
267 {
268     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
269     VirtQueue *vq = virtio_get_queue(vdev, n);
270     EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
271     bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
272     bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
273     MemoryRegion *modern_mr = &proxy->notify.mr;
274     MemoryRegion *legacy_mr = &proxy->bar;
275     hwaddr modern_addr = QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
276                          virtio_get_queue_index(vq);
277     hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
278     int r = 0;
279
280     if (assign) {
281         r = event_notifier_init(notifier, 1);
282         if (r < 0) {
283             error_report("%s: unable to init event notifier: %d",
284                          __func__, r);
285             return r;
286         }
287         virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
288         if (modern) {
289             memory_region_add_eventfd(modern_mr, modern_addr, 2,
290                                       true, n, notifier);
291         }
292         if (legacy) {
293             memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
294                                       true, n, notifier);
295         }
296     } else {
297         if (modern) {
298             memory_region_del_eventfd(modern_mr, modern_addr, 2,
299                                       true, n, notifier);
300         }
301         if (legacy) {
302             memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
303                                       true, n, notifier);
304         }
305         virtio_queue_set_host_notifier_fd_handler(vq, false, false);
306         event_notifier_cleanup(notifier);
307     }
308     return r;
309 }
310
311 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
312 {
313     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
314     int n, r;
315
316     if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
317         proxy->ioeventfd_disabled ||
318         proxy->ioeventfd_started) {
319         return;
320     }
321
322     for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
323         if (!virtio_queue_get_num(vdev, n)) {
324             continue;
325         }
326
327         r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
328         if (r < 0) {
329             goto assign_error;
330         }
331     }
332     proxy->ioeventfd_started = true;
333     return;
334
335 assign_error:
336     while (--n >= 0) {
337         if (!virtio_queue_get_num(vdev, n)) {
338             continue;
339         }
340
341         r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
342         assert(r >= 0);
343     }
344     proxy->ioeventfd_started = false;
345     error_report("%s: failed. Fallback to a userspace (slower).", __func__);
346 }
347
348 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
349 {
350     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
351     int r;
352     int n;
353
354     if (!proxy->ioeventfd_started) {
355         return;
356     }
357
358     for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
359         if (!virtio_queue_get_num(vdev, n)) {
360             continue;
361         }
362
363         r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
364         assert(r >= 0);
365     }
366     proxy->ioeventfd_started = false;
367 }
368
369 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
370 {
371     VirtIOPCIProxy *proxy = opaque;
372     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
373     hwaddr pa;
374
375     switch (addr) {
376     case VIRTIO_PCI_GUEST_FEATURES:
377         /* Guest does not negotiate properly?  We have to assume nothing. */
378         if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
379             val = virtio_bus_get_vdev_bad_features(&proxy->bus);
380         }
381         virtio_set_features(vdev, val);
382         break;
383     case VIRTIO_PCI_QUEUE_PFN:
384         pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
385         if (pa == 0) {
386             virtio_pci_stop_ioeventfd(proxy);
387             virtio_reset(vdev);
388             msix_unuse_all_vectors(&proxy->pci_dev);
389         }
390         else
391             virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
392         break;
393     case VIRTIO_PCI_QUEUE_SEL:
394         if (val < VIRTIO_QUEUE_MAX)
395             vdev->queue_sel = val;
396         break;
397     case VIRTIO_PCI_QUEUE_NOTIFY:
398         if (val < VIRTIO_QUEUE_MAX) {
399             virtio_queue_notify(vdev, val);
400         }
401         break;
402     case VIRTIO_PCI_STATUS:
403         if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
404             virtio_pci_stop_ioeventfd(proxy);
405         }
406
407         virtio_set_status(vdev, val & 0xFF);
408
409         if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
410             virtio_pci_start_ioeventfd(proxy);
411         }
412
413         if (vdev->status == 0) {
414             virtio_reset(vdev);
415             msix_unuse_all_vectors(&proxy->pci_dev);
416         }
417
418         /* Linux before 2.6.34 drives the device without enabling
419            the PCI device bus master bit. Enable it automatically
420            for the guest. This is a PCI spec violation but so is
421            initiating DMA with bus master bit clear. */
422         if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
423             pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
424                                      proxy->pci_dev.config[PCI_COMMAND] |
425                                      PCI_COMMAND_MASTER, 1);
426         }
427         break;
428     case VIRTIO_MSI_CONFIG_VECTOR:
429         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
430         /* Make it possible for guest to discover an error took place. */
431         if (msix_vector_use(&proxy->pci_dev, val) < 0)
432             val = VIRTIO_NO_VECTOR;
433         vdev->config_vector = val;
434         break;
435     case VIRTIO_MSI_QUEUE_VECTOR:
436         msix_vector_unuse(&proxy->pci_dev,
437                           virtio_queue_vector(vdev, vdev->queue_sel));
438         /* Make it possible for guest to discover an error took place. */
439         if (msix_vector_use(&proxy->pci_dev, val) < 0)
440             val = VIRTIO_NO_VECTOR;
441         virtio_queue_set_vector(vdev, vdev->queue_sel, val);
442         break;
443     default:
444         error_report("%s: unexpected address 0x%x value 0x%x",
445                      __func__, addr, val);
446         break;
447     }
448 }
449
450 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
451 {
452     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
453     uint32_t ret = 0xFFFFFFFF;
454
455     switch (addr) {
456     case VIRTIO_PCI_HOST_FEATURES:
457         ret = vdev->host_features;
458         break;
459     case VIRTIO_PCI_GUEST_FEATURES:
460         ret = vdev->guest_features;
461         break;
462     case VIRTIO_PCI_QUEUE_PFN:
463         ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
464               >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
465         break;
466     case VIRTIO_PCI_QUEUE_NUM:
467         ret = virtio_queue_get_num(vdev, vdev->queue_sel);
468         break;
469     case VIRTIO_PCI_QUEUE_SEL:
470         ret = vdev->queue_sel;
471         break;
472     case VIRTIO_PCI_STATUS:
473         ret = vdev->status;
474         break;
475     case VIRTIO_PCI_ISR:
476         /* reading from the ISR also clears it. */
477         ret = vdev->isr;
478         vdev->isr = 0;
479         pci_irq_deassert(&proxy->pci_dev);
480         break;
481     case VIRTIO_MSI_CONFIG_VECTOR:
482         ret = vdev->config_vector;
483         break;
484     case VIRTIO_MSI_QUEUE_VECTOR:
485         ret = virtio_queue_vector(vdev, vdev->queue_sel);
486         break;
487     default:
488         break;
489     }
490
491     return ret;
492 }
493
494 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
495                                        unsigned size)
496 {
497     VirtIOPCIProxy *proxy = opaque;
498     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
499     uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
500     uint64_t val = 0;
501     if (addr < config) {
502         return virtio_ioport_read(proxy, addr);
503     }
504     addr -= config;
505
506     switch (size) {
507     case 1:
508         val = virtio_config_readb(vdev, addr);
509         break;
510     case 2:
511         val = virtio_config_readw(vdev, addr);
512         if (virtio_is_big_endian(vdev)) {
513             val = bswap16(val);
514         }
515         break;
516     case 4:
517         val = virtio_config_readl(vdev, addr);
518         if (virtio_is_big_endian(vdev)) {
519             val = bswap32(val);
520         }
521         break;
522     }
523     return val;
524 }
525
526 static void virtio_pci_config_write(void *opaque, hwaddr addr,
527                                     uint64_t val, unsigned size)
528 {
529     VirtIOPCIProxy *proxy = opaque;
530     uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
531     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
532     if (addr < config) {
533         virtio_ioport_write(proxy, addr, val);
534         return;
535     }
536     addr -= config;
537     /*
538      * Virtio-PCI is odd. Ioports are LE but config space is target native
539      * endian.
540      */
541     switch (size) {
542     case 1:
543         virtio_config_writeb(vdev, addr, val);
544         break;
545     case 2:
546         if (virtio_is_big_endian(vdev)) {
547             val = bswap16(val);
548         }
549         virtio_config_writew(vdev, addr, val);
550         break;
551     case 4:
552         if (virtio_is_big_endian(vdev)) {
553             val = bswap32(val);
554         }
555         virtio_config_writel(vdev, addr, val);
556         break;
557     }
558 }
559
560 static const MemoryRegionOps virtio_pci_config_ops = {
561     .read = virtio_pci_config_read,
562     .write = virtio_pci_config_write,
563     .impl = {
564         .min_access_size = 1,
565         .max_access_size = 4,
566     },
567     .endianness = DEVICE_LITTLE_ENDIAN,
568 };
569
570 /* Below are generic functions to do memcpy from/to an address space,
571  * without byteswaps, with input validation.
572  *
573  * As regular address_space_* APIs all do some kind of byteswap at least for
574  * some host/target combinations, we are forced to explicitly convert to a
575  * known-endianness integer value.
576  * It doesn't really matter which endian format to go through, so the code
577  * below selects the endian that causes the least amount of work on the given
578  * host.
579  *
580  * Note: host pointer must be aligned.
581  */
582 static
583 void virtio_address_space_write(AddressSpace *as, hwaddr addr,
584                                 const uint8_t *buf, int len)
585 {
586     uint32_t val;
587
588     /* address_space_* APIs assume an aligned address.
589      * As address is under guest control, handle illegal values.
590      */
591     addr &= ~(len - 1);
592
593     /* Make sure caller aligned buf properly */
594     assert(!(((uintptr_t)buf) & (len - 1)));
595
596     switch (len) {
597     case 1:
598         val = pci_get_byte(buf);
599         address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
600         break;
601     case 2:
602         val = pci_get_word(buf);
603         address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
604         break;
605     case 4:
606         val = pci_get_long(buf);
607         address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
608         break;
609     default:
610         /* As length is under guest control, handle illegal values. */
611         break;
612     }
613 }
614
615 static void
616 virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
617 {
618     uint32_t val;
619
620     /* address_space_* APIs assume an aligned address.
621      * As address is under guest control, handle illegal values.
622      */
623     addr &= ~(len - 1);
624
625     /* Make sure caller aligned buf properly */
626     assert(!(((uintptr_t)buf) & (len - 1)));
627
628     switch (len) {
629     case 1:
630         val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
631         pci_set_byte(buf, val);
632         break;
633     case 2:
634         val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
635         pci_set_word(buf, val);
636         break;
637     case 4:
638         val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
639         pci_set_long(buf, val);
640         break;
641     default:
642         /* As length is under guest control, handle illegal values. */
643         break;
644     }
645 }
646
647 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
648                                 uint32_t val, int len)
649 {
650     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
651     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
652     struct virtio_pci_cfg_cap *cfg;
653
654     pci_default_write_config(pci_dev, address, val, len);
655
656     if (range_covers_byte(address, len, PCI_COMMAND) &&
657         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
658         virtio_pci_stop_ioeventfd(proxy);
659         virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
660     }
661
662     if (proxy->config_cap &&
663         ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
664                                                                   pci_cfg_data),
665                        sizeof cfg->pci_cfg_data)) {
666         uint32_t off;
667         uint32_t len;
668
669         cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
670         off = le32_to_cpu(cfg->cap.offset);
671         len = le32_to_cpu(cfg->cap.length);
672
673         if (len == 1 || len == 2 || len == 4) {
674             assert(len <= sizeof cfg->pci_cfg_data);
675             virtio_address_space_write(&proxy->modern_as, off,
676                                        cfg->pci_cfg_data, len);
677         }
678     }
679 }
680
681 static uint32_t virtio_read_config(PCIDevice *pci_dev,
682                                    uint32_t address, int len)
683 {
684     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
685     struct virtio_pci_cfg_cap *cfg;
686
687     if (proxy->config_cap &&
688         ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
689                                                                   pci_cfg_data),
690                        sizeof cfg->pci_cfg_data)) {
691         uint32_t off;
692         uint32_t len;
693
694         cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
695         off = le32_to_cpu(cfg->cap.offset);
696         len = le32_to_cpu(cfg->cap.length);
697
698         if (len == 1 || len == 2 || len == 4) {
699             assert(len <= sizeof cfg->pci_cfg_data);
700             virtio_address_space_read(&proxy->modern_as, off,
701                                       cfg->pci_cfg_data, len);
702         }
703     }
704
705     return pci_default_read_config(pci_dev, address, len);
706 }
707
708 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
709                                         unsigned int queue_no,
710                                         unsigned int vector,
711                                         MSIMessage msg)
712 {
713     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
714     int ret;
715
716     if (irqfd->users == 0) {
717         ret = kvm_irqchip_add_msi_route(kvm_state, msg, &proxy->pci_dev);
718         if (ret < 0) {
719             return ret;
720         }
721         irqfd->virq = ret;
722     }
723     irqfd->users++;
724     return 0;
725 }
726
727 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
728                                              unsigned int vector)
729 {
730     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
731     if (--irqfd->users == 0) {
732         kvm_irqchip_release_virq(kvm_state, irqfd->virq);
733     }
734 }
735
736 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
737                                  unsigned int queue_no,
738                                  unsigned int vector)
739 {
740     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
741     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
742     VirtQueue *vq = virtio_get_queue(vdev, queue_no);
743     EventNotifier *n = virtio_queue_get_guest_notifier(vq);
744     int ret;
745     ret = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
746     return ret;
747 }
748
749 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
750                                       unsigned int queue_no,
751                                       unsigned int vector)
752 {
753     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
754     VirtQueue *vq = virtio_get_queue(vdev, queue_no);
755     EventNotifier *n = virtio_queue_get_guest_notifier(vq);
756     VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
757     int ret;
758
759     ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
760     assert(ret == 0);
761 }
762
763 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
764 {
765     PCIDevice *dev = &proxy->pci_dev;
766     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
767     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
768     unsigned int vector;
769     int ret, queue_no;
770     MSIMessage msg;
771
772     for (queue_no = 0; queue_no < nvqs; queue_no++) {
773         if (!virtio_queue_get_num(vdev, queue_no)) {
774             break;
775         }
776         vector = virtio_queue_vector(vdev, queue_no);
777         if (vector >= msix_nr_vectors_allocated(dev)) {
778             continue;
779         }
780         msg = msix_get_message(dev, vector);
781         ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
782         if (ret < 0) {
783             goto undo;
784         }
785         /* If guest supports masking, set up irqfd now.
786          * Otherwise, delay until unmasked in the frontend.
787          */
788         if (k->guest_notifier_mask) {
789             ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
790             if (ret < 0) {
791                 kvm_virtio_pci_vq_vector_release(proxy, vector);
792                 goto undo;
793             }
794         }
795     }
796     return 0;
797
798 undo:
799     while (--queue_no >= 0) {
800         vector = virtio_queue_vector(vdev, queue_no);
801         if (vector >= msix_nr_vectors_allocated(dev)) {
802             continue;
803         }
804         if (k->guest_notifier_mask) {
805             kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
806         }
807         kvm_virtio_pci_vq_vector_release(proxy, vector);
808     }
809     return ret;
810 }
811
812 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
813 {
814     PCIDevice *dev = &proxy->pci_dev;
815     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
816     unsigned int vector;
817     int queue_no;
818     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
819
820     for (queue_no = 0; queue_no < nvqs; queue_no++) {
821         if (!virtio_queue_get_num(vdev, queue_no)) {
822             break;
823         }
824         vector = virtio_queue_vector(vdev, queue_no);
825         if (vector >= msix_nr_vectors_allocated(dev)) {
826             continue;
827         }
828         /* If guest supports masking, clean up irqfd now.
829          * Otherwise, it was cleaned when masked in the frontend.
830          */
831         if (k->guest_notifier_mask) {
832             kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
833         }
834         kvm_virtio_pci_vq_vector_release(proxy, vector);
835     }
836 }
837
838 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
839                                        unsigned int queue_no,
840                                        unsigned int vector,
841                                        MSIMessage msg)
842 {
843     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
844     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
845     VirtQueue *vq = virtio_get_queue(vdev, queue_no);
846     EventNotifier *n = virtio_queue_get_guest_notifier(vq);
847     VirtIOIRQFD *irqfd;
848     int ret = 0;
849
850     if (proxy->vector_irqfd) {
851         irqfd = &proxy->vector_irqfd[vector];
852         if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
853             ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
854                                                &proxy->pci_dev);
855             if (ret < 0) {
856                 return ret;
857             }
858         }
859     }
860
861     /* If guest supports masking, irqfd is already setup, unmask it.
862      * Otherwise, set it up now.
863      */
864     if (k->guest_notifier_mask) {
865         k->guest_notifier_mask(vdev, queue_no, false);
866         /* Test after unmasking to avoid losing events. */
867         if (k->guest_notifier_pending &&
868             k->guest_notifier_pending(vdev, queue_no)) {
869             event_notifier_set(n);
870         }
871     } else {
872         ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
873     }
874     return ret;
875 }
876
877 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
878                                              unsigned int queue_no,
879                                              unsigned int vector)
880 {
881     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
882     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
883
884     /* If guest supports masking, keep irqfd but mask it.
885      * Otherwise, clean it up now.
886      */ 
887     if (k->guest_notifier_mask) {
888         k->guest_notifier_mask(vdev, queue_no, true);
889     } else {
890         kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
891     }
892 }
893
894 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
895                                     MSIMessage msg)
896 {
897     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
898     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
899     VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
900     int ret, index, unmasked = 0;
901
902     while (vq) {
903         index = virtio_get_queue_index(vq);
904         if (!virtio_queue_get_num(vdev, index)) {
905             break;
906         }
907         if (index < proxy->nvqs_with_notifiers) {
908             ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
909             if (ret < 0) {
910                 goto undo;
911             }
912             ++unmasked;
913         }
914         vq = virtio_vector_next_queue(vq);
915     }
916
917     return 0;
918
919 undo:
920     vq = virtio_vector_first_queue(vdev, vector);
921     while (vq && unmasked >= 0) {
922         index = virtio_get_queue_index(vq);
923         if (index < proxy->nvqs_with_notifiers) {
924             virtio_pci_vq_vector_mask(proxy, index, vector);
925             --unmasked;
926         }
927         vq = virtio_vector_next_queue(vq);
928     }
929     return ret;
930 }
931
932 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
933 {
934     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
935     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
936     VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
937     int index;
938
939     while (vq) {
940         index = virtio_get_queue_index(vq);
941         if (!virtio_queue_get_num(vdev, index)) {
942             break;
943         }
944         if (index < proxy->nvqs_with_notifiers) {
945             virtio_pci_vq_vector_mask(proxy, index, vector);
946         }
947         vq = virtio_vector_next_queue(vq);
948     }
949 }
950
951 static void virtio_pci_vector_poll(PCIDevice *dev,
952                                    unsigned int vector_start,
953                                    unsigned int vector_end)
954 {
955     VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
956     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
957     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
958     int queue_no;
959     unsigned int vector;
960     EventNotifier *notifier;
961     VirtQueue *vq;
962
963     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
964         if (!virtio_queue_get_num(vdev, queue_no)) {
965             break;
966         }
967         vector = virtio_queue_vector(vdev, queue_no);
968         if (vector < vector_start || vector >= vector_end ||
969             !msix_is_masked(dev, vector)) {
970             continue;
971         }
972         vq = virtio_get_queue(vdev, queue_no);
973         notifier = virtio_queue_get_guest_notifier(vq);
974         if (k->guest_notifier_pending) {
975             if (k->guest_notifier_pending(vdev, queue_no)) {
976                 msix_set_pending(dev, vector);
977             }
978         } else if (event_notifier_test_and_clear(notifier)) {
979             msix_set_pending(dev, vector);
980         }
981     }
982 }
983
984 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
985                                          bool with_irqfd)
986 {
987     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
988     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
989     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
990     VirtQueue *vq = virtio_get_queue(vdev, n);
991     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
992
993     if (assign) {
994         int r = event_notifier_init(notifier, 0);
995         if (r < 0) {
996             return r;
997         }
998         virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
999     } else {
1000         virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1001         event_notifier_cleanup(notifier);
1002     }
1003
1004     if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
1005         vdc->guest_notifier_mask(vdev, n, !assign);
1006     }
1007
1008     return 0;
1009 }
1010
1011 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
1012 {
1013     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1014     return msix_enabled(&proxy->pci_dev);
1015 }
1016
1017 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
1018 {
1019     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1020     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1021     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1022     int r, n;
1023     bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
1024         kvm_msi_via_irqfd_enabled();
1025
1026     nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
1027
1028     /* When deassigning, pass a consistent nvqs value
1029      * to avoid leaking notifiers.
1030      */
1031     assert(assign || nvqs == proxy->nvqs_with_notifiers);
1032
1033     proxy->nvqs_with_notifiers = nvqs;
1034
1035     /* Must unset vector notifier while guest notifier is still assigned */
1036     if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
1037         msix_unset_vector_notifiers(&proxy->pci_dev);
1038         if (proxy->vector_irqfd) {
1039             kvm_virtio_pci_vector_release(proxy, nvqs);
1040             g_free(proxy->vector_irqfd);
1041             proxy->vector_irqfd = NULL;
1042         }
1043     }
1044
1045     for (n = 0; n < nvqs; n++) {
1046         if (!virtio_queue_get_num(vdev, n)) {
1047             break;
1048         }
1049
1050         r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
1051         if (r < 0) {
1052             goto assign_error;
1053         }
1054     }
1055
1056     /* Must set vector notifier after guest notifier has been assigned */
1057     if ((with_irqfd || k->guest_notifier_mask) && assign) {
1058         if (with_irqfd) {
1059             proxy->vector_irqfd =
1060                 g_malloc0(sizeof(*proxy->vector_irqfd) *
1061                           msix_nr_vectors_allocated(&proxy->pci_dev));
1062             r = kvm_virtio_pci_vector_use(proxy, nvqs);
1063             if (r < 0) {
1064                 goto assign_error;
1065             }
1066         }
1067         r = msix_set_vector_notifiers(&proxy->pci_dev,
1068                                       virtio_pci_vector_unmask,
1069                                       virtio_pci_vector_mask,
1070                                       virtio_pci_vector_poll);
1071         if (r < 0) {
1072             goto notifiers_error;
1073         }
1074     }
1075
1076     return 0;
1077
1078 notifiers_error:
1079     if (with_irqfd) {
1080         assert(assign);
1081         kvm_virtio_pci_vector_release(proxy, nvqs);
1082     }
1083
1084 assign_error:
1085     /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1086     assert(assign);
1087     while (--n >= 0) {
1088         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
1089     }
1090     return r;
1091 }
1092
1093 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
1094 {
1095     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1096
1097     /* Stop using ioeventfd for virtqueue kick if the device starts using host
1098      * notifiers.  This makes it easy to avoid stepping on each others' toes.
1099      */
1100     proxy->ioeventfd_disabled = assign;
1101     if (assign) {
1102         virtio_pci_stop_ioeventfd(proxy);
1103     }
1104     /* We don't need to start here: it's not needed because backend
1105      * currently only stops on status change away from ok,
1106      * reset, vmstop and such. If we do add code to start here,
1107      * need to check vmstate, device state etc. */
1108     return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
1109 }
1110
1111 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1112 {
1113     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1114     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1115
1116     if (running) {
1117         /* Old QEMU versions did not set bus master enable on status write.
1118          * Detect DRIVER set and enable it.
1119          */
1120         if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
1121             (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
1122             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1123             pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
1124                                      proxy->pci_dev.config[PCI_COMMAND] |
1125                                      PCI_COMMAND_MASTER, 1);
1126         }
1127         virtio_pci_start_ioeventfd(proxy);
1128     } else {
1129         virtio_pci_stop_ioeventfd(proxy);
1130     }
1131 }
1132
1133 #ifdef CONFIG_VIRTFS
1134 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1135 {
1136     V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
1137     DeviceState *vdev = DEVICE(&dev->vdev);
1138
1139     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1140     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1141 }
1142
1143 static Property virtio_9p_pci_properties[] = {
1144     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1145                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1146     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1147     DEFINE_PROP_END_OF_LIST(),
1148 };
1149
1150 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1151 {
1152     DeviceClass *dc = DEVICE_CLASS(klass);
1153     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1154     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1155
1156     k->realize = virtio_9p_pci_realize;
1157     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1158     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
1159     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1160     pcidev_k->class_id = 0x2;
1161     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1162     dc->props = virtio_9p_pci_properties;
1163 }
1164
1165 static void virtio_9p_pci_instance_init(Object *obj)
1166 {
1167     V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1168
1169     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1170                                 TYPE_VIRTIO_9P);
1171 }
1172
1173 static const TypeInfo virtio_9p_pci_info = {
1174     .name          = TYPE_VIRTIO_9P_PCI,
1175     .parent        = TYPE_VIRTIO_PCI,
1176     .instance_size = sizeof(V9fsPCIState),
1177     .instance_init = virtio_9p_pci_instance_init,
1178     .class_init    = virtio_9p_pci_class_init,
1179 };
1180 #endif /* CONFIG_VIRTFS */
1181
1182 /*
1183  * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1184  */
1185
1186 static int virtio_pci_query_nvectors(DeviceState *d)
1187 {
1188     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1189
1190     return proxy->nvectors;
1191 }
1192
1193 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1194                                    struct virtio_pci_cap *cap)
1195 {
1196     PCIDevice *dev = &proxy->pci_dev;
1197     int offset;
1198
1199     offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
1200     assert(offset > 0);
1201
1202     assert(cap->cap_len >= sizeof *cap);
1203     memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1204            cap->cap_len - PCI_CAP_FLAGS);
1205
1206     return offset;
1207 }
1208
1209 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1210                                        unsigned size)
1211 {
1212     VirtIOPCIProxy *proxy = opaque;
1213     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1214     uint32_t val = 0;
1215     int i;
1216
1217     switch (addr) {
1218     case VIRTIO_PCI_COMMON_DFSELECT:
1219         val = proxy->dfselect;
1220         break;
1221     case VIRTIO_PCI_COMMON_DF:
1222         if (proxy->dfselect <= 1) {
1223             val = (vdev->host_features & ~VIRTIO_LEGACY_FEATURES) >>
1224                 (32 * proxy->dfselect);
1225         }
1226         break;
1227     case VIRTIO_PCI_COMMON_GFSELECT:
1228         val = proxy->gfselect;
1229         break;
1230     case VIRTIO_PCI_COMMON_GF:
1231         if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1232             val = proxy->guest_features[proxy->gfselect];
1233         }
1234         break;
1235     case VIRTIO_PCI_COMMON_MSIX:
1236         val = vdev->config_vector;
1237         break;
1238     case VIRTIO_PCI_COMMON_NUMQ:
1239         for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1240             if (virtio_queue_get_num(vdev, i)) {
1241                 val = i + 1;
1242             }
1243         }
1244         break;
1245     case VIRTIO_PCI_COMMON_STATUS:
1246         val = vdev->status;
1247         break;
1248     case VIRTIO_PCI_COMMON_CFGGENERATION:
1249         val = vdev->generation;
1250         break;
1251     case VIRTIO_PCI_COMMON_Q_SELECT:
1252         val = vdev->queue_sel;
1253         break;
1254     case VIRTIO_PCI_COMMON_Q_SIZE:
1255         val = virtio_queue_get_num(vdev, vdev->queue_sel);
1256         break;
1257     case VIRTIO_PCI_COMMON_Q_MSIX:
1258         val = virtio_queue_vector(vdev, vdev->queue_sel);
1259         break;
1260     case VIRTIO_PCI_COMMON_Q_ENABLE:
1261         val = proxy->vqs[vdev->queue_sel].enabled;
1262         break;
1263     case VIRTIO_PCI_COMMON_Q_NOFF:
1264         /* Simply map queues in order */
1265         val = vdev->queue_sel;
1266         break;
1267     case VIRTIO_PCI_COMMON_Q_DESCLO:
1268         val = proxy->vqs[vdev->queue_sel].desc[0];
1269         break;
1270     case VIRTIO_PCI_COMMON_Q_DESCHI:
1271         val = proxy->vqs[vdev->queue_sel].desc[1];
1272         break;
1273     case VIRTIO_PCI_COMMON_Q_AVAILLO:
1274         val = proxy->vqs[vdev->queue_sel].avail[0];
1275         break;
1276     case VIRTIO_PCI_COMMON_Q_AVAILHI:
1277         val = proxy->vqs[vdev->queue_sel].avail[1];
1278         break;
1279     case VIRTIO_PCI_COMMON_Q_USEDLO:
1280         val = proxy->vqs[vdev->queue_sel].used[0];
1281         break;
1282     case VIRTIO_PCI_COMMON_Q_USEDHI:
1283         val = proxy->vqs[vdev->queue_sel].used[1];
1284         break;
1285     default:
1286         val = 0;
1287     }
1288
1289     return val;
1290 }
1291
1292 static void virtio_pci_common_write(void *opaque, hwaddr addr,
1293                                     uint64_t val, unsigned size)
1294 {
1295     VirtIOPCIProxy *proxy = opaque;
1296     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1297
1298     switch (addr) {
1299     case VIRTIO_PCI_COMMON_DFSELECT:
1300         proxy->dfselect = val;
1301         break;
1302     case VIRTIO_PCI_COMMON_GFSELECT:
1303         proxy->gfselect = val;
1304         break;
1305     case VIRTIO_PCI_COMMON_GF:
1306         if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1307             proxy->guest_features[proxy->gfselect] = val;
1308             virtio_set_features(vdev,
1309                                 (((uint64_t)proxy->guest_features[1]) << 32) |
1310                                 proxy->guest_features[0]);
1311         }
1312         break;
1313     case VIRTIO_PCI_COMMON_MSIX:
1314         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1315         /* Make it possible for guest to discover an error took place. */
1316         if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1317             val = VIRTIO_NO_VECTOR;
1318         }
1319         vdev->config_vector = val;
1320         break;
1321     case VIRTIO_PCI_COMMON_STATUS:
1322         if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1323             virtio_pci_stop_ioeventfd(proxy);
1324         }
1325
1326         virtio_set_status(vdev, val & 0xFF);
1327
1328         if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1329             virtio_pci_start_ioeventfd(proxy);
1330         }
1331
1332         if (vdev->status == 0) {
1333             virtio_reset(vdev);
1334             msix_unuse_all_vectors(&proxy->pci_dev);
1335         }
1336
1337         break;
1338     case VIRTIO_PCI_COMMON_Q_SELECT:
1339         if (val < VIRTIO_QUEUE_MAX) {
1340             vdev->queue_sel = val;
1341         }
1342         break;
1343     case VIRTIO_PCI_COMMON_Q_SIZE:
1344         proxy->vqs[vdev->queue_sel].num = val;
1345         break;
1346     case VIRTIO_PCI_COMMON_Q_MSIX:
1347         msix_vector_unuse(&proxy->pci_dev,
1348                           virtio_queue_vector(vdev, vdev->queue_sel));
1349         /* Make it possible for guest to discover an error took place. */
1350         if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1351             val = VIRTIO_NO_VECTOR;
1352         }
1353         virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1354         break;
1355     case VIRTIO_PCI_COMMON_Q_ENABLE:
1356         /* TODO: need a way to put num back on reset. */
1357         virtio_queue_set_num(vdev, vdev->queue_sel,
1358                              proxy->vqs[vdev->queue_sel].num);
1359         virtio_queue_set_rings(vdev, vdev->queue_sel,
1360                        ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1361                        proxy->vqs[vdev->queue_sel].desc[0],
1362                        ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1363                        proxy->vqs[vdev->queue_sel].avail[0],
1364                        ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1365                        proxy->vqs[vdev->queue_sel].used[0]);
1366         break;
1367     case VIRTIO_PCI_COMMON_Q_DESCLO:
1368         proxy->vqs[vdev->queue_sel].desc[0] = val;
1369         break;
1370     case VIRTIO_PCI_COMMON_Q_DESCHI:
1371         proxy->vqs[vdev->queue_sel].desc[1] = val;
1372         break;
1373     case VIRTIO_PCI_COMMON_Q_AVAILLO:
1374         proxy->vqs[vdev->queue_sel].avail[0] = val;
1375         break;
1376     case VIRTIO_PCI_COMMON_Q_AVAILHI:
1377         proxy->vqs[vdev->queue_sel].avail[1] = val;
1378         break;
1379     case VIRTIO_PCI_COMMON_Q_USEDLO:
1380         proxy->vqs[vdev->queue_sel].used[0] = val;
1381         break;
1382     case VIRTIO_PCI_COMMON_Q_USEDHI:
1383         proxy->vqs[vdev->queue_sel].used[1] = val;
1384         break;
1385     default:
1386         break;
1387     }
1388 }
1389
1390
1391 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1392                                        unsigned size)
1393 {
1394     return 0;
1395 }
1396
1397 static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1398                                     uint64_t val, unsigned size)
1399 {
1400     VirtIODevice *vdev = opaque;
1401     unsigned queue = addr / QEMU_VIRTIO_PCI_QUEUE_MEM_MULT;
1402
1403     if (queue < VIRTIO_QUEUE_MAX) {
1404         virtio_queue_notify(vdev, queue);
1405     }
1406 }
1407
1408 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1409                                     unsigned size)
1410 {
1411     VirtIOPCIProxy *proxy = opaque;
1412     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1413     uint64_t val = vdev->isr;
1414
1415     vdev->isr = 0;
1416     pci_irq_deassert(&proxy->pci_dev);
1417
1418     return val;
1419 }
1420
1421 static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1422                                  uint64_t val, unsigned size)
1423 {
1424 }
1425
1426 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1427                                        unsigned size)
1428 {
1429     VirtIODevice *vdev = opaque;
1430     uint64_t val = 0;
1431
1432     switch (size) {
1433     case 1:
1434         val = virtio_config_modern_readb(vdev, addr);
1435         break;
1436     case 2:
1437         val = virtio_config_modern_readw(vdev, addr);
1438         break;
1439     case 4:
1440         val = virtio_config_modern_readl(vdev, addr);
1441         break;
1442     }
1443     return val;
1444 }
1445
1446 static void virtio_pci_device_write(void *opaque, hwaddr addr,
1447                                     uint64_t val, unsigned size)
1448 {
1449     VirtIODevice *vdev = opaque;
1450     switch (size) {
1451     case 1:
1452         virtio_config_modern_writeb(vdev, addr, val);
1453         break;
1454     case 2:
1455         virtio_config_modern_writew(vdev, addr, val);
1456         break;
1457     case 4:
1458         virtio_config_modern_writel(vdev, addr, val);
1459         break;
1460     }
1461 }
1462
1463 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1464 {
1465     static const MemoryRegionOps common_ops = {
1466         .read = virtio_pci_common_read,
1467         .write = virtio_pci_common_write,
1468         .impl = {
1469             .min_access_size = 1,
1470             .max_access_size = 4,
1471         },
1472         .endianness = DEVICE_LITTLE_ENDIAN,
1473     };
1474     static const MemoryRegionOps isr_ops = {
1475         .read = virtio_pci_isr_read,
1476         .write = virtio_pci_isr_write,
1477         .impl = {
1478             .min_access_size = 1,
1479             .max_access_size = 4,
1480         },
1481         .endianness = DEVICE_LITTLE_ENDIAN,
1482     };
1483     static const MemoryRegionOps device_ops = {
1484         .read = virtio_pci_device_read,
1485         .write = virtio_pci_device_write,
1486         .impl = {
1487             .min_access_size = 1,
1488             .max_access_size = 4,
1489         },
1490         .endianness = DEVICE_LITTLE_ENDIAN,
1491     };
1492     static const MemoryRegionOps notify_ops = {
1493         .read = virtio_pci_notify_read,
1494         .write = virtio_pci_notify_write,
1495         .impl = {
1496             .min_access_size = 1,
1497             .max_access_size = 4,
1498         },
1499         .endianness = DEVICE_LITTLE_ENDIAN,
1500     };
1501
1502     memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1503                           &common_ops,
1504                           proxy,
1505                           "virtio-pci-common",
1506                           proxy->common.size);
1507
1508     memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1509                           &isr_ops,
1510                           proxy,
1511                           "virtio-pci-isr",
1512                           proxy->isr.size);
1513
1514     memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1515                           &device_ops,
1516                           virtio_bus_get_device(&proxy->bus),
1517                           "virtio-pci-device",
1518                           proxy->device.size);
1519
1520     memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1521                           &notify_ops,
1522                           virtio_bus_get_device(&proxy->bus),
1523                           "virtio-pci-notify",
1524                           proxy->notify.size);
1525 }
1526
1527 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1528                                          VirtIOPCIRegion *region,
1529                                          struct virtio_pci_cap *cap)
1530 {
1531     memory_region_add_subregion(&proxy->modern_bar,
1532                                 region->offset,
1533                                 &region->mr);
1534
1535     cap->cfg_type = region->type;
1536     cap->bar = proxy->modern_mem_bar;
1537     cap->offset = cpu_to_le32(region->offset);
1538     cap->length = cpu_to_le32(region->size);
1539     virtio_pci_add_mem_cap(proxy, cap);
1540 }
1541
1542 static void virtio_pci_modern_region_unmap(VirtIOPCIProxy *proxy,
1543                                            VirtIOPCIRegion *region)
1544 {
1545     memory_region_del_subregion(&proxy->modern_bar,
1546                                 &region->mr);
1547 }
1548
1549 /* This is called by virtio-bus just after the device is plugged. */
1550 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1551 {
1552     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1553     VirtioBusState *bus = &proxy->bus;
1554     bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
1555     bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1556     uint8_t *config;
1557     uint32_t size;
1558     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1559
1560     config = proxy->pci_dev.config;
1561     if (proxy->class_code) {
1562         pci_config_set_class(config, proxy->class_code);
1563     }
1564
1565     if (legacy) {
1566         /* legacy and transitional */
1567         pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1568                      pci_get_word(config + PCI_VENDOR_ID));
1569         pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1570     } else {
1571         /* pure virtio-1.0 */
1572         pci_set_word(config + PCI_VENDOR_ID,
1573                      PCI_VENDOR_ID_REDHAT_QUMRANET);
1574         pci_set_word(config + PCI_DEVICE_ID,
1575                      0x1040 + virtio_bus_get_vdev_id(bus));
1576         pci_config_set_revision(config, 1);
1577     }
1578     config[PCI_INTERRUPT_PIN] = 1;
1579
1580
1581     if (modern) {
1582         struct virtio_pci_cap cap = {
1583             .cap_len = sizeof cap,
1584         };
1585         struct virtio_pci_notify_cap notify = {
1586             .cap.cap_len = sizeof notify,
1587             .notify_off_multiplier =
1588                 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT),
1589         };
1590         struct virtio_pci_cfg_cap cfg = {
1591             .cap.cap_len = sizeof cfg,
1592             .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1593         };
1594         struct virtio_pci_cfg_cap *cfg_mask;
1595
1596         /* TODO: add io access for speed */
1597
1598         virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1599         virtio_pci_modern_regions_init(proxy);
1600         virtio_pci_modern_region_map(proxy, &proxy->common, &cap);
1601         virtio_pci_modern_region_map(proxy, &proxy->isr, &cap);
1602         virtio_pci_modern_region_map(proxy, &proxy->device, &cap);
1603         virtio_pci_modern_region_map(proxy, &proxy->notify, &notify.cap);
1604
1605         pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar,
1606                          PCI_BASE_ADDRESS_SPACE_MEMORY |
1607                          PCI_BASE_ADDRESS_MEM_PREFETCH |
1608                          PCI_BASE_ADDRESS_MEM_TYPE_64,
1609                          &proxy->modern_bar);
1610
1611         proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1612         cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1613         pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1614         pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1615         pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1616         pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1617     }
1618
1619     if (proxy->nvectors) {
1620         int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1621                                           proxy->msix_bar);
1622         if (err) {
1623             /* Notice when a system that supports MSIx can't initialize it.  */
1624             if (err != -ENOTSUP) {
1625                 error_report("unable to init msix vectors to %" PRIu32,
1626                              proxy->nvectors);
1627             }
1628             proxy->nvectors = 0;
1629         }
1630     }
1631
1632     proxy->pci_dev.config_write = virtio_write_config;
1633     proxy->pci_dev.config_read = virtio_read_config;
1634
1635     if (legacy) {
1636         size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1637             + virtio_bus_get_vdev_config_len(bus);
1638         size = pow2ceil(size);
1639
1640         memory_region_init_io(&proxy->bar, OBJECT(proxy),
1641                               &virtio_pci_config_ops,
1642                               proxy, "virtio-pci", size);
1643
1644         pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar,
1645                          PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1646     }
1647
1648     if (!kvm_has_many_ioeventfds()) {
1649         proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1650     }
1651
1652     virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1653 }
1654
1655 static void virtio_pci_device_unplugged(DeviceState *d)
1656 {
1657     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1658     bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1659
1660     virtio_pci_stop_ioeventfd(proxy);
1661
1662     if (modern) {
1663         virtio_pci_modern_region_unmap(proxy, &proxy->common);
1664         virtio_pci_modern_region_unmap(proxy, &proxy->isr);
1665         virtio_pci_modern_region_unmap(proxy, &proxy->device);
1666         virtio_pci_modern_region_unmap(proxy, &proxy->notify);
1667     }
1668 }
1669
1670 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1671 {
1672     VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1673     VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1674
1675     /*
1676      * virtio pci bar layout used by default.
1677      * subclasses can re-arrange things if needed.
1678      *
1679      *   region 0   --  virtio legacy io bar
1680      *   region 1   --  msi-x bar
1681      *   region 4+5 --  virtio modern memory (64bit) bar
1682      *
1683      */
1684     proxy->legacy_io_bar  = 0;
1685     proxy->msix_bar       = 1;
1686     proxy->modern_mem_bar = 4;
1687
1688     proxy->common.offset = 0x0;
1689     proxy->common.size = 0x1000;
1690     proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1691
1692     proxy->isr.offset = 0x1000;
1693     proxy->isr.size = 0x1000;
1694     proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1695
1696     proxy->device.offset = 0x2000;
1697     proxy->device.size = 0x1000;
1698     proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1699
1700     proxy->notify.offset = 0x3000;
1701     proxy->notify.size =
1702         QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * VIRTIO_QUEUE_MAX;
1703     proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1704
1705     /* subclasses can enforce modern, so do this unconditionally */
1706     memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1707                        2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
1708                        VIRTIO_QUEUE_MAX);
1709
1710     memory_region_init_alias(&proxy->modern_cfg,
1711                              OBJECT(proxy),
1712                              "virtio-pci-cfg",
1713                              &proxy->modern_bar,
1714                              0,
1715                              memory_region_size(&proxy->modern_bar));
1716
1717     address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");
1718
1719     virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1720     if (k->realize) {
1721         k->realize(proxy, errp);
1722     }
1723 }
1724
1725 static void virtio_pci_exit(PCIDevice *pci_dev)
1726 {
1727     VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1728
1729     msix_uninit_exclusive_bar(pci_dev);
1730     address_space_destroy(&proxy->modern_as);
1731 }
1732
1733 static void virtio_pci_reset(DeviceState *qdev)
1734 {
1735     VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1736     VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1737     virtio_pci_stop_ioeventfd(proxy);
1738     virtio_bus_reset(bus);
1739     msix_unuse_all_vectors(&proxy->pci_dev);
1740 }
1741
1742 static Property virtio_pci_properties[] = {
1743     DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1744                     VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1745     DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy, flags,
1746                     VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT, false),
1747     DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy, flags,
1748                     VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT, true),
1749     DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
1750                     VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1751     DEFINE_PROP_END_OF_LIST(),
1752 };
1753
1754 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1755 {
1756     DeviceClass *dc = DEVICE_CLASS(klass);
1757     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1758
1759     dc->props = virtio_pci_properties;
1760     k->realize = virtio_pci_realize;
1761     k->exit = virtio_pci_exit;
1762     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1763     k->revision = VIRTIO_PCI_ABI_VERSION;
1764     k->class_id = PCI_CLASS_OTHERS;
1765     dc->reset = virtio_pci_reset;
1766 }
1767
1768 static const TypeInfo virtio_pci_info = {
1769     .name          = TYPE_VIRTIO_PCI,
1770     .parent        = TYPE_PCI_DEVICE,
1771     .instance_size = sizeof(VirtIOPCIProxy),
1772     .class_init    = virtio_pci_class_init,
1773     .class_size    = sizeof(VirtioPCIClass),
1774     .abstract      = true,
1775 };
1776
1777 /* virtio-blk-pci */
1778
1779 static Property virtio_blk_pci_properties[] = {
1780     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1781     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1782                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1783     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1784     DEFINE_PROP_END_OF_LIST(),
1785 };
1786
1787 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1788 {
1789     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1790     DeviceState *vdev = DEVICE(&dev->vdev);
1791
1792     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1793     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1794 }
1795
1796 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1797 {
1798     DeviceClass *dc = DEVICE_CLASS(klass);
1799     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1800     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1801
1802     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1803     dc->props = virtio_blk_pci_properties;
1804     k->realize = virtio_blk_pci_realize;
1805     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1806     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1807     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1808     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1809 }
1810
1811 static void virtio_blk_pci_instance_init(Object *obj)
1812 {
1813     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1814
1815     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1816                                 TYPE_VIRTIO_BLK);
1817     object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1818                               &error_abort);
1819     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1820                               "bootindex", &error_abort);
1821 }
1822
1823 static const TypeInfo virtio_blk_pci_info = {
1824     .name          = TYPE_VIRTIO_BLK_PCI,
1825     .parent        = TYPE_VIRTIO_PCI,
1826     .instance_size = sizeof(VirtIOBlkPCI),
1827     .instance_init = virtio_blk_pci_instance_init,
1828     .class_init    = virtio_blk_pci_class_init,
1829 };
1830
1831 /* virtio-scsi-pci */
1832
1833 static Property virtio_scsi_pci_properties[] = {
1834     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1835                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1836     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1837                        DEV_NVECTORS_UNSPECIFIED),
1838     DEFINE_PROP_END_OF_LIST(),
1839 };
1840
1841 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1842 {
1843     VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1844     DeviceState *vdev = DEVICE(&dev->vdev);
1845     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1846     DeviceState *proxy = DEVICE(vpci_dev);
1847     char *bus_name;
1848
1849     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1850         vpci_dev->nvectors = vs->conf.num_queues + 3;
1851     }
1852
1853     /*
1854      * For command line compatibility, this sets the virtio-scsi-device bus
1855      * name as before.
1856      */
1857     if (proxy->id) {
1858         bus_name = g_strdup_printf("%s.0", proxy->id);
1859         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1860         g_free(bus_name);
1861     }
1862
1863     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1864     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1865 }
1866
1867 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1868 {
1869     DeviceClass *dc = DEVICE_CLASS(klass);
1870     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1871     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1872
1873     k->realize = virtio_scsi_pci_realize;
1874     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1875     dc->props = virtio_scsi_pci_properties;
1876     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1877     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1878     pcidev_k->revision = 0x00;
1879     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1880 }
1881
1882 static void virtio_scsi_pci_instance_init(Object *obj)
1883 {
1884     VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1885
1886     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1887                                 TYPE_VIRTIO_SCSI);
1888     object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
1889                               &error_abort);
1890 }
1891
1892 static const TypeInfo virtio_scsi_pci_info = {
1893     .name          = TYPE_VIRTIO_SCSI_PCI,
1894     .parent        = TYPE_VIRTIO_PCI,
1895     .instance_size = sizeof(VirtIOSCSIPCI),
1896     .instance_init = virtio_scsi_pci_instance_init,
1897     .class_init    = virtio_scsi_pci_class_init,
1898 };
1899
1900 /* vhost-scsi-pci */
1901
1902 #ifdef CONFIG_VHOST_SCSI
1903 static Property vhost_scsi_pci_properties[] = {
1904     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1905                        DEV_NVECTORS_UNSPECIFIED),
1906     DEFINE_PROP_END_OF_LIST(),
1907 };
1908
1909 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1910 {
1911     VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1912     DeviceState *vdev = DEVICE(&dev->vdev);
1913     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1914
1915     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1916         vpci_dev->nvectors = vs->conf.num_queues + 3;
1917     }
1918
1919     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1920     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1921 }
1922
1923 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1924 {
1925     DeviceClass *dc = DEVICE_CLASS(klass);
1926     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1927     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1928     k->realize = vhost_scsi_pci_realize;
1929     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1930     dc->props = vhost_scsi_pci_properties;
1931     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1932     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1933     pcidev_k->revision = 0x00;
1934     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1935 }
1936
1937 static void vhost_scsi_pci_instance_init(Object *obj)
1938 {
1939     VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
1940
1941     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1942                                 TYPE_VHOST_SCSI);
1943     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1944                               "bootindex", &error_abort);
1945 }
1946
1947 static const TypeInfo vhost_scsi_pci_info = {
1948     .name          = TYPE_VHOST_SCSI_PCI,
1949     .parent        = TYPE_VIRTIO_PCI,
1950     .instance_size = sizeof(VHostSCSIPCI),
1951     .instance_init = vhost_scsi_pci_instance_init,
1952     .class_init    = vhost_scsi_pci_class_init,
1953 };
1954 #endif
1955
1956 /* virtio-balloon-pci */
1957
1958 static Property virtio_balloon_pci_properties[] = {
1959     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1960     DEFINE_PROP_END_OF_LIST(),
1961 };
1962
1963 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1964 {
1965     VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1966     DeviceState *vdev = DEVICE(&dev->vdev);
1967
1968     if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1969         vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1970         vpci_dev->class_code = PCI_CLASS_OTHERS;
1971     }
1972
1973     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1974     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1975 }
1976
1977 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1978 {
1979     DeviceClass *dc = DEVICE_CLASS(klass);
1980     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1981     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1982     k->realize = virtio_balloon_pci_realize;
1983     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1984     dc->props = virtio_balloon_pci_properties;
1985     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1986     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1987     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1988     pcidev_k->class_id = PCI_CLASS_OTHERS;
1989 }
1990
1991 static void virtio_balloon_pci_instance_init(Object *obj)
1992 {
1993     VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
1994
1995     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1996                                 TYPE_VIRTIO_BALLOON);
1997     object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
1998                                   "guest-stats", &error_abort);
1999     object_property_add_alias(obj, "guest-stats-polling-interval",
2000                               OBJECT(&dev->vdev),
2001                               "guest-stats-polling-interval", &error_abort);
2002 }
2003
2004 static const TypeInfo virtio_balloon_pci_info = {
2005     .name          = TYPE_VIRTIO_BALLOON_PCI,
2006     .parent        = TYPE_VIRTIO_PCI,
2007     .instance_size = sizeof(VirtIOBalloonPCI),
2008     .instance_init = virtio_balloon_pci_instance_init,
2009     .class_init    = virtio_balloon_pci_class_init,
2010 };
2011
2012 /* virtio-serial-pci */
2013
2014 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2015 {
2016     VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
2017     DeviceState *vdev = DEVICE(&dev->vdev);
2018     DeviceState *proxy = DEVICE(vpci_dev);
2019     char *bus_name;
2020
2021     if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
2022         vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
2023         vpci_dev->class_code != PCI_CLASS_OTHERS) {        /* qemu-kvm  */
2024             vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
2025     }
2026
2027     /* backwards-compatibility with machines that were created with
2028        DEV_NVECTORS_UNSPECIFIED */
2029     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2030         vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
2031     }
2032
2033     /*
2034      * For command line compatibility, this sets the virtio-serial-device bus
2035      * name as before.
2036      */
2037     if (proxy->id) {
2038         bus_name = g_strdup_printf("%s.0", proxy->id);
2039         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2040         g_free(bus_name);
2041     }
2042
2043     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2044     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2045 }
2046
2047 static Property virtio_serial_pci_properties[] = {
2048     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2049                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2050     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2051     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2052     DEFINE_PROP_END_OF_LIST(),
2053 };
2054
2055 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
2056 {
2057     DeviceClass *dc = DEVICE_CLASS(klass);
2058     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2059     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2060     k->realize = virtio_serial_pci_realize;
2061     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2062     dc->props = virtio_serial_pci_properties;
2063     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2064     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
2065     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2066     pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2067 }
2068
2069 static void virtio_serial_pci_instance_init(Object *obj)
2070 {
2071     VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
2072
2073     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2074                                 TYPE_VIRTIO_SERIAL);
2075 }
2076
2077 static const TypeInfo virtio_serial_pci_info = {
2078     .name          = TYPE_VIRTIO_SERIAL_PCI,
2079     .parent        = TYPE_VIRTIO_PCI,
2080     .instance_size = sizeof(VirtIOSerialPCI),
2081     .instance_init = virtio_serial_pci_instance_init,
2082     .class_init    = virtio_serial_pci_class_init,
2083 };
2084
2085 /* virtio-net-pci */
2086
2087 static Property virtio_net_properties[] = {
2088     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2089                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
2090     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2091     DEFINE_PROP_END_OF_LIST(),
2092 };
2093
2094 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2095 {
2096     DeviceState *qdev = DEVICE(vpci_dev);
2097     VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
2098     DeviceState *vdev = DEVICE(&dev->vdev);
2099
2100     virtio_net_set_netclient_name(&dev->vdev, qdev->id,
2101                                   object_get_typename(OBJECT(qdev)));
2102     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2103     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2104 }
2105
2106 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
2107 {
2108     DeviceClass *dc = DEVICE_CLASS(klass);
2109     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2110     VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
2111
2112     k->romfile = "efi-virtio.rom";
2113     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2114     k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
2115     k->revision = VIRTIO_PCI_ABI_VERSION;
2116     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2117     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2118     dc->props = virtio_net_properties;
2119     vpciklass->realize = virtio_net_pci_realize;
2120 }
2121
2122 static void virtio_net_pci_instance_init(Object *obj)
2123 {
2124     VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2125
2126     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2127                                 TYPE_VIRTIO_NET);
2128     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2129                               "bootindex", &error_abort);
2130 }
2131
2132 static const TypeInfo virtio_net_pci_info = {
2133     .name          = TYPE_VIRTIO_NET_PCI,
2134     .parent        = TYPE_VIRTIO_PCI,
2135     .instance_size = sizeof(VirtIONetPCI),
2136     .instance_init = virtio_net_pci_instance_init,
2137     .class_init    = virtio_net_pci_class_init,
2138 };
2139
2140 /* virtio-rng-pci */
2141
2142 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2143 {
2144     VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2145     DeviceState *vdev = DEVICE(&vrng->vdev);
2146     Error *err = NULL;
2147
2148     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2149     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2150     if (err) {
2151         error_propagate(errp, err);
2152         return;
2153     }
2154
2155     object_property_set_link(OBJECT(vrng),
2156                              OBJECT(vrng->vdev.conf.rng), "rng",
2157                              NULL);
2158 }
2159
2160 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2161 {
2162     DeviceClass *dc = DEVICE_CLASS(klass);
2163     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2164     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2165
2166     k->realize = virtio_rng_pci_realize;
2167     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2168
2169     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2170     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2171     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2172     pcidev_k->class_id = PCI_CLASS_OTHERS;
2173 }
2174
2175 static void virtio_rng_initfn(Object *obj)
2176 {
2177     VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2178
2179     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2180                                 TYPE_VIRTIO_RNG);
2181     object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
2182                               &error_abort);
2183 }
2184
2185 static const TypeInfo virtio_rng_pci_info = {
2186     .name          = TYPE_VIRTIO_RNG_PCI,
2187     .parent        = TYPE_VIRTIO_PCI,
2188     .instance_size = sizeof(VirtIORngPCI),
2189     .instance_init = virtio_rng_initfn,
2190     .class_init    = virtio_rng_pci_class_init,
2191 };
2192
2193 /* virtio-input-pci */
2194
2195 static Property virtio_input_pci_properties[] = {
2196     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2197     DEFINE_PROP_END_OF_LIST(),
2198 };
2199
2200 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2201 {
2202     VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2203     DeviceState *vdev = DEVICE(&vinput->vdev);
2204
2205     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2206     /* force virtio-1.0 */
2207     vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN;
2208     vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY;
2209     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2210 }
2211
2212 static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2213 {
2214     DeviceClass *dc = DEVICE_CLASS(klass);
2215     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2216     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2217
2218     dc->props = virtio_input_pci_properties;
2219     k->realize = virtio_input_pci_realize;
2220     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2221
2222     pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2223 }
2224
2225 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2226 {
2227     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2228
2229     pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2230 }
2231
2232 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2233                                                   void *data)
2234 {
2235     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2236
2237     pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2238 }
2239
2240 static void virtio_keyboard_initfn(Object *obj)
2241 {
2242     VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2243
2244     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2245                                 TYPE_VIRTIO_KEYBOARD);
2246 }
2247
2248 static void virtio_mouse_initfn(Object *obj)
2249 {
2250     VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2251
2252     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2253                                 TYPE_VIRTIO_MOUSE);
2254 }
2255
2256 static void virtio_tablet_initfn(Object *obj)
2257 {
2258     VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2259
2260     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2261                                 TYPE_VIRTIO_TABLET);
2262 }
2263
2264 static const TypeInfo virtio_input_pci_info = {
2265     .name          = TYPE_VIRTIO_INPUT_PCI,
2266     .parent        = TYPE_VIRTIO_PCI,
2267     .instance_size = sizeof(VirtIOInputPCI),
2268     .class_init    = virtio_input_pci_class_init,
2269     .abstract      = true,
2270 };
2271
2272 static const TypeInfo virtio_input_hid_pci_info = {
2273     .name          = TYPE_VIRTIO_INPUT_HID_PCI,
2274     .parent        = TYPE_VIRTIO_INPUT_PCI,
2275     .instance_size = sizeof(VirtIOInputHIDPCI),
2276     .abstract      = true,
2277 };
2278
2279 static const TypeInfo virtio_keyboard_pci_info = {
2280     .name          = TYPE_VIRTIO_KEYBOARD_PCI,
2281     .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
2282     .class_init    = virtio_input_hid_kbd_pci_class_init,
2283     .instance_size = sizeof(VirtIOInputHIDPCI),
2284     .instance_init = virtio_keyboard_initfn,
2285 };
2286
2287 static const TypeInfo virtio_mouse_pci_info = {
2288     .name          = TYPE_VIRTIO_MOUSE_PCI,
2289     .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
2290     .class_init    = virtio_input_hid_mouse_pci_class_init,
2291     .instance_size = sizeof(VirtIOInputHIDPCI),
2292     .instance_init = virtio_mouse_initfn,
2293 };
2294
2295 static const TypeInfo virtio_tablet_pci_info = {
2296     .name          = TYPE_VIRTIO_TABLET_PCI,
2297     .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
2298     .instance_size = sizeof(VirtIOInputHIDPCI),
2299     .instance_init = virtio_tablet_initfn,
2300 };
2301
2302 #ifdef CONFIG_LINUX
2303 static void virtio_host_initfn(Object *obj)
2304 {
2305     VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2306
2307     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2308                                 TYPE_VIRTIO_INPUT_HOST);
2309 }
2310
2311 static const TypeInfo virtio_host_pci_info = {
2312     .name          = TYPE_VIRTIO_INPUT_HOST_PCI,
2313     .parent        = TYPE_VIRTIO_INPUT_PCI,
2314     .instance_size = sizeof(VirtIOInputHostPCI),
2315     .instance_init = virtio_host_initfn,
2316 };
2317 #endif
2318
2319 /* virtio-pci-bus */
2320
2321 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2322                                VirtIOPCIProxy *dev)
2323 {
2324     DeviceState *qdev = DEVICE(dev);
2325     char virtio_bus_name[] = "virtio-bus";
2326
2327     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2328                         virtio_bus_name);
2329 }
2330
2331 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2332 {
2333     BusClass *bus_class = BUS_CLASS(klass);
2334     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2335     bus_class->max_dev = 1;
2336     k->notify = virtio_pci_notify;
2337     k->save_config = virtio_pci_save_config;
2338     k->load_config = virtio_pci_load_config;
2339     k->save_queue = virtio_pci_save_queue;
2340     k->load_queue = virtio_pci_load_queue;
2341     k->save_extra_state = virtio_pci_save_extra_state;
2342     k->load_extra_state = virtio_pci_load_extra_state;
2343     k->has_extra_state = virtio_pci_has_extra_state;
2344     k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2345     k->set_host_notifier = virtio_pci_set_host_notifier;
2346     k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2347     k->vmstate_change = virtio_pci_vmstate_change;
2348     k->device_plugged = virtio_pci_device_plugged;
2349     k->device_unplugged = virtio_pci_device_unplugged;
2350     k->query_nvectors = virtio_pci_query_nvectors;
2351 }
2352
2353 static const TypeInfo virtio_pci_bus_info = {
2354     .name          = TYPE_VIRTIO_PCI_BUS,
2355     .parent        = TYPE_VIRTIO_BUS,
2356     .instance_size = sizeof(VirtioPCIBusState),
2357     .class_init    = virtio_pci_bus_class_init,
2358 };
2359
2360 static void virtio_pci_register_types(void)
2361 {
2362     type_register_static(&virtio_rng_pci_info);
2363     type_register_static(&virtio_input_pci_info);
2364     type_register_static(&virtio_input_hid_pci_info);
2365     type_register_static(&virtio_keyboard_pci_info);
2366     type_register_static(&virtio_mouse_pci_info);
2367     type_register_static(&virtio_tablet_pci_info);
2368 #ifdef CONFIG_LINUX
2369     type_register_static(&virtio_host_pci_info);
2370 #endif
2371     type_register_static(&virtio_pci_bus_info);
2372     type_register_static(&virtio_pci_info);
2373 #ifdef CONFIG_VIRTFS
2374     type_register_static(&virtio_9p_pci_info);
2375 #endif
2376     type_register_static(&virtio_blk_pci_info);
2377     type_register_static(&virtio_scsi_pci_info);
2378     type_register_static(&virtio_balloon_pci_info);
2379     type_register_static(&virtio_serial_pci_info);
2380     type_register_static(&virtio_net_pci_info);
2381 #ifdef CONFIG_VHOST_SCSI
2382     type_register_static(&vhost_scsi_pci_info);
2383 #endif
2384 }
2385
2386 type_init(virtio_pci_register_types)