qdev: Convert to qdev_unrealize() manually
[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 "qapi/error.h"
30 #include "qapi/qapi-events-qdev.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/visitor.h"
33 #include "qemu/error-report.h"
34 #include "qemu/option.h"
35 #include "hw/hotplug.h"
36 #include "hw/irq.h"
37 #include "hw/qdev-properties.h"
38 #include "hw/boards.h"
39 #include "hw/sysbus.h"
40 #include "hw/qdev-clock.h"
41 #include "migration/vmstate.h"
42 #include "trace.h"
43
44 bool qdev_hotplug = false;
45 static bool qdev_hot_added = false;
46 bool qdev_hot_removed = false;
47
48 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
49 {
50 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51 return dc->vmsd;
52 }
53
54 static void bus_remove_child(BusState *bus, DeviceState *child)
55 {
56 BusChild *kid;
57
58 QTAILQ_FOREACH(kid, &bus->children, sibling) {
59 if (kid->child == child) {
60 char name[32];
61
62 snprintf(name, sizeof(name), "child[%d]", kid->index);
63 QTAILQ_REMOVE(&bus->children, kid, sibling);
64
65 bus->num_children--;
66
67 /* This gives back ownership of kid->child back to us. */
68 object_property_del(OBJECT(bus), name);
69 object_unref(OBJECT(kid->child));
70 g_free(kid);
71 return;
72 }
73 }
74 }
75
76 static void bus_add_child(BusState *bus, DeviceState *child)
77 {
78 char name[32];
79 BusChild *kid = g_malloc0(sizeof(*kid));
80
81 bus->num_children++;
82 kid->index = bus->max_index++;
83 kid->child = child;
84 object_ref(OBJECT(kid->child));
85
86 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
87
88 /* This transfers ownership of kid->child to the property. */
89 snprintf(name, sizeof(name), "child[%d]", kid->index);
90 object_property_add_link(OBJECT(bus), name,
91 object_get_typename(OBJECT(child)),
92 (Object **)&kid->child,
93 NULL, /* read-only property */
94 0);
95 }
96
97 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
98 {
99 BusState *old_parent_bus = dev->parent_bus;
100 DeviceClass *dc = DEVICE_GET_CLASS(dev);
101
102 assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type));
103
104 if (old_parent_bus) {
105 trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)),
106 old_parent_bus, object_get_typename(OBJECT(old_parent_bus)),
107 OBJECT(bus), object_get_typename(OBJECT(bus)));
108 /*
109 * Keep a reference to the device while it's not plugged into
110 * any bus, to avoid it potentially evaporating when it is
111 * dereffed in bus_remove_child().
112 * Also keep the ref of the parent bus until the end, so that
113 * we can safely call resettable_change_parent() below.
114 */
115 object_ref(OBJECT(dev));
116 bus_remove_child(dev->parent_bus, dev);
117 }
118 dev->parent_bus = bus;
119 object_ref(OBJECT(bus));
120 bus_add_child(bus, dev);
121 if (dev->realized) {
122 resettable_change_parent(OBJECT(dev), OBJECT(bus),
123 OBJECT(old_parent_bus));
124 }
125 if (old_parent_bus) {
126 object_unref(OBJECT(old_parent_bus));
127 object_unref(OBJECT(dev));
128 }
129 }
130
131 /* Create a new device. This only initializes the device state
132 structure and allows properties to be set. The device still needs
133 to be realized. See qdev-core.h. */
134 DeviceState *qdev_create(BusState *bus, const char *name)
135 {
136 DeviceState *dev;
137
138 dev = qdev_try_create(bus, name);
139 if (!dev) {
140 if (bus) {
141 error_report("Unknown device '%s' for bus '%s'", name,
142 object_get_typename(OBJECT(bus)));
143 } else {
144 error_report("Unknown device '%s' for default sysbus", name);
145 }
146 abort();
147 }
148
149 return dev;
150 }
151
152 DeviceState *qdev_try_create(BusState *bus, const char *type)
153 {
154 DeviceState *dev;
155
156 if (object_class_by_name(type) == NULL) {
157 return NULL;
158 }
159 dev = DEVICE(object_new(type));
160 if (!dev) {
161 return NULL;
162 }
163
164 if (!bus) {
165 /* Assert that the device really is a SysBusDevice before
166 * we put it onto the sysbus. Non-sysbus devices which aren't
167 * being put onto a bus should be created with object_new(TYPE_FOO),
168 * not qdev_create(NULL, TYPE_FOO).
169 */
170 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
171 bus = sysbus_get_default();
172 }
173
174 qdev_set_parent_bus(dev, bus);
175 object_unref(OBJECT(dev));
176 return dev;
177 }
178
179 /*
180 * Create a device on the heap.
181 * A type @name must exist.
182 * This only initializes the device state structure and allows
183 * properties to be set. The device still needs to be realized. See
184 * qdev-core.h.
185 */
186 DeviceState *qdev_new(const char *name)
187 {
188 return DEVICE(object_new(name));
189 }
190
191 /*
192 * Try to create a device on the heap.
193 * This is like qdev_new(), except it returns %NULL when type @name
194 * does not exist.
195 */
196 DeviceState *qdev_try_new(const char *name)
197 {
198 if (!object_class_by_name(name)) {
199 return NULL;
200 }
201
202 return DEVICE(object_new(name));
203 }
204
205 static QTAILQ_HEAD(, DeviceListener) device_listeners
206 = QTAILQ_HEAD_INITIALIZER(device_listeners);
207
208 enum ListenerDirection { Forward, Reverse };
209
210 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
211 do { \
212 DeviceListener *_listener; \
213 \
214 switch (_direction) { \
215 case Forward: \
216 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
217 if (_listener->_callback) { \
218 _listener->_callback(_listener, ##_args); \
219 } \
220 } \
221 break; \
222 case Reverse: \
223 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
224 link) { \
225 if (_listener->_callback) { \
226 _listener->_callback(_listener, ##_args); \
227 } \
228 } \
229 break; \
230 default: \
231 abort(); \
232 } \
233 } while (0)
234
235 static int device_listener_add(DeviceState *dev, void *opaque)
236 {
237 DEVICE_LISTENER_CALL(realize, Forward, dev);
238
239 return 0;
240 }
241
242 void device_listener_register(DeviceListener *listener)
243 {
244 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
245
246 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
247 NULL, NULL);
248 }
249
250 void device_listener_unregister(DeviceListener *listener)
251 {
252 QTAILQ_REMOVE(&device_listeners, listener, link);
253 }
254
255 bool qdev_should_hide_device(QemuOpts *opts)
256 {
257 int rc = -1;
258 DeviceListener *listener;
259
260 QTAILQ_FOREACH(listener, &device_listeners, link) {
261 if (listener->should_be_hidden) {
262 /*
263 * should_be_hidden_will return
264 * 1 if device matches opts and it should be hidden
265 * 0 if device matches opts and should not be hidden
266 * -1 if device doesn't match ops
267 */
268 rc = listener->should_be_hidden(listener, opts);
269 }
270
271 if (rc > 0) {
272 break;
273 }
274 }
275
276 return rc > 0;
277 }
278
279 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
280 int required_for_version)
281 {
282 assert(!dev->realized);
283 dev->instance_id_alias = alias_id;
284 dev->alias_required_for_version = required_for_version;
285 }
286
287 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
288 {
289 MachineState *machine;
290 MachineClass *mc;
291 Object *m_obj = qdev_get_machine();
292
293 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
294 machine = MACHINE(m_obj);
295 mc = MACHINE_GET_CLASS(machine);
296 if (mc->get_hotplug_handler) {
297 return mc->get_hotplug_handler(machine, dev);
298 }
299 }
300
301 return NULL;
302 }
303
304 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
305 {
306 MachineState *machine;
307 MachineClass *mc;
308 Object *m_obj = qdev_get_machine();
309
310 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
311 machine = MACHINE(m_obj);
312 mc = MACHINE_GET_CLASS(machine);
313 if (mc->hotplug_allowed) {
314 return mc->hotplug_allowed(machine, dev, errp);
315 }
316 }
317
318 return true;
319 }
320
321 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
322 {
323 if (dev->parent_bus) {
324 return dev->parent_bus->hotplug_handler;
325 }
326 return NULL;
327 }
328
329 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
330 {
331 HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
332
333 if (hotplug_ctrl == NULL && dev->parent_bus) {
334 hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
335 }
336 return hotplug_ctrl;
337 }
338
339 static int qdev_prereset(DeviceState *dev, void *opaque)
340 {
341 trace_qdev_reset_tree(dev, object_get_typename(OBJECT(dev)));
342 return 0;
343 }
344
345 static int qbus_prereset(BusState *bus, void *opaque)
346 {
347 trace_qbus_reset_tree(bus, object_get_typename(OBJECT(bus)));
348 return 0;
349 }
350
351 static int qdev_reset_one(DeviceState *dev, void *opaque)
352 {
353 device_legacy_reset(dev);
354
355 return 0;
356 }
357
358 static int qbus_reset_one(BusState *bus, void *opaque)
359 {
360 BusClass *bc = BUS_GET_CLASS(bus);
361 trace_qbus_reset(bus, object_get_typename(OBJECT(bus)));
362 if (bc->reset) {
363 bc->reset(bus);
364 }
365 return 0;
366 }
367
368 void qdev_reset_all(DeviceState *dev)
369 {
370 trace_qdev_reset_all(dev, object_get_typename(OBJECT(dev)));
371 qdev_walk_children(dev, qdev_prereset, qbus_prereset,
372 qdev_reset_one, qbus_reset_one, NULL);
373 }
374
375 void qdev_reset_all_fn(void *opaque)
376 {
377 qdev_reset_all(DEVICE(opaque));
378 }
379
380 void qbus_reset_all(BusState *bus)
381 {
382 trace_qbus_reset_all(bus, object_get_typename(OBJECT(bus)));
383 qbus_walk_children(bus, qdev_prereset, qbus_prereset,
384 qdev_reset_one, qbus_reset_one, NULL);
385 }
386
387 void qbus_reset_all_fn(void *opaque)
388 {
389 BusState *bus = opaque;
390 qbus_reset_all(bus);
391 }
392
393 void device_cold_reset(DeviceState *dev)
394 {
395 resettable_reset(OBJECT(dev), RESET_TYPE_COLD);
396 }
397
398 bool device_is_in_reset(DeviceState *dev)
399 {
400 return resettable_is_in_reset(OBJECT(dev));
401 }
402
403 static ResettableState *device_get_reset_state(Object *obj)
404 {
405 DeviceState *dev = DEVICE(obj);
406 return &dev->reset;
407 }
408
409 static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
410 void *opaque, ResetType type)
411 {
412 DeviceState *dev = DEVICE(obj);
413 BusState *bus;
414
415 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
416 cb(OBJECT(bus), opaque, type);
417 }
418 }
419
420 /* can be used as ->unplug() callback for the simple cases */
421 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
422 DeviceState *dev, Error **errp)
423 {
424 qdev_unrealize(dev);
425 }
426
427 /*
428 * Realize @dev.
429 * Device properties should be set before calling this function. IRQs
430 * and MMIO regions should be connected/mapped after calling this
431 * function.
432 * On failure, report an error with error_report() and terminate the
433 * program. This is okay during machine creation. Don't use for
434 * hotplug, because there callers need to recover from failure.
435 * Exception: if you know the device's init() callback can't fail,
436 * then qdev_init_nofail() can't fail either, and is therefore usable
437 * even then. But relying on the device implementation that way is
438 * somewhat unclean, and best avoided.
439 */
440 void qdev_init_nofail(DeviceState *dev)
441 {
442 Error *err = NULL;
443
444 assert(!dev->realized);
445
446 object_ref(OBJECT(dev));
447 object_property_set_bool(OBJECT(dev), true, "realized", &err);
448 if (err) {
449 error_reportf_err(err, "Initialization of device %s failed: ",
450 object_get_typename(OBJECT(dev)));
451 exit(1);
452 }
453 object_unref(OBJECT(dev));
454 }
455
456 /*
457 * Realize @dev.
458 * @dev must not be plugged into a bus.
459 * Plug @dev into @bus if non-null, else into the main system bus.
460 * This takes a reference to @dev.
461 * If @dev has no QOM parent, make one up, taking another reference.
462 * On success, return true.
463 * On failure, store an error through @errp and return false.
464 */
465 bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
466 {
467 Error *err = NULL;
468
469 assert(!dev->realized && !dev->parent_bus);
470
471 if (!bus) {
472 /*
473 * Assert that the device really is a SysBusDevice before we
474 * put it onto the sysbus. Non-sysbus devices which aren't
475 * being put onto a bus should be realized with
476 * object_property_set_bool(OBJECT(dev), true, "realized",
477 * errp);
478 */
479 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
480 bus = sysbus_get_default();
481 }
482
483 qdev_set_parent_bus(dev, bus);
484
485 object_property_set_bool(OBJECT(dev), true, "realized", &err);
486 if (err) {
487 error_propagate(errp, err);
488 }
489 return !err;
490 }
491
492 /*
493 * Realize @dev and drop a reference.
494 * This is like qdev_realize(), except the caller must hold a
495 * (private) reference, which is dropped on return regardless of
496 * success or failure. Intended use:
497 * dev = qdev_new();
498 * [...]
499 * qdev_realize_and_unref(dev, bus, errp);
500 * Now @dev can go away without further ado.
501 */
502 bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp)
503 {
504 bool ret;
505
506 ret = qdev_realize(dev, bus, errp);
507 object_unref(OBJECT(dev));
508 return ret;
509 }
510
511 void qdev_unrealize(DeviceState *dev)
512 {
513 object_property_set_bool(OBJECT(dev), false, "realized", &error_abort);
514 }
515
516 static int qdev_assert_realized_properly(Object *obj, void *opaque)
517 {
518 DeviceState *dev = DEVICE(object_dynamic_cast(obj, TYPE_DEVICE));
519 DeviceClass *dc;
520
521 if (dev) {
522 dc = DEVICE_GET_CLASS(dev);
523 assert(dev->realized);
524 assert(dev->parent_bus || !dc->bus_type);
525 }
526 return 0;
527 }
528
529 void qdev_machine_creation_done(void)
530 {
531 /*
532 * ok, initial machine setup is done, starting from now we can
533 * only create hotpluggable devices
534 */
535 qdev_hotplug = true;
536
537 object_child_foreach_recursive(object_get_root(),
538 qdev_assert_realized_properly, NULL);
539 }
540
541 bool qdev_machine_modified(void)
542 {
543 return qdev_hot_added || qdev_hot_removed;
544 }
545
546 BusState *qdev_get_parent_bus(DeviceState *dev)
547 {
548 return dev->parent_bus;
549 }
550
551 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
552 const char *name)
553 {
554 NamedGPIOList *ngl;
555
556 QLIST_FOREACH(ngl, &dev->gpios, node) {
557 /* NULL is a valid and matchable name. */
558 if (g_strcmp0(name, ngl->name) == 0) {
559 return ngl;
560 }
561 }
562
563 ngl = g_malloc0(sizeof(*ngl));
564 ngl->name = g_strdup(name);
565 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
566 return ngl;
567 }
568
569 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
570 qemu_irq_handler handler,
571 void *opaque,
572 const char *name, int n)
573 {
574 int i;
575 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
576
577 assert(gpio_list->num_out == 0 || !name);
578 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
579 opaque, n);
580
581 if (!name) {
582 name = "unnamed-gpio-in";
583 }
584 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
585 gchar *propname = g_strdup_printf("%s[%u]", name, i);
586
587 object_property_add_child(OBJECT(dev), propname,
588 OBJECT(gpio_list->in[i]));
589 g_free(propname);
590 }
591
592 gpio_list->num_in += n;
593 }
594
595 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
596 {
597 qdev_init_gpio_in_named(dev, handler, NULL, n);
598 }
599
600 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
601 const char *name, int n)
602 {
603 int i;
604 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
605
606 assert(gpio_list->num_in == 0 || !name);
607
608 if (!name) {
609 name = "unnamed-gpio-out";
610 }
611 memset(pins, 0, sizeof(*pins) * n);
612 for (i = 0; i < n; ++i) {
613 gchar *propname = g_strdup_printf("%s[%u]", name,
614 gpio_list->num_out + i);
615
616 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
617 (Object **)&pins[i],
618 object_property_allow_set_link,
619 OBJ_PROP_LINK_STRONG);
620 g_free(propname);
621 }
622 gpio_list->num_out += n;
623 }
624
625 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
626 {
627 qdev_init_gpio_out_named(dev, pins, NULL, n);
628 }
629
630 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
631 {
632 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
633
634 assert(n >= 0 && n < gpio_list->num_in);
635 return gpio_list->in[n];
636 }
637
638 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
639 {
640 return qdev_get_gpio_in_named(dev, NULL, n);
641 }
642
643 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
644 qemu_irq pin)
645 {
646 char *propname = g_strdup_printf("%s[%d]",
647 name ? name : "unnamed-gpio-out", n);
648 if (pin && !OBJECT(pin)->parent) {
649 /* We need a name for object_property_set_link to work */
650 object_property_add_child(container_get(qdev_get_machine(),
651 "/unattached"),
652 "non-qdev-gpio[*]", OBJECT(pin));
653 }
654 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
655 g_free(propname);
656 }
657
658 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
659 {
660 g_autofree char *propname = g_strdup_printf("%s[%d]",
661 name ? name : "unnamed-gpio-out", n);
662
663 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
664 NULL);
665
666 return ret;
667 }
668
669 /* disconnect a GPIO output, returning the disconnected input (if any) */
670
671 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
672 const char *name, int n)
673 {
674 char *propname = g_strdup_printf("%s[%d]",
675 name ? name : "unnamed-gpio-out", n);
676
677 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
678 NULL);
679 if (ret) {
680 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
681 }
682 g_free(propname);
683 return ret;
684 }
685
686 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
687 const char *name, int n)
688 {
689 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
690 qdev_connect_gpio_out_named(dev, name, n, icpt);
691 return disconnected;
692 }
693
694 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
695 {
696 qdev_connect_gpio_out_named(dev, NULL, n, pin);
697 }
698
699 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
700 const char *name)
701 {
702 int i;
703 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
704
705 for (i = 0; i < ngl->num_in; i++) {
706 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
707 char *propname = g_strdup_printf("%s[%d]", nm, i);
708
709 object_property_add_alias(OBJECT(container), propname,
710 OBJECT(dev), propname);
711 g_free(propname);
712 }
713 for (i = 0; i < ngl->num_out; i++) {
714 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
715 char *propname = g_strdup_printf("%s[%d]", nm, i);
716
717 object_property_add_alias(OBJECT(container), propname,
718 OBJECT(dev), propname);
719 g_free(propname);
720 }
721 QLIST_REMOVE(ngl, node);
722 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
723 }
724
725 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
726 {
727 BusState *bus;
728 Object *child = object_resolve_path_component(OBJECT(dev), name);
729
730 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
731 if (bus) {
732 return bus;
733 }
734
735 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
736 if (strcmp(name, bus->name) == 0) {
737 return bus;
738 }
739 }
740 return NULL;
741 }
742
743 int qdev_walk_children(DeviceState *dev,
744 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
745 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
746 void *opaque)
747 {
748 BusState *bus;
749 int err;
750
751 if (pre_devfn) {
752 err = pre_devfn(dev, opaque);
753 if (err) {
754 return err;
755 }
756 }
757
758 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
759 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
760 post_devfn, post_busfn, opaque);
761 if (err < 0) {
762 return err;
763 }
764 }
765
766 if (post_devfn) {
767 err = post_devfn(dev, opaque);
768 if (err) {
769 return err;
770 }
771 }
772
773 return 0;
774 }
775
776 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
777 {
778 BusChild *kid;
779 DeviceState *ret;
780 BusState *child;
781
782 QTAILQ_FOREACH(kid, &bus->children, sibling) {
783 DeviceState *dev = kid->child;
784
785 if (dev->id && strcmp(dev->id, id) == 0) {
786 return dev;
787 }
788
789 QLIST_FOREACH(child, &dev->child_bus, sibling) {
790 ret = qdev_find_recursive(child, id);
791 if (ret) {
792 return ret;
793 }
794 }
795 }
796 return NULL;
797 }
798
799 char *qdev_get_dev_path(DeviceState *dev)
800 {
801 BusClass *bc;
802
803 if (!dev || !dev->parent_bus) {
804 return NULL;
805 }
806
807 bc = BUS_GET_CLASS(dev->parent_bus);
808 if (bc->get_dev_path) {
809 return bc->get_dev_path(dev);
810 }
811
812 return NULL;
813 }
814
815 /**
816 * Legacy property handling
817 */
818
819 static void qdev_get_legacy_property(Object *obj, Visitor *v,
820 const char *name, void *opaque,
821 Error **errp)
822 {
823 DeviceState *dev = DEVICE(obj);
824 Property *prop = opaque;
825
826 char buffer[1024];
827 char *ptr = buffer;
828
829 prop->info->print(dev, prop, buffer, sizeof(buffer));
830 visit_type_str(v, name, &ptr, errp);
831 }
832
833 /**
834 * qdev_class_add_legacy_property:
835 * @dev: Device to add the property to.
836 * @prop: The qdev property definition.
837 *
838 * Add a legacy QOM property to @dev for qdev property @prop.
839 *
840 * Legacy properties are string versions of QOM properties. The format of
841 * the string depends on the property type. Legacy properties are only
842 * needed for "info qtree".
843 *
844 * Do not use this in new code! QOM Properties added through this interface
845 * will be given names in the "legacy" namespace.
846 */
847 static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop)
848 {
849 g_autofree char *name = NULL;
850
851 /* Register pointer properties as legacy properties */
852 if (!prop->info->print && prop->info->get) {
853 return;
854 }
855
856 name = g_strdup_printf("legacy-%s", prop->name);
857 object_class_property_add(OBJECT_CLASS(dc), name, "str",
858 prop->info->print ? qdev_get_legacy_property : prop->info->get,
859 NULL, NULL, prop);
860 }
861
862 void qdev_property_add_static(DeviceState *dev, Property *prop)
863 {
864 Object *obj = OBJECT(dev);
865 ObjectProperty *op;
866
867 assert(!prop->info->create);
868
869 op = object_property_add(obj, prop->name, prop->info->name,
870 prop->info->get, prop->info->set,
871 prop->info->release,
872 prop);
873
874 object_property_set_description(obj, prop->name,
875 prop->info->description);
876
877 if (prop->set_default) {
878 prop->info->set_default_value(op, prop);
879 if (op->init) {
880 op->init(obj, op);
881 }
882 }
883 }
884
885 static void qdev_class_add_property(DeviceClass *klass, Property *prop)
886 {
887 ObjectClass *oc = OBJECT_CLASS(klass);
888
889 if (prop->info->create) {
890 prop->info->create(oc, prop);
891 } else {
892 ObjectProperty *op;
893
894 op = object_class_property_add(oc,
895 prop->name, prop->info->name,
896 prop->info->get, prop->info->set,
897 prop->info->release,
898 prop);
899 if (prop->set_default) {
900 prop->info->set_default_value(op, prop);
901 }
902 }
903 object_class_property_set_description(oc, prop->name,
904 prop->info->description);
905 }
906
907 /* @qdev_alias_all_properties - Add alias properties to the source object for
908 * all qdev properties on the target DeviceState.
909 */
910 void qdev_alias_all_properties(DeviceState *target, Object *source)
911 {
912 ObjectClass *class;
913 Property *prop;
914
915 class = object_get_class(OBJECT(target));
916 do {
917 DeviceClass *dc = DEVICE_CLASS(class);
918
919 for (prop = dc->props_; prop && prop->name; prop++) {
920 object_property_add_alias(source, prop->name,
921 OBJECT(target), prop->name);
922 }
923 class = object_class_get_parent(class);
924 } while (class != object_class_by_name(TYPE_DEVICE));
925 }
926
927 static bool device_get_realized(Object *obj, Error **errp)
928 {
929 DeviceState *dev = DEVICE(obj);
930 return dev->realized;
931 }
932
933 static bool check_only_migratable(Object *obj, Error **errp)
934 {
935 DeviceClass *dc = DEVICE_GET_CLASS(obj);
936
937 if (!vmstate_check_only_migratable(dc->vmsd)) {
938 error_setg(errp, "Device %s is not migratable, but "
939 "--only-migratable was specified",
940 object_get_typename(obj));
941 return false;
942 }
943
944 return true;
945 }
946
947 static void device_set_realized(Object *obj, bool value, Error **errp)
948 {
949 DeviceState *dev = DEVICE(obj);
950 DeviceClass *dc = DEVICE_GET_CLASS(dev);
951 HotplugHandler *hotplug_ctrl;
952 BusState *bus;
953 NamedClockList *ncl;
954 Error *local_err = NULL;
955 bool unattached_parent = false;
956 static int unattached_count;
957
958 if (dev->hotplugged && !dc->hotpluggable) {
959 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
960 return;
961 }
962
963 if (value && !dev->realized) {
964 if (!check_only_migratable(obj, &local_err)) {
965 goto fail;
966 }
967
968 if (!obj->parent) {
969 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
970
971 object_property_add_child(container_get(qdev_get_machine(),
972 "/unattached"),
973 name, obj);
974 unattached_parent = true;
975 g_free(name);
976 }
977
978 hotplug_ctrl = qdev_get_hotplug_handler(dev);
979 if (hotplug_ctrl) {
980 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
981 if (local_err != NULL) {
982 goto fail;
983 }
984 }
985
986 if (dc->realize) {
987 dc->realize(dev, &local_err);
988 if (local_err != NULL) {
989 goto fail;
990 }
991 }
992
993 DEVICE_LISTENER_CALL(realize, Forward, dev);
994
995 /*
996 * always free/re-initialize here since the value cannot be cleaned up
997 * in device_unrealize due to its usage later on in the unplug path
998 */
999 g_free(dev->canonical_path);
1000 dev->canonical_path = object_get_canonical_path(OBJECT(dev));
1001 QLIST_FOREACH(ncl, &dev->clocks, node) {
1002 if (ncl->alias) {
1003 continue;
1004 } else {
1005 clock_setup_canonical_path(ncl->clock);
1006 }
1007 }
1008
1009 if (qdev_get_vmsd(dev)) {
1010 if (vmstate_register_with_alias_id(VMSTATE_IF(dev),
1011 VMSTATE_INSTANCE_ID_ANY,
1012 qdev_get_vmsd(dev), dev,
1013 dev->instance_id_alias,
1014 dev->alias_required_for_version,
1015 &local_err) < 0) {
1016 goto post_realize_fail;
1017 }
1018 }
1019
1020 /*
1021 * Clear the reset state, in case the object was previously unrealized
1022 * with a dirty state.
1023 */
1024 resettable_state_clear(&dev->reset);
1025
1026 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
1027 if (!qbus_realize(bus, errp)) {
1028 goto child_realize_fail;
1029 }
1030 }
1031 if (dev->hotplugged) {
1032 /*
1033 * Reset the device, as well as its subtree which, at this point,
1034 * should be realized too.
1035 */
1036 resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD);
1037 resettable_change_parent(OBJECT(dev), OBJECT(dev->parent_bus),
1038 NULL);
1039 resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD);
1040 }
1041 dev->pending_deleted_event = false;
1042
1043 if (hotplug_ctrl) {
1044 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
1045 if (local_err != NULL) {
1046 goto child_realize_fail;
1047 }
1048 }
1049
1050 } else if (!value && dev->realized) {
1051 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
1052 qbus_unrealize(bus);
1053 }
1054 if (qdev_get_vmsd(dev)) {
1055 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
1056 }
1057 if (dc->unrealize) {
1058 dc->unrealize(dev);
1059 }
1060 dev->pending_deleted_event = true;
1061 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
1062 }
1063
1064 assert(local_err == NULL);
1065 dev->realized = value;
1066 return;
1067
1068 child_realize_fail:
1069 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
1070 qbus_unrealize(bus);
1071 }
1072
1073 if (qdev_get_vmsd(dev)) {
1074 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
1075 }
1076
1077 post_realize_fail:
1078 g_free(dev->canonical_path);
1079 dev->canonical_path = NULL;
1080 if (dc->unrealize) {
1081 dc->unrealize(dev);
1082 }
1083
1084 fail:
1085 error_propagate(errp, local_err);
1086 if (unattached_parent) {
1087 /*
1088 * Beware, this doesn't just revert
1089 * object_property_add_child(), it also runs bus_remove()!
1090 */
1091 object_unparent(OBJECT(dev));
1092 unattached_count--;
1093 }
1094 }
1095
1096 static bool device_get_hotpluggable(Object *obj, Error **errp)
1097 {
1098 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1099 DeviceState *dev = DEVICE(obj);
1100
1101 return dc->hotpluggable && (dev->parent_bus == NULL ||
1102 qbus_is_hotpluggable(dev->parent_bus));
1103 }
1104
1105 static bool device_get_hotplugged(Object *obj, Error **errp)
1106 {
1107 DeviceState *dev = DEVICE(obj);
1108
1109 return dev->hotplugged;
1110 }
1111
1112 static void device_initfn(Object *obj)
1113 {
1114 DeviceState *dev = DEVICE(obj);
1115
1116 if (qdev_hotplug) {
1117 dev->hotplugged = 1;
1118 qdev_hot_added = true;
1119 }
1120
1121 dev->instance_id_alias = -1;
1122 dev->realized = false;
1123 dev->allow_unplug_during_migration = false;
1124
1125 QLIST_INIT(&dev->gpios);
1126 QLIST_INIT(&dev->clocks);
1127 }
1128
1129 static void device_post_init(Object *obj)
1130 {
1131 /*
1132 * Note: ordered so that the user's global properties take
1133 * precedence.
1134 */
1135 object_apply_compat_props(obj);
1136 qdev_prop_set_globals(DEVICE(obj));
1137 }
1138
1139 /* Unlink device from bus and free the structure. */
1140 static void device_finalize(Object *obj)
1141 {
1142 NamedGPIOList *ngl, *next;
1143
1144 DeviceState *dev = DEVICE(obj);
1145
1146 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1147 QLIST_REMOVE(ngl, node);
1148 qemu_free_irqs(ngl->in, ngl->num_in);
1149 g_free(ngl->name);
1150 g_free(ngl);
1151 /* ngl->out irqs are owned by the other end and should not be freed
1152 * here
1153 */
1154 }
1155
1156 qdev_finalize_clocklist(dev);
1157
1158 /* Only send event if the device had been completely realized */
1159 if (dev->pending_deleted_event) {
1160 g_assert(dev->canonical_path);
1161
1162 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
1163 g_free(dev->canonical_path);
1164 dev->canonical_path = NULL;
1165 }
1166
1167 qemu_opts_del(dev->opts);
1168 }
1169
1170 static void device_class_base_init(ObjectClass *class, void *data)
1171 {
1172 DeviceClass *klass = DEVICE_CLASS(class);
1173
1174 /* We explicitly look up properties in the superclasses,
1175 * so do not propagate them to the subclasses.
1176 */
1177 klass->props_ = NULL;
1178 }
1179
1180 static void device_unparent(Object *obj)
1181 {
1182 DeviceState *dev = DEVICE(obj);
1183 BusState *bus;
1184
1185 if (dev->realized) {
1186 qdev_unrealize(dev);
1187 }
1188 while (dev->num_child_bus) {
1189 bus = QLIST_FIRST(&dev->child_bus);
1190 object_unparent(OBJECT(bus));
1191 }
1192 if (dev->parent_bus) {
1193 bus_remove_child(dev->parent_bus, dev);
1194 object_unref(OBJECT(dev->parent_bus));
1195 dev->parent_bus = NULL;
1196 }
1197 }
1198
1199 static char *
1200 device_vmstate_if_get_id(VMStateIf *obj)
1201 {
1202 DeviceState *dev = DEVICE(obj);
1203
1204 return qdev_get_dev_path(dev);
1205 }
1206
1207 /**
1208 * device_phases_reset:
1209 * Transition reset method for devices to allow moving
1210 * smoothly from legacy reset method to multi-phases
1211 */
1212 static void device_phases_reset(DeviceState *dev)
1213 {
1214 ResettableClass *rc = RESETTABLE_GET_CLASS(dev);
1215
1216 if (rc->phases.enter) {
1217 rc->phases.enter(OBJECT(dev), RESET_TYPE_COLD);
1218 }
1219 if (rc->phases.hold) {
1220 rc->phases.hold(OBJECT(dev));
1221 }
1222 if (rc->phases.exit) {
1223 rc->phases.exit(OBJECT(dev));
1224 }
1225 }
1226
1227 static void device_transitional_reset(Object *obj)
1228 {
1229 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1230
1231 /*
1232 * This will call either @device_phases_reset (for multi-phases transitioned
1233 * devices) or a device's specific method for not-yet transitioned devices.
1234 * In both case, it does not reset children.
1235 */
1236 if (dc->reset) {
1237 dc->reset(DEVICE(obj));
1238 }
1239 }
1240
1241 /**
1242 * device_get_transitional_reset:
1243 * check if the device's class is ready for multi-phase
1244 */
1245 static ResettableTrFunction device_get_transitional_reset(Object *obj)
1246 {
1247 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1248 if (dc->reset != device_phases_reset) {
1249 /*
1250 * dc->reset has been overridden by a subclass,
1251 * the device is not ready for multi phase yet.
1252 */
1253 return device_transitional_reset;
1254 }
1255 return NULL;
1256 }
1257
1258 static void device_class_init(ObjectClass *class, void *data)
1259 {
1260 DeviceClass *dc = DEVICE_CLASS(class);
1261 VMStateIfClass *vc = VMSTATE_IF_CLASS(class);
1262 ResettableClass *rc = RESETTABLE_CLASS(class);
1263
1264 class->unparent = device_unparent;
1265
1266 /* by default all devices were considered as hotpluggable,
1267 * so with intent to check it in generic qdev_unplug() /
1268 * device_set_realized() functions make every device
1269 * hotpluggable. Devices that shouldn't be hotpluggable,
1270 * should override it in their class_init()
1271 */
1272 dc->hotpluggable = true;
1273 dc->user_creatable = true;
1274 vc->get_id = device_vmstate_if_get_id;
1275 rc->get_state = device_get_reset_state;
1276 rc->child_foreach = device_reset_child_foreach;
1277
1278 /*
1279 * @device_phases_reset is put as the default reset method below, allowing
1280 * to do the multi-phase transition from base classes to leaf classes. It
1281 * allows a legacy-reset Device class to extend a multi-phases-reset
1282 * Device class for the following reason:
1283 * + If a base class B has been moved to multi-phase, then it does not
1284 * override this default reset method and may have defined phase methods.
1285 * + A child class C (extending class B) which uses
1286 * device_class_set_parent_reset() (or similar means) to override the
1287 * reset method will still work as expected. @device_phases_reset function
1288 * will be registered as the parent reset method and effectively call
1289 * parent reset phases.
1290 */
1291 dc->reset = device_phases_reset;
1292 rc->get_transitional_function = device_get_transitional_reset;
1293
1294 object_class_property_add_bool(class, "realized",
1295 device_get_realized, device_set_realized);
1296 object_class_property_add_bool(class, "hotpluggable",
1297 device_get_hotpluggable, NULL);
1298 object_class_property_add_bool(class, "hotplugged",
1299 device_get_hotplugged, NULL);
1300 object_class_property_add_link(class, "parent_bus", TYPE_BUS,
1301 offsetof(DeviceState, parent_bus), NULL, 0);
1302 }
1303
1304 void device_class_set_props(DeviceClass *dc, Property *props)
1305 {
1306 Property *prop;
1307
1308 dc->props_ = props;
1309 for (prop = props; prop && prop->name; prop++) {
1310 qdev_class_add_legacy_property(dc, prop);
1311 qdev_class_add_property(dc, prop);
1312 }
1313 }
1314
1315 void device_class_set_parent_reset(DeviceClass *dc,
1316 DeviceReset dev_reset,
1317 DeviceReset *parent_reset)
1318 {
1319 *parent_reset = dc->reset;
1320 dc->reset = dev_reset;
1321 }
1322
1323 void device_class_set_parent_realize(DeviceClass *dc,
1324 DeviceRealize dev_realize,
1325 DeviceRealize *parent_realize)
1326 {
1327 *parent_realize = dc->realize;
1328 dc->realize = dev_realize;
1329 }
1330
1331 void device_class_set_parent_unrealize(DeviceClass *dc,
1332 DeviceUnrealize dev_unrealize,
1333 DeviceUnrealize *parent_unrealize)
1334 {
1335 *parent_unrealize = dc->unrealize;
1336 dc->unrealize = dev_unrealize;
1337 }
1338
1339 void device_legacy_reset(DeviceState *dev)
1340 {
1341 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1342
1343 trace_qdev_reset(dev, object_get_typename(OBJECT(dev)));
1344 if (klass->reset) {
1345 klass->reset(dev);
1346 }
1347 }
1348
1349 Object *qdev_get_machine(void)
1350 {
1351 static Object *dev;
1352
1353 if (dev == NULL) {
1354 dev = container_get(object_get_root(), "/machine");
1355 }
1356
1357 return dev;
1358 }
1359
1360 static const TypeInfo device_type_info = {
1361 .name = TYPE_DEVICE,
1362 .parent = TYPE_OBJECT,
1363 .instance_size = sizeof(DeviceState),
1364 .instance_init = device_initfn,
1365 .instance_post_init = device_post_init,
1366 .instance_finalize = device_finalize,
1367 .class_base_init = device_class_base_init,
1368 .class_init = device_class_init,
1369 .abstract = true,
1370 .class_size = sizeof(DeviceClass),
1371 .interfaces = (InterfaceInfo[]) {
1372 { TYPE_VMSTATE_IF },
1373 { TYPE_RESETTABLE_INTERFACE },
1374 { }
1375 }
1376 };
1377
1378 static void qdev_register_types(void)
1379 {
1380 type_register_static(&device_type_info);
1381 }
1382
1383 type_init(qdev_register_types)