]>
Commit | Line | Data |
---|---|---|
d5970055 MT |
1 | #ifndef VHOST_H |
2 | #define VHOST_H | |
3 | ||
24d1eb33 | 4 | #include "hw/virtio/vhost-backend.h" |
0d09e41a | 5 | #include "hw/virtio/virtio.h" |
022c62cb | 6 | #include "exec/memory.h" |
d5970055 | 7 | |
f3034ad7 LV |
8 | #define VHOST_F_DEVICE_IOTLB 63 |
9 | #define VHOST_USER_F_PROTOCOL_FEATURES 30 | |
10 | ||
d5970055 | 11 | /* Generic structures common for any vhost based device. */ |
5ad204bf XY |
12 | |
13 | struct vhost_inflight { | |
14 | int fd; | |
15 | void *addr; | |
16 | uint64_t size; | |
17 | uint64_t offset; | |
18 | uint16_t queue_size; | |
19 | }; | |
20 | ||
d5970055 MT |
21 | struct vhost_virtqueue { |
22 | int kick; | |
23 | int call; | |
24 | void *desc; | |
25 | void *avail; | |
26 | void *used; | |
27 | int num; | |
f1f9e6c5 GK |
28 | unsigned long long desc_phys; |
29 | unsigned desc_size; | |
30 | unsigned long long avail_phys; | |
31 | unsigned avail_size; | |
d5970055 MT |
32 | unsigned long long used_phys; |
33 | unsigned used_size; | |
f56a1247 | 34 | EventNotifier masked_notifier; |
ae50ae0b | 35 | EventNotifier error_notifier; |
f9a09ca3 | 36 | EventNotifier masked_config_notifier; |
c471ad0e | 37 | struct vhost_dev *dev; |
d5970055 MT |
38 | }; |
39 | ||
40 | typedef unsigned long vhost_log_chunk_t; | |
41 | #define VHOST_LOG_PAGE 0x1000 | |
42 | #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t)) | |
43 | #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS) | |
2e6d46d7 | 44 | #define VHOST_INVALID_FEATURE_BIT (0xff) |
f9a09ca3 | 45 | #define VHOST_QUEUE_NUM_CONFIG_INR 0 |
d5970055 | 46 | |
309750fa JW |
47 | struct vhost_log { |
48 | unsigned long long size; | |
49 | int refcnt; | |
15324404 MAL |
50 | int fd; |
51 | vhost_log_chunk_t *log; | |
309750fa JW |
52 | }; |
53 | ||
375f74f4 JW |
54 | struct vhost_dev; |
55 | struct vhost_iommu { | |
56 | struct vhost_dev *hdev; | |
57 | MemoryRegion *mr; | |
58 | hwaddr iommu_offset; | |
59 | IOMMUNotifier n; | |
60 | QLIST_ENTRY(vhost_iommu) iommu_next; | |
61 | }; | |
62 | ||
4c3e257b CL |
63 | typedef struct VhostDevConfigOps { |
64 | /* Vhost device config space changed callback | |
65 | */ | |
66 | int (*vhost_dev_config_notifier)(struct vhost_dev *dev); | |
67 | } VhostDevConfigOps; | |
68 | ||
d5970055 | 69 | struct vhost_memory; |
27351992 AB |
70 | |
71 | /** | |
72 | * struct vhost_dev - common vhost_dev structure | |
73 | * @vhost_ops: backend specific ops | |
74 | * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier) | |
75 | */ | |
d5970055 | 76 | struct vhost_dev { |
c471ad0e | 77 | VirtIODevice *vdev; |
04097f7c | 78 | MemoryListener memory_listener; |
375f74f4 | 79 | MemoryListener iommu_listener; |
d5970055 | 80 | struct vhost_memory *mem; |
2817b260 AK |
81 | int n_mem_sections; |
82 | MemoryRegionSection *mem_sections; | |
c44317ef DDAG |
83 | int n_tmp_sections; |
84 | MemoryRegionSection *tmp_sections; | |
d5970055 | 85 | struct vhost_virtqueue *vqs; |
5fc13603 | 86 | unsigned int nvqs; |
9be6e69f | 87 | /* the first virtqueue which would be used by this vhost dev */ |
a9f98bb5 | 88 | int vq_index; |
245cf2c2 EP |
89 | /* one past the last vq index for the virtio device (not vhost) */ |
90 | int vq_index_end; | |
c90bd505 KW |
91 | /* if non-zero, minimum required value for max_queues */ |
92 | int num_queues; | |
9600c98e AB |
93 | /** |
94 | * vhost feature handling requires matching the feature set | |
95 | * offered by a backend which may be a subset of the total | |
96 | * features eventually offered to the guest. | |
97 | * | |
98 | * @features: available features provided by the backend | |
99 | * @acked_features: final negotiated features with front-end driver | |
100 | * | |
101 | * @backend_features: this is used in a couple of places to either | |
102 | * store VHOST_USER_F_PROTOCOL_FEATURES to apply to | |
103 | * VHOST_USER_SET_FEATURES or VHOST_NET_F_VIRTIO_NET_HDR. Its | |
104 | * future use should be discouraged and the variable retired as | |
105 | * its easy to confuse with the VirtIO backend_features. | |
106 | */ | |
21e70425 MAL |
107 | uint64_t features; |
108 | uint64_t acked_features; | |
109 | uint64_t backend_features; | |
9600c98e AB |
110 | |
111 | /** | |
112 | * @protocol_features: is the vhost-user only feature set by | |
113 | * VHOST_USER_SET_PROTOCOL_FEATURES. Protocol features are only | |
114 | * negotiated if VHOST_USER_F_PROTOCOL_FEATURES has been offered | |
115 | * by the backend (see @features). | |
116 | */ | |
21e70425 | 117 | uint64_t protocol_features; |
9600c98e | 118 | |
21e70425 | 119 | uint64_t max_queues; |
b37556ed | 120 | uint64_t backend_cap; |
b8f3e6a1 | 121 | /* @started: is the vhost device started? */ |
d5970055 MT |
122 | bool started; |
123 | bool log_enabled; | |
21e70425 | 124 | uint64_t log_size; |
7145872e | 125 | Error *migration_blocker; |
24d1eb33 | 126 | const VhostOps *vhost_ops; |
1a1bfac9 | 127 | void *opaque; |
309750fa | 128 | struct vhost_log *log; |
2ce68e4c | 129 | QLIST_ENTRY(vhost_dev) entry; |
375f74f4 | 130 | QLIST_HEAD(, vhost_iommu) iommu_list; |
c471ad0e | 131 | IOMMUNotifier n; |
4c3e257b | 132 | const VhostDevConfigOps *config_ops; |
d5970055 MT |
133 | }; |
134 | ||
9b1d929a TG |
135 | extern const VhostOps kernel_ops; |
136 | extern const VhostOps user_ops; | |
137 | extern const VhostOps vdpa_ops; | |
138 | ||
108a6481 CL |
139 | struct vhost_net { |
140 | struct vhost_dev dev; | |
141 | struct vhost_virtqueue vqs[2]; | |
142 | int backend; | |
143 | NetClientState *nc; | |
144 | }; | |
145 | ||
27351992 AB |
146 | /** |
147 | * vhost_dev_init() - initialise the vhost interface | |
148 | * @hdev: the common vhost_dev structure | |
149 | * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa) | |
150 | * @backend_type: type of backend | |
151 | * @busyloop_timeout: timeout for polling virtqueue | |
152 | * @errp: error handle | |
153 | * | |
154 | * The initialisation of the vhost device will trigger the | |
155 | * initialisation of the backend and potentially capability | |
156 | * negotiation of backend interface. Configuration of the VirtIO | |
157 | * itself won't happen until the interface is started. | |
158 | * | |
159 | * Return: 0 on success, non-zero on error while setting errp. | |
160 | */ | |
81647a65 | 161 | int vhost_dev_init(struct vhost_dev *hdev, void *opaque, |
69e87b32 | 162 | VhostBackendType backend_type, |
a6945f22 | 163 | uint32_t busyloop_timeout, Error **errp); |
27351992 AB |
164 | |
165 | /** | |
166 | * vhost_dev_cleanup() - tear down and cleanup vhost interface | |
167 | * @hdev: the common vhost_dev structure | |
168 | */ | |
d5970055 | 169 | void vhost_dev_cleanup(struct vhost_dev *hdev); |
27351992 AB |
170 | |
171 | /** | |
172 | * vhost_dev_enable_notifiers() - enable event notifiers | |
173 | * @hdev: common vhost_dev structure | |
174 | * @vdev: the VirtIODevice structure | |
175 | * | |
176 | * Enable notifications directly to the vhost device rather than being | |
177 | * triggered by QEMU itself. Notifications should be enabled before | |
178 | * the vhost device is started via @vhost_dev_start. | |
179 | * | |
180 | * Return: 0 on success, < 0 on error. | |
181 | */ | |
b0b3db79 | 182 | int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); |
27351992 AB |
183 | |
184 | /** | |
185 | * vhost_dev_disable_notifiers - disable event notifications | |
186 | * @hdev: common vhost_dev structure | |
187 | * @vdev: the VirtIODevice structure | |
188 | * | |
189 | * Disable direct notifications to vhost device. | |
190 | */ | |
b0b3db79 | 191 | void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); |
f9a09ca3 CL |
192 | bool vhost_config_pending(struct vhost_dev *hdev); |
193 | void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask); | |
d5970055 | 194 | |
b8f3e6a1 AB |
195 | /** |
196 | * vhost_dev_is_started() - report status of vhost device | |
197 | * @hdev: common vhost_dev structure | |
198 | * | |
199 | * Return the started status of the vhost device | |
200 | */ | |
201 | static inline bool vhost_dev_is_started(struct vhost_dev *hdev) | |
202 | { | |
203 | return hdev->started; | |
204 | } | |
205 | ||
27351992 AB |
206 | /** |
207 | * vhost_dev_start() - start the vhost device | |
208 | * @hdev: common vhost_dev structure | |
209 | * @vdev: the VirtIODevice structure | |
4daa5054 | 210 | * @vrings: true to have vrings enabled in this call |
27351992 AB |
211 | * |
212 | * Starts the vhost device. From this point VirtIO feature negotiation | |
213 | * can start and the device can start processing VirtIO transactions. | |
214 | * | |
215 | * Return: 0 on success, < 0 on error. | |
216 | */ | |
4daa5054 | 217 | int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings); |
27351992 AB |
218 | |
219 | /** | |
220 | * vhost_dev_stop() - stop the vhost device | |
221 | * @hdev: common vhost_dev structure | |
222 | * @vdev: the VirtIODevice structure | |
4daa5054 | 223 | * @vrings: true to have vrings disabled in this call |
27351992 AB |
224 | * |
225 | * Stop the vhost device. After the device is stopped the notifiers | |
226 | * can be disabled (@vhost_dev_disable_notifiers) and the device can | |
227 | * be torn down (@vhost_dev_cleanup). | |
228 | */ | |
4daa5054 | 229 | void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings); |
27351992 AB |
230 | |
231 | /** | |
232 | * DOC: vhost device configuration handling | |
233 | * | |
234 | * The VirtIO device configuration space is used for rarely changing | |
235 | * or initialisation time parameters. The configuration can be updated | |
236 | * by either the guest driver or the device itself. If the device can | |
237 | * change the configuration over time the vhost handler should | |
238 | * register a @VhostDevConfigOps structure with | |
239 | * @vhost_dev_set_config_notifier so the guest can be notified. Some | |
240 | * devices register a handler anyway and will signal an error if an | |
241 | * unexpected config change happens. | |
242 | */ | |
243 | ||
244 | /** | |
245 | * vhost_dev_get_config() - fetch device configuration | |
246 | * @hdev: common vhost_dev_structure | |
247 | * @config: pointer to device appropriate config structure | |
248 | * @config_len: size of device appropriate config structure | |
249 | * | |
250 | * Return: 0 on success, < 0 on error while setting errp | |
251 | */ | |
252 | int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, | |
253 | uint32_t config_len, Error **errp); | |
254 | ||
255 | /** | |
256 | * vhost_dev_set_config() - set device configuration | |
257 | * @hdev: common vhost_dev_structure | |
258 | * @data: pointer to data to set | |
259 | * @offset: offset into configuration space | |
260 | * @size: length of set | |
261 | * @flags: @VhostSetConfigType flags | |
262 | * | |
263 | * By use of @offset/@size a subset of the configuration space can be | |
264 | * written to. The @flags are used to indicate if it is a normal | |
265 | * transaction or related to migration. | |
266 | * | |
267 | * Return: 0 on success, non-zero on error | |
268 | */ | |
269 | int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data, | |
270 | uint32_t offset, uint32_t size, uint32_t flags); | |
271 | ||
272 | /** | |
273 | * vhost_dev_set_config_notifier() - register VhostDevConfigOps | |
274 | * @hdev: common vhost_dev_structure | |
275 | * @ops: notifier ops | |
276 | * | |
277 | * If the device is expected to change configuration a notifier can be | |
278 | * setup to handle the case. | |
279 | */ | |
280 | void vhost_dev_set_config_notifier(struct vhost_dev *dev, | |
281 | const VhostDevConfigOps *ops); | |
282 | ||
283 | ||
f56a1247 MT |
284 | /* Test and clear masked event pending status. |
285 | * Should be called after unmask to avoid losing events. | |
286 | */ | |
287 | bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n); | |
288 | ||
289 | /* Mask/unmask events from this vq. | |
290 | */ | |
291 | void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, | |
292 | bool mask); | |
2055c2a4 AB |
293 | |
294 | /** | |
295 | * vhost_get_features() - return a sanitised set of feature bits | |
296 | * @hdev: common vhost_dev structure | |
297 | * @feature_bits: pointer to terminated table of feature bits | |
298 | * @features: original feature set | |
299 | * | |
300 | * This returns a set of features bits that is an intersection of what | |
301 | * is supported by the vhost backend (hdev->features), the supported | |
302 | * feature_bits and the requested feature set. | |
303 | */ | |
9a2ba823 CH |
304 | uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, |
305 | uint64_t features); | |
81cf38f3 AB |
306 | |
307 | /** | |
308 | * vhost_ack_features() - set vhost acked_features | |
309 | * @hdev: common vhost_dev structure | |
310 | * @feature_bits: pointer to terminated table of feature bits | |
311 | * @features: requested feature set | |
312 | * | |
313 | * This sets the internal hdev->acked_features to the intersection of | |
314 | * the backends advertised features and the supported feature_bits. | |
315 | */ | |
2e6d46d7 | 316 | void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, |
9a2ba823 | 317 | uint64_t features); |
2ce68e4c | 318 | bool vhost_has_free_slot(void); |
950d94ba MAL |
319 | |
320 | int vhost_net_set_backend(struct vhost_dev *hdev, | |
321 | struct vhost_vring_file *file); | |
322 | ||
ee071f67 | 323 | void vhost_toggle_device_iotlb(VirtIODevice *vdev); |
fc58bd0d | 324 | int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write); |
5ad204bf | 325 | |
ff48b628 KX |
326 | int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, |
327 | struct vhost_virtqueue *vq, unsigned idx); | |
e1f101d9 KX |
328 | void vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev, |
329 | struct vhost_virtqueue *vq, unsigned idx); | |
ff48b628 | 330 | |
5ad204bf XY |
331 | void vhost_dev_reset_inflight(struct vhost_inflight *inflight); |
332 | void vhost_dev_free_inflight(struct vhost_inflight *inflight); | |
333 | void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f); | |
334 | int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f); | |
1b0063b3 | 335 | int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev); |
5ad204bf XY |
336 | int vhost_dev_set_inflight(struct vhost_dev *dev, |
337 | struct vhost_inflight *inflight); | |
338 | int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, | |
339 | struct vhost_inflight *inflight); | |
74b5d2b5 | 340 | bool vhost_dev_has_iommu(struct vhost_dev *dev); |
d5970055 | 341 | #endif |