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