]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/sysbus.c
qga: Make qemu-ga compile statically for Windows
[mirror_qemu.git] / hw / core / 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
18c86e2b 20#include "qemu/osdep.h"
83c9f4ca 21#include "hw/sysbus.h"
83c9089e 22#include "monitor/monitor.h"
022c62cb 23#include "exec/address-spaces.h"
aae9460e 24
10c4c98a 25static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
c646f74f 26static char *sysbus_get_fw_dev_path(DeviceState *dev);
10c4c98a 27
eb572280
AG
28typedef struct SysBusFind {
29 void *opaque;
30 FindSysbusDeviceFunc *func;
31} SysBusFind;
32
33/* Run func() for every sysbus device, traverse the tree for everything else */
34static int find_sysbus_device(Object *obj, void *opaque)
35{
36 SysBusFind *find = opaque;
37 Object *dev;
38 SysBusDevice *sbdev;
39
40 dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
41 sbdev = (SysBusDevice *)dev;
42
43 if (!sbdev) {
44 /* Container, traverse it for children */
45 return object_child_foreach(obj, find_sysbus_device, opaque);
46 }
47
48 find->func(sbdev, find->opaque);
49
50 return 0;
51}
52
53/*
54 * Loop through all dynamically created sysbus devices and call
55 * func() for each instance.
56 */
57void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
58{
59 Object *container;
60 SysBusFind find = {
61 .func = func,
62 .opaque = opaque,
63 };
64
65 /* Loop through all sysbus devices that were spawened outside the machine */
66 container = container_get(qdev_get_machine(), "/peripheral");
67 find_sysbus_device(container, &find);
68 container = container_get(qdev_get_machine(), "/peripheral-anon");
69 find_sysbus_device(container, &find);
70}
71
72
0d936928
AL
73static void system_bus_class_init(ObjectClass *klass, void *data)
74{
75 BusClass *k = BUS_CLASS(klass);
76
77 k->print_dev = sysbus_dev_print;
78 k->get_fw_dev_path = sysbus_get_fw_dev_path;
79}
80
81static const TypeInfo system_bus_info = {
82 .name = TYPE_SYSTEM_BUS,
83 .parent = TYPE_BUS,
84 .instance_size = sizeof(BusState),
85 .class_init = system_bus_class_init,
10c4c98a
GH
86};
87
b7973186
AG
88/* Check whether an IRQ source exists */
89bool sysbus_has_irq(SysBusDevice *dev, int n)
90{
91 char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
92 ObjectProperty *r;
93
94 r = object_property_find(OBJECT(dev), prop, NULL);
84b5d556
GA
95 g_free(prop);
96
b7973186
AG
97 return (r != NULL);
98}
99
100bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
101{
102 return !!sysbus_get_connected_irq(dev, n);
103}
104
105qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
106{
107 DeviceState *d = DEVICE(dev);
108 return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
109}
110
aae9460e
PB
111void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
112{
715ca691
EA
113 SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev);
114
b5917219 115 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
715ca691
EA
116
117 if (sbd->connect_irq_notifier) {
118 sbd->connect_irq_notifier(dev, irq);
119 }
aae9460e
PB
120}
121
471a9bc1
AG
122/* Check whether an MMIO region exists */
123bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
124{
125 return (n < dev->num_mmio);
126}
127
7feb640c 128static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
a1ff8ae0 129 bool may_overlap, int priority)
aae9460e
PB
130{
131 assert(n >= 0 && n < dev->num_mmio);
132
133 if (dev->mmio[n].addr == addr) {
134 /* ??? region already mapped here. */
135 return;
136 }
a8170e5e 137 if (dev->mmio[n].addr != (hwaddr)-1) {
aae9460e 138 /* Unregister previous mapping. */
e114fead 139 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
aae9460e
PB
140 }
141 dev->mmio[n].addr = addr;
7feb640c
AK
142 if (may_overlap) {
143 memory_region_add_subregion_overlap(get_system_memory(),
144 addr,
145 dev->mmio[n].memory,
146 priority);
147 }
148 else {
149 memory_region_add_subregion(get_system_memory(),
150 addr,
151 dev->mmio[n].memory);
152 }
aae9460e
PB
153}
154
7feb640c
AK
155void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
156{
157 sysbus_mmio_map_common(dev, n, addr, false, 0);
158}
159
160void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
a1ff8ae0 161 int priority)
7feb640c
AK
162{
163 sysbus_mmio_map_common(dev, n, addr, true, priority);
164}
aae9460e
PB
165
166/* Request an IRQ source. The actual IRQ object may be populated later. */
167void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
168{
b5917219 169 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
aae9460e
PB
170}
171
172/* Pass IRQs from a target device. */
173void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
174{
b5917219 175 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
aae9460e
PB
176}
177
750ecd44 178void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
ec3bb837
AK
179{
180 int n;
181
182 assert(dev->num_mmio < QDEV_MAX_MMIO);
183 n = dev->num_mmio++;
184 dev->mmio[n].addr = -1;
ec3bb837
AK
185 dev->mmio[n].memory = memory;
186}
187
46c305ef
PM
188MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
189{
190 return dev->mmio[n].memory;
191}
192
89a80e74 193void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
c646f74f 194{
89a80e74 195 uint32_t i;
c646f74f
GN
196
197 for (i = 0; i < size; i++) {
198 assert(dev->num_pio < QDEV_MAX_PIO);
199 dev->pio[dev->num_pio++] = ioport++;
200 }
201}
202
d307af79 203static int sysbus_device_init(DeviceState *dev)
aae9460e 204{
999e12bb
AL
205 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
206 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
aae9460e 207
4ce5dae8
PM
208 if (!sbc->init) {
209 return 0;
210 }
999e12bb 211 return sbc->init(sd);
aae9460e
PB
212}
213
aae9460e 214DeviceState *sysbus_create_varargs(const char *name,
a8170e5e 215 hwaddr addr, ...)
aae9460e
PB
216{
217 DeviceState *dev;
218 SysBusDevice *s;
219 va_list va;
220 qemu_irq irq;
221 int n;
222
223 dev = qdev_create(NULL, name);
1356b98d 224 s = SYS_BUS_DEVICE(dev);
e23a1b33 225 qdev_init_nofail(dev);
a8170e5e 226 if (addr != (hwaddr)-1) {
aae9460e
PB
227 sysbus_mmio_map(s, 0, addr);
228 }
229 va_start(va, addr);
230 n = 0;
231 while (1) {
4912371f
BS
232 irq = va_arg(va, qemu_irq);
233 if (!irq) {
234 break;
235 }
236 sysbus_connect_irq(s, n, irq);
237 n++;
238 }
d0bc5bc3 239 va_end(va);
4912371f
BS
240 return dev;
241}
242
243DeviceState *sysbus_try_create_varargs(const char *name,
a8170e5e 244 hwaddr addr, ...)
4912371f
BS
245{
246 DeviceState *dev;
247 SysBusDevice *s;
248 va_list va;
249 qemu_irq irq;
250 int n;
251
252 dev = qdev_try_create(NULL, name);
253 if (!dev) {
254 return NULL;
255 }
1356b98d 256 s = SYS_BUS_DEVICE(dev);
4912371f 257 qdev_init_nofail(dev);
a8170e5e 258 if (addr != (hwaddr)-1) {
4912371f
BS
259 sysbus_mmio_map(s, 0, addr);
260 }
261 va_start(va, addr);
262 n = 0;
263 while (1) {
aae9460e
PB
264 irq = va_arg(va, qemu_irq);
265 if (!irq) {
266 break;
267 }
268 sysbus_connect_irq(s, n, irq);
269 n++;
270 }
d0bc5bc3 271 va_end(va);
aae9460e
PB
272 return dev;
273}
cae4956e 274
10c4c98a 275static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
cae4956e 276{
1356b98d 277 SysBusDevice *s = SYS_BUS_DEVICE(dev);
a8170e5e 278 hwaddr size;
cae4956e
GH
279 int i;
280
281 for (i = 0; i < s->num_mmio; i++) {
e114fead 282 size = memory_region_size(s->mmio[i].memory);
cae4956e 283 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
3f7f1c80 284 indent, "", s->mmio[i].addr, size);
cae4956e
GH
285 }
286}
c646f74f
GN
287
288static char *sysbus_get_fw_dev_path(DeviceState *dev)
289{
1356b98d 290 SysBusDevice *s = SYS_BUS_DEVICE(dev);
0b336b3b
LE
291 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
292 /* for the explicit unit address fallback case: */
293 char *addr, *fw_dev_path;
c646f74f
GN
294
295 if (s->num_mmio) {
5ba03e2d
LE
296 return g_strdup_printf("%s@" TARGET_FMT_plx, qdev_fw_name(dev),
297 s->mmio[0].addr);
c646f74f 298 }
5ba03e2d
LE
299 if (s->num_pio) {
300 return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
301 }
0b336b3b
LE
302 if (sbc->explicit_ofw_unit_address) {
303 addr = sbc->explicit_ofw_unit_address(s);
304 if (addr) {
305 fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
306 g_free(addr);
307 return fw_dev_path;
308 }
309 }
5ba03e2d 310 return g_strdup(qdev_fw_name(dev));
c646f74f 311}
2b985d9c 312
a8170e5e 313void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
2b985d9c
AK
314 MemoryRegion *mem)
315{
316 memory_region_add_subregion(get_system_io(), addr, mem);
317}
318
62ec4832
AK
319MemoryRegion *sysbus_address_space(SysBusDevice *dev)
320{
321 return get_system_memory();
322}
999e12bb 323
39bffca2
AL
324static void sysbus_device_class_init(ObjectClass *klass, void *data)
325{
326 DeviceClass *k = DEVICE_CLASS(klass);
327 k->init = sysbus_device_init;
0d936928 328 k->bus_type = TYPE_SYSTEM_BUS;
39bffca2
AL
329}
330
8c43a6f0 331static const TypeInfo sysbus_device_type_info = {
999e12bb
AL
332 .name = TYPE_SYS_BUS_DEVICE,
333 .parent = TYPE_DEVICE,
334 .instance_size = sizeof(SysBusDevice),
335 .abstract = true,
336 .class_size = sizeof(SysBusDeviceClass),
39bffca2 337 .class_init = sysbus_device_class_init,
999e12bb
AL
338};
339
8185d216
PB
340/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
341static BusState *main_system_bus;
342
343static void main_system_bus_create(void)
344{
345 /* assign main_system_bus before qbus_create_inplace()
346 * in order to make "if (bus != sysbus_get_default())" work */
0d936928 347 main_system_bus = g_malloc0(system_bus_info.instance_size);
fb17dfe0
AF
348 qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
349 TYPE_SYSTEM_BUS, NULL, "main-system-bus");
64b625f4 350 OBJECT(main_system_bus)->free = g_free;
f968fc68
AL
351 object_property_add_child(container_get(qdev_get_machine(),
352 "/unattached"),
353 "sysbus", OBJECT(main_system_bus), NULL);
8185d216
PB
354}
355
356BusState *sysbus_get_default(void)
357{
358 if (!main_system_bus) {
359 main_system_bus_create();
360 }
361 return main_system_bus;
362}
363
83f7d43a 364static void sysbus_register_types(void)
999e12bb 365{
0d936928 366 type_register_static(&system_bus_info);
999e12bb
AL
367 type_register_static(&sysbus_device_type_info);
368}
369
83f7d43a 370type_init(sysbus_register_types)