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"
25 * Aliases were a bad idea from the start. Let's keep them
26 * from spreading further.
28 typedef struct QDevAlias
34 static const QDevAlias qdev_alias_table
[] = {
35 { "virtio-blk-pci", "virtio-blk" },
36 { "virtio-net-pci", "virtio-net" },
37 { "virtio-serial-pci", "virtio-serial" },
38 { "virtio-balloon-pci", "virtio-balloon" },
39 { "virtio-blk-s390", "virtio-blk" },
40 { "virtio-net-s390", "virtio-net" },
41 { "virtio-serial-s390", "virtio-serial" },
42 { "lsi53c895a", "lsi" },
43 { "ich9-ahci", "ahci" },
47 static const char *qdev_class_get_alias(DeviceClass
*dc
)
49 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
52 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
53 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
54 return qdev_alias_table
[i
].alias
;
61 static bool qdev_class_has_alias(DeviceClass
*dc
)
63 return (qdev_class_get_alias(dc
) != NULL
);
66 static void qdev_print_devinfo(ObjectClass
*klass
, void *opaque
)
69 bool *show_no_user
= opaque
;
71 dc
= (DeviceClass
*)object_class_dynamic_cast(klass
, TYPE_DEVICE
);
73 if (!dc
|| (show_no_user
&& !*show_no_user
&& dc
->no_user
)) {
77 error_printf("name \"%s\"", object_class_get_name(klass
));
79 error_printf(", bus %s", dc
->bus_info
->name
);
81 if (qdev_class_has_alias(dc
)) {
82 error_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
85 error_printf(", desc \"%s\"", dc
->desc
);
88 error_printf(", no-user");
93 static int set_property(const char *name
, const char *value
, void *opaque
)
95 DeviceState
*dev
= opaque
;
97 if (strcmp(name
, "driver") == 0)
99 if (strcmp(name
, "bus") == 0)
102 if (qdev_prop_parse(dev
, name
, value
) == -1) {
108 static const char *find_typename_by_alias(const char *alias
)
112 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
113 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
114 return qdev_alias_table
[i
].typename
;
121 int qdev_device_help(QemuOpts
*opts
)
128 driver
= qemu_opt_get(opts
, "driver");
129 if (driver
&& !strcmp(driver
, "?")) {
130 bool show_no_user
= false;
131 object_class_foreach(qdev_print_devinfo
, TYPE_DEVICE
, false, &show_no_user
);
135 if (!driver
|| !qemu_opt_get(opts
, "?")) {
139 klass
= object_class_by_name(driver
);
141 const char *typename
= find_typename_by_alias(driver
);
145 klass
= object_class_by_name(driver
);
152 info
= DEVICE_CLASS(klass
);
154 for (prop
= info
->props
; prop
&& prop
->name
; prop
++) {
156 * TODO Properties without a parser are just for dirty hacks.
157 * qdev_prop_ptr is the only such PropertyInfo. It's marked
158 * for removal. This conditional should be removed along with
161 if (!prop
->info
->set
) {
162 continue; /* no way to set it, don't show */
164 error_printf("%s.%s=%s\n", driver
, prop
->name
,
165 prop
->info
->legacy_name
?: prop
->info
->name
);
167 if (info
->bus_info
) {
168 for (prop
= info
->bus_info
->props
; prop
&& prop
->name
; prop
++) {
169 if (!prop
->info
->set
) {
170 continue; /* no way to set it, don't show */
172 error_printf("%s.%s=%s\n", driver
, prop
->name
,
173 prop
->info
->legacy_name
?: prop
->info
->name
);
179 static Object
*qdev_get_peripheral(void)
184 dev
= container_get(qdev_get_machine(), "/peripheral");
190 static Object
*qdev_get_peripheral_anon(void)
195 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
201 static void qbus_list_bus(DeviceState
*dev
)
204 const char *sep
= " ";
206 error_printf("child busses at \"%s\":",
207 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
208 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
209 error_printf("%s\"%s\"", sep
, child
->name
);
215 static void qbus_list_dev(BusState
*bus
)
218 const char *sep
= " ";
220 error_printf("devices at \"%s\":", bus
->name
);
221 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
222 error_printf("%s\"%s\"", sep
, object_get_typename(OBJECT(dev
)));
224 error_printf("/\"%s\"", dev
->id
);
230 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
234 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
235 if (strcmp(child
->name
, elem
) == 0) {
242 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
247 * try to match in order:
248 * (1) instance id, if present
250 * (3) driver alias, if present
252 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
253 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
257 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
258 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
262 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
263 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
265 if (qdev_class_has_alias(dc
) &&
266 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
273 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
277 BusState
*child
, *ret
;
280 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
283 if (info
&& (bus
->info
!= info
)) {
290 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
291 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
292 ret
= qbus_find_recursive(child
, name
, info
);
301 static BusState
*qbus_find(const char *path
)
308 /* find start element */
309 if (path
[0] == '/') {
310 bus
= sysbus_get_default();
313 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
317 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
319 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
326 assert(path
[pos
] == '/' || !path
[pos
]);
327 while (path
[pos
] == '/') {
330 if (path
[pos
] == '\0') {
335 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
340 dev
= qbus_find_dev(bus
, elem
);
342 qerror_report(QERR_DEVICE_NOT_FOUND
, elem
);
343 if (!monitor_cur_is_qmp()) {
349 assert(path
[pos
] == '/' || !path
[pos
]);
350 while (path
[pos
] == '/') {
353 if (path
[pos
] == '\0') {
354 /* last specified element is a device. If it has exactly
355 * one child bus accept it nevertheless */
356 switch (dev
->num_child_bus
) {
358 qerror_report(QERR_DEVICE_NO_BUS
, elem
);
361 return QLIST_FIRST(&dev
->child_bus
);
363 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES
, elem
);
364 if (!monitor_cur_is_qmp()) {
372 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
377 bus
= qbus_find_bus(dev
, elem
);
379 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
380 if (!monitor_cur_is_qmp()) {
388 DeviceState
*qdev_device_add(QemuOpts
*opts
)
392 const char *driver
, *path
, *id
;
396 driver
= qemu_opt_get(opts
, "driver");
398 qerror_report(QERR_MISSING_PARAMETER
, "driver");
403 obj
= object_class_by_name(driver
);
405 const char *typename
= find_typename_by_alias(driver
);
409 obj
= object_class_by_name(driver
);
414 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "driver", "device type");
418 k
= DEVICE_CLASS(obj
);
421 path
= qemu_opt_get(opts
, "bus");
423 bus
= qbus_find(path
);
427 if (bus
->info
!= k
->bus_info
) {
428 qerror_report(QERR_BAD_BUS_FOR_DEVICE
,
429 driver
, bus
->info
->name
);
433 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, k
->bus_info
);
435 qerror_report(QERR_NO_BUS_FOR_DEVICE
,
436 driver
, k
->bus_info
->name
);
440 if (qdev_hotplug
&& !bus
->allow_hotplug
) {
441 qerror_report(QERR_BUS_NO_HOTPLUG
, bus
->name
);
446 bus
= sysbus_get_default();
449 /* create device, set properties */
450 qdev
= DEVICE(object_new(driver
));
451 qdev_set_parent_bus(qdev
, bus
);
452 qdev_prop_set_globals(qdev
);
454 id
= qemu_opts_id(opts
);
458 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
463 object_property_add_child(qdev_get_peripheral(), qdev
->id
,
466 static int anon_count
;
467 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
468 object_property_add_child(qdev_get_peripheral_anon(), name
,
472 if (qdev_init(qdev
) < 0) {
473 qerror_report(QERR_DEVICE_INIT_FAILED
, driver
);
481 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
482 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
484 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
485 const char *prefix
, int indent
)
489 for (; props
->name
; props
++) {
492 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
493 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
494 value
= object_property_get_str(OBJECT(dev
), legacy_name
, &err
);
496 value
= object_property_print(OBJECT(dev
), props
->name
, &err
);
504 qdev_printf("%s-prop: %s = %s\n", prefix
, props
->name
,
505 value
&& *value
? value
: "<null>");
510 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
513 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
514 dev
->id
? dev
->id
: "");
516 if (dev
->num_gpio_in
) {
517 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
519 if (dev
->num_gpio_out
) {
520 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
522 qdev_print_props(mon
, dev
, qdev_get_props(dev
), "dev", indent
);
523 qdev_print_props(mon
, dev
, dev
->parent_bus
->info
->props
, "bus", indent
);
524 if (dev
->parent_bus
->info
->print_dev
)
525 dev
->parent_bus
->info
->print_dev(mon
, dev
, indent
);
526 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
527 qbus_print(mon
, child
, indent
);
531 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
533 struct DeviceState
*dev
;
535 qdev_printf("bus: %s\n", bus
->name
);
537 qdev_printf("type %s\n", bus
->info
->name
);
538 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
539 qdev_print(mon
, dev
, indent
);
544 void do_info_qtree(Monitor
*mon
)
546 if (sysbus_get_default())
547 qbus_print(mon
, sysbus_get_default(), 0);
550 void do_info_qdm(Monitor
*mon
)
552 object_class_foreach(qdev_print_devinfo
, TYPE_DEVICE
, false, NULL
);
555 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
559 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
);
563 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
567 if (!qdev_device_add(opts
)) {
574 void qmp_device_del(const char *id
, Error
**errp
)
578 dev
= qdev_find_recursive(sysbus_get_default(), id
);
580 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
584 qdev_unplug(dev
, errp
);
587 void qdev_machine_init(void)
589 qdev_get_peripheral_anon();
590 qdev_get_peripheral();