1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "hw/sysbus.h"
21#include "monitor/monitor.h"
22#include "exec/address-spaces.h"
23
24static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25static char *sysbus_get_fw_dev_path(DeviceState *dev);
26
27typedef struct SysBusFind {
28 void *opaque;
29 FindSysbusDeviceFunc *func;
30} SysBusFind;
31
32
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
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
54
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
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
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,
85};
86
87
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 g_free(prop);
95
96 return (r != NULL);
97}
98
99bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
100{
101 return !!sysbus_get_connected_irq(dev, n);
102}
103
104qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
105{
106 DeviceState *d = DEVICE(dev);
107 return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
108}
109
110void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
111{
112 SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev);
113
114 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
115
116 if (sbd->connect_irq_notifier) {
117 sbd->connect_irq_notifier(dev, irq);
118 }
119}
120
121
122bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
123{
124 return (n < dev->num_mmio);
125}
126
127static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
128 bool may_overlap, int priority)
129{
130 assert(n >= 0 && n < dev->num_mmio);
131
132 if (dev->mmio[n].addr == addr) {
133
134 return;
135 }
136 if (dev->mmio[n].addr != (hwaddr)-1) {
137
138 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
139 }
140 dev->mmio[n].addr = addr;
141 if (may_overlap) {
142 memory_region_add_subregion_overlap(get_system_memory(),
143 addr,
144 dev->mmio[n].memory,
145 priority);
146 }
147 else {
148 memory_region_add_subregion(get_system_memory(),
149 addr,
150 dev->mmio[n].memory);
151 }
152}
153
154void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
155{
156 sysbus_mmio_map_common(dev, n, addr, false, 0);
157}
158
159void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
160 int priority)
161{
162 sysbus_mmio_map_common(dev, n, addr, true, priority);
163}
164
165
166void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
167{
168 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
169}
170
171
172void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
173{
174 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
175}
176
177void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
178{
179 int n;
180
181 assert(dev->num_mmio < QDEV_MAX_MMIO);
182 n = dev->num_mmio++;
183 dev->mmio[n].addr = -1;
184 dev->mmio[n].memory = memory;
185}
186
187MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
188{
189 return dev->mmio[n].memory;
190}
191
192void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
193{
194 pio_addr_t i;
195
196 for (i = 0; i < size; i++) {
197 assert(dev->num_pio < QDEV_MAX_PIO);
198 dev->pio[dev->num_pio++] = ioport++;
199 }
200}
201
202static int sysbus_device_init(DeviceState *dev)
203{
204 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
205 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
206
207 if (!sbc->init) {
208 return 0;
209 }
210 return sbc->init(sd);
211}
212
213DeviceState *sysbus_create_varargs(const char *name,
214 hwaddr addr, ...)
215{
216 DeviceState *dev;
217 SysBusDevice *s;
218 va_list va;
219 qemu_irq irq;
220 int n;
221
222 dev = qdev_create(NULL, name);
223 s = SYS_BUS_DEVICE(dev);
224 qdev_init_nofail(dev);
225 if (addr != (hwaddr)-1) {
226 sysbus_mmio_map(s, 0, addr);
227 }
228 va_start(va, addr);
229 n = 0;
230 while (1) {
231 irq = va_arg(va, qemu_irq);
232 if (!irq) {
233 break;
234 }
235 sysbus_connect_irq(s, n, irq);
236 n++;
237 }
238 va_end(va);
239 return dev;
240}
241
242DeviceState *sysbus_try_create_varargs(const char *name,
243 hwaddr addr, ...)
244{
245 DeviceState *dev;
246 SysBusDevice *s;
247 va_list va;
248 qemu_irq irq;
249 int n;
250
251 dev = qdev_try_create(NULL, name);
252 if (!dev) {
253 return NULL;
254 }
255 s = SYS_BUS_DEVICE(dev);
256 qdev_init_nofail(dev);
257 if (addr != (hwaddr)-1) {
258 sysbus_mmio_map(s, 0, addr);
259 }
260 va_start(va, addr);
261 n = 0;
262 while (1) {
263 irq = va_arg(va, qemu_irq);
264 if (!irq) {
265 break;
266 }
267 sysbus_connect_irq(s, n, irq);
268 n++;
269 }
270 va_end(va);
271 return dev;
272}
273
274static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
275{
276 SysBusDevice *s = SYS_BUS_DEVICE(dev);
277 hwaddr size;
278 int i;
279
280 for (i = 0; i < s->num_mmio; i++) {
281 size = memory_region_size(s->mmio[i].memory);
282 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
283 indent, "", s->mmio[i].addr, size);
284 }
285}
286
287static char *sysbus_get_fw_dev_path(DeviceState *dev)
288{
289 SysBusDevice *s = SYS_BUS_DEVICE(dev);
290 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
291
292 char *addr, *fw_dev_path;
293
294 if (s->num_mmio) {
295 return g_strdup_printf("%s@" TARGET_FMT_plx, qdev_fw_name(dev),
296 s->mmio[0].addr);
297 }
298 if (s->num_pio) {
299 return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
300 }
301 if (sbc->explicit_ofw_unit_address) {
302 addr = sbc->explicit_ofw_unit_address(s);
303 if (addr) {
304 fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
305 g_free(addr);
306 return fw_dev_path;
307 }
308 }
309 return g_strdup(qdev_fw_name(dev));
310}
311
312void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
313 MemoryRegion *mem)
314{
315 memory_region_add_subregion(get_system_io(), addr, mem);
316}
317
318MemoryRegion *sysbus_address_space(SysBusDevice *dev)
319{
320 return get_system_memory();
321}
322
323static void sysbus_device_class_init(ObjectClass *klass, void *data)
324{
325 DeviceClass *k = DEVICE_CLASS(klass);
326 k->init = sysbus_device_init;
327 k->bus_type = TYPE_SYSTEM_BUS;
328}
329
330static const TypeInfo sysbus_device_type_info = {
331 .name = TYPE_SYS_BUS_DEVICE,
332 .parent = TYPE_DEVICE,
333 .instance_size = sizeof(SysBusDevice),
334 .abstract = true,
335 .class_size = sizeof(SysBusDeviceClass),
336 .class_init = sysbus_device_class_init,
337};
338
339
340static BusState *main_system_bus;
341
342static void main_system_bus_create(void)
343{
344
345
346 main_system_bus = g_malloc0(system_bus_info.instance_size);
347 qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
348 TYPE_SYSTEM_BUS, NULL, "main-system-bus");
349 OBJECT(main_system_bus)->free = g_free;
350 object_property_add_child(container_get(qdev_get_machine(),
351 "/unattached"),
352 "sysbus", OBJECT(main_system_bus), NULL);
353}
354
355BusState *sysbus_get_default(void)
356{
357 if (!main_system_bus) {
358 main_system_bus_create();
359 }
360 return main_system_bus;
361}
362
363static void sysbus_register_types(void)
364{
365 type_register_static(&system_bus_info);
366 type_register_static(&sysbus_device_type_info);
367}
368
369type_init(sysbus_register_types)
370