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