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/>.
22 #include "qmp-commands.h"
23 #include "arch_init.h"
26 * Aliases were a bad idea from the start. Let's keep them
27 * from spreading further.
29 typedef struct QDevAlias
36 static const QDevAlias qdev_alias_table[] = {
37 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
38 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
39 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
40 { "virtio-balloon-pci", "virtio-balloon",
41 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
42 { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X },
43 { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X },
44 { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X },
45 { "lsi53c895a", "lsi" },
46 { "ich9-ahci", "ahci" },
50 static const char *qdev_class_get_alias(DeviceClass *dc)
52 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
55 for (i = 0; qdev_alias_table[i].typename; i++) {
56 if (qdev_alias_table[i].arch_mask &&
57 !(qdev_alias_table[i].arch_mask & arch_type)) {
61 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
62 return qdev_alias_table[i].alias;
69 static bool qdev_class_has_alias(DeviceClass *dc)
71 return (qdev_class_get_alias(dc) != NULL);
74 static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
77 bool *show_no_user = opaque;
79 dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
81 if (!dc || (show_no_user && !*show_no_user && dc->no_user)) {
85 error_printf("name \"%s\"", object_class_get_name(klass));
87 error_printf(", bus %s", dc->bus_type);
89 if (qdev_class_has_alias(dc)) {
90 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
93 error_printf(", desc \"%s\"", dc->desc);
96 error_printf(", no-user");
101 static int set_property(const char *name, const char *value, void *opaque)
103 DeviceState *dev = opaque;
105 if (strcmp(name, "driver") == 0)
107 if (strcmp(name, "bus") == 0)
110 if (qdev_prop_parse(dev, name, value) == -1) {
116 static const char *find_typename_by_alias(const char *alias)
120 for (i = 0; qdev_alias_table[i].alias; i++) {
121 if (qdev_alias_table[i].arch_mask &&
122 !(qdev_alias_table[i].arch_mask & arch_type)) {
126 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
127 return qdev_alias_table[i].typename;
134 int qdev_device_help(QemuOpts *opts)
140 driver = qemu_opt_get(opts, "driver");
141 if (driver && !strcmp(driver, "?")) {
142 bool show_no_user = false;
143 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user);
147 if (!driver || !qemu_opt_get(opts, "?")) {
151 klass = object_class_by_name(driver);
153 const char *typename = find_typename_by_alias(driver);
157 klass = object_class_by_name(driver);
165 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
167 * TODO Properties without a parser are just for dirty hacks.
168 * qdev_prop_ptr is the only such PropertyInfo. It's marked
169 * for removal. This conditional should be removed along with
172 if (!prop->info->set) {
173 continue; /* no way to set it, don't show */
175 error_printf("%s.%s=%s\n", driver, prop->name,
176 prop->info->legacy_name ?: prop->info->name);
178 klass = object_class_get_parent(klass);
179 } while (klass != object_class_by_name(TYPE_DEVICE));
183 static Object *qdev_get_peripheral(void)
188 dev = container_get(qdev_get_machine(), "/peripheral");
194 static Object *qdev_get_peripheral_anon(void)
199 dev = container_get(qdev_get_machine(), "/peripheral-anon");
205 static void qbus_list_bus(DeviceState *dev)
208 const char *sep = " ";
210 error_printf("child busses at \"%s\":",
211 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
212 QLIST_FOREACH(child, &dev->child_bus, sibling) {
213 error_printf("%s\"%s\"", sep, child->name);
219 static void qbus_list_dev(BusState *bus)
222 const char *sep = " ";
224 error_printf("devices at \"%s\":", bus->name);
225 QTAILQ_FOREACH(kid, &bus->children, sibling) {
226 DeviceState *dev = kid->child;
227 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
229 error_printf("/\"%s\"", dev->id);
235 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
239 QLIST_FOREACH(child, &dev->child_bus, sibling) {
240 if (strcmp(child->name, elem) == 0) {
247 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
252 * try to match in order:
253 * (1) instance id, if present
255 * (3) driver alias, if present
257 QTAILQ_FOREACH(kid, &bus->children, sibling) {
258 DeviceState *dev = kid->child;
259 if (dev->id && strcmp(dev->id, elem) == 0) {
263 QTAILQ_FOREACH(kid, &bus->children, sibling) {
264 DeviceState *dev = kid->child;
265 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
269 QTAILQ_FOREACH(kid, &bus->children, sibling) {
270 DeviceState *dev = kid->child;
271 DeviceClass *dc = DEVICE_GET_CLASS(dev);
273 if (qdev_class_has_alias(dc) &&
274 strcmp(qdev_class_get_alias(dc), elem) == 0) {
281 static BusState *qbus_find_recursive(BusState *bus, const char *name,
282 const char *bus_typename)
285 BusState *child, *ret;
288 if (name && (strcmp(bus->name, name) != 0)) {
292 (strcmp(object_get_typename(OBJECT(bus)), bus_typename) != 0)) {
299 QTAILQ_FOREACH(kid, &bus->children, sibling) {
300 DeviceState *dev = kid->child;
301 QLIST_FOREACH(child, &dev->child_bus, sibling) {
302 ret = qbus_find_recursive(child, name, bus_typename);
311 static BusState *qbus_find(const char *path)
318 /* find start element */
319 if (path[0] == '/') {
320 bus = sysbus_get_default();
323 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
327 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
329 qerror_report(QERR_BUS_NOT_FOUND, elem);
336 assert(path[pos] == '/' || !path[pos]);
337 while (path[pos] == '/') {
340 if (path[pos] == '\0') {
345 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
350 dev = qbus_find_dev(bus, elem);
352 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
353 if (!monitor_cur_is_qmp()) {
359 assert(path[pos] == '/' || !path[pos]);
360 while (path[pos] == '/') {
363 if (path[pos] == '\0') {
364 /* last specified element is a device. If it has exactly
365 * one child bus accept it nevertheless */
366 switch (dev->num_child_bus) {
368 qerror_report(QERR_DEVICE_NO_BUS, elem);
371 return QLIST_FIRST(&dev->child_bus);
373 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
374 if (!monitor_cur_is_qmp()) {
382 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
387 bus = qbus_find_bus(dev, elem);
389 qerror_report(QERR_BUS_NOT_FOUND, elem);
390 if (!monitor_cur_is_qmp()) {
398 DeviceState *qdev_device_add(QemuOpts *opts)
402 const char *driver, *path, *id;
406 driver = qemu_opt_get(opts, "driver");
408 qerror_report(QERR_MISSING_PARAMETER, "driver");
413 obj = object_class_by_name(driver);
415 const char *typename = find_typename_by_alias(driver);
419 obj = object_class_by_name(driver);
424 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
428 k = DEVICE_CLASS(obj);
431 path = qemu_opt_get(opts, "bus");
433 bus = qbus_find(path);
437 if (strcmp(object_get_typename(OBJECT(bus)), k->bus_type) != 0) {
438 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
439 driver, object_get_typename(OBJECT(bus)));
443 bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
445 qerror_report(QERR_NO_BUS_FOR_DEVICE,
446 driver, k->bus_type);
450 if (qdev_hotplug && !bus->allow_hotplug) {
451 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
456 bus = sysbus_get_default();
459 /* create device, set properties */
460 qdev = DEVICE(object_new(driver));
461 qdev_set_parent_bus(qdev, bus);
463 id = qemu_opts_id(opts);
467 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
472 object_property_add_child(qdev_get_peripheral(), qdev->id,
475 static int anon_count;
476 gchar *name = g_strdup_printf("device[%d]", anon_count++);
477 object_property_add_child(qdev_get_peripheral_anon(), name,
481 if (qdev_init(qdev) < 0) {
482 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
490 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
491 static void qbus_print(Monitor *mon, BusState *bus, int indent);
493 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
498 for (; props->name; props++) {
501 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
502 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
503 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
505 value = object_property_print(OBJECT(dev), props->name, &err);
513 qdev_printf("%s = %s\n", props->name,
514 value && *value ? value : "<null>");
519 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
521 BusClass *bc = BUS_GET_CLASS(bus);
524 bc->print_dev(mon, dev, indent);
528 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
532 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
533 dev->id ? dev->id : "");
535 if (dev->num_gpio_in) {
536 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
538 if (dev->num_gpio_out) {
539 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
541 class = object_get_class(OBJECT(dev));
543 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
544 class = object_class_get_parent(class);
545 } while (class != object_class_by_name(TYPE_DEVICE));
546 bus_print_dev(dev->parent_bus, mon, dev, indent + 2);
547 QLIST_FOREACH(child, &dev->child_bus, sibling) {
548 qbus_print(mon, child, indent);
552 static void qbus_print(Monitor *mon, BusState *bus, int indent)
556 qdev_printf("bus: %s\n", bus->name);
558 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
559 QTAILQ_FOREACH(kid, &bus->children, sibling) {
560 DeviceState *dev = kid->child;
561 qdev_print(mon, dev, indent);
566 void do_info_qtree(Monitor *mon)
568 if (sysbus_get_default())
569 qbus_print(mon, sysbus_get_default(), 0);
572 void do_info_qdm(Monitor *mon)
574 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
577 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
579 Error *local_err = NULL;
582 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
583 if (error_is_set(&local_err)) {
584 qerror_report_err(local_err);
585 error_free(local_err);
588 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
592 if (!qdev_device_add(opts)) {
599 void qmp_device_del(const char *id, Error **errp)
603 dev = qdev_find_recursive(sysbus_get_default(), id);
605 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
609 qdev_unplug(dev, errp);
612 void qdev_machine_init(void)
614 qdev_get_peripheral_anon();
615 qdev_get_peripheral();