]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/bus.c
qom: Put name parameter before value / visitor parameter
[mirror_qemu.git] / hw / core / bus.c
CommitLineData
a62c8911
AF
1/*
2 * Dynamic device configuration and creation -- buses.
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
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "qemu/osdep.h"
a27bd6c7 21#include "hw/qdev-properties.h"
856dfd8a 22#include "qemu/ctype.h"
0b8fa32f 23#include "qemu/module.h"
a62c8911
AF
24#include "qapi/error.h"
25
9bc6bfdf 26void qbus_set_hotplug_handler(BusState *bus, Object *handler)
a62c8911 27{
5325cc34
MA
28 object_property_set_link(OBJECT(bus), QDEV_HOTPLUG_HANDLER_PROPERTY,
29 handler, &error_abort);
a62c8911
AF
30}
31
cd7c8660 32void qbus_set_bus_hotplug_handler(BusState *bus)
a62c8911 33{
9bc6bfdf 34 qbus_set_hotplug_handler(bus, OBJECT(bus));
a62c8911
AF
35}
36
37int qbus_walk_children(BusState *bus,
38 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
39 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
40 void *opaque)
41{
42 BusChild *kid;
43 int err;
44
45 if (pre_busfn) {
46 err = pre_busfn(bus, opaque);
47 if (err) {
48 return err;
49 }
50 }
51
52 QTAILQ_FOREACH(kid, &bus->children, sibling) {
53 err = qdev_walk_children(kid->child,
54 pre_devfn, pre_busfn,
55 post_devfn, post_busfn, opaque);
56 if (err < 0) {
57 return err;
58 }
59 }
60
61 if (post_busfn) {
62 err = post_busfn(bus, opaque);
63 if (err) {
64 return err;
65 }
66 }
67
68 return 0;
69}
70
abb89dbf
DH
71void bus_cold_reset(BusState *bus)
72{
73 resettable_reset(OBJECT(bus), RESET_TYPE_COLD);
74}
75
c11256aa
DH
76bool bus_is_in_reset(BusState *bus)
77{
78 return resettable_is_in_reset(OBJECT(bus));
79}
80
81static ResettableState *bus_get_reset_state(Object *obj)
82{
83 BusState *bus = BUS(obj);
84 return &bus->reset;
85}
86
87static void bus_reset_child_foreach(Object *obj, ResettableChildCallback cb,
88 void *opaque, ResetType type)
89{
90 BusState *bus = BUS(obj);
91 BusChild *kid;
92
93 QTAILQ_FOREACH(kid, &bus->children, sibling) {
94 cb(OBJECT(kid->child), opaque, type);
95 }
96}
97
30884d1b 98static void qbus_init(BusState *bus, DeviceState *parent, const char *name)
a62c8911
AF
99{
100 const char *typename = object_get_typename(OBJECT(bus));
101 BusClass *bc;
f73480c3 102 int i, bus_id;
a62c8911
AF
103
104 bus->parent = parent;
105
106 if (name) {
107 bus->name = g_strdup(name);
108 } else if (bus->parent && bus->parent->id) {
109 /* parent device has id -> use it plus parent-bus-id for bus name */
110 bus_id = bus->parent->num_child_bus;
f73480c3 111 bus->name = g_strdup_printf("%s.%d", bus->parent->id, bus_id);
a62c8911
AF
112 } else {
113 /* no id -> use lowercase bus type plus global bus-id for bus name */
114 bc = BUS_GET_CLASS(bus);
115 bus_id = bc->automatic_ids++;
f73480c3
MAL
116 bus->name = g_strdup_printf("%s.%d", typename, bus_id);
117 for (i = 0; bus->name[i]; i++) {
118 bus->name[i] = qemu_tolower(bus->name[i]);
a62c8911 119 }
a62c8911
AF
120 }
121
122 if (bus->parent) {
123 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
124 bus->parent->num_child_bus++;
d2623129 125 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus));
0d1e8d6f 126 object_unref(OBJECT(bus));
be1ba4d5
PM
127 } else {
128 /* The only bus without a parent is the main system bus */
129 assert(bus == sysbus_get_default());
a62c8911
AF
130 }
131}
132
133static void bus_unparent(Object *obj)
134{
135 BusState *bus = BUS(obj);
136 BusChild *kid;
137
be1ba4d5
PM
138 /* Only the main system bus has no parent, and that bus is never freed */
139 assert(bus->parent);
140
a62c8911
AF
141 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
142 DeviceState *dev = kid->child;
143 object_unparent(OBJECT(dev));
144 }
be1ba4d5
PM
145 QLIST_REMOVE(bus, sibling);
146 bus->parent->num_child_bus--;
147 bus->parent = NULL;
a62c8911
AF
148}
149
150void qbus_create_inplace(void *bus, size_t size, const char *typename,
151 DeviceState *parent, const char *name)
152{
153 object_initialize(bus, size, typename);
30884d1b 154 qbus_init(bus, parent, name);
a62c8911
AF
155}
156
157BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
158{
159 BusState *bus;
160
161 bus = BUS(object_new(typename));
30884d1b 162 qbus_init(bus, parent, name);
a62c8911
AF
163
164 return bus;
165}
166
9940b2cf
MA
167bool qbus_realize(BusState *bus, Error **errp)
168{
169 Error *err = NULL;
170
5325cc34 171 object_property_set_bool(OBJECT(bus), "realized", true, &err);
9940b2cf
MA
172 error_propagate(errp, err);
173 return !err;
174}
175
176void qbus_unrealize(BusState *bus)
177{
5325cc34 178 object_property_set_bool(OBJECT(bus), "realized", false, &error_abort);
9940b2cf
MA
179}
180
a62c8911
AF
181static bool bus_get_realized(Object *obj, Error **errp)
182{
183 BusState *bus = BUS(obj);
184
185 return bus->realized;
186}
187
188static void bus_set_realized(Object *obj, bool value, Error **errp)
189{
190 BusState *bus = BUS(obj);
191 BusClass *bc = BUS_GET_CLASS(bus);
192 BusChild *kid;
a62c8911
AF
193
194 if (value && !bus->realized) {
195 if (bc->realize) {
b69c3c21 196 bc->realize(bus, errp);
a62c8911
AF
197 }
198
199 /* TODO: recursive realization */
200 } else if (!value && bus->realized) {
201 QTAILQ_FOREACH(kid, &bus->children, sibling) {
202 DeviceState *dev = kid->child;
981c3dcd 203 qdev_unrealize(dev);
a62c8911 204 }
b69c3c21
MA
205 if (bc->unrealize) {
206 bc->unrealize(bus);
a62c8911
AF
207 }
208 }
209
a62c8911
AF
210 bus->realized = value;
211}
212
213static void qbus_initfn(Object *obj)
214{
215 BusState *bus = BUS(obj);
216
217 QTAILQ_INIT(&bus->children);
218 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
219 TYPE_HOTPLUG_HANDLER,
220 (Object **)&bus->hotplug_handler,
221 object_property_allow_set_link,
d2623129 222 0);
a62c8911 223 object_property_add_bool(obj, "realized",
d2623129 224 bus_get_realized, bus_set_realized);
a62c8911
AF
225}
226
227static char *default_bus_get_fw_dev_path(DeviceState *dev)
228{
229 return g_strdup(object_get_typename(OBJECT(dev)));
230}
231
c11256aa
DH
232/**
233 * bus_phases_reset:
234 * Transition reset method for buses to allow moving
235 * smoothly from legacy reset method to multi-phases
236 */
237static void bus_phases_reset(BusState *bus)
238{
239 ResettableClass *rc = RESETTABLE_GET_CLASS(bus);
240
241 if (rc->phases.enter) {
242 rc->phases.enter(OBJECT(bus), RESET_TYPE_COLD);
243 }
244 if (rc->phases.hold) {
245 rc->phases.hold(OBJECT(bus));
246 }
247 if (rc->phases.exit) {
248 rc->phases.exit(OBJECT(bus));
249 }
250}
251
252static void bus_transitional_reset(Object *obj)
253{
254 BusClass *bc = BUS_GET_CLASS(obj);
255
256 /*
257 * This will call either @bus_phases_reset (for multi-phases transitioned
258 * buses) or a bus's specific method for not-yet transitioned buses.
259 * In both case, it does not reset children.
260 */
261 if (bc->reset) {
262 bc->reset(BUS(obj));
263 }
264}
265
266/**
267 * bus_get_transitional_reset:
268 * check if the bus's class is ready for multi-phase
269 */
270static ResettableTrFunction bus_get_transitional_reset(Object *obj)
271{
272 BusClass *dc = BUS_GET_CLASS(obj);
273 if (dc->reset != bus_phases_reset) {
274 /*
275 * dc->reset has been overridden by a subclass,
276 * the bus is not ready for multi phase yet.
277 */
278 return bus_transitional_reset;
279 }
280 return NULL;
281}
282
a62c8911
AF
283static void bus_class_init(ObjectClass *class, void *data)
284{
285 BusClass *bc = BUS_CLASS(class);
c11256aa 286 ResettableClass *rc = RESETTABLE_CLASS(class);
a62c8911
AF
287
288 class->unparent = bus_unparent;
289 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
c11256aa
DH
290
291 rc->get_state = bus_get_reset_state;
292 rc->child_foreach = bus_reset_child_foreach;
293
294 /*
295 * @bus_phases_reset is put as the default reset method below, allowing
296 * to do the multi-phase transition from base classes to leaf classes. It
297 * allows a legacy-reset Bus class to extend a multi-phases-reset
298 * Bus class for the following reason:
299 * + If a base class B has been moved to multi-phase, then it does not
300 * override this default reset method and may have defined phase methods.
301 * + A child class C (extending class B) which uses
302 * bus_class_set_parent_reset() (or similar means) to override the
303 * reset method will still work as expected. @bus_phases_reset function
304 * will be registered as the parent reset method and effectively call
305 * parent reset phases.
306 */
307 bc->reset = bus_phases_reset;
308 rc->get_transitional_function = bus_get_transitional_reset;
a62c8911
AF
309}
310
311static void qbus_finalize(Object *obj)
312{
313 BusState *bus = BUS(obj);
314
f73480c3 315 g_free(bus->name);
a62c8911
AF
316}
317
318static const TypeInfo bus_info = {
319 .name = TYPE_BUS,
320 .parent = TYPE_OBJECT,
321 .instance_size = sizeof(BusState),
322 .abstract = true,
323 .class_size = sizeof(BusClass),
324 .instance_init = qbus_initfn,
325 .instance_finalize = qbus_finalize,
326 .class_init = bus_class_init,
c11256aa
DH
327 .interfaces = (InterfaceInfo[]) {
328 { TYPE_RESETTABLE_INTERFACE },
329 { }
330 },
a62c8911
AF
331};
332
333static void bus_register_types(void)
334{
335 type_register_static(&bus_info);
336}
337
338type_init(bus_register_types)