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 const char *qdev_fw_name(DeviceState
*dev
)
50 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
56 return object_get_typename(OBJECT(dev
));
59 bool qdev_exists(const char *name
)
61 return !!object_class_by_name(name
);
64 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
67 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
)
70 assert(bus
->allow_hotplug
);
73 dev
->parent_bus
= bus
;
74 QTAILQ_INSERT_HEAD(&bus
->children
, dev
, sibling
);
77 /* Create a new device. This only initializes the device state structure
78 and allows properties to be set. qdev_init should be called to
79 initialize the actual device emulation. */
80 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
84 dev
= qdev_try_create(bus
, name
);
87 hw_error("Unknown device '%s' for bus '%s'\n", name
,
90 hw_error("Unknown device '%s' for default sysbus\n", name
);
97 DeviceState
*qdev_try_create(BusState
*bus
, const char *type
)
101 if (object_class_by_name(type
) == NULL
) {
104 dev
= DEVICE(object_new(type
));
110 bus
= sysbus_get_default();
113 qdev_set_parent_bus(dev
, bus
);
118 /* Initialize a device. Device properties should be set before calling
119 this function. IRQs and MMIO regions should be connected/mapped after
120 calling this function.
121 On failure, destroy the device and return negative value.
122 Return 0 on success. */
123 int qdev_init(DeviceState
*dev
)
125 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
128 assert(dev
->state
== DEV_STATE_CREATED
);
136 if (!OBJECT(dev
)->parent
) {
137 static int unattached_count
= 0;
138 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
140 object_property_add_child(container_get(qdev_get_machine(),
142 name
, OBJECT(dev
), NULL
);
146 if (qdev_get_vmsd(dev
)) {
147 vmstate_register_with_alias_id(dev
, -1, qdev_get_vmsd(dev
), dev
,
148 dev
->instance_id_alias
,
149 dev
->alias_required_for_version
);
151 dev
->state
= DEV_STATE_INITIALIZED
;
152 if (dev
->hotplugged
) {
158 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
159 int required_for_version
)
161 assert(dev
->state
== DEV_STATE_CREATED
);
162 dev
->instance_id_alias
= alias_id
;
163 dev
->alias_required_for_version
= required_for_version
;
166 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
168 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
170 if (!dev
->parent_bus
->allow_hotplug
) {
171 error_set(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
174 assert(dc
->unplug
!= NULL
);
176 qdev_hot_removed
= true;
178 if (dc
->unplug(dev
) < 0) {
179 error_set(errp
, QERR_UNDEFINED_ERROR
);
184 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
191 BusState
*sysbus_get_default(void)
193 if (!main_system_bus
) {
194 main_system_bus_create();
196 return main_system_bus
;
199 static int qbus_reset_one(BusState
*bus
, void *opaque
)
201 if (bus
->info
->reset
) {
202 return bus
->info
->reset(bus
);
207 void qdev_reset_all(DeviceState
*dev
)
209 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
212 void qbus_reset_all_fn(void *opaque
)
214 BusState
*bus
= opaque
;
215 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
218 /* can be used as ->unplug() callback for the simple cases */
219 int qdev_simple_unplug_cb(DeviceState
*dev
)
222 object_unparent(OBJECT(dev
));
228 /* Like qdev_init(), but terminate program via error_report() instead of
229 returning an error value. This is okay during machine creation.
230 Don't use for hotplug, because there callers need to recover from
231 failure. Exception: if you know the device's init() callback can't
232 fail, then qdev_init_nofail() can't fail either, and is therefore
233 usable even then. But relying on the device implementation that
234 way is somewhat unclean, and best avoided. */
235 void qdev_init_nofail(DeviceState
*dev
)
237 if (qdev_init(dev
) < 0) {
238 error_report("Initialization of device %s failed",
239 object_get_typename(OBJECT(dev
)));
244 /* Unlink device from bus and free the structure. */
245 void qdev_free(DeviceState
*dev
)
247 object_delete(OBJECT(dev
));
250 void qdev_machine_creation_done(void)
253 * ok, initial machine setup is done, starting from now we can
254 * only create hotpluggable devices
259 bool qdev_machine_modified(void)
261 return qdev_hot_added
|| qdev_hot_removed
;
264 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
266 return dev
->parent_bus
;
269 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
271 assert(dev
->num_gpio_in
== 0);
272 dev
->num_gpio_in
= n
;
273 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
276 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
278 assert(dev
->num_gpio_out
== 0);
279 dev
->num_gpio_out
= n
;
280 dev
->gpio_out
= pins
;
283 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
285 assert(n
>= 0 && n
< dev
->num_gpio_in
);
286 return dev
->gpio_in
[n
];
289 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
291 assert(n
>= 0 && n
< dev
->num_gpio_out
);
292 dev
->gpio_out
[n
] = pin
;
295 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
297 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
299 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
301 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
302 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
303 qdev_prop_exists(dev
, "vectors")) {
304 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
306 nd
->instantiated
= 1;
309 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
313 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
314 if (strcmp(name
, bus
->name
) == 0) {
321 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
322 qbus_walkerfn
*busfn
, void *opaque
)
328 err
= busfn(bus
, opaque
);
334 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
335 err
= qdev_walk_children(dev
, devfn
, busfn
, opaque
);
344 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
345 qbus_walkerfn
*busfn
, void *opaque
)
351 err
= devfn(dev
, opaque
);
357 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
358 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
367 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
369 DeviceState
*dev
, *ret
;
372 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
373 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
375 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
376 ret
= qdev_find_recursive(child
, id
);
385 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
386 DeviceState
*parent
, const char *name
)
392 bus
->parent
= parent
;
395 /* use supplied name */
396 bus
->name
= g_strdup(name
);
397 } else if (parent
&& parent
->id
) {
398 /* parent device has id -> use it for bus name */
399 len
= strlen(parent
->id
) + 16;
401 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
404 /* no id -> use lowercase bus type for bus name */
405 len
= strlen(info
->name
) + 16;
407 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
408 parent
? parent
->num_child_bus
: 0);
409 for (i
= 0; i
< len
; i
++)
410 buf
[i
] = qemu_tolower(buf
[i
]);
414 QTAILQ_INIT(&bus
->children
);
416 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
417 parent
->num_child_bus
++;
418 } else if (bus
!= main_system_bus
) {
419 /* TODO: once all bus devices are qdevified,
420 only reset handler for main_system_bus should be registered here. */
421 qemu_register_reset(qbus_reset_all_fn
, bus
);
425 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
429 bus
= g_malloc0(info
->size
);
430 bus
->qdev_allocated
= 1;
431 qbus_create_inplace(bus
, info
, parent
, name
);
435 static void main_system_bus_create(void)
437 /* assign main_system_bus before qbus_create_inplace()
438 * in order to make "if (bus != main_system_bus)" work */
439 main_system_bus
= g_malloc0(system_bus_info
.size
);
440 main_system_bus
->qdev_allocated
= 1;
441 qbus_create_inplace(main_system_bus
, &system_bus_info
, NULL
,
445 void qbus_free(BusState
*bus
)
449 while ((dev
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
453 QLIST_REMOVE(bus
, sibling
);
454 bus
->parent
->num_child_bus
--;
456 assert(bus
!= main_system_bus
); /* main_system_bus is never freed */
457 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
459 g_free((void*)bus
->name
);
460 if (bus
->qdev_allocated
) {
465 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
469 if (dev
&& dev
->parent_bus
) {
471 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
472 if (dev
->parent_bus
->info
->get_fw_dev_path
) {
473 d
= dev
->parent_bus
->info
->get_fw_dev_path(dev
);
474 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
477 l
+= snprintf(p
+ l
, size
- l
, "%s", object_get_typename(OBJECT(dev
)));
480 l
+= snprintf(p
+ l
, size
- l
, "/");
485 char* qdev_get_fw_dev_path(DeviceState
*dev
)
490 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
498 * Legacy property handling
501 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
502 const char *name
, Error
**errp
)
504 DeviceState
*dev
= DEVICE(obj
);
505 Property
*prop
= opaque
;
510 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
511 visit_type_str(v
, &ptr
, name
, errp
);
514 static void qdev_set_legacy_property(Object
*obj
, Visitor
*v
, void *opaque
,
515 const char *name
, Error
**errp
)
517 DeviceState
*dev
= DEVICE(obj
);
518 Property
*prop
= opaque
;
519 Error
*local_err
= NULL
;
523 if (dev
->state
!= DEV_STATE_CREATED
) {
524 error_set(errp
, QERR_PERMISSION_DENIED
);
528 visit_type_str(v
, &ptr
, name
, &local_err
);
530 error_propagate(errp
, local_err
);
534 ret
= prop
->info
->parse(dev
, prop
, ptr
);
535 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
540 * @qdev_add_legacy_property - adds a legacy property
542 * Do not use this is new code! Properties added through this interface will
543 * be given names and types in the "legacy" namespace.
545 * Legacy properties are string versions of other OOM properties. The format
546 * of the string depends on the property type.
548 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
553 /* Register pointer properties as legacy properties */
554 if (!prop
->info
->print
&& !prop
->info
->parse
&&
555 (prop
->info
->set
|| prop
->info
->get
)) {
559 name
= g_strdup_printf("legacy-%s", prop
->name
);
560 type
= g_strdup_printf("legacy<%s>",
561 prop
->info
->legacy_name
?: prop
->info
->name
);
563 object_property_add(OBJECT(dev
), name
, type
,
564 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
565 prop
->info
->parse
? qdev_set_legacy_property
: prop
->info
->set
,
574 * @qdev_property_add_static - add a @Property to a device.
576 * Static properties access data in a struct. The actual type of the
577 * property and the field depends on the property type.
579 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
583 * TODO qdev_prop_ptr does not have getters or setters. It must
584 * go now that it can be replaced with links. The test should be
585 * removed along with it: all static properties are read/write.
587 if (!prop
->info
->get
&& !prop
->info
->set
) {
591 object_property_add(OBJECT(dev
), prop
->name
, prop
->info
->name
,
592 prop
->info
->get
, prop
->info
->set
,
597 static void device_initfn(Object
*obj
)
599 DeviceState
*dev
= DEVICE(obj
);
605 qdev_hot_added
= true;
608 dev
->instance_id_alias
= -1;
609 dev
->state
= DEV_STATE_CREATED
;
611 class = object_get_class(OBJECT(dev
));
613 for (prop
= DEVICE_CLASS(class)->props
; prop
&& prop
->name
; prop
++) {
614 qdev_property_add_legacy(dev
, prop
, NULL
);
615 qdev_property_add_static(dev
, prop
, NULL
);
617 qdev_prop_set_defaults(dev
, DEVICE_CLASS(class)->props
);
618 class = object_class_get_parent(class);
619 } while (class != object_class_by_name(TYPE_DEVICE
));
620 qdev_prop_set_globals(dev
);
623 /* Unlink device from bus and free the structure. */
624 static void device_finalize(Object
*obj
)
626 DeviceState
*dev
= DEVICE(obj
);
628 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
630 if (dev
->state
== DEV_STATE_INITIALIZED
) {
631 while (dev
->num_child_bus
) {
632 bus
= QLIST_FIRST(&dev
->child_bus
);
635 if (qdev_get_vmsd(dev
)) {
636 vmstate_unregister(dev
, qdev_get_vmsd(dev
), dev
);
642 qemu_opts_del(dev
->opts
);
645 QTAILQ_REMOVE(&dev
->parent_bus
->children
, dev
, sibling
);
648 static void device_class_base_init(ObjectClass
*class, void *data
)
650 DeviceClass
*klass
= DEVICE_CLASS(class);
652 /* We explicitly look up properties in the superclasses,
653 * so do not propagate them to the subclasses.
658 void device_reset(DeviceState
*dev
)
660 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
667 Object
*qdev_get_machine(void)
672 dev
= container_get(object_get_root(), "/machine");
678 static TypeInfo device_type_info
= {
680 .parent
= TYPE_OBJECT
,
681 .instance_size
= sizeof(DeviceState
),
682 .instance_init
= device_initfn
,
683 .instance_finalize
= device_finalize
,
684 .class_base_init
= device_class_base_init
,
686 .class_size
= sizeof(DeviceClass
),
689 static void qdev_register_types(void)
691 type_register_static(&device_type_info
);
694 type_init(qdev_register_types
)