Statistics
| Branch: | Revision:

root / hw / qdev.c @ b1ee5829

History | View | Annotate | Download (22.6 kB)

1 aae9460e Paul Brook
/*
2 aae9460e Paul Brook
 *  Dynamic device configuration and creation.
3 aae9460e Paul Brook
 *
4 aae9460e Paul Brook
 *  Copyright (c) 2009 CodeSourcery
5 aae9460e Paul Brook
 *
6 aae9460e Paul Brook
 * This library is free software; you can redistribute it and/or
7 aae9460e Paul Brook
 * modify it under the terms of the GNU Lesser General Public
8 aae9460e Paul Brook
 * License as published by the Free Software Foundation; either
9 aae9460e Paul Brook
 * version 2 of the License, or (at your option) any later version.
10 aae9460e Paul Brook
 *
11 aae9460e Paul Brook
 * This library is distributed in the hope that it will be useful,
12 aae9460e Paul Brook
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 aae9460e Paul Brook
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 aae9460e Paul Brook
 * Lesser General Public License for more details.
15 aae9460e Paul Brook
 *
16 aae9460e Paul Brook
 * You should have received a copy of the GNU Lesser General Public
17 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 aae9460e Paul Brook
 */
19 aae9460e Paul Brook
20 aae9460e Paul Brook
/* The theory here is that it should be possible to create a machine without
21 aae9460e Paul Brook
   knowledge of specific devices.  Historically board init routines have
22 aae9460e Paul Brook
   passed a bunch of arguments to each device, requiring the board know
23 aae9460e Paul Brook
   exactly which device it is dealing with.  This file provides an abstract
24 aae9460e Paul Brook
   API for device configuration and initialization.  Devices will generally
25 aae9460e Paul Brook
   inherit from a particular bus (e.g. PCI or I2C) rather than
26 aae9460e Paul Brook
   this API directly.  */
27 aae9460e Paul Brook
28 83c9f4ca Paolo Bonzini
#include "hw/qdev.h"
29 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
30 7b1b5d19 Paolo Bonzini
#include "qapi/error.h"
31 b4a42f81 Paolo Bonzini
#include "qapi/qmp/qerror.h"
32 7b1b5d19 Paolo Bonzini
#include "qapi/visitor.h"
33 0402a5d6 Michael S. Tsirkin
#include "qapi/qmp/qjson.h"
34 0402a5d6 Michael S. Tsirkin
#include "monitor/monitor.h"
35 aae9460e Paul Brook
36 ee46d8a5 Anthony Liguori
int qdev_hotplug = 0;
37 0ac8ef71 Alex Williamson
static bool qdev_hot_added = false;
38 0ac8ef71 Alex Williamson
static bool qdev_hot_removed = false;
39 3418bd25 Gerd Hoffmann
40 4be9f0d1 Anthony Liguori
const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
41 4be9f0d1 Anthony Liguori
{
42 6e008585 Anthony Liguori
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
43 6e008585 Anthony Liguori
    return dc->vmsd;
44 4be9f0d1 Anthony Liguori
}
45 4be9f0d1 Anthony Liguori
46 4be9f0d1 Anthony Liguori
const char *qdev_fw_name(DeviceState *dev)
47 4be9f0d1 Anthony Liguori
{
48 6e008585 Anthony Liguori
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
49 4be9f0d1 Anthony Liguori
50 6e008585 Anthony Liguori
    if (dc->fw_name) {
51 6e008585 Anthony Liguori
        return dc->fw_name;
52 4be9f0d1 Anthony Liguori
    }
53 4be9f0d1 Anthony Liguori
54 4be9f0d1 Anthony Liguori
    return object_get_typename(OBJECT(dev));
55 4be9f0d1 Anthony Liguori
}
56 4be9f0d1 Anthony Liguori
57 ca2cc788 Paolo Bonzini
static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
58 ca2cc788 Paolo Bonzini
                                     Error **errp);
59 ca2cc788 Paolo Bonzini
60 0866aca1 Anthony Liguori
static void bus_remove_child(BusState *bus, DeviceState *child)
61 0c17542d Markus Armbruster
{
62 0866aca1 Anthony Liguori
    BusChild *kid;
63 0866aca1 Anthony Liguori
64 0866aca1 Anthony Liguori
    QTAILQ_FOREACH(kid, &bus->children, sibling) {
65 0866aca1 Anthony Liguori
        if (kid->child == child) {
66 0866aca1 Anthony Liguori
            char name[32];
67 0866aca1 Anthony Liguori
68 0866aca1 Anthony Liguori
            snprintf(name, sizeof(name), "child[%d]", kid->index);
69 0866aca1 Anthony Liguori
            QTAILQ_REMOVE(&bus->children, kid, sibling);
70 9d127820 Paolo Bonzini
71 9d127820 Paolo Bonzini
            /* This gives back ownership of kid->child back to us.  */
72 0866aca1 Anthony Liguori
            object_property_del(OBJECT(bus), name, NULL);
73 9d127820 Paolo Bonzini
            object_unref(OBJECT(kid->child));
74 0866aca1 Anthony Liguori
            g_free(kid);
75 0866aca1 Anthony Liguori
            return;
76 0866aca1 Anthony Liguori
        }
77 0866aca1 Anthony Liguori
    }
78 0866aca1 Anthony Liguori
}
79 0866aca1 Anthony Liguori
80 0866aca1 Anthony Liguori
static void bus_add_child(BusState *bus, DeviceState *child)
81 0866aca1 Anthony Liguori
{
82 0866aca1 Anthony Liguori
    char name[32];
83 0866aca1 Anthony Liguori
    BusChild *kid = g_malloc0(sizeof(*kid));
84 0c17542d Markus Armbruster
85 0c17542d Markus Armbruster
    if (qdev_hotplug) {
86 0c17542d Markus Armbruster
        assert(bus->allow_hotplug);
87 0c17542d Markus Armbruster
    }
88 a5296ca9 Anthony Liguori
89 0866aca1 Anthony Liguori
    kid->index = bus->max_index++;
90 0866aca1 Anthony Liguori
    kid->child = child;
91 9d127820 Paolo Bonzini
    object_ref(OBJECT(kid->child));
92 a5296ca9 Anthony Liguori
93 0866aca1 Anthony Liguori
    QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
94 0866aca1 Anthony Liguori
95 9d127820 Paolo Bonzini
    /* This transfers ownership of kid->child to the property.  */
96 0866aca1 Anthony Liguori
    snprintf(name, sizeof(name), "child[%d]", kid->index);
97 0866aca1 Anthony Liguori
    object_property_add_link(OBJECT(bus), name,
98 0866aca1 Anthony Liguori
                             object_get_typename(OBJECT(child)),
99 0866aca1 Anthony Liguori
                             (Object **)&kid->child,
100 0866aca1 Anthony Liguori
                             NULL);
101 0866aca1 Anthony Liguori
}
102 0866aca1 Anthony Liguori
103 0866aca1 Anthony Liguori
void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
104 0866aca1 Anthony Liguori
{
105 9fbe6127 Anthony Liguori
    dev->parent_bus = bus;
106 62d7ba66 Paolo Bonzini
    object_ref(OBJECT(bus));
107 0866aca1 Anthony Liguori
    bus_add_child(bus, dev);
108 0c17542d Markus Armbruster
}
109 0c17542d Markus Armbruster
110 aae9460e Paul Brook
/* Create a new device.  This only initializes the device state structure
111 aae9460e Paul Brook
   and allows properties to be set.  qdev_init should be called to
112 aae9460e Paul Brook
   initialize the actual device emulation.  */
