xref: /openbmc/qemu/hw/core/bus.c (revision 4a09d0bb)
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     int i, bus_id;
82 
83     bus->parent = parent;
84 
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]);
98         }
99     }
100 
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);
110     }
111 }
112 
113 static void bus_unparent(Object *obj)
114 {
115     BusState *bus = BUS(obj);
116     BusChild *kid;
117 
118     while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
119         DeviceState *dev = kid->child;
120         object_unparent(OBJECT(dev));
121     }
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);
129     }
130 }
131 
132 void qbus_create_inplace(void *bus, size_t size, const char *typename,
133                          DeviceState *parent, const char *name)
134 {
135     object_initialize(bus, size, typename);
136     qbus_realize(bus, parent, name);
137 }
138 
139 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
140 {
141     BusState *bus;
142 
143     bus = BUS(object_new(typename));
144     qbus_realize(bus, parent, name);
145 
146     return bus;
147 }
148 
149 static bool bus_get_realized(Object *obj, Error **errp)
150 {
151     BusState *bus = BUS(obj);
152 
153     return bus->realized;
154 }
155 
156 static void bus_set_realized(Object *obj, bool value, Error **errp)
157 {
158     BusState *bus = BUS(obj);
159     BusClass *bc = BUS_GET_CLASS(bus);
160     BusChild *kid;
161     Error *local_err = NULL;
162 
163     if (value && !bus->realized) {
164         if (bc->realize) {
165             bc->realize(bus, &local_err);
166         }
167 
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;
176             }
177         }
178         if (bc->unrealize && local_err == NULL) {
179             bc->unrealize(bus, &local_err);
180         }
181     }
182 
183     if (local_err != NULL) {
184         error_propagate(errp, local_err);
185         return;
186     }
187 
188     bus->realized = value;
189 }
190 
191 static void qbus_initfn(Object *obj)
192 {
193     BusState *bus = BUS(obj);
194 
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);
204 }
205 
206 static char *default_bus_get_fw_dev_path(DeviceState *dev)
207 {
208     return g_strdup(object_get_typename(OBJECT(dev)));
209 }
210 
211 static void bus_class_init(ObjectClass *class, void *data)
212 {
213     BusClass *bc = BUS_CLASS(class);
214 
215     class->unparent = bus_unparent;
216     bc->get_fw_dev_path = default_bus_get_fw_dev_path;
217 }
218 
219 static void qbus_finalize(Object *obj)
220 {
221     BusState *bus = BUS(obj);
222 
223     g_free(bus->name);
224 }
225 
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,
235 };
236 
237 static void bus_register_types(void)
238 {
239     type_register_static(&bus_info);
240 }
241 
242 type_init(bus_register_types)
243