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" },
47 { "kvm-pci-assign", "pci-assign" },
51 static const char *qdev_class_get_alias(DeviceClass *dc)
53 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
56 for (i = 0; qdev_alias_table[i].typename; i++) {
57 if (qdev_alias_table[i].arch_mask &&
58 !(qdev_alias_table[i].arch_mask & arch_type)) {
62 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
63 return qdev_alias_table[i].alias;
70 static bool qdev_class_has_alias(DeviceClass *dc)
72 return (qdev_class_get_alias(dc) != NULL);
75 static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
78 bool *show_no_user = opaque;
80 dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
82 if (!dc || (show_no_user && !*show_no_user && dc->no_user)) {
86 error_printf("name \"%s\"", object_class_get_name(klass));
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));
94 error_printf(", desc \"%s\"", dc->desc);
97 error_printf(", no-user");
102 static int set_property(const char *name, const char *value, void *opaque)
104 DeviceState *dev = opaque;
106 if (strcmp(name, "driver") == 0)
108 if (strcmp(name, "bus") == 0)
111 if (qdev_prop_parse(dev, name, value) == -1) {
117 static const char *find_typename_by_alias(const char *alias)
121 for (i = 0; qdev_alias_table[i].alias; i++) {
122 if (qdev_alias_table[i].arch_mask &&
123 !(qdev_alias_table[i].arch_mask & arch_type)) {
127 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
128 return qdev_alias_table[i].typename;
135 int qdev_device_help(QemuOpts *opts)
141 driver = qemu_opt_get(opts, "driver");
142 if (driver && is_help_option(driver)) {
143 bool show_no_user = false;
144 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user);
148 if (!driver || !qemu_opt_has_help_opt(opts)) {
152 klass = object_class_by_name(driver);
154 const char *typename = find_typename_by_alias(driver);
158 klass = object_class_by_name(driver);
166 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
168 * TODO Properties without a parser are just for dirty hacks.
169 * qdev_prop_ptr is the only such PropertyInfo. It's marked
170 * for removal. This conditional should be removed along with
173 if (!prop->info->set) {
174 continue; /* no way to set it, don't show */
176 error_printf("%s.%s=%s\n", driver, prop->name,
177 prop->info->legacy_name ?: prop->info->name);
179 klass = object_class_get_parent(klass);
180 } while (klass != object_class_by_name(TYPE_DEVICE));
184 static Object *qdev_get_peripheral(void)
189 dev = container_get(qdev_get_machine(), "/peripheral");
195 static Object *qdev_get_peripheral_anon(void)
200 dev = container_get(qdev_get_machine(), "/peripheral-anon");
206 static void qbus_list_bus(DeviceState *dev)
209 const char *sep = " ";
211 error_printf("child busses at \"%s\":",
212 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
213 QLIST_FOREACH(child, &dev->child_bus, sibling) {
214 error_printf("%s\"%s\"", sep, child->name);
220 static void qbus_list_dev(BusState *bus)
223 const char *sep = " ";
225 error_printf("devices at \"%s\":", bus->name);
226 QTAILQ_FOREACH(kid, &bus->children, sibling) {
227 DeviceState *dev = kid->child;
228 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
230 error_printf("/\"%s\"", dev->id);
236 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
240 QLIST_FOREACH(child, &dev->child_bus, sibling) {
241 if (strcmp(child->name, elem) == 0) {
248 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
253 * try to match in order:
254 * (1) instance id, if present
256 * (3) driver alias, if present
258 QTAILQ_FOREACH(kid, &bus->children, sibling) {
259 DeviceState *dev = kid->child;
260 if (dev->id && strcmp(dev->id, elem) == 0) {
264 QTAILQ_FOREACH(kid, &bus->children, sibling) {
265 DeviceState *dev = kid->child;
266 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
270 QTAILQ_FOREACH(kid, &bus->children, sibling) {
271 DeviceState *dev = kid->child;
272 DeviceClass *dc = DEVICE_GET_CLASS(dev);
274 if (qdev_class_has_alias(dc) &&
275 strcmp(qdev_class_get_alias(dc), elem) == 0) {
282 static BusState *qbus_find_recursive(BusState *bus, const char *name,
283 const char *bus_typename)
286 BusState *child, *ret;
289 if (name && (strcmp(bus->name, name) != 0)) {
293 (strcmp(object_get_typename(OBJECT(bus)), bus_typename) != 0)) {
300 QTAILQ_FOREACH(kid, &bus->children, sibling) {
301 DeviceState *dev = kid->child;
302 QLIST_FOREACH(child, &dev->child_bus, sibling) {
303 ret = qbus_find_recursive(child, name, bus_typename);
312 static BusState *qbus_find(const char *path)
319 /* find start element */
320 if (path[0] == '/') {
321 bus = sysbus_get_default();
324 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
328 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
330 qerror_report(QERR_BUS_NOT_FOUND, elem);
337 assert(path[pos] == '/' || !path[pos]);
338 while (path[pos] == '/') {
341 if (path[pos] == '\0') {
346 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
351 dev = qbus_find_dev(bus, elem);
353 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
354 if (!monitor_cur_is_qmp()) {
360 assert(path[pos] == '/' || !path[pos]);
361 while (path[pos] == '/') {
364 if (path[pos] == '\0') {
365 /* last specified element is a device. If it has exactly
366 * one child bus accept it nevertheless */
367 switch (dev->num_child_bus) {
369 qerror_report(QERR_DEVICE_NO_BUS, elem);
372 return QLIST_FIRST(&dev->child_bus);
374 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
375 if (!monitor_cur_is_qmp()) {
383 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
388 bus = qbus_find_bus(dev, elem);
390 qerror_report(QERR_BUS_NOT_FOUND, elem);
391 if (!monitor_cur_is_qmp()) {
399 DeviceState *qdev_device_add(QemuOpts *opts)
403 const char *driver, *path, *id;
407 driver = qemu_opt_get(opts, "driver");
409 qerror_report(QERR_MISSING_PARAMETER, "driver");
414 obj = object_class_by_name(driver);
416 const char *typename = find_typename_by_alias(driver);
420 obj = object_class_by_name(driver);
425 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
429 k = DEVICE_CLASS(obj);
432 path = qemu_opt_get(opts, "bus");
434 bus = qbus_find(path);
438 if (strcmp(object_get_typename(OBJECT(bus)), k->bus_type) != 0) {
439 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
440 driver, object_get_typename(OBJECT(bus)));
444 bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
446 qerror_report(QERR_NO_BUS_FOR_DEVICE,
447 k->bus_type, driver);
451 if (qdev_hotplug && !bus->allow_hotplug) {
452 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
457 bus = sysbus_get_default();
460 /* create device, set properties */
461 qdev = DEVICE(object_new(driver));
462 qdev_set_parent_bus(qdev, bus);
464 id = qemu_opts_id(opts);
468 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
473 object_property_add_child(qdev_get_peripheral(), qdev->id,
476 static int anon_count;
477 gchar *name = g_strdup_printf("device[%d]", anon_count++);
478 object_property_add_child(qdev_get_peripheral_anon(), name,
482 if (qdev_init(qdev) < 0) {
483 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
491 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
492 static void qbus_print(Monitor *mon, BusState *bus, int indent);
494 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
499 for (; props->name; props++) {
502 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
503 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
504 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
506 value = object_property_print(OBJECT(dev), props->name, &err);
514 qdev_printf("%s = %s\n", props->name,
515 value && *value ? value : "<null>");
520 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
522 BusClass *bc = BUS_GET_CLASS(bus);
525 bc->print_dev(mon, dev, indent);
529 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
533 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
534 dev->id ? dev->id : "");
536 if (dev->num_gpio_in) {
537 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
539 if (dev->num_gpio_out) {
540 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
542 class = object_get_class(OBJECT(dev));
544 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
545 class = object_class_get_parent(class);
546 } while (class != object_class_by_name(TYPE_DEVICE));
547 bus_print_dev(dev->parent_bus, mon, dev, indent);
548 QLIST_FOREACH(child, &dev->child_bus, sibling) {
549 qbus_print(mon, child, indent);
553 static void qbus_print(Monitor *mon, BusState *bus, int indent)
557 qdev_printf("bus: %s\n", bus->name);
559 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
560 QTAILQ_FOREACH(kid, &bus->children, sibling) {
561 DeviceState *dev = kid->child;
562 qdev_print(mon, dev, indent);
567 void do_info_qtree(Monitor *mon)
569 if (sysbus_get_default())
570 qbus_print(mon, sysbus_get_default(), 0);
573 void do_info_qdm(Monitor *mon)
575 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
578 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
580 Error *local_err = NULL;
583 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
584 if (error_is_set(&local_err)) {
585 qerror_report_err(local_err);
586 error_free(local_err);
589 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
593 if (!qdev_device_add(opts)) {
600 void qmp_device_del(const char *id, Error **errp)
604 dev = qdev_find_recursive(sysbus_get_default(), id);
606 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
610 qdev_unplug(dev, errp);
613 void qdev_machine_init(void)
615 qdev_get_peripheral_anon();
616 qdev_get_peripheral();