Statistics
| Branch: | Revision:

root / hw / sysbus.c @ 1d7a1197

History | View | Annotate | Download (6.7 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 cae4956e Gerd Hoffmann
#include "monitor.h"
22 ec3bb837 Avi Kivity
#include "exec-memory.h"
23 aae9460e Paul Brook
24 10c4c98a Gerd Hoffmann
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25 c646f74f Gleb Natapov
static char *sysbus_get_fw_dev_path(DeviceState *dev);
26 10c4c98a Gerd Hoffmann
27 10c4c98a Gerd Hoffmann
struct BusInfo system_bus_info = {
28 10c4c98a Gerd Hoffmann
    .name       = "System",
29 10c4c98a Gerd Hoffmann
    .size       = sizeof(BusState),
30 10c4c98a Gerd Hoffmann
    .print_dev  = sysbus_dev_print,
31 c646f74f Gleb Natapov
    .get_fw_dev_path = sysbus_get_fw_dev_path,
32 10c4c98a Gerd Hoffmann
};
33 10c4c98a Gerd Hoffmann
34 aae9460e Paul Brook
void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
35 aae9460e Paul Brook
{
36 aae9460e Paul Brook
    assert(n >= 0 && n < dev->num_irq);
37 660f11be Blue Swirl
    dev->irqs[n] = NULL;
38 aae9460e Paul Brook
    if (dev->irqp[n]) {
39 aae9460e Paul Brook
        *dev->irqp[n] = irq;
40 aae9460e Paul Brook
    }
41 aae9460e Paul Brook
}
42 aae9460e Paul Brook
43 c227f099 Anthony Liguori
void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
44 aae9460e Paul Brook
{
45 aae9460e Paul Brook
    assert(n >= 0 && n < dev->num_mmio);
46 aae9460e Paul Brook
47 aae9460e Paul Brook
    if (dev->mmio[n].addr == addr) {
48 aae9460e Paul Brook
        /* ??? region already mapped here.  */
49 aae9460e Paul Brook
        return;
50 aae9460e Paul Brook
    }
51 c227f099 Anthony Liguori
    if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
52 aae9460e Paul Brook
        /* Unregister previous mapping.  */
53 e114fead Peter Maydell
        memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
54 aae9460e Paul Brook
    }
55 aae9460e Paul Brook
    dev->mmio[n].addr = addr;
56 e114fead Peter Maydell
    memory_region_add_subregion(get_system_memory(),
57 e114fead Peter Maydell
                                addr,
58 e114fead Peter Maydell
                                dev->mmio[n].memory);
59 aae9460e Paul Brook
}
60 aae9460e Paul Brook
61 aae9460e Paul Brook
62 aae9460e Paul Brook
/* Request an IRQ source.  The actual IRQ object may be populated later.  */
63 aae9460e Paul Brook
void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
64 aae9460e Paul Brook
{
65 aae9460e Paul Brook
    int n;
66 aae9460e Paul Brook
67 aae9460e Paul Brook
    assert(dev->num_irq < QDEV_MAX_IRQ);
68 aae9460e Paul Brook
    n = dev->num_irq++;
69 aae9460e Paul Brook
    dev->irqp[n] = p;
70 aae9460e Paul Brook
}
71 aae9460e Paul Brook
72 aae9460e Paul Brook
/* Pass IRQs from a target device.  */
73 aae9460e Paul Brook
void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
74 aae9460e Paul Brook
{
75 aae9460e Paul Brook
    int i;
76 aae9460e Paul Brook
    assert(dev->num_irq == 0);
77 aae9460e Paul Brook
    dev->num_irq = target->num_irq;
78 aae9460e Paul Brook
    for (i = 0; i < dev->num_irq; i++) {
79 aae9460e Paul Brook
        dev->irqp[i] = target->irqp[i];
80 aae9460e Paul Brook
    }
81 aae9460e Paul Brook
}
82 aae9460e Paul Brook
83 750ecd44 Avi Kivity
void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
84 ec3bb837 Avi Kivity
{
85 ec3bb837 Avi Kivity
    int n;
86 ec3bb837 Avi Kivity
87 ec3bb837 Avi Kivity
    assert(dev->num_mmio < QDEV_MAX_MMIO);
88 ec3bb837 Avi Kivity
    n = dev->num_mmio++;
89 ec3bb837 Avi Kivity
    dev->mmio[n].addr = -1;
90 ec3bb837 Avi Kivity
    dev->mmio[n].memory = memory;
91 ec3bb837 Avi Kivity
}
92 ec3bb837 Avi Kivity
93 46c305ef Peter Maydell
MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
94 46c305ef Peter Maydell
{
95 46c305ef Peter Maydell
    return dev->mmio[n].memory;
96 46c305ef Peter Maydell
}
97 46c305ef Peter Maydell
98 c646f74f Gleb Natapov
void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
99 c646f74f Gleb Natapov
{
100 c646f74f Gleb Natapov
    pio_addr_t i;
101 c646f74f Gleb Natapov
102 c646f74f Gleb Natapov
    for (i = 0; i < size; i++) {
103 c646f74f Gleb Natapov
        assert(dev->num_pio < QDEV_MAX_PIO);
104 c646f74f Gleb Natapov
        dev->pio[dev->num_pio++] = ioport++;
105 c646f74f Gleb Natapov
    }
106 c646f74f Gleb Natapov
}
107 c646f74f Gleb Natapov
108 d307af79 Anthony Liguori
static int sysbus_device_init(DeviceState *dev)
109 aae9460e Paul Brook
{
110 999e12bb Anthony Liguori
    SysBusDevice *sd = SYS_BUS_DEVICE(dev);
111 999e12bb Anthony Liguori
    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
112 aae9460e Paul Brook
113 999e12bb Anthony Liguori
    return sbc->init(sd);
114 aae9460e Paul Brook
}
115 aae9460e Paul Brook
116 aae9460e Paul Brook
DeviceState *sysbus_create_varargs(const char *name,
117 c227f099 Anthony Liguori
                                   target_phys_addr_t addr, ...)
