blob: a954c2192b01aa36c60be1107ba2e22b4392ec35 [file] [log] [blame]
Anthony Liguoriee46d8a2011-12-22 15:24:20 -06001/*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010021#include "hw/qdev.h"
Andreas Färber2f7bd822013-04-16 03:50:21 +020022#include "hw/sysbus.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010023#include "monitor/monitor.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010024#include "monitor/qdev.h"
Luiz Capitulinoa15fef22012-03-29 12:38:50 -030025#include "qmp-commands.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/arch_init.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010027#include "qapi/error.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010028#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010029#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010031#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020032#include "qemu/help_option.h"
Kevin Wolf9680cae2016-09-20 13:38:42 +020033#include "sysemu/block-backend.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020034#include "migration/misc.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060035
36/*
37 * Aliases were a bad idea from the start. Let's keep them
38 * from spreading further.
39 */
40typedef struct QDevAlias
41{
42 const char *typename;
43 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020044 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060045} QDevAlias;
46
Sascha Silbe36e99162016-02-18 22:44:13 +010047/* Please keep this table sorted by typename. */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060048static const QDevAlias qdev_alias_table[] = {
Sascha Silbe36e99162016-02-18 22:44:13 +010049 { "e1000", "e1000-82540em" },
50 { "ich9-ahci", "ahci" },
Sascha Silbe36e99162016-02-18 22:44:13 +010051 { "lsi53c895a", "lsi" },
Sascha Silbe588c36c2016-02-18 22:44:14 +010052 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
53 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
54 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
Alexander Graf5f629d92012-05-18 02:36:26 +020055 { "virtio-balloon-pci", "virtio-balloon",
56 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020057 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010058 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010059 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
60 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
61 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
62 { "virtio-input-host-pci", "virtio-input-host",
63 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
64 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
65 { "virtio-keyboard-pci", "virtio-keyboard",
66 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
67 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
68 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020069 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010070 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010071 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
72 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
73 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
74 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020075 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010076 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010077 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
78 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060079 { }
80};
81
82static const char *qdev_class_get_alias(DeviceClass *dc)
83{
84 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
85 int i;
86
87 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +020088 if (qdev_alias_table[i].arch_mask &&
89 !(qdev_alias_table[i].arch_mask & arch_type)) {
90 continue;
91 }
92
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060093 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
94 return qdev_alias_table[i].alias;
95 }
96 }
97
98 return NULL;
99}
100
101static bool qdev_class_has_alias(DeviceClass *dc)
102{
103 return (qdev_class_get_alias(dc) != NULL);
104}
105
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200106static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600107{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200108 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +0200109 if (dc->bus_type) {
110 error_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600111 }
112 if (qdev_class_has_alias(dc)) {
113 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
114 }
115 if (dc->desc) {
116 error_printf(", desc \"%s\"", dc->desc);
117 }
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300118 if (!dc->user_creatable) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600119 error_printf(", no-user");
120 }
121 error_printf("\n");
122}
123
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200124static gint devinfo_cmp(gconstpointer a, gconstpointer b)
125{
126 return strcasecmp(object_class_get_name((ObjectClass *)a),
127 object_class_get_name((ObjectClass *)b));
128}
129
130static void qdev_print_devinfos(bool show_no_user)
131{
132 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
133 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
134 [DEVICE_CATEGORY_USB] = "USB",
135 [DEVICE_CATEGORY_STORAGE] = "Storage",
136 [DEVICE_CATEGORY_NETWORK] = "Network",
137 [DEVICE_CATEGORY_INPUT] = "Input",
138 [DEVICE_CATEGORY_DISPLAY] = "Display",
139 [DEVICE_CATEGORY_SOUND] = "Sound",
140 [DEVICE_CATEGORY_MISC] = "Misc",
Thomas Huthba31cc72017-01-20 14:01:16 +0100141 [DEVICE_CATEGORY_CPU] = "CPU",
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200142 [DEVICE_CATEGORY_MAX] = "Uncategorized",
143 };
144 GSList *list, *elt;
145 int i;
146 bool cat_printed;
147
148 list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
149 devinfo_cmp);
150
151 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
152 cat_printed = false;
153 for (elt = list; elt; elt = elt->next) {
154 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
155 TYPE_DEVICE);
156 if ((i < DEVICE_CATEGORY_MAX
157 ? !test_bit(i, dc->categories)
158 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100159 || (!show_no_user
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300160 && !dc->user_creatable)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200161 continue;
162 }
163 if (!cat_printed) {
164 error_printf("%s%s devices:\n", i ? "\n" : "",
165 cat_name[i]);
166 cat_printed = true;
167 }
168 qdev_print_devinfo(dc);
169 }
170 }
171
172 g_slist_free(list);
173}
174
Markus Armbruster71df1d82015-03-12 08:40:25 +0100175static int set_property(void *opaque, const char *name, const char *value,
176 Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600177{
Paolo Bonzini98a65282014-02-08 11:01:49 +0100178 Object *obj = opaque;
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200179 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600180
181 if (strcmp(name, "driver") == 0)
182 return 0;
183 if (strcmp(name, "bus") == 0)
184 return 0;
185
Paolo Bonzini98a65282014-02-08 11:01:49 +0100186 object_property_parse(obj, value, name, &err);
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200187 if (err != NULL) {
Markus Armbruster4caa4892015-03-12 13:58:02 +0100188 error_propagate(errp, err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600189 return -1;
190 }
191 return 0;
192}
193
194static const char *find_typename_by_alias(const char *alias)
195{
196 int i;
197
198 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200199 if (qdev_alias_table[i].arch_mask &&
200 !(qdev_alias_table[i].arch_mask & arch_type)) {
201 continue;
202 }
203
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600204 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
205 return qdev_alias_table[i].typename;
206 }
207 }
208
209 return NULL;
210}
211
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200212static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
213{
214 ObjectClass *oc;
215 DeviceClass *dc;
Sascha Silbef6b53192016-02-18 22:44:12 +0100216 const char *original_name = *driver;
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200217
218 oc = object_class_by_name(*driver);
219 if (!oc) {
220 const char *typename = find_typename_by_alias(*driver);
221
222 if (typename) {
223 *driver = typename;
224 oc = object_class_by_name(*driver);
225 }
226 }
227
228 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
Sascha Silbef6b53192016-02-18 22:44:12 +0100229 if (*driver != original_name) {
230 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
231 " name", original_name, *driver);
232 } else {
233 error_setg(errp, "'%s' is not a valid device model name", *driver);
234 }
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200235 return NULL;
236 }
237
238 if (object_class_is_abstract(oc)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100239 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
240 "non-abstract device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200241 return NULL;
242 }
243
244 dc = DEVICE_CLASS(oc);
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300245 if (!dc->user_creatable ||
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200246 (qdev_hotplug && !dc->hotpluggable)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100247 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
248 "pluggable device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200249 return NULL;
250 }
251
252 return dc;
253}
254
255
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600256int qdev_device_help(QemuOpts *opts)
257{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200258 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600259 const char *driver;
Stefan Hajnoczief523582014-07-09 14:01:32 +0200260 DevicePropertyInfoList *prop_list;
261 DevicePropertyInfoList *prop;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600262
263 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100264 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200265 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600266 return 1;
267 }
268
Peter Maydellc8057f92012-08-02 13:45:54 +0100269 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600270 return 0;
271 }
272
Markus Armbruster33fe9682015-10-01 10:59:59 +0200273 if (!object_class_by_name(driver)) {
274 const char *typename = find_typename_by_alias(driver);
275
276 if (typename) {
277 driver = typename;
278 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600279 }
280
Stefan Hajnoczief523582014-07-09 14:01:32 +0200281 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800282 if (local_err) {
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200283 goto error;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600284 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200285
286 for (prop = prop_list; prop; prop = prop->next) {
Gonglei07d09c52014-10-07 14:33:23 +0800287 error_printf("%s.%s=%s", driver,
Stefan Hajnoczief523582014-07-09 14:01:32 +0200288 prop->value->name,
289 prop->value->type);
Gonglei07d09c52014-10-07 14:33:23 +0800290 if (prop->value->has_description) {
291 error_printf(" (%s)\n", prop->value->description);
292 } else {
293 error_printf("\n");
294 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200295 }
296
297 qapi_free_DevicePropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600298 return 1;
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200299
300error:
Markus Armbruster78288672015-12-18 16:35:07 +0100301 error_report_err(local_err);
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200302 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600303}
304
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600305static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600306{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600307 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600308
309 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200310 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600311 }
312
Anthony Liguori8b45d442011-12-23 09:08:05 -0600313 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600314}
315
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600316static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600317{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600318 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600319
320 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200321 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600322 }
323
Anthony Liguori8b45d442011-12-23 09:08:05 -0600324 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600325}
326
Eric Blake50b7b002015-09-10 10:19:16 -0600327static void qbus_list_bus(DeviceState *dev, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600328{
329 BusState *child;
330 const char *sep = " ";
331
Eric Blake50b7b002015-09-10 10:19:16 -0600332 error_append_hint(errp, "child buses at \"%s\":",
333 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600334 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Eric Blake50b7b002015-09-10 10:19:16 -0600335 error_append_hint(errp, "%s\"%s\"", sep, child->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600336 sep = ", ";
337 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100338 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600339}
340
Eric Blake50b7b002015-09-10 10:19:16 -0600341static void qbus_list_dev(BusState *bus, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600342{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600343 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600344 const char *sep = " ";
345
Eric Blake50b7b002015-09-10 10:19:16 -0600346 error_append_hint(errp, "devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600347 QTAILQ_FOREACH(kid, &bus->children, sibling) {
348 DeviceState *dev = kid->child;
Eric Blake50b7b002015-09-10 10:19:16 -0600349 error_append_hint(errp, "%s\"%s\"", sep,
350 object_get_typename(OBJECT(dev)));
351 if (dev->id) {
352 error_append_hint(errp, "/\"%s\"", dev->id);
353 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600354 sep = ", ";
355 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100356 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600357}
358
359static BusState *qbus_find_bus(DeviceState *dev, char *elem)
360{
361 BusState *child;
362
363 QLIST_FOREACH(child, &dev->child_bus, sibling) {
364 if (strcmp(child->name, elem) == 0) {
365 return child;
366 }
367 }
368 return NULL;
369}
370
371static DeviceState *qbus_find_dev(BusState *bus, char *elem)
372{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600373 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600374
375 /*
376 * try to match in order:
377 * (1) instance id, if present
378 * (2) driver name
379 * (3) driver alias, if present
380 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600381 QTAILQ_FOREACH(kid, &bus->children, sibling) {
382 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600383 if (dev->id && strcmp(dev->id, elem) == 0) {
384 return dev;
385 }
386 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600387 QTAILQ_FOREACH(kid, &bus->children, sibling) {
388 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600389 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
390 return dev;
391 }
392 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600393 QTAILQ_FOREACH(kid, &bus->children, sibling) {
394 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600395 DeviceClass *dc = DEVICE_GET_CLASS(dev);
396
397 if (qdev_class_has_alias(dc) &&
398 strcmp(qdev_class_get_alias(dc), elem) == 0) {
399 return dev;
400 }
401 }
402 return NULL;
403}
404
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100405static inline bool qbus_is_full(BusState *bus)
406{
407 BusClass *bus_class = BUS_GET_CLASS(bus);
408 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
409}
410
411/*
412 * Search the tree rooted at @bus for a bus.
413 * If @name, search for a bus with that name. Note that bus names
414 * need not be unique. Yes, that's screwed up.
415 * Else search for a bus that is a subtype of @bus_typename.
416 * If more than one exists, prefer one that can take another device.
417 * Return the bus if found, else %NULL.
418 */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600419static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200420 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600421{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600422 BusChild *kid;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100423 BusState *pick, *child, *ret;
424 bool match;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600425
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100426 assert(name || bus_typename);
427 if (name) {
428 match = !strcmp(bus->name, name);
429 } else {
430 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
KONRAD Frederic1395af62013-01-15 00:08:00 +0100431 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100432
433 if (match && !qbus_is_full(bus)) {
434 return bus; /* root matches and isn't full */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600435 }
436
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100437 pick = match ? bus : NULL;
438
Anthony Liguori0866aca2011-12-23 15:34:39 -0600439 QTAILQ_FOREACH(kid, &bus->children, sibling) {
440 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600441 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200442 ret = qbus_find_recursive(child, name, bus_typename);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100443 if (ret && !qbus_is_full(ret)) {
444 return ret; /* a descendant matches and isn't full */
445 }
446 if (ret && !pick) {
447 pick = ret;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600448 }
449 }
450 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100451
452 /* root or a descendant matches, but is full */
453 return pick;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600454}
455
Markus Armbrusterd2828422015-03-11 19:16:04 +0100456static BusState *qbus_find(const char *path, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600457{
458 DeviceState *dev;
459 BusState *bus;
460 char elem[128];
461 int pos, len;
462
463 /* find start element */
464 if (path[0] == '/') {
465 bus = sysbus_get_default();
466 pos = 0;
467 } else {
468 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
469 assert(!path[0]);
470 elem[0] = len = 0;
471 }
472 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
473 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100474 error_setg(errp, "Bus '%s' not found", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600475 return NULL;
476 }
477 pos = len;
478 }
479
480 for (;;) {
481 assert(path[pos] == '/' || !path[pos]);
482 while (path[pos] == '/') {
483 pos++;
484 }
485 if (path[pos] == '\0') {
Markus Armbrustered238ba2015-03-11 18:39:16 +0100486 break;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600487 }
488
489 /* find device */
490 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200491 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600492 elem[0] = len = 0;
493 }
494 pos += len;
495 dev = qbus_find_dev(bus, elem);
496 if (!dev) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100497 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
498 "Device '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600499 qbus_list_dev(bus, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600500 return NULL;
501 }
502
503 assert(path[pos] == '/' || !path[pos]);
504 while (path[pos] == '/') {
505 pos++;
506 }
507 if (path[pos] == '\0') {
508 /* last specified element is a device. If it has exactly
509 * one child bus accept it nevertheless */
Markus Armbrustered238ba2015-03-11 18:39:16 +0100510 if (dev->num_child_bus == 1) {
511 bus = QLIST_FIRST(&dev->child_bus);
512 break;
513 }
514 if (dev->num_child_bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100515 error_setg(errp, "Device '%s' has multiple child buses",
516 elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600517 qbus_list_bus(dev, errp);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100518 } else {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100519 error_setg(errp, "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600520 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100521 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600522 }
523
524 /* find bus */
525 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200526 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600527 elem[0] = len = 0;
528 }
529 pos += len;
530 bus = qbus_find_bus(dev, elem);
531 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100532 error_setg(errp, "Bus '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600533 qbus_list_bus(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600534 return NULL;
535 }
536 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100537
538 if (qbus_is_full(bus)) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100539 error_setg(errp, "Bus '%s' is full", path);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100540 return NULL;
541 }
542 return bus;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600543}
544
Juergen Grossce49b732016-11-22 07:10:57 +0100545void qdev_set_id(DeviceState *dev, const char *id)
546{
547 if (id) {
548 dev->id = id;
549 }
550
551 if (dev->id) {
552 object_property_add_child(qdev_get_peripheral(), dev->id,
553 OBJECT(dev), NULL);
554 } else {
555 static int anon_count;
556 gchar *name = g_strdup_printf("device[%d]", anon_count++);
557 object_property_add_child(qdev_get_peripheral_anon(), name,
558 OBJECT(dev), NULL);
559 g_free(name);
560 }
561}
562
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100563DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600564{
Andreas Färberf4d85792013-08-24 01:21:22 +0200565 DeviceClass *dc;
Juergen Grossce49b732016-11-22 07:10:57 +0100566 const char *driver, *path;
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200567 DeviceState *dev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200568 BusState *bus = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200569 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600570
571 driver = qemu_opt_get(opts, "driver");
572 if (!driver) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100573 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600574 return NULL;
575 }
576
577 /* find driver */
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100578 dc = qdev_get_device_class(&driver, errp);
579 if (!dc) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100580 return NULL;
581 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600582
583 /* find bus */
584 path = qemu_opt_get(opts, "bus");
585 if (path != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100586 bus = qbus_find(path, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600587 if (!bus) {
588 return NULL;
589 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200590 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100591 error_setg(errp, "Device '%s' can't go on %s bus",
592 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600593 return NULL;
594 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200595 } else if (dc->bus_type != NULL) {
596 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100597 if (!bus || qbus_is_full(bus)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100598 error_setg(errp, "No '%s' bus found for device '%s'",
599 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600600 return NULL;
601 }
602 }
Igor Mammedov39b888b2014-09-26 09:28:17 +0000603 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100604 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600605 return NULL;
606 }
607
Juan Quintelab06424d2017-03-22 17:34:27 +0100608 if (!migration_is_idle()) {
609 error_setg(errp, "device_add not allowed while migrating");
610 return NULL;
611 }
612
Amos Kong7b030942014-03-03 15:57:55 +0800613 /* create device */
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200614 dev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200615
616 if (bus) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200617 qdev_set_parent_bus(dev, bus);
Thomas Huth03fcbd92017-11-02 11:10:06 +0100618 } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
619 /* No bus, no machine hotplug handler --> device is not hotpluggable */
620 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
621 driver);
622 goto err_del_dev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200623 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600624
Juergen Grossce49b732016-11-22 07:10:57 +0100625 qdev_set_id(dev, qemu_opts_id(opts));
Bandan Das52aa17c2014-02-26 10:32:40 -0700626
Amos Kong7b030942014-03-03 15:57:55 +0800627 /* set properties */
Markus Armbruster4caa4892015-03-12 13:58:02 +0100628 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
Thomas Huth58346212017-11-02 11:10:05 +0100629 goto err_del_dev;
Amos Kong7b030942014-03-03 15:57:55 +0800630 }
631
Bandan Das52aa17c2014-02-26 10:32:40 -0700632 dev->opts = opts;
Andreas Färber852e2c52013-10-07 16:42:34 +0200633 object_property_set_bool(OBJECT(dev), true, "realized", &err);
634 if (err != NULL) {
Bandan Das52aa17c2014-02-26 10:32:40 -0700635 dev->opts = NULL;
Thomas Huth58346212017-11-02 11:10:05 +0100636 goto err_del_dev;
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200637 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200638 return dev;
Thomas Huth58346212017-11-02 11:10:05 +0100639
640err_del_dev:
641 error_propagate(errp, err);
642 object_unparent(OBJECT(dev));
643 object_unref(OBJECT(dev));
644 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600645}
646
647
648#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
649static void qbus_print(Monitor *mon, BusState *bus, int indent);
650
651static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200652 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600653{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600654 if (!props)
655 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100656 for (; props->name; props++) {
657 Error *err = NULL;
658 char *value;
659 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
660 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
661 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
662 } else {
Paolo Bonzinidae3bda2014-02-08 11:01:51 +0100663 value = object_property_print(OBJECT(dev), props->name, true, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600664 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100665 g_free(legacy_name);
666
667 if (err) {
668 error_free(err);
669 continue;
670 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200671 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100672 value && *value ? value : "<null>");
673 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600674 }
675}
676
Anthony Liguori0d936922012-05-02 09:00:20 +0200677static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
678{
679 BusClass *bc = BUS_GET_CLASS(bus);
680
681 if (bc->print_dev) {
682 bc->print_dev(mon, dev, indent);
683 }
684}
685
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600686static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
687{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200688 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600689 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700690 NamedGPIOList *ngl;
691
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600692 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
693 dev->id ? dev->id : "");
694 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700695 QLIST_FOREACH(ngl, &dev->gpios, node) {
696 if (ngl->num_in) {
697 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
698 ngl->num_in);
699 }
700 if (ngl->num_out) {
701 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
702 ngl->num_out);
703 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600704 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200705 class = object_get_class(OBJECT(dev));
706 do {
707 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
708 class = object_class_get_parent(class);
709 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200710 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600711 QLIST_FOREACH(child, &dev->child_bus, sibling) {
712 qbus_print(mon, child, indent);
713 }
714}
715
716static void qbus_print(Monitor *mon, BusState *bus, int indent)
717{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600718 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600719
720 qdev_printf("bus: %s\n", bus->name);
721 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200722 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600723 QTAILQ_FOREACH(kid, &bus->children, sibling) {
724 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600725 qdev_print(mon, dev, indent);
726 }
727}
728#undef qdev_printf
729
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100730void hmp_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600731{
732 if (sysbus_get_default())
733 qbus_print(mon, sysbus_get_default(), 0);
734}
735
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100736void hmp_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600737{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200738 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600739}
740
Andreas Färbera01ff752014-05-07 17:03:18 +0200741typedef struct QOMCompositionState {
742 Monitor *mon;
743 int indent;
744} QOMCompositionState;
745
746static void print_qom_composition(Monitor *mon, Object *obj, int indent);
747
748static int print_qom_composition_child(Object *obj, void *opaque)
749{
750 QOMCompositionState *s = opaque;
751
752 print_qom_composition(s->mon, obj, s->indent);
753
754 return 0;
755}
756
757static void print_qom_composition(Monitor *mon, Object *obj, int indent)
758{
759 QOMCompositionState s = {
760 .mon = mon,
761 .indent = indent + 2,
762 };
763 char *name;
764
765 if (obj == object_get_root()) {
766 name = g_strdup("");
767 } else {
768 name = object_get_canonical_path_component(obj);
769 }
770 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
771 object_get_typename(obj));
772 g_free(name);
773 object_child_foreach(obj, print_qom_composition_child, &s);
774}
775
776void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
777{
778 const char *path = qdict_get_try_str(dict, "path");
779 Object *obj;
780 bool ambiguous = false;
781
782 if (path) {
783 obj = object_resolve_path(path, &ambiguous);
784 if (!obj) {
785 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
786 return;
787 }
788 if (ambiguous) {
789 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
790 return;
791 }
792 } else {
793 obj = qdev_get_machine();
794 }
795 print_qom_composition(mon, obj, 0);
796}
797
Markus Armbruster485febc2015-03-13 17:25:50 +0100798void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600799{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300800 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600801 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100802 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600803
Luiz Capitulino4e899782012-04-18 17:24:01 -0300804 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100805 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100806 error_propagate(errp, local_err);
807 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600808 }
809 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
810 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100811 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600812 }
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100813 dev = qdev_device_add(opts, &local_err);
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100814 if (!dev) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100815 error_propagate(errp, local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600816 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100817 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600818 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100819 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600820}
821
Kevin Wolf6c1db522016-09-20 13:38:41 +0200822static DeviceState *find_device_state(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600823{
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000824 Object *obj;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600825
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100826 if (id[0] == '/') {
827 obj = object_resolve_path(id, NULL);
828 } else {
829 char *root_path = object_get_canonical_path(qdev_get_peripheral());
830 char *path = g_strdup_printf("%s/%s", root_path, id);
831
832 g_free(root_path);
833 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
834 g_free(path);
835 }
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000836
837 if (!obj) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100838 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
839 "Device '%s' not found", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200840 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600841 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300842
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100843 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
844 error_setg(errp, "%s is not a hotpluggable device", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200845 return NULL;
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100846 }
847
Kevin Wolf6c1db522016-09-20 13:38:41 +0200848 return DEVICE(obj);
849}
850
Juan Quintela32900672017-03-28 11:22:51 +0200851void qdev_unplug(DeviceState *dev, Error **errp)
852{
853 DeviceClass *dc = DEVICE_GET_CLASS(dev);
854 HotplugHandler *hotplug_ctrl;
855 HotplugHandlerClass *hdc;
856
857 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
858 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
859 return;
860 }
861
862 if (!dc->hotpluggable) {
863 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
864 object_get_typename(OBJECT(dev)));
865 return;
866 }
867
Juan Quintelab06424d2017-03-22 17:34:27 +0100868 if (!migration_is_idle()) {
869 error_setg(errp, "device_del not allowed while migrating");
870 return;
871 }
872
Juan Quintela32900672017-03-28 11:22:51 +0200873 qdev_hot_removed = true;
874
875 hotplug_ctrl = qdev_get_hotplug_handler(dev);
876 /* hotpluggable device MUST have HotplugHandler, if it doesn't
877 * then something is very wrong with it */
878 g_assert(hotplug_ctrl);
879
880 /* If device supports async unplug just request it to be done,
881 * otherwise just remove it synchronously */
882 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
883 if (hdc->unplug_request) {
884 hotplug_handler_unplug_request(hotplug_ctrl, dev, errp);
885 } else {
886 hotplug_handler_unplug(hotplug_ctrl, dev, errp);
887 }
888}
889
Kevin Wolf6c1db522016-09-20 13:38:41 +0200890void qmp_device_del(const char *id, Error **errp)
891{
892 DeviceState *dev = find_device_state(id, errp);
893 if (dev != NULL) {
894 qdev_unplug(dev, errp);
895 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600896}
897
Kevin Wolf9680cae2016-09-20 13:38:42 +0200898BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
899{
900 DeviceState *dev;
901 BlockBackend *blk;
902
903 dev = find_device_state(id, errp);
904 if (dev == NULL) {
905 return NULL;
906 }
907
908 blk = blk_by_dev(dev);
909 if (!blk) {
910 error_setg(errp, "Device does not have a block device backend");
911 }
912 return blk;
913}
914
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600915void qdev_machine_init(void)
916{
917 qdev_get_peripheral_anon();
918 qdev_get_peripheral();
919}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100920
921QemuOptsList qemu_device_opts = {
922 .name = "device",
923 .implied_opt_name = "driver",
924 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
925 .desc = {
926 /*
927 * no elements => accept any
928 * sanity checking will happen later
929 * when setting device properties
930 */
931 { /* end of list */ }
932 },
933};
934
935QemuOptsList qemu_global_opts = {
936 .name = "global",
937 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
938 .desc = {
939 {
940 .name = "driver",
941 .type = QEMU_OPT_STRING,
942 },{
943 .name = "property",
944 .type = QEMU_OPT_STRING,
945 },{
946 .name = "value",
947 .type = QEMU_OPT_STRING,
948 },
949 { /* end of list */ }
950 },
951};
952
953int qemu_global_option(const char *str)
954{
955 char driver[64], property[64];
956 QemuOpts *opts;
957 int rc, offset;
958
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200959 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
960 if (rc == 2 && str[offset] == '=') {
961 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
962 qemu_opt_set(opts, "driver", driver, &error_abort);
963 qemu_opt_set(opts, "property", property, &error_abort);
964 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
965 return 0;
966 }
967
Markus Armbruster70b94332015-02-13 12:50:26 +0100968 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200969 if (!opts) {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100970 return -1;
971 }
972
Paolo Bonzini4d454572012-11-26 16:03:42 +0100973 return 0;
974}