xref: /linux-6.15/include/linux/vfio.h (revision bb970707)
1 /*
2  * VFIO API definition
3  *
4  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
5  *     Author: Alex Williamson <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #ifndef VFIO_H
12 #define VFIO_H
13 
14 
15 #include <linux/iommu.h>
16 #include <linux/mm.h>
17 #include <linux/workqueue.h>
18 #include <linux/poll.h>
19 #include <uapi/linux/vfio.h>
20 
21 /**
22  * struct vfio_device_ops - VFIO bus driver device callbacks
23  *
24  * @open: Called when userspace creates new file descriptor for device
25  * @release: Called when userspace releases file descriptor for device
26  * @read: Perform read(2) on device file descriptor
27  * @write: Perform write(2) on device file descriptor
28  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
29  *         operations documented below
30  * @mmap: Perform mmap(2) on a region of the device file descriptor
31  * @request: Request for the bus driver to release the device
32  */
33 struct vfio_device_ops {
34 	char	*name;
35 	int	(*open)(void *device_data);
36 	void	(*release)(void *device_data);
37 	ssize_t	(*read)(void *device_data, char __user *buf,
38 			size_t count, loff_t *ppos);
39 	ssize_t	(*write)(void *device_data, const char __user *buf,
40 			 size_t count, loff_t *size);
41 	long	(*ioctl)(void *device_data, unsigned int cmd,
42 			 unsigned long arg);
43 	int	(*mmap)(void *device_data, struct vm_area_struct *vma);
44 	void	(*request)(void *device_data, unsigned int count);
45 };
46 
47 extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
48 extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
49 
50 extern int vfio_add_group_dev(struct device *dev,
51 			      const struct vfio_device_ops *ops,
52 			      void *device_data);
53 
54 extern void *vfio_del_group_dev(struct device *dev);
55 extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
56 extern void vfio_device_put(struct vfio_device *device);
57 extern void *vfio_device_data(struct vfio_device *device);
58 
59 /**
60  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
61  */
62 struct vfio_iommu_driver_ops {
63 	char		*name;
64 	struct module	*owner;
65 	void		*(*open)(unsigned long arg);
66 	void		(*release)(void *iommu_data);
67 	ssize_t		(*read)(void *iommu_data, char __user *buf,
68 				size_t count, loff_t *ppos);
69 	ssize_t		(*write)(void *iommu_data, const char __user *buf,
70 				 size_t count, loff_t *size);
71 	long		(*ioctl)(void *iommu_data, unsigned int cmd,
72 				 unsigned long arg);
73 	int		(*mmap)(void *iommu_data, struct vm_area_struct *vma);
74 	int		(*attach_group)(void *iommu_data,
75 					struct iommu_group *group);
76 	void		(*detach_group)(void *iommu_data,
77 					struct iommu_group *group);
78 
79 };
80 
81 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
82 
83 extern void vfio_unregister_iommu_driver(
84 				const struct vfio_iommu_driver_ops *ops);
85 
86 /*
87  * External user API
88  */
89 extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
90 extern void vfio_group_put_external_user(struct vfio_group *group);
91 extern int vfio_external_user_iommu_id(struct vfio_group *group);
92 extern long vfio_external_check_extension(struct vfio_group *group,
93 					  unsigned long arg);
94 
95 /*
96  * Sub-module helpers
97  */
98 struct vfio_info_cap {
99 	struct vfio_info_cap_header *buf;
100 	size_t size;
101 };
102 extern struct vfio_info_cap_header *vfio_info_cap_add(
103 		struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
104 extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
105 
106 struct pci_dev;
107 #ifdef CONFIG_EEH
108 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
109 extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
110 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
111 				       unsigned int cmd,
112 				       unsigned long arg);
113 #else
114 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
115 {
116 }
117 
118 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
119 {
120 }
121 
122 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
123 					      unsigned int cmd,
124 					      unsigned long arg)
125 {
126 	return -ENOTTY;
127 }
128 #endif /* CONFIG_EEH */
129 
130 /*
131  * IRQfd - generic
132  */
133 struct virqfd {
134 	void			*opaque;
135 	struct eventfd_ctx	*eventfd;
136 	int			(*handler)(void *, void *);
137 	void			(*thread)(void *, void *);
138 	void			*data;
139 	struct work_struct	inject;
140 	wait_queue_t		wait;
141 	poll_table		pt;
142 	struct work_struct	shutdown;
143 	struct virqfd		**pvirqfd;
144 };
145 
146 extern int vfio_virqfd_enable(void *opaque,
147 			      int (*handler)(void *, void *),
148 			      void (*thread)(void *, void *),
149 			      void *data, struct virqfd **pvirqfd, int fd);
150 extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
151 
152 #endif /* VFIO_H */
153