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