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/>.
21 #include "monitor/monitor.h"
22 #include "monitor/qdev.h"
23 #include "qmp-commands.h"
24 #include "sysemu/arch_init.h"
25 #include "qemu/config-file.h"
28 * Aliases were a bad idea from the start. Let's keep them
29 * from spreading further.
31 typedef struct QDevAlias
38 static const QDevAlias qdev_alias_table[] = {
39 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
40 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
41 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
42 { "virtio-balloon-pci", "virtio-balloon",
43 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
44 { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X },
45 { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X },
46 { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X },
47 { "lsi53c895a", "lsi" },
48 { "ich9-ahci", "ahci" },
49 { "kvm-pci-assign", "pci-assign" },
53 static const char *qdev_class_get_alias(DeviceClass *dc)
55 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
58 for (i = 0; qdev_alias_table[i].typename; i++) {
59 if (qdev_alias_table[i].arch_mask &&
60 !(qdev_alias_table[i].arch_mask & arch_type)) {
64 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
65 return qdev_alias_table[i].alias;
72 static bool qdev_class_has_alias(DeviceClass *dc)
74 return (qdev_class_get_alias(dc) != NULL);
77 static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
80 bool *show_no_user = opaque;
82 dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
84 if (!dc || (show_no_user && !*show_no_user && dc->no_user)) {
88 error_printf("name \"%s\"", object_class_get_name(klass));
90 error_printf(", bus %s", dc->bus_type);
92 if (qdev_class_has_alias(dc)) {
93 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
96 error_printf(", desc \"%s\"", dc->desc);
99 error_printf(", no-user");
104 static int set_property(const char *name, const char *value, void *opaque)
106 DeviceState *dev = opaque;
108 if (strcmp(name, "driver") == 0)
110 if (strcmp(name, "bus") == 0)
113 if (qdev_prop_parse(dev, name, value) == -1) {
119 static const char *find_typename_by_alias(const char *alias)
123 for (i = 0; qdev_alias_table[i].alias; i++) {
124 if (qdev_alias_table[i].arch_mask &&
125 !(qdev_alias_table[i].arch_mask & arch_type)) {
129 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
130 return qdev_alias_table[i].typename;
137 int qdev_device_help(QemuOpts *opts)
143 driver = qemu_opt_get(opts, "driver");
144 if (driver && is_help_option(driver)) {
145 bool show_no_user = false;
146 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user);
150 if (!driver || !qemu_opt_has_help_opt(opts)) {
154 klass = object_class_by_name(driver);
156 const char *typename = find_typename_by_alias(driver);
160 klass = object_class_by_name(driver);
168 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
170 * TODO Properties without a parser are just for dirty hacks.
171 * qdev_prop_ptr is the only such PropertyInfo. It's marked
172 * for removal. This conditional should be removed along with
175 if (!prop->info->set) {
176 continue; /* no way to set it, don't show */
178 error_printf("%s.%s=%s\n", driver, prop->name,
179 prop->info->legacy_name ?: prop->info->name);
181 klass = object_class_get_parent(klass);
182 } while (klass != object_class_by_name(TYPE_DEVICE));
186 static Object *qdev_get_peripheral(void)
191 dev = container_get(qdev_get_machine(), "/peripheral");
197 static Object *qdev_get_peripheral_anon(void)
202 dev = container_get(qdev_get_machine(), "/peripheral-anon");
208 static void qbus_list_bus(DeviceState *dev)
211 const char *sep = " ";
213 error_printf("child busses at \"%s\":",
214 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
215 QLIST_FOREACH(child, &dev->child_bus, sibling) {
216 error_printf("%s\"%s\"", sep, child->name);
222 static void qbus_list_dev(BusState *bus)
225 const char *sep = " ";
227 error_printf("devices at \"%s\":", bus->name);
228 QTAILQ_FOREACH(kid, &bus->children, sibling) {
229 DeviceState *dev = kid->child;
230 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
232 error_printf("/\"%s\"", dev->id);
238 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
242 QLIST_FOREACH(child, &dev->child_bus, sibling) {
243 if (strcmp(child->name, elem) == 0) {
250 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
255 * try to match in order:
256 * (1) instance id, if present
258 * (3) driver alias, if present
260 QTAILQ_FOREACH(kid, &bus->children, sibling) {
261 DeviceState *dev = kid->child;
262 if (dev->id && strcmp(dev->id, elem) == 0) {
266 QTAILQ_FOREACH(kid, &bus->children, sibling) {
267 DeviceState *dev = kid->child;
268 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
272 QTAILQ_FOREACH(kid, &bus->children, sibling) {
273 DeviceState *dev = kid->child;
274 DeviceClass *dc = DEVICE_GET_CLASS(dev);
276 if (qdev_class_has_alias(dc) &&
277 strcmp(qdev_class_get_alias(dc), elem) == 0) {
284 static BusState *qbus_find_recursive(BusState *bus, const char *name,
285 const char *bus_typename)
287 BusClass *bus_class = BUS_GET_CLASS(bus);
289 BusState *child, *ret;
292 if (name && (strcmp(bus->name, name) != 0)) {
295 if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) {
298 if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) {
300 /* bus was explicitly specified: return an error. */
301 qerror_report(ERROR_CLASS_GENERIC_ERROR, "Bus '%s' is full",
305 /* bus was not specified: try to find another one. */
313 QTAILQ_FOREACH(kid, &bus->children, sibling) {
314 DeviceState *dev = kid->child;
315 QLIST_FOREACH(child, &dev->child_bus, sibling) {
316 ret = qbus_find_recursive(child, name, bus_typename);
325 static BusState *qbus_find(const char *path)
332 /* find start element */
333 if (path[0] == '/') {
334 bus = sysbus_get_default();
337 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
341 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
343 qerror_report(QERR_BUS_NOT_FOUND, elem);
350 assert(path[pos] == '/' || !path[pos]);
351 while (path[pos] == '/') {
354 if (path[pos] == '\0') {
359 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
364 dev = qbus_find_dev(bus, elem);
366 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
367 if (!monitor_cur_is_qmp()) {
373 assert(path[pos] == '/' || !path[pos]);
374 while (path[pos] == '/') {
377 if (path[pos] == '\0') {
378 /* last specified element is a device. If it has exactly
379 * one child bus accept it nevertheless */
380 switch (dev->num_child_bus) {
382 qerror_report(QERR_DEVICE_NO_BUS, elem);
385 return QLIST_FIRST(&dev->child_bus);
387 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
388 if (!monitor_cur_is_qmp()) {
396 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
401 bus = qbus_find_bus(dev, elem);
403 qerror_report(QERR_BUS_NOT_FOUND, elem);
404 if (!monitor_cur_is_qmp()) {
412 DeviceState *qdev_device_add(QemuOpts *opts)
416 const char *driver, *path, *id;
420 driver = qemu_opt_get(opts, "driver");
422 qerror_report(QERR_MISSING_PARAMETER, "driver");
427 obj = object_class_by_name(driver);
429 const char *typename = find_typename_by_alias(driver);
433 obj = object_class_by_name(driver);
438 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
442 k = DEVICE_CLASS(obj);
445 path = qemu_opt_get(opts, "bus");
447 bus = qbus_find(path);
451 if (!object_dynamic_cast(OBJECT(bus), k->bus_type)) {
452 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
453 driver, object_get_typename(OBJECT(bus)));
457 bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
459 qerror_report(QERR_NO_BUS_FOR_DEVICE,
460 k->bus_type, driver);
464 if (qdev_hotplug && !bus->allow_hotplug) {
465 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
470 bus = sysbus_get_default();
473 /* create device, set properties */
474 qdev = DEVICE(object_new(driver));
475 qdev_set_parent_bus(qdev, bus);
477 id = qemu_opts_id(opts);
481 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
486 object_property_add_child(qdev_get_peripheral(), qdev->id,
489 static int anon_count;
490 gchar *name = g_strdup_printf("device[%d]", anon_count++);
491 object_property_add_child(qdev_get_peripheral_anon(), name,
495 if (qdev_init(qdev) < 0) {
496 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
504 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
505 static void qbus_print(Monitor *mon, BusState *bus, int indent);
507 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
512 for (; props->name; props++) {
515 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
516 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
517 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
519 value = object_property_print(OBJECT(dev), props->name, &err);
527 qdev_printf("%s = %s\n", props->name,
528 value && *value ? value : "<null>");
533 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
535 BusClass *bc = BUS_GET_CLASS(bus);
538 bc->print_dev(mon, dev, indent);
542 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
546 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
547 dev->id ? dev->id : "");
549 if (dev->num_gpio_in) {
550 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
552 if (dev->num_gpio_out) {
553 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
555 class = object_get_class(OBJECT(dev));
557 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
558 class = object_class_get_parent(class);
559 } while (class != object_class_by_name(TYPE_DEVICE));
560 bus_print_dev(dev->parent_bus, mon, dev, indent);
561 QLIST_FOREACH(child, &dev->child_bus, sibling) {
562 qbus_print(mon, child, indent);
566 static void qbus_print(Monitor *mon, BusState *bus, int indent)
570 qdev_printf("bus: %s\n", bus->name);
572 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
573 QTAILQ_FOREACH(kid, &bus->children, sibling) {
574 DeviceState *dev = kid->child;
575 qdev_print(mon, dev, indent);
580 void do_info_qtree(Monitor *mon, const QDict *qdict)
582 if (sysbus_get_default())
583 qbus_print(mon, sysbus_get_default(), 0);
586 void do_info_qdm(Monitor *mon, const QDict *qdict)
588 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
591 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
593 Error *local_err = NULL;
597 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
598 if (error_is_set(&local_err)) {
599 qerror_report_err(local_err);
600 error_free(local_err);
603 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
607 dev = qdev_device_add(opts);
612 object_unref(OBJECT(dev));
616 void qmp_device_del(const char *id, Error **errp)
620 dev = qdev_find_recursive(sysbus_get_default(), id);
622 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
626 qdev_unplug(dev, errp);
629 void qdev_machine_init(void)
631 qdev_get_peripheral_anon();
632 qdev_get_peripheral();
635 QemuOptsList qemu_device_opts = {
637 .implied_opt_name = "driver",
638 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
641 * no elements => accept any
642 * sanity checking will happen later
643 * when setting device properties
645 { /* end of list */ }
649 QemuOptsList qemu_global_opts = {
651 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
655 .type = QEMU_OPT_STRING,
658 .type = QEMU_OPT_STRING,
661 .type = QEMU_OPT_STRING,
663 { /* end of list */ }
667 int qemu_global_option(const char *str)
669 char driver[64], property[64];
673 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
674 if (rc < 2 || str[offset] != '=') {
675 error_report("can't parse: \"%s\"", str);
679 opts = qemu_opts_create_nofail(&qemu_global_opts);
680 qemu_opt_set(opts, "driver", driver);
681 qemu_opt_set(opts, "property", property);
682 qemu_opt_set(opts, "value", str+offset+1);