4 #include "hw/virtio/vhost-backend.h"
5 #include "hw/virtio/virtio.h"
6 #include "exec/memory.h"
8 #define VHOST_F_DEVICE_IOTLB 63
9 #define VHOST_USER_F_PROTOCOL_FEATURES 30
11 /* Generic structures common for any vhost based device. */
13 struct vhost_inflight
{
21 struct vhost_virtqueue
{
28 unsigned long long desc_phys
;
30 unsigned long long avail_phys
;
32 unsigned long long used_phys
;
34 EventNotifier masked_notifier
;
35 EventNotifier error_notifier
;
36 struct vhost_dev
*dev
;
39 typedef unsigned long vhost_log_chunk_t
;
40 #define VHOST_LOG_PAGE 0x1000
41 #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
42 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
43 #define VHOST_INVALID_FEATURE_BIT (0xff)
46 unsigned long long size
;
49 vhost_log_chunk_t
*log
;
54 struct vhost_dev
*hdev
;
58 QLIST_ENTRY(vhost_iommu
) iommu_next
;
61 typedef struct VhostDevConfigOps
{
62 /* Vhost device config space changed callback
64 int (*vhost_dev_config_notifier
)(struct vhost_dev
*dev
);
70 * struct vhost_dev - common vhost_dev structure
71 * @vhost_ops: backend specific ops
72 * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier)
76 MemoryListener memory_listener
;
77 MemoryListener iommu_listener
;
78 struct vhost_memory
*mem
;
80 MemoryRegionSection
*mem_sections
;
82 MemoryRegionSection
*tmp_sections
;
83 struct vhost_virtqueue
*vqs
;
85 /* the first virtqueue which would be used by this vhost dev */
87 /* one past the last vq index for the virtio device (not vhost) */
89 /* if non-zero, minimum required value for max_queues */
92 * vhost feature handling requires matching the feature set
93 * offered by a backend which may be a subset of the total
94 * features eventually offered to the guest.
96 * @features: available features provided by the backend
97 * @acked_features: final negotiated features with front-end driver
99 * @backend_features: this is used in a couple of places to either
100 * store VHOST_USER_F_PROTOCOL_FEATURES to apply to
101 * VHOST_USER_SET_FEATURES or VHOST_NET_F_VIRTIO_NET_HDR. Its
102 * future use should be discouraged and the variable retired as
103 * its easy to confuse with the VirtIO backend_features.
106 uint64_t acked_features
;
107 uint64_t backend_features
;
110 * @protocol_features: is the vhost-user only feature set by
111 * VHOST_USER_SET_PROTOCOL_FEATURES. Protocol features are only
112 * negotiated if VHOST_USER_F_PROTOCOL_FEATURES has been offered
113 * by the backend (see @features).
115 uint64_t protocol_features
;
118 uint64_t backend_cap
;
119 /* @started: is the vhost device started? */
123 Error
*migration_blocker
;
124 const VhostOps
*vhost_ops
;
126 struct vhost_log
*log
;
127 QLIST_ENTRY(vhost_dev
) entry
;
128 QLIST_HEAD(, vhost_iommu
) iommu_list
;
130 const VhostDevConfigOps
*config_ops
;
133 extern const VhostOps kernel_ops
;
134 extern const VhostOps user_ops
;
135 extern const VhostOps vdpa_ops
;
138 struct vhost_dev dev
;
139 struct vhost_virtqueue vqs
[2];
145 * vhost_dev_init() - initialise the vhost interface
146 * @hdev: the common vhost_dev structure
147 * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa)
148 * @backend_type: type of backend
149 * @busyloop_timeout: timeout for polling virtqueue
150 * @errp: error handle
152 * The initialisation of the vhost device will trigger the
153 * initialisation of the backend and potentially capability
154 * negotiation of backend interface. Configuration of the VirtIO
155 * itself won't happen until the interface is started.
157 * Return: 0 on success, non-zero on error while setting errp.
159 int vhost_dev_init(struct vhost_dev
*hdev
, void *opaque
,
160 VhostBackendType backend_type
,
161 uint32_t busyloop_timeout
, Error
**errp
);
164 * vhost_dev_cleanup() - tear down and cleanup vhost interface
165 * @hdev: the common vhost_dev structure
167 void vhost_dev_cleanup(struct vhost_dev
*hdev
);
170 * vhost_dev_enable_notifiers() - enable event notifiers
171 * @hdev: common vhost_dev structure
172 * @vdev: the VirtIODevice structure
174 * Enable notifications directly to the vhost device rather than being
175 * triggered by QEMU itself. Notifications should be enabled before
176 * the vhost device is started via @vhost_dev_start.
178 * Return: 0 on success, < 0 on error.
180 int vhost_dev_enable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
);
183 * vhost_dev_disable_notifiers - disable event notifications
184 * @hdev: common vhost_dev structure
185 * @vdev: the VirtIODevice structure
187 * Disable direct notifications to vhost device.
189 void vhost_dev_disable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
);
192 * vhost_dev_is_started() - report status of vhost device
193 * @hdev: common vhost_dev structure
195 * Return the started status of the vhost device
197 static inline bool vhost_dev_is_started(struct vhost_dev
*hdev
)
199 return hdev
->started
;
203 * vhost_dev_start() - start the vhost device
204 * @hdev: common vhost_dev structure
205 * @vdev: the VirtIODevice structure
206 * @vrings: true to have vrings enabled in this call
208 * Starts the vhost device. From this point VirtIO feature negotiation
209 * can start and the device can start processing VirtIO transactions.
211 * Return: 0 on success, < 0 on error.
213 int vhost_dev_start(struct vhost_dev
*hdev
, VirtIODevice
*vdev
, bool vrings
);
216 * vhost_dev_stop() - stop the vhost device
217 * @hdev: common vhost_dev structure
218 * @vdev: the VirtIODevice structure
219 * @vrings: true to have vrings disabled in this call
221 * Stop the vhost device. After the device is stopped the notifiers
222 * can be disabled (@vhost_dev_disable_notifiers) and the device can
223 * be torn down (@vhost_dev_cleanup).
225 void vhost_dev_stop(struct vhost_dev
*hdev
, VirtIODevice
*vdev
, bool vrings
);
228 * DOC: vhost device configuration handling
230 * The VirtIO device configuration space is used for rarely changing
231 * or initialisation time parameters. The configuration can be updated
232 * by either the guest driver or the device itself. If the device can
233 * change the configuration over time the vhost handler should
234 * register a @VhostDevConfigOps structure with
235 * @vhost_dev_set_config_notifier so the guest can be notified. Some
236 * devices register a handler anyway and will signal an error if an
237 * unexpected config change happens.
241 * vhost_dev_get_config() - fetch device configuration
242 * @hdev: common vhost_dev_structure
243 * @config: pointer to device appropriate config structure
244 * @config_len: size of device appropriate config structure
246 * Return: 0 on success, < 0 on error while setting errp
248 int vhost_dev_get_config(struct vhost_dev
*hdev
, uint8_t *config
,
249 uint32_t config_len
, Error
**errp
);
252 * vhost_dev_set_config() - set device configuration
253 * @hdev: common vhost_dev_structure
254 * @data: pointer to data to set
255 * @offset: offset into configuration space
256 * @size: length of set
257 * @flags: @VhostSetConfigType flags
259 * By use of @offset/@size a subset of the configuration space can be
260 * written to. The @flags are used to indicate if it is a normal
261 * transaction or related to migration.
263 * Return: 0 on success, non-zero on error
265 int vhost_dev_set_config(struct vhost_dev
*dev
, const uint8_t *data
,
266 uint32_t offset
, uint32_t size
, uint32_t flags
);
269 * vhost_dev_set_config_notifier() - register VhostDevConfigOps
270 * @hdev: common vhost_dev_structure
273 * If the device is expected to change configuration a notifier can be
274 * setup to handle the case.
276 void vhost_dev_set_config_notifier(struct vhost_dev
*dev
,
277 const VhostDevConfigOps
*ops
);
280 /* Test and clear masked event pending status.
281 * Should be called after unmask to avoid losing events.
283 bool vhost_virtqueue_pending(struct vhost_dev
*hdev
, int n
);
285 /* Mask/unmask events from this vq.
287 void vhost_virtqueue_mask(struct vhost_dev
*hdev
, VirtIODevice
*vdev
, int n
,
291 * vhost_get_features() - return a sanitised set of feature bits
292 * @hdev: common vhost_dev structure
293 * @feature_bits: pointer to terminated table of feature bits
294 * @features: original feature set
296 * This returns a set of features bits that is an intersection of what
297 * is supported by the vhost backend (hdev->features), the supported
298 * feature_bits and the requested feature set.
300 uint64_t vhost_get_features(struct vhost_dev
*hdev
, const int *feature_bits
,
304 * vhost_ack_features() - set vhost acked_features
305 * @hdev: common vhost_dev structure
306 * @feature_bits: pointer to terminated table of feature bits
307 * @features: requested feature set
309 * This sets the internal hdev->acked_features to the intersection of
310 * the backends advertised features and the supported feature_bits.
312 void vhost_ack_features(struct vhost_dev
*hdev
, const int *feature_bits
,
314 bool vhost_has_free_slot(void);
316 int vhost_net_set_backend(struct vhost_dev
*hdev
,
317 struct vhost_vring_file
*file
);
319 int vhost_device_iotlb_miss(struct vhost_dev
*dev
, uint64_t iova
, int write
);
321 int vhost_virtqueue_start(struct vhost_dev
*dev
, struct VirtIODevice
*vdev
,
322 struct vhost_virtqueue
*vq
, unsigned idx
);
323 void vhost_virtqueue_stop(struct vhost_dev
*dev
, struct VirtIODevice
*vdev
,
324 struct vhost_virtqueue
*vq
, unsigned idx
);
326 void vhost_dev_reset_inflight(struct vhost_inflight
*inflight
);
327 void vhost_dev_free_inflight(struct vhost_inflight
*inflight
);
328 void vhost_dev_save_inflight(struct vhost_inflight
*inflight
, QEMUFile
*f
);
329 int vhost_dev_load_inflight(struct vhost_inflight
*inflight
, QEMUFile
*f
);
330 int vhost_dev_prepare_inflight(struct vhost_dev
*hdev
, VirtIODevice
*vdev
);
331 int vhost_dev_set_inflight(struct vhost_dev
*dev
,
332 struct vhost_inflight
*inflight
);
333 int vhost_dev_get_inflight(struct vhost_dev
*dev
, uint16_t queue_size
,
334 struct vhost_inflight
*inflight
);