blob: f6db4614763c6b3a9bd585b4a9cb897928d383d7 [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/config-file.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060027
28/*
29 * Aliases were a bad idea from the start. Let's keep them
30 * from spreading further.
31 */
32typedef struct QDevAlias
33{
34 const char *typename;
35 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020036 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060037} QDevAlias;
38
39static const QDevAlias qdev_alias_table[] = {
Alexander Graf5f629d92012-05-18 02:36:26 +020040 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
41 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
42 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
43 { "virtio-balloon-pci", "virtio-balloon",
44 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
45 { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X },
46 { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X },
47 { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060048 { "lsi53c895a", "lsi" },
49 { "ich9-ahci", "ahci" },
Jan Kiszkac3ebd3b2012-08-30 20:30:00 +020050 { "kvm-pci-assign", "pci-assign" },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060051 { }
52};
53
54static const char *qdev_class_get_alias(DeviceClass *dc)
55{
56 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
57 int i;
58
59 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +020060 if (qdev_alias_table[i].arch_mask &&
61 !(qdev_alias_table[i].arch_mask & arch_type)) {
62 continue;
63 }
64
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060065 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
66 return qdev_alias_table[i].alias;
67 }
68 }
69
70 return NULL;
71}
72
73static bool qdev_class_has_alias(DeviceClass *dc)
74{
75 return (qdev_class_get_alias(dc) != NULL);
76}
77
Markus Armbrustera3400ae2013-10-10 15:00:21 +020078static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060079{
Markus Armbrustera3400ae2013-10-10 15:00:21 +020080 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +020081 if (dc->bus_type) {
82 error_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060083 }
84 if (qdev_class_has_alias(dc)) {
85 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
86 }
87 if (dc->desc) {
88 error_printf(", desc \"%s\"", dc->desc);
89 }
Markus Armbrusterefec3dd2013-11-28 17:26:54 +010090 if (dc->cannot_instantiate_with_device_add_yet) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060091 error_printf(", no-user");
92 }
93 error_printf("\n");
94}
95
Markus Armbrustera3400ae2013-10-10 15:00:21 +020096static gint devinfo_cmp(gconstpointer a, gconstpointer b)
97{
98 return strcasecmp(object_class_get_name((ObjectClass *)a),
99 object_class_get_name((ObjectClass *)b));
100}
101
102static void qdev_print_devinfos(bool show_no_user)
103{
104 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
105 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
106 [DEVICE_CATEGORY_USB] = "USB",
107 [DEVICE_CATEGORY_STORAGE] = "Storage",
108 [DEVICE_CATEGORY_NETWORK] = "Network",
109 [DEVICE_CATEGORY_INPUT] = "Input",
110 [DEVICE_CATEGORY_DISPLAY] = "Display",
111 [DEVICE_CATEGORY_SOUND] = "Sound",
112 [DEVICE_CATEGORY_MISC] = "Misc",
113 [DEVICE_CATEGORY_MAX] = "Uncategorized",
114 };
115 GSList *list, *elt;
116 int i;
117 bool cat_printed;
118
119 list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
120 devinfo_cmp);
121
122 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
123 cat_printed = false;
124 for (elt = list; elt; elt = elt->next) {
125 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
126 TYPE_DEVICE);
127 if ((i < DEVICE_CATEGORY_MAX
128 ? !test_bit(i, dc->categories)
129 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100130 || (!show_no_user
131 && dc->cannot_instantiate_with_device_add_yet)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200132 continue;
133 }
134 if (!cat_printed) {
135 error_printf("%s%s devices:\n", i ? "\n" : "",
136 cat_name[i]);
137 cat_printed = true;
138 }
139 qdev_print_devinfo(dc);
140 }
141 }
142
143 g_slist_free(list);
144}
145
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600146static int set_property(const char *name, const char *value, void *opaque)
147{
Paolo Bonzini98a65282014-02-08 11:01:49 +0100148 Object *obj = opaque;
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200149 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600150
151 if (strcmp(name, "driver") == 0)
152 return 0;
153 if (strcmp(name, "bus") == 0)
154 return 0;
155
Paolo Bonzini98a65282014-02-08 11:01:49 +0100156 object_property_parse(obj, value, name, &err);
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200157 if (err != NULL) {
158 qerror_report_err(err);
159 error_free(err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600160 return -1;
161 }
162 return 0;
163}
164
165static const char *find_typename_by_alias(const char *alias)
166{
167 int i;
168
169 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200170 if (qdev_alias_table[i].arch_mask &&
171 !(qdev_alias_table[i].arch_mask & arch_type)) {
172 continue;
173 }
174
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600175 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
176 return qdev_alias_table[i].typename;
177 }
178 }
179
180 return NULL;
181}
182
183int qdev_device_help(QemuOpts *opts)
184{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200185 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600186 const char *driver;
Stefan Hajnoczief523582014-07-09 14:01:32 +0200187 DevicePropertyInfoList *prop_list;
188 DevicePropertyInfoList *prop;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600189
190 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100191 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200192 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600193 return 1;
194 }
195
Peter Maydellc8057f92012-08-02 13:45:54 +0100196 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600197 return 0;
198 }
199
Stefan Hajnoczief523582014-07-09 14:01:32 +0200200 if (!object_class_by_name(driver)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600201 const char *typename = find_typename_by_alias(driver);
202
203 if (typename) {
204 driver = typename;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600205 }
206 }
207
Stefan Hajnoczief523582014-07-09 14:01:32 +0200208 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800209 if (local_err) {
Stefan Hajnoczief523582014-07-09 14:01:32 +0200210 error_printf("%s\n", error_get_pretty(local_err));
211 error_free(local_err);
212 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600213 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200214
215 for (prop = prop_list; prop; prop = prop->next) {
216 error_printf("%s.%s=%s\n", driver,
217 prop->value->name,
218 prop->value->type);
219 }
220
221 qapi_free_DevicePropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600222 return 1;
223}
224
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600225static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600226{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600227 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600228
229 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200230 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600231 }
232
Anthony Liguori8b45d442011-12-23 09:08:05 -0600233 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600234}
235
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600236static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600237{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600238 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600239
240 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200241 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600242 }
243
Anthony Liguori8b45d442011-12-23 09:08:05 -0600244 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600245}
246
247static void qbus_list_bus(DeviceState *dev)
248{
249 BusState *child;
250 const char *sep = " ";
251
252 error_printf("child busses at \"%s\":",
253 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
254 QLIST_FOREACH(child, &dev->child_bus, sibling) {
255 error_printf("%s\"%s\"", sep, child->name);
256 sep = ", ";
257 }
258 error_printf("\n");
259}
260
261static void qbus_list_dev(BusState *bus)
262{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600263 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600264 const char *sep = " ";
265
266 error_printf("devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600267 QTAILQ_FOREACH(kid, &bus->children, sibling) {
268 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600269 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
270 if (dev->id)
271 error_printf("/\"%s\"", dev->id);
272 sep = ", ";
273 }
274 error_printf("\n");
275}
276
277static BusState *qbus_find_bus(DeviceState *dev, char *elem)
278{
279 BusState *child;
280
281 QLIST_FOREACH(child, &dev->child_bus, sibling) {
282 if (strcmp(child->name, elem) == 0) {
283 return child;
284 }
285 }
286 return NULL;
287}
288
289static DeviceState *qbus_find_dev(BusState *bus, char *elem)
290{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600291 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600292
293 /*
294 * try to match in order:
295 * (1) instance id, if present
296 * (2) driver name
297 * (3) driver alias, if present
298 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600299 QTAILQ_FOREACH(kid, &bus->children, sibling) {
300 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600301 if (dev->id && strcmp(dev->id, elem) == 0) {
302 return dev;
303 }
304 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600305 QTAILQ_FOREACH(kid, &bus->children, sibling) {
306 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600307 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
308 return dev;
309 }
310 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600311 QTAILQ_FOREACH(kid, &bus->children, sibling) {
312 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600313 DeviceClass *dc = DEVICE_GET_CLASS(dev);
314
315 if (qdev_class_has_alias(dc) &&
316 strcmp(qdev_class_get_alias(dc), elem) == 0) {
317 return dev;
318 }
319 }
320 return NULL;
321}
322
323static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200324 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600325{
KONRAD Frederic1395af62013-01-15 00:08:00 +0100326 BusClass *bus_class = BUS_GET_CLASS(bus);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600327 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600328 BusState *child, *ret;
329 int match = 1;
330
331 if (name && (strcmp(bus->name, name) != 0)) {
332 match = 0;
Alexey Kardashevskiy95e2af92013-04-17 17:49:00 +1000333 } else if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600334 match = 0;
Alexey Kardashevskiy95e2af92013-04-17 17:49:00 +1000335 } else if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) {
KONRAD Frederic1395af62013-01-15 00:08:00 +0100336 if (name != NULL) {
337 /* bus was explicitly specified: return an error. */
338 qerror_report(ERROR_CLASS_GENERIC_ERROR, "Bus '%s' is full",
339 bus->name);
340 return NULL;
341 } else {
342 /* bus was not specified: try to find another one. */
343 match = 0;
344 }
345 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600346 if (match) {
347 return bus;
348 }
349
Anthony Liguori0866aca2011-12-23 15:34:39 -0600350 QTAILQ_FOREACH(kid, &bus->children, sibling) {
351 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600352 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200353 ret = qbus_find_recursive(child, name, bus_typename);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600354 if (ret) {
355 return ret;
356 }
357 }
358 }
359 return NULL;
360}
361
362static BusState *qbus_find(const char *path)
363{
364 DeviceState *dev;
365 BusState *bus;
366 char elem[128];
367 int pos, len;
368
369 /* find start element */
370 if (path[0] == '/') {
371 bus = sysbus_get_default();
372 pos = 0;
373 } else {
374 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
375 assert(!path[0]);
376 elem[0] = len = 0;
377 }
378 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
379 if (!bus) {
380 qerror_report(QERR_BUS_NOT_FOUND, elem);
381 return NULL;
382 }
383 pos = len;
384 }
385
386 for (;;) {
387 assert(path[pos] == '/' || !path[pos]);
388 while (path[pos] == '/') {
389 pos++;
390 }
391 if (path[pos] == '\0') {
392 return bus;
393 }
394
395 /* find device */
396 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200397 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600398 elem[0] = len = 0;
399 }
400 pos += len;
401 dev = qbus_find_dev(bus, elem);
402 if (!dev) {
403 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
404 if (!monitor_cur_is_qmp()) {
405 qbus_list_dev(bus);
406 }
407 return NULL;
408 }
409
410 assert(path[pos] == '/' || !path[pos]);
411 while (path[pos] == '/') {
412 pos++;
413 }
414 if (path[pos] == '\0') {
415 /* last specified element is a device. If it has exactly
416 * one child bus accept it nevertheless */
417 switch (dev->num_child_bus) {
418 case 0:
Cole Robinsonf231b882014-03-21 19:42:26 -0400419 qerror_report(ERROR_CLASS_GENERIC_ERROR,
420 "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600421 return NULL;
422 case 1:
423 return QLIST_FIRST(&dev->child_bus);
424 default:
Cole Robinsonf231b882014-03-21 19:42:26 -0400425 qerror_report(ERROR_CLASS_GENERIC_ERROR,
426 "Device '%s' has multiple child busses", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600427 if (!monitor_cur_is_qmp()) {
428 qbus_list_bus(dev);
429 }
430 return NULL;
431 }
432 }
433
434 /* find bus */
435 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200436 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600437 elem[0] = len = 0;
438 }
439 pos += len;
440 bus = qbus_find_bus(dev, elem);
441 if (!bus) {
442 qerror_report(QERR_BUS_NOT_FOUND, elem);
443 if (!monitor_cur_is_qmp()) {
444 qbus_list_bus(dev);
445 }
446 return NULL;
447 }
448 }
449}
450
451DeviceState *qdev_device_add(QemuOpts *opts)
452{
Andreas Färberf4d85792013-08-24 01:21:22 +0200453 ObjectClass *oc;
454 DeviceClass *dc;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600455 const char *driver, *path, *id;
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200456 DeviceState *dev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200457 BusState *bus = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200458 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600459
460 driver = qemu_opt_get(opts, "driver");
461 if (!driver) {
462 qerror_report(QERR_MISSING_PARAMETER, "driver");
463 return NULL;
464 }
465
466 /* find driver */
Andreas Färberf4d85792013-08-24 01:21:22 +0200467 oc = object_class_by_name(driver);
468 if (!oc) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600469 const char *typename = find_typename_by_alias(driver);
470
471 if (typename) {
472 driver = typename;
Andreas Färberf4d85792013-08-24 01:21:22 +0200473 oc = object_class_by_name(driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600474 }
475 }
476
Markus Armbruster061e84f2013-11-28 17:02:24 +0100477 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
Markus Armbruster11c308b2013-12-19 15:30:13 +0100478 qerror_report(ERROR_CLASS_GENERIC_ERROR,
479 "'%s' is not a valid device model name", driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600480 return NULL;
481 }
482
Igor Mammedov2fa4e562013-09-17 15:32:32 +0200483 if (object_class_is_abstract(oc)) {
484 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver",
485 "non-abstract device type");
486 return NULL;
487 }
488
Andreas Färberf4d85792013-08-24 01:21:22 +0200489 dc = DEVICE_CLASS(oc);
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100490 if (dc->cannot_instantiate_with_device_add_yet) {
491 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver",
492 "pluggable device type");
493 return NULL;
494 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600495
496 /* find bus */
497 path = qemu_opt_get(opts, "bus");
498 if (path != NULL) {
499 bus = qbus_find(path);
500 if (!bus) {
501 return NULL;
502 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200503 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400504 qerror_report(ERROR_CLASS_GENERIC_ERROR,
505 "Device '%s' can't go on a %s bus",
Anthony Liguori0d936922012-05-02 09:00:20 +0200506 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600507 return NULL;
508 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200509 } else if (dc->bus_type != NULL) {
510 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600511 if (!bus) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400512 qerror_report(ERROR_CLASS_GENERIC_ERROR,
513 "No '%s' bus found for device '%s'",
Andreas Färberf4d85792013-08-24 01:21:22 +0200514 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600515 return NULL;
516 }
517 }
Igor Mammedov39b888b2014-09-26 09:28:17 +0000518 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600519 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
520 return NULL;
521 }
522
Amos Kong7b030942014-03-03 15:57:55 +0800523 /* create device */
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200524 dev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200525
526 if (bus) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200527 qdev_set_parent_bus(dev, bus);
Andreas Färber2f7bd822013-04-16 03:50:21 +0200528 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600529
530 id = qemu_opts_id(opts);
531 if (id) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200532 dev->id = id;
Anthony Liguorib2d4b3f2012-02-12 11:36:24 -0600533 }
Amos Kong7b030942014-03-03 15:57:55 +0800534
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200535 if (dev->id) {
536 object_property_add_child(qdev_get_peripheral(), dev->id,
537 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600538 } else {
539 static int anon_count;
540 gchar *name = g_strdup_printf("device[%d]", anon_count++);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600541 object_property_add_child(qdev_get_peripheral_anon(), name,
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200542 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600543 g_free(name);
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200544 }
Bandan Das52aa17c2014-02-26 10:32:40 -0700545
Amos Kong7b030942014-03-03 15:57:55 +0800546 /* set properties */
547 if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) {
548 object_unparent(OBJECT(dev));
549 object_unref(OBJECT(dev));
550 return NULL;
551 }
552
Bandan Das52aa17c2014-02-26 10:32:40 -0700553 dev->opts = opts;
Andreas Färber852e2c52013-10-07 16:42:34 +0200554 object_property_set_bool(OBJECT(dev), true, "realized", &err);
555 if (err != NULL) {
556 qerror_report_err(err);
557 error_free(err);
Bandan Das52aa17c2014-02-26 10:32:40 -0700558 dev->opts = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200559 object_unparent(OBJECT(dev));
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200560 object_unref(OBJECT(dev));
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200561 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
562 return NULL;
563 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200564 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600565}
566
567
568#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
569static void qbus_print(Monitor *mon, BusState *bus, int indent);
570
571static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200572 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600573{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600574 if (!props)
575 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100576 for (; props->name; props++) {
577 Error *err = NULL;
578 char *value;
579 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
580 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
581 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
582 } else {
Paolo Bonzinidae3bda2014-02-08 11:01:51 +0100583 value = object_property_print(OBJECT(dev), props->name, true, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600584 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100585 g_free(legacy_name);
586
587 if (err) {
588 error_free(err);
589 continue;
590 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200591 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100592 value && *value ? value : "<null>");
593 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600594 }
595}
596
Anthony Liguori0d936922012-05-02 09:00:20 +0200597static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
598{
599 BusClass *bc = BUS_GET_CLASS(bus);
600
601 if (bc->print_dev) {
602 bc->print_dev(mon, dev, indent);
603 }
604}
605
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600606static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
607{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200608 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600609 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700610 NamedGPIOList *ngl;
611
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600612 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
613 dev->id ? dev->id : "");
614 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700615 QLIST_FOREACH(ngl, &dev->gpios, node) {
616 if (ngl->num_in) {
617 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
618 ngl->num_in);
619 }
620 if (ngl->num_out) {
621 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
622 ngl->num_out);
623 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600624 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200625 class = object_get_class(OBJECT(dev));
626 do {
627 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
628 class = object_class_get_parent(class);
629 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200630 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600631 QLIST_FOREACH(child, &dev->child_bus, sibling) {
632 qbus_print(mon, child, indent);
633 }
634}
635
636static void qbus_print(Monitor *mon, BusState *bus, int indent)
637{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600638 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600639
640 qdev_printf("bus: %s\n", bus->name);
641 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200642 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600643 QTAILQ_FOREACH(kid, &bus->children, sibling) {
644 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600645 qdev_print(mon, dev, indent);
646 }
647}
648#undef qdev_printf
649
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800650void do_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600651{
652 if (sysbus_get_default())
653 qbus_print(mon, sysbus_get_default(), 0);
654}
655
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800656void do_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600657{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200658 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600659}
660
661int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
662{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300663 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600664 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100665 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600666
Luiz Capitulino4e899782012-04-18 17:24:01 -0300667 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100668 if (local_err) {
Luiz Capitulino4e899782012-04-18 17:24:01 -0300669 qerror_report_err(local_err);
670 error_free(local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600671 return -1;
672 }
673 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
674 qemu_opts_del(opts);
675 return 0;
676 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100677 dev = qdev_device_add(opts);
678 if (!dev) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600679 qemu_opts_del(opts);
680 return -1;
681 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100682 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600683 return 0;
684}
685
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300686void qmp_device_del(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600687{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600688 DeviceState *dev;
689
690 dev = qdev_find_recursive(sysbus_get_default(), id);
Gonglei8108fd32014-08-11 21:00:55 +0800691 if (!dev) {
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300692 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
693 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600694 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300695
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300696 qdev_unplug(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600697}
698
699void qdev_machine_init(void)
700{
701 qdev_get_peripheral_anon();
702 qdev_get_peripheral();
703}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100704
705QemuOptsList qemu_device_opts = {
706 .name = "device",
707 .implied_opt_name = "driver",
708 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
709 .desc = {
710 /*
711 * no elements => accept any
712 * sanity checking will happen later
713 * when setting device properties
714 */
715 { /* end of list */ }
716 },
717};
718
719QemuOptsList qemu_global_opts = {
720 .name = "global",
721 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
722 .desc = {
723 {
724 .name = "driver",
725 .type = QEMU_OPT_STRING,
726 },{
727 .name = "property",
728 .type = QEMU_OPT_STRING,
729 },{
730 .name = "value",
731 .type = QEMU_OPT_STRING,
732 },
733 { /* end of list */ }
734 },
735};
736
737int qemu_global_option(const char *str)
738{
739 char driver[64], property[64];
740 QemuOpts *opts;
741 int rc, offset;
742
743 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
744 if (rc < 2 || str[offset] != '=') {
745 error_report("can't parse: \"%s\"", str);
746 return -1;
747 }
748
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800749 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
Paolo Bonzini4d454572012-11-26 16:03:42 +0100750 qemu_opt_set(opts, "driver", driver);
751 qemu_opt_set(opts, "property", property);
752 qemu_opt_set(opts, "value", str+offset+1);
753 return 0;
754}