qdev: Introduce FWPathProvider interface
[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 "hw/qdev.h"
29 #include "hw/fw-path-provider.h"
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "monitor/monitor.h"
36 #include "hw/hotplug.h"
37
38 int qdev_hotplug = 0;
39 static bool qdev_hot_added = false;
40 static bool qdev_hot_removed = false;
41
42 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
43 {
44 DeviceClass *dc = DEVICE_GET_CLASS(dev);
45 return dc->vmsd;
46 }
47
48 const char *qdev_fw_name(DeviceState *dev)
49 {
50 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51
52 if (dc->fw_name) {
53 return dc->fw_name;
54 }
55
56 return object_get_typename(OBJECT(dev));
57 }
58
59 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
60 Error **errp);
61
62 static void bus_remove_child(BusState *bus, DeviceState *child)
63 {
64 BusChild *kid;
65
66 QTAILQ_FOREACH(kid, &bus->children, sibling) {
67 if (kid->child == child) {
68 char name[32];
69
70 snprintf(name, sizeof(name), "child[%d]", kid->index);
71 QTAILQ_REMOVE(&bus->children, kid, sibling);
72
73 /* This gives back ownership of kid->child back to us. */
74 object_property_del(OBJECT(bus), name, NULL);
75 object_unref(OBJECT(kid->child));
76 g_free(kid);
77 return;
78 }
79 }
80 }
81
82 static void bus_add_child(BusState *bus, DeviceState *child)
83 {
84 char name[32];
85 BusChild *kid = g_malloc0(sizeof(*kid));
86
87 if (qdev_hotplug) {
88 assert(bus->allow_hotplug);
89 }
90
91 kid->index = bus->max_index++;
92 kid->child = child;
93 object_ref(OBJECT(kid->child));
94
95 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
96
97 /* This transfers ownership of kid->child to the property. */
98 snprintf(name, sizeof(name), "child[%d]", kid->index);
99 object_property_add_link(OBJECT(bus), name,
100 object_get_typename(OBJECT(child)),
101 (Object **)&kid->child,
102 NULL);
103 }
104
105 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
106 {
107 dev->parent_bus = bus;
108 object_ref(OBJECT(bus));
109 bus_add_child(bus, dev);
110 }
111
112 /* Create a new device. This only initializes the device state structure
113 and allows properties to be set. qdev_init should be called to
114 initialize the actual device emulation. */
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 bus = sysbus_get_default();
147 }
148
149 qdev_set_parent_bus(dev, bus);
150 object_unref(OBJECT(dev));
151 return dev;
152 }
153
154 /* Initialize a device. Device properties should be set before calling
155 this function. IRQs and MMIO regions should be connected/mapped after
156 calling this function.
157 On failure, destroy the device and return negative value.
158 Return 0 on success. */
159 int qdev_init(DeviceState *dev)
160 {
161 Error *local_err = NULL;
162
163 assert(!dev->realized);
164
165 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
166 if (local_err != NULL) {
167 qerror_report_err(local_err);
168 error_free(local_err);
169 object_unparent(OBJECT(dev));
170 return -1;
171 }
172 return 0;
173 }
174
175 static void device_realize(DeviceState *dev, Error **err)
176 {
177 DeviceClass *dc = DEVICE_GET_CLASS(dev);
178
179 if (dc->init) {
180 int rc = dc->init(dev);
181 if (rc < 0) {
182 error_setg(err, "Device initialization failed.");
183 return;
184 }
185 }
186 }
187
188 static void device_unrealize(DeviceState *dev, Error **errp)
189 {
190 DeviceClass *dc = DEVICE_GET_CLASS(dev);
191
192 if (dc->exit) {
193 int rc = dc->exit(dev);
194 if (rc < 0) {
195 error_setg(errp, "Device exit failed.");
196 return;
197 }
198 }
199 }
200
201 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
202 int required_for_version)
203 {
204 assert(!dev->realized);
205 dev->instance_id_alias = alias_id;
206 dev->alias_required_for_version = required_for_version;
207 }
208
209 void qdev_unplug(DeviceState *dev, Error **errp)
210 {
211 DeviceClass *dc = DEVICE_GET_CLASS(dev);
212
213 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
214 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
215 return;
216 }
217
218 if (!dc->hotpluggable) {
219 error_set(errp, QERR_DEVICE_NO_HOTPLUG,
220 object_get_typename(OBJECT(dev)));
221 return;
222 }
223
224 qdev_hot_removed = true;
225
226 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
227 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
228 } else {
229 assert(dc->unplug != NULL);
230 if (dc->unplug(dev) < 0) { /* legacy handler */
231 error_set(errp, QERR_UNDEFINED_ERROR);
232 }
233 }
234 }
235
236 static int qdev_reset_one(DeviceState *dev, void *opaque)
237 {
238 device_reset(dev);
239
240 return 0;
241 }
242
243 static int qbus_reset_one(BusState *bus, void *opaque)
244 {
245 BusClass *bc = BUS_GET_CLASS(bus);
246 if (bc->reset) {
247 bc->reset(bus);
248 }
249 return 0;
250 }
251
252 void qdev_reset_all(DeviceState *dev)
253 {
254 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
255 }
256
257 void qbus_reset_all(BusState *bus)
258 {
259 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
260 }
261
262 void qbus_reset_all_fn(void *opaque)
263 {
264 BusState *bus = opaque;
265 qbus_reset_all(bus);
266 }
267
268 /* can be used as ->unplug() callback for the simple cases */
269 int qdev_simple_unplug_cb(DeviceState *dev)
270 {
271 /* just zap it */
272 object_unparent(OBJECT(dev));
273 return 0;
274 }
275
276
277 /* Like qdev_init(), but terminate program via error_report() instead of
278 returning an error value. This is okay during machine creation.
279 Don't use for hotplug, because there callers need to recover from
280 failure. Exception: if you know the device's init() callback can't
281 fail, then qdev_init_nofail() can't fail either, and is therefore
282 usable even then. But relying on the device implementation that
283 way is somewhat unclean, and best avoided. */
284 void qdev_init_nofail(DeviceState *dev)
285 {
286 const char *typename = object_get_typename(OBJECT(dev));
287
288 if (qdev_init(dev) < 0) {
289 error_report("Initialization of device %s failed", typename);
290 exit(1);
291 }
292 }
293
294 void qdev_machine_creation_done(void)
295 {
296 /*
297 * ok, initial machine setup is done, starting from now we can
298 * only create hotpluggable devices
299 */
300 qdev_hotplug = 1;
301 }
302
303 bool qdev_machine_modified(void)
304 {
305 return qdev_hot_added || qdev_hot_removed;
306 }
307
308 BusState *qdev_get_parent_bus(DeviceState *dev)
309 {
310 return dev->parent_bus;
311 }
312
313 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
314 {
315 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
316 dev, n);
317 dev->num_gpio_in += n;
318 }
319
320 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
321 {
322 assert(dev->num_gpio_out == 0);
323 dev->num_gpio_out = n;
324 dev->gpio_out = pins;
325 }
326
327 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
328 {
329 assert(n >= 0 && n < dev->num_gpio_in);
330 return dev->gpio_in[n];
331 }
332
333 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
334 {
335 assert(n >= 0 && n < dev->num_gpio_out);
336 dev->gpio_out[n] = pin;
337 }
338
339 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
340 {
341 BusState *bus;
342
343 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
344 if (strcmp(name, bus->name) == 0) {
345 return bus;
346 }
347 }
348 return NULL;
349 }
350
351 int qbus_walk_children(BusState *bus,
352 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
353 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
354 void *opaque)
355 {
356 BusChild *kid;
357 int err;
358
359 if (pre_busfn) {
360 err = pre_busfn(bus, opaque);
361 if (err) {
362 return err;
363 }
364 }
365
366 QTAILQ_FOREACH(kid, &bus->children, sibling) {
367 err = qdev_walk_children(kid->child,
368 pre_devfn, pre_busfn,
369 post_devfn, post_busfn, opaque);
370 if (err < 0) {
371 return err;
372 }
373 }
374
375 if (post_busfn) {
376 err = post_busfn(bus, opaque);
377 if (err) {
378 return err;
379 }
380 }
381
382 return 0;
383 }
384
385 int qdev_walk_children(DeviceState *dev,
386 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
387 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
388 void *opaque)
389 {
390 BusState *bus;
391 int err;
392
393 if (pre_devfn) {
394 err = pre_devfn(dev, opaque);
395 if (err) {
396 return err;
397 }
398 }
399
400 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
401 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
402 post_devfn, post_busfn, opaque);
403 if (err < 0) {
404 return err;
405 }
406 }
407
408 if (post_devfn) {
409 err = post_devfn(dev, opaque);
410 if (err) {
411 return err;
412 }
413 }
414
415 return 0;
416 }
417
418 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
419 {
420 BusChild *kid;
421 DeviceState *ret;
422 BusState *child;
423
424 QTAILQ_FOREACH(kid, &bus->children, sibling) {
425 DeviceState *dev = kid->child;
426
427 if (dev->id && strcmp(dev->id, id) == 0) {
428 return dev;
429 }
430
431 QLIST_FOREACH(child, &dev->child_bus, sibling) {
432 ret = qdev_find_recursive(child, id);
433 if (ret) {
434 return ret;
435 }
436 }
437 }
438 return NULL;
439 }
440
441 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
442 {
443 const char *typename = object_get_typename(OBJECT(bus));
444 BusClass *bc;
445 char *buf;
446 int i, len, bus_id;
447
448 bus->parent = parent;
449
450 if (name) {
451 bus->name = g_strdup(name);
452 } else if (bus->parent && bus->parent->id) {
453 /* parent device has id -> use it plus parent-bus-id for bus name */
454 bus_id = bus->parent->num_child_bus;
455
456 len = strlen(bus->parent->id) + 16;
457 buf = g_malloc(len);
458 snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
459 bus->name = buf;
460 } else {
461 /* no id -> use lowercase bus type plus global bus-id for bus name */
462 bc = BUS_GET_CLASS(bus);
463 bus_id = bc->automatic_ids++;
464
465 len = strlen(typename) + 16;
466 buf = g_malloc(len);
467 len = snprintf(buf, len, "%s.%d", typename, bus_id);
468 for (i = 0; i < len; i++) {
469 buf[i] = qemu_tolower(buf[i]);
470 }
471 bus->name = buf;
472 }
473
474 if (bus->parent) {
475 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
476 bus->parent->num_child_bus++;
477 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
478 object_unref(OBJECT(bus));
479 } else if (bus != sysbus_get_default()) {
480 /* TODO: once all bus devices are qdevified,
481 only reset handler for main_system_bus should be registered here. */
482 qemu_register_reset(qbus_reset_all_fn, bus);
483 }
484 }
485
486 static void bus_unparent(Object *obj)
487 {
488 BusState *bus = BUS(obj);
489 BusChild *kid;
490
491 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
492 DeviceState *dev = kid->child;
493 object_unparent(OBJECT(dev));
494 }
495 if (bus->parent) {
496 QLIST_REMOVE(bus, sibling);
497 bus->parent->num_child_bus--;
498 bus->parent = NULL;
499 } else {
500 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
501 qemu_unregister_reset(qbus_reset_all_fn, bus);
502 }
503 }
504
505 static bool bus_get_realized(Object *obj, Error **err)
506 {
507 BusState *bus = BUS(obj);
508
509 return bus->realized;
510 }
511
512 static void bus_set_realized(Object *obj, bool value, Error **err)
513 {
514 BusState *bus = BUS(obj);
515 BusClass *bc = BUS_GET_CLASS(bus);
516 Error *local_err = NULL;
517
518 if (value && !bus->realized) {
519 if (bc->realize) {
520 bc->realize(bus, &local_err);
521
522 if (local_err != NULL) {
523 goto error;
524 }
525
526 }
527 } else if (!value && bus->realized) {
528 if (bc->unrealize) {
529 bc->unrealize(bus, &local_err);
530
531 if (local_err != NULL) {
532 goto error;
533 }
534 }
535 }
536
537 bus->realized = value;
538 return;
539
540 error:
541 error_propagate(err, local_err);
542 }
543
544 void qbus_create_inplace(void *bus, size_t size, const char *typename,
545 DeviceState *parent, const char *name)
546 {
547 object_initialize(bus, size, typename);
548 qbus_realize(bus, parent, name);
549 }
550
551 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
552 {
553 BusState *bus;
554
555 bus = BUS(object_new(typename));
556 qbus_realize(bus, parent, name);
557
558 return bus;
559 }
560
561 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
562 {
563 BusClass *bc = BUS_GET_CLASS(bus);
564
565 if (bc->get_fw_dev_path) {
566 return bc->get_fw_dev_path(dev);
567 }
568
569 return NULL;
570 }
571
572 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
573 {
574 Object *obj = OBJECT(dev);
575 char *d = NULL;
576
577 while (!d && obj->parent) {
578 obj = obj->parent;
579 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
580 }
581 return d;
582 }
583
584 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
585 {
586 int l = 0;
587
588 if (dev && dev->parent_bus) {
589 char *d;
590 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
591 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
592 if (!d) {
593 d = bus_get_fw_dev_path(dev->parent_bus, dev);
594 }
595 if (d) {
596 l += snprintf(p + l, size - l, "%s", d);
597 g_free(d);
598 } else {
599 return l;
600 }
601 }
602 l += snprintf(p + l , size - l, "/");
603
604 return l;
605 }
606
607 char* qdev_get_fw_dev_path(DeviceState *dev)
608 {
609 char path[128];
610 int l;
611
612 l = qdev_get_fw_dev_path_helper(dev, path, 128);
613
614 path[l-1] = '\0';
615
616 return g_strdup(path);
617 }
618
619 char *qdev_get_dev_path(DeviceState *dev)
620 {
621 BusClass *bc;
622
623 if (!dev || !dev->parent_bus) {
624 return NULL;
625 }
626
627 bc = BUS_GET_CLASS(dev->parent_bus);
628 if (bc->get_dev_path) {
629 return bc->get_dev_path(dev);
630 }
631
632 return NULL;
633 }
634
635 /**
636 * Legacy property handling
637 */
638
639 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
640 const char *name, Error **errp)
641 {
642 DeviceState *dev = DEVICE(obj);
643 Property *prop = opaque;
644
645 char buffer[1024];
646 char *ptr = buffer;
647
648 prop->info->print(dev, prop, buffer, sizeof(buffer));
649 visit_type_str(v, &ptr, name, errp);
650 }
651
652 /**
653 * @qdev_add_legacy_property - adds a legacy property
654 *
655 * Do not use this is new code! Properties added through this interface will
656 * be given names and types in the "legacy" namespace.
657 *
658 * Legacy properties are string versions of other OOM properties. The format
659 * of the string depends on the property type.
660 */
661 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
662 Error **errp)
663 {
664 gchar *name;
665
666 /* Register pointer properties as legacy properties */
667 if (!prop->info->print && prop->info->get) {
668 return;
669 }
670
671 name = g_strdup_printf("legacy-%s", prop->name);
672 object_property_add(OBJECT(dev), name, "str",
673 prop->info->print ? qdev_get_legacy_property : prop->info->get,
674 NULL,
675 NULL,
676 prop, errp);
677
678 g_free(name);
679 }
680
681 /**
682 * @qdev_property_add_static - add a @Property to a device.
683 *
684 * Static properties access data in a struct. The actual type of the
685 * property and the field depends on the property type.
686 */
687 void qdev_property_add_static(DeviceState *dev, Property *prop,
688 Error **errp)
689 {
690 Error *local_err = NULL;
691 Object *obj = OBJECT(dev);
692
693 /*
694 * TODO qdev_prop_ptr does not have getters or setters. It must
695 * go now that it can be replaced with links. The test should be
696 * removed along with it: all static properties are read/write.
697 */
698 if (!prop->info->get && !prop->info->set) {
699 return;
700 }
701
702 object_property_add(obj, prop->name, prop->info->name,
703 prop->info->get, prop->info->set,
704 prop->info->release,
705 prop, &local_err);
706
707 if (local_err) {
708 error_propagate(errp, local_err);
709 return;
710 }
711 if (prop->qtype == QTYPE_NONE) {
712 return;
713 }
714
715 if (prop->qtype == QTYPE_QBOOL) {
716 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
717 } else if (prop->info->enum_table) {
718 object_property_set_str(obj, prop->info->enum_table[prop->defval],
719 prop->name, &error_abort);
720 } else if (prop->qtype == QTYPE_QINT) {
721 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
722 }
723 }
724
725 static bool device_get_realized(Object *obj, Error **err)
726 {
727 DeviceState *dev = DEVICE(obj);
728 return dev->realized;
729 }
730
731 static void device_set_realized(Object *obj, bool value, Error **err)
732 {
733 DeviceState *dev = DEVICE(obj);
734 DeviceClass *dc = DEVICE_GET_CLASS(dev);
735 BusState *bus;
736 Error *local_err = NULL;
737
738 if (dev->hotplugged && !dc->hotpluggable) {
739 error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
740 return;
741 }
742
743 if (value && !dev->realized) {
744 if (!obj->parent && local_err == NULL) {
745 static int unattached_count;
746 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
747
748 object_property_add_child(container_get(qdev_get_machine(),
749 "/unattached"),
750 name, obj, &local_err);
751 g_free(name);
752 }
753
754 if (dc->realize) {
755 dc->realize(dev, &local_err);
756 }
757
758 if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
759 local_err == NULL) {
760 hotplug_handler_plug(dev->parent_bus->hotplug_handler,
761 dev, &local_err);
762 }
763
764 if (qdev_get_vmsd(dev) && local_err == NULL) {
765 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
766 dev->instance_id_alias,
767 dev->alias_required_for_version);
768 }
769 if (local_err == NULL) {
770 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
771 object_property_set_bool(OBJECT(bus), true, "realized",
772 &local_err);
773 if (local_err != NULL) {
774 break;
775 }
776 }
777 }
778 if (dev->hotplugged && local_err == NULL) {
779 device_reset(dev);
780 }
781 } else if (!value && dev->realized) {
782 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
783 object_property_set_bool(OBJECT(bus), false, "realized",
784 &local_err);
785 if (local_err != NULL) {
786 break;
787 }
788 }
789 if (qdev_get_vmsd(dev) && local_err == NULL) {
790 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
791 }
792 if (dc->unrealize && local_err == NULL) {
793 dc->unrealize(dev, &local_err);
794 }
795 }
796
797 if (local_err != NULL) {
798 error_propagate(err, local_err);
799 return;
800 }
801
802 dev->realized = value;
803 }
804
805 static bool device_get_hotpluggable(Object *obj, Error **err)
806 {
807 DeviceClass *dc = DEVICE_GET_CLASS(obj);
808 DeviceState *dev = DEVICE(obj);
809
810 return dc->hotpluggable && (dev->parent_bus == NULL ||
811 dev->parent_bus->allow_hotplug);
812 }
813
814 static void device_initfn(Object *obj)
815 {
816 DeviceState *dev = DEVICE(obj);
817 ObjectClass *class;
818 Property *prop;
819
820 if (qdev_hotplug) {
821 dev->hotplugged = 1;
822 qdev_hot_added = true;
823 }
824
825 dev->instance_id_alias = -1;
826 dev->realized = false;
827
828 object_property_add_bool(obj, "realized",
829 device_get_realized, device_set_realized, NULL);
830 object_property_add_bool(obj, "hotpluggable",
831 device_get_hotpluggable, NULL, NULL);
832
833 class = object_get_class(OBJECT(dev));
834 do {
835 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
836 qdev_property_add_legacy(dev, prop, &error_abort);
837 qdev_property_add_static(dev, prop, &error_abort);
838 }
839 class = object_class_get_parent(class);
840 } while (class != object_class_by_name(TYPE_DEVICE));
841
842 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
843 (Object **)&dev->parent_bus, &error_abort);
844 }
845
846 static void device_post_init(Object *obj)
847 {
848 qdev_prop_set_globals(DEVICE(obj), &error_abort);
849 }
850
851 /* Unlink device from bus and free the structure. */
852 static void device_finalize(Object *obj)
853 {
854 DeviceState *dev = DEVICE(obj);
855 if (dev->opts) {
856 qemu_opts_del(dev->opts);
857 }
858 }
859
860 static void device_class_base_init(ObjectClass *class, void *data)
861 {
862 DeviceClass *klass = DEVICE_CLASS(class);
863
864 /* We explicitly look up properties in the superclasses,
865 * so do not propagate them to the subclasses.
866 */
867 klass->props = NULL;
868 }
869
870 static void device_unparent(Object *obj)
871 {
872 DeviceState *dev = DEVICE(obj);
873 BusState *bus;
874 QObject *event_data;
875 bool have_realized = dev->realized;
876
877 if (dev->realized) {
878 object_property_set_bool(obj, false, "realized", NULL);
879 }
880 while (dev->num_child_bus) {
881 bus = QLIST_FIRST(&dev->child_bus);
882 object_unparent(OBJECT(bus));
883 }
884 if (dev->parent_bus) {
885 bus_remove_child(dev->parent_bus, dev);
886 object_unref(OBJECT(dev->parent_bus));
887 dev->parent_bus = NULL;
888 }
889
890 /* Only send event if the device had been completely realized */
891 if (have_realized) {
892 gchar *path = object_get_canonical_path(OBJECT(dev));
893
894 if (dev->id) {
895 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
896 dev->id, path);
897 } else {
898 event_data = qobject_from_jsonf("{ 'path': %s }", path);
899 }
900 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
901 qobject_decref(event_data);
902 g_free(path);
903 }
904 }
905
906 static void device_class_init(ObjectClass *class, void *data)
907 {
908 DeviceClass *dc = DEVICE_CLASS(class);
909
910 class->unparent = device_unparent;
911 dc->realize = device_realize;
912 dc->unrealize = device_unrealize;
913
914 /* by default all devices were considered as hotpluggable,
915 * so with intent to check it in generic qdev_unplug() /
916 * device_set_realized() functions make every device
917 * hotpluggable. Devices that shouldn't be hotpluggable,
918 * should override it in their class_init()
919 */
920 dc->hotpluggable = true;
921 }
922
923 void device_reset(DeviceState *dev)
924 {
925 DeviceClass *klass = DEVICE_GET_CLASS(dev);
926
927 if (klass->reset) {
928 klass->reset(dev);
929 }
930 }
931
932 Object *qdev_get_machine(void)
933 {
934 static Object *dev;
935
936 if (dev == NULL) {
937 dev = container_get(object_get_root(), "/machine");
938 }
939
940 return dev;
941 }
942
943 static const TypeInfo device_type_info = {
944 .name = TYPE_DEVICE,
945 .parent = TYPE_OBJECT,
946 .instance_size = sizeof(DeviceState),
947 .instance_init = device_initfn,
948 .instance_post_init = device_post_init,
949 .instance_finalize = device_finalize,
950 .class_base_init = device_class_base_init,
951 .class_init = device_class_init,
952 .abstract = true,
953 .class_size = sizeof(DeviceClass),
954 };
955
956 static void qbus_initfn(Object *obj)
957 {
958 BusState *bus = BUS(obj);
959
960 QTAILQ_INIT(&bus->children);
961 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
962 TYPE_HOTPLUG_HANDLER,
963 (Object **)&bus->hotplug_handler, NULL);
964 object_property_add_bool(obj, "realized",
965 bus_get_realized, bus_set_realized, NULL);
966 }
967
968 static char *default_bus_get_fw_dev_path(DeviceState *dev)
969 {
970 return g_strdup(object_get_typename(OBJECT(dev)));
971 }
972
973 static void bus_class_init(ObjectClass *class, void *data)
974 {
975 BusClass *bc = BUS_CLASS(class);
976
977 class->unparent = bus_unparent;
978 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
979 }
980
981 static void qbus_finalize(Object *obj)
982 {
983 BusState *bus = BUS(obj);
984
985 g_free((char *)bus->name);
986 }
987
988 static const TypeInfo bus_info = {
989 .name = TYPE_BUS,
990 .parent = TYPE_OBJECT,
991 .instance_size = sizeof(BusState),
992 .abstract = true,
993 .class_size = sizeof(BusClass),
994 .instance_init = qbus_initfn,
995 .instance_finalize = qbus_finalize,
996 .class_init = bus_class_init,
997 };
998
999 static void qdev_register_types(void)
1000 {
1001 type_register_static(&bus_info);
1002 type_register_static(&device_type_info);
1003 }
1004
1005 type_init(qdev_register_types)