]>
Commit | Line | Data |
---|---|---|
967f97fa AL |
1 | /* |
2 | * Virtio Support | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
9b8bfe21 | 14 | #include "qemu/osdep.h" |
da34e65c | 15 | #include "qapi/error.h" |
4771d756 PB |
16 | #include "qemu-common.h" |
17 | #include "cpu.h" | |
64979a4d | 18 | #include "trace.h" |
fdfba1a2 | 19 | #include "exec/address-spaces.h" |
1de7afc9 | 20 | #include "qemu/error-report.h" |
0d09e41a | 21 | #include "hw/virtio/virtio.h" |
1de7afc9 | 22 | #include "qemu/atomic.h" |
0d09e41a | 23 | #include "hw/virtio/virtio-bus.h" |
6b321a3d | 24 | #include "migration/migration.h" |
cee3ca00 | 25 | #include "hw/virtio/virtio-access.h" |
967f97fa | 26 | |
6ce69d1c PM |
27 | /* |
28 | * The alignment to use between consumer and producer parts of vring. | |
29 | * x86 pagesize again. This is the default, used by transports like PCI | |
30 | * which don't provide a means for the guest to tell the host the alignment. | |
31 | */ | |
f46f15bc AL |
32 | #define VIRTIO_PCI_VRING_ALIGN 4096 |
33 | ||
967f97fa AL |
34 | typedef struct VRingDesc |
35 | { | |
36 | uint64_t addr; | |
37 | uint32_t len; | |
38 | uint16_t flags; | |
39 | uint16_t next; | |
40 | } VRingDesc; | |
41 | ||
42 | typedef struct VRingAvail | |
43 | { | |
44 | uint16_t flags; | |
45 | uint16_t idx; | |
46 | uint16_t ring[0]; | |
47 | } VRingAvail; | |
48 | ||
49 | typedef struct VRingUsedElem | |
50 | { | |
51 | uint32_t id; | |
52 | uint32_t len; | |
53 | } VRingUsedElem; | |
54 | ||
55 | typedef struct VRingUsed | |
56 | { | |
57 | uint16_t flags; | |
58 | uint16_t idx; | |
59 | VRingUsedElem ring[0]; | |
60 | } VRingUsed; | |
61 | ||
62 | typedef struct VRing | |
63 | { | |
64 | unsigned int num; | |
46c5d082 | 65 | unsigned int num_default; |
6ce69d1c | 66 | unsigned int align; |
a8170e5e AK |
67 | hwaddr desc; |
68 | hwaddr avail; | |
69 | hwaddr used; | |
967f97fa AL |
70 | } VRing; |
71 | ||
72 | struct VirtQueue | |
73 | { | |
74 | VRing vring; | |
be1fea9b VM |
75 | |
76 | /* Next head to pop */ | |
967f97fa | 77 | uint16_t last_avail_idx; |
b796fcd1 | 78 | |
be1fea9b VM |
79 | /* Last avail_idx read from VQ. */ |
80 | uint16_t shadow_avail_idx; | |
81 | ||
b796fcd1 VM |
82 | uint16_t used_idx; |
83 | ||
bcbabae8 MT |
84 | /* Last used index value we have signalled on */ |
85 | uint16_t signalled_used; | |
86 | ||
87 | /* Last used index value we have signalled on */ | |
88 | bool signalled_used_valid; | |
89 | ||
90 | /* Notification enabled? */ | |
91 | bool notification; | |
92 | ||
e78a2b42 JW |
93 | uint16_t queue_index; |
94 | ||
967f97fa | 95 | int inuse; |
bcbabae8 | 96 | |
7055e687 | 97 | uint16_t vector; |
bf1780b0 FZ |
98 | VirtIOHandleOutput handle_output; |
99 | VirtIOHandleOutput handle_aio_output; | |
1cbdabe2 MT |
100 | VirtIODevice *vdev; |
101 | EventNotifier guest_notifier; | |
102 | EventNotifier host_notifier; | |
e0d686bf | 103 | QLIST_ENTRY(VirtQueue) node; |
967f97fa AL |
104 | }; |
105 | ||
967f97fa | 106 | /* virt queue functions */ |
ab223c95 | 107 | void virtio_queue_update_rings(VirtIODevice *vdev, int n) |
967f97fa | 108 | { |
ab223c95 | 109 | VRing *vring = &vdev->vq[n].vring; |
53c25cea | 110 | |
ab223c95 CH |
111 | if (!vring->desc) { |
112 | /* not yet setup -> nothing to do */ | |
113 | return; | |
114 | } | |
115 | vring->avail = vring->desc + vring->num * sizeof(VRingDesc); | |
116 | vring->used = vring_align(vring->avail + | |
117 | offsetof(VRingAvail, ring[vring->num]), | |
118 | vring->align); | |
967f97fa AL |
119 | } |
120 | ||
aa570d6f PB |
121 | static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc, |
122 | hwaddr desc_pa, int i) | |
967f97fa | 123 | { |
aa570d6f PB |
124 | address_space_read(&address_space_memory, desc_pa + i * sizeof(VRingDesc), |
125 | MEMTXATTRS_UNSPECIFIED, (void *)desc, sizeof(VRingDesc)); | |
126 | virtio_tswap64s(vdev, &desc->addr); | |
127 | virtio_tswap32s(vdev, &desc->len); | |
128 | virtio_tswap16s(vdev, &desc->flags); | |
129 | virtio_tswap16s(vdev, &desc->next); | |
967f97fa AL |
130 | } |
131 | ||
132 | static inline uint16_t vring_avail_flags(VirtQueue *vq) | |
133 | { | |
a8170e5e | 134 | hwaddr pa; |
967f97fa | 135 | pa = vq->vring.avail + offsetof(VRingAvail, flags); |
cee3ca00 | 136 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
137 | } |
138 | ||
139 | static inline uint16_t vring_avail_idx(VirtQueue *vq) | |
140 | { | |
a8170e5e | 141 | hwaddr pa; |
967f97fa | 142 | pa = vq->vring.avail + offsetof(VRingAvail, idx); |
be1fea9b VM |
143 | vq->shadow_avail_idx = virtio_lduw_phys(vq->vdev, pa); |
144 | return vq->shadow_avail_idx; | |
967f97fa AL |
145 | } |
146 | ||
147 | static inline uint16_t vring_avail_ring(VirtQueue *vq, int i) | |
148 | { | |
a8170e5e | 149 | hwaddr pa; |
967f97fa | 150 | pa = vq->vring.avail + offsetof(VRingAvail, ring[i]); |
cee3ca00 | 151 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
152 | } |
153 | ||
e9600c6c | 154 | static inline uint16_t vring_get_used_event(VirtQueue *vq) |
bcbabae8 MT |
155 | { |
156 | return vring_avail_ring(vq, vq->vring.num); | |
157 | } | |
158 | ||
1cdd2ee5 VM |
159 | static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem, |
160 | int i) | |
967f97fa | 161 | { |
a8170e5e | 162 | hwaddr pa; |
1cdd2ee5 VM |
163 | virtio_tswap32s(vq->vdev, &uelem->id); |
164 | virtio_tswap32s(vq->vdev, &uelem->len); | |
165 | pa = vq->vring.used + offsetof(VRingUsed, ring[i]); | |
166 | address_space_write(&address_space_memory, pa, MEMTXATTRS_UNSPECIFIED, | |
167 | (void *)uelem, sizeof(VRingUsedElem)); | |
967f97fa AL |
168 | } |
169 | ||
170 | static uint16_t vring_used_idx(VirtQueue *vq) | |
171 | { | |
a8170e5e | 172 | hwaddr pa; |
967f97fa | 173 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
cee3ca00 | 174 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
175 | } |
176 | ||
bcbabae8 | 177 | static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val) |
967f97fa | 178 | { |
a8170e5e | 179 | hwaddr pa; |
967f97fa | 180 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
cee3ca00 | 181 | virtio_stw_phys(vq->vdev, pa, val); |
b796fcd1 | 182 | vq->used_idx = val; |
967f97fa AL |
183 | } |
184 | ||
185 | static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask) | |
186 | { | |
cee3ca00 | 187 | VirtIODevice *vdev = vq->vdev; |
a8170e5e | 188 | hwaddr pa; |
967f97fa | 189 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
cee3ca00 | 190 | virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) | mask); |
967f97fa AL |
191 | } |
192 | ||
193 | static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask) | |
194 | { | |
cee3ca00 | 195 | VirtIODevice *vdev = vq->vdev; |
a8170e5e | 196 | hwaddr pa; |
967f97fa | 197 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
cee3ca00 | 198 | virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) & ~mask); |
967f97fa AL |
199 | } |
200 | ||
e9600c6c | 201 | static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val) |
bcbabae8 | 202 | { |
a8170e5e | 203 | hwaddr pa; |
bcbabae8 MT |
204 | if (!vq->notification) { |
205 | return; | |
206 | } | |
207 | pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]); | |
cee3ca00 | 208 | virtio_stw_phys(vq->vdev, pa, val); |
bcbabae8 MT |
209 | } |
210 | ||
967f97fa AL |
211 | void virtio_queue_set_notification(VirtQueue *vq, int enable) |
212 | { | |
bcbabae8 | 213 | vq->notification = enable; |
95129d6f | 214 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { |
e9600c6c | 215 | vring_set_avail_event(vq, vring_avail_idx(vq)); |
bcbabae8 | 216 | } else if (enable) { |
967f97fa | 217 | vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 218 | } else { |
967f97fa | 219 | vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 220 | } |
92045d80 MT |
221 | if (enable) { |
222 | /* Expose avail event/used flags before caller checks the avail idx. */ | |
223 | smp_mb(); | |
224 | } | |
967f97fa AL |
225 | } |
226 | ||
227 | int virtio_queue_ready(VirtQueue *vq) | |
228 | { | |
229 | return vq->vring.avail != 0; | |
230 | } | |
231 | ||
be1fea9b VM |
232 | /* Fetch avail_idx from VQ memory only when we really need to know if |
233 | * guest has added some buffers. */ | |
967f97fa AL |
234 | int virtio_queue_empty(VirtQueue *vq) |
235 | { | |
be1fea9b VM |
236 | if (vq->shadow_avail_idx != vq->last_avail_idx) { |
237 | return 0; | |
238 | } | |
239 | ||
967f97fa AL |
240 | return vring_avail_idx(vq) == vq->last_avail_idx; |
241 | } | |
242 | ||
ce317461 JW |
243 | static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, |
244 | unsigned int len) | |
967f97fa AL |
245 | { |
246 | unsigned int offset; | |
247 | int i; | |
248 | ||
967f97fa AL |
249 | offset = 0; |
250 | for (i = 0; i < elem->in_num; i++) { | |
251 | size_t size = MIN(len - offset, elem->in_sg[i].iov_len); | |
252 | ||
26b258e1 AL |
253 | cpu_physical_memory_unmap(elem->in_sg[i].iov_base, |
254 | elem->in_sg[i].iov_len, | |
255 | 1, size); | |
967f97fa | 256 | |
0cea71a2 | 257 | offset += size; |
967f97fa AL |
258 | } |
259 | ||
26b258e1 AL |
260 | for (i = 0; i < elem->out_num; i++) |
261 | cpu_physical_memory_unmap(elem->out_sg[i].iov_base, | |
262 | elem->out_sg[i].iov_len, | |
263 | 0, elem->out_sg[i].iov_len); | |
ce317461 JW |
264 | } |
265 | ||
2640d2a5 SH |
266 | /* virtqueue_detach_element: |
267 | * @vq: The #VirtQueue | |
268 | * @elem: The #VirtQueueElement | |
269 | * @len: number of bytes written | |
270 | * | |
271 | * Detach the element from the virtqueue. This function is suitable for device | |
272 | * reset or other situations where a #VirtQueueElement is simply freed and will | |
273 | * not be pushed or discarded. | |
274 | */ | |
275 | void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem, | |
276 | unsigned int len) | |
277 | { | |
278 | vq->inuse--; | |
279 | virtqueue_unmap_sg(vq, elem, len); | |
280 | } | |
281 | ||
282 | /* virtqueue_discard: | |
283 | * @vq: The #VirtQueue | |
284 | * @elem: The #VirtQueueElement | |
285 | * @len: number of bytes written | |
286 | * | |
287 | * Pretend the most recent element wasn't popped from the virtqueue. The next | |
288 | * call to virtqueue_pop() will refetch the element. | |
289 | */ | |
29b9f5ef JW |
290 | void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem, |
291 | unsigned int len) | |
292 | { | |
293 | vq->last_avail_idx--; | |
2640d2a5 | 294 | virtqueue_detach_element(vq, elem, len); |
29b9f5ef JW |
295 | } |
296 | ||
297a75e6 SH |
297 | /* virtqueue_rewind: |
298 | * @vq: The #VirtQueue | |
299 | * @num: Number of elements to push back | |
300 | * | |
301 | * Pretend that elements weren't popped from the virtqueue. The next | |
302 | * virtqueue_pop() will refetch the oldest element. | |
303 | * | |
304 | * Use virtqueue_discard() instead if you have a VirtQueueElement. | |
305 | * | |
306 | * Returns: true on success, false if @num is greater than the number of in use | |
307 | * elements. | |
308 | */ | |
309 | bool virtqueue_rewind(VirtQueue *vq, unsigned int num) | |
310 | { | |
311 | if (num > vq->inuse) { | |
312 | return false; | |
313 | } | |
314 | vq->last_avail_idx -= num; | |
315 | vq->inuse -= num; | |
316 | return true; | |
317 | } | |
318 | ||
ce317461 JW |
319 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, |
320 | unsigned int len, unsigned int idx) | |
321 | { | |
1cdd2ee5 VM |
322 | VRingUsedElem uelem; |
323 | ||
ce317461 JW |
324 | trace_virtqueue_fill(vq, elem, len, idx); |
325 | ||
326 | virtqueue_unmap_sg(vq, elem, len); | |
26b258e1 | 327 | |
f5ed3663 SH |
328 | if (unlikely(vq->vdev->broken)) { |
329 | return; | |
330 | } | |
331 | ||
b796fcd1 | 332 | idx = (idx + vq->used_idx) % vq->vring.num; |
967f97fa | 333 | |
1cdd2ee5 VM |
334 | uelem.id = elem->index; |
335 | uelem.len = len; | |
336 | vring_used_write(vq, &uelem, idx); | |
967f97fa AL |
337 | } |
338 | ||
339 | void virtqueue_flush(VirtQueue *vq, unsigned int count) | |
340 | { | |
bcbabae8 | 341 | uint16_t old, new; |
f5ed3663 SH |
342 | |
343 | if (unlikely(vq->vdev->broken)) { | |
344 | vq->inuse -= count; | |
345 | return; | |
346 | } | |
347 | ||
967f97fa | 348 | /* Make sure buffer is written before we update index. */ |
b90d2f35 | 349 | smp_wmb(); |
64979a4d | 350 | trace_virtqueue_flush(vq, count); |
b796fcd1 | 351 | old = vq->used_idx; |
bcbabae8 MT |
352 | new = old + count; |
353 | vring_used_idx_set(vq, new); | |
967f97fa | 354 | vq->inuse -= count; |
bcbabae8 MT |
355 | if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))) |
356 | vq->signalled_used_valid = false; | |
967f97fa AL |
357 | } |
358 | ||
359 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, | |
360 | unsigned int len) | |
361 | { | |
362 | virtqueue_fill(vq, elem, len, 0); | |
363 | virtqueue_flush(vq, 1); | |
364 | } | |
365 | ||
366 | static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx) | |
367 | { | |
368 | uint16_t num_heads = vring_avail_idx(vq) - idx; | |
369 | ||
370 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
bb6834cf | 371 | if (num_heads > vq->vring.num) { |
4355c1ab | 372 | virtio_error(vq->vdev, "Guest moved used index from %u to %u", |
be1fea9b | 373 | idx, vq->shadow_avail_idx); |
4355c1ab | 374 | return -EINVAL; |
bb6834cf | 375 | } |
a821ce59 MT |
376 | /* On success, callers read a descriptor at vq->last_avail_idx. |
377 | * Make sure descriptor read does not bypass avail index read. */ | |
378 | if (num_heads) { | |
379 | smp_rmb(); | |
380 | } | |
967f97fa AL |
381 | |
382 | return num_heads; | |
383 | } | |
384 | ||
fb1131b6 SH |
385 | static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx, |
386 | unsigned int *head) | |
967f97fa | 387 | { |
967f97fa AL |
388 | /* Grab the next descriptor number they're advertising, and increment |
389 | * the index we've seen. */ | |
fb1131b6 | 390 | *head = vring_avail_ring(vq, idx % vq->vring.num); |
967f97fa AL |
391 | |
392 | /* If their number is silly, that's a fatal mistake. */ | |
fb1131b6 SH |
393 | if (*head >= vq->vring.num) { |
394 | virtio_error(vq->vdev, "Guest says index %u is available", *head); | |
395 | return false; | |
bb6834cf | 396 | } |
967f97fa | 397 | |
fb1131b6 | 398 | return true; |
967f97fa AL |
399 | } |
400 | ||
412e0e81 SH |
401 | enum { |
402 | VIRTQUEUE_READ_DESC_ERROR = -1, | |
403 | VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */ | |
404 | VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */ | |
405 | }; | |
967f97fa | 406 | |
412e0e81 SH |
407 | static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc, |
408 | hwaddr desc_pa, unsigned int max, | |
409 | unsigned int *next) | |
410 | { | |
967f97fa | 411 | /* If this descriptor says it doesn't chain, we're done. */ |
aa570d6f | 412 | if (!(desc->flags & VRING_DESC_F_NEXT)) { |
412e0e81 | 413 | return VIRTQUEUE_READ_DESC_DONE; |
cee3ca00 | 414 | } |
967f97fa AL |
415 | |
416 | /* Check they're not leading us off end of descriptors. */ | |
412e0e81 | 417 | *next = desc->next; |
967f97fa | 418 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
b90d2f35 | 419 | smp_wmb(); |
967f97fa | 420 | |
412e0e81 SH |
421 | if (*next >= max) { |
422 | virtio_error(vdev, "Desc next is %u", *next); | |
423 | return VIRTQUEUE_READ_DESC_ERROR; | |
bb6834cf | 424 | } |
967f97fa | 425 | |
412e0e81 SH |
426 | vring_desc_read(vdev, desc, desc_pa, *next); |
427 | return VIRTQUEUE_READ_DESC_MORE; | |
967f97fa AL |
428 | } |
429 | ||
0d8d7690 | 430 | void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, |
e1f7b481 MT |
431 | unsigned int *out_bytes, |
432 | unsigned max_in_bytes, unsigned max_out_bytes) | |
967f97fa | 433 | { |
efeea6d0 | 434 | unsigned int idx; |
385ce95d | 435 | unsigned int total_bufs, in_total, out_total; |
412e0e81 | 436 | int rc; |
967f97fa AL |
437 | |
438 | idx = vq->last_avail_idx; | |
439 | ||
efeea6d0 | 440 | total_bufs = in_total = out_total = 0; |
4355c1ab | 441 | while ((rc = virtqueue_num_heads(vq, idx)) > 0) { |
cee3ca00 | 442 | VirtIODevice *vdev = vq->vdev; |
efeea6d0 | 443 | unsigned int max, num_bufs, indirect = 0; |
aa570d6f | 444 | VRingDesc desc; |
a8170e5e | 445 | hwaddr desc_pa; |
b1c7c07f | 446 | unsigned int i; |
967f97fa | 447 | |
efeea6d0 MM |
448 | max = vq->vring.num; |
449 | num_bufs = total_bufs; | |
fb1131b6 SH |
450 | |
451 | if (!virtqueue_get_head(vq, idx++, &i)) { | |
452 | goto err; | |
453 | } | |
454 | ||
efeea6d0 | 455 | desc_pa = vq->vring.desc; |
aa570d6f | 456 | vring_desc_read(vdev, &desc, desc_pa, i); |
efeea6d0 | 457 | |
aa570d6f PB |
458 | if (desc.flags & VRING_DESC_F_INDIRECT) { |
459 | if (desc.len % sizeof(VRingDesc)) { | |
d65abf85 SH |
460 | virtio_error(vdev, "Invalid size for indirect buffer table"); |
461 | goto err; | |
efeea6d0 MM |
462 | } |
463 | ||
464 | /* If we've got too many, that implies a descriptor loop. */ | |
465 | if (num_bufs >= max) { | |
d65abf85 SH |
466 | virtio_error(vdev, "Looped descriptor"); |
467 | goto err; | |
efeea6d0 MM |
468 | } |
469 | ||
470 | /* loop over the indirect descriptor table */ | |
471 | indirect = 1; | |
aa570d6f PB |
472 | max = desc.len / sizeof(VRingDesc); |
473 | desc_pa = desc.addr; | |
1ae2757c | 474 | num_bufs = i = 0; |
aa570d6f | 475 | vring_desc_read(vdev, &desc, desc_pa, i); |
efeea6d0 MM |
476 | } |
477 | ||
967f97fa AL |
478 | do { |
479 | /* If we've got too many, that implies a descriptor loop. */ | |
5774cf98 | 480 | if (++num_bufs > max) { |
d65abf85 SH |
481 | virtio_error(vdev, "Looped descriptor"); |
482 | goto err; | |
bb6834cf | 483 | } |
967f97fa | 484 | |
aa570d6f PB |
485 | if (desc.flags & VRING_DESC_F_WRITE) { |
486 | in_total += desc.len; | |
967f97fa | 487 | } else { |
aa570d6f | 488 | out_total += desc.len; |
967f97fa | 489 | } |
e1f7b481 MT |
490 | if (in_total >= max_in_bytes && out_total >= max_out_bytes) { |
491 | goto done; | |
492 | } | |
412e0e81 SH |
493 | |
494 | rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i); | |
495 | } while (rc == VIRTQUEUE_READ_DESC_MORE); | |
496 | ||
497 | if (rc == VIRTQUEUE_READ_DESC_ERROR) { | |
498 | goto err; | |
499 | } | |
efeea6d0 MM |
500 | |
501 | if (!indirect) | |
502 | total_bufs = num_bufs; | |
503 | else | |
504 | total_bufs++; | |
967f97fa | 505 | } |
4355c1ab SH |
506 | |
507 | if (rc < 0) { | |
508 | goto err; | |
509 | } | |
510 | ||
e1f7b481 | 511 | done: |
0d8d7690 AS |
512 | if (in_bytes) { |
513 | *in_bytes = in_total; | |
514 | } | |
515 | if (out_bytes) { | |
516 | *out_bytes = out_total; | |
517 | } | |
d65abf85 SH |
518 | return; |
519 | ||
520 | err: | |
521 | in_total = out_total = 0; | |
522 | goto done; | |
0d8d7690 | 523 | } |
967f97fa | 524 | |
0d8d7690 AS |
525 | int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, |
526 | unsigned int out_bytes) | |
527 | { | |
528 | unsigned int in_total, out_total; | |
529 | ||
e1f7b481 MT |
530 | virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes); |
531 | return in_bytes <= in_total && out_bytes <= out_total; | |
967f97fa AL |
532 | } |
533 | ||
ec55da19 SH |
534 | static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg, |
535 | hwaddr *addr, struct iovec *iov, | |
3b3b0628 PB |
536 | unsigned int max_num_sg, bool is_write, |
537 | hwaddr pa, size_t sz) | |
538 | { | |
ec55da19 | 539 | bool ok = false; |
3b3b0628 PB |
540 | unsigned num_sg = *p_num_sg; |
541 | assert(num_sg <= max_num_sg); | |
542 | ||
1e7aed70 | 543 | if (!sz) { |
ec55da19 SH |
544 | virtio_error(vdev, "virtio: zero sized buffers are not allowed"); |
545 | goto out; | |
1e7aed70 PP |
546 | } |
547 | ||
3b3b0628 PB |
548 | while (sz) { |
549 | hwaddr len = sz; | |
550 | ||
551 | if (num_sg == max_num_sg) { | |
ec55da19 SH |
552 | virtio_error(vdev, "virtio: too many write descriptors in " |
553 | "indirect table"); | |
554 | goto out; | |
3b3b0628 PB |
555 | } |
556 | ||
557 | iov[num_sg].iov_base = cpu_physical_memory_map(pa, &len, is_write); | |
973e7170 | 558 | if (!iov[num_sg].iov_base) { |
ec55da19 SH |
559 | virtio_error(vdev, "virtio: bogus descriptor or out of resources"); |
560 | goto out; | |
973e7170 PP |
561 | } |
562 | ||
3b3b0628 PB |
563 | iov[num_sg].iov_len = len; |
564 | addr[num_sg] = pa; | |
565 | ||
566 | sz -= len; | |
567 | pa += len; | |
568 | num_sg++; | |
569 | } | |
ec55da19 SH |
570 | ok = true; |
571 | ||
572 | out: | |
3b3b0628 | 573 | *p_num_sg = num_sg; |
ec55da19 SH |
574 | return ok; |
575 | } | |
576 | ||
577 | /* Only used by error code paths before we have a VirtQueueElement (therefore | |
578 | * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to | |
579 | * yet. | |
580 | */ | |
581 | static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num, | |
582 | struct iovec *iov) | |
583 | { | |
584 | unsigned int i; | |
585 | ||
586 | for (i = 0; i < out_num + in_num; i++) { | |
587 | int is_write = i >= out_num; | |
588 | ||
589 | cpu_physical_memory_unmap(iov->iov_base, iov->iov_len, is_write, 0); | |
590 | iov++; | |
591 | } | |
3b3b0628 PB |
592 | } |
593 | ||
8059feee MT |
594 | static void virtqueue_map_iovec(struct iovec *sg, hwaddr *addr, |
595 | unsigned int *num_sg, unsigned int max_size, | |
596 | int is_write) | |
42fb2e07 KW |
597 | { |
598 | unsigned int i; | |
a8170e5e | 599 | hwaddr len; |
42fb2e07 | 600 | |
8059feee MT |
601 | /* Note: this function MUST validate input, some callers |
602 | * are passing in num_sg values received over the network. | |
603 | */ | |
604 | /* TODO: teach all callers that this can fail, and return failure instead | |
605 | * of asserting here. | |
606 | * When we do, we might be able to re-enable NDEBUG below. | |
607 | */ | |
608 | #ifdef NDEBUG | |
609 | #error building with NDEBUG is not supported | |
610 | #endif | |
611 | assert(*num_sg <= max_size); | |
612 | ||
613 | for (i = 0; i < *num_sg; i++) { | |
42fb2e07 KW |
614 | len = sg[i].iov_len; |
615 | sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write); | |
8059feee | 616 | if (!sg[i].iov_base) { |
1a285899 | 617 | error_report("virtio: error trying to map MMIO memory"); |
42fb2e07 KW |
618 | exit(1); |
619 | } | |
3b3b0628 PB |
620 | if (len != sg[i].iov_len) { |
621 | error_report("virtio: unexpected memory split"); | |
8059feee MT |
622 | exit(1); |
623 | } | |
42fb2e07 KW |
624 | } |
625 | } | |
626 | ||
8059feee MT |
627 | void virtqueue_map(VirtQueueElement *elem) |
628 | { | |
629 | virtqueue_map_iovec(elem->in_sg, elem->in_addr, &elem->in_num, | |
3724650d | 630 | VIRTQUEUE_MAX_SIZE, 1); |
8059feee | 631 | virtqueue_map_iovec(elem->out_sg, elem->out_addr, &elem->out_num, |
3724650d PB |
632 | VIRTQUEUE_MAX_SIZE, 0); |
633 | } | |
634 | ||
635 | void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num) | |
636 | { | |
637 | VirtQueueElement *elem; | |
638 | size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0])); | |
639 | size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]); | |
640 | size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]); | |
641 | size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0])); | |
642 | size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]); | |
643 | size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]); | |
644 | ||
645 | assert(sz >= sizeof(VirtQueueElement)); | |
646 | elem = g_malloc(out_sg_end); | |
647 | elem->out_num = out_num; | |
648 | elem->in_num = in_num; | |
649 | elem->in_addr = (void *)elem + in_addr_ofs; | |
650 | elem->out_addr = (void *)elem + out_addr_ofs; | |
651 | elem->in_sg = (void *)elem + in_sg_ofs; | |
652 | elem->out_sg = (void *)elem + out_sg_ofs; | |
653 | return elem; | |
8059feee MT |
654 | } |
655 | ||
51b19ebe | 656 | void *virtqueue_pop(VirtQueue *vq, size_t sz) |
967f97fa | 657 | { |
5774cf98 | 658 | unsigned int i, head, max; |
a8170e5e | 659 | hwaddr desc_pa = vq->vring.desc; |
cee3ca00 | 660 | VirtIODevice *vdev = vq->vdev; |
51b19ebe | 661 | VirtQueueElement *elem; |
3b3b0628 PB |
662 | unsigned out_num, in_num; |
663 | hwaddr addr[VIRTQUEUE_MAX_SIZE]; | |
664 | struct iovec iov[VIRTQUEUE_MAX_SIZE]; | |
aa570d6f | 665 | VRingDesc desc; |
412e0e81 | 666 | int rc; |
967f97fa | 667 | |
f5ed3663 SH |
668 | if (unlikely(vdev->broken)) { |
669 | return NULL; | |
670 | } | |
be1fea9b | 671 | if (virtio_queue_empty(vq)) { |
51b19ebe PB |
672 | return NULL; |
673 | } | |
be1fea9b VM |
674 | /* Needed after virtio_queue_empty(), see comment in |
675 | * virtqueue_num_heads(). */ | |
676 | smp_rmb(); | |
967f97fa AL |
677 | |
678 | /* When we start there are none of either input nor output. */ | |
3b3b0628 | 679 | out_num = in_num = 0; |
967f97fa | 680 | |
5774cf98 MM |
681 | max = vq->vring.num; |
682 | ||
afd9096e | 683 | if (vq->inuse >= vq->vring.num) { |
ec55da19 SH |
684 | virtio_error(vdev, "Virtqueue size exceeded"); |
685 | return NULL; | |
afd9096e SH |
686 | } |
687 | ||
fb1131b6 SH |
688 | if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) { |
689 | return NULL; | |
690 | } | |
691 | ||
95129d6f | 692 | if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
e9600c6c | 693 | vring_set_avail_event(vq, vq->last_avail_idx); |
bcbabae8 | 694 | } |
efeea6d0 | 695 | |
fb1131b6 | 696 | i = head; |
aa570d6f PB |
697 | vring_desc_read(vdev, &desc, desc_pa, i); |
698 | if (desc.flags & VRING_DESC_F_INDIRECT) { | |
699 | if (desc.len % sizeof(VRingDesc)) { | |
ec55da19 SH |
700 | virtio_error(vdev, "Invalid size for indirect buffer table"); |
701 | return NULL; | |
efeea6d0 MM |
702 | } |
703 | ||
704 | /* loop over the indirect descriptor table */ | |
aa570d6f PB |
705 | max = desc.len / sizeof(VRingDesc); |
706 | desc_pa = desc.addr; | |
efeea6d0 | 707 | i = 0; |
aa570d6f | 708 | vring_desc_read(vdev, &desc, desc_pa, i); |
efeea6d0 MM |
709 | } |
710 | ||
42fb2e07 | 711 | /* Collect all the descriptors */ |
967f97fa | 712 | do { |
ec55da19 SH |
713 | bool map_ok; |
714 | ||
aa570d6f | 715 | if (desc.flags & VRING_DESC_F_WRITE) { |
ec55da19 SH |
716 | map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num, |
717 | iov + out_num, | |
718 | VIRTQUEUE_MAX_SIZE - out_num, true, | |
719 | desc.addr, desc.len); | |
42fb2e07 | 720 | } else { |
3b3b0628 | 721 | if (in_num) { |
ec55da19 SH |
722 | virtio_error(vdev, "Incorrect order for descriptors"); |
723 | goto err_undo_map; | |
c8eac1cf | 724 | } |
ec55da19 SH |
725 | map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov, |
726 | VIRTQUEUE_MAX_SIZE, false, | |
727 | desc.addr, desc.len); | |
728 | } | |
729 | if (!map_ok) { | |
730 | goto err_undo_map; | |
42fb2e07 | 731 | } |
967f97fa | 732 | |
967f97fa | 733 | /* If we've got too many, that implies a descriptor loop. */ |
3b3b0628 | 734 | if ((in_num + out_num) > max) { |
ec55da19 SH |
735 | virtio_error(vdev, "Looped descriptor"); |
736 | goto err_undo_map; | |
bb6834cf | 737 | } |
412e0e81 SH |
738 | |
739 | rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i); | |
740 | } while (rc == VIRTQUEUE_READ_DESC_MORE); | |
741 | ||
742 | if (rc == VIRTQUEUE_READ_DESC_ERROR) { | |
743 | goto err_undo_map; | |
744 | } | |
967f97fa | 745 | |
3b3b0628 PB |
746 | /* Now copy what we have collected and mapped */ |
747 | elem = virtqueue_alloc_element(sz, out_num, in_num); | |
967f97fa | 748 | elem->index = head; |
3b3b0628 PB |
749 | for (i = 0; i < out_num; i++) { |
750 | elem->out_addr[i] = addr[i]; | |
751 | elem->out_sg[i] = iov[i]; | |
752 | } | |
753 | for (i = 0; i < in_num; i++) { | |
754 | elem->in_addr[i] = addr[out_num + i]; | |
755 | elem->in_sg[i] = iov[out_num + i]; | |
756 | } | |
967f97fa AL |
757 | |
758 | vq->inuse++; | |
759 | ||
64979a4d | 760 | trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); |
51b19ebe | 761 | return elem; |
ec55da19 SH |
762 | |
763 | err_undo_map: | |
764 | virtqueue_undo_map_desc(out_num, in_num, iov); | |
765 | return NULL; | |
967f97fa AL |
766 | } |
767 | ||
3724650d PB |
768 | /* Reading and writing a structure directly to QEMUFile is *awful*, but |
769 | * it is what QEMU has always done by mistake. We can change it sooner | |
770 | * or later by bumping the version number of the affected vm states. | |
771 | * In the meanwhile, since the in-memory layout of VirtQueueElement | |
772 | * has changed, we need to marshal to and from the layout that was | |
773 | * used before the change. | |
774 | */ | |
775 | typedef struct VirtQueueElementOld { | |
776 | unsigned int index; | |
777 | unsigned int out_num; | |
778 | unsigned int in_num; | |
779 | hwaddr in_addr[VIRTQUEUE_MAX_SIZE]; | |
780 | hwaddr out_addr[VIRTQUEUE_MAX_SIZE]; | |
781 | struct iovec in_sg[VIRTQUEUE_MAX_SIZE]; | |
782 | struct iovec out_sg[VIRTQUEUE_MAX_SIZE]; | |
783 | } VirtQueueElementOld; | |
784 | ||
ab281c17 PB |
785 | void *qemu_get_virtqueue_element(QEMUFile *f, size_t sz) |
786 | { | |
3724650d PB |
787 | VirtQueueElement *elem; |
788 | VirtQueueElementOld data; | |
789 | int i; | |
790 | ||
791 | qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld)); | |
792 | ||
793 | elem = virtqueue_alloc_element(sz, data.out_num, data.in_num); | |
794 | elem->index = data.index; | |
795 | ||
796 | for (i = 0; i < elem->in_num; i++) { | |
797 | elem->in_addr[i] = data.in_addr[i]; | |
798 | } | |
799 | ||
800 | for (i = 0; i < elem->out_num; i++) { | |
801 | elem->out_addr[i] = data.out_addr[i]; | |
802 | } | |
803 | ||
804 | for (i = 0; i < elem->in_num; i++) { | |
805 | /* Base is overwritten by virtqueue_map. */ | |
806 | elem->in_sg[i].iov_base = 0; | |
807 | elem->in_sg[i].iov_len = data.in_sg[i].iov_len; | |
808 | } | |
809 | ||
810 | for (i = 0; i < elem->out_num; i++) { | |
811 | /* Base is overwritten by virtqueue_map. */ | |
812 | elem->out_sg[i].iov_base = 0; | |
813 | elem->out_sg[i].iov_len = data.out_sg[i].iov_len; | |
814 | } | |
815 | ||
ab281c17 PB |
816 | virtqueue_map(elem); |
817 | return elem; | |
818 | } | |
819 | ||
820 | void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem) | |
821 | { | |
3724650d PB |
822 | VirtQueueElementOld data; |
823 | int i; | |
824 | ||
825 | memset(&data, 0, sizeof(data)); | |
826 | data.index = elem->index; | |
827 | data.in_num = elem->in_num; | |
828 | data.out_num = elem->out_num; | |
829 | ||
830 | for (i = 0; i < elem->in_num; i++) { | |
831 | data.in_addr[i] = elem->in_addr[i]; | |
832 | } | |
833 | ||
834 | for (i = 0; i < elem->out_num; i++) { | |
835 | data.out_addr[i] = elem->out_addr[i]; | |
836 | } | |
837 | ||
838 | for (i = 0; i < elem->in_num; i++) { | |
839 | /* Base is overwritten by virtqueue_map when loading. Do not | |
840 | * save it, as it would leak the QEMU address space layout. */ | |
841 | data.in_sg[i].iov_len = elem->in_sg[i].iov_len; | |
842 | } | |
843 | ||
844 | for (i = 0; i < elem->out_num; i++) { | |
845 | /* Do not save iov_base as above. */ | |
846 | data.out_sg[i].iov_len = elem->out_sg[i].iov_len; | |
847 | } | |
848 | qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld)); | |
ab281c17 PB |
849 | } |
850 | ||
967f97fa | 851 | /* virtio device */ |
7055e687 MT |
852 | static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector) |
853 | { | |
1c819449 FK |
854 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
855 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
856 | ||
f5ed3663 SH |
857 | if (unlikely(vdev->broken)) { |
858 | return; | |
859 | } | |
860 | ||
1c819449 FK |
861 | if (k->notify) { |
862 | k->notify(qbus->parent, vector); | |
7055e687 MT |
863 | } |
864 | } | |
967f97fa | 865 | |
53c25cea | 866 | void virtio_update_irq(VirtIODevice *vdev) |
967f97fa | 867 | { |
7055e687 | 868 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
967f97fa AL |
869 | } |
870 | ||
0b352fd6 CH |
871 | static int virtio_validate_features(VirtIODevice *vdev) |
872 | { | |
873 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
874 | ||
875 | if (k->validate_features) { | |
876 | return k->validate_features(vdev); | |
877 | } else { | |
878 | return 0; | |
879 | } | |
880 | } | |
881 | ||
882 | int virtio_set_status(VirtIODevice *vdev, uint8_t val) | |
4e1837f8 | 883 | { |
181103cd | 884 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
4e1837f8 SH |
885 | trace_virtio_set_status(vdev, val); |
886 | ||
95129d6f | 887 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
0b352fd6 CH |
888 | if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) && |
889 | val & VIRTIO_CONFIG_S_FEATURES_OK) { | |
890 | int ret = virtio_validate_features(vdev); | |
891 | ||
892 | if (ret) { | |
893 | return ret; | |
894 | } | |
895 | } | |
896 | } | |
181103cd FK |
897 | if (k->set_status) { |
898 | k->set_status(vdev, val); | |
4e1837f8 SH |
899 | } |
900 | vdev->status = val; | |
0b352fd6 | 901 | return 0; |
4e1837f8 SH |
902 | } |
903 | ||
616a6552 GK |
904 | bool target_words_bigendian(void); |
905 | static enum virtio_device_endian virtio_default_endian(void) | |
906 | { | |
907 | if (target_words_bigendian()) { | |
908 | return VIRTIO_DEVICE_ENDIAN_BIG; | |
909 | } else { | |
910 | return VIRTIO_DEVICE_ENDIAN_LITTLE; | |
911 | } | |
912 | } | |
913 | ||
914 | static enum virtio_device_endian virtio_current_cpu_endian(void) | |
915 | { | |
916 | CPUClass *cc = CPU_GET_CLASS(current_cpu); | |
917 | ||
918 | if (cc->virtio_is_big_endian(current_cpu)) { | |
919 | return VIRTIO_DEVICE_ENDIAN_BIG; | |
920 | } else { | |
921 | return VIRTIO_DEVICE_ENDIAN_LITTLE; | |
922 | } | |
923 | } | |
924 | ||
53c25cea | 925 | void virtio_reset(void *opaque) |
967f97fa AL |
926 | { |
927 | VirtIODevice *vdev = opaque; | |
181103cd | 928 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
929 | int i; |
930 | ||
e0c472d8 | 931 | virtio_set_status(vdev, 0); |
616a6552 GK |
932 | if (current_cpu) { |
933 | /* Guest initiated reset */ | |
934 | vdev->device_endian = virtio_current_cpu_endian(); | |
935 | } else { | |
936 | /* System reset */ | |
937 | vdev->device_endian = virtio_default_endian(); | |
938 | } | |
e0c472d8 | 939 | |
181103cd FK |
940 | if (k->reset) { |
941 | k->reset(vdev); | |
942 | } | |
967f97fa | 943 | |
f5ed3663 | 944 | vdev->broken = false; |
704a76fc | 945 | vdev->guest_features = 0; |
967f97fa AL |
946 | vdev->queue_sel = 0; |
947 | vdev->status = 0; | |
948 | vdev->isr = 0; | |
7055e687 MT |
949 | vdev->config_vector = VIRTIO_NO_VECTOR; |
950 | virtio_notify_vector(vdev, vdev->config_vector); | |
967f97fa | 951 | |
87b3bd1c | 952 | for(i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
953 | vdev->vq[i].vring.desc = 0; |
954 | vdev->vq[i].vring.avail = 0; | |
955 | vdev->vq[i].vring.used = 0; | |
956 | vdev->vq[i].last_avail_idx = 0; | |
be1fea9b | 957 | vdev->vq[i].shadow_avail_idx = 0; |
b796fcd1 | 958 | vdev->vq[i].used_idx = 0; |
e0d686bf | 959 | virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR); |
bcbabae8 MT |
960 | vdev->vq[i].signalled_used = 0; |
961 | vdev->vq[i].signalled_used_valid = false; | |
962 | vdev->vq[i].notification = true; | |
46c5d082 | 963 | vdev->vq[i].vring.num = vdev->vq[i].vring.num_default; |
4b7f91ed | 964 | vdev->vq[i].inuse = 0; |
967f97fa AL |
965 | } |
966 | } | |
967 | ||
53c25cea | 968 | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 969 | { |
181103cd | 970 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
971 | uint8_t val; |
972 | ||
5f5a1318 | 973 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 974 | return (uint32_t)-1; |
5f5a1318 JW |
975 | } |
976 | ||
977 | k->get_config(vdev, vdev->config); | |
967f97fa | 978 | |
06dbfc6f | 979 | val = ldub_p(vdev->config + addr); |
967f97fa AL |
980 | return val; |
981 | } | |
982 | ||
53c25cea | 983 | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 984 | { |
181103cd | 985 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
986 | uint16_t val; |
987 | ||
5f5a1318 | 988 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 989 | return (uint32_t)-1; |
5f5a1318 JW |
990 | } |
991 | ||
992 | k->get_config(vdev, vdev->config); | |
967f97fa | 993 | |
06dbfc6f | 994 | val = lduw_p(vdev->config + addr); |
967f97fa AL |
995 | return val; |
996 | } | |
997 | ||
53c25cea | 998 | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 999 | { |
181103cd | 1000 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1001 | uint32_t val; |
1002 | ||
5f5a1318 | 1003 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 1004 | return (uint32_t)-1; |
5f5a1318 JW |
1005 | } |
1006 | ||
1007 | k->get_config(vdev, vdev->config); | |
967f97fa | 1008 | |
06dbfc6f | 1009 | val = ldl_p(vdev->config + addr); |
967f97fa AL |
1010 | return val; |
1011 | } | |
1012 | ||
53c25cea | 1013 | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 1014 | { |
181103cd | 1015 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1016 | uint8_t val = data; |
1017 | ||
5f5a1318 | 1018 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 1019 | return; |
5f5a1318 | 1020 | } |
967f97fa | 1021 | |
06dbfc6f | 1022 | stb_p(vdev->config + addr, val); |
967f97fa | 1023 | |
181103cd FK |
1024 | if (k->set_config) { |
1025 | k->set_config(vdev, vdev->config); | |
1026 | } | |
967f97fa AL |
1027 | } |
1028 | ||
53c25cea | 1029 | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 1030 | { |
181103cd | 1031 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1032 | uint16_t val = data; |
1033 | ||
5f5a1318 | 1034 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 1035 | return; |
5f5a1318 | 1036 | } |
967f97fa | 1037 | |
06dbfc6f | 1038 | stw_p(vdev->config + addr, val); |
967f97fa | 1039 | |
181103cd FK |
1040 | if (k->set_config) { |
1041 | k->set_config(vdev, vdev->config); | |
1042 | } | |
967f97fa AL |
1043 | } |
1044 | ||
53c25cea | 1045 | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 1046 | { |
181103cd | 1047 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1048 | uint32_t val = data; |
1049 | ||
5f5a1318 | 1050 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 1051 | return; |
5f5a1318 | 1052 | } |
967f97fa | 1053 | |
06dbfc6f | 1054 | stl_p(vdev->config + addr, val); |
967f97fa | 1055 | |
181103cd FK |
1056 | if (k->set_config) { |
1057 | k->set_config(vdev, vdev->config); | |
1058 | } | |
967f97fa AL |
1059 | } |
1060 | ||
adfb743c MT |
1061 | uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr) |
1062 | { | |
1063 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
1064 | uint8_t val; | |
1065 | ||
1066 | if (addr + sizeof(val) > vdev->config_len) { | |
1067 | return (uint32_t)-1; | |
1068 | } | |
1069 | ||
1070 | k->get_config(vdev, vdev->config); | |
1071 | ||
1072 | val = ldub_p(vdev->config + addr); | |
1073 | return val; | |
1074 | } | |
1075 | ||
1076 | uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr) | |
1077 | { | |
1078 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
1079 | uint16_t val; | |
1080 | ||
1081 | if (addr + sizeof(val) > vdev->config_len) { | |
1082 | return (uint32_t)-1; | |
1083 | } | |
1084 | ||
1085 | k->get_config(vdev, vdev->config); | |
1086 | ||
1087 | val = lduw_le_p(vdev->config + addr); | |
1088 | return val; | |
1089 | } | |
1090 | ||
1091 | uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr) | |
1092 | { | |
1093 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
1094 | uint32_t val; | |
1095 | ||
1096 | if (addr + sizeof(val) > vdev->config_len) { | |
1097 | return (uint32_t)-1; | |
1098 | } | |
1099 | ||
1100 | k->get_config(vdev, vdev->config); | |
1101 | ||
1102 | val = ldl_le_p(vdev->config + addr); | |
1103 | return val; | |
1104 | } | |
1105 | ||
1106 | void virtio_config_modern_writeb(VirtIODevice *vdev, | |
1107 | uint32_t addr, uint32_t data) | |
1108 | { | |
1109 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
1110 | uint8_t val = data; | |
1111 | ||
1112 | if (addr + sizeof(val) > vdev->config_len) { | |
1113 | return; | |
1114 | } | |
1115 | ||
1116 | stb_p(vdev->config + addr, val); | |
1117 | ||
1118 | if (k->set_config) { | |
1119 | k->set_config(vdev, vdev->config); | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | void virtio_config_modern_writew(VirtIODevice *vdev, | |
1124 | uint32_t addr, uint32_t data) | |
1125 | { | |
1126 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
1127 | uint16_t val = data; | |
1128 | ||
1129 | if (addr + sizeof(val) > vdev->config_len) { | |
1130 | return; | |
1131 | } | |
1132 | ||
1133 | stw_le_p(vdev->config + addr, val); | |
1134 | ||
1135 | if (k->set_config) { | |
1136 | k->set_config(vdev, vdev->config); | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | void virtio_config_modern_writel(VirtIODevice *vdev, | |
1141 | uint32_t addr, uint32_t data) | |
1142 | { | |
1143 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
1144 | uint32_t val = data; | |
1145 | ||
1146 | if (addr + sizeof(val) > vdev->config_len) { | |
1147 | return; | |
1148 | } | |
1149 | ||
1150 | stl_le_p(vdev->config + addr, val); | |
1151 | ||
1152 | if (k->set_config) { | |
1153 | k->set_config(vdev, vdev->config); | |
1154 | } | |
1155 | } | |
1156 | ||
a8170e5e | 1157 | void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr) |
967f97fa | 1158 | { |
ab223c95 CH |
1159 | vdev->vq[n].vring.desc = addr; |
1160 | virtio_queue_update_rings(vdev, n); | |
53c25cea PB |
1161 | } |
1162 | ||
a8170e5e | 1163 | hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) |
53c25cea | 1164 | { |
ab223c95 CH |
1165 | return vdev->vq[n].vring.desc; |
1166 | } | |
1167 | ||
1168 | void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc, | |
1169 | hwaddr avail, hwaddr used) | |
1170 | { | |
1171 | vdev->vq[n].vring.desc = desc; | |
1172 | vdev->vq[n].vring.avail = avail; | |
1173 | vdev->vq[n].vring.used = used; | |
53c25cea PB |
1174 | } |
1175 | ||
e63c0ba1 PM |
1176 | void virtio_queue_set_num(VirtIODevice *vdev, int n, int num) |
1177 | { | |
f6049f44 PM |
1178 | /* Don't allow guest to flip queue between existent and |
1179 | * nonexistent states, or to set it to an invalid size. | |
1180 | */ | |
1181 | if (!!num != !!vdev->vq[n].vring.num || | |
1182 | num > VIRTQUEUE_MAX_SIZE || | |
1183 | num < 0) { | |
1184 | return; | |
e63c0ba1 | 1185 | } |
f6049f44 | 1186 | vdev->vq[n].vring.num = num; |
e63c0ba1 PM |
1187 | } |
1188 | ||
e0d686bf JW |
1189 | VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector) |
1190 | { | |
1191 | return QLIST_FIRST(&vdev->vector_queues[vector]); | |
1192 | } | |
1193 | ||
1194 | VirtQueue *virtio_vector_next_queue(VirtQueue *vq) | |
1195 | { | |
1196 | return QLIST_NEXT(vq, node); | |
1197 | } | |
1198 | ||
53c25cea PB |
1199 | int virtio_queue_get_num(VirtIODevice *vdev, int n) |
1200 | { | |
1201 | return vdev->vq[n].vring.num; | |
1202 | } | |
967f97fa | 1203 | |
8ad176aa JW |
1204 | int virtio_get_num_queues(VirtIODevice *vdev) |
1205 | { | |
1206 | int i; | |
1207 | ||
87b3bd1c | 1208 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
8ad176aa JW |
1209 | if (!virtio_queue_get_num(vdev, i)) { |
1210 | break; | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | return i; | |
1215 | } | |
1216 | ||
6ce69d1c PM |
1217 | void virtio_queue_set_align(VirtIODevice *vdev, int n, int align) |
1218 | { | |
1219 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
1220 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1221 | ||
ab223c95 | 1222 | /* virtio-1 compliant devices cannot change the alignment */ |
95129d6f | 1223 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
ab223c95 CH |
1224 | error_report("tried to modify queue alignment for virtio-1 device"); |
1225 | return; | |
1226 | } | |
6ce69d1c PM |
1227 | /* Check that the transport told us it was going to do this |
1228 | * (so a buggy transport will immediately assert rather than | |
1229 | * silently failing to migrate this state) | |
1230 | */ | |
1231 | assert(k->has_variable_vring_alignment); | |
1232 | ||
1233 | vdev->vq[n].vring.align = align; | |
ab223c95 | 1234 | virtio_queue_update_rings(vdev, n); |
6ce69d1c PM |
1235 | } |
1236 | ||
344dc16f MT |
1237 | static void virtio_queue_notify_aio_vq(VirtQueue *vq) |
1238 | { | |
1239 | if (vq->vring.desc && vq->handle_aio_output) { | |
1240 | VirtIODevice *vdev = vq->vdev; | |
1241 | ||
1242 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); | |
1243 | vq->handle_aio_output(vdev, vq); | |
1244 | } | |
1245 | } | |
1246 | ||
2b2cbcad | 1247 | static void virtio_queue_notify_vq(VirtQueue *vq) |
25db9ebe | 1248 | { |
9e0f5b81 | 1249 | if (vq->vring.desc && vq->handle_output) { |
25db9ebe | 1250 | VirtIODevice *vdev = vq->vdev; |
9e0f5b81 | 1251 | |
f5ed3663 SH |
1252 | if (unlikely(vdev->broken)) { |
1253 | return; | |
1254 | } | |
1255 | ||
25db9ebe SH |
1256 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); |
1257 | vq->handle_output(vdev, vq); | |
1258 | } | |
1259 | } | |
1260 | ||
53c25cea PB |
1261 | void virtio_queue_notify(VirtIODevice *vdev, int n) |
1262 | { | |
7157e2e2 | 1263 | virtio_queue_notify_vq(&vdev->vq[n]); |
967f97fa AL |
1264 | } |
1265 | ||
7055e687 MT |
1266 | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n) |
1267 | { | |
87b3bd1c | 1268 | return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector : |
7055e687 MT |
1269 | VIRTIO_NO_VECTOR; |
1270 | } | |
1271 | ||
1272 | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector) | |
1273 | { | |
e0d686bf JW |
1274 | VirtQueue *vq = &vdev->vq[n]; |
1275 | ||
87b3bd1c | 1276 | if (n < VIRTIO_QUEUE_MAX) { |
e0d686bf JW |
1277 | if (vdev->vector_queues && |
1278 | vdev->vq[n].vector != VIRTIO_NO_VECTOR) { | |
1279 | QLIST_REMOVE(vq, node); | |
1280 | } | |
7055e687 | 1281 | vdev->vq[n].vector = vector; |
e0d686bf JW |
1282 | if (vdev->vector_queues && |
1283 | vector != VIRTIO_NO_VECTOR) { | |
1284 | QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node); | |
1285 | } | |
1286 | } | |
7055e687 MT |
1287 | } |
1288 | ||
f1ac6a55 PB |
1289 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, |
1290 | VirtIOHandleOutput handle_output) | |
967f97fa AL |
1291 | { |
1292 | int i; | |
1293 | ||
87b3bd1c | 1294 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
1295 | if (vdev->vq[i].vring.num == 0) |
1296 | break; | |
1297 | } | |
1298 | ||
87b3bd1c | 1299 | if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE) |
967f97fa AL |
1300 | abort(); |
1301 | ||
1302 | vdev->vq[i].vring.num = queue_size; | |
46c5d082 | 1303 | vdev->vq[i].vring.num_default = queue_size; |
6ce69d1c | 1304 | vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN; |
967f97fa | 1305 | vdev->vq[i].handle_output = handle_output; |
344dc16f | 1306 | vdev->vq[i].handle_aio_output = NULL; |
967f97fa AL |
1307 | |
1308 | return &vdev->vq[i]; | |
1309 | } | |
1310 | ||
f23fd811 JW |
1311 | void virtio_del_queue(VirtIODevice *vdev, int n) |
1312 | { | |
87b3bd1c | 1313 | if (n < 0 || n >= VIRTIO_QUEUE_MAX) { |
f23fd811 JW |
1314 | abort(); |
1315 | } | |
1316 | ||
1317 | vdev->vq[n].vring.num = 0; | |
46c5d082 | 1318 | vdev->vq[n].vring.num_default = 0; |
f23fd811 JW |
1319 | } |
1320 | ||
1cbdabe2 MT |
1321 | void virtio_irq(VirtQueue *vq) |
1322 | { | |
64979a4d | 1323 | trace_virtio_irq(vq); |
1cbdabe2 MT |
1324 | vq->vdev->isr |= 0x01; |
1325 | virtio_notify_vector(vq->vdev, vq->vector); | |
1326 | } | |
1327 | ||
adb3feda | 1328 | bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) |
bcbabae8 MT |
1329 | { |
1330 | uint16_t old, new; | |
1331 | bool v; | |
a281ebc1 MT |
1332 | /* We need to expose used array entries before checking used event. */ |
1333 | smp_mb(); | |
97b83deb | 1334 | /* Always notify when queue is empty (when feature acknowledge) */ |
95129d6f | 1335 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) && |
be1fea9b | 1336 | !vq->inuse && virtio_queue_empty(vq)) { |
bcbabae8 MT |
1337 | return true; |
1338 | } | |
1339 | ||
95129d6f | 1340 | if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
bcbabae8 MT |
1341 | return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT); |
1342 | } | |
1343 | ||
1344 | v = vq->signalled_used_valid; | |
1345 | vq->signalled_used_valid = true; | |
1346 | old = vq->signalled_used; | |
b796fcd1 | 1347 | new = vq->signalled_used = vq->used_idx; |
e9600c6c | 1348 | return !v || vring_need_event(vring_get_used_event(vq), new, old); |
bcbabae8 MT |
1349 | } |
1350 | ||
1351 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) | |
1352 | { | |
adb3feda | 1353 | if (!virtio_should_notify(vdev, vq)) { |
967f97fa | 1354 | return; |
bcbabae8 | 1355 | } |
967f97fa | 1356 | |
64979a4d | 1357 | trace_virtio_notify(vdev, vq); |
967f97fa | 1358 | vdev->isr |= 0x01; |
7055e687 | 1359 | virtio_notify_vector(vdev, vq->vector); |
967f97fa AL |
1360 | } |
1361 | ||
1362 | void virtio_notify_config(VirtIODevice *vdev) | |
1363 | { | |
7625162c AL |
1364 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) |
1365 | return; | |
1366 | ||
967f97fa | 1367 | vdev->isr |= 0x03; |
b8f05908 | 1368 | vdev->generation++; |
7055e687 | 1369 | virtio_notify_vector(vdev, vdev->config_vector); |
967f97fa AL |
1370 | } |
1371 | ||
616a6552 GK |
1372 | static bool virtio_device_endian_needed(void *opaque) |
1373 | { | |
1374 | VirtIODevice *vdev = opaque; | |
1375 | ||
1376 | assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN); | |
95129d6f | 1377 | if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
3c185597 CH |
1378 | return vdev->device_endian != virtio_default_endian(); |
1379 | } | |
1380 | /* Devices conforming to VIRTIO 1.0 or later are always LE. */ | |
1381 | return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE; | |
616a6552 GK |
1382 | } |
1383 | ||
019a3edb GH |
1384 | static bool virtio_64bit_features_needed(void *opaque) |
1385 | { | |
1386 | VirtIODevice *vdev = opaque; | |
1387 | ||
1388 | return (vdev->host_features >> 32) != 0; | |
1389 | } | |
1390 | ||
74aae7b2 JW |
1391 | static bool virtio_virtqueue_needed(void *opaque) |
1392 | { | |
1393 | VirtIODevice *vdev = opaque; | |
1394 | ||
1395 | return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1); | |
1396 | } | |
1397 | ||
46c5d082 CH |
1398 | static bool virtio_ringsize_needed(void *opaque) |
1399 | { | |
1400 | VirtIODevice *vdev = opaque; | |
1401 | int i; | |
1402 | ||
1403 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
1404 | if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) { | |
1405 | return true; | |
1406 | } | |
1407 | } | |
1408 | return false; | |
1409 | } | |
1410 | ||
a6df8adf JW |
1411 | static bool virtio_extra_state_needed(void *opaque) |
1412 | { | |
1413 | VirtIODevice *vdev = opaque; | |
1414 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
1415 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1416 | ||
1417 | return k->has_extra_state && | |
1418 | k->has_extra_state(qbus->parent); | |
1419 | } | |
1420 | ||
791b1daf SH |
1421 | static bool virtio_broken_needed(void *opaque) |
1422 | { | |
1423 | VirtIODevice *vdev = opaque; | |
1424 | ||
1425 | return vdev->broken; | |
1426 | } | |
1427 | ||
50e5ae4d | 1428 | static const VMStateDescription vmstate_virtqueue = { |
74aae7b2 | 1429 | .name = "virtqueue_state", |
50e5ae4d DDAG |
1430 | .version_id = 1, |
1431 | .minimum_version_id = 1, | |
1432 | .fields = (VMStateField[]) { | |
1433 | VMSTATE_UINT64(vring.avail, struct VirtQueue), | |
1434 | VMSTATE_UINT64(vring.used, struct VirtQueue), | |
1435 | VMSTATE_END_OF_LIST() | |
1436 | } | |
74aae7b2 JW |
1437 | }; |
1438 | ||
1439 | static const VMStateDescription vmstate_virtio_virtqueues = { | |
1440 | .name = "virtio/virtqueues", | |
1441 | .version_id = 1, | |
1442 | .minimum_version_id = 1, | |
1443 | .needed = &virtio_virtqueue_needed, | |
1444 | .fields = (VMStateField[]) { | |
3e996cc5 DDAG |
1445 | VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice, |
1446 | VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue), | |
74aae7b2 JW |
1447 | VMSTATE_END_OF_LIST() |
1448 | } | |
1449 | }; | |
1450 | ||
50e5ae4d | 1451 | static const VMStateDescription vmstate_ringsize = { |
46c5d082 | 1452 | .name = "ringsize_state", |
50e5ae4d DDAG |
1453 | .version_id = 1, |
1454 | .minimum_version_id = 1, | |
1455 | .fields = (VMStateField[]) { | |
1456 | VMSTATE_UINT32(vring.num_default, struct VirtQueue), | |
1457 | VMSTATE_END_OF_LIST() | |
1458 | } | |
46c5d082 CH |
1459 | }; |
1460 | ||
1461 | static const VMStateDescription vmstate_virtio_ringsize = { | |
1462 | .name = "virtio/ringsize", | |
1463 | .version_id = 1, | |
1464 | .minimum_version_id = 1, | |
1465 | .needed = &virtio_ringsize_needed, | |
1466 | .fields = (VMStateField[]) { | |
3e996cc5 DDAG |
1467 | VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice, |
1468 | VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue), | |
46c5d082 CH |
1469 | VMSTATE_END_OF_LIST() |
1470 | } | |
1471 | }; | |
1472 | ||
a6df8adf JW |
1473 | static int get_extra_state(QEMUFile *f, void *pv, size_t size) |
1474 | { | |
1475 | VirtIODevice *vdev = pv; | |
1476 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
1477 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1478 | ||
1479 | if (!k->load_extra_state) { | |
1480 | return -1; | |
1481 | } else { | |
1482 | return k->load_extra_state(qbus->parent, f); | |
1483 | } | |
1484 | } | |
1485 | ||
1486 | static void put_extra_state(QEMUFile *f, void *pv, size_t size) | |
1487 | { | |
1488 | VirtIODevice *vdev = pv; | |
1489 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
1490 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1491 | ||
1492 | k->save_extra_state(qbus->parent, f); | |
1493 | } | |
1494 | ||
1495 | static const VMStateInfo vmstate_info_extra_state = { | |
1496 | .name = "virtqueue_extra_state", | |
1497 | .get = get_extra_state, | |
1498 | .put = put_extra_state, | |
1499 | }; | |
1500 | ||
1501 | static const VMStateDescription vmstate_virtio_extra_state = { | |
1502 | .name = "virtio/extra_state", | |
1503 | .version_id = 1, | |
1504 | .minimum_version_id = 1, | |
1505 | .needed = &virtio_extra_state_needed, | |
1506 | .fields = (VMStateField[]) { | |
1507 | { | |
1508 | .name = "extra_state", | |
1509 | .version_id = 0, | |
1510 | .field_exists = NULL, | |
1511 | .size = 0, | |
1512 | .info = &vmstate_info_extra_state, | |
1513 | .flags = VMS_SINGLE, | |
1514 | .offset = 0, | |
1515 | }, | |
1516 | VMSTATE_END_OF_LIST() | |
1517 | } | |
1518 | }; | |
1519 | ||
616a6552 GK |
1520 | static const VMStateDescription vmstate_virtio_device_endian = { |
1521 | .name = "virtio/device_endian", | |
1522 | .version_id = 1, | |
1523 | .minimum_version_id = 1, | |
5cd8cada | 1524 | .needed = &virtio_device_endian_needed, |
616a6552 GK |
1525 | .fields = (VMStateField[]) { |
1526 | VMSTATE_UINT8(device_endian, VirtIODevice), | |
1527 | VMSTATE_END_OF_LIST() | |
1528 | } | |
1529 | }; | |
1530 | ||
019a3edb GH |
1531 | static const VMStateDescription vmstate_virtio_64bit_features = { |
1532 | .name = "virtio/64bit_features", | |
1533 | .version_id = 1, | |
1534 | .minimum_version_id = 1, | |
5cd8cada | 1535 | .needed = &virtio_64bit_features_needed, |
019a3edb GH |
1536 | .fields = (VMStateField[]) { |
1537 | VMSTATE_UINT64(guest_features, VirtIODevice), | |
1538 | VMSTATE_END_OF_LIST() | |
1539 | } | |
1540 | }; | |
1541 | ||
791b1daf SH |
1542 | static const VMStateDescription vmstate_virtio_broken = { |
1543 | .name = "virtio/broken", | |
1544 | .version_id = 1, | |
1545 | .minimum_version_id = 1, | |
1546 | .needed = &virtio_broken_needed, | |
1547 | .fields = (VMStateField[]) { | |
1548 | VMSTATE_BOOL(broken, VirtIODevice), | |
1549 | VMSTATE_END_OF_LIST() | |
1550 | } | |
1551 | }; | |
1552 | ||
6b321a3d GK |
1553 | static const VMStateDescription vmstate_virtio = { |
1554 | .name = "virtio", | |
1555 | .version_id = 1, | |
1556 | .minimum_version_id = 1, | |
1557 | .minimum_version_id_old = 1, | |
1558 | .fields = (VMStateField[]) { | |
1559 | VMSTATE_END_OF_LIST() | |
616a6552 | 1560 | }, |
5cd8cada JQ |
1561 | .subsections = (const VMStateDescription*[]) { |
1562 | &vmstate_virtio_device_endian, | |
1563 | &vmstate_virtio_64bit_features, | |
74aae7b2 | 1564 | &vmstate_virtio_virtqueues, |
46c5d082 | 1565 | &vmstate_virtio_ringsize, |
791b1daf | 1566 | &vmstate_virtio_broken, |
a6df8adf | 1567 | &vmstate_virtio_extra_state, |
5cd8cada | 1568 | NULL |
6b321a3d GK |
1569 | } |
1570 | }; | |
1571 | ||
967f97fa AL |
1572 | void virtio_save(VirtIODevice *vdev, QEMUFile *f) |
1573 | { | |
1c819449 FK |
1574 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1575 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1b5fc0de | 1576 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
019a3edb | 1577 | uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff); |
967f97fa AL |
1578 | int i; |
1579 | ||
1c819449 FK |
1580 | if (k->save_config) { |
1581 | k->save_config(qbus->parent, f); | |
1582 | } | |
967f97fa | 1583 | |
967f97fa AL |
1584 | qemu_put_8s(f, &vdev->status); |
1585 | qemu_put_8s(f, &vdev->isr); | |
1586 | qemu_put_be16s(f, &vdev->queue_sel); | |
019a3edb | 1587 | qemu_put_be32s(f, &guest_features_lo); |
967f97fa AL |
1588 | qemu_put_be32(f, vdev->config_len); |
1589 | qemu_put_buffer(f, vdev->config, vdev->config_len); | |
1590 | ||
87b3bd1c | 1591 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
1592 | if (vdev->vq[i].vring.num == 0) |
1593 | break; | |
1594 | } | |
1595 | ||
1596 | qemu_put_be32(f, i); | |
1597 | ||
87b3bd1c | 1598 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
1599 | if (vdev->vq[i].vring.num == 0) |
1600 | break; | |
1601 | ||
1602 | qemu_put_be32(f, vdev->vq[i].vring.num); | |
6ce69d1c PM |
1603 | if (k->has_variable_vring_alignment) { |
1604 | qemu_put_be32(f, vdev->vq[i].vring.align); | |
1605 | } | |
ab223c95 CH |
1606 | /* XXX virtio-1 devices */ |
1607 | qemu_put_be64(f, vdev->vq[i].vring.desc); | |
967f97fa | 1608 | qemu_put_be16s(f, &vdev->vq[i].last_avail_idx); |
1c819449 FK |
1609 | if (k->save_queue) { |
1610 | k->save_queue(qbus->parent, i, f); | |
1611 | } | |
967f97fa | 1612 | } |
1b5fc0de GK |
1613 | |
1614 | if (vdc->save != NULL) { | |
1615 | vdc->save(vdev, f); | |
1616 | } | |
6b321a3d | 1617 | |
ea43e259 DDAG |
1618 | if (vdc->vmsd) { |
1619 | vmstate_save_state(f, vdc->vmsd, vdev, NULL); | |
1620 | } | |
1621 | ||
6b321a3d | 1622 | /* Subsections */ |
8118f095 | 1623 | vmstate_save_state(f, &vmstate_virtio, vdev, NULL); |
967f97fa AL |
1624 | } |
1625 | ||
1a665855 HP |
1626 | /* A wrapper for use as a VMState .put function */ |
1627 | static void virtio_device_put(QEMUFile *f, void *opaque, size_t size) | |
1628 | { | |
1629 | virtio_save(VIRTIO_DEVICE(opaque), f); | |
1630 | } | |
1631 | ||
1632 | /* A wrapper for use as a VMState .get function */ | |
1633 | static int virtio_device_get(QEMUFile *f, void *opaque, size_t size) | |
1634 | { | |
1635 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); | |
1636 | DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev)); | |
1637 | ||
1638 | return virtio_load(vdev, f, dc->vmsd->version_id); | |
1639 | } | |
1640 | ||
1641 | const VMStateInfo virtio_vmstate_info = { | |
1642 | .name = "virtio", | |
1643 | .get = virtio_device_get, | |
1644 | .put = virtio_device_put, | |
1645 | }; | |
1646 | ||
6c0196d7 | 1647 | static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val) |
ad0c9332 | 1648 | { |
181103cd | 1649 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
6b8f1020 | 1650 | bool bad = (val & ~(vdev->host_features)) != 0; |
ad0c9332 | 1651 | |
6b8f1020 | 1652 | val &= vdev->host_features; |
181103cd FK |
1653 | if (k->set_features) { |
1654 | k->set_features(vdev, val); | |
ad0c9332 PB |
1655 | } |
1656 | vdev->guest_features = val; | |
1657 | return bad ? -1 : 0; | |
1658 | } | |
1659 | ||
6c0196d7 CH |
1660 | int virtio_set_features(VirtIODevice *vdev, uint64_t val) |
1661 | { | |
1662 | /* | |
1663 | * The driver must not attempt to set features after feature negotiation | |
1664 | * has finished. | |
1665 | */ | |
1666 | if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) { | |
1667 | return -EINVAL; | |
1668 | } | |
1669 | return virtio_set_features_nocheck(vdev, val); | |
1670 | } | |
1671 | ||
1b5fc0de | 1672 | int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) |
967f97fa | 1673 | { |
cc459952 | 1674 | int i, ret; |
a890a2f9 | 1675 | int32_t config_len; |
cc459952 | 1676 | uint32_t num; |
6d74ca5a | 1677 | uint32_t features; |
1c819449 FK |
1678 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1679 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1b5fc0de | 1680 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa | 1681 | |
616a6552 GK |
1682 | /* |
1683 | * We poison the endianness to ensure it does not get used before | |
1684 | * subsections have been loaded. | |
1685 | */ | |
1686 | vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN; | |
1687 | ||
1c819449 FK |
1688 | if (k->load_config) { |
1689 | ret = k->load_config(qbus->parent, f); | |
ff24bd58 MT |
1690 | if (ret) |
1691 | return ret; | |
1692 | } | |
967f97fa | 1693 | |
967f97fa AL |
1694 | qemu_get_8s(f, &vdev->status); |
1695 | qemu_get_8s(f, &vdev->isr); | |
1696 | qemu_get_be16s(f, &vdev->queue_sel); | |
87b3bd1c | 1697 | if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) { |
4b53c2c7 MR |
1698 | return -1; |
1699 | } | |
6d74ca5a | 1700 | qemu_get_be32s(f, &features); |
ad0c9332 | 1701 | |
62cee1a2 MT |
1702 | /* |
1703 | * Temporarily set guest_features low bits - needed by | |
1704 | * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS | |
1705 | * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ. | |
1706 | * | |
1707 | * Note: devices should always test host features in future - don't create | |
1708 | * new dependencies like this. | |
1709 | */ | |
1710 | vdev->guest_features = features; | |
1711 | ||
a890a2f9 | 1712 | config_len = qemu_get_be32(f); |
2f5732e9 DDAG |
1713 | |
1714 | /* | |
1715 | * There are cases where the incoming config can be bigger or smaller | |
1716 | * than what we have; so load what we have space for, and skip | |
1717 | * any excess that's in the stream. | |
1718 | */ | |
1719 | qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len)); | |
1720 | ||
1721 | while (config_len > vdev->config_len) { | |
1722 | qemu_get_byte(f); | |
1723 | config_len--; | |
a890a2f9 | 1724 | } |
967f97fa AL |
1725 | |
1726 | num = qemu_get_be32(f); | |
1727 | ||
87b3bd1c | 1728 | if (num > VIRTIO_QUEUE_MAX) { |
8a1be662 | 1729 | error_report("Invalid number of virtqueues: 0x%x", num); |
cc459952 MT |
1730 | return -1; |
1731 | } | |
1732 | ||
967f97fa AL |
1733 | for (i = 0; i < num; i++) { |
1734 | vdev->vq[i].vring.num = qemu_get_be32(f); | |
6ce69d1c PM |
1735 | if (k->has_variable_vring_alignment) { |
1736 | vdev->vq[i].vring.align = qemu_get_be32(f); | |
1737 | } | |
ab223c95 | 1738 | vdev->vq[i].vring.desc = qemu_get_be64(f); |
967f97fa | 1739 | qemu_get_be16s(f, &vdev->vq[i].last_avail_idx); |
bcbabae8 MT |
1740 | vdev->vq[i].signalled_used_valid = false; |
1741 | vdev->vq[i].notification = true; | |
967f97fa | 1742 | |
ab223c95 CH |
1743 | if (vdev->vq[i].vring.desc) { |
1744 | /* XXX virtio-1 devices */ | |
1745 | virtio_queue_update_rings(vdev, i); | |
1abeb5a6 MT |
1746 | } else if (vdev->vq[i].last_avail_idx) { |
1747 | error_report("VQ %d address 0x0 " | |
6daf194d | 1748 | "inconsistent with Host index 0x%x", |
1abeb5a6 MT |
1749 | i, vdev->vq[i].last_avail_idx); |
1750 | return -1; | |
8275e2f6 | 1751 | } |
1c819449 FK |
1752 | if (k->load_queue) { |
1753 | ret = k->load_queue(qbus->parent, i, f); | |
ff24bd58 MT |
1754 | if (ret) |
1755 | return ret; | |
7055e687 | 1756 | } |
967f97fa AL |
1757 | } |
1758 | ||
7055e687 | 1759 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
1b5fc0de GK |
1760 | |
1761 | if (vdc->load != NULL) { | |
6b321a3d GK |
1762 | ret = vdc->load(vdev, f, version_id); |
1763 | if (ret) { | |
1764 | return ret; | |
1765 | } | |
1b5fc0de GK |
1766 | } |
1767 | ||
ea43e259 DDAG |
1768 | if (vdc->vmsd) { |
1769 | ret = vmstate_load_state(f, vdc->vmsd, vdev, version_id); | |
1770 | if (ret) { | |
1771 | return ret; | |
1772 | } | |
1773 | } | |
1774 | ||
616a6552 GK |
1775 | /* Subsections */ |
1776 | ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1); | |
1777 | if (ret) { | |
1778 | return ret; | |
1779 | } | |
1780 | ||
1781 | if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) { | |
1782 | vdev->device_endian = virtio_default_endian(); | |
1783 | } | |
1784 | ||
019a3edb GH |
1785 | if (virtio_64bit_features_needed(vdev)) { |
1786 | /* | |
1787 | * Subsection load filled vdev->guest_features. Run them | |
1788 | * through virtio_set_features to sanity-check them against | |
1789 | * host_features. | |
1790 | */ | |
1791 | uint64_t features64 = vdev->guest_features; | |
6c0196d7 | 1792 | if (virtio_set_features_nocheck(vdev, features64) < 0) { |
019a3edb GH |
1793 | error_report("Features 0x%" PRIx64 " unsupported. " |
1794 | "Allowed features: 0x%" PRIx64, | |
1795 | features64, vdev->host_features); | |
1796 | return -1; | |
1797 | } | |
1798 | } else { | |
6c0196d7 | 1799 | if (virtio_set_features_nocheck(vdev, features) < 0) { |
019a3edb GH |
1800 | error_report("Features 0x%x unsupported. " |
1801 | "Allowed features: 0x%" PRIx64, | |
1802 | features, vdev->host_features); | |
1803 | return -1; | |
1804 | } | |
1805 | } | |
1806 | ||
616a6552 | 1807 | for (i = 0; i < num; i++) { |
ab223c95 | 1808 | if (vdev->vq[i].vring.desc) { |
616a6552 GK |
1809 | uint16_t nheads; |
1810 | nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx; | |
1811 | /* Check it isn't doing strange things with descriptor numbers. */ | |
1812 | if (nheads > vdev->vq[i].vring.num) { | |
1813 | error_report("VQ %d size 0x%x Guest index 0x%x " | |
1814 | "inconsistent with Host index 0x%x: delta 0x%x", | |
1815 | i, vdev->vq[i].vring.num, | |
1816 | vring_avail_idx(&vdev->vq[i]), | |
1817 | vdev->vq[i].last_avail_idx, nheads); | |
1818 | return -1; | |
1819 | } | |
b796fcd1 | 1820 | vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]); |
be1fea9b | 1821 | vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]); |
bccdef6b SH |
1822 | |
1823 | /* | |
1824 | * Some devices migrate VirtQueueElements that have been popped | |
1825 | * from the avail ring but not yet returned to the used ring. | |
1826 | */ | |
1827 | vdev->vq[i].inuse = vdev->vq[i].last_avail_idx - | |
1828 | vdev->vq[i].used_idx; | |
1829 | if (vdev->vq[i].inuse > vdev->vq[i].vring.num) { | |
1830 | error_report("VQ %d size 0x%x < last_avail_idx 0x%x - " | |
1831 | "used_idx 0x%x", | |
1832 | i, vdev->vq[i].vring.num, | |
1833 | vdev->vq[i].last_avail_idx, | |
1834 | vdev->vq[i].used_idx); | |
1835 | return -1; | |
1836 | } | |
616a6552 GK |
1837 | } |
1838 | } | |
1839 | ||
1840 | return 0; | |
967f97fa AL |
1841 | } |
1842 | ||
6a1a8cc7 | 1843 | void virtio_cleanup(VirtIODevice *vdev) |
b946a153 | 1844 | { |
85cf2a8d | 1845 | qemu_del_vm_change_state_handler(vdev->vmstate); |
6f79e06b | 1846 | g_free(vdev->config); |
7267c094 | 1847 | g_free(vdev->vq); |
e0d686bf | 1848 | g_free(vdev->vector_queues); |
8e05db92 FK |
1849 | } |
1850 | ||
1dfb4dd9 | 1851 | static void virtio_vmstate_change(void *opaque, int running, RunState state) |
85cf2a8d MT |
1852 | { |
1853 | VirtIODevice *vdev = opaque; | |
1c819449 FK |
1854 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1855 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
85cf2a8d | 1856 | bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK); |
9e8e8c48 | 1857 | vdev->vm_running = running; |
85cf2a8d MT |
1858 | |
1859 | if (backend_run) { | |
1860 | virtio_set_status(vdev, vdev->status); | |
1861 | } | |
1862 | ||
1c819449 FK |
1863 | if (k->vmstate_change) { |
1864 | k->vmstate_change(qbus->parent, backend_run); | |
85cf2a8d MT |
1865 | } |
1866 | ||
1867 | if (!backend_run) { | |
1868 | virtio_set_status(vdev, vdev->status); | |
1869 | } | |
1870 | } | |
1871 | ||
c8075caf GA |
1872 | void virtio_instance_init_common(Object *proxy_obj, void *data, |
1873 | size_t vdev_size, const char *vdev_name) | |
1874 | { | |
1875 | DeviceState *vdev = data; | |
1876 | ||
1877 | object_initialize(vdev, vdev_size, vdev_name); | |
1878 | object_property_add_child(proxy_obj, "virtio-backend", OBJECT(vdev), NULL); | |
1879 | object_unref(OBJECT(vdev)); | |
1880 | qdev_alias_all_properties(vdev, proxy_obj); | |
1881 | } | |
1882 | ||
8e05db92 FK |
1883 | void virtio_init(VirtIODevice *vdev, const char *name, |
1884 | uint16_t device_id, size_t config_size) | |
967f97fa | 1885 | { |
e0d686bf JW |
1886 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1887 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
b8193adb | 1888 | int i; |
e0d686bf JW |
1889 | int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0; |
1890 | ||
1891 | if (nvectors) { | |
1892 | vdev->vector_queues = | |
1893 | g_malloc0(sizeof(*vdev->vector_queues) * nvectors); | |
1894 | } | |
1895 | ||
53c25cea | 1896 | vdev->device_id = device_id; |
967f97fa AL |
1897 | vdev->status = 0; |
1898 | vdev->isr = 0; | |
1899 | vdev->queue_sel = 0; | |
7055e687 | 1900 | vdev->config_vector = VIRTIO_NO_VECTOR; |
87b3bd1c | 1901 | vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX); |
1354869c | 1902 | vdev->vm_running = runstate_is_running(); |
f5ed3663 | 1903 | vdev->broken = false; |
87b3bd1c | 1904 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
b8193adb | 1905 | vdev->vq[i].vector = VIRTIO_NO_VECTOR; |
1cbdabe2 | 1906 | vdev->vq[i].vdev = vdev; |
e78a2b42 | 1907 | vdev->vq[i].queue_index = i; |
1cbdabe2 | 1908 | } |
967f97fa | 1909 | |
967f97fa AL |
1910 | vdev->name = name; |
1911 | vdev->config_len = config_size; | |
8e05db92 | 1912 | if (vdev->config_len) { |
7267c094 | 1913 | vdev->config = g_malloc0(config_size); |
8e05db92 | 1914 | } else { |
967f97fa | 1915 | vdev->config = NULL; |
8e05db92 FK |
1916 | } |
1917 | vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change, | |
1918 | vdev); | |
616a6552 | 1919 | vdev->device_endian = virtio_default_endian(); |
5669655a | 1920 | vdev->use_guest_notifier_mask = true; |
8e05db92 | 1921 | } |
967f97fa | 1922 | |
a8170e5e | 1923 | hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1924 | { |
1925 | return vdev->vq[n].vring.desc; | |
1926 | } | |
1927 | ||
a8170e5e | 1928 | hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1929 | { |
1930 | return vdev->vq[n].vring.avail; | |
1931 | } | |
1932 | ||
a8170e5e | 1933 | hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1934 | { |
1935 | return vdev->vq[n].vring.used; | |
1936 | } | |
1937 | ||
a8170e5e | 1938 | hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1939 | { |
1940 | return vdev->vq[n].vring.desc; | |
1941 | } | |
1942 | ||
a8170e5e | 1943 | hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1944 | { |
1945 | return sizeof(VRingDesc) * vdev->vq[n].vring.num; | |
1946 | } | |
1947 | ||
a8170e5e | 1948 | hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1949 | { |
1950 | return offsetof(VRingAvail, ring) + | |
50764fc8 | 1951 | sizeof(uint16_t) * vdev->vq[n].vring.num; |
1cbdabe2 MT |
1952 | } |
1953 | ||
a8170e5e | 1954 | hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1955 | { |
1956 | return offsetof(VRingUsed, ring) + | |
1957 | sizeof(VRingUsedElem) * vdev->vq[n].vring.num; | |
1958 | } | |
1959 | ||
a8170e5e | 1960 | hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1961 | { |
1962 | return vdev->vq[n].vring.used - vdev->vq[n].vring.desc + | |
1963 | virtio_queue_get_used_size(vdev, n); | |
1964 | } | |
1965 | ||
1966 | uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) | |
1967 | { | |
1968 | return vdev->vq[n].last_avail_idx; | |
1969 | } | |
1970 | ||
1971 | void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx) | |
1972 | { | |
1973 | vdev->vq[n].last_avail_idx = idx; | |
be1fea9b | 1974 | vdev->vq[n].shadow_avail_idx = idx; |
1cbdabe2 MT |
1975 | } |
1976 | ||
6793dfd1 SH |
1977 | void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n) |
1978 | { | |
1979 | vdev->vq[n].signalled_used_valid = false; | |
1980 | } | |
1981 | ||
1cbdabe2 MT |
1982 | VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n) |
1983 | { | |
1984 | return vdev->vq + n; | |
1985 | } | |
1986 | ||
e78a2b42 JW |
1987 | uint16_t virtio_get_queue_index(VirtQueue *vq) |
1988 | { | |
1989 | return vq->queue_index; | |
1990 | } | |
1991 | ||
15b2bd18 PB |
1992 | static void virtio_queue_guest_notifier_read(EventNotifier *n) |
1993 | { | |
1994 | VirtQueue *vq = container_of(n, VirtQueue, guest_notifier); | |
1995 | if (event_notifier_test_and_clear(n)) { | |
1996 | virtio_irq(vq); | |
1997 | } | |
1998 | } | |
1999 | ||
2000 | void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, | |
2001 | bool with_irqfd) | |
2002 | { | |
2003 | if (assign && !with_irqfd) { | |
54e18d35 | 2004 | event_notifier_set_handler(&vq->guest_notifier, false, |
15b2bd18 PB |
2005 | virtio_queue_guest_notifier_read); |
2006 | } else { | |
54e18d35 | 2007 | event_notifier_set_handler(&vq->guest_notifier, false, NULL); |
15b2bd18 PB |
2008 | } |
2009 | if (!assign) { | |
2010 | /* Test and clear notifier before closing it, | |
2011 | * in case poll callback didn't have time to run. */ | |
2012 | virtio_queue_guest_notifier_read(&vq->guest_notifier); | |
2013 | } | |
2014 | } | |
2015 | ||
1cbdabe2 MT |
2016 | EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq) |
2017 | { | |
2018 | return &vq->guest_notifier; | |
2019 | } | |
b1f416aa | 2020 | |
344dc16f | 2021 | static void virtio_queue_host_notifier_aio_read(EventNotifier *n) |
b1f416aa PB |
2022 | { |
2023 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
2024 | if (event_notifier_test_and_clear(n)) { | |
344dc16f | 2025 | virtio_queue_notify_aio_vq(vq); |
b1f416aa PB |
2026 | } |
2027 | } | |
2028 | ||
a1afb606 | 2029 | void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx, |
bf1780b0 | 2030 | VirtIOHandleOutput handle_output) |
a1afb606 | 2031 | { |
a378b49a PB |
2032 | if (handle_output) { |
2033 | vq->handle_aio_output = handle_output; | |
a1afb606 | 2034 | aio_set_event_notifier(ctx, &vq->host_notifier, true, |
344dc16f | 2035 | virtio_queue_host_notifier_aio_read); |
a1afb606 PB |
2036 | } else { |
2037 | aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL); | |
a1afb606 PB |
2038 | /* Test and clear notifier before after disabling event, |
2039 | * in case poll callback didn't have time to run. */ | |
344dc16f | 2040 | virtio_queue_host_notifier_aio_read(&vq->host_notifier); |
a378b49a | 2041 | vq->handle_aio_output = NULL; |
344dc16f MT |
2042 | } |
2043 | } | |
2044 | ||
2045 | static void virtio_queue_host_notifier_read(EventNotifier *n) | |
2046 | { | |
2047 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
2048 | if (event_notifier_test_and_clear(n)) { | |
2049 | virtio_queue_notify_vq(vq); | |
a1afb606 PB |
2050 | } |
2051 | } | |
2052 | ||
26b9b5fe PB |
2053 | void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign, |
2054 | bool set_handler) | |
b1f416aa | 2055 | { |
26b9b5fe | 2056 | if (assign && set_handler) { |
f1ac6a55 | 2057 | event_notifier_set_handler(&vq->host_notifier, true, |
b1f416aa PB |
2058 | virtio_queue_host_notifier_read); |
2059 | } else { | |
f1ac6a55 | 2060 | event_notifier_set_handler(&vq->host_notifier, true, NULL); |
26b9b5fe PB |
2061 | } |
2062 | if (!assign) { | |
b1f416aa PB |
2063 | /* Test and clear notifier before after disabling event, |
2064 | * in case poll callback didn't have time to run. */ | |
2065 | virtio_queue_host_notifier_read(&vq->host_notifier); | |
2066 | } | |
2067 | } | |
2068 | ||
1cbdabe2 MT |
2069 | EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq) |
2070 | { | |
2071 | return &vq->host_notifier; | |
2072 | } | |
8e05db92 | 2073 | |
1034e9cf FK |
2074 | void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name) |
2075 | { | |
9e288406 | 2076 | g_free(vdev->bus_name); |
80e0090a | 2077 | vdev->bus_name = g_strdup(bus_name); |
1034e9cf FK |
2078 | } |
2079 | ||
f5ed3663 SH |
2080 | void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice *vdev, const char *fmt, ...) |
2081 | { | |
2082 | va_list ap; | |
2083 | ||
2084 | va_start(ap, fmt); | |
2085 | error_vreport(fmt, ap); | |
2086 | va_end(ap); | |
2087 | ||
2088 | vdev->broken = true; | |
2089 | ||
2090 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { | |
2091 | virtio_set_status(vdev, vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET); | |
2092 | virtio_notify_config(vdev); | |
2093 | } | |
2094 | } | |
2095 | ||
1d244b42 AF |
2096 | static void virtio_device_realize(DeviceState *dev, Error **errp) |
2097 | { | |
2098 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
2099 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); | |
2100 | Error *err = NULL; | |
2101 | ||
ea43e259 DDAG |
2102 | /* Devices should either use vmsd or the load/save methods */ |
2103 | assert(!vdc->vmsd || !vdc->load); | |
2104 | ||
1d244b42 AF |
2105 | if (vdc->realize != NULL) { |
2106 | vdc->realize(dev, &err); | |
2107 | if (err != NULL) { | |
2108 | error_propagate(errp, err); | |
2109 | return; | |
2110 | } | |
8e05db92 | 2111 | } |
e8398045 JW |
2112 | |
2113 | virtio_bus_device_plugged(vdev, &err); | |
2114 | if (err != NULL) { | |
2115 | error_propagate(errp, err); | |
2116 | return; | |
2117 | } | |
8e05db92 FK |
2118 | } |
2119 | ||
1d244b42 | 2120 | static void virtio_device_unrealize(DeviceState *dev, Error **errp) |
1034e9cf | 2121 | { |
1d244b42 | 2122 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
306ec6c3 AF |
2123 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); |
2124 | Error *err = NULL; | |
1d244b42 | 2125 | |
83d07047 PB |
2126 | virtio_bus_device_unplugged(vdev); |
2127 | ||
306ec6c3 AF |
2128 | if (vdc->unrealize != NULL) { |
2129 | vdc->unrealize(dev, &err); | |
2130 | if (err != NULL) { | |
2131 | error_propagate(errp, err); | |
2132 | return; | |
2133 | } | |
5e96f5d2 | 2134 | } |
1d244b42 | 2135 | |
9e288406 MA |
2136 | g_free(vdev->bus_name); |
2137 | vdev->bus_name = NULL; | |
1034e9cf FK |
2138 | } |
2139 | ||
6b8f1020 CH |
2140 | static Property virtio_properties[] = { |
2141 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features), | |
2142 | DEFINE_PROP_END_OF_LIST(), | |
2143 | }; | |
2144 | ||
ff4c07df PB |
2145 | static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev) |
2146 | { | |
2147 | VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev))); | |
ff4c07df PB |
2148 | int n, r, err; |
2149 | ||
2150 | for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { | |
2151 | if (!virtio_queue_get_num(vdev, n)) { | |
2152 | continue; | |
2153 | } | |
ed08a2a0 | 2154 | r = virtio_bus_set_host_notifier(qbus, n, true); |
ff4c07df PB |
2155 | if (r < 0) { |
2156 | err = r; | |
2157 | goto assign_error; | |
2158 | } | |
6019f3b9 PB |
2159 | virtio_queue_set_host_notifier_fd_handler(&vdev->vq[n], true, true); |
2160 | } | |
2161 | ||
2162 | for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { | |
2163 | /* Kick right away to begin processing requests already in vring */ | |
2164 | VirtQueue *vq = &vdev->vq[n]; | |
2165 | if (!vq->vring.num) { | |
2166 | continue; | |
2167 | } | |
2168 | event_notifier_set(&vq->host_notifier); | |
ff4c07df PB |
2169 | } |
2170 | return 0; | |
2171 | ||
2172 | assign_error: | |
2173 | while (--n >= 0) { | |
2174 | if (!virtio_queue_get_num(vdev, n)) { | |
2175 | continue; | |
2176 | } | |
2177 | ||
ed08a2a0 | 2178 | r = virtio_bus_set_host_notifier(qbus, n, false); |
ff4c07df PB |
2179 | assert(r >= 0); |
2180 | } | |
2181 | return err; | |
2182 | } | |
2183 | ||
2184 | int virtio_device_start_ioeventfd(VirtIODevice *vdev) | |
2185 | { | |
2186 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
2187 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
2188 | ||
2189 | return virtio_bus_start_ioeventfd(vbus); | |
2190 | } | |
2191 | ||
2192 | static void virtio_device_stop_ioeventfd_impl(VirtIODevice *vdev) | |
2193 | { | |
2194 | VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev))); | |
ff4c07df PB |
2195 | int n, r; |
2196 | ||
2197 | for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { | |
2198 | if (!virtio_queue_get_num(vdev, n)) { | |
2199 | continue; | |
2200 | } | |
ed08a2a0 | 2201 | r = virtio_bus_set_host_notifier(qbus, n, false); |
ff4c07df PB |
2202 | assert(r >= 0); |
2203 | } | |
2204 | } | |
2205 | ||
2206 | void virtio_device_stop_ioeventfd(VirtIODevice *vdev) | |
2207 | { | |
2208 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
2209 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
2210 | ||
2211 | virtio_bus_stop_ioeventfd(vbus); | |
2212 | } | |
2213 | ||
8e05db92 FK |
2214 | static void virtio_device_class_init(ObjectClass *klass, void *data) |
2215 | { | |
2216 | /* Set the default value here. */ | |
ff4c07df | 2217 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
8e05db92 | 2218 | DeviceClass *dc = DEVICE_CLASS(klass); |
1d244b42 AF |
2219 | |
2220 | dc->realize = virtio_device_realize; | |
2221 | dc->unrealize = virtio_device_unrealize; | |
8e05db92 | 2222 | dc->bus_type = TYPE_VIRTIO_BUS; |
6b8f1020 | 2223 | dc->props = virtio_properties; |
ff4c07df PB |
2224 | vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl; |
2225 | vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl; | |
8e05db92 FK |
2226 | } |
2227 | ||
8e93cef1 PB |
2228 | bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev) |
2229 | { | |
2230 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
2231 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
2232 | ||
2233 | return virtio_bus_ioeventfd_enabled(vbus); | |
2234 | } | |
2235 | ||
8e05db92 FK |
2236 | static const TypeInfo virtio_device_info = { |
2237 | .name = TYPE_VIRTIO_DEVICE, | |
2238 | .parent = TYPE_DEVICE, | |
2239 | .instance_size = sizeof(VirtIODevice), | |
2240 | .class_init = virtio_device_class_init, | |
2241 | .abstract = true, | |
2242 | .class_size = sizeof(VirtioDeviceClass), | |
2243 | }; | |
2244 | ||
2245 | static void virtio_register_types(void) | |
2246 | { | |
2247 | type_register_static(&virtio_device_info); | |
2248 | } | |
2249 | ||
2250 | type_init(virtio_register_types) |