Revert "gdbstub: Do not kill target in system emulation mode"
[qemu/qmp-unstable.git] / hw / core / sysbus.c
blobb53c351aa489b20cab5d7930e57f28d1cda56730
1 /*
2 * System (CPU) Bus device support code
4 * Copyright (c) 2009 CodeSourcery
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "hw/sysbus.h"
21 #include "monitor/monitor.h"
22 #include "exec/address-spaces.h"
24 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25 static char *sysbus_get_fw_dev_path(DeviceState *dev);
27 typedef struct SysBusFind {
28 void *opaque;
29 FindSysbusDeviceFunc *func;
30 } SysBusFind;
32 /* Run func() for every sysbus device, traverse the tree for everything else */
33 static int find_sysbus_device(Object *obj, void *opaque)
35 SysBusFind *find = opaque;
36 Object *dev;
37 SysBusDevice *sbdev;
39 dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
40 sbdev = (SysBusDevice *)dev;
42 if (!sbdev) {
43 /* Container, traverse it for children */
44 return object_child_foreach(obj, find_sysbus_device, opaque);
47 find->func(sbdev, find->opaque);
49 return 0;
53 * Loop through all dynamically created sysbus devices and call
54 * func() for each instance.
56 void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
58 Object *container;
59 SysBusFind find = {
60 .func = func,
61 .opaque = opaque,
64 /* Loop through all sysbus devices that were spawened outside the machine */
65 container = container_get(qdev_get_machine(), "/peripheral");
66 find_sysbus_device(container, &find);
67 container = container_get(qdev_get_machine(), "/peripheral-anon");
68 find_sysbus_device(container, &find);
72 static void system_bus_class_init(ObjectClass *klass, void *data)
74 BusClass *k = BUS_CLASS(klass);
76 k->print_dev = sysbus_dev_print;
77 k->get_fw_dev_path = sysbus_get_fw_dev_path;
80 static const TypeInfo system_bus_info = {
81 .name = TYPE_SYSTEM_BUS,
82 .parent = TYPE_BUS,
83 .instance_size = sizeof(BusState),
84 .class_init = system_bus_class_init,
87 /* Check whether an IRQ source exists */
88 bool sysbus_has_irq(SysBusDevice *dev, int n)
90 char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
91 ObjectProperty *r;
93 r = object_property_find(OBJECT(dev), prop, NULL);
94 g_free(prop);
96 return (r != NULL);
99 bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
101 return !!sysbus_get_connected_irq(dev, n);
104 qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
106 DeviceState *d = DEVICE(dev);
107 return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
110 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
112 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
115 /* Check whether an MMIO region exists */
116 bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
118 return (n < dev->num_mmio);
121 static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
122 bool may_overlap, int priority)
124 assert(n >= 0 && n < dev->num_mmio);
126 if (dev->mmio[n].addr == addr) {
127 /* ??? region already mapped here. */
128 return;
130 if (dev->mmio[n].addr != (hwaddr)-1) {
131 /* Unregister previous mapping. */
132 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
134 dev->mmio[n].addr = addr;
135 if (may_overlap) {
136 memory_region_add_subregion_overlap(get_system_memory(),
137 addr,
138 dev->mmio[n].memory,
139 priority);
141 else {
142 memory_region_add_subregion(get_system_memory(),
143 addr,
144 dev->mmio[n].memory);
148 void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
150 sysbus_mmio_map_common(dev, n, addr, false, 0);
153 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
154 int priority)
156 sysbus_mmio_map_common(dev, n, addr, true, priority);
159 /* Request an IRQ source. The actual IRQ object may be populated later. */
160 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
162 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
165 /* Pass IRQs from a target device. */
166 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
168 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
171 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
173 int n;
175 assert(dev->num_mmio < QDEV_MAX_MMIO);
176 n = dev->num_mmio++;
177 dev->mmio[n].addr = -1;
178 dev->mmio[n].memory = memory;
181 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
183 return dev->mmio[n].memory;
186 void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
188 pio_addr_t i;
190 for (i = 0; i < size; i++) {
191 assert(dev->num_pio < QDEV_MAX_PIO);
192 dev->pio[dev->num_pio++] = ioport++;
196 static int sysbus_device_init(DeviceState *dev)
198 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
199 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
201 if (!sbc->init) {
202 return 0;
204 return sbc->init(sd);
207 DeviceState *sysbus_create_varargs(const char *name,
208 hwaddr addr, ...)
210 DeviceState *dev;
211 SysBusDevice *s;
212 va_list va;
213 qemu_irq irq;
214 int n;
216 dev = qdev_create(NULL, name);
217 s = SYS_BUS_DEVICE(dev);
218 qdev_init_nofail(dev);
219 if (addr != (hwaddr)-1) {
220 sysbus_mmio_map(s, 0, addr);
222 va_start(va, addr);
223 n = 0;
224 while (1) {
225 irq = va_arg(va, qemu_irq);
226 if (!irq) {
227 break;
229 sysbus_connect_irq(s, n, irq);
230 n++;
232 va_end(va);
233 return dev;
236 DeviceState *sysbus_try_create_varargs(const char *name,
237 hwaddr addr, ...)
239 DeviceState *dev;
240 SysBusDevice *s;
241 va_list va;
242 qemu_irq irq;
243 int n;
245 dev = qdev_try_create(NULL, name);
246 if (!dev) {
247 return NULL;
249 s = SYS_BUS_DEVICE(dev);
250 qdev_init_nofail(dev);
251 if (addr != (hwaddr)-1) {
252 sysbus_mmio_map(s, 0, addr);
254 va_start(va, addr);
255 n = 0;
256 while (1) {
257 irq = va_arg(va, qemu_irq);
258 if (!irq) {
259 break;
261 sysbus_connect_irq(s, n, irq);
262 n++;
264 va_end(va);
265 return dev;
268 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
270 SysBusDevice *s = SYS_BUS_DEVICE(dev);
271 hwaddr size;
272 int i;
274 for (i = 0; i < s->num_mmio; i++) {
275 size = memory_region_size(s->mmio[i].memory);
276 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
277 indent, "", s->mmio[i].addr, size);
281 static char *sysbus_get_fw_dev_path(DeviceState *dev)
283 SysBusDevice *s = SYS_BUS_DEVICE(dev);
284 char path[40];
285 int off;
287 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
289 if (s->num_mmio) {
290 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
291 s->mmio[0].addr);
292 } else if (s->num_pio) {
293 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
296 return g_strdup(path);
299 void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
300 MemoryRegion *mem)
302 memory_region_add_subregion(get_system_io(), addr, mem);
305 MemoryRegion *sysbus_address_space(SysBusDevice *dev)
307 return get_system_memory();
310 static void sysbus_device_class_init(ObjectClass *klass, void *data)
312 DeviceClass *k = DEVICE_CLASS(klass);
313 k->init = sysbus_device_init;
314 k->bus_type = TYPE_SYSTEM_BUS;
317 static const TypeInfo sysbus_device_type_info = {
318 .name = TYPE_SYS_BUS_DEVICE,
319 .parent = TYPE_DEVICE,
320 .instance_size = sizeof(SysBusDevice),
321 .abstract = true,
322 .class_size = sizeof(SysBusDeviceClass),
323 .class_init = sysbus_device_class_init,
326 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
327 static BusState *main_system_bus;
329 static void main_system_bus_create(void)
331 /* assign main_system_bus before qbus_create_inplace()
332 * in order to make "if (bus != sysbus_get_default())" work */
333 main_system_bus = g_malloc0(system_bus_info.instance_size);
334 qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
335 TYPE_SYSTEM_BUS, NULL, "main-system-bus");
336 OBJECT(main_system_bus)->free = g_free;
337 object_property_add_child(container_get(qdev_get_machine(),
338 "/unattached"),
339 "sysbus", OBJECT(main_system_bus), NULL);
342 BusState *sysbus_get_default(void)
344 if (!main_system_bus) {
345 main_system_bus_create();
347 return main_system_bus;
350 static void sysbus_register_types(void)
352 type_register_static(&system_bus_info);
353 type_register_static(&sysbus_device_type_info);
356 type_init(sysbus_register_types)