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     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     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
48     QLIST_HEAD(, VFIORamDiscardListener) vrdl_list;
49     QLIST_ENTRY(VFIOContainerBase) next;
50     QLIST_HEAD(, VFIODevice) device_list;
51     GList *iova_ranges;
52     NotifierWithReturn cpr_reboot_notifier;
53 } VFIOContainerBase;
54 
55 typedef struct VFIOGuestIOMMU {
56     VFIOContainerBase *bcontainer;
57     IOMMUMemoryRegion *iommu_mr;
58     hwaddr iommu_offset;
59     IOMMUNotifier n;
60     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
61 } VFIOGuestIOMMU;
62 
63 typedef struct VFIORamDiscardListener {
64     VFIOContainerBase *bcontainer;
65     MemoryRegion *mr;
66     hwaddr offset_within_address_space;
67     hwaddr size;
68     uint64_t granularity;
69     RamDiscardListener listener;
70     QLIST_ENTRY(VFIORamDiscardListener) next;
71 } VFIORamDiscardListener;
72 
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,
81                                        Error **errp);
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);
88 
89 #define TYPE_VFIO_IOMMU "vfio-iommu"
90 #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
91 #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
92 #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
93 
94 OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU)
95 
96 struct VFIOIOMMUClass {
97     ObjectClass parent_class;
98 
99     /* Properties */
100     const char *hiod_typename;
101 
102     /* basic feature */
103     bool (*setup)(VFIOContainerBase *bcontainer, Error **errp);
104     int (*dma_map)(const VFIOContainerBase *bcontainer,
105                    hwaddr iova, ram_addr_t size,
106                    void *vaddr, bool readonly);
107     int (*dma_unmap)(const VFIOContainerBase *bcontainer,
108                      hwaddr iova, ram_addr_t size,
109                      IOMMUTLBEntry *iotlb);
110     bool (*attach_device)(const char *name, VFIODevice *vbasedev,
111                           AddressSpace *as, Error **errp);
112     void (*detach_device)(VFIODevice *vbasedev);
113 
114     /* migration feature */
115 
116     /**
117      * @set_dirty_page_tracking
118      *
119      * Start or stop dirty pages tracking on VFIO container
120      *
121      * @bcontainer: #VFIOContainerBase on which to de/activate dirty
122      *              page tracking
123      * @start: indicates whether to start or stop dirty pages tracking
124      * @errp: pointer to Error*, to store an error if it happens.
125      *
126      * Returns zero to indicate success and negative for error
127      */
128     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
129                                    bool start, Error **errp);
130     /**
131      * @query_dirty_bitmap
132      *
133      * Get bitmap of dirty pages from container
134      *
135      * @bcontainer: #VFIOContainerBase from which to get dirty pages
136      * @vbmap: #VFIOBitmap internal bitmap structure
137      * @iova: iova base address
138      * @size: size of iova range
139      * @errp: pointer to Error*, to store an error if it happens.
140      *
141      * Returns zero to indicate success and negative for error
142      */
143     int (*query_dirty_bitmap)(const VFIOContainerBase *bcontainer,
144                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
145     /* PCI specific */
146     int (*pci_hot_reset)(VFIODevice *vbasedev, bool single);
147 
148     /* SPAPR specific */
149     bool (*add_window)(VFIOContainerBase *bcontainer,
150                        MemoryRegionSection *section,
151                        Error **errp);
152     void (*del_window)(VFIOContainerBase *bcontainer,
153                        MemoryRegionSection *section);
154     void (*release)(VFIOContainerBase *bcontainer);
155 };
156 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
157