xref: /qemu/include/hw/virtio/vhost.h (revision 950d94ba0671e7f154a9e87a277f8efbddcee28f)
1d5970055SMichael S. Tsirkin #ifndef VHOST_H
2d5970055SMichael S. Tsirkin #define VHOST_H
3d5970055SMichael S. Tsirkin 
4d5970055SMichael S. Tsirkin #include "hw/hw.h"
524d1eb33SNikolay Nikolaev #include "hw/virtio/vhost-backend.h"
60d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h"
7022c62cbSPaolo Bonzini #include "exec/memory.h"
8d5970055SMichael S. Tsirkin 
9d5970055SMichael S. Tsirkin /* Generic structures common for any vhost based device. */
10d5970055SMichael S. Tsirkin struct vhost_virtqueue {
11d5970055SMichael S. Tsirkin     int kick;
12d5970055SMichael S. Tsirkin     int call;
13d5970055SMichael S. Tsirkin     void *desc;
14d5970055SMichael S. Tsirkin     void *avail;
15d5970055SMichael S. Tsirkin     void *used;
16d5970055SMichael S. Tsirkin     int num;
17d5970055SMichael S. Tsirkin     unsigned long long used_phys;
18d5970055SMichael S. Tsirkin     unsigned used_size;
19d5970055SMichael S. Tsirkin     void *ring;
20d5970055SMichael S. Tsirkin     unsigned long long ring_phys;
21d5970055SMichael S. Tsirkin     unsigned ring_size;
22f56a1247SMichael S. Tsirkin     EventNotifier masked_notifier;
23d5970055SMichael S. Tsirkin };
24d5970055SMichael S. Tsirkin 
25d5970055SMichael S. Tsirkin typedef unsigned long vhost_log_chunk_t;
26d5970055SMichael S. Tsirkin #define VHOST_LOG_PAGE 0x1000
27d5970055SMichael S. Tsirkin #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
28d5970055SMichael S. Tsirkin #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
292e6d46d7SNikolay Nikolaev #define VHOST_INVALID_FEATURE_BIT   (0xff)
30d5970055SMichael S. Tsirkin 
31309750faSJason Wang struct vhost_log {
32309750faSJason Wang     unsigned long long size;
33309750faSJason Wang     int refcnt;
3415324404SMarc-André Lureau     int fd;
3515324404SMarc-André Lureau     vhost_log_chunk_t *log;
36309750faSJason Wang };
37309750faSJason Wang 
38d5970055SMichael S. Tsirkin struct vhost_memory;
39d5970055SMichael S. Tsirkin struct vhost_dev {
4004097f7cSAvi Kivity     MemoryListener memory_listener;
41d5970055SMichael S. Tsirkin     struct vhost_memory *mem;
422817b260SAvi Kivity     int n_mem_sections;
432817b260SAvi Kivity     MemoryRegionSection *mem_sections;
44d5970055SMichael S. Tsirkin     struct vhost_virtqueue *vqs;
45d5970055SMichael S. Tsirkin     int nvqs;
469be6e69fSGreg Kurz     /* the first virtqueue which would be used by this vhost dev */
47a9f98bb5SJason Wang     int vq_index;
4821e70425SMarc-André Lureau     uint64_t features;
4921e70425SMarc-André Lureau     uint64_t acked_features;
5021e70425SMarc-André Lureau     uint64_t backend_features;
5121e70425SMarc-André Lureau     uint64_t protocol_features;
5221e70425SMarc-André Lureau     uint64_t max_queues;
53d5970055SMichael S. Tsirkin     bool started;
54d5970055SMichael S. Tsirkin     bool log_enabled;
5521e70425SMarc-André Lureau     uint64_t log_size;
567145872eSMichael S. Tsirkin     Error *migration_blocker;
57af603142SNicholas Bellinger     bool memory_changed;
58af603142SNicholas Bellinger     hwaddr mem_changed_start_addr;
59af603142SNicholas Bellinger     hwaddr mem_changed_end_addr;
6024d1eb33SNikolay Nikolaev     const VhostOps *vhost_ops;
611a1bfac9SNikolay Nikolaev     void *opaque;
62309750faSJason Wang     struct vhost_log *log;
632ce68e4cSIgor Mammedov     QLIST_ENTRY(vhost_dev) entry;
64d5970055SMichael S. Tsirkin };
65d5970055SMichael S. Tsirkin 
6681647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
6769e87b32SJason Wang                    VhostBackendType backend_type,
6869e87b32SJason Wang                    uint32_t busyloop_timeout);
69d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev);
70d5970055SMichael S. Tsirkin int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
71d5970055SMichael S. Tsirkin void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
72b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
73b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
74d5970055SMichael S. Tsirkin 
75f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status.
76f56a1247SMichael S. Tsirkin  * Should be called after unmask to avoid losing events.
77f56a1247SMichael S. Tsirkin  */
78f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
79f56a1247SMichael S. Tsirkin 
80f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq.
81f56a1247SMichael S. Tsirkin  */
82f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
83f56a1247SMichael S. Tsirkin                           bool mask);
849a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
859a2ba823SCornelia Huck                             uint64_t features);
862e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
879a2ba823SCornelia Huck                         uint64_t features);
882ce68e4cSIgor Mammedov bool vhost_has_free_slot(void);
89*950d94baSMarc-André Lureau 
90*950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev,
91*950d94baSMarc-André Lureau                           struct vhost_vring_file *file);
92*950d94baSMarc-André Lureau 
93d5970055SMichael S. Tsirkin #endif
94