xref: /qemu/include/hw/virtio/vhost.h (revision 375f74f473cfcb0c73a64088a6a5880829c155da)
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;
17f1f9e6c5SGreg Kurz     unsigned long long desc_phys;
18f1f9e6c5SGreg Kurz     unsigned desc_size;
19f1f9e6c5SGreg Kurz     unsigned long long avail_phys;
20f1f9e6c5SGreg Kurz     unsigned avail_size;
21d5970055SMichael S. Tsirkin     unsigned long long used_phys;
22d5970055SMichael S. Tsirkin     unsigned used_size;
23f56a1247SMichael S. Tsirkin     EventNotifier masked_notifier;
24c471ad0eSJason Wang     struct vhost_dev *dev;
25d5970055SMichael S. Tsirkin };
26d5970055SMichael S. Tsirkin 
27d5970055SMichael S. Tsirkin typedef unsigned long vhost_log_chunk_t;
28d5970055SMichael S. Tsirkin #define VHOST_LOG_PAGE 0x1000
29d5970055SMichael S. Tsirkin #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
30d5970055SMichael S. Tsirkin #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
312e6d46d7SNikolay Nikolaev #define VHOST_INVALID_FEATURE_BIT   (0xff)
32d5970055SMichael S. Tsirkin 
33309750faSJason Wang struct vhost_log {
34309750faSJason Wang     unsigned long long size;
35309750faSJason Wang     int refcnt;
3615324404SMarc-André Lureau     int fd;
3715324404SMarc-André Lureau     vhost_log_chunk_t *log;
38309750faSJason Wang };
39309750faSJason Wang 
40*375f74f4SJason Wang struct vhost_dev;
41*375f74f4SJason Wang struct vhost_iommu {
42*375f74f4SJason Wang     struct vhost_dev *hdev;
43*375f74f4SJason Wang     MemoryRegion *mr;
44*375f74f4SJason Wang     hwaddr iommu_offset;
45*375f74f4SJason Wang     IOMMUNotifier n;
46*375f74f4SJason Wang     QLIST_ENTRY(vhost_iommu) iommu_next;
47*375f74f4SJason Wang };
48*375f74f4SJason Wang 
49d5970055SMichael S. Tsirkin struct vhost_memory;
50d5970055SMichael S. Tsirkin struct vhost_dev {
51c471ad0eSJason Wang     VirtIODevice *vdev;
5204097f7cSAvi Kivity     MemoryListener memory_listener;
53*375f74f4SJason Wang     MemoryListener iommu_listener;
54d5970055SMichael S. Tsirkin     struct vhost_memory *mem;
552817b260SAvi Kivity     int n_mem_sections;
562817b260SAvi Kivity     MemoryRegionSection *mem_sections;
57d5970055SMichael S. Tsirkin     struct vhost_virtqueue *vqs;
58d5970055SMichael S. Tsirkin     int nvqs;
599be6e69fSGreg Kurz     /* the first virtqueue which would be used by this vhost dev */
60a9f98bb5SJason Wang     int vq_index;
6121e70425SMarc-André Lureau     uint64_t features;
6221e70425SMarc-André Lureau     uint64_t acked_features;
6321e70425SMarc-André Lureau     uint64_t backend_features;
6421e70425SMarc-André Lureau     uint64_t protocol_features;
6521e70425SMarc-André Lureau     uint64_t max_queues;
66d5970055SMichael S. Tsirkin     bool started;
67d5970055SMichael S. Tsirkin     bool log_enabled;
6821e70425SMarc-André Lureau     uint64_t log_size;
697145872eSMichael S. Tsirkin     Error *migration_blocker;
70af603142SNicholas Bellinger     bool memory_changed;
71af603142SNicholas Bellinger     hwaddr mem_changed_start_addr;
72af603142SNicholas Bellinger     hwaddr mem_changed_end_addr;
7324d1eb33SNikolay Nikolaev     const VhostOps *vhost_ops;
741a1bfac9SNikolay Nikolaev     void *opaque;
75309750faSJason Wang     struct vhost_log *log;
762ce68e4cSIgor Mammedov     QLIST_ENTRY(vhost_dev) entry;
77*375f74f4SJason Wang     QLIST_HEAD(, vhost_iommu) iommu_list;
78c471ad0eSJason Wang     IOMMUNotifier n;
79d5970055SMichael S. Tsirkin };
80d5970055SMichael S. Tsirkin 
8181647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
8269e87b32SJason Wang                    VhostBackendType backend_type,
8369e87b32SJason Wang                    uint32_t busyloop_timeout);
84d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev);
85d5970055SMichael S. Tsirkin int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
86d5970055SMichael S. Tsirkin void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
87b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
88b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
89d5970055SMichael S. Tsirkin 
90f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status.
91f56a1247SMichael S. Tsirkin  * Should be called after unmask to avoid losing events.
92f56a1247SMichael S. Tsirkin  */
93f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
94f56a1247SMichael S. Tsirkin 
95f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq.
96f56a1247SMichael S. Tsirkin  */
97f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
98f56a1247SMichael S. Tsirkin                           bool mask);
999a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
1009a2ba823SCornelia Huck                             uint64_t features);
1012e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
1029a2ba823SCornelia Huck                         uint64_t features);
1032ce68e4cSIgor Mammedov bool vhost_has_free_slot(void);
104950d94baSMarc-André Lureau 
105950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev,
106950d94baSMarc-André Lureau                           struct vhost_vring_file *file);
107950d94baSMarc-André Lureau 
108c471ad0eSJason Wang void vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
109d5970055SMichael S. Tsirkin #endif
110