xref: /openbmc/linux/include/uapi/linux/vduse.h (revision ad146355)
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 
21379a463beSXie Yongji /**
21479a463beSXie Yongji  * struct vduse_iova_umem - userspace memory configuration for one IOVA region
21579a463beSXie Yongji  * @uaddr: start address of userspace memory, it must be aligned to page size
21679a463beSXie Yongji  * @iova: start of the IOVA region
21779a463beSXie Yongji  * @size: size of the IOVA region
21879a463beSXie Yongji  * @reserved: for future use, needs to be initialized to zero
21979a463beSXie Yongji  *
22079a463beSXie Yongji  * Structure used by VDUSE_IOTLB_REG_UMEM and VDUSE_IOTLB_DEREG_UMEM
22179a463beSXie Yongji  * ioctls to register/de-register userspace memory for IOVA regions
22279a463beSXie Yongji  */
22379a463beSXie Yongji struct vduse_iova_umem {
22479a463beSXie Yongji 	__u64 uaddr;
22579a463beSXie Yongji 	__u64 iova;
22679a463beSXie Yongji 	__u64 size;
22779a463beSXie Yongji 	__u64 reserved[3];
22879a463beSXie Yongji };
22979a463beSXie Yongji 
23079a463beSXie Yongji /* Register userspace memory for IOVA regions */
23179a463beSXie Yongji #define VDUSE_IOTLB_REG_UMEM	_IOW(VDUSE_BASE, 0x18, struct vduse_iova_umem)
23279a463beSXie Yongji 
23379a463beSXie Yongji /* De-register the userspace memory. Caller should set iova and size field. */
23479a463beSXie Yongji #define VDUSE_IOTLB_DEREG_UMEM	_IOW(VDUSE_BASE, 0x19, struct vduse_iova_umem)
23579a463beSXie Yongji 
236*ad146355SXie Yongji /**
237*ad146355SXie Yongji  * struct vduse_iova_info - information of one IOVA region
238*ad146355SXie Yongji  * @start: start of the IOVA region
239*ad146355SXie Yongji  * @last: last of the IOVA region
240*ad146355SXie Yongji  * @capability: capability of the IOVA regsion
241*ad146355SXie Yongji  * @reserved: for future use, needs to be initialized to zero
242*ad146355SXie Yongji  *
243*ad146355SXie Yongji  * Structure used by VDUSE_IOTLB_GET_INFO ioctl to get information of
244*ad146355SXie Yongji  * one IOVA region.
245*ad146355SXie Yongji  */
246*ad146355SXie Yongji struct vduse_iova_info {
247*ad146355SXie Yongji 	__u64 start;
248*ad146355SXie Yongji 	__u64 last;
249*ad146355SXie Yongji #define VDUSE_IOVA_CAP_UMEM (1 << 0)
250*ad146355SXie Yongji 	__u64 capability;
251*ad146355SXie Yongji 	__u64 reserved[3];
252*ad146355SXie Yongji };
253*ad146355SXie Yongji 
254*ad146355SXie Yongji /*
255*ad146355SXie Yongji  * Find the first IOVA region that overlaps with the range [start, last]
256*ad146355SXie Yongji  * and return some information on it. Caller should set start and last fields.
257*ad146355SXie Yongji  */
258*ad146355SXie Yongji #define VDUSE_IOTLB_GET_INFO	_IOWR(VDUSE_BASE, 0x1a, struct vduse_iova_info)
259*ad146355SXie Yongji 
260c8a6153bSXie Yongji /* The control messages definition for read(2)/write(2) on /dev/vduse/$NAME */
261c8a6153bSXie Yongji 
262c8a6153bSXie Yongji /**
263c8a6153bSXie Yongji  * enum vduse_req_type - request type
264c8a6153bSXie Yongji  * @VDUSE_GET_VQ_STATE: get the state for specified virtqueue from userspace
265c8a6153bSXie Yongji  * @VDUSE_SET_STATUS: set the device status
266c8a6153bSXie Yongji  * @VDUSE_UPDATE_IOTLB: Notify userspace to update the memory mapping for
267c8a6153bSXie Yongji  *                      specified IOVA range via VDUSE_IOTLB_GET_FD ioctl
268c8a6153bSXie Yongji  */
269c8a6153bSXie Yongji enum vduse_req_type {
270c8a6153bSXie Yongji 	VDUSE_GET_VQ_STATE,
271c8a6153bSXie Yongji 	VDUSE_SET_STATUS,
272c8a6153bSXie Yongji 	VDUSE_UPDATE_IOTLB,
273c8a6153bSXie Yongji };
274c8a6153bSXie Yongji 
275c8a6153bSXie Yongji /**
276c8a6153bSXie Yongji  * struct vduse_vq_state - virtqueue state
277c8a6153bSXie Yongji  * @index: virtqueue index
278c8a6153bSXie Yongji  * @split: split virtqueue state
279c8a6153bSXie Yongji  * @packed: packed virtqueue state
280c8a6153bSXie Yongji  */
281c8a6153bSXie Yongji struct vduse_vq_state {
282c8a6153bSXie Yongji 	__u32 index;
283c8a6153bSXie Yongji 	union {
284c8a6153bSXie Yongji 		struct vduse_vq_state_split split;
285c8a6153bSXie Yongji 		struct vduse_vq_state_packed packed;
286c8a6153bSXie Yongji 	};
287c8a6153bSXie Yongji };
288c8a6153bSXie Yongji 
289c8a6153bSXie Yongji /**
290c8a6153bSXie Yongji  * struct vduse_dev_status - device status
291c8a6153bSXie Yongji  * @status: device status
292c8a6153bSXie Yongji  */
293c8a6153bSXie Yongji struct vduse_dev_status {
294c8a6153bSXie Yongji 	__u8 status;
295c8a6153bSXie Yongji };
296c8a6153bSXie Yongji 
297c8a6153bSXie Yongji /**
298c8a6153bSXie Yongji  * struct vduse_iova_range - IOVA range [start, last]
299c8a6153bSXie Yongji  * @start: start of the IOVA range
300c8a6153bSXie Yongji  * @last: last of the IOVA range
301c8a6153bSXie Yongji  */
302c8a6153bSXie Yongji struct vduse_iova_range {
303c8a6153bSXie Yongji 	__u64 start;
304c8a6153bSXie Yongji 	__u64 last;
305c8a6153bSXie Yongji };
306c8a6153bSXie Yongji 
307c8a6153bSXie Yongji /**
308c8a6153bSXie Yongji  * struct vduse_dev_request - control request
309c8a6153bSXie Yongji  * @type: request type
310c8a6153bSXie Yongji  * @request_id: request id
311c8a6153bSXie Yongji  * @reserved: for future use
312c8a6153bSXie Yongji  * @vq_state: virtqueue state, only index field is available
313c8a6153bSXie Yongji  * @s: device status
314c8a6153bSXie Yongji  * @iova: IOVA range for updating
315c8a6153bSXie Yongji  * @padding: padding
316c8a6153bSXie Yongji  *
317c8a6153bSXie Yongji  * Structure used by read(2) on /dev/vduse/$NAME.
318c8a6153bSXie Yongji  */
319c8a6153bSXie Yongji struct vduse_dev_request {
320c8a6153bSXie Yongji 	__u32 type;
321c8a6153bSXie Yongji 	__u32 request_id;
322c8a6153bSXie Yongji 	__u32 reserved[4];
323c8a6153bSXie Yongji 	union {
324c8a6153bSXie Yongji 		struct vduse_vq_state vq_state;
325c8a6153bSXie Yongji 		struct vduse_dev_status s;
326c8a6153bSXie Yongji 		struct vduse_iova_range iova;
327c8a6153bSXie Yongji 		__u32 padding[32];
328c8a6153bSXie Yongji 	};
329c8a6153bSXie Yongji };
330c8a6153bSXie Yongji 
331c8a6153bSXie Yongji /**
332c8a6153bSXie Yongji  * struct vduse_dev_response - response to control request
333c8a6153bSXie Yongji  * @request_id: corresponding request id
334c8a6153bSXie Yongji  * @result: the result of request
335c8a6153bSXie Yongji  * @reserved: for future use, needs to be initialized to zero
336c8a6153bSXie Yongji  * @vq_state: virtqueue state
337c8a6153bSXie Yongji  * @padding: padding
338c8a6153bSXie Yongji  *
339c8a6153bSXie Yongji  * Structure used by write(2) on /dev/vduse/$NAME.
340c8a6153bSXie Yongji  */
341c8a6153bSXie Yongji struct vduse_dev_response {
342c8a6153bSXie Yongji 	__u32 request_id;
343c8a6153bSXie Yongji #define VDUSE_REQ_RESULT_OK	0x00
344c8a6153bSXie Yongji #define VDUSE_REQ_RESULT_FAILED	0x01
345c8a6153bSXie Yongji 	__u32 result;
346c8a6153bSXie Yongji 	__u32 reserved[4];
347c8a6153bSXie Yongji 	union {
348c8a6153bSXie Yongji 		struct vduse_vq_state vq_state;
349c8a6153bSXie Yongji 		__u32 padding[32];
350c8a6153bSXie Yongji 	};
351c8a6153bSXie Yongji };
352c8a6153bSXie Yongji 
353c8a6153bSXie Yongji #endif /* _UAPI_VDUSE_H_ */
354