2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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
29 #include "sysemu/sysemu.h"
30 #include "qapi/error.h"
31 #include "qapi/visitor.h"
34 static bool qdev_hot_added
= false;
35 static bool qdev_hot_removed
= false;
37 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
)
39 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
43 const char *qdev_fw_name(DeviceState
*dev
)
45 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
51 return object_get_typename(OBJECT(dev
));
54 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
57 static void bus_remove_child(BusState
*bus
, DeviceState
*child
)
61 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
62 if (kid
->child
== child
) {
65 snprintf(name
, sizeof(name
), "child[%d]", kid
->index
);
66 QTAILQ_REMOVE(&bus
->children
, kid
, sibling
);
68 /* This gives back ownership of kid->child back to us. */
69 object_property_del(OBJECT(bus
), name
, NULL
);
70 object_unref(OBJECT(kid
->child
));
77 static void bus_add_child(BusState
*bus
, DeviceState
*child
)
80 BusChild
*kid
= g_malloc0(sizeof(*kid
));
83 assert(bus
->allow_hotplug
);
86 kid
->index
= bus
->max_index
++;
88 object_ref(OBJECT(kid
->child
));
90 QTAILQ_INSERT_HEAD(&bus
->children
, kid
, sibling
);
92 /* This transfers ownership of kid->child to the property. */
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
,
100 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
)
102 dev
->parent_bus
= bus
;
103 object_ref(OBJECT(bus
));
104 bus_add_child(bus
, dev
);
107 /* Create a new device. This only initializes the device state structure
108 and allows properties to be set. qdev_init should be called to
109 initialize the actual device emulation. */
110 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
114 dev
= qdev_try_create(bus
, name
);
117 error_report("Unknown device '%s' for bus '%s'\n", name
,
118 object_get_typename(OBJECT(bus
)));
121 error_report("Unknown device '%s' for default sysbus\n", name
);
129 DeviceState
*qdev_try_create(BusState
*bus
, const char *type
)
133 if (object_class_by_name(type
) == NULL
) {
136 dev
= DEVICE(object_new(type
));
142 bus
= sysbus_get_default();
145 qdev_set_parent_bus(dev
, bus
);
146 object_unref(OBJECT(dev
));
150 /* Initialize a device. Device properties should be set before calling
151 this function. IRQs and MMIO regions should be connected/mapped after
152 calling this function.
153 On failure, destroy the device and return negative value.
154 Return 0 on success. */
155 int qdev_init(DeviceState
*dev
)
157 Error
*local_err
= NULL
;
159 assert(!dev
->realized
);
161 object_property_set_bool(OBJECT(dev
), true, "realized", &local_err
);
162 if (local_err
!= NULL
) {
163 error_free(local_err
);
170 static void device_realize(DeviceState
*dev
, Error
**err
)
172 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
175 int rc
= dc
->init(dev
);
177 error_setg(err
, "Device initialization failed.");
183 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
184 int required_for_version
)
186 assert(!dev
->realized
);
187 dev
->instance_id_alias
= alias_id
;
188 dev
->alias_required_for_version
= required_for_version
;
191 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
193 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
195 if (!dev
->parent_bus
->allow_hotplug
) {
196 error_set(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
199 assert(dc
->unplug
!= NULL
);
201 qdev_hot_removed
= true;
203 if (dc
->unplug(dev
) < 0) {
204 error_set(errp
, QERR_UNDEFINED_ERROR
);
209 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
216 static int qbus_reset_one(BusState
*bus
, void *opaque
)
218 BusClass
*bc
= BUS_GET_CLASS(bus
);
220 return bc
->reset(bus
);
225 void qdev_reset_all(DeviceState
*dev
)
227 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
230 void qbus_reset_all(BusState
*bus
)
232 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
235 void qbus_reset_all_fn(void *opaque
)
237 BusState
*bus
= opaque
;
241 /* can be used as ->unplug() callback for the simple cases */
242 int qdev_simple_unplug_cb(DeviceState
*dev
)
250 /* Like qdev_init(), but terminate program via error_report() instead of
251 returning an error value. This is okay during machine creation.
252 Don't use for hotplug, because there callers need to recover from
253 failure. Exception: if you know the device's init() callback can't
254 fail, then qdev_init_nofail() can't fail either, and is therefore
255 usable even then. But relying on the device implementation that
256 way is somewhat unclean, and best avoided. */
257 void qdev_init_nofail(DeviceState
*dev
)
259 const char *typename
= object_get_typename(OBJECT(dev
));
261 if (qdev_init(dev
) < 0) {
262 error_report("Initialization of device %s failed", typename
);
267 /* Unlink device from bus and free the structure. */
268 void qdev_free(DeviceState
*dev
)
270 object_unparent(OBJECT(dev
));
273 void qdev_machine_creation_done(void)
276 * ok, initial machine setup is done, starting from now we can
277 * only create hotpluggable devices
282 bool qdev_machine_modified(void)
284 return qdev_hot_added
|| qdev_hot_removed
;
287 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
289 return dev
->parent_bus
;
292 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
294 dev
->gpio_in
= qemu_extend_irqs(dev
->gpio_in
, dev
->num_gpio_in
, handler
,
296 dev
->num_gpio_in
+= n
;
299 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
301 assert(dev
->num_gpio_out
== 0);
302 dev
->num_gpio_out
= n
;
303 dev
->gpio_out
= pins
;
306 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
308 assert(n
>= 0 && n
< dev
->num_gpio_in
);
309 return dev
->gpio_in
[n
];
312 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
314 assert(n
>= 0 && n
< dev
->num_gpio_out
);
315 dev
->gpio_out
[n
] = pin
;
318 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
322 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
323 if (strcmp(name
, bus
->name
) == 0) {
330 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
331 qbus_walkerfn
*busfn
, void *opaque
)
337 err
= busfn(bus
, opaque
);
343 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
344 err
= qdev_walk_children(kid
->child
, devfn
, busfn
, opaque
);
353 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
354 qbus_walkerfn
*busfn
, void *opaque
)
360 err
= devfn(dev
, opaque
);
366 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
367 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
376 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
382 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
383 DeviceState
*dev
= kid
->child
;
385 if (dev
->id
&& strcmp(dev
->id
, id
) == 0) {
389 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
390 ret
= qdev_find_recursive(child
, id
);
399 static void qbus_realize(BusState
*bus
, DeviceState
*parent
, const char *name
)
401 const char *typename
= object_get_typename(OBJECT(bus
));
405 bus
->parent
= parent
;
408 bus
->name
= g_strdup(name
);
409 } else if (bus
->parent
&& bus
->parent
->id
) {
410 /* parent device has id -> use it for bus name */
411 len
= strlen(bus
->parent
->id
) + 16;
413 snprintf(buf
, len
, "%s.%d", bus
->parent
->id
, bus
->parent
->num_child_bus
);
416 /* no id -> use lowercase bus type for bus name */
417 len
= strlen(typename
) + 16;
419 len
= snprintf(buf
, len
, "%s.%d", typename
,
420 bus
->parent
? bus
->parent
->num_child_bus
: 0);
421 for (i
= 0; i
< len
; i
++)
422 buf
[i
] = qemu_tolower(buf
[i
]);
427 QLIST_INSERT_HEAD(&bus
->parent
->child_bus
, bus
, sibling
);
428 bus
->parent
->num_child_bus
++;
429 object_property_add_child(OBJECT(bus
->parent
), bus
->name
, OBJECT(bus
), NULL
);
430 object_unref(OBJECT(bus
));
431 } else if (bus
!= sysbus_get_default()) {
432 /* TODO: once all bus devices are qdevified,
433 only reset handler for main_system_bus should be registered here. */
434 qemu_register_reset(qbus_reset_all_fn
, bus
);
438 static void bus_unparent(Object
*obj
)
440 BusState
*bus
= BUS(obj
);
443 while ((kid
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
444 DeviceState
*dev
= kid
->child
;
448 QLIST_REMOVE(bus
, sibling
);
449 bus
->parent
->num_child_bus
--;
452 assert(bus
!= sysbus_get_default()); /* main_system_bus is never freed */
453 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
457 void qbus_create_inplace(void *bus
, const char *typename
,
458 DeviceState
*parent
, const char *name
)
460 object_initialize(bus
, typename
);
461 qbus_realize(bus
, parent
, name
);
464 BusState
*qbus_create(const char *typename
, DeviceState
*parent
, const char *name
)
468 bus
= BUS(object_new(typename
));
469 qbus_realize(bus
, parent
, name
);
474 void qbus_free(BusState
*bus
)
476 object_unparent(OBJECT(bus
));
479 static char *bus_get_fw_dev_path(BusState
*bus
, DeviceState
*dev
)
481 BusClass
*bc
= BUS_GET_CLASS(bus
);
483 if (bc
->get_fw_dev_path
) {
484 return bc
->get_fw_dev_path(dev
);
490 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
494 if (dev
&& dev
->parent_bus
) {
496 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
497 d
= bus_get_fw_dev_path(dev
->parent_bus
, dev
);
499 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
502 l
+= snprintf(p
+ l
, size
- l
, "%s", object_get_typename(OBJECT(dev
)));
505 l
+= snprintf(p
+ l
, size
- l
, "/");
510 char* qdev_get_fw_dev_path(DeviceState
*dev
)
515 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
519 return g_strdup(path
);
522 char *qdev_get_dev_path(DeviceState
*dev
)
526 if (!dev
|| !dev
->parent_bus
) {
530 bc
= BUS_GET_CLASS(dev
->parent_bus
);
531 if (bc
->get_dev_path
) {
532 return bc
->get_dev_path(dev
);
539 * Legacy property handling
542 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
543 const char *name
, Error
**errp
)
545 DeviceState
*dev
= DEVICE(obj
);
546 Property
*prop
= opaque
;
551 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
552 visit_type_str(v
, &ptr
, name
, errp
);
555 static void qdev_set_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
556 const char *name
, Error
**errp
)
558 DeviceState
*dev
= DEVICE(obj
);
559 Property
*prop
= opaque
;
560 Error
*local_err
= NULL
;
565 error_set(errp
, QERR_PERMISSION_DENIED
);
569 visit_type_str(v
, &ptr
, name
, &local_err
);
571 error_propagate(errp
, local_err
);
575 ret
= prop
->info
->parse(dev
, prop
, ptr
);
576 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
581 * @qdev_add_legacy_property - adds a legacy property
583 * Do not use this is new code! Properties added through this interface will
584 * be given names and types in the "legacy" namespace.
586 * Legacy properties are string versions of other OOM properties. The format
587 * of the string depends on the property type.
589 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
594 /* Register pointer properties as legacy properties */
595 if (!prop
->info
->print
&& !prop
->info
->parse
&&
596 (prop
->info
->set
|| prop
->info
->get
)) {
600 name
= g_strdup_printf("legacy-%s", prop
->name
);
601 type
= g_strdup_printf("legacy<%s>",
602 prop
->info
->legacy_name
?: prop
->info
->name
);
604 object_property_add(OBJECT(dev
), name
, type
,
605 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
606 prop
->info
->parse
? qdev_set_legacy_property
: prop
->info
->set
,
615 * @qdev_property_add_static - add a @Property to a device.
617 * Static properties access data in a struct. The actual type of the
618 * property and the field depends on the property type.
620 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
623 Error
*local_err
= NULL
;
624 Object
*obj
= OBJECT(dev
);
627 * TODO qdev_prop_ptr does not have getters or setters. It must
628 * go now that it can be replaced with links. The test should be
629 * removed along with it: all static properties are read/write.
631 if (!prop
->info
->get
&& !prop
->info
->set
) {
635 object_property_add(obj
, prop
->name
, prop
->info
->name
,
636 prop
->info
->get
, prop
->info
->set
,
641 error_propagate(errp
, local_err
);
644 if (prop
->qtype
== QTYPE_NONE
) {
648 if (prop
->qtype
== QTYPE_QBOOL
) {
649 object_property_set_bool(obj
, prop
->defval
, prop
->name
, &local_err
);
650 } else if (prop
->info
->enum_table
) {
651 object_property_set_str(obj
, prop
->info
->enum_table
[prop
->defval
],
652 prop
->name
, &local_err
);
653 } else if (prop
->qtype
== QTYPE_QINT
) {
654 object_property_set_int(obj
, prop
->defval
, prop
->name
, &local_err
);
656 assert_no_error(local_err
);
659 static bool device_get_realized(Object
*obj
, Error
**err
)
661 DeviceState
*dev
= DEVICE(obj
);
662 return dev
->realized
;
665 static void device_set_realized(Object
*obj
, bool value
, Error
**err
)
667 DeviceState
*dev
= DEVICE(obj
);
668 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
669 Error
*local_err
= NULL
;
671 if (value
&& !dev
->realized
) {
673 dc
->realize(dev
, &local_err
);
676 if (!obj
->parent
&& local_err
== NULL
) {
677 static int unattached_count
;
678 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
680 object_property_add_child(container_get(qdev_get_machine(),
682 name
, obj
, &local_err
);
686 if (qdev_get_vmsd(dev
) && local_err
== NULL
) {
687 vmstate_register_with_alias_id(dev
, -1, qdev_get_vmsd(dev
), dev
,
688 dev
->instance_id_alias
,
689 dev
->alias_required_for_version
);
691 if (dev
->hotplugged
&& local_err
== NULL
) {
694 } else if (!value
&& dev
->realized
) {
696 dc
->unrealize(dev
, &local_err
);
700 if (local_err
!= NULL
) {
701 error_propagate(err
, local_err
);
705 dev
->realized
= value
;
708 static void device_initfn(Object
*obj
)
710 DeviceState
*dev
= DEVICE(obj
);
716 qdev_hot_added
= true;
719 dev
->instance_id_alias
= -1;
720 dev
->realized
= false;
722 object_property_add_bool(obj
, "realized",
723 device_get_realized
, device_set_realized
, NULL
);
725 class = object_get_class(OBJECT(dev
));
727 for (prop
= DEVICE_CLASS(class)->props
; prop
&& prop
->name
; prop
++) {
728 qdev_property_add_legacy(dev
, prop
, NULL
);
729 qdev_property_add_static(dev
, prop
, NULL
);
731 class = object_class_get_parent(class);
732 } while (class != object_class_by_name(TYPE_DEVICE
));
733 qdev_prop_set_globals(dev
);
735 object_property_add_link(OBJECT(dev
), "parent_bus", TYPE_BUS
,
736 (Object
**)&dev
->parent_bus
, NULL
);
739 /* Unlink device from bus and free the structure. */
740 static void device_finalize(Object
*obj
)
742 DeviceState
*dev
= DEVICE(obj
);
744 qemu_opts_del(dev
->opts
);
748 static void device_class_base_init(ObjectClass
*class, void *data
)
750 DeviceClass
*klass
= DEVICE_CLASS(class);
752 /* We explicitly look up properties in the superclasses,
753 * so do not propagate them to the subclasses.
758 static void device_unparent(Object
*obj
)
760 DeviceState
*dev
= DEVICE(obj
);
761 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
764 while (dev
->num_child_bus
) {
765 bus
= QLIST_FIRST(&dev
->child_bus
);
769 if (qdev_get_vmsd(dev
)) {
770 vmstate_unregister(dev
, qdev_get_vmsd(dev
), dev
);
776 if (dev
->parent_bus
) {
777 bus_remove_child(dev
->parent_bus
, dev
);
778 object_unref(OBJECT(dev
->parent_bus
));
779 dev
->parent_bus
= NULL
;
783 static void device_class_init(ObjectClass
*class, void *data
)
785 DeviceClass
*dc
= DEVICE_CLASS(class);
787 class->unparent
= device_unparent
;
788 dc
->realize
= device_realize
;
791 void device_reset(DeviceState
*dev
)
793 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
800 Object
*qdev_get_machine(void)
805 dev
= container_get(object_get_root(), "/machine");
811 static const TypeInfo device_type_info
= {
813 .parent
= TYPE_OBJECT
,
814 .instance_size
= sizeof(DeviceState
),
815 .instance_init
= device_initfn
,
816 .instance_finalize
= device_finalize
,
817 .class_base_init
= device_class_base_init
,
818 .class_init
= device_class_init
,
820 .class_size
= sizeof(DeviceClass
),
823 static void qbus_initfn(Object
*obj
)
825 BusState
*bus
= BUS(obj
);
827 QTAILQ_INIT(&bus
->children
);
830 static void bus_class_init(ObjectClass
*class, void *data
)
832 class->unparent
= bus_unparent
;
835 static void qbus_finalize(Object
*obj
)
837 BusState
*bus
= BUS(obj
);
839 g_free((char *)bus
->name
);
842 static const TypeInfo bus_info
= {
844 .parent
= TYPE_OBJECT
,
845 .instance_size
= sizeof(BusState
),
847 .class_size
= sizeof(BusClass
),
848 .instance_init
= qbus_initfn
,
849 .instance_finalize
= qbus_finalize
,
850 .class_init
= bus_class_init
,
853 static void qdev_register_types(void)
855 type_register_static(&bus_info
);
856 type_register_static(&device_type_info
);
859 type_init(qdev_register_types
)