xref: /qemu/include/hw/vfio/vfio-device.h (revision 499e53cce9445d23ee1bf54562de558562fc8d22)
1 /*
2  * common header for vfio based device assignment support
3  *
4  * Copyright Red Hat, Inc. 2012
5  *
6  * Authors:
7  *  Alex Williamson <alex.williamson@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  * Based on qemu-kvm device-assignment:
13  *  Adapted for KVM by Qumranet.
14  *  Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
15  *  Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
16  *  Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
17  *  Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
18  *  Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
19  */
20 
21 #ifndef HW_VFIO_VFIO_COMMON_H
22 #define HW_VFIO_VFIO_COMMON_H
23 
24 #include "system/memory.h"
25 #include "qemu/queue.h"
26 #ifdef CONFIG_LINUX
27 #include <linux/vfio.h>
28 #endif
29 #include "system/system.h"
30 #include "hw/vfio/vfio-container-base.h"
31 #include "system/host_iommu_device.h"
32 #include "system/iommufd.h"
33 
34 #define VFIO_MSG_PREFIX "vfio %s: "
35 
36 enum {
37     VFIO_DEVICE_TYPE_PCI = 0,
38     VFIO_DEVICE_TYPE_PLATFORM = 1,
39     VFIO_DEVICE_TYPE_CCW = 2,
40     VFIO_DEVICE_TYPE_AP = 3,
41 };
42 struct VFIOGroup;
43 
44 typedef struct VFIOContainer {
45     VFIOContainerBase bcontainer;
46     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
47     unsigned iommu_type;
48     QLIST_HEAD(, VFIOGroup) group_list;
49 } VFIOContainer;
50 
51 OBJECT_DECLARE_SIMPLE_TYPE(VFIOContainer, VFIO_IOMMU_LEGACY);
52 
53 typedef struct VFIODeviceOps VFIODeviceOps;
54 typedef struct VFIOMigration VFIOMigration;
55 
56 typedef struct IOMMUFDBackend IOMMUFDBackend;
57 typedef struct VFIOIOASHwpt VFIOIOASHwpt;
58 
59 typedef struct VFIODevice {
60     QLIST_ENTRY(VFIODevice) next;
61     QLIST_ENTRY(VFIODevice) container_next;
62     QLIST_ENTRY(VFIODevice) global_next;
63     struct VFIOGroup *group;
64     VFIOContainerBase *bcontainer;
65     char *sysfsdev;
66     char *name;
67     DeviceState *dev;
68     int fd;
69     int type;
70     bool mdev;
71     bool reset_works;
72     bool needs_reset;
73     bool no_mmap;
74     bool ram_block_discard_allowed;
75     OnOffAuto enable_migration;
76     OnOffAuto migration_multifd_transfer;
77     bool migration_events;
78     VFIODeviceOps *ops;
79     unsigned int num_irqs;
80     unsigned int num_regions;
81     unsigned int flags;
82     VFIOMigration *migration;
83     Error *migration_blocker;
84     OnOffAuto pre_copy_dirty_page_tracking;
85     OnOffAuto device_dirty_page_tracking;
86     bool dirty_pages_supported;
87     bool dirty_tracking; /* Protected by BQL */
88     bool iommu_dirty_tracking;
89     HostIOMMUDevice *hiod;
90     int devid;
91     IOMMUFDBackend *iommufd;
92     VFIOIOASHwpt *hwpt;
93     QLIST_ENTRY(VFIODevice) hwpt_next;
94 } VFIODevice;
95 
96 struct VFIODeviceOps {
97     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
98     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
99     void (*vfio_eoi)(VFIODevice *vdev);
100     Object *(*vfio_get_object)(VFIODevice *vdev);
101 
102     /**
103      * @vfio_save_config
104      *
105      * Save device config state
106      *
107      * @vdev: #VFIODevice for which to save the config
108      * @f: #QEMUFile where to send the data
109      * @errp: pointer to Error*, to store an error if it happens.
110      *
111      * Returns zero to indicate success and negative for error
112      */
113     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
114 
115     /**
116      * @vfio_load_config
117      *
118      * Load device config state
119      *
120      * @vdev: #VFIODevice for which to load the config
121      * @f: #QEMUFile where to get the data
122      *
123      * Returns zero to indicate success and negative for error
124      */
125     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
126 };
127 
128 typedef struct VFIOGroup {
129     int fd;
130     int groupid;
131     VFIOContainer *container;
132     QLIST_HEAD(, VFIODevice) device_list;
133     QLIST_ENTRY(VFIOGroup) next;
134     QLIST_ENTRY(VFIOGroup) container_next;
135     bool ram_block_discard_allowed;
136 } VFIOGroup;
137 
138 #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
139 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \
140             TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
141 
142 VFIOAddressSpace *vfio_get_address_space(AddressSpace *as);
143 void vfio_put_address_space(VFIOAddressSpace *space);
144 void vfio_address_space_insert(VFIOAddressSpace *space,
145                                VFIOContainerBase *bcontainer);
146 
147 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
148 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
149 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
150 bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
151                             int action, int fd, Error **errp);
152 
153 void vfio_reset_handler(void *opaque);
154 struct vfio_device_info *vfio_get_device_info(int fd);
155 bool vfio_device_is_mdev(VFIODevice *vbasedev);
156 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp);
157 bool vfio_attach_device(char *name, VFIODevice *vbasedev,
158                         AddressSpace *as, Error **errp);
159 void vfio_detach_device(VFIODevice *vbasedev);
160 VFIODevice *vfio_get_vfio_device(Object *obj);
161 
162 int vfio_kvm_device_add_fd(int fd, Error **errp);
163 int vfio_kvm_device_del_fd(int fd, Error **errp);
164 
165 bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp);
166 void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer);
167 
168 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
169 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
170 extern VFIOGroupList vfio_group_list;
171 extern VFIODeviceList vfio_device_list;
172 extern const MemoryListener vfio_memory_listener;
173 extern int vfio_kvm_device_fd;
174 
175 #ifdef CONFIG_LINUX
176 int vfio_get_region_info(VFIODevice *vbasedev, int index,
177                          struct vfio_region_info **info);
178 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
179                              uint32_t subtype, struct vfio_region_info **info);
180 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
181 struct vfio_info_cap_header *
182 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
183 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
184                              unsigned int *avail);
185 struct vfio_info_cap_header *
186 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
187 struct vfio_info_cap_header *
188 vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);
189 #endif
190 
191 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size);
192 bool vfio_devices_all_dirty_tracking_started(
193     const VFIOContainerBase *bcontainer);
194 bool
195 vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
196 int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
197                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
198 int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
199                           uint64_t size, ram_addr_t ram_addr, Error **errp);
200 
201 /* Returns 0 on success, or a negative errno. */
202 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
203 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
204 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
205                       DeviceState *dev, bool ram_discard);
206 int vfio_device_get_aw_bits(VFIODevice *vdev);
207 #endif /* HW_VFIO_VFIO_COMMON_H */
208