2 * common header for vfio based device assignment support
4 * Copyright Red Hat, Inc. 2012
7 * Alex Williamson <alex.williamson@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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)
21 #ifndef HW_VFIO_VFIO_COMMON_H
22 #define HW_VFIO_VFIO_COMMON_H
24 #include "exec/memory.h"
25 #include "qemu/queue.h"
26 #include "qemu/notify.h"
27 #include "ui/console.h"
28 #include "hw/display/ramfb.h"
30 #include <linux/vfio.h>
32 #include "sysemu/sysemu.h"
33 #include "hw/vfio/vfio-container-base.h"
34 #include "sysemu/host_iommu_device.h"
35 #include "sysemu/iommufd.h"
37 #define VFIO_MSG_PREFIX "vfio %s: "
40 VFIO_DEVICE_TYPE_PCI
= 0,
41 VFIO_DEVICE_TYPE_PLATFORM
= 1,
42 VFIO_DEVICE_TYPE_CCW
= 2,
43 VFIO_DEVICE_TYPE_AP
= 3,
46 typedef struct VFIOMmap
{
53 typedef struct VFIORegion
{
54 struct VFIODevice
*vbasedev
;
55 off_t fd_offset
; /* offset of region within device fd */
56 MemoryRegion
*mem
; /* slow, read/write access */
58 uint32_t flags
; /* VFIO region flags (rd/wr/mmap) */
61 uint8_t nr
; /* cache the region number for debug */
64 typedef struct VFIOMigration
{
65 struct VFIODevice
*vbasedev
;
66 VMChangeStateEntry
*vm_state
;
67 NotifierWithReturn migration_state
;
68 uint32_t device_state
;
71 size_t data_buffer_size
;
73 uint64_t precopy_init_size
;
74 uint64_t precopy_dirty_size
;
75 bool initial_data_sent
;
80 typedef struct VFIOContainer
{
81 VFIOContainerBase bcontainer
;
82 int fd
; /* /dev/vfio/vfio, empowered by the attached groups */
84 QLIST_HEAD(, VFIOGroup
) group_list
;
87 OBJECT_DECLARE_SIMPLE_TYPE(VFIOContainer
, VFIO_IOMMU_LEGACY
);
89 typedef struct VFIOHostDMAWindow
{
92 uint64_t iova_pgsizes
;
93 QLIST_ENTRY(VFIOHostDMAWindow
) hostwin_next
;
96 typedef struct IOMMUFDBackend IOMMUFDBackend
;
98 typedef struct VFIOIOMMUFDContainer
{
99 VFIOContainerBase bcontainer
;
102 } VFIOIOMMUFDContainer
;
104 OBJECT_DECLARE_SIMPLE_TYPE(VFIOIOMMUFDContainer
, VFIO_IOMMU_IOMMUFD
);
106 typedef struct VFIODeviceOps VFIODeviceOps
;
108 typedef struct VFIODevice
{
109 QLIST_ENTRY(VFIODevice
) next
;
110 QLIST_ENTRY(VFIODevice
) container_next
;
111 QLIST_ENTRY(VFIODevice
) global_next
;
112 struct VFIOGroup
*group
;
113 VFIOContainerBase
*bcontainer
;
122 bool ram_block_discard_allowed
;
123 OnOffAuto enable_migration
;
124 bool migration_events
;
126 unsigned int num_irqs
;
127 unsigned int num_regions
;
129 VFIOMigration
*migration
;
130 Error
*migration_blocker
;
131 OnOffAuto pre_copy_dirty_page_tracking
;
132 bool dirty_pages_supported
;
134 HostIOMMUDevice
*hiod
;
136 IOMMUFDBackend
*iommufd
;
139 struct VFIODeviceOps
{
140 void (*vfio_compute_needs_reset
)(VFIODevice
*vdev
);
141 int (*vfio_hot_reset_multi
)(VFIODevice
*vdev
);
142 void (*vfio_eoi
)(VFIODevice
*vdev
);
143 Object
*(*vfio_get_object
)(VFIODevice
*vdev
);
148 * Save device config state
150 * @vdev: #VFIODevice for which to save the config
151 * @f: #QEMUFile where to send the data
152 * @errp: pointer to Error*, to store an error if it happens.
154 * Returns zero to indicate success and negative for error
156 int (*vfio_save_config
)(VFIODevice
*vdev
, QEMUFile
*f
, Error
**errp
);
161 * Load device config state
163 * @vdev: #VFIODevice for which to load the config
164 * @f: #QEMUFile where to get the data
166 * Returns zero to indicate success and negative for error
168 int (*vfio_load_config
)(VFIODevice
*vdev
, QEMUFile
*f
);
171 typedef struct VFIOGroup
{
174 VFIOContainer
*container
;
175 QLIST_HEAD(, VFIODevice
) device_list
;
176 QLIST_ENTRY(VFIOGroup
) next
;
177 QLIST_ENTRY(VFIOGroup
) container_next
;
178 bool ram_block_discard_allowed
;
181 #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
182 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \
183 TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
185 typedef struct VFIODMABuf
{
187 uint32_t pos_x
, pos_y
, pos_updates
;
188 uint32_t hot_x
, hot_y
, hot_updates
;
190 QTAILQ_ENTRY(VFIODMABuf
) next
;
193 typedef struct VFIODisplay
{
196 struct vfio_region_info
*edid_info
;
197 struct vfio_region_gfx_edid
*edid_regs
;
199 QEMUTimer
*edid_link_timer
;
202 DisplaySurface
*surface
;
205 QTAILQ_HEAD(, VFIODMABuf
) bufs
;
211 VFIOAddressSpace
*vfio_get_address_space(AddressSpace
*as
);
212 void vfio_put_address_space(VFIOAddressSpace
*space
);
213 void vfio_address_space_insert(VFIOAddressSpace
*space
,
214 VFIOContainerBase
*bcontainer
);
216 void vfio_disable_irqindex(VFIODevice
*vbasedev
, int index
);
217 void vfio_unmask_single_irqindex(VFIODevice
*vbasedev
, int index
);
218 void vfio_mask_single_irqindex(VFIODevice
*vbasedev
, int index
);
219 bool vfio_set_irq_signaling(VFIODevice
*vbasedev
, int index
, int subindex
,
220 int action
, int fd
, Error
**errp
);
221 void vfio_region_write(void *opaque
, hwaddr addr
,
222 uint64_t data
, unsigned size
);
223 uint64_t vfio_region_read(void *opaque
,
224 hwaddr addr
, unsigned size
);
225 int vfio_region_setup(Object
*obj
, VFIODevice
*vbasedev
, VFIORegion
*region
,
226 int index
, const char *name
);
227 int vfio_region_mmap(VFIORegion
*region
);
228 void vfio_region_mmaps_set_enabled(VFIORegion
*region
, bool enabled
);
229 void vfio_region_unmap(VFIORegion
*region
);
230 void vfio_region_exit(VFIORegion
*region
);
231 void vfio_region_finalize(VFIORegion
*region
);
232 void vfio_reset_handler(void *opaque
);
233 struct vfio_device_info
*vfio_get_device_info(int fd
);
234 bool vfio_attach_device(char *name
, VFIODevice
*vbasedev
,
235 AddressSpace
*as
, Error
**errp
);
236 void vfio_detach_device(VFIODevice
*vbasedev
);
238 int vfio_kvm_device_add_fd(int fd
, Error
**errp
);
239 int vfio_kvm_device_del_fd(int fd
, Error
**errp
);
241 bool vfio_cpr_register_container(VFIOContainerBase
*bcontainer
, Error
**errp
);
242 void vfio_cpr_unregister_container(VFIOContainerBase
*bcontainer
);
244 extern const MemoryRegionOps vfio_region_ops
;
245 typedef QLIST_HEAD(VFIOGroupList
, VFIOGroup
) VFIOGroupList
;
246 typedef QLIST_HEAD(VFIODeviceList
, VFIODevice
) VFIODeviceList
;
247 extern VFIOGroupList vfio_group_list
;
248 extern VFIODeviceList vfio_device_list
;
249 extern const MemoryListener vfio_memory_listener
;
250 extern int vfio_kvm_device_fd
;
252 bool vfio_mig_active(void);
253 int vfio_block_multiple_devices_migration(VFIODevice
*vbasedev
, Error
**errp
);
254 void vfio_unblock_multiple_devices_migration(void);
255 bool vfio_viommu_preset(VFIODevice
*vbasedev
);
256 int64_t vfio_mig_bytes_transferred(void);
257 void vfio_reset_bytes_transferred(void);
258 bool vfio_device_state_is_running(VFIODevice
*vbasedev
);
259 bool vfio_device_state_is_precopy(VFIODevice
*vbasedev
);
262 int vfio_get_region_info(VFIODevice
*vbasedev
, int index
,
263 struct vfio_region_info
**info
);
264 int vfio_get_dev_region_info(VFIODevice
*vbasedev
, uint32_t type
,
265 uint32_t subtype
, struct vfio_region_info
**info
);
266 bool vfio_has_region_cap(VFIODevice
*vbasedev
, int region
, uint16_t cap_type
);
267 struct vfio_info_cap_header
*
268 vfio_get_region_info_cap(struct vfio_region_info
*info
, uint16_t id
);
269 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info
*info
,
270 unsigned int *avail
);
271 struct vfio_info_cap_header
*
272 vfio_get_device_info_cap(struct vfio_device_info
*info
, uint16_t id
);
273 struct vfio_info_cap_header
*
274 vfio_get_cap(void *ptr
, uint32_t cap_offset
, uint16_t id
);
277 bool vfio_migration_realize(VFIODevice
*vbasedev
, Error
**errp
);
278 void vfio_migration_exit(VFIODevice
*vbasedev
);
280 int vfio_bitmap_alloc(VFIOBitmap
*vbmap
, hwaddr size
);
282 vfio_devices_all_running_and_mig_active(const VFIOContainerBase
*bcontainer
);
284 vfio_devices_all_device_dirty_tracking(const VFIOContainerBase
*bcontainer
);
285 int vfio_devices_query_dirty_bitmap(const VFIOContainerBase
*bcontainer
,
286 VFIOBitmap
*vbmap
, hwaddr iova
, hwaddr size
, Error
**errp
);
287 int vfio_get_dirty_bitmap(const VFIOContainerBase
*bcontainer
, uint64_t iova
,
288 uint64_t size
, ram_addr_t ram_addr
, Error
**errp
);
290 /* Returns 0 on success, or a negative errno. */
291 bool vfio_device_get_name(VFIODevice
*vbasedev
, Error
**errp
);
292 void vfio_device_set_fd(VFIODevice
*vbasedev
, const char *str
, Error
**errp
);
293 void vfio_device_init(VFIODevice
*vbasedev
, int type
, VFIODeviceOps
*ops
,
294 DeviceState
*dev
, bool ram_discard
);
295 int vfio_device_get_aw_bits(VFIODevice
*vdev
);
296 #endif /* HW_VFIO_VFIO_COMMON_H */