]>
Commit | Line | Data |
---|---|---|
967f97fa AL |
1 | /* |
2 | * Virtio Support | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
14 | #include <inttypes.h> | |
967f97fa | 15 | |
64979a4d | 16 | #include "trace.h" |
fdfba1a2 | 17 | #include "exec/address-spaces.h" |
1de7afc9 | 18 | #include "qemu/error-report.h" |
0d09e41a | 19 | #include "hw/virtio/virtio.h" |
1de7afc9 | 20 | #include "qemu/atomic.h" |
0d09e41a | 21 | #include "hw/virtio/virtio-bus.h" |
6b321a3d | 22 | #include "migration/migration.h" |
cee3ca00 | 23 | #include "hw/virtio/virtio-access.h" |
967f97fa | 24 | |
6ce69d1c PM |
25 | /* |
26 | * The alignment to use between consumer and producer parts of vring. | |
27 | * x86 pagesize again. This is the default, used by transports like PCI | |
28 | * which don't provide a means for the guest to tell the host the alignment. | |
29 | */ | |
f46f15bc AL |
30 | #define VIRTIO_PCI_VRING_ALIGN 4096 |
31 | ||
967f97fa AL |
32 | typedef struct VRingDesc |
33 | { | |
34 | uint64_t addr; | |
35 | uint32_t len; | |
36 | uint16_t flags; | |
37 | uint16_t next; | |
38 | } VRingDesc; | |
39 | ||
40 | typedef struct VRingAvail | |
41 | { | |
42 | uint16_t flags; | |
43 | uint16_t idx; | |
44 | uint16_t ring[0]; | |
45 | } VRingAvail; | |
46 | ||
47 | typedef struct VRingUsedElem | |
48 | { | |
49 | uint32_t id; | |
50 | uint32_t len; | |
51 | } VRingUsedElem; | |
52 | ||
53 | typedef struct VRingUsed | |
54 | { | |
55 | uint16_t flags; | |
56 | uint16_t idx; | |
57 | VRingUsedElem ring[0]; | |
58 | } VRingUsed; | |
59 | ||
60 | typedef struct VRing | |
61 | { | |
62 | unsigned int num; | |
6ce69d1c | 63 | unsigned int align; |
a8170e5e AK |
64 | hwaddr desc; |
65 | hwaddr avail; | |
66 | hwaddr used; | |
967f97fa AL |
67 | } VRing; |
68 | ||
69 | struct VirtQueue | |
70 | { | |
71 | VRing vring; | |
a8170e5e | 72 | hwaddr pa; |
967f97fa | 73 | uint16_t last_avail_idx; |
bcbabae8 MT |
74 | /* Last used index value we have signalled on */ |
75 | uint16_t signalled_used; | |
76 | ||
77 | /* Last used index value we have signalled on */ | |
78 | bool signalled_used_valid; | |
79 | ||
80 | /* Notification enabled? */ | |
81 | bool notification; | |
82 | ||
e78a2b42 JW |
83 | uint16_t queue_index; |
84 | ||
967f97fa | 85 | int inuse; |
bcbabae8 | 86 | |
7055e687 | 87 | uint16_t vector; |
967f97fa | 88 | void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq); |
1cbdabe2 MT |
89 | VirtIODevice *vdev; |
90 | EventNotifier guest_notifier; | |
91 | EventNotifier host_notifier; | |
e0d686bf | 92 | QLIST_ENTRY(VirtQueue) node; |
967f97fa AL |
93 | }; |
94 | ||
967f97fa | 95 | /* virt queue functions */ |
53c25cea | 96 | static void virtqueue_init(VirtQueue *vq) |
967f97fa | 97 | { |
a8170e5e | 98 | hwaddr pa = vq->pa; |
53c25cea | 99 | |
967f97fa AL |
100 | vq->vring.desc = pa; |
101 | vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc); | |
f46f15bc AL |
102 | vq->vring.used = vring_align(vq->vring.avail + |
103 | offsetof(VRingAvail, ring[vq->vring.num]), | |
6ce69d1c | 104 | vq->vring.align); |
967f97fa AL |
105 | } |
106 | ||
cee3ca00 RR |
107 | static inline uint64_t vring_desc_addr(VirtIODevice *vdev, hwaddr desc_pa, |
108 | int i) | |
967f97fa | 109 | { |
a8170e5e | 110 | hwaddr pa; |
5774cf98 | 111 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr); |
cee3ca00 | 112 | return virtio_ldq_phys(vdev, pa); |
967f97fa AL |
113 | } |
114 | ||
cee3ca00 | 115 | static inline uint32_t vring_desc_len(VirtIODevice *vdev, hwaddr desc_pa, int i) |
967f97fa | 116 | { |
a8170e5e | 117 | hwaddr pa; |
5774cf98 | 118 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len); |
cee3ca00 | 119 | return virtio_ldl_phys(vdev, pa); |
967f97fa AL |
120 | } |
121 | ||
cee3ca00 RR |
122 | static inline uint16_t vring_desc_flags(VirtIODevice *vdev, hwaddr desc_pa, |
123 | int i) | |
967f97fa | 124 | { |
a8170e5e | 125 | hwaddr pa; |
5774cf98 | 126 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags); |
cee3ca00 | 127 | return virtio_lduw_phys(vdev, pa); |
967f97fa AL |
128 | } |
129 | ||
cee3ca00 RR |
130 | static inline uint16_t vring_desc_next(VirtIODevice *vdev, hwaddr desc_pa, |
131 | int i) | |
967f97fa | 132 | { |
a8170e5e | 133 | hwaddr pa; |
5774cf98 | 134 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next); |
cee3ca00 | 135 | return virtio_lduw_phys(vdev, pa); |
967f97fa AL |
136 | } |
137 | ||
138 | static inline uint16_t vring_avail_flags(VirtQueue *vq) | |
139 | { | |
a8170e5e | 140 | hwaddr pa; |
967f97fa | 141 | pa = vq->vring.avail + offsetof(VRingAvail, flags); |
cee3ca00 | 142 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
143 | } |
144 | ||
145 | static inline uint16_t vring_avail_idx(VirtQueue *vq) | |
146 | { | |
a8170e5e | 147 | hwaddr pa; |
967f97fa | 148 | pa = vq->vring.avail + offsetof(VRingAvail, idx); |
cee3ca00 | 149 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
150 | } |
151 | ||
152 | static inline uint16_t vring_avail_ring(VirtQueue *vq, int i) | |
153 | { | |
a8170e5e | 154 | hwaddr pa; |
967f97fa | 155 | pa = vq->vring.avail + offsetof(VRingAvail, ring[i]); |
cee3ca00 | 156 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
157 | } |
158 | ||
e9600c6c | 159 | static inline uint16_t vring_get_used_event(VirtQueue *vq) |
bcbabae8 MT |
160 | { |
161 | return vring_avail_ring(vq, vq->vring.num); | |
162 | } | |
163 | ||
967f97fa AL |
164 | static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val) |
165 | { | |
a8170e5e | 166 | hwaddr pa; |
967f97fa | 167 | pa = vq->vring.used + offsetof(VRingUsed, ring[i].id); |
cee3ca00 | 168 | virtio_stl_phys(vq->vdev, pa, val); |
967f97fa AL |
169 | } |
170 | ||
171 | static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val) | |
172 | { | |
a8170e5e | 173 | hwaddr pa; |
967f97fa | 174 | pa = vq->vring.used + offsetof(VRingUsed, ring[i].len); |
cee3ca00 | 175 | virtio_stl_phys(vq->vdev, pa, val); |
967f97fa AL |
176 | } |
177 | ||
178 | static uint16_t vring_used_idx(VirtQueue *vq) | |
179 | { | |
a8170e5e | 180 | hwaddr pa; |
967f97fa | 181 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
cee3ca00 | 182 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
183 | } |
184 | ||
bcbabae8 | 185 | static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val) |
967f97fa | 186 | { |
a8170e5e | 187 | hwaddr pa; |
967f97fa | 188 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
cee3ca00 | 189 | virtio_stw_phys(vq->vdev, pa, val); |
967f97fa AL |
190 | } |
191 | ||
192 | static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask) | |
193 | { | |
cee3ca00 | 194 | VirtIODevice *vdev = vq->vdev; |
a8170e5e | 195 | hwaddr pa; |
967f97fa | 196 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
cee3ca00 | 197 | virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) | mask); |
967f97fa AL |
198 | } |
199 | ||
200 | static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask) | |
201 | { | |
cee3ca00 | 202 | VirtIODevice *vdev = vq->vdev; |
a8170e5e | 203 | hwaddr pa; |
967f97fa | 204 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
cee3ca00 | 205 | virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) & ~mask); |
967f97fa AL |
206 | } |
207 | ||
e9600c6c | 208 | static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val) |
bcbabae8 | 209 | { |
a8170e5e | 210 | hwaddr pa; |
bcbabae8 MT |
211 | if (!vq->notification) { |
212 | return; | |
213 | } | |
214 | pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]); | |
cee3ca00 | 215 | virtio_stw_phys(vq->vdev, pa, val); |
bcbabae8 MT |
216 | } |
217 | ||
967f97fa AL |
218 | void virtio_queue_set_notification(VirtQueue *vq, int enable) |
219 | { | |
bcbabae8 | 220 | vq->notification = enable; |
ef546f12 | 221 | if (virtio_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { |
e9600c6c | 222 | vring_set_avail_event(vq, vring_avail_idx(vq)); |
bcbabae8 | 223 | } else if (enable) { |
967f97fa | 224 | vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 225 | } else { |
967f97fa | 226 | vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 227 | } |
92045d80 MT |
228 | if (enable) { |
229 | /* Expose avail event/used flags before caller checks the avail idx. */ | |
230 | smp_mb(); | |
231 | } | |
967f97fa AL |
232 | } |
233 | ||
234 | int virtio_queue_ready(VirtQueue *vq) | |
235 | { | |
236 | return vq->vring.avail != 0; | |
237 | } | |
238 | ||
239 | int virtio_queue_empty(VirtQueue *vq) | |
240 | { | |
241 | return vring_avail_idx(vq) == vq->last_avail_idx; | |
242 | } | |
243 | ||
244 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, | |
245 | unsigned int len, unsigned int idx) | |
246 | { | |
247 | unsigned int offset; | |
248 | int i; | |
249 | ||
64979a4d SH |
250 | trace_virtqueue_fill(vq, elem, len, idx); |
251 | ||
967f97fa AL |
252 | offset = 0; |
253 | for (i = 0; i < elem->in_num; i++) { | |
254 | size_t size = MIN(len - offset, elem->in_sg[i].iov_len); | |
255 | ||
26b258e1 AL |
256 | cpu_physical_memory_unmap(elem->in_sg[i].iov_base, |
257 | elem->in_sg[i].iov_len, | |
258 | 1, size); | |
967f97fa | 259 | |
0cea71a2 | 260 | offset += size; |
967f97fa AL |
261 | } |
262 | ||
26b258e1 AL |
263 | for (i = 0; i < elem->out_num; i++) |
264 | cpu_physical_memory_unmap(elem->out_sg[i].iov_base, | |
265 | elem->out_sg[i].iov_len, | |
266 | 0, elem->out_sg[i].iov_len); | |
267 | ||
967f97fa AL |
268 | idx = (idx + vring_used_idx(vq)) % vq->vring.num; |
269 | ||
270 | /* Get a pointer to the next entry in the used ring. */ | |
271 | vring_used_ring_id(vq, idx, elem->index); | |
272 | vring_used_ring_len(vq, idx, len); | |
273 | } | |
274 | ||
275 | void virtqueue_flush(VirtQueue *vq, unsigned int count) | |
276 | { | |
bcbabae8 | 277 | uint16_t old, new; |
967f97fa | 278 | /* Make sure buffer is written before we update index. */ |
b90d2f35 | 279 | smp_wmb(); |
64979a4d | 280 | trace_virtqueue_flush(vq, count); |
bcbabae8 MT |
281 | old = vring_used_idx(vq); |
282 | new = old + count; | |
283 | vring_used_idx_set(vq, new); | |
967f97fa | 284 | vq->inuse -= count; |
bcbabae8 MT |
285 | if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))) |
286 | vq->signalled_used_valid = false; | |
967f97fa AL |
287 | } |
288 | ||
289 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, | |
290 | unsigned int len) | |
291 | { | |
292 | virtqueue_fill(vq, elem, len, 0); | |
293 | virtqueue_flush(vq, 1); | |
294 | } | |
295 | ||
296 | static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx) | |
297 | { | |
298 | uint16_t num_heads = vring_avail_idx(vq) - idx; | |
299 | ||
300 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
bb6834cf | 301 | if (num_heads > vq->vring.num) { |
ce67ed65 SH |
302 | error_report("Guest moved used index from %u to %u", |
303 | idx, vring_avail_idx(vq)); | |
bb6834cf AL |
304 | exit(1); |
305 | } | |
a821ce59 MT |
306 | /* On success, callers read a descriptor at vq->last_avail_idx. |
307 | * Make sure descriptor read does not bypass avail index read. */ | |
308 | if (num_heads) { | |
309 | smp_rmb(); | |
310 | } | |
967f97fa AL |
311 | |
312 | return num_heads; | |
313 | } | |
314 | ||
315 | static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx) | |
316 | { | |
317 | unsigned int head; | |
318 | ||
319 | /* Grab the next descriptor number they're advertising, and increment | |
320 | * the index we've seen. */ | |
321 | head = vring_avail_ring(vq, idx % vq->vring.num); | |
322 | ||
323 | /* If their number is silly, that's a fatal mistake. */ | |
bb6834cf | 324 | if (head >= vq->vring.num) { |
ce67ed65 | 325 | error_report("Guest says index %u is available", head); |
bb6834cf AL |
326 | exit(1); |
327 | } | |
967f97fa AL |
328 | |
329 | return head; | |
330 | } | |
331 | ||
cee3ca00 | 332 | static unsigned virtqueue_next_desc(VirtIODevice *vdev, hwaddr desc_pa, |
5774cf98 | 333 | unsigned int i, unsigned int max) |
967f97fa AL |
334 | { |
335 | unsigned int next; | |
336 | ||
337 | /* If this descriptor says it doesn't chain, we're done. */ | |
cee3ca00 | 338 | if (!(vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_NEXT)) { |
5774cf98 | 339 | return max; |
cee3ca00 | 340 | } |
967f97fa AL |
341 | |
342 | /* Check they're not leading us off end of descriptors. */ | |
cee3ca00 | 343 | next = vring_desc_next(vdev, desc_pa, i); |
967f97fa | 344 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
b90d2f35 | 345 | smp_wmb(); |
967f97fa | 346 | |
5774cf98 | 347 | if (next >= max) { |
ce67ed65 | 348 | error_report("Desc next is %u", next); |
bb6834cf AL |
349 | exit(1); |
350 | } | |
967f97fa AL |
351 | |
352 | return next; | |
353 | } | |
354 | ||
0d8d7690 | 355 | void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, |
e1f7b481 MT |
356 | unsigned int *out_bytes, |
357 | unsigned max_in_bytes, unsigned max_out_bytes) | |
967f97fa | 358 | { |
efeea6d0 | 359 | unsigned int idx; |
385ce95d | 360 | unsigned int total_bufs, in_total, out_total; |
967f97fa AL |
361 | |
362 | idx = vq->last_avail_idx; | |
363 | ||
efeea6d0 | 364 | total_bufs = in_total = out_total = 0; |
967f97fa | 365 | while (virtqueue_num_heads(vq, idx)) { |
cee3ca00 | 366 | VirtIODevice *vdev = vq->vdev; |
efeea6d0 | 367 | unsigned int max, num_bufs, indirect = 0; |
a8170e5e | 368 | hwaddr desc_pa; |
967f97fa AL |
369 | int i; |
370 | ||
efeea6d0 MM |
371 | max = vq->vring.num; |
372 | num_bufs = total_bufs; | |
967f97fa | 373 | i = virtqueue_get_head(vq, idx++); |
efeea6d0 MM |
374 | desc_pa = vq->vring.desc; |
375 | ||
cee3ca00 RR |
376 | if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_INDIRECT) { |
377 | if (vring_desc_len(vdev, desc_pa, i) % sizeof(VRingDesc)) { | |
ce67ed65 | 378 | error_report("Invalid size for indirect buffer table"); |
efeea6d0 MM |
379 | exit(1); |
380 | } | |
381 | ||
382 | /* If we've got too many, that implies a descriptor loop. */ | |
383 | if (num_bufs >= max) { | |
ce67ed65 | 384 | error_report("Looped descriptor"); |
efeea6d0 MM |
385 | exit(1); |
386 | } | |
387 | ||
388 | /* loop over the indirect descriptor table */ | |
389 | indirect = 1; | |
cee3ca00 RR |
390 | max = vring_desc_len(vdev, desc_pa, i) / sizeof(VRingDesc); |
391 | desc_pa = vring_desc_addr(vdev, desc_pa, i); | |
1ae2757c | 392 | num_bufs = i = 0; |
efeea6d0 MM |
393 | } |
394 | ||
967f97fa AL |
395 | do { |
396 | /* If we've got too many, that implies a descriptor loop. */ | |
5774cf98 | 397 | if (++num_bufs > max) { |
ce67ed65 | 398 | error_report("Looped descriptor"); |
bb6834cf AL |
399 | exit(1); |
400 | } | |
967f97fa | 401 | |
cee3ca00 RR |
402 | if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_WRITE) { |
403 | in_total += vring_desc_len(vdev, desc_pa, i); | |
967f97fa | 404 | } else { |
cee3ca00 | 405 | out_total += vring_desc_len(vdev, desc_pa, i); |
967f97fa | 406 | } |
e1f7b481 MT |
407 | if (in_total >= max_in_bytes && out_total >= max_out_bytes) { |
408 | goto done; | |
409 | } | |
cee3ca00 | 410 | } while ((i = virtqueue_next_desc(vdev, desc_pa, i, max)) != max); |
efeea6d0 MM |
411 | |
412 | if (!indirect) | |
413 | total_bufs = num_bufs; | |
414 | else | |
415 | total_bufs++; | |
967f97fa | 416 | } |
e1f7b481 | 417 | done: |
0d8d7690 AS |
418 | if (in_bytes) { |
419 | *in_bytes = in_total; | |
420 | } | |
421 | if (out_bytes) { | |
422 | *out_bytes = out_total; | |
423 | } | |
424 | } | |
967f97fa | 425 | |
0d8d7690 AS |
426 | int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, |
427 | unsigned int out_bytes) | |
428 | { | |
429 | unsigned int in_total, out_total; | |
430 | ||
e1f7b481 MT |
431 | virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes); |
432 | return in_bytes <= in_total && out_bytes <= out_total; | |
967f97fa AL |
433 | } |
434 | ||
a8170e5e | 435 | void virtqueue_map_sg(struct iovec *sg, hwaddr *addr, |
42fb2e07 KW |
436 | size_t num_sg, int is_write) |
437 | { | |
438 | unsigned int i; | |
a8170e5e | 439 | hwaddr len; |
42fb2e07 | 440 | |
93725140 | 441 | if (num_sg > VIRTQUEUE_MAX_SIZE) { |
36cf2a37 MT |
442 | error_report("virtio: map attempt out of bounds: %zd > %d", |
443 | num_sg, VIRTQUEUE_MAX_SIZE); | |
444 | exit(1); | |
445 | } | |
446 | ||
42fb2e07 KW |
447 | for (i = 0; i < num_sg; i++) { |
448 | len = sg[i].iov_len; | |
449 | sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write); | |
450 | if (sg[i].iov_base == NULL || len != sg[i].iov_len) { | |
1a285899 | 451 | error_report("virtio: error trying to map MMIO memory"); |
42fb2e07 KW |
452 | exit(1); |
453 | } | |
454 | } | |
455 | } | |
456 | ||
967f97fa AL |
457 | int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem) |
458 | { | |
5774cf98 | 459 | unsigned int i, head, max; |
a8170e5e | 460 | hwaddr desc_pa = vq->vring.desc; |
cee3ca00 | 461 | VirtIODevice *vdev = vq->vdev; |
967f97fa AL |
462 | |
463 | if (!virtqueue_num_heads(vq, vq->last_avail_idx)) | |
464 | return 0; | |
465 | ||
466 | /* When we start there are none of either input nor output. */ | |
467 | elem->out_num = elem->in_num = 0; | |
468 | ||
5774cf98 MM |
469 | max = vq->vring.num; |
470 | ||
967f97fa | 471 | i = head = virtqueue_get_head(vq, vq->last_avail_idx++); |
ef546f12 | 472 | if (virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
e9600c6c | 473 | vring_set_avail_event(vq, vq->last_avail_idx); |
bcbabae8 | 474 | } |
efeea6d0 | 475 | |
cee3ca00 RR |
476 | if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_INDIRECT) { |
477 | if (vring_desc_len(vdev, desc_pa, i) % sizeof(VRingDesc)) { | |
ce67ed65 | 478 | error_report("Invalid size for indirect buffer table"); |
efeea6d0 MM |
479 | exit(1); |
480 | } | |
481 | ||
482 | /* loop over the indirect descriptor table */ | |
cee3ca00 RR |
483 | max = vring_desc_len(vdev, desc_pa, i) / sizeof(VRingDesc); |
484 | desc_pa = vring_desc_addr(vdev, desc_pa, i); | |
efeea6d0 MM |
485 | i = 0; |
486 | } | |
487 | ||
42fb2e07 | 488 | /* Collect all the descriptors */ |
967f97fa AL |
489 | do { |
490 | struct iovec *sg; | |
491 | ||
cee3ca00 | 492 | if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_WRITE) { |
c8eac1cf MT |
493 | if (elem->in_num >= ARRAY_SIZE(elem->in_sg)) { |
494 | error_report("Too many write descriptors in indirect table"); | |
495 | exit(1); | |
496 | } | |
cee3ca00 | 497 | elem->in_addr[elem->in_num] = vring_desc_addr(vdev, desc_pa, i); |
967f97fa | 498 | sg = &elem->in_sg[elem->in_num++]; |
42fb2e07 | 499 | } else { |
c8eac1cf MT |
500 | if (elem->out_num >= ARRAY_SIZE(elem->out_sg)) { |
501 | error_report("Too many read descriptors in indirect table"); | |
502 | exit(1); | |
503 | } | |
cee3ca00 | 504 | elem->out_addr[elem->out_num] = vring_desc_addr(vdev, desc_pa, i); |
967f97fa | 505 | sg = &elem->out_sg[elem->out_num++]; |
42fb2e07 | 506 | } |
967f97fa | 507 | |
cee3ca00 | 508 | sg->iov_len = vring_desc_len(vdev, desc_pa, i); |
967f97fa AL |
509 | |
510 | /* If we've got too many, that implies a descriptor loop. */ | |
5774cf98 | 511 | if ((elem->in_num + elem->out_num) > max) { |
ce67ed65 | 512 | error_report("Looped descriptor"); |
bb6834cf AL |
513 | exit(1); |
514 | } | |
cee3ca00 | 515 | } while ((i = virtqueue_next_desc(vdev, desc_pa, i, max)) != max); |
967f97fa | 516 | |
42fb2e07 KW |
517 | /* Now map what we have collected */ |
518 | virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1); | |
519 | virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0); | |
520 | ||
967f97fa AL |
521 | elem->index = head; |
522 | ||
523 | vq->inuse++; | |
524 | ||
64979a4d | 525 | trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); |
967f97fa AL |
526 | return elem->in_num + elem->out_num; |
527 | } | |
528 | ||
529 | /* virtio device */ | |
7055e687 MT |
530 | static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector) |
531 | { | |
1c819449 FK |
532 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
533 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
534 | ||
535 | if (k->notify) { | |
536 | k->notify(qbus->parent, vector); | |
7055e687 MT |
537 | } |
538 | } | |
967f97fa | 539 | |
53c25cea | 540 | void virtio_update_irq(VirtIODevice *vdev) |
967f97fa | 541 | { |
7055e687 | 542 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
967f97fa AL |
543 | } |
544 | ||
4e1837f8 SH |
545 | void virtio_set_status(VirtIODevice *vdev, uint8_t val) |
546 | { | |
181103cd | 547 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
4e1837f8 SH |
548 | trace_virtio_set_status(vdev, val); |
549 | ||
181103cd FK |
550 | if (k->set_status) { |
551 | k->set_status(vdev, val); | |
4e1837f8 SH |
552 | } |
553 | vdev->status = val; | |
554 | } | |
555 | ||
616a6552 GK |
556 | bool target_words_bigendian(void); |
557 | static enum virtio_device_endian virtio_default_endian(void) | |
558 | { | |
559 | if (target_words_bigendian()) { | |
560 | return VIRTIO_DEVICE_ENDIAN_BIG; | |
561 | } else { | |
562 | return VIRTIO_DEVICE_ENDIAN_LITTLE; | |
563 | } | |
564 | } | |
565 | ||
566 | static enum virtio_device_endian virtio_current_cpu_endian(void) | |
567 | { | |
568 | CPUClass *cc = CPU_GET_CLASS(current_cpu); | |
569 | ||
570 | if (cc->virtio_is_big_endian(current_cpu)) { | |
571 | return VIRTIO_DEVICE_ENDIAN_BIG; | |
572 | } else { | |
573 | return VIRTIO_DEVICE_ENDIAN_LITTLE; | |
574 | } | |
575 | } | |
576 | ||
53c25cea | 577 | void virtio_reset(void *opaque) |
967f97fa AL |
578 | { |
579 | VirtIODevice *vdev = opaque; | |
181103cd | 580 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
581 | int i; |
582 | ||
e0c472d8 | 583 | virtio_set_status(vdev, 0); |
616a6552 GK |
584 | if (current_cpu) { |
585 | /* Guest initiated reset */ | |
586 | vdev->device_endian = virtio_current_cpu_endian(); | |
587 | } else { | |
588 | /* System reset */ | |
589 | vdev->device_endian = virtio_default_endian(); | |
590 | } | |
e0c472d8 | 591 | |
181103cd FK |
592 | if (k->reset) { |
593 | k->reset(vdev); | |
594 | } | |
967f97fa | 595 | |
704a76fc | 596 | vdev->guest_features = 0; |
967f97fa AL |
597 | vdev->queue_sel = 0; |
598 | vdev->status = 0; | |
599 | vdev->isr = 0; | |
7055e687 MT |
600 | vdev->config_vector = VIRTIO_NO_VECTOR; |
601 | virtio_notify_vector(vdev, vdev->config_vector); | |
967f97fa AL |
602 | |
603 | for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
604 | vdev->vq[i].vring.desc = 0; | |
605 | vdev->vq[i].vring.avail = 0; | |
606 | vdev->vq[i].vring.used = 0; | |
607 | vdev->vq[i].last_avail_idx = 0; | |
53c25cea | 608 | vdev->vq[i].pa = 0; |
e0d686bf | 609 | virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR); |
bcbabae8 MT |
610 | vdev->vq[i].signalled_used = 0; |
611 | vdev->vq[i].signalled_used_valid = false; | |
612 | vdev->vq[i].notification = true; | |
967f97fa AL |
613 | } |
614 | } | |
615 | ||
53c25cea | 616 | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 617 | { |
181103cd | 618 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
619 | uint8_t val; |
620 | ||
5f5a1318 | 621 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 622 | return (uint32_t)-1; |
5f5a1318 JW |
623 | } |
624 | ||
625 | k->get_config(vdev, vdev->config); | |
967f97fa | 626 | |
06dbfc6f | 627 | val = ldub_p(vdev->config + addr); |
967f97fa AL |
628 | return val; |
629 | } | |
630 | ||
53c25cea | 631 | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 632 | { |
181103cd | 633 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
634 | uint16_t val; |
635 | ||
5f5a1318 | 636 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 637 | return (uint32_t)-1; |
5f5a1318 JW |
638 | } |
639 | ||
640 | k->get_config(vdev, vdev->config); | |
967f97fa | 641 | |
06dbfc6f | 642 | val = lduw_p(vdev->config + addr); |
967f97fa AL |
643 | return val; |
644 | } | |
645 | ||
53c25cea | 646 | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 647 | { |
181103cd | 648 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
649 | uint32_t val; |
650 | ||
5f5a1318 | 651 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 652 | return (uint32_t)-1; |
5f5a1318 JW |
653 | } |
654 | ||
655 | k->get_config(vdev, vdev->config); | |
967f97fa | 656 | |
06dbfc6f | 657 | val = ldl_p(vdev->config + addr); |
967f97fa AL |
658 | return val; |
659 | } | |
660 | ||
53c25cea | 661 | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 662 | { |
181103cd | 663 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
664 | uint8_t val = data; |
665 | ||
5f5a1318 | 666 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 667 | return; |
5f5a1318 | 668 | } |
967f97fa | 669 | |
06dbfc6f | 670 | stb_p(vdev->config + addr, val); |
967f97fa | 671 | |
181103cd FK |
672 | if (k->set_config) { |
673 | k->set_config(vdev, vdev->config); | |
674 | } | |
967f97fa AL |
675 | } |
676 | ||
53c25cea | 677 | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 678 | { |
181103cd | 679 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
680 | uint16_t val = data; |
681 | ||
5f5a1318 | 682 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 683 | return; |
5f5a1318 | 684 | } |
967f97fa | 685 | |
06dbfc6f | 686 | stw_p(vdev->config + addr, val); |
967f97fa | 687 | |
181103cd FK |
688 | if (k->set_config) { |
689 | k->set_config(vdev, vdev->config); | |
690 | } | |
967f97fa AL |
691 | } |
692 | ||
53c25cea | 693 | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 694 | { |
181103cd | 695 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
696 | uint32_t val = data; |
697 | ||
5f5a1318 | 698 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 699 | return; |
5f5a1318 | 700 | } |
967f97fa | 701 | |
06dbfc6f | 702 | stl_p(vdev->config + addr, val); |
967f97fa | 703 | |
181103cd FK |
704 | if (k->set_config) { |
705 | k->set_config(vdev, vdev->config); | |
706 | } | |
967f97fa AL |
707 | } |
708 | ||
a8170e5e | 709 | void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr) |
967f97fa | 710 | { |
7055e687 MT |
711 | vdev->vq[n].pa = addr; |
712 | virtqueue_init(&vdev->vq[n]); | |
53c25cea PB |
713 | } |
714 | ||
a8170e5e | 715 | hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) |
53c25cea PB |
716 | { |
717 | return vdev->vq[n].pa; | |
718 | } | |
719 | ||
e63c0ba1 PM |
720 | void virtio_queue_set_num(VirtIODevice *vdev, int n, int num) |
721 | { | |
f6049f44 PM |
722 | /* Don't allow guest to flip queue between existent and |
723 | * nonexistent states, or to set it to an invalid size. | |
724 | */ | |
725 | if (!!num != !!vdev->vq[n].vring.num || | |
726 | num > VIRTQUEUE_MAX_SIZE || | |
727 | num < 0) { | |
728 | return; | |
e63c0ba1 | 729 | } |
f6049f44 PM |
730 | vdev->vq[n].vring.num = num; |
731 | virtqueue_init(&vdev->vq[n]); | |
e63c0ba1 PM |
732 | } |
733 | ||
e0d686bf JW |
734 | VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector) |
735 | { | |
736 | return QLIST_FIRST(&vdev->vector_queues[vector]); | |
737 | } | |
738 | ||
739 | VirtQueue *virtio_vector_next_queue(VirtQueue *vq) | |
740 | { | |
741 | return QLIST_NEXT(vq, node); | |
742 | } | |
743 | ||
53c25cea PB |
744 | int virtio_queue_get_num(VirtIODevice *vdev, int n) |
745 | { | |
746 | return vdev->vq[n].vring.num; | |
747 | } | |
967f97fa | 748 | |
c80decdb PB |
749 | int virtio_queue_get_id(VirtQueue *vq) |
750 | { | |
751 | VirtIODevice *vdev = vq->vdev; | |
752 | assert(vq >= &vdev->vq[0] && vq < &vdev->vq[VIRTIO_PCI_QUEUE_MAX]); | |
753 | return vq - &vdev->vq[0]; | |
754 | } | |
755 | ||
6ce69d1c PM |
756 | void virtio_queue_set_align(VirtIODevice *vdev, int n, int align) |
757 | { | |
758 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
759 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
760 | ||
761 | /* Check that the transport told us it was going to do this | |
762 | * (so a buggy transport will immediately assert rather than | |
763 | * silently failing to migrate this state) | |
764 | */ | |
765 | assert(k->has_variable_vring_alignment); | |
766 | ||
767 | vdev->vq[n].vring.align = align; | |
768 | virtqueue_init(&vdev->vq[n]); | |
769 | } | |
770 | ||
25db9ebe SH |
771 | void virtio_queue_notify_vq(VirtQueue *vq) |
772 | { | |
9e0f5b81 | 773 | if (vq->vring.desc && vq->handle_output) { |
25db9ebe | 774 | VirtIODevice *vdev = vq->vdev; |
9e0f5b81 | 775 | |
25db9ebe SH |
776 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); |
777 | vq->handle_output(vdev, vq); | |
778 | } | |
779 | } | |
780 | ||
53c25cea PB |
781 | void virtio_queue_notify(VirtIODevice *vdev, int n) |
782 | { | |
7157e2e2 | 783 | virtio_queue_notify_vq(&vdev->vq[n]); |
967f97fa AL |
784 | } |
785 | ||
7055e687 MT |
786 | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n) |
787 | { | |
788 | return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector : | |
789 | VIRTIO_NO_VECTOR; | |
790 | } | |
791 | ||
792 | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector) | |
793 | { | |
e0d686bf JW |
794 | VirtQueue *vq = &vdev->vq[n]; |
795 | ||
796 | if (n < VIRTIO_PCI_QUEUE_MAX) { | |
797 | if (vdev->vector_queues && | |
798 | vdev->vq[n].vector != VIRTIO_NO_VECTOR) { | |
799 | QLIST_REMOVE(vq, node); | |
800 | } | |
7055e687 | 801 | vdev->vq[n].vector = vector; |
e0d686bf JW |
802 | if (vdev->vector_queues && |
803 | vector != VIRTIO_NO_VECTOR) { | |
804 | QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node); | |
805 | } | |
806 | } | |
7055e687 MT |
807 | } |
808 | ||
967f97fa AL |
809 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, |
810 | void (*handle_output)(VirtIODevice *, VirtQueue *)) | |
811 | { | |
812 | int i; | |
813 | ||
814 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
815 | if (vdev->vq[i].vring.num == 0) | |
816 | break; | |
817 | } | |
818 | ||
819 | if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE) | |
820 | abort(); | |
821 | ||
822 | vdev->vq[i].vring.num = queue_size; | |
6ce69d1c | 823 | vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN; |
967f97fa AL |
824 | vdev->vq[i].handle_output = handle_output; |
825 | ||
826 | return &vdev->vq[i]; | |
827 | } | |
828 | ||
f23fd811 JW |
829 | void virtio_del_queue(VirtIODevice *vdev, int n) |
830 | { | |
831 | if (n < 0 || n >= VIRTIO_PCI_QUEUE_MAX) { | |
832 | abort(); | |
833 | } | |
834 | ||
835 | vdev->vq[n].vring.num = 0; | |
836 | } | |
837 | ||
1cbdabe2 MT |
838 | void virtio_irq(VirtQueue *vq) |
839 | { | |
64979a4d | 840 | trace_virtio_irq(vq); |
1cbdabe2 MT |
841 | vq->vdev->isr |= 0x01; |
842 | virtio_notify_vector(vq->vdev, vq->vector); | |
843 | } | |
844 | ||
bcbabae8 MT |
845 | static bool vring_notify(VirtIODevice *vdev, VirtQueue *vq) |
846 | { | |
847 | uint16_t old, new; | |
848 | bool v; | |
a281ebc1 MT |
849 | /* We need to expose used array entries before checking used event. */ |
850 | smp_mb(); | |
97b83deb | 851 | /* Always notify when queue is empty (when feature acknowledge) */ |
ef546f12 CH |
852 | if (virtio_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) && |
853 | !vq->inuse && vring_avail_idx(vq) == vq->last_avail_idx) { | |
bcbabae8 MT |
854 | return true; |
855 | } | |
856 | ||
ef546f12 | 857 | if (!virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
bcbabae8 MT |
858 | return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT); |
859 | } | |
860 | ||
861 | v = vq->signalled_used_valid; | |
862 | vq->signalled_used_valid = true; | |
863 | old = vq->signalled_used; | |
864 | new = vq->signalled_used = vring_used_idx(vq); | |
e9600c6c | 865 | return !v || vring_need_event(vring_get_used_event(vq), new, old); |
bcbabae8 MT |
866 | } |
867 | ||
868 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) | |
869 | { | |
870 | if (!vring_notify(vdev, vq)) { | |
967f97fa | 871 | return; |
bcbabae8 | 872 | } |
967f97fa | 873 | |
64979a4d | 874 | trace_virtio_notify(vdev, vq); |
967f97fa | 875 | vdev->isr |= 0x01; |
7055e687 | 876 | virtio_notify_vector(vdev, vq->vector); |
967f97fa AL |
877 | } |
878 | ||
879 | void virtio_notify_config(VirtIODevice *vdev) | |
880 | { | |
7625162c AL |
881 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) |
882 | return; | |
883 | ||
967f97fa | 884 | vdev->isr |= 0x03; |
7055e687 | 885 | virtio_notify_vector(vdev, vdev->config_vector); |
967f97fa AL |
886 | } |
887 | ||
616a6552 GK |
888 | static bool virtio_device_endian_needed(void *opaque) |
889 | { | |
890 | VirtIODevice *vdev = opaque; | |
891 | ||
892 | assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN); | |
893 | return vdev->device_endian != virtio_default_endian(); | |
894 | } | |
895 | ||
896 | static const VMStateDescription vmstate_virtio_device_endian = { | |
897 | .name = "virtio/device_endian", | |
898 | .version_id = 1, | |
899 | .minimum_version_id = 1, | |
900 | .fields = (VMStateField[]) { | |
901 | VMSTATE_UINT8(device_endian, VirtIODevice), | |
902 | VMSTATE_END_OF_LIST() | |
903 | } | |
904 | }; | |
905 | ||
6b321a3d GK |
906 | static const VMStateDescription vmstate_virtio = { |
907 | .name = "virtio", | |
908 | .version_id = 1, | |
909 | .minimum_version_id = 1, | |
910 | .minimum_version_id_old = 1, | |
911 | .fields = (VMStateField[]) { | |
912 | VMSTATE_END_OF_LIST() | |
616a6552 GK |
913 | }, |
914 | .subsections = (VMStateSubsection[]) { | |
915 | { | |
916 | .vmsd = &vmstate_virtio_device_endian, | |
917 | .needed = &virtio_device_endian_needed | |
918 | }, | |
919 | { 0 } | |
6b321a3d GK |
920 | } |
921 | }; | |
922 | ||
967f97fa AL |
923 | void virtio_save(VirtIODevice *vdev, QEMUFile *f) |
924 | { | |
1c819449 FK |
925 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
926 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1b5fc0de | 927 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
928 | int i; |
929 | ||
1c819449 FK |
930 | if (k->save_config) { |
931 | k->save_config(qbus->parent, f); | |
932 | } | |
967f97fa | 933 | |
967f97fa AL |
934 | qemu_put_8s(f, &vdev->status); |
935 | qemu_put_8s(f, &vdev->isr); | |
936 | qemu_put_be16s(f, &vdev->queue_sel); | |
704a76fc | 937 | qemu_put_be32s(f, &vdev->guest_features); |
967f97fa AL |
938 | qemu_put_be32(f, vdev->config_len); |
939 | qemu_put_buffer(f, vdev->config, vdev->config_len); | |
940 | ||
941 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
942 | if (vdev->vq[i].vring.num == 0) | |
943 | break; | |
944 | } | |
945 | ||
946 | qemu_put_be32(f, i); | |
947 | ||
948 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
949 | if (vdev->vq[i].vring.num == 0) | |
950 | break; | |
951 | ||
952 | qemu_put_be32(f, vdev->vq[i].vring.num); | |
6ce69d1c PM |
953 | if (k->has_variable_vring_alignment) { |
954 | qemu_put_be32(f, vdev->vq[i].vring.align); | |
955 | } | |
53c25cea | 956 | qemu_put_be64(f, vdev->vq[i].pa); |
967f97fa | 957 | qemu_put_be16s(f, &vdev->vq[i].last_avail_idx); |
1c819449 FK |
958 | if (k->save_queue) { |
959 | k->save_queue(qbus->parent, i, f); | |
960 | } | |
967f97fa | 961 | } |
1b5fc0de GK |
962 | |
963 | if (vdc->save != NULL) { | |
964 | vdc->save(vdev, f); | |
965 | } | |
6b321a3d GK |
966 | |
967 | /* Subsections */ | |
8118f095 | 968 | vmstate_save_state(f, &vmstate_virtio, vdev, NULL); |
967f97fa AL |
969 | } |
970 | ||
ad0c9332 PB |
971 | int virtio_set_features(VirtIODevice *vdev, uint32_t val) |
972 | { | |
1c819449 FK |
973 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
974 | VirtioBusClass *vbusk = VIRTIO_BUS_GET_CLASS(qbus); | |
181103cd | 975 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
1c819449 | 976 | uint32_t supported_features = vbusk->get_features(qbus->parent); |
ad0c9332 PB |
977 | bool bad = (val & ~supported_features) != 0; |
978 | ||
979 | val &= supported_features; | |
181103cd FK |
980 | if (k->set_features) { |
981 | k->set_features(vdev, val); | |
ad0c9332 PB |
982 | } |
983 | vdev->guest_features = val; | |
984 | return bad ? -1 : 0; | |
985 | } | |
986 | ||
1b5fc0de | 987 | int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) |
967f97fa | 988 | { |
cc459952 | 989 | int i, ret; |
a890a2f9 | 990 | int32_t config_len; |
cc459952 | 991 | uint32_t num; |
6d74ca5a | 992 | uint32_t features; |
ad0c9332 | 993 | uint32_t supported_features; |
1c819449 FK |
994 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
995 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1b5fc0de | 996 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa | 997 | |
616a6552 GK |
998 | /* |
999 | * We poison the endianness to ensure it does not get used before | |
1000 | * subsections have been loaded. | |
1001 | */ | |
1002 | vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN; | |
1003 | ||
1c819449 FK |
1004 | if (k->load_config) { |
1005 | ret = k->load_config(qbus->parent, f); | |
ff24bd58 MT |
1006 | if (ret) |
1007 | return ret; | |
1008 | } | |
967f97fa | 1009 | |
967f97fa AL |
1010 | qemu_get_8s(f, &vdev->status); |
1011 | qemu_get_8s(f, &vdev->isr); | |
1012 | qemu_get_be16s(f, &vdev->queue_sel); | |
4b53c2c7 MR |
1013 | if (vdev->queue_sel >= VIRTIO_PCI_QUEUE_MAX) { |
1014 | return -1; | |
1015 | } | |
6d74ca5a | 1016 | qemu_get_be32s(f, &features); |
ad0c9332 PB |
1017 | |
1018 | if (virtio_set_features(vdev, features) < 0) { | |
1c819449 | 1019 | supported_features = k->get_features(qbus->parent); |
ce67ed65 SH |
1020 | error_report("Features 0x%x unsupported. Allowed features: 0x%x", |
1021 | features, supported_features); | |
6d74ca5a MT |
1022 | return -1; |
1023 | } | |
a890a2f9 | 1024 | config_len = qemu_get_be32(f); |
2f5732e9 DDAG |
1025 | |
1026 | /* | |
1027 | * There are cases where the incoming config can be bigger or smaller | |
1028 | * than what we have; so load what we have space for, and skip | |
1029 | * any excess that's in the stream. | |
1030 | */ | |
1031 | qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len)); | |
1032 | ||
1033 | while (config_len > vdev->config_len) { | |
1034 | qemu_get_byte(f); | |
1035 | config_len--; | |
a890a2f9 | 1036 | } |
967f97fa AL |
1037 | |
1038 | num = qemu_get_be32(f); | |
1039 | ||
cc459952 MT |
1040 | if (num > VIRTIO_PCI_QUEUE_MAX) { |
1041 | error_report("Invalid number of PCI queues: 0x%x", num); | |
1042 | return -1; | |
1043 | } | |
1044 | ||
967f97fa AL |
1045 | for (i = 0; i < num; i++) { |
1046 | vdev->vq[i].vring.num = qemu_get_be32(f); | |
6ce69d1c PM |
1047 | if (k->has_variable_vring_alignment) { |
1048 | vdev->vq[i].vring.align = qemu_get_be32(f); | |
1049 | } | |
53c25cea | 1050 | vdev->vq[i].pa = qemu_get_be64(f); |
967f97fa | 1051 | qemu_get_be16s(f, &vdev->vq[i].last_avail_idx); |
bcbabae8 MT |
1052 | vdev->vq[i].signalled_used_valid = false; |
1053 | vdev->vq[i].notification = true; | |
967f97fa | 1054 | |
53c25cea PB |
1055 | if (vdev->vq[i].pa) { |
1056 | virtqueue_init(&vdev->vq[i]); | |
1abeb5a6 MT |
1057 | } else if (vdev->vq[i].last_avail_idx) { |
1058 | error_report("VQ %d address 0x0 " | |
6daf194d | 1059 | "inconsistent with Host index 0x%x", |
1abeb5a6 MT |
1060 | i, vdev->vq[i].last_avail_idx); |
1061 | return -1; | |
258dc7c9 | 1062 | } |
1c819449 FK |
1063 | if (k->load_queue) { |
1064 | ret = k->load_queue(qbus->parent, i, f); | |
ff24bd58 MT |
1065 | if (ret) |
1066 | return ret; | |
7055e687 | 1067 | } |
967f97fa AL |
1068 | } |
1069 | ||
7055e687 | 1070 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
1b5fc0de GK |
1071 | |
1072 | if (vdc->load != NULL) { | |
6b321a3d GK |
1073 | ret = vdc->load(vdev, f, version_id); |
1074 | if (ret) { | |
1075 | return ret; | |
1076 | } | |
1b5fc0de GK |
1077 | } |
1078 | ||
616a6552 GK |
1079 | /* Subsections */ |
1080 | ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1); | |
1081 | if (ret) { | |
1082 | return ret; | |
1083 | } | |
1084 | ||
1085 | if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) { | |
1086 | vdev->device_endian = virtio_default_endian(); | |
1087 | } | |
1088 | ||
1089 | for (i = 0; i < num; i++) { | |
1090 | if (vdev->vq[i].pa) { | |
1091 | uint16_t nheads; | |
1092 | nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx; | |
1093 | /* Check it isn't doing strange things with descriptor numbers. */ | |
1094 | if (nheads > vdev->vq[i].vring.num) { | |
1095 | error_report("VQ %d size 0x%x Guest index 0x%x " | |
1096 | "inconsistent with Host index 0x%x: delta 0x%x", | |
1097 | i, vdev->vq[i].vring.num, | |
1098 | vring_avail_idx(&vdev->vq[i]), | |
1099 | vdev->vq[i].last_avail_idx, nheads); | |
1100 | return -1; | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | return 0; | |
967f97fa AL |
1106 | } |
1107 | ||
6a1a8cc7 | 1108 | void virtio_cleanup(VirtIODevice *vdev) |
b946a153 | 1109 | { |
85cf2a8d | 1110 | qemu_del_vm_change_state_handler(vdev->vmstate); |
6f79e06b | 1111 | g_free(vdev->config); |
7267c094 | 1112 | g_free(vdev->vq); |
e0d686bf | 1113 | g_free(vdev->vector_queues); |
8e05db92 FK |
1114 | } |
1115 | ||
1dfb4dd9 | 1116 | static void virtio_vmstate_change(void *opaque, int running, RunState state) |
85cf2a8d MT |
1117 | { |
1118 | VirtIODevice *vdev = opaque; | |
1c819449 FK |
1119 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1120 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
85cf2a8d | 1121 | bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK); |
9e8e8c48 | 1122 | vdev->vm_running = running; |
85cf2a8d MT |
1123 | |
1124 | if (backend_run) { | |
1125 | virtio_set_status(vdev, vdev->status); | |
1126 | } | |
1127 | ||
1c819449 FK |
1128 | if (k->vmstate_change) { |
1129 | k->vmstate_change(qbus->parent, backend_run); | |
85cf2a8d MT |
1130 | } |
1131 | ||
1132 | if (!backend_run) { | |
1133 | virtio_set_status(vdev, vdev->status); | |
1134 | } | |
1135 | } | |
1136 | ||
c8075caf GA |
1137 | void virtio_instance_init_common(Object *proxy_obj, void *data, |
1138 | size_t vdev_size, const char *vdev_name) | |
1139 | { | |
1140 | DeviceState *vdev = data; | |
1141 | ||
1142 | object_initialize(vdev, vdev_size, vdev_name); | |
1143 | object_property_add_child(proxy_obj, "virtio-backend", OBJECT(vdev), NULL); | |
1144 | object_unref(OBJECT(vdev)); | |
1145 | qdev_alias_all_properties(vdev, proxy_obj); | |
1146 | } | |
1147 | ||
8e05db92 FK |
1148 | void virtio_init(VirtIODevice *vdev, const char *name, |
1149 | uint16_t device_id, size_t config_size) | |
967f97fa | 1150 | { |
e0d686bf JW |
1151 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1152 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
b8193adb | 1153 | int i; |
e0d686bf JW |
1154 | int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0; |
1155 | ||
1156 | if (nvectors) { | |
1157 | vdev->vector_queues = | |
1158 | g_malloc0(sizeof(*vdev->vector_queues) * nvectors); | |
1159 | } | |
1160 | ||
53c25cea | 1161 | vdev->device_id = device_id; |
967f97fa AL |
1162 | vdev->status = 0; |
1163 | vdev->isr = 0; | |
1164 | vdev->queue_sel = 0; | |
7055e687 | 1165 | vdev->config_vector = VIRTIO_NO_VECTOR; |
7267c094 | 1166 | vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX); |
1354869c | 1167 | vdev->vm_running = runstate_is_running(); |
8e05db92 | 1168 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { |
b8193adb | 1169 | vdev->vq[i].vector = VIRTIO_NO_VECTOR; |
1cbdabe2 | 1170 | vdev->vq[i].vdev = vdev; |
e78a2b42 | 1171 | vdev->vq[i].queue_index = i; |
1cbdabe2 | 1172 | } |
967f97fa | 1173 | |
967f97fa AL |
1174 | vdev->name = name; |
1175 | vdev->config_len = config_size; | |
8e05db92 | 1176 | if (vdev->config_len) { |
7267c094 | 1177 | vdev->config = g_malloc0(config_size); |
8e05db92 | 1178 | } else { |
967f97fa | 1179 | vdev->config = NULL; |
8e05db92 FK |
1180 | } |
1181 | vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change, | |
1182 | vdev); | |
616a6552 | 1183 | vdev->device_endian = virtio_default_endian(); |
8e05db92 | 1184 | } |
967f97fa | 1185 | |
a8170e5e | 1186 | hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1187 | { |
1188 | return vdev->vq[n].vring.desc; | |
1189 | } | |
1190 | ||
a8170e5e | 1191 | hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1192 | { |
1193 | return vdev->vq[n].vring.avail; | |
1194 | } | |
1195 | ||
a8170e5e | 1196 | hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1197 | { |
1198 | return vdev->vq[n].vring.used; | |
1199 | } | |
1200 | ||
a8170e5e | 1201 | hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1202 | { |
1203 | return vdev->vq[n].vring.desc; | |
1204 | } | |
1205 | ||
a8170e5e | 1206 | hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1207 | { |
1208 | return sizeof(VRingDesc) * vdev->vq[n].vring.num; | |
1209 | } | |
1210 | ||
a8170e5e | 1211 | hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1212 | { |
1213 | return offsetof(VRingAvail, ring) + | |
2b3af999 | 1214 | sizeof(uint64_t) * vdev->vq[n].vring.num; |
1cbdabe2 MT |
1215 | } |
1216 | ||
a8170e5e | 1217 | hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1218 | { |
1219 | return offsetof(VRingUsed, ring) + | |
1220 | sizeof(VRingUsedElem) * vdev->vq[n].vring.num; | |
1221 | } | |
1222 | ||
a8170e5e | 1223 | hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1224 | { |
1225 | return vdev->vq[n].vring.used - vdev->vq[n].vring.desc + | |
1226 | virtio_queue_get_used_size(vdev, n); | |
1227 | } | |
1228 | ||
1229 | uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) | |
1230 | { | |
1231 | return vdev->vq[n].last_avail_idx; | |
1232 | } | |
1233 | ||
1234 | void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx) | |
1235 | { | |
1236 | vdev->vq[n].last_avail_idx = idx; | |
1237 | } | |
1238 | ||
6793dfd1 SH |
1239 | void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n) |
1240 | { | |
1241 | vdev->vq[n].signalled_used_valid = false; | |
1242 | } | |
1243 | ||
1cbdabe2 MT |
1244 | VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n) |
1245 | { | |
1246 | return vdev->vq + n; | |
1247 | } | |
1248 | ||
e78a2b42 JW |
1249 | uint16_t virtio_get_queue_index(VirtQueue *vq) |
1250 | { | |
1251 | return vq->queue_index; | |
1252 | } | |
1253 | ||
15b2bd18 PB |
1254 | static void virtio_queue_guest_notifier_read(EventNotifier *n) |
1255 | { | |
1256 | VirtQueue *vq = container_of(n, VirtQueue, guest_notifier); | |
1257 | if (event_notifier_test_and_clear(n)) { | |
1258 | virtio_irq(vq); | |
1259 | } | |
1260 | } | |
1261 | ||
1262 | void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, | |
1263 | bool with_irqfd) | |
1264 | { | |
1265 | if (assign && !with_irqfd) { | |
1266 | event_notifier_set_handler(&vq->guest_notifier, | |
1267 | virtio_queue_guest_notifier_read); | |
1268 | } else { | |
1269 | event_notifier_set_handler(&vq->guest_notifier, NULL); | |
1270 | } | |
1271 | if (!assign) { | |
1272 | /* Test and clear notifier before closing it, | |
1273 | * in case poll callback didn't have time to run. */ | |
1274 | virtio_queue_guest_notifier_read(&vq->guest_notifier); | |
1275 | } | |
1276 | } | |
1277 | ||
1cbdabe2 MT |
1278 | EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq) |
1279 | { | |
1280 | return &vq->guest_notifier; | |
1281 | } | |
b1f416aa PB |
1282 | |
1283 | static void virtio_queue_host_notifier_read(EventNotifier *n) | |
1284 | { | |
1285 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
1286 | if (event_notifier_test_and_clear(n)) { | |
1287 | virtio_queue_notify_vq(vq); | |
1288 | } | |
1289 | } | |
1290 | ||
26b9b5fe PB |
1291 | void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign, |
1292 | bool set_handler) | |
b1f416aa | 1293 | { |
26b9b5fe | 1294 | if (assign && set_handler) { |
b1f416aa PB |
1295 | event_notifier_set_handler(&vq->host_notifier, |
1296 | virtio_queue_host_notifier_read); | |
1297 | } else { | |
1298 | event_notifier_set_handler(&vq->host_notifier, NULL); | |
26b9b5fe PB |
1299 | } |
1300 | if (!assign) { | |
b1f416aa PB |
1301 | /* Test and clear notifier before after disabling event, |
1302 | * in case poll callback didn't have time to run. */ | |
1303 | virtio_queue_host_notifier_read(&vq->host_notifier); | |
1304 | } | |
1305 | } | |
1306 | ||
1cbdabe2 MT |
1307 | EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq) |
1308 | { | |
1309 | return &vq->host_notifier; | |
1310 | } | |
8e05db92 | 1311 | |
1034e9cf FK |
1312 | void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name) |
1313 | { | |
9e288406 | 1314 | g_free(vdev->bus_name); |
80e0090a | 1315 | vdev->bus_name = g_strdup(bus_name); |
1034e9cf FK |
1316 | } |
1317 | ||
1d244b42 AF |
1318 | static void virtio_device_realize(DeviceState *dev, Error **errp) |
1319 | { | |
1320 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
1321 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); | |
1322 | Error *err = NULL; | |
1323 | ||
1d244b42 AF |
1324 | if (vdc->realize != NULL) { |
1325 | vdc->realize(dev, &err); | |
1326 | if (err != NULL) { | |
1327 | error_propagate(errp, err); | |
1328 | return; | |
1329 | } | |
8e05db92 | 1330 | } |
5e96f5d2 | 1331 | virtio_bus_device_plugged(vdev); |
8e05db92 FK |
1332 | } |
1333 | ||
1d244b42 | 1334 | static void virtio_device_unrealize(DeviceState *dev, Error **errp) |
1034e9cf | 1335 | { |
1d244b42 | 1336 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
306ec6c3 AF |
1337 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); |
1338 | Error *err = NULL; | |
1d244b42 | 1339 | |
83d07047 PB |
1340 | virtio_bus_device_unplugged(vdev); |
1341 | ||
306ec6c3 AF |
1342 | if (vdc->unrealize != NULL) { |
1343 | vdc->unrealize(dev, &err); | |
1344 | if (err != NULL) { | |
1345 | error_propagate(errp, err); | |
1346 | return; | |
1347 | } | |
5e96f5d2 | 1348 | } |
1d244b42 | 1349 | |
9e288406 MA |
1350 | g_free(vdev->bus_name); |
1351 | vdev->bus_name = NULL; | |
1034e9cf FK |
1352 | } |
1353 | ||
8e05db92 FK |
1354 | static void virtio_device_class_init(ObjectClass *klass, void *data) |
1355 | { | |
1356 | /* Set the default value here. */ | |
1357 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1d244b42 AF |
1358 | |
1359 | dc->realize = virtio_device_realize; | |
1360 | dc->unrealize = virtio_device_unrealize; | |
8e05db92 FK |
1361 | dc->bus_type = TYPE_VIRTIO_BUS; |
1362 | } | |
1363 | ||
1364 | static const TypeInfo virtio_device_info = { | |
1365 | .name = TYPE_VIRTIO_DEVICE, | |
1366 | .parent = TYPE_DEVICE, | |
1367 | .instance_size = sizeof(VirtIODevice), | |
1368 | .class_init = virtio_device_class_init, | |
1369 | .abstract = true, | |
1370 | .class_size = sizeof(VirtioDeviceClass), | |
1371 | }; | |
1372 | ||
1373 | static void virtio_register_types(void) | |
1374 | { | |
1375 | type_register_static(&virtio_device_info); | |
1376 | } | |
1377 | ||
1378 | type_init(virtio_register_types) |