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;
19f61dddd7SZhenzhong Duan typedef struct VFIOIOMMUOps VFIOIOMMUOps;
20f61dddd7SZhenzhong Duan 
21f61dddd7SZhenzhong Duan typedef struct {
22f61dddd7SZhenzhong Duan     unsigned long *bitmap;
23f61dddd7SZhenzhong Duan     hwaddr size;
24f61dddd7SZhenzhong Duan     hwaddr pages;
25f61dddd7SZhenzhong Duan } VFIOBitmap;
26f61dddd7SZhenzhong Duan 
27e5597063SEric Auger typedef struct VFIOAddressSpace {
28e5597063SEric Auger     AddressSpace *as;
29e5597063SEric Auger     QLIST_HEAD(, VFIOContainerBase) containers;
30e5597063SEric Auger     QLIST_ENTRY(VFIOAddressSpace) list;
31e5597063SEric Auger } VFIOAddressSpace;
32e5597063SEric Auger 
33f61dddd7SZhenzhong Duan /*
34f61dddd7SZhenzhong Duan  * This is the base object for vfio container backends
35f61dddd7SZhenzhong Duan  */
36f61dddd7SZhenzhong Duan typedef struct VFIOContainerBase {
37f61dddd7SZhenzhong Duan     const VFIOIOMMUOps *ops;
38e5597063SEric Auger     VFIOAddressSpace *space;
39c7b313d3SEric Auger     MemoryListener listener;
40c7b313d3SEric Auger     Error *error;
41c7b313d3SEric Auger     bool initialized;
424d6b9501SEric Auger     uint64_t dirty_pgsizes;
434d6b9501SEric Auger     uint64_t max_dirty_bitmap_size;
447ab1cb74SEric Auger     unsigned long pgsizes;
457ab1cb74SEric Auger     unsigned int dma_max_mappings;
46bb424490SEric Auger     bool dirty_pages_supported;
47dddf83abSEric Auger     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
48dc74a4b0SZhenzhong Duan     QLIST_HEAD(, VFIORamDiscardListener) vrdl_list;
49e5597063SEric Auger     QLIST_ENTRY(VFIOContainerBase) next;
503e6015d1SZhenzhong Duan     QLIST_HEAD(, VFIODevice) device_list;
51*f79baf8cSZhenzhong Duan     GList *iova_ranges;
52f61dddd7SZhenzhong Duan } VFIOContainerBase;
53f61dddd7SZhenzhong Duan 
54dddf83abSEric Auger typedef struct VFIOGuestIOMMU {
55dddf83abSEric Auger     VFIOContainerBase *bcontainer;
56dddf83abSEric Auger     IOMMUMemoryRegion *iommu_mr;
57dddf83abSEric Auger     hwaddr iommu_offset;
58dddf83abSEric Auger     IOMMUNotifier n;
59dddf83abSEric Auger     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
60dddf83abSEric Auger } VFIOGuestIOMMU;
61dddf83abSEric Auger 
62dc74a4b0SZhenzhong Duan typedef struct VFIORamDiscardListener {
63dc74a4b0SZhenzhong Duan     VFIOContainerBase *bcontainer;
64dc74a4b0SZhenzhong Duan     MemoryRegion *mr;
65dc74a4b0SZhenzhong Duan     hwaddr offset_within_address_space;
66dc74a4b0SZhenzhong Duan     hwaddr size;
67dc74a4b0SZhenzhong Duan     uint64_t granularity;
68dc74a4b0SZhenzhong Duan     RamDiscardListener listener;
69dc74a4b0SZhenzhong Duan     QLIST_ENTRY(VFIORamDiscardListener) next;
70dc74a4b0SZhenzhong Duan } VFIORamDiscardListener;
71dc74a4b0SZhenzhong Duan 
72b08501a9SEric Auger int vfio_container_dma_map(VFIOContainerBase *bcontainer,
73b08501a9SEric Auger                            hwaddr iova, ram_addr_t size,
74b08501a9SEric Auger                            void *vaddr, bool readonly);
75b08501a9SEric Auger int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
76b08501a9SEric Auger                              hwaddr iova, ram_addr_t size,
77b08501a9SEric Auger                              IOMMUTLBEntry *iotlb);
78bb424490SEric Auger int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
79bb424490SEric Auger                                            bool start);
80bb424490SEric Auger int vfio_container_query_dirty_bitmap(VFIOContainerBase *bcontainer,
81bb424490SEric Auger                                       VFIOBitmap *vbmap,
82bb424490SEric Auger                                       hwaddr iova, hwaddr size);
83b08501a9SEric Auger 
84ed2f7f80SZhenzhong Duan void vfio_container_init(VFIOContainerBase *bcontainer,
85e5597063SEric Auger                          VFIOAddressSpace *space,
86ed2f7f80SZhenzhong Duan                          const VFIOIOMMUOps *ops);
87ed2f7f80SZhenzhong Duan void vfio_container_destroy(VFIOContainerBase *bcontainer);
88ed2f7f80SZhenzhong Duan 
89f61dddd7SZhenzhong Duan struct VFIOIOMMUOps {
90f61dddd7SZhenzhong Duan     /* basic feature */
91f61dddd7SZhenzhong Duan     int (*dma_map)(VFIOContainerBase *bcontainer,
92f61dddd7SZhenzhong Duan                    hwaddr iova, ram_addr_t size,
93f61dddd7SZhenzhong Duan                    void *vaddr, bool readonly);
94f61dddd7SZhenzhong Duan     int (*dma_unmap)(VFIOContainerBase *bcontainer,
95f61dddd7SZhenzhong Duan                      hwaddr iova, ram_addr_t size,
96f61dddd7SZhenzhong Duan                      IOMMUTLBEntry *iotlb);
97f61dddd7SZhenzhong Duan     int (*attach_device)(const char *name, VFIODevice *vbasedev,
98f61dddd7SZhenzhong Duan                          AddressSpace *as, Error **errp);
99f61dddd7SZhenzhong Duan     void (*detach_device)(VFIODevice *vbasedev);
100f61dddd7SZhenzhong Duan     /* migration feature */
101f61dddd7SZhenzhong Duan     int (*set_dirty_page_tracking)(VFIOContainerBase *bcontainer, bool start);
102f61dddd7SZhenzhong Duan     int (*query_dirty_bitmap)(VFIOContainerBase *bcontainer, VFIOBitmap *vbmap,
103f61dddd7SZhenzhong Duan                               hwaddr iova, hwaddr size);
104f61dddd7SZhenzhong Duan };
105f61dddd7SZhenzhong Duan #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
106