arm: Add header to host common definition for nRF51 SOC peripherals
[qemu/kevin.git] / qdev-monitor.c
blob07147c63bf8ba74b77f011b6bf8bb9f257d56ba5
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 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/qdev.h"
22 #include "hw/sysbus.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-misc.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 "sysemu/block-backend.h"
35 #include "migration/misc.h"
38 * Aliases were a bad idea from the start. Let's keep them
39 * from spreading further.
41 typedef struct QDevAlias
43 const char *typename;
44 const char *alias;
45 uint32_t arch_mask;
46 } QDevAlias;
48 /* Please keep this table sorted by typename. */
49 static const QDevAlias qdev_alias_table[] = {
50 { "e1000", "e1000-82540em" },
51 { "ich9-ahci", "ahci" },
52 { "lsi53c895a", "lsi" },
53 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
54 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
55 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
56 { "virtio-balloon-pci", "virtio-balloon",
57 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
58 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
59 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
60 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
61 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
62 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
63 { "virtio-input-host-pci", "virtio-input-host",
64 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
65 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
66 { "virtio-keyboard-pci", "virtio-keyboard",
67 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
68 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
69 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
70 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
71 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
72 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
73 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
74 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
75 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
76 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
77 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
78 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
79 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
80 { }
83 static const char *qdev_class_get_alias(DeviceClass *dc)
85 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
86 int i;
88 for (i = 0; qdev_alias_table[i].typename; i++) {
89 if (qdev_alias_table[i].arch_mask &&
90 !(qdev_alias_table[i].arch_mask & arch_type)) {
91 continue;
94 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
95 return qdev_alias_table[i].alias;
99 return NULL;
102 static bool qdev_class_has_alias(DeviceClass *dc)
104 return (qdev_class_get_alias(dc) != NULL);
107 static void out_printf(const char *fmt, ...)
109 va_list ap;
111 va_start(ap, fmt);
112 monitor_vfprintf(stdout, fmt, ap);
113 va_end(ap);
116 static void qdev_print_devinfo(DeviceClass *dc)
118 out_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
119 if (dc->bus_type) {
120 out_printf(", bus %s", dc->bus_type);
122 if (qdev_class_has_alias(dc)) {
123 out_printf(", alias \"%s\"", qdev_class_get_alias(dc));
125 if (dc->desc) {
126 out_printf(", desc \"%s\"", dc->desc);
128 if (!dc->user_creatable) {
129 out_printf(", no-user");
131 out_printf("\n");
134 static void qdev_print_devinfos(bool show_no_user)
136 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
137 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
138 [DEVICE_CATEGORY_USB] = "USB",
139 [DEVICE_CATEGORY_STORAGE] = "Storage",
140 [DEVICE_CATEGORY_NETWORK] = "Network",
141 [DEVICE_CATEGORY_INPUT] = "Input",
142 [DEVICE_CATEGORY_DISPLAY] = "Display",
143 [DEVICE_CATEGORY_SOUND] = "Sound",
144 [DEVICE_CATEGORY_MISC] = "Misc",
145 [DEVICE_CATEGORY_CPU] = "CPU",
146 [DEVICE_CATEGORY_MAX] = "Uncategorized",
148 GSList *list, *elt;
149 int i;
150 bool cat_printed;
152 list = object_class_get_list_sorted(TYPE_DEVICE, false);
154 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
155 cat_printed = false;
156 for (elt = list; elt; elt = elt->next) {
157 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
158 TYPE_DEVICE);
159 if ((i < DEVICE_CATEGORY_MAX
160 ? !test_bit(i, dc->categories)
161 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
162 || (!show_no_user
163 && !dc->user_creatable)) {
164 continue;
166 if (!cat_printed) {
167 out_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
168 cat_printed = true;
170 qdev_print_devinfo(dc);
174 g_slist_free(list);
177 static int set_property(void *opaque, const char *name, const char *value,
178 Error **errp)
180 Object *obj = opaque;
181 Error *err = NULL;
183 if (strcmp(name, "driver") == 0)
184 return 0;
185 if (strcmp(name, "bus") == 0)
186 return 0;
188 object_property_parse(obj, value, name, &err);
189 if (err != NULL) {
190 error_propagate(errp, err);
191 return -1;
193 return 0;
196 static const char *find_typename_by_alias(const char *alias)
198 int i;
200 for (i = 0; qdev_alias_table[i].alias; i++) {
201 if (qdev_alias_table[i].arch_mask &&
202 !(qdev_alias_table[i].arch_mask & arch_type)) {
203 continue;
206 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
207 return qdev_alias_table[i].typename;
211 return NULL;
214 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
216 ObjectClass *oc;
217 DeviceClass *dc;
218 const char *original_name = *driver;
220 oc = object_class_by_name(*driver);
221 if (!oc) {
222 const char *typename = find_typename_by_alias(*driver);
224 if (typename) {
225 *driver = typename;
226 oc = object_class_by_name(*driver);
230 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
231 if (*driver != original_name) {
232 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
233 " name", original_name, *driver);
234 } else {
235 error_setg(errp, "'%s' is not a valid device model name", *driver);
237 return NULL;
240 if (object_class_is_abstract(oc)) {
241 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
242 "non-abstract device type");
243 return NULL;
246 dc = DEVICE_CLASS(oc);
247 if (!dc->user_creatable ||
248 (qdev_hotplug && !dc->hotpluggable)) {
249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
250 "pluggable device type");
251 return NULL;
254 return dc;
258 int qdev_device_help(QemuOpts *opts)
260 Error *local_err = NULL;
261 const char *driver;
262 ObjectPropertyInfoList *prop_list;
263 ObjectPropertyInfoList *prop;
265 driver = qemu_opt_get(opts, "driver");
266 if (driver && is_help_option(driver)) {
267 qdev_print_devinfos(false);
268 return 1;
271 if (!driver || !qemu_opt_has_help_opt(opts)) {
272 return 0;
275 if (!object_class_by_name(driver)) {
276 const char *typename = find_typename_by_alias(driver);
278 if (typename) {
279 driver = typename;
283 prop_list = qmp_device_list_properties(driver, &local_err);
284 if (local_err) {
285 goto error;
288 if (prop_list) {
289 out_printf("%s options:\n", driver);
290 } else {
291 out_printf("There are no options for %s.\n", driver);
293 for (prop = prop_list; prop; prop = prop->next) {
294 int len;
295 out_printf(" %s=<%s>%n", prop->value->name, prop->value->type, &len);
296 if (prop->value->has_description) {
297 if (len < 24) {
298 out_printf("%*s", 24 - len, "");
300 out_printf(" - %s\n", prop->value->description);
301 } else {
302 out_printf("\n");
306 qapi_free_ObjectPropertyInfoList(prop_list);
307 return 1;
309 error:
310 error_report_err(local_err);
311 return 1;
314 static Object *qdev_get_peripheral(void)
316 static Object *dev;
318 if (dev == NULL) {
319 dev = container_get(qdev_get_machine(), "/peripheral");
322 return dev;
325 static Object *qdev_get_peripheral_anon(void)
327 static Object *dev;
329 if (dev == NULL) {
330 dev = container_get(qdev_get_machine(), "/peripheral-anon");
333 return dev;
336 static void qbus_list_bus(DeviceState *dev, Error **errp)
338 BusState *child;
339 const char *sep = " ";
341 error_append_hint(errp, "child buses at \"%s\":",
342 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
343 QLIST_FOREACH(child, &dev->child_bus, sibling) {
344 error_append_hint(errp, "%s\"%s\"", sep, child->name);
345 sep = ", ";
347 error_append_hint(errp, "\n");
350 static void qbus_list_dev(BusState *bus, Error **errp)
352 BusChild *kid;
353 const char *sep = " ";
355 error_append_hint(errp, "devices at \"%s\":", bus->name);
356 QTAILQ_FOREACH(kid, &bus->children, sibling) {
357 DeviceState *dev = kid->child;
358 error_append_hint(errp, "%s\"%s\"", sep,
359 object_get_typename(OBJECT(dev)));
360 if (dev->id) {
361 error_append_hint(errp, "/\"%s\"", dev->id);
363 sep = ", ";
365 error_append_hint(errp, "\n");
368 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
370 BusState *child;
372 QLIST_FOREACH(child, &dev->child_bus, sibling) {
373 if (strcmp(child->name, elem) == 0) {
374 return child;
377 return NULL;
380 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
382 BusChild *kid;
385 * try to match in order:
386 * (1) instance id, if present
387 * (2) driver name
388 * (3) driver alias, if present
390 QTAILQ_FOREACH(kid, &bus->children, sibling) {
391 DeviceState *dev = kid->child;
392 if (dev->id && strcmp(dev->id, elem) == 0) {
393 return dev;
396 QTAILQ_FOREACH(kid, &bus->children, sibling) {
397 DeviceState *dev = kid->child;
398 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
399 return dev;
402 QTAILQ_FOREACH(kid, &bus->children, sibling) {
403 DeviceState *dev = kid->child;
404 DeviceClass *dc = DEVICE_GET_CLASS(dev);
406 if (qdev_class_has_alias(dc) &&
407 strcmp(qdev_class_get_alias(dc), elem) == 0) {
408 return dev;
411 return NULL;
414 static inline bool qbus_is_full(BusState *bus)
416 BusClass *bus_class = BUS_GET_CLASS(bus);
417 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
421 * Search the tree rooted at @bus for a bus.
422 * If @name, search for a bus with that name. Note that bus names
423 * need not be unique. Yes, that's screwed up.
424 * Else search for a bus that is a subtype of @bus_typename.
425 * If more than one exists, prefer one that can take another device.
426 * Return the bus if found, else %NULL.
428 static BusState *qbus_find_recursive(BusState *bus, const char *name,
429 const char *bus_typename)
431 BusChild *kid;
432 BusState *pick, *child, *ret;
433 bool match;
435 assert(name || bus_typename);
436 if (name) {
437 match = !strcmp(bus->name, name);
438 } else {
439 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
442 if (match && !qbus_is_full(bus)) {
443 return bus; /* root matches and isn't full */
446 pick = match ? bus : NULL;
448 QTAILQ_FOREACH(kid, &bus->children, sibling) {
449 DeviceState *dev = kid->child;
450 QLIST_FOREACH(child, &dev->child_bus, sibling) {
451 ret = qbus_find_recursive(child, name, bus_typename);
452 if (ret && !qbus_is_full(ret)) {
453 return ret; /* a descendant matches and isn't full */
455 if (ret && !pick) {
456 pick = ret;
461 /* root or a descendant matches, but is full */
462 return pick;
465 static BusState *qbus_find(const char *path, Error **errp)
467 DeviceState *dev;
468 BusState *bus;
469 char elem[128];
470 int pos, len;
472 /* find start element */
473 if (path[0] == '/') {
474 bus = sysbus_get_default();
475 pos = 0;
476 } else {
477 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
478 assert(!path[0]);
479 elem[0] = len = 0;
481 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
482 if (!bus) {
483 error_setg(errp, "Bus '%s' not found", elem);
484 return NULL;
486 pos = len;
489 for (;;) {
490 assert(path[pos] == '/' || !path[pos]);
491 while (path[pos] == '/') {
492 pos++;
494 if (path[pos] == '\0') {
495 break;
498 /* find device */
499 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
500 g_assert_not_reached();
501 elem[0] = len = 0;
503 pos += len;
504 dev = qbus_find_dev(bus, elem);
505 if (!dev) {
506 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
507 "Device '%s' not found", elem);
508 qbus_list_dev(bus, errp);
509 return NULL;
512 assert(path[pos] == '/' || !path[pos]);
513 while (path[pos] == '/') {
514 pos++;
516 if (path[pos] == '\0') {
517 /* last specified element is a device. If it has exactly
518 * one child bus accept it nevertheless */
519 if (dev->num_child_bus == 1) {
520 bus = QLIST_FIRST(&dev->child_bus);
521 break;
523 if (dev->num_child_bus) {
524 error_setg(errp, "Device '%s' has multiple child buses",
525 elem);
526 qbus_list_bus(dev, errp);
527 } else {
528 error_setg(errp, "Device '%s' has no child bus", elem);
530 return NULL;
533 /* find bus */
534 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
535 g_assert_not_reached();
536 elem[0] = len = 0;
538 pos += len;
539 bus = qbus_find_bus(dev, elem);
540 if (!bus) {
541 error_setg(errp, "Bus '%s' not found", elem);
542 qbus_list_bus(dev, errp);
543 return NULL;
547 if (qbus_is_full(bus)) {
548 error_setg(errp, "Bus '%s' is full", path);
549 return NULL;
551 return bus;
554 void qdev_set_id(DeviceState *dev, const char *id)
556 if (id) {
557 dev->id = id;
560 if (dev->id) {
561 object_property_add_child(qdev_get_peripheral(), dev->id,
562 OBJECT(dev), NULL);
563 } else {
564 static int anon_count;
565 gchar *name = g_strdup_printf("device[%d]", anon_count++);
566 object_property_add_child(qdev_get_peripheral_anon(), name,
567 OBJECT(dev), NULL);
568 g_free(name);
572 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
574 DeviceClass *dc;
575 const char *driver, *path;
576 DeviceState *dev;
577 BusState *bus = NULL;
578 Error *err = NULL;
580 driver = qemu_opt_get(opts, "driver");
581 if (!driver) {
582 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
583 return NULL;
586 /* find driver */
587 dc = qdev_get_device_class(&driver, errp);
588 if (!dc) {
589 return NULL;
592 /* find bus */
593 path = qemu_opt_get(opts, "bus");
594 if (path != NULL) {
595 bus = qbus_find(path, errp);
596 if (!bus) {
597 return NULL;
599 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
600 error_setg(errp, "Device '%s' can't go on %s bus",
601 driver, object_get_typename(OBJECT(bus)));
602 return NULL;
604 } else if (dc->bus_type != NULL) {
605 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
606 if (!bus || qbus_is_full(bus)) {
607 error_setg(errp, "No '%s' bus found for device '%s'",
608 dc->bus_type, driver);
609 return NULL;
612 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
613 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
614 return NULL;
617 if (!migration_is_idle()) {
618 error_setg(errp, "device_add not allowed while migrating");
619 return NULL;
622 /* create device */
623 dev = DEVICE(object_new(driver));
625 if (bus) {
626 qdev_set_parent_bus(dev, bus);
627 } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
628 /* No bus, no machine hotplug handler --> device is not hotpluggable */
629 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
630 driver);
631 goto err_del_dev;
634 qdev_set_id(dev, qemu_opts_id(opts));
636 /* set properties */
637 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
638 goto err_del_dev;
641 dev->opts = opts;
642 object_property_set_bool(OBJECT(dev), true, "realized", &err);
643 if (err != NULL) {
644 dev->opts = NULL;
645 goto err_del_dev;
647 return dev;
649 err_del_dev:
650 error_propagate(errp, err);
651 object_unparent(OBJECT(dev));
652 object_unref(OBJECT(dev));
653 return NULL;
657 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
658 static void qbus_print(Monitor *mon, BusState *bus, int indent);
660 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
661 int indent)
663 if (!props)
664 return;
665 for (; props->name; props++) {
666 Error *err = NULL;
667 char *value;
668 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
669 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
670 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
671 } else {
672 value = object_property_print(OBJECT(dev), props->name, true, &err);
674 g_free(legacy_name);
676 if (err) {
677 error_free(err);
678 continue;
680 qdev_printf("%s = %s\n", props->name,
681 value && *value ? value : "<null>");
682 g_free(value);
686 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
688 BusClass *bc = BUS_GET_CLASS(bus);
690 if (bc->print_dev) {
691 bc->print_dev(mon, dev, indent);
695 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
697 ObjectClass *class;
698 BusState *child;
699 NamedGPIOList *ngl;
701 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
702 dev->id ? dev->id : "");
703 indent += 2;
704 QLIST_FOREACH(ngl, &dev->gpios, node) {
705 if (ngl->num_in) {
706 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
707 ngl->num_in);
709 if (ngl->num_out) {
710 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
711 ngl->num_out);
714 class = object_get_class(OBJECT(dev));
715 do {
716 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
717 class = object_class_get_parent(class);
718 } while (class != object_class_by_name(TYPE_DEVICE));
719 bus_print_dev(dev->parent_bus, mon, dev, indent);
720 QLIST_FOREACH(child, &dev->child_bus, sibling) {
721 qbus_print(mon, child, indent);
725 static void qbus_print(Monitor *mon, BusState *bus, int indent)
727 BusChild *kid;
729 qdev_printf("bus: %s\n", bus->name);
730 indent += 2;
731 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
732 QTAILQ_FOREACH(kid, &bus->children, sibling) {
733 DeviceState *dev = kid->child;
734 qdev_print(mon, dev, indent);
737 #undef qdev_printf
739 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
741 if (sysbus_get_default())
742 qbus_print(mon, sysbus_get_default(), 0);
745 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
747 qdev_print_devinfos(true);
750 typedef struct QOMCompositionState {
751 Monitor *mon;
752 int indent;
753 } QOMCompositionState;
755 static void print_qom_composition(Monitor *mon, Object *obj, int indent);
757 static int print_qom_composition_child(Object *obj, void *opaque)
759 QOMCompositionState *s = opaque;
761 print_qom_composition(s->mon, obj, s->indent);
763 return 0;
766 static void print_qom_composition(Monitor *mon, Object *obj, int indent)
768 QOMCompositionState s = {
769 .mon = mon,
770 .indent = indent + 2,
772 char *name;
774 if (obj == object_get_root()) {
775 name = g_strdup("");
776 } else {
777 name = object_get_canonical_path_component(obj);
779 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
780 object_get_typename(obj));
781 g_free(name);
782 object_child_foreach(obj, print_qom_composition_child, &s);
785 void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
787 const char *path = qdict_get_try_str(dict, "path");
788 Object *obj;
789 bool ambiguous = false;
791 if (path) {
792 obj = object_resolve_path(path, &ambiguous);
793 if (!obj) {
794 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
795 return;
797 if (ambiguous) {
798 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
799 return;
801 } else {
802 obj = qdev_get_machine();
804 print_qom_composition(mon, obj, 0);
807 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
809 Error *local_err = NULL;
810 QemuOpts *opts;
811 DeviceState *dev;
813 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
814 if (local_err) {
815 error_propagate(errp, local_err);
816 return;
818 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
819 qemu_opts_del(opts);
820 return;
822 dev = qdev_device_add(opts, &local_err);
823 if (!dev) {
824 error_propagate(errp, local_err);
825 qemu_opts_del(opts);
826 return;
828 object_unref(OBJECT(dev));
831 static DeviceState *find_device_state(const char *id, Error **errp)
833 Object *obj;
835 if (id[0] == '/') {
836 obj = object_resolve_path(id, NULL);
837 } else {
838 char *root_path = object_get_canonical_path(qdev_get_peripheral());
839 char *path = g_strdup_printf("%s/%s", root_path, id);
841 g_free(root_path);
842 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
843 g_free(path);
846 if (!obj) {
847 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
848 "Device '%s' not found", id);
849 return NULL;
852 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
853 error_setg(errp, "%s is not a hotpluggable device", id);
854 return NULL;
857 return DEVICE(obj);
860 void qdev_unplug(DeviceState *dev, Error **errp)
862 DeviceClass *dc = DEVICE_GET_CLASS(dev);
863 HotplugHandler *hotplug_ctrl;
864 HotplugHandlerClass *hdc;
866 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
867 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
868 return;
871 if (!dc->hotpluggable) {
872 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
873 object_get_typename(OBJECT(dev)));
874 return;
877 if (!migration_is_idle()) {
878 error_setg(errp, "device_del not allowed while migrating");
879 return;
882 qdev_hot_removed = true;
884 hotplug_ctrl = qdev_get_hotplug_handler(dev);
885 /* hotpluggable device MUST have HotplugHandler, if it doesn't
886 * then something is very wrong with it */
887 g_assert(hotplug_ctrl);
889 /* If device supports async unplug just request it to be done,
890 * otherwise just remove it synchronously */
891 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
892 if (hdc->unplug_request) {
893 hotplug_handler_unplug_request(hotplug_ctrl, dev, errp);
894 } else {
895 hotplug_handler_unplug(hotplug_ctrl, dev, errp);
899 void qmp_device_del(const char *id, Error **errp)
901 DeviceState *dev = find_device_state(id, errp);
902 if (dev != NULL) {
903 qdev_unplug(dev, errp);
907 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
909 DeviceState *dev;
910 BlockBackend *blk;
912 dev = find_device_state(id, errp);
913 if (dev == NULL) {
914 return NULL;
917 blk = blk_by_dev(dev);
918 if (!blk) {
919 error_setg(errp, "Device does not have a block device backend");
921 return blk;
924 void qdev_machine_init(void)
926 qdev_get_peripheral_anon();
927 qdev_get_peripheral();
930 QemuOptsList qemu_device_opts = {
931 .name = "device",
932 .implied_opt_name = "driver",
933 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
934 .desc = {
936 * no elements => accept any
937 * sanity checking will happen later
938 * when setting device properties
940 { /* end of list */ }
944 QemuOptsList qemu_global_opts = {
945 .name = "global",
946 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
947 .desc = {
949 .name = "driver",
950 .type = QEMU_OPT_STRING,
952 .name = "property",
953 .type = QEMU_OPT_STRING,
955 .name = "value",
956 .type = QEMU_OPT_STRING,
958 { /* end of list */ }
962 int qemu_global_option(const char *str)
964 char driver[64], property[64];
965 QemuOpts *opts;
966 int rc, offset;
968 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
969 if (rc == 2 && str[offset] == '=') {
970 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
971 qemu_opt_set(opts, "driver", driver, &error_abort);
972 qemu_opt_set(opts, "property", property, &error_abort);
973 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
974 return 0;
977 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
978 if (!opts) {
979 return -1;
982 return 0;