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 static void system_bus_class_init(ObjectClass *klass, void *data)
29 BusClass *k = BUS_CLASS(klass);
31 k->print_dev = sysbus_dev_print;
32 k->get_fw_dev_path = sysbus_get_fw_dev_path;
35 static const TypeInfo system_bus_info = {
36 .name = TYPE_SYSTEM_BUS,
38 .instance_size = sizeof(BusState),
39 .class_init = system_bus_class_init,
42 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
44 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
47 static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
48 bool may_overlap, int priority)
50 assert(n >= 0 && n < dev->num_mmio);
52 if (dev->mmio[n].addr == addr) {
53 /* ??? region already mapped here. */
56 if (dev->mmio[n].addr != (hwaddr)-1) {
57 /* Unregister previous mapping. */
58 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
60 dev->mmio[n].addr = addr;
62 memory_region_add_subregion_overlap(get_system_memory(),
68 memory_region_add_subregion(get_system_memory(),
74 void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
76 sysbus_mmio_map_common(dev, n, addr, false, 0);
79 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
82 sysbus_mmio_map_common(dev, n, addr, true, priority);
85 /* Request an IRQ source. The actual IRQ object may be populated later. */
86 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
88 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
91 /* Pass IRQs from a target device. */
92 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
94 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
97 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
101 assert(dev->num_mmio < QDEV_MAX_MMIO);
103 dev->mmio[n].addr = -1;
104 dev->mmio[n].memory = memory;
107 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
109 return dev->mmio[n].memory;
112 void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
116 for (i = 0; i < size; i++) {
117 assert(dev->num_pio < QDEV_MAX_PIO);
118 dev->pio[dev->num_pio++] = ioport++;
122 static int sysbus_device_init(DeviceState *dev)
124 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
125 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
130 return sbc->init(sd);
133 DeviceState *sysbus_create_varargs(const char *name,
142 dev = qdev_create(NULL, name);
143 s = SYS_BUS_DEVICE(dev);
144 qdev_init_nofail(dev);
145 if (addr != (hwaddr)-1) {
146 sysbus_mmio_map(s, 0, addr);
151 irq = va_arg(va, qemu_irq);
155 sysbus_connect_irq(s, n, irq);
162 DeviceState *sysbus_try_create_varargs(const char *name,
171 dev = qdev_try_create(NULL, name);
175 s = SYS_BUS_DEVICE(dev);
176 qdev_init_nofail(dev);
177 if (addr != (hwaddr)-1) {
178 sysbus_mmio_map(s, 0, addr);
183 irq = va_arg(va, qemu_irq);
187 sysbus_connect_irq(s, n, irq);
194 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
196 SysBusDevice *s = SYS_BUS_DEVICE(dev);
200 for (i = 0; i < s->num_mmio; i++) {
201 size = memory_region_size(s->mmio[i].memory);
202 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
203 indent, "", s->mmio[i].addr, size);
207 static char *sysbus_get_fw_dev_path(DeviceState *dev)
209 SysBusDevice *s = SYS_BUS_DEVICE(dev);
213 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
216 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
218 } else if (s->num_pio) {
219 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
222 return g_strdup(path);
225 void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
228 memory_region_add_subregion(get_system_io(), addr, mem);
231 MemoryRegion *sysbus_address_space(SysBusDevice *dev)
233 return get_system_memory();
236 static void sysbus_device_class_init(ObjectClass *klass, void *data)
238 DeviceClass *k = DEVICE_CLASS(klass);
239 k->init = sysbus_device_init;
240 k->bus_type = TYPE_SYSTEM_BUS;
242 * device_add plugs devices into suitable bus. For "real" buses,
243 * that actually connects the device. For sysbus, the connections
244 * need to be made separately, and device_add can't do that. The
245 * device would be left unconnected, and could not possibly work.
247 k->cannot_instantiate_with_device_add_yet = true;
250 static const TypeInfo sysbus_device_type_info = {
251 .name = TYPE_SYS_BUS_DEVICE,
252 .parent = TYPE_DEVICE,
253 .instance_size = sizeof(SysBusDevice),
255 .class_size = sizeof(SysBusDeviceClass),
256 .class_init = sysbus_device_class_init,
259 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
260 static BusState *main_system_bus;
262 static void main_system_bus_create(void)
264 /* assign main_system_bus before qbus_create_inplace()
265 * in order to make "if (bus != sysbus_get_default())" work */
266 main_system_bus = g_malloc0(system_bus_info.instance_size);
267 qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
268 TYPE_SYSTEM_BUS, NULL, "main-system-bus");
269 OBJECT(main_system_bus)->free = g_free;
270 object_property_add_child(container_get(qdev_get_machine(),
272 "sysbus", OBJECT(main_system_bus), NULL);
275 BusState *sysbus_get_default(void)
277 if (!main_system_bus) {
278 main_system_bus_create();
280 return main_system_bus;
283 static void sysbus_register_types(void)
285 type_register_static(&system_bus_info);
286 type_register_static(&sysbus_device_type_info);
289 type_init(sysbus_register_types)