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"
35 #define VFIO_MSG_PREFIX "vfio %s: "
38 VFIO_DEVICE_TYPE_PCI
= 0,
39 VFIO_DEVICE_TYPE_PLATFORM
= 1,
40 VFIO_DEVICE_TYPE_CCW
= 2,
41 VFIO_DEVICE_TYPE_AP
= 3,
44 typedef struct VFIOMmap
{
51 typedef struct VFIORegion
{
52 struct VFIODevice
*vbasedev
;
53 off_t fd_offset
; /* offset of region within device fd */
54 MemoryRegion
*mem
; /* slow, read/write access */
56 uint32_t flags
; /* VFIO region flags (rd/wr/mmap) */
59 uint8_t nr
; /* cache the region number for debug */
62 typedef struct VFIOMigration
{
63 struct VFIODevice
*vbasedev
;
64 VMChangeStateEntry
*vm_state
;
65 Notifier migration_state
;
66 uint32_t device_state
;
69 size_t data_buffer_size
;
71 uint64_t precopy_init_size
;
72 uint64_t precopy_dirty_size
;
73 bool initial_data_sent
;
78 typedef struct VFIOContainer
{
79 VFIOContainerBase bcontainer
;
80 int fd
; /* /dev/vfio/vfio, empowered by the attached groups */
81 MemoryListener prereg_listener
;
83 uint64_t dirty_pgsizes
;
84 uint64_t max_dirty_bitmap_size
;
85 QLIST_HEAD(, VFIOHostDMAWindow
) hostwin_list
;
86 QLIST_HEAD(, VFIOGroup
) group_list
;
90 typedef struct VFIOHostDMAWindow
{
93 uint64_t iova_pgsizes
;
94 QLIST_ENTRY(VFIOHostDMAWindow
) hostwin_next
;
97 typedef struct VFIODeviceOps VFIODeviceOps
;
99 typedef struct VFIODevice
{
100 QLIST_ENTRY(VFIODevice
) next
;
101 QLIST_ENTRY(VFIODevice
) container_next
;
102 QLIST_ENTRY(VFIODevice
) global_next
;
103 struct VFIOGroup
*group
;
104 VFIOContainerBase
*bcontainer
;
113 bool ram_block_discard_allowed
;
114 OnOffAuto enable_migration
;
116 unsigned int num_irqs
;
117 unsigned int num_regions
;
119 VFIOMigration
*migration
;
120 Error
*migration_blocker
;
121 OnOffAuto pre_copy_dirty_page_tracking
;
122 bool dirty_pages_supported
;
126 struct VFIODeviceOps
{
127 void (*vfio_compute_needs_reset
)(VFIODevice
*vdev
);
128 int (*vfio_hot_reset_multi
)(VFIODevice
*vdev
);
129 void (*vfio_eoi
)(VFIODevice
*vdev
);
130 Object
*(*vfio_get_object
)(VFIODevice
*vdev
);
131 void (*vfio_save_config
)(VFIODevice
*vdev
, QEMUFile
*f
);
132 int (*vfio_load_config
)(VFIODevice
*vdev
, QEMUFile
*f
);
135 typedef struct VFIOGroup
{
138 VFIOContainer
*container
;
139 QLIST_HEAD(, VFIODevice
) device_list
;
140 QLIST_ENTRY(VFIOGroup
) next
;
141 QLIST_ENTRY(VFIOGroup
) container_next
;
142 bool ram_block_discard_allowed
;
145 typedef struct VFIODMABuf
{
147 uint32_t pos_x
, pos_y
, pos_updates
;
148 uint32_t hot_x
, hot_y
, hot_updates
;
150 QTAILQ_ENTRY(VFIODMABuf
) next
;
153 typedef struct VFIODisplay
{
156 struct vfio_region_info
*edid_info
;
157 struct vfio_region_gfx_edid
*edid_regs
;
159 QEMUTimer
*edid_link_timer
;
162 DisplaySurface
*surface
;
165 QTAILQ_HEAD(, VFIODMABuf
) bufs
;
171 VFIOAddressSpace
*vfio_get_address_space(AddressSpace
*as
);
172 void vfio_put_address_space(VFIOAddressSpace
*space
);
175 int vfio_container_add_section_window(VFIOContainer
*container
,
176 MemoryRegionSection
*section
,
178 void vfio_container_del_section_window(VFIOContainer
*container
,
179 MemoryRegionSection
*section
);
180 int vfio_spapr_container_init(VFIOContainer
*container
, Error
**errp
);
181 void vfio_spapr_container_deinit(VFIOContainer
*container
);
183 void vfio_disable_irqindex(VFIODevice
*vbasedev
, int index
);
184 void vfio_unmask_single_irqindex(VFIODevice
*vbasedev
, int index
);
185 void vfio_mask_single_irqindex(VFIODevice
*vbasedev
, int index
);
186 int vfio_set_irq_signaling(VFIODevice
*vbasedev
, int index
, int subindex
,
187 int action
, int fd
, Error
**errp
);
188 void vfio_region_write(void *opaque
, hwaddr addr
,
189 uint64_t data
, unsigned size
);
190 uint64_t vfio_region_read(void *opaque
,
191 hwaddr addr
, unsigned size
);
192 int vfio_region_setup(Object
*obj
, VFIODevice
*vbasedev
, VFIORegion
*region
,
193 int index
, const char *name
);
194 int vfio_region_mmap(VFIORegion
*region
);
195 void vfio_region_mmaps_set_enabled(VFIORegion
*region
, bool enabled
);
196 void vfio_region_unmap(VFIORegion
*region
);
197 void vfio_region_exit(VFIORegion
*region
);
198 void vfio_region_finalize(VFIORegion
*region
);
199 void vfio_reset_handler(void *opaque
);
200 struct vfio_device_info
*vfio_get_device_info(int fd
);
201 int vfio_attach_device(char *name
, VFIODevice
*vbasedev
,
202 AddressSpace
*as
, Error
**errp
);
203 void vfio_detach_device(VFIODevice
*vbasedev
);
205 int vfio_kvm_device_add_fd(int fd
, Error
**errp
);
206 int vfio_kvm_device_del_fd(int fd
, Error
**errp
);
208 extern const MemoryRegionOps vfio_region_ops
;
209 typedef QLIST_HEAD(VFIOGroupList
, VFIOGroup
) VFIOGroupList
;
210 typedef QLIST_HEAD(VFIODeviceList
, VFIODevice
) VFIODeviceList
;
211 extern VFIOGroupList vfio_group_list
;
212 extern VFIODeviceList vfio_device_list
;
213 extern const VFIOIOMMUOps vfio_legacy_ops
;
214 extern const MemoryListener vfio_memory_listener
;
215 extern int vfio_kvm_device_fd
;
217 bool vfio_mig_active(void);
218 int vfio_block_multiple_devices_migration(VFIODevice
*vbasedev
, Error
**errp
);
219 void vfio_unblock_multiple_devices_migration(void);
220 bool vfio_viommu_preset(VFIODevice
*vbasedev
);
221 int64_t vfio_mig_bytes_transferred(void);
222 void vfio_reset_bytes_transferred(void);
223 bool vfio_device_state_is_running(VFIODevice
*vbasedev
);
224 bool vfio_device_state_is_precopy(VFIODevice
*vbasedev
);
227 int vfio_get_region_info(VFIODevice
*vbasedev
, int index
,
228 struct vfio_region_info
**info
);
229 int vfio_get_dev_region_info(VFIODevice
*vbasedev
, uint32_t type
,
230 uint32_t subtype
, struct vfio_region_info
**info
);
231 bool vfio_has_region_cap(VFIODevice
*vbasedev
, int region
, uint16_t cap_type
);
232 struct vfio_info_cap_header
*
233 vfio_get_region_info_cap(struct vfio_region_info
*info
, uint16_t id
);
234 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info
*info
,
235 unsigned int *avail
);
236 struct vfio_info_cap_header
*
237 vfio_get_device_info_cap(struct vfio_device_info
*info
, uint16_t id
);
238 struct vfio_info_cap_header
*
239 vfio_get_cap(void *ptr
, uint32_t cap_offset
, uint16_t id
);
242 bool vfio_migration_realize(VFIODevice
*vbasedev
, Error
**errp
);
243 void vfio_migration_exit(VFIODevice
*vbasedev
);
245 int vfio_bitmap_alloc(VFIOBitmap
*vbmap
, hwaddr size
);
246 bool vfio_devices_all_running_and_mig_active(VFIOContainerBase
*bcontainer
);
247 bool vfio_devices_all_device_dirty_tracking(VFIOContainerBase
*bcontainer
);
248 int vfio_devices_query_dirty_bitmap(VFIOContainerBase
*bcontainer
,
249 VFIOBitmap
*vbmap
, hwaddr iova
,
251 int vfio_get_dirty_bitmap(VFIOContainerBase
*bcontainer
, uint64_t iova
,
252 uint64_t size
, ram_addr_t ram_addr
);
253 #endif /* HW_VFIO_VFIO_COMMON_H */