113 02e2da45 Paul Brook
DeviceState *qdev_create(BusState *bus, const char *name)
114 aae9460e Paul Brook
{
115 0bcdeda7 Blue Swirl
    DeviceState *dev;
116 0bcdeda7 Blue Swirl
117 0bcdeda7 Blue Swirl
    dev = qdev_try_create(bus, name);
118 0bcdeda7 Blue Swirl
    if (!dev) {
119 e92714c7 Peter Maydell
        if (bus) {
120 312fd5f2 Markus Armbruster
            error_report("Unknown device '%s' for bus '%s'", name,
121 23e3fbec Eduardo Habkost
                         object_get_typename(OBJECT(bus)));
122 e92714c7 Peter Maydell
        } else {
123 312fd5f2 Markus Armbruster
            error_report("Unknown device '%s' for default sysbus", name);
124 e92714c7 Peter Maydell
        }
125 01ed1d52 liguang
        abort();
126 0bcdeda7 Blue Swirl
    }
127 0bcdeda7 Blue Swirl
128 0bcdeda7 Blue Swirl
    return dev;
129 0bcdeda7 Blue Swirl
}
130 0bcdeda7 Blue Swirl
131 da57febf Paolo Bonzini
DeviceState *qdev_try_create(BusState *bus, const char *type)
132 0bcdeda7 Blue Swirl
{
133 9fbe6127 Anthony Liguori
    DeviceState *dev;
134 9fbe6127 Anthony Liguori
135 da57febf Paolo Bonzini
    if (object_class_by_name(type) == NULL) {
136 4ed658ca Andreas Färber
        return NULL;
137 4ed658ca Andreas Färber
    }
138 da57febf Paolo Bonzini
    dev = DEVICE(object_new(type));
139 9fbe6127 Anthony Liguori
    if (!dev) {
140 9fbe6127 Anthony Liguori
        return NULL;
141 9fbe6127 Anthony Liguori
    }
142 9fbe6127 Anthony Liguori
143 10c4c98a Gerd Hoffmann
    if (!bus) {
144 68694897 Stefan Weil
        bus = sysbus_get_default();
145 10c4c98a Gerd Hoffmann
    }
146 10c4c98a Gerd Hoffmann
147 9fbe6127 Anthony Liguori
    qdev_set_parent_bus(dev, bus);
148 b09995ae Paolo Bonzini
    object_unref(OBJECT(dev));
149 9fbe6127 Anthony Liguori
    return dev;
150 aae9460e Paul Brook
}
151 aae9460e Paul Brook
152 aae9460e Paul Brook
/* Initialize a device.  Device properties should be set before calling
153 aae9460e Paul Brook
   this function.  IRQs and MMIO regions should be connected/mapped after
154 18cfeb52 Markus Armbruster
   calling this function.
155 18cfeb52 Markus Armbruster
   On failure, destroy the device and return negative value.
156 18cfeb52 Markus Armbruster
   Return 0 on success.  */
157 81a322d4 Gerd Hoffmann
int qdev_init(DeviceState *dev)
158 aae9460e Paul Brook
{
159 249d4172 Andreas Färber
    Error *local_err = NULL;
160 959f733a Gerd Hoffmann
161 7983c8a3 Andreas Färber
    assert(!dev->realized);
162 6e008585 Anthony Liguori
163 249d4172 Andreas Färber
    object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
164 249d4172 Andreas Färber
    if (local_err != NULL) {
165 249d4172 Andreas Färber
        error_free(local_err);
166 18cfeb52 Markus Armbruster
        qdev_free(dev);
167 249d4172 Andreas Färber
        return -1;
168 18cfeb52 Markus Armbruster
    }
169 249d4172 Andreas Färber
    return 0;
170 249d4172 Andreas Färber
}
171 da57febf Paolo Bonzini
172 249d4172 Andreas Färber
static void device_realize(DeviceState *dev, Error **err)
173 249d4172 Andreas Färber
{
174 249d4172 Andreas Färber
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
175 da57febf Paolo Bonzini
176 249d4172 Andreas Färber
    if (dc->init) {
177 249d4172 Andreas Färber
        int rc = dc->init(dev);
178 249d4172 Andreas Färber
        if (rc < 0) {
179 249d4172 Andreas Färber
            error_setg(err, "Device initialization failed.");
180 249d4172 Andreas Färber
            return;
181 249d4172 Andreas Färber
        }
182 5ab28c83 Jan Kiszka
    }
183 02e2da45 Paul Brook
}
184 02e2da45 Paul Brook
185 4d2ffa08 Jan Kiszka
void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
186 4d2ffa08 Jan Kiszka
                                 int required_for_version)
