blob: ffa08c670fac549af6e0471bf2198741ad09376a [file] [log] [blame]
Anthony Liguoriee46d8a2011-12-22 15:24:20 -06001/*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Andreas Färber2f7bd822013-04-16 03:50:21 +020021#include "hw/sysbus.h"
Markus Armbrusterd94bd092019-07-09 20:59:36 +020022#include "monitor/hmp.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010023#include "monitor/monitor.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010024#include "monitor/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/arch_init.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010026#include "qapi/error.h"
Markus Armbrusterc577ff62019-06-19 22:10:37 +020027#include "qapi/qapi-commands-qdev.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010028#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010029#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010031#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020032#include "qemu/help_option.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010033#include "qemu/option.h"
Markus Armbruster8acb2a72019-04-17 21:06:41 +020034#include "qemu/qemu-print.h"
Jens Freimannf3a85052019-10-29 12:48:55 +010035#include "qemu/option_int.h"
Kevin Wolf9680cae2016-09-20 13:38:42 +020036#include "sysemu/block-backend.h"
Markus Armbrusterd5938f22019-08-12 07:23:56 +020037#include "sysemu/sysemu.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020038#include "migration/misc.h"
Jens Freimannf3a85052019-10-29 12:48:55 +010039#include "migration/migration.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060040
41/*
42 * Aliases were a bad idea from the start. Let's keep them
43 * from spreading further.
44 */
45typedef struct QDevAlias
46{
47 const char *typename;
48 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020049 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060050} QDevAlias;
51
Sascha Silbe36e99162016-02-18 22:44:13 +010052/* Please keep this table sorted by typename. */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060053static const QDevAlias qdev_alias_table[] = {
Sascha Silbe36e99162016-02-18 22:44:13 +010054 { "e1000", "e1000-82540em" },
55 { "ich9-ahci", "ahci" },
Sascha Silbe36e99162016-02-18 22:44:13 +010056 { "lsi53c895a", "lsi" },
Sascha Silbe588c36c2016-02-18 22:44:14 +010057 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
58 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
59 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
Alexander Graf5f629d92012-05-18 02:36:26 +020060 { "virtio-balloon-pci", "virtio-balloon",
61 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020062 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010063 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010064 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
65 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
66 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
67 { "virtio-input-host-pci", "virtio-input-host",
68 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
69 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
70 { "virtio-keyboard-pci", "virtio-keyboard",
71 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
72 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
73 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020074 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010075 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010076 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
77 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
78 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
79 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020080 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010081 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010082 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
83 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060084 { }
85};
86
87static const char *qdev_class_get_alias(DeviceClass *dc)
88{
89 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
90 int i;
91
92 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +020093 if (qdev_alias_table[i].arch_mask &&
94 !(qdev_alias_table[i].arch_mask & arch_type)) {
95 continue;
96 }
97
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060098 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
99 return qdev_alias_table[i].alias;
100 }
101 }
102
103 return NULL;
104}
105
106static bool qdev_class_has_alias(DeviceClass *dc)
107{
108 return (qdev_class_get_alias(dc) != NULL);
109}
110
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200111static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600112{
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200113 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +0200114 if (dc->bus_type) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200115 qemu_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600116 }
117 if (qdev_class_has_alias(dc)) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200118 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600119 }
120 if (dc->desc) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200121 qemu_printf(", desc \"%s\"", dc->desc);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600122 }
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300123 if (!dc->user_creatable) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200124 qemu_printf(", no-user");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600125 }
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200126 qemu_printf("\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600127}
128
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200129static void qdev_print_devinfos(bool show_no_user)
130{
131 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
132 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
133 [DEVICE_CATEGORY_USB] = "USB",
134 [DEVICE_CATEGORY_STORAGE] = "Storage",
135 [DEVICE_CATEGORY_NETWORK] = "Network",
136 [DEVICE_CATEGORY_INPUT] = "Input",
137 [DEVICE_CATEGORY_DISPLAY] = "Display",
138 [DEVICE_CATEGORY_SOUND] = "Sound",
139 [DEVICE_CATEGORY_MISC] = "Misc",
Thomas Huthba31cc72017-01-20 14:01:16 +0100140 [DEVICE_CATEGORY_CPU] = "CPU",
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200141 [DEVICE_CATEGORY_MAX] = "Uncategorized",
142 };
143 GSList *list, *elt;
144 int i;
145 bool cat_printed;
146
Paolo Bonzini47c66002018-03-03 08:33:10 +0100147 list = object_class_get_list_sorted(TYPE_DEVICE, false);
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200148
149 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
150 cat_printed = false;
151 for (elt = list; elt; elt = elt->next) {
152 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
153 TYPE_DEVICE);
154 if ((i < DEVICE_CATEGORY_MAX
155 ? !test_bit(i, dc->categories)
156 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100157 || (!show_no_user
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300158 && !dc->user_creatable)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200159 continue;
160 }
161 if (!cat_printed) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200162 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200163 cat_printed = true;
164 }
165 qdev_print_devinfo(dc);
166 }
167 }
168
169 g_slist_free(list);
170}
171
Markus Armbruster71df1d82015-03-12 08:40:25 +0100172static int set_property(void *opaque, const char *name, const char *value,
173 Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600174{
Paolo Bonzini98a65282014-02-08 11:01:49 +0100175 Object *obj = opaque;
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200176 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600177
178 if (strcmp(name, "driver") == 0)
179 return 0;
180 if (strcmp(name, "bus") == 0)
181 return 0;
182
Paolo Bonzini98a65282014-02-08 11:01:49 +0100183 object_property_parse(obj, value, name, &err);
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200184 if (err != NULL) {
Markus Armbruster4caa4892015-03-12 13:58:02 +0100185 error_propagate(errp, err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600186 return -1;
187 }
188 return 0;
189}
190
191static const char *find_typename_by_alias(const char *alias)
192{
193 int i;
194
195 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200196 if (qdev_alias_table[i].arch_mask &&
197 !(qdev_alias_table[i].arch_mask & arch_type)) {
198 continue;
199 }
200
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600201 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
202 return qdev_alias_table[i].typename;
203 }
204 }
205
206 return NULL;
207}
208
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200209static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
210{
211 ObjectClass *oc;
212 DeviceClass *dc;
Sascha Silbef6b53192016-02-18 22:44:12 +0100213 const char *original_name = *driver;
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200214
215 oc = object_class_by_name(*driver);
216 if (!oc) {
217 const char *typename = find_typename_by_alias(*driver);
218
219 if (typename) {
220 *driver = typename;
221 oc = object_class_by_name(*driver);
222 }
223 }
224
225 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
Sascha Silbef6b53192016-02-18 22:44:12 +0100226 if (*driver != original_name) {
227 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
228 " name", original_name, *driver);
229 } else {
230 error_setg(errp, "'%s' is not a valid device model name", *driver);
231 }
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200232 return NULL;
233 }
234
235 if (object_class_is_abstract(oc)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100236 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
237 "non-abstract device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200238 return NULL;
239 }
240
241 dc = DEVICE_CLASS(oc);
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300242 if (!dc->user_creatable ||
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200243 (qdev_hotplug && !dc->hotpluggable)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100244 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
245 "pluggable device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200246 return NULL;
247 }
248
249 return dc;
250}
251
252
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600253int qdev_device_help(QemuOpts *opts)
254{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200255 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600256 const char *driver;
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100257 ObjectPropertyInfoList *prop_list;
258 ObjectPropertyInfoList *prop;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600259
260 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100261 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200262 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600263 return 1;
264 }
265
Peter Maydellc8057f92012-08-02 13:45:54 +0100266 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600267 return 0;
268 }
269
Markus Armbruster33fe9682015-10-01 10:59:59 +0200270 if (!object_class_by_name(driver)) {
271 const char *typename = find_typename_by_alias(driver);
272
273 if (typename) {
274 driver = typename;
275 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600276 }
277
Stefan Hajnoczief523582014-07-09 14:01:32 +0200278 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800279 if (local_err) {
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200280 goto error;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600281 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200282
Max Reitz9c2762b2018-10-19 18:49:27 +0200283 if (prop_list) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200284 qemu_printf("%s options:\n", driver);
Max Reitz9c2762b2018-10-19 18:49:27 +0200285 } else {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200286 qemu_printf("There are no options for %s.\n", driver);
Max Reitz9c2762b2018-10-19 18:49:27 +0200287 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200288 for (prop = prop_list; prop; prop = prop->next) {
Max Reitz9c2762b2018-10-19 18:49:27 +0200289 int len;
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200290 qemu_printf(" %s=<%s>%n", prop->value->name, prop->value->type, &len);
Gonglei07d09c52014-10-07 14:33:23 +0800291 if (prop->value->has_description) {
Max Reitz9c2762b2018-10-19 18:49:27 +0200292 if (len < 24) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200293 qemu_printf("%*s", 24 - len, "");
Max Reitz9c2762b2018-10-19 18:49:27 +0200294 }
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200295 qemu_printf(" - %s\n", prop->value->description);
Gonglei07d09c52014-10-07 14:33:23 +0800296 } else {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200297 qemu_printf("\n");
Gonglei07d09c52014-10-07 14:33:23 +0800298 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200299 }
300
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100301 qapi_free_ObjectPropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600302 return 1;
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200303
304error:
Markus Armbruster78288672015-12-18 16:35:07 +0100305 error_report_err(local_err);
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200306 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600307}
308
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600309static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600310{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600311 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600312
313 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200314 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600315 }
316
Anthony Liguori8b45d442011-12-23 09:08:05 -0600317 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600318}
319
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600320static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600321{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600322 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600323
324 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200325 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600326 }
327
Anthony Liguori8b45d442011-12-23 09:08:05 -0600328 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600329}
330
Eric Blake50b7b002015-09-10 10:19:16 -0600331static void qbus_list_bus(DeviceState *dev, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600332{
333 BusState *child;
334 const char *sep = " ";
335
Eric Blake50b7b002015-09-10 10:19:16 -0600336 error_append_hint(errp, "child buses at \"%s\":",
337 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600338 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Eric Blake50b7b002015-09-10 10:19:16 -0600339 error_append_hint(errp, "%s\"%s\"", sep, child->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600340 sep = ", ";
341 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100342 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600343}
344
Eric Blake50b7b002015-09-10 10:19:16 -0600345static void qbus_list_dev(BusState *bus, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600346{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600347 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600348 const char *sep = " ";
349
Eric Blake50b7b002015-09-10 10:19:16 -0600350 error_append_hint(errp, "devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600351 QTAILQ_FOREACH(kid, &bus->children, sibling) {
352 DeviceState *dev = kid->child;
Eric Blake50b7b002015-09-10 10:19:16 -0600353 error_append_hint(errp, "%s\"%s\"", sep,
354 object_get_typename(OBJECT(dev)));
355 if (dev->id) {
356 error_append_hint(errp, "/\"%s\"", dev->id);
357 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600358 sep = ", ";
359 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100360 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600361}
362
363static BusState *qbus_find_bus(DeviceState *dev, char *elem)
364{
365 BusState *child;
366
367 QLIST_FOREACH(child, &dev->child_bus, sibling) {
368 if (strcmp(child->name, elem) == 0) {
369 return child;
370 }
371 }
372 return NULL;
373}
374
375static DeviceState *qbus_find_dev(BusState *bus, char *elem)
376{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600377 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600378
379 /*
380 * try to match in order:
381 * (1) instance id, if present
382 * (2) driver name
383 * (3) driver alias, if present
384 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600385 QTAILQ_FOREACH(kid, &bus->children, sibling) {
386 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600387 if (dev->id && strcmp(dev->id, elem) == 0) {
388 return dev;
389 }
390 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600391 QTAILQ_FOREACH(kid, &bus->children, sibling) {
392 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600393 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
394 return dev;
395 }
396 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600397 QTAILQ_FOREACH(kid, &bus->children, sibling) {
398 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600399 DeviceClass *dc = DEVICE_GET_CLASS(dev);
400
401 if (qdev_class_has_alias(dc) &&
402 strcmp(qdev_class_get_alias(dc), elem) == 0) {
403 return dev;
404 }
405 }
406 return NULL;
407}
408
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100409static inline bool qbus_is_full(BusState *bus)
410{
411 BusClass *bus_class = BUS_GET_CLASS(bus);
Tony Krowiak12b2e9f2018-12-17 10:57:30 -0500412 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100413}
414
415/*
416 * Search the tree rooted at @bus for a bus.
417 * If @name, search for a bus with that name. Note that bus names
418 * need not be unique. Yes, that's screwed up.
419 * Else search for a bus that is a subtype of @bus_typename.
420 * If more than one exists, prefer one that can take another device.
421 * Return the bus if found, else %NULL.
422 */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600423static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200424 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600425{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600426 BusChild *kid;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100427 BusState *pick, *child, *ret;
428 bool match;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600429
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100430 assert(name || bus_typename);
431 if (name) {
432 match = !strcmp(bus->name, name);
433 } else {
434 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
KONRAD Frederic1395af62013-01-15 00:08:00 +0100435 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100436
437 if (match && !qbus_is_full(bus)) {
438 return bus; /* root matches and isn't full */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600439 }
440
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100441 pick = match ? bus : NULL;
442
Anthony Liguori0866aca2011-12-23 15:34:39 -0600443 QTAILQ_FOREACH(kid, &bus->children, sibling) {
444 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600445 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200446 ret = qbus_find_recursive(child, name, bus_typename);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100447 if (ret && !qbus_is_full(ret)) {
448 return ret; /* a descendant matches and isn't full */
449 }
450 if (ret && !pick) {
451 pick = ret;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600452 }
453 }
454 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100455
456 /* root or a descendant matches, but is full */
457 return pick;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600458}
459
Markus Armbrusterd2828422015-03-11 19:16:04 +0100460static BusState *qbus_find(const char *path, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600461{
462 DeviceState *dev;
463 BusState *bus;
464 char elem[128];
465 int pos, len;
466
467 /* find start element */
468 if (path[0] == '/') {
469 bus = sysbus_get_default();
470 pos = 0;
471 } else {
472 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
473 assert(!path[0]);
474 elem[0] = len = 0;
475 }
476 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
477 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100478 error_setg(errp, "Bus '%s' not found", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600479 return NULL;
480 }
481 pos = len;
482 }
483
484 for (;;) {
485 assert(path[pos] == '/' || !path[pos]);
486 while (path[pos] == '/') {
487 pos++;
488 }
489 if (path[pos] == '\0') {
Markus Armbrustered238ba2015-03-11 18:39:16 +0100490 break;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600491 }
492
493 /* find device */
494 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200495 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600496 elem[0] = len = 0;
497 }
498 pos += len;
499 dev = qbus_find_dev(bus, elem);
500 if (!dev) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100501 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
502 "Device '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600503 qbus_list_dev(bus, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600504 return NULL;
505 }
506
507 assert(path[pos] == '/' || !path[pos]);
508 while (path[pos] == '/') {
509 pos++;
510 }
511 if (path[pos] == '\0') {
512 /* last specified element is a device. If it has exactly
513 * one child bus accept it nevertheless */
Markus Armbrustered238ba2015-03-11 18:39:16 +0100514 if (dev->num_child_bus == 1) {
515 bus = QLIST_FIRST(&dev->child_bus);
516 break;
517 }
518 if (dev->num_child_bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100519 error_setg(errp, "Device '%s' has multiple child buses",
520 elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600521 qbus_list_bus(dev, errp);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100522 } else {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100523 error_setg(errp, "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600524 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100525 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600526 }
527
528 /* find bus */
529 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200530 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600531 elem[0] = len = 0;
532 }
533 pos += len;
534 bus = qbus_find_bus(dev, elem);
535 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100536 error_setg(errp, "Bus '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600537 qbus_list_bus(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600538 return NULL;
539 }
540 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100541
542 if (qbus_is_full(bus)) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100543 error_setg(errp, "Bus '%s' is full", path);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100544 return NULL;
545 }
546 return bus;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600547}
548
Juergen Grossce49b732016-11-22 07:10:57 +0100549void qdev_set_id(DeviceState *dev, const char *id)
550{
551 if (id) {
552 dev->id = id;
553 }
554
555 if (dev->id) {
556 object_property_add_child(qdev_get_peripheral(), dev->id,
557 OBJECT(dev), NULL);
558 } else {
559 static int anon_count;
560 gchar *name = g_strdup_printf("device[%d]", anon_count++);
561 object_property_add_child(qdev_get_peripheral_anon(), name,
562 OBJECT(dev), NULL);
563 g_free(name);
564 }
565}
566
Jens Freimannf3a85052019-10-29 12:48:55 +0100567static int is_failover_device(void *opaque, const char *name, const char *value,
568 Error **errp)
569{
570 if (strcmp(name, "failover_pair_id") == 0) {
571 QemuOpts *opts = (QemuOpts *)opaque;
572
573 if (qdev_should_hide_device(opts)) {
574 return 1;
575 }
576 }
577
578 return 0;
579}
580
581static bool should_hide_device(QemuOpts *opts)
582{
583 if (qemu_opt_foreach(opts, is_failover_device, opts, NULL) == 0) {
584 return false;
585 }
586 return true;
587}
588
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100589DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600590{
Andreas Färberf4d85792013-08-24 01:21:22 +0200591 DeviceClass *dc;
Juergen Grossce49b732016-11-22 07:10:57 +0100592 const char *driver, *path;
Jens Freimannf3a85052019-10-29 12:48:55 +0100593 DeviceState *dev = NULL;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200594 BusState *bus = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200595 Error *err = NULL;
Jens Freimannf3a85052019-10-29 12:48:55 +0100596 bool hide;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600597
598 driver = qemu_opt_get(opts, "driver");
599 if (!driver) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100600 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600601 return NULL;
602 }
603
604 /* find driver */
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100605 dc = qdev_get_device_class(&driver, errp);
606 if (!dc) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100607 return NULL;
608 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600609
610 /* find bus */
611 path = qemu_opt_get(opts, "bus");
612 if (path != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100613 bus = qbus_find(path, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600614 if (!bus) {
615 return NULL;
616 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200617 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100618 error_setg(errp, "Device '%s' can't go on %s bus",
619 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600620 return NULL;
621 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200622 } else if (dc->bus_type != NULL) {
623 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100624 if (!bus || qbus_is_full(bus)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100625 error_setg(errp, "No '%s' bus found for device '%s'",
626 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600627 return NULL;
628 }
629 }
Jens Freimannf3a85052019-10-29 12:48:55 +0100630 hide = should_hide_device(opts);
631
632 if ((hide || qdev_hotplug) && bus && !qbus_is_hotpluggable(bus)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100633 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600634 return NULL;
635 }
636
Jens Freimannf3a85052019-10-29 12:48:55 +0100637 if (hide) {
638 return NULL;
639 }
640
Juan Quintelab06424d2017-03-22 17:34:27 +0100641 if (!migration_is_idle()) {
642 error_setg(errp, "device_add not allowed while migrating");
643 return NULL;
644 }
645
Amos Kong7b030942014-03-03 15:57:55 +0800646 /* create device */
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200647 dev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200648
Peter Xud2321d32019-09-16 16:07:16 +0800649 /* Check whether the hotplug is allowed by the machine */
650 if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) {
651 /* Error must be set in the machine hook */
652 assert(err);
653 goto err_del_dev;
654 }
655
Andreas Färber2f7bd822013-04-16 03:50:21 +0200656 if (bus) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200657 qdev_set_parent_bus(dev, bus);
Thomas Huth03fcbd92017-11-02 11:10:06 +0100658 } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
659 /* No bus, no machine hotplug handler --> device is not hotpluggable */
660 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
661 driver);
662 goto err_del_dev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200663 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600664
Juergen Grossce49b732016-11-22 07:10:57 +0100665 qdev_set_id(dev, qemu_opts_id(opts));
Bandan Das52aa17c2014-02-26 10:32:40 -0700666
Amos Kong7b030942014-03-03 15:57:55 +0800667 /* set properties */
Markus Armbruster4caa4892015-03-12 13:58:02 +0100668 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
Thomas Huth58346212017-11-02 11:10:05 +0100669 goto err_del_dev;
Amos Kong7b030942014-03-03 15:57:55 +0800670 }
671
Bandan Das52aa17c2014-02-26 10:32:40 -0700672 dev->opts = opts;
Andreas Färber852e2c52013-10-07 16:42:34 +0200673 object_property_set_bool(OBJECT(dev), true, "realized", &err);
674 if (err != NULL) {
Bandan Das52aa17c2014-02-26 10:32:40 -0700675 dev->opts = NULL;
Thomas Huth58346212017-11-02 11:10:05 +0100676 goto err_del_dev;
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200677 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200678 return dev;
Thomas Huth58346212017-11-02 11:10:05 +0100679
680err_del_dev:
681 error_propagate(errp, err);
Jens Freimannf3a85052019-10-29 12:48:55 +0100682 if (dev) {
683 object_unparent(OBJECT(dev));
684 object_unref(OBJECT(dev));
685 }
Thomas Huth58346212017-11-02 11:10:05 +0100686 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600687}
688
689
690#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
691static void qbus_print(Monitor *mon, BusState *bus, int indent);
692
693static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200694 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600695{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600696 if (!props)
697 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100698 for (; props->name; props++) {
699 Error *err = NULL;
700 char *value;
701 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
702 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
703 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
704 } else {
Paolo Bonzinidae3bda2014-02-08 11:01:51 +0100705 value = object_property_print(OBJECT(dev), props->name, true, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600706 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100707 g_free(legacy_name);
708
709 if (err) {
710 error_free(err);
711 continue;
712 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200713 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100714 value && *value ? value : "<null>");
715 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600716 }
717}
718
Anthony Liguori0d936922012-05-02 09:00:20 +0200719static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
720{
721 BusClass *bc = BUS_GET_CLASS(bus);
722
723 if (bc->print_dev) {
724 bc->print_dev(mon, dev, indent);
725 }
726}
727
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600728static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
729{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200730 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600731 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700732 NamedGPIOList *ngl;
733
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600734 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
735 dev->id ? dev->id : "");
736 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700737 QLIST_FOREACH(ngl, &dev->gpios, node) {
738 if (ngl->num_in) {
739 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
740 ngl->num_in);
741 }
742 if (ngl->num_out) {
743 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
744 ngl->num_out);
745 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600746 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200747 class = object_get_class(OBJECT(dev));
748 do {
749 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
750 class = object_class_get_parent(class);
751 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200752 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600753 QLIST_FOREACH(child, &dev->child_bus, sibling) {
754 qbus_print(mon, child, indent);
755 }
756}
757
758static void qbus_print(Monitor *mon, BusState *bus, int indent)
759{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600760 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600761
762 qdev_printf("bus: %s\n", bus->name);
763 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200764 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600765 QTAILQ_FOREACH(kid, &bus->children, sibling) {
766 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600767 qdev_print(mon, dev, indent);
768 }
769}
770#undef qdev_printf
771
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100772void hmp_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600773{
774 if (sysbus_get_default())
775 qbus_print(mon, sysbus_get_default(), 0);
776}
777
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100778void hmp_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600779{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200780 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600781}
782
Markus Armbruster485febc2015-03-13 17:25:50 +0100783void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600784{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300785 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600786 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100787 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600788
Luiz Capitulino4e899782012-04-18 17:24:01 -0300789 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100790 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100791 error_propagate(errp, local_err);
792 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600793 }
794 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
795 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100796 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600797 }
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100798 dev = qdev_device_add(opts, &local_err);
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100799 if (!dev) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100800 error_propagate(errp, local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600801 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100802 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600803 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100804 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600805}
806
Kevin Wolf6c1db522016-09-20 13:38:41 +0200807static DeviceState *find_device_state(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600808{
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000809 Object *obj;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600810
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100811 if (id[0] == '/') {
812 obj = object_resolve_path(id, NULL);
813 } else {
814 char *root_path = object_get_canonical_path(qdev_get_peripheral());
815 char *path = g_strdup_printf("%s/%s", root_path, id);
816
817 g_free(root_path);
818 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
819 g_free(path);
820 }
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000821
822 if (!obj) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100823 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
824 "Device '%s' not found", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200825 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600826 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300827
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100828 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
829 error_setg(errp, "%s is not a hotpluggable device", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200830 return NULL;
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100831 }
832
Kevin Wolf6c1db522016-09-20 13:38:41 +0200833 return DEVICE(obj);
834}
835
Juan Quintela32900672017-03-28 11:22:51 +0200836void qdev_unplug(DeviceState *dev, Error **errp)
837{
838 DeviceClass *dc = DEVICE_GET_CLASS(dev);
839 HotplugHandler *hotplug_ctrl;
840 HotplugHandlerClass *hdc;
David Hildenbrand07578b02019-02-28 13:28:47 +0100841 Error *local_err = NULL;
Juan Quintela32900672017-03-28 11:22:51 +0200842
843 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
844 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
845 return;
846 }
847
848 if (!dc->hotpluggable) {
849 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
850 object_get_typename(OBJECT(dev)));
851 return;
852 }
853
Juan Quintelab06424d2017-03-22 17:34:27 +0100854 if (!migration_is_idle()) {
855 error_setg(errp, "device_del not allowed while migrating");
856 return;
857 }
858
Juan Quintela32900672017-03-28 11:22:51 +0200859 qdev_hot_removed = true;
860
861 hotplug_ctrl = qdev_get_hotplug_handler(dev);
862 /* hotpluggable device MUST have HotplugHandler, if it doesn't
863 * then something is very wrong with it */
864 g_assert(hotplug_ctrl);
865
866 /* If device supports async unplug just request it to be done,
867 * otherwise just remove it synchronously */
868 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
869 if (hdc->unplug_request) {
David Hildenbrand07578b02019-02-28 13:28:47 +0100870 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
Juan Quintela32900672017-03-28 11:22:51 +0200871 } else {
David Hildenbrand07578b02019-02-28 13:28:47 +0100872 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
873 if (!local_err) {
874 object_unparent(OBJECT(dev));
875 }
Juan Quintela32900672017-03-28 11:22:51 +0200876 }
David Hildenbrand07578b02019-02-28 13:28:47 +0100877 error_propagate(errp, local_err);
Juan Quintela32900672017-03-28 11:22:51 +0200878}
879
Kevin Wolf6c1db522016-09-20 13:38:41 +0200880void qmp_device_del(const char *id, Error **errp)
881{
882 DeviceState *dev = find_device_state(id, errp);
883 if (dev != NULL) {
884 qdev_unplug(dev, errp);
885 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600886}
887
Markus Armbrusterd94bd092019-07-09 20:59:36 +0200888void hmp_device_add(Monitor *mon, const QDict *qdict)
889{
890 Error *err = NULL;
891
892 qmp_device_add((QDict *)qdict, NULL, &err);
893 hmp_handle_error(mon, &err);
894}
895
896void hmp_device_del(Monitor *mon, const QDict *qdict)
897{
898 const char *id = qdict_get_str(qdict, "id");
899 Error *err = NULL;
900
901 qmp_device_del(id, &err);
902 hmp_handle_error(mon, &err);
903}
904
Kevin Wolf9680cae2016-09-20 13:38:42 +0200905BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
906{
907 DeviceState *dev;
908 BlockBackend *blk;
909
910 dev = find_device_state(id, errp);
911 if (dev == NULL) {
912 return NULL;
913 }
914
915 blk = blk_by_dev(dev);
916 if (!blk) {
917 error_setg(errp, "Device does not have a block device backend");
918 }
919 return blk;
920}
921
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600922void qdev_machine_init(void)
923{
924 qdev_get_peripheral_anon();
925 qdev_get_peripheral();
926}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100927
928QemuOptsList qemu_device_opts = {
929 .name = "device",
930 .implied_opt_name = "driver",
931 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
932 .desc = {
933 /*
934 * no elements => accept any
935 * sanity checking will happen later
936 * when setting device properties
937 */
938 { /* end of list */ }
939 },
940};
941
942QemuOptsList qemu_global_opts = {
943 .name = "global",
944 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
945 .desc = {
946 {
947 .name = "driver",
948 .type = QEMU_OPT_STRING,
949 },{
950 .name = "property",
951 .type = QEMU_OPT_STRING,
952 },{
953 .name = "value",
954 .type = QEMU_OPT_STRING,
955 },
956 { /* end of list */ }
957 },
958};
959
960int qemu_global_option(const char *str)
961{
962 char driver[64], property[64];
963 QemuOpts *opts;
964 int rc, offset;
965
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200966 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
967 if (rc == 2 && str[offset] == '=') {
968 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
969 qemu_opt_set(opts, "driver", driver, &error_abort);
970 qemu_opt_set(opts, "property", property, &error_abort);
971 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
972 return 0;
973 }
974
Markus Armbruster70b94332015-02-13 12:50:26 +0100975 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200976 if (!opts) {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100977 return -1;
978 }
979
Paolo Bonzini4d454572012-11-26 16:03:42 +0100980 return 0;
981}