qdev: Convert qbus_find() to QError
[qemu/kevin.git] / hw / qdev.c
blob27dc8dfe85dc10d26f374758444119968ea3da45
1 /*
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
26 this API directly. */
28 #include "net.h"
29 #include "qdev.h"
30 #include "sysemu.h"
31 #include "monitor.h"
32 #include "qerror.h"
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,
42 const BusInfo *info);
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));
49 assert(!info->next);
51 info->next = device_info_list;
52 device_info_list = info;
55 static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
57 DeviceInfo *info;
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)
62 continue;
63 if (strcmp(info->name, name) != 0)
64 continue;
65 return info;
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)
71 continue;
72 if (!info->alias)
73 continue;
74 if (strcmp(info->alias, name) != 0)
75 continue;
76 return info;
78 return NULL;
81 static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
83 DeviceState *dev;
85 assert(bus->info == info->bus_info);
86 dev = qemu_mallocz(info->size);
87 dev->info = info;
88 dev->parent_bus = bus;
89 qdev_prop_set_defaults(dev, dev->info->props);
90 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
91 qdev_prop_set_globals(dev);
92 QLIST_INSERT_HEAD(&bus->children, dev, sibling);
93 if (qdev_hotplug) {
94 assert(bus->allow_hotplug);
95 dev->hotplugged = 1;
97 dev->state = DEV_STATE_CREATED;
98 return dev;
101 /* Create a new device. This only initializes the device state structure
102 and allows properties to be set. qdev_init should be called to
103 initialize the actual device emulation. */
104 DeviceState *qdev_create(BusState *bus, const char *name)
106 DeviceInfo *info;
108 if (!bus) {
109 if (!main_system_bus) {
110 main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus");
112 bus = main_system_bus;
115 info = qdev_find_info(bus->info, name);
116 if (!info) {
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);
127 if (info->alias) {
128 error_printf(", alias \"%s\"", info->alias);
130 if (info->desc) {
131 error_printf(", desc \"%s\"", info->desc);
133 if (info->no_user) {
134 error_printf(", no-user");
136 error_printf("\n");
139 static int set_property(const char *name, const char *value, void *opaque)
141 DeviceState *dev = opaque;
143 if (strcmp(name, "driver") == 0)
144 return 0;
145 if (strcmp(name, "bus") == 0)
146 return 0;
148 if (qdev_prop_parse(dev, name, value) == -1) {
149 return -1;
151 return 0;
154 int qdev_device_help(QemuOpts *opts)
156 const char *driver;
157 DeviceInfo *info;
158 Property *prop;
160 driver = qemu_opt_get(opts, "driver");
161 if (driver && !strcmp(driver, "?")) {
162 for (info = device_info_list; info != NULL; info = info->next) {
163 if (info->no_user) {
164 continue; /* not available, don't show */
166 qdev_print_devinfo(info);
168 return 1;
171 if (!qemu_opt_get(opts, "?")) {
172 return 0;
175 info = qdev_find_info(NULL, driver);
176 if (!info) {
177 return 0;
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
185 * it.
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);
192 return 1;
195 DeviceState *qdev_device_add(QemuOpts *opts)
197 const char *driver, *path, *id;
198 DeviceInfo *info;
199 DeviceState *qdev;
200 BusState *bus;
202 driver = qemu_opt_get(opts, "driver");
203 if (!driver) {
204 error_report("-device: no driver specified");
205 return NULL;
208 /* find driver */
209 info = qdev_find_info(NULL, driver);
210 if (!info || info->no_user) {
211 qerror_report(QERR_DEVICE_NOT_FOUND, driver);
212 return NULL;
215 /* find bus */
216 path = qemu_opt_get(opts, "bus");
217 if (path != NULL) {
218 bus = qbus_find(path);
219 if (!bus) {
220 return NULL;
222 if (bus->info != info->bus_info) {
223 error_report("Device '%s' can't go on a %s bus",
224 driver, bus->info->name);
225 return NULL;
227 } else {
228 bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
229 if (!bus) {
230 error_report("Did not find %s bus for %s",
231 info->bus_info->name, info->name);
232 return NULL;
235 if (qdev_hotplug && !bus->allow_hotplug) {
236 error_report("Bus %s does not support hotplugging",
237 bus->name);
238 return NULL;
241 /* create device, set properties */
242 qdev = qdev_create_from_info(bus, info);
243 id = qemu_opts_id(opts);
244 if (id) {
245 qdev->id = id;
247 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
248 qdev_free(qdev);
249 return NULL;
251 if (qdev_init(qdev) < 0) {
252 error_report("Error initializing device %s", driver);
253 return NULL;
255 qdev->opts = opts;
256 return qdev;
259 static void qdev_reset(void *opaque)
261 DeviceState *dev = opaque;
262 if (dev->info->reset)
263 dev->info->reset(dev);
266 /* Initialize a device. Device properties should be set before calling
267 this function. IRQs and MMIO regions should be connected/mapped after
268 calling this function.
269 On failure, destroy the device and return negative value.
270 Return 0 on success. */
271 int qdev_init(DeviceState *dev)
273 int rc;
275 assert(dev->state == DEV_STATE_CREATED);
276 rc = dev->info->init(dev, dev->info);
277 if (rc < 0) {
278 qdev_free(dev);
279 return rc;
281 qemu_register_reset(qdev_reset, dev);
282 if (dev->info->vmsd)
283 vmstate_register(-1, dev->info->vmsd, dev);
284 dev->state = DEV_STATE_INITIALIZED;
285 return 0;
288 int qdev_unplug(DeviceState *dev)
290 if (!dev->parent_bus->allow_hotplug) {
291 error_report("Bus %s does not support hotplugging",
292 dev->parent_bus->name);
293 return -1;
295 assert(dev->info->unplug != NULL);
297 return dev->info->unplug(dev);
300 /* can be used as ->unplug() callback for the simple cases */
301 int qdev_simple_unplug_cb(DeviceState *dev)
303 /* just zap it */
304 qdev_free(dev);
305 return 0;
308 /* Like qdev_init(), but terminate program via hw_error() instead of
309 returning an error value. This is okay during machine creation.
310 Don't use for hotplug, because there callers need to recover from
311 failure. Exception: if you know the device's init() callback can't
312 fail, then qdev_init_nofail() can't fail either, and is therefore
313 usable even then. But relying on the device implementation that
314 way is somewhat unclean, and best avoided. */
315 void qdev_init_nofail(DeviceState *dev)
317 DeviceInfo *info = dev->info;
319 if (qdev_init(dev) < 0)
320 hw_error("Initialization of device %s failed\n", info->name);
323 /* Unlink device from bus and free the structure. */
324 void qdev_free(DeviceState *dev)
326 BusState *bus;
328 if (dev->state == DEV_STATE_INITIALIZED) {
329 while (dev->num_child_bus) {
330 bus = QLIST_FIRST(&dev->child_bus);
331 qbus_free(bus);
333 if (dev->info->vmsd)
334 vmstate_unregister(dev->info->vmsd, dev);
335 if (dev->info->exit)
336 dev->info->exit(dev);
337 if (dev->opts)
338 qemu_opts_del(dev->opts);
340 qemu_unregister_reset(qdev_reset, dev);
341 QLIST_REMOVE(dev, sibling);
342 qemu_free(dev);
345 void qdev_machine_creation_done(void)
348 * ok, initial machine setup is done, starting from now we can
349 * only create hotpluggable devices
351 qdev_hotplug = 1;
354 /* Get a character (serial) device interface. */
355 CharDriverState *qdev_init_chardev(DeviceState *dev)
357 static int next_serial;
359 /* FIXME: This function needs to go away: use chardev properties! */
360 return serial_hds[next_serial++];
363 BusState *qdev_get_parent_bus(DeviceState *dev)
365 return dev->parent_bus;
368 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
370 assert(dev->num_gpio_in == 0);
371 dev->num_gpio_in = n;
372 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
375 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
377 assert(dev->num_gpio_out == 0);
378 dev->num_gpio_out = n;
379 dev->gpio_out = pins;
382 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
384 assert(n >= 0 && n < dev->num_gpio_in);
385 return dev->gpio_in[n];
388 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
390 assert(n >= 0 && n < dev->num_gpio_out);
391 dev->gpio_out[n] = pin;
394 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
396 qdev_prop_set_macaddr(dev, "mac", nd->macaddr);
397 if (nd->vlan)
398 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
399 if (nd->netdev)
400 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
401 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
402 qdev_prop_exists(dev, "vectors")) {
403 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
407 static int next_block_unit[IF_COUNT];
409 /* Get a block device. This should only be used for single-drive devices
410 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
411 appropriate bus. */
412 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
414 int unit = next_block_unit[type]++;
415 DriveInfo *dinfo;
417 dinfo = drive_get(type, 0, unit);
418 return dinfo ? dinfo->bdrv : NULL;
421 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
423 BusState *bus;
425 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
426 if (strcmp(name, bus->name) == 0) {
427 return bus;
430 return NULL;
433 static BusState *qbus_find_recursive(BusState *bus, const char *name,
434 const BusInfo *info)
436 DeviceState *dev;
437 BusState *child, *ret;
438 int match = 1;
440 if (name && (strcmp(bus->name, name) != 0)) {
441 match = 0;
443 if (info && (bus->info != info)) {
444 match = 0;
446 if (match) {
447 return bus;
450 QLIST_FOREACH(dev, &bus->children, sibling) {
451 QLIST_FOREACH(child, &dev->child_bus, sibling) {
452 ret = qbus_find_recursive(child, name, info);
453 if (ret) {
454 return ret;
458 return NULL;
461 static DeviceState *qdev_find_recursive(BusState *bus, const char *id)
463 DeviceState *dev, *ret;
464 BusState *child;
466 QLIST_FOREACH(dev, &bus->children, sibling) {
467 if (dev->id && strcmp(dev->id, id) == 0)
468 return dev;
469 QLIST_FOREACH(child, &dev->child_bus, sibling) {
470 ret = qdev_find_recursive(child, id);
471 if (ret) {
472 return ret;
476 return NULL;
479 static void qbus_list_bus(DeviceState *dev)
481 BusState *child;
482 const char *sep = " ";
484 error_printf("child busses at \"%s\":",
485 dev->id ? dev->id : dev->info->name);
486 QLIST_FOREACH(child, &dev->child_bus, sibling) {
487 error_printf("%s\"%s\"", sep, child->name);
488 sep = ", ";
490 error_printf("\n");
493 static void qbus_list_dev(BusState *bus)
495 DeviceState *dev;
496 const char *sep = " ";
498 error_printf("devices at \"%s\":", bus->name);
499 QLIST_FOREACH(dev, &bus->children, sibling) {
500 error_printf("%s\"%s\"", sep, dev->info->name);
501 if (dev->id)
502 error_printf("/\"%s\"", dev->id);
503 sep = ", ";
505 error_printf("\n");
508 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
510 BusState *child;
512 QLIST_FOREACH(child, &dev->child_bus, sibling) {
513 if (strcmp(child->name, elem) == 0) {
514 return child;
517 return NULL;
520 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
522 DeviceState *dev;
525 * try to match in order:
526 * (1) instance id, if present
527 * (2) driver name
528 * (3) driver alias, if present
530 QLIST_FOREACH(dev, &bus->children, sibling) {
531 if (dev->id && strcmp(dev->id, elem) == 0) {
532 return dev;
535 QLIST_FOREACH(dev, &bus->children, sibling) {
536 if (strcmp(dev->info->name, elem) == 0) {
537 return dev;
540 QLIST_FOREACH(dev, &bus->children, sibling) {
541 if (dev->info->alias && strcmp(dev->info->alias, elem) == 0) {
542 return dev;
545 return NULL;
548 static BusState *qbus_find(const char *path)
550 DeviceState *dev;
551 BusState *bus;
552 char elem[128];
553 int pos, len;
555 /* find start element */
556 if (path[0] == '/') {
557 bus = main_system_bus;
558 pos = 0;
559 } else {
560 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
561 assert(!path[0]);
562 elem[0] = len = 0;
564 bus = qbus_find_recursive(main_system_bus, elem, NULL);
565 if (!bus) {
566 qerror_report(QERR_BUS_NOT_FOUND, elem);
567 return NULL;
569 pos = len;
572 for (;;) {
573 assert(path[pos] == '/' || !path[pos]);
574 while (path[pos] == '/') {
575 pos++;
577 if (path[pos] == '\0') {
578 return bus;
581 /* find device */
582 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
583 assert(0);
584 elem[0] = len = 0;
586 pos += len;
587 dev = qbus_find_dev(bus, elem);
588 if (!dev) {
589 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
590 qbus_list_dev(bus);
591 return NULL;
594 assert(path[pos] == '/' || !path[pos]);
595 while (path[pos] == '/') {
596 pos++;
598 if (path[pos] == '\0') {
599 /* last specified element is a device. If it has exactly
600 * one child bus accept it nevertheless */
601 switch (dev->num_child_bus) {
602 case 0:
603 qerror_report(QERR_DEVICE_NO_BUS, elem);
604 return NULL;
605 case 1:
606 return QLIST_FIRST(&dev->child_bus);
607 default:
608 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
609 qbus_list_bus(dev);
610 return NULL;
614 /* find bus */
615 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
616 assert(0);
617 elem[0] = len = 0;
619 pos += len;
620 bus = qbus_find_bus(dev, elem);
621 if (!bus) {
622 qerror_report(QERR_BUS_NOT_FOUND, elem);
623 qbus_list_bus(dev);
624 return NULL;
629 void qbus_create_inplace(BusState *bus, BusInfo *info,
630 DeviceState *parent, const char *name)
632 char *buf;
633 int i,len;
635 bus->info = info;
636 bus->parent = parent;
638 if (name) {
639 /* use supplied name */
640 bus->name = qemu_strdup(name);
641 } else if (parent && parent->id) {
642 /* parent device has id -> use it for bus name */
643 len = strlen(parent->id) + 16;
644 buf = qemu_malloc(len);
645 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
646 bus->name = buf;
647 } else {
648 /* no id -> use lowercase bus type for bus name */
649 len = strlen(info->name) + 16;
650 buf = qemu_malloc(len);
651 len = snprintf(buf, len, "%s.%d", info->name,
652 parent ? parent->num_child_bus : 0);
653 for (i = 0; i < len; i++)
654 buf[i] = qemu_tolower(buf[i]);
655 bus->name = buf;
658 QLIST_INIT(&bus->children);
659 if (parent) {
660 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
661 parent->num_child_bus++;
666 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
668 BusState *bus;
670 bus = qemu_mallocz(info->size);
671 bus->qdev_allocated = 1;
672 qbus_create_inplace(bus, info, parent, name);
673 return bus;
676 void qbus_free(BusState *bus)
678 DeviceState *dev;
680 while ((dev = QLIST_FIRST(&bus->children)) != NULL) {
681 qdev_free(dev);
683 if (bus->parent) {
684 QLIST_REMOVE(bus, sibling);
685 bus->parent->num_child_bus--;
687 if (bus->qdev_allocated) {
688 qemu_free(bus);
692 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
693 static void qbus_print(Monitor *mon, BusState *bus, int indent);
695 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
696 const char *prefix, int indent)
698 char buf[64];
700 if (!props)
701 return;
702 while (props->name) {
704 * TODO Properties without a print method are just for dirty
705 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
706 * marked for removal. The test props->info->print should be
707 * removed along with it.
709 if (props->info->print) {
710 props->info->print(dev, props, buf, sizeof(buf));
711 qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
713 props++;
717 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
719 BusState *child;
720 qdev_printf("dev: %s, id \"%s\"\n", dev->info->name,
721 dev->id ? dev->id : "");
722 indent += 2;
723 if (dev->num_gpio_in) {
724 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
726 if (dev->num_gpio_out) {
727 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
729 qdev_print_props(mon, dev, dev->info->props, "dev", indent);
730 qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
731 if (dev->parent_bus->info->print_dev)
732 dev->parent_bus->info->print_dev(mon, dev, indent);
733 QLIST_FOREACH(child, &dev->child_bus, sibling) {
734 qbus_print(mon, child, indent);
738 static void qbus_print(Monitor *mon, BusState *bus, int indent)
740 struct DeviceState *dev;
742 qdev_printf("bus: %s\n", bus->name);
743 indent += 2;
744 qdev_printf("type %s\n", bus->info->name);
745 QLIST_FOREACH(dev, &bus->children, sibling) {
746 qdev_print(mon, dev, indent);
749 #undef qdev_printf
751 void do_info_qtree(Monitor *mon)
753 if (main_system_bus)
754 qbus_print(mon, main_system_bus, 0);
757 void do_info_qdm(Monitor *mon)
759 DeviceInfo *info;
761 for (info = device_info_list; info != NULL; info = info->next) {
762 qdev_print_devinfo(info);
766 void do_device_add(Monitor *mon, const QDict *qdict)
768 QemuOpts *opts;
770 opts = qemu_opts_parse(&qemu_device_opts,
771 qdict_get_str(qdict, "config"), "driver");
772 if (opts) {
773 if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
774 qemu_opts_del(opts);
779 void do_device_del(Monitor *mon, const QDict *qdict)
781 const char *id = qdict_get_str(qdict, "id");
782 DeviceState *dev;
784 dev = qdev_find_recursive(main_system_bus, id);
785 if (NULL == dev) {
786 error_report("Device '%s' not found", id);
787 return;
789 qdev_unplug(dev);