187 4d2ffa08 Jan Kiszka
{
188 7983c8a3 Andreas Färber
    assert(!dev->realized);
189 4d2ffa08 Jan Kiszka
    dev->instance_id_alias = alias_id;
190 4d2ffa08 Jan Kiszka
    dev->alias_required_for_version = required_for_version;
191 4d2ffa08 Jan Kiszka
}
192 4d2ffa08 Jan Kiszka
193 56f9107e Luiz Capitulino
void qdev_unplug(DeviceState *dev, Error **errp)
194 3418bd25 Gerd Hoffmann
{
195 6e008585 Anthony Liguori
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
196 6e008585 Anthony Liguori
197 3418bd25 Gerd Hoffmann
    if (!dev->parent_bus->allow_hotplug) {
198 56f9107e Luiz Capitulino
        error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
199 56f9107e Luiz Capitulino
        return;
200 3418bd25 Gerd Hoffmann
    }
201 6e008585 Anthony Liguori
    assert(dc->unplug != NULL);
202 593831de Amit Shah
203 0ac8ef71 Alex Williamson
    qdev_hot_removed = true;
204 0ac8ef71 Alex Williamson
205 56f9107e Luiz Capitulino
    if (dc->unplug(dev) < 0) {
206 56f9107e Luiz Capitulino
        error_set(errp, QERR_UNDEFINED_ERROR);
207 56f9107e Luiz Capitulino
        return;
208 56f9107e Luiz Capitulino
    }
209 3418bd25 Gerd Hoffmann
}
210 3418bd25 Gerd Hoffmann
211 ec990eb6 Anthony Liguori
static int qdev_reset_one(DeviceState *dev, void *opaque)
212 ec990eb6 Anthony Liguori
{
213 94afdadc Anthony Liguori
    device_reset(dev);
214 ec990eb6 Anthony Liguori
215 ec990eb6 Anthony Liguori
    return 0;
216 ec990eb6 Anthony Liguori
}
217 ec990eb6 Anthony Liguori
218 b4694b7c Isaku Yamahata
static int qbus_reset_one(BusState *bus, void *opaque)
219 b4694b7c Isaku Yamahata
{
220 0d936928 Anthony Liguori
    BusClass *bc = BUS_GET_CLASS(bus);
221 0d936928 Anthony Liguori
    if (bc->reset) {
222 0d936928 Anthony Liguori
        return bc->reset(bus);
223 b4694b7c Isaku Yamahata
    }
224 b4694b7c Isaku Yamahata
    return 0;
225 b4694b7c Isaku Yamahata
}
226 b4694b7c Isaku Yamahata
227 5af0a04b Isaku Yamahata
void qdev_reset_all(DeviceState *dev)
228 5af0a04b Isaku Yamahata
{
229 5af0a04b Isaku Yamahata
    qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
230 5af0a04b Isaku Yamahata
}
231 5af0a04b Isaku Yamahata
232 d0508c36 Paolo Bonzini
void qbus_reset_all(BusState *bus)
233 d0508c36 Paolo Bonzini
{
234 d0508c36 Paolo Bonzini
    qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
235 d0508c36 Paolo Bonzini
}
236 d0508c36 Paolo Bonzini
237 80376c3f Isaku Yamahata
void qbus_reset_all_fn(void *opaque)
238 80376c3f Isaku Yamahata
{
239 80376c3f Isaku Yamahata
    BusState *bus = opaque;
240 d0508c36 Paolo Bonzini
    qbus_reset_all(bus);
241 80376c3f Isaku Yamahata
}
242 80376c3f Isaku Yamahata
243 3418bd25 Gerd Hoffmann
/* can be used as ->unplug() callback for the simple cases */
244 3418bd25 Gerd Hoffmann
int qdev_simple_unplug_cb(DeviceState *dev)
245 3418bd25 Gerd Hoffmann
{
246 3418bd25 Gerd Hoffmann
    /* just zap it */
247 3418bd25 Gerd Hoffmann
    qdev_free(dev);
248 3418bd25 Gerd Hoffmann
    return 0;
249 3418bd25 Gerd Hoffmann
}
250 3418bd25 Gerd Hoffmann
251 3b29a101 Michael Tokarev
252 3b29a101 Michael Tokarev
/* Like qdev_init(), but terminate program via error_report() instead of
253 e23a1b33 Markus Armbruster
   returning an error value.  This is okay during machine creation.
254 e23a1b33 Markus Armbruster
   Don't use for hotplug, because there callers need to recover from
255 e23a1b33 Markus Armbruster
   failure.  Exception: if you know the device's init() callback can't
256 e23a1b33 Markus Armbruster
   fail, then qdev_init_nofail() can't fail either, and is therefore
257 e23a1b33 Markus Armbruster
   usable even then.  But relying on the device implementation that
258 e23a1b33 Markus Armbruster
   way is somewhat unclean, and best avoided.  */
259 e23a1b33 Markus Armbruster
void qdev_init_nofail(DeviceState *dev)
260 e23a1b33 Markus Armbruster
{
261 7de3abe5 Anthony Liguori
    const char *typename = object_get_typename(OBJECT(dev));
262 7de3abe5 Anthony Liguori
263 bd6c9a61 Markus Armbruster
    if (qdev_init(dev) < 0) {
264 7de3abe5 Anthony Liguori
        error_report("Initialization of device %s failed", typename);
265 bd6c9a61 Markus Armbruster
        exit(1);
266 bd6c9a61 Markus Armbruster
    }
267 e23a1b33 Markus Armbruster
}
268 e23a1b33 Markus Armbruster
269 02e2da45 Paul Brook
/* Unlink device from bus and free the structure.  */
270 02e2da45 Paul Brook
void qdev_free(DeviceState *dev)
271 02e2da45 Paul Brook
{
272 dc7389b7 Paolo Bonzini
    object_unparent(OBJECT(dev));
273 aae9460e Paul Brook
}
274 aae9460e Paul Brook
275 3418bd25 Gerd Hoffmann
void qdev_machine_creation_done(void)
276 3418bd25 Gerd Hoffmann
{
277 3418bd25 Gerd Hoffmann
    /*
278 3418bd25 Gerd Hoffmann
     * ok, initial machine setup is done, starting from now we can
279 3418bd25 Gerd Hoffmann
     * only create hotpluggable devices
280 3418bd25 Gerd Hoffmann
     */
281 3418bd25 Gerd Hoffmann
    qdev_hotplug = 1;
282 3418bd25 Gerd Hoffmann
}
283 3418bd25 Gerd Hoffmann
284 0ac8ef71 Alex Williamson
bool qdev_machine_modified(void)
285 0ac8ef71 Alex Williamson
{
286 0ac8ef71 Alex Williamson
    return qdev_hot_added || qdev_hot_removed;
287 0ac8ef71 Alex Williamson
}
288 0ac8ef71 Alex Williamson
289 02e2da45 Paul Brook
BusState *qdev_get_parent_bus(DeviceState *dev)
290 aae9460e Paul Brook
{
291 02e2da45 Paul Brook
    return dev->parent_bus;
292 aae9460e Paul Brook
}
293 aae9460e Paul Brook
294 aae9460e Paul Brook
void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
295 aae9460e Paul Brook
{
296 1e5b31e6 Peter A. G. Crosthwaite
    dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
297 1e5b31e6 Peter A. G. Crosthwaite
                                        dev, n);
298 1e5b31e6 Peter A. G. Crosthwaite
    dev->num_gpio_in += n;
299 aae9460e Paul Brook
}
300 aae9460e Paul Brook
301 aae9460e Paul Brook
void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
302 aae9460e Paul Brook
{
303 aae9460e Paul Brook
    assert(dev->num_gpio_out == 0);
304 aae9460e Paul Brook
    dev->num_gpio_out = n;
305 aae9460e Paul Brook
    dev->gpio_out = pins;
306 aae9460e Paul Brook
}
307 aae9460e Paul Brook
308 aae9460e Paul Brook
qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
309 aae9460e Paul Brook
{
310 aae9460e Paul Brook
    assert(n >= 0 && n < dev->num_gpio_in);
311 aae9460e Paul Brook
    return dev->gpio_in[n];
312 aae9460e Paul Brook
}
313 aae9460e Paul Brook
314 aae9460e Paul Brook
void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
315 aae9460e Paul Brook
{
316 aae9460e Paul Brook
    assert(n >= 0 && n < dev->num_gpio_out);
317 aae9460e Paul Brook
    dev->gpio_out[n] = pin;
318 aae9460e Paul Brook
}
319 aae9460e Paul Brook
320 02e2da45 Paul Brook
BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
321 4d6ae674 Paul Brook
{
322 02e2da45 Paul Brook
    BusState *bus;
323 4d6ae674 Paul Brook
324 72cf2d4f Blue Swirl
    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
325 4d6ae674 Paul Brook
        if (strcmp(name, bus->name) == 0) {
326 02e2da45 Paul Brook
            return bus;
327 4d6ae674 Paul Brook
        }
328 4d6ae674 Paul Brook
    }
329 4d6ae674 Paul Brook
    return NULL;
330 4d6ae674 Paul Brook
}
331 4d6ae674 Paul Brook
332 81699d8a Anthony Liguori
int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
333 81699d8a Anthony Liguori
                       qbus_walkerfn *busfn, void *opaque)
