xref: /kvmtool/include/kvm/vfio.h (revision 0b5e55fc032d1c6394b8ec7fe02d842813c903df)
1 #ifndef KVM__VFIO_H
2 #define KVM__VFIO_H
3 
4 #include "kvm/mutex.h"
5 #include "kvm/parse-options.h"
6 #include "kvm/pci.h"
7 
8 #include <linux/vfio.h>
9 
10 #define vfio_dev_err(vdev, fmt, ...) \
11 	pr_err("%s: " fmt, (vdev)->params->name, ##__VA_ARGS__)
12 #define vfio_dev_warn(vdev, fmt, ...) \
13 	pr_warning("%s: " fmt, (vdev)->params->name, ##__VA_ARGS__)
14 #define vfio_dev_info(vdev, fmt, ...) \
15 	pr_info("%s: " fmt, (vdev)->params->name, ##__VA_ARGS__)
16 #define vfio_dev_dbg(vdev, fmt, ...) \
17 	pr_debug("%s: " fmt, (vdev)->params->name, ##__VA_ARGS__)
18 #define vfio_dev_die(vdev, fmt, ...) \
19 	die("%s: " fmt, (vdev)->params->name, ##__VA_ARGS__)
20 
21 /* Currently limited by num_vfio_devices */
22 #define MAX_VFIO_DEVICES		256
23 
24 enum vfio_device_type {
25 	VFIO_DEVICE_PCI,
26 };
27 
28 /* MSI/MSI-X capability enabled */
29 #define VFIO_PCI_MSI_STATE_ENABLED	(1 << 0)
30 /* MSI/MSI-X capability or individual vector masked */
31 #define VFIO_PCI_MSI_STATE_MASKED	(1 << 1)
32 /* MSI-X capability has no vector enabled yet */
33 #define VFIO_PCI_MSI_STATE_EMPTY	(1 << 2)
34 
35 struct vfio_pci_msi_entry {
36 	struct msix_table		config;
37 	int				gsi;
38 	int				eventfd;
39 	u8				guest_state;
40 	u8				host_state;
41 };
42 
43 struct vfio_pci_msix_table {
44 	size_t				size;
45 	unsigned int			bar;
46 	u32				guest_phys_addr;
47 };
48 
49 struct vfio_pci_msix_pba {
50 	size_t				size;
51 	off_t				fd_offset; /* in VFIO device fd */
52 	unsigned int			bar;
53 	u32				bar_offset; /* in the shared BAR */
54 	u32				guest_phys_addr;
55 };
56 
57 /* Common data for MSI and MSI-X */
58 struct vfio_pci_msi_common {
59 	off_t				pos;
60 	u8				guest_state;
61 	u8				host_state;
62 	struct mutex			mutex;
63 	struct vfio_irq_info		info;
64 	struct vfio_irq_set		*irq_set;
65 	size_t				nr_entries;
66 	struct vfio_pci_msi_entry	*entries;
67 };
68 
69 #define VFIO_PCI_IRQ_MODE_INTX		(1 << 0)
70 #define VFIO_PCI_IRQ_MODE_MSI		(1 << 1)
71 #define VFIO_PCI_IRQ_MODE_MSIX		(1 << 2)
72 
73 struct vfio_pci_device {
74 	struct pci_device_header	hdr;
75 
76 	unsigned long			irq_modes;
77 	int				intx_fd;
78 	int				unmask_fd;
79 	unsigned int			intx_gsi;
80 	struct vfio_pci_msi_common	msi;
81 	struct vfio_pci_msi_common	msix;
82 	struct vfio_pci_msix_table	msix_table;
83 	struct vfio_pci_msix_pba	msix_pba;
84 };
85 
86 struct vfio_region {
87 	struct vfio_region_info		info;
88 	struct vfio_device		*vdev;
89 	u64				guest_phys_addr;
90 	void				*host_addr;
91 	u32				port_base;
92 	int				is_ioport	:1;
93 };
94 
95 struct vfio_device {
96 	struct device_header		dev_hdr;
97 	struct vfio_device_params	*params;
98 	struct vfio_group		*group;
99 
100 	int				fd;
101 	struct vfio_device_info		info;
102 	struct vfio_region		*regions;
103 
104 	char				*sysfs_path;
105 
106 	struct vfio_pci_device		pci;
107 };
108 
109 struct vfio_device_params {
110 	char				*name;
111 	const char			*bus;
112 	enum vfio_device_type		type;
113 };
114 
115 struct vfio_group {
116 	unsigned long			id; /* iommu_group number in sysfs */
117 	int				fd;
118 	int				refs;
119 	struct list_head		list;
120 };
121 
122 int vfio_device_parser(const struct option *opt, const char *arg, int unset);
123 int vfio_map_region(struct kvm *kvm, struct vfio_device *vdev,
124 		    struct vfio_region *region);
125 void vfio_unmap_region(struct kvm *kvm, struct vfio_region *region);
126 int vfio_pci_setup_device(struct kvm *kvm, struct vfio_device *device);
127 void vfio_pci_teardown_device(struct kvm *kvm, struct vfio_device *vdev);
128 
129 #endif /* KVM__VFIO_H */
130