xref: /qemu/include/hw/virtio/vhost.h (revision 81cf38f3ff3c7db8fcd2f46df9a294fdf6f4a910)
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 
8d5970055SMichael S. Tsirkin /* Generic structures common for any vhost based device. */
95ad204bfSXie Yongji 
105ad204bfSXie Yongji struct vhost_inflight {
115ad204bfSXie Yongji     int fd;
125ad204bfSXie Yongji     void *addr;
135ad204bfSXie Yongji     uint64_t size;
145ad204bfSXie Yongji     uint64_t offset;
155ad204bfSXie Yongji     uint16_t queue_size;
165ad204bfSXie Yongji };
175ad204bfSXie Yongji 
18d5970055SMichael S. Tsirkin struct vhost_virtqueue {
19d5970055SMichael S. Tsirkin     int kick;
20d5970055SMichael S. Tsirkin     int call;
21d5970055SMichael S. Tsirkin     void *desc;
22d5970055SMichael S. Tsirkin     void *avail;
23d5970055SMichael S. Tsirkin     void *used;
24d5970055SMichael S. Tsirkin     int num;
25f1f9e6c5SGreg Kurz     unsigned long long desc_phys;
26f1f9e6c5SGreg Kurz     unsigned desc_size;
27f1f9e6c5SGreg Kurz     unsigned long long avail_phys;
28f1f9e6c5SGreg Kurz     unsigned avail_size;
29d5970055SMichael S. Tsirkin     unsigned long long used_phys;
30d5970055SMichael S. Tsirkin     unsigned used_size;
31f56a1247SMichael S. Tsirkin     EventNotifier masked_notifier;
32ae50ae0bSKonstantin Khlebnikov     EventNotifier error_notifier;
33c471ad0eSJason Wang     struct vhost_dev *dev;
34d5970055SMichael S. Tsirkin };
35d5970055SMichael S. Tsirkin 
36d5970055SMichael S. Tsirkin typedef unsigned long vhost_log_chunk_t;
37d5970055SMichael S. Tsirkin #define VHOST_LOG_PAGE 0x1000
38d5970055SMichael S. Tsirkin #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
39d5970055SMichael S. Tsirkin #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
402e6d46d7SNikolay Nikolaev #define VHOST_INVALID_FEATURE_BIT   (0xff)
41d5970055SMichael S. Tsirkin 
42309750faSJason Wang struct vhost_log {
43309750faSJason Wang     unsigned long long size;
44309750faSJason Wang     int refcnt;
4515324404SMarc-André Lureau     int fd;
4615324404SMarc-André Lureau     vhost_log_chunk_t *log;
47309750faSJason Wang };
48309750faSJason Wang 
49375f74f4SJason Wang struct vhost_dev;
50375f74f4SJason Wang struct vhost_iommu {
51375f74f4SJason Wang     struct vhost_dev *hdev;
52375f74f4SJason Wang     MemoryRegion *mr;
53375f74f4SJason Wang     hwaddr iommu_offset;
54375f74f4SJason Wang     IOMMUNotifier n;
55375f74f4SJason Wang     QLIST_ENTRY(vhost_iommu) iommu_next;
56375f74f4SJason Wang };
57375f74f4SJason Wang 
584c3e257bSChangpeng Liu typedef struct VhostDevConfigOps {
594c3e257bSChangpeng Liu     /* Vhost device config space changed callback
604c3e257bSChangpeng Liu      */
614c3e257bSChangpeng Liu     int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
624c3e257bSChangpeng Liu } VhostDevConfigOps;
634c3e257bSChangpeng Liu 
64d5970055SMichael S. Tsirkin struct vhost_memory;
6527351992SAlex Bennée 
6627351992SAlex Bennée /**
6727351992SAlex Bennée  * struct vhost_dev - common vhost_dev structure
6827351992SAlex Bennée  * @vhost_ops: backend specific ops
6927351992SAlex Bennée  * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier)
7027351992SAlex Bennée  */
71d5970055SMichael S. Tsirkin struct vhost_dev {
72c471ad0eSJason Wang     VirtIODevice *vdev;
7304097f7cSAvi Kivity     MemoryListener memory_listener;
74375f74f4SJason Wang     MemoryListener iommu_listener;
75d5970055SMichael S. Tsirkin     struct vhost_memory *mem;
762817b260SAvi Kivity     int n_mem_sections;
772817b260SAvi Kivity     MemoryRegionSection *mem_sections;
78c44317efSDr. David Alan Gilbert     int n_tmp_sections;
79c44317efSDr. David Alan Gilbert     MemoryRegionSection *tmp_sections;
80d5970055SMichael S. Tsirkin     struct vhost_virtqueue *vqs;
815fc13603SJason Wang     unsigned int nvqs;
829be6e69fSGreg Kurz     /* the first virtqueue which would be used by this vhost dev */
83a9f98bb5SJason Wang     int vq_index;
84245cf2c2SEugenio Pérez     /* one past the last vq index for the virtio device (not vhost) */
85245cf2c2SEugenio Pérez     int vq_index_end;
86c90bd505SKevin Wolf     /* if non-zero, minimum required value for max_queues */
87c90bd505SKevin Wolf     int num_queues;
8821e70425SMarc-André Lureau     uint64_t features;
8921e70425SMarc-André Lureau     uint64_t acked_features;
9021e70425SMarc-André Lureau     uint64_t backend_features;
9121e70425SMarc-André Lureau     uint64_t protocol_features;
9221e70425SMarc-André Lureau     uint64_t max_queues;
93b37556edSJason Wang     uint64_t backend_cap;
94d5970055SMichael S. Tsirkin     bool started;
95d5970055SMichael S. Tsirkin     bool log_enabled;
9621e70425SMarc-André Lureau     uint64_t log_size;
977145872eSMichael S. Tsirkin     Error *migration_blocker;
9824d1eb33SNikolay Nikolaev     const VhostOps *vhost_ops;
991a1bfac9SNikolay Nikolaev     void *opaque;
100309750faSJason Wang     struct vhost_log *log;
1012ce68e4cSIgor Mammedov     QLIST_ENTRY(vhost_dev) entry;
102375f74f4SJason Wang     QLIST_HEAD(, vhost_iommu) iommu_list;
103c471ad0eSJason Wang     IOMMUNotifier n;
1044c3e257bSChangpeng Liu     const VhostDevConfigOps *config_ops;
105d5970055SMichael S. Tsirkin };
106d5970055SMichael S. Tsirkin 
1079b1d929aSTiberiu Georgescu extern const VhostOps kernel_ops;
1089b1d929aSTiberiu Georgescu extern const VhostOps user_ops;
1099b1d929aSTiberiu Georgescu extern const VhostOps vdpa_ops;
1109b1d929aSTiberiu Georgescu 
111108a6481SCindy Lu struct vhost_net {
112108a6481SCindy Lu     struct vhost_dev dev;
113108a6481SCindy Lu     struct vhost_virtqueue vqs[2];
114108a6481SCindy Lu     int backend;
115108a6481SCindy Lu     NetClientState *nc;
116108a6481SCindy Lu };
117108a6481SCindy Lu 
11827351992SAlex Bennée /**
11927351992SAlex Bennée  * vhost_dev_init() - initialise the vhost interface
12027351992SAlex Bennée  * @hdev: the common vhost_dev structure
12127351992SAlex Bennée  * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa)
12227351992SAlex Bennée  * @backend_type: type of backend
12327351992SAlex Bennée  * @busyloop_timeout: timeout for polling virtqueue
12427351992SAlex Bennée  * @errp: error handle
12527351992SAlex Bennée  *
12627351992SAlex Bennée  * The initialisation of the vhost device will trigger the
12727351992SAlex Bennée  * initialisation of the backend and potentially capability
12827351992SAlex Bennée  * negotiation of backend interface. Configuration of the VirtIO
12927351992SAlex Bennée  * itself won't happen until the interface is started.
13027351992SAlex Bennée  *
13127351992SAlex Bennée  * Return: 0 on success, non-zero on error while setting errp.
13227351992SAlex Bennée  */
13381647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
13469e87b32SJason Wang                    VhostBackendType backend_type,
135a6945f22SKevin Wolf                    uint32_t busyloop_timeout, Error **errp);
13627351992SAlex Bennée 
13727351992SAlex Bennée /**
13827351992SAlex Bennée  * vhost_dev_cleanup() - tear down and cleanup vhost interface
13927351992SAlex Bennée  * @hdev: the common vhost_dev structure
14027351992SAlex Bennée  */
141d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev);
14227351992SAlex Bennée 
14327351992SAlex Bennée /**
14427351992SAlex Bennée  * vhost_dev_enable_notifiers() - enable event notifiers
14527351992SAlex Bennée  * @hdev: common vhost_dev structure
14627351992SAlex Bennée  * @vdev: the VirtIODevice structure
14727351992SAlex Bennée  *
14827351992SAlex Bennée  * Enable notifications directly to the vhost device rather than being
14927351992SAlex Bennée  * triggered by QEMU itself. Notifications should be enabled before
15027351992SAlex Bennée  * the vhost device is started via @vhost_dev_start.
15127351992SAlex Bennée  *
15227351992SAlex Bennée  * Return: 0 on success, < 0 on error.
15327351992SAlex Bennée  */
154b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
15527351992SAlex Bennée 
15627351992SAlex Bennée /**
15727351992SAlex Bennée  * vhost_dev_disable_notifiers - disable event notifications
15827351992SAlex Bennée  * @hdev: common vhost_dev structure
15927351992SAlex Bennée  * @vdev: the VirtIODevice structure
16027351992SAlex Bennée  *
16127351992SAlex Bennée  * Disable direct notifications to vhost device.
16227351992SAlex Bennée  */
163b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
164d5970055SMichael S. Tsirkin 
16527351992SAlex Bennée /**
16627351992SAlex Bennée  * vhost_dev_start() - start the vhost device
16727351992SAlex Bennée  * @hdev: common vhost_dev structure
16827351992SAlex Bennée  * @vdev: the VirtIODevice structure
16927351992SAlex Bennée  *
17027351992SAlex Bennée  * Starts the vhost device. From this point VirtIO feature negotiation
17127351992SAlex Bennée  * can start and the device can start processing VirtIO transactions.
17227351992SAlex Bennée  *
17327351992SAlex Bennée  * Return: 0 on success, < 0 on error.
17427351992SAlex Bennée  */
17527351992SAlex Bennée int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
17627351992SAlex Bennée 
17727351992SAlex Bennée /**
17827351992SAlex Bennée  * vhost_dev_stop() - stop the vhost device
17927351992SAlex Bennée  * @hdev: common vhost_dev structure
18027351992SAlex Bennée  * @vdev: the VirtIODevice structure
18127351992SAlex Bennée  *
18227351992SAlex Bennée  * Stop the vhost device. After the device is stopped the notifiers
18327351992SAlex Bennée  * can be disabled (@vhost_dev_disable_notifiers) and the device can
18427351992SAlex Bennée  * be torn down (@vhost_dev_cleanup).
18527351992SAlex Bennée  */
18627351992SAlex Bennée void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
18727351992SAlex Bennée 
18827351992SAlex Bennée /**
18927351992SAlex Bennée  * DOC: vhost device configuration handling
19027351992SAlex Bennée  *
19127351992SAlex Bennée  * The VirtIO device configuration space is used for rarely changing
19227351992SAlex Bennée  * or initialisation time parameters. The configuration can be updated
19327351992SAlex Bennée  * by either the guest driver or the device itself. If the device can
19427351992SAlex Bennée  * change the configuration over time the vhost handler should
19527351992SAlex Bennée  * register a @VhostDevConfigOps structure with
19627351992SAlex Bennée  * @vhost_dev_set_config_notifier so the guest can be notified. Some
19727351992SAlex Bennée  * devices register a handler anyway and will signal an error if an
19827351992SAlex Bennée  * unexpected config change happens.
19927351992SAlex Bennée  */
20027351992SAlex Bennée 
20127351992SAlex Bennée /**
20227351992SAlex Bennée  * vhost_dev_get_config() - fetch device configuration
20327351992SAlex Bennée  * @hdev: common vhost_dev_structure
20427351992SAlex Bennée  * @config: pointer to device appropriate config structure
20527351992SAlex Bennée  * @config_len: size of device appropriate config structure
20627351992SAlex Bennée  *
20727351992SAlex Bennée  * Return: 0 on success, < 0 on error while setting errp
20827351992SAlex Bennée  */
20927351992SAlex Bennée int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
21027351992SAlex Bennée                          uint32_t config_len, Error **errp);
21127351992SAlex Bennée 
21227351992SAlex Bennée /**
21327351992SAlex Bennée  * vhost_dev_set_config() - set device configuration
21427351992SAlex Bennée  * @hdev: common vhost_dev_structure
21527351992SAlex Bennée  * @data: pointer to data to set
21627351992SAlex Bennée  * @offset: offset into configuration space
21727351992SAlex Bennée  * @size: length of set
21827351992SAlex Bennée  * @flags: @VhostSetConfigType flags
21927351992SAlex Bennée  *
22027351992SAlex Bennée  * By use of @offset/@size a subset of the configuration space can be
22127351992SAlex Bennée  * written to. The @flags are used to indicate if it is a normal
22227351992SAlex Bennée  * transaction or related to migration.
22327351992SAlex Bennée  *
22427351992SAlex Bennée  * Return: 0 on success, non-zero on error
22527351992SAlex Bennée  */
22627351992SAlex Bennée int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
22727351992SAlex Bennée                          uint32_t offset, uint32_t size, uint32_t flags);
22827351992SAlex Bennée 
22927351992SAlex Bennée /**
23027351992SAlex Bennée  * vhost_dev_set_config_notifier() - register VhostDevConfigOps
23127351992SAlex Bennée  * @hdev: common vhost_dev_structure
23227351992SAlex Bennée  * @ops: notifier ops
23327351992SAlex Bennée  *
23427351992SAlex Bennée  * If the device is expected to change configuration a notifier can be
23527351992SAlex Bennée  * setup to handle the case.
23627351992SAlex Bennée  */
23727351992SAlex Bennée void vhost_dev_set_config_notifier(struct vhost_dev *dev,
23827351992SAlex Bennée                                    const VhostDevConfigOps *ops);
23927351992SAlex Bennée 
24027351992SAlex Bennée 
241f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status.
242f56a1247SMichael S. Tsirkin  * Should be called after unmask to avoid losing events.
243f56a1247SMichael S. Tsirkin  */
244f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
245f56a1247SMichael S. Tsirkin 
246f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq.
247f56a1247SMichael S. Tsirkin  */
248f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
249f56a1247SMichael S. Tsirkin                           bool mask);
2502055c2a4SAlex Bennée 
2512055c2a4SAlex Bennée /**
2522055c2a4SAlex Bennée  * vhost_get_features() - return a sanitised set of feature bits
2532055c2a4SAlex Bennée  * @hdev: common vhost_dev structure
2542055c2a4SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
2552055c2a4SAlex Bennée  * @features: original feature set
2562055c2a4SAlex Bennée  *
2572055c2a4SAlex Bennée  * This returns a set of features bits that is an intersection of what
2582055c2a4SAlex Bennée  * is supported by the vhost backend (hdev->features), the supported
2592055c2a4SAlex Bennée  * feature_bits and the requested feature set.
2602055c2a4SAlex Bennée  */
2619a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
2629a2ba823SCornelia Huck                             uint64_t features);
263*81cf38f3SAlex Bennée 
264*81cf38f3SAlex Bennée /**
265*81cf38f3SAlex Bennée  * vhost_ack_features() - set vhost acked_features
266*81cf38f3SAlex Bennée  * @hdev: common vhost_dev structure
267*81cf38f3SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
268*81cf38f3SAlex Bennée  * @features: requested feature set
269*81cf38f3SAlex Bennée  *
270*81cf38f3SAlex Bennée  * This sets the internal hdev->acked_features to the intersection of
271*81cf38f3SAlex Bennée  * the backends advertised features and the supported feature_bits.
272*81cf38f3SAlex Bennée  */
2732e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
2749a2ba823SCornelia Huck                         uint64_t features);
2752ce68e4cSIgor Mammedov bool vhost_has_free_slot(void);
276950d94baSMarc-André Lureau 
277950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev,
278950d94baSMarc-André Lureau                           struct vhost_vring_file *file);
279950d94baSMarc-André Lureau 
280fc58bd0dSMaxime Coquelin int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
2815ad204bfSXie Yongji 
2825ad204bfSXie Yongji void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
2835ad204bfSXie Yongji void vhost_dev_free_inflight(struct vhost_inflight *inflight);
2845ad204bfSXie Yongji void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
2855ad204bfSXie Yongji int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
2861b0063b3SJin Yu int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
2875ad204bfSXie Yongji int vhost_dev_set_inflight(struct vhost_dev *dev,
2885ad204bfSXie Yongji                            struct vhost_inflight *inflight);
2895ad204bfSXie Yongji int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
2905ad204bfSXie Yongji                            struct vhost_inflight *inflight);
291d5970055SMichael S. Tsirkin #endif
292