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
34 static int qdev_hotplug
= 0;
36 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
37 static BusState
*main_system_bus
;
39 DeviceInfo
*device_info_list
;
41 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
43 static BusState
*qbus_find(const char *path
);
45 /* Register a new device type. */
46 void qdev_register(DeviceInfo
*info
)
48 assert(info
->size
>= sizeof(DeviceState
));
51 info
->next
= device_info_list
;
52 device_info_list
= info
;
55 static DeviceInfo
*qdev_find_info(BusInfo
*bus_info
, const char *name
)
59 /* first check device names */
60 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
61 if (bus_info
&& info
->bus_info
!= bus_info
)
63 if (strcmp(info
->name
, name
) != 0)
68 /* failing that check the aliases */
69 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
70 if (bus_info
&& info
->bus_info
!= bus_info
)
74 if (strcmp(info
->alias
, name
) != 0)
81 /* Create a new device. This only initializes the device state structure
82 and allows properties to be set. qdev_init should be called to
83 initialize the actual device emulation. */
84 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
90 if (!main_system_bus
) {
91 main_system_bus
= qbus_create(&system_bus_info
, NULL
, "main-system-bus");
93 bus
= main_system_bus
;
96 info
= qdev_find_info(bus
->info
, name
);
98 hw_error("Unknown device '%s' for bus '%s'\n", name
, bus
->info
->name
);
101 dev
= qemu_mallocz(info
->size
);
103 dev
->parent_bus
= bus
;
104 qdev_prop_set_defaults(dev
, dev
->info
->props
);
105 qdev_prop_set_defaults(dev
, dev
->parent_bus
->info
->props
);
106 qdev_prop_set_globals(dev
);
107 QLIST_INSERT_HEAD(&bus
->children
, dev
, sibling
);
109 assert(bus
->allow_hotplug
);
112 dev
->state
= DEV_STATE_CREATED
;
116 static void qdev_print_devinfo(DeviceInfo
*info
)
118 error_printf("name \"%s\", bus %s",
119 info
->name
, info
->bus_info
->name
);
121 error_printf(", alias \"%s\"", info
->alias
);
124 error_printf(", desc \"%s\"", info
->desc
);
127 error_printf(", no-user");
132 static int set_property(const char *name
, const char *value
, void *opaque
)
134 DeviceState
*dev
= opaque
;
136 if (strcmp(name
, "driver") == 0)
138 if (strcmp(name
, "bus") == 0)
141 if (qdev_prop_parse(dev
, name
, value
) == -1) {
142 qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"\n",
143 name
, value
, dev
->info
->name
);
149 int qdev_device_help(QemuOpts
*opts
)
155 driver
= qemu_opt_get(opts
, "driver");
156 if (driver
&& !strcmp(driver
, "?")) {
157 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
158 qdev_print_devinfo(info
);
163 if (!qemu_opt_get(opts
, "?")) {
167 info
= qdev_find_info(NULL
, driver
);
172 for (prop
= info
->props
; prop
&& prop
->name
; prop
++) {
173 error_printf("%s.%s=%s\n", info
->name
, prop
->name
, prop
->info
->name
);
178 DeviceState
*qdev_device_add(QemuOpts
*opts
)
180 const char *driver
, *path
, *id
;
185 driver
= qemu_opt_get(opts
, "driver");
187 qemu_error("-device: no driver specified\n");
192 info
= qdev_find_info(NULL
, driver
);
194 qemu_error_new(QERR_DEVICE_NOT_FOUND
, driver
);
198 qemu_error("device \"%s\" can't be added via command line\n",
204 path
= qemu_opt_get(opts
, "bus");
206 bus
= qbus_find(path
);
208 bus
= qbus_find_recursive(main_system_bus
, NULL
, info
->bus_info
);
211 qemu_error("Did not find %s bus for %s\n",
212 path
? path
: info
->bus_info
->name
, info
->name
);
215 if (qdev_hotplug
&& !bus
->allow_hotplug
) {
216 qemu_error("Bus %s does not support hotplugging\n",
221 /* create device, set properties */
222 qdev
= qdev_create(bus
, driver
);
223 id
= qemu_opts_id(opts
);
227 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
231 if (qdev_init(qdev
) < 0) {
232 qemu_error("Error initializing device %s\n", driver
);
239 static void qdev_reset(void *opaque
)
241 DeviceState
*dev
= opaque
;
242 if (dev
->info
->reset
)
243 dev
->info
->reset(dev
);
246 /* Initialize a device. Device properties should be set before calling
247 this function. IRQs and MMIO regions should be connected/mapped after
248 calling this function.
249 On failure, destroy the device and return negative value.
250 Return 0 on success. */
251 int qdev_init(DeviceState
*dev
)
255 assert(dev
->state
== DEV_STATE_CREATED
);
256 rc
= dev
->info
->init(dev
, dev
->info
);
261 qemu_register_reset(qdev_reset
, dev
);
263 vmstate_register(-1, dev
->info
->vmsd
, dev
);
264 dev
->state
= DEV_STATE_INITIALIZED
;
268 int qdev_unplug(DeviceState
*dev
)
270 if (!dev
->parent_bus
->allow_hotplug
) {
271 qemu_error("Bus %s does not support hotplugging\n",
272 dev
->parent_bus
->name
);
275 assert(dev
->info
->unplug
!= NULL
);
277 return dev
->info
->unplug(dev
);
280 /* can be used as ->unplug() callback for the simple cases */
281 int qdev_simple_unplug_cb(DeviceState
*dev
)
288 /* Like qdev_init(), but terminate program via hw_error() instead of
289 returning an error value. This is okay during machine creation.
290 Don't use for hotplug, because there callers need to recover from
291 failure. Exception: if you know the device's init() callback can't
292 fail, then qdev_init_nofail() can't fail either, and is therefore
293 usable even then. But relying on the device implementation that
294 way is somewhat unclean, and best avoided. */
295 void qdev_init_nofail(DeviceState
*dev
)
297 DeviceInfo
*info
= dev
->info
;
299 if (qdev_init(dev
) < 0)
300 hw_error("Initialization of device %s failed\n", info
->name
);
303 /* Unlink device from bus and free the structure. */
304 void qdev_free(DeviceState
*dev
)
308 if (dev
->state
== DEV_STATE_INITIALIZED
) {
309 while (dev
->num_child_bus
) {
310 bus
= QLIST_FIRST(&dev
->child_bus
);
314 vmstate_unregister(dev
->info
->vmsd
, dev
);
316 dev
->info
->exit(dev
);
318 qemu_opts_del(dev
->opts
);
320 qemu_unregister_reset(qdev_reset
, dev
);
321 QLIST_REMOVE(dev
, sibling
);
325 void qdev_machine_creation_done(void)
328 * ok, initial machine setup is done, starting from now we can
329 * only create hotpluggable devices
334 /* Get a character (serial) device interface. */
335 CharDriverState
*qdev_init_chardev(DeviceState
*dev
)
337 static int next_serial
;
339 /* FIXME: This function needs to go away: use chardev properties! */
340 return serial_hds
[next_serial
++];
343 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
345 return dev
->parent_bus
;
348 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
350 assert(dev
->num_gpio_in
== 0);
351 dev
->num_gpio_in
= n
;
352 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
355 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
357 assert(dev
->num_gpio_out
== 0);
358 dev
->num_gpio_out
= n
;
359 dev
->gpio_out
= pins
;
362 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
364 assert(n
>= 0 && n
< dev
->num_gpio_in
);
365 return dev
->gpio_in
[n
];
368 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
370 assert(n
>= 0 && n
< dev
->num_gpio_out
);
371 dev
->gpio_out
[n
] = pin
;
374 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
376 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
);
378 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
380 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
381 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
382 qdev_prop_exists(dev
, "vectors")) {
383 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
387 static int next_block_unit
[IF_COUNT
];
389 /* Get a block device. This should only be used for single-drive devices
390 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
392 BlockDriverState
*qdev_init_bdrv(DeviceState
*dev
, BlockInterfaceType type
)
394 int unit
= next_block_unit
[type
]++;
397 dinfo
= drive_get(type
, 0, unit
);
398 return dinfo
? dinfo
->bdrv
: NULL
;
401 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
405 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
406 if (strcmp(name
, bus
->name
) == 0) {
413 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
417 BusState
*child
, *ret
;
420 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
423 if (info
&& (bus
->info
!= info
)) {
430 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
431 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
432 ret
= qbus_find_recursive(child
, name
, info
);
441 static DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
443 DeviceState
*dev
, *ret
;
446 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
447 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
449 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
450 ret
= qdev_find_recursive(child
, id
);
459 static void qbus_list_bus(DeviceState
*dev
)
462 const char *sep
= " ";
464 error_printf("child busses at \"%s\":",
465 dev
->id
? dev
->id
: dev
->info
->name
);
466 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
467 error_printf("%s\"%s\"", sep
, child
->name
);
473 static void qbus_list_dev(BusState
*bus
)
476 const char *sep
= " ";
478 error_printf("devices at \"%s\":", bus
->name
);
479 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
480 error_printf("%s\"%s\"", sep
, dev
->info
->name
);
482 error_printf("/\"%s\"", dev
->id
);
488 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
492 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
493 if (strcmp(child
->name
, elem
) == 0) {
500 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
505 * try to match in order:
506 * (1) instance id, if present
508 * (3) driver alias, if present
510 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
511 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
515 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
516 if (strcmp(dev
->info
->name
, elem
) == 0) {
520 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
521 if (dev
->info
->alias
&& strcmp(dev
->info
->alias
, elem
) == 0) {
528 static BusState
*qbus_find(const char *path
)
535 /* find start element */
536 if (path
[0] == '/') {
537 bus
= main_system_bus
;
540 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
541 qemu_error("path parse error (\"%s\")\n", path
);
544 bus
= qbus_find_recursive(main_system_bus
, elem
, NULL
);
546 qemu_error("bus \"%s\" not found\n", elem
);
553 if (path
[pos
] == '\0') {
559 if (sscanf(path
+pos
, "/%127[^/]%n", elem
, &len
) != 1) {
560 qemu_error("path parse error (\"%s\" pos %d)\n", path
, pos
);
564 dev
= qbus_find_dev(bus
, elem
);
566 qemu_error("device \"%s\" not found\n", elem
);
570 if (path
[pos
] == '\0') {
571 /* last specified element is a device. If it has exactly
572 * one child bus accept it nevertheless */
573 switch (dev
->num_child_bus
) {
575 qemu_error("device has no child bus (%s)\n", path
);
578 return QLIST_FIRST(&dev
->child_bus
);
580 qemu_error("device has multiple child busses (%s)\n", path
);
587 if (sscanf(path
+pos
, "/%127[^/]%n", elem
, &len
) != 1) {
588 qemu_error("path parse error (\"%s\" pos %d)\n", path
, pos
);
592 bus
= qbus_find_bus(dev
, elem
);
594 qemu_error("child bus \"%s\" not found\n", elem
);
601 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
602 DeviceState
*parent
, const char *name
)
608 bus
->parent
= parent
;
611 /* use supplied name */
612 bus
->name
= qemu_strdup(name
);
613 } else if (parent
&& parent
->id
) {
614 /* parent device has id -> use it for bus name */
615 len
= strlen(parent
->id
) + 16;
616 buf
= qemu_malloc(len
);
617 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
620 /* no id -> use lowercase bus type for bus name */
621 len
= strlen(info
->name
) + 16;
622 buf
= qemu_malloc(len
);
623 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
624 parent
? parent
->num_child_bus
: 0);
625 for (i
= 0; i
< len
; i
++)
626 buf
[i
] = qemu_tolower(buf
[i
]);
630 QLIST_INIT(&bus
->children
);
632 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
633 parent
->num_child_bus
++;
638 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
642 bus
= qemu_mallocz(info
->size
);
643 bus
->qdev_allocated
= 1;
644 qbus_create_inplace(bus
, info
, parent
, name
);
648 void qbus_free(BusState
*bus
)
652 while ((dev
= QLIST_FIRST(&bus
->children
)) != NULL
) {
656 QLIST_REMOVE(bus
, sibling
);
657 bus
->parent
->num_child_bus
--;
659 if (bus
->qdev_allocated
) {
664 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
665 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
667 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
668 const char *prefix
, int indent
)
674 while (props
->name
) {
675 if (props
->info
->print
) {
676 props
->info
->print(dev
, props
, buf
, sizeof(buf
));
677 qdev_printf("%s-prop: %s = %s\n", prefix
, props
->name
, buf
);
683 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
686 qdev_printf("dev: %s, id \"%s\"\n", dev
->info
->name
,
687 dev
->id
? dev
->id
: "");
689 if (dev
->num_gpio_in
) {
690 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
692 if (dev
->num_gpio_out
) {
693 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
695 qdev_print_props(mon
, dev
, dev
->info
->props
, "dev", indent
);
696 qdev_print_props(mon
, dev
, dev
->parent_bus
->info
->props
, "bus", indent
);
697 if (dev
->parent_bus
->info
->print_dev
)
698 dev
->parent_bus
->info
->print_dev(mon
, dev
, indent
);
699 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
700 qbus_print(mon
, child
, indent
);
704 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
706 struct DeviceState
*dev
;
708 qdev_printf("bus: %s\n", bus
->name
);
710 qdev_printf("type %s\n", bus
->info
->name
);
711 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
712 qdev_print(mon
, dev
, indent
);
717 void do_info_qtree(Monitor
*mon
)
720 qbus_print(mon
, main_system_bus
, 0);
723 void do_info_qdm(Monitor
*mon
)
727 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
728 qdev_print_devinfo(info
);
732 void do_device_add(Monitor
*mon
, const QDict
*qdict
)
736 opts
= qemu_opts_parse(&qemu_device_opts
,
737 qdict_get_str(qdict
, "config"), "driver");
739 if (qdev_device_help(opts
) || qdev_device_add(opts
) == NULL
) {
745 void do_device_del(Monitor
*mon
, const QDict
*qdict
)
747 const char *id
= qdict_get_str(qdict
, "id");
750 dev
= qdev_find_recursive(main_system_bus
, id
);
752 qemu_error("Device '%s' not found\n", id
);