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