sysbus: add a sysbus_mmio_unmap() helper
[qemu/ar7.git] / hw / core / sysbus.c
blob689a867a2274905a7a0aa167b89dc4ae1f69af93
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 "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "hw/sysbus.h"
23 #include "monitor/monitor.h"
24 #include "exec/address-spaces.h"
26 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
27 static char *sysbus_get_fw_dev_path(DeviceState *dev);
29 typedef struct SysBusFind {
30 void *opaque;
31 FindSysbusDeviceFunc *func;
32 } SysBusFind;
34 /* Run func() for every sysbus device, traverse the tree for everything else */
35 static int find_sysbus_device(Object *obj, void *opaque)
37 SysBusFind *find = opaque;
38 Object *dev;
39 SysBusDevice *sbdev;
41 dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
42 sbdev = (SysBusDevice *)dev;
44 if (!sbdev) {
45 /* Container, traverse it for children */
46 return object_child_foreach(obj, find_sysbus_device, opaque);
49 find->func(sbdev, find->opaque);
51 return 0;
55 * Loop through all dynamically created sysbus devices and call
56 * func() for each instance.
58 void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
60 Object *container;
61 SysBusFind find = {
62 .func = func,
63 .opaque = opaque,
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);
74 static void system_bus_class_init(ObjectClass *klass, void *data)
76 BusClass *k = BUS_CLASS(klass);
78 k->print_dev = sysbus_dev_print;
79 k->get_fw_dev_path = sysbus_get_fw_dev_path;
82 static 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,
89 /* Check whether an IRQ source exists */
90 bool sysbus_has_irq(SysBusDevice *dev, int n)
92 char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
93 ObjectProperty *r;
95 r = object_property_find(OBJECT(dev), prop, NULL);
96 g_free(prop);
98 return (r != NULL);
101 bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
103 return !!sysbus_get_connected_irq(dev, n);
106 qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
108 DeviceState *d = DEVICE(dev);
109 return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
112 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
114 SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev);
116 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
118 if (sbd->connect_irq_notifier) {
119 sbd->connect_irq_notifier(dev, irq);
123 /* Check whether an MMIO region exists */
124 bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
126 return (n < dev->num_mmio);
129 static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
130 bool may_overlap, int priority)
132 assert(n >= 0 && n < dev->num_mmio);
134 if (dev->mmio[n].addr == addr) {
135 /* ??? region already mapped here. */
136 return;
138 if (dev->mmio[n].addr != (hwaddr)-1) {
139 /* Unregister previous mapping. */
140 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
142 dev->mmio[n].addr = addr;
143 if (may_overlap) {
144 memory_region_add_subregion_overlap(get_system_memory(),
145 addr,
146 dev->mmio[n].memory,
147 priority);
149 else {
150 memory_region_add_subregion(get_system_memory(),
151 addr,
152 dev->mmio[n].memory);
156 void sysbus_mmio_unmap(SysBusDevice *dev, int n)
158 assert(n >= 0 && n < dev->num_mmio);
160 if (dev->mmio[n].addr != (hwaddr)-1) {
161 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
162 dev->mmio[n].addr = (hwaddr)-1;
166 void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
168 sysbus_mmio_map_common(dev, n, addr, false, 0);
171 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
172 int priority)
174 sysbus_mmio_map_common(dev, n, addr, true, priority);
177 /* Request an IRQ source. The actual IRQ object may be populated later. */
178 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
180 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
183 /* Pass IRQs from a target device. */
184 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
186 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
189 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
191 int n;
193 assert(dev->num_mmio < QDEV_MAX_MMIO);
194 n = dev->num_mmio++;
195 dev->mmio[n].addr = -1;
196 dev->mmio[n].memory = memory;
199 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
201 return dev->mmio[n].memory;
204 void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
206 uint32_t i;
208 for (i = 0; i < size; i++) {
209 assert(dev->num_pio < QDEV_MAX_PIO);
210 dev->pio[dev->num_pio++] = ioport++;
214 /* The purpose of preserving this empty realize function
215 * is to prevent the parent_realize field of some subclasses
216 * from being set to NULL to break the normal init/realize
217 * of some devices.
219 static void sysbus_realize(DeviceState *dev, Error **errp)
223 DeviceState *sysbus_create_varargs(const char *name,
224 hwaddr addr, ...)
226 DeviceState *dev;
227 SysBusDevice *s;
228 va_list va;
229 qemu_irq irq;
230 int n;
232 dev = qdev_create(NULL, name);
233 s = SYS_BUS_DEVICE(dev);
234 qdev_init_nofail(dev);
235 if (addr != (hwaddr)-1) {
236 sysbus_mmio_map(s, 0, addr);
238 va_start(va, addr);
239 n = 0;
240 while (1) {
241 irq = va_arg(va, qemu_irq);
242 if (!irq) {
243 break;
245 sysbus_connect_irq(s, n, irq);
246 n++;
248 va_end(va);
249 return dev;
252 DeviceState *sysbus_try_create_varargs(const char *name,
253 hwaddr addr, ...)
255 DeviceState *dev;
256 SysBusDevice *s;
257 va_list va;
258 qemu_irq irq;
259 int n;
261 dev = qdev_try_create(NULL, name);
262 if (!dev) {
263 return NULL;
265 s = SYS_BUS_DEVICE(dev);
266 qdev_init_nofail(dev);
267 if (addr != (hwaddr)-1) {
268 sysbus_mmio_map(s, 0, addr);
270 va_start(va, addr);
271 n = 0;
272 while (1) {
273 irq = va_arg(va, qemu_irq);
274 if (!irq) {
275 break;
277 sysbus_connect_irq(s, n, irq);
278 n++;
280 va_end(va);
281 return dev;
284 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
286 SysBusDevice *s = SYS_BUS_DEVICE(dev);
287 hwaddr size;
288 int i;
290 for (i = 0; i < s->num_mmio; i++) {
291 size = memory_region_size(s->mmio[i].memory);
292 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
293 indent, "", s->mmio[i].addr, size);
297 static char *sysbus_get_fw_dev_path(DeviceState *dev)
299 SysBusDevice *s = SYS_BUS_DEVICE(dev);
300 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
301 char *addr, *fw_dev_path;
303 if (sbc->explicit_ofw_unit_address) {
304 addr = sbc->explicit_ofw_unit_address(s);
305 if (addr) {
306 fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
307 g_free(addr);
308 return fw_dev_path;
311 if (s->num_mmio) {
312 return g_strdup_printf("%s@" TARGET_FMT_plx, qdev_fw_name(dev),
313 s->mmio[0].addr);
315 if (s->num_pio) {
316 return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
318 return g_strdup(qdev_fw_name(dev));
321 void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
322 MemoryRegion *mem)
324 memory_region_add_subregion(get_system_io(), addr, mem);
327 MemoryRegion *sysbus_address_space(SysBusDevice *dev)
329 return get_system_memory();
332 static void sysbus_device_class_init(ObjectClass *klass, void *data)
334 DeviceClass *k = DEVICE_CLASS(klass);
335 k->realize = sysbus_realize;
336 k->bus_type = TYPE_SYSTEM_BUS;
338 * device_add plugs devices into a suitable bus. For "real" buses,
339 * that actually connects the device. For sysbus, the connections
340 * need to be made separately, and device_add can't do that. The
341 * device would be left unconnected, and will probably not work
343 * However, a few machines can handle device_add/-device with
344 * a few specific sysbus devices. In those cases, the device
345 * subclass needs to override it and set user_creatable=true.
347 k->user_creatable = false;
350 static const TypeInfo sysbus_device_type_info = {
351 .name = TYPE_SYS_BUS_DEVICE,
352 .parent = TYPE_DEVICE,
353 .instance_size = sizeof(SysBusDevice),
354 .abstract = true,
355 .class_size = sizeof(SysBusDeviceClass),
356 .class_init = sysbus_device_class_init,
359 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
360 static BusState *main_system_bus;
362 static void main_system_bus_create(void)
364 /* assign main_system_bus before qbus_create_inplace()
365 * in order to make "if (bus != sysbus_get_default())" work */
366 main_system_bus = g_malloc0(system_bus_info.instance_size);
367 qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
368 TYPE_SYSTEM_BUS, NULL, "main-system-bus");
369 OBJECT(main_system_bus)->free = g_free;
372 BusState *sysbus_get_default(void)
374 if (!main_system_bus) {
375 main_system_bus_create();
377 return main_system_bus;
380 void sysbus_init_child_obj(Object *parent, const char *childname, void *child,
381 size_t childsize, const char *childtype)
383 object_initialize_child(parent, childname, child, childsize, childtype,
384 &error_abort, NULL);
385 qdev_set_parent_bus(DEVICE(child), sysbus_get_default());
388 static void sysbus_register_types(void)
390 type_register_static(&system_bus_info);
391 type_register_static(&sysbus_device_type_info);
394 type_init(sysbus_register_types)