]> git.proxmox.com Git - mirror_qemu.git/blame - softmmu/qdev-monitor.c
hw/loongarch: Add irq hierarchy for the system
[mirror_qemu.git] / softmmu / qdev-monitor.c
CommitLineData
ee46d8a5
AL
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.
ee46d8a5
AL
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
d38ea87a 20#include "qemu/osdep.h"
2f7bd829 21#include "hw/sysbus.h"
d94bd092 22#include "monitor/hmp.h"
83c9089e 23#include "monitor/monitor.h"
b4a42f81 24#include "monitor/qdev.h"
9c17d615 25#include "sysemu/arch_init.h"
e688df6b 26#include "qapi/error.h"
c577ff62 27#include "qapi/qapi-commands-qdev.h"
164dafd1 28#include "qapi/qmp/dispatch.h"
452fcdbc 29#include "qapi/qmp/qdict.h"
cc7a8ea7 30#include "qapi/qmp/qerror.h"
c34efece
KW
31#include "qapi/qmp/qstring.h"
32#include "qapi/qobject-input-visitor.h"
1de7afc9 33#include "qemu/config-file.h"
d49b6836 34#include "qemu/error-report.h"
f348b6d1 35#include "qemu/help_option.h"
922a01a0 36#include "qemu/option.h"
8acb2a75 37#include "qemu/qemu-print.h"
f3a85056 38#include "qemu/option_int.h"
9680caee 39#include "sysemu/block-backend.h"
c4b63b7c 40#include "migration/misc.h"
f3a85056 41#include "migration/migration.h"
e1043d67 42#include "qemu/cutils.h"
d3fd6e73 43#include "hw/qdev-properties.h"
9f2ff99c 44#include "hw/clock.h"
312e1b1f 45#include "hw/boards.h"
ee46d8a5
AL
46
47/*
48 * Aliases were a bad idea from the start. Let's keep them
49 * from spreading further.
50 */
51typedef struct QDevAlias
52{
53 const char *typename;
54 const char *alias;
5f629d94 55 uint32_t arch_mask;
ee46d8a5
AL
56} QDevAlias;
57
3669282c
PM
58/* default virtio transport per architecture */
59#define QEMU_ARCH_VIRTIO_PCI (QEMU_ARCH_ALPHA | QEMU_ARCH_ARM | \
60 QEMU_ARCH_HPPA | QEMU_ARCH_I386 | \
61 QEMU_ARCH_MIPS | QEMU_ARCH_PPC | \
62 QEMU_ARCH_RISCV | QEMU_ARCH_SH4 | \
63 QEMU_ARCH_SPARC | QEMU_ARCH_XTENSA)
64#define QEMU_ARCH_VIRTIO_CCW (QEMU_ARCH_S390X)
65#define QEMU_ARCH_VIRTIO_MMIO (QEMU_ARCH_M68K)
66
36e99168 67/* Please keep this table sorted by typename. */
ee46d8a5 68static const QDevAlias qdev_alias_table[] = {
2957f5ad 69 { "AC97", "ac97" }, /* -soundhw name */
36e99168 70 { "e1000", "e1000-82540em" },
0e933feb 71 { "ES1370", "es1370" }, /* -soundhw name */
36e99168 72 { "ich9-ahci", "ahci" },
36e99168 73 { "lsi53c895a", "lsi" },
4c5806a5 74 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
75 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW },
76 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 77 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
78 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW },
79 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 80 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
81 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW },
82 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 83 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
84 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW },
85 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI },
f4ba24b3
AO
86 { "virtio-gpu-gl-device", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_MMIO },
87 { "virtio-gpu-gl-pci", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 88 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
89 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW },
90 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI },
91 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 92 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
93 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW },
94 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 95 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
96 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW },
97 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 98 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
99 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW },
100 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 101 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
102 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW },
103 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 104 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
105 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW },
106 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI },
4c5806a5 107 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
108 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW },
109 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI},
4c5806a5 110 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO },
203adb43
LV
111 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW },
112 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI },
ee46d8a5
AL
113 { }
114};
115
116static const char *qdev_class_get_alias(DeviceClass *dc)
117{
118 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
119 int i;
120
121 for (i = 0; qdev_alias_table[i].typename; i++) {
5f629d94
AG
122 if (qdev_alias_table[i].arch_mask &&
123 !(qdev_alias_table[i].arch_mask & arch_type)) {
124 continue;
125 }
126
ee46d8a5
AL
127 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
128 return qdev_alias_table[i].alias;
129 }
130 }
131
132 return NULL;
133}
134
135static bool qdev_class_has_alias(DeviceClass *dc)
136{
137 return (qdev_class_get_alias(dc) != NULL);
138}
139
a3400aee 140static void qdev_print_devinfo(DeviceClass *dc)
ee46d8a5 141{
8acb2a75 142 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
0d936928 143 if (dc->bus_type) {
8acb2a75 144 qemu_printf(", bus %s", dc->bus_type);
ee46d8a5
AL
145 }
146 if (qdev_class_has_alias(dc)) {
8acb2a75 147 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
ee46d8a5
AL
148 }
149 if (dc->desc) {
8acb2a75 150 qemu_printf(", desc \"%s\"", dc->desc);
ee46d8a5 151 }
e90f2a8c 152 if (!dc->user_creatable) {
8acb2a75 153 qemu_printf(", no-user");
ee46d8a5 154 }
8acb2a75 155 qemu_printf("\n");
ee46d8a5
AL
156}
157
a3400aee
MA
158static void qdev_print_devinfos(bool show_no_user)
159{
160 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
161 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
162 [DEVICE_CATEGORY_USB] = "USB",
163 [DEVICE_CATEGORY_STORAGE] = "Storage",
164 [DEVICE_CATEGORY_NETWORK] = "Network",
165 [DEVICE_CATEGORY_INPUT] = "Input",
166 [DEVICE_CATEGORY_DISPLAY] = "Display",
167 [DEVICE_CATEGORY_SOUND] = "Sound",
168 [DEVICE_CATEGORY_MISC] = "Misc",
ba31cc72 169 [DEVICE_CATEGORY_CPU] = "CPU",
b10cb627 170 [DEVICE_CATEGORY_WATCHDOG]= "Watchdog",
a3400aee
MA
171 [DEVICE_CATEGORY_MAX] = "Uncategorized",
172 };
173 GSList *list, *elt;
174 int i;
175 bool cat_printed;
176
7ab6e7fc 177 module_load_qom_all();
47c66009 178 list = object_class_get_list_sorted(TYPE_DEVICE, false);
a3400aee
MA
179
180 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
181 cat_printed = false;
182 for (elt = list; elt; elt = elt->next) {
183 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
184 TYPE_DEVICE);
185 if ((i < DEVICE_CATEGORY_MAX
186 ? !test_bit(i, dc->categories)
187 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
efec3dd6 188 || (!show_no_user
e90f2a8c 189 && !dc->user_creatable)) {
a3400aee
MA
190 continue;
191 }
192 if (!cat_printed) {
8acb2a75 193 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
a3400aee
MA
194 cat_printed = true;
195 }
196 qdev_print_devinfo(dc);
197 }
198 }
199
200 g_slist_free(list);
201}
202
ee46d8a5
AL
203static const char *find_typename_by_alias(const char *alias)
204{
205 int i;
206
207 for (i = 0; qdev_alias_table[i].alias; i++) {
5f629d94
AG
208 if (qdev_alias_table[i].arch_mask &&
209 !(qdev_alias_table[i].arch_mask & arch_type)) {
210 continue;
211 }
212
ee46d8a5
AL
213 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
214 return qdev_alias_table[i].typename;
215 }
216 }
217
218 return NULL;
219}
220
43c95d78
EH
221static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
222{
223 ObjectClass *oc;
224 DeviceClass *dc;
f6b5319d 225 const char *original_name = *driver;
43c95d78 226
7ab6e7fc 227 oc = module_object_class_by_name(*driver);
43c95d78
EH
228 if (!oc) {
229 const char *typename = find_typename_by_alias(*driver);
230
231 if (typename) {
232 *driver = typename;
7ab6e7fc 233 oc = module_object_class_by_name(*driver);
43c95d78
EH
234 }
235 }
236
237 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
f6b5319d
SS
238 if (*driver != original_name) {
239 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
240 " name", original_name, *driver);
241 } else {
242 error_setg(errp, "'%s' is not a valid device model name", *driver);
243 }
43c95d78
EH
244 return NULL;
245 }
246
247 if (object_class_is_abstract(oc)) {
c6bd8c70 248 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
6cc0667d 249 "a non-abstract device type");
43c95d78
EH
250 return NULL;
251 }
252
253 dc = DEVICE_CLASS(oc);
e90f2a8c 254 if (!dc->user_creatable ||
2f181fbd 255 (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
c6bd8c70 256 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
6cc0667d 257 "a pluggable device type");
43c95d78
EH
258 return NULL;
259 }
260
312e1b1f
DH
261 if (object_class_dynamic_cast(oc, TYPE_SYS_BUS_DEVICE)) {
262 /* sysbus devices need to be allowed by the machine */
263 MachineClass *mc = MACHINE_CLASS(object_get_class(qdev_get_machine()));
264 if (!device_type_is_dynamic_sysbus(mc, *driver)) {
265 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
266 "a dynamic sysbus device type for the machine");
267 return NULL;
268 }
269 }
270
43c95d78
EH
271 return dc;
272}
273
274
ee46d8a5
AL
275int qdev_device_help(QemuOpts *opts)
276{
ef523587 277 Error *local_err = NULL;
ee46d8a5 278 const char *driver;
35f63767
AK
279 ObjectPropertyInfoList *prop_list;
280 ObjectPropertyInfoList *prop;
e1043d67
MAL
281 GPtrArray *array;
282 int i;
ee46d8a5
AL
283
284 driver = qemu_opt_get(opts, "driver");
c8057f95 285 if (driver && is_help_option(driver)) {
a3400aee 286 qdev_print_devinfos(false);
ee46d8a5
AL
287 return 1;
288 }
289
c8057f95 290 if (!driver || !qemu_opt_has_help_opt(opts)) {
ee46d8a5
AL
291 return 0;
292 }
293
33fe9683
MA
294 if (!object_class_by_name(driver)) {
295 const char *typename = find_typename_by_alias(driver);
296
297 if (typename) {
298 driver = typename;
299 }
ee46d8a5
AL
300 }
301
ef523587 302 prop_list = qmp_device_list_properties(driver, &local_err);
0722eba9 303 if (local_err) {
5185f0e0 304 goto error;
ee46d8a5 305 }
ef523587 306
9c2762b4 307 if (prop_list) {
8acb2a75 308 qemu_printf("%s options:\n", driver);
9c2762b4 309 } else {
8acb2a75 310 qemu_printf("There are no options for %s.\n", driver);
9c2762b4 311 }
e1043d67 312 array = g_ptr_array_new();
ef523587 313 for (prop = prop_list; prop; prop = prop->next) {
e1043d67
MAL
314 g_ptr_array_add(array,
315 object_property_help(prop->value->name,
316 prop->value->type,
317 prop->value->default_value,
318 prop->value->description));
ef523587 319 }
e1043d67
MAL
320 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
321 for (i = 0; i < array->len; i++) {
029afc4e 322 qemu_printf("%s\n", (char *)array->pdata[i]);
e1043d67
MAL
323 }
324 g_ptr_array_set_free_func(array, g_free);
325 g_ptr_array_free(array, true);
35f63767 326 qapi_free_ObjectPropertyInfoList(prop_list);
ee46d8a5 327 return 1;
5185f0e0
EH
328
329error:
78288671 330 error_report_err(local_err);
5185f0e0 331 return 1;
ee46d8a5
AL
332}
333
57c9fafe 334static Object *qdev_get_peripheral(void)
ee46d8a5 335{
8b45d447 336 static Object *dev;
ee46d8a5
AL
337
338 if (dev == NULL) {
dfe47e70 339 dev = container_get(qdev_get_machine(), "/peripheral");
ee46d8a5
AL
340 }
341
8b45d447 342 return dev;
ee46d8a5
AL
343}
344
57c9fafe 345static Object *qdev_get_peripheral_anon(void)
ee46d8a5 346{
8b45d447 347 static Object *dev;
ee46d8a5
AL
348
349 if (dev == NULL) {
dfe47e70 350 dev = container_get(qdev_get_machine(), "/peripheral-anon");
ee46d8a5
AL
351 }
352
8b45d447 353 return dev;
ee46d8a5
AL
354}
355
34077326
VSO
356static void qbus_error_append_bus_list_hint(DeviceState *dev,
357 Error *const *errp)
ee46d8a5
AL
358{
359 BusState *child;
360 const char *sep = " ";
361
50b7b000
EB
362 error_append_hint(errp, "child buses at \"%s\":",
363 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
ee46d8a5 364 QLIST_FOREACH(child, &dev->child_bus, sibling) {
50b7b000 365 error_append_hint(errp, "%s\"%s\"", sep, child->name);
ee46d8a5
AL
366 sep = ", ";
367 }
543202c0 368 error_append_hint(errp, "\n");
ee46d8a5
AL
369}
370
34077326
VSO
371static void qbus_error_append_dev_list_hint(BusState *bus,
372 Error *const *errp)
ee46d8a5 373{
0866aca1 374 BusChild *kid;
ee46d8a5
AL
375 const char *sep = " ";
376
50b7b000 377 error_append_hint(errp, "devices at \"%s\":", bus->name);
0866aca1
AL
378 QTAILQ_FOREACH(kid, &bus->children, sibling) {
379 DeviceState *dev = kid->child;
50b7b000
EB
380 error_append_hint(errp, "%s\"%s\"", sep,
381 object_get_typename(OBJECT(dev)));
382 if (dev->id) {
383 error_append_hint(errp, "/\"%s\"", dev->id);
384 }
ee46d8a5
AL
385 sep = ", ";
386 }
543202c0 387 error_append_hint(errp, "\n");
ee46d8a5
AL
388}
389
390static BusState *qbus_find_bus(DeviceState *dev, char *elem)
391{
392 BusState *child;
393
394 QLIST_FOREACH(child, &dev->child_bus, sibling) {
395 if (strcmp(child->name, elem) == 0) {
396 return child;
397 }
398 }
399 return NULL;
400}
401
402static DeviceState *qbus_find_dev(BusState *bus, char *elem)
403{
0866aca1 404 BusChild *kid;
ee46d8a5
AL
405
406 /*
407 * try to match in order:
408 * (1) instance id, if present
409 * (2) driver name
410 * (3) driver alias, if present
411 */
0866aca1
AL
412 QTAILQ_FOREACH(kid, &bus->children, sibling) {
413 DeviceState *dev = kid->child;
ee46d8a5
AL
414 if (dev->id && strcmp(dev->id, elem) == 0) {
415 return dev;
416 }
417 }
0866aca1
AL
418 QTAILQ_FOREACH(kid, &bus->children, sibling) {
419 DeviceState *dev = kid->child;
ee46d8a5
AL
420 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
421 return dev;
422 }
423 }
0866aca1
AL
424 QTAILQ_FOREACH(kid, &bus->children, sibling) {
425 DeviceState *dev = kid->child;
ee46d8a5
AL
426 DeviceClass *dc = DEVICE_GET_CLASS(dev);
427
428 if (qdev_class_has_alias(dc) &&
429 strcmp(qdev_class_get_alias(dc), elem) == 0) {
430 return dev;
431 }
432 }
433 return NULL;
434}
435
a5ec494e
MA
436static inline bool qbus_is_full(BusState *bus)
437{
1518562b
PM
438 BusClass *bus_class;
439
440 if (bus->full) {
441 return true;
442 }
443 bus_class = BUS_GET_CLASS(bus);
12b2e9f3 444 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
a5ec494e
MA
445}
446
447/*
448 * Search the tree rooted at @bus for a bus.
449 * If @name, search for a bus with that name. Note that bus names
450 * need not be unique. Yes, that's screwed up.
451 * Else search for a bus that is a subtype of @bus_typename.
452 * If more than one exists, prefer one that can take another device.
453 * Return the bus if found, else %NULL.
454 */
ee46d8a5 455static BusState *qbus_find_recursive(BusState *bus, const char *name,
0d936928 456 const char *bus_typename)
ee46d8a5 457{
0866aca1 458 BusChild *kid;
a5ec494e
MA
459 BusState *pick, *child, *ret;
460 bool match;
461
462 assert(name || bus_typename);
463 if (name) {
464 match = !strcmp(bus->name, name);
465 } else {
466 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
1395af6f 467 }
a5ec494e
MA
468
469 if (match && !qbus_is_full(bus)) {
470 return bus; /* root matches and isn't full */
ee46d8a5
AL
471 }
472
a5ec494e
MA
473 pick = match ? bus : NULL;
474
0866aca1
AL
475 QTAILQ_FOREACH(kid, &bus->children, sibling) {
476 DeviceState *dev = kid->child;
ee46d8a5 477 QLIST_FOREACH(child, &dev->child_bus, sibling) {
0d936928 478 ret = qbus_find_recursive(child, name, bus_typename);
a5ec494e
MA
479 if (ret && !qbus_is_full(ret)) {
480 return ret; /* a descendant matches and isn't full */
481 }
482 if (ret && !pick) {
483 pick = ret;
ee46d8a5
AL
484 }
485 }
486 }
a5ec494e
MA
487
488 /* root or a descendant matches, but is full */
489 return pick;
ee46d8a5
AL
490}
491
d2828429 492static BusState *qbus_find(const char *path, Error **errp)
ee46d8a5
AL
493{
494 DeviceState *dev;
495 BusState *bus;
496 char elem[128];
497 int pos, len;
498
499 /* find start element */
500 if (path[0] == '/') {
501 bus = sysbus_get_default();
502 pos = 0;
503 } else {
504 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
505 assert(!path[0]);
506 elem[0] = len = 0;
507 }
508 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
509 if (!bus) {
d2828429 510 error_setg(errp, "Bus '%s' not found", elem);
ee46d8a5
AL
511 return NULL;
512 }
513 pos = len;
514 }
515
516 for (;;) {
517 assert(path[pos] == '/' || !path[pos]);
518 while (path[pos] == '/') {
519 pos++;
520 }
521 if (path[pos] == '\0') {
ed238ba2 522 break;
ee46d8a5
AL
523 }
524
525 /* find device */
526 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
dfc6f865 527 g_assert_not_reached();
ee46d8a5
AL
528 elem[0] = len = 0;
529 }
530 pos += len;
531 dev = qbus_find_dev(bus, elem);
532 if (!dev) {
75158ebb
MA
533 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
534 "Device '%s' not found", elem);
34077326 535 qbus_error_append_dev_list_hint(bus, errp);
ee46d8a5
AL
536 return NULL;
537 }
538
539 assert(path[pos] == '/' || !path[pos]);
540 while (path[pos] == '/') {
541 pos++;
542 }
543 if (path[pos] == '\0') {
544 /* last specified element is a device. If it has exactly
545 * one child bus accept it nevertheless */
ed238ba2
MA
546 if (dev->num_child_bus == 1) {
547 bus = QLIST_FIRST(&dev->child_bus);
548 break;
549 }
550 if (dev->num_child_bus) {
d2828429
MA
551 error_setg(errp, "Device '%s' has multiple child buses",
552 elem);
34077326 553 qbus_error_append_bus_list_hint(dev, errp);
ed238ba2 554 } else {
d2828429 555 error_setg(errp, "Device '%s' has no child bus", elem);
ee46d8a5 556 }
ed238ba2 557 return NULL;
ee46d8a5
AL
558 }
559
560 /* find bus */
561 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
dfc6f865 562 g_assert_not_reached();
ee46d8a5
AL
563 elem[0] = len = 0;
564 }
565 pos += len;
566 bus = qbus_find_bus(dev, elem);
567 if (!bus) {
d2828429 568 error_setg(errp, "Bus '%s' not found", elem);
34077326 569 qbus_error_append_bus_list_hint(dev, errp);
ee46d8a5
AL
570 return NULL;
571 }
572 }
ed238ba2
MA
573
574 if (qbus_is_full(bus)) {
d2828429 575 error_setg(errp, "Bus '%s' is full", path);
ed238ba2
MA
576 return NULL;
577 }
578 return bus;
ee46d8a5
AL
579}
580
163f3847 581/* Takes ownership of @id, will be freed when deleting the device */
4a1d9377 582const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
ce49b734 583{
4a1d9377 584 ObjectProperty *prop;
ce49b734 585
4a1d9377
DH
586 assert(!dev->id && !dev->realized);
587
588 /*
589 * object_property_[try_]add_child() below will assert the device
590 * has no parent
591 */
592 if (id) {
593 prop = object_property_try_add_child(qdev_get_peripheral(), id,
594 OBJECT(dev), NULL);
595 if (prop) {
596 dev->id = id;
597 } else {
4a1d9377 598 error_setg(errp, "Duplicate device ID '%s'", id);
b66cecb2 599 g_free(id);
4a1d9377
DH
600 return NULL;
601 }
ce49b734
JG
602 } else {
603 static int anon_count;
604 gchar *name = g_strdup_printf("device[%d]", anon_count++);
4a1d9377
DH
605 prop = object_property_add_child(qdev_get_peripheral_anon(), name,
606 OBJECT(dev));
ce49b734
JG
607 g_free(name);
608 }
4a1d9377
DH
609
610 return prop->name;
ce49b734
JG
611}
612
f3558b1b
KW
613DeviceState *qdev_device_add_from_qdict(const QDict *opts,
614 bool from_json, Error **errp)
ee46d8a5 615{
7d618082 616 ERRP_GUARD();
f4d85795 617 DeviceClass *dc;
ce49b734 618 const char *driver, *path;
f3558b1b 619 char *id;
f3a85056 620 DeviceState *dev = NULL;
2f7bd829 621 BusState *bus = NULL;
ee46d8a5 622
f3558b1b 623 driver = qdict_get_try_str(opts, "driver");
ee46d8a5 624 if (!driver) {
c6bd8c70 625 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
ee46d8a5
AL
626 return NULL;
627 }
628
629 /* find driver */
f006cf7f
MA
630 dc = qdev_get_device_class(&driver, errp);
631 if (!dc) {
7ea5e78f
MA
632 return NULL;
633 }
ee46d8a5
AL
634
635 /* find bus */
f3558b1b 636 path = qdict_get_try_str(opts, "bus");
ee46d8a5 637 if (path != NULL) {
f006cf7f 638 bus = qbus_find(path, errp);
ee46d8a5
AL
639 if (!bus) {
640 return NULL;
641 }
f4d85795 642 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
f006cf7f
MA
643 error_setg(errp, "Device '%s' can't go on %s bus",
644 driver, object_get_typename(OBJECT(bus)));
ee46d8a5
AL
645 return NULL;
646 }
f4d85795
AF
647 } else if (dc->bus_type != NULL) {
648 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
a5ec494e 649 if (!bus || qbus_is_full(bus)) {
f006cf7f
MA
650 error_setg(errp, "No '%s' bus found for device '%s'",
651 dc->bus_type, driver);
ee46d8a5
AL
652 return NULL;
653 }
654 }
f3a85056 655
bcfc906b
LV
656 if (qdev_should_hide_device(opts, from_json, errp)) {
657 if (bus && !qbus_is_hotpluggable(bus)) {
658 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
5f2ef3b0 659 }
bcfc906b
LV
660 return NULL;
661 } else if (*errp) {
662 return NULL;
ee46d8a5
AL
663 }
664
2f181fbd 665 if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
5f2ef3b0 666 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
f3a85056
JF
667 return NULL;
668 }
669
b06424de
JQ
670 if (!migration_is_idle()) {
671 error_setg(errp, "device_add not allowed while migrating");
672 return NULL;
673 }
674
7b030949 675 /* create device */
4e3a6778 676 dev = qdev_new(driver);
2f7bd829 677
d2321d31 678 /* Check whether the hotplug is allowed by the machine */
2f181fbd
PB
679 if (phase_check(PHASE_MACHINE_READY)) {
680 if (!qdev_hotplug_allowed(dev, errp)) {
681 goto err_del_dev;
682 }
d2321d31 683
2f181fbd
PB
684 if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
685 /* No bus, no machine hotplug handler --> device is not hotpluggable */
686 error_setg(errp, "Device '%s' can not be hotplugged on this machine",
687 driver);
688 goto err_del_dev;
689 }
2f7bd829 690 }
ee46d8a5 691
4a1d9377
DH
692 /*
693 * set dev's parent and register its id.
694 * If it fails it means the id is already taken.
695 */
f3558b1b
KW
696 id = g_strdup(qdict_get_try_str(opts, "id"));
697 if (!qdev_set_id(dev, id, errp)) {
4a1d9377
DH
698 goto err_del_dev;
699 }
52aa17cb 700
7b030949 701 /* set properties */
f3558b1b
KW
702 dev->opts = qdict_clone_shallow(opts);
703 qdict_del(dev->opts, "driver");
704 qdict_del(dev->opts, "bus");
705 qdict_del(dev->opts, "id");
706
707 object_set_properties_from_keyval(&dev->parent_obj, dev->opts, from_json,
708 errp);
709 if (*errp) {
58346214 710 goto err_del_dev;
7b030949
AK
711 }
712
992861fb 713 if (!qdev_realize(DEVICE(dev), bus, errp)) {
58346214 714 goto err_del_dev;
f424d5c4 715 }
2bcb0c62 716 return dev;
58346214
TH
717
718err_del_dev:
f3a85056
JF
719 if (dev) {
720 object_unparent(OBJECT(dev));
721 object_unref(OBJECT(dev));
722 }
58346214 723 return NULL;
ee46d8a5
AL
724}
725
f3558b1b
KW
726/* Takes ownership of @opts on success */
727DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
728{
729 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
730 DeviceState *ret;
731
732 ret = qdev_device_add_from_qdict(qdict, false, errp);
733 if (ret) {
734 qemu_opts_del(opts);
735 }
736 qobject_unref(qdict);
737 return ret;
738}
ee46d8a5
AL
739
740#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
741static void qbus_print(Monitor *mon, BusState *bus, int indent);
742
743static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
bce54474 744 int indent)
ee46d8a5 745{
ee46d8a5
AL
746 if (!props)
747 return;
d822979b 748 for (; props->name; props++) {
d822979b
PB
749 char *value;
750 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
a43770df 751
d822979b 752 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
a43770df 753 value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
d822979b 754 } else {
a43770df
MA
755 value = object_property_print(OBJECT(dev), props->name, true,
756 NULL);
d822979b
PB
757 }
758 g_free(legacy_name);
759
a43770df 760 if (!value) {
d822979b 761 continue;
ee46d8a5 762 }
bce54474 763 qdev_printf("%s = %s\n", props->name,
a43770df 764 *value ? value : "<null>");
d822979b 765 g_free(value);
ee46d8a5
AL
766 }
767}
768
0d936928
AL
769static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
770{
771 BusClass *bc = BUS_GET_CLASS(bus);
772
773 if (bc->print_dev) {
774 bc->print_dev(mon, dev, indent);
775 }
776}
777
ee46d8a5
AL
778static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
779{
bce54474 780 ObjectClass *class;
ee46d8a5 781 BusState *child;
a5f54290 782 NamedGPIOList *ngl;
9f2ff99c 783 NamedClockList *ncl;
a5f54290 784
ee46d8a5
AL
785 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
786 dev->id ? dev->id : "");
787 indent += 2;
a5f54290
PC
788 QLIST_FOREACH(ngl, &dev->gpios, node) {
789 if (ngl->num_in) {
790 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
791 ngl->num_in);
792 }
793 if (ngl->num_out) {
794 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
795 ngl->num_out);
796 }
ee46d8a5 797 }
9f2ff99c 798 QLIST_FOREACH(ncl, &dev->clocks, node) {
b7cd9c1e
PM
799 g_autofree char *freq_str = clock_display_freq(ncl->clock);
800 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
9f2ff99c
DH
801 ncl->output ? "out" : "in",
802 ncl->alias ? " (alias)" : "",
b7cd9c1e 803 ncl->name, freq_str);
9f2ff99c 804 }
bce54474
PB
805 class = object_get_class(OBJECT(dev));
806 do {
385d8f22 807 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
bce54474
PB
808 class = object_class_get_parent(class);
809 } while (class != object_class_by_name(TYPE_DEVICE));
da9fbe76 810 bus_print_dev(dev->parent_bus, mon, dev, indent);
ee46d8a5
AL
811 QLIST_FOREACH(child, &dev->child_bus, sibling) {
812 qbus_print(mon, child, indent);
813 }
814}
815
816static void qbus_print(Monitor *mon, BusState *bus, int indent)
817{
0866aca1 818 BusChild *kid;
ee46d8a5
AL
819
820 qdev_printf("bus: %s\n", bus->name);
821 indent += 2;
0d936928 822 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
0866aca1
AL
823 QTAILQ_FOREACH(kid, &bus->children, sibling) {
824 DeviceState *dev = kid->child;
ee46d8a5
AL
825 qdev_print(mon, dev, indent);
826 }
827}
828#undef qdev_printf
829
1ce6be24 830void hmp_info_qtree(Monitor *mon, const QDict *qdict)
ee46d8a5
AL
831{
832 if (sysbus_get_default())
833 qbus_print(mon, sysbus_get_default(), 0);
834}
835
1ce6be24 836void hmp_info_qdm(Monitor *mon, const QDict *qdict)
ee46d8a5 837{
a3400aee 838 qdev_print_devinfos(true);
ee46d8a5
AL
839}
840
485febc6 841void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
ee46d8a5
AL
842{
843 QemuOpts *opts;
b09995ae 844 DeviceState *dev;
ee46d8a5 845
c6ecec43
MA
846 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
847 if (!opts) {
485febc6 848 return;
ee46d8a5
AL
849 }
850 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
851 qemu_opts_del(opts);
485febc6 852 return;
ee46d8a5 853 }
668f62ec 854 dev = qdev_device_add(opts, errp);
7bed8995
ML
855
856 /*
857 * Drain all pending RCU callbacks. This is done because
858 * some bus related operations can delay a device removal
859 * (in this case this can happen if device is added and then
860 * removed due to a configuration error)
861 * to a RCU callback, but user might expect that this interface
862 * will finish its job completely once qmp command returns result
863 * to the user
864 */
865 drain_call_rcu();
866
b09995ae 867 if (!dev) {
ee46d8a5 868 qemu_opts_del(opts);
485febc6 869 return;
ee46d8a5 870 }
b09995ae 871 object_unref(OBJECT(dev));
ee46d8a5
AL
872}
873
6c1db528 874static DeviceState *find_device_state(const char *id, Error **errp)
ee46d8a5 875{
1bf4d329 876 Object *obj = object_resolve_path_at(qdev_get_peripheral(), id);
69520261 877 DeviceState *dev;
ee46d8a5 878
b6cc36ab 879 if (!obj) {
75158ebb
MA
880 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
881 "Device '%s' not found", id);
6c1db528 882 return NULL;
56f9107e
LC
883 }
884
69520261
MA
885 dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
886 if (!dev) {
6287d827 887 error_setg(errp, "%s is not a hotpluggable device", id);
6c1db528 888 return NULL;
6287d827
DB
889 }
890
69520261 891 return dev;
6c1db528
KW
892}
893
32900679
JQ
894void qdev_unplug(DeviceState *dev, Error **errp)
895{
896 DeviceClass *dc = DEVICE_GET_CLASS(dev);
897 HotplugHandler *hotplug_ctrl;
898 HotplugHandlerClass *hdc;
07578b0a 899 Error *local_err = NULL;
32900679
JQ
900
901 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
902 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
903 return;
904 }
905
906 if (!dc->hotpluggable) {
907 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
908 object_get_typename(OBJECT(dev)));
909 return;
910 }
911
a1190ab6 912 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
b06424de
JQ
913 error_setg(errp, "device_del not allowed while migrating");
914 return;
915 }
916
32900679
JQ
917 qdev_hot_removed = true;
918
919 hotplug_ctrl = qdev_get_hotplug_handler(dev);
920 /* hotpluggable device MUST have HotplugHandler, if it doesn't
921 * then something is very wrong with it */
922 g_assert(hotplug_ctrl);
923
924 /* If device supports async unplug just request it to be done,
925 * otherwise just remove it synchronously */
926 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
927 if (hdc->unplug_request) {
07578b0a 928 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
32900679 929 } else {
07578b0a
DH
930 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
931 if (!local_err) {
932 object_unparent(OBJECT(dev));
933 }
32900679 934 }
07578b0a 935 error_propagate(errp, local_err);
32900679
JQ
936}
937
6c1db528
KW
938void qmp_device_del(const char *id, Error **errp)
939{
940 DeviceState *dev = find_device_state(id, errp);
941 if (dev != NULL) {
18416c62
GH
942 if (dev->pending_deleted_event &&
943 (dev->pending_deleted_expires_ms == 0 ||
944 dev->pending_deleted_expires_ms > qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL))) {
cce8944c
JS
945 error_setg(errp, "Device %s is already in the "
946 "process of unplug", id);
947 return;
948 }
949
6c1db528
KW
950 qdev_unplug(dev, errp);
951 }
ee46d8a5
AL
952}
953
d94bd092
MA
954void hmp_device_add(Monitor *mon, const QDict *qdict)
955{
956 Error *err = NULL;
957
958 qmp_device_add((QDict *)qdict, NULL, &err);
187c6147 959 hmp_handle_error(mon, err);
d94bd092
MA
960}
961
962void hmp_device_del(Monitor *mon, const QDict *qdict)
963{
964 const char *id = qdict_get_str(qdict, "id");
965 Error *err = NULL;
966
967 qmp_device_del(id, &err);
187c6147 968 hmp_handle_error(mon, err);
d94bd092
MA
969}
970
9680caee
KW
971BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
972{
973 DeviceState *dev;
974 BlockBackend *blk;
975
0439c5a4
EGE
976 GLOBAL_STATE_CODE();
977
9680caee
KW
978 dev = find_device_state(id, errp);
979 if (dev == NULL) {
980 return NULL;
981 }
982
983 blk = blk_by_dev(dev);
984 if (!blk) {
985 error_setg(errp, "Device does not have a block device backend");
986 }
987 return blk;
988}
989
4d454574
PB
990QemuOptsList qemu_device_opts = {
991 .name = "device",
992 .implied_opt_name = "driver",
993 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
994 .desc = {
995 /*
996 * no elements => accept any
997 * sanity checking will happen later
998 * when setting device properties
999 */
1000 { /* end of list */ }
1001 },
1002};
1003
1004QemuOptsList qemu_global_opts = {
1005 .name = "global",
1006 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
1007 .desc = {
1008 {
1009 .name = "driver",
1010 .type = QEMU_OPT_STRING,
1011 },{
1012 .name = "property",
1013 .type = QEMU_OPT_STRING,
1014 },{
1015 .name = "value",
1016 .type = QEMU_OPT_STRING,
1017 },
1018 { /* end of list */ }
1019 },
1020};
1021
1022int qemu_global_option(const char *str)
1023{
1024 char driver[64], property[64];
1025 QemuOpts *opts;
1026 int rc, offset;
1027
3751d7c4
PB
1028 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
1029 if (rc == 2 && str[offset] == '=') {
1030 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
1031 qemu_opt_set(opts, "driver", driver, &error_abort);
1032 qemu_opt_set(opts, "property", property, &error_abort);
1033 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
1034 return 0;
1035 }
1036
70b94331 1037 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
3751d7c4 1038 if (!opts) {
4d454574
PB
1039 return -1;
1040 }
818e1636
RK
1041 if (!qemu_opt_get(opts, "driver")
1042 || !qemu_opt_get(opts, "property")
1043 || !qemu_opt_get(opts, "value")) {
1044 error_report("options 'driver', 'property', and 'value'"
1045 " are required");
1046 return -1;
1047 }
4d454574 1048
4d454574
PB
1049 return 0;
1050}
164dafd1
PB
1051
1052bool qmp_command_available(const QmpCommand *cmd, Error **errp)
1053{
2f181fbd 1054 if (!phase_check(PHASE_MACHINE_READY) &&
164dafd1
PB
1055 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
1056 error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
1057 cmd->name);
1058 return false;
1059 }
1060 return true;
1061}