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
33 static bool qdev_hot_added
= false;
34 static bool qdev_hot_removed
= false;
36 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
37 static BusState
*main_system_bus
;
38 static void main_system_bus_create(void);
40 /* Register a new device type. */
41 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
)
43 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
47 BusInfo
*qdev_get_bus_info(DeviceState
*dev
)
49 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
53 Property
*qdev_get_props(DeviceState
*dev
)
55 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
59 const char *qdev_fw_name(DeviceState
*dev
)
61 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
67 return object_get_typename(OBJECT(dev
));
70 bool qdev_exists(const char *name
)
72 return !!object_class_by_name(name
);
75 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
78 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
)
83 assert(bus
->allow_hotplug
);
86 dev
->parent_bus
= bus
;
87 QTAILQ_INSERT_HEAD(&bus
->children
, dev
, sibling
);
89 for (prop
= qdev_get_bus_info(dev
)->props
; prop
&& prop
->name
; prop
++) {
90 qdev_property_add_legacy(dev
, prop
, NULL
);
91 qdev_property_add_static(dev
, prop
, NULL
);
93 qdev_prop_set_defaults(dev
, dev
->parent_bus
->info
->props
);
96 /* Create a new device. This only initializes the device state structure
97 and allows properties to be set. qdev_init should be called to
98 initialize the actual device emulation. */
99 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
103 dev
= qdev_try_create(bus
, name
);
106 hw_error("Unknown device '%s' for bus '%s'\n", name
,
109 hw_error("Unknown device '%s' for default sysbus\n", name
);
116 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
)
120 dev
= DEVICE(object_new(name
));
126 bus
= sysbus_get_default();
129 qdev_set_parent_bus(dev
, bus
);
130 qdev_prop_set_globals(dev
);
135 /* Initialize a device. Device properties should be set before calling
136 this function. IRQs and MMIO regions should be connected/mapped after
137 calling this function.
138 On failure, destroy the device and return negative value.
139 Return 0 on success. */
140 int qdev_init(DeviceState
*dev
)
142 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
145 assert(dev
->state
== DEV_STATE_CREATED
);
152 if (qdev_get_vmsd(dev
)) {
153 vmstate_register_with_alias_id(dev
, -1, qdev_get_vmsd(dev
), dev
,
154 dev
->instance_id_alias
,
155 dev
->alias_required_for_version
);
157 dev
->state
= DEV_STATE_INITIALIZED
;
158 if (dev
->hotplugged
) {
164 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
165 int required_for_version
)
167 assert(dev
->state
== DEV_STATE_CREATED
);
168 dev
->instance_id_alias
= alias_id
;
169 dev
->alias_required_for_version
= required_for_version
;
172 int qdev_unplug(DeviceState
*dev
)
174 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
176 if (!dev
->parent_bus
->allow_hotplug
) {
177 qerror_report(QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
180 assert(dc
->unplug
!= NULL
);
182 qdev_hot_removed
= true;
184 return dc
->unplug(dev
);
187 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
194 BusState
*sysbus_get_default(void)
196 if (!main_system_bus
) {
197 main_system_bus_create();
199 return main_system_bus
;
202 static int qbus_reset_one(BusState
*bus
, void *opaque
)
204 if (bus
->info
->reset
) {
205 return bus
->info
->reset(bus
);
210 void qdev_reset_all(DeviceState
*dev
)
212 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
215 void qbus_reset_all_fn(void *opaque
)
217 BusState
*bus
= opaque
;
218 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
221 /* can be used as ->unplug() callback for the simple cases */
222 int qdev_simple_unplug_cb(DeviceState
*dev
)
225 object_unparent(OBJECT(dev
));
231 /* Like qdev_init(), but terminate program via error_report() instead of
232 returning an error value. This is okay during machine creation.
233 Don't use for hotplug, because there callers need to recover from
234 failure. Exception: if you know the device's init() callback can't
235 fail, then qdev_init_nofail() can't fail either, and is therefore
236 usable even then. But relying on the device implementation that
237 way is somewhat unclean, and best avoided. */
238 void qdev_init_nofail(DeviceState
*dev
)
240 if (qdev_init(dev
) < 0) {
241 error_report("Initialization of device %s failed",
242 object_get_typename(OBJECT(dev
)));
247 /* Unlink device from bus and free the structure. */
248 void qdev_free(DeviceState
*dev
)
250 object_delete(OBJECT(dev
));
253 void qdev_machine_creation_done(void)
256 * ok, initial machine setup is done, starting from now we can
257 * only create hotpluggable devices
262 bool qdev_machine_modified(void)
264 return qdev_hot_added
|| qdev_hot_removed
;
267 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
269 return dev
->parent_bus
;
272 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
274 assert(dev
->num_gpio_in
== 0);
275 dev
->num_gpio_in
= n
;
276 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
279 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
281 assert(dev
->num_gpio_out
== 0);
282 dev
->num_gpio_out
= n
;
283 dev
->gpio_out
= pins
;
286 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
288 assert(n
>= 0 && n
< dev
->num_gpio_in
);
289 return dev
->gpio_in
[n
];
292 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
294 assert(n
>= 0 && n
< dev
->num_gpio_out
);
295 dev
->gpio_out
[n
] = pin
;
298 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
300 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
302 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
304 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
305 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
306 qdev_prop_exists(dev
, "vectors")) {
307 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
309 nd
->instantiated
= 1;
312 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
316 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
317 if (strcmp(name
, bus
->name
) == 0) {
324 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
325 qbus_walkerfn
*busfn
, void *opaque
)
331 err
= busfn(bus
, opaque
);
337 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
338 err
= qdev_walk_children(dev
, devfn
, busfn
, opaque
);
347 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
348 qbus_walkerfn
*busfn
, void *opaque
)
354 err
= devfn(dev
, opaque
);
360 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
361 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
370 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
372 DeviceState
*dev
, *ret
;
375 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
376 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
378 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
379 ret
= qdev_find_recursive(child
, id
);
388 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
389 DeviceState
*parent
, const char *name
)
395 bus
->parent
= parent
;
398 /* use supplied name */
399 bus
->name
= g_strdup(name
);
400 } else if (parent
&& parent
->id
) {
401 /* parent device has id -> use it for bus name */
402 len
= strlen(parent
->id
) + 16;
404 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
407 /* no id -> use lowercase bus type for bus name */
408 len
= strlen(info
->name
) + 16;
410 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
411 parent
? parent
->num_child_bus
: 0);
412 for (i
= 0; i
< len
; i
++)
413 buf
[i
] = qemu_tolower(buf
[i
]);
417 QTAILQ_INIT(&bus
->children
);
419 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
420 parent
->num_child_bus
++;
421 } else if (bus
!= main_system_bus
) {
422 /* TODO: once all bus devices are qdevified,
423 only reset handler for main_system_bus should be registered here. */
424 qemu_register_reset(qbus_reset_all_fn
, bus
);
428 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
432 bus
= g_malloc0(info
->size
);
433 bus
->qdev_allocated
= 1;
434 qbus_create_inplace(bus
, info
, parent
, name
);
438 static void main_system_bus_create(void)
440 /* assign main_system_bus before qbus_create_inplace()
441 * in order to make "if (bus != main_system_bus)" work */
442 main_system_bus
= g_malloc0(system_bus_info
.size
);
443 main_system_bus
->qdev_allocated
= 1;
444 qbus_create_inplace(main_system_bus
, &system_bus_info
, NULL
,
448 void qbus_free(BusState
*bus
)
452 while ((dev
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
456 QLIST_REMOVE(bus
, sibling
);
457 bus
->parent
->num_child_bus
--;
459 assert(bus
!= main_system_bus
); /* main_system_bus is never freed */
460 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
462 g_free((void*)bus
->name
);
463 if (bus
->qdev_allocated
) {
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 if (dev
->parent_bus
->info
->get_fw_dev_path
) {
476 d
= dev
->parent_bus
->info
->get_fw_dev_path(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 static char *qdev_get_type(Object
*obj
, Error
**errp
)
502 return g_strdup(object_get_typename(obj
));
506 * Legacy property handling
509 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
510 const char *name
, Error
**errp
)
512 DeviceState
*dev
= DEVICE(obj
);
513 Property
*prop
= opaque
;
518 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
519 visit_type_str(v
, &ptr
, name
, errp
);
522 static void qdev_set_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
523 const char *name
, Error
**errp
)
525 DeviceState
*dev
= DEVICE(obj
);
526 Property
*prop
= opaque
;
527 Error
*local_err
= NULL
;
531 if (dev
->state
!= DEV_STATE_CREATED
) {
532 error_set(errp
, QERR_PERMISSION_DENIED
);
536 visit_type_str(v
, &ptr
, name
, &local_err
);
538 error_propagate(errp
, local_err
);
542 ret
= prop
->info
->parse(dev
, prop
, ptr
);
543 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
548 * @qdev_add_legacy_property - adds a legacy property
550 * Do not use this is new code! Properties added through this interface will
551 * be given names and types in the "legacy" namespace.
553 * Legacy properties are string versions of other OOM properties. The format
554 * of the string depends on the property type.
556 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
561 if (!prop
->info
->print
&& !prop
->info
->parse
) {
564 name
= g_strdup_printf("legacy-%s", prop
->name
);
565 type
= g_strdup_printf("legacy<%s>",
566 prop
->info
->legacy_name
?: prop
->info
->name
);
568 object_property_add(OBJECT(dev
), name
, type
,
569 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
570 prop
->info
->parse
? qdev_set_legacy_property
: prop
->info
->set
,
579 * @qdev_property_add_static - add a @Property to a device.
581 * Static properties access data in a struct. The actual type of the
582 * property and the field depends on the property type.
584 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
588 * TODO qdev_prop_ptr does not have getters or setters. It must
589 * go now that it can be replaced with links. The test should be
590 * removed along with it: all static properties are read/write.
592 if (!prop
->info
->get
&& !prop
->info
->set
) {
596 object_property_add(OBJECT(dev
), prop
->name
, prop
->info
->name
,
597 prop
->info
->get
, prop
->info
->set
,
602 static void device_initfn(Object
*obj
)
604 DeviceState
*dev
= DEVICE(obj
);
609 qdev_hot_added
= true;
612 dev
->instance_id_alias
= -1;
613 dev
->state
= DEV_STATE_CREATED
;
615 for (prop
= qdev_get_props(dev
); prop
&& prop
->name
; prop
++) {
616 qdev_property_add_legacy(dev
, prop
, NULL
);
617 qdev_property_add_static(dev
, prop
, NULL
);
620 object_property_add_str(OBJECT(dev
), "type", qdev_get_type
, NULL
, NULL
);
621 qdev_prop_set_defaults(dev
, qdev_get_props(dev
));
624 /* Unlink device from bus and free the structure. */
625 static void device_finalize(Object
*obj
)
627 DeviceState
*dev
= DEVICE(obj
);
629 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
631 if (dev
->state
== DEV_STATE_INITIALIZED
) {
632 while (dev
->num_child_bus
) {
633 bus
= QLIST_FIRST(&dev
->child_bus
);
636 if (qdev_get_vmsd(dev
)) {
637 vmstate_unregister(dev
, qdev_get_vmsd(dev
), dev
);
643 qemu_opts_del(dev
->opts
);
646 QTAILQ_REMOVE(&dev
->parent_bus
->children
, dev
, sibling
);
649 void device_reset(DeviceState
*dev
)
651 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
658 static TypeInfo device_type_info
= {
660 .parent
= TYPE_OBJECT
,
661 .instance_size
= sizeof(DeviceState
),
662 .instance_init
= device_initfn
,
663 .instance_finalize
= device_finalize
,
665 .class_size
= sizeof(DeviceClass
),
668 static void init_qdev(void)
670 type_register_static(&device_type_info
);
673 device_init(init_qdev
);