xref: /openbmc/qemu/include/hw/virtio/vhost.h (revision 0fbb5d2d)
1 #ifndef VHOST_H
2 #define VHOST_H
3 
4 #include "hw/virtio/vhost-backend.h"
5 #include "hw/virtio/virtio.h"
6 #include "exec/memory.h"
7 
8 /* Generic structures common for any vhost based device. */
9 
10 struct vhost_inflight {
11     int fd;
12     void *addr;
13     uint64_t size;
14     uint64_t offset;
15     uint16_t queue_size;
16 };
17 
18 struct vhost_virtqueue {
19     int kick;
20     int call;
21     void *desc;
22     void *avail;
23     void *used;
24     int num;
25     unsigned long long desc_phys;
26     unsigned desc_size;
27     unsigned long long avail_phys;
28     unsigned avail_size;
29     unsigned long long used_phys;
30     unsigned used_size;
31     EventNotifier masked_notifier;
32     EventNotifier masked_config_notifier;
33     struct vhost_dev *dev;
34 };
35 
36 typedef unsigned long vhost_log_chunk_t;
37 #define VHOST_LOG_PAGE 0x1000
38 #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
39 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
40 #define VHOST_INVALID_FEATURE_BIT   (0xff)
41 #define VHOST_QUEUE_NUM_CONFIG_INR 0
42 
43 struct vhost_log {
44     unsigned long long size;
45     int refcnt;
46     int fd;
47     vhost_log_chunk_t *log;
48 };
49 
50 struct vhost_dev;
51 struct vhost_iommu {
52     struct vhost_dev *hdev;
53     MemoryRegion *mr;
54     hwaddr iommu_offset;
55     IOMMUNotifier n;
56     QLIST_ENTRY(vhost_iommu) iommu_next;
57 };
58 
59 typedef struct VhostDevConfigOps {
60     /* Vhost device config space changed callback
61      */
62     int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
63 } VhostDevConfigOps;
64 
65 struct vhost_memory;
66 struct vhost_dev {
67     VirtIODevice *vdev;
68     MemoryListener memory_listener;
69     MemoryListener iommu_listener;
70     struct vhost_memory *mem;
71     int n_mem_sections;
72     MemoryRegionSection *mem_sections;
73     int n_tmp_sections;
74     MemoryRegionSection *tmp_sections;
75     struct vhost_virtqueue *vqs;
76     unsigned int nvqs;
77     /* the first virtqueue which would be used by this vhost dev */
78     int vq_index;
79     /* one past the last vq index for the virtio device (not vhost) */
80     int vq_index_end;
81     /* if non-zero, minimum required value for max_queues */
82     int num_queues;
83     uint64_t features;
84     uint64_t acked_features;
85     uint64_t backend_features;
86     uint64_t protocol_features;
87     uint64_t max_queues;
88     uint64_t backend_cap;
89     bool started;
90     bool log_enabled;
91     uint64_t log_size;
92     Error *migration_blocker;
93     const VhostOps *vhost_ops;
94     void *opaque;
95     struct vhost_log *log;
96     QLIST_ENTRY(vhost_dev) entry;
97     QLIST_HEAD(, vhost_iommu) iommu_list;
98     IOMMUNotifier n;
99     const VhostDevConfigOps *config_ops;
100 };
101 
102 extern const VhostOps kernel_ops;
103 extern const VhostOps user_ops;
104 extern const VhostOps vdpa_ops;
105 
106 struct vhost_net {
107     struct vhost_dev dev;
108     struct vhost_virtqueue vqs[2];
109     int backend;
110     NetClientState *nc;
111 };
112 
113 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
114                    VhostBackendType backend_type,
115                    uint32_t busyloop_timeout, Error **errp);
116 void vhost_dev_cleanup(struct vhost_dev *hdev);
117 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
118 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
119 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
120 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
121 bool vhost_config_pending(struct vhost_dev *hdev);
122 void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask);
123 
124 /* Test and clear masked event pending status.
125  * Should be called after unmask to avoid losing events.
126  */
127 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
128 
129 /* Mask/unmask events from this vq.
130  */
131 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
132                           bool mask);
133 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
134                             uint64_t features);
135 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
136                         uint64_t features);
137 bool vhost_has_free_slot(void);
138 
139 int vhost_net_set_backend(struct vhost_dev *hdev,
140                           struct vhost_vring_file *file);
141 
142 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
143 int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
144                          uint32_t config_len, Error **errp);
145 int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
146                          uint32_t offset, uint32_t size, uint32_t flags);
147 /* notifier callback in case vhost device config space changed
148  */
149 void vhost_dev_set_config_notifier(struct vhost_dev *dev,
150                                    const VhostDevConfigOps *ops);
151 
152 void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
153 void vhost_dev_free_inflight(struct vhost_inflight *inflight);
154 void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
155 int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
156 int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
157 int vhost_dev_set_inflight(struct vhost_dev *dev,
158                            struct vhost_inflight *inflight);
159 int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
160                            struct vhost_inflight *inflight);
161 #endif
162