hw/sd: sd: Only SDSC cards support CMD28/29/30
[qemu/ar7.git] / softmmu / qdev-monitor.c
blob8dc656becca98450f90b33d7d03ca6c48447ee85
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 "sysemu/sysemu.h"
39 #include "migration/misc.h"
40 #include "migration/migration.h"
41 #include "qemu/cutils.h"
42 #include "hw/qdev-properties.h"
43 #include "hw/clock.h"
46 * Aliases were a bad idea from the start. Let's keep them
47 * from spreading further.
49 typedef struct QDevAlias
51 const char *typename;
52 const char *alias;
53 uint32_t arch_mask;
54 } QDevAlias;
56 /* Please keep this table sorted by typename. */
57 static const QDevAlias qdev_alias_table[] = {
58 { "AC97", "ac97" }, /* -soundhw name */
59 { "e1000", "e1000-82540em" },
60 { "ES1370", "es1370" }, /* -soundhw name */
61 { "ich9-ahci", "ahci" },
62 { "lsi53c895a", "lsi" },
63 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
64 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
65 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
66 { "virtio-balloon-pci", "virtio-balloon",
67 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
68 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
69 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
70 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
71 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
72 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
73 { "virtio-input-host-pci", "virtio-input-host",
74 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
75 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
76 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
77 { "virtio-keyboard-pci", "virtio-keyboard",
78 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
79 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
80 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
81 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
82 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
83 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
84 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
85 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
86 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
87 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
88 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
89 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
90 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
91 { }
94 static const char *qdev_class_get_alias(DeviceClass *dc)
96 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
97 int i;
99 for (i = 0; qdev_alias_table[i].typename; i++) {
100 if (qdev_alias_table[i].arch_mask &&
101 !(qdev_alias_table[i].arch_mask & arch_type)) {
102 continue;
105 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
106 return qdev_alias_table[i].alias;
110 return NULL;
113 static bool qdev_class_has_alias(DeviceClass *dc)
115 return (qdev_class_get_alias(dc) != NULL);
118 static void qdev_print_devinfo(DeviceClass *dc)
120 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
121 if (dc->bus_type) {
122 qemu_printf(", bus %s", dc->bus_type);
124 if (qdev_class_has_alias(dc)) {
125 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
127 if (dc->desc) {
128 qemu_printf(", desc \"%s\"", dc->desc);
130 if (!dc->user_creatable) {
131 qemu_printf(", no-user");
133 qemu_printf("\n");
136 static void qdev_print_devinfos(bool show_no_user)
138 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
139 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
140 [DEVICE_CATEGORY_USB] = "USB",
141 [DEVICE_CATEGORY_STORAGE] = "Storage",
142 [DEVICE_CATEGORY_NETWORK] = "Network",
143 [DEVICE_CATEGORY_INPUT] = "Input",
144 [DEVICE_CATEGORY_DISPLAY] = "Display",
145 [DEVICE_CATEGORY_SOUND] = "Sound",
146 [DEVICE_CATEGORY_MISC] = "Misc",
147 [DEVICE_CATEGORY_CPU] = "CPU",
148 [DEVICE_CATEGORY_MAX] = "Uncategorized",
150 GSList *list, *elt;
151 int i;
152 bool cat_printed;
154 module_load_qom_all();
155 list = object_class_get_list_sorted(TYPE_DEVICE, false);
157 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
158 cat_printed = false;
159 for (elt = list; elt; elt = elt->next) {
160 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
161 TYPE_DEVICE);
162 if ((i < DEVICE_CATEGORY_MAX
163 ? !test_bit(i, dc->categories)
164 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
165 || (!show_no_user
166 && !dc->user_creatable)) {
167 continue;
169 if (!cat_printed) {
170 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
171 cat_printed = true;
173 qdev_print_devinfo(dc);
177 g_slist_free(list);
180 static int set_property(void *opaque, const char *name, const char *value,
181 Error **errp)
183 Object *obj = opaque;
185 if (strcmp(name, "driver") == 0)
186 return 0;
187 if (strcmp(name, "bus") == 0)
188 return 0;
190 if (!object_property_parse(obj, name, value, errp)) {
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 = module_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 = module_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 "a non-abstract device type");
243 return NULL;
246 dc = DEVICE_CLASS(oc);
247 if (!dc->user_creatable ||
248 (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
250 "a 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;
264 GPtrArray *array;
265 int i;
267 driver = qemu_opt_get(opts, "driver");
268 if (driver && is_help_option(driver)) {
269 qdev_print_devinfos(false);
270 return 1;
273 if (!driver || !qemu_opt_has_help_opt(opts)) {
274 return 0;
277 if (!object_class_by_name(driver)) {
278 const char *typename = find_typename_by_alias(driver);
280 if (typename) {
281 driver = typename;
285 prop_list = qmp_device_list_properties(driver, &local_err);
286 if (local_err) {
287 goto error;
290 if (prop_list) {
291 qemu_printf("%s options:\n", driver);
292 } else {
293 qemu_printf("There are no options for %s.\n", driver);
295 array = g_ptr_array_new();
296 for (prop = prop_list; prop; prop = prop->next) {
297 g_ptr_array_add(array,
298 object_property_help(prop->value->name,
299 prop->value->type,
300 prop->value->default_value,
301 prop->value->description));
303 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
304 for (i = 0; i < array->len; i++) {
305 qemu_printf("%s\n", (char *)array->pdata[i]);
307 g_ptr_array_set_free_func(array, g_free);
308 g_ptr_array_free(array, true);
309 qapi_free_ObjectPropertyInfoList(prop_list);
310 return 1;
312 error:
313 error_report_err(local_err);
314 return 1;
317 static Object *qdev_get_peripheral(void)
319 static Object *dev;
321 if (dev == NULL) {
322 dev = container_get(qdev_get_machine(), "/peripheral");
325 return dev;
328 static Object *qdev_get_peripheral_anon(void)
330 static Object *dev;
332 if (dev == NULL) {
333 dev = container_get(qdev_get_machine(), "/peripheral-anon");
336 return dev;
339 static void qbus_error_append_bus_list_hint(DeviceState *dev,
340 Error *const *errp)
342 BusState *child;
343 const char *sep = " ";
345 error_append_hint(errp, "child buses at \"%s\":",
346 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
347 QLIST_FOREACH(child, &dev->child_bus, sibling) {
348 error_append_hint(errp, "%s\"%s\"", sep, child->name);
349 sep = ", ";
351 error_append_hint(errp, "\n");
354 static void qbus_error_append_dev_list_hint(BusState *bus,
355 Error *const *errp)
357 BusChild *kid;
358 const char *sep = " ";
360 error_append_hint(errp, "devices at \"%s\":", bus->name);
361 QTAILQ_FOREACH(kid, &bus->children, sibling) {
362 DeviceState *dev = kid->child;
363 error_append_hint(errp, "%s\"%s\"", sep,
364 object_get_typename(OBJECT(dev)));
365 if (dev->id) {
366 error_append_hint(errp, "/\"%s\"", dev->id);
368 sep = ", ";
370 error_append_hint(errp, "\n");
373 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
375 BusState *child;
377 QLIST_FOREACH(child, &dev->child_bus, sibling) {
378 if (strcmp(child->name, elem) == 0) {
379 return child;
382 return NULL;
385 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
387 BusChild *kid;
390 * try to match in order:
391 * (1) instance id, if present
392 * (2) driver name
393 * (3) driver alias, if present
395 QTAILQ_FOREACH(kid, &bus->children, sibling) {
396 DeviceState *dev = kid->child;
397 if (dev->id && strcmp(dev->id, elem) == 0) {
398 return dev;
401 QTAILQ_FOREACH(kid, &bus->children, sibling) {
402 DeviceState *dev = kid->child;
403 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
404 return dev;
407 QTAILQ_FOREACH(kid, &bus->children, sibling) {
408 DeviceState *dev = kid->child;
409 DeviceClass *dc = DEVICE_GET_CLASS(dev);
411 if (qdev_class_has_alias(dc) &&
412 strcmp(qdev_class_get_alias(dc), elem) == 0) {
413 return dev;
416 return NULL;
419 static inline bool qbus_is_full(BusState *bus)
421 BusClass *bus_class = BUS_GET_CLASS(bus);
422 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
426 * Search the tree rooted at @bus for a bus.
427 * If @name, search for a bus with that name. Note that bus names
428 * need not be unique. Yes, that's screwed up.
429 * Else search for a bus that is a subtype of @bus_typename.
430 * If more than one exists, prefer one that can take another device.
431 * Return the bus if found, else %NULL.
433 static BusState *qbus_find_recursive(BusState *bus, const char *name,
434 const char *bus_typename)
436 BusChild *kid;
437 BusState *pick, *child, *ret;
438 bool match;
440 assert(name || bus_typename);
441 if (name) {
442 match = !strcmp(bus->name, name);
443 } else {
444 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
447 if (match && !qbus_is_full(bus)) {
448 return bus; /* root matches and isn't full */
451 pick = match ? bus : NULL;
453 QTAILQ_FOREACH(kid, &bus->children, sibling) {
454 DeviceState *dev = kid->child;
455 QLIST_FOREACH(child, &dev->child_bus, sibling) {
456 ret = qbus_find_recursive(child, name, bus_typename);
457 if (ret && !qbus_is_full(ret)) {
458 return ret; /* a descendant matches and isn't full */
460 if (ret && !pick) {
461 pick = ret;
466 /* root or a descendant matches, but is full */
467 return pick;
470 static BusState *qbus_find(const char *path, Error **errp)
472 DeviceState *dev;
473 BusState *bus;
474 char elem[128];
475 int pos, len;
477 /* find start element */
478 if (path[0] == '/') {
479 bus = sysbus_get_default();
480 pos = 0;
481 } else {
482 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
483 assert(!path[0]);
484 elem[0] = len = 0;
486 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
487 if (!bus) {
488 error_setg(errp, "Bus '%s' not found", elem);
489 return NULL;
491 pos = len;
494 for (;;) {
495 assert(path[pos] == '/' || !path[pos]);
496 while (path[pos] == '/') {
497 pos++;
499 if (path[pos] == '\0') {
500 break;
503 /* find device */
504 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
505 g_assert_not_reached();
506 elem[0] = len = 0;
508 pos += len;
509 dev = qbus_find_dev(bus, elem);
510 if (!dev) {
511 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
512 "Device '%s' not found", elem);
513 qbus_error_append_dev_list_hint(bus, errp);
514 return NULL;
517 assert(path[pos] == '/' || !path[pos]);
518 while (path[pos] == '/') {
519 pos++;
521 if (path[pos] == '\0') {
522 /* last specified element is a device. If it has exactly
523 * one child bus accept it nevertheless */
524 if (dev->num_child_bus == 1) {
525 bus = QLIST_FIRST(&dev->child_bus);
526 break;
528 if (dev->num_child_bus) {
529 error_setg(errp, "Device '%s' has multiple child buses",
530 elem);
531 qbus_error_append_bus_list_hint(dev, errp);
532 } else {
533 error_setg(errp, "Device '%s' has no child bus", elem);
535 return NULL;
538 /* find bus */
539 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
540 g_assert_not_reached();
541 elem[0] = len = 0;
543 pos += len;
544 bus = qbus_find_bus(dev, elem);
545 if (!bus) {
546 error_setg(errp, "Bus '%s' not found", elem);
547 qbus_error_append_bus_list_hint(dev, errp);
548 return NULL;
552 if (qbus_is_full(bus)) {
553 error_setg(errp, "Bus '%s' is full", path);
554 return NULL;
556 return bus;
559 void qdev_set_id(DeviceState *dev, const char *id)
561 if (id) {
562 dev->id = id;
565 if (dev->id) {
566 object_property_add_child(qdev_get_peripheral(), dev->id,
567 OBJECT(dev));
568 } else {
569 static int anon_count;
570 gchar *name = g_strdup_printf("device[%d]", anon_count++);
571 object_property_add_child(qdev_get_peripheral_anon(), name,
572 OBJECT(dev));
573 g_free(name);
577 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
579 DeviceClass *dc;
580 const char *driver, *path;
581 DeviceState *dev = NULL;
582 BusState *bus = NULL;
584 driver = qemu_opt_get(opts, "driver");
585 if (!driver) {
586 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
587 return NULL;
590 /* find driver */
591 dc = qdev_get_device_class(&driver, errp);
592 if (!dc) {
593 return NULL;
596 /* find bus */
597 path = qemu_opt_get(opts, "bus");
598 if (path != NULL) {
599 bus = qbus_find(path, errp);
600 if (!bus) {
601 return NULL;
603 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
604 error_setg(errp, "Device '%s' can't go on %s bus",
605 driver, object_get_typename(OBJECT(bus)));
606 return NULL;
608 } else if (dc->bus_type != NULL) {
609 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
610 if (!bus || qbus_is_full(bus)) {
611 error_setg(errp, "No '%s' bus found for device '%s'",
612 dc->bus_type, driver);
613 return NULL;
617 if (qemu_opt_get(opts, "failover_pair_id")) {
618 if (!opts->id) {
619 error_setg(errp, "Device with failover_pair_id don't have id");
620 return NULL;
622 if (qdev_should_hide_device(opts)) {
623 if (bus && !qbus_is_hotpluggable(bus)) {
624 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
626 return NULL;
630 if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
631 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
632 return NULL;
635 if (!migration_is_idle()) {
636 error_setg(errp, "device_add not allowed while migrating");
637 return NULL;
640 /* create device */
641 dev = qdev_new(driver);
643 /* Check whether the hotplug is allowed by the machine */
644 if (phase_check(PHASE_MACHINE_READY)) {
645 if (!qdev_hotplug_allowed(dev, errp)) {
646 goto err_del_dev;
649 if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
650 /* No bus, no machine hotplug handler --> device is not hotpluggable */
651 error_setg(errp, "Device '%s' can not be hotplugged on this machine",
652 driver);
653 goto err_del_dev;
657 qdev_set_id(dev, qemu_opts_id(opts));
659 /* set properties */
660 if (qemu_opt_foreach(opts, set_property, dev, errp)) {
661 goto err_del_dev;
664 dev->opts = opts;
665 if (!qdev_realize(DEVICE(dev), bus, errp)) {
666 dev->opts = NULL;
667 goto err_del_dev;
669 return dev;
671 err_del_dev:
672 if (dev) {
673 object_unparent(OBJECT(dev));
674 object_unref(OBJECT(dev));
676 return NULL;
680 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
681 static void qbus_print(Monitor *mon, BusState *bus, int indent);
683 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
684 int indent)
686 if (!props)
687 return;
688 for (; props->name; props++) {
689 char *value;
690 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
692 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
693 value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
694 } else {
695 value = object_property_print(OBJECT(dev), props->name, true,
696 NULL);
698 g_free(legacy_name);
700 if (!value) {
701 continue;
703 qdev_printf("%s = %s\n", props->name,
704 *value ? value : "<null>");
705 g_free(value);
709 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
711 BusClass *bc = BUS_GET_CLASS(bus);
713 if (bc->print_dev) {
714 bc->print_dev(mon, dev, indent);
718 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
720 ObjectClass *class;
721 BusState *child;
722 NamedGPIOList *ngl;
723 NamedClockList *ncl;
725 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
726 dev->id ? dev->id : "");
727 indent += 2;
728 QLIST_FOREACH(ngl, &dev->gpios, node) {
729 if (ngl->num_in) {
730 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
731 ngl->num_in);
733 if (ngl->num_out) {
734 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
735 ngl->num_out);
738 QLIST_FOREACH(ncl, &dev->clocks, node) {
739 g_autofree char *freq_str = clock_display_freq(ncl->clock);
740 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
741 ncl->output ? "out" : "in",
742 ncl->alias ? " (alias)" : "",
743 ncl->name, freq_str);
745 class = object_get_class(OBJECT(dev));
746 do {
747 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
748 class = object_class_get_parent(class);
749 } while (class != object_class_by_name(TYPE_DEVICE));
750 bus_print_dev(dev->parent_bus, mon, dev, indent);
751 QLIST_FOREACH(child, &dev->child_bus, sibling) {
752 qbus_print(mon, child, indent);
756 static void qbus_print(Monitor *mon, BusState *bus, int indent)
758 BusChild *kid;
760 qdev_printf("bus: %s\n", bus->name);
761 indent += 2;
762 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
763 QTAILQ_FOREACH(kid, &bus->children, sibling) {
764 DeviceState *dev = kid->child;
765 qdev_print(mon, dev, indent);
768 #undef qdev_printf
770 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
772 if (sysbus_get_default())
773 qbus_print(mon, sysbus_get_default(), 0);
776 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
778 qdev_print_devinfos(true);
781 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
783 QemuOpts *opts;
784 DeviceState *dev;
786 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
787 if (!opts) {
788 return;
790 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
791 qemu_opts_del(opts);
792 return;
794 dev = qdev_device_add(opts, errp);
797 * Drain all pending RCU callbacks. This is done because
798 * some bus related operations can delay a device removal
799 * (in this case this can happen if device is added and then
800 * removed due to a configuration error)
801 * to a RCU callback, but user might expect that this interface
802 * will finish its job completely once qmp command returns result
803 * to the user
805 drain_call_rcu();
807 if (!dev) {
808 qemu_opts_del(opts);
809 return;
811 object_unref(OBJECT(dev));
814 static DeviceState *find_device_state(const char *id, Error **errp)
816 Object *obj;
818 if (id[0] == '/') {
819 obj = object_resolve_path(id, NULL);
820 } else {
821 char *root_path = object_get_canonical_path(qdev_get_peripheral());
822 char *path = g_strdup_printf("%s/%s", root_path, id);
824 g_free(root_path);
825 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
826 g_free(path);
829 if (!obj) {
830 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
831 "Device '%s' not found", id);
832 return NULL;
835 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
836 error_setg(errp, "%s is not a hotpluggable device", id);
837 return NULL;
840 return DEVICE(obj);
843 void qdev_unplug(DeviceState *dev, Error **errp)
845 DeviceClass *dc = DEVICE_GET_CLASS(dev);
846 HotplugHandler *hotplug_ctrl;
847 HotplugHandlerClass *hdc;
848 Error *local_err = NULL;
850 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
851 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
852 return;
855 if (!dc->hotpluggable) {
856 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
857 object_get_typename(OBJECT(dev)));
858 return;
861 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
862 error_setg(errp, "device_del not allowed while migrating");
863 return;
866 qdev_hot_removed = true;
868 hotplug_ctrl = qdev_get_hotplug_handler(dev);
869 /* hotpluggable device MUST have HotplugHandler, if it doesn't
870 * then something is very wrong with it */
871 g_assert(hotplug_ctrl);
873 /* If device supports async unplug just request it to be done,
874 * otherwise just remove it synchronously */
875 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
876 if (hdc->unplug_request) {
877 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
878 } else {
879 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
880 if (!local_err) {
881 object_unparent(OBJECT(dev));
884 error_propagate(errp, local_err);
887 void qmp_device_del(const char *id, Error **errp)
889 DeviceState *dev = find_device_state(id, errp);
890 if (dev != NULL) {
891 if (dev->pending_deleted_event) {
892 error_setg(errp, "Device %s is already in the "
893 "process of unplug", id);
894 return;
897 qdev_unplug(dev, errp);
901 void hmp_device_add(Monitor *mon, const QDict *qdict)
903 Error *err = NULL;
905 qmp_device_add((QDict *)qdict, NULL, &err);
906 hmp_handle_error(mon, err);
909 void hmp_device_del(Monitor *mon, const QDict *qdict)
911 const char *id = qdict_get_str(qdict, "id");
912 Error *err = NULL;
914 qmp_device_del(id, &err);
915 hmp_handle_error(mon, err);
918 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
920 DeviceState *dev;
921 BlockBackend *blk;
923 dev = find_device_state(id, errp);
924 if (dev == NULL) {
925 return NULL;
928 blk = blk_by_dev(dev);
929 if (!blk) {
930 error_setg(errp, "Device does not have a block device backend");
932 return blk;
935 QemuOptsList qemu_device_opts = {
936 .name = "device",
937 .implied_opt_name = "driver",
938 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
939 .desc = {
941 * no elements => accept any
942 * sanity checking will happen later
943 * when setting device properties
945 { /* end of list */ }
949 QemuOptsList qemu_global_opts = {
950 .name = "global",
951 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
952 .desc = {
954 .name = "driver",
955 .type = QEMU_OPT_STRING,
957 .name = "property",
958 .type = QEMU_OPT_STRING,
960 .name = "value",
961 .type = QEMU_OPT_STRING,
963 { /* end of list */ }
967 int qemu_global_option(const char *str)
969 char driver[64], property[64];
970 QemuOpts *opts;
971 int rc, offset;
973 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
974 if (rc == 2 && str[offset] == '=') {
975 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
976 qemu_opt_set(opts, "driver", driver, &error_abort);
977 qemu_opt_set(opts, "property", property, &error_abort);
978 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
979 return 0;
982 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
983 if (!opts) {
984 return -1;
987 return 0;
990 bool qmp_command_available(const QmpCommand *cmd, Error **errp)
992 if (!phase_check(PHASE_MACHINE_READY) &&
993 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
994 error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
995 cmd->name);
996 return false;
998 return true;