xref: /openbmc/qemu/include/hw/virtio/vhost.h (revision 8f0a3716)
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 struct vhost_virtqueue {
11     int kick;
12     int call;
13     void *desc;
14     void *avail;
15     void *used;
16     int num;
17     unsigned long long desc_phys;
18     unsigned desc_size;
19     unsigned long long avail_phys;
20     unsigned avail_size;
21     unsigned long long used_phys;
22     unsigned used_size;
23     EventNotifier masked_notifier;
24     struct vhost_dev *dev;
25 };
26 
27 typedef unsigned long vhost_log_chunk_t;
28 #define VHOST_LOG_PAGE 0x1000
29 #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
30 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
31 #define VHOST_INVALID_FEATURE_BIT   (0xff)
32 
33 struct vhost_log {
34     unsigned long long size;
35     int refcnt;
36     int fd;
37     vhost_log_chunk_t *log;
38 };
39 
40 struct vhost_dev;
41 struct vhost_iommu {
42     struct vhost_dev *hdev;
43     MemoryRegion *mr;
44     hwaddr iommu_offset;
45     IOMMUNotifier n;
46     QLIST_ENTRY(vhost_iommu) iommu_next;
47 };
48 
49 typedef struct VhostDevConfigOps {
50     /* Vhost device config space changed callback
51      */
52     int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
53 } VhostDevConfigOps;
54 
55 struct vhost_memory;
56 struct vhost_dev {
57     VirtIODevice *vdev;
58     MemoryListener memory_listener;
59     MemoryListener iommu_listener;
60     struct vhost_memory *mem;
61     int n_mem_sections;
62     MemoryRegionSection *mem_sections;
63     struct vhost_virtqueue *vqs;
64     int nvqs;
65     /* the first virtqueue which would be used by this vhost dev */
66     int vq_index;
67     uint64_t features;
68     uint64_t acked_features;
69     uint64_t backend_features;
70     uint64_t protocol_features;
71     uint64_t max_queues;
72     bool started;
73     bool log_enabled;
74     uint64_t log_size;
75     Error *migration_blocker;
76     bool memory_changed;
77     hwaddr mem_changed_start_addr;
78     hwaddr mem_changed_end_addr;
79     const VhostOps *vhost_ops;
80     void *opaque;
81     struct vhost_log *log;
82     QLIST_ENTRY(vhost_dev) entry;
83     QLIST_HEAD(, vhost_iommu) iommu_list;
84     IOMMUNotifier n;
85     const VhostDevConfigOps *config_ops;
86 };
87 
88 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
89                    VhostBackendType backend_type,
90                    uint32_t busyloop_timeout);
91 void vhost_dev_cleanup(struct vhost_dev *hdev);
92 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
93 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
94 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
95 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
96 
97 /* Test and clear masked event pending status.
98  * Should be called after unmask to avoid losing events.
99  */
100 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
101 
102 /* Mask/unmask events from this vq.
103  */
104 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
105                           bool mask);
106 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
107                             uint64_t features);
108 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
109                         uint64_t features);
110 bool vhost_has_free_slot(void);
111 
112 int vhost_net_set_backend(struct vhost_dev *hdev,
113                           struct vhost_vring_file *file);
114 
115 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
116 int vhost_dev_get_config(struct vhost_dev *dev, uint8_t *config,
117                          uint32_t config_len);
118 int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
119                          uint32_t offset, uint32_t size, uint32_t flags);
120 /* notifier callback in case vhost device config space changed
121  */
122 void vhost_dev_set_config_notifier(struct vhost_dev *dev,
123                                    const VhostDevConfigOps *ops);
124 #endif
125