blob: f239902291caa8bff477b74baffa3e6c99fcbb2e [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
Gerd Hoffmanncdaed7c2009-10-06 21:17:52 +020037/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
Blue Swirlb9aaf7f2009-06-09 18:38:51 +000038static BusState *main_system_bus;
Isaku Yamahata2da8bb92011-08-02 10:59:13 +090039static void main_system_bus_create(void);
Paul Brook4d6ae672009-05-14 22:35:06 +010040
Paul Brookaae94602009-05-14 22:35:06 +010041/* Register a new device type. */
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060042const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
43{
Anthony Liguori6e008582011-12-09 11:06:57 -060044 DeviceClass *dc = DEVICE_GET_CLASS(dev);
45 return dc->vmsd;
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060046}
47
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060048const char *qdev_fw_name(DeviceState *dev)
49{
Anthony Liguori6e008582011-12-09 11:06:57 -060050 DeviceClass *dc = DEVICE_GET_CLASS(dev);
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060051
Anthony Liguori6e008582011-12-09 11:06:57 -060052 if (dc->fw_name) {
53 return dc->fw_name;
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060054 }
55
56 return object_get_typename(OBJECT(dev));
57}
58
Blue Swirla369da52011-09-27 19:15:42 +000059bool qdev_exists(const char *name)
60{
Anthony Liguori212ad112012-02-01 09:34:28 -060061 return !!object_class_by_name(name);
Blue Swirla369da52011-09-27 19:15:42 +000062}
Anthony Liguori40021f02011-12-04 12:22:06 -060063
Paolo Bonzinica2cc782011-12-18 17:05:11 +010064static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
65 Error **errp);
66
Anthony Liguori9fbe6122011-12-22 15:14:27 -060067void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
Paul Brookaae94602009-05-14 22:35:06 +010068{
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020069 if (qdev_hotplug) {
70 assert(bus->allow_hotplug);
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020071 }
Anthony Liguoria5296ca2011-12-12 14:29:27 -060072
Anthony Liguori9fbe6122011-12-22 15:14:27 -060073 dev->parent_bus = bus;
Anthony Liguori9674bfe2011-12-22 15:06:37 -060074 QTAILQ_INSERT_HEAD(&bus->children, dev, sibling);
Paul Brookaae94602009-05-14 22:35:06 +010075}
76
Markus Armbruster0c175422010-02-19 19:12:18 +010077/* Create a new device. This only initializes the device state structure
78 and allows properties to be set. qdev_init should be called to
79 initialize the actual device emulation. */
80DeviceState *qdev_create(BusState *bus, const char *name)
81{
Blue Swirl0bcdeda2011-02-05 14:34:25 +000082 DeviceState *dev;
83
84 dev = qdev_try_create(bus, name);
85 if (!dev) {
Peter Maydelle92714c2011-08-03 23:49:04 +010086 if (bus) {
87 hw_error("Unknown device '%s' for bus '%s'\n", name,
88 bus->info->name);
89 } else {
90 hw_error("Unknown device '%s' for default sysbus\n", name);
91 }
Blue Swirl0bcdeda2011-02-05 14:34:25 +000092 }
93
94 return dev;
95}
96
Paolo Bonzinida57feb2012-03-27 18:38:47 +020097DeviceState *qdev_try_create(BusState *bus, const char *type)
Blue Swirl0bcdeda2011-02-05 14:34:25 +000098{
Anthony Liguori9fbe6122011-12-22 15:14:27 -060099 DeviceState *dev;
100
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200101 if (object_class_by_name(type) == NULL) {
Andreas Färber4ed658c2012-02-17 02:47:44 +0100102 return NULL;
103 }
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200104 dev = DEVICE(object_new(type));
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600105 if (!dev) {
106 return NULL;
107 }
108
Markus Armbruster0c175422010-02-19 19:12:18 +0100109 if (!bus) {
Stefan Weil68694892010-12-16 19:33:22 +0100110 bus = sysbus_get_default();
Markus Armbruster0c175422010-02-19 19:12:18 +0100111 }
112
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600113 qdev_set_parent_bus(dev, bus);
114 qdev_prop_set_globals(dev);
115
116 return dev;
Markus Armbruster0c175422010-02-19 19:12:18 +0100117}
118
Paul Brookaae94602009-05-14 22:35:06 +0100119/* Initialize a device. Device properties should be set before calling
120 this function. IRQs and MMIO regions should be connected/mapped after
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200121 calling this function.
122 On failure, destroy the device and return negative value.
123 Return 0 on success. */
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200124int qdev_init(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100125{
Anthony Liguori6e008582011-12-09 11:06:57 -0600126 DeviceClass *dc = DEVICE_GET_CLASS(dev);
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200127 int rc;
128
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200129 assert(dev->state == DEV_STATE_CREATED);
Anthony Liguori6e008582011-12-09 11:06:57 -0600130
Anthony Liguorid307af72011-12-09 15:02:56 -0600131 rc = dc->init(dev);
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200132 if (rc < 0) {
133 qdev_free(dev);
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200134 return rc;
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200135 }
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200136
137 if (!OBJECT(dev)->parent) {
138 static int unattached_count = 0;
139 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
140
Andreas Färberdfe47e72012-04-05 13:21:46 +0200141 object_property_add_child(container_get(qdev_get_machine(),
142 "/unattached"),
143 name, OBJECT(dev), NULL);
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200144 g_free(name);
145 }
146
Anthony Liguori6e008582011-12-09 11:06:57 -0600147 if (qdev_get_vmsd(dev)) {
148 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200149 dev->instance_id_alias,
150 dev->alias_required_for_version);
151 }
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200152 dev->state = DEV_STATE_INITIALIZED;
Anthony Liguori94afdad2011-12-04 11:36:01 -0600153 if (dev->hotplugged) {
154 device_reset(dev);
Jan Kiszka5ab28c82011-07-24 19:38:36 +0200155 }
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200156 return 0;
Paul Brook02e2da42009-05-23 00:05:19 +0100157}
158
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200159void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
160 int required_for_version)
161{
162 assert(dev->state == DEV_STATE_CREATED);
163 dev->instance_id_alias = alias_id;
164 dev->alias_required_for_version = required_for_version;
165}
166
Luiz Capitulino56f91072012-03-14 17:37:38 -0300167void qdev_unplug(DeviceState *dev, Error **errp)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200168{
Anthony Liguori6e008582011-12-09 11:06:57 -0600169 DeviceClass *dc = DEVICE_GET_CLASS(dev);
170
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200171 if (!dev->parent_bus->allow_hotplug) {
Luiz Capitulino56f91072012-03-14 17:37:38 -0300172 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
173 return;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200174 }
Anthony Liguori6e008582011-12-09 11:06:57 -0600175 assert(dc->unplug != NULL);
Amit Shah593831d2009-11-02 14:56:41 +0530176
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700177 qdev_hot_removed = true;
178
Luiz Capitulino56f91072012-03-14 17:37:38 -0300179 if (dc->unplug(dev) < 0) {
180 error_set(errp, QERR_UNDEFINED_ERROR);
181 return;
182 }
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200183}
184
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900185static int qdev_reset_one(DeviceState *dev, void *opaque)
186{
Anthony Liguori94afdad2011-12-04 11:36:01 -0600187 device_reset(dev);
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900188
189 return 0;
190}
191
192BusState *sysbus_get_default(void)
193{
Stefan Weil68694892010-12-16 19:33:22 +0100194 if (!main_system_bus) {
Isaku Yamahata2da8bb92011-08-02 10:59:13 +0900195 main_system_bus_create();
Stefan Weil68694892010-12-16 19:33:22 +0100196 }
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900197 return main_system_bus;
198}
199
Isaku Yamahatab4694b72010-11-19 18:56:00 +0900200static int qbus_reset_one(BusState *bus, void *opaque)
201{
202 if (bus->info->reset) {
203 return bus->info->reset(bus);
204 }
205 return 0;
206}
207
Isaku Yamahata5af0a042010-11-19 18:56:01 +0900208void qdev_reset_all(DeviceState *dev)
209{
210 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
211}
212
Isaku Yamahata80376c32010-12-20 14:33:35 +0900213void qbus_reset_all_fn(void *opaque)
214{
215 BusState *bus = opaque;
Michael S. Tsirkinf530cce2010-12-20 15:17:10 +0200216 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
Isaku Yamahata80376c32010-12-20 14:33:35 +0900217}
218
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200219/* can be used as ->unplug() callback for the simple cases */
220int qdev_simple_unplug_cb(DeviceState *dev)
221{
222 /* just zap it */
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600223 object_unparent(OBJECT(dev));
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200224 qdev_free(dev);
225 return 0;
226}
227
Michael Tokarev3b29a102011-04-06 17:51:59 +0400228
229/* Like qdev_init(), but terminate program via error_report() instead of
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200230 returning an error value. This is okay during machine creation.
231 Don't use for hotplug, because there callers need to recover from
232 failure. Exception: if you know the device's init() callback can't
233 fail, then qdev_init_nofail() can't fail either, and is therefore
234 usable even then. But relying on the device implementation that
235 way is somewhat unclean, and best avoided. */
236void qdev_init_nofail(DeviceState *dev)
237{
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200238 if (qdev_init(dev) < 0) {
Anthony Liguori6e008582011-12-09 11:06:57 -0600239 error_report("Initialization of device %s failed",
240 object_get_typename(OBJECT(dev)));
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200241 exit(1);
242 }
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200243}
244
Paul Brook02e2da42009-05-23 00:05:19 +0100245/* Unlink device from bus and free the structure. */
246void qdev_free(DeviceState *dev)
247{
Anthony Liguori32fea402011-12-16 14:34:46 -0600248 object_delete(OBJECT(dev));
Paul Brookaae94602009-05-14 22:35:06 +0100249}
250
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200251void qdev_machine_creation_done(void)
252{
253 /*
254 * ok, initial machine setup is done, starting from now we can
255 * only create hotpluggable devices
256 */
257 qdev_hotplug = 1;
258}
259
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700260bool qdev_machine_modified(void)
261{
262 return qdev_hot_added || qdev_hot_removed;
263}
264
Paul Brook02e2da42009-05-23 00:05:19 +0100265BusState *qdev_get_parent_bus(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100266{
Paul Brook02e2da42009-05-23 00:05:19 +0100267 return dev->parent_bus;
Paul Brookaae94602009-05-14 22:35:06 +0100268}
269
Paul Brookaae94602009-05-14 22:35:06 +0100270void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
271{
272 assert(dev->num_gpio_in == 0);
273 dev->num_gpio_in = n;
274 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
275}
276
277void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
278{
279 assert(dev->num_gpio_out == 0);
280 dev->num_gpio_out = n;
281 dev->gpio_out = pins;
282}
283
284qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
285{
286 assert(n >= 0 && n < dev->num_gpio_in);
287 return dev->gpio_in[n];
288}
289
290void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
291{
292 assert(n >= 0 && n < dev->num_gpio_out);
293 dev->gpio_out[n] = pin;
294}
295
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200296void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
297{
Jan Kiszka6eed1852011-07-20 12:20:22 +0200298 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200299 if (nd->vlan)
300 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
301 if (nd->netdev)
302 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
Amit Shah75422b02010-02-25 17:24:43 +0530303 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200304 qdev_prop_exists(dev, "vectors")) {
305 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
306 }
Peter Maydell48e2faf2011-05-20 16:50:01 +0100307 nd->instantiated = 1;
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200308}
309
Paul Brook02e2da42009-05-23 00:05:19 +0100310BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
Paul Brook4d6ae672009-05-14 22:35:06 +0100311{
Paul Brook02e2da42009-05-23 00:05:19 +0100312 BusState *bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100313
Blue Swirl72cf2d42009-09-12 07:36:22 +0000314 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
Paul Brook4d6ae672009-05-14 22:35:06 +0100315 if (strcmp(name, bus->name) == 0) {
Paul Brook02e2da42009-05-23 00:05:19 +0100316 return bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100317 }
318 }
319 return NULL;
320}
321
Anthony Liguori81699d82010-11-19 18:55:58 +0900322int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
323 qbus_walkerfn *busfn, void *opaque)
324{
325 DeviceState *dev;
326 int err;
327
328 if (busfn) {
329 err = busfn(bus, opaque);
330 if (err) {
331 return err;
332 }
333 }
334
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200335 QTAILQ_FOREACH(dev, &bus->children, sibling) {
Anthony Liguori81699d82010-11-19 18:55:58 +0900336 err = qdev_walk_children(dev, devfn, busfn, opaque);
337 if (err < 0) {
338 return err;
339 }
340 }
341
342 return 0;
343}
344
345int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
346 qbus_walkerfn *busfn, void *opaque)
347{
348 BusState *bus;
349 int err;
350
351 if (devfn) {
352 err = devfn(dev, opaque);
353 if (err) {
354 return err;
355 }
356 }
357
358 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
359 err = qbus_walk_children(bus, devfn, busfn, opaque);
360 if (err < 0) {
361 return err;
362 }
363 }
364
365 return 0;
366}
367
Isaku Yamahataa2ee6b42010-12-24 12:14:12 +0900368DeviceState *qdev_find_recursive(BusState *bus, const char *id)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200369{
370 DeviceState *dev, *ret;
371 BusState *child;
372
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200373 QTAILQ_FOREACH(dev, &bus->children, sibling) {
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200374 if (dev->id && strcmp(dev->id, id) == 0)
375 return dev;
376 QLIST_FOREACH(child, &dev->child_bus, sibling) {
377 ret = qdev_find_recursive(child, id);
378 if (ret) {
379 return ret;
380 }
381 }
382 }
383 return NULL;
384}
385
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200386void qbus_create_inplace(BusState *bus, BusInfo *info,
387 DeviceState *parent, const char *name)
Paul Brook02e2da42009-05-23 00:05:19 +0100388{
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200389 char *buf;
390 int i,len;
Paul Brook02e2da42009-05-23 00:05:19 +0100391
Gerd Hoffmann10c4c982009-06-30 14:12:08 +0200392 bus->info = info;
Paul Brook02e2da42009-05-23 00:05:19 +0100393 bus->parent = parent;
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200394
395 if (name) {
396 /* use supplied name */
Anthony Liguori7267c092011-08-20 22:09:37 -0500397 bus->name = g_strdup(name);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200398 } else if (parent && parent->id) {
399 /* parent device has id -> use it for bus name */
400 len = strlen(parent->id) + 16;
Anthony Liguori7267c092011-08-20 22:09:37 -0500401 buf = g_malloc(len);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200402 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
403 bus->name = buf;
404 } else {
405 /* no id -> use lowercase bus type for bus name */
406 len = strlen(info->name) + 16;
Anthony Liguori7267c092011-08-20 22:09:37 -0500407 buf = g_malloc(len);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200408 len = snprintf(buf, len, "%s.%d", info->name,
409 parent ? parent->num_child_bus : 0);
410 for (i = 0; i < len; i++)
Christoph Eggerbb87ece2009-07-30 15:28:45 +0200411 buf[i] = qemu_tolower(buf[i]);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200412 bus->name = buf;
413 }
414
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200415 QTAILQ_INIT(&bus->children);
Paul Brook02e2da42009-05-23 00:05:19 +0100416 if (parent) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000417 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200418 parent->num_child_bus++;
Isaku Yamahata80376c32010-12-20 14:33:35 +0900419 } else if (bus != main_system_bus) {
420 /* TODO: once all bus devices are qdevified,
421 only reset handler for main_system_bus should be registered here. */
422 qemu_register_reset(qbus_reset_all_fn, bus);
Paul Brook02e2da42009-05-23 00:05:19 +0100423 }
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200424}
425
426BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
427{
428 BusState *bus;
429
Anthony Liguori7267c092011-08-20 22:09:37 -0500430 bus = g_malloc0(info->size);
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200431 bus->qdev_allocated = 1;
432 qbus_create_inplace(bus, info, parent, name);
Paul Brook02e2da42009-05-23 00:05:19 +0100433 return bus;
434}
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100435
Isaku Yamahata2da8bb92011-08-02 10:59:13 +0900436static void main_system_bus_create(void)
437{
438 /* assign main_system_bus before qbus_create_inplace()
439 * in order to make "if (bus != main_system_bus)" work */
Anthony Liguori7267c092011-08-20 22:09:37 -0500440 main_system_bus = g_malloc0(system_bus_info.size);
Isaku Yamahata2da8bb92011-08-02 10:59:13 +0900441 main_system_bus->qdev_allocated = 1;
442 qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
443 "main-system-bus");
444}
445
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200446void qbus_free(BusState *bus)
447{
448 DeviceState *dev;
449
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200450 while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) {
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200451 qdev_free(dev);
452 }
453 if (bus->parent) {
454 QLIST_REMOVE(bus, sibling);
455 bus->parent->num_child_bus--;
Isaku Yamahata80376c32010-12-20 14:33:35 +0900456 } else {
457 assert(bus != main_system_bus); /* main_system_bus is never freed */
458 qemu_unregister_reset(qbus_reset_all_fn, bus);
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200459 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500460 g_free((void*)bus->name);
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200461 if (bus->qdev_allocated) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500462 g_free(bus);
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200463 }
464}
465
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200466static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
467{
468 int l = 0;
469
470 if (dev && dev->parent_bus) {
471 char *d;
472 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
473 if (dev->parent_bus->info->get_fw_dev_path) {
474 d = dev->parent_bus->info->get_fw_dev_path(dev);
475 l += snprintf(p + l, size - l, "%s", d);
Anthony Liguori7267c092011-08-20 22:09:37 -0500476 g_free(d);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200477 } else {
Anthony Liguorif79f2bf2011-12-04 11:17:51 -0600478 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200479 }
480 }
481 l += snprintf(p + l , size - l, "/");
482
483 return l;
484}
485
486char* qdev_get_fw_dev_path(DeviceState *dev)
487{
488 char path[128];
489 int l;
490
491 l = qdev_get_fw_dev_path_helper(dev, path, 128);
492
493 path[l-1] = '\0';
494
495 return strdup(path);
496}
Anthony Liguori85ed3032011-12-12 14:29:25 -0600497
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600498/**
499 * Legacy property handling
500 */
501
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600502static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600503 const char *name, Error **errp)
504{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600505 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600506 Property *prop = opaque;
507
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100508 char buffer[1024];
509 char *ptr = buffer;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600510
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100511 prop->info->print(dev, prop, buffer, sizeof(buffer));
512 visit_type_str(v, &ptr, name, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600513}
514
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600515static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600516 const char *name, Error **errp)
517{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600518 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600519 Property *prop = opaque;
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100520 Error *local_err = NULL;
521 char *ptr = NULL;
522 int ret;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600523
524 if (dev->state != DEV_STATE_CREATED) {
525 error_set(errp, QERR_PERMISSION_DENIED);
526 return;
527 }
528
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100529 visit_type_str(v, &ptr, name, &local_err);
530 if (local_err) {
531 error_propagate(errp, local_err);
532 return;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600533 }
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100534
535 ret = prop->info->parse(dev, prop, ptr);
Paolo Bonzini7db4c4e2011-12-18 17:05:07 +0100536 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100537 g_free(ptr);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600538}
539
540/**
541 * @qdev_add_legacy_property - adds a legacy property
542 *
543 * Do not use this is new code! Properties added through this interface will
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100544 * be given names and types in the "legacy" namespace.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600545 *
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100546 * Legacy properties are string versions of other OOM properties. The format
547 * of the string depends on the property type.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600548 */
549void qdev_property_add_legacy(DeviceState *dev, Property *prop,
550 Error **errp)
551{
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100552 gchar *name, *type;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600553
Anthony Liguorif3be0162012-05-02 13:31:07 +0200554 /* Register pointer properties as legacy properties */
555 if (!prop->info->print && !prop->info->parse &&
556 (prop->info->set || prop->info->get)) {
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100557 return;
558 }
Anthony Liguorif3be0162012-05-02 13:31:07 +0200559
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100560 name = g_strdup_printf("legacy-%s", prop->name);
Paolo Bonzinicafe5bd2011-12-18 17:05:10 +0100561 type = g_strdup_printf("legacy<%s>",
562 prop->info->legacy_name ?: prop->info->name);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600563
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600564 object_property_add(OBJECT(dev), name, type,
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100565 prop->info->print ? qdev_get_legacy_property : prop->info->get,
566 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600567 NULL,
568 prop, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600569
570 g_free(type);
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100571 g_free(name);
572}
573
574/**
575 * @qdev_property_add_static - add a @Property to a device.
576 *
577 * Static properties access data in a struct. The actual type of the
578 * property and the field depends on the property type.
579 */
580void qdev_property_add_static(DeviceState *dev, Property *prop,
581 Error **errp)
582{
Paolo Bonzinid8229792012-02-02 09:47:13 +0100583 /*
584 * TODO qdev_prop_ptr does not have getters or setters. It must
585 * go now that it can be replaced with links. The test should be
586 * removed along with it: all static properties are read/write.
587 */
588 if (!prop->info->get && !prop->info->set) {
589 return;
590 }
591
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600592 object_property_add(OBJECT(dev), prop->name, prop->info->name,
593 prop->info->get, prop->info->set,
Paolo Bonzinidd0ba252012-02-02 13:08:48 +0100594 prop->info->release,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600595 prop, errp);
Anthony Liguori6a146eb2011-12-12 14:29:42 -0600596}
Anthony Liguori1de81d22011-12-19 16:37:46 -0600597
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600598static void device_initfn(Object *obj)
599{
600 DeviceState *dev = DEVICE(obj);
Paolo Bonzinibce54472012-03-28 18:12:47 +0200601 ObjectClass *class;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600602 Property *prop;
603
604 if (qdev_hotplug) {
605 dev->hotplugged = 1;
606 qdev_hot_added = true;
607 }
608
609 dev->instance_id_alias = -1;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600610 dev->state = DEV_STATE_CREATED;
611
Paolo Bonzinibce54472012-03-28 18:12:47 +0200612 class = object_get_class(OBJECT(dev));
613 do {
614 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
615 qdev_property_add_legacy(dev, prop, NULL);
616 qdev_property_add_static(dev, prop, NULL);
617 }
618 qdev_prop_set_defaults(dev, DEVICE_CLASS(class)->props);
619 class = object_class_get_parent(class);
620 } while (class != object_class_by_name(TYPE_DEVICE));
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600621}
622
Anthony Liguori60adba32011-12-23 08:38:56 -0600623/* Unlink device from bus and free the structure. */
624static void device_finalize(Object *obj)
625{
626 DeviceState *dev = DEVICE(obj);
627 BusState *bus;
Anthony Liguori60adba32011-12-23 08:38:56 -0600628 DeviceClass *dc = DEVICE_GET_CLASS(dev);
629
630 if (dev->state == DEV_STATE_INITIALIZED) {
631 while (dev->num_child_bus) {
632 bus = QLIST_FIRST(&dev->child_bus);
633 qbus_free(bus);
634 }
635 if (qdev_get_vmsd(dev)) {
636 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
637 }
638 if (dc->exit) {
639 dc->exit(dev);
640 }
641 if (dev->opts) {
642 qemu_opts_del(dev->opts);
643 }
644 }
645 QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling);
Anthony Liguori60adba32011-12-23 08:38:56 -0600646}
647
Paolo Bonzinibce54472012-03-28 18:12:47 +0200648static void device_class_base_init(ObjectClass *class, void *data)
649{
650 DeviceClass *klass = DEVICE_CLASS(class);
651
652 /* We explicitly look up properties in the superclasses,
653 * so do not propagate them to the subclasses.
654 */
655 klass->props = NULL;
656}
657
Anthony Liguori94afdad2011-12-04 11:36:01 -0600658void device_reset(DeviceState *dev)
659{
660 DeviceClass *klass = DEVICE_GET_CLASS(dev);
661
662 if (klass->reset) {
663 klass->reset(dev);
664 }
665}
666
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200667Object *qdev_get_machine(void)
668{
669 static Object *dev;
670
671 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200672 dev = container_get(object_get_root(), "/machine");
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200673 }
674
675 return dev;
676}
677
Anthony Liguori32fea402011-12-16 14:34:46 -0600678static TypeInfo device_type_info = {
679 .name = TYPE_DEVICE,
680 .parent = TYPE_OBJECT,
681 .instance_size = sizeof(DeviceState),
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600682 .instance_init = device_initfn,
Anthony Liguori60adba32011-12-23 08:38:56 -0600683 .instance_finalize = device_finalize,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200684 .class_base_init = device_class_base_init,
Anthony Liguori32fea402011-12-16 14:34:46 -0600685 .abstract = true,
686 .class_size = sizeof(DeviceClass),
687};
688
Andreas Färber83f7d432012-02-09 15:20:55 +0100689static void qdev_register_types(void)
Anthony Liguori32fea402011-12-16 14:34:46 -0600690{
691 type_register_static(&device_type_info);
692}
693
Andreas Färber83f7d432012-02-09 15:20:55 +0100694type_init(qdev_register_types)