Statistics
| Branch: | Revision:

root / hw / sysbus.c @ 6c510fbf

History | View | Annotate | Download (4.5 kB)

1 aae9460e Paul Brook
/*
2 aae9460e Paul Brook
 *  System (CPU) Bus device support code
3 aae9460e Paul Brook
 *
4 aae9460e Paul Brook
 *  Copyright (c) 2009 CodeSourcery
5 aae9460e Paul Brook
 *
6 aae9460e Paul Brook
 * This library is free software; you can redistribute it and/or
7 aae9460e Paul Brook
 * modify it under the terms of the GNU Lesser General Public
8 aae9460e Paul Brook
 * License as published by the Free Software Foundation; either
9 aae9460e Paul Brook
 * version 2 of the License, or (at your option) any later version.
10 aae9460e Paul Brook
 *
11 aae9460e Paul Brook
 * This library is distributed in the hope that it will be useful,
12 aae9460e Paul Brook
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 aae9460e Paul Brook
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 aae9460e Paul Brook
 * Lesser General Public License for more details.
15 aae9460e Paul Brook
 *
16 aae9460e Paul Brook
 * You should have received a copy of the GNU Lesser General Public
17 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 aae9460e Paul Brook
 */
19 aae9460e Paul Brook
20 aae9460e Paul Brook
#include "sysbus.h"
21 aae9460e Paul Brook
#include "sysemu.h"
22 cae4956e Gerd Hoffmann
#include "monitor.h"
23 aae9460e Paul Brook
24 10c4c98a Gerd Hoffmann
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25 10c4c98a Gerd Hoffmann
26 10c4c98a Gerd Hoffmann
struct BusInfo system_bus_info = {
27 10c4c98a Gerd Hoffmann
    .name       = "System",
28 10c4c98a Gerd Hoffmann
    .size       = sizeof(BusState),
29 10c4c98a Gerd Hoffmann
    .print_dev  = sysbus_dev_print,
30 10c4c98a Gerd Hoffmann
};
31 10c4c98a Gerd Hoffmann
32 aae9460e Paul Brook
void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
33 aae9460e Paul Brook
{
34 aae9460e Paul Brook
    assert(n >= 0 && n < dev->num_irq);
35 660f11be Blue Swirl
    dev->irqs[n] = NULL;
36 aae9460e Paul Brook
    if (dev->irqp[n]) {
37 aae9460e Paul Brook
        *dev->irqp[n] = irq;
38 aae9460e Paul Brook
    }
39 aae9460e Paul Brook
}
40 aae9460e Paul Brook
41 c227f099 Anthony Liguori
void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
42 aae9460e Paul Brook
{
43 aae9460e Paul Brook
    assert(n >= 0 && n < dev->num_mmio);
44 aae9460e Paul Brook
45 aae9460e Paul Brook
    if (dev->mmio[n].addr == addr) {
46 aae9460e Paul Brook
        /* ??? region already mapped here.  */
47 aae9460e Paul Brook
        return;
48 aae9460e Paul Brook
    }
49 c227f099 Anthony Liguori
    if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
50 aae9460e Paul Brook
        /* Unregister previous mapping.  */
51 aae9460e Paul Brook
        cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
52 aae9460e Paul Brook
                                     IO_MEM_UNASSIGNED);
53 aae9460e Paul Brook
    }
54 aae9460e Paul Brook
    dev->mmio[n].addr = addr;
55 aae9460e Paul Brook
    if (dev->mmio[n].cb) {
56 aae9460e Paul Brook
        dev->mmio[n].cb(dev, addr);
57 aae9460e Paul Brook
    } else {
58 aae9460e Paul Brook
        cpu_register_physical_memory(addr, dev->mmio[n].size,
59 aae9460e Paul Brook
                                     dev->mmio[n].iofunc);
60 aae9460e Paul Brook
    }
61 aae9460e Paul Brook
}
62 aae9460e Paul Brook
63 aae9460e Paul Brook
64 aae9460e Paul Brook
/* Request an IRQ source.  The actual IRQ object may be populated later.  */
65 aae9460e Paul Brook
void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
66 aae9460e Paul Brook
{
67 aae9460e Paul Brook
    int n;
68 aae9460e Paul Brook
69 aae9460e Paul Brook
    assert(dev->num_irq < QDEV_MAX_IRQ);
70 aae9460e Paul Brook
    n = dev->num_irq++;
71 aae9460e Paul Brook
    dev->irqp[n] = p;
72 aae9460e Paul Brook
}
73 aae9460e Paul Brook
74 aae9460e Paul Brook
/* Pass IRQs from a target device.  */
75 aae9460e Paul Brook
void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
76 aae9460e Paul Brook
{
77 aae9460e Paul Brook
    int i;
78 aae9460e Paul Brook
    assert(dev->num_irq == 0);
79 aae9460e Paul Brook
    dev->num_irq = target->num_irq;
80 aae9460e Paul Brook
    for (i = 0; i < dev->num_irq; i++) {
81 aae9460e Paul Brook
        dev->irqp[i] = target->irqp[i];
82 aae9460e Paul Brook
    }
83 aae9460e Paul Brook
}
84 aae9460e Paul Brook
85 c227f099 Anthony Liguori
void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size, int iofunc)
86 aae9460e Paul Brook
{
87 aae9460e Paul Brook
    int n;
88 aae9460e Paul Brook
89 aae9460e Paul Brook
    assert(dev->num_mmio < QDEV_MAX_MMIO);
90 aae9460e Paul Brook
    n = dev->num_mmio++;
91 aae9460e Paul Brook
    dev->mmio[n].addr = -1;
92 aae9460e Paul Brook
    dev->mmio[n].size = size;
93 aae9460e Paul Brook
    dev->mmio[n].iofunc = iofunc;
94 aae9460e Paul Brook
}
95 aae9460e Paul Brook
96 c227f099 Anthony Liguori
void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
97 aae9460e Paul Brook
                         mmio_mapfunc cb)
