]> git.proxmox.com Git - qemu.git/blame - hw/qdev.c
PPC: Fix TLB invalidation bug within the PPC interrupt handler.
[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
9d07d757 28#include "net.h"
aae9460e
PB
29#include "qdev.h"
30#include "sysemu.h"
56f9107e 31#include "error.h"
aae9460e 32
ee46d8a5 33int qdev_hotplug = 0;
0ac8ef71
AW
34static bool qdev_hot_added = false;
35static bool qdev_hot_removed = false;
3418bd25 36
cdaed7c7 37/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
b9aaf7f8 38static BusState *main_system_bus;
2da8bb92 39static void main_system_bus_create(void);
4d6ae674 40
aae9460e 41/* Register a new device type. */
4be9f0d1
AL
42const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
43{
6e008585
AL
44 DeviceClass *dc = DEVICE_GET_CLASS(dev);
45 return dc->vmsd;
4be9f0d1
AL
46}
47
48BusInfo *qdev_get_bus_info(DeviceState *dev)
49{
6e008585
AL
50 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51 return dc->bus_info;
4be9f0d1
AL
52}
53
54Property *qdev_get_props(DeviceState *dev)
55{
6e008585
AL
56 DeviceClass *dc = DEVICE_GET_CLASS(dev);
57 return dc->props;
4be9f0d1
AL
58}
59
60const char *qdev_fw_name(DeviceState *dev)
61{
6e008585 62 DeviceClass *dc = DEVICE_GET_CLASS(dev);
4be9f0d1 63
6e008585
AL
64 if (dc->fw_name) {
65 return dc->fw_name;
4be9f0d1
AL
66 }
67
68 return object_get_typename(OBJECT(dev));
69}
70
a369da5f
BS
71bool qdev_exists(const char *name)
72{
212ad111 73 return !!object_class_by_name(name);
a369da5f 74}
40021f08 75
ca2cc788
PB
76static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
77 Error **errp);
78
9fbe6127 79void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
0c17542d 80{
a5296ca9 81 Property *prop;
0c17542d 82
0c17542d
MA
83 if (qdev_hotplug) {
84 assert(bus->allow_hotplug);
0c17542d 85 }
a5296ca9 86
9fbe6127 87 dev->parent_bus = bus;
9674bfe4 88 QTAILQ_INSERT_HEAD(&bus->children, dev, sibling);
a5296ca9 89
6e008585 90 for (prop = qdev_get_bus_info(dev)->props; prop && prop->name; prop++) {
a5296ca9 91 qdev_property_add_legacy(dev, prop, NULL);
ca2cc788 92 qdev_property_add_static(dev, prop, NULL);
a5296ca9 93 }
4f2d3d70 94 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
0c17542d
MA
95}
96
aae9460e
PB
97/* Create a new device. This only initializes the device state structure
98 and allows properties to be set. qdev_init should be called to
99 initialize the actual device emulation. */
02e2da45 100DeviceState *qdev_create(BusState *bus, const char *name)
0bcdeda7
BS
101{
102 DeviceState *dev;
103
104 dev = qdev_try_create(bus, name);
105 if (!dev) {
e92714c7
PM
106 if (bus) {
107 hw_error("Unknown device '%s' for bus '%s'\n", name,
108 bus->info->name);
109 } else {
110 hw_error("Unknown device '%s' for default sysbus\n", name);
111 }
0bcdeda7
BS
112 }
113
114 return dev;
115}
116
da57febf 117DeviceState *qdev_try_create(BusState *bus, const char *type)
aae9460e 118{
9fbe6127
AL
119 DeviceState *dev;
120
da57febf 121 if (object_class_by_name(type) == NULL) {
4ed658ca
AF
122 return NULL;
123 }
da57febf 124 dev = DEVICE(object_new(type));
9fbe6127
AL
125 if (!dev) {
126 return NULL;
127 }
128
10c4c98a 129 if (!bus) {
68694897 130 bus = sysbus_get_default();
10c4c98a
GH
131 }
132
9fbe6127
AL
133 qdev_set_parent_bus(dev, bus);
134 qdev_prop_set_globals(dev);
135
136 return dev;
aae9460e
PB
137}
138
139/* Initialize a device. Device properties should be set before calling
140 this function. IRQs and MMIO regions should be connected/mapped after
18cfeb52
MA
141 calling this function.
142 On failure, destroy the device and return negative value.
143 Return 0 on success. */
81a322d4 144int qdev_init(DeviceState *dev)
aae9460e 145{
6e008585 146 DeviceClass *dc = DEVICE_GET_CLASS(dev);
959f733a
GH
147 int rc;
148
131ec1bd 149 assert(dev->state == DEV_STATE_CREATED);
6e008585 150
d307af79 151 rc = dc->init(dev);
18cfeb52
MA
152 if (rc < 0) {
153 qdev_free(dev);
959f733a 154 return rc;
18cfeb52 155 }
da57febf
PB
156
157 if (!OBJECT(dev)->parent) {
158 static int unattached_count = 0;
159 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
160
f05f6b4a 161 object_property_add_child(container_get("/machine/unattached"), name,
da57febf
PB
162 OBJECT(dev), NULL);
163 g_free(name);
164 }
165
6e008585
AL
166 if (qdev_get_vmsd(dev)) {
167 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
4d2ffa08
JK
168 dev->instance_id_alias,
169 dev->alias_required_for_version);
170 }
131ec1bd 171 dev->state = DEV_STATE_INITIALIZED;
94afdadc
AL
172 if (dev->hotplugged) {
173 device_reset(dev);
5ab28c83 174 }
959f733a 175 return 0;
02e2da45
PB
176}
177
4d2ffa08
JK
178void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
179 int required_for_version)
180{
181 assert(dev->state == DEV_STATE_CREATED);
182 dev->instance_id_alias = alias_id;
183 dev->alias_required_for_version = required_for_version;
184}
185
56f9107e 186void qdev_unplug(DeviceState *dev, Error **errp)
3418bd25 187{
6e008585
AL
188 DeviceClass *dc = DEVICE_GET_CLASS(dev);
189
3418bd25 190 if (!dev->parent_bus->allow_hotplug) {
56f9107e
LC
191 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
192 return;
3418bd25 193 }
6e008585 194 assert(dc->unplug != NULL);
593831de 195
0ac8ef71
AW
196 qdev_hot_removed = true;
197
56f9107e
LC
198 if (dc->unplug(dev) < 0) {
199 error_set(errp, QERR_UNDEFINED_ERROR);
200 return;
201 }
3418bd25
GH
202}
203
ec990eb6
AL
204static int qdev_reset_one(DeviceState *dev, void *opaque)
205{
94afdadc 206 device_reset(dev);
ec990eb6
AL
207
208 return 0;
209}
210
211BusState *sysbus_get_default(void)
212{
68694897 213 if (!main_system_bus) {
2da8bb92 214 main_system_bus_create();
68694897 215 }
ec990eb6
AL
216 return main_system_bus;
217}
218
b4694b7c
IY
219static int qbus_reset_one(BusState *bus, void *opaque)
220{
221 if (bus->info->reset) {
222 return bus->info->reset(bus);
223 }
224 return 0;
225}
226
5af0a04b
IY
227void qdev_reset_all(DeviceState *dev)
228{
229 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
230}
231
80376c3f
IY
232void qbus_reset_all_fn(void *opaque)
233{
234 BusState *bus = opaque;
f530cce3 235 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
80376c3f
IY
236}
237
3418bd25
GH
238/* can be used as ->unplug() callback for the simple cases */
239int qdev_simple_unplug_cb(DeviceState *dev)
240{
241 /* just zap it */
57c9fafe 242 object_unparent(OBJECT(dev));
3418bd25
GH
243 qdev_free(dev);
244 return 0;
245}
246
3b29a101
MT
247
248/* Like qdev_init(), but terminate program via error_report() instead of
e23a1b33
MA
249 returning an error value. This is okay during machine creation.
250 Don't use for hotplug, because there callers need to recover from
251 failure. Exception: if you know the device's init() callback can't
252 fail, then qdev_init_nofail() can't fail either, and is therefore
253 usable even then. But relying on the device implementation that
254 way is somewhat unclean, and best avoided. */
255void qdev_init_nofail(DeviceState *dev)
256{
bd6c9a61 257 if (qdev_init(dev) < 0) {
6e008585
AL
258 error_report("Initialization of device %s failed",
259 object_get_typename(OBJECT(dev)));
bd6c9a61
MA
260 exit(1);
261 }
e23a1b33
MA
262}
263
02e2da45
PB
264/* Unlink device from bus and free the structure. */
265void qdev_free(DeviceState *dev)
266{
32fea402 267 object_delete(OBJECT(dev));
aae9460e
PB
268}
269
3418bd25
GH
270void qdev_machine_creation_done(void)
271{
272 /*
273 * ok, initial machine setup is done, starting from now we can
274 * only create hotpluggable devices
275 */
276 qdev_hotplug = 1;
277}
278
0ac8ef71
AW
279bool qdev_machine_modified(void)
280{
281 return qdev_hot_added || qdev_hot_removed;
282}
283
02e2da45 284BusState *qdev_get_parent_bus(DeviceState *dev)
aae9460e 285{
02e2da45 286 return dev->parent_bus;
aae9460e
PB
287}
288
aae9460e
PB
289void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
290{
291 assert(dev->num_gpio_in == 0);
292 dev->num_gpio_in = n;
293 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
294}
295
296void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
297{
298 assert(dev->num_gpio_out == 0);
299 dev->num_gpio_out = n;
300 dev->gpio_out = pins;
301}
302
303qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
304{
305 assert(n >= 0 && n < dev->num_gpio_in);
306 return dev->gpio_in[n];
307}
308
309void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
310{
311 assert(n >= 0 && n < dev->num_gpio_out);
312 dev->gpio_out[n] = pin;
313}
314
ed16ab5a
GH
315void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
316{
6eed1856 317 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
ed16ab5a
GH
318 if (nd->vlan)
319 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
320 if (nd->netdev)
321 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
75422b0d 322 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
97b15621
GH
323 qdev_prop_exists(dev, "vectors")) {
324 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
325 }
48e2faf2 326 nd->instantiated = 1;
ed16ab5a
GH
327}
328
02e2da45 329BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
4d6ae674 330{
02e2da45 331 BusState *bus;
4d6ae674 332
72cf2d4f 333 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
4d6ae674 334 if (strcmp(name, bus->name) == 0) {
02e2da45 335 return bus;
4d6ae674
PB
336 }
337 }
338 return NULL;
339}
340
81699d8a
AL
341int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
342 qbus_walkerfn *busfn, void *opaque)
343{
344 DeviceState *dev;
345 int err;
346
347 if (busfn) {
348 err = busfn(bus, opaque);
349 if (err) {
350 return err;
351 }
352 }
353
d8bb00d6 354 QTAILQ_FOREACH(dev, &bus->children, sibling) {
81699d8a
AL
355 err = qdev_walk_children(dev, devfn, busfn, opaque);
356 if (err < 0) {
357 return err;
358 }
359 }
360
361 return 0;
362}
363
364int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
365 qbus_walkerfn *busfn, void *opaque)
366{
367 BusState *bus;
368 int err;
369
370 if (devfn) {
371 err = devfn(dev, opaque);
372 if (err) {
373 return err;
374 }
375 }
376
377 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
378 err = qbus_walk_children(bus, devfn, busfn, opaque);
379 if (err < 0) {
380 return err;
381 }
382 }
383
384 return 0;
385}
386
a2ee6b4f 387DeviceState *qdev_find_recursive(BusState *bus, const char *id)
3418bd25
GH
388{
389 DeviceState *dev, *ret;
390 BusState *child;
391
d8bb00d6 392 QTAILQ_FOREACH(dev, &bus->children, sibling) {
3418bd25
GH
393 if (dev->id && strcmp(dev->id, id) == 0)
394 return dev;
395 QLIST_FOREACH(child, &dev->child_bus, sibling) {
396 ret = qdev_find_recursive(child, id);
397 if (ret) {
398 return ret;
399 }
400 }
401 }
402 return NULL;
403}
404
cd739fb6
GH
405void qbus_create_inplace(BusState *bus, BusInfo *info,
406 DeviceState *parent, const char *name)
02e2da45 407{
d271de9f
GH
408 char *buf;
409 int i,len;
02e2da45 410
10c4c98a 411 bus->info = info;
02e2da45 412 bus->parent = parent;
d271de9f
GH
413
414 if (name) {
415 /* use supplied name */
7267c094 416 bus->name = g_strdup(name);
d271de9f
GH
417 } else if (parent && parent->id) {
418 /* parent device has id -> use it for bus name */
419 len = strlen(parent->id) + 16;
7267c094 420 buf = g_malloc(len);
d271de9f
GH
421 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
422 bus->name = buf;
423 } else {
424 /* no id -> use lowercase bus type for bus name */
425 len = strlen(info->name) + 16;
7267c094 426 buf = g_malloc(len);
d271de9f
GH
427 len = snprintf(buf, len, "%s.%d", info->name,
428 parent ? parent->num_child_bus : 0);
429 for (i = 0; i < len; i++)
bb87ece5 430 buf[i] = qemu_tolower(buf[i]);
d271de9f
GH
431 bus->name = buf;
432 }
433
d8bb00d6 434 QTAILQ_INIT(&bus->children);
02e2da45 435 if (parent) {
72cf2d4f 436 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
d271de9f 437 parent->num_child_bus++;
80376c3f
IY
438 } else if (bus != main_system_bus) {
439 /* TODO: once all bus devices are qdevified,
440 only reset handler for main_system_bus should be registered here. */
441 qemu_register_reset(qbus_reset_all_fn, bus);
02e2da45 442 }
cd739fb6
GH
443}
444
445BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
446{
447 BusState *bus;
448
7267c094 449 bus = g_malloc0(info->size);
cd739fb6
GH
450 bus->qdev_allocated = 1;
451 qbus_create_inplace(bus, info, parent, name);
02e2da45
PB
452 return bus;
453}
cae4956e 454
2da8bb92
IY
455static void main_system_bus_create(void)
456{
457 /* assign main_system_bus before qbus_create_inplace()
458 * in order to make "if (bus != main_system_bus)" work */
7267c094 459 main_system_bus = g_malloc0(system_bus_info.size);
2da8bb92
IY
460 main_system_bus->qdev_allocated = 1;
461 qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
462 "main-system-bus");
463}
464
131ec1bd
GH
465void qbus_free(BusState *bus)
466{
467 DeviceState *dev;
468
d8bb00d6 469 while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) {
131ec1bd
GH
470 qdev_free(dev);
471 }
472 if (bus->parent) {
473 QLIST_REMOVE(bus, sibling);
474 bus->parent->num_child_bus--;
80376c3f
IY
475 } else {
476 assert(bus != main_system_bus); /* main_system_bus is never freed */
477 qemu_unregister_reset(qbus_reset_all_fn, bus);
131ec1bd 478 }
7267c094 479 g_free((void*)bus->name);
131ec1bd 480 if (bus->qdev_allocated) {
7267c094 481 g_free(bus);
131ec1bd
GH
482 }
483}
484
1ca4d09a
GN
485static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
486{
487 int l = 0;
488
489 if (dev && dev->parent_bus) {
490 char *d;
491 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
492 if (dev->parent_bus->info->get_fw_dev_path) {
493 d = dev->parent_bus->info->get_fw_dev_path(dev);
494 l += snprintf(p + l, size - l, "%s", d);
7267c094 495 g_free(d);
1ca4d09a 496 } else {
f79f2bfc 497 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
1ca4d09a
GN
498 }
499 }
500 l += snprintf(p + l , size - l, "/");
501
502 return l;
503}
504
505char* qdev_get_fw_dev_path(DeviceState *dev)
506{
507 char path[128];
508 int l;
509
510 l = qdev_get_fw_dev_path_helper(dev, path, 128);
511
512 path[l-1] = '\0';
513
514 return strdup(path);
515}
85ed303b 516
57c9fafe 517static char *qdev_get_type(Object *obj, Error **errp)
85ed303b 518{
57c9fafe 519 return g_strdup(object_get_typename(obj));
44677ded 520}
a5296ca9
AL
521
522/**
523 * Legacy property handling
524 */
525
57c9fafe 526static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
a5296ca9
AL
527 const char *name, Error **errp)
528{
57c9fafe 529 DeviceState *dev = DEVICE(obj);
a5296ca9
AL
530 Property *prop = opaque;
531
e3cb6ba6
PB
532 char buffer[1024];
533 char *ptr = buffer;
a5296ca9 534
e3cb6ba6
PB
535 prop->info->print(dev, prop, buffer, sizeof(buffer));
536 visit_type_str(v, &ptr, name, errp);
a5296ca9
AL
537}
538
57c9fafe 539static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
a5296ca9
AL
540 const char *name, Error **errp)
541{
57c9fafe 542 DeviceState *dev = DEVICE(obj);
a5296ca9 543 Property *prop = opaque;
e3cb6ba6
PB
544 Error *local_err = NULL;
545 char *ptr = NULL;
546 int ret;
a5296ca9
AL
547
548 if (dev->state != DEV_STATE_CREATED) {
549 error_set(errp, QERR_PERMISSION_DENIED);
550 return;
551 }
552
e3cb6ba6
PB
553 visit_type_str(v, &ptr, name, &local_err);
554 if (local_err) {
555 error_propagate(errp, local_err);
556 return;
557 }
a5296ca9 558
e3cb6ba6 559 ret = prop->info->parse(dev, prop, ptr);
7db4c4e8 560 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
e3cb6ba6 561 g_free(ptr);
a5296ca9
AL
562}
563
564/**
565 * @qdev_add_legacy_property - adds a legacy property
566 *
567 * Do not use this is new code! Properties added through this interface will
ca2cc788 568 * be given names and types in the "legacy" namespace.
a5296ca9 569 *
68ee3569
PB
570 * Legacy properties are string versions of other OOM properties. The format
571 * of the string depends on the property type.
a5296ca9
AL
572 */
573void qdev_property_add_legacy(DeviceState *dev, Property *prop,
574 Error **errp)
575{
ca2cc788 576 gchar *name, *type;
a5296ca9 577
68ee3569
PB
578 if (!prop->info->print && !prop->info->parse) {
579 return;
580 }
ca2cc788 581 name = g_strdup_printf("legacy-%s", prop->name);
cafe5bdb
PB
582 type = g_strdup_printf("legacy<%s>",
583 prop->info->legacy_name ?: prop->info->name);
a5296ca9 584
57c9fafe 585 object_property_add(OBJECT(dev), name, type,
68ee3569
PB
586 prop->info->print ? qdev_get_legacy_property : prop->info->get,
587 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
57c9fafe
AL
588 NULL,
589 prop, errp);
a5296ca9
AL
590
591 g_free(type);
ca2cc788
PB
592 g_free(name);
593}
594
595/**
596 * @qdev_property_add_static - add a @Property to a device.
597 *
598 * Static properties access data in a struct. The actual type of the
599 * property and the field depends on the property type.
600 */
601void qdev_property_add_static(DeviceState *dev, Property *prop,
602 Error **errp)
603{
d822979b
PB
604 /*
605 * TODO qdev_prop_ptr does not have getters or setters. It must
606 * go now that it can be replaced with links. The test should be
607 * removed along with it: all static properties are read/write.
608 */
609 if (!prop->info->get && !prop->info->set) {
610 return;
611 }
612
57c9fafe
AL
613 object_property_add(OBJECT(dev), prop->name, prop->info->name,
614 prop->info->get, prop->info->set,
dd0ba250 615 prop->info->release,
57c9fafe 616 prop, errp);
6a146eba 617}
1de81d28 618
9674bfe4
AL
619static void device_initfn(Object *obj)
620{
621 DeviceState *dev = DEVICE(obj);
622 Property *prop;
623
624 if (qdev_hotplug) {
625 dev->hotplugged = 1;
626 qdev_hot_added = true;
627 }
628
629 dev->instance_id_alias = -1;
9674bfe4
AL
630 dev->state = DEV_STATE_CREATED;
631
9674bfe4
AL
632 for (prop = qdev_get_props(dev); prop && prop->name; prop++) {
633 qdev_property_add_legacy(dev, prop, NULL);
634 qdev_property_add_static(dev, prop, NULL);
635 }
636
57c9fafe 637 object_property_add_str(OBJECT(dev), "type", qdev_get_type, NULL, NULL);
4f2d3d70 638 qdev_prop_set_defaults(dev, qdev_get_props(dev));
9674bfe4
AL
639}
640
60adba37
AL
641/* Unlink device from bus and free the structure. */
642static void device_finalize(Object *obj)
643{
644 DeviceState *dev = DEVICE(obj);
645 BusState *bus;
60adba37
AL
646 DeviceClass *dc = DEVICE_GET_CLASS(dev);
647
648 if (dev->state == DEV_STATE_INITIALIZED) {
649 while (dev->num_child_bus) {
650 bus = QLIST_FIRST(&dev->child_bus);
651 qbus_free(bus);
652 }
653 if (qdev_get_vmsd(dev)) {
654 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
655 }
656 if (dc->exit) {
657 dc->exit(dev);
658 }
659 if (dev->opts) {
660 qemu_opts_del(dev->opts);
661 }
662 }
663 QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling);
60adba37
AL
664}
665
94afdadc
AL
666void device_reset(DeviceState *dev)
667{
668 DeviceClass *klass = DEVICE_GET_CLASS(dev);
669
670 if (klass->reset) {
671 klass->reset(dev);
672 }
673}
674
f05f6b4a
PB
675Object *qdev_get_machine(void)
676{
677 static Object *dev;
678
679 if (dev == NULL) {
680 dev = container_get("/machine");
681 }
682
683 return dev;
684}
685
32fea402
AL
686static TypeInfo device_type_info = {
687 .name = TYPE_DEVICE,
688 .parent = TYPE_OBJECT,
689 .instance_size = sizeof(DeviceState),
9674bfe4 690 .instance_init = device_initfn,
60adba37 691 .instance_finalize = device_finalize,
32fea402
AL
692 .abstract = true,
693 .class_size = sizeof(DeviceClass),
694};
695
83f7d43a 696static void qdev_register_types(void)
32fea402
AL
697{
698 type_register_static(&device_type_info);
699}
700
83f7d43a 701type_init(qdev_register_types)