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