1 /* SPDX-License-Identifier: GPL-2.0-only */
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
12 #include <linux/iommu.h>
14 #include <linux/workqueue.h>
15 #include <linux/poll.h>
16 #include <uapi/linux/vfio.h>
19 * VFIO devices can be placed in a set, this allows all devices to share this
20 * structure and the VFIO core will provide a lock that is held around
21 * open_device()/close_device() for all devices in the set.
23 struct vfio_device_set {
26 struct list_head device_list;
27 unsigned int device_count;
32 const struct vfio_device_ops *ops;
33 struct vfio_group *group;
34 struct vfio_device_set *dev_set;
35 struct list_head dev_set_list;
36 unsigned int migration_flags;
38 /* Members below here are private, not for driver use */
40 unsigned int open_count;
41 struct completion comp;
42 struct list_head group_next;
46 * struct vfio_device_ops - VFIO bus driver device callbacks
48 * @open_device: Called when the first file descriptor is opened for this device
49 * @close_device: Opposite of open_device
50 * @read: Perform read(2) on device file descriptor
51 * @write: Perform write(2) on device file descriptor
52 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
53 * operations documented below
54 * @mmap: Perform mmap(2) on a region of the device file descriptor
55 * @request: Request for the bus driver to release the device
56 * @match: Optional device name match callback (return: 0 for no-match, >0 for
57 * match, -errno for abort (ex. match with insufficient or incorrect
59 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
60 * @migration_set_state: Optional callback to change the migration state for
61 * devices that support migration. It's mandatory for
62 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
63 * The returned FD is used for data transfer according to the FSM
64 * definition. The driver is responsible to ensure that FD reaches end
65 * of stream or error whenever the migration FSM leaves a data transfer
66 * state or before close_device() returns.
67 * @migration_get_state: Optional callback to get the migration state for
68 * devices that support migration. It's mandatory for
69 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
71 struct vfio_device_ops {
73 int (*open_device)(struct vfio_device *vdev);
74 void (*close_device)(struct vfio_device *vdev);
75 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
76 size_t count, loff_t *ppos);
77 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
78 size_t count, loff_t *size);
79 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
81 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
82 void (*request)(struct vfio_device *vdev, unsigned int count);
83 int (*match)(struct vfio_device *vdev, char *buf);
84 int (*device_feature)(struct vfio_device *device, u32 flags,
85 void __user *arg, size_t argsz);
86 struct file *(*migration_set_state)(
87 struct vfio_device *device,
88 enum vfio_device_mig_state new_state);
89 int (*migration_get_state)(struct vfio_device *device,
90 enum vfio_device_mig_state *curr_state);
94 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
95 * @flags: Arg from the device_feature op
96 * @argsz: Arg from the device_feature op
97 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
99 * @minsz: Minimum data size the driver accepts
101 * For use in a driver's device_feature op. Checks that the inputs to the
102 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
103 * the driver should execute the get or set, otherwise the relevant
104 * value should be returned.
106 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
109 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
112 if (flags & VFIO_DEVICE_FEATURE_PROBE)
114 /* Without PROBE one of GET or SET must be requested */
115 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
122 void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
123 const struct vfio_device_ops *ops);
124 void vfio_uninit_group_dev(struct vfio_device *device);
125 int vfio_register_group_dev(struct vfio_device *device);
126 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
127 void vfio_unregister_group_dev(struct vfio_device *device);
128 extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
129 extern void vfio_device_put(struct vfio_device *device);
131 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
133 int vfio_mig_get_next_state(struct vfio_device *device,
134 enum vfio_device_mig_state cur_fsm,
135 enum vfio_device_mig_state new_fsm,
136 enum vfio_device_mig_state *next_fsm);
141 extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
142 extern void vfio_group_put_external_user(struct vfio_group *group);
143 extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
145 extern bool vfio_external_group_match_file(struct vfio_group *group,
147 extern int vfio_external_user_iommu_id(struct vfio_group *group);
148 extern long vfio_external_check_extension(struct vfio_group *group,
151 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
153 extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
154 int npage, int prot, unsigned long *phys_pfn);
155 extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
158 extern int vfio_group_pin_pages(struct vfio_group *group,
159 unsigned long *user_iova_pfn, int npage,
160 int prot, unsigned long *phys_pfn);
161 extern int vfio_group_unpin_pages(struct vfio_group *group,
162 unsigned long *user_iova_pfn, int npage);
164 extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
165 void *data, size_t len, bool write);
167 extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
169 /* each type has independent events */
170 enum vfio_notify_type {
171 VFIO_IOMMU_NOTIFY = 0,
172 VFIO_GROUP_NOTIFY = 1,
175 /* events for VFIO_IOMMU_NOTIFY */
176 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
178 /* events for VFIO_GROUP_NOTIFY */
179 #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
181 extern int vfio_register_notifier(struct device *dev,
182 enum vfio_notify_type type,
183 unsigned long *required_events,
184 struct notifier_block *nb);
185 extern int vfio_unregister_notifier(struct device *dev,
186 enum vfio_notify_type type,
187 struct notifier_block *nb);
190 extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
195 struct vfio_info_cap {
196 struct vfio_info_cap_header *buf;
199 extern struct vfio_info_cap_header *vfio_info_cap_add(
200 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
201 extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
203 extern int vfio_info_add_capability(struct vfio_info_cap *caps,
204 struct vfio_info_cap_header *cap,
207 extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
208 int num_irqs, int max_irq_type,
212 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
213 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
214 extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
215 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
219 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
223 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
227 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
233 #endif /* CONFIG_VFIO_SPAPR_EEH */
240 struct eventfd_ctx *eventfd;
241 int (*handler)(void *, void *);
242 void (*thread)(void *, void *);
244 struct work_struct inject;
245 wait_queue_entry_t wait;
247 struct work_struct shutdown;
248 struct virqfd **pvirqfd;
251 extern int vfio_virqfd_enable(void *opaque,
252 int (*handler)(void *, void *),
253 void (*thread)(void *, void *),
254 void *data, struct virqfd **pvirqfd, int fd);
255 extern void vfio_virqfd_disable(struct virqfd **pvirqfd);