blob: a35098f711b7f1333f4a9de16935159f1926001c [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
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010020#include "hw/qdev.h"
Andreas Färber2f7bd822013-04-16 03:50:21 +020021#include "hw/sysbus.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010022#include "monitor/monitor.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010023#include "monitor/qdev.h"
Luiz Capitulinoa15fef22012-03-29 12:38:50 -030024#include "qmp-commands.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/arch_init.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010026#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010028#include "qemu/error-report.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060029
30/*
31 * Aliases were a bad idea from the start. Let's keep them
32 * from spreading further.
33 */
34typedef struct QDevAlias
35{
36 const char *typename;
37 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020038 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060039} QDevAlias;
40
41static const QDevAlias qdev_alias_table[] = {
Alexander Graf5f629d92012-05-18 02:36:26 +020042 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
43 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
44 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
45 { "virtio-balloon-pci", "virtio-balloon",
46 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020047 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
48 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
49 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060050 { "lsi53c895a", "lsi" },
51 { "ich9-ahci", "ahci" },
Jan Kiszkac3ebd3b2012-08-30 20:30:00 +020052 { "kvm-pci-assign", "pci-assign" },
Jason Wang83044022015-09-28 13:37:26 +080053 { "e1000", "e1000-82540em" },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060054 { }
55};
56
57static const char *qdev_class_get_alias(DeviceClass *dc)
58{
59 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
60 int i;
61
62 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +020063 if (qdev_alias_table[i].arch_mask &&
64 !(qdev_alias_table[i].arch_mask & arch_type)) {
65 continue;
66 }
67
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060068 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
69 return qdev_alias_table[i].alias;
70 }
71 }
72
73 return NULL;
74}
75
76static bool qdev_class_has_alias(DeviceClass *dc)
77{
78 return (qdev_class_get_alias(dc) != NULL);
79}
80
Markus Armbrustera3400ae2013-10-10 15:00:21 +020081static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060082{
Markus Armbrustera3400ae2013-10-10 15:00:21 +020083 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +020084 if (dc->bus_type) {
85 error_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060086 }
87 if (qdev_class_has_alias(dc)) {
88 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
89 }
90 if (dc->desc) {
91 error_printf(", desc \"%s\"", dc->desc);
92 }
Markus Armbrusterefec3dd2013-11-28 17:26:54 +010093 if (dc->cannot_instantiate_with_device_add_yet) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060094 error_printf(", no-user");
95 }
96 error_printf("\n");
97}
98
Markus Armbrustera3400ae2013-10-10 15:00:21 +020099static gint devinfo_cmp(gconstpointer a, gconstpointer b)
100{
101 return strcasecmp(object_class_get_name((ObjectClass *)a),
102 object_class_get_name((ObjectClass *)b));
103}
104
105static void qdev_print_devinfos(bool show_no_user)
106{
107 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
108 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
109 [DEVICE_CATEGORY_USB] = "USB",
110 [DEVICE_CATEGORY_STORAGE] = "Storage",
111 [DEVICE_CATEGORY_NETWORK] = "Network",
112 [DEVICE_CATEGORY_INPUT] = "Input",
113 [DEVICE_CATEGORY_DISPLAY] = "Display",
114 [DEVICE_CATEGORY_SOUND] = "Sound",
115 [DEVICE_CATEGORY_MISC] = "Misc",
116 [DEVICE_CATEGORY_MAX] = "Uncategorized",
117 };
118 GSList *list, *elt;
119 int i;
120 bool cat_printed;
121
122 list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
123 devinfo_cmp);
124
125 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
126 cat_printed = false;
127 for (elt = list; elt; elt = elt->next) {
128 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
129 TYPE_DEVICE);
130 if ((i < DEVICE_CATEGORY_MAX
131 ? !test_bit(i, dc->categories)
132 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100133 || (!show_no_user
134 && dc->cannot_instantiate_with_device_add_yet)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200135 continue;
136 }
137 if (!cat_printed) {
138 error_printf("%s%s devices:\n", i ? "\n" : "",
139 cat_name[i]);
140 cat_printed = true;
141 }
142 qdev_print_devinfo(dc);
143 }
144 }
145
146 g_slist_free(list);
147}
148
Markus Armbruster71df1d82015-03-12 08:40:25 +0100149static int set_property(void *opaque, const char *name, const char *value,
150 Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600151{
Paolo Bonzini98a65282014-02-08 11:01:49 +0100152 Object *obj = opaque;
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200153 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600154
155 if (strcmp(name, "driver") == 0)
156 return 0;
157 if (strcmp(name, "bus") == 0)
158 return 0;
159
Paolo Bonzini98a65282014-02-08 11:01:49 +0100160 object_property_parse(obj, value, name, &err);
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200161 if (err != NULL) {
Markus Armbruster4caa4892015-03-12 13:58:02 +0100162 error_propagate(errp, err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600163 return -1;
164 }
165 return 0;
166}
167
168static const char *find_typename_by_alias(const char *alias)
169{
170 int i;
171
172 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200173 if (qdev_alias_table[i].arch_mask &&
174 !(qdev_alias_table[i].arch_mask & arch_type)) {
175 continue;
176 }
177
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600178 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
179 return qdev_alias_table[i].typename;
180 }
181 }
182
183 return NULL;
184}
185
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200186static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
187{
188 ObjectClass *oc;
189 DeviceClass *dc;
190
191 oc = object_class_by_name(*driver);
192 if (!oc) {
193 const char *typename = find_typename_by_alias(*driver);
194
195 if (typename) {
196 *driver = typename;
197 oc = object_class_by_name(*driver);
198 }
199 }
200
201 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
202 error_setg(errp, "'%s' is not a valid device model name", *driver);
203 return NULL;
204 }
205
206 if (object_class_is_abstract(oc)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100207 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
208 "non-abstract device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200209 return NULL;
210 }
211
212 dc = DEVICE_CLASS(oc);
213 if (dc->cannot_instantiate_with_device_add_yet ||
214 (qdev_hotplug && !dc->hotpluggable)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100215 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
216 "pluggable device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200217 return NULL;
218 }
219
220 return dc;
221}
222
223
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600224int qdev_device_help(QemuOpts *opts)
225{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200226 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600227 const char *driver;
Stefan Hajnoczief523582014-07-09 14:01:32 +0200228 DevicePropertyInfoList *prop_list;
229 DevicePropertyInfoList *prop;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600230
231 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100232 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200233 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600234 return 1;
235 }
236
Peter Maydellc8057f92012-08-02 13:45:54 +0100237 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600238 return 0;
239 }
240
Markus Armbruster33fe9682015-10-01 10:59:59 +0200241 if (!object_class_by_name(driver)) {
242 const char *typename = find_typename_by_alias(driver);
243
244 if (typename) {
245 driver = typename;
246 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600247 }
248
Stefan Hajnoczief523582014-07-09 14:01:32 +0200249 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800250 if (local_err) {
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200251 goto error;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600252 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200253
254 for (prop = prop_list; prop; prop = prop->next) {
Gonglei07d09c52014-10-07 14:33:23 +0800255 error_printf("%s.%s=%s", driver,
Stefan Hajnoczief523582014-07-09 14:01:32 +0200256 prop->value->name,
257 prop->value->type);
Gonglei07d09c52014-10-07 14:33:23 +0800258 if (prop->value->has_description) {
259 error_printf(" (%s)\n", prop->value->description);
260 } else {
261 error_printf("\n");
262 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200263 }
264
265 qapi_free_DevicePropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600266 return 1;
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200267
268error:
269 error_printf("%s\n", error_get_pretty(local_err));
270 error_free(local_err);
271 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600272}
273
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600274static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600275{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600276 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600277
278 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200279 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600280 }
281
Anthony Liguori8b45d442011-12-23 09:08:05 -0600282 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600283}
284
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600285static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600286{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600287 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600288
289 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200290 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600291 }
292
Anthony Liguori8b45d442011-12-23 09:08:05 -0600293 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600294}
295
Eric Blake50b7b002015-09-10 10:19:16 -0600296static void qbus_list_bus(DeviceState *dev, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600297{
298 BusState *child;
299 const char *sep = " ";
300
Eric Blake50b7b002015-09-10 10:19:16 -0600301 error_append_hint(errp, "child buses at \"%s\":",
302 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600303 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Eric Blake50b7b002015-09-10 10:19:16 -0600304 error_append_hint(errp, "%s\"%s\"", sep, child->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600305 sep = ", ";
306 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600307}
308
Eric Blake50b7b002015-09-10 10:19:16 -0600309static void qbus_list_dev(BusState *bus, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600310{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600311 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600312 const char *sep = " ";
313
Eric Blake50b7b002015-09-10 10:19:16 -0600314 error_append_hint(errp, "devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600315 QTAILQ_FOREACH(kid, &bus->children, sibling) {
316 DeviceState *dev = kid->child;
Eric Blake50b7b002015-09-10 10:19:16 -0600317 error_append_hint(errp, "%s\"%s\"", sep,
318 object_get_typename(OBJECT(dev)));
319 if (dev->id) {
320 error_append_hint(errp, "/\"%s\"", dev->id);
321 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600322 sep = ", ";
323 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600324}
325
326static BusState *qbus_find_bus(DeviceState *dev, char *elem)
327{
328 BusState *child;
329
330 QLIST_FOREACH(child, &dev->child_bus, sibling) {
331 if (strcmp(child->name, elem) == 0) {
332 return child;
333 }
334 }
335 return NULL;
336}
337
338static DeviceState *qbus_find_dev(BusState *bus, char *elem)
339{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600340 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600341
342 /*
343 * try to match in order:
344 * (1) instance id, if present
345 * (2) driver name
346 * (3) driver alias, if present
347 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600348 QTAILQ_FOREACH(kid, &bus->children, sibling) {
349 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600350 if (dev->id && strcmp(dev->id, elem) == 0) {
351 return dev;
352 }
353 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600354 QTAILQ_FOREACH(kid, &bus->children, sibling) {
355 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600356 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
357 return dev;
358 }
359 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600360 QTAILQ_FOREACH(kid, &bus->children, sibling) {
361 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600362 DeviceClass *dc = DEVICE_GET_CLASS(dev);
363
364 if (qdev_class_has_alias(dc) &&
365 strcmp(qdev_class_get_alias(dc), elem) == 0) {
366 return dev;
367 }
368 }
369 return NULL;
370}
371
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100372static inline bool qbus_is_full(BusState *bus)
373{
374 BusClass *bus_class = BUS_GET_CLASS(bus);
375 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
376}
377
378/*
379 * Search the tree rooted at @bus for a bus.
380 * If @name, search for a bus with that name. Note that bus names
381 * need not be unique. Yes, that's screwed up.
382 * Else search for a bus that is a subtype of @bus_typename.
383 * If more than one exists, prefer one that can take another device.
384 * Return the bus if found, else %NULL.
385 */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600386static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200387 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600388{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600389 BusChild *kid;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100390 BusState *pick, *child, *ret;
391 bool match;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600392
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100393 assert(name || bus_typename);
394 if (name) {
395 match = !strcmp(bus->name, name);
396 } else {
397 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
KONRAD Frederic1395af62013-01-15 00:08:00 +0100398 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100399
400 if (match && !qbus_is_full(bus)) {
401 return bus; /* root matches and isn't full */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600402 }
403
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100404 pick = match ? bus : NULL;
405
Anthony Liguori0866aca2011-12-23 15:34:39 -0600406 QTAILQ_FOREACH(kid, &bus->children, sibling) {
407 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600408 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200409 ret = qbus_find_recursive(child, name, bus_typename);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100410 if (ret && !qbus_is_full(ret)) {
411 return ret; /* a descendant matches and isn't full */
412 }
413 if (ret && !pick) {
414 pick = ret;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600415 }
416 }
417 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100418
419 /* root or a descendant matches, but is full */
420 return pick;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600421}
422
Markus Armbrusterd2828422015-03-11 19:16:04 +0100423static BusState *qbus_find(const char *path, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600424{
425 DeviceState *dev;
426 BusState *bus;
427 char elem[128];
428 int pos, len;
429
430 /* find start element */
431 if (path[0] == '/') {
432 bus = sysbus_get_default();
433 pos = 0;
434 } else {
435 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
436 assert(!path[0]);
437 elem[0] = len = 0;
438 }
439 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
440 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100441 error_setg(errp, "Bus '%s' not found", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600442 return NULL;
443 }
444 pos = len;
445 }
446
447 for (;;) {
448 assert(path[pos] == '/' || !path[pos]);
449 while (path[pos] == '/') {
450 pos++;
451 }
452 if (path[pos] == '\0') {
Markus Armbrustered238ba2015-03-11 18:39:16 +0100453 break;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600454 }
455
456 /* find device */
457 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200458 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600459 elem[0] = len = 0;
460 }
461 pos += len;
462 dev = qbus_find_dev(bus, elem);
463 if (!dev) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100464 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
465 "Device '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600466 qbus_list_dev(bus, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600467 return NULL;
468 }
469
470 assert(path[pos] == '/' || !path[pos]);
471 while (path[pos] == '/') {
472 pos++;
473 }
474 if (path[pos] == '\0') {
475 /* last specified element is a device. If it has exactly
476 * one child bus accept it nevertheless */
Markus Armbrustered238ba2015-03-11 18:39:16 +0100477 if (dev->num_child_bus == 1) {
478 bus = QLIST_FIRST(&dev->child_bus);
479 break;
480 }
481 if (dev->num_child_bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100482 error_setg(errp, "Device '%s' has multiple child buses",
483 elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600484 qbus_list_bus(dev, errp);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100485 } else {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100486 error_setg(errp, "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600487 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100488 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600489 }
490
491 /* find bus */
492 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200493 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600494 elem[0] = len = 0;
495 }
496 pos += len;
497 bus = qbus_find_bus(dev, elem);
498 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100499 error_setg(errp, "Bus '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600500 qbus_list_bus(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600501 return NULL;
502 }
503 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100504
505 if (qbus_is_full(bus)) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100506 error_setg(errp, "Bus '%s' is full", path);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100507 return NULL;
508 }
509 return bus;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600510}
511
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100512DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600513{
Andreas Färberf4d85792013-08-24 01:21:22 +0200514 DeviceClass *dc;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600515 const char *driver, *path, *id;
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200516 DeviceState *dev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200517 BusState *bus = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200518 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600519
520 driver = qemu_opt_get(opts, "driver");
521 if (!driver) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100522 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600523 return NULL;
524 }
525
526 /* find driver */
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100527 dc = qdev_get_device_class(&driver, errp);
528 if (!dc) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100529 return NULL;
530 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600531
532 /* find bus */
533 path = qemu_opt_get(opts, "bus");
534 if (path != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100535 bus = qbus_find(path, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600536 if (!bus) {
537 return NULL;
538 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200539 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100540 error_setg(errp, "Device '%s' can't go on %s bus",
541 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600542 return NULL;
543 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200544 } else if (dc->bus_type != NULL) {
545 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100546 if (!bus || qbus_is_full(bus)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100547 error_setg(errp, "No '%s' bus found for device '%s'",
548 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600549 return NULL;
550 }
551 }
Igor Mammedov39b888b2014-09-26 09:28:17 +0000552 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100553 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600554 return NULL;
555 }
556
Amos Kong7b030942014-03-03 15:57:55 +0800557 /* create device */
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200558 dev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200559
560 if (bus) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200561 qdev_set_parent_bus(dev, bus);
Andreas Färber2f7bd822013-04-16 03:50:21 +0200562 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600563
564 id = qemu_opts_id(opts);
565 if (id) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200566 dev->id = id;
Anthony Liguorib2d4b3f2012-02-12 11:36:24 -0600567 }
Amos Kong7b030942014-03-03 15:57:55 +0800568
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200569 if (dev->id) {
570 object_property_add_child(qdev_get_peripheral(), dev->id,
571 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600572 } else {
573 static int anon_count;
574 gchar *name = g_strdup_printf("device[%d]", anon_count++);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600575 object_property_add_child(qdev_get_peripheral_anon(), name,
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200576 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600577 g_free(name);
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200578 }
Bandan Das52aa17c2014-02-26 10:32:40 -0700579
Amos Kong7b030942014-03-03 15:57:55 +0800580 /* set properties */
Markus Armbruster4caa4892015-03-12 13:58:02 +0100581 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100582 error_propagate(errp, err);
Amos Kong7b030942014-03-03 15:57:55 +0800583 object_unparent(OBJECT(dev));
584 object_unref(OBJECT(dev));
585 return NULL;
586 }
587
Bandan Das52aa17c2014-02-26 10:32:40 -0700588 dev->opts = opts;
Andreas Färber852e2c52013-10-07 16:42:34 +0200589 object_property_set_bool(OBJECT(dev), true, "realized", &err);
590 if (err != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100591 error_propagate(errp, err);
Bandan Das52aa17c2014-02-26 10:32:40 -0700592 dev->opts = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200593 object_unparent(OBJECT(dev));
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200594 object_unref(OBJECT(dev));
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200595 return NULL;
596 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200597 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600598}
599
600
601#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
602static void qbus_print(Monitor *mon, BusState *bus, int indent);
603
604static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200605 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600606{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600607 if (!props)
608 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100609 for (; props->name; props++) {
610 Error *err = NULL;
611 char *value;
612 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
613 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
614 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
615 } else {
Paolo Bonzinidae3bda2014-02-08 11:01:51 +0100616 value = object_property_print(OBJECT(dev), props->name, true, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600617 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100618 g_free(legacy_name);
619
620 if (err) {
621 error_free(err);
622 continue;
623 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200624 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100625 value && *value ? value : "<null>");
626 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600627 }
628}
629
Anthony Liguori0d936922012-05-02 09:00:20 +0200630static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
631{
632 BusClass *bc = BUS_GET_CLASS(bus);
633
634 if (bc->print_dev) {
635 bc->print_dev(mon, dev, indent);
636 }
637}
638
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600639static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
640{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200641 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600642 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700643 NamedGPIOList *ngl;
644
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600645 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
646 dev->id ? dev->id : "");
647 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700648 QLIST_FOREACH(ngl, &dev->gpios, node) {
649 if (ngl->num_in) {
650 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
651 ngl->num_in);
652 }
653 if (ngl->num_out) {
654 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
655 ngl->num_out);
656 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600657 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200658 class = object_get_class(OBJECT(dev));
659 do {
660 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
661 class = object_class_get_parent(class);
662 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200663 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600664 QLIST_FOREACH(child, &dev->child_bus, sibling) {
665 qbus_print(mon, child, indent);
666 }
667}
668
669static void qbus_print(Monitor *mon, BusState *bus, int indent)
670{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600671 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600672
673 qdev_printf("bus: %s\n", bus->name);
674 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200675 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600676 QTAILQ_FOREACH(kid, &bus->children, sibling) {
677 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600678 qdev_print(mon, dev, indent);
679 }
680}
681#undef qdev_printf
682
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100683void hmp_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600684{
685 if (sysbus_get_default())
686 qbus_print(mon, sysbus_get_default(), 0);
687}
688
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100689void hmp_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600690{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200691 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600692}
693
Andreas Färbera01ff752014-05-07 17:03:18 +0200694typedef struct QOMCompositionState {
695 Monitor *mon;
696 int indent;
697} QOMCompositionState;
698
699static void print_qom_composition(Monitor *mon, Object *obj, int indent);
700
701static int print_qom_composition_child(Object *obj, void *opaque)
702{
703 QOMCompositionState *s = opaque;
704
705 print_qom_composition(s->mon, obj, s->indent);
706
707 return 0;
708}
709
710static void print_qom_composition(Monitor *mon, Object *obj, int indent)
711{
712 QOMCompositionState s = {
713 .mon = mon,
714 .indent = indent + 2,
715 };
716 char *name;
717
718 if (obj == object_get_root()) {
719 name = g_strdup("");
720 } else {
721 name = object_get_canonical_path_component(obj);
722 }
723 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
724 object_get_typename(obj));
725 g_free(name);
726 object_child_foreach(obj, print_qom_composition_child, &s);
727}
728
729void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
730{
731 const char *path = qdict_get_try_str(dict, "path");
732 Object *obj;
733 bool ambiguous = false;
734
735 if (path) {
736 obj = object_resolve_path(path, &ambiguous);
737 if (!obj) {
738 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
739 return;
740 }
741 if (ambiguous) {
742 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
743 return;
744 }
745 } else {
746 obj = qdev_get_machine();
747 }
748 print_qom_composition(mon, obj, 0);
749}
750
Markus Armbruster485febc2015-03-13 17:25:50 +0100751void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600752{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300753 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600754 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100755 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600756
Luiz Capitulino4e899782012-04-18 17:24:01 -0300757 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100758 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100759 error_propagate(errp, local_err);
760 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600761 }
762 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
763 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100764 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600765 }
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100766 dev = qdev_device_add(opts, &local_err);
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100767 if (!dev) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100768 error_propagate(errp, local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600769 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100770 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600771 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100772 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600773}
774
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300775void qmp_device_del(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600776{
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000777 Object *obj;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600778
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100779 if (id[0] == '/') {
780 obj = object_resolve_path(id, NULL);
781 } else {
782 char *root_path = object_get_canonical_path(qdev_get_peripheral());
783 char *path = g_strdup_printf("%s/%s", root_path, id);
784
785 g_free(root_path);
786 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
787 g_free(path);
788 }
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000789
790 if (!obj) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100791 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
792 "Device '%s' not found", id);
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300793 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600794 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300795
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100796 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
797 error_setg(errp, "%s is not a hotpluggable device", id);
798 return;
799 }
800
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000801 qdev_unplug(DEVICE(obj), errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600802}
803
804void qdev_machine_init(void)
805{
806 qdev_get_peripheral_anon();
807 qdev_get_peripheral();
808}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100809
810QemuOptsList qemu_device_opts = {
811 .name = "device",
812 .implied_opt_name = "driver",
813 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
814 .desc = {
815 /*
816 * no elements => accept any
817 * sanity checking will happen later
818 * when setting device properties
819 */
820 { /* end of list */ }
821 },
822};
823
824QemuOptsList qemu_global_opts = {
825 .name = "global",
826 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
827 .desc = {
828 {
829 .name = "driver",
830 .type = QEMU_OPT_STRING,
831 },{
832 .name = "property",
833 .type = QEMU_OPT_STRING,
834 },{
835 .name = "value",
836 .type = QEMU_OPT_STRING,
837 },
838 { /* end of list */ }
839 },
840};
841
842int qemu_global_option(const char *str)
843{
844 char driver[64], property[64];
845 QemuOpts *opts;
846 int rc, offset;
847
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200848 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
849 if (rc == 2 && str[offset] == '=') {
850 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
851 qemu_opt_set(opts, "driver", driver, &error_abort);
852 qemu_opt_set(opts, "property", property, &error_abort);
853 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
854 return 0;
855 }
856
Markus Armbruster70b94332015-02-13 12:50:26 +0100857 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200858 if (!opts) {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100859 return -1;
860 }
861
Paolo Bonzini4d454572012-11-26 16:03:42 +0100862 return 0;
863}