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 int qdev_hotplug
= 0;
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 DeviceInfo
*device_info_list
;
43 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
45 static BusState
*qbus_find(const char *path
);
47 /* Register a new device type. */
48 void qdev_register(DeviceInfo
*info
)
50 assert(info
->size
>= sizeof(DeviceState
));
53 info
->next
= device_info_list
;
54 device_info_list
= info
;
57 static DeviceInfo
*qdev_find_info(BusInfo
*bus_info
, const char *name
)
61 /* first check device names */
62 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
63 if (bus_info
&& info
->bus_info
!= bus_info
)
65 if (strcmp(info
->name
, name
) != 0)
70 /* failing that check the aliases */
71 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
72 if (bus_info
&& info
->bus_info
!= bus_info
)
76 if (strcmp(info
->alias
, name
) != 0)
83 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
86 static DeviceState
*qdev_create_from_info(BusState
*bus
, DeviceInfo
*info
)
91 assert(bus
->info
== info
->bus_info
);
92 dev
= g_malloc0(info
->size
);
94 dev
->parent_bus
= bus
;
95 qdev_prop_set_defaults(dev
, dev
->info
->props
);
96 qdev_prop_set_defaults(dev
, dev
->parent_bus
->info
->props
);
97 qdev_prop_set_globals(dev
);
98 QTAILQ_INSERT_HEAD(&bus
->children
, dev
, sibling
);
100 assert(bus
->allow_hotplug
);
102 qdev_hot_added
= true;
104 dev
->instance_id_alias
= -1;
105 QTAILQ_INIT(&dev
->properties
);
106 dev
->state
= DEV_STATE_CREATED
;
108 for (prop
= dev
->info
->props
; prop
&& prop
->name
; prop
++) {
109 qdev_property_add_legacy(dev
, prop
, NULL
);
110 qdev_property_add_static(dev
, prop
, NULL
);
113 for (prop
= dev
->info
->bus_info
->props
; prop
&& prop
->name
; prop
++) {
114 qdev_property_add_legacy(dev
, prop
, NULL
);
115 qdev_property_add_static(dev
, prop
, NULL
);
118 qdev_property_add_str(dev
, "type", qdev_get_type
, NULL
, NULL
);
123 /* Create a new device. This only initializes the device state structure
124 and allows properties to be set. qdev_init should be called to
125 initialize the actual device emulation. */
126 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
130 dev
= qdev_try_create(bus
, name
);
133 hw_error("Unknown device '%s' for bus '%s'\n", name
,
136 hw_error("Unknown device '%s' for default sysbus\n", name
);
143 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
)
148 bus
= sysbus_get_default();
151 info
= qdev_find_info(bus
->info
, name
);
156 return qdev_create_from_info(bus
, info
);
159 static void qdev_print_devinfo(DeviceInfo
*info
)
161 error_printf("name \"%s\", bus %s",
162 info
->name
, info
->bus_info
->name
);
164 error_printf(", alias \"%s\"", info
->alias
);
167 error_printf(", desc \"%s\"", info
->desc
);
170 error_printf(", no-user");
175 static int set_property(const char *name
, const char *value
, void *opaque
)
177 DeviceState
*dev
= opaque
;
179 if (strcmp(name
, "driver") == 0)
181 if (strcmp(name
, "bus") == 0)
184 if (qdev_prop_parse(dev
, name
, value
) == -1) {
190 int qdev_device_help(QemuOpts
*opts
)
196 driver
= qemu_opt_get(opts
, "driver");
197 if (driver
&& !strcmp(driver
, "?")) {
198 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
200 continue; /* not available, don't show */
202 qdev_print_devinfo(info
);
207 if (!driver
|| !qemu_opt_get(opts
, "?")) {
211 info
= qdev_find_info(NULL
, driver
);
216 for (prop
= info
->props
; prop
&& prop
->name
; prop
++) {
218 * TODO Properties without a parser are just for dirty hacks.
219 * qdev_prop_ptr is the only such PropertyInfo. It's marked
220 * for removal. This conditional should be removed along with
223 if (!prop
->info
->parse
) {
224 continue; /* no way to set it, don't show */
226 error_printf("%s.%s=%s\n", info
->name
, prop
->name
,
227 prop
->info
->legacy_name
?: prop
->info
->name
);
229 for (prop
= info
->bus_info
->props
; prop
&& prop
->name
; prop
++) {
230 if (!prop
->info
->parse
) {
231 continue; /* no way to set it, don't show */
233 error_printf("%s.%s=%s\n", info
->name
, prop
->name
,
234 prop
->info
->legacy_name
?: prop
->info
->name
);
239 static DeviceState
*qdev_get_peripheral(void)
241 static DeviceState
*dev
;
244 dev
= qdev_create(NULL
, "container");
245 qdev_property_add_child(qdev_get_root(), "peripheral", dev
, NULL
);
246 qdev_init_nofail(dev
);
252 static DeviceState
*qdev_get_peripheral_anon(void)
254 static DeviceState
*dev
;
257 dev
= qdev_create(NULL
, "container");
258 qdev_property_add_child(qdev_get_root(), "peripheral-anon", dev
, NULL
);
259 qdev_init_nofail(dev
);
265 DeviceState
*qdev_device_add(QemuOpts
*opts
)
267 const char *driver
, *path
, *id
;
272 driver
= qemu_opt_get(opts
, "driver");
274 qerror_report(QERR_MISSING_PARAMETER
, "driver");
279 info
= qdev_find_info(NULL
, driver
);
280 if (!info
|| info
->no_user
) {
281 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "driver", "a driver name");
282 error_printf_unless_qmp("Try with argument '?' for a list.\n");
287 path
= qemu_opt_get(opts
, "bus");
289 bus
= qbus_find(path
);
293 if (bus
->info
!= info
->bus_info
) {
294 qerror_report(QERR_BAD_BUS_FOR_DEVICE
,
295 driver
, bus
->info
->name
);
299 bus
= qbus_find_recursive(main_system_bus
, NULL
, info
->bus_info
);
301 qerror_report(QERR_NO_BUS_FOR_DEVICE
,
302 info
->name
, info
->bus_info
->name
);
306 if (qdev_hotplug
&& !bus
->allow_hotplug
) {
307 qerror_report(QERR_BUS_NO_HOTPLUG
, bus
->name
);
311 /* create device, set properties */
312 qdev
= qdev_create_from_info(bus
, info
);
313 id
= qemu_opts_id(opts
);
316 qdev_property_add_child(qdev_get_peripheral(), qdev
->id
, qdev
, NULL
);
318 static int anon_count
;
319 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
320 qdev_property_add_child(qdev_get_peripheral_anon(), name
,
324 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
328 if (qdev_init(qdev
) < 0) {
329 qerror_report(QERR_DEVICE_INIT_FAILED
, driver
);
336 /* Initialize a device. Device properties should be set before calling
337 this function. IRQs and MMIO regions should be connected/mapped after
338 calling this function.
339 On failure, destroy the device and return negative value.
340 Return 0 on success. */
341 int qdev_init(DeviceState
*dev
)
345 assert(dev
->state
== DEV_STATE_CREATED
);
346 rc
= dev
->info
->init(dev
, dev
->info
);
351 if (dev
->info
->vmsd
) {
352 vmstate_register_with_alias_id(dev
, -1, dev
->info
->vmsd
, dev
,
353 dev
->instance_id_alias
,
354 dev
->alias_required_for_version
);
356 dev
->state
= DEV_STATE_INITIALIZED
;
357 if (dev
->hotplugged
&& dev
->info
->reset
) {
358 dev
->info
->reset(dev
);
363 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
364 int required_for_version
)
366 assert(dev
->state
== DEV_STATE_CREATED
);
367 dev
->instance_id_alias
= alias_id
;
368 dev
->alias_required_for_version
= required_for_version
;
371 int qdev_unplug(DeviceState
*dev
)
373 if (!dev
->parent_bus
->allow_hotplug
) {
374 qerror_report(QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
377 assert(dev
->info
->unplug
!= NULL
);
379 qdev_hot_removed
= true;
381 return dev
->info
->unplug(dev
);
384 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
386 if (dev
->info
->reset
) {
387 dev
->info
->reset(dev
);
393 BusState
*sysbus_get_default(void)
395 if (!main_system_bus
) {
396 main_system_bus_create();
398 return main_system_bus
;
401 static int qbus_reset_one(BusState
*bus
, void *opaque
)
403 if (bus
->info
->reset
) {
404 return bus
->info
->reset(bus
);
409 void qdev_reset_all(DeviceState
*dev
)
411 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
414 void qbus_reset_all_fn(void *opaque
)
416 BusState
*bus
= opaque
;
417 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
420 /* can be used as ->unplug() callback for the simple cases */
421 int qdev_simple_unplug_cb(DeviceState
*dev
)
429 /* Like qdev_init(), but terminate program via error_report() instead of
430 returning an error value. This is okay during machine creation.
431 Don't use for hotplug, because there callers need to recover from
432 failure. Exception: if you know the device's init() callback can't
433 fail, then qdev_init_nofail() can't fail either, and is therefore
434 usable even then. But relying on the device implementation that
435 way is somewhat unclean, and best avoided. */
436 void qdev_init_nofail(DeviceState
*dev
)
438 DeviceInfo
*info
= dev
->info
;
440 if (qdev_init(dev
) < 0) {
441 error_report("Initialization of device %s failed", info
->name
);
446 static void qdev_property_del_all(DeviceState
*dev
)
448 while (!QTAILQ_EMPTY(&dev
->properties
)) {
449 DeviceProperty
*prop
= QTAILQ_FIRST(&dev
->properties
);
451 QTAILQ_REMOVE(&dev
->properties
, prop
, node
);
454 prop
->release(dev
, prop
->name
, prop
->opaque
);
463 static void qdev_property_del_child(DeviceState
*dev
, DeviceState
*child
, Error
**errp
)
465 DeviceProperty
*prop
;
467 QTAILQ_FOREACH(prop
, &dev
->properties
, node
) {
468 if (strstart(prop
->type
, "child<", NULL
) && prop
->opaque
== child
) {
473 g_assert(prop
!= NULL
);
475 QTAILQ_REMOVE(&dev
->properties
, prop
, node
);
478 prop
->release(dev
, prop
->name
, prop
->opaque
);
486 /* Unlink device from bus and free the structure. */
487 void qdev_free(DeviceState
*dev
)
492 qdev_property_del_all(dev
);
494 if (dev
->state
== DEV_STATE_INITIALIZED
) {
495 while (dev
->num_child_bus
) {
496 bus
= QLIST_FIRST(&dev
->child_bus
);
500 vmstate_unregister(dev
, dev
->info
->vmsd
, dev
);
502 dev
->info
->exit(dev
);
504 qemu_opts_del(dev
->opts
);
506 QTAILQ_REMOVE(&dev
->parent_bus
->children
, dev
, sibling
);
507 for (prop
= dev
->info
->props
; prop
&& prop
->name
; prop
++) {
508 if (prop
->info
->free
) {
509 prop
->info
->free(dev
, prop
);
513 qdev_property_del_child(dev
->parent
, dev
, NULL
);
516 qerror_report(QERR_DEVICE_IN_USE
, dev
->id
?:"");
521 void qdev_machine_creation_done(void)
524 * ok, initial machine setup is done, starting from now we can
525 * only create hotpluggable devices
530 bool qdev_machine_modified(void)
532 return qdev_hot_added
|| qdev_hot_removed
;
535 /* Get a character (serial) device interface. */
536 CharDriverState
*qdev_init_chardev(DeviceState
*dev
)
538 static int next_serial
;
540 /* FIXME: This function needs to go away: use chardev properties! */
541 return serial_hds
[next_serial
++];
544 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
546 return dev
->parent_bus
;
549 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
551 assert(dev
->num_gpio_in
== 0);
552 dev
->num_gpio_in
= n
;
553 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
556 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
558 assert(dev
->num_gpio_out
== 0);
559 dev
->num_gpio_out
= n
;
560 dev
->gpio_out
= pins
;
563 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
565 assert(n
>= 0 && n
< dev
->num_gpio_in
);
566 return dev
->gpio_in
[n
];
569 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
571 assert(n
>= 0 && n
< dev
->num_gpio_out
);
572 dev
->gpio_out
[n
] = pin
;
575 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
577 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
579 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
581 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
582 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
583 qdev_prop_exists(dev
, "vectors")) {
584 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
586 nd
->instantiated
= 1;
589 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
593 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
594 if (strcmp(name
, bus
->name
) == 0) {
601 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
602 qbus_walkerfn
*busfn
, void *opaque
)
608 err
= busfn(bus
, opaque
);
614 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
615 err
= qdev_walk_children(dev
, devfn
, busfn
, opaque
);
624 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
625 qbus_walkerfn
*busfn
, void *opaque
)
631 err
= devfn(dev
, opaque
);
637 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
638 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
647 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
651 BusState
*child
, *ret
;
654 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
657 if (info
&& (bus
->info
!= info
)) {
664 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
665 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
666 ret
= qbus_find_recursive(child
, name
, info
);
675 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
677 DeviceState
*dev
, *ret
;
680 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
681 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
683 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
684 ret
= qdev_find_recursive(child
, id
);
693 static void qbus_list_bus(DeviceState
*dev
)
696 const char *sep
= " ";
698 error_printf("child busses at \"%s\":",
699 dev
->id
? dev
->id
: dev
->info
->name
);
700 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
701 error_printf("%s\"%s\"", sep
, child
->name
);
707 static void qbus_list_dev(BusState
*bus
)
710 const char *sep
= " ";
712 error_printf("devices at \"%s\":", bus
->name
);
713 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
714 error_printf("%s\"%s\"", sep
, dev
->info
->name
);
716 error_printf("/\"%s\"", dev
->id
);
722 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
726 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
727 if (strcmp(child
->name
, elem
) == 0) {
734 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
739 * try to match in order:
740 * (1) instance id, if present
742 * (3) driver alias, if present
744 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
745 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
749 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
750 if (strcmp(dev
->info
->name
, elem
) == 0) {
754 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
755 if (dev
->info
->alias
&& strcmp(dev
->info
->alias
, elem
) == 0) {
762 static BusState
*qbus_find(const char *path
)
769 /* find start element */
770 if (path
[0] == '/') {
771 bus
= main_system_bus
;
774 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
778 bus
= qbus_find_recursive(main_system_bus
, elem
, NULL
);
780 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
787 assert(path
[pos
] == '/' || !path
[pos
]);
788 while (path
[pos
] == '/') {
791 if (path
[pos
] == '\0') {
796 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
801 dev
= qbus_find_dev(bus
, elem
);
803 qerror_report(QERR_DEVICE_NOT_FOUND
, elem
);
804 if (!monitor_cur_is_qmp()) {
810 assert(path
[pos
] == '/' || !path
[pos
]);
811 while (path
[pos
] == '/') {
814 if (path
[pos
] == '\0') {
815 /* last specified element is a device. If it has exactly
816 * one child bus accept it nevertheless */
817 switch (dev
->num_child_bus
) {
819 qerror_report(QERR_DEVICE_NO_BUS
, elem
);
822 return QLIST_FIRST(&dev
->child_bus
);
824 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES
, elem
);
825 if (!monitor_cur_is_qmp()) {
833 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
838 bus
= qbus_find_bus(dev
, elem
);
840 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
841 if (!monitor_cur_is_qmp()) {
849 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
850 DeviceState
*parent
, const char *name
)
856 bus
->parent
= parent
;
859 /* use supplied name */
860 bus
->name
= g_strdup(name
);
861 } else if (parent
&& parent
->id
) {
862 /* parent device has id -> use it for bus name */
863 len
= strlen(parent
->id
) + 16;
865 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
868 /* no id -> use lowercase bus type for bus name */
869 len
= strlen(info
->name
) + 16;
871 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
872 parent
? parent
->num_child_bus
: 0);
873 for (i
= 0; i
< len
; i
++)
874 buf
[i
] = qemu_tolower(buf
[i
]);
878 QTAILQ_INIT(&bus
->children
);
880 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
881 parent
->num_child_bus
++;
882 } else if (bus
!= main_system_bus
) {
883 /* TODO: once all bus devices are qdevified,
884 only reset handler for main_system_bus should be registered here. */
885 qemu_register_reset(qbus_reset_all_fn
, bus
);
889 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
893 bus
= g_malloc0(info
->size
);
894 bus
->qdev_allocated
= 1;
895 qbus_create_inplace(bus
, info
, parent
, name
);
899 static void main_system_bus_create(void)
901 /* assign main_system_bus before qbus_create_inplace()
902 * in order to make "if (bus != main_system_bus)" work */
903 main_system_bus
= g_malloc0(system_bus_info
.size
);
904 main_system_bus
->qdev_allocated
= 1;
905 qbus_create_inplace(main_system_bus
, &system_bus_info
, NULL
,
909 void qbus_free(BusState
*bus
)
913 while ((dev
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
917 QLIST_REMOVE(bus
, sibling
);
918 bus
->parent
->num_child_bus
--;
920 assert(bus
!= main_system_bus
); /* main_system_bus is never freed */
921 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
923 g_free((void*)bus
->name
);
924 if (bus
->qdev_allocated
) {
929 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
930 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
932 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
933 const char *prefix
, int indent
)
939 while (props
->name
) {
941 * TODO Properties without a print method are just for dirty
942 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
943 * marked for removal. The test props->info->print should be
944 * removed along with it.
946 if (props
->info
->print
) {
947 props
->info
->print(dev
, props
, buf
, sizeof(buf
));
948 qdev_printf("%s-prop: %s = %s\n", prefix
, props
->name
, buf
);
954 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
957 qdev_printf("dev: %s, id \"%s\"\n", dev
->info
->name
,
958 dev
->id
? dev
->id
: "");
960 if (dev
->num_gpio_in
) {
961 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
963 if (dev
->num_gpio_out
) {
964 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
966 qdev_print_props(mon
, dev
, dev
->info
->props
, "dev", indent
);
967 qdev_print_props(mon
, dev
, dev
->parent_bus
->info
->props
, "bus", indent
);
968 if (dev
->parent_bus
->info
->print_dev
)
969 dev
->parent_bus
->info
->print_dev(mon
, dev
, indent
);
970 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
971 qbus_print(mon
, child
, indent
);
975 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
977 struct DeviceState
*dev
;
979 qdev_printf("bus: %s\n", bus
->name
);
981 qdev_printf("type %s\n", bus
->info
->name
);
982 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
983 qdev_print(mon
, dev
, indent
);
988 void do_info_qtree(Monitor
*mon
)
991 qbus_print(mon
, main_system_bus
, 0);
994 void do_info_qdm(Monitor
*mon
)
998 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
999 qdev_print_devinfo(info
);
1003 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1007 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
);
1011 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
1012 qemu_opts_del(opts
);
1015 if (!qdev_device_add(opts
)) {
1016 qemu_opts_del(opts
);
1022 int do_device_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1024 const char *id
= qdict_get_str(qdict
, "id");
1027 dev
= qdev_find_recursive(main_system_bus
, id
);
1029 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1032 return qdev_unplug(dev
);
1035 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
1039 if (dev
&& dev
->parent_bus
) {
1041 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
1042 if (dev
->parent_bus
->info
->get_fw_dev_path
) {
1043 d
= dev
->parent_bus
->info
->get_fw_dev_path(dev
);
1044 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
1047 l
+= snprintf(p
+ l
, size
- l
, "%s", dev
->info
->name
);
1050 l
+= snprintf(p
+ l
, size
- l
, "/");
1055 char* qdev_get_fw_dev_path(DeviceState
*dev
)
1060 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
1064 return strdup(path
);
1067 char *qdev_get_type(DeviceState
*dev
, Error
**errp
)
1069 return g_strdup(dev
->info
->name
);
1072 void qdev_ref(DeviceState
*dev
)
1077 void qdev_unref(DeviceState
*dev
)
1079 g_assert(dev
->ref
> 0);
1083 void qdev_property_add(DeviceState
*dev
, const char *name
, const char *type
,
1084 DevicePropertyAccessor
*get
, DevicePropertyAccessor
*set
,
1085 DevicePropertyRelease
*release
,
1086 void *opaque
, Error
**errp
)
1088 DeviceProperty
*prop
= g_malloc0(sizeof(*prop
));
1090 prop
->name
= g_strdup(name
);
1091 prop
->type
= g_strdup(type
);
1095 prop
->release
= release
;
1096 prop
->opaque
= opaque
;
1098 QTAILQ_INSERT_TAIL(&dev
->properties
, prop
, node
);
1101 static DeviceProperty
*qdev_property_find(DeviceState
*dev
, const char *name
)
1103 DeviceProperty
*prop
;
1105 QTAILQ_FOREACH(prop
, &dev
->properties
, node
) {
1106 if (strcmp(prop
->name
, name
) == 0) {
1114 void qdev_property_get(DeviceState
*dev
, Visitor
*v
, const char *name
,
1117 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1120 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1125 error_set(errp
, QERR_PERMISSION_DENIED
);
1127 prop
->get(dev
, v
, prop
->opaque
, name
, errp
);
1131 void qdev_property_set(DeviceState
*dev
, Visitor
*v
, const char *name
,
1134 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1137 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1142 error_set(errp
, QERR_PERMISSION_DENIED
);
1144 prop
->set(dev
, v
, prop
->opaque
, name
, errp
);
1148 const char *qdev_property_get_type(DeviceState
*dev
, const char *name
, Error
**errp
)
1150 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1153 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1161 * Legacy property handling
1164 static void qdev_get_legacy_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1165 const char *name
, Error
**errp
)
1167 Property
*prop
= opaque
;
1172 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
1173 visit_type_str(v
, &ptr
, name
, errp
);
1176 static void qdev_set_legacy_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1177 const char *name
, Error
**errp
)
1179 Property
*prop
= opaque
;
1180 Error
*local_err
= NULL
;
1184 if (dev
->state
!= DEV_STATE_CREATED
) {
1185 error_set(errp
, QERR_PERMISSION_DENIED
);
1189 visit_type_str(v
, &ptr
, name
, &local_err
);
1191 error_propagate(errp
, local_err
);
1195 ret
= prop
->info
->parse(dev
, prop
, ptr
);
1196 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
1201 * @qdev_add_legacy_property - adds a legacy property
1203 * Do not use this is new code! Properties added through this interface will
1204 * be given names and types in the "legacy" namespace.
1206 * Legacy properties are always processed as strings. The format of the string
1207 * depends on the property type.
1209 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
1214 name
= g_strdup_printf("legacy-%s", prop
->name
);
1215 type
= g_strdup_printf("legacy<%s>",
1216 prop
->info
->legacy_name
?: prop
->info
->name
);
1218 qdev_property_add(dev
, name
, type
,
1219 prop
->info
->print
? qdev_get_legacy_property
: NULL
,
1220 prop
->info
->parse
? qdev_set_legacy_property
: NULL
,
1229 * @qdev_property_add_static - add a @Property to a device.
1231 * Static properties access data in a struct. The actual type of the
1232 * property and the field depends on the property type.
1234 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
1237 qdev_property_add(dev
, prop
->name
, prop
->info
->name
,
1238 prop
->info
->get
, prop
->info
->set
,
1243 DeviceState
*qdev_get_root(void)
1245 static DeviceState
*qdev_root
;
1248 qdev_root
= qdev_create(NULL
, "container");
1249 qdev_init_nofail(qdev_root
);
1255 static void qdev_get_child_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1256 const char *name
, Error
**errp
)
1258 DeviceState
*child
= opaque
;
1261 path
= qdev_get_canonical_path(child
);
1262 visit_type_str(v
, &path
, name
, errp
);
1266 static void qdev_release_child_property(DeviceState
*dev
, const char *name
,
1269 DeviceState
*child
= opaque
;
1274 void qdev_property_add_child(DeviceState
*dev
, const char *name
,
1275 DeviceState
*child
, Error
**errp
)
1279 type
= g_strdup_printf("child<%s>", child
->info
->name
);
1281 qdev_property_add(dev
, name
, type
, qdev_get_child_property
,
1282 NULL
, qdev_release_child_property
,
1286 g_assert(child
->parent
== NULL
);
1287 child
->parent
= dev
;
1292 static void qdev_get_link_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1293 const char *name
, Error
**errp
)
1295 DeviceState
**child
= opaque
;
1299 path
= qdev_get_canonical_path(*child
);
1300 visit_type_str(v
, &path
, name
, errp
);
1304 visit_type_str(v
, &path
, name
, errp
);
1308 static void qdev_set_link_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1309 const char *name
, Error
**errp
)
1311 DeviceState
**child
= opaque
;
1312 bool ambiguous
= false;
1316 type
= qdev_property_get_type(dev
, name
, NULL
);
1318 visit_type_str(v
, &path
, name
, errp
);
1324 if (strcmp(path
, "") != 0) {
1325 DeviceState
*target
;
1327 target
= qdev_resolve_path(path
, &ambiguous
);
1331 target_type
= g_strdup_printf("link<%s>", target
->info
->name
);
1332 if (strcmp(target_type
, type
) == 0) {
1336 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, type
);
1339 g_free(target_type
);
1341 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
1350 void qdev_property_add_link(DeviceState
*dev
, const char *name
,
1351 const char *type
, DeviceState
**child
,
1356 full_type
= g_strdup_printf("link<%s>", type
);
1358 qdev_property_add(dev
, name
, full_type
,
1359 qdev_get_link_property
,
1360 qdev_set_link_property
,
1366 gchar
*qdev_get_canonical_path(DeviceState
*dev
)
1368 DeviceState
*root
= qdev_get_root();
1369 char *newpath
= NULL
, *path
= NULL
;
1371 while (dev
!= root
) {
1372 DeviceProperty
*prop
= NULL
;
1374 g_assert(dev
->parent
!= NULL
);
1376 QTAILQ_FOREACH(prop
, &dev
->parent
->properties
, node
) {
1377 if (!strstart(prop
->type
, "child<", NULL
)) {
1381 if (prop
->opaque
== dev
) {
1383 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1387 path
= g_strdup(prop
->name
);
1393 g_assert(prop
!= NULL
);
1398 newpath
= g_strdup_printf("/%s", path
);
1404 static DeviceState
*qdev_resolve_abs_path(DeviceState
*parent
,
1408 DeviceProperty
*prop
;
1411 if (parts
[index
] == NULL
) {
1415 if (strcmp(parts
[index
], "") == 0) {
1416 return qdev_resolve_abs_path(parent
, parts
, index
+ 1);
1419 prop
= qdev_property_find(parent
, parts
[index
]);
1425 if (strstart(prop
->type
, "link<", NULL
)) {
1426 DeviceState
**pchild
= prop
->opaque
;
1430 } else if (strstart(prop
->type
, "child<", NULL
)) {
1431 child
= prop
->opaque
;
1438 return qdev_resolve_abs_path(child
, parts
, index
+ 1);
1441 static DeviceState
*qdev_resolve_partial_path(DeviceState
*parent
,
1446 DeviceProperty
*prop
;
1448 dev
= qdev_resolve_abs_path(parent
, parts
, 0);
1450 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1453 if (!strstart(prop
->type
, "child<", NULL
)) {
1457 found
= qdev_resolve_partial_path(prop
->opaque
, parts
, ambiguous
);
1468 if (ambiguous
&& *ambiguous
) {
1476 DeviceState
*qdev_resolve_path(const char *path
, bool *ambiguous
)
1478 bool partial_path
= true;
1482 parts
= g_strsplit(path
, "/", 0);
1483 if (parts
== NULL
|| parts
[0] == NULL
) {
1485 return qdev_get_root();
1488 if (strcmp(parts
[0], "") == 0) {
1489 partial_path
= false;
1496 dev
= qdev_resolve_partial_path(qdev_get_root(), parts
, ambiguous
);
1498 dev
= qdev_resolve_abs_path(qdev_get_root(), parts
, 1);
1506 typedef struct StringProperty
1508 char *(*get
)(DeviceState
*, Error
**);
1509 void (*set
)(DeviceState
*, const char *, Error
**);
1512 static void qdev_property_get_str(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1513 const char *name
, Error
**errp
)
1515 StringProperty
*prop
= opaque
;
1518 value
= prop
->get(dev
, errp
);
1520 visit_type_str(v
, &value
, name
, errp
);
1525 static void qdev_property_set_str(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1526 const char *name
, Error
**errp
)
1528 StringProperty
*prop
= opaque
;
1530 Error
*local_err
= NULL
;
1532 visit_type_str(v
, &value
, name
, &local_err
);
1534 error_propagate(errp
, local_err
);
1538 prop
->set(dev
, value
, errp
);
1542 static void qdev_property_release_str(DeviceState
*dev
, const char *name
,
1545 StringProperty
*prop
= opaque
;
1549 void qdev_property_add_str(DeviceState
*dev
, const char *name
,
1550 char *(*get
)(DeviceState
*, Error
**),
1551 void (*set
)(DeviceState
*, const char *, Error
**),
1554 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1559 qdev_property_add(dev
, name
, "string",
1560 get
? qdev_property_get_str
: NULL
,
1561 set
? qdev_property_set_str
: NULL
,
1562 qdev_property_release_str
,
1566 void qdev_machine_init(void)
1568 qdev_get_peripheral_anon();
1569 qdev_get_peripheral();