kernel.h: update comment about simple_strto<foo>() functions
[sfrench/cifs-2.6.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 /**
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  */
30 struct vfio_device_ops {
31         char    *name;
32         int     (*open)(void *device_data);
33         void    (*release)(void *device_data);
34         ssize_t (*read)(void *device_data, char __user *buf,
35                         size_t count, loff_t *ppos);
36         ssize_t (*write)(void *device_data, const char __user *buf,
37                          size_t count, loff_t *size);
38         long    (*ioctl)(void *device_data, unsigned int cmd,
39                          unsigned long arg);
40         int     (*mmap)(void *device_data, struct vm_area_struct *vma);
41         void    (*request)(void *device_data, unsigned int count);
42 };
43
44 extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
45 extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
46
47 extern int vfio_add_group_dev(struct device *dev,
48                               const struct vfio_device_ops *ops,
49                               void *device_data);
50
51 extern void *vfio_del_group_dev(struct device *dev);
52 extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
53 extern void vfio_device_put(struct vfio_device *device);
54 extern void *vfio_device_data(struct vfio_device *device);
55
56 /**
57  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
58  */
59 struct vfio_iommu_driver_ops {
60         char            *name;
61         struct module   *owner;
62         void            *(*open)(unsigned long arg);
63         void            (*release)(void *iommu_data);
64         ssize_t         (*read)(void *iommu_data, char __user *buf,
65                                 size_t count, loff_t *ppos);
66         ssize_t         (*write)(void *iommu_data, const char __user *buf,
67                                  size_t count, loff_t *size);
68         long            (*ioctl)(void *iommu_data, unsigned int cmd,
69                                  unsigned long arg);
70         int             (*mmap)(void *iommu_data, struct vm_area_struct *vma);
71         int             (*attach_group)(void *iommu_data,
72                                         struct iommu_group *group);
73         void            (*detach_group)(void *iommu_data,
74                                         struct iommu_group *group);
75         int             (*pin_pages)(void *iommu_data, unsigned long *user_pfn,
76                                      int npage, int prot,
77                                      unsigned long *phys_pfn);
78         int             (*unpin_pages)(void *iommu_data,
79                                        unsigned long *user_pfn, int npage);
80         int             (*register_notifier)(void *iommu_data,
81                                              unsigned long *events,
82                                              struct notifier_block *nb);
83         int             (*unregister_notifier)(void *iommu_data,
84                                                struct notifier_block *nb);
85 };
86
87 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
88
89 extern void vfio_unregister_iommu_driver(
90                                 const struct vfio_iommu_driver_ops *ops);
91
92 /*
93  * External user API
94  */
95 extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
96 extern void vfio_group_put_external_user(struct vfio_group *group);
97 extern bool vfio_external_group_match_file(struct vfio_group *group,
98                                            struct file *filep);
99 extern int vfio_external_user_iommu_id(struct vfio_group *group);
100 extern long vfio_external_check_extension(struct vfio_group *group,
101                                           unsigned long arg);
102
103 #define VFIO_PIN_PAGES_MAX_ENTRIES      (PAGE_SIZE/sizeof(unsigned long))
104
105 extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
106                           int npage, int prot, unsigned long *phys_pfn);
107 extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
108                             int npage);
109
110 /* each type has independent events */
111 enum vfio_notify_type {
112         VFIO_IOMMU_NOTIFY = 0,
113         VFIO_GROUP_NOTIFY = 1,
114 };
115
116 /* events for VFIO_IOMMU_NOTIFY */
117 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP     BIT(0)
118
119 /* events for VFIO_GROUP_NOTIFY */
120 #define VFIO_GROUP_NOTIFY_SET_KVM       BIT(0)
121
122 extern int vfio_register_notifier(struct device *dev,
123                                   enum vfio_notify_type type,
124                                   unsigned long *required_events,
125                                   struct notifier_block *nb);
126 extern int vfio_unregister_notifier(struct device *dev,
127                                     enum vfio_notify_type type,
128                                     struct notifier_block *nb);
129
130 struct kvm;
131 extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
132
133 /*
134  * Sub-module helpers
135  */
136 struct vfio_info_cap {
137         struct vfio_info_cap_header *buf;
138         size_t size;
139 };
140 extern struct vfio_info_cap_header *vfio_info_cap_add(
141                 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
142 extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
143
144 extern int vfio_info_add_capability(struct vfio_info_cap *caps,
145                                     struct vfio_info_cap_header *cap,
146                                     size_t size);
147
148 extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
149                                               int num_irqs, int max_irq_type,
150                                               size_t *data_size);
151
152 struct pci_dev;
153 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
154 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
155 extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
156 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
157                                        unsigned int cmd,
158                                        unsigned long arg);
159 #else
160 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
161 {
162 }
163
164 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
165 {
166 }
167
168 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
169                                               unsigned int cmd,
170                                               unsigned long arg)
171 {
172         return -ENOTTY;
173 }
174 #endif /* CONFIG_VFIO_SPAPR_EEH */
175
176 /*
177  * IRQfd - generic
178  */
179 struct virqfd {
180         void                    *opaque;
181         struct eventfd_ctx      *eventfd;
182         int                     (*handler)(void *, void *);
183         void                    (*thread)(void *, void *);
184         void                    *data;
185         struct work_struct      inject;
186         wait_queue_entry_t              wait;
187         poll_table              pt;
188         struct work_struct      shutdown;
189         struct virqfd           **pvirqfd;
190 };
191
192 extern int vfio_virqfd_enable(void *opaque,
193                               int (*handler)(void *, void *),
194                               void (*thread)(void *, void *),
195                               void *data, struct virqfd **pvirqfd, int fd);
196 extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
197
198 #endif /* VFIO_H */