xref: /openbmc/qemu/hw/vfio/container-base.c (revision 2137d2fd)
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     g_assert(bcontainer->ops->dma_map);
23     return bcontainer->ops->dma_map(bcontainer, iova, size, vaddr, readonly);
24 }
25 
26 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
27                              hwaddr iova, ram_addr_t size,
28                              IOMMUTLBEntry *iotlb)
29 {
30     g_assert(bcontainer->ops->dma_unmap);
31     return bcontainer->ops->dma_unmap(bcontainer, iova, size, iotlb);
32 }
33 
34 bool vfio_container_add_section_window(VFIOContainerBase *bcontainer,
35                                        MemoryRegionSection *section,
36                                        Error **errp)
37 {
38     if (!bcontainer->ops->add_window) {
39         return true;
40     }
41 
42     return bcontainer->ops->add_window(bcontainer, section, errp);
43 }
44 
45 void vfio_container_del_section_window(VFIOContainerBase *bcontainer,
46                                        MemoryRegionSection *section)
47 {
48     if (!bcontainer->ops->del_window) {
49         return;
50     }
51 
52     return bcontainer->ops->del_window(bcontainer, section);
53 }
54 
55 int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
56                                            bool start, Error **errp)
57 {
58     if (!bcontainer->dirty_pages_supported) {
59         return 0;
60     }
61 
62     g_assert(bcontainer->ops->set_dirty_page_tracking);
63     return bcontainer->ops->set_dirty_page_tracking(bcontainer, start, errp);
64 }
65 
66 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
67                    VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
68 {
69     g_assert(bcontainer->ops->query_dirty_bitmap);
70     return bcontainer->ops->query_dirty_bitmap(bcontainer, vbmap, iova, size,
71                                                errp);
72 }
73 
74 void vfio_container_init(VFIOContainerBase *bcontainer,
75                          const VFIOIOMMUClass *ops)
76 {
77     bcontainer->ops = ops;
78 }
79 
80 void vfio_container_destroy(VFIOContainerBase *bcontainer)
81 {
82     VFIOGuestIOMMU *giommu, *tmp;
83 
84     QLIST_REMOVE(bcontainer, next);
85 
86     QLIST_FOREACH_SAFE(giommu, &bcontainer->giommu_list, giommu_next, tmp) {
87         memory_region_unregister_iommu_notifier(
88                 MEMORY_REGION(giommu->iommu_mr), &giommu->n);
89         QLIST_REMOVE(giommu, giommu_next);
90         g_free(giommu);
91     }
92 
93     g_list_free_full(bcontainer->iova_ranges, g_free);
94 }
95 
96 static void vfio_container_instance_init(Object *obj)
97 {
98     VFIOContainerBase *bcontainer = VFIO_IOMMU(obj);
99 
100     bcontainer->error = NULL;
101     bcontainer->dirty_pages_supported = false;
102     bcontainer->dma_max_mappings = 0;
103     bcontainer->iova_ranges = NULL;
104     QLIST_INIT(&bcontainer->giommu_list);
105     QLIST_INIT(&bcontainer->vrdl_list);
106 }
107 
108 static const TypeInfo types[] = {
109     {
110         .name = TYPE_VFIO_IOMMU,
111         .parent = TYPE_OBJECT,
112         .instance_init = vfio_container_instance_init,
113         .instance_size = sizeof(VFIOContainerBase),
114         .class_size = sizeof(VFIOIOMMUClass),
115         .abstract = true,
116     },
117 };
118 
119 DEFINE_TYPES(types)
120