ppc/pnv: Add support for POWER8+ LPC Controller
[qemu.git] / tests / libqos / virtio.c
1 /*
2 * libqos virtio driver
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 #include "qemu/osdep.h"
11 #include "libqtest.h"
12 #include "libqos/virtio.h"
13 #include "standard-headers/linux/virtio_config.h"
14 #include "standard-headers/linux/virtio_ring.h"
15
16 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr)
17 {
18 return d->bus->config_readb(d, addr);
19 }
20
21 uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr)
22 {
23 return d->bus->config_readw(d, addr);
24 }
25
26 uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr)
27 {
28 return d->bus->config_readl(d, addr);
29 }
30
31 uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr)
32 {
33 return d->bus->config_readq(d, addr);
34 }
35
36 uint32_t qvirtio_get_features(QVirtioDevice *d)
37 {
38 return d->bus->get_features(d);
39 }
40
41 void qvirtio_set_features(QVirtioDevice *d, uint32_t features)
42 {
43 d->bus->set_features(d, features);
44 }
45
46 QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
47 QGuestAllocator *alloc, uint16_t index)
48 {
49 return d->bus->virtqueue_setup(d, alloc, index);
50 }
51
52 void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
53 QGuestAllocator *alloc)
54 {
55 return bus->virtqueue_cleanup(vq, alloc);
56 }
57
58 void qvirtio_reset(QVirtioDevice *d)
59 {
60 d->bus->set_status(d, 0);
61 g_assert_cmphex(d->bus->get_status(d), ==, 0);
62 }
63
64 void qvirtio_set_acknowledge(QVirtioDevice *d)
65 {
66 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_ACKNOWLEDGE);
67 g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE);
68 }
69
70 void qvirtio_set_driver(QVirtioDevice *d)
71 {
72 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER);
73 g_assert_cmphex(d->bus->get_status(d), ==,
74 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
75 }
76
77 void qvirtio_set_driver_ok(QVirtioDevice *d)
78 {
79 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER_OK);
80 g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_DRIVER_OK |
81 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
82 }
83
84 void qvirtio_wait_queue_isr(QVirtioDevice *d,
85 QVirtQueue *vq, gint64 timeout_us)
86 {
87 gint64 start_time = g_get_monotonic_time();
88
89 for (;;) {
90 clock_step(100);
91 if (d->bus->get_queue_isr_status(d, vq)) {
92 return;
93 }
94 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
95 }
96 }
97
98 /* Wait for the status byte at given guest memory address to be set
99 *
100 * The virtqueue interrupt must not be raised, making this useful for testing
101 * event_index functionality.
102 */
103 uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d,
104 QVirtQueue *vq,
105 uint64_t addr,
106 gint64 timeout_us)
107 {
108 gint64 start_time = g_get_monotonic_time();
109 uint8_t val;
110
111 while ((val = readb(addr)) == 0xff) {
112 clock_step(100);
113 g_assert(!d->bus->get_queue_isr_status(d, vq));
114 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
115 }
116 return val;
117 }
118
119 void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
120 {
121 gint64 start_time = g_get_monotonic_time();
122
123 for (;;) {
124 clock_step(100);
125 if (d->bus->get_config_isr_status(d)) {
126 return;
127 }
128 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
129 }
130 }
131
132 void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
133 {
134 int i;
135
136 vq->desc = addr;
137 vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
138 vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
139 + vq->align - 1) & ~(vq->align - 1));
140
141 for (i = 0; i < vq->size - 1; i++) {
142 /* vq->desc[i].addr */
143 writeq(vq->desc + (16 * i), 0);
144 /* vq->desc[i].next */
145 writew(vq->desc + (16 * i) + 14, i + 1);
146 }
147
148 /* vq->avail->flags */
149 writew(vq->avail, 0);
150 /* vq->avail->idx */
151 writew(vq->avail + 2, 0);
152 /* vq->avail->used_event */
153 writew(vq->avail + 4 + (2 * vq->size), 0);
154
155 /* vq->used->flags */
156 writew(vq->used, 0);
157 /* vq->used->avail_event */
158 writew(vq->used + 2 + sizeof(struct vring_used_elem) * vq->size, 0);
159 }
160
161 QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
162 QGuestAllocator *alloc, uint16_t elem)
163 {
164 int i;
165 QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
166
167 indirect->index = 0;
168 indirect->elem = elem;
169 indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
170
171 for (i = 0; i < elem - 1; ++i) {
172 /* indirect->desc[i].addr */
173 writeq(indirect->desc + (16 * i), 0);
174 /* indirect->desc[i].flags */
175 writew(indirect->desc + (16 * i) + 12, VRING_DESC_F_NEXT);
176 /* indirect->desc[i].next */
177 writew(indirect->desc + (16 * i) + 14, i + 1);
178 }
179
180 return indirect;
181 }
182
183 void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
184 uint32_t len, bool write)
185 {
186 uint16_t flags;
187
188 g_assert_cmpint(indirect->index, <, indirect->elem);
189
190 flags = readw(indirect->desc + (16 * indirect->index) + 12);
191
192 if (write) {
193 flags |= VRING_DESC_F_WRITE;
194 }
195
196 /* indirect->desc[indirect->index].addr */
197 writeq(indirect->desc + (16 * indirect->index), data);
198 /* indirect->desc[indirect->index].len */
199 writel(indirect->desc + (16 * indirect->index) + 8, len);
200 /* indirect->desc[indirect->index].flags */
201 writew(indirect->desc + (16 * indirect->index) + 12, flags);
202
203 indirect->index++;
204 }
205
206 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
207 bool next)
208 {
209 uint16_t flags = 0;
210 vq->num_free--;
211
212 if (write) {
213 flags |= VRING_DESC_F_WRITE;
214 }
215
216 if (next) {
217 flags |= VRING_DESC_F_NEXT;
218 }
219
220 /* vq->desc[vq->free_head].addr */
221 writeq(vq->desc + (16 * vq->free_head), data);
222 /* vq->desc[vq->free_head].len */
223 writel(vq->desc + (16 * vq->free_head) + 8, len);
224 /* vq->desc[vq->free_head].flags */
225 writew(vq->desc + (16 * vq->free_head) + 12, flags);
226
227 return vq->free_head++; /* Return and increase, in this order */
228 }
229
230 uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
231 {
232 g_assert(vq->indirect);
233 g_assert_cmpint(vq->size, >=, indirect->elem);
234 g_assert_cmpint(indirect->index, ==, indirect->elem);
235
236 vq->num_free--;
237
238 /* vq->desc[vq->free_head].addr */
239 writeq(vq->desc + (16 * vq->free_head), indirect->desc);
240 /* vq->desc[vq->free_head].len */
241 writel(vq->desc + (16 * vq->free_head) + 8,
242 sizeof(struct vring_desc) * indirect->elem);
243 /* vq->desc[vq->free_head].flags */
244 writew(vq->desc + (16 * vq->free_head) + 12, VRING_DESC_F_INDIRECT);
245
246 return vq->free_head++; /* Return and increase, in this order */
247 }
248
249 void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head)
250 {
251 /* vq->avail->idx */
252 uint16_t idx = readw(vq->avail + 2);
253 /* vq->used->flags */
254 uint16_t flags;
255 /* vq->used->avail_event */
256 uint16_t avail_event;
257
258 /* vq->avail->ring[idx % vq->size] */
259 writew(vq->avail + 4 + (2 * (idx % vq->size)), free_head);
260 /* vq->avail->idx */
261 writew(vq->avail + 2, idx + 1);
262
263 /* Must read after idx is updated */
264 flags = readw(vq->avail);
265 avail_event = readw(vq->used + 4 +
266 sizeof(struct vring_used_elem) * vq->size);
267
268 /* < 1 because we add elements to avail queue one by one */
269 if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
270 (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
271 d->bus->virtqueue_kick(d, vq);
272 }
273 }
274
275 void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx)
276 {
277 g_assert(vq->event);
278
279 /* vq->avail->used_event */
280 writew(vq->avail + 4 + (2 * vq->size), idx);
281 }