Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/core/sysbus.c
Line
Count
Source
1
/*
2
 *  System (CPU) Bus device support code
3
 *
4
 *  Copyright (c) 2009 CodeSourcery
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library 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 GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include "qemu/osdep.h"
21
#include "qapi/error.h"
22
#include "hw/core/sysbus.h"
23
#include "monitor/monitor.h"
24
#include "monitor/hmp.h"
25
#include "system/address-spaces.h"
26
27
#ifdef CONFIG_HMP
28
static void sysbus_dev_print(MonitorHMP *hmp, DeviceState *dev, int indent);
29
#endif
30
static char *sysbus_get_fw_dev_path(DeviceState *dev);
31
32
typedef struct SysBusFind {
33
    void *opaque;
34
    FindSysbusDeviceFunc *func;
35
} SysBusFind;
36
37
/* Run func() for every sysbus device, traverse the tree for everything else */
38
static int find_sysbus_device(Object *obj, void *opaque)
39
0
{
40
0
    SysBusFind *find = opaque;
41
0
    Object *dev;
42
0
    SysBusDevice *sbdev;
43
44
0
    dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
45
0
    sbdev = (SysBusDevice *)dev;
46
47
0
    if (!sbdev) {
48
        /* Container, traverse it for children */
49
0
        return object_child_foreach(obj, find_sysbus_device, opaque);
50
0
    }
51
52
0
    find->func(sbdev, find->opaque);
53
54
0
    return 0;
55
0
}
56
57
/*
58
 * Loop through all dynamically created sysbus devices and call
59
 * func() for each instance.
60
 */
61
void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
62
0
{
63
0
    Object *container;
64
0
    SysBusFind find = {
65
0
        .func = func,
66
0
        .opaque = opaque,
67
0
    };
68
69
    /* Loop through all sysbus devices that were spawned outside the machine */
70
0
    container = machine_get_container("peripheral");
71
0
    find_sysbus_device(container, &find);
72
0
    container = machine_get_container("peripheral-anon");
73
0
    find_sysbus_device(container, &find);
74
0
}
75
76
77
static void system_bus_class_init(ObjectClass *klass, const void *data)
78
61
{
79
61
    BusClass *k = BUS_CLASS(klass);
80
81
61
#ifdef CONFIG_HMP
82
61
    k->print_dev = sysbus_dev_print;
83
61
#endif
84
61
    k->get_fw_dev_path = sysbus_get_fw_dev_path;
85
61
}
86
87
/* Check whether an IRQ source exists */
88
bool sysbus_has_irq(const SysBusDevice *dev, int n)
89
0
{
90
0
    char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
91
0
    ObjectProperty *r;
92
93
0
    r = object_property_find(OBJECT(dev), prop);
94
0
    g_free(prop);
95
96
0
    return (r != NULL);
97
0
}
98
99
bool sysbus_is_irq_connected(const SysBusDevice *dev, int n)
100
0
{
101
0
    return !!sysbus_get_connected_irq(dev, n);
102
0
}
103
104
qemu_irq sysbus_get_connected_irq(const SysBusDevice *dev, int n)
105
0
{
106
0
    DeviceState *d = DEVICE(dev);
107
0
    return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
108
0
}
109
110
void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
111
1.46k
{
112
1.46k
    qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
113
1.46k
}
114
115
/* Check whether an MMIO region exists */
116
bool sysbus_has_mmio(const SysBusDevice *dev, unsigned int n)
117
0
{
118
0
    return (n < dev->num_mmio);
119
0
}
120
121
static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
122
                                   bool may_overlap, int priority)
