1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * VFIO API definition
4  *
5  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
6  *     Author: Alex Williamson <alex.williamson@redhat.com>
7  */
8 #ifndef VFIO_H
9 #define VFIO_H
10 
11 
12 #include <linux/iommu.h>
13 #include <linux/mm.h>
14 #include <linux/workqueue.h>
15 #include <linux/poll.h>
16 #include <uapi/linux/vfio.h>
17 
18 struct kvm;
19 
20 /*
21  * VFIO devices can be placed in a set, this allows all devices to share this
22  * structure and the VFIO core will provide a lock that is held around
23  * open_device()/close_device() for all devices in the set.
24  */
25 struct vfio_device_set {
26 	void *set_id;
27 	struct mutex lock;
28 	struct list_head device_list;
29 	unsigned int device_count;
30 };
31 
32 struct vfio_device {
33 	struct device *dev;
34 	const struct vfio_device_ops *ops;
35 	/*
36 	 * mig_ops is a static property of the vfio_device which must be set
37 	 * prior to registering the vfio_device.
38 	 */
39 	const struct vfio_migration_ops *mig_ops;
40 	struct vfio_group *group;
41 	struct vfio_device_set *dev_set;
42 	struct list_head dev_set_list;
43 	unsigned int migration_flags;
44 	/* Driver must reference the kvm during open_device or never touch it */
45 	struct kvm *kvm;
46 
47 	/* Members below here are private, not for driver use */
48 	refcount_t refcount;
49 	unsigned int open_count;
50 	struct completion comp;
51 	struct list_head group_next;
52 };
53 
54 /**
55  * struct vfio_device_ops - VFIO bus driver device callbacks
56  *
57  * @open_device: Called when the first file descriptor is opened for this device
58  * @close_device: Opposite of open_device
59  * @read: Perform read(2) on device file descriptor
60  * @write: Perform write(2) on device file descriptor
61  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
62  *         operations documented below
63  * @mmap: Perform mmap(2) on a region of the device file descriptor
64  * @request: Request for the bus driver to release the device
65  * @match: Optional device name match callback (return: 0 for no-match, >0 for
66  *         match, -errno for abort (ex. match with insufficient or incorrect
67  *         additional args)
68  * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
69  */
70 struct vfio_device_ops {
71 	char	*name;
72 	int	(*open_device)(struct vfio_device *vdev);
73 	void	(*close_device)(struct vfio_device *vdev);
74 	ssize_t	(*read)(struct vfio_device *vdev, char __user *buf,
75 			size_t count, loff_t *ppos);
76 	ssize_t	(*write)(struct vfio_device *vdev, const char __user *buf,
77 			 size_t count, loff_t *size);
78 	long	(*ioctl)(struct vfio_device *vdev, unsigned int cmd,
79 			 unsigned long arg);
80 	int	(*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
81 	void	(*request)(struct vfio_device *vdev, unsigned int count);
82 	int	(*match)(struct vfio_device *vdev, char *buf);
83 	int	(*device_feature)(struct vfio_device *device, u32 flags,
84 				  void __user *arg, size_t argsz);
85 };
86 
87 /**
88  * @migration_set_state: Optional callback to change the migration state for
89  *         devices that support migration. It's mandatory for
90  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
91  *         The returned FD is used for data transfer according to the FSM
92  *         definition. The driver is responsible to ensure that FD reaches end
93  *         of stream or error whenever the migration FSM leaves a data transfer
94  *         state or before close_device() returns.
95  * @migration_get_state: Optional callback to get the migration state for
96  *         devices that support migration. It's mandatory for
97  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
98  */
99 struct vfio_migration_ops {
100 	struct file *(*migration_set_state)(
101 		struct vfio_device *device,
102 		enum vfio_device_mig_state new_state);
103 	int (*migration_get_state)(struct vfio_device *device,
104 				   enum vfio_device_mig_state *curr_state);
105 };
106 
107 /**
108  * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
109  * @flags: Arg from the device_feature op
110  * @argsz: Arg from the device_feature op
111  * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
112  *                 supports
113  * @minsz: Minimum data size the driver accepts
114  *
115  * For use in a driver's device_feature op. Checks that the inputs to the
116  * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
117  * the driver should execute the get or set, otherwise the relevant
118  * value should be returned.
119  */
vfio_check_feature(u32 flags,size_t argsz,u32 supported_ops,size_t minsz)120 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
121 				    size_t minsz)
122 {
123 	if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
124 	    ~supported_ops)
125 		return -EINVAL;
126 	if (flags & VFIO_DEVICE_FEATURE_PROBE)
127 		return 0;
128 	/* Without PROBE one of GET or SET must be requested */
129 	if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
130 		return -EINVAL;
131 	if (argsz < minsz)
132 		return -EINVAL;
133 	return 1;
134 }
135 
136 void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
137 			 const struct vfio_device_ops *ops);
138 void vfio_uninit_group_dev(struct vfio_device *device);
139 int vfio_register_group_dev(struct vfio_device *device);
140 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
141 void vfio_unregister_group_dev(struct vfio_device *device);
142 
143 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
144 
145 int vfio_mig_get_next_state(struct vfio_device *device,
146 			    enum vfio_device_mig_state cur_fsm,
147 			    enum vfio_device_mig_state new_fsm,
148 			    enum vfio_device_mig_state *next_fsm);
149 
150 /*
151  * External user API
152  */
153 extern struct iommu_group *vfio_file_iommu_group(struct file *file);
154 extern bool vfio_file_enforced_coherent(struct file *file);
155 extern void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
156 extern bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
157 
158 #define VFIO_PIN_PAGES_MAX_ENTRIES	(PAGE_SIZE/sizeof(unsigned long))
159 
160 extern int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
161 			  int npage, int prot, unsigned long *phys_pfn);
162 extern int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
163 			    int npage);
164 extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
165 		       void *data, size_t len, bool write);
166 
167 /* each type has independent events */
168 enum vfio_notify_type {
169 	VFIO_IOMMU_NOTIFY = 0,
170 };
171 
172 /* events for VFIO_IOMMU_NOTIFY */
173 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP	BIT(0)
174 
175 extern int vfio_register_notifier(struct vfio_device *device,
176 				  enum vfio_notify_type type,
177 				  unsigned long *required_events,
178 				  struct notifier_block *nb);
179 extern int vfio_unregister_notifier(struct vfio_device *device,
180 				    enum vfio_notify_type type,
181 				    struct notifier_block *nb);
182 
183 
184 /*
185  * Sub-module helpers
186  */
187 struct vfio_info_cap {
188 	struct vfio_info_cap_header *buf;
189 	size_t size;
190 };
191 extern struct vfio_info_cap_header *vfio_info_cap_add(
192 		struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
193 extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
194 
195 extern int vfio_info_add_capability(struct vfio_info_cap *caps,
196 				    struct vfio_info_cap_header *cap,
197 				    size_t size);
198 
199 extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
200 					      int num_irqs, int max_irq_type,
201 					      size_t *data_size);
202 
203 struct pci_dev;
204 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
205 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
206 extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
207 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
208 				       unsigned int cmd,
209 				       unsigned long arg);
210 #else
vfio_spapr_pci_eeh_open(struct pci_dev * pdev)211 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
212 {
213 }
214 
vfio_spapr_pci_eeh_release(struct pci_dev * pdev)215 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
216 {
217 }
218 
vfio_spapr_iommu_eeh_ioctl(struct iommu_group * group,unsigned int cmd,unsigned long arg)219 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
220 					      unsigned int cmd,
221 					      unsigned long arg)
222 {
223 	return -ENOTTY;
224 }
225 #endif /* CONFIG_VFIO_SPAPR_EEH */
226 
227 /*
228  * IRQfd - generic
229  */
230 struct virqfd {
231 	void			*opaque;
232 	struct eventfd_ctx	*eventfd;
233 	int			(*handler)(void *, void *);
234 	void			(*thread)(void *, void *);
235 	void			*data;
236 	struct work_struct	inject;
237 	wait_queue_entry_t		wait;
238 	poll_table		pt;
239 	struct work_struct	shutdown;
240 	struct virqfd		**pvirqfd;
241 };
242 
243 extern int vfio_virqfd_enable(void *opaque,
244 			      int (*handler)(void *, void *),
245 			      void (*thread)(void *, void *),
246 			      void *data, struct virqfd **pvirqfd, int fd);
247 extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
248 
249 #endif /* VFIO_H */
250