sysbus.c 9.8 KB
Newer Older
P
Paul Brook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  System (CPU) Bus device support code
 *
 *  Copyright (c) 2009 CodeSourcery
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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/>.
P
Paul Brook 已提交
18 19
 */

20
#include "hw/sysbus.h"
21
#include "monitor/monitor.h"
22
#include "exec/address-spaces.h"
P
Paul Brook 已提交
23

24
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25
static char *sysbus_get_fw_dev_path(DeviceState *dev);
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
typedef struct SysBusFind {
    void *opaque;
    FindSysbusDeviceFunc *func;
} SysBusFind;

/* Run func() for every sysbus device, traverse the tree for everything else */
static int find_sysbus_device(Object *obj, void *opaque)
{
    SysBusFind *find = opaque;
    Object *dev;
    SysBusDevice *sbdev;

    dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
    sbdev = (SysBusDevice *)dev;

    if (!sbdev) {
        /* Container, traverse it for children */
        return object_child_foreach(obj, find_sysbus_device, opaque);
    }

    find->func(sbdev, find->opaque);

    return 0;
}

/*
 * Loop through all dynamically created sysbus devices and call
 * func() for each instance.
 */
void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
{
    Object *container;
    SysBusFind find = {
        .func = func,
        .opaque = opaque,
    };

    /* Loop through all sysbus devices that were spawened outside the machine */
    container = container_get(qdev_get_machine(), "/peripheral");
    find_sysbus_device(container, &find);
    container = container_get(qdev_get_machine(), "/peripheral-anon");
    find_sysbus_device(container, &find);
}


72 73 74 75 76 77 78 79 80 81 82 83 84
static void system_bus_class_init(ObjectClass *klass, void *data)
{
    BusClass *k = BUS_CLASS(klass);

    k->print_dev = sysbus_dev_print;
    k->get_fw_dev_path = sysbus_get_fw_dev_path;
}

static const TypeInfo system_bus_info = {
    .name = TYPE_SYSTEM_BUS,
    .parent = TYPE_BUS,
    .instance_size = sizeof(BusState),
    .class_init = system_bus_class_init,
85 86
};

87 88 89 90 91 92 93
/* Check whether an IRQ source exists */
bool sysbus_has_irq(SysBusDevice *dev, int n)
{
    char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
    ObjectProperty *r;

    r = object_property_find(OBJECT(dev), prop, NULL);
G
Gonglei 已提交
94 95
    g_free(prop);

96 97 98 99 100 101 102 103 104 105 106 107 108 109
    return (r != NULL);
}

bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
{
    return !!sysbus_get_connected_irq(dev, n);
}

qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
{
    DeviceState *d = DEVICE(dev);
    return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
}

P
Paul Brook 已提交
110 111
void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
{
E
Eric Auger 已提交
112 113
    SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev);

114
    qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
E
Eric Auger 已提交
115 116 117 118

    if (sbd->connect_irq_notifier) {
        sbd->connect_irq_notifier(dev, irq);
    }
P
Paul Brook 已提交
119 120
}

121 122 123 124 125 126
/* Check whether an MMIO region exists */
bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
{
    return (n < dev->num_mmio);
}

127
static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
128
                                   bool may_overlap, int priority)
P
Paul Brook 已提交
129 130 131 132 133 134 135
{
    assert(n >= 0 && n < dev->num_mmio);

    if (dev->mmio[n].addr == addr) {
        /* ??? region already mapped here.  */
        return;
    }
A
Avi Kivity 已提交
136
    if (dev->mmio[n].addr != (hwaddr)-1) {
P
Paul Brook 已提交
137
        /* Unregister previous mapping.  */
138
        memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
P
Paul Brook 已提交
139 140
    }
    dev->mmio[n].addr = addr;
141 142 143 144 145 146 147 148 149 150 151
    if (may_overlap) {
        memory_region_add_subregion_overlap(get_system_memory(),
                                            addr,
                                            dev->mmio[n].memory,
                                            priority);
    }
    else {
        memory_region_add_subregion(get_system_memory(),
                                    addr,
                                    dev->mmio[n].memory);
    }
P
Paul Brook 已提交
152 153
}

154 155 156 157 158 159
void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
{
    sysbus_mmio_map_common(dev, n, addr, false, 0);
}

void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
160
                             int priority)
161 162 163
{
    sysbus_mmio_map_common(dev, n, addr, true, priority);
}
P
Paul Brook 已提交
164 165 166 167

/* Request an IRQ source.  The actual IRQ object may be populated later.  */
void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
{
168
    qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
P
Paul Brook 已提交
169 170 171 172 173
}

/* Pass IRQs from a target device.  */
void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
{
174
    qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
P
Paul Brook 已提交
175 176
}

177
void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
178 179 180 181 182 183 184 185 186
{
    int n;

    assert(dev->num_mmio < QDEV_MAX_MMIO);
    n = dev->num_mmio++;
    dev->mmio[n].addr = -1;
    dev->mmio[n].memory = memory;
}

187 188 189 190 191
MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
{
    return dev->mmio[n].memory;
}

192 193 194 195 196 197 198 199 200 201
void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
{
    pio_addr_t i;

    for (i = 0; i < size; i++) {
        assert(dev->num_pio < QDEV_MAX_PIO);
        dev->pio[dev->num_pio++] = ioport++;
    }
}

A
Anthony Liguori 已提交
202
static int sysbus_device_init(DeviceState *dev)
P
Paul Brook 已提交
203
{
204 205
    SysBusDevice *sd = SYS_BUS_DEVICE(dev);
    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
P
Paul Brook 已提交
206

207 208 209
    if (!sbc->init) {
        return 0;
    }
210
    return sbc->init(sd);
P
Paul Brook 已提交
211 212 213
}

