xref: /openbmc/linux/drivers/vfio/vfio.h (revision bdef2b78)
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 	refcount_t			users;
52 	struct completion		users_comp;
53 	unsigned int			container_users;
54 	struct iommu_group		*iommu_group;
55 	struct vfio_container		*container;
56 	struct list_head		device_list;
57 	struct mutex			device_lock;
58 	struct list_head		vfio_next;
59 	struct list_head		container_next;
60 	enum vfio_group_type		type;
61 	struct rw_semaphore		group_rwsem;
62 	struct kvm			*kvm;
63 	struct file			*opened_file;
64 	struct blocking_notifier_head	notifier;
65 };
66 
67 /* events for the backend driver notify callback */
68 enum vfio_iommu_notify_type {
69 	VFIO_IOMMU_CONTAINER_CLOSE = 0,
70 };
71 
72 /**
73  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
74  */
75 struct vfio_iommu_driver_ops {
76 	char		*name;
77 	struct module	*owner;
78 	void		*(*open)(unsigned long arg);
79 	void		(*release)(void *iommu_data);
80 	long		(*ioctl)(void *iommu_data, unsigned int cmd,
81 				 unsigned long arg);
82 	int		(*attach_group)(void *iommu_data,
83 					struct iommu_group *group,
84 					enum vfio_group_type);
85 	void		(*detach_group)(void *iommu_data,
86 					struct iommu_group *group);
87 	int		(*pin_pages)(void *iommu_data,
88 				     struct iommu_group *group,
89 				     dma_addr_t user_iova,
90 				     int npage, int prot,
91 				     struct page **pages);
92 	void		(*unpin_pages)(void *iommu_data,
93 				       dma_addr_t user_iova, int npage);
94 	void		(*register_device)(void *iommu_data,
95 					   struct vfio_device *vdev);
96 	void		(*unregister_device)(void *iommu_data,
97 					     struct vfio_device *vdev);
98 	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
99 				  void *data, size_t count, bool write);
100 	struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
101 						   struct iommu_group *group);
102 	void		(*notify)(void *iommu_data,
103 				  enum vfio_iommu_notify_type event);
104 };
105 
106 struct vfio_iommu_driver {
107 	const struct vfio_iommu_driver_ops	*ops;
108 	struct list_head			vfio_next;
109 };
110 
111 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
112 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops);
113 
114 bool vfio_assert_device_open(struct vfio_device *device);
115 
116 struct vfio_container *vfio_container_from_file(struct file *filep);
117 int vfio_device_assign_container(struct vfio_device *device);
118 void vfio_device_unassign_container(struct vfio_device *device);
119 int vfio_container_attach_group(struct vfio_container *container,
120 				struct vfio_group *group);
121 void vfio_group_detach_container(struct vfio_group *group);
122 void vfio_device_container_register(struct vfio_device *device);
123 void vfio_device_container_unregister(struct vfio_device *device);
124 long vfio_container_ioctl_check_extension(struct vfio_container *container,
125 					  unsigned long arg);
126 int __init vfio_container_init(void);
127 void vfio_container_cleanup(void);
128 
129 #ifdef CONFIG_VFIO_NOIOMMU
130 extern bool vfio_noiommu __read_mostly;
131 #else
132 enum { vfio_noiommu = false };
133 #endif
134 
135 #endif
136