xref: /qemu/hw/vfio/container-base.c (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
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 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/error-report.h"
16 #include "hw/vfio/vfio-container-base.h"
17 
18 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
19                            hwaddr iova, ram_addr_t size,
20                            void *vaddr, bool readonly)
21 {
22     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
23 
24     g_assert(vioc->dma_map);
25     return vioc->dma_map(bcontainer, iova, size, vaddr, readonly);
26 }
27 
28 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
29                              hwaddr iova, ram_addr_t size,
30                              IOMMUTLBEntry *iotlb)
31 {
32     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
33 
34     g_assert(vioc->dma_unmap);
35     return vioc->dma_unmap(bcontainer, iova, size, iotlb);
36 }
37 
38 bool vfio_container_add_section_window(VFIOContainerBase *bcontainer,
39                                        MemoryRegionSection *section,
40                                        Error **errp)
41 {
42     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
43 
44     if (!vioc->add_window) {
45         return true;
46     }
47 
48     return vioc->add_window(bcontainer, section, errp);
49 }
50 
51 void vfio_container_del_section_window(VFIOContainerBase *bcontainer,
52                                        MemoryRegionSection *section)
53 {
54     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
55 
56     if (!vioc->del_window) {
57         return;
58     }
59 
60     return vioc->del_window(bcontainer, section);
61 }
62 
63 int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
64                                            bool start, Error **errp)
65 {
66     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
67     int ret;
68 
69     if (!bcontainer->dirty_pages_supported) {
70         return 0;
71     }
72 
73     g_assert(vioc->set_dirty_page_tracking);
74     if (bcontainer->dirty_pages_started == start) {
75         return 0;
76     }
77 
78     ret = vioc->set_dirty_page_tracking(bcontainer, start, errp);
79     if (!ret) {
80         bcontainer->dirty_pages_started = start;
81     }
82 
83     return ret;
84 }
85 
86 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
87                    VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
88 {
89     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
90 
91     g_assert(vioc->query_dirty_bitmap);
92     return vioc->query_dirty_bitmap(bcontainer, vbmap, iova, size,
93                                                errp);
94 }
95 
96 static gpointer copy_iova_range(gconstpointer src, gpointer data)
97 {
98      Range *source = (Range *)src;
99      Range *dest = g_new(Range, 1);
100 
101      range_set_bounds(dest, range_lob(source), range_upb(source));
102      return dest;
103 }
104 
105 GList *vfio_container_get_iova_ranges(const VFIOContainerBase *bcontainer)
106 {
107     assert(bcontainer);
108     return g_list_copy_deep(bcontainer->iova_ranges, copy_iova_range, NULL);
109 }
110 
111 static void vfio_container_instance_finalize(Object *obj)
112 {
113     VFIOContainerBase *bcontainer = VFIO_IOMMU(obj);
114     VFIOGuestIOMMU *giommu, *tmp;
115 
116     QLIST_SAFE_REMOVE(bcontainer, next);
117 
118     QLIST_FOREACH_SAFE(giommu, &bcontainer->giommu_list, giommu_next, tmp) {
119         memory_region_unregister_iommu_notifier(
120                 MEMORY_REGION(giommu->iommu_mr), &giommu->n);
121         QLIST_REMOVE(giommu, giommu_next);
122         g_free(giommu);
123     }
124 
125     g_list_free_full(bcontainer->iova_ranges, g_free);
126 }
127 
128 static void vfio_container_instance_init(Object *obj)
129 {
130     VFIOContainerBase *bcontainer = VFIO_IOMMU(obj);
131 
132     bcontainer->error = NULL;
133     bcontainer->dirty_pages_supported = false;
134     bcontainer->dma_max_mappings = 0;
135     bcontainer->iova_ranges = NULL;
136     QLIST_INIT(&bcontainer->giommu_list);
137     QLIST_INIT(&bcontainer->vrdl_list);
138 }
139 
140 static const TypeInfo types[] = {
141     {
142         .name = TYPE_VFIO_IOMMU,
143         .parent = TYPE_OBJECT,
144         .instance_init = vfio_container_instance_init,
145         .instance_finalize = vfio_container_instance_finalize,
146         .instance_size = sizeof(VFIOContainerBase),
147         .class_size = sizeof(VFIOIOMMUClass),
148         .abstract = true,
149     },
150 };
151 
152 DEFINE_TYPES(types)
153