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 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
)
66 assert(bus
->allow_hotplug
);
69 dev
->parent_bus
= bus
;
70 QTAILQ_INSERT_HEAD(&bus
->children
, dev
, sibling
);
73 /* Create a new device. This only initializes the device state structure
74 and allows properties to be set. qdev_init should be called to
75 initialize the actual device emulation. */
76 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
80 dev
= qdev_try_create(bus
, name
);
83 hw_error("Unknown device '%s' for bus '%s'\n", name
,
84 object_get_typename(OBJECT(bus
)));
86 hw_error("Unknown device '%s' for default sysbus\n", name
);
93 DeviceState
*qdev_try_create(BusState
*bus
, const char *type
)
97 if (object_class_by_name(type
) == NULL
) {
100 dev
= DEVICE(object_new(type
));
106 bus
= sysbus_get_default();
109 qdev_set_parent_bus(dev
, bus
);
114 /* Initialize a device. Device properties should be set before calling
115 this function. IRQs and MMIO regions should be connected/mapped after
116 calling this function.
117 On failure, destroy the device and return negative value.
118 Return 0 on success. */
119 int qdev_init(DeviceState
*dev
)
121 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
124 assert(dev
->state
== DEV_STATE_CREATED
);
132 if (!OBJECT(dev
)->parent
) {
133 static int unattached_count
= 0;
134 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
136 object_property_add_child(container_get(qdev_get_machine(),
138 name
, OBJECT(dev
), NULL
);
142 if (qdev_get_vmsd(dev
)) {
143 vmstate_register_with_alias_id(dev
, -1, qdev_get_vmsd(dev
), dev
,
144 dev
->instance_id_alias
,
145 dev
->alias_required_for_version
);
147 dev
->state
= DEV_STATE_INITIALIZED
;
148 if (dev
->hotplugged
) {
154 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
155 int required_for_version
)
157 assert(dev
->state
== DEV_STATE_CREATED
);
158 dev
->instance_id_alias
= alias_id
;
159 dev
->alias_required_for_version
= required_for_version
;
162 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
164 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
166 if (!dev
->parent_bus
->allow_hotplug
) {
167 error_set(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
170 assert(dc
->unplug
!= NULL
);
172 qdev_hot_removed
= true;
174 if (dc
->unplug(dev
) < 0) {
175 error_set(errp
, QERR_UNDEFINED_ERROR
);
180 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
187 static int qbus_reset_one(BusState
*bus
, void *opaque
)
189 BusClass
*bc
= BUS_GET_CLASS(bus
);
191 return bc
->reset(bus
);
196 void qdev_reset_all(DeviceState
*dev
)
198 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
201 void qbus_reset_all_fn(void *opaque
)
203 BusState
*bus
= opaque
;
204 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
207 /* can be used as ->unplug() callback for the simple cases */
208 int qdev_simple_unplug_cb(DeviceState
*dev
)
211 object_unparent(OBJECT(dev
));
217 /* Like qdev_init(), but terminate program via error_report() instead of
218 returning an error value. This is okay during machine creation.
219 Don't use for hotplug, because there callers need to recover from
220 failure. Exception: if you know the device's init() callback can't
221 fail, then qdev_init_nofail() can't fail either, and is therefore
222 usable even then. But relying on the device implementation that
223 way is somewhat unclean, and best avoided. */
224 void qdev_init_nofail(DeviceState
*dev
)
226 if (qdev_init(dev
) < 0) {
227 error_report("Initialization of device %s failed",
228 object_get_typename(OBJECT(dev
)));
233 /* Unlink device from bus and free the structure. */
234 void qdev_free(DeviceState
*dev
)
236 object_delete(OBJECT(dev
));
239 void qdev_machine_creation_done(void)
242 * ok, initial machine setup is done, starting from now we can
243 * only create hotpluggable devices
248 bool qdev_machine_modified(void)
250 return qdev_hot_added
|| qdev_hot_removed
;
253 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
255 return dev
->parent_bus
;
258 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
260 assert(dev
->num_gpio_in
== 0);
261 dev
->num_gpio_in
= n
;
262 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
265 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
267 assert(dev
->num_gpio_out
== 0);
268 dev
->num_gpio_out
= n
;
269 dev
->gpio_out
= pins
;
272 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
274 assert(n
>= 0 && n
< dev
->num_gpio_in
);
275 return dev
->gpio_in
[n
];
278 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
280 assert(n
>= 0 && n
< dev
->num_gpio_out
);
281 dev
->gpio_out
[n
] = pin
;
284 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
286 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
288 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
290 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
291 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
292 qdev_prop_exists(dev
, "vectors")) {
293 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
295 nd
->instantiated
= 1;
298 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
302 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
303 if (strcmp(name
, bus
->name
) == 0) {
310 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
311 qbus_walkerfn
*busfn
, void *opaque
)
317 err
= busfn(bus
, opaque
);
323 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
324 err
= qdev_walk_children(dev
, devfn
, busfn
, opaque
);
333 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
334 qbus_walkerfn
*busfn
, void *opaque
)
340 err
= devfn(dev
, opaque
);
346 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
347 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
356 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
358 DeviceState
*dev
, *ret
;
361 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
362 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
364 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
365 ret
= qdev_find_recursive(child
, id
);
374 /* FIXME move this logic into instance_init */
375 static void do_qbus_create_inplace(BusState
*bus
, const char *typename
,
376 DeviceState
*parent
, const char *name
)
381 bus
->parent
= parent
;
384 /* use supplied name */
385 bus
->name
= g_strdup(name
);
386 } else if (parent
&& parent
->id
) {
387 /* parent device has id -> use it for bus name */
388 len
= strlen(parent
->id
) + 16;
390 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
393 /* no id -> use lowercase bus type for bus name */
394 len
= strlen(typename
) + 16;
396 len
= snprintf(buf
, len
, "%s.%d", typename
,
397 parent
? parent
->num_child_bus
: 0);
398 for (i
= 0; i
< len
; i
++)
399 buf
[i
] = qemu_tolower(buf
[i
]);
403 QTAILQ_INIT(&bus
->children
);
405 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
406 parent
->num_child_bus
++;
407 object_property_add_child(OBJECT(parent
), bus
->name
, OBJECT(bus
), NULL
);
408 } else if (bus
!= sysbus_get_default()) {
409 /* TODO: once all bus devices are qdevified,
410 only reset handler for main_system_bus should be registered here. */
411 qemu_register_reset(qbus_reset_all_fn
, bus
);
415 void qbus_create_inplace(BusState
*bus
, const char *typename
,
416 DeviceState
*parent
, const char *name
)
418 object_initialize(bus
, typename
);
419 do_qbus_create_inplace(bus
, typename
, parent
, name
);
422 BusState
*qbus_create(const char *typename
, DeviceState
*parent
, const char *name
)
426 bus
= BUS(object_new(typename
));
427 bus
->qom_allocated
= true;
428 do_qbus_create_inplace(bus
, typename
, parent
, name
);
432 void qbus_free(BusState
*bus
)
436 while ((dev
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
440 QLIST_REMOVE(bus
, sibling
);
441 bus
->parent
->num_child_bus
--;
443 assert(bus
!= sysbus_get_default()); /* main_system_bus is never freed */
444 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
446 g_free((void*)bus
->name
);
447 if (bus
->qom_allocated
) {
448 object_delete(OBJECT(bus
));
450 object_finalize(OBJECT(bus
));
451 if (bus
->glib_allocated
) {
457 static char *bus_get_fw_dev_path(BusState
*bus
, DeviceState
*dev
)
459 BusClass
*bc
= BUS_GET_CLASS(bus
);
461 if (bc
->get_fw_dev_path
) {
462 return bc
->get_fw_dev_path(dev
);
468 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
472 if (dev
&& dev
->parent_bus
) {
474 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
475 d
= bus_get_fw_dev_path(dev
->parent_bus
, dev
);
477 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
480 l
+= snprintf(p
+ l
, size
- l
, "%s", object_get_typename(OBJECT(dev
)));
483 l
+= snprintf(p
+ l
, size
- l
, "/");
488 char* qdev_get_fw_dev_path(DeviceState
*dev
)
493 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
500 char *qdev_get_dev_path(DeviceState
*dev
)
504 if (!dev
|| !dev
->parent_bus
) {
508 bc
= BUS_GET_CLASS(dev
->parent_bus
);
509 if (bc
->get_dev_path
) {
510 return bc
->get_dev_path(dev
);
517 * Legacy property handling
520 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
521 const char *name
, Error
**errp
)
523 DeviceState
*dev
= DEVICE(obj
);
524 Property
*prop
= opaque
;
529 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
530 visit_type_str(v
, &ptr
, name
, errp
);
533 static void qdev_set_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
534 const char *name
, Error
**errp
)
536 DeviceState
*dev
= DEVICE(obj
);
537 Property
*prop
= opaque
;
538 Error
*local_err
= NULL
;
542 if (dev
->state
!= DEV_STATE_CREATED
) {
543 error_set(errp
, QERR_PERMISSION_DENIED
);
547 visit_type_str(v
, &ptr
, name
, &local_err
);
549 error_propagate(errp
, local_err
);
553 ret
= prop
->info
->parse(dev
, prop
, ptr
);
554 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
559 * @qdev_add_legacy_property - adds a legacy property
561 * Do not use this is new code! Properties added through this interface will
562 * be given names and types in the "legacy" namespace.
564 * Legacy properties are string versions of other OOM properties. The format
565 * of the string depends on the property type.
567 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
572 /* Register pointer properties as legacy properties */
573 if (!prop
->info
->print
&& !prop
->info
->parse
&&
574 (prop
->info
->set
|| prop
->info
->get
)) {
578 name
= g_strdup_printf("legacy-%s", prop
->name
);
579 type
= g_strdup_printf("legacy<%s>",
580 prop
->info
->legacy_name
?: prop
->info
->name
);
582 object_property_add(OBJECT(dev
), name
, type
,
583 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
584 prop
->info
->parse
? qdev_set_legacy_property
: prop
->info
->set
,
593 * @qdev_property_add_static - add a @Property to a device.
595 * Static properties access data in a struct. The actual type of the
596 * property and the field depends on the property type.
598 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
601 Error
*local_err
= NULL
;
602 Object
*obj
= OBJECT(dev
);
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.
609 if (!prop
->info
->get
&& !prop
->info
->set
) {
613 object_property_add(obj
, prop
->name
, prop
->info
->name
,
614 prop
->info
->get
, prop
->info
->set
,
619 error_propagate(errp
, local_err
);
622 if (prop
->qtype
== QTYPE_NONE
) {
626 if (prop
->qtype
== QTYPE_QBOOL
) {
627 object_property_set_bool(obj
, prop
->defval
, prop
->name
, &local_err
);
628 } else if (prop
->info
->enum_table
) {
629 object_property_set_str(obj
, prop
->info
->enum_table
[prop
->defval
],
630 prop
->name
, &local_err
);
631 } else if (prop
->qtype
== QTYPE_QINT
) {
632 object_property_set_int(obj
, prop
->defval
, prop
->name
, &local_err
);
634 assert_no_error(local_err
);
637 static void device_initfn(Object
*obj
)
639 DeviceState
*dev
= DEVICE(obj
);
645 qdev_hot_added
= true;
648 dev
->instance_id_alias
= -1;
649 dev
->state
= DEV_STATE_CREATED
;
651 class = object_get_class(OBJECT(dev
));
653 for (prop
= DEVICE_CLASS(class)->props
; prop
&& prop
->name
; prop
++) {
654 qdev_property_add_legacy(dev
, prop
, NULL
);
655 qdev_property_add_static(dev
, prop
, NULL
);
657 class = object_class_get_parent(class);
658 } while (class != object_class_by_name(TYPE_DEVICE
));
659 qdev_prop_set_globals(dev
);
661 object_property_add_link(OBJECT(dev
), "parent_bus", TYPE_BUS
,
662 (Object
**)&dev
->parent_bus
, NULL
);
665 /* Unlink device from bus and free the structure. */
666 static void device_finalize(Object
*obj
)
668 DeviceState
*dev
= DEVICE(obj
);
670 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
672 if (dev
->state
== DEV_STATE_INITIALIZED
) {
673 while (dev
->num_child_bus
) {
674 bus
= QLIST_FIRST(&dev
->child_bus
);
677 if (qdev_get_vmsd(dev
)) {
678 vmstate_unregister(dev
, qdev_get_vmsd(dev
), dev
);
684 qemu_opts_del(dev
->opts
);
687 QTAILQ_REMOVE(&dev
->parent_bus
->children
, dev
, sibling
);
690 static void device_class_base_init(ObjectClass
*class, void *data
)
692 DeviceClass
*klass
= DEVICE_CLASS(class);
694 /* We explicitly look up properties in the superclasses,
695 * so do not propagate them to the subclasses.
700 void device_reset(DeviceState
*dev
)
702 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
709 Object
*qdev_get_machine(void)
714 dev
= container_get(object_get_root(), "/machine");
720 static TypeInfo device_type_info
= {
722 .parent
= TYPE_OBJECT
,
723 .instance_size
= sizeof(DeviceState
),
724 .instance_init
= device_initfn
,
725 .instance_finalize
= device_finalize
,
726 .class_base_init
= device_class_base_init
,
728 .class_size
= sizeof(DeviceClass
),
731 static const TypeInfo bus_info
= {
733 .parent
= TYPE_OBJECT
,
734 .instance_size
= sizeof(BusState
),
736 .class_size
= sizeof(BusClass
),
739 static void qdev_register_types(void)
741 type_register_static(&bus_info
);
742 type_register_static(&device_type_info
);
745 type_init(qdev_register_types
)