Eric Auger | b08501a | 2023-11-02 15:12:29 +0800 | [diff] [blame] | 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 | } |
Zhenzhong Duan | ed2f7f8 | 2023-11-02 15:12:30 +0800 | [diff] [blame] | 33 | |
Zhenzhong Duan | 33e4c22 | 2024-05-07 14:42:46 +0800 | [diff] [blame] | 34 | bool vfio_container_add_section_window(VFIOContainerBase *bcontainer, |
| 35 | MemoryRegionSection *section, |
| 36 | Error **errp) |
Zhenzhong Duan | 233309e | 2023-11-02 15:12:43 +0800 | [diff] [blame] | 37 | { |
| 38 | if (!bcontainer->ops->add_window) { |
Zhenzhong Duan | 33e4c22 | 2024-05-07 14:42:46 +0800 | [diff] [blame] | 39 | return true; |
Zhenzhong Duan | 233309e | 2023-11-02 15:12:43 +0800 | [diff] [blame] | 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 | |
Eric Auger | bb42449 | 2023-11-02 15:12:33 +0800 | [diff] [blame] | 55 | int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer, |
Cédric Le Goater | 836bb30 | 2024-05-16 14:46:50 +0200 | [diff] [blame] | 56 | bool start, Error **errp) |
Eric Auger | bb42449 | 2023-11-02 15:12:33 +0800 | [diff] [blame] | 57 | { |
Zhenzhong Duan | 36e84d0 | 2023-11-21 16:44:04 +0800 | [diff] [blame] | 58 | if (!bcontainer->dirty_pages_supported) { |
| 59 | return 0; |
| 60 | } |
| 61 | |
Eric Auger | bb42449 | 2023-11-02 15:12:33 +0800 | [diff] [blame] | 62 | g_assert(bcontainer->ops->set_dirty_page_tracking); |
Cédric Le Goater | 836bb30 | 2024-05-16 14:46:50 +0200 | [diff] [blame] | 63 | return bcontainer->ops->set_dirty_page_tracking(bcontainer, start, errp); |
Eric Auger | bb42449 | 2023-11-02 15:12:33 +0800 | [diff] [blame] | 64 | } |
| 65 | |
Zhenzhong Duan | 4517c33 | 2023-11-21 16:44:17 +0800 | [diff] [blame] | 66 | int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer, |
Cédric Le Goater | 2da5f9e | 2024-05-16 14:46:57 +0200 | [diff] [blame] | 67 | VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp) |
Eric Auger | bb42449 | 2023-11-02 15:12:33 +0800 | [diff] [blame] | 68 | { |
| 69 | g_assert(bcontainer->ops->query_dirty_bitmap); |
Cédric Le Goater | 2da5f9e | 2024-05-16 14:46:57 +0200 | [diff] [blame] | 70 | return bcontainer->ops->query_dirty_bitmap(bcontainer, vbmap, iova, size, |
| 71 | errp); |
Eric Auger | bb42449 | 2023-11-02 15:12:33 +0800 | [diff] [blame] | 72 | } |
| 73 | |
Eric Auger | e559706 | 2023-11-02 15:12:32 +0800 | [diff] [blame] | 74 | void vfio_container_init(VFIOContainerBase *bcontainer, VFIOAddressSpace *space, |
Cédric Le Goater | fdaa774 | 2023-12-19 07:58:19 +0100 | [diff] [blame] | 75 | const VFIOIOMMUClass *ops) |
Zhenzhong Duan | ed2f7f8 | 2023-11-02 15:12:30 +0800 | [diff] [blame] | 76 | { |
| 77 | bcontainer->ops = ops; |
Eric Auger | e559706 | 2023-11-02 15:12:32 +0800 | [diff] [blame] | 78 | bcontainer->space = space; |
Eric Auger | c7b313d | 2023-11-02 15:12:38 +0800 | [diff] [blame] | 79 | bcontainer->error = NULL; |
Eric Auger | bb42449 | 2023-11-02 15:12:33 +0800 | [diff] [blame] | 80 | bcontainer->dirty_pages_supported = false; |
Eric Auger | 7ab1cb7 | 2023-11-02 15:12:36 +0800 | [diff] [blame] | 81 | bcontainer->dma_max_mappings = 0; |
Zhenzhong Duan | f79baf8 | 2023-11-02 15:12:40 +0800 | [diff] [blame] | 82 | bcontainer->iova_ranges = NULL; |
Eric Auger | dddf83a | 2023-11-02 15:12:31 +0800 | [diff] [blame] | 83 | QLIST_INIT(&bcontainer->giommu_list); |
Zhenzhong Duan | dc74a4b | 2023-11-02 15:12:37 +0800 | [diff] [blame] | 84 | QLIST_INIT(&bcontainer->vrdl_list); |
Zhenzhong Duan | ed2f7f8 | 2023-11-02 15:12:30 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void vfio_container_destroy(VFIOContainerBase *bcontainer) |
| 88 | { |
Eric Auger | dddf83a | 2023-11-02 15:12:31 +0800 | [diff] [blame] | 89 | VFIOGuestIOMMU *giommu, *tmp; |
| 90 | |
Eric Auger | e559706 | 2023-11-02 15:12:32 +0800 | [diff] [blame] | 91 | QLIST_REMOVE(bcontainer, next); |
| 92 | |
Eric Auger | dddf83a | 2023-11-02 15:12:31 +0800 | [diff] [blame] | 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 | } |
Zhenzhong Duan | f79baf8 | 2023-11-02 15:12:40 +0800 | [diff] [blame] | 99 | |
| 100 | g_list_free_full(bcontainer->iova_ranges, g_free); |
Zhenzhong Duan | ed2f7f8 | 2023-11-02 15:12:30 +0800 | [diff] [blame] | 101 | } |
Cédric Le Goater | fdaa774 | 2023-12-19 07:58:19 +0100 | [diff] [blame] | 102 | |
| 103 | static const TypeInfo types[] = { |
| 104 | { |
| 105 | .name = TYPE_VFIO_IOMMU, |
| 106 | .parent = TYPE_INTERFACE, |
| 107 | .class_size = sizeof(VFIOIOMMUClass), |
| 108 | }, |
| 109 | }; |
| 110 | |
| 111 | DEFINE_TYPES(types) |