Only call kvm_set_irqfd() if CONFIG_KVM is defined
[qemu/qemu-dev-zwu.git] / hw / virtio-pci.c
blob938937e51320160ad5e806a1e421000f51cd4846
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 "pci.h"
22 #include "qemu-error.h"
23 #include "msix.h"
24 #include "net.h"
25 #include "loader.h"
26 #include "kvm.h"
28 /* from Linux's linux/virtio_pci.h */
30 /* A 32-bit r/o bitmask of the features supported by the host */
31 #define VIRTIO_PCI_HOST_FEATURES 0
33 /* A 32-bit r/w bitmask of features activated by the guest */
34 #define VIRTIO_PCI_GUEST_FEATURES 4
36 /* A 32-bit r/w PFN for the currently selected queue */
37 #define VIRTIO_PCI_QUEUE_PFN 8
39 /* A 16-bit r/o queue size for the currently selected queue */
40 #define VIRTIO_PCI_QUEUE_NUM 12
42 /* A 16-bit r/w queue selector */
43 #define VIRTIO_PCI_QUEUE_SEL 14
45 /* A 16-bit r/w queue notifier */
46 #define VIRTIO_PCI_QUEUE_NOTIFY 16
48 /* An 8-bit device status register. */
49 #define VIRTIO_PCI_STATUS 18
51 /* An 8-bit r/o interrupt status register. Reading the value will return the
52 * current contents of the ISR and will also clear it. This is effectively
53 * a read-and-acknowledge. */
54 #define VIRTIO_PCI_ISR 19
56 /* MSI-X registers: only enabled if MSI-X is enabled. */
57 /* A 16-bit vector for configuration changes. */
58 #define VIRTIO_MSI_CONFIG_VECTOR 20
59 /* A 16-bit vector for selected queue notifications. */
60 #define VIRTIO_MSI_QUEUE_VECTOR 22
62 /* Config space size */
63 #define VIRTIO_PCI_CONFIG_NOMSI 20
64 #define VIRTIO_PCI_CONFIG_MSI 24
65 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
66 VIRTIO_PCI_CONFIG_MSI : \
67 VIRTIO_PCI_CONFIG_NOMSI)
69 /* The remaining space is defined by each driver as the per-driver
70 * configuration space */
71 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
72 VIRTIO_PCI_CONFIG_MSI : \
73 VIRTIO_PCI_CONFIG_NOMSI)
75 /* Virtio ABI version, if we increment this, we break the guest driver. */
76 #define VIRTIO_PCI_ABI_VERSION 0
78 /* How many bits to shift physical queue address written to QUEUE_PFN.
79 * 12 is historical, and due to x86 page size. */
80 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
82 /* We can catch some guest bugs inside here so we continue supporting older
83 guests. */
84 #define VIRTIO_PCI_BUG_BUS_MASTER (1 << 0)
86 /* QEMU doesn't strictly need write barriers since everything runs in
87 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
88 * KVM or if kqemu gets SMP support.
90 #define wmb() do { } while (0)
92 /* PCI bindings. */
94 typedef struct {
95 PCIDevice pci_dev;
96 VirtIODevice *vdev;
97 uint32_t bugs;
98 uint32_t addr;
99 uint32_t class_code;
100 uint32_t nvectors;
101 BlockConf block;
102 NICConf nic;
103 uint32_t host_features;
104 #ifdef CONFIG_LINUX
105 V9fsConf fsconf;
106 #endif
107 /* Max. number of ports we can have for a the virtio-serial device */
108 uint32_t max_virtserial_ports;
109 } VirtIOPCIProxy;
111 /* virtio device */
113 static void virtio_pci_notify(void *opaque, uint16_t vector)
115 VirtIOPCIProxy *proxy = opaque;
116 if (msix_enabled(&proxy->pci_dev))
117 msix_notify(&proxy->pci_dev, vector);
118 else
119 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
122 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
124 VirtIOPCIProxy *proxy = opaque;
125 pci_device_save(&proxy->pci_dev, f);
126 msix_save(&proxy->pci_dev, f);
127 if (msix_present(&proxy->pci_dev))
128 qemu_put_be16(f, proxy->vdev->config_vector);
131 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
133 VirtIOPCIProxy *proxy = opaque;
134 if (msix_present(&proxy->pci_dev))
135 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
138 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
140 VirtIOPCIProxy *proxy = opaque;
141 int ret;
142 ret = pci_device_load(&proxy->pci_dev, f);
143 if (ret) {
144 return ret;
146 msix_load(&proxy->pci_dev, f);
147 if (msix_present(&proxy->pci_dev)) {
148 qemu_get_be16s(f, &proxy->vdev->config_vector);
149 } else {
150 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
152 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
153 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
156 /* Try to find out if the guest has bus master disabled, but is
157 in ready state. Then we have a buggy guest OS. */
158 if (!(proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
159 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
160 proxy->bugs |= VIRTIO_PCI_BUG_BUS_MASTER;
162 return 0;
165 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
167 VirtIOPCIProxy *proxy = opaque;
168 uint16_t vector;
169 if (msix_present(&proxy->pci_dev)) {
170 qemu_get_be16s(f, &vector);
171 } else {
172 vector = VIRTIO_NO_VECTOR;
174 virtio_queue_set_vector(proxy->vdev, n, vector);
175 if (vector != VIRTIO_NO_VECTOR) {
176 return msix_vector_use(&proxy->pci_dev, vector);
178 return 0;
181 static void virtio_pci_reset(DeviceState *d)
183 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
184 virtio_reset(proxy->vdev);
185 msix_reset(&proxy->pci_dev);
186 proxy->bugs = 0;
189 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
191 VirtIOPCIProxy *proxy = opaque;
192 VirtIODevice *vdev = proxy->vdev;
193 target_phys_addr_t pa;
195 switch (addr) {
196 case VIRTIO_PCI_GUEST_FEATURES:
197 /* Guest does not negotiate properly? We have to assume nothing. */
198 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
199 if (vdev->bad_features)
200 val = proxy->host_features & vdev->bad_features(vdev);
201 else
202 val = 0;
204 if (vdev->set_features)
205 vdev->set_features(vdev, val);
206 vdev->guest_features = val;
207 break;
208 case VIRTIO_PCI_QUEUE_PFN:
209 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
210 if (pa == 0) {
211 virtio_reset(proxy->vdev);
212 msix_unuse_all_vectors(&proxy->pci_dev);
214 else
215 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
216 break;
217 case VIRTIO_PCI_QUEUE_SEL:
218 if (val < VIRTIO_PCI_QUEUE_MAX)
219 vdev->queue_sel = val;
220 break;
221 case VIRTIO_PCI_QUEUE_NOTIFY:
222 virtio_queue_notify(vdev, val);
223 break;
224 case VIRTIO_PCI_STATUS:
225 virtio_set_status(vdev, val & 0xFF);
226 if (vdev->status == 0) {
227 virtio_reset(proxy->vdev);
228 msix_unuse_all_vectors(&proxy->pci_dev);
231 /* Linux before 2.6.34 sets the device as OK without enabling
232 the PCI device bus master bit. In this case we need to disable
233 some safety checks. */
234 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
235 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
236 proxy->bugs |= VIRTIO_PCI_BUG_BUS_MASTER;
238 break;
239 case VIRTIO_MSI_CONFIG_VECTOR:
240 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
241 /* Make it possible for guest to discover an error took place. */
242 if (msix_vector_use(&proxy->pci_dev, val) < 0)
243 val = VIRTIO_NO_VECTOR;
244 vdev->config_vector = val;
245 break;
246 case VIRTIO_MSI_QUEUE_VECTOR:
247 msix_vector_unuse(&proxy->pci_dev,
248 virtio_queue_vector(vdev, vdev->queue_sel));
249 /* Make it possible for guest to discover an error took place. */
250 if (msix_vector_use(&proxy->pci_dev, val) < 0)
251 val = VIRTIO_NO_VECTOR;
252 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
253 break;
254 default:
255 fprintf(stderr, "%s: unexpected address 0x%x value 0x%x\n",
256 __func__, addr, val);
257 break;
261 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
263 VirtIODevice *vdev = proxy->vdev;
264 uint32_t ret = 0xFFFFFFFF;
266 switch (addr) {
267 case VIRTIO_PCI_HOST_FEATURES:
268 ret = proxy->host_features;
269 break;
270 case VIRTIO_PCI_GUEST_FEATURES:
271 ret = vdev->guest_features;
272 break;
273 case VIRTIO_PCI_QUEUE_PFN:
274 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
275 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
276 break;
277 case VIRTIO_PCI_QUEUE_NUM:
278 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
279 break;
280 case VIRTIO_PCI_QUEUE_SEL:
281 ret = vdev->queue_sel;
282 break;
283 case VIRTIO_PCI_STATUS:
284 ret = vdev->status;
285 break;
286 case VIRTIO_PCI_ISR:
287 /* reading from the ISR also clears it. */
288 ret = vdev->isr;
289 vdev->isr = 0;
290 qemu_set_irq(proxy->pci_dev.irq[0], 0);
291 break;
292 case VIRTIO_MSI_CONFIG_VECTOR:
293 ret = vdev->config_vector;
294 break;
295 case VIRTIO_MSI_QUEUE_VECTOR:
296 ret = virtio_queue_vector(vdev, vdev->queue_sel);
297 break;
298 default:
299 break;
302 return ret;
305 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
307 VirtIOPCIProxy *proxy = opaque;
308 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
309 addr -= proxy->addr;
310 if (addr < config)
311 return virtio_ioport_read(proxy, addr);
312 addr -= config;
313 return virtio_config_readb(proxy->vdev, addr);
316 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
318 VirtIOPCIProxy *proxy = opaque;
319 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
320 addr -= proxy->addr;
321 if (addr < config)
322 return virtio_ioport_read(proxy, addr);
323 addr -= config;
324 return virtio_config_readw(proxy->vdev, addr);
327 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
329 VirtIOPCIProxy *proxy = opaque;
330 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
331 addr -= proxy->addr;
332 if (addr < config)
333 return virtio_ioport_read(proxy, addr);
334 addr -= config;
335 return virtio_config_readl(proxy->vdev, addr);
338 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
340 VirtIOPCIProxy *proxy = opaque;
341 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
342 addr -= proxy->addr;
343 if (addr < config) {
344 virtio_ioport_write(proxy, addr, val);
345 return;
347 addr -= config;
348 virtio_config_writeb(proxy->vdev, addr, val);
351 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
353 VirtIOPCIProxy *proxy = opaque;
354 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
355 addr -= proxy->addr;
356 if (addr < config) {
357 virtio_ioport_write(proxy, addr, val);
358 return;
360 addr -= config;
361 virtio_config_writew(proxy->vdev, addr, val);
364 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
366 VirtIOPCIProxy *proxy = opaque;
367 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
368 addr -= proxy->addr;
369 if (addr < config) {
370 virtio_ioport_write(proxy, addr, val);
371 return;
373 addr -= config;
374 virtio_config_writel(proxy->vdev, addr, val);
377 static void virtio_map(PCIDevice *pci_dev, int region_num,
378 pcibus_t addr, pcibus_t size, int type)
380 VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
381 VirtIODevice *vdev = proxy->vdev;
382 unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
384 proxy->addr = addr;
386 register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
387 register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
388 register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
389 register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
390 register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
391 register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
393 if (vdev->config_len)
394 vdev->get_config(vdev, vdev->config);
397 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
398 uint32_t val, int len)
400 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
402 if (PCI_COMMAND == address) {
403 if (!(val & PCI_COMMAND_MASTER)) {
404 if (!(proxy->bugs & VIRTIO_PCI_BUG_BUS_MASTER)) {
405 virtio_set_status(proxy->vdev,
406 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
411 pci_default_write_config(pci_dev, address, val, len);
412 msix_write_config(pci_dev, address, val, len);
415 static unsigned virtio_pci_get_features(void *opaque)
417 VirtIOPCIProxy *proxy = opaque;
418 return proxy->host_features;
421 static void virtio_pci_guest_notifier_read(void *opaque)
423 VirtQueue *vq = opaque;
424 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
425 if (event_notifier_test_and_clear(n)) {
426 virtio_irq(vq);
430 static int virtio_pci_mask_notifier(PCIDevice *dev, unsigned vector,
431 void *opaque, int masked)
433 #ifdef CONFIG_KVM
434 VirtQueue *vq = opaque;
435 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
436 int r = kvm_set_irqfd(dev->msix_irq_entries[vector].gsi,
437 event_notifier_get_fd(notifier),
438 !masked);
439 if (r < 0) {
440 return (r == -ENOSYS) ? 0 : r;
442 if (masked) {
443 qemu_set_fd_handler(event_notifier_get_fd(notifier),
444 virtio_pci_guest_notifier_read, NULL, vq);
445 } else {
446 qemu_set_fd_handler(event_notifier_get_fd(notifier),
447 NULL, NULL, NULL);
449 return 0;
450 #else
451 return -ENOSYS;
452 #endif
455 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
457 VirtIOPCIProxy *proxy = opaque;
458 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
459 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
461 if (assign) {
462 int r = event_notifier_init(notifier, 0);
463 if (r < 0) {
464 return r;
466 qemu_set_fd_handler(event_notifier_get_fd(notifier),
467 virtio_pci_guest_notifier_read, NULL, vq);
468 msix_set_mask_notifier(&proxy->pci_dev,
469 virtio_queue_vector(proxy->vdev, n), vq);
470 } else {
471 msix_unset_mask_notifier(&proxy->pci_dev,
472 virtio_queue_vector(proxy->vdev, n));
473 qemu_set_fd_handler(event_notifier_get_fd(notifier),
474 NULL, NULL, NULL);
475 /* Test and clear notifier before closing it,
476 * in case poll callback didn't have time to run. */
477 virtio_pci_guest_notifier_read(vq);
478 event_notifier_cleanup(notifier);
481 return 0;
484 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
486 VirtIOPCIProxy *proxy = opaque;
487 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
488 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
489 int r;
490 if (assign) {
491 r = event_notifier_init(notifier, 1);
492 if (r < 0) {
493 return r;
495 r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
496 proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
497 n, assign);
498 if (r < 0) {
499 event_notifier_cleanup(notifier);
501 } else {
502 r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
503 proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
504 n, assign);
505 if (r < 0) {
506 return r;
508 event_notifier_cleanup(notifier);
510 return r;
513 static const VirtIOBindings virtio_pci_bindings = {
514 .notify = virtio_pci_notify,
515 .save_config = virtio_pci_save_config,
516 .load_config = virtio_pci_load_config,
517 .save_queue = virtio_pci_save_queue,
518 .load_queue = virtio_pci_load_queue,
519 .get_features = virtio_pci_get_features,
520 .set_host_notifier = virtio_pci_set_host_notifier,
521 .set_guest_notifier = virtio_pci_set_guest_notifier,
524 static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
525 uint16_t vendor, uint16_t device,
526 uint16_t class_code, uint8_t pif)
528 uint8_t *config;
529 uint32_t size;
531 proxy->vdev = vdev;
533 config = proxy->pci_dev.config;
534 pci_config_set_vendor_id(config, vendor);
535 pci_config_set_device_id(config, device);
537 config[0x08] = VIRTIO_PCI_ABI_VERSION;
539 config[0x09] = pif;
540 pci_config_set_class(config, class_code);
541 config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;
543 config[0x2c] = vendor & 0xFF;
544 config[0x2d] = (vendor >> 8) & 0xFF;
545 config[0x2e] = vdev->device_id & 0xFF;
546 config[0x2f] = (vdev->device_id >> 8) & 0xFF;
548 config[0x3d] = 1;
550 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
551 pci_register_bar(&proxy->pci_dev, 1,
552 msix_bar_size(&proxy->pci_dev),
553 PCI_BASE_ADDRESS_SPACE_MEMORY,
554 msix_mmio_map);
555 } else
556 vdev->nvectors = 0;
558 proxy->pci_dev.config_write = virtio_write_config;
560 proxy->pci_dev.msix_mask_notifier = virtio_pci_mask_notifier;
562 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
563 if (size & (size-1))
564 size = 1 << qemu_fls(size);
566 pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
567 virtio_map);
569 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
570 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
571 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
572 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
575 static int virtio_blk_init_pci(PCIDevice *pci_dev)
577 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
578 VirtIODevice *vdev;
580 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
581 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
582 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
584 if (!proxy->block.dinfo) {
585 error_report("virtio-blk-pci: drive property not set");
586 return -1;
588 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block);
589 vdev->nvectors = proxy->nvectors;
590 virtio_init_pci(proxy, vdev,
591 PCI_VENDOR_ID_REDHAT_QUMRANET,
592 PCI_DEVICE_ID_VIRTIO_BLOCK,
593 proxy->class_code, 0x00);
594 /* make the actual value visible */
595 proxy->nvectors = vdev->nvectors;
596 return 0;
599 static int virtio_exit_pci(PCIDevice *pci_dev)
601 return msix_uninit(pci_dev);
604 static int virtio_blk_exit_pci(PCIDevice *pci_dev)
606 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
608 drive_uninit(proxy->block.dinfo);
609 return virtio_exit_pci(pci_dev);
612 static int virtio_serial_init_pci(PCIDevice *pci_dev)
614 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
615 VirtIODevice *vdev;
617 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
618 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
619 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
620 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
622 vdev = virtio_serial_init(&pci_dev->qdev, proxy->max_virtserial_ports);
623 if (!vdev) {
624 return -1;
626 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
627 ? proxy->max_virtserial_ports + 1
628 : proxy->nvectors;
629 virtio_init_pci(proxy, vdev,
630 PCI_VENDOR_ID_REDHAT_QUMRANET,
631 PCI_DEVICE_ID_VIRTIO_CONSOLE,
632 proxy->class_code, 0x00);
633 proxy->nvectors = vdev->nvectors;
634 return 0;
637 static int virtio_net_init_pci(PCIDevice *pci_dev)
639 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
640 VirtIODevice *vdev;
642 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic);
644 vdev->nvectors = proxy->nvectors;
645 virtio_init_pci(proxy, vdev,
646 PCI_VENDOR_ID_REDHAT_QUMRANET,
647 PCI_DEVICE_ID_VIRTIO_NET,
648 PCI_CLASS_NETWORK_ETHERNET,
649 0x00);
651 /* make the actual value visible */
652 proxy->nvectors = vdev->nvectors;
653 return 0;
656 static int virtio_net_exit_pci(PCIDevice *pci_dev)
658 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
660 virtio_net_exit(proxy->vdev);
661 return virtio_exit_pci(pci_dev);
664 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
666 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
667 VirtIODevice *vdev;
669 vdev = virtio_balloon_init(&pci_dev->qdev);
670 virtio_init_pci(proxy, vdev,
671 PCI_VENDOR_ID_REDHAT_QUMRANET,
672 PCI_DEVICE_ID_VIRTIO_BALLOON,
673 PCI_CLASS_MEMORY_RAM,
674 0x00);
675 return 0;
678 #ifdef CONFIG_LINUX
679 static int virtio_9p_init_pci(PCIDevice *pci_dev)
681 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
682 VirtIODevice *vdev;
684 vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf);
685 virtio_init_pci(proxy, vdev,
686 PCI_VENDOR_ID_REDHAT_QUMRANET,
687 0x1009,
688 0x2,
689 0x00);
691 return 0;
693 #endif
695 static PCIDeviceInfo virtio_info[] = {
697 .qdev.name = "virtio-blk-pci",
698 .qdev.size = sizeof(VirtIOPCIProxy),
699 .init = virtio_blk_init_pci,
700 .exit = virtio_blk_exit_pci,
701 .qdev.props = (Property[]) {
702 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
703 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
704 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
705 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
706 DEFINE_PROP_END_OF_LIST(),
708 .qdev.reset = virtio_pci_reset,
710 .qdev.name = "virtio-net-pci",
711 .qdev.size = sizeof(VirtIOPCIProxy),
712 .init = virtio_net_init_pci,
713 .exit = virtio_net_exit_pci,
714 .romfile = "pxe-virtio.bin",
715 .qdev.props = (Property[]) {
716 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
717 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
718 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
719 DEFINE_PROP_END_OF_LIST(),
721 .qdev.reset = virtio_pci_reset,
723 .qdev.name = "virtio-serial-pci",
724 .qdev.alias = "virtio-serial",
725 .qdev.size = sizeof(VirtIOPCIProxy),
726 .init = virtio_serial_init_pci,
727 .exit = virtio_exit_pci,
728 .qdev.props = (Property[]) {
729 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
730 DEV_NVECTORS_UNSPECIFIED),
731 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
732 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
733 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, max_virtserial_ports,
734 31),
735 DEFINE_PROP_END_OF_LIST(),
737 .qdev.reset = virtio_pci_reset,
739 .qdev.name = "virtio-balloon-pci",
740 .qdev.size = sizeof(VirtIOPCIProxy),
741 .init = virtio_balloon_init_pci,
742 .exit = virtio_exit_pci,
743 .qdev.props = (Property[]) {
744 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
745 DEFINE_PROP_END_OF_LIST(),
747 .qdev.reset = virtio_pci_reset,
749 #ifdef CONFIG_LINUX
750 .qdev.name = "virtio-9p-pci",
751 .qdev.size = sizeof(VirtIOPCIProxy),
752 .init = virtio_9p_init_pci,
753 .qdev.props = (Property[]) {
754 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
755 DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
756 DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
757 DEFINE_PROP_END_OF_LIST(),
759 }, {
760 #endif
761 /* end of list */
765 static void virtio_pci_register_devices(void)
767 pci_qdev_register_many(virtio_info);
770 device_init(virtio_pci_register_devices)