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