xref: /linux/drivers/vfio/vfio.h (revision 8b6f173a)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
4  *     Author: Alex Williamson <alex.williamson@redhat.com>
5  */
6 #ifndef __VFIO_VFIO_H__
7 #define __VFIO_VFIO_H__
8 
9 #include <linux/file.h>
10 #include <linux/device.h>
11 #include <linux/cdev.h>
12 #include <linux/module.h>
13 #include <linux/vfio.h>
14 
15 struct iommufd_ctx;
16 struct iommu_group;
17 struct vfio_container;
18 
19 struct vfio_device_file {
20 	struct vfio_device *device;
21 	struct vfio_group *group;
22 
23 	u8 access_granted;
24 	u32 devid; /* only valid when iommufd is valid */
25 	spinlock_t kvm_ref_lock; /* protect kvm field */
26 	struct kvm *kvm;
27 	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
28 };
29 
30 void vfio_device_put_registration(struct vfio_device *device);
31 bool vfio_device_try_get_registration(struct vfio_device *device);
32 int vfio_df_open(struct vfio_device_file *df);
33 void vfio_df_close(struct vfio_device_file *df);
34 struct vfio_device_file *
35 vfio_allocate_device_file(struct vfio_device *device);
36 
37 extern const struct file_operations vfio_device_fops;
38 
39 enum vfio_group_type {
40 	/*
41 	 * Physical device with IOMMU backing.
42 	 */
43 	VFIO_IOMMU,
44 
45 	/*
46 	 * Virtual device without IOMMU backing. The VFIO core fakes up an
47 	 * iommu_group as the iommu_group sysfs interface is part of the
48 	 * userspace ABI.  The user of these devices must not be able to
49 	 * directly trigger unmediated DMA.
50 	 */
51 	VFIO_EMULATED_IOMMU,
52 
53 	/*
54 	 * Physical device without IOMMU backing. The VFIO core fakes up an
55 	 * iommu_group as the iommu_group sysfs interface is part of the
56 	 * userspace ABI.  Users can trigger unmediated DMA by the device,
57 	 * usage is highly dangerous, requires an explicit opt-in and will
58 	 * taint the kernel.
59 	 */
60 	VFIO_NO_IOMMU,
61 };
62 
63 struct vfio_group {
64 	struct device 			dev;
65 	struct cdev			cdev;
66 	/*
67 	 * When drivers is non-zero a driver is attached to the struct device
68 	 * that provided the iommu_group and thus the iommu_group is a valid
69 	 * pointer. When drivers is 0 the driver is being detached. Once users
70 	 * reaches 0 then the iommu_group is invalid.
71 	 */
72 	refcount_t			drivers;
73 	unsigned int			container_users;
74 	struct iommu_group		*iommu_group;
75 	struct vfio_container		*container;
76 	struct list_head		device_list;
77 	struct mutex			device_lock;
78 	struct list_head		vfio_next;
79 #if IS_ENABLED(CONFIG_VFIO_CONTAINER)
80 	struct list_head		container_next;
81 #endif
82 	enum vfio_group_type		type;
83 	struct mutex			group_lock;
84 	struct kvm			*kvm;
85 	struct file			*opened_file;
86 	struct blocking_notifier_head	notifier;
87 	struct iommufd_ctx		*iommufd;
88 	spinlock_t			kvm_ref_lock;
89 	unsigned int			cdev_device_open_cnt;
90 };
91 
92 int vfio_device_block_group(struct vfio_device *device);
93 void vfio_device_unblock_group(struct vfio_device *device);
94 int vfio_device_set_group(struct vfio_device *device,
95 			  enum vfio_group_type type);
96 void vfio_device_remove_group(struct vfio_device *device);
97 void vfio_device_group_register(struct vfio_device *device);
98 void vfio_device_group_unregister(struct vfio_device *device);
99 int vfio_device_group_use_iommu(struct vfio_device *device);
100 void vfio_device_group_unuse_iommu(struct vfio_device *device);
101 void vfio_df_group_close(struct vfio_device_file *df);
102 struct vfio_group *vfio_group_from_file(struct file *file);
103 bool vfio_group_enforced_coherent(struct vfio_group *group);
104 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
105 bool vfio_device_has_container(struct vfio_device *device);
106 int __init vfio_group_init(void);
107 void vfio_group_cleanup(void);
108 
109 static inline bool vfio_device_is_noiommu(struct vfio_device *vdev)
110 {
111 	return IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&
112 	       vdev->group->type == VFIO_NO_IOMMU;
113 }
114 
115 #if IS_ENABLED(CONFIG_VFIO_CONTAINER)
116 /**
117  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
118  */
119 struct vfio_iommu_driver_ops {
120 	char		*name;
121 	struct module	*owner;
122 	void		*(*open)(unsigned long arg);
123 	void		(*release)(void *iommu_data);
124 	long		(*ioctl)(void *iommu_data, unsigned int cmd,
125 				 unsigned long arg);
126 	int		(*attach_group)(void *iommu_data,
127 					struct iommu_group *group,
128 					enum vfio_group_type);
129 	void		(*detach_group)(void *iommu_data,
130 					struct iommu_group *group);
131 	int		(*pin_pages)(void *iommu_data,
132 				     struct iommu_group *group,
133 				     dma_addr_t user_iova,
134 				     int npage, int prot,
135 				     struct page **pages);
136 	void		(*unpin_pages)(void *iommu_data,
137 				       dma_addr_t user_iova, int npage);
138 	void		(*register_device)(void *iommu_data,
139 					   struct vfio_device *vdev);
140 	void		(*unregister_device)(void *iommu_data,
141 					     struct vfio_device *vdev);
142 	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
143 				  void *data, size_t count, bool write);
144 	struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
145 						   struct iommu_group *group);
146 };
147 
148 struct vfio_iommu_driver {
149 	const struct vfio_iommu_driver_ops	*ops;
150 	struct list_head			vfio_next;
151 };
152 
153 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
154 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops);
155 
156 struct vfio_container *vfio_container_from_file(struct file *filep);
157 int vfio_group_use_container(struct vfio_group *group);
158 void vfio_group_unuse_container(struct vfio_group *group);
159 int vfio_container_attach_group(struct vfio_container *container,
160 				struct vfio_group *group);
161 void vfio_group_detach_container(struct vfio_group *group);
162 void vfio_device_container_register(struct vfio_device *device);
163 void vfio_device_container_unregister(struct vfio_device *device);
164 int vfio_device_container_pin_pages(struct vfio_device *device,
165 				    dma_addr_t iova, int npage,
166 				    int prot, struct page **pages);
167 void vfio_device_container_unpin_pages(struct vfio_device *device,
168 				       dma_addr_t iova, int npage);
169 int vfio_device_container_dma_rw(struct vfio_device *device,
170 				 dma_addr_t iova, void *data,
171 				 size_t len, bool write);
172 
173 int __init vfio_container_init(void);
174 void vfio_container_cleanup(void);
175 #else
176 static inline struct vfio_container *
177 vfio_container_from_file(struct file *filep)
178 {
179 	return NULL;
180 }
181 
182 static inline int vfio_group_use_container(struct vfio_group *group)
183 {
184 	return -EOPNOTSUPP;
185 }
186 
187 static inline void vfio_group_unuse_container(struct vfio_group *group)
188 {
189 }
190 
191 static inline int vfio_container_attach_group(struct vfio_container *container,
192 					      struct vfio_group *group)
193 {
194 	return -EOPNOTSUPP;
195 }
196 
197 static inline void vfio_group_detach_container(struct vfio_group *group)
198 {
199 }
200 
201 static inline void vfio_device_container_register(struct vfio_device *device)
202 {
203 }
204 
205 static inline void vfio_device_container_unregister(struct vfio_device *device)
206 {
207 }
208 
209 static inline int vfio_device_container_pin_pages(struct vfio_device *device,
210 						  dma_addr_t iova, int npage,
211 						  int prot, struct page **pages)
212 {
213 	return -EOPNOTSUPP;
214 }
215 
216 static inline void vfio_device_container_unpin_pages(struct vfio_device *device,
217 						     dma_addr_t iova, int npage)
218 {
219 }
220 
221 static inline int vfio_device_container_dma_rw(struct vfio_device *device,
222 					       dma_addr_t iova, void *data,
223 					       size_t len, bool write)
224 {
225 	return -EOPNOTSUPP;
226 }
227 
228 static inline int vfio_container_init(void)
229 {
230 	return 0;
231 }
232 static inline void vfio_container_cleanup(void)
233 {
234 }
235 #endif
236 
237 #if IS_ENABLED(CONFIG_IOMMUFD)
238 bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
239 					 struct iommufd_ctx *ictx);
240 int vfio_df_iommufd_bind(struct vfio_device_file *df);
241 void vfio_df_iommufd_unbind(struct vfio_device_file *df);
242 int vfio_iommufd_compat_attach_ioas(struct vfio_device *device,
243 				    struct iommufd_ctx *ictx);
244 #else
245 static inline bool
246 vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
247 				    struct iommufd_ctx *ictx)
248 {
249 	return false;
250 }
251 
252 static inline int vfio_df_iommufd_bind(struct vfio_device_file *fd)
253 {
254 	return -EOPNOTSUPP;
255 }
256 
257 static inline void vfio_df_iommufd_unbind(struct vfio_device_file *df)
258 {
259 }
260 
261 static inline int
262 vfio_iommufd_compat_attach_ioas(struct vfio_device *device,
263 				struct iommufd_ctx *ictx)
264 {
265 	return -EOPNOTSUPP;
266 }
267 #endif
268 
269 #if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
270 void vfio_init_device_cdev(struct vfio_device *device);
271 
272 static inline int vfio_device_add(struct vfio_device *device)
273 {
274 	/* cdev does not support noiommu device */
275 	if (vfio_device_is_noiommu(device))
276 		return device_add(&device->device);
277 	vfio_init_device_cdev(device);
278 	return cdev_device_add(&device->cdev, &device->device);
279 }
280 
281 static inline void vfio_device_del(struct vfio_device *device)
282 {
283 	if (vfio_device_is_noiommu(device))
284 		device_del(&device->device);
285 	else
286 		cdev_device_del(&device->cdev, &device->device);
287 }
288 
289 int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep);
290 int vfio_cdev_init(struct class *device_class);
291 void vfio_cdev_cleanup(void);
292 #else
293 static inline void vfio_init_device_cdev(struct vfio_device *device)
294 {
295 }
296 
297 static inline int vfio_device_add(struct vfio_device *device)
298 {
299 	return device_add(&device->device);
300 }
301 
302 static inline void vfio_device_del(struct vfio_device *device)
303 {
304 	device_del(&device->device);
305 }
306 
307 static inline int vfio_device_fops_cdev_open(struct inode *inode,
308 					     struct file *filep)
309 {
310 	return 0;
311 }
312 
313 static inline int vfio_cdev_init(struct class *device_class)
314 {
315 	return 0;
316 }
317 
318 static inline void vfio_cdev_cleanup(void)
319 {
320 }
321 #endif /* CONFIG_VFIO_DEVICE_CDEV */
322 
323 #if IS_ENABLED(CONFIG_VFIO_VIRQFD)
324 int __init vfio_virqfd_init(void);
325 void vfio_virqfd_exit(void);
326 #else
327 static inline int __init vfio_virqfd_init(void)
328 {
329 	return 0;
330 }
331 static inline void vfio_virqfd_exit(void)
332 {
333 }
334 #endif
335 
336 #ifdef CONFIG_VFIO_NOIOMMU
337 extern bool vfio_noiommu __read_mostly;
338 #else
339 enum { vfio_noiommu = false };
340 #endif
341 
342 #ifdef CONFIG_HAVE_KVM
343 void _vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm);
344 void vfio_device_put_kvm(struct vfio_device *device);
345 #else
346 static inline void _vfio_device_get_kvm_safe(struct vfio_device *device,
347 					     struct kvm *kvm)
348 {
349 }
350 
351 static inline void vfio_device_put_kvm(struct vfio_device *device)
352 {
353 }
354 #endif
355 
356 #endif
357