1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 /**
9  * @file vhost.h
10  *
11  * @brief VHOST APIs for ACRN Project
12  */
13 
14 #ifndef __VHOST_H__
15 #define __VHOST_H__
16 
17 #include "virtio.h"
18 
19 /**
20  * @brief vhost APIs
21  *
22  * @addtogroup acrn_virtio
23  *
24  */
25 
26 struct vhost_vq {
27 	int kick_fd;		/**< fd of kick eventfd */
28 	int call_fd;		/**< fd of call eventfd */
29 	int idx;		/**< index of this vq in vhost dev */
30 	struct vhost_dev *dev;	/**< pointer to vhost_dev */
31 };
32 
33 struct vhost_dev {
34 	/**
35 	 * backpointer to virtio_base
36 	 */
37 	struct virtio_base *base;
38 
39 	/**
40 	 * pointer to vhost_vq array
41 	 */
42 	struct vhost_vq *vqs;
43 
44 	/**
45 	 * number of virtqueues
46 	 */
47 	int nvqs;
48 
49 	/**
50 	 * vhost chardev fd
51 	 */
52 	int fd;
53 
54 	/**
55 	 * first vq's index in virtio_vq_info
56 	 */
57 	int vq_idx;
58 
59 	/**
60 	 * supported virtio defined features
61 	 */
62 	uint64_t vhost_features;
63 
64 	/**
65 	 * vhost self-defined internal features bits used for
66 	 * communicate between vhost user-space and kernel-space modules
67 	 */
68 	uint64_t vhost_ext_features;
69 
70 	/**
71 	 * vq busyloop timeout in us
72 	 */
73 	uint32_t busyloop_timeout;
74 
75 	/**
76 	 * whether vhost is started
77 	 */
78 	bool started;
79 };
80 
81 /**
82  * @brief vhost_dev initialization.
83  *
84  * This interface is called to initialize the vhost_dev. It must be called
85  * before the actual feature negotiation with the guest OS starts.
86  *
87  * @param vdev Pointer to struct vhost_dev.
88  * @param base Pointer to struct virtio_base.
89  * @param fd fd of the vhost chardev.
90  * @param vq_idx The first virtqueue which would be used by this vhost dev.
91  * @param vhost_features Subset of vhost features which would be enabled.
92  * @param vhost_ext_features Specific vhost internal features to be enabled.
93  * @param busyloop_timeout Busy loop timeout in us.
94  *
95  * @return 0 on success and -1 on failure.
96  */
97 int vhost_dev_init(struct vhost_dev *vdev, struct virtio_base *base, int fd,
98 		   int vq_idx, uint64_t vhost_features,
99 		   uint64_t vhost_ext_features, uint32_t busyloop_timeout);
100 
101 /**
102  * @brief vhost_dev cleanup.
103  *
104  * This interface is called to cleanup the vhost_dev.
105  *
106  * @param vdev Pointer to struct vhost_dev.
107  *
108  * @return 0 on success and -1 on failure.
109  */
110 int vhost_dev_deinit(struct vhost_dev *vdev);
111 
112 /**
113  * @brief start vhost data plane.
114  *
115  * This interface is called to start the data plane in vhost.
116  *
117  * @param vdev Pointer to struct vhost_dev.
118  *
119  * @return 0 on success and -1 on failure.
120  */
121 int vhost_dev_start(struct vhost_dev *vdev);
122 
123 /**
124  * @brief stop vhost data plane.
125  *
126  * This interface is called to stop the data plane in vhost.
127  *
128  * @param vdev Pointer to struct vhost_dev.
129  *
130  * @return 0 on success and -1 on failure.
131  */
132 int vhost_dev_stop(struct vhost_dev *vdev);
133 
134 /**
135  * @brief vhost kernel dev ioctrl function.
136  *
137  * This interface is used to operation the vhost dev kernel.
138  *
139  * @param vdev Pointer to struct vhost_dev.
140  * @param request to vhost kernel.
141  * @param arg of vhost kernel operation.
142  *
143  * @return 0 on success and -1 on failure.
144  */
145 int vhost_kernel_ioctl(struct vhost_dev *vdev, unsigned long int request, void *arg);
146 #endif /* __VHOST_H__ */
147