blob: e5136d78e0c1d9a7138efb2bdddf9069bd3f3fd4 [file] [log] [blame]
Anthony Liguoriee46d8a2011-12-22 15:24:20 -06001/*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010021#include "hw/qdev.h"
Andreas Färber2f7bd822013-04-16 03:50:21 +020022#include "hw/sysbus.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010023#include "monitor/monitor.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010024#include "monitor/qdev.h"
Luiz Capitulinoa15fef22012-03-29 12:38:50 -030025#include "qmp-commands.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/arch_init.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qemu/error-report.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060030
31/*
32 * Aliases were a bad idea from the start. Let's keep them
33 * from spreading further.
34 */
35typedef struct QDevAlias
36{
37 const char *typename;
38 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020039 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060040} QDevAlias;
41
42static const QDevAlias qdev_alias_table[] = {
Alexander Graf5f629d92012-05-18 02:36:26 +020043 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
44 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
45 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
46 { "virtio-balloon-pci", "virtio-balloon",
47 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020048 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
49 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
50 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060051 { "lsi53c895a", "lsi" },
52 { "ich9-ahci", "ahci" },
Jan Kiszkac3ebd3b2012-08-30 20:30:00 +020053 { "kvm-pci-assign", "pci-assign" },
Jason Wang83044022015-09-28 13:37:26 +080054 { "e1000", "e1000-82540em" },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060055 { }
56};
57
58static const char *qdev_class_get_alias(DeviceClass *dc)
59{
60 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
61 int i;
62
63 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +020064 if (qdev_alias_table[i].arch_mask &&
65 !(qdev_alias_table[i].arch_mask & arch_type)) {
66 continue;
67 }
68
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060069 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
70 return qdev_alias_table[i].alias;
71 }
72 }
73
74 return NULL;
75}
76
77static bool qdev_class_has_alias(DeviceClass *dc)
78{
79 return (qdev_class_get_alias(dc) != NULL);
80}
81
Markus Armbrustera3400ae2013-10-10 15:00:21 +020082static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060083{
Markus Armbrustera3400ae2013-10-10 15:00:21 +020084 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +020085 if (dc->bus_type) {
86 error_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060087 }
88 if (qdev_class_has_alias(dc)) {
89 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
90 }
91 if (dc->desc) {
92 error_printf(", desc \"%s\"", dc->desc);
93 }
Markus Armbrusterefec3dd2013-11-28 17:26:54 +010094 if (dc->cannot_instantiate_with_device_add_yet) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060095 error_printf(", no-user");
96 }
97 error_printf("\n");
98}
99
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200100static gint devinfo_cmp(gconstpointer a, gconstpointer b)
101{
102 return strcasecmp(object_class_get_name((ObjectClass *)a),
103 object_class_get_name((ObjectClass *)b));
104}
105
106static void qdev_print_devinfos(bool show_no_user)
107{
108 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
109 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
110 [DEVICE_CATEGORY_USB] = "USB",
111 [DEVICE_CATEGORY_STORAGE] = "Storage",
112 [DEVICE_CATEGORY_NETWORK] = "Network",
113 [DEVICE_CATEGORY_INPUT] = "Input",
114 [DEVICE_CATEGORY_DISPLAY] = "Display",
115 [DEVICE_CATEGORY_SOUND] = "Sound",
116 [DEVICE_CATEGORY_MISC] = "Misc",
117 [DEVICE_CATEGORY_MAX] = "Uncategorized",
118 };
119 GSList *list, *elt;
120 int i;
121 bool cat_printed;
122
123 list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
124 devinfo_cmp);
125
126 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
127 cat_printed = false;
128 for (elt = list; elt; elt = elt->next) {
129 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
130 TYPE_DEVICE);
131 if ((i < DEVICE_CATEGORY_MAX
132 ? !test_bit(i, dc->categories)
133 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100134 || (!show_no_user
135 && dc->cannot_instantiate_with_device_add_yet)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200136 continue;
137 }
138 if (!cat_printed) {
139 error_printf("%s%s devices:\n", i ? "\n" : "",
140 cat_name[i]);
141 cat_printed = true;
142 }
143 qdev_print_devinfo(dc);
144 }
145 }
146
147 g_slist_free(list);
148}
149
Markus Armbruster71df1d82015-03-12 08:40:25 +0100150static int set_property(void *opaque, const char *name, const char *value,
151 Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600152{
Paolo Bonzini98a65282014-02-08 11:01:49 +0100153 Object *obj = opaque;
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200154 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600155
156 if (strcmp(name, "driver") == 0)
157 return 0;
158 if (strcmp(name, "bus") == 0)
159 return 0;
160
Paolo Bonzini98a65282014-02-08 11:01:49 +0100161 object_property_parse(obj, value, name, &err);
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200162 if (err != NULL) {
Markus Armbruster4caa4892015-03-12 13:58:02 +0100163 error_propagate(errp, err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600164 return -1;
165 }
166 return 0;
167}
168
169static const char *find_typename_by_alias(const char *alias)
170{
171 int i;
172
173 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200174 if (qdev_alias_table[i].arch_mask &&
175 !(qdev_alias_table[i].arch_mask & arch_type)) {
176 continue;
177 }
178
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600179 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
180 return qdev_alias_table[i].typename;
181 }
182 }
183
184 return NULL;
185}
186
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200187static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
188{
189 ObjectClass *oc;
190 DeviceClass *dc;
Sascha Silbef6b53192016-02-18 22:44:12 +0100191 const char *original_name = *driver;
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200192
193 oc = object_class_by_name(*driver);
194 if (!oc) {
195 const char *typename = find_typename_by_alias(*driver);
196
197 if (typename) {
198 *driver = typename;
199 oc = object_class_by_name(*driver);
200 }
201 }
202
203 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
Sascha Silbef6b53192016-02-18 22:44:12 +0100204 if (*driver != original_name) {
205 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
206 " name", original_name, *driver);
207 } else {
208 error_setg(errp, "'%s' is not a valid device model name", *driver);
209 }
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200210 return NULL;
211 }
212
213 if (object_class_is_abstract(oc)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100214 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
215 "non-abstract device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200216 return NULL;
217 }
218
219 dc = DEVICE_CLASS(oc);
220 if (dc->cannot_instantiate_with_device_add_yet ||
221 (qdev_hotplug && !dc->hotpluggable)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100222 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
223 "pluggable device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200224 return NULL;
225 }
226
227 return dc;
228}
229
230
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600231int qdev_device_help(QemuOpts *opts)
232{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200233 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600234 const char *driver;
Stefan Hajnoczief523582014-07-09 14:01:32 +0200235 DevicePropertyInfoList *prop_list;
236 DevicePropertyInfoList *prop;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600237
238 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100239 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200240 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600241 return 1;
242 }
243
Peter Maydellc8057f92012-08-02 13:45:54 +0100244 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600245 return 0;
246 }
247
Markus Armbruster33fe9682015-10-01 10:59:59 +0200248 if (!object_class_by_name(driver)) {
249 const char *typename = find_typename_by_alias(driver);
250
251 if (typename) {
252 driver = typename;
253 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600254 }
255
Stefan Hajnoczief523582014-07-09 14:01:32 +0200256 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800257 if (local_err) {
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200258 goto error;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600259 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200260
261 for (prop = prop_list; prop; prop = prop->next) {
Gonglei07d09c52014-10-07 14:33:23 +0800262 error_printf("%s.%s=%s", driver,
Stefan Hajnoczief523582014-07-09 14:01:32 +0200263 prop->value->name,
264 prop->value->type);
Gonglei07d09c52014-10-07 14:33:23 +0800265 if (prop->value->has_description) {
266 error_printf(" (%s)\n", prop->value->description);
267 } else {
268 error_printf("\n");
269 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200270 }
271
272 qapi_free_DevicePropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600273 return 1;
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200274
275error:
Markus Armbruster78288672015-12-18 16:35:07 +0100276 error_report_err(local_err);
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200277 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600278}
279
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600280static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600281{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600282 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600283
284 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200285 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600286 }
287
Anthony Liguori8b45d442011-12-23 09:08:05 -0600288 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600289}
290
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600291static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600292{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600293 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600294
295 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200296 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600297 }
298
Anthony Liguori8b45d442011-12-23 09:08:05 -0600299 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600300}
301
Eric Blake50b7b002015-09-10 10:19:16 -0600302static void qbus_list_bus(DeviceState *dev, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600303{
304 BusState *child;
305 const char *sep = " ";
306
Eric Blake50b7b002015-09-10 10:19:16 -0600307 error_append_hint(errp, "child buses at \"%s\":",
308 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600309 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Eric Blake50b7b002015-09-10 10:19:16 -0600310 error_append_hint(errp, "%s\"%s\"", sep, child->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600311 sep = ", ";
312 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100313 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600314}
315
Eric Blake50b7b002015-09-10 10:19:16 -0600316static void qbus_list_dev(BusState *bus, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600317{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600318 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600319 const char *sep = " ";
320
Eric Blake50b7b002015-09-10 10:19:16 -0600321 error_append_hint(errp, "devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600322 QTAILQ_FOREACH(kid, &bus->children, sibling) {
323 DeviceState *dev = kid->child;
Eric Blake50b7b002015-09-10 10:19:16 -0600324 error_append_hint(errp, "%s\"%s\"", sep,
325 object_get_typename(OBJECT(dev)));
326 if (dev->id) {
327 error_append_hint(errp, "/\"%s\"", dev->id);
328 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600329 sep = ", ";
330 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100331 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600332}
333
334static BusState *qbus_find_bus(DeviceState *dev, char *elem)
335{
336 BusState *child;
337
338 QLIST_FOREACH(child, &dev->child_bus, sibling) {
339 if (strcmp(child->name, elem) == 0) {
340 return child;
341 }
342 }
343 return NULL;
344}
345
346static DeviceState *qbus_find_dev(BusState *bus, char *elem)
347{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600348 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600349
350 /*
351 * try to match in order:
352 * (1) instance id, if present
353 * (2) driver name
354 * (3) driver alias, if present
355 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600356 QTAILQ_FOREACH(kid, &bus->children, sibling) {
357 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600358 if (dev->id && strcmp(dev->id, elem) == 0) {
359 return dev;
360 }
361 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600362 QTAILQ_FOREACH(kid, &bus->children, sibling) {
363 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600364 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
365 return dev;
366 }
367 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600368 QTAILQ_FOREACH(kid, &bus->children, sibling) {
369 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600370 DeviceClass *dc = DEVICE_GET_CLASS(dev);
371
372 if (qdev_class_has_alias(dc) &&
373 strcmp(qdev_class_get_alias(dc), elem) == 0) {
374 return dev;
375 }
376 }
377 return NULL;
378}
379
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100380static inline bool qbus_is_full(BusState *bus)
381{
382 BusClass *bus_class = BUS_GET_CLASS(bus);
383 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
384}
385
386/*
387 * Search the tree rooted at @bus for a bus.
388 * If @name, search for a bus with that name. Note that bus names
389 * need not be unique. Yes, that's screwed up.
390 * Else search for a bus that is a subtype of @bus_typename.
391 * If more than one exists, prefer one that can take another device.
392 * Return the bus if found, else %NULL.
393 */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600394static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200395 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600396{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600397 BusChild *kid;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100398 BusState *pick, *child, *ret;
399 bool match;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600400
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100401 assert(name || bus_typename);
402 if (name) {
403 match = !strcmp(bus->name, name);
404 } else {
405 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
KONRAD Frederic1395af62013-01-15 00:08:00 +0100406 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100407
408 if (match && !qbus_is_full(bus)) {
409 return bus; /* root matches and isn't full */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600410 }
411
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100412 pick = match ? bus : NULL;
413
Anthony Liguori0866aca2011-12-23 15:34:39 -0600414 QTAILQ_FOREACH(kid, &bus->children, sibling) {
415 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600416 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200417 ret = qbus_find_recursive(child, name, bus_typename);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100418 if (ret && !qbus_is_full(ret)) {
419 return ret; /* a descendant matches and isn't full */
420 }
421 if (ret && !pick) {
422 pick = ret;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600423 }
424 }
425 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100426
427 /* root or a descendant matches, but is full */
428 return pick;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600429}
430
Markus Armbrusterd2828422015-03-11 19:16:04 +0100431static BusState *qbus_find(const char *path, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600432{
433 DeviceState *dev;
434 BusState *bus;
435 char elem[128];
436 int pos, len;
437
438 /* find start element */
439 if (path[0] == '/') {
440 bus = sysbus_get_default();
441 pos = 0;
442 } else {
443 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
444 assert(!path[0]);
445 elem[0] = len = 0;
446 }
447 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
448 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100449 error_setg(errp, "Bus '%s' not found", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600450 return NULL;
451 }
452 pos = len;
453 }
454
455 for (;;) {
456 assert(path[pos] == '/' || !path[pos]);
457 while (path[pos] == '/') {
458 pos++;
459 }
460 if (path[pos] == '\0') {
Markus Armbrustered238ba2015-03-11 18:39:16 +0100461 break;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600462 }
463
464 /* find device */
465 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200466 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600467 elem[0] = len = 0;
468 }
469 pos += len;
470 dev = qbus_find_dev(bus, elem);
471 if (!dev) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100472 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
473 "Device '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600474 qbus_list_dev(bus, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600475 return NULL;
476 }
477
478 assert(path[pos] == '/' || !path[pos]);
479 while (path[pos] == '/') {
480 pos++;
481 }
482 if (path[pos] == '\0') {
483 /* last specified element is a device. If it has exactly
484 * one child bus accept it nevertheless */
Markus Armbrustered238ba2015-03-11 18:39:16 +0100485 if (dev->num_child_bus == 1) {
486 bus = QLIST_FIRST(&dev->child_bus);
487 break;
488 }
489 if (dev->num_child_bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100490 error_setg(errp, "Device '%s' has multiple child buses",
491 elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600492 qbus_list_bus(dev, errp);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100493 } else {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100494 error_setg(errp, "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600495 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100496 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600497 }
498
499 /* find bus */
500 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200501 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600502 elem[0] = len = 0;
503 }
504 pos += len;
505 bus = qbus_find_bus(dev, elem);
506 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100507 error_setg(errp, "Bus '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600508 qbus_list_bus(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600509 return NULL;
510 }
511 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100512
513 if (qbus_is_full(bus)) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100514 error_setg(errp, "Bus '%s' is full", path);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100515 return NULL;
516 }
517 return bus;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600518}
519
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100520DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600521{
Andreas Färberf4d85792013-08-24 01:21:22 +0200522 DeviceClass *dc;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600523 const char *driver, *path, *id;
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200524 DeviceState *dev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200525 BusState *bus = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200526 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600527
528 driver = qemu_opt_get(opts, "driver");
529 if (!driver) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100530 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600531 return NULL;
532 }
533
534 /* find driver */
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100535 dc = qdev_get_device_class(&driver, errp);
536 if (!dc) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100537 return NULL;
538 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600539
540 /* find bus */
541 path = qemu_opt_get(opts, "bus");
542 if (path != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100543 bus = qbus_find(path, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600544 if (!bus) {
545 return NULL;
546 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200547 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100548 error_setg(errp, "Device '%s' can't go on %s bus",
549 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600550 return NULL;
551 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200552 } else if (dc->bus_type != NULL) {
553 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100554 if (!bus || qbus_is_full(bus)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100555 error_setg(errp, "No '%s' bus found for device '%s'",
556 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600557 return NULL;
558 }
559 }
Igor Mammedov39b888b2014-09-26 09:28:17 +0000560 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100561 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600562 return NULL;
563 }
564
Amos Kong7b030942014-03-03 15:57:55 +0800565 /* create device */
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200566 dev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200567
568 if (bus) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200569 qdev_set_parent_bus(dev, bus);
Andreas Färber2f7bd822013-04-16 03:50:21 +0200570 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600571
572 id = qemu_opts_id(opts);
573 if (id) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200574 dev->id = id;
Anthony Liguorib2d4b3f2012-02-12 11:36:24 -0600575 }
Amos Kong7b030942014-03-03 15:57:55 +0800576
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200577 if (dev->id) {
578 object_property_add_child(qdev_get_peripheral(), dev->id,
579 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600580 } else {
581 static int anon_count;
582 gchar *name = g_strdup_printf("device[%d]", anon_count++);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600583 object_property_add_child(qdev_get_peripheral_anon(), name,
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200584 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600585 g_free(name);
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200586 }
Bandan Das52aa17c2014-02-26 10:32:40 -0700587
Amos Kong7b030942014-03-03 15:57:55 +0800588 /* set properties */
Markus Armbruster4caa4892015-03-12 13:58:02 +0100589 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100590 error_propagate(errp, err);
Amos Kong7b030942014-03-03 15:57:55 +0800591 object_unparent(OBJECT(dev));
592 object_unref(OBJECT(dev));
593 return NULL;
594 }
595
Bandan Das52aa17c2014-02-26 10:32:40 -0700596 dev->opts = opts;
Andreas Färber852e2c52013-10-07 16:42:34 +0200597 object_property_set_bool(OBJECT(dev), true, "realized", &err);
598 if (err != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100599 error_propagate(errp, err);
Bandan Das52aa17c2014-02-26 10:32:40 -0700600 dev->opts = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200601 object_unparent(OBJECT(dev));
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200602 object_unref(OBJECT(dev));
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200603 return NULL;
604 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200605 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600606}
607
608
609#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
610static void qbus_print(Monitor *mon, BusState *bus, int indent);
611
612static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200613 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600614{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600615 if (!props)
616 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100617 for (; props->name; props++) {
618 Error *err = NULL;
619 char *value;
620 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
621 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
622 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
623 } else {
Paolo Bonzinidae3bda2014-02-08 11:01:51 +0100624 value = object_property_print(OBJECT(dev), props->name, true, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600625 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100626 g_free(legacy_name);
627
628 if (err) {
629 error_free(err);
630 continue;
631 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200632 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100633 value && *value ? value : "<null>");
634 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600635 }
636}
637
Anthony Liguori0d936922012-05-02 09:00:20 +0200638static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
639{
640 BusClass *bc = BUS_GET_CLASS(bus);
641
642 if (bc->print_dev) {
643 bc->print_dev(mon, dev, indent);
644 }
645}
646
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600647static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
648{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200649 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600650 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700651 NamedGPIOList *ngl;
652
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600653 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
654 dev->id ? dev->id : "");
655 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700656 QLIST_FOREACH(ngl, &dev->gpios, node) {
657 if (ngl->num_in) {
658 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
659 ngl->num_in);
660 }
661 if (ngl->num_out) {
662 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
663 ngl->num_out);
664 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600665 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200666 class = object_get_class(OBJECT(dev));
667 do {
668 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
669 class = object_class_get_parent(class);
670 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200671 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600672 QLIST_FOREACH(child, &dev->child_bus, sibling) {
673 qbus_print(mon, child, indent);
674 }
675}
676
677static void qbus_print(Monitor *mon, BusState *bus, int indent)
678{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600679 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600680
681 qdev_printf("bus: %s\n", bus->name);
682 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200683 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600684 QTAILQ_FOREACH(kid, &bus->children, sibling) {
685 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600686 qdev_print(mon, dev, indent);
687 }
688}
689#undef qdev_printf
690
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100691void hmp_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600692{
693 if (sysbus_get_default())
694 qbus_print(mon, sysbus_get_default(), 0);
695}
696
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100697void hmp_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600698{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200699 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600700}
701
Andreas Färbera01ff752014-05-07 17:03:18 +0200702typedef struct QOMCompositionState {
703 Monitor *mon;
704 int indent;
705} QOMCompositionState;
706
707static void print_qom_composition(Monitor *mon, Object *obj, int indent);
708
709static int print_qom_composition_child(Object *obj, void *opaque)
710{
711 QOMCompositionState *s = opaque;
712
713 print_qom_composition(s->mon, obj, s->indent);
714
715 return 0;
716}
717
718static void print_qom_composition(Monitor *mon, Object *obj, int indent)
719{
720 QOMCompositionState s = {
721 .mon = mon,
722 .indent = indent + 2,
723 };
724 char *name;
725
726 if (obj == object_get_root()) {
727 name = g_strdup("");
728 } else {
729 name = object_get_canonical_path_component(obj);
730 }
731 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
732 object_get_typename(obj));
733 g_free(name);
734 object_child_foreach(obj, print_qom_composition_child, &s);
735}
736
737void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
738{
739 const char *path = qdict_get_try_str(dict, "path");
740 Object *obj;
741 bool ambiguous = false;
742
743 if (path) {
744 obj = object_resolve_path(path, &ambiguous);
745 if (!obj) {
746 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
747 return;
748 }
749 if (ambiguous) {
750 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
751 return;
752 }
753 } else {
754 obj = qdev_get_machine();
755 }
756 print_qom_composition(mon, obj, 0);
757}
758
Markus Armbruster485febc2015-03-13 17:25:50 +0100759void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600760{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300761 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600762 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100763 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600764
Luiz Capitulino4e899782012-04-18 17:24:01 -0300765 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100766 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100767 error_propagate(errp, local_err);
768 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600769 }
770 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
771 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100772 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600773 }
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100774 dev = qdev_device_add(opts, &local_err);
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100775 if (!dev) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100776 error_propagate(errp, local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600777 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100778 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600779 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100780 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600781}
782
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300783void qmp_device_del(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600784{
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000785 Object *obj;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600786
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100787 if (id[0] == '/') {
788 obj = object_resolve_path(id, NULL);
789 } else {
790 char *root_path = object_get_canonical_path(qdev_get_peripheral());
791 char *path = g_strdup_printf("%s/%s", root_path, id);
792
793 g_free(root_path);
794 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
795 g_free(path);
796 }
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000797
798 if (!obj) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100799 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
800 "Device '%s' not found", id);
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300801 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600802 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300803
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100804 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
805 error_setg(errp, "%s is not a hotpluggable device", id);
806 return;
807 }
808
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000809 qdev_unplug(DEVICE(obj), errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600810}
811
812void qdev_machine_init(void)
813{
814 qdev_get_peripheral_anon();
815 qdev_get_peripheral();
816}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100817
818QemuOptsList qemu_device_opts = {
819 .name = "device",
820 .implied_opt_name = "driver",
821 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
822 .desc = {
823 /*
824 * no elements => accept any
825 * sanity checking will happen later
826 * when setting device properties
827 */
828 { /* end of list */ }
829 },
830};
831
832QemuOptsList qemu_global_opts = {
833 .name = "global",
834 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
835 .desc = {
836 {
837 .name = "driver",
838 .type = QEMU_OPT_STRING,
839 },{
840 .name = "property",
841 .type = QEMU_OPT_STRING,
842 },{
843 .name = "value",
844 .type = QEMU_OPT_STRING,
845 },
846 { /* end of list */ }
847 },
848};
849
850int qemu_global_option(const char *str)
851{
852 char driver[64], property[64];
853 QemuOpts *opts;
854 int rc, offset;
855
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200856 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
857 if (rc == 2 && str[offset] == '=') {
858 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
859 qemu_opt_set(opts, "driver", driver, &error_abort);
860 qemu_opt_set(opts, "property", property, &error_abort);
861 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
862 return 0;
863 }
864
Markus Armbruster70b94332015-02-13 12:50:26 +0100865 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200866 if (!opts) {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100867 return -1;
868 }
869
Paolo Bonzini4d454572012-11-26 16:03:42 +0100870 return 0;
871}