blob: 760d9d0622b2e847ecb3368c88df772efb06043f [file] [log] [blame]
Eric Augerb08501a2023-11-02 15:12:29 +08001/*
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
18int 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
26int 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}
Zhenzhong Duaned2f7f82023-11-02 15:12:30 +080033
Zhenzhong Duan33e4c222024-05-07 14:42:46 +080034bool vfio_container_add_section_window(VFIOContainerBase *bcontainer,
35 MemoryRegionSection *section,
36 Error **errp)
Zhenzhong Duan233309e2023-11-02 15:12:43 +080037{
38 if (!bcontainer->ops->add_window) {
Zhenzhong Duan33e4c222024-05-07 14:42:46 +080039 return true;
Zhenzhong Duan233309e2023-11-02 15:12:43 +080040 }
41
42 return bcontainer->ops->add_window(bcontainer, section, errp);
43}
44
45void 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
Eric Augerbb424492023-11-02 15:12:33 +080055int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
Cédric Le Goater836bb302024-05-16 14:46:50 +020056 bool start, Error **errp)
Eric Augerbb424492023-11-02 15:12:33 +080057{
Zhenzhong Duan36e84d02023-11-21 16:44:04 +080058 if (!bcontainer->dirty_pages_supported) {
59 return 0;
60 }
61
Eric Augerbb424492023-11-02 15:12:33 +080062 g_assert(bcontainer->ops->set_dirty_page_tracking);
Cédric Le Goater836bb302024-05-16 14:46:50 +020063 return bcontainer->ops->set_dirty_page_tracking(bcontainer, start, errp);
Eric Augerbb424492023-11-02 15:12:33 +080064}
65
Zhenzhong Duan4517c332023-11-21 16:44:17 +080066int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
Cédric Le Goater2da5f9e2024-05-16 14:46:57 +020067 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
Eric Augerbb424492023-11-02 15:12:33 +080068{
69 g_assert(bcontainer->ops->query_dirty_bitmap);
Cédric Le Goater2da5f9e2024-05-16 14:46:57 +020070 return bcontainer->ops->query_dirty_bitmap(bcontainer, vbmap, iova, size,
71 errp);
Eric Augerbb424492023-11-02 15:12:33 +080072}
73
Eric Augere5597062023-11-02 15:12:32 +080074void vfio_container_init(VFIOContainerBase *bcontainer, VFIOAddressSpace *space,
Cédric Le Goaterfdaa7742023-12-19 07:58:19 +010075 const VFIOIOMMUClass *ops)
Zhenzhong Duaned2f7f82023-11-02 15:12:30 +080076{
77 bcontainer->ops = ops;
Eric Augere5597062023-11-02 15:12:32 +080078 bcontainer->space = space;
Eric Augerc7b313d2023-11-02 15:12:38 +080079 bcontainer->error = NULL;
Eric Augerbb424492023-11-02 15:12:33 +080080 bcontainer->dirty_pages_supported = false;
Eric Auger7ab1cb72023-11-02 15:12:36 +080081 bcontainer->dma_max_mappings = 0;
Zhenzhong Duanf79baf82023-11-02 15:12:40 +080082 bcontainer->iova_ranges = NULL;
Eric Augerdddf83a2023-11-02 15:12:31 +080083 QLIST_INIT(&bcontainer->giommu_list);
Zhenzhong Duandc74a4b2023-11-02 15:12:37 +080084 QLIST_INIT(&bcontainer->vrdl_list);
Zhenzhong Duaned2f7f82023-11-02 15:12:30 +080085}
86
87void vfio_container_destroy(VFIOContainerBase *bcontainer)
88{
Eric Augerdddf83a2023-11-02 15:12:31 +080089 VFIOGuestIOMMU *giommu, *tmp;
90
Eric Augere5597062023-11-02 15:12:32 +080091 QLIST_REMOVE(bcontainer, next);
92
Eric Augerdddf83a2023-11-02 15:12:31 +080093 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 }
Zhenzhong Duanf79baf82023-11-02 15:12:40 +080099
100 g_list_free_full(bcontainer->iova_ranges, g_free);
Zhenzhong Duaned2f7f82023-11-02 15:12:30 +0800101}
Cédric Le Goaterfdaa7742023-12-19 07:58:19 +0100102
103static const TypeInfo types[] = {
104 {
105 .name = TYPE_VFIO_IOMMU,
106 .parent = TYPE_INTERFACE,
107 .class_size = sizeof(VFIOIOMMUClass),
108 },
109};
110
111DEFINE_TYPES(types)