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