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 VFIOIOMMUOps VFIOIOMMUOps;
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     const VFIOIOMMUOps *ops;
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 } VFIOContainerBase;
52 
53 typedef struct VFIOGuestIOMMU {
54     VFIOContainerBase *bcontainer;
55     IOMMUMemoryRegion *iommu_mr;
56     hwaddr iommu_offset;
57     IOMMUNotifier n;
58     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
59 } VFIOGuestIOMMU;
60 
61 typedef struct VFIORamDiscardListener {
62     VFIOContainerBase *bcontainer;
63     MemoryRegion *mr;
64     hwaddr offset_within_address_space;
65     hwaddr size;
66     uint64_t granularity;
67     RamDiscardListener listener;
68     QLIST_ENTRY(VFIORamDiscardListener) next;
69 } VFIORamDiscardListener;
70 
71 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
72                            hwaddr iova, ram_addr_t size,
73                            void *vaddr, bool readonly);
74 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
75                              hwaddr iova, ram_addr_t size,
76                              IOMMUTLBEntry *iotlb);
77 int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
78                                            bool start);
79 int vfio_container_query_dirty_bitmap(VFIOContainerBase *bcontainer,
80                                       VFIOBitmap *vbmap,
81                                       hwaddr iova, hwaddr size);
82 
83 void vfio_container_init(VFIOContainerBase *bcontainer,
84                          VFIOAddressSpace *space,
85                          const VFIOIOMMUOps *ops);
86 void vfio_container_destroy(VFIOContainerBase *bcontainer);
87 
88 struct VFIOIOMMUOps {
89     /* basic feature */
90     int (*dma_map)(VFIOContainerBase *bcontainer,
91                    hwaddr iova, ram_addr_t size,
92                    void *vaddr, bool readonly);
93     int (*dma_unmap)(VFIOContainerBase *bcontainer,
94                      hwaddr iova, ram_addr_t size,
95                      IOMMUTLBEntry *iotlb);
96     int (*attach_device)(const char *name, VFIODevice *vbasedev,
97                          AddressSpace *as, Error **errp);
98     void (*detach_device)(VFIODevice *vbasedev);
99     /* migration feature */
100     int (*set_dirty_page_tracking)(VFIOContainerBase *bcontainer, bool start);
101     int (*query_dirty_bitmap)(VFIOContainerBase *bcontainer, VFIOBitmap *vbmap,
102                               hwaddr iova, hwaddr size);
103 };
104 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
105