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.
25 /* from Linux's linux/virtio_pci.h */
27 /* A 32-bit r/o bitmask of the features supported by the host */
28 #define VIRTIO_PCI_HOST_FEATURES 0
30 /* A 32-bit r/w bitmask of features activated by the guest */
31 #define VIRTIO_PCI_GUEST_FEATURES 4
33 /* A 32-bit r/w PFN for the currently selected queue */
34 #define VIRTIO_PCI_QUEUE_PFN 8
36 /* A 16-bit r/o queue size for the currently selected queue */
37 #define VIRTIO_PCI_QUEUE_NUM 12
39 /* A 16-bit r/w queue selector */
40 #define VIRTIO_PCI_QUEUE_SEL 14
42 /* A 16-bit r/w queue notifier */
43 #define VIRTIO_PCI_QUEUE_NOTIFY 16
45 /* An 8-bit device status register. */
46 #define VIRTIO_PCI_STATUS 18
48 /* An 8-bit r/o interrupt status register. Reading the value will return the
49 * current contents of the ISR and will also clear it. This is effectively
50 * a read-and-acknowledge. */
51 #define VIRTIO_PCI_ISR 19
53 /* MSI-X registers: only enabled if MSI-X is enabled. */
54 /* A 16-bit vector for configuration changes. */
55 #define VIRTIO_MSI_CONFIG_VECTOR 20
56 /* A 16-bit vector for selected queue notifications. */
57 #define VIRTIO_MSI_QUEUE_VECTOR 22
59 /* Config space size */
60 #define VIRTIO_PCI_CONFIG_NOMSI 20
61 #define VIRTIO_PCI_CONFIG_MSI 24
62 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
63 VIRTIO_PCI_CONFIG_MSI : \
64 VIRTIO_PCI_CONFIG_NOMSI)
66 /* The remaining space is defined by each driver as the per-driver
67 * configuration space */
68 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
69 VIRTIO_PCI_CONFIG_MSI : \
70 VIRTIO_PCI_CONFIG_NOMSI)
72 /* Virtio ABI version, if we increment this, we break the guest driver. */
73 #define VIRTIO_PCI_ABI_VERSION 0
75 /* How many bits to shift physical queue address written to QUEUE_PFN.
76 * 12 is historical, and due to x86 page size. */
77 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
79 /* QEMU doesn't strictly need write barriers since everything runs in
80 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
81 * KVM or if kqemu gets SMP support.
83 #define wmb() do { } while (0)
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
);
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
;
128 ret
= pci_device_load(&proxy
->pci_dev
, f
);
132 msix_load(&proxy
->pci_dev
, f
);
133 if (msix_present(&proxy
->pci_dev
)) {
134 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
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
);
144 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
146 VirtIOPCIProxy
*proxy
= opaque
;
148 if (msix_present(&proxy
->pci_dev
)) {
149 qemu_get_be16s(f
, &vector
);
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
);
160 static void virtio_pci_reset(DeviceState
*d
)
162 VirtIOPCIProxy
*proxy
= container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
163 virtio_reset(proxy
->vdev
);
164 msix_reset(&proxy
->pci_dev
);
167 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
169 VirtIOPCIProxy
*proxy
= opaque
;
170 VirtIODevice
*vdev
= proxy
->vdev
;
171 target_phys_addr_t pa
;
174 case VIRTIO_PCI_GUEST_FEATURES
:
175 /* Guest does not negotiate properly? We have to assume nothing. */
176 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
177 if (vdev
->bad_features
)
178 val
= vdev
->bad_features(vdev
);
182 if (vdev
->set_features
)
183 vdev
->set_features(vdev
, val
);
184 vdev
->features
= val
;
186 case VIRTIO_PCI_QUEUE_PFN
:
187 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
189 virtio_reset(proxy
->vdev
);
190 msix_unuse_all_vectors(&proxy
->pci_dev
);
193 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
195 case VIRTIO_PCI_QUEUE_SEL
:
196 if (val
< VIRTIO_PCI_QUEUE_MAX
)
197 vdev
->queue_sel
= val
;
199 case VIRTIO_PCI_QUEUE_NOTIFY
:
200 virtio_queue_notify(vdev
, val
);
202 case VIRTIO_PCI_STATUS
:
203 vdev
->status
= val
& 0xFF;
204 if (vdev
->status
== 0) {
205 virtio_reset(proxy
->vdev
);
206 msix_unuse_all_vectors(&proxy
->pci_dev
);
209 case VIRTIO_MSI_CONFIG_VECTOR
:
210 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
211 /* Make it possible for guest to discover an error took place. */
212 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
213 val
= VIRTIO_NO_VECTOR
;
214 vdev
->config_vector
= val
;
216 case VIRTIO_MSI_QUEUE_VECTOR
:
217 msix_vector_unuse(&proxy
->pci_dev
,
218 virtio_queue_vector(vdev
, vdev
->queue_sel
));
219 /* Make it possible for guest to discover an error took place. */
220 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
221 val
= VIRTIO_NO_VECTOR
;
222 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
225 fprintf(stderr
, "%s: unexpected address 0x%x value 0x%x\n",
226 __func__
, addr
, val
);
231 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
233 VirtIODevice
*vdev
= proxy
->vdev
;
234 uint32_t ret
= 0xFFFFFFFF;
237 case VIRTIO_PCI_HOST_FEATURES
:
238 ret
= vdev
->get_features(vdev
);
239 ret
|= (1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
240 ret
|= (1 << VIRTIO_RING_F_INDIRECT_DESC
);
241 ret
|= (1 << VIRTIO_F_BAD_FEATURE
);
243 case VIRTIO_PCI_GUEST_FEATURES
:
244 ret
= vdev
->features
;
246 case VIRTIO_PCI_QUEUE_PFN
:
247 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
248 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
250 case VIRTIO_PCI_QUEUE_NUM
:
251 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
253 case VIRTIO_PCI_QUEUE_SEL
:
254 ret
= vdev
->queue_sel
;
256 case VIRTIO_PCI_STATUS
:
260 /* reading from the ISR also clears it. */
263 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
265 case VIRTIO_MSI_CONFIG_VECTOR
:
266 ret
= vdev
->config_vector
;
268 case VIRTIO_MSI_QUEUE_VECTOR
:
269 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
278 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
280 VirtIOPCIProxy
*proxy
= opaque
;
281 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
284 return virtio_ioport_read(proxy
, addr
);
286 return virtio_config_readb(proxy
->vdev
, addr
);
289 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
291 VirtIOPCIProxy
*proxy
= opaque
;
292 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
295 return virtio_ioport_read(proxy
, addr
);
297 return virtio_config_readw(proxy
->vdev
, addr
);
300 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
302 VirtIOPCIProxy
*proxy
= opaque
;
303 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
306 return virtio_ioport_read(proxy
, addr
);
308 return virtio_config_readl(proxy
->vdev
, addr
);
311 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
313 VirtIOPCIProxy
*proxy
= opaque
;
314 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
317 virtio_ioport_write(proxy
, addr
, val
);
321 virtio_config_writeb(proxy
->vdev
, addr
, val
);
324 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
326 VirtIOPCIProxy
*proxy
= opaque
;
327 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
330 virtio_ioport_write(proxy
, addr
, val
);
334 virtio_config_writew(proxy
->vdev
, addr
, val
);
337 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
339 VirtIOPCIProxy
*proxy
= opaque
;
340 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
343 virtio_ioport_write(proxy
, addr
, val
);
347 virtio_config_writel(proxy
->vdev
, addr
, val
);
350 static void virtio_map(PCIDevice
*pci_dev
, int region_num
,
351 pcibus_t addr
, pcibus_t size
, int type
)
353 VirtIOPCIProxy
*proxy
= container_of(pci_dev
, VirtIOPCIProxy
, pci_dev
);
354 VirtIODevice
*vdev
= proxy
->vdev
;
355 unsigned config_len
= VIRTIO_PCI_REGION_SIZE(pci_dev
) + vdev
->config_len
;
359 register_ioport_write(addr
, config_len
, 1, virtio_pci_config_writeb
, proxy
);
360 register_ioport_write(addr
, config_len
, 2, virtio_pci_config_writew
, proxy
);
361 register_ioport_write(addr
, config_len
, 4, virtio_pci_config_writel
, proxy
);
362 register_ioport_read(addr
, config_len
, 1, virtio_pci_config_readb
, proxy
);
363 register_ioport_read(addr
, config_len
, 2, virtio_pci_config_readw
, proxy
);
364 register_ioport_read(addr
, config_len
, 4, virtio_pci_config_readl
, proxy
);
366 if (vdev
->config_len
)
367 vdev
->get_config(vdev
, vdev
->config
);
370 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
371 uint32_t val
, int len
)
373 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
375 if (PCI_COMMAND
== address
) {
376 if (!(val
& PCI_COMMAND_MASTER
)) {
377 proxy
->vdev
->status
&= !VIRTIO_CONFIG_S_DRIVER_OK
;
381 pci_default_write_config(pci_dev
, address
, val
, len
);
382 msix_write_config(pci_dev
, address
, val
, len
);
385 static const VirtIOBindings virtio_pci_bindings
= {
386 .notify
= virtio_pci_notify
,
387 .save_config
= virtio_pci_save_config
,
388 .load_config
= virtio_pci_load_config
,
389 .save_queue
= virtio_pci_save_queue
,
390 .load_queue
= virtio_pci_load_queue
,
393 static void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
,
394 uint16_t vendor
, uint16_t device
,
395 uint16_t class_code
, uint8_t pif
)
402 config
= proxy
->pci_dev
.config
;
403 pci_config_set_vendor_id(config
, vendor
);
404 pci_config_set_device_id(config
, device
);
406 config
[0x08] = VIRTIO_PCI_ABI_VERSION
;
409 pci_config_set_class(config
, class_code
);
410 config
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
;
412 config
[0x2c] = vendor
& 0xFF;
413 config
[0x2d] = (vendor
>> 8) & 0xFF;
414 config
[0x2e] = vdev
->device_id
& 0xFF;
415 config
[0x2f] = (vdev
->device_id
>> 8) & 0xFF;
419 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
, 1, 0)) {
420 pci_register_bar(&proxy
->pci_dev
, 1,
421 msix_bar_size(&proxy
->pci_dev
),
422 PCI_BASE_ADDRESS_SPACE_MEMORY
,
427 proxy
->pci_dev
.config_write
= virtio_write_config
;
429 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
431 size
= 1 << qemu_fls(size
);
433 pci_register_bar(&proxy
->pci_dev
, 0, size
, PCI_BASE_ADDRESS_SPACE_IO
,
436 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
439 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
441 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
444 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
445 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
446 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
449 qemu_error("virtio-blk-pci: drive property not set\n");
452 vdev
= virtio_blk_init(&pci_dev
->qdev
, proxy
->dinfo
);
453 vdev
->nvectors
= proxy
->nvectors
;
454 virtio_init_pci(proxy
, vdev
,
455 PCI_VENDOR_ID_REDHAT_QUMRANET
,
456 PCI_DEVICE_ID_VIRTIO_BLOCK
,
457 proxy
->class_code
, 0x00);
458 /* make the actual value visible */
459 proxy
->nvectors
= vdev
->nvectors
;
463 static int virtio_exit_pci(PCIDevice
*pci_dev
)
465 return msix_uninit(pci_dev
);
468 static int virtio_blk_exit_pci(PCIDevice
*pci_dev
)
470 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
472 drive_uninit(proxy
->dinfo
);
473 return virtio_exit_pci(pci_dev
);
476 static int virtio_console_init_pci(PCIDevice
*pci_dev
)
478 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
481 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
482 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
483 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
484 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
486 vdev
= virtio_console_init(&pci_dev
->qdev
);
490 virtio_init_pci(proxy
, vdev
,
491 PCI_VENDOR_ID_REDHAT_QUMRANET
,
492 PCI_DEVICE_ID_VIRTIO_CONSOLE
,
493 proxy
->class_code
, 0x00);
497 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
499 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
502 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
);
504 vdev
->nvectors
= proxy
->nvectors
;
505 virtio_init_pci(proxy
, vdev
,
506 PCI_VENDOR_ID_REDHAT_QUMRANET
,
507 PCI_DEVICE_ID_VIRTIO_NET
,
508 PCI_CLASS_NETWORK_ETHERNET
,
511 /* make the actual value visible */
512 proxy
->nvectors
= vdev
->nvectors
;
514 if (!pci_dev
->qdev
.hotplugged
) {
515 static int loaded
= 0;
517 rom_add_option("pxe-virtio.bin");
524 static int virtio_net_exit_pci(PCIDevice
*pci_dev
)
526 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
528 virtio_net_exit(proxy
->vdev
);
529 return virtio_exit_pci(pci_dev
);
532 static int virtio_balloon_init_pci(PCIDevice
*pci_dev
)
534 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
537 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
538 virtio_init_pci(proxy
, vdev
,
539 PCI_VENDOR_ID_REDHAT_QUMRANET
,
540 PCI_DEVICE_ID_VIRTIO_BALLOON
,
541 PCI_CLASS_MEMORY_RAM
,
546 static PCIDeviceInfo virtio_info
[] = {
548 .qdev
.name
= "virtio-blk-pci",
549 .qdev
.size
= sizeof(VirtIOPCIProxy
),
550 .init
= virtio_blk_init_pci
,
551 .exit
= virtio_blk_exit_pci
,
552 .qdev
.props
= (Property
[]) {
553 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
554 DEFINE_PROP_DRIVE("drive", VirtIOPCIProxy
, dinfo
),
555 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
556 DEFINE_PROP_END_OF_LIST(),
558 .qdev
.reset
= virtio_pci_reset
,
560 .qdev
.name
= "virtio-net-pci",
561 .qdev
.size
= sizeof(VirtIOPCIProxy
),
562 .init
= virtio_net_init_pci
,
563 .exit
= virtio_net_exit_pci
,
564 .qdev
.props
= (Property
[]) {
565 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
566 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
567 DEFINE_PROP_END_OF_LIST(),
569 .qdev
.reset
= virtio_pci_reset
,
571 .qdev
.name
= "virtio-console-pci",
572 .qdev
.size
= sizeof(VirtIOPCIProxy
),
573 .init
= virtio_console_init_pci
,
574 .exit
= virtio_exit_pci
,
575 .qdev
.props
= (Property
[]) {
576 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
577 DEFINE_PROP_END_OF_LIST(),
579 .qdev
.reset
= virtio_pci_reset
,
581 .qdev
.name
= "virtio-balloon-pci",
582 .qdev
.size
= sizeof(VirtIOPCIProxy
),
583 .init
= virtio_balloon_init_pci
,
584 .exit
= virtio_exit_pci
,
585 .qdev
.reset
= virtio_pci_reset
,
591 static void virtio_pci_register_devices(void)
593 pci_qdev_register_many(virtio_info
);
596 device_init(virtio_pci_register_devices
)