xref: /qemu/include/hw/vfio/vfio-container-base.h (revision 504d297e10fdfe1b1243274e334abb0074ee69f4)
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 "exec/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     const VFIOIOMMUClass *ops;
39     VFIOAddressSpace *space;
40     MemoryListener listener;
41     Error *error;
42     bool initialized;
43     uint64_t dirty_pgsizes;
44     uint64_t max_dirty_bitmap_size;
45     unsigned long pgsizes;
46     unsigned int dma_max_mappings;
47     bool dirty_pages_supported;
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 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
75                            hwaddr iova, ram_addr_t size,
76                            void *vaddr, bool readonly);
77 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
78                              hwaddr iova, ram_addr_t size,
79                              IOMMUTLBEntry *iotlb);
80 bool vfio_container_add_section_window(VFIOContainerBase *bcontainer,
81                                        MemoryRegionSection *section,
82                                        Error **errp);
83 void vfio_container_del_section_window(VFIOContainerBase *bcontainer,
84                                        MemoryRegionSection *section);
85 int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
86                                            bool start, Error **errp);
87 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
88                    VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
89 
90 void vfio_container_init(VFIOContainerBase *bcontainer,
91                          const VFIOIOMMUClass *ops);
92 void vfio_container_destroy(VFIOContainerBase *bcontainer);
93 
94 
95 #define TYPE_VFIO_IOMMU "vfio-iommu"
96 #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
97 #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
98 #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
99 
100 OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU)
101 
102 struct VFIOIOMMUClass {
103     ObjectClass parent_class;
104 
105     /* Properties */
106     const char *hiod_typename;
107 
108     /* basic feature */
109     bool (*setup)(VFIOContainerBase *bcontainer, Error **errp);
110     int (*dma_map)(const VFIOContainerBase *bcontainer,
111                    hwaddr iova, ram_addr_t size,
112                    void *vaddr, bool readonly);
113     int (*dma_unmap)(const VFIOContainerBase *bcontainer,
114                      hwaddr iova, ram_addr_t size,
115                      IOMMUTLBEntry *iotlb);
116     bool (*attach_device)(const char *name, VFIODevice *vbasedev,
117                           AddressSpace *as, Error **errp);
118     void (*detach_device)(VFIODevice *vbasedev);
119 
120     /* migration feature */
121 
122     /**
123      * @set_dirty_page_tracking
124      *
125      * Start or stop dirty pages tracking on VFIO container
126      *
127      * @bcontainer: #VFIOContainerBase on which to de/activate dirty
128      *              page tracking
129      * @start: indicates whether to start or stop dirty pages tracking
130      * @errp: pointer to Error*, to store an error if it happens.
131      *
132      * Returns zero to indicate success and negative for error
133      */
134     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
135                                    bool start, Error **errp);
136     /**
137      * @query_dirty_bitmap
138      *
139      * Get bitmap of dirty pages from container
140      *
141      * @bcontainer: #VFIOContainerBase from which to get dirty pages
142      * @vbmap: #VFIOBitmap internal bitmap structure
143      * @iova: iova base address
144      * @size: size of iova range
145      * @errp: pointer to Error*, to store an error if it happens.
146      *
147      * Returns zero to indicate success and negative for error
148      */
149     int (*query_dirty_bitmap)(const VFIOContainerBase *bcontainer,
150                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
151     /* PCI specific */
152     int (*pci_hot_reset)(VFIODevice *vbasedev, bool single);
153 
154     /* SPAPR specific */
155     bool (*add_window)(VFIOContainerBase *bcontainer,
156                        MemoryRegionSection *section,
157                        Error **errp);
158     void (*del_window)(VFIOContainerBase *bcontainer,
159                        MemoryRegionSection *section);
160     void (*release)(VFIOContainerBase *bcontainer);
161 };
162 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
163