xref: /openbmc/qemu/hw/vfio/container-base.c (revision 233309e8)
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 int vfio_container_add_section_window(VFIOContainerBase *bcontainer,
35                                       MemoryRegionSection *section,
36                                       Error **errp)
37 {
38     if (!bcontainer->ops->add_window) {
39         return 0;
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)
57 {
58     g_assert(bcontainer->ops->set_dirty_page_tracking);
59     return bcontainer->ops->set_dirty_page_tracking(bcontainer, start);
60 }
61 
62 int vfio_container_query_dirty_bitmap(VFIOContainerBase *bcontainer,
63                                       VFIOBitmap *vbmap,
64                                       hwaddr iova, hwaddr size)
65 {
66     g_assert(bcontainer->ops->query_dirty_bitmap);
67     return bcontainer->ops->query_dirty_bitmap(bcontainer, vbmap, iova, size);
68 }
69 
70 void vfio_container_init(VFIOContainerBase *bcontainer, VFIOAddressSpace *space,
71                          const VFIOIOMMUOps *ops)
72 {
73     bcontainer->ops = ops;
74     bcontainer->space = space;
75     bcontainer->error = NULL;
76     bcontainer->dirty_pages_supported = false;
77     bcontainer->dma_max_mappings = 0;
78     bcontainer->iova_ranges = NULL;
79     QLIST_INIT(&bcontainer->giommu_list);
80     QLIST_INIT(&bcontainer->vrdl_list);
81 }
82 
83 void vfio_container_destroy(VFIOContainerBase *bcontainer)
84 {
85     VFIOGuestIOMMU *giommu, *tmp;
86 
87     QLIST_REMOVE(bcontainer, next);
88 
89     QLIST_FOREACH_SAFE(giommu, &bcontainer->giommu_list, giommu_next, tmp) {
90         memory_region_unregister_iommu_notifier(
91                 MEMORY_REGION(giommu->iommu_mr), &giommu->n);
92         QLIST_REMOVE(giommu, giommu_next);
93         g_free(giommu);
94     }
95 
96     g_list_free_full(bcontainer->iova_ranges, g_free);
97 }
98