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 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
38 static BusState
*main_system_bus
;
39 static void main_system_bus_create(void);
41 /* Register a new device type. */
42 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
)
44 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
48 BusInfo
*qdev_get_bus_info(DeviceState
*dev
)
50 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
54 Property
*qdev_get_props(DeviceState
*dev
)
56 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
60 const char *qdev_fw_name(DeviceState
*dev
)
62 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
68 return object_get_typename(OBJECT(dev
));
71 bool qdev_exists(const char *name
)
73 return !!object_class_by_name(name
);
76 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
79 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
)
84 assert(bus
->allow_hotplug
);
87 dev
->parent_bus
= bus
;
88 QTAILQ_INSERT_HEAD(&bus
->children
, dev
, sibling
);
90 for (prop
= qdev_get_bus_info(dev
)->props
; prop
&& prop
->name
; prop
++) {
91 qdev_property_add_legacy(dev
, prop
, NULL
);
92 qdev_property_add_static(dev
, prop
, NULL
);
94 qdev_prop_set_defaults(dev
, dev
->parent_bus
->info
->props
);
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. */
100 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
104 dev
= qdev_try_create(bus
, name
);
107 hw_error("Unknown device '%s' for bus '%s'\n", name
,
110 hw_error("Unknown device '%s' for default sysbus\n", name
);
117 DeviceState
*qdev_try_create(BusState
*bus
, const char *type
)
121 if (object_class_by_name(type
) == NULL
) {
124 dev
= DEVICE(object_new(type
));
130 bus
= sysbus_get_default();
133 qdev_set_parent_bus(dev
, bus
);
134 qdev_prop_set_globals(dev
);
139 /* Initialize a device. Device properties should be set before calling
140 this function. IRQs and MMIO regions should be connected/mapped after
141 calling this function.
142 On failure, destroy the device and return negative value.
143 Return 0 on success. */
144 int qdev_init(DeviceState
*dev
)
146 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
149 assert(dev
->state
== DEV_STATE_CREATED
);
157 if (!OBJECT(dev
)->parent
) {
158 static int unattached_count
= 0;
159 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
161 object_property_add_child(container_get("/machine/unattached"), name
,
166 if (qdev_get_vmsd(dev
)) {
167 vmstate_register_with_alias_id(dev
, -1, qdev_get_vmsd(dev
), dev
,
168 dev
->instance_id_alias
,
169 dev
->alias_required_for_version
);
171 dev
->state
= DEV_STATE_INITIALIZED
;
172 if (dev
->hotplugged
) {
178 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
179 int required_for_version
)
181 assert(dev
->state
== DEV_STATE_CREATED
);
182 dev
->instance_id_alias
= alias_id
;
183 dev
->alias_required_for_version
= required_for_version
;
186 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
188 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
190 if (!dev
->parent_bus
->allow_hotplug
) {
191 error_set(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
194 assert(dc
->unplug
!= NULL
);
196 qdev_hot_removed
= true;
198 if (dc
->unplug(dev
) < 0) {
199 error_set(errp
, QERR_UNDEFINED_ERROR
);
204 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
211 BusState
*sysbus_get_default(void)
213 if (!main_system_bus
) {
214 main_system_bus_create();
216 return main_system_bus
;
219 static int qbus_reset_one(BusState
*bus
, void *opaque
)
221 if (bus
->info
->reset
) {
222 return bus
->info
->reset(bus
);
227 void qdev_reset_all(DeviceState
*dev
)
229 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
232 void qbus_reset_all_fn(void *opaque
)
234 BusState
*bus
= opaque
;
235 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
238 /* can be used as ->unplug() callback for the simple cases */
239 int qdev_simple_unplug_cb(DeviceState
*dev
)
242 object_unparent(OBJECT(dev
));
248 /* Like qdev_init(), but terminate program via error_report() instead of
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. */
255 void qdev_init_nofail(DeviceState
*dev
)
257 if (qdev_init(dev
) < 0) {
258 error_report("Initialization of device %s failed",
259 object_get_typename(OBJECT(dev
)));
264 /* Unlink device from bus and free the structure. */
265 void qdev_free(DeviceState
*dev
)
267 object_delete(OBJECT(dev
));
270 void qdev_machine_creation_done(void)
273 * ok, initial machine setup is done, starting from now we can
274 * only create hotpluggable devices
279 bool qdev_machine_modified(void)
281 return qdev_hot_added
|| qdev_hot_removed
;
284 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
286 return dev
->parent_bus
;
289 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
291 assert(dev
->num_gpio_in
== 0);
292 dev
->num_gpio_in
= n
;
293 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
296 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
298 assert(dev
->num_gpio_out
== 0);
299 dev
->num_gpio_out
= n
;
300 dev
->gpio_out
= pins
;
303 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
305 assert(n
>= 0 && n
< dev
->num_gpio_in
);
306 return dev
->gpio_in
[n
];
309 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
311 assert(n
>= 0 && n
< dev
->num_gpio_out
);
312 dev
->gpio_out
[n
] = pin
;
315 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
317 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
319 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
321 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
322 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
323 qdev_prop_exists(dev
, "vectors")) {
324 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
326 nd
->instantiated
= 1;
329 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
333 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
334 if (strcmp(name
, bus
->name
) == 0) {
341 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
342 qbus_walkerfn
*busfn
, void *opaque
)
348 err
= busfn(bus
, opaque
);
354 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
355 err
= qdev_walk_children(dev
, devfn
, busfn
, opaque
);
364 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
365 qbus_walkerfn
*busfn
, void *opaque
)
371 err
= devfn(dev
, opaque
);
377 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
378 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
387 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
389 DeviceState
*dev
, *ret
;
392 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
393 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
395 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
396 ret
= qdev_find_recursive(child
, id
);
405 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
406 DeviceState
*parent
, const char *name
)
412 bus
->parent
= parent
;
415 /* use supplied name */
416 bus
->name
= g_strdup(name
);
417 } else if (parent
&& parent
->id
) {
418 /* parent device has id -> use it for bus name */
419 len
= strlen(parent
->id
) + 16;
421 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
424 /* no id -> use lowercase bus type for bus name */
425 len
= strlen(info
->name
) + 16;
427 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
428 parent
? parent
->num_child_bus
: 0);
429 for (i
= 0; i
< len
; i
++)
430 buf
[i
] = qemu_tolower(buf
[i
]);
434 QTAILQ_INIT(&bus
->children
);
436 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
437 parent
->num_child_bus
++;
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
);
445 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
449 bus
= g_malloc0(info
->size
);
450 bus
->qdev_allocated
= 1;
451 qbus_create_inplace(bus
, info
, parent
, name
);
455 static void main_system_bus_create(void)
457 /* assign main_system_bus before qbus_create_inplace()
458 * in order to make "if (bus != main_system_bus)" work */
459 main_system_bus
= g_malloc0(system_bus_info
.size
);
460 main_system_bus
->qdev_allocated
= 1;
461 qbus_create_inplace(main_system_bus
, &system_bus_info
, NULL
,
465 void qbus_free(BusState
*bus
)
469 while ((dev
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
473 QLIST_REMOVE(bus
, sibling
);
474 bus
->parent
->num_child_bus
--;
476 assert(bus
!= main_system_bus
); /* main_system_bus is never freed */
477 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
479 g_free((void*)bus
->name
);
480 if (bus
->qdev_allocated
) {
485 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
489 if (dev
&& dev
->parent_bus
) {
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
);
497 l
+= snprintf(p
+ l
, size
- l
, "%s", object_get_typename(OBJECT(dev
)));
500 l
+= snprintf(p
+ l
, size
- l
, "/");
505 char* qdev_get_fw_dev_path(DeviceState
*dev
)
510 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
517 static char *qdev_get_type(Object
*obj
, Error
**errp
)
519 return g_strdup(object_get_typename(obj
));
523 * Legacy property handling
526 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
527 const char *name
, Error
**errp
)
529 DeviceState
*dev
= DEVICE(obj
);
530 Property
*prop
= opaque
;
535 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
536 visit_type_str(v
, &ptr
, name
, errp
);
539 static void qdev_set_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
540 const char *name
, Error
**errp
)
542 DeviceState
*dev
= DEVICE(obj
);
543 Property
*prop
= opaque
;
544 Error
*local_err
= NULL
;
548 if (dev
->state
!= DEV_STATE_CREATED
) {
549 error_set(errp
, QERR_PERMISSION_DENIED
);
553 visit_type_str(v
, &ptr
, name
, &local_err
);
555 error_propagate(errp
, local_err
);
559 ret
= prop
->info
->parse(dev
, prop
, ptr
);
560 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
565 * @qdev_add_legacy_property - adds a legacy property
567 * Do not use this is new code! Properties added through this interface will
568 * be given names and types in the "legacy" namespace.
570 * Legacy properties are string versions of other OOM properties. The format
571 * of the string depends on the property type.
573 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
578 if (!prop
->info
->print
&& !prop
->info
->parse
) {
581 name
= g_strdup_printf("legacy-%s", prop
->name
);
582 type
= g_strdup_printf("legacy<%s>",
583 prop
->info
->legacy_name
?: prop
->info
->name
);
585 object_property_add(OBJECT(dev
), name
, type
,
586 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
587 prop
->info
->parse
? qdev_set_legacy_property
: prop
->info
->set
,
596 * @qdev_property_add_static - add a @Property to a device.
598 * Static properties access data in a struct. The actual type of the
599 * property and the field depends on the property type.
601 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
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(OBJECT(dev
), prop
->name
, prop
->info
->name
,
614 prop
->info
->get
, prop
->info
->set
,
619 static void device_initfn(Object
*obj
)
621 DeviceState
*dev
= DEVICE(obj
);
626 qdev_hot_added
= true;
629 dev
->instance_id_alias
= -1;
630 dev
->state
= DEV_STATE_CREATED
;
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
);
637 object_property_add_str(OBJECT(dev
), "type", qdev_get_type
, NULL
, NULL
);
638 qdev_prop_set_defaults(dev
, qdev_get_props(dev
));
641 /* Unlink device from bus and free the structure. */
642 static void device_finalize(Object
*obj
)
644 DeviceState
*dev
= DEVICE(obj
);
646 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
648 if (dev
->state
== DEV_STATE_INITIALIZED
) {
649 while (dev
->num_child_bus
) {
650 bus
= QLIST_FIRST(&dev
->child_bus
);
653 if (qdev_get_vmsd(dev
)) {
654 vmstate_unregister(dev
, qdev_get_vmsd(dev
), dev
);
660 qemu_opts_del(dev
->opts
);
663 QTAILQ_REMOVE(&dev
->parent_bus
->children
, dev
, sibling
);
666 void device_reset(DeviceState
*dev
)
668 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
675 Object
*qdev_get_machine(void)
680 dev
= container_get("/machine");
686 static TypeInfo device_type_info
= {
688 .parent
= TYPE_OBJECT
,
689 .instance_size
= sizeof(DeviceState
),
690 .instance_init
= device_initfn
,
691 .instance_finalize
= device_finalize
,
693 .class_size
= sizeof(DeviceClass
),
696 static void qdev_register_types(void)
698 type_register_static(&device_type_info
);
701 type_init(qdev_register_types
)