98 aae9460e Paul Brook
{
99 aae9460e Paul Brook
    int n;
100 aae9460e Paul Brook
101 aae9460e Paul Brook
    assert(dev->num_mmio < QDEV_MAX_MMIO);
102 aae9460e Paul Brook
    n = dev->num_mmio++;
103 aae9460e Paul Brook
    dev->mmio[n].addr = -1;
104 aae9460e Paul Brook
    dev->mmio[n].size = size;
105 aae9460e Paul Brook
    dev->mmio[n].cb = cb;
106 aae9460e Paul Brook
}
107 aae9460e Paul Brook
108 81a322d4 Gerd Hoffmann
static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
109 aae9460e Paul Brook
{
110 02e2da45 Paul Brook
    SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
111 aae9460e Paul Brook
112 81a322d4 Gerd Hoffmann
    return info->init(sysbus_from_qdev(dev));
113 aae9460e Paul Brook
}
114 aae9460e Paul Brook
115 074f2fff Gerd Hoffmann
void sysbus_register_withprop(SysBusDeviceInfo *info)
116 aae9460e Paul Brook
{
117 02e2da45 Paul Brook
    info->qdev.init = sysbus_device_init;
118 10c4c98a Gerd Hoffmann
    info->qdev.bus_info = &system_bus_info;
119 02e2da45 Paul Brook
120 074f2fff Gerd Hoffmann
    assert(info->qdev.size >= sizeof(SysBusDevice));
121 074f2fff Gerd Hoffmann
    qdev_register(&info->qdev);
122 aae9460e Paul Brook
}
123 aae9460e Paul Brook
124 1431b6a1 Paul Brook
void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
125 1431b6a1 Paul Brook
{
126 1431b6a1 Paul Brook
    SysBusDeviceInfo *info;
127 1431b6a1 Paul Brook
128 1431b6a1 Paul Brook
    info = qemu_mallocz(sizeof(*info));
129 074f2fff Gerd Hoffmann
    info->qdev.name = qemu_strdup(name);
130 074f2fff Gerd Hoffmann
    info->qdev.size = size;
131 1431b6a1 Paul Brook
    info->init = init;
132 074f2fff Gerd Hoffmann
    sysbus_register_withprop(info);
133 1431b6a1 Paul Brook
}
134 1431b6a1 Paul Brook
135 aae9460e Paul Brook
DeviceState *sysbus_create_varargs(const char *name,
136 c227f099 Anthony Liguori
                                   target_phys_addr_t addr, ...)
137 aae9460e Paul Brook
{
138 aae9460e Paul Brook
    DeviceState *dev;
139 aae9460e Paul Brook
    SysBusDevice *s;
140 aae9460e Paul Brook
    va_list va;
141 aae9460e Paul Brook
    qemu_irq irq;
142 aae9460e Paul Brook
    int n;
143 aae9460e Paul Brook
144 aae9460e Paul Brook
    dev = qdev_create(NULL, name);
145 aae9460e Paul Brook
    s = sysbus_from_qdev(dev);
146 e23a1b33 Markus Armbruster
    qdev_init_nofail(dev);
147 c227f099 Anthony Liguori
    if (addr != (target_phys_addr_t)-1) {
148 aae9460e Paul Brook
        sysbus_mmio_map(s, 0, addr);
149 aae9460e Paul Brook
    }
150 aae9460e Paul Brook
    va_start(va, addr);
151 aae9460e Paul Brook
    n = 0;
152 aae9460e Paul Brook
    while (1) {
153 aae9460e Paul Brook
        irq = va_arg(va, qemu_irq);
154 aae9460e Paul Brook
        if (!irq) {
155 aae9460e Paul Brook
            break;
156 aae9460e Paul Brook
        }
157 aae9460e Paul Brook
        sysbus_connect_irq(s, n, irq);
158 aae9460e Paul Brook
        n++;
159 aae9460e Paul Brook
    }
160 aae9460e Paul Brook
    return dev;
161 aae9460e Paul Brook
}
162 cae4956e Gerd Hoffmann
163 10c4c98a Gerd Hoffmann
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
164 cae4956e Gerd Hoffmann
{
165 cae4956e Gerd Hoffmann
    SysBusDevice *s = sysbus_from_qdev(dev);
166 cae4956e Gerd Hoffmann
    int i;
167 cae4956e Gerd Hoffmann
168 cae4956e Gerd Hoffmann
    for (i = 0; i < s->num_mmio; i++) {
169 cae4956e Gerd Hoffmann
        monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
170 cae4956e Gerd Hoffmann
                       indent, "", s->mmio[i].addr, s->mmio[i].size);
171 cae4956e Gerd Hoffmann
    }
172 cae4956e Gerd Hoffmann
}