]>
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" |
ce67ed65 | 17 | #include "qemu-error.h" |
967f97fa | 18 | #include "virtio.h" |
b90d2f35 | 19 | #include "qemu-barrier.h" |
967f97fa | 20 | |
f46f15bc AL |
21 | /* The alignment to use between consumer and producer parts of vring. |
22 | * x86 pagesize again. */ | |
23 | #define VIRTIO_PCI_VRING_ALIGN 4096 | |
24 | ||
967f97fa AL |
25 | typedef struct VRingDesc |
26 | { | |
27 | uint64_t addr; | |
28 | uint32_t len; | |
29 | uint16_t flags; | |
30 | uint16_t next; | |
31 | } VRingDesc; | |
32 | ||
33 | typedef struct VRingAvail | |
34 | { | |
35 | uint16_t flags; | |
36 | uint16_t idx; | |
37 | uint16_t ring[0]; | |
38 | } VRingAvail; | |
39 | ||
40 | typedef struct VRingUsedElem | |
41 | { | |
42 | uint32_t id; | |
43 | uint32_t len; | |
44 | } VRingUsedElem; | |
45 | ||
46 | typedef struct VRingUsed | |
47 | { | |
48 | uint16_t flags; | |
49 | uint16_t idx; | |
50 | VRingUsedElem ring[0]; | |
51 | } VRingUsed; | |
52 | ||
53 | typedef struct VRing | |
54 | { | |
55 | unsigned int num; | |
c227f099 AL |
56 | target_phys_addr_t desc; |
57 | target_phys_addr_t avail; | |
58 | target_phys_addr_t used; | |
967f97fa AL |
59 | } VRing; |
60 | ||
61 | struct VirtQueue | |
62 | { | |
63 | VRing vring; | |
c227f099 | 64 | target_phys_addr_t pa; |
967f97fa | 65 | uint16_t last_avail_idx; |
bcbabae8 MT |
66 | /* Last used index value we have signalled on */ |
67 | uint16_t signalled_used; | |
68 | ||
69 | /* Last used index value we have signalled on */ | |
70 | bool signalled_used_valid; | |
71 | ||
72 | /* Notification enabled? */ | |
73 | bool notification; | |
74 | ||
967f97fa | 75 | int inuse; |
bcbabae8 | 76 | |
7055e687 | 77 | uint16_t vector; |
967f97fa | 78 | void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq); |
1cbdabe2 MT |
79 | VirtIODevice *vdev; |
80 | EventNotifier guest_notifier; | |
81 | EventNotifier host_notifier; | |
967f97fa AL |
82 | }; |
83 | ||
967f97fa | 84 | /* virt queue functions */ |
53c25cea | 85 | static void virtqueue_init(VirtQueue *vq) |
967f97fa | 86 | { |
c227f099 | 87 | target_phys_addr_t pa = vq->pa; |
53c25cea | 88 | |
967f97fa AL |
89 | vq->vring.desc = pa; |
90 | vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc); | |
f46f15bc AL |
91 | vq->vring.used = vring_align(vq->vring.avail + |
92 | offsetof(VRingAvail, ring[vq->vring.num]), | |
93 | VIRTIO_PCI_VRING_ALIGN); | |
967f97fa AL |
94 | } |
95 | ||
c227f099 | 96 | static inline uint64_t vring_desc_addr(target_phys_addr_t desc_pa, int i) |
967f97fa | 97 | { |
c227f099 | 98 | target_phys_addr_t pa; |
5774cf98 | 99 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr); |
967f97fa AL |
100 | return ldq_phys(pa); |
101 | } | |
102 | ||
c227f099 | 103 | static inline uint32_t vring_desc_len(target_phys_addr_t desc_pa, int i) |
967f97fa | 104 | { |
c227f099 | 105 | target_phys_addr_t pa; |
5774cf98 | 106 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len); |
967f97fa AL |
107 | return ldl_phys(pa); |
108 | } | |
109 | ||
c227f099 | 110 | static inline uint16_t vring_desc_flags(target_phys_addr_t desc_pa, int i) |
967f97fa | 111 | { |
c227f099 | 112 | target_phys_addr_t pa; |
5774cf98 | 113 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags); |
967f97fa AL |
114 | return lduw_phys(pa); |
115 | } | |
116 | ||
c227f099 | 117 | static inline uint16_t vring_desc_next(target_phys_addr_t desc_pa, int i) |
967f97fa | 118 | { |
c227f099 | 119 | target_phys_addr_t pa; |
5774cf98 | 120 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next); |
967f97fa AL |
121 | return lduw_phys(pa); |
122 | } | |
123 | ||
124 | static inline uint16_t vring_avail_flags(VirtQueue *vq) | |
125 | { | |
c227f099 | 126 | target_phys_addr_t pa; |
967f97fa AL |
127 | pa = vq->vring.avail + offsetof(VRingAvail, flags); |
128 | return lduw_phys(pa); | |
129 | } | |
130 | ||
131 | static inline uint16_t vring_avail_idx(VirtQueue *vq) | |
132 | { | |
c227f099 | 133 | target_phys_addr_t pa; |
967f97fa AL |
134 | pa = vq->vring.avail + offsetof(VRingAvail, idx); |
135 | return lduw_phys(pa); | |
136 | } | |
137 | ||
138 | static inline uint16_t vring_avail_ring(VirtQueue *vq, int i) | |
139 | { | |
c227f099 | 140 | target_phys_addr_t pa; |
967f97fa AL |
141 | pa = vq->vring.avail + offsetof(VRingAvail, ring[i]); |
142 | return lduw_phys(pa); | |
143 | } | |
144 | ||
bcbabae8 MT |
145 | static inline uint16_t vring_used_event(VirtQueue *vq) |
146 | { | |
147 | return vring_avail_ring(vq, vq->vring.num); | |
148 | } | |
149 | ||
967f97fa AL |
150 | static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val) |
151 | { | |
c227f099 | 152 | target_phys_addr_t pa; |
967f97fa AL |
153 | pa = vq->vring.used + offsetof(VRingUsed, ring[i].id); |
154 | stl_phys(pa, val); | |
155 | } | |
156 | ||
157 | static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val) | |
158 | { | |
c227f099 | 159 | target_phys_addr_t pa; |
967f97fa AL |
160 | pa = vq->vring.used + offsetof(VRingUsed, ring[i].len); |
161 | stl_phys(pa, val); | |
162 | } | |
163 | ||
164 | static uint16_t vring_used_idx(VirtQueue *vq) | |
165 | { | |
c227f099 | 166 | target_phys_addr_t pa; |
967f97fa AL |
167 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
168 | return lduw_phys(pa); | |
169 | } | |
170 | ||
bcbabae8 | 171 | static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val) |
967f97fa | 172 | { |
c227f099 | 173 | target_phys_addr_t pa; |
967f97fa | 174 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
bcbabae8 | 175 | stw_phys(pa, val); |
967f97fa AL |
176 | } |
177 | ||
178 | static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask) | |
179 | { | |
c227f099 | 180 | target_phys_addr_t pa; |
967f97fa AL |
181 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
182 | stw_phys(pa, lduw_phys(pa) | mask); | |
183 | } | |
184 | ||
185 | static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask) | |
186 | { | |
c227f099 | 187 | target_phys_addr_t pa; |
967f97fa AL |
188 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
189 | stw_phys(pa, lduw_phys(pa) & ~mask); | |
190 | } | |
191 | ||
bcbabae8 MT |
192 | static inline void vring_avail_event(VirtQueue *vq, uint16_t val) |
193 | { | |
194 | target_phys_addr_t pa; | |
195 | if (!vq->notification) { | |
196 | return; | |
197 | } | |
198 | pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]); | |
199 | stw_phys(pa, val); | |
200 | } | |
201 | ||
967f97fa AL |
202 | void virtio_queue_set_notification(VirtQueue *vq, int enable) |
203 | { | |
bcbabae8 MT |
204 | vq->notification = enable; |
205 | if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) { | |
206 | vring_avail_event(vq, vring_avail_idx(vq)); | |
207 | } else if (enable) { | |
967f97fa | 208 | vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 209 | } else { |
967f97fa | 210 | vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY); |
bcbabae8 | 211 | } |
967f97fa AL |
212 | } |
213 | ||
214 | int virtio_queue_ready(VirtQueue *vq) | |
215 | { | |
216 | return vq->vring.avail != 0; | |
217 | } | |
218 | ||
219 | int virtio_queue_empty(VirtQueue *vq) | |
220 | { | |
221 | return vring_avail_idx(vq) == vq->last_avail_idx; | |
222 | } | |
223 | ||
224 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, | |
225 | unsigned int len, unsigned int idx) | |
226 | { | |
227 | unsigned int offset; | |
228 | int i; | |
229 | ||
64979a4d SH |
230 | trace_virtqueue_fill(vq, elem, len, idx); |
231 | ||
967f97fa AL |
232 | offset = 0; |
233 | for (i = 0; i < elem->in_num; i++) { | |
234 | size_t size = MIN(len - offset, elem->in_sg[i].iov_len); | |
235 | ||
26b258e1 AL |
236 | cpu_physical_memory_unmap(elem->in_sg[i].iov_base, |
237 | elem->in_sg[i].iov_len, | |
238 | 1, size); | |
967f97fa | 239 | |
26b258e1 | 240 | offset += elem->in_sg[i].iov_len; |
967f97fa AL |
241 | } |
242 | ||
26b258e1 AL |
243 | for (i = 0; i < elem->out_num; i++) |
244 | cpu_physical_memory_unmap(elem->out_sg[i].iov_base, | |
245 | elem->out_sg[i].iov_len, | |
246 | 0, elem->out_sg[i].iov_len); | |
247 | ||
967f97fa AL |
248 | idx = (idx + vring_used_idx(vq)) % vq->vring.num; |
249 | ||
250 | /* Get a pointer to the next entry in the used ring. */ | |
251 | vring_used_ring_id(vq, idx, elem->index); | |
252 | vring_used_ring_len(vq, idx, len); | |
253 | } | |
254 | ||
255 | void virtqueue_flush(VirtQueue *vq, unsigned int count) | |
256 | { | |
bcbabae8 | 257 | uint16_t old, new; |
967f97fa | 258 | /* Make sure buffer is written before we update index. */ |
b90d2f35 | 259 | smp_wmb(); |
64979a4d | 260 | trace_virtqueue_flush(vq, count); |
bcbabae8 MT |
261 | old = vring_used_idx(vq); |
262 | new = old + count; | |
263 | vring_used_idx_set(vq, new); | |
967f97fa | 264 | vq->inuse -= count; |
bcbabae8 MT |
265 | if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))) |
266 | vq->signalled_used_valid = false; | |
967f97fa AL |
267 | } |
268 | ||
269 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, | |
270 | unsigned int len) | |
271 | { | |
272 | virtqueue_fill(vq, elem, len, 0); | |
273 | virtqueue_flush(vq, 1); | |
274 | } | |
275 | ||
276 | static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx) | |
277 | { | |
278 | uint16_t num_heads = vring_avail_idx(vq) - idx; | |
279 | ||
280 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
bb6834cf | 281 | if (num_heads > vq->vring.num) { |
ce67ed65 SH |
282 | error_report("Guest moved used index from %u to %u", |
283 | idx, vring_avail_idx(vq)); | |
bb6834cf AL |
284 | exit(1); |
285 | } | |
967f97fa AL |
286 | |
287 | return num_heads; | |
288 | } | |
289 | ||
290 | static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx) | |
291 | { | |
292 | unsigned int head; | |
293 | ||
294 | /* Grab the next descriptor number they're advertising, and increment | |
295 | * the index we've seen. */ | |
296 | head = vring_avail_ring(vq, idx % vq->vring.num); | |
297 | ||
298 | /* If their number is silly, that's a fatal mistake. */ | |
bb6834cf | 299 | if (head >= vq->vring.num) { |
ce67ed65 | 300 | error_report("Guest says index %u is available", head); |
bb6834cf AL |
301 | exit(1); |
302 | } | |
967f97fa AL |
303 | |
304 | return head; | |
305 | } | |
306 | ||
c227f099 | 307 | static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa, |
5774cf98 | 308 | unsigned int i, unsigned int max) |
967f97fa AL |
309 | { |
310 | unsigned int next; | |
311 | ||
312 | /* If this descriptor says it doesn't chain, we're done. */ | |
5774cf98 MM |
313 | if (!(vring_desc_flags(desc_pa, i) & VRING_DESC_F_NEXT)) |
314 | return max; | |
967f97fa AL |
315 | |
316 | /* Check they're not leading us off end of descriptors. */ | |
5774cf98 | 317 | next = vring_desc_next(desc_pa, i); |
967f97fa | 318 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
b90d2f35 | 319 | smp_wmb(); |
967f97fa | 320 | |
5774cf98 | 321 | if (next >= max) { |
ce67ed65 | 322 | error_report("Desc next is %u", next); |
bb6834cf AL |
323 | exit(1); |
324 | } | |
967f97fa AL |
325 | |
326 | return next; | |
327 | } | |
328 | ||
329 | int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes) | |
330 | { | |
efeea6d0 MM |
331 | unsigned int idx; |
332 | int total_bufs, in_total, out_total; | |
967f97fa AL |
333 | |
334 | idx = vq->last_avail_idx; | |
335 | ||
efeea6d0 | 336 | total_bufs = in_total = out_total = 0; |
967f97fa | 337 | while (virtqueue_num_heads(vq, idx)) { |
efeea6d0 | 338 | unsigned int max, num_bufs, indirect = 0; |
c227f099 | 339 | target_phys_addr_t desc_pa; |
967f97fa AL |
340 | int i; |
341 | ||
efeea6d0 MM |
342 | max = vq->vring.num; |
343 | num_bufs = total_bufs; | |
967f97fa | 344 | i = virtqueue_get_head(vq, idx++); |
efeea6d0 MM |
345 | desc_pa = vq->vring.desc; |
346 | ||
347 | if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) { | |
348 | if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) { | |
ce67ed65 | 349 | error_report("Invalid size for indirect buffer table"); |
efeea6d0 MM |
350 | exit(1); |
351 | } | |
352 | ||
353 | /* If we've got too many, that implies a descriptor loop. */ | |
354 | if (num_bufs >= max) { | |
ce67ed65 | 355 | error_report("Looped descriptor"); |
efeea6d0 MM |
356 | exit(1); |
357 | } | |
358 | ||
359 | /* loop over the indirect descriptor table */ | |
360 | indirect = 1; | |
361 | max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc); | |
362 | num_bufs = i = 0; | |
363 | desc_pa = vring_desc_addr(desc_pa, i); | |
364 | } | |
365 | ||
967f97fa AL |
366 | do { |
367 | /* If we've got too many, that implies a descriptor loop. */ | |
5774cf98 | 368 | if (++num_bufs > max) { |
ce67ed65 | 369 | error_report("Looped descriptor"); |
bb6834cf AL |
370 | exit(1); |
371 | } | |
967f97fa | 372 | |
5774cf98 | 373 | if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) { |
967f97fa | 374 | if (in_bytes > 0 && |
5774cf98 | 375 | (in_total += vring_desc_len(desc_pa, i)) >= in_bytes) |
967f97fa AL |
376 | return 1; |
377 | } else { | |
378 | if (out_bytes > 0 && | |
5774cf98 | 379 | (out_total += vring_desc_len(desc_pa, i)) >= out_bytes) |
967f97fa AL |
380 | return 1; |
381 | } | |
5774cf98 | 382 | } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max); |
efeea6d0 MM |
383 | |
384 | if (!indirect) | |
385 | total_bufs = num_bufs; | |
386 | else | |
387 | total_bufs++; | |
967f97fa AL |
388 | } |
389 | ||
390 | return 0; | |
391 | } | |
392 | ||
42fb2e07 KW |
393 | void virtqueue_map_sg(struct iovec *sg, target_phys_addr_t *addr, |
394 | size_t num_sg, int is_write) | |
395 | { | |
396 | unsigned int i; | |
397 | target_phys_addr_t len; | |
398 | ||
399 | for (i = 0; i < num_sg; i++) { | |
400 | len = sg[i].iov_len; | |
401 | sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write); | |
402 | if (sg[i].iov_base == NULL || len != sg[i].iov_len) { | |
ce67ed65 | 403 | error_report("virtio: trying to map MMIO memory"); |
42fb2e07 KW |
404 | exit(1); |
405 | } | |
406 | } | |
407 | } | |
408 | ||
967f97fa AL |
409 | int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem) |
410 | { | |
5774cf98 | 411 | unsigned int i, head, max; |
c227f099 | 412 | target_phys_addr_t desc_pa = vq->vring.desc; |
967f97fa AL |
413 | |
414 | if (!virtqueue_num_heads(vq, vq->last_avail_idx)) | |
415 | return 0; | |
416 | ||
417 | /* When we start there are none of either input nor output. */ | |
418 | elem->out_num = elem->in_num = 0; | |
419 | ||
5774cf98 MM |
420 | max = vq->vring.num; |
421 | ||
967f97fa | 422 | i = head = virtqueue_get_head(vq, vq->last_avail_idx++); |
bcbabae8 MT |
423 | if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) { |
424 | vring_avail_event(vq, vring_avail_idx(vq)); | |
425 | } | |
efeea6d0 MM |
426 | |
427 | if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) { | |
428 | if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) { | |
ce67ed65 | 429 | error_report("Invalid size for indirect buffer table"); |
efeea6d0 MM |
430 | exit(1); |
431 | } | |
432 | ||
433 | /* loop over the indirect descriptor table */ | |
434 | max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc); | |
435 | desc_pa = vring_desc_addr(desc_pa, i); | |
436 | i = 0; | |
437 | } | |
438 | ||
42fb2e07 | 439 | /* Collect all the descriptors */ |
967f97fa AL |
440 | do { |
441 | struct iovec *sg; | |
442 | ||
5774cf98 | 443 | if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) { |
c8eac1cf MT |
444 | if (elem->in_num >= ARRAY_SIZE(elem->in_sg)) { |
445 | error_report("Too many write descriptors in indirect table"); | |
446 | exit(1); | |
447 | } | |
5774cf98 | 448 | elem->in_addr[elem->in_num] = vring_desc_addr(desc_pa, i); |
967f97fa | 449 | sg = &elem->in_sg[elem->in_num++]; |
42fb2e07 | 450 | } else { |
c8eac1cf MT |
451 | if (elem->out_num >= ARRAY_SIZE(elem->out_sg)) { |
452 | error_report("Too many read descriptors in indirect table"); | |
453 | exit(1); | |
454 | } | |
42fb2e07 | 455 | elem->out_addr[elem->out_num] = vring_desc_addr(desc_pa, i); |
967f97fa | 456 | sg = &elem->out_sg[elem->out_num++]; |
42fb2e07 | 457 | } |
967f97fa | 458 | |
5774cf98 | 459 | sg->iov_len = vring_desc_len(desc_pa, i); |
967f97fa AL |
460 | |
461 | /* If we've got too many, that implies a descriptor loop. */ | |
5774cf98 | 462 | if ((elem->in_num + elem->out_num) > max) { |
ce67ed65 | 463 | error_report("Looped descriptor"); |
bb6834cf AL |
464 | exit(1); |
465 | } | |
5774cf98 | 466 | } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max); |
967f97fa | 467 | |
42fb2e07 KW |
468 | /* Now map what we have collected */ |
469 | virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1); | |
470 | virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0); | |
471 | ||
967f97fa AL |
472 | elem->index = head; |
473 | ||
474 | vq->inuse++; | |
475 | ||
64979a4d | 476 | trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); |
967f97fa AL |
477 | return elem->in_num + elem->out_num; |
478 | } | |
479 | ||
480 | /* virtio device */ | |
7055e687 MT |
481 | static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector) |
482 | { | |
483 | if (vdev->binding->notify) { | |
484 | vdev->binding->notify(vdev->binding_opaque, vector); | |
485 | } | |
486 | } | |
967f97fa | 487 | |
53c25cea | 488 | void virtio_update_irq(VirtIODevice *vdev) |
967f97fa | 489 | { |
7055e687 | 490 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
967f97fa AL |
491 | } |
492 | ||
4e1837f8 SH |
493 | void virtio_set_status(VirtIODevice *vdev, uint8_t val) |
494 | { | |
495 | trace_virtio_set_status(vdev, val); | |
496 | ||
497 | if (vdev->set_status) { | |
498 | vdev->set_status(vdev, val); | |
499 | } | |
500 | vdev->status = val; | |
501 | } | |
502 | ||
53c25cea | 503 | void virtio_reset(void *opaque) |
967f97fa AL |
504 | { |
505 | VirtIODevice *vdev = opaque; | |
506 | int i; | |
507 | ||
e0c472d8 MT |
508 | virtio_set_status(vdev, 0); |
509 | ||
967f97fa AL |
510 | if (vdev->reset) |
511 | vdev->reset(vdev); | |
512 | ||
704a76fc | 513 | vdev->guest_features = 0; |
967f97fa AL |
514 | vdev->queue_sel = 0; |
515 | vdev->status = 0; | |
516 | vdev->isr = 0; | |
7055e687 MT |
517 | vdev->config_vector = VIRTIO_NO_VECTOR; |
518 | virtio_notify_vector(vdev, vdev->config_vector); | |
967f97fa AL |
519 | |
520 | for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
521 | vdev->vq[i].vring.desc = 0; | |
522 | vdev->vq[i].vring.avail = 0; | |
523 | vdev->vq[i].vring.used = 0; | |
524 | vdev->vq[i].last_avail_idx = 0; | |
53c25cea | 525 | vdev->vq[i].pa = 0; |
7055e687 | 526 | vdev->vq[i].vector = VIRTIO_NO_VECTOR; |
bcbabae8 MT |
527 | vdev->vq[i].signalled_used = 0; |
528 | vdev->vq[i].signalled_used_valid = false; | |
529 | vdev->vq[i].notification = true; | |
967f97fa AL |
530 | } |
531 | } | |
532 | ||
53c25cea | 533 | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 534 | { |
967f97fa AL |
535 | uint8_t val; |
536 | ||
537 | vdev->get_config(vdev, vdev->config); | |
538 | ||
967f97fa AL |
539 | if (addr > (vdev->config_len - sizeof(val))) |
540 | return (uint32_t)-1; | |
541 | ||
542 | memcpy(&val, vdev->config + addr, sizeof(val)); | |
543 | return val; | |
544 | } | |
545 | ||
53c25cea | 546 | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 547 | { |
967f97fa AL |
548 | uint16_t val; |
549 | ||
550 | vdev->get_config(vdev, vdev->config); | |
551 | ||
967f97fa AL |
552 | if (addr > (vdev->config_len - sizeof(val))) |
553 | return (uint32_t)-1; | |
554 | ||
555 | memcpy(&val, vdev->config + addr, sizeof(val)); | |
556 | return val; | |
557 | } | |
558 | ||
53c25cea | 559 | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr) |
967f97fa | 560 | { |
967f97fa AL |
561 | uint32_t val; |
562 | ||
563 | vdev->get_config(vdev, vdev->config); | |
564 | ||
967f97fa AL |
565 | if (addr > (vdev->config_len - sizeof(val))) |
566 | return (uint32_t)-1; | |
567 | ||
568 | memcpy(&val, vdev->config + addr, sizeof(val)); | |
569 | return val; | |
570 | } | |
571 | ||
53c25cea | 572 | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 573 | { |
967f97fa AL |
574 | uint8_t val = data; |
575 | ||
967f97fa AL |
576 | if (addr > (vdev->config_len - sizeof(val))) |
577 | return; | |
578 | ||
579 | memcpy(vdev->config + addr, &val, sizeof(val)); | |
580 | ||
581 | if (vdev->set_config) | |
582 | vdev->set_config(vdev, vdev->config); | |
583 | } | |
584 | ||
53c25cea | 585 | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 586 | { |
967f97fa AL |
587 | uint16_t val = data; |
588 | ||
967f97fa AL |
589 | if (addr > (vdev->config_len - sizeof(val))) |
590 | return; | |
591 | ||
592 | memcpy(vdev->config + addr, &val, sizeof(val)); | |
593 | ||
594 | if (vdev->set_config) | |
595 | vdev->set_config(vdev, vdev->config); | |
596 | } | |
597 | ||
53c25cea | 598 | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
967f97fa | 599 | { |
967f97fa AL |
600 | uint32_t val = data; |
601 | ||
967f97fa AL |
602 | if (addr > (vdev->config_len - sizeof(val))) |
603 | return; | |
604 | ||
605 | memcpy(vdev->config + addr, &val, sizeof(val)); | |
606 | ||
607 | if (vdev->set_config) | |
608 | vdev->set_config(vdev, vdev->config); | |
609 | } | |
610 | ||
c227f099 | 611 | void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr) |
967f97fa | 612 | { |
7055e687 MT |
613 | vdev->vq[n].pa = addr; |
614 | virtqueue_init(&vdev->vq[n]); | |
53c25cea PB |
615 | } |
616 | ||
c227f099 | 617 | target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n) |
53c25cea PB |
618 | { |
619 | return vdev->vq[n].pa; | |
620 | } | |
621 | ||
622 | int virtio_queue_get_num(VirtIODevice *vdev, int n) | |
623 | { | |
624 | return vdev->vq[n].vring.num; | |
625 | } | |
967f97fa | 626 | |
25db9ebe SH |
627 | void virtio_queue_notify_vq(VirtQueue *vq) |
628 | { | |
629 | if (vq->vring.desc) { | |
630 | VirtIODevice *vdev = vq->vdev; | |
631 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); | |
632 | vq->handle_output(vdev, vq); | |
633 | } | |
634 | } | |
635 | ||
53c25cea PB |
636 | void virtio_queue_notify(VirtIODevice *vdev, int n) |
637 | { | |
7157e2e2 | 638 | virtio_queue_notify_vq(&vdev->vq[n]); |
967f97fa AL |
639 | } |
640 | ||
7055e687 MT |
641 | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n) |
642 | { | |
643 | return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector : | |
644 | VIRTIO_NO_VECTOR; | |
645 | } | |
646 | ||
647 | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector) | |
648 | { | |
649 | if (n < VIRTIO_PCI_QUEUE_MAX) | |
650 | vdev->vq[n].vector = vector; | |
651 | } | |
652 | ||
967f97fa AL |
653 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, |
654 | void (*handle_output)(VirtIODevice *, VirtQueue *)) | |
655 | { | |
656 | int i; | |
657 | ||
658 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
659 | if (vdev->vq[i].vring.num == 0) | |
660 | break; | |
661 | } | |
662 | ||
663 | if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE) | |
664 | abort(); | |
665 | ||
666 | vdev->vq[i].vring.num = queue_size; | |
667 | vdev->vq[i].handle_output = handle_output; | |
668 | ||
669 | return &vdev->vq[i]; | |
670 | } | |
671 | ||
1cbdabe2 MT |
672 | void virtio_irq(VirtQueue *vq) |
673 | { | |
64979a4d | 674 | trace_virtio_irq(vq); |
1cbdabe2 MT |
675 | vq->vdev->isr |= 0x01; |
676 | virtio_notify_vector(vq->vdev, vq->vector); | |
677 | } | |
678 | ||
bcbabae8 MT |
679 | /* Assuming a given event_idx value from the other size, if |
680 | * we have just incremented index from old to new_idx, | |
681 | * should we trigger an event? */ | |
682 | static inline int vring_need_event(uint16_t event, uint16_t new, uint16_t old) | |
967f97fa | 683 | { |
bcbabae8 MT |
684 | /* Note: Xen has similar logic for notification hold-off |
685 | * in include/xen/interface/io/ring.h with req_event and req_prod | |
686 | * corresponding to event_idx + 1 and new respectively. | |
687 | * Note also that req_event and req_prod in Xen start at 1, | |
688 | * event indexes in virtio start at 0. */ | |
689 | return (uint16_t)(new - event - 1) < (uint16_t)(new - old); | |
690 | } | |
691 | ||
692 | static bool vring_notify(VirtIODevice *vdev, VirtQueue *vq) | |
693 | { | |
694 | uint16_t old, new; | |
695 | bool v; | |
97b83deb | 696 | /* Always notify when queue is empty (when feature acknowledge) */ |
bcbabae8 MT |
697 | if (((vdev->guest_features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) && |
698 | !vq->inuse && vring_avail_idx(vq) == vq->last_avail_idx)) { | |
699 | return true; | |
700 | } | |
701 | ||
702 | if (!(vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX))) { | |
703 | return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT); | |
704 | } | |
705 | ||
706 | v = vq->signalled_used_valid; | |
707 | vq->signalled_used_valid = true; | |
708 | old = vq->signalled_used; | |
709 | new = vq->signalled_used = vring_used_idx(vq); | |
710 | return !v || vring_need_event(vring_used_event(vq), new, old); | |
711 | } | |
712 | ||
713 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) | |
714 | { | |
715 | if (!vring_notify(vdev, vq)) { | |
967f97fa | 716 | return; |
bcbabae8 | 717 | } |
967f97fa | 718 | |
64979a4d | 719 | trace_virtio_notify(vdev, vq); |
967f97fa | 720 | vdev->isr |= 0x01; |
7055e687 | 721 | virtio_notify_vector(vdev, vq->vector); |
967f97fa AL |
722 | } |
723 | ||
724 | void virtio_notify_config(VirtIODevice *vdev) | |
725 | { | |
7625162c AL |
726 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) |
727 | return; | |
728 | ||
967f97fa | 729 | vdev->isr |= 0x03; |
7055e687 | 730 | virtio_notify_vector(vdev, vdev->config_vector); |
967f97fa AL |
731 | } |
732 | ||
733 | void virtio_save(VirtIODevice *vdev, QEMUFile *f) | |
734 | { | |
735 | int i; | |
736 | ||
ff24bd58 MT |
737 | if (vdev->binding->save_config) |
738 | vdev->binding->save_config(vdev->binding_opaque, f); | |
967f97fa | 739 | |
967f97fa AL |
740 | qemu_put_8s(f, &vdev->status); |
741 | qemu_put_8s(f, &vdev->isr); | |
742 | qemu_put_be16s(f, &vdev->queue_sel); | |
704a76fc | 743 | qemu_put_be32s(f, &vdev->guest_features); |
967f97fa AL |
744 | qemu_put_be32(f, vdev->config_len); |
745 | qemu_put_buffer(f, vdev->config, vdev->config_len); | |
746 | ||
747 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
748 | if (vdev->vq[i].vring.num == 0) | |
749 | break; | |
750 | } | |
751 | ||
752 | qemu_put_be32(f, i); | |
753 | ||
754 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { | |
755 | if (vdev->vq[i].vring.num == 0) | |
756 | break; | |
757 | ||
758 | qemu_put_be32(f, vdev->vq[i].vring.num); | |
53c25cea | 759 | qemu_put_be64(f, vdev->vq[i].pa); |
967f97fa | 760 | qemu_put_be16s(f, &vdev->vq[i].last_avail_idx); |
ff24bd58 MT |
761 | if (vdev->binding->save_queue) |
762 | vdev->binding->save_queue(vdev->binding_opaque, i, f); | |
967f97fa AL |
763 | } |
764 | } | |
765 | ||
ff24bd58 | 766 | int virtio_load(VirtIODevice *vdev, QEMUFile *f) |
967f97fa | 767 | { |
ff24bd58 | 768 | int num, i, ret; |
6d74ca5a | 769 | uint32_t features; |
8172539d | 770 | uint32_t supported_features = |
6d74ca5a | 771 | vdev->binding->get_features(vdev->binding_opaque); |
967f97fa | 772 | |
ff24bd58 MT |
773 | if (vdev->binding->load_config) { |
774 | ret = vdev->binding->load_config(vdev->binding_opaque, f); | |
775 | if (ret) | |
776 | return ret; | |
777 | } | |
967f97fa | 778 | |
967f97fa AL |
779 | qemu_get_8s(f, &vdev->status); |
780 | qemu_get_8s(f, &vdev->isr); | |
781 | qemu_get_be16s(f, &vdev->queue_sel); | |
6d74ca5a MT |
782 | qemu_get_be32s(f, &features); |
783 | if (features & ~supported_features) { | |
ce67ed65 SH |
784 | error_report("Features 0x%x unsupported. Allowed features: 0x%x", |
785 | features, supported_features); | |
6d74ca5a MT |
786 | return -1; |
787 | } | |
fae054b0 MT |
788 | if (vdev->set_features) |
789 | vdev->set_features(vdev, features); | |
704a76fc | 790 | vdev->guest_features = features; |
967f97fa AL |
791 | vdev->config_len = qemu_get_be32(f); |
792 | qemu_get_buffer(f, vdev->config, vdev->config_len); | |
793 | ||
794 | num = qemu_get_be32(f); | |
795 | ||
796 | for (i = 0; i < num; i++) { | |
797 | vdev->vq[i].vring.num = qemu_get_be32(f); | |
53c25cea | 798 | vdev->vq[i].pa = qemu_get_be64(f); |
967f97fa | 799 | qemu_get_be16s(f, &vdev->vq[i].last_avail_idx); |
bcbabae8 MT |
800 | vdev->vq[i].signalled_used_valid = false; |
801 | vdev->vq[i].notification = true; | |
967f97fa | 802 | |
53c25cea | 803 | if (vdev->vq[i].pa) { |
1abeb5a6 | 804 | uint16_t nheads; |
53c25cea | 805 | virtqueue_init(&vdev->vq[i]); |
1abeb5a6 MT |
806 | nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx; |
807 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
808 | if (nheads > vdev->vq[i].vring.num) { | |
809 | error_report("VQ %d size 0x%x Guest index 0x%x " | |
6daf194d | 810 | "inconsistent with Host index 0x%x: delta 0x%x", |
1abeb5a6 MT |
811 | i, vdev->vq[i].vring.num, |
812 | vring_avail_idx(&vdev->vq[i]), | |
813 | vdev->vq[i].last_avail_idx, nheads); | |
814 | return -1; | |
815 | } | |
816 | } else if (vdev->vq[i].last_avail_idx) { | |
817 | error_report("VQ %d address 0x0 " | |
6daf194d | 818 | "inconsistent with Host index 0x%x", |
1abeb5a6 MT |
819 | i, vdev->vq[i].last_avail_idx); |
820 | return -1; | |
258dc7c9 | 821 | } |
ff24bd58 MT |
822 | if (vdev->binding->load_queue) { |
823 | ret = vdev->binding->load_queue(vdev->binding_opaque, i, f); | |
824 | if (ret) | |
825 | return ret; | |
7055e687 | 826 | } |
967f97fa AL |
827 | } |
828 | ||
7055e687 | 829 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
ff24bd58 | 830 | return 0; |
967f97fa AL |
831 | } |
832 | ||
b946a153 AL |
833 | void virtio_cleanup(VirtIODevice *vdev) |
834 | { | |
85cf2a8d | 835 | qemu_del_vm_change_state_handler(vdev->vmstate); |
b946a153 | 836 | if (vdev->config) |
7267c094 AL |
837 | g_free(vdev->config); |
838 | g_free(vdev->vq); | |
839 | g_free(vdev); | |
b946a153 AL |
840 | } |
841 | ||
1dfb4dd9 | 842 | static void virtio_vmstate_change(void *opaque, int running, RunState state) |
85cf2a8d MT |
843 | { |
844 | VirtIODevice *vdev = opaque; | |
845 | bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK); | |
846 | vdev->vm_running = running; | |
847 | ||
848 | if (backend_run) { | |
849 | virtio_set_status(vdev, vdev->status); | |
850 | } | |
851 | ||
852 | if (vdev->binding->vmstate_change) { | |
853 | vdev->binding->vmstate_change(vdev->binding_opaque, backend_run); | |
854 | } | |
855 | ||
856 | if (!backend_run) { | |
857 | virtio_set_status(vdev, vdev->status); | |
858 | } | |
859 | } | |
860 | ||
53c25cea PB |
861 | VirtIODevice *virtio_common_init(const char *name, uint16_t device_id, |
862 | size_t config_size, size_t struct_size) | |
967f97fa AL |
863 | { |
864 | VirtIODevice *vdev; | |
b8193adb | 865 | int i; |
967f97fa | 866 | |
7267c094 | 867 | vdev = g_malloc0(struct_size); |
967f97fa | 868 | |
53c25cea | 869 | vdev->device_id = device_id; |
967f97fa AL |
870 | vdev->status = 0; |
871 | vdev->isr = 0; | |
872 | vdev->queue_sel = 0; | |
7055e687 | 873 | vdev->config_vector = VIRTIO_NO_VECTOR; |
7267c094 | 874 | vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX); |
1354869c | 875 | vdev->vm_running = runstate_is_running(); |
1cbdabe2 | 876 | for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { |
b8193adb | 877 | vdev->vq[i].vector = VIRTIO_NO_VECTOR; |
1cbdabe2 MT |
878 | vdev->vq[i].vdev = vdev; |
879 | } | |
967f97fa | 880 | |
967f97fa AL |
881 | vdev->name = name; |
882 | vdev->config_len = config_size; | |
883 | if (vdev->config_len) | |
7267c094 | 884 | vdev->config = g_malloc0(config_size); |
967f97fa AL |
885 | else |
886 | vdev->config = NULL; | |
887 | ||
85cf2a8d MT |
888 | vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change, vdev); |
889 | ||
967f97fa AL |
890 | return vdev; |
891 | } | |
53c25cea PB |
892 | |
893 | void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding, | |
894 | void *opaque) | |
895 | { | |
896 | vdev->binding = binding; | |
897 | vdev->binding_opaque = opaque; | |
898 | } | |
1cbdabe2 MT |
899 | |
900 | target_phys_addr_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n) | |
901 | { | |
902 | return vdev->vq[n].vring.desc; | |
903 | } | |
904 | ||
905 | target_phys_addr_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n) | |
906 | { | |
907 | return vdev->vq[n].vring.avail; | |
908 | } | |
909 | ||
910 | target_phys_addr_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n) | |
911 | { | |
912 | return vdev->vq[n].vring.used; | |
913 | } | |
914 | ||
915 | target_phys_addr_t virtio_queue_get_ring_addr(VirtIODevice *vdev, int n) | |
916 | { | |
917 | return vdev->vq[n].vring.desc; | |
918 | } | |
919 | ||
920 | target_phys_addr_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n) | |
921 | { | |
922 | return sizeof(VRingDesc) * vdev->vq[n].vring.num; | |
923 | } | |
924 | ||
925 | target_phys_addr_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n) | |
926 | { | |
927 | return offsetof(VRingAvail, ring) + | |
2b3af999 | 928 | sizeof(uint64_t) * vdev->vq[n].vring.num; |
1cbdabe2 MT |
929 | } |
930 | ||
931 | target_phys_addr_t virtio_queue_get_used_size(VirtIODevice *vdev, int n) | |
932 | { | |
933 | return offsetof(VRingUsed, ring) + | |
934 | sizeof(VRingUsedElem) * vdev->vq[n].vring.num; | |
935 | } | |
936 | ||
937 | target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n) | |
938 | { | |
939 | return vdev->vq[n].vring.used - vdev->vq[n].vring.desc + | |
940 | virtio_queue_get_used_size(vdev, n); | |
941 | } | |
942 | ||
943 | uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) | |
944 | { | |
945 | return vdev->vq[n].last_avail_idx; | |
946 | } | |
947 | ||
948 | void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx) | |
949 | { | |
950 | vdev->vq[n].last_avail_idx = idx; | |
951 | } | |
952 | ||
953 | VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n) | |
954 | { | |
955 | return vdev->vq + n; | |
956 | } | |
957 | ||
958 | EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq) | |
959 | { | |
960 | return &vq->guest_notifier; | |
961 | } | |
962 | EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq) | |
963 | { | |
964 | return &vq->host_notifier; | |
965 | } |