334 81699d8a Anthony Liguori
{
335 0866aca1 Anthony Liguori
    BusChild *kid;
336 81699d8a Anthony Liguori
    int err;
337 81699d8a Anthony Liguori
338 81699d8a Anthony Liguori
    if (busfn) {
339 81699d8a Anthony Liguori
        err = busfn(bus, opaque);
340 81699d8a Anthony Liguori
        if (err) {
341 81699d8a Anthony Liguori
            return err;
342 81699d8a Anthony Liguori
        }
343 81699d8a Anthony Liguori
    }
344 81699d8a Anthony Liguori
345 0866aca1 Anthony Liguori
    QTAILQ_FOREACH(kid, &bus->children, sibling) {
346 0866aca1 Anthony Liguori
        err = qdev_walk_children(kid->child, devfn, busfn, opaque);
347 81699d8a Anthony Liguori
        if (err < 0) {
348 81699d8a Anthony Liguori
            return err;
349 81699d8a Anthony Liguori
        }
350 81699d8a Anthony Liguori
    }
351 81699d8a Anthony Liguori
352 81699d8a Anthony Liguori
    return 0;
353 81699d8a Anthony Liguori
}
354 81699d8a Anthony Liguori
355 81699d8a Anthony Liguori
int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
356 81699d8a Anthony Liguori
                       qbus_walkerfn *busfn, void *opaque)
357 81699d8a Anthony Liguori
{
358 81699d8a Anthony Liguori
    BusState *bus;
359 81699d8a Anthony Liguori
    int err;
360 81699d8a Anthony Liguori
361 81699d8a Anthony Liguori
    if (devfn) {
362 81699d8a Anthony Liguori
        err = devfn(dev, opaque);
363 81699d8a Anthony Liguori
        if (err) {
364 81699d8a Anthony Liguori
            return err;
365 81699d8a Anthony Liguori
        }
366 81699d8a Anthony Liguori
    }
367 81699d8a Anthony Liguori
368 81699d8a Anthony Liguori
    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
369 81699d8a Anthony Liguori
        err = qbus_walk_children(bus, devfn, busfn, opaque);
370 81699d8a Anthony Liguori
        if (err < 0) {
371 81699d8a Anthony Liguori
            return err;
372 81699d8a Anthony Liguori
        }
373 81699d8a Anthony Liguori
    }
374 81699d8a Anthony Liguori
375 81699d8a Anthony Liguori
    return 0;
376 81699d8a Anthony Liguori
}
377 81699d8a Anthony Liguori
378 a2ee6b4f Isaku Yamahata
DeviceState *qdev_find_recursive(BusState *bus, const char *id)
379 3418bd25 Gerd Hoffmann
{
380 0866aca1 Anthony Liguori
    BusChild *kid;
381 0866aca1 Anthony Liguori
    DeviceState *ret;
382 3418bd25 Gerd Hoffmann
    BusState *child;
383 3418bd25 Gerd Hoffmann
384 0866aca1 Anthony Liguori
    QTAILQ_FOREACH(kid, &bus->children, sibling) {
385 0866aca1 Anthony Liguori
        DeviceState *dev = kid->child;
386 0866aca1 Anthony Liguori
387 0866aca1 Anthony Liguori
        if (dev->id && strcmp(dev->id, id) == 0) {
388 3418bd25 Gerd Hoffmann
            return dev;
389 0866aca1 Anthony Liguori
        }
390 0866aca1 Anthony Liguori
391 3418bd25 Gerd Hoffmann
        QLIST_FOREACH(child, &dev->child_bus, sibling) {
392 3418bd25 Gerd Hoffmann
            ret = qdev_find_recursive(child, id);
393 3418bd25 Gerd Hoffmann
            if (ret) {
394 3418bd25 Gerd Hoffmann
                return ret;
395 3418bd25 Gerd Hoffmann
            }
396 3418bd25 Gerd Hoffmann
        }
397 3418bd25 Gerd Hoffmann
    }
398 3418bd25 Gerd Hoffmann
    return NULL;
399 3418bd25 Gerd Hoffmann
}
400 3418bd25 Gerd Hoffmann
401 013e1182 Paolo Bonzini
static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
402 02e2da45 Paul Brook
{
403 ac7d1ba6 Anthony Liguori
    const char *typename = object_get_typename(OBJECT(bus));
404 d271de9f Gerd Hoffmann
    char *buf;
405 d271de9f Gerd Hoffmann
    int i,len;
406 02e2da45 Paul Brook
407 013e1182 Paolo Bonzini
    bus->parent = parent;
408 013e1182 Paolo Bonzini
409 013e1182 Paolo Bonzini
    if (name) {
410 013e1182 Paolo Bonzini
        bus->name = g_strdup(name);
411 ac7d1ba6 Anthony Liguori
    } else if (bus->parent && bus->parent->id) {
412 d271de9f Gerd Hoffmann
        /* parent device has id -> use it for bus name */
413 ac7d1ba6 Anthony Liguori
        len = strlen(bus->parent->id) + 16;
414 7267c094 Anthony Liguori
        buf = g_malloc(len);
415 ac7d1ba6 Anthony Liguori
        snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
416 d271de9f Gerd Hoffmann
        bus->name = buf;
417 d271de9f Gerd Hoffmann
    } else {
418 d271de9f Gerd Hoffmann
        /* no id -> use lowercase bus type for bus name */
419 0d936928 Anthony Liguori
        len = strlen(typename) + 16;
420 7267c094 Anthony Liguori
        buf = g_malloc(len);
421 0d936928 Anthony Liguori
        len = snprintf(buf, len, "%s.%d", typename,
422 ac7d1ba6 Anthony Liguori
                       bus->parent ? bus->parent->num_child_bus : 0);
423 d271de9f Gerd Hoffmann
        for (i = 0; i < len; i++)
424 bb87ece5 Christoph Egger
            buf[i] = qemu_tolower(buf[i]);
425 d271de9f Gerd Hoffmann
        bus->name = buf;
426 d271de9f Gerd Hoffmann
    }
427 d271de9f Gerd Hoffmann
428 ac7d1ba6 Anthony Liguori
    if (bus->parent) {
429 ac7d1ba6 Anthony Liguori
        QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
430 ac7d1ba6 Anthony Liguori
        bus->parent->num_child_bus++;
431 ac7d1ba6 Anthony Liguori
        object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
432 b09995ae Paolo Bonzini
        object_unref(OBJECT(bus));
433 8185d216 Paolo Bonzini
    } else if (bus != sysbus_get_default()) {
434 80376c3f Isaku Yamahata
        /* TODO: once all bus devices are qdevified,
435 80376c3f Isaku Yamahata
           only reset handler for main_system_bus should be registered here. */
436 80376c3f Isaku Yamahata
        qemu_register_reset(qbus_reset_all_fn, bus);
437 02e2da45 Paul Brook
    }
438 cd739fb6 Gerd Hoffmann
}
439 cd739fb6 Gerd Hoffmann
440 6853d27a Paolo Bonzini
static void bus_unparent(Object *obj)
441 6853d27a Paolo Bonzini
{
442 6853d27a Paolo Bonzini
    BusState *bus = BUS(obj);
443 6853d27a Paolo Bonzini
    BusChild *kid;
444 6853d27a Paolo Bonzini
445 6853d27a Paolo Bonzini
    while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
446 6853d27a Paolo Bonzini
        DeviceState *dev = kid->child;
447 6853d27a Paolo Bonzini
        qdev_free(dev);
448 6853d27a Paolo Bonzini
    }