118 aae9460e Paul Brook
{
119 aae9460e Paul Brook
    DeviceState *dev;
120 aae9460e Paul Brook
    SysBusDevice *s;
121 aae9460e Paul Brook
    va_list va;
122 aae9460e Paul Brook
    qemu_irq irq;
123 aae9460e Paul Brook
    int n;
124 aae9460e Paul Brook
125 aae9460e Paul Brook
    dev = qdev_create(NULL, name);
126 aae9460e Paul Brook
    s = sysbus_from_qdev(dev);
127 e23a1b33 Markus Armbruster
    qdev_init_nofail(dev);
128 c227f099 Anthony Liguori
    if (addr != (target_phys_addr_t)-1) {
129 aae9460e Paul Brook
        sysbus_mmio_map(s, 0, addr);
130 aae9460e Paul Brook
    }
131 aae9460e Paul Brook
    va_start(va, addr);
132 aae9460e Paul Brook
    n = 0;
133 aae9460e Paul Brook
    while (1) {
134 4912371f Blue Swirl
        irq = va_arg(va, qemu_irq);
135 4912371f Blue Swirl
        if (!irq) {
136 4912371f Blue Swirl
            break;
137 4912371f Blue Swirl
        }
138 4912371f Blue Swirl
        sysbus_connect_irq(s, n, irq);
139 4912371f Blue Swirl
        n++;
140 4912371f Blue Swirl
    }
141 d0bc5bc3 Markus Armbruster
    va_end(va);
142 4912371f Blue Swirl
    return dev;
143 4912371f Blue Swirl
}
144 4912371f Blue Swirl
145 4912371f Blue Swirl
DeviceState *sysbus_try_create_varargs(const char *name,
146 4912371f Blue Swirl
                                       target_phys_addr_t addr, ...)
