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