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