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