123
145
{
124
145
    assert(n >= 0 && n < dev->num_mmio);
125
126
145
    if (dev->mmio[n].addr == addr) {
127
        /* ??? region already mapped here.  */
128
0
        return;
129
0
    }
130
145
    if (dev->mmio[n].addr != (hwaddr)-1) {
131
        /* Unregister previous mapping.  */
132
0
        memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
133
0
    }
134
145
    dev->mmio[n].addr = addr;
135
145
    if (may_overlap) {
136
23
        memory_region_add_subregion_overlap(get_system_memory(),
137
23
                                            addr,
138
23
                                            dev->mmio[n].memory,
139
23
                                            priority);
140
23
    }
141
122
    else {
142
122
        memory_region_add_subregion(get_system_memory(),
143
122
                                    addr,
144
122
                                    dev->mmio[n].memory);
145
122
    }
146
145
}
147
148
void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
149
122
{
150
122
    sysbus_mmio_map_common(dev, n, addr, false, 0);
151
122
}
152
153
int sysbus_mmio_map_name(SysBusDevice *dev, const char *name, hwaddr addr)
154
0
{
155
0
    for (int i = 0; i < dev->num_mmio; i++) {
156
0
        if (!strcmp(dev->mmio[i].memory->name, name)) {
157
0
            sysbus_mmio_map(dev, i, addr);
158
0
            return i;
159
0
        }
160
0
    }
161
0
    return -1;
162
0
}
163
164
void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
165
                             int priority)
166
23
{
167
23
    sysbus_mmio_map_common(dev, n, addr, true, priority);
168
23
}
169
170
/* Request an IRQ source.  The actual IRQ object may be populated later.  */
171
void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
172
1.95k
{
173
1.95k
    qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
174
1.95k
}
175
176
/* Pass IRQs from a target device.  */
177
void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
178
0
{
179
0
    qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
180
0
}
181
182
void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
183
145
{
184
145
    int n;
185
186
145
    assert(dev->num_mmio < QDEV_MAX_MMIO);
187
145
    n = dev->num_mmio++;
188
145
    dev->mmio[n].addr = -1;
189
145
    dev->mmio[n].memory = memory;
190
145
}
191
192
MemoryRegion *sysbus_mmio_get_region(const SysBusDevice *dev, int n)
193
0
{
194
0
    assert(n >= 0 && n < QDEV_MAX_MMIO);
195
0
    return dev->mmio[n].memory;
196
0
}
197
198
void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
199
183
{
200
183
    uint32_t i;
201
202
793
    for (i = 0; i < size; i++) {
203
610
        assert(dev->num_pio < QDEV_MAX_PIO);
204
610
        dev->pio[dev->num_pio++] = ioport++;
205
610
    }
206
183
}
207
208
/* The purpose of preserving this empty realize function
209
 * is to prevent the parent_realize field of some subclasses
210
 * from being set to NULL to break the normal init/realize
211
 * of some devices.
212
 */
213
static void sysbus_device_realize(DeviceState *dev, Error **errp)
214
0
{
215
0
}
216
217
DeviceState *sysbus_create_varargs(const char *name,
218
                                   hwaddr addr, ...)
