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
34 static bool qdev_hot_added
= false;
35 static bool qdev_hot_removed
= false;
37 /* Register a new device type. */
38 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
)
40 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
44 const char *qdev_fw_name(DeviceState
*dev
)
46 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
52 return object_get_typename(OBJECT(dev
));
55 bool qdev_exists(const char *name
)
57 return !!object_class_by_name(name
);
60 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
63 static void bus_remove_child(BusState
*bus
, DeviceState
*child
)
67 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
68 if (kid
->child
== child
) {
71 snprintf(name
, sizeof(name
), "child[%d]", kid
->index
);
72 QTAILQ_REMOVE(&bus
->children
, kid
, sibling
);
73 object_property_del(OBJECT(bus
), name
, NULL
);
80 static void bus_add_child(BusState
*bus
, DeviceState
*child
)
83 BusChild
*kid
= g_malloc0(sizeof(*kid
));
86 assert(bus
->allow_hotplug
);
89 kid
->index
= bus
->max_index
++;
92 QTAILQ_INSERT_HEAD(&bus
->children
, kid
, sibling
);
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
,
101 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
)
103 dev
->parent_bus
= 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 hw_error("Unknown device '%s' for bus '%s'\n", name
,
118 object_get_typename(OBJECT(bus
)));
120 hw_error("Unknown device '%s' for default sysbus\n", name
);
127 DeviceState
*qdev_try_create(BusState
*bus
, const char *type
)
131 if (object_class_by_name(type
) == NULL
) {
134 dev
= DEVICE(object_new(type
));
140 bus
= sysbus_get_default();
143 qdev_set_parent_bus(dev
, bus
);
148 /* Initialize a device. Device properties should be set before calling
149 this function. IRQs and MMIO regions should be connected/mapped after
150 calling this function.
151 On failure, destroy the device and return negative value.
152 Return 0 on success. */
153 int qdev_init(DeviceState
*dev
)
155 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
158 assert(dev
->state
== DEV_STATE_CREATED
);
162 object_unparent(OBJECT(dev
));
167 if (!OBJECT(dev
)->parent
) {
168 static int unattached_count
= 0;
169 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
171 object_property_add_child(container_get(qdev_get_machine(),
173 name
, OBJECT(dev
), NULL
);
177 if (qdev_get_vmsd(dev
)) {
178 vmstate_register_with_alias_id(dev
, -1, qdev_get_vmsd(dev
), dev
,
179 dev
->instance_id_alias
,
180 dev
->alias_required_for_version
);
182 dev
->state
= DEV_STATE_INITIALIZED
;
183 if (dev
->hotplugged
) {
189 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
190 int required_for_version
)
192 assert(dev
->state
== DEV_STATE_CREATED
);
193 dev
->instance_id_alias
= alias_id
;
194 dev
->alias_required_for_version
= required_for_version
;
197 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
199 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
201 if (!dev
->parent_bus
->allow_hotplug
) {
202 error_set(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
205 assert(dc
->unplug
!= NULL
);
207 qdev_hot_removed
= true;
209 if (dc
->unplug(dev
) < 0) {
210 error_set(errp
, QERR_UNDEFINED_ERROR
);
215 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
222 static int qbus_reset_one(BusState
*bus
, void *opaque
)
224 BusClass
*bc
= BUS_GET_CLASS(bus
);
226 return bc
->reset(bus
);
231 void qdev_reset_all(DeviceState
*dev
)
233 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
236 void qbus_reset_all_fn(void *opaque
)
238 BusState
*bus
= opaque
;
239 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
242 /* can be used as ->unplug() callback for the simple cases */
243 int qdev_simple_unplug_cb(DeviceState
*dev
)
246 object_unparent(OBJECT(dev
));
252 /* Like qdev_init(), but terminate program via error_report() instead of
253 returning an error value. This is okay during machine creation.
254 Don't use for hotplug, because there callers need to recover from
255 failure. Exception: if you know the device's init() callback can't
256 fail, then qdev_init_nofail() can't fail either, and is therefore
257 usable even then. But relying on the device implementation that
258 way is somewhat unclean, and best avoided. */
259 void qdev_init_nofail(DeviceState
*dev
)
261 const char *typename
= object_get_typename(OBJECT(dev
));
263 if (qdev_init(dev
) < 0) {
264 error_report("Initialization of device %s failed", typename
);
269 /* Unlink device from bus and free the structure. */
270 void qdev_free(DeviceState
*dev
)
272 object_delete(OBJECT(dev
));
275 void qdev_machine_creation_done(void)
278 * ok, initial machine setup is done, starting from now we can
279 * only create hotpluggable devices
284 bool qdev_machine_modified(void)
286 return qdev_hot_added
|| qdev_hot_removed
;
289 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
291 return dev
->parent_bus
;
294 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
296 assert(dev
->num_gpio_in
== 0);
297 dev
->num_gpio_in
= n
;
298 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
301 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
303 assert(dev
->num_gpio_out
== 0);
304 dev
->num_gpio_out
= n
;
305 dev
->gpio_out
= pins
;
308 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
310 assert(n
>= 0 && n
< dev
->num_gpio_in
);
311 return dev
->gpio_in
[n
];
314 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
316 assert(n
>= 0 && n
< dev
->num_gpio_out
);
317 dev
->gpio_out
[n
] = pin
;
320 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
322 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
324 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
326 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
327 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
328 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
329 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
331 nd
->instantiated
= 1;
334 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
338 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
339 if (strcmp(name
, bus
->name
) == 0) {
346 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
347 qbus_walkerfn
*busfn
, void *opaque
)
353 err
= busfn(bus
, opaque
);
359 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
360 err
= qdev_walk_children(kid
->child
, devfn
, busfn
, opaque
);
369 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
370 qbus_walkerfn
*busfn
, void *opaque
)
376 err
= devfn(dev
, opaque
);
382 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
383 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
392 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
398 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
399 DeviceState
*dev
= kid
->child
;
401 if (dev
->id
&& strcmp(dev
->id
, id
) == 0) {
405 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
406 ret
= qdev_find_recursive(child
, id
);
415 static void qbus_realize(BusState
*bus
)
417 const char *typename
= object_get_typename(OBJECT(bus
));
422 /* use supplied name */
423 } else if (bus
->parent
&& bus
->parent
->id
) {
424 /* parent device has id -> use it for bus name */
425 len
= strlen(bus
->parent
->id
) + 16;
427 snprintf(buf
, len
, "%s.%d", bus
->parent
->id
, bus
->parent
->num_child_bus
);
430 /* no id -> use lowercase bus type for bus name */
431 len
= strlen(typename
) + 16;
433 len
= snprintf(buf
, len
, "%s.%d", typename
,
434 bus
->parent
? bus
->parent
->num_child_bus
: 0);
435 for (i
= 0; i
< len
; i
++)
436 buf
[i
] = qemu_tolower(buf
[i
]);
441 QLIST_INSERT_HEAD(&bus
->parent
->child_bus
, bus
, sibling
);
442 bus
->parent
->num_child_bus
++;
443 object_property_add_child(OBJECT(bus
->parent
), bus
->name
, OBJECT(bus
), NULL
);
444 } else if (bus
!= sysbus_get_default()) {
445 /* TODO: once all bus devices are qdevified,
446 only reset handler for main_system_bus should be registered here. */
447 qemu_register_reset(qbus_reset_all_fn
, bus
);
451 void qbus_create_inplace(BusState
*bus
, const char *typename
,
452 DeviceState
*parent
, const char *name
)
454 object_initialize(bus
, typename
);
456 bus
->parent
= parent
;
457 bus
->name
= name
? g_strdup(name
) : NULL
;
461 BusState
*qbus_create(const char *typename
, DeviceState
*parent
, const char *name
)
465 bus
= BUS(object_new(typename
));
466 bus
->qom_allocated
= true;
468 bus
->parent
= parent
;
469 bus
->name
= name
? g_strdup(name
) : NULL
;
475 void qbus_free(BusState
*bus
)
477 if (bus
->qom_allocated
) {
478 object_delete(OBJECT(bus
));
480 object_finalize(OBJECT(bus
));
481 if (bus
->glib_allocated
) {
487 static char *bus_get_fw_dev_path(BusState
*bus
, DeviceState
*dev
)
489 BusClass
*bc
= BUS_GET_CLASS(bus
);
491 if (bc
->get_fw_dev_path
) {
492 return bc
->get_fw_dev_path(dev
);
498 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
502 if (dev
&& dev
->parent_bus
) {
504 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
505 d
= bus_get_fw_dev_path(dev
->parent_bus
, dev
);
507 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
510 l
+= snprintf(p
+ l
, size
- l
, "%s", object_get_typename(OBJECT(dev
)));
513 l
+= snprintf(p
+ l
, size
- l
, "/");
518 char* qdev_get_fw_dev_path(DeviceState
*dev
)
523 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
530 char *qdev_get_dev_path(DeviceState
*dev
)
534 if (!dev
|| !dev
->parent_bus
) {
538 bc
= BUS_GET_CLASS(dev
->parent_bus
);
539 if (bc
->get_dev_path
) {
540 return bc
->get_dev_path(dev
);
547 * Legacy property handling
550 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
551 const char *name
, Error
**errp
)
553 DeviceState
*dev
= DEVICE(obj
);
554 Property
*prop
= opaque
;
559 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
560 visit_type_str(v
, &ptr
, name
, errp
);
563 static void qdev_set_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
564 const char *name
, Error
**errp
)
566 DeviceState
*dev
= DEVICE(obj
);
567 Property
*prop
= opaque
;
568 Error
*local_err
= NULL
;
572 if (dev
->state
!= DEV_STATE_CREATED
) {
573 error_set(errp
, QERR_PERMISSION_DENIED
);
577 visit_type_str(v
, &ptr
, name
, &local_err
);
579 error_propagate(errp
, local_err
);
583 ret
= prop
->info
->parse(dev
, prop
, ptr
);
584 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
589 * @qdev_add_legacy_property - adds a legacy property
591 * Do not use this is new code! Properties added through this interface will
592 * be given names and types in the "legacy" namespace.
594 * Legacy properties are string versions of other OOM properties. The format
595 * of the string depends on the property type.
597 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
602 /* Register pointer properties as legacy properties */
603 if (!prop
->info
->print
&& !prop
->info
->parse
&&
604 (prop
->info
->set
|| prop
->info
->get
)) {
608 name
= g_strdup_printf("legacy-%s", prop
->name
);
609 type
= g_strdup_printf("legacy<%s>",
610 prop
->info
->legacy_name
?: prop
->info
->name
);
612 object_property_add(OBJECT(dev
), name
, type
,
613 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
614 prop
->info
->parse
? qdev_set_legacy_property
: prop
->info
->set
,
623 * @qdev_property_add_static - add a @Property to a device.
625 * Static properties access data in a struct. The actual type of the
626 * property and the field depends on the property type.
628 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
631 Error
*local_err
= NULL
;
632 Object
*obj
= OBJECT(dev
);
635 * TODO qdev_prop_ptr does not have getters or setters. It must
636 * go now that it can be replaced with links. The test should be
637 * removed along with it: all static properties are read/write.
639 if (!prop
->info
->get
&& !prop
->info
->set
) {
643 object_property_add(obj
, prop
->name
, prop
->info
->name
,
644 prop
->info
->get
, prop
->info
->set
,
649 error_propagate(errp
, local_err
);
652 if (prop
->qtype
== QTYPE_NONE
) {
656 if (prop
->qtype
== QTYPE_QBOOL
) {
657 object_property_set_bool(obj
, prop
->defval
, prop
->name
, &local_err
);
658 } else if (prop
->info
->enum_table
) {
659 object_property_set_str(obj
, prop
->info
->enum_table
[prop
->defval
],
660 prop
->name
, &local_err
);
661 } else if (prop
->qtype
== QTYPE_QINT
) {
662 object_property_set_int(obj
, prop
->defval
, prop
->name
, &local_err
);
664 assert_no_error(local_err
);
667 static void device_initfn(Object
*obj
)
669 DeviceState
*dev
= DEVICE(obj
);
675 qdev_hot_added
= true;
678 dev
->instance_id_alias
= -1;
679 dev
->state
= DEV_STATE_CREATED
;
681 class = object_get_class(OBJECT(dev
));
683 for (prop
= DEVICE_CLASS(class)->props
; prop
&& prop
->name
; prop
++) {
684 qdev_property_add_legacy(dev
, prop
, NULL
);
685 qdev_property_add_static(dev
, prop
, NULL
);
687 class = object_class_get_parent(class);
688 } while (class != object_class_by_name(TYPE_DEVICE
));
689 qdev_prop_set_globals(dev
);
691 object_property_add_link(OBJECT(dev
), "parent_bus", TYPE_BUS
,
692 (Object
**)&dev
->parent_bus
, NULL
);
695 /* Unlink device from bus and free the structure. */
696 static void device_finalize(Object
*obj
)
698 DeviceState
*dev
= DEVICE(obj
);
700 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
702 if (dev
->state
== DEV_STATE_INITIALIZED
) {
703 while (dev
->num_child_bus
) {
704 bus
= QLIST_FIRST(&dev
->child_bus
);
707 if (qdev_get_vmsd(dev
)) {
708 vmstate_unregister(dev
, qdev_get_vmsd(dev
), dev
);
714 qemu_opts_del(dev
->opts
);
717 if (dev
->parent_bus
) {
718 bus_remove_child(dev
->parent_bus
, dev
);
722 static void device_class_base_init(ObjectClass
*class, void *data
)
724 DeviceClass
*klass
= DEVICE_CLASS(class);
726 /* We explicitly look up properties in the superclasses,
727 * so do not propagate them to the subclasses.
732 void device_reset(DeviceState
*dev
)
734 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
741 Object
*qdev_get_machine(void)
746 dev
= container_get(object_get_root(), "/machine");
752 static TypeInfo device_type_info
= {
754 .parent
= TYPE_OBJECT
,
755 .instance_size
= sizeof(DeviceState
),
756 .instance_init
= device_initfn
,
757 .instance_finalize
= device_finalize
,
758 .class_base_init
= device_class_base_init
,
760 .class_size
= sizeof(DeviceClass
),
763 static void qbus_initfn(Object
*obj
)
765 BusState
*bus
= BUS(obj
);
767 QTAILQ_INIT(&bus
->children
);
770 static void qbus_finalize(Object
*obj
)
772 BusState
*bus
= BUS(obj
);
775 while ((kid
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
776 DeviceState
*dev
= kid
->child
;
780 QLIST_REMOVE(bus
, sibling
);
781 bus
->parent
->num_child_bus
--;
783 assert(bus
!= sysbus_get_default()); /* main_system_bus is never freed */
784 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
786 g_free((char *)bus
->name
);
789 static const TypeInfo bus_info
= {
791 .parent
= TYPE_OBJECT
,
792 .instance_size
= sizeof(BusState
),
794 .class_size
= sizeof(BusClass
),
795 .instance_init
= qbus_initfn
,
796 .instance_finalize
= qbus_finalize
,
799 static void qdev_register_types(void)
801 type_register_static(&bus_info
);
802 type_register_static(&device_type_info
);
805 type_init(qdev_register_types
)