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 "qemu/cutils.h"
42 #include "hw/qdev-properties.h"
44 #include "hw/boards.h"
47 * Aliases were a bad idea from the start. Let's keep them
48 * from spreading further.
50 typedef struct QDevAlias
57 /* default virtio transport per architecture */
58 #define QEMU_ARCH_VIRTIO_PCI (QEMU_ARCH_ALPHA | QEMU_ARCH_ARM | \
59 QEMU_ARCH_HPPA | QEMU_ARCH_I386 | \
60 QEMU_ARCH_MIPS | QEMU_ARCH_PPC | \
61 QEMU_ARCH_RISCV | QEMU_ARCH_SH4 | \
62 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-gpu-rutabaga-device", "virtio-gpu-rutabaga",
89 QEMU_ARCH_VIRTIO_MMIO
},
90 { "virtio-gpu-rutabaga-pci", "virtio-gpu-rutabaga", QEMU_ARCH_VIRTIO_PCI
},
91 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO
},
92 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW
},
93 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI
},
94 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI
},
95 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO
},
96 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW
},
97 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI
},
98 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO
},
99 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW
},
100 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI
},
101 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO
},
102 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW
},
103 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI
},
104 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO
},
105 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW
},
106 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI
},
107 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO
},
108 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW
},
109 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI
},
110 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO
},
111 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW
},
112 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI
},
113 { "virtio-sound-device", "virtio-sound", QEMU_ARCH_VIRTIO_MMIO
},
114 { "virtio-sound-pci", "virtio-sound", QEMU_ARCH_VIRTIO_PCI
},
115 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO
},
116 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW
},
117 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI
},
121 static const char *qdev_class_get_alias(DeviceClass
*dc
)
123 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
126 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
127 if (qdev_alias_table
[i
].arch_mask
&&
128 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
132 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
133 return qdev_alias_table
[i
].alias
;
140 static bool qdev_class_has_alias(DeviceClass
*dc
)
142 return (qdev_class_get_alias(dc
) != NULL
);
145 static void qdev_print_devinfo(DeviceClass
*dc
)
147 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
149 qemu_printf(", bus %s", dc
->bus_type
);
151 if (qdev_class_has_alias(dc
)) {
152 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
155 qemu_printf(", desc \"%s\"", dc
->desc
);
157 if (!dc
->user_creatable
) {
158 qemu_printf(", no-user");
163 static void qdev_print_devinfos(bool show_no_user
)
165 static const char *cat_name
[DEVICE_CATEGORY_MAX
+ 1] = {
166 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
167 [DEVICE_CATEGORY_USB
] = "USB",
168 [DEVICE_CATEGORY_STORAGE
] = "Storage",
169 [DEVICE_CATEGORY_NETWORK
] = "Network",
170 [DEVICE_CATEGORY_INPUT
] = "Input",
171 [DEVICE_CATEGORY_DISPLAY
] = "Display",
172 [DEVICE_CATEGORY_SOUND
] = "Sound",
173 [DEVICE_CATEGORY_MISC
] = "Misc",
174 [DEVICE_CATEGORY_CPU
] = "CPU",
175 [DEVICE_CATEGORY_WATCHDOG
]= "Watchdog",
176 [DEVICE_CATEGORY_MAX
] = "Uncategorized",
182 module_load_qom_all();
183 list
= object_class_get_list_sorted(TYPE_DEVICE
, false);
185 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
187 for (elt
= list
; elt
; elt
= elt
->next
) {
188 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
190 if ((i
< DEVICE_CATEGORY_MAX
191 ? !test_bit(i
, dc
->categories
)
192 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
194 && !dc
->user_creatable
)) {
198 qemu_printf("%s%s devices:\n", i
? "\n" : "", cat_name
[i
]);
201 qdev_print_devinfo(dc
);
208 static const char *find_typename_by_alias(const char *alias
)
212 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
213 if (qdev_alias_table
[i
].arch_mask
&&
214 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
218 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
219 return qdev_alias_table
[i
].typename
;
226 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
230 const char *original_name
= *driver
;
232 oc
= module_object_class_by_name(*driver
);
234 const char *typename
= find_typename_by_alias(*driver
);
238 oc
= module_object_class_by_name(*driver
);
242 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
243 if (*driver
!= original_name
) {
244 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
245 " name", original_name
, *driver
);
247 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
252 if (object_class_is_abstract(oc
)) {
253 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
254 "a non-abstract device type");
258 dc
= DEVICE_CLASS(oc
);
259 if (!dc
->user_creatable
||
260 (phase_check(PHASE_MACHINE_READY
) && !dc
->hotpluggable
)) {
261 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
262 "a pluggable device type");
266 if (object_class_dynamic_cast(oc
, TYPE_SYS_BUS_DEVICE
)) {
267 /* sysbus devices need to be allowed by the machine */
268 MachineClass
*mc
= MACHINE_CLASS(object_get_class(qdev_get_machine()));
269 if (!device_type_is_dynamic_sysbus(mc
, *driver
)) {
270 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
271 "a dynamic sysbus device type for the machine");
280 int qdev_device_help(QemuOpts
*opts
)
282 Error
*local_err
= NULL
;
284 ObjectPropertyInfoList
*prop_list
;
285 ObjectPropertyInfoList
*prop
;
289 driver
= qemu_opt_get(opts
, "driver");
290 if (driver
&& is_help_option(driver
)) {
291 qdev_print_devinfos(false);
295 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
299 if (!object_class_by_name(driver
)) {
300 const char *typename
= find_typename_by_alias(driver
);
307 prop_list
= qmp_device_list_properties(driver
, &local_err
);
313 qemu_printf("%s options:\n", driver
);
315 qemu_printf("There are no options for %s.\n", driver
);
317 array
= g_ptr_array_new();
318 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
319 g_ptr_array_add(array
,
320 object_property_help(prop
->value
->name
,
322 prop
->value
->default_value
,
323 prop
->value
->description
));
325 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
326 for (i
= 0; i
< array
->len
; i
++) {
327 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
329 g_ptr_array_set_free_func(array
, g_free
);
330 g_ptr_array_free(array
, true);
331 qapi_free_ObjectPropertyInfoList(prop_list
);
335 error_report_err(local_err
);
339 static Object
*qdev_get_peripheral(void)
344 dev
= container_get(qdev_get_machine(), "/peripheral");
350 static Object
*qdev_get_peripheral_anon(void)
355 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
361 static void qbus_error_append_bus_list_hint(DeviceState
*dev
,
365 const char *sep
= " ";
367 error_append_hint(errp
, "child buses at \"%s\":",
368 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
369 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
370 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
373 error_append_hint(errp
, "\n");
376 static void qbus_error_append_dev_list_hint(BusState
*bus
,
380 const char *sep
= " ";
382 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
383 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
384 DeviceState
*dev
= kid
->child
;
385 error_append_hint(errp
, "%s\"%s\"", sep
,
386 object_get_typename(OBJECT(dev
)));
388 error_append_hint(errp
, "/\"%s\"", dev
->id
);
392 error_append_hint(errp
, "\n");
395 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
399 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
400 if (strcmp(child
->name
, elem
) == 0) {
407 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
412 * try to match in order:
413 * (1) instance id, if present
415 * (3) driver alias, if present
417 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
418 DeviceState
*dev
= kid
->child
;
419 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
423 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
424 DeviceState
*dev
= kid
->child
;
425 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
429 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
430 DeviceState
*dev
= kid
->child
;
431 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
433 if (qdev_class_has_alias(dc
) &&
434 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
441 static inline bool qbus_is_full(BusState
*bus
)
448 bus_class
= BUS_GET_CLASS(bus
);
449 return bus_class
->max_dev
&& bus
->num_children
>= bus_class
->max_dev
;
453 * Search the tree rooted at @bus for a bus.
454 * If @name, search for a bus with that name. Note that bus names
455 * need not be unique. Yes, that's screwed up.
456 * Else search for a bus that is a subtype of @bus_typename.
457 * If more than one exists, prefer one that can take another device.
458 * Return the bus if found, else %NULL.
460 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
461 const char *bus_typename
)
464 BusState
*pick
, *child
, *ret
;
467 assert(name
|| bus_typename
);
469 match
= !strcmp(bus
->name
, name
);
471 match
= !!object_dynamic_cast(OBJECT(bus
), bus_typename
);
474 if (match
&& !qbus_is_full(bus
)) {
475 return bus
; /* root matches and isn't full */
478 pick
= match
? bus
: NULL
;
480 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
481 DeviceState
*dev
= kid
->child
;
482 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
483 ret
= qbus_find_recursive(child
, name
, bus_typename
);
484 if (ret
&& !qbus_is_full(ret
)) {
485 return ret
; /* a descendant matches and isn't full */
493 /* root or a descendant matches, but is full */
497 static BusState
*qbus_find(const char *path
, Error
**errp
)
504 /* find start element */
505 if (path
[0] == '/') {
506 bus
= sysbus_get_default();
509 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
513 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
515 error_setg(errp
, "Bus '%s' not found", elem
);
522 assert(path
[pos
] == '/' || !path
[pos
]);
523 while (path
[pos
] == '/') {
526 if (path
[pos
] == '\0') {
531 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
532 g_assert_not_reached();
536 dev
= qbus_find_dev(bus
, elem
);
538 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
539 "Device '%s' not found", elem
);
540 qbus_error_append_dev_list_hint(bus
, errp
);
544 assert(path
[pos
] == '/' || !path
[pos
]);
545 while (path
[pos
] == '/') {
548 if (path
[pos
] == '\0') {
549 /* last specified element is a device. If it has exactly
550 * one child bus accept it nevertheless */
551 if (dev
->num_child_bus
== 1) {
552 bus
= QLIST_FIRST(&dev
->child_bus
);
555 if (dev
->num_child_bus
) {
556 error_setg(errp
, "Device '%s' has multiple child buses",
558 qbus_error_append_bus_list_hint(dev
, errp
);
560 error_setg(errp
, "Device '%s' has no child bus", elem
);
566 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
567 g_assert_not_reached();
571 bus
= qbus_find_bus(dev
, elem
);
573 error_setg(errp
, "Bus '%s' not found", elem
);
574 qbus_error_append_bus_list_hint(dev
, errp
);
579 if (qbus_is_full(bus
)) {
580 error_setg(errp
, "Bus '%s' is full", path
);
586 /* Takes ownership of @id, will be freed when deleting the device */
587 const char *qdev_set_id(DeviceState
*dev
, char *id
, Error
**errp
)
589 ObjectProperty
*prop
;
591 assert(!dev
->id
&& !dev
->realized
);
594 * object_property_[try_]add_child() below will assert the device
598 prop
= object_property_try_add_child(qdev_get_peripheral(), id
,
603 error_setg(errp
, "Duplicate device ID '%s'", id
);
608 static int anon_count
;
609 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
610 prop
= object_property_add_child(qdev_get_peripheral_anon(), name
,
618 DeviceState
*qdev_device_add_from_qdict(const QDict
*opts
,
619 bool from_json
, Error
**errp
)
623 const char *driver
, *path
;
625 DeviceState
*dev
= NULL
;
626 BusState
*bus
= NULL
;
628 driver
= qdict_get_try_str(opts
, "driver");
630 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
635 dc
= qdev_get_device_class(&driver
, errp
);
641 path
= qdict_get_try_str(opts
, "bus");
643 bus
= qbus_find(path
, errp
);
647 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
648 error_setg(errp
, "Device '%s' can't go on %s bus",
649 driver
, object_get_typename(OBJECT(bus
)));
652 } else if (dc
->bus_type
!= NULL
) {
653 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
654 if (!bus
|| qbus_is_full(bus
)) {
655 error_setg(errp
, "No '%s' bus found for device '%s'",
656 dc
->bus_type
, driver
);
661 if (qdev_should_hide_device(opts
, from_json
, errp
)) {
662 if (bus
&& !qbus_is_hotpluggable(bus
)) {
663 error_setg(errp
, "Bus '%s' does not support hotplugging",
671 if (phase_check(PHASE_MACHINE_READY
) && bus
&& !qbus_is_hotpluggable(bus
)) {
672 error_setg(errp
, "Bus '%s' does not support hotplugging", bus
->name
);
676 if (!migration_is_idle()) {
677 error_setg(errp
, "device_add not allowed while migrating");
682 dev
= qdev_new(driver
);
684 /* Check whether the hotplug is allowed by the machine */
685 if (phase_check(PHASE_MACHINE_READY
)) {
686 if (!qdev_hotplug_allowed(dev
, errp
)) {
690 if (!bus
&& !qdev_get_machine_hotplug_handler(dev
)) {
691 /* No bus, no machine hotplug handler --> device is not hotpluggable */
692 error_setg(errp
, "Device '%s' can not be hotplugged on this machine",
699 * set dev's parent and register its id.
700 * If it fails it means the id is already taken.
702 id
= g_strdup(qdict_get_try_str(opts
, "id"));
703 if (!qdev_set_id(dev
, id
, errp
)) {
708 dev
->opts
= qdict_clone_shallow(opts
);
709 qdict_del(dev
->opts
, "driver");
710 qdict_del(dev
->opts
, "bus");
711 qdict_del(dev
->opts
, "id");
713 object_set_properties_from_keyval(&dev
->parent_obj
, dev
->opts
, from_json
,
719 if (!qdev_realize(dev
, bus
, errp
)) {
726 object_unparent(OBJECT(dev
));
727 object_unref(OBJECT(dev
));
732 /* Takes ownership of @opts on success */
733 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
735 QDict
*qdict
= qemu_opts_to_qdict(opts
, NULL
);
738 ret
= qdev_device_add_from_qdict(qdict
, false, errp
);
742 qobject_unref(qdict
);
746 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
748 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
753 for (; props
->name
; props
++) {
755 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
757 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
758 value
= object_property_get_str(OBJECT(dev
), legacy_name
, NULL
);
760 value
= object_property_print(OBJECT(dev
), props
->name
, true,
768 qdev_printf("%s = %s\n", props
->name
,
769 *value
? value
: "<null>");
774 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
776 BusClass
*bc
= BUS_GET_CLASS(bus
);
779 bc
->print_dev(mon
, dev
, indent
);
783 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
789 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
791 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
795 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
799 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
800 g_autofree
char *freq_str
= clock_display_freq(ncl
->clock
);
801 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
802 ncl
->output
? "out" : "in",
803 ncl
->alias
? " (alias)" : "",
804 ncl
->name
, freq_str
);
806 class = object_get_class(OBJECT(dev
));
808 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props_
, indent
);
809 class = object_class_get_parent(class);
810 } while (class != object_class_by_name(TYPE_DEVICE
));
811 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
814 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
, bool details
)
818 qdev_printf("bus: %s\n", bus
->name
);
820 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
821 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
823 DeviceState
*dev
= kid
->child
;
824 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
825 dev
->id
? dev
->id
: "");
827 qdev_print(mon
, dev
, indent
+ 2);
829 QLIST_FOREACH(child_bus
, &dev
->child_bus
, sibling
) {
830 qbus_print(mon
, child_bus
, indent
+ 2, details
);
836 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
838 bool details
= !qdict_get_try_bool(qdict
, "brief", false);
840 if (sysbus_get_default()) {
841 qbus_print(mon
, sysbus_get_default(), 0, details
);
845 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
847 qdev_print_devinfos(true);
850 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
855 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, errp
);
859 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
863 dev
= qdev_device_add(opts
, errp
);
866 * Drain all pending RCU callbacks. This is done because
867 * some bus related operations can delay a device removal
868 * (in this case this can happen if device is added and then
869 * removed due to a configuration error)
870 * to a RCU callback, but user might expect that this interface
871 * will finish its job completely once qmp command returns result
879 object_unref(OBJECT(dev
));
882 static DeviceState
*find_device_state(const char *id
, Error
**errp
)
884 Object
*obj
= object_resolve_path_at(qdev_get_peripheral(), id
);
888 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
889 "Device '%s' not found", id
);
893 dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
895 error_setg(errp
, "%s is not a device", id
);
902 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
904 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
905 HotplugHandler
*hotplug_ctrl
;
906 HotplugHandlerClass
*hdc
;
907 Error
*local_err
= NULL
;
909 if (qdev_unplug_blocked(dev
, errp
)) {
913 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
914 error_setg(errp
, "Bus '%s' does not support hotplugging",
915 dev
->parent_bus
->name
);
919 if (!dc
->hotpluggable
) {
920 error_setg(errp
, "Device '%s' does not support hotplugging",
921 object_get_typename(OBJECT(dev
)));
925 if (!migration_is_idle() && !dev
->allow_unplug_during_migration
) {
926 error_setg(errp
, "device_del not allowed while migrating");
930 qdev_hot_removed
= true;
932 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
933 /* hotpluggable device MUST have HotplugHandler, if it doesn't
934 * then something is very wrong with it */
935 g_assert(hotplug_ctrl
);
937 /* If device supports async unplug just request it to be done,
938 * otherwise just remove it synchronously */
939 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
940 if (hdc
->unplug_request
) {
941 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &local_err
);
943 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
945 object_unparent(OBJECT(dev
));
948 error_propagate(errp
, local_err
);
951 void qmp_device_del(const char *id
, Error
**errp
)
953 DeviceState
*dev
= find_device_state(id
, errp
);
955 if (dev
->pending_deleted_event
&&
956 (dev
->pending_deleted_expires_ms
== 0 ||
957 dev
->pending_deleted_expires_ms
> qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
))) {
958 error_setg(errp
, "Device %s is already in the "
959 "process of unplug", id
);
963 qdev_unplug(dev
, errp
);
967 void hmp_device_add(Monitor
*mon
, const QDict
*qdict
)
971 qmp_device_add((QDict
*)qdict
, NULL
, &err
);
972 hmp_handle_error(mon
, err
);
975 void hmp_device_del(Monitor
*mon
, const QDict
*qdict
)
977 const char *id
= qdict_get_str(qdict
, "id");
980 qmp_device_del(id
, &err
);
981 hmp_handle_error(mon
, err
);
984 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
994 readline_set_completion_index(rs
, len
);
995 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
997 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1000 if (dc
->user_creatable
) {
1001 readline_add_completion_of(rs
, str
,
1002 object_class_get_name(OBJECT_CLASS(dc
)));
1009 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1011 GSList
**list
= opaque
;
1012 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1018 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1019 *list
= g_slist_append(*list
, dev
);
1025 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1027 GSList
*list
= NULL
;
1029 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1034 static void peripheral_device_del_completion(ReadLineState
*rs
,
1037 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1038 GSList
*list
, *item
;
1040 list
= qdev_build_hotpluggable_device_list(peripheral
);
1045 for (item
= list
; item
; item
= g_slist_next(item
)) {
1046 DeviceState
*dev
= item
->data
;
1049 readline_add_completion_of(rs
, str
, dev
->id
);
1056 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1062 readline_set_completion_index(rs
, strlen(str
));
1063 peripheral_device_del_completion(rs
, str
);
1066 BlockBackend
*blk_by_qdev_id(const char *id
, Error
**errp
)
1071 GLOBAL_STATE_CODE();
1073 dev
= find_device_state(id
, errp
);
1078 blk
= blk_by_dev(dev
);
1080 error_setg(errp
, "Device does not have a block device backend");
1085 QemuOptsList qemu_device_opts
= {
1087 .implied_opt_name
= "driver",
1088 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
1091 * no elements => accept any
1092 * sanity checking will happen later
1093 * when setting device properties
1095 { /* end of list */ }
1099 QemuOptsList qemu_global_opts
= {
1101 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
1105 .type
= QEMU_OPT_STRING
,
1108 .type
= QEMU_OPT_STRING
,
1111 .type
= QEMU_OPT_STRING
,
1113 { /* end of list */ }
1117 int qemu_global_option(const char *str
)
1119 char driver
[64], property
[64];
1123 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
1124 if (rc
== 2 && str
[offset
] == '=') {
1125 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
1126 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
1127 qemu_opt_set(opts
, "property", property
, &error_abort
);
1128 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
1132 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);
1136 if (!qemu_opt_get(opts
, "driver")
1137 || !qemu_opt_get(opts
, "property")
1138 || !qemu_opt_get(opts
, "value")) {
1139 error_report("options 'driver', 'property', and 'value'"
1147 bool qmp_command_available(const QmpCommand
*cmd
, Error
**errp
)
1149 if (!phase_check(PHASE_MACHINE_READY
) &&
1150 !(cmd
->options
& QCO_ALLOW_PRECONFIG
)) {
1151 error_setg(errp
, "The command '%s' is permitted only after machine initialization has completed",