219
61
{
220
61
    DeviceState *dev;
221
61
    SysBusDevice *s;
222
61
    va_list va;
223
61
    qemu_irq irq;
224
61
    int n;
225
226
61
    dev = qdev_new(name);
227
61
    s = SYS_BUS_DEVICE(dev);
228
61
    sysbus_realize_and_unref(s, &error_fatal);
229
61
    if (addr != (hwaddr)-1) {
230
0
        sysbus_mmio_map(s, 0, addr);
231
0
    }
232
61
    va_start(va, addr);
233
61
    n = 0;
234
61
    while (1) {
235
61
        irq = va_arg(va, qemu_irq);
236
61
        if (!irq) {
237
61
            break;
238
61
        }
239
0
        sysbus_connect_irq(s, n, irq);
240
0
        n++;
241
0
    }
242
61
    va_end(va);
243
61
    return dev;
244
61
}
245
246
bool sysbus_realize(SysBusDevice *dev, Error **errp)
247
122
{
248
122
    return qdev_realize(DEVICE(dev), sysbus_get_default(), errp);
249
122
}
250
251
bool sysbus_realize_and_unref(SysBusDevice *dev, Error **errp)
252
328
{
253
328
    return qdev_realize_and_unref(DEVICE(dev), sysbus_get_default(), errp);
254
328
}
255
256
#ifdef CONFIG_HMP
257
static void sysbus_dev_print(MonitorHMP *hmp, DeviceState *dev, int indent)
258
0
{
259
0
    SysBusDevice *s = SYS_BUS_DEVICE(dev);
260
0
    hwaddr size;
261
0
    int i;
262
263
0
    for (i = 0; i < s->num_mmio; i++) {
264
0
        size = memory_region_size(s->mmio[i].memory);
265
0
        monitor_hmp_printf(hmp, "%*smmio " HWADDR_FMT_plx "/" HWADDR_FMT_plx "\n",
266
0
                           indent, "", s->mmio[i].addr, size);
267
0
    }
268
0
}
269
#endif
270
271
static char *sysbus_get_fw_dev_path(DeviceState *dev)
272
0
{
273
0
    SysBusDevice *s = SYS_BUS_DEVICE(dev);
274
0
    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
275
0
    char *addr, *fw_dev_path;
276
277
0
    if (sbc->explicit_ofw_unit_address) {
278
0
        addr = sbc->explicit_ofw_unit_address(s);
279
0
        if (addr) {
280
0
            fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
281
0
            g_free(addr);
282
0
            return fw_dev_path;
283
0
        }
284
0
    }
285
0
    if (s->num_mmio) {
286
0
        return g_strdup_printf("%s@" HWADDR_FMT_plx, qdev_fw_name(dev),
287
0
                               s->mmio[0].addr);
288
0
    }
289
0
    if (s->num_pio) {
290
0
        return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
291
0
    }
292
0
    return g_strdup(qdev_fw_name(dev));
293
0
}
294
295
static void sysbus_device_class_init(ObjectClass *klass, const void *data)
296
61
{
297
61
    DeviceClass *k = DEVICE_CLASS(klass);
298
61
    k->realize = sysbus_device_realize;
299
61
    k->bus_type = TYPE_SYSTEM_BUS;
300
    /*
301
     * device_add plugs devices into a suitable bus.  For "real" buses,
302
     * that actually connects the device.  For sysbus, the connections
303
     * need to be made separately, and device_add can't do that.  The
304
     * device would be left unconnected, and will probably not work
305
     *
306
     * However, a few machines can handle device_add/-device with
307
     * a few specific sysbus devices. In those cases, the device
308
     * subclass needs to override it and set user_creatable=true.
309
     */
310
61
    k->user_creatable = false;
311
61
}
312
313
static BusState *main_system_bus;
314
315
static void main_system_bus_create(void)
316
61
{
317
    /*
318
     * assign main_system_bus before qbus_init()
319
     * in order to make "if (bus != sysbus_get_default())" work
320
     */
321
61
    main_system_bus = g_new0(BusState, 1);
322
61
    qbus_init(main_system_bus, sizeof(BusState),
323
61
              TYPE_SYSTEM_BUS, NULL, "main-system-bus");
324
61
    OBJECT(main_system_bus)->free = g_free;
325
61
}
326
327
BusState *sysbus_get_default(void)
328
795
{
329
795
    if (!main_system_bus) {
330
61
        main_system_bus_create();
331
61
    }
332
795
    return main_system_bus;
333
795
}
334
335
static void dynamic_sysbus_device_class_init(ObjectClass *klass,
336
                                             const void *data)
337
61
{
338
61
    DeviceClass *k = DEVICE_CLASS(klass);
339
340
61
    k->user_creatable = true;
341
    k->hotpluggable = false;
342
61
}
343
344
static const TypeInfo sysbus_types[] = {
345
    {
346
        .name           = TYPE_SYSTEM_BUS,
347
        .parent         = TYPE_BUS,
348
        .instance_size  = sizeof(BusState),
349
        .class_init     = system_bus_class_init,
350
    },
351
    {
352
        .name           = TYPE_SYS_BUS_DEVICE,
353
        .parent         = TYPE_DEVICE,
354
        .instance_size  = sizeof(SysBusDevice),
355
        .abstract       = true,
356
        .class_size     = sizeof(SysBusDeviceClass),
357
        .class_init     = sysbus_device_class_init,
358
    },
359
    {
360
        .name           = TYPE_DYNAMIC_SYS_BUS_DEVICE,
361
        .parent         = TYPE_SYS_BUS_DEVICE,
362
        .class_init     = dynamic_sysbus_device_class_init,
363
        .abstract       = true,
364
    }
365
};
366
367
DEFINE_TYPES(sysbus_types)