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