]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/qdev.c
numa: Teach ram block notifiers about resizeable ram blocks
[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
61f3c91a 9 * version 2.1 of the License, or (at your option) any later version.
aae9460e
PB
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"
e688df6b 29#include "qapi/error.h"
c577ff62 30#include "qapi/qapi-events-qdev.h"
b4a42f81 31#include "qapi/qmp/qerror.h"
7b1b5d19 32#include "qapi/visitor.h"
d49b6836 33#include "qemu/error-report.h"
922a01a0 34#include "qemu/option.h"
0ee4de6c 35#include "hw/hotplug.h"
64552b6b 36#include "hw/irq.h"
a27bd6c7 37#include "hw/qdev-properties.h"
b7454548 38#include "hw/boards.h"
7474f1be 39#include "hw/sysbus.h"
0e6934f2 40#include "hw/qdev-clock.h"
d6454270 41#include "migration/vmstate.h"
70804c83 42#include "trace.h"
aae9460e 43
0ac8ef71 44static bool qdev_hot_added = false;
21def24a 45bool qdev_hot_removed = false;
3418bd25 46
4be9f0d1
AL
47const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
48{
6e008585
AL
49 DeviceClass *dc = DEVICE_GET_CLASS(dev);
50 return dc->vmsd;
4be9f0d1
AL
51}
52
2d24a646
ML
53static void bus_free_bus_child(BusChild *kid)
54{
55 object_unref(OBJECT(kid->child));
56 g_free(kid);
57}
58
0866aca1 59static void bus_remove_child(BusState *bus, DeviceState *child)
0c17542d 60{
0866aca1
AL
61 BusChild *kid;
62
63 QTAILQ_FOREACH(kid, &bus->children, sibling) {
64 if (kid->child == child) {
65 char name[32];
66
67 snprintf(name, sizeof(name), "child[%d]", kid->index);
2d24a646 68 QTAILQ_REMOVE_RCU(&bus->children, kid, sibling);
9d127820 69
12b2e9f3
TK
70 bus->num_children--;
71
9d127820 72 /* This gives back ownership of kid->child back to us. */
df4fe0b2 73 object_property_del(OBJECT(bus), name);
2d24a646
ML
74
75 /* free the bus kid, when it is safe to do so*/
76 call_rcu(kid, bus_free_bus_child, rcu);
77 break;
0866aca1
AL
78 }
79 }
80}
81
82static void bus_add_child(BusState *bus, DeviceState *child)
83{
84 char name[32];
85 BusChild *kid = g_malloc0(sizeof(*kid));
0c17542d 86
12b2e9f3 87 bus->num_children++;
0866aca1
AL
88 kid->index = bus->max_index++;
89 kid->child = child;
9d127820 90 object_ref(OBJECT(kid->child));
a5296ca9 91
2d24a646 92 QTAILQ_INSERT_HEAD_RCU(&bus->children, kid, sibling);
0866aca1 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 */
d2623129 100 0);
0866aca1
AL
101}
102
bb755ba4
PB
103static bool bus_check_address(BusState *bus, DeviceState *child, Error **errp)
104{
105 BusClass *bc = BUS_GET_CLASS(bus);
106 return !bc->check_address || bc->check_address(bus, child, errp);
107}
108
109bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp)
0866aca1 110{
a7c3a4f9 111 BusState *old_parent_bus = dev->parent_bus;
81cb0573
MA
112 DeviceClass *dc = DEVICE_GET_CLASS(dev);
113
114 assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type));
91c968ac 115
bb755ba4
PB
116 if (!bus_check_address(bus, dev, errp)) {
117 return false;
118 }
119
a7c3a4f9 120 if (old_parent_bus) {
70804c83 121 trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)),
a7c3a4f9 122 old_parent_bus, object_get_typename(OBJECT(old_parent_bus)),
70804c83
DH
123 OBJECT(bus), object_get_typename(OBJECT(bus)));
124 /*
125 * Keep a reference to the device while it's not plugged into
91c968ac
PM
126 * any bus, to avoid it potentially evaporating when it is
127 * dereffed in bus_remove_child().
a7c3a4f9
DH
128 * Also keep the ref of the parent bus until the end, so that
129 * we can safely call resettable_change_parent() below.
91c968ac
PM
130 */
131 object_ref(OBJECT(dev));
132 bus_remove_child(dev->parent_bus, dev);
91c968ac 133 }
9fbe6127 134 dev->parent_bus = bus;
62d7ba66 135 object_ref(OBJECT(bus));
0866aca1 136 bus_add_child(bus, dev);
a7c3a4f9
DH
137 if (dev->realized) {
138 resettable_change_parent(OBJECT(dev), OBJECT(bus),
139 OBJECT(old_parent_bus));
140 }
141 if (old_parent_bus) {
142 object_unref(OBJECT(old_parent_bus));
91c968ac
PM
143 object_unref(OBJECT(dev));
144 }
bb755ba4 145 return true;
0c17542d
MA
146}
147
9940b2cf
MA
148DeviceState *qdev_new(const char *name)
149{
7ab6e7fc
GH
150 if (!object_class_by_name(name)) {
151 module_load_qom_one(name);
152 }
9940b2cf
MA
153 return DEVICE(object_new(name));
154}
155
9940b2cf
MA
156DeviceState *qdev_try_new(const char *name)
157{
7ab6e7fc 158 if (!module_object_class_by_name(name)) {
9940b2cf
MA
159 return NULL;
160 }
9940b2cf
MA
161 return DEVICE(object_new(name));
162}
163
eae3eb3e 164static QTAILQ_HEAD(, DeviceListener) device_listeners
707ff800
PD
165 = QTAILQ_HEAD_INITIALIZER(device_listeners);
166
167enum ListenerDirection { Forward, Reverse };
168
169#define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
170 do { \
171 DeviceListener *_listener; \
172 \
173 switch (_direction) { \
174 case Forward: \
175 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
176 if (_listener->_callback) { \
177 _listener->_callback(_listener, ##_args); \
178 } \
179 } \
180 break; \
181 case Reverse: \
182 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
eae3eb3e 183 link) { \
707ff800
PD
184 if (_listener->_callback) { \
185 _listener->_callback(_listener, ##_args); \
186 } \
187 } \
188 break; \
189 default: \
190 abort(); \
191 } \
192 } while (0)
193
194static int device_listener_add(DeviceState *dev, void *opaque)
195{
196 DEVICE_LISTENER_CALL(realize, Forward, dev);
197
198 return 0;
199}
200
201void device_listener_register(DeviceListener *listener)
202{
203 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
204
205 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
206 NULL, NULL);
207}
208
209void device_listener_unregister(DeviceListener *listener)
210{
211 QTAILQ_REMOVE(&device_listeners, listener, link);
212}
213
f3a85056
JF
214bool qdev_should_hide_device(QemuOpts *opts)
215{
f3a85056
JF
216 DeviceListener *listener;
217
218 QTAILQ_FOREACH(listener, &device_listeners, link) {
b91ad981
JQ
219 if (listener->hide_device) {
220 if (listener->hide_device(listener, opts)) {
89631fed
JQ
221 return true;
222 }
f3a85056
JF
223 }
224 }
225
89631fed 226 return false;
f3a85056
JF
227}
228
4d2ffa08
JK
229void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
230 int required_for_version)
231{
7983c8a3 232 assert(!dev->realized);
4d2ffa08
JK
233 dev->instance_id_alias = alias_id;
234 dev->alias_required_for_version = required_for_version;
235}
236
03fcbd9d
TH
237HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
238{
239 MachineState *machine;
240 MachineClass *mc;
241 Object *m_obj = qdev_get_machine();
242
243 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
244 machine = MACHINE(m_obj);
245 mc = MACHINE_GET_CLASS(machine);
246 if (mc->get_hotplug_handler) {
247 return mc->get_hotplug_handler(machine, dev);
248 }
249 }
250
251 return NULL;
252}
253
d2321d31
PX
254bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
255{
256 MachineState *machine;
257 MachineClass *mc;
258 Object *m_obj = qdev_get_machine();
259
260 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
261 machine = MACHINE(m_obj);
262 mc = MACHINE_GET_CLASS(machine);
263 if (mc->hotplug_allowed) {
264 return mc->hotplug_allowed(machine, dev, errp);
265 }
266 }
267
268 return true;
269}
270
14405c27
DH
271HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
272{
273 if (dev->parent_bus) {
274 return dev->parent_bus->hotplug_handler;
275 }
276 return NULL;
277}
278
c06b2ffb 279HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
7716b8ca 280{
17cc0128 281 HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
7716b8ca 282
17cc0128 283 if (hotplug_ctrl == NULL && dev->parent_bus) {
14405c27 284 hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
7716b8ca
IM
285 }
286 return hotplug_ctrl;
287}
288
70804c83
DH
289static int qdev_prereset(DeviceState *dev, void *opaque)
290{
291 trace_qdev_reset_tree(dev, object_get_typename(OBJECT(dev)));
292 return 0;
293}
294
295static int qbus_prereset(BusState *bus, void *opaque)
296{
297 trace_qbus_reset_tree(bus, object_get_typename(OBJECT(bus)));
298 return 0;
299}
300
ec990eb6
AL
301static int qdev_reset_one(DeviceState *dev, void *opaque)
302{
f703a04c 303 device_legacy_reset(dev);
ec990eb6
AL
304
305 return 0;
306}
307
b4694b7c
IY
308static int qbus_reset_one(BusState *bus, void *opaque)
309{
0d936928 310 BusClass *bc = BUS_GET_CLASS(bus);
70804c83 311 trace_qbus_reset(bus, object_get_typename(OBJECT(bus)));
0d936928 312 if (bc->reset) {
dcc20931 313 bc->reset(bus);
b4694b7c
IY
314 }
315 return 0;
316}
317
5af0a04b
IY
318void qdev_reset_all(DeviceState *dev)
319{
70804c83
DH
320 trace_qdev_reset_all(dev, object_get_typename(OBJECT(dev)));
321 qdev_walk_children(dev, qdev_prereset, qbus_prereset,
322 qdev_reset_one, qbus_reset_one, NULL);
5af0a04b
IY
323}
324
ff8de075
DH
325void qdev_reset_all_fn(void *opaque)
326{
327 qdev_reset_all(DEVICE(opaque));
328}
329
d0508c36
PB
330void qbus_reset_all(BusState *bus)
331{
70804c83
DH
332 trace_qbus_reset_all(bus, object_get_typename(OBJECT(bus)));
333 qbus_walk_children(bus, qdev_prereset, qbus_prereset,
334 qdev_reset_one, qbus_reset_one, NULL);
d0508c36
PB
335}
336
80376c3f
IY
337void qbus_reset_all_fn(void *opaque)
338{
339 BusState *bus = opaque;
d0508c36 340 qbus_reset_all(bus);
80376c3f
IY
341}
342
abb89dbf
DH
343void device_cold_reset(DeviceState *dev)
344{
345 resettable_reset(OBJECT(dev), RESET_TYPE_COLD);
346}
347
c11256aa
DH
348bool device_is_in_reset(DeviceState *dev)
349{
350 return resettable_is_in_reset(OBJECT(dev));
351}
352
353static ResettableState *device_get_reset_state(Object *obj)
354{
355 DeviceState *dev = DEVICE(obj);
356 return &dev->reset;
357}
358
359static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
360 void *opaque, ResetType type)
361{
362 DeviceState *dev = DEVICE(obj);
363 BusState *bus;
364
365 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
366 cb(OBJECT(bus), opaque, type);
367 }
368}
369
3418bd25 370/* can be used as ->unplug() callback for the simple cases */
014176f9
IM
371void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
372 DeviceState *dev, Error **errp)
373{
dc3edf8d 374 qdev_unrealize(dev);
014176f9 375}
3b29a101 376
9940b2cf
MA
377bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
378{
9940b2cf 379 assert(!dev->realized && !dev->parent_bus);
9940b2cf 380
510ef98d 381 if (bus) {
bb755ba4
PB
382 if (!qdev_set_parent_bus(dev, bus, errp)) {
383 return false;
384 }
510ef98d
MA
385 } else {
386 assert(!DEVICE_GET_CLASS(dev)->bus_type);
387 }
9940b2cf 388
f07ad48d 389 return object_property_set_bool(OBJECT(dev), "realized", true, errp);
9940b2cf
MA
390}
391
9940b2cf
MA
392bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp)
393{
394 bool ret;
395
396 ret = qdev_realize(dev, bus, errp);
397 object_unref(OBJECT(dev));
398 return ret;
399}
400
401void qdev_unrealize(DeviceState *dev)
402{
5325cc34 403 object_property_set_bool(OBJECT(dev), "realized", false, &error_abort);
9940b2cf
MA
404}
405
f66dc873 406static int qdev_assert_realized_properly_cb(Object *obj, void *opaque)
dfe8c79c
MA
407{
408 DeviceState *dev = DEVICE(object_dynamic_cast(obj, TYPE_DEVICE));
409 DeviceClass *dc;
410
411 if (dev) {
412 dc = DEVICE_GET_CLASS(dev);
413 assert(dev->realized);
414 assert(dev->parent_bus || !dc->bus_type);
415 }
416 return 0;
417}
418
f66dc873 419void qdev_assert_realized_properly(void)
3418bd25 420{
dfe8c79c 421 object_child_foreach_recursive(object_get_root(),
f66dc873 422 qdev_assert_realized_properly_cb, NULL);
3418bd25
GH
423}
424
0ac8ef71
AW
425bool qdev_machine_modified(void)
426{
427 return qdev_hot_added || qdev_hot_removed;
428}
429
02e2da45 430BusState *qdev_get_parent_bus(DeviceState *dev)
aae9460e 431{
02e2da45 432 return dev->parent_bus;
aae9460e
PB
433}
434
a5f54290
PC
435static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
436 const char *name)
437{
438 NamedGPIOList *ngl;
439
440 QLIST_FOREACH(ngl, &dev->gpios, node) {
3a87dde8
MAL
441 /* NULL is a valid and matchable name. */
442 if (g_strcmp0(name, ngl->name) == 0) {
a5f54290
PC
443 return ngl;
444 }
445 }
446
447 ngl = g_malloc0(sizeof(*ngl));
448 ngl->name = g_strdup(name);
449 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
450 return ngl;
451}
452
4a151677
PM
453void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
454 qemu_irq_handler handler,
455 void *opaque,
456 const char *name, int n)
a5f54290 457{
a69bef1c 458 int i;
a5f54290
PC
459 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
460
b235a71f 461 assert(gpio_list->num_out == 0 || !name);
a5f54290 462 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
4a151677 463 opaque, n);
a69bef1c 464
6c76b377
PF
465 if (!name) {
466 name = "unnamed-gpio-in";
467 }
a69bef1c 468 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
6c76b377
PF
469 gchar *propname = g_strdup_printf("%s[%u]", name, i);
470
a69bef1c 471 object_property_add_child(OBJECT(dev), propname,
d2623129 472 OBJECT(gpio_list->in[i]));
6c76b377 473 g_free(propname);
a69bef1c 474 }
a69bef1c 475
a5f54290
PC
476 gpio_list->num_in += n;
477}
478
aae9460e
PB
479void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
480{
a5f54290
PC
481 qdev_init_gpio_in_named(dev, handler, NULL, n);
482}
483
484void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
485 const char *name, int n)
486{
688b057a 487 int i;
a5f54290
PC
488 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
489
b235a71f 490 assert(gpio_list->num_in == 0 || !name);
688b057a 491
6c76b377
PF
492 if (!name) {
493 name = "unnamed-gpio-out";
494 }
495 memset(pins, 0, sizeof(*pins) * n);
688b057a 496 for (i = 0; i < n; ++i) {
6c76b377
PF
497 gchar *propname = g_strdup_printf("%s[%u]", name,
498 gpio_list->num_out + i);
499
688b057a
PC
500 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
501 (Object **)&pins[i],
502 object_property_allow_set_link,
d2623129 503 OBJ_PROP_LINK_STRONG);
6c76b377 504 g_free(propname);
688b057a 505 }
6c76b377 506 gpio_list->num_out += n;
aae9460e
PB
507}
508
509void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
510{
a5f54290
PC
511 qdev_init_gpio_out_named(dev, pins, NULL, n);
512}
513
514qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
515{
516 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
517
518 assert(n >= 0 && n < gpio_list->num_in);
519 return gpio_list->in[n];
aae9460e
PB
520}
521
522qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
523{
a5f54290
PC
524 return qdev_get_gpio_in_named(dev, NULL, n);
525}
526
527void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
528 qemu_irq pin)
529{
02757df2
PC
530 char *propname = g_strdup_printf("%s[%d]",
531 name ? name : "unnamed-gpio-out", n);
9f742c28
MA
532 if (pin && !OBJECT(pin)->parent) {
533 /* We need a name for object_property_set_link to work */
88950eef
AF
534 object_property_add_child(container_get(qdev_get_machine(),
535 "/unattached"),
d2623129 536 "non-qdev-gpio[*]", OBJECT(pin));
02757df2 537 }
5325cc34 538 object_property_set_link(OBJECT(dev), propname, OBJECT(pin), &error_abort);
02757df2 539 g_free(propname);
aae9460e
PB
540}
541
b7973186
AG
542qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
543{
2244f233 544 g_autofree char *propname = g_strdup_printf("%s[%d]",
b7973186
AG
545 name ? name : "unnamed-gpio-out", n);
546
547 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
548 NULL);
549
550 return ret;
551}
552
67cc32eb 553/* disconnect a GPIO output, returning the disconnected input (if any) */
0c24db2b
PC
554
555static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
556 const char *name, int n)
557{
558 char *propname = g_strdup_printf("%s[%d]",
559 name ? name : "unnamed-gpio-out", n);
560
561 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
562 NULL);
563 if (ret) {
5325cc34 564 object_property_set_link(OBJECT(dev), propname, NULL, NULL);
0c24db2b
PC
565 }
566 g_free(propname);
567 return ret;
568}
a5f54290 569
0c24db2b
PC
570qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
571 const char *name, int n)
572{
573 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
574 qdev_connect_gpio_out_named(dev, name, n, icpt);
575 return disconnected;
aae9460e
PB
576}
577
578void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
579{
a5f54290 580 qdev_connect_gpio_out_named(dev, NULL, n, pin);
aae9460e
PB
581}
582
17a96a14
PC
583void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
584 const char *name)
585{
586 int i;
587 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
588
589 for (i = 0; i < ngl->num_in; i++) {
590 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
591 char *propname = g_strdup_printf("%s[%d]", nm, i);
592
593 object_property_add_alias(OBJECT(container), propname,
d2623129 594 OBJECT(dev), propname);
6bc5cf92 595 g_free(propname);
17a96a14
PC
596 }
597 for (i = 0; i < ngl->num_out; i++) {
598 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
599 char *propname = g_strdup_printf("%s[%d]", nm, i);
600
601 object_property_add_alias(OBJECT(container), propname,
d2623129 602 OBJECT(dev), propname);
6bc5cf92 603 g_free(propname);
17a96a14
PC
604 }
605 QLIST_REMOVE(ngl, node);
606 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
607}
608
02e2da45 609BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
4d6ae674 610{
02e2da45 611 BusState *bus;
f698c8ba
PC
612 Object *child = object_resolve_path_component(OBJECT(dev), name);
613
614 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
615 if (bus) {
616 return bus;
617 }
4d6ae674 618
72cf2d4f 619 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
4d6ae674 620 if (strcmp(name, bus->name) == 0) {
02e2da45 621 return bus;
4d6ae674
PB
622 }
623 }
624 return NULL;
625}
626
0293214b
PB
627int qdev_walk_children(DeviceState *dev,
628 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
629 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
630 void *opaque)
81699d8a
AL
631{
632 BusState *bus;
633 int err;
634
0293214b
PB
635 if (pre_devfn) {
636 err = pre_devfn(dev, opaque);
81699d8a
AL
637 if (err) {
638 return err;
639 }
640 }
641
642 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
0293214b
PB
643 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
644 post_devfn, post_busfn, opaque);
81699d8a
AL
645 if (err < 0) {
646 return err;
647 }
648 }
649
0293214b
PB
650 if (post_devfn) {
651 err = post_devfn(dev, opaque);
652 if (err) {
653 return err;
654 }
655 }
656
81699d8a
AL
657 return 0;
658}
659
a2ee6b4f 660DeviceState *qdev_find_recursive(BusState *bus, const char *id)
3418bd25 661{
0866aca1
AL
662 BusChild *kid;
663 DeviceState *ret;
3418bd25
GH
664 BusState *child;
665
2d24a646
ML
666 WITH_RCU_READ_LOCK_GUARD() {
667 QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
668 DeviceState *dev = kid->child;
0866aca1 669
2d24a646
ML
670 if (dev->id && strcmp(dev->id, id) == 0) {
671 return dev;
672 }
0866aca1 673
2d24a646
ML
674 QLIST_FOREACH(child, &dev->child_bus, sibling) {
675 ret = qdev_find_recursive(child, id);
676 if (ret) {
677 return ret;
678 }
3418bd25
GH
679 }
680 }
681 }
682 return NULL;
683}
684
09e5ab63 685char *qdev_get_dev_path(DeviceState *dev)
85ed303b 686{
0d936928 687 BusClass *bc;
09e5ab63
AL
688
689 if (!dev || !dev->parent_bus) {
690 return NULL;
691 }
692
0d936928
AL
693 bc = BUS_GET_CLASS(dev->parent_bus);
694 if (bc->get_dev_path) {
695 return bc->get_dev_path(dev);
09e5ab63
AL
696 }
697
698 return NULL;
44677ded 699}
a5296ca9 700
a7737e44 701static bool device_get_realized(Object *obj, Error **errp)
249d4172
AF
702{
703 DeviceState *dev = DEVICE(obj);
704 return dev->realized;
705}
706
40f03bd5 707static bool check_only_migratable(Object *obj, Error **errp)
1bfe5f05
JQ
708{
709 DeviceClass *dc = DEVICE_GET_CLASS(obj);
710
711 if (!vmstate_check_only_migratable(dc->vmsd)) {
40f03bd5 712 error_setg(errp, "Device %s is not migratable, but "
1bfe5f05
JQ
713 "--only-migratable was specified",
714 object_get_typename(obj));
715 return false;
716 }
717
718 return true;
719}
720
a7737e44 721static void device_set_realized(Object *obj, bool value, Error **errp)
249d4172
AF
722{
723 DeviceState *dev = DEVICE(obj);
724 DeviceClass *dc = DEVICE_GET_CLASS(dev);
7716b8ca 725 HotplugHandler *hotplug_ctrl;
5c21ce77 726 BusState *bus;
0e6934f2 727 NamedClockList *ncl;
249d4172 728 Error *local_err = NULL;
69382d8b
IM
729 bool unattached_parent = false;
730 static int unattached_count;
249d4172 731
1a37eca1 732 if (dev->hotplugged && !dc->hotpluggable) {
c6bd8c70 733 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
1a37eca1
IM
734 return;
735 }
736
249d4172 737 if (value && !dev->realized) {
a5f9b9df 738 if (!check_only_migratable(obj, errp)) {
7562f907
AA
739 goto fail;
740 }
741
d578029e 742 if (!obj->parent) {
249d4172
AF
743 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
744
745 object_property_add_child(container_get(qdev_get_machine(),
746 "/unattached"),
d2623129 747 name, obj);
69382d8b 748 unattached_parent = true;
249d4172
AF
749 g_free(name);
750 }
751
41346263
IM
752 hotplug_ctrl = qdev_get_hotplug_handler(dev);
753 if (hotplug_ctrl) {
754 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
755 if (local_err != NULL) {
756 goto fail;
757 }
758 }
759
a7ddba52
IM
760 if (dc->realize) {
761 dc->realize(dev, &local_err);
40f03bd5
VSO
762 if (local_err != NULL) {
763 goto fail;
764 }
1d45a705
GA
765 }
766
707ff800
PD
767 DEVICE_LISTENER_CALL(realize, Forward, dev);
768
04162f8f
MR
769 /*
770 * always free/re-initialize here since the value cannot be cleaned up
771 * in device_unrealize due to its usage later on in the unplug path
772 */
773 g_free(dev->canonical_path);
774 dev->canonical_path = object_get_canonical_path(OBJECT(dev));
0e6934f2
DH
775 QLIST_FOREACH(ncl, &dev->clocks, node) {
776 if (ncl->alias) {
777 continue;
778 } else {
779 clock_setup_canonical_path(ncl->clock);
780 }
781 }
04162f8f 782
1d45a705 783 if (qdev_get_vmsd(dev)) {
3cad405b 784 if (vmstate_register_with_alias_id(VMSTATE_IF(dev),
1df2c9a2
PX
785 VMSTATE_INSTANCE_ID_ANY,
786 qdev_get_vmsd(dev), dev,
67980031
DDAG
787 dev->instance_id_alias,
788 dev->alias_required_for_version,
789 &local_err) < 0) {
790 goto post_realize_fail;
791 }
249d4172 792 }
1d45a705 793
e755e127
DH
794 /*
795 * Clear the reset state, in case the object was previously unrealized
796 * with a dirty state.
797 */
798 resettable_state_clear(&dev->reset);
799
1d45a705 800 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
f1483b46 801 if (!qbus_realize(bus, errp)) {
1d45a705 802 goto child_realize_fail;
5c21ce77
BD
803 }
804 }
1d45a705 805 if (dev->hotplugged) {
e755e127
DH
806 /*
807 * Reset the device, as well as its subtree which, at this point,
808 * should be realized too.
809 */
810 resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD);
811 resettable_change_parent(OBJECT(dev), OBJECT(dev->parent_bus),
812 NULL);
813 resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD);
249d4172 814 }
352e8da7 815 dev->pending_deleted_event = false;
25e89788
SH
816
817 if (hotplug_ctrl) {
8b5e6caf
IM
818 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
819 if (local_err != NULL) {
820 goto child_realize_fail;
821 }
822 }
823
a23151e8
ML
824 qatomic_store_release(&dev->realized, value);
825
249d4172 826 } else if (!value && dev->realized) {
a23151e8
ML
827
828 /*
829 * Change the value so that any concurrent users are aware
830 * that the device is going to be unrealized
831 *
832 * TODO: change .realized property to enum that states
833 * each phase of the device realization/unrealization
834 */
835
836 qatomic_set(&dev->realized, value);
837 /*
838 * Ensure that concurrent users see this update prior to
839 * any other changes done by unrealize.
840 */
841 smp_wmb();
842
5c21ce77 843 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
f1483b46 844 qbus_unrealize(bus);
5c21ce77 845 }
cd4520ad 846 if (qdev_get_vmsd(dev)) {
3cad405b 847 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
fe6c2117 848 }
cd4520ad 849 if (dc->unrealize) {
b69c3c21 850 dc->unrealize(dev);
249d4172 851 }
352e8da7 852 dev->pending_deleted_event = true;
707ff800 853 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
249d4172
AF
854 }
855
40f03bd5 856 assert(local_err == NULL);
1d45a705
GA
857 return;
858
859child_realize_fail:
860 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
f1483b46 861 qbus_unrealize(bus);
1d45a705
GA
862 }
863
864 if (qdev_get_vmsd(dev)) {
3cad405b 865 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
1d45a705
GA
866 }
867
868post_realize_fail:
04162f8f
MR
869 g_free(dev->canonical_path);
870 dev->canonical_path = NULL;
1d45a705 871 if (dc->unrealize) {
b69c3c21 872 dc->unrealize(dev);
1d45a705
GA
873 }
874
875fail:
876 error_propagate(errp, local_err);
69382d8b 877 if (unattached_parent) {
9940b2cf
MA
878 /*
879 * Beware, this doesn't just revert
880 * object_property_add_child(), it also runs bus_remove()!
881 */
69382d8b
IM
882 object_unparent(OBJECT(dev));
883 unattached_count--;
884 }
249d4172
AF
885}
886
a7737e44 887static bool device_get_hotpluggable(Object *obj, Error **errp)
1a37eca1
IM
888{
889 DeviceClass *dc = DEVICE_GET_CLASS(obj);
890 DeviceState *dev = DEVICE(obj);
891
2b81b35f 892 return dc->hotpluggable && (dev->parent_bus == NULL ||
39b888bd 893 qbus_is_hotpluggable(dev->parent_bus));
1a37eca1
IM
894}
895
40f03bd5 896static bool device_get_hotplugged(Object *obj, Error **errp)
d012ffc1
IM
897{
898 DeviceState *dev = DEVICE(obj);
899
900 return dev->hotplugged;
901}
902
9674bfe4
AL
903static void device_initfn(Object *obj)
904{
905 DeviceState *dev = DEVICE(obj);
9674bfe4 906
2f181fbd 907 if (phase_check(PHASE_MACHINE_READY)) {
9674bfe4
AL
908 dev->hotplugged = 1;
909 qdev_hot_added = true;
910 }
911
912 dev->instance_id_alias = -1;
7983c8a3 913 dev->realized = false;
a1190ab6 914 dev->allow_unplug_during_migration = false;
9674bfe4 915
a5f54290 916 QLIST_INIT(&dev->gpios);
0e6934f2 917 QLIST_INIT(&dev->clocks);
9674bfe4
AL
918}
919
1c3994f6
MAL
920static void device_post_init(Object *obj)
921{
1a3ec8c1
MA
922 /*
923 * Note: ordered so that the user's global properties take
924 * precedence.
925 */
1c3994f6 926 object_apply_compat_props(obj);
25f8dd96 927 qdev_prop_set_globals(DEVICE(obj));
99a0b036
EH
928}
929
60adba37
AL
930/* Unlink device from bus and free the structure. */
931static void device_finalize(Object *obj)
932{
a5f54290
PC
933 NamedGPIOList *ngl, *next;
934
60adba37 935 DeviceState *dev = DEVICE(obj);
a5f54290
PC
936
937 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
938 QLIST_REMOVE(ngl, node);
f173d57a 939 qemu_free_irqs(ngl->in, ngl->num_in);
a5f54290
PC
940 g_free(ngl->name);
941 g_free(ngl);
942 /* ngl->out irqs are owned by the other end and should not be freed
943 * here
944 */
945 }
f7b879e0 946
0e6934f2
DH
947 qdev_finalize_clocklist(dev);
948
f7b879e0
MR
949 /* Only send event if the device had been completely realized */
950 if (dev->pending_deleted_event) {
951 g_assert(dev->canonical_path);
952
3ab72385 953 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
f7b879e0
MR
954 g_free(dev->canonical_path);
955 dev->canonical_path = NULL;
956 }
957
958 qemu_opts_del(dev->opts);
60adba37
AL
959}
960
bce54474
PB
961static void device_class_base_init(ObjectClass *class, void *data)
962{
963 DeviceClass *klass = DEVICE_CLASS(class);
964
965 /* We explicitly look up properties in the superclasses,
966 * so do not propagate them to the subclasses.
967 */
385d8f22 968 klass->props_ = NULL;
60adba37
AL
969}
970
5d5b24d0 971static void device_unparent(Object *obj)
667d22d1
PB
972{
973 DeviceState *dev = DEVICE(obj);
06f7f2bb 974 BusState *bus;
667d22d1 975
5c21ce77 976 if (dev->realized) {
dc3edf8d 977 qdev_unrealize(dev);
5c21ce77 978 }
06f7f2bb
PB
979 while (dev->num_child_bus) {
980 bus = QLIST_FIRST(&dev->child_bus);
6780a22c 981 object_unparent(OBJECT(bus));
06f7f2bb 982 }
06f7f2bb 983 if (dev->parent_bus) {
5d5b24d0 984 bus_remove_child(dev->parent_bus, dev);
62d7ba66
PB
985 object_unref(OBJECT(dev->parent_bus));
986 dev->parent_bus = NULL;
5d5b24d0 987 }
667d22d1
PB
988}
989
107b5969
MAL
990static char *
991device_vmstate_if_get_id(VMStateIf *obj)
992{
993 DeviceState *dev = DEVICE(obj);
994
995 return qdev_get_dev_path(dev);
996}
997
c11256aa
DH
998/**
999 * device_phases_reset:
1000 * Transition reset method for devices to allow moving
1001 * smoothly from legacy reset method to multi-phases
1002 */
1003static void device_phases_reset(DeviceState *dev)
1004{
1005 ResettableClass *rc = RESETTABLE_GET_CLASS(dev);
1006
1007 if (rc->phases.enter) {
1008 rc->phases.enter(OBJECT(dev), RESET_TYPE_COLD);
1009 }
1010 if (rc->phases.hold) {
1011 rc->phases.hold(OBJECT(dev));
1012 }
1013 if (rc->phases.exit) {
1014 rc->phases.exit(OBJECT(dev));
1015 }
1016}
1017
1018static void device_transitional_reset(Object *obj)
1019{
1020 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1021
1022 /*
1023 * This will call either @device_phases_reset (for multi-phases transitioned
1024 * devices) or a device's specific method for not-yet transitioned devices.
1025 * In both case, it does not reset children.
1026 */
1027 if (dc->reset) {
1028 dc->reset(DEVICE(obj));
1029 }
1030}
1031
1032/**
1033 * device_get_transitional_reset:
1034 * check if the device's class is ready for multi-phase
1035 */
1036static ResettableTrFunction device_get_transitional_reset(Object *obj)
1037{
1038 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1039 if (dc->reset != device_phases_reset) {
1040 /*
1041 * dc->reset has been overridden by a subclass,
1042 * the device is not ready for multi phase yet.
1043 */
1044 return device_transitional_reset;
1045 }
1046 return NULL;
1047}
1048
667d22d1
PB
1049static void device_class_init(ObjectClass *class, void *data)
1050{
249d4172 1051 DeviceClass *dc = DEVICE_CLASS(class);
107b5969 1052 VMStateIfClass *vc = VMSTATE_IF_CLASS(class);
c11256aa 1053 ResettableClass *rc = RESETTABLE_CLASS(class);
249d4172 1054
5d5b24d0 1055 class->unparent = device_unparent;
267a3264
IM
1056
1057 /* by default all devices were considered as hotpluggable,
1058 * so with intent to check it in generic qdev_unplug() /
1059 * device_set_realized() functions make every device
1060 * hotpluggable. Devices that shouldn't be hotpluggable,
1061 * should override it in their class_init()
1062 */
1063 dc->hotpluggable = true;
e90f2a8c 1064 dc->user_creatable = true;
107b5969 1065 vc->get_id = device_vmstate_if_get_id;
c11256aa
DH
1066 rc->get_state = device_get_reset_state;
1067 rc->child_foreach = device_reset_child_foreach;
1068
1069 /*
1070 * @device_phases_reset is put as the default reset method below, allowing
1071 * to do the multi-phase transition from base classes to leaf classes. It
1072 * allows a legacy-reset Device class to extend a multi-phases-reset
1073 * Device class for the following reason:
1074 * + If a base class B has been moved to multi-phase, then it does not
1075 * override this default reset method and may have defined phase methods.
1076 * + A child class C (extending class B) which uses
1077 * device_class_set_parent_reset() (or similar means) to override the
1078 * reset method will still work as expected. @device_phases_reset function
1079 * will be registered as the parent reset method and effectively call
1080 * parent reset phases.
1081 */
1082 dc->reset = device_phases_reset;
1083 rc->get_transitional_function = device_get_transitional_reset;
c68fc935
MAL
1084
1085 object_class_property_add_bool(class, "realized",
d2623129 1086 device_get_realized, device_set_realized);
c68fc935 1087 object_class_property_add_bool(class, "hotpluggable",
d2623129 1088 device_get_hotpluggable, NULL);
c68fc935 1089 object_class_property_add_bool(class, "hotplugged",
d2623129 1090 device_get_hotplugged, NULL);
c68fc935 1091 object_class_property_add_link(class, "parent_bus", TYPE_BUS,
d2623129 1092 offsetof(DeviceState, parent_bus), NULL, 0);
667d22d1
PB
1093}
1094
46795cf2
PMD
1095void device_class_set_parent_reset(DeviceClass *dc,
1096 DeviceReset dev_reset,
1097 DeviceReset *parent_reset)
1098{
1099 *parent_reset = dc->reset;
1100 dc->reset = dev_reset;
1101}
1102
1103void device_class_set_parent_realize(DeviceClass *dc,
1104 DeviceRealize dev_realize,
1105 DeviceRealize *parent_realize)
1106{
1107 *parent_realize = dc->realize;
1108 dc->realize = dev_realize;
1109}
1110
1111void device_class_set_parent_unrealize(DeviceClass *dc,
1112 DeviceUnrealize dev_unrealize,
1113 DeviceUnrealize *parent_unrealize)
1114{
1115 *parent_unrealize = dc->unrealize;
1116 dc->unrealize = dev_unrealize;
1117}
1118
f703a04c 1119void device_legacy_reset(DeviceState *dev)
94afdadc
AL
1120{
1121 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1122
70804c83 1123 trace_qdev_reset(dev, object_get_typename(OBJECT(dev)));
94afdadc
AL
1124 if (klass->reset) {
1125 klass->reset(dev);
1126 }
1127}
1128
f05f6b4a
PB
1129Object *qdev_get_machine(void)
1130{
1131 static Object *dev;
1132
1133 if (dev == NULL) {
dfe47e70 1134 dev = container_get(object_get_root(), "/machine");
f05f6b4a
PB
1135 }
1136
1137 return dev;
1138}
1139
2f181fbd
PB
1140static MachineInitPhase machine_phase;
1141
1142bool phase_check(MachineInitPhase phase)
1143{
1144 return machine_phase >= phase;
1145}
1146
1147void phase_advance(MachineInitPhase phase)
1148{
1149 assert(machine_phase == phase - 1);
1150 machine_phase = phase;
1151}
1152
8c43a6f0 1153static const TypeInfo device_type_info = {
32fea402
AL
1154 .name = TYPE_DEVICE,
1155 .parent = TYPE_OBJECT,
1156 .instance_size = sizeof(DeviceState),
9674bfe4 1157 .instance_init = device_initfn,
99a0b036 1158 .instance_post_init = device_post_init,
60adba37 1159 .instance_finalize = device_finalize,
bce54474 1160 .class_base_init = device_class_base_init,
667d22d1 1161 .class_init = device_class_init,
32fea402
AL
1162 .abstract = true,
1163 .class_size = sizeof(DeviceClass),
107b5969
MAL
1164 .interfaces = (InterfaceInfo[]) {
1165 { TYPE_VMSTATE_IF },
c11256aa 1166 { TYPE_RESETTABLE_INTERFACE },
107b5969
MAL
1167 { }
1168 }
32fea402
AL
1169};
1170
83f7d43a 1171static void qdev_register_types(void)
32fea402
AL
1172{
1173 type_register_static(&device_type_info);
1174}
1175
83f7d43a 1176type_init(qdev_register_types)