target-i386: Add API to get note's size
[qemu/ar7.git] / hw / qdev.c
blob6a8f6bda2bc3970b376b8cf88041fe96ae12f942
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 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
38 static BusState *main_system_bus;
39 static void main_system_bus_create(void);
41 /* Register a new device type. */
42 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
44 DeviceClass *dc = DEVICE_GET_CLASS(dev);
45 return dc->vmsd;
48 BusInfo *qdev_get_bus_info(DeviceState *dev)
50 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51 return dc->bus_info;
54 Property *qdev_get_props(DeviceState *dev)
56 DeviceClass *dc = DEVICE_GET_CLASS(dev);
57 return dc->props;
60 const char *qdev_fw_name(DeviceState *dev)
62 DeviceClass *dc = DEVICE_GET_CLASS(dev);
64 if (dc->fw_name) {
65 return dc->fw_name;
68 return object_get_typename(OBJECT(dev));
71 bool qdev_exists(const char *name)
73 return !!object_class_by_name(name);
76 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
77 Error **errp);
79 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
81 Property *prop;
83 if (qdev_hotplug) {
84 assert(bus->allow_hotplug);
87 dev->parent_bus = bus;
88 QTAILQ_INSERT_HEAD(&bus->children, dev, sibling);
90 for (prop = qdev_get_bus_info(dev)->props; prop && prop->name; prop++) {
91 qdev_property_add_legacy(dev, prop, NULL);
92 qdev_property_add_static(dev, prop, NULL);
94 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
97 /* Create a new device. This only initializes the device state structure
98 and allows properties to be set. qdev_init should be called to
99 initialize the actual device emulation. */
100 DeviceState *qdev_create(BusState *bus, const char *name)
102 DeviceState *dev;
104 dev = qdev_try_create(bus, name);
105 if (!dev) {
106 if (bus) {
107 hw_error("Unknown device '%s' for bus '%s'\n", name,
108 bus->info->name);
109 } else {
110 hw_error("Unknown device '%s' for default sysbus\n", name);
114 return dev;
117 DeviceState *qdev_try_create(BusState *bus, const char *type)
119 DeviceState *dev;
121 if (object_class_by_name(type) == NULL) {
122 return NULL;
124 dev = DEVICE(object_new(type));
125 if (!dev) {
126 return NULL;
129 if (!bus) {
130 bus = sysbus_get_default();
133 qdev_set_parent_bus(dev, bus);
134 qdev_prop_set_globals(dev);
136 return dev;
139 /* Initialize a device. Device properties should be set before calling
140 this function. IRQs and MMIO regions should be connected/mapped after
141 calling this function.
142 On failure, destroy the device and return negative value.
143 Return 0 on success. */
144 int qdev_init(DeviceState *dev)
146 DeviceClass *dc = DEVICE_GET_CLASS(dev);
147 int rc;
149 assert(dev->state == DEV_STATE_CREATED);
151 rc = dc->init(dev);
152 if (rc < 0) {
153 qdev_free(dev);
154 return rc;
157 if (!OBJECT(dev)->parent) {
158 static int unattached_count = 0;
159 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
161 object_property_add_child(container_get(qdev_get_machine(),
162 "/unattached"),
163 name, OBJECT(dev), NULL);
164 g_free(name);
167 if (qdev_get_vmsd(dev)) {
168 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
169 dev->instance_id_alias,
170 dev->alias_required_for_version);
172 dev->state = DEV_STATE_INITIALIZED;
173 if (dev->hotplugged) {
174 device_reset(dev);
176 return 0;
179 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
180 int required_for_version)
182 assert(dev->state == DEV_STATE_CREATED);
183 dev->instance_id_alias = alias_id;
184 dev->alias_required_for_version = required_for_version;
187 void qdev_unplug(DeviceState *dev, Error **errp)
189 DeviceClass *dc = DEVICE_GET_CLASS(dev);
191 if (!dev->parent_bus->allow_hotplug) {
192 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
193 return;
195 assert(dc->unplug != NULL);
197 qdev_hot_removed = true;
199 if (dc->unplug(dev) < 0) {
200 error_set(errp, QERR_UNDEFINED_ERROR);
201 return;
205 static int qdev_reset_one(DeviceState *dev, void *opaque)
207 device_reset(dev);
209 return 0;
212 BusState *sysbus_get_default(void)
214 if (!main_system_bus) {
215 main_system_bus_create();
217 return main_system_bus;
220 static int qbus_reset_one(BusState *bus, void *opaque)
222 if (bus->info->reset) {
223 return bus->info->reset(bus);
225 return 0;
228 void qdev_reset_all(DeviceState *dev)
230 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
233 void qbus_reset_all_fn(void *opaque)
235 BusState *bus = opaque;
236 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
239 /* can be used as ->unplug() callback for the simple cases */
240 int qdev_simple_unplug_cb(DeviceState *dev)
242 /* just zap it */
243 object_unparent(OBJECT(dev));
244 qdev_free(dev);
245 return 0;
249 /* Like qdev_init(), but terminate program via error_report() instead of
250 returning an error value. This is okay during machine creation.
251 Don't use for hotplug, because there callers need to recover from
252 failure. Exception: if you know the device's init() callback can't
253 fail, then qdev_init_nofail() can't fail either, and is therefore
254 usable even then. But relying on the device implementation that
255 way is somewhat unclean, and best avoided. */
256 void qdev_init_nofail(DeviceState *dev)
258 if (qdev_init(dev) < 0) {
259 error_report("Initialization of device %s failed",
260 object_get_typename(OBJECT(dev)));
261 exit(1);
265 /* Unlink device from bus and free the structure. */
266 void qdev_free(DeviceState *dev)
268 object_delete(OBJECT(dev));
271 void qdev_machine_creation_done(void)
274 * ok, initial machine setup is done, starting from now we can
275 * only create hotpluggable devices
277 qdev_hotplug = 1;
280 bool qdev_machine_modified(void)
282 return qdev_hot_added || qdev_hot_removed;
285 BusState *qdev_get_parent_bus(DeviceState *dev)
287 return dev->parent_bus;
290 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
292 assert(dev->num_gpio_in == 0);
293 dev->num_gpio_in = n;
294 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
297 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
299 assert(dev->num_gpio_out == 0);
300 dev->num_gpio_out = n;
301 dev->gpio_out = pins;
304 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
306 assert(n >= 0 && n < dev->num_gpio_in);
307 return dev->gpio_in[n];
310 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
312 assert(n >= 0 && n < dev->num_gpio_out);
313 dev->gpio_out[n] = pin;
316 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
318 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
319 if (nd->vlan)
320 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
321 if (nd->netdev)
322 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
323 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
324 qdev_prop_exists(dev, "vectors")) {
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 DeviceState *dev;
346 int err;
348 if (busfn) {
349 err = busfn(bus, opaque);
350 if (err) {
351 return err;
355 QTAILQ_FOREACH(dev, &bus->children, sibling) {
356 err = qdev_walk_children(dev, 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 DeviceState *dev, *ret;
391 BusState *child;
393 QTAILQ_FOREACH(dev, &bus->children, sibling) {
394 if (dev->id && strcmp(dev->id, id) == 0)
395 return dev;
396 QLIST_FOREACH(child, &dev->child_bus, sibling) {
397 ret = qdev_find_recursive(child, id);
398 if (ret) {
399 return ret;
403 return NULL;
406 void qbus_create_inplace(BusState *bus, BusInfo *info,
407 DeviceState *parent, const char *name)
409 char *buf;
410 int i,len;
412 bus->info = info;
413 bus->parent = parent;
415 if (name) {
416 /* use supplied name */
417 bus->name = g_strdup(name);
418 } else if (parent && parent->id) {
419 /* parent device has id -> use it for bus name */
420 len = strlen(parent->id) + 16;
421 buf = g_malloc(len);
422 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
423 bus->name = buf;
424 } else {
425 /* no id -> use lowercase bus type for bus name */
426 len = strlen(info->name) + 16;
427 buf = g_malloc(len);
428 len = snprintf(buf, len, "%s.%d", info->name,
429 parent ? parent->num_child_bus : 0);
430 for (i = 0; i < len; i++)
431 buf[i] = qemu_tolower(buf[i]);
432 bus->name = buf;
435 QTAILQ_INIT(&bus->children);
436 if (parent) {
437 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
438 parent->num_child_bus++;
439 } else if (bus != main_system_bus) {
440 /* TODO: once all bus devices are qdevified,
441 only reset handler for main_system_bus should be registered here. */
442 qemu_register_reset(qbus_reset_all_fn, bus);
446 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
448 BusState *bus;
450 bus = g_malloc0(info->size);
451 bus->qdev_allocated = 1;
452 qbus_create_inplace(bus, info, parent, name);
453 return bus;
456 static void main_system_bus_create(void)
458 /* assign main_system_bus before qbus_create_inplace()
459 * in order to make "if (bus != main_system_bus)" work */
460 main_system_bus = g_malloc0(system_bus_info.size);
461 main_system_bus->qdev_allocated = 1;
462 qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
463 "main-system-bus");
466 void qbus_free(BusState *bus)
468 DeviceState *dev;
470 while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) {
471 qdev_free(dev);
473 if (bus->parent) {
474 QLIST_REMOVE(bus, sibling);
475 bus->parent->num_child_bus--;
476 } else {
477 assert(bus != main_system_bus); /* main_system_bus is never freed */
478 qemu_unregister_reset(qbus_reset_all_fn, bus);
480 g_free((void*)bus->name);
481 if (bus->qdev_allocated) {
482 g_free(bus);
486 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
488 int l = 0;
490 if (dev && dev->parent_bus) {
491 char *d;
492 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
493 if (dev->parent_bus->info->get_fw_dev_path) {
494 d = dev->parent_bus->info->get_fw_dev_path(dev);
495 l += snprintf(p + l, size - l, "%s", d);
496 g_free(d);
497 } else {
498 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
501 l += snprintf(p + l , size - l, "/");
503 return l;
506 char* qdev_get_fw_dev_path(DeviceState *dev)
508 char path[128];
509 int l;
511 l = qdev_get_fw_dev_path_helper(dev, path, 128);
513 path[l-1] = '\0';
515 return strdup(path);
518 static char *qdev_get_type(Object *obj, Error **errp)
520 return g_strdup(object_get_typename(obj));
524 * Legacy property handling
527 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
528 const char *name, Error **errp)
530 DeviceState *dev = DEVICE(obj);
531 Property *prop = opaque;
533 char buffer[1024];
534 char *ptr = buffer;
536 prop->info->print(dev, prop, buffer, sizeof(buffer));
537 visit_type_str(v, &ptr, name, errp);
540 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
541 const char *name, Error **errp)
543 DeviceState *dev = DEVICE(obj);
544 Property *prop = opaque;
545 Error *local_err = NULL;
546 char *ptr = NULL;
547 int ret;
549 if (dev->state != DEV_STATE_CREATED) {
550 error_set(errp, QERR_PERMISSION_DENIED);
551 return;
554 visit_type_str(v, &ptr, name, &local_err);
555 if (local_err) {
556 error_propagate(errp, local_err);
557 return;
560 ret = prop->info->parse(dev, prop, ptr);
561 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
562 g_free(ptr);
566 * @qdev_add_legacy_property - adds a legacy property
568 * Do not use this is new code! Properties added through this interface will
569 * be given names and types in the "legacy" namespace.
571 * Legacy properties are string versions of other OOM properties. The format
572 * of the string depends on the property type.
574 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
575 Error **errp)
577 gchar *name, *type;
579 /* Register pointer properties as legacy properties */
580 if (!prop->info->print && !prop->info->parse &&
581 (prop->info->set || prop->info->get)) {
582 return;
585 name = g_strdup_printf("legacy-%s", prop->name);
586 type = g_strdup_printf("legacy<%s>",
587 prop->info->legacy_name ?: prop->info->name);
589 object_property_add(OBJECT(dev), name, type,
590 prop->info->print ? qdev_get_legacy_property : prop->info->get,
591 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
592 NULL,
593 prop, errp);
595 g_free(type);
596 g_free(name);
600 * @qdev_property_add_static - add a @Property to a device.
602 * Static properties access data in a struct. The actual type of the
603 * property and the field depends on the property type.
605 void qdev_property_add_static(DeviceState *dev, Property *prop,
606 Error **errp)
609 * TODO qdev_prop_ptr does not have getters or setters. It must
610 * go now that it can be replaced with links. The test should be
611 * removed along with it: all static properties are read/write.
613 if (!prop->info->get && !prop->info->set) {
614 return;
617 object_property_add(OBJECT(dev), prop->name, prop->info->name,
618 prop->info->get, prop->info->set,
619 prop->info->release,
620 prop, errp);
623 static void device_initfn(Object *obj)
625 DeviceState *dev = DEVICE(obj);
626 Property *prop;
628 if (qdev_hotplug) {
629 dev->hotplugged = 1;
630 qdev_hot_added = true;
633 dev->instance_id_alias = -1;
634 dev->state = DEV_STATE_CREATED;
636 for (prop = qdev_get_props(dev); prop && prop->name; prop++) {
637 qdev_property_add_legacy(dev, prop, NULL);
638 qdev_property_add_static(dev, prop, NULL);
641 object_property_add_str(OBJECT(dev), "type", qdev_get_type, NULL, NULL);
642 qdev_prop_set_defaults(dev, qdev_get_props(dev));
645 /* Unlink device from bus and free the structure. */
646 static void device_finalize(Object *obj)
648 DeviceState *dev = DEVICE(obj);
649 BusState *bus;
650 DeviceClass *dc = DEVICE_GET_CLASS(dev);
652 if (dev->state == DEV_STATE_INITIALIZED) {
653 while (dev->num_child_bus) {
654 bus = QLIST_FIRST(&dev->child_bus);
655 qbus_free(bus);
657 if (qdev_get_vmsd(dev)) {
658 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
660 if (dc->exit) {
661 dc->exit(dev);
663 if (dev->opts) {
664 qemu_opts_del(dev->opts);
667 QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling);
670 void device_reset(DeviceState *dev)
672 DeviceClass *klass = DEVICE_GET_CLASS(dev);
674 if (klass->reset) {
675 klass->reset(dev);
679 Object *qdev_get_machine(void)
681 static Object *dev;
683 if (dev == NULL) {
684 dev = container_get(object_get_root(), "/machine");
687 return dev;
690 static TypeInfo device_type_info = {
691 .name = TYPE_DEVICE,
692 .parent = TYPE_OBJECT,
693 .instance_size = sizeof(DeviceState),
694 .instance_init = device_initfn,
695 .instance_finalize = device_finalize,
696 .abstract = true,
697 .class_size = sizeof(DeviceClass),
700 static void qdev_register_types(void)
702 type_register_static(&device_type_info);
705 type_init(qdev_register_types)