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