]>
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 | ||
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; | |
46c5d082 | 63 | unsigned int num_default; |
6ce69d1c | 64 | unsigned int align; |
a8170e5e AK |
65 | hwaddr desc; |
66 | hwaddr avail; | |
67 | hwaddr used; | |
967f97fa AL |
68 | } VRing; |
69 | ||
70 | struct VirtQueue | |
71 | { | |
72 | VRing vring; | |
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 */ |
ab223c95 | 96 | void virtio_queue_update_rings(VirtIODevice *vdev, int n) |
967f97fa | 97 | { |
ab223c95 | 98 | VRing *vring = &vdev->vq[n].vring; |
53c25cea | 99 | |
ab223c95 CH |
100 | if (!vring->desc) { |
101 | /* not yet setup -> nothing to do */ | |
102 | return; | |
103 | } | |
104 | vring->avail = vring->desc + vring->num * sizeof(VRingDesc); | |
105 | vring->used = vring_align(vring->avail + | |
106 | offsetof(VRingAvail, ring[vring->num]), | |
107 | vring->align); | |
967f97fa AL |
108 | } |
109 | ||
cee3ca00 RR |
110 | static inline uint64_t vring_desc_addr(VirtIODevice *vdev, hwaddr desc_pa, |
111 | int i) | |
967f97fa | 112 | { |
a8170e5e | 113 | hwaddr pa; |
5774cf98 | 114 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr); |
cee3ca00 | 115 | return virtio_ldq_phys(vdev, pa); |
967f97fa AL |
116 | } |
117 | ||
cee3ca00 | 118 | static inline uint32_t vring_desc_len(VirtIODevice *vdev, hwaddr desc_pa, int i) |
967f97fa | 119 | { |
a8170e5e | 120 | hwaddr pa; |
5774cf98 | 121 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len); |
cee3ca00 | 122 | return virtio_ldl_phys(vdev, pa); |
967f97fa AL |
123 | } |
124 | ||
cee3ca00 RR |
125 | static inline uint16_t vring_desc_flags(VirtIODevice *vdev, hwaddr desc_pa, |
126 | int i) | |
967f97fa | 127 | { |
a8170e5e | 128 | hwaddr pa; |
5774cf98 | 129 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags); |
cee3ca00 | 130 | return virtio_lduw_phys(vdev, pa); |
967f97fa AL |
131 | } |
132 | ||
cee3ca00 RR |
133 | static inline uint16_t vring_desc_next(VirtIODevice *vdev, hwaddr desc_pa, |
134 | int i) | |
967f97fa | 135 | { |
a8170e5e | 136 | hwaddr pa; |
5774cf98 | 137 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next); |
cee3ca00 | 138 | return virtio_lduw_phys(vdev, pa); |
967f97fa AL |
139 | } |
140 | ||
141 | static inline uint16_t vring_avail_flags(VirtQueue *vq) | |
142 | { | |
a8170e5e | 143 | hwaddr pa; |
967f97fa | 144 | pa = vq->vring.avail + offsetof(VRingAvail, flags); |
cee3ca00 | 145 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
146 | } |
147 | ||
148 | static inline uint16_t vring_avail_idx(VirtQueue *vq) | |
149 | { | |
a8170e5e | 150 | hwaddr pa; |
967f97fa | 151 | pa = vq->vring.avail + offsetof(VRingAvail, idx); |
cee3ca00 | 152 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
153 | } |
154 | ||
155 | static inline uint16_t vring_avail_ring(VirtQueue *vq, int i) | |
156 | { | |
a8170e5e | 157 | hwaddr pa; |
967f97fa | 158 | pa = vq->vring.avail + offsetof(VRingAvail, ring[i]); |
cee3ca00 | 159 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
160 | } |
161 | ||
e9600c6c | 162 | static inline uint16_t vring_get_used_event(VirtQueue *vq) |
bcbabae8 MT |
163 | { |
164 | return vring_avail_ring(vq, vq->vring.num); | |
165 | } | |
166 | ||
967f97fa AL |
167 | static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val) |
168 | { | |
a8170e5e | 169 | hwaddr pa; |
967f97fa | 170 | pa = vq->vring.used + offsetof(VRingUsed, ring[i].id); |
cee3ca00 | 171 | virtio_stl_phys(vq->vdev, pa, val); |
967f97fa AL |
172 | } |
173 | ||
174 | static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val) | |
175 | { | |
a8170e5e | 176 | hwaddr pa; |
967f97fa | 177 | pa = vq->vring.used + offsetof(VRingUsed, ring[i].len); |
cee3ca00 | 178 | virtio_stl_phys(vq->vdev, pa, val); |
967f97fa AL |
179 | } |
180 | ||
181 | static uint16_t vring_used_idx(VirtQueue *vq) | |
182 | { | |
a8170e5e | 183 | hwaddr pa; |
967f97fa | 184 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
cee3ca00 | 185 | return virtio_lduw_phys(vq->vdev, pa); |
967f97fa AL |
186 | } |
187 | ||
bcbabae8 | 188 | static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val) |
967f97fa | 189 | { |
a8170e5e | 190 | hwaddr pa; |
967f97fa | 191 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
cee3ca00 | 192 | virtio_stw_phys(vq->vdev, pa, val); |
967f97fa AL |
193 | } |
194 | ||
195 | static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask) | |
196 | { | |
cee3ca00 | 197 | VirtIODevice *vdev = vq->vdev; |
a8170e5e | 198 | hwaddr pa; |
967f97fa | 199 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
cee3ca00 | 200 | virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) | mask); |
967f97fa AL |
201 | } |
202 | ||
203 | static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask) | |
204 | { | |
cee3ca00 | 205 | VirtIODevice *vdev = vq->vdev; |
a8170e5e | 206 | hwaddr pa; |
967f97fa | 207 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
cee3ca00 | 208 | virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) & ~mask); |
967f97fa AL |
209 | } |
210 | ||
e9600c6c | 211 | static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val) |
bcbabae8 | 212 | { |
a8170e5e | 213 | hwaddr pa; |
bcbabae8 MT |
214 | if (!vq->notification) { |
215 | return; | |
216 | } | |
217 | pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]); | |
cee3ca00 | 218 | virtio_stw_phys(vq->vdev, pa, val); |
bcbabae8 MT |
219 | } |
220 | ||
967f97fa AL |
221 | void virtio_queue_set_notification(VirtQueue *vq, int enable) |
222 | { | |
bcbabae8 | 223 | vq->notification = enable; |
95129d6f | 224 | if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { |
e9600c6c | 225 | vring_set_avail_event(vq, vring_avail_idx(vq)); |
bcbabae8 | 226 | } else if (enable) { |
967f97fa | 227 | vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 228 | } else { |
967f97fa | 229 | vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 230 | } |
92045d80 MT |
231 | if (enable) { |
232 | /* Expose avail event/used flags before caller checks the avail idx. */ | |
233 | smp_mb(); | |
234 | } | |
967f97fa AL |
235 | } |
236 | ||
237 | int virtio_queue_ready(VirtQueue *vq) | |
238 | { | |
239 | return vq->vring.avail != 0; | |
240 | } | |
241 | ||
242 | int virtio_queue_empty(VirtQueue *vq) | |
243 | { | |
244 | return vring_avail_idx(vq) == vq->last_avail_idx; | |
245 | } | |
246 | ||
ce317461 JW |
247 | static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, |
248 | unsigned int len) | |
967f97fa AL |
249 | { |
250 | unsigned int offset; | |
251 | int i; | |
252 | ||
967f97fa AL |
253 | offset = 0; |
254 | for (i = 0; i < elem->in_num; i++) { | |
255 | size_t size = MIN(len - offset, elem->in_sg[i].iov_len); | |
256 | ||
26b258e1 AL |
257 | cpu_physical_memory_unmap(elem->in_sg[i].iov_base, |
258 | elem->in_sg[i].iov_len, | |
259 | 1, size); | |
967f97fa | 260 | |
0cea71a2 | 261 | offset += size; |
967f97fa AL |
262 | } |
263 | ||
26b258e1 AL |
264 | for (i = 0; i < elem->out_num; i++) |
265 | cpu_physical_memory_unmap(elem->out_sg[i].iov_base, | |
266 | elem->out_sg[i].iov_len, | |
267 | 0, elem->out_sg[i].iov_len); | |
ce317461 JW |
268 | } |
269 | ||
29b9f5ef JW |
270 | void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem, |
271 | unsigned int len) | |
272 | { | |
273 | vq->last_avail_idx--; | |
274 | virtqueue_unmap_sg(vq, elem, len); | |
275 | } | |
276 | ||
ce317461 JW |
277 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, |
278 | unsigned int len, unsigned int idx) | |
279 | { | |
280 | trace_virtqueue_fill(vq, elem, len, idx); | |
281 | ||
282 | virtqueue_unmap_sg(vq, elem, len); | |
26b258e1 | 283 | |
967f97fa AL |
284 | idx = (idx + vring_used_idx(vq)) % vq->vring.num; |
285 | ||
286 | /* Get a pointer to the next entry in the used ring. */ | |
287 | vring_used_ring_id(vq, idx, elem->index); | |
288 | vring_used_ring_len(vq, idx, len); | |
289 | } | |
290 | ||
291 | void virtqueue_flush(VirtQueue *vq, unsigned int count) | |
292 | { | |
bcbabae8 | 293 | uint16_t old, new; |
967f97fa | 294 | /* Make sure buffer is written before we update index. */ |
b90d2f35 | 295 | smp_wmb(); |
64979a4d | 296 | trace_virtqueue_flush(vq, count); |
bcbabae8 MT |
297 | old = vring_used_idx(vq); |
298 | new = old + count; | |
299 | vring_used_idx_set(vq, new); | |
967f97fa | 300 | vq->inuse -= count; |
bcbabae8 MT |
301 | if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))) |
302 | vq->signalled_used_valid = false; | |
967f97fa AL |
303 | } |
304 | ||
305 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, | |
306 | unsigned int len) | |
307 | { | |
308 | virtqueue_fill(vq, elem, len, 0); | |
309 | virtqueue_flush(vq, 1); | |
310 | } | |
311 | ||
312 | static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx) | |
313 | { | |
314 | uint16_t num_heads = vring_avail_idx(vq) - idx; | |
315 | ||
316 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
bb6834cf | 317 | if (num_heads > vq->vring.num) { |
ce67ed65 SH |
318 | error_report("Guest moved used index from %u to %u", |
319 | idx, vring_avail_idx(vq)); | |
bb6834cf AL |
320 | exit(1); |
321 | } | |
a821ce59 MT |
322 | /* On success, callers read a descriptor at vq->last_avail_idx. |
323 | * Make sure descriptor read does not bypass avail index read. */ | |
324 | if (num_heads) { | |
325 | smp_rmb(); | |
326 | } | |
967f97fa AL |
327 | |
328 | return num_heads; | |
329 | } | |
330 | ||
331 | static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx) | |
332 | { | |
333 | unsigned int head; | |
334 | ||
335 | /* Grab the next descriptor number they're advertising, and increment | |
336 | * the index we've seen. */ | |
337 | head = vring_avail_ring(vq, idx % vq->vring.num); | |
338 | ||
339 | /* If their number is silly, that's a fatal mistake. */ | |
bb6834cf | 340 | if (head >= vq->vring.num) { |
ce67ed65 | 341 | error_report("Guest says index %u is available", head); |
bb6834cf AL |
342 | exit(1); |
343 | } | |
967f97fa AL |
344 | |
345 | return head; | |
346 | } | |
347 | ||
cee3ca00 | 348 | static unsigned virtqueue_next_desc(VirtIODevice *vdev, hwaddr desc_pa, |
5774cf98 | 349 | unsigned int i, unsigned int max) |
967f97fa AL |
350 | { |
351 | unsigned int next; | |
352 | ||
353 | /* If this descriptor says it doesn't chain, we're done. */ | |
cee3ca00 | 354 | if (!(vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_NEXT)) { |
5774cf98 | 355 | return max; |
cee3ca00 | 356 | } |
967f97fa AL |
357 | |
358 | /* Check they're not leading us off end of descriptors. */ | |
cee3ca00 | 359 | next = vring_desc_next(vdev, desc_pa, i); |
967f97fa | 360 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
b90d2f35 | 361 | smp_wmb(); |
967f97fa | 362 | |
5774cf98 | 363 | if (next >= max) { |
ce67ed65 | 364 | error_report("Desc next is %u", next); |
bb6834cf AL |
365 | exit(1); |
366 | } | |
967f97fa AL |
367 | |
368 | return next; | |
369 | } | |
370 | ||
0d8d7690 | 371 | void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, |
e1f7b481 MT |
372 | unsigned int *out_bytes, |
373 | unsigned max_in_bytes, unsigned max_out_bytes) | |
967f97fa | 374 | { |
efeea6d0 | 375 | unsigned int idx; |
385ce95d | 376 | unsigned int total_bufs, in_total, out_total; |
967f97fa AL |
377 | |
378 | idx = vq->last_avail_idx; | |
379 | ||
efeea6d0 | 380 | total_bufs = in_total = out_total = 0; |
967f97fa | 381 | while (virtqueue_num_heads(vq, idx)) { |
cee3ca00 | 382 | VirtIODevice *vdev = vq->vdev; |
efeea6d0 | 383 | unsigned int max, num_bufs, indirect = 0; |
a8170e5e | 384 | hwaddr desc_pa; |
967f97fa AL |
385 | int i; |
386 | ||
efeea6d0 MM |
387 | max = vq->vring.num; |
388 | num_bufs = total_bufs; | |
967f97fa | 389 | i = virtqueue_get_head(vq, idx++); |
efeea6d0 MM |
390 | desc_pa = vq->vring.desc; |
391 | ||
cee3ca00 RR |
392 | if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_INDIRECT) { |
393 | if (vring_desc_len(vdev, desc_pa, i) % sizeof(VRingDesc)) { | |
ce67ed65 | 394 | error_report("Invalid size for indirect buffer table"); |
efeea6d0 MM |
395 | exit(1); |
396 | } | |
397 | ||
398 | /* If we've got too many, that implies a descriptor loop. */ | |
399 | if (num_bufs >= max) { | |
ce67ed65 | 400 | error_report("Looped descriptor"); |
efeea6d0 MM |
401 | exit(1); |
402 | } | |
403 | ||
404 | /* loop over the indirect descriptor table */ | |
405 | indirect = 1; | |
cee3ca00 RR |
406 | max = vring_desc_len(vdev, desc_pa, i) / sizeof(VRingDesc); |
407 | desc_pa = vring_desc_addr(vdev, desc_pa, i); | |
1ae2757c | 408 | num_bufs = i = 0; |
efeea6d0 MM |
409 | } |
410 | ||
967f97fa AL |
411 | do { |
412 | /* If we've got too many, that implies a descriptor loop. */ | |
5774cf98 | 413 | if (++num_bufs > max) { |
ce67ed65 | 414 | error_report("Looped descriptor"); |
bb6834cf AL |
415 | exit(1); |
416 | } | |
967f97fa | 417 | |
cee3ca00 RR |
418 | if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_WRITE) { |
419 | in_total += vring_desc_len(vdev, desc_pa, i); | |
967f97fa | 420 | } else { |
cee3ca00 | 421 | out_total += vring_desc_len(vdev, desc_pa, i); |
967f97fa | 422 | } |
e1f7b481 MT |
423 | if (in_total >= max_in_bytes && out_total >= max_out_bytes) { |
424 | goto done; | |
425 | } | |
cee3ca00 | 426 | } while ((i = virtqueue_next_desc(vdev, desc_pa, i, max)) != max); |
efeea6d0 MM |
427 | |
428 | if (!indirect) | |
429 | total_bufs = num_bufs; | |
430 | else | |
431 | total_bufs++; | |
967f97fa | 432 | } |
e1f7b481 | 433 | done: |
0d8d7690 AS |
434 | if (in_bytes) { |
435 | *in_bytes = in_total; | |
436 | } | |
437 | if (out_bytes) { | |
438 | *out_bytes = out_total; | |
439 | } | |
440 | } | |
967f97fa | 441 | |
0d8d7690 AS |
442 | int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, |
443 | unsigned int out_bytes) | |
444 | { | |
445 | unsigned int in_total, out_total; | |
446 | ||
e1f7b481 MT |
447 | virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes); |
448 | return in_bytes <= in_total && out_bytes <= out_total; | |
967f97fa AL |
449 | } |
450 | ||
a8170e5e | 451 | void virtqueue_map_sg(struct iovec *sg, hwaddr *addr, |
42fb2e07 KW |
452 | size_t num_sg, int is_write) |
453 | { | |
454 | unsigned int i; | |
a8170e5e | 455 | hwaddr len; |
42fb2e07 | 456 | |
93725140 | 457 | if (num_sg > VIRTQUEUE_MAX_SIZE) { |
36cf2a37 MT |
458 | error_report("virtio: map attempt out of bounds: %zd > %d", |
459 | num_sg, VIRTQUEUE_MAX_SIZE); | |
460 | exit(1); | |
461 | } | |
462 | ||
42fb2e07 KW |
463 | for (i = 0; i < num_sg; i++) { |
464 | len = sg[i].iov_len; | |
465 | sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write); | |
466 | if (sg[i].iov_base == NULL || len != sg[i].iov_len) { | |
1a285899 | 467 | error_report("virtio: error trying to map MMIO memory"); |
42fb2e07 KW |
468 | exit(1); |
469 | } | |
470 | } | |
471 | } | |
472 | ||
967f97fa AL |
473 | int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem) |
474 | { | |
5774cf98 | 475 | unsigned int i, head, max; |
a8170e5e | 476 | hwaddr desc_pa = vq->vring.desc; |
cee3ca00 | 477 | VirtIODevice *vdev = vq->vdev; |
967f97fa AL |
478 | |
479 | if (!virtqueue_num_heads(vq, vq->last_avail_idx)) | |
480 | return 0; | |
481 | ||
482 | /* When we start there are none of either input nor output. */ | |
483 | elem->out_num = elem->in_num = 0; | |
484 | ||
5774cf98 MM |
485 | max = vq->vring.num; |
486 | ||
967f97fa | 487 | i = head = virtqueue_get_head(vq, vq->last_avail_idx++); |
95129d6f | 488 | if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
e9600c6c | 489 | vring_set_avail_event(vq, vq->last_avail_idx); |
bcbabae8 | 490 | } |
efeea6d0 | 491 | |
cee3ca00 RR |
492 | if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_INDIRECT) { |
493 | if (vring_desc_len(vdev, desc_pa, i) % sizeof(VRingDesc)) { | |
ce67ed65 | 494 | error_report("Invalid size for indirect buffer table"); |
efeea6d0 MM |
495 | exit(1); |
496 | } | |
497 | ||
498 | /* loop over the indirect descriptor table */ | |
cee3ca00 RR |
499 | max = vring_desc_len(vdev, desc_pa, i) / sizeof(VRingDesc); |
500 | desc_pa = vring_desc_addr(vdev, desc_pa, i); | |
efeea6d0 MM |
501 | i = 0; |
502 | } | |
503 | ||
42fb2e07 | 504 | /* Collect all the descriptors */ |
967f97fa AL |
505 | do { |
506 | struct iovec *sg; | |
507 | ||
cee3ca00 | 508 | if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_WRITE) { |
c8eac1cf MT |
509 | if (elem->in_num >= ARRAY_SIZE(elem->in_sg)) { |
510 | error_report("Too many write descriptors in indirect table"); | |
511 | exit(1); | |
512 | } | |
cee3ca00 | 513 | elem->in_addr[elem->in_num] = vring_desc_addr(vdev, desc_pa, i); |
967f97fa | 514 | sg = &elem->in_sg[elem->in_num++]; |
42fb2e07 | 515 | } else { |
c8eac1cf MT |
516 | if (elem->out_num >= ARRAY_SIZE(elem->out_sg)) { |
517 | error_report("Too many read descriptors in indirect table"); | |
518 | exit(1); | |
519 | } | |
cee3ca00 | 520 | elem->out_addr[elem->out_num] = vring_desc_addr(vdev, desc_pa, i); |
967f97fa | 521 | sg = &elem->out_sg[elem->out_num++]; |
42fb2e07 | 522 | } |
967f97fa | 523 | |
cee3ca00 | 524 | sg->iov_len = vring_desc_len(vdev, desc_pa, i); |
967f97fa AL |
525 | |
526 | /* If we've got too many, that implies a descriptor loop. */ | |
5774cf98 | 527 | if ((elem->in_num + elem->out_num) > max) { |
ce67ed65 | 528 | error_report("Looped descriptor"); |
bb6834cf AL |
529 | exit(1); |
530 | } | |
cee3ca00 | 531 | } while ((i = virtqueue_next_desc(vdev, desc_pa, i, max)) != max); |
967f97fa | 532 | |
42fb2e07 KW |
533 | /* Now map what we have collected */ |
534 | virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1); | |
535 | virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0); | |
536 | ||
967f97fa AL |
537 | elem->index = head; |
538 | ||
539 | vq->inuse++; | |
540 | ||
64979a4d | 541 | trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); |
967f97fa AL |
542 | return elem->in_num + elem->out_num; |
543 | } | |
544 | ||
545 | /* virtio device */ | |
7055e687 MT |
546 | static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector) |
547 | { | |
1c819449 FK |
548 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
549 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
550 | ||
551 | if (k->notify) { | |
552 | k->notify(qbus->parent, vector); | |
7055e687 MT |
553 | } |
554 | } | |
967f97fa | 555 | |
53c25cea | 556 | void virtio_update_irq(VirtIODevice *vdev) |
967f97fa | 557 | { |
7055e687 | 558 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
967f97fa AL |
559 | } |
560 | ||
0b352fd6 CH |
561 | static int virtio_validate_features(VirtIODevice *vdev) |
562 | { | |
563 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
564 | ||
565 | if (k->validate_features) { | |
566 | return k->validate_features(vdev); | |
567 | } else { | |
568 | return 0; | |
569 | } | |
570 | } | |
571 | ||
572 | int virtio_set_status(VirtIODevice *vdev, uint8_t val) | |
4e1837f8 | 573 | { |
181103cd | 574 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
4e1837f8 SH |
575 | trace_virtio_set_status(vdev, val); |
576 | ||
95129d6f | 577 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
0b352fd6 CH |
578 | if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) && |
579 | val & VIRTIO_CONFIG_S_FEATURES_OK) { | |
580 | int ret = virtio_validate_features(vdev); | |
581 | ||
582 | if (ret) { | |
583 | return ret; | |
584 | } | |
585 | } | |
586 | } | |
181103cd FK |
587 | if (k->set_status) { |
588 | k->set_status(vdev, val); | |
4e1837f8 SH |
589 | } |
590 | vdev->status = val; | |
0b352fd6 | 591 | return 0; |
4e1837f8 SH |
592 | } |
593 | ||
616a6552 GK |
594 | bool target_words_bigendian(void); |
595 | static enum virtio_device_endian virtio_default_endian(void) | |
596 | { | |
597 | if (target_words_bigendian()) { | |
598 | return VIRTIO_DEVICE_ENDIAN_BIG; | |
599 | } else { | |
600 | return VIRTIO_DEVICE_ENDIAN_LITTLE; | |
601 | } | |
602 | } | |
603 | ||
604 | static enum virtio_device_endian virtio_current_cpu_endian(void) | |
605 | { | |
606 | CPUClass *cc = CPU_GET_CLASS(current_cpu); | |
607 | ||
608 | if (cc->virtio_is_big_endian(current_cpu)) { | |
609 | return VIRTIO_DEVICE_ENDIAN_BIG; | |
610 | } else { | |
611 | return VIRTIO_DEVICE_ENDIAN_LITTLE; | |
612 | } | |
613 | } | |
614 | ||
53c25cea | 615 | void virtio_reset(void *opaque) |
967f97fa AL |
616 | { |
617 | VirtIODevice *vdev = opaque; | |
181103cd | 618 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
619 | int i; |
620 | ||
e0c472d8 | 621 | virtio_set_status(vdev, 0); |
616a6552 GK |
622 | if (current_cpu) { |
623 | /* Guest initiated reset */ | |
624 | vdev->device_endian = virtio_current_cpu_endian(); | |
625 | } else { | |
626 | /* System reset */ | |
627 | vdev->device_endian = virtio_default_endian(); | |
628 | } | |
e0c472d8 | 629 | |
181103cd FK |
630 | if (k->reset) { |
631 | k->reset(vdev); | |
632 | } | |
967f97fa | 633 | |
704a76fc | 634 | vdev->guest_features = 0; |
967f97fa AL |
635 | vdev->queue_sel = 0; |
636 | vdev->status = 0; | |
637 | vdev->isr = 0; | |
7055e687 MT |
638 | vdev->config_vector = VIRTIO_NO_VECTOR; |
639 | virtio_notify_vector(vdev, vdev->config_vector); | |
967f97fa | 640 | |
87b3bd1c | 641 | for(i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
642 | vdev->vq[i].vring.desc = 0; |
643 | vdev->vq[i].vring.avail = 0; | |
644 | vdev->vq[i].vring.used = 0; | |
645 | vdev->vq[i].last_avail_idx = 0; | |
e0d686bf | 646 | virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR); |
bcbabae8 MT |
647 | vdev->vq[i].signalled_used = 0; |
648 | vdev->vq[i].signalled_used_valid = false; | |
649 | vdev->vq[i].notification = true; | |
46c5d082 | 650 | vdev->vq[i].vring.num = vdev->vq[i].vring.num_default; |
967f97fa AL |
651 | } |
652 | } | |
653 | ||
53c25cea | 654 | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 655 | { |
181103cd | 656 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
657 | uint8_t val; |
658 | ||
5f5a1318 | 659 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 660 | return (uint32_t)-1; |
5f5a1318 JW |
661 | } |
662 | ||
663 | k->get_config(vdev, vdev->config); | |
967f97fa | 664 | |
06dbfc6f | 665 | val = ldub_p(vdev->config + addr); |
967f97fa AL |
666 | return val; |
667 | } | |
668 | ||
53c25cea | 669 | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 670 | { |
181103cd | 671 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
672 | uint16_t val; |
673 | ||
5f5a1318 | 674 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 675 | return (uint32_t)-1; |
5f5a1318 JW |
676 | } |
677 | ||
678 | k->get_config(vdev, vdev->config); | |
967f97fa | 679 | |
06dbfc6f | 680 | val = lduw_p(vdev->config + addr); |
967f97fa AL |
681 | return val; |
682 | } | |
683 | ||
53c25cea | 684 | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 685 | { |
181103cd | 686 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
687 | uint32_t val; |
688 | ||
5f5a1318 | 689 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 690 | return (uint32_t)-1; |
5f5a1318 JW |
691 | } |
692 | ||
693 | k->get_config(vdev, vdev->config); | |
967f97fa | 694 | |
06dbfc6f | 695 | val = ldl_p(vdev->config + addr); |
967f97fa AL |
696 | return val; |
697 | } | |
698 | ||
53c25cea | 699 | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 700 | { |
181103cd | 701 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
702 | uint8_t val = data; |
703 | ||
5f5a1318 | 704 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 705 | return; |
5f5a1318 | 706 | } |
967f97fa | 707 | |
06dbfc6f | 708 | stb_p(vdev->config + addr, val); |
967f97fa | 709 | |
181103cd FK |
710 | if (k->set_config) { |
711 | k->set_config(vdev, vdev->config); | |
712 | } | |
967f97fa AL |
713 | } |
714 | ||
53c25cea | 715 | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 716 | { |
181103cd | 717 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
718 | uint16_t val = data; |
719 | ||
5f5a1318 | 720 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 721 | return; |
5f5a1318 | 722 | } |
967f97fa | 723 | |
06dbfc6f | 724 | stw_p(vdev->config + addr, val); |
967f97fa | 725 | |
181103cd FK |
726 | if (k->set_config) { |
727 | k->set_config(vdev, vdev->config); | |
728 | } | |
967f97fa AL |
729 | } |
730 | ||
53c25cea | 731 | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 732 | { |
181103cd | 733 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa AL |
734 | uint32_t val = data; |
735 | ||
5f5a1318 | 736 | if (addr + sizeof(val) > vdev->config_len) { |
967f97fa | 737 | return; |
5f5a1318 | 738 | } |
967f97fa | 739 | |
06dbfc6f | 740 | stl_p(vdev->config + addr, val); |
967f97fa | 741 | |
181103cd FK |
742 | if (k->set_config) { |
743 | k->set_config(vdev, vdev->config); | |
744 | } | |
967f97fa AL |
745 | } |
746 | ||
adfb743c MT |
747 | uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr) |
748 | { | |
749 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
750 | uint8_t val; | |
751 | ||
752 | if (addr + sizeof(val) > vdev->config_len) { | |
753 | return (uint32_t)-1; | |
754 | } | |
755 | ||
756 | k->get_config(vdev, vdev->config); | |
757 | ||
758 | val = ldub_p(vdev->config + addr); | |
759 | return val; | |
760 | } | |
761 | ||
762 | uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr) | |
763 | { | |
764 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
765 | uint16_t val; | |
766 | ||
767 | if (addr + sizeof(val) > vdev->config_len) { | |
768 | return (uint32_t)-1; | |
769 | } | |
770 | ||
771 | k->get_config(vdev, vdev->config); | |
772 | ||
773 | val = lduw_le_p(vdev->config + addr); | |
774 | return val; | |
775 | } | |
776 | ||
777 | uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr) | |
778 | { | |
779 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
780 | uint32_t val; | |
781 | ||
782 | if (addr + sizeof(val) > vdev->config_len) { | |
783 | return (uint32_t)-1; | |
784 | } | |
785 | ||
786 | k->get_config(vdev, vdev->config); | |
787 | ||
788 | val = ldl_le_p(vdev->config + addr); | |
789 | return val; | |
790 | } | |
791 | ||
792 | void virtio_config_modern_writeb(VirtIODevice *vdev, | |
793 | uint32_t addr, uint32_t data) | |
794 | { | |
795 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
796 | uint8_t val = data; | |
797 | ||
798 | if (addr + sizeof(val) > vdev->config_len) { | |
799 | return; | |
800 | } | |
801 | ||
802 | stb_p(vdev->config + addr, val); | |
803 | ||
804 | if (k->set_config) { | |
805 | k->set_config(vdev, vdev->config); | |
806 | } | |
807 | } | |
808 | ||
809 | void virtio_config_modern_writew(VirtIODevice *vdev, | |
810 | uint32_t addr, uint32_t data) | |
811 | { | |
812 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
813 | uint16_t val = data; | |
814 | ||
815 | if (addr + sizeof(val) > vdev->config_len) { | |
816 | return; | |
817 | } | |
818 | ||
819 | stw_le_p(vdev->config + addr, val); | |
820 | ||
821 | if (k->set_config) { | |
822 | k->set_config(vdev, vdev->config); | |
823 | } | |
824 | } | |
825 | ||
826 | void virtio_config_modern_writel(VirtIODevice *vdev, | |
827 | uint32_t addr, uint32_t data) | |
828 | { | |
829 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
830 | uint32_t val = data; | |
831 | ||
832 | if (addr + sizeof(val) > vdev->config_len) { | |
833 | return; | |
834 | } | |
835 | ||
836 | stl_le_p(vdev->config + addr, val); | |
837 | ||
838 | if (k->set_config) { | |
839 | k->set_config(vdev, vdev->config); | |
840 | } | |
841 | } | |
842 | ||
a8170e5e | 843 | void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr) |
967f97fa | 844 | { |
ab223c95 CH |
845 | vdev->vq[n].vring.desc = addr; |
846 | virtio_queue_update_rings(vdev, n); | |
53c25cea PB |
847 | } |
848 | ||
a8170e5e | 849 | hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) |
53c25cea | 850 | { |
ab223c95 CH |
851 | return vdev->vq[n].vring.desc; |
852 | } | |
853 | ||
854 | void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc, | |
855 | hwaddr avail, hwaddr used) | |
856 | { | |
857 | vdev->vq[n].vring.desc = desc; | |
858 | vdev->vq[n].vring.avail = avail; | |
859 | vdev->vq[n].vring.used = used; | |
53c25cea PB |
860 | } |
861 | ||
e63c0ba1 PM |
862 | void virtio_queue_set_num(VirtIODevice *vdev, int n, int num) |
863 | { | |
f6049f44 PM |
864 | /* Don't allow guest to flip queue between existent and |
865 | * nonexistent states, or to set it to an invalid size. | |
866 | */ | |
867 | if (!!num != !!vdev->vq[n].vring.num || | |
868 | num > VIRTQUEUE_MAX_SIZE || | |
869 | num < 0) { | |
870 | return; | |
e63c0ba1 | 871 | } |
f6049f44 | 872 | vdev->vq[n].vring.num = num; |
e63c0ba1 PM |
873 | } |
874 | ||
e0d686bf JW |
875 | VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector) |
876 | { | |
877 | return QLIST_FIRST(&vdev->vector_queues[vector]); | |
878 | } | |
879 | ||
880 | VirtQueue *virtio_vector_next_queue(VirtQueue *vq) | |
881 | { | |
882 | return QLIST_NEXT(vq, node); | |
883 | } | |
884 | ||
53c25cea PB |
885 | int virtio_queue_get_num(VirtIODevice *vdev, int n) |
886 | { | |
887 | return vdev->vq[n].vring.num; | |
888 | } | |
967f97fa | 889 | |
8ad176aa JW |
890 | int virtio_get_num_queues(VirtIODevice *vdev) |
891 | { | |
892 | int i; | |
893 | ||
87b3bd1c | 894 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
8ad176aa JW |
895 | if (!virtio_queue_get_num(vdev, i)) { |
896 | break; | |
897 | } | |
898 | } | |
899 | ||
900 | return i; | |
901 | } | |
902 | ||
c80decdb PB |
903 | int virtio_queue_get_id(VirtQueue *vq) |
904 | { | |
905 | VirtIODevice *vdev = vq->vdev; | |
87b3bd1c | 906 | assert(vq >= &vdev->vq[0] && vq < &vdev->vq[VIRTIO_QUEUE_MAX]); |
c80decdb PB |
907 | return vq - &vdev->vq[0]; |
908 | } | |
909 | ||
6ce69d1c PM |
910 | void virtio_queue_set_align(VirtIODevice *vdev, int n, int align) |
911 | { | |
912 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); | |
913 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
914 | ||
ab223c95 | 915 | /* virtio-1 compliant devices cannot change the alignment */ |
95129d6f | 916 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
ab223c95 CH |
917 | error_report("tried to modify queue alignment for virtio-1 device"); |
918 | return; | |
919 | } | |
6ce69d1c PM |
920 | /* Check that the transport told us it was going to do this |
921 | * (so a buggy transport will immediately assert rather than | |
922 | * silently failing to migrate this state) | |
923 | */ | |
924 | assert(k->has_variable_vring_alignment); | |
925 | ||
926 | vdev->vq[n].vring.align = align; | |
ab223c95 | 927 | virtio_queue_update_rings(vdev, n); |
6ce69d1c PM |
928 | } |
929 | ||
25db9ebe SH |
930 | void virtio_queue_notify_vq(VirtQueue *vq) |
931 | { | |
9e0f5b81 | 932 | if (vq->vring.desc && vq->handle_output) { |
25db9ebe | 933 | VirtIODevice *vdev = vq->vdev; |
9e0f5b81 | 934 | |
25db9ebe SH |
935 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); |
936 | vq->handle_output(vdev, vq); | |
937 | } | |
938 | } | |
939 | ||
53c25cea PB |
940 | void virtio_queue_notify(VirtIODevice *vdev, int n) |
941 | { | |
7157e2e2 | 942 | virtio_queue_notify_vq(&vdev->vq[n]); |
967f97fa AL |
943 | } |
944 | ||
7055e687 MT |
945 | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n) |
946 | { | |
87b3bd1c | 947 | return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector : |
7055e687 MT |
948 | VIRTIO_NO_VECTOR; |
949 | } | |
950 | ||
951 | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector) | |
952 | { | |
e0d686bf JW |
953 | VirtQueue *vq = &vdev->vq[n]; |
954 | ||
87b3bd1c | 955 | if (n < VIRTIO_QUEUE_MAX) { |
e0d686bf JW |
956 | if (vdev->vector_queues && |
957 | vdev->vq[n].vector != VIRTIO_NO_VECTOR) { | |
958 | QLIST_REMOVE(vq, node); | |
959 | } | |
7055e687 | 960 | vdev->vq[n].vector = vector; |
e0d686bf JW |
961 | if (vdev->vector_queues && |
962 | vector != VIRTIO_NO_VECTOR) { | |
963 | QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node); | |
964 | } | |
965 | } | |
7055e687 MT |
966 | } |
967 | ||
967f97fa AL |
968 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, |
969 | void (*handle_output)(VirtIODevice *, VirtQueue *)) | |
970 | { | |
971 | int i; | |
972 | ||
87b3bd1c | 973 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
974 | if (vdev->vq[i].vring.num == 0) |
975 | break; | |
976 | } | |
977 | ||
87b3bd1c | 978 | if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE) |
967f97fa AL |
979 | abort(); |
980 | ||
981 | vdev->vq[i].vring.num = queue_size; | |
46c5d082 | 982 | vdev->vq[i].vring.num_default = queue_size; |
6ce69d1c | 983 | vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN; |
967f97fa AL |
984 | vdev->vq[i].handle_output = handle_output; |
985 | ||
986 | return &vdev->vq[i]; | |
987 | } | |
988 | ||
f23fd811 JW |
989 | void virtio_del_queue(VirtIODevice *vdev, int n) |
990 | { | |
87b3bd1c | 991 | if (n < 0 || n >= VIRTIO_QUEUE_MAX) { |
f23fd811 JW |
992 | abort(); |
993 | } | |
994 | ||
995 | vdev->vq[n].vring.num = 0; | |
46c5d082 | 996 | vdev->vq[n].vring.num_default = 0; |
f23fd811 JW |
997 | } |
998 | ||
1cbdabe2 MT |
999 | void virtio_irq(VirtQueue *vq) |
1000 | { | |
64979a4d | 1001 | trace_virtio_irq(vq); |
1cbdabe2 MT |
1002 | vq->vdev->isr |= 0x01; |
1003 | virtio_notify_vector(vq->vdev, vq->vector); | |
1004 | } | |
1005 | ||
bcbabae8 MT |
1006 | static bool vring_notify(VirtIODevice *vdev, VirtQueue *vq) |
1007 | { | |
1008 | uint16_t old, new; | |
1009 | bool v; | |
a281ebc1 MT |
1010 | /* We need to expose used array entries before checking used event. */ |
1011 | smp_mb(); | |
97b83deb | 1012 | /* Always notify when queue is empty (when feature acknowledge) */ |
95129d6f | 1013 | if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) && |
ef546f12 | 1014 | !vq->inuse && vring_avail_idx(vq) == vq->last_avail_idx) { |
bcbabae8 MT |
1015 | return true; |
1016 | } | |
1017 | ||
95129d6f | 1018 | if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
bcbabae8 MT |
1019 | return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT); |
1020 | } | |
1021 | ||
1022 | v = vq->signalled_used_valid; | |
1023 | vq->signalled_used_valid = true; | |
1024 | old = vq->signalled_used; | |
1025 | new = vq->signalled_used = vring_used_idx(vq); | |
e9600c6c | 1026 | return !v || vring_need_event(vring_get_used_event(vq), new, old); |
bcbabae8 MT |
1027 | } |
1028 | ||
1029 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) | |
1030 | { | |
1031 | if (!vring_notify(vdev, vq)) { | |
967f97fa | 1032 | return; |
bcbabae8 | 1033 | } |
967f97fa | 1034 | |
64979a4d | 1035 | trace_virtio_notify(vdev, vq); |
967f97fa | 1036 | vdev->isr |= 0x01; |
7055e687 | 1037 | virtio_notify_vector(vdev, vq->vector); |
967f97fa AL |
1038 | } |
1039 | ||
1040 | void virtio_notify_config(VirtIODevice *vdev) | |
1041 | { | |
7625162c AL |
1042 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) |
1043 | return; | |
1044 | ||
967f97fa | 1045 | vdev->isr |= 0x03; |
b8f05908 | 1046 | vdev->generation++; |
7055e687 | 1047 | virtio_notify_vector(vdev, vdev->config_vector); |
967f97fa AL |
1048 | } |
1049 | ||
616a6552 GK |
1050 | static bool virtio_device_endian_needed(void *opaque) |
1051 | { | |
1052 | VirtIODevice *vdev = opaque; | |
1053 | ||
1054 | assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN); | |
95129d6f | 1055 | if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
3c185597 CH |
1056 | return vdev->device_endian != virtio_default_endian(); |
1057 | } | |
1058 | /* Devices conforming to VIRTIO 1.0 or later are always LE. */ | |
1059 | return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE; | |
616a6552 GK |
1060 | } |
1061 | ||
019a3edb GH |
1062 | static bool virtio_64bit_features_needed(void *opaque) |
1063 | { | |
1064 | VirtIODevice *vdev = opaque; | |
1065 | ||
1066 | return (vdev->host_features >> 32) != 0; | |
1067 | } | |
1068 | ||
74aae7b2 JW |
1069 | static bool virtio_virtqueue_needed(void *opaque) |
1070 | { | |
1071 | VirtIODevice *vdev = opaque; | |
1072 | ||
1073 | return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1); | |
1074 | } | |
1075 | ||
46c5d082 CH |
1076 | static bool virtio_ringsize_needed(void *opaque) |
1077 | { | |
1078 | VirtIODevice *vdev = opaque; | |
1079 | int i; | |
1080 | ||
1081 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
1082 | if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) { | |
1083 | return true; | |
1084 | } | |
1085 | } | |
1086 | return false; | |
1087 | } | |
1088 | ||
74aae7b2 JW |
1089 | static void put_virtqueue_state(QEMUFile *f, void *pv, size_t size) |
1090 | { | |
1091 | VirtIODevice *vdev = pv; | |
1092 | int i; | |
1093 | ||
1094 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
1095 | qemu_put_be64(f, vdev->vq[i].vring.avail); | |
1096 | qemu_put_be64(f, vdev->vq[i].vring.used); | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | static int get_virtqueue_state(QEMUFile *f, void *pv, size_t size) | |
1101 | { | |
1102 | VirtIODevice *vdev = pv; | |
1103 | int i; | |
1104 | ||
1105 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
1106 | vdev->vq[i].vring.avail = qemu_get_be64(f); | |
1107 | vdev->vq[i].vring.used = qemu_get_be64(f); | |
1108 | } | |
1109 | return 0; | |
1110 | } | |
1111 | ||
1112 | static VMStateInfo vmstate_info_virtqueue = { | |
1113 | .name = "virtqueue_state", | |
1114 | .get = get_virtqueue_state, | |
1115 | .put = put_virtqueue_state, | |
1116 | }; | |
1117 | ||
1118 | static const VMStateDescription vmstate_virtio_virtqueues = { | |
1119 | .name = "virtio/virtqueues", | |
1120 | .version_id = 1, | |
1121 | .minimum_version_id = 1, | |
1122 | .needed = &virtio_virtqueue_needed, | |
1123 | .fields = (VMStateField[]) { | |
1124 | { | |
1125 | .name = "virtqueues", | |
1126 | .version_id = 0, | |
1127 | .field_exists = NULL, | |
1128 | .size = 0, | |
1129 | .info = &vmstate_info_virtqueue, | |
1130 | .flags = VMS_SINGLE, | |
1131 | .offset = 0, | |
1132 | }, | |
1133 | VMSTATE_END_OF_LIST() | |
1134 | } | |
1135 | }; | |
1136 | ||
46c5d082 CH |
1137 | static void put_ringsize_state(QEMUFile *f, void *pv, size_t size) |
1138 | { | |
1139 | VirtIODevice *vdev = pv; | |
1140 | int i; | |
1141 | ||
1142 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
1143 | qemu_put_be32(f, vdev->vq[i].vring.num_default); | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | static int get_ringsize_state(QEMUFile *f, void *pv, size_t size) | |
1148 | { | |
1149 | VirtIODevice *vdev = pv; | |
1150 | int i; | |
1151 | ||
1152 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
1153 | vdev->vq[i].vring.num_default = qemu_get_be32(f); | |
1154 | } | |
1155 | return 0; | |
1156 | } | |
1157 | ||
1158 | static VMStateInfo vmstate_info_ringsize = { | |
1159 | .name = "ringsize_state", | |
1160 | .get = get_ringsize_state, | |
1161 | .put = put_ringsize_state, | |
1162 | }; | |
1163 | ||
1164 | static const VMStateDescription vmstate_virtio_ringsize = { | |
1165 | .name = "virtio/ringsize", | |
1166 | .version_id = 1, | |
1167 | .minimum_version_id = 1, | |
1168 | .needed = &virtio_ringsize_needed, | |
1169 | .fields = (VMStateField[]) { | |
1170 | { | |
1171 | .name = "ringsize", | |
1172 | .version_id = 0, | |
1173 | .field_exists = NULL, | |
1174 | .size = 0, | |
1175 | .info = &vmstate_info_ringsize, | |
1176 | .flags = VMS_SINGLE, | |
1177 | .offset = 0, | |
1178 | }, | |
1179 | VMSTATE_END_OF_LIST() | |
1180 | } | |
1181 | }; | |
1182 | ||
616a6552 GK |
1183 | static const VMStateDescription vmstate_virtio_device_endian = { |
1184 | .name = "virtio/device_endian", | |
1185 | .version_id = 1, | |
1186 | .minimum_version_id = 1, | |
5cd8cada | 1187 | .needed = &virtio_device_endian_needed, |
616a6552 GK |
1188 | .fields = (VMStateField[]) { |
1189 | VMSTATE_UINT8(device_endian, VirtIODevice), | |
1190 | VMSTATE_END_OF_LIST() | |
1191 | } | |
1192 | }; | |
1193 | ||
019a3edb GH |
1194 | static const VMStateDescription vmstate_virtio_64bit_features = { |
1195 | .name = "virtio/64bit_features", | |
1196 | .version_id = 1, | |
1197 | .minimum_version_id = 1, | |
5cd8cada | 1198 | .needed = &virtio_64bit_features_needed, |
019a3edb GH |
1199 | .fields = (VMStateField[]) { |
1200 | VMSTATE_UINT64(guest_features, VirtIODevice), | |
1201 | VMSTATE_END_OF_LIST() | |
1202 | } | |
1203 | }; | |
1204 | ||
6b321a3d GK |
1205 | static const VMStateDescription vmstate_virtio = { |
1206 | .name = "virtio", | |
1207 | .version_id = 1, | |
1208 | .minimum_version_id = 1, | |
1209 | .minimum_version_id_old = 1, | |
1210 | .fields = (VMStateField[]) { | |
1211 | VMSTATE_END_OF_LIST() | |
616a6552 | 1212 | }, |
5cd8cada JQ |
1213 | .subsections = (const VMStateDescription*[]) { |
1214 | &vmstate_virtio_device_endian, | |
1215 | &vmstate_virtio_64bit_features, | |
74aae7b2 | 1216 | &vmstate_virtio_virtqueues, |
46c5d082 | 1217 | &vmstate_virtio_ringsize, |
5cd8cada | 1218 | NULL |
6b321a3d GK |
1219 | } |
1220 | }; | |
1221 | ||
967f97fa AL |
1222 | void virtio_save(VirtIODevice *vdev, QEMUFile *f) |
1223 | { | |
1c819449 FK |
1224 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1225 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1b5fc0de | 1226 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
019a3edb | 1227 | uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff); |
967f97fa AL |
1228 | int i; |
1229 | ||
1c819449 FK |
1230 | if (k->save_config) { |
1231 | k->save_config(qbus->parent, f); | |
1232 | } | |
967f97fa | 1233 | |
967f97fa AL |
1234 | qemu_put_8s(f, &vdev->status); |
1235 | qemu_put_8s(f, &vdev->isr); | |
1236 | qemu_put_be16s(f, &vdev->queue_sel); | |
019a3edb | 1237 | qemu_put_be32s(f, &guest_features_lo); |
967f97fa AL |
1238 | qemu_put_be32(f, vdev->config_len); |
1239 | qemu_put_buffer(f, vdev->config, vdev->config_len); | |
1240 | ||
87b3bd1c | 1241 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
1242 | if (vdev->vq[i].vring.num == 0) |
1243 | break; | |
1244 | } | |
1245 | ||
1246 | qemu_put_be32(f, i); | |
1247 | ||
87b3bd1c | 1248 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
967f97fa AL |
1249 | if (vdev->vq[i].vring.num == 0) |
1250 | break; | |
1251 | ||
1252 | qemu_put_be32(f, vdev->vq[i].vring.num); | |
6ce69d1c PM |
1253 | if (k->has_variable_vring_alignment) { |
1254 | qemu_put_be32(f, vdev->vq[i].vring.align); | |
1255 | } | |
ab223c95 CH |
1256 | /* XXX virtio-1 devices */ |
1257 | qemu_put_be64(f, vdev->vq[i].vring.desc); | |
967f97fa | 1258 | qemu_put_be16s(f, &vdev->vq[i].last_avail_idx); |
1c819449 FK |
1259 | if (k->save_queue) { |
1260 | k->save_queue(qbus->parent, i, f); | |
1261 | } | |
967f97fa | 1262 | } |
1b5fc0de GK |
1263 | |
1264 | if (vdc->save != NULL) { | |
1265 | vdc->save(vdev, f); | |
1266 | } | |
6b321a3d GK |
1267 | |
1268 | /* Subsections */ | |
8118f095 | 1269 | vmstate_save_state(f, &vmstate_virtio, vdev, NULL); |
967f97fa AL |
1270 | } |
1271 | ||
6c0196d7 | 1272 | static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val) |
ad0c9332 | 1273 | { |
181103cd | 1274 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
6b8f1020 | 1275 | bool bad = (val & ~(vdev->host_features)) != 0; |
ad0c9332 | 1276 | |
6b8f1020 | 1277 | val &= vdev->host_features; |
181103cd FK |
1278 | if (k->set_features) { |
1279 | k->set_features(vdev, val); | |
ad0c9332 PB |
1280 | } |
1281 | vdev->guest_features = val; | |
1282 | return bad ? -1 : 0; | |
1283 | } | |
1284 | ||
6c0196d7 CH |
1285 | int virtio_set_features(VirtIODevice *vdev, uint64_t val) |
1286 | { | |
1287 | /* | |
1288 | * The driver must not attempt to set features after feature negotiation | |
1289 | * has finished. | |
1290 | */ | |
1291 | if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) { | |
1292 | return -EINVAL; | |
1293 | } | |
1294 | return virtio_set_features_nocheck(vdev, val); | |
1295 | } | |
1296 | ||
1b5fc0de | 1297 | int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) |
967f97fa | 1298 | { |
cc459952 | 1299 | int i, ret; |
a890a2f9 | 1300 | int32_t config_len; |
cc459952 | 1301 | uint32_t num; |
6d74ca5a | 1302 | uint32_t features; |
1c819449 FK |
1303 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1304 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
1b5fc0de | 1305 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
967f97fa | 1306 | |
616a6552 GK |
1307 | /* |
1308 | * We poison the endianness to ensure it does not get used before | |
1309 | * subsections have been loaded. | |
1310 | */ | |
1311 | vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN; | |
1312 | ||
1c819449 FK |
1313 | if (k->load_config) { |
1314 | ret = k->load_config(qbus->parent, f); | |
ff24bd58 MT |
1315 | if (ret) |
1316 | return ret; | |
1317 | } | |
967f97fa | 1318 | |
967f97fa AL |
1319 | qemu_get_8s(f, &vdev->status); |
1320 | qemu_get_8s(f, &vdev->isr); | |
1321 | qemu_get_be16s(f, &vdev->queue_sel); | |
87b3bd1c | 1322 | if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) { |
4b53c2c7 MR |
1323 | return -1; |
1324 | } | |
6d74ca5a | 1325 | qemu_get_be32s(f, &features); |
ad0c9332 | 1326 | |
a890a2f9 | 1327 | config_len = qemu_get_be32(f); |
2f5732e9 DDAG |
1328 | |
1329 | /* | |
1330 | * There are cases where the incoming config can be bigger or smaller | |
1331 | * than what we have; so load what we have space for, and skip | |
1332 | * any excess that's in the stream. | |
1333 | */ | |
1334 | qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len)); | |
1335 | ||
1336 | while (config_len > vdev->config_len) { | |
1337 | qemu_get_byte(f); | |
1338 | config_len--; | |
a890a2f9 | 1339 | } |
967f97fa AL |
1340 | |
1341 | num = qemu_get_be32(f); | |
1342 | ||
87b3bd1c | 1343 | if (num > VIRTIO_QUEUE_MAX) { |
cc459952 MT |
1344 | error_report("Invalid number of PCI queues: 0x%x", num); |
1345 | return -1; | |
1346 | } | |
1347 | ||
967f97fa AL |
1348 | for (i = 0; i < num; i++) { |
1349 | vdev->vq[i].vring.num = qemu_get_be32(f); | |
6ce69d1c PM |
1350 | if (k->has_variable_vring_alignment) { |
1351 | vdev->vq[i].vring.align = qemu_get_be32(f); | |
1352 | } | |
ab223c95 | 1353 | vdev->vq[i].vring.desc = qemu_get_be64(f); |
967f97fa | 1354 | qemu_get_be16s(f, &vdev->vq[i].last_avail_idx); |
bcbabae8 MT |
1355 | vdev->vq[i].signalled_used_valid = false; |
1356 | vdev->vq[i].notification = true; | |
967f97fa | 1357 | |
ab223c95 CH |
1358 | if (vdev->vq[i].vring.desc) { |
1359 | /* XXX virtio-1 devices */ | |
1360 | virtio_queue_update_rings(vdev, i); | |
1abeb5a6 MT |
1361 | } else if (vdev->vq[i].last_avail_idx) { |
1362 | error_report("VQ %d address 0x0 " | |
6daf194d | 1363 | "inconsistent with Host index 0x%x", |
1abeb5a6 MT |
1364 | i, vdev->vq[i].last_avail_idx); |
1365 | return -1; | |
258dc7c9 | 1366 | } |
1c819449 FK |
1367 | if (k->load_queue) { |
1368 | ret = k->load_queue(qbus->parent, i, f); | |
ff24bd58 MT |
1369 | if (ret) |
1370 | return ret; | |
7055e687 | 1371 | } |
967f97fa AL |
1372 | } |
1373 | ||
7055e687 | 1374 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
1b5fc0de GK |
1375 | |
1376 | if (vdc->load != NULL) { | |
6b321a3d GK |
1377 | ret = vdc->load(vdev, f, version_id); |
1378 | if (ret) { | |
1379 | return ret; | |
1380 | } | |
1b5fc0de GK |
1381 | } |
1382 | ||
616a6552 GK |
1383 | /* Subsections */ |
1384 | ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1); | |
1385 | if (ret) { | |
1386 | return ret; | |
1387 | } | |
1388 | ||
1389 | if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) { | |
1390 | vdev->device_endian = virtio_default_endian(); | |
1391 | } | |
1392 | ||
019a3edb GH |
1393 | if (virtio_64bit_features_needed(vdev)) { |
1394 | /* | |
1395 | * Subsection load filled vdev->guest_features. Run them | |
1396 | * through virtio_set_features to sanity-check them against | |
1397 | * host_features. | |
1398 | */ | |
1399 | uint64_t features64 = vdev->guest_features; | |
6c0196d7 | 1400 | if (virtio_set_features_nocheck(vdev, features64) < 0) { |
019a3edb GH |
1401 | error_report("Features 0x%" PRIx64 " unsupported. " |
1402 | "Allowed features: 0x%" PRIx64, | |
1403 | features64, vdev->host_features); | |
1404 | return -1; | |
1405 | } | |
1406 | } else { | |
6c0196d7 | 1407 | if (virtio_set_features_nocheck(vdev, features) < 0) { |
019a3edb GH |
1408 | error_report("Features 0x%x unsupported. " |
1409 | "Allowed features: 0x%" PRIx64, | |
1410 | features, vdev->host_features); | |
1411 | return -1; | |
1412 | } | |
1413 | } | |
1414 | ||
616a6552 | 1415 | for (i = 0; i < num; i++) { |
ab223c95 | 1416 | if (vdev->vq[i].vring.desc) { |
616a6552 GK |
1417 | uint16_t nheads; |
1418 | nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx; | |
1419 | /* Check it isn't doing strange things with descriptor numbers. */ | |
1420 | if (nheads > vdev->vq[i].vring.num) { | |
1421 | error_report("VQ %d size 0x%x Guest index 0x%x " | |
1422 | "inconsistent with Host index 0x%x: delta 0x%x", | |
1423 | i, vdev->vq[i].vring.num, | |
1424 | vring_avail_idx(&vdev->vq[i]), | |
1425 | vdev->vq[i].last_avail_idx, nheads); | |
1426 | return -1; | |
1427 | } | |
1428 | } | |
1429 | } | |
1430 | ||
1431 | return 0; | |
967f97fa AL |
1432 | } |
1433 | ||
6a1a8cc7 | 1434 | void virtio_cleanup(VirtIODevice *vdev) |
b946a153 | 1435 | { |
85cf2a8d | 1436 | qemu_del_vm_change_state_handler(vdev->vmstate); |
6f79e06b | 1437 | g_free(vdev->config); |
7267c094 | 1438 | g_free(vdev->vq); |
e0d686bf | 1439 | g_free(vdev->vector_queues); |
8e05db92 FK |
1440 | } |
1441 | ||
1dfb4dd9 | 1442 | static void virtio_vmstate_change(void *opaque, int running, RunState state) |
85cf2a8d MT |
1443 | { |
1444 | VirtIODevice *vdev = opaque; | |
1c819449 FK |
1445 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1446 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
85cf2a8d | 1447 | bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK); |
9e8e8c48 | 1448 | vdev->vm_running = running; |
85cf2a8d MT |
1449 | |
1450 | if (backend_run) { | |
1451 | virtio_set_status(vdev, vdev->status); | |
1452 | } | |
1453 | ||
1c819449 FK |
1454 | if (k->vmstate_change) { |
1455 | k->vmstate_change(qbus->parent, backend_run); | |
85cf2a8d MT |
1456 | } |
1457 | ||
1458 | if (!backend_run) { | |
1459 | virtio_set_status(vdev, vdev->status); | |
1460 | } | |
1461 | } | |
1462 | ||
c8075caf GA |
1463 | void virtio_instance_init_common(Object *proxy_obj, void *data, |
1464 | size_t vdev_size, const char *vdev_name) | |
1465 | { | |
1466 | DeviceState *vdev = data; | |
1467 | ||
1468 | object_initialize(vdev, vdev_size, vdev_name); | |
1469 | object_property_add_child(proxy_obj, "virtio-backend", OBJECT(vdev), NULL); | |
1470 | object_unref(OBJECT(vdev)); | |
1471 | qdev_alias_all_properties(vdev, proxy_obj); | |
1472 | } | |
1473 | ||
8e05db92 FK |
1474 | void virtio_init(VirtIODevice *vdev, const char *name, |
1475 | uint16_t device_id, size_t config_size) | |
967f97fa | 1476 | { |
e0d686bf JW |
1477 | BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); |
1478 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
b8193adb | 1479 | int i; |
e0d686bf JW |
1480 | int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0; |
1481 | ||
1482 | if (nvectors) { | |
1483 | vdev->vector_queues = | |
1484 | g_malloc0(sizeof(*vdev->vector_queues) * nvectors); | |
1485 | } | |
1486 | ||
53c25cea | 1487 | vdev->device_id = device_id; |
967f97fa AL |
1488 | vdev->status = 0; |
1489 | vdev->isr = 0; | |
1490 | vdev->queue_sel = 0; | |
7055e687 | 1491 | vdev->config_vector = VIRTIO_NO_VECTOR; |
87b3bd1c | 1492 | vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX); |
1354869c | 1493 | vdev->vm_running = runstate_is_running(); |
87b3bd1c | 1494 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { |
b8193adb | 1495 | vdev->vq[i].vector = VIRTIO_NO_VECTOR; |
1cbdabe2 | 1496 | vdev->vq[i].vdev = vdev; |
e78a2b42 | 1497 | vdev->vq[i].queue_index = i; |
1cbdabe2 | 1498 | } |
967f97fa | 1499 | |
967f97fa AL |
1500 | vdev->name = name; |
1501 | vdev->config_len = config_size; | |
8e05db92 | 1502 | if (vdev->config_len) { |
7267c094 | 1503 | vdev->config = g_malloc0(config_size); |
8e05db92 | 1504 | } else { |
967f97fa | 1505 | vdev->config = NULL; |
8e05db92 FK |
1506 | } |
1507 | vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change, | |
1508 | vdev); | |
616a6552 | 1509 | vdev->device_endian = virtio_default_endian(); |
8e05db92 | 1510 | } |
967f97fa | 1511 | |
a8170e5e | 1512 | hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1513 | { |
1514 | return vdev->vq[n].vring.desc; | |
1515 | } | |
1516 | ||
a8170e5e | 1517 | hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1518 | { |
1519 | return vdev->vq[n].vring.avail; | |
1520 | } | |
1521 | ||
a8170e5e | 1522 | hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1523 | { |
1524 | return vdev->vq[n].vring.used; | |
1525 | } | |
1526 | ||
a8170e5e | 1527 | hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1528 | { |
1529 | return vdev->vq[n].vring.desc; | |
1530 | } | |
1531 | ||
a8170e5e | 1532 | hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1533 | { |
1534 | return sizeof(VRingDesc) * vdev->vq[n].vring.num; | |
1535 | } | |
1536 | ||
a8170e5e | 1537 | hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1538 | { |
1539 | return offsetof(VRingAvail, ring) + | |
50764fc8 | 1540 | sizeof(uint16_t) * vdev->vq[n].vring.num; |
1cbdabe2 MT |
1541 | } |
1542 | ||
a8170e5e | 1543 | hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1544 | { |
1545 | return offsetof(VRingUsed, ring) + | |
1546 | sizeof(VRingUsedElem) * vdev->vq[n].vring.num; | |
1547 | } | |
1548 | ||
a8170e5e | 1549 | hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n) |
1cbdabe2 MT |
1550 | { |
1551 | return vdev->vq[n].vring.used - vdev->vq[n].vring.desc + | |
1552 | virtio_queue_get_used_size(vdev, n); | |
1553 | } | |
1554 | ||
1555 | uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) | |
1556 | { | |
1557 | return vdev->vq[n].last_avail_idx; | |
1558 | } | |
1559 | ||
1560 | void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx) | |
1561 | { | |
1562 | vdev->vq[n].last_avail_idx = idx; | |
1563 | } | |
1564 | ||
6793dfd1 SH |
1565 | void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n) |
1566 | { | |
1567 | vdev->vq[n].signalled_used_valid = false; | |
1568 | } | |
1569 | ||
1cbdabe2 MT |
1570 | VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n) |
1571 | { | |
1572 | return vdev->vq + n; | |
1573 | } | |
1574 | ||
e78a2b42 JW |
1575 | uint16_t virtio_get_queue_index(VirtQueue *vq) |
1576 | { | |
1577 | return vq->queue_index; | |
1578 | } | |
1579 | ||
15b2bd18 PB |
1580 | static void virtio_queue_guest_notifier_read(EventNotifier *n) |
1581 | { | |
1582 | VirtQueue *vq = container_of(n, VirtQueue, guest_notifier); | |
1583 | if (event_notifier_test_and_clear(n)) { | |
1584 | virtio_irq(vq); | |
1585 | } | |
1586 | } | |
1587 | ||
1588 | void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, | |
1589 | bool with_irqfd) | |
1590 | { | |
1591 | if (assign && !with_irqfd) { | |
1592 | event_notifier_set_handler(&vq->guest_notifier, | |
1593 | virtio_queue_guest_notifier_read); | |
1594 | } else { | |
1595 | event_notifier_set_handler(&vq->guest_notifier, NULL); | |
1596 | } | |
1597 | if (!assign) { | |
1598 | /* Test and clear notifier before closing it, | |
1599 | * in case poll callback didn't have time to run. */ | |
1600 | virtio_queue_guest_notifier_read(&vq->guest_notifier); | |
1601 | } | |
1602 | } | |
1603 | ||
1cbdabe2 MT |
1604 | EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq) |
1605 | { | |
1606 | return &vq->guest_notifier; | |
1607 | } | |
b1f416aa PB |
1608 | |
1609 | static void virtio_queue_host_notifier_read(EventNotifier *n) | |
1610 | { | |
1611 | VirtQueue *vq = container_of(n, VirtQueue, host_notifier); | |
1612 | if (event_notifier_test_and_clear(n)) { | |
1613 | virtio_queue_notify_vq(vq); | |
1614 | } | |
1615 | } | |
1616 | ||
26b9b5fe PB |
1617 | void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign, |
1618 | bool set_handler) | |
b1f416aa | 1619 | { |
26b9b5fe | 1620 | if (assign && set_handler) { |
b1f416aa PB |
1621 | event_notifier_set_handler(&vq->host_notifier, |
1622 | virtio_queue_host_notifier_read); | |
1623 | } else { | |
1624 | event_notifier_set_handler(&vq->host_notifier, NULL); | |
26b9b5fe PB |
1625 | } |
1626 | if (!assign) { | |
b1f416aa PB |
1627 | /* Test and clear notifier before after disabling event, |
1628 | * in case poll callback didn't have time to run. */ | |
1629 | virtio_queue_host_notifier_read(&vq->host_notifier); | |
1630 | } | |
1631 | } | |
1632 | ||
1cbdabe2 MT |
1633 | EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq) |
1634 | { | |
1635 | return &vq->host_notifier; | |
1636 | } | |
8e05db92 | 1637 | |
1034e9cf FK |
1638 | void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name) |
1639 | { | |
9e288406 | 1640 | g_free(vdev->bus_name); |
80e0090a | 1641 | vdev->bus_name = g_strdup(bus_name); |
1034e9cf FK |
1642 | } |
1643 | ||
1d244b42 AF |
1644 | static void virtio_device_realize(DeviceState *dev, Error **errp) |
1645 | { | |
1646 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
1647 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); | |
1648 | Error *err = NULL; | |
1649 | ||
1d244b42 AF |
1650 | if (vdc->realize != NULL) { |
1651 | vdc->realize(dev, &err); | |
1652 | if (err != NULL) { | |
1653 | error_propagate(errp, err); | |
1654 | return; | |
1655 | } | |
8e05db92 | 1656 | } |
e8398045 JW |
1657 | |
1658 | virtio_bus_device_plugged(vdev, &err); | |
1659 | if (err != NULL) { | |
1660 | error_propagate(errp, err); | |
1661 | return; | |
1662 | } | |
8e05db92 FK |
1663 | } |
1664 | ||
1d244b42 | 1665 | static void virtio_device_unrealize(DeviceState *dev, Error **errp) |
1034e9cf | 1666 | { |
1d244b42 | 1667 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
306ec6c3 AF |
1668 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); |
1669 | Error *err = NULL; | |
1d244b42 | 1670 | |
83d07047 PB |
1671 | virtio_bus_device_unplugged(vdev); |
1672 | ||
306ec6c3 AF |
1673 | if (vdc->unrealize != NULL) { |
1674 | vdc->unrealize(dev, &err); | |
1675 | if (err != NULL) { | |
1676 | error_propagate(errp, err); | |
1677 | return; | |
1678 | } | |
5e96f5d2 | 1679 | } |
1d244b42 | 1680 | |
9e288406 MA |
1681 | g_free(vdev->bus_name); |
1682 | vdev->bus_name = NULL; | |
1034e9cf FK |
1683 | } |
1684 | ||
6b8f1020 CH |
1685 | static Property virtio_properties[] = { |
1686 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features), | |
1687 | DEFINE_PROP_END_OF_LIST(), | |
1688 | }; | |
1689 | ||
8e05db92 FK |
1690 | static void virtio_device_class_init(ObjectClass *klass, void *data) |
1691 | { | |
1692 | /* Set the default value here. */ | |
1693 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1d244b42 AF |
1694 | |
1695 | dc->realize = virtio_device_realize; | |
1696 | dc->unrealize = virtio_device_unrealize; | |
8e05db92 | 1697 | dc->bus_type = TYPE_VIRTIO_BUS; |
6b8f1020 | 1698 | dc->props = virtio_properties; |
8e05db92 FK |
1699 | } |
1700 | ||
1701 | static const TypeInfo virtio_device_info = { | |
1702 | .name = TYPE_VIRTIO_DEVICE, | |
1703 | .parent = TYPE_DEVICE, | |
1704 | .instance_size = sizeof(VirtIODevice), | |
1705 | .class_init = virtio_device_class_init, | |
1706 | .abstract = true, | |
1707 | .class_size = sizeof(VirtioDeviceClass), | |
1708 | }; | |
1709 | ||
1710 | static void virtio_register_types(void) | |
1711 | { | |
1712 | type_register_static(&virtio_device_info); | |
1713 | } | |
1714 | ||
1715 | type_init(virtio_register_types) |