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.1 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 "qemu/module.h"
23 #include "hw/sysbus.h"
24 #include "monitor/monitor.h"
25 #include "exec/address-spaces.h"
27 static void sysbus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
28 static char *sysbus_get_fw_dev_path(DeviceState
*dev
);
30 typedef struct SysBusFind
{
32 FindSysbusDeviceFunc
*func
;
35 /* Run func() for every sysbus device, traverse the tree for everything else */
36 static int find_sysbus_device(Object
*obj
, void *opaque
)
38 SysBusFind
*find
= opaque
;
42 dev
= object_dynamic_cast(obj
, TYPE_SYS_BUS_DEVICE
);
43 sbdev
= (SysBusDevice
*)dev
;
46 /* Container, traverse it for children */
47 return object_child_foreach(obj
, find_sysbus_device
, opaque
);
50 find
->func(sbdev
, find
->opaque
);
56 * Loop through all dynamically created sysbus devices and call
57 * func() for each instance.
59 void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc
*func
, void *opaque
)
67 /* Loop through all sysbus devices that were spawned outside the machine */
68 container
= container_get(qdev_get_machine(), "/peripheral");
69 find_sysbus_device(container
, &find
);
70 container
= container_get(qdev_get_machine(), "/peripheral-anon");
71 find_sysbus_device(container
, &find
);
75 static void system_bus_class_init(ObjectClass
*klass
, void *data
)
77 BusClass
*k
= BUS_CLASS(klass
);
79 k
->print_dev
= sysbus_dev_print
;
80 k
->get_fw_dev_path
= sysbus_get_fw_dev_path
;
83 static const TypeInfo system_bus_info
= {
84 .name
= TYPE_SYSTEM_BUS
,
86 .instance_size
= sizeof(BusState
),
87 .class_init
= system_bus_class_init
,
90 /* Check whether an IRQ source exists */
91 bool sysbus_has_irq(SysBusDevice
*dev
, int n
)
93 char *prop
= g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ
, n
);
96 r
= object_property_find(OBJECT(dev
), prop
);
102 bool sysbus_is_irq_connected(SysBusDevice
*dev
, int n
)
104 return !!sysbus_get_connected_irq(dev
, n
);
107 qemu_irq
sysbus_get_connected_irq(SysBusDevice
*dev
, int n
)
109 DeviceState
*d
= DEVICE(dev
);
110 return qdev_get_gpio_out_connector(d
, SYSBUS_DEVICE_GPIO_IRQ
, n
);
113 void sysbus_connect_irq(SysBusDevice
*dev
, int n
, qemu_irq irq
)
115 SysBusDeviceClass
*sbd
= SYS_BUS_DEVICE_GET_CLASS(dev
);
117 qdev_connect_gpio_out_named(DEVICE(dev
), SYSBUS_DEVICE_GPIO_IRQ
, n
, irq
);
119 if (sbd
->connect_irq_notifier
) {
120 sbd
->connect_irq_notifier(dev
, irq
);
124 /* Check whether an MMIO region exists */
125 bool sysbus_has_mmio(SysBusDevice
*dev
, unsigned int n
)
127 return (n
< dev
->num_mmio
);
130 static void sysbus_mmio_map_common(SysBusDevice
*dev
, int n
, hwaddr addr
,
131 bool may_overlap
, int priority
)
133 assert(n
>= 0 && n
< dev
->num_mmio
);
135 if (dev
->mmio
[n
].addr
== addr
) {
136 /* ??? region already mapped here. */
139 if (dev
->mmio
[n
].addr
!= (hwaddr
)-1) {
140 /* Unregister previous mapping. */
141 memory_region_del_subregion(get_system_memory(), dev
->mmio
[n
].memory
);
143 dev
->mmio
[n
].addr
= addr
;
145 memory_region_add_subregion_overlap(get_system_memory(),
151 memory_region_add_subregion(get_system_memory(),
153 dev
->mmio
[n
].memory
);
157 void sysbus_mmio_map(SysBusDevice
*dev
, int n
, hwaddr addr
)
159 sysbus_mmio_map_common(dev
, n
, addr
, false, 0);
162 void sysbus_mmio_map_overlap(SysBusDevice
*dev
, int n
, hwaddr addr
,
165 sysbus_mmio_map_common(dev
, n
, addr
, true, priority
);
168 /* Request an IRQ source. The actual IRQ object may be populated later. */
169 void sysbus_init_irq(SysBusDevice
*dev
, qemu_irq
*p
)
171 qdev_init_gpio_out_named(DEVICE(dev
), p
, SYSBUS_DEVICE_GPIO_IRQ
, 1);
174 /* Pass IRQs from a target device. */
175 void sysbus_pass_irq(SysBusDevice
*dev
, SysBusDevice
*target
)
177 qdev_pass_gpios(DEVICE(target
), DEVICE(dev
), SYSBUS_DEVICE_GPIO_IRQ
);
180 void sysbus_init_mmio(SysBusDevice
*dev
, MemoryRegion
*memory
)
184 assert(dev
->num_mmio
< QDEV_MAX_MMIO
);
186 dev
->mmio
[n
].addr
= -1;
187 dev
->mmio
[n
].memory
= memory
;
190 MemoryRegion
*sysbus_mmio_get_region(SysBusDevice
*dev
, int n
)
192 assert(n
>= 0 && n
< QDEV_MAX_MMIO
);
193 return dev
->mmio
[n
].memory
;
196 void sysbus_init_ioports(SysBusDevice
*dev
, uint32_t ioport
, uint32_t size
)
200 for (i
= 0; i
< size
; i
++) {
201 assert(dev
->num_pio
< QDEV_MAX_PIO
);
202 dev
->pio
[dev
->num_pio
++] = ioport
++;
206 /* The purpose of preserving this empty realize function
207 * is to prevent the parent_realize field of some subclasses
208 * from being set to NULL to break the normal init/realize
211 static void sysbus_device_realize(DeviceState
*dev
, Error
**errp
)
215 DeviceState
*sysbus_create_varargs(const char *name
,
224 dev
= qdev_new(name
);
225 s
= SYS_BUS_DEVICE(dev
);
226 sysbus_realize_and_unref(s
, &error_fatal
);
227 if (addr
!= (hwaddr
)-1) {
228 sysbus_mmio_map(s
, 0, addr
);
233 irq
= va_arg(va
, qemu_irq
);
237 sysbus_connect_irq(s
, n
, irq
);
244 bool sysbus_realize(SysBusDevice
*dev
, Error
**errp
)
246 return qdev_realize(DEVICE(dev
), sysbus_get_default(), errp
);
249 bool sysbus_realize_and_unref(SysBusDevice
*dev
, Error
**errp
)
251 return qdev_realize_and_unref(DEVICE(dev
), sysbus_get_default(), errp
);
254 static void sysbus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
256 SysBusDevice
*s
= SYS_BUS_DEVICE(dev
);
260 for (i
= 0; i
< s
->num_mmio
; i
++) {
261 size
= memory_region_size(s
->mmio
[i
].memory
);
262 monitor_printf(mon
, "%*smmio " HWADDR_FMT_plx
"/" HWADDR_FMT_plx
"\n",
263 indent
, "", s
->mmio
[i
].addr
, size
);
267 static char *sysbus_get_fw_dev_path(DeviceState
*dev
)
269 SysBusDevice
*s
= SYS_BUS_DEVICE(dev
);
270 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_GET_CLASS(s
);
271 char *addr
, *fw_dev_path
;
273 if (sbc
->explicit_ofw_unit_address
) {
274 addr
= sbc
->explicit_ofw_unit_address(s
);
276 fw_dev_path
= g_strdup_printf("%s@%s", qdev_fw_name(dev
), addr
);
282 return g_strdup_printf("%s@" HWADDR_FMT_plx
, qdev_fw_name(dev
),
286 return g_strdup_printf("%s@i%04x", qdev_fw_name(dev
), s
->pio
[0]);
288 return g_strdup(qdev_fw_name(dev
));
291 static void sysbus_device_class_init(ObjectClass
*klass
, void *data
)
293 DeviceClass
*k
= DEVICE_CLASS(klass
);
294 k
->realize
= sysbus_device_realize
;
295 k
->bus_type
= TYPE_SYSTEM_BUS
;
297 * device_add plugs devices into a suitable bus. For "real" buses,
298 * that actually connects the device. For sysbus, the connections
299 * need to be made separately, and device_add can't do that. The
300 * device would be left unconnected, and will probably not work
302 * However, a few machines can handle device_add/-device with
303 * a few specific sysbus devices. In those cases, the device
304 * subclass needs to override it and set user_creatable=true.
306 k
->user_creatable
= false;
309 static const TypeInfo sysbus_device_type_info
= {
310 .name
= TYPE_SYS_BUS_DEVICE
,
311 .parent
= TYPE_DEVICE
,
312 .instance_size
= sizeof(SysBusDevice
),
314 .class_size
= sizeof(SysBusDeviceClass
),
315 .class_init
= sysbus_device_class_init
,
318 static BusState
*main_system_bus
;
320 static void main_system_bus_create(void)
323 * assign main_system_bus before qbus_init()
324 * in order to make "if (bus != sysbus_get_default())" work
326 main_system_bus
= g_malloc0(system_bus_info
.instance_size
);
327 qbus_init(main_system_bus
, system_bus_info
.instance_size
,
328 TYPE_SYSTEM_BUS
, NULL
, "main-system-bus");
329 OBJECT(main_system_bus
)->free
= g_free
;
332 BusState
*sysbus_get_default(void)
334 if (!main_system_bus
) {
335 main_system_bus_create();
337 return main_system_bus
;
340 static void sysbus_register_types(void)
342 type_register_static(&system_bus_info
);
343 type_register_static(&sysbus_device_type_info
);
346 type_init(sysbus_register_types
)