DeviceState *sysbus_create_varargs(const char *name,
A
Avi Kivity 已提交
214
                                   hwaddr addr, ...)
P
Paul Brook 已提交
215 216 217 218 219 220 221 222
{
    DeviceState *dev;
    SysBusDevice *s;
    va_list va;
    qemu_irq irq;
    int n;

    dev = qdev_create(NULL, name);
223
    s = SYS_BUS_DEVICE(dev);
M
Markus Armbruster 已提交
224
    qdev_init_nofail(dev);
A
Avi Kivity 已提交
225
    if (addr != (hwaddr)-1) {
P
Paul Brook 已提交
226 227 228 229 230
        sysbus_mmio_map(s, 0, addr);
    }
    va_start(va, addr);
    n = 0;
    while (1) {
231 232 233 234 235 236 237
        irq = va_arg(va, qemu_irq);
        if (!irq) {
            break;
        }
        sysbus_connect_irq(s, n, irq);
        n++;
    }
238
    va_end(va);
239 240 241 242
    return dev;
}

DeviceState *sysbus_try_create_varargs(const char *name,
A
Avi Kivity 已提交
243
                                       hwaddr addr, ...)
244 245 246 247 248 249 250 251 252 253 254
{
    DeviceState *dev;
    SysBusDevice *s;
    va_list va;
    qemu_irq irq;
    int n;

    dev = qdev_try_create(NULL, name);
    if (!dev) {
        return NULL;
    }
255
    s = SYS_BUS_DEVICE(dev);
256
    qdev_init_nofail(dev);
A
Avi Kivity 已提交
257
    if (addr != (hwaddr)-1) {
258 259 260 261 262
        sysbus_mmio_map(s, 0, addr);
    }
    va_start(va, addr);
    n = 0;
    while (1) {
P
Paul Brook 已提交
263 264 265 266 267 268 269
        irq = va_arg(va, qemu_irq);
        if (!irq) {
            break;
        }
        sysbus_connect_irq(s, n, irq);
        n++;
    }
270
    va_end(va);
P
Paul Brook 已提交
271 272
    return dev;
}
273

274
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
275
{
276
    SysBusDevice *s = SYS_BUS_DEVICE(dev);
A
Avi Kivity 已提交
277
    hwaddr size;
278 279 280
    int i;

    for (i = 0; i < s->num_mmio; i++) {
281
        size = memory_region_size(s->mmio[i].memory);
282
        monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
A
Avi Kivity 已提交
283
                       indent, "", s->mmio[i].addr, size);
284 285
    }
}
286 287 288

static char *sysbus_get_fw_dev_path(DeviceState *dev)
{
289
    SysBusDevice *s = SYS_BUS_DEVICE(dev);
290 291 292
    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
    /* for the explicit unit address fallback case: */
    char *addr, *fw_dev_path;
293 294

    if (s->num_mmio) {
295 296
        return g_strdup_printf("%s@" TARGET_FMT_plx, qdev_fw_name(dev),
                               s->mmio[0].addr);
297
    }
298 299 300
    if (s->num_pio) {
        return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
    }
301 302 303 304 305 306 307 308
    if (sbc->explicit_ofw_unit_address) {
        addr = sbc->explicit_ofw_unit_address(s);
        if (addr) {
            fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
            g_free(addr);
            return fw_dev_path;
        }
    }
309
    return g_strdup(qdev_fw_name(dev));
310
}
311

A
Avi Kivity 已提交
312
void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
313 314 315 316 317
                       MemoryRegion *mem)
{
    memory_region_add_subregion(get_system_io(), addr, mem);
}

A
Avi Kivity 已提交
318 319 320 321
MemoryRegion *sysbus_address_space(SysBusDevice *dev)
{
    return get_system_memory();
}
322

323 324 325 326
static void sysbus_device_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *k = DEVICE_CLASS(klass);
    k->init = sysbus_device_init;
327
    k->bus_type = TYPE_SYSTEM_BUS;
328 329
}

330
static const TypeInfo sysbus_device_type_info = {
331 332 333 334 335
    .name = TYPE_SYS_BUS_DEVICE,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(SysBusDevice),
    .abstract = true,
    .class_size = sizeof(SysBusDeviceClass),
336
    .class_init = sysbus_device_class_init,
337 338
};

339 340 341 342 343 344 345
/* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
static BusState *main_system_bus;

static void main_system_bus_create(void)
{
    /* assign main_system_bus before qbus_create_inplace()
     * in order to make "if (bus != sysbus_get_default())" work */
346
    main_system_bus = g_malloc0(system_bus_info.instance_size);
347 348
    qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
                        TYPE_SYSTEM_BUS, NULL, "main-system-bus");
349
    OBJECT(main_system_bus)->free = g_free;
350 351 352
    object_property_add_child(container_get(qdev_get_machine(),
                                            "/unattached"),
                              "sysbus", OBJECT(main_system_bus), NULL);
353 354 355 356 357 358 359 360 361 362
}

BusState *sysbus_get_default(void)
{
    if (!main_system_bus) {
        main_system_bus_create();
    }
    return main_system_bus;
}

A
Andreas Färber 已提交
363
static void sysbus_register_types(void)
364
{
365
    type_register_static(&system_bus_info);
366 367 368
    type_register_static(&sysbus_device_type_info);
}

A
Andreas Färber 已提交
369
type_init(sysbus_register_types)