Statistics
| Branch: | Revision:

root / hw / sysbus.c @ 1129714f

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