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