]> git.proxmox.com Git - mirror_qemu.git/blob - hw/core/qdev.c
qdev: Implement named GPIOs
[mirror_qemu.git] / hw / core / qdev.c
1 /*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
26 this API directly. */
27
28 #include "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, /* read-only property */
103 0, /* return ownership on prop deletion */
104 NULL);
105 }
106
107 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
108 {
109 dev->parent_bus = bus;
110 object_ref(OBJECT(bus));
111 bus_add_child(bus, dev);
112 }
113
114 /* Create a new device. This only initializes the device state structure
115 and allows properties to be set. qdev_init should be called to
116 initialize the actual device emulation. */
117 DeviceState *qdev_create(BusState *bus, const char *name)
118 {
119 DeviceState *dev;
120
121 dev = qdev_try_create(bus, name);
122 if (!dev) {
123 if (bus) {
124 error_report("Unknown device '%s' for bus '%s'", name,
125 object_get_typename(OBJECT(bus)));
126 } else {
127 error_report("Unknown device '%s' for default sysbus", name);
128 }
129 abort();
130 }
131
132 return dev;
133 }
134
135 DeviceState *qdev_try_create(BusState *bus, const char *type)
136 {
137 DeviceState *dev;
138
139 if (object_class_by_name(type) == NULL) {
140 return NULL;
141 }
142 dev = DEVICE(object_new(type));
143 if (!dev) {
144 return NULL;
145 }
146
147 if (!bus) {
148 bus = sysbus_get_default();
149 }
150
151 qdev_set_parent_bus(dev, bus);
152 object_unref(OBJECT(dev));
153 return dev;
154 }
155
156 /* Initialize a device. Device properties should be set before calling
157 this function. IRQs and MMIO regions should be connected/mapped after
158 calling this function.
159 On failure, destroy the device and return negative value.
160 Return 0 on success. */
161 int qdev_init(DeviceState *dev)
162 {
163 Error *local_err = NULL;
164
165 assert(!dev->realized);
166
167 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
168 if (local_err != NULL) {
169 qerror_report_err(local_err);
170 error_free(local_err);
171 object_unparent(OBJECT(dev));
172 return -1;
173 }
174 return 0;
175 }
176
177 static void device_realize(DeviceState *dev, Error **errp)
178 {
179 DeviceClass *dc = DEVICE_GET_CLASS(dev);
180
181 if (dc->init) {
182 int rc = dc->init(dev);
183 if (rc < 0) {
184 error_setg(errp, "Device initialization failed.");
185 return;
186 }
187 }
188 }
189
190 static void device_unrealize(DeviceState *dev, Error **errp)
191 {
192 DeviceClass *dc = DEVICE_GET_CLASS(dev);
193
194 if (dc->exit) {
195 int rc = dc->exit(dev);
196 if (rc < 0) {
197 error_setg(errp, "Device exit failed.");
198 return;
199 }
200 }
201 }
202
203 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
204 int required_for_version)
205 {
206 assert(!dev->realized);
207 dev->instance_id_alias = alias_id;
208 dev->alias_required_for_version = required_for_version;
209 }
210
211 void qdev_unplug(DeviceState *dev, Error **errp)
212 {
213 DeviceClass *dc = DEVICE_GET_CLASS(dev);
214
215 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
216 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
217 return;
218 }
219
220 if (!dc->hotpluggable) {
221 error_set(errp, QERR_DEVICE_NO_HOTPLUG,
222 object_get_typename(OBJECT(dev)));
223 return;
224 }
225
226 qdev_hot_removed = true;
227
228 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
229 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
230 } else {
231 assert(dc->unplug != NULL);
232 if (dc->unplug(dev) < 0) { /* legacy handler */
233 error_set(errp, QERR_UNDEFINED_ERROR);
234 }
235 }
236 }
237
238 static int qdev_reset_one(DeviceState *dev, void *opaque)
239 {
240 device_reset(dev);
241
242 return 0;
243 }
244
245 static int qbus_reset_one(BusState *bus, void *opaque)
246 {
247 BusClass *bc = BUS_GET_CLASS(bus);
248 if (bc->reset) {
249 bc->reset(bus);
250 }
251 return 0;
252 }
253
254 void qdev_reset_all(DeviceState *dev)
255 {
256 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
257 }
258
259 void qbus_reset_all(BusState *bus)
260 {
261 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
262 }
263
264 void qbus_reset_all_fn(void *opaque)
265 {
266 BusState *bus = opaque;
267 qbus_reset_all(bus);
268 }
269
270 /* can be used as ->unplug() callback for the simple cases */
271 int qdev_simple_unplug_cb(DeviceState *dev)
272 {
273 /* just zap it */
274 object_unparent(OBJECT(dev));
275 return 0;
276 }
277
278
279 /* Like qdev_init(), but terminate program via error_report() instead of
280 returning an error value. This is okay during machine creation.
281 Don't use for hotplug, because there callers need to recover from
282 failure. Exception: if you know the device's init() callback can't
283 fail, then qdev_init_nofail() can't fail either, and is therefore
284 usable even then. But relying on the device implementation that
285 way is somewhat unclean, and best avoided. */
286 void qdev_init_nofail(DeviceState *dev)
287 {
288 const char *typename = object_get_typename(OBJECT(dev));
289
290 if (qdev_init(dev) < 0) {
291 error_report("Initialization of device %s failed", typename);
292 exit(1);
293 }
294 }
295
296 void qdev_machine_creation_done(void)
297 {
298 /*
299 * ok, initial machine setup is done, starting from now we can
300 * only create hotpluggable devices
301 */
302 qdev_hotplug = 1;
303 }
304
305 bool qdev_machine_modified(void)
306 {
307 return qdev_hot_added || qdev_hot_removed;
308 }
309
310 BusState *qdev_get_parent_bus(DeviceState *dev)
311 {
312 return dev->parent_bus;
313 }
314
315 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
316 const char *name)
317 {
318 NamedGPIOList *ngl;
319
320 QLIST_FOREACH(ngl, &dev->gpios, node) {
321 /* NULL is a valid and matchable name, otherwise do a normal
322 * strcmp match.
323 */
324 if ((!ngl->name && !name) ||
325 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
326 return ngl;
327 }
328 }
329
330 ngl = g_malloc0(sizeof(*ngl));
331 ngl->name = g_strdup(name);
332 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
333 return ngl;
334 }
335
336 void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
337 const char *name, int n)
338 {
339 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
340
341 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
342 dev, n);
343 gpio_list->num_in += n;
344 }
345
346 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
347 {
348 qdev_init_gpio_in_named(dev, handler, NULL, n);
349 }
350
351 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
352 const char *name, int n)
353 {
354 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
355
356 assert(gpio_list->num_out == 0);
357 gpio_list->num_out = n;
358 gpio_list->out = pins;
359 }
360
361 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
362 {
363 qdev_init_gpio_out_named(dev, pins, NULL, n);
364 }
365
366 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
367 {
368 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
369
370 assert(n >= 0 && n < gpio_list->num_in);
371 return gpio_list->in[n];
372 }
373
374 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
375 {
376 return qdev_get_gpio_in_named(dev, NULL, n);
377 }
378
379 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
380 qemu_irq pin)
381 {
382 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
383
384 assert(n >= 0 && n < gpio_list->num_out);
385 gpio_list->out[n] = pin;
386 }
387
388 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
389 {
390 qdev_connect_gpio_out_named(dev, NULL, n, pin);
391 }
392
393 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
394 {
395 BusState *bus;
396
397 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
398 if (strcmp(name, bus->name) == 0) {
399 return bus;
400 }
401 }
402 return NULL;
403 }
404
405 int qbus_walk_children(BusState *bus,
406 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
407 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
408 void *opaque)
409 {
410 BusChild *kid;
411 int err;
412
413 if (pre_busfn) {
414 err = pre_busfn(bus, opaque);
415 if (err) {
416 return err;
417 }
418 }
419
420 QTAILQ_FOREACH(kid, &bus->children, sibling) {
421 err = qdev_walk_children(kid->child,
422 pre_devfn, pre_busfn,
423 post_devfn, post_busfn, opaque);
424 if (err < 0) {
425 return err;
426 }
427 }
428
429 if (post_busfn) {
430 err = post_busfn(bus, opaque);
431 if (err) {
432 return err;
433 }
434 }
435
436 return 0;
437 }
438
439 int qdev_walk_children(DeviceState *dev,
440 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
441 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
442 void *opaque)
443 {
444 BusState *bus;
445 int err;
446
447 if (pre_devfn) {
448 err = pre_devfn(dev, opaque);
449 if (err) {
450 return err;
451 }
452 }
453
454 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
455 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
456 post_devfn, post_busfn, opaque);
457 if (err < 0) {
458 return err;
459 }
460 }
461
462 if (post_devfn) {
463 err = post_devfn(dev, opaque);
464 if (err) {
465 return err;
466 }
467 }
468
469 return 0;
470 }
471
472 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
473 {
474 BusChild *kid;
475 DeviceState *ret;
476 BusState *child;
477
478 QTAILQ_FOREACH(kid, &bus->children, sibling) {
479 DeviceState *dev = kid->child;
480
481 if (dev->id && strcmp(dev->id, id) == 0) {
482 return dev;
483 }
484
485 QLIST_FOREACH(child, &dev->child_bus, sibling) {
486 ret = qdev_find_recursive(child, id);
487 if (ret) {
488 return ret;
489 }
490 }
491 }
492 return NULL;
493 }
494
495 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
496 {
497 const char *typename = object_get_typename(OBJECT(bus));
498 BusClass *bc;
499 char *buf;
500 int i, len, bus_id;
501
502 bus->parent = parent;
503
504 if (name) {
505 bus->name = g_strdup(name);
506 } else if (bus->parent && bus->parent->id) {
507 /* parent device has id -> use it plus parent-bus-id for bus name */
508 bus_id = bus->parent->num_child_bus;
509
510 len = strlen(bus->parent->id) + 16;
511 buf = g_malloc(len);
512 snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
513 bus->name = buf;
514 } else {
515 /* no id -> use lowercase bus type plus global bus-id for bus name */
516 bc = BUS_GET_CLASS(bus);
517 bus_id = bc->automatic_ids++;
518
519 len = strlen(typename) + 16;
520 buf = g_malloc(len);
521 len = snprintf(buf, len, "%s.%d", typename, bus_id);
522 for (i = 0; i < len; i++) {
523 buf[i] = qemu_tolower(buf[i]);
524 }
525 bus->name = buf;
526 }
527
528 if (bus->parent) {
529 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
530 bus->parent->num_child_bus++;
531 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
532 object_unref(OBJECT(bus));
533 } else if (bus != sysbus_get_default()) {
534 /* TODO: once all bus devices are qdevified,
535 only reset handler for main_system_bus should be registered here. */
536 qemu_register_reset(qbus_reset_all_fn, bus);
537 }
538 }
539
540 static void bus_unparent(Object *obj)
541 {
542 BusState *bus = BUS(obj);
543 BusChild *kid;
544
545 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
546 DeviceState *dev = kid->child;
547 object_unparent(OBJECT(dev));
548 }
549 if (bus->parent) {
550 QLIST_REMOVE(bus, sibling);
551 bus->parent->num_child_bus--;
552 bus->parent = NULL;
553 } else {
554 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
555 qemu_unregister_reset(qbus_reset_all_fn, bus);
556 }
557 }
558
559 static bool bus_get_realized(Object *obj, Error **errp)
560 {
561 BusState *bus = BUS(obj);
562
563 return bus->realized;
564 }
565
566 static void bus_set_realized(Object *obj, bool value, Error **errp)
567 {
568 BusState *bus = BUS(obj);
569 BusClass *bc = BUS_GET_CLASS(bus);
570 Error *local_err = NULL;
571
572 if (value && !bus->realized) {
573 if (bc->realize) {
574 bc->realize(bus, &local_err);
575
576 if (local_err != NULL) {
577 goto error;
578 }
579
580 }
581 } else if (!value && bus->realized) {
582 if (bc->unrealize) {
583 bc->unrealize(bus, &local_err);
584
585 if (local_err != NULL) {
586 goto error;
587 }
588 }
589 }
590
591 bus->realized = value;
592 return;
593
594 error:
595 error_propagate(errp, local_err);
596 }
597
598 void qbus_create_inplace(void *bus, size_t size, const char *typename,
599 DeviceState *parent, const char *name)
600 {
601 object_initialize(bus, size, typename);
602 qbus_realize(bus, parent, name);
603 }
604
605 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
606 {
607 BusState *bus;
608
609 bus = BUS(object_new(typename));
610 qbus_realize(bus, parent, name);
611
612 return bus;
613 }
614
615 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
616 {
617 BusClass *bc = BUS_GET_CLASS(bus);
618
619 if (bc->get_fw_dev_path) {
620 return bc->get_fw_dev_path(dev);
621 }
622
623 return NULL;
624 }
625
626 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
627 {
628 Object *obj = OBJECT(dev);
629 char *d = NULL;
630
631 while (!d && obj->parent) {
632 obj = obj->parent;
633 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
634 }
635 return d;
636 }
637
638 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
639 {
640 int l = 0;
641
642 if (dev && dev->parent_bus) {
643 char *d;
644 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
645 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
646 if (!d) {
647 d = bus_get_fw_dev_path(dev->parent_bus, dev);
648 }
649 if (d) {
650 l += snprintf(p + l, size - l, "%s", d);
651 g_free(d);
652 } else {
653 return l;
654 }
655 }
656 l += snprintf(p + l , size - l, "/");
657
658 return l;
659 }
660
661 char* qdev_get_fw_dev_path(DeviceState *dev)
662 {
663 char path[128];
664 int l;
665
666 l = qdev_get_fw_dev_path_helper(dev, path, 128);
667
668 path[l-1] = '\0';
669
670 return g_strdup(path);
671 }
672
673 char *qdev_get_dev_path(DeviceState *dev)
674 {
675 BusClass *bc;
676
677 if (!dev || !dev->parent_bus) {
678 return NULL;
679 }
680
681 bc = BUS_GET_CLASS(dev->parent_bus);
682 if (bc->get_dev_path) {
683 return bc->get_dev_path(dev);
684 }
685
686 return NULL;
687 }
688
689 /**
690 * Legacy property handling
691 */
692
693 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
694 const char *name, Error **errp)
695 {
696 DeviceState *dev = DEVICE(obj);
697 Property *prop = opaque;
698
699 char buffer[1024];
700 char *ptr = buffer;
701
702 prop->info->print(dev, prop, buffer, sizeof(buffer));
703 visit_type_str(v, &ptr, name, errp);
704 }
705
706 /**
707 * @qdev_add_legacy_property - adds a legacy property
708 *
709 * Do not use this is new code! Properties added through this interface will
710 * be given names and types in the "legacy" namespace.
711 *
712 * Legacy properties are string versions of other OOM properties. The format
713 * of the string depends on the property type.
714 */
715 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
716 Error **errp)
717 {
718 gchar *name;
719
720 /* Register pointer properties as legacy properties */
721 if (!prop->info->print && prop->info->get) {
722 return;
723 }
724
725 name = g_strdup_printf("legacy-%s", prop->name);
726 object_property_add(OBJECT(dev), name, "str",
727 prop->info->print ? qdev_get_legacy_property : prop->info->get,
728 NULL,
729 NULL,
730 prop, errp);
731
732 g_free(name);
733 }
734
735 /**
736 * @qdev_property_add_static - add a @Property to a device.
737 *
738 * Static properties access data in a struct. The actual type of the
739 * property and the field depends on the property type.
740 */
741 void qdev_property_add_static(DeviceState *dev, Property *prop,
742 Error **errp)
743 {
744 Error *local_err = NULL;
745 Object *obj = OBJECT(dev);
746
747 /*
748 * TODO qdev_prop_ptr does not have getters or setters. It must
749 * go now that it can be replaced with links. The test should be
750 * removed along with it: all static properties are read/write.
751 */
752 if (!prop->info->get && !prop->info->set) {
753 return;
754 }
755
756 object_property_add(obj, prop->name, prop->info->name,
757 prop->info->get, prop->info->set,
758 prop->info->release,
759 prop, &local_err);
760
761 if (local_err) {
762 error_propagate(errp, local_err);
763 return;
764 }
765 if (prop->qtype == QTYPE_NONE) {
766 return;
767 }
768
769 if (prop->qtype == QTYPE_QBOOL) {
770 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
771 } else if (prop->info->enum_table) {
772 object_property_set_str(obj, prop->info->enum_table[prop->defval],
773 prop->name, &error_abort);
774 } else if (prop->qtype == QTYPE_QINT) {
775 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
776 }
777 }
778
779 static bool device_get_realized(Object *obj, Error **errp)
780 {
781 DeviceState *dev = DEVICE(obj);
782 return dev->realized;
783 }
784
785 static void device_set_realized(Object *obj, bool value, Error **errp)
786 {
787 DeviceState *dev = DEVICE(obj);
788 DeviceClass *dc = DEVICE_GET_CLASS(dev);
789 BusState *bus;
790 Error *local_err = NULL;
791
792 if (dev->hotplugged && !dc->hotpluggable) {
793 error_set(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
794 return;
795 }
796
797 if (value && !dev->realized) {
798 if (!obj->parent && local_err == NULL) {
799 static int unattached_count;
800 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
801
802 object_property_add_child(container_get(qdev_get_machine(),
803 "/unattached"),
804 name, obj, &local_err);
805 g_free(name);
806 }
807
808 if (dc->realize) {
809 dc->realize(dev, &local_err);
810 }
811
812 if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
813 local_err == NULL) {
814 hotplug_handler_plug(dev->parent_bus->hotplug_handler,
815 dev, &local_err);
816 }
817
818 if (qdev_get_vmsd(dev) && local_err == NULL) {
819 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
820 dev->instance_id_alias,
821 dev->alias_required_for_version);
822 }
823 if (local_err == NULL) {
824 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
825 object_property_set_bool(OBJECT(bus), true, "realized",
826 &local_err);
827 if (local_err != NULL) {
828 break;
829 }
830 }
831 }
832 if (dev->hotplugged && local_err == NULL) {
833 device_reset(dev);
834 }
835 } else if (!value && dev->realized) {
836 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
837 object_property_set_bool(OBJECT(bus), false, "realized",
838 &local_err);
839 if (local_err != NULL) {
840 break;
841 }
842 }
843 if (qdev_get_vmsd(dev) && local_err == NULL) {
844 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
845 }
846 if (dc->unrealize && local_err == NULL) {
847 dc->unrealize(dev, &local_err);
848 }
849 }
850
851 if (local_err != NULL) {
852 error_propagate(errp, local_err);
853 return;
854 }
855
856 dev->realized = value;
857 }
858
859 static bool device_get_hotpluggable(Object *obj, Error **errp)
860 {
861 DeviceClass *dc = DEVICE_GET_CLASS(obj);
862 DeviceState *dev = DEVICE(obj);
863
864 return dc->hotpluggable && (dev->parent_bus == NULL ||
865 dev->parent_bus->allow_hotplug);
866 }
867
868 static void device_initfn(Object *obj)
869 {
870 DeviceState *dev = DEVICE(obj);
871 ObjectClass *class;
872 Property *prop;
873
874 if (qdev_hotplug) {
875 dev->hotplugged = 1;
876 qdev_hot_added = true;
877 }
878
879 dev->instance_id_alias = -1;
880 dev->realized = false;
881
882 object_property_add_bool(obj, "realized",
883 device_get_realized, device_set_realized, NULL);
884 object_property_add_bool(obj, "hotpluggable",
885 device_get_hotpluggable, NULL, NULL);
886
887 class = object_get_class(OBJECT(dev));
888 do {
889 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
890 qdev_property_add_legacy(dev, prop, &error_abort);
891 qdev_property_add_static(dev, prop, &error_abort);
892 }
893 class = object_class_get_parent(class);
894 } while (class != object_class_by_name(TYPE_DEVICE));
895
896 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
897 (Object **)&dev->parent_bus, NULL, 0,
898 &error_abort);
899 QLIST_INIT(&dev->gpios);
900 }
901
902 static void device_post_init(Object *obj)
903 {
904 qdev_prop_set_globals(DEVICE(obj), &error_abort);
905 }
906
907 /* Unlink device from bus and free the structure. */
908 static void device_finalize(Object *obj)
909 {
910 NamedGPIOList *ngl, *next;
911
912 DeviceState *dev = DEVICE(obj);
913 if (dev->opts) {
914 qemu_opts_del(dev->opts);
915 }
916
917 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
918 QLIST_REMOVE(ngl, node);
919 qemu_free_irqs(ngl->in);
920 g_free(ngl->name);
921 g_free(ngl);
922 /* ngl->out irqs are owned by the other end and should not be freed
923 * here
924 */
925 }
926 }
927
928 static void device_class_base_init(ObjectClass *class, void *data)
929 {
930 DeviceClass *klass = DEVICE_CLASS(class);
931
932 /* We explicitly look up properties in the superclasses,
933 * so do not propagate them to the subclasses.
934 */
935 klass->props = NULL;
936 }
937
938 static void device_unparent(Object *obj)
939 {
940 DeviceState *dev = DEVICE(obj);
941 BusState *bus;
942 QObject *event_data;
943 bool have_realized = dev->realized;
944
945 if (dev->realized) {
946 object_property_set_bool(obj, false, "realized", NULL);
947 }
948 while (dev->num_child_bus) {
949 bus = QLIST_FIRST(&dev->child_bus);
950 object_unparent(OBJECT(bus));
951 }
952 if (dev->parent_bus) {
953 bus_remove_child(dev->parent_bus, dev);
954 object_unref(OBJECT(dev->parent_bus));
955 dev->parent_bus = NULL;
956 }
957
958 /* Only send event if the device had been completely realized */
959 if (have_realized) {
960 gchar *path = object_get_canonical_path(OBJECT(dev));
961
962 if (dev->id) {
963 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
964 dev->id, path);
965 } else {
966 event_data = qobject_from_jsonf("{ 'path': %s }", path);
967 }
968 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
969 qobject_decref(event_data);
970 g_free(path);
971 }
972 }
973
974 static void device_class_init(ObjectClass *class, void *data)
975 {
976 DeviceClass *dc = DEVICE_CLASS(class);
977
978 class->unparent = device_unparent;
979 dc->realize = device_realize;
980 dc->unrealize = device_unrealize;
981
982 /* by default all devices were considered as hotpluggable,
983 * so with intent to check it in generic qdev_unplug() /
984 * device_set_realized() functions make every device
985 * hotpluggable. Devices that shouldn't be hotpluggable,
986 * should override it in their class_init()
987 */
988 dc->hotpluggable = true;
989 }
990
991 void device_reset(DeviceState *dev)
992 {
993 DeviceClass *klass = DEVICE_GET_CLASS(dev);
994
995 if (klass->reset) {
996 klass->reset(dev);
997 }
998 }
999
1000 Object *qdev_get_machine(void)
1001 {
1002 static Object *dev;
1003
1004 if (dev == NULL) {
1005 dev = container_get(object_get_root(), "/machine");
1006 }
1007
1008 return dev;
1009 }
1010
1011 static const TypeInfo device_type_info = {
1012 .name = TYPE_DEVICE,
1013 .parent = TYPE_OBJECT,
1014 .instance_size = sizeof(DeviceState),
1015 .instance_init = device_initfn,
1016 .instance_post_init = device_post_init,
1017 .instance_finalize = device_finalize,
1018 .class_base_init = device_class_base_init,
1019 .class_init = device_class_init,
1020 .abstract = true,
1021 .class_size = sizeof(DeviceClass),
1022 };
1023
1024 static void qbus_initfn(Object *obj)
1025 {
1026 BusState *bus = BUS(obj);
1027
1028 QTAILQ_INIT(&bus->children);
1029 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
1030 TYPE_HOTPLUG_HANDLER,
1031 (Object **)&bus->hotplug_handler,
1032 object_property_allow_set_link,
1033 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1034 NULL);
1035 object_property_add_bool(obj, "realized",
1036 bus_get_realized, bus_set_realized, NULL);
1037 }
1038
1039 static char *default_bus_get_fw_dev_path(DeviceState *dev)
1040 {
1041 return g_strdup(object_get_typename(OBJECT(dev)));
1042 }
1043
1044 static void bus_class_init(ObjectClass *class, void *data)
1045 {
1046 BusClass *bc = BUS_CLASS(class);
1047
1048 class->unparent = bus_unparent;
1049 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
1050 }
1051
1052 static void qbus_finalize(Object *obj)
1053 {
1054 BusState *bus = BUS(obj);
1055
1056 g_free((char *)bus->name);
1057 }
1058
1059 static const TypeInfo bus_info = {
1060 .name = TYPE_BUS,
1061 .parent = TYPE_OBJECT,
1062 .instance_size = sizeof(BusState),
1063 .abstract = true,
1064 .class_size = sizeof(BusClass),
1065 .instance_init = qbus_initfn,
1066 .instance_finalize = qbus_finalize,
1067 .class_init = bus_class_init,
1068 };
1069
1070 static void qdev_register_types(void)
1071 {
1072 type_register_static(&bus_info);
1073 type_register_static(&device_type_info);
1074 }
1075
1076 type_init(qdev_register_types)