]> Git Repo - qemu.git/blob - tests/libqos/virtio.h
libqos: Added EVENT_IDX support
[qemu.git] / tests / libqos / virtio.h
1 /*
2  * libqos virtio definitions
3  *
4  * Copyright (c) 2014 Marc MarĂ­
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9
10 #ifndef LIBQOS_VIRTIO_H
11 #define LIBQOS_VIRTIO_H
12
13 #include "libqos/malloc.h"
14
15 #define QVIRTIO_VENDOR_ID       0x1AF4
16
17 #define QVIRTIO_RESET           0x0
18 #define QVIRTIO_ACKNOWLEDGE     0x1
19 #define QVIRTIO_DRIVER          0x2
20 #define QVIRTIO_DRIVER_OK       0x4
21
22 #define QVIRTIO_NET_DEVICE_ID   0x1
23 #define QVIRTIO_BLK_DEVICE_ID   0x2
24
25 #define QVIRTIO_F_NOTIFY_ON_EMPTY       0x01000000
26 #define QVIRTIO_F_ANY_LAYOUT            0x08000000
27 #define QVIRTIO_F_RING_INDIRECT_DESC    0x10000000
28 #define QVIRTIO_F_RING_EVENT_IDX        0x20000000
29 #define QVIRTIO_F_BAD_FEATURE           0x40000000
30
31 #define QVRING_DESC_F_NEXT      0x1
32 #define QVRING_DESC_F_WRITE     0x2
33 #define QVRING_DESC_F_INDIRECT  0x4
34
35 #define QVIRTIO_F_NOTIFY_ON_EMPTY       0x01000000
36 #define QVIRTIO_F_ANY_LAYOUT            0x08000000
37 #define QVIRTIO_F_RING_INDIRECT_DESC    0x10000000
38 #define QVIRTIO_F_RING_EVENT_IDX        0x20000000
39 #define QVIRTIO_F_BAD_FEATURE           0x40000000
40
41 #define QVRING_AVAIL_F_NO_INTERRUPT     1
42
43 #define QVRING_USED_F_NO_NOTIFY     1
44
45 typedef struct QVirtioDevice {
46     /* Device type */
47     uint16_t device_type;
48 } QVirtioDevice;
49
50 typedef struct QVRingDesc {
51     uint64_t addr;
52     uint32_t len;
53     uint16_t flags;
54     uint16_t next;
55 } QVRingDesc;
56
57 typedef struct QVRingAvail {
58     uint16_t flags;
59     uint16_t idx;
60     uint16_t ring[0]; /* This is an array of uint16_t */
61     uint16_t used_event;
62 } QVRingAvail;
63
64 typedef struct QVRingUsedElem {
65     uint32_t id;
66     uint32_t len;
67 } QVRingUsedElem;
68
69 typedef struct QVRingUsed {
70     uint16_t flags;
71     uint16_t idx;
72     QVRingUsedElem ring[0]; /* This is an array of QVRingUsedElem structs */
73     uint16_t avail_event;
74 } QVRingUsed;
75
76 typedef struct QVirtQueue {
77     uint64_t desc; /* This points to an array of QVRingDesc */
78     uint64_t avail; /* This points to a QVRingAvail */
79     uint64_t used; /* This points to a QVRingDesc */
80     uint16_t index;
81     uint32_t size;
82     uint32_t free_head;
83     uint32_t num_free;
84     uint32_t align;
85     bool indirect;
86     bool event;
87 } QVirtQueue;
88
89 typedef struct QVRingIndirectDesc {
90     uint64_t desc; /* This points to an array fo QVRingDesc */
91     uint16_t index;
92     uint16_t elem;
93 } QVRingIndirectDesc;
94
95 typedef struct QVirtioBus {
96     uint8_t (*config_readb)(QVirtioDevice *d, void *addr);
97     uint16_t (*config_readw)(QVirtioDevice *d, void *addr);
98     uint32_t (*config_readl)(QVirtioDevice *d, void *addr);
99     uint64_t (*config_readq)(QVirtioDevice *d, void *addr);
100
101     /* Get features of the device */
102     uint32_t (*get_features)(QVirtioDevice *d);
103
104     /* Set features of the device */
105     void (*set_features)(QVirtioDevice *d, uint32_t features);
106
107     /* Get features of the guest */
108     uint32_t (*get_guest_features)(QVirtioDevice *d);
109
110     /* Get status of the device */
111     uint8_t (*get_status)(QVirtioDevice *d);
112
113     /* Set status of the device  */
114     void (*set_status)(QVirtioDevice *d, uint8_t status);
115
116     /* Get the queue ISR status of the device */
117     bool (*get_queue_isr_status)(QVirtioDevice *d, QVirtQueue *vq);
118
119     /* Get the configuration ISR status of the device */
120     bool (*get_config_isr_status)(QVirtioDevice *d);
121
122     /* Select a queue to work on */
123     void (*queue_select)(QVirtioDevice *d, uint16_t index);
124
125     /* Get the size of the selected queue */
126     uint16_t (*get_queue_size)(QVirtioDevice *d);
127
128     /* Set the address of the selected queue */
129     void (*set_queue_address)(QVirtioDevice *d, uint32_t pfn);
130
131     /* Setup the virtqueue specified by index */
132     QVirtQueue *(*virtqueue_setup)(QVirtioDevice *d, QGuestAllocator *alloc,
133                                                                 uint16_t index);
134
135     /* Notify changes in virtqueue */
136     void (*virtqueue_kick)(QVirtioDevice *d, QVirtQueue *vq);
137 } QVirtioBus;
138
139 static inline uint32_t qvring_size(uint32_t num, uint32_t align)
140 {
141     return ((sizeof(struct QVRingDesc) * num + sizeof(uint16_t) * (3 + num)
142         + align - 1) & ~(align - 1))
143         + sizeof(uint16_t) * 3 + sizeof(struct QVRingUsedElem) * num;
144 }
145
146 uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d,
147                                                                 void *addr);
148 uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d,
149                                                                 void *addr);
150 uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d,
151                                                                 void *addr);
152 uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d,
153                                                                 void *addr);
154 uint32_t qvirtio_get_features(const QVirtioBus *bus, QVirtioDevice *d);
155 void qvirtio_set_features(const QVirtioBus *bus, QVirtioDevice *d,
156                                                             uint32_t features);
157
158 void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d);
159 void qvirtio_set_acknowledge(const QVirtioBus *bus, QVirtioDevice *d);
160 void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d);
161 void qvirtio_set_driver_ok(const QVirtioBus *bus, QVirtioDevice *d);
162
163 bool qvirtio_wait_queue_isr(const QVirtioBus *bus, QVirtioDevice *d,
164                                             QVirtQueue *vq, uint64_t timeout);
165 bool qvirtio_wait_config_isr(const QVirtioBus *bus, QVirtioDevice *d,
166                                                             uint64_t timeout);
167 QVirtQueue *qvirtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d,
168                                         QGuestAllocator *alloc, uint16_t index);
169
170 void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr);
171 QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
172                                         QGuestAllocator *alloc, uint16_t elem);
173 void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
174                                                     uint32_t len, bool write);
175 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
176                                                                     bool next);
177 uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect);
178 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
179                                                             uint32_t free_head);
180
181 void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx);
182 #endif
This page took 0.032568 seconds and 4 git commands to generate.