449 6853d27a Paolo Bonzini
    if (bus->parent) {
450 6853d27a Paolo Bonzini
        QLIST_REMOVE(bus, sibling);
451 6853d27a Paolo Bonzini
        bus->parent->num_child_bus--;
452 6853d27a Paolo Bonzini
        bus->parent = NULL;
453 6853d27a Paolo Bonzini
    } else {
454 6853d27a Paolo Bonzini
        assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
455 6853d27a Paolo Bonzini
        qemu_unregister_reset(qbus_reset_all_fn, bus);
456 6853d27a Paolo Bonzini
    }
457 6853d27a Paolo Bonzini
}
458 6853d27a Paolo Bonzini
459 39355c38 Paolo Bonzini
void qbus_create_inplace(void *bus, const char *typename,
460 0d936928 Anthony Liguori
                         DeviceState *parent, const char *name)
461 cd739fb6 Gerd Hoffmann
{
462 0d936928 Anthony Liguori
    object_initialize(bus, typename);
463 013e1182 Paolo Bonzini
    qbus_realize(bus, parent, name);
464 02e2da45 Paul Brook
}
465 cae4956e Gerd Hoffmann
466 0d936928 Anthony Liguori
BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
467 2da8bb92 Isaku Yamahata
{
468 cd739fb6 Gerd Hoffmann
    BusState *bus;
469 cd739fb6 Gerd Hoffmann
470 0d936928 Anthony Liguori
    bus = BUS(object_new(typename));
471 013e1182 Paolo Bonzini
    qbus_realize(bus, parent, name);
472 ac7d1ba6 Anthony Liguori
473 02e2da45 Paul Brook
    return bus;
474 2da8bb92 Isaku Yamahata
}
475 2da8bb92 Isaku Yamahata
476 131ec1bd Gerd Hoffmann
void qbus_free(BusState *bus)
477 131ec1bd Gerd Hoffmann
{
478 dc7389b7 Paolo Bonzini
    object_unparent(OBJECT(bus));
479 0d936928 Anthony Liguori
}
480 0d936928 Anthony Liguori
481 0d936928 Anthony Liguori
static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
482 0d936928 Anthony Liguori
{
483 0d936928 Anthony Liguori
    BusClass *bc = BUS_GET_CLASS(bus);
484 0d936928 Anthony Liguori
485 0d936928 Anthony Liguori
    if (bc->get_fw_dev_path) {
486 0d936928 Anthony Liguori
        return bc->get_fw_dev_path(dev);
487 131ec1bd Gerd Hoffmann
    }
488 0d936928 Anthony Liguori
489 0d936928 Anthony Liguori
    return NULL;
490 131ec1bd Gerd Hoffmann
}
491 131ec1bd Gerd Hoffmann
492 1ca4d09a Gleb Natapov
static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
493 1ca4d09a Gleb Natapov
{
494 1ca4d09a Gleb Natapov
    int l = 0;
495 1ca4d09a Gleb Natapov
496 1ca4d09a Gleb Natapov
    if (dev && dev->parent_bus) {
497 1ca4d09a Gleb Natapov
        char *d;
498 1ca4d09a Gleb Natapov
        l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
499 0d936928 Anthony Liguori
        d = bus_get_fw_dev_path(dev->parent_bus, dev);
500 0d936928 Anthony Liguori
        if (d) {
501 1ca4d09a Gleb Natapov
            l += snprintf(p + l, size - l, "%s", d);
502 7267c094 Anthony Liguori
            g_free(d);
503 1ca4d09a Gleb Natapov
        } else {
504 f79f2bfc Anthony Liguori
            l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
505 1ca4d09a Gleb Natapov
        }
506 1ca4d09a Gleb Natapov
    }
507 1ca4d09a Gleb Natapov
    l += snprintf(p + l , size - l, "/");
508 1ca4d09a Gleb Natapov
509 1ca4d09a Gleb Natapov
    return l;
510 1ca4d09a Gleb Natapov
}
511 1ca4d09a Gleb Natapov
512 1ca4d09a Gleb Natapov
char* qdev_get_fw_dev_path(DeviceState *dev)
513 1ca4d09a Gleb Natapov
{
514 1ca4d09a Gleb Natapov
    char path[128];
515 1ca4d09a Gleb Natapov
    int l;
516 1ca4d09a Gleb Natapov
517 1ca4d09a Gleb Natapov
    l = qdev_get_fw_dev_path_helper(dev, path, 128);
518 1ca4d09a Gleb Natapov
519 1ca4d09a Gleb Natapov
    path[l-1] = '\0';
520 1ca4d09a Gleb Natapov
521 a5cf8262 Jim Meyering
    return g_strdup(path);
522 1ca4d09a Gleb Natapov
}
523 85ed303b Anthony Liguori
524 09e5ab63 Anthony Liguori
char *qdev_get_dev_path(DeviceState *dev)
525 85ed303b Anthony Liguori
{
526 0d936928 Anthony Liguori
    BusClass *bc;
527 09e5ab63 Anthony Liguori
528 09e5ab63 Anthony Liguori
    if (!dev || !dev->parent_bus) {
529 09e5ab63 Anthony Liguori
        return NULL;
530 09e5ab63 Anthony Liguori
    }
531 09e5ab63 Anthony Liguori
532 0d936928 Anthony Liguori
    bc = BUS_GET_CLASS(dev->parent_bus);
533 0d936928 Anthony Liguori
    if (bc->get_dev_path) {
534 0d936928 Anthony Liguori
        return bc->get_dev_path(dev);
535 09e5ab63 Anthony Liguori
    }
536 09e5ab63 Anthony Liguori
537 09e5ab63 Anthony Liguori
    return NULL;
538 44677ded Anthony Liguori
}
539 a5296ca9 Anthony Liguori
540 a5296ca9 Anthony Liguori
/**
541 a5296ca9 Anthony Liguori
 * Legacy property handling
542 a5296ca9 Anthony Liguori
 */
543 a5296ca9 Anthony Liguori
544 57c9fafe Anthony Liguori
static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
545 a5296ca9 Anthony Liguori
                                     const char *name, Error **errp)
546 a5296ca9 Anthony Liguori
{
547 57c9fafe Anthony Liguori
    DeviceState *dev = DEVICE(obj);
548 a5296ca9 Anthony Liguori
    Property *prop = opaque;
549 a5296ca9 Anthony Liguori
550 e3cb6ba6 Paolo Bonzini
    char buffer[1024];
551 e3cb6ba6 Paolo Bonzini
    char *ptr = buffer;
552 a5296ca9 Anthony Liguori
553 e3cb6ba6 Paolo Bonzini
    prop->info->print(dev, prop, buffer, sizeof(buffer));
554 e3cb6ba6 Paolo Bonzini
    visit_type_str(v, &ptr, name, errp);
555 a5296ca9 Anthony Liguori
}
556 a5296ca9 Anthony Liguori
557 57c9fafe Anthony Liguori
static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
558 a5296ca9 Anthony Liguori
                                     const char *name, Error **errp)
