1f61dddd7SZhenzhong Duan /*
2f61dddd7SZhenzhong Duan  * VFIO BASE CONTAINER
3f61dddd7SZhenzhong Duan  *
4f61dddd7SZhenzhong Duan  * Copyright (C) 2023 Intel Corporation.
5f61dddd7SZhenzhong Duan  * Copyright Red Hat, Inc. 2023
6f61dddd7SZhenzhong Duan  *
7f61dddd7SZhenzhong Duan  * Authors: Yi Liu <yi.l.liu@intel.com>
8f61dddd7SZhenzhong Duan  *          Eric Auger <eric.auger@redhat.com>
9f61dddd7SZhenzhong Duan  *
10f61dddd7SZhenzhong Duan  * SPDX-License-Identifier: GPL-2.0-or-later
11f61dddd7SZhenzhong Duan  */
12f61dddd7SZhenzhong Duan 
13f61dddd7SZhenzhong Duan #ifndef HW_VFIO_VFIO_CONTAINER_BASE_H
14f61dddd7SZhenzhong Duan #define HW_VFIO_VFIO_CONTAINER_BASE_H
15f61dddd7SZhenzhong Duan 
16f61dddd7SZhenzhong Duan #include "exec/memory.h"
17f61dddd7SZhenzhong Duan 
18f61dddd7SZhenzhong Duan typedef struct VFIODevice VFIODevice;
19*fdaa774eSCédric Le Goater typedef struct VFIOIOMMUClass VFIOIOMMUClass;
20*fdaa774eSCédric Le Goater #define VFIOIOMMUOps VFIOIOMMUClass /* To remove */
21f61dddd7SZhenzhong Duan 
22f61dddd7SZhenzhong Duan typedef struct {
23f61dddd7SZhenzhong Duan     unsigned long *bitmap;
24f61dddd7SZhenzhong Duan     hwaddr size;
25f61dddd7SZhenzhong Duan     hwaddr pages;
26f61dddd7SZhenzhong Duan } VFIOBitmap;
27f61dddd7SZhenzhong Duan 
28e5597063SEric Auger typedef struct VFIOAddressSpace {
29e5597063SEric Auger     AddressSpace *as;
30e5597063SEric Auger     QLIST_HEAD(, VFIOContainerBase) containers;
31e5597063SEric Auger     QLIST_ENTRY(VFIOAddressSpace) list;
32e5597063SEric Auger } VFIOAddressSpace;
33e5597063SEric Auger 
34f61dddd7SZhenzhong Duan /*
35f61dddd7SZhenzhong Duan  * This is the base object for vfio container backends
36f61dddd7SZhenzhong Duan  */
37f61dddd7SZhenzhong Duan typedef struct VFIOContainerBase {
38*fdaa774eSCédric Le Goater     const VFIOIOMMUClass *ops;
39e5597063SEric Auger     VFIOAddressSpace *space;
40c7b313d3SEric Auger     MemoryListener listener;
41c7b313d3SEric Auger     Error *error;
42c7b313d3SEric Auger     bool initialized;
434d6b9501SEric Auger     uint64_t dirty_pgsizes;
444d6b9501SEric Auger     uint64_t max_dirty_bitmap_size;
457ab1cb74SEric Auger     unsigned long pgsizes;
467ab1cb74SEric Auger     unsigned int dma_max_mappings;
47bb424490SEric Auger     bool dirty_pages_supported;
48dddf83abSEric Auger     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
49dc74a4b0SZhenzhong Duan     QLIST_HEAD(, VFIORamDiscardListener) vrdl_list;
50e5597063SEric Auger     QLIST_ENTRY(VFIOContainerBase) next;
513e6015d1SZhenzhong Duan     QLIST_HEAD(, VFIODevice) device_list;
52f79baf8cSZhenzhong Duan     GList *iova_ranges;
53f61dddd7SZhenzhong Duan } VFIOContainerBase;
54f61dddd7SZhenzhong Duan 
55dddf83abSEric Auger typedef struct VFIOGuestIOMMU {
56dddf83abSEric Auger     VFIOContainerBase *bcontainer;
57dddf83abSEric Auger     IOMMUMemoryRegion *iommu_mr;
58dddf83abSEric Auger     hwaddr iommu_offset;
59dddf83abSEric Auger     IOMMUNotifier n;
60dddf83abSEric Auger     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
61dddf83abSEric Auger } VFIOGuestIOMMU;
62dddf83abSEric Auger 
63dc74a4b0SZhenzhong Duan typedef struct VFIORamDiscardListener {
64dc74a4b0SZhenzhong Duan     VFIOContainerBase *bcontainer;
65dc74a4b0SZhenzhong Duan     MemoryRegion *mr;
66dc74a4b0SZhenzhong Duan     hwaddr offset_within_address_space;
67dc74a4b0SZhenzhong Duan     hwaddr size;
68dc74a4b0SZhenzhong Duan     uint64_t granularity;
69dc74a4b0SZhenzhong Duan     RamDiscardListener listener;
70dc74a4b0SZhenzhong Duan     QLIST_ENTRY(VFIORamDiscardListener) next;
71dc74a4b0SZhenzhong Duan } VFIORamDiscardListener;
72dc74a4b0SZhenzhong Duan 
73b08501a9SEric Auger int vfio_container_dma_map(VFIOContainerBase *bcontainer,
74b08501a9SEric Auger                            hwaddr iova, ram_addr_t size,
75b08501a9SEric Auger                            void *vaddr, bool readonly);
76b08501a9SEric Auger int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
77b08501a9SEric Auger                              hwaddr iova, ram_addr_t size,
78b08501a9SEric Auger                              IOMMUTLBEntry *iotlb);
79233309e8SZhenzhong Duan int vfio_container_add_section_window(VFIOContainerBase *bcontainer,
80233309e8SZhenzhong Duan                                       MemoryRegionSection *section,
81233309e8SZhenzhong Duan                                       Error **errp);
82233309e8SZhenzhong Duan void vfio_container_del_section_window(VFIOContainerBase *bcontainer,
83233309e8SZhenzhong Duan                                        MemoryRegionSection *section);
84bb424490SEric Auger int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
85bb424490SEric Auger                                            bool start);
864517c33cSZhenzhong Duan int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
87bb424490SEric Auger                                       VFIOBitmap *vbmap,
88bb424490SEric Auger                                       hwaddr iova, hwaddr size);
89b08501a9SEric Auger 
90ed2f7f80SZhenzhong Duan void vfio_container_init(VFIOContainerBase *bcontainer,
91e5597063SEric Auger                          VFIOAddressSpace *space,
92*fdaa774eSCédric Le Goater                          const VFIOIOMMUClass *ops);
93ed2f7f80SZhenzhong Duan void vfio_container_destroy(VFIOContainerBase *bcontainer);
94ed2f7f80SZhenzhong Duan 
95*fdaa774eSCédric Le Goater 
96*fdaa774eSCédric Le Goater #define TYPE_VFIO_IOMMU "vfio-iommu"
97*fdaa774eSCédric Le Goater 
98*fdaa774eSCédric Le Goater /*
99*fdaa774eSCédric Le Goater  * VFIOContainerBase is not an abstract QOM object because it felt
100*fdaa774eSCédric Le Goater  * unnecessary to expose all the IOMMU backends to the QEMU machine
101*fdaa774eSCédric Le Goater  * and human interface. However, we can still abstract the IOMMU
102*fdaa774eSCédric Le Goater  * backend handlers using a QOM interface class. This provides more
103*fdaa774eSCédric Le Goater  * flexibility when referencing the various implementations.
104*fdaa774eSCédric Le Goater  */
105*fdaa774eSCédric Le Goater DECLARE_CLASS_CHECKERS(VFIOIOMMUClass, VFIO_IOMMU, TYPE_VFIO_IOMMU)
106*fdaa774eSCédric Le Goater 
107*fdaa774eSCédric Le Goater struct VFIOIOMMUClass {
108*fdaa774eSCédric Le Goater     InterfaceClass parent_class;
109*fdaa774eSCédric Le Goater 
110f61dddd7SZhenzhong Duan     /* basic feature */
1114517c33cSZhenzhong Duan     int (*dma_map)(const VFIOContainerBase *bcontainer,
112f61dddd7SZhenzhong Duan                    hwaddr iova, ram_addr_t size,
113f61dddd7SZhenzhong Duan                    void *vaddr, bool readonly);
1144517c33cSZhenzhong Duan     int (*dma_unmap)(const VFIOContainerBase *bcontainer,
115f61dddd7SZhenzhong Duan                      hwaddr iova, ram_addr_t size,
116f61dddd7SZhenzhong Duan                      IOMMUTLBEntry *iotlb);
117f61dddd7SZhenzhong Duan     int (*attach_device)(const char *name, VFIODevice *vbasedev,
118f61dddd7SZhenzhong Duan                          AddressSpace *as, Error **errp);
119f61dddd7SZhenzhong Duan     void (*detach_device)(VFIODevice *vbasedev);
120f61dddd7SZhenzhong Duan     /* migration feature */
1214517c33cSZhenzhong Duan     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
1224517c33cSZhenzhong Duan                                    bool start);
1234517c33cSZhenzhong Duan     int (*query_dirty_bitmap)(const VFIOContainerBase *bcontainer,
1244517c33cSZhenzhong Duan                               VFIOBitmap *vbmap,
125f61dddd7SZhenzhong Duan                               hwaddr iova, hwaddr size);
126c328e7e8SZhenzhong Duan     /* PCI specific */
127c328e7e8SZhenzhong Duan     int (*pci_hot_reset)(VFIODevice *vbasedev, bool single);
128c328e7e8SZhenzhong Duan 
1299b7d38bfSZhenzhong Duan     /* SPAPR specific */
1309b7d38bfSZhenzhong Duan     int (*add_window)(VFIOContainerBase *bcontainer,
1319b7d38bfSZhenzhong Duan                       MemoryRegionSection *section,
1329b7d38bfSZhenzhong Duan                       Error **errp);
1339b7d38bfSZhenzhong Duan     void (*del_window)(VFIOContainerBase *bcontainer,
1349b7d38bfSZhenzhong Duan                        MemoryRegionSection *section);
135001a013eSCédric Le Goater     void (*release)(VFIOContainerBase *bcontainer);
136f61dddd7SZhenzhong Duan };
137f61dddd7SZhenzhong Duan #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
138