]>
Commit | Line | Data |
---|---|---|
967f97fa AL |
1 | /* |
2 | * Virtio Support | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
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 | 16 | #include "cpu.h" |
64979a4d | 17 | #include "trace.h" |
fdfba1a2 | 18 | #include "exec/address-spaces.h" |
1de7afc9 | 19 | #include "qemu/error-report.h" |
db725815 | 20 | #include "qemu/main-loop.h" |
0b8fa32f | 21 | #include "qemu/module.h" |
0d09e41a | 22 | #include "hw/virtio/virtio.h" |
ca77ee28 | 23 | #include "migration/qemu-file-types.h" |
1de7afc9 | 24 | #include "qemu/atomic.h" |
0d09e41a | 25 | #include "hw/virtio/virtio-bus.h" |
a27bd6c7 | 26 | #include "hw/qdev-properties.h" |
cee3ca00 | 27 | #include "hw/virtio/virtio-access.h" |
8607f5c3 | 28 | #include "sysemu/dma.h" |
54d31236 | 29 | #include "sysemu/runstate.h" |
967f97fa | 30 | |
6ce69d1c PM |
31 | /* |
32 | * The alignment to use between consumer and producer parts of vring. | |
33 | * x86 pagesize again. This is the default, used by transports like PCI | |
34 | * which don't provide a means for the guest to tell the host the alignment. | |
35 | */ | |
f46f15bc AL |
36 | #define VIRTIO_PCI_VRING_ALIGN 4096 |
37 | ||
967f97fa AL |
38 | typedef struct VRingDesc |
39 | { | |
40 | uint64_t addr; | |
41 | uint32_t len; | |
42 | uint16_t flags; | |
43 | uint16_t next; | |
44 | } VRingDesc; | |
45 | ||
a40dcec9 WX |
46 | typedef struct VRingPackedDesc { |
47 | uint64_t addr; | |
48 | uint32_t len; | |
49 | uint16_t id; | |
50 | uint16_t flags; | |
51 | } VRingPackedDesc; | |
52 | ||
967f97fa AL |
53 | typedef struct VRingAvail |
54 | { | |
55 | uint16_t flags; | |
56 | uint16_t idx; | |
57 | uint16_t ring[0]; | |
58 | } VRingAvail; | |
59 | ||
60 | typedef struct VRingUsedElem | |
61 | { | |
62 | uint32_t id; | |
63 | uint32_t len; | |
64 | } VRingUsedElem; | |
65 | ||
66 | typedef struct VRingUsed | |
67 | { | |
68 | uint16_t flags; | |
69 | uint16_t idx; | |
70 | VRingUsedElem ring[0]; | |
71 | } VRingUsed; | |
72 | ||
c611c764 PB |
73 | typedef struct VRingMemoryRegionCaches { |
74 | struct rcu_head rcu; | |
75 | MemoryRegionCache desc; | |
76 | MemoryRegionCache avail; | |
77 | MemoryRegionCache used; | |
78 | } VRingMemoryRegionCaches; | |
79 | ||
967f97fa AL |
80 | typedef struct VRing |
81 | { | |
82 | unsigned int num; | |
46c5d082 | 83 | unsigned int num_default; |
6ce69d1c | 84 | unsigned int align; |
a8170e5e AK |
85 | hwaddr desc; |
86 | hwaddr avail; | |
87 | hwaddr used; | |
c611c764 | 88 | VRingMemoryRegionCaches *caches; |
967f97fa AL |
89 | } VRing; |
90 | ||
a40dcec9 WX |
91 | typedef struct VRingPackedDescEvent { |
92 | uint16_t off_wrap; | |
93 | uint16_t flags; | |
94 | } VRingPackedDescEvent ; | |
95 | ||
967f97fa AL |
96 | struct VirtQueue |
97 | { | |
98 | VRing vring; | |
86044b24 | 99 | VirtQueueElement *used_elems; |
be1fea9b VM |
100 | |
101 | /* Next head to pop */ | |
967f97fa | 102 | uint16_t last_avail_idx; |
a40dcec9 | 103 | bool last_avail_wrap_counter; |
b796fcd1 | 104 | |
be1fea9b VM |
105 | /* Last avail_idx read from VQ. */ |
106 | uint16_t shadow_avail_idx; | |
a40dcec9 | 107 | bool shadow_avail_wrap_counter; |
be1fea9b | 108 | |
b796fcd1 | 109 | uint16_t used_idx; |
a40dcec9 | 110 | bool used_wrap_counter; |
b796fcd1 | 111 | |
bcbabae8 MT |
112 | /* Last used index value we have signalled on */ |
113 | uint16_t signalled_used; | |
114 | ||
115 | /* Last used index value we have signalled on */ | |
116 | bool signalled_used_valid; | |
117 | ||
332fa82d SH |
118 | /* Notification enabled? */ |
119 | bool notification; | |
bcbabae8 | 120 | |
e78a2b42 JW |
121 | uint16_t queue_index; |
122 | ||
e66bcc40 | 123 | unsigned int inuse; |
bcbabae8 | 124 | |
7055e687 | 125 | uint16_t vector; |
bf1780b0 | 126 | VirtIOHandleOutput handle_output; |
07931698 | 127 | VirtIOHandleAIOOutput handle_aio_output; |
1cbdabe2 MT |
128 | VirtIODevice *vdev; |
129 | EventNotifier guest_notifier; | |
130 | EventNotifier host_notifier; | |
fcccb271 | 131 | bool host_notifier_enabled; |
e0d686bf | 132 | QLIST_ENTRY(VirtQueue) node; |
967f97fa AL |
133 | }; |
134 | ||
c611c764 PB |
135 | static void virtio_free_region_cache(VRingMemoryRegionCaches *caches) |
136 | { | |
137 | if (!caches) { | |
138 | return; | |
139 | } | |
140 | ||
141 | address_space_cache_destroy(&caches->desc); | |
142 | address_space_cache_destroy(&caches->avail); | |
143 | address_space_cache_destroy(&caches->used); | |
144 | g_free(caches); | |
145 | } | |
146 | ||
45641dba PB |
147 | static void virtio_virtqueue_reset_region_cache(struct VirtQueue *vq) |
148 | { | |
149 | VRingMemoryRegionCaches *caches; | |
150 | ||
151 | caches = atomic_read(&vq->vring.caches); | |
152 | atomic_rcu_set(&vq->vring.caches, NULL); | |
153 | if (caches) { | |
154 | call_rcu(caches, virtio_free_region_cache, rcu); | |
155 | } | |
156 | } | |
157 | ||
c611c764 PB |
158 | static void virtio_init_region_cache(VirtIODevice *vdev, int n) |
159 | { | |
160 | VirtQueue *vq = &vdev->vq[n]; | |
161 | VRingMemoryRegionCaches *old = vq->vring.caches; | |
45641dba | 162 | VRingMemoryRegionCaches *new = NULL; |
c611c764 | 163 | hwaddr addr, size; |
e45da653 | 164 | int64_t len; |
86044b24 | 165 | bool packed; |
c611c764 | 166 | |
c611c764 PB |
167 | |
168 | addr = vq->vring.desc; | |
169 | if (!addr) { | |
45641dba | 170 | goto out_no_cache; |
c611c764 PB |
171 | } |
172 | new = g_new0(VRingMemoryRegionCaches, 1); | |
173 | size = virtio_queue_get_desc_size(vdev, n); | |
86044b24 JW |
174 | packed = virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ? |
175 | true : false; | |
e45da653 | 176 | len = address_space_cache_init(&new->desc, vdev->dma_as, |
86044b24 | 177 | addr, size, packed); |
e45da653 JW |
178 | if (len < size) { |
179 | virtio_error(vdev, "Cannot map desc"); | |
180 | goto err_desc; | |
181 | } | |
c611c764 | 182 | |
f90cda63 | 183 | size = virtio_queue_get_used_size(vdev, n); |
e45da653 JW |
184 | len = address_space_cache_init(&new->used, vdev->dma_as, |
185 | vq->vring.used, size, true); | |
186 | if (len < size) { | |
187 | virtio_error(vdev, "Cannot map used"); | |
188 | goto err_used; | |
189 | } | |
c611c764 | 190 | |
f90cda63 | 191 | size = virtio_queue_get_avail_size(vdev, n); |
e45da653 JW |
192 | len = address_space_cache_init(&new->avail, vdev->dma_as, |
193 | vq->vring.avail, size, false); | |
194 | if (len < size) { | |
195 | virtio_error(vdev, "Cannot map avail"); | |
196 | goto err_avail; | |
197 | } | |
c611c764 PB |
198 | |
199 | atomic_rcu_set(&vq->vring.caches, new); | |
200 | if (old) { | |
201 | call_rcu(old, virtio_free_region_cache, rcu); | |
202 | } | |
e45da653 JW |
203 | return; |
204 | ||
205 | err_avail: | |
45641dba | 206 | address_space_cache_destroy(&new->avail); |
e45da653 | 207 | err_used: |
45641dba | 208 | address_space_cache_destroy(&new->used); |
e45da653 | 209 | err_desc: |
45641dba PB |
210 | address_space_cache_destroy(&new->desc); |
211 | out_no_cache: | |
e45da653 | 212 | g_free(new); |
45641dba | 213 | virtio_virtqueue_reset_region_cache(vq); |
c611c764 PB |
214 | } |
215 | ||
967f97fa | 216 | /* virt queue functions */ |
ab223c95 | 217 | void virtio_queue_update_rings(VirtIODevice *vdev, int n) |
967f97fa | 218 | { |
ab223c95 | 219 | VRing *vring = &vdev->vq[n].vring; |
53c25cea | 220 | |
758ead31 | 221 | if (!vring->num || !vring->desc || !vring->align) { |
ab223c95 CH |
222 | /* not yet setup -> nothing to do */ |
223 | return; | |
224 | } | |
225 | vring->avail = vring->desc + vring->num * sizeof(VRingDesc); | |
226 | vring->used = vring_align(vring->avail + | |
227 | offsetof(VRingAvail, ring[vring->num]), | |
228 | vring->align); | |
c611c764 | 229 | virtio_init_region_cache(vdev, n); |
967f97fa AL |
230 | } |
231 | ||
97cd965c | 232 | /* Called within rcu_read_lock(). */ |
86044b24 JW |
233 | static void vring_split_desc_read(VirtIODevice *vdev, VRingDesc *desc, |
234 | MemoryRegionCache *cache, int i) | |
967f97fa | 235 | { |
5eba0404 PB |
236 | address_space_read_cached(cache, i * sizeof(VRingDesc), |
237 | desc, sizeof(VRingDesc)); | |
aa570d6f PB |
238 | virtio_tswap64s(vdev, &desc->addr); |
239 | virtio_tswap32s(vdev, &desc->len); | |
240 | virtio_tswap16s(vdev, &desc->flags); | |
241 | virtio_tswap16s(vdev, &desc->next); | |
967f97fa AL |
242 | } |
243 | ||
683f7665 JW |
244 | static void vring_packed_event_read(VirtIODevice *vdev, |
245 | MemoryRegionCache *cache, | |
246 | VRingPackedDescEvent *e) | |
247 | { | |
248 | hwaddr off_off = offsetof(VRingPackedDescEvent, off_wrap); | |
249 | hwaddr off_flags = offsetof(VRingPackedDescEvent, flags); | |
250 | ||
251 | address_space_read_cached(cache, off_flags, &e->flags, | |
252 | sizeof(e->flags)); | |
253 | /* Make sure flags is seen before off_wrap */ | |
254 | smp_rmb(); | |
255 | address_space_read_cached(cache, off_off, &e->off_wrap, | |
256 | sizeof(e->off_wrap)); | |
257 | virtio_tswap16s(vdev, &e->off_wrap); | |
258 | virtio_tswap16s(vdev, &e->flags); | |
259 | } | |
260 | ||
261 | static void vring_packed_off_wrap_write(VirtIODevice *vdev, | |
262 | MemoryRegionCache *cache, | |
263 | uint16_t off_wrap) | |
264 | { | |
265 | hwaddr off = offsetof(VRingPackedDescEvent, off_wrap); | |
266 | ||
267 | virtio_tswap16s(vdev, &off_wrap); | |
268 | address_space_write_cached(cache, off, &off_wrap, sizeof(off_wrap)); | |
269 | address_space_cache_invalidate(cache, off, sizeof(off_wrap)); | |
270 | } | |
271 | ||
272 | static void vring_packed_flags_write(VirtIODevice *vdev, | |
273 | MemoryRegionCache *cache, uint16_t flags) | |
274 | { | |
275 | hwaddr off = offsetof(VRingPackedDescEvent, flags); | |
276 | ||
277 | virtio_tswap16s(vdev, &flags); | |
278 | address_space_write_cached(cache, off, &flags, sizeof(flags)); | |
279 | address_space_cache_invalidate(cache, off, sizeof(flags)); | |
280 | } | |
281 | ||
86044b24 | 282 | /* Called within rcu_read_lock(). */ |
e0e2d644 JW |
283 | static VRingMemoryRegionCaches *vring_get_region_caches(struct VirtQueue *vq) |
284 | { | |
285 | VRingMemoryRegionCaches *caches = atomic_rcu_read(&vq->vring.caches); | |
286 | assert(caches != NULL); | |
287 | return caches; | |
288 | } | |
97cd965c | 289 | /* Called within rcu_read_lock(). */ |
967f97fa AL |
290 | static inline uint16_t vring_avail_flags(VirtQueue *vq) |
291 | { | |
e0e2d644 | 292 | VRingMemoryRegionCaches *caches = vring_get_region_caches(vq); |
97cd965c PB |
293 | hwaddr pa = offsetof(VRingAvail, flags); |
294 | return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa); | |
967f97fa AL |
295 | } |
296 | ||
97cd965c | 297 | /* Called within rcu_read_lock(). */ |
967f97fa AL |
298 | static inline uint16_t vring_avail_idx(VirtQueue *vq) |
299 | { | |
e0e2d644 | 300 | VRingMemoryRegionCaches *caches = vring_get_region_caches(vq); |
97cd965c PB |
301 | hwaddr pa = offsetof(VRingAvail, idx); |
302 | vq->shadow_avail_idx = virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa); | |
be1fea9b | 303 | return vq->shadow_avail_idx; |
967f97fa AL |
304 | } |
305 | ||
97cd965c | 306 | /* Called within rcu_read_lock(). */ |
967f97fa AL |
307 | static inline uint16_t vring_avail_ring(VirtQueue *vq, int i) |
308 | { | |
e0e2d644 | 309 | VRingMemoryRegionCaches *caches = vring_get_region_caches(vq); |
97cd965c PB |
310 | hwaddr pa = offsetof(VRingAvail, ring[i]); |
311 | return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa); | |
967f97fa AL |
312 | } |
313 | ||
97cd965c | 314 | /* Called within rcu_read_lock(). */ |
e9600c6c | 315 | static inline uint16_t vring_get_used_event(VirtQueue *vq) |
bcbabae8 MT |
316 | { |
317 | return vring_avail_ring(vq, vq->vring.num); | |
318 | } | |
319 | ||
97cd965c | 320 | /* Called within rcu_read_lock(). */ |
1cdd2ee5 VM |
321 | static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem, |
322 | int i) | |
967f97fa | 323 | { |
e0e2d644 | 324 | VRingMemoryRegionCaches *caches = vring_get_region_caches(vq); |
97cd965c | 325 | hwaddr pa = offsetof(VRingUsed, ring[i]); |
1cdd2ee5 VM |
326 | virtio_tswap32s(vq->vdev, &uelem->id); |
327 | virtio_tswap32s(vq->vdev, &uelem->len); | |
97cd965c PB |
328 | address_space_write_cached(&caches->used, pa, uelem, sizeof(VRingUsedElem)); |
329 | address_space_cache_invalidate(&caches->used, pa, sizeof(VRingUsedElem)); | |
967f97fa AL |
330 | } |
331 | ||
97cd965c | 332 | /* Called within rcu_read_lock(). */ |
967f97fa AL |
333 | static uint16_t vring_used_idx(VirtQueue *vq) |
334 | { | |
e0e2d644 | 335 | VRingMemoryRegionCaches *caches = vring_get_region_caches(vq); |
97cd965c PB |
336 | hwaddr pa = offsetof(VRingUsed, idx); |
337 | return virtio_lduw_phys_cached(vq->vdev, &caches->used, pa); | |
967f97fa AL |
338 | } |
339 | ||
97cd965c | 340 | /* Called within rcu_read_lock(). */ |
bcbabae8 | 341 | static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val) |
967f97fa | 342 | { |
e0e2d644 | 343 | VRingMemoryRegionCaches *caches = vring_get_region_caches(vq); |
97cd965c PB |
344 | hwaddr pa = offsetof(VRingUsed, idx); |
345 | virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val); | |
346 | address_space_cache_invalidate(&caches->used, pa, sizeof(val)); | |
b796fcd1 | 347 | vq->used_idx = val; |
967f97fa AL |
348 | } |
349 | ||
97cd965c | 350 | /* Called within rcu_read_lock(). */ |
967f97fa AL |
351 | static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask) |
352 | { | |
e0e2d644 | 353 | VRingMemoryRegionCaches *caches = vring_get_region_caches(vq); |
cee3ca00 | 354 | VirtIODevice *vdev = vq->vdev; |
97cd965c PB |
355 | hwaddr pa = offsetof(VRingUsed, flags); |
356 | uint16_t flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa); | |
357 | ||
358 | virtio_stw_phys_cached(vdev, &caches->used, pa, flags | mask); | |
359 | address_space_cache_invalidate(&caches->used, pa, sizeof(flags)); | |
967f97fa AL |
360 | } |
361 | ||
97cd965c | 362 | /* Called within rcu_read_lock(). */ |
967f97fa AL |
363 | static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask) |
364 | { | |
e0e2d644 | 365 | VRingMemoryRegionCaches *caches = vring_get_region_caches(vq); |
cee3ca00 | 366 | VirtIODevice *vdev = vq->vdev; |
97cd965c PB |
367 | hwaddr pa = offsetof(VRingUsed, flags); |
368 | uint16_t flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa); | |
369 | ||
370 | virtio_stw_phys_cached(vdev, &caches->used, pa, flags & ~mask); | |
371 | address_space_cache_invalidate(&caches->used, pa, sizeof(flags)); | |
967f97fa AL |
372 | } |
373 | ||
97cd965c | 374 | /* Called within rcu_read_lock(). */ |
e9600c6c | 375 | static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val) |
bcbabae8 | 376 | { |
97cd965c | 377 | VRingMemoryRegionCaches *caches; |
a8170e5e | 378 | hwaddr pa; |
332fa82d | 379 | if (!vq->notification) { |
bcbabae8 MT |
380 | return; |
381 | } | |
97cd965c | 382 | |
e0e2d644 | 383 | caches = vring_get_region_caches(vq); |
97cd965c PB |
384 | pa = offsetof(VRingUsed, ring[vq->vring.num]); |
385 | virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val); | |
3cdf8473 | 386 | address_space_cache_invalidate(&caches->used, pa, sizeof(val)); |
bcbabae8 MT |
387 | } |
388 | ||
683f7665 | 389 | static void virtio_queue_split_set_notification(VirtQueue *vq, int enable) |
967f97fa | 390 | { |
b5f53d04 DDAG |
391 | RCU_READ_LOCK_GUARD(); |
392 | ||
95129d6f | 393 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { |
e9600c6c | 394 | vring_set_avail_event(vq, vring_avail_idx(vq)); |
bcbabae8 | 395 | } else if (enable) { |
967f97fa | 396 | vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 397 | } else { |
967f97fa | 398 | vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 399 | } |
92045d80 MT |
400 | if (enable) { |
401 | /* Expose avail event/used flags before caller checks the avail idx. */ | |
402 | smp_mb(); | |
403 | } | |
967f97fa AL |
404 | } |
405 | ||
683f7665 JW |
406 | static void virtio_queue_packed_set_notification(VirtQueue *vq, int enable) |
407 | { | |
408 | uint16_t off_wrap; | |
409 | VRingPackedDescEvent e; | |
410 | VRingMemoryRegionCaches *caches; | |
411 | ||
b5f53d04 | 412 | RCU_READ_LOCK_GUARD(); |
683f7665 JW |
413 | caches = vring_get_region_caches(vq); |
414 | vring_packed_event_read(vq->vdev, &caches->used, &e); | |
415 | ||
416 | if (!enable) { | |
417 | e.flags = VRING_PACKED_EVENT_FLAG_DISABLE; | |
418 | } else if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { | |
419 | off_wrap = vq->shadow_avail_idx | vq->shadow_avail_wrap_counter << 15; | |
420 | vring_packed_off_wrap_write(vq->vdev, &caches->used, off_wrap); | |
421 | /* Make sure off_wrap is wrote before flags */ | |
422 | smp_wmb(); | |
423 | e.flags = VRING_PACKED_EVENT_FLAG_DESC; | |
424 | } else { | |
425 | e.flags = VRING_PACKED_EVENT_FLAG_ENABLE; | |
426 | } | |
427 | ||
428 | vring_packed_flags_write(vq->vdev, &caches->used, e.flags); | |
429 | if (enable) { | |
430 | /* Expose avail event/used flags before caller checks the avail idx. */ | |
431 | smp_mb(); | |
432 | } | |
683f7665 JW |
433 | } |
434 | ||
435 | void virtio_queue_set_notification(VirtQueue *vq, int enable) | |
436 | { | |
437 | vq->notification = enable; | |
438 | ||
439 | if (!vq->vring.desc) { | |
440 | return; | |
441 | } | |
442 | ||
443 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { | |
444 | virtio_queue_packed_set_notification(vq, enable); | |
445 | } else { | |
446 | virtio_queue_split_set_notification(vq, enable); | |
447 | } | |
448 | } | |
449 | ||
967f97fa AL |
450 | int virtio_queue_ready(VirtQueue *vq) |
451 | { | |
452 | return vq->vring.avail != 0; | |
453 | } | |
454 | ||
86044b24 JW |
455 | static void vring_packed_desc_read_flags(VirtIODevice *vdev, |
456 | uint16_t *flags, | |
457 | MemoryRegionCache *cache, | |
458 | int i) | |
459 | { | |
460 | address_space_read_cached(cache, | |
461 | i * sizeof(VRingPackedDesc) + | |
462 | offsetof(VRingPackedDesc, flags), | |
463 | flags, sizeof(*flags)); | |
464 | virtio_tswap16s(vdev, flags); | |
465 | } | |
466 | ||
467 | static void vring_packed_desc_read(VirtIODevice *vdev, | |
468 | VRingPackedDesc *desc, | |
469 | MemoryRegionCache *cache, | |
470 | int i, bool strict_order) | |
471 | { | |
472 | hwaddr off = i * sizeof(VRingPackedDesc); | |
473 | ||
474 | vring_packed_desc_read_flags(vdev, &desc->flags, cache, i); | |
475 | ||
476 | if (strict_order) { | |
477 | /* Make sure flags is read before the rest fields. */ | |
478 | smp_rmb(); | |
479 | } | |
480 | ||
481 | address_space_read_cached(cache, off + offsetof(VRingPackedDesc, addr), | |
482 | &desc->addr, sizeof(desc->addr)); | |
483 | address_space_read_cached(cache, off + offsetof(VRingPackedDesc, id), | |
484 | &desc->id, sizeof(desc->id)); | |
485 | address_space_read_cached(cache, off + offsetof(VRingPackedDesc, len), | |
486 | &desc->len, sizeof(desc->len)); | |
487 | virtio_tswap64s(vdev, &desc->addr); | |
488 | virtio_tswap16s(vdev, &desc->id); | |
489 | virtio_tswap32s(vdev, &desc->len); | |
490 | } | |
491 | ||
492 | static void vring_packed_desc_write_data(VirtIODevice *vdev, | |
493 | VRingPackedDesc *desc, | |
494 | MemoryRegionCache *cache, | |
495 | int i) | |
496 | { | |
497 | hwaddr off_id = i * sizeof(VRingPackedDesc) + | |
498 | offsetof(VRingPackedDesc, id); | |
499 | hwaddr off_len = i * sizeof(VRingPackedDesc) + | |
500 | offsetof(VRingPackedDesc, len); | |
501 | ||
502 | virtio_tswap32s(vdev, &desc->len); | |
503 | virtio_tswap16s(vdev, &desc->id); | |
504 | address_space_write_cached(cache, off_id, &desc->id, sizeof(desc->id)); | |
505 | address_space_cache_invalidate(cache, off_id, sizeof(desc->id)); | |
506 | address_space_write_cached(cache, off_len, &desc->len, sizeof(desc->len)); | |
507 | address_space_cache_invalidate(cache, off_len, sizeof(desc->len)); | |
508 | } | |
509 | ||
510 | static void vring_packed_desc_write_flags(VirtIODevice *vdev, | |
511 | VRingPackedDesc *desc, | |
512 | MemoryRegionCache *cache, | |
513 | int i) | |
514 | { | |
515 | hwaddr off = i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, flags); | |
516 | ||
517 | virtio_tswap16s(vdev, &desc->flags); | |
518 | address_space_write_cached(cache, off, &desc->flags, sizeof(desc->flags)); | |
519 | address_space_cache_invalidate(cache, off, sizeof(desc->flags)); | |
520 | } | |
521 | ||
522 | static void vring_packed_desc_write(VirtIODevice *vdev, | |
523 | VRingPackedDesc *desc, | |
524 | MemoryRegionCache *cache, | |
525 | int i, bool strict_order) | |
526 | { | |
527 | vring_packed_desc_write_data(vdev, desc, cache, i); | |
528 | if (strict_order) { | |
529 | /* Make sure data is wrote before flags. */ | |
530 | smp_wmb(); | |
531 | } | |
532 | vring_packed_desc_write_flags(vdev, desc, cache, i); | |
533 | } | |
534 | ||
535 | static inline bool is_desc_avail(uint16_t flags, bool wrap_counter) | |
536 | { | |
537 | bool avail, used; | |
538 | ||
539 | avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL)); | |
540 | used = !!(flags & (1 << VRING_PACKED_DESC_F_USED)); | |
541 | return (avail != used) && (avail == wrap_counter); | |
542 | } | |
543 | ||
be1fea9b | 544 | /* Fetch avail_idx from VQ memory only when we really need to know if |
97cd965c PB |
545 | * guest has added some buffers. |
546 | * Called within rcu_read_lock(). */ | |
547 | static int virtio_queue_empty_rcu(VirtQueue *vq) | |
967f97fa | 548 | { |
2d1df859 FZ |
549 | if (unlikely(vq->vdev->broken)) { |
550 | return 1; | |
551 | } | |
552 | ||
168e4af3 JW |
553 | if (unlikely(!vq->vring.avail)) { |
554 | return 1; | |
555 | } | |
556 | ||
be1fea9b VM |
557 | if (vq->shadow_avail_idx != vq->last_avail_idx) { |
558 | return 0; | |
559 | } | |
560 | ||
967f97fa AL |
561 | return vring_avail_idx(vq) == vq->last_avail_idx; |
562 | } | |
563 | ||
86044b24 | 564 | static int virtio_queue_split_empty(VirtQueue *vq) |
97cd965c PB |
565 | { |
566 | bool empty; | |
567 | ||
2d1df859 FZ |
568 | if (unlikely(vq->vdev->broken)) { |
569 | return 1; | |
570 | } | |
571 | ||
168e4af3 JW |
572 | if (unlikely(!vq->vring.avail)) { |
573 | return 1; | |
574 | } | |
575 | ||
97cd965c PB |
576 | if (vq->shadow_avail_idx != vq->last_avail_idx) { |
577 | return 0; | |
578 | } | |
579 | ||
b5f53d04 | 580 | RCU_READ_LOCK_GUARD(); |
97cd965c | 581 | empty = vring_avail_idx(vq) == vq->last_avail_idx; |
97cd965c PB |
582 | return empty; |
583 | } | |
584 | ||
86044b24 JW |
585 | static int virtio_queue_packed_empty_rcu(VirtQueue *vq) |
586 | { | |
587 | struct VRingPackedDesc desc; | |
588 | VRingMemoryRegionCaches *cache; | |
589 | ||
590 | if (unlikely(!vq->vring.desc)) { | |
591 | return 1; | |
592 | } | |
593 | ||
594 | cache = vring_get_region_caches(vq); | |
595 | vring_packed_desc_read_flags(vq->vdev, &desc.flags, &cache->desc, | |
596 | vq->last_avail_idx); | |
597 | ||
598 | return !is_desc_avail(desc.flags, vq->last_avail_wrap_counter); | |
599 | } | |
600 | ||
601 | static int virtio_queue_packed_empty(VirtQueue *vq) | |
602 | { | |
b5f53d04 DDAG |
603 | RCU_READ_LOCK_GUARD(); |
604 | return virtio_queue_packed_empty_rcu(vq); | |
86044b24 JW |
605 | } |
606 | ||
607 | int virtio_queue_empty(VirtQueue *vq) | |
608 | { | |
609 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { | |
610 | return virtio_queue_packed_empty(vq); | |
611 | } else { | |
612 | return virtio_queue_split_empty(vq); | |
613 | } | |
614 | } | |
615 | ||
ce317461 JW |
616 | static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, |
617 | unsigned int len) | |
967f97fa | 618 | { |
8607f5c3 | 619 | AddressSpace *dma_as = vq->vdev->dma_as; |
967f97fa AL |
620 | unsigned int offset; |
621 | int i; | |
622 | ||
967f97fa AL |
623 | offset = 0; |
624 | for (i = 0; i < elem->in_num; i++) { | |
625 | size_t size = MIN(len - offset, elem->in_sg[i].iov_len); | |
626 | ||
8607f5c3 JW |
627 | dma_memory_unmap(dma_as, elem->in_sg[i].iov_base, |
628 | elem->in_sg[i].iov_len, | |
629 | DMA_DIRECTION_FROM_DEVICE, size); | |
967f97fa | 630 | |
0cea71a2 | 631 | offset += size; |
967f97fa AL |
632 | } |
633 | ||
26b258e1 | 634 | for (i = 0; i < elem->out_num; i++) |
8607f5c3 JW |
635 | dma_memory_unmap(dma_as, elem->out_sg[i].iov_base, |
636 | elem->out_sg[i].iov_len, | |
637 | DMA_DIRECTION_TO_DEVICE, | |
638 | elem->out_sg[i].iov_len); | |
ce317461 JW |
639 | } |
640 | ||
2640d2a5 SH |
641 | /* virtqueue_detach_element: |
642 | * @vq: The #VirtQueue | |
643 | * @elem: The #VirtQueueElement | |
644 | * @len: number of bytes written | |
645 | * | |
646 | * Detach the element from the virtqueue. This function is suitable for device | |
647 | * reset or other situations where a #VirtQueueElement is simply freed and will | |
648 | * not be pushed or discarded. | |
649 | */ | |
650 | void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem, | |
651 | unsigned int len) | |
652 | { | |
86044b24 | 653 | vq->inuse -= elem->ndescs; |
2640d2a5 SH |
654 | virtqueue_unmap_sg(vq, elem, len); |
655 | } | |
656 | ||
86044b24 JW |
657 | static void virtqueue_split_rewind(VirtQueue *vq, unsigned int num) |
658 | { | |
659 | vq->last_avail_idx -= num; | |
660 | } | |
661 | ||
662 | static void virtqueue_packed_rewind(VirtQueue *vq, unsigned int num) | |
663 | { | |
664 | if (vq->last_avail_idx < num) { | |
665 | vq->last_avail_idx = vq->vring.num + vq->last_avail_idx - num; | |
666 | vq->last_avail_wrap_counter ^= 1; | |
667 | } else { | |
668 | vq->last_avail_idx -= num; | |
669 | } | |
670 | } | |
671 | ||
27e57efe | 672 | /* virtqueue_unpop: |
2640d2a5 SH |
673 | * @vq: The #VirtQueue |
674 | * @elem: The #VirtQueueElement | |
675 | * @len: number of bytes written | |
676 | * | |
677 | * Pretend the most recent element wasn't popped from the virtqueue. The next | |
678 | * call to virtqueue_pop() will refetch the element. | |
679 | */ | |
27e57efe LP |
680 | void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem, |
681 | unsigned int len) | |
29b9f5ef | 682 | { |
86044b24 JW |
683 | |
684 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { | |
685 | virtqueue_packed_rewind(vq, 1); | |
686 | } else { | |
687 | virtqueue_split_rewind(vq, 1); | |
688 | } | |
689 | ||
2640d2a5 | 690 | virtqueue_detach_element(vq, elem, len); |
29b9f5ef JW |
691 | } |
692 | ||
297a75e6 SH |
693 | /* virtqueue_rewind: |
694 | * @vq: The #VirtQueue | |
695 | * @num: Number of elements to push back | |
696 | * | |
697 | * Pretend that elements weren't popped from the virtqueue. The next | |
698 | * virtqueue_pop() will refetch the oldest element. | |
699 | * | |
27e57efe | 700 | * Use virtqueue_unpop() instead if you have a VirtQueueElement. |
297a75e6 SH |
701 | * |
702 | * Returns: true on success, false if @num is greater than the number of in use | |
703 | * elements. | |
704 | */ | |
705 | bool virtqueue_rewind(VirtQueue *vq, unsigned int num) | |
706 | { | |
707 | if (num > vq->inuse) { | |
708 | return false; | |
709 | } | |
86044b24 | 710 | |
297a75e6 | 711 | vq->inuse -= num; |
86044b24 JW |
712 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { |
713 | virtqueue_packed_rewind(vq, num); | |
714 | } else { | |
715 | virtqueue_split_rewind(vq, num); | |
716 | } | |
297a75e6 SH |
717 | return true; |
718 | } | |
719 | ||
86044b24 | 720 | static void virtqueue_split_fill(VirtQueue *vq, const VirtQueueElement *elem, |
ce317461 JW |
721 | unsigned int len, unsigned int idx) |
722 | { | |
1cdd2ee5 VM |
723 | VRingUsedElem uelem; |
724 | ||
168e4af3 JW |
725 | if (unlikely(!vq->vring.used)) { |
726 | return; | |
727 | } | |
728 | ||
b796fcd1 | 729 | idx = (idx + vq->used_idx) % vq->vring.num; |
967f97fa | 730 | |
1cdd2ee5 VM |
731 | uelem.id = elem->index; |
732 | uelem.len = len; | |
733 | vring_used_write(vq, &uelem, idx); | |
967f97fa AL |
734 | } |
735 | ||
86044b24 JW |
736 | static void virtqueue_packed_fill(VirtQueue *vq, const VirtQueueElement *elem, |
737 | unsigned int len, unsigned int idx) | |
738 | { | |
739 | vq->used_elems[idx].index = elem->index; | |
740 | vq->used_elems[idx].len = len; | |
741 | vq->used_elems[idx].ndescs = elem->ndescs; | |
742 | } | |
743 | ||
744 | static void virtqueue_packed_fill_desc(VirtQueue *vq, | |
745 | const VirtQueueElement *elem, | |
746 | unsigned int idx, | |
747 | bool strict_order) | |
748 | { | |
749 | uint16_t head; | |
750 | VRingMemoryRegionCaches *caches; | |
751 | VRingPackedDesc desc = { | |
752 | .id = elem->index, | |
753 | .len = elem->len, | |
754 | }; | |
755 | bool wrap_counter = vq->used_wrap_counter; | |
756 | ||
757 | if (unlikely(!vq->vring.desc)) { | |
758 | return; | |
759 | } | |
760 | ||
761 | head = vq->used_idx + idx; | |
762 | if (head >= vq->vring.num) { | |
763 | head -= vq->vring.num; | |
764 | wrap_counter ^= 1; | |
765 | } | |
766 | if (wrap_counter) { | |
767 | desc.flags |= (1 << VRING_PACKED_DESC_F_AVAIL); | |
768 | desc.flags |= (1 << VRING_PACKED_DESC_F_USED); | |
769 | } else { | |
770 | desc.flags &= ~(1 << VRING_PACKED_DESC_F_AVAIL); | |
771 | desc.flags &= ~(1 << VRING_PACKED_DESC_F_USED); | |
772 | } | |
773 | ||
774 | caches = vring_get_region_caches(vq); | |
775 | vring_packed_desc_write(vq->vdev, &desc, &caches->desc, head, strict_order); | |
776 | } | |
777 | ||
97cd965c | 778 | /* Called within rcu_read_lock(). */ |
86044b24 JW |
779 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, |
780 | unsigned int len, unsigned int idx) | |
967f97fa | 781 | { |
86044b24 JW |
782 | trace_virtqueue_fill(vq, elem, len, idx); |
783 | ||
784 | virtqueue_unmap_sg(vq, elem, len); | |
f5ed3663 SH |
785 | |
786 | if (unlikely(vq->vdev->broken)) { | |
f5ed3663 SH |
787 | return; |
788 | } | |
789 | ||
86044b24 JW |
790 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { |
791 | virtqueue_packed_fill(vq, elem, len, idx); | |
792 | } else { | |
793 | virtqueue_split_fill(vq, elem, len, idx); | |
794 | } | |
795 | } | |
796 | ||
797 | /* Called within rcu_read_lock(). */ | |
798 | static void virtqueue_split_flush(VirtQueue *vq, unsigned int count) | |
799 | { | |
800 | uint16_t old, new; | |
801 | ||
168e4af3 JW |
802 | if (unlikely(!vq->vring.used)) { |
803 | return; | |
804 | } | |
805 | ||
967f97fa | 806 | /* Make sure buffer is written before we update index. */ |
b90d2f35 | 807 | smp_wmb(); |
64979a4d | 808 | trace_virtqueue_flush(vq, count); |
b796fcd1 | 809 | old = vq->used_idx; |
bcbabae8 MT |
810 | new = old + count; |
811 | vring_used_idx_set(vq, new); | |
967f97fa | 812 | vq->inuse -= count; |
bcbabae8 MT |
813 | if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))) |
814 | vq->signalled_used_valid = false; | |
967f97fa AL |
815 | } |
816 | ||
86044b24 JW |
817 | static void virtqueue_packed_flush(VirtQueue *vq, unsigned int count) |
818 | { | |
819 | unsigned int i, ndescs = 0; | |
820 | ||
821 | if (unlikely(!vq->vring.desc)) { | |
822 | return; | |
823 | } | |
824 | ||
825 | for (i = 1; i < count; i++) { | |
826 | virtqueue_packed_fill_desc(vq, &vq->used_elems[i], i, false); | |
827 | ndescs += vq->used_elems[i].ndescs; | |
828 | } | |
829 | virtqueue_packed_fill_desc(vq, &vq->used_elems[0], 0, true); | |
830 | ndescs += vq->used_elems[0].ndescs; | |
831 | ||
832 | vq->inuse -= ndescs; | |
833 | vq->used_idx += ndescs; | |
834 | if (vq->used_idx >= vq->vring.num) { | |
835 | vq->used_idx -= vq->vring.num; | |
836 | vq->used_wrap_counter ^= 1; | |
837 | } | |
838 | } | |
839 | ||
840 | void virtqueue_flush(VirtQueue *vq, unsigned int count) | |
841 | { | |
842 | if (unlikely(vq->vdev->broken)) { | |
843 | vq->inuse -= count; | |
844 | return; | |
845 | } | |
846 | ||
847 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { | |
848 | virtqueue_packed_flush(vq, count); | |
849 | } else { | |
850 | virtqueue_split_flush(vq, count); | |
851 | } | |
852 | } | |
853 | ||
967f97fa AL |
854 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, |
855 | unsigned int len) | |
856 | { | |
b5f53d04 | 857 | RCU_READ_LOCK_GUARD(); |
967f97fa AL |
858 | virtqueue_fill(vq, elem, len, 0); |
859 | virtqueue_flush(vq, 1); | |
860 | } | |
861 | ||
97cd965c | 862 | /* Called within rcu_read_lock(). */ |
967f97fa AL |
863 | static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx) |
864 | { | |
865 | uint16_t num_heads = vring_avail_idx(vq) - idx; | |
866 | ||
867 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
bb6834cf | 868 | if (num_heads > vq->vring.num) { |
4355c1ab | 869 | virtio_error(vq->vdev, "Guest moved used index from %u to %u", |
be1fea9b | 870 | idx, vq->shadow_avail_idx); |
4355c1ab | 871 | return -EINVAL; |
bb6834cf | 872 | } |
a821ce59 MT |
873 | /* On success, callers read a descriptor at vq->last_avail_idx. |
874 | * Make sure descriptor read does not bypass avail index read. */ | |
875 | if (num_heads) { | |
876 | smp_rmb(); | |
877 | } | |
967f97fa AL |
878 | |
879 | return num_heads; | |
880 | } | |
881 | ||
97cd965c | 882 | /* Called within rcu_read_lock(). */ |
fb1131b6 SH |
883 | static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx, |
884 | unsigned int *head) | |
967f97fa | 885 | { |
967f97fa AL |
886 | /* Grab the next descriptor number they're advertising, and increment |
887 | * the index we've seen. */ | |
fb1131b6 | 888 | *head = vring_avail_ring(vq, idx % vq->vring.num); |
967f97fa AL |
889 | |
890 | /* If their number is silly, that's a fatal mistake. */ | |
fb1131b6 SH |
891 | if (*head >= vq->vring.num) { |
892 | virtio_error(vq->vdev, "Guest says index %u is available", *head); | |
893 | return false; | |
bb6834cf | 894 | } |
967f97fa | 895 | |
fb1131b6 | 896 | return true; |
967f97fa AL |
897 | } |
898 | ||
412e0e81 SH |
899 | enum { |
900 | VIRTQUEUE_READ_DESC_ERROR = -1, | |
901 | VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */ | |
902 | VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */ | |
903 | }; | |
967f97fa | 904 | |
86044b24 JW |
905 | static int virtqueue_split_read_next_desc(VirtIODevice *vdev, VRingDesc *desc, |
906 | MemoryRegionCache *desc_cache, | |
907 | unsigned int max, unsigned int *next) | |
412e0e81 | 908 | { |
967f97fa | 909 | /* If this descriptor says it doesn't chain, we're done. */ |
aa570d6f | 910 | if (!(desc->flags & VRING_DESC_F_NEXT)) { |
412e0e81 | 911 | return VIRTQUEUE_READ_DESC_DONE; |
cee3ca00 | 912 | } |
967f97fa AL |
913 | |
914 | /* Check they're not leading us off end of descriptors. */ | |
412e0e81 | 915 | *next = desc->next; |
967f97fa | 916 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
b90d2f35 | 917 | smp_wmb(); |
967f97fa | 918 | |
412e0e81 SH |
919 | if (*next >= max) { |
920 | virtio_error(vdev, "Desc next is %u", *next); | |
921 | return VIRTQUEUE_READ_DESC_ERROR; | |
bb6834cf | 922 | } |
967f97fa | 923 | |
86044b24 | 924 | vring_split_desc_read(vdev, desc, desc_cache, *next); |
412e0e81 | 925 | return VIRTQUEUE_READ_DESC_MORE; |
967f97fa AL |
926 | } |
927 | ||
86044b24 JW |
928 | static void virtqueue_split_get_avail_bytes(VirtQueue *vq, |
929 | unsigned int *in_bytes, unsigned int *out_bytes, | |
930 | unsigned max_in_bytes, unsigned max_out_bytes) | |
967f97fa | 931 | { |
9796d0ac PB |
932 | VirtIODevice *vdev = vq->vdev; |
933 | unsigned int max, idx; | |
385ce95d | 934 | unsigned int total_bufs, in_total, out_total; |
991976f7 | 935 | VRingMemoryRegionCaches *caches; |
5eba0404 PB |
936 | MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID; |
937 | int64_t len = 0; | |
412e0e81 | 938 | int rc; |
967f97fa | 939 | |
b5f53d04 DDAG |
940 | RCU_READ_LOCK_GUARD(); |
941 | ||
967f97fa | 942 | idx = vq->last_avail_idx; |
efeea6d0 | 943 | total_bufs = in_total = out_total = 0; |
9796d0ac PB |
944 | |
945 | max = vq->vring.num; | |
e0e2d644 | 946 | caches = vring_get_region_caches(vq); |
4355c1ab | 947 | while ((rc = virtqueue_num_heads(vq, idx)) > 0) { |
991976f7 | 948 | MemoryRegionCache *desc_cache = &caches->desc; |
9796d0ac | 949 | unsigned int num_bufs; |
aa570d6f | 950 | VRingDesc desc; |
b1c7c07f | 951 | unsigned int i; |
967f97fa | 952 | |
efeea6d0 | 953 | num_bufs = total_bufs; |
fb1131b6 SH |
954 | |
955 | if (!virtqueue_get_head(vq, idx++, &i)) { | |
956 | goto err; | |
957 | } | |
958 | ||
86044b24 | 959 | vring_split_desc_read(vdev, &desc, desc_cache, i); |
efeea6d0 | 960 | |
aa570d6f | 961 | if (desc.flags & VRING_DESC_F_INDIRECT) { |
74231929 | 962 | if (!desc.len || (desc.len % sizeof(VRingDesc))) { |
d65abf85 SH |
963 | virtio_error(vdev, "Invalid size for indirect buffer table"); |
964 | goto err; | |
efeea6d0 MM |
965 | } |
966 | ||
967 | /* If we've got too many, that implies a descriptor loop. */ | |
968 | if (num_bufs >= max) { | |
d65abf85 SH |
969 | virtio_error(vdev, "Looped descriptor"); |
970 | goto err; | |
efeea6d0 MM |
971 | } |
972 | ||
973 | /* loop over the indirect descriptor table */ | |
5eba0404 PB |
974 | len = address_space_cache_init(&indirect_desc_cache, |
975 | vdev->dma_as, | |
976 | desc.addr, desc.len, false); | |
977 | desc_cache = &indirect_desc_cache; | |
9796d0ac PB |
978 | if (len < desc.len) { |
979 | virtio_error(vdev, "Cannot map indirect buffer"); | |
980 | goto err; | |
981 | } | |
982 | ||
aa570d6f | 983 | max = desc.len / sizeof(VRingDesc); |
1ae2757c | 984 | num_bufs = i = 0; |
86044b24 | 985 | vring_split_desc_read(vdev, &desc, desc_cache, i); |
efeea6d0 MM |
986 | } |
987 | ||
967f97fa AL |
988 | do { |
989 | /* If we've got too many, that implies a descriptor loop. */ | |
5774cf98 | 990 | if (++num_bufs > max) { |
d65abf85 SH |
991 | virtio_error(vdev, "Looped descriptor"); |
992 | goto err; | |
bb6834cf | 993 | } |
967f97fa | 994 | |
aa570d6f PB |
995 | if (desc.flags & VRING_DESC_F_WRITE) { |
996 | in_total += desc.len; | |
967f97fa | 997 | } else { |
aa570d6f | 998 | out_total += desc.len; |
967f97fa | 999 | } |
e1f7b481 MT |
1000 | if (in_total >= max_in_bytes && out_total >= max_out_bytes) { |
1001 | goto done; | |
1002 | } | |
412e0e81 | 1003 | |
86044b24 | 1004 | rc = virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max, &i); |
412e0e81 SH |
1005 | } while (rc == VIRTQUEUE_READ_DESC_MORE); |
1006 | ||
1007 | if (rc == VIRTQUEUE_READ_DESC_ERROR) { | |
1008 | goto err; | |
1009 | } | |
efeea6d0 | 1010 | |
5eba0404 PB |
1011 | if (desc_cache == &indirect_desc_cache) { |
1012 | address_space_cache_destroy(&indirect_desc_cache); | |
efeea6d0 | 1013 | total_bufs++; |
9796d0ac PB |
1014 | } else { |
1015 | total_bufs = num_bufs; | |
1016 | } | |
967f97fa | 1017 | } |
4355c1ab SH |
1018 | |
1019 | if (rc < 0) { | |
1020 | goto err; | |
1021 | } | |
1022 | ||
e1f7b481 | 1023 | done: |
5eba0404 | 1024 | address_space_cache_destroy(&indirect_desc_cache); |
0d8d7690 | 1025 | if (in_bytes) { |
86044b24 JW |
1026 | *in_bytes = in_total; |
1027 | } | |
1028 | if (out_bytes) { | |
1029 | *out_bytes = out_total; | |
1030 | } | |
86044b24 JW |
1031 | return; |
1032 | ||
1033 | err: | |
1034 | in_total = out_total = 0; | |
1035 | goto done; | |
1036 | } | |
1037 | ||
1038 | static int virtqueue_packed_read_next_desc(VirtQueue *vq, | |
1039 | VRingPackedDesc *desc, | |
1040 | MemoryRegionCache | |
1041 | *desc_cache, | |
1042 | unsigned int max, | |
1043 | unsigned int *next, | |
1044 | bool indirect) | |
1045 | { | |
1046 | /* If this descriptor says it doesn't chain, we're done. */ | |
1047 | if (!indirect && !(desc->flags & VRING_DESC_F_NEXT)) { | |
1048 | return VIRTQUEUE_READ_DESC_DONE; | |
1049 | } | |
1050 | ||
1051 | ++*next; | |
1052 | if (*next == max) { | |
1053 | if (indirect) { | |
1054 | return VIRTQUEUE_READ_DESC_DONE; | |
1055 | } else { | |
1056 | (*next) -= vq->vring.num; | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | vring_packed_desc_read(vq->vdev, desc, desc_cache, *next, false); | |
1061 | return VIRTQUEUE_READ_DESC_MORE; | |
1062 | } | |
1063 | ||
1064 | static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, | |
1065 | unsigned int *in_bytes, | |
1066 | unsigned int *out_bytes, | |
1067 | unsigned max_in_bytes, | |
1068 | unsigned max_out_bytes) | |
1069 | { | |
1070 | VirtIODevice *vdev = vq->vdev; | |
1071 | unsigned int max, idx; | |
1072 | unsigned int total_bufs, in_total, out_total; | |
1073 | MemoryRegionCache *desc_cache; | |
1074 | VRingMemoryRegionCaches *caches; | |
1075 | MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID; | |
1076 | int64_t len = 0; | |
1077 | VRingPackedDesc desc; | |
1078 | bool wrap_counter; | |
1079 | ||
b5f53d04 | 1080 | RCU_READ_LOCK_GUARD(); |
86044b24 JW |
1081 | idx = vq->last_avail_idx; |
1082 | wrap_counter = vq->last_avail_wrap_counter; | |
1083 | total_bufs = in_total = out_total = 0; | |
1084 | ||
1085 | max = vq->vring.num; | |
1086 | caches = vring_get_region_caches(vq); | |
1087 | ||
1088 | for (;;) { | |
1089 | unsigned int num_bufs = total_bufs; | |
1090 | unsigned int i = idx; | |
1091 | int rc; | |
1092 | ||
1093 | desc_cache = &caches->desc; | |
1094 | vring_packed_desc_read(vdev, &desc, desc_cache, idx, true); | |
1095 | if (!is_desc_avail(desc.flags, wrap_counter)) { | |
1096 | break; | |
1097 | } | |
1098 | ||
1099 | if (desc.flags & VRING_DESC_F_INDIRECT) { | |
1100 | if (desc.len % sizeof(VRingPackedDesc)) { | |
1101 | virtio_error(vdev, "Invalid size for indirect buffer table"); | |
1102 | goto err; | |
1103 | } | |
1104 | ||
1105 | /* If we've got too many, that implies a descriptor loop. */ | |
1106 | if (num_bufs >= max) { | |
1107 | virtio_error(vdev, "Looped descriptor"); | |
1108 | goto err; | |
1109 | } | |
1110 | ||
1111 | /* loop over the indirect descriptor table */ | |
1112 | len = address_space_cache_init(&indirect_desc_cache, | |
1113 | vdev->dma_as, | |
1114 | desc.addr, desc.len, false); | |
1115 | desc_cache = &indirect_desc_cache; | |
1116 | if (len < desc.len) { | |
1117 | virtio_error(vdev, "Cannot map indirect buffer"); | |
1118 | goto err; | |
1119 | } | |
1120 | ||
1121 | max = desc.len / sizeof(VRingPackedDesc); | |
1122 | num_bufs = i = 0; | |
1123 | vring_packed_desc_read(vdev, &desc, desc_cache, i, false); | |
1124 | } | |
1125 | ||
1126 | do { | |
1127 | /* If we've got too many, that implies a descriptor loop. */ | |
1128 | if (++num_bufs > max) { | |
1129 | virtio_error(vdev, "Looped descriptor"); | |
1130 | goto err; | |
1131 | } | |
1132 | ||
1133 | if (desc.flags & VRING_DESC_F_WRITE) { | |
1134 | in_total += desc.len; | |
1135 | } else { | |
1136 | out_total += desc.len; | |
1137 | } | |
1138 | if (in_total >= max_in_bytes && out_total >= max_out_bytes) { | |
1139 | goto done; | |
1140 | } | |
1141 | ||
1142 | rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, | |
1143 | &i, desc_cache == | |
1144 | &indirect_desc_cache); | |
1145 | } while (rc == VIRTQUEUE_READ_DESC_MORE); | |
1146 | ||
1147 | if (desc_cache == &indirect_desc_cache) { | |
1148 | address_space_cache_destroy(&indirect_desc_cache); | |
1149 | total_bufs++; | |
1150 | idx++; | |
1151 | } else { | |
1152 | idx += num_bufs - total_bufs; | |
1153 | total_bufs = num_bufs; | |
1154 | } | |
1155 | ||
1156 | if (idx >= vq->vring.num) { | |
1157 | idx -= vq->vring.num; | |
1158 | wrap_counter ^= 1; | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | /* Record the index and wrap counter for a kick we want */ | |
1163 | vq->shadow_avail_idx = idx; | |
1164 | vq->shadow_avail_wrap_counter = wrap_counter; | |
1165 | done: | |
1166 | address_space_cache_destroy(&indirect_desc_cache); | |
1167 | if (in_bytes) { | |
1168 | *in_bytes = in_total; | |
1169 | } | |
1170 | if (out_bytes) { | |
1171 | *out_bytes = out_total; | |
1172 | } | |
86044b24 JW |
1173 | return; |
1174 | ||
1175 | err: | |
1176 | in_total = out_total = 0; | |
1177 | goto done; | |
1178 | } | |
1179 | ||
1180 | void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, | |
1181 | unsigned int *out_bytes, | |
1182 | unsigned max_in_bytes, unsigned max_out_bytes) | |
1183 | { | |
1184 | uint16_t desc_size; | |
1185 | VRingMemoryRegionCaches *caches; | |
1186 | ||
1187 | if (unlikely(!vq->vring.desc)) { | |
1188 | goto err; | |
1189 | } | |
1190 | ||
1191 | caches = vring_get_region_caches(vq); | |
1192 | desc_size = virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ? | |
1193 | sizeof(VRingPackedDesc) : sizeof(VRingDesc); | |
1194 | if (caches->desc.len < vq->vring.num * desc_size) { | |
1195 | virtio_error(vq->vdev, "Cannot map descriptor ring"); | |
1196 | goto err; | |
1197 | } | |
1198 | ||
1199 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { | |
1200 | virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes, | |
1201 | max_in_bytes, max_out_bytes); | |
1202 | } else { | |
1203 | virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes, | |
1204 | max_in_bytes, max_out_bytes); | |
1205 | } | |
1206 | ||
1207 | return; | |
1208 | err: | |
1209 | if (in_bytes) { | |
1210 | *in_bytes = 0; | |
0d8d7690 AS |
1211 | } |
1212 | if (out_bytes) { | |
86044b24 | 1213 | *out_bytes = 0; |
0d8d7690 AS |
1214 | } |
1215 | } | |
967f97fa | 1216 | |
0d8d7690 AS |
1217 | int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, |
1218 | unsigned int out_bytes) | |
1219 | { | |
1220 | unsigned int in_total, out_total; | |
1221 | ||
e1f7b481 MT |
1222 | virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes); |
1223 | return in_bytes <= in_total && out_bytes <= out_total; | |
967f97fa AL |
1224 | } |
1225 | ||
ec55da19 SH |
1226 | static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg, |
1227 | hwaddr *addr, struct iovec *iov, | |
3b3b0628 PB |
1228 | unsigned int max_num_sg, bool is_write, |
1229 | hwaddr pa, size_t sz) | |
1230 | { | |
ec55da19 | 1231 | bool ok = false; |
3b3b0628 PB |
1232 | unsigned num_sg = *p_num_sg; |
1233 | assert(num_sg <= max_num_sg); | |
1234 | ||
1e7aed70 | 1235 | if (!sz) { |
ec55da19 SH |
1236 | virtio_error(vdev, "virtio: zero sized buffers are not allowed"); |
1237 | goto out; | |
1e7aed70 PP |
1238 | } |
1239 | ||
3b3b0628 PB |
1240 | while (sz) { |
1241 | hwaddr len = sz; | |
1242 | ||
1243 | if (num_sg == max_num_sg) { | |
ec55da19 SH |
1244 | virtio_error(vdev, "virtio: too many write descriptors in " |
1245 | "indirect table"); | |
1246 | goto out; | |
3b3b0628 PB |
1247 | } |
1248 | ||
8607f5c3 JW |
1249 | iov[num_sg].iov_base = dma_memory_map(vdev->dma_as, pa, &len, |
1250 | is_write ? | |
1251 | DMA_DIRECTION_FROM_DEVICE : | |
1252 | DMA_DIRECTION_TO_DEVICE); | |
973e7170 | 1253 | if (!iov[num_sg].iov_base) { |
ec55da19 SH |
1254 | virtio_error(vdev, "virtio: bogus descriptor or out of resources"); |
1255 | goto out; | |
973e7170 PP |
1256 | } |
1257 | ||
3b3b0628 PB |
1258 | iov[num_sg].iov_len = len; |
1259 | addr[num_sg] = pa; | |
1260 | ||
1261 | sz -= len; | |
1262 | pa += len; | |
1263 | num_sg++; | |
1264 | } | |
ec55da19 SH |
1265 | ok = true; |
1266 | ||
1267 | out: | |
3b3b0628 | 1268 | *p_num_sg = num_sg; |
ec55da19 SH |
1269 | return ok; |
1270 | } | |
1271 | ||
1272 | /* Only used by error code paths before we have a VirtQueueElement (therefore | |
1273 | * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to | |
1274 | * yet. | |
1275 | */ | |
1276 | static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num, | |
1277 | struct iovec *iov) | |
1278 | { | |
1279 | unsigned int i; | |
1280 | ||
1281 | for (i = 0; i < out_num + in_num; i++) { | |
1282 | int is_write = i >= out_num; | |
1283 | ||
1284 | cpu_physical_memory_unmap(iov->iov_base, iov->iov_len, is_write, 0); | |
1285 | iov++; | |
1286 | } | |
3b3b0628 PB |
1287 | } |
1288 | ||
8607f5c3 | 1289 | static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg, |
e4fbf5b2 | 1290 | hwaddr *addr, unsigned int num_sg, |
6bdc21c0 | 1291 | int is_write) |
42fb2e07 KW |
1292 | { |
1293 | unsigned int i; | |
a8170e5e | 1294 | hwaddr len; |
42fb2e07 | 1295 | |
e4fbf5b2 | 1296 | for (i = 0; i < num_sg; i++) { |
42fb2e07 | 1297 | len = sg[i].iov_len; |
8607f5c3 JW |
1298 | sg[i].iov_base = dma_memory_map(vdev->dma_as, |
1299 | addr[i], &len, is_write ? | |
1300 | DMA_DIRECTION_FROM_DEVICE : | |
1301 | DMA_DIRECTION_TO_DEVICE); | |
8059feee | 1302 | if (!sg[i].iov_base) { |
1a285899 | 1303 | error_report("virtio: error trying to map MMIO memory"); |
42fb2e07 KW |
1304 | exit(1); |
1305 | } | |
3b3b0628 PB |
1306 | if (len != sg[i].iov_len) { |
1307 | error_report("virtio: unexpected memory split"); | |
8059feee MT |
1308 | exit(1); |
1309 | } | |
42fb2e07 KW |
1310 | } |
1311 | } | |
1312 | ||
8607f5c3 | 1313 | void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem) |
8059feee | 1314 | { |
e4fbf5b2 DZ |
1315 | virtqueue_map_iovec(vdev, elem->in_sg, elem->in_addr, elem->in_num, 1); |
1316 | virtqueue_map_iovec(vdev, elem->out_sg, elem->out_addr, elem->out_num, 0); | |
3724650d PB |
1317 | } |
1318 | ||
bf91bd27 | 1319 | static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num) |
3724650d PB |
1320 | { |
1321 | VirtQueueElement *elem; | |
1322 | size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0])); | |
1323 | size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]); | |
1324 | size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]); | |
1325 | size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0])); | |
1326 | size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]); | |
1327 | size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]); | |
1328 | ||
1329 | assert(sz >= sizeof(VirtQueueElement)); | |
1330 | elem = g_malloc(out_sg_end); | |
b0ac429f | 1331 | trace_virtqueue_alloc_element(elem, sz, in_num, out_num); |
3724650d PB |
1332 | elem->out_num = out_num; |
1333 | elem->in_num = in_num; | |
1334 | elem->in_addr = (void *)elem + in_addr_ofs; | |
1335 | elem->out_addr = (void *)elem + out_addr_ofs; | |
1336 | elem->in_sg = (void *)elem + in_sg_ofs; | |
1337 | elem->out_sg = (void *)elem + out_sg_ofs; | |
1338 | return elem; | |
8059feee MT |
1339 | } |
1340 | ||
86044b24 | 1341 | static void *virtqueue_split_pop(VirtQueue *vq, size_t sz) |
967f97fa | 1342 | { |
5774cf98 | 1343 | unsigned int i, head, max; |
991976f7 | 1344 | VRingMemoryRegionCaches *caches; |
5eba0404 PB |
1345 | MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID; |
1346 | MemoryRegionCache *desc_cache; | |
1347 | int64_t len; | |
cee3ca00 | 1348 | VirtIODevice *vdev = vq->vdev; |
9796d0ac | 1349 | VirtQueueElement *elem = NULL; |
37ef70be | 1350 | unsigned out_num, in_num, elem_entries; |
3b3b0628 PB |
1351 | hwaddr addr[VIRTQUEUE_MAX_SIZE]; |
1352 | struct iovec iov[VIRTQUEUE_MAX_SIZE]; | |
aa570d6f | 1353 | VRingDesc desc; |
412e0e81 | 1354 | int rc; |
967f97fa | 1355 | |
b5f53d04 | 1356 | RCU_READ_LOCK_GUARD(); |
97cd965c PB |
1357 | if (virtio_queue_empty_rcu(vq)) { |
1358 | goto done; | |
51b19ebe | 1359 | } |
be1fea9b VM |
1360 | /* Needed after virtio_queue_empty(), see comment in |
1361 | * virtqueue_num_heads(). */ | |
1362 | smp_rmb(); | |
967f97fa AL |
1363 | |
1364 | /* When we start there are none of either input nor output. */ | |
37ef70be | 1365 | out_num = in_num = elem_entries = 0; |
967f97fa | 1366 | |
5774cf98 MM |
1367 | max = vq->vring.num; |
1368 | ||
afd9096e | 1369 | if (vq->inuse >= vq->vring.num) { |
ec55da19 | 1370 | virtio_error(vdev, "Virtqueue size exceeded"); |
97cd965c | 1371 | goto done; |
afd9096e SH |
1372 | } |
1373 | ||
fb1131b6 | 1374 | if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) { |
97cd965c | 1375 | goto done; |
fb1131b6 SH |
1376 | } |
1377 | ||
95129d6f | 1378 | if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
e9600c6c | 1379 | vring_set_avail_event(vq, vq->last_avail_idx); |
bcbabae8 | 1380 | } |
efeea6d0 | 1381 | |
fb1131b6 | 1382 | i = head; |
9796d0ac | 1383 | |
e0e2d644 | 1384 | caches = vring_get_region_caches(vq); |
991976f7 | 1385 | if (caches->desc.len < max * sizeof(VRingDesc)) { |
9796d0ac PB |
1386 | virtio_error(vdev, "Cannot map descriptor ring"); |
1387 | goto done; | |
1388 | } | |
1389 | ||
991976f7 | 1390 | desc_cache = &caches->desc; |
86044b24 | 1391 | vring_split_desc_read(vdev, &desc, desc_cache, i); |
aa570d6f | 1392 | if (desc.flags & VRING_DESC_F_INDIRECT) { |
74231929 | 1393 | if (!desc.len || (desc.len % sizeof(VRingDesc))) { |
ec55da19 | 1394 | virtio_error(vdev, "Invalid size for indirect buffer table"); |
9796d0ac | 1395 | goto done; |
efeea6d0 MM |
1396 | } |
1397 | ||
1398 | /* loop over the indirect descriptor table */ | |
5eba0404 PB |
1399 | len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as, |
1400 | desc.addr, desc.len, false); | |
1401 | desc_cache = &indirect_desc_cache; | |
9796d0ac PB |
1402 | if (len < desc.len) { |
1403 | virtio_error(vdev, "Cannot map indirect buffer"); | |
1404 | goto done; | |
1405 | } | |
1406 | ||
aa570d6f | 1407 | max = desc.len / sizeof(VRingDesc); |
efeea6d0 | 1408 | i = 0; |
86044b24 | 1409 | vring_split_desc_read(vdev, &desc, desc_cache, i); |
efeea6d0 MM |
1410 | } |
1411 | ||
42fb2e07 | 1412 | /* Collect all the descriptors */ |
967f97fa | 1413 | do { |
ec55da19 SH |
1414 | bool map_ok; |
1415 | ||
aa570d6f | 1416 | if (desc.flags & VRING_DESC_F_WRITE) { |
ec55da19 SH |
1417 | map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num, |
1418 | iov + out_num, | |
1419 | VIRTQUEUE_MAX_SIZE - out_num, true, | |
1420 | desc.addr, desc.len); | |
42fb2e07 | 1421 | } else { |
3b3b0628 | 1422 | if (in_num) { |
ec55da19 SH |
1423 | virtio_error(vdev, "Incorrect order for descriptors"); |
1424 | goto err_undo_map; | |
c8eac1cf | 1425 | } |
ec55da19 SH |
1426 | map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov, |
1427 | VIRTQUEUE_MAX_SIZE, false, | |
1428 | desc.addr, desc.len); | |
1429 | } | |
1430 | if (!map_ok) { | |
1431 | goto err_undo_map; | |
42fb2e07 | 1432 | } |
967f97fa | 1433 | |
967f97fa | 1434 | /* If we've got too many, that implies a descriptor loop. */ |
37ef70be | 1435 | if (++elem_entries > max) { |
ec55da19 SH |
1436 | virtio_error(vdev, "Looped descriptor"); |
1437 | goto err_undo_map; | |
bb6834cf | 1438 | } |
412e0e81 | 1439 | |
86044b24 | 1440 | rc = virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max, &i); |
412e0e81 SH |
1441 | } while (rc == VIRTQUEUE_READ_DESC_MORE); |
1442 | ||
1443 | if (rc == VIRTQUEUE_READ_DESC_ERROR) { | |
1444 | goto err_undo_map; | |
1445 | } | |
967f97fa | 1446 | |
3b3b0628 PB |
1447 | /* Now copy what we have collected and mapped */ |
1448 | elem = virtqueue_alloc_element(sz, out_num, in_num); | |
967f97fa | 1449 | elem->index = head; |
86044b24 | 1450 | elem->ndescs = 1; |
3b3b0628 PB |
1451 | for (i = 0; i < out_num; i++) { |
1452 | elem->out_addr[i] = addr[i]; | |
1453 | elem->out_sg[i] = iov[i]; | |
1454 | } | |
1455 | for (i = 0; i < in_num; i++) { | |
1456 | elem->in_addr[i] = addr[out_num + i]; | |
1457 | elem->in_sg[i] = iov[out_num + i]; | |
1458 | } | |
967f97fa AL |
1459 | |
1460 | vq->inuse++; | |
1461 | ||
64979a4d | 1462 | trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); |
9796d0ac | 1463 | done: |
5eba0404 | 1464 | address_space_cache_destroy(&indirect_desc_cache); |
9796d0ac | 1465 | |
51b19ebe | 1466 | return elem; |
ec55da19 SH |
1467 | |
1468 | err_undo_map: | |
1469 | virtqueue_undo_map_desc(out_num, in_num, iov); | |
9796d0ac | 1470 | goto done; |
967f97fa AL |
1471 | } |
1472 | ||
86044b24 JW |
1473 | static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz) |
1474 | { | |
1475 | unsigned int i, max; | |
1476 | VRingMemoryRegionCaches *caches; | |
1477 | MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID; | |
1478 | MemoryRegionCache *desc_cache; | |
1479 | int64_t len; | |
1480 | VirtIODevice *vdev = vq->vdev; | |
1481 | VirtQueueElement *elem = NULL; | |
1482 | unsigned out_num, in_num, elem_entries; | |
1483 | hwaddr addr[VIRTQUEUE_MAX_SIZE]; | |
1484 | struct iovec iov[VIRTQUEUE_MAX_SIZE]; | |
1485 | VRingPackedDesc desc; | |
1486 | uint16_t id; | |
1487 | int rc; | |
1488 | ||
b5f53d04 | 1489 | RCU_READ_LOCK_GUARD(); |
86044b24 JW |
1490 | if (virtio_queue_packed_empty_rcu(vq)) { |
1491 | goto done; | |
1492 | } | |
1493 | ||
1494 | /* When we start there are none of either input nor output. */ | |
1495 | out_num = in_num = elem_entries = 0; | |
1496 | ||
1497 | max = vq->vring.num; | |
1498 | ||
1499 | if (vq->inuse >= vq->vring.num) { | |
1500 | virtio_error(vdev, "Virtqueue size exceeded"); | |
1501 | goto done; | |
1502 | } | |
1503 | ||
1504 | i = vq->last_avail_idx; | |
1505 | ||
1506 | caches = vring_get_region_caches(vq); | |
1507 | if (caches->desc.len < max * sizeof(VRingDesc)) { | |
1508 | virtio_error(vdev, "Cannot map descriptor ring"); | |
1509 | goto done; | |
1510 | } | |
1511 | ||
1512 | desc_cache = &caches->desc; | |
1513 | vring_packed_desc_read(vdev, &desc, desc_cache, i, true); | |
1514 | id = desc.id; | |
1515 | if (desc.flags & VRING_DESC_F_INDIRECT) { | |
1516 | if (desc.len % sizeof(VRingPackedDesc)) { | |
1517 | virtio_error(vdev, "Invalid size for indirect buffer table"); | |
1518 | goto done; | |
1519 | } | |
1520 | ||
1521 | /* loop over the indirect descriptor table */ | |
1522 | len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as, | |
1523 | desc.addr, desc.len, false); | |
1524 | desc_cache = &indirect_desc_cache; | |
1525 | if (len < desc.len) { | |
1526 | virtio_error(vdev, "Cannot map indirect buffer"); | |
1527 | goto done; | |
1528 | } | |
1529 | ||
1530 | max = desc.len / sizeof(VRingPackedDesc); | |
1531 | i = 0; | |
1532 | vring_packed_desc_read(vdev, &desc, desc_cache, i, false); | |
1533 | } | |
1534 | ||
1535 | /* Collect all the descriptors */ | |
1536 | do { | |
1537 | bool map_ok; | |
1538 | ||
1539 | if (desc.flags & VRING_DESC_F_WRITE) { | |
1540 | map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num, | |
1541 | iov + out_num, | |
1542 | VIRTQUEUE_MAX_SIZE - out_num, true, | |
1543 | desc.addr, desc.len); | |
1544 | } else { | |
1545 | if (in_num) { | |
1546 | virtio_error(vdev, "Incorrect order for descriptors"); | |
1547 | goto err_undo_map; | |
1548 | } | |
1549 | map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov, | |
1550 | VIRTQUEUE_MAX_SIZE, false, | |
1551 | desc.addr, desc.len); | |
1552 | } | |
1553 | if (!map_ok) { | |
1554 | goto err_undo_map; | |
1555 | } | |
1556 | ||
1557 | /* If we've got too many, that implies a descriptor loop. */ | |
1558 | if (++elem_entries > max) { | |
1559 | virtio_error(vdev, "Looped descriptor"); | |
1560 | goto err_undo_map; | |
1561 | } | |
1562 | ||
1563 | rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, &i, | |
1564 | desc_cache == | |
1565 | &indirect_desc_cache); | |
1566 | } while (rc == VIRTQUEUE_READ_DESC_MORE); | |
1567 | ||
1568 | /* Now copy what we have collected and mapped */ | |
1569 | elem = virtqueue_alloc_element(sz, out_num, in_num); | |
1570 | for (i = 0; i < out_num; i++) { | |
1571 | elem->out_addr[i] = addr[i]; | |
1572 | elem->out_sg[i] = iov[i]; | |
1573 | } | |
1574 | for (i = 0; i < in_num; i++) { | |
1575 | elem->in_addr[i] = addr[out_num + i]; | |
1576 | elem->in_sg[i] = iov[out_num + i]; | |
1577 | } | |
1578 | ||
1579 | elem->index = id; | |
1580 | elem->ndescs = (desc_cache == &indirect_desc_cache) ? 1 : elem_entries; | |
1581 | vq->last_avail_idx += elem->ndescs; | |
1582 | vq->inuse += elem->ndescs; | |
1583 | ||
1584 | if (vq->last_avail_idx >= vq->vring.num) { | |
1585 | vq->last_avail_idx -= vq->vring.num; | |
1586 | vq->last_avail_wrap_counter ^= 1; | |
1587 | } | |
1588 | ||
1589 | vq->shadow_avail_idx = vq->last_avail_idx; | |
1590 | vq->shadow_avail_wrap_counter = vq->last_avail_wrap_counter; | |
1591 | ||
1592 | trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); | |
1593 | done: | |
1594 | address_space_cache_destroy(&indirect_desc_cache); | |
86044b24 JW |
1595 | |
1596 | return elem; | |
1597 | ||
1598 | err_undo_map: | |
1599 | virtqueue_undo_map_desc(out_num, in_num, iov); | |
1600 | goto done; | |
1601 | } | |
1602 | ||
1603 | void *virtqueue_pop(VirtQueue *vq, size_t sz) | |
1604 | { | |
1605 | if (unlikely(vq->vdev->broken)) { | |
1606 | return NULL; | |
1607 | } | |
1608 | ||
1609 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { | |
1610 | return virtqueue_packed_pop(vq, sz); | |
1611 | } else { | |
1612 | return virtqueue_split_pop(vq, sz); | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | static unsigned int virtqueue_packed_drop_all(VirtQueue *vq) | |
54e17709 | 1617 | { |
86044b24 JW |
1618 | VRingMemoryRegionCaches *caches; |
1619 | MemoryRegionCache *desc_cache; | |
54e17709 YB |
1620 | unsigned int dropped = 0; |
1621 | VirtQueueElement elem = {}; | |
1622 | VirtIODevice *vdev = vq->vdev; | |
86044b24 | 1623 | VRingPackedDesc desc; |
54e17709 | 1624 | |
86044b24 JW |
1625 | caches = vring_get_region_caches(vq); |
1626 | desc_cache = &caches->desc; | |
1627 | ||
1628 | virtio_queue_set_notification(vq, 0); | |
1629 | ||
1630 | while (vq->inuse < vq->vring.num) { | |
1631 | unsigned int idx = vq->last_avail_idx; | |
1632 | /* | |
1633 | * works similar to virtqueue_pop but does not map buffers | |
1634 | * and does not allocate any memory. | |
1635 | */ | |
1636 | vring_packed_desc_read(vdev, &desc, desc_cache, | |
1637 | vq->last_avail_idx , true); | |
1638 | if (!is_desc_avail(desc.flags, vq->last_avail_wrap_counter)) { | |
1639 | break; | |
1640 | } | |
1641 | elem.index = desc.id; | |
1642 | elem.ndescs = 1; | |
1643 | while (virtqueue_packed_read_next_desc(vq, &desc, desc_cache, | |
1644 | vq->vring.num, &idx, false)) { | |
1645 | ++elem.ndescs; | |
1646 | } | |
1647 | /* | |
1648 | * immediately push the element, nothing to unmap | |
1649 | * as both in_num and out_num are set to 0. | |
1650 | */ | |
1651 | virtqueue_push(vq, &elem, 0); | |
1652 | dropped++; | |
1653 | vq->last_avail_idx += elem.ndescs; | |
1654 | if (vq->last_avail_idx >= vq->vring.num) { | |
1655 | vq->last_avail_idx -= vq->vring.num; | |
1656 | vq->last_avail_wrap_counter ^= 1; | |
1657 | } | |
54e17709 YB |
1658 | } |
1659 | ||
86044b24 JW |
1660 | return dropped; |
1661 | } | |
1662 | ||
1663 | static unsigned int virtqueue_split_drop_all(VirtQueue *vq) | |
1664 | { | |
1665 | unsigned int dropped = 0; | |
1666 | VirtQueueElement elem = {}; | |
1667 | VirtIODevice *vdev = vq->vdev; | |
1668 | bool fEventIdx = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); | |
1669 | ||
54e17709 YB |
1670 | while (!virtio_queue_empty(vq) && vq->inuse < vq->vring.num) { |
1671 | /* works similar to virtqueue_pop but does not map buffers | |
1672 | * and does not allocate any memory */ | |
1673 | smp_rmb(); | |
1674 | if (!virtqueue_get_head(vq, vq->last_avail_idx, &elem.index)) { | |
1675 | break; | |
1676 | } | |
1677 | vq->inuse++; | |
1678 | vq->last_avail_idx++; | |
1679 | if (fEventIdx) { | |
1680 | vring_set_avail_event(vq, vq->last_avail_idx); | |
1681 | } | |
1682 | /* immediately push the element, nothing to unmap | |
1683 | * as both in_num and out_num are set to 0 */ | |
1684 | virtqueue_push(vq, &elem, 0); | |
1685 | dropped++; | |
1686 | } | |
1687 | ||
1688 | return dropped; | |
1689 | } | |
1690 | ||
86044b24 JW |
1691 | /* virtqueue_drop_all: |
1692 | * @vq: The #VirtQueue | |
1693 | * Drops all queued buffers and indicates them to the guest | |
1694 | * as if they are done. Useful when buffers can not be | |
1695 | * processed but must be returned to the guest. | |
1696 | */ | |
1697 | unsigned int virtqueue_drop_all(VirtQueue *vq) | |
1698 | { | |
1699 | struct VirtIODevice *vdev = vq->vdev; | |
1700 | ||
1701 | if (unlikely(vdev->broken)) { | |
1702 | return 0; | |
1703 | } | |
1704 | ||
1705 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { | |
1706 | return virtqueue_packed_drop_all(vq); | |
1707 | } else { | |
1708 | return virtqueue_split_drop_all(vq); | |
1709 | } | |
1710 | } | |
1711 | ||
3724650d PB |
1712 | /* Reading and writing a structure directly to QEMUFile is *awful*, but |
1713 | * it is what QEMU has always done by mistake. We can change it sooner | |
1714 | * or later by bumping the version number of the affected vm states. | |
1715 | * In the meanwhile, since the in-memory layout of VirtQueueElement | |
1716 | * has changed, we need to marshal to and from the layout that was | |
1717 | * used before the change. | |
1718 | */ | |
1719 | typedef struct VirtQueueElementOld { | |
1720 | unsigned int index; | |
1721 | unsigned int out_num; | |
1722 | unsigned int in_num; | |
1723 | hwaddr in_addr[VIRTQUEUE_MAX_SIZE]; | |
1724 | hwaddr out_addr[VIRTQUEUE_MAX_SIZE]; | |
1725 | struct iovec in_sg[VIRTQUEUE_MAX_SIZE]; | |
1726 | struct iovec out_sg[VIRTQUEUE_MAX_SIZE]; | |
1727 | } VirtQueueElementOld; | |
1728 | ||
8607f5c3 | 1729 | void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz) |
ab281c17 | 1730 | { |
3724650d PB |
1731 | VirtQueueElement *elem; |
1732 | VirtQueueElementOld data; | |
1733 | int i; | |
1734 | ||
1735 | qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld)); | |
1736 | ||
6bdc21c0 MT |
1737 | /* TODO: teach all callers that this can fail, and return failure instead |
1738 | * of asserting here. | |
262a69f4 EB |
1739 | * This is just one thing (there are probably more) that must be |
1740 | * fixed before we can allow NDEBUG compilation. | |
6bdc21c0 | 1741 | */ |
6bdc21c0 MT |
1742 | assert(ARRAY_SIZE(data.in_addr) >= data.in_num); |
1743 | assert(ARRAY_SIZE(data.out_addr) >= data.out_num); | |
1744 | ||
3724650d PB |
1745 | elem = virtqueue_alloc_element(sz, data.out_num, data.in_num); |
1746 | elem->index = data.index; | |
1747 | ||
1748 | for (i = 0; i < elem->in_num; i++) { | |
1749 | elem->in_addr[i] = data.in_addr[i]; | |
1750 | } | |
1751 | ||
1752 | for (i = 0; i < elem->out_num; i++) { | |
1753 | elem->out_addr[i] = data.out_addr[i]; | |
1754 | } | |
1755 | ||
1756 | for (i = 0; i < elem->in_num; i++) { | |
1757 | /* Base is overwritten by virtqueue_map. */ | |
1758 | elem->in_sg[i].iov_base = 0; | |
1759 | elem->in_sg[i].iov_len = data.in_sg[i].iov_len; | |
1760 | } | |
1761 | ||
1762 | for (i = 0; i < elem->out_num; i++) { | |
1763 | /* Base is overwritten by virtqueue_map. */ | |
1764 | elem->out_sg[i].iov_base = 0; | |
1765 | elem->out_sg[i].iov_len = data.out_sg[i].iov_len; | |
1766 | } | |
1767 | ||
86044b24 JW |
1768 | if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) { |
1769 | qemu_get_be32s(f, &elem->ndescs); | |
1770 | } | |
1771 | ||
8607f5c3 | 1772 | virtqueue_map(vdev, elem); |
ab281c17 PB |
1773 | return elem; |
1774 | } | |
1775 | ||
86044b24 JW |
1776 | void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, |
1777 | VirtQueueElement *elem) | |
ab281c17 | 1778 | { |
3724650d PB |
1779 | VirtQueueElementOld data; |
1780 | int i; | |
1781 | ||
1782 | memset(&data, 0, sizeof(data)); | |
1783 | data.index = elem->index; | |
1784 | data.in_num = elem->in_num; | |
1785 | data.out_num = elem->out_num; | |
1786 | ||
1787 | for (i = 0; i < elem->in_num; i++) { | |
1788 | data.in_addr[i] = elem->in_addr[i]; | |
1789 | } | |
1790 | ||
1791 | for (i = 0; i < elem->out_num; i++) { | |
1792 | data.out_addr[i] = elem->out_addr[i]; | |
1793 | } | |
1794 | ||
1795 | for (i = 0; i < elem->in_num; i++) { | |
1796 | /* Base is overwritten by virtqueue_map when loading. Do not | |
1797 | * save it, as it would leak the QEMU address space layout. */ | |
1798 | data.in_sg[i].iov_len = elem->in_sg[i].iov_len; | |
1799 | } | |
1800 | ||
1801 | for (i = 0; i < elem->out_num; i++) { | |
1802 | /* Do not save iov_base as above. */ | |
1803 | data.out_sg[i].iov_len = elem->out_sg[i].iov_len; | |
1804 | } | |
86044b24 JW |
1805 | |
1806 | if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) { | |
1807 | qemu_put_be32s(f, &elem->ndescs); | |
1808 | } | |
1809 | ||
3724650d | 1810 | qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld)); |
ab281c17 PB |
1811 | } |
1812 | ||
967f97fa | 1813 | /* virtio device */ |
7055e687 MT |
1814 | static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector) |
1815 | { | |
1c819449 FK |
1816 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1817 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1818 | ||
f5ed3663 SH |
1819 | if (unlikely(vdev->broken)) { |
1820 | return; | |
1821 | } | |
1822 | ||
1c819449 FK |
1823 | if (k->notify) { |
1824 | k->notify(qbus->parent, vector); | |
7055e687 MT |
1825 | } |
1826 | } | |
967f97fa | 1827 | |
53c25cea | 1828 | void virtio_update_irq(VirtIODevice *vdev) |
967f97fa | 1829 | { |
7055e687 | 1830 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
967f97fa AL |
1831 | } |
1832 | ||
0b352fd6 CH |
1833 | static int virtio_validate_features(VirtIODevice *vdev) |
1834 | { | |
1835 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
1836 | ||
8607f5c3 JW |
1837 | if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM) && |
1838 | !virtio_vdev_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) { | |
1839 | return -EFAULT; | |
1840 | } | |
1841 | ||
0b352fd6 CH |
1842 | if (k->validate_features) { |
1843 | return k->validate_features(vdev); | |
1844 | } else { | |
1845 | return 0; | |
1846 | } | |
1847 | } | |
1848 | ||
1849 | int virtio_set_status(VirtIODevice *vdev, uint8_t val) | |
4e1837f8 | 1850 | { |
181103cd | 1851 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
4e1837f8 SH |
1852 | trace_virtio_set_status(vdev, val); |
1853 | ||
95129d6f | 1854 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
0b352fd6 CH |
1855 | if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) && |
1856 | val & VIRTIO_CONFIG_S_FEATURES_OK) { | |
1857 | int ret = virtio_validate_features(vdev); | |
1858 | ||
1859 | if (ret) { | |
1860 | return ret; | |
1861 | } | |
1862 | } | |
1863 | } | |
e57f2c31 | 1864 | |
4c5cf37b XY |
1865 | if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) != |
1866 | (val & VIRTIO_CONFIG_S_DRIVER_OK)) { | |
1867 | virtio_set_started(vdev, val & VIRTIO_CONFIG_S_DRIVER_OK); | |
1868 | } | |
badaf79c | 1869 | |
181103cd FK |
1870 | if (k->set_status) { |
1871 | k->set_status(vdev, val); | |
4e1837f8 SH |
1872 | } |
1873 | vdev->status = val; | |
badaf79c | 1874 | |
0b352fd6 | 1875 | return 0; |
4e1837f8 SH |
1876 | } |
1877 | ||
616a6552 GK |
1878 | static enum virtio_device_endian virtio_default_endian(void) |
1879 | { | |
1880 | if (target_words_bigendian()) { | |
1881 | return VIRTIO_DEVICE_ENDIAN_BIG; | |
1882 | } else { | |
1883 | return VIRTIO_DEVICE_ENDIAN_LITTLE; | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | static enum virtio_device_endian virtio_current_cpu_endian(void) | |
1888 | { | |
1889 | CPUClass *cc = CPU_GET_CLASS(current_cpu); | |
1890 | ||
1891 | if (cc->virtio_is_big_endian(current_cpu)) { | |
1892 | return VIRTIO_DEVICE_ENDIAN_BIG; | |
1893 | } else { | |
1894 | return VIRTIO_DEVICE_ENDIAN_LITTLE; | |
1895 | } | |
1896 | } | |
1897 | ||
53c25cea | 1898 | void virtio_reset(void *opaque) |
967f97fa AL |
1899 | { |
1900 | VirtIODevice *vdev = opaque; | |
181103cd | 1901 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1902 | int i; |
1903 | ||
e0c472d8 | 1904 | virtio_set_status(vdev, 0); |
616a6552 GK |
1905 | if (current_cpu) { |
1906 | /* Guest initiated reset */ | |
1907 | vdev->device_endian = virtio_current_cpu_endian(); | |
1908 | } else { | |
1909 | /* System reset */ | |
1910 | vdev->device_endian = virtio_default_endian(); | |
1911 | } | |
e0c472d8 | 1912 | |
181103cd FK |
1913 | if (k->reset) { |
1914 | k->reset(vdev); | |
1915 | } | |
967f97fa | 1916 | |
868a8f44 | 1917 | vdev->start_on_kick = false; |
badaf79c | 1918 | vdev->started = false; |
f5ed3663 | 1919 | vdev->broken = false; |
704a76fc | 1920 | vdev->guest_features = 0; |
967f97fa AL |
1921 | vdev->queue_sel = 0; |
1922 | vdev->status = 0; | |
0687c37c | 1923 | atomic_set(&vdev->isr, 0); |
7055e687 MT |
1924 | vdev->config_vector = VIRTIO_NO_VECTOR; |
1925 | virtio_notify_vector(vdev, vdev->config_vector); | |
967f97fa | 1926 | |
87b3bd1c | 1927 | for(i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
1928 | vdev->vq[i].vring.desc = 0; |
1929 | vdev->vq[i].vring.avail = 0; | |
1930 | vdev->vq[i].vring.used = 0; | |
1931 | vdev->vq[i].last_avail_idx = 0; | |
be1fea9b | 1932 | vdev->vq[i].shadow_avail_idx = 0; |
b796fcd1 | 1933 | vdev->vq[i].used_idx = 0; |
86044b24 JW |
1934 | vdev->vq[i].last_avail_wrap_counter = true; |
1935 | vdev->vq[i].shadow_avail_wrap_counter = true; | |
1936 | vdev->vq[i].used_wrap_counter = true; | |
e0d686bf | 1937 | virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR); |
bcbabae8 MT |
1938 | vdev->vq[i].signalled_used = 0; |
1939 | vdev->vq[i].signalled_used_valid = false; | |
332fa82d | 1940 | vdev->vq[i].notification = true; |
46c5d082 | 1941 | vdev->vq[i].vring.num = vdev->vq[i].vring.num_default; |
4b7f91ed | 1942 | vdev->vq[i].inuse = 0; |
e0e2d644 | 1943 | virtio_virtqueue_reset_region_cache(&vdev->vq[i]); |
967f97fa AL |
1944 | } |
1945 | } | |
1946 | ||
53c25cea | 1947 | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 1948 | { |
181103cd | 1949 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1950 | uint8_t val; |
1951 | ||
5f5a1318 | 1952 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 1953 | return (uint32_t)-1; |
5f5a1318 JW |
1954 | } |
1955 | ||
1956 | k->get_config(vdev, vdev->config); | |
967f97fa | 1957 | |
06dbfc6f | 1958 | val = ldub_p(vdev->config + addr); |
967f97fa AL |
1959 | return val; |
1960 | } | |
1961 | ||
53c25cea | 1962 | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 1963 | { |
181103cd | 1964 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1965 | uint16_t val; |
1966 | ||
5f5a1318 | 1967 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 1968 | return (uint32_t)-1; |
5f5a1318 JW |
1969 | } |
1970 | ||
1971 | k->get_config(vdev, vdev->config); | |
967f97fa | 1972 | |
06dbfc6f | 1973 | val = lduw_p(vdev->config + addr); |
967f97fa AL |
1974 | return val; |
1975 | } | |
1976 | ||
53c25cea | 1977 | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 1978 | { |
181103cd | 1979 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1980 | uint32_t val; |
1981 | ||
5f5a1318 | 1982 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 1983 | return (uint32_t)-1; |
5f5a1318 JW |
1984 | } |
1985 | ||
1986 | k->get_config(vdev, vdev->config); | |
967f97fa | 1987 | |
06dbfc6f | 1988 | val = ldl_p(vdev->config + addr); |
967f97fa AL |
1989 | return val; |
1990 | } | |
1991 | ||
53c25cea | 1992 | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 1993 | { |
181103cd | 1994 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
1995 | uint8_t val = data; |
1996 | ||
5f5a1318 | 1997 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 1998 | return; |
5f5a1318 | 1999 | } |
967f97fa | 2000 | |
06dbfc6f | 2001 | stb_p(vdev->config + addr, val); |
967f97fa | 2002 | |
181103cd FK |
2003 | if (k->set_config) { |
2004 | k->set_config(vdev, vdev->config); | |
2005 | } | |
967f97fa AL |
2006 | } |
2007 | ||
53c25cea | 2008 | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 2009 | { |
181103cd | 2010 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
2011 | uint16_t val = data; |
2012 | ||
5f5a1318 | 2013 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 2014 | return; |
5f5a1318 | 2015 | } |
967f97fa | 2016 | |
06dbfc6f | 2017 | stw_p(vdev->config + addr, val); |
967f97fa | 2018 | |
181103cd FK |
2019 | if (k->set_config) { |
2020 | k->set_config(vdev, vdev->config); | |
2021 | } | |
967f97fa AL |
2022 | } |
2023 | ||
53c25cea | 2024 | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 2025 | { |
181103cd | 2026 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
2027 | uint32_t val = data; |
2028 | ||
5f5a1318 | 2029 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 2030 | return; |
5f5a1318 | 2031 | } |
967f97fa | 2032 | |
06dbfc6f | 2033 | stl_p(vdev->config + addr, val); |
967f97fa | 2034 | |
181103cd FK |
2035 | if (k->set_config) { |
2036 | k->set_config(vdev, vdev->config); | |
2037 | } | |
967f97fa AL |
2038 | } |
2039 | ||
adfb743c MT |
2040 | uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr) |
2041 | { | |
2042 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
2043 | uint8_t val; | |
2044 | ||
2045 | if (addr + sizeof(val) > vdev->config_len) { | |
2046 | return (uint32_t)-1; | |
2047 | } | |
2048 | ||
2049 | k->get_config(vdev, vdev->config); | |
2050 | ||
2051 | val = ldub_p(vdev->config + addr); | |
2052 | return val; | |
2053 | } | |
2054 | ||
2055 | uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr) | |
2056 | { | |
2057 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
2058 | uint16_t val; | |
2059 | ||
2060 | if (addr + sizeof(val) > vdev->config_len) { | |
2061 | return (uint32_t)-1; | |
2062 | } | |
2063 | ||
2064 | k->get_config(vdev, vdev->config); | |
2065 | ||
2066 | val = lduw_le_p(vdev->config + addr); | |
2067 | return val; | |
2068 | } | |
2069 | ||
2070 | uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr) | |
2071 | { | |
2072 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
2073 | uint32_t val; | |
2074 | ||
2075 | if (addr + sizeof(val) > vdev->config_len) { | |
2076 | return (uint32_t)-1; | |
2077 | } | |
2078 | ||
2079 | k->get_config(vdev, vdev->config); | |
2080 | ||
2081 | val = ldl_le_p(vdev->config + addr); | |
2082 | return val; | |
2083 | } | |
2084 | ||
2085 | void virtio_config_modern_writeb(VirtIODevice *vdev, | |
2086 | uint32_t addr, uint32_t data) | |
2087 | { | |
2088 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
2089 | uint8_t val = data; | |
2090 | ||
2091 | if (addr + sizeof(val) > vdev->config_len) { | |
2092 | return; | |
2093 | } | |
2094 | ||
2095 | stb_p(vdev->config + addr, val); | |
2096 | ||
2097 | if (k->set_config) { | |
2098 | k->set_config(vdev, vdev->config); | |
2099 | } | |
2100 | } | |
2101 | ||
2102 | void virtio_config_modern_writew(VirtIODevice *vdev, | |
2103 | uint32_t addr, uint32_t data) | |
2104 | { | |
2105 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
2106 | uint16_t val = data; | |
2107 | ||
2108 | if (addr + sizeof(val) > vdev->config_len) { | |
2109 | return; | |
2110 | } | |
2111 | ||
2112 | stw_le_p(vdev->config + addr, val); | |
2113 | ||
2114 | if (k->set_config) { | |
2115 | k->set_config(vdev, vdev->config); | |
2116 | } | |
2117 | } | |
2118 | ||
2119 | void virtio_config_modern_writel(VirtIODevice *vdev, | |
2120 | uint32_t addr, uint32_t data) | |
2121 | { | |
2122 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
2123 | uint32_t val = data; | |
2124 | ||
2125 | if (addr + sizeof(val) > vdev->config_len) { | |
2126 | return; | |
2127 | } | |
2128 | ||
2129 | stl_le_p(vdev->config + addr, val); | |
2130 | ||
2131 | if (k->set_config) { | |
2132 | k->set_config(vdev, vdev->config); | |
2133 | } | |
2134 | } | |
2135 | ||
a8170e5e | 2136 | void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr) |
967f97fa | 2137 | { |
758ead31 PP |
2138 | if (!vdev->vq[n].vring.num) { |
2139 | return; | |
2140 | } | |
ab223c95 CH |
2141 | vdev->vq[n].vring.desc = addr; |
2142 | virtio_queue_update_rings(vdev, n); | |
53c25cea PB |
2143 | } |
2144 | ||
a8170e5e | 2145 | hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) |
53c25cea | 2146 | { |
ab223c95 CH |
2147 | return vdev->vq[n].vring.desc; |
2148 | } | |
2149 | ||
2150 | void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc, | |
2151 | hwaddr avail, hwaddr used) | |
2152 | { | |
758ead31 PP |
2153 | if (!vdev->vq[n].vring.num) { |
2154 | return; | |
2155 | } | |
ab223c95 CH |
2156 | vdev->vq[n].vring.desc = desc; |
2157 | vdev->vq[n].vring.avail = avail; | |
2158 | vdev->vq[n].vring.used = used; | |
c611c764 | 2159 | virtio_init_region_cache(vdev, n); |
53c25cea PB |
2160 | } |
2161 | ||
e63c0ba1 PM |
2162 | void virtio_queue_set_num(VirtIODevice *vdev, int n, int num) |
2163 | { | |
f6049f44 PM |
2164 | /* Don't allow guest to flip queue between existent and |
2165 | * nonexistent states, or to set it to an invalid size. | |
2166 | */ | |
2167 | if (!!num != !!vdev->vq[n].vring.num || | |
2168 | num > VIRTQUEUE_MAX_SIZE || | |
2169 | num < 0) { | |
2170 | return; | |
e63c0ba1 | 2171 | } |
f6049f44 | 2172 | vdev->vq[n].vring.num = num; |
e63c0ba1 PM |
2173 | } |
2174 | ||
e0d686bf JW |
2175 | VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector) |
2176 | { | |
2177 | return QLIST_FIRST(&vdev->vector_queues[vector]); | |
2178 | } | |
2179 | ||
2180 | VirtQueue *virtio_vector_next_queue(VirtQueue *vq) | |
2181 | { | |
2182 | return QLIST_NEXT(vq, node); | |
2183 | } | |
2184 | ||
53c25cea PB |
2185 | int virtio_queue_get_num(VirtIODevice *vdev, int n) |
2186 | { | |
2187 | return vdev->vq[n].vring.num; | |
2188 | } | |
967f97fa | 2189 | |
8c797e75 MT |
2190 | int virtio_queue_get_max_num(VirtIODevice *vdev, int n) |
2191 | { | |
2192 | return vdev->vq[n].vring.num_default; | |
2193 | } | |
2194 | ||
8ad176aa JW |
2195 | int virtio_get_num_queues(VirtIODevice *vdev) |
2196 | { | |
2197 | int i; | |
2198 | ||
87b3bd1c | 2199 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
8ad176aa JW |
2200 | if (!virtio_queue_get_num(vdev, i)) { |
2201 | break; | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | return i; | |
2206 | } | |
2207 | ||
6ce69d1c PM |
2208 | void virtio_queue_set_align(VirtIODevice *vdev, int n, int align) |
2209 | { | |
2210 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
2211 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
2212 | ||
ab223c95 | 2213 | /* virtio-1 compliant devices cannot change the alignment */ |
95129d6f | 2214 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
ab223c95 CH |
2215 | error_report("tried to modify queue alignment for virtio-1 device"); |
2216 | return; | |
2217 | } | |
6ce69d1c PM |
2218 | /* Check that the transport told us it was going to do this |
2219 | * (so a buggy transport will immediately assert rather than | |
2220 | * silently failing to migrate this state) | |
2221 | */ | |
2222 | assert(k->has_variable_vring_alignment); | |
2223 | ||
758ead31 PP |
2224 | if (align) { |
2225 | vdev->vq[n].vring.align = align; | |
2226 | virtio_queue_update_rings(vdev, n); | |
2227 | } | |
6ce69d1c PM |
2228 | } |
2229 | ||
07931698 | 2230 | static bool virtio_queue_notify_aio_vq(VirtQueue *vq) |
344dc16f | 2231 | { |
badaf79c XY |
2232 | bool ret = false; |
2233 | ||
344dc16f MT |
2234 | if (vq->vring.desc && vq->handle_aio_output) { |
2235 | VirtIODevice *vdev = vq->vdev; | |
2236 | ||
2237 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); | |
badaf79c XY |
2238 | ret = vq->handle_aio_output(vdev, vq); |
2239 | ||
2240 | if (unlikely(vdev->start_on_kick)) { | |
e57f2c31 | 2241 | virtio_set_started(vdev, true); |
badaf79c | 2242 | } |
344dc16f | 2243 | } |
07931698 | 2244 | |
badaf79c | 2245 | return ret; |
344dc16f MT |
2246 | } |
2247 | ||
2b2cbcad | 2248 | static void virtio_queue_notify_vq(VirtQueue *vq) |
25db9ebe | 2249 | { |
9e0f5b81 | 2250 | if (vq->vring.desc && vq->handle_output) { |
25db9ebe | 2251 | VirtIODevice *vdev = vq->vdev; |
9e0f5b81 | 2252 | |
f5ed3663 SH |
2253 | if (unlikely(vdev->broken)) { |
2254 | return; | |
2255 | } | |
2256 | ||
25db9ebe SH |
2257 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); |
2258 | vq->handle_output(vdev, vq); | |
badaf79c XY |
2259 | |
2260 | if (unlikely(vdev->start_on_kick)) { | |
e57f2c31 | 2261 | virtio_set_started(vdev, true); |
badaf79c | 2262 | } |
25db9ebe SH |
2263 | } |
2264 | } | |
2265 | ||
53c25cea PB |
2266 | void virtio_queue_notify(VirtIODevice *vdev, int n) |
2267 | { | |
e49a6618 PB |
2268 | VirtQueue *vq = &vdev->vq[n]; |
2269 | ||
2270 | if (unlikely(!vq->vring.desc || vdev->broken)) { | |
2271 | return; | |
2272 | } | |
2273 | ||
2274 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); | |
fcccb271 | 2275 | if (vq->host_notifier_enabled) { |
e49a6618 PB |
2276 | event_notifier_set(&vq->host_notifier); |
2277 | } else if (vq->handle_output) { | |
2278 | vq->handle_output(vdev, vq); | |
badaf79c | 2279 | |
8b04e2c7 XY |
2280 | if (unlikely(vdev->start_on_kick)) { |
2281 | virtio_set_started(vdev, true); | |
2282 | } | |
badaf79c | 2283 | } |
967f97fa AL |
2284 | } |
2285 | ||
7055e687 MT |
2286 | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n) |
2287 | { | |
87b3bd1c | 2288 | return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector : |
7055e687 MT |
2289 | VIRTIO_NO_VECTOR; |
2290 | } | |
2291 | ||
2292 | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector) | |
2293 | { | |
e0d686bf JW |
2294 | VirtQueue *vq = &vdev->vq[n]; |
2295 | ||
87b3bd1c | 2296 | if (n < VIRTIO_QUEUE_MAX) { |
e0d686bf JW |
2297 | if (vdev->vector_queues && |
2298 | vdev->vq[n].vector != VIRTIO_NO_VECTOR) { | |
2299 | QLIST_REMOVE(vq, node); | |
2300 | } | |
7055e687 | 2301 | vdev->vq[n].vector = vector; |
e0d686bf JW |
2302 | if (vdev->vector_queues && |
2303 | vector != VIRTIO_NO_VECTOR) { | |
2304 | QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node); | |
2305 | } | |
2306 | } | |
7055e687 MT |
2307 | } |
2308 | ||
f1ac6a55 PB |
2309 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, |
2310 | VirtIOHandleOutput handle_output) | |
967f97fa AL |
2311 | { |
2312 | int i; | |
2313 | ||
87b3bd1c | 2314 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
2315 | if (vdev->vq[i].vring.num == 0) |
2316 | break; | |
2317 | } | |
2318 | ||
87b3bd1c | 2319 | if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE) |
967f97fa AL |
2320 | abort(); |
2321 | ||
2322 | vdev->vq[i].vring.num = queue_size; | |
46c5d082 | 2323 | vdev->vq[i].vring.num_default = queue_size; |
6ce69d1c | 2324 | vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN; |
967f97fa | 2325 | vdev->vq[i].handle_output = handle_output; |
344dc16f | 2326 | vdev->vq[i].handle_aio_output = NULL; |
86044b24 JW |
2327 | vdev->vq[i].used_elems = g_malloc0(sizeof(VirtQueueElement) * |
2328 | queue_size); | |
967f97fa AL |
2329 | |
2330 | return &vdev->vq[i]; | |
2331 | } | |
2332 | ||
722f8c51 MT |
2333 | void virtio_delete_queue(VirtQueue *vq) |
2334 | { | |
2335 | vq->vring.num = 0; | |
2336 | vq->vring.num_default = 0; | |
2337 | vq->handle_output = NULL; | |
2338 | vq->handle_aio_output = NULL; | |
2339 | g_free(vq->used_elems); | |
2340 | } | |
2341 | ||
f23fd811 JW |
2342 | void virtio_del_queue(VirtIODevice *vdev, int n) |
2343 | { | |
87b3bd1c | 2344 | if (n < 0 || n >= VIRTIO_QUEUE_MAX) { |
f23fd811 JW |
2345 | abort(); |
2346 | } | |
2347 | ||
722f8c51 | 2348 | virtio_delete_queue(&vdev->vq[n]); |
f23fd811 JW |
2349 | } |
2350 | ||
0687c37c PB |
2351 | static void virtio_set_isr(VirtIODevice *vdev, int value) |
2352 | { | |
2353 | uint8_t old = atomic_read(&vdev->isr); | |
2354 | ||
2355 | /* Do not write ISR if it does not change, so that its cacheline remains | |
2356 | * shared in the common case where the guest does not read it. | |
2357 | */ | |
2358 | if ((old & value) != value) { | |
2359 | atomic_or(&vdev->isr, value); | |
2360 | } | |
2361 | } | |
2362 | ||
683f7665 | 2363 | static bool virtio_split_should_notify(VirtIODevice *vdev, VirtQueue *vq) |
bcbabae8 MT |
2364 | { |
2365 | uint16_t old, new; | |
2366 | bool v; | |
a281ebc1 MT |
2367 | /* We need to expose used array entries before checking used event. */ |
2368 | smp_mb(); | |
97b83deb | 2369 | /* Always notify when queue is empty (when feature acknowledge) */ |
95129d6f | 2370 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) && |
be1fea9b | 2371 | !vq->inuse && virtio_queue_empty(vq)) { |
bcbabae8 MT |
2372 | return true; |
2373 | } | |
2374 | ||
95129d6f | 2375 | if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
bcbabae8 MT |
2376 | return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT); |
2377 | } | |
2378 | ||
2379 | v = vq->signalled_used_valid; | |
2380 | vq->signalled_used_valid = true; | |
2381 | old = vq->signalled_used; | |
b796fcd1 | 2382 | new = vq->signalled_used = vq->used_idx; |
e9600c6c | 2383 | return !v || vring_need_event(vring_get_used_event(vq), new, old); |
bcbabae8 MT |
2384 | } |
2385 | ||
683f7665 JW |
2386 | static bool vring_packed_need_event(VirtQueue *vq, bool wrap, |
2387 | uint16_t off_wrap, uint16_t new, | |
2388 | uint16_t old) | |
2389 | { | |
2390 | int off = off_wrap & ~(1 << 15); | |
2391 | ||
2392 | if (wrap != off_wrap >> 15) { | |
2393 | off -= vq->vring.num; | |
2394 | } | |
2395 | ||
2396 | return vring_need_event(off, new, old); | |
2397 | } | |
2398 | ||
2399 | static bool virtio_packed_should_notify(VirtIODevice *vdev, VirtQueue *vq) | |
2400 | { | |
2401 | VRingPackedDescEvent e; | |
2402 | uint16_t old, new; | |
2403 | bool v; | |
2404 | VRingMemoryRegionCaches *caches; | |
2405 | ||
2406 | caches = vring_get_region_caches(vq); | |
2407 | vring_packed_event_read(vdev, &caches->avail, &e); | |
2408 | ||
2409 | old = vq->signalled_used; | |
2410 | new = vq->signalled_used = vq->used_idx; | |
2411 | v = vq->signalled_used_valid; | |
2412 | vq->signalled_used_valid = true; | |
2413 | ||
2414 | if (e.flags == VRING_PACKED_EVENT_FLAG_DISABLE) { | |
2415 | return false; | |
2416 | } else if (e.flags == VRING_PACKED_EVENT_FLAG_ENABLE) { | |
2417 | return true; | |
2418 | } | |
2419 | ||
2420 | return !v || vring_packed_need_event(vq, vq->used_wrap_counter, | |
2421 | e.off_wrap, new, old); | |
2422 | } | |
2423 | ||
2424 | /* Called within rcu_read_lock(). */ | |
2425 | static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) | |
2426 | { | |
2427 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { | |
2428 | return virtio_packed_should_notify(vdev, vq); | |
2429 | } else { | |
2430 | return virtio_split_should_notify(vdev, vq); | |
2431 | } | |
2432 | } | |
2433 | ||
83d768b5 PB |
2434 | void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq) |
2435 | { | |
b5f53d04 DDAG |
2436 | WITH_RCU_READ_LOCK_GUARD() { |
2437 | if (!virtio_should_notify(vdev, vq)) { | |
2438 | return; | |
2439 | } | |
83d768b5 PB |
2440 | } |
2441 | ||
2442 | trace_virtio_notify_irqfd(vdev, vq); | |
2443 | ||
2444 | /* | |
2445 | * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but | |
2446 | * windows drivers included in virtio-win 1.8.0 (circa 2015) are | |
2447 | * incorrectly polling this bit during crashdump and hibernation | |
2448 | * in MSI mode, causing a hang if this bit is never updated. | |
2449 | * Recent releases of Windows do not really shut down, but rather | |
2450 | * log out and hibernate to make the next startup faster. Hence, | |
2451 | * this manifested as a more serious hang during shutdown with | |
2452 | * | |
2453 | * Next driver release from 2016 fixed this problem, so working around it | |
2454 | * is not a must, but it's easy to do so let's do it here. | |
2455 | * | |
2456 | * Note: it's safe to update ISR from any thread as it was switched | |
2457 | * to an atomic operation. | |
2458 | */ | |
2459 | virtio_set_isr(vq->vdev, 0x1); | |
2460 | event_notifier_set(&vq->guest_notifier); | |
2461 | } | |
2462 | ||
b4b9862b MT |
2463 | static void virtio_irq(VirtQueue *vq) |
2464 | { | |
2465 | virtio_set_isr(vq->vdev, 0x1); | |
2466 | virtio_notify_vector(vq->vdev, vq->vector); | |
2467 | } | |
2468 | ||
bcbabae8 MT |
2469 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) |
2470 | { | |
b5f53d04 DDAG |
2471 | WITH_RCU_READ_LOCK_GUARD() { |
2472 | if (!virtio_should_notify(vdev, vq)) { | |
2473 | return; | |
2474 | } | |
bcbabae8 | 2475 | } |
967f97fa | 2476 | |
64979a4d | 2477 | trace_virtio_notify(vdev, vq); |
b4b9862b | 2478 | virtio_irq(vq); |
967f97fa AL |
2479 | } |
2480 | ||
2481 | void virtio_notify_config(VirtIODevice *vdev) | |
2482 | { | |
7625162c AL |
2483 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) |
2484 | return; | |
2485 | ||
0687c37c | 2486 | virtio_set_isr(vdev, 0x3); |
b8f05908 | 2487 | vdev->generation++; |
7055e687 | 2488 | virtio_notify_vector(vdev, vdev->config_vector); |
967f97fa AL |
2489 | } |
2490 | ||
616a6552 GK |
2491 | static bool virtio_device_endian_needed(void *opaque) |
2492 | { | |
2493 | VirtIODevice *vdev = opaque; | |
2494 | ||
2495 | assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN); | |
95129d6f | 2496 | if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
3c185597 CH |
2497 | return vdev->device_endian != virtio_default_endian(); |
2498 | } | |
2499 | /* Devices conforming to VIRTIO 1.0 or later are always LE. */ | |
2500 | return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE; | |
616a6552 GK |
2501 | } |
2502 | ||
019a3edb GH |
2503 | static bool virtio_64bit_features_needed(void *opaque) |
2504 | { | |
2505 | VirtIODevice *vdev = opaque; | |
2506 | ||
2507 | return (vdev->host_features >> 32) != 0; | |
2508 | } | |
2509 | ||
74aae7b2 JW |
2510 | static bool virtio_virtqueue_needed(void *opaque) |
2511 | { | |
2512 | VirtIODevice *vdev = opaque; | |
2513 | ||
2514 | return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1); | |
2515 | } | |
2516 | ||
86044b24 JW |
2517 | static bool virtio_packed_virtqueue_needed(void *opaque) |
2518 | { | |
2519 | VirtIODevice *vdev = opaque; | |
2520 | ||
2521 | return virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED); | |
2522 | } | |
2523 | ||
46c5d082 CH |
2524 | static bool virtio_ringsize_needed(void *opaque) |
2525 | { | |
2526 | VirtIODevice *vdev = opaque; | |
2527 | int i; | |
2528 | ||
2529 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
2530 | if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) { | |
2531 | return true; | |
2532 | } | |
2533 | } | |
2534 | return false; | |
2535 | } | |
2536 | ||
a6df8adf JW |
2537 | static bool virtio_extra_state_needed(void *opaque) |
2538 | { | |
2539 | VirtIODevice *vdev = opaque; | |
2540 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
2541 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
2542 | ||
2543 | return k->has_extra_state && | |
2544 | k->has_extra_state(qbus->parent); | |
2545 | } | |
2546 | ||
791b1daf SH |
2547 | static bool virtio_broken_needed(void *opaque) |
2548 | { | |
2549 | VirtIODevice *vdev = opaque; | |
2550 | ||
2551 | return vdev->broken; | |
2552 | } | |
2553 | ||
badaf79c XY |
2554 | static bool virtio_started_needed(void *opaque) |
2555 | { | |
2556 | VirtIODevice *vdev = opaque; | |
2557 | ||
2558 | return vdev->started; | |
2559 | } | |
2560 | ||
50e5ae4d | 2561 | static const VMStateDescription vmstate_virtqueue = { |
74aae7b2 | 2562 | .name = "virtqueue_state", |
50e5ae4d DDAG |
2563 | .version_id = 1, |
2564 | .minimum_version_id = 1, | |
2565 | .fields = (VMStateField[]) { | |
2566 | VMSTATE_UINT64(vring.avail, struct VirtQueue), | |
2567 | VMSTATE_UINT64(vring.used, struct VirtQueue), | |
2568 | VMSTATE_END_OF_LIST() | |
2569 | } | |
74aae7b2 JW |
2570 | }; |
2571 | ||
86044b24 JW |
2572 | static const VMStateDescription vmstate_packed_virtqueue = { |
2573 | .name = "packed_virtqueue_state", | |
2574 | .version_id = 1, | |
2575 | .minimum_version_id = 1, | |
2576 | .fields = (VMStateField[]) { | |
2577 | VMSTATE_UINT16(last_avail_idx, struct VirtQueue), | |
2578 | VMSTATE_BOOL(last_avail_wrap_counter, struct VirtQueue), | |
2579 | VMSTATE_UINT16(used_idx, struct VirtQueue), | |
2580 | VMSTATE_BOOL(used_wrap_counter, struct VirtQueue), | |
2581 | VMSTATE_UINT32(inuse, struct VirtQueue), | |
2582 | VMSTATE_END_OF_LIST() | |
2583 | } | |
2584 | }; | |
2585 | ||
74aae7b2 JW |
2586 | static const VMStateDescription vmstate_virtio_virtqueues = { |
2587 | .name = "virtio/virtqueues", | |
2588 | .version_id = 1, | |
2589 | .minimum_version_id = 1, | |
2590 | .needed = &virtio_virtqueue_needed, | |
2591 | .fields = (VMStateField[]) { | |
3e996cc5 DDAG |
2592 | VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice, |
2593 | VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue), | |
74aae7b2 JW |
2594 | VMSTATE_END_OF_LIST() |
2595 | } | |
2596 | }; | |
2597 | ||
86044b24 JW |
2598 | static const VMStateDescription vmstate_virtio_packed_virtqueues = { |
2599 | .name = "virtio/packed_virtqueues", | |
2600 | .version_id = 1, | |
2601 | .minimum_version_id = 1, | |
2602 | .needed = &virtio_packed_virtqueue_needed, | |
2603 | .fields = (VMStateField[]) { | |
2604 | VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice, | |
2605 | VIRTIO_QUEUE_MAX, 0, vmstate_packed_virtqueue, VirtQueue), | |
2606 | VMSTATE_END_OF_LIST() | |
2607 | } | |
2608 | }; | |
2609 | ||
50e5ae4d | 2610 | static const VMStateDescription vmstate_ringsize = { |
46c5d082 | 2611 | .name = "ringsize_state", |
50e5ae4d DDAG |
2612 | .version_id = 1, |
2613 | .minimum_version_id = 1, | |
2614 | .fields = (VMStateField[]) { | |
2615 | VMSTATE_UINT32(vring.num_default, struct VirtQueue), | |
2616 | VMSTATE_END_OF_LIST() | |
2617 | } | |
46c5d082 CH |
2618 | }; |
2619 | ||
2620 | static const VMStateDescription vmstate_virtio_ringsize = { | |
2621 | .name = "virtio/ringsize", | |
2622 | .version_id = 1, | |
2623 | .minimum_version_id = 1, | |
2624 | .needed = &virtio_ringsize_needed, | |
2625 | .fields = (VMStateField[]) { | |
3e996cc5 DDAG |
2626 | VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice, |
2627 | VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue), | |
46c5d082 CH |
2628 | VMSTATE_END_OF_LIST() |
2629 | } | |
2630 | }; | |
2631 | ||
2c21ee76 | 2632 | static int get_extra_state(QEMUFile *f, void *pv, size_t size, |
03fee66f | 2633 | const VMStateField *field) |
a6df8adf JW |
2634 | { |
2635 | VirtIODevice *vdev = pv; | |
2636 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
2637 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
2638 | ||
2639 | if (!k->load_extra_state) { | |
2640 | return -1; | |
2641 | } else { | |
2642 | return k->load_extra_state(qbus->parent, f); | |
2643 | } | |
2644 | } | |
2645 | ||
2c21ee76 | 2646 | static int put_extra_state(QEMUFile *f, void *pv, size_t size, |
03fee66f | 2647 | const VMStateField *field, QJSON *vmdesc) |
a6df8adf JW |
2648 | { |
2649 | VirtIODevice *vdev = pv; | |
2650 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
2651 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
2652 | ||
2653 | k->save_extra_state(qbus->parent, f); | |
2c21ee76 | 2654 | return 0; |
a6df8adf JW |
2655 | } |
2656 | ||
2657 | static const VMStateInfo vmstate_info_extra_state = { | |
2658 | .name = "virtqueue_extra_state", | |
2659 | .get = get_extra_state, | |
2660 | .put = put_extra_state, | |
2661 | }; | |
2662 | ||
2663 | static const VMStateDescription vmstate_virtio_extra_state = { | |
2664 | .name = "virtio/extra_state", | |
2665 | .version_id = 1, | |
2666 | .minimum_version_id = 1, | |
2667 | .needed = &virtio_extra_state_needed, | |
2668 | .fields = (VMStateField[]) { | |
2669 | { | |
2670 | .name = "extra_state", | |
2671 | .version_id = 0, | |
2672 | .field_exists = NULL, | |
2673 | .size = 0, | |
2674 | .info = &vmstate_info_extra_state, | |
2675 | .flags = VMS_SINGLE, | |
2676 | .offset = 0, | |
2677 | }, | |
2678 | VMSTATE_END_OF_LIST() | |
2679 | } | |
2680 | }; | |
2681 | ||
616a6552 GK |
2682 | static const VMStateDescription vmstate_virtio_device_endian = { |
2683 | .name = "virtio/device_endian", | |
2684 | .version_id = 1, | |
2685 | .minimum_version_id = 1, | |
5cd8cada | 2686 | .needed = &virtio_device_endian_needed, |
616a6552 GK |
2687 | .fields = (VMStateField[]) { |
2688 | VMSTATE_UINT8(device_endian, VirtIODevice), | |
2689 | VMSTATE_END_OF_LIST() | |
2690 | } | |
2691 | }; | |
2692 | ||
019a3edb GH |
2693 | static const VMStateDescription vmstate_virtio_64bit_features = { |
2694 | .name = "virtio/64bit_features", | |
2695 | .version_id = 1, | |
2696 | .minimum_version_id = 1, | |
5cd8cada | 2697 | .needed = &virtio_64bit_features_needed, |
019a3edb GH |
2698 | .fields = (VMStateField[]) { |
2699 | VMSTATE_UINT64(guest_features, VirtIODevice), | |
2700 | VMSTATE_END_OF_LIST() | |
2701 | } | |
2702 | }; | |
2703 | ||
791b1daf SH |
2704 | static const VMStateDescription vmstate_virtio_broken = { |
2705 | .name = "virtio/broken", | |
2706 | .version_id = 1, | |
2707 | .minimum_version_id = 1, | |
2708 | .needed = &virtio_broken_needed, | |
2709 | .fields = (VMStateField[]) { | |
2710 | VMSTATE_BOOL(broken, VirtIODevice), | |
2711 | VMSTATE_END_OF_LIST() | |
2712 | } | |
2713 | }; | |
2714 | ||
badaf79c XY |
2715 | static const VMStateDescription vmstate_virtio_started = { |
2716 | .name = "virtio/started", | |
2717 | .version_id = 1, | |
2718 | .minimum_version_id = 1, | |
2719 | .needed = &virtio_started_needed, | |
2720 | .fields = (VMStateField[]) { | |
2721 | VMSTATE_BOOL(started, VirtIODevice), | |
2722 | VMSTATE_END_OF_LIST() | |
2723 | } | |
2724 | }; | |
2725 | ||
6b321a3d GK |
2726 | static const VMStateDescription vmstate_virtio = { |
2727 | .name = "virtio", | |
2728 | .version_id = 1, | |
2729 | .minimum_version_id = 1, | |
2730 | .minimum_version_id_old = 1, | |
2731 | .fields = (VMStateField[]) { | |
2732 | VMSTATE_END_OF_LIST() | |
616a6552 | 2733 | }, |
5cd8cada JQ |
2734 | .subsections = (const VMStateDescription*[]) { |
2735 | &vmstate_virtio_device_endian, | |
2736 | &vmstate_virtio_64bit_features, | |
74aae7b2 | 2737 | &vmstate_virtio_virtqueues, |
46c5d082 | 2738 | &vmstate_virtio_ringsize, |
791b1daf | 2739 | &vmstate_virtio_broken, |
a6df8adf | 2740 | &vmstate_virtio_extra_state, |
badaf79c | 2741 | &vmstate_virtio_started, |
86044b24 | 2742 | &vmstate_virtio_packed_virtqueues, |
5cd8cada | 2743 | NULL |
6b321a3d GK |
2744 | } |
2745 | }; | |
2746 | ||
2f168d07 | 2747 | int virtio_save(VirtIODevice *vdev, QEMUFile *f) |
967f97fa | 2748 | { |
1c819449 FK |
2749 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
2750 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1b5fc0de | 2751 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
019a3edb | 2752 | uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff); |
967f97fa AL |
2753 | int i; |
2754 | ||
1c819449 FK |
2755 | if (k->save_config) { |
2756 | k->save_config(qbus->parent, f); | |
2757 | } | |
967f97fa | 2758 | |
967f97fa AL |
2759 | qemu_put_8s(f, &vdev->status); |
2760 | qemu_put_8s(f, &vdev->isr); | |
2761 | qemu_put_be16s(f, &vdev->queue_sel); | |
019a3edb | 2762 | qemu_put_be32s(f, &guest_features_lo); |
967f97fa AL |
2763 | qemu_put_be32(f, vdev->config_len); |
2764 | qemu_put_buffer(f, vdev->config, vdev->config_len); | |
2765 | ||
87b3bd1c | 2766 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
2767 | if (vdev->vq[i].vring.num == 0) |
2768 | break; | |
2769 | } | |
2770 | ||
2771 | qemu_put_be32(f, i); | |
2772 | ||
87b3bd1c | 2773 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
2774 | if (vdev->vq[i].vring.num == 0) |
2775 | break; | |
2776 | ||
2777 | qemu_put_be32(f, vdev->vq[i].vring.num); | |
6ce69d1c PM |
2778 | if (k->has_variable_vring_alignment) { |
2779 | qemu_put_be32(f, vdev->vq[i].vring.align); | |
2780 | } | |
874adf45 SH |
2781 | /* |
2782 | * Save desc now, the rest of the ring addresses are saved in | |
2783 | * subsections for VIRTIO-1 devices. | |
2784 | */ | |
ab223c95 | 2785 | qemu_put_be64(f, vdev->vq[i].vring.desc); |
967f97fa | 2786 | qemu_put_be16s(f, &vdev->vq[i].last_avail_idx); |
1c819449 FK |
2787 | if (k->save_queue) { |
2788 | k->save_queue(qbus->parent, i, f); | |
2789 | } | |
967f97fa | 2790 | } |
1b5fc0de GK |
2791 | |
2792 | if (vdc->save != NULL) { | |
2793 | vdc->save(vdev, f); | |
2794 | } | |
6b321a3d | 2795 | |
ea43e259 | 2796 | if (vdc->vmsd) { |
2f168d07 DDAG |
2797 | int ret = vmstate_save_state(f, vdc->vmsd, vdev, NULL); |
2798 | if (ret) { | |
2799 | return ret; | |
2800 | } | |
ea43e259 DDAG |
2801 | } |
2802 | ||
6b321a3d | 2803 | /* Subsections */ |
2f168d07 | 2804 | return vmstate_save_state(f, &vmstate_virtio, vdev, NULL); |
967f97fa AL |
2805 | } |
2806 | ||
1a665855 | 2807 | /* A wrapper for use as a VMState .put function */ |
2c21ee76 | 2808 | static int virtio_device_put(QEMUFile *f, void *opaque, size_t size, |
03fee66f | 2809 | const VMStateField *field, QJSON *vmdesc) |
1a665855 | 2810 | { |
2f168d07 | 2811 | return virtio_save(VIRTIO_DEVICE(opaque), f); |
1a665855 HP |
2812 | } |
2813 | ||
2814 | /* A wrapper for use as a VMState .get function */ | |
2c21ee76 | 2815 | static int virtio_device_get(QEMUFile *f, void *opaque, size_t size, |
03fee66f | 2816 | const VMStateField *field) |
1a665855 HP |
2817 | { |
2818 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); | |
2819 | DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev)); | |
2820 | ||
2821 | return virtio_load(vdev, f, dc->vmsd->version_id); | |
2822 | } | |
2823 | ||
2824 | const VMStateInfo virtio_vmstate_info = { | |
2825 | .name = "virtio", | |
2826 | .get = virtio_device_get, | |
2827 | .put = virtio_device_put, | |
2828 | }; | |
2829 | ||
6c0196d7 | 2830 | static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val) |
ad0c9332 | 2831 | { |
181103cd | 2832 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
6b8f1020 | 2833 | bool bad = (val & ~(vdev->host_features)) != 0; |
ad0c9332 | 2834 | |
6b8f1020 | 2835 | val &= vdev->host_features; |
181103cd FK |
2836 | if (k->set_features) { |
2837 | k->set_features(vdev, val); | |
ad0c9332 PB |
2838 | } |
2839 | vdev->guest_features = val; | |
2840 | return bad ? -1 : 0; | |
2841 | } | |
2842 | ||
6c0196d7 CH |
2843 | int virtio_set_features(VirtIODevice *vdev, uint64_t val) |
2844 | { | |
db812c40 PB |
2845 | int ret; |
2846 | /* | |
6c0196d7 CH |
2847 | * The driver must not attempt to set features after feature negotiation |
2848 | * has finished. | |
2849 | */ | |
2850 | if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) { | |
2851 | return -EINVAL; | |
2852 | } | |
db812c40 | 2853 | ret = virtio_set_features_nocheck(vdev, val); |
868a8f44 XY |
2854 | if (!ret) { |
2855 | if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { | |
2856 | /* VIRTIO_RING_F_EVENT_IDX changes the size of the caches. */ | |
2857 | int i; | |
2858 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
2859 | if (vdev->vq[i].vring.num != 0) { | |
2860 | virtio_init_region_cache(vdev, i); | |
2861 | } | |
db812c40 PB |
2862 | } |
2863 | } | |
868a8f44 XY |
2864 | |
2865 | if (!virtio_device_started(vdev, vdev->status) && | |
2866 | !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { | |
2867 | vdev->start_on_kick = true; | |
2868 | } | |
db812c40 PB |
2869 | } |
2870 | return ret; | |
6c0196d7 CH |
2871 | } |
2872 | ||
ba550851 SG |
2873 | size_t virtio_feature_get_config_size(VirtIOFeature *feature_sizes, |
2874 | uint64_t host_features) | |
2875 | { | |
2876 | size_t config_size = 0; | |
2877 | int i; | |
2878 | ||
2879 | for (i = 0; feature_sizes[i].flags != 0; i++) { | |
2880 | if (host_features & feature_sizes[i].flags) { | |
2881 | config_size = MAX(feature_sizes[i].end, config_size); | |
2882 | } | |
2883 | } | |
2884 | ||
2885 | return config_size; | |
2886 | } | |
2887 | ||
1b5fc0de | 2888 | int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) |
967f97fa | 2889 | { |
cc459952 | 2890 | int i, ret; |
a890a2f9 | 2891 | int32_t config_len; |
cc459952 | 2892 | uint32_t num; |
6d74ca5a | 2893 | uint32_t features; |
1c819449 FK |
2894 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
2895 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1b5fc0de | 2896 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa | 2897 | |
616a6552 GK |
2898 | /* |
2899 | * We poison the endianness to ensure it does not get used before | |
2900 | * subsections have been loaded. | |
2901 | */ | |
2902 | vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN; | |
2903 | ||
1c819449 FK |
2904 | if (k->load_config) { |
2905 | ret = k->load_config(qbus->parent, f); | |
ff24bd58 MT |
2906 | if (ret) |
2907 | return ret; | |
2908 | } | |
967f97fa | 2909 | |
967f97fa AL |
2910 | qemu_get_8s(f, &vdev->status); |
2911 | qemu_get_8s(f, &vdev->isr); | |
2912 | qemu_get_be16s(f, &vdev->queue_sel); | |
87b3bd1c | 2913 | if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) { |
4b53c2c7 MR |
2914 | return -1; |
2915 | } | |
6d74ca5a | 2916 | qemu_get_be32s(f, &features); |
ad0c9332 | 2917 | |
62cee1a2 MT |
2918 | /* |
2919 | * Temporarily set guest_features low bits - needed by | |
2920 | * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS | |
2921 | * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ. | |
2922 | * | |
2923 | * Note: devices should always test host features in future - don't create | |
2924 | * new dependencies like this. | |
2925 | */ | |
2926 | vdev->guest_features = features; | |
2927 | ||
a890a2f9 | 2928 | config_len = qemu_get_be32(f); |
2f5732e9 DDAG |
2929 | |
2930 | /* | |
2931 | * There are cases where the incoming config can be bigger or smaller | |
2932 | * than what we have; so load what we have space for, and skip | |
2933 | * any excess that's in the stream. | |
2934 | */ | |
2935 | qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len)); | |
2936 | ||
2937 | while (config_len > vdev->config_len) { | |
2938 | qemu_get_byte(f); | |
2939 | config_len--; | |
a890a2f9 | 2940 | } |
967f97fa AL |
2941 | |
2942 | num = qemu_get_be32(f); | |
2943 | ||
87b3bd1c | 2944 | if (num > VIRTIO_QUEUE_MAX) { |
8a1be662 | 2945 | error_report("Invalid number of virtqueues: 0x%x", num); |
cc459952 MT |
2946 | return -1; |
2947 | } | |
2948 | ||
967f97fa AL |
2949 | for (i = 0; i < num; i++) { |
2950 | vdev->vq[i].vring.num = qemu_get_be32(f); | |
6ce69d1c PM |
2951 | if (k->has_variable_vring_alignment) { |
2952 | vdev->vq[i].vring.align = qemu_get_be32(f); | |
2953 | } | |
ab223c95 | 2954 | vdev->vq[i].vring.desc = qemu_get_be64(f); |
967f97fa | 2955 | qemu_get_be16s(f, &vdev->vq[i].last_avail_idx); |
bcbabae8 | 2956 | vdev->vq[i].signalled_used_valid = false; |
332fa82d | 2957 | vdev->vq[i].notification = true; |
967f97fa | 2958 | |
874adf45 | 2959 | if (!vdev->vq[i].vring.desc && vdev->vq[i].last_avail_idx) { |
1abeb5a6 | 2960 | error_report("VQ %d address 0x0 " |
6daf194d | 2961 | "inconsistent with Host index 0x%x", |
1abeb5a6 | 2962 | i, vdev->vq[i].last_avail_idx); |
874adf45 | 2963 | return -1; |
8275e2f6 | 2964 | } |
1c819449 FK |
2965 | if (k->load_queue) { |
2966 | ret = k->load_queue(qbus->parent, i, f); | |
ff24bd58 MT |
2967 | if (ret) |
2968 | return ret; | |
7055e687 | 2969 | } |
967f97fa AL |
2970 | } |
2971 | ||
7055e687 | 2972 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
1b5fc0de GK |
2973 | |
2974 | if (vdc->load != NULL) { | |
6b321a3d GK |
2975 | ret = vdc->load(vdev, f, version_id); |
2976 | if (ret) { | |
2977 | return ret; | |
2978 | } | |
1b5fc0de GK |
2979 | } |
2980 | ||
ea43e259 DDAG |
2981 | if (vdc->vmsd) { |
2982 | ret = vmstate_load_state(f, vdc->vmsd, vdev, version_id); | |
2983 | if (ret) { | |
2984 | return ret; | |
2985 | } | |
2986 | } | |
2987 | ||
616a6552 GK |
2988 | /* Subsections */ |
2989 | ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1); | |
2990 | if (ret) { | |
2991 | return ret; | |
2992 | } | |
2993 | ||
2994 | if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) { | |
2995 | vdev->device_endian = virtio_default_endian(); | |
2996 | } | |
2997 | ||
019a3edb GH |
2998 | if (virtio_64bit_features_needed(vdev)) { |
2999 | /* | |
3000 | * Subsection load filled vdev->guest_features. Run them | |
3001 | * through virtio_set_features to sanity-check them against | |
3002 | * host_features. | |
3003 | */ | |
3004 | uint64_t features64 = vdev->guest_features; | |
6c0196d7 | 3005 | if (virtio_set_features_nocheck(vdev, features64) < 0) { |
019a3edb GH |
3006 | error_report("Features 0x%" PRIx64 " unsupported. " |
3007 | "Allowed features: 0x%" PRIx64, | |
3008 | features64, vdev->host_features); | |
3009 | return -1; | |
3010 | } | |
3011 | } else { | |
6c0196d7 | 3012 | if (virtio_set_features_nocheck(vdev, features) < 0) { |
019a3edb GH |
3013 | error_report("Features 0x%x unsupported. " |
3014 | "Allowed features: 0x%" PRIx64, | |
3015 | features, vdev->host_features); | |
3016 | return -1; | |
3017 | } | |
3018 | } | |
3019 | ||
868a8f44 XY |
3020 | if (!virtio_device_started(vdev, vdev->status) && |
3021 | !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { | |
3022 | vdev->start_on_kick = true; | |
3023 | } | |
3024 | ||
b5f53d04 | 3025 | RCU_READ_LOCK_GUARD(); |
616a6552 | 3026 | for (i = 0; i < num; i++) { |
ab223c95 | 3027 | if (vdev->vq[i].vring.desc) { |
616a6552 | 3028 | uint16_t nheads; |
874adf45 SH |
3029 | |
3030 | /* | |
3031 | * VIRTIO-1 devices migrate desc, used, and avail ring addresses so | |
3032 | * only the region cache needs to be set up. Legacy devices need | |
3033 | * to calculate used and avail ring addresses based on the desc | |
3034 | * address. | |
3035 | */ | |
3036 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { | |
3037 | virtio_init_region_cache(vdev, i); | |
3038 | } else { | |
3039 | virtio_queue_update_rings(vdev, i); | |
3040 | } | |
3041 | ||
86044b24 JW |
3042 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { |
3043 | vdev->vq[i].shadow_avail_idx = vdev->vq[i].last_avail_idx; | |
3044 | vdev->vq[i].shadow_avail_wrap_counter = | |
3045 | vdev->vq[i].last_avail_wrap_counter; | |
3046 | continue; | |
3047 | } | |
3048 | ||
616a6552 GK |
3049 | nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx; |
3050 | /* Check it isn't doing strange things with descriptor numbers. */ | |
3051 | if (nheads > vdev->vq[i].vring.num) { | |
3052 | error_report("VQ %d size 0x%x Guest index 0x%x " | |
3053 | "inconsistent with Host index 0x%x: delta 0x%x", | |
3054 | i, vdev->vq[i].vring.num, | |
3055 | vring_avail_idx(&vdev->vq[i]), | |
3056 | vdev->vq[i].last_avail_idx, nheads); | |
3057 | return -1; | |
3058 | } | |
b796fcd1 | 3059 | vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]); |
be1fea9b | 3060 | vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]); |
bccdef6b SH |
3061 | |
3062 | /* | |
3063 | * Some devices migrate VirtQueueElements that have been popped | |
3064 | * from the avail ring but not yet returned to the used ring. | |
e66bcc40 HP |
3065 | * Since max ring size < UINT16_MAX it's safe to use modulo |
3066 | * UINT16_MAX + 1 subtraction. | |
bccdef6b | 3067 | */ |
e66bcc40 HP |
3068 | vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx - |
3069 | vdev->vq[i].used_idx); | |
bccdef6b SH |
3070 | if (vdev->vq[i].inuse > vdev->vq[i].vring.num) { |
3071 | error_report("VQ %d size 0x%x < last_avail_idx 0x%x - " | |
3072 | "used_idx 0x%x", | |
3073 | i, vdev->vq[i].vring.num, | |
3074 | vdev->vq[i].last_avail_idx, | |
3075 | vdev->vq[i].used_idx); | |
3076 | return -1; | |
3077 | } | |
616a6552 GK |
3078 | } |
3079 | } | |
3080 | ||
1dd71383 MT |
3081 | if (vdc->post_load) { |
3082 | ret = vdc->post_load(vdev); | |
3083 | if (ret) { | |
3084 | return ret; | |
3085 | } | |
3086 | } | |
3087 | ||
616a6552 | 3088 | return 0; |
967f97fa AL |
3089 | } |
3090 | ||
6a1a8cc7 | 3091 | void virtio_cleanup(VirtIODevice *vdev) |
b946a153 | 3092 | { |
85cf2a8d | 3093 | qemu_del_vm_change_state_handler(vdev->vmstate); |
8e05db92 FK |
3094 | } |
3095 | ||
1dfb4dd9 | 3096 | static void virtio_vmstate_change(void *opaque, int running, RunState state) |
85cf2a8d MT |
3097 | { |
3098 | VirtIODevice *vdev = opaque; | |
1c819449 FK |
3099 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
3100 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
e57f2c31 | 3101 | bool backend_run = running && virtio_device_started(vdev, vdev->status); |
9e8e8c48 | 3102 | vdev->vm_running = running; |
85cf2a8d MT |
3103 | |
3104 | if (backend_run) { | |
3105 | virtio_set_status(vdev, vdev->status); | |
3106 | } | |
3107 | ||
1c819449 FK |
3108 | if (k->vmstate_change) { |
3109 | k->vmstate_change(qbus->parent, backend_run); | |
85cf2a8d MT |
3110 | } |
3111 | ||
3112 | if (!backend_run) { | |
3113 | virtio_set_status(vdev, vdev->status); | |
3114 | } | |
3115 | } | |
3116 | ||
c8075caf GA |
3117 | void virtio_instance_init_common(Object *proxy_obj, void *data, |
3118 | size_t vdev_size, const char *vdev_name) | |
3119 | { | |
3120 | DeviceState *vdev = data; | |
3121 | ||
3d2fc923 PMD |
3122 | object_initialize_child(proxy_obj, "virtio-backend", vdev, vdev_size, |
3123 | vdev_name, &error_abort, NULL); | |
c8075caf GA |
3124 | qdev_alias_all_properties(vdev, proxy_obj); |
3125 | } | |
3126 | ||
8e05db92 FK |
3127 | void virtio_init(VirtIODevice *vdev, const char *name, |
3128 | uint16_t device_id, size_t config_size) | |
967f97fa | 3129 | { |
e0d686bf JW |
3130 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
3131 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
b8193adb | 3132 | int i; |
e0d686bf JW |
3133 | int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0; |
3134 | ||
3135 | if (nvectors) { | |
3136 | vdev->vector_queues = | |
3137 | g_malloc0(sizeof(*vdev->vector_queues) * nvectors); | |
3138 | } | |
3139 | ||
868a8f44 | 3140 | vdev->start_on_kick = false; |
badaf79c | 3141 | vdev->started = false; |
53c25cea | 3142 | vdev->device_id = device_id; |
967f97fa | 3143 | vdev->status = 0; |
0687c37c | 3144 | atomic_set(&vdev->isr, 0); |
967f97fa | 3145 | vdev->queue_sel = 0; |
7055e687 | 3146 | vdev->config_vector = VIRTIO_NO_VECTOR; |
87b3bd1c | 3147 | vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX); |
1354869c | 3148 | vdev->vm_running = runstate_is_running(); |
f5ed3663 | 3149 | vdev->broken = false; |
87b3bd1c | 3150 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
b8193adb | 3151 | vdev->vq[i].vector = VIRTIO_NO_VECTOR; |
1cbdabe2 | 3152 | vdev->vq[i].vdev = vdev; |
e78a2b42 | 3153 | vdev->vq[i].queue_index = i; |
fcccb271 | 3154 | vdev->vq[i].host_notifier_enabled = false; |
1cbdabe2 | 3155 | } |
967f97fa | 3156 | |
967f97fa AL |
3157 | vdev->name = name; |
3158 | vdev->config_len = config_size; | |
8e05db92 | 3159 | if (vdev->config_len) { |
7267c094 | 3160 | vdev->config = g_malloc0(config_size); |
8e05db92 | 3161 | } else { |
967f97fa | 3162 | vdev->config = NULL; |
8e05db92 | 3163 | } |
1a8c091c SH |
3164 | vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev), |
3165 | virtio_vmstate_change, vdev); | |
616a6552 | 3166 | vdev->device_endian = virtio_default_endian(); |
5669655a | 3167 | vdev->use_guest_notifier_mask = true; |
8e05db92 | 3168 | } |
967f97fa | 3169 | |
a8170e5e | 3170 | hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
3171 | { |
3172 | return vdev->vq[n].vring.desc; | |
3173 | } | |
3174 | ||
23bfaf77 JW |
3175 | bool virtio_queue_enabled(VirtIODevice *vdev, int n) |
3176 | { | |
3177 | return virtio_queue_get_desc_addr(vdev, n) != 0; | |
3178 | } | |
3179 | ||
a8170e5e | 3180 | hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
3181 | { |
3182 | return vdev->vq[n].vring.avail; | |
3183 | } | |
3184 | ||
a8170e5e | 3185 | hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
3186 | { |
3187 | return vdev->vq[n].vring.used; | |
3188 | } | |
3189 | ||
a8170e5e | 3190 | hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
3191 | { |
3192 | return sizeof(VRingDesc) * vdev->vq[n].vring.num; | |
3193 | } | |
3194 | ||
a8170e5e | 3195 | hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n) |
1cbdabe2 | 3196 | { |
f90cda63 WX |
3197 | int s; |
3198 | ||
86044b24 JW |
3199 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { |
3200 | return sizeof(struct VRingPackedDescEvent); | |
3201 | } | |
3202 | ||
f90cda63 | 3203 | s = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
1cbdabe2 | 3204 | return offsetof(VRingAvail, ring) + |
f90cda63 | 3205 | sizeof(uint16_t) * vdev->vq[n].vring.num + s; |
1cbdabe2 MT |
3206 | } |
3207 | ||
a8170e5e | 3208 | hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n) |
1cbdabe2 | 3209 | { |
f90cda63 WX |
3210 | int s; |
3211 | ||
86044b24 JW |
3212 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { |
3213 | return sizeof(struct VRingPackedDescEvent); | |
3214 | } | |
3215 | ||
f90cda63 | 3216 | s = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
1cbdabe2 | 3217 | return offsetof(VRingUsed, ring) + |
f90cda63 | 3218 | sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s; |
1cbdabe2 MT |
3219 | } |
3220 | ||
86044b24 JW |
3221 | static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice *vdev, |
3222 | int n) | |
3223 | { | |
3224 | unsigned int avail, used; | |
3225 | ||
3226 | avail = vdev->vq[n].last_avail_idx; | |
3227 | avail |= ((uint16_t)vdev->vq[n].last_avail_wrap_counter) << 15; | |
3228 | ||
3229 | used = vdev->vq[n].used_idx; | |
3230 | used |= ((uint16_t)vdev->vq[n].used_wrap_counter) << 15; | |
3231 | ||
3232 | return avail | used << 16; | |
3233 | } | |
3234 | ||
3235 | static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice *vdev, | |
3236 | int n) | |
1cbdabe2 MT |
3237 | { |
3238 | return vdev->vq[n].last_avail_idx; | |
3239 | } | |
3240 | ||
86044b24 | 3241 | unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) |
1cbdabe2 | 3242 | { |
86044b24 JW |
3243 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { |
3244 | return virtio_queue_packed_get_last_avail_idx(vdev, n); | |
3245 | } else { | |
3246 | return virtio_queue_split_get_last_avail_idx(vdev, n); | |
3247 | } | |
1cbdabe2 MT |
3248 | } |
3249 | ||
86044b24 JW |
3250 | static void virtio_queue_packed_set_last_avail_idx(VirtIODevice *vdev, |
3251 | int n, unsigned int idx) | |
3252 | { | |
3253 | struct VirtQueue *vq = &vdev->vq[n]; | |
3254 | ||
3255 | vq->last_avail_idx = vq->shadow_avail_idx = idx & 0x7fff; | |
3256 | vq->last_avail_wrap_counter = | |
3257 | vq->shadow_avail_wrap_counter = !!(idx & 0x8000); | |
3258 | idx >>= 16; | |
3259 | vq->used_idx = idx & 0x7ffff; | |
3260 | vq->used_wrap_counter = !!(idx & 0x8000); | |
3261 | } | |
3262 | ||
3263 | static void virtio_queue_split_set_last_avail_idx(VirtIODevice *vdev, | |
3264 | int n, unsigned int idx) | |
3265 | { | |
3266 | vdev->vq[n].last_avail_idx = idx; | |
3267 | vdev->vq[n].shadow_avail_idx = idx; | |
3268 | } | |
3269 | ||
3270 | void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, | |
3271 | unsigned int idx) | |
3272 | { | |
3273 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { | |
3274 | virtio_queue_packed_set_last_avail_idx(vdev, n, idx); | |
3275 | } else { | |
3276 | virtio_queue_split_set_last_avail_idx(vdev, n, idx); | |
3277 | } | |
3278 | } | |
3279 | ||
3280 | static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice *vdev, | |
3281 | int n) | |
3282 | { | |
3283 | /* We don't have a reference like avail idx in shared memory */ | |
3284 | return; | |
3285 | } | |
3286 | ||
3287 | static void virtio_queue_split_restore_last_avail_idx(VirtIODevice *vdev, | |
3288 | int n) | |
2d4ba6cc | 3289 | { |
b5f53d04 | 3290 | RCU_READ_LOCK_GUARD(); |
2d4ba6cc MC |
3291 | if (vdev->vq[n].vring.desc) { |
3292 | vdev->vq[n].last_avail_idx = vring_used_idx(&vdev->vq[n]); | |
3293 | vdev->vq[n].shadow_avail_idx = vdev->vq[n].last_avail_idx; | |
3294 | } | |
2d4ba6cc MC |
3295 | } |
3296 | ||
86044b24 JW |
3297 | void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n) |
3298 | { | |
3299 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { | |
3300 | virtio_queue_packed_restore_last_avail_idx(vdev, n); | |
3301 | } else { | |
3302 | virtio_queue_split_restore_last_avail_idx(vdev, n); | |
3303 | } | |
3304 | } | |
3305 | ||
3306 | static void virtio_queue_packed_update_used_idx(VirtIODevice *vdev, int n) | |
3307 | { | |
3308 | /* used idx was updated through set_last_avail_idx() */ | |
3309 | return; | |
3310 | } | |
3311 | ||
3312 | static void virtio_split_packed_update_used_idx(VirtIODevice *vdev, int n) | |
312d3b35 | 3313 | { |
b5f53d04 | 3314 | RCU_READ_LOCK_GUARD(); |
ca0176ad PB |
3315 | if (vdev->vq[n].vring.desc) { |
3316 | vdev->vq[n].used_idx = vring_used_idx(&vdev->vq[n]); | |
3317 | } | |
312d3b35 YB |
3318 | } |
3319 | ||
86044b24 JW |
3320 | void virtio_queue_update_used_idx(VirtIODevice *vdev, int n) |
3321 | { | |
3322 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { | |
3323 | return virtio_queue_packed_update_used_idx(vdev, n); | |
3324 | } else { | |
3325 | return virtio_split_packed_update_used_idx(vdev, n); | |
3326 | } | |
3327 | } | |
3328 | ||
6793dfd1 SH |
3329 | void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n) |
3330 | { | |
3331 | vdev->vq[n].signalled_used_valid = false; | |
3332 | } | |
3333 | ||
1cbdabe2 MT |
3334 | VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n) |
3335 | { | |
3336 | return vdev->vq + n; | |
3337 | } | |
3338 | ||
e78a2b42 JW |
3339 | uint16_t virtio_get_queue_index(VirtQueue *vq) |
3340 | { | |
3341 | return vq->queue_index; | |
3342 | } | |
3343 | ||
15b2bd18 PB |
3344 | static void virtio_queue_guest_notifier_read(EventNotifier *n) |
3345 | { | |
3346 | VirtQueue *vq = container_of(n, VirtQueue, guest_notifier); | |
3347 | if (event_notifier_test_and_clear(n)) { | |
b4b9862b | 3348 | virtio_irq(vq); |
15b2bd18 PB |
3349 | } |
3350 | } | |
3351 | ||
3352 | void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, | |
3353 | bool with_irqfd) | |
3354 | { | |
3355 | if (assign && !with_irqfd) { | |
d6da1e9e | 3356 | event_notifier_set_handler(&vq->guest_notifier, |
15b2bd18 PB |
3357 | virtio_queue_guest_notifier_read); |
3358 | } else { | |
d6da1e9e | 3359 | event_notifier_set_handler(&vq->guest_notifier, NULL); |
15b2bd18 PB |
3360 | } |
3361 | if (!assign) { | |
3362 | /* Test and clear notifier before closing it, | |
3363 | * in case poll callback didn't have time to run. */ | |
3364 | virtio_queue_guest_notifier_read(&vq->guest_notifier); | |
3365 | } | |
3366 | } | |
3367 | ||
1cbdabe2 MT |
3368 | EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq) |
3369 | { | |
3370 | return &vq->guest_notifier; | |
3371 | } | |
b1f416aa | 3372 | |
344dc16f | 3373 | static void virtio_queue_host_notifier_aio_read(EventNotifier *n) |
b1f416aa PB |
3374 | { |
3375 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
3376 | if (event_notifier_test_and_clear(n)) { | |
344dc16f | 3377 | virtio_queue_notify_aio_vq(vq); |
b1f416aa PB |
3378 | } |
3379 | } | |
3380 | ||
a7c8215e SH |
3381 | static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier *n) |
3382 | { | |
3383 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
3384 | ||
3385 | virtio_queue_set_notification(vq, 0); | |
3386 | } | |
3387 | ||
0062ea0f SH |
3388 | static bool virtio_queue_host_notifier_aio_poll(void *opaque) |
3389 | { | |
3390 | EventNotifier *n = opaque; | |
3391 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
07931698 | 3392 | bool progress; |
0062ea0f | 3393 | |
dd3dd4ba | 3394 | if (!vq->vring.desc || virtio_queue_empty(vq)) { |
0062ea0f SH |
3395 | return false; |
3396 | } | |
3397 | ||
07931698 | 3398 | progress = virtio_queue_notify_aio_vq(vq); |
1448c133 SH |
3399 | |
3400 | /* In case the handler function re-enabled notifications */ | |
3401 | virtio_queue_set_notification(vq, 0); | |
07931698 | 3402 | return progress; |
0062ea0f SH |
3403 | } |
3404 | ||
a7c8215e SH |
3405 | static void virtio_queue_host_notifier_aio_poll_end(EventNotifier *n) |
3406 | { | |
3407 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
3408 | ||
3409 | /* Caller polls once more after this to catch requests that race with us */ | |
3410 | virtio_queue_set_notification(vq, 1); | |
3411 | } | |
3412 | ||
a1afb606 | 3413 | void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx, |
07931698 | 3414 | VirtIOHandleAIOOutput handle_output) |
a1afb606 | 3415 | { |
a378b49a PB |
3416 | if (handle_output) { |
3417 | vq->handle_aio_output = handle_output; | |
a1afb606 | 3418 | aio_set_event_notifier(ctx, &vq->host_notifier, true, |
0062ea0f SH |
3419 | virtio_queue_host_notifier_aio_read, |
3420 | virtio_queue_host_notifier_aio_poll); | |
a7c8215e SH |
3421 | aio_set_event_notifier_poll(ctx, &vq->host_notifier, |
3422 | virtio_queue_host_notifier_aio_poll_begin, | |
3423 | virtio_queue_host_notifier_aio_poll_end); | |
a1afb606 | 3424 | } else { |
f6a51c84 | 3425 | aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL, NULL); |
a1afb606 PB |
3426 | /* Test and clear notifier before after disabling event, |
3427 | * in case poll callback didn't have time to run. */ | |
344dc16f | 3428 | virtio_queue_host_notifier_aio_read(&vq->host_notifier); |
a378b49a | 3429 | vq->handle_aio_output = NULL; |
344dc16f MT |
3430 | } |
3431 | } | |
3432 | ||
fa283a4a | 3433 | void virtio_queue_host_notifier_read(EventNotifier *n) |
344dc16f MT |
3434 | { |
3435 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
3436 | if (event_notifier_test_and_clear(n)) { | |
3437 | virtio_queue_notify_vq(vq); | |
a1afb606 PB |
3438 | } |
3439 | } | |
3440 | ||
1cbdabe2 MT |
3441 | EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq) |
3442 | { | |
3443 | return &vq->host_notifier; | |
3444 | } | |
8e05db92 | 3445 | |
fcccb271 SH |
3446 | void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled) |
3447 | { | |
3448 | vq->host_notifier_enabled = enabled; | |
3449 | } | |
3450 | ||
6f80e617 TB |
3451 | int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n, |
3452 | MemoryRegion *mr, bool assign) | |
3453 | { | |
3454 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
3455 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
3456 | ||
3457 | if (k->set_host_notifier_mr) { | |
3458 | return k->set_host_notifier_mr(qbus->parent, n, mr, assign); | |
3459 | } | |
3460 | ||
3461 | return -1; | |
3462 | } | |
3463 | ||
1034e9cf FK |
3464 | void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name) |
3465 | { | |
9e288406 | 3466 | g_free(vdev->bus_name); |
80e0090a | 3467 | vdev->bus_name = g_strdup(bus_name); |
1034e9cf FK |
3468 | } |
3469 | ||
f5ed3663 SH |
3470 | void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice *vdev, const char *fmt, ...) |
3471 | { | |
3472 | va_list ap; | |
3473 | ||
3474 | va_start(ap, fmt); | |
3475 | error_vreport(fmt, ap); | |
3476 | va_end(ap); | |
3477 | ||
f5ed3663 | 3478 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
8fc47c87 | 3479 | vdev->status = vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET; |
f5ed3663 SH |
3480 | virtio_notify_config(vdev); |
3481 | } | |
66453cff GK |
3482 | |
3483 | vdev->broken = true; | |
f5ed3663 SH |
3484 | } |
3485 | ||
c611c764 PB |
3486 | static void virtio_memory_listener_commit(MemoryListener *listener) |
3487 | { | |
3488 | VirtIODevice *vdev = container_of(listener, VirtIODevice, listener); | |
3489 | int i; | |
3490 | ||
3491 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
3492 | if (vdev->vq[i].vring.num == 0) { | |
3493 | break; | |
3494 | } | |
3495 | virtio_init_region_cache(vdev, i); | |
3496 | } | |
3497 | } | |
3498 | ||
1d244b42 AF |
3499 | static void virtio_device_realize(DeviceState *dev, Error **errp) |
3500 | { | |
3501 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
3502 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); | |
3503 | Error *err = NULL; | |
3504 | ||
ea43e259 DDAG |
3505 | /* Devices should either use vmsd or the load/save methods */ |
3506 | assert(!vdc->vmsd || !vdc->load); | |
3507 | ||
1d244b42 AF |
3508 | if (vdc->realize != NULL) { |
3509 | vdc->realize(dev, &err); | |
3510 | if (err != NULL) { | |
3511 | error_propagate(errp, err); | |
3512 | return; | |
3513 | } | |
8e05db92 | 3514 | } |
e8398045 JW |
3515 | |
3516 | virtio_bus_device_plugged(vdev, &err); | |
3517 | if (err != NULL) { | |
3518 | error_propagate(errp, err); | |
7abea552 | 3519 | vdc->unrealize(dev, NULL); |
e8398045 JW |
3520 | return; |
3521 | } | |
c611c764 PB |
3522 | |
3523 | vdev->listener.commit = virtio_memory_listener_commit; | |
3524 | memory_listener_register(&vdev->listener, vdev->dma_as); | |
8e05db92 FK |
3525 | } |
3526 | ||
1d244b42 | 3527 | static void virtio_device_unrealize(DeviceState *dev, Error **errp) |
1034e9cf | 3528 | { |
1d244b42 | 3529 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
306ec6c3 AF |
3530 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); |
3531 | Error *err = NULL; | |
1d244b42 | 3532 | |
83d07047 PB |
3533 | virtio_bus_device_unplugged(vdev); |
3534 | ||
306ec6c3 AF |
3535 | if (vdc->unrealize != NULL) { |
3536 | vdc->unrealize(dev, &err); | |
3537 | if (err != NULL) { | |
3538 | error_propagate(errp, err); | |
3539 | return; | |
3540 | } | |
5e96f5d2 | 3541 | } |
1d244b42 | 3542 | |
9e288406 MA |
3543 | g_free(vdev->bus_name); |
3544 | vdev->bus_name = NULL; | |
1034e9cf FK |
3545 | } |
3546 | ||
c611c764 PB |
3547 | static void virtio_device_free_virtqueues(VirtIODevice *vdev) |
3548 | { | |
3549 | int i; | |
3550 | if (!vdev->vq) { | |
3551 | return; | |
3552 | } | |
3553 | ||
3554 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
c611c764 PB |
3555 | if (vdev->vq[i].vring.num == 0) { |
3556 | break; | |
3557 | } | |
e0e2d644 | 3558 | virtio_virtqueue_reset_region_cache(&vdev->vq[i]); |
c611c764 PB |
3559 | } |
3560 | g_free(vdev->vq); | |
3561 | } | |
3562 | ||
3563 | static void virtio_device_instance_finalize(Object *obj) | |
3564 | { | |
3565 | VirtIODevice *vdev = VIRTIO_DEVICE(obj); | |
3566 | ||
3567 | memory_listener_unregister(&vdev->listener); | |
3568 | virtio_device_free_virtqueues(vdev); | |
3569 | ||
3570 | g_free(vdev->config); | |
3571 | g_free(vdev->vector_queues); | |
3572 | } | |
3573 | ||
6b8f1020 CH |
3574 | static Property virtio_properties[] = { |
3575 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features), | |
e57f2c31 | 3576 | DEFINE_PROP_BOOL("use-started", VirtIODevice, use_started, true), |
6b8f1020 CH |
3577 | DEFINE_PROP_END_OF_LIST(), |
3578 | }; | |
3579 | ||
ff4c07df PB |
3580 | static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev) |
3581 | { | |
3582 | VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev))); | |
710fccf8 | 3583 | int i, n, r, err; |
ff4c07df | 3584 | |
710fccf8 | 3585 | memory_region_transaction_begin(); |
ff4c07df | 3586 | for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { |
fa283a4a | 3587 | VirtQueue *vq = &vdev->vq[n]; |
ff4c07df PB |
3588 | if (!virtio_queue_get_num(vdev, n)) { |
3589 | continue; | |
3590 | } | |
ed08a2a0 | 3591 | r = virtio_bus_set_host_notifier(qbus, n, true); |
ff4c07df PB |
3592 | if (r < 0) { |
3593 | err = r; | |
3594 | goto assign_error; | |
3595 | } | |
d6da1e9e | 3596 | event_notifier_set_handler(&vq->host_notifier, |
fa283a4a | 3597 | virtio_queue_host_notifier_read); |
6019f3b9 PB |
3598 | } |
3599 | ||
3600 | for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { | |
3601 | /* Kick right away to begin processing requests already in vring */ | |
3602 | VirtQueue *vq = &vdev->vq[n]; | |
3603 | if (!vq->vring.num) { | |
3604 | continue; | |
3605 | } | |
3606 | event_notifier_set(&vq->host_notifier); | |
ff4c07df | 3607 | } |
710fccf8 | 3608 | memory_region_transaction_commit(); |
ff4c07df PB |
3609 | return 0; |
3610 | ||
3611 | assign_error: | |
710fccf8 | 3612 | i = n; /* save n for a second iteration after transaction is committed. */ |
ff4c07df | 3613 | while (--n >= 0) { |
fa283a4a | 3614 | VirtQueue *vq = &vdev->vq[n]; |
ff4c07df PB |
3615 | if (!virtio_queue_get_num(vdev, n)) { |
3616 | continue; | |
3617 | } | |
3618 | ||
d6da1e9e | 3619 | event_notifier_set_handler(&vq->host_notifier, NULL); |
ed08a2a0 | 3620 | r = virtio_bus_set_host_notifier(qbus, n, false); |
ff4c07df | 3621 | assert(r >= 0); |
710fccf8 GH |
3622 | } |
3623 | memory_region_transaction_commit(); | |
3624 | ||
3625 | while (--i >= 0) { | |
3626 | if (!virtio_queue_get_num(vdev, i)) { | |
3627 | continue; | |
3628 | } | |
3629 | virtio_bus_cleanup_host_notifier(qbus, i); | |
ff4c07df PB |
3630 | } |
3631 | return err; | |
3632 | } | |
3633 | ||
3634 | int virtio_device_start_ioeventfd(VirtIODevice *vdev) | |
3635 | { | |
3636 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
3637 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
3638 | ||
3639 | return virtio_bus_start_ioeventfd(vbus); | |
3640 | } | |
3641 | ||
3642 | static void virtio_device_stop_ioeventfd_impl(VirtIODevice *vdev) | |
3643 | { | |
3644 | VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev))); | |
ff4c07df PB |
3645 | int n, r; |
3646 | ||
710fccf8 | 3647 | memory_region_transaction_begin(); |
ff4c07df | 3648 | for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { |
fa283a4a PB |
3649 | VirtQueue *vq = &vdev->vq[n]; |
3650 | ||
ff4c07df PB |
3651 | if (!virtio_queue_get_num(vdev, n)) { |
3652 | continue; | |
3653 | } | |
d6da1e9e | 3654 | event_notifier_set_handler(&vq->host_notifier, NULL); |
ed08a2a0 | 3655 | r = virtio_bus_set_host_notifier(qbus, n, false); |
ff4c07df | 3656 | assert(r >= 0); |
710fccf8 GH |
3657 | } |
3658 | memory_region_transaction_commit(); | |
3659 | ||
3660 | for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { | |
3661 | if (!virtio_queue_get_num(vdev, n)) { | |
3662 | continue; | |
3663 | } | |
76143618 | 3664 | virtio_bus_cleanup_host_notifier(qbus, n); |
ff4c07df PB |
3665 | } |
3666 | } | |
3667 | ||
310837de PB |
3668 | int virtio_device_grab_ioeventfd(VirtIODevice *vdev) |
3669 | { | |
3670 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
3671 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
3672 | ||
3673 | return virtio_bus_grab_ioeventfd(vbus); | |
3674 | } | |
3675 | ||
3676 | void virtio_device_release_ioeventfd(VirtIODevice *vdev) | |
3677 | { | |
3678 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
3679 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
3680 | ||
3681 | virtio_bus_release_ioeventfd(vbus); | |
3682 | } | |
3683 | ||
8e05db92 FK |
3684 | static void virtio_device_class_init(ObjectClass *klass, void *data) |
3685 | { | |
3686 | /* Set the default value here. */ | |
ff4c07df | 3687 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
8e05db92 | 3688 | DeviceClass *dc = DEVICE_CLASS(klass); |
1d244b42 AF |
3689 | |
3690 | dc->realize = virtio_device_realize; | |
3691 | dc->unrealize = virtio_device_unrealize; | |
8e05db92 | 3692 | dc->bus_type = TYPE_VIRTIO_BUS; |
6b8f1020 | 3693 | dc->props = virtio_properties; |
ff4c07df PB |
3694 | vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl; |
3695 | vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl; | |
9b706dbb MT |
3696 | |
3697 | vdc->legacy_features |= VIRTIO_LEGACY_FEATURES; | |
8e05db92 FK |
3698 | } |
3699 | ||
8e93cef1 PB |
3700 | bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev) |
3701 | { | |
3702 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
3703 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
3704 | ||
3705 | return virtio_bus_ioeventfd_enabled(vbus); | |
3706 | } | |
3707 | ||
8e05db92 FK |
3708 | static const TypeInfo virtio_device_info = { |
3709 | .name = TYPE_VIRTIO_DEVICE, | |
3710 | .parent = TYPE_DEVICE, | |
3711 | .instance_size = sizeof(VirtIODevice), | |
3712 | .class_init = virtio_device_class_init, | |
c611c764 | 3713 | .instance_finalize = virtio_device_instance_finalize, |
8e05db92 FK |
3714 | .abstract = true, |
3715 | .class_size = sizeof(VirtioDeviceClass), | |
3716 | }; | |
3717 | ||
3718 | static void virtio_register_types(void) | |
3719 | { | |
3720 | type_register_static(&virtio_device_info); | |
3721 | } | |
3722 | ||
3723 | type_init(virtio_register_types) |