147 4912371f Blue Swirl
{
148 4912371f Blue Swirl
    DeviceState *dev;
149 4912371f Blue Swirl
    SysBusDevice *s;
150 4912371f Blue Swirl
    va_list va;
151 4912371f Blue Swirl
    qemu_irq irq;
152 4912371f Blue Swirl
    int n;
153 4912371f Blue Swirl
154 4912371f Blue Swirl
    dev = qdev_try_create(NULL, name);
155 4912371f Blue Swirl
    if (!dev) {
156 4912371f Blue Swirl
        return NULL;
157 4912371f Blue Swirl
    }
158 4912371f Blue Swirl
    s = sysbus_from_qdev(dev);
159 4912371f Blue Swirl
    qdev_init_nofail(dev);
160 4912371f Blue Swirl
    if (addr != (target_phys_addr_t)-1) {
161 4912371f Blue Swirl
        sysbus_mmio_map(s, 0, addr);
162 4912371f Blue Swirl
    }
163 4912371f Blue Swirl
    va_start(va, addr);
164 4912371f Blue Swirl
    n = 0;
165 4912371f Blue Swirl
    while (1) {
166 aae9460e Paul Brook
        irq = va_arg(va, qemu_irq);
167 aae9460e Paul Brook
        if (!irq) {
168 aae9460e Paul Brook
            break;
169 aae9460e Paul Brook
        }
170 aae9460e Paul Brook
        sysbus_connect_irq(s, n, irq);
171 aae9460e Paul Brook
        n++;
172 aae9460e Paul Brook
    }
173 d0bc5bc3 Markus Armbruster
    va_end(va);
174 aae9460e Paul Brook
    return dev;
175 aae9460e Paul Brook
}
176 cae4956e Gerd Hoffmann
177 10c4c98a Gerd Hoffmann
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
178 cae4956e Gerd Hoffmann
{
179 cae4956e Gerd Hoffmann
    SysBusDevice *s = sysbus_from_qdev(dev);
180 3f7f1c80 Avi Kivity
    target_phys_addr_t size;
181 cae4956e Gerd Hoffmann
    int i;
182 cae4956e Gerd Hoffmann
183 0fba9fd6 Dmitry Eremin-Solenikov
    monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
184 cae4956e Gerd Hoffmann
    for (i = 0; i < s->num_mmio; i++) {
185 e114fead Peter Maydell
        size = memory_region_size(s->mmio[i].memory);
186 cae4956e Gerd Hoffmann
        monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
187 3f7f1c80 Avi Kivity
                       indent, "", s->mmio[i].addr, size);
188 cae4956e Gerd Hoffmann
    }
189 cae4956e Gerd Hoffmann
}
190 c646f74f Gleb Natapov
191 c646f74f Gleb Natapov
static char *sysbus_get_fw_dev_path(DeviceState *dev)
192 c646f74f Gleb Natapov
{
193 c646f74f Gleb Natapov
    SysBusDevice *s = sysbus_from_qdev(dev);
194 c646f74f Gleb Natapov
    char path[40];
195 c646f74f Gleb Natapov
    int off;
196 c646f74f Gleb Natapov
197 c646f74f Gleb Natapov
    off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
198 c646f74f Gleb Natapov
199 c646f74f Gleb Natapov
    if (s->num_mmio) {
200 c646f74f Gleb Natapov
        snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
201 c646f74f Gleb Natapov
                 s->mmio[0].addr);
202 c646f74f Gleb Natapov
    } else if (s->num_pio) {
203 c646f74f Gleb Natapov
        snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
204 c646f74f Gleb Natapov
    }
205 c646f74f Gleb Natapov
206 c646f74f Gleb Natapov
    return strdup(path);
