root / hw / virtio.h @ 5703c174
History | View | Annotate | Download (3.8 kB)
1 | 967f97fa | aliguori | /*
|
---|---|---|---|
2 | 967f97fa | aliguori | * Virtio Support
|
3 | 967f97fa | aliguori | *
|
4 | 967f97fa | aliguori | * Copyright IBM, Corp. 2007
|
5 | 967f97fa | aliguori | *
|
6 | 967f97fa | aliguori | * Authors:
|
7 | 967f97fa | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | 967f97fa | aliguori | *
|
9 | 967f97fa | aliguori | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | 967f97fa | aliguori | * the COPYING file in the top-level directory.
|
11 | 967f97fa | aliguori | *
|
12 | 967f97fa | aliguori | */
|
13 | 967f97fa | aliguori | |
14 | 967f97fa | aliguori | #ifndef _QEMU_VIRTIO_H
|
15 | 967f97fa | aliguori | #define _QEMU_VIRTIO_H
|
16 | 967f97fa | aliguori | |
17 | 967f97fa | aliguori | #include "hw.h" |
18 | 967f97fa | aliguori | #include "pci.h" |
19 | 967f97fa | aliguori | |
20 | 967f97fa | aliguori | /* from Linux's linux/virtio_config.h */
|
21 | 967f97fa | aliguori | |
22 | 967f97fa | aliguori | /* Status byte for guest to report progress, and synchronize features. */
|
23 | 967f97fa | aliguori | /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
|
24 | 967f97fa | aliguori | #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1 |
25 | 967f97fa | aliguori | /* We have found a driver for the device. */
|
26 | 967f97fa | aliguori | #define VIRTIO_CONFIG_S_DRIVER 2 |
27 | 967f97fa | aliguori | /* Driver has used its parts of the config, and is happy */
|
28 | 967f97fa | aliguori | #define VIRTIO_CONFIG_S_DRIVER_OK 4 |
29 | 967f97fa | aliguori | /* We've given up on this device. */
|
30 | 967f97fa | aliguori | #define VIRTIO_CONFIG_S_FAILED 0x80 |
31 | 967f97fa | aliguori | |
32 | 967f97fa | aliguori | /* We notify when the ring is completely used, even if the guest is supressing
|
33 | 967f97fa | aliguori | * callbacks */
|
34 | 967f97fa | aliguori | #define VIRTIO_F_NOTIFY_ON_EMPTY 24 |
35 | 967f97fa | aliguori | |
36 | 967f97fa | aliguori | /* from Linux's linux/virtio_ring.h */
|
37 | 967f97fa | aliguori | |
38 | 967f97fa | aliguori | /* This marks a buffer as continuing via the next field. */
|
39 | 967f97fa | aliguori | #define VRING_DESC_F_NEXT 1 |
40 | 967f97fa | aliguori | /* This marks a buffer as write-only (otherwise read-only). */
|
41 | 967f97fa | aliguori | #define VRING_DESC_F_WRITE 2 |
42 | 967f97fa | aliguori | |
43 | 967f97fa | aliguori | /* This means don't notify other side when buffer added. */
|
44 | 967f97fa | aliguori | #define VRING_USED_F_NO_NOTIFY 1 |
45 | 967f97fa | aliguori | /* This means don't interrupt guest when buffer consumed. */
|
46 | 967f97fa | aliguori | #define VRING_AVAIL_F_NO_INTERRUPT 1 |
47 | 967f97fa | aliguori | |
48 | 967f97fa | aliguori | struct VirtQueue;
|
49 | 967f97fa | aliguori | |
50 | f46f15bc | aliguori | static inline target_phys_addr_t vring_align(target_phys_addr_t addr, |
51 | f46f15bc | aliguori | unsigned long align) |
52 | f46f15bc | aliguori | { |
53 | f46f15bc | aliguori | return (addr + align - 1) & ~(align - 1); |
54 | f46f15bc | aliguori | } |
55 | f46f15bc | aliguori | |
56 | 967f97fa | aliguori | typedef struct VirtQueue VirtQueue; |
57 | 967f97fa | aliguori | typedef struct VirtIODevice VirtIODevice; |
58 | 967f97fa | aliguori | |
59 | 967f97fa | aliguori | #define VIRTQUEUE_MAX_SIZE 1024 |
60 | 967f97fa | aliguori | |
61 | 967f97fa | aliguori | typedef struct VirtQueueElement |
62 | 967f97fa | aliguori | { |
63 | 967f97fa | aliguori | unsigned int index; |
64 | 967f97fa | aliguori | unsigned int out_num; |
65 | 967f97fa | aliguori | unsigned int in_num; |
66 | 967f97fa | aliguori | target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE]; |
67 | 967f97fa | aliguori | struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
|
68 | 967f97fa | aliguori | struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
|
69 | 967f97fa | aliguori | } VirtQueueElement; |
70 | 967f97fa | aliguori | |
71 | 967f97fa | aliguori | #define VIRTIO_PCI_QUEUE_MAX 16 |
72 | 967f97fa | aliguori | |
73 | 967f97fa | aliguori | struct VirtIODevice
|
74 | 967f97fa | aliguori | { |
75 | 967f97fa | aliguori | PCIDevice pci_dev; |
76 | 967f97fa | aliguori | const char *name; |
77 | 967f97fa | aliguori | uint32_t addr; |
78 | 967f97fa | aliguori | uint8_t status; |
79 | 967f97fa | aliguori | uint8_t isr; |
80 | 967f97fa | aliguori | uint16_t queue_sel; |
81 | 967f97fa | aliguori | uint32_t features; |
82 | 967f97fa | aliguori | size_t config_len; |
83 | 967f97fa | aliguori | void *config;
|
84 | 967f97fa | aliguori | uint32_t (*get_features)(VirtIODevice *vdev); |
85 | 967f97fa | aliguori | void (*set_features)(VirtIODevice *vdev, uint32_t val);
|
86 | 967f97fa | aliguori | void (*get_config)(VirtIODevice *vdev, uint8_t *config);
|
87 | 967f97fa | aliguori | void (*set_config)(VirtIODevice *vdev, const uint8_t *config); |
88 | 967f97fa | aliguori | void (*reset)(VirtIODevice *vdev);
|
89 | 967f97fa | aliguori | VirtQueue *vq; |
90 | 967f97fa | aliguori | }; |
91 | 967f97fa | aliguori | |
92 | 967f97fa | aliguori | VirtIODevice *virtio_init_pci(PCIBus *bus, const char *name, |
93 | 967f97fa | aliguori | uint16_t vendor, uint16_t device, |
94 | 967f97fa | aliguori | uint16_t subvendor, uint16_t subdevice, |
95 | 967f97fa | aliguori | uint8_t class_code, uint8_t subclass_code, |
96 | 967f97fa | aliguori | uint8_t pif, size_t config_size, |
97 | 967f97fa | aliguori | size_t struct_size); |
98 | 967f97fa | aliguori | |
99 | 967f97fa | aliguori | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
|
100 | 967f97fa | aliguori | void (*handle_output)(VirtIODevice *,
|
101 | 967f97fa | aliguori | VirtQueue *)); |
102 | 967f97fa | aliguori | |
103 | 967f97fa | aliguori | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, |
104 | 967f97fa | aliguori | unsigned int len); |
105 | 967f97fa | aliguori | void virtqueue_flush(VirtQueue *vq, unsigned int count); |
106 | 967f97fa | aliguori | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, |
107 | 967f97fa | aliguori | unsigned int len, unsigned int idx); |
108 | 967f97fa | aliguori | |
109 | 967f97fa | aliguori | int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
|
110 | 967f97fa | aliguori | int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes); |
111 | 967f97fa | aliguori | |
112 | 967f97fa | aliguori | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
|
113 | 967f97fa | aliguori | |
114 | 967f97fa | aliguori | void virtio_save(VirtIODevice *vdev, QEMUFile *f);
|
115 | 967f97fa | aliguori | |
116 | 967f97fa | aliguori | void virtio_load(VirtIODevice *vdev, QEMUFile *f);
|
117 | 967f97fa | aliguori | |
118 | 967f97fa | aliguori | void virtio_notify_config(VirtIODevice *vdev);
|
119 | 967f97fa | aliguori | |
120 | 967f97fa | aliguori | void virtio_queue_set_notification(VirtQueue *vq, int enable); |
121 | 967f97fa | aliguori | |
122 | 967f97fa | aliguori | int virtio_queue_ready(VirtQueue *vq);
|
123 | 967f97fa | aliguori | |
124 | 967f97fa | aliguori | int virtio_queue_empty(VirtQueue *vq);
|
125 | 967f97fa | aliguori | |
126 | 967f97fa | aliguori | #endif |