4 * Copyright (C) 2023 Intel Corporation.
5 * Copyright Red Hat, Inc. 2023
7 * Authors: Yi Liu <yi.l.liu@intel.com>
8 * Eric Auger <eric.auger@redhat.com>
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #ifndef HW_VFIO_VFIO_CONTAINER_BASE_H
14 #define HW_VFIO_VFIO_CONTAINER_BASE_H
16 #include "exec/memory.h"
18 typedef struct VFIODevice VFIODevice
;
19 typedef struct VFIOIOMMUClass VFIOIOMMUClass
;
22 unsigned long *bitmap
;
27 typedef struct VFIOAddressSpace
{
29 QLIST_HEAD(, VFIOContainerBase
) containers
;
30 QLIST_ENTRY(VFIOAddressSpace
) list
;
34 * This is the base object for vfio container backends
36 typedef struct VFIOContainerBase
{
38 VFIOAddressSpace
*space
;
39 MemoryListener listener
;
42 uint64_t dirty_pgsizes
;
43 uint64_t max_dirty_bitmap_size
;
44 unsigned long pgsizes
;
45 unsigned int dma_max_mappings
;
46 bool dirty_pages_supported
;
47 QLIST_HEAD(, VFIOGuestIOMMU
) giommu_list
;
48 QLIST_HEAD(, VFIORamDiscardListener
) vrdl_list
;
49 QLIST_ENTRY(VFIOContainerBase
) next
;
50 QLIST_HEAD(, VFIODevice
) device_list
;
52 NotifierWithReturn cpr_reboot_notifier
;
55 typedef struct VFIOGuestIOMMU
{
56 VFIOContainerBase
*bcontainer
;
57 IOMMUMemoryRegion
*iommu_mr
;
60 QLIST_ENTRY(VFIOGuestIOMMU
) giommu_next
;
63 typedef struct VFIORamDiscardListener
{
64 VFIOContainerBase
*bcontainer
;
66 hwaddr offset_within_address_space
;
69 RamDiscardListener listener
;
70 QLIST_ENTRY(VFIORamDiscardListener
) next
;
71 } VFIORamDiscardListener
;
73 int vfio_container_dma_map(VFIOContainerBase
*bcontainer
,
74 hwaddr iova
, ram_addr_t size
,
75 void *vaddr
, bool readonly
);
76 int vfio_container_dma_unmap(VFIOContainerBase
*bcontainer
,
77 hwaddr iova
, ram_addr_t size
,
78 IOMMUTLBEntry
*iotlb
);
79 bool vfio_container_add_section_window(VFIOContainerBase
*bcontainer
,
80 MemoryRegionSection
*section
,
82 void vfio_container_del_section_window(VFIOContainerBase
*bcontainer
,
83 MemoryRegionSection
*section
);
84 int vfio_container_set_dirty_page_tracking(VFIOContainerBase
*bcontainer
,
85 bool start
, Error
**errp
);
86 int vfio_container_query_dirty_bitmap(const VFIOContainerBase
*bcontainer
,
87 VFIOBitmap
*vbmap
, hwaddr iova
, hwaddr size
, Error
**errp
);
89 GList
*vfio_container_get_iova_ranges(const VFIOContainerBase
*bcontainer
);
91 static inline uint64_t
92 vfio_container_get_page_size_mask(const VFIOContainerBase
*bcontainer
)
95 return bcontainer
->pgsizes
;
98 #define TYPE_VFIO_IOMMU "vfio-iommu"
99 #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
100 #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
101 #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
103 OBJECT_DECLARE_TYPE(VFIOContainerBase
, VFIOIOMMUClass
, VFIO_IOMMU
)
105 struct VFIOIOMMUClass
{
106 ObjectClass parent_class
;
109 const char *hiod_typename
;
112 bool (*setup
)(VFIOContainerBase
*bcontainer
, Error
**errp
);
113 int (*dma_map
)(const VFIOContainerBase
*bcontainer
,
114 hwaddr iova
, ram_addr_t size
,
115 void *vaddr
, bool readonly
);
116 int (*dma_unmap
)(const VFIOContainerBase
*bcontainer
,
117 hwaddr iova
, ram_addr_t size
,
118 IOMMUTLBEntry
*iotlb
);
119 bool (*attach_device
)(const char *name
, VFIODevice
*vbasedev
,
120 AddressSpace
*as
, Error
**errp
);
121 void (*detach_device
)(VFIODevice
*vbasedev
);
123 /* migration feature */
126 * @set_dirty_page_tracking
128 * Start or stop dirty pages tracking on VFIO container
130 * @bcontainer: #VFIOContainerBase on which to de/activate dirty
132 * @start: indicates whether to start or stop dirty pages tracking
133 * @errp: pointer to Error*, to store an error if it happens.
135 * Returns zero to indicate success and negative for error
137 int (*set_dirty_page_tracking
)(const VFIOContainerBase
*bcontainer
,
138 bool start
, Error
**errp
);
140 * @query_dirty_bitmap
142 * Get bitmap of dirty pages from container
144 * @bcontainer: #VFIOContainerBase from which to get dirty pages
145 * @vbmap: #VFIOBitmap internal bitmap structure
146 * @iova: iova base address
147 * @size: size of iova range
148 * @errp: pointer to Error*, to store an error if it happens.
150 * Returns zero to indicate success and negative for error
152 int (*query_dirty_bitmap
)(const VFIOContainerBase
*bcontainer
,
153 VFIOBitmap
*vbmap
, hwaddr iova
, hwaddr size
, Error
**errp
);
155 int (*pci_hot_reset
)(VFIODevice
*vbasedev
, bool single
);
158 bool (*add_window
)(VFIOContainerBase
*bcontainer
,
159 MemoryRegionSection
*section
,
161 void (*del_window
)(VFIOContainerBase
*bcontainer
,
162 MemoryRegionSection
*section
);
163 void (*release
)(VFIOContainerBase
*bcontainer
);
165 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */