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/>.
20 /* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
33 static int qdev_hotplug
= 0;
34 static bool qdev_hot_added
= false;
35 static bool qdev_hot_removed
= false;
37 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
38 static BusState
*main_system_bus
;
40 DeviceInfo
*device_info_list
;
42 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
44 static BusState
*qbus_find(const char *path
);
46 /* Register a new device type. */
47 void qdev_register(DeviceInfo
*info
)
49 assert(info
->size
>= sizeof(DeviceState
));
52 info
->next
= device_info_list
;
53 device_info_list
= info
;
56 static DeviceInfo
*qdev_find_info(BusInfo
*bus_info
, const char *name
)
60 /* first check device names */
61 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
62 if (bus_info
&& info
->bus_info
!= bus_info
)
64 if (strcmp(info
->name
, name
) != 0)
69 /* failing that check the aliases */
70 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
71 if (bus_info
&& info
->bus_info
!= bus_info
)
75 if (strcmp(info
->alias
, name
) != 0)
82 static DeviceState
*qdev_create_from_info(BusState
*bus
, DeviceInfo
*info
)
86 assert(bus
->info
== info
->bus_info
);
87 dev
= qemu_mallocz(info
->size
);
89 dev
->parent_bus
= bus
;
90 qdev_prop_set_defaults(dev
, dev
->info
->props
);
91 qdev_prop_set_defaults(dev
, dev
->parent_bus
->info
->props
);
92 qdev_prop_set_globals(dev
);
93 QLIST_INSERT_HEAD(&bus
->children
, dev
, sibling
);
95 assert(bus
->allow_hotplug
);
97 qdev_hot_added
= true;
99 dev
->instance_id_alias
= -1;
100 dev
->state
= DEV_STATE_CREATED
;
104 /* Create a new device. This only initializes the device state structure
105 and allows properties to be set. qdev_init should be called to
106 initialize the actual device emulation. */
107 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
112 bus
= sysbus_get_default();
115 info
= qdev_find_info(bus
->info
, name
);
117 hw_error("Unknown device '%s' for bus '%s'\n", name
, bus
->info
->name
);
120 return qdev_create_from_info(bus
, info
);
123 static void qdev_print_devinfo(DeviceInfo
*info
)
125 error_printf("name \"%s\", bus %s",
126 info
->name
, info
->bus_info
->name
);
128 error_printf(", alias \"%s\"", info
->alias
);
131 error_printf(", desc \"%s\"", info
->desc
);
134 error_printf(", no-user");
139 static int set_property(const char *name
, const char *value
, void *opaque
)
141 DeviceState
*dev
= opaque
;
143 if (strcmp(name
, "driver") == 0)
145 if (strcmp(name
, "bus") == 0)
148 if (qdev_prop_parse(dev
, name
, value
) == -1) {
154 int qdev_device_help(QemuOpts
*opts
)
160 driver
= qemu_opt_get(opts
, "driver");
161 if (driver
&& !strcmp(driver
, "?")) {
162 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
164 continue; /* not available, don't show */
166 qdev_print_devinfo(info
);
171 if (!qemu_opt_get(opts
, "?")) {
175 info
= qdev_find_info(NULL
, driver
);
180 for (prop
= info
->props
; prop
&& prop
->name
; prop
++) {
182 * TODO Properties without a parser are just for dirty hacks.
183 * qdev_prop_ptr is the only such PropertyInfo. It's marked
184 * for removal. This conditional should be removed along with
187 if (!prop
->info
->parse
) {
188 continue; /* no way to set it, don't show */
190 error_printf("%s.%s=%s\n", info
->name
, prop
->name
, prop
->info
->name
);
195 DeviceState
*qdev_device_add(QemuOpts
*opts
)
197 const char *driver
, *path
, *id
;
202 driver
= qemu_opt_get(opts
, "driver");
204 qerror_report(QERR_MISSING_PARAMETER
, "driver");
209 info
= qdev_find_info(NULL
, driver
);
210 if (!info
|| info
->no_user
) {
211 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "driver", "a driver name");
212 error_printf_unless_qmp("Try with argument '?' for a list.\n");
217 path
= qemu_opt_get(opts
, "bus");
219 bus
= qbus_find(path
);
223 if (bus
->info
!= info
->bus_info
) {
224 qerror_report(QERR_BAD_BUS_FOR_DEVICE
,
225 driver
, bus
->info
->name
);
229 bus
= qbus_find_recursive(main_system_bus
, NULL
, info
->bus_info
);
231 qerror_report(QERR_NO_BUS_FOR_DEVICE
,
232 info
->name
, info
->bus_info
->name
);
236 if (qdev_hotplug
&& !bus
->allow_hotplug
) {
237 qerror_report(QERR_BUS_NO_HOTPLUG
, bus
->name
);
241 /* create device, set properties */
242 qdev
= qdev_create_from_info(bus
, info
);
243 id
= qemu_opts_id(opts
);
247 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
251 if (qdev_init(qdev
) < 0) {
252 qerror_report(QERR_DEVICE_INIT_FAILED
, driver
);
259 /* Initialize a device. Device properties should be set before calling
260 this function. IRQs and MMIO regions should be connected/mapped after
261 calling this function.
262 On failure, destroy the device and return negative value.
263 Return 0 on success. */
264 int qdev_init(DeviceState
*dev
)
268 assert(dev
->state
== DEV_STATE_CREATED
);
269 rc
= dev
->info
->init(dev
, dev
->info
);
274 if (dev
->info
->vmsd
) {
275 vmstate_register_with_alias_id(dev
, -1, dev
->info
->vmsd
, dev
,
276 dev
->instance_id_alias
,
277 dev
->alias_required_for_version
);
279 dev
->state
= DEV_STATE_INITIALIZED
;
283 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
284 int required_for_version
)
286 assert(dev
->state
== DEV_STATE_CREATED
);
287 dev
->instance_id_alias
= alias_id
;
288 dev
->alias_required_for_version
= required_for_version
;
291 int qdev_unplug(DeviceState
*dev
)
293 if (!dev
->parent_bus
->allow_hotplug
) {
294 qerror_report(QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
297 assert(dev
->info
->unplug
!= NULL
);
299 qdev_hot_removed
= true;
301 return dev
->info
->unplug(dev
);
304 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
306 if (dev
->info
->reset
) {
307 dev
->info
->reset(dev
);
313 BusState
*sysbus_get_default(void)
315 if (!main_system_bus
) {
316 main_system_bus
= qbus_create(&system_bus_info
, NULL
,
319 return main_system_bus
;
322 static int qbus_reset_one(BusState
*bus
, void *opaque
)
324 if (bus
->info
->reset
) {
325 return bus
->info
->reset(bus
);
330 void qdev_reset_all(DeviceState
*dev
)
332 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
335 void qbus_reset_all_fn(void *opaque
)
337 BusState
*bus
= opaque
;
338 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
341 /* can be used as ->unplug() callback for the simple cases */
342 int qdev_simple_unplug_cb(DeviceState
*dev
)
349 /* Like qdev_init(), but terminate program via hw_error() instead of
350 returning an error value. This is okay during machine creation.
351 Don't use for hotplug, because there callers need to recover from
352 failure. Exception: if you know the device's init() callback can't
353 fail, then qdev_init_nofail() can't fail either, and is therefore
354 usable even then. But relying on the device implementation that
355 way is somewhat unclean, and best avoided. */
356 void qdev_init_nofail(DeviceState
*dev
)
358 DeviceInfo
*info
= dev
->info
;
360 if (qdev_init(dev
) < 0) {
361 error_report("Initialization of device %s failed\n", info
->name
);
366 /* Unlink device from bus and free the structure. */
367 void qdev_free(DeviceState
*dev
)
372 if (dev
->state
== DEV_STATE_INITIALIZED
) {
373 while (dev
->num_child_bus
) {
374 bus
= QLIST_FIRST(&dev
->child_bus
);
378 vmstate_unregister(dev
, dev
->info
->vmsd
, dev
);
380 dev
->info
->exit(dev
);
382 qemu_opts_del(dev
->opts
);
384 QLIST_REMOVE(dev
, sibling
);
385 for (prop
= dev
->info
->props
; prop
&& prop
->name
; prop
++) {
386 if (prop
->info
->free
) {
387 prop
->info
->free(dev
, prop
);
393 void qdev_machine_creation_done(void)
396 * ok, initial machine setup is done, starting from now we can
397 * only create hotpluggable devices
402 bool qdev_machine_modified(void)
404 return qdev_hot_added
|| qdev_hot_removed
;
407 /* Get a character (serial) device interface. */
408 CharDriverState
*qdev_init_chardev(DeviceState
*dev
)
410 static int next_serial
;
412 /* FIXME: This function needs to go away: use chardev properties! */
413 return serial_hds
[next_serial
++];
416 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
418 return dev
->parent_bus
;
421 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
423 assert(dev
->num_gpio_in
== 0);
424 dev
->num_gpio_in
= n
;
425 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
428 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
430 assert(dev
->num_gpio_out
== 0);
431 dev
->num_gpio_out
= n
;
432 dev
->gpio_out
= pins
;
435 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
437 assert(n
>= 0 && n
< dev
->num_gpio_in
);
438 return dev
->gpio_in
[n
];
441 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
443 assert(n
>= 0 && n
< dev
->num_gpio_out
);
444 dev
->gpio_out
[n
] = pin
;
447 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
449 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
);
451 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
453 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
454 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
455 qdev_prop_exists(dev
, "vectors")) {
456 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
460 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
464 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
465 if (strcmp(name
, bus
->name
) == 0) {
472 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
473 qbus_walkerfn
*busfn
, void *opaque
)
479 err
= busfn(bus
, opaque
);
485 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
486 err
= qdev_walk_children(dev
, devfn
, busfn
, opaque
);
495 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
496 qbus_walkerfn
*busfn
, void *opaque
)
502 err
= devfn(dev
, opaque
);
508 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
509 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
518 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
522 BusState
*child
, *ret
;
525 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
528 if (info
&& (bus
->info
!= info
)) {
535 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
536 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
537 ret
= qbus_find_recursive(child
, name
, info
);
546 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
548 DeviceState
*dev
, *ret
;
551 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
552 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
554 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
555 ret
= qdev_find_recursive(child
, id
);
564 static void qbus_list_bus(DeviceState
*dev
)
567 const char *sep
= " ";
569 error_printf("child busses at \"%s\":",
570 dev
->id
? dev
->id
: dev
->info
->name
);
571 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
572 error_printf("%s\"%s\"", sep
, child
->name
);
578 static void qbus_list_dev(BusState
*bus
)
581 const char *sep
= " ";
583 error_printf("devices at \"%s\":", bus
->name
);
584 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
585 error_printf("%s\"%s\"", sep
, dev
->info
->name
);
587 error_printf("/\"%s\"", dev
->id
);
593 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
597 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
598 if (strcmp(child
->name
, elem
) == 0) {
605 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
610 * try to match in order:
611 * (1) instance id, if present
613 * (3) driver alias, if present
615 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
616 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
620 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
621 if (strcmp(dev
->info
->name
, elem
) == 0) {
625 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
626 if (dev
->info
->alias
&& strcmp(dev
->info
->alias
, elem
) == 0) {
633 static BusState
*qbus_find(const char *path
)
640 /* find start element */
641 if (path
[0] == '/') {
642 bus
= main_system_bus
;
645 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
649 bus
= qbus_find_recursive(main_system_bus
, elem
, NULL
);
651 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
658 assert(path
[pos
] == '/' || !path
[pos
]);
659 while (path
[pos
] == '/') {
662 if (path
[pos
] == '\0') {
667 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
672 dev
= qbus_find_dev(bus
, elem
);
674 qerror_report(QERR_DEVICE_NOT_FOUND
, elem
);
675 if (!monitor_cur_is_qmp()) {
681 assert(path
[pos
] == '/' || !path
[pos
]);
682 while (path
[pos
] == '/') {
685 if (path
[pos
] == '\0') {
686 /* last specified element is a device. If it has exactly
687 * one child bus accept it nevertheless */
688 switch (dev
->num_child_bus
) {
690 qerror_report(QERR_DEVICE_NO_BUS
, elem
);
693 return QLIST_FIRST(&dev
->child_bus
);
695 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES
, elem
);
696 if (!monitor_cur_is_qmp()) {
704 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
709 bus
= qbus_find_bus(dev
, elem
);
711 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
712 if (!monitor_cur_is_qmp()) {
720 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
721 DeviceState
*parent
, const char *name
)
727 bus
->parent
= parent
;
730 /* use supplied name */
731 bus
->name
= qemu_strdup(name
);
732 } else if (parent
&& parent
->id
) {
733 /* parent device has id -> use it for bus name */
734 len
= strlen(parent
->id
) + 16;
735 buf
= qemu_malloc(len
);
736 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
739 /* no id -> use lowercase bus type for bus name */
740 len
= strlen(info
->name
) + 16;
741 buf
= qemu_malloc(len
);
742 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
743 parent
? parent
->num_child_bus
: 0);
744 for (i
= 0; i
< len
; i
++)
745 buf
[i
] = qemu_tolower(buf
[i
]);
749 QLIST_INIT(&bus
->children
);
751 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
752 parent
->num_child_bus
++;
753 } else if (bus
!= main_system_bus
) {
754 /* TODO: once all bus devices are qdevified,
755 only reset handler for main_system_bus should be registered here. */
756 qemu_register_reset(qbus_reset_all_fn
, bus
);
760 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
764 bus
= qemu_mallocz(info
->size
);
765 bus
->qdev_allocated
= 1;
766 qbus_create_inplace(bus
, info
, parent
, name
);
770 void qbus_free(BusState
*bus
)
774 while ((dev
= QLIST_FIRST(&bus
->children
)) != NULL
) {
778 QLIST_REMOVE(bus
, sibling
);
779 bus
->parent
->num_child_bus
--;
781 assert(bus
!= main_system_bus
); /* main_system_bus is never freed */
782 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
784 qemu_free((void*)bus
->name
);
785 if (bus
->qdev_allocated
) {
790 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
791 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
793 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
794 const char *prefix
, int indent
)
800 while (props
->name
) {
802 * TODO Properties without a print method are just for dirty
803 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
804 * marked for removal. The test props->info->print should be
805 * removed along with it.
807 if (props
->info
->print
) {
808 props
->info
->print(dev
, props
, buf
, sizeof(buf
));
809 qdev_printf("%s-prop: %s = %s\n", prefix
, props
->name
, buf
);
815 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
818 qdev_printf("dev: %s, id \"%s\"\n", dev
->info
->name
,
819 dev
->id
? dev
->id
: "");
821 if (dev
->num_gpio_in
) {
822 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
824 if (dev
->num_gpio_out
) {
825 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
827 qdev_print_props(mon
, dev
, dev
->info
->props
, "dev", indent
);
828 qdev_print_props(mon
, dev
, dev
->parent_bus
->info
->props
, "bus", indent
);
829 if (dev
->parent_bus
->info
->print_dev
)
830 dev
->parent_bus
->info
->print_dev(mon
, dev
, indent
);
831 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
832 qbus_print(mon
, child
, indent
);
836 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
838 struct DeviceState
*dev
;
840 qdev_printf("bus: %s\n", bus
->name
);
842 qdev_printf("type %s\n", bus
->info
->name
);
843 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
844 qdev_print(mon
, dev
, indent
);
849 void do_info_qtree(Monitor
*mon
)
852 qbus_print(mon
, main_system_bus
, 0);
855 void do_info_qdm(Monitor
*mon
)
859 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
860 qdev_print_devinfo(info
);
864 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
868 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
);
872 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
876 if (!qdev_device_add(opts
)) {
883 int do_device_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
885 const char *id
= qdict_get_str(qdict
, "id");
888 dev
= qdev_find_recursive(main_system_bus
, id
);
890 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
893 return qdev_unplug(dev
);
896 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
900 if (dev
&& dev
->parent_bus
) {
902 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
903 if (dev
->parent_bus
->info
->get_fw_dev_path
) {
904 d
= dev
->parent_bus
->info
->get_fw_dev_path(dev
);
905 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
908 l
+= snprintf(p
+ l
, size
- l
, "%s", dev
->info
->name
);
911 l
+= snprintf(p
+ l
, size
- l
, "/");
916 char* qdev_get_fw_dev_path(DeviceState
*dev
)
921 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);