blob: fc79b241035362f5c333cd500f2023ed3604f687 [file] [log] [blame]
Paul Brookaae94602009-05-14 22:35:06 +01001/*
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 Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Paul Brookaae94602009-05-14 22:35:06 +010018 */
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 Brook9d07d752009-05-14 22:35:07 +010028#include "net.h"
Paul Brookaae94602009-05-14 22:35:06 +010029#include "qdev.h"
30#include "sysemu.h"
Luiz Capitulino56f91072012-03-14 17:37:38 -030031#include "error.h"
Paul Brookaae94602009-05-14 22:35:06 +010032
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060033int qdev_hotplug = 0;
Alex Williamson0ac8ef72011-01-04 12:37:50 -070034static bool qdev_hot_added = false;
35static bool qdev_hot_removed = false;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020036
Paul Brookaae94602009-05-14 22:35:06 +010037/* Register a new device type. */
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060038const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
39{
Anthony Liguori6e008582011-12-09 11:06:57 -060040 DeviceClass *dc = DEVICE_GET_CLASS(dev);
41 return dc->vmsd;
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060042}
43
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060044const char *qdev_fw_name(DeviceState *dev)
45{
Anthony Liguori6e008582011-12-09 11:06:57 -060046 DeviceClass *dc = DEVICE_GET_CLASS(dev);
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060047
Anthony Liguori6e008582011-12-09 11:06:57 -060048 if (dc->fw_name) {
49 return dc->fw_name;
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060050 }
51
52 return object_get_typename(OBJECT(dev));
53}
54
Blue Swirla369da52011-09-27 19:15:42 +000055bool qdev_exists(const char *name)
56{
Anthony Liguori212ad112012-02-01 09:34:28 -060057 return !!object_class_by_name(name);
Blue Swirla369da52011-09-27 19:15:42 +000058}
Anthony Liguori40021f02011-12-04 12:22:06 -060059
Paolo Bonzinica2cc782011-12-18 17:05:11 +010060static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
61 Error **errp);
62
Anthony Liguori0866aca2011-12-23 15:34:39 -060063static void bus_remove_child(BusState *bus, DeviceState *child)
Paul Brookaae94602009-05-14 22:35:06 +010064{
Anthony Liguori0866aca2011-12-23 15:34:39 -060065 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
80static void bus_add_child(BusState *bus, DeviceState *child)
81{
82 char name[32];
83 BusChild *kid = g_malloc0(sizeof(*kid));
84
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020085 if (qdev_hotplug) {
86 assert(bus->allow_hotplug);
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020087 }
Anthony Liguoria5296ca2011-12-12 14:29:27 -060088
Anthony Liguori0866aca2011-12-23 15:34:39 -060089 kid->index = bus->max_index++;
90 kid->child = child;
91
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
101void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
102{
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600103 dev->parent_bus = bus;
Anthony Liguori0866aca2011-12-23 15:34:39 -0600104 bus_add_child(bus, dev);
Paul Brookaae94602009-05-14 22:35:06 +0100105}
106
Markus Armbruster0c175422010-02-19 19:12:18 +0100107/* 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. */
110DeviceState *qdev_create(BusState *bus, const char *name)
111{
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000112 DeviceState *dev;
113
114 dev = qdev_try_create(bus, name);
115 if (!dev) {
Peter Maydelle92714c2011-08-03 23:49:04 +0100116 if (bus) {
117 hw_error("Unknown device '%s' for bus '%s'\n", name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200118 object_get_typename(OBJECT(bus)));
Peter Maydelle92714c2011-08-03 23:49:04 +0100119 } else {
120 hw_error("Unknown device '%s' for default sysbus\n", name);
121 }
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000122 }
123
124 return dev;
125}
126
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200127DeviceState *qdev_try_create(BusState *bus, const char *type)
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000128{
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600129 DeviceState *dev;
130
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200131 if (object_class_by_name(type) == NULL) {
Andreas Färber4ed658c2012-02-17 02:47:44 +0100132 return NULL;
133 }
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200134 dev = DEVICE(object_new(type));
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600135 if (!dev) {
136 return NULL;
137 }
138
Markus Armbruster0c175422010-02-19 19:12:18 +0100139 if (!bus) {
Stefan Weil68694892010-12-16 19:33:22 +0100140 bus = sysbus_get_default();
Markus Armbruster0c175422010-02-19 19:12:18 +0100141 }
142
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600143 qdev_set_parent_bus(dev, bus);
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600144
145 return dev;
Markus Armbruster0c175422010-02-19 19:12:18 +0100146}
147
Paul Brookaae94602009-05-14 22:35:06 +0100148/* Initialize a device. Device properties should be set before calling
149 this function. IRQs and MMIO regions should be connected/mapped after
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200150 calling this function.
151 On failure, destroy the device and return negative value.
152 Return 0 on success. */
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200153int qdev_init(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100154{
Anthony Liguori6e008582011-12-09 11:06:57 -0600155 DeviceClass *dc = DEVICE_GET_CLASS(dev);
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200156 int rc;
157
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200158 assert(dev->state == DEV_STATE_CREATED);
Anthony Liguori6e008582011-12-09 11:06:57 -0600159
Anthony Liguorid307af72011-12-09 15:02:56 -0600160 rc = dc->init(dev);
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200161 if (rc < 0) {
162 qdev_free(dev);
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200163 return rc;
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200164 }
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200165
166 if (!OBJECT(dev)->parent) {
167 static int unattached_count = 0;
168 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
169
Andreas Färberdfe47e72012-04-05 13:21:46 +0200170 object_property_add_child(container_get(qdev_get_machine(),
171 "/unattached"),
172 name, OBJECT(dev), NULL);
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200173 g_free(name);
174 }
175
Anthony Liguori6e008582011-12-09 11:06:57 -0600176 if (qdev_get_vmsd(dev)) {
177 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200178 dev->instance_id_alias,
179 dev->alias_required_for_version);
180 }
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200181 dev->state = DEV_STATE_INITIALIZED;
Anthony Liguori94afdad2011-12-04 11:36:01 -0600182 if (dev->hotplugged) {
183 device_reset(dev);
Jan Kiszka5ab28c82011-07-24 19:38:36 +0200184 }
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200185 return 0;
Paul Brook02e2da42009-05-23 00:05:19 +0100186}
187
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200188void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
189 int required_for_version)
190{
191 assert(dev->state == DEV_STATE_CREATED);
192 dev->instance_id_alias = alias_id;
193 dev->alias_required_for_version = required_for_version;
194}
195
Luiz Capitulino56f91072012-03-14 17:37:38 -0300196void qdev_unplug(DeviceState *dev, Error **errp)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200197{
Anthony Liguori6e008582011-12-09 11:06:57 -0600198 DeviceClass *dc = DEVICE_GET_CLASS(dev);
199
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200200 if (!dev->parent_bus->allow_hotplug) {
Luiz Capitulino56f91072012-03-14 17:37:38 -0300201 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
202 return;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200203 }
Anthony Liguori6e008582011-12-09 11:06:57 -0600204 assert(dc->unplug != NULL);
Amit Shah593831d2009-11-02 14:56:41 +0530205
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700206 qdev_hot_removed = true;
207
Luiz Capitulino56f91072012-03-14 17:37:38 -0300208 if (dc->unplug(dev) < 0) {
209 error_set(errp, QERR_UNDEFINED_ERROR);
210 return;
211 }
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200212}
213
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900214static int qdev_reset_one(DeviceState *dev, void *opaque)
215{
Anthony Liguori94afdad2011-12-04 11:36:01 -0600216 device_reset(dev);
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900217
218 return 0;
219}
220
Isaku Yamahatab4694b72010-11-19 18:56:00 +0900221static int qbus_reset_one(BusState *bus, void *opaque)
222{
Anthony Liguori0d936922012-05-02 09:00:20 +0200223 BusClass *bc = BUS_GET_CLASS(bus);
224 if (bc->reset) {
225 return bc->reset(bus);
Isaku Yamahatab4694b72010-11-19 18:56:00 +0900226 }
227 return 0;
228}
229
Isaku Yamahata5af0a042010-11-19 18:56:01 +0900230void qdev_reset_all(DeviceState *dev)
231{
232 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
233}
234
Isaku Yamahata80376c32010-12-20 14:33:35 +0900235void qbus_reset_all_fn(void *opaque)
236{
237 BusState *bus = opaque;
Michael S. Tsirkinf530cce2010-12-20 15:17:10 +0200238 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
Isaku Yamahata80376c32010-12-20 14:33:35 +0900239}
240
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200241/* can be used as ->unplug() callback for the simple cases */
242int qdev_simple_unplug_cb(DeviceState *dev)
243{
244 /* just zap it */
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600245 object_unparent(OBJECT(dev));
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200246 qdev_free(dev);
247 return 0;
248}
249
Michael Tokarev3b29a102011-04-06 17:51:59 +0400250
251/* Like qdev_init(), but terminate program via error_report() instead of
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200252 returning an error value. This is okay during machine creation.
253 Don't use for hotplug, because there callers need to recover from
254 failure. Exception: if you know the device's init() callback can't
255 fail, then qdev_init_nofail() can't fail either, and is therefore
256 usable even then. But relying on the device implementation that
257 way is somewhat unclean, and best avoided. */
258void qdev_init_nofail(DeviceState *dev)
259{
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200260 if (qdev_init(dev) < 0) {
Anthony Liguori6e008582011-12-09 11:06:57 -0600261 error_report("Initialization of device %s failed",
262 object_get_typename(OBJECT(dev)));
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200263 exit(1);
264 }
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200265}
266
Paul Brook02e2da42009-05-23 00:05:19 +0100267/* Unlink device from bus and free the structure. */
268void qdev_free(DeviceState *dev)
269{
Anthony Liguori32fea402011-12-16 14:34:46 -0600270 object_delete(OBJECT(dev));
Paul Brookaae94602009-05-14 22:35:06 +0100271}
272
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200273void qdev_machine_creation_done(void)
274{
275 /*
276 * ok, initial machine setup is done, starting from now we can
277 * only create hotpluggable devices
278 */
279 qdev_hotplug = 1;
280}
281
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700282bool qdev_machine_modified(void)
283{
284 return qdev_hot_added || qdev_hot_removed;
285}
286
Paul Brook02e2da42009-05-23 00:05:19 +0100287BusState *qdev_get_parent_bus(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100288{
Paul Brook02e2da42009-05-23 00:05:19 +0100289 return dev->parent_bus;
Paul Brookaae94602009-05-14 22:35:06 +0100290}
291
Paul Brookaae94602009-05-14 22:35:06 +0100292void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
293{
294 assert(dev->num_gpio_in == 0);
295 dev->num_gpio_in = n;
296 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
297}
298
299void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
300{
301 assert(dev->num_gpio_out == 0);
302 dev->num_gpio_out = n;
303 dev->gpio_out = pins;
304}
305
306qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
307{
308 assert(n >= 0 && n < dev->num_gpio_in);
309 return dev->gpio_in[n];
310}
311
312void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
313{
314 assert(n >= 0 && n < dev->num_gpio_out);
315 dev->gpio_out[n] = pin;
316}
317
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200318void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
319{
Jan Kiszka6eed1852011-07-20 12:20:22 +0200320 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200321 if (nd->vlan)
322 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
323 if (nd->netdev)
324 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
Amit Shah75422b02010-02-25 17:24:43 +0530325 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200326 qdev_prop_exists(dev, "vectors")) {
327 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
328 }
Peter Maydell48e2faf2011-05-20 16:50:01 +0100329 nd->instantiated = 1;
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200330}
331
Paul Brook02e2da42009-05-23 00:05:19 +0100332BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
Paul Brook4d6ae672009-05-14 22:35:06 +0100333{
Paul Brook02e2da42009-05-23 00:05:19 +0100334 BusState *bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100335
Blue Swirl72cf2d42009-09-12 07:36:22 +0000336 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
Paul Brook4d6ae672009-05-14 22:35:06 +0100337 if (strcmp(name, bus->name) == 0) {
Paul Brook02e2da42009-05-23 00:05:19 +0100338 return bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100339 }
340 }
341 return NULL;
342}
343
Anthony Liguori81699d82010-11-19 18:55:58 +0900344int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
345 qbus_walkerfn *busfn, void *opaque)
346{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600347 BusChild *kid;
Anthony Liguori81699d82010-11-19 18:55:58 +0900348 int err;
349
350 if (busfn) {
351 err = busfn(bus, opaque);
352 if (err) {
353 return err;
354 }
355 }
356
Anthony Liguori0866aca2011-12-23 15:34:39 -0600357 QTAILQ_FOREACH(kid, &bus->children, sibling) {
358 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
Anthony Liguori81699d82010-11-19 18:55:58 +0900359 if (err < 0) {
360 return err;
361 }
362 }
363
364 return 0;
365}
366
367int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
368 qbus_walkerfn *busfn, void *opaque)
369{
370 BusState *bus;
371 int err;
372
373 if (devfn) {
374 err = devfn(dev, opaque);
375 if (err) {
376 return err;
377 }
378 }
379
380 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
381 err = qbus_walk_children(bus, devfn, busfn, opaque);
382 if (err < 0) {
383 return err;
384 }
385 }
386
387 return 0;
388}
389
Isaku Yamahataa2ee6b42010-12-24 12:14:12 +0900390DeviceState *qdev_find_recursive(BusState *bus, const char *id)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200391{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600392 BusChild *kid;
393 DeviceState *ret;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200394 BusState *child;
395
Anthony Liguori0866aca2011-12-23 15:34:39 -0600396 QTAILQ_FOREACH(kid, &bus->children, sibling) {
397 DeviceState *dev = kid->child;
398
399 if (dev->id && strcmp(dev->id, id) == 0) {
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200400 return dev;
Anthony Liguori0866aca2011-12-23 15:34:39 -0600401 }
402
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200403 QLIST_FOREACH(child, &dev->child_bus, sibling) {
404 ret = qdev_find_recursive(child, id);
405 if (ret) {
406 return ret;
407 }
408 }
409 }
410 return NULL;
411}
412
Anthony Liguori0d936922012-05-02 09:00:20 +0200413/* FIXME move this logic into instance_init */
414static void do_qbus_create_inplace(BusState *bus, const char *typename,
415 DeviceState *parent, const char *name)
Paul Brook02e2da42009-05-23 00:05:19 +0100416{
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200417 char *buf;
418 int i,len;
Paul Brook02e2da42009-05-23 00:05:19 +0100419
Paul Brook02e2da42009-05-23 00:05:19 +0100420 bus->parent = parent;
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200421
422 if (name) {
423 /* use supplied name */
Anthony Liguori7267c092011-08-20 22:09:37 -0500424 bus->name = g_strdup(name);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200425 } else if (parent && parent->id) {
426 /* parent device has id -> use it for bus name */
427 len = strlen(parent->id) + 16;
Anthony Liguori7267c092011-08-20 22:09:37 -0500428 buf = g_malloc(len);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200429 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
430 bus->name = buf;
431 } else {
432 /* no id -> use lowercase bus type for bus name */
Anthony Liguori0d936922012-05-02 09:00:20 +0200433 len = strlen(typename) + 16;
Anthony Liguori7267c092011-08-20 22:09:37 -0500434 buf = g_malloc(len);
Anthony Liguori0d936922012-05-02 09:00:20 +0200435 len = snprintf(buf, len, "%s.%d", typename,
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200436 parent ? parent->num_child_bus : 0);
437 for (i = 0; i < len; i++)
Christoph Eggerbb87ece2009-07-30 15:28:45 +0200438 buf[i] = qemu_tolower(buf[i]);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200439 bus->name = buf;
440 }
441
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200442 QTAILQ_INIT(&bus->children);
Paul Brook02e2da42009-05-23 00:05:19 +0100443 if (parent) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000444 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200445 parent->num_child_bus++;
Anthony Liguorif968fc62012-05-02 10:39:01 +0200446 object_property_add_child(OBJECT(parent), bus->name, OBJECT(bus), NULL);
Paolo Bonzini8185d212012-05-02 12:06:55 +0200447 } else if (bus != sysbus_get_default()) {
Isaku Yamahata80376c32010-12-20 14:33:35 +0900448 /* TODO: once all bus devices are qdevified,
449 only reset handler for main_system_bus should be registered here. */
450 qemu_register_reset(qbus_reset_all_fn, bus);
Paul Brook02e2da42009-05-23 00:05:19 +0100451 }
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200452}
453
Anthony Liguori0d936922012-05-02 09:00:20 +0200454void qbus_create_inplace(BusState *bus, const char *typename,
455 DeviceState *parent, const char *name)
456{
457 object_initialize(bus, typename);
458 do_qbus_create_inplace(bus, typename, parent, name);
459}
460
461BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200462{
463 BusState *bus;
464
Anthony Liguori0d936922012-05-02 09:00:20 +0200465 bus = BUS(object_new(typename));
466 bus->qom_allocated = true;
467 do_qbus_create_inplace(bus, typename, parent, name);
Paul Brook02e2da42009-05-23 00:05:19 +0100468 return bus;
469}
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100470
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200471void qbus_free(BusState *bus)
472{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600473 BusChild *kid;
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200474
Anthony Liguori0866aca2011-12-23 15:34:39 -0600475 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
476 DeviceState *dev = kid->child;
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200477 qdev_free(dev);
478 }
479 if (bus->parent) {
480 QLIST_REMOVE(bus, sibling);
481 bus->parent->num_child_bus--;
Isaku Yamahata80376c32010-12-20 14:33:35 +0900482 } else {
Paolo Bonzini8185d212012-05-02 12:06:55 +0200483 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
Isaku Yamahata80376c32010-12-20 14:33:35 +0900484 qemu_unregister_reset(qbus_reset_all_fn, bus);
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200485 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500486 g_free((void*)bus->name);
Anthony Liguori0d936922012-05-02 09:00:20 +0200487 if (bus->qom_allocated) {
488 object_delete(OBJECT(bus));
489 } else {
490 object_finalize(OBJECT(bus));
491 if (bus->glib_allocated) {
492 g_free(bus);
493 }
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200494 }
495}
496
Anthony Liguori0d936922012-05-02 09:00:20 +0200497static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
498{
499 BusClass *bc = BUS_GET_CLASS(bus);
500
501 if (bc->get_fw_dev_path) {
502 return bc->get_fw_dev_path(dev);
503 }
504
505 return NULL;
506}
507
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200508static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
509{
510 int l = 0;
511
512 if (dev && dev->parent_bus) {
513 char *d;
514 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
Anthony Liguori0d936922012-05-02 09:00:20 +0200515 d = bus_get_fw_dev_path(dev->parent_bus, dev);
516 if (d) {
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200517 l += snprintf(p + l, size - l, "%s", d);
Anthony Liguori7267c092011-08-20 22:09:37 -0500518 g_free(d);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200519 } else {
Anthony Liguorif79f2bf2011-12-04 11:17:51 -0600520 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200521 }
522 }
523 l += snprintf(p + l , size - l, "/");
524
525 return l;
526}
527
528char* qdev_get_fw_dev_path(DeviceState *dev)
529{
530 char path[128];
531 int l;
532
533 l = qdev_get_fw_dev_path_helper(dev, path, 128);
534
535 path[l-1] = '\0';
536
537 return strdup(path);
538}
Anthony Liguori85ed3032011-12-12 14:29:25 -0600539
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600540char *qdev_get_dev_path(DeviceState *dev)
541{
Anthony Liguori0d936922012-05-02 09:00:20 +0200542 BusClass *bc;
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600543
544 if (!dev || !dev->parent_bus) {
545 return NULL;
546 }
547
Anthony Liguori0d936922012-05-02 09:00:20 +0200548 bc = BUS_GET_CLASS(dev->parent_bus);
549 if (bc->get_dev_path) {
550 return bc->get_dev_path(dev);
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600551 }
552
553 return NULL;
554}
555
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600556/**
557 * Legacy property handling
558 */
559
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600560static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600561 const char *name, Error **errp)
562{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600563 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600564 Property *prop = opaque;
565
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100566 char buffer[1024];
567 char *ptr = buffer;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600568
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100569 prop->info->print(dev, prop, buffer, sizeof(buffer));
570 visit_type_str(v, &ptr, name, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600571}
572
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600573static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600574 const char *name, Error **errp)
575{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600576 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600577 Property *prop = opaque;
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100578 Error *local_err = NULL;
579 char *ptr = NULL;
580 int ret;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600581
582 if (dev->state != DEV_STATE_CREATED) {
583 error_set(errp, QERR_PERMISSION_DENIED);
584 return;
585 }
586
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100587 visit_type_str(v, &ptr, name, &local_err);
588 if (local_err) {
589 error_propagate(errp, local_err);
590 return;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600591 }
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100592
593 ret = prop->info->parse(dev, prop, ptr);
Paolo Bonzini7db4c4e2011-12-18 17:05:07 +0100594 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100595 g_free(ptr);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600596}
597
598/**
599 * @qdev_add_legacy_property - adds a legacy property
600 *
601 * Do not use this is new code! Properties added through this interface will
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100602 * be given names and types in the "legacy" namespace.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600603 *
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100604 * Legacy properties are string versions of other OOM properties. The format
605 * of the string depends on the property type.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600606 */
607void qdev_property_add_legacy(DeviceState *dev, Property *prop,
608 Error **errp)
609{
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100610 gchar *name, *type;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600611
Anthony Liguorif3be0162012-05-02 13:31:07 +0200612 /* Register pointer properties as legacy properties */
613 if (!prop->info->print && !prop->info->parse &&
614 (prop->info->set || prop->info->get)) {
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100615 return;
616 }
Anthony Liguorif3be0162012-05-02 13:31:07 +0200617
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100618 name = g_strdup_printf("legacy-%s", prop->name);
Paolo Bonzinicafe5bd2011-12-18 17:05:10 +0100619 type = g_strdup_printf("legacy<%s>",
620 prop->info->legacy_name ?: prop->info->name);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600621
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600622 object_property_add(OBJECT(dev), name, type,
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100623 prop->info->print ? qdev_get_legacy_property : prop->info->get,
624 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600625 NULL,
626 prop, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600627
628 g_free(type);
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100629 g_free(name);
630}
631
632/**
633 * @qdev_property_add_static - add a @Property to a device.
634 *
635 * Static properties access data in a struct. The actual type of the
636 * property and the field depends on the property type.
637 */
638void qdev_property_add_static(DeviceState *dev, Property *prop,
639 Error **errp)
640{
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200641 Error *local_err = NULL;
642 Object *obj = OBJECT(dev);
643
Paolo Bonzinid8229792012-02-02 09:47:13 +0100644 /*
645 * TODO qdev_prop_ptr does not have getters or setters. It must
646 * go now that it can be replaced with links. The test should be
647 * removed along with it: all static properties are read/write.
648 */
649 if (!prop->info->get && !prop->info->set) {
650 return;
651 }
652
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200653 object_property_add(obj, prop->name, prop->info->name,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600654 prop->info->get, prop->info->set,
Paolo Bonzinidd0ba252012-02-02 13:08:48 +0100655 prop->info->release,
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200656 prop, &local_err);
657
658 if (local_err) {
659 error_propagate(errp, local_err);
660 return;
661 }
662 if (prop->qtype == QTYPE_NONE) {
663 return;
664 }
665
666 if (prop->qtype == QTYPE_QBOOL) {
667 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
668 } else if (prop->info->enum_table) {
669 object_property_set_str(obj, prop->info->enum_table[prop->defval],
670 prop->name, &local_err);
671 } else if (prop->qtype == QTYPE_QINT) {
672 object_property_set_int(obj, prop->defval, prop->name, &local_err);
673 }
674 assert_no_error(local_err);
Anthony Liguori6a146eb2011-12-12 14:29:42 -0600675}
Anthony Liguori1de81d22011-12-19 16:37:46 -0600676
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600677static void device_initfn(Object *obj)
678{
679 DeviceState *dev = DEVICE(obj);
Paolo Bonzinibce54472012-03-28 18:12:47 +0200680 ObjectClass *class;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600681 Property *prop;
682
683 if (qdev_hotplug) {
684 dev->hotplugged = 1;
685 qdev_hot_added = true;
686 }
687
688 dev->instance_id_alias = -1;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600689 dev->state = DEV_STATE_CREATED;
690
Paolo Bonzinibce54472012-03-28 18:12:47 +0200691 class = object_get_class(OBJECT(dev));
692 do {
693 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
694 qdev_property_add_legacy(dev, prop, NULL);
695 qdev_property_add_static(dev, prop, NULL);
696 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200697 class = object_class_get_parent(class);
698 } while (class != object_class_by_name(TYPE_DEVICE));
Paolo Bonzini4b3582b2012-04-03 10:05:07 +0200699 qdev_prop_set_globals(dev);
Anthony Liguorif968fc62012-05-02 10:39:01 +0200700
701 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
702 (Object **)&dev->parent_bus, NULL);
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600703}
704
Anthony Liguori60adba32011-12-23 08:38:56 -0600705/* Unlink device from bus and free the structure. */
706static void device_finalize(Object *obj)
707{
708 DeviceState *dev = DEVICE(obj);
709 BusState *bus;
Anthony Liguori60adba32011-12-23 08:38:56 -0600710 DeviceClass *dc = DEVICE_GET_CLASS(dev);
711
712 if (dev->state == DEV_STATE_INITIALIZED) {
713 while (dev->num_child_bus) {
714 bus = QLIST_FIRST(&dev->child_bus);
715 qbus_free(bus);
716 }
717 if (qdev_get_vmsd(dev)) {
718 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
719 }
720 if (dc->exit) {
721 dc->exit(dev);
722 }
723 if (dev->opts) {
724 qemu_opts_del(dev->opts);
725 }
726 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600727 if (dev->parent_bus) {
728 bus_remove_child(dev->parent_bus, dev);
729 }
Anthony Liguori60adba32011-12-23 08:38:56 -0600730}
731
Paolo Bonzinibce54472012-03-28 18:12:47 +0200732static void device_class_base_init(ObjectClass *class, void *data)
733{
734 DeviceClass *klass = DEVICE_CLASS(class);
735
736 /* We explicitly look up properties in the superclasses,
737 * so do not propagate them to the subclasses.
738 */
739 klass->props = NULL;
740}
741
Anthony Liguori94afdad2011-12-04 11:36:01 -0600742void device_reset(DeviceState *dev)
743{
744 DeviceClass *klass = DEVICE_GET_CLASS(dev);
745
746 if (klass->reset) {
747 klass->reset(dev);
748 }
749}
750
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200751Object *qdev_get_machine(void)
752{
753 static Object *dev;
754
755 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200756 dev = container_get(object_get_root(), "/machine");
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200757 }
758
759 return dev;
760}
761
Anthony Liguori32fea402011-12-16 14:34:46 -0600762static TypeInfo device_type_info = {
763 .name = TYPE_DEVICE,
764 .parent = TYPE_OBJECT,
765 .instance_size = sizeof(DeviceState),
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600766 .instance_init = device_initfn,
Anthony Liguori60adba32011-12-23 08:38:56 -0600767 .instance_finalize = device_finalize,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200768 .class_base_init = device_class_base_init,
Anthony Liguori32fea402011-12-16 14:34:46 -0600769 .abstract = true,
770 .class_size = sizeof(DeviceClass),
771};
772
Anthony Liguori0d936922012-05-02 09:00:20 +0200773static const TypeInfo bus_info = {
774 .name = TYPE_BUS,
775 .parent = TYPE_OBJECT,
776 .instance_size = sizeof(BusState),
777 .abstract = true,
778 .class_size = sizeof(BusClass),
779};
780
Andreas Färber83f7d432012-02-09 15:20:55 +0100781static void qdev_register_types(void)
Anthony Liguori32fea402011-12-16 14:34:46 -0600782{
Anthony Liguori0d936922012-05-02 09:00:20 +0200783 type_register_static(&bus_info);
Anthony Liguori32fea402011-12-16 14:34:46 -0600784 type_register_static(&device_type_info);
785}
786
Andreas Färber83f7d432012-02-09 15:20:55 +0100787type_init(qdev_register_types)