Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/i386/x86-iommu.c
Line
Count
Source
1
/*
2
 * QEMU emulation of common X86 IOMMU
3
 *
4
 * Copyright (C) 2016 Peter Xu, Red Hat <peterx@redhat.com>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include "qemu/osdep.h"
21
#include "hw/core/sysbus.h"
22
#include "hw/i386/x86-iommu.h"
23
#include "hw/core/qdev-properties.h"
24
#include "hw/i386/pc.h"
25
#include "qapi/error.h"
26
#include "qemu/error-report.h"
27
#include "trace.h"
28
#include "system/kvm.h"
29
#include "hw/pci/pci_bus.h"
30
31
void x86_iommu_iec_register_notifier(X86IOMMUState *iommu,
32
                                     iec_notify_fn fn, void *data)
33
0
{
34
0
    IEC_Notifier *notifier = g_new0(IEC_Notifier, 1);
35
36
0
    notifier->iec_notify = fn;
37
0
    notifier->private = data;
38
39
0
    QLIST_INSERT_HEAD(&iommu->iec_notifiers, notifier, list);
40
0
}
41
42
void x86_iommu_iec_notify_all(X86IOMMUState *iommu, bool global,
43
                              uint32_t index, uint32_t mask)
44
0
{
45
0
    IEC_Notifier *notifier;
46
47
0
    trace_x86_iommu_iec_notify(global, index, mask);
48
49
0
    QLIST_FOREACH(notifier, &iommu->iec_notifiers, list) {
50
0
        if (notifier->iec_notify) {
51
0
            notifier->iec_notify(notifier->private, global,
52
0
                                 index, mask);
53
0
        }
54
0
    }
55
0
}
56
57
/* Generate one MSI message from VTDIrq info */
58
void x86_iommu_irq_to_msi_message(X86IOMMUIrq *irq, MSIMessage *msg_out)
59
0
{
60
0
    X86IOMMU_MSIMessage msg = {};
61
62
    /* Generate address bits */
63
0
    msg.dest_mode = irq->dest_mode;
64
0
    msg.redir_hint = irq->redir_hint;
65
0
    msg.dest = irq->dest;
66
0
    msg.__addr_hi = irq->dest & 0xffffff00;
67
0
    msg.__addr_head = 0xfee;
68
    /* Keep this from original MSI address bits */
69
0
    msg.__not_used = irq->msi_addr_last_bits;
70
71
    /* Generate data bits */
72
0
    msg.vector = irq->vector;
73
0
    msg.delivery_mode = irq->delivery_mode;
74
0
    msg.level = 1;
75
0
    msg.trigger_mode = irq->trigger_mode;
76
77
0
    msg_out->address = msg.msi_addr;
78
0
    msg_out->data = msg.msi_data;
79
0
}
80
81
X86IOMMUState *x86_iommu_get_default(void)
82
0
{
83
0
    MachineState *ms = MACHINE(qdev_get_machine());
84
0
    PCMachineState *pcms =
85
0
        PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
86
87
0
    if (pcms &&
88
0
        object_dynamic_cast(OBJECT(pcms->iommu), TYPE_X86_IOMMU_DEVICE)) {
89
0
        return X86_IOMMU_DEVICE(pcms->iommu);
90
0
    }
91
0
    return NULL;
92
0
}
93
94
X86IOMMUState *x86_iommu_device_get(PCIDevice *dev)
95
0
{
96
0
    PCIBus *iommu_bus;
97
0
    X86IOMMUState *iommu = NULL;
98
99
0
    pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL);
100
101
0
    if (iommu_bus && iommu_bus->iommu_ops->get_x86_iommu) {
102
0
        iommu_bus->iommu_ops->get_x86_iommu(iommu_bus->iommu_opaque,
103
0
                                            (void **)&iommu);
104
0
    }
105
106
0
    return iommu;
107
0
}
108
109
static void x86_iommu_realize(DeviceState *dev, Error **errp)
110
0
{
111
0
    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
112
0
    X86IOMMUClass *x86_class = X86_IOMMU_DEVICE_GET_CLASS(dev);
113
0
    MachineState *ms = MACHINE(qdev_get_machine());
114
0
    MachineClass *mc = MACHINE_GET_CLASS(ms);
115
0
    PCMachineState *pcms =
116
0
        PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
117
0
    QLIST_INIT(&x86_iommu->iec_notifiers);
118
0
    bool irq_all_kernel = kvm_irqchip_in_kernel() && !kvm_irqchip_is_split();
119
120
0
    if (!pcms || !pcms->pcibus) {
121
0
        error_setg(errp, "Machine-type '%s' not supported by IOMMU",
122
0
                   mc->name);
123
0
        return;
124
0
    }
125
126
    /* If the user didn't specify IR, choose a default value for it */
127
0
    if (x86_iommu->intr_supported == ON_OFF_AUTO_AUTO) {
128
0
        x86_iommu->intr_supported = irq_all_kernel ?
129
0
            ON_OFF_AUTO_OFF : ON_OFF_AUTO_ON;
130
0
    }
131
132
    /* Both Intel and AMD IOMMU IR only support "kernel-irqchip={off|split}" */
133
0
    if (x86_iommu_ir_supported(x86_iommu) && irq_all_kernel) {
134
0
        error_setg(errp, "Interrupt Remapping cannot work with "
135
0
                         "kernel-irqchip=on, please use 'split|off'.");
136
0
        return;
137
0
    }
138
139
0
    if (x86_class->realize) {
140
0
        x86_class->realize(dev, errp);
141
0
    }
142
0
}
143
144
static const Property x86_iommu_properties[] = {
145
    DEFINE_PROP_ON_OFF_AUTO("intremap", X86IOMMUState,
146
                            intr_supported, ON_OFF_AUTO_AUTO),
147
    DEFINE_PROP_BOOL("device-iotlb", X86IOMMUState, dt_supported, false),
148
    DEFINE_PROP_BOOL("dma-translation", X86IOMMUState, dma_translation, true),
149
};
150
151
static void x86_iommu_class_init(ObjectClass *klass, const void *data)
152
63
{
153
63
    DeviceClass *dc = DEVICE_CLASS(klass);
154
63
    dc->realize = x86_iommu_realize;
155
63
    device_class_set_props(dc, x86_iommu_properties);
156
63
}
157
158
bool x86_iommu_ir_supported(X86IOMMUState *s)
159
0
{
160
0
    return s->intr_supported == ON_OFF_AUTO_ON;
161
0
}
162
163
static const TypeInfo x86_iommu_info = {
164
    .name          = TYPE_X86_IOMMU_DEVICE,
165
    .parent        = TYPE_DYNAMIC_SYS_BUS_DEVICE,
166
    .instance_size = sizeof(X86IOMMUState),
167
    .class_init    = x86_iommu_class_init,
168
    .class_size    = sizeof(X86IOMMUClass),
169
    .abstract      = true,
170
};
171
172
static void x86_iommu_register_types(void)
173
63
{
174
63
    type_register_static(&x86_iommu_info);
175
63
}
176
177
type_init(x86_iommu_register_types)