Statistics
| Branch: | Revision:

root / hw / sysbus.c @ b651fc6f

History | View | Annotate | Download (6 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 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 aae9460e Paul Brook
        cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
54 aae9460e Paul Brook
                                     IO_MEM_UNASSIGNED);
55 aae9460e Paul Brook
    }
56 aae9460e Paul Brook
    dev->mmio[n].addr = addr;
57 aae9460e Paul Brook
    if (dev->mmio[n].cb) {
58 aae9460e Paul Brook
        dev->mmio[n].cb(dev, addr);
59 aae9460e Paul Brook
    } else {
60 aae9460e Paul Brook
        cpu_register_physical_memory(addr, dev->mmio[n].size,
61 aae9460e Paul Brook
                                     dev->mmio[n].iofunc);
62 aae9460e Paul Brook
    }
63 aae9460e Paul Brook
}
64 aae9460e Paul Brook
65 aae9460e Paul Brook
66 aae9460e Paul Brook
/* Request an IRQ source.  The actual IRQ object may be populated later.  */
67 aae9460e Paul Brook
void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
68 aae9460e Paul Brook
{
69 aae9460e Paul Brook
    int n;
70 aae9460e Paul Brook
71 aae9460e Paul Brook
    assert(dev->num_irq < QDEV_MAX_IRQ);
72 aae9460e Paul Brook
    n = dev->num_irq++;
73 aae9460e Paul Brook
    dev->irqp[n] = p;
74 aae9460e Paul Brook
}
75 aae9460e Paul Brook
76 aae9460e Paul Brook
/* Pass IRQs from a target device.  */
77 aae9460e Paul Brook
void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
78 aae9460e Paul Brook
{
79 aae9460e Paul Brook
    int i;
80 aae9460e Paul Brook
    assert(dev->num_irq == 0);
81 aae9460e Paul Brook
    dev->num_irq = target->num_irq;
82 aae9460e Paul Brook
    for (i = 0; i < dev->num_irq; i++) {
83 aae9460e Paul Brook
        dev->irqp[i] = target->irqp[i];
84 aae9460e Paul Brook
    }
85 aae9460e Paul Brook
}
86 aae9460e Paul Brook
87 3f7132d1 Blue Swirl
void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
88 3f7132d1 Blue Swirl
                      ram_addr_t iofunc)
89 aae9460e Paul Brook
{
90 aae9460e Paul Brook
    int n;
91 aae9460e Paul Brook
92 aae9460e Paul Brook
    assert(dev->num_mmio < QDEV_MAX_MMIO);
93 aae9460e Paul Brook
    n = dev->num_mmio++;
94 aae9460e Paul Brook
    dev->mmio[n].addr = -1;
95 aae9460e Paul Brook
    dev->mmio[n].size = size;
96 aae9460e Paul Brook
    dev->mmio[n].iofunc = iofunc;
97 aae9460e Paul Brook
}
98 aae9460e Paul Brook
99 c227f099 Anthony Liguori
void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
100 aae9460e Paul Brook
                         mmio_mapfunc cb)
101 aae9460e Paul Brook
{
102 aae9460e Paul Brook
    int n;
103 aae9460e Paul Brook
104 aae9460e Paul Brook
    assert(dev->num_mmio < QDEV_MAX_MMIO);
105 aae9460e Paul Brook
    n = dev->num_mmio++;
106 aae9460e Paul Brook
    dev->mmio[n].addr = -1;
107 aae9460e Paul Brook
    dev->mmio[n].size = size;
108 aae9460e Paul Brook
    dev->mmio[n].cb = cb;
109 aae9460e Paul Brook
}
110 aae9460e Paul Brook
111 c646f74f Gleb Natapov
void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
112 c646f74f Gleb Natapov
{
113 c646f74f Gleb Natapov
    pio_addr_t i;
114 c646f74f Gleb Natapov
115 c646f74f Gleb Natapov
    for (i = 0; i < size; i++) {
116 c646f74f Gleb Natapov
        assert(dev->num_pio < QDEV_MAX_PIO);
117 c646f74f Gleb Natapov
        dev->pio[dev->num_pio++] = ioport++;
118 c646f74f Gleb Natapov
    }
119 c646f74f Gleb Natapov
}
120 c646f74f Gleb Natapov
121 81a322d4 Gerd Hoffmann
static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
122 aae9460e Paul Brook
{
123 02e2da45 Paul Brook
    SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
124 aae9460e Paul Brook
125 81a322d4 Gerd Hoffmann
    return info->init(sysbus_from_qdev(dev));
126 aae9460e Paul Brook
}
127 aae9460e Paul Brook
128 074f2fff Gerd Hoffmann
void sysbus_register_withprop(SysBusDeviceInfo *info)
129 aae9460e Paul Brook
{
130 02e2da45 Paul Brook
    info->qdev.init = sysbus_device_init;
131 10c4c98a Gerd Hoffmann
    info->qdev.bus_info = &system_bus_info;
132 02e2da45 Paul Brook
133 074f2fff Gerd Hoffmann
    assert(info->qdev.size >= sizeof(SysBusDevice));
134 074f2fff Gerd Hoffmann
    qdev_register(&info->qdev);
135 aae9460e Paul Brook
}
136 aae9460e Paul Brook
137 1431b6a1 Paul Brook
void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
138 1431b6a1 Paul Brook
{
139 1431b6a1 Paul Brook
    SysBusDeviceInfo *info;
140 1431b6a1 Paul Brook
141 1431b6a1 Paul Brook
    info = qemu_mallocz(sizeof(*info));
142 074f2fff Gerd Hoffmann
    info->qdev.name = qemu_strdup(name);
143 074f2fff Gerd Hoffmann
    info->qdev.size = size;
144 1431b6a1 Paul Brook
    info->init = init;
145 074f2fff Gerd Hoffmann
    sysbus_register_withprop(info);
146 1431b6a1 Paul Brook
}
147 1431b6a1 Paul Brook
148 aae9460e Paul Brook
DeviceState *sysbus_create_varargs(const char *name,
149 c227f099 Anthony Liguori
                                   target_phys_addr_t addr, ...)
