• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
4  *     Author: Alex Williamson <alex.williamson@redhat.com>
5  */
6 #ifndef __VFIO_VFIO_H__
7 #define __VFIO_VFIO_H__
8 
9 #include <linux/device.h>
10 #include <linux/cdev.h>
11 #include <linux/module.h>
12 
13 struct iommu_group;
14 struct vfio_device;
15 struct vfio_container;
16 
17 enum vfio_group_type {
18 	/*
19 	 * Physical device with IOMMU backing.
20 	 */
21 	VFIO_IOMMU,
22 
23 	/*
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.
28 	 */
29 	VFIO_EMULATED_IOMMU,
30 
31 	/*
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
36 	 * taint the kernel.
37 	 */
38 	VFIO_NO_IOMMU,
39 };
40 
41 struct vfio_group {
42 	struct device 			dev;
43 	struct cdev			cdev;
44 	/*
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.
49 	 */
50 	refcount_t			drivers;
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;
60 	struct kvm			*kvm;
61 	struct file			*opened_file;
62 	struct blocking_notifier_head	notifier;
63 };
64 
65 /* events for the backend driver notify callback */
66 enum vfio_iommu_notify_type {
67 	VFIO_IOMMU_CONTAINER_CLOSE = 0,
68 };
69 
70 /**
71  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
72  */
73 struct vfio_iommu_driver_ops {
74 	char		*name;
75 	struct module	*owner;
76 	void		*(*open)(unsigned long arg);
77 	void		(*release)(void *iommu_data);
78 	long		(*ioctl)(void *iommu_data, unsigned int cmd,
79 				 unsigned long arg);
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,
87 				     dma_addr_t user_iova,
88 				     int npage, int prot,
89 				     struct page **pages);
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);
102 };
103 
104 struct vfio_iommu_driver {
105 	const struct vfio_iommu_driver_ops	*ops;
106 	struct list_head			vfio_next;
107 };
108 
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);
111 
112 bool vfio_assert_device_open(struct vfio_device *device);
113 
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,
123 					  unsigned long arg);
124 int __init vfio_container_init(void);
125 void vfio_container_cleanup(void);
126 
127 #ifdef CONFIG_VFIO_NOIOMMU
128 extern bool vfio_noiommu __read_mostly;
129 #else
130 enum { vfio_noiommu = false };
131 #endif
132 
133 #endif
134