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 | |
| 20 | #include "qdev.h" |
| 21 | #include "monitor.h" |
Luiz Capitulino | a15fef2 | 2012-03-29 12:38:50 -0300 | [diff] [blame] | 22 | #include "qmp-commands.h" |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 23 | #include "arch_init.h" |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * Aliases were a bad idea from the start. Let's keep them |
| 27 | * from spreading further. |
| 28 | */ |
| 29 | typedef struct QDevAlias |
| 30 | { |
| 31 | const char *typename; |
| 32 | const char *alias; |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 33 | uint32_t arch_mask; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 34 | } QDevAlias; |
| 35 | |
| 36 | static const QDevAlias qdev_alias_table[] = { |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 37 | { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
| 38 | { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
| 39 | { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
| 40 | { "virtio-balloon-pci", "virtio-balloon", |
| 41 | QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
| 42 | { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X }, |
| 43 | { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X }, |
| 44 | { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X }, |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 45 | { "lsi53c895a", "lsi" }, |
| 46 | { "ich9-ahci", "ahci" }, |
Jan Kiszka | c3ebd3b | 2012-08-30 20:30:00 +0200 | [diff] [blame] | 47 | { "kvm-pci-assign", "pci-assign" }, |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 48 | { } |
| 49 | }; |
| 50 | |
| 51 | static const char *qdev_class_get_alias(DeviceClass *dc) |
| 52 | { |
| 53 | const char *typename = object_class_get_name(OBJECT_CLASS(dc)); |
| 54 | int i; |
| 55 | |
| 56 | for (i = 0; qdev_alias_table[i].typename; i++) { |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 57 | if (qdev_alias_table[i].arch_mask && |
| 58 | !(qdev_alias_table[i].arch_mask & arch_type)) { |
| 59 | continue; |
| 60 | } |
| 61 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 62 | if (strcmp(qdev_alias_table[i].typename, typename) == 0) { |
| 63 | return qdev_alias_table[i].alias; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | static bool qdev_class_has_alias(DeviceClass *dc) |
| 71 | { |
| 72 | return (qdev_class_get_alias(dc) != NULL); |
| 73 | } |
| 74 | |
| 75 | static void qdev_print_devinfo(ObjectClass *klass, void *opaque) |
| 76 | { |
| 77 | DeviceClass *dc; |
| 78 | bool *show_no_user = opaque; |
| 79 | |
| 80 | dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE); |
| 81 | |
| 82 | if (!dc || (show_no_user && !*show_no_user && dc->no_user)) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | error_printf("name \"%s\"", object_class_get_name(klass)); |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 87 | if (dc->bus_type) { |
| 88 | error_printf(", bus %s", dc->bus_type); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 89 | } |
| 90 | if (qdev_class_has_alias(dc)) { |
| 91 | error_printf(", alias \"%s\"", qdev_class_get_alias(dc)); |
| 92 | } |
| 93 | if (dc->desc) { |
| 94 | error_printf(", desc \"%s\"", dc->desc); |
| 95 | } |
| 96 | if (dc->no_user) { |
| 97 | error_printf(", no-user"); |
| 98 | } |
| 99 | error_printf("\n"); |
| 100 | } |
| 101 | |
| 102 | static int set_property(const char *name, const char *value, void *opaque) |
| 103 | { |
| 104 | DeviceState *dev = opaque; |
| 105 | |
| 106 | if (strcmp(name, "driver") == 0) |
| 107 | return 0; |
| 108 | if (strcmp(name, "bus") == 0) |
| 109 | return 0; |
| 110 | |
| 111 | if (qdev_prop_parse(dev, name, value) == -1) { |
| 112 | return -1; |
| 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static const char *find_typename_by_alias(const char *alias) |
| 118 | { |
| 119 | int i; |
| 120 | |
| 121 | for (i = 0; qdev_alias_table[i].alias; i++) { |
Alexander Graf | 5f629d9 | 2012-05-18 02:36:26 +0200 | [diff] [blame] | 122 | if (qdev_alias_table[i].arch_mask && |
| 123 | !(qdev_alias_table[i].arch_mask & arch_type)) { |
| 124 | continue; |
| 125 | } |
| 126 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 127 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { |
| 128 | return qdev_alias_table[i].typename; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | int qdev_device_help(QemuOpts *opts) |
| 136 | { |
| 137 | const char *driver; |
| 138 | Property *prop; |
| 139 | ObjectClass *klass; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 140 | |
| 141 | driver = qemu_opt_get(opts, "driver"); |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 142 | if (driver && is_help_option(driver)) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 143 | bool show_no_user = false; |
| 144 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user); |
| 145 | return 1; |
| 146 | } |
| 147 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 148 | if (!driver || !qemu_opt_has_help_opt(opts)) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | klass = object_class_by_name(driver); |
| 153 | if (!klass) { |
| 154 | const char *typename = find_typename_by_alias(driver); |
| 155 | |
| 156 | if (typename) { |
| 157 | driver = typename; |
| 158 | klass = object_class_by_name(driver); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (!klass) { |
| 163 | return 0; |
| 164 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 165 | do { |
| 166 | for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) { |
| 167 | /* |
| 168 | * TODO Properties without a parser are just for dirty hacks. |
| 169 | * qdev_prop_ptr is the only such PropertyInfo. It's marked |
| 170 | * for removal. This conditional should be removed along with |
| 171 | * it. |
| 172 | */ |
Paolo Bonzini | 90ca64a | 2012-05-02 13:30:59 +0200 | [diff] [blame] | 173 | if (!prop->info->set) { |
Anthony Liguori | d03d6b4 | 2011-12-23 08:16:58 -0600 | [diff] [blame] | 174 | continue; /* no way to set it, don't show */ |
| 175 | } |
| 176 | error_printf("%s.%s=%s\n", driver, prop->name, |
| 177 | prop->info->legacy_name ?: prop->info->name); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 178 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 179 | klass = object_class_get_parent(klass); |
| 180 | } while (klass != object_class_by_name(TYPE_DEVICE)); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 184 | static Object *qdev_get_peripheral(void) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 185 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 186 | static Object *dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 187 | |
| 188 | if (dev == NULL) { |
Andreas Färber | dfe47e7 | 2012-04-05 13:21:46 +0200 | [diff] [blame] | 189 | dev = container_get(qdev_get_machine(), "/peripheral"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 190 | } |
| 191 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 192 | return dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 193 | } |
| 194 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 195 | static Object *qdev_get_peripheral_anon(void) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 196 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 197 | static Object *dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 198 | |
| 199 | if (dev == NULL) { |
Andreas Färber | dfe47e7 | 2012-04-05 13:21:46 +0200 | [diff] [blame] | 200 | dev = container_get(qdev_get_machine(), "/peripheral-anon"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 203 | return dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void qbus_list_bus(DeviceState *dev) |
| 207 | { |
| 208 | BusState *child; |
| 209 | const char *sep = " "; |
| 210 | |
| 211 | error_printf("child busses at \"%s\":", |
| 212 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); |
| 213 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 214 | error_printf("%s\"%s\"", sep, child->name); |
| 215 | sep = ", "; |
| 216 | } |
| 217 | error_printf("\n"); |
| 218 | } |
| 219 | |
| 220 | static void qbus_list_dev(BusState *bus) |
| 221 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 222 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 223 | const char *sep = " "; |
| 224 | |
| 225 | error_printf("devices at \"%s\":", bus->name); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 226 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 227 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 228 | error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev))); |
| 229 | if (dev->id) |
| 230 | error_printf("/\"%s\"", dev->id); |
| 231 | sep = ", "; |
| 232 | } |
| 233 | error_printf("\n"); |
| 234 | } |
| 235 | |
| 236 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) |
| 237 | { |
| 238 | BusState *child; |
| 239 | |
| 240 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 241 | if (strcmp(child->name, elem) == 0) { |
| 242 | return child; |
| 243 | } |
| 244 | } |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) |
| 249 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 250 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * try to match in order: |
| 254 | * (1) instance id, if present |
| 255 | * (2) driver name |
| 256 | * (3) driver alias, if present |
| 257 | */ |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 258 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 259 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 260 | if (dev->id && strcmp(dev->id, elem) == 0) { |
| 261 | return dev; |
| 262 | } |
| 263 | } |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 264 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 265 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 266 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { |
| 267 | return dev; |
| 268 | } |
| 269 | } |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 270 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 271 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 272 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 273 | |
| 274 | if (qdev_class_has_alias(dc) && |
| 275 | strcmp(qdev_class_get_alias(dc), elem) == 0) { |
| 276 | return dev; |
| 277 | } |
| 278 | } |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | static BusState *qbus_find_recursive(BusState *bus, const char *name, |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 283 | const char *bus_typename) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 284 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 285 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 286 | BusState *child, *ret; |
| 287 | int match = 1; |
| 288 | |
| 289 | if (name && (strcmp(bus->name, name) != 0)) { |
| 290 | match = 0; |
| 291 | } |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 292 | if (bus_typename && |
| 293 | (strcmp(object_get_typename(OBJECT(bus)), bus_typename) != 0)) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 294 | match = 0; |
| 295 | } |
| 296 | if (match) { |
| 297 | return bus; |
| 298 | } |
| 299 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 300 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 301 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 302 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 303 | ret = qbus_find_recursive(child, name, bus_typename); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 304 | if (ret) { |
| 305 | return ret; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | static BusState *qbus_find(const char *path) |
| 313 | { |
| 314 | DeviceState *dev; |
| 315 | BusState *bus; |
| 316 | char elem[128]; |
| 317 | int pos, len; |
| 318 | |
| 319 | /* find start element */ |
| 320 | if (path[0] == '/') { |
| 321 | bus = sysbus_get_default(); |
| 322 | pos = 0; |
| 323 | } else { |
| 324 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { |
| 325 | assert(!path[0]); |
| 326 | elem[0] = len = 0; |
| 327 | } |
| 328 | bus = qbus_find_recursive(sysbus_get_default(), elem, NULL); |
| 329 | if (!bus) { |
| 330 | qerror_report(QERR_BUS_NOT_FOUND, elem); |
| 331 | return NULL; |
| 332 | } |
| 333 | pos = len; |
| 334 | } |
| 335 | |
| 336 | for (;;) { |
| 337 | assert(path[pos] == '/' || !path[pos]); |
| 338 | while (path[pos] == '/') { |
| 339 | pos++; |
| 340 | } |
| 341 | if (path[pos] == '\0') { |
| 342 | return bus; |
| 343 | } |
| 344 | |
| 345 | /* find device */ |
| 346 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
| 347 | assert(0); |
| 348 | elem[0] = len = 0; |
| 349 | } |
| 350 | pos += len; |
| 351 | dev = qbus_find_dev(bus, elem); |
| 352 | if (!dev) { |
| 353 | qerror_report(QERR_DEVICE_NOT_FOUND, elem); |
| 354 | if (!monitor_cur_is_qmp()) { |
| 355 | qbus_list_dev(bus); |
| 356 | } |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | assert(path[pos] == '/' || !path[pos]); |
| 361 | while (path[pos] == '/') { |
| 362 | pos++; |
| 363 | } |
| 364 | if (path[pos] == '\0') { |
| 365 | /* last specified element is a device. If it has exactly |
| 366 | * one child bus accept it nevertheless */ |
| 367 | switch (dev->num_child_bus) { |
| 368 | case 0: |
| 369 | qerror_report(QERR_DEVICE_NO_BUS, elem); |
| 370 | return NULL; |
| 371 | case 1: |
| 372 | return QLIST_FIRST(&dev->child_bus); |
| 373 | default: |
| 374 | qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem); |
| 375 | if (!monitor_cur_is_qmp()) { |
| 376 | qbus_list_bus(dev); |
| 377 | } |
| 378 | return NULL; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /* find bus */ |
| 383 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
| 384 | assert(0); |
| 385 | elem[0] = len = 0; |
| 386 | } |
| 387 | pos += len; |
| 388 | bus = qbus_find_bus(dev, elem); |
| 389 | if (!bus) { |
| 390 | qerror_report(QERR_BUS_NOT_FOUND, elem); |
| 391 | if (!monitor_cur_is_qmp()) { |
| 392 | qbus_list_bus(dev); |
| 393 | } |
| 394 | return NULL; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | DeviceState *qdev_device_add(QemuOpts *opts) |
| 400 | { |
| 401 | ObjectClass *obj; |
| 402 | DeviceClass *k; |
| 403 | const char *driver, *path, *id; |
| 404 | DeviceState *qdev; |
| 405 | BusState *bus; |
| 406 | |
| 407 | driver = qemu_opt_get(opts, "driver"); |
| 408 | if (!driver) { |
| 409 | qerror_report(QERR_MISSING_PARAMETER, "driver"); |
| 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | /* find driver */ |
| 414 | obj = object_class_by_name(driver); |
| 415 | if (!obj) { |
| 416 | const char *typename = find_typename_by_alias(driver); |
| 417 | |
| 418 | if (typename) { |
| 419 | driver = typename; |
| 420 | obj = object_class_by_name(driver); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (!obj) { |
| 425 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type"); |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | k = DEVICE_CLASS(obj); |
| 430 | |
| 431 | /* find bus */ |
| 432 | path = qemu_opt_get(opts, "bus"); |
| 433 | if (path != NULL) { |
| 434 | bus = qbus_find(path); |
| 435 | if (!bus) { |
| 436 | return NULL; |
| 437 | } |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 438 | if (strcmp(object_get_typename(OBJECT(bus)), k->bus_type) != 0) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 439 | qerror_report(QERR_BAD_BUS_FOR_DEVICE, |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 440 | driver, object_get_typename(OBJECT(bus))); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 441 | return NULL; |
| 442 | } |
| 443 | } else { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 444 | bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 445 | if (!bus) { |
| 446 | qerror_report(QERR_NO_BUS_FOR_DEVICE, |
Alberto Garcia | c3594ed | 2012-08-14 14:41:28 +0300 | [diff] [blame] | 447 | k->bus_type, driver); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 448 | return NULL; |
| 449 | } |
| 450 | } |
| 451 | if (qdev_hotplug && !bus->allow_hotplug) { |
| 452 | qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | if (!bus) { |
| 457 | bus = sysbus_get_default(); |
| 458 | } |
| 459 | |
| 460 | /* create device, set properties */ |
| 461 | qdev = DEVICE(object_new(driver)); |
| 462 | qdev_set_parent_bus(qdev, bus); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 463 | |
| 464 | id = qemu_opts_id(opts); |
| 465 | if (id) { |
| 466 | qdev->id = id; |
Anthony Liguori | b2d4b3f | 2012-02-12 11:36:24 -0600 | [diff] [blame] | 467 | } |
| 468 | if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { |
| 469 | qdev_free(qdev); |
| 470 | return NULL; |
| 471 | } |
Anthony Liguori | b2d4b3f | 2012-02-12 11:36:24 -0600 | [diff] [blame] | 472 | if (qdev->id) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 473 | object_property_add_child(qdev_get_peripheral(), qdev->id, |
| 474 | OBJECT(qdev), NULL); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 475 | } else { |
| 476 | static int anon_count; |
| 477 | gchar *name = g_strdup_printf("device[%d]", anon_count++); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 478 | object_property_add_child(qdev_get_peripheral_anon(), name, |
| 479 | OBJECT(qdev), NULL); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 480 | g_free(name); |
| 481 | } |
Paolo Bonzini | f424d5c | 2012-03-27 18:38:46 +0200 | [diff] [blame] | 482 | if (qdev_init(qdev) < 0) { |
| 483 | qerror_report(QERR_DEVICE_INIT_FAILED, driver); |
| 484 | return NULL; |
| 485 | } |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 486 | qdev->opts = opts; |
| 487 | return qdev; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) |
| 492 | static void qbus_print(Monitor *mon, BusState *bus, int indent); |
| 493 | |
| 494 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 495 | int indent) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 496 | { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 497 | if (!props) |
| 498 | return; |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 499 | for (; props->name; props++) { |
| 500 | Error *err = NULL; |
| 501 | char *value; |
| 502 | char *legacy_name = g_strdup_printf("legacy-%s", props->name); |
| 503 | if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) { |
| 504 | value = object_property_get_str(OBJECT(dev), legacy_name, &err); |
| 505 | } else { |
Paolo Bonzini | 8185bfc | 2012-05-02 13:31:00 +0200 | [diff] [blame] | 506 | value = object_property_print(OBJECT(dev), props->name, &err); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 507 | } |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 508 | g_free(legacy_name); |
| 509 | |
| 510 | if (err) { |
| 511 | error_free(err); |
| 512 | continue; |
| 513 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 514 | qdev_printf("%s = %s\n", props->name, |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 515 | value && *value ? value : "<null>"); |
| 516 | g_free(value); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 520 | static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent) |
| 521 | { |
| 522 | BusClass *bc = BUS_GET_CLASS(bus); |
| 523 | |
| 524 | if (bc->print_dev) { |
| 525 | bc->print_dev(mon, dev, indent); |
| 526 | } |
| 527 | } |
| 528 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 529 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
| 530 | { |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 531 | ObjectClass *class; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 532 | BusState *child; |
| 533 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), |
| 534 | dev->id ? dev->id : ""); |
| 535 | indent += 2; |
| 536 | if (dev->num_gpio_in) { |
| 537 | qdev_printf("gpio-in %d\n", dev->num_gpio_in); |
| 538 | } |
| 539 | if (dev->num_gpio_out) { |
| 540 | qdev_printf("gpio-out %d\n", dev->num_gpio_out); |
| 541 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 542 | class = object_get_class(OBJECT(dev)); |
| 543 | do { |
| 544 | qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent); |
| 545 | class = object_class_get_parent(class); |
| 546 | } while (class != object_class_by_name(TYPE_DEVICE)); |
Gerd Hoffmann | da9fbe7 | 2012-07-11 12:21:23 +0200 | [diff] [blame] | 547 | bus_print_dev(dev->parent_bus, mon, dev, indent); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 548 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 549 | qbus_print(mon, child, indent); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | static void qbus_print(Monitor *mon, BusState *bus, int indent) |
| 554 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 555 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 556 | |
| 557 | qdev_printf("bus: %s\n", bus->name); |
| 558 | indent += 2; |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 559 | qdev_printf("type %s\n", object_get_typename(OBJECT(bus))); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 560 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 561 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 562 | qdev_print(mon, dev, indent); |
| 563 | } |
| 564 | } |
| 565 | #undef qdev_printf |
| 566 | |
| 567 | void do_info_qtree(Monitor *mon) |
| 568 | { |
| 569 | if (sysbus_get_default()) |
| 570 | qbus_print(mon, sysbus_get_default(), 0); |
| 571 | } |
| 572 | |
| 573 | void do_info_qdm(Monitor *mon) |
| 574 | { |
| 575 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL); |
| 576 | } |
| 577 | |
| 578 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) |
| 579 | { |
Luiz Capitulino | 4e89978 | 2012-04-18 17:24:01 -0300 | [diff] [blame] | 580 | Error *local_err = NULL; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 581 | QemuOpts *opts; |
| 582 | |
Luiz Capitulino | 4e89978 | 2012-04-18 17:24:01 -0300 | [diff] [blame] | 583 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err); |
| 584 | if (error_is_set(&local_err)) { |
| 585 | qerror_report_err(local_err); |
| 586 | error_free(local_err); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 587 | return -1; |
| 588 | } |
| 589 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { |
| 590 | qemu_opts_del(opts); |
| 591 | return 0; |
| 592 | } |
| 593 | if (!qdev_device_add(opts)) { |
| 594 | qemu_opts_del(opts); |
| 595 | return -1; |
| 596 | } |
| 597 | return 0; |
| 598 | } |
| 599 | |
Luiz Capitulino | a15fef2 | 2012-03-29 12:38:50 -0300 | [diff] [blame] | 600 | void qmp_device_del(const char *id, Error **errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 601 | { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 602 | DeviceState *dev; |
| 603 | |
| 604 | dev = qdev_find_recursive(sysbus_get_default(), id); |
| 605 | if (NULL == dev) { |
Luiz Capitulino | a15fef2 | 2012-03-29 12:38:50 -0300 | [diff] [blame] | 606 | error_set(errp, QERR_DEVICE_NOT_FOUND, id); |
| 607 | return; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 608 | } |
Luiz Capitulino | 56f9107 | 2012-03-14 17:37:38 -0300 | [diff] [blame] | 609 | |
Luiz Capitulino | a15fef2 | 2012-03-29 12:38:50 -0300 | [diff] [blame] | 610 | qdev_unplug(dev, errp); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void qdev_machine_init(void) |
| 614 | { |
| 615 | qdev_get_peripheral_anon(); |
| 616 | qdev_get_peripheral(); |
| 617 | } |