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
|= vdev
->binding
->get_features(proxy
);
241 case VIRTIO_PCI_GUEST_FEATURES
:
242 ret
= vdev
->features
;
244 case VIRTIO_PCI_QUEUE_PFN
:
245 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
246 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
248 case VIRTIO_PCI_QUEUE_NUM
:
249 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
251 case VIRTIO_PCI_QUEUE_SEL
:
252 ret
= vdev
->queue_sel
;
254 case VIRTIO_PCI_STATUS
:
258 /* reading from the ISR also clears it. */
261 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
263 case VIRTIO_MSI_CONFIG_VECTOR
:
264 ret
= vdev
->config_vector
;
266 case VIRTIO_MSI_QUEUE_VECTOR
:
267 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
276 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
278 VirtIOPCIProxy
*proxy
= opaque
;
279 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
282 return virtio_ioport_read(proxy
, addr
);
284 return virtio_config_readb(proxy
->vdev
, addr
);
287 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
289 VirtIOPCIProxy
*proxy
= opaque
;
290 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
293 return virtio_ioport_read(proxy
, addr
);
295 return virtio_config_readw(proxy
->vdev
, addr
);
298 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
300 VirtIOPCIProxy
*proxy
= opaque
;
301 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
304 return virtio_ioport_read(proxy
, addr
);
306 return virtio_config_readl(proxy
->vdev
, addr
);
309 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
311 VirtIOPCIProxy
*proxy
= opaque
;
312 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
315 virtio_ioport_write(proxy
, addr
, val
);
319 virtio_config_writeb(proxy
->vdev
, addr
, val
);
322 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
324 VirtIOPCIProxy
*proxy
= opaque
;
325 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
328 virtio_ioport_write(proxy
, addr
, val
);
332 virtio_config_writew(proxy
->vdev
, addr
, val
);
335 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
337 VirtIOPCIProxy
*proxy
= opaque
;
338 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
341 virtio_ioport_write(proxy
, addr
, val
);
345 virtio_config_writel(proxy
->vdev
, addr
, val
);
348 static void virtio_map(PCIDevice
*pci_dev
, int region_num
,
349 pcibus_t addr
, pcibus_t size
, int type
)
351 VirtIOPCIProxy
*proxy
= container_of(pci_dev
, VirtIOPCIProxy
, pci_dev
);
352 VirtIODevice
*vdev
= proxy
->vdev
;
353 unsigned config_len
= VIRTIO_PCI_REGION_SIZE(pci_dev
) + vdev
->config_len
;
357 register_ioport_write(addr
, config_len
, 1, virtio_pci_config_writeb
, proxy
);
358 register_ioport_write(addr
, config_len
, 2, virtio_pci_config_writew
, proxy
);
359 register_ioport_write(addr
, config_len
, 4, virtio_pci_config_writel
, proxy
);
360 register_ioport_read(addr
, config_len
, 1, virtio_pci_config_readb
, proxy
);
361 register_ioport_read(addr
, config_len
, 2, virtio_pci_config_readw
, proxy
);
362 register_ioport_read(addr
, config_len
, 4, virtio_pci_config_readl
, proxy
);
364 if (vdev
->config_len
)
365 vdev
->get_config(vdev
, vdev
->config
);
368 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
369 uint32_t val
, int len
)
371 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
373 if (PCI_COMMAND
== address
) {
374 if (!(val
& PCI_COMMAND_MASTER
)) {
375 proxy
->vdev
->status
&= ~VIRTIO_CONFIG_S_DRIVER_OK
;
379 pci_default_write_config(pci_dev
, address
, val
, len
);
380 msix_write_config(pci_dev
, address
, val
, len
);
383 static unsigned virtio_pci_get_features(void *opaque
)
386 ret
|= (1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
387 ret
|= (1 << VIRTIO_RING_F_INDIRECT_DESC
);
388 ret
|= (1 << VIRTIO_F_BAD_FEATURE
);
392 static const VirtIOBindings virtio_pci_bindings
= {
393 .notify
= virtio_pci_notify
,
394 .save_config
= virtio_pci_save_config
,
395 .load_config
= virtio_pci_load_config
,
396 .save_queue
= virtio_pci_save_queue
,
397 .load_queue
= virtio_pci_load_queue
,
398 .get_features
= virtio_pci_get_features
,
401 static void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
,
402 uint16_t vendor
, uint16_t device
,
403 uint16_t class_code
, uint8_t pif
)
410 config
= proxy
->pci_dev
.config
;
411 pci_config_set_vendor_id(config
, vendor
);
412 pci_config_set_device_id(config
, device
);
414 config
[0x08] = VIRTIO_PCI_ABI_VERSION
;
417 pci_config_set_class(config
, class_code
);
418 config
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
;
420 config
[0x2c] = vendor
& 0xFF;
421 config
[0x2d] = (vendor
>> 8) & 0xFF;
422 config
[0x2e] = vdev
->device_id
& 0xFF;
423 config
[0x2f] = (vdev
->device_id
>> 8) & 0xFF;
427 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
, 1, 0)) {
428 pci_register_bar(&proxy
->pci_dev
, 1,
429 msix_bar_size(&proxy
->pci_dev
),
430 PCI_BASE_ADDRESS_SPACE_MEMORY
,
435 proxy
->pci_dev
.config_write
= virtio_write_config
;
437 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
439 size
= 1 << qemu_fls(size
);
441 pci_register_bar(&proxy
->pci_dev
, 0, size
, PCI_BASE_ADDRESS_SPACE_IO
,
444 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
447 static int virtio_blk_init_pci(PCIDevice
*pci_dev
)
449 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
452 if (proxy
->class_code
!= PCI_CLASS_STORAGE_SCSI
&&
453 proxy
->class_code
!= PCI_CLASS_STORAGE_OTHER
)
454 proxy
->class_code
= PCI_CLASS_STORAGE_SCSI
;
457 qemu_error("virtio-blk-pci: drive property not set\n");
460 vdev
= virtio_blk_init(&pci_dev
->qdev
, proxy
->dinfo
);
461 vdev
->nvectors
= proxy
->nvectors
;
462 virtio_init_pci(proxy
, vdev
,
463 PCI_VENDOR_ID_REDHAT_QUMRANET
,
464 PCI_DEVICE_ID_VIRTIO_BLOCK
,
465 proxy
->class_code
, 0x00);
466 /* make the actual value visible */
467 proxy
->nvectors
= vdev
->nvectors
;
471 static int virtio_exit_pci(PCIDevice
*pci_dev
)
473 return msix_uninit(pci_dev
);
476 static int virtio_blk_exit_pci(PCIDevice
*pci_dev
)
478 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
480 drive_uninit(proxy
->dinfo
);
481 return virtio_exit_pci(pci_dev
);
484 static int virtio_console_init_pci(PCIDevice
*pci_dev
)
486 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
489 if (proxy
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
490 proxy
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
491 proxy
->class_code
!= PCI_CLASS_OTHERS
) /* qemu-kvm */
492 proxy
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
494 vdev
= virtio_console_init(&pci_dev
->qdev
);
498 virtio_init_pci(proxy
, vdev
,
499 PCI_VENDOR_ID_REDHAT_QUMRANET
,
500 PCI_DEVICE_ID_VIRTIO_CONSOLE
,
501 proxy
->class_code
, 0x00);
505 static int virtio_net_init_pci(PCIDevice
*pci_dev
)
507 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
510 vdev
= virtio_net_init(&pci_dev
->qdev
, &proxy
->nic
);
512 vdev
->nvectors
= proxy
->nvectors
;
513 virtio_init_pci(proxy
, vdev
,
514 PCI_VENDOR_ID_REDHAT_QUMRANET
,
515 PCI_DEVICE_ID_VIRTIO_NET
,
516 PCI_CLASS_NETWORK_ETHERNET
,
519 /* make the actual value visible */
520 proxy
->nvectors
= vdev
->nvectors
;
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 .romfile
= "pxe-virtio.bin",
565 .qdev
.props
= (Property
[]) {
566 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
567 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy
, nic
),
568 DEFINE_PROP_END_OF_LIST(),
570 .qdev
.reset
= virtio_pci_reset
,
572 .qdev
.name
= "virtio-console-pci",
573 .qdev
.size
= sizeof(VirtIOPCIProxy
),
574 .init
= virtio_console_init_pci
,
575 .exit
= virtio_exit_pci
,
576 .qdev
.props
= (Property
[]) {
577 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
578 DEFINE_PROP_END_OF_LIST(),
580 .qdev
.reset
= virtio_pci_reset
,
582 .qdev
.name
= "virtio-balloon-pci",
583 .qdev
.size
= sizeof(VirtIOPCIProxy
),
584 .init
= virtio_balloon_init_pci
,
585 .exit
= virtio_exit_pci
,
586 .qdev
.reset
= virtio_pci_reset
,
592 static void virtio_pci_register_devices(void)
594 pci_qdev_register_many(virtio_info
);
597 device_init(virtio_pci_register_devices
)