1d5970055SMichael S. Tsirkin #ifndef VHOST_H 2d5970055SMichael S. Tsirkin #define VHOST_H 3d5970055SMichael S. Tsirkin 424d1eb33SNikolay Nikolaev #include "hw/virtio/vhost-backend.h" 50d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h" 6022c62cbSPaolo Bonzini #include "exec/memory.h" 7d5970055SMichael S. Tsirkin 8d5970055SMichael S. Tsirkin /* Generic structures common for any vhost based device. */ 95ad204bfSXie Yongji 105ad204bfSXie Yongji struct vhost_inflight { 115ad204bfSXie Yongji int fd; 125ad204bfSXie Yongji void *addr; 135ad204bfSXie Yongji uint64_t size; 145ad204bfSXie Yongji uint64_t offset; 155ad204bfSXie Yongji uint16_t queue_size; 165ad204bfSXie Yongji }; 175ad204bfSXie Yongji 18d5970055SMichael S. Tsirkin struct vhost_virtqueue { 19d5970055SMichael S. Tsirkin int kick; 20d5970055SMichael S. Tsirkin int call; 21d5970055SMichael S. Tsirkin void *desc; 22d5970055SMichael S. Tsirkin void *avail; 23d5970055SMichael S. Tsirkin void *used; 24d5970055SMichael S. Tsirkin int num; 25f1f9e6c5SGreg Kurz unsigned long long desc_phys; 26f1f9e6c5SGreg Kurz unsigned desc_size; 27f1f9e6c5SGreg Kurz unsigned long long avail_phys; 28f1f9e6c5SGreg Kurz unsigned avail_size; 29d5970055SMichael S. Tsirkin unsigned long long used_phys; 30d5970055SMichael S. Tsirkin unsigned used_size; 31f56a1247SMichael S. Tsirkin EventNotifier masked_notifier; 32ae50ae0bSKonstantin Khlebnikov EventNotifier error_notifier; 33c471ad0eSJason Wang struct vhost_dev *dev; 34d5970055SMichael S. Tsirkin }; 35d5970055SMichael S. Tsirkin 36d5970055SMichael S. Tsirkin typedef unsigned long vhost_log_chunk_t; 37d5970055SMichael S. Tsirkin #define VHOST_LOG_PAGE 0x1000 38d5970055SMichael S. Tsirkin #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t)) 39d5970055SMichael S. Tsirkin #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS) 402e6d46d7SNikolay Nikolaev #define VHOST_INVALID_FEATURE_BIT (0xff) 41d5970055SMichael S. Tsirkin 42309750faSJason Wang struct vhost_log { 43309750faSJason Wang unsigned long long size; 44309750faSJason Wang int refcnt; 4515324404SMarc-André Lureau int fd; 4615324404SMarc-André Lureau vhost_log_chunk_t *log; 47309750faSJason Wang }; 48309750faSJason Wang 49375f74f4SJason Wang struct vhost_dev; 50375f74f4SJason Wang struct vhost_iommu { 51375f74f4SJason Wang struct vhost_dev *hdev; 52375f74f4SJason Wang MemoryRegion *mr; 53375f74f4SJason Wang hwaddr iommu_offset; 54375f74f4SJason Wang IOMMUNotifier n; 55375f74f4SJason Wang QLIST_ENTRY(vhost_iommu) iommu_next; 56375f74f4SJason Wang }; 57375f74f4SJason Wang 584c3e257bSChangpeng Liu typedef struct VhostDevConfigOps { 594c3e257bSChangpeng Liu /* Vhost device config space changed callback 604c3e257bSChangpeng Liu */ 614c3e257bSChangpeng Liu int (*vhost_dev_config_notifier)(struct vhost_dev *dev); 624c3e257bSChangpeng Liu } VhostDevConfigOps; 634c3e257bSChangpeng Liu 64d5970055SMichael S. Tsirkin struct vhost_memory; 6527351992SAlex Bennée 6627351992SAlex Bennée /** 6727351992SAlex Bennée * struct vhost_dev - common vhost_dev structure 6827351992SAlex Bennée * @vhost_ops: backend specific ops 6927351992SAlex Bennée * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier) 7027351992SAlex Bennée */ 71d5970055SMichael S. Tsirkin struct vhost_dev { 72c471ad0eSJason Wang VirtIODevice *vdev; 7304097f7cSAvi Kivity MemoryListener memory_listener; 74375f74f4SJason Wang MemoryListener iommu_listener; 75d5970055SMichael S. Tsirkin struct vhost_memory *mem; 762817b260SAvi Kivity int n_mem_sections; 772817b260SAvi Kivity MemoryRegionSection *mem_sections; 78c44317efSDr. David Alan Gilbert int n_tmp_sections; 79c44317efSDr. David Alan Gilbert MemoryRegionSection *tmp_sections; 80d5970055SMichael S. Tsirkin struct vhost_virtqueue *vqs; 815fc13603SJason Wang unsigned int nvqs; 829be6e69fSGreg Kurz /* the first virtqueue which would be used by this vhost dev */ 83a9f98bb5SJason Wang int vq_index; 84245cf2c2SEugenio Pérez /* one past the last vq index for the virtio device (not vhost) */ 85245cf2c2SEugenio Pérez int vq_index_end; 86c90bd505SKevin Wolf /* if non-zero, minimum required value for max_queues */ 87c90bd505SKevin Wolf int num_queues; 8821e70425SMarc-André Lureau uint64_t features; 89*c7066f2dSAlex Bennée /** @acked_features: final set of negotiated features */ 9021e70425SMarc-André Lureau uint64_t acked_features; 91*c7066f2dSAlex Bennée /** @backend_features: backend specific feature bits */ 9221e70425SMarc-André Lureau uint64_t backend_features; 93*c7066f2dSAlex Bennée /** @protocol_features: final negotiated protocol features */ 9421e70425SMarc-André Lureau uint64_t protocol_features; 9521e70425SMarc-André Lureau uint64_t max_queues; 96b37556edSJason Wang uint64_t backend_cap; 97d5970055SMichael S. Tsirkin bool started; 98d5970055SMichael S. Tsirkin bool log_enabled; 9921e70425SMarc-André Lureau uint64_t log_size; 1007145872eSMichael S. Tsirkin Error *migration_blocker; 10124d1eb33SNikolay Nikolaev const VhostOps *vhost_ops; 1021a1bfac9SNikolay Nikolaev void *opaque; 103309750faSJason Wang struct vhost_log *log; 1042ce68e4cSIgor Mammedov QLIST_ENTRY(vhost_dev) entry; 105375f74f4SJason Wang QLIST_HEAD(, vhost_iommu) iommu_list; 106c471ad0eSJason Wang IOMMUNotifier n; 1074c3e257bSChangpeng Liu const VhostDevConfigOps *config_ops; 108d5970055SMichael S. Tsirkin }; 109d5970055SMichael S. Tsirkin 1109b1d929aSTiberiu Georgescu extern const VhostOps kernel_ops; 1119b1d929aSTiberiu Georgescu extern const VhostOps user_ops; 1129b1d929aSTiberiu Georgescu extern const VhostOps vdpa_ops; 1139b1d929aSTiberiu Georgescu 114108a6481SCindy Lu struct vhost_net { 115108a6481SCindy Lu struct vhost_dev dev; 116108a6481SCindy Lu struct vhost_virtqueue vqs[2]; 117108a6481SCindy Lu int backend; 118108a6481SCindy Lu NetClientState *nc; 119108a6481SCindy Lu }; 120108a6481SCindy Lu 12127351992SAlex Bennée /** 12227351992SAlex Bennée * vhost_dev_init() - initialise the vhost interface 12327351992SAlex Bennée * @hdev: the common vhost_dev structure 12427351992SAlex Bennée * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa) 12527351992SAlex Bennée * @backend_type: type of backend 12627351992SAlex Bennée * @busyloop_timeout: timeout for polling virtqueue 12727351992SAlex Bennée * @errp: error handle 12827351992SAlex Bennée * 12927351992SAlex Bennée * The initialisation of the vhost device will trigger the 13027351992SAlex Bennée * initialisation of the backend and potentially capability 13127351992SAlex Bennée * negotiation of backend interface. Configuration of the VirtIO 13227351992SAlex Bennée * itself won't happen until the interface is started. 13327351992SAlex Bennée * 13427351992SAlex Bennée * Return: 0 on success, non-zero on error while setting errp. 13527351992SAlex Bennée */ 13681647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque, 13769e87b32SJason Wang VhostBackendType backend_type, 138a6945f22SKevin Wolf uint32_t busyloop_timeout, Error **errp); 13927351992SAlex Bennée 14027351992SAlex Bennée /** 14127351992SAlex Bennée * vhost_dev_cleanup() - tear down and cleanup vhost interface 14227351992SAlex Bennée * @hdev: the common vhost_dev structure 14327351992SAlex Bennée */ 144d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev); 14527351992SAlex Bennée 14627351992SAlex Bennée /** 14727351992SAlex Bennée * vhost_dev_enable_notifiers() - enable event notifiers 14827351992SAlex Bennée * @hdev: common vhost_dev structure 14927351992SAlex Bennée * @vdev: the VirtIODevice structure 15027351992SAlex Bennée * 15127351992SAlex Bennée * Enable notifications directly to the vhost device rather than being 15227351992SAlex Bennée * triggered by QEMU itself. Notifications should be enabled before 15327351992SAlex Bennée * the vhost device is started via @vhost_dev_start. 15427351992SAlex Bennée * 15527351992SAlex Bennée * Return: 0 on success, < 0 on error. 15627351992SAlex Bennée */ 157b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); 15827351992SAlex Bennée 15927351992SAlex Bennée /** 16027351992SAlex Bennée * vhost_dev_disable_notifiers - disable event notifications 16127351992SAlex Bennée * @hdev: common vhost_dev structure 16227351992SAlex Bennée * @vdev: the VirtIODevice structure 16327351992SAlex Bennée * 16427351992SAlex Bennée * Disable direct notifications to vhost device. 16527351992SAlex Bennée */ 166b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); 167d5970055SMichael S. Tsirkin 16827351992SAlex Bennée /** 16927351992SAlex Bennée * vhost_dev_start() - start the vhost device 17027351992SAlex Bennée * @hdev: common vhost_dev structure 17127351992SAlex Bennée * @vdev: the VirtIODevice structure 17227351992SAlex Bennée * 17327351992SAlex Bennée * Starts the vhost device. From this point VirtIO feature negotiation 17427351992SAlex Bennée * can start and the device can start processing VirtIO transactions. 17527351992SAlex Bennée * 17627351992SAlex Bennée * Return: 0 on success, < 0 on error. 17727351992SAlex Bennée */ 17827351992SAlex Bennée int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev); 17927351992SAlex Bennée 18027351992SAlex Bennée /** 18127351992SAlex Bennée * vhost_dev_stop() - stop the vhost device 18227351992SAlex Bennée * @hdev: common vhost_dev structure 18327351992SAlex Bennée * @vdev: the VirtIODevice structure 18427351992SAlex Bennée * 18527351992SAlex Bennée * Stop the vhost device. After the device is stopped the notifiers 18627351992SAlex Bennée * can be disabled (@vhost_dev_disable_notifiers) and the device can 18727351992SAlex Bennée * be torn down (@vhost_dev_cleanup). 18827351992SAlex Bennée */ 18927351992SAlex Bennée void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev); 19027351992SAlex Bennée 19127351992SAlex Bennée /** 19227351992SAlex Bennée * DOC: vhost device configuration handling 19327351992SAlex Bennée * 19427351992SAlex Bennée * The VirtIO device configuration space is used for rarely changing 19527351992SAlex Bennée * or initialisation time parameters. The configuration can be updated 19627351992SAlex Bennée * by either the guest driver or the device itself. If the device can 19727351992SAlex Bennée * change the configuration over time the vhost handler should 19827351992SAlex Bennée * register a @VhostDevConfigOps structure with 19927351992SAlex Bennée * @vhost_dev_set_config_notifier so the guest can be notified. Some 20027351992SAlex Bennée * devices register a handler anyway and will signal an error if an 20127351992SAlex Bennée * unexpected config change happens. 20227351992SAlex Bennée */ 20327351992SAlex Bennée 20427351992SAlex Bennée /** 20527351992SAlex Bennée * vhost_dev_get_config() - fetch device configuration 20627351992SAlex Bennée * @hdev: common vhost_dev_structure 20727351992SAlex Bennée * @config: pointer to device appropriate config structure 20827351992SAlex Bennée * @config_len: size of device appropriate config structure 20927351992SAlex Bennée * 21027351992SAlex Bennée * Return: 0 on success, < 0 on error while setting errp 21127351992SAlex Bennée */ 21227351992SAlex Bennée int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, 21327351992SAlex Bennée uint32_t config_len, Error **errp); 21427351992SAlex Bennée 21527351992SAlex Bennée /** 21627351992SAlex Bennée * vhost_dev_set_config() - set device configuration 21727351992SAlex Bennée * @hdev: common vhost_dev_structure 21827351992SAlex Bennée * @data: pointer to data to set 21927351992SAlex Bennée * @offset: offset into configuration space 22027351992SAlex Bennée * @size: length of set 22127351992SAlex Bennée * @flags: @VhostSetConfigType flags 22227351992SAlex Bennée * 22327351992SAlex Bennée * By use of @offset/@size a subset of the configuration space can be 22427351992SAlex Bennée * written to. The @flags are used to indicate if it is a normal 22527351992SAlex Bennée * transaction or related to migration. 22627351992SAlex Bennée * 22727351992SAlex Bennée * Return: 0 on success, non-zero on error 22827351992SAlex Bennée */ 22927351992SAlex Bennée int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data, 23027351992SAlex Bennée uint32_t offset, uint32_t size, uint32_t flags); 23127351992SAlex Bennée 23227351992SAlex Bennée /** 23327351992SAlex Bennée * vhost_dev_set_config_notifier() - register VhostDevConfigOps 23427351992SAlex Bennée * @hdev: common vhost_dev_structure 23527351992SAlex Bennée * @ops: notifier ops 23627351992SAlex Bennée * 23727351992SAlex Bennée * If the device is expected to change configuration a notifier can be 23827351992SAlex Bennée * setup to handle the case. 23927351992SAlex Bennée */ 24027351992SAlex Bennée void vhost_dev_set_config_notifier(struct vhost_dev *dev, 24127351992SAlex Bennée const VhostDevConfigOps *ops); 24227351992SAlex Bennée 24327351992SAlex Bennée 244f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status. 245f56a1247SMichael S. Tsirkin * Should be called after unmask to avoid losing events. 246f56a1247SMichael S. Tsirkin */ 247f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n); 248f56a1247SMichael S. Tsirkin 249f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq. 250f56a1247SMichael S. Tsirkin */ 251f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, 252f56a1247SMichael S. Tsirkin bool mask); 2532055c2a4SAlex Bennée 2542055c2a4SAlex Bennée /** 2552055c2a4SAlex Bennée * vhost_get_features() - return a sanitised set of feature bits 2562055c2a4SAlex Bennée * @hdev: common vhost_dev structure 2572055c2a4SAlex Bennée * @feature_bits: pointer to terminated table of feature bits 2582055c2a4SAlex Bennée * @features: original feature set 2592055c2a4SAlex Bennée * 2602055c2a4SAlex Bennée * This returns a set of features bits that is an intersection of what 2612055c2a4SAlex Bennée * is supported by the vhost backend (hdev->features), the supported 2622055c2a4SAlex Bennée * feature_bits and the requested feature set. 2632055c2a4SAlex Bennée */ 2649a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, 2659a2ba823SCornelia Huck uint64_t features); 26681cf38f3SAlex Bennée 26781cf38f3SAlex Bennée /** 26881cf38f3SAlex Bennée * vhost_ack_features() - set vhost acked_features 26981cf38f3SAlex Bennée * @hdev: common vhost_dev structure 27081cf38f3SAlex Bennée * @feature_bits: pointer to terminated table of feature bits 27181cf38f3SAlex Bennée * @features: requested feature set 27281cf38f3SAlex Bennée * 27381cf38f3SAlex Bennée * This sets the internal hdev->acked_features to the intersection of 27481cf38f3SAlex Bennée * the backends advertised features and the supported feature_bits. 27581cf38f3SAlex Bennée */ 2762e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, 2779a2ba823SCornelia Huck uint64_t features); 2782ce68e4cSIgor Mammedov bool vhost_has_free_slot(void); 279950d94baSMarc-André Lureau 280950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev, 281950d94baSMarc-André Lureau struct vhost_vring_file *file); 282950d94baSMarc-André Lureau 283fc58bd0dSMaxime Coquelin int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write); 2845ad204bfSXie Yongji 2855ad204bfSXie Yongji void vhost_dev_reset_inflight(struct vhost_inflight *inflight); 2865ad204bfSXie Yongji void vhost_dev_free_inflight(struct vhost_inflight *inflight); 2875ad204bfSXie Yongji void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f); 2885ad204bfSXie Yongji int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f); 2891b0063b3SJin Yu int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev); 2905ad204bfSXie Yongji int vhost_dev_set_inflight(struct vhost_dev *dev, 2915ad204bfSXie Yongji struct vhost_inflight *inflight); 2925ad204bfSXie Yongji int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, 2935ad204bfSXie Yongji struct vhost_inflight *inflight); 294d5970055SMichael S. Tsirkin #endif 295