]>
Commit | Line | Data |
---|---|---|
aae9460e PB |
1 | /* |
2 | * Dynamic device configuration and creation. | |
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
aae9460e PB |
18 | */ |
19 | ||
20 | /* The theory here is that it should be possible to create a machine without | |
21 | knowledge of specific devices. Historically board init routines have | |
22 | passed a bunch of arguments to each device, requiring the board know | |
23 | exactly which device it is dealing with. This file provides an abstract | |
24 | API for device configuration and initialization. Devices will generally | |
25 | inherit from a particular bus (e.g. PCI or I2C) rather than | |
26 | this API directly. */ | |
27 | ||
83c9f4ca | 28 | #include "hw/qdev.h" |
9c17d615 | 29 | #include "sysemu/sysemu.h" |
7b1b5d19 | 30 | #include "qapi/error.h" |
b4a42f81 | 31 | #include "qapi/qmp/qerror.h" |
7b1b5d19 | 32 | #include "qapi/visitor.h" |
0402a5d6 MT |
33 | #include "qapi/qmp/qjson.h" |
34 | #include "monitor/monitor.h" | |
0ee4de6c | 35 | #include "hw/hotplug.h" |
aae9460e | 36 | |
ee46d8a5 | 37 | int qdev_hotplug = 0; |
0ac8ef71 AW |
38 | static bool qdev_hot_added = false; |
39 | static bool qdev_hot_removed = false; | |
3418bd25 | 40 | |
4be9f0d1 AL |
41 | const VMStateDescription *qdev_get_vmsd(DeviceState *dev) |
42 | { | |
6e008585 AL |
43 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
44 | return dc->vmsd; | |
4be9f0d1 AL |
45 | } |
46 | ||
4be9f0d1 AL |
47 | const char *qdev_fw_name(DeviceState *dev) |
48 | { | |
6e008585 | 49 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
4be9f0d1 | 50 | |
6e008585 AL |
51 | if (dc->fw_name) { |
52 | return dc->fw_name; | |
4be9f0d1 AL |
53 | } |
54 | ||
55 | return object_get_typename(OBJECT(dev)); | |
56 | } | |
57 | ||
ca2cc788 PB |
58 | static void qdev_property_add_legacy(DeviceState *dev, Property *prop, |
59 | Error **errp); | |
60 | ||
0866aca1 | 61 | static void bus_remove_child(BusState *bus, DeviceState *child) |
0c17542d | 62 | { |
0866aca1 AL |
63 | BusChild *kid; |
64 | ||
65 | QTAILQ_FOREACH(kid, &bus->children, sibling) { | |
66 | if (kid->child == child) { | |
67 | char name[32]; | |
68 | ||
69 | snprintf(name, sizeof(name), "child[%d]", kid->index); | |
70 | QTAILQ_REMOVE(&bus->children, kid, sibling); | |
9d127820 PB |
71 | |
72 | /* This gives back ownership of kid->child back to us. */ | |
0866aca1 | 73 | object_property_del(OBJECT(bus), name, NULL); |
9d127820 | 74 | object_unref(OBJECT(kid->child)); |
0866aca1 AL |
75 | g_free(kid); |
76 | return; | |
77 | } | |
78 | } | |
79 | } | |
80 | ||
81 | static void bus_add_child(BusState *bus, DeviceState *child) | |
82 | { | |
83 | char name[32]; | |
84 | BusChild *kid = g_malloc0(sizeof(*kid)); | |
0c17542d | 85 | |
0c17542d MA |
86 | if (qdev_hotplug) { |
87 | assert(bus->allow_hotplug); | |
0c17542d | 88 | } |
a5296ca9 | 89 | |
0866aca1 AL |
90 | kid->index = bus->max_index++; |
91 | kid->child = child; | |
9d127820 | 92 | object_ref(OBJECT(kid->child)); |
a5296ca9 | 93 | |
0866aca1 AL |
94 | QTAILQ_INSERT_HEAD(&bus->children, kid, sibling); |
95 | ||
9d127820 | 96 | /* This transfers ownership of kid->child to the property. */ |
0866aca1 AL |
97 | snprintf(name, sizeof(name), "child[%d]", kid->index); |
98 | object_property_add_link(OBJECT(bus), name, | |
99 | object_get_typename(OBJECT(child)), | |
100 | (Object **)&kid->child, | |
101 | NULL); | |
102 | } | |
103 | ||
104 | void qdev_set_parent_bus(DeviceState *dev, BusState *bus) | |
105 | { | |
9fbe6127 | 106 | dev->parent_bus = bus; |
62d7ba66 | 107 | object_ref(OBJECT(bus)); |
0866aca1 | 108 | bus_add_child(bus, dev); |
0c17542d MA |
109 | } |
110 | ||
aae9460e PB |
111 | /* Create a new device. This only initializes the device state structure |
112 | and allows properties to be set. qdev_init should be called to | |
113 | initialize the actual device emulation. */ | |
02e2da45 | 114 | DeviceState *qdev_create(BusState *bus, const char *name) |
0bcdeda7 BS |
115 | { |
116 | DeviceState *dev; | |
117 | ||
118 | dev = qdev_try_create(bus, name); | |
119 | if (!dev) { | |
e92714c7 | 120 | if (bus) { |
312fd5f2 | 121 | error_report("Unknown device '%s' for bus '%s'", name, |
23e3fbec | 122 | object_get_typename(OBJECT(bus))); |
e92714c7 | 123 | } else { |
312fd5f2 | 124 | error_report("Unknown device '%s' for default sysbus", name); |
e92714c7 | 125 | } |
01ed1d52 | 126 | abort(); |
0bcdeda7 BS |
127 | } |
128 | ||
129 | return dev; | |
130 | } | |
131 | ||
da57febf | 132 | DeviceState *qdev_try_create(BusState *bus, const char *type) |
aae9460e | 133 | { |
9fbe6127 AL |
134 | DeviceState *dev; |
135 | ||
da57febf | 136 | if (object_class_by_name(type) == NULL) { |
4ed658ca AF |
137 | return NULL; |
138 | } | |
da57febf | 139 | dev = DEVICE(object_new(type)); |
9fbe6127 AL |
140 | if (!dev) { |
141 | return NULL; | |
142 | } | |
143 | ||
10c4c98a | 144 | if (!bus) { |
68694897 | 145 | bus = sysbus_get_default(); |
10c4c98a GH |
146 | } |
147 | ||
9fbe6127 | 148 | qdev_set_parent_bus(dev, bus); |
b09995ae | 149 | object_unref(OBJECT(dev)); |
9fbe6127 | 150 | return dev; |
aae9460e PB |
151 | } |
152 | ||
153 | /* Initialize a device. Device properties should be set before calling | |
154 | this function. IRQs and MMIO regions should be connected/mapped after | |
18cfeb52 MA |
155 | calling this function. |
156 | On failure, destroy the device and return negative value. | |
157 | Return 0 on success. */ | |
81a322d4 | 158 | int qdev_init(DeviceState *dev) |
aae9460e | 159 | { |
249d4172 | 160 | Error *local_err = NULL; |
959f733a | 161 | |
7983c8a3 | 162 | assert(!dev->realized); |
6e008585 | 163 | |
249d4172 AF |
164 | object_property_set_bool(OBJECT(dev), true, "realized", &local_err); |
165 | if (local_err != NULL) { | |
cffc5113 | 166 | qerror_report_err(local_err); |
249d4172 | 167 | error_free(local_err); |
02a5c4c9 | 168 | object_unparent(OBJECT(dev)); |
249d4172 | 169 | return -1; |
18cfeb52 | 170 | } |
249d4172 AF |
171 | return 0; |
172 | } | |
da57febf | 173 | |
249d4172 AF |
174 | static void device_realize(DeviceState *dev, Error **err) |
175 | { | |
176 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
da57febf | 177 | |
249d4172 AF |
178 | if (dc->init) { |
179 | int rc = dc->init(dev); | |
180 | if (rc < 0) { | |
181 | error_setg(err, "Device initialization failed."); | |
182 | return; | |
183 | } | |
5ab28c83 | 184 | } |
02e2da45 PB |
185 | } |
186 | ||
fe6c2117 AF |
187 | static void device_unrealize(DeviceState *dev, Error **errp) |
188 | { | |
189 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
190 | ||
191 | if (dc->exit) { | |
192 | int rc = dc->exit(dev); | |
193 | if (rc < 0) { | |
194 | error_setg(errp, "Device exit failed."); | |
195 | return; | |
196 | } | |
197 | } | |
198 | } | |
199 | ||
4d2ffa08 JK |
200 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
201 | int required_for_version) | |
202 | { | |
7983c8a3 | 203 | assert(!dev->realized); |
4d2ffa08 JK |
204 | dev->instance_id_alias = alias_id; |
205 | dev->alias_required_for_version = required_for_version; | |
206 | } | |
207 | ||
56f9107e | 208 | void qdev_unplug(DeviceState *dev, Error **errp) |
3418bd25 | 209 | { |
6e008585 AL |
210 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
211 | ||
120dc38f | 212 | if (dev->parent_bus && !dev->parent_bus->allow_hotplug) { |
56f9107e LC |
213 | error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); |
214 | return; | |
3418bd25 | 215 | } |
593831de | 216 | |
1a37eca1 IM |
217 | if (!dc->hotpluggable) { |
218 | error_set(errp, QERR_DEVICE_NO_HOTPLUG, | |
219 | object_get_typename(OBJECT(dev))); | |
220 | return; | |
221 | } | |
222 | ||
0ac8ef71 AW |
223 | qdev_hot_removed = true; |
224 | ||
5e954943 IM |
225 | if (dev->parent_bus && dev->parent_bus->hotplug_handler) { |
226 | hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp); | |
227 | } else { | |
228 | assert(dc->unplug != NULL); | |
229 | if (dc->unplug(dev) < 0) { /* legacy handler */ | |
230 | error_set(errp, QERR_UNDEFINED_ERROR); | |
231 | } | |
56f9107e | 232 | } |
3418bd25 GH |
233 | } |
234 | ||
ec990eb6 AL |
235 | static int qdev_reset_one(DeviceState *dev, void *opaque) |
236 | { | |
94afdadc | 237 | device_reset(dev); |
ec990eb6 AL |
238 | |
239 | return 0; | |
240 | } | |
241 | ||
b4694b7c IY |
242 | static int qbus_reset_one(BusState *bus, void *opaque) |
243 | { | |
0d936928 AL |
244 | BusClass *bc = BUS_GET_CLASS(bus); |
245 | if (bc->reset) { | |
dcc20931 | 246 | bc->reset(bus); |
b4694b7c IY |
247 | } |
248 | return 0; | |
249 | } | |
250 | ||
5af0a04b IY |
251 | void qdev_reset_all(DeviceState *dev) |
252 | { | |
dcc20931 | 253 | qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL); |
5af0a04b IY |
254 | } |
255 | ||
d0508c36 PB |
256 | void qbus_reset_all(BusState *bus) |
257 | { | |
dcc20931 | 258 | qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL); |
d0508c36 PB |
259 | } |
260 | ||
80376c3f IY |
261 | void qbus_reset_all_fn(void *opaque) |
262 | { | |
263 | BusState *bus = opaque; | |
d0508c36 | 264 | qbus_reset_all(bus); |
80376c3f IY |
265 | } |
266 | ||
3418bd25 GH |
267 | /* can be used as ->unplug() callback for the simple cases */ |
268 | int qdev_simple_unplug_cb(DeviceState *dev) | |
269 | { | |
270 | /* just zap it */ | |
02a5c4c9 | 271 | object_unparent(OBJECT(dev)); |
3418bd25 GH |
272 | return 0; |
273 | } | |
274 | ||
3b29a101 MT |
275 | |
276 | /* Like qdev_init(), but terminate program via error_report() instead of | |
e23a1b33 MA |
277 | returning an error value. This is okay during machine creation. |
278 | Don't use for hotplug, because there callers need to recover from | |
279 | failure. Exception: if you know the device's init() callback can't | |
280 | fail, then qdev_init_nofail() can't fail either, and is therefore | |
281 | usable even then. But relying on the device implementation that | |
282 | way is somewhat unclean, and best avoided. */ | |
283 | void qdev_init_nofail(DeviceState *dev) | |
284 | { | |
7de3abe5 AL |
285 | const char *typename = object_get_typename(OBJECT(dev)); |
286 | ||
bd6c9a61 | 287 | if (qdev_init(dev) < 0) { |
7de3abe5 | 288 | error_report("Initialization of device %s failed", typename); |
bd6c9a61 MA |
289 | exit(1); |
290 | } | |
e23a1b33 MA |
291 | } |
292 | ||
3418bd25 GH |
293 | void qdev_machine_creation_done(void) |
294 | { | |
295 | /* | |
296 | * ok, initial machine setup is done, starting from now we can | |
297 | * only create hotpluggable devices | |
298 | */ | |
299 | qdev_hotplug = 1; | |
300 | } | |
301 | ||
0ac8ef71 AW |
302 | bool qdev_machine_modified(void) |
303 | { | |
304 | return qdev_hot_added || qdev_hot_removed; | |
305 | } | |
306 | ||
02e2da45 | 307 | BusState *qdev_get_parent_bus(DeviceState *dev) |
aae9460e | 308 | { |
02e2da45 | 309 | return dev->parent_bus; |
aae9460e PB |
310 | } |
311 | ||
aae9460e PB |
312 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) |
313 | { | |
1e5b31e6 PC |
314 | dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler, |
315 | dev, n); | |
316 | dev->num_gpio_in += n; | |
aae9460e PB |
317 | } |
318 | ||
319 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) | |
320 | { | |
321 | assert(dev->num_gpio_out == 0); | |
322 | dev->num_gpio_out = n; | |
323 | dev->gpio_out = pins; | |
324 | } | |
325 | ||
326 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) | |
327 | { | |
328 | assert(n >= 0 && n < dev->num_gpio_in); | |
329 | return dev->gpio_in[n]; | |
330 | } | |
331 | ||
332 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) | |
333 | { | |
334 | assert(n >= 0 && n < dev->num_gpio_out); | |
335 | dev->gpio_out[n] = pin; | |
336 | } | |
337 | ||
02e2da45 | 338 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
4d6ae674 | 339 | { |
02e2da45 | 340 | BusState *bus; |
4d6ae674 | 341 | |
72cf2d4f | 342 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
4d6ae674 | 343 | if (strcmp(name, bus->name) == 0) { |
02e2da45 | 344 | return bus; |
4d6ae674 PB |
345 | } |
346 | } | |
347 | return NULL; | |
348 | } | |
349 | ||
0293214b PB |
350 | int qbus_walk_children(BusState *bus, |
351 | qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, | |
352 | qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, | |
353 | void *opaque) | |
81699d8a | 354 | { |
0866aca1 | 355 | BusChild *kid; |
81699d8a AL |
356 | int err; |
357 | ||
0293214b PB |
358 | if (pre_busfn) { |
359 | err = pre_busfn(bus, opaque); | |
81699d8a AL |
360 | if (err) { |
361 | return err; | |
362 | } | |
363 | } | |
364 | ||
0866aca1 | 365 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
0293214b PB |
366 | err = qdev_walk_children(kid->child, |
367 | pre_devfn, pre_busfn, | |
368 | post_devfn, post_busfn, opaque); | |
81699d8a AL |
369 | if (err < 0) { |
370 | return err; | |
371 | } | |
372 | } | |
373 | ||
0293214b PB |
374 | if (post_busfn) { |
375 | err = post_busfn(bus, opaque); | |
376 | if (err) { | |
377 | return err; | |
378 | } | |
379 | } | |
380 | ||
81699d8a AL |
381 | return 0; |
382 | } | |
383 | ||
0293214b PB |
384 | int qdev_walk_children(DeviceState *dev, |
385 | qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, | |
386 | qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, | |
387 | void *opaque) | |
81699d8a AL |
388 | { |
389 | BusState *bus; | |
390 | int err; | |
391 | ||
0293214b PB |
392 | if (pre_devfn) { |
393 | err = pre_devfn(dev, opaque); | |
81699d8a AL |
394 | if (err) { |
395 | return err; | |
396 | } | |
397 | } | |
398 | ||
399 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { | |
0293214b PB |
400 | err = qbus_walk_children(bus, pre_devfn, pre_busfn, |
401 | post_devfn, post_busfn, opaque); | |
81699d8a AL |
402 | if (err < 0) { |
403 | return err; | |
404 | } | |
405 | } | |
406 | ||
0293214b PB |
407 | if (post_devfn) { |
408 | err = post_devfn(dev, opaque); | |
409 | if (err) { | |
410 | return err; | |
411 | } | |
412 | } | |
413 | ||
81699d8a AL |
414 | return 0; |
415 | } | |
416 | ||
a2ee6b4f | 417 | DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
3418bd25 | 418 | { |
0866aca1 AL |
419 | BusChild *kid; |
420 | DeviceState *ret; | |
3418bd25 GH |
421 | BusState *child; |
422 | ||
0866aca1 AL |
423 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
424 | DeviceState *dev = kid->child; | |
425 | ||
426 | if (dev->id && strcmp(dev->id, id) == 0) { | |
3418bd25 | 427 | return dev; |
0866aca1 AL |
428 | } |
429 | ||
3418bd25 GH |
430 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
431 | ret = qdev_find_recursive(child, id); | |
432 | if (ret) { | |
433 | return ret; | |
434 | } | |
435 | } | |
436 | } | |
437 | return NULL; | |
438 | } | |
439 | ||
013e1182 | 440 | static void qbus_realize(BusState *bus, DeviceState *parent, const char *name) |
02e2da45 | 441 | { |
ac7d1ba6 | 442 | const char *typename = object_get_typename(OBJECT(bus)); |
61de3676 | 443 | BusClass *bc; |
d271de9f | 444 | char *buf; |
61de3676 | 445 | int i, len, bus_id; |
02e2da45 | 446 | |
013e1182 PB |
447 | bus->parent = parent; |
448 | ||
449 | if (name) { | |
450 | bus->name = g_strdup(name); | |
ac7d1ba6 | 451 | } else if (bus->parent && bus->parent->id) { |
61de3676 AG |
452 | /* parent device has id -> use it plus parent-bus-id for bus name */ |
453 | bus_id = bus->parent->num_child_bus; | |
454 | ||
ac7d1ba6 | 455 | len = strlen(bus->parent->id) + 16; |
7267c094 | 456 | buf = g_malloc(len); |
61de3676 | 457 | snprintf(buf, len, "%s.%d", bus->parent->id, bus_id); |
d271de9f GH |
458 | bus->name = buf; |
459 | } else { | |
61de3676 AG |
460 | /* no id -> use lowercase bus type plus global bus-id for bus name */ |
461 | bc = BUS_GET_CLASS(bus); | |
462 | bus_id = bc->automatic_ids++; | |
463 | ||
0d936928 | 464 | len = strlen(typename) + 16; |
7267c094 | 465 | buf = g_malloc(len); |
61de3676 AG |
466 | len = snprintf(buf, len, "%s.%d", typename, bus_id); |
467 | for (i = 0; i < len; i++) { | |
bb87ece5 | 468 | buf[i] = qemu_tolower(buf[i]); |
61de3676 | 469 | } |
d271de9f GH |
470 | bus->name = buf; |
471 | } | |
472 | ||
ac7d1ba6 AL |
473 | if (bus->parent) { |
474 | QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling); | |
475 | bus->parent->num_child_bus++; | |
476 | object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL); | |
b09995ae | 477 | object_unref(OBJECT(bus)); |
8185d216 | 478 | } else if (bus != sysbus_get_default()) { |
80376c3f IY |
479 | /* TODO: once all bus devices are qdevified, |
480 | only reset handler for main_system_bus should be registered here. */ | |
481 | qemu_register_reset(qbus_reset_all_fn, bus); | |
02e2da45 | 482 | } |
cd739fb6 GH |
483 | } |
484 | ||
6853d27a PB |
485 | static void bus_unparent(Object *obj) |
486 | { | |
487 | BusState *bus = BUS(obj); | |
488 | BusChild *kid; | |
489 | ||
490 | while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) { | |
491 | DeviceState *dev = kid->child; | |
02a5c4c9 | 492 | object_unparent(OBJECT(dev)); |
6853d27a PB |
493 | } |
494 | if (bus->parent) { | |
495 | QLIST_REMOVE(bus, sibling); | |
496 | bus->parent->num_child_bus--; | |
497 | bus->parent = NULL; | |
498 | } else { | |
499 | assert(bus != sysbus_get_default()); /* main_system_bus is never freed */ | |
500 | qemu_unregister_reset(qbus_reset_all_fn, bus); | |
501 | } | |
502 | } | |
503 | ||
02e7f85d BD |
504 | static bool bus_get_realized(Object *obj, Error **err) |
505 | { | |
506 | BusState *bus = BUS(obj); | |
507 | ||
508 | return bus->realized; | |
509 | } | |
510 | ||
511 | static void bus_set_realized(Object *obj, bool value, Error **err) | |
512 | { | |
513 | BusState *bus = BUS(obj); | |
514 | BusClass *bc = BUS_GET_CLASS(bus); | |
515 | Error *local_err = NULL; | |
516 | ||
517 | if (value && !bus->realized) { | |
518 | if (bc->realize) { | |
519 | bc->realize(bus, &local_err); | |
520 | ||
521 | if (local_err != NULL) { | |
522 | goto error; | |
523 | } | |
524 | ||
525 | } | |
526 | } else if (!value && bus->realized) { | |
527 | if (bc->unrealize) { | |
528 | bc->unrealize(bus, &local_err); | |
529 | ||
530 | if (local_err != NULL) { | |
531 | goto error; | |
532 | } | |
533 | } | |
534 | } | |
535 | ||
536 | bus->realized = value; | |
537 | return; | |
538 | ||
539 | error: | |
540 | error_propagate(err, local_err); | |
541 | } | |
542 | ||
fb17dfe0 | 543 | void qbus_create_inplace(void *bus, size_t size, const char *typename, |
0d936928 | 544 | DeviceState *parent, const char *name) |
cd739fb6 | 545 | { |
213f0c4f | 546 | object_initialize(bus, size, typename); |
013e1182 | 547 | qbus_realize(bus, parent, name); |
02e2da45 | 548 | } |
cae4956e | 549 | |
0d936928 | 550 | BusState *qbus_create(const char *typename, DeviceState *parent, const char *name) |
2da8bb92 | 551 | { |
cd739fb6 GH |
552 | BusState *bus; |
553 | ||
0d936928 | 554 | bus = BUS(object_new(typename)); |
013e1182 | 555 | qbus_realize(bus, parent, name); |
ac7d1ba6 | 556 | |
02e2da45 | 557 | return bus; |
2da8bb92 IY |
558 | } |
559 | ||
0d936928 AL |
560 | static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev) |
561 | { | |
562 | BusClass *bc = BUS_GET_CLASS(bus); | |
563 | ||
564 | if (bc->get_fw_dev_path) { | |
565 | return bc->get_fw_dev_path(dev); | |
131ec1bd | 566 | } |
0d936928 AL |
567 | |
568 | return NULL; | |
131ec1bd GH |
569 | } |
570 | ||
1ca4d09a GN |
571 | static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size) |
572 | { | |
573 | int l = 0; | |
574 | ||
575 | if (dev && dev->parent_bus) { | |
576 | char *d; | |
577 | l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size); | |
0d936928 AL |
578 | d = bus_get_fw_dev_path(dev->parent_bus, dev); |
579 | if (d) { | |
1ca4d09a | 580 | l += snprintf(p + l, size - l, "%s", d); |
7267c094 | 581 | g_free(d); |
1ca4d09a | 582 | } else { |
bbfa18fc | 583 | return l; |
1ca4d09a GN |
584 | } |
585 | } | |
586 | l += snprintf(p + l , size - l, "/"); | |
587 | ||
588 | return l; | |
589 | } | |
590 | ||
591 | char* qdev_get_fw_dev_path(DeviceState *dev) | |
592 | { | |
593 | char path[128]; | |
594 | int l; | |
595 | ||
596 | l = qdev_get_fw_dev_path_helper(dev, path, 128); | |
597 | ||
598 | path[l-1] = '\0'; | |
599 | ||
a5cf8262 | 600 | return g_strdup(path); |
1ca4d09a | 601 | } |
85ed303b | 602 | |
09e5ab63 | 603 | char *qdev_get_dev_path(DeviceState *dev) |
85ed303b | 604 | { |
0d936928 | 605 | BusClass *bc; |
09e5ab63 AL |
606 | |
607 | if (!dev || !dev->parent_bus) { | |
608 | return NULL; | |
609 | } | |
610 | ||
0d936928 AL |
611 | bc = BUS_GET_CLASS(dev->parent_bus); |
612 | if (bc->get_dev_path) { | |
613 | return bc->get_dev_path(dev); | |
09e5ab63 AL |
614 | } |
615 | ||
616 | return NULL; | |
44677ded | 617 | } |
a5296ca9 AL |
618 | |
619 | /** | |
620 | * Legacy property handling | |
621 | */ | |
622 | ||
57c9fafe | 623 | static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque, |
a5296ca9 AL |
624 | const char *name, Error **errp) |
625 | { | |
57c9fafe | 626 | DeviceState *dev = DEVICE(obj); |
a5296ca9 AL |
627 | Property *prop = opaque; |
628 | ||
e3cb6ba6 PB |
629 | char buffer[1024]; |
630 | char *ptr = buffer; | |
a5296ca9 | 631 | |
e3cb6ba6 PB |
632 | prop->info->print(dev, prop, buffer, sizeof(buffer)); |
633 | visit_type_str(v, &ptr, name, errp); | |
a5296ca9 AL |
634 | } |
635 | ||
a5296ca9 AL |
636 | /** |
637 | * @qdev_add_legacy_property - adds a legacy property | |
638 | * | |
639 | * Do not use this is new code! Properties added through this interface will | |
ca2cc788 | 640 | * be given names and types in the "legacy" namespace. |
a5296ca9 | 641 | * |
68ee3569 PB |
642 | * Legacy properties are string versions of other OOM properties. The format |
643 | * of the string depends on the property type. | |
a5296ca9 AL |
644 | */ |
645 | void qdev_property_add_legacy(DeviceState *dev, Property *prop, | |
646 | Error **errp) | |
647 | { | |
7ce7ffe0 | 648 | gchar *name; |
a5296ca9 | 649 | |
f3be016d | 650 | /* Register pointer properties as legacy properties */ |
03ff7770 | 651 | if (!prop->info->print && prop->info->get) { |
68ee3569 PB |
652 | return; |
653 | } | |
f3be016d | 654 | |
ca2cc788 | 655 | name = g_strdup_printf("legacy-%s", prop->name); |
7ce7ffe0 | 656 | object_property_add(OBJECT(dev), name, "str", |
68ee3569 | 657 | prop->info->print ? qdev_get_legacy_property : prop->info->get, |
03ff7770 | 658 | NULL, |
57c9fafe AL |
659 | NULL, |
660 | prop, errp); | |
a5296ca9 | 661 | |
ca2cc788 PB |
662 | g_free(name); |
663 | } | |
664 | ||
665 | /** | |
666 | * @qdev_property_add_static - add a @Property to a device. | |
667 | * | |
668 | * Static properties access data in a struct. The actual type of the | |
669 | * property and the field depends on the property type. | |
670 | */ | |
671 | void qdev_property_add_static(DeviceState *dev, Property *prop, | |
672 | Error **errp) | |
673 | { | |
fdae245f PB |
674 | Error *local_err = NULL; |
675 | Object *obj = OBJECT(dev); | |
676 | ||
d822979b PB |
677 | /* |
678 | * TODO qdev_prop_ptr does not have getters or setters. It must | |
679 | * go now that it can be replaced with links. The test should be | |
680 | * removed along with it: all static properties are read/write. | |
681 | */ | |
682 | if (!prop->info->get && !prop->info->set) { | |
683 | return; | |
684 | } | |
685 | ||
fdae245f | 686 | object_property_add(obj, prop->name, prop->info->name, |
57c9fafe | 687 | prop->info->get, prop->info->set, |
dd0ba250 | 688 | prop->info->release, |
fdae245f PB |
689 | prop, &local_err); |
690 | ||
691 | if (local_err) { | |
692 | error_propagate(errp, local_err); | |
693 | return; | |
694 | } | |
695 | if (prop->qtype == QTYPE_NONE) { | |
696 | return; | |
697 | } | |
698 | ||
699 | if (prop->qtype == QTYPE_QBOOL) { | |
5433a0a8 | 700 | object_property_set_bool(obj, prop->defval, prop->name, &error_abort); |
fdae245f PB |
701 | } else if (prop->info->enum_table) { |
702 | object_property_set_str(obj, prop->info->enum_table[prop->defval], | |
5433a0a8 | 703 | prop->name, &error_abort); |
fdae245f | 704 | } else if (prop->qtype == QTYPE_QINT) { |
5433a0a8 | 705 | object_property_set_int(obj, prop->defval, prop->name, &error_abort); |
fdae245f | 706 | } |
6a146eba | 707 | } |
1de81d28 | 708 | |
249d4172 AF |
709 | static bool device_get_realized(Object *obj, Error **err) |
710 | { | |
711 | DeviceState *dev = DEVICE(obj); | |
712 | return dev->realized; | |
713 | } | |
714 | ||
715 | static void device_set_realized(Object *obj, bool value, Error **err) | |
716 | { | |
717 | DeviceState *dev = DEVICE(obj); | |
718 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
5c21ce77 | 719 | BusState *bus; |
249d4172 AF |
720 | Error *local_err = NULL; |
721 | ||
1a37eca1 IM |
722 | if (dev->hotplugged && !dc->hotpluggable) { |
723 | error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj)); | |
724 | return; | |
725 | } | |
726 | ||
249d4172 | 727 | if (value && !dev->realized) { |
249d4172 AF |
728 | if (!obj->parent && local_err == NULL) { |
729 | static int unattached_count; | |
730 | gchar *name = g_strdup_printf("device[%d]", unattached_count++); | |
731 | ||
732 | object_property_add_child(container_get(qdev_get_machine(), | |
733 | "/unattached"), | |
734 | name, obj, &local_err); | |
735 | g_free(name); | |
736 | } | |
737 | ||
a7ddba52 IM |
738 | if (dc->realize) { |
739 | dc->realize(dev, &local_err); | |
740 | } | |
741 | ||
5e954943 IM |
742 | if (dev->parent_bus && dev->parent_bus->hotplug_handler && |
743 | local_err == NULL) { | |
744 | hotplug_handler_plug(dev->parent_bus->hotplug_handler, | |
745 | dev, &local_err); | |
746 | } | |
747 | ||
249d4172 AF |
748 | if (qdev_get_vmsd(dev) && local_err == NULL) { |
749 | vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, | |
750 | dev->instance_id_alias, | |
751 | dev->alias_required_for_version); | |
752 | } | |
5c21ce77 BD |
753 | if (local_err == NULL) { |
754 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { | |
755 | object_property_set_bool(OBJECT(bus), true, "realized", | |
756 | &local_err); | |
757 | if (local_err != NULL) { | |
758 | break; | |
759 | } | |
760 | } | |
761 | } | |
249d4172 AF |
762 | if (dev->hotplugged && local_err == NULL) { |
763 | device_reset(dev); | |
764 | } | |
765 | } else if (!value && dev->realized) { | |
5c21ce77 BD |
766 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
767 | object_property_set_bool(OBJECT(bus), false, "realized", | |
768 | &local_err); | |
769 | if (local_err != NULL) { | |
770 | break; | |
771 | } | |
772 | } | |
773 | if (qdev_get_vmsd(dev) && local_err == NULL) { | |
fe6c2117 AF |
774 | vmstate_unregister(dev, qdev_get_vmsd(dev), dev); |
775 | } | |
5c21ce77 | 776 | if (dc->unrealize && local_err == NULL) { |
249d4172 AF |
777 | dc->unrealize(dev, &local_err); |
778 | } | |
779 | } | |
780 | ||
781 | if (local_err != NULL) { | |
782 | error_propagate(err, local_err); | |
783 | return; | |
784 | } | |
785 | ||
786 | dev->realized = value; | |
787 | } | |
788 | ||
1a37eca1 IM |
789 | static bool device_get_hotpluggable(Object *obj, Error **err) |
790 | { | |
791 | DeviceClass *dc = DEVICE_GET_CLASS(obj); | |
792 | DeviceState *dev = DEVICE(obj); | |
793 | ||
2b81b35f AF |
794 | return dc->hotpluggable && (dev->parent_bus == NULL || |
795 | dev->parent_bus->allow_hotplug); | |
1a37eca1 IM |
796 | } |
797 | ||
9674bfe4 AL |
798 | static void device_initfn(Object *obj) |
799 | { | |
800 | DeviceState *dev = DEVICE(obj); | |
bce54474 | 801 | ObjectClass *class; |
9674bfe4 AL |
802 | Property *prop; |
803 | ||
804 | if (qdev_hotplug) { | |
805 | dev->hotplugged = 1; | |
806 | qdev_hot_added = true; | |
807 | } | |
808 | ||
809 | dev->instance_id_alias = -1; | |
7983c8a3 | 810 | dev->realized = false; |
9674bfe4 | 811 | |
249d4172 AF |
812 | object_property_add_bool(obj, "realized", |
813 | device_get_realized, device_set_realized, NULL); | |
1a37eca1 IM |
814 | object_property_add_bool(obj, "hotpluggable", |
815 | device_get_hotpluggable, NULL, NULL); | |
249d4172 | 816 | |
bce54474 PB |
817 | class = object_get_class(OBJECT(dev)); |
818 | do { | |
819 | for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) { | |
5433a0a8 PC |
820 | qdev_property_add_legacy(dev, prop, &error_abort); |
821 | qdev_property_add_static(dev, prop, &error_abort); | |
bce54474 | 822 | } |
bce54474 PB |
823 | class = object_class_get_parent(class); |
824 | } while (class != object_class_by_name(TYPE_DEVICE)); | |
9674bfe4 | 825 | |
f968fc68 | 826 | object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS, |
5433a0a8 | 827 | (Object **)&dev->parent_bus, &error_abort); |
9674bfe4 AL |
828 | } |
829 | ||
99a0b036 EH |
830 | static void device_post_init(Object *obj) |
831 | { | |
5433a0a8 | 832 | qdev_prop_set_globals(DEVICE(obj), &error_abort); |
99a0b036 EH |
833 | } |
834 | ||
60adba37 AL |
835 | /* Unlink device from bus and free the structure. */ |
836 | static void device_finalize(Object *obj) | |
837 | { | |
838 | DeviceState *dev = DEVICE(obj); | |
06f7f2bb PB |
839 | if (dev->opts) { |
840 | qemu_opts_del(dev->opts); | |
60adba37 | 841 | } |
60adba37 AL |
842 | } |
843 | ||
bce54474 PB |
844 | static void device_class_base_init(ObjectClass *class, void *data) |
845 | { | |
846 | DeviceClass *klass = DEVICE_CLASS(class); | |
847 | ||
848 | /* We explicitly look up properties in the superclasses, | |
849 | * so do not propagate them to the subclasses. | |
850 | */ | |
851 | klass->props = NULL; | |
60adba37 AL |
852 | } |
853 | ||
5d5b24d0 | 854 | static void device_unparent(Object *obj) |
667d22d1 PB |
855 | { |
856 | DeviceState *dev = DEVICE(obj); | |
06f7f2bb | 857 | BusState *bus; |
0402a5d6 | 858 | QObject *event_data; |
b1ee5829 | 859 | bool have_realized = dev->realized; |
667d22d1 | 860 | |
5c21ce77 BD |
861 | if (dev->realized) { |
862 | object_property_set_bool(obj, false, "realized", NULL); | |
863 | } | |
06f7f2bb PB |
864 | while (dev->num_child_bus) { |
865 | bus = QLIST_FIRST(&dev->child_bus); | |
6780a22c | 866 | object_unparent(OBJECT(bus)); |
06f7f2bb | 867 | } |
06f7f2bb | 868 | if (dev->parent_bus) { |
5d5b24d0 | 869 | bus_remove_child(dev->parent_bus, dev); |
62d7ba66 PB |
870 | object_unref(OBJECT(dev->parent_bus)); |
871 | dev->parent_bus = NULL; | |
5d5b24d0 | 872 | } |
0402a5d6 | 873 | |
b1ee5829 AL |
874 | /* Only send event if the device had been completely realized */ |
875 | if (have_realized) { | |
876 | gchar *path = object_get_canonical_path(OBJECT(dev)); | |
877 | ||
878 | if (dev->id) { | |
879 | event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }", | |
880 | dev->id, path); | |
881 | } else { | |
882 | event_data = qobject_from_jsonf("{ 'path': %s }", path); | |
883 | } | |
884 | monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data); | |
885 | qobject_decref(event_data); | |
886 | g_free(path); | |
0402a5d6 | 887 | } |
667d22d1 PB |
888 | } |
889 | ||
890 | static void device_class_init(ObjectClass *class, void *data) | |
891 | { | |
249d4172 AF |
892 | DeviceClass *dc = DEVICE_CLASS(class); |
893 | ||
5d5b24d0 | 894 | class->unparent = device_unparent; |
249d4172 | 895 | dc->realize = device_realize; |
fe6c2117 | 896 | dc->unrealize = device_unrealize; |
267a3264 IM |
897 | |
898 | /* by default all devices were considered as hotpluggable, | |
899 | * so with intent to check it in generic qdev_unplug() / | |
900 | * device_set_realized() functions make every device | |
901 | * hotpluggable. Devices that shouldn't be hotpluggable, | |
902 | * should override it in their class_init() | |
903 | */ | |
904 | dc->hotpluggable = true; | |
667d22d1 PB |
905 | } |
906 | ||
94afdadc AL |
907 | void device_reset(DeviceState *dev) |
908 | { | |
909 | DeviceClass *klass = DEVICE_GET_CLASS(dev); | |
910 | ||
911 | if (klass->reset) { | |
912 | klass->reset(dev); | |
913 | } | |
914 | } | |
915 | ||
f05f6b4a PB |
916 | Object *qdev_get_machine(void) |
917 | { | |
918 | static Object *dev; | |
919 | ||
920 | if (dev == NULL) { | |
dfe47e70 | 921 | dev = container_get(object_get_root(), "/machine"); |
f05f6b4a PB |
922 | } |
923 | ||
924 | return dev; | |
925 | } | |
926 | ||
8c43a6f0 | 927 | static const TypeInfo device_type_info = { |
32fea402 AL |
928 | .name = TYPE_DEVICE, |
929 | .parent = TYPE_OBJECT, | |
930 | .instance_size = sizeof(DeviceState), | |
9674bfe4 | 931 | .instance_init = device_initfn, |
99a0b036 | 932 | .instance_post_init = device_post_init, |
60adba37 | 933 | .instance_finalize = device_finalize, |
bce54474 | 934 | .class_base_init = device_class_base_init, |
667d22d1 | 935 | .class_init = device_class_init, |
32fea402 AL |
936 | .abstract = true, |
937 | .class_size = sizeof(DeviceClass), | |
938 | }; | |
939 | ||
ac7d1ba6 AL |
940 | static void qbus_initfn(Object *obj) |
941 | { | |
942 | BusState *bus = BUS(obj); | |
943 | ||
944 | QTAILQ_INIT(&bus->children); | |
0ee4de6c IM |
945 | object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY, |
946 | TYPE_HOTPLUG_HANDLER, | |
947 | (Object **)&bus->hotplug_handler, NULL); | |
02e7f85d BD |
948 | object_property_add_bool(obj, "realized", |
949 | bus_get_realized, bus_set_realized, NULL); | |
ac7d1ba6 AL |
950 | } |
951 | ||
bbfa18fc AK |
952 | static char *default_bus_get_fw_dev_path(DeviceState *dev) |
953 | { | |
954 | return g_strdup(object_get_typename(OBJECT(dev))); | |
955 | } | |
956 | ||
6853d27a PB |
957 | static void bus_class_init(ObjectClass *class, void *data) |
958 | { | |
bbfa18fc AK |
959 | BusClass *bc = BUS_CLASS(class); |
960 | ||
6853d27a | 961 | class->unparent = bus_unparent; |
bbfa18fc | 962 | bc->get_fw_dev_path = default_bus_get_fw_dev_path; |
6853d27a PB |
963 | } |
964 | ||
ac7d1ba6 AL |
965 | static void qbus_finalize(Object *obj) |
966 | { | |
967 | BusState *bus = BUS(obj); | |
ac7d1ba6 | 968 | |
ac7d1ba6 AL |
969 | g_free((char *)bus->name); |
970 | } | |
971 | ||
0d936928 AL |
972 | static const TypeInfo bus_info = { |
973 | .name = TYPE_BUS, | |
974 | .parent = TYPE_OBJECT, | |
975 | .instance_size = sizeof(BusState), | |
976 | .abstract = true, | |
977 | .class_size = sizeof(BusClass), | |
ac7d1ba6 AL |
978 | .instance_init = qbus_initfn, |
979 | .instance_finalize = qbus_finalize, | |
6853d27a | 980 | .class_init = bus_class_init, |
0d936928 AL |
981 | }; |
982 | ||
83f7d43a | 983 | static void qdev_register_types(void) |
32fea402 | 984 | { |
0d936928 | 985 | type_register_static(&bus_info); |
32fea402 AL |
986 | type_register_static(&device_type_info); |
987 | } | |
988 | ||
83f7d43a | 989 | type_init(qdev_register_types) |