559 a5296ca9 Anthony Liguori
{
560 57c9fafe Anthony Liguori
    DeviceState *dev = DEVICE(obj);
561 a5296ca9 Anthony Liguori
    Property *prop = opaque;
562 e3cb6ba6 Paolo Bonzini
    Error *local_err = NULL;
563 e3cb6ba6 Paolo Bonzini
    char *ptr = NULL;
564 e3cb6ba6 Paolo Bonzini
    int ret;
565 a5296ca9 Anthony Liguori
566 7983c8a3 Andreas Färber
    if (dev->realized) {
567 b000dfbd Peter Maydell
        qdev_prop_set_after_realize(dev, name, errp);
568 a5296ca9 Anthony Liguori
        return;
569 a5296ca9 Anthony Liguori
    }
570 a5296ca9 Anthony Liguori
571 e3cb6ba6 Paolo Bonzini
    visit_type_str(v, &ptr, name, &local_err);
572 e3cb6ba6 Paolo Bonzini
    if (local_err) {
573 e3cb6ba6 Paolo Bonzini
        error_propagate(errp, local_err);
574 e3cb6ba6 Paolo Bonzini
        return;
575 e3cb6ba6 Paolo Bonzini
    }
576 a5296ca9 Anthony Liguori
577 e3cb6ba6 Paolo Bonzini
    ret = prop->info->parse(dev, prop, ptr);
578 7db4c4e8 Paolo Bonzini
    error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
579 e3cb6ba6 Paolo Bonzini
    g_free(ptr);
580 a5296ca9 Anthony Liguori
}
581 a5296ca9 Anthony Liguori
582 a5296ca9 Anthony Liguori
/**
583 a5296ca9 Anthony Liguori
 * @qdev_add_legacy_property - adds a legacy property
584 a5296ca9 Anthony Liguori
 *
585 a5296ca9 Anthony Liguori
 * Do not use this is new code!  Properties added through this interface will
586 ca2cc788 Paolo Bonzini
 * be given names and types in the "legacy" namespace.
587 a5296ca9 Anthony Liguori
 *
588 68ee3569 Paolo Bonzini
 * Legacy properties are string versions of other OOM properties.  The format
589 68ee3569 Paolo Bonzini
 * of the string depends on the property type.
590 a5296ca9 Anthony Liguori
 */
591 a5296ca9 Anthony Liguori
void qdev_property_add_legacy(DeviceState *dev, Property *prop,
592 a5296ca9 Anthony Liguori
                              Error **errp)
593 a5296ca9 Anthony Liguori
{
594 ca2cc788 Paolo Bonzini
    gchar *name, *type;
595 a5296ca9 Anthony Liguori
596 f3be016d Anthony Liguori
    /* Register pointer properties as legacy properties */
597 f3be016d Anthony Liguori
    if (!prop->info->print && !prop->info->parse &&
598 f3be016d Anthony Liguori
        (prop->info->set || prop->info->get)) {
599 68ee3569 Paolo Bonzini
        return;
600 68ee3569 Paolo Bonzini
    }
601 f3be016d Anthony Liguori
602 ca2cc788 Paolo Bonzini
    name = g_strdup_printf("legacy-%s", prop->name);
603 cafe5bdb Paolo Bonzini
    type = g_strdup_printf("legacy<%s>",
604 cafe5bdb Paolo Bonzini
                           prop->info->legacy_name ?: prop->info->name);
605 a5296ca9 Anthony Liguori
606 57c9fafe Anthony Liguori
    object_property_add(OBJECT(dev), name, type,
607 68ee3569 Paolo Bonzini
                        prop->info->print ? qdev_get_legacy_property : prop->info->get,
608 68ee3569 Paolo Bonzini
                        prop->info->parse ? qdev_set_legacy_property : prop->info->set,
609 57c9fafe Anthony Liguori
                        NULL,
610 57c9fafe Anthony Liguori
                        prop, errp);
611 a5296ca9 Anthony Liguori
612 a5296ca9 Anthony Liguori
    g_free(type);
613 ca2cc788 Paolo Bonzini
    g_free(name);
614 ca2cc788 Paolo Bonzini
}
615 ca2cc788 Paolo Bonzini
616 ca2cc788 Paolo Bonzini
/**
617 ca2cc788 Paolo Bonzini
 * @qdev_property_add_static - add a @Property to a device.
618 ca2cc788 Paolo Bonzini
 *
619 ca2cc788 Paolo Bonzini
 * Static properties access data in a struct.  The actual type of the
620 ca2cc788 Paolo Bonzini
 * property and the field depends on the property type.
621 ca2cc788 Paolo Bonzini
 */
622 ca2cc788 Paolo Bonzini
void qdev_property_add_static(DeviceState *dev, Property *prop,
623 ca2cc788 Paolo Bonzini
                              Error **errp)
624 ca2cc788 Paolo Bonzini
{
625 fdae245f Paolo Bonzini
    Error *local_err = NULL;
626 fdae245f Paolo Bonzini
    Object *obj = OBJECT(dev);
627 fdae245f Paolo Bonzini
628 d822979b Paolo Bonzini
    /*
629 d822979b Paolo Bonzini
     * TODO qdev_prop_ptr does not have getters or setters.  It must
630 d822979b Paolo Bonzini
     * go now that it can be replaced with links.  The test should be
631 d822979b Paolo Bonzini
     * removed along with it: all static properties are read/write.
632 d822979b Paolo Bonzini
     */
633 d822979b Paolo Bonzini
    if (!prop->info->get && !prop->info->set) {
634 d822979b Paolo Bonzini
        return;
635 d822979b Paolo Bonzini
    }
636 d822979b Paolo Bonzini
637 fdae245f Paolo Bonzini
    object_property_add(obj, prop->name, prop->info->name,
638 57c9fafe Anthony Liguori
                        prop->info->get, prop->info->set,
639 dd0ba250 Paolo Bonzini
                        prop->info->release,
640 fdae245f Paolo Bonzini
                        prop, &local_err);
641 fdae245f Paolo Bonzini
642 fdae245f Paolo Bonzini
    if (local_err) {
643 fdae245f Paolo Bonzini
        error_propagate(errp, local_err);
644 fdae245f Paolo Bonzini
        return;
645 fdae245f Paolo Bonzini
    }
646 fdae245f Paolo Bonzini
    if (prop->qtype == QTYPE_NONE) {
647 fdae245f Paolo Bonzini
        return;
648 fdae245f Paolo Bonzini
    }
649 fdae245f Paolo Bonzini
650 fdae245f Paolo Bonzini
    if (prop->qtype == QTYPE_QBOOL) {
651 fdae245f Paolo Bonzini
        object_property_set_bool(obj, prop->defval, prop->name, &local_err);
652 fdae245f Paolo Bonzini
    } else if (prop->info->enum_table) {
653 fdae245f Paolo Bonzini
        object_property_set_str(obj, prop->info->enum_table[prop->defval],
654 fdae245f Paolo Bonzini
                                prop->name, &local_err);
655 fdae245f Paolo Bonzini
    } else if (prop->qtype == QTYPE_QINT) {
656 fdae245f Paolo Bonzini
        object_property_set_int(obj, prop->defval, prop->name, &local_err);
657 fdae245f Paolo Bonzini
    }
658 fdae245f Paolo Bonzini
    assert_no_error(local_err);
659 6a146eba Anthony Liguori
}
660 1de81d28 Anthony Liguori
661 249d4172 Andreas Färber
static bool device_get_realized(Object *obj, Error **err)
662 249d4172 Andreas Färber
{
663 249d4172 Andreas Färber
    DeviceState *dev = DEVICE(obj);
664 249d4172 Andreas Färber
    return dev->realized;
665 249d4172 Andreas Färber
}
666 249d4172 Andreas Färber
667 249d4172 Andreas Färber
static void device_set_realized(Object *obj, bool value, Error **err)
668 249d4172 Andreas Färber
{
669 249d4172 Andreas Färber
    DeviceState *dev = DEVICE(obj);
670 249d4172 Andreas Färber
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
671 249d4172 Andreas Färber
    Error *local_err = NULL;
672 249d4172 Andreas Färber
673 249d4172 Andreas Färber
    if (value && !dev->realized) {
674 249d4172 Andreas Färber
        if (dc->realize) {
675 249d4172 Andreas Färber
            dc->realize(dev, &local_err);
676 249d4172 Andreas Färber
        }
677 249d4172 Andreas Färber
678 249d4172 Andreas Färber
        if (!obj->parent && local_err == NULL) {
679 249d4172 Andreas Färber
            static int unattached_count;
680 249d4172 Andreas Färber
            gchar *name = g_strdup_printf("device[%d]", unattached_count++);
681 249d4172 Andreas Färber
682 249d4172 Andreas Färber
            object_property_add_child(container_get(qdev_get_machine(),
683 249d4172 Andreas Färber
                                                    "/unattached"),
684 249d4172 Andreas Färber
                                      name, obj, &local_err);
685 249d4172 Andreas Färber
            g_free(name);
686 249d4172 Andreas Färber
        }
687 249d4172 Andreas Färber
688 249d4172 Andreas Färber
        if (qdev_get_vmsd(dev) && local_err == NULL) {
689 249d4172 Andreas Färber
            vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
690 249d4172 Andreas Färber
                                           dev->instance_id_alias,
691 249d4172 Andreas Färber
                                           dev->alias_required_for_version);
692 249d4172 Andreas Färber
        }
693 249d4172 Andreas Färber
        if (dev->hotplugged && local_err == NULL) {
694 249d4172 Andreas Färber
            device_reset(dev);
695 249d4172 Andreas Färber
        }
696 249d4172 Andreas Färber
    } else if (!value && dev->realized) {
697 249d4172 Andreas Färber
        if (dc->unrealize) {
698 249d4172 Andreas Färber
            dc->unrealize(dev, &local_err);
699 249d4172 Andreas Färber
        }
700 249d4172 Andreas Färber
    }
