xref: /openbmc/linux/include/linux/vfio.h (revision b1a79260)
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 /**
19  * struct vfio_device_ops - VFIO bus driver device callbacks
20  *
21  * @open: Called when userspace creates new file descriptor for device
22  * @release: Called when userspace releases file descriptor for device
23  * @read: Perform read(2) on device file descriptor
24  * @write: Perform write(2) on device file descriptor
25  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
26  *         operations documented below
27  * @mmap: Perform mmap(2) on a region of the device file descriptor
28  * @request: Request for the bus driver to release the device
29  * @match: Optional device name match callback (return: 0 for no-match, >0 for
30  *         match, -errno for abort (ex. match with insufficient or incorrect
31  *         additional args)
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 	int	(*match)(void *device_data, char *buf);
46 };
47 
48 extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
49 extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
50 
51 extern int vfio_add_group_dev(struct device *dev,
52 			      const struct vfio_device_ops *ops,
53 			      void *device_data);
54 
55 extern void *vfio_del_group_dev(struct device *dev);
56 extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
57 extern void vfio_device_put(struct vfio_device *device);
58 extern void *vfio_device_data(struct vfio_device *device);
59 
60 /* events for the backend driver notify callback */
61 enum vfio_iommu_notify_type {
62 	VFIO_IOMMU_CONTAINER_CLOSE = 0,
63 };
64 
65 /**
66  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
67  */
68 struct vfio_iommu_driver_ops {
69 	char		*name;
70 	struct module	*owner;
71 	void		*(*open)(unsigned long arg);
72 	void		(*release)(void *iommu_data);
73 	ssize_t		(*read)(void *iommu_data, char __user *buf,
74 				size_t count, loff_t *ppos);
75 	ssize_t		(*write)(void *iommu_data, const char __user *buf,
76 				 size_t count, loff_t *size);
77 	long		(*ioctl)(void *iommu_data, unsigned int cmd,
78 				 unsigned long arg);
79 	int		(*mmap)(void *iommu_data, struct vm_area_struct *vma);
80 	int		(*attach_group)(void *iommu_data,
81 					struct iommu_group *group);
82 	void		(*detach_group)(void *iommu_data,
83 					struct iommu_group *group);
84 	int		(*pin_pages)(void *iommu_data,
85 				     struct iommu_group *group,
86 				     unsigned long *user_pfn,
87 				     int npage, int prot,
88 				     unsigned long *phys_pfn);
89 	int		(*unpin_pages)(void *iommu_data,
90 				       unsigned long *user_pfn, int npage);
91 	int		(*register_notifier)(void *iommu_data,
92 					     unsigned long *events,
93 					     struct notifier_block *nb);
94 	int		(*unregister_notifier)(void *iommu_data,
95 					       struct notifier_block *nb);
96 	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
97 				  void *data, size_t count, bool write);
98 	struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
99 						   struct iommu_group *group);
100 	void		(*notify)(void *iommu_data,
101 				  enum vfio_iommu_notify_type event);
102 };
103 
104 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
105 
106 extern void vfio_unregister_iommu_driver(
107 				const struct vfio_iommu_driver_ops *ops);
108 
109 /*
110  * External user API
111  */
112 extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
113 extern void vfio_group_put_external_user(struct vfio_group *group);
114 extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
115 								*dev);
116 extern bool vfio_external_group_match_file(struct vfio_group *group,
117 					   struct file *filep);
118 extern int vfio_external_user_iommu_id(struct vfio_group *group);
119 extern long vfio_external_check_extension(struct vfio_group *group,
120 					  unsigned long arg);
121 
122 #define VFIO_PIN_PAGES_MAX_ENTRIES	(PAGE_SIZE/sizeof(unsigned long))
123 
124 extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
125 			  int npage, int prot, unsigned long *phys_pfn);
126 extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
127 			    int npage);
128 
129 extern int vfio_group_pin_pages(struct vfio_group *group,
130 				unsigned long *user_iova_pfn, int npage,
131 				int prot, unsigned long *phys_pfn);
132 extern int vfio_group_unpin_pages(struct vfio_group *group,
133 				  unsigned long *user_iova_pfn, int npage);
134 
135 extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
136 		       void *data, size_t len, bool write);
137 
138 extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
139 
140 /* each type has independent events */
141 enum vfio_notify_type {
142 	VFIO_IOMMU_NOTIFY = 0,
143 	VFIO_GROUP_NOTIFY = 1,
144 };
145 
146 /* events for VFIO_IOMMU_NOTIFY */
147 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP	BIT(0)
148 
149 /* events for VFIO_GROUP_NOTIFY */
150 #define VFIO_GROUP_NOTIFY_SET_KVM	BIT(0)
151 
152 extern int vfio_register_notifier(struct device *dev,
153 				  enum vfio_notify_type type,
154 				  unsigned long *required_events,
155 				  struct notifier_block *nb);
156 extern int vfio_unregister_notifier(struct device *dev,
157 				    enum vfio_notify_type type,
158 				    struct notifier_block *nb);
159 
160 struct kvm;
161 extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
162 
163 /*
164  * Sub-module helpers
165  */
166 struct vfio_info_cap {
167 	struct vfio_info_cap_header *buf;
168 	size_t size;
169 };
170 extern struct vfio_info_cap_header *vfio_info_cap_add(
171 		struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
172 extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
173 
174 extern int vfio_info_add_capability(struct vfio_info_cap *caps,
175 				    struct vfio_info_cap_header *cap,
176 				    size_t size);
177 
178 extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
179 					      int num_irqs, int max_irq_type,
180 					      size_t *data_size);
181 
182 struct pci_dev;
183 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
184 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
185 extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
186 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
187 				       unsigned int cmd,
188 				       unsigned long arg);
189 #else
190 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
191 {
192 }
193 
194 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
195 {
196 }
197 
198 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
199 					      unsigned int cmd,
200 					      unsigned long arg)
201 {
202 	return -ENOTTY;
203 }
204 #endif /* CONFIG_VFIO_SPAPR_EEH */
205 
206 /*
207  * IRQfd - generic
208  */
209 struct virqfd {
210 	void			*opaque;
211 	struct eventfd_ctx	*eventfd;
212 	int			(*handler)(void *, void *);
213 	void			(*thread)(void *, void *);
214 	void			*data;
215 	struct work_struct	inject;
216 	wait_queue_entry_t		wait;
217 	poll_table		pt;
218 	struct work_struct	shutdown;
219 	struct virqfd		**pvirqfd;
220 };
221 
222 extern int vfio_virqfd_enable(void *opaque,
223 			      int (*handler)(void *, void *),
224 			      void (*thread)(void *, void *),
225 			      void *data, struct virqfd **pvirqfd, int fd);
226 extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
227 
228 #endif /* VFIO_H */
229