150 aae9460e Paul Brook
{
151 aae9460e Paul Brook
    DeviceState *dev;
152 aae9460e Paul Brook
    SysBusDevice *s;
153 aae9460e Paul Brook
    va_list va;
154 aae9460e Paul Brook
    qemu_irq irq;
155 aae9460e Paul Brook
    int n;
156 aae9460e Paul Brook
157 aae9460e Paul Brook
    dev = qdev_create(NULL, name);
158 aae9460e Paul Brook
    s = sysbus_from_qdev(dev);
159 e23a1b33 Markus Armbruster
    qdev_init_nofail(dev);
160 c227f099 Anthony Liguori
    if (addr != (target_phys_addr_t)-1) {
161 aae9460e Paul Brook
        sysbus_mmio_map(s, 0, addr);
162 aae9460e Paul Brook
    }
163 aae9460e Paul Brook
    va_start(va, addr);
164 aae9460e Paul Brook
    n = 0;
165 aae9460e Paul Brook
    while (1) {
166 4912371f Blue Swirl
        irq = va_arg(va, qemu_irq);
167 4912371f Blue Swirl
        if (!irq) {
168 4912371f Blue Swirl
            break;
169 4912371f Blue Swirl
        }
170 4912371f Blue Swirl
        sysbus_connect_irq(s, n, irq);
171 4912371f Blue Swirl
        n++;
172 4912371f Blue Swirl
    }
173 4912371f Blue Swirl
    return dev;
174 4912371f Blue Swirl
}
175 4912371f Blue Swirl
176 4912371f Blue Swirl
DeviceState *sysbus_try_create_varargs(const char *name,
177 4912371f Blue Swirl
                                       target_phys_addr_t addr, ...)
178 4912371f Blue Swirl
{
179 4912371f Blue Swirl
    DeviceState *dev;
180 4912371f Blue Swirl
    SysBusDevice *s;
181 4912371f Blue Swirl
    va_list va;
182 4912371f Blue Swirl
    qemu_irq irq;
183 4912371f Blue Swirl
    int n;
184 4912371f Blue Swirl
185 4912371f Blue Swirl
    dev = qdev_try_create(NULL, name);
186 4912371f Blue Swirl
    if (!dev) {
187 4912371f Blue Swirl
        return NULL;
188 4912371f Blue Swirl
    }
189 4912371f Blue Swirl
    s = sysbus_from_qdev(dev);
190 4912371f Blue Swirl
    qdev_init_nofail(dev);
191 4912371f Blue Swirl
    if (addr != (target_phys_addr_t)-1) {
192 4912371f Blue Swirl
        sysbus_mmio_map(s, 0, addr);
193 4912371f Blue Swirl
    }
194 4912371f Blue Swirl
    va_start(va, addr);
195 4912371f Blue Swirl
    n = 0;
196 4912371f Blue Swirl
    while (1) {
197 aae9460e Paul Brook
        irq = va_arg(va, qemu_irq);
198 aae9460e Paul Brook
        if (!irq) {
199 aae9460e Paul Brook
            break;
200 aae9460e Paul Brook
        }
201 aae9460e Paul Brook
        sysbus_connect_irq(s, n, irq);
202 aae9460e Paul Brook
        n++;
203 aae9460e Paul Brook
    }
204 aae9460e Paul Brook
    return dev;
205 aae9460e Paul Brook
}
206 cae4956e Gerd Hoffmann
207 10c4c98a Gerd Hoffmann
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
208 cae4956e Gerd Hoffmann
{
209 cae4956e Gerd Hoffmann
    SysBusDevice *s = sysbus_from_qdev(dev);
210 cae4956e Gerd Hoffmann
    int i;
211 cae4956e Gerd Hoffmann
212 0fba9fd6 Dmitry Eremin-Solenikov
    monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
213 cae4956e Gerd Hoffmann
    for (i = 0; i < s->num_mmio; i++) {
214 cae4956e Gerd Hoffmann
        monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
215 cae4956e Gerd Hoffmann
                       indent, "", s->mmio[i].addr, s->mmio[i].size);
216 cae4956e Gerd Hoffmann
    }
217 cae4956e Gerd Hoffmann
}
218 c646f74f Gleb Natapov
219 c646f74f Gleb Natapov
static char *sysbus_get_fw_dev_path(DeviceState *dev)
220 c646f74f Gleb Natapov
{
221 c646f74f Gleb Natapov
    SysBusDevice *s = sysbus_from_qdev(dev);
222 c646f74f Gleb Natapov
    char path[40];
223 c646f74f Gleb Natapov
    int off;
224 c646f74f Gleb Natapov
225 c646f74f Gleb Natapov
    off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
226 c646f74f Gleb Natapov
227 c646f74f Gleb Natapov
    if (s->num_mmio) {
228 c646f74f Gleb Natapov
        snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
229 c646f74f Gleb Natapov
                 s->mmio[0].addr);
230 c646f74f Gleb Natapov
    } else if (s->num_pio) {
231 c646f74f Gleb Natapov
        snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
232 c646f74f Gleb Natapov
    }
233 c646f74f Gleb Natapov
234 c646f74f Gleb Natapov
    return strdup(path);
235 c646f74f Gleb Natapov
}