sync with latest
[sdk/emulator/qemu.git] / hw / virtio-pci-new.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 "virtio.h"
21 #include "virtio-transport.h"
22 #include "virtio-blk.h"
23 #include "virtio-net.h"
24 #include "virtio-serial.h"
25 #include "virtio-scsi.h"
26 #include "virtio-balloon.h"
27 #include "pci.h"
28 #include "qemu-error.h"
29 #include "msi.h"
30 #include "msix.h"
31 #include "net.h"
32 #include "loader.h"
33 #include "kvm.h"
34 #include "blockdev.h"
35 #include "virtio-pci.h"
36 #include "range.h"
37
38 /* from Linux's linux/virtio_pci.h */
39
40 /* A 32-bit r/o bitmask of the features supported by the host */
41 #define VIRTIO_PCI_HOST_FEATURES        0
42
43 /* A 32-bit r/w bitmask of features activated by the guest */
44 #define VIRTIO_PCI_GUEST_FEATURES       4
45
46 /* A 32-bit r/w PFN for the currently selected queue */
47 #define VIRTIO_PCI_QUEUE_PFN            8
48
49 /* A 16-bit r/o queue size for the currently selected queue */
50 #define VIRTIO_PCI_QUEUE_NUM            12
51
52 /* A 16-bit r/w queue selector */
53 #define VIRTIO_PCI_QUEUE_SEL            14
54
55 /* A 16-bit r/w queue notifier */
56 #define VIRTIO_PCI_QUEUE_NOTIFY         16
57
58 /* An 8-bit device status register.  */
59 #define VIRTIO_PCI_STATUS               18
60
61 /* An 8-bit r/o interrupt status register.  Reading the value will return the
62  * current contents of the ISR and will also clear it.  This is effectively
63  * a read-and-acknowledge. */
64 #define VIRTIO_PCI_ISR                  19
65
66 /* MSI-X registers: only enabled if MSI-X is enabled. */
67 /* A 16-bit vector for configuration changes. */
68 #define VIRTIO_MSI_CONFIG_VECTOR        20
69 /* A 16-bit vector for selected queue notifications. */
70 #define VIRTIO_MSI_QUEUE_VECTOR         22
71
72 /* Config space size */
73 #define VIRTIO_PCI_CONFIG_NOMSI         20
74 #define VIRTIO_PCI_CONFIG_MSI           24
75 #define VIRTIO_PCI_REGION_SIZE(dev)     (msix_present(dev) ? \
76                                          VIRTIO_PCI_CONFIG_MSI : \
77                                          VIRTIO_PCI_CONFIG_NOMSI)
78
79 /* The remaining space is defined by each driver as the per-driver
80  * configuration space */
81 #define VIRTIO_PCI_CONFIG(dev)          (msix_enabled(dev) ? \
82                                          VIRTIO_PCI_CONFIG_MSI : \
83                                          VIRTIO_PCI_CONFIG_NOMSI)
84
85 /* How many bits to shift physical queue address written to QUEUE_PFN.
86  * 12 is historical, and due to x86 page size. */
87 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
88
89 /* Flags track per-device state like workarounds for quirks in older guests. */
90 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
91
92 /* QEMU doesn't strictly need write barriers since everything runs in
93  * lock-step.  We'll leave the calls to wmb() in though to make it obvious for
94  * KVM or if kqemu gets SMP support.
95  */
96 #define wmb() do { } while (0)
97
98 /* HACK for virtio to determine if it's running a big endian guest */
99 bool virtio_is_big_endian(void);
100
101 /* virtio device */
102
103 static void virtio_pci_notify(void *opaque, uint16_t vector)
104 {
105     VirtIOPCI *s = opaque;
106     if (msix_enabled(&s->pci_dev)) {
107         msix_notify(&s->pci_dev, vector);
108     }
109     else {
110         qemu_set_irq(s->pci_dev.irq[0], s->vdev->isr & 1);
111     }
112 }
113
114 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
115 {
116     VirtIOPCI *s = opaque;
117     pci_device_save(&s->pci_dev, f);
118     msix_save(&s->pci_dev, f);
119     if (msix_present(&s->pci_dev)) {
120         qemu_put_be16(f, s->vdev->config_vector);
121     }
122 }
123
124 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
125 {
126     VirtIOPCI *s = opaque;
127     if (msix_present(&s->pci_dev)) {
128         qemu_put_be16(f, virtio_queue_vector(s->vdev, n));
129     }
130 }
131
132 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
133 {
134     VirtIOPCI *s = opaque;
135     int ret;
136     ret = pci_device_load(&s->pci_dev, f);
137     if (ret) {
138         return ret;
139     }
140     msix_unuse_all_vectors(&s->pci_dev);
141     msix_load(&s->pci_dev, f);
142     if (msix_present(&s->pci_dev)) {
143         qemu_get_be16s(f, &s->vdev->config_vector);
144     } else {
145         s->vdev->config_vector = VIRTIO_NO_VECTOR;
146     }
147     if (s->vdev->config_vector != VIRTIO_NO_VECTOR) {
148         return msix_vector_use(&s->pci_dev, s->vdev->config_vector);
149     }
150     return 0;
151 }
152
153 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
154 {
155     VirtIOPCI *s = opaque;
156     uint16_t vector;
157     if (msix_present(&s->pci_dev)) {
158         qemu_get_be16s(f, &vector);
159     } else {
160         vector = VIRTIO_NO_VECTOR;
161     }
162     virtio_queue_set_vector(s->vdev, n, vector);
163     if (vector != VIRTIO_NO_VECTOR) {
164         return msix_vector_use(&s->pci_dev, vector);
165     }
166     return 0;
167 }
168
169 static int virtio_pci_set_host_notifier_internal(VirtIOPCI *s, int n,
170         bool assign, bool set_handler)
171 {
172     VirtQueue *vq = virtio_get_queue(s->vdev, n);
173     EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
174     int r = 0;
175
176     if (assign) {
177         r = event_notifier_init(notifier, 1);
178         if (r < 0) {
179             error_report("%s: unable to init event notifier: %d", __func__, r);
180             return r;
181         }
182         virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
183         memory_region_add_eventfd(&s->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, true, n,
184                 notifier);
185     } else {
186         memory_region_del_eventfd(&s->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, true, n,
187                 notifier);
188         virtio_queue_set_host_notifier_fd_handler(vq, false, false);
189         event_notifier_cleanup(notifier);
190     }
191     return r;
192 }
193
194 static void virtio_pci_start_ioeventfd(VirtIOPCI *s)
195 {
196     int n, r;
197
198     if (!(s->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
199         s->ioeventfd_disabled ||
200         s->ioeventfd_started) {
201         return;
202     }
203
204     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
205         if (!virtio_queue_get_num(s->vdev, n)) {
206             continue;
207         }
208
209         r = virtio_pci_set_host_notifier_internal(s, n, true, true);
210         if (r < 0) {
211             goto assign_error;
212         }
213     }
214     s->ioeventfd_started = true;
215     return;
216
217 assign_error:
218     while (--n >= 0) {
219         if (!virtio_queue_get_num(s->vdev, n)) {
220             continue;
221         }
222
223         r = virtio_pci_set_host_notifier_internal(s, n, false, false);
224         assert(r >= 0);
225     }
226     s->ioeventfd_started = false;
227     error_report("%s: failed. Fallback to a userspace (slower).", __func__);
228 }
229
230 static void virtio_pci_stop_ioeventfd(VirtIOPCI *s)
231 {
232     int r;
233     int n;
234
235     if (!s->ioeventfd_started) {
236         return;
237     }
238
239     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
240         if (!virtio_queue_get_num(s->vdev, n)) {
241             continue;
242         }
243
244         r = virtio_pci_set_host_notifier_internal(s, n, false, false);
245         assert(r >= 0);
246     }
247     s->ioeventfd_started = false;
248 }
249
250 void virtio_pci_reset_(DeviceState *d)
251 {
252     VirtIOPCI *s =
253             container_of(d, VirtIOPCI, pci_dev.qdev);
254     virtio_pci_stop_ioeventfd(s);
255     if (s->vdev) {
256         virtio_reset(s->vdev);
257     }
258     msix_unuse_all_vectors(&s->pci_dev);
259     s->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
260 }
261
262 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
263 {
264     VirtIOPCI *s = opaque;
265     VirtIODevice *vdev = s->vdev;
266     target_phys_addr_t pa;
267
268     switch (addr) {
269     case VIRTIO_PCI_GUEST_FEATURES:
270         /* Guest does not negotiate properly?  We have to assume nothing. */
271         if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
272             val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
273         }
274         virtio_set_features(vdev, val);
275         break;
276     case VIRTIO_PCI_QUEUE_PFN:
277         pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
278         if (pa == 0) {
279             virtio_pci_stop_ioeventfd(s);
280             virtio_reset(s->vdev);
281             msix_unuse_all_vectors(&s->pci_dev);
282         }
283         else
284             virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
285         break;
286     case VIRTIO_PCI_QUEUE_SEL:
287         if (val < VIRTIO_PCI_QUEUE_MAX)
288             vdev->queue_sel = val;
289         break;
290     case VIRTIO_PCI_QUEUE_NOTIFY:
291         if (val < VIRTIO_PCI_QUEUE_MAX) {
292             virtio_queue_notify(vdev, val);
293         }
294         break;
295     case VIRTIO_PCI_STATUS:
296         if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
297             virtio_pci_stop_ioeventfd(s);
298         }
299
300         virtio_set_status(vdev, val & 0xFF);
301
302         if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
303             virtio_pci_start_ioeventfd(s);
304         }
305
306         if (vdev->status == 0) {
307             virtio_reset(s->vdev);
308             msix_unuse_all_vectors(&s->pci_dev);
309         }
310
311         /* Linux before 2.6.34 sets the device as OK without enabling
312            the PCI device bus master bit. In this case we need to disable
313            some safety checks. */
314         if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
315             !(s->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
316             s->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
317         }
318         break;
319     case VIRTIO_MSI_CONFIG_VECTOR:
320         msix_vector_unuse(&s->pci_dev, vdev->config_vector);
321         /* Make it possible for guest to discover an error took place. */
322         if (msix_vector_use(&s->pci_dev, val) < 0)
323             val = VIRTIO_NO_VECTOR;
324         vdev->config_vector = val;
325         break;
326     case VIRTIO_MSI_QUEUE_VECTOR:
327         msix_vector_unuse(&s->pci_dev,
328                           virtio_queue_vector(vdev, vdev->queue_sel));
329         /* Make it possible for guest to discover an error took place. */
330         if (msix_vector_use(&s->pci_dev, val) < 0)
331             val = VIRTIO_NO_VECTOR;
332         virtio_queue_set_vector(vdev, vdev->queue_sel, val);
333         break;
334     default:
335         error_report("%s: unexpected address 0x%x value 0x%x",
336                      __func__, addr, val);
337         break;
338     }
339 }
340
341 static uint32_t virtio_ioport_read(VirtIOPCI *s, uint32_t addr)
342 {
343     VirtIODevice *vdev = s->vdev;
344     uint32_t ret = 0xFFFFFFFF;
345
346     switch (addr) {
347     case VIRTIO_PCI_HOST_FEATURES:
348         ret = s->host_features;
349         break;
350     case VIRTIO_PCI_GUEST_FEATURES:
351         ret = vdev->guest_features;
352         break;
353     case VIRTIO_PCI_QUEUE_PFN:
354         ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
355               >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
356         break;
357     case VIRTIO_PCI_QUEUE_NUM:
358         ret = virtio_queue_get_num(vdev, vdev->queue_sel);
359         break;
360     case VIRTIO_PCI_QUEUE_SEL:
361         ret = vdev->queue_sel;
362         break;
363     case VIRTIO_PCI_STATUS:
364         ret = vdev->status;
365         break;
366     case VIRTIO_PCI_ISR:
367         /* reading from the ISR also clears it. */
368         ret = vdev->isr;
369         vdev->isr = 0;
370         qemu_set_irq(s->pci_dev.irq[0], 0);
371         break;
372     case VIRTIO_MSI_CONFIG_VECTOR:
373         ret = vdev->config_vector;
374         break;
375     case VIRTIO_MSI_QUEUE_VECTOR:
376         ret = virtio_queue_vector(vdev, vdev->queue_sel);
377         break;
378     default:
379         break;
380     }
381
382     return ret;
383 }
384
385 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
386 {
387     VirtIOPCI *s = opaque;
388     uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
389     if (addr < config)
390         return virtio_ioport_read(s, addr);
391     addr -= config;
392     return virtio_config_readb(s->vdev, addr);
393 }
394
395 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
396 {
397     VirtIOPCI *s = opaque;
398     uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
399     uint16_t val;
400     if (addr < config)
401         return virtio_ioport_read(s, addr);
402     addr -= config;
403     val = virtio_config_readw(s->vdev, addr);
404     if (virtio_is_big_endian()) {
405         /*
406          * virtio is odd, ioports are LE but config space is target native
407          * endian. However, in qemu, all PIO is LE, so we need to re-swap
408          * on BE targets
409          */
410         val = bswap16(val);
411     }
412     return val;
413 }
414
415 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
416 {
417     VirtIOPCI *s = opaque;
418     uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
419     uint32_t val;
420     if (addr < config)
421         return virtio_ioport_read(s, addr);
422     addr -= config;
423     val = virtio_config_readl(s->vdev, addr);
424     if (virtio_is_big_endian()) {
425         val = bswap32(val);
426     }
427     return val;
428 }
429
430 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
431 {
432     VirtIOPCI *s = opaque;
433     uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
434     if (addr < config) {
435         virtio_ioport_write(s, addr, val);
436         return;
437     }
438     addr -= config;
439     virtio_config_writeb(s->vdev, addr, val);
440 }
441
442 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
443 {
444     VirtIOPCI *s = opaque;
445     uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
446     if (addr < config) {
447         virtio_ioport_write(s, addr, val);
448         return;
449     }
450     addr -= config;
451     if (virtio_is_big_endian()) {
452         val = bswap16(val);
453     }
454     virtio_config_writew(s->vdev, addr, val);
455 }
456
457 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
458 {
459     VirtIOPCI *s = opaque;
460     uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
461     if (addr < config) {
462         virtio_ioport_write(s, addr, val);
463         return;
464     }
465     addr -= config;
466     if (virtio_is_big_endian()) {
467         val = bswap32(val);
468     }
469     virtio_config_writel(s->vdev, addr, val);
470 }
471
472 static const MemoryRegionPortio virtio_portio[] = {
473     { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
474     { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
475     { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
476     { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
477     { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
478     { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
479     PORTIO_END_OF_LIST()
480 };
481
482 static const MemoryRegionOps virtio_pci_config_ops = {
483     .old_portio = virtio_portio,
484     .endianness = DEVICE_LITTLE_ENDIAN,
485 };
486
487 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
488                                 uint32_t val, int len)
489 {
490     VirtIOPCI *s = DO_UPCAST(VirtIOPCI,
491             pci_dev, pci_dev);
492
493     pci_default_write_config(pci_dev, address, val, len);
494
495     if (range_covers_byte(address, len, PCI_COMMAND) &&
496         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
497         !(s->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
498         virtio_pci_stop_ioeventfd(s);
499         virtio_set_status(s->vdev,
500                           s->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
501     }
502 }
503
504 static unsigned virtio_pci_get_features(void *opaque)
505 {
506     VirtIOPCI *s = opaque;
507     return s->host_features;
508 }
509
510 static int kvm_virtio_pci_vq_vector_use(VirtIOPCI *s,
511                                         unsigned int queue_no,
512                                         unsigned int vector,
513                                         MSIMessage msg)
514 {
515     VirtQueue *vq = virtio_get_queue(s->vdev, queue_no);
516     EventNotifier *n = virtio_queue_get_guest_notifier(vq);
517     VirtIOIRQFD *irqfd = &s->vector_irqfd[vector];
518     int ret;
519
520     if (irqfd->users == 0) {
521         ret = kvm_irqchip_add_msi_route(kvm_state, msg);
522         if (ret < 0) {
523             return ret;
524         }
525         irqfd->virq = ret;
526     }
527     irqfd->users++;
528
529     ret = kvm_irqchip_add_irq_notifier(kvm_state, n, irqfd->virq);
530     if (ret < 0) {
531         if (--irqfd->users == 0) {
532             kvm_irqchip_release_virq(kvm_state, irqfd->virq);
533         }
534         return ret;
535     }
536
537     virtio_queue_set_guest_notifier_fd_handler(vq, true, true);
538     return 0;
539 }
540
541 static void kvm_virtio_pci_vq_vector_release(VirtIOPCI *s,
542                                              unsigned int queue_no,
543                                              unsigned int vector)
544 {
545     VirtQueue *vq = virtio_get_queue(s->vdev, queue_no);
546     EventNotifier *n = virtio_queue_get_guest_notifier(vq);
547     VirtIOIRQFD *irqfd = &s->vector_irqfd[vector];
548     int ret;
549
550     ret = kvm_irqchip_remove_irq_notifier(kvm_state, n, irqfd->virq);
551     assert(ret == 0);
552
553     if (--irqfd->users == 0) {
554         kvm_irqchip_release_virq(kvm_state, irqfd->virq);
555     }
556
557     virtio_queue_set_guest_notifier_fd_handler(vq, true, false);
558 }
559
560 static int kvm_virtio_pci_vector_use(PCIDevice *dev, unsigned vector,
561                                      MSIMessage msg)
562 {
563     VirtIOPCI *s = container_of(dev, VirtIOPCI, pci_dev);
564     VirtIODevice *vdev = s->vdev;
565     int ret, queue_no;
566
567     for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
568         if (!virtio_queue_get_num(vdev, queue_no)) {
569             break;
570         }
571         if (virtio_queue_vector(vdev, queue_no) != vector) {
572             continue;
573         }
574         ret = kvm_virtio_pci_vq_vector_use(s, queue_no, vector, msg);
575         if (ret < 0) {
576             goto undo;
577         }
578     }
579     return 0;
580
581 undo:
582     while (--queue_no >= 0) {
583         if (virtio_queue_vector(vdev, queue_no) != vector) {
584             continue;
585         }
586         kvm_virtio_pci_vq_vector_release(s, queue_no, vector);
587     }
588     return ret;
589 }
590
591 static void kvm_virtio_pci_vector_release(PCIDevice *dev, unsigned vector)
592 {
593     VirtIOPCI *s = container_of(dev, VirtIOPCI, pci_dev);
594     VirtIODevice *vdev = s->vdev;
595     int queue_no;
596
597     for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
598         if (!virtio_queue_get_num(vdev, queue_no)) {
599             break;
600         }
601         if (virtio_queue_vector(vdev, queue_no) != vector) {
602             continue;
603         }
604         kvm_virtio_pci_vq_vector_release(s, queue_no, vector);
605     }
606 }
607
608 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
609 {
610     VirtIOPCI *s = opaque;
611     VirtQueue *vq = virtio_get_queue(s->vdev, n);
612     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
613
614     if (assign) {
615         int r = event_notifier_init(notifier, 0);
616         if (r < 0) {
617             return r;
618         }
619         virtio_queue_set_guest_notifier_fd_handler(vq, true, false);
620     } else {
621         virtio_queue_set_guest_notifier_fd_handler(vq, false, false);
622         event_notifier_cleanup(notifier);
623     }
624
625     return 0;
626 }
627
628 static bool virtio_pci_query_guest_notifiers(void *opaque)
629 {
630     VirtIOPCI *s = opaque;
631     return msix_enabled(&s->pci_dev);
632 }
633
634 static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
635 {
636     VirtIOPCI *s = opaque;
637     VirtIODevice *vdev = s->vdev;
638     int r, n;
639
640     /* Must unset vector notifier while guest notifier is still assigned */
641     if (kvm_msi_via_irqfd_enabled() && !assign) {
642         msix_unset_vector_notifiers(&s->pci_dev);
643         g_free(s->vector_irqfd);
644         s->vector_irqfd = NULL;
645     }
646
647     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
648         if (!virtio_queue_get_num(vdev, n)) {
649             break;
650         }
651
652         r = virtio_pci_set_guest_notifier(opaque, n, assign);
653         if (r < 0) {
654             goto assign_error;
655         }
656     }
657
658     /* Must set vector notifier after guest notifier has been assigned */
659     if (kvm_msi_via_irqfd_enabled() && assign) {
660         s->vector_irqfd =
661             g_malloc0(sizeof(*s->vector_irqfd) *
662                       msix_nr_vectors_allocated(&s->pci_dev));
663         r = msix_set_vector_notifiers(&s->pci_dev,
664                                       kvm_virtio_pci_vector_use,
665                                       kvm_virtio_pci_vector_release);
666         if (r < 0) {
667             goto assign_error;
668         }
669     }
670
671     return 0;
672
673 assign_error:
674     /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
675     assert(assign);
676     while (--n >= 0) {
677         virtio_pci_set_guest_notifier(opaque, n, !assign);
678     }
679     return r;
680 }
681
682 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
683 {
684     VirtIOPCI *s = opaque;
685
686     /* Stop using ioeventfd for virtqueue kick if the device starts using host
687      * notifiers.  This makes it easy to avoid stepping on each others' toes.
688      */
689     s->ioeventfd_disabled = assign;
690     if (assign) {
691         virtio_pci_stop_ioeventfd(s);
692     }
693     /* We don't need to start here: it's not needed because backend
694      * currently only stops on status change away from ok,
695      * reset, vmstop and such. If we do add code to start here,
696      * need to check vmstate, device state etc. */
697     return virtio_pci_set_host_notifier_internal(s, n, assign, false);
698 }
699
700 static void virtio_pci_vmstate_change(void *opaque, bool running)
701 {
702     VirtIOPCI *s = opaque;
703
704     if (running) {
705         /* Try to find out if the guest has bus master disabled, but is
706            in ready state. Then we have a buggy guest OS. */
707         if ((s->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
708             !(s->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
709             s->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
710         }
711         virtio_pci_start_ioeventfd(s);
712     } else {
713         virtio_pci_stop_ioeventfd(s);
714     }
715 }
716
717 static const VirtIOBindings virtio_pci_bindings = {
718     .notify = virtio_pci_notify,
719     .save_config = virtio_pci_save_config,
720     .load_config = virtio_pci_load_config,
721     .save_queue = virtio_pci_save_queue,
722     .load_queue = virtio_pci_load_queue,
723     .get_features = virtio_pci_get_features,
724     .query_guest_notifiers = virtio_pci_query_guest_notifiers,
725     .set_host_notifier = virtio_pci_set_host_notifier,
726     .set_guest_notifiers = virtio_pci_set_guest_notifiers,
727     .vmstate_change = virtio_pci_vmstate_change,
728 };
729
730 static void virtio_init_pci_(VirtIOPCI *s, VirtIODevice *vdev)
731 {
732     uint8_t *config;
733     uint32_t size;
734
735     s->vdev = vdev;
736
737     config = s->pci_dev.config;
738
739     if (s->class_code) {
740         pci_config_set_class(config, s->class_code);
741     }
742     pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
743                  pci_get_word(config + PCI_VENDOR_ID));
744     pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
745     config[PCI_INTERRUPT_PIN] = 1;
746
747     if (vdev->nvectors &&
748         msix_init_exclusive_bar(&s->pci_dev, vdev->nvectors, 1)) {
749         vdev->nvectors = 0;
750     }
751
752     s->pci_dev.config_write = virtio_write_config;
753
754     size = VIRTIO_PCI_REGION_SIZE(&s->pci_dev) + vdev->config_len;
755     if (size & (size-1))
756         size = 1 << qemu_fls(size);
757
758     memory_region_init_io(&s->bar, &virtio_pci_config_ops, s,
759                           "virtio-pci", size);
760     pci_register_bar(&s->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
761                      &s->bar);
762
763     if (!kvm_has_many_ioeventfds()) {
764         s->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
765     }
766
767     virtio_bind_device(vdev, &virtio_pci_bindings, s);
768     s->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
769     s->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
770     s->host_features = vdev->get_features(vdev, s->host_features);
771 }
772
773 static void virtio_exit_pci(PCIDevice *pci_dev)
774 {
775     VirtIOPCI *s = DO_UPCAST(VirtIOPCI, pci_dev, pci_dev);
776
777     memory_region_destroy(&s->bar);
778     msix_uninit_exclusive_bar(pci_dev);
779 }
780
781 static int virtio_pci_transport_cb(DeviceState *dev, VirtIODevice *vdev,
782         VirtIOTransportLink *trl)
783 {
784     PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, trl->tr);
785     VirtIOPCI *s = DO_UPCAST(VirtIOPCI, pci_dev, pci_dev);
786
787     virtio_plug_into_transport(dev, trl);
788
789     // TODO: Figure out if props were explicitly set before
790
791     /* Get default host_features passed from back-end */
792     s->host_features = s->trl->host_features;
793
794     switch (vdev->device_id) {
795     case VIRTIO_ID_BLOCK:
796         s->flags |= VIRTIO_PCI_FLAG_USE_IOEVENTFD;
797         s->nvectors = 2;
798
799         if (s->class_code != PCI_CLASS_STORAGE_SCSI &&
800             s->class_code != PCI_CLASS_STORAGE_OTHER) {
801             s->class_code = PCI_CLASS_STORAGE_SCSI;
802         }
803
804         vdev->nvectors = s->nvectors;
805         pci_config_set_device_id(s->pci_dev.config, PCI_DEVICE_ID_VIRTIO_BLOCK);
806         pci_config_set_class(s->pci_dev.config, PCI_CLASS_STORAGE_SCSI);
807         virtio_init_pci_(s, vdev);
808         s->nvectors = vdev->nvectors;
809         break;
810     case VIRTIO_ID_NET:
811         s->nvectors = 3;
812
813         /* load rom */
814         pci_dev->romfile = g_strdup("pxe-virtio.rom");
815         pci_add_option_rom(pci_dev, false);
816
817         vdev->nvectors = s->nvectors;
818         pci_config_set_device_id(s->pci_dev.config, PCI_DEVICE_ID_VIRTIO_NET);
819         pci_config_set_class(s->pci_dev.config, PCI_CLASS_NETWORK_ETHERNET);
820         virtio_init_pci_(s, vdev);
821         s->nvectors = vdev->nvectors;
822         break;
823     case VIRTIO_ID_BALLOON:
824         break;
825     case VIRTIO_ID_SCSI:
826         break;
827     case VIRTIO_ID_CONSOLE:
828     {
829         break;
830     }
831     default:
832         fprintf(stderr,
833                 "Unknown back-end device id: 0x%" PRIx16 "\n", vdev->device_id);
834         return -1;
835     }
836
837     return 0;
838 }
839
840 static int virtio_pci_device_init(PCIDevice *pci_dev)
841 {
842     VirtIOPCI *s =
843             DO_UPCAST(VirtIOPCI, pci_dev, pci_dev);
844
845     virtio_init_transport(&pci_dev->qdev, &s->trl, VIRTIO_PCI,
846             virtio_pci_transport_cb);
847
848     return 0;
849 }
850
851 static void virtio_pci_device_exit(PCIDevice *pci_dev)
852 {
853     VirtIOPCI *s =
854             DO_UPCAST(VirtIOPCI, pci_dev, pci_dev);
855
856     switch (s->vdev->device_id) {
857         case VIRTIO_ID_BLOCK:
858             virtio_pci_stop_ioeventfd(s);
859             virtio_blk_exit(s->vdev);
860             break;
861         case VIRTIO_ID_NET:
862             virtio_pci_stop_ioeventfd(s);
863             virtio_net_exit(s->vdev);
864             break;
865         case VIRTIO_ID_BALLOON:
866             virtio_pci_stop_ioeventfd(s);
867             virtio_balloon_exit(s->vdev);
868             break;
869         case VIRTIO_ID_SCSI:
870             virtio_scsi_exit(s->vdev);
871             break;
872         case VIRTIO_ID_CONSOLE:
873             virtio_pci_stop_ioeventfd(s);
874             virtio_serial_exit(s->vdev);
875             break;
876         default:
877             fprintf(stderr,
878                     "Unknown back-end device id: 0x%" PRIx16 "\n",
879                     s->vdev->device_id);
880             return;
881         }
882
883     virtio_exit_pci(pci_dev);
884
885     return;
886 }
887
888 /******************** VirtIOPCI Device **********************/
889
890 static Property virtio_pci_properties[] = {
891     DEFINE_PROP_HEX32("class", VirtIOPCI, class_code, 0),
892     DEFINE_PROP_BIT("ioeventfd", VirtIOPCI, flags,
893             VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
894     DEFINE_PROP_UINT32("vectors", VirtIOPCI, nvectors, 0),
895     DEFINE_PROP_END_OF_LIST(),
896 };
897
898 static void virtio_pci_class_init(ObjectClass *klass, void *data)
899 {
900     DeviceClass *dc = DEVICE_CLASS(klass);
901     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
902     k->init = virtio_pci_device_init;
903     k->exit = virtio_pci_device_exit;
904     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
905     k->revision = VIRTIO_PCI_ABI_VERSION;
906     k->class_id = PCI_CLASS_OTHERS;
907     dc->reset = virtio_pci_reset_;
908     dc->props = virtio_pci_properties;
909 }
910
911 static TypeInfo virtio_pci_info = {
912     .name = VIRTIO_PCI,
913     .parent = TYPE_PCI_DEVICE,
914     .instance_size = sizeof(VirtIOPCI),
915     .class_init = virtio_pci_class_init,
916 };
917
918 /************************************************************/
919
920 static void virtio_pci_register_types(void)
921 {
922     type_register_static(&virtio_pci_info);
923 }
924
925 type_init(virtio_pci_register_types)