io: cope with websock 'Connection' header having multiple values
[qemu/ar7.git] / hw / core / sysbus.c
blob5d0887f499deb59b69798e756b5c4d149ddf7ee8
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 "hw/sysbus.h"
22 #include "monitor/monitor.h"
23 #include "exec/address-spaces.h"
25 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
26 static char *sysbus_get_fw_dev_path(DeviceState *dev);
28 typedef struct SysBusFind {
29 void *opaque;
30 FindSysbusDeviceFunc *func;
31 } SysBusFind;
33 /* Run func() for every sysbus device, traverse the tree for everything else */
34 static int find_sysbus_device(Object *obj, void *opaque)
36 SysBusFind *find = opaque;
37 Object *dev;
38 SysBusDevice *sbdev;
40 dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
41 sbdev = (SysBusDevice *)dev;
43 if (!sbdev) {
44 /* Container, traverse it for children */
45 return object_child_foreach(obj, find_sysbus_device, opaque);
48 find->func(sbdev, find->opaque);
50 return 0;
54 * Loop through all dynamically created sysbus devices and call
55 * func() for each instance.
57 void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
59 Object *container;
60 SysBusFind find = {
61 .func = func,
62 .opaque = opaque,
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);
73 static void system_bus_class_init(ObjectClass *klass, void *data)
75 BusClass *k = BUS_CLASS(klass);
77 k->print_dev = sysbus_dev_print;
78 k->get_fw_dev_path = sysbus_get_fw_dev_path;
81 static 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,
88 /* Check whether an IRQ source exists */
89 bool sysbus_has_irq(SysBusDevice *dev, int n)
91 char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
92 ObjectProperty *r;
94 r = object_property_find(OBJECT(dev), prop, NULL);
95 g_free(prop);
97 return (r != NULL);
100 bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
102 return !!sysbus_get_connected_irq(dev, n);
105 qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
107 DeviceState *d = DEVICE(dev);
108 return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
111 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
113 SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev);
115 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
117 if (sbd->connect_irq_notifier) {
118 sbd->connect_irq_notifier(dev, irq);
122 /* Check whether an MMIO region exists */
123 bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
125 return (n < dev->num_mmio);
128 static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
129 bool may_overlap, int priority)
131 assert(n >= 0 && n < dev->num_mmio);
133 if (dev->mmio[n].addr == addr) {
134 /* ??? region already mapped here. */
135 return;
137 if (dev->mmio[n].addr != (hwaddr)-1) {
138 /* Unregister previous mapping. */
139 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
141 dev->mmio[n].addr = addr;
142 if (may_overlap) {
143 memory_region_add_subregion_overlap(get_system_memory(),
144 addr,
145 dev->mmio[n].memory,
146 priority);
148 else {
149 memory_region_add_subregion(get_system_memory(),
150 addr,
151 dev->mmio[n].memory);
155 void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
157 sysbus_mmio_map_common(dev, n, addr, false, 0);
160 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
161 int priority)
163 sysbus_mmio_map_common(dev, n, addr, true, priority);
166 /* Request an IRQ source. The actual IRQ object may be populated later. */
167 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
169 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
172 /* Pass IRQs from a target device. */
173 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
175 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
178 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
180 int n;
182 assert(dev->num_mmio < QDEV_MAX_MMIO);
183 n = dev->num_mmio++;
184 dev->mmio[n].addr = -1;
185 dev->mmio[n].memory = memory;
188 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
190 return dev->mmio[n].memory;
193 void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
195 uint32_t i;
197 for (i = 0; i < size; i++) {
198 assert(dev->num_pio < QDEV_MAX_PIO);
199 dev->pio[dev->num_pio++] = ioport++;
203 static int sysbus_device_init(DeviceState *dev)
205 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
206 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
208 if (!sbc->init) {
209 return 0;
211 return sbc->init(sd);
214 DeviceState *sysbus_create_varargs(const char *name,
215 hwaddr addr, ...)
217 DeviceState *dev;
218 SysBusDevice *s;
219 va_list va;
220 qemu_irq irq;
221 int n;
223 dev = qdev_create(NULL, name);
224 s = SYS_BUS_DEVICE(dev);
225 qdev_init_nofail(dev);
226 if (addr != (hwaddr)-1) {
227 sysbus_mmio_map(s, 0, addr);
229 va_start(va, addr);
230 n = 0;
231 while (1) {
232 irq = va_arg(va, qemu_irq);
233 if (!irq) {
234 break;
236 sysbus_connect_irq(s, n, irq);
237 n++;
239 va_end(va);
240 return dev;
243 DeviceState *sysbus_try_create_varargs(const char *name,
244 hwaddr addr, ...)
246 DeviceState *dev;
247 SysBusDevice *s;
248 va_list va;
249 qemu_irq irq;
250 int n;
252 dev = qdev_try_create(NULL, name);
253 if (!dev) {
254 return NULL;
256 s = SYS_BUS_DEVICE(dev);
257 qdev_init_nofail(dev);
258 if (addr != (hwaddr)-1) {
259 sysbus_mmio_map(s, 0, addr);
261 va_start(va, addr);
262 n = 0;
263 while (1) {
264 irq = va_arg(va, qemu_irq);
265 if (!irq) {
266 break;
268 sysbus_connect_irq(s, n, irq);
269 n++;
271 va_end(va);
272 return dev;
275 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
277 SysBusDevice *s = SYS_BUS_DEVICE(dev);
278 hwaddr size;
279 int i;
281 for (i = 0; i < s->num_mmio; i++) {
282 size = memory_region_size(s->mmio[i].memory);
283 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
284 indent, "", s->mmio[i].addr, size);
288 static char *sysbus_get_fw_dev_path(DeviceState *dev)
290 SysBusDevice *s = SYS_BUS_DEVICE(dev);
291 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
292 /* for the explicit unit address fallback case: */
293 char *addr, *fw_dev_path;
295 if (s->num_mmio) {
296 return g_strdup_printf("%s@" TARGET_FMT_plx, qdev_fw_name(dev),
297 s->mmio[0].addr);
299 if (s->num_pio) {
300 return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
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;
310 return g_strdup(qdev_fw_name(dev));
313 void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
314 MemoryRegion *mem)
316 memory_region_add_subregion(get_system_io(), addr, mem);
319 MemoryRegion *sysbus_address_space(SysBusDevice *dev)
321 return get_system_memory();
324 static void sysbus_device_class_init(ObjectClass *klass, void *data)
326 DeviceClass *k = DEVICE_CLASS(klass);
327 k->init = sysbus_device_init;
328 k->bus_type = TYPE_SYSTEM_BUS;
330 * device_add plugs devices into a suitable bus. For "real" buses,
331 * that actually connects the device. For sysbus, the connections
332 * need to be made separately, and device_add can't do that. The
333 * device would be left unconnected, and will probably not work
335 * However, a few machines can handle device_add/-device with
336 * a few specific sysbus devices. In those cases, the device
337 * subclass needs to override it and set user_creatable=true.
339 k->user_creatable = false;
342 static const TypeInfo sysbus_device_type_info = {
343 .name = TYPE_SYS_BUS_DEVICE,
344 .parent = TYPE_DEVICE,
345 .instance_size = sizeof(SysBusDevice),
346 .abstract = true,
347 .class_size = sizeof(SysBusDeviceClass),
348 .class_init = sysbus_device_class_init,
351 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
352 static BusState *main_system_bus;
354 static void main_system_bus_create(void)
356 /* assign main_system_bus before qbus_create_inplace()
357 * in order to make "if (bus != sysbus_get_default())" work */
358 main_system_bus = g_malloc0(system_bus_info.instance_size);
359 qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
360 TYPE_SYSTEM_BUS, NULL, "main-system-bus");
361 OBJECT(main_system_bus)->free = g_free;
362 object_property_add_child(container_get(qdev_get_machine(),
363 "/unattached"),
364 "sysbus", OBJECT(main_system_bus), NULL);
367 BusState *sysbus_get_default(void)
369 if (!main_system_bus) {
370 main_system_bus_create();
372 return main_system_bus;
375 static void sysbus_register_types(void)
377 type_register_static(&system_bus_info);
378 type_register_static(&sysbus_device_type_info);
381 type_init(sysbus_register_types)