701 249d4172 Andreas Färber
702 249d4172 Andreas Färber
    if (local_err != NULL) {
703 249d4172 Andreas Färber
        error_propagate(err, local_err);
704 249d4172 Andreas Färber
        return;
705 249d4172 Andreas Färber
    }
706 249d4172 Andreas Färber
707 249d4172 Andreas Färber
    dev->realized = value;
708 249d4172 Andreas Färber
}
709 249d4172 Andreas Färber
710 9674bfe4 Anthony Liguori
static void device_initfn(Object *obj)
711 9674bfe4 Anthony Liguori
{
712 9674bfe4 Anthony Liguori
    DeviceState *dev = DEVICE(obj);
713 bce54474 Paolo Bonzini
    ObjectClass *class;
714 9674bfe4 Anthony Liguori
    Property *prop;
715 e769bdc2 Peter Maydell
    Error *err = NULL;
716 9674bfe4 Anthony Liguori
717 9674bfe4 Anthony Liguori
    if (qdev_hotplug) {
718 9674bfe4 Anthony Liguori
        dev->hotplugged = 1;
719 9674bfe4 Anthony Liguori
        qdev_hot_added = true;
720 9674bfe4 Anthony Liguori
    }
721 9674bfe4 Anthony Liguori
722 9674bfe4 Anthony Liguori
    dev->instance_id_alias = -1;
723 7983c8a3 Andreas Färber
    dev->realized = false;
724 9674bfe4 Anthony Liguori
725 249d4172 Andreas Färber
    object_property_add_bool(obj, "realized",
726 249d4172 Andreas Färber
                             device_get_realized, device_set_realized, NULL);
727 249d4172 Andreas Färber
728 bce54474 Paolo Bonzini
    class = object_get_class(OBJECT(dev));
729 bce54474 Paolo Bonzini
    do {
730 bce54474 Paolo Bonzini
        for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
731 e769bdc2 Peter Maydell
            qdev_property_add_legacy(dev, prop, &err);
732 e769bdc2 Peter Maydell
            assert_no_error(err);
733 e769bdc2 Peter Maydell
            qdev_property_add_static(dev, prop, &err);
734 e769bdc2 Peter Maydell
            assert_no_error(err);
735 bce54474 Paolo Bonzini
        }
736 bce54474 Paolo Bonzini
        class = object_class_get_parent(class);
737 bce54474 Paolo Bonzini
    } while (class != object_class_by_name(TYPE_DEVICE));
738 4b3582b0 Paolo Bonzini
    qdev_prop_set_globals(dev);
739 9674bfe4 Anthony Liguori
740 f968fc68 Anthony Liguori
    object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
741 e769bdc2 Peter Maydell
                             (Object **)&dev->parent_bus, &err);
742 e769bdc2 Peter Maydell
    assert_no_error(err);
743 9674bfe4 Anthony Liguori
}
744 9674bfe4 Anthony Liguori
745 60adba37 Anthony Liguori
/* Unlink device from bus and free the structure.  */
746 60adba37 Anthony Liguori
static void device_finalize(Object *obj)
747 60adba37 Anthony Liguori
{
748 60adba37 Anthony Liguori
    DeviceState *dev = DEVICE(obj);
749 06f7f2bb Paolo Bonzini
    if (dev->opts) {
750 06f7f2bb Paolo Bonzini
        qemu_opts_del(dev->opts);
751 60adba37 Anthony Liguori
    }
752 60adba37 Anthony Liguori
}
753 60adba37 Anthony Liguori
754 bce54474 Paolo Bonzini
static void device_class_base_init(ObjectClass *class, void *data)
755 bce54474 Paolo Bonzini
{
756 bce54474 Paolo Bonzini
    DeviceClass *klass = DEVICE_CLASS(class);
757 bce54474 Paolo Bonzini
758 bce54474 Paolo Bonzini
    /* We explicitly look up properties in the superclasses,
759 bce54474 Paolo Bonzini
     * so do not propagate them to the subclasses.
760 bce54474 Paolo Bonzini
     */
761 bce54474 Paolo Bonzini
    klass->props = NULL;
762 60adba37 Anthony Liguori
}
763 60adba37 Anthony Liguori
764 5d5b24d0 Andreas Färber
static void device_unparent(Object *obj)
765 667d22d1 Paolo Bonzini
{
766 667d22d1 Paolo Bonzini
    DeviceState *dev = DEVICE(obj);
767 06f7f2bb Paolo Bonzini
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
768 06f7f2bb Paolo Bonzini
    BusState *bus;
769 0402a5d6 Michael S. Tsirkin
    QObject *event_data;
770 b1ee5829 Anthony Liguori
    bool have_realized = dev->realized;
771 667d22d1 Paolo Bonzini
772 06f7f2bb Paolo Bonzini
    while (dev->num_child_bus) {
773 06f7f2bb Paolo Bonzini
        bus = QLIST_FIRST(&dev->child_bus);
774 06f7f2bb Paolo Bonzini
        qbus_free(bus);
775 06f7f2bb Paolo Bonzini
    }
776 06f7f2bb Paolo Bonzini
    if (dev->realized) {
777 06f7f2bb Paolo Bonzini
        if (qdev_get_vmsd(dev)) {
778 06f7f2bb Paolo Bonzini
            vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
779 06f7f2bb Paolo Bonzini
        }
780 06f7f2bb Paolo Bonzini
        if (dc->exit) {
781 06f7f2bb Paolo Bonzini
            dc->exit(dev);
782 06f7f2bb Paolo Bonzini
        }
783 06f7f2bb Paolo Bonzini
    }
784 06f7f2bb Paolo Bonzini
    if (dev->parent_bus) {
785 5d5b24d0 Andreas Färber
        bus_remove_child(dev->parent_bus, dev);
786 62d7ba66 Paolo Bonzini
        object_unref(OBJECT(dev->parent_bus));
787 62d7ba66 Paolo Bonzini
        dev->parent_bus = NULL;
788 5d5b24d0 Andreas Färber
    }
789 0402a5d6 Michael S. Tsirkin
790 b1ee5829 Anthony Liguori
    /* Only send event if the device had been completely realized */
791 b1ee5829 Anthony Liguori
    if (have_realized) {
792 b1ee5829 Anthony Liguori
        gchar *path = object_get_canonical_path(OBJECT(dev));
793 b1ee5829 Anthony Liguori
794 b1ee5829 Anthony Liguori
        if (dev->id) {
795 b1ee5829 Anthony Liguori
            event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
796 b1ee5829 Anthony Liguori
                                            dev->id, path);
797 b1ee5829 Anthony Liguori
        } else {
798 b1ee5829 Anthony Liguori
            event_data = qobject_from_jsonf("{ 'path': %s }", path);
799 b1ee5829 Anthony Liguori
        }
800 b1ee5829 Anthony Liguori
        monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
801 b1ee5829 Anthony Liguori
        qobject_decref(event_data);
802 b1ee5829 Anthony Liguori
        g_free(path);
803 0402a5d6 Michael S. Tsirkin
    }
