xref: /qemu/include/hw/virtio/vhost.h (revision 2ce68e4cf5be9b5176a3c3c372948d6340724d2d)
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 used_phys;
18     unsigned used_size;
19     void *ring;
20     unsigned long long ring_phys;
21     unsigned ring_size;
22     EventNotifier masked_notifier;
23 };
24 
25 typedef unsigned long vhost_log_chunk_t;
26 #define VHOST_LOG_PAGE 0x1000
27 #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
28 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
29 #define VHOST_INVALID_FEATURE_BIT   (0xff)
30 
31 struct vhost_log {
32     unsigned long long size;
33     int refcnt;
34     vhost_log_chunk_t log[0];
35 };
36 
37 struct vhost_memory;
38 struct vhost_dev {
39     MemoryListener memory_listener;
40     struct vhost_memory *mem;
41     int n_mem_sections;
42     MemoryRegionSection *mem_sections;
43     struct vhost_virtqueue *vqs;
44     int nvqs;
45     /* the first virtqueue which would be used by this vhost dev */
46     int vq_index;
47     unsigned long long features;
48     unsigned long long acked_features;
49     unsigned long long backend_features;
50     unsigned long long protocol_features;
51     unsigned long long max_queues;
52     bool started;
53     bool log_enabled;
54     unsigned long long log_size;
55     Error *migration_blocker;
56     bool memory_changed;
57     hwaddr mem_changed_start_addr;
58     hwaddr mem_changed_end_addr;
59     const VhostOps *vhost_ops;
60     void *opaque;
61     struct vhost_log *log;
62     QLIST_ENTRY(vhost_dev) entry;
63 };
64 
65 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
66                    VhostBackendType backend_type);
67 void vhost_dev_cleanup(struct vhost_dev *hdev);
68 bool vhost_dev_query(struct vhost_dev *hdev, VirtIODevice *vdev);
69 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
70 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
71 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
72 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
73 
74 /* Test and clear masked event pending status.
75  * Should be called after unmask to avoid losing events.
76  */
77 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
78 
79 /* Mask/unmask events from this vq.
80  */
81 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
82                           bool mask);
83 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
84                             uint64_t features);
85 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
86                         uint64_t features);
87 bool vhost_has_free_slot(void);
88 #endif
89