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