804 667d22d1 Paolo Bonzini
}
805 667d22d1 Paolo Bonzini
806 667d22d1 Paolo Bonzini
static void device_class_init(ObjectClass *class, void *data)
807 667d22d1 Paolo Bonzini
{
808 249d4172 Andreas Färber
    DeviceClass *dc = DEVICE_CLASS(class);
809 249d4172 Andreas Färber
810 5d5b24d0 Andreas Färber
    class->unparent = device_unparent;
811 249d4172 Andreas Färber
    dc->realize = device_realize;
812 667d22d1 Paolo Bonzini
}
813 667d22d1 Paolo Bonzini
814 94afdadc Anthony Liguori
void device_reset(DeviceState *dev)
815 94afdadc Anthony Liguori
{
816 94afdadc Anthony Liguori
    DeviceClass *klass = DEVICE_GET_CLASS(dev);
817 94afdadc Anthony Liguori
818 94afdadc Anthony Liguori
    if (klass->reset) {
819 94afdadc Anthony Liguori
        klass->reset(dev);
820 94afdadc Anthony Liguori
    }
821 94afdadc Anthony Liguori
}
822 94afdadc Anthony Liguori
823 f05f6b4a Paolo Bonzini
Object *qdev_get_machine(void)
824 f05f6b4a Paolo Bonzini
{
825 f05f6b4a Paolo Bonzini
    static Object *dev;
826 f05f6b4a Paolo Bonzini
827 f05f6b4a Paolo Bonzini
    if (dev == NULL) {
828 dfe47e70 Andreas Färber
        dev = container_get(object_get_root(), "/machine");
829 f05f6b4a Paolo Bonzini
    }
830 f05f6b4a Paolo Bonzini
831 f05f6b4a Paolo Bonzini
    return dev;
832 f05f6b4a Paolo Bonzini
}
833 f05f6b4a Paolo Bonzini
834 8c43a6f0 Andreas Färber
static const TypeInfo device_type_info = {
835 32fea402 Anthony Liguori
    .name = TYPE_DEVICE,
836 32fea402 Anthony Liguori
    .parent = TYPE_OBJECT,
837 32fea402 Anthony Liguori
    .instance_size = sizeof(DeviceState),
838 9674bfe4 Anthony Liguori
    .instance_init = device_initfn,
839 60adba37 Anthony Liguori
    .instance_finalize = device_finalize,
840 bce54474 Paolo Bonzini
    .class_base_init = device_class_base_init,
841 667d22d1 Paolo Bonzini
    .class_init = device_class_init,
842 32fea402 Anthony Liguori
    .abstract = true,
843 32fea402 Anthony Liguori
    .class_size = sizeof(DeviceClass),
844 32fea402 Anthony Liguori
};
845 32fea402 Anthony Liguori
846 ac7d1ba6 Anthony Liguori
static void qbus_initfn(Object *obj)
847 ac7d1ba6 Anthony Liguori
{
848 ac7d1ba6 Anthony Liguori
    BusState *bus = BUS(obj);
849 ac7d1ba6 Anthony Liguori
850 ac7d1ba6 Anthony Liguori
    QTAILQ_INIT(&bus->children);
851 ac7d1ba6 Anthony Liguori
}
852 ac7d1ba6 Anthony Liguori
853 6853d27a Paolo Bonzini
static void bus_class_init(ObjectClass *class, void *data)
854 6853d27a Paolo Bonzini
{
855 6853d27a Paolo Bonzini
    class->unparent = bus_unparent;
856 6853d27a Paolo Bonzini
}
857 6853d27a Paolo Bonzini
858 ac7d1ba6 Anthony Liguori
static void qbus_finalize(Object *obj)
859 ac7d1ba6 Anthony Liguori
{
860 ac7d1ba6 Anthony Liguori
    BusState *bus = BUS(obj);
861 ac7d1ba6 Anthony Liguori
862 ac7d1ba6 Anthony Liguori
    g_free((char *)bus->name);
863 ac7d1ba6 Anthony Liguori
}
864 ac7d1ba6 Anthony Liguori
865 0d936928 Anthony Liguori
static const TypeInfo bus_info = {
866 0d936928 Anthony Liguori
    .name = TYPE_BUS,
867 0d936928 Anthony Liguori
    .parent = TYPE_OBJECT,
868 0d936928 Anthony Liguori
    .instance_size = sizeof(BusState),
869 0d936928 Anthony Liguori
    .abstract = true,
870 0d936928 Anthony Liguori
    .class_size = sizeof(BusClass),
871 ac7d1ba6 Anthony Liguori
    .instance_init = qbus_initfn,
872 ac7d1ba6 Anthony Liguori
    .instance_finalize = qbus_finalize,
873 6853d27a Paolo Bonzini
    .class_init = bus_class_init,
874 0d936928 Anthony Liguori
};
875 0d936928 Anthony Liguori
876 83f7d43a Andreas Färber
static void qdev_register_types(void)
877 32fea402 Anthony Liguori
{
878 0d936928 Anthony Liguori
    type_register_static(&bus_info);
879 32fea402 Anthony Liguori
    type_register_static(&device_type_info);
880 32fea402 Anthony Liguori
}
881 32fea402 Anthony Liguori
882 83f7d43a Andreas Färber
type_init(qdev_register_types)