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