xref: /openbmc/qemu/hw/vfio/container-base.c (revision 96b7af43)
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 
68     if (!bcontainer->dirty_pages_supported) {
69         return 0;
70     }
71 
72     g_assert(vioc->set_dirty_page_tracking);
73     return vioc->set_dirty_page_tracking(bcontainer, start, errp);
74 }
75 
76 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
77                    VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
78 {
79     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
80 
81     g_assert(vioc->query_dirty_bitmap);
82     return vioc->query_dirty_bitmap(bcontainer, vbmap, iova, size,
83                                                errp);
84 }
85 
86 static void vfio_container_instance_finalize(Object *obj)
87 {
88     VFIOContainerBase *bcontainer = VFIO_IOMMU(obj);
89     VFIOGuestIOMMU *giommu, *tmp;
90 
91     QLIST_REMOVE(bcontainer, next);
92 
93     QLIST_FOREACH_SAFE(giommu, &bcontainer->giommu_list, giommu_next, tmp) {
94         memory_region_unregister_iommu_notifier(
95                 MEMORY_REGION(giommu->iommu_mr), &giommu->n);
96         QLIST_REMOVE(giommu, giommu_next);
97         g_free(giommu);
98     }
99 
100     g_list_free_full(bcontainer->iova_ranges, g_free);
101 }
102 
103 static void vfio_container_instance_init(Object *obj)
104 {
105     VFIOContainerBase *bcontainer = VFIO_IOMMU(obj);
106 
107     bcontainer->error = NULL;
108     bcontainer->dirty_pages_supported = false;
109     bcontainer->dma_max_mappings = 0;
110     bcontainer->iova_ranges = NULL;
111     QLIST_INIT(&bcontainer->giommu_list);
112     QLIST_INIT(&bcontainer->vrdl_list);
113 }
114 
115 static const TypeInfo types[] = {
116     {
117         .name = TYPE_VFIO_IOMMU,
118         .parent = TYPE_OBJECT,
119         .instance_init = vfio_container_instance_init,
120         .instance_finalize = vfio_container_instance_finalize,
121         .instance_size = sizeof(VFIOContainerBase),
122         .class_size = sizeof(VFIOIOMMUClass),
123         .abstract = true,
124     },
125 };
126 
127 DEFINE_TYPES(types)
128