xref: /qemu/hw/core/bus.c (revision 609f45ea)
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     } else if (bus != sysbus_get_default()) {
106         /* TODO: once all bus devices are qdevified,
107            only reset handler for main_system_bus should be registered here. */
108         qemu_register_reset(qbus_reset_all_fn, bus);
109     }
110 }
111 
112 static void bus_unparent(Object *obj)
113 {
114     BusState *bus = BUS(obj);
115     BusChild *kid;
116 
117     while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
118         DeviceState *dev = kid->child;
119         object_unparent(OBJECT(dev));
120     }
121     if (bus->parent) {
122         QLIST_REMOVE(bus, sibling);
123         bus->parent->num_child_bus--;
124         bus->parent = NULL;
125     } else {
126         assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
127         qemu_unregister_reset(qbus_reset_all_fn, bus);
128     }
129 }
130 
131 void qbus_create_inplace(void *bus, size_t size, const char *typename,
132                          DeviceState *parent, const char *name)
133 {
134     object_initialize(bus, size, typename);
135     qbus_realize(bus, parent, name);
136 }
137 
138 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
139 {
140     BusState *bus;
141 
142     bus = BUS(object_new(typename));
143     qbus_realize(bus, parent, name);
144 
145     return bus;
146 }
147 
148 static bool bus_get_realized(Object *obj, Error **errp)
149 {
150     BusState *bus = BUS(obj);
151 
152     return bus->realized;
153 }
154 
155 static void bus_set_realized(Object *obj, bool value, Error **errp)
156 {
157     BusState *bus = BUS(obj);
158     BusClass *bc = BUS_GET_CLASS(bus);
159     BusChild *kid;
160     Error *local_err = NULL;
161 
162     if (value && !bus->realized) {
163         if (bc->realize) {
164             bc->realize(bus, &local_err);
165         }
166 
167         /* TODO: recursive realization */
168     } else if (!value && bus->realized) {
169         QTAILQ_FOREACH(kid, &bus->children, sibling) {
170             DeviceState *dev = kid->child;
171             object_property_set_bool(OBJECT(dev), false, "realized",
172                                      &local_err);
173             if (local_err != NULL) {
174                 break;
175             }
176         }
177         if (bc->unrealize && local_err == NULL) {
178             bc->unrealize(bus, &local_err);
179         }
180     }
181 
182     if (local_err != NULL) {
183         error_propagate(errp, local_err);
184         return;
185     }
186 
187     bus->realized = value;
188 }
189 
190 static void qbus_initfn(Object *obj)
191 {
192     BusState *bus = BUS(obj);
193 
194     QTAILQ_INIT(&bus->children);
195     object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
196                              TYPE_HOTPLUG_HANDLER,
197                              (Object **)&bus->hotplug_handler,
198                              object_property_allow_set_link,
199                              0,
200                              NULL);
201     object_property_add_bool(obj, "realized",
202                              bus_get_realized, bus_set_realized, NULL);
203 }
204 
205 static char *default_bus_get_fw_dev_path(DeviceState *dev)
206 {
207     return g_strdup(object_get_typename(OBJECT(dev)));
208 }
209 
210 static void bus_class_init(ObjectClass *class, void *data)
211 {
212     BusClass *bc = BUS_CLASS(class);
213 
214     class->unparent = bus_unparent;
215     bc->get_fw_dev_path = default_bus_get_fw_dev_path;
216 }
217 
218 static void qbus_finalize(Object *obj)
219 {
220     BusState *bus = BUS(obj);
221 
222     g_free(bus->name);
223 }
224 
225 static const TypeInfo bus_info = {
226     .name = TYPE_BUS,
227     .parent = TYPE_OBJECT,
228     .instance_size = sizeof(BusState),
229     .abstract = true,
230     .class_size = sizeof(BusClass),
231     .instance_init = qbus_initfn,
232     .instance_finalize = qbus_finalize,
233     .class_init = bus_class_init,
234 };
235 
236 static void bus_register_types(void)
237 {
238     type_register_static(&bus_info);
239 }
240 
241 type_init(bus_register_types)
242