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