xref: /qemu/include/hw/vfio/vfio-container-base.h (revision 5db05230)
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     GList *iova_ranges;
52 } VFIOContainerBase;
53 
54 typedef struct VFIOGuestIOMMU {
55     VFIOContainerBase *bcontainer;
56     IOMMUMemoryRegion *iommu_mr;
57     hwaddr iommu_offset;
58     IOMMUNotifier n;
59     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
60 } VFIOGuestIOMMU;
61 
62 typedef struct VFIORamDiscardListener {
63     VFIOContainerBase *bcontainer;
64     MemoryRegion *mr;
65     hwaddr offset_within_address_space;
66     hwaddr size;
67     uint64_t granularity;
68     RamDiscardListener listener;
69     QLIST_ENTRY(VFIORamDiscardListener) next;
70 } VFIORamDiscardListener;
71 
72 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
73                            hwaddr iova, ram_addr_t size,
74                            void *vaddr, bool readonly);
75 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
76                              hwaddr iova, ram_addr_t size,
77                              IOMMUTLBEntry *iotlb);
78 int vfio_container_add_section_window(VFIOContainerBase *bcontainer,
79                                       MemoryRegionSection *section,
80                                       Error **errp);
81 void vfio_container_del_section_window(VFIOContainerBase *bcontainer,
82                                        MemoryRegionSection *section);
83 int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
84                                            bool start);
85 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
86                                       VFIOBitmap *vbmap,
87                                       hwaddr iova, hwaddr size);
88 
89 void vfio_container_init(VFIOContainerBase *bcontainer,
90                          VFIOAddressSpace *space,
91                          const VFIOIOMMUOps *ops);
92 void vfio_container_destroy(VFIOContainerBase *bcontainer);
93 
94 struct VFIOIOMMUOps {
95     /* basic feature */
96     int (*dma_map)(const VFIOContainerBase *bcontainer,
97                    hwaddr iova, ram_addr_t size,
98                    void *vaddr, bool readonly);
99     int (*dma_unmap)(const VFIOContainerBase *bcontainer,
100                      hwaddr iova, ram_addr_t size,
101                      IOMMUTLBEntry *iotlb);
102     int (*attach_device)(const char *name, VFIODevice *vbasedev,
103                          AddressSpace *as, Error **errp);
104     void (*detach_device)(VFIODevice *vbasedev);
105     /* migration feature */
106     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
107                                    bool start);
108     int (*query_dirty_bitmap)(const VFIOContainerBase *bcontainer,
109                               VFIOBitmap *vbmap,
110                               hwaddr iova, hwaddr size);
111     /* PCI specific */
112     int (*pci_hot_reset)(VFIODevice *vbasedev, bool single);
113 
114     /* SPAPR specific */
115     int (*add_window)(VFIOContainerBase *bcontainer,
116                       MemoryRegionSection *section,
117                       Error **errp);
118     void (*del_window)(VFIOContainerBase *bcontainer,
119                        MemoryRegionSection *section);
120 };
121 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */
122