blob: c72145127e673927a1735833e5507be18cfe1be8 [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);
Igor Mammedovce9835e2014-09-26 09:28:18 +0000490 if (dc->cannot_instantiate_with_device_add_yet ||
491 (qdev_hotplug && !dc->hotpluggable)) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100492 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver",
493 "pluggable device type");
494 return NULL;
495 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600496
497 /* find bus */
498 path = qemu_opt_get(opts, "bus");
499 if (path != NULL) {
500 bus = qbus_find(path);
501 if (!bus) {
502 return NULL;
503 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200504 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400505 qerror_report(ERROR_CLASS_GENERIC_ERROR,
506 "Device '%s' can't go on a %s bus",
Anthony Liguori0d936922012-05-02 09:00:20 +0200507 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600508 return NULL;
509 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200510 } else if (dc->bus_type != NULL) {
511 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600512 if (!bus) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400513 qerror_report(ERROR_CLASS_GENERIC_ERROR,
514 "No '%s' bus found for device '%s'",
Andreas Färberf4d85792013-08-24 01:21:22 +0200515 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600516 return NULL;
517 }
518 }
Igor Mammedov39b888b2014-09-26 09:28:17 +0000519 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600520 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
521 return NULL;
522 }
523
Amos Kong7b030942014-03-03 15:57:55 +0800524 /* create device */
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200525 dev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200526
527 if (bus) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200528 qdev_set_parent_bus(dev, bus);
Andreas Färber2f7bd822013-04-16 03:50:21 +0200529 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600530
531 id = qemu_opts_id(opts);
532 if (id) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200533 dev->id = id;
Anthony Liguorib2d4b3f2012-02-12 11:36:24 -0600534 }
Amos Kong7b030942014-03-03 15:57:55 +0800535
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200536 if (dev->id) {
537 object_property_add_child(qdev_get_peripheral(), dev->id,
538 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600539 } else {
540 static int anon_count;
541 gchar *name = g_strdup_printf("device[%d]", anon_count++);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600542 object_property_add_child(qdev_get_peripheral_anon(), name,
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200543 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600544 g_free(name);
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200545 }
Bandan Das52aa17c2014-02-26 10:32:40 -0700546
Amos Kong7b030942014-03-03 15:57:55 +0800547 /* set properties */
548 if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) {
549 object_unparent(OBJECT(dev));
550 object_unref(OBJECT(dev));
551 return NULL;
552 }
553
Bandan Das52aa17c2014-02-26 10:32:40 -0700554 dev->opts = opts;
Andreas Färber852e2c52013-10-07 16:42:34 +0200555 object_property_set_bool(OBJECT(dev), true, "realized", &err);
556 if (err != NULL) {
557 qerror_report_err(err);
558 error_free(err);
Bandan Das52aa17c2014-02-26 10:32:40 -0700559 dev->opts = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200560 object_unparent(OBJECT(dev));
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200561 object_unref(OBJECT(dev));
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200562 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
563 return NULL;
564 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200565 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600566}
567
568
569#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
570static void qbus_print(Monitor *mon, BusState *bus, int indent);
571
572static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200573 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600574{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600575 if (!props)
576 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100577 for (; props->name; props++) {
578 Error *err = NULL;
579 char *value;
580 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
581 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
582 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
583 } else {
Paolo Bonzinidae3bda2014-02-08 11:01:51 +0100584 value = object_property_print(OBJECT(dev), props->name, true, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600585 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100586 g_free(legacy_name);
587
588 if (err) {
589 error_free(err);
590 continue;
591 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200592 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100593 value && *value ? value : "<null>");
594 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600595 }
596}
597
Anthony Liguori0d936922012-05-02 09:00:20 +0200598static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
599{
600 BusClass *bc = BUS_GET_CLASS(bus);
601
602 if (bc->print_dev) {
603 bc->print_dev(mon, dev, indent);
604 }
605}
606
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600607static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
608{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200609 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600610 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700611 NamedGPIOList *ngl;
612
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600613 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
614 dev->id ? dev->id : "");
615 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700616 QLIST_FOREACH(ngl, &dev->gpios, node) {
617 if (ngl->num_in) {
618 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
619 ngl->num_in);
620 }
621 if (ngl->num_out) {
622 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
623 ngl->num_out);
624 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600625 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200626 class = object_get_class(OBJECT(dev));
627 do {
628 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
629 class = object_class_get_parent(class);
630 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200631 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600632 QLIST_FOREACH(child, &dev->child_bus, sibling) {
633 qbus_print(mon, child, indent);
634 }
635}
636
637static void qbus_print(Monitor *mon, BusState *bus, int indent)
638{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600639 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600640
641 qdev_printf("bus: %s\n", bus->name);
642 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200643 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600644 QTAILQ_FOREACH(kid, &bus->children, sibling) {
645 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600646 qdev_print(mon, dev, indent);
647 }
648}
649#undef qdev_printf
650
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800651void do_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600652{
653 if (sysbus_get_default())
654 qbus_print(mon, sysbus_get_default(), 0);
655}
656
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800657void do_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600658{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200659 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600660}
661
662int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
663{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300664 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600665 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100666 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600667
Luiz Capitulino4e899782012-04-18 17:24:01 -0300668 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100669 if (local_err) {
Luiz Capitulino4e899782012-04-18 17:24:01 -0300670 qerror_report_err(local_err);
671 error_free(local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600672 return -1;
673 }
674 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
675 qemu_opts_del(opts);
676 return 0;
677 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100678 dev = qdev_device_add(opts);
679 if (!dev) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600680 qemu_opts_del(opts);
681 return -1;
682 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100683 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600684 return 0;
685}
686
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300687void qmp_device_del(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600688{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600689 DeviceState *dev;
690
691 dev = qdev_find_recursive(sysbus_get_default(), id);
Gonglei8108fd32014-08-11 21:00:55 +0800692 if (!dev) {
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300693 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
694 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600695 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300696
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300697 qdev_unplug(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600698}
699
700void qdev_machine_init(void)
701{
702 qdev_get_peripheral_anon();
703 qdev_get_peripheral();
704}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100705
706QemuOptsList qemu_device_opts = {
707 .name = "device",
708 .implied_opt_name = "driver",
709 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
710 .desc = {
711 /*
712 * no elements => accept any
713 * sanity checking will happen later
714 * when setting device properties
715 */
716 { /* end of list */ }
717 },
718};
719
720QemuOptsList qemu_global_opts = {
721 .name = "global",
722 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
723 .desc = {
724 {
725 .name = "driver",
726 .type = QEMU_OPT_STRING,
727 },{
728 .name = "property",
729 .type = QEMU_OPT_STRING,
730 },{
731 .name = "value",
732 .type = QEMU_OPT_STRING,
733 },
734 { /* end of list */ }
735 },
736};
737
738int qemu_global_option(const char *str)
739{
740 char driver[64], property[64];
741 QemuOpts *opts;
742 int rc, offset;
743
744 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
745 if (rc < 2 || str[offset] != '=') {
746 error_report("can't parse: \"%s\"", str);
747 return -1;
748 }
749
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800750 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
Paolo Bonzini4d454572012-11-26 16:03:42 +0100751 qemu_opt_set(opts, "driver", driver);
752 qemu_opt_set(opts, "property", property);
753 qemu_opt_set(opts, "value", str+offset+1);
754 return 0;
755}