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>
20 const struct vfio_device_ops *ops;
21 struct vfio_group *group;
23 /* Members below here are private, not for driver use */
25 struct completion comp;
26 struct list_head group_next;
31 * struct vfio_device_ops - VFIO bus driver device callbacks
33 * @open: Called when userspace creates new file descriptor for device
34 * @release: Called when userspace releases file descriptor for device
35 * @read: Perform read(2) on device file descriptor
36 * @write: Perform write(2) on device file descriptor
37 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
38 * operations documented below
39 * @mmap: Perform mmap(2) on a region of the device file descriptor
40 * @request: Request for the bus driver to release the device
41 * @match: Optional device name match callback (return: 0 for no-match, >0 for
42 * match, -errno for abort (ex. match with insufficient or incorrect
45 struct vfio_device_ops {
47 int (*open)(struct vfio_device *vdev);
48 void (*release)(struct vfio_device *vdev);
49 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
50 size_t count, loff_t *ppos);
51 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
52 size_t count, loff_t *size);
53 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
55 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
56 void (*request)(struct vfio_device *vdev, unsigned int count);
57 int (*match)(struct vfio_device *vdev, char *buf);
60 extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
61 extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
63 void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
64 const struct vfio_device_ops *ops, void *device_data);
65 int vfio_register_group_dev(struct vfio_device *device);
66 void vfio_unregister_group_dev(struct vfio_device *device);
67 extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
68 extern void vfio_device_put(struct vfio_device *device);
69 extern void *vfio_device_data(struct vfio_device *device);
71 /* events for the backend driver notify callback */
72 enum vfio_iommu_notify_type {
73 VFIO_IOMMU_CONTAINER_CLOSE = 0,
77 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
79 struct vfio_iommu_driver_ops {
82 void *(*open)(unsigned long arg);
83 void (*release)(void *iommu_data);
84 ssize_t (*read)(void *iommu_data, char __user *buf,
85 size_t count, loff_t *ppos);
86 ssize_t (*write)(void *iommu_data, const char __user *buf,
87 size_t count, loff_t *size);
88 long (*ioctl)(void *iommu_data, unsigned int cmd,
90 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
91 int (*attach_group)(void *iommu_data,
92 struct iommu_group *group);
93 void (*detach_group)(void *iommu_data,
94 struct iommu_group *group);
95 int (*pin_pages)(void *iommu_data,
96 struct iommu_group *group,
97 unsigned long *user_pfn,
99 unsigned long *phys_pfn);
100 int (*unpin_pages)(void *iommu_data,
101 unsigned long *user_pfn, int npage);
102 int (*register_notifier)(void *iommu_data,
103 unsigned long *events,
104 struct notifier_block *nb);
105 int (*unregister_notifier)(void *iommu_data,
106 struct notifier_block *nb);
107 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
108 void *data, size_t count, bool write);
109 struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
110 struct iommu_group *group);
111 void (*notify)(void *iommu_data,
112 enum vfio_iommu_notify_type event);
115 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
117 extern void vfio_unregister_iommu_driver(
118 const struct vfio_iommu_driver_ops *ops);
123 extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
124 extern void vfio_group_put_external_user(struct vfio_group *group);
125 extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
127 extern bool vfio_external_group_match_file(struct vfio_group *group,
129 extern int vfio_external_user_iommu_id(struct vfio_group *group);
130 extern long vfio_external_check_extension(struct vfio_group *group,
133 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
135 extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
136 int npage, int prot, unsigned long *phys_pfn);
137 extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
140 extern int vfio_group_pin_pages(struct vfio_group *group,
141 unsigned long *user_iova_pfn, int npage,
142 int prot, unsigned long *phys_pfn);
143 extern int vfio_group_unpin_pages(struct vfio_group *group,
144 unsigned long *user_iova_pfn, int npage);
146 extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
147 void *data, size_t len, bool write);
149 extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
151 /* each type has independent events */
152 enum vfio_notify_type {
153 VFIO_IOMMU_NOTIFY = 0,
154 VFIO_GROUP_NOTIFY = 1,
157 /* events for VFIO_IOMMU_NOTIFY */
158 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
160 /* events for VFIO_GROUP_NOTIFY */
161 #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
163 extern int vfio_register_notifier(struct device *dev,
164 enum vfio_notify_type type,
165 unsigned long *required_events,
166 struct notifier_block *nb);
167 extern int vfio_unregister_notifier(struct device *dev,
168 enum vfio_notify_type type,
169 struct notifier_block *nb);
172 extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
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);