Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 1 | /* |
| 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 Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
Andreas Färber | 2f7bd82 | 2013-04-16 03:50:21 +0200 | [diff] [blame] | 21 | #include "hw/sysbus.h" |
Markus Armbruster | d94bd09 | 2019-07-09 20:59:36 +0200 | [diff] [blame] | 22 | #include "monitor/hmp.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 23 | #include "monitor/monitor.h" |
Paolo Bonzini | b4a42f8 | 2013-02-04 11:37:52 +0100 | [diff] [blame] | 24 | #include "monitor/qdev.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 25 | #include "sysemu/arch_init.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Markus Armbruster | c577ff6 | 2019-06-19 22:10:37 +0200 | [diff] [blame] | 27 | #include "qapi/qapi-commands-qdev.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 28 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 29 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 30 | #include "qemu/config-file.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 31 | #include "qemu/error-report.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 32 | #include "qemu/help_option.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 33 | #include "qemu/option.h" |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 34 | #include "qemu/qemu-print.h" |
Jens Freimann | f3a8505 | 2019-10-29 12:48:55 +0100 | [diff] [blame] | 35 | #include "qemu/option_int.h" |
Kevin Wolf | 9680cae | 2016-09-20 13:38:42 +0200 | [diff] [blame] | 36 | #include "sysemu/block-backend.h" |
Markus Armbruster | d5938f2 | 2019-08-12 07:23:56 +0200 | [diff] [blame] | 37 | #include "sysemu/sysemu.h" |
Juan Quintela | c4b63b7 | 2017-04-24 19:02:44 +0200 | [diff] [blame] | 38 | #include "migration/misc.h" |
Jens Freimann | f3a8505 | 2019-10-29 12:48:55 +0100 | [diff] [blame] | 39 | #include "migration/migration.h" |
Marc-André Lureau | e1043d6 | 2020-01-10 19:30:39 +0400 | [diff] [blame] | 40 | #include "qemu/cutils.h" |
Damien Hedde | 9f2ff99 | 2020-04-06 15:52:51 +0200 | [diff] [blame] | 41 | #include "hw/clock.h" |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Aliases were a bad idea from the start. Let's keep them |
| 45 | * from spreading further. |
| 46 | */ |
| 47 | typedef struct QDevAlias |
| 48 | { |
| 49 | const char *typename; |
| 50 | const char *alias; |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 51 | uint32_t arch_mask; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 52 | } QDevAlias; |
| 53 | |
Sascha Silbe | 36e9916 | 2016-02-18 22:44:13 +0100 | [diff] [blame] | 54 | /* Please keep this table sorted by typename. */ |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 55 | static const QDevAlias qdev_alias_table[] = { |
Gerd Hoffmann | 2957f5a | 2020-07-02 15:25:09 +0200 | [diff] [blame] | 56 | { "AC97", "ac97" }, /* -soundhw name */ |
Sascha Silbe | 36e9916 | 2016-02-18 22:44:13 +0100 | [diff] [blame] | 57 | { "e1000", "e1000-82540em" }, |
Gerd Hoffmann | 0e933fe | 2020-07-02 15:25:10 +0200 | [diff] [blame] | 58 | { "ES1370", "es1370" }, /* -soundhw name */ |
Sascha Silbe | 36e9916 | 2016-02-18 22:44:13 +0100 | [diff] [blame] | 59 | { "ich9-ahci", "ahci" }, |
Sascha Silbe | 36e9916 | 2016-02-18 22:44:13 +0100 | [diff] [blame] | 60 | { "lsi53c895a", "lsi" }, |
Sascha Silbe | 588c36c | 2016-02-18 22:44:14 +0100 | [diff] [blame] | 61 | { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X }, |
| 62 | { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
| 63 | { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X }, |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 64 | { "virtio-balloon-pci", "virtio-balloon", |
| 65 | QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Alexander Graf | 1f68f1d | 2015-06-16 23:06:33 +0200 | [diff] [blame] | 66 | { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X }, |
Sascha Silbe | 36e9916 | 2016-02-18 22:44:13 +0100 | [diff] [blame] | 67 | { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Sascha Silbe | 588c36c | 2016-02-18 22:44:14 +0100 | [diff] [blame] | 68 | { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X }, |
| 69 | { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
| 70 | { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X }, |
| 71 | { "virtio-input-host-pci", "virtio-input-host", |
| 72 | QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Eric Auger | 8b4eb09 | 2020-02-14 14:27:43 +0100 | [diff] [blame] | 73 | { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Sascha Silbe | 588c36c | 2016-02-18 22:44:14 +0100 | [diff] [blame] | 74 | { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X }, |
| 75 | { "virtio-keyboard-pci", "virtio-keyboard", |
| 76 | QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
| 77 | { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X }, |
| 78 | { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Alexander Graf | 1f68f1d | 2015-06-16 23:06:33 +0200 | [diff] [blame] | 79 | { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X }, |
Sascha Silbe | 36e9916 | 2016-02-18 22:44:13 +0100 | [diff] [blame] | 80 | { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Sascha Silbe | 588c36c | 2016-02-18 22:44:14 +0100 | [diff] [blame] | 81 | { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X }, |
| 82 | { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
| 83 | { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X }, |
| 84 | { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Alexander Graf | 1f68f1d | 2015-06-16 23:06:33 +0200 | [diff] [blame] | 85 | { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X }, |
Sascha Silbe | 36e9916 | 2016-02-18 22:44:13 +0100 | [diff] [blame] | 86 | { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Sascha Silbe | 588c36c | 2016-02-18 22:44:14 +0100 | [diff] [blame] | 87 | { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X }, |
| 88 | { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 89 | { } |
| 90 | }; |
| 91 | |
| 92 | static const char *qdev_class_get_alias(DeviceClass *dc) |
| 93 | { |
| 94 | const char *typename = object_class_get_name(OBJECT_CLASS(dc)); |
| 95 | int i; |
| 96 | |
| 97 | for (i = 0; qdev_alias_table[i].typename; i++) { |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 98 | if (qdev_alias_table[i].arch_mask && |
| 99 | !(qdev_alias_table[i].arch_mask & arch_type)) { |
| 100 | continue; |
| 101 | } |
| 102 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 103 | if (strcmp(qdev_alias_table[i].typename, typename) == 0) { |
| 104 | return qdev_alias_table[i].alias; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | static bool qdev_class_has_alias(DeviceClass *dc) |
| 112 | { |
| 113 | return (qdev_class_get_alias(dc) != NULL); |
| 114 | } |
| 115 | |
Markus Armbruster | a3400ae | 2013-10-10 15:00:21 +0200 | [diff] [blame] | 116 | static void qdev_print_devinfo(DeviceClass *dc) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 117 | { |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 118 | qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc))); |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 119 | if (dc->bus_type) { |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 120 | qemu_printf(", bus %s", dc->bus_type); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 121 | } |
| 122 | if (qdev_class_has_alias(dc)) { |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 123 | qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc)); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 124 | } |
| 125 | if (dc->desc) { |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 126 | qemu_printf(", desc \"%s\"", dc->desc); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 127 | } |
Eduardo Habkost | e90f2a8 | 2017-05-03 17:35:44 -0300 | [diff] [blame] | 128 | if (!dc->user_creatable) { |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 129 | qemu_printf(", no-user"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 130 | } |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 131 | qemu_printf("\n"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 132 | } |
| 133 | |
Markus Armbruster | a3400ae | 2013-10-10 15:00:21 +0200 | [diff] [blame] | 134 | static void qdev_print_devinfos(bool show_no_user) |
| 135 | { |
| 136 | static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = { |
| 137 | [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub", |
| 138 | [DEVICE_CATEGORY_USB] = "USB", |
| 139 | [DEVICE_CATEGORY_STORAGE] = "Storage", |
| 140 | [DEVICE_CATEGORY_NETWORK] = "Network", |
| 141 | [DEVICE_CATEGORY_INPUT] = "Input", |
| 142 | [DEVICE_CATEGORY_DISPLAY] = "Display", |
| 143 | [DEVICE_CATEGORY_SOUND] = "Sound", |
| 144 | [DEVICE_CATEGORY_MISC] = "Misc", |
Thomas Huth | ba31cc7 | 2017-01-20 14:01:16 +0100 | [diff] [blame] | 145 | [DEVICE_CATEGORY_CPU] = "CPU", |
Markus Armbruster | a3400ae | 2013-10-10 15:00:21 +0200 | [diff] [blame] | 146 | [DEVICE_CATEGORY_MAX] = "Uncategorized", |
| 147 | }; |
| 148 | GSList *list, *elt; |
| 149 | int i; |
| 150 | bool cat_printed; |
| 151 | |
Gerd Hoffmann | 7ab6e7f | 2020-06-24 15:10:38 +0200 | [diff] [blame] | 152 | module_load_qom_all(); |
Paolo Bonzini | 47c6600 | 2018-03-03 08:33:10 +0100 | [diff] [blame] | 153 | list = object_class_get_list_sorted(TYPE_DEVICE, false); |
Markus Armbruster | a3400ae | 2013-10-10 15:00:21 +0200 | [diff] [blame] | 154 | |
| 155 | for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) { |
| 156 | cat_printed = false; |
| 157 | for (elt = list; elt; elt = elt->next) { |
| 158 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, |
| 159 | TYPE_DEVICE); |
| 160 | if ((i < DEVICE_CATEGORY_MAX |
| 161 | ? !test_bit(i, dc->categories) |
| 162 | : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX)) |
Markus Armbruster | efec3dd | 2013-11-28 17:26:54 +0100 | [diff] [blame] | 163 | || (!show_no_user |
Eduardo Habkost | e90f2a8 | 2017-05-03 17:35:44 -0300 | [diff] [blame] | 164 | && !dc->user_creatable)) { |
Markus Armbruster | a3400ae | 2013-10-10 15:00:21 +0200 | [diff] [blame] | 165 | continue; |
| 166 | } |
| 167 | if (!cat_printed) { |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 168 | qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]); |
Markus Armbruster | a3400ae | 2013-10-10 15:00:21 +0200 | [diff] [blame] | 169 | cat_printed = true; |
| 170 | } |
| 171 | qdev_print_devinfo(dc); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | g_slist_free(list); |
| 176 | } |
| 177 | |
Markus Armbruster | 71df1d8 | 2015-03-12 08:40:25 +0100 | [diff] [blame] | 178 | static int set_property(void *opaque, const char *name, const char *value, |
| 179 | Error **errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 180 | { |
Paolo Bonzini | 98a6528 | 2014-02-08 11:01:49 +0100 | [diff] [blame] | 181 | Object *obj = opaque; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 182 | |
| 183 | if (strcmp(name, "driver") == 0) |
| 184 | return 0; |
| 185 | if (strcmp(name, "bus") == 0) |
| 186 | return 0; |
| 187 | |
Markus Armbruster | 668f62e | 2020-07-07 18:06:02 +0200 | [diff] [blame] | 188 | if (!object_property_parse(obj, name, value, errp)) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 189 | return -1; |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static const char *find_typename_by_alias(const char *alias) |
| 195 | { |
| 196 | int i; |
| 197 | |
| 198 | for (i = 0; qdev_alias_table[i].alias; i++) { |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 199 | if (qdev_alias_table[i].arch_mask && |
| 200 | !(qdev_alias_table[i].arch_mask & arch_type)) { |
| 201 | continue; |
| 202 | } |
| 203 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 204 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { |
| 205 | return qdev_alias_table[i].typename; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return NULL; |
| 210 | } |
| 211 | |
Eduardo Habkost | 43c95d7 | 2014-11-01 13:56:09 -0200 | [diff] [blame] | 212 | static DeviceClass *qdev_get_device_class(const char **driver, Error **errp) |
| 213 | { |
| 214 | ObjectClass *oc; |
| 215 | DeviceClass *dc; |
Sascha Silbe | f6b5319 | 2016-02-18 22:44:12 +0100 | [diff] [blame] | 216 | const char *original_name = *driver; |
Eduardo Habkost | 43c95d7 | 2014-11-01 13:56:09 -0200 | [diff] [blame] | 217 | |
Gerd Hoffmann | 7ab6e7f | 2020-06-24 15:10:38 +0200 | [diff] [blame] | 218 | oc = module_object_class_by_name(*driver); |
Eduardo Habkost | 43c95d7 | 2014-11-01 13:56:09 -0200 | [diff] [blame] | 219 | if (!oc) { |
| 220 | const char *typename = find_typename_by_alias(*driver); |
| 221 | |
| 222 | if (typename) { |
| 223 | *driver = typename; |
Gerd Hoffmann | 7ab6e7f | 2020-06-24 15:10:38 +0200 | [diff] [blame] | 224 | oc = module_object_class_by_name(*driver); |
Eduardo Habkost | 43c95d7 | 2014-11-01 13:56:09 -0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) { |
Sascha Silbe | f6b5319 | 2016-02-18 22:44:12 +0100 | [diff] [blame] | 229 | if (*driver != original_name) { |
| 230 | error_setg(errp, "'%s' (alias '%s') is not a valid device model" |
| 231 | " name", original_name, *driver); |
| 232 | } else { |
| 233 | error_setg(errp, "'%s' is not a valid device model name", *driver); |
| 234 | } |
Eduardo Habkost | 43c95d7 | 2014-11-01 13:56:09 -0200 | [diff] [blame] | 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | if (object_class_is_abstract(oc)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 239 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", |
| 240 | "non-abstract device type"); |
Eduardo Habkost | 43c95d7 | 2014-11-01 13:56:09 -0200 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | dc = DEVICE_CLASS(oc); |
Eduardo Habkost | e90f2a8 | 2017-05-03 17:35:44 -0300 | [diff] [blame] | 245 | if (!dc->user_creatable || |
Eduardo Habkost | 43c95d7 | 2014-11-01 13:56:09 -0200 | [diff] [blame] | 246 | (qdev_hotplug && !dc->hotpluggable)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 247 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", |
| 248 | "pluggable device type"); |
Eduardo Habkost | 43c95d7 | 2014-11-01 13:56:09 -0200 | [diff] [blame] | 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | return dc; |
| 253 | } |
| 254 | |
| 255 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 256 | int qdev_device_help(QemuOpts *opts) |
| 257 | { |
Stefan Hajnoczi | ef52358 | 2014-07-09 14:01:32 +0200 | [diff] [blame] | 258 | Error *local_err = NULL; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 259 | const char *driver; |
Alexey Kardashevskiy | 35f6376 | 2018-03-02 00:09:38 +1100 | [diff] [blame] | 260 | ObjectPropertyInfoList *prop_list; |
| 261 | ObjectPropertyInfoList *prop; |
Marc-André Lureau | e1043d6 | 2020-01-10 19:30:39 +0400 | [diff] [blame] | 262 | GPtrArray *array; |
| 263 | int i; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 264 | |
| 265 | driver = qemu_opt_get(opts, "driver"); |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 266 | if (driver && is_help_option(driver)) { |
Markus Armbruster | a3400ae | 2013-10-10 15:00:21 +0200 | [diff] [blame] | 267 | qdev_print_devinfos(false); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 271 | if (!driver || !qemu_opt_has_help_opt(opts)) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
Markus Armbruster | 33fe968 | 2015-10-01 10:59:59 +0200 | [diff] [blame] | 275 | if (!object_class_by_name(driver)) { |
| 276 | const char *typename = find_typename_by_alias(driver); |
| 277 | |
| 278 | if (typename) { |
| 279 | driver = typename; |
| 280 | } |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 281 | } |
| 282 | |
Stefan Hajnoczi | ef52358 | 2014-07-09 14:01:32 +0200 | [diff] [blame] | 283 | prop_list = qmp_device_list_properties(driver, &local_err); |
Gonglei | 0722eba | 2014-09-16 10:19:33 +0800 | [diff] [blame] | 284 | if (local_err) { |
Eduardo Habkost | 5185f0e | 2014-11-01 13:56:10 -0200 | [diff] [blame] | 285 | goto error; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 286 | } |
Stefan Hajnoczi | ef52358 | 2014-07-09 14:01:32 +0200 | [diff] [blame] | 287 | |
Max Reitz | 9c2762b | 2018-10-19 18:49:27 +0200 | [diff] [blame] | 288 | if (prop_list) { |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 289 | qemu_printf("%s options:\n", driver); |
Max Reitz | 9c2762b | 2018-10-19 18:49:27 +0200 | [diff] [blame] | 290 | } else { |
Markus Armbruster | 8acb2a7 | 2019-04-17 21:06:41 +0200 | [diff] [blame] | 291 | qemu_printf("There are no options for %s.\n", driver); |
Max Reitz | 9c2762b | 2018-10-19 18:49:27 +0200 | [diff] [blame] | 292 | } |
Marc-André Lureau | e1043d6 | 2020-01-10 19:30:39 +0400 | [diff] [blame] | 293 | array = g_ptr_array_new(); |
Stefan Hajnoczi | ef52358 | 2014-07-09 14:01:32 +0200 | [diff] [blame] | 294 | for (prop = prop_list; prop; prop = prop->next) { |
Marc-André Lureau | e1043d6 | 2020-01-10 19:30:39 +0400 | [diff] [blame] | 295 | g_ptr_array_add(array, |
| 296 | object_property_help(prop->value->name, |
| 297 | prop->value->type, |
| 298 | prop->value->default_value, |
| 299 | prop->value->description)); |
Stefan Hajnoczi | ef52358 | 2014-07-09 14:01:32 +0200 | [diff] [blame] | 300 | } |
Marc-André Lureau | e1043d6 | 2020-01-10 19:30:39 +0400 | [diff] [blame] | 301 | g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0); |
| 302 | for (i = 0; i < array->len; i++) { |
Markus Armbruster | 029afc4 | 2020-07-14 18:01:58 +0200 | [diff] [blame] | 303 | qemu_printf("%s\n", (char *)array->pdata[i]); |
Marc-André Lureau | e1043d6 | 2020-01-10 19:30:39 +0400 | [diff] [blame] | 304 | } |
| 305 | g_ptr_array_set_free_func(array, g_free); |
| 306 | g_ptr_array_free(array, true); |
Alexey Kardashevskiy | 35f6376 | 2018-03-02 00:09:38 +1100 | [diff] [blame] | 307 | qapi_free_ObjectPropertyInfoList(prop_list); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 308 | return 1; |
Eduardo Habkost | 5185f0e | 2014-11-01 13:56:10 -0200 | [diff] [blame] | 309 | |
| 310 | error: |
Markus Armbruster | 7828867 | 2015-12-18 16:35:07 +0100 | [diff] [blame] | 311 | error_report_err(local_err); |
Eduardo Habkost | 5185f0e | 2014-11-01 13:56:10 -0200 | [diff] [blame] | 312 | return 1; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 313 | } |
| 314 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 315 | static Object *qdev_get_peripheral(void) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 316 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 317 | static Object *dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 318 | |
| 319 | if (dev == NULL) { |
Andreas Färber | dfe47e7 | 2012-04-05 13:21:46 +0200 | [diff] [blame] | 320 | dev = container_get(qdev_get_machine(), "/peripheral"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 321 | } |
| 322 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 323 | return dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 324 | } |
| 325 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 326 | static Object *qdev_get_peripheral_anon(void) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 327 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 328 | static Object *dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 329 | |
| 330 | if (dev == NULL) { |
Andreas Färber | dfe47e7 | 2012-04-05 13:21:46 +0200 | [diff] [blame] | 331 | dev = container_get(qdev_get_machine(), "/peripheral-anon"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 332 | } |
| 333 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 334 | return dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 335 | } |
| 336 | |
Vladimir Sementsov-Ogievskiy | 3407732 | 2019-12-05 20:46:20 +0300 | [diff] [blame] | 337 | static void qbus_error_append_bus_list_hint(DeviceState *dev, |
| 338 | Error *const *errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 339 | { |
| 340 | BusState *child; |
| 341 | const char *sep = " "; |
| 342 | |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 343 | error_append_hint(errp, "child buses at \"%s\":", |
| 344 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 345 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 346 | error_append_hint(errp, "%s\"%s\"", sep, child->name); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 347 | sep = ", "; |
| 348 | } |
Markus Armbruster | 543202c | 2015-12-17 17:35:14 +0100 | [diff] [blame] | 349 | error_append_hint(errp, "\n"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 350 | } |
| 351 | |
Vladimir Sementsov-Ogievskiy | 3407732 | 2019-12-05 20:46:20 +0300 | [diff] [blame] | 352 | static void qbus_error_append_dev_list_hint(BusState *bus, |
| 353 | Error *const *errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 354 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 355 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 356 | const char *sep = " "; |
| 357 | |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 358 | error_append_hint(errp, "devices at \"%s\":", bus->name); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 359 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 360 | DeviceState *dev = kid->child; |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 361 | error_append_hint(errp, "%s\"%s\"", sep, |
| 362 | object_get_typename(OBJECT(dev))); |
| 363 | if (dev->id) { |
| 364 | error_append_hint(errp, "/\"%s\"", dev->id); |
| 365 | } |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 366 | sep = ", "; |
| 367 | } |
Markus Armbruster | 543202c | 2015-12-17 17:35:14 +0100 | [diff] [blame] | 368 | error_append_hint(errp, "\n"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) |
| 372 | { |
| 373 | BusState *child; |
| 374 | |
| 375 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 376 | if (strcmp(child->name, elem) == 0) { |
| 377 | return child; |
| 378 | } |
| 379 | } |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) |
| 384 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 385 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | * try to match in order: |
| 389 | * (1) instance id, if present |
| 390 | * (2) driver name |
| 391 | * (3) driver alias, if present |
| 392 | */ |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 393 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 394 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 395 | if (dev->id && strcmp(dev->id, elem) == 0) { |
| 396 | return dev; |
| 397 | } |
| 398 | } |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 399 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 400 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 401 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { |
| 402 | return dev; |
| 403 | } |
| 404 | } |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 405 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 406 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 407 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 408 | |
| 409 | if (qdev_class_has_alias(dc) && |
| 410 | strcmp(qdev_class_get_alias(dc), elem) == 0) { |
| 411 | return dev; |
| 412 | } |
| 413 | } |
| 414 | return NULL; |
| 415 | } |
| 416 | |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 417 | static inline bool qbus_is_full(BusState *bus) |
| 418 | { |
| 419 | BusClass *bus_class = BUS_GET_CLASS(bus); |
Tony Krowiak | 12b2e9f | 2018-12-17 10:57:30 -0500 | [diff] [blame] | 420 | return bus_class->max_dev && bus->num_children >= bus_class->max_dev; |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Search the tree rooted at @bus for a bus. |
| 425 | * If @name, search for a bus with that name. Note that bus names |
| 426 | * need not be unique. Yes, that's screwed up. |
| 427 | * Else search for a bus that is a subtype of @bus_typename. |
| 428 | * If more than one exists, prefer one that can take another device. |
| 429 | * Return the bus if found, else %NULL. |
| 430 | */ |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 431 | static BusState *qbus_find_recursive(BusState *bus, const char *name, |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 432 | const char *bus_typename) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 433 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 434 | BusChild *kid; |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 435 | BusState *pick, *child, *ret; |
| 436 | bool match; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 437 | |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 438 | assert(name || bus_typename); |
| 439 | if (name) { |
| 440 | match = !strcmp(bus->name, name); |
| 441 | } else { |
| 442 | match = !!object_dynamic_cast(OBJECT(bus), bus_typename); |
KONRAD Frederic | 1395af6 | 2013-01-15 00:08:00 +0100 | [diff] [blame] | 443 | } |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 444 | |
| 445 | if (match && !qbus_is_full(bus)) { |
| 446 | return bus; /* root matches and isn't full */ |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 447 | } |
| 448 | |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 449 | pick = match ? bus : NULL; |
| 450 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 451 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 452 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 453 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 454 | ret = qbus_find_recursive(child, name, bus_typename); |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 455 | if (ret && !qbus_is_full(ret)) { |
| 456 | return ret; /* a descendant matches and isn't full */ |
| 457 | } |
| 458 | if (ret && !pick) { |
| 459 | pick = ret; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 463 | |
| 464 | /* root or a descendant matches, but is full */ |
| 465 | return pick; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 466 | } |
| 467 | |
Markus Armbruster | d282842 | 2015-03-11 19:16:04 +0100 | [diff] [blame] | 468 | static BusState *qbus_find(const char *path, Error **errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 469 | { |
| 470 | DeviceState *dev; |
| 471 | BusState *bus; |
| 472 | char elem[128]; |
| 473 | int pos, len; |
| 474 | |
| 475 | /* find start element */ |
| 476 | if (path[0] == '/') { |
| 477 | bus = sysbus_get_default(); |
| 478 | pos = 0; |
| 479 | } else { |
| 480 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { |
| 481 | assert(!path[0]); |
| 482 | elem[0] = len = 0; |
| 483 | } |
| 484 | bus = qbus_find_recursive(sysbus_get_default(), elem, NULL); |
| 485 | if (!bus) { |
Markus Armbruster | d282842 | 2015-03-11 19:16:04 +0100 | [diff] [blame] | 486 | error_setg(errp, "Bus '%s' not found", elem); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 487 | return NULL; |
| 488 | } |
| 489 | pos = len; |
| 490 | } |
| 491 | |
| 492 | for (;;) { |
| 493 | assert(path[pos] == '/' || !path[pos]); |
| 494 | while (path[pos] == '/') { |
| 495 | pos++; |
| 496 | } |
| 497 | if (path[pos] == '\0') { |
Markus Armbruster | ed238ba | 2015-03-11 18:39:16 +0100 | [diff] [blame] | 498 | break; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* find device */ |
| 502 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
Stefan Weil | dfc6f86 | 2013-07-25 18:21:28 +0200 | [diff] [blame] | 503 | g_assert_not_reached(); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 504 | elem[0] = len = 0; |
| 505 | } |
| 506 | pos += len; |
| 507 | dev = qbus_find_dev(bus, elem); |
| 508 | if (!dev) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 509 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 510 | "Device '%s' not found", elem); |
Vladimir Sementsov-Ogievskiy | 3407732 | 2019-12-05 20:46:20 +0300 | [diff] [blame] | 511 | qbus_error_append_dev_list_hint(bus, errp); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 512 | return NULL; |
| 513 | } |
| 514 | |
| 515 | assert(path[pos] == '/' || !path[pos]); |
| 516 | while (path[pos] == '/') { |
| 517 | pos++; |
| 518 | } |
| 519 | if (path[pos] == '\0') { |
| 520 | /* last specified element is a device. If it has exactly |
| 521 | * one child bus accept it nevertheless */ |
Markus Armbruster | ed238ba | 2015-03-11 18:39:16 +0100 | [diff] [blame] | 522 | if (dev->num_child_bus == 1) { |
| 523 | bus = QLIST_FIRST(&dev->child_bus); |
| 524 | break; |
| 525 | } |
| 526 | if (dev->num_child_bus) { |
Markus Armbruster | d282842 | 2015-03-11 19:16:04 +0100 | [diff] [blame] | 527 | error_setg(errp, "Device '%s' has multiple child buses", |
| 528 | elem); |
Vladimir Sementsov-Ogievskiy | 3407732 | 2019-12-05 20:46:20 +0300 | [diff] [blame] | 529 | qbus_error_append_bus_list_hint(dev, errp); |
Markus Armbruster | ed238ba | 2015-03-11 18:39:16 +0100 | [diff] [blame] | 530 | } else { |
Markus Armbruster | d282842 | 2015-03-11 19:16:04 +0100 | [diff] [blame] | 531 | error_setg(errp, "Device '%s' has no child bus", elem); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 532 | } |
Markus Armbruster | ed238ba | 2015-03-11 18:39:16 +0100 | [diff] [blame] | 533 | return NULL; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /* find bus */ |
| 537 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
Stefan Weil | dfc6f86 | 2013-07-25 18:21:28 +0200 | [diff] [blame] | 538 | g_assert_not_reached(); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 539 | elem[0] = len = 0; |
| 540 | } |
| 541 | pos += len; |
| 542 | bus = qbus_find_bus(dev, elem); |
| 543 | if (!bus) { |
Markus Armbruster | d282842 | 2015-03-11 19:16:04 +0100 | [diff] [blame] | 544 | error_setg(errp, "Bus '%s' not found", elem); |
Vladimir Sementsov-Ogievskiy | 3407732 | 2019-12-05 20:46:20 +0300 | [diff] [blame] | 545 | qbus_error_append_bus_list_hint(dev, errp); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 546 | return NULL; |
| 547 | } |
| 548 | } |
Markus Armbruster | ed238ba | 2015-03-11 18:39:16 +0100 | [diff] [blame] | 549 | |
| 550 | if (qbus_is_full(bus)) { |
Markus Armbruster | d282842 | 2015-03-11 19:16:04 +0100 | [diff] [blame] | 551 | error_setg(errp, "Bus '%s' is full", path); |
Markus Armbruster | ed238ba | 2015-03-11 18:39:16 +0100 | [diff] [blame] | 552 | return NULL; |
| 553 | } |
| 554 | return bus; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 555 | } |
| 556 | |
Juergen Gross | ce49b73 | 2016-11-22 07:10:57 +0100 | [diff] [blame] | 557 | void qdev_set_id(DeviceState *dev, const char *id) |
| 558 | { |
| 559 | if (id) { |
| 560 | dev->id = id; |
| 561 | } |
| 562 | |
| 563 | if (dev->id) { |
| 564 | object_property_add_child(qdev_get_peripheral(), dev->id, |
Markus Armbruster | d262312 | 2020-05-05 17:29:22 +0200 | [diff] [blame] | 565 | OBJECT(dev)); |
Juergen Gross | ce49b73 | 2016-11-22 07:10:57 +0100 | [diff] [blame] | 566 | } else { |
| 567 | static int anon_count; |
| 568 | gchar *name = g_strdup_printf("device[%d]", anon_count++); |
| 569 | object_property_add_child(qdev_get_peripheral_anon(), name, |
Markus Armbruster | d262312 | 2020-05-05 17:29:22 +0200 | [diff] [blame] | 570 | OBJECT(dev)); |
Juergen Gross | ce49b73 | 2016-11-22 07:10:57 +0100 | [diff] [blame] | 571 | g_free(name); |
| 572 | } |
| 573 | } |
| 574 | |
Jens Freimann | f3a8505 | 2019-10-29 12:48:55 +0100 | [diff] [blame] | 575 | static int is_failover_device(void *opaque, const char *name, const char *value, |
| 576 | Error **errp) |
| 577 | { |
| 578 | if (strcmp(name, "failover_pair_id") == 0) { |
| 579 | QemuOpts *opts = (QemuOpts *)opaque; |
| 580 | |
| 581 | if (qdev_should_hide_device(opts)) { |
| 582 | return 1; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static bool should_hide_device(QemuOpts *opts) |
| 590 | { |
| 591 | if (qemu_opt_foreach(opts, is_failover_device, opts, NULL) == 0) { |
| 592 | return false; |
| 593 | } |
| 594 | return true; |
| 595 | } |
| 596 | |
Markus Armbruster | f006cf7 | 2015-03-12 14:00:41 +0100 | [diff] [blame] | 597 | DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 598 | { |
Andreas Färber | f4d8579 | 2013-08-24 01:21:22 +0200 | [diff] [blame] | 599 | DeviceClass *dc; |
Juergen Gross | ce49b73 | 2016-11-22 07:10:57 +0100 | [diff] [blame] | 600 | const char *driver, *path; |
Jens Freimann | f3a8505 | 2019-10-29 12:48:55 +0100 | [diff] [blame] | 601 | DeviceState *dev = NULL; |
Andreas Färber | 2f7bd82 | 2013-04-16 03:50:21 +0200 | [diff] [blame] | 602 | BusState *bus = NULL; |
Jens Freimann | f3a8505 | 2019-10-29 12:48:55 +0100 | [diff] [blame] | 603 | bool hide; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 604 | |
| 605 | driver = qemu_opt_get(opts, "driver"); |
| 606 | if (!driver) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 607 | error_setg(errp, QERR_MISSING_PARAMETER, "driver"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | /* find driver */ |
Markus Armbruster | f006cf7 | 2015-03-12 14:00:41 +0100 | [diff] [blame] | 612 | dc = qdev_get_device_class(&driver, errp); |
| 613 | if (!dc) { |
Markus Armbruster | 7ea5e78 | 2013-11-28 17:27:03 +0100 | [diff] [blame] | 614 | return NULL; |
| 615 | } |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 616 | |
| 617 | /* find bus */ |
| 618 | path = qemu_opt_get(opts, "bus"); |
| 619 | if (path != NULL) { |
Markus Armbruster | f006cf7 | 2015-03-12 14:00:41 +0100 | [diff] [blame] | 620 | bus = qbus_find(path, errp); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 621 | if (!bus) { |
| 622 | return NULL; |
| 623 | } |
Andreas Färber | f4d8579 | 2013-08-24 01:21:22 +0200 | [diff] [blame] | 624 | if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) { |
Markus Armbruster | f006cf7 | 2015-03-12 14:00:41 +0100 | [diff] [blame] | 625 | error_setg(errp, "Device '%s' can't go on %s bus", |
| 626 | driver, object_get_typename(OBJECT(bus))); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 627 | return NULL; |
| 628 | } |
Andreas Färber | f4d8579 | 2013-08-24 01:21:22 +0200 | [diff] [blame] | 629 | } else if (dc->bus_type != NULL) { |
| 630 | bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type); |
Markus Armbruster | a5ec494 | 2015-03-11 17:26:31 +0100 | [diff] [blame] | 631 | if (!bus || qbus_is_full(bus)) { |
Markus Armbruster | f006cf7 | 2015-03-12 14:00:41 +0100 | [diff] [blame] | 632 | error_setg(errp, "No '%s' bus found for device '%s'", |
| 633 | dc->bus_type, driver); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 634 | return NULL; |
| 635 | } |
| 636 | } |
Jens Freimann | f3a8505 | 2019-10-29 12:48:55 +0100 | [diff] [blame] | 637 | hide = should_hide_device(opts); |
| 638 | |
| 639 | if ((hide || qdev_hotplug) && bus && !qbus_is_hotpluggable(bus)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 640 | error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 641 | return NULL; |
| 642 | } |
| 643 | |
Jens Freimann | f3a8505 | 2019-10-29 12:48:55 +0100 | [diff] [blame] | 644 | if (hide) { |
| 645 | return NULL; |
| 646 | } |
| 647 | |
Juan Quintela | b06424d | 2017-03-22 17:34:27 +0100 | [diff] [blame] | 648 | if (!migration_is_idle()) { |
| 649 | error_setg(errp, "device_add not allowed while migrating"); |
| 650 | return NULL; |
| 651 | } |
| 652 | |
Amos Kong | 7b03094 | 2014-03-03 15:57:55 +0800 | [diff] [blame] | 653 | /* create device */ |
Markus Armbruster | 4e3a677 | 2020-06-10 07:31:54 +0200 | [diff] [blame] | 654 | dev = qdev_new(driver); |
Andreas Färber | 2f7bd82 | 2013-04-16 03:50:21 +0200 | [diff] [blame] | 655 | |
Peter Xu | d2321d3 | 2019-09-16 16:07:16 +0800 | [diff] [blame] | 656 | /* Check whether the hotplug is allowed by the machine */ |
Markus Armbruster | 992861f | 2020-07-07 18:06:04 +0200 | [diff] [blame] | 657 | if (qdev_hotplug && !qdev_hotplug_allowed(dev, errp)) { |
Peter Xu | d2321d3 | 2019-09-16 16:07:16 +0800 | [diff] [blame] | 658 | goto err_del_dev; |
| 659 | } |
| 660 | |
Markus Armbruster | 464a22c | 2020-06-10 07:32:44 +0200 | [diff] [blame] | 661 | if (!bus && qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) { |
Thomas Huth | 03fcbd9 | 2017-11-02 11:10:06 +0100 | [diff] [blame] | 662 | /* No bus, no machine hotplug handler --> device is not hotpluggable */ |
Markus Armbruster | 992861f | 2020-07-07 18:06:04 +0200 | [diff] [blame] | 663 | error_setg(errp, "Device '%s' can not be hotplugged on this machine", |
Thomas Huth | 03fcbd9 | 2017-11-02 11:10:06 +0100 | [diff] [blame] | 664 | driver); |
| 665 | goto err_del_dev; |
Andreas Färber | 2f7bd82 | 2013-04-16 03:50:21 +0200 | [diff] [blame] | 666 | } |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 667 | |
Juergen Gross | ce49b73 | 2016-11-22 07:10:57 +0100 | [diff] [blame] | 668 | qdev_set_id(dev, qemu_opts_id(opts)); |
Bandan Das | 52aa17c | 2014-02-26 10:32:40 -0700 | [diff] [blame] | 669 | |
Amos Kong | 7b03094 | 2014-03-03 15:57:55 +0800 | [diff] [blame] | 670 | /* set properties */ |
Markus Armbruster | 992861f | 2020-07-07 18:06:04 +0200 | [diff] [blame] | 671 | if (qemu_opt_foreach(opts, set_property, dev, errp)) { |
Thomas Huth | 5834621 | 2017-11-02 11:10:05 +0100 | [diff] [blame] | 672 | goto err_del_dev; |
Amos Kong | 7b03094 | 2014-03-03 15:57:55 +0800 | [diff] [blame] | 673 | } |
| 674 | |
Bandan Das | 52aa17c | 2014-02-26 10:32:40 -0700 | [diff] [blame] | 675 | dev->opts = opts; |
Markus Armbruster | 992861f | 2020-07-07 18:06:04 +0200 | [diff] [blame] | 676 | if (!qdev_realize(DEVICE(dev), bus, errp)) { |
Bandan Das | 52aa17c | 2014-02-26 10:32:40 -0700 | [diff] [blame] | 677 | dev->opts = NULL; |
Thomas Huth | 5834621 | 2017-11-02 11:10:05 +0100 | [diff] [blame] | 678 | goto err_del_dev; |
Paolo Bonzini | f424d5c | 2012-03-27 18:38:46 +0200 | [diff] [blame] | 679 | } |
Andreas Färber | 2bcb0c6 | 2013-10-07 16:17:54 +0200 | [diff] [blame] | 680 | return dev; |
Thomas Huth | 5834621 | 2017-11-02 11:10:05 +0100 | [diff] [blame] | 681 | |
| 682 | err_del_dev: |
Jens Freimann | f3a8505 | 2019-10-29 12:48:55 +0100 | [diff] [blame] | 683 | if (dev) { |
| 684 | object_unparent(OBJECT(dev)); |
| 685 | object_unref(OBJECT(dev)); |
| 686 | } |
Thomas Huth | 5834621 | 2017-11-02 11:10:05 +0100 | [diff] [blame] | 687 | return NULL; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | |
| 691 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) |
| 692 | static void qbus_print(Monitor *mon, BusState *bus, int indent); |
| 693 | |
| 694 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 695 | int indent) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 696 | { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 697 | if (!props) |
| 698 | return; |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 699 | for (; props->name; props++) { |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 700 | char *value; |
| 701 | char *legacy_name = g_strdup_printf("legacy-%s", props->name); |
Markus Armbruster | a43770d | 2020-07-07 18:06:13 +0200 | [diff] [blame] | 702 | |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 703 | if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) { |
Markus Armbruster | a43770d | 2020-07-07 18:06:13 +0200 | [diff] [blame] | 704 | value = object_property_get_str(OBJECT(dev), legacy_name, NULL); |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 705 | } else { |
Markus Armbruster | a43770d | 2020-07-07 18:06:13 +0200 | [diff] [blame] | 706 | value = object_property_print(OBJECT(dev), props->name, true, |
| 707 | NULL); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 708 | } |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 709 | g_free(legacy_name); |
| 710 | |
Markus Armbruster | a43770d | 2020-07-07 18:06:13 +0200 | [diff] [blame] | 711 | if (!value) { |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 712 | continue; |
| 713 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 714 | qdev_printf("%s = %s\n", props->name, |
Markus Armbruster | a43770d | 2020-07-07 18:06:13 +0200 | [diff] [blame] | 715 | *value ? value : "<null>"); |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 716 | g_free(value); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 720 | static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent) |
| 721 | { |
| 722 | BusClass *bc = BUS_GET_CLASS(bus); |
| 723 | |
| 724 | if (bc->print_dev) { |
| 725 | bc->print_dev(mon, dev, indent); |
| 726 | } |
| 727 | } |
| 728 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 729 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
| 730 | { |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 731 | ObjectClass *class; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 732 | BusState *child; |
Peter Crosthwaite | a5f5429 | 2014-05-19 23:30:58 -0700 | [diff] [blame] | 733 | NamedGPIOList *ngl; |
Damien Hedde | 9f2ff99 | 2020-04-06 15:52:51 +0200 | [diff] [blame] | 734 | NamedClockList *ncl; |
Peter Crosthwaite | a5f5429 | 2014-05-19 23:30:58 -0700 | [diff] [blame] | 735 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 736 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), |
| 737 | dev->id ? dev->id : ""); |
| 738 | indent += 2; |
Peter Crosthwaite | a5f5429 | 2014-05-19 23:30:58 -0700 | [diff] [blame] | 739 | QLIST_FOREACH(ngl, &dev->gpios, node) { |
| 740 | if (ngl->num_in) { |
| 741 | qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "", |
| 742 | ngl->num_in); |
| 743 | } |
| 744 | if (ngl->num_out) { |
| 745 | qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "", |
| 746 | ngl->num_out); |
| 747 | } |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 748 | } |
Damien Hedde | 9f2ff99 | 2020-04-06 15:52:51 +0200 | [diff] [blame] | 749 | QLIST_FOREACH(ncl, &dev->clocks, node) { |
| 750 | qdev_printf("clock-%s%s \"%s\" freq_hz=%e\n", |
| 751 | ncl->output ? "out" : "in", |
| 752 | ncl->alias ? " (alias)" : "", |
| 753 | ncl->name, |
| 754 | CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl->clock))); |
| 755 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 756 | class = object_get_class(OBJECT(dev)); |
| 757 | do { |
Paolo Bonzini | 385d8f2 | 2020-01-23 12:11:38 +0100 | [diff] [blame] | 758 | qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent); |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 759 | class = object_class_get_parent(class); |
| 760 | } while (class != object_class_by_name(TYPE_DEVICE)); |
Gerd Hoffmann | da9fbe7 | 2012-07-11 12:21:23 +0200 | [diff] [blame] | 761 | bus_print_dev(dev->parent_bus, mon, dev, indent); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 762 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 763 | qbus_print(mon, child, indent); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | static void qbus_print(Monitor *mon, BusState *bus, int indent) |
| 768 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 769 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 770 | |
| 771 | qdev_printf("bus: %s\n", bus->name); |
| 772 | indent += 2; |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 773 | qdev_printf("type %s\n", object_get_typename(OBJECT(bus))); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 774 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 775 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 776 | qdev_print(mon, dev, indent); |
| 777 | } |
| 778 | } |
| 779 | #undef qdev_printf |
| 780 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 781 | void hmp_info_qtree(Monitor *mon, const QDict *qdict) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 782 | { |
| 783 | if (sysbus_get_default()) |
| 784 | qbus_print(mon, sysbus_get_default(), 0); |
| 785 | } |
| 786 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 787 | void hmp_info_qdm(Monitor *mon, const QDict *qdict) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 788 | { |
Markus Armbruster | a3400ae | 2013-10-10 15:00:21 +0200 | [diff] [blame] | 789 | qdev_print_devinfos(true); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 790 | } |
| 791 | |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 792 | void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 793 | { |
| 794 | QemuOpts *opts; |
Paolo Bonzini | b09995a | 2013-01-25 14:12:37 +0100 | [diff] [blame] | 795 | DeviceState *dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 796 | |
Markus Armbruster | c6ecec4 | 2020-07-07 18:05:35 +0200 | [diff] [blame] | 797 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp); |
| 798 | if (!opts) { |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 799 | return; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 800 | } |
| 801 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { |
| 802 | qemu_opts_del(opts); |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 803 | return; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 804 | } |
Markus Armbruster | 668f62e | 2020-07-07 18:06:02 +0200 | [diff] [blame] | 805 | dev = qdev_device_add(opts, errp); |
Paolo Bonzini | b09995a | 2013-01-25 14:12:37 +0100 | [diff] [blame] | 806 | if (!dev) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 807 | qemu_opts_del(opts); |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 808 | return; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 809 | } |
Paolo Bonzini | b09995a | 2013-01-25 14:12:37 +0100 | [diff] [blame] | 810 | object_unref(OBJECT(dev)); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 811 | } |
| 812 | |
Kevin Wolf | 6c1db52 | 2016-09-20 13:38:41 +0200 | [diff] [blame] | 813 | static DeviceState *find_device_state(const char *id, Error **errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 814 | { |
Igor Mammedov | b6cc36a | 2014-10-02 10:08:45 +0000 | [diff] [blame] | 815 | Object *obj; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 816 | |
Daniel P. Berrange | 6287d82 | 2015-09-11 13:33:56 +0100 | [diff] [blame] | 817 | if (id[0] == '/') { |
| 818 | obj = object_resolve_path(id, NULL); |
| 819 | } else { |
| 820 | char *root_path = object_get_canonical_path(qdev_get_peripheral()); |
| 821 | char *path = g_strdup_printf("%s/%s", root_path, id); |
| 822 | |
| 823 | g_free(root_path); |
| 824 | obj = object_resolve_path_type(path, TYPE_DEVICE, NULL); |
| 825 | g_free(path); |
| 826 | } |
Igor Mammedov | b6cc36a | 2014-10-02 10:08:45 +0000 | [diff] [blame] | 827 | |
| 828 | if (!obj) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 829 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 830 | "Device '%s' not found", id); |
Kevin Wolf | 6c1db52 | 2016-09-20 13:38:41 +0200 | [diff] [blame] | 831 | return NULL; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 832 | } |
Luiz Capitulino | 56f9107 | 2012-03-14 17:37:38 -0300 | [diff] [blame] | 833 | |
Daniel P. Berrange | 6287d82 | 2015-09-11 13:33:56 +0100 | [diff] [blame] | 834 | if (!object_dynamic_cast(obj, TYPE_DEVICE)) { |
| 835 | error_setg(errp, "%s is not a hotpluggable device", id); |
Kevin Wolf | 6c1db52 | 2016-09-20 13:38:41 +0200 | [diff] [blame] | 836 | return NULL; |
Daniel P. Berrange | 6287d82 | 2015-09-11 13:33:56 +0100 | [diff] [blame] | 837 | } |
| 838 | |
Kevin Wolf | 6c1db52 | 2016-09-20 13:38:41 +0200 | [diff] [blame] | 839 | return DEVICE(obj); |
| 840 | } |
| 841 | |
Juan Quintela | 3290067 | 2017-03-28 11:22:51 +0200 | [diff] [blame] | 842 | void qdev_unplug(DeviceState *dev, Error **errp) |
| 843 | { |
| 844 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 845 | HotplugHandler *hotplug_ctrl; |
| 846 | HotplugHandlerClass *hdc; |
David Hildenbrand | 07578b0 | 2019-02-28 13:28:47 +0100 | [diff] [blame] | 847 | Error *local_err = NULL; |
Juan Quintela | 3290067 | 2017-03-28 11:22:51 +0200 | [diff] [blame] | 848 | |
| 849 | if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) { |
| 850 | error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); |
| 851 | return; |
| 852 | } |
| 853 | |
| 854 | if (!dc->hotpluggable) { |
| 855 | error_setg(errp, QERR_DEVICE_NO_HOTPLUG, |
| 856 | object_get_typename(OBJECT(dev))); |
| 857 | return; |
| 858 | } |
| 859 | |
Jens Freimann | a1190ab | 2019-10-29 12:49:01 +0100 | [diff] [blame] | 860 | if (!migration_is_idle() && !dev->allow_unplug_during_migration) { |
Juan Quintela | b06424d | 2017-03-22 17:34:27 +0100 | [diff] [blame] | 861 | error_setg(errp, "device_del not allowed while migrating"); |
| 862 | return; |
| 863 | } |
| 864 | |
Juan Quintela | 3290067 | 2017-03-28 11:22:51 +0200 | [diff] [blame] | 865 | qdev_hot_removed = true; |
| 866 | |
| 867 | hotplug_ctrl = qdev_get_hotplug_handler(dev); |
| 868 | /* hotpluggable device MUST have HotplugHandler, if it doesn't |
| 869 | * then something is very wrong with it */ |
| 870 | g_assert(hotplug_ctrl); |
| 871 | |
| 872 | /* If device supports async unplug just request it to be done, |
| 873 | * otherwise just remove it synchronously */ |
| 874 | hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl); |
| 875 | if (hdc->unplug_request) { |
David Hildenbrand | 07578b0 | 2019-02-28 13:28:47 +0100 | [diff] [blame] | 876 | hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err); |
Juan Quintela | 3290067 | 2017-03-28 11:22:51 +0200 | [diff] [blame] | 877 | } else { |
David Hildenbrand | 07578b0 | 2019-02-28 13:28:47 +0100 | [diff] [blame] | 878 | hotplug_handler_unplug(hotplug_ctrl, dev, &local_err); |
| 879 | if (!local_err) { |
| 880 | object_unparent(OBJECT(dev)); |
| 881 | } |
Juan Quintela | 3290067 | 2017-03-28 11:22:51 +0200 | [diff] [blame] | 882 | } |
David Hildenbrand | 07578b0 | 2019-02-28 13:28:47 +0100 | [diff] [blame] | 883 | error_propagate(errp, local_err); |
Juan Quintela | 3290067 | 2017-03-28 11:22:51 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Kevin Wolf | 6c1db52 | 2016-09-20 13:38:41 +0200 | [diff] [blame] | 886 | void qmp_device_del(const char *id, Error **errp) |
| 887 | { |
| 888 | DeviceState *dev = find_device_state(id, errp); |
| 889 | if (dev != NULL) { |
Julia Suvorova | cce8944 | 2020-02-20 17:55:56 +0100 | [diff] [blame] | 890 | if (dev->pending_deleted_event) { |
| 891 | error_setg(errp, "Device %s is already in the " |
| 892 | "process of unplug", id); |
| 893 | return; |
| 894 | } |
| 895 | |
Kevin Wolf | 6c1db52 | 2016-09-20 13:38:41 +0200 | [diff] [blame] | 896 | qdev_unplug(dev, errp); |
| 897 | } |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 898 | } |
| 899 | |
Markus Armbruster | d94bd09 | 2019-07-09 20:59:36 +0200 | [diff] [blame] | 900 | void hmp_device_add(Monitor *mon, const QDict *qdict) |
| 901 | { |
| 902 | Error *err = NULL; |
| 903 | |
| 904 | qmp_device_add((QDict *)qdict, NULL, &err); |
Vladimir Sementsov-Ogievskiy | 187c614 | 2019-12-05 20:46:18 +0300 | [diff] [blame] | 905 | hmp_handle_error(mon, err); |
Markus Armbruster | d94bd09 | 2019-07-09 20:59:36 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void hmp_device_del(Monitor *mon, const QDict *qdict) |
| 909 | { |
| 910 | const char *id = qdict_get_str(qdict, "id"); |
| 911 | Error *err = NULL; |
| 912 | |
| 913 | qmp_device_del(id, &err); |
Vladimir Sementsov-Ogievskiy | 187c614 | 2019-12-05 20:46:18 +0300 | [diff] [blame] | 914 | hmp_handle_error(mon, err); |
Markus Armbruster | d94bd09 | 2019-07-09 20:59:36 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Kevin Wolf | 9680cae | 2016-09-20 13:38:42 +0200 | [diff] [blame] | 917 | BlockBackend *blk_by_qdev_id(const char *id, Error **errp) |
| 918 | { |
| 919 | DeviceState *dev; |
| 920 | BlockBackend *blk; |
| 921 | |
| 922 | dev = find_device_state(id, errp); |
| 923 | if (dev == NULL) { |
| 924 | return NULL; |
| 925 | } |
| 926 | |
| 927 | blk = blk_by_dev(dev); |
| 928 | if (!blk) { |
| 929 | error_setg(errp, "Device does not have a block device backend"); |
| 930 | } |
| 931 | return blk; |
| 932 | } |
| 933 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 934 | void qdev_machine_init(void) |
| 935 | { |
| 936 | qdev_get_peripheral_anon(); |
| 937 | qdev_get_peripheral(); |
| 938 | } |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 939 | |
| 940 | QemuOptsList qemu_device_opts = { |
| 941 | .name = "device", |
| 942 | .implied_opt_name = "driver", |
| 943 | .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), |
| 944 | .desc = { |
| 945 | /* |
| 946 | * no elements => accept any |
| 947 | * sanity checking will happen later |
| 948 | * when setting device properties |
| 949 | */ |
| 950 | { /* end of list */ } |
| 951 | }, |
| 952 | }; |
| 953 | |
| 954 | QemuOptsList qemu_global_opts = { |
| 955 | .name = "global", |
| 956 | .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head), |
| 957 | .desc = { |
| 958 | { |
| 959 | .name = "driver", |
| 960 | .type = QEMU_OPT_STRING, |
| 961 | },{ |
| 962 | .name = "property", |
| 963 | .type = QEMU_OPT_STRING, |
| 964 | },{ |
| 965 | .name = "value", |
| 966 | .type = QEMU_OPT_STRING, |
| 967 | }, |
| 968 | { /* end of list */ } |
| 969 | }, |
| 970 | }; |
| 971 | |
| 972 | int qemu_global_option(const char *str) |
| 973 | { |
| 974 | char driver[64], property[64]; |
| 975 | QemuOpts *opts; |
| 976 | int rc, offset; |
| 977 | |
Paolo Bonzini | 3751d7c | 2015-04-09 14:16:19 +0200 | [diff] [blame] | 978 | rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset); |
| 979 | if (rc == 2 && str[offset] == '=') { |
| 980 | opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort); |
| 981 | qemu_opt_set(opts, "driver", driver, &error_abort); |
| 982 | qemu_opt_set(opts, "property", property, &error_abort); |
| 983 | qemu_opt_set(opts, "value", str + offset + 1, &error_abort); |
| 984 | return 0; |
| 985 | } |
| 986 | |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 987 | opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false); |
Paolo Bonzini | 3751d7c | 2015-04-09 14:16:19 +0200 | [diff] [blame] | 988 | if (!opts) { |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 989 | return -1; |
| 990 | } |
| 991 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 992 | return 0; |
| 993 | } |