]>
Commit | Line | Data |
---|---|---|
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 | ||
18c86e2b | 20 | #include "qemu/osdep.h" |
dbfe0013 | 21 | #include "qapi/error.h" |
0b8fa32f | 22 | #include "qemu/module.h" |
83c9f4ca | 23 | #include "hw/sysbus.h" |
83c9089e | 24 | #include "monitor/monitor.h" |
022c62cb | 25 | #include "exec/address-spaces.h" |
aae9460e | 26 | |
10c4c98a | 27 | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
c646f74f | 28 | static char *sysbus_get_fw_dev_path(DeviceState *dev); |
10c4c98a | 29 | |
eb572280 AG |
30 | typedef struct SysBusFind { |
31 | void *opaque; | |
32 | FindSysbusDeviceFunc *func; | |
33 | } SysBusFind; | |
34 | ||
35 | /* Run func() for every sysbus device, traverse the tree for everything else */ | |
36 | static int find_sysbus_device(Object *obj, void *opaque) | |
37 | { | |
38 | SysBusFind *find = opaque; | |
39 | Object *dev; | |
40 | SysBusDevice *sbdev; | |
41 | ||
42 | dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE); | |
43 | sbdev = (SysBusDevice *)dev; | |
44 | ||
45 | if (!sbdev) { | |
46 | /* Container, traverse it for children */ | |
47 | return object_child_foreach(obj, find_sysbus_device, opaque); | |
48 | } | |
49 | ||
50 | find->func(sbdev, find->opaque); | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | /* | |
56 | * Loop through all dynamically created sysbus devices and call | |
57 | * func() for each instance. | |
58 | */ | |
59 | void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque) | |
60 | { | |
61 | Object *container; | |
62 | SysBusFind find = { | |
63 | .func = func, | |
64 | .opaque = opaque, | |
65 | }; | |
66 | ||
67 | /* Loop through all sysbus devices that were spawened 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); | |
72 | } | |
73 | ||
74 | ||
0d936928 AL |
75 | static void system_bus_class_init(ObjectClass *klass, void *data) |
76 | { | |
77 | BusClass *k = BUS_CLASS(klass); | |
78 | ||
79 | k->print_dev = sysbus_dev_print; | |
80 | k->get_fw_dev_path = sysbus_get_fw_dev_path; | |
81 | } | |
82 | ||
83 | static const TypeInfo system_bus_info = { | |
84 | .name = TYPE_SYSTEM_BUS, | |
85 | .parent = TYPE_BUS, | |
86 | .instance_size = sizeof(BusState), | |
87 | .class_init = system_bus_class_init, | |
10c4c98a GH |
88 | }; |
89 | ||
b7973186 AG |
90 | /* Check whether an IRQ source exists */ |
91 | bool sysbus_has_irq(SysBusDevice *dev, int n) | |
92 | { | |
93 | char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n); | |
94 | ObjectProperty *r; | |
95 | ||
96 | r = object_property_find(OBJECT(dev), prop, NULL); | |
84b5d556 GA |
97 | g_free(prop); |
98 | ||
b7973186 AG |
99 | return (r != NULL); |
100 | } | |
101 | ||
102 | bool sysbus_is_irq_connected(SysBusDevice *dev, int n) | |
103 | { | |
104 | return !!sysbus_get_connected_irq(dev, n); | |
105 | } | |
106 | ||
107 | qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n) | |
108 | { | |
109 | DeviceState *d = DEVICE(dev); | |
110 | return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n); | |
111 | } | |
112 | ||
aae9460e PB |
113 | void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq) |
114 | { | |
715ca691 EA |
115 | SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev); |
116 | ||
b5917219 | 117 | qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq); |
715ca691 EA |
118 | |
119 | if (sbd->connect_irq_notifier) { | |
120 | sbd->connect_irq_notifier(dev, irq); | |
121 | } | |
aae9460e PB |
122 | } |
123 | ||
471a9bc1 AG |
124 | /* Check whether an MMIO region exists */ |
125 | bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n) | |
126 | { | |
127 | return (n < dev->num_mmio); | |
128 | } | |
129 | ||
7feb640c | 130 | static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr, |
a1ff8ae0 | 131 | bool may_overlap, int priority) |
aae9460e PB |
132 | { |
133 | assert(n >= 0 && n < dev->num_mmio); | |
134 | ||
135 | if (dev->mmio[n].addr == addr) { | |
136 | /* ??? region already mapped here. */ | |
137 | return; | |
138 | } | |
a8170e5e | 139 | if (dev->mmio[n].addr != (hwaddr)-1) { |
aae9460e | 140 | /* Unregister previous mapping. */ |
e114fead | 141 | memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory); |
aae9460e PB |
142 | } |
143 | dev->mmio[n].addr = addr; | |
7feb640c AK |
144 | if (may_overlap) { |
145 | memory_region_add_subregion_overlap(get_system_memory(), | |
146 | addr, | |
147 | dev->mmio[n].memory, | |
148 | priority); | |
149 | } | |
150 | else { | |
151 | memory_region_add_subregion(get_system_memory(), | |
152 | addr, | |
153 | dev->mmio[n].memory); | |
154 | } | |
aae9460e PB |
155 | } |
156 | ||
90c20e1e CLG |
157 | void sysbus_mmio_unmap(SysBusDevice *dev, int n) |
158 | { | |
159 | assert(n >= 0 && n < dev->num_mmio); | |
160 | ||
161 | if (dev->mmio[n].addr != (hwaddr)-1) { | |
162 | memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory); | |
163 | dev->mmio[n].addr = (hwaddr)-1; | |
164 | } | |
165 | } | |
166 | ||
7feb640c AK |
167 | void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr) |
168 | { | |
169 | sysbus_mmio_map_common(dev, n, addr, false, 0); | |
170 | } | |
171 | ||
172 | void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr, | |
a1ff8ae0 | 173 | int priority) |
7feb640c AK |
174 | { |
175 | sysbus_mmio_map_common(dev, n, addr, true, priority); | |
176 | } | |
aae9460e PB |
177 | |
178 | /* Request an IRQ source. The actual IRQ object may be populated later. */ | |
179 | void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p) | |
180 | { | |
b5917219 | 181 | qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1); |
aae9460e PB |
182 | } |
183 | ||
184 | /* Pass IRQs from a target device. */ | |
185 | void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target) | |
186 | { | |
b5917219 | 187 | qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ); |
aae9460e PB |
188 | } |
189 | ||
750ecd44 | 190 | void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory) |
ec3bb837 AK |
191 | { |
192 | int n; | |
193 | ||
194 | assert(dev->num_mmio < QDEV_MAX_MMIO); | |
195 | n = dev->num_mmio++; | |
196 | dev->mmio[n].addr = -1; | |
ec3bb837 AK |
197 | dev->mmio[n].memory = memory; |
198 | } | |
199 | ||
46c305ef PM |
200 | MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n) |
201 | { | |
202 | return dev->mmio[n].memory; | |
203 | } | |
204 | ||
89a80e74 | 205 | void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size) |
c646f74f | 206 | { |
89a80e74 | 207 | uint32_t i; |
c646f74f GN |
208 | |
209 | for (i = 0; i < size; i++) { | |
210 | assert(dev->num_pio < QDEV_MAX_PIO); | |
211 | dev->pio[dev->num_pio++] = ioport++; | |
212 | } | |
213 | } | |
214 | ||
817a17fc MZ |
215 | /* The purpose of preserving this empty realize function |
216 | * is to prevent the parent_realize field of some subclasses | |
217 | * from being set to NULL to break the normal init/realize | |
218 | * of some devices. | |
219 | */ | |
dbfe0013 | 220 | static void sysbus_realize(DeviceState *dev, Error **errp) |
aae9460e | 221 | { |
aae9460e PB |
222 | } |
223 | ||
aae9460e | 224 | DeviceState *sysbus_create_varargs(const char *name, |
a8170e5e | 225 | hwaddr addr, ...) |
aae9460e PB |
226 | { |
227 | DeviceState *dev; | |
228 | SysBusDevice *s; | |
229 | va_list va; | |
230 | qemu_irq irq; | |
231 | int n; | |
232 | ||
233 | dev = qdev_create(NULL, name); | |
1356b98d | 234 | s = SYS_BUS_DEVICE(dev); |
e23a1b33 | 235 | qdev_init_nofail(dev); |
a8170e5e | 236 | if (addr != (hwaddr)-1) { |
aae9460e PB |
237 | sysbus_mmio_map(s, 0, addr); |
238 | } | |
239 | va_start(va, addr); | |
240 | n = 0; | |
241 | while (1) { | |
4912371f BS |
242 | irq = va_arg(va, qemu_irq); |
243 | if (!irq) { | |
244 | break; | |
245 | } | |
246 | sysbus_connect_irq(s, n, irq); | |
247 | n++; | |
248 | } | |
d0bc5bc3 | 249 | va_end(va); |
4912371f BS |
250 | return dev; |
251 | } | |
252 | ||
253 | DeviceState *sysbus_try_create_varargs(const char *name, | |
a8170e5e | 254 | hwaddr addr, ...) |
4912371f BS |
255 | { |
256 | DeviceState *dev; | |
257 | SysBusDevice *s; | |
258 | va_list va; | |
259 | qemu_irq irq; | |
260 | int n; | |
261 | ||
262 | dev = qdev_try_create(NULL, name); | |
263 | if (!dev) { | |
264 | return NULL; | |
265 | } | |
1356b98d | 266 | s = SYS_BUS_DEVICE(dev); |
4912371f | 267 | qdev_init_nofail(dev); |
a8170e5e | 268 | if (addr != (hwaddr)-1) { |
4912371f BS |
269 | sysbus_mmio_map(s, 0, addr); |
270 | } | |
271 | va_start(va, addr); | |
272 | n = 0; | |
273 | while (1) { | |
aae9460e PB |
274 | irq = va_arg(va, qemu_irq); |
275 | if (!irq) { | |
276 | break; | |
277 | } | |
278 | sysbus_connect_irq(s, n, irq); | |
279 | n++; | |
280 | } | |
d0bc5bc3 | 281 | va_end(va); |
aae9460e PB |
282 | return dev; |
283 | } | |
cae4956e | 284 | |
10c4c98a | 285 | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
cae4956e | 286 | { |
1356b98d | 287 | SysBusDevice *s = SYS_BUS_DEVICE(dev); |
a8170e5e | 288 | hwaddr size; |
cae4956e GH |
289 | int i; |
290 | ||
291 | for (i = 0; i < s->num_mmio; i++) { | |
e114fead | 292 | size = memory_region_size(s->mmio[i].memory); |
cae4956e | 293 | monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n", |
3f7f1c80 | 294 | indent, "", s->mmio[i].addr, size); |
cae4956e GH |
295 | } |
296 | } | |
c646f74f GN |
297 | |
298 | static char *sysbus_get_fw_dev_path(DeviceState *dev) | |
299 | { | |
1356b98d | 300 | SysBusDevice *s = SYS_BUS_DEVICE(dev); |
0b336b3b | 301 | SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s); |
0b336b3b | 302 | char *addr, *fw_dev_path; |
c646f74f | 303 | |
0b336b3b LE |
304 | if (sbc->explicit_ofw_unit_address) { |
305 | addr = sbc->explicit_ofw_unit_address(s); | |
306 | if (addr) { | |
307 | fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr); | |
308 | g_free(addr); | |
309 | return fw_dev_path; | |
310 | } | |
311 | } | |
be64d777 MCA |
312 | if (s->num_mmio) { |
313 | return g_strdup_printf("%s@" TARGET_FMT_plx, qdev_fw_name(dev), | |
314 | s->mmio[0].addr); | |
315 | } | |
316 | if (s->num_pio) { | |
317 | return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]); | |
318 | } | |
5ba03e2d | 319 | return g_strdup(qdev_fw_name(dev)); |
c646f74f | 320 | } |
2b985d9c | 321 | |
a8170e5e | 322 | void sysbus_add_io(SysBusDevice *dev, hwaddr addr, |
2b985d9c AK |
323 | MemoryRegion *mem) |
324 | { | |
325 | memory_region_add_subregion(get_system_io(), addr, mem); | |
326 | } | |
327 | ||
62ec4832 AK |
328 | MemoryRegion *sysbus_address_space(SysBusDevice *dev) |
329 | { | |
330 | return get_system_memory(); | |
331 | } | |
999e12bb | 332 | |
39bffca2 AL |
333 | static void sysbus_device_class_init(ObjectClass *klass, void *data) |
334 | { | |
335 | DeviceClass *k = DEVICE_CLASS(klass); | |
dbfe0013 | 336 | k->realize = sysbus_realize; |
0d936928 | 337 | k->bus_type = TYPE_SYSTEM_BUS; |
e4f4fb1e EH |
338 | /* |
339 | * device_add plugs devices into a suitable bus. For "real" buses, | |
340 | * that actually connects the device. For sysbus, the connections | |
341 | * need to be made separately, and device_add can't do that. The | |
342 | * device would be left unconnected, and will probably not work | |
343 | * | |
344 | * However, a few machines can handle device_add/-device with | |
345 | * a few specific sysbus devices. In those cases, the device | |
346 | * subclass needs to override it and set user_creatable=true. | |
347 | */ | |
348 | k->user_creatable = false; | |
39bffca2 AL |
349 | } |
350 | ||
8c43a6f0 | 351 | static const TypeInfo sysbus_device_type_info = { |
999e12bb AL |
352 | .name = TYPE_SYS_BUS_DEVICE, |
353 | .parent = TYPE_DEVICE, | |
354 | .instance_size = sizeof(SysBusDevice), | |
355 | .abstract = true, | |
356 | .class_size = sizeof(SysBusDeviceClass), | |
39bffca2 | 357 | .class_init = sysbus_device_class_init, |
999e12bb AL |
358 | }; |
359 | ||
8185d216 PB |
360 | /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ |
361 | static BusState *main_system_bus; | |
362 | ||
363 | static void main_system_bus_create(void) | |
364 | { | |
365 | /* assign main_system_bus before qbus_create_inplace() | |
366 | * in order to make "if (bus != sysbus_get_default())" work */ | |
0d936928 | 367 | main_system_bus = g_malloc0(system_bus_info.instance_size); |
fb17dfe0 AF |
368 | qbus_create_inplace(main_system_bus, system_bus_info.instance_size, |
369 | TYPE_SYSTEM_BUS, NULL, "main-system-bus"); | |
64b625f4 | 370 | OBJECT(main_system_bus)->free = g_free; |
8185d216 PB |
371 | } |
372 | ||
373 | BusState *sysbus_get_default(void) | |
374 | { | |
375 | if (!main_system_bus) { | |
376 | main_system_bus_create(); | |
377 | } | |
378 | return main_system_bus; | |
379 | } | |
380 | ||
046f370f TH |
381 | void sysbus_init_child_obj(Object *parent, const char *childname, void *child, |
382 | size_t childsize, const char *childtype) | |
383 | { | |
384 | object_initialize_child(parent, childname, child, childsize, childtype, | |
385 | &error_abort, NULL); | |
386 | qdev_set_parent_bus(DEVICE(child), sysbus_get_default()); | |
387 | } | |
388 | ||
83f7d43a | 389 | static void sysbus_register_types(void) |
999e12bb | 390 | { |
0d936928 | 391 | type_register_static(&system_bus_info); |
999e12bb AL |
392 | type_register_static(&sysbus_device_type_info); |
393 | } | |
394 | ||
83f7d43a | 395 | type_init(sysbus_register_types) |