Statistics
| Branch: | Revision:

root / hw / qdev.c @ 9e2c1298

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