xref: /qemu/include/hw/vfio/vfio-container-base.h (revision 35e6d2c1d07dfd45c8b00019a98499d2cd6442ca)
1 /*
2  * VFIO BASE CONTAINER
3  *
4  * Copyright (C) 2023 Intel Corporation.
5  * Copyright Red Hat, Inc. 2023
6  *
7  * Authors: Yi Liu <yi.l.liu@intel.com>
8  *          Eric Auger <eric.auger@redhat.com>
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #ifndef HW_VFIO_VFIO_CONTAINER_BASE_H
14 #define HW_VFIO_VFIO_CONTAINER_BASE_H
15 
16 #include "system/memory.h"
17 
18 typedef struct VFIODevice VFIODevice;
19 typedef struct VFIOIOMMUClass VFIOIOMMUClass;
20 
21 typedef struct {
22     unsigned long *bitmap;
23     hwaddr size;
24     hwaddr pages;
25 } VFIOBitmap;
26 
27 typedef struct VFIOAddressSpace {
28     AddressSpace *as;
29     QLIST_HEAD(, VFIOContainerBase) containers;
30     QLIST_ENTRY(VFIOAddressSpace) list;
31 } VFIOAddressSpace;
32 
33 /*
34  * This is the base object for vfio container backends
35  */
36 typedef struct VFIOContainerBase {
37     Object parent;
38     VFIOAddressSpace *space;
39     MemoryListener listener;
40     Error *error;
41     bool initialized;
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     bool dirty_pages_started; /* Protected by BQL */
48     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
49     QLIST_HEAD(, VFIORamDiscardListener) vrdl_list;
50     QLIST_ENTRY(VFIOContainerBase) next;
51     QLIST_HEAD(, VFIODevice) device_list;
52     GList *iova_ranges;
53     NotifierWithReturn cpr_reboot_notifier;
54 } VFIOContainerBase;
55 
56 typedef struct VFIOGuestIOMMU {
57     VFIOContainerBase *bcontainer;
58     IOMMUMemoryRegion *iommu_mr;
59     hwaddr iommu_offset;
60     IOMMUNotifier n;
61     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
62 } VFIOGuestIOMMU;
63 
64 typedef struct VFIORamDiscardListener {
65     VFIOContainerBase *bcontainer;
66     MemoryRegion *mr;
67     hwaddr offset_within_address_space;
68     hwaddr size;
69     uint64_t granularity;
70     RamDiscardListener listener;
71     QLIST_ENTRY(VFIORamDiscardListener) next;
72 } VFIORamDiscardListener;
73 
74 VFIOAddressSpace *vfio_address_space_get(AddressSpace *as);
75 void vfio_address_space_put(VFIOAddressSpace *space);
76 void vfio_address_space_insert(VFIOAddressSpace *space,
77                                VFIOContainerBase *bcontainer);
78 
79 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
80                            hwaddr iova, ram_addr_t size,
81                            void *vaddr, bool readonly);
82 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
83                              hwaddr iova, ram_addr_t size,
84                              IOMMUTLBEntry *iotlb);
85 bool vfio_container_add_section_window(VFIOContainerBase *bcontainer,
86                                        MemoryRegionSection *section,
87                                        Error **errp);
88 void vfio_container_del_section_window(VFIOContainerBase *bcontainer,
89                                        MemoryRegionSection *section);
90 int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
91                                            bool start, Error **errp);
92 bool vfio_devices_all_dirty_tracking_started(const VFIOContainerBase *bcontainer);
93 bool vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
94 int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
95                           uint64_t size, ram_addr_t ram_addr, Error **errp);
96 
97 GList *vfio_container_get_iova_ranges(const VFIOContainerBase *bcontainer);
98 
99 static inline uint64_t
100 vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer)
101 {
102     assert(bcontainer);
103     return bcontainer->pgsizes;
104 }
105 
106 #define TYPE_VFIO_IOMMU "vfio-iommu"
107 #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
108 #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
109 #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
110 
111 OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU)
112 
113 struct VFIOIOMMUClass {
114     ObjectClass parent_class;
115 
116     /* Properties */
117     const char *hiod_typename;
118 
119     /* basic feature */
120     bool (*setup)(VFIOContainerBase *bcontainer, Error **errp);
121     int (*dma_map)(const VFIOContainerBase *bcontainer,
122                    hwaddr iova, ram_addr_t size,
123                    void *vaddr, bool readonly);
124     int (*dma_unmap)(const VFIOContainerBase *bcontainer,
125                      hwaddr iova, ram_addr_t size,
126                      IOMMUTLBEntry *iotlb);
127     bool (*attach_device)(const char *name, VFIODevice *vbasedev,
128                           AddressSpace *as, Error **errp);
129     void (*detach_device)(VFIODevice *vbasedev);
130 
131     /* migration feature */
132 
133     /**
134      * @set_dirty_page_tracking
135      *
136      * Start or stop dirty pages tracking on VFIO container
137      *
138      * @bcontainer: #VFIOContainerBase on which to de/activate dirty
139      *              page tracking
140      * @start: indicates whether to start or stop dirty pages tracking
141      * @errp: pointer to Error*, to store an error if it happens.
142      *
143      * Returns zero to indicate success and negative for error
144      */
145     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
146                                    bool start, Error **errp);
147     /**
148      * @query_dirty_bitmap
149      *
150      * Get bitmap of dirty pages from container
151      *
152      * @bcontainer: #VFIOContainerBase from which to get dirty pages
153      * @vbmap: #VFIOBitmap internal bitmap structure
154      * @iova: iova base address
155      * @size: size of iova range
156      * @errp: pointer to Error*, to store an error if it happens.
157      *
158      * Returns zero to indicate success and negative for error
159      */
160     int (*query_dirty_bitmap)(const VFIOContainerBase *bcontainer,
161                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
162     /* PCI specific */
163     int (*pci_hot_reset)(VFIODevice *vbasedev, bool single);
164 
165     /* SPAPR specific */
166     bool (*add_window)(VFIOContainerBase *bcontainer,
167                        MemoryRegionSection *section,
168                        Error **errp);
169     void (*del_window)(VFIOContainerBase *bcontainer,
170                        MemoryRegionSection *section);
171     void (*release)(VFIOContainerBase *bcontainer);
172 };
173 
174 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
175