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 "hw/sysbus.h"
22 #include "monitor/monitor.h"
23 #include "monitor/qdev.h"
24 #include "qmp-commands.h"
25 #include "sysemu/arch_init.h"
26 #include "qemu/config-file.h"
29 * Aliases were a bad idea from the start. Let's keep them
30 * from spreading further.
32 typedef struct QDevAlias
39 static const QDevAlias qdev_alias_table[] = {
40 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
41 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
42 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
43 { "virtio-balloon-pci", "virtio-balloon",
44 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
45 { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X },
46 { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X },
47 { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X },
48 { "lsi53c895a", "lsi" },
49 { "ich9-ahci", "ahci" },
50 { "kvm-pci-assign", "pci-assign" },
54 static const char *qdev_class_get_alias(DeviceClass *dc)
56 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
59 for (i = 0; qdev_alias_table[i].typename; i++) {
60 if (qdev_alias_table[i].arch_mask &&
61 !(qdev_alias_table[i].arch_mask & arch_type)) {
65 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
66 return qdev_alias_table[i].alias;
73 static bool qdev_class_has_alias(DeviceClass *dc)
75 return (qdev_class_get_alias(dc) != NULL);
78 static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
81 bool *show_no_user = opaque;
83 dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
85 if (!dc || (show_no_user && !*show_no_user && dc->no_user)) {
89 error_printf("name \"%s\"", object_class_get_name(klass));
91 error_printf(", bus %s", dc->bus_type);
93 if (qdev_class_has_alias(dc)) {
94 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
97 error_printf(", desc \"%s\"", dc->desc);
100 error_printf(", no-user");
105 static int set_property(const char *name, const char *value, void *opaque)
107 DeviceState *dev = opaque;
110 if (strcmp(name, "driver") == 0)
112 if (strcmp(name, "bus") == 0)
115 qdev_prop_parse(dev, name, value, &err);
117 qerror_report_err(err);
124 static const char *find_typename_by_alias(const char *alias)
128 for (i = 0; qdev_alias_table[i].alias; i++) {
129 if (qdev_alias_table[i].arch_mask &&
130 !(qdev_alias_table[i].arch_mask & arch_type)) {
134 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
135 return qdev_alias_table[i].typename;
142 int qdev_device_help(QemuOpts *opts)
148 driver = qemu_opt_get(opts, "driver");
149 if (driver && is_help_option(driver)) {
150 bool show_no_user = false;
151 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user);
155 if (!driver || !qemu_opt_has_help_opt(opts)) {
159 klass = object_class_by_name(driver);
161 const char *typename = find_typename_by_alias(driver);
165 klass = object_class_by_name(driver);
173 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
175 * TODO Properties without a parser are just for dirty hacks.
176 * qdev_prop_ptr is the only such PropertyInfo. It's marked
177 * for removal. This conditional should be removed along with
180 if (!prop->info->set) {
181 continue; /* no way to set it, don't show */
183 error_printf("%s.%s=%s\n", driver, prop->name,
184 prop->info->legacy_name ?: prop->info->name);
186 klass = object_class_get_parent(klass);
187 } while (klass != object_class_by_name(TYPE_DEVICE));
191 static Object *qdev_get_peripheral(void)
196 dev = container_get(qdev_get_machine(), "/peripheral");
202 static Object *qdev_get_peripheral_anon(void)
207 dev = container_get(qdev_get_machine(), "/peripheral-anon");
213 static void qbus_list_bus(DeviceState *dev)
216 const char *sep = " ";
218 error_printf("child busses at \"%s\":",
219 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
220 QLIST_FOREACH(child, &dev->child_bus, sibling) {
221 error_printf("%s\"%s\"", sep, child->name);
227 static void qbus_list_dev(BusState *bus)
230 const char *sep = " ";
232 error_printf("devices at \"%s\":", bus->name);
233 QTAILQ_FOREACH(kid, &bus->children, sibling) {
234 DeviceState *dev = kid->child;
235 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
237 error_printf("/\"%s\"", dev->id);
243 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
247 QLIST_FOREACH(child, &dev->child_bus, sibling) {
248 if (strcmp(child->name, elem) == 0) {
255 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
260 * try to match in order:
261 * (1) instance id, if present
263 * (3) driver alias, if present
265 QTAILQ_FOREACH(kid, &bus->children, sibling) {
266 DeviceState *dev = kid->child;
267 if (dev->id && strcmp(dev->id, elem) == 0) {
271 QTAILQ_FOREACH(kid, &bus->children, sibling) {
272 DeviceState *dev = kid->child;
273 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
277 QTAILQ_FOREACH(kid, &bus->children, sibling) {
278 DeviceState *dev = kid->child;
279 DeviceClass *dc = DEVICE_GET_CLASS(dev);
281 if (qdev_class_has_alias(dc) &&
282 strcmp(qdev_class_get_alias(dc), elem) == 0) {
289 static BusState *qbus_find_recursive(BusState *bus, const char *name,
290 const char *bus_typename)
292 BusClass *bus_class = BUS_GET_CLASS(bus);
294 BusState *child, *ret;
297 if (name && (strcmp(bus->name, name) != 0)) {
299 } else if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) {
301 } else if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) {
303 /* bus was explicitly specified: return an error. */
304 qerror_report(ERROR_CLASS_GENERIC_ERROR, "Bus '%s' is full",
308 /* bus was not specified: try to find another one. */
316 QTAILQ_FOREACH(kid, &bus->children, sibling) {
317 DeviceState *dev = kid->child;
318 QLIST_FOREACH(child, &dev->child_bus, sibling) {
319 ret = qbus_find_recursive(child, name, bus_typename);
328 static BusState *qbus_find(const char *path)
335 /* find start element */
336 if (path[0] == '/') {
337 bus = sysbus_get_default();
340 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
344 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
346 qerror_report(QERR_BUS_NOT_FOUND, elem);
353 assert(path[pos] == '/' || !path[pos]);
354 while (path[pos] == '/') {
357 if (path[pos] == '\0') {
362 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
367 dev = qbus_find_dev(bus, elem);
369 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
370 if (!monitor_cur_is_qmp()) {
376 assert(path[pos] == '/' || !path[pos]);
377 while (path[pos] == '/') {
380 if (path[pos] == '\0') {
381 /* last specified element is a device. If it has exactly
382 * one child bus accept it nevertheless */
383 switch (dev->num_child_bus) {
385 qerror_report(QERR_DEVICE_NO_BUS, elem);
388 return QLIST_FIRST(&dev->child_bus);
390 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
391 if (!monitor_cur_is_qmp()) {
399 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
404 bus = qbus_find_bus(dev, elem);
406 qerror_report(QERR_BUS_NOT_FOUND, elem);
407 if (!monitor_cur_is_qmp()) {
415 DeviceState *qdev_device_add(QemuOpts *opts)
419 const char *driver, *path, *id;
421 BusState *bus = NULL;
423 driver = qemu_opt_get(opts, "driver");
425 qerror_report(QERR_MISSING_PARAMETER, "driver");
430 obj = object_class_by_name(driver);
432 const char *typename = find_typename_by_alias(driver);
436 obj = object_class_by_name(driver);
441 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
445 k = DEVICE_CLASS(obj);
448 path = qemu_opt_get(opts, "bus");
450 bus = qbus_find(path);
454 if (!object_dynamic_cast(OBJECT(bus), k->bus_type)) {
455 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
456 driver, object_get_typename(OBJECT(bus)));
459 } else if (k->bus_type != NULL) {
460 bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
462 qerror_report(QERR_NO_BUS_FOR_DEVICE,
463 k->bus_type, driver);
467 if (qdev_hotplug && bus && !bus->allow_hotplug) {
468 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
472 /* create device, set properties */
473 qdev = DEVICE(object_new(driver));
476 qdev_set_parent_bus(qdev, bus);
479 id = qemu_opts_id(opts);
483 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
488 object_property_add_child(qdev_get_peripheral(), qdev->id,
491 static int anon_count;
492 gchar *name = g_strdup_printf("device[%d]", anon_count++);
493 object_property_add_child(qdev_get_peripheral_anon(), name,
497 if (qdev_init(qdev) < 0) {
498 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
506 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
507 static void qbus_print(Monitor *mon, BusState *bus, int indent);
509 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
514 for (; props->name; props++) {
517 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
518 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
519 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
521 value = object_property_print(OBJECT(dev), props->name, &err);
529 qdev_printf("%s = %s\n", props->name,
530 value && *value ? value : "<null>");
535 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
537 BusClass *bc = BUS_GET_CLASS(bus);
540 bc->print_dev(mon, dev, indent);
544 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
548 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
549 dev->id ? dev->id : "");
551 if (dev->num_gpio_in) {
552 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
554 if (dev->num_gpio_out) {
555 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
557 class = object_get_class(OBJECT(dev));
559 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
560 class = object_class_get_parent(class);
561 } while (class != object_class_by_name(TYPE_DEVICE));
562 bus_print_dev(dev->parent_bus, mon, dev, indent);
563 QLIST_FOREACH(child, &dev->child_bus, sibling) {
564 qbus_print(mon, child, indent);
568 static void qbus_print(Monitor *mon, BusState *bus, int indent)
572 qdev_printf("bus: %s\n", bus->name);
574 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
575 QTAILQ_FOREACH(kid, &bus->children, sibling) {
576 DeviceState *dev = kid->child;
577 qdev_print(mon, dev, indent);
582 void do_info_qtree(Monitor *mon, const QDict *qdict)
584 if (sysbus_get_default())
585 qbus_print(mon, sysbus_get_default(), 0);
588 void do_info_qdm(Monitor *mon, const QDict *qdict)
590 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
593 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
595 Error *local_err = NULL;
599 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
600 if (error_is_set(&local_err)) {
601 qerror_report_err(local_err);
602 error_free(local_err);
605 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
609 dev = qdev_device_add(opts);
614 object_unref(OBJECT(dev));
618 void qmp_device_del(const char *id, Error **errp)
622 dev = qdev_find_recursive(sysbus_get_default(), id);
624 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
628 qdev_unplug(dev, errp);
631 void qdev_machine_init(void)
633 qdev_get_peripheral_anon();
634 qdev_get_peripheral();
637 QemuOptsList qemu_device_opts = {
639 .implied_opt_name = "driver",
640 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
643 * no elements => accept any
644 * sanity checking will happen later
645 * when setting device properties
647 { /* end of list */ }
651 QemuOptsList qemu_global_opts = {
653 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
657 .type = QEMU_OPT_STRING,
660 .type = QEMU_OPT_STRING,
663 .type = QEMU_OPT_STRING,
665 { /* end of list */ }
669 int qemu_global_option(const char *str)
671 char driver[64], property[64];
675 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
676 if (rc < 2 || str[offset] != '=') {
677 error_report("can't parse: \"%s\"", str);
681 opts = qemu_opts_create_nofail(&qemu_global_opts);
682 qemu_opt_set(opts, "driver", driver);
683 qemu_opt_set(opts, "property", property);
684 qemu_opt_set(opts, "value", str+offset+1);