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;
39bb424490SEric Auger     bool dirty_pages_supported;
40dddf83abSEric Auger     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
41e5597063SEric Auger     QLIST_ENTRY(VFIOContainerBase) next;
42*3e6015d1SZhenzhong Duan     QLIST_HEAD(, VFIODevice) device_list;
43f61dddd7SZhenzhong Duan } VFIOContainerBase;
44f61dddd7SZhenzhong Duan 
45dddf83abSEric Auger typedef struct VFIOGuestIOMMU {
46dddf83abSEric Auger     VFIOContainerBase *bcontainer;
47dddf83abSEric Auger     IOMMUMemoryRegion *iommu_mr;
48dddf83abSEric Auger     hwaddr iommu_offset;
49dddf83abSEric Auger     IOMMUNotifier n;
50dddf83abSEric Auger     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
51dddf83abSEric Auger } VFIOGuestIOMMU;
52dddf83abSEric Auger 
53b08501a9SEric Auger int vfio_container_dma_map(VFIOContainerBase *bcontainer,
54b08501a9SEric Auger                            hwaddr iova, ram_addr_t size,
55b08501a9SEric Auger                            void *vaddr, bool readonly);
56b08501a9SEric Auger int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
57b08501a9SEric Auger                              hwaddr iova, ram_addr_t size,
58b08501a9SEric Auger                              IOMMUTLBEntry *iotlb);
59bb424490SEric Auger int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
60bb424490SEric Auger                                            bool start);
61bb424490SEric Auger int vfio_container_query_dirty_bitmap(VFIOContainerBase *bcontainer,
62bb424490SEric Auger                                       VFIOBitmap *vbmap,
63bb424490SEric Auger                                       hwaddr iova, hwaddr size);
64b08501a9SEric Auger 
65ed2f7f80SZhenzhong Duan void vfio_container_init(VFIOContainerBase *bcontainer,
66e5597063SEric Auger                          VFIOAddressSpace *space,
67ed2f7f80SZhenzhong Duan                          const VFIOIOMMUOps *ops);
68ed2f7f80SZhenzhong Duan void vfio_container_destroy(VFIOContainerBase *bcontainer);
69ed2f7f80SZhenzhong Duan 
70f61dddd7SZhenzhong Duan struct VFIOIOMMUOps {
71f61dddd7SZhenzhong Duan     /* basic feature */
72f61dddd7SZhenzhong Duan     int (*dma_map)(VFIOContainerBase *bcontainer,
73f61dddd7SZhenzhong Duan                    hwaddr iova, ram_addr_t size,
74f61dddd7SZhenzhong Duan                    void *vaddr, bool readonly);
75f61dddd7SZhenzhong Duan     int (*dma_unmap)(VFIOContainerBase *bcontainer,
76f61dddd7SZhenzhong Duan                      hwaddr iova, ram_addr_t size,
77f61dddd7SZhenzhong Duan                      IOMMUTLBEntry *iotlb);
78f61dddd7SZhenzhong Duan     int (*attach_device)(const char *name, VFIODevice *vbasedev,
79f61dddd7SZhenzhong Duan                          AddressSpace *as, Error **errp);
80f61dddd7SZhenzhong Duan     void (*detach_device)(VFIODevice *vbasedev);
81f61dddd7SZhenzhong Duan     /* migration feature */
82f61dddd7SZhenzhong Duan     int (*set_dirty_page_tracking)(VFIOContainerBase *bcontainer, bool start);
83f61dddd7SZhenzhong Duan     int (*query_dirty_bitmap)(VFIOContainerBase *bcontainer, VFIOBitmap *vbmap,
84f61dddd7SZhenzhong Duan                               hwaddr iova, hwaddr size);
85f61dddd7SZhenzhong Duan };
86f61dddd7SZhenzhong Duan #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
87