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 "qemu-common.h"
25 #include "exec/address-spaces.h"
26 #include "exec/memory.h"
27 #include "qemu/queue.h"
28 #include "qemu/notify.h"
29 #include "ui/console.h"
31 #include <linux/vfio.h>
34 #define ERR_PREFIX "vfio error: %s: "
35 #define WARN_PREFIX "vfio warning: %s: "
37 /*#define DEBUG_VFIO*/
39 #define DPRINTF(fmt, ...) \
40 do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0)
42 #define DPRINTF(fmt, ...) \
47 VFIO_DEVICE_TYPE_PCI
= 0,
48 VFIO_DEVICE_TYPE_PLATFORM
= 1,
49 VFIO_DEVICE_TYPE_CCW
= 2,
52 typedef struct VFIOMmap
{
59 typedef struct VFIORegion
{
60 struct VFIODevice
*vbasedev
;
61 off_t fd_offset
; /* offset of region within device fd */
62 MemoryRegion
*mem
; /* slow, read/write access */
64 uint32_t flags
; /* VFIO region flags (rd/wr/mmap) */
67 uint8_t nr
; /* cache the region number for debug */
70 typedef struct VFIOAddressSpace
{
72 QLIST_HEAD(, VFIOContainer
) containers
;
73 QLIST_ENTRY(VFIOAddressSpace
) list
;
78 typedef struct VFIOContainer
{
79 VFIOAddressSpace
*space
;
80 int fd
; /* /dev/vfio/vfio, empowered by the attached groups */
81 MemoryListener listener
;
82 MemoryListener prereg_listener
;
87 * This assumes the host IOMMU can support only a single
88 * contiguous IOVA window. We may need to generalize that in
91 QLIST_HEAD(, VFIOGuestIOMMU
) giommu_list
;
92 QLIST_HEAD(, VFIOHostDMAWindow
) hostwin_list
;
93 QLIST_HEAD(, VFIOGroup
) group_list
;
94 QLIST_ENTRY(VFIOContainer
) next
;
97 typedef struct VFIOGuestIOMMU
{
98 VFIOContainer
*container
;
99 IOMMUMemoryRegion
*iommu
;
102 QLIST_ENTRY(VFIOGuestIOMMU
) giommu_next
;
105 typedef struct VFIOHostDMAWindow
{
108 uint64_t iova_pgsizes
;
109 QLIST_ENTRY(VFIOHostDMAWindow
) hostwin_next
;
112 typedef struct VFIODeviceOps VFIODeviceOps
;
114 typedef struct VFIODevice
{
115 QLIST_ENTRY(VFIODevice
) next
;
116 struct VFIOGroup
*group
;
126 unsigned int num_irqs
;
127 unsigned int num_regions
;
131 struct VFIODeviceOps
{
132 void (*vfio_compute_needs_reset
)(VFIODevice
*vdev
);
133 int (*vfio_hot_reset_multi
)(VFIODevice
*vdev
);
134 void (*vfio_eoi
)(VFIODevice
*vdev
);
137 typedef struct VFIOGroup
{
140 VFIOContainer
*container
;
141 QLIST_HEAD(, VFIODevice
) device_list
;
142 QLIST_ENTRY(VFIOGroup
) next
;
143 QLIST_ENTRY(VFIOGroup
) container_next
;
146 typedef struct VFIODMABuf
{
148 uint32_t pos_x
, pos_y
, pos_updates
;
149 uint32_t hot_x
, hot_y
, hot_updates
;
151 QTAILQ_ENTRY(VFIODMABuf
) next
;
154 typedef struct VFIODisplay
{
158 DisplaySurface
*surface
;
161 QTAILQ_HEAD(, VFIODMABuf
) bufs
;
167 void vfio_put_base_device(VFIODevice
*vbasedev
);
168 void vfio_disable_irqindex(VFIODevice
*vbasedev
, int index
);
169 void vfio_unmask_single_irqindex(VFIODevice
*vbasedev
, int index
);
170 void vfio_mask_single_irqindex(VFIODevice
*vbasedev
, int index
);
171 void vfio_region_write(void *opaque
, hwaddr addr
,
172 uint64_t data
, unsigned size
);
173 uint64_t vfio_region_read(void *opaque
,
174 hwaddr addr
, unsigned size
);
175 int vfio_region_setup(Object
*obj
, VFIODevice
*vbasedev
, VFIORegion
*region
,
176 int index
, const char *name
);
177 int vfio_region_mmap(VFIORegion
*region
);
178 void vfio_region_mmaps_set_enabled(VFIORegion
*region
, bool enabled
);
179 void vfio_region_exit(VFIORegion
*region
);
180 void vfio_region_finalize(VFIORegion
*region
);
181 void vfio_reset_handler(void *opaque
);
182 VFIOGroup
*vfio_get_group(int groupid
, AddressSpace
*as
, Error
**errp
);
183 void vfio_put_group(VFIOGroup
*group
);
184 int vfio_get_device(VFIOGroup
*group
, const char *name
,
185 VFIODevice
*vbasedev
, Error
**errp
);
187 extern const MemoryRegionOps vfio_region_ops
;
188 extern QLIST_HEAD(vfio_group_head
, VFIOGroup
) vfio_group_list
;
189 extern QLIST_HEAD(vfio_as_head
, VFIOAddressSpace
) vfio_address_spaces
;
192 int vfio_get_region_info(VFIODevice
*vbasedev
, int index
,
193 struct vfio_region_info
**info
);
194 int vfio_get_dev_region_info(VFIODevice
*vbasedev
, uint32_t type
,
195 uint32_t subtype
, struct vfio_region_info
**info
);
196 bool vfio_has_region_cap(VFIODevice
*vbasedev
, int region
, uint16_t cap_type
);
198 extern const MemoryListener vfio_prereg_listener
;
200 int vfio_spapr_create_window(VFIOContainer
*container
,
201 MemoryRegionSection
*section
,
203 int vfio_spapr_remove_window(VFIOContainer
*container
,
204 hwaddr offset_within_address_space
);
206 #endif /* HW_VFIO_VFIO_COMMON_H */