]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/qdev.c
vl, qapi: offset calculation in RTC_CHANGE event reverted
[mirror_qemu.git] / hw / core / qdev.c
CommitLineData
aae9460e
PB
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
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aae9460e
PB
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
18c86e2b 28#include "qemu/osdep.h"
83c9f4ca 29#include "hw/qdev.h"
9c17d615 30#include "sysemu/sysemu.h"
e688df6b 31#include "qapi/error.h"
112ed241 32#include "qapi/qapi-events-misc.h"
b4a42f81 33#include "qapi/qmp/qerror.h"
7b1b5d19 34#include "qapi/visitor.h"
d49b6836 35#include "qemu/error-report.h"
922a01a0 36#include "qemu/option.h"
0ee4de6c 37#include "hw/hotplug.h"
b7454548 38#include "hw/boards.h"
7474f1be 39#include "hw/sysbus.h"
aae9460e 40
9bed84c1 41bool qdev_hotplug = false;
0ac8ef71 42static bool qdev_hot_added = false;
21def24a 43bool qdev_hot_removed = false;
3418bd25 44
4be9f0d1
AL
45const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
46{
6e008585
AL
47 DeviceClass *dc = DEVICE_GET_CLASS(dev);
48 return dc->vmsd;
4be9f0d1
AL
49}
50
0866aca1 51static void bus_remove_child(BusState *bus, DeviceState *child)
0c17542d 52{
0866aca1
AL
53 BusChild *kid;
54
55 QTAILQ_FOREACH(kid, &bus->children, sibling) {
56 if (kid->child == child) {
57 char name[32];
58
59 snprintf(name, sizeof(name), "child[%d]", kid->index);
60 QTAILQ_REMOVE(&bus->children, kid, sibling);
9d127820
PB
61
62 /* This gives back ownership of kid->child back to us. */
0866aca1 63 object_property_del(OBJECT(bus), name, NULL);
9d127820 64 object_unref(OBJECT(kid->child));
0866aca1
AL
65 g_free(kid);
66 return;
67 }
68 }
69}
70
71static void bus_add_child(BusState *bus, DeviceState *child)
72{
73 char name[32];
74 BusChild *kid = g_malloc0(sizeof(*kid));
0c17542d 75
0866aca1
AL
76 kid->index = bus->max_index++;
77 kid->child = child;
9d127820 78 object_ref(OBJECT(kid->child));
a5296ca9 79
0866aca1
AL
80 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
81
9d127820 82 /* This transfers ownership of kid->child to the property. */
0866aca1
AL
83 snprintf(name, sizeof(name), "child[%d]", kid->index);
84 object_property_add_link(OBJECT(bus), name,
85 object_get_typename(OBJECT(child)),
39f72ef9
SH
86 (Object **)&kid->child,
87 NULL, /* read-only property */
88 0, /* return ownership on prop deletion */
89 NULL);
0866aca1
AL
90}
91
92void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
93{
91c968ac
PM
94 bool replugging = dev->parent_bus != NULL;
95
96 if (replugging) {
97 /* Keep a reference to the device while it's not plugged into
98 * any bus, to avoid it potentially evaporating when it is
99 * dereffed in bus_remove_child().
100 */
101 object_ref(OBJECT(dev));
102 bus_remove_child(dev->parent_bus, dev);
103 object_unref(OBJECT(dev->parent_bus));
104 }
9fbe6127 105 dev->parent_bus = bus;
62d7ba66 106 object_ref(OBJECT(bus));
0866aca1 107 bus_add_child(bus, dev);
91c968ac
PM
108 if (replugging) {
109 object_unref(OBJECT(dev));
110 }
0c17542d
MA
111}
112
0210afe6
MA
113/* Create a new device. This only initializes the device state
114 structure and allows properties to be set. The device still needs
115 to be realized. See qdev-core.h. */
02e2da45 116DeviceState *qdev_create(BusState *bus, const char *name)
0bcdeda7
BS
117{
118 DeviceState *dev;
119
120 dev = qdev_try_create(bus, name);
121 if (!dev) {
e92714c7 122 if (bus) {
312fd5f2 123 error_report("Unknown device '%s' for bus '%s'", name,
23e3fbec 124 object_get_typename(OBJECT(bus)));
e92714c7 125 } else {
312fd5f2 126 error_report("Unknown device '%s' for default sysbus", name);
e92714c7 127 }
01ed1d52 128 abort();
0bcdeda7
BS
129 }
130
131 return dev;
132}
133
da57febf 134DeviceState *qdev_try_create(BusState *bus, const char *type)
aae9460e 135{
9fbe6127
AL
136 DeviceState *dev;
137
da57febf 138 if (object_class_by_name(type) == NULL) {
4ed658ca
AF
139 return NULL;
140 }
da57febf 141 dev = DEVICE(object_new(type));
9fbe6127
AL
142 if (!dev) {
143 return NULL;
144 }
145
10c4c98a 146 if (!bus) {
7474f1be
PM
147 /* Assert that the device really is a SysBusDevice before
148 * we put it onto the sysbus. Non-sysbus devices which aren't
149 * being put onto a bus should be created with object_new(TYPE_FOO),
150 * not qdev_create(NULL, TYPE_FOO).
151 */
152 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
68694897 153 bus = sysbus_get_default();
10c4c98a
GH
154 }
155
9fbe6127 156 qdev_set_parent_bus(dev, bus);
b09995ae 157 object_unref(OBJECT(dev));
9fbe6127 158 return dev;
aae9460e
PB
159}
160
707ff800
PD
161static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
162 = QTAILQ_HEAD_INITIALIZER(device_listeners);
163
164enum ListenerDirection { Forward, Reverse };
165
166#define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
167 do { \
168 DeviceListener *_listener; \
169 \
170 switch (_direction) { \
171 case Forward: \
172 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
173 if (_listener->_callback) { \
174 _listener->_callback(_listener, ##_args); \
175 } \
176 } \
177 break; \
178 case Reverse: \
179 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
180 device_listeners, link) { \
181 if (_listener->_callback) { \
182 _listener->_callback(_listener, ##_args); \
183 } \
184 } \
185 break; \
186 default: \
187 abort(); \
188 } \
189 } while (0)
190
191static int device_listener_add(DeviceState *dev, void *opaque)
192{
193 DEVICE_LISTENER_CALL(realize, Forward, dev);
194
195 return 0;
196}
197
198void device_listener_register(DeviceListener *listener)
199{
200 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
201
202 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
203 NULL, NULL);
204}
205
206void device_listener_unregister(DeviceListener *listener)
207{
208 QTAILQ_REMOVE(&device_listeners, listener, link);
209}
210
4d2ffa08
JK
211void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
212 int required_for_version)
213{
7983c8a3 214 assert(!dev->realized);
4d2ffa08
JK
215 dev->instance_id_alias = alias_id;
216 dev->alias_required_for_version = required_for_version;
217}
218
03fcbd9d
TH
219HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
220{
221 MachineState *machine;
222 MachineClass *mc;
223 Object *m_obj = qdev_get_machine();
224
225 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
226 machine = MACHINE(m_obj);
227 mc = MACHINE_GET_CLASS(machine);
228 if (mc->get_hotplug_handler) {
229 return mc->get_hotplug_handler(machine, dev);
230 }
231 }
232
233 return NULL;
234}
235
c06b2ffb 236HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
7716b8ca 237{
03fcbd9d 238 HotplugHandler *hotplug_ctrl;
7716b8ca
IM
239
240 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
241 hotplug_ctrl = dev->parent_bus->hotplug_handler;
03fcbd9d
TH
242 } else {
243 hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
7716b8ca
IM
244 }
245 return hotplug_ctrl;
246}
247
ec990eb6
AL
248static int qdev_reset_one(DeviceState *dev, void *opaque)
249{
94afdadc 250 device_reset(dev);
ec990eb6
AL
251
252 return 0;
253}
254
b4694b7c
IY
255static int qbus_reset_one(BusState *bus, void *opaque)
256{
0d936928
AL
257 BusClass *bc = BUS_GET_CLASS(bus);
258 if (bc->reset) {
dcc20931 259 bc->reset(bus);
b4694b7c
IY
260 }
261 return 0;
262}
263
5af0a04b
IY
264void qdev_reset_all(DeviceState *dev)
265{
dcc20931 266 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
5af0a04b
IY
267}
268
ff8de075
DH
269void qdev_reset_all_fn(void *opaque)
270{
271 qdev_reset_all(DEVICE(opaque));
272}
273
d0508c36
PB
274void qbus_reset_all(BusState *bus)
275{
dcc20931 276 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
d0508c36
PB
277}
278
80376c3f
IY
279void qbus_reset_all_fn(void *opaque)
280{
281 BusState *bus = opaque;
d0508c36 282 qbus_reset_all(bus);
80376c3f
IY
283}
284
3418bd25 285/* can be used as ->unplug() callback for the simple cases */
014176f9
IM
286void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
287 DeviceState *dev, Error **errp)
288{
2d9a982f
IM
289 /* just zap it */
290 object_unparent(OBJECT(dev));
014176f9 291}
3b29a101 292
0210afe6
MA
293/*
294 * Realize @dev.
295 * Device properties should be set before calling this function. IRQs
296 * and MMIO regions should be connected/mapped after calling this
297 * function.
298 * On failure, report an error with error_report() and terminate the
299 * program. This is okay during machine creation. Don't use for
300 * hotplug, because there callers need to recover from failure.
301 * Exception: if you know the device's init() callback can't fail,
302 * then qdev_init_nofail() can't fail either, and is therefore usable
303 * even then. But relying on the device implementation that way is
304 * somewhat unclean, and best avoided.
305 */
e23a1b33
MA
306void qdev_init_nofail(DeviceState *dev)
307{
c4bacafb 308 Error *err = NULL;
7de3abe5 309
c4bacafb
MA
310 assert(!dev->realized);
311
0d4104e5 312 object_ref(OBJECT(dev));
c4bacafb
MA
313 object_property_set_bool(OBJECT(dev), true, "realized", &err);
314 if (err) {
c29b77f9
MA
315 error_reportf_err(err, "Initialization of device %s failed: ",
316 object_get_typename(OBJECT(dev)));
bd6c9a61
MA
317 exit(1);
318 }
0d4104e5 319 object_unref(OBJECT(dev));
e23a1b33
MA
320}
321
3418bd25
GH
322void qdev_machine_creation_done(void)
323{
324 /*
325 * ok, initial machine setup is done, starting from now we can
326 * only create hotpluggable devices
327 */
9bed84c1 328 qdev_hotplug = true;
3418bd25
GH
329}
330
0ac8ef71
AW
331bool qdev_machine_modified(void)
332{
333 return qdev_hot_added || qdev_hot_removed;
334}
335
02e2da45 336BusState *qdev_get_parent_bus(DeviceState *dev)
aae9460e 337{
02e2da45 338 return dev->parent_bus;
aae9460e
PB
339}
340
a5f54290
PC
341static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
342 const char *name)
343{
344 NamedGPIOList *ngl;
345
346 QLIST_FOREACH(ngl, &dev->gpios, node) {
347 /* NULL is a valid and matchable name, otherwise do a normal
348 * strcmp match.
349 */
350 if ((!ngl->name && !name) ||
351 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
352 return ngl;
353 }
354 }
355
356 ngl = g_malloc0(sizeof(*ngl));
357 ngl->name = g_strdup(name);
358 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
359 return ngl;
360}
361
4a151677
PM
362void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
363 qemu_irq_handler handler,
364 void *opaque,
365 const char *name, int n)
a5f54290 366{
a69bef1c 367 int i;
a5f54290
PC
368 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
369
b235a71f 370 assert(gpio_list->num_out == 0 || !name);
a5f54290 371 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
4a151677 372 opaque, n);
a69bef1c 373
6c76b377
PF
374 if (!name) {
375 name = "unnamed-gpio-in";
376 }
a69bef1c 377 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
6c76b377
PF
378 gchar *propname = g_strdup_printf("%s[%u]", name, i);
379
a69bef1c
PC
380 object_property_add_child(OBJECT(dev), propname,
381 OBJECT(gpio_list->in[i]), &error_abort);
6c76b377 382 g_free(propname);
a69bef1c 383 }
a69bef1c 384
a5f54290
PC
385 gpio_list->num_in += n;
386}
387
aae9460e
PB
388void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
389{
a5f54290
PC
390 qdev_init_gpio_in_named(dev, handler, NULL, n);
391}
392
393void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
394 const char *name, int n)
395{
688b057a 396 int i;
a5f54290
PC
397 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
398
b235a71f 399 assert(gpio_list->num_in == 0 || !name);
688b057a 400
6c76b377
PF
401 if (!name) {
402 name = "unnamed-gpio-out";
403 }
404 memset(pins, 0, sizeof(*pins) * n);
688b057a 405 for (i = 0; i < n; ++i) {
6c76b377
PF
406 gchar *propname = g_strdup_printf("%s[%u]", name,
407 gpio_list->num_out + i);
408
688b057a
PC
409 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
410 (Object **)&pins[i],
411 object_property_allow_set_link,
265b578c 412 OBJ_PROP_LINK_STRONG,
688b057a 413 &error_abort);
6c76b377 414 g_free(propname);
688b057a 415 }
6c76b377 416 gpio_list->num_out += n;
aae9460e
PB
417}
418
419void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
420{
a5f54290
PC
421 qdev_init_gpio_out_named(dev, pins, NULL, n);
422}
423
424qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
425{
426 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
427
428 assert(n >= 0 && n < gpio_list->num_in);
429 return gpio_list->in[n];
aae9460e
PB
430}
431
432qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
433{
a5f54290
PC
434 return qdev_get_gpio_in_named(dev, NULL, n);
435}
436
437void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
438 qemu_irq pin)
439{
02757df2
PC
440 char *propname = g_strdup_printf("%s[%d]",
441 name ? name : "unnamed-gpio-out", n);
442 if (pin) {
443 /* We need a name for object_property_set_link to work. If the
444 * object has a parent, object_property_add_child will come back
445 * with an error without doing anything. If it has none, it will
446 * never fail. So we can just call it with a NULL Error pointer.
447 */
88950eef
AF
448 object_property_add_child(container_get(qdev_get_machine(),
449 "/unattached"),
450 "non-qdev-gpio[*]", OBJECT(pin), NULL);
02757df2
PC
451 }
452 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
453 g_free(propname);
aae9460e
PB
454}
455
b7973186
AG
456qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
457{
458 char *propname = g_strdup_printf("%s[%d]",
459 name ? name : "unnamed-gpio-out", n);
460
461 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
462 NULL);
463
464 return ret;
465}
466
67cc32eb 467/* disconnect a GPIO output, returning the disconnected input (if any) */
0c24db2b
PC
468
469static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
470 const char *name, int n)
471{
472 char *propname = g_strdup_printf("%s[%d]",
473 name ? name : "unnamed-gpio-out", n);
474
475 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
476 NULL);
477 if (ret) {
478 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
479 }
480 g_free(propname);
481 return ret;
482}
a5f54290 483
0c24db2b
PC
484qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
485 const char *name, int n)
486{
487 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
488 qdev_connect_gpio_out_named(dev, name, n, icpt);
489 return disconnected;
aae9460e
PB
490}
491
492void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
493{
a5f54290 494 qdev_connect_gpio_out_named(dev, NULL, n, pin);
aae9460e
PB
495}
496
17a96a14
PC
497void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
498 const char *name)
499{
500 int i;
501 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
502
503 for (i = 0; i < ngl->num_in; i++) {
504 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
505 char *propname = g_strdup_printf("%s[%d]", nm, i);
506
507 object_property_add_alias(OBJECT(container), propname,
508 OBJECT(dev), propname,
509 &error_abort);
6bc5cf92 510 g_free(propname);
17a96a14
PC
511 }
512 for (i = 0; i < ngl->num_out; i++) {
513 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
514 char *propname = g_strdup_printf("%s[%d]", nm, i);
515
516 object_property_add_alias(OBJECT(container), propname,
517 OBJECT(dev), propname,
518 &error_abort);
6bc5cf92 519 g_free(propname);
17a96a14
PC
520 }
521 QLIST_REMOVE(ngl, node);
522 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
523}
524
02e2da45 525BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
4d6ae674 526{
02e2da45 527 BusState *bus;
f698c8ba
PC
528 Object *child = object_resolve_path_component(OBJECT(dev), name);
529
530 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
531 if (bus) {
532 return bus;
533 }
4d6ae674 534
72cf2d4f 535 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
4d6ae674 536 if (strcmp(name, bus->name) == 0) {
02e2da45 537 return bus;
4d6ae674
PB
538 }
539 }
540 return NULL;
541}
542
0293214b
PB
543int qdev_walk_children(DeviceState *dev,
544 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
545 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
546 void *opaque)
81699d8a
AL
547{
548 BusState *bus;
549 int err;
550
0293214b
PB
551 if (pre_devfn) {
552 err = pre_devfn(dev, opaque);
81699d8a
AL
553 if (err) {
554 return err;
555 }
556 }
557
558 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
0293214b
PB
559 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
560 post_devfn, post_busfn, opaque);
81699d8a
AL
561 if (err < 0) {
562 return err;
563 }
564 }
565
0293214b
PB
566 if (post_devfn) {
567 err = post_devfn(dev, opaque);
568 if (err) {
569 return err;
570 }
571 }
572
81699d8a
AL
573 return 0;
574}
575
a2ee6b4f 576DeviceState *qdev_find_recursive(BusState *bus, const char *id)
3418bd25 577{
0866aca1
AL
578 BusChild *kid;
579 DeviceState *ret;
3418bd25
GH
580 BusState *child;
581
0866aca1
AL
582 QTAILQ_FOREACH(kid, &bus->children, sibling) {
583 DeviceState *dev = kid->child;
584
585 if (dev->id && strcmp(dev->id, id) == 0) {
3418bd25 586 return dev;
0866aca1
AL
587 }
588
3418bd25
GH
589 QLIST_FOREACH(child, &dev->child_bus, sibling) {
590 ret = qdev_find_recursive(child, id);
591 if (ret) {
592 return ret;
593 }
594 }
595 }
596 return NULL;
597}
598
09e5ab63 599char *qdev_get_dev_path(DeviceState *dev)
85ed303b 600{
0d936928 601 BusClass *bc;
09e5ab63
AL
602
603 if (!dev || !dev->parent_bus) {
604 return NULL;
605 }
606
0d936928
AL
607 bc = BUS_GET_CLASS(dev->parent_bus);
608 if (bc->get_dev_path) {
609 return bc->get_dev_path(dev);
09e5ab63
AL
610 }
611
612 return NULL;
44677ded 613}
a5296ca9
AL
614
615/**
616 * Legacy property handling
617 */
618
d7bce999
EB
619static void qdev_get_legacy_property(Object *obj, Visitor *v,
620 const char *name, void *opaque,
621 Error **errp)
a5296ca9 622{
57c9fafe 623 DeviceState *dev = DEVICE(obj);
a5296ca9
AL
624 Property *prop = opaque;
625
e3cb6ba6
PB
626 char buffer[1024];
627 char *ptr = buffer;
a5296ca9 628
e3cb6ba6 629 prop->info->print(dev, prop, buffer, sizeof(buffer));
51e72bc1 630 visit_type_str(v, name, &ptr, errp);
a5296ca9
AL
631}
632
a5296ca9 633/**
d9d8d452
C
634 * qdev_property_add_legacy:
635 * @dev: Device to add the property to.
636 * @prop: The qdev property definition.
637 * @errp: location to store error information.
638 *
639 * Add a legacy QOM property to @dev for qdev property @prop.
640 * On error, store error in @errp.
a5296ca9 641 *
d9d8d452
C
642 * Legacy properties are string versions of QOM properties. The format of
643 * the string depends on the property type. Legacy properties are only
644 * needed for "info qtree".
a5296ca9 645 *
6871a0d0 646 * Do not use this in new code! QOM Properties added through this interface
d9d8d452 647 * will be given names in the "legacy" namespace.
a5296ca9 648 */
f5a014d2
SW
649static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
650 Error **errp)
a5296ca9 651{
7ce7ffe0 652 gchar *name;
a5296ca9 653
f3be016d 654 /* Register pointer properties as legacy properties */
03ff7770 655 if (!prop->info->print && prop->info->get) {
68ee3569
PB
656 return;
657 }
f3be016d 658
faabdbb7
FZ
659 if (prop->info->create) {
660 return;
661 }
662
ca2cc788 663 name = g_strdup_printf("legacy-%s", prop->name);
7ce7ffe0 664 object_property_add(OBJECT(dev), name, "str",
68ee3569 665 prop->info->print ? qdev_get_legacy_property : prop->info->get,
03ff7770 666 NULL,
57c9fafe
AL
667 NULL,
668 prop, errp);
a5296ca9 669
ca2cc788
PB
670 g_free(name);
671}
672
673/**
d9d8d452
C
674 * qdev_property_add_static:
675 * @dev: Device to add the property to.
676 * @prop: The qdev property definition.
677 * @errp: location to store error information.
ca2cc788 678 *
d9d8d452
C
679 * Add a static QOM property to @dev for qdev property @prop.
680 * On error, store error in @errp. Static properties access data in a struct.
681 * The type of the QOM property is derived from prop->info.
ca2cc788
PB
682 */
683void qdev_property_add_static(DeviceState *dev, Property *prop,
684 Error **errp)
685{
fdae245f
PB
686 Error *local_err = NULL;
687 Object *obj = OBJECT(dev);
688
faabdbb7
FZ
689 if (prop->info->create) {
690 prop->info->create(obj, prop, &local_err);
691 } else {
692 /*
693 * TODO qdev_prop_ptr does not have getters or setters. It must
694 * go now that it can be replaced with links. The test should be
695 * removed along with it: all static properties are read/write.
696 */
697 if (!prop->info->get && !prop->info->set) {
698 return;
699 }
700 object_property_add(obj, prop->name, prop->info->name,
701 prop->info->get, prop->info->set,
702 prop->info->release,
703 prop, &local_err);
d822979b
PB
704 }
705
fdae245f
PB
706 if (local_err) {
707 error_propagate(errp, local_err);
708 return;
709 }
b8c9cd5c
GA
710
711 object_property_set_description(obj, prop->name,
712 prop->info->description,
713 &error_abort);
714
5cc56cc6 715 if (prop->set_default) {
a2740ad5 716 prop->info->set_default_value(obj, prop);
fdae245f 717 }
6a146eba 718}
1de81d28 719
67cc7e0a
SH
720/* @qdev_alias_all_properties - Add alias properties to the source object for
721 * all qdev properties on the target DeviceState.
722 */
723void qdev_alias_all_properties(DeviceState *target, Object *source)
724{
725 ObjectClass *class;
726 Property *prop;
727
728 class = object_get_class(OBJECT(target));
729 do {
730 DeviceClass *dc = DEVICE_CLASS(class);
731
732 for (prop = dc->props; prop && prop->name; prop++) {
733 object_property_add_alias(source, prop->name,
734 OBJECT(target), prop->name,
735 &error_abort);
736 }
737 class = object_class_get_parent(class);
738 } while (class != object_class_by_name(TYPE_DEVICE));
739}
740
4cae4d5a 741static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
66e56b13
ZG
742{
743 GSList **list = opaque;
09d56017
JL
744 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
745 TYPE_DEVICE);
746
747 if (dev == NULL) {
748 return 0;
749 }
66e56b13
ZG
750
751 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
752 *list = g_slist_append(*list, dev);
753 }
754
66e56b13
ZG
755 return 0;
756}
757
4cae4d5a
MA
758GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
759{
760 GSList *list = NULL;
761
762 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
763
764 return list;
765}
766
a7737e44 767static bool device_get_realized(Object *obj, Error **errp)
249d4172
AF
768{
769 DeviceState *dev = DEVICE(obj);
770 return dev->realized;
771}
772
1bfe5f05
JQ
773static bool check_only_migratable(Object *obj, Error **err)
774{
775 DeviceClass *dc = DEVICE_GET_CLASS(obj);
776
777 if (!vmstate_check_only_migratable(dc->vmsd)) {
778 error_setg(err, "Device %s is not migratable, but "
779 "--only-migratable was specified",
780 object_get_typename(obj));
781 return false;
782 }
783
784 return true;
785}
786
a7737e44 787static void device_set_realized(Object *obj, bool value, Error **errp)
249d4172
AF
788{
789 DeviceState *dev = DEVICE(obj);
790 DeviceClass *dc = DEVICE_GET_CLASS(dev);
7716b8ca 791 HotplugHandler *hotplug_ctrl;
5c21ce77 792 BusState *bus;
249d4172 793 Error *local_err = NULL;
69382d8b
IM
794 bool unattached_parent = false;
795 static int unattached_count;
249d4172 796
1a37eca1 797 if (dev->hotplugged && !dc->hotpluggable) {
c6bd8c70 798 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
1a37eca1
IM
799 return;
800 }
801
249d4172 802 if (value && !dev->realized) {
1bfe5f05 803 if (!check_only_migratable(obj, &local_err)) {
7562f907
AA
804 goto fail;
805 }
806
d578029e 807 if (!obj->parent) {
249d4172
AF
808 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
809
810 object_property_add_child(container_get(qdev_get_machine(),
811 "/unattached"),
d578029e 812 name, obj, &error_abort);
69382d8b 813 unattached_parent = true;
249d4172
AF
814 g_free(name);
815 }
816
41346263
IM
817 hotplug_ctrl = qdev_get_hotplug_handler(dev);
818 if (hotplug_ctrl) {
819 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
820 if (local_err != NULL) {
821 goto fail;
822 }
823 }
824
a7ddba52
IM
825 if (dc->realize) {
826 dc->realize(dev, &local_err);
827 }
828
1d45a705
GA
829 if (local_err != NULL) {
830 goto fail;
831 }
832
707ff800
PD
833 DEVICE_LISTENER_CALL(realize, Forward, dev);
834
7716b8ca
IM
835 if (hotplug_ctrl) {
836 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
5e954943
IM
837 }
838
1d45a705
GA
839 if (local_err != NULL) {
840 goto post_realize_fail;
841 }
842
04162f8f
MR
843 /*
844 * always free/re-initialize here since the value cannot be cleaned up
845 * in device_unrealize due to its usage later on in the unplug path
846 */
847 g_free(dev->canonical_path);
848 dev->canonical_path = object_get_canonical_path(OBJECT(dev));
849
1d45a705 850 if (qdev_get_vmsd(dev)) {
67980031
DDAG
851 if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
852 dev->instance_id_alias,
853 dev->alias_required_for_version,
854 &local_err) < 0) {
855 goto post_realize_fail;
856 }
249d4172 857 }
1d45a705
GA
858
859 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
860 object_property_set_bool(OBJECT(bus), true, "realized",
5c21ce77 861 &local_err);
1d45a705
GA
862 if (local_err != NULL) {
863 goto child_realize_fail;
5c21ce77
BD
864 }
865 }
1d45a705 866 if (dev->hotplugged) {
249d4172
AF
867 device_reset(dev);
868 }
352e8da7 869 dev->pending_deleted_event = false;
25e89788
SH
870
871 if (hotplug_ctrl) {
872 hotplug_handler_post_plug(hotplug_ctrl, dev);
873 }
249d4172 874 } else if (!value && dev->realized) {
cd4520ad 875 Error **local_errp = NULL;
5c21ce77 876 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
cd4520ad 877 local_errp = local_err ? NULL : &local_err;
5c21ce77 878 object_property_set_bool(OBJECT(bus), false, "realized",
cd4520ad 879 local_errp);
5c21ce77 880 }
cd4520ad 881 if (qdev_get_vmsd(dev)) {
fe6c2117
AF
882 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
883 }
cd4520ad
GA
884 if (dc->unrealize) {
885 local_errp = local_err ? NULL : &local_err;
886 dc->unrealize(dev, local_errp);
249d4172 887 }
352e8da7 888 dev->pending_deleted_event = true;
707ff800 889 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
c7f8d0f3 890 }
249d4172 891
c7f8d0f3
XG
892 if (local_err != NULL) {
893 goto fail;
249d4172
AF
894 }
895
c7f8d0f3 896 dev->realized = value;
1d45a705
GA
897 return;
898
899child_realize_fail:
900 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
901 object_property_set_bool(OBJECT(bus), false, "realized",
902 NULL);
903 }
904
905 if (qdev_get_vmsd(dev)) {
906 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
907 }
908
909post_realize_fail:
04162f8f
MR
910 g_free(dev->canonical_path);
911 dev->canonical_path = NULL;
1d45a705
GA
912 if (dc->unrealize) {
913 dc->unrealize(dev, NULL);
914 }
915
916fail:
917 error_propagate(errp, local_err);
69382d8b
IM
918 if (unattached_parent) {
919 object_unparent(OBJECT(dev));
920 unattached_count--;
921 }
249d4172
AF
922}
923
a7737e44 924static bool device_get_hotpluggable(Object *obj, Error **errp)
1a37eca1
IM
925{
926 DeviceClass *dc = DEVICE_GET_CLASS(obj);
927 DeviceState *dev = DEVICE(obj);
928
2b81b35f 929 return dc->hotpluggable && (dev->parent_bus == NULL ||
39b888bd 930 qbus_is_hotpluggable(dev->parent_bus));
1a37eca1
IM
931}
932
d012ffc1
IM
933static bool device_get_hotplugged(Object *obj, Error **err)
934{
935 DeviceState *dev = DEVICE(obj);
936
937 return dev->hotplugged;
938}
939
9674bfe4
AL
940static void device_initfn(Object *obj)
941{
942 DeviceState *dev = DEVICE(obj);
bce54474 943 ObjectClass *class;
9674bfe4
AL
944 Property *prop;
945
946 if (qdev_hotplug) {
947 dev->hotplugged = 1;
948 qdev_hot_added = true;
949 }
950
951 dev->instance_id_alias = -1;
7983c8a3 952 dev->realized = false;
9674bfe4 953
249d4172
AF
954 object_property_add_bool(obj, "realized",
955 device_get_realized, device_set_realized, NULL);
1a37eca1
IM
956 object_property_add_bool(obj, "hotpluggable",
957 device_get_hotpluggable, NULL, NULL);
d012ffc1 958 object_property_add_bool(obj, "hotplugged",
36cccb8c 959 device_get_hotplugged, NULL,
d012ffc1 960 &error_abort);
249d4172 961
bce54474
PB
962 class = object_get_class(OBJECT(dev));
963 do {
964 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
5433a0a8
PC
965 qdev_property_add_legacy(dev, prop, &error_abort);
966 qdev_property_add_static(dev, prop, &error_abort);
bce54474 967 }
bce54474
PB
968 class = object_class_get_parent(class);
969 } while (class != object_class_by_name(TYPE_DEVICE));
9674bfe4 970
f968fc68 971 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
39f72ef9 972 (Object **)&dev->parent_bus, NULL, 0,
9561fda8 973 &error_abort);
a5f54290 974 QLIST_INIT(&dev->gpios);
9674bfe4
AL
975}
976
99a0b036
EH
977static void device_post_init(Object *obj)
978{
25f8dd96 979 qdev_prop_set_globals(DEVICE(obj));
99a0b036
EH
980}
981
60adba37
AL
982/* Unlink device from bus and free the structure. */
983static void device_finalize(Object *obj)
984{
a5f54290
PC
985 NamedGPIOList *ngl, *next;
986
60adba37 987 DeviceState *dev = DEVICE(obj);
a5f54290
PC
988
989 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
990 QLIST_REMOVE(ngl, node);
f173d57a 991 qemu_free_irqs(ngl->in, ngl->num_in);
a5f54290
PC
992 g_free(ngl->name);
993 g_free(ngl);
994 /* ngl->out irqs are owned by the other end and should not be freed
995 * here
996 */
997 }
f7b879e0
MR
998
999 /* Only send event if the device had been completely realized */
1000 if (dev->pending_deleted_event) {
1001 g_assert(dev->canonical_path);
1002
3ab72385 1003 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
f7b879e0
MR
1004 g_free(dev->canonical_path);
1005 dev->canonical_path = NULL;
1006 }
1007
1008 qemu_opts_del(dev->opts);
60adba37
AL
1009}
1010
bce54474
PB
1011static void device_class_base_init(ObjectClass *class, void *data)
1012{
1013 DeviceClass *klass = DEVICE_CLASS(class);
1014
1015 /* We explicitly look up properties in the superclasses,
1016 * so do not propagate them to the subclasses.
1017 */
1018 klass->props = NULL;
60adba37
AL
1019}
1020
5d5b24d0 1021static void device_unparent(Object *obj)
667d22d1
PB
1022{
1023 DeviceState *dev = DEVICE(obj);
06f7f2bb 1024 BusState *bus;
667d22d1 1025
5c21ce77
BD
1026 if (dev->realized) {
1027 object_property_set_bool(obj, false, "realized", NULL);
1028 }
06f7f2bb
PB
1029 while (dev->num_child_bus) {
1030 bus = QLIST_FIRST(&dev->child_bus);
6780a22c 1031 object_unparent(OBJECT(bus));
06f7f2bb 1032 }
06f7f2bb 1033 if (dev->parent_bus) {
5d5b24d0 1034 bus_remove_child(dev->parent_bus, dev);
62d7ba66
PB
1035 object_unref(OBJECT(dev->parent_bus));
1036 dev->parent_bus = NULL;
5d5b24d0 1037 }
667d22d1
PB
1038}
1039
1040static void device_class_init(ObjectClass *class, void *data)
1041{
249d4172
AF
1042 DeviceClass *dc = DEVICE_CLASS(class);
1043
5d5b24d0 1044 class->unparent = device_unparent;
267a3264
IM
1045
1046 /* by default all devices were considered as hotpluggable,
1047 * so with intent to check it in generic qdev_unplug() /
1048 * device_set_realized() functions make every device
1049 * hotpluggable. Devices that shouldn't be hotpluggable,
1050 * should override it in their class_init()
1051 */
1052 dc->hotpluggable = true;
e90f2a8c 1053 dc->user_creatable = true;
667d22d1
PB
1054}
1055
46795cf2
PMD
1056void device_class_set_parent_reset(DeviceClass *dc,
1057 DeviceReset dev_reset,
1058 DeviceReset *parent_reset)
1059{
1060 *parent_reset = dc->reset;
1061 dc->reset = dev_reset;
1062}
1063
1064void device_class_set_parent_realize(DeviceClass *dc,
1065 DeviceRealize dev_realize,
1066 DeviceRealize *parent_realize)
1067{
1068 *parent_realize = dc->realize;
1069 dc->realize = dev_realize;
1070}
1071
1072void device_class_set_parent_unrealize(DeviceClass *dc,
1073 DeviceUnrealize dev_unrealize,
1074 DeviceUnrealize *parent_unrealize)
1075{
1076 *parent_unrealize = dc->unrealize;
1077 dc->unrealize = dev_unrealize;
1078}
1079
94afdadc
AL
1080void device_reset(DeviceState *dev)
1081{
1082 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1083
1084 if (klass->reset) {
1085 klass->reset(dev);
1086 }
1087}
1088
f05f6b4a
PB
1089Object *qdev_get_machine(void)
1090{
1091 static Object *dev;
1092
1093 if (dev == NULL) {
dfe47e70 1094 dev = container_get(object_get_root(), "/machine");
f05f6b4a
PB
1095 }
1096
1097 return dev;
1098}
1099
8c43a6f0 1100static const TypeInfo device_type_info = {
32fea402
AL
1101 .name = TYPE_DEVICE,
1102 .parent = TYPE_OBJECT,
1103 .instance_size = sizeof(DeviceState),
9674bfe4 1104 .instance_init = device_initfn,
99a0b036 1105 .instance_post_init = device_post_init,
60adba37 1106 .instance_finalize = device_finalize,
bce54474 1107 .class_base_init = device_class_base_init,
667d22d1 1108 .class_init = device_class_init,
32fea402
AL
1109 .abstract = true,
1110 .class_size = sizeof(DeviceClass),
1111};
1112
83f7d43a 1113static void qdev_register_types(void)
32fea402
AL
1114{
1115 type_register_static(&device_type_info);
1116}
1117
83f7d43a 1118type_init(qdev_register_types)