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