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.
23 /* from Linux's linux/virtio_pci.h */
25 /* A 32-bit r/o bitmask of the features supported by the host */
26 #define VIRTIO_PCI_HOST_FEATURES 0
28 /* A 32-bit r/w bitmask of features activated by the guest */
29 #define VIRTIO_PCI_GUEST_FEATURES 4
31 /* A 32-bit r/w PFN for the currently selected queue */
32 #define VIRTIO_PCI_QUEUE_PFN 8
34 /* A 16-bit r/o queue size for the currently selected queue */
35 #define VIRTIO_PCI_QUEUE_NUM 12
37 /* A 16-bit r/w queue selector */
38 #define VIRTIO_PCI_QUEUE_SEL 14
40 /* A 16-bit r/w queue notifier */
41 #define VIRTIO_PCI_QUEUE_NOTIFY 16
43 /* An 8-bit device status register. */
44 #define VIRTIO_PCI_STATUS 18
46 /* An 8-bit r/o interrupt status register. Reading the value will return the
47 * current contents of the ISR and will also clear it. This is effectively
48 * a read-and-acknowledge. */
49 #define VIRTIO_PCI_ISR 19
51 /* MSI-X registers: only enabled if MSI-X is enabled. */
52 /* A 16-bit vector for configuration changes. */
53 #define VIRTIO_MSI_CONFIG_VECTOR 20
54 /* A 16-bit vector for selected queue notifications. */
55 #define VIRTIO_MSI_QUEUE_VECTOR 22
57 /* Config space size */
58 #define VIRTIO_PCI_CONFIG_NOMSI 20
59 #define VIRTIO_PCI_CONFIG_MSI 24
60 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
61 VIRTIO_PCI_CONFIG_MSI : \
62 VIRTIO_PCI_CONFIG_NOMSI)
64 /* The remaining space is defined by each driver as the per-driver
65 * configuration space */
66 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
67 VIRTIO_PCI_CONFIG_MSI : \
68 VIRTIO_PCI_CONFIG_NOMSI)
70 /* Virtio ABI version, if we increment this, we break the guest driver. */
71 #define VIRTIO_PCI_ABI_VERSION 0
73 /* How many bits to shift physical queue address written to QUEUE_PFN.
74 * 12 is historical, and due to x86 page size. */
75 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
77 /* QEMU doesn't strictly need write barriers since everything runs in
78 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
79 * KVM or if kqemu gets SMP support.
81 #define wmb() do { } while (0)
93 static void virtio_pci_notify(void *opaque
, uint16_t vector
)
95 VirtIOPCIProxy
*proxy
= opaque
;
96 if (msix_enabled(&proxy
->pci_dev
))
97 msix_notify(&proxy
->pci_dev
, vector
);
99 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
102 static void virtio_pci_save_config(void * opaque
, QEMUFile
*f
)
104 VirtIOPCIProxy
*proxy
= opaque
;
105 pci_device_save(&proxy
->pci_dev
, f
);
106 msix_save(&proxy
->pci_dev
, f
);
107 if (msix_present(&proxy
->pci_dev
))
108 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
111 static void virtio_pci_save_queue(void * opaque
, int n
, QEMUFile
*f
)
113 VirtIOPCIProxy
*proxy
= opaque
;
114 if (msix_present(&proxy
->pci_dev
))
115 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
118 static int virtio_pci_load_config(void * opaque
, QEMUFile
*f
)
120 VirtIOPCIProxy
*proxy
= opaque
;
122 ret
= pci_device_load(&proxy
->pci_dev
, f
);
126 msix_load(&proxy
->pci_dev
, f
);
127 if (msix_present(&proxy
->pci_dev
)) {
128 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
130 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
132 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
133 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
138 static int virtio_pci_load_queue(void * opaque
, int n
, QEMUFile
*f
)
140 VirtIOPCIProxy
*proxy
= opaque
;
142 if (msix_present(&proxy
->pci_dev
)) {
143 qemu_get_be16s(f
, &vector
);
145 vector
= VIRTIO_NO_VECTOR
;
147 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
148 if (vector
!= VIRTIO_NO_VECTOR
) {
149 return msix_vector_use(&proxy
->pci_dev
, vector
);
154 static void virtio_pci_reset(void *opaque
)
156 VirtIOPCIProxy
*proxy
= opaque
;
157 virtio_reset(proxy
->vdev
);
158 msix_reset(&proxy
->pci_dev
);
161 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
163 VirtIOPCIProxy
*proxy
= opaque
;
164 VirtIODevice
*vdev
= proxy
->vdev
;
165 target_phys_addr_t pa
;
168 case VIRTIO_PCI_GUEST_FEATURES
:
169 /* Guest does not negotiate properly? We have to assume nothing. */
170 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
171 if (vdev
->bad_features
)
172 val
= vdev
->bad_features(vdev
);
176 if (vdev
->set_features
)
177 vdev
->set_features(vdev
, val
);
178 vdev
->features
= val
;
180 case VIRTIO_PCI_QUEUE_PFN
:
181 pa
= (target_phys_addr_t
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
183 virtio_pci_reset(proxy
);
185 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
187 case VIRTIO_PCI_QUEUE_SEL
:
188 if (val
< VIRTIO_PCI_QUEUE_MAX
)
189 vdev
->queue_sel
= val
;
191 case VIRTIO_PCI_QUEUE_NOTIFY
:
192 virtio_queue_notify(vdev
, val
);
194 case VIRTIO_PCI_STATUS
:
195 vdev
->status
= val
& 0xFF;
196 if (vdev
->status
== 0)
197 virtio_pci_reset(proxy
);
199 case VIRTIO_MSI_CONFIG_VECTOR
:
200 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
201 /* Make it possible for guest to discover an error took place. */
202 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
203 val
= VIRTIO_NO_VECTOR
;
204 vdev
->config_vector
= val
;
206 case VIRTIO_MSI_QUEUE_VECTOR
:
207 msix_vector_unuse(&proxy
->pci_dev
,
208 virtio_queue_vector(vdev
, vdev
->queue_sel
));
209 /* Make it possible for guest to discover an error took place. */
210 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
211 val
= VIRTIO_NO_VECTOR
;
212 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
215 fprintf(stderr
, "%s: unexpected address 0x%x value 0x%x\n",
216 __func__
, addr
, val
);
221 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
223 VirtIODevice
*vdev
= proxy
->vdev
;
224 uint32_t ret
= 0xFFFFFFFF;
227 case VIRTIO_PCI_HOST_FEATURES
:
228 ret
= vdev
->get_features(vdev
);
229 ret
|= (1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
230 ret
|= (1 << VIRTIO_RING_F_INDIRECT_DESC
);
231 ret
|= (1 << VIRTIO_F_BAD_FEATURE
);
233 case VIRTIO_PCI_GUEST_FEATURES
:
234 ret
= vdev
->features
;
236 case VIRTIO_PCI_QUEUE_PFN
:
237 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
238 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
240 case VIRTIO_PCI_QUEUE_NUM
:
241 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
243 case VIRTIO_PCI_QUEUE_SEL
:
244 ret
= vdev
->queue_sel
;
246 case VIRTIO_PCI_STATUS
:
250 /* reading from the ISR also clears it. */
253 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
255 case VIRTIO_MSI_CONFIG_VECTOR
:
256 ret
= vdev
->config_vector
;
258 case VIRTIO_MSI_QUEUE_VECTOR
:
259 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
268 static uint32_t virtio_pci_config_readb(void *opaque
, uint32_t addr
)
270 VirtIOPCIProxy
*proxy
= opaque
;
271 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
274 return virtio_ioport_read(proxy
, addr
);
276 return virtio_config_readb(proxy
->vdev
, addr
);
279 static uint32_t virtio_pci_config_readw(void *opaque
, uint32_t addr
)
281 VirtIOPCIProxy
*proxy
= opaque
;
282 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
285 return virtio_ioport_read(proxy
, addr
);
287 return virtio_config_readw(proxy
->vdev
, addr
);
290 static uint32_t virtio_pci_config_readl(void *opaque
, uint32_t addr
)
292 VirtIOPCIProxy
*proxy
= opaque
;
293 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
296 return virtio_ioport_read(proxy
, addr
);
298 return virtio_config_readl(proxy
->vdev
, addr
);
301 static void virtio_pci_config_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
303 VirtIOPCIProxy
*proxy
= opaque
;
304 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
307 virtio_ioport_write(proxy
, addr
, val
);
311 virtio_config_writeb(proxy
->vdev
, addr
, val
);
314 static void virtio_pci_config_writew(void *opaque
, uint32_t addr
, uint32_t val
)
316 VirtIOPCIProxy
*proxy
= opaque
;
317 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
320 virtio_ioport_write(proxy
, addr
, val
);
324 virtio_config_writew(proxy
->vdev
, addr
, val
);
327 static void virtio_pci_config_writel(void *opaque
, uint32_t addr
, uint32_t val
)
329 VirtIOPCIProxy
*proxy
= opaque
;
330 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
333 virtio_ioport_write(proxy
, addr
, val
);
337 virtio_config_writel(proxy
->vdev
, addr
, val
);
340 static void virtio_map(PCIDevice
*pci_dev
, int region_num
,
341 uint32_t addr
, uint32_t size
, int type
)
343 VirtIOPCIProxy
*proxy
= container_of(pci_dev
, VirtIOPCIProxy
, pci_dev
);
344 VirtIODevice
*vdev
= proxy
->vdev
;
345 unsigned config_len
= VIRTIO_PCI_REGION_SIZE(pci_dev
) + vdev
->config_len
;
349 register_ioport_write(addr
, config_len
, 1, virtio_pci_config_writeb
, proxy
);
350 register_ioport_write(addr
, config_len
, 2, virtio_pci_config_writew
, proxy
);
351 register_ioport_write(addr
, config_len
, 4, virtio_pci_config_writel
, proxy
);
352 register_ioport_read(addr
, config_len
, 1, virtio_pci_config_readb
, proxy
);
353 register_ioport_read(addr
, config_len
, 2, virtio_pci_config_readw
, proxy
);
354 register_ioport_read(addr
, config_len
, 4, virtio_pci_config_readl
, proxy
);
356 if (vdev
->config_len
)
357 vdev
->get_config(vdev
, vdev
->config
);
360 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
361 uint32_t val
, int len
)
363 pci_default_write_config(pci_dev
, address
, val
, len
);
364 msix_write_config(pci_dev
, address
, val
, len
);
367 static const VirtIOBindings virtio_pci_bindings
= {
368 .notify
= virtio_pci_notify
,
369 .save_config
= virtio_pci_save_config
,
370 .load_config
= virtio_pci_load_config
,
371 .save_queue
= virtio_pci_save_queue
,
372 .load_queue
= virtio_pci_load_queue
,
375 static void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
,
376 uint16_t vendor
, uint16_t device
,
377 uint16_t class_code
, uint8_t pif
)
384 config
= proxy
->pci_dev
.config
;
385 pci_config_set_vendor_id(config
, vendor
);
386 pci_config_set_device_id(config
, device
);
388 config
[0x08] = VIRTIO_PCI_ABI_VERSION
;
391 pci_config_set_class(config
, class_code
);
392 config
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
;
394 config
[0x2c] = vendor
& 0xFF;
395 config
[0x2d] = (vendor
>> 8) & 0xFF;
396 config
[0x2e] = vdev
->device_id
& 0xFF;
397 config
[0x2f] = (vdev
->device_id
>> 8) & 0xFF;
401 if (vdev
->nvectors
&& !msix_init(&proxy
->pci_dev
, vdev
->nvectors
, 1, 0)) {
402 pci_register_bar(&proxy
->pci_dev
, 1,
403 msix_bar_size(&proxy
->pci_dev
),
404 PCI_ADDRESS_SPACE_MEM
,
406 proxy
->pci_dev
.config_write
= virtio_write_config
;
407 proxy
->pci_dev
.unregister
= msix_uninit
;
411 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
413 size
= 1 << qemu_fls(size
);
415 pci_register_bar(&proxy
->pci_dev
, 0, size
, PCI_ADDRESS_SPACE_IO
,
418 qemu_register_reset(virtio_pci_reset
, proxy
);
420 virtio_bind_device(vdev
, &virtio_pci_bindings
, proxy
);
423 static void virtio_blk_init_pci_with_class(PCIDevice
*pci_dev
,
426 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
429 vdev
= virtio_blk_init(&pci_dev
->qdev
);
430 virtio_init_pci(proxy
, vdev
,
431 PCI_VENDOR_ID_REDHAT_QUMRANET
,
432 PCI_DEVICE_ID_VIRTIO_BLOCK
,
436 static void virtio_blk_init_pci(PCIDevice
*pci_dev
)
438 virtio_blk_init_pci_with_class(pci_dev
, PCI_CLASS_STORAGE_SCSI
);
441 static void virtio_blk_init_pci_0_10(PCIDevice
*pci_dev
)
443 virtio_blk_init_pci_with_class(pci_dev
, PCI_CLASS_STORAGE_OTHER
);
446 static void virtio_console_init_pci_with_class(PCIDevice
*pci_dev
,
449 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
452 vdev
= virtio_console_init(&pci_dev
->qdev
);
453 virtio_init_pci(proxy
, vdev
,
454 PCI_VENDOR_ID_REDHAT_QUMRANET
,
455 PCI_DEVICE_ID_VIRTIO_CONSOLE
,
459 static void virtio_console_init_pci(PCIDevice
*pci_dev
)
461 virtio_console_init_pci_with_class(pci_dev
, PCI_CLASS_SERIAL_OTHER
);
464 static void virtio_console_init_pci_0_10(PCIDevice
*pci_dev
)
466 virtio_console_init_pci_with_class(pci_dev
, PCI_CLASS_DISPLAY_OTHER
);
469 static void virtio_net_init_pci(PCIDevice
*pci_dev
)
471 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
474 vdev
= virtio_net_init(&pci_dev
->qdev
);
475 virtio_init_pci(proxy
, vdev
,
476 PCI_VENDOR_ID_REDHAT_QUMRANET
,
477 PCI_DEVICE_ID_VIRTIO_NET
,
478 PCI_CLASS_NETWORK_ETHERNET
,
482 static void virtio_balloon_init_pci(PCIDevice
*pci_dev
)
484 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
487 vdev
= virtio_balloon_init(&pci_dev
->qdev
);
488 virtio_init_pci(proxy
, vdev
,
489 PCI_VENDOR_ID_REDHAT_QUMRANET
,
490 PCI_DEVICE_ID_VIRTIO_BALLOON
,
491 PCI_CLASS_MEMORY_RAM
,
495 static PCIDeviceInfo virtio_info
[] = {
497 .qdev
.name
= "virtio-blk-pci",
498 .qdev
.size
= sizeof(VirtIOPCIProxy
),
499 .init
= virtio_blk_init_pci
,
501 .qdev
.name
= "virtio-net-pci",
502 .qdev
.size
= sizeof(VirtIOPCIProxy
),
503 .init
= virtio_net_init_pci
,
505 .qdev
.name
= "virtio-console-pci",
506 .qdev
.size
= sizeof(VirtIOPCIProxy
),
507 .init
= virtio_console_init_pci
,
509 .qdev
.name
= "virtio-balloon-pci",
510 .qdev
.size
= sizeof(VirtIOPCIProxy
),
511 .init
= virtio_balloon_init_pci
,
513 /* For compatibility with 0.10 */
514 .qdev
.name
= "virtio-blk-pci-0-10",
515 .qdev
.size
= sizeof(VirtIOPCIProxy
),
516 .init
= virtio_blk_init_pci_0_10
,
518 .qdev
.name
= "virtio-console-pci-0-10",
519 .qdev
.size
= sizeof(VirtIOPCIProxy
),
520 .init
= virtio_console_init_pci_0_10
,
526 static void virtio_pci_register_devices(void)
528 pci_qdev_register_many(virtio_info
);
531 device_init(virtio_pci_register_devices
)