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>
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.
25 struct vfio_device_set {
28 struct list_head device_list;
29 unsigned int device_count;
34 const struct vfio_device_ops *ops;
35 struct vfio_group *group;
36 struct vfio_device_set *dev_set;
37 struct list_head dev_set_list;
38 unsigned int migration_flags;
39 /* Driver must reference the kvm during open_device or never touch it */
42 /* Members below here are private, not for driver use */
44 unsigned int open_count;
45 struct completion comp;
46 struct list_head group_next;
50 * struct vfio_device_ops - VFIO bus driver device callbacks
52 * @open_device: Called when the first file descriptor is opened for this device
53 * @close_device: Opposite of open_device
54 * @read: Perform read(2) on device file descriptor
55 * @write: Perform write(2) on device file descriptor
56 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
57 * operations documented below
58 * @mmap: Perform mmap(2) on a region of the device file descriptor
59 * @request: Request for the bus driver to release the device
60 * @match: Optional device name match callback (return: 0 for no-match, >0 for
61 * match, -errno for abort (ex. match with insufficient or incorrect
63 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
64 * @migration_set_state: Optional callback to change the migration state for
65 * devices that support migration. It's mandatory for
66 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
67 * The returned FD is used for data transfer according to the FSM
68 * definition. The driver is responsible to ensure that FD reaches end
69 * of stream or error whenever the migration FSM leaves a data transfer
70 * state or before close_device() returns.
71 * @migration_get_state: Optional callback to get the migration state for
72 * devices that support migration. It's mandatory for
73 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
75 struct vfio_device_ops {
77 int (*open_device)(struct vfio_device *vdev);
78 void (*close_device)(struct vfio_device *vdev);
79 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
80 size_t count, loff_t *ppos);
81 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
82 size_t count, loff_t *size);
83 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
85 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
86 void (*request)(struct vfio_device *vdev, unsigned int count);
87 int (*match)(struct vfio_device *vdev, char *buf);
88 int (*device_feature)(struct vfio_device *device, u32 flags,
89 void __user *arg, size_t argsz);
90 struct file *(*migration_set_state)(
91 struct vfio_device *device,
92 enum vfio_device_mig_state new_state);
93 int (*migration_get_state)(struct vfio_device *device,
94 enum vfio_device_mig_state *curr_state);
98 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
99 * @flags: Arg from the device_feature op
100 * @argsz: Arg from the device_feature op
101 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
103 * @minsz: Minimum data size the driver accepts
105 * For use in a driver's device_feature op. Checks that the inputs to the
106 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
107 * the driver should execute the get or set, otherwise the relevant
108 * value should be returned.
110 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
113 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
116 if (flags & VFIO_DEVICE_FEATURE_PROBE)
118 /* Without PROBE one of GET or SET must be requested */
119 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
126 void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
127 const struct vfio_device_ops *ops);
128 void vfio_uninit_group_dev(struct vfio_device *device);
129 int vfio_register_group_dev(struct vfio_device *device);
130 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
131 void vfio_unregister_group_dev(struct vfio_device *device);
133 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
135 int vfio_mig_get_next_state(struct vfio_device *device,
136 enum vfio_device_mig_state cur_fsm,
137 enum vfio_device_mig_state new_fsm,
138 enum vfio_device_mig_state *next_fsm);
143 extern struct iommu_group *vfio_file_iommu_group(struct file *file);
144 extern bool vfio_file_enforced_coherent(struct file *file);
145 extern void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
146 extern bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
148 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
150 extern int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
151 int npage, int prot, unsigned long *phys_pfn);
152 extern int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
154 extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
155 void *data, size_t len, bool write);
157 /* each type has independent events */
158 enum vfio_notify_type {
159 VFIO_IOMMU_NOTIFY = 0,
162 /* events for VFIO_IOMMU_NOTIFY */
163 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
165 extern int vfio_register_notifier(struct vfio_device *device,
166 enum vfio_notify_type type,
167 unsigned long *required_events,
168 struct notifier_block *nb);
169 extern int vfio_unregister_notifier(struct vfio_device *device,
170 enum vfio_notify_type type,
171 struct notifier_block *nb);
177 struct vfio_info_cap {
178 struct vfio_info_cap_header *buf;
181 extern struct vfio_info_cap_header *vfio_info_cap_add(
182 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
183 extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
185 extern int vfio_info_add_capability(struct vfio_info_cap *caps,
186 struct vfio_info_cap_header *cap,
189 extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
190 int num_irqs, int max_irq_type,
194 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
195 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
196 extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
197 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
201 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
205 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
209 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
215 #endif /* CONFIG_VFIO_SPAPR_EEH */
222 struct eventfd_ctx *eventfd;
223 int (*handler)(void *, void *);
224 void (*thread)(void *, void *);
226 struct work_struct inject;
227 wait_queue_entry_t wait;
229 struct work_struct shutdown;
230 struct virqfd **pvirqfd;
233 extern int vfio_virqfd_enable(void *opaque,
234 int (*handler)(void *, void *),
235 void (*thread)(void *, void *),
236 void *data, struct virqfd **pvirqfd, int fd);
237 extern void vfio_virqfd_disable(struct virqfd **pvirqfd);