Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/remote/iommu.c
Line
Count
Source
1
/**
2
 * IOMMU for remote device
3
 *
4
 * Copyright © 2022 Oracle and/or its affiliates.
5
 *
6
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7
 * See the COPYING file in the top-level directory.
8
 *
9
 */
10
11
#include "qemu/osdep.h"
12
13
#include "hw/remote/iommu.h"
14
#include "hw/pci/pci_bus.h"
15
#include "hw/pci/pci.h"
16
#include "system/memory.h"
17
#include "system/address-spaces.h"
18
#include "trace.h"
19
20
/**
21
 * IOMMU for TYPE_REMOTE_MACHINE - manages DMA address space isolation
22
 *     for remote machine. It is used by TYPE_VFIO_USER_SERVER.
23
 *
24
 * - Each TYPE_VFIO_USER_SERVER instance handles one PCIDevice on a PCIBus.
25
 *   There is one RemoteIommu per PCIBus, so the RemoteIommu tracks multiple
26
 *   PCIDevices by maintaining a ->elem_by_devfn mapping.
27
 *
28
 * - memory_region_init_iommu() is not used because vfio-user MemoryRegions
29
 *   will be added to the elem->mr container instead. This is more natural
30
 *   than implementing the IOMMUMemoryRegionClass APIs since vfio-user
31
 *   provides something that is close to a full-fledged MemoryRegion and
32
 *   not like an IOMMU mapping.
33
 *
34
 * - When a device is hot unplugged, the elem->mr reference is dropped so
35
 *   all vfio-user MemoryRegions associated with this vfio-user server are
36
 *   destroyed.
37
 */
38
39
static AddressSpace *remote_iommu_find_add_as(PCIBus *pci_bus,
40
                                              void *opaque, int devfn)
41
0
{
42
0
    RemoteIommu *iommu = opaque;
43
0
    RemoteIommuElem *elem = NULL;
44
45
0
    qemu_mutex_lock(&iommu->lock);
46
47
0
    elem = g_hash_table_lookup(iommu->elem_by_devfn, INT2VOIDP(devfn));
48
49
0
    if (!elem) {
50
0
        elem = g_new0(RemoteIommuElem, 1);
51
0
        g_hash_table_insert(iommu->elem_by_devfn, INT2VOIDP(devfn), elem);
52
0
    }
53
54
0
    if (!elem->mr) {
55
0
        elem->mr = MEMORY_REGION(object_new(TYPE_MEMORY_REGION));
56
0
        memory_region_set_size(elem->mr, UINT64_MAX);
57
0
        address_space_init(&elem->as, elem->mr, NULL);
58
0
    }
59
60
0
    qemu_mutex_unlock(&iommu->lock);
61
62
0
    return &elem->as;
63
0
}
64
65
void remote_iommu_unplug_dev(PCIDevice *pci_dev)
66
0
{
67
0
    AddressSpace *as = pci_device_iommu_address_space(pci_dev);
68
0
    RemoteIommuElem *elem = NULL;
69
70
0
    if (as == &address_space_memory) {
71
0
        return;
72
0
    }
73
74
0
    elem = container_of(as, RemoteIommuElem, as);
75
76
0
    address_space_destroy(&elem->as);
77
78
0
    object_unref(elem->mr);
79
80
0
    elem->mr = NULL;
81
0
}
82
83
static void remote_iommu_init(Object *obj)
84
0
{
85
0
    RemoteIommu *iommu = REMOTE_IOMMU(obj);
86
87
0
    iommu->elem_by_devfn = g_hash_table_new_full(NULL, NULL, NULL, g_free);
88
89
0
    qemu_mutex_init(&iommu->lock);
90
0
}
91
92
static void remote_iommu_finalize(Object *obj)
93
0
{
94
0
    RemoteIommu *iommu = REMOTE_IOMMU(obj);
95
96
0
    qemu_mutex_destroy(&iommu->lock);
97
98
0
    g_hash_table_destroy(iommu->elem_by_devfn);
99
100
0
    iommu->elem_by_devfn = NULL;
101
0
}
102
103
static const PCIIOMMUOps remote_iommu_ops = {
104
    .get_address_space = remote_iommu_find_add_as,
105
};
106
107
void remote_iommu_setup(PCIBus *pci_bus)
108
0
{
109
0
    RemoteIommu *iommu = NULL;
110
111
0
    g_assert(pci_bus);
112
113
0
    iommu = REMOTE_IOMMU(object_new(TYPE_REMOTE_IOMMU));
114
115
0
    pci_setup_iommu(pci_bus, &remote_iommu_ops, iommu);
116
117
0
    object_property_add_child(OBJECT(pci_bus), "remote-iommu", OBJECT(iommu));
118
119
0
    object_unref(OBJECT(iommu));
120
0
}
121
122
static const TypeInfo remote_iommu_info = {
123
    .name = TYPE_REMOTE_IOMMU,
124
    .parent = TYPE_OBJECT,
125
    .instance_size = sizeof(RemoteIommu),
126
    .instance_init = remote_iommu_init,
127
    .instance_finalize = remote_iommu_finalize,
128
};
129
130
static void remote_iommu_register_types(void)
131
63
{
132
63
    type_register_static(&remote_iommu_info);
133
63
}
134
135
type_init(remote_iommu_register_types)