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