]>
Commit | Line | Data |
---|---|---|
311e666a MM |
1 | /* |
2 | * libqos virtio definitions | |
3 | * | |
4 | * Copyright (c) 2014 Marc Marí | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | */ | |
9 | ||
10 | #ifndef LIBQOS_VIRTIO_H | |
11 | #define LIBQOS_VIRTIO_H | |
12 | ||
a2ce7dbd | 13 | #include "malloc.h" |
780b11a0 | 14 | #include "standard-headers/linux/virtio_ring.h" |
bf3c63d2 | 15 | |
a9340358 | 16 | #define QVIRTIO_F_BAD_FEATURE 0x40000000ull |
f294b029 | 17 | |
6b9cdf4c LV |
18 | typedef struct QVirtioBus QVirtioBus; |
19 | ||
311e666a | 20 | typedef struct QVirtioDevice { |
6b9cdf4c | 21 | const QVirtioBus *bus; |
311e666a MM |
22 | /* Device type */ |
23 | uint16_t device_type; | |
583349d1 | 24 | uint64_t features; |
d5006a45 | 25 | bool big_endian; |
56140fbb | 26 | bool features_negotiated; |
311e666a MM |
27 | } QVirtioDevice; |
28 | ||
bf3c63d2 | 29 | typedef struct QVirtQueue { |
bccd82b4 | 30 | QVirtioDevice *vdev; |
780b11a0 SH |
31 | uint64_t desc; /* This points to an array of struct vring_desc */ |
32 | uint64_t avail; /* This points to a struct vring_avail */ | |
afbccba6 | 33 | uint64_t used; /* This points to a struct vring_used */ |
bf3c63d2 MM |
34 | uint16_t index; |
35 | uint32_t size; | |
36 | uint32_t free_head; | |
37 | uint32_t num_free; | |
38 | uint32_t align; | |
e77abbe9 | 39 | uint16_t last_used_idx; |
f294b029 | 40 | bool indirect; |
1053587c | 41 | bool event; |
bf3c63d2 MM |
42 | } QVirtQueue; |
43 | ||
f294b029 | 44 | typedef struct QVRingIndirectDesc { |
780b11a0 | 45 | uint64_t desc; /* This points to an array fo struct vring_desc */ |
f294b029 MM |
46 | uint16_t index; |
47 | uint16_t elem; | |
48 | } QVRingIndirectDesc; | |
49 | ||
6b9cdf4c | 50 | struct QVirtioBus { |
728312b8 MM |
51 | uint8_t (*config_readb)(QVirtioDevice *d, uint64_t addr); |
52 | uint16_t (*config_readw)(QVirtioDevice *d, uint64_t addr); | |
53 | uint32_t (*config_readl)(QVirtioDevice *d, uint64_t addr); | |
54 | uint64_t (*config_readq)(QVirtioDevice *d, uint64_t addr); | |
46e0cf76 | 55 | |
bf3c63d2 | 56 | /* Get features of the device */ |
a9340358 | 57 | uint64_t (*get_features)(QVirtioDevice *d); |
bf3c63d2 | 58 | |
f294b029 | 59 | /* Set features of the device */ |
a9340358 | 60 | void (*set_features)(QVirtioDevice *d, uint64_t features); |
bf3c63d2 | 61 | |
f294b029 | 62 | /* Get features of the guest */ |
a9340358 | 63 | uint64_t (*get_guest_features)(QVirtioDevice *d); |
f294b029 | 64 | |
46e0cf76 MM |
65 | /* Get status of the device */ |
66 | uint8_t (*get_status)(QVirtioDevice *d); | |
67 | ||
68 | /* Set status of the device */ | |
69 | void (*set_status)(QVirtioDevice *d, uint8_t status); | |
bf3c63d2 | 70 | |
58368113 MM |
71 | /* Get the queue ISR status of the device */ |
72 | bool (*get_queue_isr_status)(QVirtioDevice *d, QVirtQueue *vq); | |
73 | ||
b57ebd57 TH |
74 | /* Wait for the configuration ISR status of the device */ |
75 | void (*wait_config_isr_status)(QVirtioDevice *d, gint64 timeout_us); | |
bf3c63d2 MM |
76 | |
77 | /* Select a queue to work on */ | |
78 | void (*queue_select)(QVirtioDevice *d, uint16_t index); | |
79 | ||
80 | /* Get the size of the selected queue */ | |
81 | uint16_t (*get_queue_size)(QVirtioDevice *d); | |
82 | ||
83 | /* Set the address of the selected queue */ | |
1e59a866 | 84 | void (*set_queue_address)(QVirtioDevice *d, QVirtQueue *vq); |
bf3c63d2 MM |
85 | |
86 | /* Setup the virtqueue specified by index */ | |
87 | QVirtQueue *(*virtqueue_setup)(QVirtioDevice *d, QGuestAllocator *alloc, | |
88 | uint16_t index); | |
89 | ||
f1d3b991 SH |
90 | /* Free virtqueue resources */ |
91 | void (*virtqueue_cleanup)(QVirtQueue *vq, QGuestAllocator *alloc); | |
92 | ||
bf3c63d2 MM |
93 | /* Notify changes in virtqueue */ |
94 | void (*virtqueue_kick)(QVirtioDevice *d, QVirtQueue *vq); | |
6b9cdf4c | 95 | }; |
46e0cf76 | 96 | |
bf3c63d2 MM |
97 | static inline uint32_t qvring_size(uint32_t num, uint32_t align) |
98 | { | |
780b11a0 | 99 | return ((sizeof(struct vring_desc) * num + sizeof(uint16_t) * (3 + num) |
bf3c63d2 | 100 | + align - 1) & ~(align - 1)) |
780b11a0 | 101 | + sizeof(uint16_t) * 3 + sizeof(struct vring_used_elem) * num; |
bf3c63d2 MM |
102 | } |
103 | ||
6b9cdf4c LV |
104 | uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr); |
105 | uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr); | |
106 | uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr); | |
107 | uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr); | |
a9340358 SH |
108 | uint64_t qvirtio_get_features(QVirtioDevice *d); |
109 | void qvirtio_set_features(QVirtioDevice *d, uint64_t features); | |
d5006a45 | 110 | bool qvirtio_is_big_endian(QVirtioDevice *d); |
6b9cdf4c LV |
111 | |
112 | void qvirtio_reset(QVirtioDevice *d); | |
113 | void qvirtio_set_acknowledge(QVirtioDevice *d); | |
114 | void qvirtio_set_driver(QVirtioDevice *d); | |
115 | void qvirtio_set_driver_ok(QVirtioDevice *d); | |
116 | ||
b57ebd57 | 117 | void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d, |
70556264 | 118 | QVirtQueue *vq, gint64 timeout_us); |
1999a70a | 119 | uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d, |
e8c81b4d SH |
120 | QVirtQueue *vq, |
121 | uint64_t addr, | |
122 | gint64 timeout_us); | |
1999a70a | 123 | void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d, |
e77abbe9 SH |
124 | QVirtQueue *vq, |
125 | uint32_t desc_idx, | |
be3a6781 | 126 | uint32_t *len, |
e77abbe9 | 127 | gint64 timeout_us); |
6b9cdf4c LV |
128 | void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us); |
129 | QVirtQueue *qvirtqueue_setup(QVirtioDevice *d, | |
130 | QGuestAllocator *alloc, uint16_t index); | |
f1d3b991 SH |
131 | void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq, |
132 | QGuestAllocator *alloc); | |
bf3c63d2 | 133 | |
8b898f59 TH |
134 | void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq, |
135 | uint64_t addr); | |
1999a70a TH |
136 | QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d, |
137 | QGuestAllocator *alloc, | |
138 | uint16_t elem); | |
bccd82b4 SH |
139 | void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts, |
140 | QVRingIndirectDesc *indirect, | |
1999a70a TH |
141 | uint64_t data, uint32_t len, bool write); |
142 | uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data, | |
143 | uint32_t len, bool write, bool next); | |
144 | uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq, | |
145 | QVRingIndirectDesc *indirect); | |
146 | void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq, | |
147 | uint32_t free_head); | |
148 | bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx, | |
149 | uint32_t *len); | |
150 | ||
151 | void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx); | |
2f84a92e | 152 | |
34c97748 | 153 | void qvirtio_start_device(QVirtioDevice *vdev); |
2f84a92e | 154 | |
311e666a | 155 | #endif |