207 c646f74f Gleb Natapov
}
208 2b985d9c Avi Kivity
209 2b985d9c Avi Kivity
void sysbus_add_memory(SysBusDevice *dev, target_phys_addr_t addr,
210 2b985d9c Avi Kivity
                       MemoryRegion *mem)
211 2b985d9c Avi Kivity
{
212 2b985d9c Avi Kivity
    memory_region_add_subregion(get_system_memory(), addr, mem);
213 2b985d9c Avi Kivity
}
214 2b985d9c Avi Kivity
215 d40b2af8 Avi Kivity
void sysbus_add_memory_overlap(SysBusDevice *dev, target_phys_addr_t addr,
216 d40b2af8 Avi Kivity
                               MemoryRegion *mem, unsigned priority)
217 d40b2af8 Avi Kivity
{
218 d40b2af8 Avi Kivity
    memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
219 d40b2af8 Avi Kivity
                                        priority);
220 d40b2af8 Avi Kivity
}
221 d40b2af8 Avi Kivity
222 2b985d9c Avi Kivity
void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
223 2b985d9c Avi Kivity
{
224 2b985d9c Avi Kivity
    memory_region_del_subregion(get_system_memory(), mem);
225 2b985d9c Avi Kivity
}
226 2b985d9c Avi Kivity
227 2b985d9c Avi Kivity
void sysbus_add_io(SysBusDevice *dev, target_phys_addr_t addr,
228 2b985d9c Avi Kivity
                       MemoryRegion *mem)
229 2b985d9c Avi Kivity
{
230 2b985d9c Avi Kivity
    memory_region_add_subregion(get_system_io(), addr, mem);
231 2b985d9c Avi Kivity
}
232 2b985d9c Avi Kivity
233 2b985d9c Avi Kivity
void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
234 2b985d9c Avi Kivity
{
235 2b985d9c Avi Kivity
    memory_region_del_subregion(get_system_io(), mem);
236 2b985d9c Avi Kivity
}
237 62ec4832 Avi Kivity
238 62ec4832 Avi Kivity
MemoryRegion *sysbus_address_space(SysBusDevice *dev)
239 62ec4832 Avi Kivity
{
240 62ec4832 Avi Kivity
    return get_system_memory();
241 62ec4832 Avi Kivity
}
242 999e12bb Anthony Liguori
243 39bffca2 Anthony Liguori
static void sysbus_device_class_init(ObjectClass *klass, void *data)
244 39bffca2 Anthony Liguori
{
245 39bffca2 Anthony Liguori
    DeviceClass *k = DEVICE_CLASS(klass);
246 39bffca2 Anthony Liguori
    k->init = sysbus_device_init;
247 39bffca2 Anthony Liguori
    k->bus_info = &system_bus_info;
248 39bffca2 Anthony Liguori
}
249 39bffca2 Anthony Liguori
250 999e12bb Anthony Liguori
static TypeInfo sysbus_device_type_info = {
251 999e12bb Anthony Liguori
    .name = TYPE_SYS_BUS_DEVICE,
252 999e12bb Anthony Liguori
    .parent = TYPE_DEVICE,
253 999e12bb Anthony Liguori
    .instance_size = sizeof(SysBusDevice),
254 999e12bb Anthony Liguori
    .abstract = true,
255 999e12bb Anthony Liguori
    .class_size = sizeof(SysBusDeviceClass),
256 39bffca2 Anthony Liguori
    .class_init = sysbus_device_class_init,
257 999e12bb Anthony Liguori
};
258 999e12bb Anthony Liguori
259 83f7d43a Andreas Färber
static void sysbus_register_types(void)
260 999e12bb Anthony Liguori
{
261 999e12bb Anthony Liguori
    type_register_static(&sysbus_device_type_info);
262 999e12bb Anthony Liguori
}
263 999e12bb Anthony Liguori
264 83f7d43a Andreas Färber
type_init(sysbus_register_types)