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