2 * Dynamic device configuration and creation -- buses.
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/>.
20 #include "qemu/osdep.h"
21 #include "hw/qdev-properties.h"
22 #include "qemu/ctype.h"
23 #include "qemu/module.h"
24 #include "qapi/error.h"
26 void qbus_set_hotplug_handler(BusState
*bus
, Object
*handler
, Error
**errp
)
28 object_property_set_link(OBJECT(bus
), OBJECT(handler
),
29 QDEV_HOTPLUG_HANDLER_PROPERTY
, errp
);
32 void qbus_set_bus_hotplug_handler(BusState
*bus
, Error
**errp
)
34 qbus_set_hotplug_handler(bus
, OBJECT(bus
), errp
);
37 int qbus_walk_children(BusState
*bus
,
38 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
39 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
46 err
= pre_busfn(bus
, opaque
);
52 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
53 err
= qdev_walk_children(kid
->child
,
55 post_devfn
, post_busfn
, opaque
);
62 err
= post_busfn(bus
, opaque
);
71 void bus_cold_reset(BusState
*bus
)
73 resettable_reset(OBJECT(bus
), RESET_TYPE_COLD
);
76 bool bus_is_in_reset(BusState
*bus
)
78 return resettable_is_in_reset(OBJECT(bus
));
81 static ResettableState
*bus_get_reset_state(Object
*obj
)
83 BusState
*bus
= BUS(obj
);
87 static void bus_reset_child_foreach(Object
*obj
, ResettableChildCallback cb
,
88 void *opaque
, ResetType type
)
90 BusState
*bus
= BUS(obj
);
93 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
94 cb(OBJECT(kid
->child
), opaque
, type
);
98 static void qbus_realize(BusState
*bus
, DeviceState
*parent
, const char *name
)
100 const char *typename
= object_get_typename(OBJECT(bus
));
104 bus
->parent
= parent
;
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
;
111 bus
->name
= g_strdup_printf("%s.%d", bus
->parent
->id
, bus_id
);
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
++;
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
]);
123 QLIST_INSERT_HEAD(&bus
->parent
->child_bus
, bus
, sibling
);
124 bus
->parent
->num_child_bus
++;
125 object_property_add_child(OBJECT(bus
->parent
), bus
->name
, OBJECT(bus
));
126 object_unref(OBJECT(bus
));
128 /* The only bus without a parent is the main system bus */
129 assert(bus
== sysbus_get_default());
133 static void bus_unparent(Object
*obj
)
135 BusState
*bus
= BUS(obj
);
138 /* Only the main system bus has no parent, and that bus is never freed */
141 while ((kid
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
142 DeviceState
*dev
= kid
->child
;
143 object_unparent(OBJECT(dev
));
145 QLIST_REMOVE(bus
, sibling
);
146 bus
->parent
->num_child_bus
--;
150 void qbus_create_inplace(void *bus
, size_t size
, const char *typename
,
151 DeviceState
*parent
, const char *name
)
153 object_initialize(bus
, size
, typename
);
154 qbus_realize(bus
, parent
, name
);
157 BusState
*qbus_create(const char *typename
, DeviceState
*parent
, const char *name
)
161 bus
= BUS(object_new(typename
));
162 qbus_realize(bus
, parent
, name
);
167 static bool bus_get_realized(Object
*obj
, Error
**errp
)
169 BusState
*bus
= BUS(obj
);
171 return bus
->realized
;
174 static void bus_set_realized(Object
*obj
, bool value
, Error
**errp
)
176 BusState
*bus
= BUS(obj
);
177 BusClass
*bc
= BUS_GET_CLASS(bus
);
180 if (value
&& !bus
->realized
) {
182 bc
->realize(bus
, errp
);
185 /* TODO: recursive realization */
186 } else if (!value
&& bus
->realized
) {
187 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
188 DeviceState
*dev
= kid
->child
;
189 object_property_set_bool(OBJECT(dev
), false, "realized",
197 bus
->realized
= value
;
200 static void qbus_initfn(Object
*obj
)
202 BusState
*bus
= BUS(obj
);
204 QTAILQ_INIT(&bus
->children
);
205 object_property_add_link(obj
, QDEV_HOTPLUG_HANDLER_PROPERTY
,
206 TYPE_HOTPLUG_HANDLER
,
207 (Object
**)&bus
->hotplug_handler
,
208 object_property_allow_set_link
,
210 object_property_add_bool(obj
, "realized",
211 bus_get_realized
, bus_set_realized
);
214 static char *default_bus_get_fw_dev_path(DeviceState
*dev
)
216 return g_strdup(object_get_typename(OBJECT(dev
)));
221 * Transition reset method for buses to allow moving
222 * smoothly from legacy reset method to multi-phases
224 static void bus_phases_reset(BusState
*bus
)
226 ResettableClass
*rc
= RESETTABLE_GET_CLASS(bus
);
228 if (rc
->phases
.enter
) {
229 rc
->phases
.enter(OBJECT(bus
), RESET_TYPE_COLD
);
231 if (rc
->phases
.hold
) {
232 rc
->phases
.hold(OBJECT(bus
));
234 if (rc
->phases
.exit
) {
235 rc
->phases
.exit(OBJECT(bus
));
239 static void bus_transitional_reset(Object
*obj
)
241 BusClass
*bc
= BUS_GET_CLASS(obj
);
244 * This will call either @bus_phases_reset (for multi-phases transitioned
245 * buses) or a bus's specific method for not-yet transitioned buses.
246 * In both case, it does not reset children.
254 * bus_get_transitional_reset:
255 * check if the bus's class is ready for multi-phase
257 static ResettableTrFunction
bus_get_transitional_reset(Object
*obj
)
259 BusClass
*dc
= BUS_GET_CLASS(obj
);
260 if (dc
->reset
!= bus_phases_reset
) {
262 * dc->reset has been overridden by a subclass,
263 * the bus is not ready for multi phase yet.
265 return bus_transitional_reset
;
270 static void bus_class_init(ObjectClass
*class, void *data
)
272 BusClass
*bc
= BUS_CLASS(class);
273 ResettableClass
*rc
= RESETTABLE_CLASS(class);
275 class->unparent
= bus_unparent
;
276 bc
->get_fw_dev_path
= default_bus_get_fw_dev_path
;
278 rc
->get_state
= bus_get_reset_state
;
279 rc
->child_foreach
= bus_reset_child_foreach
;
282 * @bus_phases_reset is put as the default reset method below, allowing
283 * to do the multi-phase transition from base classes to leaf classes. It
284 * allows a legacy-reset Bus class to extend a multi-phases-reset
285 * Bus class for the following reason:
286 * + If a base class B has been moved to multi-phase, then it does not
287 * override this default reset method and may have defined phase methods.
288 * + A child class C (extending class B) which uses
289 * bus_class_set_parent_reset() (or similar means) to override the
290 * reset method will still work as expected. @bus_phases_reset function
291 * will be registered as the parent reset method and effectively call
292 * parent reset phases.
294 bc
->reset
= bus_phases_reset
;
295 rc
->get_transitional_function
= bus_get_transitional_reset
;
298 static void qbus_finalize(Object
*obj
)
300 BusState
*bus
= BUS(obj
);
305 static const TypeInfo bus_info
= {
307 .parent
= TYPE_OBJECT
,
308 .instance_size
= sizeof(BusState
),
310 .class_size
= sizeof(BusClass
),
311 .instance_init
= qbus_initfn
,
312 .instance_finalize
= qbus_finalize
,
313 .class_init
= bus_class_init
,
314 .interfaces
= (InterfaceInfo
[]) {
315 { TYPE_RESETTABLE_INTERFACE
},
320 static void bus_register_types(void)
322 type_register_static(&bus_info
);
325 type_init(bus_register_types
)