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/>.
22 #include "exec-memory.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 assert(n
>= 0 && n
< dev
->num_irq
);
51 void sysbus_mmio_map(SysBusDevice
*dev
, int n
, hwaddr addr
)
53 assert(n
>= 0 && n
< dev
->num_mmio
);
55 if (dev
->mmio
[n
].addr
== addr
) {
56 /* ??? region already mapped here. */
59 if (dev
->mmio
[n
].addr
!= (hwaddr
)-1) {
60 /* Unregister previous mapping. */
61 memory_region_del_subregion(get_system_memory(), dev
->mmio
[n
].memory
);
63 dev
->mmio
[n
].addr
= addr
;
64 memory_region_add_subregion(get_system_memory(),
70 /* Request an IRQ source. The actual IRQ object may be populated later. */
71 void sysbus_init_irq(SysBusDevice
*dev
, qemu_irq
*p
)
75 assert(dev
->num_irq
< QDEV_MAX_IRQ
);
80 /* Pass IRQs from a target device. */
81 void sysbus_pass_irq(SysBusDevice
*dev
, SysBusDevice
*target
)
84 assert(dev
->num_irq
== 0);
85 dev
->num_irq
= target
->num_irq
;
86 for (i
= 0; i
< dev
->num_irq
; i
++) {
87 dev
->irqp
[i
] = target
->irqp
[i
];
91 void sysbus_init_mmio(SysBusDevice
*dev
, MemoryRegion
*memory
)
95 assert(dev
->num_mmio
< QDEV_MAX_MMIO
);
97 dev
->mmio
[n
].addr
= -1;
98 dev
->mmio
[n
].memory
= memory
;
101 MemoryRegion
*sysbus_mmio_get_region(SysBusDevice
*dev
, int n
)
103 return dev
->mmio
[n
].memory
;
106 void sysbus_init_ioports(SysBusDevice
*dev
, pio_addr_t ioport
, pio_addr_t size
)
110 for (i
= 0; i
< size
; i
++) {
111 assert(dev
->num_pio
< QDEV_MAX_PIO
);
112 dev
->pio
[dev
->num_pio
++] = ioport
++;
116 static int sysbus_device_init(DeviceState
*dev
)
118 SysBusDevice
*sd
= SYS_BUS_DEVICE(dev
);
119 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_GET_CLASS(sd
);
121 return sbc
->init(sd
);
124 DeviceState
*sysbus_create_varargs(const char *name
,
133 dev
= qdev_create(NULL
, name
);
134 s
= sysbus_from_qdev(dev
);
135 qdev_init_nofail(dev
);
136 if (addr
!= (hwaddr
)-1) {
137 sysbus_mmio_map(s
, 0, addr
);
142 irq
= va_arg(va
, qemu_irq
);
146 sysbus_connect_irq(s
, n
, irq
);
153 DeviceState
*sysbus_try_create_varargs(const char *name
,
162 dev
= qdev_try_create(NULL
, name
);
166 s
= sysbus_from_qdev(dev
);
167 qdev_init_nofail(dev
);
168 if (addr
!= (hwaddr
)-1) {
169 sysbus_mmio_map(s
, 0, addr
);
174 irq
= va_arg(va
, qemu_irq
);
178 sysbus_connect_irq(s
, n
, irq
);
185 static void sysbus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
187 SysBusDevice
*s
= sysbus_from_qdev(dev
);
191 monitor_printf(mon
, "%*sirq %d\n", indent
, "", s
->num_irq
);
192 for (i
= 0; i
< s
->num_mmio
; i
++) {
193 size
= memory_region_size(s
->mmio
[i
].memory
);
194 monitor_printf(mon
, "%*smmio " TARGET_FMT_plx
"/" TARGET_FMT_plx
"\n",
195 indent
, "", s
->mmio
[i
].addr
, size
);
199 static char *sysbus_get_fw_dev_path(DeviceState
*dev
)
201 SysBusDevice
*s
= sysbus_from_qdev(dev
);
205 off
= snprintf(path
, sizeof(path
), "%s", qdev_fw_name(dev
));
208 snprintf(path
+ off
, sizeof(path
) - off
, "@"TARGET_FMT_plx
,
210 } else if (s
->num_pio
) {
211 snprintf(path
+ off
, sizeof(path
) - off
, "@i%04x", s
->pio
[0]);
214 return g_strdup(path
);
217 void sysbus_add_memory(SysBusDevice
*dev
, hwaddr addr
,
220 memory_region_add_subregion(get_system_memory(), addr
, mem
);
223 void sysbus_add_memory_overlap(SysBusDevice
*dev
, hwaddr addr
,
224 MemoryRegion
*mem
, unsigned priority
)
226 memory_region_add_subregion_overlap(get_system_memory(), addr
, mem
,
230 void sysbus_del_memory(SysBusDevice
*dev
, MemoryRegion
*mem
)
232 memory_region_del_subregion(get_system_memory(), mem
);
235 void sysbus_add_io(SysBusDevice
*dev
, hwaddr addr
,
238 memory_region_add_subregion(get_system_io(), addr
, mem
);
241 void sysbus_del_io(SysBusDevice
*dev
, MemoryRegion
*mem
)
243 memory_region_del_subregion(get_system_io(), mem
);
246 MemoryRegion
*sysbus_address_space(SysBusDevice
*dev
)
248 return get_system_memory();
251 static void sysbus_device_class_init(ObjectClass
*klass
, void *data
)
253 DeviceClass
*k
= DEVICE_CLASS(klass
);
254 k
->init
= sysbus_device_init
;
255 k
->bus_type
= TYPE_SYSTEM_BUS
;
258 static TypeInfo sysbus_device_type_info
= {
259 .name
= TYPE_SYS_BUS_DEVICE
,
260 .parent
= TYPE_DEVICE
,
261 .instance_size
= sizeof(SysBusDevice
),
263 .class_size
= sizeof(SysBusDeviceClass
),
264 .class_init
= sysbus_device_class_init
,
267 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
268 static BusState
*main_system_bus
;
270 static void main_system_bus_create(void)
272 /* assign main_system_bus before qbus_create_inplace()
273 * in order to make "if (bus != sysbus_get_default())" work */
274 main_system_bus
= g_malloc0(system_bus_info
.instance_size
);
275 qbus_create_inplace(main_system_bus
, TYPE_SYSTEM_BUS
, NULL
,
277 main_system_bus
->glib_allocated
= true;
278 object_property_add_child(container_get(qdev_get_machine(),
280 "sysbus", OBJECT(main_system_bus
), NULL
);
283 BusState
*sysbus_get_default(void)
285 if (!main_system_bus
) {
286 main_system_bus_create();
288 return main_system_bus
;
291 static void sysbus_register_types(void)
293 type_register_static(&system_bus_info
);
294 type_register_static(&sysbus_device_type_info
);
297 type_init(sysbus_register_types
)