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