qemu/hw/core/sysbus.c
<<
>>
Prefs
   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 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 "hw/sysbus.h"
  22#include "monitor/monitor.h"
  23#include "exec/address-spaces.h"
  24
  25static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
  26static char *sysbus_get_fw_dev_path(DeviceState *dev);
  27
  28typedef struct SysBusFind {
  29    void *opaque;
  30    FindSysbusDeviceFunc *func;
  31} SysBusFind;
  32
  33/* Run func() for every sysbus device, traverse the tree for everything else */
  34static int find_sysbus_device(Object *obj, void *opaque)
  35{
  36    SysBusFind *find = opaque;
  37    Object *dev;
  38    SysBusDevice *sbdev;
  39
  40    dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
  41    sbdev = (SysBusDevice *)dev;
  42
  43    if (!sbdev) {
  44        /* Container, traverse it for children */
  45        return object_child_foreach(obj, find_sysbus_device, opaque);
  46    }
  47
  48    find->func(sbdev, find->opaque);
  49
  50    return 0;
  51}
  52
  53/*
  54 * Loop through all dynamically created sysbus devices and call
  55 * func() for each instance.
  56 */
  57void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
  58{
  59    Object *container;
  60    SysBusFind find = {
  61        .func = func,
  62        .opaque = opaque,
  63    };
  64
  65    /* Loop through all sysbus devices that were spawened outside the machine */
  66    container = container_get(qdev_get_machine(), "/peripheral");
  67    find_sysbus_device(container, &find);
  68    container = container_get(qdev_get_machine(), "/peripheral-anon");
  69    find_sysbus_device(container, &find);
  70}
  71
  72
  73static void system_bus_class_init(ObjectClass *klass, void *data)
  74{
  75    BusClass *k = BUS_CLASS(klass);
  76
  77    k->print_dev = sysbus_dev_print;
  78    k->get_fw_dev_path = sysbus_get_fw_dev_path;
  79}
  80
  81static const TypeInfo system_bus_info = {
  82    .name = TYPE_SYSTEM_BUS,
  83    .parent = TYPE_BUS,
  84    .instance_size = sizeof(BusState),
  85    .class_init = system_bus_class_init,
  86};
  87
  88/* Check whether an IRQ source exists */
  89bool sysbus_has_irq(SysBusDevice *dev, int n)
  90{
  91    char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
  92    ObjectProperty *r;
  93
  94    r = object_property_find(OBJECT(dev), prop, NULL);
  95    g_free(prop);
  96
  97    return (r != NULL);
  98}
  99
 100bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
 101{
 102    return !!sysbus_get_connected_irq(dev, n);
 103}
 104
 105qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
 106{
 107    DeviceState *d = DEVICE(dev);
 108    return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
 109}
 110
 111void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
 112{
 113    SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev);
 114
 115    qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
 116
 117    if (sbd->connect_irq_notifier) {
 118        sbd->connect_irq_notifier(dev, irq);
 119    }
 120}
 121
 122/* Check whether an MMIO region exists */
 123bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
 124{
 125    return (n < dev->num_mmio);
 126}
 127
 128static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
 129                                   bool may_overlap, int priority)
 130{
 131    assert(n >= 0 && n < dev->num_mmio);
 132
 133    if (dev->mmio[n].addr == addr) {
 134        /* ??? region already mapped here.  */
 135        return;
 136    }
 137    if (dev->mmio[n].addr != (hwaddr)-1) {
 138        /* Unregister previous mapping.  */
 139        memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
 140    }
 141    dev->mmio[n].addr = addr;
 142    if (may_overlap) {
 143        memory_region_add_subregion_overlap(get_system_memory(),
 144                                            addr,
 145                                            dev->mmio[n].memory,
 146                                            priority);
 147    }
 148    else {
 149        memory_region_add_subregion(get_system_memory(),
 150                                    addr,
 151                                    dev->mmio[n].memory);
 152    }
 153}
 154
 155void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
 156{
 157    sysbus_mmio_map_common(dev, n, addr, false, 0);
 158}
 159
 160void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
 161                             int priority)
 162{
 163    sysbus_mmio_map_common(dev, n, addr, true, priority);
 164}
 165
 166/* Request an IRQ source.  The actual IRQ object may be populated later.  */
 167void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
 168{
 169    qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
 170}
 171
 172/* Pass IRQs from a target device.  */
 173void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
 174{
 175    qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
 176}
 177
 178void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
 179{
 180    int n;
 181
 182    assert(dev->num_mmio < QDEV_MAX_MMIO);
 183    n = dev->num_mmio++;
 184    dev->mmio[n].addr = -1;
 185    dev->mmio[n].memory = memory;
 186}
 187
 188MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
 189{
 190    return dev->mmio[n].memory;
 191}
 192
 193void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
 194{
 195    pio_addr_t i;
 196
 197    for (i = 0; i < size; i++) {
 198        assert(dev->num_pio < QDEV_MAX_PIO);
 199        dev->pio[dev->num_pio++] = ioport++;
 200    }
 201}
 202
 203static int sysbus_device_init(DeviceState *dev)
 204{
 205    SysBusDevice *sd = SYS_BUS_DEVICE(dev);
 206    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
 207
 208    if (!sbc->init) {
 209        return 0;
 210    }
 211    return sbc->init(sd);
 212}
 213
 214DeviceState *sysbus_create_varargs(const char *name,
 215                                   hwaddr addr, ...)
 216{
 217    DeviceState *dev;
 218    SysBusDevice *s;
 219    va_list va;
 220    qemu_irq irq;
 221    int n;
 222
 223    dev = qdev_create(NULL, name);
 224    s = SYS_BUS_DEVICE(dev);
 225    qdev_init_nofail(dev);
 226    if (addr != (hwaddr)-1) {
 227        sysbus_mmio_map(s, 0, addr);
 228    }
 229    va_start(va, addr);
 230    n = 0;
 231    while (1) {
 232        irq = va_arg(va, qemu_irq);
 233        if (!irq) {
 234            break;
 235        }
 236        sysbus_connect_irq(s, n, irq);
 237        n++;
 238    }
 239    va_end(va);
 240    return dev;
 241}
 242
 243DeviceState *sysbus_try_create_varargs(const char *name,
 244                                       hwaddr addr, ...)
 245{
 246    DeviceState *dev;
 247    SysBusDevice *s;
 248    va_list va;
 249    qemu_irq irq;
 250    int n;
 251
 252    dev = qdev_try_create(NULL, name);
 253    if (!dev) {
 254        return NULL;
 255    }
 256    s = SYS_BUS_DEVICE(dev);
 257    qdev_init_nofail(dev);
 258    if (addr != (hwaddr)-1) {
 259        sysbus_mmio_map(s, 0, addr);
 260    }
 261    va_start(va, addr);
 262    n = 0;
 263    while (1) {
 264        irq = va_arg(va, qemu_irq);
 265        if (!irq) {
 266            break;
 267        }
 268        sysbus_connect_irq(s, n, irq);
 269        n++;
 270    }
 271    va_end(va);
 272    return dev;
 273}
 274
 275static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
 276{
 277    SysBusDevice *s = SYS_BUS_DEVICE(dev);
 278    hwaddr size;
 279    int i;
 280
 281    for (i = 0; i < s->num_mmio; i++) {
 282        size = memory_region_size(s->mmio[i].memory);
 283        monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
 284                       indent, "", s->mmio[i].addr, size);
 285    }
 286}
 287
 288static char *sysbus_get_fw_dev_path(DeviceState *dev)
 289{
 290    SysBusDevice *s = SYS_BUS_DEVICE(dev);
 291    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
 292    /* for the explicit unit address fallback case: */
 293    char *addr, *fw_dev_path;
 294
 295    if (s->num_mmio) {
 296        return g_strdup_printf("%s@" TARGET_FMT_plx, qdev_fw_name(dev),
 297                               s->mmio[0].addr);
 298    }
 299    if (s->num_pio) {
 300        return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
 301    }
 302    if (sbc->explicit_ofw_unit_address) {
 303        addr = sbc->explicit_ofw_unit_address(s);
 304        if (addr) {
 305            fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
 306            g_free(addr);
 307            return fw_dev_path;
 308        }
 309    }
 310    return g_strdup(qdev_fw_name(dev));
 311}
 312
 313void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
 314                       MemoryRegion *mem)
 315{
 316    memory_region_add_subregion(get_system_io(), addr, mem);
 317}
 318
 319MemoryRegion *sysbus_address_space(SysBusDevice *dev)
 320{
 321    return get_system_memory();
 322}
 323
 324static void sysbus_device_class_init(ObjectClass *klass, void *data)
 325{
 326    DeviceClass *k = DEVICE_CLASS(klass);
 327    k->init = sysbus_device_init;
 328    k->bus_type = TYPE_SYSTEM_BUS;
 329}
 330
 331static const TypeInfo sysbus_device_type_info = {
 332    .name = TYPE_SYS_BUS_DEVICE,
 333    .parent = TYPE_DEVICE,
 334    .instance_size = sizeof(SysBusDevice),
 335    .abstract = true,
 336    .class_size = sizeof(SysBusDeviceClass),
 337    .class_init = sysbus_device_class_init,
 338};
 339
 340/* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
 341static BusState *main_system_bus;
 342
 343static void main_system_bus_create(void)
 344{
 345    /* assign main_system_bus before qbus_create_inplace()
 346     * in order to make "if (bus != sysbus_get_default())" work */
 347    main_system_bus = g_malloc0(system_bus_info.instance_size);
 348    qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
 349                        TYPE_SYSTEM_BUS, NULL, "main-system-bus");
 350    OBJECT(main_system_bus)->free = g_free;
 351    object_property_add_child(container_get(qdev_get_machine(),
 352                                            "/unattached"),
 353                              "sysbus", OBJECT(main_system_bus), NULL);
 354}
 355
 356BusState *sysbus_get_default(void)
 357{
 358    if (!main_system_bus) {
 359        main_system_bus_create();
 360    }
 361    return main_system_bus;
 362}
 363
 364static void sysbus_register_types(void)
 365{
 366    type_register_static(&system_bus_info);
 367    type_register_static(&sysbus_device_type_info);
 368}
 369
 370type_init(sysbus_register_types)
 371