1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
4 * Author: Alex Williamson <alex.williamson@redhat.com>
6 #ifndef __VFIO_VFIO_H__
7 #define __VFIO_VFIO_H__
9 #include <linux/file.h>
10 #include <linux/device.h>
11 #include <linux/cdev.h>
12 #include <linux/module.h>
17 struct vfio_container;
19 void vfio_device_put_registration(struct vfio_device *device);
20 bool vfio_device_try_get_registration(struct vfio_device *device);
21 int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd);
22 void vfio_device_close(struct vfio_device *device,
23 struct iommufd_ctx *iommufd);
25 extern const struct file_operations vfio_device_fops;
27 enum vfio_group_type {
29 * Physical device with IOMMU backing.
34 * Virtual device without IOMMU backing. The VFIO core fakes up an
35 * iommu_group as the iommu_group sysfs interface is part of the
36 * userspace ABI. The user of these devices must not be able to
37 * directly trigger unmediated DMA.
42 * Physical device without IOMMU backing. The VFIO core fakes up an
43 * iommu_group as the iommu_group sysfs interface is part of the
44 * userspace ABI. Users can trigger unmediated DMA by the device,
45 * usage is highly dangerous, requires an explicit opt-in and will
55 * When drivers is non-zero a driver is attached to the struct device
56 * that provided the iommu_group and thus the iommu_group is a valid
57 * pointer. When drivers is 0 the driver is being detached. Once users
58 * reaches 0 then the iommu_group is invalid.
61 unsigned int container_users;
62 struct iommu_group *iommu_group;
63 struct vfio_container *container;
64 struct list_head device_list;
65 struct mutex device_lock;
66 struct list_head vfio_next;
67 #if IS_ENABLED(CONFIG_VFIO_CONTAINER)
68 struct list_head container_next;
70 enum vfio_group_type type;
71 struct mutex group_lock;
73 struct file *opened_file;
74 struct blocking_notifier_head notifier;
75 struct iommufd_ctx *iommufd;
76 spinlock_t kvm_ref_lock;
79 int vfio_device_set_group(struct vfio_device *device,
80 enum vfio_group_type type);
81 void vfio_device_remove_group(struct vfio_device *device);
82 void vfio_device_group_register(struct vfio_device *device);
83 void vfio_device_group_unregister(struct vfio_device *device);
84 int vfio_device_group_use_iommu(struct vfio_device *device);
85 void vfio_device_group_unuse_iommu(struct vfio_device *device);
86 void vfio_device_group_close(struct vfio_device *device);
87 bool vfio_device_has_container(struct vfio_device *device);
88 int __init vfio_group_init(void);
89 void vfio_group_cleanup(void);
91 #if IS_ENABLED(CONFIG_VFIO_CONTAINER)
93 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
95 struct vfio_iommu_driver_ops {
98 void *(*open)(unsigned long arg);
99 void (*release)(void *iommu_data);
100 long (*ioctl)(void *iommu_data, unsigned int cmd,
102 int (*attach_group)(void *iommu_data,
103 struct iommu_group *group,
104 enum vfio_group_type);
105 void (*detach_group)(void *iommu_data,
106 struct iommu_group *group);
107 int (*pin_pages)(void *iommu_data,
108 struct iommu_group *group,
109 dma_addr_t user_iova,
111 struct page **pages);
112 void (*unpin_pages)(void *iommu_data,
113 dma_addr_t user_iova, int npage);
114 void (*register_device)(void *iommu_data,
115 struct vfio_device *vdev);
116 void (*unregister_device)(void *iommu_data,
117 struct vfio_device *vdev);
118 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
119 void *data, size_t count, bool write);
120 struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
121 struct iommu_group *group);
124 struct vfio_iommu_driver {
125 const struct vfio_iommu_driver_ops *ops;
126 struct list_head vfio_next;
129 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
130 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops);
132 struct vfio_container *vfio_container_from_file(struct file *filep);
133 int vfio_group_use_container(struct vfio_group *group);
134 void vfio_group_unuse_container(struct vfio_group *group);
135 int vfio_container_attach_group(struct vfio_container *container,
136 struct vfio_group *group);
137 void vfio_group_detach_container(struct vfio_group *group);
138 void vfio_device_container_register(struct vfio_device *device);
139 void vfio_device_container_unregister(struct vfio_device *device);
140 int vfio_device_container_pin_pages(struct vfio_device *device,
141 dma_addr_t iova, int npage,
142 int prot, struct page **pages);
143 void vfio_device_container_unpin_pages(struct vfio_device *device,
144 dma_addr_t iova, int npage);
145 int vfio_device_container_dma_rw(struct vfio_device *device,
146 dma_addr_t iova, void *data,
147 size_t len, bool write);
149 int __init vfio_container_init(void);
150 void vfio_container_cleanup(void);
152 static inline struct vfio_container *
153 vfio_container_from_file(struct file *filep)
158 static inline int vfio_group_use_container(struct vfio_group *group)
163 static inline void vfio_group_unuse_container(struct vfio_group *group)
167 static inline int vfio_container_attach_group(struct vfio_container *container,
168 struct vfio_group *group)
173 static inline void vfio_group_detach_container(struct vfio_group *group)
177 static inline void vfio_device_container_register(struct vfio_device *device)
181 static inline void vfio_device_container_unregister(struct vfio_device *device)
185 static inline int vfio_device_container_pin_pages(struct vfio_device *device,
186 dma_addr_t iova, int npage,
187 int prot, struct page **pages)
192 static inline void vfio_device_container_unpin_pages(struct vfio_device *device,
193 dma_addr_t iova, int npage)
197 static inline int vfio_device_container_dma_rw(struct vfio_device *device,
198 dma_addr_t iova, void *data,
199 size_t len, bool write)
204 static inline int vfio_container_init(void)
208 static inline void vfio_container_cleanup(void)
213 #if IS_ENABLED(CONFIG_IOMMUFD)
214 int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
215 void vfio_iommufd_unbind(struct vfio_device *device);
217 static inline int vfio_iommufd_bind(struct vfio_device *device,
218 struct iommufd_ctx *ictx)
223 static inline void vfio_iommufd_unbind(struct vfio_device *device)
228 #if IS_ENABLED(CONFIG_VFIO_VIRQFD)
229 int __init vfio_virqfd_init(void);
230 void vfio_virqfd_exit(void);
232 static inline int __init vfio_virqfd_init(void)
236 static inline void vfio_virqfd_exit(void)
241 #ifdef CONFIG_VFIO_NOIOMMU
242 extern bool vfio_noiommu __read_mostly;
244 enum { vfio_noiommu = false };
247 #ifdef CONFIG_HAVE_KVM
248 void _vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm);
249 void vfio_device_put_kvm(struct vfio_device *device);
251 static inline void _vfio_device_get_kvm_safe(struct vfio_device *device,
256 static inline void vfio_device_put_kvm(struct vfio_device *device)