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 #define VFIOIOMMUOps VFIOIOMMUClass /* To remove */
21 
22 typedef struct {
23     unsigned long *bitmap;
24     hwaddr size;
25     hwaddr pages;
26 } VFIOBitmap;
27 
28 typedef struct VFIOAddressSpace {
29     AddressSpace *as;
30     QLIST_HEAD(, VFIOContainerBase) containers;
31     QLIST_ENTRY(VFIOAddressSpace) list;
32 } VFIOAddressSpace;
33 
34 /*
35  * This is the base object for vfio container backends
36  */
37 typedef struct VFIOContainerBase {
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 } 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 int 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);
86 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
87                                       VFIOBitmap *vbmap,
88                                       hwaddr iova, hwaddr size);
89 
90 void vfio_container_init(VFIOContainerBase *bcontainer,
91                          VFIOAddressSpace *space,
92                          const VFIOIOMMUClass *ops);
93 void vfio_container_destroy(VFIOContainerBase *bcontainer);
94 
95 
96 #define TYPE_VFIO_IOMMU "vfio-iommu"
97 #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
98 
99 /*
100  * VFIOContainerBase is not an abstract QOM object because it felt
101  * unnecessary to expose all the IOMMU backends to the QEMU machine
102  * and human interface. However, we can still abstract the IOMMU
103  * backend handlers using a QOM interface class. This provides more
104  * flexibility when referencing the various implementations.
105  */
106 DECLARE_CLASS_CHECKERS(VFIOIOMMUClass, VFIO_IOMMU, TYPE_VFIO_IOMMU)
107 
108 struct VFIOIOMMUClass {
109     InterfaceClass parent_class;
110 
111     /* basic feature */
112     int (*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     int (*attach_device)(const char *name, VFIODevice *vbasedev,
120                          AddressSpace *as, Error **errp);
121     void (*detach_device)(VFIODevice *vbasedev);
122     /* migration feature */
123     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
124                                    bool start);
125     int (*query_dirty_bitmap)(const VFIOContainerBase *bcontainer,
126                               VFIOBitmap *vbmap,
127                               hwaddr iova, hwaddr size);
128     /* PCI specific */
129     int (*pci_hot_reset)(VFIODevice *vbasedev, bool single);
130 
131     /* SPAPR specific */
132     int (*add_window)(VFIOContainerBase *bcontainer,
133                       MemoryRegionSection *section,
134                       Error **errp);
135     void (*del_window)(VFIOContainerBase *bcontainer,
136                        MemoryRegionSection *section);
137     void (*release)(VFIOContainerBase *bcontainer);
138 };
139 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
140