]>
Commit | Line | Data |
---|---|---|
46e0cf76 MM |
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 | ||
681c28a3 | 10 | #include "qemu/osdep.h" |
46e0cf76 MM |
11 | #include "libqtest.h" |
12 | #include "libqos/virtio.h" | |
1373a4c2 | 13 | #include "standard-headers/linux/virtio_config.h" |
ee3b850a | 14 | #include "standard-headers/linux/virtio_ring.h" |
46e0cf76 | 15 | |
6b9cdf4c | 16 | uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr) |
46e0cf76 | 17 | { |
6b9cdf4c | 18 | return d->bus->config_readb(d, addr); |
46e0cf76 MM |
19 | } |
20 | ||
6b9cdf4c | 21 | uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr) |
46e0cf76 | 22 | { |
6b9cdf4c | 23 | return d->bus->config_readw(d, addr); |
46e0cf76 MM |
24 | } |
25 | ||
6b9cdf4c | 26 | uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr) |
46e0cf76 | 27 | { |
6b9cdf4c | 28 | return d->bus->config_readl(d, addr); |
46e0cf76 MM |
29 | } |
30 | ||
6b9cdf4c | 31 | uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr) |
46e0cf76 | 32 | { |
6b9cdf4c | 33 | return d->bus->config_readq(d, addr); |
46e0cf76 MM |
34 | } |
35 | ||
6b9cdf4c | 36 | uint32_t qvirtio_get_features(QVirtioDevice *d) |
bf3c63d2 | 37 | { |
6b9cdf4c | 38 | return d->bus->get_features(d); |
bf3c63d2 MM |
39 | } |
40 | ||
6b9cdf4c | 41 | void qvirtio_set_features(QVirtioDevice *d, uint32_t features) |
bf3c63d2 | 42 | { |
6b9cdf4c | 43 | d->bus->set_features(d, features); |
bf3c63d2 MM |
44 | } |
45 | ||
6b9cdf4c LV |
46 | QVirtQueue *qvirtqueue_setup(QVirtioDevice *d, |
47 | QGuestAllocator *alloc, uint16_t index) | |
bf3c63d2 | 48 | { |
6b9cdf4c | 49 | return d->bus->virtqueue_setup(d, alloc, index); |
bf3c63d2 MM |
50 | } |
51 | ||
f1d3b991 SH |
52 | void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq, |
53 | QGuestAllocator *alloc) | |
54 | { | |
55 | return bus->virtqueue_cleanup(vq, alloc); | |
56 | } | |
57 | ||
6b9cdf4c | 58 | void qvirtio_reset(QVirtioDevice *d) |
46e0cf76 | 59 | { |
6b9cdf4c LV |
60 | d->bus->set_status(d, 0); |
61 | g_assert_cmphex(d->bus->get_status(d), ==, 0); | |
46e0cf76 MM |
62 | } |
63 | ||
6b9cdf4c | 64 | void qvirtio_set_acknowledge(QVirtioDevice *d) |
46e0cf76 | 65 | { |
6b9cdf4c LV |
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); | |
46e0cf76 MM |
68 | } |
69 | ||
6b9cdf4c | 70 | void qvirtio_set_driver(QVirtioDevice *d) |
46e0cf76 | 71 | { |
6b9cdf4c LV |
72 | d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER); |
73 | g_assert_cmphex(d->bus->get_status(d), ==, | |
1373a4c2 | 74 | VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE); |
46e0cf76 | 75 | } |
bf3c63d2 | 76 | |
6b9cdf4c | 77 | void qvirtio_set_driver_ok(QVirtioDevice *d) |
bf3c63d2 | 78 | { |
6b9cdf4c LV |
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 | | |
1373a4c2 | 81 | VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE); |
bf3c63d2 MM |
82 | } |
83 | ||
6b9cdf4c | 84 | void qvirtio_wait_queue_isr(QVirtioDevice *d, |
70556264 | 85 | QVirtQueue *vq, gint64 timeout_us) |
58368113 | 86 | { |
70556264 SH |
87 | gint64 start_time = g_get_monotonic_time(); |
88 | ||
89 | for (;;) { | |
6cd14054 | 90 | clock_step(100); |
6b9cdf4c | 91 | if (d->bus->get_queue_isr_status(d, vq)) { |
70556264 | 92 | return; |
58368113 | 93 | } |
70556264 SH |
94 | g_assert(g_get_monotonic_time() - start_time <= timeout_us); |
95 | } | |
58368113 MM |
96 | } |
97 | ||
e8c81b4d SH |
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 | */ | |
6b9cdf4c | 103 | uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d, |
e8c81b4d SH |
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); | |
6b9cdf4c | 113 | g_assert(!d->bus->get_queue_isr_status(d, vq)); |
e8c81b4d SH |
114 | g_assert(g_get_monotonic_time() - start_time <= timeout_us); |
115 | } | |
116 | return val; | |
117 | } | |
118 | ||
e77abbe9 SH |
119 | /* |
120 | * qvirtio_wait_used_elem: | |
121 | * @desc_idx: The next expected vq->desc[] index in the used ring | |
122 | * @timeout_us: How many microseconds to wait before failing | |
123 | * | |
124 | * This function waits for the next completed request on the used ring. | |
125 | */ | |
126 | void qvirtio_wait_used_elem(QVirtioDevice *d, | |
127 | QVirtQueue *vq, | |
128 | uint32_t desc_idx, | |
129 | gint64 timeout_us) | |
130 | { | |
131 | gint64 start_time = g_get_monotonic_time(); | |
132 | ||
133 | for (;;) { | |
134 | uint32_t got_desc_idx; | |
135 | ||
136 | clock_step(100); | |
137 | ||
138 | if (d->bus->get_queue_isr_status(d, vq) && | |
139 | qvirtqueue_get_buf(vq, &got_desc_idx)) { | |
140 | g_assert_cmpint(got_desc_idx, ==, desc_idx); | |
141 | return; | |
142 | } | |
143 | ||
144 | g_assert(g_get_monotonic_time() - start_time <= timeout_us); | |
145 | } | |
146 | } | |
147 | ||
6b9cdf4c | 148 | void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us) |
bf3c63d2 | 149 | { |
70556264 SH |
150 | gint64 start_time = g_get_monotonic_time(); |
151 | ||
152 | for (;;) { | |
6cd14054 | 153 | clock_step(100); |
6b9cdf4c | 154 | if (d->bus->get_config_isr_status(d)) { |
70556264 | 155 | return; |
bf3c63d2 | 156 | } |
70556264 SH |
157 | g_assert(g_get_monotonic_time() - start_time <= timeout_us); |
158 | } | |
bf3c63d2 MM |
159 | } |
160 | ||
161 | void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr) | |
162 | { | |
163 | int i; | |
164 | ||
165 | vq->desc = addr; | |
780b11a0 | 166 | vq->avail = vq->desc + vq->size * sizeof(struct vring_desc); |
bf3c63d2 MM |
167 | vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size) |
168 | + vq->align - 1) & ~(vq->align - 1)); | |
169 | ||
170 | for (i = 0; i < vq->size - 1; i++) { | |
171 | /* vq->desc[i].addr */ | |
844c8229 | 172 | writeq(vq->desc + (16 * i), 0); |
bf3c63d2 MM |
173 | /* vq->desc[i].next */ |
174 | writew(vq->desc + (16 * i) + 14, i + 1); | |
175 | } | |
176 | ||
177 | /* vq->avail->flags */ | |
178 | writew(vq->avail, 0); | |
179 | /* vq->avail->idx */ | |
180 | writew(vq->avail + 2, 0); | |
1053587c MM |
181 | /* vq->avail->used_event */ |
182 | writew(vq->avail + 4 + (2 * vq->size), 0); | |
bf3c63d2 MM |
183 | |
184 | /* vq->used->flags */ | |
185 | writew(vq->used, 0); | |
1053587c | 186 | /* vq->used->avail_event */ |
780b11a0 | 187 | writew(vq->used + 2 + sizeof(struct vring_used_elem) * vq->size, 0); |
bf3c63d2 MM |
188 | } |
189 | ||
f294b029 MM |
190 | QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d, |
191 | QGuestAllocator *alloc, uint16_t elem) | |
192 | { | |
193 | int i; | |
194 | QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect)); | |
195 | ||
196 | indirect->index = 0; | |
197 | indirect->elem = elem; | |
780b11a0 | 198 | indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem); |
f294b029 MM |
199 | |
200 | for (i = 0; i < elem - 1; ++i) { | |
201 | /* indirect->desc[i].addr */ | |
202 | writeq(indirect->desc + (16 * i), 0); | |
203 | /* indirect->desc[i].flags */ | |
ee3b850a | 204 | writew(indirect->desc + (16 * i) + 12, VRING_DESC_F_NEXT); |
f294b029 MM |
205 | /* indirect->desc[i].next */ |
206 | writew(indirect->desc + (16 * i) + 14, i + 1); | |
207 | } | |
208 | ||
209 | return indirect; | |
210 | } | |
211 | ||
212 | void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data, | |
213 | uint32_t len, bool write) | |
214 | { | |
215 | uint16_t flags; | |
216 | ||
217 | g_assert_cmpint(indirect->index, <, indirect->elem); | |
218 | ||
219 | flags = readw(indirect->desc + (16 * indirect->index) + 12); | |
220 | ||
221 | if (write) { | |
ee3b850a | 222 | flags |= VRING_DESC_F_WRITE; |
f294b029 MM |
223 | } |
224 | ||
225 | /* indirect->desc[indirect->index].addr */ | |
226 | writeq(indirect->desc + (16 * indirect->index), data); | |
227 | /* indirect->desc[indirect->index].len */ | |
228 | writel(indirect->desc + (16 * indirect->index) + 8, len); | |
229 | /* indirect->desc[indirect->index].flags */ | |
230 | writew(indirect->desc + (16 * indirect->index) + 12, flags); | |
231 | ||
232 | indirect->index++; | |
233 | } | |
234 | ||
bf3c63d2 MM |
235 | uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write, |
236 | bool next) | |
237 | { | |
238 | uint16_t flags = 0; | |
239 | vq->num_free--; | |
240 | ||
241 | if (write) { | |
ee3b850a | 242 | flags |= VRING_DESC_F_WRITE; |
bf3c63d2 MM |
243 | } |
244 | ||
245 | if (next) { | |
ee3b850a | 246 | flags |= VRING_DESC_F_NEXT; |
bf3c63d2 MM |
247 | } |
248 | ||
249 | /* vq->desc[vq->free_head].addr */ | |
250 | writeq(vq->desc + (16 * vq->free_head), data); | |
251 | /* vq->desc[vq->free_head].len */ | |
252 | writel(vq->desc + (16 * vq->free_head) + 8, len); | |
253 | /* vq->desc[vq->free_head].flags */ | |
254 | writew(vq->desc + (16 * vq->free_head) + 12, flags); | |
255 | ||
256 | return vq->free_head++; /* Return and increase, in this order */ | |
257 | } | |
258 | ||
f294b029 MM |
259 | uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect) |
260 | { | |
261 | g_assert(vq->indirect); | |
262 | g_assert_cmpint(vq->size, >=, indirect->elem); | |
263 | g_assert_cmpint(indirect->index, ==, indirect->elem); | |
264 | ||
265 | vq->num_free--; | |
266 | ||
267 | /* vq->desc[vq->free_head].addr */ | |
268 | writeq(vq->desc + (16 * vq->free_head), indirect->desc); | |
269 | /* vq->desc[vq->free_head].len */ | |
270 | writel(vq->desc + (16 * vq->free_head) + 8, | |
780b11a0 | 271 | sizeof(struct vring_desc) * indirect->elem); |
f294b029 | 272 | /* vq->desc[vq->free_head].flags */ |
ee3b850a | 273 | writew(vq->desc + (16 * vq->free_head) + 12, VRING_DESC_F_INDIRECT); |
f294b029 MM |
274 | |
275 | return vq->free_head++; /* Return and increase, in this order */ | |
276 | } | |
277 | ||
6b9cdf4c | 278 | void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head) |
bf3c63d2 MM |
279 | { |
280 | /* vq->avail->idx */ | |
e49f8277 | 281 | uint16_t idx = readw(vq->avail + 2); |
1053587c MM |
282 | /* vq->used->flags */ |
283 | uint16_t flags; | |
284 | /* vq->used->avail_event */ | |
285 | uint16_t avail_event; | |
bf3c63d2 MM |
286 | |
287 | /* vq->avail->ring[idx % vq->size] */ | |
e49f8277 | 288 | writew(vq->avail + 4 + (2 * (idx % vq->size)), free_head); |
bf3c63d2 | 289 | /* vq->avail->idx */ |
e49f8277 | 290 | writew(vq->avail + 2, idx + 1); |
bf3c63d2 | 291 | |
1053587c MM |
292 | /* Must read after idx is updated */ |
293 | flags = readw(vq->avail); | |
294 | avail_event = readw(vq->used + 4 + | |
780b11a0 | 295 | sizeof(struct vring_used_elem) * vq->size); |
1053587c MM |
296 | |
297 | /* < 1 because we add elements to avail queue one by one */ | |
ee3b850a | 298 | if ((flags & VRING_USED_F_NO_NOTIFY) == 0 && |
1053587c | 299 | (!vq->event || (uint16_t)(idx-avail_event) < 1)) { |
6b9cdf4c | 300 | d->bus->virtqueue_kick(d, vq); |
1053587c MM |
301 | } |
302 | } | |
303 | ||
e77abbe9 SH |
304 | /* |
305 | * qvirtqueue_get_buf: | |
306 | * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL | |
307 | * | |
308 | * This function gets the next used element if there is one ready. | |
309 | * | |
310 | * Returns: true if an element was ready, false otherwise | |
311 | */ | |
312 | bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx) | |
313 | { | |
314 | uint16_t idx; | |
315 | ||
316 | idx = readw(vq->used + offsetof(struct vring_used, idx)); | |
317 | if (idx == vq->last_used_idx) { | |
318 | return false; | |
319 | } | |
320 | ||
321 | if (desc_idx) { | |
322 | uint64_t elem_addr; | |
323 | ||
324 | elem_addr = vq->used + | |
325 | offsetof(struct vring_used, ring) + | |
326 | (vq->last_used_idx % vq->size) * | |
327 | sizeof(struct vring_used_elem); | |
328 | *desc_idx = readl(elem_addr + offsetof(struct vring_used_elem, id)); | |
329 | } | |
330 | ||
331 | vq->last_used_idx++; | |
332 | return true; | |
333 | } | |
334 | ||
1053587c MM |
335 | void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx) |
336 | { | |
337 | g_assert(vq->event); | |
338 | ||
339 | /* vq->avail->used_event */ | |
340 | writew(vq->avail + 4 + (2 * vq->size), idx); | |
bf3c63d2 | 341 | } |
2f84a92e TH |
342 | |
343 | /* | |
344 | * qvirtio_get_dev_type: | |
345 | * Returns: the preferred virtio bus/device type for the current architecture. | |
346 | */ | |
347 | const char *qvirtio_get_dev_type(void) | |
348 | { | |
349 | const char *arch = qtest_get_arch(); | |
350 | ||
351 | if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) { | |
352 | return "device"; /* for virtio-mmio */ | |
353 | } else if (g_str_equal(arch, "s390x")) { | |
354 | return "ccw"; | |
355 | } else { | |
356 | return "pci"; | |
357 | } | |
358 | } |