Statistics
| Branch: | Revision:

root / hw / qdev.c @ 7e0a9247

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