]>
git.proxmox.com Git - qemu.git/blob - hw/qdev.c
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 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
34 static BusState
*main_system_bus
;
36 static DeviceInfo
*device_info_list
;
38 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
40 static BusState
*qbus_find(const char *path
);
42 /* Register a new device type. */
43 void qdev_register(DeviceInfo
*info
)
45 assert(info
->size
>= sizeof(DeviceState
));
48 info
->next
= device_info_list
;
49 device_info_list
= info
;
52 static DeviceInfo
*qdev_find_info(BusInfo
*bus_info
, const char *name
)
56 /* first check device names */
57 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
58 if (bus_info
&& info
->bus_info
!= bus_info
)
60 if (strcmp(info
->name
, name
) != 0)
65 /* failing that check the aliases */
66 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
67 if (bus_info
&& info
->bus_info
!= bus_info
)
71 if (strcmp(info
->alias
, name
) != 0)
78 /* Create a new device. This only initializes the device state structure
79 and allows properties to be set. qdev_init should be called to
80 initialize the actual device emulation. */
81 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
87 if (!main_system_bus
) {
88 main_system_bus
= qbus_create(&system_bus_info
, NULL
, "main-system-bus");
90 bus
= main_system_bus
;
93 info
= qdev_find_info(bus
->info
, name
);
95 hw_error("Unknown device '%s' for bus '%s'\n", name
, bus
->info
->name
);
98 dev
= qemu_mallocz(info
->size
);
100 dev
->parent_bus
= bus
;
101 qdev_prop_set_defaults(dev
, dev
->info
->props
);
102 qdev_prop_set_defaults(dev
, dev
->parent_bus
->info
->props
);
103 qdev_prop_set_compat(dev
);
104 QLIST_INSERT_HEAD(&bus
->children
, dev
, sibling
);
105 dev
->state
= DEV_STATE_CREATED
;
109 static int qdev_print_devinfo(DeviceInfo
*info
, char *dest
, int len
)
114 ret
= snprintf(dest
+pos
, len
-pos
, "name \"%s\", bus %s",
115 info
->name
, info
->bus_info
->name
);
116 pos
+= MIN(len
-pos
,ret
);
118 ret
= snprintf(dest
+pos
, len
-pos
, ", alias \"%s\"", info
->alias
);
119 pos
+= MIN(len
-pos
,ret
);
122 ret
= snprintf(dest
+pos
, len
-pos
, ", desc \"%s\"", info
->desc
);
123 pos
+= MIN(len
-pos
,ret
);
126 ret
= snprintf(dest
+pos
, len
-pos
, ", no-user");
127 pos
+= MIN(len
-pos
,ret
);
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 DeviceState
*qdev_device_add(QemuOpts
*opts
)
151 const char *driver
, *path
, *id
;
156 driver
= qemu_opt_get(opts
, "driver");
158 qemu_error("-device: no driver specified\n");
161 if (strcmp(driver
, "?") == 0) {
163 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
164 qdev_print_devinfo(info
, msg
, sizeof(msg
));
165 qemu_error("%s\n", msg
);
171 info
= qdev_find_info(NULL
, driver
);
173 qemu_error("Device \"%s\" not found. Try -device '?' for a list.\n",
178 qemu_error("device \"%s\" can't be added via command line\n",
184 path
= qemu_opt_get(opts
, "bus");
186 bus
= qbus_find(path
);
188 bus
= qbus_find_recursive(main_system_bus
, NULL
, info
->bus_info
);
191 qemu_error("Did not find %s bus for %s\n",
192 path
? path
: info
->bus_info
->name
, info
->name
);
196 /* create device, set properties */
197 qdev
= qdev_create(bus
, driver
);
198 id
= qemu_opts_id(opts
);
202 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
206 if (qdev_init(qdev
) != 0) {
213 /* Initialize a device. Device properties should be set before calling
214 this function. IRQs and MMIO regions should be connected/mapped after
215 calling this function. */
216 int qdev_init(DeviceState
*dev
)
220 assert(dev
->state
== DEV_STATE_CREATED
);
221 rc
= dev
->info
->init(dev
, dev
->info
);
224 if (dev
->info
->reset
)
225 qemu_register_reset(dev
->info
->reset
, dev
);
227 vmstate_register(-1, dev
->info
->vmsd
, dev
);
228 dev
->state
= DEV_STATE_INITIALIZED
;
232 /* Unlink device from bus and free the structure. */
233 void qdev_free(DeviceState
*dev
)
237 if (dev
->state
== DEV_STATE_INITIALIZED
) {
238 while (dev
->num_child_bus
) {
239 bus
= QLIST_FIRST(&dev
->child_bus
);
242 #if 0 /* FIXME: need sane vmstate_unregister function */
244 vmstate_unregister(dev
->info
->vmsd
, dev
);
246 if (dev
->info
->reset
)
247 qemu_unregister_reset(dev
->info
->reset
, dev
);
249 dev
->info
->exit(dev
);
251 QLIST_REMOVE(dev
, sibling
);
255 /* Get a character (serial) device interface. */
256 CharDriverState
*qdev_init_chardev(DeviceState
*dev
)
258 static int next_serial
;
259 static int next_virtconsole
;
260 /* FIXME: This is a nasty hack that needs to go away. */
261 if (strncmp(dev
->info
->name
, "virtio", 6) == 0) {
262 return virtcon_hds
[next_virtconsole
++];
264 return serial_hds
[next_serial
++];
268 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
270 return dev
->parent_bus
;
273 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
275 assert(dev
->num_gpio_in
== 0);
276 dev
->num_gpio_in
= n
;
277 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
280 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
282 assert(dev
->num_gpio_out
== 0);
283 dev
->num_gpio_out
= n
;
284 dev
->gpio_out
= pins
;
287 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
289 assert(n
>= 0 && n
< dev
->num_gpio_in
);
290 return dev
->gpio_in
[n
];
293 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
295 assert(n
>= 0 && n
< dev
->num_gpio_out
);
296 dev
->gpio_out
[n
] = pin
;
299 VLANClientState
*qdev_get_vlan_client(DeviceState
*dev
,
300 NetCanReceive
*can_receive
,
302 NetReceiveIOV
*receive_iov
,
306 NICInfo
*nd
= dev
->nd
;
308 nd
->vc
= qemu_new_vlan_client(nd
->vlan
, nd
->model
, nd
->name
, can_receive
,
309 receive
, receive_iov
, cleanup
, opaque
);
314 void qdev_get_macaddr(DeviceState
*dev
, uint8_t *macaddr
)
316 memcpy(macaddr
, dev
->nd
->macaddr
, 6);
319 static int next_block_unit
[IF_COUNT
];
321 /* Get a block device. This should only be used for single-drive devices
322 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
324 BlockDriverState
*qdev_init_bdrv(DeviceState
*dev
, BlockInterfaceType type
)
326 int unit
= next_block_unit
[type
]++;
329 dinfo
= drive_get(type
, 0, unit
);
330 return dinfo
? dinfo
->bdrv
: NULL
;
333 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
337 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
338 if (strcmp(name
, bus
->name
) == 0) {
345 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
349 BusState
*child
, *ret
;
352 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
355 if (info
&& (bus
->info
!= info
)) {
362 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
363 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
364 ret
= qbus_find_recursive(child
, name
, info
);
373 static void qbus_list_bus(DeviceState
*dev
, char *dest
, int len
)
376 const char *sep
= " ";
379 pos
+= snprintf(dest
+pos
, len
-pos
,"child busses at \"%s\":",
380 dev
->id
? dev
->id
: dev
->info
->name
);
381 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
382 pos
+= snprintf(dest
+pos
, len
-pos
, "%s\"%s\"", sep
, child
->name
);
387 static void qbus_list_dev(BusState
*bus
, char *dest
, int len
)
390 const char *sep
= " ";
393 pos
+= snprintf(dest
+pos
, len
-pos
, "devices at \"%s\":",
395 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
396 pos
+= snprintf(dest
+pos
, len
-pos
, "%s\"%s\"",
397 sep
, dev
->info
->name
);
399 pos
+= snprintf(dest
+pos
, len
-pos
, "/\"%s\"", dev
->id
);
404 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
408 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
409 if (strcmp(child
->name
, elem
) == 0) {
416 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
421 * try to match in order:
422 * (1) instance id, if present
424 * (3) driver alias, if present
426 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
427 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
431 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
432 if (strcmp(dev
->info
->name
, elem
) == 0) {
436 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
437 if (dev
->info
->alias
&& strcmp(dev
->info
->alias
, elem
) == 0) {
444 static BusState
*qbus_find(const char *path
)
448 char elem
[128], msg
[256];
451 /* find start element */
452 if (path
[0] == '/') {
453 bus
= main_system_bus
;
456 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
457 qemu_error("path parse error (\"%s\")\n", path
);
460 bus
= qbus_find_recursive(main_system_bus
, elem
, NULL
);
462 qemu_error("bus \"%s\" not found\n", elem
);
469 if (path
[pos
] == '\0') {
475 if (sscanf(path
+pos
, "/%127[^/]%n", elem
, &len
) != 1) {
476 qemu_error("path parse error (\"%s\" pos %d)\n", path
, pos
);
480 dev
= qbus_find_dev(bus
, elem
);
482 qbus_list_dev(bus
, msg
, sizeof(msg
));
483 qemu_error("device \"%s\" not found\n%s\n", elem
, msg
);
486 if (path
[pos
] == '\0') {
487 /* last specified element is a device. If it has exactly
488 * one child bus accept it nevertheless */
489 switch (dev
->num_child_bus
) {
491 qemu_error("device has no child bus (%s)\n", path
);
494 return QLIST_FIRST(&dev
->child_bus
);
496 qbus_list_bus(dev
, msg
, sizeof(msg
));
497 qemu_error("device has multiple child busses (%s)\n%s\n",
504 if (sscanf(path
+pos
, "/%127[^/]%n", elem
, &len
) != 1) {
505 qemu_error("path parse error (\"%s\" pos %d)\n", path
, pos
);
509 bus
= qbus_find_bus(dev
, elem
);
511 qbus_list_bus(dev
, msg
, sizeof(msg
));
512 qemu_error("child bus \"%s\" not found\n%s\n", elem
, msg
);
518 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
519 DeviceState
*parent
, const char *name
)
525 bus
->parent
= parent
;
528 /* use supplied name */
529 bus
->name
= qemu_strdup(name
);
530 } else if (parent
&& parent
->id
) {
531 /* parent device has id -> use it for bus name */
532 len
= strlen(parent
->id
) + 16;
533 buf
= qemu_malloc(len
);
534 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
537 /* no id -> use lowercase bus type for bus name */
538 len
= strlen(info
->name
) + 16;
539 buf
= qemu_malloc(len
);
540 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
541 parent
? parent
->num_child_bus
: 0);
542 for (i
= 0; i
< len
; i
++)
543 buf
[i
] = qemu_tolower(buf
[i
]);
547 QLIST_INIT(&bus
->children
);
549 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
550 parent
->num_child_bus
++;
555 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
559 bus
= qemu_mallocz(info
->size
);
560 bus
->qdev_allocated
= 1;
561 qbus_create_inplace(bus
, info
, parent
, name
);
565 void qbus_free(BusState
*bus
)
569 while ((dev
= QLIST_FIRST(&bus
->children
)) != NULL
) {
573 QLIST_REMOVE(bus
, sibling
);
574 bus
->parent
->num_child_bus
--;
576 if (bus
->qdev_allocated
) {
581 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
582 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
584 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
585 const char *prefix
, int indent
)
591 while (props
->name
) {
592 if (props
->info
->print
) {
593 props
->info
->print(dev
, props
, buf
, sizeof(buf
));
594 qdev_printf("%s-prop: %s = %s\n", prefix
, props
->name
, buf
);
600 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
603 qdev_printf("dev: %s, id \"%s\"\n", dev
->info
->name
,
604 dev
->id
? dev
->id
: "");
606 if (dev
->num_gpio_in
) {
607 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
609 if (dev
->num_gpio_out
) {
610 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
612 qdev_print_props(mon
, dev
, dev
->info
->props
, "dev", indent
);
613 qdev_print_props(mon
, dev
, dev
->parent_bus
->info
->props
, "bus", indent
);
614 if (dev
->parent_bus
->info
->print_dev
)
615 dev
->parent_bus
->info
->print_dev(mon
, dev
, indent
);
616 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
617 qbus_print(mon
, child
, indent
);
621 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
623 struct DeviceState
*dev
;
625 qdev_printf("bus: %s\n", bus
->name
);
627 qdev_printf("type %s\n", bus
->info
->name
);
628 QLIST_FOREACH(dev
, &bus
->children
, sibling
) {
629 qdev_print(mon
, dev
, indent
);
634 void do_info_qtree(Monitor
*mon
)
637 qbus_print(mon
, main_system_bus
, 0);
640 void do_info_qdm(Monitor
*mon
)
645 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
646 qdev_print_devinfo(info
, msg
, sizeof(msg
));
647 monitor_printf(mon
, "%s\n", msg
);