xref: /openbmc/linux/include/uapi/linux/vduse.h (revision 79a463be)
1c8a6153bSXie Yongji /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2c8a6153bSXie Yongji #ifndef _UAPI_VDUSE_H_
3c8a6153bSXie Yongji #define _UAPI_VDUSE_H_
4c8a6153bSXie Yongji 
5c8a6153bSXie Yongji #include <linux/types.h>
6c8a6153bSXie Yongji 
7c8a6153bSXie Yongji #define VDUSE_BASE	0x81
8c8a6153bSXie Yongji 
9c8a6153bSXie Yongji /* The ioctls for control device (/dev/vduse/control) */
10c8a6153bSXie Yongji 
11c8a6153bSXie Yongji #define VDUSE_API_VERSION	0
12c8a6153bSXie Yongji 
13c8a6153bSXie Yongji /*
14c8a6153bSXie Yongji  * Get the version of VDUSE API that kernel supported (VDUSE_API_VERSION).
15c8a6153bSXie Yongji  * This is used for future extension.
16c8a6153bSXie Yongji  */
17c8a6153bSXie Yongji #define VDUSE_GET_API_VERSION	_IOR(VDUSE_BASE, 0x00, __u64)
18c8a6153bSXie Yongji 
19c8a6153bSXie Yongji /* Set the version of VDUSE API that userspace supported. */
20c8a6153bSXie Yongji #define VDUSE_SET_API_VERSION	_IOW(VDUSE_BASE, 0x01, __u64)
21c8a6153bSXie Yongji 
22c8a6153bSXie Yongji /**
23c8a6153bSXie Yongji  * struct vduse_dev_config - basic configuration of a VDUSE device
24c8a6153bSXie Yongji  * @name: VDUSE device name, needs to be NUL terminated
25c8a6153bSXie Yongji  * @vendor_id: virtio vendor id
26c8a6153bSXie Yongji  * @device_id: virtio device id
27c8a6153bSXie Yongji  * @features: virtio features
28c8a6153bSXie Yongji  * @vq_num: the number of virtqueues
29c8a6153bSXie Yongji  * @vq_align: the allocation alignment of virtqueue's metadata
30c8a6153bSXie Yongji  * @reserved: for future use, needs to be initialized to zero
31c8a6153bSXie Yongji  * @config_size: the size of the configuration space
32c8a6153bSXie Yongji  * @config: the buffer of the configuration space
33c8a6153bSXie Yongji  *
34c8a6153bSXie Yongji  * Structure used by VDUSE_CREATE_DEV ioctl to create VDUSE device.
35c8a6153bSXie Yongji  */
36c8a6153bSXie Yongji struct vduse_dev_config {
37c8a6153bSXie Yongji #define VDUSE_NAME_MAX	256
38c8a6153bSXie Yongji 	char name[VDUSE_NAME_MAX];
39c8a6153bSXie Yongji 	__u32 vendor_id;
40c8a6153bSXie Yongji 	__u32 device_id;
41c8a6153bSXie Yongji 	__u64 features;
42c8a6153bSXie Yongji 	__u32 vq_num;
43c8a6153bSXie Yongji 	__u32 vq_align;
44c8a6153bSXie Yongji 	__u32 reserved[13];
45c8a6153bSXie Yongji 	__u32 config_size;
46c8a6153bSXie Yongji 	__u8 config[];
47c8a6153bSXie Yongji };
48c8a6153bSXie Yongji 
49c8a6153bSXie Yongji /* Create a VDUSE device which is represented by a char device (/dev/vduse/$NAME) */
50c8a6153bSXie Yongji #define VDUSE_CREATE_DEV	_IOW(VDUSE_BASE, 0x02, struct vduse_dev_config)
51c8a6153bSXie Yongji 
52c8a6153bSXie Yongji /*
53c8a6153bSXie Yongji  * Destroy a VDUSE device. Make sure there are no more references
54c8a6153bSXie Yongji  * to the char device (/dev/vduse/$NAME).
55c8a6153bSXie Yongji  */
56c8a6153bSXie Yongji #define VDUSE_DESTROY_DEV	_IOW(VDUSE_BASE, 0x03, char[VDUSE_NAME_MAX])
57c8a6153bSXie Yongji 
58c8a6153bSXie Yongji /* The ioctls for VDUSE device (/dev/vduse/$NAME) */
59c8a6153bSXie Yongji 
60c8a6153bSXie Yongji /**
61c8a6153bSXie Yongji  * struct vduse_iotlb_entry - entry of IOTLB to describe one IOVA region [start, last]
62c8a6153bSXie Yongji  * @offset: the mmap offset on returned file descriptor
63c8a6153bSXie Yongji  * @start: start of the IOVA region
64c8a6153bSXie Yongji  * @last: last of the IOVA region
65c8a6153bSXie Yongji  * @perm: access permission of the IOVA region
66c8a6153bSXie Yongji  *
67c8a6153bSXie Yongji  * Structure used by VDUSE_IOTLB_GET_FD ioctl to find an overlapped IOVA region.
68c8a6153bSXie Yongji  */
69c8a6153bSXie Yongji struct vduse_iotlb_entry {
70c8a6153bSXie Yongji 	__u64 offset;
71c8a6153bSXie Yongji 	__u64 start;
72c8a6153bSXie Yongji 	__u64 last;
73c8a6153bSXie Yongji #define VDUSE_ACCESS_RO 0x1
74c8a6153bSXie Yongji #define VDUSE_ACCESS_WO 0x2
75c8a6153bSXie Yongji #define VDUSE_ACCESS_RW 0x3
76c8a6153bSXie Yongji 	__u8 perm;
77c8a6153bSXie Yongji };
78c8a6153bSXie Yongji 
79c8a6153bSXie Yongji /*
80c8a6153bSXie Yongji  * Find the first IOVA region that overlaps with the range [start, last]
81c8a6153bSXie Yongji  * and return the corresponding file descriptor. Return -EINVAL means the
82c8a6153bSXie Yongji  * IOVA region doesn't exist. Caller should set start and last fields.
83c8a6153bSXie Yongji  */
84c8a6153bSXie Yongji #define VDUSE_IOTLB_GET_FD	_IOWR(VDUSE_BASE, 0x10, struct vduse_iotlb_entry)
85c8a6153bSXie Yongji 
86c8a6153bSXie Yongji /*
87c8a6153bSXie Yongji  * Get the negotiated virtio features. It's a subset of the features in
88c8a6153bSXie Yongji  * struct vduse_dev_config which can be accepted by virtio driver. It's
89c8a6153bSXie Yongji  * only valid after FEATURES_OK status bit is set.
90c8a6153bSXie Yongji  */
91c8a6153bSXie Yongji #define VDUSE_DEV_GET_FEATURES	_IOR(VDUSE_BASE, 0x11, __u64)
92c8a6153bSXie Yongji 
93c8a6153bSXie Yongji /**
94c8a6153bSXie Yongji  * struct vduse_config_data - data used to update configuration space
95c8a6153bSXie Yongji  * @offset: the offset from the beginning of configuration space
96c8a6153bSXie Yongji  * @length: the length to write to configuration space
97c8a6153bSXie Yongji  * @buffer: the buffer used to write from
98c8a6153bSXie Yongji  *
99c8a6153bSXie Yongji  * Structure used by VDUSE_DEV_SET_CONFIG ioctl to update device
100c8a6153bSXie Yongji  * configuration space.
101c8a6153bSXie Yongji  */
102c8a6153bSXie Yongji struct vduse_config_data {
103c8a6153bSXie Yongji 	__u32 offset;
104c8a6153bSXie Yongji 	__u32 length;
105c8a6153bSXie Yongji 	__u8 buffer[];
106c8a6153bSXie Yongji };
107c8a6153bSXie Yongji 
108c8a6153bSXie Yongji /* Set device configuration space */
109c8a6153bSXie Yongji #define VDUSE_DEV_SET_CONFIG	_IOW(VDUSE_BASE, 0x12, struct vduse_config_data)
110c8a6153bSXie Yongji 
111c8a6153bSXie Yongji /*
112c8a6153bSXie Yongji  * Inject a config interrupt. It's usually used to notify virtio driver
113c8a6153bSXie Yongji  * that device configuration space has changed.
114c8a6153bSXie Yongji  */
115c8a6153bSXie Yongji #define VDUSE_DEV_INJECT_CONFIG_IRQ	_IO(VDUSE_BASE, 0x13)
116c8a6153bSXie Yongji 
117c8a6153bSXie Yongji /**
118c8a6153bSXie Yongji  * struct vduse_vq_config - basic configuration of a virtqueue
119c8a6153bSXie Yongji  * @index: virtqueue index
120c8a6153bSXie Yongji  * @max_size: the max size of virtqueue
121c8a6153bSXie Yongji  * @reserved: for future use, needs to be initialized to zero
122c8a6153bSXie Yongji  *
123c8a6153bSXie Yongji  * Structure used by VDUSE_VQ_SETUP ioctl to setup a virtqueue.
124c8a6153bSXie Yongji  */
125c8a6153bSXie Yongji struct vduse_vq_config {
126c8a6153bSXie Yongji 	__u32 index;
127c8a6153bSXie Yongji 	__u16 max_size;
128c8a6153bSXie Yongji 	__u16 reserved[13];
129c8a6153bSXie Yongji };
130c8a6153bSXie Yongji 
131c8a6153bSXie Yongji /*
132c8a6153bSXie Yongji  * Setup the specified virtqueue. Make sure all virtqueues have been
133c8a6153bSXie Yongji  * configured before the device is attached to vDPA bus.
134c8a6153bSXie Yongji  */
135c8a6153bSXie Yongji #define VDUSE_VQ_SETUP		_IOW(VDUSE_BASE, 0x14, struct vduse_vq_config)
136c8a6153bSXie Yongji 
137c8a6153bSXie Yongji /**
138c8a6153bSXie Yongji  * struct vduse_vq_state_split - split virtqueue state
139c8a6153bSXie Yongji  * @avail_index: available index
140c8a6153bSXie Yongji  */
141c8a6153bSXie Yongji struct vduse_vq_state_split {
142c8a6153bSXie Yongji 	__u16 avail_index;
143c8a6153bSXie Yongji };
144c8a6153bSXie Yongji 
145c8a6153bSXie Yongji /**
146c8a6153bSXie Yongji  * struct vduse_vq_state_packed - packed virtqueue state
147c8a6153bSXie Yongji  * @last_avail_counter: last driver ring wrap counter observed by device
148c8a6153bSXie Yongji  * @last_avail_idx: device available index
149c8a6153bSXie Yongji  * @last_used_counter: device ring wrap counter
150c8a6153bSXie Yongji  * @last_used_idx: used index
151c8a6153bSXie Yongji  */
152c8a6153bSXie Yongji struct vduse_vq_state_packed {
153c8a6153bSXie Yongji 	__u16 last_avail_counter;
154c8a6153bSXie Yongji 	__u16 last_avail_idx;
155c8a6153bSXie Yongji 	__u16 last_used_counter;
156c8a6153bSXie Yongji 	__u16 last_used_idx;
157c8a6153bSXie Yongji };
158c8a6153bSXie Yongji 
159c8a6153bSXie Yongji /**
160c8a6153bSXie Yongji  * struct vduse_vq_info - information of a virtqueue
161c8a6153bSXie Yongji  * @index: virtqueue index
162c8a6153bSXie Yongji  * @num: the size of virtqueue
163c8a6153bSXie Yongji  * @desc_addr: address of desc area
164c8a6153bSXie Yongji  * @driver_addr: address of driver area
165c8a6153bSXie Yongji  * @device_addr: address of device area
166c8a6153bSXie Yongji  * @split: split virtqueue state
167c8a6153bSXie Yongji  * @packed: packed virtqueue state
168c8a6153bSXie Yongji  * @ready: ready status of virtqueue
169c8a6153bSXie Yongji  *
170c8a6153bSXie Yongji  * Structure used by VDUSE_VQ_GET_INFO ioctl to get virtqueue's information.
171c8a6153bSXie Yongji  */
172c8a6153bSXie Yongji struct vduse_vq_info {
173c8a6153bSXie Yongji 	__u32 index;
174c8a6153bSXie Yongji 	__u32 num;
175c8a6153bSXie Yongji 	__u64 desc_addr;
176c8a6153bSXie Yongji 	__u64 driver_addr;
177c8a6153bSXie Yongji 	__u64 device_addr;
178c8a6153bSXie Yongji 	union {
179c8a6153bSXie Yongji 		struct vduse_vq_state_split split;
180c8a6153bSXie Yongji 		struct vduse_vq_state_packed packed;
181c8a6153bSXie Yongji 	};
182c8a6153bSXie Yongji 	__u8 ready;
183c8a6153bSXie Yongji };
184c8a6153bSXie Yongji 
185c8a6153bSXie Yongji /* Get the specified virtqueue's information. Caller should set index field. */
186c8a6153bSXie Yongji #define VDUSE_VQ_GET_INFO	_IOWR(VDUSE_BASE, 0x15, struct vduse_vq_info)
187c8a6153bSXie Yongji 
188c8a6153bSXie Yongji /**
189c8a6153bSXie Yongji  * struct vduse_vq_eventfd - eventfd configuration for a virtqueue
190c8a6153bSXie Yongji  * @index: virtqueue index
191c8a6153bSXie Yongji  * @fd: eventfd, -1 means de-assigning the eventfd
192c8a6153bSXie Yongji  *
193c8a6153bSXie Yongji  * Structure used by VDUSE_VQ_SETUP_KICKFD ioctl to setup kick eventfd.
194c8a6153bSXie Yongji  */
195c8a6153bSXie Yongji struct vduse_vq_eventfd {
196c8a6153bSXie Yongji 	__u32 index;
197c8a6153bSXie Yongji #define VDUSE_EVENTFD_DEASSIGN -1
198c8a6153bSXie Yongji 	int fd;
199c8a6153bSXie Yongji };
200c8a6153bSXie Yongji 
201c8a6153bSXie Yongji /*
202c8a6153bSXie Yongji  * Setup kick eventfd for specified virtqueue. The kick eventfd is used
203c8a6153bSXie Yongji  * by VDUSE kernel module to notify userspace to consume the avail vring.
204c8a6153bSXie Yongji  */
205c8a6153bSXie Yongji #define VDUSE_VQ_SETUP_KICKFD	_IOW(VDUSE_BASE, 0x16, struct vduse_vq_eventfd)
206c8a6153bSXie Yongji 
207c8a6153bSXie Yongji /*
208c8a6153bSXie Yongji  * Inject an interrupt for specific virtqueue. It's used to notify virtio driver
209c8a6153bSXie Yongji  * to consume the used vring.
210c8a6153bSXie Yongji  */
211c8a6153bSXie Yongji #define VDUSE_VQ_INJECT_IRQ	_IOW(VDUSE_BASE, 0x17, __u32)
212c8a6153bSXie Yongji 
213*79a463beSXie Yongji /**
214*79a463beSXie Yongji  * struct vduse_iova_umem - userspace memory configuration for one IOVA region
215*79a463beSXie Yongji  * @uaddr: start address of userspace memory, it must be aligned to page size
216*79a463beSXie Yongji  * @iova: start of the IOVA region
217*79a463beSXie Yongji  * @size: size of the IOVA region
218*79a463beSXie Yongji  * @reserved: for future use, needs to be initialized to zero
219*79a463beSXie Yongji  *
220*79a463beSXie Yongji  * Structure used by VDUSE_IOTLB_REG_UMEM and VDUSE_IOTLB_DEREG_UMEM
221*79a463beSXie Yongji  * ioctls to register/de-register userspace memory for IOVA regions
222*79a463beSXie Yongji  */
223*79a463beSXie Yongji struct vduse_iova_umem {
224*79a463beSXie Yongji 	__u64 uaddr;
225*79a463beSXie Yongji 	__u64 iova;
226*79a463beSXie Yongji 	__u64 size;
227*79a463beSXie Yongji 	__u64 reserved[3];
228*79a463beSXie Yongji };
229*79a463beSXie Yongji 
230*79a463beSXie Yongji /* Register userspace memory for IOVA regions */
231*79a463beSXie Yongji #define VDUSE_IOTLB_REG_UMEM	_IOW(VDUSE_BASE, 0x18, struct vduse_iova_umem)
232*79a463beSXie Yongji 
233*79a463beSXie Yongji /* De-register the userspace memory. Caller should set iova and size field. */
234*79a463beSXie Yongji #define VDUSE_IOTLB_DEREG_UMEM	_IOW(VDUSE_BASE, 0x19, struct vduse_iova_umem)
235*79a463beSXie Yongji 
236c8a6153bSXie Yongji /* The control messages definition for read(2)/write(2) on /dev/vduse/$NAME */
237c8a6153bSXie Yongji 
238c8a6153bSXie Yongji /**
239c8a6153bSXie Yongji  * enum vduse_req_type - request type
240c8a6153bSXie Yongji  * @VDUSE_GET_VQ_STATE: get the state for specified virtqueue from userspace
241c8a6153bSXie Yongji  * @VDUSE_SET_STATUS: set the device status
242c8a6153bSXie Yongji  * @VDUSE_UPDATE_IOTLB: Notify userspace to update the memory mapping for
243c8a6153bSXie Yongji  *                      specified IOVA range via VDUSE_IOTLB_GET_FD ioctl
244c8a6153bSXie Yongji  */
245c8a6153bSXie Yongji enum vduse_req_type {
246c8a6153bSXie Yongji 	VDUSE_GET_VQ_STATE,
247c8a6153bSXie Yongji 	VDUSE_SET_STATUS,
248c8a6153bSXie Yongji 	VDUSE_UPDATE_IOTLB,
249c8a6153bSXie Yongji };
250c8a6153bSXie Yongji 
251c8a6153bSXie Yongji /**
252c8a6153bSXie Yongji  * struct vduse_vq_state - virtqueue state
253c8a6153bSXie Yongji  * @index: virtqueue index
254c8a6153bSXie Yongji  * @split: split virtqueue state
255c8a6153bSXie Yongji  * @packed: packed virtqueue state
256c8a6153bSXie Yongji  */
257c8a6153bSXie Yongji struct vduse_vq_state {
258c8a6153bSXie Yongji 	__u32 index;
259c8a6153bSXie Yongji 	union {
260c8a6153bSXie Yongji 		struct vduse_vq_state_split split;
261c8a6153bSXie Yongji 		struct vduse_vq_state_packed packed;
262c8a6153bSXie Yongji 	};
263c8a6153bSXie Yongji };
264c8a6153bSXie Yongji 
265c8a6153bSXie Yongji /**
266c8a6153bSXie Yongji  * struct vduse_dev_status - device status
267c8a6153bSXie Yongji  * @status: device status
268c8a6153bSXie Yongji  */
269c8a6153bSXie Yongji struct vduse_dev_status {
270c8a6153bSXie Yongji 	__u8 status;
271c8a6153bSXie Yongji };
272c8a6153bSXie Yongji 
273c8a6153bSXie Yongji /**
274c8a6153bSXie Yongji  * struct vduse_iova_range - IOVA range [start, last]
275c8a6153bSXie Yongji  * @start: start of the IOVA range
276c8a6153bSXie Yongji  * @last: last of the IOVA range
277c8a6153bSXie Yongji  */
278c8a6153bSXie Yongji struct vduse_iova_range {
279c8a6153bSXie Yongji 	__u64 start;
280c8a6153bSXie Yongji 	__u64 last;
281c8a6153bSXie Yongji };
282c8a6153bSXie Yongji 
283c8a6153bSXie Yongji /**
284c8a6153bSXie Yongji  * struct vduse_dev_request - control request
285c8a6153bSXie Yongji  * @type: request type
286c8a6153bSXie Yongji  * @request_id: request id
287c8a6153bSXie Yongji  * @reserved: for future use
288c8a6153bSXie Yongji  * @vq_state: virtqueue state, only index field is available
289c8a6153bSXie Yongji  * @s: device status
290c8a6153bSXie Yongji  * @iova: IOVA range for updating
291c8a6153bSXie Yongji  * @padding: padding
292c8a6153bSXie Yongji  *
293c8a6153bSXie Yongji  * Structure used by read(2) on /dev/vduse/$NAME.
294c8a6153bSXie Yongji  */
295c8a6153bSXie Yongji struct vduse_dev_request {
296c8a6153bSXie Yongji 	__u32 type;
297c8a6153bSXie Yongji 	__u32 request_id;
298c8a6153bSXie Yongji 	__u32 reserved[4];
299c8a6153bSXie Yongji 	union {
300c8a6153bSXie Yongji 		struct vduse_vq_state vq_state;
301c8a6153bSXie Yongji 		struct vduse_dev_status s;
302c8a6153bSXie Yongji 		struct vduse_iova_range iova;
303c8a6153bSXie Yongji 		__u32 padding[32];
304c8a6153bSXie Yongji 	};
305c8a6153bSXie Yongji };
306c8a6153bSXie Yongji 
307c8a6153bSXie Yongji /**
308c8a6153bSXie Yongji  * struct vduse_dev_response - response to control request
309c8a6153bSXie Yongji  * @request_id: corresponding request id
310c8a6153bSXie Yongji  * @result: the result of request
311c8a6153bSXie Yongji  * @reserved: for future use, needs to be initialized to zero
312c8a6153bSXie Yongji  * @vq_state: virtqueue state
313c8a6153bSXie Yongji  * @padding: padding
314c8a6153bSXie Yongji  *
315c8a6153bSXie Yongji  * Structure used by write(2) on /dev/vduse/$NAME.
316c8a6153bSXie Yongji  */
317c8a6153bSXie Yongji struct vduse_dev_response {
318c8a6153bSXie Yongji 	__u32 request_id;
319c8a6153bSXie Yongji #define VDUSE_REQ_RESULT_OK	0x00
320c8a6153bSXie Yongji #define VDUSE_REQ_RESULT_FAILED	0x01
321c8a6153bSXie Yongji 	__u32 result;
322c8a6153bSXie Yongji 	__u32 reserved[4];
323c8a6153bSXie Yongji 	union {
324c8a6153bSXie Yongji 		struct vduse_vq_state vq_state;
325c8a6153bSXie Yongji 		__u32 padding[32];
326c8a6153bSXie Yongji 	};
327c8a6153bSXie Yongji };
328c8a6153bSXie Yongji 
329c8a6153bSXie Yongji #endif /* _UAPI_VDUSE_H_ */
330