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 #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/qdict.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "qemu/help_option.h"
33 #include "qemu/option.h"
34 #include "qemu/qemu-print.h"
35 #include "qemu/option_int.h"
36 #include "sysemu/block-backend.h"
37 #include "sysemu/sysemu.h"
38 #include "migration/misc.h"
39 #include "migration/migration.h"
40 #include "qemu/cutils.h"
44 * Aliases were a bad idea from the start. Let's keep them
45 * from spreading further.
47 typedef struct QDevAlias
54 /* Please keep this table sorted by typename. */
55 static const QDevAlias qdev_alias_table
[] = {
56 { "AC97", "ac97" }, /* -soundhw name */
57 { "e1000", "e1000-82540em" },
58 { "ich9-ahci", "ahci" },
59 { "lsi53c895a", "lsi" },
60 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X
},
61 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
62 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X
},
63 { "virtio-balloon-pci", "virtio-balloon",
64 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
65 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X
},
66 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
67 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X
},
68 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
69 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X
},
70 { "virtio-input-host-pci", "virtio-input-host",
71 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
72 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
73 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X
},
74 { "virtio-keyboard-pci", "virtio-keyboard",
75 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
76 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X
},
77 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
78 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X
},
79 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
80 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X
},
81 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
82 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X
},
83 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
84 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X
},
85 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
86 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X
},
87 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
91 static const char *qdev_class_get_alias(DeviceClass
*dc
)
93 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
96 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
97 if (qdev_alias_table
[i
].arch_mask
&&
98 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
102 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
103 return qdev_alias_table
[i
].alias
;
110 static bool qdev_class_has_alias(DeviceClass
*dc
)
112 return (qdev_class_get_alias(dc
) != NULL
);
115 static void qdev_print_devinfo(DeviceClass
*dc
)
117 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
119 qemu_printf(", bus %s", dc
->bus_type
);
121 if (qdev_class_has_alias(dc
)) {
122 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
125 qemu_printf(", desc \"%s\"", dc
->desc
);
127 if (!dc
->user_creatable
) {
128 qemu_printf(", no-user");
133 static void qdev_print_devinfos(bool show_no_user
)
135 static const char *cat_name
[DEVICE_CATEGORY_MAX
+ 1] = {
136 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
137 [DEVICE_CATEGORY_USB
] = "USB",
138 [DEVICE_CATEGORY_STORAGE
] = "Storage",
139 [DEVICE_CATEGORY_NETWORK
] = "Network",
140 [DEVICE_CATEGORY_INPUT
] = "Input",
141 [DEVICE_CATEGORY_DISPLAY
] = "Display",
142 [DEVICE_CATEGORY_SOUND
] = "Sound",
143 [DEVICE_CATEGORY_MISC
] = "Misc",
144 [DEVICE_CATEGORY_CPU
] = "CPU",
145 [DEVICE_CATEGORY_MAX
] = "Uncategorized",
151 list
= object_class_get_list_sorted(TYPE_DEVICE
, false);
153 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
155 for (elt
= list
; elt
; elt
= elt
->next
) {
156 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
158 if ((i
< DEVICE_CATEGORY_MAX
159 ? !test_bit(i
, dc
->categories
)
160 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
162 && !dc
->user_creatable
)) {
166 qemu_printf("%s%s devices:\n", i
? "\n" : "", cat_name
[i
]);
169 qdev_print_devinfo(dc
);
176 static int set_property(void *opaque
, const char *name
, const char *value
,
179 Object
*obj
= opaque
;
182 if (strcmp(name
, "driver") == 0)
184 if (strcmp(name
, "bus") == 0)
187 object_property_parse(obj
, value
, name
, &err
);
189 error_propagate(errp
, err
);
195 static const char *find_typename_by_alias(const char *alias
)
199 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
200 if (qdev_alias_table
[i
].arch_mask
&&
201 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
205 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
206 return qdev_alias_table
[i
].typename
;
213 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
217 const char *original_name
= *driver
;
219 oc
= object_class_by_name(*driver
);
221 const char *typename
= find_typename_by_alias(*driver
);
225 oc
= object_class_by_name(*driver
);
229 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
230 if (*driver
!= original_name
) {
231 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
232 " name", original_name
, *driver
);
234 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
239 if (object_class_is_abstract(oc
)) {
240 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
241 "non-abstract device type");
245 dc
= DEVICE_CLASS(oc
);
246 if (!dc
->user_creatable
||
247 (qdev_hotplug
&& !dc
->hotpluggable
)) {
248 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
249 "pluggable device type");
257 int qdev_device_help(QemuOpts
*opts
)
259 Error
*local_err
= NULL
;
261 ObjectPropertyInfoList
*prop_list
;
262 ObjectPropertyInfoList
*prop
;
266 driver
= qemu_opt_get(opts
, "driver");
267 if (driver
&& is_help_option(driver
)) {
268 qdev_print_devinfos(false);
272 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
276 if (!object_class_by_name(driver
)) {
277 const char *typename
= find_typename_by_alias(driver
);
284 prop_list
= qmp_device_list_properties(driver
, &local_err
);
290 qemu_printf("%s options:\n", driver
);
292 qemu_printf("There are no options for %s.\n", driver
);
294 array
= g_ptr_array_new();
295 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
296 g_ptr_array_add(array
,
297 object_property_help(prop
->value
->name
,
299 prop
->value
->default_value
,
300 prop
->value
->description
));
302 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
303 for (i
= 0; i
< array
->len
; i
++) {
304 printf("%s\n", (char *)array
->pdata
[i
]);
306 g_ptr_array_set_free_func(array
, g_free
);
307 g_ptr_array_free(array
, true);
308 qapi_free_ObjectPropertyInfoList(prop_list
);
312 error_report_err(local_err
);
316 static Object
*qdev_get_peripheral(void)
321 dev
= container_get(qdev_get_machine(), "/peripheral");
327 static Object
*qdev_get_peripheral_anon(void)
332 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
338 static void qbus_error_append_bus_list_hint(DeviceState
*dev
,
342 const char *sep
= " ";
344 error_append_hint(errp
, "child buses at \"%s\":",
345 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
346 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
347 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
350 error_append_hint(errp
, "\n");
353 static void qbus_error_append_dev_list_hint(BusState
*bus
,
357 const char *sep
= " ";
359 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
360 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
361 DeviceState
*dev
= kid
->child
;
362 error_append_hint(errp
, "%s\"%s\"", sep
,
363 object_get_typename(OBJECT(dev
)));
365 error_append_hint(errp
, "/\"%s\"", dev
->id
);
369 error_append_hint(errp
, "\n");
372 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
376 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
377 if (strcmp(child
->name
, elem
) == 0) {
384 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
389 * try to match in order:
390 * (1) instance id, if present
392 * (3) driver alias, if present
394 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
395 DeviceState
*dev
= kid
->child
;
396 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
400 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
401 DeviceState
*dev
= kid
->child
;
402 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
406 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
407 DeviceState
*dev
= kid
->child
;
408 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
410 if (qdev_class_has_alias(dc
) &&
411 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
418 static inline bool qbus_is_full(BusState
*bus
)
420 BusClass
*bus_class
= BUS_GET_CLASS(bus
);
421 return bus_class
->max_dev
&& bus
->num_children
>= bus_class
->max_dev
;
425 * Search the tree rooted at @bus for a bus.
426 * If @name, search for a bus with that name. Note that bus names
427 * need not be unique. Yes, that's screwed up.
428 * Else search for a bus that is a subtype of @bus_typename.
429 * If more than one exists, prefer one that can take another device.
430 * Return the bus if found, else %NULL.
432 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
433 const char *bus_typename
)
436 BusState
*pick
, *child
, *ret
;
439 assert(name
|| bus_typename
);
441 match
= !strcmp(bus
->name
, name
);
443 match
= !!object_dynamic_cast(OBJECT(bus
), bus_typename
);
446 if (match
&& !qbus_is_full(bus
)) {
447 return bus
; /* root matches and isn't full */
450 pick
= match
? bus
: NULL
;
452 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
453 DeviceState
*dev
= kid
->child
;
454 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
455 ret
= qbus_find_recursive(child
, name
, bus_typename
);
456 if (ret
&& !qbus_is_full(ret
)) {
457 return ret
; /* a descendant matches and isn't full */
465 /* root or a descendant matches, but is full */
469 static BusState
*qbus_find(const char *path
, Error
**errp
)
476 /* find start element */
477 if (path
[0] == '/') {
478 bus
= sysbus_get_default();
481 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
485 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
487 error_setg(errp
, "Bus '%s' not found", elem
);
494 assert(path
[pos
] == '/' || !path
[pos
]);
495 while (path
[pos
] == '/') {
498 if (path
[pos
] == '\0') {
503 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
504 g_assert_not_reached();
508 dev
= qbus_find_dev(bus
, elem
);
510 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
511 "Device '%s' not found", elem
);
512 qbus_error_append_dev_list_hint(bus
, errp
);
516 assert(path
[pos
] == '/' || !path
[pos
]);
517 while (path
[pos
] == '/') {
520 if (path
[pos
] == '\0') {
521 /* last specified element is a device. If it has exactly
522 * one child bus accept it nevertheless */
523 if (dev
->num_child_bus
== 1) {
524 bus
= QLIST_FIRST(&dev
->child_bus
);
527 if (dev
->num_child_bus
) {
528 error_setg(errp
, "Device '%s' has multiple child buses",
530 qbus_error_append_bus_list_hint(dev
, errp
);
532 error_setg(errp
, "Device '%s' has no child bus", elem
);
538 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
539 g_assert_not_reached();
543 bus
= qbus_find_bus(dev
, elem
);
545 error_setg(errp
, "Bus '%s' not found", elem
);
546 qbus_error_append_bus_list_hint(dev
, errp
);
551 if (qbus_is_full(bus
)) {
552 error_setg(errp
, "Bus '%s' is full", path
);
558 void qdev_set_id(DeviceState
*dev
, const char *id
)
565 object_property_add_child(qdev_get_peripheral(), dev
->id
,
568 static int anon_count
;
569 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
570 object_property_add_child(qdev_get_peripheral_anon(), name
,
576 static int is_failover_device(void *opaque
, const char *name
, const char *value
,
579 if (strcmp(name
, "failover_pair_id") == 0) {
580 QemuOpts
*opts
= (QemuOpts
*)opaque
;
582 if (qdev_should_hide_device(opts
)) {
590 static bool should_hide_device(QemuOpts
*opts
)
592 if (qemu_opt_foreach(opts
, is_failover_device
, opts
, NULL
) == 0) {
598 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
601 const char *driver
, *path
;
602 DeviceState
*dev
= NULL
;
603 BusState
*bus
= NULL
;
607 driver
= qemu_opt_get(opts
, "driver");
609 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
614 dc
= qdev_get_device_class(&driver
, errp
);
620 path
= qemu_opt_get(opts
, "bus");
622 bus
= qbus_find(path
, errp
);
626 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
627 error_setg(errp
, "Device '%s' can't go on %s bus",
628 driver
, object_get_typename(OBJECT(bus
)));
631 } else if (dc
->bus_type
!= NULL
) {
632 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
633 if (!bus
|| qbus_is_full(bus
)) {
634 error_setg(errp
, "No '%s' bus found for device '%s'",
635 dc
->bus_type
, driver
);
639 hide
= should_hide_device(opts
);
641 if ((hide
|| qdev_hotplug
) && bus
&& !qbus_is_hotpluggable(bus
)) {
642 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
650 if (!migration_is_idle()) {
651 error_setg(errp
, "device_add not allowed while migrating");
656 dev
= qdev_new(driver
);
658 /* Check whether the hotplug is allowed by the machine */
659 if (qdev_hotplug
&& !qdev_hotplug_allowed(dev
, &err
)) {
660 /* Error must be set in the machine hook */
665 if (!bus
&& qdev_hotplug
&& !qdev_get_machine_hotplug_handler(dev
)) {
666 /* No bus, no machine hotplug handler --> device is not hotpluggable */
667 error_setg(&err
, "Device '%s' can not be hotplugged on this machine",
672 qdev_set_id(dev
, qemu_opts_id(opts
));
675 if (qemu_opt_foreach(opts
, set_property
, dev
, &err
)) {
680 qdev_realize(DEVICE(dev
), bus
, &err
);
688 error_propagate(errp
, err
);
690 object_unparent(OBJECT(dev
));
691 object_unref(OBJECT(dev
));
697 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
698 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
700 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
705 for (; props
->name
; props
++) {
708 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
709 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
710 value
= object_property_get_str(OBJECT(dev
), legacy_name
, &err
);
712 value
= object_property_print(OBJECT(dev
), props
->name
, true, &err
);
720 qdev_printf("%s = %s\n", props
->name
,
721 value
&& *value
? value
: "<null>");
726 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
728 BusClass
*bc
= BUS_GET_CLASS(bus
);
731 bc
->print_dev(mon
, dev
, indent
);
735 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
742 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
743 dev
->id
? dev
->id
: "");
745 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
747 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
751 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
755 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
756 qdev_printf("clock-%s%s \"%s\" freq_hz=%e\n",
757 ncl
->output
? "out" : "in",
758 ncl
->alias
? " (alias)" : "",
760 CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl
->clock
)));
762 class = object_get_class(OBJECT(dev
));
764 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props_
, indent
);
765 class = object_class_get_parent(class);
766 } while (class != object_class_by_name(TYPE_DEVICE
));
767 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
768 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
769 qbus_print(mon
, child
, indent
);
773 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
777 qdev_printf("bus: %s\n", bus
->name
);
779 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
780 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
781 DeviceState
*dev
= kid
->child
;
782 qdev_print(mon
, dev
, indent
);
787 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
789 if (sysbus_get_default())
790 qbus_print(mon
, sysbus_get_default(), 0);
793 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
795 qdev_print_devinfos(true);
798 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
800 Error
*local_err
= NULL
;
804 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, &local_err
);
806 error_propagate(errp
, local_err
);
809 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
813 dev
= qdev_device_add(opts
, &local_err
);
815 error_propagate(errp
, local_err
);
819 object_unref(OBJECT(dev
));
822 static DeviceState
*find_device_state(const char *id
, Error
**errp
)
827 obj
= object_resolve_path(id
, NULL
);
829 char *root_path
= object_get_canonical_path(qdev_get_peripheral());
830 char *path
= g_strdup_printf("%s/%s", root_path
, id
);
833 obj
= object_resolve_path_type(path
, TYPE_DEVICE
, NULL
);
838 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
839 "Device '%s' not found", id
);
843 if (!object_dynamic_cast(obj
, TYPE_DEVICE
)) {
844 error_setg(errp
, "%s is not a hotpluggable device", id
);
851 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
853 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
854 HotplugHandler
*hotplug_ctrl
;
855 HotplugHandlerClass
*hdc
;
856 Error
*local_err
= NULL
;
858 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
859 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
863 if (!dc
->hotpluggable
) {
864 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
,
865 object_get_typename(OBJECT(dev
)));
869 if (!migration_is_idle() && !dev
->allow_unplug_during_migration
) {
870 error_setg(errp
, "device_del not allowed while migrating");
874 qdev_hot_removed
= true;
876 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
877 /* hotpluggable device MUST have HotplugHandler, if it doesn't
878 * then something is very wrong with it */
879 g_assert(hotplug_ctrl
);
881 /* If device supports async unplug just request it to be done,
882 * otherwise just remove it synchronously */
883 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
884 if (hdc
->unplug_request
) {
885 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &local_err
);
887 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
889 object_unparent(OBJECT(dev
));
892 error_propagate(errp
, local_err
);
895 void qmp_device_del(const char *id
, Error
**errp
)
897 DeviceState
*dev
= find_device_state(id
, errp
);
899 if (dev
->pending_deleted_event
) {
900 error_setg(errp
, "Device %s is already in the "
901 "process of unplug", id
);
905 qdev_unplug(dev
, errp
);
909 void hmp_device_add(Monitor
*mon
, const QDict
*qdict
)
913 qmp_device_add((QDict
*)qdict
, NULL
, &err
);
914 hmp_handle_error(mon
, err
);
917 void hmp_device_del(Monitor
*mon
, const QDict
*qdict
)
919 const char *id
= qdict_get_str(qdict
, "id");
922 qmp_device_del(id
, &err
);
923 hmp_handle_error(mon
, err
);
926 BlockBackend
*blk_by_qdev_id(const char *id
, Error
**errp
)
931 dev
= find_device_state(id
, errp
);
936 blk
= blk_by_dev(dev
);
938 error_setg(errp
, "Device does not have a block device backend");
943 void qdev_machine_init(void)
945 qdev_get_peripheral_anon();
946 qdev_get_peripheral();
949 QemuOptsList qemu_device_opts
= {
951 .implied_opt_name
= "driver",
952 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
955 * no elements => accept any
956 * sanity checking will happen later
957 * when setting device properties
959 { /* end of list */ }
963 QemuOptsList qemu_global_opts
= {
965 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
969 .type
= QEMU_OPT_STRING
,
972 .type
= QEMU_OPT_STRING
,
975 .type
= QEMU_OPT_STRING
,
977 { /* end of list */ }
981 int qemu_global_option(const char *str
)
983 char driver
[64], property
[64];
987 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
988 if (rc
== 2 && str
[offset
] == '=') {
989 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
990 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
991 qemu_opt_set(opts
, "property", property
, &error_abort
);
992 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
996 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);