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_class_devinfo(DeviceClass *dc)
80 DeviceCategory category;
86 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
88 error_printf(", bus %s", dc->bus_type);
90 if (qdev_class_has_alias(dc)) {
91 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
93 error_printf(", categories");
94 for (category = 0; category < DEVICE_CATEGORY_MAX; ++category) {
95 if (test_bit(category, dc->categories)) {
96 error_printf(" \"%s\"", qdev_category_get_name(category));
100 error_printf(", desc \"%s\"", dc->desc);
103 error_printf(", no-user");
108 static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
112 dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
114 qdev_print_class_devinfo(dc);
117 static int set_property(const char *name, const char *value, void *opaque)
119 DeviceState *dev = opaque;
122 if (strcmp(name, "driver") == 0)
124 if (strcmp(name, "bus") == 0)
127 qdev_prop_parse(dev, name, value, &err);
129 qerror_report_err(err);
136 static const char *find_typename_by_alias(const char *alias)
140 for (i = 0; qdev_alias_table[i].alias; i++) {
141 if (qdev_alias_table[i].arch_mask &&
142 !(qdev_alias_table[i].arch_mask & arch_type)) {
146 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
147 return qdev_alias_table[i].typename;
154 static void qdev_print_category_devices(DeviceCategory category)
159 list = object_class_get_list(TYPE_DEVICE, false);
160 for (curr = list; curr; curr = g_slist_next(curr)) {
161 dc = (DeviceClass *)object_class_dynamic_cast(curr->data, TYPE_DEVICE);
162 if (!dc->no_user && test_bit(category, dc->categories)) {
163 qdev_print_class_devinfo(dc);
169 int qdev_device_help(QemuOpts *opts)
175 driver = qemu_opt_get(opts, "driver");
176 if (driver && is_help_option(driver)) {
177 DeviceCategory category;
178 for (category = 0; category < DEVICE_CATEGORY_MAX; ++category) {
179 qdev_print_category_devices(category);
185 if (!driver || !qemu_opt_has_help_opt(opts)) {
189 klass = object_class_by_name(driver);
191 const char *typename = find_typename_by_alias(driver);
195 klass = object_class_by_name(driver);
203 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
205 * TODO Properties without a parser are just for dirty hacks.
206 * qdev_prop_ptr is the only such PropertyInfo. It's marked
207 * for removal. This conditional should be removed along with
210 if (!prop->info->set) {
211 continue; /* no way to set it, don't show */
213 error_printf("%s.%s=%s\n", driver, prop->name,
214 prop->info->legacy_name ?: prop->info->name);
216 klass = object_class_get_parent(klass);
217 } while (klass != object_class_by_name(TYPE_DEVICE));
221 static Object *qdev_get_peripheral(void)
226 dev = container_get(qdev_get_machine(), "/peripheral");
232 static Object *qdev_get_peripheral_anon(void)
237 dev = container_get(qdev_get_machine(), "/peripheral-anon");
243 static void qbus_list_bus(DeviceState *dev)
246 const char *sep = " ";
248 error_printf("child busses at \"%s\":",
249 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
250 QLIST_FOREACH(child, &dev->child_bus, sibling) {
251 error_printf("%s\"%s\"", sep, child->name);
257 static void qbus_list_dev(BusState *bus)
260 const char *sep = " ";
262 error_printf("devices at \"%s\":", bus->name);
263 QTAILQ_FOREACH(kid, &bus->children, sibling) {
264 DeviceState *dev = kid->child;
265 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
267 error_printf("/\"%s\"", dev->id);
273 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
277 QLIST_FOREACH(child, &dev->child_bus, sibling) {
278 if (strcmp(child->name, elem) == 0) {
285 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
290 * try to match in order:
291 * (1) instance id, if present
293 * (3) driver alias, if present
295 QTAILQ_FOREACH(kid, &bus->children, sibling) {
296 DeviceState *dev = kid->child;
297 if (dev->id && strcmp(dev->id, elem) == 0) {
301 QTAILQ_FOREACH(kid, &bus->children, sibling) {
302 DeviceState *dev = kid->child;
303 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
307 QTAILQ_FOREACH(kid, &bus->children, sibling) {
308 DeviceState *dev = kid->child;
309 DeviceClass *dc = DEVICE_GET_CLASS(dev);
311 if (qdev_class_has_alias(dc) &&
312 strcmp(qdev_class_get_alias(dc), elem) == 0) {
319 static BusState *qbus_find_recursive(BusState *bus, const char *name,
320 const char *bus_typename)
322 BusClass *bus_class = BUS_GET_CLASS(bus);
324 BusState *child, *ret;
327 if (name && (strcmp(bus->name, name) != 0)) {
329 } else if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) {
331 } else if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) {
333 /* bus was explicitly specified: return an error. */
334 qerror_report(ERROR_CLASS_GENERIC_ERROR, "Bus '%s' is full",
338 /* bus was not specified: try to find another one. */
346 QTAILQ_FOREACH(kid, &bus->children, sibling) {
347 DeviceState *dev = kid->child;
348 QLIST_FOREACH(child, &dev->child_bus, sibling) {
349 ret = qbus_find_recursive(child, name, bus_typename);
358 static BusState *qbus_find(const char *path)
365 /* find start element */
366 if (path[0] == '/') {
367 bus = sysbus_get_default();
370 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
374 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
376 qerror_report(QERR_BUS_NOT_FOUND, elem);
383 assert(path[pos] == '/' || !path[pos]);
384 while (path[pos] == '/') {
387 if (path[pos] == '\0') {
392 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
393 g_assert_not_reached();
397 dev = qbus_find_dev(bus, elem);
399 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
400 if (!monitor_cur_is_qmp()) {
406 assert(path[pos] == '/' || !path[pos]);
407 while (path[pos] == '/') {
410 if (path[pos] == '\0') {
411 /* last specified element is a device. If it has exactly
412 * one child bus accept it nevertheless */
413 switch (dev->num_child_bus) {
415 qerror_report(QERR_DEVICE_NO_BUS, elem);
418 return QLIST_FIRST(&dev->child_bus);
420 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
421 if (!monitor_cur_is_qmp()) {
429 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
430 g_assert_not_reached();
434 bus = qbus_find_bus(dev, elem);
436 qerror_report(QERR_BUS_NOT_FOUND, elem);
437 if (!monitor_cur_is_qmp()) {
445 DeviceState *qdev_device_add(QemuOpts *opts)
449 const char *driver, *path, *id;
451 BusState *bus = NULL;
453 driver = qemu_opt_get(opts, "driver");
455 qerror_report(QERR_MISSING_PARAMETER, "driver");
460 obj = object_class_by_name(driver);
462 const char *typename = find_typename_by_alias(driver);
466 obj = object_class_by_name(driver);
471 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
475 k = DEVICE_CLASS(obj);
478 path = qemu_opt_get(opts, "bus");
480 bus = qbus_find(path);
484 if (!object_dynamic_cast(OBJECT(bus), k->bus_type)) {
485 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
486 driver, object_get_typename(OBJECT(bus)));
489 } else if (k->bus_type != NULL) {
490 bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
492 qerror_report(QERR_NO_BUS_FOR_DEVICE,
493 k->bus_type, driver);
497 if (qdev_hotplug && bus && !bus->allow_hotplug) {
498 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
502 /* create device, set properties */
503 qdev = DEVICE(object_new(driver));
506 qdev_set_parent_bus(qdev, bus);
509 id = qemu_opts_id(opts);
513 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
518 object_property_add_child(qdev_get_peripheral(), qdev->id,
521 static int anon_count;
522 gchar *name = g_strdup_printf("device[%d]", anon_count++);
523 object_property_add_child(qdev_get_peripheral_anon(), name,
527 if (qdev_init(qdev) < 0) {
528 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
536 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
537 static void qbus_print(Monitor *mon, BusState *bus, int indent);
539 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
544 for (; props->name; props++) {
547 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
548 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
549 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
551 value = object_property_print(OBJECT(dev), props->name, &err);
559 qdev_printf("%s = %s\n", props->name,
560 value && *value ? value : "<null>");
565 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
567 BusClass *bc = BUS_GET_CLASS(bus);
570 bc->print_dev(mon, dev, indent);
574 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
578 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
579 dev->id ? dev->id : "");
581 if (dev->num_gpio_in) {
582 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
584 if (dev->num_gpio_out) {
585 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
587 class = object_get_class(OBJECT(dev));
589 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
590 class = object_class_get_parent(class);
591 } while (class != object_class_by_name(TYPE_DEVICE));
592 bus_print_dev(dev->parent_bus, mon, dev, indent);
593 QLIST_FOREACH(child, &dev->child_bus, sibling) {
594 qbus_print(mon, child, indent);
598 static void qbus_print(Monitor *mon, BusState *bus, int indent)
602 qdev_printf("bus: %s\n", bus->name);
604 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
605 QTAILQ_FOREACH(kid, &bus->children, sibling) {
606 DeviceState *dev = kid->child;
607 qdev_print(mon, dev, indent);
612 void do_info_qtree(Monitor *mon, const QDict *qdict)
614 if (sysbus_get_default())
615 qbus_print(mon, sysbus_get_default(), 0);
618 void do_info_qdm(Monitor *mon, const QDict *qdict)
620 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
623 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
625 Error *local_err = NULL;
629 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
630 if (error_is_set(&local_err)) {
631 qerror_report_err(local_err);
632 error_free(local_err);
635 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
639 dev = qdev_device_add(opts);
644 object_unref(OBJECT(dev));
648 void qmp_device_del(const char *id, Error **errp)
652 dev = qdev_find_recursive(sysbus_get_default(), id);
654 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
658 qdev_unplug(dev, errp);
661 void qdev_machine_init(void)
663 qdev_get_peripheral_anon();
664 qdev_get_peripheral();
667 QemuOptsList qemu_device_opts = {
669 .implied_opt_name = "driver",
670 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
673 * no elements => accept any
674 * sanity checking will happen later
675 * when setting device properties
677 { /* end of list */ }
681 QemuOptsList qemu_global_opts = {
683 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
687 .type = QEMU_OPT_STRING,
690 .type = QEMU_OPT_STRING,
693 .type = QEMU_OPT_STRING,
695 { /* end of list */ }
699 int qemu_global_option(const char *str)
701 char driver[64], property[64];
705 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
706 if (rc < 2 || str[offset] != '=') {
707 error_report("can't parse: \"%s\"", str);
711 opts = qemu_opts_create_nofail(&qemu_global_opts);
712 qemu_opt_set(opts, "driver", driver);
713 qemu_opt_set(opts, "property", property);
714 qemu_opt_set(opts, "value", str+offset+1);