blob: af419b9c1318e1728824984698f1639faea46554 [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
48BusInfo *qdev_get_bus_info(DeviceState *dev)
49{
Anthony Liguori6e008582011-12-09 11:06:57 -060050 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51 return dc->bus_info;
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060052}
53
54Property *qdev_get_props(DeviceState *dev)
55{
Anthony Liguori6e008582011-12-09 11:06:57 -060056 DeviceClass *dc = DEVICE_GET_CLASS(dev);
57 return dc->props;
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060058}
59
60const char *qdev_fw_name(DeviceState *dev)
61{
Anthony Liguori6e008582011-12-09 11:06:57 -060062 DeviceClass *dc = DEVICE_GET_CLASS(dev);
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060063
Anthony Liguori6e008582011-12-09 11:06:57 -060064 if (dc->fw_name) {
65 return dc->fw_name;
Anthony Liguori4be9f0d2011-12-09 10:51:49 -060066 }
67
68 return object_get_typename(OBJECT(dev));
69}
70
Blue Swirla369da52011-09-27 19:15:42 +000071bool qdev_exists(const char *name)
72{
Anthony Liguori212ad112012-02-01 09:34:28 -060073 return !!object_class_by_name(name);
Blue Swirla369da52011-09-27 19:15:42 +000074}
Anthony Liguori40021f02011-12-04 12:22:06 -060075
Paolo Bonzinica2cc782011-12-18 17:05:11 +010076static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
77 Error **errp);
78
Anthony Liguori9fbe6122011-12-22 15:14:27 -060079void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
Paul Brookaae94602009-05-14 22:35:06 +010080{
Anthony Liguoria5296ca2011-12-12 14:29:27 -060081 Property *prop;
Paul Brookaae94602009-05-14 22:35:06 +010082
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020083 if (qdev_hotplug) {
84 assert(bus->allow_hotplug);
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020085 }
Anthony Liguoria5296ca2011-12-12 14:29:27 -060086
Anthony Liguori9fbe6122011-12-22 15:14:27 -060087 dev->parent_bus = bus;
Anthony Liguori9674bfe2011-12-22 15:06:37 -060088 QTAILQ_INSERT_HEAD(&bus->children, dev, sibling);
Anthony Liguoria5296ca2011-12-12 14:29:27 -060089
Anthony Liguori6e008582011-12-09 11:06:57 -060090 for (prop = qdev_get_bus_info(dev)->props; prop && prop->name; prop++) {
Anthony Liguoria5296ca2011-12-12 14:29:27 -060091 qdev_property_add_legacy(dev, prop, NULL);
Paolo Bonzinica2cc782011-12-18 17:05:11 +010092 qdev_property_add_static(dev, prop, NULL);
Anthony Liguoria5296ca2011-12-12 14:29:27 -060093 }
Paolo Bonzini4f2d3d72012-02-02 09:43:02 +010094 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
Paul Brookaae94602009-05-14 22:35:06 +010095}
96
Markus Armbruster0c175422010-02-19 19:12:18 +010097/* Create a new device. This only initializes the device state structure
98 and allows properties to be set. qdev_init should be called to
99 initialize the actual device emulation. */
100DeviceState *qdev_create(BusState *bus, const char *name)
101{
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000102 DeviceState *dev;
103
104 dev = qdev_try_create(bus, name);
105 if (!dev) {
Peter Maydelle92714c2011-08-03 23:49:04 +0100106 if (bus) {
107 hw_error("Unknown device '%s' for bus '%s'\n", name,
108 bus->info->name);
109 } else {
110 hw_error("Unknown device '%s' for default sysbus\n", name);
111 }
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000112 }
113
114 return dev;
115}
116
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200117DeviceState *qdev_try_create(BusState *bus, const char *type)
Blue Swirl0bcdeda2011-02-05 14:34:25 +0000118{
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600119 DeviceState *dev;
120
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200121 if (object_class_by_name(type) == NULL) {
Andreas Färber4ed658c2012-02-17 02:47:44 +0100122 return NULL;
123 }
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200124 dev = DEVICE(object_new(type));
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600125 if (!dev) {
126 return NULL;
127 }
128
Markus Armbruster0c175422010-02-19 19:12:18 +0100129 if (!bus) {
Stefan Weil68694892010-12-16 19:33:22 +0100130 bus = sysbus_get_default();
Markus Armbruster0c175422010-02-19 19:12:18 +0100131 }
132
Anthony Liguori9fbe6122011-12-22 15:14:27 -0600133 qdev_set_parent_bus(dev, bus);
134 qdev_prop_set_globals(dev);
135
136 return dev;
Markus Armbruster0c175422010-02-19 19:12:18 +0100137}
138
Paul Brookaae94602009-05-14 22:35:06 +0100139/* Initialize a device. Device properties should be set before calling
140 this function. IRQs and MMIO regions should be connected/mapped after
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200141 calling this function.
142 On failure, destroy the device and return negative value.
143 Return 0 on success. */
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200144int qdev_init(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100145{
Anthony Liguori6e008582011-12-09 11:06:57 -0600146 DeviceClass *dc = DEVICE_GET_CLASS(dev);
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200147 int rc;
148
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200149 assert(dev->state == DEV_STATE_CREATED);
Anthony Liguori6e008582011-12-09 11:06:57 -0600150
Anthony Liguorid307af72011-12-09 15:02:56 -0600151 rc = dc->init(dev);
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200152 if (rc < 0) {
Jason Baron266ca112012-05-02 22:42:10 -0400153 object_unparent(OBJECT(dev));
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200154 qdev_free(dev);
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200155 return rc;
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200156 }
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200157
158 if (!OBJECT(dev)->parent) {
159 static int unattached_count = 0;
160 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
161
Andreas Färberdfe47e72012-04-05 13:21:46 +0200162 object_property_add_child(container_get(qdev_get_machine(),
163 "/unattached"),
164 name, OBJECT(dev), NULL);
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200165 g_free(name);
166 }
167
Anthony Liguori6e008582011-12-09 11:06:57 -0600168 if (qdev_get_vmsd(dev)) {
169 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200170 dev->instance_id_alias,
171 dev->alias_required_for_version);
172 }
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200173 dev->state = DEV_STATE_INITIALIZED;
Anthony Liguori94afdad2011-12-04 11:36:01 -0600174 if (dev->hotplugged) {
175 device_reset(dev);
Jan Kiszka5ab28c82011-07-24 19:38:36 +0200176 }
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200177 return 0;
Paul Brook02e2da42009-05-23 00:05:19 +0100178}
179
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200180void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
181 int required_for_version)
182{
183 assert(dev->state == DEV_STATE_CREATED);
184 dev->instance_id_alias = alias_id;
185 dev->alias_required_for_version = required_for_version;
186}
187
Luiz Capitulino56f91072012-03-14 17:37:38 -0300188void qdev_unplug(DeviceState *dev, Error **errp)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200189{
Anthony Liguori6e008582011-12-09 11:06:57 -0600190 DeviceClass *dc = DEVICE_GET_CLASS(dev);
191
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200192 if (!dev->parent_bus->allow_hotplug) {
Luiz Capitulino56f91072012-03-14 17:37:38 -0300193 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
194 return;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200195 }
Anthony Liguori6e008582011-12-09 11:06:57 -0600196 assert(dc->unplug != NULL);
Amit Shah593831d2009-11-02 14:56:41 +0530197
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700198 qdev_hot_removed = true;
199
Luiz Capitulino56f91072012-03-14 17:37:38 -0300200 if (dc->unplug(dev) < 0) {
201 error_set(errp, QERR_UNDEFINED_ERROR);
202 return;
203 }
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200204}
205
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900206static int qdev_reset_one(DeviceState *dev, void *opaque)
207{
Anthony Liguori94afdad2011-12-04 11:36:01 -0600208 device_reset(dev);
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900209
210 return 0;
211}
212
213BusState *sysbus_get_default(void)
214{
Stefan Weil68694892010-12-16 19:33:22 +0100215 if (!main_system_bus) {
Isaku Yamahata2da8bb92011-08-02 10:59:13 +0900216 main_system_bus_create();
Stefan Weil68694892010-12-16 19:33:22 +0100217 }
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900218 return main_system_bus;
219}
220
Isaku Yamahatab4694b72010-11-19 18:56:00 +0900221static int qbus_reset_one(BusState *bus, void *opaque)
222{
223 if (bus->info->reset) {
224 return bus->info->reset(bus);
225 }
226 return 0;
227}
228
Isaku Yamahata5af0a042010-11-19 18:56:01 +0900229void qdev_reset_all(DeviceState *dev)
230{
231 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
232}
233
Isaku Yamahata80376c32010-12-20 14:33:35 +0900234void qbus_reset_all_fn(void *opaque)
235{
236 BusState *bus = opaque;
Michael S. Tsirkinf530cce2010-12-20 15:17:10 +0200237 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
Isaku Yamahata80376c32010-12-20 14:33:35 +0900238}
239
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200240/* can be used as ->unplug() callback for the simple cases */
241int qdev_simple_unplug_cb(DeviceState *dev)
242{
243 /* just zap it */
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600244 object_unparent(OBJECT(dev));
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200245 qdev_free(dev);
246 return 0;
247}
248
Michael Tokarev3b29a102011-04-06 17:51:59 +0400249
250/* Like qdev_init(), but terminate program via error_report() instead of
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200251 returning an error value. This is okay during machine creation.
252 Don't use for hotplug, because there callers need to recover from
253 failure. Exception: if you know the device's init() callback can't
254 fail, then qdev_init_nofail() can't fail either, and is therefore
255 usable even then. But relying on the device implementation that
256 way is somewhat unclean, and best avoided. */
257void qdev_init_nofail(DeviceState *dev)
258{
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200259 if (qdev_init(dev) < 0) {
Anthony Liguori6e008582011-12-09 11:06:57 -0600260 error_report("Initialization of device %s failed",
261 object_get_typename(OBJECT(dev)));
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200262 exit(1);
263 }
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200264}
265
Paul Brook02e2da42009-05-23 00:05:19 +0100266/* Unlink device from bus and free the structure. */
267void qdev_free(DeviceState *dev)
268{
Anthony Liguori32fea402011-12-16 14:34:46 -0600269 object_delete(OBJECT(dev));
Paul Brookaae94602009-05-14 22:35:06 +0100270}
271
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200272void qdev_machine_creation_done(void)
273{
274 /*
275 * ok, initial machine setup is done, starting from now we can
276 * only create hotpluggable devices
277 */
278 qdev_hotplug = 1;
279}
280
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700281bool qdev_machine_modified(void)
282{
283 return qdev_hot_added || qdev_hot_removed;
284}
285
Paul Brook02e2da42009-05-23 00:05:19 +0100286BusState *qdev_get_parent_bus(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100287{
Paul Brook02e2da42009-05-23 00:05:19 +0100288 return dev->parent_bus;
Paul Brookaae94602009-05-14 22:35:06 +0100289}
290
Paul Brookaae94602009-05-14 22:35:06 +0100291void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
292{
293 assert(dev->num_gpio_in == 0);
294 dev->num_gpio_in = n;
295 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
296}
297
298void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
299{
300 assert(dev->num_gpio_out == 0);
301 dev->num_gpio_out = n;
302 dev->gpio_out = pins;
303}
304
305qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
306{
307 assert(n >= 0 && n < dev->num_gpio_in);
308 return dev->gpio_in[n];
309}
310
311void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
312{
313 assert(n >= 0 && n < dev->num_gpio_out);
314 dev->gpio_out[n] = pin;
315}
316
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200317void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
318{
Jan Kiszka6eed1852011-07-20 12:20:22 +0200319 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200320 if (nd->vlan)
321 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
322 if (nd->netdev)
323 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
Amit Shah75422b02010-02-25 17:24:43 +0530324 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200325 qdev_prop_exists(dev, "vectors")) {
326 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
327 }
Peter Maydell48e2faf2011-05-20 16:50:01 +0100328 nd->instantiated = 1;
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200329}
330
Paul Brook02e2da42009-05-23 00:05:19 +0100331BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
Paul Brook4d6ae672009-05-14 22:35:06 +0100332{
Paul Brook02e2da42009-05-23 00:05:19 +0100333 BusState *bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100334
Blue Swirl72cf2d42009-09-12 07:36:22 +0000335 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
Paul Brook4d6ae672009-05-14 22:35:06 +0100336 if (strcmp(name, bus->name) == 0) {
Paul Brook02e2da42009-05-23 00:05:19 +0100337 return bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100338 }
339 }
340 return NULL;
341}
342
Anthony Liguori81699d82010-11-19 18:55:58 +0900343int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
344 qbus_walkerfn *busfn, void *opaque)
345{
346 DeviceState *dev;
347 int err;
348
349 if (busfn) {
350 err = busfn(bus, opaque);
351 if (err) {
352 return err;
353 }
354 }
355
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200356 QTAILQ_FOREACH(dev, &bus->children, sibling) {
Anthony Liguori81699d82010-11-19 18:55:58 +0900357 err = qdev_walk_children(dev, devfn, busfn, opaque);
358 if (err < 0) {
359 return err;
360 }
361 }
362
363 return 0;
364}
365
366int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
367 qbus_walkerfn *busfn, void *opaque)
368{
369 BusState *bus;
370 int err;
371
372 if (devfn) {
373 err = devfn(dev, opaque);
374 if (err) {
375 return err;
376 }
377 }
378
379 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
380 err = qbus_walk_children(bus, devfn, busfn, opaque);
381 if (err < 0) {
382 return err;
383 }
384 }
385
386 return 0;
387}
388
Isaku Yamahataa2ee6b42010-12-24 12:14:12 +0900389DeviceState *qdev_find_recursive(BusState *bus, const char *id)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200390{
391 DeviceState *dev, *ret;
392 BusState *child;
393
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200394 QTAILQ_FOREACH(dev, &bus->children, sibling) {
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200395 if (dev->id && strcmp(dev->id, id) == 0)
396 return dev;
397 QLIST_FOREACH(child, &dev->child_bus, sibling) {
398 ret = qdev_find_recursive(child, id);
399 if (ret) {
400 return ret;
401 }
402 }
403 }
404 return NULL;
405}
406
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200407void qbus_create_inplace(BusState *bus, BusInfo *info,
408 DeviceState *parent, const char *name)
Paul Brook02e2da42009-05-23 00:05:19 +0100409{
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200410 char *buf;
411 int i,len;
Paul Brook02e2da42009-05-23 00:05:19 +0100412
Gerd Hoffmann10c4c982009-06-30 14:12:08 +0200413 bus->info = info;
Paul Brook02e2da42009-05-23 00:05:19 +0100414 bus->parent = parent;
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200415
416 if (name) {
417 /* use supplied name */
Anthony Liguori7267c092011-08-20 22:09:37 -0500418 bus->name = g_strdup(name);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200419 } else if (parent && parent->id) {
420 /* parent device has id -> use it for bus name */
421 len = strlen(parent->id) + 16;
Anthony Liguori7267c092011-08-20 22:09:37 -0500422 buf = g_malloc(len);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200423 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
424 bus->name = buf;
425 } else {
426 /* no id -> use lowercase bus type for bus name */
427 len = strlen(info->name) + 16;
Anthony Liguori7267c092011-08-20 22:09:37 -0500428 buf = g_malloc(len);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200429 len = snprintf(buf, len, "%s.%d", info->name,
430 parent ? parent->num_child_bus : 0);
431 for (i = 0; i < len; i++)
Christoph Eggerbb87ece2009-07-30 15:28:45 +0200432 buf[i] = qemu_tolower(buf[i]);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200433 bus->name = buf;
434 }
435
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200436 QTAILQ_INIT(&bus->children);
Paul Brook02e2da42009-05-23 00:05:19 +0100437 if (parent) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000438 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200439 parent->num_child_bus++;
Isaku Yamahata80376c32010-12-20 14:33:35 +0900440 } else if (bus != main_system_bus) {
441 /* TODO: once all bus devices are qdevified,
442 only reset handler for main_system_bus should be registered here. */
443 qemu_register_reset(qbus_reset_all_fn, bus);
Paul Brook02e2da42009-05-23 00:05:19 +0100444 }
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200445}
446
447BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
448{
449 BusState *bus;
450
Anthony Liguori7267c092011-08-20 22:09:37 -0500451 bus = g_malloc0(info->size);
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200452 bus->qdev_allocated = 1;
453 qbus_create_inplace(bus, info, parent, name);
Paul Brook02e2da42009-05-23 00:05:19 +0100454 return bus;
455}
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100456
Isaku Yamahata2da8bb92011-08-02 10:59:13 +0900457static void main_system_bus_create(void)
458{
459 /* assign main_system_bus before qbus_create_inplace()
460 * in order to make "if (bus != main_system_bus)" work */
Anthony Liguori7267c092011-08-20 22:09:37 -0500461 main_system_bus = g_malloc0(system_bus_info.size);
Isaku Yamahata2da8bb92011-08-02 10:59:13 +0900462 main_system_bus->qdev_allocated = 1;
463 qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
464 "main-system-bus");
465}
466
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200467void qbus_free(BusState *bus)
468{
469 DeviceState *dev;
470
Paolo Bonzinid8bb00d2011-09-14 09:28:06 +0200471 while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) {
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200472 qdev_free(dev);
473 }
474 if (bus->parent) {
475 QLIST_REMOVE(bus, sibling);
476 bus->parent->num_child_bus--;
Isaku Yamahata80376c32010-12-20 14:33:35 +0900477 } else {
478 assert(bus != main_system_bus); /* main_system_bus is never freed */
479 qemu_unregister_reset(qbus_reset_all_fn, bus);
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200480 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500481 g_free((void*)bus->name);
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200482 if (bus->qdev_allocated) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500483 g_free(bus);
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200484 }
485}
486
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200487static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
488{
489 int l = 0;
490
491 if (dev && dev->parent_bus) {
492 char *d;
493 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
494 if (dev->parent_bus->info->get_fw_dev_path) {
495 d = dev->parent_bus->info->get_fw_dev_path(dev);
496 l += snprintf(p + l, size - l, "%s", d);
Anthony Liguori7267c092011-08-20 22:09:37 -0500497 g_free(d);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200498 } else {
Anthony Liguorif79f2bf2011-12-04 11:17:51 -0600499 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200500 }
501 }
502 l += snprintf(p + l , size - l, "/");
503
504 return l;
505}
506
507char* qdev_get_fw_dev_path(DeviceState *dev)
508{
509 char path[128];
510 int l;
511
512 l = qdev_get_fw_dev_path_helper(dev, path, 128);
513
514 path[l-1] = '\0';
515
516 return strdup(path);
517}
Anthony Liguori85ed3032011-12-12 14:29:25 -0600518
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600519static char *qdev_get_type(Object *obj, Error **errp)
Anthony Liguoricd34d662011-12-12 14:29:43 -0600520{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600521 return g_strdup(object_get_typename(obj));
Anthony Liguori44677de2011-12-12 14:29:26 -0600522}
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600523
524/**
525 * Legacy property handling
526 */
527
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600528static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600529 const char *name, Error **errp)
530{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600531 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600532 Property *prop = opaque;
533
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100534 char buffer[1024];
535 char *ptr = buffer;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600536
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100537 prop->info->print(dev, prop, buffer, sizeof(buffer));
538 visit_type_str(v, &ptr, name, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600539}
540
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600541static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600542 const char *name, Error **errp)
543{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600544 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600545 Property *prop = opaque;
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100546 Error *local_err = NULL;
547 char *ptr = NULL;
548 int ret;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600549
550 if (dev->state != DEV_STATE_CREATED) {
551 error_set(errp, QERR_PERMISSION_DENIED);
552 return;
553 }
554
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100555 visit_type_str(v, &ptr, name, &local_err);
556 if (local_err) {
557 error_propagate(errp, local_err);
558 return;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600559 }
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100560
561 ret = prop->info->parse(dev, prop, ptr);
Paolo Bonzini7db4c4e2011-12-18 17:05:07 +0100562 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100563 g_free(ptr);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600564}
565
566/**
567 * @qdev_add_legacy_property - adds a legacy property
568 *
569 * Do not use this is new code! Properties added through this interface will
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100570 * be given names and types in the "legacy" namespace.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600571 *
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100572 * Legacy properties are string versions of other OOM properties. The format
573 * of the string depends on the property type.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600574 */
575void qdev_property_add_legacy(DeviceState *dev, Property *prop,
576 Error **errp)
577{
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100578 gchar *name, *type;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600579
Anthony Liguorif3be0162012-05-02 13:31:07 +0200580 /* Register pointer properties as legacy properties */
581 if (!prop->info->print && !prop->info->parse &&
582 (prop->info->set || prop->info->get)) {
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100583 return;
584 }
Anthony Liguorif3be0162012-05-02 13:31:07 +0200585
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100586 name = g_strdup_printf("legacy-%s", prop->name);
Paolo Bonzinicafe5bd2011-12-18 17:05:10 +0100587 type = g_strdup_printf("legacy<%s>",
588 prop->info->legacy_name ?: prop->info->name);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600589
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600590 object_property_add(OBJECT(dev), name, type,
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100591 prop->info->print ? qdev_get_legacy_property : prop->info->get,
592 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600593 NULL,
594 prop, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600595
596 g_free(type);
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100597 g_free(name);
598}
599
600/**
601 * @qdev_property_add_static - add a @Property to a device.
602 *
603 * Static properties access data in a struct. The actual type of the
604 * property and the field depends on the property type.
605 */
606void qdev_property_add_static(DeviceState *dev, Property *prop,
607 Error **errp)
608{
Paolo Bonzinid8229792012-02-02 09:47:13 +0100609 /*
610 * TODO qdev_prop_ptr does not have getters or setters. It must
611 * go now that it can be replaced with links. The test should be
612 * removed along with it: all static properties are read/write.
613 */
614 if (!prop->info->get && !prop->info->set) {
615 return;
616 }
617
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600618 object_property_add(OBJECT(dev), prop->name, prop->info->name,
619 prop->info->get, prop->info->set,
Paolo Bonzinidd0ba252012-02-02 13:08:48 +0100620 prop->info->release,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600621 prop, errp);
Anthony Liguori6a146eb2011-12-12 14:29:42 -0600622}
Anthony Liguori1de81d22011-12-19 16:37:46 -0600623
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600624static void device_initfn(Object *obj)
625{
626 DeviceState *dev = DEVICE(obj);
627 Property *prop;
628
629 if (qdev_hotplug) {
630 dev->hotplugged = 1;
631 qdev_hot_added = true;
632 }
633
634 dev->instance_id_alias = -1;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600635 dev->state = DEV_STATE_CREATED;
636
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600637 for (prop = qdev_get_props(dev); prop && prop->name; prop++) {
638 qdev_property_add_legacy(dev, prop, NULL);
639 qdev_property_add_static(dev, prop, NULL);
640 }
641
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600642 object_property_add_str(OBJECT(dev), "type", qdev_get_type, NULL, NULL);
Paolo Bonzini4f2d3d72012-02-02 09:43:02 +0100643 qdev_prop_set_defaults(dev, qdev_get_props(dev));
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600644}
645
Anthony Liguori60adba32011-12-23 08:38:56 -0600646/* Unlink device from bus and free the structure. */
647static void device_finalize(Object *obj)
648{
649 DeviceState *dev = DEVICE(obj);
650 BusState *bus;
Anthony Liguori60adba32011-12-23 08:38:56 -0600651 DeviceClass *dc = DEVICE_GET_CLASS(dev);
652
653 if (dev->state == DEV_STATE_INITIALIZED) {
654 while (dev->num_child_bus) {
655 bus = QLIST_FIRST(&dev->child_bus);
656 qbus_free(bus);
657 }
658 if (qdev_get_vmsd(dev)) {
659 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
660 }
661 if (dc->exit) {
662 dc->exit(dev);
663 }
664 if (dev->opts) {
665 qemu_opts_del(dev->opts);
666 }
667 }
668 QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling);
Anthony Liguori60adba32011-12-23 08:38:56 -0600669}
670
Anthony Liguori94afdad2011-12-04 11:36:01 -0600671void device_reset(DeviceState *dev)
672{
673 DeviceClass *klass = DEVICE_GET_CLASS(dev);
674
675 if (klass->reset) {
676 klass->reset(dev);
677 }
678}
679
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200680Object *qdev_get_machine(void)
681{
682 static Object *dev;
683
684 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200685 dev = container_get(object_get_root(), "/machine");
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200686 }
687
688 return dev;
689}
690
Anthony Liguori32fea402011-12-16 14:34:46 -0600691static TypeInfo device_type_info = {
692 .name = TYPE_DEVICE,
693 .parent = TYPE_OBJECT,
694 .instance_size = sizeof(DeviceState),
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600695 .instance_init = device_initfn,
Anthony Liguori60adba32011-12-23 08:38:56 -0600696 .instance_finalize = device_finalize,
Anthony Liguori32fea402011-12-16 14:34:46 -0600697 .abstract = true,
698 .class_size = sizeof(DeviceClass),
699};
700
Andreas Färber83f7d432012-02-09 15:20:55 +0100701static void qdev_register_types(void)
Anthony Liguori32fea402011-12-16 14:34:46 -0600702{
703 type_register_static(&device_type_info);
704}
705
Andreas Färber83f7d432012-02-09 15:20:55 +0100706type_init(qdev_register_types)