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,
22 struct iommufd_ctx *iommufd, struct kvm *kvm);
23 void vfio_device_close(struct vfio_device *device,
24 struct iommufd_ctx *iommufd);
26 extern const struct file_operations vfio_device_fops;
28 enum vfio_group_type {
30 * Physical device with IOMMU backing.
35 * Virtual device without IOMMU backing. The VFIO core fakes up an
36 * iommu_group as the iommu_group sysfs interface is part of the
37 * userspace ABI. The user of these devices must not be able to
38 * directly trigger unmediated DMA.
43 * Physical device without IOMMU backing. The VFIO core fakes up an
44 * iommu_group as the iommu_group sysfs interface is part of the
45 * userspace ABI. Users can trigger unmediated DMA by the device,
46 * usage is highly dangerous, requires an explicit opt-in and will
56 * When drivers is non-zero a driver is attached to the struct device
57 * that provided the iommu_group and thus the iommu_group is a valid
58 * pointer. When drivers is 0 the driver is being detached. Once users
59 * reaches 0 then the iommu_group is invalid.
62 unsigned int container_users;
63 struct iommu_group *iommu_group;
64 struct vfio_container *container;
65 struct list_head device_list;
66 struct mutex device_lock;
67 struct list_head vfio_next;
68 #if IS_ENABLED(CONFIG_VFIO_CONTAINER)
69 struct list_head container_next;
71 enum vfio_group_type type;
72 struct mutex group_lock;
74 struct file *opened_file;
75 struct blocking_notifier_head notifier;
76 struct iommufd_ctx *iommufd;
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)
92 /* events for the backend driver notify callback */
93 enum vfio_iommu_notify_type {
94 VFIO_IOMMU_CONTAINER_CLOSE = 0,
98 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
100 struct vfio_iommu_driver_ops {
102 struct module *owner;
103 void *(*open)(unsigned long arg);
104 void (*release)(void *iommu_data);
105 long (*ioctl)(void *iommu_data, unsigned int cmd,
107 int (*attach_group)(void *iommu_data,
108 struct iommu_group *group,
109 enum vfio_group_type);
110 void (*detach_group)(void *iommu_data,
111 struct iommu_group *group);
112 int (*pin_pages)(void *iommu_data,
113 struct iommu_group *group,
114 dma_addr_t user_iova,
116 struct page **pages);
117 void (*unpin_pages)(void *iommu_data,
118 dma_addr_t user_iova, int npage);
119 void (*register_device)(void *iommu_data,
120 struct vfio_device *vdev);
121 void (*unregister_device)(void *iommu_data,
122 struct vfio_device *vdev);
123 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
124 void *data, size_t count, bool write);
125 struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
126 struct iommu_group *group);
127 void (*notify)(void *iommu_data,
128 enum vfio_iommu_notify_type event);
131 struct vfio_iommu_driver {
132 const struct vfio_iommu_driver_ops *ops;
133 struct list_head vfio_next;
136 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
137 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops);
139 struct vfio_container *vfio_container_from_file(struct file *filep);
140 int vfio_group_use_container(struct vfio_group *group);
141 void vfio_group_unuse_container(struct vfio_group *group);
142 int vfio_container_attach_group(struct vfio_container *container,
143 struct vfio_group *group);
144 void vfio_group_detach_container(struct vfio_group *group);
145 void vfio_device_container_register(struct vfio_device *device);
146 void vfio_device_container_unregister(struct vfio_device *device);
147 int vfio_device_container_pin_pages(struct vfio_device *device,
148 dma_addr_t iova, int npage,
149 int prot, struct page **pages);
150 void vfio_device_container_unpin_pages(struct vfio_device *device,
151 dma_addr_t iova, int npage);
152 int vfio_device_container_dma_rw(struct vfio_device *device,
153 dma_addr_t iova, void *data,
154 size_t len, bool write);
156 int __init vfio_container_init(void);
157 void vfio_container_cleanup(void);
159 static inline struct vfio_container *
160 vfio_container_from_file(struct file *filep)
165 static inline int vfio_group_use_container(struct vfio_group *group)
170 static inline void vfio_group_unuse_container(struct vfio_group *group)
174 static inline int vfio_container_attach_group(struct vfio_container *container,
175 struct vfio_group *group)
180 static inline void vfio_group_detach_container(struct vfio_group *group)
184 static inline void vfio_device_container_register(struct vfio_device *device)
188 static inline void vfio_device_container_unregister(struct vfio_device *device)
192 static inline int vfio_device_container_pin_pages(struct vfio_device *device,
193 dma_addr_t iova, int npage,
194 int prot, struct page **pages)
199 static inline void vfio_device_container_unpin_pages(struct vfio_device *device,
200 dma_addr_t iova, int npage)
204 static inline int vfio_device_container_dma_rw(struct vfio_device *device,
205 dma_addr_t iova, void *data,
206 size_t len, bool write)
211 static inline int vfio_container_init(void)
215 static inline void vfio_container_cleanup(void)
220 #if IS_ENABLED(CONFIG_IOMMUFD)
221 int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
222 void vfio_iommufd_unbind(struct vfio_device *device);
224 static inline int vfio_iommufd_bind(struct vfio_device *device,
225 struct iommufd_ctx *ictx)
230 static inline void vfio_iommufd_unbind(struct vfio_device *device)
235 #if IS_ENABLED(CONFIG_VFIO_VIRQFD)
236 int __init vfio_virqfd_init(void);
237 void vfio_virqfd_exit(void);
239 static inline int __init vfio_virqfd_init(void)
243 static inline void vfio_virqfd_exit(void)
248 #ifdef CONFIG_VFIO_NOIOMMU
249 extern bool vfio_noiommu __read_mostly;
251 enum { vfio_noiommu = false };