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);
129 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
131 int vfio_mig_get_next_state(struct vfio_device *device,
132 enum vfio_device_mig_state cur_fsm,
133 enum vfio_device_mig_state new_fsm,
134 enum vfio_device_mig_state *next_fsm);
139 extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
140 extern void vfio_group_put_external_user(struct vfio_group *group);
141 extern bool vfio_external_group_match_file(struct vfio_group *group,
143 extern int vfio_external_user_iommu_id(struct vfio_group *group);
144 extern long vfio_external_check_extension(struct vfio_group *group,
147 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
149 extern int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
150 int npage, int prot, unsigned long *phys_pfn);
151 extern int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
153 extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
154 void *data, size_t len, bool write);
156 /* each type has independent events */
157 enum vfio_notify_type {
158 VFIO_IOMMU_NOTIFY = 0,
159 VFIO_GROUP_NOTIFY = 1,
162 /* events for VFIO_IOMMU_NOTIFY */
163 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
165 /* events for VFIO_GROUP_NOTIFY */
166 #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
168 extern int vfio_register_notifier(struct vfio_device *device,
169 enum vfio_notify_type type,
170 unsigned long *required_events,
171 struct notifier_block *nb);
172 extern int vfio_unregister_notifier(struct vfio_device *device,
173 enum vfio_notify_type type,
174 struct notifier_block *nb);
177 extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
182 struct vfio_info_cap {
183 struct vfio_info_cap_header *buf;
186 extern struct vfio_info_cap_header *vfio_info_cap_add(
187 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
188 extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
190 extern int vfio_info_add_capability(struct vfio_info_cap *caps,
191 struct vfio_info_cap_header *cap,
194 extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
195 int num_irqs, int max_irq_type,
199 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
200 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
201 extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
202 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
206 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
210 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
214 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
220 #endif /* CONFIG_VFIO_SPAPR_EEH */
227 struct eventfd_ctx *eventfd;
228 int (*handler)(void *, void *);
229 void (*thread)(void *, void *);
231 struct work_struct inject;
232 wait_queue_entry_t wait;
234 struct work_struct shutdown;
235 struct virqfd **pvirqfd;
238 extern int vfio_virqfd_enable(void *opaque,
239 int (*handler)(void *, void *),
240 void (*thread)(void *, void *),
241 void *data, struct virqfd **pvirqfd, int fd);
242 extern void vfio_virqfd_disable(struct virqfd **pvirqfd);