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