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