target/riscv: Remove an unused CASE_OP_32_64 macro
[qemu/ar7.git] / softmmu / qdev-monitor.c
blob721dec2d82006127578a8061870c55a36330941b
1 /*
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 "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/help_option.h"
34 #include "qemu/option.h"
35 #include "qemu/qemu-print.h"
36 #include "qemu/option_int.h"
37 #include "sysemu/block-backend.h"
38 #include "migration/misc.h"
39 #include "migration/migration.h"
40 #include "qemu/cutils.h"
41 #include "hw/qdev-properties.h"
42 #include "hw/clock.h"
45 * Aliases were a bad idea from the start. Let's keep them
46 * from spreading further.
48 typedef struct QDevAlias
50 const char *typename;
51 const char *alias;
52 uint32_t arch_mask;
53 } QDevAlias;
55 /* Please keep this table sorted by typename. */
56 static const QDevAlias qdev_alias_table[] = {
57 { "AC97", "ac97" }, /* -soundhw name */
58 { "e1000", "e1000-82540em" },
59 { "ES1370", "es1370" }, /* -soundhw name */
60 { "ich9-ahci", "ahci" },
61 { "lsi53c895a", "lsi" },
62 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO },
63 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW },
64 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI },
65 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO },
66 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW },
67 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI },
68 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO },
69 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW },
70 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI },
71 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO },
72 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW },
73 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI },
74 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO },
75 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW },
76 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI },
77 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI },
78 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO },
79 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW },
80 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI },
81 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO },
82 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW },
83 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI },
84 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO },
85 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW },
86 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI },
87 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO },
88 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW },
89 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI },
90 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO },
91 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW },
92 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI },
93 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO },
94 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW },
95 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI},
96 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO },
97 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW },
98 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI },
99 { }
102 static const char *qdev_class_get_alias(DeviceClass *dc)
104 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
105 int i;
107 for (i = 0; qdev_alias_table[i].typename; i++) {
108 if (qdev_alias_table[i].arch_mask &&
109 !(qdev_alias_table[i].arch_mask & arch_type)) {
110 continue;
113 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
114 return qdev_alias_table[i].alias;
118 return NULL;
121 static bool qdev_class_has_alias(DeviceClass *dc)
123 return (qdev_class_get_alias(dc) != NULL);
126 static void qdev_print_devinfo(DeviceClass *dc)
128 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
129 if (dc->bus_type) {
130 qemu_printf(", bus %s", dc->bus_type);
132 if (qdev_class_has_alias(dc)) {
133 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
135 if (dc->desc) {
136 qemu_printf(", desc \"%s\"", dc->desc);
138 if (!dc->user_creatable) {
139 qemu_printf(", no-user");
141 qemu_printf("\n");
144 static void qdev_print_devinfos(bool show_no_user)
146 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
147 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
148 [DEVICE_CATEGORY_USB] = "USB",
149 [DEVICE_CATEGORY_STORAGE] = "Storage",
150 [DEVICE_CATEGORY_NETWORK] = "Network",
151 [DEVICE_CATEGORY_INPUT] = "Input",
152 [DEVICE_CATEGORY_DISPLAY] = "Display",
153 [DEVICE_CATEGORY_SOUND] = "Sound",
154 [DEVICE_CATEGORY_MISC] = "Misc",
155 [DEVICE_CATEGORY_CPU] = "CPU",
156 [DEVICE_CATEGORY_MAX] = "Uncategorized",
158 GSList *list, *elt;
159 int i;
160 bool cat_printed;
162 module_load_qom_all();
163 list = object_class_get_list_sorted(TYPE_DEVICE, false);
165 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
166 cat_printed = false;
167 for (elt = list; elt; elt = elt->next) {
168 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
169 TYPE_DEVICE);
170 if ((i < DEVICE_CATEGORY_MAX
171 ? !test_bit(i, dc->categories)
172 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
173 || (!show_no_user
174 && !dc->user_creatable)) {
175 continue;
177 if (!cat_printed) {
178 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
179 cat_printed = true;
181 qdev_print_devinfo(dc);
185 g_slist_free(list);
188 static int set_property(void *opaque, const char *name, const char *value,
189 Error **errp)
191 Object *obj = opaque;
193 if (strcmp(name, "driver") == 0)
194 return 0;
195 if (strcmp(name, "bus") == 0)
196 return 0;
198 if (!object_property_parse(obj, name, value, errp)) {
199 return -1;
201 return 0;
204 static const char *find_typename_by_alias(const char *alias)
206 int i;
208 for (i = 0; qdev_alias_table[i].alias; i++) {
209 if (qdev_alias_table[i].arch_mask &&
210 !(qdev_alias_table[i].arch_mask & arch_type)) {
211 continue;
214 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
215 return qdev_alias_table[i].typename;
219 return NULL;
222 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
224 ObjectClass *oc;
225 DeviceClass *dc;
226 const char *original_name = *driver;
228 oc = module_object_class_by_name(*driver);
229 if (!oc) {
230 const char *typename = find_typename_by_alias(*driver);
232 if (typename) {
233 *driver = typename;
234 oc = module_object_class_by_name(*driver);
238 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
239 if (*driver != original_name) {
240 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
241 " name", original_name, *driver);
242 } else {
243 error_setg(errp, "'%s' is not a valid device model name", *driver);
245 return NULL;
248 if (object_class_is_abstract(oc)) {
249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
250 "a non-abstract device type");
251 return NULL;
254 dc = DEVICE_CLASS(oc);
255 if (!dc->user_creatable ||
256 (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
257 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
258 "a pluggable device type");
259 return NULL;
262 return dc;
266 int qdev_device_help(QemuOpts *opts)
268 Error *local_err = NULL;
269 const char *driver;
270 ObjectPropertyInfoList *prop_list;
271 ObjectPropertyInfoList *prop;
272 GPtrArray *array;
273 int i;
275 driver = qemu_opt_get(opts, "driver");
276 if (driver && is_help_option(driver)) {
277 qdev_print_devinfos(false);
278 return 1;
281 if (!driver || !qemu_opt_has_help_opt(opts)) {
282 return 0;
285 if (!object_class_by_name(driver)) {
286 const char *typename = find_typename_by_alias(driver);
288 if (typename) {
289 driver = typename;
293 prop_list = qmp_device_list_properties(driver, &local_err);
294 if (local_err) {
295 goto error;
298 if (prop_list) {
299 qemu_printf("%s options:\n", driver);
300 } else {
301 qemu_printf("There are no options for %s.\n", driver);
303 array = g_ptr_array_new();
304 for (prop = prop_list; prop; prop = prop->next) {
305 g_ptr_array_add(array,
306 object_property_help(prop->value->name,
307 prop->value->type,
308 prop->value->default_value,
309 prop->value->description));
311 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
312 for (i = 0; i < array->len; i++) {
313 qemu_printf("%s\n", (char *)array->pdata[i]);
315 g_ptr_array_set_free_func(array, g_free);
316 g_ptr_array_free(array, true);
317 qapi_free_ObjectPropertyInfoList(prop_list);
318 return 1;
320 error:
321 error_report_err(local_err);
322 return 1;
325 static Object *qdev_get_peripheral(void)
327 static Object *dev;
329 if (dev == NULL) {
330 dev = container_get(qdev_get_machine(), "/peripheral");
333 return dev;
336 static Object *qdev_get_peripheral_anon(void)
338 static Object *dev;
340 if (dev == NULL) {
341 dev = container_get(qdev_get_machine(), "/peripheral-anon");
344 return dev;
347 static void qbus_error_append_bus_list_hint(DeviceState *dev,
348 Error *const *errp)
350 BusState *child;
351 const char *sep = " ";
353 error_append_hint(errp, "child buses at \"%s\":",
354 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
355 QLIST_FOREACH(child, &dev->child_bus, sibling) {
356 error_append_hint(errp, "%s\"%s\"", sep, child->name);
357 sep = ", ";
359 error_append_hint(errp, "\n");
362 static void qbus_error_append_dev_list_hint(BusState *bus,
363 Error *const *errp)
365 BusChild *kid;
366 const char *sep = " ";
368 error_append_hint(errp, "devices at \"%s\":", bus->name);
369 QTAILQ_FOREACH(kid, &bus->children, sibling) {
370 DeviceState *dev = kid->child;
371 error_append_hint(errp, "%s\"%s\"", sep,
372 object_get_typename(OBJECT(dev)));
373 if (dev->id) {
374 error_append_hint(errp, "/\"%s\"", dev->id);
376 sep = ", ";
378 error_append_hint(errp, "\n");
381 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
383 BusState *child;
385 QLIST_FOREACH(child, &dev->child_bus, sibling) {
386 if (strcmp(child->name, elem) == 0) {
387 return child;
390 return NULL;
393 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
395 BusChild *kid;
398 * try to match in order:
399 * (1) instance id, if present
400 * (2) driver name
401 * (3) driver alias, if present
403 QTAILQ_FOREACH(kid, &bus->children, sibling) {
404 DeviceState *dev = kid->child;
405 if (dev->id && strcmp(dev->id, elem) == 0) {
406 return dev;
409 QTAILQ_FOREACH(kid, &bus->children, sibling) {
410 DeviceState *dev = kid->child;
411 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
412 return dev;
415 QTAILQ_FOREACH(kid, &bus->children, sibling) {
416 DeviceState *dev = kid->child;
417 DeviceClass *dc = DEVICE_GET_CLASS(dev);
419 if (qdev_class_has_alias(dc) &&
420 strcmp(qdev_class_get_alias(dc), elem) == 0) {
421 return dev;
424 return NULL;
427 static inline bool qbus_is_full(BusState *bus)
429 BusClass *bus_class = BUS_GET_CLASS(bus);
430 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
434 * Search the tree rooted at @bus for a bus.
435 * If @name, search for a bus with that name. Note that bus names
436 * need not be unique. Yes, that's screwed up.
437 * Else search for a bus that is a subtype of @bus_typename.
438 * If more than one exists, prefer one that can take another device.
439 * Return the bus if found, else %NULL.
441 static BusState *qbus_find_recursive(BusState *bus, const char *name,
442 const char *bus_typename)
444 BusChild *kid;
445 BusState *pick, *child, *ret;
446 bool match;
448 assert(name || bus_typename);
449 if (name) {
450 match = !strcmp(bus->name, name);
451 } else {
452 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
455 if (match && !qbus_is_full(bus)) {
456 return bus; /* root matches and isn't full */
459 pick = match ? bus : NULL;
461 QTAILQ_FOREACH(kid, &bus->children, sibling) {
462 DeviceState *dev = kid->child;
463 QLIST_FOREACH(child, &dev->child_bus, sibling) {
464 ret = qbus_find_recursive(child, name, bus_typename);
465 if (ret && !qbus_is_full(ret)) {
466 return ret; /* a descendant matches and isn't full */
468 if (ret && !pick) {
469 pick = ret;
474 /* root or a descendant matches, but is full */
475 return pick;
478 static BusState *qbus_find(const char *path, Error **errp)
480 DeviceState *dev;
481 BusState *bus;
482 char elem[128];
483 int pos, len;
485 /* find start element */
486 if (path[0] == '/') {
487 bus = sysbus_get_default();
488 pos = 0;
489 } else {
490 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
491 assert(!path[0]);
492 elem[0] = len = 0;
494 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
495 if (!bus) {
496 error_setg(errp, "Bus '%s' not found", elem);
497 return NULL;
499 pos = len;
502 for (;;) {
503 assert(path[pos] == '/' || !path[pos]);
504 while (path[pos] == '/') {
505 pos++;
507 if (path[pos] == '\0') {
508 break;
511 /* find device */
512 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
513 g_assert_not_reached();
514 elem[0] = len = 0;
516 pos += len;
517 dev = qbus_find_dev(bus, elem);
518 if (!dev) {
519 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
520 "Device '%s' not found", elem);
521 qbus_error_append_dev_list_hint(bus, errp);
522 return NULL;
525 assert(path[pos] == '/' || !path[pos]);
526 while (path[pos] == '/') {
527 pos++;
529 if (path[pos] == '\0') {
530 /* last specified element is a device. If it has exactly
531 * one child bus accept it nevertheless */
532 if (dev->num_child_bus == 1) {
533 bus = QLIST_FIRST(&dev->child_bus);
534 break;
536 if (dev->num_child_bus) {
537 error_setg(errp, "Device '%s' has multiple child buses",
538 elem);
539 qbus_error_append_bus_list_hint(dev, errp);
540 } else {
541 error_setg(errp, "Device '%s' has no child bus", elem);
543 return NULL;
546 /* find bus */
547 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
548 g_assert_not_reached();
549 elem[0] = len = 0;
551 pos += len;
552 bus = qbus_find_bus(dev, elem);
553 if (!bus) {
554 error_setg(errp, "Bus '%s' not found", elem);
555 qbus_error_append_bus_list_hint(dev, errp);
556 return NULL;
560 if (qbus_is_full(bus)) {
561 error_setg(errp, "Bus '%s' is full", path);
562 return NULL;
564 return bus;
567 void qdev_set_id(DeviceState *dev, const char *id)
569 if (id) {
570 dev->id = id;
573 if (dev->id) {
574 object_property_add_child(qdev_get_peripheral(), dev->id,
575 OBJECT(dev));
576 } else {
577 static int anon_count;
578 gchar *name = g_strdup_printf("device[%d]", anon_count++);
579 object_property_add_child(qdev_get_peripheral_anon(), name,
580 OBJECT(dev));
581 g_free(name);
585 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
587 DeviceClass *dc;
588 const char *driver, *path;
589 DeviceState *dev = NULL;
590 BusState *bus = NULL;
592 driver = qemu_opt_get(opts, "driver");
593 if (!driver) {
594 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
595 return NULL;
598 /* find driver */
599 dc = qdev_get_device_class(&driver, errp);
600 if (!dc) {
601 return NULL;
604 /* find bus */
605 path = qemu_opt_get(opts, "bus");
606 if (path != NULL) {
607 bus = qbus_find(path, errp);
608 if (!bus) {
609 return NULL;
611 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
612 error_setg(errp, "Device '%s' can't go on %s bus",
613 driver, object_get_typename(OBJECT(bus)));
614 return NULL;
616 } else if (dc->bus_type != NULL) {
617 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
618 if (!bus || qbus_is_full(bus)) {
619 error_setg(errp, "No '%s' bus found for device '%s'",
620 dc->bus_type, driver);
621 return NULL;
625 if (qemu_opt_get(opts, "failover_pair_id")) {
626 if (!opts->id) {
627 error_setg(errp, "Device with failover_pair_id don't have id");
628 return NULL;
630 if (qdev_should_hide_device(opts)) {
631 if (bus && !qbus_is_hotpluggable(bus)) {
632 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
634 return NULL;
638 if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
639 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
640 return NULL;
643 if (!migration_is_idle()) {
644 error_setg(errp, "device_add not allowed while migrating");
645 return NULL;
648 /* create device */
649 dev = qdev_new(driver);
651 /* Check whether the hotplug is allowed by the machine */
652 if (phase_check(PHASE_MACHINE_READY)) {
653 if (!qdev_hotplug_allowed(dev, errp)) {
654 goto err_del_dev;
657 if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
658 /* No bus, no machine hotplug handler --> device is not hotpluggable */
659 error_setg(errp, "Device '%s' can not be hotplugged on this machine",
660 driver);
661 goto err_del_dev;
665 qdev_set_id(dev, qemu_opts_id(opts));
667 /* set properties */
668 if (qemu_opt_foreach(opts, set_property, dev, errp)) {
669 goto err_del_dev;
672 dev->opts = opts;
673 if (!qdev_realize(DEVICE(dev), bus, errp)) {
674 dev->opts = NULL;
675 goto err_del_dev;
677 return dev;
679 err_del_dev:
680 if (dev) {
681 object_unparent(OBJECT(dev));
682 object_unref(OBJECT(dev));
684 return NULL;
688 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
689 static void qbus_print(Monitor *mon, BusState *bus, int indent);
691 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
692 int indent)
694 if (!props)
695 return;
696 for (; props->name; props++) {
697 char *value;
698 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
700 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
701 value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
702 } else {
703 value = object_property_print(OBJECT(dev), props->name, true,
704 NULL);
706 g_free(legacy_name);
708 if (!value) {
709 continue;
711 qdev_printf("%s = %s\n", props->name,
712 *value ? value : "<null>");
713 g_free(value);
717 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
719 BusClass *bc = BUS_GET_CLASS(bus);
721 if (bc->print_dev) {
722 bc->print_dev(mon, dev, indent);
726 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
728 ObjectClass *class;
729 BusState *child;
730 NamedGPIOList *ngl;
731 NamedClockList *ncl;
733 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
734 dev->id ? dev->id : "");
735 indent += 2;
736 QLIST_FOREACH(ngl, &dev->gpios, node) {
737 if (ngl->num_in) {
738 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
739 ngl->num_in);
741 if (ngl->num_out) {
742 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
743 ngl->num_out);
746 QLIST_FOREACH(ncl, &dev->clocks, node) {
747 g_autofree char *freq_str = clock_display_freq(ncl->clock);
748 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
749 ncl->output ? "out" : "in",
750 ncl->alias ? " (alias)" : "",
751 ncl->name, freq_str);
753 class = object_get_class(OBJECT(dev));
754 do {
755 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
756 class = object_class_get_parent(class);
757 } while (class != object_class_by_name(TYPE_DEVICE));
758 bus_print_dev(dev->parent_bus, mon, dev, indent);
759 QLIST_FOREACH(child, &dev->child_bus, sibling) {
760 qbus_print(mon, child, indent);
764 static void qbus_print(Monitor *mon, BusState *bus, int indent)
766 BusChild *kid;
768 qdev_printf("bus: %s\n", bus->name);
769 indent += 2;
770 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
771 QTAILQ_FOREACH(kid, &bus->children, sibling) {
772 DeviceState *dev = kid->child;
773 qdev_print(mon, dev, indent);
776 #undef qdev_printf
778 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
780 if (sysbus_get_default())
781 qbus_print(mon, sysbus_get_default(), 0);
784 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
786 qdev_print_devinfos(true);
789 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
791 QemuOpts *opts;
792 DeviceState *dev;
794 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
795 if (!opts) {
796 return;
798 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
799 qemu_opts_del(opts);
800 return;
802 dev = qdev_device_add(opts, errp);
805 * Drain all pending RCU callbacks. This is done because
806 * some bus related operations can delay a device removal
807 * (in this case this can happen if device is added and then
808 * removed due to a configuration error)
809 * to a RCU callback, but user might expect that this interface
810 * will finish its job completely once qmp command returns result
811 * to the user
813 drain_call_rcu();
815 if (!dev) {
816 qemu_opts_del(opts);
817 return;
819 object_unref(OBJECT(dev));
822 static DeviceState *find_device_state(const char *id, Error **errp)
824 Object *obj;
826 if (id[0] == '/') {
827 obj = object_resolve_path(id, NULL);
828 } else {
829 char *root_path = object_get_canonical_path(qdev_get_peripheral());
830 char *path = g_strdup_printf("%s/%s", root_path, id);
832 g_free(root_path);
833 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
834 g_free(path);
837 if (!obj) {
838 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
839 "Device '%s' not found", id);
840 return NULL;
843 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
844 error_setg(errp, "%s is not a hotpluggable device", id);
845 return NULL;
848 return DEVICE(obj);
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);
860 return;
863 if (!dc->hotpluggable) {
864 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
865 object_get_typename(OBJECT(dev)));
866 return;
869 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
870 error_setg(errp, "device_del not allowed while migrating");
871 return;
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);
886 } else {
887 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
888 if (!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);
898 if (dev != NULL) {
899 if (dev->pending_deleted_event) {
900 error_setg(errp, "Device %s is already in the "
901 "process of unplug", id);
902 return;
905 qdev_unplug(dev, errp);
909 void hmp_device_add(Monitor *mon, const QDict *qdict)
911 Error *err = NULL;
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");
920 Error *err = NULL;
922 qmp_device_del(id, &err);
923 hmp_handle_error(mon, err);
926 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
928 DeviceState *dev;
929 BlockBackend *blk;
931 dev = find_device_state(id, errp);
932 if (dev == NULL) {
933 return NULL;
936 blk = blk_by_dev(dev);
937 if (!blk) {
938 error_setg(errp, "Device does not have a block device backend");
940 return blk;
943 QemuOptsList qemu_device_opts = {
944 .name = "device",
945 .implied_opt_name = "driver",
946 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
947 .desc = {
949 * no elements => accept any
950 * sanity checking will happen later
951 * when setting device properties
953 { /* end of list */ }
957 QemuOptsList qemu_global_opts = {
958 .name = "global",
959 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
960 .desc = {
962 .name = "driver",
963 .type = QEMU_OPT_STRING,
965 .name = "property",
966 .type = QEMU_OPT_STRING,
968 .name = "value",
969 .type = QEMU_OPT_STRING,
971 { /* end of list */ }
975 int qemu_global_option(const char *str)
977 char driver[64], property[64];
978 QemuOpts *opts;
979 int rc, offset;
981 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
982 if (rc == 2 && str[offset] == '=') {
983 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
984 qemu_opt_set(opts, "driver", driver, &error_abort);
985 qemu_opt_set(opts, "property", property, &error_abort);
986 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
987 return 0;
990 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
991 if (!opts) {
992 return -1;
995 return 0;
998 bool qmp_command_available(const QmpCommand *cmd, Error **errp)
1000 if (!phase_check(PHASE_MACHINE_READY) &&
1001 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
1002 error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
1003 cmd->name);
1004 return false;
1006 return true;