]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/qdev.c
qga: Make qemu-ga compile statically for Windows
[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"
6b1566cb 30#include "hw/fw-path-provider.h"
9c17d615 31#include "sysemu/sysemu.h"
b4a42f81 32#include "qapi/qmp/qerror.h"
7b1b5d19 33#include "qapi/visitor.h"
0402a5d6 34#include "qapi/qmp/qjson.h"
d49b6836 35#include "qemu/error-report.h"
0ee4de6c 36#include "hw/hotplug.h"
b7454548 37#include "hw/boards.h"
7474f1be 38#include "hw/sysbus.h"
24b699fb 39#include "qapi-event.h"
7562f907 40#include "migration/migration.h"
aae9460e 41
ee46d8a5 42int qdev_hotplug = 0;
0ac8ef71
AW
43static bool qdev_hot_added = false;
44static bool qdev_hot_removed = false;
3418bd25 45
4be9f0d1
AL
46const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
47{
6e008585
AL
48 DeviceClass *dc = DEVICE_GET_CLASS(dev);
49 return dc->vmsd;
4be9f0d1
AL
50}
51
4be9f0d1
AL
52const char *qdev_fw_name(DeviceState *dev)
53{
6e008585 54 DeviceClass *dc = DEVICE_GET_CLASS(dev);
4be9f0d1 55
6e008585
AL
56 if (dc->fw_name) {
57 return dc->fw_name;
4be9f0d1
AL
58 }
59
60 return object_get_typename(OBJECT(dev));
61}
62
0866aca1 63static void bus_remove_child(BusState *bus, DeviceState *child)
0c17542d 64{
0866aca1
AL
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);
9d127820
PB
73
74 /* This gives back ownership of kid->child back to us. */
0866aca1 75 object_property_del(OBJECT(bus), name, NULL);
9d127820 76 object_unref(OBJECT(kid->child));
0866aca1
AL
77 g_free(kid);
78 return;
79 }
80 }
81}
82
83static void bus_add_child(BusState *bus, DeviceState *child)
84{
85 char name[32];
86 BusChild *kid = g_malloc0(sizeof(*kid));
0c17542d 87
0866aca1
AL
88 kid->index = bus->max_index++;
89 kid->child = child;
9d127820 90 object_ref(OBJECT(kid->child));
a5296ca9 91
0866aca1
AL
92 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
93
9d127820 94 /* This transfers ownership of kid->child to the property. */
0866aca1
AL
95 snprintf(name, sizeof(name), "child[%d]", kid->index);
96 object_property_add_link(OBJECT(bus), name,
97 object_get_typename(OBJECT(child)),
39f72ef9
SH
98 (Object **)&kid->child,
99 NULL, /* read-only property */
100 0, /* return ownership on prop deletion */
101 NULL);
0866aca1
AL
102}
103
104void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
105{
91c968ac
PM
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 }
9fbe6127 117 dev->parent_bus = bus;
62d7ba66 118 object_ref(OBJECT(bus));
0866aca1 119 bus_add_child(bus, dev);
91c968ac
PM
120 if (replugging) {
121 object_unref(OBJECT(dev));
122 }
0c17542d
MA
123}
124
0210afe6
MA
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. */
02e2da45 128DeviceState *qdev_create(BusState *bus, const char *name)
0bcdeda7
BS
129{
130 DeviceState *dev;
131
132 dev = qdev_try_create(bus, name);
133 if (!dev) {
e92714c7 134 if (bus) {
312fd5f2 135 error_report("Unknown device '%s' for bus '%s'", name,
23e3fbec 136 object_get_typename(OBJECT(bus)));
e92714c7 137 } else {
312fd5f2 138 error_report("Unknown device '%s' for default sysbus", name);
e92714c7 139 }
01ed1d52 140 abort();
0bcdeda7
BS
141 }
142
143 return dev;
144}
145
da57febf 146DeviceState *qdev_try_create(BusState *bus, const char *type)
aae9460e 147{
9fbe6127
AL
148 DeviceState *dev;
149
da57febf 150 if (object_class_by_name(type) == NULL) {
4ed658ca
AF
151 return NULL;
152 }
da57febf 153 dev = DEVICE(object_new(type));
9fbe6127
AL
154 if (!dev) {
155 return NULL;
156 }
157
10c4c98a 158 if (!bus) {
7474f1be
PM
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));
68694897 165 bus = sysbus_get_default();
10c4c98a
GH
166 }
167
9fbe6127 168 qdev_set_parent_bus(dev, bus);
b09995ae 169 object_unref(OBJECT(dev));
9fbe6127 170 return dev;
aae9460e
PB
171}
172
707ff800
PD
173static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
174 = QTAILQ_HEAD_INITIALIZER(device_listeners);
175
176enum 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
203static int device_listener_add(DeviceState *dev, void *opaque)
204{
205 DEVICE_LISTENER_CALL(realize, Forward, dev);
206
207 return 0;
208}
209
210void 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
218void device_listener_unregister(DeviceListener *listener)
219{
220 QTAILQ_REMOVE(&device_listeners, listener, link);
221}
222
a7737e44 223static void device_realize(DeviceState *dev, Error **errp)
249d4172
AF
224{
225 DeviceClass *dc = DEVICE_GET_CLASS(dev);
da57febf 226
249d4172
AF
227 if (dc->init) {
228 int rc = dc->init(dev);
229 if (rc < 0) {
a7737e44 230 error_setg(errp, "Device initialization failed.");
249d4172
AF
231 return;
232 }
5ab28c83 233 }
02e2da45
PB
234}
235
fe6c2117
AF
236static 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
4d2ffa08
JK
249void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
250 int required_for_version)
251{
7983c8a3 252 assert(!dev->realized);
4d2ffa08
JK
253 dev->instance_id_alias = alias_id;
254 dev->alias_required_for_version = required_for_version;
255}
256
c06b2ffb 257HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
7716b8ca
IM
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
56f9107e 274void qdev_unplug(DeviceState *dev, Error **errp)
3418bd25 275{
6e008585 276 DeviceClass *dc = DEVICE_GET_CLASS(dev);
7716b8ca
IM
277 HotplugHandler *hotplug_ctrl;
278 HotplugHandlerClass *hdc;
6e008585 279
39b888bd 280 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
c6bd8c70 281 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
56f9107e 282 return;
3418bd25 283 }
593831de 284
1a37eca1 285 if (!dc->hotpluggable) {
c6bd8c70
MA
286 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
287 object_get_typename(OBJECT(dev)));
1a37eca1
IM
288 return;
289 }
290
0ac8ef71
AW
291 qdev_hot_removed = true;
292
7716b8ca
IM
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);
5e954943 303 } else {
7716b8ca 304 hotplug_handler_unplug(hotplug_ctrl, dev, errp);
56f9107e 305 }
3418bd25
GH
306}
307
ec990eb6
AL
308static int qdev_reset_one(DeviceState *dev, void *opaque)
309{
94afdadc 310 device_reset(dev);
ec990eb6
AL
311
312 return 0;
313}
314
b4694b7c
IY
315static int qbus_reset_one(BusState *bus, void *opaque)
316{
0d936928
AL
317 BusClass *bc = BUS_GET_CLASS(bus);
318 if (bc->reset) {
dcc20931 319 bc->reset(bus);
b4694b7c
IY
320 }
321 return 0;
322}
323
5af0a04b
IY
324void qdev_reset_all(DeviceState *dev)
325{
dcc20931 326 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
5af0a04b
IY
327}
328
ff8de075
DH
329void qdev_reset_all_fn(void *opaque)
330{
331 qdev_reset_all(DEVICE(opaque));
332}
333
d0508c36
PB
334void qbus_reset_all(BusState *bus)
335{
dcc20931 336 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
d0508c36
PB
337}
338
80376c3f
IY
339void qbus_reset_all_fn(void *opaque)
340{
341 BusState *bus = opaque;
d0508c36 342 qbus_reset_all(bus);
80376c3f
IY
343}
344
3418bd25 345/* can be used as ->unplug() callback for the simple cases */
014176f9
IM
346void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
347 DeviceState *dev, Error **errp)
348{
2d9a982f
IM
349 /* just zap it */
350 object_unparent(OBJECT(dev));
014176f9 351}
3b29a101 352
0210afe6
MA
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 */
e23a1b33
MA
366void qdev_init_nofail(DeviceState *dev)
367{
c4bacafb 368 Error *err = NULL;
7de3abe5 369
c4bacafb
MA
370 assert(!dev->realized);
371
0d4104e5 372 object_ref(OBJECT(dev));
c4bacafb
MA
373 object_property_set_bool(OBJECT(dev), true, "realized", &err);
374 if (err) {
c29b77f9
MA
375 error_reportf_err(err, "Initialization of device %s failed: ",
376 object_get_typename(OBJECT(dev)));
bd6c9a61
MA
377 exit(1);
378 }
0d4104e5 379 object_unref(OBJECT(dev));
e23a1b33
MA
380}
381
3418bd25
GH
382void 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
0ac8ef71
AW
391bool qdev_machine_modified(void)
392{
393 return qdev_hot_added || qdev_hot_removed;
394}
395
02e2da45 396BusState *qdev_get_parent_bus(DeviceState *dev)
aae9460e 397{
02e2da45 398 return dev->parent_bus;
aae9460e
PB
399}
400
a5f54290
PC
401static 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
422void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
423 const char *name, int n)
424{
a69bef1c 425 int i;
a5f54290
PC
426 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
427
b235a71f 428 assert(gpio_list->num_out == 0 || !name);
a5f54290
PC
429 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
430 dev, n);
a69bef1c 431
6c76b377
PF
432 if (!name) {
433 name = "unnamed-gpio-in";
434 }
a69bef1c 435 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
6c76b377
PF
436 gchar *propname = g_strdup_printf("%s[%u]", name, i);
437
a69bef1c
PC
438 object_property_add_child(OBJECT(dev), propname,
439 OBJECT(gpio_list->in[i]), &error_abort);
6c76b377 440 g_free(propname);
a69bef1c 441 }
a69bef1c 442
a5f54290
PC
443 gpio_list->num_in += n;
444}
445
aae9460e
PB
446void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
447{
a5f54290
PC
448 qdev_init_gpio_in_named(dev, handler, NULL, n);
449}
450
451void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
452 const char *name, int n)
453{
688b057a 454 int i;
a5f54290
PC
455 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
456
b235a71f 457 assert(gpio_list->num_in == 0 || !name);
688b057a 458
6c76b377
PF
459 if (!name) {
460 name = "unnamed-gpio-out";
461 }
462 memset(pins, 0, sizeof(*pins) * n);
688b057a 463 for (i = 0; i < n; ++i) {
6c76b377
PF
464 gchar *propname = g_strdup_printf("%s[%u]", name,
465 gpio_list->num_out + i);
466
688b057a
PC
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);
6c76b377 472 g_free(propname);
688b057a 473 }
6c76b377 474 gpio_list->num_out += n;
aae9460e
PB
475}
476
477void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
478{
a5f54290
PC
479 qdev_init_gpio_out_named(dev, pins, NULL, n);
480}
481
482qemu_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];
aae9460e
PB
488}
489
490qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
491{
a5f54290
PC
492 return qdev_get_gpio_in_named(dev, NULL, n);
493}
494
495void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
496 qemu_irq pin)
497{
02757df2
PC
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 */
88950eef
AF
506 object_property_add_child(container_get(qdev_get_machine(),
507 "/unattached"),
508 "non-qdev-gpio[*]", OBJECT(pin), NULL);
02757df2
PC
509 }
510 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
511 g_free(propname);
aae9460e
PB
512}
513
b7973186
AG
514qemu_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
67cc32eb 525/* disconnect a GPIO output, returning the disconnected input (if any) */
0c24db2b
PC
526
527static 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}
a5f54290 541
0c24db2b
PC
542qemu_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;
aae9460e
PB
548}
549
550void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
551{
a5f54290 552 qdev_connect_gpio_out_named(dev, NULL, n, pin);
aae9460e
PB
553}
554
17a96a14
PC
555void 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);
6bc5cf92 568 g_free(propname);
17a96a14
PC
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);
6bc5cf92 577 g_free(propname);
17a96a14
PC
578 }
579 QLIST_REMOVE(ngl, node);
580 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
581}
582
02e2da45 583BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
4d6ae674 584{
02e2da45 585 BusState *bus;
f698c8ba
PC
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 }
4d6ae674 592
72cf2d4f 593 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
4d6ae674 594 if (strcmp(name, bus->name) == 0) {
02e2da45 595 return bus;
4d6ae674
PB
596 }
597 }
598 return NULL;
599}
600
0293214b
PB
601int 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)
81699d8a
AL
605{
606 BusState *bus;
607 int err;
608
0293214b
PB
609 if (pre_devfn) {
610 err = pre_devfn(dev, opaque);
81699d8a
AL
611 if (err) {
612 return err;
613 }
614 }
615
616 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
0293214b
PB
617 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
618 post_devfn, post_busfn, opaque);
81699d8a
AL
619 if (err < 0) {
620 return err;
621 }
622 }
623
0293214b
PB
624 if (post_devfn) {
625 err = post_devfn(dev, opaque);
626 if (err) {
627 return err;
628 }
629 }
630
81699d8a
AL
631 return 0;
632}
633
a2ee6b4f 634DeviceState *qdev_find_recursive(BusState *bus, const char *id)
3418bd25 635{
0866aca1
AL
636 BusChild *kid;
637 DeviceState *ret;
3418bd25
GH
638 BusState *child;
639
0866aca1
AL
640 QTAILQ_FOREACH(kid, &bus->children, sibling) {
641 DeviceState *dev = kid->child;
642
643 if (dev->id && strcmp(dev->id, id) == 0) {
3418bd25 644 return dev;
0866aca1
AL
645 }
646
3418bd25
GH
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
0d936928
AL
657static 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);
131ec1bd 663 }
0d936928
AL
664
665 return NULL;
131ec1bd
GH
666}
667
6b1566cb
PB
668static 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
0be63901
GA
680char *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
1ca4d09a
GN
687static 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);
6b1566cb
PB
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 }
0d936928 698 if (d) {
1ca4d09a 699 l += snprintf(p + l, size - l, "%s", d);
7267c094 700 g_free(d);
1ca4d09a 701 } else {
bbfa18fc 702 return l;
1ca4d09a
GN
703 }
704 }
705 l += snprintf(p + l , size - l, "/");
706
707 return l;
708}
709
710char* 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
a5cf8262 719 return g_strdup(path);
1ca4d09a 720}
85ed303b 721
09e5ab63 722char *qdev_get_dev_path(DeviceState *dev)
85ed303b 723{
0d936928 724 BusClass *bc;
09e5ab63
AL
725
726 if (!dev || !dev->parent_bus) {
727 return NULL;
728 }
729
0d936928
AL
730 bc = BUS_GET_CLASS(dev->parent_bus);
731 if (bc->get_dev_path) {
732 return bc->get_dev_path(dev);
09e5ab63
AL
733 }
734
735 return NULL;
44677ded 736}
a5296ca9
AL
737
738/**
739 * Legacy property handling
740 */
741
d7bce999
EB
742static void qdev_get_legacy_property(Object *obj, Visitor *v,
743 const char *name, void *opaque,
744 Error **errp)
a5296ca9 745{
57c9fafe 746 DeviceState *dev = DEVICE(obj);
a5296ca9
AL
747 Property *prop = opaque;
748
e3cb6ba6
PB
749 char buffer[1024];
750 char *ptr = buffer;
a5296ca9 751
e3cb6ba6 752 prop->info->print(dev, prop, buffer, sizeof(buffer));
51e72bc1 753 visit_type_str(v, name, &ptr, errp);
a5296ca9
AL
754}
755
a5296ca9 756/**
d9d8d452
C
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.
a5296ca9 764 *
d9d8d452
C
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".
a5296ca9 768 *
d9d8d452
C
769 * Do not use this is new code! QOM Properties added through this interface
770 * will be given names in the "legacy" namespace.
a5296ca9 771 */
f5a014d2
SW
772static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
773 Error **errp)
a5296ca9 774{
7ce7ffe0 775 gchar *name;
a5296ca9 776
f3be016d 777 /* Register pointer properties as legacy properties */
03ff7770 778 if (!prop->info->print && prop->info->get) {
68ee3569
PB
779 return;
780 }
f3be016d 781
ca2cc788 782 name = g_strdup_printf("legacy-%s", prop->name);
7ce7ffe0 783 object_property_add(OBJECT(dev), name, "str",
68ee3569 784 prop->info->print ? qdev_get_legacy_property : prop->info->get,
03ff7770 785 NULL,
57c9fafe
AL
786 NULL,
787 prop, errp);
a5296ca9 788
ca2cc788
PB
789 g_free(name);
790}
791
792/**
d9d8d452
C
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.
ca2cc788 797 *
d9d8d452
C
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.
ca2cc788
PB
801 */
802void qdev_property_add_static(DeviceState *dev, Property *prop,
803 Error **errp)
804{
fdae245f
PB
805 Error *local_err = NULL;
806 Object *obj = OBJECT(dev);
807
d822979b
PB
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
fdae245f 817 object_property_add(obj, prop->name, prop->info->name,
57c9fafe 818 prop->info->get, prop->info->set,
dd0ba250 819 prop->info->release,
fdae245f
PB
820 prop, &local_err);
821
822 if (local_err) {
823 error_propagate(errp, local_err);
824 return;
825 }
b8c9cd5c
GA
826
827 object_property_set_description(obj, prop->name,
828 prop->info->description,
829 &error_abort);
830
fdae245f
PB
831 if (prop->qtype == QTYPE_NONE) {
832 return;
833 }
834
835 if (prop->qtype == QTYPE_QBOOL) {
5433a0a8 836 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
fdae245f
PB
837 } else if (prop->info->enum_table) {
838 object_property_set_str(obj, prop->info->enum_table[prop->defval],
5433a0a8 839 prop->name, &error_abort);
fdae245f 840 } else if (prop->qtype == QTYPE_QINT) {
5433a0a8 841 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
fdae245f 842 }
6a146eba 843}
1de81d28 844
67cc7e0a
SH
845/* @qdev_alias_all_properties - Add alias properties to the source object for
846 * all qdev properties on the target DeviceState.
847 */
848void 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
4cae4d5a 866static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
66e56b13
ZG
867{
868 GSList **list = opaque;
09d56017
JL
869 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
870 TYPE_DEVICE);
871
872 if (dev == NULL) {
873 return 0;
874 }
66e56b13
ZG
875
876 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
877 *list = g_slist_append(*list, dev);
878 }
879
66e56b13
ZG
880 return 0;
881}
882
4cae4d5a
MA
883GSList *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
a7737e44 892static bool device_get_realized(Object *obj, Error **errp)
249d4172
AF
893{
894 DeviceState *dev = DEVICE(obj);
895 return dev->realized;
896}
897
a7737e44 898static void device_set_realized(Object *obj, bool value, Error **errp)
249d4172
AF
899{
900 DeviceState *dev = DEVICE(obj);
901 DeviceClass *dc = DEVICE_GET_CLASS(dev);
7716b8ca 902 HotplugHandler *hotplug_ctrl;
5c21ce77 903 BusState *bus;
249d4172 904 Error *local_err = NULL;
69382d8b
IM
905 bool unattached_parent = false;
906 static int unattached_count;
7562f907 907 int ret;
249d4172 908
1a37eca1 909 if (dev->hotplugged && !dc->hotpluggable) {
c6bd8c70 910 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
1a37eca1
IM
911 return;
912 }
913
249d4172 914 if (value && !dev->realized) {
7562f907
AA
915 ret = check_migratable(obj, &local_err);
916 if (ret < 0) {
917 goto fail;
918 }
919
d578029e 920 if (!obj->parent) {
249d4172
AF
921 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
922
923 object_property_add_child(container_get(qdev_get_machine(),
924 "/unattached"),
d578029e 925 name, obj, &error_abort);
69382d8b 926 unattached_parent = true;
249d4172
AF
927 g_free(name);
928 }
929
41346263
IM
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
a7ddba52
IM
938 if (dc->realize) {
939 dc->realize(dev, &local_err);
940 }
941
1d45a705
GA
942 if (local_err != NULL) {
943 goto fail;
944 }
945
707ff800
PD
946 DEVICE_LISTENER_CALL(realize, Forward, dev);
947
7716b8ca
IM
948 if (hotplug_ctrl) {
949 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
5e954943
IM
950 }
951
1d45a705
GA
952 if (local_err != NULL) {
953 goto post_realize_fail;
954 }
955
956 if (qdev_get_vmsd(dev)) {
67980031
DDAG
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 }
249d4172 963 }
1d45a705
GA
964
965 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
966 object_property_set_bool(OBJECT(bus), true, "realized",
5c21ce77 967 &local_err);
1d45a705
GA
968 if (local_err != NULL) {
969 goto child_realize_fail;
5c21ce77
BD
970 }
971 }
1d45a705 972 if (dev->hotplugged) {
249d4172
AF
973 device_reset(dev);
974 }
352e8da7 975 dev->pending_deleted_event = false;
249d4172 976 } else if (!value && dev->realized) {
cd4520ad 977 Error **local_errp = NULL;
5c21ce77 978 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
cd4520ad 979 local_errp = local_err ? NULL : &local_err;
5c21ce77 980 object_property_set_bool(OBJECT(bus), false, "realized",
cd4520ad 981 local_errp);
5c21ce77 982 }
cd4520ad 983 if (qdev_get_vmsd(dev)) {
fe6c2117
AF
984 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
985 }
cd4520ad
GA
986 if (dc->unrealize) {
987 local_errp = local_err ? NULL : &local_err;
988 dc->unrealize(dev, local_errp);
249d4172 989 }
352e8da7 990 dev->pending_deleted_event = true;
707ff800 991 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
c7f8d0f3 992 }
249d4172 993
c7f8d0f3
XG
994 if (local_err != NULL) {
995 goto fail;
249d4172
AF
996 }
997
c7f8d0f3 998 dev->realized = value;
1d45a705
GA
999 return;
1000
1001child_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
1011post_realize_fail:
1012 if (dc->unrealize) {
1013 dc->unrealize(dev, NULL);
1014 }
1015
1016fail:
1017 error_propagate(errp, local_err);
69382d8b
IM
1018 if (unattached_parent) {
1019 object_unparent(OBJECT(dev));
1020 unattached_count--;
1021 }
249d4172
AF
1022}
1023
a7737e44 1024static bool device_get_hotpluggable(Object *obj, Error **errp)
1a37eca1
IM
1025{
1026 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1027 DeviceState *dev = DEVICE(obj);
1028
2b81b35f 1029 return dc->hotpluggable && (dev->parent_bus == NULL ||
39b888bd 1030 qbus_is_hotpluggable(dev->parent_bus));
1a37eca1
IM
1031}
1032
d012ffc1
IM
1033static bool device_get_hotplugged(Object *obj, Error **err)
1034{
1035 DeviceState *dev = DEVICE(obj);
1036
1037 return dev->hotplugged;
1038}
1039
1040static void device_set_hotplugged(Object *obj, bool value, Error **err)
1041{
1042 DeviceState *dev = DEVICE(obj);
1043
1044 dev->hotplugged = value;
1045}
1046
9674bfe4
AL
1047static void device_initfn(Object *obj)
1048{
1049 DeviceState *dev = DEVICE(obj);
bce54474 1050 ObjectClass *class;
9674bfe4
AL
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;
7983c8a3 1059 dev->realized = false;
9674bfe4 1060
249d4172
AF
1061 object_property_add_bool(obj, "realized",
1062 device_get_realized, device_set_realized, NULL);
1a37eca1
IM
1063 object_property_add_bool(obj, "hotpluggable",
1064 device_get_hotpluggable, NULL, NULL);
d012ffc1
IM
1065 object_property_add_bool(obj, "hotplugged",
1066 device_get_hotplugged, device_set_hotplugged,
1067 &error_abort);
249d4172 1068
bce54474
PB
1069 class = object_get_class(OBJECT(dev));
1070 do {
1071 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
5433a0a8
PC
1072 qdev_property_add_legacy(dev, prop, &error_abort);
1073 qdev_property_add_static(dev, prop, &error_abort);
bce54474 1074 }
bce54474
PB
1075 class = object_class_get_parent(class);
1076 } while (class != object_class_by_name(TYPE_DEVICE));
9674bfe4 1077
f968fc68 1078 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
39f72ef9 1079 (Object **)&dev->parent_bus, NULL, 0,
9561fda8 1080 &error_abort);
a5f54290 1081 QLIST_INIT(&dev->gpios);
9674bfe4
AL
1082}
1083
99a0b036
EH
1084static void device_post_init(Object *obj)
1085{
25f8dd96 1086 qdev_prop_set_globals(DEVICE(obj));
99a0b036
EH
1087}
1088
60adba37
AL
1089/* Unlink device from bus and free the structure. */
1090static void device_finalize(Object *obj)
1091{
a5f54290
PC
1092 NamedGPIOList *ngl, *next;
1093
60adba37 1094 DeviceState *dev = DEVICE(obj);
a5f54290
PC
1095
1096 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1097 QLIST_REMOVE(ngl, node);
f173d57a 1098 qemu_free_irqs(ngl->in, ngl->num_in);
a5f54290
PC
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 }
60adba37
AL
1105}
1106
bce54474
PB
1107static 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;
60adba37
AL
1115}
1116
5d5b24d0 1117static void device_unparent(Object *obj)
667d22d1
PB
1118{
1119 DeviceState *dev = DEVICE(obj);
06f7f2bb 1120 BusState *bus;
667d22d1 1121
5c21ce77
BD
1122 if (dev->realized) {
1123 object_property_set_bool(obj, false, "realized", NULL);
1124 }
06f7f2bb
PB
1125 while (dev->num_child_bus) {
1126 bus = QLIST_FIRST(&dev->child_bus);
6780a22c 1127 object_unparent(OBJECT(bus));
06f7f2bb 1128 }
06f7f2bb 1129 if (dev->parent_bus) {
5d5b24d0 1130 bus_remove_child(dev->parent_bus, dev);
62d7ba66
PB
1131 object_unref(OBJECT(dev->parent_bus));
1132 dev->parent_bus = NULL;
5d5b24d0 1133 }
0402a5d6 1134
b1ee5829 1135 /* Only send event if the device had been completely realized */
352e8da7 1136 if (dev->pending_deleted_event) {
b1ee5829
AL
1137 gchar *path = object_get_canonical_path(OBJECT(dev));
1138
24b699fb 1139 qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
b1ee5829 1140 g_free(path);
0402a5d6 1141 }
abed886e
PB
1142
1143 qemu_opts_del(dev->opts);
1144 dev->opts = NULL;
667d22d1
PB
1145}
1146
1147static void device_class_init(ObjectClass *class, void *data)
1148{
249d4172
AF
1149 DeviceClass *dc = DEVICE_CLASS(class);
1150
5d5b24d0 1151 class->unparent = device_unparent;
249d4172 1152 dc->realize = device_realize;
fe6c2117 1153 dc->unrealize = device_unrealize;
267a3264
IM
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;
667d22d1
PB
1162}
1163
94afdadc
AL
1164void device_reset(DeviceState *dev)
1165{
1166 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1167
1168 if (klass->reset) {
1169 klass->reset(dev);
1170 }
1171}
1172
f05f6b4a
PB
1173Object *qdev_get_machine(void)
1174{
1175 static Object *dev;
1176
1177 if (dev == NULL) {
dfe47e70 1178 dev = container_get(object_get_root(), "/machine");
f05f6b4a
PB
1179 }
1180
1181 return dev;
1182}
1183
8c43a6f0 1184static const TypeInfo device_type_info = {
32fea402
AL
1185 .name = TYPE_DEVICE,
1186 .parent = TYPE_OBJECT,
1187 .instance_size = sizeof(DeviceState),
9674bfe4 1188 .instance_init = device_initfn,
99a0b036 1189 .instance_post_init = device_post_init,
60adba37 1190 .instance_finalize = device_finalize,
bce54474 1191 .class_base_init = device_class_base_init,
667d22d1 1192 .class_init = device_class_init,
32fea402
AL
1193 .abstract = true,
1194 .class_size = sizeof(DeviceClass),
1195};
1196
83f7d43a 1197static void qdev_register_types(void)
32fea402
AL
1198{
1199 type_register_static(&device_type_info);
1200}
1201
83f7d43a 1202type_init(qdev_register_types)