virtio-9p: Add fidtype so that we can do type specific operation
[qemu/stefanha.git] / hw / virtio-pci.c
blob6e8f88a141109d62adb6b20db73c3372ffe97bd7
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"
27 #include "blockdev.h"
29 /* from Linux's linux/virtio_pci.h */
31 /* A 32-bit r/o bitmask of the features supported by the host */
32 #define VIRTIO_PCI_HOST_FEATURES 0
34 /* A 32-bit r/w bitmask of features activated by the guest */
35 #define VIRTIO_PCI_GUEST_FEATURES 4
37 /* A 32-bit r/w PFN for the currently selected queue */
38 #define VIRTIO_PCI_QUEUE_PFN 8
40 /* A 16-bit r/o queue size for the currently selected queue */
41 #define VIRTIO_PCI_QUEUE_NUM 12
43 /* A 16-bit r/w queue selector */
44 #define VIRTIO_PCI_QUEUE_SEL 14
46 /* A 16-bit r/w queue notifier */
47 #define VIRTIO_PCI_QUEUE_NOTIFY 16
49 /* An 8-bit device status register. */
50 #define VIRTIO_PCI_STATUS 18
52 /* An 8-bit r/o interrupt status register. Reading the value will return the
53 * current contents of the ISR and will also clear it. This is effectively
54 * a read-and-acknowledge. */
55 #define VIRTIO_PCI_ISR 19
57 /* MSI-X registers: only enabled if MSI-X is enabled. */
58 /* A 16-bit vector for configuration changes. */
59 #define VIRTIO_MSI_CONFIG_VECTOR 20
60 /* A 16-bit vector for selected queue notifications. */
61 #define VIRTIO_MSI_QUEUE_VECTOR 22
63 /* Config space size */
64 #define VIRTIO_PCI_CONFIG_NOMSI 20
65 #define VIRTIO_PCI_CONFIG_MSI 24
66 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
67 VIRTIO_PCI_CONFIG_MSI : \
68 VIRTIO_PCI_CONFIG_NOMSI)
70 /* The remaining space is defined by each driver as the per-driver
71 * configuration space */
72 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
73 VIRTIO_PCI_CONFIG_MSI : \
74 VIRTIO_PCI_CONFIG_NOMSI)
76 /* Virtio ABI version, if we increment this, we break the guest driver. */
77 #define VIRTIO_PCI_ABI_VERSION 0
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 /* We can catch some guest bugs inside here so we continue supporting older
84 guests. */
85 #define VIRTIO_PCI_BUG_BUS_MASTER (1 << 0)
87 /* QEMU doesn't strictly need write barriers since everything runs in
88 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
89 * KVM or if kqemu gets SMP support.
91 #define wmb() do { } while (0)
93 /* PCI bindings. */
95 typedef struct {
96 PCIDevice pci_dev;
97 VirtIODevice *vdev;
98 uint32_t bugs;
99 uint32_t addr;
100 uint32_t class_code;
101 uint32_t nvectors;
102 BlockConf block;
103 NICConf nic;
104 uint32_t host_features;
105 #ifdef CONFIG_LINUX
106 V9fsConf fsconf;
107 #endif
108 /* Max. number of ports we can have for a the virtio-serial device */
109 uint32_t max_virtserial_ports;
110 } VirtIOPCIProxy;
112 /* virtio device */
114 static void virtio_pci_notify(void *opaque, uint16_t vector)
116 VirtIOPCIProxy *proxy = opaque;
117 if (msix_enabled(&proxy->pci_dev))
118 msix_notify(&proxy->pci_dev, vector);
119 else
120 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
123 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
125 VirtIOPCIProxy *proxy = opaque;
126 pci_device_save(&proxy->pci_dev, f);
127 msix_save(&proxy->pci_dev, f);
128 if (msix_present(&proxy->pci_dev))
129 qemu_put_be16(f, proxy->vdev->config_vector);
132 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
134 VirtIOPCIProxy *proxy = opaque;
135 if (msix_present(&proxy->pci_dev))
136 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
139 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
141 VirtIOPCIProxy *proxy = opaque;
142 int ret;
143 ret = pci_device_load(&proxy->pci_dev, f);
144 if (ret) {
145 return ret;
147 msix_load(&proxy->pci_dev, f);
148 if (msix_present(&proxy->pci_dev)) {
149 qemu_get_be16s(f, &proxy->vdev->config_vector);
150 } else {
151 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
153 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
154 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
157 /* Try to find out if the guest has bus master disabled, but is
158 in ready state. Then we have a buggy guest OS. */
159 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
160 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
161 proxy->bugs |= VIRTIO_PCI_BUG_BUS_MASTER;
163 return 0;
166 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
168 VirtIOPCIProxy *proxy = opaque;
169 uint16_t vector;
170 if (msix_present(&proxy->pci_dev)) {
171 qemu_get_be16s(f, &vector);
172 } else {
173 vector = VIRTIO_NO_VECTOR;
175 virtio_queue_set_vector(proxy->vdev, n, vector);
176 if (vector != VIRTIO_NO_VECTOR) {
177 return msix_vector_use(&proxy->pci_dev, vector);
179 return 0;
182 static void virtio_pci_reset(DeviceState *d)
184 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
185 virtio_reset(proxy->vdev);
186 msix_reset(&proxy->pci_dev);
187 proxy->bugs = 0;
190 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
192 VirtIOPCIProxy *proxy = opaque;
193 VirtIODevice *vdev = proxy->vdev;
194 target_phys_addr_t pa;
196 switch (addr) {
197 case VIRTIO_PCI_GUEST_FEATURES:
198 /* Guest does not negotiate properly? We have to assume nothing. */
199 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
200 if (vdev->bad_features)
201 val = proxy->host_features & vdev->bad_features(vdev);
202 else
203 val = 0;
205 if (vdev->set_features)
206 vdev->set_features(vdev, val);
207 vdev->guest_features = val;
208 break;
209 case VIRTIO_PCI_QUEUE_PFN:
210 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
211 if (pa == 0) {
212 virtio_reset(proxy->vdev);
213 msix_unuse_all_vectors(&proxy->pci_dev);
215 else
216 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
217 break;
218 case VIRTIO_PCI_QUEUE_SEL:
219 if (val < VIRTIO_PCI_QUEUE_MAX)
220 vdev->queue_sel = val;
221 break;
222 case VIRTIO_PCI_QUEUE_NOTIFY:
223 virtio_queue_notify(vdev, val);
224 break;
225 case VIRTIO_PCI_STATUS:
226 virtio_set_status(vdev, val & 0xFF);
227 if (vdev->status == 0) {
228 virtio_reset(proxy->vdev);
229 msix_unuse_all_vectors(&proxy->pci_dev);
232 /* Linux before 2.6.34 sets the device as OK without enabling
233 the PCI device bus master bit. In this case we need to disable
234 some safety checks. */
235 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
236 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
237 proxy->bugs |= VIRTIO_PCI_BUG_BUS_MASTER;
239 break;
240 case VIRTIO_MSI_CONFIG_VECTOR:
241 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
242 /* Make it possible for guest to discover an error took place. */
243 if (msix_vector_use(&proxy->pci_dev, val) < 0)
244 val = VIRTIO_NO_VECTOR;
245 vdev->config_vector = val;
246 break;
247 case VIRTIO_MSI_QUEUE_VECTOR:
248 msix_vector_unuse(&proxy->pci_dev,
249 virtio_queue_vector(vdev, vdev->queue_sel));
250 /* Make it possible for guest to discover an error took place. */
251 if (msix_vector_use(&proxy->pci_dev, val) < 0)
252 val = VIRTIO_NO_VECTOR;
253 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
254 break;
255 default:
256 fprintf(stderr, "%s: unexpected address 0x%x value 0x%x\n",
257 __func__, addr, val);
258 break;
262 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
264 VirtIODevice *vdev = proxy->vdev;
265 uint32_t ret = 0xFFFFFFFF;
267 switch (addr) {
268 case VIRTIO_PCI_HOST_FEATURES:
269 ret = proxy->host_features;
270 break;
271 case VIRTIO_PCI_GUEST_FEATURES:
272 ret = vdev->guest_features;
273 break;
274 case VIRTIO_PCI_QUEUE_PFN:
275 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
276 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
277 break;
278 case VIRTIO_PCI_QUEUE_NUM:
279 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
280 break;
281 case VIRTIO_PCI_QUEUE_SEL:
282 ret = vdev->queue_sel;
283 break;
284 case VIRTIO_PCI_STATUS:
285 ret = vdev->status;
286 break;
287 case VIRTIO_PCI_ISR:
288 /* reading from the ISR also clears it. */
289 ret = vdev->isr;
290 vdev->isr = 0;
291 qemu_set_irq(proxy->pci_dev.irq[0], 0);
292 break;
293 case VIRTIO_MSI_CONFIG_VECTOR:
294 ret = vdev->config_vector;
295 break;
296 case VIRTIO_MSI_QUEUE_VECTOR:
297 ret = virtio_queue_vector(vdev, vdev->queue_sel);
298 break;
299 default:
300 break;
303 return ret;
306 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
308 VirtIOPCIProxy *proxy = opaque;
309 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
310 addr -= proxy->addr;
311 if (addr < config)
312 return virtio_ioport_read(proxy, addr);
313 addr -= config;
314 return virtio_config_readb(proxy->vdev, addr);
317 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
319 VirtIOPCIProxy *proxy = opaque;
320 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
321 addr -= proxy->addr;
322 if (addr < config)
323 return virtio_ioport_read(proxy, addr);
324 addr -= config;
325 return virtio_config_readw(proxy->vdev, addr);
328 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
330 VirtIOPCIProxy *proxy = opaque;
331 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
332 addr -= proxy->addr;
333 if (addr < config)
334 return virtio_ioport_read(proxy, addr);
335 addr -= config;
336 return virtio_config_readl(proxy->vdev, addr);
339 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
341 VirtIOPCIProxy *proxy = opaque;
342 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
343 addr -= proxy->addr;
344 if (addr < config) {
345 virtio_ioport_write(proxy, addr, val);
346 return;
348 addr -= config;
349 virtio_config_writeb(proxy->vdev, addr, val);
352 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
354 VirtIOPCIProxy *proxy = opaque;
355 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
356 addr -= proxy->addr;
357 if (addr < config) {
358 virtio_ioport_write(proxy, addr, val);
359 return;
361 addr -= config;
362 virtio_config_writew(proxy->vdev, addr, val);
365 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
367 VirtIOPCIProxy *proxy = opaque;
368 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
369 addr -= proxy->addr;
370 if (addr < config) {
371 virtio_ioport_write(proxy, addr, val);
372 return;
374 addr -= config;
375 virtio_config_writel(proxy->vdev, addr, val);
378 static void virtio_map(PCIDevice *pci_dev, int region_num,
379 pcibus_t addr, pcibus_t size, int type)
381 VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
382 VirtIODevice *vdev = proxy->vdev;
383 unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
385 proxy->addr = addr;
387 register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
388 register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
389 register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
390 register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
391 register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
392 register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
394 if (vdev->config_len)
395 vdev->get_config(vdev, vdev->config);
398 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
399 uint32_t val, int len)
401 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
403 if (PCI_COMMAND == address) {
404 if (!(val & PCI_COMMAND_MASTER)) {
405 if (!(proxy->bugs & VIRTIO_PCI_BUG_BUS_MASTER)) {
406 virtio_set_status(proxy->vdev,
407 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
412 pci_default_write_config(pci_dev, address, val, len);
413 msix_write_config(pci_dev, address, val, len);
416 static unsigned virtio_pci_get_features(void *opaque)
418 VirtIOPCIProxy *proxy = opaque;
419 return proxy->host_features;
422 static void virtio_pci_guest_notifier_read(void *opaque)
424 VirtQueue *vq = opaque;
425 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
426 if (event_notifier_test_and_clear(n)) {
427 virtio_irq(vq);
431 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
433 VirtIOPCIProxy *proxy = opaque;
434 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
435 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
437 if (assign) {
438 int r = event_notifier_init(notifier, 0);
439 if (r < 0) {
440 return r;
442 qemu_set_fd_handler(event_notifier_get_fd(notifier),
443 virtio_pci_guest_notifier_read, NULL, vq);
444 } else {
445 qemu_set_fd_handler(event_notifier_get_fd(notifier),
446 NULL, NULL, NULL);
447 event_notifier_cleanup(notifier);
450 return 0;
453 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
455 VirtIOPCIProxy *proxy = opaque;
456 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
457 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
458 int r;
459 if (assign) {
460 r = event_notifier_init(notifier, 1);
461 if (r < 0) {
462 return r;
464 r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
465 proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
466 n, assign);
467 if (r < 0) {
468 event_notifier_cleanup(notifier);
470 } else {
471 r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
472 proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
473 n, assign);
474 if (r < 0) {
475 return r;
477 event_notifier_cleanup(notifier);
479 return r;
482 static const VirtIOBindings virtio_pci_bindings = {
483 .notify = virtio_pci_notify,
484 .save_config = virtio_pci_save_config,
485 .load_config = virtio_pci_load_config,
486 .save_queue = virtio_pci_save_queue,
487 .load_queue = virtio_pci_load_queue,
488 .get_features = virtio_pci_get_features,
489 .set_host_notifier = virtio_pci_set_host_notifier,
490 .set_guest_notifier = virtio_pci_set_guest_notifier,
493 static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
494 uint16_t vendor, uint16_t device,
495 uint16_t class_code, uint8_t pif)
497 uint8_t *config;
498 uint32_t size;
500 proxy->vdev = vdev;
502 config = proxy->pci_dev.config;
503 pci_config_set_vendor_id(config, vendor);
504 pci_config_set_device_id(config, device);
506 config[0x08] = VIRTIO_PCI_ABI_VERSION;
508 config[0x09] = pif;
509 pci_config_set_class(config, class_code);
511 config[0x2c] = vendor & 0xFF;
512 config[0x2d] = (vendor >> 8) & 0xFF;
513 config[0x2e] = vdev->device_id & 0xFF;
514 config[0x2f] = (vdev->device_id >> 8) & 0xFF;
516 config[0x3d] = 1;
518 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
519 pci_register_bar(&proxy->pci_dev, 1,
520 msix_bar_size(&proxy->pci_dev),
521 PCI_BASE_ADDRESS_SPACE_MEMORY,
522 msix_mmio_map);
523 } else
524 vdev->nvectors = 0;
526 proxy->pci_dev.config_write = virtio_write_config;
528 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
529 if (size & (size-1))
530 size = 1 << qemu_fls(size);
532 pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
533 virtio_map);
535 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
536 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
537 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
538 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
541 static int virtio_blk_init_pci(PCIDevice *pci_dev)
543 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
544 VirtIODevice *vdev;
546 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
547 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
548 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
550 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block);
551 if (!vdev) {
552 return -1;
554 vdev->nvectors = proxy->nvectors;
555 virtio_init_pci(proxy, vdev,
556 PCI_VENDOR_ID_REDHAT_QUMRANET,
557 PCI_DEVICE_ID_VIRTIO_BLOCK,
558 proxy->class_code, 0x00);
559 /* make the actual value visible */
560 proxy->nvectors = vdev->nvectors;
561 return 0;
564 static int virtio_exit_pci(PCIDevice *pci_dev)
566 return msix_uninit(pci_dev);
569 static int virtio_blk_exit_pci(PCIDevice *pci_dev)
571 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
573 virtio_blk_exit(proxy->vdev);
574 blockdev_mark_auto_del(proxy->block.bs);
575 return virtio_exit_pci(pci_dev);
578 static int virtio_serial_init_pci(PCIDevice *pci_dev)
580 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
581 VirtIODevice *vdev;
583 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
584 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
585 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
586 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
588 vdev = virtio_serial_init(&pci_dev->qdev, proxy->max_virtserial_ports);
589 if (!vdev) {
590 return -1;
592 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
593 ? proxy->max_virtserial_ports + 1
594 : proxy->nvectors;
595 virtio_init_pci(proxy, vdev,
596 PCI_VENDOR_ID_REDHAT_QUMRANET,
597 PCI_DEVICE_ID_VIRTIO_CONSOLE,
598 proxy->class_code, 0x00);
599 proxy->nvectors = vdev->nvectors;
600 return 0;
603 static int virtio_serial_exit_pci(PCIDevice *pci_dev)
605 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
607 virtio_serial_exit(proxy->vdev);
608 return virtio_exit_pci(pci_dev);
611 static int virtio_net_init_pci(PCIDevice *pci_dev)
613 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
614 VirtIODevice *vdev;
616 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic);
618 vdev->nvectors = proxy->nvectors;
619 virtio_init_pci(proxy, vdev,
620 PCI_VENDOR_ID_REDHAT_QUMRANET,
621 PCI_DEVICE_ID_VIRTIO_NET,
622 PCI_CLASS_NETWORK_ETHERNET,
623 0x00);
625 /* make the actual value visible */
626 proxy->nvectors = vdev->nvectors;
627 return 0;
630 static int virtio_net_exit_pci(PCIDevice *pci_dev)
632 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
634 virtio_net_exit(proxy->vdev);
635 return virtio_exit_pci(pci_dev);
638 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
640 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
641 VirtIODevice *vdev;
643 vdev = virtio_balloon_init(&pci_dev->qdev);
644 virtio_init_pci(proxy, vdev,
645 PCI_VENDOR_ID_REDHAT_QUMRANET,
646 PCI_DEVICE_ID_VIRTIO_BALLOON,
647 PCI_CLASS_MEMORY_RAM,
648 0x00);
649 return 0;
652 #ifdef CONFIG_VIRTFS
653 static int virtio_9p_init_pci(PCIDevice *pci_dev)
655 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
656 VirtIODevice *vdev;
658 vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf);
659 virtio_init_pci(proxy, vdev,
660 PCI_VENDOR_ID_REDHAT_QUMRANET,
661 0x1009,
662 0x2,
663 0x00);
665 return 0;
667 #endif
669 static PCIDeviceInfo virtio_info[] = {
671 .qdev.name = "virtio-blk-pci",
672 .qdev.size = sizeof(VirtIOPCIProxy),
673 .init = virtio_blk_init_pci,
674 .exit = virtio_blk_exit_pci,
675 .qdev.props = (Property[]) {
676 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
677 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
678 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
679 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
680 DEFINE_PROP_END_OF_LIST(),
682 .qdev.reset = virtio_pci_reset,
684 .qdev.name = "virtio-net-pci",
685 .qdev.size = sizeof(VirtIOPCIProxy),
686 .init = virtio_net_init_pci,
687 .exit = virtio_net_exit_pci,
688 .romfile = "pxe-virtio.bin",
689 .qdev.props = (Property[]) {
690 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
691 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
692 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
693 DEFINE_PROP_END_OF_LIST(),
695 .qdev.reset = virtio_pci_reset,
697 .qdev.name = "virtio-serial-pci",
698 .qdev.alias = "virtio-serial",
699 .qdev.size = sizeof(VirtIOPCIProxy),
700 .init = virtio_serial_init_pci,
701 .exit = virtio_serial_exit_pci,
702 .qdev.props = (Property[]) {
703 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
704 DEV_NVECTORS_UNSPECIFIED),
705 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
706 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
707 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, max_virtserial_ports,
708 31),
709 DEFINE_PROP_END_OF_LIST(),
711 .qdev.reset = virtio_pci_reset,
713 .qdev.name = "virtio-balloon-pci",
714 .qdev.size = sizeof(VirtIOPCIProxy),
715 .init = virtio_balloon_init_pci,
716 .exit = virtio_exit_pci,
717 .qdev.props = (Property[]) {
718 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
719 DEFINE_PROP_END_OF_LIST(),
721 .qdev.reset = virtio_pci_reset,
723 #ifdef CONFIG_VIRTFS
724 .qdev.name = "virtio-9p-pci",
725 .qdev.size = sizeof(VirtIOPCIProxy),
726 .init = virtio_9p_init_pci,
727 .qdev.props = (Property[]) {
728 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
729 DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
730 DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
731 DEFINE_PROP_END_OF_LIST(),
733 }, {
734 #endif
735 /* end of list */
739 static void virtio_pci_register_devices(void)
741 pci_qdev_register_many(virtio_info);
744 device_init(virtio_pci_register_devices)