kvm: Simplify invtsc check
[qemu/ar7.git] / hw / core / bus.c
blobcf383fc1afb60065bdbd109bf377bbf2d9bddcf7
1 /*
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 "qemu-common.h"
22 #include "hw/qdev.h"
23 #include "qapi/error.h"
25 static void qbus_set_hotplug_handler_internal(BusState *bus, Object *handler,
26 Error **errp)
29 object_property_set_link(OBJECT(bus), OBJECT(handler),
30 QDEV_HOTPLUG_HANDLER_PROPERTY, errp);
33 void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, Error **errp)
35 qbus_set_hotplug_handler_internal(bus, OBJECT(handler), errp);
38 void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp)
40 qbus_set_hotplug_handler_internal(bus, OBJECT(bus), errp);
43 int qbus_walk_children(BusState *bus,
44 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
45 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
46 void *opaque)
48 BusChild *kid;
49 int err;
51 if (pre_busfn) {
52 err = pre_busfn(bus, opaque);
53 if (err) {
54 return err;
58 QTAILQ_FOREACH(kid, &bus->children, sibling) {
59 err = qdev_walk_children(kid->child,
60 pre_devfn, pre_busfn,
61 post_devfn, post_busfn, opaque);
62 if (err < 0) {
63 return err;
67 if (post_busfn) {
68 err = post_busfn(bus, opaque);
69 if (err) {
70 return err;
74 return 0;
77 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
79 const char *typename = object_get_typename(OBJECT(bus));
80 BusClass *bc;
81 int i, bus_id;
83 bus->parent = parent;
85 if (name) {
86 bus->name = g_strdup(name);
87 } else if (bus->parent && bus->parent->id) {
88 /* parent device has id -> use it plus parent-bus-id for bus name */
89 bus_id = bus->parent->num_child_bus;
90 bus->name = g_strdup_printf("%s.%d", bus->parent->id, bus_id);
91 } else {
92 /* no id -> use lowercase bus type plus global bus-id for bus name */
93 bc = BUS_GET_CLASS(bus);
94 bus_id = bc->automatic_ids++;
95 bus->name = g_strdup_printf("%s.%d", typename, bus_id);
96 for (i = 0; bus->name[i]; i++) {
97 bus->name[i] = qemu_tolower(bus->name[i]);
101 if (bus->parent) {
102 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
103 bus->parent->num_child_bus++;
104 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
105 object_unref(OBJECT(bus));
106 } else if (bus != sysbus_get_default()) {
107 /* TODO: once all bus devices are qdevified,
108 only reset handler for main_system_bus should be registered here. */
109 qemu_register_reset(qbus_reset_all_fn, bus);
113 static void bus_unparent(Object *obj)
115 BusState *bus = BUS(obj);
116 BusChild *kid;
118 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
119 DeviceState *dev = kid->child;
120 object_unparent(OBJECT(dev));
122 if (bus->parent) {
123 QLIST_REMOVE(bus, sibling);
124 bus->parent->num_child_bus--;
125 bus->parent = NULL;
126 } else {
127 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
128 qemu_unregister_reset(qbus_reset_all_fn, bus);
132 void qbus_create_inplace(void *bus, size_t size, const char *typename,
133 DeviceState *parent, const char *name)
135 object_initialize(bus, size, typename);
136 qbus_realize(bus, parent, name);
139 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
141 BusState *bus;
143 bus = BUS(object_new(typename));
144 qbus_realize(bus, parent, name);
146 return bus;
149 static bool bus_get_realized(Object *obj, Error **errp)
151 BusState *bus = BUS(obj);
153 return bus->realized;
156 static void bus_set_realized(Object *obj, bool value, Error **errp)
158 BusState *bus = BUS(obj);
159 BusClass *bc = BUS_GET_CLASS(bus);
160 BusChild *kid;
161 Error *local_err = NULL;
163 if (value && !bus->realized) {
164 if (bc->realize) {
165 bc->realize(bus, &local_err);
168 /* TODO: recursive realization */
169 } else if (!value && bus->realized) {
170 QTAILQ_FOREACH(kid, &bus->children, sibling) {
171 DeviceState *dev = kid->child;
172 object_property_set_bool(OBJECT(dev), false, "realized",
173 &local_err);
174 if (local_err != NULL) {
175 break;
178 if (bc->unrealize && local_err == NULL) {
179 bc->unrealize(bus, &local_err);
183 if (local_err != NULL) {
184 error_propagate(errp, local_err);
185 return;
188 bus->realized = value;
191 static void qbus_initfn(Object *obj)
193 BusState *bus = BUS(obj);
195 QTAILQ_INIT(&bus->children);
196 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
197 TYPE_HOTPLUG_HANDLER,
198 (Object **)&bus->hotplug_handler,
199 object_property_allow_set_link,
200 OBJ_PROP_LINK_UNREF_ON_RELEASE,
201 NULL);
202 object_property_add_bool(obj, "realized",
203 bus_get_realized, bus_set_realized, NULL);
206 static char *default_bus_get_fw_dev_path(DeviceState *dev)
208 return g_strdup(object_get_typename(OBJECT(dev)));
211 static void bus_class_init(ObjectClass *class, void *data)
213 BusClass *bc = BUS_CLASS(class);
215 class->unparent = bus_unparent;
216 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
219 static void qbus_finalize(Object *obj)
221 BusState *bus = BUS(obj);
223 g_free(bus->name);
226 static const TypeInfo bus_info = {
227 .name = TYPE_BUS,
228 .parent = TYPE_OBJECT,
229 .instance_size = sizeof(BusState),
230 .abstract = true,
231 .class_size = sizeof(BusClass),
232 .instance_init = qbus_initfn,
233 .instance_finalize = qbus_finalize,
234 .class_init = bus_class_init,
237 static void bus_register_types(void)
239 type_register_static(&bus_info);
242 type_init(bus_register_types)