4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
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.
19 #include "virtio-blk.h"
20 #include "virtio-net.h"
27 /* from Linux's linux/virtio_pci.h */
29 /* A 32-bit r/o bitmask of the features supported by the host */
30 #define VIRTIO_PCI_HOST_FEATURES 0
32 /* A 32-bit r/w bitmask of features activated by the guest */
33 #define VIRTIO_PCI_GUEST_FEATURES 4
35 /* A 32-bit r/w PFN for the currently selected queue */
36 #define VIRTIO_PCI_QUEUE_PFN 8
38 /* A 16-bit r/o queue size for the currently selected queue */
39 #define VIRTIO_PCI_QUEUE_NUM 12
41 /* A 16-bit r/w queue selector */
42 #define VIRTIO_PCI_QUEUE_SEL 14
44 /* A 16-bit r/w queue notifier */
45 #define VIRTIO_PCI_QUEUE_NOTIFY 16
47 /* An 8-bit device status register. */
48 #define VIRTIO_PCI_STATUS 18
50 /* An 8-bit r/o interrupt status register. Reading the value will return the
51 * current contents of the ISR and will also clear it. This is effectively
52 * a read-and-acknowledge. */
53 #define VIRTIO_PCI_ISR 19
55 /* MSI-X registers: only enabled if MSI-X is enabled. */
56 /* A 16-bit vector for configuration changes. */
57 #define VIRTIO_MSI_CONFIG_VECTOR 20
58 /* A 16-bit vector for selected queue notifications. */
59 #define VIRTIO_MSI_QUEUE_VECTOR 22
61 /* Config space size */
62 #define VIRTIO_PCI_CONFIG_NOMSI 20
63 #define VIRTIO_PCI_CONFIG_MSI 24
64 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
65 VIRTIO_PCI_CONFIG_MSI : \
66 VIRTIO_PCI_CONFIG_NOMSI)
68 /* The remaining space is defined by each driver as the per-driver
69 * configuration space */
70 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
71 VIRTIO_PCI_CONFIG_MSI : \
72 VIRTIO_PCI_CONFIG_NOMSI)
74 /* Virtio ABI version, if we increment this, we break the guest driver. */
75 #define VIRTIO_PCI_ABI_VERSION 0
77 /* How many bits to shift physical queue address written to QUEUE_PFN.
78 * 12 is historical, and due to x86 page size. */
79 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
81 /* QEMU doesn't strictly need write barriers since everything runs in
82 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
83 * KVM or if kqemu gets SMP support.
85 #define wmb() do { } while (0)
97 uint32_t host_features
;
98 /* Max. number of ports we can have for a the virtio-serial device */
99 uint32_t max_virtserial_ports
;
104 static void virtio_pci_notify(void *opaque
, uint16_t vector
)
106 VirtIOPCIProxy
*proxy
= opaque
;
107 if (msix_enabled(&proxy
->pci_dev
))
108 msix_notify(&proxy
->pci_dev
, vector
);
110 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
113 static void virtio_pci_save_config(void * opaque
, QEMUFile
*f
)
115 VirtIOPCIProxy
*proxy
= opaque
;
116 pci_device_save(&proxy
->pci_dev
, f
);
117 msix_save(&proxy
->pci_dev
, f
);
118 if (msix_present(&proxy
->pci_dev
))
119 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
122 static void virtio_pci_save_queue(void * opaque
, int n
, QEMUFile
*f
)
124 VirtIOPCIProxy
*proxy
= opaque
;
125 if (msix_present(&proxy
->pci_dev
))
126 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
129 static int virtio_pci_load_config(void * opaque
, QEMUFile
*f
)
131 VirtIOPCIProxy
*proxy
= opaque
;
133 ret
= pci_device_load(&proxy
->pci_dev
, f
);
137 msix_load(&proxy
->pci_dev
, f
);
138 if (msix_present(&proxy
->pci_dev
)) {
139 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
141 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
143 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
144 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
149 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
151 VirtIOPCIProxy
*proxy
= opaque
;
153 if (msix_present(&proxy
->pci_dev
)) {
154 qemu_get_be16s(f
, &vector
);
156 vector
= VIRTIO_NO_VECTOR
;
158 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
159 if (vector
!= VIRTIO_NO_VECTOR
) {
160 return msix_vector_use(&proxy
->pci_dev
, vector
);
165 static void virtio_pci_reset(DeviceState
*d
)
167 VirtIOPCIProxy
*proxy
= container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
168 virtio_reset(proxy
->vdev
);
169 msix_reset(&proxy
->pci_dev
);
172 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
174 VirtIOPCIProxy
*proxy
= opaque
;
175 VirtIODevice
*vdev
= proxy
->vdev
;
176 target_phys_addr_t pa
;
179 case VIRTIO_PCI_GUEST_FEATURES
:
180 /* Guest does not negotiate properly? We have to assume nothing. */
181 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
182 if (vdev
->bad_features
)
183 val
= proxy
->host_features
& vdev
->bad_features(vdev
);
187 if (vdev
->set_features
)
188 vdev
->set_features(vdev
, val
);
189 vdev
->guest_features
= val
;
191 case VIRTIO_PCI_QUEUE_PFN
:
192 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
194 virtio_reset(proxy
->vdev
);
195 msix_unuse_all_vectors(&proxy
->pci_dev
);
198 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
200 case VIRTIO_PCI_QUEUE_SEL
:
201 if (val
< VIRTIO_PCI_QUEUE_MAX
)
202 vdev
->queue_sel
= val
;
204 case VIRTIO_PCI_QUEUE_NOTIFY
:
205 virtio_queue_notify(vdev
, val
);
207 case VIRTIO_PCI_STATUS
:
208 vdev
->status
= val
& 0xFF;
209 if (vdev
->status
== 0) {
210 virtio_reset(proxy
->vdev
);
211 msix_unuse_all_vectors(&proxy
->pci_dev
);
214 case VIRTIO_MSI_CONFIG_VECTOR
:
215 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
216 /* Make it possible for guest to discover an error took place. */
217 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
218 val
= VIRTIO_NO_VECTOR
;
219 vdev
->config_vector
= val
;
221 case VIRTIO_MSI_QUEUE_VECTOR
:
222 msix_vector_unuse(&proxy
->pci_dev
,
223 virtio_queue_vector(vdev
, vdev
->queue_sel
));
224 /* Make it possible for guest to discover an error took place. */
225 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
226 val
= VIRTIO_NO_VECTOR
;
227 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
230 fprintf(stderr
, "%s: unexpected address 0x%x value 0x%x\n",
231 __func__
, addr
, val
);
236 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
238 VirtIODevice
*vdev
= proxy
->vdev
;
239 uint32_t ret
= 0xFFFFFFFF;
242 case VIRTIO_PCI_HOST_FEATURES
:
243 ret
= proxy
->host_features
;
245 case VIRTIO_PCI_GUEST_FEATURES
:
246 ret
= vdev
->guest_features
;
248 case VIRTIO_PCI_QUEUE_PFN
:
249 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
250 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
252 case VIRTIO_PCI_QUEUE_NUM
:
253 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
255 case VIRTIO_PCI_QUEUE_SEL
:
256 ret
= vdev
->queue_sel
;
258 case VIRTIO_PCI_STATUS
:
262 /* reading from the ISR also clears it. */
265 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
267 case VIRTIO_MSI_CONFIG_VECTOR
:
268 ret
= vdev
->config_vector
;
270 case VIRTIO_MSI_QUEUE_VECTOR
:
271 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
280 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
282 VirtIOPCIProxy
*proxy
= opaque
;
283 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
286 return virtio_ioport_read(proxy
, addr
);
288 return virtio_config_readb(proxy
->vdev
, addr
);
291 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
293 VirtIOPCIProxy
*proxy
= opaque
;
294 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
297 return virtio_ioport_read(proxy
, addr
);
299 return virtio_config_readw(proxy
->vdev
, addr
);
302 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
304 VirtIOPCIProxy
*proxy
= opaque
;
305 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
308 return virtio_ioport_read(proxy
, addr
);
310 return virtio_config_readl(proxy
->vdev
, addr
);
313 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
315 VirtIOPCIProxy
*proxy
= opaque
;
316 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
319 virtio_ioport_write(proxy
, addr
, val
);
323 virtio_config_writeb(proxy
->vdev
, addr
, val
);
326 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
328 VirtIOPCIProxy
*proxy
= opaque
;
329 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
332 virtio_ioport_write(proxy
, addr
, val
);
336 virtio_config_writew(proxy
->vdev
, addr
, val
);
339 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
341 VirtIOPCIProxy
*proxy
= opaque
;
342 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
345 virtio_ioport_write(proxy
, addr
, val
);
349 virtio_config_writel(proxy
->vdev
, addr
, val
);
352 static void virtio_map(PCIDevice
*pci_dev
, int region_num
,
353 pcibus_t addr
, pcibus_t size
, int type
)
355 VirtIOPCIProxy
*proxy
= container_of(pci_dev
, VirtIOPCIProxy
, pci_dev
);
356 VirtIODevice
*vdev
= proxy
->vdev
;
357 unsigned config_len
= VIRTIO_PCI_REGION_SIZE(pci_dev
) + vdev
->config_len
;
361 register_ioport_write(addr
, config_len
, 1, virtio_pci_config_writeb
, proxy
);
362 register_ioport_write(addr
, config_len
, 2, virtio_pci_config_writew
, proxy
);
363 register_ioport_write(addr
, config_len
, 4, virtio_pci_config_writel
, proxy
);
364 register_ioport_read(addr
, config_len
, 1, virtio_pci_config_readb
, proxy
);
365 register_ioport_read(addr
, config_len
, 2, virtio_pci_config_readw
, proxy
);
366 register_ioport_read(addr
, config_len
, 4, virtio_pci_config_readl
, proxy
);
368 if (vdev
->config_len
)
369 vdev
->get_config(vdev
, vdev
->config
);
372 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
373 uint32_t val
, int len
)
375 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
377 if (PCI_COMMAND
== address
) {
378 if (!(val
& PCI_COMMAND_MASTER
)) {
379 proxy
->vdev
->status
&= ~VIRTIO_CONFIG_S_DRIVER_OK
;
383 pci_default_write_config(pci_dev
, address
, val
, len
);
384 msix_write_config(pci_dev
, address
, val
, len
);
387 static unsigned virtio_pci_get_features(void *opaque
)
389 VirtIOPCIProxy
*proxy
= opaque
;
390 return proxy
->host_features
;
393 static const VirtIOBindings virtio_pci_bindings
= {
394 .notify
= virtio_pci_notify
,
395 .save_config
= virtio_pci_save_config
,
396 .load_config
= virtio_pci_load_config
,
397 .save_queue
= virtio_pci_save_queue
,
398 .load_queue
= virtio_pci_load_queue
,
399 .get_features
= virtio_pci_get_features
,
402 static void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
,
403 uint16_t vendor
, uint16_t device
,
404 uint16_t class_code
, uint8_t pif
)
411 config
= proxy
->pci_dev
.config
;
412 pci_config_set_vendor_id(config
, vendor
);
413 pci_config_set_device_id(config
, device
);
415 config
[0x08] = VIRTIO_PCI_ABI_VERSION
;
418 pci_config_set_class(config
, class_code
);
419 config
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
;
421 config
[0x2c] = vendor
& 0xFF;
422 config
[0x2d] = (vendor
>> 8) & 0xFF;
423 config
[0x2e] = vdev
->device_id
& 0xFF;
424 config
[0x2f] = (vdev
->device_id
>> 8) & 0xFF;
428 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
, 1, 0)) {
429 pci_register_bar(&proxy
->pci_dev
, 1,
430 msix_bar_size(&proxy
->pci_dev
),
431 PCI_BASE_ADDRESS_SPACE_MEMORY
,
436 proxy
->pci_dev
.config_write
= virtio_write_config
;
438 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
440 size
= 1 << qemu_fls(size
);
442 pci_register_bar(&proxy
->pci_dev
, 0, size
, PCI_BASE_ADDRESS_SPACE_IO
,
445 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
446 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
447 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
448 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
451 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
453 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
456 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
457 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
458 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
461 qemu_error("virtio-blk-pci: drive property not set\n");
464 vdev
= virtio_blk_init(&pci_dev
->qdev
, proxy
->dinfo
);
465 vdev
->nvectors
= proxy
->nvectors
;
466 virtio_init_pci(proxy
, vdev
,
467 PCI_VENDOR_ID_REDHAT_QUMRANET
,
468 PCI_DEVICE_ID_VIRTIO_BLOCK
,
469 proxy
->class_code
, 0x00);
470 /* make the actual value visible */
471 proxy
->nvectors
= vdev
->nvectors
;
475 static int virtio_exit_pci(PCIDevice
*pci_dev
)
477 return msix_uninit(pci_dev
);
480 static int virtio_blk_exit_pci(PCIDevice
*pci_dev
)
482 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
484 drive_uninit(proxy
->dinfo
);
485 return virtio_exit_pci(pci_dev
);
488 static int virtio_serial_init_pci(PCIDevice
*pci_dev
)
490 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
493 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
494 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
495 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
496 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
498 vdev
= virtio_serial_init(&pci_dev
->qdev
, proxy
->max_virtserial_ports
);
502 vdev
->nvectors
= proxy
->nvectors
? proxy
->nvectors
503 : proxy
->max_virtserial_ports
+ 1;
504 virtio_init_pci(proxy
, vdev
,
505 PCI_VENDOR_ID_REDHAT_QUMRANET
,
506 PCI_DEVICE_ID_VIRTIO_CONSOLE
,
507 proxy
->class_code
, 0x00);
508 proxy
->nvectors
= vdev
->nvectors
;
512 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
514 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
517 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
);
519 vdev
->nvectors
= proxy
->nvectors
;
520 virtio_init_pci(proxy
, vdev
,
521 PCI_VENDOR_ID_REDHAT_QUMRANET
,
522 PCI_DEVICE_ID_VIRTIO_NET
,
523 PCI_CLASS_NETWORK_ETHERNET
,
526 /* make the actual value visible */
527 proxy
->nvectors
= vdev
->nvectors
;
531 static int virtio_net_exit_pci(PCIDevice
*pci_dev
)
533 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
535 virtio_net_exit(proxy
->vdev
);
536 return virtio_exit_pci(pci_dev
);
539 static int virtio_balloon_init_pci(PCIDevice
*pci_dev
)
541 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
544 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
545 virtio_init_pci(proxy
, vdev
,
546 PCI_VENDOR_ID_REDHAT_QUMRANET
,
547 PCI_DEVICE_ID_VIRTIO_BALLOON
,
548 PCI_CLASS_MEMORY_RAM
,
553 static PCIDeviceInfo virtio_info
[] = {
555 .qdev
.name
= "virtio-blk-pci",
556 .qdev
.size
= sizeof(VirtIOPCIProxy
),
557 .init
= virtio_blk_init_pci
,
558 .exit
= virtio_blk_exit_pci
,
559 .qdev
.props
= (Property
[]) {
560 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
561 DEFINE_PROP_DRIVE("drive", VirtIOPCIProxy
, dinfo
),
562 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
563 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
564 DEFINE_PROP_END_OF_LIST(),
566 .qdev
.reset
= virtio_pci_reset
,
568 .qdev
.name
= "virtio-net-pci",
569 .qdev
.size
= sizeof(VirtIOPCIProxy
),
570 .init
= virtio_net_init_pci
,
571 .exit
= virtio_net_exit_pci
,
572 .romfile
= "pxe-virtio.bin",
573 .qdev
.props
= (Property
[]) {
574 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
575 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
576 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
577 DEFINE_PROP_END_OF_LIST(),
579 .qdev
.reset
= virtio_pci_reset
,
581 .qdev
.name
= "virtio-serial-pci",
582 .qdev
.alias
= "virtio-serial",
583 .qdev
.size
= sizeof(VirtIOPCIProxy
),
584 .init
= virtio_serial_init_pci
,
585 .exit
= virtio_exit_pci
,
586 .qdev
.props
= (Property
[]) {
587 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 0),
588 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
589 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
590 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy
, max_virtserial_ports
,
592 DEFINE_PROP_END_OF_LIST(),
594 .qdev
.reset
= virtio_pci_reset
,
596 .qdev
.name
= "virtio-balloon-pci",
597 .qdev
.size
= sizeof(VirtIOPCIProxy
),
598 .init
= virtio_balloon_init_pci
,
599 .exit
= virtio_exit_pci
,
600 .qdev
.props
= (Property
[]) {
601 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
602 DEFINE_PROP_END_OF_LIST(),
604 .qdev
.reset
= virtio_pci_reset
,
610 static void virtio_pci_register_devices(void)
612 pci_qdev_register_many(virtio_info
);
615 device_init(virtio_pci_register_devices
)