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> 163a4d5c94SMichael S. Tsirkin 17c23f3445STejun Heo struct vhost_work; 18c23f3445STejun Heo typedef void (*vhost_work_fn_t)(struct vhost_work *work); 19c23f3445STejun Heo 2004b96e55SJason Wang #define VHOST_WORK_QUEUED 1 21c23f3445STejun Heo struct vhost_work { 2204b96e55SJason Wang struct llist_node node; 23c23f3445STejun Heo vhost_work_fn_t fn; 2404b96e55SJason Wang unsigned long flags; 25c23f3445STejun Heo }; 26c23f3445STejun Heo 273a4d5c94SMichael S. Tsirkin /* Poll a file (eventfd or socket) */ 283a4d5c94SMichael S. Tsirkin /* Note: there's nothing vhost specific about this structure. */ 293a4d5c94SMichael S. Tsirkin struct vhost_poll { 303a4d5c94SMichael S. Tsirkin poll_table table; 313a4d5c94SMichael S. Tsirkin wait_queue_head_t *wqh; 32ac6424b9SIngo Molnar wait_queue_entry_t wait; 33c23f3445STejun Heo struct vhost_work work; 3458e3b602SAl Viro __poll_t mask; 35c23f3445STejun Heo struct vhost_dev *dev; 363a4d5c94SMichael S. Tsirkin }; 373a4d5c94SMichael S. Tsirkin 38163049aeSStefan Hajnoczi void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn); 39163049aeSStefan Hajnoczi void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work); 40526d3e7fSJason Wang bool vhost_has_work(struct vhost_dev *dev); 41163049aeSStefan Hajnoczi 42c23f3445STejun Heo void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, 4358e3b602SAl Viro __poll_t mask, struct vhost_dev *dev); 442b8b328bSJason Wang int vhost_poll_start(struct vhost_poll *poll, struct file *file); 453a4d5c94SMichael S. Tsirkin void vhost_poll_stop(struct vhost_poll *poll); 463a4d5c94SMichael S. Tsirkin void vhost_poll_flush(struct vhost_poll *poll); 473a4d5c94SMichael S. Tsirkin void vhost_poll_queue(struct vhost_poll *poll); 486ac1afbfSAsias He void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work); 4926b36604SSonny Rao long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); 503a4d5c94SMichael S. Tsirkin 513a4d5c94SMichael S. Tsirkin struct vhost_log { 523a4d5c94SMichael S. Tsirkin u64 addr; 533a4d5c94SMichael S. Tsirkin u64 len; 543a4d5c94SMichael S. Tsirkin }; 553a4d5c94SMichael S. Tsirkin 56f8894913SJason Wang enum vhost_uaddr_type { 57f8894913SJason Wang VHOST_ADDR_DESC = 0, 58f8894913SJason Wang VHOST_ADDR_AVAIL = 1, 59f8894913SJason Wang VHOST_ADDR_USED = 2, 60f8894913SJason Wang VHOST_NUM_ADDRS = 3, 61f8894913SJason Wang }; 62f8894913SJason Wang 633a4d5c94SMichael S. Tsirkin /* The virtqueue structure describes a queue attached to a device. */ 643a4d5c94SMichael S. Tsirkin struct vhost_virtqueue { 653a4d5c94SMichael S. Tsirkin struct vhost_dev *dev; 663a4d5c94SMichael S. Tsirkin 673a4d5c94SMichael S. Tsirkin /* The actual ring of buffers. */ 683a4d5c94SMichael S. Tsirkin struct mutex mutex; 693a4d5c94SMichael S. Tsirkin unsigned int num; 70a865e420SMichael S. Tsirkin vring_desc_t __user *desc; 71a865e420SMichael S. Tsirkin vring_avail_t __user *avail; 72a865e420SMichael S. Tsirkin vring_used_t __user *used; 730bbe3066SJason Wang const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS]; 743a4d5c94SMichael S. Tsirkin struct file *kick; 753a4d5c94SMichael S. Tsirkin struct eventfd_ctx *call_ctx; 763a4d5c94SMichael S. Tsirkin struct eventfd_ctx *error_ctx; 773a4d5c94SMichael S. Tsirkin struct eventfd_ctx *log_ctx; 783a4d5c94SMichael S. Tsirkin 793a4d5c94SMichael S. Tsirkin struct vhost_poll poll; 803a4d5c94SMichael S. Tsirkin 813a4d5c94SMichael S. Tsirkin /* The routine to call when the Guest pings us, or timeout. */ 82c23f3445STejun Heo vhost_work_fn_t handle_kick; 833a4d5c94SMichael S. Tsirkin 843a4d5c94SMichael S. Tsirkin /* Last available index we saw. */ 853a4d5c94SMichael S. Tsirkin u16 last_avail_idx; 863a4d5c94SMichael S. Tsirkin 873a4d5c94SMichael S. Tsirkin /* Caches available index value from user. */ 883a4d5c94SMichael S. Tsirkin u16 avail_idx; 893a4d5c94SMichael S. Tsirkin 903a4d5c94SMichael S. Tsirkin /* Last index we used. */ 913a4d5c94SMichael S. Tsirkin u16 last_used_idx; 923a4d5c94SMichael S. Tsirkin 933a4d5c94SMichael S. Tsirkin /* Used flags */ 943a4d5c94SMichael S. Tsirkin u16 used_flags; 953a4d5c94SMichael S. Tsirkin 968ea8cf89SMichael S. Tsirkin /* Last used index value we have signalled on */ 978ea8cf89SMichael S. Tsirkin u16 signalled_used; 988ea8cf89SMichael S. Tsirkin 998ea8cf89SMichael S. Tsirkin /* Last used index value we have signalled on */ 1008ea8cf89SMichael S. Tsirkin bool signalled_used_valid; 1018ea8cf89SMichael S. Tsirkin 1023a4d5c94SMichael S. Tsirkin /* Log writes to used structure. */ 1033a4d5c94SMichael S. Tsirkin bool log_used; 1043a4d5c94SMichael S. Tsirkin u64 log_addr; 1053a4d5c94SMichael S. Tsirkin 106e0e9b406SJason Wang struct iovec iov[UIO_MAXIOV]; 1076b1e6cc7SJason Wang struct iovec iotlb_iov[64]; 108e0e9b406SJason Wang struct iovec *indirect; 109e0e9b406SJason Wang struct vring_used_elem *heads; 11022fa90c7SAsias He /* Protected by virtqueue mutex. */ 1110bbe3066SJason Wang struct vhost_iotlb *umem; 1120bbe3066SJason Wang struct vhost_iotlb *iotlb; 11322fa90c7SAsias He void *private_data; 114bd82752aSMichael S. Tsirkin u64 acked_features; 115429711aeSJason Wang u64 acked_backend_features; 1163a4d5c94SMichael S. Tsirkin /* Log write descriptors */ 1173a4d5c94SMichael S. Tsirkin void __user *log_base; 118e0e9b406SJason Wang struct vhost_log *log; 1192751c988SGreg Kurz 1202751c988SGreg Kurz /* Ring endianness. Defaults to legacy native endianness. 1212751c988SGreg Kurz * Set to true when starting a modern virtio device. */ 1222751c988SGreg Kurz bool is_le; 1232751c988SGreg Kurz #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 1242751c988SGreg Kurz /* Ring endianness requested by userspace for cross-endian support. */ 1252751c988SGreg Kurz bool user_be; 1262751c988SGreg Kurz #endif 12703088137SJason Wang u32 busyloop_timeout; 1283a4d5c94SMichael S. Tsirkin }; 1293a4d5c94SMichael S. Tsirkin 1306b1e6cc7SJason Wang struct vhost_msg_node { 131429711aeSJason Wang union { 1326b1e6cc7SJason Wang struct vhost_msg msg; 133429711aeSJason Wang struct vhost_msg_v2 msg_v2; 134429711aeSJason Wang }; 1356b1e6cc7SJason Wang struct vhost_virtqueue *vq; 1366b1e6cc7SJason Wang struct list_head node; 1376b1e6cc7SJason Wang }; 1386b1e6cc7SJason Wang 1393a4d5c94SMichael S. Tsirkin struct vhost_dev { 1403a4d5c94SMichael S. Tsirkin struct mm_struct *mm; 1413a4d5c94SMichael S. Tsirkin struct mutex mutex; 1423ab2e420SAsias He struct vhost_virtqueue **vqs; 1433a4d5c94SMichael S. Tsirkin int nvqs; 1443a4d5c94SMichael S. Tsirkin struct eventfd_ctx *log_ctx; 14504b96e55SJason Wang struct llist_head work_list; 146c23f3445STejun Heo struct task_struct *worker; 1470bbe3066SJason Wang struct vhost_iotlb *umem; 1480bbe3066SJason Wang struct vhost_iotlb *iotlb; 1496b1e6cc7SJason Wang spinlock_t iotlb_lock; 1506b1e6cc7SJason Wang struct list_head read_list; 1516b1e6cc7SJason Wang struct list_head pending_list; 1526b1e6cc7SJason Wang wait_queue_head_t wait; 153b46a0bf7SJason Wang int iov_limit; 154e82b9b07SJason Wang int weight; 155e82b9b07SJason Wang int byte_weight; 1568f6a7f96SAndrey Konovalov u64 kcov_handle; 157*01fcb1cbSJason Wang bool use_worker; 158792a4f2eSJason Wang int (*msg_handler)(struct vhost_dev *dev, 159792a4f2eSJason Wang struct vhost_iotlb_msg *msg); 1603a4d5c94SMichael S. Tsirkin }; 1613a4d5c94SMichael S. Tsirkin 162e82b9b07SJason Wang bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len); 163b46a0bf7SJason Wang void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, 164792a4f2eSJason Wang int nvqs, int iov_limit, int weight, int byte_weight, 165*01fcb1cbSJason Wang bool use_worker, 166792a4f2eSJason Wang int (*msg_handler)(struct vhost_dev *dev, 167792a4f2eSJason Wang struct vhost_iotlb_msg *msg)); 16854db63c2SAsias He long vhost_dev_set_owner(struct vhost_dev *dev); 16905c05351SMichael S. Tsirkin bool vhost_dev_has_owner(struct vhost_dev *dev); 1703a4d5c94SMichael S. Tsirkin long vhost_dev_check_owner(struct vhost_dev *); 1710bbe3066SJason Wang struct vhost_iotlb *vhost_dev_reset_owner_prepare(void); 1720bbe3066SJason Wang void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb); 173f6f93f75S夷则(Caspar) void vhost_dev_cleanup(struct vhost_dev *); 174b211616dSMichael S. Tsirkin void vhost_dev_stop(struct vhost_dev *); 175935cdee7SMichael S. Tsirkin long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp); 17626b36604SSonny Rao long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); 177ddd3d408SStefan Hajnoczi bool vhost_vq_access_ok(struct vhost_virtqueue *vq); 178ddd3d408SStefan Hajnoczi bool vhost_log_access_ok(struct vhost_dev *); 1793a4d5c94SMichael S. Tsirkin 18047283befSMichael S. Tsirkin int vhost_get_vq_desc(struct vhost_virtqueue *, 1813a4d5c94SMichael S. Tsirkin struct iovec iov[], unsigned int iov_count, 1823a4d5c94SMichael S. Tsirkin unsigned int *out_num, unsigned int *in_num, 1833a4d5c94SMichael S. Tsirkin struct vhost_log *log, unsigned int *log_num); 1848dd014adSDavid Stevens void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); 1853a4d5c94SMichael S. Tsirkin 18680f7d030SGreg Kurz int vhost_vq_init_access(struct vhost_virtqueue *); 1873a4d5c94SMichael S. Tsirkin int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); 1888dd014adSDavid Stevens int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, 1898dd014adSDavid Stevens unsigned count); 1903a4d5c94SMichael S. Tsirkin void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *, 1918dd014adSDavid Stevens unsigned int id, int len); 1928dd014adSDavid Stevens void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *, 1938dd014adSDavid Stevens struct vring_used_elem *heads, unsigned count); 1948dd014adSDavid Stevens void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *); 1958ea8cf89SMichael S. Tsirkin void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *); 196d4a60603SJason Wang bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *); 1978ea8cf89SMichael S. Tsirkin bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *); 1983a4d5c94SMichael S. Tsirkin 1993a4d5c94SMichael S. Tsirkin int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, 200cc5e7107SJason Wang unsigned int log_num, u64 len, 201cc5e7107SJason Wang struct iovec *iov, int count); 2029b5e830bSJason Wang int vq_meta_prefetch(struct vhost_virtqueue *vq); 2036b1e6cc7SJason Wang 2046b1e6cc7SJason Wang struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type); 2056b1e6cc7SJason Wang void vhost_enqueue_msg(struct vhost_dev *dev, 2066b1e6cc7SJason Wang struct list_head *head, 2076b1e6cc7SJason Wang struct vhost_msg_node *node); 2086b1e6cc7SJason Wang struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, 2096b1e6cc7SJason Wang struct list_head *head); 210afc9a42bSAl Viro __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, 2116b1e6cc7SJason Wang poll_table *wait); 2126b1e6cc7SJason Wang ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, 2136b1e6cc7SJason Wang int noblock); 2146b1e6cc7SJason Wang ssize_t vhost_chr_write_iter(struct vhost_dev *dev, 2156b1e6cc7SJason Wang struct iov_iter *from); 2166b1e6cc7SJason Wang int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled); 2173a4d5c94SMichael S. Tsirkin 2180bbe3066SJason Wang void vhost_iotlb_map_free(struct vhost_iotlb *iotlb, 2190bbe3066SJason Wang struct vhost_iotlb_map *map); 2200bbe3066SJason Wang 2213a4d5c94SMichael S. Tsirkin #define vq_err(vq, fmt, ...) do { \ 2223a4d5c94SMichael S. Tsirkin pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ 2233a4d5c94SMichael S. Tsirkin if ((vq)->error_ctx) \ 2243a4d5c94SMichael S. Tsirkin eventfd_signal((vq)->error_ctx, 1);\ 2253a4d5c94SMichael S. Tsirkin } while (0) 2263a4d5c94SMichael S. Tsirkin 2273a4d5c94SMichael S. Tsirkin enum { 2288ea8cf89SMichael S. Tsirkin VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | 2298ea8cf89SMichael S. Tsirkin (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | 2308ea8cf89SMichael S. Tsirkin (1ULL << VIRTIO_RING_F_EVENT_IDX) | 2314e9fa50cSMichael S. Tsirkin (1ULL << VHOST_F_LOG_ALL) | 2324e9fa50cSMichael S. Tsirkin (1ULL << VIRTIO_F_ANY_LAYOUT) | 2334e9fa50cSMichael S. Tsirkin (1ULL << VIRTIO_F_VERSION_1) 2343a4d5c94SMichael S. Tsirkin }; 2353a4d5c94SMichael S. Tsirkin 236247643f8SEugenio Pérez /** 237247643f8SEugenio Pérez * vhost_vq_set_backend - Set backend. 238247643f8SEugenio Pérez * 239247643f8SEugenio Pérez * @vq Virtqueue. 240247643f8SEugenio Pérez * @private_data The private data. 241247643f8SEugenio Pérez * 242247643f8SEugenio Pérez * Context: Need to call with vq->mutex acquired. 243247643f8SEugenio Pérez */ 244247643f8SEugenio Pérez static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq, 245247643f8SEugenio Pérez void *private_data) 246247643f8SEugenio Pérez { 247247643f8SEugenio Pérez vq->private_data = private_data; 248247643f8SEugenio Pérez } 249247643f8SEugenio Pérez 250247643f8SEugenio Pérez /** 251247643f8SEugenio Pérez * vhost_vq_get_backend - Get backend. 252247643f8SEugenio Pérez * 253247643f8SEugenio Pérez * @vq Virtqueue. 254247643f8SEugenio Pérez * 255247643f8SEugenio Pérez * Context: Need to call with vq->mutex acquired. 256247643f8SEugenio Pérez * Return: Private data previously set with vhost_vq_set_backend. 257247643f8SEugenio Pérez */ 258247643f8SEugenio Pérez static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq) 259247643f8SEugenio Pérez { 260247643f8SEugenio Pérez return vq->private_data; 261247643f8SEugenio Pérez } 262247643f8SEugenio Pérez 263bd82752aSMichael S. Tsirkin static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit) 2643a4d5c94SMichael S. Tsirkin { 265bd82752aSMichael S. Tsirkin return vq->acked_features & (1ULL << bit); 2663a4d5c94SMichael S. Tsirkin } 267e05fd12bSMichael S. Tsirkin 268429711aeSJason Wang static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit) 269429711aeSJason Wang { 270429711aeSJason Wang return vq->acked_backend_features & (1ULL << bit); 271429711aeSJason Wang } 272429711aeSJason Wang 273e407f39aSMichael S. Tsirkin #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 274ab27c07fSGreg Kurz static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) 275ab27c07fSGreg Kurz { 2762751c988SGreg Kurz return vq->is_le; 277ab27c07fSGreg Kurz } 278e407f39aSMichael S. Tsirkin #else 279e407f39aSMichael S. Tsirkin static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) 280e407f39aSMichael S. Tsirkin { 281e407f39aSMichael S. Tsirkin return virtio_legacy_is_little_endian() || vq->is_le; 282e407f39aSMichael S. Tsirkin } 283e407f39aSMichael S. Tsirkin #endif 284ab27c07fSGreg Kurz 285e05fd12bSMichael S. Tsirkin /* Memory accessors */ 286e05fd12bSMichael S. Tsirkin static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val) 287e05fd12bSMichael S. Tsirkin { 288ab27c07fSGreg Kurz return __virtio16_to_cpu(vhost_is_little_endian(vq), val); 289e05fd12bSMichael S. Tsirkin } 290e05fd12bSMichael S. Tsirkin 291e05fd12bSMichael S. Tsirkin static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val) 292e05fd12bSMichael S. Tsirkin { 293ab27c07fSGreg Kurz return __cpu_to_virtio16(vhost_is_little_endian(vq), val); 294e05fd12bSMichael S. Tsirkin } 295e05fd12bSMichael S. Tsirkin 296e05fd12bSMichael S. Tsirkin static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val) 297e05fd12bSMichael S. Tsirkin { 298ab27c07fSGreg Kurz return __virtio32_to_cpu(vhost_is_little_endian(vq), val); 299e05fd12bSMichael S. Tsirkin } 300e05fd12bSMichael S. Tsirkin 301e05fd12bSMichael S. Tsirkin static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val) 302e05fd12bSMichael S. Tsirkin { 303ab27c07fSGreg Kurz return __cpu_to_virtio32(vhost_is_little_endian(vq), val); 304e05fd12bSMichael S. Tsirkin } 305e05fd12bSMichael S. Tsirkin 306e05fd12bSMichael S. Tsirkin static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val) 307e05fd12bSMichael S. Tsirkin { 308ab27c07fSGreg Kurz return __virtio64_to_cpu(vhost_is_little_endian(vq), val); 309e05fd12bSMichael S. Tsirkin } 310e05fd12bSMichael S. Tsirkin 311e05fd12bSMichael S. Tsirkin static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val) 312e05fd12bSMichael S. Tsirkin { 313ab27c07fSGreg Kurz return __cpu_to_virtio64(vhost_is_little_endian(vq), val); 314e05fd12bSMichael S. Tsirkin } 3153a4d5c94SMichael S. Tsirkin #endif 316