]> git.proxmox.com Git - mirror_qemu.git/blob - hw/core/qdev.c
Drop superfluous includes of qapi/qmp/qjson.h
[mirror_qemu.git] / hw / core / qdev.c
1 /*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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. */
27
28 #include "qemu/osdep.h"
29 #include "hw/qdev.h"
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qemu/error-report.h"
35 #include "hw/hotplug.h"
36 #include "hw/boards.h"
37 #include "hw/sysbus.h"
38 #include "qapi-event.h"
39
40 bool qdev_hotplug = false;
41 static bool qdev_hot_added = false;
42 bool qdev_hot_removed = false;
43
44 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
45 {
46 DeviceClass *dc = DEVICE_GET_CLASS(dev);
47 return dc->vmsd;
48 }
49
50 static void bus_remove_child(BusState *bus, DeviceState *child)
51 {
52 BusChild *kid;
53
54 QTAILQ_FOREACH(kid, &bus->children, sibling) {
55 if (kid->child == child) {
56 char name[32];
57
58 snprintf(name, sizeof(name), "child[%d]", kid->index);
59 QTAILQ_REMOVE(&bus->children, kid, sibling);
60
61 /* This gives back ownership of kid->child back to us. */
62 object_property_del(OBJECT(bus), name, NULL);
63 object_unref(OBJECT(kid->child));
64 g_free(kid);
65 return;
66 }
67 }
68 }
69
70 static void bus_add_child(BusState *bus, DeviceState *child)
71 {
72 char name[32];
73 BusChild *kid = g_malloc0(sizeof(*kid));
74
75 kid->index = bus->max_index++;
76 kid->child = child;
77 object_ref(OBJECT(kid->child));
78
79 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
80
81 /* This transfers ownership of kid->child to the property. */
82 snprintf(name, sizeof(name), "child[%d]", kid->index);
83 object_property_add_link(OBJECT(bus), name,
84 object_get_typename(OBJECT(child)),
85 (Object **)&kid->child,
86 NULL, /* read-only property */
87 0, /* return ownership on prop deletion */
88 NULL);
89 }
90
91 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
92 {
93 bool replugging = dev->parent_bus != NULL;
94
95 if (replugging) {
96 /* Keep a reference to the device while it's not plugged into
97 * any bus, to avoid it potentially evaporating when it is
98 * dereffed in bus_remove_child().
99 */
100 object_ref(OBJECT(dev));
101 bus_remove_child(dev->parent_bus, dev);
102 object_unref(OBJECT(dev->parent_bus));
103 }
104 dev->parent_bus = bus;
105 object_ref(OBJECT(bus));
106 bus_add_child(bus, dev);
107 if (replugging) {
108 object_unref(OBJECT(dev));
109 }
110 }
111
112 /* Create a new device. This only initializes the device state
113 structure and allows properties to be set. The device still needs
114 to be realized. See qdev-core.h. */
115 DeviceState *qdev_create(BusState *bus, const char *name)
116 {
117 DeviceState *dev;
118
119 dev = qdev_try_create(bus, name);
120 if (!dev) {
121 if (bus) {
122 error_report("Unknown device '%s' for bus '%s'", name,
123 object_get_typename(OBJECT(bus)));
124 } else {
125 error_report("Unknown device '%s' for default sysbus", name);
126 }
127 abort();
128 }
129
130 return dev;
131 }
132
133 DeviceState *qdev_try_create(BusState *bus, const char *type)
134 {
135 DeviceState *dev;
136
137 if (object_class_by_name(type) == NULL) {
138 return NULL;
139 }
140 dev = DEVICE(object_new(type));
141 if (!dev) {
142 return NULL;
143 }
144
145 if (!bus) {
146 /* Assert that the device really is a SysBusDevice before
147 * we put it onto the sysbus. Non-sysbus devices which aren't
148 * being put onto a bus should be created with object_new(TYPE_FOO),
149 * not qdev_create(NULL, TYPE_FOO).
150 */
151 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
152 bus = sysbus_get_default();
153 }
154
155 qdev_set_parent_bus(dev, bus);
156 object_unref(OBJECT(dev));
157 return dev;
158 }
159
160 static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
161 = QTAILQ_HEAD_INITIALIZER(device_listeners);
162
163 enum ListenerDirection { Forward, Reverse };
164
165 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
166 do { \
167 DeviceListener *_listener; \
168 \
169 switch (_direction) { \
170 case Forward: \
171 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
172 if (_listener->_callback) { \
173 _listener->_callback(_listener, ##_args); \
174 } \
175 } \
176 break; \
177 case Reverse: \
178 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
179 device_listeners, link) { \
180 if (_listener->_callback) { \
181 _listener->_callback(_listener, ##_args); \
182 } \
183 } \
184 break; \
185 default: \
186 abort(); \
187 } \
188 } while (0)
189
190 static int device_listener_add(DeviceState *dev, void *opaque)
191 {
192 DEVICE_LISTENER_CALL(realize, Forward, dev);
193
194 return 0;
195 }
196
197 void device_listener_register(DeviceListener *listener)
198 {
199 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
200
201 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
202 NULL, NULL);
203 }
204
205 void device_listener_unregister(DeviceListener *listener)
206 {
207 QTAILQ_REMOVE(&device_listeners, listener, link);
208 }
209
210 static void device_realize(DeviceState *dev, Error **errp)
211 {
212 DeviceClass *dc = DEVICE_GET_CLASS(dev);
213
214 if (dc->init) {
215 int rc = dc->init(dev);
216 if (rc < 0) {
217 error_setg(errp, "Device initialization failed.");
218 return;
219 }
220 }
221 }
222
223 static void device_unrealize(DeviceState *dev, Error **errp)
224 {
225 DeviceClass *dc = DEVICE_GET_CLASS(dev);
226
227 if (dc->exit) {
228 int rc = dc->exit(dev);
229 if (rc < 0) {
230 error_setg(errp, "Device exit failed.");
231 return;
232 }
233 }
234 }
235
236 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
237 int required_for_version)
238 {
239 assert(!dev->realized);
240 dev->instance_id_alias = alias_id;
241 dev->alias_required_for_version = required_for_version;
242 }
243
244 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
245 {
246 MachineState *machine;
247 MachineClass *mc;
248 Object *m_obj = qdev_get_machine();
249
250 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
251 machine = MACHINE(m_obj);
252 mc = MACHINE_GET_CLASS(machine);
253 if (mc->get_hotplug_handler) {
254 return mc->get_hotplug_handler(machine, dev);
255 }
256 }
257
258 return NULL;
259 }
260
261 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
262 {
263 HotplugHandler *hotplug_ctrl;
264
265 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
266 hotplug_ctrl = dev->parent_bus->hotplug_handler;
267 } else {
268 hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
269 }
270 return hotplug_ctrl;
271 }
272
273 static int qdev_reset_one(DeviceState *dev, void *opaque)
274 {
275 device_reset(dev);
276
277 return 0;
278 }
279
280 static int qbus_reset_one(BusState *bus, void *opaque)
281 {
282 BusClass *bc = BUS_GET_CLASS(bus);
283 if (bc->reset) {
284 bc->reset(bus);
285 }
286 return 0;
287 }
288
289 void qdev_reset_all(DeviceState *dev)
290 {
291 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
292 }
293
294 void qdev_reset_all_fn(void *opaque)
295 {
296 qdev_reset_all(DEVICE(opaque));
297 }
298
299 void qbus_reset_all(BusState *bus)
300 {
301 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
302 }
303
304 void qbus_reset_all_fn(void *opaque)
305 {
306 BusState *bus = opaque;
307 qbus_reset_all(bus);
308 }
309
310 /* can be used as ->unplug() callback for the simple cases */
311 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
312 DeviceState *dev, Error **errp)
313 {
314 /* just zap it */
315 object_unparent(OBJECT(dev));
316 }
317
318 /*
319 * Realize @dev.
320 * Device properties should be set before calling this function. IRQs
321 * and MMIO regions should be connected/mapped after calling this
322 * function.
323 * On failure, report an error with error_report() and terminate the
324 * program. This is okay during machine creation. Don't use for
325 * hotplug, because there callers need to recover from failure.
326 * Exception: if you know the device's init() callback can't fail,
327 * then qdev_init_nofail() can't fail either, and is therefore usable
328 * even then. But relying on the device implementation that way is
329 * somewhat unclean, and best avoided.
330 */
331 void qdev_init_nofail(DeviceState *dev)
332 {
333 Error *err = NULL;
334
335 assert(!dev->realized);
336
337 object_ref(OBJECT(dev));
338 object_property_set_bool(OBJECT(dev), true, "realized", &err);
339 if (err) {
340 error_reportf_err(err, "Initialization of device %s failed: ",
341 object_get_typename(OBJECT(dev)));
342 exit(1);
343 }
344 object_unref(OBJECT(dev));
345 }
346
347 void qdev_machine_creation_done(void)
348 {
349 /*
350 * ok, initial machine setup is done, starting from now we can
351 * only create hotpluggable devices
352 */
353 qdev_hotplug = true;
354 }
355
356 bool qdev_machine_modified(void)
357 {
358 return qdev_hot_added || qdev_hot_removed;
359 }
360
361 BusState *qdev_get_parent_bus(DeviceState *dev)
362 {
363 return dev->parent_bus;
364 }
365
366 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
367 const char *name)
368 {
369 NamedGPIOList *ngl;
370
371 QLIST_FOREACH(ngl, &dev->gpios, node) {
372 /* NULL is a valid and matchable name, otherwise do a normal
373 * strcmp match.
374 */
375 if ((!ngl->name && !name) ||
376 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
377 return ngl;
378 }
379 }
380
381 ngl = g_malloc0(sizeof(*ngl));
382 ngl->name = g_strdup(name);
383 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
384 return ngl;
385 }
386
387 void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
388 const char *name, int n)
389 {
390 int i;
391 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
392
393 assert(gpio_list->num_out == 0 || !name);
394 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
395 dev, n);
396
397 if (!name) {
398 name = "unnamed-gpio-in";
399 }
400 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
401 gchar *propname = g_strdup_printf("%s[%u]", name, i);
402
403 object_property_add_child(OBJECT(dev), propname,
404 OBJECT(gpio_list->in[i]), &error_abort);
405 g_free(propname);
406 }
407
408 gpio_list->num_in += n;
409 }
410
411 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
412 {
413 qdev_init_gpio_in_named(dev, handler, NULL, n);
414 }
415
416 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
417 const char *name, int n)
418 {
419 int i;
420 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
421
422 assert(gpio_list->num_in == 0 || !name);
423
424 if (!name) {
425 name = "unnamed-gpio-out";
426 }
427 memset(pins, 0, sizeof(*pins) * n);
428 for (i = 0; i < n; ++i) {
429 gchar *propname = g_strdup_printf("%s[%u]", name,
430 gpio_list->num_out + i);
431
432 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
433 (Object **)&pins[i],
434 object_property_allow_set_link,
435 OBJ_PROP_LINK_UNREF_ON_RELEASE,
436 &error_abort);
437 g_free(propname);
438 }
439 gpio_list->num_out += n;
440 }
441
442 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
443 {
444 qdev_init_gpio_out_named(dev, pins, NULL, n);
445 }
446
447 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
448 {
449 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
450
451 assert(n >= 0 && n < gpio_list->num_in);
452 return gpio_list->in[n];
453 }
454
455 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
456 {
457 return qdev_get_gpio_in_named(dev, NULL, n);
458 }
459
460 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
461 qemu_irq pin)
462 {
463 char *propname = g_strdup_printf("%s[%d]",
464 name ? name : "unnamed-gpio-out", n);
465 if (pin) {
466 /* We need a name for object_property_set_link to work. If the
467 * object has a parent, object_property_add_child will come back
468 * with an error without doing anything. If it has none, it will
469 * never fail. So we can just call it with a NULL Error pointer.
470 */
471 object_property_add_child(container_get(qdev_get_machine(),
472 "/unattached"),
473 "non-qdev-gpio[*]", OBJECT(pin), NULL);
474 }
475 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
476 g_free(propname);
477 }
478
479 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
480 {
481 char *propname = g_strdup_printf("%s[%d]",
482 name ? name : "unnamed-gpio-out", n);
483
484 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
485 NULL);
486
487 return ret;
488 }
489
490 /* disconnect a GPIO output, returning the disconnected input (if any) */
491
492 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
493 const char *name, int n)
494 {
495 char *propname = g_strdup_printf("%s[%d]",
496 name ? name : "unnamed-gpio-out", n);
497
498 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
499 NULL);
500 if (ret) {
501 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
502 }
503 g_free(propname);
504 return ret;
505 }
506
507 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
508 const char *name, int n)
509 {
510 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
511 qdev_connect_gpio_out_named(dev, name, n, icpt);
512 return disconnected;
513 }
514
515 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
516 {
517 qdev_connect_gpio_out_named(dev, NULL, n, pin);
518 }
519
520 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
521 const char *name)
522 {
523 int i;
524 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
525
526 for (i = 0; i < ngl->num_in; i++) {
527 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
528 char *propname = g_strdup_printf("%s[%d]", nm, i);
529
530 object_property_add_alias(OBJECT(container), propname,
531 OBJECT(dev), propname,
532 &error_abort);
533 g_free(propname);
534 }
535 for (i = 0; i < ngl->num_out; i++) {
536 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
537 char *propname = g_strdup_printf("%s[%d]", nm, i);
538
539 object_property_add_alias(OBJECT(container), propname,
540 OBJECT(dev), propname,
541 &error_abort);
542 g_free(propname);
543 }
544 QLIST_REMOVE(ngl, node);
545 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
546 }
547
548 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
549 {
550 BusState *bus;
551 Object *child = object_resolve_path_component(OBJECT(dev), name);
552
553 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
554 if (bus) {
555 return bus;
556 }
557
558 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
559 if (strcmp(name, bus->name) == 0) {
560 return bus;
561 }
562 }
563 return NULL;
564 }
565
566 int qdev_walk_children(DeviceState *dev,
567 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
568 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
569 void *opaque)
570 {
571 BusState *bus;
572 int err;
573
574 if (pre_devfn) {
575 err = pre_devfn(dev, opaque);
576 if (err) {
577 return err;
578 }
579 }
580
581 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
582 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
583 post_devfn, post_busfn, opaque);
584 if (err < 0) {
585 return err;
586 }
587 }
588
589 if (post_devfn) {
590 err = post_devfn(dev, opaque);
591 if (err) {
592 return err;
593 }
594 }
595
596 return 0;
597 }
598
599 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
600 {
601 BusChild *kid;
602 DeviceState *ret;
603 BusState *child;
604
605 QTAILQ_FOREACH(kid, &bus->children, sibling) {
606 DeviceState *dev = kid->child;
607
608 if (dev->id && strcmp(dev->id, id) == 0) {
609 return dev;
610 }
611
612 QLIST_FOREACH(child, &dev->child_bus, sibling) {
613 ret = qdev_find_recursive(child, id);
614 if (ret) {
615 return ret;
616 }
617 }
618 }
619 return NULL;
620 }
621
622 char *qdev_get_dev_path(DeviceState *dev)
623 {
624 BusClass *bc;
625
626 if (!dev || !dev->parent_bus) {
627 return NULL;
628 }
629
630 bc = BUS_GET_CLASS(dev->parent_bus);
631 if (bc->get_dev_path) {
632 return bc->get_dev_path(dev);
633 }
634
635 return NULL;
636 }
637
638 /**
639 * Legacy property handling
640 */
641
642 static void qdev_get_legacy_property(Object *obj, Visitor *v,
643 const char *name, void *opaque,
644 Error **errp)
645 {
646 DeviceState *dev = DEVICE(obj);
647 Property *prop = opaque;
648
649 char buffer[1024];
650 char *ptr = buffer;
651
652 prop->info->print(dev, prop, buffer, sizeof(buffer));
653 visit_type_str(v, name, &ptr, errp);
654 }
655
656 /**
657 * qdev_property_add_legacy:
658 * @dev: Device to add the property to.
659 * @prop: The qdev property definition.
660 * @errp: location to store error information.
661 *
662 * Add a legacy QOM property to @dev for qdev property @prop.
663 * On error, store error in @errp.
664 *
665 * Legacy properties are string versions of QOM properties. The format of
666 * the string depends on the property type. Legacy properties are only
667 * needed for "info qtree".
668 *
669 * Do not use this is new code! QOM Properties added through this interface
670 * will be given names in the "legacy" namespace.
671 */
672 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
673 Error **errp)
674 {
675 gchar *name;
676
677 /* Register pointer properties as legacy properties */
678 if (!prop->info->print && prop->info->get) {
679 return;
680 }
681
682 if (prop->info->create) {
683 return;
684 }
685
686 name = g_strdup_printf("legacy-%s", prop->name);
687 object_property_add(OBJECT(dev), name, "str",
688 prop->info->print ? qdev_get_legacy_property : prop->info->get,
689 NULL,
690 NULL,
691 prop, errp);
692
693 g_free(name);
694 }
695
696 /**
697 * qdev_property_add_static:
698 * @dev: Device to add the property to.
699 * @prop: The qdev property definition.
700 * @errp: location to store error information.
701 *
702 * Add a static QOM property to @dev for qdev property @prop.
703 * On error, store error in @errp. Static properties access data in a struct.
704 * The type of the QOM property is derived from prop->info.
705 */
706 void qdev_property_add_static(DeviceState *dev, Property *prop,
707 Error **errp)
708 {
709 Error *local_err = NULL;
710 Object *obj = OBJECT(dev);
711
712 if (prop->info->create) {
713 prop->info->create(obj, prop, &local_err);
714 } else {
715 /*
716 * TODO qdev_prop_ptr does not have getters or setters. It must
717 * go now that it can be replaced with links. The test should be
718 * removed along with it: all static properties are read/write.
719 */
720 if (!prop->info->get && !prop->info->set) {
721 return;
722 }
723 object_property_add(obj, prop->name, prop->info->name,
724 prop->info->get, prop->info->set,
725 prop->info->release,
726 prop, &local_err);
727 }
728
729 if (local_err) {
730 error_propagate(errp, local_err);
731 return;
732 }
733
734 object_property_set_description(obj, prop->name,
735 prop->info->description,
736 &error_abort);
737
738 if (prop->set_default) {
739 prop->info->set_default_value(obj, prop);
740 }
741 }
742
743 /* @qdev_alias_all_properties - Add alias properties to the source object for
744 * all qdev properties on the target DeviceState.
745 */
746 void qdev_alias_all_properties(DeviceState *target, Object *source)
747 {
748 ObjectClass *class;
749 Property *prop;
750
751 class = object_get_class(OBJECT(target));
752 do {
753 DeviceClass *dc = DEVICE_CLASS(class);
754
755 for (prop = dc->props; prop && prop->name; prop++) {
756 object_property_add_alias(source, prop->name,
757 OBJECT(target), prop->name,
758 &error_abort);
759 }
760 class = object_class_get_parent(class);
761 } while (class != object_class_by_name(TYPE_DEVICE));
762 }
763
764 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
765 {
766 GSList **list = opaque;
767 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
768 TYPE_DEVICE);
769
770 if (dev == NULL) {
771 return 0;
772 }
773
774 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
775 *list = g_slist_append(*list, dev);
776 }
777
778 return 0;
779 }
780
781 GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
782 {
783 GSList *list = NULL;
784
785 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
786
787 return list;
788 }
789
790 static bool device_get_realized(Object *obj, Error **errp)
791 {
792 DeviceState *dev = DEVICE(obj);
793 return dev->realized;
794 }
795
796 static bool check_only_migratable(Object *obj, Error **err)
797 {
798 DeviceClass *dc = DEVICE_GET_CLASS(obj);
799
800 if (!vmstate_check_only_migratable(dc->vmsd)) {
801 error_setg(err, "Device %s is not migratable, but "
802 "--only-migratable was specified",
803 object_get_typename(obj));
804 return false;
805 }
806
807 return true;
808 }
809
810 static void device_set_realized(Object *obj, bool value, Error **errp)
811 {
812 DeviceState *dev = DEVICE(obj);
813 DeviceClass *dc = DEVICE_GET_CLASS(dev);
814 HotplugHandler *hotplug_ctrl;
815 BusState *bus;
816 Error *local_err = NULL;
817 bool unattached_parent = false;
818 static int unattached_count;
819
820 if (dev->hotplugged && !dc->hotpluggable) {
821 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
822 return;
823 }
824
825 if (value && !dev->realized) {
826 if (!check_only_migratable(obj, &local_err)) {
827 goto fail;
828 }
829
830 if (!obj->parent) {
831 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
832
833 object_property_add_child(container_get(qdev_get_machine(),
834 "/unattached"),
835 name, obj, &error_abort);
836 unattached_parent = true;
837 g_free(name);
838 }
839
840 hotplug_ctrl = qdev_get_hotplug_handler(dev);
841 if (hotplug_ctrl) {
842 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
843 if (local_err != NULL) {
844 goto fail;
845 }
846 }
847
848 if (dc->realize) {
849 dc->realize(dev, &local_err);
850 }
851
852 if (local_err != NULL) {
853 goto fail;
854 }
855
856 DEVICE_LISTENER_CALL(realize, Forward, dev);
857
858 if (hotplug_ctrl) {
859 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
860 }
861
862 if (local_err != NULL) {
863 goto post_realize_fail;
864 }
865
866 /*
867 * always free/re-initialize here since the value cannot be cleaned up
868 * in device_unrealize due to its usage later on in the unplug path
869 */
870 g_free(dev->canonical_path);
871 dev->canonical_path = object_get_canonical_path(OBJECT(dev));
872
873 if (qdev_get_vmsd(dev)) {
874 if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
875 dev->instance_id_alias,
876 dev->alias_required_for_version,
877 &local_err) < 0) {
878 goto post_realize_fail;
879 }
880 }
881
882 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
883 object_property_set_bool(OBJECT(bus), true, "realized",
884 &local_err);
885 if (local_err != NULL) {
886 goto child_realize_fail;
887 }
888 }
889 if (dev->hotplugged) {
890 device_reset(dev);
891 }
892 dev->pending_deleted_event = false;
893 } else if (!value && dev->realized) {
894 Error **local_errp = NULL;
895 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
896 local_errp = local_err ? NULL : &local_err;
897 object_property_set_bool(OBJECT(bus), false, "realized",
898 local_errp);
899 }
900 if (qdev_get_vmsd(dev)) {
901 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
902 }
903 if (dc->unrealize) {
904 local_errp = local_err ? NULL : &local_err;
905 dc->unrealize(dev, local_errp);
906 }
907 dev->pending_deleted_event = true;
908 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
909 }
910
911 if (local_err != NULL) {
912 goto fail;
913 }
914
915 dev->realized = value;
916 return;
917
918 child_realize_fail:
919 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
920 object_property_set_bool(OBJECT(bus), false, "realized",
921 NULL);
922 }
923
924 if (qdev_get_vmsd(dev)) {
925 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
926 }
927
928 post_realize_fail:
929 g_free(dev->canonical_path);
930 dev->canonical_path = NULL;
931 if (dc->unrealize) {
932 dc->unrealize(dev, NULL);
933 }
934
935 fail:
936 error_propagate(errp, local_err);
937 if (unattached_parent) {
938 object_unparent(OBJECT(dev));
939 unattached_count--;
940 }
941 }
942
943 static bool device_get_hotpluggable(Object *obj, Error **errp)
944 {
945 DeviceClass *dc = DEVICE_GET_CLASS(obj);
946 DeviceState *dev = DEVICE(obj);
947
948 return dc->hotpluggable && (dev->parent_bus == NULL ||
949 qbus_is_hotpluggable(dev->parent_bus));
950 }
951
952 static bool device_get_hotplugged(Object *obj, Error **err)
953 {
954 DeviceState *dev = DEVICE(obj);
955
956 return dev->hotplugged;
957 }
958
959 static void device_initfn(Object *obj)
960 {
961 DeviceState *dev = DEVICE(obj);
962 ObjectClass *class;
963 Property *prop;
964
965 if (qdev_hotplug) {
966 dev->hotplugged = 1;
967 qdev_hot_added = true;
968 }
969
970 dev->instance_id_alias = -1;
971 dev->realized = false;
972
973 object_property_add_bool(obj, "realized",
974 device_get_realized, device_set_realized, NULL);
975 object_property_add_bool(obj, "hotpluggable",
976 device_get_hotpluggable, NULL, NULL);
977 object_property_add_bool(obj, "hotplugged",
978 device_get_hotplugged, NULL,
979 &error_abort);
980
981 class = object_get_class(OBJECT(dev));
982 do {
983 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
984 qdev_property_add_legacy(dev, prop, &error_abort);
985 qdev_property_add_static(dev, prop, &error_abort);
986 }
987 class = object_class_get_parent(class);
988 } while (class != object_class_by_name(TYPE_DEVICE));
989
990 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
991 (Object **)&dev->parent_bus, NULL, 0,
992 &error_abort);
993 QLIST_INIT(&dev->gpios);
994 }
995
996 static void device_post_init(Object *obj)
997 {
998 qdev_prop_set_globals(DEVICE(obj));
999 }
1000
1001 /* Unlink device from bus and free the structure. */
1002 static void device_finalize(Object *obj)
1003 {
1004 NamedGPIOList *ngl, *next;
1005
1006 DeviceState *dev = DEVICE(obj);
1007
1008 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1009 QLIST_REMOVE(ngl, node);
1010 qemu_free_irqs(ngl->in, ngl->num_in);
1011 g_free(ngl->name);
1012 g_free(ngl);
1013 /* ngl->out irqs are owned by the other end and should not be freed
1014 * here
1015 */
1016 }
1017
1018 /* Only send event if the device had been completely realized */
1019 if (dev->pending_deleted_event) {
1020 g_assert(dev->canonical_path);
1021
1022 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path,
1023 &error_abort);
1024 g_free(dev->canonical_path);
1025 dev->canonical_path = NULL;
1026 }
1027
1028 qemu_opts_del(dev->opts);
1029 }
1030
1031 static void device_class_base_init(ObjectClass *class, void *data)
1032 {
1033 DeviceClass *klass = DEVICE_CLASS(class);
1034
1035 /* We explicitly look up properties in the superclasses,
1036 * so do not propagate them to the subclasses.
1037 */
1038 klass->props = NULL;
1039 }
1040
1041 static void device_unparent(Object *obj)
1042 {
1043 DeviceState *dev = DEVICE(obj);
1044 BusState *bus;
1045
1046 if (dev->realized) {
1047 object_property_set_bool(obj, false, "realized", NULL);
1048 }
1049 while (dev->num_child_bus) {
1050 bus = QLIST_FIRST(&dev->child_bus);
1051 object_unparent(OBJECT(bus));
1052 }
1053 if (dev->parent_bus) {
1054 bus_remove_child(dev->parent_bus, dev);
1055 object_unref(OBJECT(dev->parent_bus));
1056 dev->parent_bus = NULL;
1057 }
1058 }
1059
1060 static void device_class_init(ObjectClass *class, void *data)
1061 {
1062 DeviceClass *dc = DEVICE_CLASS(class);
1063
1064 class->unparent = device_unparent;
1065 dc->realize = device_realize;
1066 dc->unrealize = device_unrealize;
1067
1068 /* by default all devices were considered as hotpluggable,
1069 * so with intent to check it in generic qdev_unplug() /
1070 * device_set_realized() functions make every device
1071 * hotpluggable. Devices that shouldn't be hotpluggable,
1072 * should override it in their class_init()
1073 */
1074 dc->hotpluggable = true;
1075 dc->user_creatable = true;
1076 }
1077
1078 void device_class_set_parent_reset(DeviceClass *dc,
1079 DeviceReset dev_reset,
1080 DeviceReset *parent_reset)
1081 {
1082 *parent_reset = dc->reset;
1083 dc->reset = dev_reset;
1084 }
1085
1086 void device_class_set_parent_realize(DeviceClass *dc,
1087 DeviceRealize dev_realize,
1088 DeviceRealize *parent_realize)
1089 {
1090 *parent_realize = dc->realize;
1091 dc->realize = dev_realize;
1092 }
1093
1094 void device_class_set_parent_unrealize(DeviceClass *dc,
1095 DeviceUnrealize dev_unrealize,
1096 DeviceUnrealize *parent_unrealize)
1097 {
1098 *parent_unrealize = dc->unrealize;
1099 dc->unrealize = dev_unrealize;
1100 }
1101
1102 void device_reset(DeviceState *dev)
1103 {
1104 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1105
1106 if (klass->reset) {
1107 klass->reset(dev);
1108 }
1109 }
1110
1111 Object *qdev_get_machine(void)
1112 {
1113 static Object *dev;
1114
1115 if (dev == NULL) {
1116 dev = container_get(object_get_root(), "/machine");
1117 }
1118
1119 return dev;
1120 }
1121
1122 static const TypeInfo device_type_info = {
1123 .name = TYPE_DEVICE,
1124 .parent = TYPE_OBJECT,
1125 .instance_size = sizeof(DeviceState),
1126 .instance_init = device_initfn,
1127 .instance_post_init = device_post_init,
1128 .instance_finalize = device_finalize,
1129 .class_base_init = device_class_base_init,
1130 .class_init = device_class_init,
1131 .abstract = true,
1132 .class_size = sizeof(DeviceClass),
1133 };
1134
1135 static void qdev_register_types(void)
1136 {
1137 type_register_static(&device_type_info);
1138 }
1139
1140 type_init(qdev_register_types)