xref: /openbmc/qemu/include/hw/virtio/vhost.h (revision d5938f29)
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     struct vhost_dev *dev;
33 };
34 
35 typedef unsigned long vhost_log_chunk_t;
36 #define VHOST_LOG_PAGE 0x1000
37 #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
38 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
39 #define VHOST_INVALID_FEATURE_BIT   (0xff)
40 
41 struct vhost_log {
42     unsigned long long size;
43     int refcnt;
44     int fd;
45     vhost_log_chunk_t *log;
46 };
47 
48 struct vhost_dev;
49 struct vhost_iommu {
50     struct vhost_dev *hdev;
51     MemoryRegion *mr;
52     hwaddr iommu_offset;
53     IOMMUNotifier n;
54     QLIST_ENTRY(vhost_iommu) iommu_next;
55 };
56 
57 typedef struct VhostDevConfigOps {
58     /* Vhost device config space changed callback
59      */
60     int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
61 } VhostDevConfigOps;
62 
63 struct vhost_memory;
64 struct vhost_dev {
65     VirtIODevice *vdev;
66     MemoryListener memory_listener;
67     MemoryListener iommu_listener;
68     struct vhost_memory *mem;
69     int n_mem_sections;
70     MemoryRegionSection *mem_sections;
71     int n_tmp_sections;
72     MemoryRegionSection *tmp_sections;
73     struct vhost_virtqueue *vqs;
74     int nvqs;
75     /* the first virtqueue which would be used by this vhost dev */
76     int vq_index;
77     uint64_t features;
78     uint64_t acked_features;
79     uint64_t backend_features;
80     uint64_t protocol_features;
81     uint64_t max_queues;
82     bool started;
83     bool log_enabled;
84     uint64_t log_size;
85     Error *migration_blocker;
86     const VhostOps *vhost_ops;
87     void *opaque;
88     struct vhost_log *log;
89     QLIST_ENTRY(vhost_dev) entry;
90     QLIST_HEAD(, vhost_iommu) iommu_list;
91     IOMMUNotifier n;
92     const VhostDevConfigOps *config_ops;
93 };
94 
95 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
96                    VhostBackendType backend_type,
97                    uint32_t busyloop_timeout);
98 void vhost_dev_cleanup(struct vhost_dev *hdev);
99 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
100 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
101 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
102 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
103 
104 /* Test and clear masked event pending status.
105  * Should be called after unmask to avoid losing events.
106  */
107 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
108 
109 /* Mask/unmask events from this vq.
110  */
111 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
112                           bool mask);
113 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
114                             uint64_t features);
115 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
116                         uint64_t features);
117 bool vhost_has_free_slot(void);
118 
119 int vhost_net_set_backend(struct vhost_dev *hdev,
120                           struct vhost_vring_file *file);
121 
122 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
123 int vhost_dev_get_config(struct vhost_dev *dev, uint8_t *config,
124                          uint32_t config_len);
125 int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
126                          uint32_t offset, uint32_t size, uint32_t flags);
127 /* notifier callback in case vhost device config space changed
128  */
129 void vhost_dev_set_config_notifier(struct vhost_dev *dev,
130                                    const VhostDevConfigOps *ops);
131 
132 void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
133 void vhost_dev_free_inflight(struct vhost_inflight *inflight);
134 void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
135 int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
136 int vhost_dev_set_inflight(struct vhost_dev *dev,
137                            struct vhost_inflight *inflight);
138 int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
139                            struct vhost_inflight *inflight);
140 #endif
141