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 "sysemu/block-backend.h"
36 #include "sysemu/sysemu.h"
37 #include "migration/misc.h"
40 * Aliases were a bad idea from the start. Let's keep them
41 * from spreading further.
43 typedef struct QDevAlias
50 /* Please keep this table sorted by typename. */
51 static const QDevAlias qdev_alias_table[] = {
52 { "e1000", "e1000-82540em" },
53 { "ich9-ahci", "ahci" },
54 { "lsi53c895a", "lsi" },
55 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
56 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
57 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
58 { "virtio-balloon-pci", "virtio-balloon",
59 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
60 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
61 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
62 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
63 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
64 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
65 { "virtio-input-host-pci", "virtio-input-host",
66 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
67 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
68 { "virtio-keyboard-pci", "virtio-keyboard",
69 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
70 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
71 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
72 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
73 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
74 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
75 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
76 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
77 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
78 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
79 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
80 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
81 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
85 static const char *qdev_class_get_alias(DeviceClass *dc)
87 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
90 for (i = 0; qdev_alias_table[i].typename; i++) {
91 if (qdev_alias_table[i].arch_mask &&
92 !(qdev_alias_table[i].arch_mask & arch_type)) {
96 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
97 return qdev_alias_table[i].alias;
104 static bool qdev_class_has_alias(DeviceClass *dc)
106 return (qdev_class_get_alias(dc) != NULL);
109 static void qdev_print_devinfo(DeviceClass *dc)
111 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
113 qemu_printf(", bus %s", dc->bus_type);
115 if (qdev_class_has_alias(dc)) {
116 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
119 qemu_printf(", desc \"%s\"", dc->desc);
121 if (!dc->user_creatable) {
122 qemu_printf(", no-user");
127 static void qdev_print_devinfos(bool show_no_user)
129 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
130 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
131 [DEVICE_CATEGORY_USB] = "USB",
132 [DEVICE_CATEGORY_STORAGE] = "Storage",
133 [DEVICE_CATEGORY_NETWORK] = "Network",
134 [DEVICE_CATEGORY_INPUT] = "Input",
135 [DEVICE_CATEGORY_DISPLAY] = "Display",
136 [DEVICE_CATEGORY_SOUND] = "Sound",
137 [DEVICE_CATEGORY_MISC] = "Misc",
138 [DEVICE_CATEGORY_CPU] = "CPU",
139 [DEVICE_CATEGORY_MAX] = "Uncategorized",
145 list = object_class_get_list_sorted(TYPE_DEVICE, false);
147 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
149 for (elt = list; elt; elt = elt->next) {
150 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
152 if ((i < DEVICE_CATEGORY_MAX
153 ? !test_bit(i, dc->categories)
154 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
156 && !dc->user_creatable)) {
160 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
163 qdev_print_devinfo(dc);
170 static int set_property(void *opaque, const char *name, const char *value,
173 Object *obj = opaque;
176 if (strcmp(name, "driver") == 0)
178 if (strcmp(name, "bus") == 0)
181 object_property_parse(obj, value, name, &err);
183 error_propagate(errp, err);
189 static const char *find_typename_by_alias(const char *alias)
193 for (i = 0; qdev_alias_table[i].alias; i++) {
194 if (qdev_alias_table[i].arch_mask &&
195 !(qdev_alias_table[i].arch_mask & arch_type)) {
199 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
200 return qdev_alias_table[i].typename;
207 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
211 const char *original_name = *driver;
213 oc = object_class_by_name(*driver);
215 const char *typename = find_typename_by_alias(*driver);
219 oc = object_class_by_name(*driver);
223 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
224 if (*driver != original_name) {
225 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
226 " name", original_name, *driver);
228 error_setg(errp, "'%s' is not a valid device model name", *driver);
233 if (object_class_is_abstract(oc)) {
234 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
235 "non-abstract device type");
239 dc = DEVICE_CLASS(oc);
240 if (!dc->user_creatable ||
241 (qdev_hotplug && !dc->hotpluggable)) {
242 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
243 "pluggable device type");
251 int qdev_device_help(QemuOpts *opts)
253 Error *local_err = NULL;
255 ObjectPropertyInfoList *prop_list;
256 ObjectPropertyInfoList *prop;
258 driver = qemu_opt_get(opts, "driver");
259 if (driver && is_help_option(driver)) {
260 qdev_print_devinfos(false);
264 if (!driver || !qemu_opt_has_help_opt(opts)) {
268 if (!object_class_by_name(driver)) {
269 const char *typename = find_typename_by_alias(driver);
276 prop_list = qmp_device_list_properties(driver, &local_err);
282 qemu_printf("%s options:\n", driver);
284 qemu_printf("There are no options for %s.\n", driver);
286 for (prop = prop_list; prop; prop = prop->next) {
288 qemu_printf(" %s=<%s>%n", prop->value->name, prop->value->type, &len);
289 if (prop->value->has_description) {
291 qemu_printf("%*s", 24 - len, "");
293 qemu_printf(" - %s\n", prop->value->description);
299 qapi_free_ObjectPropertyInfoList(prop_list);
303 error_report_err(local_err);
307 static Object *qdev_get_peripheral(void)
312 dev = container_get(qdev_get_machine(), "/peripheral");
318 static Object *qdev_get_peripheral_anon(void)
323 dev = container_get(qdev_get_machine(), "/peripheral-anon");
329 static void qbus_list_bus(DeviceState *dev, Error **errp)
332 const char *sep = " ";
334 error_append_hint(errp, "child buses at \"%s\":",
335 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
336 QLIST_FOREACH(child, &dev->child_bus, sibling) {
337 error_append_hint(errp, "%s\"%s\"", sep, child->name);
340 error_append_hint(errp, "\n");
343 static void qbus_list_dev(BusState *bus, Error **errp)
346 const char *sep = " ";
348 error_append_hint(errp, "devices at \"%s\":", bus->name);
349 QTAILQ_FOREACH(kid, &bus->children, sibling) {
350 DeviceState *dev = kid->child;
351 error_append_hint(errp, "%s\"%s\"", sep,
352 object_get_typename(OBJECT(dev)));
354 error_append_hint(errp, "/\"%s\"", dev->id);
358 error_append_hint(errp, "\n");
361 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
365 QLIST_FOREACH(child, &dev->child_bus, sibling) {
366 if (strcmp(child->name, elem) == 0) {
373 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
378 * try to match in order:
379 * (1) instance id, if present
381 * (3) driver alias, if present
383 QTAILQ_FOREACH(kid, &bus->children, sibling) {
384 DeviceState *dev = kid->child;
385 if (dev->id && strcmp(dev->id, elem) == 0) {
389 QTAILQ_FOREACH(kid, &bus->children, sibling) {
390 DeviceState *dev = kid->child;
391 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
395 QTAILQ_FOREACH(kid, &bus->children, sibling) {
396 DeviceState *dev = kid->child;
397 DeviceClass *dc = DEVICE_GET_CLASS(dev);
399 if (qdev_class_has_alias(dc) &&
400 strcmp(qdev_class_get_alias(dc), elem) == 0) {
407 static inline bool qbus_is_full(BusState *bus)
409 BusClass *bus_class = BUS_GET_CLASS(bus);
410 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
414 * Search the tree rooted at @bus for a bus.
415 * If @name, search for a bus with that name. Note that bus names
416 * need not be unique. Yes, that's screwed up.
417 * Else search for a bus that is a subtype of @bus_typename.
418 * If more than one exists, prefer one that can take another device.
419 * Return the bus if found, else %NULL.
421 static BusState *qbus_find_recursive(BusState *bus, const char *name,
422 const char *bus_typename)
425 BusState *pick, *child, *ret;
428 assert(name || bus_typename);
430 match = !strcmp(bus->name, name);
432 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
435 if (match && !qbus_is_full(bus)) {
436 return bus; /* root matches and isn't full */
439 pick = match ? bus : NULL;
441 QTAILQ_FOREACH(kid, &bus->children, sibling) {
442 DeviceState *dev = kid->child;
443 QLIST_FOREACH(child, &dev->child_bus, sibling) {
444 ret = qbus_find_recursive(child, name, bus_typename);
445 if (ret && !qbus_is_full(ret)) {
446 return ret; /* a descendant matches and isn't full */
454 /* root or a descendant matches, but is full */
458 static BusState *qbus_find(const char *path, Error **errp)
465 /* find start element */
466 if (path[0] == '/') {
467 bus = sysbus_get_default();
470 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
474 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
476 error_setg(errp, "Bus '%s' not found", elem);
483 assert(path[pos] == '/' || !path[pos]);
484 while (path[pos] == '/') {
487 if (path[pos] == '\0') {
492 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
493 g_assert_not_reached();
497 dev = qbus_find_dev(bus, elem);
499 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
500 "Device '%s' not found", elem);
501 qbus_list_dev(bus, errp);
505 assert(path[pos] == '/' || !path[pos]);
506 while (path[pos] == '/') {
509 if (path[pos] == '\0') {
510 /* last specified element is a device. If it has exactly
511 * one child bus accept it nevertheless */
512 if (dev->num_child_bus == 1) {
513 bus = QLIST_FIRST(&dev->child_bus);
516 if (dev->num_child_bus) {
517 error_setg(errp, "Device '%s' has multiple child buses",
519 qbus_list_bus(dev, errp);
521 error_setg(errp, "Device '%s' has no child bus", elem);
527 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
528 g_assert_not_reached();
532 bus = qbus_find_bus(dev, elem);
534 error_setg(errp, "Bus '%s' not found", elem);
535 qbus_list_bus(dev, errp);
540 if (qbus_is_full(bus)) {
541 error_setg(errp, "Bus '%s' is full", path);
547 void qdev_set_id(DeviceState *dev, const char *id)
554 object_property_add_child(qdev_get_peripheral(), dev->id,
557 static int anon_count;
558 gchar *name = g_strdup_printf("device[%d]", anon_count++);
559 object_property_add_child(qdev_get_peripheral_anon(), name,
565 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
568 const char *driver, *path;
570 BusState *bus = NULL;
573 driver = qemu_opt_get(opts, "driver");
575 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
580 dc = qdev_get_device_class(&driver, errp);
586 path = qemu_opt_get(opts, "bus");
588 bus = qbus_find(path, errp);
592 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
593 error_setg(errp, "Device '%s' can't go on %s bus",
594 driver, object_get_typename(OBJECT(bus)));
597 } else if (dc->bus_type != NULL) {
598 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
599 if (!bus || qbus_is_full(bus)) {
600 error_setg(errp, "No '%s' bus found for device '%s'",
601 dc->bus_type, driver);
605 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
606 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
610 if (!migration_is_idle()) {
611 error_setg(errp, "device_add not allowed while migrating");
616 dev = DEVICE(object_new(driver));
618 /* Check whether the hotplug is allowed by the machine */
619 if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) {
620 /* Error must be set in the machine hook */
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",
634 qdev_set_id(dev, qemu_opts_id(opts));
637 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
642 object_property_set_bool(OBJECT(dev), true, "realized", &err);
650 error_propagate(errp, err);
651 object_unparent(OBJECT(dev));
652 object_unref(OBJECT(dev));
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,
665 for (; props->name; props++) {
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);
672 value = object_property_print(OBJECT(dev), props->name, true, &err);
680 qdev_printf("%s = %s\n", props->name,
681 value && *value ? value : "<null>");
686 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
688 BusClass *bc = BUS_GET_CLASS(bus);
691 bc->print_dev(mon, dev, indent);
695 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
701 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
702 dev->id ? dev->id : "");
704 QLIST_FOREACH(ngl, &dev->gpios, node) {
706 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
710 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
714 class = object_get_class(OBJECT(dev));
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)
729 qdev_printf("bus: %s\n", bus->name);
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);
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 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
752 Error *local_err = NULL;
756 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
758 error_propagate(errp, local_err);
761 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
765 dev = qdev_device_add(opts, &local_err);
767 error_propagate(errp, local_err);
771 object_unref(OBJECT(dev));
774 static DeviceState *find_device_state(const char *id, Error **errp)
779 obj = object_resolve_path(id, NULL);
781 char *root_path = object_get_canonical_path(qdev_get_peripheral());
782 char *path = g_strdup_printf("%s/%s", root_path, id);
785 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
790 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
791 "Device '%s' not found", id);
795 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
796 error_setg(errp, "%s is not a hotpluggable device", id);
803 void qdev_unplug(DeviceState *dev, Error **errp)
805 DeviceClass *dc = DEVICE_GET_CLASS(dev);
806 HotplugHandler *hotplug_ctrl;
807 HotplugHandlerClass *hdc;
808 Error *local_err = NULL;
810 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
811 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
815 if (!dc->hotpluggable) {
816 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
817 object_get_typename(OBJECT(dev)));
821 if (!migration_is_idle()) {
822 error_setg(errp, "device_del not allowed while migrating");
826 qdev_hot_removed = true;
828 hotplug_ctrl = qdev_get_hotplug_handler(dev);
829 /* hotpluggable device MUST have HotplugHandler, if it doesn't
830 * then something is very wrong with it */
831 g_assert(hotplug_ctrl);
833 /* If device supports async unplug just request it to be done,
834 * otherwise just remove it synchronously */
835 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
836 if (hdc->unplug_request) {
837 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
839 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
841 object_unparent(OBJECT(dev));
844 error_propagate(errp, local_err);
847 void qmp_device_del(const char *id, Error **errp)
849 DeviceState *dev = find_device_state(id, errp);
851 qdev_unplug(dev, errp);
855 void hmp_device_add(Monitor *mon, const QDict *qdict)
859 qmp_device_add((QDict *)qdict, NULL, &err);
860 hmp_handle_error(mon, &err);
863 void hmp_device_del(Monitor *mon, const QDict *qdict)
865 const char *id = qdict_get_str(qdict, "id");
868 qmp_device_del(id, &err);
869 hmp_handle_error(mon, &err);
872 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
877 dev = find_device_state(id, errp);
882 blk = blk_by_dev(dev);
884 error_setg(errp, "Device does not have a block device backend");
889 void qdev_machine_init(void)
891 qdev_get_peripheral_anon();
892 qdev_get_peripheral();
895 QemuOptsList qemu_device_opts = {
897 .implied_opt_name = "driver",
898 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
901 * no elements => accept any
902 * sanity checking will happen later
903 * when setting device properties
905 { /* end of list */ }
909 QemuOptsList qemu_global_opts = {
911 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
915 .type = QEMU_OPT_STRING,
918 .type = QEMU_OPT_STRING,
921 .type = QEMU_OPT_STRING,
923 { /* end of list */ }
927 int qemu_global_option(const char *str)
929 char driver[64], property[64];
933 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
934 if (rc == 2 && str[offset] == '=') {
935 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
936 qemu_opt_set(opts, "driver", driver, &error_abort);
937 qemu_opt_set(opts, "property", property, &error_abort);
938 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
942 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);