build: fix race with creating qapi-generated
[qemu.git] / hw / virtio-pci.c
blobdf27c198b0938475a7c4e030bae79d40c0624975
1 /*
2 * Virtio PCI Bindings
4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #include <inttypes.h>
18 #include "virtio.h"
19 #include "virtio-blk.h"
20 #include "virtio-net.h"
21 #include "virtio-serial.h"
22 #include "pci.h"
23 #include "qemu-error.h"
24 #include "msix.h"
25 #include "net.h"
26 #include "loader.h"
27 #include "kvm.h"
28 #include "blockdev.h"
29 #include "virtio-pci.h"
30 #include "range.h"
32 /* from Linux's linux/virtio_pci.h */
34 /* A 32-bit r/o bitmask of the features supported by the host */
35 #define VIRTIO_PCI_HOST_FEATURES 0
37 /* A 32-bit r/w bitmask of features activated by the guest */
38 #define VIRTIO_PCI_GUEST_FEATURES 4
40 /* A 32-bit r/w PFN for the currently selected queue */
41 #define VIRTIO_PCI_QUEUE_PFN 8
43 /* A 16-bit r/o queue size for the currently selected queue */
44 #define VIRTIO_PCI_QUEUE_NUM 12
46 /* A 16-bit r/w queue selector */
47 #define VIRTIO_PCI_QUEUE_SEL 14
49 /* A 16-bit r/w queue notifier */
50 #define VIRTIO_PCI_QUEUE_NOTIFY 16
52 /* An 8-bit device status register. */
53 #define VIRTIO_PCI_STATUS 18
55 /* An 8-bit r/o interrupt status register. Reading the value will return the
56 * current contents of the ISR and will also clear it. This is effectively
57 * a read-and-acknowledge. */
58 #define VIRTIO_PCI_ISR 19
60 /* MSI-X registers: only enabled if MSI-X is enabled. */
61 /* A 16-bit vector for configuration changes. */
62 #define VIRTIO_MSI_CONFIG_VECTOR 20
63 /* A 16-bit vector for selected queue notifications. */
64 #define VIRTIO_MSI_QUEUE_VECTOR 22
66 /* Config space size */
67 #define VIRTIO_PCI_CONFIG_NOMSI 20
68 #define VIRTIO_PCI_CONFIG_MSI 24
69 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
70 VIRTIO_PCI_CONFIG_MSI : \
71 VIRTIO_PCI_CONFIG_NOMSI)
73 /* The remaining space is defined by each driver as the per-driver
74 * configuration space */
75 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
76 VIRTIO_PCI_CONFIG_MSI : \
77 VIRTIO_PCI_CONFIG_NOMSI)
79 /* How many bits to shift physical queue address written to QUEUE_PFN.
80 * 12 is historical, and due to x86 page size. */
81 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
83 /* Flags track per-device state like workarounds for quirks in older guests. */
84 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
86 /* Performance improves when virtqueue kick processing is decoupled from the
87 * vcpu thread using ioeventfd for some devices. */
88 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
89 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
91 /* QEMU doesn't strictly need write barriers since everything runs in
92 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
93 * KVM or if kqemu gets SMP support.
95 #define wmb() do { } while (0)
97 /* virtio device */
99 static void virtio_pci_notify(void *opaque, uint16_t vector)
101 VirtIOPCIProxy *proxy = opaque;
102 if (msix_enabled(&proxy->pci_dev))
103 msix_notify(&proxy->pci_dev, vector);
104 else
105 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
108 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
110 VirtIOPCIProxy *proxy = opaque;
111 pci_device_save(&proxy->pci_dev, f);
112 msix_save(&proxy->pci_dev, f);
113 if (msix_present(&proxy->pci_dev))
114 qemu_put_be16(f, proxy->vdev->config_vector);
117 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
119 VirtIOPCIProxy *proxy = opaque;
120 if (msix_present(&proxy->pci_dev))
121 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
124 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
126 VirtIOPCIProxy *proxy = opaque;
127 int ret;
128 ret = pci_device_load(&proxy->pci_dev, f);
129 if (ret) {
130 return ret;
132 msix_load(&proxy->pci_dev, f);
133 if (msix_present(&proxy->pci_dev)) {
134 qemu_get_be16s(f, &proxy->vdev->config_vector);
135 } else {
136 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
138 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
139 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
141 return 0;
144 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
146 VirtIOPCIProxy *proxy = opaque;
147 uint16_t vector;
148 if (msix_present(&proxy->pci_dev)) {
149 qemu_get_be16s(f, &vector);
150 } else {
151 vector = VIRTIO_NO_VECTOR;
153 virtio_queue_set_vector(proxy->vdev, n, vector);
154 if (vector != VIRTIO_NO_VECTOR) {
155 return msix_vector_use(&proxy->pci_dev, vector);
157 return 0;
160 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
161 int n, bool assign)
163 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
164 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
165 int r = 0;
167 if (assign) {
168 r = event_notifier_init(notifier, 1);
169 if (r < 0) {
170 error_report("%s: unable to init event notifier: %d",
171 __func__, r);
172 return r;
174 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
175 true, n, event_notifier_get_fd(notifier));
176 } else {
177 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
178 true, n, event_notifier_get_fd(notifier));
179 /* Handle the race condition where the guest kicked and we deassigned
180 * before we got around to handling the kick.
182 if (event_notifier_test_and_clear(notifier)) {
183 virtio_queue_notify_vq(vq);
186 event_notifier_cleanup(notifier);
188 return r;
191 static void virtio_pci_host_notifier_read(void *opaque)
193 VirtQueue *vq = opaque;
194 EventNotifier *n = virtio_queue_get_host_notifier(vq);
195 if (event_notifier_test_and_clear(n)) {
196 virtio_queue_notify_vq(vq);
200 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
201 int n, bool assign)
203 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
204 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
205 if (assign) {
206 qemu_set_fd_handler(event_notifier_get_fd(notifier),
207 virtio_pci_host_notifier_read, NULL, vq);
208 } else {
209 qemu_set_fd_handler(event_notifier_get_fd(notifier),
210 NULL, NULL, NULL);
214 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
216 int n, r;
218 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
219 proxy->ioeventfd_disabled ||
220 proxy->ioeventfd_started) {
221 return;
224 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
225 if (!virtio_queue_get_num(proxy->vdev, n)) {
226 continue;
229 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
230 if (r < 0) {
231 goto assign_error;
234 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
236 proxy->ioeventfd_started = true;
237 return;
239 assign_error:
240 while (--n >= 0) {
241 if (!virtio_queue_get_num(proxy->vdev, n)) {
242 continue;
245 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
246 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
247 assert(r >= 0);
249 proxy->ioeventfd_started = false;
250 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
253 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
255 int r;
256 int n;
258 if (!proxy->ioeventfd_started) {
259 return;
262 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
263 if (!virtio_queue_get_num(proxy->vdev, n)) {
264 continue;
267 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
268 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
269 assert(r >= 0);
271 proxy->ioeventfd_started = false;
274 static void virtio_pci_reset(DeviceState *d)
276 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
277 virtio_pci_stop_ioeventfd(proxy);
278 virtio_reset(proxy->vdev);
279 msix_reset(&proxy->pci_dev);
280 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
283 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
285 VirtIOPCIProxy *proxy = opaque;
286 VirtIODevice *vdev = proxy->vdev;
287 target_phys_addr_t pa;
289 switch (addr) {
290 case VIRTIO_PCI_GUEST_FEATURES:
291 /* Guest does not negotiate properly? We have to assume nothing. */
292 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
293 if (vdev->bad_features)
294 val = proxy->host_features & vdev->bad_features(vdev);
295 else
296 val = 0;
298 if (vdev->set_features)
299 vdev->set_features(vdev, val);
300 vdev->guest_features = val;
301 break;
302 case VIRTIO_PCI_QUEUE_PFN:
303 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
304 if (pa == 0) {
305 virtio_pci_stop_ioeventfd(proxy);
306 virtio_reset(proxy->vdev);
307 msix_unuse_all_vectors(&proxy->pci_dev);
309 else
310 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
311 break;
312 case VIRTIO_PCI_QUEUE_SEL:
313 if (val < VIRTIO_PCI_QUEUE_MAX)
314 vdev->queue_sel = val;
315 break;
316 case VIRTIO_PCI_QUEUE_NOTIFY:
317 if (val < VIRTIO_PCI_QUEUE_MAX) {
318 virtio_queue_notify(vdev, val);
320 break;
321 case VIRTIO_PCI_STATUS:
322 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
323 virtio_pci_stop_ioeventfd(proxy);
326 virtio_set_status(vdev, val & 0xFF);
328 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
329 virtio_pci_start_ioeventfd(proxy);
332 if (vdev->status == 0) {
333 virtio_reset(proxy->vdev);
334 msix_unuse_all_vectors(&proxy->pci_dev);
337 /* Linux before 2.6.34 sets the device as OK without enabling
338 the PCI device bus master bit. In this case we need to disable
339 some safety checks. */
340 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
341 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
342 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
344 break;
345 case VIRTIO_MSI_CONFIG_VECTOR:
346 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
347 /* Make it possible for guest to discover an error took place. */
348 if (msix_vector_use(&proxy->pci_dev, val) < 0)
349 val = VIRTIO_NO_VECTOR;
350 vdev->config_vector = val;
351 break;
352 case VIRTIO_MSI_QUEUE_VECTOR:
353 msix_vector_unuse(&proxy->pci_dev,
354 virtio_queue_vector(vdev, vdev->queue_sel));
355 /* Make it possible for guest to discover an error took place. */
356 if (msix_vector_use(&proxy->pci_dev, val) < 0)
357 val = VIRTIO_NO_VECTOR;
358 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
359 break;
360 default:
361 error_report("%s: unexpected address 0x%x value 0x%x",
362 __func__, addr, val);
363 break;
367 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
369 VirtIODevice *vdev = proxy->vdev;
370 uint32_t ret = 0xFFFFFFFF;
372 switch (addr) {
373 case VIRTIO_PCI_HOST_FEATURES:
374 ret = proxy->host_features;
375 break;
376 case VIRTIO_PCI_GUEST_FEATURES:
377 ret = vdev->guest_features;
378 break;
379 case VIRTIO_PCI_QUEUE_PFN:
380 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
381 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
382 break;
383 case VIRTIO_PCI_QUEUE_NUM:
384 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
385 break;
386 case VIRTIO_PCI_QUEUE_SEL:
387 ret = vdev->queue_sel;
388 break;
389 case VIRTIO_PCI_STATUS:
390 ret = vdev->status;
391 break;
392 case VIRTIO_PCI_ISR:
393 /* reading from the ISR also clears it. */
394 ret = vdev->isr;
395 vdev->isr = 0;
396 qemu_set_irq(proxy->pci_dev.irq[0], 0);
397 break;
398 case VIRTIO_MSI_CONFIG_VECTOR:
399 ret = vdev->config_vector;
400 break;
401 case VIRTIO_MSI_QUEUE_VECTOR:
402 ret = virtio_queue_vector(vdev, vdev->queue_sel);
403 break;
404 default:
405 break;
408 return ret;
411 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
413 VirtIOPCIProxy *proxy = opaque;
414 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
415 if (addr < config)
416 return virtio_ioport_read(proxy, addr);
417 addr -= config;
418 return virtio_config_readb(proxy->vdev, addr);
421 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
423 VirtIOPCIProxy *proxy = opaque;
424 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
425 if (addr < config)
426 return virtio_ioport_read(proxy, addr);
427 addr -= config;
428 return virtio_config_readw(proxy->vdev, addr);
431 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
433 VirtIOPCIProxy *proxy = opaque;
434 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
435 if (addr < config)
436 return virtio_ioport_read(proxy, addr);
437 addr -= config;
438 return virtio_config_readl(proxy->vdev, addr);
441 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
443 VirtIOPCIProxy *proxy = opaque;
444 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
445 if (addr < config) {
446 virtio_ioport_write(proxy, addr, val);
447 return;
449 addr -= config;
450 virtio_config_writeb(proxy->vdev, addr, val);
453 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
455 VirtIOPCIProxy *proxy = opaque;
456 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
457 if (addr < config) {
458 virtio_ioport_write(proxy, addr, val);
459 return;
461 addr -= config;
462 virtio_config_writew(proxy->vdev, addr, val);
465 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
467 VirtIOPCIProxy *proxy = opaque;
468 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
469 if (addr < config) {
470 virtio_ioport_write(proxy, addr, val);
471 return;
473 addr -= config;
474 virtio_config_writel(proxy->vdev, addr, val);
477 const MemoryRegionPortio virtio_portio[] = {
478 { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
479 { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
480 { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
481 { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
482 { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
483 { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
484 PORTIO_END_OF_LIST()
487 static const MemoryRegionOps virtio_pci_config_ops = {
488 .old_portio = virtio_portio,
489 .endianness = DEVICE_LITTLE_ENDIAN,
492 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
493 uint32_t val, int len)
495 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
497 pci_default_write_config(pci_dev, address, val, len);
499 if (range_covers_byte(address, len, PCI_COMMAND) &&
500 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
501 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
502 virtio_pci_stop_ioeventfd(proxy);
503 virtio_set_status(proxy->vdev,
504 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
507 msix_write_config(pci_dev, address, val, len);
510 static unsigned virtio_pci_get_features(void *opaque)
512 VirtIOPCIProxy *proxy = opaque;
513 return proxy->host_features;
516 static void virtio_pci_guest_notifier_read(void *opaque)
518 VirtQueue *vq = opaque;
519 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
520 if (event_notifier_test_and_clear(n)) {
521 virtio_irq(vq);
525 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
527 VirtIOPCIProxy *proxy = opaque;
528 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
529 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
531 if (assign) {
532 int r = event_notifier_init(notifier, 0);
533 if (r < 0) {
534 return r;
536 qemu_set_fd_handler(event_notifier_get_fd(notifier),
537 virtio_pci_guest_notifier_read, NULL, vq);
538 } else {
539 qemu_set_fd_handler(event_notifier_get_fd(notifier),
540 NULL, NULL, NULL);
541 event_notifier_cleanup(notifier);
544 return 0;
547 static bool virtio_pci_query_guest_notifiers(void *opaque)
549 VirtIOPCIProxy *proxy = opaque;
550 return msix_enabled(&proxy->pci_dev);
553 static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
555 VirtIOPCIProxy *proxy = opaque;
556 VirtIODevice *vdev = proxy->vdev;
557 int r, n;
559 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
560 if (!virtio_queue_get_num(vdev, n)) {
561 break;
564 r = virtio_pci_set_guest_notifier(opaque, n, assign);
565 if (r < 0) {
566 goto assign_error;
570 return 0;
572 assign_error:
573 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
574 while (--n >= 0) {
575 virtio_pci_set_guest_notifier(opaque, n, !assign);
577 return r;
580 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
582 VirtIOPCIProxy *proxy = opaque;
584 /* Stop using ioeventfd for virtqueue kick if the device starts using host
585 * notifiers. This makes it easy to avoid stepping on each others' toes.
587 proxy->ioeventfd_disabled = assign;
588 if (assign) {
589 virtio_pci_stop_ioeventfd(proxy);
591 /* We don't need to start here: it's not needed because backend
592 * currently only stops on status change away from ok,
593 * reset, vmstop and such. If we do add code to start here,
594 * need to check vmstate, device state etc. */
595 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
598 static void virtio_pci_vmstate_change(void *opaque, bool running)
600 VirtIOPCIProxy *proxy = opaque;
602 if (running) {
603 /* Try to find out if the guest has bus master disabled, but is
604 in ready state. Then we have a buggy guest OS. */
605 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
606 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
607 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
609 virtio_pci_start_ioeventfd(proxy);
610 } else {
611 virtio_pci_stop_ioeventfd(proxy);
615 static const VirtIOBindings virtio_pci_bindings = {
616 .notify = virtio_pci_notify,
617 .save_config = virtio_pci_save_config,
618 .load_config = virtio_pci_load_config,
619 .save_queue = virtio_pci_save_queue,
620 .load_queue = virtio_pci_load_queue,
621 .get_features = virtio_pci_get_features,
622 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
623 .set_host_notifier = virtio_pci_set_host_notifier,
624 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
625 .vmstate_change = virtio_pci_vmstate_change,
628 void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
630 uint8_t *config;
631 uint32_t size;
633 proxy->vdev = vdev;
635 config = proxy->pci_dev.config;
637 if (proxy->class_code) {
638 pci_config_set_class(config, proxy->class_code);
640 pci_set_word(config + 0x2c, pci_get_word(config + PCI_VENDOR_ID));
641 pci_set_word(config + 0x2e, vdev->device_id);
642 config[0x3d] = 1;
644 memory_region_init(&proxy->msix_bar, "virtio-msix", 4096);
645 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors,
646 &proxy->msix_bar, 1, 0)) {
647 pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
648 &proxy->msix_bar);
649 } else
650 vdev->nvectors = 0;
652 proxy->pci_dev.config_write = virtio_write_config;
654 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
655 if (size & (size-1))
656 size = 1 << qemu_fls(size);
658 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
659 "virtio-pci", size);
660 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
661 &proxy->bar);
663 if (!kvm_has_many_ioeventfds()) {
664 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
667 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
668 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
669 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
670 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
673 static int virtio_blk_init_pci(PCIDevice *pci_dev)
675 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
676 VirtIODevice *vdev;
678 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
679 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
680 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
682 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
683 &proxy->block_serial);
684 if (!vdev) {
685 return -1;
687 vdev->nvectors = proxy->nvectors;
688 virtio_init_pci(proxy, vdev);
689 /* make the actual value visible */
690 proxy->nvectors = vdev->nvectors;
691 return 0;
694 static int virtio_exit_pci(PCIDevice *pci_dev)
696 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
697 int r;
699 memory_region_destroy(&proxy->bar);
700 r = msix_uninit(pci_dev, &proxy->msix_bar);
701 memory_region_destroy(&proxy->msix_bar);
702 return r;
705 static int virtio_blk_exit_pci(PCIDevice *pci_dev)
707 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
709 virtio_pci_stop_ioeventfd(proxy);
710 virtio_blk_exit(proxy->vdev);
711 blockdev_mark_auto_del(proxy->block.bs);
712 return virtio_exit_pci(pci_dev);
715 static int virtio_serial_init_pci(PCIDevice *pci_dev)
717 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
718 VirtIODevice *vdev;
720 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
721 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
722 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
723 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
725 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
726 if (!vdev) {
727 return -1;
729 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
730 ? proxy->serial.max_virtserial_ports + 1
731 : proxy->nvectors;
732 virtio_init_pci(proxy, vdev);
733 proxy->nvectors = vdev->nvectors;
734 return 0;
737 static int virtio_serial_exit_pci(PCIDevice *pci_dev)
739 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
741 virtio_pci_stop_ioeventfd(proxy);
742 virtio_serial_exit(proxy->vdev);
743 return virtio_exit_pci(pci_dev);
746 static int virtio_net_init_pci(PCIDevice *pci_dev)
748 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
749 VirtIODevice *vdev;
751 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
753 vdev->nvectors = proxy->nvectors;
754 virtio_init_pci(proxy, vdev);
756 /* make the actual value visible */
757 proxy->nvectors = vdev->nvectors;
758 return 0;
761 static int virtio_net_exit_pci(PCIDevice *pci_dev)
763 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
765 virtio_pci_stop_ioeventfd(proxy);
766 virtio_net_exit(proxy->vdev);
767 return virtio_exit_pci(pci_dev);
770 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
772 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
773 VirtIODevice *vdev;
775 vdev = virtio_balloon_init(&pci_dev->qdev);
776 if (!vdev) {
777 return -1;
779 virtio_init_pci(proxy, vdev);
780 return 0;
783 static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
785 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
787 virtio_pci_stop_ioeventfd(proxy);
788 virtio_balloon_exit(proxy->vdev);
789 return virtio_exit_pci(pci_dev);
792 static PCIDeviceInfo virtio_info[] = {
794 .qdev.name = "virtio-blk-pci",
795 .qdev.alias = "virtio-blk",
796 .qdev.size = sizeof(VirtIOPCIProxy),
797 .init = virtio_blk_init_pci,
798 .exit = virtio_blk_exit_pci,
799 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
800 .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK,
801 .revision = VIRTIO_PCI_ABI_VERSION,
802 .class_id = PCI_CLASS_STORAGE_SCSI,
803 .qdev.props = (Property[]) {
804 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
805 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
806 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
807 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
808 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
809 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
810 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
811 DEFINE_PROP_END_OF_LIST(),
813 .qdev.reset = virtio_pci_reset,
815 .qdev.name = "virtio-net-pci",
816 .qdev.alias = "virtio-net",
817 .qdev.size = sizeof(VirtIOPCIProxy),
818 .init = virtio_net_init_pci,
819 .exit = virtio_net_exit_pci,
820 .romfile = "pxe-virtio.rom",
821 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
822 .device_id = PCI_DEVICE_ID_VIRTIO_NET,
823 .revision = VIRTIO_PCI_ABI_VERSION,
824 .class_id = PCI_CLASS_NETWORK_ETHERNET,
825 .qdev.props = (Property[]) {
826 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
827 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
828 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
829 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
830 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
831 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
832 net.txtimer, TX_TIMER_INTERVAL),
833 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
834 net.txburst, TX_BURST),
835 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
836 DEFINE_PROP_END_OF_LIST(),
838 .qdev.reset = virtio_pci_reset,
840 .qdev.name = "virtio-serial-pci",
841 .qdev.alias = "virtio-serial",
842 .qdev.size = sizeof(VirtIOPCIProxy),
843 .init = virtio_serial_init_pci,
844 .exit = virtio_serial_exit_pci,
845 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
846 .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
847 .revision = VIRTIO_PCI_ABI_VERSION,
848 .class_id = PCI_CLASS_COMMUNICATION_OTHER,
849 .qdev.props = (Property[]) {
850 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
851 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
852 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
853 DEV_NVECTORS_UNSPECIFIED),
854 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
855 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
856 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
857 serial.max_virtserial_ports, 31),
858 DEFINE_PROP_END_OF_LIST(),
860 .qdev.reset = virtio_pci_reset,
862 .qdev.name = "virtio-balloon-pci",
863 .qdev.alias = "virtio-balloon",
864 .qdev.size = sizeof(VirtIOPCIProxy),
865 .init = virtio_balloon_init_pci,
866 .exit = virtio_balloon_exit_pci,
867 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
868 .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON,
869 .revision = VIRTIO_PCI_ABI_VERSION,
870 .class_id = PCI_CLASS_MEMORY_RAM,
871 .qdev.props = (Property[]) {
872 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
873 DEFINE_PROP_END_OF_LIST(),
875 .qdev.reset = virtio_pci_reset,
877 /* end of list */
881 static void virtio_pci_register_devices(void)
883 pci_qdev_register_many(virtio_info);
886 device_init(virtio_pci_register_devices)