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/>.
24 * Aliases were a bad idea from the start. Let's keep them
25 * from spreading further.
27 typedef struct QDevAlias
33 static const QDevAlias qdev_alias_table
[] = {
34 { "virtio-blk-pci", "virtio-blk" },
35 { "virtio-net-pci", "virtio-net" },
36 { "virtio-serial-pci", "virtio-serial" },
37 { "virtio-balloon-pci", "virtio-balloon" },
38 { "virtio-blk-s390", "virtio-blk" },
39 { "virtio-net-s390", "virtio-net" },
40 { "virtio-serial-s390", "virtio-serial" },
41 { "lsi53c895a", "lsi" },
42 { "ich9-ahci", "ahci" },
46 static const char *qdev_class_get_alias(DeviceClass
*dc
)
48 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
51 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
52 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
53 return qdev_alias_table
[i
].alias
;
60 static bool qdev_class_has_alias(DeviceClass
*dc
)
62 return (qdev_class_get_alias(dc
) != NULL
);
65 static void qdev_print_devinfo(ObjectClass
*klass
, void *opaque
)
68 bool *show_no_user
= opaque
;
70 dc
= (DeviceClass
*)object_class_dynamic_cast(klass
, TYPE_DEVICE
);
72 if (!dc
|| (show_no_user
&& !*show_no_user
&& dc
->no_user
)) {
76 error_printf("name \"%s\"", object_class_get_name(klass
));
78 error_printf(", bus %s", dc
->bus_info
->name
);
80 if (qdev_class_has_alias(dc
)) {
81 error_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
84 error_printf(", desc \"%s\"", dc
->desc
);
87 error_printf(", no-user");
92 static int set_property(const char *name
, const char *value
, void *opaque
)
94 DeviceState
*dev
= opaque
;
96 if (strcmp(name
, "driver") == 0)
98 if (strcmp(name
, "bus") == 0)
101 if (qdev_prop_parse(dev
, name
, value
) == -1) {
107 static const char *find_typename_by_alias(const char *alias
)
111 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
112 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
113 return qdev_alias_table
[i
].typename
;
120 int qdev_device_help(QemuOpts
*opts
)
127 driver
= qemu_opt_get(opts
, "driver");
128 if (driver
&& !strcmp(driver
, "?")) {
129 bool show_no_user
= false;
130 object_class_foreach(qdev_print_devinfo
, TYPE_DEVICE
, false, &show_no_user
);
134 if (!driver
|| !qemu_opt_get(opts
, "?")) {
138 klass
= object_class_by_name(driver
);
140 const char *typename
= find_typename_by_alias(driver
);
144 klass
= object_class_by_name(driver
);
151 info
= DEVICE_CLASS(klass
);
153 for (prop
= info
->props
; prop
&& prop
->name
; prop
++) {
155 * TODO Properties without a parser are just for dirty hacks.
156 * qdev_prop_ptr is the only such PropertyInfo. It's marked
157 * for removal. This conditional should be removed along with
160 if (!prop
->info
->parse
) {
161 continue; /* no way to set it, don't show */
163 error_printf("%s.%s=%s\n", driver
, prop
->name
,
164 prop
->info
->legacy_name
?: prop
->info
->name
);
166 if (info
->bus_info
) {
167 for (prop
= info
->bus_info
->props
; prop
&& prop
->name
; prop
++) {
168 if (!prop
->info
->parse
) {
169 continue; /* no way to set it, don't show */
171 error_printf("%s.%s=%s\n", driver
, prop
->name
,
172 prop
->info
->legacy_name
?: prop
->info
->name
);
178 static Object
*qdev_get_peripheral(void)
180 static DeviceState
*dev
;
183 dev
= qdev_create(NULL
, "container");
184 object_property_add_child(object_get_root(), "peripheral",
186 qdev_init_nofail(dev
);
192 static Object
*qdev_get_peripheral_anon(void)
194 static DeviceState
*dev
;
197 dev
= qdev_create(NULL
, "container");
198 object_property_add_child(object_get_root(), "peripheral-anon",
200 qdev_init_nofail(dev
);
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(dev
, &bus
->children
, sibling
) {
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(dev
, &bus
->children
, sibling
) {
258 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
262 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
263 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
267 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
268 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
270 if (qdev_class_has_alias(dc
) &&
271 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
278 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
282 BusState
*child
, *ret
;
285 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
288 if (info
&& (bus
->info
!= info
)) {
295 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
296 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
297 ret
= qbus_find_recursive(child
, name
, info
);
306 static BusState
*qbus_find(const char *path
)
313 /* find start element */
314 if (path
[0] == '/') {
315 bus
= sysbus_get_default();
318 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
322 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
324 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
331 assert(path
[pos
] == '/' || !path
[pos
]);
332 while (path
[pos
] == '/') {
335 if (path
[pos
] == '\0') {
340 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
345 dev
= qbus_find_dev(bus
, elem
);
347 qerror_report(QERR_DEVICE_NOT_FOUND
, elem
);
348 if (!monitor_cur_is_qmp()) {
354 assert(path
[pos
] == '/' || !path
[pos
]);
355 while (path
[pos
] == '/') {
358 if (path
[pos
] == '\0') {
359 /* last specified element is a device. If it has exactly
360 * one child bus accept it nevertheless */
361 switch (dev
->num_child_bus
) {
363 qerror_report(QERR_DEVICE_NO_BUS
, elem
);
366 return QLIST_FIRST(&dev
->child_bus
);
368 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES
, elem
);
369 if (!monitor_cur_is_qmp()) {
377 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
382 bus
= qbus_find_bus(dev
, elem
);
384 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
385 if (!monitor_cur_is_qmp()) {
393 DeviceState
*qdev_device_add(QemuOpts
*opts
)
397 const char *driver
, *path
, *id
;
401 driver
= qemu_opt_get(opts
, "driver");
403 qerror_report(QERR_MISSING_PARAMETER
, "driver");
408 obj
= object_class_by_name(driver
);
410 const char *typename
= find_typename_by_alias(driver
);
414 obj
= object_class_by_name(driver
);
419 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "driver", "device type");
423 k
= DEVICE_CLASS(obj
);
426 path
= qemu_opt_get(opts
, "bus");
428 bus
= qbus_find(path
);
432 if (bus
->info
!= k
->bus_info
) {
433 qerror_report(QERR_BAD_BUS_FOR_DEVICE
,
434 driver
, bus
->info
->name
);
438 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, k
->bus_info
);
440 qerror_report(QERR_NO_BUS_FOR_DEVICE
,
441 driver
, k
->bus_info
->name
);
445 if (qdev_hotplug
&& !bus
->allow_hotplug
) {
446 qerror_report(QERR_BUS_NO_HOTPLUG
, bus
->name
);
451 bus
= sysbus_get_default();
454 /* create device, set properties */
455 qdev
= DEVICE(object_new(driver
));
456 qdev_set_parent_bus(qdev
, bus
);
457 qdev_prop_set_globals(qdev
);
459 id
= qemu_opts_id(opts
);
462 object_property_add_child(qdev_get_peripheral(), qdev
->id
,
465 static int anon_count
;
466 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
467 object_property_add_child(qdev_get_peripheral_anon(), name
,
471 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
475 if (qdev_init(qdev
) < 0) {
476 qerror_report(QERR_DEVICE_INIT_FAILED
, driver
);
484 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
485 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
487 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
488 const char *prefix
, int indent
)
494 while (props
->name
) {
496 * TODO Properties without a print method are just for dirty
497 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
498 * marked for removal. The test props->info->print should be
499 * removed along with it.
501 if (props
->info
->print
) {
502 props
->info
->print(dev
, props
, buf
, sizeof(buf
));
503 qdev_printf("%s-prop: %s = %s\n", prefix
, props
->name
, buf
);
509 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
512 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
513 dev
->id
? dev
->id
: "");
515 if (dev
->num_gpio_in
) {
516 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
518 if (dev
->num_gpio_out
) {
519 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
521 qdev_print_props(mon
, dev
, qdev_get_props(dev
), "dev", indent
);
522 qdev_print_props(mon
, dev
, dev
->parent_bus
->info
->props
, "bus", indent
);
523 if (dev
->parent_bus
->info
->print_dev
)
524 dev
->parent_bus
->info
->print_dev(mon
, dev
, indent
);
525 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
526 qbus_print(mon
, child
, indent
);
530 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
532 struct DeviceState
*dev
;
534 qdev_printf("bus: %s\n", bus
->name
);
536 qdev_printf("type %s\n", bus
->info
->name
);
537 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
538 qdev_print(mon
, dev
, indent
);
543 void do_info_qtree(Monitor
*mon
)
545 if (sysbus_get_default())
546 qbus_print(mon
, sysbus_get_default(), 0);
549 void do_info_qdm(Monitor
*mon
)
551 object_class_foreach(qdev_print_devinfo
, TYPE_DEVICE
, false, NULL
);
554 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
558 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
);
562 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
566 if (!qdev_device_add(opts
)) {
573 int do_device_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
575 const char *id
= qdict_get_str(qdict
, "id");
578 dev
= qdev_find_recursive(sysbus_get_default(), id
);
580 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
583 return qdev_unplug(dev
);
586 void qdev_machine_init(void)
588 qdev_get_peripheral_anon();
589 qdev_get_peripheral();