xref: /qemu/include/hw/virtio/vhost.h (revision f3034ad71fcd0a6a58bc37830f182b307f089159)
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 
8*f3034ad7SLaurent Vivier #define VHOST_F_DEVICE_IOTLB 63
9*f3034ad7SLaurent Vivier #define VHOST_USER_F_PROTOCOL_FEATURES 30
10*f3034ad7SLaurent 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;
36c471ad0eSJason Wang     struct vhost_dev *dev;
37d5970055SMichael S. Tsirkin };
38d5970055SMichael S. Tsirkin 
39d5970055SMichael S. Tsirkin typedef unsigned long vhost_log_chunk_t;
40d5970055SMichael S. Tsirkin #define VHOST_LOG_PAGE 0x1000
41d5970055SMichael S. Tsirkin #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
42d5970055SMichael S. Tsirkin #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
432e6d46d7SNikolay Nikolaev #define VHOST_INVALID_FEATURE_BIT   (0xff)
44d5970055SMichael S. Tsirkin 
45309750faSJason Wang struct vhost_log {
46309750faSJason Wang     unsigned long long size;
47309750faSJason Wang     int refcnt;
4815324404SMarc-André Lureau     int fd;
4915324404SMarc-André Lureau     vhost_log_chunk_t *log;
50309750faSJason Wang };
51309750faSJason Wang 
52375f74f4SJason Wang struct vhost_dev;
53375f74f4SJason Wang struct vhost_iommu {
54375f74f4SJason Wang     struct vhost_dev *hdev;
55375f74f4SJason Wang     MemoryRegion *mr;
56375f74f4SJason Wang     hwaddr iommu_offset;
57375f74f4SJason Wang     IOMMUNotifier n;
58375f74f4SJason Wang     QLIST_ENTRY(vhost_iommu) iommu_next;
59375f74f4SJason Wang };
60375f74f4SJason Wang 
614c3e257bSChangpeng Liu typedef struct VhostDevConfigOps {
624c3e257bSChangpeng Liu     /* Vhost device config space changed callback
634c3e257bSChangpeng Liu      */
644c3e257bSChangpeng Liu     int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
654c3e257bSChangpeng Liu } VhostDevConfigOps;
664c3e257bSChangpeng Liu 
67d5970055SMichael S. Tsirkin struct vhost_memory;
6827351992SAlex Bennée 
6927351992SAlex Bennée /**
7027351992SAlex Bennée  * struct vhost_dev - common vhost_dev structure
7127351992SAlex Bennée  * @vhost_ops: backend specific ops
7227351992SAlex Bennée  * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier)
7327351992SAlex Bennée  */
74d5970055SMichael S. Tsirkin struct vhost_dev {
75c471ad0eSJason Wang     VirtIODevice *vdev;
7604097f7cSAvi Kivity     MemoryListener memory_listener;
77375f74f4SJason Wang     MemoryListener iommu_listener;
78d5970055SMichael S. Tsirkin     struct vhost_memory *mem;
792817b260SAvi Kivity     int n_mem_sections;
802817b260SAvi Kivity     MemoryRegionSection *mem_sections;
81c44317efSDr. David Alan Gilbert     int n_tmp_sections;
82c44317efSDr. David Alan Gilbert     MemoryRegionSection *tmp_sections;
83d5970055SMichael S. Tsirkin     struct vhost_virtqueue *vqs;
845fc13603SJason Wang     unsigned int nvqs;
859be6e69fSGreg Kurz     /* the first virtqueue which would be used by this vhost dev */
86a9f98bb5SJason Wang     int vq_index;
87245cf2c2SEugenio Pérez     /* one past the last vq index for the virtio device (not vhost) */
88245cf2c2SEugenio Pérez     int vq_index_end;
89c90bd505SKevin Wolf     /* if non-zero, minimum required value for max_queues */
90c90bd505SKevin Wolf     int num_queues;
9121e70425SMarc-André Lureau     uint64_t features;
92c7066f2dSAlex Bennée     /** @acked_features: final set of negotiated features */
9321e70425SMarc-André Lureau     uint64_t acked_features;
94c7066f2dSAlex Bennée     /** @backend_features: backend specific feature bits */
9521e70425SMarc-André Lureau     uint64_t backend_features;
96c7066f2dSAlex Bennée     /** @protocol_features: final negotiated protocol features */
9721e70425SMarc-André Lureau     uint64_t protocol_features;
9821e70425SMarc-André Lureau     uint64_t max_queues;
99b37556edSJason Wang     uint64_t backend_cap;
100b8f3e6a1SAlex Bennée     /* @started: is the vhost device started? */
101d5970055SMichael S. Tsirkin     bool started;
102d5970055SMichael S. Tsirkin     bool log_enabled;
10321e70425SMarc-André Lureau     uint64_t log_size;
1047145872eSMichael S. Tsirkin     Error *migration_blocker;
10524d1eb33SNikolay Nikolaev     const VhostOps *vhost_ops;
1061a1bfac9SNikolay Nikolaev     void *opaque;
107309750faSJason Wang     struct vhost_log *log;
1082ce68e4cSIgor Mammedov     QLIST_ENTRY(vhost_dev) entry;
109375f74f4SJason Wang     QLIST_HEAD(, vhost_iommu) iommu_list;
110c471ad0eSJason Wang     IOMMUNotifier n;
1114c3e257bSChangpeng Liu     const VhostDevConfigOps *config_ops;
112d5970055SMichael S. Tsirkin };
113d5970055SMichael S. Tsirkin 
1149b1d929aSTiberiu Georgescu extern const VhostOps kernel_ops;
1159b1d929aSTiberiu Georgescu extern const VhostOps user_ops;
1169b1d929aSTiberiu Georgescu extern const VhostOps vdpa_ops;
1179b1d929aSTiberiu Georgescu 
118108a6481SCindy Lu struct vhost_net {
119108a6481SCindy Lu     struct vhost_dev dev;
120108a6481SCindy Lu     struct vhost_virtqueue vqs[2];
121108a6481SCindy Lu     int backend;
122108a6481SCindy Lu     NetClientState *nc;
123108a6481SCindy Lu };
124108a6481SCindy Lu 
12527351992SAlex Bennée /**
12627351992SAlex Bennée  * vhost_dev_init() - initialise the vhost interface
12727351992SAlex Bennée  * @hdev: the common vhost_dev structure
12827351992SAlex Bennée  * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa)
12927351992SAlex Bennée  * @backend_type: type of backend
13027351992SAlex Bennée  * @busyloop_timeout: timeout for polling virtqueue
13127351992SAlex Bennée  * @errp: error handle
13227351992SAlex Bennée  *
13327351992SAlex Bennée  * The initialisation of the vhost device will trigger the
13427351992SAlex Bennée  * initialisation of the backend and potentially capability
13527351992SAlex Bennée  * negotiation of backend interface. Configuration of the VirtIO
13627351992SAlex Bennée  * itself won't happen until the interface is started.
13727351992SAlex Bennée  *
13827351992SAlex Bennée  * Return: 0 on success, non-zero on error while setting errp.
13927351992SAlex Bennée  */
14081647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
14169e87b32SJason Wang                    VhostBackendType backend_type,
142a6945f22SKevin Wolf                    uint32_t busyloop_timeout, Error **errp);
14327351992SAlex Bennée 
14427351992SAlex Bennée /**
14527351992SAlex Bennée  * vhost_dev_cleanup() - tear down and cleanup vhost interface
14627351992SAlex Bennée  * @hdev: the common vhost_dev structure
14727351992SAlex Bennée  */
148d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev);
14927351992SAlex Bennée 
15027351992SAlex Bennée /**
15127351992SAlex Bennée  * vhost_dev_enable_notifiers() - enable event notifiers
15227351992SAlex Bennée  * @hdev: common vhost_dev structure
15327351992SAlex Bennée  * @vdev: the VirtIODevice structure
15427351992SAlex Bennée  *
15527351992SAlex Bennée  * Enable notifications directly to the vhost device rather than being
15627351992SAlex Bennée  * triggered by QEMU itself. Notifications should be enabled before
15727351992SAlex Bennée  * the vhost device is started via @vhost_dev_start.
15827351992SAlex Bennée  *
15927351992SAlex Bennée  * Return: 0 on success, < 0 on error.
16027351992SAlex Bennée  */
161b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
16227351992SAlex Bennée 
16327351992SAlex Bennée /**
16427351992SAlex Bennée  * vhost_dev_disable_notifiers - disable event notifications
16527351992SAlex Bennée  * @hdev: common vhost_dev structure
16627351992SAlex Bennée  * @vdev: the VirtIODevice structure
16727351992SAlex Bennée  *
16827351992SAlex Bennée  * Disable direct notifications to vhost device.
16927351992SAlex Bennée  */
170b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
171d5970055SMichael S. Tsirkin 
17227351992SAlex Bennée /**
173b8f3e6a1SAlex Bennée  * vhost_dev_is_started() - report status of vhost device
174b8f3e6a1SAlex Bennée  * @hdev: common vhost_dev structure
175b8f3e6a1SAlex Bennée  *
176b8f3e6a1SAlex Bennée  * Return the started status of the vhost device
177b8f3e6a1SAlex Bennée  */
178b8f3e6a1SAlex Bennée static inline bool vhost_dev_is_started(struct vhost_dev *hdev)
179b8f3e6a1SAlex Bennée {
180b8f3e6a1SAlex Bennée     return hdev->started;
181b8f3e6a1SAlex Bennée }
182b8f3e6a1SAlex Bennée 
183b8f3e6a1SAlex Bennée /**
18427351992SAlex Bennée  * vhost_dev_start() - start the vhost device
18527351992SAlex Bennée  * @hdev: common vhost_dev structure
18627351992SAlex Bennée  * @vdev: the VirtIODevice structure
18727351992SAlex Bennée  *
18827351992SAlex Bennée  * Starts the vhost device. From this point VirtIO feature negotiation
18927351992SAlex Bennée  * can start and the device can start processing VirtIO transactions.
19027351992SAlex Bennée  *
19127351992SAlex Bennée  * Return: 0 on success, < 0 on error.
19227351992SAlex Bennée  */
19327351992SAlex Bennée int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
19427351992SAlex Bennée 
19527351992SAlex Bennée /**
19627351992SAlex Bennée  * vhost_dev_stop() - stop the vhost device
19727351992SAlex Bennée  * @hdev: common vhost_dev structure
19827351992SAlex Bennée  * @vdev: the VirtIODevice structure
19927351992SAlex Bennée  *
20027351992SAlex Bennée  * Stop the vhost device. After the device is stopped the notifiers
20127351992SAlex Bennée  * can be disabled (@vhost_dev_disable_notifiers) and the device can
20227351992SAlex Bennée  * be torn down (@vhost_dev_cleanup).
20327351992SAlex Bennée  */
20427351992SAlex Bennée void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
20527351992SAlex Bennée 
20627351992SAlex Bennée /**
20727351992SAlex Bennée  * DOC: vhost device configuration handling
20827351992SAlex Bennée  *
20927351992SAlex Bennée  * The VirtIO device configuration space is used for rarely changing
21027351992SAlex Bennée  * or initialisation time parameters. The configuration can be updated
21127351992SAlex Bennée  * by either the guest driver or the device itself. If the device can
21227351992SAlex Bennée  * change the configuration over time the vhost handler should
21327351992SAlex Bennée  * register a @VhostDevConfigOps structure with
21427351992SAlex Bennée  * @vhost_dev_set_config_notifier so the guest can be notified. Some
21527351992SAlex Bennée  * devices register a handler anyway and will signal an error if an
21627351992SAlex Bennée  * unexpected config change happens.
21727351992SAlex Bennée  */
21827351992SAlex Bennée 
21927351992SAlex Bennée /**
22027351992SAlex Bennée  * vhost_dev_get_config() - fetch device configuration
22127351992SAlex Bennée  * @hdev: common vhost_dev_structure
22227351992SAlex Bennée  * @config: pointer to device appropriate config structure
22327351992SAlex Bennée  * @config_len: size of device appropriate config structure
22427351992SAlex Bennée  *
22527351992SAlex Bennée  * Return: 0 on success, < 0 on error while setting errp
22627351992SAlex Bennée  */
22727351992SAlex Bennée int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
22827351992SAlex Bennée                          uint32_t config_len, Error **errp);
22927351992SAlex Bennée 
23027351992SAlex Bennée /**
23127351992SAlex Bennée  * vhost_dev_set_config() - set device configuration
23227351992SAlex Bennée  * @hdev: common vhost_dev_structure
23327351992SAlex Bennée  * @data: pointer to data to set
23427351992SAlex Bennée  * @offset: offset into configuration space
23527351992SAlex Bennée  * @size: length of set
23627351992SAlex Bennée  * @flags: @VhostSetConfigType flags
23727351992SAlex Bennée  *
23827351992SAlex Bennée  * By use of @offset/@size a subset of the configuration space can be
23927351992SAlex Bennée  * written to. The @flags are used to indicate if it is a normal
24027351992SAlex Bennée  * transaction or related to migration.
24127351992SAlex Bennée  *
24227351992SAlex Bennée  * Return: 0 on success, non-zero on error
24327351992SAlex Bennée  */
24427351992SAlex Bennée int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
24527351992SAlex Bennée                          uint32_t offset, uint32_t size, uint32_t flags);
24627351992SAlex Bennée 
24727351992SAlex Bennée /**
24827351992SAlex Bennée  * vhost_dev_set_config_notifier() - register VhostDevConfigOps
24927351992SAlex Bennée  * @hdev: common vhost_dev_structure
25027351992SAlex Bennée  * @ops: notifier ops
25127351992SAlex Bennée  *
25227351992SAlex Bennée  * If the device is expected to change configuration a notifier can be
25327351992SAlex Bennée  * setup to handle the case.
25427351992SAlex Bennée  */
25527351992SAlex Bennée void vhost_dev_set_config_notifier(struct vhost_dev *dev,
25627351992SAlex Bennée                                    const VhostDevConfigOps *ops);
25727351992SAlex Bennée 
25827351992SAlex Bennée 
259f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status.
260f56a1247SMichael S. Tsirkin  * Should be called after unmask to avoid losing events.
261f56a1247SMichael S. Tsirkin  */
262f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
263f56a1247SMichael S. Tsirkin 
264f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq.
265f56a1247SMichael S. Tsirkin  */
266f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
267f56a1247SMichael S. Tsirkin                           bool mask);
2682055c2a4SAlex Bennée 
2692055c2a4SAlex Bennée /**
2702055c2a4SAlex Bennée  * vhost_get_features() - return a sanitised set of feature bits
2712055c2a4SAlex Bennée  * @hdev: common vhost_dev structure
2722055c2a4SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
2732055c2a4SAlex Bennée  * @features: original feature set
2742055c2a4SAlex Bennée  *
2752055c2a4SAlex Bennée  * This returns a set of features bits that is an intersection of what
2762055c2a4SAlex Bennée  * is supported by the vhost backend (hdev->features), the supported
2772055c2a4SAlex Bennée  * feature_bits and the requested feature set.
2782055c2a4SAlex Bennée  */
2799a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
2809a2ba823SCornelia Huck                             uint64_t features);
28181cf38f3SAlex Bennée 
28281cf38f3SAlex Bennée /**
28381cf38f3SAlex Bennée  * vhost_ack_features() - set vhost acked_features
28481cf38f3SAlex Bennée  * @hdev: common vhost_dev structure
28581cf38f3SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
28681cf38f3SAlex Bennée  * @features: requested feature set
28781cf38f3SAlex Bennée  *
28881cf38f3SAlex Bennée  * This sets the internal hdev->acked_features to the intersection of
28981cf38f3SAlex Bennée  * the backends advertised features and the supported feature_bits.
29081cf38f3SAlex Bennée  */
2912e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
2929a2ba823SCornelia Huck                         uint64_t features);
2932ce68e4cSIgor Mammedov bool vhost_has_free_slot(void);
294950d94baSMarc-André Lureau 
295950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev,
296950d94baSMarc-André Lureau                           struct vhost_vring_file *file);
297950d94baSMarc-André Lureau 
298fc58bd0dSMaxime Coquelin int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
2995ad204bfSXie Yongji 
3005ad204bfSXie Yongji void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
3015ad204bfSXie Yongji void vhost_dev_free_inflight(struct vhost_inflight *inflight);
3025ad204bfSXie Yongji void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
3035ad204bfSXie Yongji int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
3041b0063b3SJin Yu int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
3055ad204bfSXie Yongji int vhost_dev_set_inflight(struct vhost_dev *dev,
3065ad204bfSXie Yongji                            struct vhost_inflight *inflight);
3075ad204bfSXie Yongji int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
3085ad204bfSXie Yongji                            struct vhost_inflight *inflight);
309d5970055SMichael S. Tsirkin #endif
310