1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 23a4d5c94SMichael S. Tsirkin #ifndef _VHOST_H 33a4d5c94SMichael S. Tsirkin #define _VHOST_H 43a4d5c94SMichael S. Tsirkin 53a4d5c94SMichael S. Tsirkin #include <linux/eventfd.h> 63a4d5c94SMichael S. Tsirkin #include <linux/vhost.h> 73a4d5c94SMichael S. Tsirkin #include <linux/mm.h> 83a4d5c94SMichael S. Tsirkin #include <linux/mutex.h> 93a4d5c94SMichael S. Tsirkin #include <linux/poll.h> 103a4d5c94SMichael S. Tsirkin #include <linux/file.h> 113a4d5c94SMichael S. Tsirkin #include <linux/uio.h> 123a4d5c94SMichael S. Tsirkin #include <linux/virtio_config.h> 133a4d5c94SMichael S. Tsirkin #include <linux/virtio_ring.h> 1460063497SArun Sharma #include <linux/atomic.h> 150bbe3066SJason Wang #include <linux/vhost_iotlb.h> 16265a0ad8SZhu Lingshan #include <linux/irqbypass.h> 173a4d5c94SMichael S. Tsirkin 18c23f3445STejun Heo struct vhost_work; 19c23f3445STejun Heo typedef void (*vhost_work_fn_t)(struct vhost_work *work); 20c23f3445STejun Heo 2104b96e55SJason Wang #define VHOST_WORK_QUEUED 1 22c23f3445STejun Heo struct vhost_work { 2304b96e55SJason Wang struct llist_node node; 24c23f3445STejun Heo vhost_work_fn_t fn; 2504b96e55SJason Wang unsigned long flags; 26c23f3445STejun Heo }; 27c23f3445STejun Heo 283a4d5c94SMichael S. Tsirkin /* Poll a file (eventfd or socket) */ 293a4d5c94SMichael S. Tsirkin /* Note: there's nothing vhost specific about this structure. */ 303a4d5c94SMichael S. Tsirkin struct vhost_poll { 313a4d5c94SMichael S. Tsirkin poll_table table; 323a4d5c94SMichael S. Tsirkin wait_queue_head_t *wqh; 33ac6424b9SIngo Molnar wait_queue_entry_t wait; 34c23f3445STejun Heo struct vhost_work work; 3558e3b602SAl Viro __poll_t mask; 36c23f3445STejun Heo struct vhost_dev *dev; 373a4d5c94SMichael S. Tsirkin }; 383a4d5c94SMichael S. Tsirkin 39163049aeSStefan Hajnoczi void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn); 40163049aeSStefan Hajnoczi void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work); 41526d3e7fSJason Wang bool vhost_has_work(struct vhost_dev *dev); 42163049aeSStefan Hajnoczi 43c23f3445STejun Heo void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, 4458e3b602SAl Viro __poll_t mask, struct vhost_dev *dev); 452b8b328bSJason Wang int vhost_poll_start(struct vhost_poll *poll, struct file *file); 463a4d5c94SMichael S. Tsirkin void vhost_poll_stop(struct vhost_poll *poll); 473a4d5c94SMichael S. Tsirkin void vhost_poll_queue(struct vhost_poll *poll); 48*b2ffa407SMike Christie void vhost_dev_flush(struct vhost_dev *dev); 493a4d5c94SMichael S. Tsirkin 503a4d5c94SMichael S. Tsirkin struct vhost_log { 513a4d5c94SMichael S. Tsirkin u64 addr; 523a4d5c94SMichael S. Tsirkin u64 len; 533a4d5c94SMichael S. Tsirkin }; 543a4d5c94SMichael S. Tsirkin 55f8894913SJason Wang enum vhost_uaddr_type { 56f8894913SJason Wang VHOST_ADDR_DESC = 0, 57f8894913SJason Wang VHOST_ADDR_AVAIL = 1, 58f8894913SJason Wang VHOST_ADDR_USED = 2, 59f8894913SJason Wang VHOST_NUM_ADDRS = 3, 60f8894913SJason Wang }; 61f8894913SJason Wang 62265a0ad8SZhu Lingshan struct vhost_vring_call { 63265a0ad8SZhu Lingshan struct eventfd_ctx *ctx; 64265a0ad8SZhu Lingshan struct irq_bypass_producer producer; 65265a0ad8SZhu Lingshan }; 66265a0ad8SZhu Lingshan 673a4d5c94SMichael S. Tsirkin /* The virtqueue structure describes a queue attached to a device. */ 683a4d5c94SMichael S. Tsirkin struct vhost_virtqueue { 693a4d5c94SMichael S. Tsirkin struct vhost_dev *dev; 703a4d5c94SMichael S. Tsirkin 713a4d5c94SMichael S. Tsirkin /* The actual ring of buffers. */ 723a4d5c94SMichael S. Tsirkin struct mutex mutex; 733a4d5c94SMichael S. Tsirkin unsigned int num; 74a865e420SMichael S. Tsirkin vring_desc_t __user *desc; 75a865e420SMichael S. Tsirkin vring_avail_t __user *avail; 76a865e420SMichael S. Tsirkin vring_used_t __user *used; 770bbe3066SJason Wang const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS]; 783a4d5c94SMichael S. Tsirkin struct file *kick; 79265a0ad8SZhu Lingshan struct vhost_vring_call call_ctx; 803a4d5c94SMichael S. Tsirkin struct eventfd_ctx *error_ctx; 813a4d5c94SMichael S. Tsirkin struct eventfd_ctx *log_ctx; 823a4d5c94SMichael S. Tsirkin 833a4d5c94SMichael S. Tsirkin struct vhost_poll poll; 843a4d5c94SMichael S. Tsirkin 853a4d5c94SMichael S. Tsirkin /* The routine to call when the Guest pings us, or timeout. */ 86c23f3445STejun Heo vhost_work_fn_t handle_kick; 873a4d5c94SMichael S. Tsirkin 883a4d5c94SMichael S. Tsirkin /* Last available index we saw. */ 893a4d5c94SMichael S. Tsirkin u16 last_avail_idx; 903a4d5c94SMichael S. Tsirkin 913a4d5c94SMichael S. Tsirkin /* Caches available index value from user. */ 923a4d5c94SMichael S. Tsirkin u16 avail_idx; 933a4d5c94SMichael S. Tsirkin 943a4d5c94SMichael S. Tsirkin /* Last index we used. */ 953a4d5c94SMichael S. Tsirkin u16 last_used_idx; 963a4d5c94SMichael S. Tsirkin 973a4d5c94SMichael S. Tsirkin /* Used flags */ 983a4d5c94SMichael S. Tsirkin u16 used_flags; 993a4d5c94SMichael S. Tsirkin 1008ea8cf89SMichael S. Tsirkin /* Last used index value we have signalled on */ 1018ea8cf89SMichael S. Tsirkin u16 signalled_used; 1028ea8cf89SMichael S. Tsirkin 1038ea8cf89SMichael S. Tsirkin /* Last used index value we have signalled on */ 1048ea8cf89SMichael S. Tsirkin bool signalled_used_valid; 1058ea8cf89SMichael S. Tsirkin 1063a4d5c94SMichael S. Tsirkin /* Log writes to used structure. */ 1073a4d5c94SMichael S. Tsirkin bool log_used; 1083a4d5c94SMichael S. Tsirkin u64 log_addr; 1093a4d5c94SMichael S. Tsirkin 110e0e9b406SJason Wang struct iovec iov[UIO_MAXIOV]; 1116b1e6cc7SJason Wang struct iovec iotlb_iov[64]; 112e0e9b406SJason Wang struct iovec *indirect; 113e0e9b406SJason Wang struct vring_used_elem *heads; 11422fa90c7SAsias He /* Protected by virtqueue mutex. */ 1150bbe3066SJason Wang struct vhost_iotlb *umem; 1160bbe3066SJason Wang struct vhost_iotlb *iotlb; 11722fa90c7SAsias He void *private_data; 118bd82752aSMichael S. Tsirkin u64 acked_features; 119429711aeSJason Wang u64 acked_backend_features; 1203a4d5c94SMichael S. Tsirkin /* Log write descriptors */ 1213a4d5c94SMichael S. Tsirkin void __user *log_base; 122e0e9b406SJason Wang struct vhost_log *log; 1235e5e8736SLi Wang struct iovec log_iov[64]; 1242751c988SGreg Kurz 1252751c988SGreg Kurz /* Ring endianness. Defaults to legacy native endianness. 1262751c988SGreg Kurz * Set to true when starting a modern virtio device. */ 1272751c988SGreg Kurz bool is_le; 1282751c988SGreg Kurz #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 1292751c988SGreg Kurz /* Ring endianness requested by userspace for cross-endian support. */ 1302751c988SGreg Kurz bool user_be; 1312751c988SGreg Kurz #endif 13203088137SJason Wang u32 busyloop_timeout; 1333a4d5c94SMichael S. Tsirkin }; 1343a4d5c94SMichael S. Tsirkin 1356b1e6cc7SJason Wang struct vhost_msg_node { 136429711aeSJason Wang union { 1376b1e6cc7SJason Wang struct vhost_msg msg; 138429711aeSJason Wang struct vhost_msg_v2 msg_v2; 139429711aeSJason Wang }; 1406b1e6cc7SJason Wang struct vhost_virtqueue *vq; 1416b1e6cc7SJason Wang struct list_head node; 1426b1e6cc7SJason Wang }; 1436b1e6cc7SJason Wang 1443a4d5c94SMichael S. Tsirkin struct vhost_dev { 1453a4d5c94SMichael S. Tsirkin struct mm_struct *mm; 1463a4d5c94SMichael S. Tsirkin struct mutex mutex; 1473ab2e420SAsias He struct vhost_virtqueue **vqs; 1483a4d5c94SMichael S. Tsirkin int nvqs; 1493a4d5c94SMichael S. Tsirkin struct eventfd_ctx *log_ctx; 15004b96e55SJason Wang struct llist_head work_list; 151c23f3445STejun Heo struct task_struct *worker; 1520bbe3066SJason Wang struct vhost_iotlb *umem; 1530bbe3066SJason Wang struct vhost_iotlb *iotlb; 1546b1e6cc7SJason Wang spinlock_t iotlb_lock; 1556b1e6cc7SJason Wang struct list_head read_list; 1566b1e6cc7SJason Wang struct list_head pending_list; 1576b1e6cc7SJason Wang wait_queue_head_t wait; 158b46a0bf7SJason Wang int iov_limit; 159e82b9b07SJason Wang int weight; 160e82b9b07SJason Wang int byte_weight; 1618f6a7f96SAndrey Konovalov u64 kcov_handle; 16201fcb1cbSJason Wang bool use_worker; 16391233ad7SGautam Dawar int (*msg_handler)(struct vhost_dev *dev, u32 asid, 164792a4f2eSJason Wang struct vhost_iotlb_msg *msg); 1653a4d5c94SMichael S. Tsirkin }; 1663a4d5c94SMichael S. Tsirkin 167e82b9b07SJason Wang bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len); 168b46a0bf7SJason Wang void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, 169792a4f2eSJason Wang int nvqs, int iov_limit, int weight, int byte_weight, 17001fcb1cbSJason Wang bool use_worker, 17191233ad7SGautam Dawar int (*msg_handler)(struct vhost_dev *dev, u32 asid, 172792a4f2eSJason Wang struct vhost_iotlb_msg *msg)); 17354db63c2SAsias He long vhost_dev_set_owner(struct vhost_dev *dev); 17405c05351SMichael S. Tsirkin bool vhost_dev_has_owner(struct vhost_dev *dev); 1753a4d5c94SMichael S. Tsirkin long vhost_dev_check_owner(struct vhost_dev *); 1760bbe3066SJason Wang struct vhost_iotlb *vhost_dev_reset_owner_prepare(void); 1770bbe3066SJason Wang void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb); 178f6f93f75S夷则(Caspar) void vhost_dev_cleanup(struct vhost_dev *); 179b211616dSMichael S. Tsirkin void vhost_dev_stop(struct vhost_dev *); 180935cdee7SMichael S. Tsirkin long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp); 18126b36604SSonny Rao long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); 182ddd3d408SStefan Hajnoczi bool vhost_vq_access_ok(struct vhost_virtqueue *vq); 183ddd3d408SStefan Hajnoczi bool vhost_log_access_ok(struct vhost_dev *); 1843a4d5c94SMichael S. Tsirkin 18547283befSMichael S. Tsirkin int vhost_get_vq_desc(struct vhost_virtqueue *, 1863a4d5c94SMichael S. Tsirkin struct iovec iov[], unsigned int iov_count, 1873a4d5c94SMichael S. Tsirkin unsigned int *out_num, unsigned int *in_num, 1883a4d5c94SMichael S. Tsirkin struct vhost_log *log, unsigned int *log_num); 1898dd014adSDavid Stevens void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); 1903a4d5c94SMichael S. Tsirkin 1916bcf3422SMike Christie bool vhost_vq_is_setup(struct vhost_virtqueue *vq); 19280f7d030SGreg Kurz int vhost_vq_init_access(struct vhost_virtqueue *); 1933a4d5c94SMichael S. Tsirkin int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); 1948dd014adSDavid Stevens int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, 1958dd014adSDavid Stevens unsigned count); 1963a4d5c94SMichael S. Tsirkin void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *, 1978dd014adSDavid Stevens unsigned int id, int len); 1988dd014adSDavid Stevens void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *, 1998dd014adSDavid Stevens struct vring_used_elem *heads, unsigned count); 2008dd014adSDavid Stevens void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *); 2018ea8cf89SMichael S. Tsirkin void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *); 202d4a60603SJason Wang bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *); 2038ea8cf89SMichael S. Tsirkin bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *); 2043a4d5c94SMichael S. Tsirkin 2053a4d5c94SMichael S. Tsirkin int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, 206cc5e7107SJason Wang unsigned int log_num, u64 len, 207cc5e7107SJason Wang struct iovec *iov, int count); 2089b5e830bSJason Wang int vq_meta_prefetch(struct vhost_virtqueue *vq); 2096b1e6cc7SJason Wang 2106b1e6cc7SJason Wang struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type); 2116b1e6cc7SJason Wang void vhost_enqueue_msg(struct vhost_dev *dev, 2126b1e6cc7SJason Wang struct list_head *head, 2136b1e6cc7SJason Wang struct vhost_msg_node *node); 2146b1e6cc7SJason Wang struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, 2156b1e6cc7SJason Wang struct list_head *head); 216460f7ce1SJason Wang void vhost_set_backend_features(struct vhost_dev *dev, u64 features); 217460f7ce1SJason Wang 218afc9a42bSAl Viro __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, 2196b1e6cc7SJason Wang poll_table *wait); 2206b1e6cc7SJason Wang ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, 2216b1e6cc7SJason Wang int noblock); 2226b1e6cc7SJason Wang ssize_t vhost_chr_write_iter(struct vhost_dev *dev, 2236b1e6cc7SJason Wang struct iov_iter *from); 2246b1e6cc7SJason Wang int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled); 2253a4d5c94SMichael S. Tsirkin 2260bbe3066SJason Wang void vhost_iotlb_map_free(struct vhost_iotlb *iotlb, 2270bbe3066SJason Wang struct vhost_iotlb_map *map); 2280bbe3066SJason Wang 2293a4d5c94SMichael S. Tsirkin #define vq_err(vq, fmt, ...) do { \ 2303a4d5c94SMichael S. Tsirkin pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ 2313a4d5c94SMichael S. Tsirkin if ((vq)->error_ctx) \ 2323a4d5c94SMichael S. Tsirkin eventfd_signal((vq)->error_ctx, 1);\ 2333a4d5c94SMichael S. Tsirkin } while (0) 2343a4d5c94SMichael S. Tsirkin 2353a4d5c94SMichael S. Tsirkin enum { 2368ea8cf89SMichael S. Tsirkin VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | 2378ea8cf89SMichael S. Tsirkin (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | 2388ea8cf89SMichael S. Tsirkin (1ULL << VIRTIO_RING_F_EVENT_IDX) | 2394e9fa50cSMichael S. Tsirkin (1ULL << VHOST_F_LOG_ALL) | 2404e9fa50cSMichael S. Tsirkin (1ULL << VIRTIO_F_ANY_LAYOUT) | 2414e9fa50cSMichael S. Tsirkin (1ULL << VIRTIO_F_VERSION_1) 2423a4d5c94SMichael S. Tsirkin }; 2433a4d5c94SMichael S. Tsirkin 244247643f8SEugenio Pérez /** 245247643f8SEugenio Pérez * vhost_vq_set_backend - Set backend. 246247643f8SEugenio Pérez * 247247643f8SEugenio Pérez * @vq Virtqueue. 248247643f8SEugenio Pérez * @private_data The private data. 249247643f8SEugenio Pérez * 250247643f8SEugenio Pérez * Context: Need to call with vq->mutex acquired. 251247643f8SEugenio Pérez */ 252247643f8SEugenio Pérez static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq, 253247643f8SEugenio Pérez void *private_data) 254247643f8SEugenio Pérez { 255247643f8SEugenio Pérez vq->private_data = private_data; 256247643f8SEugenio Pérez } 257247643f8SEugenio Pérez 258247643f8SEugenio Pérez /** 259247643f8SEugenio Pérez * vhost_vq_get_backend - Get backend. 260247643f8SEugenio Pérez * 261247643f8SEugenio Pérez * @vq Virtqueue. 262247643f8SEugenio Pérez * 263247643f8SEugenio Pérez * Context: Need to call with vq->mutex acquired. 264247643f8SEugenio Pérez * Return: Private data previously set with vhost_vq_set_backend. 265247643f8SEugenio Pérez */ 266247643f8SEugenio Pérez static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq) 267247643f8SEugenio Pérez { 268247643f8SEugenio Pérez return vq->private_data; 269247643f8SEugenio Pérez } 270247643f8SEugenio Pérez 271bd82752aSMichael S. Tsirkin static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit) 2723a4d5c94SMichael S. Tsirkin { 273bd82752aSMichael S. Tsirkin return vq->acked_features & (1ULL << bit); 2743a4d5c94SMichael S. Tsirkin } 275e05fd12bSMichael S. Tsirkin 276429711aeSJason Wang static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit) 277429711aeSJason Wang { 278429711aeSJason Wang return vq->acked_backend_features & (1ULL << bit); 279429711aeSJason Wang } 280429711aeSJason Wang 281e407f39aSMichael S. Tsirkin #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 282ab27c07fSGreg Kurz static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) 283ab27c07fSGreg Kurz { 2842751c988SGreg Kurz return vq->is_le; 285ab27c07fSGreg Kurz } 286e407f39aSMichael S. Tsirkin #else 287e407f39aSMichael S. Tsirkin static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) 288e407f39aSMichael S. Tsirkin { 289e407f39aSMichael S. Tsirkin return virtio_legacy_is_little_endian() || vq->is_le; 290e407f39aSMichael S. Tsirkin } 291e407f39aSMichael S. Tsirkin #endif 292ab27c07fSGreg Kurz 293e05fd12bSMichael S. Tsirkin /* Memory accessors */ 294e05fd12bSMichael S. Tsirkin static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val) 295e05fd12bSMichael S. Tsirkin { 296ab27c07fSGreg Kurz return __virtio16_to_cpu(vhost_is_little_endian(vq), val); 297e05fd12bSMichael S. Tsirkin } 298e05fd12bSMichael S. Tsirkin 299e05fd12bSMichael S. Tsirkin static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val) 300e05fd12bSMichael S. Tsirkin { 301ab27c07fSGreg Kurz return __cpu_to_virtio16(vhost_is_little_endian(vq), val); 302e05fd12bSMichael S. Tsirkin } 303e05fd12bSMichael S. Tsirkin 304e05fd12bSMichael S. Tsirkin static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val) 305e05fd12bSMichael S. Tsirkin { 306ab27c07fSGreg Kurz return __virtio32_to_cpu(vhost_is_little_endian(vq), val); 307e05fd12bSMichael S. Tsirkin } 308e05fd12bSMichael S. Tsirkin 309e05fd12bSMichael S. Tsirkin static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val) 310e05fd12bSMichael S. Tsirkin { 311ab27c07fSGreg Kurz return __cpu_to_virtio32(vhost_is_little_endian(vq), val); 312e05fd12bSMichael S. Tsirkin } 313e05fd12bSMichael S. Tsirkin 314e05fd12bSMichael S. Tsirkin static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val) 315e05fd12bSMichael S. Tsirkin { 316ab27c07fSGreg Kurz return __virtio64_to_cpu(vhost_is_little_endian(vq), val); 317e05fd12bSMichael S. Tsirkin } 318e05fd12bSMichael S. Tsirkin 319e05fd12bSMichael S. Tsirkin static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val) 320e05fd12bSMichael S. Tsirkin { 321ab27c07fSGreg Kurz return __cpu_to_virtio64(vhost_is_little_endian(vq), val); 322e05fd12bSMichael S. Tsirkin } 3233a4d5c94SMichael S. Tsirkin #endif 324