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_flush(struct vhost_poll *poll); 483a4d5c94SMichael S. Tsirkin void vhost_poll_queue(struct vhost_poll *poll); 496ac1afbfSAsias He void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work); 5026b36604SSonny Rao long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); 513a4d5c94SMichael S. Tsirkin 523a4d5c94SMichael S. Tsirkin struct vhost_log { 533a4d5c94SMichael S. Tsirkin u64 addr; 543a4d5c94SMichael S. Tsirkin u64 len; 553a4d5c94SMichael S. Tsirkin }; 563a4d5c94SMichael S. Tsirkin 57f8894913SJason Wang enum vhost_uaddr_type { 58f8894913SJason Wang VHOST_ADDR_DESC = 0, 59f8894913SJason Wang VHOST_ADDR_AVAIL = 1, 60f8894913SJason Wang VHOST_ADDR_USED = 2, 61f8894913SJason Wang VHOST_NUM_ADDRS = 3, 62f8894913SJason Wang }; 63f8894913SJason Wang 64265a0ad8SZhu Lingshan struct vhost_vring_call { 65265a0ad8SZhu Lingshan struct eventfd_ctx *ctx; 66265a0ad8SZhu Lingshan struct irq_bypass_producer producer; 67265a0ad8SZhu Lingshan }; 68265a0ad8SZhu Lingshan 693a4d5c94SMichael S. Tsirkin /* The virtqueue structure describes a queue attached to a device. */ 703a4d5c94SMichael S. Tsirkin struct vhost_virtqueue { 713a4d5c94SMichael S. Tsirkin struct vhost_dev *dev; 723a4d5c94SMichael S. Tsirkin 733a4d5c94SMichael S. Tsirkin /* The actual ring of buffers. */ 743a4d5c94SMichael S. Tsirkin struct mutex mutex; 753a4d5c94SMichael S. Tsirkin unsigned int num; 76a865e420SMichael S. Tsirkin vring_desc_t __user *desc; 77a865e420SMichael S. Tsirkin vring_avail_t __user *avail; 78a865e420SMichael S. Tsirkin vring_used_t __user *used; 790bbe3066SJason Wang const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS]; 803a4d5c94SMichael S. Tsirkin struct file *kick; 81265a0ad8SZhu Lingshan struct vhost_vring_call call_ctx; 823a4d5c94SMichael S. Tsirkin struct eventfd_ctx *error_ctx; 833a4d5c94SMichael S. Tsirkin struct eventfd_ctx *log_ctx; 843a4d5c94SMichael S. Tsirkin 853a4d5c94SMichael S. Tsirkin struct vhost_poll poll; 863a4d5c94SMichael S. Tsirkin 873a4d5c94SMichael S. Tsirkin /* The routine to call when the Guest pings us, or timeout. */ 88c23f3445STejun Heo vhost_work_fn_t handle_kick; 893a4d5c94SMichael S. Tsirkin 903a4d5c94SMichael S. Tsirkin /* Last available index we saw. */ 913a4d5c94SMichael S. Tsirkin u16 last_avail_idx; 923a4d5c94SMichael S. Tsirkin 933a4d5c94SMichael S. Tsirkin /* Caches available index value from user. */ 943a4d5c94SMichael S. Tsirkin u16 avail_idx; 953a4d5c94SMichael S. Tsirkin 963a4d5c94SMichael S. Tsirkin /* Last index we used. */ 973a4d5c94SMichael S. Tsirkin u16 last_used_idx; 983a4d5c94SMichael S. Tsirkin 993a4d5c94SMichael S. Tsirkin /* Used flags */ 1003a4d5c94SMichael S. Tsirkin u16 used_flags; 1013a4d5c94SMichael S. Tsirkin 1028ea8cf89SMichael S. Tsirkin /* Last used index value we have signalled on */ 1038ea8cf89SMichael S. Tsirkin u16 signalled_used; 1048ea8cf89SMichael S. Tsirkin 1058ea8cf89SMichael S. Tsirkin /* Last used index value we have signalled on */ 1068ea8cf89SMichael S. Tsirkin bool signalled_used_valid; 1078ea8cf89SMichael S. Tsirkin 1083a4d5c94SMichael S. Tsirkin /* Log writes to used structure. */ 1093a4d5c94SMichael S. Tsirkin bool log_used; 1103a4d5c94SMichael S. Tsirkin u64 log_addr; 1113a4d5c94SMichael S. Tsirkin 112e0e9b406SJason Wang struct iovec iov[UIO_MAXIOV]; 1136b1e6cc7SJason Wang struct iovec iotlb_iov[64]; 114e0e9b406SJason Wang struct iovec *indirect; 115e0e9b406SJason Wang struct vring_used_elem *heads; 11622fa90c7SAsias He /* Protected by virtqueue mutex. */ 1170bbe3066SJason Wang struct vhost_iotlb *umem; 1180bbe3066SJason Wang struct vhost_iotlb *iotlb; 11922fa90c7SAsias He void *private_data; 120bd82752aSMichael S. Tsirkin u64 acked_features; 121429711aeSJason Wang u64 acked_backend_features; 1223a4d5c94SMichael S. Tsirkin /* Log write descriptors */ 1233a4d5c94SMichael S. Tsirkin void __user *log_base; 124e0e9b406SJason Wang struct vhost_log *log; 1255e5e8736SLi Wang struct iovec log_iov[64]; 1262751c988SGreg Kurz 1272751c988SGreg Kurz /* Ring endianness. Defaults to legacy native endianness. 1282751c988SGreg Kurz * Set to true when starting a modern virtio device. */ 1292751c988SGreg Kurz bool is_le; 1302751c988SGreg Kurz #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 1312751c988SGreg Kurz /* Ring endianness requested by userspace for cross-endian support. */ 1322751c988SGreg Kurz bool user_be; 1332751c988SGreg Kurz #endif 13403088137SJason Wang u32 busyloop_timeout; 1353a4d5c94SMichael S. Tsirkin }; 1363a4d5c94SMichael S. Tsirkin 1376b1e6cc7SJason Wang struct vhost_msg_node { 138429711aeSJason Wang union { 1396b1e6cc7SJason Wang struct vhost_msg msg; 140429711aeSJason Wang struct vhost_msg_v2 msg_v2; 141429711aeSJason Wang }; 1426b1e6cc7SJason Wang struct vhost_virtqueue *vq; 1436b1e6cc7SJason Wang struct list_head node; 1446b1e6cc7SJason Wang }; 1456b1e6cc7SJason Wang 1463a4d5c94SMichael S. Tsirkin struct vhost_dev { 1473a4d5c94SMichael S. Tsirkin struct mm_struct *mm; 1483a4d5c94SMichael S. Tsirkin struct mutex mutex; 1493ab2e420SAsias He struct vhost_virtqueue **vqs; 1503a4d5c94SMichael S. Tsirkin int nvqs; 1513a4d5c94SMichael S. Tsirkin struct eventfd_ctx *log_ctx; 15204b96e55SJason Wang struct llist_head work_list; 153c23f3445STejun Heo struct task_struct *worker; 1540bbe3066SJason Wang struct vhost_iotlb *umem; 1550bbe3066SJason Wang struct vhost_iotlb *iotlb; 1566b1e6cc7SJason Wang spinlock_t iotlb_lock; 1576b1e6cc7SJason Wang struct list_head read_list; 1586b1e6cc7SJason Wang struct list_head pending_list; 1596b1e6cc7SJason Wang wait_queue_head_t wait; 160b46a0bf7SJason Wang int iov_limit; 161e82b9b07SJason Wang int weight; 162e82b9b07SJason Wang int byte_weight; 1638f6a7f96SAndrey Konovalov u64 kcov_handle; 16401fcb1cbSJason Wang bool use_worker; 165792a4f2eSJason Wang int (*msg_handler)(struct vhost_dev *dev, 166792a4f2eSJason Wang struct vhost_iotlb_msg *msg); 1673a4d5c94SMichael S. Tsirkin }; 1683a4d5c94SMichael S. Tsirkin 169e82b9b07SJason Wang bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len); 170b46a0bf7SJason Wang void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, 171792a4f2eSJason Wang int nvqs, int iov_limit, int weight, int byte_weight, 17201fcb1cbSJason Wang bool use_worker, 173792a4f2eSJason Wang int (*msg_handler)(struct vhost_dev *dev, 174792a4f2eSJason Wang struct vhost_iotlb_msg *msg)); 17554db63c2SAsias He long vhost_dev_set_owner(struct vhost_dev *dev); 17605c05351SMichael S. Tsirkin bool vhost_dev_has_owner(struct vhost_dev *dev); 1773a4d5c94SMichael S. Tsirkin long vhost_dev_check_owner(struct vhost_dev *); 1780bbe3066SJason Wang struct vhost_iotlb *vhost_dev_reset_owner_prepare(void); 1790bbe3066SJason Wang void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb); 180f6f93f75S夷则(Caspar) void vhost_dev_cleanup(struct vhost_dev *); 181b211616dSMichael S. Tsirkin void vhost_dev_stop(struct vhost_dev *); 182935cdee7SMichael S. Tsirkin long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp); 18326b36604SSonny Rao long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); 184ddd3d408SStefan Hajnoczi bool vhost_vq_access_ok(struct vhost_virtqueue *vq); 185ddd3d408SStefan Hajnoczi bool vhost_log_access_ok(struct vhost_dev *); 1863a4d5c94SMichael S. Tsirkin 18747283befSMichael S. Tsirkin int vhost_get_vq_desc(struct vhost_virtqueue *, 1883a4d5c94SMichael S. Tsirkin struct iovec iov[], unsigned int iov_count, 1893a4d5c94SMichael S. Tsirkin unsigned int *out_num, unsigned int *in_num, 1903a4d5c94SMichael S. Tsirkin struct vhost_log *log, unsigned int *log_num); 1918dd014adSDavid Stevens void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); 1923a4d5c94SMichael S. Tsirkin 193*6bcf3422SMike Christie bool vhost_vq_is_setup(struct vhost_virtqueue *vq); 19480f7d030SGreg Kurz int vhost_vq_init_access(struct vhost_virtqueue *); 1953a4d5c94SMichael S. Tsirkin int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); 1968dd014adSDavid Stevens int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, 1978dd014adSDavid Stevens unsigned count); 1983a4d5c94SMichael S. Tsirkin void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *, 1998dd014adSDavid Stevens unsigned int id, int len); 2008dd014adSDavid Stevens void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *, 2018dd014adSDavid Stevens struct vring_used_elem *heads, unsigned count); 2028dd014adSDavid Stevens void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *); 2038ea8cf89SMichael S. Tsirkin void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *); 204d4a60603SJason Wang bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *); 2058ea8cf89SMichael S. Tsirkin bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *); 2063a4d5c94SMichael S. Tsirkin 2073a4d5c94SMichael S. Tsirkin int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, 208cc5e7107SJason Wang unsigned int log_num, u64 len, 209cc5e7107SJason Wang struct iovec *iov, int count); 2109b5e830bSJason Wang int vq_meta_prefetch(struct vhost_virtqueue *vq); 2116b1e6cc7SJason Wang 2126b1e6cc7SJason Wang struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type); 2136b1e6cc7SJason Wang void vhost_enqueue_msg(struct vhost_dev *dev, 2146b1e6cc7SJason Wang struct list_head *head, 2156b1e6cc7SJason Wang struct vhost_msg_node *node); 2166b1e6cc7SJason Wang struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, 2176b1e6cc7SJason Wang struct list_head *head); 218460f7ce1SJason Wang void vhost_set_backend_features(struct vhost_dev *dev, u64 features); 219460f7ce1SJason Wang 220afc9a42bSAl Viro __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, 2216b1e6cc7SJason Wang poll_table *wait); 2226b1e6cc7SJason Wang ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, 2236b1e6cc7SJason Wang int noblock); 2246b1e6cc7SJason Wang ssize_t vhost_chr_write_iter(struct vhost_dev *dev, 2256b1e6cc7SJason Wang struct iov_iter *from); 2266b1e6cc7SJason Wang int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled); 2273a4d5c94SMichael S. Tsirkin 2280bbe3066SJason Wang void vhost_iotlb_map_free(struct vhost_iotlb *iotlb, 2290bbe3066SJason Wang struct vhost_iotlb_map *map); 2300bbe3066SJason Wang 2313a4d5c94SMichael S. Tsirkin #define vq_err(vq, fmt, ...) do { \ 2323a4d5c94SMichael S. Tsirkin pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ 2333a4d5c94SMichael S. Tsirkin if ((vq)->error_ctx) \ 2343a4d5c94SMichael S. Tsirkin eventfd_signal((vq)->error_ctx, 1);\ 2353a4d5c94SMichael S. Tsirkin } while (0) 2363a4d5c94SMichael S. Tsirkin 2373a4d5c94SMichael S. Tsirkin enum { 2388ea8cf89SMichael S. Tsirkin VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | 2398ea8cf89SMichael S. Tsirkin (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | 2408ea8cf89SMichael S. Tsirkin (1ULL << VIRTIO_RING_F_EVENT_IDX) | 2414e9fa50cSMichael S. Tsirkin (1ULL << VHOST_F_LOG_ALL) | 2424e9fa50cSMichael S. Tsirkin (1ULL << VIRTIO_F_ANY_LAYOUT) | 2434e9fa50cSMichael S. Tsirkin (1ULL << VIRTIO_F_VERSION_1) 2443a4d5c94SMichael S. Tsirkin }; 2453a4d5c94SMichael S. Tsirkin 246247643f8SEugenio Pérez /** 247247643f8SEugenio Pérez * vhost_vq_set_backend - Set backend. 248247643f8SEugenio Pérez * 249247643f8SEugenio Pérez * @vq Virtqueue. 250247643f8SEugenio Pérez * @private_data The private data. 251247643f8SEugenio Pérez * 252247643f8SEugenio Pérez * Context: Need to call with vq->mutex acquired. 253247643f8SEugenio Pérez */ 254247643f8SEugenio Pérez static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq, 255247643f8SEugenio Pérez void *private_data) 256247643f8SEugenio Pérez { 257247643f8SEugenio Pérez vq->private_data = private_data; 258247643f8SEugenio Pérez } 259247643f8SEugenio Pérez 260247643f8SEugenio Pérez /** 261247643f8SEugenio Pérez * vhost_vq_get_backend - Get backend. 262247643f8SEugenio Pérez * 263247643f8SEugenio Pérez * @vq Virtqueue. 264247643f8SEugenio Pérez * 265247643f8SEugenio Pérez * Context: Need to call with vq->mutex acquired. 266247643f8SEugenio Pérez * Return: Private data previously set with vhost_vq_set_backend. 267247643f8SEugenio Pérez */ 268247643f8SEugenio Pérez static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq) 269247643f8SEugenio Pérez { 270247643f8SEugenio Pérez return vq->private_data; 271247643f8SEugenio Pérez } 272247643f8SEugenio Pérez 273bd82752aSMichael S. Tsirkin static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit) 2743a4d5c94SMichael S. Tsirkin { 275bd82752aSMichael S. Tsirkin return vq->acked_features & (1ULL << bit); 2763a4d5c94SMichael S. Tsirkin } 277e05fd12bSMichael S. Tsirkin 278429711aeSJason Wang static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit) 279429711aeSJason Wang { 280429711aeSJason Wang return vq->acked_backend_features & (1ULL << bit); 281429711aeSJason Wang } 282429711aeSJason Wang 283e407f39aSMichael S. Tsirkin #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 284ab27c07fSGreg Kurz static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) 285ab27c07fSGreg Kurz { 2862751c988SGreg Kurz return vq->is_le; 287ab27c07fSGreg Kurz } 288e407f39aSMichael S. Tsirkin #else 289e407f39aSMichael S. Tsirkin static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) 290e407f39aSMichael S. Tsirkin { 291e407f39aSMichael S. Tsirkin return virtio_legacy_is_little_endian() || vq->is_le; 292e407f39aSMichael S. Tsirkin } 293e407f39aSMichael S. Tsirkin #endif 294ab27c07fSGreg Kurz 295e05fd12bSMichael S. Tsirkin /* Memory accessors */ 296e05fd12bSMichael S. Tsirkin static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val) 297e05fd12bSMichael S. Tsirkin { 298ab27c07fSGreg Kurz return __virtio16_to_cpu(vhost_is_little_endian(vq), val); 299e05fd12bSMichael S. Tsirkin } 300e05fd12bSMichael S. Tsirkin 301e05fd12bSMichael S. Tsirkin static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val) 302e05fd12bSMichael S. Tsirkin { 303ab27c07fSGreg Kurz return __cpu_to_virtio16(vhost_is_little_endian(vq), val); 304e05fd12bSMichael S. Tsirkin } 305e05fd12bSMichael S. Tsirkin 306e05fd12bSMichael S. Tsirkin static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val) 307e05fd12bSMichael S. Tsirkin { 308ab27c07fSGreg Kurz return __virtio32_to_cpu(vhost_is_little_endian(vq), val); 309e05fd12bSMichael S. Tsirkin } 310e05fd12bSMichael S. Tsirkin 311e05fd12bSMichael S. Tsirkin static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val) 312e05fd12bSMichael S. Tsirkin { 313ab27c07fSGreg Kurz return __cpu_to_virtio32(vhost_is_little_endian(vq), val); 314e05fd12bSMichael S. Tsirkin } 315e05fd12bSMichael S. Tsirkin 316e05fd12bSMichael S. Tsirkin static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val) 317e05fd12bSMichael S. Tsirkin { 318ab27c07fSGreg Kurz return __virtio64_to_cpu(vhost_is_little_endian(vq), val); 319e05fd12bSMichael S. Tsirkin } 320e05fd12bSMichael S. Tsirkin 321e05fd12bSMichael S. Tsirkin static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val) 322e05fd12bSMichael S. Tsirkin { 323ab27c07fSGreg Kurz return __cpu_to_virtio64(vhost_is_little_endian(vq), val); 324e05fd12bSMichael S. Tsirkin } 3253a4d5c94SMichael S. Tsirkin #endif 326