slirp: Remove unused return value of tftp_send_next_block
[qemu/agraf.git] / hw / qdev.c
blobb5a52ac503a0aec1fbef8afc20c75cdc4d8aa9c8
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 "error.h"
33 int qdev_hotplug = 0;
34 static bool qdev_hot_added = false;
35 static bool qdev_hot_removed = false;
37 /* Register a new device type. */
38 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
40 DeviceClass *dc = DEVICE_GET_CLASS(dev);
41 return dc->vmsd;
44 const char *qdev_fw_name(DeviceState *dev)
46 DeviceClass *dc = DEVICE_GET_CLASS(dev);
48 if (dc->fw_name) {
49 return dc->fw_name;
52 return object_get_typename(OBJECT(dev));
55 bool qdev_exists(const char *name)
57 return !!object_class_by_name(name);
60 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
61 Error **errp);
63 static void bus_remove_child(BusState *bus, DeviceState *child)
65 BusChild *kid;
67 QTAILQ_FOREACH(kid, &bus->children, sibling) {
68 if (kid->child == child) {
69 char name[32];
71 snprintf(name, sizeof(name), "child[%d]", kid->index);
72 QTAILQ_REMOVE(&bus->children, kid, sibling);
73 object_property_del(OBJECT(bus), name, NULL);
74 g_free(kid);
75 return;
80 static void bus_add_child(BusState *bus, DeviceState *child)
82 char name[32];
83 BusChild *kid = g_malloc0(sizeof(*kid));
85 if (qdev_hotplug) {
86 assert(bus->allow_hotplug);
89 kid->index = bus->max_index++;
90 kid->child = child;
92 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
94 snprintf(name, sizeof(name), "child[%d]", kid->index);
95 object_property_add_link(OBJECT(bus), name,
96 object_get_typename(OBJECT(child)),
97 (Object **)&kid->child,
98 NULL);
101 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
103 dev->parent_bus = bus;
104 bus_add_child(bus, dev);
107 /* Create a new device. This only initializes the device state structure
108 and allows properties to be set. qdev_init should be called to
109 initialize the actual device emulation. */
110 DeviceState *qdev_create(BusState *bus, const char *name)
112 DeviceState *dev;
114 dev = qdev_try_create(bus, name);
115 if (!dev) {
116 if (bus) {
117 hw_error("Unknown device '%s' for bus '%s'\n", name,
118 object_get_typename(OBJECT(bus)));
119 } else {
120 hw_error("Unknown device '%s' for default sysbus\n", name);
124 return dev;
127 DeviceState *qdev_try_create(BusState *bus, const char *type)
129 DeviceState *dev;
131 if (object_class_by_name(type) == NULL) {
132 return NULL;
134 dev = DEVICE(object_new(type));
135 if (!dev) {
136 return NULL;
139 if (!bus) {
140 bus = sysbus_get_default();
143 qdev_set_parent_bus(dev, bus);
145 return dev;
148 /* Initialize a device. Device properties should be set before calling
149 this function. IRQs and MMIO regions should be connected/mapped after
150 calling this function.
151 On failure, destroy the device and return negative value.
152 Return 0 on success. */
153 int qdev_init(DeviceState *dev)
155 DeviceClass *dc = DEVICE_GET_CLASS(dev);
156 int rc;
158 assert(dev->state == DEV_STATE_CREATED);
160 rc = dc->init(dev);
161 if (rc < 0) {
162 qdev_free(dev);
163 return rc;
166 if (!OBJECT(dev)->parent) {
167 static int unattached_count = 0;
168 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
170 object_property_add_child(container_get(qdev_get_machine(),
171 "/unattached"),
172 name, OBJECT(dev), NULL);
173 g_free(name);
176 if (qdev_get_vmsd(dev)) {
177 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
178 dev->instance_id_alias,
179 dev->alias_required_for_version);
181 dev->state = DEV_STATE_INITIALIZED;
182 if (dev->hotplugged) {
183 device_reset(dev);
185 return 0;
188 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
189 int required_for_version)
191 assert(dev->state == DEV_STATE_CREATED);
192 dev->instance_id_alias = alias_id;
193 dev->alias_required_for_version = required_for_version;
196 void qdev_unplug(DeviceState *dev, Error **errp)
198 DeviceClass *dc = DEVICE_GET_CLASS(dev);
200 if (!dev->parent_bus->allow_hotplug) {
201 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
202 return;
204 assert(dc->unplug != NULL);
206 qdev_hot_removed = true;
208 if (dc->unplug(dev) < 0) {
209 error_set(errp, QERR_UNDEFINED_ERROR);
210 return;
214 static int qdev_reset_one(DeviceState *dev, void *opaque)
216 device_reset(dev);
218 return 0;
221 static int qbus_reset_one(BusState *bus, void *opaque)
223 BusClass *bc = BUS_GET_CLASS(bus);
224 if (bc->reset) {
225 return bc->reset(bus);
227 return 0;
230 void qdev_reset_all(DeviceState *dev)
232 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
235 void qbus_reset_all_fn(void *opaque)
237 BusState *bus = opaque;
238 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
241 /* can be used as ->unplug() callback for the simple cases */
242 int qdev_simple_unplug_cb(DeviceState *dev)
244 /* just zap it */
245 qdev_free(dev);
246 return 0;
250 /* Like qdev_init(), but terminate program via error_report() instead of
251 returning an error value. This is okay during machine creation.
252 Don't use for hotplug, because there callers need to recover from
253 failure. Exception: if you know the device's init() callback can't
254 fail, then qdev_init_nofail() can't fail either, and is therefore
255 usable even then. But relying on the device implementation that
256 way is somewhat unclean, and best avoided. */
257 void qdev_init_nofail(DeviceState *dev)
259 const char *typename = object_get_typename(OBJECT(dev));
261 if (qdev_init(dev) < 0) {
262 error_report("Initialization of device %s failed", typename);
263 exit(1);
267 /* Unlink device from bus and free the structure. */
268 void qdev_free(DeviceState *dev)
270 object_delete(OBJECT(dev));
273 void qdev_machine_creation_done(void)
276 * ok, initial machine setup is done, starting from now we can
277 * only create hotpluggable devices
279 qdev_hotplug = 1;
282 bool qdev_machine_modified(void)
284 return qdev_hot_added || qdev_hot_removed;
287 BusState *qdev_get_parent_bus(DeviceState *dev)
289 return dev->parent_bus;
292 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
294 assert(dev->num_gpio_in == 0);
295 dev->num_gpio_in = n;
296 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
299 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
301 assert(dev->num_gpio_out == 0);
302 dev->num_gpio_out = n;
303 dev->gpio_out = pins;
306 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
308 assert(n >= 0 && n < dev->num_gpio_in);
309 return dev->gpio_in[n];
312 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
314 assert(n >= 0 && n < dev->num_gpio_out);
315 dev->gpio_out[n] = pin;
318 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
320 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
321 if (nd->netdev)
322 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
323 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
324 object_property_find(OBJECT(dev), "vectors", NULL)) {
325 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
327 nd->instantiated = 1;
330 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
332 BusState *bus;
334 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
335 if (strcmp(name, bus->name) == 0) {
336 return bus;
339 return NULL;
342 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
343 qbus_walkerfn *busfn, void *opaque)
345 BusChild *kid;
346 int err;
348 if (busfn) {
349 err = busfn(bus, opaque);
350 if (err) {
351 return err;
355 QTAILQ_FOREACH(kid, &bus->children, sibling) {
356 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
357 if (err < 0) {
358 return err;
362 return 0;
365 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
366 qbus_walkerfn *busfn, void *opaque)
368 BusState *bus;
369 int err;
371 if (devfn) {
372 err = devfn(dev, opaque);
373 if (err) {
374 return err;
378 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
379 err = qbus_walk_children(bus, devfn, busfn, opaque);
380 if (err < 0) {
381 return err;
385 return 0;
388 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
390 BusChild *kid;
391 DeviceState *ret;
392 BusState *child;
394 QTAILQ_FOREACH(kid, &bus->children, sibling) {
395 DeviceState *dev = kid->child;
397 if (dev->id && strcmp(dev->id, id) == 0) {
398 return dev;
401 QLIST_FOREACH(child, &dev->child_bus, sibling) {
402 ret = qdev_find_recursive(child, id);
403 if (ret) {
404 return ret;
408 return NULL;
411 static void qbus_realize(BusState *bus)
413 const char *typename = object_get_typename(OBJECT(bus));
414 char *buf;
415 int i,len;
417 if (bus->name) {
418 /* use supplied name */
419 } else if (bus->parent && bus->parent->id) {
420 /* parent device has id -> use it for bus name */
421 len = strlen(bus->parent->id) + 16;
422 buf = g_malloc(len);
423 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
424 bus->name = buf;
425 } else {
426 /* no id -> use lowercase bus type for bus name */
427 len = strlen(typename) + 16;
428 buf = g_malloc(len);
429 len = snprintf(buf, len, "%s.%d", typename,
430 bus->parent ? bus->parent->num_child_bus : 0);
431 for (i = 0; i < len; i++)
432 buf[i] = qemu_tolower(buf[i]);
433 bus->name = buf;
436 if (bus->parent) {
437 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
438 bus->parent->num_child_bus++;
439 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
440 } else if (bus != sysbus_get_default()) {
441 /* TODO: once all bus devices are qdevified,
442 only reset handler for main_system_bus should be registered here. */
443 qemu_register_reset(qbus_reset_all_fn, bus);
447 void qbus_create_inplace(BusState *bus, const char *typename,
448 DeviceState *parent, const char *name)
450 object_initialize(bus, typename);
452 bus->parent = parent;
453 bus->name = name ? g_strdup(name) : NULL;
454 qbus_realize(bus);
457 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
459 BusState *bus;
461 bus = BUS(object_new(typename));
462 bus->qom_allocated = true;
464 bus->parent = parent;
465 bus->name = name ? g_strdup(name) : NULL;
466 qbus_realize(bus);
468 return bus;
471 void qbus_free(BusState *bus)
473 if (bus->qom_allocated) {
474 object_delete(OBJECT(bus));
475 } else {
476 object_finalize(OBJECT(bus));
477 if (bus->glib_allocated) {
478 g_free(bus);
483 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
485 BusClass *bc = BUS_GET_CLASS(bus);
487 if (bc->get_fw_dev_path) {
488 return bc->get_fw_dev_path(dev);
491 return NULL;
494 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
496 int l = 0;
498 if (dev && dev->parent_bus) {
499 char *d;
500 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
501 d = bus_get_fw_dev_path(dev->parent_bus, dev);
502 if (d) {
503 l += snprintf(p + l, size - l, "%s", d);
504 g_free(d);
505 } else {
506 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
509 l += snprintf(p + l , size - l, "/");
511 return l;
514 char* qdev_get_fw_dev_path(DeviceState *dev)
516 char path[128];
517 int l;
519 l = qdev_get_fw_dev_path_helper(dev, path, 128);
521 path[l-1] = '\0';
523 return strdup(path);
526 char *qdev_get_dev_path(DeviceState *dev)
528 BusClass *bc;
530 if (!dev || !dev->parent_bus) {
531 return NULL;
534 bc = BUS_GET_CLASS(dev->parent_bus);
535 if (bc->get_dev_path) {
536 return bc->get_dev_path(dev);
539 return NULL;
543 * Legacy property handling
546 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
547 const char *name, Error **errp)
549 DeviceState *dev = DEVICE(obj);
550 Property *prop = opaque;
552 char buffer[1024];
553 char *ptr = buffer;
555 prop->info->print(dev, prop, buffer, sizeof(buffer));
556 visit_type_str(v, &ptr, name, errp);
559 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
560 const char *name, Error **errp)
562 DeviceState *dev = DEVICE(obj);
563 Property *prop = opaque;
564 Error *local_err = NULL;
565 char *ptr = NULL;
566 int ret;
568 if (dev->state != DEV_STATE_CREATED) {
569 error_set(errp, QERR_PERMISSION_DENIED);
570 return;
573 visit_type_str(v, &ptr, name, &local_err);
574 if (local_err) {
575 error_propagate(errp, local_err);
576 return;
579 ret = prop->info->parse(dev, prop, ptr);
580 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
581 g_free(ptr);
585 * @qdev_add_legacy_property - adds a legacy property
587 * Do not use this is new code! Properties added through this interface will
588 * be given names and types in the "legacy" namespace.
590 * Legacy properties are string versions of other OOM properties. The format
591 * of the string depends on the property type.
593 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
594 Error **errp)
596 gchar *name, *type;
598 /* Register pointer properties as legacy properties */
599 if (!prop->info->print && !prop->info->parse &&
600 (prop->info->set || prop->info->get)) {
601 return;
604 name = g_strdup_printf("legacy-%s", prop->name);
605 type = g_strdup_printf("legacy<%s>",
606 prop->info->legacy_name ?: prop->info->name);
608 object_property_add(OBJECT(dev), name, type,
609 prop->info->print ? qdev_get_legacy_property : prop->info->get,
610 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
611 NULL,
612 prop, errp);
614 g_free(type);
615 g_free(name);
619 * @qdev_property_add_static - add a @Property to a device.
621 * Static properties access data in a struct. The actual type of the
622 * property and the field depends on the property type.
624 void qdev_property_add_static(DeviceState *dev, Property *prop,
625 Error **errp)
627 Error *local_err = NULL;
628 Object *obj = OBJECT(dev);
631 * TODO qdev_prop_ptr does not have getters or setters. It must
632 * go now that it can be replaced with links. The test should be
633 * removed along with it: all static properties are read/write.
635 if (!prop->info->get && !prop->info->set) {
636 return;
639 object_property_add(obj, prop->name, prop->info->name,
640 prop->info->get, prop->info->set,
641 prop->info->release,
642 prop, &local_err);
644 if (local_err) {
645 error_propagate(errp, local_err);
646 return;
648 if (prop->qtype == QTYPE_NONE) {
649 return;
652 if (prop->qtype == QTYPE_QBOOL) {
653 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
654 } else if (prop->info->enum_table) {
655 object_property_set_str(obj, prop->info->enum_table[prop->defval],
656 prop->name, &local_err);
657 } else if (prop->qtype == QTYPE_QINT) {
658 object_property_set_int(obj, prop->defval, prop->name, &local_err);
660 assert_no_error(local_err);
663 static void device_initfn(Object *obj)
665 DeviceState *dev = DEVICE(obj);
666 ObjectClass *class;
667 Property *prop;
669 if (qdev_hotplug) {
670 dev->hotplugged = 1;
671 qdev_hot_added = true;
674 dev->instance_id_alias = -1;
675 dev->state = DEV_STATE_CREATED;
677 class = object_get_class(OBJECT(dev));
678 do {
679 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
680 qdev_property_add_legacy(dev, prop, NULL);
681 qdev_property_add_static(dev, prop, NULL);
683 class = object_class_get_parent(class);
684 } while (class != object_class_by_name(TYPE_DEVICE));
685 qdev_prop_set_globals(dev);
687 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
688 (Object **)&dev->parent_bus, NULL);
691 /* Unlink device from bus and free the structure. */
692 static void device_finalize(Object *obj)
694 DeviceState *dev = DEVICE(obj);
695 BusState *bus;
696 DeviceClass *dc = DEVICE_GET_CLASS(dev);
698 if (dev->state == DEV_STATE_INITIALIZED) {
699 while (dev->num_child_bus) {
700 bus = QLIST_FIRST(&dev->child_bus);
701 qbus_free(bus);
703 if (qdev_get_vmsd(dev)) {
704 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
706 if (dc->exit) {
707 dc->exit(dev);
709 if (dev->opts) {
710 qemu_opts_del(dev->opts);
713 if (dev->parent_bus) {
714 bus_remove_child(dev->parent_bus, dev);
718 static void device_class_base_init(ObjectClass *class, void *data)
720 DeviceClass *klass = DEVICE_CLASS(class);
722 /* We explicitly look up properties in the superclasses,
723 * so do not propagate them to the subclasses.
725 klass->props = NULL;
728 void device_reset(DeviceState *dev)
730 DeviceClass *klass = DEVICE_GET_CLASS(dev);
732 if (klass->reset) {
733 klass->reset(dev);
737 Object *qdev_get_machine(void)
739 static Object *dev;
741 if (dev == NULL) {
742 dev = container_get(object_get_root(), "/machine");
745 return dev;
748 static TypeInfo device_type_info = {
749 .name = TYPE_DEVICE,
750 .parent = TYPE_OBJECT,
751 .instance_size = sizeof(DeviceState),
752 .instance_init = device_initfn,
753 .instance_finalize = device_finalize,
754 .class_base_init = device_class_base_init,
755 .abstract = true,
756 .class_size = sizeof(DeviceClass),
759 static void qbus_initfn(Object *obj)
761 BusState *bus = BUS(obj);
763 QTAILQ_INIT(&bus->children);
766 static void qbus_finalize(Object *obj)
768 BusState *bus = BUS(obj);
769 BusChild *kid;
771 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
772 DeviceState *dev = kid->child;
773 qdev_free(dev);
775 if (bus->parent) {
776 QLIST_REMOVE(bus, sibling);
777 bus->parent->num_child_bus--;
778 } else {
779 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
780 qemu_unregister_reset(qbus_reset_all_fn, bus);
782 g_free((char *)bus->name);
785 static const TypeInfo bus_info = {
786 .name = TYPE_BUS,
787 .parent = TYPE_OBJECT,
788 .instance_size = sizeof(BusState),
789 .abstract = true,
790 .class_size = sizeof(BusClass),
791 .instance_init = qbus_initfn,
792 .instance_finalize = qbus_finalize,
795 static void qdev_register_types(void)
797 type_register_static(&bus_info);
798 type_register_static(&device_type_info);
801 type_init(qdev_register_types)