xref: /openbmc/qemu/hw/core/bus.c (revision 35658f6e)
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 "qemu-common.h"
22 #include "hw/qdev.h"
23 #include "qapi/error.h"
24 
25 static void qbus_set_hotplug_handler_internal(BusState *bus, Object *handler,
26                                               Error **errp)
27 {
28 
29     object_property_set_link(OBJECT(bus), OBJECT(handler),
30                              QDEV_HOTPLUG_HANDLER_PROPERTY, errp);
31 }
32 
33 void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, Error **errp)
34 {
35     qbus_set_hotplug_handler_internal(bus, OBJECT(handler), errp);
36 }
37 
38 void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp)
39 {
40     qbus_set_hotplug_handler_internal(bus, OBJECT(bus), errp);
41 }
42 
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)
47 {
48     BusChild *kid;
49     int err;
50 
51     if (pre_busfn) {
52         err = pre_busfn(bus, opaque);
53         if (err) {
54             return err;
55         }
56     }
57 
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;
64         }
65     }
66 
67     if (post_busfn) {
68         err = post_busfn(bus, opaque);
69         if (err) {
70             return err;
71         }
72     }
73 
74     return 0;
75 }
76 
77 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
78 {
79     const char *typename = object_get_typename(OBJECT(bus));
80     BusClass *bc;
81     char *buf;
82     int i, len, bus_id;
83 
84     bus->parent = parent;
85 
86     if (name) {
87         bus->name = g_strdup(name);
88     } else if (bus->parent && bus->parent->id) {
89         /* parent device has id -> use it plus parent-bus-id for bus name */
90         bus_id = bus->parent->num_child_bus;
91 
92         len = strlen(bus->parent->id) + 16;
93         buf = g_malloc(len);
94         snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
95         bus->name = buf;
96     } else {
97         /* no id -> use lowercase bus type plus global bus-id for bus name */
98         bc = BUS_GET_CLASS(bus);
99         bus_id = bc->automatic_ids++;
100 
101         len = strlen(typename) + 16;
102         buf = g_malloc(len);
103         len = snprintf(buf, len, "%s.%d", typename, bus_id);
104         for (i = 0; i < len; i++) {
105             buf[i] = qemu_tolower(buf[i]);
106         }
107         bus->name = buf;
108     }
109 
110     if (bus->parent) {
111         QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
112         bus->parent->num_child_bus++;
113         object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
114         object_unref(OBJECT(bus));
115     } else if (bus != sysbus_get_default()) {
116         /* TODO: once all bus devices are qdevified,
117            only reset handler for main_system_bus should be registered here. */
118         qemu_register_reset(qbus_reset_all_fn, bus);
119     }
120 }
121 
122 static void bus_unparent(Object *obj)
123 {
124     BusState *bus = BUS(obj);
125     BusChild *kid;
126 
127     while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
128         DeviceState *dev = kid->child;
129         object_unparent(OBJECT(dev));
130     }
131     if (bus->parent) {
132         QLIST_REMOVE(bus, sibling);
133         bus->parent->num_child_bus--;
134         bus->parent = NULL;
135     } else {
136         assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
137         qemu_unregister_reset(qbus_reset_all_fn, bus);
138     }
139 }
140 
141 void qbus_create_inplace(void *bus, size_t size, const char *typename,
142                          DeviceState *parent, const char *name)
143 {
144     object_initialize(bus, size, typename);
145     qbus_realize(bus, parent, name);
146 }
147 
148 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
149 {
150     BusState *bus;
151 
152     bus = BUS(object_new(typename));
153     qbus_realize(bus, parent, name);
154 
155     return bus;
156 }
157 
158 static bool bus_get_realized(Object *obj, Error **errp)
159 {
160     BusState *bus = BUS(obj);
161 
162     return bus->realized;
163 }
164 
165 static void bus_set_realized(Object *obj, bool value, Error **errp)
166 {
167     BusState *bus = BUS(obj);
168     BusClass *bc = BUS_GET_CLASS(bus);
169     BusChild *kid;
170     Error *local_err = NULL;
171 
172     if (value && !bus->realized) {
173         if (bc->realize) {
174             bc->realize(bus, &local_err);
175         }
176 
177         /* TODO: recursive realization */
178     } else if (!value && bus->realized) {
179         QTAILQ_FOREACH(kid, &bus->children, sibling) {
180             DeviceState *dev = kid->child;
181             object_property_set_bool(OBJECT(dev), false, "realized",
182                                      &local_err);
183             if (local_err != NULL) {
184                 break;
185             }
186         }
187         if (bc->unrealize && local_err == NULL) {
188             bc->unrealize(bus, &local_err);
189         }
190     }
191 
192     if (local_err != NULL) {
193         error_propagate(errp, local_err);
194         return;
195     }
196 
197     bus->realized = value;
198 }
199 
200 static void qbus_initfn(Object *obj)
201 {
202     BusState *bus = BUS(obj);
203 
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,
209                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
210                              NULL);
211     object_property_add_bool(obj, "realized",
212                              bus_get_realized, bus_set_realized, NULL);
213 }
214 
215 static char *default_bus_get_fw_dev_path(DeviceState *dev)
216 {
217     return g_strdup(object_get_typename(OBJECT(dev)));
218 }
219 
220 static void bus_class_init(ObjectClass *class, void *data)
221 {
222     BusClass *bc = BUS_CLASS(class);
223 
224     class->unparent = bus_unparent;
225     bc->get_fw_dev_path = default_bus_get_fw_dev_path;
226 }
227 
228 static void qbus_finalize(Object *obj)
229 {
230     BusState *bus = BUS(obj);
231 
232     g_free((char *)bus->name);
233 }
234 
235 static const TypeInfo bus_info = {
236     .name = TYPE_BUS,
237     .parent = TYPE_OBJECT,
238     .instance_size = sizeof(BusState),
239     .abstract = true,
240     .class_size = sizeof(BusClass),
241     .instance_init = qbus_initfn,
242     .instance_finalize = qbus_finalize,
243     .class_init = bus_class_init,
244 };
245 
246 static void bus_register_types(void)
247 {
248     type_register_static(&bus_info);
249 }
250 
251 type_init(bus_register_types)
252