Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* The theory here is that it should be possible to create a machine without |
| 21 | knowledge of specific devices. Historically board init routines have |
| 22 | passed a bunch of arguments to each device, requiring the board know |
| 23 | exactly which device it is dealing with. This file provides an abstract |
| 24 | API for device configuration and initialization. Devices will generally |
| 25 | inherit from a particular bus (e.g. PCI or I2C) rather than |
| 26 | this API directly. */ |
| 27 | |
Paul Brook | 9d07d75 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 28 | #include "net.h" |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 29 | #include "qdev.h" |
| 30 | #include "sysemu.h" |
Luiz Capitulino | 56f9107 | 2012-03-14 17:37:38 -0300 | [diff] [blame] | 31 | #include "error.h" |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 32 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 33 | int qdev_hotplug = 0; |
Alex Williamson | 0ac8ef7 | 2011-01-04 12:37:50 -0700 | [diff] [blame] | 34 | static bool qdev_hot_added = false; |
| 35 | static bool qdev_hot_removed = false; |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 36 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 37 | /* Register a new device type. */ |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 38 | const VMStateDescription *qdev_get_vmsd(DeviceState *dev) |
| 39 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 40 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 41 | return dc->vmsd; |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 42 | } |
| 43 | |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 44 | const char *qdev_fw_name(DeviceState *dev) |
| 45 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 46 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 47 | |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 48 | if (dc->fw_name) { |
| 49 | return dc->fw_name; |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return object_get_typename(OBJECT(dev)); |
| 53 | } |
| 54 | |
Blue Swirl | a369da5 | 2011-09-27 19:15:42 +0000 | [diff] [blame] | 55 | bool qdev_exists(const char *name) |
| 56 | { |
Anthony Liguori | 212ad11 | 2012-02-01 09:34:28 -0600 | [diff] [blame] | 57 | return !!object_class_by_name(name); |
Blue Swirl | a369da5 | 2011-09-27 19:15:42 +0000 | [diff] [blame] | 58 | } |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 59 | |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 60 | static void qdev_property_add_legacy(DeviceState *dev, Property *prop, |
| 61 | Error **errp); |
| 62 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 63 | static void bus_remove_child(BusState *bus, DeviceState *child) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 64 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 65 | BusChild *kid; |
| 66 | |
| 67 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 68 | if (kid->child == child) { |
| 69 | char name[32]; |
| 70 | |
| 71 | snprintf(name, sizeof(name), "child[%d]", kid->index); |
| 72 | QTAILQ_REMOVE(&bus->children, kid, sibling); |
| 73 | object_property_del(OBJECT(bus), name, NULL); |
| 74 | g_free(kid); |
| 75 | return; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void bus_add_child(BusState *bus, DeviceState *child) |
| 81 | { |
| 82 | char name[32]; |
| 83 | BusChild *kid = g_malloc0(sizeof(*kid)); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 84 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 85 | if (qdev_hotplug) { |
| 86 | assert(bus->allow_hotplug); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 87 | } |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 88 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 89 | kid->index = bus->max_index++; |
| 90 | kid->child = child; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 91 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 92 | QTAILQ_INSERT_HEAD(&bus->children, kid, sibling); |
| 93 | |
| 94 | snprintf(name, sizeof(name), "child[%d]", kid->index); |
| 95 | object_property_add_link(OBJECT(bus), name, |
| 96 | object_get_typename(OBJECT(child)), |
| 97 | (Object **)&kid->child, |
| 98 | NULL); |
| 99 | } |
| 100 | |
| 101 | void qdev_set_parent_bus(DeviceState *dev, BusState *bus) |
| 102 | { |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 103 | dev->parent_bus = bus; |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 104 | bus_add_child(bus, dev); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 107 | /* Create a new device. This only initializes the device state structure |
| 108 | and allows properties to be set. qdev_init should be called to |
| 109 | initialize the actual device emulation. */ |
| 110 | DeviceState *qdev_create(BusState *bus, const char *name) |
| 111 | { |
Blue Swirl | 0bcdeda | 2011-02-05 14:34:25 +0000 | [diff] [blame] | 112 | DeviceState *dev; |
| 113 | |
| 114 | dev = qdev_try_create(bus, name); |
| 115 | if (!dev) { |
Peter Maydell | e92714c | 2011-08-03 23:49:04 +0100 | [diff] [blame] | 116 | if (bus) { |
| 117 | hw_error("Unknown device '%s' for bus '%s'\n", name, |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 118 | object_get_typename(OBJECT(bus))); |
Peter Maydell | e92714c | 2011-08-03 23:49:04 +0100 | [diff] [blame] | 119 | } else { |
| 120 | hw_error("Unknown device '%s' for default sysbus\n", name); |
| 121 | } |
Blue Swirl | 0bcdeda | 2011-02-05 14:34:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | return dev; |
| 125 | } |
| 126 | |
Paolo Bonzini | da57feb | 2012-03-27 18:38:47 +0200 | [diff] [blame] | 127 | DeviceState *qdev_try_create(BusState *bus, const char *type) |
Blue Swirl | 0bcdeda | 2011-02-05 14:34:25 +0000 | [diff] [blame] | 128 | { |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 129 | DeviceState *dev; |
| 130 | |
Paolo Bonzini | da57feb | 2012-03-27 18:38:47 +0200 | [diff] [blame] | 131 | if (object_class_by_name(type) == NULL) { |
Andreas Färber | 4ed658c | 2012-02-17 02:47:44 +0100 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
Paolo Bonzini | da57feb | 2012-03-27 18:38:47 +0200 | [diff] [blame] | 134 | dev = DEVICE(object_new(type)); |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 135 | if (!dev) { |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 139 | if (!bus) { |
Stefan Weil | 6869489 | 2010-12-16 19:33:22 +0100 | [diff] [blame] | 140 | bus = sysbus_get_default(); |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 143 | qdev_set_parent_bus(dev, bus); |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 144 | |
| 145 | return dev; |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 148 | /* Initialize a device. Device properties should be set before calling |
| 149 | this function. IRQs and MMIO regions should be connected/mapped after |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 150 | calling this function. |
| 151 | On failure, destroy the device and return negative value. |
| 152 | Return 0 on success. */ |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 153 | int qdev_init(DeviceState *dev) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 154 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 155 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 156 | int rc; |
| 157 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 158 | assert(dev->state == DEV_STATE_CREATED); |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 159 | |
Anthony Liguori | d307af7 | 2011-12-09 15:02:56 -0600 | [diff] [blame] | 160 | rc = dc->init(dev); |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 161 | if (rc < 0) { |
Jason Baron | 266ca11 | 2012-05-02 22:42:10 -0400 | [diff] [blame] | 162 | object_unparent(OBJECT(dev)); |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 163 | qdev_free(dev); |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 164 | return rc; |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 165 | } |
Paolo Bonzini | da57feb | 2012-03-27 18:38:47 +0200 | [diff] [blame] | 166 | |
| 167 | if (!OBJECT(dev)->parent) { |
| 168 | static int unattached_count = 0; |
| 169 | gchar *name = g_strdup_printf("device[%d]", unattached_count++); |
| 170 | |
Andreas Färber | dfe47e7 | 2012-04-05 13:21:46 +0200 | [diff] [blame] | 171 | object_property_add_child(container_get(qdev_get_machine(), |
| 172 | "/unattached"), |
| 173 | name, OBJECT(dev), NULL); |
Paolo Bonzini | da57feb | 2012-03-27 18:38:47 +0200 | [diff] [blame] | 174 | g_free(name); |
| 175 | } |
| 176 | |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 177 | if (qdev_get_vmsd(dev)) { |
| 178 | vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 179 | dev->instance_id_alias, |
| 180 | dev->alias_required_for_version); |
| 181 | } |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 182 | dev->state = DEV_STATE_INITIALIZED; |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 183 | if (dev->hotplugged) { |
| 184 | device_reset(dev); |
Jan Kiszka | 5ab28c8 | 2011-07-24 19:38:36 +0200 | [diff] [blame] | 185 | } |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 186 | return 0; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 189 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
| 190 | int required_for_version) |
| 191 | { |
| 192 | assert(dev->state == DEV_STATE_CREATED); |
| 193 | dev->instance_id_alias = alias_id; |
| 194 | dev->alias_required_for_version = required_for_version; |
| 195 | } |
| 196 | |
Luiz Capitulino | 56f9107 | 2012-03-14 17:37:38 -0300 | [diff] [blame] | 197 | void qdev_unplug(DeviceState *dev, Error **errp) |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 198 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 199 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 200 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 201 | if (!dev->parent_bus->allow_hotplug) { |
Luiz Capitulino | 56f9107 | 2012-03-14 17:37:38 -0300 | [diff] [blame] | 202 | error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); |
| 203 | return; |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 204 | } |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 205 | assert(dc->unplug != NULL); |
Amit Shah | 593831d | 2009-11-02 14:56:41 +0530 | [diff] [blame] | 206 | |
Alex Williamson | 0ac8ef7 | 2011-01-04 12:37:50 -0700 | [diff] [blame] | 207 | qdev_hot_removed = true; |
| 208 | |
Luiz Capitulino | 56f9107 | 2012-03-14 17:37:38 -0300 | [diff] [blame] | 209 | if (dc->unplug(dev) < 0) { |
| 210 | error_set(errp, QERR_UNDEFINED_ERROR); |
| 211 | return; |
| 212 | } |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Anthony Liguori | ec990eb | 2010-11-19 18:55:59 +0900 | [diff] [blame] | 215 | static int qdev_reset_one(DeviceState *dev, void *opaque) |
| 216 | { |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 217 | device_reset(dev); |
Anthony Liguori | ec990eb | 2010-11-19 18:55:59 +0900 | [diff] [blame] | 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
Isaku Yamahata | b4694b7 | 2010-11-19 18:56:00 +0900 | [diff] [blame] | 222 | static int qbus_reset_one(BusState *bus, void *opaque) |
| 223 | { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 224 | BusClass *bc = BUS_GET_CLASS(bus); |
| 225 | if (bc->reset) { |
| 226 | return bc->reset(bus); |
Isaku Yamahata | b4694b7 | 2010-11-19 18:56:00 +0900 | [diff] [blame] | 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
Isaku Yamahata | 5af0a04 | 2010-11-19 18:56:01 +0900 | [diff] [blame] | 231 | void qdev_reset_all(DeviceState *dev) |
| 232 | { |
| 233 | qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL); |
| 234 | } |
| 235 | |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 236 | void qbus_reset_all_fn(void *opaque) |
| 237 | { |
| 238 | BusState *bus = opaque; |
Michael S. Tsirkin | f530cce | 2010-12-20 15:17:10 +0200 | [diff] [blame] | 239 | qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL); |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 242 | /* can be used as ->unplug() callback for the simple cases */ |
| 243 | int qdev_simple_unplug_cb(DeviceState *dev) |
| 244 | { |
| 245 | /* just zap it */ |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 246 | object_unparent(OBJECT(dev)); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 247 | qdev_free(dev); |
| 248 | return 0; |
| 249 | } |
| 250 | |
Michael Tokarev | 3b29a10 | 2011-04-06 17:51:59 +0400 | [diff] [blame] | 251 | |
| 252 | /* Like qdev_init(), but terminate program via error_report() instead of |
Markus Armbruster | e23a1b3 | 2009-10-07 01:15:58 +0200 | [diff] [blame] | 253 | returning an error value. This is okay during machine creation. |
| 254 | Don't use for hotplug, because there callers need to recover from |
| 255 | failure. Exception: if you know the device's init() callback can't |
| 256 | fail, then qdev_init_nofail() can't fail either, and is therefore |
| 257 | usable even then. But relying on the device implementation that |
| 258 | way is somewhat unclean, and best avoided. */ |
| 259 | void qdev_init_nofail(DeviceState *dev) |
| 260 | { |
Anthony Liguori | 7de3abe | 2012-06-27 07:37:54 -0500 | [diff] [blame] | 261 | const char *typename = object_get_typename(OBJECT(dev)); |
| 262 | |
Markus Armbruster | bd6c9a6 | 2010-05-27 21:23:08 +0200 | [diff] [blame] | 263 | if (qdev_init(dev) < 0) { |
Anthony Liguori | 7de3abe | 2012-06-27 07:37:54 -0500 | [diff] [blame] | 264 | error_report("Initialization of device %s failed", typename); |
Markus Armbruster | bd6c9a6 | 2010-05-27 21:23:08 +0200 | [diff] [blame] | 265 | exit(1); |
| 266 | } |
Markus Armbruster | e23a1b3 | 2009-10-07 01:15:58 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 269 | /* Unlink device from bus and free the structure. */ |
| 270 | void qdev_free(DeviceState *dev) |
| 271 | { |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 272 | object_delete(OBJECT(dev)); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 275 | void qdev_machine_creation_done(void) |
| 276 | { |
| 277 | /* |
| 278 | * ok, initial machine setup is done, starting from now we can |
| 279 | * only create hotpluggable devices |
| 280 | */ |
| 281 | qdev_hotplug = 1; |
| 282 | } |
| 283 | |
Alex Williamson | 0ac8ef7 | 2011-01-04 12:37:50 -0700 | [diff] [blame] | 284 | bool qdev_machine_modified(void) |
| 285 | { |
| 286 | return qdev_hot_added || qdev_hot_removed; |
| 287 | } |
| 288 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 289 | BusState *qdev_get_parent_bus(DeviceState *dev) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 290 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 291 | return dev->parent_bus; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 294 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) |
| 295 | { |
| 296 | assert(dev->num_gpio_in == 0); |
| 297 | dev->num_gpio_in = n; |
| 298 | dev->gpio_in = qemu_allocate_irqs(handler, dev, n); |
| 299 | } |
| 300 | |
| 301 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) |
| 302 | { |
| 303 | assert(dev->num_gpio_out == 0); |
| 304 | dev->num_gpio_out = n; |
| 305 | dev->gpio_out = pins; |
| 306 | } |
| 307 | |
| 308 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) |
| 309 | { |
| 310 | assert(n >= 0 && n < dev->num_gpio_in); |
| 311 | return dev->gpio_in[n]; |
| 312 | } |
| 313 | |
| 314 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) |
| 315 | { |
| 316 | assert(n >= 0 && n < dev->num_gpio_out); |
| 317 | dev->gpio_out[n] = pin; |
| 318 | } |
| 319 | |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 320 | void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd) |
| 321 | { |
Jan Kiszka | 6eed185 | 2011-07-20 12:20:22 +0200 | [diff] [blame] | 322 | qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a); |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 323 | if (nd->vlan) |
| 324 | qdev_prop_set_vlan(dev, "vlan", nd->vlan); |
| 325 | if (nd->netdev) |
| 326 | qdev_prop_set_netdev(dev, "netdev", nd->netdev); |
Amit Shah | 75422b0 | 2010-02-25 17:24:43 +0530 | [diff] [blame] | 327 | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 328 | object_property_find(OBJECT(dev), "vectors", NULL)) { |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 329 | qdev_prop_set_uint32(dev, "vectors", nd->nvectors); |
| 330 | } |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 331 | nd->instantiated = 1; |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 334 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 335 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 336 | BusState *bus; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 337 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 338 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 339 | if (strcmp(name, bus->name) == 0) { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 340 | return bus; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | return NULL; |
| 344 | } |
| 345 | |
Anthony Liguori | 81699d8 | 2010-11-19 18:55:58 +0900 | [diff] [blame] | 346 | int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, |
| 347 | qbus_walkerfn *busfn, void *opaque) |
| 348 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 349 | BusChild *kid; |
Anthony Liguori | 81699d8 | 2010-11-19 18:55:58 +0900 | [diff] [blame] | 350 | int err; |
| 351 | |
| 352 | if (busfn) { |
| 353 | err = busfn(bus, opaque); |
| 354 | if (err) { |
| 355 | return err; |
| 356 | } |
| 357 | } |
| 358 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 359 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 360 | err = qdev_walk_children(kid->child, devfn, busfn, opaque); |
Anthony Liguori | 81699d8 | 2010-11-19 18:55:58 +0900 | [diff] [blame] | 361 | if (err < 0) { |
| 362 | return err; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, |
| 370 | qbus_walkerfn *busfn, void *opaque) |
| 371 | { |
| 372 | BusState *bus; |
| 373 | int err; |
| 374 | |
| 375 | if (devfn) { |
| 376 | err = devfn(dev, opaque); |
| 377 | if (err) { |
| 378 | return err; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 383 | err = qbus_walk_children(bus, devfn, busfn, opaque); |
| 384 | if (err < 0) { |
| 385 | return err; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Isaku Yamahata | a2ee6b4 | 2010-12-24 12:14:12 +0900 | [diff] [blame] | 392 | DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 393 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 394 | BusChild *kid; |
| 395 | DeviceState *ret; |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 396 | BusState *child; |
| 397 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 398 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 399 | DeviceState *dev = kid->child; |
| 400 | |
| 401 | if (dev->id && strcmp(dev->id, id) == 0) { |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 402 | return dev; |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 403 | } |
| 404 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 405 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 406 | ret = qdev_find_recursive(child, id); |
| 407 | if (ret) { |
| 408 | return ret; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | return NULL; |
| 413 | } |
| 414 | |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 415 | static void qbus_realize(BusState *bus) |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 416 | { |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 417 | const char *typename = object_get_typename(OBJECT(bus)); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 418 | char *buf; |
| 419 | int i,len; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 420 | |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 421 | if (bus->name) { |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 422 | /* use supplied name */ |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 423 | } else if (bus->parent && bus->parent->id) { |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 424 | /* parent device has id -> use it for bus name */ |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 425 | len = strlen(bus->parent->id) + 16; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 426 | buf = g_malloc(len); |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 427 | snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 428 | bus->name = buf; |
| 429 | } else { |
| 430 | /* no id -> use lowercase bus type for bus name */ |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 431 | len = strlen(typename) + 16; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 432 | buf = g_malloc(len); |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 433 | len = snprintf(buf, len, "%s.%d", typename, |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 434 | bus->parent ? bus->parent->num_child_bus : 0); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 435 | for (i = 0; i < len; i++) |
Christoph Egger | bb87ece | 2009-07-30 15:28:45 +0200 | [diff] [blame] | 436 | buf[i] = qemu_tolower(buf[i]); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 437 | bus->name = buf; |
| 438 | } |
| 439 | |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 440 | if (bus->parent) { |
| 441 | QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling); |
| 442 | bus->parent->num_child_bus++; |
| 443 | object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL); |
Paolo Bonzini | 8185d21 | 2012-05-02 12:06:55 +0200 | [diff] [blame] | 444 | } else if (bus != sysbus_get_default()) { |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 445 | /* TODO: once all bus devices are qdevified, |
| 446 | only reset handler for main_system_bus should be registered here. */ |
| 447 | qemu_register_reset(qbus_reset_all_fn, bus); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 448 | } |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 451 | void qbus_create_inplace(BusState *bus, const char *typename, |
| 452 | DeviceState *parent, const char *name) |
| 453 | { |
| 454 | object_initialize(bus, typename); |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 455 | |
| 456 | bus->parent = parent; |
| 457 | bus->name = name ? g_strdup(name) : NULL; |
| 458 | qbus_realize(bus); |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | BusState *qbus_create(const char *typename, DeviceState *parent, const char *name) |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 462 | { |
| 463 | BusState *bus; |
| 464 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 465 | bus = BUS(object_new(typename)); |
| 466 | bus->qom_allocated = true; |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 467 | |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 468 | bus->parent = parent; |
| 469 | bus->name = name ? g_strdup(name) : NULL; |
| 470 | qbus_realize(bus); |
| 471 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 472 | return bus; |
Isaku Yamahata | 2da8bb9 | 2011-08-02 10:59:13 +0900 | [diff] [blame] | 473 | } |
| 474 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 475 | void qbus_free(BusState *bus) |
| 476 | { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 477 | if (bus->qom_allocated) { |
| 478 | object_delete(OBJECT(bus)); |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 479 | } else { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 480 | object_finalize(OBJECT(bus)); |
| 481 | if (bus->glib_allocated) { |
| 482 | g_free(bus); |
| 483 | } |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 484 | } |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 487 | static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev) |
| 488 | { |
| 489 | BusClass *bc = BUS_GET_CLASS(bus); |
| 490 | |
| 491 | if (bc->get_fw_dev_path) { |
| 492 | return bc->get_fw_dev_path(dev); |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 493 | } |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 494 | |
| 495 | return NULL; |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 498 | static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size) |
| 499 | { |
| 500 | int l = 0; |
| 501 | |
| 502 | if (dev && dev->parent_bus) { |
| 503 | char *d; |
| 504 | l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size); |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 505 | d = bus_get_fw_dev_path(dev->parent_bus, dev); |
| 506 | if (d) { |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 507 | l += snprintf(p + l, size - l, "%s", d); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 508 | g_free(d); |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 509 | } else { |
Anthony Liguori | f79f2bf | 2011-12-04 11:17:51 -0600 | [diff] [blame] | 510 | l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev))); |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | l += snprintf(p + l , size - l, "/"); |
| 514 | |
| 515 | return l; |
| 516 | } |
| 517 | |
| 518 | char* qdev_get_fw_dev_path(DeviceState *dev) |
| 519 | { |
| 520 | char path[128]; |
| 521 | int l; |
| 522 | |
| 523 | l = qdev_get_fw_dev_path_helper(dev, path, 128); |
| 524 | |
| 525 | path[l-1] = '\0'; |
| 526 | |
| 527 | return strdup(path); |
| 528 | } |
Anthony Liguori | 85ed303 | 2011-12-12 14:29:25 -0600 | [diff] [blame] | 529 | |
Anthony Liguori | 09e5ab6 | 2012-02-03 12:28:43 -0600 | [diff] [blame] | 530 | char *qdev_get_dev_path(DeviceState *dev) |
Anthony Liguori | cd34d66 | 2011-12-12 14:29:43 -0600 | [diff] [blame] | 531 | { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 532 | BusClass *bc; |
Anthony Liguori | 09e5ab6 | 2012-02-03 12:28:43 -0600 | [diff] [blame] | 533 | |
| 534 | if (!dev || !dev->parent_bus) { |
| 535 | return NULL; |
| 536 | } |
| 537 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 538 | bc = BUS_GET_CLASS(dev->parent_bus); |
| 539 | if (bc->get_dev_path) { |
| 540 | return bc->get_dev_path(dev); |
Anthony Liguori | 09e5ab6 | 2012-02-03 12:28:43 -0600 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | return NULL; |
Anthony Liguori | 44677de | 2011-12-12 14:29:26 -0600 | [diff] [blame] | 544 | } |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 545 | |
| 546 | /** |
| 547 | * Legacy property handling |
| 548 | */ |
| 549 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 550 | static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque, |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 551 | const char *name, Error **errp) |
| 552 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 553 | DeviceState *dev = DEVICE(obj); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 554 | Property *prop = opaque; |
| 555 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 556 | char buffer[1024]; |
| 557 | char *ptr = buffer; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 558 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 559 | prop->info->print(dev, prop, buffer, sizeof(buffer)); |
| 560 | visit_type_str(v, &ptr, name, errp); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 561 | } |
| 562 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 563 | static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque, |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 564 | const char *name, Error **errp) |
| 565 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 566 | DeviceState *dev = DEVICE(obj); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 567 | Property *prop = opaque; |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 568 | Error *local_err = NULL; |
| 569 | char *ptr = NULL; |
| 570 | int ret; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 571 | |
| 572 | if (dev->state != DEV_STATE_CREATED) { |
| 573 | error_set(errp, QERR_PERMISSION_DENIED); |
| 574 | return; |
| 575 | } |
| 576 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 577 | visit_type_str(v, &ptr, name, &local_err); |
| 578 | if (local_err) { |
| 579 | error_propagate(errp, local_err); |
| 580 | return; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 581 | } |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 582 | |
| 583 | ret = prop->info->parse(dev, prop, ptr); |
Paolo Bonzini | 7db4c4e | 2011-12-18 17:05:07 +0100 | [diff] [blame] | 584 | error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr); |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 585 | g_free(ptr); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /** |
| 589 | * @qdev_add_legacy_property - adds a legacy property |
| 590 | * |
| 591 | * Do not use this is new code! Properties added through this interface will |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 592 | * be given names and types in the "legacy" namespace. |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 593 | * |
Paolo Bonzini | 68ee356 | 2012-02-02 10:17:19 +0100 | [diff] [blame] | 594 | * Legacy properties are string versions of other OOM properties. The format |
| 595 | * of the string depends on the property type. |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 596 | */ |
| 597 | void qdev_property_add_legacy(DeviceState *dev, Property *prop, |
| 598 | Error **errp) |
| 599 | { |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 600 | gchar *name, *type; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 601 | |
Anthony Liguori | f3be016 | 2012-05-02 13:31:07 +0200 | [diff] [blame] | 602 | /* Register pointer properties as legacy properties */ |
| 603 | if (!prop->info->print && !prop->info->parse && |
| 604 | (prop->info->set || prop->info->get)) { |
Paolo Bonzini | 68ee356 | 2012-02-02 10:17:19 +0100 | [diff] [blame] | 605 | return; |
| 606 | } |
Anthony Liguori | f3be016 | 2012-05-02 13:31:07 +0200 | [diff] [blame] | 607 | |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 608 | name = g_strdup_printf("legacy-%s", prop->name); |
Paolo Bonzini | cafe5bd | 2011-12-18 17:05:10 +0100 | [diff] [blame] | 609 | type = g_strdup_printf("legacy<%s>", |
| 610 | prop->info->legacy_name ?: prop->info->name); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 611 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 612 | object_property_add(OBJECT(dev), name, type, |
Paolo Bonzini | 68ee356 | 2012-02-02 10:17:19 +0100 | [diff] [blame] | 613 | prop->info->print ? qdev_get_legacy_property : prop->info->get, |
| 614 | prop->info->parse ? qdev_set_legacy_property : prop->info->set, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 615 | NULL, |
| 616 | prop, errp); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 617 | |
| 618 | g_free(type); |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 619 | g_free(name); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * @qdev_property_add_static - add a @Property to a device. |
| 624 | * |
| 625 | * Static properties access data in a struct. The actual type of the |
| 626 | * property and the field depends on the property type. |
| 627 | */ |
| 628 | void qdev_property_add_static(DeviceState *dev, Property *prop, |
| 629 | Error **errp) |
| 630 | { |
Paolo Bonzini | fdae245 | 2012-04-02 22:40:26 +0200 | [diff] [blame] | 631 | Error *local_err = NULL; |
| 632 | Object *obj = OBJECT(dev); |
| 633 | |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 634 | /* |
| 635 | * TODO qdev_prop_ptr does not have getters or setters. It must |
| 636 | * go now that it can be replaced with links. The test should be |
| 637 | * removed along with it: all static properties are read/write. |
| 638 | */ |
| 639 | if (!prop->info->get && !prop->info->set) { |
| 640 | return; |
| 641 | } |
| 642 | |
Paolo Bonzini | fdae245 | 2012-04-02 22:40:26 +0200 | [diff] [blame] | 643 | object_property_add(obj, prop->name, prop->info->name, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 644 | prop->info->get, prop->info->set, |
Paolo Bonzini | dd0ba25 | 2012-02-02 13:08:48 +0100 | [diff] [blame] | 645 | prop->info->release, |
Paolo Bonzini | fdae245 | 2012-04-02 22:40:26 +0200 | [diff] [blame] | 646 | prop, &local_err); |
| 647 | |
| 648 | if (local_err) { |
| 649 | error_propagate(errp, local_err); |
| 650 | return; |
| 651 | } |
| 652 | if (prop->qtype == QTYPE_NONE) { |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | if (prop->qtype == QTYPE_QBOOL) { |
| 657 | object_property_set_bool(obj, prop->defval, prop->name, &local_err); |
| 658 | } else if (prop->info->enum_table) { |
| 659 | object_property_set_str(obj, prop->info->enum_table[prop->defval], |
| 660 | prop->name, &local_err); |
| 661 | } else if (prop->qtype == QTYPE_QINT) { |
| 662 | object_property_set_int(obj, prop->defval, prop->name, &local_err); |
| 663 | } |
| 664 | assert_no_error(local_err); |
Anthony Liguori | 6a146eb | 2011-12-12 14:29:42 -0600 | [diff] [blame] | 665 | } |
Anthony Liguori | 1de81d2 | 2011-12-19 16:37:46 -0600 | [diff] [blame] | 666 | |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 667 | static void device_initfn(Object *obj) |
| 668 | { |
| 669 | DeviceState *dev = DEVICE(obj); |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 670 | ObjectClass *class; |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 671 | Property *prop; |
| 672 | |
| 673 | if (qdev_hotplug) { |
| 674 | dev->hotplugged = 1; |
| 675 | qdev_hot_added = true; |
| 676 | } |
| 677 | |
| 678 | dev->instance_id_alias = -1; |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 679 | dev->state = DEV_STATE_CREATED; |
| 680 | |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 681 | class = object_get_class(OBJECT(dev)); |
| 682 | do { |
| 683 | for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) { |
| 684 | qdev_property_add_legacy(dev, prop, NULL); |
| 685 | qdev_property_add_static(dev, prop, NULL); |
| 686 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 687 | class = object_class_get_parent(class); |
| 688 | } while (class != object_class_by_name(TYPE_DEVICE)); |
Paolo Bonzini | 4b3582b | 2012-04-03 10:05:07 +0200 | [diff] [blame] | 689 | qdev_prop_set_globals(dev); |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 690 | |
Anthony Liguori | f968fc6 | 2012-05-02 10:39:01 +0200 | [diff] [blame] | 691 | object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS, |
| 692 | (Object **)&dev->parent_bus, NULL); |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 693 | } |
| 694 | |
Anthony Liguori | 60adba3 | 2011-12-23 08:38:56 -0600 | [diff] [blame] | 695 | /* Unlink device from bus and free the structure. */ |
| 696 | static void device_finalize(Object *obj) |
| 697 | { |
| 698 | DeviceState *dev = DEVICE(obj); |
| 699 | BusState *bus; |
Anthony Liguori | 60adba3 | 2011-12-23 08:38:56 -0600 | [diff] [blame] | 700 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 701 | |
| 702 | if (dev->state == DEV_STATE_INITIALIZED) { |
| 703 | while (dev->num_child_bus) { |
| 704 | bus = QLIST_FIRST(&dev->child_bus); |
| 705 | qbus_free(bus); |
| 706 | } |
| 707 | if (qdev_get_vmsd(dev)) { |
| 708 | vmstate_unregister(dev, qdev_get_vmsd(dev), dev); |
| 709 | } |
| 710 | if (dc->exit) { |
| 711 | dc->exit(dev); |
| 712 | } |
| 713 | if (dev->opts) { |
| 714 | qemu_opts_del(dev->opts); |
| 715 | } |
| 716 | } |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 717 | if (dev->parent_bus) { |
| 718 | bus_remove_child(dev->parent_bus, dev); |
| 719 | } |
Anthony Liguori | 60adba3 | 2011-12-23 08:38:56 -0600 | [diff] [blame] | 720 | } |
| 721 | |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 722 | static void device_class_base_init(ObjectClass *class, void *data) |
| 723 | { |
| 724 | DeviceClass *klass = DEVICE_CLASS(class); |
| 725 | |
| 726 | /* We explicitly look up properties in the superclasses, |
| 727 | * so do not propagate them to the subclasses. |
| 728 | */ |
| 729 | klass->props = NULL; |
Anthony Liguori | 1de81d2 | 2011-12-19 16:37:46 -0600 | [diff] [blame] | 730 | } |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 731 | |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 732 | void device_reset(DeviceState *dev) |
| 733 | { |
| 734 | DeviceClass *klass = DEVICE_GET_CLASS(dev); |
| 735 | |
| 736 | if (klass->reset) { |
| 737 | klass->reset(dev); |
| 738 | } |
| 739 | } |
| 740 | |
Paolo Bonzini | f05f6b4 | 2012-03-28 16:34:12 +0200 | [diff] [blame] | 741 | Object *qdev_get_machine(void) |
| 742 | { |
| 743 | static Object *dev; |
| 744 | |
| 745 | if (dev == NULL) { |
Andreas Färber | dfe47e7 | 2012-04-05 13:21:46 +0200 | [diff] [blame] | 746 | dev = container_get(object_get_root(), "/machine"); |
Paolo Bonzini | f05f6b4 | 2012-03-28 16:34:12 +0200 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | return dev; |
| 750 | } |
| 751 | |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 752 | static TypeInfo device_type_info = { |
| 753 | .name = TYPE_DEVICE, |
| 754 | .parent = TYPE_OBJECT, |
| 755 | .instance_size = sizeof(DeviceState), |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 756 | .instance_init = device_initfn, |
Anthony Liguori | 60adba3 | 2011-12-23 08:38:56 -0600 | [diff] [blame] | 757 | .instance_finalize = device_finalize, |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 758 | .class_base_init = device_class_base_init, |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 759 | .abstract = true, |
| 760 | .class_size = sizeof(DeviceClass), |
| 761 | }; |
| 762 | |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 763 | static void qbus_initfn(Object *obj) |
| 764 | { |
| 765 | BusState *bus = BUS(obj); |
| 766 | |
| 767 | QTAILQ_INIT(&bus->children); |
| 768 | } |
| 769 | |
| 770 | static void qbus_finalize(Object *obj) |
| 771 | { |
| 772 | BusState *bus = BUS(obj); |
| 773 | BusChild *kid; |
| 774 | |
| 775 | while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) { |
| 776 | DeviceState *dev = kid->child; |
| 777 | qdev_free(dev); |
| 778 | } |
| 779 | if (bus->parent) { |
| 780 | QLIST_REMOVE(bus, sibling); |
| 781 | bus->parent->num_child_bus--; |
| 782 | } else { |
| 783 | assert(bus != sysbus_get_default()); /* main_system_bus is never freed */ |
| 784 | qemu_unregister_reset(qbus_reset_all_fn, bus); |
| 785 | } |
| 786 | g_free((char *)bus->name); |
| 787 | } |
| 788 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 789 | static const TypeInfo bus_info = { |
| 790 | .name = TYPE_BUS, |
| 791 | .parent = TYPE_OBJECT, |
| 792 | .instance_size = sizeof(BusState), |
| 793 | .abstract = true, |
| 794 | .class_size = sizeof(BusClass), |
Anthony Liguori | ac7d1ba | 2012-02-03 13:32:19 -0600 | [diff] [blame] | 795 | .instance_init = qbus_initfn, |
| 796 | .instance_finalize = qbus_finalize, |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 797 | }; |
| 798 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 799 | static void qdev_register_types(void) |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 800 | { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 801 | type_register_static(&bus_info); |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 802 | type_register_static(&device_type_info); |
| 803 | } |
| 804 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 805 | type_init(qdev_register_types) |