blob: 4f3200d54bb1ca4134f201d4bbf3467b8c463e0d [file] [log] [blame]
Paul Brookaae94602009-05-14 22:35:06 +01001/*
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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Paul Brookaae94602009-05-14 22:35:06 +010018 */
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
Peter Maydell18c86e22016-01-26 18:17:29 +000028#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010029#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010031#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060032#include "qapi/qapi-events-misc.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010033#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010034#include "qapi/visitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010035#include "qemu/error-report.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010036#include "qemu/option.h"
Igor Mammedov0ee4de62014-02-05 16:36:45 +010037#include "hw/hotplug.h"
Igor Mammedovb7454542014-06-02 15:25:03 +020038#include "hw/boards.h"
Peter Maydell7474f1b2016-05-10 11:30:42 +010039#include "hw/sysbus.h"
Paul Brookaae94602009-05-14 22:35:06 +010040
Juan Quintela9bed84c2017-03-28 11:08:52 +020041bool qdev_hotplug = false;
Alex Williamson0ac8ef72011-01-04 12:37:50 -070042static bool qdev_hot_added = false;
Juan Quintela21def242017-03-28 11:22:10 +020043bool qdev_hot_removed = false;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020044
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060045const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
46{
Anthony Liguori6e008582011-12-09 11:06:57 -060047 DeviceClass *dc = DEVICE_GET_CLASS(dev);
48 return dc->vmsd;
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060049}
50
Anthony Liguori0866aca2011-12-23 15:34:39 -060051static void bus_remove_child(BusState *bus, DeviceState *child)
Paul Brookaae94602009-05-14 22:35:06 +010052{
Anthony Liguori0866aca2011-12-23 15:34:39 -060053 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);
Paolo Bonzini9d127822013-01-25 14:12:32 +010061
Tony Krowiak12b2e9f2018-12-17 10:57:30 -050062 bus->num_children--;
63
Paolo Bonzini9d127822013-01-25 14:12:32 +010064 /* This gives back ownership of kid->child back to us. */
Anthony Liguori0866aca2011-12-23 15:34:39 -060065 object_property_del(OBJECT(bus), name, NULL);
Paolo Bonzini9d127822013-01-25 14:12:32 +010066 object_unref(OBJECT(kid->child));
Anthony Liguori0866aca2011-12-23 15:34:39 -060067 g_free(kid);
68 return;
69 }
70 }
71}
72
73static void bus_add_child(BusState *bus, DeviceState *child)
74{
75 char name[32];
76 BusChild *kid = g_malloc0(sizeof(*kid));
Paul Brookaae94602009-05-14 22:35:06 +010077
Tony Krowiak12b2e9f2018-12-17 10:57:30 -050078 bus->num_children++;
Anthony Liguori0866aca2011-12-23 15:34:39 -060079 kid->index = bus->max_index++;
80 kid->child = child;
Paolo Bonzini9d127822013-01-25 14:12:32 +010081 object_ref(OBJECT(kid->child));
Anthony Liguoria5296ca2011-12-12 14:29:27 -060082
Anthony Liguori0866aca2011-12-23 15:34:39 -060083 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
84
Paolo Bonzini9d127822013-01-25 14:12:32 +010085 /* This transfers ownership of kid->child to the property. */
Anthony Liguori0866aca2011-12-23 15:34:39 -060086 snprintf(name, sizeof(name), "child[%d]", kid->index);
87 object_property_add_link(OBJECT(bus), name,
88 object_get_typename(OBJECT(child)),
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +010089 (Object **)&kid->child,
90 NULL, /* read-only property */
91 0, /* return ownership on prop deletion */
92 NULL);
Anthony Liguori0866aca2011-12-23 15:34:39 -060093}
94
95void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
96{
Peter Maydell91c968a2017-02-28 14:55:08 +000097 bool replugging = dev->parent_bus != NULL;
98
99 if (replugging) {
100 /* Keep a reference to the device while it's not plugged into
101 * any bus, to avoid it potentially evaporating when it is
102 * dereffed in bus_remove_child().
103 */
104 object_ref(OBJECT(dev));
105 bus_remove_child(dev->parent_bus, dev);
106 object_unref(OBJECT(dev->parent_bus));
107 }
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600108 dev->parent_bus = bus;
Paolo Bonzini62d7ba62013-01-25 14:12:35 +0100109 object_ref(OBJECT(bus));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600110 bus_add_child(bus, dev);
Peter Maydell91c968a2017-02-28 14:55:08 +0000111 if (replugging) {
112 object_unref(OBJECT(dev));
113 }
Paul Brookaae94602009-05-14 22:35:06 +0100114}
115
Markus Armbruster0210afe2015-06-19 16:17:22 +0200116/* Create a new device. This only initializes the device state
117 structure and allows properties to be set. The device still needs
118 to be realized. See qdev-core.h. */
Markus Armbruster0c175422010-02-19 19:12:18 +0100119DeviceState *qdev_create(BusState *bus, const char *name)
120{
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000121 DeviceState *dev;
122
123 dev = qdev_try_create(bus, name);
124 if (!dev) {
Peter Maydelle92714c2011-08-03 23:49:04 +0100125 if (bus) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100126 error_report("Unknown device '%s' for bus '%s'", name,
Eduardo Habkost23e3fbe2012-12-04 11:19:34 -0200127 object_get_typename(OBJECT(bus)));
Peter Maydelle92714c2011-08-03 23:49:04 +0100128 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100129 error_report("Unknown device '%s' for default sysbus", name);
Peter Maydelle92714c2011-08-03 23:49:04 +0100130 }
liguang01ed1d52013-03-22 16:44:14 +0800131 abort();
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000132 }
133
134 return dev;
135}
136
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200137DeviceState *qdev_try_create(BusState *bus, const char *type)
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000138{
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600139 DeviceState *dev;
140
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200141 if (object_class_by_name(type) == NULL) {
Andreas Färber4ed658c2012-02-17 02:47:44 +0100142 return NULL;
143 }
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200144 dev = DEVICE(object_new(type));
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600145 if (!dev) {
146 return NULL;
147 }
148
Markus Armbruster0c175422010-02-19 19:12:18 +0100149 if (!bus) {
Peter Maydell7474f1b2016-05-10 11:30:42 +0100150 /* Assert that the device really is a SysBusDevice before
151 * we put it onto the sysbus. Non-sysbus devices which aren't
152 * being put onto a bus should be created with object_new(TYPE_FOO),
153 * not qdev_create(NULL, TYPE_FOO).
154 */
155 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
Stefan Weil68694892010-12-16 19:33:22 +0100156 bus = sysbus_get_default();
Markus Armbruster0c175422010-02-19 19:12:18 +0100157 }
158
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600159 qdev_set_parent_bus(dev, bus);
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100160 object_unref(OBJECT(dev));
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600161 return dev;
Markus Armbruster0c175422010-02-19 19:12:18 +0100162}
163
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100164static QTAILQ_HEAD(, DeviceListener) device_listeners
Paul Durrant707ff802015-01-20 11:05:07 +0000165 = 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, \
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100183 link) { \
Paul Durrant707ff802015-01-20 11:05:07 +0000184 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
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200214void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
215 int required_for_version)
216{
Andreas Färber7983c8a2013-01-09 03:58:10 +0100217 assert(!dev->realized);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200218 dev->instance_id_alias = alias_id;
219 dev->alias_required_for_version = required_for_version;
220}
221
Thomas Huth03fcbd92017-11-02 11:10:06 +0100222HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
223{
224 MachineState *machine;
225 MachineClass *mc;
226 Object *m_obj = qdev_get_machine();
227
228 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
229 machine = MACHINE(m_obj);
230 mc = MACHINE_GET_CLASS(machine);
231 if (mc->get_hotplug_handler) {
232 return mc->get_hotplug_handler(machine, dev);
233 }
234 }
235
236 return NULL;
237}
238
David Hildenbrand14405c22019-02-28 13:28:49 +0100239HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
240{
241 if (dev->parent_bus) {
242 return dev->parent_bus->hotplug_handler;
243 }
244 return NULL;
245}
246
Zhu Guihuac06b2ff2015-04-27 16:47:21 +0800247HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
Igor Mammedov7716b8c2014-09-26 09:28:41 +0000248{
Igor Mammedov17cc0122019-02-28 13:28:48 +0100249 HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
Igor Mammedov7716b8c2014-09-26 09:28:41 +0000250
Igor Mammedov17cc0122019-02-28 13:28:48 +0100251 if (hotplug_ctrl == NULL && dev->parent_bus) {
David Hildenbrand14405c22019-02-28 13:28:49 +0100252 hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
Igor Mammedov7716b8c2014-09-26 09:28:41 +0000253 }
254 return hotplug_ctrl;
255}
256
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900257static int qdev_reset_one(DeviceState *dev, void *opaque)
258{
Anthony Liguori94afdad2011-12-04 11:36:01 -0600259 device_reset(dev);
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900260
261 return 0;
262}
263
Isaku Yamahatab4694b72010-11-19 18:56:00 +0900264static int qbus_reset_one(BusState *bus, void *opaque)
265{
Anthony Liguori0d936922012-05-02 09:00:20 +0200266 BusClass *bc = BUS_GET_CLASS(bus);
267 if (bc->reset) {
Paolo Bonzinidcc20932013-12-06 17:54:27 +0100268 bc->reset(bus);
Isaku Yamahatab4694b72010-11-19 18:56:00 +0900269 }
270 return 0;
271}
272
Isaku Yamahata5af0a042010-11-19 18:56:01 +0900273void qdev_reset_all(DeviceState *dev)
274{
Paolo Bonzinidcc20932013-12-06 17:54:27 +0100275 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
Isaku Yamahata5af0a042010-11-19 18:56:01 +0900276}
277
David Hildenbrandff8de072015-07-21 08:32:07 +0200278void qdev_reset_all_fn(void *opaque)
279{
280 qdev_reset_all(DEVICE(opaque));
281}
282
Paolo Bonzinid0508c32013-01-10 15:49:07 +0100283void qbus_reset_all(BusState *bus)
284{
Paolo Bonzinidcc20932013-12-06 17:54:27 +0100285 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
Paolo Bonzinid0508c32013-01-10 15:49:07 +0100286}
287
Isaku Yamahata80376c32010-12-20 14:33:35 +0900288void qbus_reset_all_fn(void *opaque)
289{
290 BusState *bus = opaque;
Paolo Bonzinid0508c32013-01-10 15:49:07 +0100291 qbus_reset_all(bus);
Isaku Yamahata80376c32010-12-20 14:33:35 +0900292}
293
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200294/* can be used as ->unplug() callback for the simple cases */
Igor Mammedov014176f2014-09-26 09:28:21 +0000295void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
296 DeviceState *dev, Error **errp)
297{
David Hildenbrand07578b02019-02-28 13:28:47 +0100298 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
Igor Mammedov014176f2014-09-26 09:28:21 +0000299}
Michael Tokarev3b29a102011-04-06 17:51:59 +0400300
Markus Armbruster0210afe2015-06-19 16:17:22 +0200301/*
302 * Realize @dev.
303 * Device properties should be set before calling this function. IRQs
304 * and MMIO regions should be connected/mapped after calling this
305 * function.
306 * On failure, report an error with error_report() and terminate the
307 * program. This is okay during machine creation. Don't use for
308 * hotplug, because there callers need to recover from failure.
309 * Exception: if you know the device's init() callback can't fail,
310 * then qdev_init_nofail() can't fail either, and is therefore usable
311 * even then. But relying on the device implementation that way is
312 * somewhat unclean, and best avoided.
313 */
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200314void qdev_init_nofail(DeviceState *dev)
315{
Markus Armbrusterc4bacaf2015-02-04 18:33:01 +0100316 Error *err = NULL;
Anthony Liguori7de3abe2012-06-27 07:37:54 -0500317
Markus Armbrusterc4bacaf2015-02-04 18:33:01 +0100318 assert(!dev->realized);
319
Fam Zheng0d4104e2016-08-02 11:41:41 +0800320 object_ref(OBJECT(dev));
Markus Armbrusterc4bacaf2015-02-04 18:33:01 +0100321 object_property_set_bool(OBJECT(dev), true, "realized", &err);
322 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100323 error_reportf_err(err, "Initialization of device %s failed: ",
324 object_get_typename(OBJECT(dev)));
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200325 exit(1);
326 }
Fam Zheng0d4104e2016-08-02 11:41:41 +0800327 object_unref(OBJECT(dev));
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200328}
329
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200330void qdev_machine_creation_done(void)
331{
332 /*
333 * ok, initial machine setup is done, starting from now we can
334 * only create hotpluggable devices
335 */
Juan Quintela9bed84c2017-03-28 11:08:52 +0200336 qdev_hotplug = true;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200337}
338
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700339bool qdev_machine_modified(void)
340{
341 return qdev_hot_added || qdev_hot_removed;
342}
343
Paul Brook02e2da42009-05-23 00:05:19 +0100344BusState *qdev_get_parent_bus(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100345{
Paul Brook02e2da42009-05-23 00:05:19 +0100346 return dev->parent_bus;
Paul Brookaae94602009-05-14 22:35:06 +0100347}
348
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700349static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
350 const char *name)
351{
352 NamedGPIOList *ngl;
353
354 QLIST_FOREACH(ngl, &dev->gpios, node) {
355 /* NULL is a valid and matchable name, otherwise do a normal
356 * strcmp match.
357 */
358 if ((!ngl->name && !name) ||
359 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
360 return ngl;
361 }
362 }
363
364 ngl = g_malloc0(sizeof(*ngl));
365 ngl->name = g_strdup(name);
366 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
367 return ngl;
368}
369
Peter Maydell4a151672018-03-02 10:45:38 +0000370void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
371 qemu_irq_handler handler,
372 void *opaque,
373 const char *name, int n)
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700374{
Peter Crosthwaitea69bef12014-09-25 22:17:41 -0700375 int i;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700376 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
377
Peter Crosthwaiteb235a712014-09-25 22:17:08 -0700378 assert(gpio_list->num_out == 0 || !name);
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700379 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
Peter Maydell4a151672018-03-02 10:45:38 +0000380 opaque, n);
Peter Crosthwaitea69bef12014-09-25 22:17:41 -0700381
Pavel Fedin6c76b372015-07-31 15:23:22 +0300382 if (!name) {
383 name = "unnamed-gpio-in";
384 }
Peter Crosthwaitea69bef12014-09-25 22:17:41 -0700385 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
Pavel Fedin6c76b372015-07-31 15:23:22 +0300386 gchar *propname = g_strdup_printf("%s[%u]", name, i);
387
Peter Crosthwaitea69bef12014-09-25 22:17:41 -0700388 object_property_add_child(OBJECT(dev), propname,
389 OBJECT(gpio_list->in[i]), &error_abort);
Pavel Fedin6c76b372015-07-31 15:23:22 +0300390 g_free(propname);
Peter Crosthwaitea69bef12014-09-25 22:17:41 -0700391 }
Peter Crosthwaitea69bef12014-09-25 22:17:41 -0700392
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700393 gpio_list->num_in += n;
394}
395
Paul Brookaae94602009-05-14 22:35:06 +0100396void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
397{
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700398 qdev_init_gpio_in_named(dev, handler, NULL, n);
399}
400
401void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
402 const char *name, int n)
403{
Peter Crosthwaite688b0572014-09-25 22:18:14 -0700404 int i;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700405 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
406
Peter Crosthwaiteb235a712014-09-25 22:17:08 -0700407 assert(gpio_list->num_in == 0 || !name);
Peter Crosthwaite688b0572014-09-25 22:18:14 -0700408
Pavel Fedin6c76b372015-07-31 15:23:22 +0300409 if (!name) {
410 name = "unnamed-gpio-out";
411 }
412 memset(pins, 0, sizeof(*pins) * n);
Peter Crosthwaite688b0572014-09-25 22:18:14 -0700413 for (i = 0; i < n; ++i) {
Pavel Fedin6c76b372015-07-31 15:23:22 +0300414 gchar *propname = g_strdup_printf("%s[%u]", name,
415 gpio_list->num_out + i);
416
Peter Crosthwaite688b0572014-09-25 22:18:14 -0700417 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
418 (Object **)&pins[i],
419 object_property_allow_set_link,
Marc-André Lureau265b5782018-05-31 21:51:17 +0200420 OBJ_PROP_LINK_STRONG,
Peter Crosthwaite688b0572014-09-25 22:18:14 -0700421 &error_abort);
Pavel Fedin6c76b372015-07-31 15:23:22 +0300422 g_free(propname);
Peter Crosthwaite688b0572014-09-25 22:18:14 -0700423 }
Pavel Fedin6c76b372015-07-31 15:23:22 +0300424 gpio_list->num_out += n;
Paul Brookaae94602009-05-14 22:35:06 +0100425}
426
427void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
428{
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700429 qdev_init_gpio_out_named(dev, pins, NULL, n);
430}
431
432qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
433{
434 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
435
436 assert(n >= 0 && n < gpio_list->num_in);
437 return gpio_list->in[n];
Paul Brookaae94602009-05-14 22:35:06 +0100438}
439
440qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
441{
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700442 return qdev_get_gpio_in_named(dev, NULL, n);
443}
444
445void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
446 qemu_irq pin)
447{
Peter Crosthwaite02757df2014-09-25 22:20:25 -0700448 char *propname = g_strdup_printf("%s[%d]",
449 name ? name : "unnamed-gpio-out", n);
450 if (pin) {
451 /* We need a name for object_property_set_link to work. If the
452 * object has a parent, object_property_add_child will come back
453 * with an error without doing anything. If it has none, it will
454 * never fail. So we can just call it with a NULL Error pointer.
455 */
Andreas Färber88950ee2015-03-12 16:09:34 +0100456 object_property_add_child(container_get(qdev_get_machine(),
457 "/unattached"),
458 "non-qdev-gpio[*]", OBJECT(pin), NULL);
Peter Crosthwaite02757df2014-09-25 22:20:25 -0700459 }
460 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
461 g_free(propname);
Paul Brookaae94602009-05-14 22:35:06 +0100462}
Paul Brookaae94602009-05-14 22:35:06 +0100463
Alexander Grafb7973182014-09-24 12:32:17 +0200464qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
465{
466 char *propname = g_strdup_printf("%s[%d]",
467 name ? name : "unnamed-gpio-out", n);
468
469 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
470 NULL);
471
472 return ret;
473}
474
Veres Lajos67cc32e2015-09-08 22:45:14 +0100475/* disconnect a GPIO output, returning the disconnected input (if any) */
Peter Crosthwaite0c24db22014-09-25 22:20:58 -0700476
477static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
478 const char *name, int n)
479{
480 char *propname = g_strdup_printf("%s[%d]",
481 name ? name : "unnamed-gpio-out", n);
482
483 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
484 NULL);
485 if (ret) {
486 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
487 }
488 g_free(propname);
489 return ret;
490}
491
492qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
493 const char *name, int n)
494{
495 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
496 qdev_connect_gpio_out_named(dev, name, n, icpt);
497 return disconnected;
Paul Brookaae94602009-05-14 22:35:06 +0100498}
499
500void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
501{
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700502 qdev_connect_gpio_out_named(dev, NULL, n, pin);
Paul Brookaae94602009-05-14 22:35:06 +0100503}
504
Peter Crosthwaite17a96a12014-09-25 22:23:42 -0700505void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
506 const char *name)
507{
508 int i;
509 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
510
511 for (i = 0; i < ngl->num_in; i++) {
512 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
513 char *propname = g_strdup_printf("%s[%d]", nm, i);
514
515 object_property_add_alias(OBJECT(container), propname,
516 OBJECT(dev), propname,
517 &error_abort);
Eduardo Habkost6bc5cf92015-04-09 16:57:30 -0300518 g_free(propname);
Peter Crosthwaite17a96a12014-09-25 22:23:42 -0700519 }
520 for (i = 0; i < ngl->num_out; i++) {
521 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
522 char *propname = g_strdup_printf("%s[%d]", nm, i);
523
524 object_property_add_alias(OBJECT(container), propname,
525 OBJECT(dev), propname,
526 &error_abort);
Eduardo Habkost6bc5cf92015-04-09 16:57:30 -0300527 g_free(propname);
Peter Crosthwaite17a96a12014-09-25 22:23:42 -0700528 }
529 QLIST_REMOVE(ngl, node);
530 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
531}
532
Paul Brook02e2da42009-05-23 00:05:19 +0100533BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
Paul Brook4d6ae672009-05-14 22:35:06 +0100534{
Paul Brook02e2da42009-05-23 00:05:19 +0100535 BusState *bus;
Peter Crosthwaitef698c8b2016-01-21 14:15:03 +0000536 Object *child = object_resolve_path_component(OBJECT(dev), name);
537
538 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
539 if (bus) {
540 return bus;
541 }
Paul Brook4d6ae672009-05-14 22:35:06 +0100542
Blue Swirl72cf2d42009-09-12 07:36:22 +0000543 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
Paul Brook4d6ae672009-05-14 22:35:06 +0100544 if (strcmp(name, bus->name) == 0) {
Paul Brook02e2da42009-05-23 00:05:19 +0100545 return bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100546 }
547 }
548 return NULL;
549}
550
Paolo Bonzini02932142013-12-06 17:54:26 +0100551int qdev_walk_children(DeviceState *dev,
552 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
553 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
554 void *opaque)
Anthony Liguori81699d82010-11-19 18:55:58 +0900555{
556 BusState *bus;
557 int err;
558
Paolo Bonzini02932142013-12-06 17:54:26 +0100559 if (pre_devfn) {
560 err = pre_devfn(dev, opaque);
Anthony Liguori81699d82010-11-19 18:55:58 +0900561 if (err) {
562 return err;
563 }
564 }
565
566 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
Paolo Bonzini02932142013-12-06 17:54:26 +0100567 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
568 post_devfn, post_busfn, opaque);
Anthony Liguori81699d82010-11-19 18:55:58 +0900569 if (err < 0) {
570 return err;
571 }
572 }
573
Paolo Bonzini02932142013-12-06 17:54:26 +0100574 if (post_devfn) {
575 err = post_devfn(dev, opaque);
576 if (err) {
577 return err;
578 }
579 }
580
Anthony Liguori81699d82010-11-19 18:55:58 +0900581 return 0;
582}
583
Isaku Yamahataa2ee6b42010-12-24 12:14:12 +0900584DeviceState *qdev_find_recursive(BusState *bus, const char *id)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200585{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600586 BusChild *kid;
587 DeviceState *ret;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200588 BusState *child;
589
Anthony Liguori0866aca2011-12-23 15:34:39 -0600590 QTAILQ_FOREACH(kid, &bus->children, sibling) {
591 DeviceState *dev = kid->child;
592
593 if (dev->id && strcmp(dev->id, id) == 0) {
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200594 return dev;
Anthony Liguori0866aca2011-12-23 15:34:39 -0600595 }
596
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200597 QLIST_FOREACH(child, &dev->child_bus, sibling) {
598 ret = qdev_find_recursive(child, id);
599 if (ret) {
600 return ret;
601 }
602 }
603 }
604 return NULL;
605}
606
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600607char *qdev_get_dev_path(DeviceState *dev)
Anthony Liguoricd34d662011-12-12 14:29:43 -0600608{
Anthony Liguori0d936922012-05-02 09:00:20 +0200609 BusClass *bc;
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600610
611 if (!dev || !dev->parent_bus) {
612 return NULL;
613 }
614
Anthony Liguori0d936922012-05-02 09:00:20 +0200615 bc = BUS_GET_CLASS(dev->parent_bus);
616 if (bc->get_dev_path) {
617 return bc->get_dev_path(dev);
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600618 }
619
620 return NULL;
Anthony Liguori44677de2011-12-12 14:29:26 -0600621}
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600622
623/**
624 * Legacy property handling
625 */
626
Eric Blaked7bce992016-01-29 06:48:55 -0700627static void qdev_get_legacy_property(Object *obj, Visitor *v,
628 const char *name, void *opaque,
629 Error **errp)
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600630{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600631 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600632 Property *prop = opaque;
633
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100634 char buffer[1024];
635 char *ptr = buffer;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600636
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100637 prop->info->print(dev, prop, buffer, sizeof(buffer));
Eric Blake51e72bc2016-01-29 06:48:54 -0700638 visit_type_str(v, name, &ptr, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600639}
640
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600641/**
Cao jind9d8d452016-04-17 15:45:54 +0800642 * qdev_property_add_legacy:
643 * @dev: Device to add the property to.
644 * @prop: The qdev property definition.
645 * @errp: location to store error information.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600646 *
Cao jind9d8d452016-04-17 15:45:54 +0800647 * Add a legacy QOM property to @dev for qdev property @prop.
648 * On error, store error in @errp.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600649 *
Cao jind9d8d452016-04-17 15:45:54 +0800650 * Legacy properties are string versions of QOM properties. The format of
651 * the string depends on the property type. Legacy properties are only
652 * needed for "info qtree".
653 *
Li Qiang6871a0d2018-09-04 23:49:01 -0700654 * Do not use this in new code! QOM Properties added through this interface
Cao jind9d8d452016-04-17 15:45:54 +0800655 * will be given names in the "legacy" namespace.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600656 */
Stefan Weilf5a014d2014-05-02 22:22:57 +0200657static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
658 Error **errp)
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600659{
Paolo Bonzini7ce7ffe2014-02-08 11:01:48 +0100660 gchar *name;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600661
Anthony Liguorif3be0162012-05-02 13:31:07 +0200662 /* Register pointer properties as legacy properties */
Paolo Bonzini03ff7772014-02-08 11:01:47 +0100663 if (!prop->info->print && prop->info->get) {
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100664 return;
665 }
Anthony Liguorif3be0162012-05-02 13:31:07 +0200666
Fam Zhengfaabdbb2017-07-14 10:14:51 +0800667 if (prop->info->create) {
668 return;
669 }
670
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100671 name = g_strdup_printf("legacy-%s", prop->name);
Paolo Bonzini7ce7ffe2014-02-08 11:01:48 +0100672 object_property_add(OBJECT(dev), name, "str",
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100673 prop->info->print ? qdev_get_legacy_property : prop->info->get,
Paolo Bonzini03ff7772014-02-08 11:01:47 +0100674 NULL,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600675 NULL,
676 prop, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600677
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100678 g_free(name);
679}
680
681/**
Cao jind9d8d452016-04-17 15:45:54 +0800682 * qdev_property_add_static:
683 * @dev: Device to add the property to.
684 * @prop: The qdev property definition.
685 * @errp: location to store error information.
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100686 *
Cao jind9d8d452016-04-17 15:45:54 +0800687 * Add a static QOM property to @dev for qdev property @prop.
688 * On error, store error in @errp. Static properties access data in a struct.
689 * The type of the QOM property is derived from prop->info.
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100690 */
691void qdev_property_add_static(DeviceState *dev, Property *prop,
692 Error **errp)
693{
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200694 Error *local_err = NULL;
695 Object *obj = OBJECT(dev);
696
Fam Zhengfaabdbb2017-07-14 10:14:51 +0800697 if (prop->info->create) {
698 prop->info->create(obj, prop, &local_err);
699 } else {
700 /*
701 * TODO qdev_prop_ptr does not have getters or setters. It must
702 * go now that it can be replaced with links. The test should be
703 * removed along with it: all static properties are read/write.
704 */
705 if (!prop->info->get && !prop->info->set) {
706 return;
707 }
708 object_property_add(obj, prop->name, prop->info->name,
709 prop->info->get, prop->info->set,
710 prop->info->release,
711 prop, &local_err);
Paolo Bonzinid8229792012-02-02 09:47:13 +0100712 }
713
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200714 if (local_err) {
715 error_propagate(errp, local_err);
716 return;
717 }
Gongleib8c9cd52014-10-07 14:33:22 +0800718
719 object_property_set_description(obj, prop->name,
720 prop->info->description,
721 &error_abort);
722
Peter Maydell5cc56cc2017-07-17 13:36:06 +0100723 if (prop->set_default) {
Marc-André Lureaua2740ad2017-06-07 20:35:53 +0400724 prop->info->set_default_value(obj, prop);
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200725 }
Anthony Liguori6a146eb2011-12-12 14:29:42 -0600726}
Anthony Liguori1de81d22011-12-19 16:37:46 -0600727
Stefan Hajnoczi67cc7e02014-06-18 17:58:32 +0800728/* @qdev_alias_all_properties - Add alias properties to the source object for
729 * all qdev properties on the target DeviceState.
730 */
731void qdev_alias_all_properties(DeviceState *target, Object *source)
732{
733 ObjectClass *class;
734 Property *prop;
735
736 class = object_get_class(OBJECT(target));
737 do {
738 DeviceClass *dc = DEVICE_CLASS(class);
739
740 for (prop = dc->props; prop && prop->name; prop++) {
741 object_property_add_alias(source, prop->name,
742 OBJECT(target), prop->name,
743 &error_abort);
744 }
745 class = object_class_get_parent(class);
746 } while (class != object_class_by_name(TYPE_DEVICE));
747}
748
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +0200749static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
Zhu Guihua66e56b12014-10-21 19:46:04 +0800750{
751 GSList **list = opaque;
Jun Li09d56012014-11-05 15:03:03 +0800752 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
753 TYPE_DEVICE);
754
755 if (dev == NULL) {
756 return 0;
757 }
Zhu Guihua66e56b12014-10-21 19:46:04 +0800758
759 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
760 *list = g_slist_append(*list, dev);
761 }
762
Zhu Guihua66e56b12014-10-21 19:46:04 +0800763 return 0;
764}
765
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +0200766GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
767{
768 GSList *list = NULL;
769
770 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
771
772 return list;
773}
774
Markus Armbrustera7737e42014-04-25 12:44:21 +0200775static bool device_get_realized(Object *obj, Error **errp)
Andreas Färber249d4172013-01-09 03:58:11 +0100776{
777 DeviceState *dev = DEVICE(obj);
778 return dev->realized;
779}
780
Juan Quintela1bfe5f02017-04-17 14:57:54 +0200781static bool check_only_migratable(Object *obj, Error **err)
782{
783 DeviceClass *dc = DEVICE_GET_CLASS(obj);
784
785 if (!vmstate_check_only_migratable(dc->vmsd)) {
786 error_setg(err, "Device %s is not migratable, but "
787 "--only-migratable was specified",
788 object_get_typename(obj));
789 return false;
790 }
791
792 return true;
793}
794
Markus Armbrustera7737e42014-04-25 12:44:21 +0200795static void device_set_realized(Object *obj, bool value, Error **errp)
Andreas Färber249d4172013-01-09 03:58:11 +0100796{
797 DeviceState *dev = DEVICE(obj);
798 DeviceClass *dc = DEVICE_GET_CLASS(dev);
Igor Mammedov7716b8c2014-09-26 09:28:41 +0000799 HotplugHandler *hotplug_ctrl;
Bandan Das5c21ce72014-03-12 21:02:12 +0100800 BusState *bus;
Andreas Färber249d4172013-01-09 03:58:11 +0100801 Error *local_err = NULL;
Igor Mammedov69382d82016-07-25 11:59:22 +0200802 bool unattached_parent = false;
803 static int unattached_count;
Andreas Färber249d4172013-01-09 03:58:11 +0100804
Igor Mammedov1a37eca2014-02-05 16:36:46 +0100805 if (dev->hotplugged && !dc->hotpluggable) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100806 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
Igor Mammedov1a37eca2014-02-05 16:36:46 +0100807 return;
808 }
809
Andreas Färber249d4172013-01-09 03:58:11 +0100810 if (value && !dev->realized) {
Juan Quintela1bfe5f02017-04-17 14:57:54 +0200811 if (!check_only_migratable(obj, &local_err)) {
Ashijeet Acharya7562f902017-02-13 23:34:48 +0530812 goto fail;
813 }
814
Gongleid5780292014-09-02 20:03:05 +0800815 if (!obj->parent) {
Andreas Färber249d4172013-01-09 03:58:11 +0100816 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
817
818 object_property_add_child(container_get(qdev_get_machine(),
819 "/unattached"),
Gongleid5780292014-09-02 20:03:05 +0800820 name, obj, &error_abort);
Igor Mammedov69382d82016-07-25 11:59:22 +0200821 unattached_parent = true;
Andreas Färber249d4172013-01-09 03:58:11 +0100822 g_free(name);
823 }
824
Igor Mammedov41346262016-05-12 09:18:15 +0530825 hotplug_ctrl = qdev_get_hotplug_handler(dev);
826 if (hotplug_ctrl) {
827 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
828 if (local_err != NULL) {
829 goto fail;
830 }
831 }
832
Igor Mammedova7ddba52013-04-11 16:51:56 +0200833 if (dc->realize) {
834 dc->realize(dev, &local_err);
835 }
836
Gonglei1d45a702014-09-04 10:18:26 +0800837 if (local_err != NULL) {
838 goto fail;
839 }
840
Paul Durrant707ff802015-01-20 11:05:07 +0000841 DEVICE_LISTENER_CALL(realize, Forward, dev);
842
Michael Roth04162f82017-10-16 17:23:13 -0500843 /*
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
Gonglei1d45a702014-09-04 10:18:26 +0800850 if (qdev_get_vmsd(dev)) {
Dr. David Alan Gilbert67980032017-02-02 12:59:56 +0000851 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 }
Andreas Färber249d4172013-01-09 03:58:11 +0100857 }
Gonglei1d45a702014-09-04 10:18:26 +0800858
859 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
860 object_property_set_bool(OBJECT(bus), true, "realized",
Bandan Das5c21ce72014-03-12 21:02:12 +0100861 &local_err);
Gonglei1d45a702014-09-04 10:18:26 +0800862 if (local_err != NULL) {
863 goto child_realize_fail;
Bandan Das5c21ce72014-03-12 21:02:12 +0100864 }
865 }
Gonglei1d45a702014-09-04 10:18:26 +0800866 if (dev->hotplugged) {
Andreas Färber249d4172013-01-09 03:58:11 +0100867 device_reset(dev);
868 }
Paolo Bonzini352e8da2014-06-26 15:10:03 +0200869 dev->pending_deleted_event = false;
Stefan Hajnoczi25e89782018-07-16 09:37:31 +0100870
871 if (hotplug_ctrl) {
Igor Mammedov8b5e6ca2018-10-16 15:33:40 +0200872 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
873 if (local_err != NULL) {
874 goto child_realize_fail;
875 }
876 }
877
Andreas Färber249d4172013-01-09 03:58:11 +0100878 } else if (!value && dev->realized) {
Gongleicd4520a2014-09-04 10:18:25 +0800879 Error **local_errp = NULL;
Bandan Das5c21ce72014-03-12 21:02:12 +0100880 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
Gongleicd4520a2014-09-04 10:18:25 +0800881 local_errp = local_err ? NULL : &local_err;
Bandan Das5c21ce72014-03-12 21:02:12 +0100882 object_property_set_bool(OBJECT(bus), false, "realized",
Gongleicd4520a2014-09-04 10:18:25 +0800883 local_errp);
Bandan Das5c21ce72014-03-12 21:02:12 +0100884 }
Gongleicd4520a2014-09-04 10:18:25 +0800885 if (qdev_get_vmsd(dev)) {
Andreas Färberfe6c2112013-04-15 18:34:10 +0200886 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
887 }
Gongleicd4520a2014-09-04 10:18:25 +0800888 if (dc->unrealize) {
889 local_errp = local_err ? NULL : &local_err;
890 dc->unrealize(dev, local_errp);
Andreas Färber249d4172013-01-09 03:58:11 +0100891 }
Paolo Bonzini352e8da2014-06-26 15:10:03 +0200892 dev->pending_deleted_event = true;
Paul Durrant707ff802015-01-20 11:05:07 +0000893 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
Andreas Färber249d4172013-01-09 03:58:11 +0100894 }
895
Xiao Guangrongc7f8d0f2016-11-07 19:13:36 +0800896 if (local_err != NULL) {
897 goto fail;
898 }
899
900 dev->realized = value;
Gonglei1d45a702014-09-04 10:18:26 +0800901 return;
902
903child_realize_fail:
904 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
905 object_property_set_bool(OBJECT(bus), false, "realized",
906 NULL);
907 }
908
909 if (qdev_get_vmsd(dev)) {
910 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
911 }
912
913post_realize_fail:
Michael Roth04162f82017-10-16 17:23:13 -0500914 g_free(dev->canonical_path);
915 dev->canonical_path = NULL;
Gonglei1d45a702014-09-04 10:18:26 +0800916 if (dc->unrealize) {
917 dc->unrealize(dev, NULL);
918 }
919
920fail:
921 error_propagate(errp, local_err);
Igor Mammedov69382d82016-07-25 11:59:22 +0200922 if (unattached_parent) {
923 object_unparent(OBJECT(dev));
924 unattached_count--;
925 }
Andreas Färber249d4172013-01-09 03:58:11 +0100926}
927
Markus Armbrustera7737e42014-04-25 12:44:21 +0200928static bool device_get_hotpluggable(Object *obj, Error **errp)
Igor Mammedov1a37eca2014-02-05 16:36:46 +0100929{
930 DeviceClass *dc = DEVICE_GET_CLASS(obj);
931 DeviceState *dev = DEVICE(obj);
932
Andreas Färber2b81b352014-03-07 19:04:13 +0100933 return dc->hotpluggable && (dev->parent_bus == NULL ||
Igor Mammedov39b888b2014-09-26 09:28:17 +0000934 qbus_is_hotpluggable(dev->parent_bus));
Igor Mammedov1a37eca2014-02-05 16:36:46 +0100935}
936
Igor Mammedovd012ffc2014-06-02 15:25:04 +0200937static bool device_get_hotplugged(Object *obj, Error **err)
938{
939 DeviceState *dev = DEVICE(obj);
940
941 return dev->hotplugged;
942}
943
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600944static void device_initfn(Object *obj)
945{
946 DeviceState *dev = DEVICE(obj);
Paolo Bonzinibce54472012-03-28 18:12:47 +0200947 ObjectClass *class;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600948 Property *prop;
949
950 if (qdev_hotplug) {
951 dev->hotplugged = 1;
952 qdev_hot_added = true;
953 }
954
955 dev->instance_id_alias = -1;
Andreas Färber7983c8a2013-01-09 03:58:10 +0100956 dev->realized = false;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600957
Andreas Färber249d4172013-01-09 03:58:11 +0100958 object_property_add_bool(obj, "realized",
959 device_get_realized, device_set_realized, NULL);
Igor Mammedov1a37eca2014-02-05 16:36:46 +0100960 object_property_add_bool(obj, "hotpluggable",
961 device_get_hotpluggable, NULL, NULL);
Igor Mammedovd012ffc2014-06-02 15:25:04 +0200962 object_property_add_bool(obj, "hotplugged",
Eduardo Habkost36cccb82017-02-22 16:26:47 -0300963 device_get_hotplugged, NULL,
Igor Mammedovd012ffc2014-06-02 15:25:04 +0200964 &error_abort);
Andreas Färber249d4172013-01-09 03:58:11 +0100965
Paolo Bonzinibce54472012-03-28 18:12:47 +0200966 class = object_get_class(OBJECT(dev));
967 do {
968 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
Peter Crosthwaite5433a0a2014-01-01 18:48:08 -0800969 qdev_property_add_legacy(dev, prop, &error_abort);
970 qdev_property_add_static(dev, prop, &error_abort);
Paolo Bonzinibce54472012-03-28 18:12:47 +0200971 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200972 class = object_class_get_parent(class);
973 } while (class != object_class_by_name(TYPE_DEVICE));
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600974
Anthony Liguorif968fc62012-05-02 10:39:01 +0200975 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +0100976 (Object **)&dev->parent_bus, NULL, 0,
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +0100977 &error_abort);
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700978 QLIST_INIT(&dev->gpios);
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600979}
980
Markus Armbruster1a3ec8c2019-03-08 14:14:34 +0100981/*
982 * Global property defaults
983 * Slot 0: accelerator's global property defaults
984 * Slot 1: machine's global property defaults
985 * Each is a GPtrArray of of GlobalProperty.
986 * Applied in order, later entries override earlier ones.
987 */
988static GPtrArray *object_compat_props[2];
989
990/*
991 * Set machine's global property defaults to @compat_props.
992 * May be called at most once.
993 */
994void object_set_machine_compat_props(GPtrArray *compat_props)
995{
996 assert(!object_compat_props[1]);
997 object_compat_props[1] = compat_props;
998}
999
1000/*
1001 * Set accelerator's global property defaults to @compat_props.
1002 * May be called at most once.
1003 */
1004void object_set_accelerator_compat_props(GPtrArray *compat_props)
1005{
1006 assert(!object_compat_props[0]);
1007 object_compat_props[0] = compat_props;
1008}
1009
Marc-André Lureau1c3994f2018-12-02 01:23:27 +04001010void object_apply_compat_props(Object *obj)
Eduardo Habkost99a0b032013-07-10 17:08:42 -03001011{
Markus Armbruster1a3ec8c2019-03-08 14:14:34 +01001012 int i;
Marc-André Lureauea9ce892018-11-26 22:04:32 +04001013
Markus Armbruster1a3ec8c2019-03-08 14:14:34 +01001014 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
1015 object_apply_global_props(obj, object_compat_props[i],
1016 &error_abort);
Marc-André Lureauea9ce892018-11-26 22:04:32 +04001017 }
Marc-André Lureau1c3994f2018-12-02 01:23:27 +04001018}
Marc-André Lureauea9ce892018-11-26 22:04:32 +04001019
Marc-André Lureau1c3994f2018-12-02 01:23:27 +04001020static void device_post_init(Object *obj)
1021{
Markus Armbruster1a3ec8c2019-03-08 14:14:34 +01001022 /*
1023 * Note: ordered so that the user's global properties take
1024 * precedence.
1025 */
Marc-André Lureau1c3994f2018-12-02 01:23:27 +04001026 object_apply_compat_props(obj);
Markus Armbruster25f8dd92015-01-20 10:04:07 +01001027 qdev_prop_set_globals(DEVICE(obj));
Eduardo Habkost99a0b032013-07-10 17:08:42 -03001028}
1029
Anthony Liguori60adba32011-12-23 08:38:56 -06001030/* Unlink device from bus and free the structure. */
1031static void device_finalize(Object *obj)
1032{
Peter Crosthwaitea5f54292014-05-19 23:30:58 -07001033 NamedGPIOList *ngl, *next;
1034
Anthony Liguori60adba32011-12-23 08:38:56 -06001035 DeviceState *dev = DEVICE(obj);
Peter Crosthwaitea5f54292014-05-19 23:30:58 -07001036
1037 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1038 QLIST_REMOVE(ngl, node);
Peter Crosthwaitef173d572014-06-18 00:56:31 -07001039 qemu_free_irqs(ngl->in, ngl->num_in);
Peter Crosthwaitea5f54292014-05-19 23:30:58 -07001040 g_free(ngl->name);
1041 g_free(ngl);
1042 /* ngl->out irqs are owned by the other end and should not be freed
1043 * here
1044 */
1045 }
Michael Rothf7b879e2017-10-16 17:23:15 -05001046
1047 /* Only send event if the device had been completely realized */
1048 if (dev->pending_deleted_event) {
1049 g_assert(dev->canonical_path);
1050
Peter Xu3ab72382018-08-15 21:37:37 +08001051 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
Michael Rothf7b879e2017-10-16 17:23:15 -05001052 g_free(dev->canonical_path);
1053 dev->canonical_path = NULL;
1054 }
1055
1056 qemu_opts_del(dev->opts);
Anthony Liguori60adba32011-12-23 08:38:56 -06001057}
1058
Paolo Bonzinibce54472012-03-28 18:12:47 +02001059static void device_class_base_init(ObjectClass *class, void *data)
1060{
1061 DeviceClass *klass = DEVICE_CLASS(class);
1062
1063 /* We explicitly look up properties in the superclasses,
1064 * so do not propagate them to the subclasses.
1065 */
1066 klass->props = NULL;
Anthony Liguori1de81d22011-12-19 16:37:46 -06001067}
Anthony Liguori32fea402011-12-16 14:34:46 -06001068
Andreas Färber5d5b24d2013-01-04 18:13:00 +01001069static void device_unparent(Object *obj)
Paolo Bonzini667d22d2012-11-23 09:47:13 +01001070{
1071 DeviceState *dev = DEVICE(obj);
Paolo Bonzini06f7f2b2013-01-25 14:12:34 +01001072 BusState *bus;
Paolo Bonzini667d22d2012-11-23 09:47:13 +01001073
Bandan Das5c21ce72014-03-12 21:02:12 +01001074 if (dev->realized) {
1075 object_property_set_bool(obj, false, "realized", NULL);
1076 }
Paolo Bonzini06f7f2b2013-01-25 14:12:34 +01001077 while (dev->num_child_bus) {
1078 bus = QLIST_FIRST(&dev->child_bus);
Stefan Hajnoczi6780a222013-12-18 17:15:51 +01001079 object_unparent(OBJECT(bus));
Paolo Bonzini06f7f2b2013-01-25 14:12:34 +01001080 }
Paolo Bonzini06f7f2b2013-01-25 14:12:34 +01001081 if (dev->parent_bus) {
Andreas Färber5d5b24d2013-01-04 18:13:00 +01001082 bus_remove_child(dev->parent_bus, dev);
Paolo Bonzini62d7ba62013-01-25 14:12:35 +01001083 object_unref(OBJECT(dev->parent_bus));
1084 dev->parent_bus = NULL;
Andreas Färber5d5b24d2013-01-04 18:13:00 +01001085 }
Paolo Bonzini667d22d2012-11-23 09:47:13 +01001086}
1087
1088static void device_class_init(ObjectClass *class, void *data)
1089{
Andreas Färber249d4172013-01-09 03:58:11 +01001090 DeviceClass *dc = DEVICE_CLASS(class);
1091
Andreas Färber5d5b24d2013-01-04 18:13:00 +01001092 class->unparent = device_unparent;
Igor Mammedov267a3262014-02-18 17:56:53 +01001093
1094 /* by default all devices were considered as hotpluggable,
1095 * so with intent to check it in generic qdev_unplug() /
1096 * device_set_realized() functions make every device
1097 * hotpluggable. Devices that shouldn't be hotpluggable,
1098 * should override it in their class_init()
1099 */
1100 dc->hotpluggable = true;
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03001101 dc->user_creatable = true;
Paolo Bonzini667d22d2012-11-23 09:47:13 +01001102}
1103
Philippe Mathieu-Daudé46795cf2018-01-13 23:04:11 -03001104void device_class_set_parent_reset(DeviceClass *dc,
1105 DeviceReset dev_reset,
1106 DeviceReset *parent_reset)
1107{
1108 *parent_reset = dc->reset;
1109 dc->reset = dev_reset;
1110}
1111
1112void device_class_set_parent_realize(DeviceClass *dc,
1113 DeviceRealize dev_realize,
1114 DeviceRealize *parent_realize)
1115{
1116 *parent_realize = dc->realize;
1117 dc->realize = dev_realize;
1118}
1119
1120void device_class_set_parent_unrealize(DeviceClass *dc,
1121 DeviceUnrealize dev_unrealize,
1122 DeviceUnrealize *parent_unrealize)
1123{
1124 *parent_unrealize = dc->unrealize;
1125 dc->unrealize = dev_unrealize;
1126}
1127
Anthony Liguori94afdad2011-12-04 11:36:01 -06001128void device_reset(DeviceState *dev)
1129{
1130 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1131
1132 if (klass->reset) {
1133 klass->reset(dev);
1134 }
1135}
1136
Paolo Bonzinif05f6b42012-03-28 16:34:12 +02001137Object *qdev_get_machine(void)
1138{
1139 static Object *dev;
1140
1141 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +02001142 dev = container_get(object_get_root(), "/machine");
Paolo Bonzinif05f6b42012-03-28 16:34:12 +02001143 }
1144
1145 return dev;
1146}
1147
Andreas Färber8c43a6f2013-01-10 16:19:07 +01001148static const TypeInfo device_type_info = {
Anthony Liguori32fea402011-12-16 14:34:46 -06001149 .name = TYPE_DEVICE,
1150 .parent = TYPE_OBJECT,
1151 .instance_size = sizeof(DeviceState),
Anthony Liguori9674bfe2011-12-22 15:06:37 -06001152 .instance_init = device_initfn,
Eduardo Habkost99a0b032013-07-10 17:08:42 -03001153 .instance_post_init = device_post_init,
Anthony Liguori60adba32011-12-23 08:38:56 -06001154 .instance_finalize = device_finalize,
Paolo Bonzinibce54472012-03-28 18:12:47 +02001155 .class_base_init = device_class_base_init,
Paolo Bonzini667d22d2012-11-23 09:47:13 +01001156 .class_init = device_class_init,
Anthony Liguori32fea402011-12-16 14:34:46 -06001157 .abstract = true,
1158 .class_size = sizeof(DeviceClass),
1159};
1160
Andreas Färber83f7d432012-02-09 15:20:55 +01001161static void qdev_register_types(void)
Anthony Liguori32fea402011-12-16 14:34:46 -06001162{
1163 type_register_static(&device_type_info);
1164}
1165
Andreas Färber83f7d432012-02-09 15:20:55 +01001166type_init(qdev_register_types)