]> git.proxmox.com Git - qemu.git/blame - hw/sysbus.c
monitor: move include files to include/monitor/
[qemu.git] / hw / sysbus.c
CommitLineData
aae9460e
PB
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
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aae9460e
PB
18 */
19
20#include "sysbus.h"
83c9089e 21#include "monitor/monitor.h"
022c62cb 22#include "exec/address-spaces.h"
aae9460e 23
10c4c98a 24static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
c646f74f 25static char *sysbus_get_fw_dev_path(DeviceState *dev);
10c4c98a 26
0d936928
AL
27static void system_bus_class_init(ObjectClass *klass, void *data)
28{
29 BusClass *k = BUS_CLASS(klass);
30
31 k->print_dev = sysbus_dev_print;
32 k->get_fw_dev_path = sysbus_get_fw_dev_path;
33}
34
35static const TypeInfo system_bus_info = {
36 .name = TYPE_SYSTEM_BUS,
37 .parent = TYPE_BUS,
38 .instance_size = sizeof(BusState),
39 .class_init = system_bus_class_init,
10c4c98a
GH
40};
41
aae9460e
PB
42void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
43{
44 assert(n >= 0 && n < dev->num_irq);
660f11be 45 dev->irqs[n] = NULL;
aae9460e
PB
46 if (dev->irqp[n]) {
47 *dev->irqp[n] = irq;
48 }
49}
50
a8170e5e 51void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
aae9460e
PB
52{
53 assert(n >= 0 && n < dev->num_mmio);
54
55 if (dev->mmio[n].addr == addr) {
56 /* ??? region already mapped here. */
57 return;
58 }
a8170e5e 59 if (dev->mmio[n].addr != (hwaddr)-1) {
aae9460e 60 /* Unregister previous mapping. */
e114fead 61 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
aae9460e
PB
62 }
63 dev->mmio[n].addr = addr;
e114fead
PM
64 memory_region_add_subregion(get_system_memory(),
65 addr,
66 dev->mmio[n].memory);
aae9460e
PB
67}
68
69
70/* Request an IRQ source. The actual IRQ object may be populated later. */
71void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
72{
73 int n;
74
75 assert(dev->num_irq < QDEV_MAX_IRQ);
76 n = dev->num_irq++;
77 dev->irqp[n] = p;
78}
79
80/* Pass IRQs from a target device. */
81void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
82{
83 int i;
84 assert(dev->num_irq == 0);
85 dev->num_irq = target->num_irq;
86 for (i = 0; i < dev->num_irq; i++) {
87 dev->irqp[i] = target->irqp[i];
88 }
89}
90
750ecd44 91void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
ec3bb837
AK
92{
93 int n;
94
95 assert(dev->num_mmio < QDEV_MAX_MMIO);
96 n = dev->num_mmio++;
97 dev->mmio[n].addr = -1;
ec3bb837
AK
98 dev->mmio[n].memory = memory;
99}
100
46c305ef
PM
101MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
102{
103 return dev->mmio[n].memory;
104}
105
c646f74f
GN
106void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
107{
108 pio_addr_t i;
109
110 for (i = 0; i < size; i++) {
111 assert(dev->num_pio < QDEV_MAX_PIO);
112 dev->pio[dev->num_pio++] = ioport++;
113 }
114}
115
d307af79 116static int sysbus_device_init(DeviceState *dev)
aae9460e 117{
999e12bb
AL
118 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
119 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
aae9460e 120
999e12bb 121 return sbc->init(sd);
aae9460e
PB
122}
123
aae9460e 124DeviceState *sysbus_create_varargs(const char *name,
a8170e5e 125 hwaddr addr, ...)
aae9460e
PB
126{
127 DeviceState *dev;
128 SysBusDevice *s;
129 va_list va;
130 qemu_irq irq;
131 int n;
132
133 dev = qdev_create(NULL, name);
134 s = sysbus_from_qdev(dev);
e23a1b33 135 qdev_init_nofail(dev);
a8170e5e 136 if (addr != (hwaddr)-1) {
aae9460e
PB
137 sysbus_mmio_map(s, 0, addr);
138 }
139 va_start(va, addr);
140 n = 0;
141 while (1) {
4912371f
BS
142 irq = va_arg(va, qemu_irq);
143 if (!irq) {
144 break;
145 }
146 sysbus_connect_irq(s, n, irq);
147 n++;
148 }
d0bc5bc3 149 va_end(va);
4912371f
BS
150 return dev;
151}
152
153DeviceState *sysbus_try_create_varargs(const char *name,
a8170e5e 154 hwaddr addr, ...)
4912371f
BS
155{
156 DeviceState *dev;
157 SysBusDevice *s;
158 va_list va;
159 qemu_irq irq;
160 int n;
161
162 dev = qdev_try_create(NULL, name);
163 if (!dev) {
164 return NULL;
165 }
166 s = sysbus_from_qdev(dev);
167 qdev_init_nofail(dev);
a8170e5e 168 if (addr != (hwaddr)-1) {
4912371f
BS
169 sysbus_mmio_map(s, 0, addr);
170 }
171 va_start(va, addr);
172 n = 0;
173 while (1) {
aae9460e
PB
174 irq = va_arg(va, qemu_irq);
175 if (!irq) {
176 break;
177 }
178 sysbus_connect_irq(s, n, irq);
179 n++;
180 }
d0bc5bc3 181 va_end(va);
aae9460e
PB
182 return dev;
183}
cae4956e 184
10c4c98a 185static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
cae4956e
GH
186{
187 SysBusDevice *s = sysbus_from_qdev(dev);
a8170e5e 188 hwaddr size;
cae4956e
GH
189 int i;
190
0fba9fd6 191 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
cae4956e 192 for (i = 0; i < s->num_mmio; i++) {
e114fead 193 size = memory_region_size(s->mmio[i].memory);
cae4956e 194 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
3f7f1c80 195 indent, "", s->mmio[i].addr, size);
cae4956e
GH
196 }
197}
c646f74f
GN
198
199static char *sysbus_get_fw_dev_path(DeviceState *dev)
200{
201 SysBusDevice *s = sysbus_from_qdev(dev);
202 char path[40];
203 int off;
204
205 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
206
207 if (s->num_mmio) {
208 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
209 s->mmio[0].addr);
210 } else if (s->num_pio) {
211 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
212 }
213
a5cf8262 214 return g_strdup(path);
c646f74f 215}
2b985d9c 216
a8170e5e 217void sysbus_add_memory(SysBusDevice *dev, hwaddr addr,
2b985d9c
AK
218 MemoryRegion *mem)
219{
220 memory_region_add_subregion(get_system_memory(), addr, mem);
221}
222
a8170e5e 223void sysbus_add_memory_overlap(SysBusDevice *dev, hwaddr addr,
d40b2af8
AK
224 MemoryRegion *mem, unsigned priority)
225{
226 memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
227 priority);
228}
229
2b985d9c
AK
230void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
231{
232 memory_region_del_subregion(get_system_memory(), mem);
233}
234
a8170e5e 235void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
2b985d9c
AK
236 MemoryRegion *mem)
237{
238 memory_region_add_subregion(get_system_io(), addr, mem);
239}
240
241void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
242{
243 memory_region_del_subregion(get_system_io(), mem);
244}
62ec4832
AK
245
246MemoryRegion *sysbus_address_space(SysBusDevice *dev)
247{
248 return get_system_memory();
249}
999e12bb 250
39bffca2
AL
251static void sysbus_device_class_init(ObjectClass *klass, void *data)
252{
253 DeviceClass *k = DEVICE_CLASS(klass);
254 k->init = sysbus_device_init;
0d936928 255 k->bus_type = TYPE_SYSTEM_BUS;
39bffca2
AL
256}
257
999e12bb
AL
258static TypeInfo sysbus_device_type_info = {
259 .name = TYPE_SYS_BUS_DEVICE,
260 .parent = TYPE_DEVICE,
261 .instance_size = sizeof(SysBusDevice),
262 .abstract = true,
263 .class_size = sizeof(SysBusDeviceClass),
39bffca2 264 .class_init = sysbus_device_class_init,
999e12bb
AL
265};
266
8185d216
PB
267/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
268static BusState *main_system_bus;
269
270static void main_system_bus_create(void)
271{
272 /* assign main_system_bus before qbus_create_inplace()
273 * in order to make "if (bus != sysbus_get_default())" work */
0d936928
AL
274 main_system_bus = g_malloc0(system_bus_info.instance_size);
275 qbus_create_inplace(main_system_bus, TYPE_SYSTEM_BUS, NULL,
8185d216 276 "main-system-bus");
64b625f4 277 OBJECT(main_system_bus)->free = g_free;
f968fc68
AL
278 object_property_add_child(container_get(qdev_get_machine(),
279 "/unattached"),
280 "sysbus", OBJECT(main_system_bus), NULL);
8185d216
PB
281}
282
283BusState *sysbus_get_default(void)
284{
285 if (!main_system_bus) {
286 main_system_bus_create();
287 }
288 return main_system_bus;
289}
290
83f7d43a 291static void sysbus_register_types(void)
999e12bb 292{
0d936928 293 type_register_static(&system_bus_info);
999e12bb
AL
294 type_register_static(&sysbus_device_type_info);
295}
296
83f7d43a 297type_init(sysbus_register_types)