]> Git Repo - qemu.git/blame - hw/core/sysbus.c
sysbus: Add new platform bus helper device
[qemu.git] / hw / core / sysbus.c
CommitLineData
aae9460e
PB
1/*
2 * System (CPU) Bus device support code
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aae9460e
PB
18 */
19
83c9f4ca 20#include "hw/sysbus.h"
83c9089e 21#include "monitor/monitor.h"
022c62cb 22#include "exec/address-spaces.h"
aae9460e 23
10c4c98a 24static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
c646f74f 25static char *sysbus_get_fw_dev_path(DeviceState *dev);
10c4c98a 26
eb572280
AG
27typedef struct SysBusFind {
28 void *opaque;
29 FindSysbusDeviceFunc *func;
30} SysBusFind;
31
32/* Run func() for every sysbus device, traverse the tree for everything else */
33static int find_sysbus_device(Object *obj, void *opaque)
34{
35 SysBusFind *find = opaque;
36 Object *dev;
37 SysBusDevice *sbdev;
38
39 dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
40 sbdev = (SysBusDevice *)dev;
41
42 if (!sbdev) {
43 /* Container, traverse it for children */
44 return object_child_foreach(obj, find_sysbus_device, opaque);
45 }
46
47 find->func(sbdev, find->opaque);
48
49 return 0;
50}
51
52/*
53 * Loop through all dynamically created sysbus devices and call
54 * func() for each instance.
55 */
56void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
57{
58 Object *container;
59 SysBusFind find = {
60 .func = func,
61 .opaque = opaque,
62 };
63
64 /* Loop through all sysbus devices that were spawened outside the machine */
65 container = container_get(qdev_get_machine(), "/peripheral");
66 find_sysbus_device(container, &find);
67 container = container_get(qdev_get_machine(), "/peripheral-anon");
68 find_sysbus_device(container, &find);
69}
70
71
0d936928
AL
72static void system_bus_class_init(ObjectClass *klass, void *data)
73{
74 BusClass *k = BUS_CLASS(klass);
75
76 k->print_dev = sysbus_dev_print;
77 k->get_fw_dev_path = sysbus_get_fw_dev_path;
78}
79
80static const TypeInfo system_bus_info = {
81 .name = TYPE_SYSTEM_BUS,
82 .parent = TYPE_BUS,
83 .instance_size = sizeof(BusState),
84 .class_init = system_bus_class_init,
10c4c98a
GH
85};
86
b7973186
AG
87/* Check whether an IRQ source exists */
88bool sysbus_has_irq(SysBusDevice *dev, int n)
89{
90 char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
91 ObjectProperty *r;
92
93 r = object_property_find(OBJECT(dev), prop, NULL);
94 return (r != NULL);
95}
96
97bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
98{
99 return !!sysbus_get_connected_irq(dev, n);
100}
101
102qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
103{
104 DeviceState *d = DEVICE(dev);
105 return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
106}
107
aae9460e
PB
108void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
109{
b5917219 110 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
aae9460e
PB
111}
112
471a9bc1
AG
113/* Check whether an MMIO region exists */
114bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
115{
116 return (n < dev->num_mmio);
117}
118
7feb640c 119static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
a1ff8ae0 120 bool may_overlap, int priority)
aae9460e
PB
121{
122 assert(n >= 0 && n < dev->num_mmio);
123
124 if (dev->mmio[n].addr == addr) {
125 /* ??? region already mapped here. */
126 return;
127 }
a8170e5e 128 if (dev->mmio[n].addr != (hwaddr)-1) {
aae9460e 129 /* Unregister previous mapping. */
e114fead 130 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
aae9460e
PB
131 }
132 dev->mmio[n].addr = addr;
7feb640c
AK
133 if (may_overlap) {
134 memory_region_add_subregion_overlap(get_system_memory(),
135 addr,
136 dev->mmio[n].memory,
137 priority);
138 }
139 else {
140 memory_region_add_subregion(get_system_memory(),
141 addr,
142 dev->mmio[n].memory);
143 }
aae9460e
PB
144}
145
7feb640c
AK
146void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
147{
148 sysbus_mmio_map_common(dev, n, addr, false, 0);
149}
150
151void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
a1ff8ae0 152 int priority)
7feb640c
AK
153{
154 sysbus_mmio_map_common(dev, n, addr, true, priority);
155}
aae9460e
PB
156
157/* Request an IRQ source. The actual IRQ object may be populated later. */
158void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
159{
b5917219 160 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
aae9460e
PB
161}
162
163/* Pass IRQs from a target device. */
164void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
165{
b5917219 166 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
aae9460e
PB
167}
168
750ecd44 169void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
ec3bb837
AK
170{
171 int n;
172
173 assert(dev->num_mmio < QDEV_MAX_MMIO);
174 n = dev->num_mmio++;
175 dev->mmio[n].addr = -1;
ec3bb837
AK
176 dev->mmio[n].memory = memory;
177}
178
46c305ef
PM
179MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
180{
181 return dev->mmio[n].memory;
182}
183
c646f74f
GN
184void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
185{
186 pio_addr_t i;
187
188 for (i = 0; i < size; i++) {
189 assert(dev->num_pio < QDEV_MAX_PIO);
190 dev->pio[dev->num_pio++] = ioport++;
191 }
192}
193
d307af79 194static int sysbus_device_init(DeviceState *dev)
aae9460e 195{
999e12bb
AL
196 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
197 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
aae9460e 198
4ce5dae8
PM
199 if (!sbc->init) {
200 return 0;
201 }
999e12bb 202 return sbc->init(sd);
aae9460e
PB
203}
204
aae9460e 205DeviceState *sysbus_create_varargs(const char *name,
a8170e5e 206 hwaddr addr, ...)
aae9460e
PB
207{
208 DeviceState *dev;
209 SysBusDevice *s;
210 va_list va;
211 qemu_irq irq;
212 int n;
213
214 dev = qdev_create(NULL, name);
1356b98d 215 s = SYS_BUS_DEVICE(dev);
e23a1b33 216 qdev_init_nofail(dev);
a8170e5e 217 if (addr != (hwaddr)-1) {
aae9460e
PB
218 sysbus_mmio_map(s, 0, addr);
219 }
220 va_start(va, addr);
221 n = 0;
222 while (1) {
4912371f
BS
223 irq = va_arg(va, qemu_irq);
224 if (!irq) {
225 break;
226 }
227 sysbus_connect_irq(s, n, irq);
228 n++;
229 }
d0bc5bc3 230 va_end(va);
4912371f
BS
231 return dev;
232}
233
234DeviceState *sysbus_try_create_varargs(const char *name,
a8170e5e 235 hwaddr addr, ...)
4912371f
BS
236{
237 DeviceState *dev;
238 SysBusDevice *s;
239 va_list va;
240 qemu_irq irq;
241 int n;
242
243 dev = qdev_try_create(NULL, name);
244 if (!dev) {
245 return NULL;
246 }
1356b98d 247 s = SYS_BUS_DEVICE(dev);
4912371f 248 qdev_init_nofail(dev);
a8170e5e 249 if (addr != (hwaddr)-1) {
4912371f
BS
250 sysbus_mmio_map(s, 0, addr);
251 }
252 va_start(va, addr);
253 n = 0;
254 while (1) {
aae9460e
PB
255 irq = va_arg(va, qemu_irq);
256 if (!irq) {
257 break;
258 }
259 sysbus_connect_irq(s, n, irq);
260 n++;
261 }
d0bc5bc3 262 va_end(va);
aae9460e
PB
263 return dev;
264}
cae4956e 265
10c4c98a 266static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
cae4956e 267{
1356b98d 268 SysBusDevice *s = SYS_BUS_DEVICE(dev);
a8170e5e 269 hwaddr size;
cae4956e
GH
270 int i;
271
272 for (i = 0; i < s->num_mmio; i++) {
e114fead 273 size = memory_region_size(s->mmio[i].memory);
cae4956e 274 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
3f7f1c80 275 indent, "", s->mmio[i].addr, size);
cae4956e
GH
276 }
277}
c646f74f
GN
278
279static char *sysbus_get_fw_dev_path(DeviceState *dev)
280{
1356b98d 281 SysBusDevice *s = SYS_BUS_DEVICE(dev);
c646f74f
GN
282 char path[40];
283 int off;
284
285 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
286
287 if (s->num_mmio) {
288 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
289 s->mmio[0].addr);
290 } else if (s->num_pio) {
291 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
292 }
293
a5cf8262 294 return g_strdup(path);
c646f74f 295}
2b985d9c 296
a8170e5e 297void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
2b985d9c
AK
298 MemoryRegion *mem)
299{
300 memory_region_add_subregion(get_system_io(), addr, mem);
301}
302
62ec4832
AK
303MemoryRegion *sysbus_address_space(SysBusDevice *dev)
304{
305 return get_system_memory();
306}
999e12bb 307
39bffca2
AL
308static void sysbus_device_class_init(ObjectClass *klass, void *data)
309{
310 DeviceClass *k = DEVICE_CLASS(klass);
311 k->init = sysbus_device_init;
0d936928 312 k->bus_type = TYPE_SYSTEM_BUS;
39bffca2
AL
313}
314
8c43a6f0 315static const TypeInfo sysbus_device_type_info = {
999e12bb
AL
316 .name = TYPE_SYS_BUS_DEVICE,
317 .parent = TYPE_DEVICE,
318 .instance_size = sizeof(SysBusDevice),
319 .abstract = true,
320 .class_size = sizeof(SysBusDeviceClass),
39bffca2 321 .class_init = sysbus_device_class_init,
999e12bb
AL
322};
323
8185d216
PB
324/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
325static BusState *main_system_bus;
326
327static void main_system_bus_create(void)
328{
329 /* assign main_system_bus before qbus_create_inplace()
330 * in order to make "if (bus != sysbus_get_default())" work */
0d936928 331 main_system_bus = g_malloc0(system_bus_info.instance_size);
fb17dfe0
AF
332 qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
333 TYPE_SYSTEM_BUS, NULL, "main-system-bus");
64b625f4 334 OBJECT(main_system_bus)->free = g_free;
f968fc68
AL
335 object_property_add_child(container_get(qdev_get_machine(),
336 "/unattached"),
337 "sysbus", OBJECT(main_system_bus), NULL);
8185d216
PB
338}
339
340BusState *sysbus_get_default(void)
341{
342 if (!main_system_bus) {
343 main_system_bus_create();
344 }
345 return main_system_bus;
346}
347
83f7d43a 348static void sysbus_register_types(void)
999e12bb 349{
0d936928 350 type_register_static(&system_bus_info);
999e12bb
AL
351 type_register_static(&sysbus_device_type_info);
352}
353
83f7d43a 354type_init(sysbus_register_types)
This page took 0.761112 seconds and 4 git commands to generate.