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.1 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 #include "qemu/osdep.h"
21 #include "hw/sysbus.h"
22 #include "monitor/hmp.h"
23 #include "monitor/monitor.h"
24 #include "monitor/qdev.h"
25 #include "sysemu/arch_init.h"
26 #include "qapi/error.h"
27 #include "qapi/qapi-commands-qdev.h"
28 #include "qapi/qmp/dispatch.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qstring.h"
32 #include "qapi/qobject-input-visitor.h"
33 #include "qemu/config-file.h"
34 #include "qemu/error-report.h"
35 #include "qemu/help_option.h"
36 #include "qemu/option.h"
37 #include "qemu/qemu-print.h"
38 #include "qemu/option_int.h"
39 #include "sysemu/block-backend.h"
40 #include "migration/misc.h"
41 #include "migration/migration.h"
42 #include "qemu/cutils.h"
43 #include "hw/qdev-properties.h"
45 #include "hw/boards.h"
48 * Aliases were a bad idea from the start. Let's keep them
49 * from spreading further.
51 typedef struct QDevAlias
58 /* default virtio transport per architecture */
59 #define QEMU_ARCH_VIRTIO_PCI (QEMU_ARCH_ALPHA | QEMU_ARCH_ARM | \
60 QEMU_ARCH_HPPA | QEMU_ARCH_I386 | \
61 QEMU_ARCH_MIPS | QEMU_ARCH_PPC | \
62 QEMU_ARCH_RISCV | QEMU_ARCH_SH4 | \
63 QEMU_ARCH_SPARC | QEMU_ARCH_XTENSA)
64 #define QEMU_ARCH_VIRTIO_CCW (QEMU_ARCH_S390X)
65 #define QEMU_ARCH_VIRTIO_MMIO (QEMU_ARCH_M68K)
67 /* Please keep this table sorted by typename. */
68 static const QDevAlias qdev_alias_table
[] = {
69 { "AC97", "ac97" }, /* -soundhw name */
70 { "e1000", "e1000-82540em" },
71 { "ES1370", "es1370" }, /* -soundhw name */
72 { "ich9-ahci", "ahci" },
73 { "lsi53c895a", "lsi" },
74 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO
},
75 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW
},
76 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI
},
77 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO
},
78 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW
},
79 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI
},
80 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO
},
81 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW
},
82 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI
},
83 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO
},
84 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW
},
85 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI
},
86 { "virtio-gpu-gl-device", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_MMIO
},
87 { "virtio-gpu-gl-pci", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_PCI
},
88 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO
},
89 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW
},
90 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI
},
91 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI
},
92 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO
},
93 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW
},
94 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI
},
95 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO
},
96 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW
},
97 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI
},
98 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO
},
99 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW
},
100 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI
},
101 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO
},
102 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW
},
103 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI
},
104 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO
},
105 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW
},
106 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI
},
107 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO
},
108 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW
},
109 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI
},
110 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO
},
111 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW
},
112 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI
},
116 static const char *qdev_class_get_alias(DeviceClass
*dc
)
118 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
121 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
122 if (qdev_alias_table
[i
].arch_mask
&&
123 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
127 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
128 return qdev_alias_table
[i
].alias
;
135 static bool qdev_class_has_alias(DeviceClass
*dc
)
137 return (qdev_class_get_alias(dc
) != NULL
);
140 static void qdev_print_devinfo(DeviceClass
*dc
)
142 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
144 qemu_printf(", bus %s", dc
->bus_type
);
146 if (qdev_class_has_alias(dc
)) {
147 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
150 qemu_printf(", desc \"%s\"", dc
->desc
);
152 if (!dc
->user_creatable
) {
153 qemu_printf(", no-user");
158 static void qdev_print_devinfos(bool show_no_user
)
160 static const char *cat_name
[DEVICE_CATEGORY_MAX
+ 1] = {
161 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
162 [DEVICE_CATEGORY_USB
] = "USB",
163 [DEVICE_CATEGORY_STORAGE
] = "Storage",
164 [DEVICE_CATEGORY_NETWORK
] = "Network",
165 [DEVICE_CATEGORY_INPUT
] = "Input",
166 [DEVICE_CATEGORY_DISPLAY
] = "Display",
167 [DEVICE_CATEGORY_SOUND
] = "Sound",
168 [DEVICE_CATEGORY_MISC
] = "Misc",
169 [DEVICE_CATEGORY_CPU
] = "CPU",
170 [DEVICE_CATEGORY_WATCHDOG
]= "Watchdog",
171 [DEVICE_CATEGORY_MAX
] = "Uncategorized",
177 module_load_qom_all();
178 list
= object_class_get_list_sorted(TYPE_DEVICE
, false);
180 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
182 for (elt
= list
; elt
; elt
= elt
->next
) {
183 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
185 if ((i
< DEVICE_CATEGORY_MAX
186 ? !test_bit(i
, dc
->categories
)
187 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
189 && !dc
->user_creatable
)) {
193 qemu_printf("%s%s devices:\n", i
? "\n" : "", cat_name
[i
]);
196 qdev_print_devinfo(dc
);
203 static const char *find_typename_by_alias(const char *alias
)
207 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
208 if (qdev_alias_table
[i
].arch_mask
&&
209 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
213 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
214 return qdev_alias_table
[i
].typename
;
221 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
225 const char *original_name
= *driver
;
227 oc
= module_object_class_by_name(*driver
);
229 const char *typename
= find_typename_by_alias(*driver
);
233 oc
= module_object_class_by_name(*driver
);
237 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
238 if (*driver
!= original_name
) {
239 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
240 " name", original_name
, *driver
);
242 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
247 if (object_class_is_abstract(oc
)) {
248 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
249 "a non-abstract device type");
253 dc
= DEVICE_CLASS(oc
);
254 if (!dc
->user_creatable
||
255 (phase_check(PHASE_MACHINE_READY
) && !dc
->hotpluggable
)) {
256 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
257 "a pluggable device type");
261 if (object_class_dynamic_cast(oc
, TYPE_SYS_BUS_DEVICE
)) {
262 /* sysbus devices need to be allowed by the machine */
263 MachineClass
*mc
= MACHINE_CLASS(object_get_class(qdev_get_machine()));
264 if (!device_type_is_dynamic_sysbus(mc
, *driver
)) {
265 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
266 "a dynamic sysbus device type for the machine");
275 int qdev_device_help(QemuOpts
*opts
)
277 Error
*local_err
= NULL
;
279 ObjectPropertyInfoList
*prop_list
;
280 ObjectPropertyInfoList
*prop
;
284 driver
= qemu_opt_get(opts
, "driver");
285 if (driver
&& is_help_option(driver
)) {
286 qdev_print_devinfos(false);
290 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
294 if (!object_class_by_name(driver
)) {
295 const char *typename
= find_typename_by_alias(driver
);
302 prop_list
= qmp_device_list_properties(driver
, &local_err
);
308 qemu_printf("%s options:\n", driver
);
310 qemu_printf("There are no options for %s.\n", driver
);
312 array
= g_ptr_array_new();
313 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
314 g_ptr_array_add(array
,
315 object_property_help(prop
->value
->name
,
317 prop
->value
->default_value
,
318 prop
->value
->description
));
320 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
321 for (i
= 0; i
< array
->len
; i
++) {
322 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
324 g_ptr_array_set_free_func(array
, g_free
);
325 g_ptr_array_free(array
, true);
326 qapi_free_ObjectPropertyInfoList(prop_list
);
330 error_report_err(local_err
);
334 static Object
*qdev_get_peripheral(void)
339 dev
= container_get(qdev_get_machine(), "/peripheral");
345 static Object
*qdev_get_peripheral_anon(void)
350 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
356 static void qbus_error_append_bus_list_hint(DeviceState
*dev
,
360 const char *sep
= " ";
362 error_append_hint(errp
, "child buses at \"%s\":",
363 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
364 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
365 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
368 error_append_hint(errp
, "\n");
371 static void qbus_error_append_dev_list_hint(BusState
*bus
,
375 const char *sep
= " ";
377 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
378 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
379 DeviceState
*dev
= kid
->child
;
380 error_append_hint(errp
, "%s\"%s\"", sep
,
381 object_get_typename(OBJECT(dev
)));
383 error_append_hint(errp
, "/\"%s\"", dev
->id
);
387 error_append_hint(errp
, "\n");
390 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
394 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
395 if (strcmp(child
->name
, elem
) == 0) {
402 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
407 * try to match in order:
408 * (1) instance id, if present
410 * (3) driver alias, if present
412 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
413 DeviceState
*dev
= kid
->child
;
414 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
418 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
419 DeviceState
*dev
= kid
->child
;
420 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
424 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
425 DeviceState
*dev
= kid
->child
;
426 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
428 if (qdev_class_has_alias(dc
) &&
429 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
436 static inline bool qbus_is_full(BusState
*bus
)
443 bus_class
= BUS_GET_CLASS(bus
);
444 return bus_class
->max_dev
&& bus
->num_children
>= bus_class
->max_dev
;
448 * Search the tree rooted at @bus for a bus.
449 * If @name, search for a bus with that name. Note that bus names
450 * need not be unique. Yes, that's screwed up.
451 * Else search for a bus that is a subtype of @bus_typename.
452 * If more than one exists, prefer one that can take another device.
453 * Return the bus if found, else %NULL.
455 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
456 const char *bus_typename
)
459 BusState
*pick
, *child
, *ret
;
462 assert(name
|| bus_typename
);
464 match
= !strcmp(bus
->name
, name
);
466 match
= !!object_dynamic_cast(OBJECT(bus
), bus_typename
);
469 if (match
&& !qbus_is_full(bus
)) {
470 return bus
; /* root matches and isn't full */
473 pick
= match
? bus
: NULL
;
475 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
476 DeviceState
*dev
= kid
->child
;
477 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
478 ret
= qbus_find_recursive(child
, name
, bus_typename
);
479 if (ret
&& !qbus_is_full(ret
)) {
480 return ret
; /* a descendant matches and isn't full */
488 /* root or a descendant matches, but is full */
492 static BusState
*qbus_find(const char *path
, Error
**errp
)
499 /* find start element */
500 if (path
[0] == '/') {
501 bus
= sysbus_get_default();
504 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
508 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
510 error_setg(errp
, "Bus '%s' not found", elem
);
517 assert(path
[pos
] == '/' || !path
[pos
]);
518 while (path
[pos
] == '/') {
521 if (path
[pos
] == '\0') {
526 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
527 g_assert_not_reached();
531 dev
= qbus_find_dev(bus
, elem
);
533 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
534 "Device '%s' not found", elem
);
535 qbus_error_append_dev_list_hint(bus
, errp
);
539 assert(path
[pos
] == '/' || !path
[pos
]);
540 while (path
[pos
] == '/') {
543 if (path
[pos
] == '\0') {
544 /* last specified element is a device. If it has exactly
545 * one child bus accept it nevertheless */
546 if (dev
->num_child_bus
== 1) {
547 bus
= QLIST_FIRST(&dev
->child_bus
);
550 if (dev
->num_child_bus
) {
551 error_setg(errp
, "Device '%s' has multiple child buses",
553 qbus_error_append_bus_list_hint(dev
, errp
);
555 error_setg(errp
, "Device '%s' has no child bus", elem
);
561 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
562 g_assert_not_reached();
566 bus
= qbus_find_bus(dev
, elem
);
568 error_setg(errp
, "Bus '%s' not found", elem
);
569 qbus_error_append_bus_list_hint(dev
, errp
);
574 if (qbus_is_full(bus
)) {
575 error_setg(errp
, "Bus '%s' is full", path
);
581 /* Takes ownership of @id, will be freed when deleting the device */
582 const char *qdev_set_id(DeviceState
*dev
, char *id
, Error
**errp
)
584 ObjectProperty
*prop
;
586 assert(!dev
->id
&& !dev
->realized
);
589 * object_property_[try_]add_child() below will assert the device
593 prop
= object_property_try_add_child(qdev_get_peripheral(), id
,
598 error_setg(errp
, "Duplicate device ID '%s'", id
);
603 static int anon_count
;
604 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
605 prop
= object_property_add_child(qdev_get_peripheral_anon(), name
,
613 DeviceState
*qdev_device_add_from_qdict(const QDict
*opts
,
614 bool from_json
, Error
**errp
)
618 const char *driver
, *path
;
620 DeviceState
*dev
= NULL
;
621 BusState
*bus
= NULL
;
623 driver
= qdict_get_try_str(opts
, "driver");
625 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
630 dc
= qdev_get_device_class(&driver
, errp
);
636 path
= qdict_get_try_str(opts
, "bus");
638 bus
= qbus_find(path
, errp
);
642 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
643 error_setg(errp
, "Device '%s' can't go on %s bus",
644 driver
, object_get_typename(OBJECT(bus
)));
647 } else if (dc
->bus_type
!= NULL
) {
648 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
649 if (!bus
|| qbus_is_full(bus
)) {
650 error_setg(errp
, "No '%s' bus found for device '%s'",
651 dc
->bus_type
, driver
);
656 if (qdev_should_hide_device(opts
, from_json
, errp
)) {
657 if (bus
&& !qbus_is_hotpluggable(bus
)) {
658 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
665 if (phase_check(PHASE_MACHINE_READY
) && bus
&& !qbus_is_hotpluggable(bus
)) {
666 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
670 if (!migration_is_idle()) {
671 error_setg(errp
, "device_add not allowed while migrating");
676 dev
= qdev_new(driver
);
678 /* Check whether the hotplug is allowed by the machine */
679 if (phase_check(PHASE_MACHINE_READY
)) {
680 if (!qdev_hotplug_allowed(dev
, errp
)) {
684 if (!bus
&& !qdev_get_machine_hotplug_handler(dev
)) {
685 /* No bus, no machine hotplug handler --> device is not hotpluggable */
686 error_setg(errp
, "Device '%s' can not be hotplugged on this machine",
693 * set dev's parent and register its id.
694 * If it fails it means the id is already taken.
696 id
= g_strdup(qdict_get_try_str(opts
, "id"));
697 if (!qdev_set_id(dev
, id
, errp
)) {
702 dev
->opts
= qdict_clone_shallow(opts
);
703 qdict_del(dev
->opts
, "driver");
704 qdict_del(dev
->opts
, "bus");
705 qdict_del(dev
->opts
, "id");
707 object_set_properties_from_keyval(&dev
->parent_obj
, dev
->opts
, from_json
,
713 if (!qdev_realize(DEVICE(dev
), bus
, errp
)) {
720 object_unparent(OBJECT(dev
));
721 object_unref(OBJECT(dev
));
726 /* Takes ownership of @opts on success */
727 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
729 QDict
*qdict
= qemu_opts_to_qdict(opts
, NULL
);
732 ret
= qdev_device_add_from_qdict(qdict
, false, errp
);
736 qobject_unref(qdict
);
740 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
741 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
743 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
748 for (; props
->name
; props
++) {
750 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
752 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
753 value
= object_property_get_str(OBJECT(dev
), legacy_name
, NULL
);
755 value
= object_property_print(OBJECT(dev
), props
->name
, true,
763 qdev_printf("%s = %s\n", props
->name
,
764 *value
? value
: "<null>");
769 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
771 BusClass
*bc
= BUS_GET_CLASS(bus
);
774 bc
->print_dev(mon
, dev
, indent
);
778 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
785 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
786 dev
->id
? dev
->id
: "");
788 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
790 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
794 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
798 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
799 g_autofree
char *freq_str
= clock_display_freq(ncl
->clock
);
800 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
801 ncl
->output
? "out" : "in",
802 ncl
->alias
? " (alias)" : "",
803 ncl
->name
, freq_str
);
805 class = object_get_class(OBJECT(dev
));
807 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props_
, indent
);
808 class = object_class_get_parent(class);
809 } while (class != object_class_by_name(TYPE_DEVICE
));
810 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
811 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
812 qbus_print(mon
, child
, indent
);
816 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
820 qdev_printf("bus: %s\n", bus
->name
);
822 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
823 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
824 DeviceState
*dev
= kid
->child
;
825 qdev_print(mon
, dev
, indent
);
830 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
832 if (sysbus_get_default())
833 qbus_print(mon
, sysbus_get_default(), 0);
836 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
838 qdev_print_devinfos(true);
841 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
846 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, errp
);
850 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
854 dev
= qdev_device_add(opts
, errp
);
857 * Drain all pending RCU callbacks. This is done because
858 * some bus related operations can delay a device removal
859 * (in this case this can happen if device is added and then
860 * removed due to a configuration error)
861 * to a RCU callback, but user might expect that this interface
862 * will finish its job completely once qmp command returns result
871 object_unref(OBJECT(dev
));
874 static DeviceState
*find_device_state(const char *id
, Error
**errp
)
876 Object
*obj
= object_resolve_path_at(qdev_get_peripheral(), id
);
880 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
881 "Device '%s' not found", id
);
885 dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
887 error_setg(errp
, "%s is not a hotpluggable device", id
);
894 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
896 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
897 HotplugHandler
*hotplug_ctrl
;
898 HotplugHandlerClass
*hdc
;
899 Error
*local_err
= NULL
;
901 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
902 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
906 if (!dc
->hotpluggable
) {
907 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
,
908 object_get_typename(OBJECT(dev
)));
912 if (!migration_is_idle() && !dev
->allow_unplug_during_migration
) {
913 error_setg(errp
, "device_del not allowed while migrating");
917 qdev_hot_removed
= true;
919 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
920 /* hotpluggable device MUST have HotplugHandler, if it doesn't
921 * then something is very wrong with it */
922 g_assert(hotplug_ctrl
);
924 /* If device supports async unplug just request it to be done,
925 * otherwise just remove it synchronously */
926 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
927 if (hdc
->unplug_request
) {
928 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &local_err
);
930 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
932 object_unparent(OBJECT(dev
));
935 error_propagate(errp
, local_err
);
938 void qmp_device_del(const char *id
, Error
**errp
)
940 DeviceState
*dev
= find_device_state(id
, errp
);
942 if (dev
->pending_deleted_event
&&
943 (dev
->pending_deleted_expires_ms
== 0 ||
944 dev
->pending_deleted_expires_ms
> qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
))) {
945 error_setg(errp
, "Device %s is already in the "
946 "process of unplug", id
);
950 qdev_unplug(dev
, errp
);
954 void hmp_device_add(Monitor
*mon
, const QDict
*qdict
)
958 qmp_device_add((QDict
*)qdict
, NULL
, &err
);
959 hmp_handle_error(mon
, err
);
962 void hmp_device_del(Monitor
*mon
, const QDict
*qdict
)
964 const char *id
= qdict_get_str(qdict
, "id");
967 qmp_device_del(id
, &err
);
968 hmp_handle_error(mon
, err
);
971 BlockBackend
*blk_by_qdev_id(const char *id
, Error
**errp
)
978 dev
= find_device_state(id
, errp
);
983 blk
= blk_by_dev(dev
);
985 error_setg(errp
, "Device does not have a block device backend");
990 QemuOptsList qemu_device_opts
= {
992 .implied_opt_name
= "driver",
993 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
996 * no elements => accept any
997 * sanity checking will happen later
998 * when setting device properties
1000 { /* end of list */ }
1004 QemuOptsList qemu_global_opts
= {
1006 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
1010 .type
= QEMU_OPT_STRING
,
1013 .type
= QEMU_OPT_STRING
,
1016 .type
= QEMU_OPT_STRING
,
1018 { /* end of list */ }
1022 int qemu_global_option(const char *str
)
1024 char driver
[64], property
[64];
1028 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
1029 if (rc
== 2 && str
[offset
] == '=') {
1030 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
1031 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
1032 qemu_opt_set(opts
, "property", property
, &error_abort
);
1033 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
1037 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);
1045 bool qmp_command_available(const QmpCommand
*cmd
, Error
**errp
)
1047 if (!phase_check(PHASE_MACHINE_READY
) &&
1048 !(cmd
->options
& QCO_ALLOW_PRECONFIG
)) {
1049 error_setg(errp
, "The command '%s' is permitted only after machine initialization has completed",