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