xref: /qemu/include/hw/virtio/vhost.h (revision cd89c065b06d49691ecb1f3707849472acdf3ea5)
1d5970055SMichael S. Tsirkin #ifndef VHOST_H
2d5970055SMichael S. Tsirkin #define VHOST_H
3d5970055SMichael S. Tsirkin 
424d1eb33SNikolay Nikolaev #include "hw/virtio/vhost-backend.h"
50d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h"
6022c62cbSPaolo Bonzini #include "exec/memory.h"
7d5970055SMichael S. Tsirkin 
8f3034ad7SLaurent Vivier #define VHOST_F_DEVICE_IOTLB 63
9f3034ad7SLaurent Vivier #define VHOST_USER_F_PROTOCOL_FEATURES 30
10f3034ad7SLaurent Vivier 
11d5970055SMichael S. Tsirkin /* Generic structures common for any vhost based device. */
125ad204bfSXie Yongji 
135ad204bfSXie Yongji struct vhost_inflight {
145ad204bfSXie Yongji     int fd;
155ad204bfSXie Yongji     void *addr;
165ad204bfSXie Yongji     uint64_t size;
175ad204bfSXie Yongji     uint64_t offset;
185ad204bfSXie Yongji     uint16_t queue_size;
195ad204bfSXie Yongji };
205ad204bfSXie Yongji 
21d5970055SMichael S. Tsirkin struct vhost_virtqueue {
22d5970055SMichael S. Tsirkin     int kick;
23d5970055SMichael S. Tsirkin     int call;
24d5970055SMichael S. Tsirkin     void *desc;
25d5970055SMichael S. Tsirkin     void *avail;
26d5970055SMichael S. Tsirkin     void *used;
27d5970055SMichael S. Tsirkin     int num;
28f1f9e6c5SGreg Kurz     unsigned long long desc_phys;
29f1f9e6c5SGreg Kurz     unsigned desc_size;
30f1f9e6c5SGreg Kurz     unsigned long long avail_phys;
31f1f9e6c5SGreg Kurz     unsigned avail_size;
32d5970055SMichael S. Tsirkin     unsigned long long used_phys;
33d5970055SMichael S. Tsirkin     unsigned used_size;
34f56a1247SMichael S. Tsirkin     EventNotifier masked_notifier;
35ae50ae0bSKonstantin Khlebnikov     EventNotifier error_notifier;
36f9a09ca3SCindy Lu     EventNotifier masked_config_notifier;
37c471ad0eSJason Wang     struct vhost_dev *dev;
38d5970055SMichael S. Tsirkin };
39d5970055SMichael S. Tsirkin 
40d5970055SMichael S. Tsirkin typedef unsigned long vhost_log_chunk_t;
41d5970055SMichael S. Tsirkin #define VHOST_LOG_PAGE 0x1000
42d5970055SMichael S. Tsirkin #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
43d5970055SMichael S. Tsirkin #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
442e6d46d7SNikolay Nikolaev #define VHOST_INVALID_FEATURE_BIT   (0xff)
45f9a09ca3SCindy Lu #define VHOST_QUEUE_NUM_CONFIG_INR 0
46d5970055SMichael S. Tsirkin 
47309750faSJason Wang struct vhost_log {
48309750faSJason Wang     unsigned long long size;
49309750faSJason Wang     int refcnt;
5015324404SMarc-André Lureau     int fd;
5115324404SMarc-André Lureau     vhost_log_chunk_t *log;
52309750faSJason Wang };
53309750faSJason Wang 
54375f74f4SJason Wang struct vhost_dev;
55375f74f4SJason Wang struct vhost_iommu {
56375f74f4SJason Wang     struct vhost_dev *hdev;
57375f74f4SJason Wang     MemoryRegion *mr;
58375f74f4SJason Wang     hwaddr iommu_offset;
59375f74f4SJason Wang     IOMMUNotifier n;
60375f74f4SJason Wang     QLIST_ENTRY(vhost_iommu) iommu_next;
61375f74f4SJason Wang };
62375f74f4SJason Wang 
634c3e257bSChangpeng Liu typedef struct VhostDevConfigOps {
644c3e257bSChangpeng Liu     /* Vhost device config space changed callback
654c3e257bSChangpeng Liu      */
664c3e257bSChangpeng Liu     int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
674c3e257bSChangpeng Liu } VhostDevConfigOps;
684c3e257bSChangpeng Liu 
69d5970055SMichael S. Tsirkin struct vhost_memory;
7027351992SAlex Bennée 
7127351992SAlex Bennée /**
7227351992SAlex Bennée  * struct vhost_dev - common vhost_dev structure
7327351992SAlex Bennée  * @vhost_ops: backend specific ops
7427351992SAlex Bennée  * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier)
7527351992SAlex Bennée  */
76d5970055SMichael S. Tsirkin struct vhost_dev {
77c471ad0eSJason Wang     VirtIODevice *vdev;
7804097f7cSAvi Kivity     MemoryListener memory_listener;
79375f74f4SJason Wang     MemoryListener iommu_listener;
80d5970055SMichael S. Tsirkin     struct vhost_memory *mem;
812817b260SAvi Kivity     int n_mem_sections;
822817b260SAvi Kivity     MemoryRegionSection *mem_sections;
83c44317efSDr. David Alan Gilbert     int n_tmp_sections;
84c44317efSDr. David Alan Gilbert     MemoryRegionSection *tmp_sections;
85d5970055SMichael S. Tsirkin     struct vhost_virtqueue *vqs;
865fc13603SJason Wang     unsigned int nvqs;
879be6e69fSGreg Kurz     /* the first virtqueue which would be used by this vhost dev */
88a9f98bb5SJason Wang     int vq_index;
89245cf2c2SEugenio Pérez     /* one past the last vq index for the virtio device (not vhost) */
90245cf2c2SEugenio Pérez     int vq_index_end;
91c90bd505SKevin Wolf     /* if non-zero, minimum required value for max_queues */
92c90bd505SKevin Wolf     int num_queues;
939600c98eSAlex Bennée     /**
949600c98eSAlex Bennée      * vhost feature handling requires matching the feature set
959600c98eSAlex Bennée      * offered by a backend which may be a subset of the total
969600c98eSAlex Bennée      * features eventually offered to the guest.
979600c98eSAlex Bennée      *
989600c98eSAlex Bennée      * @features: available features provided by the backend
999600c98eSAlex Bennée      * @acked_features: final negotiated features with front-end driver
1009600c98eSAlex Bennée      *
1019600c98eSAlex Bennée      * @backend_features: this is used in a couple of places to either
1029600c98eSAlex Bennée      * store VHOST_USER_F_PROTOCOL_FEATURES to apply to
1039600c98eSAlex Bennée      * VHOST_USER_SET_FEATURES or VHOST_NET_F_VIRTIO_NET_HDR. Its
1049600c98eSAlex Bennée      * future use should be discouraged and the variable retired as
1059600c98eSAlex Bennée      * its easy to confuse with the VirtIO backend_features.
1069600c98eSAlex Bennée      */
10721e70425SMarc-André Lureau     uint64_t features;
10821e70425SMarc-André Lureau     uint64_t acked_features;
10921e70425SMarc-André Lureau     uint64_t backend_features;
1109600c98eSAlex Bennée 
1119600c98eSAlex Bennée     /**
1129600c98eSAlex Bennée      * @protocol_features: is the vhost-user only feature set by
1139600c98eSAlex Bennée      * VHOST_USER_SET_PROTOCOL_FEATURES. Protocol features are only
1149600c98eSAlex Bennée      * negotiated if VHOST_USER_F_PROTOCOL_FEATURES has been offered
1159600c98eSAlex Bennée      * by the backend (see @features).
1169600c98eSAlex Bennée      */
11721e70425SMarc-André Lureau     uint64_t protocol_features;
1189600c98eSAlex Bennée 
11921e70425SMarc-André Lureau     uint64_t max_queues;
120b37556edSJason Wang     uint64_t backend_cap;
121b8f3e6a1SAlex Bennée     /* @started: is the vhost device started? */
122d5970055SMichael S. Tsirkin     bool started;
123d5970055SMichael S. Tsirkin     bool log_enabled;
12421e70425SMarc-André Lureau     uint64_t log_size;
1257145872eSMichael S. Tsirkin     Error *migration_blocker;
12624d1eb33SNikolay Nikolaev     const VhostOps *vhost_ops;
1271a1bfac9SNikolay Nikolaev     void *opaque;
128309750faSJason Wang     struct vhost_log *log;
1292ce68e4cSIgor Mammedov     QLIST_ENTRY(vhost_dev) entry;
130375f74f4SJason Wang     QLIST_HEAD(, vhost_iommu) iommu_list;
131c471ad0eSJason Wang     IOMMUNotifier n;
1324c3e257bSChangpeng Liu     const VhostDevConfigOps *config_ops;
133d5970055SMichael S. Tsirkin };
134d5970055SMichael S. Tsirkin 
1359b1d929aSTiberiu Georgescu extern const VhostOps kernel_ops;
1369b1d929aSTiberiu Georgescu extern const VhostOps user_ops;
1379b1d929aSTiberiu Georgescu extern const VhostOps vdpa_ops;
1389b1d929aSTiberiu Georgescu 
139108a6481SCindy Lu struct vhost_net {
140108a6481SCindy Lu     struct vhost_dev dev;
141108a6481SCindy Lu     struct vhost_virtqueue vqs[2];
142108a6481SCindy Lu     int backend;
143108a6481SCindy Lu     NetClientState *nc;
144108a6481SCindy Lu };
145108a6481SCindy Lu 
14627351992SAlex Bennée /**
14727351992SAlex Bennée  * vhost_dev_init() - initialise the vhost interface
14827351992SAlex Bennée  * @hdev: the common vhost_dev structure
14927351992SAlex Bennée  * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa)
15027351992SAlex Bennée  * @backend_type: type of backend
15127351992SAlex Bennée  * @busyloop_timeout: timeout for polling virtqueue
15227351992SAlex Bennée  * @errp: error handle
15327351992SAlex Bennée  *
15427351992SAlex Bennée  * The initialisation of the vhost device will trigger the
15527351992SAlex Bennée  * initialisation of the backend and potentially capability
15627351992SAlex Bennée  * negotiation of backend interface. Configuration of the VirtIO
15727351992SAlex Bennée  * itself won't happen until the interface is started.
15827351992SAlex Bennée  *
15927351992SAlex Bennée  * Return: 0 on success, non-zero on error while setting errp.
16027351992SAlex Bennée  */
16181647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
16269e87b32SJason Wang                    VhostBackendType backend_type,
163a6945f22SKevin Wolf                    uint32_t busyloop_timeout, Error **errp);
16427351992SAlex Bennée 
16527351992SAlex Bennée /**
16627351992SAlex Bennée  * vhost_dev_cleanup() - tear down and cleanup vhost interface
16727351992SAlex Bennée  * @hdev: the common vhost_dev structure
16827351992SAlex Bennée  */
169d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev);
17027351992SAlex Bennée 
17127351992SAlex Bennée /**
17227351992SAlex Bennée  * vhost_dev_enable_notifiers() - enable event notifiers
17327351992SAlex Bennée  * @hdev: common vhost_dev structure
17427351992SAlex Bennée  * @vdev: the VirtIODevice structure
17527351992SAlex Bennée  *
17627351992SAlex Bennée  * Enable notifications directly to the vhost device rather than being
17727351992SAlex Bennée  * triggered by QEMU itself. Notifications should be enabled before
17827351992SAlex Bennée  * the vhost device is started via @vhost_dev_start.
17927351992SAlex Bennée  *
18027351992SAlex Bennée  * Return: 0 on success, < 0 on error.
18127351992SAlex Bennée  */
182b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
18327351992SAlex Bennée 
18427351992SAlex Bennée /**
18527351992SAlex Bennée  * vhost_dev_disable_notifiers - disable event notifications
18627351992SAlex Bennée  * @hdev: common vhost_dev structure
18727351992SAlex Bennée  * @vdev: the VirtIODevice structure
18827351992SAlex Bennée  *
18927351992SAlex Bennée  * Disable direct notifications to vhost device.
19027351992SAlex Bennée  */
191b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
192f9a09ca3SCindy Lu bool vhost_config_pending(struct vhost_dev *hdev);
193f9a09ca3SCindy Lu void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask);
194d5970055SMichael S. Tsirkin 
19527351992SAlex Bennée /**
196b8f3e6a1SAlex Bennée  * vhost_dev_is_started() - report status of vhost device
197b8f3e6a1SAlex Bennée  * @hdev: common vhost_dev structure
198b8f3e6a1SAlex Bennée  *
199b8f3e6a1SAlex Bennée  * Return the started status of the vhost device
200b8f3e6a1SAlex Bennée  */
201b8f3e6a1SAlex Bennée static inline bool vhost_dev_is_started(struct vhost_dev *hdev)
202b8f3e6a1SAlex Bennée {
203b8f3e6a1SAlex Bennée     return hdev->started;
204b8f3e6a1SAlex Bennée }
205b8f3e6a1SAlex Bennée 
206b8f3e6a1SAlex Bennée /**
20727351992SAlex Bennée  * vhost_dev_start() - start the vhost device
20827351992SAlex Bennée  * @hdev: common vhost_dev structure
20927351992SAlex Bennée  * @vdev: the VirtIODevice structure
2104daa5054SStefano Garzarella  * @vrings: true to have vrings enabled in this call
21127351992SAlex Bennée  *
21227351992SAlex Bennée  * Starts the vhost device. From this point VirtIO feature negotiation
21327351992SAlex Bennée  * can start and the device can start processing VirtIO transactions.
21427351992SAlex Bennée  *
21527351992SAlex Bennée  * Return: 0 on success, < 0 on error.
21627351992SAlex Bennée  */
2174daa5054SStefano Garzarella int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings);
21827351992SAlex Bennée 
21927351992SAlex Bennée /**
22027351992SAlex Bennée  * vhost_dev_stop() - stop the vhost device
22127351992SAlex Bennée  * @hdev: common vhost_dev structure
22227351992SAlex Bennée  * @vdev: the VirtIODevice structure
2234daa5054SStefano Garzarella  * @vrings: true to have vrings disabled in this call
22427351992SAlex Bennée  *
22527351992SAlex Bennée  * Stop the vhost device. After the device is stopped the notifiers
22627351992SAlex Bennée  * can be disabled (@vhost_dev_disable_notifiers) and the device can
22727351992SAlex Bennée  * be torn down (@vhost_dev_cleanup).
22827351992SAlex Bennée  */
2294daa5054SStefano Garzarella void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings);
23027351992SAlex Bennée 
23127351992SAlex Bennée /**
23227351992SAlex Bennée  * DOC: vhost device configuration handling
23327351992SAlex Bennée  *
23427351992SAlex Bennée  * The VirtIO device configuration space is used for rarely changing
23527351992SAlex Bennée  * or initialisation time parameters. The configuration can be updated
23627351992SAlex Bennée  * by either the guest driver or the device itself. If the device can
23727351992SAlex Bennée  * change the configuration over time the vhost handler should
23827351992SAlex Bennée  * register a @VhostDevConfigOps structure with
23927351992SAlex Bennée  * @vhost_dev_set_config_notifier so the guest can be notified. Some
24027351992SAlex Bennée  * devices register a handler anyway and will signal an error if an
24127351992SAlex Bennée  * unexpected config change happens.
24227351992SAlex Bennée  */
24327351992SAlex Bennée 
24427351992SAlex Bennée /**
24527351992SAlex Bennée  * vhost_dev_get_config() - fetch device configuration
24627351992SAlex Bennée  * @hdev: common vhost_dev_structure
24727351992SAlex Bennée  * @config: pointer to device appropriate config structure
24827351992SAlex Bennée  * @config_len: size of device appropriate config structure
24927351992SAlex Bennée  *
25027351992SAlex Bennée  * Return: 0 on success, < 0 on error while setting errp
25127351992SAlex Bennée  */
25227351992SAlex Bennée int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
25327351992SAlex Bennée                          uint32_t config_len, Error **errp);
25427351992SAlex Bennée 
25527351992SAlex Bennée /**
25627351992SAlex Bennée  * vhost_dev_set_config() - set device configuration
25727351992SAlex Bennée  * @hdev: common vhost_dev_structure
25827351992SAlex Bennée  * @data: pointer to data to set
25927351992SAlex Bennée  * @offset: offset into configuration space
26027351992SAlex Bennée  * @size: length of set
26127351992SAlex Bennée  * @flags: @VhostSetConfigType flags
26227351992SAlex Bennée  *
26327351992SAlex Bennée  * By use of @offset/@size a subset of the configuration space can be
26427351992SAlex Bennée  * written to. The @flags are used to indicate if it is a normal
26527351992SAlex Bennée  * transaction or related to migration.
26627351992SAlex Bennée  *
26727351992SAlex Bennée  * Return: 0 on success, non-zero on error
26827351992SAlex Bennée  */
26927351992SAlex Bennée int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
27027351992SAlex Bennée                          uint32_t offset, uint32_t size, uint32_t flags);
27127351992SAlex Bennée 
27227351992SAlex Bennée /**
27327351992SAlex Bennée  * vhost_dev_set_config_notifier() - register VhostDevConfigOps
27427351992SAlex Bennée  * @hdev: common vhost_dev_structure
27527351992SAlex Bennée  * @ops: notifier ops
27627351992SAlex Bennée  *
27727351992SAlex Bennée  * If the device is expected to change configuration a notifier can be
27827351992SAlex Bennée  * setup to handle the case.
27927351992SAlex Bennée  */
28027351992SAlex Bennée void vhost_dev_set_config_notifier(struct vhost_dev *dev,
28127351992SAlex Bennée                                    const VhostDevConfigOps *ops);
28227351992SAlex Bennée 
28327351992SAlex Bennée 
284f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status.
285f56a1247SMichael S. Tsirkin  * Should be called after unmask to avoid losing events.
286f56a1247SMichael S. Tsirkin  */
287f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
288f56a1247SMichael S. Tsirkin 
289f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq.
290f56a1247SMichael S. Tsirkin  */
291f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
292f56a1247SMichael S. Tsirkin                           bool mask);
2932055c2a4SAlex Bennée 
2942055c2a4SAlex Bennée /**
2952055c2a4SAlex Bennée  * vhost_get_features() - return a sanitised set of feature bits
2962055c2a4SAlex Bennée  * @hdev: common vhost_dev structure
2972055c2a4SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
2982055c2a4SAlex Bennée  * @features: original feature set
2992055c2a4SAlex Bennée  *
3002055c2a4SAlex Bennée  * This returns a set of features bits that is an intersection of what
3012055c2a4SAlex Bennée  * is supported by the vhost backend (hdev->features), the supported
3022055c2a4SAlex Bennée  * feature_bits and the requested feature set.
3032055c2a4SAlex Bennée  */
3049a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
3059a2ba823SCornelia Huck                             uint64_t features);
30681cf38f3SAlex Bennée 
30781cf38f3SAlex Bennée /**
30881cf38f3SAlex Bennée  * vhost_ack_features() - set vhost acked_features
30981cf38f3SAlex Bennée  * @hdev: common vhost_dev structure
31081cf38f3SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
31181cf38f3SAlex Bennée  * @features: requested feature set
31281cf38f3SAlex Bennée  *
31381cf38f3SAlex Bennée  * This sets the internal hdev->acked_features to the intersection of
31481cf38f3SAlex Bennée  * the backends advertised features and the supported feature_bits.
31581cf38f3SAlex Bennée  */
3162e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
3179a2ba823SCornelia Huck                         uint64_t features);
318*cd89c065SDavid Hildenbrand unsigned int vhost_get_max_memslots(void);
3198c49951cSDavid Hildenbrand unsigned int vhost_get_free_memslots(void);
320950d94baSMarc-André Lureau 
321950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev,
322950d94baSMarc-André Lureau                           struct vhost_vring_file *file);
323950d94baSMarc-André Lureau 
324ee071f67SViktor Prutyanov void vhost_toggle_device_iotlb(VirtIODevice *vdev);
325fc58bd0dSMaxime Coquelin int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
3265ad204bfSXie Yongji 
327ff48b628SKangjie Xu int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev,
328ff48b628SKangjie Xu                           struct vhost_virtqueue *vq, unsigned idx);
329e1f101d9SKangjie Xu void vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev,
330e1f101d9SKangjie Xu                           struct vhost_virtqueue *vq, unsigned idx);
331ff48b628SKangjie Xu 
3325ad204bfSXie Yongji void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
3335ad204bfSXie Yongji void vhost_dev_free_inflight(struct vhost_inflight *inflight);
3345ad204bfSXie Yongji void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
3355ad204bfSXie Yongji int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
3361b0063b3SJin Yu int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
3375ad204bfSXie Yongji int vhost_dev_set_inflight(struct vhost_dev *dev,
3385ad204bfSXie Yongji                            struct vhost_inflight *inflight);
3395ad204bfSXie Yongji int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
3405ad204bfSXie Yongji                            struct vhost_inflight *inflight);
34174b5d2b5SCindy Lu bool vhost_dev_has_iommu(struct vhost_dev *dev);
342d5970055SMichael S. Tsirkin #endif
343