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/device.h>
10 #include <linux/cdev.h>
11 #include <linux/module.h>
15 struct vfio_container;
17 enum vfio_group_type {
19 * Physical device with IOMMU backing.
24 * Virtual device without IOMMU backing. The VFIO core fakes up an
25 * iommu_group as the iommu_group sysfs interface is part of the
26 * userspace ABI. The user of these devices must not be able to
27 * directly trigger unmediated DMA.
32 * Physical device without IOMMU backing. The VFIO core fakes up an
33 * iommu_group as the iommu_group sysfs interface is part of the
34 * userspace ABI. Users can trigger unmediated DMA by the device,
35 * usage is highly dangerous, requires an explicit opt-in and will
45 * When drivers is non-zero a driver is attached to the struct device
46 * that provided the iommu_group and thus the iommu_group is a valid
47 * pointer. When drivers is 0 the driver is being detached. Once users
48 * reaches 0 then the iommu_group is invalid.
51 unsigned int container_users;
52 struct iommu_group *iommu_group;
53 struct vfio_container *container;
54 struct list_head device_list;
55 struct mutex device_lock;
56 struct list_head vfio_next;
57 struct list_head container_next;
58 enum vfio_group_type type;
59 struct mutex group_lock;
61 struct file *opened_file;
62 struct blocking_notifier_head notifier;
65 /* events for the backend driver notify callback */
66 enum vfio_iommu_notify_type {
67 VFIO_IOMMU_CONTAINER_CLOSE = 0,
71 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
73 struct vfio_iommu_driver_ops {
76 void *(*open)(unsigned long arg);
77 void (*release)(void *iommu_data);
78 long (*ioctl)(void *iommu_data, unsigned int cmd,
80 int (*attach_group)(void *iommu_data,
81 struct iommu_group *group,
82 enum vfio_group_type);
83 void (*detach_group)(void *iommu_data,
84 struct iommu_group *group);
85 int (*pin_pages)(void *iommu_data,
86 struct iommu_group *group,
90 void (*unpin_pages)(void *iommu_data,
91 dma_addr_t user_iova, int npage);
92 void (*register_device)(void *iommu_data,
93 struct vfio_device *vdev);
94 void (*unregister_device)(void *iommu_data,
95 struct vfio_device *vdev);
96 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
97 void *data, size_t count, bool write);
98 struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
99 struct iommu_group *group);
100 void (*notify)(void *iommu_data,
101 enum vfio_iommu_notify_type event);
104 struct vfio_iommu_driver {
105 const struct vfio_iommu_driver_ops *ops;
106 struct list_head vfio_next;
109 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
110 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops);
112 bool vfio_assert_device_open(struct vfio_device *device);
114 struct vfio_container *vfio_container_from_file(struct file *filep);
115 int vfio_device_assign_container(struct vfio_device *device);
116 void vfio_device_unassign_container(struct vfio_device *device);
117 int vfio_container_attach_group(struct vfio_container *container,
118 struct vfio_group *group);
119 void vfio_group_detach_container(struct vfio_group *group);
120 void vfio_device_container_register(struct vfio_device *device);
121 void vfio_device_container_unregister(struct vfio_device *device);
122 long vfio_container_ioctl_check_extension(struct vfio_container *container,
124 int __init vfio_container_init(void);
125 void vfio_container_cleanup(void);
127 #ifdef CONFIG_VFIO_NOIOMMU
128 extern bool vfio_noiommu __read_mostly;
130 enum { vfio_noiommu = false };