vfio-iommufd: Support iommufd for physical VFIO devices
[linux-block.git] / include / linux / vfio.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
cba3345c
AW
2/*
3 * VFIO API definition
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
cba3345c
AW
7 */
8#ifndef VFIO_H
9#define VFIO_H
10
cba3345c
AW
11
12#include <linux/iommu.h>
13#include <linux/mm.h>
7e992d69
AM
14#include <linux/workqueue.h>
15#include <linux/poll.h>
607ca46e 16#include <uapi/linux/vfio.h>
80c4b92a 17#include <linux/iova_bitmap.h>
cba3345c 18
ba70a89f 19struct kvm;
a4d1f91d
JG
20struct iommufd_ctx;
21struct iommufd_device;
ba70a89f 22
2fd585f4
JG
23/*
24 * VFIO devices can be placed in a set, this allows all devices to share this
25 * structure and the VFIO core will provide a lock that is held around
26 * open_device()/close_device() for all devices in the set.
27 */
28struct vfio_device_set {
29 void *set_id;
30 struct mutex lock;
31 struct list_head device_list;
32 unsigned int device_count;
33};
34
0bfc6a4e
JG
35struct vfio_device {
36 struct device *dev;
37 const struct vfio_device_ops *ops;
6e97eba8 38 /*
80c4b92a
YH
39 * mig_ops/log_ops is a static property of the vfio_device which must
40 * be set prior to registering the vfio_device.
6e97eba8
YH
41 */
42 const struct vfio_migration_ops *mig_ops;
80c4b92a 43 const struct vfio_log_ops *log_ops;
0bfc6a4e 44 struct vfio_group *group;
2fd585f4
JG
45 struct vfio_device_set *dev_set;
46 struct list_head dev_set_list;
8cb3d83b 47 unsigned int migration_flags;
421cfe65
MR
48 /* Driver must reference the kvm during open_device or never touch it */
49 struct kvm *kvm;
0bfc6a4e
JG
50
51 /* Members below here are private, not for driver use */
3c28a761
YL
52 unsigned int index;
53 struct device device; /* device.kref covers object life circle */
cb9ff3f3 54 refcount_t refcount; /* user count on registered device*/
2fd585f4 55 unsigned int open_count;
0bfc6a4e
JG
56 struct completion comp;
57 struct list_head group_next;
8cfc5b60 58 struct list_head iommu_entry;
a4d1f91d
JG
59#if IS_ENABLED(CONFIG_IOMMUFD)
60 struct iommufd_device *iommufd_device;
61 bool iommufd_attached;
62#endif
0bfc6a4e
JG
63};
64
cba3345c
AW
65/**
66 * struct vfio_device_ops - VFIO bus driver device callbacks
67 *
cb9ff3f3
KT
68 * @init: initialize private fields in device structure
69 * @release: Reclaim private fields in device structure
2fd585f4
JG
70 * @open_device: Called when the first file descriptor is opened for this device
71 * @close_device: Opposite of open_device
cba3345c
AW
72 * @read: Perform read(2) on device file descriptor
73 * @write: Perform write(2) on device file descriptor
74 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
75 * operations documented below
76 * @mmap: Perform mmap(2) on a region of the device file descriptor
13060b64 77 * @request: Request for the bus driver to release the device
5f3874c2
AW
78 * @match: Optional device name match callback (return: 0 for no-match, >0 for
79 * match, -errno for abort (ex. match with insufficient or incorrect
80 * additional args)
ce4b4657
JG
81 * @dma_unmap: Called when userspace unmaps IOVA from the container
82 * this device is attached to.
445ad495 83 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
cba3345c
AW
84 */
85struct vfio_device_ops {
86 char *name;
cb9ff3f3
KT
87 int (*init)(struct vfio_device *vdev);
88 void (*release)(struct vfio_device *vdev);
a4d1f91d
JG
89 int (*bind_iommufd)(struct vfio_device *vdev,
90 struct iommufd_ctx *ictx, u32 *out_device_id);
91 void (*unbind_iommufd)(struct vfio_device *vdev);
92 int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id);
2fd585f4
JG
93 int (*open_device)(struct vfio_device *vdev);
94 void (*close_device)(struct vfio_device *vdev);
6df62c5b 95 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
cba3345c 96 size_t count, loff_t *ppos);
6df62c5b 97 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
cba3345c 98 size_t count, loff_t *size);
6df62c5b 99 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
cba3345c 100 unsigned long arg);
6df62c5b
JG
101 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
102 void (*request)(struct vfio_device *vdev, unsigned int count);
103 int (*match)(struct vfio_device *vdev, char *buf);
ce4b4657 104 void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
445ad495
JG
105 int (*device_feature)(struct vfio_device *device, u32 flags,
106 void __user *arg, size_t argsz);
6e97eba8
YH
107};
108
a4d1f91d
JG
109#if IS_ENABLED(CONFIG_IOMMUFD)
110int vfio_iommufd_physical_bind(struct vfio_device *vdev,
111 struct iommufd_ctx *ictx, u32 *out_device_id);
112void vfio_iommufd_physical_unbind(struct vfio_device *vdev);
113int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
114#else
115#define vfio_iommufd_physical_bind \
116 ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \
117 u32 *out_device_id)) NULL)
118#define vfio_iommufd_physical_unbind \
119 ((void (*)(struct vfio_device *vdev)) NULL)
120#define vfio_iommufd_physical_attach_ioas \
121 ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
122#endif
123
6e97eba8
YH
124/**
125 * @migration_set_state: Optional callback to change the migration state for
126 * devices that support migration. It's mandatory for
127 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
128 * The returned FD is used for data transfer according to the FSM
129 * definition. The driver is responsible to ensure that FD reaches end
130 * of stream or error whenever the migration FSM leaves a data transfer
131 * state or before close_device() returns.
132 * @migration_get_state: Optional callback to get the migration state for
133 * devices that support migration. It's mandatory for
134 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
135 */
136struct vfio_migration_ops {
115dcec6
JG
137 struct file *(*migration_set_state)(
138 struct vfio_device *device,
139 enum vfio_device_mig_state new_state);
140 int (*migration_get_state)(struct vfio_device *device,
141 enum vfio_device_mig_state *curr_state);
cba3345c
AW
142};
143
80c4b92a
YH
144/**
145 * @log_start: Optional callback to ask the device start DMA logging.
146 * @log_stop: Optional callback to ask the device stop DMA logging.
147 * @log_read_and_clear: Optional callback to ask the device read
148 * and clear the dirty DMAs in some given range.
149 *
150 * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set
151 * of features does not track logging state relative to the device,
152 * therefore the device implementation of vfio_log_ops must handle
153 * arbitrary user requests. This includes rejecting subsequent calls
154 * to log_start without an intervening log_stop, as well as graceful
155 * handling of log_stop and log_read_and_clear from invalid states.
156 */
157struct vfio_log_ops {
158 int (*log_start)(struct vfio_device *device,
159 struct rb_root_cached *ranges, u32 nnodes, u64 *page_size);
160 int (*log_stop)(struct vfio_device *device);
161 int (*log_read_and_clear)(struct vfio_device *device,
162 unsigned long iova, unsigned long length,
163 struct iova_bitmap *dirty);
164};
165
445ad495
JG
166/**
167 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
168 * @flags: Arg from the device_feature op
169 * @argsz: Arg from the device_feature op
170 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
171 * supports
172 * @minsz: Minimum data size the driver accepts
173 *
174 * For use in a driver's device_feature op. Checks that the inputs to the
175 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
176 * the driver should execute the get or set, otherwise the relevant
177 * value should be returned.
178 */
179static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
180 size_t minsz)
181{
182 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
183 ~supported_ops)
184 return -EINVAL;
185 if (flags & VFIO_DEVICE_FEATURE_PROBE)
186 return 0;
187 /* Without PROBE one of GET or SET must be requested */
188 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
189 return -EINVAL;
190 if (argsz < minsz)
191 return -EINVAL;
192 return 1;
193}
194
cb9ff3f3
KT
195struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev,
196 const struct vfio_device_ops *ops);
197#define vfio_alloc_device(dev_struct, member, dev, ops) \
198 container_of(_vfio_alloc_device(sizeof(struct dev_struct) + \
199 BUILD_BUG_ON_ZERO(offsetof( \
200 struct dev_struct, member)), \
201 dev, ops), \
202 struct dev_struct, member)
203
204int vfio_init_device(struct vfio_device *device, struct device *dev,
205 const struct vfio_device_ops *ops);
206void vfio_free_device(struct vfio_device *device);
cb9ff3f3
KT
207static inline void vfio_put_device(struct vfio_device *device)
208{
3c28a761 209 put_device(&device->device);
cb9ff3f3
KT
210}
211
0bfc6a4e 212int vfio_register_group_dev(struct vfio_device *device);
c68ea0d0 213int vfio_register_emulated_iommu_dev(struct vfio_device *device);
0bfc6a4e 214void vfio_unregister_group_dev(struct vfio_device *device);
cba3345c 215
2fd585f4
JG
216int vfio_assign_device_set(struct vfio_device *device, void *set_id);
217
115dcec6
JG
218int vfio_mig_get_next_state(struct vfio_device *device,
219 enum vfio_device_mig_state cur_fsm,
220 enum vfio_device_mig_state new_fsm,
221 enum vfio_device_mig_state *next_fsm);
222
6cdd9782
AK
223/*
224 * External user API
225 */
d1877e63 226struct iommu_group *vfio_file_iommu_group(struct file *file);
4b22ef04 227bool vfio_file_is_group(struct file *file);
d1877e63
AW
228bool vfio_file_enforced_coherent(struct file *file);
229void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
230bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
6cdd9782 231
2169037d
KW
232#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
233
44abdd16 234int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
34a255e6 235 int npage, int prot, struct page **pages);
44abdd16 236void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
8561aa4f 237int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
d1877e63 238 void *data, size_t len, bool write);
8d46c0cc 239
d7a8d5ed
AW
240/*
241 * Sub-module helpers
242 */
243struct vfio_info_cap {
244 struct vfio_info_cap_header *buf;
245 size_t size;
246};
d1877e63
AW
247struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
248 size_t size, u16 id,
249 u16 version);
250void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
d7a8d5ed 251
d1877e63
AW
252int vfio_info_add_capability(struct vfio_info_cap *caps,
253 struct vfio_info_cap_header *cap, size_t size);
b3c0a866 254
d1877e63
AW
255int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
256 int num_irqs, int max_irq_type,
257 size_t *data_size);
c747f08a 258
92d18a68 259struct pci_dev;
bb67b496 260#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
d1877e63
AW
261void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
262void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
263long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, unsigned int cmd,
264 unsigned long arg);
1b69be5e 265#else
9b936c96 266static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
1b69be5e 267{
1b69be5e
GS
268}
269
270static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
271{
272}
273
274static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
275 unsigned int cmd,
276 unsigned long arg)
277{
278 return -ENOTTY;
279}
bb67b496 280#endif /* CONFIG_VFIO_SPAPR_EEH */
7e992d69
AM
281
282/*
283 * IRQfd - generic
284 */
285struct virqfd {
286 void *opaque;
287 struct eventfd_ctx *eventfd;
288 int (*handler)(void *, void *);
289 void (*thread)(void *, void *);
290 void *data;
291 struct work_struct inject;
ac6424b9 292 wait_queue_entry_t wait;
7e992d69
AM
293 poll_table pt;
294 struct work_struct shutdown;
295 struct virqfd **pvirqfd;
296};
297
d1877e63
AW
298int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
299 void (*thread)(void *, void *), void *data,
300 struct virqfd **pvirqfd, int fd);
301void vfio_virqfd_disable(struct virqfd **pvirqfd);
7e992d69 302
cba3345c 303#endif /* VFIO_H */