hw/pc_sysfw: support system flash memory with pflash
[qemu/kevin.git] / hw / qdev.c
blobee21d90e80128aa3b39a838f9f34b8c9468b17d1
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"
32 int qdev_hotplug = 0;
33 static bool qdev_hot_added = false;
34 static bool qdev_hot_removed = false;
36 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
37 static BusState *main_system_bus;
38 static void main_system_bus_create(void);
40 /* Register a new device type. */
41 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
43 DeviceClass *dc = DEVICE_GET_CLASS(dev);
44 return dc->vmsd;
47 BusInfo *qdev_get_bus_info(DeviceState *dev)
49 DeviceClass *dc = DEVICE_GET_CLASS(dev);
50 return dc->bus_info;
53 Property *qdev_get_props(DeviceState *dev)
55 DeviceClass *dc = DEVICE_GET_CLASS(dev);
56 return dc->props;
59 const char *qdev_fw_name(DeviceState *dev)
61 DeviceClass *dc = DEVICE_GET_CLASS(dev);
63 if (dc->fw_name) {
64 return dc->fw_name;
67 return object_get_typename(OBJECT(dev));
70 bool qdev_exists(const char *name)
72 return !!object_class_by_name(name);
75 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
76 Error **errp);
78 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
80 Property *prop;
82 if (qdev_hotplug) {
83 assert(bus->allow_hotplug);
86 dev->parent_bus = bus;
87 QTAILQ_INSERT_HEAD(&bus->children, dev, sibling);
89 for (prop = qdev_get_bus_info(dev)->props; prop && prop->name; prop++) {
90 qdev_property_add_legacy(dev, prop, NULL);
91 qdev_property_add_static(dev, prop, NULL);
93 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
96 /* Create a new device. This only initializes the device state structure
97 and allows properties to be set. qdev_init should be called to
98 initialize the actual device emulation. */
99 DeviceState *qdev_create(BusState *bus, const char *name)
101 DeviceState *dev;
103 dev = qdev_try_create(bus, name);
104 if (!dev) {
105 if (bus) {
106 hw_error("Unknown device '%s' for bus '%s'\n", name,
107 bus->info->name);
108 } else {
109 hw_error("Unknown device '%s' for default sysbus\n", name);
113 return dev;
116 DeviceState *qdev_try_create(BusState *bus, const char *name)
118 DeviceState *dev;
120 if (object_class_by_name(name) == NULL) {
121 return NULL;
123 dev = DEVICE(object_new(name));
124 if (!dev) {
125 return NULL;
128 if (!bus) {
129 bus = sysbus_get_default();
132 qdev_set_parent_bus(dev, bus);
133 qdev_prop_set_globals(dev);
135 return dev;
138 /* Initialize a device. Device properties should be set before calling
139 this function. IRQs and MMIO regions should be connected/mapped after
140 calling this function.
141 On failure, destroy the device and return negative value.
142 Return 0 on success. */
143 int qdev_init(DeviceState *dev)
145 DeviceClass *dc = DEVICE_GET_CLASS(dev);
146 int rc;
148 assert(dev->state == DEV_STATE_CREATED);
150 rc = dc->init(dev);
151 if (rc < 0) {
152 qdev_free(dev);
153 return rc;
155 if (qdev_get_vmsd(dev)) {
156 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
157 dev->instance_id_alias,
158 dev->alias_required_for_version);
160 dev->state = DEV_STATE_INITIALIZED;
161 if (dev->hotplugged) {
162 device_reset(dev);
164 return 0;
167 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
168 int required_for_version)
170 assert(dev->state == DEV_STATE_CREATED);
171 dev->instance_id_alias = alias_id;
172 dev->alias_required_for_version = required_for_version;
175 int qdev_unplug(DeviceState *dev)
177 DeviceClass *dc = DEVICE_GET_CLASS(dev);
179 if (!dev->parent_bus->allow_hotplug) {
180 qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
181 return -1;
183 assert(dc->unplug != NULL);
185 qdev_hot_removed = true;
187 return dc->unplug(dev);
190 static int qdev_reset_one(DeviceState *dev, void *opaque)
192 device_reset(dev);
194 return 0;
197 BusState *sysbus_get_default(void)
199 if (!main_system_bus) {
200 main_system_bus_create();
202 return main_system_bus;
205 static int qbus_reset_one(BusState *bus, void *opaque)
207 if (bus->info->reset) {
208 return bus->info->reset(bus);
210 return 0;
213 void qdev_reset_all(DeviceState *dev)
215 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
218 void qbus_reset_all_fn(void *opaque)
220 BusState *bus = opaque;
221 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
224 /* can be used as ->unplug() callback for the simple cases */
225 int qdev_simple_unplug_cb(DeviceState *dev)
227 /* just zap it */
228 object_unparent(OBJECT(dev));
229 qdev_free(dev);
230 return 0;
234 /* Like qdev_init(), but terminate program via error_report() instead of
235 returning an error value. This is okay during machine creation.
236 Don't use for hotplug, because there callers need to recover from
237 failure. Exception: if you know the device's init() callback can't
238 fail, then qdev_init_nofail() can't fail either, and is therefore
239 usable even then. But relying on the device implementation that
240 way is somewhat unclean, and best avoided. */
241 void qdev_init_nofail(DeviceState *dev)
243 if (qdev_init(dev) < 0) {
244 error_report("Initialization of device %s failed",
245 object_get_typename(OBJECT(dev)));
246 exit(1);
250 /* Unlink device from bus and free the structure. */
251 void qdev_free(DeviceState *dev)
253 object_delete(OBJECT(dev));
256 void qdev_machine_creation_done(void)
259 * ok, initial machine setup is done, starting from now we can
260 * only create hotpluggable devices
262 qdev_hotplug = 1;
265 bool qdev_machine_modified(void)
267 return qdev_hot_added || qdev_hot_removed;
270 BusState *qdev_get_parent_bus(DeviceState *dev)
272 return dev->parent_bus;
275 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
277 assert(dev->num_gpio_in == 0);
278 dev->num_gpio_in = n;
279 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
282 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
284 assert(dev->num_gpio_out == 0);
285 dev->num_gpio_out = n;
286 dev->gpio_out = pins;
289 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
291 assert(n >= 0 && n < dev->num_gpio_in);
292 return dev->gpio_in[n];
295 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
297 assert(n >= 0 && n < dev->num_gpio_out);
298 dev->gpio_out[n] = pin;
301 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
303 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
304 if (nd->vlan)
305 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
306 if (nd->netdev)
307 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
308 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
309 qdev_prop_exists(dev, "vectors")) {
310 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
312 nd->instantiated = 1;
315 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
317 BusState *bus;
319 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
320 if (strcmp(name, bus->name) == 0) {
321 return bus;
324 return NULL;
327 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
328 qbus_walkerfn *busfn, void *opaque)
330 DeviceState *dev;
331 int err;
333 if (busfn) {
334 err = busfn(bus, opaque);
335 if (err) {
336 return err;
340 QTAILQ_FOREACH(dev, &bus->children, sibling) {
341 err = qdev_walk_children(dev, devfn, busfn, opaque);
342 if (err < 0) {
343 return err;
347 return 0;
350 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
351 qbus_walkerfn *busfn, void *opaque)
353 BusState *bus;
354 int err;
356 if (devfn) {
357 err = devfn(dev, opaque);
358 if (err) {
359 return err;
363 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
364 err = qbus_walk_children(bus, devfn, busfn, opaque);
365 if (err < 0) {
366 return err;
370 return 0;
373 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
375 DeviceState *dev, *ret;
376 BusState *child;
378 QTAILQ_FOREACH(dev, &bus->children, sibling) {
379 if (dev->id && strcmp(dev->id, id) == 0)
380 return dev;
381 QLIST_FOREACH(child, &dev->child_bus, sibling) {
382 ret = qdev_find_recursive(child, id);
383 if (ret) {
384 return ret;
388 return NULL;
391 void qbus_create_inplace(BusState *bus, BusInfo *info,
392 DeviceState *parent, const char *name)
394 char *buf;
395 int i,len;
397 bus->info = info;
398 bus->parent = parent;
400 if (name) {
401 /* use supplied name */
402 bus->name = g_strdup(name);
403 } else if (parent && parent->id) {
404 /* parent device has id -> use it for bus name */
405 len = strlen(parent->id) + 16;
406 buf = g_malloc(len);
407 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
408 bus->name = buf;
409 } else {
410 /* no id -> use lowercase bus type for bus name */
411 len = strlen(info->name) + 16;
412 buf = g_malloc(len);
413 len = snprintf(buf, len, "%s.%d", info->name,
414 parent ? parent->num_child_bus : 0);
415 for (i = 0; i < len; i++)
416 buf[i] = qemu_tolower(buf[i]);
417 bus->name = buf;
420 QTAILQ_INIT(&bus->children);
421 if (parent) {
422 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
423 parent->num_child_bus++;
424 } else if (bus != main_system_bus) {
425 /* TODO: once all bus devices are qdevified,
426 only reset handler for main_system_bus should be registered here. */
427 qemu_register_reset(qbus_reset_all_fn, bus);
431 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
433 BusState *bus;
435 bus = g_malloc0(info->size);
436 bus->qdev_allocated = 1;
437 qbus_create_inplace(bus, info, parent, name);
438 return bus;
441 static void main_system_bus_create(void)
443 /* assign main_system_bus before qbus_create_inplace()
444 * in order to make "if (bus != main_system_bus)" work */
445 main_system_bus = g_malloc0(system_bus_info.size);
446 main_system_bus->qdev_allocated = 1;
447 qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
448 "main-system-bus");
451 void qbus_free(BusState *bus)
453 DeviceState *dev;
455 while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) {
456 qdev_free(dev);
458 if (bus->parent) {
459 QLIST_REMOVE(bus, sibling);
460 bus->parent->num_child_bus--;
461 } else {
462 assert(bus != main_system_bus); /* main_system_bus is never freed */
463 qemu_unregister_reset(qbus_reset_all_fn, bus);
465 g_free((void*)bus->name);
466 if (bus->qdev_allocated) {
467 g_free(bus);
471 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
473 int l = 0;
475 if (dev && dev->parent_bus) {
476 char *d;
477 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
478 if (dev->parent_bus->info->get_fw_dev_path) {
479 d = dev->parent_bus->info->get_fw_dev_path(dev);
480 l += snprintf(p + l, size - l, "%s", d);
481 g_free(d);
482 } else {
483 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
486 l += snprintf(p + l , size - l, "/");
488 return l;
491 char* qdev_get_fw_dev_path(DeviceState *dev)
493 char path[128];
494 int l;
496 l = qdev_get_fw_dev_path_helper(dev, path, 128);
498 path[l-1] = '\0';
500 return strdup(path);
503 static char *qdev_get_type(Object *obj, Error **errp)
505 return g_strdup(object_get_typename(obj));
509 * Legacy property handling
512 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
513 const char *name, Error **errp)
515 DeviceState *dev = DEVICE(obj);
516 Property *prop = opaque;
518 char buffer[1024];
519 char *ptr = buffer;
521 prop->info->print(dev, prop, buffer, sizeof(buffer));
522 visit_type_str(v, &ptr, name, errp);
525 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
526 const char *name, Error **errp)
528 DeviceState *dev = DEVICE(obj);
529 Property *prop = opaque;
530 Error *local_err = NULL;
531 char *ptr = NULL;
532 int ret;
534 if (dev->state != DEV_STATE_CREATED) {
535 error_set(errp, QERR_PERMISSION_DENIED);
536 return;
539 visit_type_str(v, &ptr, name, &local_err);
540 if (local_err) {
541 error_propagate(errp, local_err);
542 return;
545 ret = prop->info->parse(dev, prop, ptr);
546 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
547 g_free(ptr);
551 * @qdev_add_legacy_property - adds a legacy property
553 * Do not use this is new code! Properties added through this interface will
554 * be given names and types in the "legacy" namespace.
556 * Legacy properties are string versions of other OOM properties. The format
557 * of the string depends on the property type.
559 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
560 Error **errp)
562 gchar *name, *type;
564 if (!prop->info->print && !prop->info->parse) {
565 return;
567 name = g_strdup_printf("legacy-%s", prop->name);
568 type = g_strdup_printf("legacy<%s>",
569 prop->info->legacy_name ?: prop->info->name);
571 object_property_add(OBJECT(dev), name, type,
572 prop->info->print ? qdev_get_legacy_property : prop->info->get,
573 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
574 NULL,
575 prop, errp);
577 g_free(type);
578 g_free(name);
582 * @qdev_property_add_static - add a @Property to a device.
584 * Static properties access data in a struct. The actual type of the
585 * property and the field depends on the property type.
587 void qdev_property_add_static(DeviceState *dev, Property *prop,
588 Error **errp)
591 * TODO qdev_prop_ptr does not have getters or setters. It must
592 * go now that it can be replaced with links. The test should be
593 * removed along with it: all static properties are read/write.
595 if (!prop->info->get && !prop->info->set) {
596 return;
599 object_property_add(OBJECT(dev), prop->name, prop->info->name,
600 prop->info->get, prop->info->set,
601 prop->info->release,
602 prop, errp);
605 static void device_initfn(Object *obj)
607 DeviceState *dev = DEVICE(obj);
608 Property *prop;
610 if (qdev_hotplug) {
611 dev->hotplugged = 1;
612 qdev_hot_added = true;
615 dev->instance_id_alias = -1;
616 dev->state = DEV_STATE_CREATED;
618 for (prop = qdev_get_props(dev); prop && prop->name; prop++) {
619 qdev_property_add_legacy(dev, prop, NULL);
620 qdev_property_add_static(dev, prop, NULL);
623 object_property_add_str(OBJECT(dev), "type", qdev_get_type, NULL, NULL);
624 qdev_prop_set_defaults(dev, qdev_get_props(dev));
627 /* Unlink device from bus and free the structure. */
628 static void device_finalize(Object *obj)
630 DeviceState *dev = DEVICE(obj);
631 BusState *bus;
632 DeviceClass *dc = DEVICE_GET_CLASS(dev);
634 if (dev->state == DEV_STATE_INITIALIZED) {
635 while (dev->num_child_bus) {
636 bus = QLIST_FIRST(&dev->child_bus);
637 qbus_free(bus);
639 if (qdev_get_vmsd(dev)) {
640 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
642 if (dc->exit) {
643 dc->exit(dev);
645 if (dev->opts) {
646 qemu_opts_del(dev->opts);
649 QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling);
652 void device_reset(DeviceState *dev)
654 DeviceClass *klass = DEVICE_GET_CLASS(dev);
656 if (klass->reset) {
657 klass->reset(dev);
661 static TypeInfo device_type_info = {
662 .name = TYPE_DEVICE,
663 .parent = TYPE_OBJECT,
664 .instance_size = sizeof(DeviceState),
665 .instance_init = device_initfn,
666 .instance_finalize = device_finalize,
667 .abstract = true,
668 .class_size = sizeof(DeviceClass),
671 static void qdev_register_types(void)
673 type_register_static(&device_type_info);
676 type_init(qdev_register_types)