blob: a6c4c02947748bc3d091fe8a3bae2a8dcaf653ac [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));
Paul Brookaae94602009-05-14 22:35:06 +010084
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;
Anthony Liguoria5296ca2011-12-12 14:29:27 -060091
Anthony Liguori0866aca2011-12-23 15:34:39 -060092 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 Liguoria5296ca2011-12-12 14:29: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) {
Jason Baron266ca112012-05-02 22:42:10 -0400162 object_unparent(OBJECT(dev));
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200163 qdev_free(dev);
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200164 return rc;
Markus Armbruster18cfeb52009-10-07 01:15:56 +0200165 }
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200166
167 if (!OBJECT(dev)->parent) {
168 static int unattached_count = 0;
169 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
170
Andreas Färberdfe47e72012-04-05 13:21:46 +0200171 object_property_add_child(container_get(qdev_get_machine(),
172 "/unattached"),
173 name, OBJECT(dev), NULL);
Paolo Bonzinida57feb2012-03-27 18:38:47 +0200174 g_free(name);
175 }
176
Anthony Liguori6e008582011-12-09 11:06:57 -0600177 if (qdev_get_vmsd(dev)) {
178 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200179 dev->instance_id_alias,
180 dev->alias_required_for_version);
181 }
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200182 dev->state = DEV_STATE_INITIALIZED;
Anthony Liguori94afdad2011-12-04 11:36:01 -0600183 if (dev->hotplugged) {
184 device_reset(dev);
Jan Kiszka5ab28c82011-07-24 19:38:36 +0200185 }
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200186 return 0;
Paul Brook02e2da42009-05-23 00:05:19 +0100187}
188
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200189void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
190 int required_for_version)
191{
192 assert(dev->state == DEV_STATE_CREATED);
193 dev->instance_id_alias = alias_id;
194 dev->alias_required_for_version = required_for_version;
195}
196
Luiz Capitulino56f91072012-03-14 17:37:38 -0300197void qdev_unplug(DeviceState *dev, Error **errp)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200198{
Anthony Liguori6e008582011-12-09 11:06:57 -0600199 DeviceClass *dc = DEVICE_GET_CLASS(dev);
200
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200201 if (!dev->parent_bus->allow_hotplug) {
Luiz Capitulino56f91072012-03-14 17:37:38 -0300202 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
203 return;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200204 }
Anthony Liguori6e008582011-12-09 11:06:57 -0600205 assert(dc->unplug != NULL);
Amit Shah593831d2009-11-02 14:56:41 +0530206
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700207 qdev_hot_removed = true;
208
Luiz Capitulino56f91072012-03-14 17:37:38 -0300209 if (dc->unplug(dev) < 0) {
210 error_set(errp, QERR_UNDEFINED_ERROR);
211 return;
212 }
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200213}
214
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900215static int qdev_reset_one(DeviceState *dev, void *opaque)
216{
Anthony Liguori94afdad2011-12-04 11:36:01 -0600217 device_reset(dev);
Anthony Liguoriec990eb2010-11-19 18:55:59 +0900218
219 return 0;
220}
221
Isaku Yamahatab4694b72010-11-19 18:56:00 +0900222static int qbus_reset_one(BusState *bus, void *opaque)
223{
Anthony Liguori0d936922012-05-02 09:00:20 +0200224 BusClass *bc = BUS_GET_CLASS(bus);
225 if (bc->reset) {
226 return bc->reset(bus);
Isaku Yamahatab4694b72010-11-19 18:56:00 +0900227 }
228 return 0;
229}
230
Isaku Yamahata5af0a042010-11-19 18:56:01 +0900231void qdev_reset_all(DeviceState *dev)
232{
233 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
234}
235
Isaku Yamahata80376c32010-12-20 14:33:35 +0900236void qbus_reset_all_fn(void *opaque)
237{
238 BusState *bus = opaque;
Michael S. Tsirkinf530cce2010-12-20 15:17:10 +0200239 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
Isaku Yamahata80376c32010-12-20 14:33:35 +0900240}
241
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200242/* can be used as ->unplug() callback for the simple cases */
243int qdev_simple_unplug_cb(DeviceState *dev)
244{
245 /* just zap it */
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600246 object_unparent(OBJECT(dev));
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200247 qdev_free(dev);
248 return 0;
249}
250
Michael Tokarev3b29a102011-04-06 17:51:59 +0400251
252/* Like qdev_init(), but terminate program via error_report() instead of
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200253 returning an error value. This is okay during machine creation.
254 Don't use for hotplug, because there callers need to recover from
255 failure. Exception: if you know the device's init() callback can't
256 fail, then qdev_init_nofail() can't fail either, and is therefore
257 usable even then. But relying on the device implementation that
258 way is somewhat unclean, and best avoided. */
259void qdev_init_nofail(DeviceState *dev)
260{
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200261 if (qdev_init(dev) < 0) {
Anthony Liguori6e008582011-12-09 11:06:57 -0600262 error_report("Initialization of device %s failed",
263 object_get_typename(OBJECT(dev)));
Markus Armbrusterbd6c9a62010-05-27 21:23:08 +0200264 exit(1);
265 }
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200266}
267
Paul Brook02e2da42009-05-23 00:05:19 +0100268/* Unlink device from bus and free the structure. */
269void qdev_free(DeviceState *dev)
270{
Anthony Liguori32fea402011-12-16 14:34:46 -0600271 object_delete(OBJECT(dev));
Paul Brookaae94602009-05-14 22:35:06 +0100272}
273
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200274void qdev_machine_creation_done(void)
275{
276 /*
277 * ok, initial machine setup is done, starting from now we can
278 * only create hotpluggable devices
279 */
280 qdev_hotplug = 1;
281}
282
Alex Williamson0ac8ef72011-01-04 12:37:50 -0700283bool qdev_machine_modified(void)
284{
285 return qdev_hot_added || qdev_hot_removed;
286}
287
Paul Brook02e2da42009-05-23 00:05:19 +0100288BusState *qdev_get_parent_bus(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100289{
Paul Brook02e2da42009-05-23 00:05:19 +0100290 return dev->parent_bus;
Paul Brookaae94602009-05-14 22:35:06 +0100291}
292
Paul Brookaae94602009-05-14 22:35:06 +0100293void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
294{
295 assert(dev->num_gpio_in == 0);
296 dev->num_gpio_in = n;
297 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
298}
299
300void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
301{
302 assert(dev->num_gpio_out == 0);
303 dev->num_gpio_out = n;
304 dev->gpio_out = pins;
305}
306
307qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
308{
309 assert(n >= 0 && n < dev->num_gpio_in);
310 return dev->gpio_in[n];
311}
312
313void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
314{
315 assert(n >= 0 && n < dev->num_gpio_out);
316 dev->gpio_out[n] = pin;
317}
318
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200319void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
320{
Jan Kiszka6eed1852011-07-20 12:20:22 +0200321 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200322 if (nd->vlan)
323 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
324 if (nd->netdev)
325 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
Amit Shah75422b02010-02-25 17:24:43 +0530326 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200327 object_property_find(OBJECT(dev), "vectors", NULL)) {
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200328 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
329 }
Peter Maydell48e2faf2011-05-20 16:50:01 +0100330 nd->instantiated = 1;
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200331}
332
Paul Brook02e2da42009-05-23 00:05:19 +0100333BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
Paul Brook4d6ae672009-05-14 22:35:06 +0100334{
Paul Brook02e2da42009-05-23 00:05:19 +0100335 BusState *bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100336
Blue Swirl72cf2d42009-09-12 07:36:22 +0000337 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
Paul Brook4d6ae672009-05-14 22:35:06 +0100338 if (strcmp(name, bus->name) == 0) {
Paul Brook02e2da42009-05-23 00:05:19 +0100339 return bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100340 }
341 }
342 return NULL;
343}
344
Anthony Liguori81699d82010-11-19 18:55:58 +0900345int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
346 qbus_walkerfn *busfn, void *opaque)
347{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600348 BusChild *kid;
Anthony Liguori81699d82010-11-19 18:55:58 +0900349 int err;
350
351 if (busfn) {
352 err = busfn(bus, opaque);
353 if (err) {
354 return err;
355 }
356 }
357
Anthony Liguori0866aca2011-12-23 15:34:39 -0600358 QTAILQ_FOREACH(kid, &bus->children, sibling) {
359 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
Anthony Liguori81699d82010-11-19 18:55:58 +0900360 if (err < 0) {
361 return err;
362 }
363 }
364
365 return 0;
366}
367
368int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
369 qbus_walkerfn *busfn, void *opaque)
370{
371 BusState *bus;
372 int err;
373
374 if (devfn) {
375 err = devfn(dev, opaque);
376 if (err) {
377 return err;
378 }
379 }
380
381 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
382 err = qbus_walk_children(bus, devfn, busfn, opaque);
383 if (err < 0) {
384 return err;
385 }
386 }
387
388 return 0;
389}
390
Isaku Yamahataa2ee6b42010-12-24 12:14:12 +0900391DeviceState *qdev_find_recursive(BusState *bus, const char *id)
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200392{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600393 BusChild *kid;
394 DeviceState *ret;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200395 BusState *child;
396
Anthony Liguori0866aca2011-12-23 15:34:39 -0600397 QTAILQ_FOREACH(kid, &bus->children, sibling) {
398 DeviceState *dev = kid->child;
399
400 if (dev->id && strcmp(dev->id, id) == 0) {
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200401 return dev;
Anthony Liguori0866aca2011-12-23 15:34:39 -0600402 }
403
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200404 QLIST_FOREACH(child, &dev->child_bus, sibling) {
405 ret = qdev_find_recursive(child, id);
406 if (ret) {
407 return ret;
408 }
409 }
410 }
411 return NULL;
412}
413
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600414static void qbus_realize(BusState *bus)
Paul Brook02e2da42009-05-23 00:05:19 +0100415{
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600416 const char *typename = object_get_typename(OBJECT(bus));
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200417 char *buf;
418 int i,len;
Paul Brook02e2da42009-05-23 00:05:19 +0100419
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600420 if (bus->name) {
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200421 /* use supplied name */
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600422 } else if (bus->parent && bus->parent->id) {
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200423 /* parent device has id -> use it for bus name */
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600424 len = strlen(bus->parent->id) + 16;
Anthony Liguori7267c092011-08-20 22:09:37 -0500425 buf = g_malloc(len);
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600426 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200427 bus->name = buf;
428 } else {
429 /* no id -> use lowercase bus type for bus name */
Anthony Liguori0d936922012-05-02 09:00:20 +0200430 len = strlen(typename) + 16;
Anthony Liguori7267c092011-08-20 22:09:37 -0500431 buf = g_malloc(len);
Anthony Liguori0d936922012-05-02 09:00:20 +0200432 len = snprintf(buf, len, "%s.%d", typename,
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600433 bus->parent ? bus->parent->num_child_bus : 0);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200434 for (i = 0; i < len; i++)
Christoph Eggerbb87ece2009-07-30 15:28:45 +0200435 buf[i] = qemu_tolower(buf[i]);
Gerd Hoffmannd271de92009-07-15 13:59:24 +0200436 bus->name = buf;
437 }
438
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600439 if (bus->parent) {
440 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
441 bus->parent->num_child_bus++;
442 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
Paolo Bonzini8185d212012-05-02 12:06:55 +0200443 } else if (bus != sysbus_get_default()) {
Isaku Yamahata80376c32010-12-20 14:33:35 +0900444 /* TODO: once all bus devices are qdevified,
445 only reset handler for main_system_bus should be registered here. */
446 qemu_register_reset(qbus_reset_all_fn, bus);
Paul Brook02e2da42009-05-23 00:05:19 +0100447 }
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200448}
449
Anthony Liguori0d936922012-05-02 09:00:20 +0200450void qbus_create_inplace(BusState *bus, const char *typename,
451 DeviceState *parent, const char *name)
452{
453 object_initialize(bus, typename);
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600454
455 bus->parent = parent;
456 bus->name = name ? g_strdup(name) : NULL;
457 qbus_realize(bus);
Anthony Liguori0d936922012-05-02 09:00:20 +0200458}
459
460BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200461{
462 BusState *bus;
463
Anthony Liguori0d936922012-05-02 09:00:20 +0200464 bus = BUS(object_new(typename));
465 bus->qom_allocated = true;
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100466
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600467 bus->parent = parent;
468 bus->name = name ? g_strdup(name) : NULL;
469 qbus_realize(bus);
470
Paul Brook02e2da42009-05-23 00:05:19 +0100471 return bus;
Isaku Yamahata2da8bb92011-08-02 10:59:13 +0900472}
473
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200474void qbus_free(BusState *bus)
475{
Anthony Liguori0d936922012-05-02 09:00:20 +0200476 if (bus->qom_allocated) {
477 object_delete(OBJECT(bus));
Isaku Yamahata80376c32010-12-20 14:33:35 +0900478 } else {
Anthony Liguori0d936922012-05-02 09:00:20 +0200479 object_finalize(OBJECT(bus));
480 if (bus->glib_allocated) {
481 g_free(bus);
482 }
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200483 }
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200484}
485
Anthony Liguori0d936922012-05-02 09:00:20 +0200486static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
487{
488 BusClass *bc = BUS_GET_CLASS(bus);
489
490 if (bc->get_fw_dev_path) {
491 return bc->get_fw_dev_path(dev);
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100492 }
Anthony Liguori0d936922012-05-02 09:00:20 +0200493
494 return NULL;
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100495}
496
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200497static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
498{
499 int l = 0;
500
501 if (dev && dev->parent_bus) {
502 char *d;
503 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
Anthony Liguori0d936922012-05-02 09:00:20 +0200504 d = bus_get_fw_dev_path(dev->parent_bus, dev);
505 if (d) {
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200506 l += snprintf(p + l, size - l, "%s", d);
Anthony Liguori7267c092011-08-20 22:09:37 -0500507 g_free(d);
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200508 } else {
Anthony Liguorif79f2bf2011-12-04 11:17:51 -0600509 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200510 }
511 }
512 l += snprintf(p + l , size - l, "/");
513
514 return l;
515}
516
517char* qdev_get_fw_dev_path(DeviceState *dev)
518{
519 char path[128];
520 int l;
521
522 l = qdev_get_fw_dev_path_helper(dev, path, 128);
523
524 path[l-1] = '\0';
525
526 return strdup(path);
527}
Anthony Liguori85ed3032011-12-12 14:29:25 -0600528
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600529char *qdev_get_dev_path(DeviceState *dev)
Anthony Liguoricd34d662011-12-12 14:29:43 -0600530{
Anthony Liguori0d936922012-05-02 09:00:20 +0200531 BusClass *bc;
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600532
533 if (!dev || !dev->parent_bus) {
534 return NULL;
535 }
536
Anthony Liguori0d936922012-05-02 09:00:20 +0200537 bc = BUS_GET_CLASS(dev->parent_bus);
538 if (bc->get_dev_path) {
539 return bc->get_dev_path(dev);
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600540 }
541
542 return NULL;
Anthony Liguori44677de2011-12-12 14:29:26 -0600543}
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600544
545/**
546 * Legacy property handling
547 */
548
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600549static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600550 const char *name, Error **errp)
551{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600552 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600553 Property *prop = opaque;
554
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100555 char buffer[1024];
556 char *ptr = buffer;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600557
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100558 prop->info->print(dev, prop, buffer, sizeof(buffer));
559 visit_type_str(v, &ptr, name, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600560}
561
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600562static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600563 const char *name, Error **errp)
564{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600565 DeviceState *dev = DEVICE(obj);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600566 Property *prop = opaque;
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100567 Error *local_err = NULL;
568 char *ptr = NULL;
569 int ret;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600570
571 if (dev->state != DEV_STATE_CREATED) {
572 error_set(errp, QERR_PERMISSION_DENIED);
573 return;
574 }
575
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100576 visit_type_str(v, &ptr, name, &local_err);
577 if (local_err) {
578 error_propagate(errp, local_err);
579 return;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600580 }
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100581
582 ret = prop->info->parse(dev, prop, ptr);
Paolo Bonzini7db4c4e2011-12-18 17:05:07 +0100583 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
Paolo Bonzinie3cb6ba2011-12-18 17:05:06 +0100584 g_free(ptr);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600585}
586
587/**
588 * @qdev_add_legacy_property - adds a legacy property
589 *
590 * Do not use this is new code! Properties added through this interface will
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100591 * be given names and types in the "legacy" namespace.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600592 *
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100593 * Legacy properties are string versions of other OOM properties. The format
594 * of the string depends on the property type.
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600595 */
596void qdev_property_add_legacy(DeviceState *dev, Property *prop,
597 Error **errp)
598{
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100599 gchar *name, *type;
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600600
Anthony Liguorif3be0162012-05-02 13:31:07 +0200601 /* Register pointer properties as legacy properties */
602 if (!prop->info->print && !prop->info->parse &&
603 (prop->info->set || prop->info->get)) {
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100604 return;
605 }
Anthony Liguorif3be0162012-05-02 13:31:07 +0200606
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100607 name = g_strdup_printf("legacy-%s", prop->name);
Paolo Bonzinicafe5bd2011-12-18 17:05:10 +0100608 type = g_strdup_printf("legacy<%s>",
609 prop->info->legacy_name ?: prop->info->name);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600610
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600611 object_property_add(OBJECT(dev), name, type,
Paolo Bonzini68ee3562012-02-02 10:17:19 +0100612 prop->info->print ? qdev_get_legacy_property : prop->info->get,
613 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600614 NULL,
615 prop, errp);
Anthony Liguoria5296ca2011-12-12 14:29:27 -0600616
617 g_free(type);
Paolo Bonzinica2cc782011-12-18 17:05:11 +0100618 g_free(name);
619}
620
621/**
622 * @qdev_property_add_static - add a @Property to a device.
623 *
624 * Static properties access data in a struct. The actual type of the
625 * property and the field depends on the property type.
626 */
627void qdev_property_add_static(DeviceState *dev, Property *prop,
628 Error **errp)
629{
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200630 Error *local_err = NULL;
631 Object *obj = OBJECT(dev);
632
Paolo Bonzinid8229792012-02-02 09:47:13 +0100633 /*
634 * TODO qdev_prop_ptr does not have getters or setters. It must
635 * go now that it can be replaced with links. The test should be
636 * removed along with it: all static properties are read/write.
637 */
638 if (!prop->info->get && !prop->info->set) {
639 return;
640 }
641
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200642 object_property_add(obj, prop->name, prop->info->name,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600643 prop->info->get, prop->info->set,
Paolo Bonzinidd0ba252012-02-02 13:08:48 +0100644 prop->info->release,
Paolo Bonzinifdae2452012-04-02 22:40:26 +0200645 prop, &local_err);
646
647 if (local_err) {
648 error_propagate(errp, local_err);
649 return;
650 }
651 if (prop->qtype == QTYPE_NONE) {
652 return;
653 }
654
655 if (prop->qtype == QTYPE_QBOOL) {
656 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
657 } else if (prop->info->enum_table) {
658 object_property_set_str(obj, prop->info->enum_table[prop->defval],
659 prop->name, &local_err);
660 } else if (prop->qtype == QTYPE_QINT) {
661 object_property_set_int(obj, prop->defval, prop->name, &local_err);
662 }
663 assert_no_error(local_err);
Anthony Liguori6a146eb2011-12-12 14:29:42 -0600664}
Anthony Liguori1de81d22011-12-19 16:37:46 -0600665
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600666static void device_initfn(Object *obj)
667{
668 DeviceState *dev = DEVICE(obj);
Paolo Bonzinibce54472012-03-28 18:12:47 +0200669 ObjectClass *class;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600670 Property *prop;
671
672 if (qdev_hotplug) {
673 dev->hotplugged = 1;
674 qdev_hot_added = true;
675 }
676
677 dev->instance_id_alias = -1;
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600678 dev->state = DEV_STATE_CREATED;
679
Paolo Bonzinibce54472012-03-28 18:12:47 +0200680 class = object_get_class(OBJECT(dev));
681 do {
682 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
683 qdev_property_add_legacy(dev, prop, NULL);
684 qdev_property_add_static(dev, prop, NULL);
685 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200686 class = object_class_get_parent(class);
687 } while (class != object_class_by_name(TYPE_DEVICE));
Paolo Bonzini4b3582b2012-04-03 10:05:07 +0200688 qdev_prop_set_globals(dev);
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600689
Anthony Liguorif968fc62012-05-02 10:39:01 +0200690 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
691 (Object **)&dev->parent_bus, NULL);
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600692}
693
Anthony Liguori60adba32011-12-23 08:38:56 -0600694/* Unlink device from bus and free the structure. */
695static void device_finalize(Object *obj)
696{
697 DeviceState *dev = DEVICE(obj);
698 BusState *bus;
Anthony Liguori60adba32011-12-23 08:38:56 -0600699 DeviceClass *dc = DEVICE_GET_CLASS(dev);
700
701 if (dev->state == DEV_STATE_INITIALIZED) {
702 while (dev->num_child_bus) {
703 bus = QLIST_FIRST(&dev->child_bus);
704 qbus_free(bus);
705 }
706 if (qdev_get_vmsd(dev)) {
707 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
708 }
709 if (dc->exit) {
710 dc->exit(dev);
711 }
712 if (dev->opts) {
713 qemu_opts_del(dev->opts);
714 }
715 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600716 if (dev->parent_bus) {
717 bus_remove_child(dev->parent_bus, dev);
718 }
Anthony Liguori60adba32011-12-23 08:38:56 -0600719}
720
Paolo Bonzinibce54472012-03-28 18:12:47 +0200721static void device_class_base_init(ObjectClass *class, void *data)
722{
723 DeviceClass *klass = DEVICE_CLASS(class);
724
725 /* We explicitly look up properties in the superclasses,
726 * so do not propagate them to the subclasses.
727 */
728 klass->props = NULL;
Anthony Liguori1de81d22011-12-19 16:37:46 -0600729}
Anthony Liguori32fea402011-12-16 14:34:46 -0600730
Anthony Liguori94afdad2011-12-04 11:36:01 -0600731void device_reset(DeviceState *dev)
732{
733 DeviceClass *klass = DEVICE_GET_CLASS(dev);
734
735 if (klass->reset) {
736 klass->reset(dev);
737 }
738}
739
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200740Object *qdev_get_machine(void)
741{
742 static Object *dev;
743
744 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200745 dev = container_get(object_get_root(), "/machine");
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200746 }
747
748 return dev;
749}
750
Anthony Liguori32fea402011-12-16 14:34:46 -0600751static TypeInfo device_type_info = {
752 .name = TYPE_DEVICE,
753 .parent = TYPE_OBJECT,
754 .instance_size = sizeof(DeviceState),
Anthony Liguori9674bfe2011-12-22 15:06:37 -0600755 .instance_init = device_initfn,
Anthony Liguori60adba32011-12-23 08:38:56 -0600756 .instance_finalize = device_finalize,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200757 .class_base_init = device_class_base_init,
Anthony Liguori32fea402011-12-16 14:34:46 -0600758 .abstract = true,
759 .class_size = sizeof(DeviceClass),
760};
761
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600762static void qbus_initfn(Object *obj)
763{
764 BusState *bus = BUS(obj);
765
766 QTAILQ_INIT(&bus->children);
767}
768
769static void qbus_finalize(Object *obj)
770{
771 BusState *bus = BUS(obj);
772 BusChild *kid;
773
774 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
775 DeviceState *dev = kid->child;
776 qdev_free(dev);
777 }
778 if (bus->parent) {
779 QLIST_REMOVE(bus, sibling);
780 bus->parent->num_child_bus--;
781 } else {
782 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
783 qemu_unregister_reset(qbus_reset_all_fn, bus);
784 }
785 g_free((char *)bus->name);
786}
787
Anthony Liguori0d936922012-05-02 09:00:20 +0200788static const TypeInfo bus_info = {
789 .name = TYPE_BUS,
790 .parent = TYPE_OBJECT,
791 .instance_size = sizeof(BusState),
792 .abstract = true,
793 .class_size = sizeof(BusClass),
Anthony Liguoriac7d1ba2012-02-03 13:32:19 -0600794 .instance_init = qbus_initfn,
795 .instance_finalize = qbus_finalize,
Anthony Liguori0d936922012-05-02 09:00:20 +0200796};
797
Andreas Färber83f7d432012-02-09 15:20:55 +0100798static void qdev_register_types(void)
Anthony Liguori32fea402011-12-16 14:34:46 -0600799{
Anthony Liguori0d936922012-05-02 09:00:20 +0200800 type_register_static(&bus_info);
Anthony Liguori32fea402011-12-16 14:34:46 -0600801 type_register_static(&device_type_info);
802}
803
Andreas Färber83f7d432012-02-09 15:20:55 +0100804type_init(qdev_register_types)